File tree Expand file tree Collapse file tree 4 files changed +38
-0
lines changed Expand file tree Collapse file tree 4 files changed +38
-0
lines changed Original file line number Diff line number Diff line change @@ -247,6 +247,16 @@ protected by @code{shell-quote-argument};
247
247
@code {combine-and-quote-strings } is @emph {not } intended to protect
248
248
special characters from shell evaluation.
249
249
250
+ @defun shell-split-string string
251
+ This function splits @var {string } into substrings, respecting double
252
+ and single quotes, as well as backslash quoting.
253
+
254
+ @smallexample
255
+ (shell-split-string "ls /tmp/'foo bar'")
256
+ @result {} ("ls" "/tmp/foo bar")
257
+ @end smallexample
258
+ @end defun
259
+
250
260
@defun split-string-and-unquote string &optional separators
251
261
This function splits @var {string } into substrings at matches for the
252
262
regular expression @var {separators }, like @code {split-string } does
Original file line number Diff line number Diff line change @@ -2970,6 +2970,11 @@ The former is now declared obsolete.
2970
2970
2971
2971
* Lisp Changes in Emacs 28.1
2972
2972
2973
+ +++
2974
+ *** New function 'shell-split-string'.
2975
+ This splits a shell string into separate components, respecting single
2976
+ and double quotes, as well as backslash quoting.
2977
+
2973
2978
---
2974
2979
*** ':safe' settings in 'defcustom' are now propagated to the loaddefs files.
2975
2980
Original file line number Diff line number Diff line change @@ -459,6 +459,15 @@ Useful for shells like zsh that has this feature."
459
459
(push (mapconcat #'identity (nreverse arg) " " ) args)))
460
460
(cons (nreverse args) (nreverse begins)))))
461
461
462
+ (defun shell-split-string (string )
463
+ " Split STRING (a shell command) into a list of strings.
464
+ General shell syntax, like single and double quoting, as well as
465
+ backslash quoting, is respected."
466
+ (with-temp-buffer
467
+ (insert string)
468
+ (let ((comint-file-name-quote-list shell-file-name-quote-list))
469
+ (car (shell--parse-pcomplete-arguments )))))
470
+
462
471
(defun shell-command-completion-function ()
463
472
" Completion function for shell command names.
464
473
This is the value of `pcomplete-command-completion-function' for
Original file line number Diff line number Diff line change 45
45
(should (equal (shell--parse-pcomplete-arguments )
46
46
'((" cd" " ba" " " ) 1 4 7 )))))
47
47
48
+ (ert-deftest shell-tests-split-string ()
49
+ (should (equal (shell-split-string " ls /tmp" )
50
+ '(" ls" " /tmp" )))
51
+ (should (equal (shell-split-string " ls '/tmp/foo bar'" )
52
+ '(" ls" " /tmp/foo bar" )))
53
+ (should (equal (shell-split-string " ls \" /tmp/foo bar\" " )
54
+ '(" ls" " /tmp/foo bar" )))
55
+ (should (equal (shell-split-string " ls /tmp/'foo bar'" )
56
+ '(" ls" " /tmp/foo bar" )))
57
+ (should (equal (shell-split-string " ls /tmp/'foo\\ bar'" )
58
+ '(" ls" " /tmp/foo\\ bar" )))
59
+ (should (equal (shell-split-string " ls /tmp/foo\\ bar" )
60
+ '(" ls" " /tmp/foo bar" ))))
61
+
48
62
; ;; shell-tests.el ends here
You can’t perform that action at this time.
0 commit comments