From: Lars Ingebrigtsen Date: Mon, 11 Jul 2022 13:34:07 +0000 (+0200) Subject: Make add-mode-abbrev use the active region X-Git-Tag: emacs-29.0.90~1447^2~1006 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=ce31339c8369f6d16d5a7b1e8769670891c57f97;p=emacs.git Make add-mode-abbrev use the active region * 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). --- diff --git a/lisp/abbrev.el b/lisp/abbrev.el index 21aa3311d6f..718938df0cb 100644 --- a/lisp/abbrev.el +++ b/lisp/abbrev.el @@ -292,8 +292,11 @@ The saved abbrevs are written to the file specified by (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\": "