@@ -50,24 +50,24 @@ The terminal emulator is specified in `ee-terminal-command'.
50
50
See `ee-start-terminal-function' for the usage.
51
51
"
52
52
(let* ((options (ee-get-terminal-options))
53
- (full-command (format " %s %s -e bash -c %s "
54
- ee-terminal-command
55
- options
56
- (shell-quote-argument command)))
57
- (proc (start-process-shell-command name nil full-command)))
53
+ (full-command (format " %s %s -e bash -c %s "
54
+ ee-terminal-command
55
+ options
56
+ (shell-quote-argument command)))
57
+ (proc (start-process-shell-command name nil full-command)))
58
58
(set-process-sentinel
59
59
proc
60
60
(lambda (p _m )
61
61
(unless (process-live-p p)
62
- (funcall callback p))))
62
+ (funcall callback p))))
63
63
proc))
64
64
65
65
(defcustom ee-start-terminal-function #'ee-start-external-terminal
66
66
" Function used to start the terminal.
67
67
See `ee-start-external-terminal' for function signature."
68
68
:type '(choice (const :tag " External terminal" ee-start-external-terminal)
69
- (const :tag " Eat" ee-eat-start-terminal)
70
- (function " Custom function" )))
69
+ (const :tag " Eat" ee-eat-start-terminal)
70
+ (function " Custom function" )))
71
71
72
72
(defun ee-start-process-shell-command-in-terminal (name command callback )
73
73
" Run COMMAND in a terminal.
@@ -84,143 +84,210 @@ NAME is passed to `ee-start-terminal-function'."
84
84
'((" find-file" . find-file)
85
85
(" browse-url" . browse-url)))
86
86
87
- (defun ee-find-file (target-file )
88
- (message " ee-find-file: %s " target-file)
89
- (when (not (string-empty-p target-file))
90
- (if (not current-prefix-arg)
91
- (find-file (string-trim target-file))
92
- (let ((action-fn
93
- (alist-get (completing-read " Action:" ee-find-file--actions)
94
- ee-find-file--actions nil nil 'equal )))
95
- (funcall action-fn target-file)))))
96
87
97
88
(defun ee--normalize-path (path )
98
89
(string-trim-right path (rx (or " \n " " \\ " " /" ))))
99
90
100
91
(defun ee-get-project-dir-or-current-dir ()
101
92
(let ((project-dir-command
102
- (format " git rev-parse --show-toplevel 2> /dev/null || echo -n %s "
93
+ (format " git rev-parse --show-toplevel 2> /dev/null || echo -n %s "
103
94
default-directory)))
104
95
(ee--normalize-path (shell-command-to-string project-dir-command))))
105
96
106
- (defun ee-find--callback (_process )
107
- (let* ((target-file (shell-command-to-string " cat /tmp/ee-find.tmp" ))
108
- (target-file (string-trim target-file)))
109
- (when (not (string-empty-p target-file))
110
- (message " ee-find opening: %s " target-file)
111
- (ee-find-file target-file))))
112
97
113
98
99
+ (defun ee-jump (destination )
100
+ " Jump to DESTINATION, which can be a file path with optional line and column numbers.
101
+ DESTINATION can be:
102
+ - /path/to/file
103
+ - /path/to/file:12
104
+ - /path/to/file:12:7
105
+ - /path/to/some-pdffile.pdf:Page 123: bala bala bala
106
+ "
107
+ (interactive " sEnter destination: " )
108
+ (let* ((components (split-string destination " :" ))
109
+ (file (car components))
110
+ (line (if (nth 1 components) (string-to-number (nth 1 components)) nil ))
111
+ ; ; pdf-page is the page number in pdf file, like: "Page 123", parse page number to pdf-page
112
+ (pdf-page-num (when (nth 1 components)
113
+ (when (string-match " Page \\ ([0-9]+\\ )" (nth 1 components))
114
+ (string-to-number (match-string 1 (nth 1 components))))))
115
+ (column (if (nth 2 components) (string-to-number (nth 2 components)) nil )))
116
+ (when (and (not (string-empty-p file)) (file-exists-p file))
117
+ (message " ee-jump get %s ; going jump to: file:%s , line:%s , column:%s " destination file line column)
118
+ (find-file file)
119
+ (when line
120
+ (goto-line line)
121
+ (when column
122
+ (move-to-column column)
123
+ (recenter )))
124
+ (when pdf-page-num
125
+ (pdf-view-goto-page pdf-page-num)))))
126
+
127
+ ; ; destination-file is a temporary file, it's content is the desitination we want to jump
128
+ (defun ee-jump-from (destination-file )
129
+ (let* ((destination (shell-command-to-string
130
+ (format " cat %s " destination-file)))
131
+ (destination (string-trim destination)))
132
+ (ee-jump destination)))
133
+
134
+ (defun ee-run (name working-directory command &optional args callback )
135
+ ; ; name is the process name, it's needed by `start-process-shell-command' s first argument
136
+ ; ; working-directory: after launch teriminal, it will cd to working-directory, then execute commands
137
+ ; ; command: the command to execute, should be a string, like: "yazi", "rg", "git", etc.
138
+ ; ; args: optional, should be list of string, the arguments for the command. like: '("arg1" "arg2" "arg3")'
139
+ ; ; callback, optional, the callback function to call after the command is executed, it will accept ee-process-output-file as argument
140
+ (let* (
141
+ ; ; pre-define ee-* commands' output file, callback function will read content from the file
142
+ (ee-process-stdout-file (format " /tmp/ee-stdout-%s .tmp " name))
143
+ ; ; construct command and args to execute in terminal:
144
+ (command-and-args (format " %s %s " command (string-join args " " )))
145
+ ; ; example:
146
+ ; ; cd [working-directory] && [command-and-args] > [/tmp/ee-output-ee-rg.tmp]
147
+ (full-command (format " cd %s && %s > %s "
148
+ working-directory
149
+ command-and-args
150
+ ee-process-stdout-file))
151
+ (process-callback (if callback
152
+ (lambda (process )
153
+ (funcall callback ee-process-stdout-file))
154
+ #'ignore )))
155
+ (ee-start-process-shell-command-in-terminal
156
+ name
157
+ full-command
158
+ process-callback)))
159
+
160
+ ; ;;;;; define ee commands here: ee-rg, ee-line, ee-yazi, etc. ;;;;;;;;;;;
161
+
162
+ ; ;; Eval Exec search line
163
+ (defun ee-line ()
164
+ (interactive )
165
+ (ee-run " ee-line" default-directory
166
+ (ee-script-path " eee-line.sh" )
167
+ (list buffer-file-name)
168
+ 'ee-jump-from ))
169
+
114
170
; ; Eval Exec find file in project or current dir
115
- (defun ee-find (&optional _arg )
116
- (interactive " P" )
117
- (let* ((working-directory (ee-get-project-dir-or-current-dir))
118
- (command
119
- (format " cd %s && %s > /tmp/ee-find.tmp "
120
- working-directory
121
- (ee-script-path " eee-find.sh" ))))
122
- (ee-start-process-shell-command-in-terminal " ee-find" command #'ee-find--callback )))
123
-
124
-
125
- (defun ee-lf--callback (_process )
126
- (let* ((target-file (shell-command-to-string " cat /tmp/ee-lf.tmp" ))
127
- (target-file (string-split target-file " \n " t )))
128
- (dolist (file target-file)
129
- (when (not (string-empty-p file))
130
- (message " ee-lf-opening: %s " file)
131
- (ee-find-file file)))))
132
-
133
- (defun ee-lf-in (dir )
134
- (let* ((command (ee-script-path " eee-lf.sh" ))
135
- (full-command (format " cd %s && %s " dir command)))
136
- (ee-start-process-shell-command-in-terminal
137
- " ee-lf" full-command #'ee-lf--callback )))
138
-
139
- (defun ee-lf (&optional _arg )
140
- (interactive " P" )
141
- (ee-lf-in default-directory))
171
+ (defun ee-find ()
172
+ (interactive )
173
+ (ee-run " ee-find" (ee-get-project-dir-or-current-dir)
174
+ (ee-script-path " eee-find.sh" )
175
+ nil
176
+ 'ee-jump-from ))
142
177
143
- (defun ee-lf-project (&optinoal _arg )
144
- (interactive " P" )
145
- (ee-lf-in (ee-get-project-dir-or-current-dir)))
178
+ ; ;; Eval Exec execute lazygit
179
+ (defun ee-lazygit ()
180
+ (interactive )
181
+ (ee-run " ee-lazygit" default-directory
182
+ (ee-script-path " eee-lazygit.sh" )
183
+ nil
184
+ 'ignore ))
146
185
147
- (defun ee-yazi--callback (_process )
148
- (let* ((target-file (shell-command-to-string " cat /tmp/ee-yazi.tmp" ))
149
- (target-file (string-trim target-file)))
150
- (when (not (string-empty-p target-file))
151
- (message " ee-yazi opening: %s " target-file)
152
- (ee-find-file target-file))))
153
186
154
- ; ; Eval Exec 鸭子 yazi https://github.com/sxyazi/yazi in current dir
155
- (defun ee-yazi-open (path )
156
- (let* ((yazi (ee-script-path " eee-yazi.sh" ))
157
- (full-command (concat yazi " " path)))
158
- (ee-start-process-shell-command-in-terminal
159
- " ee-yazi" full-command #'ee-yazi--callback )))
187
+ ; ;; Eval Exec execute ripgrep
188
+ (defun ee-rg ()
189
+ (interactive )
190
+ (ee-run " ee-rg" default-directory
191
+ (ee-script-path " eee-rg.sh" )
192
+ nil
193
+ 'ee-jump-from ))
194
+
195
+ (defun ee-lf ()
196
+ (interactive )
197
+ (ee-run " ee-lf" default-directory
198
+ (ee-script-path " eee-lf.sh" )
199
+ nil
200
+ 'ee-jump-from ))
160
201
202
+ (defun ee-lf-project ()
203
+ (interactive )
204
+ (ee-run " ee-lf-project" (ee-get-project-dir-or-current-dir)
205
+ (ee-script-path " eee-lf.sh" )
206
+ nil
207
+ 'ee-jump-from ))
161
208
162
209
; ; Eval Exec 鸭子 yazi in current dir
163
- (defun ee-yazi (&optional _arg )
210
+ (defun ee-yazi (&optional _arg )
164
211
(interactive " P" )
165
- (if buffer-file-name
166
- (ee-yazi-open buffer-file-name)
167
- (ee-yazi-open default-directory)))
212
+ (ee-run " ee-yazi" default-directory
213
+ (ee-script-path " eee-yazi.sh" )
214
+ (list buffer-file-name)
215
+ 'ee-jump-from ))
168
216
169
- ; ; Eval Exec 鸭子 yazi in current project dir
170
- (defun ee-yazi-project (&optional _arg )
217
+ ; ; Eval Exec 鸭子 yazi in current project root dir
218
+ (defun ee-yazi-project (&optional _arg )
171
219
(interactive " P" )
172
- (let ((project-directory (ee-get-project-dir-or-current-dir)))
173
- (if (and (equal (ee--normalize-path default-directory) project-directory)
174
- buffer-file-name)
175
- (ee-yazi-open buffer-file-name)
176
- (ee-yazi-open project-directory))))
177
-
178
-
179
- (defun ee-find-file-at-line-and-column (string )
180
- (when (string-match " \\ (.*?\\ ):\\ ([0-9]+\\ ):\\ ([0-9]+\\ ):" string)
181
- (let ((file (match-string 1 string))
182
- (line (string-to-number (match-string 2 string)))
183
- (column (string-to-number (match-string 3 string))))
184
- (message " ee-find file: %s line: %s column: %s " file line column)
185
- (ee-find-file file)
186
- (goto-line line)
187
- (move-to-column (1- column))
188
- (recenter ))))
189
-
190
- (defun ee-rg--callback (process )
191
- (let* ((ee-rg-result (shell-command-to-string " cat /tmp/ee-rg.tmp" )))
192
- (message " ee-rg get: %s " ee-rg-result)
193
- (ee-find-file-at-line-and-column ee-rg-result)))
194
-
195
- (defun ee-rg ()
196
- (interactive )
197
- (let* ((working-directory (ee-get-project-dir-or-current-dir))
198
- (command (format " cd %s && %s > /tmp/ee-rg.tmp "
199
- working-directory
200
- (ee-script-path " eee-rg.sh" ))))
201
- (ee-start-process-shell-command-in-terminal
202
- " ee-rg" command #'ee-rg--callback )))
203
220
221
+ (defun ee-yazi-project (&optional _arg )
222
+ (interactive " P" )
223
+ (let ((project-directory (ee-get-project-dir-or-current-dir)))
224
+ (if (and (equal (ee--normalize-path default-directory) project-directory)
225
+ buffer-file-name)
226
+ (ee-yazi-open buffer-file-name)
227
+ (ee-yazi-open project-directory))))
228
+
229
+ (let ((project-directory (ee-get-project-dir-or-current-dir))
230
+ (file (if (and (equal (ee--normalize-path default-directory) project-directory)
231
+ buffer-file-name)
232
+ (ee-yazi-open buffer-file-name)
233
+ (ee-yazi-open project-directory))))
234
+ (ee-run " ee-yazi-project" project-directory
235
+ (ee-script-path " eee-yazi.sh" )
236
+ (list file)
237
+ 'ee-jump-from )))
238
+
239
+
240
+ (defmacro ee-define (name working-directory script-path &optional args callback )
241
+ " Define a command with NAME to run a script with EE-RUN.
242
+ WORKING-DIRECTORY determines the directory to run the script from.
243
+ SCRIPT-PATH is the full path to the script to run.
244
+ ARGS are optional arguments for the script.
245
+ CALLBACK is an optional callback to be called after the script runs."
246
+ `(defun ,(intern name) (&optional _arg)
247
+ (interactive " P" )
248
+ (ee-run , name
249
+ , working-directory
250
+ , script-path
251
+ , args
252
+ ', callback )))
253
+
254
+ ; ; Define your commands using the macro
255
+ (ee-define " ee-line" default-directory (ee-script-path " eee-line.sh" )
256
+ (list buffer-file-name) ee-jump-from)
257
+
258
+ (ee-define " ee-find" (ee-get-project-dir-or-current-dir) (ee-script-path " eee-find.sh" ) nil ee-jump-from)
259
+
260
+ (ee-define " ee-lazygit" default-directory (ee-script-path " eee-lazygit.sh" ) nil ignore)
261
+
262
+ (ee-define " ee-rg" default-directory (ee-script-path " eee-rg.sh" ) nil ee-jump-from)
263
+
264
+ (ee-define " ee-rga" default-directory (ee-script-path " eee-rga.sh" ) (list buffer-file-name) ee-jump-from)
265
+
266
+ (ee-define " ee-lf" default-directory (ee-script-path " eee-lf.sh" ) nil ee-jump-from)
267
+
268
+ (ee-define " ee-lf-project" (ee-get-project-dir-or-current-dir) (ee-script-path " eee-lf.sh" ) nil ee-jump-from)
269
+
270
+ ; ; ";" is important commmand should be "htop;" since, ee-run will redirect stdout to ee-process-output-file
271
+ ; ; if command is "htop", ee-run will redirect htop's stdout to ee-process-output-file,
272
+ ; ; then you will see nothing in launched teriminal
273
+ (ee-define " ee-htop" default-directory " htop;" nil ignore)
274
+
275
+ ; ; Commands with optional arguments
276
+ (ee-define " ee-yazi" default-directory (ee-script-path " eee-yazi.sh" ) (list buffer-file-name) ee-jump-from)
277
+
278
+ (ee-define " ee-yazi-project"
279
+ (ee-get-project-dir-or-current-dir)
280
+ (ee-script-path " eee-yazi.sh" )
281
+ (if
282
+ (and
283
+ (equal
284
+ (ee--normalize-path default-directory)
285
+ (ee-get-project-dir-or-current-dir))
286
+ buffer-file-name)
287
+ (list buffer-file-name)
288
+ (list (ee-get-project-dir-or-current-dir)))
289
+ ee-jump-from)
204
290
205
- ; ;; Eval Exec execute lazygit
206
- (defun ee-lazygit ()
207
- (interactive )
208
- (let* ((command (ee-script-path " eee-lazygit.sh" )))
209
- (ee-start-process-shell-command-in-terminal " ee-lazygit" command #'ignore )))
210
-
211
- (defun ee-line--callback (process )
212
- (let* ((target-file (shell-command-to-string " cat /tmp/ee-line.tmp" ))
213
- (target-file (string-trim target-file)))
214
- (when (not (string-empty-p target-file))
215
- (message " ee-line opening: %s " target-file)
216
- (ee-find-file-at-line-and-column target-file))))
217
291
218
- ; ;; Eval Exec search line
219
- (defun ee-line ()
220
- (interactive )
221
- (let* ((command
222
- (format " %s %s " (ee-script-path " eee-line.sh" )
223
- (buffer-file-name ))))
224
- (ee-start-process-shell-command-in-terminal " ee-line" command #'ee-line--callback )))
225
292
226
293
(provide 'eee )
0 commit comments