@@ -457,7 +457,7 @@ footnote styles."
457
457
(let ((fn-regexp (footnote--current-regexp index-regexp)))
458
458
(save-excursion
459
459
(pcase-dolist (`(, fn , text . , pointers ) footnote--markers-alist)
460
- ; ; Take care of the pointers first
460
+ ; ; Take care of the pointers first.
461
461
(dolist (locn pointers)
462
462
(goto-char locn)
463
463
; ; Try to handle the case where `footnote-start-tag' and
@@ -505,15 +505,18 @@ footnote styles."
505
505
506
506
; ; Internal functions
507
507
(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 [...]."
509
510
(let ((string (concat footnote-start-tag
510
511
(footnote--index-to-string arg)
511
- footnote-end-tag)))
512
+ footnote-end-tag))
513
+ (pos (point )))
512
514
(insert
513
515
(if mousable
514
516
(propertize
515
517
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 )))
517
520
518
521
(defun footnote--renumber (to alist-elem )
519
522
" Renumber a single footnote."
@@ -550,33 +553,13 @@ footnote styles."
550
553
(or (re-search-backward footnote-signature-separator nil t )
551
554
(point )))
552
555
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)))
580
563
581
564
(defun footnote--goto-first ()
582
565
" Go to beginning of footnote area and return non-nil if successful.
@@ -586,42 +569,37 @@ Presumes we're within the footnote area already."
586
569
(re-search-backward
587
570
(concat " ^" footnote-section-tag-regexp) nil t ))
588
571
(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))))))
592
573
593
574
(defun footnote--insert-footnote (arg )
594
575
" Insert a footnote numbered ARG, at (point)."
595
576
(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))))
625
603
626
604
(defun footnote--sort (list )
627
605
(sort list #'car-less-than-car ))
@@ -671,14 +649,14 @@ With optional arg BEFORE-TAG, return position of the `footnote-section-tag'
671
649
instead, if applicable."
672
650
(cond
673
651
; ; 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) ))
677
655
(t
678
656
(save-excursion
679
- (goto-char (footnote--first-text-marker ))
657
+ (goto-char (cadr ( car footnote--markers-alist) ))
680
658
(if (re-search-backward (concat " ^" footnote-section-tag-regexp) nil t )
681
- (match-beginning 0 )
659
+ (point )
682
660
(message " Footnote section tag not found! " )
683
661
; ; This `else' should never happen, and indicates an error,
684
662
; ; ie. footnotes already exist and a footnote-section-tag is defined,
@@ -696,7 +674,7 @@ instead, if applicable."
696
674
; ; function, and repeat.
697
675
; ;
698
676
; ; TODO: integrate sanity checks at reasonable operational points.
699
- (footnote--first-text-marker ))))))
677
+ (point ))))))
700
678
701
679
(defun footnote--get-area-point-max ()
702
680
" Return the end of footnote area.
@@ -832,8 +810,8 @@ specified, jump to the text of that footnote."
832
810
((not (string-equal footnote-section-tag " " ))
833
811
(re-search-backward (concat " ^" footnote-section-tag-regexp))
834
812
(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) )))))
837
815
(t
838
816
(error " I don't see a footnote here " )))))
839
817
0 commit comments