diff options
| author | Stefan Monnier | 2019-05-02 11:00:20 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2019-05-02 11:00:20 -0400 |
| commit | 17a722982cca4e8e643c7a9102903e820e784cc6 (patch) | |
| tree | b51f9ab4880001e15f024e32547d4573bdf5f316 | |
| parent | 39acaff574140b67ef5311c5764a6b3b29491991 (diff) | |
| download | emacs-17a722982cca4e8e643c7a9102903e820e784cc6.tar.gz emacs-17a722982cca4e8e643c7a9102903e820e784cc6.zip | |
* 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.
| -rw-r--r-- | lisp/mail/footnote.el | 120 |
1 files changed, 49 insertions, 71 deletions
diff --git a/lisp/mail/footnote.el b/lisp/mail/footnote.el index d985444a8e1..327eda11dc5 100644 --- a/lisp/mail/footnote.el +++ b/lisp/mail/footnote.el | |||
| @@ -457,7 +457,7 @@ footnote styles." | |||
| 457 | (let ((fn-regexp (footnote--current-regexp index-regexp))) | 457 | (let ((fn-regexp (footnote--current-regexp index-regexp))) |
| 458 | (save-excursion | 458 | (save-excursion |
| 459 | (pcase-dolist (`(,fn ,text . ,pointers) footnote--markers-alist) | 459 | (pcase-dolist (`(,fn ,text . ,pointers) footnote--markers-alist) |
| 460 | ;; Take care of the pointers first | 460 | ;; Take care of the pointers first. |
| 461 | (dolist (locn pointers) | 461 | (dolist (locn pointers) |
| 462 | (goto-char locn) | 462 | (goto-char locn) |
| 463 | ;; Try to handle the case where `footnote-start-tag' and | 463 | ;; Try to handle the case where `footnote-start-tag' and |
| @@ -505,15 +505,18 @@ footnote styles." | |||
| 505 | 505 | ||
| 506 | ;; Internal functions | 506 | ;; Internal functions |
| 507 | (defun footnote--insert-numbered-footnote (arg &optional mousable) | 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 | (let ((string (concat footnote-start-tag | 510 | (let ((string (concat footnote-start-tag |
| 510 | (footnote--index-to-string arg) | 511 | (footnote--index-to-string arg) |
| 511 | footnote-end-tag))) | 512 | footnote-end-tag)) |
| 513 | (pos (point))) | ||
| 512 | (insert | 514 | (insert |
| 513 | (if mousable | 515 | (if mousable |
| 514 | (propertize | 516 | (propertize |
| 515 | string 'footnote-number arg footnote-mouse-highlight t) | 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 | (defun footnote--renumber (to alist-elem) | 521 | (defun footnote--renumber (to alist-elem) |
| 519 | "Renumber a single footnote." | 522 | "Renumber a single footnote." |
| @@ -550,33 +553,13 @@ footnote styles." | |||
| 550 | (or (re-search-backward footnote-signature-separator nil t) | 553 | (or (re-search-backward footnote-signature-separator nil t) |
| 551 | (point))) | 554 | (point))) |
| 552 | 555 | ||
| 553 | (defun footnote--insert-text-marker (arg locn) | 556 | (defun footnote--insert-markers (arg text ptr) |
| 554 | "Insert a marker pointing to footnote ARG, at buffer location LOCN." | 557 | "Insert the markers of new footnote ARG." |
| 555 | (let ((entry (assq arg footnote--markers-alist))) | 558 | (cl-assert (and (numberp arg) (markerp text) (markerp ptr))) |
| 556 | (unless (cadr entry) | 559 | (cl-assert (not (assq arg footnote--markers-alist))) |
| 557 | (let ((marker (copy-marker locn t))) | 560 | (push `(,arg ,text ,ptr) footnote--markers-alist) |
| 558 | (if entry | 561 | (setq footnote--markers-alist |
| 559 | (setf (cadr entry) marker) | 562 | (footnote--sort footnote--markers-alist))) |
| 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)))) | ||
| 580 | 563 | ||
| 581 | (defun footnote--goto-first () | 564 | (defun footnote--goto-first () |
| 582 | "Go to beginning of footnote area and return non-nil if successful. | 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 | (re-search-backward | 569 | (re-search-backward |
| 587 | (concat "^" footnote-section-tag-regexp) nil t)) | 570 | (concat "^" footnote-section-tag-regexp) nil t)) |
| 588 | (footnote--markers-alist | 571 | (footnote--markers-alist |
| 589 | (let ((pos (footnote--first-text-marker))) | 572 | (goto-char (cadr (car footnote--markers-alist)))))) |
| 590 | (when pos | ||
| 591 | (goto-char pos)))))) | ||
| 592 | 573 | ||
| 593 | (defun footnote--insert-footnote (arg) | 574 | (defun footnote--insert-footnote (arg) |
| 594 | "Insert a footnote numbered ARG, at (point)." | 575 | "Insert a footnote numbered ARG, at (point)." |
| 595 | (push-mark) | 576 | (push-mark) |
| 596 | (let ((old-point (point))) | 577 | (let ((ptr (footnote--insert-numbered-footnote arg t))) |
| 597 | (footnote--insert-numbered-footnote arg t) | 578 | (footnote--goto-char-point-max) |
| 598 | (footnote--insert-pointer-marker arg old-point)) | 579 | (if (footnote--goto-first) |
| 599 | (footnote--goto-char-point-max) | 580 | (save-restriction |
| 600 | (if (footnote--goto-first) | 581 | (when footnote-narrow-to-footnotes-when-editing |
| 601 | (save-restriction | 582 | (footnote--narrow-to-footnotes)) |
| 602 | (when footnote-narrow-to-footnotes-when-editing | 583 | (footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now) |
| 603 | (footnote--narrow-to-footnotes)) | 584 | ;; (message "Inserting footnote %d" arg) |
| 604 | (footnote-goto-footnote (1- arg)) ; evil, FIXME (less evil now) | 585 | (or (eq arg 1) |
| 605 | ;; (message "Inserting footnote %d" arg) | 586 | (when (re-search-forward |
| 606 | (unless | 587 | (if footnote-spaced-footnotes |
| 607 | (or (eq arg 1) | 588 | "\n\n" |
| 608 | (when (re-search-forward | 589 | (concat "\n" (footnote--current-regexp))) |
| 609 | (if footnote-spaced-footnotes | 590 | nil t) |
| 610 | "\n\n" | 591 | (beginning-of-line) |
| 611 | (concat "\n" (footnote--current-regexp))) | 592 | t) |
| 612 | nil t) | 593 | (footnote--goto-char-point-max) |
| 613 | (unless (beginning-of-line) t)) | 594 | (footnote--goto-first))) |
| 614 | (footnote--goto-char-point-max) | 595 | (unless (looking-at "^$") |
| 615 | (footnote--goto-first)))) | 596 | (insert "\n")) |
| 616 | (unless (looking-at "^$") | 597 | (when (eobp) |
| 617 | (insert "\n")) | 598 | (insert "\n")) |
| 618 | (when (eobp) | 599 | (unless (string-equal footnote-section-tag "") |
| 619 | (insert "\n")) | 600 | (insert footnote-section-tag "\n"))) |
| 620 | (unless (string-equal footnote-section-tag "") | 601 | (let ((text (footnote--insert-numbered-footnote arg nil))) |
| 621 | (insert footnote-section-tag "\n"))) | 602 | (footnote--insert-markers arg text ptr)))) |
| 622 | (let ((old-point (point))) | ||
| 623 | (footnote--insert-numbered-footnote arg nil) | ||
| 624 | (footnote--insert-text-marker arg old-point))) | ||
| 625 | 603 | ||
| 626 | (defun footnote--sort (list) | 604 | (defun footnote--sort (list) |
| 627 | (sort list #'car-less-than-car)) | 605 | (sort list #'car-less-than-car)) |
| @@ -671,14 +649,14 @@ With optional arg BEFORE-TAG, return position of the `footnote-section-tag' | |||
| 671 | instead, if applicable." | 649 | instead, if applicable." |
| 672 | (cond | 650 | (cond |
| 673 | ;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead? | 651 | ;; FIXME: Shouldn't we use `footnote--get-area-point-max' instead? |
| 674 | ((not (footnote--first-text-marker)) (point-max)) | 652 | ((not footnote--markers-alist) (point-max)) |
| 675 | ((not before-tag) (footnote--first-text-marker)) | 653 | ((not before-tag) (cadr (car footnote--markers-alist))) |
| 676 | ((string-equal footnote-section-tag "") (footnote--first-text-marker)) | 654 | ((string-equal footnote-section-tag "") (cadr (car footnote--markers-alist))) |
| 677 | (t | 655 | (t |
| 678 | (save-excursion | 656 | (save-excursion |
| 679 | (goto-char (footnote--first-text-marker)) | 657 | (goto-char (cadr (car footnote--markers-alist))) |
| 680 | (if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t) | 658 | (if (re-search-backward (concat "^" footnote-section-tag-regexp) nil t) |
| 681 | (match-beginning 0) | 659 | (point) |
| 682 | (message "Footnote section tag not found!") | 660 | (message "Footnote section tag not found!") |
| 683 | ;; This `else' should never happen, and indicates an error, | 661 | ;; This `else' should never happen, and indicates an error, |
| 684 | ;; ie. footnotes already exist and a footnote-section-tag is defined, | 662 | ;; ie. footnotes already exist and a footnote-section-tag is defined, |
| @@ -696,7 +674,7 @@ instead, if applicable." | |||
| 696 | ;; function, and repeat. | 674 | ;; function, and repeat. |
| 697 | ;; | 675 | ;; |
| 698 | ;; TODO: integrate sanity checks at reasonable operational points. | 676 | ;; TODO: integrate sanity checks at reasonable operational points. |
| 699 | (footnote--first-text-marker)))))) | 677 | (point)))))) |
| 700 | 678 | ||
| 701 | (defun footnote--get-area-point-max () | 679 | (defun footnote--get-area-point-max () |
| 702 | "Return the end of footnote area. | 680 | "Return the end of footnote area. |
| @@ -832,8 +810,8 @@ specified, jump to the text of that footnote." | |||
| 832 | ((not (string-equal footnote-section-tag "")) | 810 | ((not (string-equal footnote-section-tag "")) |
| 833 | (re-search-backward (concat "^" footnote-section-tag-regexp)) | 811 | (re-search-backward (concat "^" footnote-section-tag-regexp)) |
| 834 | (forward-line 1)) | 812 | (forward-line 1)) |
| 835 | ((footnote--first-text-marker) | 813 | (footnote--markers-alist |
| 836 | (goto-char (footnote--first-text-marker))))) | 814 | (goto-char (cadr (car footnote--markers-alist)))))) |
| 837 | (t | 815 | (t |
| 838 | (error "I don't see a footnote here"))))) | 816 | (error "I don't see a footnote here"))))) |
| 839 | 817 | ||