summaryrefslogtreecommitdiff
path: root/lisp/abbrev.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/abbrev.el')
-rw-r--r--lisp/abbrev.el82
1 files changed, 59 insertions, 23 deletions
diff --git a/lisp/abbrev.el b/lisp/abbrev.el
index b7216f5d633..718938df0cb 100644
--- a/lisp/abbrev.el
+++ b/lisp/abbrev.el
@@ -68,13 +68,11 @@ be replaced by its expansion."
(define-obsolete-variable-alias 'edit-abbrevs-map
'edit-abbrevs-mode-map "24.4")
-(defvar edit-abbrevs-mode-map
- (let ((map (make-sparse-keymap)))
- (define-key map "\C-x\C-s" 'abbrev-edit-save-buffer)
- (define-key map "\C-x\C-w" 'abbrev-edit-save-to-file)
- (define-key map "\C-c\C-c" 'edit-abbrevs-redefine)
- map)
- "Keymap used in `edit-abbrevs'.")
+(defvar-keymap edit-abbrevs-mode-map
+ :doc "Keymap used in `edit-abbrevs'."
+ "C-x C-s" #'abbrev-edit-save-buffer
+ "C-x C-w" #'abbrev-edit-save-to-file
+ "C-c C-c" #'edit-abbrevs-redefine)
(defun kill-all-abbrevs ()
"Undefine all defined abbrevs."
@@ -176,7 +174,7 @@ that may be omitted (it is usually omitted)."
(defun edit-abbrevs-redefine ()
"Redefine abbrevs according to current buffer contents."
- (interactive)
+ (interactive nil edit-abbrevs-mode)
(save-restriction
(widen)
(define-abbrevs t)
@@ -279,7 +277,8 @@ abbrevs have been saved."
(list (read-file-name "Save abbrevs to file: "
(file-name-directory
(expand-file-name abbrev-file-name))
- abbrev-file-name)))
+ abbrev-file-name))
+ edit-abbrevs-mode)
(edit-abbrevs-redefine)
(write-abbrev-file file t))
@@ -287,14 +286,17 @@ abbrevs have been saved."
"Save all the user-level abbrev definitions in current buffer.
The saved abbrevs are written to the file specified by
`abbrev-file-name'."
- (interactive)
+ (interactive nil edit-abbrevs-mode)
(abbrev-edit-save-to-file abbrev-file-name))
(defun add-mode-abbrev (arg)
"Define a mode-specific abbrev whose expansion is the last word before point.
+If there's an active region, use that as the expansion.
+
Prefix argument ARG says how many words before point to use for the expansion;
zero means the entire region is the expansion.
+
A negative ARG means to undefine the specified abbrev.
This command reads the abbreviation from the minibuffer.
@@ -304,7 +306,7 @@ if the abbreviation is already in the buffer, use that command to define
a mode-specific abbrev by specifying its expansion in the minibuffer.
Don't use this function in a Lisp program; use `define-abbrev' instead."
- (interactive "p")
+ (interactive "P")
(add-abbrev
(if only-global-abbrevs
global-abbrev-table
@@ -314,8 +316,11 @@ Don't use this function in a Lisp program; use `define-abbrev' instead."
(defun add-global-abbrev (arg)
"Define a global (all modes) abbrev whose expansion is last word before point.
+If there's an active region, use that as the expansion.
+
Prefix argument ARG says how many words before point to use for the expansion;
zero means the entire region is the expansion.
+
A negative ARG means to undefine the specified abbrev.
This command reads the abbreviation from the minibuffer.
@@ -325,15 +330,21 @@ if the abbreviation is already in the buffer, use that command to define
a global abbrev by specifying its expansion in the minibuffer.
Don't use this function in a Lisp program; use `define-abbrev' instead."
- (interactive "p")
+ (interactive "P")
(add-abbrev global-abbrev-table "Global" arg))
(defun add-abbrev (table type arg)
- (let ((exp (and (>= arg 0)
- (buffer-substring-no-properties
- (point)
- (if (= arg 0) (mark)
- (save-excursion (forward-word (- arg)) (point))))))
+ (let ((exp
+ (cond
+ ((or (and (null arg) (use-region-p))
+ (zerop (prefix-numeric-value arg)))
+ (buffer-substring-no-properties (region-beginning) (region-end)))
+ ((> (prefix-numeric-value arg) 0)
+ (buffer-substring-no-properties
+ (point)
+ (save-excursion
+ (forward-word (- (prefix-numeric-value arg)))
+ (point))))))
name)
(setq name
(read-string (format (if exp "%s abbrev that expands into \"%s\": "
@@ -491,7 +502,8 @@ PROPS is a list of properties."
(defun abbrev-table-p (object)
"Return non-nil if OBJECT is an abbrev table."
(and (obarrayp object)
- (numberp (abbrev-table-get object :abbrev-table-modiff))))
+ (numberp (ignore-error 'wrong-type-argument
+ (abbrev-table-get object :abbrev-table-modiff)))))
(defun abbrev-table-empty-p (object &optional ignore-system)
"Return nil if there are no abbrev symbols in OBJECT.
@@ -604,7 +616,8 @@ PROPS is a property list. The following properties are special:
An obsolete but still supported calling form is:
-\(define-abbrev TABLE ABBREV EXPANSION &optional HOOK COUNT SYSTEM)."
+\(define-abbrev TABLE NAME EXPANSION &optional HOOK COUNT SYSTEM)."
+ (declare (indent defun))
(when (and (consp props) (or (null (car props)) (numberp (car props))))
;; Old-style calling convention.
(setq props `(:count ,(car props)
@@ -884,8 +897,8 @@ longer than the abbrev, the benefit of informing the user is not
significant. If you always want to be informed about existing
abbrevs for the text you type, set this value to zero or less.
This setting only applies if `abbrev-suggest' is non-nil."
- :type 'number
- :version "28.1")
+ :type 'natnum
+ :version "28.1")
(defun abbrev--suggest-get-active-tables-including-parents ()
"Return a list of all active abbrev tables, including parent tables."
@@ -1164,7 +1177,7 @@ Properties with special meaning:
- `: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
instance of `expand-abbrev'."
- (declare (doc-string 3))
+ (declare (doc-string 3) (indent defun))
;; We used to manually add the docstring, but we also want to record this
;; location as the definition of the variable (in load-history), so we may
;; as well just use `defvar'.
@@ -1212,7 +1225,30 @@ SORTFUN is passed to `sort' to change the default ordering."
(define-derived-mode edit-abbrevs-mode fundamental-mode "Edit-Abbrevs"
"Major mode for editing the list of abbrev definitions.
This mode is for editing abbrevs in a buffer prepared by `edit-abbrevs',
-which see.")
+which see."
+ :interactive nil)
+
+(defun abbrev--possibly-save (query &optional arg)
+ ;; Query mode.
+ (if (eq query 'query)
+ (and save-abbrevs abbrevs-changed)
+ ;; Maybe save abbrevs, and record whether we either saved them or
+ ;; asked to.
+ (and save-abbrevs
+ abbrevs-changed
+ (progn
+ (if (or arg
+ (eq save-abbrevs 'silently)
+ (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name)))
+ (progn
+ (write-abbrev-file nil)
+ nil)
+ ;; Don't keep bothering user if they say no.
+ (setq abbrevs-changed nil)
+ ;; Inhibit message in `save-some-buffers'.
+ t)))))
+
+(add-hook 'save-some-buffers-functions #'abbrev--possibly-save)
(provide 'abbrev)