diff options
author | Lars Ingebrigtsen <larsi@gnus.org> | 2021-10-27 15:41:18 +0200 |
---|---|---|
committer | Lars Ingebrigtsen <larsi@gnus.org> | 2021-10-27 15:41:32 +0200 |
commit | 03366de3948225476545d891c584f7d30c497bd0 (patch) | |
tree | d8e5b3a92e950a4edd32f48c76340f26828f1368 /lisp/emacs-lisp | |
parent | 3fac3120f8ba7941bac89fa90f30140492fdf0eb (diff) | |
download | emacs-03366de3948225476545d891c584f7d30c497bd0.tar.gz emacs-03366de3948225476545d891c584f7d30c497bd0.tar.bz2 emacs-03366de3948225476545d891c584f7d30c497bd0.zip |
Add new function 'string-pixel-width'
* doc/lispref/display.texi (Size of Displayed Text): Mention it.
* lisp/emacs-lisp/shortdoc.el (string): Mention it.
* lisp/emacs-lisp/subr-x.el (string-pixel-width): New function.
Diffstat (limited to 'lisp/emacs-lisp')
-rw-r--r-- | lisp/emacs-lisp/shortdoc.el | 9 | ||||
-rw-r--r-- | lisp/emacs-lisp/subr-x.el | 19 |
2 files changed, 27 insertions, 1 deletions
diff --git a/lisp/emacs-lisp/shortdoc.el b/lisp/emacs-lisp/shortdoc.el index 8f654372079..817dfa6b71e 100644 --- a/lisp/emacs-lisp/shortdoc.el +++ b/lisp/emacs-lisp/shortdoc.el @@ -242,7 +242,14 @@ There can be any number of :example/:result elements." :eval (number-to-string 42)) "Data About Strings" (length - :eval (length "foo")) + :eval (length "foo") + :eval (length "avocado: 🥑")) + (string-width + :eval (string-width "foo") + :eval (string-width "avocado: 🥑")) + (string-pixel-width + :eval (string-pixel-width "foo") + :eval (string-pixel-width "avocado: 🥑")) (string-search :eval (string-search "bar" "foobarzot")) (assoc-string diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index 8d6bb19fd49..6f01209574d 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el @@ -441,6 +441,25 @@ is inserted before adjusting the number of empty lines." ((< (- (point) start) lines) (insert (make-string (- lines (- (point) start)) ?\n)))))) +;;;###autoload +(defun string-pixel-width (string) + "Return the width of STRING in pixels." + (with-temp-buffer + (insert string) + (save-window-excursion + (let ((dedicated (window-dedicated-p))) + ;; Avoid errors if the selected window is a dedicated one, + ;; and they just want to insert a document into it. + (unwind-protect + (progn + (when dedicated + (set-window-dedicated-p nil nil)) + (set-window-buffer nil (current-buffer)) + (car (window-text-pixel-size + nil (line-beginning-position) (point)))) + (when dedicated + (set-window-dedicated-p nil dedicated))))))) + (provide 'subr-x) ;;; subr-x.el ends here |