Skip to content

Instantly share code, notes, and snippets.

@mattmoor
Created March 8, 2021 18:07
Show Gist options
  • Save mattmoor/b3b38b535b45f9a45ada15f41915022c to your computer and use it in GitHub Desktop.
Save mattmoor/b3b38b535b45f9a45ada15f41915022c to your computer and use it in GitHub Desktop.
(require 'package)
(add-to-list 'package-archives
'("melpa" . "http://melpa.milkbox.net/packages/") t)
(package-initialize)
; Double Ctrl-C is always comment-region
(global-set-key (kbd "C-c C-c") 'comment-region)
; Accidental hold of ctrl mappings
(global-set-key (kbd "C-x C-b") 'switch-to-buffer)
(global-set-key (kbd "C-x C-o") 'other-window)
; Unbind transpose, it is not funny.
(global-unset-key "\C-t")
; Unbind transpose, it is not funny.
(global-set-key (kbd "M-/") 'undo)
; Disable toolbars
(when window-system (menu-bar-mode -1))
(when window-system (tool-bar-mode -1))
(when window-system (scroll-bar-mode -1))
; Get rid of that super funny start screen.
(setq inhibit-startup-screen t)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Adds a TODO comment to the given source file
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(package-initialize)
(defun insert-todo ()
"Insert a TODO comment."
(interactive)
(insert comment-start "TODO(" (user-login-name) "): ")
(forward-char -1))
(global-set-key (kbd "C-t") #'insert-todo)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Enables Go build/testing integration
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'subr-x)
(defun on-gopath (&optional path)
"Are we on the GOPATH?"
(string-prefix-p (concat (getenv "GOPATH") "/src/") (or path (file-name-directory (buffer-file-name)))))
(defun current-gopath ()
"Where are we on the GOPATH?"
(string-remove-prefix (concat (getenv "GOPATH") "/src/") (file-name-directory (buffer-file-name))))
(defun golang-build ()
"Save any unsaved buffers and test"
(interactive)
(save-some-buffers t)
(compile (concat "go build " (concat (current-gopath) "..."))))
(defun golang-test ()
"Save any unsaved buffers and test"
(interactive)
(save-some-buffers t)
(compile (concat "go test -covermode=count -coverprofile=/tmp/count.out " (concat (current-gopath) "..."))))
(defun kov ()
"Show coverage for the current file"
(interactive)
(go-coverage "/tmp/count.out"))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Enables ko applying the nearest config/ directory
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun empty-string-p (string)
"Return true if the string is empty or nil. Expects string."
(or (null string)
(zerop (length string))))
(defun find-config (path)
"Save any unsaved buffers and test"
(cond
((not (on-gopath path)) nil)
((file-directory-p (concat path "config")) (concat path "config"))
(t (find-config (file-name-directory (string-remove-suffix "/" path))))
))
(defun ko-apply ()
"Save any unsaved buffers and test"
(interactive)
(save-some-buffers t)
(with-demoted-errors "Error: %s"
(compile (concat "ko apply -W -f " (find-config (file-name-directory (buffer-file-name)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Enables running update-deps or update-codegen
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun find-hack (path)
"Save any unsaved buffers and test"
(cond
((not (on-gopath path)) nil)
((file-directory-p (concat path "hack")) (concat path "hack"))
(t (find-hack (file-name-directory (string-remove-suffix "/" path))))
))
(defun update-deps ()
"Save any unsaved buffers and test"
(interactive)
(save-some-buffers t)
(with-demoted-errors "Error: %s"
(compile (concat (find-hack (file-name-directory (buffer-file-name))) "/update-deps.sh"))))
(defun update-codegen ()
"Save any unsaved buffers and test"
(interactive)
(save-some-buffers t)
(with-demoted-errors "Error: %s"
(compile (concat (find-hack (file-name-directory (buffer-file-name))) "/update-codegen.sh"))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Hook in the various build/test helpers
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun test-this ()
"Runs a test command depending on context"
(interactive)
(cond
((not (stringp (buffer-file-name)))
(message "no file name"))
((on-gopath)
(call-interactively 'golang-test))))
(defun compile-this ()
"Runs google3-build or maven-compile depending on context"
(interactive)
(cond
((not (stringp (buffer-file-name)))
(message "no file name"))
((not (empty-string-p (find-config (file-name-directory (buffer-file-name)))))
(call-interactively 'ko-apply))
((on-gopath)
(call-interactively 'golang-build))))
; Binds malabar-run-maven-command in a compile/test configuration to {C,M}-\
(global-set-key (kbd "C-\\") 'compile-this)
(global-set-key (kbd "M-\\") 'test-this)
(setq
backup-by-copying t ; don't clobber symlinks
backup-directory-alist
'(("." . "~/.saves")) ; don't litter my fs tree
delete-old-versions t
kept-new-versions 6
kept-old-versions 2
version-control t) ; use versioned backups
(setq column-number-mode t)
(setq x-alt-keysym 'meta)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(package-selected-packages
(quote
(flycheck-golangci-lint yaml-mode atom-one-dark-theme neotree flymake-go go-autocomplete auto-complete exec-path-from-shell go-mode))))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
(setenv "GOPATH" "/Users/mattmoor/go")
(defun set-exec-path-from-shell-PATH ()
(let ((path-from-shell (replace-regexp-in-string
"[ \t\n]*$"
""
(shell-command-to-string "$SHELL --login -i -c 'echo $PATH'"))))
(setenv "PATH" (concat path-from-shell ":" (getenv "GOPATH") "/bin"))
(setq eshell-path-env path-from-shell) ; for eshell users
(setq exec-path (split-string path-from-shell path-separator))
(add-to-list 'exec-path "/Users/mattmoor/go/bin")))
(when window-system (set-exec-path-from-shell-PATH))
;; Adapted from: https://johnsogg.github.io/emacs-golang
(defun my-go-mode-hook ()
(add-hook 'before-save-hook 'gofmt-before-save) ; gofmt before every save
(add-hook 'before-save-hook 'delete-trailing-whitespace) ; remove trailing whitespace
(setq gofmt-command "goimports") ; gofmt uses invokes goimports
(local-set-key (kbd "M-.") 'godef-jump) ; Go to definition
(auto-complete-mode 1))
(add-hook 'go-mode-hook 'my-go-mode-hook)
(with-eval-after-load 'go-mode
(require 'go-autocomplete))
(setq mac-command-modifier 'meta)
(setq mac-function-modifier 'control)
(when window-system (server-start))
(add-to-list 'default-frame-alist '(fullscreen . maximized))
(shell-command-to-string "$SHELL -i -c 'echo $PATH'")
;; Follow compilation output
(setq compilation-scroll-output t)
;; Make fonts tiny.
(set-face-attribute 'default nil :height 120)
;; DEMO FONT SIZE
; (set-face-attribute 'default nil :height 200)
(setq sh-basic-offset 2)
(global-unset-key [(control z)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment