Skip to content

Commit 6113fcd

Browse files
committed
add ee-define macro
Signed-off-by: Eval EXEC <[email protected]>
1 parent b4c256a commit 6113fcd

File tree

4 files changed

+206
-125
lines changed

4 files changed

+206
-125
lines changed

eee-lf.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env bash
22

33
cat /dev/null >/tmp/ee-lf.tmp
4-
lf -print-selection >/tmp/ee-lf.tmp
5-
cat /tmp/ee-lf.tmp
4+
lf -print-selection

eee-line.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ fzf --ansi --disabled --query "$INITIAL_QUERY" \
1717
--delimiter : \
1818
--preview "bat --color=always ${QUERY_FILE} --highlight-line {1}" \
1919
--preview-window 'up,60%,border-bottom,+{1}+3/3,~3' \
20-
--layout=reverse-list | xargs -0 -I{} echo "${QUERY_FILE}":{} > /tmp/ee-line.tmp
20+
--layout=reverse-list | xargs -0 -I{} echo "${QUERY_FILE}":{}

eee-rga.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
3+
CURR_DIR=$(dirname $(readlink -f $0))
4+
. ${CURR_DIR}/eee-common.sh
5+
6+
QUERY_FILE="$1"
7+
8+
RG_PREFIX="rga --no-heading"
9+
local file
10+
FZF_DEFAULT_COMMAND="$RG_PREFIX '' '$QUERY_FILE'" \
11+
fzf --no-sort --preview="[[ ! -z {} ]] && rga --pretty --context 5 {q} \"${QUERY_FILE}\" | grep :{1}: " \
12+
--delimiter : \
13+
--border \
14+
--bind "change:reload:$RG_PREFIX {q} \"${QUERY_FILE}\" " \
15+
--preview-window="60%:wrap"

eee.el

+189-122
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,24 @@ The terminal emulator is specified in `ee-terminal-command'.
5050
See `ee-start-terminal-function' for the usage.
5151
"
5252
(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)))
5858
(set-process-sentinel
5959
proc
6060
(lambda (p _m)
6161
(unless (process-live-p p)
62-
(funcall callback p))))
62+
(funcall callback p))))
6363
proc))
6464

6565
(defcustom ee-start-terminal-function #'ee-start-external-terminal
6666
"Function used to start the terminal.
6767
See `ee-start-external-terminal' for function signature."
6868
: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")))
7171

7272
(defun ee-start-process-shell-command-in-terminal (name command callback)
7373
"Run COMMAND in a terminal.
@@ -84,143 +84,210 @@ NAME is passed to `ee-start-terminal-function'."
8484
'(("find-file" . find-file)
8585
("browse-url" . browse-url)))
8686

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)))))
9687

9788
(defun ee--normalize-path (path)
9889
(string-trim-right path (rx (or "\n" "\\" "/"))))
9990

10091
(defun ee-get-project-dir-or-current-dir()
10192
(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"
10394
default-directory)))
10495
(ee--normalize-path (shell-command-to-string project-dir-command))))
10596

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))))
11297

11398

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+
114170
;; 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))
142177

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))
146185

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))))
153186

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))
160201

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))
161208

162209
;; Eval Exec 鸭子 yazi in current dir
163-
(defun ee-yazi (&optional _arg)
210+
(defun ee-yazi(&optional _arg)
164211
(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))
168216

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)
171219
(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)))
203220

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)
204290

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))))
217291

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)))
225292

226293
(provide 'eee)

0 commit comments

Comments
 (0)