divinedominion · 2022-08-06 09:54
Since I've freed my Cmd key to be used as Super, I would like to build code with `s-b`.
`project-compile` does a good job, but it's interactive, and using it programmatically doesn't seem to be an option.
Unless there's some weird overrides?
Either way, I've copied and pasted the command to be non-interactive:
(defun ct/project-compile (command)
"Run `compile' non-interactively in the project root with `command'."
(let ((default-directory (project-root (project-current t)))
(compilation-buffer-name-function
(or project-compilation-buffer-name-function
compilation-buffer-name-function)))
(compile command)))
I'm then using it thus:
(defun ct/swift-build-command ()
(interactive)
(ct/project-compile "swift build"))
(define-key swift-mode-map (kbd "s-b") #'ct/swift-build-command)
(Could also use a lambda, but I don't mind the named function.)
-------------
How would you approach this?
- Not use `(compile "swift build")`, but drop down another level and use `(compilation-start "swift build")` directly?
- Locally `let`-override `compile-command` to be the command I want, e.g. `"swift build"`?
yyoncho · 2022-08-06 10:28
There is M-x recompile
[deleted] · 2022-08-06 14:05
[deleted]
eleven_cupfuls · 2022-08-06 17:50
Yes, I have also found that [invoking `compile` directly, passing the command string](https://gitlab.com/woolsweater/dotemacs.d/-/blob/75c9ba7bbb4db09fbbab35715d19628fea1372e1/modules/conjecture.el#L136) is the best way to do this for projects. (I've been developing a little project.el infrastructure of my own and applying it to [SPM]( https://gitlab.com/woolsweater/dotemacs.d/-/blob/75c9ba7bbb4db09fbbab35715d19628fea1372e1/modules/my-swift-mode.el#L213-227) and [Xcode](https://gitlab.com/woolsweater/dotemacs.d/-/blob/75c9ba7bbb4db09fbbab35715d19628fea1372e1/modules/convexed.el) projects:)
Another option that I use for single files: if you set `compilation-read-command` buffer-locally to `nil` then `compile` will just use `compile-command` without prompting first. I do this in some of my mode hooks. E.g., for a Swift scratch buffer:
(defun my-swift-mode:set-local-compile-command ()
"Set a buffer-local value for `compile-command'.
The command will compile and execute just this file/buffer's
contents without prompting."
(setq-local
compilation-read-command nil
compile-command (let ((file-name (or buffer-file-name
(make-temp-file
"untitled" nil ".swift"))))
(setq my-prog-mode:buffer-file-name file-name)
(format "xcrun swift '%s'" file-name))))
(From https://gitlab.com/woolsweater/dotemacs.d/-/blob/75c9ba7bbb4db09fbbab35715d19628fea1372e1/modules/my-swift-mode.el#L32-43)
divinedominion · 2022-08-09 09:16
Thanks for the links and explanation! Your `def-project-compile-action` macro looks cool. My potato function pales in comparison :)
I guess I'll get to custom modes eventually if I stick to programming in Emacs for projects for long enough. I do most of my day-to-day stuff in Xcode, so there's not a very good incentive to invest in this.
eleven_cupfuls · 2022-08-11 22:17
Thanks! Yes, I can't deny that it's a big time investment to get everything just right :D