Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add watch support #9

Open
andorsk opened this issue Nov 22, 2022 · 5 comments
Open

Add watch support #9

andorsk opened this issue Nov 22, 2022 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@andorsk
Copy link
Owner

andorsk commented Nov 22, 2022

add watch support for d2 diagrams. spins up server which you can go to the browser for

@andorsk andorsk added enhancement New feature or request help wanted Extra attention is needed and removed help wanted Extra attention is needed labels Nov 22, 2022
@andorsk andorsk self-assigned this Nov 28, 2022
@andorsk andorsk added this to the Alpha-0.1 milestone Nov 28, 2022
@andorsk andorsk linked a pull request Nov 28, 2022 that will close this issue
@andorsk
Copy link
Owner Author

andorsk commented Nov 29, 2022

Realized that while watch mode would be cool, it's not required for alpha. more important is just fixing the svg issue.

@shelper
Copy link

shelper commented Dec 5, 2022

do you mean real time recompile and refeshing if the content of the source file chagnes by this watch mode?
that would be awesome.

@andorsk
Copy link
Owner Author

andorsk commented Dec 5, 2022

yea....that was the idea. d2 already supports some server implementation, but I would need to write a bit to manage those processes. it's definitely a cool idea, but could easily get messy if handled incorrectly.

@baturkey
Copy link

I was able to get watch functionality working by adding a function to after-save-hook that runs d2

(use-package d2-mode
  :init
  (add-to-list 'auto-mode-alist '("\\.d2\\'" . d2-mode))
  :config
  (defun d2-update ()
    (if (string-suffix-p ".d2" (buffer-name))
	(start-process "d2-png" "*d2-png*" "d2" (buffer-name) (concat (substring (buffer-name) 0 -3)) ".png")))
  :hook
  (after-save . d2-update))

And then turning on auto-revert-mode for images

(use-package image-mode
  :config
  (defun image-mode-hook ()
    (auto-revert-mode))
  :hook
  (image-mode . image-mode-hook))

@terlar
Copy link
Contributor

terlar commented Jul 29, 2024

I did a similar approach with auto-revert-mode but instead went via the compile-command and then a generic mode to auto-recompile.

(defun compile-on-save-start ()
    (let ((compile-buffer (compilation-find-buffer)))
      (unless (get-buffer-process compile-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
current buffer is saved. When there is ongoing compilation,
nothing happens."
    :lighter " CoS"
    (if compile-on-save-mode
        (progn  (make-local-variable 'after-save-hook)
                (add-hook 'after-save-hook 'compile-on-save-start nil t))
      (remove-hook 'after-save-hook 'compile-on-save-start t)))

(defun d2-mode-set-compile-command ()
    "Configure compile command for d2-mode."
    (set (make-local-variable 'compile-command)
         (mapconcat #'shell-quote-argument (append (list d2-location buffer-file-name) d2-flags)
                    " ")))

(add-hook 'd2-mode-hook '#d2-mode-set-compile-command)
(add-hook 'd2-mode-hook '#compile-on-save-mode)
(add-hook 'image-mode-hook '#auto-revert-mode)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants