Function: org-edit-footnote-reference

org-edit-footnote-reference is an interactive and byte-compiled function defined in org-src.el.gz.

Signature

(org-edit-footnote-reference)

Documentation

Edit definition of footnote reference at point.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/org/org-src.el.gz
(defun org-edit-footnote-reference ()
  "Edit definition of footnote reference at point."
  (interactive)
  (let* ((context (org-element-context))
	 (label (org-element-property :label context)))
    (unless (and (org-element-type-p context 'footnote-reference)
		 (org-src--on-datum-p context))
      (user-error "Not on a footnote reference"))
    (unless label (user-error "Cannot edit remotely anonymous footnotes"))
    (let* ((definition (org-with-wide-buffer
			(org-footnote-goto-definition label)
			(backward-char)
			(org-element-context)))
	   (inline? (org-element-type-p definition 'footnote-reference))
	   (contents
	    (org-with-wide-buffer
	     (buffer-substring-no-properties
	      (or (org-element-post-affiliated definition)
		  (org-element-begin definition))
	      (cond
	       (inline? (1+ (org-element-contents-end definition)))
	       ((org-element-contents-end definition))
	       (t (goto-char (org-element-post-affiliated definition))
		  (line-end-position)))))))
      (add-text-properties
       0
       (progn (string-match (if inline? "\\`\\[fn:.*?:" "\\`.*?\\]") contents)
	      (match-end 0))
       '(read-only "Cannot edit footnote label" front-sticky t rear-nonsticky t)
       contents)
      (when inline?
	(let ((l (length contents)))
	  (add-text-properties
	   (1- l) l
	   '(read-only "Cannot edit past footnote reference"
		       front-sticky nil rear-nonsticky nil)
	   contents)))
      (org-src--edit-element
       definition
       (format "*Edit footnote [%s]*" label)
       (let ((source (current-buffer)))
	 (lambda ()
	   (org-mode)
	   (org-clone-local-variables source)))
       (lambda ()
	 (if (not inline?) (delete-region (point) (search-forward "]"))
	   (delete-region (point) (search-forward ":" nil t 2))
	   (delete-region (1- (point-max)) (point-max))
	   (when (re-search-forward "\n[ \t]*\n" nil t)
	     (user-error "Inline definitions cannot contain blank lines"))
	   ;; If footnote reference belongs to a table, make sure to
	   ;; remove any newline characters in order to preserve
	   ;; table's structure.
	   (when (org-element-lineage definition 'table-cell)
	     (while (search-forward "\n" nil t) (replace-match " ")))))
       contents
       'remote))
    ;; Report success.
    t))