From 6d60138260d54bf8403b9c87155682447808335e Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 28 Jun 2015 15:45:31 +0200 Subject: [PATCH] org-table: Do not modify buffer needlessly when aligning table * lisp/org-table.el (org-table-align): Compare new table with old one before replacing it. Only replace modified lines. --- lisp/org-table.el | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lisp/org-table.el b/lisp/org-table.el index 7b7d5f555..32dc777a2 100644 --- a/lisp/org-table.el +++ b/lisp/org-table.el @@ -723,7 +723,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.") (colpos (org-table-current-column)) (winstart (window-start)) (winstartline (org-current-line (min winstart (1- (point-max))))) - lines (new "") lengths l typenums ty fields maxfields i + lines lengths l typenums ty fields maxfields i column (indent "") cnt frac rfmt hfmt @@ -796,7 +796,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.") (setq emptystrings (make-list maxfields "")) ;; Check for special formatting. (setq i -1) - (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns + (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns (setq column (mapcar (lambda (x) (or (nth i x) "")) fields)) ;; Check if there is an explicit width specified (setq fmax nil) @@ -820,7 +820,7 @@ When nil, simply write \"#ERROR\" in corrupted fields.") (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax))) (unless (> f1 1) (user-error "Cannot narrow field starting with wide link \"%s\"" - (match-string 0 xx))) + (match-string 0 xx))) (setq f2 (length xx)) (if (= (org-string-width xx) f2) @@ -882,16 +882,15 @@ When nil, simply write \"#ERROR\" in corrupted fields.") (setq rfmt (concat rfmt "\n") hfmt (concat (substring hfmt 0 -1) "|\n")) - (setq new (mapconcat - (lambda (l) - (if l (apply 'format rfmt - (append (pop fields) emptystrings)) - hfmt)) - lines "")) (move-marker org-table-aligned-begin-marker (point)) - (insert new) - ;; Replace the old one - (delete-region (point) end) + ;; Replace modified lines only. + (dolist (l lines) + (let ((line (if l (apply #'format rfmt (append (pop fields) emptystrings)) + hfmt))) + (if (equal (buffer-substring (point) (line-beginning-position 2)) line) + (forward-line) + (insert line) + (delete-region (point) (line-beginning-position 2))))) (move-marker end nil) (move-marker org-table-aligned-end-marker (point)) (when (and orgtbl-mode (not (derived-mode-p 'org-mode))) -- 2.11.4.GIT