-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.el
More file actions
108 lines (92 loc) · 4.12 KB
/
Copy pathexec.el
File metadata and controls
108 lines (92 loc) · 4.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
;; -*- lexical-binding: t -*-
;;
;; TODO: it would be better when `discard-stderr' is nil to redirect
;; STDERR to its own separate buffer (something like
;; *COMMAND-STDERR*).
;; This cannot accomplished using `call-process': we should switch to
;; the more sophisticated `make-process' instead.
(defun exec< (command &optional discard-stderr)
"Execute COMMAND redirecting its output before point.
The command COMMAND is executed using the shell set in the environment
variable `SHELL', its output (both stdout and stderr) is redirected in
the current buffer before point, unless DISCARD-STDERR is non-nil: in
such case standard error is simply discarded.
DISCARD-STDERR can be set using `universal-argument' \\[universal-argument]."
(interactive
(list
(read-shell-command "Command: ")
current-prefix-arg))
(call-process (getenv "SHELL")
nil `(t ,(not discard-stderr)) t "-c" command))
(defun exec> (command &optional start end discard-stderr)
"Execute COMMAND providing its standard input.
The command COMMAND is executed using the shell set in the environment
variable `SHELL', its input is provided from the area in the current
buffer delimited by START and END. When called in interactive mode,
the area will correspond to the active region or, if there is no
active region, the whole buffer.
COMMAND output (both stdout and stderr) is redirected in a separate
buffer, unless DISCARD-STDERR is non-nil: in such case standard error
is simply discarded.
DISCARD-STDERR can be set using `universal-argument' \\[universal-argument]."
(interactive
(list
(read-shell-command "Command: ")
(when (use-region-p) (region-beginning))
(when (use-region-p) (region-end))
current-prefix-arg))
(unless (numberp start) (setq start (point-min)))
(unless (numberp end) (setq end (point-max)))
(let ((bufname (generate-new-buffer-name (format "*%s-OUTPUT*" (car (string-split command)))))
(temp-buffer-show-hook (lambda ()
(read-only-mode)
(text-mode))))
(with-output-to-temp-buffer bufname
(call-process-region
start end
(getenv "SHELL") nil (list bufname (not discard-stderr)) t
"-c" command))))
(defun exec| (command &optional start end discard-stderr)
"Execute COMMAND providing stdin and replacing the area selected with its output.
The command COMMAND is executed using the shell set in the environment
variable `SHELL', its input is provided from the area in the current
buffer delimited by START and END. When called in interactive mode,
the area will correspond to the active region or, if there is no
active region, the whole buffer.
COMMAND output (both stdout and stderr) will replace the area between
START and END in the current buffer, unless DISCARD-STDERR is non-nil:
in such case standard error is simply discarded.
DISCARD-STDERR can be set using `universal-argument' \\[universal-argument]."
(interactive
(list
(read-shell-command "Command: ")
(when (use-region-p) (region-beginning))
(when (use-region-p) (region-end))
current-prefix-arg))
(unless (numberp start) (setq start (point-min)))
(unless (numberp end) (setq start (point-max)))
(save-excursion
(call-process-region
start end
(getenv "SHELL") t `(t ,(not discard-stderr)) t
"-c" command)))
(defun exec! (command &optional discard-stderr)
"Execute COMMAND and print its output in a separate buffer.
The command COMMAND is executed using the shell set in the environment
variable `SHELL'.
COMMAND output (both stdout and stderr) is redirected in a separate
buffer, unless DISCARD-STDERR is non-nil: in such case standard error
is simply discarded.
DISCARD-STDERR can be set using `universal-argument' \\[universal-argument]."
(interactive
(list
(read-shell-command "Command: ")
current-prefix-arg))
(let ((bufname (generate-new-buffer-name (format "*%s-OUTPUT*" (car (string-split command)))))
(temp-buffer-show-hook (lambda ()
(read-only-mode)
(text-mode))))
(with-output-to-temp-buffer bufname
(call-process (getenv "SHELL") nil (list bufname (not discard-stderr)) t
"-c" command))))
(provide 'exec)