help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Bind two commands to one key to toggle between them for the same loc


From: Hongyi Zhao
Subject: Re: Bind two commands to one key to toggle between them for the same local keymap.
Date: Mon, 25 Oct 2021 22:52:28 +0800

On Sun, Oct 24, 2021 at 5:03 PM Michael Heerdegen
<michael_heerdegen@web.de> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > > >   (defun hz-company-search ()
> > > >     (interactive)
> > > >     (if (company-search-abort)
> > > >     (company-search-abort)
> > > >       (company-search-candidates)))
>
> Your test obviously raises an error.  Find one that works.


Thank you for your comment. I checked the definitions of  these two functions:

```emacs-lisp
(defun company-search-abort ()
  "Abort searching the completion candidates."
  (interactive)
  (company--search-assert-enabled)
  (company-search-mode 0)
  (company-set-selection company--search-old-selection t)
  (setq company-selection-changed company--search-old-changed))


(defun company-search-candidates ()
  "Start searching the completion candidates incrementally.

\\<company-search-map>Search can be controlled with the commands:
- `company-search-repeat-forward' (\\[company-search-repeat-forward])
- `company-search-repeat-backward' (\\[company-search-repeat-backward])
- `company-search-abort' (\\[company-search-abort])
- `company-search-delete-char' (\\[company-search-delete-char])

Regular characters are appended to the search string.

Customize `company-search-regexp-function' to change how the input
is interpreted when searching.

The command `company-search-toggle-filtering'
(\\[company-search-toggle-filtering])
uses the search string to filter the completion candidates."
  (interactive)
  (company-search-mode 1))
```

And figured out the following solution:

```emacs-lisp
(use-package company
  :bind (
     :map company-active-map
         ("<tab>" . (lambda ()
                    (interactive)
                    (if company-search-mode (company-search-abort)
                      (company-search-candidates))))

         :map company-search-map
         ("<tab>" . (lambda ()
                    (interactive)
                    (if company-search-mode (company-search-abort)
                      (company-search-candidates))))
     )
```

HZ



reply via email to

[Prev in Thread] Current Thread [Next in Thread]