From 15c519b84cf8305011293969f64c11230420ad50 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Fri, 20 Jan 2023 11:58:11 +0300 Subject: [PATCH] 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 Link: https://orgmode.org/list/wwufsc7edzu.fsf@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 fac9e68c1..5116b1127 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))