-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexecrun.el
More file actions
289 lines (240 loc) · 9.06 KB
/
Copy pathexecrun.el
File metadata and controls
289 lines (240 loc) · 9.06 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
;;; execrun.el --- Run through `compilation-mode' -*- lexical-binding: t; -*-
;; Copyright (C) 2022-2026 Shen, Jen-Chieh
;; Author: Shen, Jen-Chieh <jcs090218@gmail.com>
;; Maintainer: Shen, Jen-Chieh <jcs090218@gmail.com>
;; URL: https://github.com/jcs-elpa/execrun
;; Version: 0.1.0
;; Package-Requires: ((emacs "26.1") (ffpc "0.1.0") (f "0.20.0"))
;; Keywords: tools
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Run through `compilation-mode'.
;;
;;; Code:
(require 'cl-lib)
(require 'project)
(require 'ffpc)
(require 'f)
(defgroup execrun nil
"Run through `compilation-mode'."
:prefix "execrun-"
:group 'tools
:link '(url-link :tag "Repository" "https://github.com/jcs-elpa/execrun"))
(defconst execrun-base-buffer-name "execrun"
"Base filename for compilation buffer.")
(defcustom execrun-script-extensions
'("[.]sh" "[.]bat" "[.]ps1")
"A list of script file extension in RegExp."
:type 'list
:group 'execrun)
(defconst execrun-script-extension
(if (memq system-type '(cygwin windows-nt ms-dos)) "[.]bat" "[.]sh")
"Script file extension in RegExp.")
(defcustom execrun-build-script
(concat "[[:ascii:]]*build[[:ascii:]]*" execrun-script-extension)
"Name of the build/make file script."
:type 'string
:group 'execrun)
(defcustom execrun-run-script
(concat "[[:ascii:]]*run[[:ascii:]]*" execrun-script-extension)
"Name of the execute/run file script."
:type 'string
:group 'execrun)
(defcustom execrun-kill-buffer-function #'kill-this-buffer
"Function to kill output buffer."
:type 'function
:group 'execrun)
;;
;; (@* "Entry" )
;;
(defvar execrun-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "M-k") #'execrun-maybe-kill-buffer)
(define-key map (kbd "C-_") #'execrun-previous)
(define-key map (kbd "C-+") #'execrun-next)
map)
"Keymap for `execrun-mode'.")
(defcustom execrun-mode-hook nil
"Hook called when execrun minor mode is activated or deactivated."
:type 'hook
:group 'execrun)
;;;###autoload
(define-minor-mode execrun-mode
"Minor mode for build/run script text in the current compilation buffer."
:group 'execrun
:lighter nil
:keymap execrun-mode-map
:init-value nil)
;;
;; (@* "Util" )
;;
(defun execrun--get-buffers (str type)
"Return a list of buffers that match STR.
TYPE is the return type; can be `object or `string."
(execrun--get-buffers-regexp (regexp-quote str) type))
(defun execrun--get-buffers-regexp (regexp type)
"Return a list of buffers that match REGEXP.
TYPE is the return type; can be `object or `string."
(let (buf-lst buf-name)
(if (not (stringp regexp))
(user-error "[WARNING] Can't get buffers with this string/regexp: %s" regexp)
(dolist (buf (buffer-list))
(setq buf-name (buffer-name buf))
(when (and (stringp buf-name) (string-match-p regexp buf-name))
(cl-case type
(`object (push buf buf-lst))
(`string (push buf-name buf-lst))))))
buf-lst))
(defun execrun--string-compare-p (regexp str type &optional ignore-case)
"Compare STR with REGEXP by TYPE.
Argument TYPE can be on of the following symbol.
* regex - uses function `string-match-p'. (default)
* strict - uses function `string='.
* prefix - uses function `string-prefix-p'.
* suffix - uses function `string-suffix-p'.
Optional argument IGNORE-CASE is only uses when TYPE is either symbol `prefix'
or `suffix'."
(cl-case type
(`strict (string= regexp str))
(`prefix (string-prefix-p regexp str ignore-case))
(`suffix (string-suffix-p regexp str ignore-case))
(t (ignore-errors (string-match-p regexp str)))))
(defun execrun--buffer-filter (name &optional type)
"Return a list of buffers with NAME.
See function `execrun--string-compare-p' for argument TYPE."
(let (lst)
(dolist (buf (buffer-list))
(when (execrun--string-compare-p name (buffer-name buf) type)
(push buf lst)))
lst))
;;
;; (@* "Control Output" )
;;
(defun execrun-list-compilation ()
"Return the list of compilation buffers."
(execrun--buffer-filter (format "[*]%s[*]: " execrun-base-buffer-name) 'regex))
(defun execrun-set-compilation-index (index lst)
"Set compilation buffer with INDEX and LST."
(cond ((< index 0) (setq index (1- (length lst))))
((>= index (length lst)) (setq index 0)))
(switch-to-buffer (nth index lst)))
;;;###autoload
(defun execrun-popup ()
"Show output window."
(interactive)
(let ((output-lst (execrun-list-compilation)))
(if (= 0 (length output-lst))
(user-error "[INFO] No output compilation exists")
(execrun-set-compilation-index 0 output-lst))))
;;
;; (@* "Build & Run" )
;;
(defun execrun--form-file-prefix ()
"Form the prefix of the compilation buffer name."
(format "*%s*: " execrun-base-buffer-name))
;;;###autoload
(defun execrun-switch-to-buffer ()
"Switch to one of the output buffer."
(interactive)
(let* ((output-prefix (execrun--form-file-prefix))
(output-buf-lst (execrun--get-buffers output-prefix 'string))
choice)
(if (not output-buf-lst)
(user-error "[INFO] No output buffer available: %s" output-buf-lst)
(setq choice (completing-read "Output buffer: " output-buf-lst))
(switch-to-buffer choice))))
;;;###autoload
(defun execrun-project-file (file title)
"Compile FILE from the project with TITLE."
(interactive)
(execrun-compile (ffpc-project-or-current-dir file title)))
;;;###autoload
(defun execrun-compile (in-op)
"Compile command rewrapper.
IN-OP : inpuit operation script."
(let* (;; NOTE: First we need to get the script directory. In order
;; to change execute/workspace directory to the current target script's
;; directory path.
(script-dir (f-dirname in-op))
;; NOTE: Change the current execute/workspace directory
;; to the script directory temporary. So the script will execute
;; within the current directory the script is currently in.
;;
;; Without these lines of code, the script will execute in the
;; `default-directory' variables. The `default-directory' variables
;; will be the directory path where you start the Emacs. For instance,
;; if you start Emacs at path `/usr/home', then the default directory
;; will be at `usr/home' directory.
;;
;; Adding these lines of code if your scirpt is at `/usr/home/project/some-script.sh',
;; Then your `default-directory' became `usr/home/project'. Hurray!
(default-directory script-dir))
;; Compile/Execute the target script.
(compile in-op t)
(with-current-buffer "*compilation*"
(rename-buffer (format "%s%s" (execrun--form-file-prefix) (f-filename in-op)) t)
(execrun-mode 1))
(message "Executing script file: '%s'" in-op)))
;;
;; (@* "Functions" )
;;
;;;###autoload
(defun execrun ()
"Select the script to execute."
(interactive)
(execrun-project-file execrun-script-extensions "Script file: "))
;;;###autoload
(defun execrun-build ()
"Make the current build."
(interactive)
(execrun-project-file execrun-build-script "Build script: "))
;;;###autoload
(defun execrun-run ()
"Run the current build program."
(interactive)
(execrun-project-file execrun-run-script "Run script: "))
;;;###autoload
(defun execrun-previous ()
"Select the previous compilation buffer."
(interactive)
(let ((output-lst (execrun-list-compilation)) (index 0) break)
(while (and (< index (length output-lst)) (not break))
(when (equal (current-buffer) (nth index output-lst))
(bury-buffer)
(execrun-set-compilation-index (1- index) output-lst)
(setq break t))
(cl-incf index))))
;;;###autoload
(defun execrun-next ()
"Select the next compilation buffer."
(interactive)
(let ((output-lst (execrun-list-compilation)) (index 0) break)
(while (and (< index (length output-lst)) (not break))
(when (equal (current-buffer) (nth index output-lst))
(execrun-set-compilation-index (1+ index) output-lst)
(setq break t))
(cl-incf index))))
;;;###autoload
(defun execrun-maybe-kill-buffer ()
"Maybe kill buffer action in `output' buffer."
(interactive)
(let ((output-len (length (execrun-list-compilation))) prev-output-buf)
(when (< 1 output-len)
(save-window-excursion
(execrun-previous)
(setq prev-output-buf (current-buffer))))
(funcall execrun-kill-buffer-function)
(when prev-output-buf (switch-to-buffer prev-output-buf))))
(provide 'execrun)
;;; execrun.el ends here