[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] externals/shell-command+ ab6bb0e 18/36: reworked `.' command
From: |
Stefan Monnier |
Subject: |
[elpa] externals/shell-command+ ab6bb0e 18/36: reworked `.' command |
Date: |
Fri, 25 Sep 2020 11:01:24 -0400 (EDT) |
branch: externals/shell-command+
commit ab6bb0e260ba40315ff22ff9641562d29f7f9ac5
Author: Philip K <philip@warpmail.net>
Commit: Philip K <philip@warpmail.net>
reworked `.' command
---
bang.el | 70 +++++++++++++++++++++++++++++++++++++++++------------------------
1 file changed, 44 insertions(+), 26 deletions(-)
diff --git a/bang.el b/bang.el
index 8907f37..57cfe4d 100644
--- a/bang.el
+++ b/bang.el
@@ -32,6 +32,27 @@
;;; Code:
+(defconst bang--command-regexp
+ (rx bos (* space)
+ (? (group (or "." "/") (* (not space))) (+ space))
+ (or (group "<") (group ">") (group "|") "!" "")
+ (* space)
+ (group (* not-newline))
+ eos))
+
+(defun bang-expand-path (path)
+ "Expand any PATH into absolute path with additional tricks.
+
+Furthermore, replace each sequence with three or more `.'s with a
+proper upwards directory pointers. This means that '....' becomes
+'../../../..', and so on."
+ (expand-file-name
+ (replace-regexp-in-string
+ (rx (>= 2 "."))
+ (lambda (sub)
+ (mapconcat #'identity (make-list (1- (length sub)) "..") "/"))
+ path)))
+
(defun bang (command beg end)
"Intelligently execute string COMMAND in inferior shell.
@@ -39,10 +60,15 @@ When COMMAND starts with
< the output of COMMAND replaces the current selection
> COMMAND is run with the current selection as input
| the current selection is filtered through COMMAND
- . COMMAND executes in the relative path following the dot
+ ! COMMAND is simply executed (same as without any prefix)
Without any argument, `bang' will behave like `shell-command'.
+Before these characters, one may also place a relative or
+absolute path, which will be the current working directory in
+which the command will be executed. See `bang-expand-path' for
+more details on this expansion.
+
Inside COMMAND, % is replaced with the current file name. To
insert a literal % quote it using a backslash.
@@ -52,18 +78,12 @@ between BEG and END. Otherwise the whole buffer is
processed."
(if (use-region-p) (region-beginning) (point-min))
(if (use-region-p) (region-end) (point-max))))
(save-match-data
- (unless (string-match (rx bos (* space)
- (or (group "<") (group ">") (group "|")
- (group "." (* (not space))) "")
- (* space)
- (group (* not-newline))
- eos)
- command)
+ (unless (string-match bang--command-regexp command)
(error "Invalid command"))
- (let ((has-< (match-string-no-properties 1 command))
- (has-> (match-string-no-properties 2 command))
- (has-| (match-string-no-properties 3 command))
- (has-. (match-string-no-properties 4 command))
+ (let ((path (match-string-no-properties 1 command))
+ (has-< (match-string-no-properties 2 command))
+ (has-> (match-string-no-properties 3 command))
+ (has-| (match-string-no-properties 4 command))
(rest (condition-case nil
(replace-regexp-in-string
(rx (* ?\\ ?\\) (or ?\\ (group "%")))
@@ -71,20 +91,18 @@ between BEG and END. Otherwise the whole buffer is
processed."
(match-string-no-properties 5 command)
nil nil 1)
(error (match-string-no-properties 5 command)))))
- (cond (has-< (delete-region beg end)
- (shell-command rest t shell-command-default-error-buffer)
- (exchange-point-and-mark))
- (has-> (shell-command-on-region
- beg end rest nil nil
- shell-command-default-error-buffer t))
- (has-| (shell-command-on-region
- beg end rest t t
- shell-command-default-error-buffer t))
- (has-. (let ((default-directory (expand-file-name has-.)))
- (shell-command rest (if current-prefix-arg t nil)
- shell-command-default-error-buffer)))
- (t (shell-command command (if current-prefix-arg t nil)
- shell-command-default-error-buffer)))
+ (let ((default-directory (bang-expand-path path)))
+ (cond (has-< (delete-region beg end)
+ (shell-command rest t shell-command-default-error-buffer)
+ (exchange-point-and-mark))
+ (has-> (shell-command-on-region
+ beg end rest nil nil
+ shell-command-default-error-buffer t))
+ (has-| (shell-command-on-region
+ beg end rest t t
+ shell-command-default-error-buffer t))
+ (t (shell-command command (if current-prefix-arg t nil)
+ shell-command-default-error-buffer))))
(when has->
(with-current-buffer "*Shell Command Output*"
(delete-region (point-min) (point-max)))))))
- [elpa] externals/shell-command+ aecad16 13/36: added link to public inbox, (continued)
- [elpa] externals/shell-command+ aecad16 13/36: added link to public inbox, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 64c0fcf 06/36: delete shell output instead of killing it, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 253d4bc 14/36: removed history code, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ ee9e3e6 17/36: added `.' command, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 11e121a 25/36: added support for bang--command-regexp to start with ~, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ a47986d 32/36: updated commentary section, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ f004dfb 23/36: require at least one non-whitespace character in command, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 55f3e2a 16/36: emulated insert-into-buffer feature from shell-command, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ f80c7c3 29/36: autoload bang command, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 8758933 24/36: removed trailing whitespace, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ ab6bb0e 18/36: reworked `.' command,
Stefan Monnier <=
- [elpa] externals/shell-command+ 87b5ad3 21/36: version bump, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 339931d 36/36: rename bang to shell-command+, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ b5398d1 34/36: use eshell handler if available, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 9b28c60 12/36: added melpa link, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 49f3686 08/36: fixed package-lint issues, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 5c01598 09/36: fixed minor checkdoc complaints, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 5e3def9 04/36: Fixed packaging issues and mistakes, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 423c2af 15/36: updated README, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ 3fcab36 20/36: only execute real command, not bang modifiers, Stefan Monnier, 2020/09/25
- [elpa] externals/shell-command+ c395408 05/36: Re-added % substitution, Stefan Monnier, 2020/09/25