Skip to content

Commit 0efaae7

Browse files
committed
* lisp/mail/footnote.el: Use dolist and hoist regexps out of loops
(footnote--refresh-footnotes): Use pcase-dolist; compute regexp once outside of the loops. Use less confusing `literal` arg to `replace-match` and specify `fixedcase` since footnote--index-to-string already chose the proper case for us. (footnote--renumber): Use dolist; compute regexp once outside of the loops; shortcircuit when number is unchanged. (footnote--text-under-cursor): Rewrite. (footnote--make-hole): Use dolist. (footnote-add-footnote): CSE. (footnote-delete-footnote): Use dolist; compute regexp once outside of the loop. (footnote-delete-footnote): Don't renumber if there's no footnote left. (footnote-renumber-footnotes): Use dolist.
1 parent cd3a7f3 commit 0efaae7

File tree

1 file changed

+77
-106
lines changed

1 file changed

+77
-106
lines changed

lisp/mail/footnote.el

Lines changed: 77 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,9 @@ left with the first character of footnote text."
164164
"List of (FN TEXT . POINTERS).
165165
Where FN is the footnote number, TEXT is a marker pointing to
166166
the footnote's text, and POINTERS is a list of markers pointing
167-
to the places from which the footnote is referenced.")
167+
to the places from which the footnote is referenced.
168+
TEXT points right *before* the [...] and POINTERS point right
169+
*after* the [...].")
168170

169171
(defvar footnote-mouse-highlight 'highlight
170172
;; FIXME: This `highlight' property is not currently used.
@@ -452,52 +454,41 @@ Conversion is done based upon the current selected style."
452454

453455
(defun footnote--refresh-footnotes (&optional index-regexp)
454456
"Redraw all footnotes.
455-
You must call this or arrange to have this called after changing footnote
456-
styles."
457-
(unless index-regexp
458-
(setq index-regexp (footnote--current-regexp)))
459-
(save-excursion
460-
;; Take care of the pointers first
461-
(let ((i 0) locn alist)
462-
(while (setq alist (nth i footnote--markers-alist))
463-
(setq locn (cddr alist))
464-
(while locn
465-
(goto-char (car locn))
457+
You must call this or arrange to have this called after changing
458+
footnote styles."
459+
(let ((fn-regexp (concat
460+
(regexp-quote footnote-start-tag)
461+
"\\(" (or index-regexp (footnote--current-regexp)) "+\\)"
462+
(regexp-quote footnote-end-tag))))
463+
(save-excursion
464+
(pcase-dolist (`(,fn ,text . ,pointers) footnote--markers-alist)
465+
;; Take care of the pointers first
466+
(dolist (locn pointers)
467+
(goto-char locn)
466468
;; Try to handle the case where `footnote-start-tag' and
467469
;; `footnote-end-tag' are the same string.
468-
(when (looking-back (concat
469-
(regexp-quote footnote-start-tag)
470-
"\\(" index-regexp "+\\)"
471-
(regexp-quote footnote-end-tag))
470+
(when (looking-back fn-regexp
472471
(line-beginning-position))
473472
(replace-match
474473
(propertize
475474
(concat
476475
footnote-start-tag
477-
(footnote--index-to-string (1+ i))
476+
(footnote--index-to-string fn)
478477
footnote-end-tag)
479-
'footnote-number (1+ i) footnote-mouse-highlight t)
480-
nil "\\1"))
481-
(setq locn (cdr locn)))
482-
(setq i (1+ i))))
483-
484-
;; Now take care of the text section
485-
(let ((i 0) alist)
486-
(while (setq alist (nth i footnote--markers-alist))
487-
(goto-char (cadr alist))
488-
(when (looking-at (concat
489-
(regexp-quote footnote-start-tag)
490-
"\\(" index-regexp "+\\)"
491-
(regexp-quote footnote-end-tag)))
478+
'footnote-number fn footnote-mouse-highlight t)
479+
t t)))
480+
481+
;; Now take care of the text section
482+
(goto-char text)
483+
(when (looking-at fn-regexp)
492484
(replace-match
493485
(propertize
494486
(concat
495487
footnote-start-tag
496-
(footnote--index-to-string (1+ i))
488+
(footnote--index-to-string fn)
497489
footnote-end-tag)
498-
'footnote-number (1+ i))
499-
nil "\\1"))
500-
(setq i (1+ i))))))
490+
'footnote-number fn)
491+
t t))))))
501492

