Skip to content

Commit

Permalink
refactor: add read file
Browse files Browse the repository at this point in the history
Signed-off-by: Shuguang Sun <[email protected]>
  • Loading branch information
ShuguangSun committed Jun 26, 2021
1 parent 9686149 commit a5b203b
Showing 1 changed file with 26 additions and 17 deletions.
43 changes: 26 additions & 17 deletions diffpdf.el
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,29 @@
(completing-read-multiple "Select pdf file(s): "
#'read-file-name-internal nil t))

(defun diffpdf--assert (args)
"Parse transient ARGS."
(let (ret1 ret2)
;; -a, -c, -w
(setq ret1 (mapcar (lambda(x)
(if (string-match-p "--characters\\|--words" x) x))
args))
(setq ret2 (mapcar (lambda(x)
(if (string-match-p "\\`--file[12]=.+" x)
(shell-quote-argument (substring x 8))))
args))
(append ret1 ret2)))


(defun diffpdf--command (&optional args)
"Invoke the compile mode with the run command and ARGS if provided."
(interactive (list (diffpdf-arguments)))
(unless (and diffpdf-program (executable-find diffpdf-program))
(user-error "`diffpdf-program' is not defined or not in the PATH."))
(save-excursion
(let* ((arguments (string-join args " "))
(files (diffpdf--choose-files))
(m (safe-length files))
(let* ((arguments (string-join (diffpdf--assert args) " "))
command)
(when files (setq files (mapcar #'shell-quote-argument files)))
(setq command
(concat diffpdf-program " " arguments " "
(if (> m 2)
(string-join (nbutlast files (- m 2)) " ")
(string-join files " "))))
(setq command (concat diffpdf-program " " arguments))
(if diffpdf-use-compile-p
(progn
(setq compilation-read-command t)
Expand All @@ -85,7 +92,7 @@
(unless (and diffpdf-program (executable-find diffpdf-program))
(user-error "`diffpdf-program' is not defined or not in the PATH."))
(save-excursion
(let* ((arguments (string-join args " "))
(let* ((arguments (string-join (diffpdf--assert args) " "))
(files (dired-get-marked-files))
(m (safe-length files))
command)
Expand All @@ -112,13 +119,15 @@ Just for autoload."
;; Transient menus
(transient-define-prefix diffpdf-menu ()
"Open diffpdf transient menu pop up."
["Arguments"
("-a" "set the initial comparison mode to Appearance" "--appearance")
("-c" "set the initial comparison mode to Characters" "--characters")
("-w" "set the initial comparison mode to Words" "--words")]
[["Command"
("d" "run diffpdf" diffpdf--command)
("D" "run diffpdf from dired" diffpdf-dired--command)]]
["Arguments"
("-f" "File1" "--file1=" transient-read-file)
("-F" "File2" "--file2=" transient-read-file)
("-a" "set the initial comparison mode to Appearance" "--appearance")
("-c" "set the initial comparison mode to Characters" "--characters")
("-w" "set the initial comparison mode to Words" "--words")]
[["Command"
("d" "run diffpdf" diffpdf--command)
("D" "run diffpdf from dired" diffpdf-dired--command)]]
(interactive)
(transient-setup 'diffpdf-menu))

Expand Down

0 comments on commit a5b203b

Please sign in to comment.