Skip to content

Commit

Permalink
feat: enable compile-on-save for diagram tools (#353)
Browse files Browse the repository at this point in the history
* docs: add isearch-forward-symbol-at-point keybinding

* feat: prevent compilation buffer to open during recompile on save

* feat: enable compile-on-save-mode for d2 and mermaid

* feat: display compile buffer on error
  • Loading branch information
terlar committed Jun 24, 2024
1 parent 6e2a13e commit 2a3212f
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions init.org
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@ keybindings in case I would forget them.
| =C-S-backspace= | ~kill-whole-line~ | Kill entire lines, can be used to move several lines at once |
| =M-/= | ~dabbrev-expand~ | Abbreviation completion |
| =M-tab= / =C-[ C-i= | ~completion-at-point~ | More context aware completion |
| =M-s .= | ~isearch-forward-symbol-at-point~ | Search for the symbol at point |
| =C-w= | ~kill-region~ | Cut |
| =M-w= | ~kill-ring-save~ | Copy |
| =C-y= | ~yank~ | Paste |
Expand Down Expand Up @@ -2082,11 +2083,10 @@ Kill compilation process before stating another and save all buffers on ~compile
(compilation-find-buffer)
:preface
(defun compile-on-save-start ()
(let ((main-buffer (current-buffer))
(compile-buffer (compilation-find-buffer)))
(let ((compile-buffer (compilation-find-buffer)))
(unless (get-buffer-process compile-buffer)
(recompile)
(switch-to-buffer-other-window main-buffer))))
(let ((display-buffer-alist '(("^*compilation*" . (display-buffer-no-window)))))
(recompile)))))

(define-minor-mode compile-on-save-mode
"Minor mode to automatically call `recompile' whenever the
Expand All @@ -2097,6 +2097,22 @@ nothing happens."
(progn (make-local-variable 'after-save-hook)
(add-hook 'after-save-hook 'compile-on-save-start nil t))
(kill-local-variable 'after-save-hook)))

(defun compile-finish-handle-buffer-display (buffer outstr)
"Display failed compilation buffer or burry finished ones."
(let ((compilation-window (get-buffer-window buffer)))
(cond
;; If compilation failed and compilation buffer is not visible,
((and (not (string-match "finished" outstr))
(not compilation-window))
;; display that buffer.
(display-buffer "*compilation*"))
;; If compilation succeeded and compilation buffer is visible,
((and (string-match "finished" outstr)
compilation-window)
;; bury that buffer.
(with-selected-window compilation-window
(bury-buffer))))))
:general
(:keymaps
'global
Expand All @@ -2110,6 +2126,8 @@ nothing happens."
(setq compilation-ask-about-save nil)
(setq compilation-scroll-output t)

(cl-pushnew #'compile-finish-handle-buffer-display compilation-finish-functions :test #'equal)

(put 'compile-command 'safe-local-variable 'stringp))

(make-variable-buffer-local 'compile-command)
Expand Down Expand Up @@ -4494,6 +4512,7 @@ Packages that I am currently testing or evaluating.
" ")))
:hook
(d2-mode-hook . d2-mode-set-compile-command)
(d2-mode-hook . compile-on-save-mode)
:init
(setq d2-flags '("--layout" "elk" "--sketch" "--theme" "1"))
:config
Expand Down Expand Up @@ -4579,6 +4598,7 @@ Packages that I am currently testing or evaluating.
mermaid-flags)))
:hook
(mermaid-mode-hook . mermaid-mode-set-compile-command)
(mermaid-mode-hook . compile-on-save-mode)
:init
(setq mermaid-flags "--backgroundColor transparent")
:config
Expand Down

0 comments on commit 2a3212f

Please sign in to comment.