502493
(defun footnote-cycle-style ()
503494
"Select next defined footnote style."
@@ -532,31 +523,28 @@ styles."
532523

533524
(defun footnote--renumber (to alist-elem)
534525
"Renumber a single footnote."
535-
(let* ((posn-list (cddr alist-elem)))
536-
(setcar alist-elem to)
537-
(while posn-list
538-
(goto-char (car posn-list))
539-
(when (looking-back (concat (regexp-quote footnote-start-tag)
540-
(footnote--current-regexp)
541-
(regexp-quote footnote-end-tag))
542-
(line-beginning-position))
543-
(replace-match
544-
(propertize
526+
(unless (equal to (car alist-elem)) ;Nothing to do.
527+
(let* ((fn-regexp (concat (regexp-quote footnote-start-tag)
528+
(footnote--current-regexp)
529+
(regexp-quote footnote-end-tag))))
530+
(setcar alist-elem to)
531+
(dolist (posn (cddr alist-elem))
532+
(goto-char posn)
533+
(when (looking-back fn-regexp (line-beginning-position))
534+
(replace-match
535+
(propertize
536+
(concat footnote-start-tag
537+
(footnote--index-to-string to)
538+
footnote-end-tag)
539+
'footnote-number to footnote-mouse-highlight t))))
540+
(goto-char (cadr alist-elem))
541+
(when (looking-at fn-regexp)
542+
(replace-match
543+
(propertize
545544
(concat footnote-start-tag
546545
(footnote--index-to-string to)
547546
footnote-end-tag)
548-
'footnote-number to footnote-mouse-highlight t)))
549-
(setq posn-list (cdr posn-list)))
550-
(goto-char (cadr alist-elem))
551-
(when (looking-at (concat (regexp-quote footnote-start-tag)
552-
(footnote--current-regexp)
553-
(regexp-quote footnote-end-tag)))
554-
(replace-match
555-
(propertize
556-
(concat footnote-start-tag
557-
(footnote--index-to-string to)
558-
footnote-end-tag)
559-
'footnote-number to)))))
547+
'footnote-number to))))))
560548

561549
(defun footnote--narrow-to-footnotes ()
562550
"Restrict text in buffer to show only text of footnotes."
@@ -652,18 +640,11 @@ Presumes we're within the footnote area already."
652640
"Return the number of the current footnote if in footnote text.
653641
Return nil if the cursor is not positioned over the text of
654642
a footnote."
655-
(when (and footnote--markers-alist
656-
(<= (footnote--get-area-point-min)
657-
(point)
658-
(footnote--get-area-point-max)))
659-
(let ((i 1) alist-txt result)
660-
(while (and (setq alist-txt (nth i footnote--markers-alist))
661-
(null result))
662-
(when (< (point) (cadr alist-txt))
663-
(setq result (car (nth (1- i) footnote--markers-alist))))
664-
(setq i (1+ i)))
665-
(when (and (null result) (null alist-txt))
666-
(setq result (car (nth (1- i) footnote--markers-alist))))
643+
(when (<= (point) (footnote--get-area-point-max))
644+
(let ((result nil))
645+
(pcase-dolist (`(,fn ,text . ,_) footnote--markers-alist)
646+
(if (<= text (point))
647+
(setq result fn)))
667648
result)))
668649

669650
(defun footnote--under-cursor ()
@@ -750,11 +731,8 @@ footnote area, returns `point-max'."
750731

751732
(defun footnote--make-hole ()
752733
(save-excursion
753-
(let ((i 0)
754-
(notes (length footnote--markers-alist))
755-
alist-elem rc)
756-
(while (< i notes)
757-
(setq alist-elem (nth i footnote--markers-alist))
734+
(let (rc)
735+
(dolist (alist-elem footnote--markers-alist)
758736
(when (< (point) (- (cl-caddr alist-elem) 3))
759737
(unless rc
760738
(setq rc (car alist-elem)))
@@ -764,8 +742,7 @@ footnote area, returns `point-max'."
764742
(footnote--index-to-string
765743
(1+ (car alist-elem))))
766744
(footnote--renumber (1+ (car alist-elem))
767-
alist-elem)))
768-
(setq i (1+ i)))
745+
alist-elem))))
769746
rc)))
770747

771748
(defun footnote-add-footnote ()
@@ -778,9 +755,10 @@ by using `footnote-back-to-message'."
778755
(interactive "*")
779756
(let ((num
780757
(if footnote--markers-alist
781-
(if (< (point) (cl-caddar (last footnote--markers-alist)))
782-
(footnote--make-hole)
783-
(1+ (caar (last footnote--markers-alist))))
758+
(let ((last (car (last footnote--markers-alist))))
759+
(if (< (point) (cl-caddr last))
760+
(footnote--make-hole)
761+
(1+ (car last))))
784762
1)))
785763
(message "Adding footnote %d" num)
786764
(footnote--insert-footnote num)
@@ -807,20 +785,17 @@ delete the footnote with that number."
807785
(when (and arg
808786
(or (not footnote-prompt-before-deletion)
809787
(y-or-n-p (format "Really delete footnote %d?" arg))))
810-
(let (alist-elem locn)
811-
(setq alist-elem (assq arg footnote--markers-alist))
812-
(unless alist-elem
813-
(error "Can't delete footnote %d" arg))
814-
(setq locn (cddr alist-elem))
815-
(while (car locn)
788+
(let ((alist-elem (or (assq arg footnote--markers-alist)
789+
(error "Can't delete footnote %d" arg)))
790+
(fn-regexp (concat (regexp-quote footnote-start-tag)
791+
(footnote--current-regexp)
792+
(regexp-quote footnote-end-tag))))
793+
(dolist (locn (cddr alist-elem))
816794
(save-excursion
817-
(goto-char (car locn))
818-
(when (looking-back (concat (regexp-quote footnote-start-tag)
819-
(footnote--current-regexp)
820-
(regexp-quote footnote-end-tag))
795+
(goto-char locn)
796+
(when (looking-back fn-regexp
821797
(line-beginning-position))
822-
(delete-region (match-beginning 0) (match-end 0))))
823-
(setq locn (cdr locn)))
798+
(delete-region (match-beginning 0) (match-end 0)))))
824799
(save-excursion
825800
(goto-char (cadr alist-elem))
826801
(delete-region
@@ -833,8 +808,8 @@ delete the footnote with that number."
833808
(point) 'footnote-number nil (footnote--goto-char-point-max))))))
834809
(setq footnote--markers-alist
835810
(delq alist-elem footnote--markers-alist))
836-
(footnote-renumber-footnotes)
837-
(when (null footnote--markers-alist)
811+
(if footnote--markers-alist
812+
(footnote-renumber-footnotes)
838813
(save-excursion
839814
(if (not (string-equal footnote-section-tag ""))
840815
(let* ((end (footnote--goto-char-point-max))
@@ -855,13 +830,9 @@ delete the footnote with that number."
855830
"Renumber footnotes, starting from 1."
856831
(interactive "*")
857832
(save-excursion
858-
(let ((i 0)
859-
(notes (length footnote--markers-alist))
860-
alist-elem)
861-
(while (< i notes)
862-
(setq alist-elem (nth i footnote--markers-alist))
863-
(unless (= (1+ i) (car alist-elem))
864-
(footnote--renumber (1+ i) alist-elem))
833+
(let ((i 1))
834+
(dolist (alist-elem footnote--markers-alist)
835+
(footnote--renumber i alist-elem)
865836
(setq i (1+ i))))))
866837

867838
(defun footnote-goto-footnote (&optional arg)
@@ -900,13 +871,13 @@ being set it is automatically widened."
900871

901872
(defvar footnote-mode-map
902873
(let ((map (make-sparse-keymap)))
903-
(define-key map "a" 'footnote-add-footnote)
904-
(define-key map "b" 'footnote-back-to-message)
905-
(define-key map "c" 'footnote-cycle-style)
906-
(define-key map "d" 'footnote-delete-footnote)
907-
(define-key map "g" 'footnote-goto-footnote)
908-
(define-key map "r" 'footnote-renumber-footnotes)
909-
(define-key map "s" 'footnote-set-style)
874+
(define-key map "a" #'footnote-add-footnote)
875+
(define-key map "b" #'footnote-back-to-message)
876+
(define-key map "c" #'footnote-cycle-style)
877+
(define-key map "d" #'footnote-delete-footnote)
878+
(define-key map "g" #'footnote-goto-footnote)
879+
(define-key map "r" #'footnote-renumber-footnotes)
880+
(define-key map "s" #'footnote-set-style)
910881
map))
911882

912883
(defvar footnote-minor-mode-map

0 commit comments

Comments
 (0)