all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Mathias Dahl <mathias.dahl@gmail.com>
To: emacs-devel@gnu.org
Subject: Abbrev suggestions - feedback appreciated
Date: Sat, 16 Sep 2017 09:51:53 +0200	[thread overview]
Message-ID: <CABrcCQ6ECDA7ssc0VjzeQyMqoq-qBDsvWs5=Brd3pFxkJYaZhg@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 4354 bytes --]

Hi emacs-devel!

I want to have feedback on a small addition I have made to Emacs abbrev
functionality. First some background...

Some days back I was typing away in Emacs and after typing some hard to
spell word I realized that I probably had an abbrev defined for that
word. I checked by abbrev definitions and, yes, it was there. It turns
out I had many defined abbrevs that I had forgotten about, so I was
typing more than I needed. This got me thinking that it would be nice if
Emacs could tell me when I have typed a word that exists as an abbrev
expansion. After some research in abbrev.el I finally got something to
work, and you can find it below.

I would like to have people's comments on the idea, and know if this
might allredy exist (I tried to find something similar but couldn't), and
also if there are any performance problems or other in my code. The
internals of abbrevs are dark and mysterious to me, so I might miss some
crucial things. But, the code seems to do what I want it to.

Also, if people like this idea, perhaps it could be added to Emacs
proper. Surely it can live on its life as a separate library on Melpa,
Elpa or other place, but the code is quite short and this feels like
something that could be a simple option/toggle in Emacs standard abbrev
functionality. I have papers in place if this is something people would
like.

Without further ado, here is the current code I have:

;;; absug.el --- Suggest an abbrev based on the word before point

;; Copyright stuff to be added later...

;; Author: Mathias Dahl (mathias.dahl@gmail.com)

;;; Commentary:

;; This library helps the user remember defined abbrevs by suggesting
;; them after having typed an abbrev expansion manually.

;; For example, if the user has defined the abbrev `doc' with the
;; expansion `document', if the user manually types `document' with
;; `abbrev-mode' active, a message will be presented suggesting to use
;; the defined abbrev `doc' instead.

;; To install, load or require this file/library and also add the
;; following code to your .emacs or init.el file:

;; (absug-enable)

;; (It is also a command you can call interactively)

;; You can interactively turn off abbrev suggestion by calling the
;; command `absug-disable'.

;;; Code:

(defun absug-word-before-point ()
  "Return the word before point."
  (let ((lim (point))
        start end)
    (backward-word 1)
    (setq start (point))
    (forward-word 1)
    (setq end (min (point) lim))
    (buffer-substring-no-properties start end)))

(defun absug-get-active-tables-including-parents ()
  "Return a list of all active abbrev tables, including parent tables."
  (let* ((tables (abbrev--active-tables))
         (all tables))
    (dolist (table tables)
      (setq all (append (abbrev-table-get table :parents) all)))
    all))

(defun absug-get-active-abbrev-expansions ()
  "Return a list of all the active abbrev expansions.
Includes expansions from parent abbrev tables."
  (let (expansions)
    (dolist (table (absug-get-active-tables-including-parents))
      (mapatoms (lambda (e)
                  (let ((value (symbol-value (abbrev--symbol e table))))
                    (if value
                        (setq expansions
                              (cons (cons value (symbol-name e))
                                    expansions)))))
                table))
    expansions))

(defun absug-maybe-suggest ()
  "Suggest an abbrev to the user based on the word before point."
  (let* ((word (absug-word-before-point))
         (expansions (absug-get-active-abbrev-expansions))
         (abbrev (assoc word expansions)))
    (if abbrev
        (message "Abbrev suggestion: The word `%s' has the abbrev
`%s' defined" (car abbrev) (cdr abbrev)))))

(defun absug-default-expand ()
  "My version to use for `abbrev-expand-function'.
If no abbrev expansion is found by `abbrev--default-expand', see
if there is an abbrev defined for the word before point, and
suggest it to the user."
  (unless (abbrev--default-expand)
    (absug-maybe-suggest)))

(defun absug-disable ()
  "Disable abbrev suggestions"
  (interactive)
  (setq abbrev-expand-function #'abbrev--default-expand))

(defun absug-enable ()
  "Enable abbrev suggestions"
  (interactive)
  (setq abbrev-expand-function #'absug-default-expand))

(provide 'absug)

;;; absug.el ends here


Thanks!

/Mathias

[-- Attachment #2: Type: text/html, Size: 6082 bytes --]

             reply	other threads:[~2017-09-16  7:51 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-09-16  7:51 Mathias Dahl [this message]
2017-09-16  8:46 ` Abbrev suggestions - feedback appreciated Eli Zaretskii
2017-09-17 13:15   ` Mathias Dahl
2017-09-16 13:22 ` Stefan Monnier
2017-09-17 13:22   ` Mathias Dahl
2017-09-17 14:03     ` Mathias Dahl
2017-09-17 21:23     ` Stefan Monnier
2017-09-17 21:56       ` Mathias Dahl
2017-10-03 12:51         ` Kaushal Modi
2017-10-07 15:13           ` Mathias Dahl
2017-10-07 15:29             ` Stefan Monnier
2017-10-07 17:18               ` Mathias Dahl
2017-10-07 18:40                 ` Mathias Dahl
2017-10-07 22:29                   ` Ian Dunn
2017-10-07 22:44                     ` Stefan Monnier
2017-10-08 16:38                       ` Ian Dunn
2018-09-17 21:48                         ` Mathias Dahl
2018-09-18  2:05                           ` Stefan Monnier
2020-05-11 21:37                             ` Mathias Dahl
2020-05-11 22:39                               ` Mathias Dahl
2020-05-11 22:58                               ` Stefan Monnier
2020-05-16 22:10                                 ` Mathias Dahl
2020-05-16 22:22                                   ` Mathias Dahl
2020-05-17  3:13                                     ` Stefan Monnier
2020-05-17 14:59                                       ` Mathias Dahl
2020-05-17 15:45                                         ` Eli Zaretskii
2020-05-17 18:43                                           ` Mathias Dahl
2020-05-17 21:20                                             ` Stefan Monnier
2020-05-18 22:00                                               ` Mathias Dahl
2020-06-04 20:14                                                 ` Mathias Dahl
2017-10-07 22:40                 ` Stefan Monnier
2017-10-08 15:28                   ` Mathias Dahl
  -- strict thread matches above, loose matches on Subject: below --
2017-10-08  8:15 Seweryn Kokot
2017-10-08 15:25 ` Mathias Dahl

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CABrcCQ6ECDA7ssc0VjzeQyMqoq-qBDsvWs5=Brd3pFxkJYaZhg@mail.gmail.com' \
    --to=mathias.dahl@gmail.com \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.