aboutsummaryrefslogtreecommitdiffstats
path: root/lisp
diff options
context:
space:
mode:
authorMichael Albinus2022-03-22 10:29:16 +0100
committerMichael Albinus2022-03-22 10:29:16 +0100
commit0094dde11d97a0e69b053851a87f2934ef0e00aa (patch)
tree4db8bd0b777b68980b89b317f125e006dd88b267 /lisp
parent7872d496d5dc272dacd2d2a5fd9238e39f2dbea8 (diff)
downloademacs-0094dde11d97a0e69b053851a87f2934ef0e00aa.tar.gz
emacs-0094dde11d97a0e69b053851a87f2934ef0e00aa.zip
Fix grep-like functions when running on a remote host
* doc/lispref/processes.texi (Shell Arguments): * etc/NEWS: Describe change in 'shell-quote-argument'. Fix typos. * lisp/subr.el (shell-quote-argument): New optional argument POSIX. * lisp/progmodes/grep.el (grep-compute-defaults) (grep-default-command, grep-expand-keywords, lgrep) (rgrep-default-command): Use POSIX argument in `shell-quote-argument'. (Bug#54487)
Diffstat (limited to 'lisp')
-rw-r--r--lisp/progmodes/grep.el111
-rw-r--r--lisp/subr.el14
2 files changed, 68 insertions, 57 deletions
diff --git a/lisp/progmodes/grep.el b/lisp/progmodes/grep.el
index ccc58e6773c..21280888562 100644
--- a/lisp/progmodes/grep.el
+++ b/lisp/progmodes/grep.el
@@ -632,12 +632,12 @@ The value depends on `grep-command', `grep-template',
632 ,grep-use-null-filename-separator) 632 ,grep-use-null-filename-separator)
633 (grep-find-use-xargs ,grep-find-use-xargs) 633 (grep-find-use-xargs ,grep-find-use-xargs)
634 (grep-highlight-matches ,grep-highlight-matches))))) 634 (grep-highlight-matches ,grep-highlight-matches)))))
635 (let* ((host-id 635 (let* ((remote (file-remote-p default-directory))
636 (intern (or (file-remote-p default-directory) "localhost"))) 636 (host-id (intern (or remote "localhost")))
637 (host-defaults (assq host-id grep-host-defaults-alist)) 637 (host-defaults (assq host-id grep-host-defaults-alist))
638 (defaults (assq nil grep-host-defaults-alist)) 638 (defaults (assq nil grep-host-defaults-alist))
639 (quot-braces (shell-quote-argument "{}")) 639 (quot-braces (shell-quote-argument "{}" remote))
640 (quot-scolon (shell-quote-argument ";"))) 640 (quot-scolon (shell-quote-argument ";" remote)))
641 ;; There are different defaults on different hosts. They must be 641 ;; There are different defaults on different hosts. They must be
642 ;; computed for every host once. 642 ;; computed for every host once.
643 (dolist (setting '(grep-command grep-template 643 (dolist (setting '(grep-command grep-template
@@ -820,7 +820,9 @@ The value depends on `grep-command', `grep-template',
820 820
821(defun grep-default-command () 821(defun grep-default-command ()
822 "Compute the default grep command for \\[universal-argument] \\[grep] to offer." 822 "Compute the default grep command for \\[universal-argument] \\[grep] to offer."
823 (let ((tag-default (shell-quote-argument (grep-tag-default))) 823 (let ((tag-default
824 (shell-quote-argument
825 (grep-tag-default) (file-remote-p default-directory)))
824 ;; This a regexp to match single shell arguments. 826 ;; This a regexp to match single shell arguments.
825 ;; Could someone please add comments explaining it? 827 ;; Could someone please add comments explaining it?
826 (sh-arg-re 828 (sh-arg-re
@@ -963,7 +965,8 @@ easily repeat a find command."
963 ("<F>" . files) 965 ("<F>" . files)
964 ("<N>" . (null-device)) 966 ("<N>" . (null-device))
965 ("<X>" . excl) 967 ("<X>" . excl)
966 ("<R>" . (shell-quote-argument (or regexp "")))) 968 ("<R>" . (shell-quote-argument
969 (or regexp "") (file-remote-p (expand-file-name (or dir "."))))))
967 "List of substitutions performed by `grep-expand-template'. 970 "List of substitutions performed by `grep-expand-template'.
968If car of an element matches, the cdr is evalled in order to get the 971If car of an element matches, the cdr is evalled in order to get the
969substitution string. 972substitution string.
@@ -1112,11 +1115,12 @@ command before it's run."
1112 (when (and (stringp regexp) (> (length regexp) 0)) 1115 (when (and (stringp regexp) (> (length regexp) 0))
1113 (unless (and dir (file-accessible-directory-p dir)) 1116 (unless (and dir (file-accessible-directory-p dir))
1114 (setq dir default-directory)) 1117 (setq dir default-directory))
1115 (let ((command regexp)) 1118 (let ((command regexp) remote)
1116 (if (null files) 1119 (if (null files)
1117 (if (string= command grep-command) 1120 (if (string= command grep-command)
1118 (setq command nil)) 1121 (setq command nil))
1119 (setq dir (file-name-as-directory (expand-file-name dir))) 1122 (setq dir (file-name-as-directory (expand-file-name dir))
1123 remote (file-remote-p dir))
1120 (unless (or (not grep-use-directories-skip) 1124 (unless (or (not grep-use-directories-skip)
1121 (eq grep-use-directories-skip t)) 1125 (eq grep-use-directories-skip t))
1122 (setq grep-use-directories-skip 1126 (setq grep-use-directories-skip
@@ -1134,11 +1138,12 @@ command before it's run."
1134 (mapconcat 1138 (mapconcat
1135 (lambda (ignore) 1139 (lambda (ignore)
1136 (cond ((stringp ignore) 1140 (cond ((stringp ignore)
1137 (shell-quote-argument ignore)) 1141 (shell-quote-argument
1142 ignore remote))
1138 ((consp ignore) 1143 ((consp ignore)
1139 (and (funcall (car ignore) dir) 1144 (and (funcall (car ignore) dir)
1140 (shell-quote-argument 1145 (shell-quote-argument
1141 (cdr ignore)))))) 1146 (cdr ignore) remote)))))
1142 grep-find-ignored-files 1147 grep-find-ignored-files
1143 " --exclude="))) 1148 " --exclude=")))
1144 (and (eq grep-use-directories-skip t) 1149 (and (eq grep-use-directories-skip t)
@@ -1242,48 +1247,50 @@ command before it's run."
1242(defun rgrep-default-command (regexp files dir) 1247(defun rgrep-default-command (regexp files dir)
1243 "Compute the command for \\[rgrep] to use by default." 1248 "Compute the command for \\[rgrep] to use by default."
1244 (require 'find-dired) ; for `find-name-arg' 1249 (require 'find-dired) ; for `find-name-arg'
1245 (grep-expand-template 1250 (let ((remote (file-remote-p (or dir default-directory))))
1246 grep-find-template 1251 (grep-expand-template
1247 regexp 1252 grep-find-template
1248 (concat (shell-quote-argument "(") 1253 regexp
1249 " " find-name-arg " " 1254 (concat (shell-quote-argument "(" remote)
1250 (mapconcat 1255 " " find-name-arg " "
1251 #'shell-quote-argument 1256 (mapconcat
1252 (split-string files) 1257 (lambda (x) (shell-quote-argument x remote))
1253 (concat " -o " find-name-arg " ")) 1258 (split-string files)
1254 " " 1259 (concat " -o " find-name-arg " "))
1255 (shell-quote-argument ")")) 1260 " "
1256 dir 1261 (shell-quote-argument ")" remote))
1257 (concat 1262 dir
1258 (and grep-find-ignored-directories 1263 (concat
1259 (concat "-type d " 1264 (and grep-find-ignored-directories
1260 (shell-quote-argument "(") 1265 (concat "-type d "
1261 ;; we should use shell-quote-argument here 1266 (shell-quote-argument "(" remote)
1262 " -path " 1267 ;; we should use shell-quote-argument here
1263 (mapconcat (lambda (d) (shell-quote-argument (concat "*/" d))) 1268 " -path "
1264 (rgrep-find-ignored-directories dir) 1269 (mapconcat
1265 " -o -path ") 1270 (lambda (d) (shell-quote-argument (concat "*/" d) remote))
1266 " " 1271 (rgrep-find-ignored-directories dir)
1267 (shell-quote-argument ")") 1272 " -o -path ")
1268 " -prune -o ")) 1273 " "
1269 (and grep-find-ignored-files 1274 (shell-quote-argument ")" remote)
1270 (concat (shell-quote-argument "!") " -type d " 1275 " -prune -o "))
1271 (shell-quote-argument "(") 1276 (and grep-find-ignored-files
1272 ;; we should use shell-quote-argument here 1277 (concat (shell-quote-argument "!" remote) " -type d "
1273 " -name " 1278 (shell-quote-argument "(" remote)
1274 (mapconcat 1279 ;; we should use shell-quote-argument here
1275 (lambda (ignore) 1280 " -name "
1276 (cond ((stringp ignore) 1281 (mapconcat
1277 (shell-quote-argument ignore)) 1282 (lambda (ignore)
1278 ((consp ignore) 1283 (cond ((stringp ignore)
1279 (and (funcall (car ignore) dir) 1284 (shell-quote-argument ignore remote))
1280 (shell-quote-argument 1285 ((consp ignore)
1281 (cdr ignore)))))) 1286 (and (funcall (car ignore) dir)
1282 grep-find-ignored-files 1287 (shell-quote-argument
1283 " -o -name ") 1288 (cdr ignore) remote)))))
1284 " " 1289 grep-find-ignored-files
1285 (shell-quote-argument ")") 1290 " -o -name ")
1286 " -prune -o "))))) 1291 " "
1292 (shell-quote-argument ")" remote)
1293 " -prune -o "))))))
1287 1294
1288(defun grep-find-toggle-abbreviation () 1295(defun grep-find-toggle-abbreviation ()
1289 "Toggle showing the hidden part of rgrep/lgrep/zrgrep command line." 1296 "Toggle showing the hidden part of rgrep/lgrep/zrgrep command line."
diff --git a/lisp/subr.el b/lisp/subr.el
index 8aadcfd453c..603acffea7a 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3760,14 +3760,18 @@ Note: :data and :device are currently not supported on Windows."
3760 3760
3761(declare-function w32-shell-dos-semantics "w32-fns" nil) 3761(declare-function w32-shell-dos-semantics "w32-fns" nil)
3762 3762
3763(defun shell-quote-argument (argument) 3763(defun shell-quote-argument (argument &optional posix)
3764 "Quote ARGUMENT for passing as argument to an inferior shell. 3764 "Quote ARGUMENT for passing as argument to an inferior shell.
3765 3765
3766This function is designed to work with the syntax of your system's 3766This function is designed to work with the syntax of your system's
3767standard shell, and might produce incorrect results with unusual shells. 3767standard shell, and might produce incorrect results with unusual shells.
3768See Info node `(elisp)Security Considerations'." 3768See Info node `(elisp)Security Considerations'.
3769 (cond 3769
3770 ((eq system-type 'ms-dos) 3770If the optional POSIX argument is non-nil, ARGUMENT is quoted
3771according to POSIX shell quoting rules, regardless of the
3772system's shell."
3773(cond
3774 ((and (not posix) (eq system-type 'ms-dos))
3771 ;; Quote using double quotes, but escape any existing quotes in 3775 ;; Quote using double quotes, but escape any existing quotes in
3772 ;; the argument with backslashes. 3776 ;; the argument with backslashes.
3773 (let ((result "") 3777 (let ((result "")
@@ -3782,7 +3786,7 @@ See Info node `(elisp)Security Considerations'."
3782 start (1+ end)))) 3786 start (1+ end))))
3783 (concat "\"" result (substring argument start) "\""))) 3787 (concat "\"" result (substring argument start) "\"")))
3784 3788
3785 ((and (eq system-type 'windows-nt) (w32-shell-dos-semantics)) 3789 ((and (not posix) (eq system-type 'windows-nt) (w32-shell-dos-semantics))
3786 3790
3787 ;; First, quote argument so that CommandLineToArgvW will 3791 ;; First, quote argument so that CommandLineToArgvW will
3788 ;; understand it. See 3792 ;; understand it. See