reddit / emacs

compile and run codes in emacs

anupam3bittu · 2022-10-30 08:53
Hi, I use `doom-emacs` with modules enabled for several languages. I have defined several functions to compile and run code in these languages and bound them all to `, "SPC"` where `,` is the localleader. I would be grateful if you could share how you compile and run code for these languages or suggest a better way to do the same. Thanks! **Python** (defun my/compile-python() (interactive) (defvar foo) (setq foo (concat "python " (buffer-file-name))) (compile foo)) To run python with arguments with saved history in a list: (defvar my/python-args-recent-list '("")) (defun my/execute-python-w-history-args () (interactive) (delete-dups my/python-args-recent-list) (let* ((args-val (consult--read my/python-args-recent-list :prompt "type args: " :sort nil :require-match nil :history 'my/python-args-recent-list )) (python-func (concat "python " buffer-file-name " " args-val))) (compile python-func))) **Java** I sometimes create a `.projectile` file in project root to recognize it as `projectile-project-root`. (defun my/compile-java () (interactive) (defvar foo) (let ((current-project-root (projectile-project-root))) (if current-project-root (let ((relative-filepath (file-relative-name buffer-file-name current-project-root))) (cd (projectile-project-root)) (setq foo (concat "javac " relative-filepath " && java " relative-filepath))) (setq foo (concat "javac " (buffer-file-name) " && java " (buffer-file-name))) )) (compile foo)) **Kotlin** very similar to java (defun my/compile-kotlin () (interactive) (defvar foo) (let ((current-project-root (projectile-project-root))) (if current-project-root (let ((relative-filepath (file-relative-name buffer-file-name current-project-root))) (let ((jar-filepath (concat (file-name-sans-extension relative-filepath) ".jar"))) (cd (projectile-project-root)) (setq foo (concat "kotlinc " relative-filepath " -include-runtime -d " jar-filepath " && java -jar " jar-filepath)))) (let ((jar-filepath (concat (file-name-sans-extension buffer-file-name) ".jar"))) (setq foo (concat "kotlinc " (buffer-file-name) " -include-runtime -d " jar-filepath " && java -jar " jar-filepath))))) (compile foo)) ​ **C or C++** (defun execute-c-program () (interactive) (defvar foo) (setq foo (concat "g++ " (buffer-file-name) " && ./a.out" )) (compile foo)) (defun my/execute-c-program-auto_warning () (interactive) (defvar foo) (setq foo (concat "g++ -std=c++11 " (buffer-file-name) " && ./a.out" )) (compile foo)) (defun execute-c-gcc () (interactive) (defvar foo) (setq foo (concat "gcc " (buffer-file-name) " && ./a.out" )) (compile foo))
arthurno1 · 2022-10-30 23:18
https://www.emacswiki.org/emacs/CompileCommand https://www.gnu.org/software/make/manual/