Skip to content

Commit

Permalink
provide command rustic-babel-format-block
Browse files Browse the repository at this point in the history
  • Loading branch information
brotzeit committed Nov 10, 2021
1 parent 1f1450f commit 640f0df
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -495,9 +495,11 @@ Customization:
- `rustic-babel-format-src-block` format block after successful build
- `rustic-babel-display-compilation-buffer` display compilation buffer
of babel process

- `rustic-babel-auto-wrap-main` wrap body into main function

### Commands

- `rustic-babel-format-block` format block at point
- `rustic-babel-visit-project` find generated project of block at point

### Parameters
Expand Down
37 changes: 23 additions & 14 deletions rustic-babel.el
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,27 @@
:buffer err-buff
:command params
:filter #'rustic-compilation-filter
:sentinel (lambda (proc output) (rustic-babel-build-sentinel toolchain proc output)))))
:sentinel (lambda (proc output)
(rustic-babel-build-sentinel toolchain proc output)))))

(defun rustic-babel-format-block ()
"Format babel block at point."
(interactive)
(save-excursion
(let ((babel-body
(org-element-property :value (org-element-at-point)))
(proc
(make-process :name "rustic-babel-format"
:buffer "rustic-babel-format-buffer"
:command `(,rustic-rustfmt-bin
,@(rustic-compute-rustfmt-args))
:filter #'rustic-compilation-filter
:sentinel #'rustic-babel-format-sentinel)))
(while (not (process-live-p proc))
(sleep-for 0.01))
(process-send-string proc babel-body)
(process-send-eof proc)
proc)))

(defun rustic-babel-build-sentinel (toolchain proc _output)
"Sentinel for rust babel compilation process PROC.
Expand All @@ -82,21 +102,10 @@ execution with rustfmt."
(inhibit-read-only t))
(if (zerop (process-exit-status proc))
(let* ((default-directory rustic-babel-dir))

;; format babel block
(when rustic-babel-format-src-block
(let ((babel-body
(org-element-property :value (org-element-at-point)))
(proc
(make-process :name "rustic-babel-format"
:buffer "rustic-babel-format-buffer"
:command `(,rustic-rustfmt-bin
,@(rustic-compute-rustfmt-args))
:filter #'rustic-compilation-filter
:sentinel #'rustic-babel-format-sentinel)))
(while (not (process-live-p proc))
(sleep-for 0.01))
(process-send-string proc babel-body)
(process-send-eof proc)
(let ((proc (rustic-babel-format-block)))
(while (eq (process-status proc) 'run)
(sit-for 0.1))))

Expand Down

0 comments on commit 640f0df

Please sign in to comment.