Skip to content

Instantly share code, notes, and snippets.

@jlprat
Last active March 14, 2017 12:27
Show Gist options
  • Save jlprat/8b5658b9e3200e50a3f639247297a47c to your computer and use it in GitHub Desktop.
Save jlprat/8b5658b9e3200e50a3f639247297a47c to your computer and use it in GitHub Desktop.
Init.el file for emacs to be configured with Ensime, helm, and zoom-frm
; global variables
(setq
inhibit-startup-screen t
create-lockfiles nil
make-backup-files nil
column-number-mode t
scroll-error-top-bottom t
show-paren-delay 0.5
use-package-always-ensure t
sentence-end-double-space nil
user-full-name "Josep Prat")
;; buffer local variables
(setq-default
indent-tabs-mode nil
tab-width 4
c-basic-offset 4)
(defalias 'yes-or-no-p 'y-or-n-p)
;; modes
(electric-indent-mode 0)
;; the package manager
(require 'package)
(setq
package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("org" . "http://orgmode.org/elpa/")
("melpa" . "http://melpa.org/packages/")
("melpa-stable" . "http://stable.melpa.org/packages/"))
package-archive-priorities '(("melpa-stable" . 1)))
(package-initialize)
(when (not package-archive-contents)
(package-refresh-contents)
(package-install 'use-package))
(require 'use-package)
(require 'ensime)
;; custom function definition
(defun dot-emacs ()
"Go directly to .emacs, do not pass Go, do not collect $200."
(interactive)
(message "Stop procrastinating and do some work!")
(find-file "~/.emacs.d/init.el"))
(defun revert-buffer-no-confirm ()
;; http://www.emacswiki.org/emacs-en/download/misc-cmds.el
"Revert buffer without confirmation."
(interactive)
(revert-buffer t t))
(defun contextual-backspace ()
"Hungry whitespace or delete word depending on context."
(interactive)
(if (looking-back "[[:space:]\n]\\{2,\\}" (- (point) 2))
(while (looking-back "[[:space:]\n]" (- (point) 1))
(delete-char -1))
(cond
((and (boundp 'smartparens-strict-mode)
smartparens-strict-mode)
(sp-backward-kill-word 1))
(subword-mode
(subword-backward-kill 1))
(t
(backward-kill-word 1)))))
(defun scala-mode-newline-comments ()
"Custom newline appropriate for `scala-mode'."
;; shouldn't this be in a post-insert hook?
(interactive)
(newline-and-indent)
(scala-indent:insert-asterisk-on-multiline-comment))
(defun c-mode-newline-comments ()
"Newline with indent and preserve multiline comments."
(interactive)
(c-indent-new-comment-line)
(indent-according-to-mode))
(defun sp-restrict-c (sym)
"Smartparens restriction on `SYM' for C-derived parenthesis."
(sp-restrict-to-pairs-interactive "{([" sym))
(defun company-backends-for-buffer ()
"Calculate appropriate `company-backends' for the buffer.
For small projects, use TAGS for completions, otherwise use a
very minimal set."
(projectile-visit-project-tags-table)
(cl-flet ((size () (buffer-size (get-file-buffer tags-file-name))))
(let ((base '(company-keywords company-dabbrev-code company-yasnippet)))
(if (and tags-file-name (<= 20000000 (size)))
(list (push 'company-etags base))
(list base)))))
(defun contextual-backspace ()
"Hungry whitespace or delete word depending on context."
(interactive)
(if (looking-back "[[:space:]\n]\\{2,\\}" (- (point) 2))
(while (looking-back "[[:space:]\n]" (- (point) 1))
(delete-char -1))
(cond
((and (boundp 'smartparens-strict-mode)
smartparens-strict-mode)
(sp-backward-kill-word 1))
((and (boundp 'subword-mode)
subword-mode)
(subword-backward-kill 1))
(t
(backward-kill-word 1)))))
;; packages
(use-package expand-region
:commands 'er/expand-region
:bind ("C-=" . er/expand-region))
(use-package company
:diminish company-mode
:commands company-mode
:init
(setq
company-dabbrev-other-buffers t
company-dabbrev-ignore-case nil
company-dabbrev-code-ignore-case nil
company-dabbrev-downcase nil
company-dabbrev-minimum-length 4
company-idle-delay 0
company-minimum-prefix-length 4)
:config
;; dabbrev is too slow, use C-TAB explicitly
(delete 'company-dabbrev company-backends)
;; disables TAB in company-mode, freeing it for yasnippet
(define-key company-active-map [tab] nil)
(define-key company-active-map (kbd "TAB") nil))
(use-package projectile
:demand
:init (setq projectile-use-git-grep t)
:config (projectile-global-mode t)
:bind (("s-f" . projectile-find-file)
("s-F" . projectile-grep)))
(use-package highlight-symbol
:diminish highlight-symbol-mode
:commands highlight-symbol
:bind ("s-h" . highlight-symbol))
(use-package goto-chg
:commands goto-last-change
;; complementary to
;; C-x r m / C-x r l
;; and C-<space> C-<space> / C-u C-<space>
:bind (("C-." . goto-last-change)
("C-," . goto-last-change-reverse)))
(use-package popup-imenu
:commands popup-imenu
:bind ("M-i" . popup-imenu))
(use-package magit
:commands magit-status magit-blame
:init (setq
magit-revert-buffers nil)
:bind (("s-g" . magit-status)
("s-b" . magit-blame)))
(use-package smartparens
:diminish smartparens-mode
:commands
smartparens-strict-mode
smartparens-mode
sp-restrict-to-pairs-interactive
sp-local-pair
:init
(setq sp-interactive-dwim t)
:config
(require 'smartparens-config)
(sp-use-smartparens-bindings)
(sp-pair "(" ")" :wrap "C-(") ;; how do people live without this?
(sp-pair "[" "]" :wrap "s-[") ;; C-[ sends ESC
(sp-pair "{" "}" :wrap "C-{")
(sp-pair "<" ">" :wrap "C-<")
;; nice whitespace / indentation when creating statements
(sp-local-pair '(c-mode java-mode) "(" nil :post-handlers '(("||\n[i]" "RET")))
(sp-local-pair '(c-mode java-mode) "{" nil :post-handlers '(("||\n[i]" "RET")))
(sp-local-pair '(java-mode) "<" nil :post-handlers '(("||\n[i]" "RET")))
;; WORKAROUND https://github.com/Fuco1/smartparens/issues/543
(bind-key "C-<left>" nil smartparens-mode-map)
(bind-key "C-<right>" nil smartparens-mode-map)
(bind-key "s-{" 'sp-rewrap-sexp smartparens-mode-map)
(bind-key "s-<delete>" 'sp-kill-sexp smartparens-mode-map)
(bind-key "s-<backspace>" 'sp-backward-kill-sexp smartparens-mode-map)
(bind-key "s-<home>" 'sp-beginning-of-sexp smartparens-mode-map)
(bind-key "s-<end>" 'sp-end-of-sexp smartparens-mode-map)
(bind-key "s-<left>" 'sp-beginning-of-previous-sexp smartparens-mode-map)
(bind-key "s-<right>" 'sp-next-sexp smartparens-mode-map)
(bind-key "s-<up>" 'sp-backward-up-sexp smartparens-mode-map)
(bind-key "s-<down>" 'sp-down-sexp smartparens-mode-map))
(use-package scala-mode
:defer t
:pin melpa-stable
:init
(setq
scala-indent:use-javadoc-style t
scala-indent:align-parameters t)
:config
;; prefer smartparens for parens handling
(remove-hook 'post-self-insert-hook
'scala-indent:indent-on-parentheses)
(sp-local-pair 'scala-mode "(" nil :post-handlers '(("||\n[i]" "RET")))
(sp-local-pair 'scala-mode "{" nil
:post-handlers '(("||\n[i]" "RET")
("| " "SPC")
fommil-sp-wrap-with-brackets))
(bind-key "RET" 'scala-mode-newline-comments scala-mode-map)
(bind-key "s-<delete>" (sp-restrict-c 'sp-kill-sexp) scala-mode-map)
(bind-key "s-<backspace>" (sp-restrict-c 'sp-backward-kill-sexp) scala-mode-map)
(bind-key "s-<home>" (sp-restrict-c 'sp-beginning-of-sexp) scala-mode-map)
(bind-key "s-<end>" (sp-restrict-c 'sp-end-of-sexp) scala-mode-map)
;; BUG https://github.com/Fuco1/smartparens/issues/468
;; backwards/next not working particularly well
;; i.e. bypass company-mode
(bind-key "C-<tab>" 'dabbrev-expand scala-mode-map)
(bind-key "C-c c" 'sbt-command scala-mode-map)
(bind-key "C-c e" 'next-error scala-mode-map))
(defun ensime-edit-definition-with-fallback (arg)
"Variant of `ensime-edit-definition' with ctags if ENSIME is not available."
(interactive "P")
(unless (and (ensime-connection-or-nil)
(ensime-edit-definition arg))
(projectile-find-tag)))
(use-package ensime
:defer t
:pin melpa-stable
:init
(put 'ensime-auto-generate-config 'safe-local-variable #'booleanp)
(setq
ensime-startup-notification nil
ensime-startup-snapshot-notification nil)
:config
(require 'ensime-expand-region)
(add-hook 'git-timemachine-mode-hook (lambda () (ensime-mode 0)))
(bind-key "s-n" 'ensime-search ensime-mode-map)
(bind-key "s-t" 'ensime-print-type-at-point ensime-mode-map)
(bind-key "M-." 'ensime-edit-definition-with-fallback ensime-mode-map))
(use-package yasnippet
:diminish yas-minor-mode
:commands yas-minor-mode
:config
(yas-reload-all)
(define-key yas-minor-mode-map [tab] #'yas-expand))
(add-hook 'scala-mode-hook
(lambda ()
(show-paren-mode t)
(smartparens-mode t)
(yas-minor-mode t)
(company-mode t)
(scala-mode:goto-start-of-code)))
(add-hook 'ensime-mode-hook
(lambda ()
(let ((backends (company-backends-for-buffer)))
(setq company-backends (push '(ensime-company company-yasnippet) backends)))))
(use-package cc-mode
:ensure nil
:config
(bind-key "C-c c" 'sbt-command java-mode-map)
(bind-key "C-c e" 'next-error java-mode-map)
(bind-key "RET" 'c-mode-newline-comments java-mode-map))
(add-hook 'java-mode-hook
(lambda ()
(show-paren-mode t)
(smartparens-mode t)
(yas-minor-mode t)
(company-mode t)
(ensime-mode t)))
;; theme
(use-package darcula-theme)
(load-theme 'darcula t)
;;custom bindings
(global-set-key (kbd "M-x") 'helm-M-x)
(global-set-key (kbd "C-x C-m") 'helm-M-x)
(global-set-key (kbd "C-c C-m") 'helm-M-x)
(global-set-key (kbd "C-x b") 'helm-mini)
(global-set-key (kbd "C-x C-f") 'helm-find-files)
(global-set-key (kbd "C-=") 'zoom-frm-in)
(global-set-key (kbd "C--") 'zoom-frm-out)
(global-unset-key (kbd "C-z")) ;; I hate you so much C-z
(global-set-key (kbd "C-<backspace>") 'contextual-backspace)
(global-set-key (kbd "RET") 'newline-and-indent)
(global-set-key (kbd "M-.") 'projectile-find-tag)
(global-set-key (kbd "M-,") 'pop-tag-mark)
(global-set-key (kbd "C-<tab>") 'company-or-dabbrev-complete)
(global-set-key (kbd "s-s") 'replace-string)
(global-set-key (kbd "<f5>") 'revert-buffer-no-confirm)
;; some idea shortcuts
(global-set-key (kbd "C-M-l") 'ensime-format-source)
(global-set-key (kbd "s-n") 'ensime-search)
(global-set-key (kbd "s-w") 'ensime-expand-selection-command)
(global-set-key (kbd "s-q") 'ensime-show-doc-for-symbol-at-point)
(global-set-key (kbd "s-o") 'ensime-refactor-diff-organize-imports)
(global-set-key (kbd "C-S-v") 'ensime-refactor-diff-extract-local)
(global-set-key (kbd "C-S-m") 'ensime-refactor-diff-extract-method)
(global-set-key (kbd "C-S-l") 'goto-line)
(global-set-key (kbd "<f6>") 'dot-emacs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment