all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Using digits and letters to select company-mode candidates.
@ 2021-09-29  0:57 Hongyi Zhao
  2021-09-29  6:23 ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 6+ messages in thread
From: Hongyi Zhao @ 2021-09-29  0:57 UTC (permalink / raw)
  To: help-gnu-emacs

The following code snippet comes here: [1]

;;;
(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))))))

(defun ora--company-good-prefix-p (orig-fn prefix)
(unless (and (stringp prefix) (string-match-p "\\`[0-9]+\\'" prefix))
(funcall orig-fn prefix)))
(ora-advice-add 'company--good-prefix-p :around #'ora--company-good-prefix-p)

(let ((map company-active-map))
(mapc (lambda (x) (define-key map (format "%d" x) 'ora-company-number))
(number-sequence 0 9))
(define-key map " " (lambda ()
(interactive)
(company-abort)
(self-insert-command 1)))
(define-key map (kbd "<return>") nil))
;;;


The above code enables to select and insert company-mode candidates
using digits without modifier. But it can only support 10 candidates,
numbered 0-9. I want to extend this feature to support more candidates
bumbered with a single digit or letter, e.g., 0-9, q, w, e, r, t, y,
so that I can select and insert more candidates conveniently. How can
the above code be improved to achieve this goal? Any hints will be
greatly appreciated.

[1] https://github.com/abo-abo/oremacs/blob/github/modes/ora-company.el#L22

Regards, HZ



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using digits and letters to select company-mode candidates.
  2021-09-29  0:57 Using digits and letters to select company-mode candidates Hongyi Zhao
@ 2021-09-29  6:23 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-09-29 10:42   ` Hongyi Zhao
  2021-09-29 10:49   ` Hongyi Zhao
  0 siblings, 2 replies; 6+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-29  6:23 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> (define-key map (kbd "<return>") nil))

I've heard one should put keys to #'ignore if one wants them
to do nothing, is that what you want here?

> The above code enables to select and insert company-mode
> candidates using digits without modifier. But it can only
> support 10 candidates, numbered 0-9. I want to extend this
> feature to support more candidates bumbered with a single
> digit or letter, e.g., 0-9, q, w, e, r, t, y

Hm ... maybe something like this?

(defun letter-to-pos (l)
  (let ((fst ?a))
    (when (>= l fst)
      (+ 10 (- l fst) ))))

;; (letter-to-pos ?a) ; 10
;; (letter-to-pos ?b) ; 11
;;                ..  ; ..
;; (letter-to-pos ?z) ; 35

Or this?

(require 'cl-lib)

(defun letter-to-index (l &optional offset)
  (let*((letters (list ?q ?w ?e ?r ?t ?y))
        (pos     (cl-position l letters))
        (offs    (or offset 10)) )
    (when pos
      (+ offs pos) )))

;; (letter-to-index ?a) ; nil
;; (letter-to-index ?q) ; 10
;; (letter-to-index ?w) ; 11
;; (letter-to-index ?e) ; 12
;; (letter-to-index ?r) ; 13
;; (letter-to-index ?t) ; 14
;; (letter-to-index ?y) ; 15
;; (letter-to-index ?u) ; nil

HIH

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using digits and letters to select company-mode candidates.
  2021-09-29  6:23 ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-09-29 10:42   ` Hongyi Zhao
  2021-09-29 10:49   ` Hongyi Zhao
  1 sibling, 0 replies; 6+ messages in thread
From: Hongyi Zhao @ 2021-09-29 10:42 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Wed, Sep 29, 2021 at 2:23 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > (define-key map (kbd "<return>") nil))
>
> I've heard one should put keys to #'ignore if one wants them
> to do nothing, is that what you want here?

I don't know. In fact, I commented out the following code block when using it:

; (define-key map " " (lambda ()
; (interactive)
; (company-abort)
; (self-insert-command 1)))
; (define-key map (kbd "<return>") nil)


> > The above code enables to select and insert company-mode
> > candidates using digits without modifier. But it can only
> > support 10 candidates, numbered 0-9. I want to extend this
> > feature to support more candidates bumbered with a single
> > digit or letter, e.g., 0-9, q, w, e, r, t, y
>
> Hm ... maybe something like this?
>
> (defun letter-to-pos (l)
>   (let ((fst ?a))
>     (when (>= l fst)
>       (+ 10 (- l fst) ))))
>
> ;; (letter-to-pos ?a) ; 10
> ;; (letter-to-pos ?b) ; 11
> ;;                ..  ; ..
> ;; (letter-to-pos ?z) ; 35
>
> Or this?
>
> (require 'cl-lib)
>
> (defun letter-to-index (l &optional offset)
>   (let*((letters (list ?q ?w ?e ?r ?t ?y))
>         (pos     (cl-position l letters))
>         (offs    (or offset 10)) )
>     (when pos
>       (+ offs pos) )))
>
> ;; (letter-to-index ?a) ; nil
> ;; (letter-to-index ?q) ; 10
> ;; (letter-to-index ?w) ; 11
> ;; (letter-to-index ?e) ; 12
> ;; (letter-to-index ?r) ; 13
> ;; (letter-to-index ?t) ; 14
> ;; (letter-to-index ?y) ; 15
> ;; (letter-to-index ?u) ; nil

Thank you for your insightful comments/tips. But I still don't know
how to interface them with the original code snippets. Ashamed of my
superficial knowledge of lisp.

HZ



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using digits and letters to select company-mode candidates.
  2021-09-29  6:23 ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-09-29 10:42   ` Hongyi Zhao
@ 2021-09-29 10:49   ` Hongyi Zhao
  2021-09-29 18:50     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-04 11:55     ` Hongyi Zhao
  1 sibling, 2 replies; 6+ messages in thread
From: Hongyi Zhao @ 2021-09-29 10:49 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Wed, Sep 29, 2021 at 2:23 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > (define-key map (kbd "<return>") nil))
>
> I've heard one should put keys to #'ignore if one wants them
> to do nothing, is that what you want here?
>
> > The above code enables to select and insert company-mode
> > candidates using digits without modifier. But it can only
> > support 10 candidates, numbered 0-9. I want to extend this
> > feature to support more candidates bumbered with a single
> > digit or letter, e.g., 0-9, q, w, e, r, t, y
>
> Hm ... maybe something like this?
>
> (defun letter-to-pos (l)
>   (let ((fst ?a))
>     (when (>= l fst)
>       (+ 10 (- l fst) ))))
>
> ;; (letter-to-pos ?a) ; 10
> ;; (letter-to-pos ?b) ; 11
> ;;                ..  ; ..
> ;; (letter-to-pos ?z) ; 35
>
> Or this?
>
> (require 'cl-lib)
>
> (defun letter-to-index (l &optional offset)
>   (let*((letters (list ?q ?w ?e ?r ?t ?y))
>         (pos     (cl-position l letters))
>         (offs    (or offset 10)) )
>     (when pos
>       (+ offs pos) )))
>
> ;; (letter-to-index ?a) ; nil
> ;; (letter-to-index ?q) ; 10
> ;; (letter-to-index ?w) ; 11
> ;; (letter-to-index ?e) ; 12
> ;; (letter-to-index ?r) ; 13
> ;; (letter-to-index ?t) ; 14
> ;; (letter-to-index ?y) ; 15
> ;; (letter-to-index ?u) ; nil

Just as the original author abo-abo has mentioned there [1]:

Unless the number is potentially part of the candidate.
In that case, insert the number.


In the extended version, the corresponding problems should also be
handled correctly, i.e.:

Unless the number or letter is potentially part of the candidate.
In that case, insert the number or letter.

I'm afraid that the above suggested code doesn't include the
corresponding manipulation for this problem.

[1] https://github.com/abo-abo/oremacs/blob/3809390019a7083c28e8502a82da94ca3a8ebba0/modes/ora-company.el#L22

HZ



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using digits and letters to select company-mode candidates.
  2021-09-29 10:49   ` Hongyi Zhao
@ 2021-09-29 18:50     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-10-04 11:55     ` Hongyi Zhao
  1 sibling, 0 replies; 6+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-29 18:50 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

> Unless the number is potentially part of the candidate.
> In that case, insert the number.

Make a list of candidates, then use `cl-find-if' and
`string-match'.

-- 
underground experts united
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using digits and letters to select company-mode candidates.
  2021-09-29 10:49   ` Hongyi Zhao
  2021-09-29 18:50     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-10-04 11:55     ` Hongyi Zhao
  1 sibling, 0 replies; 6+ messages in thread
From: Hongyi Zhao @ 2021-10-04 11:55 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

On Wed, Sep 29, 2021 at 6:49 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> On Wed, Sep 29, 2021 at 2:23 PM Emanuel Berg via Users list for the
> GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> >
> > Hongyi Zhao wrote:
> >
> > > (define-key map (kbd "<return>") nil))
> >
> > I've heard one should put keys to #'ignore if one wants them
> > to do nothing, is that what you want here?
> >
> > > The above code enables to select and insert company-mode
> > > candidates using digits without modifier. But it can only
> > > support 10 candidates, numbered 0-9. I want to extend this
> > > feature to support more candidates bumbered with a single
> > > digit or letter, e.g., 0-9, q, w, e, r, t, y
> >
> > Hm ... maybe something like this?
> >
> > (defun letter-to-pos (l)
> >   (let ((fst ?a))
> >     (when (>= l fst)
> >       (+ 10 (- l fst) ))))
> >
> > ;; (letter-to-pos ?a) ; 10
> > ;; (letter-to-pos ?b) ; 11
> > ;;                ..  ; ..
> > ;; (letter-to-pos ?z) ; 35
> >
> > Or this?
> >
> > (require 'cl-lib)
> >
> > (defun letter-to-index (l &optional offset)
> >   (let*((letters (list ?q ?w ?e ?r ?t ?y))
> >         (pos     (cl-position l letters))
> >         (offs    (or offset 10)) )
> >     (when pos
> >       (+ offs pos) )))
> >
> > ;; (letter-to-index ?a) ; nil
> > ;; (letter-to-index ?q) ; 10
> > ;; (letter-to-index ?w) ; 11
> > ;; (letter-to-index ?e) ; 12
> > ;; (letter-to-index ?r) ; 13
> > ;; (letter-to-index ?t) ; 14
> > ;; (letter-to-index ?y) ; 15
> > ;; (letter-to-index ?u) ; nil
>
> Just as the original author abo-abo has mentioned there [1]:
>
> Unless the number is potentially part of the candidate.
> In that case, insert the number.
>
>
> In the extended version, the corresponding problems should also be
> handled correctly, i.e.:
>
> Unless the number or letter is potentially part of the candidate.
> In that case, insert the number or letter.

Hongyi Zhao wrote:

> Unless the number is potentially part of the candidate.
> In that case, insert the number.

Make a list of candidates, then use `cl-find-if' and
`string-match'.

Thank you for your advice and tips. I will try to see if I can solve
this problem. I’ve just seen your reply in the archive here[1]. Since
you only reply to the list, not including my email address, which led
to all your replies to be deleted by my filter.

[1] https://lists.gnu.org/archive/html/help-gnu-emacs/2021-09/msg00548.html

BTW, the code snippet that confuses me the most is the following part [2]:

(defun ora--company-good-prefix-p (orig-fn prefix)
(unless (and (stringp prefix) (string-match-p "\\`[0-9]+\\'" prefix))
(funcall orig-fn prefix)))
(ora-advice-add 'company--good-prefix-p :around #'ora--company-good-prefix-p)

According to my tries, even I commented them out, the method suggested
there still works smoothly.

https://github.com/abo-abo/oremacs/blob/3809390019a7083c28e8502a82da94ca3a8ebba0/modes/ora-company.el#L41-L44

Best, HZ

> I'm afraid that the above suggested code doesn't include the
> corresponding manipulation for this problem.
>
> [1] https://github.com/abo-abo/oremacs/blob/3809390019a7083c28e8502a82da94ca3a8ebba0/modes/ora-company.el#L22
>
> HZ



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-10-04 11:55 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-29  0:57 Using digits and letters to select company-mode candidates Hongyi Zhao
2021-09-29  6:23 ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 10:42   ` Hongyi Zhao
2021-09-29 10:49   ` Hongyi Zhao
2021-09-29 18:50     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-10-04 11:55     ` Hongyi Zhao

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.