forked from mirrors/org-mode
org-string-width: Fix Emacs <29 compatibility
* lisp/org-compat.el (org-buffer-text-pixel-width): New function using `buffer-text-pixel-size' in Emacs >=29 and falling back to `window-text-pixel-size' in older Emacs. * lisp/org-macs.el (org-string-width): Use the new function.
This commit is contained in:
parent
f5467b53ec
commit
11ce194dcf
|
@ -137,6 +137,27 @@ Upper-case and lower-case letters are treated as equal.
|
|||
Unibyte strings are converted to multibyte for comparison."
|
||||
(eq t (compare-strings string1 0 nil string2 0 nil t))))
|
||||
|
||||
(defun org-buffer-text-pixel-width ()
|
||||
"Return pixel width of text in current buffer.
|
||||
This function uses `buffer-text-pixel-size', when available, and falls
|
||||
back to `window-text-pixel-size' otherwise."
|
||||
(if (fboundp 'buffer-text-pixel-size)
|
||||
(car (buffer-text-pixel-size nil nil t))
|
||||
(if (get-buffer-window (current-buffer))
|
||||
(car (window-text-pixel-size
|
||||
nil (point-min) (point-max)))
|
||||
(let ((dedicatedp (window-dedicated-p))
|
||||
(oldbuffer (window-buffer)))
|
||||
(unwind-protect
|
||||
(progn
|
||||
;; Do not throw error in dedicated windows.
|
||||
(set-window-dedicated-p nil nil)
|
||||
(set-window-buffer nil (current-buffer))
|
||||
(car (window-text-pixel-size
|
||||
nil (point-min) (point-max))))
|
||||
(set-window-buffer nil oldbuffer)
|
||||
(set-window-dedicated-p nil dedicatedp))))))
|
||||
|
||||
|
||||
;;; Emacs < 28.1 compatibility
|
||||
|
||||
|
|
|
@ -1136,11 +1136,11 @@ Return width in pixels when PIXELS is non-nil."
|
|||
(with-silent-modifications
|
||||
(erase-buffer)
|
||||
(insert string)
|
||||
(setq pixel-width (car (buffer-text-pixel-size nil nil t)))
|
||||
(setq pixel-width (org-buffer-text-pixel-width))
|
||||
(unless pixels
|
||||
(erase-buffer)
|
||||
(insert "a")
|
||||
(setq symbol-width (car (buffer-text-pixel-size nil nil t)))))
|
||||
(setq symbol-width (org-buffer-text-pixel-width))))
|
||||
(if pixels
|
||||
pixel-width
|
||||
(/ pixel-width symbol-width)))))))
|
||||
|
|
Loading…
Reference in New Issue