Skip to content

Commit 4299e5e

Browse files
committed
* lisp/mail/footnote.el: Consolidate the two marker-alists
Consolidate footnote-text-marker-alist and footnote-pointer-marker-alist into a single footnote--markers-alist. (footnote--markers-alist): New var. (footnote-text-marker-alist, footnote-pointer-marker-alist): Delete vars. (footnote--refresh-footnotes, footnote--text-under-cursor) (footnote--calc-fn-alignment-column, footnote-add-footnote) (footnote-goto-footnote, footnote-back-to-message): Adjust accordingly. (footnote--make-hole, footnote-delete-footnote) (footnote-renumber-footnotes): Simplify accordingly. (footnote-cycle-style): Indicate style name in echo area. (footnote--renumber): Take a single `alist-elem` arg instead of `pointer-alist` and `text-alist`. (footnote--insert-text-marker, footnote--insert-pointer-marker): Add to footnote--markers-alist instead. (footnote--first-text-marker): New function. (footnote--get-area-point-min): Use it. footnote--goto-first): New function. (footnote--insert-footnote): Use it. (footnote-style-number): Use defvar-local.
1 parent c9b820d commit 4299e5e

File tree

1 file changed

+94
-97
lines changed

1 file changed

+94
-97
lines changed

lisp/mail/footnote.el

Lines changed: 94 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -157,17 +157,14 @@ left with the first character of footnote text."
157157

158158
;;; Private variables
159159

