From f3d50d1b2aea4f1eda436c54a15cc18596f2df0b Mon Sep 17 00:00:00 2001 From: Craig Jennings Date: Sun, 3 May 2026 20:10:40 -0500 Subject: fix: use buffer-file-name for C single-file compile command The fallback compile command in `cj/c-compile-command` was building paths from `(buffer-name)`. That broke for renamed buffers, uniquified names like `foo.c<2>`, and files outside `default-directory`. The buffer name is a display label, not a path, so `gcc -o name name` would compile (or fail to compile) the wrong target whenever the two diverged. I extracted `cj/c--single-file-compile-command` that takes the source path explicitly, shell-quotes both source and output paths, and signals a clear `user-error` for non-file buffers. The fallback now passes `buffer-file-name` instead of `(buffer-name)`. Tests for this helper landed in commit f619cbf alongside other prog-c coverage work. --- modules/prog-c.el | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/modules/prog-c.el b/modules/prog-c.el index df32b76a..dbc11899 100644 --- a/modules/prog-c.el +++ b/modules/prog-c.el @@ -100,6 +100,16 @@ ;; -------------------------------- Compilation -------------------------------- ;; Smart compilation with project detection +(defun cj/c--single-file-compile-command (file) + "Return a GCC compile command for C source FILE. +The output binary is FILE without its extension. Both paths are shell +quoted so spaces and shell metacharacters in filenames are handled." + (unless file + (user-error "C compile: buffer is not visiting a file")) + (format "gcc -Wall -Wextra -g -o %s %s" + (shell-quote-argument (file-name-sans-extension file)) + (shell-quote-argument file))) + (defun cj/c-compile-command () "Set buffer-local compile command based on project structure." (let* ((makefile (locate-dominating-file default-directory "Makefile")) @@ -114,9 +124,7 @@ (t ;; Single file compilation (setq-local compile-command - (format "gcc -Wall -Wextra -g -o %s %s" - (file-name-sans-extension (buffer-name)) - (buffer-name))))))) + (cj/c--single-file-compile-command buffer-file-name)))))) (add-hook 'c-mode-hook 'cj/c-compile-command) (add-hook 'c-ts-mode-hook 'cj/c-compile-command) -- cgit v1.2.3