org-table-justify-field-maybe: Do not leak alignment data from other tables

* lisp/org-table.el (org-table-justify-field-maybe): Make sure that
`org-table-last-alignment' and `org-table-last-column-widths' do not
lea from another table that was re-aligned recently.

Link: https://old.reddit.com/r/emacs/comments/10gegwa/recalculating_an_orgmode_table_causes/
This commit is contained in:
Ihor Radchenko 2024-02-03 15:58:41 +01:00
parent 09ced6d2c2
commit 7a6bb0904d
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 41 additions and 39 deletions

View File

@ -4474,46 +4474,48 @@ Optional argument NEW may specify text to replace the current field content."
(cond
((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
((org-at-table-hline-p))
((and (not new)
(or (not (eq (marker-buffer org-table-aligned-begin-marker)
(current-buffer)))
(< (point) org-table-aligned-begin-marker)
(>= (point) org-table-aligned-end-marker)))
;; This is not the same table, force a full re-align.
(setq org-table-may-need-update t))
(t
;; Realign the current field, based on previous full realign.
(let ((pos (point))
(col (org-table-current-column)))
(when (> col 0)
(skip-chars-backward "^|")
(if (not (looking-at " *\\(?:\\([^|\n]*?\\) *\\(|\\)\\|\\([^|\n]+?\\) *\\($\\)\\)"))
(setq org-table-may-need-update t)
(let* ((align (nth (1- col) org-table-last-alignment))
(width (nth (1- col) org-table-last-column-widths))
(cell (match-string 0))
(field (match-string 1))
(properly-closed? (/= (match-beginning 2) (match-end 2)))
(new-cell
(save-match-data
(cond (org-table-may-need-update
(format " %s |" (or new field)))
((not properly-closed?)
(setq org-table-may-need-update t)
(format " %s |" (or new field)))
((not new)
(concat (org-table--align-field field width align)
"|"))
((and width (<= (org-string-width new nil 'org-table) width))
(concat (org-table--align-field new width align)
"|"))
(t
(setq org-table-may-need-update t)
(format " %s |" new))))))
(unless (equal new-cell cell)
(let (org-table-may-need-update)
(replace-match new-cell t t)))
(goto-char pos))))))))
(when (or (not (eq (marker-buffer org-table-aligned-begin-marker)
(current-buffer)))
(< (point) org-table-aligned-begin-marker)
(>= (point) org-table-aligned-end-marker))
;; This is not the same table, force a full re-align.
(setq org-table-may-need-update t
org-table-last-alignment nil
org-table-last-column-widths nil))
(when new
;; Realign the current field, based on previous full realign.
(let ((pos (point))
(col (org-table-current-column)))
(when (> col 0)
(skip-chars-backward "^|")
(if (not (looking-at " *\\(?:\\([^|\n]*?\\) *\\(|\\)\\|\\([^|\n]+?\\) *\\($\\)\\)"))
(setq org-table-may-need-update t)
(let* ((align (nth (1- col) org-table-last-alignment))
(width (nth (1- col) org-table-last-column-widths))
(cell (match-string 0))
(field (match-string 1))
(properly-closed? (/= (match-beginning 2) (match-end 2)))
(new-cell
(save-match-data
(cond (org-table-may-need-update
(format " %s |" (or new field)))
((not properly-closed?)
(setq org-table-may-need-update t)
(format " %s |" (or new field)))
((not new)
(concat (org-table--align-field field width align)
"|"))
((and width (<= (org-string-width new nil 'org-table) width))
(concat (org-table--align-field new width align)
"|"))
(t
(setq org-table-may-need-update t)
(format " %s |" new))))))
(unless (equal new-cell cell)
(let (org-table-may-need-update)
(replace-match new-cell t t)))
(goto-char pos)))))))))
;;;###autoload
(defun org-table-sort-lines