From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] em-cmpl: fix completion of command paths Date: Thu, 26 Jan 2023 08:02:51 +0200 Message-ID: <83cz71g7ec.fsf@gnu.org> References: <20230107121943.44658-1-nicolas@n16f.net> <83ilgufsdb.fsf@gnu.org> <87bkmmwcwx.fsf@valhala.localdomain> Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13718"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org, Stefan Monnier To: Nicolas Martyanoff Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Jan 26 07:03:16 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 1pKvLw-0003L9-9K for ged-emacs-devel@m.gmane-mx.org; Thu, 26 Jan 2023 07:03:16 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pKvLM-0001NR-Oq; Thu, 26 Jan 2023 01:02:40 -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 1pKvLK-0001NB-KV for emacs-devel@gnu.org; Thu, 26 Jan 2023 01:02:38 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pKvLK-0006s3-1r; Thu, 26 Jan 2023 01:02:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=WmdTI8Q/30z/kVGaObkovi++pdT+RehKVAybnLktNLs=; b=ECJrwO9fJ8Zc qZXA+5SQLT+CVH1qvrv2sbhcGI2oC8zf8+DIc12ByH7J/APOPT7os2A7jT7CsVmM1Tw2AVqaghbXk 0efPMbV83jc7M40MiYFXlnhid75L2QCR30h6QLfz0AcpEtkOQF4ITdixVj2q4gtAe2lXvDG7Sh4WQ vQIDf/g0MC7spdOLLVKHtz2tuqmGdZCZl9H3kWelVKGoAKVqcK1VdeTaqSLH8FmJE2UNs5wIiWdcF 9vdqYKwvr4jo7kouVdSGKdPrxKKYl+SPtm5YZoYUlDtgp9ItluRh8D1mV0luwLyYS7ZmoO+1n1VGw N0sU1cL4hmkFiN/D5I7MOw==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1pKvLJ-0001et-8o; Thu, 26 Jan 2023 01:02:37 -0500 In-Reply-To: <87bkmmwcwx.fsf@valhala.localdomain> (message from Nicolas Martyanoff on Wed, 25 Jan 2023 21:57:02 +0100) 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:302660 Archived-At: > From: Nicolas Martyanoff > Date: Wed, 25 Jan 2023 21:57:02 +0100 > > Well obviously I did not use git send-email correctly, so here is the > modified patch attached manually. > > Sorry about the noise. Thanks, please use Reply All to make sure everyone in the discussion gets the responses. > >From c0eadf74e2d98df8a918b83a6d2a089772673aba Mon Sep 17 00:00:00 2001 > From: Nicolas Martyanoff > Date: Sat, 7 Jan 2023 12:40:10 +0100 > Subject: [PATCH] em-cmpl: fix completion of command file names > > 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 file > name, 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..f6636a48173 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 file > + ;; name (absolute or relative): doing so assumes that the directory part > + ;; of the file name 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