|
25 | 25 | ;;; Code:
|
26 | 26 |
|
27 | 27 | (require 'ansi-color) ;; built-in
|
| 28 | +(require 'seq) |
28 | 29 |
|
29 | 30 | ;; --------------------------------------------------------------------------
|
30 | 31 | ;; Customization
|
|
53 | 54 | :type '(repeat directory)
|
54 | 55 | :group 'sdml)
|
55 | 56 |
|
| 57 | +(defcustom sdml-mode-cli-no-color nil |
| 58 | + "Suppress color output from the command line tool." |
| 59 | + :tag "Suppress color" |
| 60 | + :type 'boolean |
| 61 | + :group 'sdml) |
| 62 | + |
56 | 63 | ;; --------------------------------------------------------------------------
|
57 | 64 | ;; Section heading
|
58 | 65 | ;; --------------------------------------------------------------------------
|
|
61 | 68 |
|
62 | 69 | (defconst sdml-mode-cli-default-error-buffer-name "*SDML Command-Line Errors*")
|
63 | 70 |
|
64 |
| -(defun sdml-mode-cli-make-arg (plist-args key &optional arg-name ignore-me) |
65 |
| - "Make a CLI argument with KEY from PLIST-ARGS." |
66 |
| - (when (not ignore-me) |
67 |
| - (let ((value (plist-get plist-args key)) |
68 |
| - (arg-name (if arg-name |
69 |
| - arg-name |
70 |
| - (substring (symbol-name key) 1)))) |
71 |
| - (if (null value) "" (format "--%s %s" arg-name value))))) |
72 |
| - |
73 |
| -(defun sdml-mode-cli-make-command (command &rest plist-args) |
74 |
| - "Make an sdml COMMAND with additional PLIST-ARGS." |
75 |
| - (interactive) |
76 |
| - (when (derived-mode-p 'sdml-mode) |
| 71 | +(defun sdml-mode-cli-make-arg (name value) |
| 72 | + "Make an argument string from NAME and VALUE." |
| 73 | + (format "--%s %s" name value)) |
| 74 | + |
| 75 | +(defun sdml-mode-cli-make-command (command &rest args) |
| 76 | + "Make an sdml COMMAND with additional ARGS." |
| 77 | + (when (or t (derived-mode-p 'sdml-mode)) |
77 | 78 | (let* ((cli-name (or sdml-mode-cli-name "sdml"))
|
78 | 79 | (cmd-name (executable-find cli-name))
|
79 |
| - (file-name (buffer-file-name (current-buffer)))) |
| 80 | + (pre-args (list |
| 81 | + cmd-name |
| 82 | + (sdml-mode-cli-make-arg 'log-filter sdml-mode-cli-log-filter) |
| 83 | + (if sdml-mode-cli-no-color "--no-color" nil) |
| 84 | + command)) |
| 85 | + (args (mapcar (lambda (arg) (cond |
| 86 | + ((eq arg 'current-buffer) |
| 87 | + (sdml-mode-cli-make-arg 'input (buffer-file-name))) |
| 88 | + (t arg))) |
| 89 | + args))) |
80 | 90 | (cond
|
81 | 91 | ((null cmd-name)
|
82 | 92 | (message "couldn't find the sdml cli: %s" cli-name))
|
83 |
| - ((null file-name) |
84 |
| - (message "buffer doesn't have a file name")) |
85 | 93 | (t
|
86 |
| - (string-join |
87 |
| - (list cmd-name |
88 |
| - (sdml-mode-cli-make-arg plist-args :log-filter) |
89 |
| - command |
90 |
| - (sdml-mode-cli-make-arg plist-args :validation-level "level" (not (string= command "validate"))) |
91 |
| - (sdml-mode-cli-make-arg plist-args :depth nil (not (string= command "deps"))) |
92 |
| - (sdml-mode-cli-make-arg plist-args :output-format) |
93 |
| - (sdml-mode-cli-make-arg plist-args :output-file) |
94 |
| - (or (sdml-mode-cli-make-arg plist-args :input-file) |
95 |
| - (let ((arg-value (plist-get plist-args :input-module))) |
96 |
| - (if (null arg-value) "" arg-value)))) |
97 |
| - " ")))))) |
98 |
| - |
99 |
| - |
100 |
| -(defun sdml-mode-cli-run-command (command &optional output-buffer-name error-buffer-name) |
| 94 | + (string-join (append pre-args args) " ")))))) |
| 95 | + |
| 96 | +(defun sdml-mode-cli--make-refresh-cmd (cmd env out err) |
| 97 | + "Return a lambda to refresh the output buffer from a command. |
| 98 | +CMD is the command-line to run, ENV is the environment variables |
| 99 | +to add, OUT is the output buffer and ERR the error buffer." |
| 100 | + (lambda () |
| 101 | + (interactive) |
| 102 | + (setq buffer-read-only nil) |
| 103 | + (delete-region (point-min) (point-max)) |
| 104 | + (with-environment-variables (("SDML_PATH" env)) |
| 105 | + (shell-command cmd out err) |
| 106 | + ;; colorize output |
| 107 | + (ansi-color-apply-on-region (point-min) (point-max)) |
| 108 | + (setq buffer-read-only t)))) |
| 109 | + |
| 110 | +(defun sdml-mode-cli-run-command (command &optional output-buffer-name error-buffer-name refresh-fn) |
101 | 111 | "Run COMMAND with output to OUTPUT-BUFFER-NAME and ERROR-BUFFER-NAME.
|
102 | 112 |
|
103 |
| -If not specified `output-buffer-name' is set to |
104 |
| -`sdml-cli-default-output-buffer-name' and `error-buffer-name' is set |
105 |
| -to `sdml-cli-default-error-buffer-name'." |
106 |
| - (let ((output-buffer-name (or output-buffer-name sdml-mode-cli-default-output-buffer-name)) |
107 |
| - (current-load-path (or (getenv "SDML_PATH") ""))) |
108 |
| - (with-environment-variables (("SDML_PATH" (concat current-load-path |
109 |
| - (string-join sdml-mode-cli-load-path ":")))) |
110 |
| - (with-output-to-temp-buffer output-buffer-name |
111 |
| - (shell-command command |
112 |
| - output-buffer-name |
113 |
| - (or error-buffer-name sdml-mode-cli-default-error-buffer-name)) |
114 |
| - (pop-to-buffer output-buffer-name) |
115 |
| - ;; colorize output |
116 |
| - (ansi-color-apply-on-region (point-min) (point-max)) |
117 |
| - ;; make read-only |
118 |
| - (special-mode))))) |
| 113 | +If not specified OUTPUT-BUFFER-NAME is set to |
| 114 | +`sdml-cli-default-output-buffer-name' and ERROR-BUFFER-NAME is set |
| 115 | +to `sdml-cli-default-error-buffer-name'. |
119 | 116 |
|
| 117 | +The boolean REFRESH-FN indicates that a refresh function should |
| 118 | +be added to the buffer with a key binding to \"g\"." |
| 119 | + (let ((output-buffer-name (or output-buffer-name sdml-mode-cli-default-output-buffer-name)) |
| 120 | + (load-path (concat (or (getenv "SDML_PATH") "") |
| 121 | + (string-join sdml-mode-cli-load-path ":")))) |
| 122 | + (with-environment-variables (("SDML_PATH" load-path)) |
| 123 | + (shell-command command |
| 124 | + output-buffer-name |
| 125 | + (or error-buffer-name sdml-mode-cli-default-error-buffer-name)) |
| 126 | + (pop-to-buffer output-buffer-name) |
| 127 | + ;; colorize output |
| 128 | + (ansi-color-apply-on-region (point-min) (point-max)) |
| 129 | + ;; make read-only |
| 130 | + (special-mode) |
| 131 | + (when refresh-fn |
| 132 | + ;; install refresh command |
| 133 | + (use-local-map (copy-keymap special-mode-map)) |
| 134 | + (local-set-key "g" (sdml-mode-cli--make-refresh-cmd command |
| 135 | + load-path |
| 136 | + output-buffer-name |
| 137 | + error-buffer-name)))))) |
120 | 138 |
|
121 | 139 | (provide 'sdml-mode-cli)
|
122 | 140 |
|
|
0 commit comments