]> git.eshelyaron.com Git - emacs.git/commitdiff
Make add-mode-abbrev use the active region
authorLars Ingebrigtsen <larsi@gnus.org>
Mon, 11 Jul 2022 13:34:07 +0000 (15:34 +0200)
committerLars Ingebrigtsen <larsi@gnus.org>
Mon, 11 Jul 2022 13:34:15 +0000 (15:34 +0200)
* lisp/abbrev.el (add-mode-abbrev, add-global-abbrev): Document it.
(add-abbrev): If there's an active region, use that as the
expansion (bug#56496).

lisp/abbrev.el

index 21aa3311d6fdcb1c1f4a0b346c833cb77667bda0..718938df0cb30da38f2b0977ffe978b9d9f38f4b 100644 (file)
@@ -292,8 +292,11 @@ The saved abbrevs are written to the file specified by
 \f
 (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.
@@ -303,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
@@ -313,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.
@@ -324,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\": "