org-table: Fix table formulas editing

* lisp/org-table.el (org-table-show-reference): Do not move point
  outside the table when editing formulas.  Fix docstring.

Reported-by: Samuel Wales <samologist@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/104472>
This commit is contained in:
Nicolas Goaziou 2016-01-27 00:28:56 +01:00
parent a37996910e
commit 75ee067410
1 changed files with 70 additions and 68 deletions

View File

@ -3927,13 +3927,13 @@ With prefix ARG, apply the new formulas to the table."
(defvar org-show-positions nil) (defvar org-show-positions nil)
(defun org-table-show-reference (&optional local) (defun org-table-show-reference (&optional local)
"Show the location/value of the $ expression at point." "Show the location/value of the $ expression at point.
When LOCAL is non-nil, show references for the table at point."
(interactive) (interactive)
(org-table-remove-rectangle-highlight) (org-table-remove-rectangle-highlight)
(when local (org-table-analyze)) (when local (org-table-analyze))
(catch 'exit (catch 'exit
(let ((pos (if local (point) org-pos)) (let ((pos (if local (point) org-pos))
(table-start (if local org-table-current-begin-pos (org-table-begin)))
(face2 'highlight) (face2 'highlight)
(org-inhibit-highlight-removal t) (org-inhibit-highlight-removal t)
(win (selected-window)) (win (selected-window))
@ -3986,73 +3986,75 @@ With prefix ARG, apply the new formulas to the table."
(marker-buffer pos))))) (marker-buffer pos)))))
(goto-char pos) (goto-char pos)
(org-table-force-dataline) (org-table-force-dataline)
(when dest (let ((table-start
(setq name (substring dest 1)) (if local org-table-current-begin-pos (org-table-begin))))
(when dest
(setq name (substring dest 1))
(cond
((org-string-match-p "\\`\\$[a-zA-Z][a-zA-Z0-9]*" dest)
(org-table-goto-field dest))
((org-string-match-p "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'"
dest)
(org-table-goto-field dest))
(t (org-table-goto-column (string-to-number name))))
(move-marker pos (point))
(org-table-highlight-rectangle nil nil face2))
(cond (cond
((org-string-match-p "\\`\\$[a-zA-Z][a-zA-Z0-9]*" dest) ((equal dest match))
(org-table-goto-field dest)) ((not match))
((org-string-match-p "\\`@\\([1-9][0-9]*\\)\\$\\([1-9][0-9]*\\)\\'" ((eq what 'range)
dest) (ignore-errors (org-table-get-range match table-start nil 'highlight)))
(org-table-goto-field dest)) ((setq e (assoc var org-table-named-field-locations))
(t (org-table-goto-column (string-to-number name)))) (org-table-goto-field var)
(move-marker pos (point)) (org-table-highlight-rectangle)
(org-table-highlight-rectangle nil nil face2)) (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
(cond ((setq e (assoc var org-table-column-names))
((equal dest match)) (org-table-goto-column (string-to-number (cdr e)))
((not match)) (org-table-highlight-rectangle)
((eq what 'range) (goto-char table-start)
(ignore-errors (org-table-get-range match table-start nil 'highlight))) (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
((setq e (assoc var org-table-named-field-locations)) (org-table-end) t)
(org-table-goto-field var) (progn
(org-table-highlight-rectangle) (goto-char (match-beginning 1))
(message "Named field, column %d of line %d" (nth 2 e) (nth 1 e))) (org-table-highlight-rectangle)
((setq e (assoc var org-table-column-names)) (message "Named column (column %s)" (cdr e)))
(org-table-goto-column (string-to-number (cdr e))) (user-error "Column name not found")))
(org-table-highlight-rectangle) ((eq what 'column)
(goto-char table-start) ;; Column number.
(if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|") (org-table-goto-column (string-to-number (substring match 1)))
(org-table-end) t) (org-table-highlight-rectangle)
(progn (message "Column %s" (substring match 1)))
(goto-char (match-beginning 1)) ((setq e (assoc var org-table-local-parameters))
(org-table-highlight-rectangle) (goto-char table-start)
(message "Named column (column %s)" (cdr e))) (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
(user-error "Column name not found"))) (progn
((eq what 'column) (goto-char (match-beginning 1))
;; Column number. (org-table-highlight-rectangle)
(org-table-goto-column (string-to-number (substring match 1))) (message "Local parameter."))
(org-table-highlight-rectangle) (user-error "Parameter not found")))
(message "Column %s" (substring match 1))) ((not var) (user-error "No reference at point"))
((setq e (assoc var org-table-local-parameters)) ((setq e (assoc var org-table-formula-constants-local))
(goto-char table-start) (message "Local Constant: $%s=%s in #+CONSTANTS line."
(if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t) var (cdr e)))
(progn ((setq e (assoc var org-table-formula-constants))
(goto-char (match-beginning 1)) (message "Constant: $%s=%s in `org-table-formula-constants'."
(org-table-highlight-rectangle) var (cdr e)))
(message "Local parameter.")) ((setq e (and (fboundp 'constants-get) (constants-get var)))
(user-error "Parameter not found"))) (message "Constant: $%s=%s, from `constants.el'%s."
((not var) (user-error "No reference at point")) var e (format " (%s units)" constants-unit-system)))
((setq e (assoc var org-table-formula-constants-local)) (t (user-error "Undefined name $%s" var)))
(message "Local Constant: $%s=%s in #+CONSTANTS line." (goto-char pos)
var (cdr e))) (when (and org-show-positions
((setq e (assoc var org-table-formula-constants)) (not (memq this-command '(org-table-fedit-scroll
(message "Constant: $%s=%s in `org-table-formula-constants'." org-table-fedit-scroll-down))))
var (cdr e))) (push pos org-show-positions)
((setq e (and (fboundp 'constants-get) (constants-get var))) (push table-start org-show-positions)
(message "Constant: $%s=%s, from `constants.el'%s." (let ((min (apply 'min org-show-positions))
var e (format " (%s units)" constants-unit-system))) (max (apply 'max org-show-positions)))
(t (user-error "Undefined name $%s" var))) (set-window-start (selected-window) min)
(goto-char pos) (goto-char max)
(when (and org-show-positions (or (pos-visible-in-window-p max)
(not (memq this-command '(org-table-fedit-scroll (set-window-start (selected-window) max)))))
org-table-fedit-scroll-down))))
(push pos org-show-positions)
(push table-start org-show-positions)
(let ((min (apply 'min org-show-positions))
(max (apply 'max org-show-positions)))
(set-window-start (selected-window) min)
(goto-char max)
(or (pos-visible-in-window-p max)
(set-window-start (selected-window) max))))
(select-window win)))) (select-window win))))
(defun org-table-force-dataline () (defun org-table-force-dataline ()