aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/prog-json.el29
-rw-r--r--modules/prog-webdev.el42
-rw-r--r--modules/prog-yaml.el30
3 files changed, 85 insertions, 16 deletions
diff --git a/modules/prog-json.el b/modules/prog-json.el
index 7b49dff1..953b5f79 100644
--- a/modules/prog-json.el
+++ b/modules/prog-json.el
@@ -41,15 +41,38 @@
;; -------------------------------- Formatting ---------------------------------
;; pretty-print with sorted keys, bound to standard format key
+(defun cj/--json-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 " *json-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/json-format-buffer ()
"Format the current JSON buffer with sorted keys.
Uses jq if available for reliable formatting, otherwise falls
back to the built-in `json-pretty-print-buffer-ordered'."
(interactive)
(if (executable-find "jq")
- (let ((point (point)))
- (shell-command-on-region (point-min) (point-max) "jq --sort-keys ." nil t)
- (goto-char (min point (point-max))))
+ (cj/--json-format-region "jq" "--sort-keys" ".")
(json-pretty-print-buffer-ordered)))
(defun cj/json-setup ()
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 ()
diff --git a/modules/prog-yaml.el b/modules/prog-yaml.el
index 6bb11a62..c2bb559b 100644
--- a/modules/prog-yaml.el
+++ b/modules/prog-yaml.el
@@ -36,15 +36,37 @@
;; -------------------------------- Formatting ---------------------------------
;; normalize indentation and style, bound to standard format key
+(defun cj/--yaml-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 " *yaml-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/yaml-format-buffer ()
"Format the current YAML buffer with prettier.
Preserves point position as closely as possible."
(interactive)
(if (executable-find "prettier")
- (let ((point (point)))
- (shell-command-on-region (point-min) (point-max)
- "prettier --parser yaml" nil t)
- (goto-char (min point (point-max))))
+ (cj/--yaml-format-region "prettier" "--parser" "yaml")
(user-error "prettier not found; install with: npm install -g prettier")))
(defun cj/yaml-setup ()