From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Nicolas Martyanoff Newsgroups: gmane.emacs.devel Subject: [PATCH] em-cmpl: fix completion of command paths Date: Sat, 7 Jan 2023 13:19:44 +0100 Message-ID: <20230107121943.44658-1-nicolas@n16f.net> Mime-Version: 1.0 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3174"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Nicolas Martyanoff To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jan 07 13:21:26 2023 Return-path: Envelope-to: ged-emacs-devel@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 1pE8CT-0000dR-QF for ged-emacs-devel@m.gmane-mx.org; Sat, 07 Jan 2023 13:21:25 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pE8Bs-0005mR-Ea; Sat, 07 Jan 2023 07:20:48 -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 1pE8Bj-0005lf-Rq for emacs-devel@gnu.org; Sat, 07 Jan 2023 07:20:39 -0500 Original-Received: from mail-wr1-x429.google.com ([2a00:1450:4864:20::429]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pE8Bh-0001SQ-Hs for emacs-devel@gnu.org; Sat, 07 Jan 2023 07:20:39 -0500 Original-Received: by mail-wr1-x429.google.com with SMTP id co23so3547516wrb.4 for ; Sat, 07 Jan 2023 04:20:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=n16f-net.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=/Zq6teQL3+A77UJK06H9WDBURDkJxyQRZzVbsUiHirg=; b=ldItyubMuLVxazXIFZbhNoH8kQjRw/eoIMCakTEojY78V2bI0FkKU2+jzJ6WVIz1ZA 95Q7pjz5QHzmzDfnslM3DMJN5uPOt3g75+rlT6mhNaWsT3nywVCLY5gy8iAQUIugTnkH tLr1PtiHKn13UDhhsCAUYmybciNisfiG+UL7z7kayagq3CkqSzCnQMOpchW73/+taS0o rn41XmZpb23FX4Qrofiv/G80KogEN/pALFzaRXtEv7JtfjDI4i3KTzQsyC9onM30NQo4 hYUr8na/r8RcJO0t4bTXAQrxhCLLMqlyBj38D4V+7reqo5+uxpGsU/PEV7ujFZ2b6dQm yq+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=/Zq6teQL3+A77UJK06H9WDBURDkJxyQRZzVbsUiHirg=; b=yDN6SGCk4MW9wHBPwxWHPdD5kMLk0DkKU4hXSjhlWlMMOWo6zEZvZLGOj3xzRsCVl4 97S25QXjqPms/RKaVvuR9WULD+JhjohWCbOJwi4bsHORvSr7etjNcVWncVElNJUrxmFs p3Puv3aed/gB+kW5w5AlwJqioIQvLRgquv/+8M8fhv3WVWPKk2Iu7avgbqKWqyiVQnbP kGf9pVuauB/GZ/BNYb2rtCPo/4MsmOmiUj8sW8jSM6ZX4MTYRueGOMSldV95wl5IyAio 94nvVZlsFmxMHMv3i+/i2+CR1tHzmhn6ZxX75m5Ti0IuP8I+Wg+zgNjnK68fElM398ao GhvA== X-Gm-Message-State: AFqh2kpJNRdmrOnD97U7cjOTOC6X8AsItH2bC77fXHbWAMltUT9M8tGg vxSCyqPYuyX0Zq+2+sngE/PYiTomqVqzJvHEe1HY8HoM X-Google-Smtp-Source: AMrXdXucKo/cO0iCyko+rbfK96rk1DJwdntjs4+Wl4F79x5dE76nuxvpFVPmPvw0ZbCz1FgxrdIPpg== X-Received: by 2002:a5d:4705:0:b0:2a1:6ad2:3cac with SMTP id y5-20020a5d4705000000b002a16ad23cacmr9843809wrq.17.1673094035193; Sat, 07 Jan 2023 04:20:35 -0800 (PST) Original-Received: from localhost (2a01cb04001912003e217b0e0a701843.ipv6.abo.wanadoo.fr. [2a01:cb04:19:1200:3e21:7b0e:a70:1843]) by smtp.gmail.com with ESMTPSA id m7-20020a056000024700b002badc292224sm4083231wrz.90.2023.01.07.04.20.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 07 Jan 2023 04:20:34 -0800 (PST) X-Mailer: git-send-email 2.38.1 Received-SPF: pass client-ip=2a00:1450:4864:20::429; envelope-from=nicolas@n16f.net; helo=mail-wr1-x429.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:302269 Archived-At: Completion was originally broken by 82c76e3. The use of `completion-table-dynamic' was introduced in 899055e to fix bug#48995. However the following issue remains: when completing a command path, absolute ("/usr/bin/") or relative ("./subdir/"), a space is automatically added at the end. Bypassing `completion-table-dynamic' for filenames containing a directory part fixes the final space bug and does not reintroduce bug#48995. As a result, `eshell--pcomplete-executables' is not needed anymore, it was only necessary to work around the way `completion-table-dynamic' works. Thanks to Stefan Monnier for his input on the problem. --- lisp/eshell/em-cmpl.el | 127 ++++++++++++++++++----------------------- 1 file changed, 56 insertions(+), 71 deletions(-) diff --git a/lisp/eshell/em-cmpl.el b/lisp/eshell/em-cmpl.el index ca51cee2558..c5427fa1a6d 100644 --- a/lisp/eshell/em-cmpl.el +++ b/lisp/eshell/em-cmpl.el @@ -378,88 +378,73 @@ eshell-complete-parse-arguments args) posns))) -(defun eshell--pcomplete-executables () - "Complete amongst a list of directories and executables. - -Wrapper for `pcomplete-executables' or `pcomplete-dirs-or-entries', -depending on the value of `eshell-force-execution'. - -Adds path prefix to candidates independent of `action' value." - ;; `pcomplete-entries' returns filenames without path on `action' to - ;; use current string directory as done in `completion-file-name-table' - ;; when `action' is nil to construct executable candidates. - (let ((table (if eshell-force-execution - (pcomplete-dirs-or-entries nil #'file-readable-p) - (pcomplete-executables)))) - (lambda (string pred action) - (let ((cands (funcall table string pred action))) - (if (eq action t) - (let ((specdir (file-name-directory string))) - (mapcar - (lambda (cand) - (if (stringp cand) - (file-name-concat specdir cand) - cand)) - cands)) - cands))))) - (defun eshell--complete-commands-list () "Generate list of applicable, visible commands." ;; Building the commands list can take quite a while, especially over Tramp ;; (bug#41423), so do it lazily. (let ((glob-name - ;; When a command is specified using `eshell-explicit-command-char', + ;; When a command is specified using `eshell-explicit-command-char', ;; that char is not part of the command and hence not part of what ;; we complete. Adjust `pcomplete-stub' accordingly! - (if (and (> (length pcomplete-stub) 0) - (eq (aref pcomplete-stub 0) eshell-explicit-command-char)) - (setq pcomplete-stub (substring pcomplete-stub 1))))) - (completion-table-dynamic - (lambda (filename) - (if (file-name-directory filename) - (eshell--pcomplete-executables) - (let* ((paths (eshell-get-path)) - (cwd (file-name-as-directory - (expand-file-name default-directory))) - (filepath "") (completions ())) - ;; Go thru each path in the search path, finding completions. - (dolist (path paths) - (setq path (file-name-as-directory - (expand-file-name (or path ".")))) - ;; Go thru each completion found, to see whether it should - ;; be used. - (dolist (file (and (file-accessible-directory-p path) - (file-name-all-completions filename path))) - (setq filepath (concat path file)) - (if (and (not (member file completions)) ; - (or (string-equal path cwd) - (not (file-directory-p filepath))) - ;; FIXME: Those repeated file tests end up - ;; very costly over Tramp, we should cache the result. - (if eshell-force-execution + (if (and (> (length pcomplete-stub) 0) + (eq (aref pcomplete-stub 0) eshell-explicit-command-char)) + (setq pcomplete-stub (substring pcomplete-stub 1)))) + (filename (pcomplete-arg))) + ;; Do not use `completion-table-dynamic' when completing a command path + ;; (absolute or relative): doing so assumes that the subpath in the input + ;; string is always a command, and appends a space character, which is + ;; incorrect (i.e. "/usr/bi" should yield "/usr/bin/" after completion, + ;; not "/usr/bin/ "). + ;; + ;; If you work on this function, be careful not to reintroduce bug#48995. + (if (file-name-directory filename) + (if eshell-force-execution + (pcomplete-dirs-or-entries nil #'file-readable-p) + (pcomplete-executables)) + (completion-table-dynamic + (lambda (filename) + (let* ((paths (eshell-get-path)) + (cwd (file-name-as-directory + (expand-file-name default-directory))) + (filepath "") (completions ())) + ;; Go thru each path in the search path, finding completions. + (dolist (path paths) + (setq path (file-name-as-directory + (expand-file-name (or path ".")))) + ;; Go thru each completion found, to see whether it should + ;; be used. + (dolist (file (and (file-accessible-directory-p path) + (file-name-all-completions filename path))) + (setq filepath (concat path file)) + (if (and (not (member file completions)) ; + (or (string-equal path cwd) + (not (file-directory-p filepath))) + ;; FIXME: Those repeated file tests end up + ;; very costly over Tramp, we should cache the result. + (if eshell-force-execution (file-readable-p filepath) (file-executable-p filepath))) - (push file completions)))) - ;; Add aliases which are currently visible, and Lisp functions. - (pcomplete-uniquify-list - (if glob-name - completions - (setq completions - (append (if (fboundp 'eshell-alias-completions) - (eshell-alias-completions filename)) - (eshell-winnow-list - (mapcar + (push file completions)))) + ;; Add aliases which are currently visible, and Lisp functions. + (pcomplete-uniquify-list + (if glob-name + completions + (setq completions + (append (if (fboundp 'eshell-alias-completions) + (eshell-alias-completions filename)) + (eshell-winnow-list + (mapcar (lambda (name) (substring name 7)) - (all-completions (concat "eshell/" filename) - obarray #'functionp)) - nil '(eshell-find-alias-function)) - completions)) - (append (and (or eshell-show-lisp-completions - (and eshell-show-lisp-alternatives - (null completions))) - (all-completions filename obarray #'functionp)) - completions))))))))) + (all-completions (concat "eshell/" filename) + obarray #'functionp)) + nil '(eshell-find-alias-function)) + completions)) + (append (and (or eshell-show-lisp-completions + (and eshell-show-lisp-alternatives + (null completions))) + (all-completions filename obarray #'functionp)) + completions))))))))) (define-obsolete-function-alias 'eshell-pcomplete #'completion-at-point "27.1") -- 2.38.1