org-footnote: Make `org-footnote-create-definition' non-interactive

* lisp/org-footnote.el (org-footnote-create-definition): Remove
  `interactive' status.  Allow to insert definitions outside of
  narrowed part of buffer.  Preserve position.  Return beginning
  position of created definition.
(org-footnote-new):
(org-footnote-action): Apply changes above.
This commit is contained in:
Nicolas Goaziou 2015-05-06 23:23:54 +02:00
parent 360c4633e2
commit c76006a42c
1 changed files with 56 additions and 58 deletions

View File

@ -536,51 +536,62 @@ or new, let the user edit the definition of the footnote."
(mapcar #'list all) nil nil (mapcar #'list all) nil nil
(and (eq org-footnote-auto-label 'confirm) propose)))))))) (and (eq org-footnote-auto-label 'confirm) propose))))))))
(cond ((not label) (cond ((not label)
(insert "[fn:: ]") (insert "[fn::]")
(backward-char 1)) (backward-char 1))
((member label all) ((member label all)
(insert "[" label "]") (insert "[" label "]")
(message "New reference to existing note")) (message "New reference to existing note"))
(org-footnote-define-inline (org-footnote-define-inline
(insert "[" label ": ]") (insert "[" label ":]")
(backward-char 1) (backward-char 1)
(org-footnote-auto-adjust-maybe)) (org-footnote-auto-adjust-maybe))
(t (t
(insert "[" label "]") (insert "[" label "]")
(org-footnote-create-definition label) (let ((l (copy-marker (org-footnote-create-definition label))))
(org-footnote-auto-adjust-maybe))))) (org-footnote-auto-adjust-maybe)
(or (ignore-errors (org-footnote-goto-definition label l))
;; Since definition was created outside current
;; scope, edit it remotely.
(progn (set-marker l nil)
(org-edit-footnote-reference))))))))
(defvar org-blank-before-new-entry) ; silence byte-compiler (defvar org-blank-before-new-entry) ; Silence byte-compiler.
(defun org-footnote-create-definition (label) (defun org-footnote-create-definition (label)
"Start the definition of a footnote with label LABEL." "Start the definition of a footnote with label LABEL.
(interactive "sLabel: ") Return buffer position at the beginning of the definition. In an
Org buffer, this function doesn't move point."
(let ((label (org-footnote-normalize-label label)) (let ((label (org-footnote-normalize-label label))
electric-indent-mode) ;; Prevent wrong indentation electric-indent-mode) ; Prevent wrong indentation.
(cond (cond
;; In an Org file. ;; In an Org document.
((derived-mode-p 'org-mode) ((derived-mode-p 'org-mode)
;; If `org-footnote-section' is defined, find it, or create it ;; If `org-footnote-section' is defined, find it, or create it
;; at the end of the buffer. ;; at the end of the buffer.
(when org-footnote-section (org-with-wide-buffer
(goto-char (point-min)) (cond
(let ((re (concat "^\\*+[ \t]+" org-footnote-section "[ \t]*$"))) ((not org-footnote-section)
(unless (or (re-search-forward re nil t) (org-footnote--goto-local-insertion-point))
(and (progn (widen) t) ((save-excursion
(re-search-forward re nil t))) (goto-char (point-min))
(goto-char (point-max)) (re-search-forward
(skip-chars-backward " \t\r\n") (concat "^\\*+[ \t]+" (regexp-quote org-footnote-section) "[ \t]*$")
(unless (bolp) (newline)) nil t))
;; Insert new section. Separate it from the previous one (goto-char (match-end 0))
;; with a blank line, unless `org-blank-before-new-entry' (forward-line)
;; explicitly says no. (unless (bolp) (insert "\n")))
(when (and (cdr (assq 'heading org-blank-before-new-entry)) (t
(zerop (save-excursion (org-back-over-empty-lines)))) (goto-char (point-max))
(insert "\n")) (unless (bolp) (insert "\n"))
(insert "* " org-footnote-section "\n")))) ;; Insert new section. Separate it from the previous one
;; Move to the end of this entry (which may be ;; with a blank line, unless `org-blank-before-new-entry'
;; `org-footnote-section' or the current one). ;; explicitly says no.
(org-footnote-goto-local-insertion-point) (when (and (cdr (assq 'heading org-blank-before-new-entry))
(org-show-context 'link-search)) (zerop (save-excursion (org-back-over-empty-lines))))
(insert "\n"))
(insert "* " org-footnote-section "\n")))
(when (zerop (org-back-over-empty-lines)) (insert "\n"))
(insert "[" label "] \n")
(line-beginning-position 0)))
(t (t
;; In a non-Org file. Search for footnote tag, or create it if ;; In a non-Org file. Search for footnote tag, or create it if
;; specified (at the end of buffer, or before signature if in ;; specified (at the end of buffer, or before signature if in
@ -615,16 +626,11 @@ or new, let the user edit the definition of the footnote."
(skip-chars-backward " \t\r\n") (skip-chars-backward " \t\r\n")
(delete-region (point) max) (delete-region (point) max)
(unless (bolp) (newline)) (unless (bolp) (newline))
(set-marker max nil)))) (set-marker max nil))
;; Insert footnote label. (when (zerop (org-back-over-empty-lines)) (insert "\n"))
(when (zerop (org-back-over-empty-lines)) (newline)) (insert "[" label "] \n")
(insert "[" label "] \n") (backward-char)
(backward-char) (line-beginning-position)))))
;; Only notify user about next possible action when in an Org
;; buffer, as the bindings may have different meanings otherwise.
(when (derived-mode-p 'org-mode)
(message
"Edit definition and go back with `C-c &' or, if unique, with `C-c C-c'."))))
;;;###autoload ;;;###autoload
(defun org-footnote-action (&optional special) (defun org-footnote-action (&optional special)
@ -651,30 +657,22 @@ offer additional commands in a menu."
;; definition. ;; definition.
((not label) ((not label)
(goto-char (org-element-property :contents-begin context))) (goto-char (org-element-property :contents-begin context)))
;; A definition exists: move to it, if possible, or offer to ;; Check if a definition exists: then move to it.
;; widen buffer to reach it. ((let ((p (nth 1 (org-footnote-get-definition label))))
((let ((definition (org-footnote-get-definition label))) (when p (org-footnote-goto-definition label p))))
(when definition
(let ((begin (nth 1 definition)))
(catch 'do-nothing
(cond ((and (>= begin (point-min)) (<= begin (point-max))))
((yes-or-no-p "Definition outside scope. Widen? ")
(widen))
(t (throw 'do-nothing nil)))
(goto-char begin)
(search-forward label)
(forward-char)
t)))))
;; No definition exists: offer to create it. ;; No definition exists: offer to create it.
((yes-or-no-p (format "No definition for %s. Create one? " label)) ((yes-or-no-p (format "No definition for %s. Create one? " label))
(org-footnote-create-definition label))))) (let ((p (org-footnote-create-definition label)))
(or (ignore-errors (org-footnote-goto-definition label p))
;; Since definition was created outside current scope,
;; edit it remotely.
(org-edit-footnote-reference)))))))
((eq type 'footnote-definition) ((eq type 'footnote-definition)
(org-footnote-goto-previous-reference (org-footnote-goto-previous-reference
(org-element-property :label context))) (org-element-property :label context)))
((or special ((or special (not (org-footnote--allow-reference-p)))
(zerop (current-column)) (message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s | \
(not (org-footnote-in-valid-context-p))) ->[n]umeric | [d]elete")
(message "Footnotes: [s]ort | [r]enumber fn:N | [S]=r+s |->[n]umeric | [d]elete")
(let ((c (read-char-exclusive))) (let ((c (read-char-exclusive)))
(cond (cond
((eq c ?s) (org-footnote-normalize 'sort)) ((eq c ?s) (org-footnote-normalize 'sort))