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: Re: Abbrev suggestions - feedback appreciated Date: Sun, 17 Sep 2017 16:03:26 +0200 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="001a1144f67837c5c60559631826" X-Trace: blaine.gmane.org 1505657038 13107 195.159.176.226 (17 Sep 2017 14:03:58 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sun, 17 Sep 2017 14:03:58 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Sep 17 16:03:53 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 1dtaAr-00037H-2L for ged-emacs-devel@m.gmane.org; Sun, 17 Sep 2017 16:03:53 +0200 Original-Received: from localhost ([::1]:60758 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dtaAv-00081m-BY for ged-emacs-devel@m.gmane.org; Sun, 17 Sep 2017 10:03:57 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:59885) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dtaAn-00081a-0N for emacs-devel@gnu.org; Sun, 17 Sep 2017 10:03:50 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dtaAl-0003aj-Mp for emacs-devel@gnu.org; Sun, 17 Sep 2017 10:03:49 -0400 Original-Received: from mail-ua0-x233.google.com ([2607:f8b0:400c:c08::233]:51725) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dtaAl-0003aS-GT for emacs-devel@gnu.org; Sun, 17 Sep 2017 10:03:47 -0400 Original-Received: by mail-ua0-x233.google.com with SMTP id 72so3236789uas.8 for ; Sun, 17 Sep 2017 07:03:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to; bh=m4SWaEvUWbpzAyXWGh7C84ZG5xkDimskBBSiNUfj+o0=; b=sxasL4Nw4oh3bHrD6GPLmzSyDJgV3LJN3bUXcuGG8xTGUq25h+RZAhSozotT1u3fvI CsaH+WBGJgUrswx4hvpJSZU6S3AAZUlnxh7gF6UYcBvqs2sDTwcoe+vPq2Co7R/5WKTv eey2nfIPhzTkRTc0eFmj+WChJ7amyMlBrVsyo9yiGskJEXpQEQf3YImIUmyHoYSWgujK ZbfCJfrtvmtxipy+Nbi7bDwmbfqcdqfrZhmu6onl/HktX3jsHxUBi+GXf6p+3fXOWdKd 5AcqMwhDAshzz5kwRDDBZVKTdgALQs4usFz6qGdN15mzQoLbKfL23WKgL6P0rRC1kB6J CvKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to; bh=m4SWaEvUWbpzAyXWGh7C84ZG5xkDimskBBSiNUfj+o0=; b=PEzHXOgu11DRxs1AAI38tHHHl3OPZ3KbNrzPJWVBuSPfepoZKi4yTvGntv53m6CQJP xjgQrWVHdCdo1uHdclU/5Occ1L9CDRKa83Jc+BANk7KnU0WUwhQ6hFKBbqUI74xbx3F8 w3wpnqhAOjVQUgGMHELnt8VGOl4MlJ09Z+7lkNac10yajNp1+XVndTFjA7LfUnLKZfaR m7/QbmSPQNHjMBs8LvLZtBs3UamVWxzQflyAf2eh4bJxqhM9G/k1lECDYqZFlt15rxcE qgIhINVnJHvDB8/b4lsvjPXdzIoO2PftvV3TJH6YbMZFjbYCPODOC1Lcddy84wJpm0D/ hxuQ== X-Gm-Message-State: AHPjjUihu8D+1FTqMf08yF7i59zJIgHMPR0PyEJimI3i/zJBJLGxp1dQ L+BZPilwrSyasxx9sl00NfrVLhhMTDw1+DFnn3FjzcRX X-Google-Smtp-Source: AOwi7QCh025XSlGJ+2wxj+BA2CThElyrurw5h002FJQGKJ2tIiZKSArVWr+nh8o38EzyyG+dPrplASG/Qn4J2cmcFIo= X-Received: by 10.176.73.169 with SMTP id e38mr26395368uad.132.1505657026494; Sun, 17 Sep 2017 07:03:46 -0700 (PDT) Original-Received: by 10.176.83.89 with HTTP; Sun, 17 Sep 2017 07:03:26 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:400c:c08::233 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:218400 Archived-At: --001a1144f67837c5c60559631826 Content-Type: text/plain; charset="UTF-8" > > True. I thought about that and concluded that it would be a lot harder > to look for more than one words, matching a certain expansion. It will > be especially tricky, I think, if the words the user types and which > match an extension, spans more than one line. Of course not impossible but > I thought I would start with the easy and probably quite common use > case. > Okay, so... Turns out it was not that hard... :) With two helper functions I could rewrite the suggest function quite nicely: (defun asug-count-words (expansion) (length (split-string expansion " " t))) (defun asug-get-previous-words (n) (let ((end (point)) words) (save-excursion (backward-word n) (replace-regexp-in-string "[\n\r]" " " (buffer-substring-no-properties (point) end))))) (defun absug-maybe-suggest () "Suggest an abbrev to the user based on the word(s) before point." (let ((expansions (absug-get-active-abbrev-expansions)) words found expansion word-count) (while (and expansions (not found)) (setq expansion (pop expansions)) (when expansion (setq word-count (asug-count-words (car expansion)) words (asug-get-previous-words word-count)) (when (and (> word-count 0) (string= words (car expansion))) (setq found t) (message "You can write `%s' using the abbrev `%s'." (car expansion) (cdr expansion))))))) It works for abbrevs that has an expansion with multiple words and it also works across lines. Now I will clean this up a bit and checkdoc it... --001a1144f67837c5c60559631826 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
True. I thought about= that and concluded that it would be a lot harder
to look for mor= e than one words, matching a certain expansion. It will
be especi= ally tricky, I think, if the words the user types and which
match= an extension, spans more than one line. Of course not impossible but
=
I thought I would start with the easy and probably quite common use
case.

Okay, so... Turns out it was not that hard... :)

= With two helper functions I could rewrite the suggest function quite nicely= :

(defun asug-count-words (expansion)
=C2=A0 (length (split-string expansion " " t)))

=
(defun asug-get-previous-words (n)
=C2=A0 (let ((end (= point))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 words)
=C2=A0 =C2= =A0 (save-excursion
=C2=A0 =C2=A0 =C2=A0 (backward-word n)
<= div>=C2=A0 =C2=A0 =C2=A0 (replace-regexp-in-string
=C2=A0 =C2=A0 = =C2=A0 =C2=A0"[\n\r]" " "
=C2=A0 =C2=A0 =C2= =A0 =C2=A0(buffer-substring-no-properties (point) end)))))

(defun absug-maybe-suggest ()
=C2=A0 "Suggest an ab= brev to the user based on the word(s) before point."
=C2=A0 = (let ((expansions (absug-get-active-abbrev-expansions))
=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0words found expansion word-count)
=C2= =A0 =C2=A0 (while (and expansions
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 (not found))
=C2=A0 =C2=A0 =C2=A0 (setq = expansion (pop expansions))
=C2=A0 =C2=A0 =C2=A0 (when expansion<= /div>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq word-count (asug-count-words (c= ar expansion))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 w= ords (asug-get-previous-words word-count))
=C2=A0 =C2=A0 =C2=A0 = =C2=A0 (when (and (> word-count 0)
=C2=A0 =C2=A0 =C2=A0 =C2=A0= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(string=3D words (car expansion))= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq found t)
=C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (message "You can write `%s' using= the abbrev `%s'."
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(car expansion) (cdr expansion)))))))

It works for abbrevs that has an expansion with= multiple words and
it also works across lines.

Now I will clean this up a bit and checkdoc it...


--001a1144f67837c5c60559631826--