From 620c12eaa78237d6c0482e3b0dc68dc353800d14 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Thu, 2 Apr 2026 14:04:04 -0400 Subject: [PATCH] Add auto-formatting for d2. --- .../emacs/files/emacs/elisp/base-functions.el | 23 +++++++++++++++++++ .../roles/emacs/files/emacs/elisp/lang-d2.el | 4 ++-- nix/configuration/roles/sway/default.nix | 3 +++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/nix/configuration/roles/emacs/files/emacs/elisp/base-functions.el b/nix/configuration/roles/emacs/files/emacs/elisp/base-functions.el index 7859e830..150441ae 100644 --- a/nix/configuration/roles/emacs/files/emacs/elisp/base-functions.el +++ b/nix/configuration/roles/emacs/files/emacs/elisp/base-functions.el @@ -27,6 +27,29 @@ ) ) +(defun run-command-on-buffer-require-output (cmd &rest args) + "Run a command using the current buffer as stdin and replacing its contents if the command succeeds with the stdout from the command. This is useful for code formatters. This version only replaces the buffer contents if the command output some text." + (let ( + (stdout-buffer (generate-new-buffer "tmp-stdout" t)) + (full-cmd (append '(call-process-region nil nil cmd nil stdout-buffer nil) args)) + ) + (unwind-protect + (let ((exit-status (eval full-cmd))) + (if (eq exit-status 0) + (if (> (buffer-size stdout-buffer) 0) + (save-excursion + (replace-buffer-contents stdout-buffer) + ) + (message "No output from command on buffer %s" (append (list cmd) args)) + ) + (message "FAILED running command on buffer %s" (append (list cmd) args)) + ) + ) + (kill-buffer stdout-buffer) + ) + ) + ) + (defun run-command-in-directory (dir cmd &rest args) "Run a command in the specified directory. If the directory is nil, the directory of the file is used. The stdout result is trimmed of whitespace and returned." (let ( diff --git a/nix/configuration/roles/emacs/files/emacs/elisp/lang-d2.el b/nix/configuration/roles/emacs/files/emacs/elisp/lang-d2.el index a4b26966..07ada725 100644 --- a/nix/configuration/roles/emacs/files/emacs/elisp/lang-d2.el +++ b/nix/configuration/roles/emacs/files/emacs/elisp/lang-d2.el @@ -1,14 +1,14 @@ (defun d2-format-buffer () "Run prettier." (interactive) - (run-command-on-buffer "d2" "fmt" "-") + (run-command-on-buffer-require-output "d2" "fmt" "-") ) (use-package d2-mode :commands (d2-mode) :hook ( (d2-mode . (lambda () - ;; (add-hook 'before-save-hook 'd2-format-buffer nil 'local) + (add-hook 'before-save-hook 'd2-format-buffer nil 'local) )) ) ) diff --git a/nix/configuration/roles/sway/default.nix b/nix/configuration/roles/sway/default.nix index 10e0cfc3..f73ed844 100644 --- a/nix/configuration/roles/sway/default.nix +++ b/nix/configuration/roles/sway/default.nix @@ -94,6 +94,9 @@ let output 'Unknown 0x095F 0x00000000' scale 1.5 output 'BOE 0x095F Unknown' scale 1.5 output 'BOE 0x0BCA Unknown' scale 1.5 + + # Breaks screen sharing: + # output 'Dell Inc. DELL U3014 P1V6N35M329L' render_bit_depth 10 ''; }; window-management = pkgs.writeTextFile {