all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Hongyi Zhao <hongyi.zhao@gmail.com>
To: help-gnu-emacs <help-gnu-emacs@gnu.org>
Subject: Re: Try to understand the ora-company-number function again.
Date: Thu, 21 Oct 2021 18:48:50 +0800	[thread overview]
Message-ID: <CAGP6POLRtLA-Wpvzf7NZvNKXTnmP51CDihhowH57XNGKm4o5fQ@mail.gmail.com> (raw)
In-Reply-To: <CAGP6POLOK5YqjZyM13e3he-a-VnL68CrcEKCmFJAOgKdKt-9UA@mail.gmail.com>

On Thu, Oct 21, 2021 at 3:11 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Thu, Oct 21, 2021 at 2:54 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> >
> > I try to understand the  ora-company-number function [1] again:
> >
> > (defun ora-company-number ()
> > "Forward to `company-complete-number'.
> >
> > Unless the number is potentially part of the candidate.
> > In that case, insert the number."
> > (interactive)
> > (let* ((k (this-command-keys))
> > (re (concat "^" company-prefix k)))
> > (if (or (cl-find-if (lambda (s) (string-match re s))
> > company-candidates)
> > (> (string-to-number k)
> > (length company-candidates))
> > (looking-back "[0-9]+\\.[0-9]*" (line-beginning-position)))
> > (self-insert-command 1)
> > (company-complete-number
> > (if (equal k "0")
> > 10
> > (string-to-number k))))))
> >
> > I've figured out all the intention of the above code snippet except
> > the following line:
> >
> > (looking-back "[0-9]+\\.[0-9]*" (line-beginning-position)
> >
> > I can't understand why the regexp used here must be written in this
> > form. Any hints will be appreciated.
>
> Based on my tries, the following code snippet is enough:
>
>   (defun ora-company-number ()
>     "Forward to `company-complete-number'.
>      Unless the number is potentially part of the candidate.
>      In that case, insert the number."
>     (interactive)
>     (let* ((k (this-command-keys))
>            (re (concat "^" company-prefix k)))
>       (if (cl-find-if (lambda (s) (string-match re s))
>                       company-candidates)
>
>       (self-insert-command 1)
>         (company-complete-number
>          (if (equal k "0") 10
>            (string-to-number k)))
>     )))
>
>
> > [1] https://github.com/abo-abo/oremacs/blob/0c5f3284acedc966948cbb930e779af5189fd54e/modes/ora-company.el#L22

I came up with the following solution that seems to work:

```emacs-lisp
(use-package comany
  :config
  (setq company-tooltip-limit 16
      company-quick-access-keys '("1" "2" "3" "4" "5" "6" "7" "8" "9"
"0" "q" "w" "e" "r" "t" "y"))

  (defun hz-company-number ()
    "Convert the company-quick-access-keys to the candidates' row
NUMBER visible on the tooltip,
     and then feed it to `company-complete-number' to quickly select
and insert company candidates.
     If the currently entered character is belongs to
company-quick-access-keys and a part of the candidate simultaneously,
     append it to the currently entered string to construct new company-prefix."
    (interactive)
    (let* ((k (this-command-keys))
           (re (concat "^" company-prefix k)))
      (if (cl-find-if (lambda (s) (string-match re s))
                      company-candidates)

      (self-insert-command 1)
        (company-complete-number
     (cond
      ((equal k "1") 1)
      ((equal k "2") 2)
      ((equal k "3") 3)
      ((equal k "4") 4)
      ((equal k "5") 5)
      ((equal k "6") 6)
      ((equal k "7") 7)
      ((equal k "8") 8)
      ((equal k "9") 9)
      ((equal k "0") 10)
      ((equal k "q") 11)
      ((equal k "w") 12)
      ((equal k "e") 13)
      ((equal k "r") 14)
      ((equal k "t") 15)
      ((equal k "y") 16))
     ))))

  (let ((map company-active-map))
    (mapc (lambda (x) (define-key map (format "%s" x) 'hz-company-number))
      '(0 1 2 3 4 5 6 7 8 9 q w e r t y))))
```

Please refer to the following related discussions for some more
related information:

[1] https://github.com/abo-abo/oremacs/issues/38#issuecomment-948472184
[2] https://lists.gnu.org/archive/html/help-gnu-emacs/2021-09/msg00527.html

Any enhancements/suggestions are welcome.

HZ



  reply	other threads:[~2021-10-21 10:48 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-21  6:54 Try to understand the ora-company-number function again Hongyi Zhao
2021-10-21  6:59 ` Hongyi Zhao
2021-10-21  7:11 ` Hongyi Zhao
2021-10-21 10:48   ` Hongyi Zhao [this message]
2021-10-21 10:51     ` Hongyi Zhao
2021-10-21 14:00       ` Hongyi Zhao
2021-10-22  2:33         ` Hongyi Zhao
2021-10-26  5:19           ` Hongyi Zhao
2021-10-26  7:00             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-26 14:56               ` Hongyi Zhao
2021-10-27 23:08                 ` Emanuel Berg via Users list for the GNU Emacs text editor

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=CAGP6POLRtLA-Wpvzf7NZvNKXTnmP51CDihhowH57XNGKm4o5fQ@mail.gmail.com \
    --to=hongyi.zhao@gmail.com \
    --cc=help-gnu-emacs@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.