160-
(defvar footnote-style-number nil
161-
"Footnote style represented as an index into footnote-style-alist.")
162-
(make-variable-buffer-local 'footnote-style-number)
160+
(defvar-local footnote-style-number nil
161+
"Footnote style represented as an index into `footnote-style-alist'.")
163162

164-
(defvar footnote-text-marker-alist nil
165-
"List of markers pointing to text of footnotes in message buffer.")
166-
(make-variable-buffer-local 'footnote-text-marker-alist)
167-
168-
(defvar footnote-pointer-marker-alist nil
169-
"List of markers pointing to footnote pointers in message buffer.")
170-
(make-variable-buffer-local 'footnote-pointer-marker-alist)
163+
(defvar-local footnote--markers-alist nil
164+
"List of (FN TEXT . POINTERS).
165+
Where FN is the footnote number, TEXT is a marker pointing to
166+
the footnote's text, and POINTERS is a list of markers pointing
167+
to the places from which the footnote is referenced.")
171168

172169
(defvar footnote-mouse-highlight 'highlight
173170
;; FIXME: This `highlight' property is not currently used.
@@ -462,8 +459,8 @@ styles."
462459
(save-excursion
463460
;; Take care of the pointers first
464461
(let ((i 0) locn alist)
465-
(while (setq alist (nth i footnote-pointer-marker-alist))
466-
(setq locn (cdr alist))
462+
(while (setq alist (nth i footnote--markers-alist))
463+
(setq locn (cddr alist))
467464
(while locn
468465
(goto-char (car locn))
469466
;; Try to handle the case where `footnote-start-tag' and
@@ -486,8 +483,8 @@ styles."
486483

487484
;; Now take care of the text section
488485
(let ((i 0) alist)
489-
(while (setq alist (nth i footnote-text-marker-alist))
490-
(goto-char (cdr alist))
486+
(while (setq alist (nth i footnote--markers-alist))
487+
(goto-char (cadr alist))
491488
(when (looking-at (concat
492489
(regexp-quote footnote-start-tag)
493490
"\\(" index-regexp "+\\)"
@@ -508,7 +505,8 @@ styles."
508505
(let ((old-desc (assq footnote-style footnote-style-alist)))
509506
(setq footnote-style (caar (or (cdr (memq old-desc footnote-style-alist))
510507
footnote-style-alist)))
511-
(footnote--refresh-footnotes (nth 2 old-desc))))
508+
(footnote--refresh-footnotes (nth 2 old-desc))
509+
(message "Style set to %s" footnote-style)))
512510

513511
(defun footnote-set-style (style)
514512
"Select a specific style."
@@ -532,11 +530,10 @@ styles."
532530
string 'footnote-number arg footnote-mouse-highlight t)
533531
(propertize string 'footnote-number arg)))))
534532

535-
(defun footnote--renumber (to pointer-alist text-alist)
533+
(defun footnote--renumber (to alist-elem)
536534
"Renumber a single footnote."
537-
(let* ((posn-list (cdr pointer-alist)))
538-
(setcar pointer-alist to)
539-
(setcar text-alist to)
535+
(let* ((posn-list (cddr alist-elem)))
536+
(setcar alist-elem to)
540537
(while posn-list
541538
(goto-char (car posn-list))
542539
(when (looking-back (concat (regexp-quote footnote-start-tag)
@@ -550,7 +547,7 @@ styles."
550547
footnote-end-tag)
551548
'footnote-number to footnote-mouse-highlight t)))
552549
(setq posn-list (cdr posn-list)))
553-
(goto-char (cdr text-alist))
550+
(goto-char (cadr alist-elem))
554551
(when (looking-at (concat (regexp-quote footnote-start-tag)
555552
(footnote--current-regexp)
556553
(regexp-quote footnote-end-tag)))
@@ -575,38 +572,51 @@ styles."
575572

576573
(defun footnote--insert-text-marker (arg locn)
577574
"Insert a marker pointing to footnote ARG, at buffer location LOCN."
578-
(let ((marker (make-marker)))
579-
(unless (assq arg footnote-text-marker-alist)
580-
(set-marker marker locn)
581-
(setq footnote-text-marker-alist
582-
(cons (cons arg marker) footnote-text-marker-alist))
583-
(setq footnote-text-marker-alist
584-
(footnote--sort footnote-text-marker-alist)))))
575+
(let ((entry (assq arg footnote--markers-alist)))
576+
(unless (cadr entry)
577+
(let ((marker (copy-marker locn)))
578+
(if entry
579+
(setf (cadr entry) marker)
580+
(push `(,arg ,marker) footnote--markers-alist)
581+
(setq footnote--markers-alist
582+
(footnote--sort footnote--markers-alist)))))))
585583

586584
(defun footnote--insert-pointer-marker (arg locn)
587585
"Insert a marker pointing to footnote ARG, at buffer location LOCN."
588-
(let ((marker (make-marker))
589-
alist)
590-
(set-marker marker locn)
591-
(if (setq alist (assq arg footnote-pointer-marker-alist))
592-
(setf alist
593-
(cons marker (cdr alist)))
594-
(setq footnote-pointer-marker-alist
595-
(cons (cons arg (list marker)) footnote-pointer-marker-alist))
596-
(setq footnote-pointer-marker-alist
597-
(footnote--sort footnote-pointer-marker-alist)))))
586+
(let ((entry (assq arg footnote--markers-alist))
587+
(marker (copy-marker locn)))
588+
(if entry
589+
(push marker (cddr entry))
590+
(push `(,arg nil ,marker) footnote--markers-alist)
591+
(setq footnote--markers-alist
592+
(footnote--sort footnote--markers-alist)))))
593+
594+
(defun footnote--first-text-marker ()
595+
(let ((tmp footnote--markers-alist))
596+
(while (and tmp (null (cadr (car footnote--markers-alist))))
597+
;; Skip entries which don't (yet) have a TEXT marker.
598+
(set tmp (cdr tmp)))
599+
(cadr (car tmp))))
600+
601+
(defun footnote--goto-first ()
602+
"Go to beginning of footnote area and return non-nil if successful.
603+
Presumes we're within the footnote area already."
604+
(cond
605+
((not (string-equal footnote-section-tag ""))
606+
(re-search-backward
607+
(concat "^" footnote-section-tag-regexp) nil t))
608+
(footnote--markers-alist
609+
(let ((pos (footnote--first-text-marker)))
610+
(when pos
611+
(goto-char pos))))))
598612

599613
(defun footnote--insert-footnote (arg)
600614
"Insert a footnote numbered ARG, at (point)."
601615
(push-mark)
602616
(footnote--insert-pointer-marker arg (point))
603617
(footnote--insert-numbered-footnote arg t)
604618
(footnote--goto-char-point-max)
605-
(if (cond
606-
((not (string-equal footnote-section-tag ""))
607-
(re-search-backward (concat "^" footnote-section-tag-regexp) nil t))
608-
(footnote-text-marker-alist
609-
(goto-char (cdar footnote-text-marker-alist))))
619+
(if (footnote--goto-first)
610620
(save-restriction
611621
(when footnote-narrow-to-footnotes-when-editing
612622
(footnote--narrow-to-footnotes))
@@ -624,12 +634,7 @@ styles."
624634
nil t)
625635
(unless (beginning-of-line) t))
626636
(footnote--goto-char-point-max)
627-
(cond
628-
((not (string-equal footnote-section-tag ""))
629-
(re-search-backward
630-
(concat "^" footnote-section-tag-regexp) nil t))
631-
(footnote-text-marker-alist
632-
(goto-char (cdar footnote-text-marker-alist)))))))
637+
(footnote--goto-first))))
633638
(unless (looking-at "^$")
634639
(insert "\n"))
635640
(when (eobp)
@@ -647,18 +652,18 @@ styles."
647652
"Return the number of the current footnote if in footnote text.
648653
Return nil if the cursor is not positioned over the text of
649654
a footnote."
650-
(when (and footnote-text-marker-alist
655+
(when (and footnote--markers-alist
651656
(<= (footnote--get-area-point-min)
652657
(point)
653658
(footnote--get-area-point-max)))
654659
(let ((i 1) alist-txt result)
655-
(while (and (setq alist-txt (nth i footnote-text-marker-alist))
660+
(while (and (setq alist-txt (nth i footnote--markers-alist))
656661
(null result))
657-
(when (< (point) (cdr alist-txt))
658-
(setq result (car (nth (1- i) footnote-text-marker-alist))))
662+
(when (< (point) (cadr alist-txt))
663+
(setq result (car (nth (1- i) footnote--markers-alist))))
659664
(setq i (1+ i)))
660665
(when (and (null result) (null alist-txt))
661-
(setq result (car (nth (1- i) footnote-text-marker-alist))))
666+
(setq result (car (nth (1- i) footnote--markers-alist))))
662667
result)))
663668

664669
(defun footnote--under-cursor ()
@@ -675,7 +680,7 @@ Return nil if the cursor is not over a footnote."
675680
(string-width
676681
(concat footnote-start-tag footnote-end-tag
677682
(footnote--index-to-string
678-
(caar (last footnote-text-marker-alist)))))))
683+
(caar (last footnote--markers-alist)))))))
679684

680685
(defun footnote--fill-prefix-string ()
681686
"Return the fill prefix to be used by footnote mode."
@@ -695,13 +700,12 @@ With optional arg BEFORE-TAG, return position of the `footnote-section-tag'
695700
instead, if applicable."
696701
(cond
697702
;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead?
698-
((not footnote-text-marker-alist) (point-max))
699-
((not before-tag) (cdr (car footnote-text-marker-alist)))
700-
((string-equal footnote-section-tag "")
701-
(cdr (car footnote-text-marker-alist)))
703+
((not (footnote--first-text-marker)) (point-max))
704+
((not before-tag) (footnote--first-text-marker))
705+
((string-equal footnote-section-tag "") (footnote--first-text-marker))
702706
(t
703707
(save-excursion
704-
(goto-char (cdr (car footnote-text-marker-alist)))
708+
(goto-char (footnote--first-text-marker))
705709
(if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t)
706710
(match-beginning 0)
707711
(message "Footnote section tag not found!")
@@ -721,7 +725,7 @@ instead, if applicable."
721725
;; function, and repeat.
722726
;;
723727
;; TODO: integrate sanity checks at reasonable operational points.
724-
(cdr (car footnote-text-marker-alist)))))))
728+
(footnote--first-text-marker))))))
725729

726730
(defun footnote--get-area-point-max ()
727731
"Return the end of footnote area.
@@ -747,22 +751,20 @@ footnote area, returns `point-max'."
747751
(defun footnote--make-hole ()
748752
(save-excursion
749753
(let ((i 0)
750-
(notes (length footnote-pointer-marker-alist))
751-
alist-ptr alist-txt rc)
754+
(notes (length footnote--markers-alist))
755+
alist-elem rc)
752756
(while (< i notes)
753-
(setq alist-ptr (nth i footnote-pointer-marker-alist))
754-
(setq alist-txt (nth i footnote-text-marker-alist))
755-
(when (< (point) (- (cadr alist-ptr) 3))
757+
(setq alist-elem (nth i footnote--markers-alist))
758+
(when (< (point) (- (cl-caddr alist-elem) 3))
756759
(unless rc
757-
(setq rc (car alist-ptr)))
760+
(setq rc (car alist-elem)))
758761
(save-excursion
759762
(message "Renumbering from %s to %s"
760-
(footnote--index-to-string (car alist-ptr))
763+
(footnote--index-to-string (car alist-elem))
761764
(footnote--index-to-string
762-
(1+ (car alist-ptr))))
763-
(footnote--renumber (1+ (car alist-ptr))
764-
alist-ptr
765-
alist-txt)))
765+
(1+ (car alist-elem))))
766+
(footnote--renumber (1+ (car alist-elem))
767+
alist-elem)))
766768
(setq i (1+ i)))
767769
rc)))
768770

@@ -775,10 +777,10 @@ the buffer is narrowed to the footnote body. The restriction is removed
775777
by using `footnote-back-to-message'."
776778
(interactive "*")
777779
(let ((num
778-
(if footnote-text-marker-alist
779-
(if (< (point) (cl-cadar (last footnote-pointer-marker-alist)))
780+
(if footnote--markers-alist
781+
(if (< (point) (cl-caddar (last footnote--markers-alist)))
780782
(footnote--make-hole)
781-
(1+ (caar (last footnote-text-marker-alist))))
783+
(1+ (caar (last footnote--markers-alist))))
782784
1)))
783785
(message "Adding footnote %d" num)
784786
(footnote--insert-footnote num)
@@ -805,12 +807,11 @@ delete the footnote with that number."
805807
(when (and arg
806808
(or (not footnote-prompt-before-deletion)
807809
(y-or-n-p (format "Really delete footnote %d?" arg))))
808-
(let (alist-ptr alist-txt locn)
809-
(setq alist-ptr (assq arg footnote-pointer-marker-alist))
810-
(setq alist-txt (assq arg footnote-text-marker-alist))
811-
(unless (and alist-ptr alist-txt)
810+
(let (alist-elem locn)
811+
(setq alist-elem (assq arg footnote--markers-alist))
812+
(unless alist-elem
812813
(error "Can't delete footnote %d" arg))
813-
(setq locn (cdr alist-ptr))
814+
(setq locn (cddr alist-elem))
814815
(while (car locn)
815816
(save-excursion
816817
(goto-char (car locn))
@@ -821,7 +822,7 @@ delete the footnote with that number."
821822
(delete-region (match-beginning 0) (match-end 0))))
822823
(setq locn (cdr locn)))
823824
(save-excursion
824-
(goto-char (cdr alist-txt))
825+
(goto-char (cadr alist-elem))
825826
(delete-region
826827
(point)
827828
(if footnote-spaced-footnotes
@@ -830,13 +831,10 @@ delete the footnote with that number."
830831
(end-of-line)
831832
(next-single-char-property-change
832833
(point) 'footnote-number nil (footnote--goto-char-point-max))))))
833-
(setq footnote-pointer-marker-alist
834-
(delq alist-ptr footnote-pointer-marker-alist))
835-
(setq footnote-text-marker-alist
836-
(delq alist-txt footnote-text-marker-alist))
834+
(setq footnote--markers-alist
835+
(delq alist-elem footnote--markers-alist))
837836
(footnote-renumber-footnotes)
838-
(when (and (null footnote-text-marker-alist)
839-
(null footnote-pointer-marker-alist))
837+
(when (null footnote--markers-alist)
840838
(save-excursion
841839
(if (not (string-equal footnote-section-tag ""))
842840
(let* ((end (footnote--goto-char-point-max))
@@ -858,13 +856,12 @@ delete the footnote with that number."
858856
(interactive "*")
859857
(save-excursion
860858
(let ((i 0)
861-
(notes (length footnote-pointer-marker-alist))
862-
alist-ptr alist-txt)
859+
(notes (length footnote--markers-alist))
860+
alist-elem)
863861
(while (< i notes)
864-
(setq alist-ptr (nth i footnote-pointer-marker-alist))
865-
(setq alist-txt (nth i footnote-text-marker-alist))
866-
(unless (= (1+ i) (car alist-ptr))
867-
(footnote--renumber (1+ i) alist-ptr alist-txt))
862+
(setq alist-elem (nth i footnote--markers-alist))
863+
(unless (= (1+ i) (car alist-elem))
864+
(footnote--renumber (1+ i) alist-elem))
868865
(setq i (1+ i))))))
869866

870867
(defun footnote-goto-footnote (&optional arg)
@@ -874,18 +871,18 @@ specified, jump to the text of that footnote."
874871
(interactive "P")
875872
(unless arg
876873
(setq arg (footnote--under-cursor)))
877-
(let ((footnote (assq arg footnote-text-marker-alist)))
874+
(let ((footnote (assq arg footnote--markers-alist)))
878875
(cond
879876
(footnote
880-
(goto-char (cdr footnote)))
877+
(goto-char (cadr footnote)))
881878
((eq arg 0)
882879
(goto-char (point-max))
883880
(cond
884881
((not (string-equal footnote-section-tag ""))
885882
(re-search-backward (concat "^" footnote-section-tag-regexp))
886883
(forward-line 1))
887-
(footnote-text-marker-alist
888-
(goto-char (cdar footnote-text-marker-alist)))))
884+
((footnote--first-text-marker)
885+
(goto-char (footnote--first-text-marker)))))
889886
(t
890887
(error "I don't see a footnote here")))))
891888

@@ -899,7 +896,7 @@ being set it is automatically widened."
899896
(when note
900897
(when footnote-narrow-to-footnotes-when-editing
901898
(widen))
902-
(goto-char (cadr (assq note footnote-pointer-marker-alist))))))
899+
(goto-char (cl-caddr (assq note footnote--markers-alist))))))
903900

904901
(defvar footnote-mode-map
905902
(let ((map (make-sparse-keymap)))

0 commit comments

Comments
 (0)