Commit 17a72298 authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/mail/footnote.el: Add TEXT and POINTERS together

Rather than adding POINTERS and TEXT separately to footnote--markers-alist,
add them together, so we don't need footnote--first-text-marker because
the TEXT part is never nil.

(footnote--insert-numbered-footnote): Return marker.
(footnote--insert-text-marker, footnote--insert-pointer-marker):
Delete functions.
(footnote--insert-markers): New function to replace them.
(footnote--insert-footnote): Adjust accordingly.
Simplify pointless `unless`.
(footnote--first-text-marker): Remove.  Replace all calls by
(cadr (car footnote--markers-alist)) or just footnote--markers-alist.
parent 39acaff5
Pipeline #1534 failed with stage
in 77 minutes and 33 seconds
...@@ -457,7 +457,7 @@ footnote styles." ...@@ -457,7 +457,7 @@ footnote styles."
(let ((fn-regexp (footnote--current-regexp index-regexp))) (let ((fn-regexp (footnote--current-regexp index-regexp)))
(save-excursion (save-excursion
(pcase-dolist (`(,fn ,text . ,pointers) footnote--markers-alist) (pcase-dolist (`(,fn ,text . ,pointers) footnote--markers-alist)
;; Take care of the pointers first ;; Take care of the pointers first.
(dolist (locn pointers) (dolist (locn pointers)
(goto-char locn) (goto-char locn)
;; Try to handle the case where `footnote-start-tag' and ;; Try to handle the case where `footnote-start-tag' and
...@@ -505,15 +505,18 @@ footnote styles." ...@@ -505,15 +505,18 @@ footnote styles."
;; Internal functions ;; Internal functions
(defun footnote--insert-numbered-footnote (arg &optional mousable) (defun footnote--insert-numbered-footnote (arg &optional mousable)
"Insert numbered footnote at point." "Insert numbered footnote at point.
Return a marker pointing to the beginning of the [...]."
(let ((string (concat footnote-start-tag (let ((string (concat footnote-start-tag
(footnote--index-to-string arg) (footnote--index-to-string arg)
footnote-end-tag))) footnote-end-tag))
(pos (point)))
(insert (insert
(if mousable (if mousable
(propertize (propertize
string 'footnote-number arg footnote-mouse-highlight t) string 'footnote-number arg footnote-mouse-highlight t)
(propertize string 'footnote-number arg))))) (propertize string 'footnote-number arg)))
(copy-marker pos t)))
(defun footnote--renumber (to alist-elem) (defun footnote--renumber (to alist-elem)
"Renumber a single footnote." "Renumber a single footnote."
...@@ -550,33 +553,13 @@ footnote styles." ...@@ -550,33 +553,13 @@ footnote styles."
(or (re-search-backward footnote-signature-separator nil t) (or (re-search-backward footnote-signature-separator nil t)
(point))) (point)))
(defun footnote--insert-text-marker (arg locn) (defun footnote--insert-markers (arg text ptr)
"Insert a marker pointing to footnote ARG, at buffer location LOCN." "Insert the markers of new footnote ARG."
(let ((entry (assq arg footnote--markers-alist))) (cl-assert (and (numberp arg) (markerp text) (markerp ptr)))
(unless (cadr entry) (cl-assert (not (assq arg footnote--markers-alist)))
(let ((marker (copy-marker locn t))) (push `(,arg ,text ,ptr) footnote--markers-alist)
(if entry
(setf (cadr entry) marker)
(push `(,arg ,marker) footnote--markers-alist)
(setq footnote--markers-alist (setq footnote--markers-alist
(footnote--sort footnote--markers-alist))))))) (footnote--sort footnote--markers-alist)))
(defun footnote--insert-pointer-marker (arg locn)
"Insert a marker pointing to footnote ARG, at buffer location LOCN."
(let ((entry (assq arg footnote--markers-alist))
(marker (copy-marker locn t)))
(if entry
(push marker (cddr entry))
(push `(,arg nil ,marker) footnote--markers-alist)
(setq footnote--markers-alist
(footnote--sort footnote--markers-alist)))))
(defun footnote--first-text-marker ()
(let ((tmp footnote--markers-alist))
(while (and tmp (null (cadr (car footnote--markers-alist))))
;; Skip entries which don't (yet) have a TEXT marker.
(set tmp (cdr tmp)))
(cadr (car tmp))))
(defun footnote--goto-first () (defun footnote--goto-first ()
"Go to beginning of footnote area and return non-nil if successful. "Go to beginning of footnote area and return non-nil if successful.
...@@ -586,16 +569,12 @@ Presumes we're within the footnote area already." ...@@ -586,16 +569,12 @@ Presumes we're within the footnote area already."
(re-search-backward (re-search-backward
(concat "^" footnote-section-tag-regexp) nil t)) (concat "^" footnote-section-tag-regexp) nil t))
(footnote--markers-alist (footnote--markers-alist
(let ((pos (footnote--first-text-marker))) (goto-char (cadr (car footnote--markers-alist))))))
(when pos
(goto-char pos))))))
(defun footnote--insert-footnote (arg) (defun footnote--insert-footnote (arg)
"Insert a footnote numbered ARG, at (point)." "Insert a footnote numbered ARG, at (point)."
(push-mark) (push-mark)
(let ((old-point (point))) (let ((ptr (footnote--insert-numbered-footnote arg t)))
(footnote--insert-numbered-footnote arg t)
(footnote--insert-pointer-marker arg old-point))
(footnote--goto-char-point-max) (footnote--goto-char-point-max)
(if (footnote--goto-first) (if (footnote--goto-first)
(save-restriction (save-restriction
...@@ -603,25 +582,24 @@ Presumes we're within the footnote area already." ...@@ -603,25 +582,24 @@ Presumes we're within the footnote area already."
(footnote--narrow-to-footnotes)) (footnote--narrow-to-footnotes))
(footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now) (footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now)
;; (message "Inserting footnote %d" arg) ;; (message "Inserting footnote %d" arg)
(unless
(or (eq arg 1) (or (eq arg 1)
(when (re-search-forward (when (re-search-forward
(if footnote-spaced-footnotes (if footnote-spaced-footnotes
"\n\n" "\n\n"
(concat "\n" (footnote--current-regexp))) (concat "\n" (footnote--current-regexp)))
nil t) nil t)
(unless (beginning-of-line) t)) (beginning-of-line)
t)
(footnote--goto-char-point-max) (footnote--goto-char-point-max)
(footnote--goto-first)))) (footnote--goto-first)))
(unless (looking-at "^$") (unless (looking-at "^$")
(insert "\n")) (insert "\n"))
(when (eobp) (when (eobp)
(insert "\n")) (insert "\n"))
(unless (string-equal footnote-section-tag "") (unless (string-equal footnote-section-tag "")
(insert footnote-section-tag "\n"))) (insert footnote-section-tag "\n")))
(let ((old-point (point))) (let ((text (footnote--insert-numbered-footnote arg nil)))
(footnote--insert-numbered-footnote arg nil) (footnote--insert-markers arg text ptr))))
(footnote--insert-text-marker arg old-point)))
(defun footnote--sort (list) (defun footnote--sort (list)
(sort list #'car-less-than-car)) (sort list #'car-less-than-car))
...@@ -671,14 +649,14 @@ With optional arg BEFORE-TAG, return position of the `footnote-section-tag' ...@@ -671,14 +649,14 @@ With optional arg BEFORE-TAG, return position of the `footnote-section-tag'
instead, if applicable." instead, if applicable."
(cond (cond
;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead? ;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead?
((not (footnote--first-text-marker)) (point-max)) ((not footnote--markers-alist) (point-max))
((not before-tag) (footnote--first-text-marker)) ((not before-tag) (cadr (car footnote--markers-alist)))
((string-equal footnote-section-tag "") (footnote--first-text-marker)) ((string-equal footnote-section-tag "") (cadr (car footnote--markers-alist)))
(t (t
(save-excursion (save-excursion
(goto-char (footnote--first-text-marker)) (goto-char (cadr (car footnote--markers-alist)))
(if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t) (if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
(match-beginning 0) (point)
(message "Footnote section tag not found!") (message "Footnote section tag not found!")
;; This `else' should never happen, and indicates an error, ;; This `else' should never happen, and indicates an error,
;; ie. footnotes already exist and a footnote-section-tag is defined, ;; ie. footnotes already exist and a footnote-section-tag is defined,
...@@ -696,7 +674,7 @@ instead, if applicable." ...@@ -696,7 +674,7 @@ instead, if applicable."
;; function, and repeat. ;; function, and repeat.
;; ;;
;; TODO: integrate sanity checks at reasonable operational points. ;; TODO: integrate sanity checks at reasonable operational points.
(footnote--first-text-marker)))))) (point))))))
(defun footnote--get-area-point-max () (defun footnote--get-area-point-max ()
"Return the end of footnote area. "Return the end of footnote area.
...@@ -832,8 +810,8 @@ specified, jump to the text of that footnote." ...@@ -832,8 +810,8 @@ specified, jump to the text of that footnote."
((not (string-equal footnote-section-tag "")) ((not (string-equal footnote-section-tag ""))
(re-search-backward (concat "^" footnote-section-tag-regexp)) (re-search-backward (concat "^" footnote-section-tag-regexp))
(forward-line 1)) (forward-line 1))
((footnote--first-text-marker) (footnote--markers-alist
(goto-char (footnote--first-text-marker))))) (goto-char (cadr (car footnote--markers-alist))))))
(t (t
(error "I don't see a footnote here"))))) (error "I don't see a footnote here")))))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment