1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Improve documentation strings and prompts in abbrev.el

* lisp/abbrev.el (abbrev-file-name, only-global-abbrevs)
(copy-abbrev-table, insert-abbrevs, list-abbrevs)
(abbrev-table-name, edit-abbrevs, define-abbrevs)
(read-abbrev-file, quietly-read-abbrev-file, write-abbrev-file)
(abbrev-edit-save-to-file, abbrev-edit-save-buffer)
(add-mode-abbrev, add-global-abbrev, inverse-add-mode-abbrev)
(inverse-add-global-abbrev, abbrev-prefix-mark)
(expand-region-abbrevs, abbrev-table-get, abbrev-table-put)
(abbrev-get, abbrev-minor-mode-table-alist, abbrevs-changed)
(abbrev-all-caps, last-abbrev-text, last-abbrev-location)
(clear-abbrev-table, define-abbrev, define-global-abbrev)
(define-mode-abbrev, abbrev--active-tables, abbrev--symbol)
(abbrev-symbol, abbrev-expansion, abbrev-insert)
(abbrev-expand-function, abbrev--suggest-above-threshold)
(abbrev--suggest-saved-recommendations)
(abbrev--suggest-shortest-abbrev, abbrev--suggest-get-totals)
(insert-abbrev-table-description, define-abbrev-table)
(abbrev-table-menu): Fix doc strings: wording, punctuation,
clarity.
(add-abbrev, inverse-add-abbrev): Improve the prompt text.
(Bug#55527)
This commit is contained in:
Eli Zaretskii 2022-05-21 10:20:41 +03:00
parent dfa3e6f424
commit 90aae0bca2

View file

@ -42,12 +42,12 @@
(defcustom abbrev-file-name (defcustom abbrev-file-name
(locate-user-emacs-file "abbrev_defs" ".abbrev_defs") (locate-user-emacs-file "abbrev_defs" ".abbrev_defs")
"Default name of file from which to read abbrevs." "Default name of file from which to read and where to save abbrevs."
:initialize 'custom-initialize-delay :initialize 'custom-initialize-delay
:type 'file) :type 'file)
(defcustom only-global-abbrevs nil (defcustom only-global-abbrevs nil
"Non-nil means user plans to use global abbrevs only. "Non-nil means user plans to use only global abbrevs.
This makes the commands that normally define mode-specific abbrevs This makes the commands that normally define mode-specific abbrevs
define global abbrevs instead." define global abbrevs instead."
:type 'boolean :type 'boolean
@ -59,7 +59,8 @@ define global abbrevs instead."
In Abbrev mode, inserting an abbreviation causes it to expand and In Abbrev mode, inserting an abbreviation causes it to expand and
be replaced by its expansion." be replaced by its expansion."
;; It's defined in C, this stops the d-m-m macro defining it again. ;; It's defined in C, this stops the 'define-minor-mode' macro from
;; defining it again.
:variable abbrev-mode) :variable abbrev-mode)
(put 'abbrev-mode 'safe-local-variable 'booleanp) (put 'abbrev-mode 'safe-local-variable 'booleanp)
@ -83,7 +84,8 @@ be replaced by its expansion."
(defun copy-abbrev-table (table) (defun copy-abbrev-table (table)
"Make a new abbrev-table with the same abbrevs as TABLE. "Make a new abbrev-table with the same abbrevs as TABLE.
Does not copy property lists." This function does not copy property lists of the abbrevs.
See `define-abbrev' for the documentation of abbrev properties."
(let ((new-table (make-abbrev-table))) (let ((new-table (make-abbrev-table)))
(obarray-map (obarray-map
(lambda (symbol) (lambda (symbol)
@ -95,8 +97,8 @@ Does not copy property lists."
new-table)) new-table))
(defun insert-abbrevs () (defun insert-abbrevs ()
"Insert after point a description of all defined abbrevs. "Insert the description of all defined abbrevs after point.
Mark is set after the inserted text." Set mark after the inserted text."
(interactive) (interactive)
(push-mark (push-mark
(save-excursion (save-excursion
@ -105,15 +107,15 @@ Mark is set after the inserted text."
(point)))) (point))))
(defun list-abbrevs (&optional local) (defun list-abbrevs (&optional local)
"Display a list of defined abbrevs. "Display a list of the defined abbrevs.
If LOCAL is non-nil, interactively when invoked with a If LOCAL is non-nil (interactively, when invoked with a
prefix arg, display only local, i.e. mode-specific, abbrevs. prefix arg), display only local, i.e. mode-specific, abbrevs.
Otherwise display all abbrevs." Otherwise display all the abbrevs."
(interactive "P") (interactive "P")
(display-buffer (prepare-abbrev-list-buffer local))) (display-buffer (prepare-abbrev-list-buffer local)))
(defun abbrev-table-name (table) (defun abbrev-table-name (table)
"Value is the name of abbrev table TABLE." "Return the name of the specified abbrev TABLE."
(let ((tables abbrev-table-name-list) (let ((tables abbrev-table-name-list)
found) found)
(while (and (not found) tables) (while (and (not found) tables)
@ -137,33 +139,33 @@ Otherwise display all abbrevs."
(dolist (table (nreverse empty-tables)) (dolist (table (nreverse empty-tables))
(insert-abbrev-table-description table t))) (insert-abbrev-table-description table t)))
;; Note: `list-abbrevs' can display only local abbrevs, in ;; Note: `list-abbrevs' can display only local abbrevs, in
;; which case editing could lose abbrevs of other tables. Thus ;; which case editing could lose abbrevs of other tables.
;; enter `edit-abbrevs-mode' only if LOCAL is nil. ;; Thus enter `edit-abbrevs-mode' only if LOCAL is nil.
(edit-abbrevs-mode)) (edit-abbrevs-mode))
(goto-char (point-min)) (goto-char (point-min))
(set-buffer-modified-p nil) (set-buffer-modified-p nil)
(current-buffer)))) (current-buffer))))
(defun edit-abbrevs () (defun edit-abbrevs ()
"Alter abbrev definitions by editing a list of them. "Alter abbrev definitions by editing the list of abbrevs.
Selects a buffer containing a list of abbrev definitions with This selects a buffer containing the list of abbrev definitions
point located in the abbrev table for the current buffer, and with point located in the abbrev table for the current buffer, and
turns on `edit-abbrevs-mode' in that buffer. turns on `edit-abbrevs-mode' in the buffer with the list of abbrevs.
You can edit them and type \\<edit-abbrevs-mode-map>\\[edit-abbrevs-redefine] \ You can edit the abbrevs and type \\<edit-abbrevs-mode-map>\\[edit-abbrevs-redefine] \
to redefine abbrevs to redefine abbrevs
according to your editing. according to your editing.
The abbrevs editing buffer contains a header line for each The abbrevs editing buffer contains a header line for each
abbrev table, which is the abbrev table name in parentheses. abbrev table, which is the abbrev table name in parentheses.
This is followed by one line per abbrev in that table: The header line is followed by one line per abbrev in that table:
NAME USECOUNT EXPANSION HOOK NAME USECOUNT EXPANSION HOOK
where NAME and EXPANSION are strings with quotes, where NAME and EXPANSION are strings with quotes,
USECOUNT is an integer, and HOOK is any valid function USECOUNT is an integer, and HOOK is any valid function
or may be omitted (it is usually omitted)." that may be omitted (it is usually omitted)."
(interactive) (interactive)
(let ((table-name (abbrev-table-name local-abbrev-table))) (let ((table-name (abbrev-table-name local-abbrev-table)))
(switch-to-buffer (prepare-abbrev-list-buffer)) (switch-to-buffer (prepare-abbrev-list-buffer))
@ -184,8 +186,9 @@ or may be omitted (it is usually omitted)."
"Define abbrevs according to current visible buffer contents. "Define abbrevs according to current visible buffer contents.
See documentation of `edit-abbrevs' for info on the format of the See documentation of `edit-abbrevs' for info on the format of the
text you must have in the buffer. text you must have in the buffer.
With argument, eliminate all abbrev definitions except If ARG is non-nil (interactively, when invoked with a prefix
the ones defined from the buffer now." argument), eliminate all abbrev definitions except the ones
defined by the current buffer contents."
(interactive "P") (interactive "P")
(if arg (kill-all-abbrevs)) (if arg (kill-all-abbrevs))
(save-excursion (save-excursion
@ -216,7 +219,8 @@ the ones defined from the buffer now."
"Read abbrev definitions from file written with `write-abbrev-file'. "Read abbrev definitions from file written with `write-abbrev-file'.
Optional argument FILE is the name of the file to read; Optional argument FILE is the name of the file to read;
it defaults to the value of `abbrev-file-name'. it defaults to the value of `abbrev-file-name'.
Optional second argument QUIETLY non-nil means don't display a message." Optional second argument QUIETLY non-nil means don't display a message
about loading the abbrevs."
(interactive (interactive
(list (list
(read-file-name (format-prompt "Read abbrev file" abbrev-file-name) (read-file-name (format-prompt "Read abbrev file" abbrev-file-name)
@ -225,22 +229,22 @@ Optional second argument QUIETLY non-nil means don't display a message."
(setq abbrevs-changed nil)) (setq abbrevs-changed nil))
(defun quietly-read-abbrev-file (&optional file) (defun quietly-read-abbrev-file (&optional file)
"Read abbrev definitions from file written with `write-abbrev-file'. "Quietly read abbrev definitions from file written with `write-abbrev-file'.
Optional argument FILE is the name of the file to read; Optional argument FILE is the name of the file to read;
it defaults to the value of `abbrev-file-name'. it defaults to the value of `abbrev-file-name'.
Does not display any message." Do not display any messages about loading the abbrevs."
;(interactive "fRead abbrev file: ") ;(interactive "fRead abbrev file: ")
(read-abbrev-file file t)) (read-abbrev-file file t))
(defun write-abbrev-file (&optional file verbose) (defun write-abbrev-file (&optional file verbose)
"Write all user-level abbrev definitions to a file of Lisp code. "Write all user-level abbrev definitions to a file of Lisp code.
This does not include system abbrevs; it includes only the abbrev tables This does not include system abbrevs; it includes only the abbrev tables
listed in listed in `abbrev-table-name-list'. listed in `abbrev-table-name-list'.
The file written can be loaded in another session to define the same abbrevs. The file written can be loaded in another session to define the same abbrevs.
The argument FILE is the file name to write. If omitted or nil, the file The argument FILE is the file name to write. If omitted or nil, it defaults
specified in `abbrev-file-name' is used. to the value of `abbrev-file-name'.
If VERBOSE is non-nil, display a message indicating where abbrevs If VERBOSE is non-nil, display a message indicating the file where the
have been saved." abbrevs have been saved."
(interactive (interactive
(list (list
(read-file-name "Write abbrev file: " (read-file-name "Write abbrev file: "
@ -253,10 +257,10 @@ have been saved."
(dolist (table (dolist (table
;; We sort the table in order to ease the automatic ;; We sort the table in order to ease the automatic
;; merging of different versions of the user's abbrevs ;; merging of different versions of the user's abbrevs
;; file. This is useful, for example, for when the ;; file. This is useful, for example, when the
;; user keeps their home directory in a revision ;; user keeps their home directory in a revision
;; control system, and is therefore keeping multiple ;; control system, and therefore keeps multiple
;; slightly-differing copies loosely synchronized. ;; slightly-differing loosely synchronized copies.
(sort (copy-sequence abbrev-table-name-list) (sort (copy-sequence abbrev-table-name-list)
(lambda (s1 s2) (lambda (s1 s2)
(string< (symbol-name s1) (string< (symbol-name s1)
@ -270,7 +274,7 @@ have been saved."
(write-region nil nil file nil (and (not verbose) 0))))) (write-region nil nil file nil (and (not verbose) 0)))))
(defun abbrev-edit-save-to-file (file) (defun abbrev-edit-save-to-file (file)
"Save all user-level abbrev definitions in current buffer to FILE." "Save to FILE all the user-level abbrev definitions in current buffer."
(interactive (interactive
(list (read-file-name "Save abbrevs to file: " (list (read-file-name "Save abbrevs to file: "
(file-name-directory (file-name-directory
@ -280,7 +284,7 @@ have been saved."
(write-abbrev-file file t)) (write-abbrev-file file t))
(defun abbrev-edit-save-buffer () (defun abbrev-edit-save-buffer ()
"Save all user-level abbrev definitions in current buffer. "Save all the user-level abbrev definitions in current buffer.
The saved abbrevs are written to the file specified by The saved abbrevs are written to the file specified by
`abbrev-file-name'." `abbrev-file-name'."
(interactive) (interactive)
@ -288,15 +292,16 @@ The saved abbrevs are written to the file specified by
(defun add-mode-abbrev (arg) (defun add-mode-abbrev (arg)
"Define mode-specific abbrev for last word(s) before point. "Define a mode-specific abbrev whose expansion is the last word before point.
Argument is how many words before point form the expansion; Prefix argument ARG says how many words before point to use for the expansion;
or zero means the region is the expansion. zero means the entire region is the expansion.
A negative argument means to undefine the specified abbrev. A negative ARG means to undefine the specified abbrev.
Reads the abbreviation in the minibuffer.
This command reads the abbreviation from the minibuffer.
See also `inverse-add-mode-abbrev', which performs the opposite task: See also `inverse-add-mode-abbrev', which performs the opposite task:
if the abbrev text is already in the buffer, use this command to if the abbreviation is already in the buffer, use that command to define
define an abbrev by specifying the expansion in the minibuffer. a mode-specific abbrev by specifying its expansion in the minibuffer.
Don't use this function in a Lisp program; use `define-abbrev' instead." Don't use this function in a Lisp program; use `define-abbrev' instead."
(interactive "p") (interactive "p")
@ -308,15 +313,16 @@ Don't use this function in a Lisp program; use `define-abbrev' instead."
"Mode" arg)) "Mode" arg))
(defun add-global-abbrev (arg) (defun add-global-abbrev (arg)
"Define global (all modes) abbrev for last word(s) before point. "Define a global (all modes) abbrev whose expansion is last word before point.
The prefix argument specifies the number of words before point that form the Prefix argument ARG says how many words before point to use for the expansion;
expansion; or zero means the region is the expansion. zero means the entire region is the expansion.
A negative argument means to undefine the specified abbrev. A negative ARG means to undefine the specified abbrev.
This command uses the minibuffer to read the abbreviation.
This command reads the abbreviation from the minibuffer.
See also `inverse-add-global-abbrev', which performs the opposite task: See also `inverse-add-global-abbrev', which performs the opposite task:
if the abbrev text is already in the buffer, use this command to if the abbreviation is already in the buffer, use that command to define
define an abbrev by specifying the expansion in the minibuffer. a global abbrev by specifying its expansion in the minibuffer.
Don't use this function in a Lisp program; use `define-abbrev' instead." Don't use this function in a Lisp program; use `define-abbrev' instead."
(interactive "p") (interactive "p")
@ -330,25 +336,30 @@ Don't use this function in a Lisp program; use `define-abbrev' instead."
(save-excursion (forward-word (- arg)) (point)))))) (save-excursion (forward-word (- arg)) (point))))))
name) name)
(setq name (setq name
(read-string (format (if exp "%s abbrev for \"%s\": " (read-string (format (if exp "%s abbrev that expands into \"%s\": "
"Undefine %s abbrev: ") "Undefine %s abbrev: ")
type exp))) type exp)))
(set-text-properties 0 (length name) nil name) (set-text-properties 0 (length name) nil name)
(if (or (null exp) (if (or (null exp)
(not (abbrev-expansion name table)) (not (abbrev-expansion name table))
(y-or-n-p (format "%s expands to \"%s\"; redefine? " (y-or-n-p (format "%s expands into \"%s\"; redefine? "
name (abbrev-expansion name table)))) name (abbrev-expansion name table))))
(define-abbrev table (downcase name) exp)))) (define-abbrev table (downcase name) exp))))
(defun inverse-add-mode-abbrev (n) (defun inverse-add-mode-abbrev (n)
"Define last word before point as a mode-specific abbrev. "Define the word before point as a mode-specific abbreviation.
With prefix argument N, defines the Nth word before point. With prefix argument N, define the Nth word before point as the
This command uses the minibuffer to read the expansion. abbreviation.
Expands the abbreviation after defining it.
If `only-global-abbrevs' is non-nil, this command defines a
global (mode-independent) abbrev instead of a mode-specific one.
This command reads the expansion from the minibuffer, defines the
abbrev, and then expands the abbreviation in the current buffer.
See also `add-mode-abbrev', which performs the opposite task: See also `add-mode-abbrev', which performs the opposite task:
if the expansion is already in the buffer, use this command if the expansion is already in the buffer, use that command
to define an abbrev by specifying the abbrev in the minibuffer." to define an abbrev by specifying the abbreviation in the minibuffer."
(interactive "p") (interactive "p")
(inverse-add-abbrev (inverse-add-abbrev
(if only-global-abbrevs (if only-global-abbrevs
@ -358,14 +369,16 @@ to define an abbrev by specifying the abbrev in the minibuffer."
"Mode" n)) "Mode" n))
(defun inverse-add-global-abbrev (n) (defun inverse-add-global-abbrev (n)
"Define last word before point as a global (mode-independent) abbrev. "Define the word before point as a global (mode-independent) abbreviation.
With prefix argument N, defines the Nth word before point. With prefix argument N, define the Nth word before point as the
This command uses the minibuffer to read the expansion. abbreviation.
Expands the abbreviation after defining it.
This command reads the expansion from the minibuffer, defines the
abbrev, and then expands the abbreviation in the current buffer.
See also `add-global-abbrev', which performs the opposite task: See also `add-global-abbrev', which performs the opposite task:
if the expansion is already in the buffer, use this command if the expansion is already in the buffer, use that command
to define an abbrev by specifying the abbrev in the minibuffer." to define an abbrev by specifying the abbreviation in the minibuffer."
(interactive "p") (interactive "p")
(inverse-add-abbrev global-abbrev-table "Global" n)) (inverse-add-abbrev global-abbrev-table "Global" n))
@ -379,10 +392,10 @@ to define an abbrev by specifying the abbrev in the minibuffer."
(setq start (point) (setq start (point)
name (buffer-substring-no-properties start end))) name (buffer-substring-no-properties start end)))
(setq exp (read-string (format "%s expansion for \"%s\": " type name) (setq exp (read-string (format "Expansion for %s abbrev \"%s\": " type name)
nil nil nil t)) nil nil nil t))
(when (or (not (abbrev-expansion name table)) (when (or (not (abbrev-expansion name table))
(y-or-n-p (format "%s expands to \"%s\"; redefine? " (y-or-n-p (format "%s expands into \"%s\"; redefine? "
name (abbrev-expansion name table)))) name (abbrev-expansion name table))))
(define-abbrev table (downcase name) exp) (define-abbrev table (downcase name) exp)
(save-excursion (save-excursion
@ -390,10 +403,11 @@ to define an abbrev by specifying the abbrev in the minibuffer."
(expand-abbrev))))) (expand-abbrev)))))
(defun abbrev-prefix-mark (&optional arg) (defun abbrev-prefix-mark (&optional arg)
"Mark current point as the beginning of an abbrev. "Mark point as the beginning of an abbreviation.
The abbrev to be expanded starts here rather than at beginning of The abbrev to be expanded starts at point rather than at the
word. This way, you can expand an abbrev with a prefix: insert beginning of a word. This way, you can expand an abbrev with
the prefix, use this command, then insert the abbrev. a prefix: insert the prefix, use this command, then insert the
abbrev.
This command inserts a hyphen after the prefix, and if the abbrev This command inserts a hyphen after the prefix, and if the abbrev
is subsequently expanded, this hyphen will be removed. is subsequently expanded, this hyphen will be removed.
@ -408,8 +422,8 @@ argument."
(insert "-")) (insert "-"))
(defun expand-region-abbrevs (start end &optional noquery) (defun expand-region-abbrevs (start end &optional noquery)
"For abbrev occurrence in the region, offer to expand it. "For each abbrev occurrence in the region, offer to expand it.
The user is asked to type `y' or `n' for each occurrence. Ask the user to type `y' or `n' for each occurrence.
A prefix argument means don't query; expand all abbrevs." A prefix argument means don't query; expand all abbrevs."
(interactive "r\nP") (interactive "r\nP")
(save-excursion (save-excursion
@ -430,18 +444,19 @@ A prefix argument means don't query; expand all abbrevs."
;;; Abbrev properties. ;;; Abbrev properties.
(defun abbrev-table-get (table prop) (defun abbrev-table-get (table prop)
"Get the PROP property of abbrev table TABLE." "Get the property PROP of abbrev table TABLE."
(let ((sym (obarray-get table ""))) (let ((sym (obarray-get table "")))
(if sym (get sym prop)))) (if sym (get sym prop))))
(defun abbrev-table-put (table prop val) (defun abbrev-table-put (table prop val)
"Set the PROP property of abbrev table TABLE to VAL." "Set the property PROP of abbrev table TABLE to VAL."
(let ((sym (obarray-put table ""))) (let ((sym (obarray-put table "")))
(set sym nil) ; Make sure it won't be confused for an abbrev. (set sym nil) ; Make sure it won't be confused for an abbrev.
(put sym prop val))) (put sym prop val)))
(defalias 'abbrev-get 'get (defalias 'abbrev-get 'get
"Get the property PROP of abbrev ABBREV "Get the property PROP of abbrev ABBREV
See `define-abbrev' for the effect of some special properties.
\(fn ABBREV PROP)") \(fn ABBREV PROP)")
@ -500,7 +515,8 @@ for any particular abbrev defined in both.")
(defvar abbrev-minor-mode-table-alist nil (defvar abbrev-minor-mode-table-alist nil
"Alist of abbrev tables to use for minor modes. "Alist of abbrev tables to use for minor modes.
Each element looks like (VARIABLE . ABBREV-TABLE); Each element looks like (VARIABLE . ABBREV-TABLE);
ABBREV-TABLE is active whenever VARIABLE's value is non-nil. ABBREV-TABLE is active whenever VARIABLE's value is non-nil;
VARIABLE is supposed to be a minor-mode variable.
ABBREV-TABLE can also be a list of abbrev tables.") ABBREV-TABLE can also be a list of abbrev tables.")
(defvar fundamental-mode-abbrev-table (defvar fundamental-mode-abbrev-table
@ -511,11 +527,11 @@ ABBREV-TABLE can also be a list of abbrev tables.")
"The abbrev table of mode-specific abbrevs for Fundamental Mode.") "The abbrev table of mode-specific abbrevs for Fundamental Mode.")
(defvar abbrevs-changed nil (defvar abbrevs-changed nil
"Set non-nil by defining or altering any word abbrevs. "Non-nil if any word abbrevs were defined or altered.
This causes `save-some-buffers' to offer to save the abbrevs.") This causes `save-some-buffers' to offer to save the abbrevs.")
(defcustom abbrev-all-caps nil (defcustom abbrev-all-caps nil
"Non-nil means expand multi-word abbrevs all caps if abbrev was so." "Non-nil means expand multi-word abbrevs in all caps if the abbrev was so."
:type 'boolean :type 'boolean
:group 'abbrev-mode) :group 'abbrev-mode)
@ -532,17 +548,17 @@ Trying to expand an abbrev in any other buffer clears `abbrev-start-location'.")
"The abbrev-symbol of the last abbrev expanded. See `abbrev-symbol'.") "The abbrev-symbol of the last abbrev expanded. See `abbrev-symbol'.")
(defvar last-abbrev-text nil (defvar last-abbrev-text nil
"The exact text of the last abbrev expanded. "The exact text of the last abbrev that was expanded.
It is nil if the abbrev has already been unexpanded.") It is nil if the abbrev has already been unexpanded.")
(defvar last-abbrev-location 0 (defvar last-abbrev-location 0
"The location of the start of the last abbrev expanded.") "The location of the start of the last abbrev that was expanded.")
;; (defvar-local local-abbrev-table fundamental-mode-abbrev-table ;; (defvar-local local-abbrev-table fundamental-mode-abbrev-table
;; "Local (mode-specific) abbrev table of current buffer.") ;; "Local (mode-specific) abbrev table of current buffer.")
(defun clear-abbrev-table (table) (defun clear-abbrev-table (table)
"Undefine all abbrevs in abbrev table TABLE, leaving it empty." "Undefine all abbrevs in abbrev table TABLE, leaving TABLE empty."
(setq abbrevs-changed t) (setq abbrevs-changed t)
(let* ((sym (obarray-get table ""))) (let* ((sym (obarray-get table "")))
(dotimes (i (length table)) (dotimes (i (length table))
@ -557,9 +573,9 @@ It is nil if the abbrev has already been unexpanded.")
;; For backward compatibility, always return nil. ;; For backward compatibility, always return nil.
nil) nil)
(defun define-abbrev (table name expansion &optional hook &rest props) (defun define-abbrev (table abbrev expansion &optional hook &rest props)
"Define an abbrev in TABLE named NAME, to expand to EXPANSION and call HOOK. "Define ABBREV in TABLE, to expand into EXPANSION and optionally call HOOK.
NAME must be a string, and should be lower-case. ABBREV must be a string, and should be lower-case.
EXPANSION should usually be a string. EXPANSION should usually be a string.
To undefine an abbrev, define it with EXPANSION = nil. To undefine an abbrev, define it with EXPANSION = nil.
If HOOK is non-nil, it should be a function of no arguments; If HOOK is non-nil, it should be a function of no arguments;
@ -583,12 +599,12 @@ PROPS is a property list. The following properties are special:
overwrite a non-system abbreviation of the same name. overwrite a non-system abbreviation of the same name.
- `:case-fixed': non-nil means that abbreviations are looked up without - `:case-fixed': non-nil means that abbreviations are looked up without
case-folding, and the expansion is not capitalized/upcased. case-folding, and the expansion is not capitalized/upcased.
- `:enable-function': a function of no argument which returns non-nil if the - `:enable-function': a function of no arguments which returns non-nil
abbrev should be used for a particular call of `expand-abbrev'. if the abbrev should be used for a particular call of `expand-abbrev'.
An obsolete but still supported calling form is: An obsolete but still supported calling form is:
\(define-abbrev TABLE NAME EXPANSION &optional HOOK COUNT SYSTEM)." \(define-abbrev TABLE ABBREV EXPANSION &optional HOOK COUNT SYSTEM)."
(when (and (consp props) (or (null (car props)) (numberp (car props)))) (when (and (consp props) (or (null (car props)) (numberp (car props))))
;; Old-style calling convention. ;; Old-style calling convention.
(setq props `(:count ,(car props) (setq props `(:count ,(car props)
@ -598,7 +614,7 @@ An obsolete but still supported calling form is:
(setq props (plist-put props :abbrev-table-modiff (setq props (plist-put props :abbrev-table-modiff
(abbrev-table-get table :abbrev-table-modiff))) (abbrev-table-get table :abbrev-table-modiff)))
(let ((system-flag (plist-get props :system)) (let ((system-flag (plist-get props :system))
(sym (obarray-put table name))) (sym (obarray-put table abbrev)))
;; Don't override a prior user-defined abbrev with a system abbrev, ;; Don't override a prior user-defined abbrev with a system abbrev,
;; unless system-flag is `force'. ;; unless system-flag is `force'.
(unless (and (not (memq system-flag '(nil force))) (unless (and (not (memq system-flag '(nil force)))
@ -618,7 +634,7 @@ An obsolete but still supported calling form is:
(if (eq 'force system-flag) (plist-put props :system t) props)) (if (eq 'force system-flag) (plist-put props :system t) props))
(abbrev-table-put table :abbrev-table-modiff (abbrev-table-put table :abbrev-table-modiff
(1+ (abbrev-table-get table :abbrev-table-modiff)))) (1+ (abbrev-table-get table :abbrev-table-modiff))))
name)) abbrev))
(defun abbrev--check-chars (abbrev global) (defun abbrev--check-chars (abbrev global)
"Check if the characters in ABBREV have word syntax in either the "Check if the characters in ABBREV have word syntax in either the
@ -638,7 +654,7 @@ current (if global is nil) or standard syntax table."
(if global "in the standard syntax" "in this mode")))))) (if global "in the standard syntax" "in this mode"))))))
(defun define-global-abbrev (abbrev expansion) (defun define-global-abbrev (abbrev expansion)
"Define ABBREV as a global abbreviation for EXPANSION. "Define ABBREV as a global abbreviation that expands into EXPANSION.
The characters in ABBREV must all be word constituents in the standard The characters in ABBREV must all be word constituents in the standard
syntax table." syntax table."
(interactive "sDefine global abbrev: \nsExpansion for %s: ") (interactive "sDefine global abbrev: \nsExpansion for %s: ")
@ -646,7 +662,7 @@ syntax table."
(define-abbrev global-abbrev-table (downcase abbrev) expansion)) (define-abbrev global-abbrev-table (downcase abbrev) expansion))
(defun define-mode-abbrev (abbrev expansion) (defun define-mode-abbrev (abbrev expansion)
"Define ABBREV as a mode-specific abbreviation for EXPANSION. "Define ABBREV as a mode-specific abbreviation that expands into EXPANSION.
The characters in ABBREV must all be word-constituents in the current mode." The characters in ABBREV must all be word-constituents in the current mode."
(interactive "sDefine mode abbrev: \nsExpansion for %s: ") (interactive "sDefine mode abbrev: \nsExpansion for %s: ")
(unless local-abbrev-table (unless local-abbrev-table
@ -655,8 +671,8 @@ The characters in ABBREV must all be word-constituents in the current mode."
(define-abbrev local-abbrev-table (downcase abbrev) expansion)) (define-abbrev local-abbrev-table (downcase abbrev) expansion))
(defun abbrev--active-tables (&optional tables) (defun abbrev--active-tables (&optional tables)
"Return the list of abbrev tables currently active. "Return the list of abbrev tables that are currently active.
TABLES if non-nil overrides the usual rules. It can hold TABLES, if non-nil, overrides the usual rules. It can hold
either a single abbrev table or a list of abbrev tables." either a single abbrev table or a list of abbrev tables."
;; We could just remove the `tables' arg and let callers use ;; We could just remove the `tables' arg and let callers use
;; (or table (abbrev--active-tables)) but then they'd have to be careful ;; (or table (abbrev--active-tables)) but then they'd have to be careful
@ -682,7 +698,7 @@ either a single abbrev table or a list of abbrev tables."
"Return the symbol representing abbrev named ABBREV in TABLE. "Return the symbol representing abbrev named ABBREV in TABLE.
This symbol's name is ABBREV, but it is not the canonical symbol of that name; This symbol's name is ABBREV, but it is not the canonical symbol of that name;
it is interned in the abbrev-table TABLE rather than the normal obarray. it is interned in the abbrev-table TABLE rather than the normal obarray.
The value is nil if that abbrev is not defined." The value is nil if such an abbrev is not defined."
(let* ((case-fold (not (abbrev-table-get table :case-fixed))) (let* ((case-fold (not (abbrev-table-get table :case-fixed)))
;; In case the table doesn't set :case-fixed but some of the ;; In case the table doesn't set :case-fixed but some of the
;; abbrevs do, we have to be careful. ;; abbrevs do, we have to be careful.
@ -699,11 +715,11 @@ The value is nil if that abbrev is not defined."
sym))) sym)))
(defun abbrev-symbol (abbrev &optional table) (defun abbrev-symbol (abbrev &optional table)
"Return the symbol representing abbrev named ABBREV. "Return the symbol representing the abbrev named ABBREV in TABLE.
This symbol's name is ABBREV, but it is not the canonical symbol of that name; This symbol's name is ABBREV, but it is not the canonical symbol of that name;
it is interned in an abbrev-table rather than the normal obarray. it is interned in an abbrev-table rather than the normal obarray.
The value is nil if that abbrev is not defined. The value is nil if such an abbrev is not defined.
Optional second arg TABLE is abbrev table to look it up in. Optional second arg TABLE is the abbrev table to look it up in.
The default is to try buffer's mode-specific abbrev table, then global table." The default is to try buffer's mode-specific abbrev table, then global table."
(let ((tables (abbrev--active-tables table)) (let ((tables (abbrev--active-tables table))
sym) sym)
@ -716,7 +732,7 @@ The default is to try buffer's mode-specific abbrev table, then global table."
(defun abbrev-expansion (abbrev &optional table) (defun abbrev-expansion (abbrev &optional table)
"Return the string that ABBREV expands into in the current buffer. "Return the string that ABBREV expands into in the current buffer.
Optionally specify an abbrev table as second arg; Optionally specify an abbrev TABLE as second arg;
then ABBREV is looked up in that table only." then ABBREV is looked up in that table only."
(symbol-value (abbrev-symbol abbrev table))) (symbol-value (abbrev-symbol abbrev table)))
@ -780,9 +796,9 @@ then ABBREV is looked up in that table only."
(defun abbrev-insert (abbrev &optional name wordstart wordend) (defun abbrev-insert (abbrev &optional name wordstart wordend)
"Insert abbrev ABBREV at point. "Insert abbrev ABBREV at point.
If non-nil, NAME is the name by which this abbrev was found. If non-nil, NAME is the name by which this abbrev was found.
If non-nil, WORDSTART is the place where to insert the abbrev. If non-nil, WORDSTART is the buffer position where to insert the abbrev.
If WORDEND is non-nil, the abbrev replaces the previous text between If WORDEND is non-nil, it is a buffer position; the abbrev replaces the
WORDSTART and WORDEND. previous text between WORDSTART and WORDEND.
Return ABBREV if the expansion should be considered as having taken place. Return ABBREV if the expansion should be considered as having taken place.
The return value can be influenced by a `no-self-insert' property; The return value can be influenced by a `no-self-insert' property;
see `define-abbrev' for details." see `define-abbrev' for details."
@ -847,7 +863,8 @@ see `define-abbrev' for details."
(defvar abbrev-expand-function #'abbrev--default-expand (defvar abbrev-expand-function #'abbrev--default-expand
"Function that `expand-abbrev' uses to perform abbrev expansion. "Function that `expand-abbrev' uses to perform abbrev expansion.
Takes no argument and should return the abbrev symbol if expansion took place.") Takes no arguments, and should return the abbrev symbol if expansion
took place.")
(defcustom abbrev-suggest nil (defcustom abbrev-suggest nil
"Non-nil means suggest using abbrevs to save typing. "Non-nil means suggest using abbrevs to save typing.
@ -906,15 +923,17 @@ Expansion is a string of one or more words."
(defun abbrev--suggest-above-threshold (expansion) (defun abbrev--suggest-above-threshold (expansion)
"Return non-nil if the abbrev in EXPANSION provides significant savings. "Return non-nil if the abbrev in EXPANSION provides significant savings.
A significant saving, here, is the difference in length between A significant saving, here, means the difference in length between
the abbrev and the abbrev expansion. EXPANSION is a cons cell the abbrev and its expansion is not below the threshold specified
where the car is the expansion and the cdr is the abbrev." by the value of `abbrev-suggest-hint-threshold'.
EXPANSION is a cons cell where the car is the expansion and the cdr is
the abbrev."
(>= (- (length (car expansion)) (>= (- (length (car expansion))
(length (cdr expansion))) (length (cdr expansion)))
abbrev-suggest-hint-threshold)) abbrev-suggest-hint-threshold))
(defvar abbrev--suggest-saved-recommendations nil (defvar abbrev--suggest-saved-recommendations nil
"Keeps a list of expansions that have abbrevs defined. "Keeps the list of expansions that have abbrevs defined.
The user can show this list by calling The user can show this list by calling
`abbrev-suggest-show-report'.") `abbrev-suggest-show-report'.")
@ -930,7 +949,7 @@ EXPANSION is a cons cell where the `car' is the expansion and the
(push expansion abbrev--suggest-saved-recommendations)) (push expansion abbrev--suggest-saved-recommendations))
(defun abbrev--suggest-shortest-abbrev (new current) (defun abbrev--suggest-shortest-abbrev (new current)
"Return the shortest abbrev of NEW and CURRENT. "Return the shortest of the two abbrevs given by NEW and CURRENT.
NEW and CURRENT are cons cells where the `car' is the expansion NEW and CURRENT are cons cells where the `car' is the expansion
and the `cdr' is the abbrev." and the `cdr' is the abbrev."
(if (not current) (if (not current)
@ -959,8 +978,8 @@ informed about the existing abbrev."
(defun abbrev--suggest-get-totals () (defun abbrev--suggest-get-totals ()
"Return a list of all expansions and how many times they were used. "Return a list of all expansions and how many times they were used.
Each expansion is a cons cell where the `car' is the expansion Each expansion in the returned list is a cons cell where the `car' is the
and the `cdr' is the number of times the expansion has been expansion text and the `cdr' is the number of times the expansion has been
typed." typed."
(let (total cell) (let (total cell)
(dolist (expansion abbrev--suggest-saved-recommendations) (dolist (expansion abbrev--suggest-saved-recommendations)
@ -1088,11 +1107,11 @@ Presumes that `standard-output' points to `current-buffer'."
(defun insert-abbrev-table-description (name &optional readable) (defun insert-abbrev-table-description (name &optional readable)
"Insert before point a full description of abbrev table named NAME. "Insert before point a full description of abbrev table named NAME.
NAME is a symbol whose value is an abbrev table. NAME is a symbol whose value is an abbrev table.
If optional 2nd arg READABLE is non-nil, a human-readable description If optional 2nd arg READABLE is non-nil, insert a human-readable
is inserted. description.
If READABLE is nil, an expression is inserted. The expression is If READABLE is nil, insert an expression. The expression is
a call to `define-abbrev-table' that when evaluated will define a call to `define-abbrev-table' that, when evaluated, will define
the abbrev table NAME exactly as it is currently defined. the abbrev table NAME exactly as it is currently defined.
Abbrevs marked as \"system abbrevs\" are ignored." Abbrevs marked as \"system abbrevs\" are ignored."
(let ((symbols (abbrev--table-symbols name readable))) (let ((symbols (abbrev--table-symbols name readable)))
@ -1139,10 +1158,10 @@ Properties with special meaning:
case-folding, and the expansion is not capitalized/upcased. case-folding, and the expansion is not capitalized/upcased.
- `:regexp' is a regular expression that specifies how to extract the - `:regexp' is a regular expression that specifies how to extract the
name of the abbrev before point. The submatch 1 is treated name of the abbrev before point. The submatch 1 is treated
as the potential name of an abbrev. If :regexp is nil, the default as the potential name of an abbrev. If `:regexp' is nil, the default
behavior uses `backward-word' and `forward-word' to extract the name behavior uses `backward-word' and `forward-word' to extract the name
of the abbrev, which can therefore only be a single word. of the abbrev, which can therefore by default only be a single word.
- `:enable-function' can be set to a function of no argument which returns - `:enable-function' can be set to a function of no arguments which returns
non-nil if and only if the abbrevs in this table should be used for this non-nil if and only if the abbrevs in this table should be used for this
instance of `expand-abbrev'." instance of `expand-abbrev'."
(declare (doc-string 3)) (declare (doc-string 3))
@ -1171,7 +1190,7 @@ Properties with special meaning:
(defun abbrev-table-menu (table &optional prompt sortfun) (defun abbrev-table-menu (table &optional prompt sortfun)
"Return a menu that shows all abbrevs in TABLE. "Return a menu that shows all abbrevs in TABLE.
Selecting an entry runs `abbrev-insert'. Selecting an entry runs `abbrev-insert' for that entry's abbrev.
PROMPT is the prompt to use for the keymap. PROMPT is the prompt to use for the keymap.
SORTFUN is passed to `sort' to change the default ordering." SORTFUN is passed to `sort' to change the default ordering."
(unless sortfun (setq sortfun 'string-lessp)) (unless sortfun (setq sortfun 'string-lessp))