]> git.armaanb.net Git - config.org.git/commitdiff
Add java-eval-buffer
authorArmaan Bhojwani <me@armaanb.net>
Tue, 18 May 2021 03:00:15 +0000 (23:00 -0400)
committerArmaan Bhojwani <me@armaanb.net>
Tue, 18 May 2021 03:00:15 +0000 (23:00 -0400)
config.org

index e109b44cea660cb3dad1b8a92bbbd145eec96199..ba34383800d04504f0645885b967573502416ea0 100644 (file)
@@ -547,6 +547,25 @@ Use Emacs globally. Use the Emacs daemon and bind a key in your wm to =emacsclie
     :straight (git-email :repo "https://git.sr.ht/~yoctocell/git-email")
     :config (git-email-piem-mode))
 #+end_src
+** Java
+*** Evaluate current buffer
+Stolen from https://stackoverflow.com/questions/19953924/how-do-you-run-java-codes-in-emacs
+#+begin_src emacs-lisp
+  (defun java-eval-buffer ()
+    "run current program (that requires no input)"
+    (interactive)
+    (let* ((source (file-name-nondirectory buffer-file-name))
+           (out    (file-name-sans-extension source))
+           (class  (concat out ".class")))
+      (save-buffer)
+      (shell-command (format "rm -f %s && javac %s" class source))
+      (if (file-exists-p class)
+          (shell-command (format "java %s" out) "*scratch*")
+        (progn
+          (set (make-local-variable 'compile-command)
+               (format "javac %s" source))
+          (command-execute 'compile)))))
+#+end_src
 * General text editing
 ** Indentation
 Indent after every change.