Function: org-table-get-formula

org-table-get-formula is a byte-compiled function defined in org-table.el.gz.

Signature

(org-table-get-formula &optional EQUATION NAMED)

Documentation

Read a formula from the minibuffer, offer stored formula as default.

When NAMED is non-nil, look for a named equation.

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-table.el.gz
(defun org-table-get-formula (&optional equation named)
  "Read a formula from the minibuffer, offer stored formula as default.
When NAMED is non-nil, look for a named equation."
  (let* ((stored-list (org-table-get-stored-formulas))
	 (name (car (rassoc (list (count-lines org-table-current-begin-pos
					       (line-beginning-position))
				  (org-table-current-column))
			    org-table-named-field-locations)))
	 (ref (format "@%d$%d"
		      (org-table-current-dline)
		      (org-table-current-column)))
	 (scol (cond
		((not named) (format "$%d" (org-table-current-column)))
		(name)
		(t ref)))
	 (name (or name ref))
	 (org-table-may-need-update nil)
	 (stored (cdr (assoc scol stored-list)))
	 (eq (cond
	      ((and stored equation (string-match-p "^ *=? *$" equation))
	       stored)
	      ((stringp equation) equation)
	      (t
	       (org-table-formula-from-user
		(read-string
		 (org-table-formula-to-user
		  (format "%s formula %s=" (if named "Field" "Column") scol))
		 (if stored (org-table-formula-to-user stored) "")
		 'org-table-formula-history)))))
	 mustsave)
    (unless (org-string-nw-p eq)
      ;; Remove formula.
      (setq stored-list (delq (assoc scol stored-list) stored-list))
      (org-table-store-formulas stored-list)
      (user-error "Formula removed"))
    (when (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
    (when (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
    (when (and name (not named))
      ;; We set the column equation, delete the named one.
      (setq stored-list (delq (assoc name stored-list) stored-list)
	    mustsave t))
    (if stored
	(setcdr (assoc scol stored-list) eq)
      (setq stored-list (cons (cons scol eq) stored-list)))
    (when (or mustsave (not (equal stored eq)))
      (org-table-store-formulas stored-list))
    eq))