diff options
Diffstat (limited to 'lisp/emacs-lisp/subr-x.el')
-rw-r--r-- | lisp/emacs-lisp/subr-x.el | 19 |
1 files changed, 19 insertions, 0 deletions
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 |