@@ -164,7 +164,9 @@ left with the first character of footnote text."
164
164
" List of (FN TEXT . POINTERS).
165
165
Where FN is the footnote number, TEXT is a marker pointing to
166
166
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 [...]." )
168
170
169
171
(defvar footnote-mouse-highlight 'highlight
170
172
; ; FIXME: This `highlight' property is not currently used.
@@ -452,52 +454,41 @@ Conversion is done based upon the current selected style."
452
454
453
455
(defun footnote--refresh-footnotes (&optional index-regexp )
454
456
" 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)
466
468
; ; Try to handle the case where `footnote-start-tag' and
467
469
; ; `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
472
471
(line-beginning-position ))
473
472
(replace-match
474
473
(propertize
475
474
(concat
476
475
footnote-start-tag
477
- (footnote--index-to-string ( 1+ i) )
476
+ (footnote--index-to-string fn )
478
477
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)
492
484
(replace-match
493
485
(propertize
494
486
(concat
495
487
footnote-start-tag
496
- (footnote--index-to-string ( 1+ i) )
488
+ (footnote--index-to-string fn )
497
489
footnote-end-tag)
498
- 'footnote-number (1+ i))
499
- nil " \\ 1" ))
500
- (setq i (1+ i))))))
490
+ 'footnote-number fn)
491
+ t t ))))))
501
492
502
493
(defun footnote-cycle-style ()
503
494
" Select next defined footnote style."
@@ -532,31 +523,28 @@ styles."
532
523
533
524
(defun footnote--renumber (to alist-elem )
534
525
" 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
545
544
(concat footnote-start-tag
546
545
(footnote--index-to-string to)
547
546
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))))))
560
548
561
549
(defun footnote--narrow-to-footnotes ()
562
550
" Restrict text in buffer to show only text of footnotes."
@@ -652,18 +640,11 @@ Presumes we're within the footnote area already."
652
640
" Return the number of the current footnote if in footnote text.
653
641
Return nil if the cursor is not positioned over the text of
654
642
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)))
667
648
result)))
668
649
669
650
(defun footnote--under-cursor ()
@@ -750,11 +731,8 @@ footnote area, returns `point-max'."
750
731
751
732
(defun footnote--make-hole ()
752
733
(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)
758
736
(when (< (point ) (- (cl-caddr alist-elem) 3 ))
759
737
(unless rc
760
738
(setq rc (car alist-elem)))
@@ -764,8 +742,7 @@ footnote area, returns `point-max'."
764
742
(footnote--index-to-string
765
743
(1+ (car alist-elem))))
766
744
(footnote--renumber (1+ (car alist-elem))
767
- alist-elem)))
768
- (setq i (1+ i)))
745
+ alist-elem))))
769
746
rc)))
770
747
771
748
(defun footnote-add-footnote ()
@@ -778,9 +755,10 @@ by using `footnote-back-to-message'."
778
755
(interactive " *" )
779
756
(let ((num
780
757
(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 ))))
784
762
1 )))
785
763
(message " Adding footnote %d " num)
786
764
(footnote--insert-footnote num)
@@ -807,20 +785,17 @@ delete the footnote with that number."
807
785
(when (and arg
808
786
(or (not footnote-prompt-before-deletion)
809
787
(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) )
816
794
(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
821
797
(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 )))))
824
799
(save-excursion
825
800
(goto-char (cadr alist-elem))
826
801
(delete-region
@@ -833,8 +808,8 @@ delete the footnote with that number."
833
808
(point ) 'footnote-number nil (footnote--goto-char-point-max))))))
834
809
(setq footnote--markers-alist
835
810
(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 )
838
813
(save-excursion
839
814
(if (not (string-equal footnote-section-tag " " ))
840
815
(let* ((end (footnote--goto-char-point-max))
@@ -855,13 +830,9 @@ delete the footnote with that number."
855
830
" Renumber footnotes, starting from 1."
856
831
(interactive " *" )
857
832
(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)
865
836
(setq i (1+ i))))))
866
837
867
838
(defun footnote-goto-footnote (&optional arg )
@@ -900,13 +871,13 @@ being set it is automatically widened."
900
871
901
872
(defvar footnote-mode-map
902
873
(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 )
910
881
map))
911
882
912
883
(defvar footnote-minor-mode-map
0 commit comments