Skip to content

Commit 17a7229

Browse files
committed
* 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.
1 parent 39acaff commit 17a7229

File tree

1 file changed

+49
-71
lines changed

1 file changed

+49
-71
lines changed

lisp/mail/footnote.el

Lines changed: 49 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ footnote styles."
457457
(let ((fn-regexp (footnote--current-regexp index-regexp)))
458458
(save-excursion
459459
(pcase-dolist (`(,fn ,text . ,pointers) footnote--markers-alist)
460-
;; Take care of the pointers first
460+
;; Take care of the pointers first.
461461
(dolist (locn pointers)
462462
(goto-char locn)
463463
;; Try to handle the case where `footnote-start-tag' and
@@ -505,15 +505,18 @@ footnote styles."
505505

506506
;; Internal functions
507507
(defun footnote--insert-numbered-footnote (arg &optional mousable)
508-
"Insert numbered footnote at point."
508+
"Insert numbered footnote at point.
509+
Return a marker pointing to the beginning of the [...]."
509510
(let ((string (concat footnote-start-tag
510511
(footnote--index-to-string arg)
511-
footnote-end-tag)))
512+
footnote-end-tag))
513+
(pos (point)))
512514
(insert
513515
(if mousable
514516
(propertize
515517
string 'footnote-number arg footnote-mouse-highlight t)
516-
(propertize string 'footnote-number arg)))))
518+
(propertize string 'footnote-number arg)))
519+
(copy-marker pos t)))
517520

518521
(defun footnote--renumber (to alist-elem)
519522
"Renumber a single footnote."
@@ -550,33 +553,13 @@ footnote styles."
550553
(or (re-search-backward footnote-signature-separator nil t)
551554
(point)))
552555

553-
(defun footnote--insert-text-marker (arg locn)
554-
"Insert a marker pointing to footnote ARG, at buffer location LOCN."
555-
(let ((entry (assq arg footnote--markers-alist)))
556-
(unless (cadr entry)
557-
(let ((marker (copy-marker locn t)))
558-
(if entry
559-
(setf (cadr entry) marker)
560-
(push `(,arg ,marker) footnote--markers-alist)
561-
(setq footnote--markers-alist
562-
(footnote--sort footnote--markers-alist)))))))
563-
564-
(defun footnote--insert-pointer-marker (arg locn)
565-
"Insert a marker pointing to footnote ARG, at buffer location LOCN."
566-
(let ((entry (assq arg footnote--markers-alist))
567-
(marker (copy-marker locn t)))
568-
(if entry
569-
(push marker (cddr entry))
570-
(push `(,arg nil ,marker) footnote--markers-alist)
571-
(setq footnote--markers-alist
572-
(footnote--sort footnote--markers-alist)))))
573-
574-
(defun footnote--first-text-marker ()
575-
(let ((tmp footnote--markers-alist))
576-
(while (and tmp (null (cadr (car footnote--markers-alist))))
577-
;; Skip entries which don't (yet) have a TEXT marker.
578-
(set tmp (cdr tmp)))
579-
(cadr (car tmp))))
556+
(defun footnote--insert-markers (arg text ptr)
557+
"Insert the markers of new footnote ARG."
558+
(cl-assert (and (numberp arg) (markerp text) (markerp ptr)))
559+
(cl-assert (not (assq arg footnote--markers-alist)))
560+
(push `(,arg ,text ,ptr) footnote--markers-alist)
561+
(setq footnote--markers-alist
562+
(footnote--sort footnote--markers-alist)))
580563

581564
(defun footnote--goto-first ()
582565
"Go to beginning of footnote area and return non-nil if successful.
@@ -586,42 +569,37 @@ Presumes we're within the footnote area already."
586569
(re-search-backward
587570
(concat "^" footnote-section-tag-regexp) nil t))
588571
(footnote--markers-alist
589-
(let ((pos (footnote--first-text-marker)))
590-
(when pos
591-
(goto-char pos))))))
572+
(goto-char (cadr (car footnote--markers-alist))))))
592573

593574
(defun footnote--insert-footnote (arg)
594575
"Insert a footnote numbered ARG, at (point)."
595576
(push-mark)
596-
(let ((old-point (point)))
597-
(footnote--insert-numbered-footnote arg t)
598-
(footnote--insert-pointer-marker arg old-point))
599-
(footnote--goto-char-point-max)
600-
(if (footnote--goto-first)
601-
(save-restriction
602-
(when footnote-narrow-to-footnotes-when-editing
603-
(footnote--narrow-to-footnotes))
604-
(footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now)
605-
;; (message "Inserting footnote %d" arg)
606-
(unless
607-
(or (eq arg 1)
608-
(when (re-search-forward
609-
(if footnote-spaced-footnotes
610-
"\n\n"
611-
(concat "\n" (footnote--current-regexp)))
612-
nil t)
613-
(unless (beginning-of-line) t))
614-
(footnote--goto-char-point-max)
615-
(footnote--goto-first))))
616-
(unless (looking-at "^$")
617-
(insert "\n"))
618-
(when (eobp)
619-
(insert "\n"))
620-
(unless (string-equal footnote-section-tag "")
621-
(insert footnote-section-tag "\n")))
622-
(let ((old-point (point)))
623-
(footnote--insert-numbered-footnote arg nil)
624-
(footnote--insert-text-marker arg old-point)))
577+
(let ((ptr (footnote--insert-numbered-footnote arg t)))
578+
(footnote--goto-char-point-max)
579+
(if (footnote--goto-first)
580+
(save-restriction
581+
(when footnote-narrow-to-footnotes-when-editing
582+
(footnote--narrow-to-footnotes))
583+
(footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now)
584+
;; (message "Inserting footnote %d" arg)
585+
(or (eq arg 1)
586+
(when (re-search-forward
587+
(if footnote-spaced-footnotes
588+
"\n\n"
589+
(concat "\n" (footnote--current-regexp)))
590+
nil t)
591+
(beginning-of-line)
592+
t)
593+
(footnote--goto-char-point-max)
594+
(footnote--goto-first)))
595+
(unless (looking-at "^$")
596+
(insert "\n"))
597+
(when (eobp)
598+
(insert "\n"))
599+
(unless (string-equal footnote-section-tag "")
600+
(insert footnote-section-tag "\n")))
601+
(let ((text (footnote--insert-numbered-footnote arg nil)))
602+
(footnote--insert-markers arg text ptr))))
625603

626604
(defun footnote--sort (list)
627605
(sort list #'car-less-than-car))
@@ -671,14 +649,14 @@ With optional arg BEFORE-TAG, return position of the `footnote-section-tag'
671649
instead, if applicable."
672650
(cond
673651
;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead?
674-
((not (footnote--first-text-marker)) (point-max))
675-
((not before-tag) (footnote--first-text-marker))
676-
((string-equal footnote-section-tag "") (footnote--first-text-marker))
652+
((not footnote--markers-alist) (point-max))
653+
((not before-tag) (cadr (car footnote--markers-alist)))
654+
((string-equal footnote-section-tag "") (cadr (car footnote--markers-alist)))
677655
(t
678656
(save-excursion
679-
(goto-char (footnote--first-text-marker))
657+
(goto-char (cadr (car footnote--markers-alist)))
680658
(if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
681-
(match-beginning 0)
659+
(point)
682660
(message "Footnote section tag not found!")
683661
;; This `else' should never happen, and indicates an error,
684662
;; ie. footnotes already exist and a footnote-section-tag is defined,
@@ -696,7 +674,7 @@ instead, if applicable."
696674
;; function, and repeat.
697675
;;
698676
;; TODO: integrate sanity checks at reasonable operational points.
699-
(footnote--first-text-marker))))))
677+
(point))))))
700678

701679
(defun footnote--get-area-point-max ()
702680
"Return the end of footnote area.
@@ -832,8 +810,8 @@ specified, jump to the text of that footnote."
832810
((not (string-equal footnote-section-tag ""))
833811
(re-search-backward (concat "^" footnote-section-tag-regexp))
834812
(forward-line 1))
835-
((footnote--first-text-marker)
836-
(goto-char (footnote--first-text-marker)))))
813+
(footnote--markers-alist
814+
(goto-char (cadr (car footnote--markers-alist))))))
837815
(t
838816
(error "I don't see a footnote here")))))
839817

0 commit comments

Comments
 (0)