[org] WIP async tangle code

This commit is contained in:
contrapunctus 2021-12-04 11:45:51 +05:30
parent 5297415cf3
commit eaafa81426
1 changed files with 49 additions and 11 deletions

View File

@ -3583,20 +3583,58 @@ Create advice for =lispy-pair= - if =lispy--in-string-or-comment-p= is true, sel
:commands explain-pause-mode
:init (explain-pause-mode))
#+END_SRC
*** async-tangle
*** WIP async-tangle
Adapted from https://stackoverflow.com/questions/16815598/run-commands-in-emacs-asynchronously-but-display-output-incrementally/16816575#16816575 and the Elisp manual
#+BEGIN_SRC emacs-lisp
(defun my-start-process* (buffer &rest command-specs)
"Execute COMMAND-SPECS sequentially.
All COMMAND-SPECS should be a list in the form
\(NAME COMMAND COMMAND-ARGS*\)"
(with-current-buffer buffer
(set (make-local-variable 'commands-list) command-specs)
(boon-mode)
(my-start-next-process)))
(defun my-start-next-process ()
"Run the first command in the list."
(if (null commands-list)
(insert "\nDone.")
(-let* [(command-spec (car commands-list))
((name command . command-args) command-spec)]
(setq commands-list (cdr commands-list))
(insert (format ">>> %s\n" command))
(let ((process (funcall #'start-process name (current-buffer) command command-args)))
(set-process-sentinel process 'my-sentinel)))))
(defun my-sentinel (process event)
"After a process exited, call `my-start-next-process' again"
(let ((buffer (process-buffer process)))
(when (buffer-live-p buffer)
(with-current-buffer buffer
(let ((moving (= (point) (process-mark process))))
(save-excursion
;; Insert the text, advancing the process marker.
(goto-char (process-mark process))
;; (insert (format "Command `%s' %s" process event))
(set-marker (process-mark process) (point))
(my-start-next-process))
(when moving (goto-char (process-mark process))))))))
#+END_SRC
#+BEGIN_SRC emacs-lisp
(defun contrapunctus-async-tangle (&optional prefix)
"Use `org-babel-tangle' on the file visited by the current buffer."
(interactive "P")
(let ((proc-buffer (get-buffer-create "*async-tangle-process*"))
(file-name (buffer-file-name))
(file-name-no-ext (file-name-sans-extension
(buffer-file-name)))
(old-win (selected-window)))
(start-process
"async-tangle" proc-buffer "emacs" "-q" "-Q" "--batch"
"--eval=(require 'ob-tangle)"
(format "--eval=(org-babel-tangle-file \"%s\")"
file-name file-name-no-ext))
(let* ((proc-buffer (get-buffer-create "*async-tangle-process*"))
(file-name (buffer-file-name))
(file-name-no-ext (file-name-sans-extension (buffer-file-name)))
(old-win (selected-window)) ; ?
(process (start-process
"async-tangle" proc-buffer "emacs" "-q" "-Q" "--batch"
"--eval=(require 'ob-tangle)"
(format "--eval=(org-babel-tangle-file \"%s\")"
file-name file-name-no-ext))))
;; don't create window if buffer already visible
(unless (get-buffer-window-list proc-buffer)
;; to avoid messing up my usual two-windows-same-buffer setup