Formats cmake file only if a project-wide configuration file is found

Otherwise do nothing.
This commit is contained in:
2024-12-18 10:31:23 +01:00
parent 4f4d5a4109
commit d9d4ff91e3

56
.emacs
View File

@@ -473,33 +473,41 @@
(defun atu/cmake-format-buffer ()
"Formats the current buffer using cmake-format"
(interactive)
(let ((error-buffer (atu/create-or-erase-buffer "*cmake-format:stderr*")))
(let (
(error-buffer (atu/create-or-erase-buffer "*cmake-format:stderr*"))
(cmake-format-config-file-exists
(file-exists-p (projectile-expand-root ".cmake-format.yaml"))
)
)
(setq-local atu/after-save-message nil)
(if (eq 0 (call-process-region
;;region
(point-min)
(point-max)
"cmake-format"
nil ;do not delete buffer
;; discard stdout copy stderr to buffer
error-buffer
nil ;dont-redisplay buffer
"-"
))
(let ((current-point (point)))
(call-process-region
(point-min) (point-max)
"cmake-format"
t t t; replace current buffer and redisplay it
"--log-level=error" "-"
(if cmake-format-config-file-exists
(if (eq 0 (call-process-region
;;region
(point-min)
(point-max)
"cmake-format"
nil ;do not delete buffer
;; discard stdout copy stderr to buffer
error-buffer
nil ;dont-redisplay buffer
"-"
))
(let ((current-point (point)))
(call-process-region
(point-min) (point-max)
"cmake-format"
t t t; replace current buffer and redisplay it
"--log-level=error" "-"
)
(goto-char (+ (point-min) current-point))
nil
)
(goto-char (+ (point-min) current-point))
nil
(message
"cmake-format: could not format %s see buffer %s for details"
(buffer-name) error-buffer)
(current-message)
)
(message
"cmake-format: could not format %s see buffer %s for details"
(buffer-name) error-buffer)
(current-message)
(message "cmake-format: skipping as no configuration file found")
)
)
)