Skip to content

Instantly share code, notes, and snippets.

@SeriousBug
Created November 13, 2015 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SeriousBug/91c38ddde617b98ffbcb to your computer and use it in GitHub Desktop.
Save SeriousBug/91c38ddde617b98ffbcb to your computer and use it in GitHub Desktop.
Some small Emacs tools I have written.
(require 's)
(set 'compile-run-flags '("-Wall" "-Wextra" "-std=c11" "-Og" "-ggdb"))
(defun compile-run-buffer ()
"Compile and run buffer."
(interactive)
(let* ((split-file-path (split-string buffer-file-name "/"))
(file-name (car (last split-file-path)))
(file-name-noext (car (split-string file-name "[.]")))
(buffer-name (concat "compile-run: " file-name-noext))
(buffer-name* (concat "*" buffer-name "*")))
(apply (apply-partially 'make-comint buffer-name "gcc" nil "-o" file-name-noext file-name) compile-run-flags)
(switch-to-buffer-other-window buffer-name*)
(set 'buffer-read-only t)
(local-unset-key (kbd "q"))
(set-process-sentinel (get-buffer-process (current-buffer))
(apply-partially 'compile-run-compilation-sentinel file-name-noext))))
(defun compile-run-compilation-sentinel (program process event)
"Sentinel for the compilation process, runs the compiled program if compilation was successful."
(set 'buffer-read-only nil)
(if (s-suffix? "finished\n" event)
(progn
(insert "Compilation successful.\n" "Running " program "\n\n\n")
(comint-exec (current-buffer) program (concat "./" program) nil nil)
(set-process-sentinel (get-buffer-process (current-buffer))
(apply-partially 'compile-run-execution-sentinel program)))
(progn
(insert "Compilation failed!\n\n")
(set 'buffer-read-only t)
(local-set-key (kbd "q") 'quit-window))))
(defun compile-run-execution-sentinel (program process event)
"Sentinel for the executed process."
(insert "\n\nProcess " program " exited.\n\n")
(set 'buffer-read-only t)
(local-set-key (kbd "q") 'quit-window))
(defun c-astyle-buffer ()
"Clean-up the current buffer, removing broken CRLF line endings
and running the code through astyle."
(interactive)
(let* ((indent-spaces (number-to-string (cdr (assoc 'c-basic-offset (assoc-string "linux" c-style-alist)))))
(astyle-command (concat "astyle -s" indent-spaces " --style=" c-indentation-style))
(error-buffer "*c-cleanup-error*"))
(progn
(shell-command-on-region (point-min) (point-max)
astyle-command
(current-buffer) t
error-buffer t)
(message astyle-command))))
(require 's)
(defun insert-current-date ()
"Insert the current date in YYYY-MM-DD format."
(interactive)
(shell-command "date +'%Y-%m-%d'" t))
(defun dired-here ()
"Open dired in current buffer."
(interactive)
(dired (s-join "/" (butlast (split-string buffer-file-name "/")))))
(defun run-love ()
"Run a Löve project."
(interactive)
(let* ((dirlist (split-string buffer-file-name "/"))
(top-directory (car (last (butlast dirlist))))
(love-buffer (concat "run-love " top-directory)))
(make-comint love-buffer "love" nil ".")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment