Subject: bug#44328: 27.1; [PATCH] Add expand-abbrev-maybe condition key binding

From: Zhu Zihao <[email-masked]>
Date: Fri, 30 Oct 2020 22:32:32 +0800

This patch add expand-abbrev-maybe. A conditional keybinding inspired
from YASnippet.

When bound to something like "TAB", it will expand abbrev if there's
some at point, it will do original binding(e.g. indent) if there's no
abbrev.

This is helpful for somebody doesn't like aggressive expand strategy in
abbrev-mode, but still doesn't want to give a exclusive keybinding for
expand-abbrev.

>From 3df90b6c045d8f9e5e91890b44a96a9685e872ae Mon Sep 17 00:00:00 2001
From: Zhu Zihao <[email-masked]>
Date: Fri, 30 Oct 2020 22:24:54 +0800
Subject: [PATCH] Add conditonal key binding expand-abbrev-maybe

A binding which will expand any existing abbrev at point and act like
original keybinding when there's none abbrev at point.

* lisp/abbrev.el (expand-abbrev-maybe): New variable.
* doc/emacs/abbrevs.texi (Abbrev Concepts): New vindex "expand-abbrev-maybe".
---
 doc/emacs/abbrevs.texi | 6 ++++++
 lisp/abbrev.el         | 7 +++++++
 2 files changed, 13 insertions(+)

diff --git a/doc/emacs/abbrevs.texi b/doc/emacs/abbrevs.texi
index e3766aae9e..2d7b4e1698 100644
--- a/doc/emacs/abbrevs.texi
+++ b/doc/emacs/abbrevs.texi
@@ -210,6 +210,12 @@ Expanding Abbrevs
 unexpand-abbrev} to cancel the last expansion without deleting the
 terminating character.
 
+@vindex expand-abbrev-maybe
+  If you don't want to check expand for every @code{self-insert-command}
+like Abbrev mode. There's a variable describing a conditional key definition
+You can bind it to some key like @kbd{TAB}. It' ll expand abbrev if
+there's some abbrev at point, and act like usual @kbd{TAB} if there's not.
+
 @findex expand-region-abbrevs
   @kbd{M-x expand-region-abbrevs} searches through the region for defined
 abbrevs, and for each one found offers to replace it with its expansion.
diff --git a/lisp/abbrev.el b/lisp/abbrev.el
index f35c637eed..7d4bb9621f 100644
--- a/lisp/abbrev.el
+++ b/lisp/abbrev.el
@@ -978,6 +978,13 @@ expand-abbrev
       (if abbrev-suggest
           (abbrev--suggest-maybe-suggest))))
 
+(defconst expand-abbrev-maybe
+  '(menu-item "" expand-abbrev
+    :filter (lambda (cmd) (and (abbrev--before-point) cmd)))
+  "A conditional key definition for `expand-abbrev'.
+When this was bound, it will expand abbrev at point if there're any possible
+abbrev.")
+
 (defun abbrev--default-expand ()
   "Default function to use for `abbrev-expand-function'.
 This also respects the obsolete wrapper hook `abbrev-expand-functions'.
-- 
2.29.1

-- 
Retrieve my PGP public key: https://meta.sr.ht/~citreu.pgp

Zihao