diff options
Diffstat (limited to '')
| -rw-r--r-- | lisp/emacs-lisp/subr-x.el | 73 |
1 files changed, 37 insertions, 36 deletions
diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el index db854863b32..b36b14b9b50 100644 --- a/lisp/emacs-lisp/subr-x.el +++ b/lisp/emacs-lisp/subr-x.el | |||
| @@ -419,44 +419,45 @@ can be used to avoid the cost of recomputing this for multiple calls to | |||
| 419 | this function using the same ELLIPSIS." | 419 | this function using the same ELLIPSIS." |
| 420 | (declare (important-return-value t)) | 420 | (declare (important-return-value t)) |
| 421 | (if (zerop (length string)) | 421 | (if (zerop (length string)) |
| 422 | 0 | 422 | string |
| 423 | ;; Keeping a work buffer around is more efficient than creating a | 423 | ;; Keeping a work buffer around is more efficient than creating a |
| 424 | ;; new temporary buffer. | 424 | ;; new temporary buffer. |
| 425 | (with-work-buffer | 425 | (let ((original-buffer (or buffer (current-buffer)))) |
| 426 | (work-buffer--prepare-pixelwise string buffer) | 426 | (with-work-buffer |
| 427 | (set-window-buffer nil (current-buffer) 'keep-margins) | 427 | (work-buffer--prepare-pixelwise string buffer) |
| 428 | ;; Use a binary search to prune the number of calls to | 428 | (set-window-buffer nil (current-buffer) 'keep-margins) |
| 429 | ;; `window-text-pixel-size'. | 429 | ;; Use a binary search to prune the number of calls to |
| 430 | ;; These are 1-based buffer indexes. | 430 | ;; `window-text-pixel-size'. |
| 431 | (let* ((low 1) | 431 | ;; These are 1-based buffer indexes. |
| 432 | (high (1+ (length string))) | 432 | (let* ((low 1) |
| 433 | mid) | 433 | (high (1+ (length string))) |
| 434 | (when (> (car (window-text-pixel-size nil 1 high)) max-pixels) | 434 | mid) |
| 435 | (when (and ellipsis (not (stringp ellipsis))) | 435 | (when (> (car (window-text-pixel-size nil 1 high)) max-pixels) |
| 436 | (setq ellipsis (truncate-string-ellipsis))) | 436 | (when (and ellipsis (not (stringp ellipsis))) |
| 437 | (setq ellipsis-pixels (if ellipsis | 437 | (setq ellipsis (truncate-string-ellipsis))) |
| 438 | (if ellipsis-pixels | 438 | (setq ellipsis-pixels (if ellipsis |
| 439 | ellipsis-pixels | 439 | (if ellipsis-pixels |
| 440 | (string-pixel-width ellipsis buffer)) | 440 | ellipsis-pixels |
| 441 | 0)) | 441 | (string-pixel-width ellipsis buffer)) |
| 442 | (let ((adjusted-pixels | 442 | 0)) |
| 443 | (if (> max-pixels ellipsis-pixels) | 443 | (let ((adjusted-pixels |
| 444 | (- max-pixels ellipsis-pixels) | 444 | (if (> max-pixels ellipsis-pixels) |
| 445 | max-pixels))) | 445 | (- max-pixels ellipsis-pixels) |
| 446 | (while (<= low high) | 446 | max-pixels))) |
| 447 | (setq mid (floor (+ low high) 2)) | 447 | (while (<= low high) |
| 448 | (if (<= (car (window-text-pixel-size nil 1 mid)) | 448 | (setq mid (floor (+ low high) 2)) |
| 449 | adjusted-pixels) | 449 | (if (<= (car (window-text-pixel-size nil 1 mid)) |
| 450 | (setq low (1+ mid)) | 450 | adjusted-pixels) |
| 451 | (setq high (1- mid)))))) | 451 | (setq low (1+ mid)) |
| 452 | (set-window-buffer nil buffer 'keep-margins) | 452 | (setq high (1- mid)))))) |
| 453 | (if mid | 453 | (set-window-buffer nil original-buffer 'keep-margins) |
| 454 | ;; Binary search ran. | 454 | (if mid |
| 455 | (if (and ellipsis (> max-pixels ellipsis-pixels)) | 455 | ;; Binary search ran. |
| 456 | (concat (substring string 0 (1- high)) ellipsis) | 456 | (if (and ellipsis (> max-pixels ellipsis-pixels)) |
| 457 | (substring string 0 (1- high))) | 457 | (concat (substring string 0 (1- high)) ellipsis) |
| 458 | ;; Fast path. | 458 | (substring string 0 (1- high))) |
| 459 | string))))) | 459 | ;; Fast path. |
| 460 | string)))))) | ||
| 460 | 461 | ||
| 461 | ;;;###autoload | 462 | ;;;###autoload |
| 462 | (defun string-glyph-split (string) | 463 | (defun string-glyph-split (string) |