unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Eric Abrahamsen <eric@ericabrahamsen.net>
Cc: 44016@debbugs.gnu.org
Subject: bug#44016: 28.0.50; Add new "gnus-search" search interface to Gnus
Date: Sun, 01 Nov 2020 18:50:50 -0500	[thread overview]
Message-ID: <jwvv9eowt9j.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <87o8kgbvv6.fsf@ericabrahamsen.net> (Eric Abrahamsen's message of "Sun, 01 Nov 2020 13:38:53 -0800")

> The expandable search keys are kept in `gnus-search-expandable-keys'.
> The programmatic completion part looks like:
>
> --8<---------------cut here---------------start------------->8---
> +(defun gnus-search-query-expand-key (key)
> +  (cond ((test-completion key gnus-search-expandable-keys)
> +	 ;; We're done!
> +	 key)
> +	;; There is more than one possible completion.
> +	((consp (cdr (completion-all-completions
> +		      key gnus-search-expandable-keys #'stringp 0)))
> +	 (signal 'gnus-search-parse-error
> +		 (list (format "Ambiguous keyword: %s" key))))
> +	;; Return KEY, either completed or untouched.
> +	((car-safe (completion-try-completion
> +		    key gnus-search-expandable-keys
> +                    #'stringp 0)))))
> --8<---------------cut here---------------end--------------->8---

IIUC this function is used to expand unambiguous abbreviations, right
(rather than the more usual "completion" which is done as the user is
typing)?

It just happens to reuse the completion machinery to do the work.
If so, it looks OK (I guess you could try and reuse the output from
completion-all-completions in the last branch instead of calling
completion-try-completion, but it might be more trouble than it's worth).

> --8<---------------cut here---------------start------------->8---
> +(defvar gnus-search-minibuffer-map
> +  (let ((km (make-sparse-keymap)))
> +    (set-keymap-parent km minibuffer-local-map)
> +    (define-key km (kbd "SPC") #'self-insert-command)
> +    (define-key km (kbd "TAB") #'gnus-search-complete-key)
> +    km))
> +
> +(defun gnus-search-complete-key ()
> +  "Complete a search key at point.
> +Used when reading a search query from the minibuffer."
> +  (interactive)
> +  (when (completion-in-region
> +	 (save-excursion
> +	   (if (re-search-backward " " (minibuffer-prompt-end) t)
> +	       (1+ (point))
> +	     (minibuffer-prompt-end)))
> +	 (point) gnus-search-expandable-keys)
> +    (insert ":")))
> +
> +(defun gnus-search-make-spec (arg)
> +  (list (cons 'query
> +	      (read-from-minibuffer
> +	       "Query: " nil gnus-search-minibuffer-map
> +	       nil 'gnus-search-history))
> +	(cons 'raw arg)))
> --8<---------------cut here---------------end--------------->8---

Hmm... here I think instead of calling `completion-in-region` yourself,
you'd want to do something like:

    (defvar gnus-search-minibuffer-map
      (let ((km (make-sparse-keymap)))
        (set-keymap-parent km minibuffer-local-map)
        (define-key km (kbd "SPC") #'self-insert-command) ;; Isn't this redundant?
        (define-key km (kbd "TAB") #'completion-at-point)
        km))

    (defun gnus-search--complete-key-data ()
      "Return completion data for gnus-search keys."
      (list (save-excursion
              (if (re-search-backward " " (minibuffer-prompt-end) t)
                  (1+ (point))
                (minibuffer-prompt-end)))
            (point)
            gnus-search-expandable-keys))

    (minibuffer-with-setup-hook
        (lambda ()
          (add-hook 'completion-at-point-functions
                    #'gnus-search--complete-key-data nil t))
      (read-from-minibuffer ...))

See `read--expression` (used to provide completion on Elisp function
and var names when reading an ELisp expression in the minibuffer, such
as in `M-:`) for an example.

If you want to auto-insert a `:` you'd then do it via an `:exit-function`.


        Stefan






  reply	other threads:[~2020-11-01 23:50 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-15 16:47 bug#44016: 28.0.50; Add new "gnus-search" search interface to Gnus Eric Abrahamsen
2020-10-16  5:08 ` Lars Ingebrigtsen
2020-10-16 15:49   ` Eric Abrahamsen
2020-11-01  5:32     ` Eric Abrahamsen
2020-11-01 18:10       ` Basil L. Contovounesios
2020-11-01 18:22         ` Eli Zaretskii
2020-11-01 21:19         ` Eric Abrahamsen
2020-11-01 21:38       ` Eric Abrahamsen
2020-11-01 23:50         ` Stefan Monnier [this message]
2020-11-02  3:43           ` Eric Abrahamsen
2020-11-02 14:24             ` Stefan Monnier
2020-11-02 16:16               ` Eric Abrahamsen
2020-11-02 20:11                 ` Eric Abrahamsen
2020-11-04  5:22                   ` Eric Abrahamsen

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

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvv9eowt9j.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=44016@debbugs.gnu.org \
    --cc=eric@ericabrahamsen.net \
    /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 public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).