Skip to content

Instantly share code, notes, and snippets.

@ksqsf
Last active June 6, 2018 16:19
Show Gist options
  • Save ksqsf/add01ff4b5740dba97f4e2dcc85fcb58 to your computer and use it in GitHub Desktop.
Save ksqsf/add01ff4b5740dba97f4e2dcc85fcb58 to your computer and use it in GitHub Desktop.
ACM Minor mode for Emacs
;
; ACM-Mode
;
; This is not a complete work.
; Be sure to tweak it as you like before you use this!
;
; NOTE: I won't update this Gist when I make improvements to my own ACM mode.
;
; This file belongs to Public Domain.
; Original author: ksqsf
;
(defvar *acm-file* nil)
(defvar *acm-executable* nil)
(defun acm-compile ()
"Compile the current file to an executable with the same name."
(interactive)
(setq *acm-file* (buffer-file-name))
(setq *acm-executable* (file-name-base *acm-file*))
(compile (format "g++ -o %s -Wall -ggdb -std=c++17 %s" *acm-executable* *acm-file*)))
(defun acm-execute ()
"Execute the executable, piping *ACM-Input* to it, and the output to *ACM-Output*."
(interactive)
(if (eq *acm-executable* nil)
(error "No file is compiled"))
(switch-to-buffer-other-window "*ACM-Input*")
(let ((command (format "./%s" *acm-executable*)))
(if (use-region-p)
(shell-command-on-region (region-beginning) (region-end) command "*ACM-Output*")
(shell-command-on-region (point-min) (point-max) command "*ACM-Output*")))
(switch-to-buffer-other-window "*ACM-Output*"))
(defun acm-debug ()
(interactive)
(gdb (format " -i=mi %s " *acm-executable*)))
(defun acm-switch-to-input ()
"Switch to buffer *ACM-Input*"
(interactive)
(switch-to-buffer-other-window "*ACM-Input*")
(acm-mode)) ; enable acm-mode on input file for C-c e
(defun acm-switch-to-output ()
(interactive)
(switch-to-buffer-other-window "*ACM-Output*"))
(define-minor-mode acm-mode
"ACM/ICPC minor mode. Optimized for single-file compilation and the execution and debugging of single executables. This doesn't make a general CC-mode hook or global key bindings, because you generally don't want to always write such short programs. ACM mode helps with organizing.
Key bindings:
\\{acm-mode-map}"
:lighter " ACM"
:keymap (let ((map (make-sparse-keymap)))
(define-key map (kbd "C-c c") 'acm-compile)
(define-key map (kbd "C-c d") 'acm-debug)
(define-key map (kbd "C-c e") 'acm-execute)
(define-key map (kbd "C-c s i") 'acm-switch-to-input)
(define-key map (kbd "C-c s o") 'acm-switch-to-output)
map))
(provide 'acm-mode)
@ksqsf
Copy link
Author

ksqsf commented Jun 6, 2018

Hey, friend! This little mode has got a major rewrite and is available as a separate repository. Be sure to check it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment