Commit c9b820dd authored by Stefan Monnier's avatar Stefan Monnier
Browse files

* lisp/mail/footnote.el: Minor simplifications

Remove redundant :group args.
(footnote-mode-hook): Let define-minor-mode define it.
(footnote--style-p): Delete function.
(footnote--index-to-string): Inline it instead, and simplify.
(footnote-cycle-style): Use a pointer into the alist as the "index"
instead of a number.
(footnote-set-style): Use footnote-style-alist as the completion table.
Prefer `assq` over `footnote--assoc-index`.
(footnote--assoc-index): Delete function.
(footnote--renumber): Remove first (unused) argument; Adjust all callers.
(footnote--sort): Use car-less-than-car.
parent d4fa998c
Pipeline #1524 passed with stage
in 86 minutes and 13 seconds
...@@ -73,50 +73,38 @@ ...@@ -73,50 +73,38 @@
(defcustom footnote-mode-line-string " FN" (defcustom footnote-mode-line-string " FN"
"String to display in modes section of the mode-line." "String to display in modes section of the mode-line."
:type 'string :type 'string)
:group 'footnote)
(defcustom footnote-mode-hook nil
"Hook functions run when footnote-mode is activated."
:type 'hook
:group 'footnote)
(defcustom footnote-narrow-to-footnotes-when-editing nil (defcustom footnote-narrow-to-footnotes-when-editing nil
"If non-nil, narrow to footnote text body while editing a footnote." "If non-nil, narrow to footnote text body while editing a footnote."
:type 'boolean :type 'boolean)
:group 'footnote)
(defcustom footnote-prompt-before-deletion t (defcustom footnote-prompt-before-deletion t
"If non-nil, prompt before deleting a footnote. "If non-nil, prompt before deleting a footnote.
There is currently no way to undo deletions." There is currently no way to undo deletions."
:type 'boolean :type 'boolean)
:group 'footnote)
(defcustom footnote-spaced-footnotes t (defcustom footnote-spaced-footnotes t
"If non-nil, insert an empty line between footnotes. "If non-nil, insert an empty line between footnotes.
Customizing this variable has no effect on buffers already Customizing this variable has no effect on buffers already
displaying footnotes." displaying footnotes."
:type 'boolean :type 'boolean)
:group 'footnote)
(defcustom footnote-use-message-mode t ; Nowhere used. (defcustom footnote-use-message-mode t ; Nowhere used.
"If non-nil, assume Footnoting will be done in `message-mode'." "If non-nil, assume Footnoting will be done in `message-mode'."
:type 'boolean :type 'boolean)
:group 'footnote)
(defcustom footnote-body-tag-spacing 2 (defcustom footnote-body-tag-spacing 2
"Number of spaces separating a footnote body tag and its text. "Number of spaces separating a footnote body tag and its text.
Customizing this variable has no effect on buffers already Customizing this variable has no effect on buffers already
displaying footnotes." displaying footnotes."
:type 'integer :type 'integer)
:group 'footnote)
(defcustom footnote-prefix [(control ?c) ?!] (defcustom footnote-prefix [(control ?c) ?!]
"Prefix key to use for Footnote command in Footnote minor mode. "Prefix key to use for Footnote command in Footnote minor mode.
The value of this variable is checked as part of loading Footnote mode. The value of this variable is checked as part of loading Footnote mode.
After that, changing the prefix key requires manipulating keymaps." After that, changing the prefix key requires manipulating keymaps."
:type 'key-sequence :type 'key-sequence)
:group 'footnote)
;;; Interface variables that probably shouldn't be changed ;;; Interface variables that probably shouldn't be changed
...@@ -127,8 +115,7 @@ value of `footnote-section-tag-regexp' is ignored. Customizing ...@@ -127,8 +115,7 @@ value of `footnote-section-tag-regexp' is ignored. Customizing
this variable has no effect on buffers already displaying this variable has no effect on buffers already displaying
footnotes." footnotes."
:version "27.1" :version "27.1"
:type 'string :type 'string)
:group 'footnote)
(defcustom footnote-section-tag-regexp (defcustom footnote-section-tag-regexp
;; Even if `footnote-section-tag' has a trailing space, let's not require it ;; Even if `footnote-section-tag' has a trailing space, let's not require it
...@@ -139,31 +126,27 @@ This variable is disregarded when `footnote-section-tag' is the ...@@ -139,31 +126,27 @@ This variable is disregarded when `footnote-section-tag' is the
empty string. Customizing this variable has no effect on buffers empty string. Customizing this variable has no effect on buffers
already displaying footnotes." already displaying footnotes."
:version "27.1" :version "27.1"
:type 'regexp :type 'regexp)
:group 'footnote)
;; The following three should be consumed by footnote styles. ;; The following three should be consumed by footnote styles.
(defcustom footnote-start-tag "[" (defcustom footnote-start-tag "["
"String used to denote start of numbered footnote. "String used to denote start of numbered footnote.
Should not be set to the empty string. Customizing this variable Should not be set to the empty string. Customizing this variable
has no effect on buffers already displaying footnotes." has no effect on buffers already displaying footnotes."
:type 'string :type 'string)
:group 'footnote)
(defcustom footnote-end-tag "]" (defcustom footnote-end-tag "]"
"String used to denote end of numbered footnote. "String used to denote end of numbered footnote.
Should not be set to the empty string. Customizing this variable Should not be set to the empty string. Customizing this variable
has no effect on buffers already displaying footnotes." has no effect on buffers already displaying footnotes."
:type 'string :type 'string)
:group 'footnote)
(defcustom footnote-signature-separator (defcustom footnote-signature-separator
(if (boundp 'message-signature-separator) (if (boundp 'message-signature-separator)
message-signature-separator message-signature-separator
"^-- $") "^-- $")
"Regexp used by Footnote mode to recognize signatures." "Regexp used by Footnote mode to recognize signatures."
:type 'regexp :type 'regexp)
:group 'footnote)
(defcustom footnote-align-to-fn-text t (defcustom footnote-align-to-fn-text t
"How to left-align footnote text. "How to left-align footnote text.
...@@ -187,6 +170,8 @@ left with the first character of footnote text." ...@@ -187,6 +170,8 @@ left with the first character of footnote text."
(make-variable-buffer-local 'footnote-pointer-marker-alist) (make-variable-buffer-local 'footnote-pointer-marker-alist)
(defvar footnote-mouse-highlight 'highlight (defvar footnote-mouse-highlight 'highlight
;; FIXME: This `highlight' property is not currently used.
;; We should use `mouse-face' and make mouse clicks work on them.
"Text property name to enable mouse over highlight.") "Text property name to enable mouse over highlight.")
(defvar footnote-mode) (defvar footnote-mode)
...@@ -441,20 +426,15 @@ Customizing this variable has no effect on buffers already ...@@ -441,20 +426,15 @@ Customizing this variable has no effect on buffers already
displaying footnotes. To change the style of footnotes in such a displaying footnotes. To change the style of footnotes in such a
buffer use the command `footnote-set-style'." buffer use the command `footnote-set-style'."
:type (cons 'choice (mapcar (lambda (x) (list 'const (car x))) :type (cons 'choice (mapcar (lambda (x) (list 'const (car x)))
footnote-style-alist)) footnote-style-alist)))
:group 'footnote)
;;; Style utilities & functions ;;; Style utilities & functions
(defun footnote--style-p (style)
"Return non-nil if style is a valid style known to `footnote-mode'."
(assq style footnote-style-alist))
(defun footnote--index-to-string (index) (defun footnote--index-to-string (index)
"Convert a binary index into a string to display as a footnote. "Convert a binary index into a string to display as a footnote.
Conversion is done based upon the current selected style." Conversion is done based upon the current selected style."
(let ((alist (if (footnote--style-p footnote-style) (let ((alist (or (assq footnote-style footnote-style-alist)
(assq footnote-style footnote-style-alist) (nth 0 footnote-style-alist))))
(nth 0 footnote-style-alist))))
(funcall (nth 1 alist) index))) (funcall (nth 1 alist) index)))
(defun footnote--current-regexp () (defun footnote--current-regexp ()
...@@ -522,41 +502,27 @@ styles." ...@@ -522,41 +502,27 @@ styles."
nil "\\1")) nil "\\1"))
(setq i (1+ i)))))) (setq i (1+ i))))))
(defun footnote--assoc-index (key alist)
"Give index of key in alist."
(let ((i 0) (max (length alist)) rc)
(while (and (null rc)
(< i max))
(when (eq key (car (nth i alist)))
(setq rc i))
(setq i (1+ i)))
rc))
(defun footnote-cycle-style () (defun footnote-cycle-style ()
"Select next defined footnote style." "Select next defined footnote style."
(interactive) (interactive)
(let ((old (footnote--assoc-index footnote-style footnote-style-alist)) (let ((old-desc (assq footnote-style footnote-style-alist)))
(max (length footnote-style-alist)) (setq footnote-style (caar (or (cdr (memq old-desc footnote-style-alist))
idx) footnote-style-alist)))
(setq idx (1+ old)) (footnote--refresh-footnotes (nth 2 old-desc))))
(when (>= idx max)
(setq idx 0)) (defun footnote-set-style (style)
(setq footnote-style (car (nth idx footnote-style-alist)))
(footnote--refresh-footnotes (nth 2 (nth old footnote-style-alist)))))
(defun footnote-set-style (&optional style)
"Select a specific style." "Select a specific style."
(interactive (interactive
(list (intern (completing-read (list (intern (completing-read
"Footnote Style: " "Footnote Style: "
obarray #'footnote--style-p 'require-match)))) footnote-style-alist nil 'require-match))))
(let ((old (footnote--assoc-index footnote-style footnote-style-alist))) (let ((old-desc (assq footnote-style footnote-style-alist)))
(setq footnote-style style) (setq footnote-style style)
(footnote--refresh-footnotes (nth 2 (nth old footnote-style-alist))))) (footnote--refresh-footnotes (nth 2 old-desc))))
;; Internal functions ;; Internal functions
(defun footnote--insert-numbered-footnote (arg &optional mousable) (defun footnote--insert-numbered-footnote (arg &optional mousable)
"Insert numbered footnote at (point)." "Insert numbered footnote at point."
(let ((string (concat footnote-start-tag (let ((string (concat footnote-start-tag
(footnote--index-to-string arg) (footnote--index-to-string arg)
footnote-end-tag))) footnote-end-tag)))
...@@ -566,7 +532,7 @@ styles." ...@@ -566,7 +532,7 @@ styles."
string 'footnote-number arg footnote-mouse-highlight t) string 'footnote-number arg footnote-mouse-highlight t)
(propertize string 'footnote-number arg))))) (propertize string 'footnote-number arg)))))
(defun footnote--renumber (_from to pointer-alist text-alist) (defun footnote--renumber (to pointer-alist text-alist)
"Renumber a single footnote." "Renumber a single footnote."
(let* ((posn-list (cdr pointer-alist))) (let* ((posn-list (cdr pointer-alist)))
(setcar pointer-alist to) (setcar pointer-alist to)
...@@ -675,8 +641,7 @@ styles." ...@@ -675,8 +641,7 @@ styles."
(footnote--insert-text-marker arg old-point))) (footnote--insert-text-marker arg old-point)))
(defun footnote--sort (list) (defun footnote--sort (list)
(sort list (lambda (e1 e2) (sort list #'car-less-than-car))
(< (car e1) (car e2)))))
(defun footnote--text-under-cursor () (defun footnote--text-under-cursor ()
"Return the number of the current footnote if in footnote text. "Return the number of the current footnote if in footnote text.
...@@ -795,8 +760,7 @@ footnote area, returns `point-max'." ...@@ -795,8 +760,7 @@ footnote area, returns `point-max'."
(footnote--index-to-string (car alist-ptr)) (footnote--index-to-string (car alist-ptr))
(footnote--index-to-string (footnote--index-to-string
(1+ (car alist-ptr)))) (1+ (car alist-ptr))))
(footnote--renumber (car alist-ptr) (footnote--renumber (1+ (car alist-ptr))
(1+ (car alist-ptr))
alist-ptr alist-ptr
alist-txt))) alist-txt)))
(setq i (1+ i))) (setq i (1+ i)))
...@@ -900,7 +864,7 @@ delete the footnote with that number." ...@@ -900,7 +864,7 @@ delete the footnote with that number."
(setq alist-ptr (nth i footnote-pointer-marker-alist)) (setq alist-ptr (nth i footnote-pointer-marker-alist))
(setq alist-txt (nth i footnote-text-marker-alist)) (setq alist-txt (nth i footnote-text-marker-alist))
(unless (= (1+ i) (car alist-ptr)) (unless (= (1+ i) (car alist-ptr))
(footnote--renumber (car alist-ptr) (1+ i) alist-ptr alist-txt)) (footnote--renumber (1+ i) alist-ptr alist-txt))
(setq i (1+ i)))))) (setq i (1+ i))))))
(defun footnote-goto-footnote (&optional arg) (defun footnote-goto-footnote (&optional arg)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment