branch: externals-release/org
commit 15c519b84cf8305011293969f64c11230420ad50
Author: Ihor Radchenko <yanta...@posteo.net>
Commit: Ihor Radchenko <yanta...@posteo.net>

    Do not assume that `org-string-width' preserves match data
    
    * lisp/org-table.el (org-table-blank-field):
    (org-table-clean-line): Avoid `org-string-width' modifying match data.
    
    As long as `org-string-width' creates a new window buffer, third-party
    modes and Emacs distributions might modify the match data in buffer
    hooks.  Hence, we cannot assume that match data is not modified even
    though `org-string-width' code itself does not alter the match data.
    
    Note: Adding `save-match-data' around function calls is generally a
    good practice when the surrounding code relies on the match data being
    intact.  Elisp conventions don't prohibit functions to modify match
    data without notice in their docstring.
    
    Reported-by: Thomas Schneider <q...@chaotikum.eu>
    Link: https://orgmode.org/list/wwufsc7edzu....@chaotikum.eu
---
 lisp/org-table.el | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/lisp/org-table.el b/lisp/org-table.el
index fac9e68c12..5116b1127f 100644
--- a/lisp/org-table.el
+++ b/lisp/org-table.el
@@ -1229,7 +1229,7 @@ Return t when the line exists, nil if it does not exist."
     (if (looking-at "|[^|\n]+")
        (let* ((pos (match-beginning 0))
               (match (match-string 0))
-              (len (org-string-width match)))
+              (len (save-match-data (org-string-width match))))
          (replace-match (concat "|" (make-string (1- len) ?\ )))
          (goto-char (+ 2 pos))
          (substring match 1)))))
@@ -1725,8 +1725,12 @@ In particular, this does handle wide and invisible 
characters."
       (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
     (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
       (setq s (replace-match
-              (concat "|" (make-string (org-string-width (match-string 1 s))
-                                       ?\ ) "|")
+              (concat "|"
+                       (make-string
+                        (save-match-data
+                          (org-string-width (match-string 1 s)))
+                       ?\ )
+                       "|")
               t t s)))
     s))
 

Reply via email to