From 1ca28f229eb360498b93bd048ef745f32f7761e1 Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Mon, 25 May 2026 18:05:55 -0500 Subject: refactor(prog): run JSON/YAML/webdev formatters via argv, not a shell MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit cj/json-format-buffer, cj/yaml-format-buffer, and cj/webdev-format-buffer ran their formatters through shell-command-on-region, which goes via a shell. I moved each to call-process-region with an explicit program and argv list, so a filename or buffer content can't be word-split or read as shell syntax. The webdev path dropped its shell-quote-argument dance once the filename became a plain argv element. Point preservation is unchanged. One deliberate improvement, and it's tested: shell-command-on-region with replace replaced the buffer with the formatter's error text on a non-zero exit. The new per-formatter helper captures output to a temp buffer, checks the exit code, replaces only on success, and otherwise raises a user-error carrying stderr — so a failed format leaves the buffer alone. I kept a small format-region helper in each of the three modules rather than one shared helper. They have no common module to live in short of system-lib, and coupling three unrelated domain modules through it wasn't worth saving sixteen lines. --- modules/prog-webdev.el | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) (limited to 'modules/prog-webdev.el') diff --git a/modules/prog-webdev.el b/modules/prog-webdev.el index 74cff732..8832446a 100644 --- a/modules/prog-webdev.el +++ b/modules/prog-webdev.el @@ -75,21 +75,45 @@ Install with: sudo pacman -S prettier") (executable-find ts-language-server-path)) (lsp-deferred))) -(defun cj/--webdev-format-command (file) - "Return the prettier command that formats FILE's contents on stdin." - (format "prettier --stdin-filepath %s" (shell-quote-argument file))) +(defun cj/--webdev-format-args (file) + "Return the prettier argv list that formats FILE's contents on stdin. +No shell quoting is needed: the args are passed to prettier directly +via `call-process-region', so FILE can contain spaces or shell +metacharacters without risk." + (list "--stdin-filepath" file)) + +(defun cj/--webdev-format-region (program &rest args) + "Replace the buffer with PROGRAM ARGS run over its contents, via argv. +Runs PROGRAM (with ARGS) on the whole buffer through +`call-process-region' — no shell, so no quoting or word-splitting. +The buffer is replaced only when PROGRAM exits zero; on a non-zero +exit the buffer is left untouched and an error is signalled with +the program's stderr text. Point is preserved as closely as the +reformatted size allows. Returns t on success." + (let* ((point (point)) + (src (current-buffer)) + (out (generate-new-buffer " *webdev-format-out*")) + (status (apply #'call-process-region + (point-min) (point-max) program + nil out nil args))) + (unwind-protect + (if (and (integerp status) (zerop status)) + (progn + (with-current-buffer src + (replace-buffer-contents out) + (goto-char (min point (point-max)))) + t) + (user-error "%s failed: %s" program + (string-trim (with-current-buffer out (buffer-string))))) + (kill-buffer out)))) (defun cj/webdev-format-buffer () "Format the current buffer with prettier. Detects the file type automatically from the filename." (interactive) (if (executable-find prettier-path) - (let ((point (point))) - (shell-command-on-region (point-min) (point-max) - (cj/--webdev-format-command - (or buffer-file-name "file.ts")) - nil t) - (goto-char (min point (point-max)))) + (apply #'cj/--webdev-format-region prettier-path + (cj/--webdev-format-args (or buffer-file-name "file.ts"))) (user-error "prettier not found; install with: sudo pacman -S prettier"))) (defun cj/webdev-keybindings () -- cgit v1.2.3