Function: org-table-fix-formulas

org-table-fix-formulas is a byte-compiled function defined in org-table.el.gz.

Signature

(org-table-fix-formulas KEY REPLACE &optional LIMIT DELTA REMOVE)

Documentation

Modify the equations after the table structure has been edited.

KEY is "@" or "$". REPLACE is an alist of numbers to replace. For all numbers larger than LIMIT, shift them by DELTA.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-table.el.gz
(defun org-table-fix-formulas (key replace &optional limit delta remove)
  "Modify the equations after the table structure has been edited.
KEY is \"@\" or \"$\".  REPLACE is an alist of numbers to replace.
For all numbers larger than LIMIT, shift them by DELTA."
  (save-excursion
    (goto-char (org-table-end))
    (while (let ((case-fold-search t)) (looking-at "[ \t]*#\\+tblfm:"))
      (let ((re (concat key "\\([0-9]+\\)"))
	    (re2
	     (when remove
	       (if (equal key "$")
		   (format "\\(@[0-9]+\\)?%s%d=.*?\\(::\\|$\\)"
			   (regexp-quote key) remove)
		 (format "@%d\\$[0-9]+=.*?\\(::\\|$\\)" remove))))
	    s n a)
	(when remove
          (save-excursion
            (while (re-search-forward re2 (line-end-position) t)
	      (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
                (if (equal (char-before (match-beginning 0)) ?.)
		    (user-error
		     "Change makes TBLFM term %s invalid, use undo to recover"
		     (match-string 0))
		  (replace-match ""))))))
        (while (re-search-forward re (line-end-position) t)
	  (unless (save-match-data (org-in-regexp "remote([^)]+?)"))
	    (setq s (match-string 1) n (string-to-number s))
	    (cond
	     ((setq a (assoc s replace))
	      (replace-match (concat key (cdr a)) t t))
	     ((and limit (> n limit))
	      (replace-match (concat key (number-to-string (+ n delta))) t t)))))
	(message "The formulas in #+TBLFM have been updated"))
      (forward-line))))