From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.bugs Subject: bug#44016: 28.0.50; Add new "gnus-search" search interface to Gnus Date: Sun, 01 Nov 2020 19:43:36 -0800 Message-ID: <87k0v4bezb.fsf@ericabrahamsen.net> References: <877drrigg6.fsf@ericabrahamsen.net> <87k0vqsqol.fsf@gnus.org> <87tuuu5fyf.fsf@ericabrahamsen.net> <874km9d4mc.fsf@ericabrahamsen.net> <87o8kgbvv6.fsf@ericabrahamsen.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27513"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 44016@debbugs.gnu.org To: Stefan Monnier Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Mon Nov 02 04:44:12 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kZQlN-00071K-Mr for geb-bug-gnu-emacs@m.gmane-mx.org; Mon, 02 Nov 2020 04:44:10 +0100 Original-Received: from localhost ([::1]:33090 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kZQlM-00058D-OJ for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 01 Nov 2020 22:44:08 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:35456) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kZQlG-000585-VG for bug-gnu-emacs@gnu.org; Sun, 01 Nov 2020 22:44:03 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]:55863) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kZQlG-0001aA-HQ for bug-gnu-emacs@gnu.org; Sun, 01 Nov 2020 22:44:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kZQlG-0001Kn-FU for bug-gnu-emacs@gnu.org; Sun, 01 Nov 2020 22:44:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Eric Abrahamsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 02 Nov 2020 03:44:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44016 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 44016-submit@debbugs.gnu.org id=B44016.16042886275095 (code B ref 44016); Mon, 02 Nov 2020 03:44:02 +0000 Original-Received: (at 44016) by debbugs.gnu.org; 2 Nov 2020 03:43:47 +0000 Original-Received: from localhost ([127.0.0.1]:39176 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kZQl1-0001K7-Fk for submit@debbugs.gnu.org; Sun, 01 Nov 2020 22:43:47 -0500 Original-Received: from ericabrahamsen.net ([52.70.2.18]:37482 helo=mail.ericabrahamsen.net) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kZQkz-0001Jt-Br for 44016@debbugs.gnu.org; Sun, 01 Nov 2020 22:43:46 -0500 Original-Received: from localhost (c-73-254-86-141.hsd1.wa.comcast.net [73.254.86.141]) (Authenticated sender: eric@ericabrahamsen.net) by mail.ericabrahamsen.net (Postfix) with ESMTPSA id 45DBBFE559; Mon, 2 Nov 2020 03:43:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ericabrahamsen.net; s=mail; t=1604288619; bh=f1OvafmfveoeryMgqt98wkL9EYMsd0Ks6p77P6/5ofo=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=y/VkwLSiGixIzpBcplYVkJGMn+56M/umlc5BrDolKVxMBwaJw7RcyoYr2sQXJV4OU 3U1uS5+X8dXFYUC0mFCFXxPeCZYgWq+K/2suFkLMq6mQ4mRs6fPoGl799G4DbLcqsp Qcox/V0NVZF6O0xS9Lpd4sNLPCwSJfEdveAQ67L0= In-Reply-To: (Stefan Monnier's message of "Sun, 01 Nov 2020 18:50:50 -0500") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:192498 Archived-At: On 11/01/20 18:50 PM, Stefan Monnier wrote: >> 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)? Right, at this stage the string is already out of the user's hands. > It just happens to reuse the completion machinery to do the work. I wrote a function to do the expansion, then realized it was just a poor implementation of half of what the completion functions already do. I understand that these functions are aimed at interactive use (the "(consp (cdr (completion-all-completions" bit above definitely felt like I was holding the tool upside down), but they're absolutely useful in a programmatic setting. I wonder if there could be some version of these functions that could be "blessed" for use in Elisp programs. > 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? Somewhere I'd gotten the idea that SPC was bound to `minibuffer-complete-word', I don't know how. > (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`. Ah, of course! And if `gnus-search--complete-key-data' is conservative about when it fires, it leaves the door open for other completion functions. This function could complete "cont" to "contact:", at which point (for example) an EBDB-specific capf function could take over and complete names or email addresses to search for. Thanks! Eric