From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Daniel Mendler via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#74865: [PATCH] Use `completion-table-with-metadata' Date: Sat, 14 Dec 2024 13:57:11 +0100 Message-ID: <87o71eifrs.fsf@daniel-mendler.de> Reply-To: Daniel Mendler Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="27529"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Eli Zaretskii , Juri Linkov To: 74865@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Dec 14 13:58:21 2024 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 1tMRiv-00073M-7u for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 14 Dec 2024 13:58:21 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tMRig-0002NM-0e; Sat, 14 Dec 2024 07:58:06 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tMRid-0002Mi-E5 for bug-gnu-emacs@gnu.org; Sat, 14 Dec 2024 07:58:03 -0500 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tMRid-0004sb-5H for bug-gnu-emacs@gnu.org; Sat, 14 Dec 2024 07:58:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-Version:Date:From:To:Subject; bh=5vhVSgvjt06Qchm4IML71Kqi+YS/swBCbfKGbV9hVCw=; b=ozuOL/pGFy+F9JU6fxiKhtFE5fQiM7FkNWxAB1ZKyHLhjcUbbGvovrL48Ee0e9M04tXsVPwkN6u0uS+0ICoUmSiAhiZVdweKXmuYPMGtGYvMVM9nWAuCSB9sF1PWJSvkx9Ex1YYS8qUghN1ZSaUurTlLXp+11aW9hHOw7zh+x39VOteinGLzgWtUxxyaijwxjMT+S1AX9mv+2QGKE/UzdGA7dbe8KGcFdVnl2LzxDEn/Z2kOq9b3WYcSrHnu3j9yCyfSJUCQbwZUerlfVGvO2A8Hphvx/jD9RdoRLOPR5oAmNLs/+vnftEnmgS1hqxOn/wccXc3Az8bbTTI5enlj0g==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1tMRic-00086a-JN for bug-gnu-emacs@gnu.org; Sat, 14 Dec 2024 07:58:02 -0500 X-Loop: help-debbugs@gnu.org Resent-From: Daniel Mendler Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 14 Dec 2024 12:58:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 74865 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.173418105031066 (code B ref -1); Sat, 14 Dec 2024 12:58:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 14 Dec 2024 12:57:30 +0000 Original-Received: from localhost ([127.0.0.1]:45864 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tMRi5-00084z-6l for submit@debbugs.gnu.org; Sat, 14 Dec 2024 07:57:30 -0500 Original-Received: from lists.gnu.org ([209.51.188.17]:46430) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1tMRhz-00084h-Ry for submit@debbugs.gnu.org; Sat, 14 Dec 2024 07:57:28 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tMRhy-0002Kc-1g for bug-gnu-emacs@gnu.org; Sat, 14 Dec 2024 07:57:22 -0500 Original-Received: from server.qxqx.de ([2a01:4f8:c012:9177::1] helo=mail.qxqx.de) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tMRht-0004qD-BI; Sat, 14 Dec 2024 07:57:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=daniel-mendler.de; s=key; h=Content-Type:MIME-Version:Message-ID:Date: Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=5vhVSgvjt06Qchm4IML71Kqi+YS/swBCbfKGbV9hVCw=; b=Td37YvhN7pN2r080BEs5x3h6kr sP7muXev3Gi/zZR8vKARCVey4llPYvsO2JIeRe7VVC0xJZTkmivnSdTk4O47jgI8NHR4ggjU078v5 scBMxcvKl3PvarHsE5oIrep0JfK+wlleggTjOFwPw3z9E7nRzCh5o2j2XNRFuM1HOY74=; Received-SPF: pass client-ip=2a01:4f8:c012:9177::1; envelope-from=mail@daniel-mendler.de; helo=mail.qxqx.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:297046 Archived-At: --=-=-= Content-Type: text/plain Tags: patch This is a small follow-up of bug#74616. There I have introduced the `completion-table-with-metadata' helper function, which we can now take advantage of at more places. By using `completion-table-with-metadata' we avoid problems with completion sessions in recursive minibuffers. Daniel --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-Use-completion-table-with-metadata.patch >From f9dca5a2a7d27a09c33af662a58a152cb371f65a Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Mon, 9 Dec 2024 22:28:08 +0100 Subject: [PATCH] Use `completion-table-with-metadata' Prefer `completion-table-with-metadata' over explicit completion table lambdas for clarity. Furthermore prefer it over `completion-extra-properties' to avoid problems with recursive minibuffers and recursive completion sessions, since the completion metadata applies only to the outer completion session. * lisp/bookmark.el (bookmark-completing-read): * lisp/faces.el (read-face-name): * lisp/international/emoji.el (emoji--read-emoji): * lisp/net/dictionary.el (dictionary-completing-read-dictionary): * lisp/net/rcirc.el (rcirc-completion-at-point): * lisp/net/eww.el (eww-read-alternate-url): * lisp/simple.el (read-from-kill-ring): Use it. * lisp/calendar/calendar.el (calendar-read-date): Use `completion-table-with-metadata' and `completion-table-case-fold'. * lisp/proced.el (proced--read-signal): New function. (proced-send-signal): Use it. --- lisp/bookmark.el | 7 ++---- lisp/calendar/calendar.el | 14 +++++------ lisp/faces.el | 24 +++++++++---------- lisp/international/emoji.el | 35 +++++++++++++-------------- lisp/net/dictionary.el | 20 +++++++++------- lisp/net/eww.el | 47 ++++++++++++++++++++----------------- lisp/net/rcirc.el | 6 ++--- lisp/proced.el | 47 +++++++++++++++++-------------------- lisp/simple.el | 8 +++---- 9 files changed, 99 insertions(+), 109 deletions(-) diff --git a/lisp/bookmark.el b/lisp/bookmark.el index d43f9f740ca..e87b43b3c78 100644 --- a/lisp/bookmark.el +++ b/lisp/bookmark.el @@ -587,11 +587,8 @@ bookmark-completing-read (let* ((completion-ignore-case bookmark-completion-ignore-case) (default (unless (equal "" default) default))) (completing-read (format-prompt prompt default) - (lambda (string pred action) - (if (eq action 'metadata) - '(metadata (category . bookmark)) - (complete-with-action - action bookmark-alist string pred))) + (completion-table-with-metadata + bookmark-alist '((category . bookmark))) nil 0 nil 'bookmark-history default)))) diff --git a/lisp/calendar/calendar.el b/lisp/calendar/calendar.el index 345687d1775..f901e1f1506 100644 --- a/lisp/calendar/calendar.el +++ b/lisp/calendar/calendar.el @@ -2335,14 +2335,14 @@ calendar-read-date defyear)) (month-array calendar-month-name-array) (defmon (aref month-array (1- (calendar-extract-month default-date)))) - (completion-ignore-case t) (month (cdr (assoc-string - (let ((completion-extra-properties - '(:category calendar-month))) - (completing-read - (format-prompt "Month name" defmon) - (append month-array nil) - nil t nil nil defmon)) + (completing-read + (format-prompt "Month name" defmon) + (completion-table-with-metadata + (completion-table-case-fold + (append month-array nil)) + `((category . calendar-month))) + nil t nil nil defmon) (calendar-make-alist month-array 1) t))) (defday (calendar-extract-day default-date)) (last (calendar-last-day-of-month month year))) diff --git a/lisp/faces.el b/lisp/faces.el index f8ec0f1a187..05df685c679 100644 --- a/lisp/faces.el +++ b/lisp/faces.el @@ -1147,17 +1147,6 @@ read-face-name (let ((prompt (if default (format-prompt prompt default) (format "%s: " prompt))) - (completion-extra-properties - `(:affixation-function - ,(lambda (faces) - (mapcar - (lambda (face) - (list face - (concat (propertize read-face-name-sample-text - 'face face) - "\t") - "")) - faces)))) aliasfaces nonaliasfaces faces) ;; Build up the completion tables. (mapatoms (lambda (s) @@ -1180,7 +1169,18 @@ read-face-name (nreverse faces)) (let ((face (completing-read prompt - (completion-table-in-turn nonaliasfaces aliasfaces) + (completion-table-with-metadata + (completion-table-in-turn nonaliasfaces aliasfaces) + `((affixation-function + . ,(lambda (faces) + (mapcar + (lambda (face) + (list face + (concat (propertize read-face-name-sample-text + 'face face) + "\t") + "")) + faces))))) nil t nil 'face-name-history defaults))) (when (facep face) (if (stringp face) (intern face) diff --git a/lisp/international/emoji.el b/lisp/international/emoji.el index 7ede6ac8058..337a2914084 100644 --- a/lisp/international/emoji.el +++ b/lisp/international/emoji.el @@ -663,25 +663,22 @@ emoji--read-emoji (name (completing-read "Insert emoji: " - (lambda (string pred action) - (if (eq action 'metadata) - (list 'metadata - (cons - 'affixation-function - ;; Add the glyphs to the start of the displayed - ;; strings when TAB-ing. - (lambda (strings) - (mapcar - (lambda (name) - (if emoji-alternate-names - (list name "" "") - (list name - (concat - (or (gethash name emoji--all-bases) " ") - "\t") - ""))) - strings)))) - (complete-with-action action table string pred))) + (completion-table-with-metadata + table + `((affixation-function + ;; Add the glyphs to the start of the displayed + ;; strings when TAB-ing. + . ,(lambda (strings) + (mapcar + (lambda (name) + (if emoji-alternate-names + (list name "" "") + (list name + (concat + (or (gethash name emoji--all-bases) " ") + "\t") + ""))) + strings))))) nil t))) (if (cl-plusp (length name)) (let ((glyph (if emoji-alternate-names diff --git a/lisp/net/dictionary.el b/lisp/net/dictionary.el index 42fb8c57b40..8c7d87f56a5 100644 --- a/lisp/net/dictionary.el +++ b/lisp/net/dictionary.el @@ -1609,15 +1609,17 @@ dictionary-dictionaries (defun dictionary-completing-read-dictionary () "Prompt for a dictionary the server supports." (let* ((dicts (dictionary-dictionaries)) - (len (apply #'max (mapcar #'length (mapcar #'car dicts)))) - (completion-extra-properties - (list :annotation-function - (lambda (key) - (concat (make-string (1+ (- len (length key))) ?\s) - (alist-get key dicts nil nil #'string=)))))) - (completing-read (format-prompt "Select dictionary" - dictionary-default-dictionary) - dicts nil t nil nil dictionary-default-dictionary))) + (len (apply #'max (mapcar #'length (mapcar #'car dicts))))) + (completing-read + (format-prompt "Select dictionary" + dictionary-default-dictionary) + (completion-table-with-metadata + dicts + `((annotation-function + . ,(lambda (key) + (concat (make-string (1+ (- len (length key))) ?\s) + (alist-get key dicts nil nil #'string=)))))) + nil t nil nil dictionary-default-dictionary))) (define-button-type 'help-word :supertype 'help-xref diff --git a/lisp/net/eww.el b/lisp/net/eww.el index 4609755a902..9b4bbca2e3e 100644 --- a/lisp/net/eww.el +++ b/lisp/net/eww.el @@ -2926,31 +2926,34 @@ eww-read-alternate-url (mapcar #'caddr alternates)))) (sep-width (string-pixel-width " "))) (if (cdr alternates) - (let ((completion-extra-properties - (list :annotation-function - (lambda (feed) - (let* ((attrs (alist-get feed - alternates - nil - nil - #'string=)) - (type (car attrs)) - (title (cadr attrs))) + (completing-read + "Alternate URL: " + (completion-table-with-metadata + alternates + `((annotation-function + . ,(lambda (feed) + (let* ((attrs (alist-get feed + alternates + nil + nil + #'string=)) + (type (car attrs)) + (title (cadr attrs))) + (concat + (propertize " " 'display + `(space :align-to + (,(+ sep-width + url-max-width)))) + title + (when type (concat (propertize " " 'display `(space :align-to - (,(+ sep-width - url-max-width)))) - title - (when type - (concat - (propertize " " 'display - `(space :align-to - (,(+ (* 2 sep-width) - url-max-width - title-max-width)))) - "[" type "]")))))))) - (completing-read "Alternate URL: " alternates nil t)) + (,(+ (* 2 sep-width) + url-max-width + title-max-width)))) + "[" type "]")))))))) + nil t) (caar alternates))))) (defun eww-copy-alternate-url () diff --git a/lisp/net/rcirc.el b/lisp/net/rcirc.el index c41e2ec153f..cced386e2d0 100644 --- a/lisp/net/rcirc.el +++ b/lisp/net/rcirc.el @@ -1323,10 +1323,8 @@ rcirc-completion-at-point (rcirc-channel-nicks (rcirc-buffer-process) rcirc-target)))))) (list beg (point) - (lambda (str pred action) - (if (eq action 'metadata) - '(metadata (cycle-sort-function . identity)) - (complete-with-action action table str pred))))))) + (completion-table-with-metadata + table '((cycle-sort-function . identity))))))) (defun rcirc-set-decode-coding-system (coding-system) "Set the decode CODING-SYSTEM used in this channel." diff --git a/lisp/proced.el b/lisp/proced.el index da9212f6802..21d1d7c9da4 100644 --- a/lisp/proced.el +++ b/lisp/proced.el @@ -2110,6 +2110,20 @@ proced-with-processes-buffer (window-height . fit-window-to-buffer))) ,@body)))) +(defun proced--read-signal (count) + "Read a SIGNAL via `completing-read' for COUNT processes." + (completing-read + (format-prompt "Send signal [%s]" + "TERM" + (if (= 1 count) + "1 process" + (format "%d processes" count))) + (completion-table-with-metadata + (completion-table-case-fold proced-signal-list) + `((annotation-function + . ,(lambda (s) (cdr (assoc s proced-signal-list)))))) + nil nil nil nil "TERM")) + (defun proced-send-signal (&optional signal process-alist) "Send a SIGNAL to processes in PROCESS-ALIST. PROCESS-ALIST is an alist as returned by `proced-marked-processes'. @@ -2124,20 +2138,10 @@ proced-send-signal and SIGNAL is queried interactively. This noninteractive usage is still supported but discouraged. It will be removed in a future version of Emacs." (interactive - (let* ((process-alist (proced-marked-processes)) - (pnum (if (= 1 (length process-alist)) - "1 process" - (format "%d processes" (length process-alist)))) - (completion-ignore-case t) - (completion-extra-properties - `(:annotation-function - ,(lambda (s) (cdr (assoc s proced-signal-list)))))) - (proced-with-processes-buffer process-alist - (list (completing-read (format-prompt "Send signal [%s]" - "TERM" pnum) - proced-signal-list - nil nil nil nil "TERM") - process-alist))) + (let ((process-alist (proced-marked-processes))) + (proced-with-processes-buffer + process-alist + (list (proced--read-signal (length process-alist)) process-alist))) proced-mode) (unless (and signal process-alist) @@ -2151,18 +2155,9 @@ proced-send-signal (sit-for 2)) (setq process-alist (proced-marked-processes)) (unless signal - (let ((pnum (if (= 1 (length process-alist)) - "1 process" - (format "%d processes" (length process-alist)))) - (completion-ignore-case t) - (completion-extra-properties - `(:annotation-function - ,(lambda (s) (cdr (assoc s proced-signal-list)))))) - (proced-with-processes-buffer process-alist - (setq signal (completing-read (format-prompt "Send signal [%s]" - "TERM" pnum) - proced-signal-list - nil nil nil nil "TERM")))))) + (proced-with-processes-buffer + process-alist + (setq signal (proced--read-signal (length process-alist)))))) (let (failures) ;; Why not always use `signal-process'? See diff --git a/lisp/simple.el b/lisp/simple.el index f2ee4a5df67..e3e6ab6b564 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -6511,11 +6511,9 @@ read-from-kill-ring map))) (completing-read prompt - (lambda (string pred action) - (if (eq action 'metadata) - ;; Keep sorted by recency - '(metadata (display-sort-function . identity)) - (complete-with-action action completions string pred))) + ;; Keep sorted by recency + (completion-table-with-metadata + completions '((display-sort-function . identity))) nil nil nil (if history-pos (cons 'read-from-kill-ring-history -- 2.45.2 --=-=-=--