[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/org d048c153dd 08/44: org-string-width: Reimplement to
From: |
ELPA Syncer |
Subject: |
[elpa] externals/org d048c153dd 08/44: org-string-width: Reimplement to work with new folding |
Date: |
Mon, 25 Apr 2022 07:57:55 -0400 (EDT) |
branch: externals/org
commit d048c153dd67a118eaa21f04a7faef2800bba03a
Author: Ihor Radchenko <yantar92@gmail.com>
Commit: Ihor Radchenko <yantar92@gmail.com>
org-string-width: Reimplement to work with new folding
* lisp/org-macs.el (org--string-from-props): Removed since it is no
longer needed.
(org-string-width): Updated to use `window-text-pixel-size'.
---
lisp/org-macs.el | 121 ++++++++++++++++++++++++++-----------------------------
1 file changed, 57 insertions(+), 64 deletions(-)
diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 6161a7bfc6..f63458f703 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -889,71 +889,64 @@ end of string are ignored."
results ;skip trailing separator
(cons (substring string i) results)))))))
-(defun org--string-from-props (s property beg end)
- "Return the visible part of string S.
-Visible part is determined according to text PROPERTY, which is
-either `invisible' or `display'. BEG and END are 0-indices
-delimiting S."
- (let ((width 0)
- (cursor beg))
- (while (setq beg (text-property-not-all beg end property nil s))
- (let* ((next (next-single-property-change beg property s end))
- (props (text-properties-at beg s))
- (spec (plist-get props property))
- (value
- (pcase property
- (`invisible
- ;; If `invisible' property in PROPS means text is to
- ;; be invisible, return 0. Otherwise return nil so
- ;; as to resume search.
- (and (or (eq t buffer-invisibility-spec)
- (assoc-string spec buffer-invisibility-spec))
- 0))
- (`display
- (pcase spec
- (`nil nil)
- (`(space . ,props)
- (let ((width (plist-get props :width)))
- (and (wholenump width) width)))
- (`(image . ,_)
- (and (fboundp 'image-size)
- (ceiling (car (image-size spec)))))
- ((pred stringp)
- ;; Displayed string could contain invisible parts,
- ;; but no nested display.
- (org--string-from-props spec 'invisible 0 (length spec)))
- (_
- ;; Un-handled `display' value. Ignore it.
- ;; Consider the original string instead.
- nil)))
- (_ (error "Unknown property: %S" property)))))
- (when value
- (cl-incf width
- ;; When looking for `display' parts, we still need
- ;; to look for `invisible' property elsewhere.
- (+ (cond ((eq property 'display)
- (org--string-from-props s 'invisible cursor beg))
- ((= cursor beg) 0)
- (t (string-width (substring s cursor beg))))
- value))
- (setq cursor next))
- (setq beg next)))
- (+ width
- ;; Look for `invisible' property in the last part of the
- ;; string. See above.
- (cond ((eq property 'display)
- (org--string-from-props s 'invisible cursor end))
- ((= cursor end) 0)
- (t (string-width (substring s cursor end)))))))
-
-(defun org-string-width (string)
+(defun org-string-width (string &optional pixels)
"Return width of STRING when displayed in the current buffer.
-Unlike `string-width', this function takes into consideration
-`invisible' and `display' text properties. It supports the
-latter in a limited way, mostly for combinations used in Org.
-Results may be off sometimes if it cannot handle a given
-`display' value."
- (org--string-from-props string 'display 0 (length string)))
+Return width in pixels when PIXELS is non-nil."
+ ;; Wrap/line prefix will make `window-text-pizel-size' return too
+ ;; large value including the prefix.
+ ;; Face should be removed to make sure that all the string symbols
+ ;; are using default face with constant width. Constant char width
+ ;; is critical to get right string width from pixel width.
+ (remove-text-properties 0 (length string)
+ '(wrap-prefix t line-prefix t face t)
+ string)
+ (let (;; We need to remove the folds to make sure that folded table
+ ;; alignment is not messed up.
+ (current-invisibility-spec
+ (or (and (not (listp buffer-invisibility-spec))
+ buffer-invisibility-spec)
+ (let (result)
+ (dolist (el buffer-invisibility-spec)
+ (unless (or (memq el
+ '(org-fold-drawer
+ org-fold-block
+ org-fold-outline))
+ (and (listp el)
+ (memq (car el)
+ '(org-fold-drawer
+ org-fold-block
+ org-fold-outline))))
+ (push el result)))
+ result)))
+ (current-char-property-alias-alist char-property-alias-alist))
+ (with-temp-buffer
+ (setq-local display-line-numbers nil)
+ (setq-local buffer-invisibility-spec
+ current-invisibility-spec)
+ (setq-local char-property-alias-alist
+ current-char-property-alias-alist)
+ (let (pixel-width symbol-width)
+ (with-silent-modifications
+ (setf (buffer-string) string)
+ (setq pixel-width
+ (if (get-buffer-window (current-buffer))
+ (car (window-text-pixel-size
+ nil (line-beginning-position) (point-max)))
+ (set-window-buffer nil (current-buffer))
+ (car (window-text-pixel-size
+ nil (line-beginning-position) (point-max)))))
+ (unless pixels
+ (setf (buffer-string) "a")
+ (setq symbol-width
+ (if (get-buffer-window (current-buffer))
+ (car (window-text-pixel-size
+ nil (line-beginning-position) (point-max)))
+ (set-window-buffer nil (current-buffer))
+ (car (window-text-pixel-size
+ nil (line-beginning-position) (point-max)))))))
+ (if pixels
+ pixel-width
+ (/ pixel-width symbol-width))))))
(defun org-not-nil (v)
"If V not nil, and also not the string \"nil\", then return V.
- [elpa] externals/org 407104459b 44/44: org-fold: Honour `org-fold-show-context-detail' for isearch, (continued)
- [elpa] externals/org 407104459b 44/44: org-fold: Honour `org-fold-show-context-detail' for isearch, ELPA Syncer, 2022/04/25
- [elpa] externals/org 219bc6c2d3 37/44: org-fold-core-fontify-region: Fix cases when fontification is not registered, ELPA Syncer, 2022/04/25
- [elpa] externals/org a6eab82fd6 16/44: Obsolete old function names that are now in org-fold, ELPA Syncer, 2022/04/25
- [elpa] externals/org f813f10818 26/44: Rename remaining org-force-cycle-archived → org-cycle-force-archived, ELPA Syncer, 2022/04/25
- [elpa] externals/org 2e3566e1e9 28/44: org-string-width: Handle undefined behaviour in older Emacs, ELPA Syncer, 2022/04/25
- [elpa] externals/org 240a14988f 24/44: Fix typo: delete-duplicates → delete-dups, ELPA Syncer, 2022/04/25
- [elpa] externals/org 52a0ef94c9 23/44: Backport contributed commits, ELPA Syncer, 2022/04/25
- [elpa] externals/org baffebbc33 25/44: Fix bug in org-get-heading, ELPA Syncer, 2022/04/25
- [elpa] externals/org e8d8db63a0 19/44: Restore old visibility behaviour of org-refile, ELPA Syncer, 2022/04/25
- [elpa] externals/org 3da1b2d240 31/44: org-fold-core: Fix fontification inside folded regions, ELPA Syncer, 2022/04/25
- [elpa] externals/org d048c153dd 08/44: org-string-width: Reimplement to work with new folding,
ELPA Syncer <=
- [elpa] externals/org fa7530c7b4 09/44: Rename old function call to use org-fold, ELPA Syncer, 2022/04/25
- [elpa] externals/org 312f39a05d 41/44: org-string-width: Do not remove face info when PIXELS is non-nil, ELPA Syncer, 2022/04/25
- [elpa] externals/org 0bca8bc418 30/44: org-fold-show-set-visibility: Fix edge case when folded region is at BOB, ELPA Syncer, 2022/04/25
- [elpa] externals/org a8f4270ac1 21/44: org-manual: Update to new org-fold function names, ELPA Syncer, 2022/04/25
- [elpa] externals/org 0daa209a74 29/44: org-string-width: Work around `window-pixel-width' bug in old Emacs, ELPA Syncer, 2022/04/25
- [elpa] externals/org d2775da08d 35/44: test-ol/org-toggle-link-display: Fix compatibility with old Emacs, ELPA Syncer, 2022/04/25
- [elpa] externals/org 24c1c877cf 43/44: org-fold-core-fontify-region: Calculate jit-lock-bounds accurately, ELPA Syncer, 2022/04/25
- [elpa] externals/org 17e762426d 18/44: Move `org-buffer-list' to org-macs.el, ELPA Syncer, 2022/04/25
- [elpa] externals/org 3fba34900e 27/44: Fix org-fold--hide-drawers--overlays, ELPA Syncer, 2022/04/25
- [elpa] externals/org fc6314b267 39/44: org-fold.el: Fix typo, ELPA Syncer, 2022/04/25