From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mathias Dahl Newsgroups: gmane.emacs.devel Subject: Abbrev suggestions - feedback appreciated Date: Sat, 16 Sep 2017 09:51:53 +0200 Message-ID: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="089e08204f6c9cebb2055949c9c7" X-Trace: blaine.gmane.org 1505548387 22500 195.159.176.226 (16 Sep 2017 07:53:07 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 16 Sep 2017 07:53:07 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Sep 16 09:53:03 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dt7uR-0005iu-8u for ged-emacs-devel@m.gmane.org; Sat, 16 Sep 2017 09:53:03 +0200 Original-Received: from localhost ([::1]:56317 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dt7uY-0004J2-EX for ged-emacs-devel@m.gmane.org; Sat, 16 Sep 2017 03:53:10 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59283) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dt7th-00048g-Dr for emacs-devel@gnu.org; Sat, 16 Sep 2017 03:52:21 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dt7tf-0008Qq-VI for emacs-devel@gnu.org; Sat, 16 Sep 2017 03:52:17 -0400 Original-Received: from mail-ua0-x232.google.com ([2607:f8b0:400c:c08::232]:45735) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dt7tf-0008QB-QT for emacs-devel@gnu.org; Sat, 16 Sep 2017 03:52:15 -0400 Original-Received: by mail-ua0-x232.google.com with SMTP id c27so2263701uah.2 for ; Sat, 16 Sep 2017 00:52:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=1YN0cPrsS5nF8rfDe9jvhX5jtfYHqaAwdVcJ/Co1gF0=; b=cgGCM6vDvgjgVqtZEi3IIGsA8l2cm61N4QUHmxYhzKmMsGLGIJZtYPayZ0GzidXipF fipy6qp4g0FVKAfwtv+MCTszqxEoBfCBYwZsSOIH97bYWei9awPQnEeQmRAx/z/Zp+Sw 5vm43ZaeTd1esdHW3MgS5iP8msic2YQQOXZPfHOFz6F8uo/4K74JtZnybu71UfWxs2ud V7nx6eGM9hADjjKtZQpq+XffL6eJGmqAHnMa879WnsTjDZ/zBWBwpYftMWaE5sOk5Ce+ dgpJG0Y9bEBzz4bMq66/06zAVb4VrviRyWGyN+lVKkzjhE7+JMX6c6I+S3XI4xlcLMvU bCFA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=1YN0cPrsS5nF8rfDe9jvhX5jtfYHqaAwdVcJ/Co1gF0=; b=H/n6R4PZ1H3RU7yCpHNSlXmJxznfZN/M+iTl2jtqOSj4otqhcxcJEZat11VzNacNNz MDDb70/iDkuhWS4dSIEvU+TLym+EtTiyjIOcq3J9DOT+OhzuVztjacOg3tZt3VTUobfJ xNXtFt03t+jQJxxwwRSHtmFC1Qufh11vbqJmZTZJG5QpvxpOHvBcSGs+pRbJkalTgfGd AWRNLy83Fz7zi/Uj1lBi/7j/NeCenL2oRVdsP776vD21lk5zx6lz/CRLDucGNMq1L/QB imYIBO0m1/lhSjt6z+c2Z6gpBgNYaxySn1z2GI2au50nAVl6ELi93v/yU4yzf+Asalhz H7fA== X-Gm-Message-State: AHPjjUiTlmnwZRm/V3zPDnOIGFZsCZnFV/+e0n40mXYaJ+FAdrtCd8Xy pYloZa/mNU5HQetDUpeBohBLknCyxtUrgneG7TTYjCfs X-Google-Smtp-Source: ADKCNb4H8zRLji+C3GiMxyTtLPOdxhBvcKd565nlG1mys4vj4H/Eu7jAXhMEoKHFIYL1AEozloOVzwub2GD2fQix6aY= X-Received: by 10.176.95.152 with SMTP id b24mr24015923uaj.30.1505548333540; Sat, 16 Sep 2017 00:52:13 -0700 (PDT) Original-Received: by 10.176.83.89 with HTTP; Sat, 16 Sep 2017 00:51:53 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400c:c08::232 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:218346 Archived-At: --089e08204f6c9cebb2055949c9c7 Content-Type: text/plain; charset="UTF-8" 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 --089e08204f6c9cebb2055949c9c7 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi emacs-devel!

I want to ha= ve feedback on a small addition I have made to Emacs abbrev
funct= ionality. First some background...

Some days back = I was typing away in Emacs and after typing some hard to
spell wo= rd 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 c= ouldn't), and
also if there are any performance problems or o= ther 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 l= ive on its life as a separate library on Melpa,
Elpa or other pla= ce, but the code is quite short and this feels like
something tha= t could be a simple option/toggle in Emacs standard abbrev
functi= onality. I have papers in place if this is something people would
like.

Without further ado, here is the current co= de 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 h= elps the user remember defined abbrevs by suggesting
;; them afte= r having typed an abbrev expansion manually.

;; Fo= r 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 suggest= ing to use
;; the defined abbrev `doc' instead.
;; To install, load or require this file/library and also add t= he
;; following code to your .emacs or init.el file:
;; (absug-enable)

;; (It is also a co= mmand you can call interactively)

;; You can inter= actively turn off abbrev suggestion by calling the
;; command `ab= sug-disable'.

;;; Code:

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

(defun absug-get-active-tables-including-parents ()
=C2= =A0 "Return a list of all active abbrev tables, including parent table= s."
=C2=A0 (let* ((tables (abbrev--active-tables))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(all tables))
=C2=A0 =C2=A0 (d= olist (table tables)
=C2=A0 =C2=A0 =C2=A0 (setq all (append (abbr= ev-table-get table :parents) all)))
=C2=A0 =C2=A0 all))

(defun absug-get-active-abbrev-expansions ()
=C2= =A0 "Return a list of all the active abbrev expansions.
Incl= udes expansions from parent abbrev tables."
=C2=A0 (let (exp= ansions)
=C2=A0 =C2=A0 (dolist (table (absug-get-active-tables-in= cluding-parents))
=C2=A0 =C2=A0 =C2=A0 (mapatoms (lambda (e)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let (= (value (symbol-value (abbrev--symbol e table))))
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (if value
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (setq expansions
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (cons= (cons value (symbol-name e))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 expansions)))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 table))
=C2=A0 =C2=A0 expansions))
=

(defun absug-maybe-suggest ()
=C2=A0 "Su= ggest an abbrev to the user based on the word before point."
=C2=A0 (let* ((word (absug-word-before-point))
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0(expansions (absug-get-active-abbrev-expansions))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(abbrev (assoc word expansions)))=
=C2=A0 =C2=A0 (if abbrev
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (messag= e "Abbrev suggestion: The word `%s' has the abbrev
`%s&#= 39; defined" (car abbrev) (cdr abbrev)))))

(d= efun absug-default-expand ()
=C2=A0 "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 fo= r the word before point, and
suggest it to the user."
<= div>=C2=A0 (unless (abbrev--default-expand)
=C2=A0 =C2=A0 (absug-= maybe-suggest)))

(defun absug-disable ()
=C2=A0 "Disable abbrev suggestions"
=C2=A0 (interactiv= e)
=C2=A0 (setq abbrev-expand-function #'abbrev--default-expa= nd))

(defun absug-enable ()
=C2=A0 "= ;Enable abbrev suggestions"
=C2=A0 (interactive)
= =C2=A0 (setq abbrev-expand-function #'absug-default-expand))
=
(provide 'absug)

;;; absug.el e= nds here


Thanks!

/Mathias

--089e08204f6c9cebb2055949c9c7--