emacs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. ;; TODO:
  2. ;; Easier undo tree traversal
  3. ;; Test easing spelling navigation, map ]s and [s to next and previously wrong word
  4. ;; Replace evil-leader with general
  5. ;;;; Startup
  6. (setq initial-scratch-message "")
  7. ;;;; Base
  8. ;; Disable beep & flash
  9. (setq ring-bell-function 'ignore)
  10. ;; Disable blinking cursor
  11. (blink-cursor-mode 0)
  12. ;; TODO finish fixing eval-last-s-exp
  13. ;;(defun evil-eval-prev-exp ()
  14. ;;(interactive)
  15. ;;(evil-append 1)
  16. ;;(call-interactively (global-key-binding "\C-x"))
  17. ;;(call-interactively (global-key-binding "\C-e"))
  18. ;;(evil-normal-state))
  19. ;; All yes or no prompts are y or n
  20. (defalias 'yes-or-no-p 'y-or-n-p)
  21. ;; Never follow symlinks
  22. (setq vc-follow-symlinks nil)
  23. ;;; Leave the OS clipboard alone (use evil's "+ and "* instead)
  24. ; Don't copy and paste to the clipboard
  25. (setq select-enable-clipboard nil)
  26. ; Don't save to the clipboard on exit
  27. (setq x-select-enable-clipboard-manager nil)
  28. ;; Text and Notes
  29. (setq sentence-end-double-space nil)
  30. ;; Save minibar history
  31. (savehist-mode 1)
  32. (setq savehist-additional-variables '(kill-ring search-ring regexp-search-ring))
  33. ;; Auto-enable elisp when opening .emacs in dotfiles (without the .)
  34. (add-to-list 'auto-mode-alist '("emacs" . emacs-lisp-mode))
  35. ;; Start in text-mode
  36. (setq initial-major-mode 'text-mode)
  37. ;; Always show matching parens
  38. (show-paren-mode t)
  39. ;; Backups (from https://stackoverflow.com/questions/151945/how-do-i-control-how-emacs-makes-backup-files/20824625#20824625)
  40. (setq version-control t ;; Use version numbers for backups.
  41. kept-new-versions 10 ;; Number of newest versions to keep.
  42. kept-old-versions 0 ;; Number of oldest versions to keep.
  43. delete-old-versions t ;; Don't ask to delete excess backup versions.
  44. backup-by-copying t) ;; Copy all files, don't rename them.
  45. (setq vc-make-backup-files t) ;; Backup versioned files
  46. ;; Default and per-save backups go here:
  47. (setq backup-directory-alist '(("" . "~/.emacs.d/backups/per-save")))
  48. (defun force-backup-of-buffer ()
  49. ;; Make a special "per session" backup at the first save of each
  50. ;; emacs session.
  51. (when (not buffer-backed-up)
  52. ;; Override the default parameters for per-session backups.
  53. (let ((backup-directory-alist '(("" . "~/.emacs.d/backup/per-session")))
  54. (kept-new-versions 3))
  55. (backup-buffer)))
  56. ;; Make a "per save" backup on each save. The first save results in
  57. ;; both a per-session and a per-save backup, to keep the numbering
  58. ;; of per-save backups consistent.
  59. (let ((buffer-backed-up nil))
  60. (backup-buffer)))
  61. (add-hook 'before-save-hook 'force-backup-of-buffer)
  62. ;; Disable toolbar
  63. (when (display-graphic-p)
  64. (tool-bar-mode -1)
  65. )
  66. ;;;; Packages
  67. ;; Package installation
  68. (require 'package)
  69. (add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/"))
  70. (add-to-list 'package-archives '("melpa" . "http://melpa.org/packages/"))
  71. (add-to-list 'package-archives '("melpa-stable" . "http://stable.melpa.org/packages/"))
  72. (setq package-enable-at-startup nil)
  73. (package-initialize)
  74. ;; Keep track of whether or not we need to refresh package contents
  75. (setq packages-installed-this-session 0)
  76. ;; Function to ensure every package in installed, and ask if it isn't.
  77. (defun ensure-package-installed (&rest packages)
  78. (mapcar
  79. (lambda (package)
  80. (if (package-installed-p package)
  81. nil
  82. (if (y-or-n-p (format "Package %s is missing. Install it? " package))
  83. ;; If this is the 1st install this session, update before install
  84. (cond ((eq packages-installed-this-session 0)
  85. (package-refresh-contents)
  86. (setq packages-installed-this-session 1)
  87. (package-install package))
  88. (t (package-install package))
  89. nil)
  90. package)))
  91. packages))
  92. ;; List of packages to install on all systems
  93. (setq required-packages
  94. '(
  95. iedit
  96. magit
  97. evil-magit
  98. undo-tree
  99. evil
  100. evil-leader
  101. evil-tabs
  102. powerline-evil
  103. monokai-theme
  104. auto-complete
  105. linum-relative
  106. multi-term
  107. neotree
  108. evil-numbers
  109. editorconfig
  110. ))
  111. ;; List of optional packages
  112. (setq optional-packages
  113. '(
  114. flymd
  115. markdown-mode
  116. latex-preview-pane
  117. tide
  118. web-mode
  119. ac-html
  120. racket-mode
  121. fuzzy
  122. general))
  123. ;; Check that all packages are installed
  124. (apply 'ensure-package-installed required-packages)
  125. ;; Declare function for optional packages
  126. (defun optional-packages-install ()
  127. (interactive)
  128. (apply 'ensure-package-installed optional-packages))
  129. ;; Activate installed packages
  130. (package-initialize)
  131. ;;; Evil
  132. ;; No C-i jump
  133. (setq evil-want-C-i-jump nil)
  134. ;; Evil tabs
  135. (global-evil-tabs-mode t)
  136. ;; Default to evil mode
  137. (evil-mode t)
  138. ;; Move all elements of evil-emacs-state-modes to evil-motion-state-modes
  139. (setq evil-motion-state-modes (append evil-emacs-state-modes evil-motion-state-modes))
  140. (setq evil-emacs-state-modes nil)
  141. ;; Delete info bindings for evil to take over
  142. (define-key Info-mode-map "g" nil)
  143. (define-key Info-mode-map "n" nil)
  144. (define-key Info-mode-map "p" nil)
  145. ;; Vim removing of windows
  146. (define-key evil-window-map (kbd "q") 'delete-window)
  147. (define-key evil-window-map (kbd "C-q") 'delete-window)
  148. ; Don't echo evil's states
  149. (setq evil-insert-state-message nil)
  150. (setq evil-visual-state-message nil)
  151. ;; Increment and decrement (evil-numbers)
  152. (define-key evil-insert-state-map (kbd "C-a") 'evil-numbers/inc-at-pt)
  153. (define-key evil-insert-state-map (kbd "C-d") 'evil-numbers/dec-at-pt)
  154. ;;; undo-tree
  155. ;; Save undo history under .emacs.d/undo
  156. (setq undo-tree-auto-save-history t
  157. undo-tree-history-directory-alist
  158. `(("." . ,(concat user-emacs-directory "undo"))))
  159. (unless (file-exists-p (concat user-emacs-directory "undo"))
  160. (make-directory (concat user-emacs-directory "undo")))
  161. ;;; Powerline
  162. (require 'powerline)
  163. (powerline-evil-center-color-theme)
  164. ;;; Recent Files
  165. (require 'recentf)
  166. (recentf-mode 1)
  167. (setq recentf-max-saved-items 200
  168. recentf-max-menu-items 15)
  169. ;;; Web mode
  170. ;; 2 spaces for an indent
  171. (defun my-web-mode-hook ()
  172. "Hooks for Web mode."
  173. (setq web-mode-markup-indent-offset 2)
  174. )
  175. (add-hook 'web-mode-hook 'my-web-mode-hook)
  176. ;; Auto-enable web-mode when opening relevent files
  177. (add-to-list 'auto-mode-alist '("\\.html\\'" . web-mode))
  178. (add-to-list 'auto-mode-alist '("\\.hbs\\'" . web-mode))
  179. (add-to-list 'auto-mode-alist '("\\.handlebars\\'" . web-mode))
  180. ;;; Autocomplete
  181. (require 'auto-complete)
  182. ;; start auto-complete
  183. (eval-and-compile
  184. (require 'auto-complete nil 'noerror))
  185. (ac-config-default)
  186. (setq ac-auto-start t)
  187. (global-set-key (kbd "<backtab>") 'ac-previous)
  188. ;; ac-html
  189. (setq web-mode-ac-sources-alist
  190. '(("css" . (ac-source-css-property))
  191. ("html" . (ac-source-words-in-buffer ac-source-abbrev))))
  192. (ac-linum-workaround)
  193. (setq ac-auto-show-menu t)
  194. ;;; Spelling
  195. ;; map ]s and [s to next and previously wrong word
  196. ;; move point to previous error
  197. ;; based on code by hatschipuh at
  198. ;; http://emacs.stackexchange.com/a/14912/2017
  199. (defun flyspell-goto-previous-error (arg)
  200. "Go to arg previous spelling error."
  201. (interactive "p")
  202. (while (not (= 0 arg))
  203. (let ((pos (point))
  204. (min (point-min)))
  205. (if (and (eq (current-buffer) flyspell-old-buffer-error)
  206. (eq pos flyspell-old-pos-error))
  207. (progn
  208. (if (= flyspell-old-pos-error min)
  209. ;; goto beginning of buffer
  210. (progn
  211. (message "Restarting from end of buffer")
  212. (goto-char (point-max)))
  213. (backward-word 1))
  214. (setq pos (point))))
  215. ;; seek the next error
  216. (while (and (> pos min)
  217. (let ((ovs (overlays-at pos))
  218. (r '()))
  219. (while (and (not r) (consp ovs))
  220. (if (flyspell-overlay-p (car ovs))
  221. (setq r t)
  222. (setq ovs (cdr ovs))))
  223. (not r)))
  224. (backward-word 1)
  225. (setq pos (point)))
  226. ;; save the current location for next invocation
  227. (setq arg (1- arg))
  228. (setq flyspell-old-pos-error pos)
  229. (setq flyspell-old-buffer-error (current-buffer))
  230. (goto-char pos)
  231. (if (= pos min)
  232. (progn
  233. (message "No more miss-spelled word!")
  234. (setq arg 0))
  235. ))))
  236. (define-key evil-normal-state-map (kbd "[s") 'flyspell-goto-previous-error)
  237. (define-key evil-normal-state-map (kbd "]s") 'flyspell-goto-next-error)
  238. ;;; Relative line numbers
  239. (require 'linum-relative)
  240. (setq linum-relative-current-symbol "")
  241. (linum-mode)
  242. (linum-relative-global-mode)
  243. ;;; flymd
  244. ; flymd.md and flymd.html are deleted upon markdown buffer killed
  245. (setq flymd-close-buffer-delete-temp-files t)
  246. ;;; evil-leader
  247. ;; Evil leader is Space
  248. (global-evil-leader-mode)
  249. (evil-leader/set-leader "<SPC>")
  250. ;; Leader keybinds
  251. (evil-leader/set-key
  252. "d" 'diff-buffer-with-file
  253. "b" 'buffer-menu
  254. ;"f" '(lambda () (interactive) (dired '"./"))
  255. "f" 'neotree-toggle
  256. "u" 'undo-tree-visualize
  257. "m" 'recentf-open-files
  258. "l" 'auto-fill-mode
  259. "s" '(lambda () (interactive) (if flyspell-mode (funcall 'flyspell-mode '0) (flyspell-mode) (flyspell-buffer)))
  260. "a" 'auto-complete-mode
  261. "g" 'magit-status
  262. "M-g" 'magit-dispatch-popup
  263. )
  264. ;; Magit
  265. (require 'magit)
  266. (setq evil-magit-state 'normal)
  267. (require 'evil-magit)
  268. (global-magit-file-mode)
  269. ;; Neotree
  270. ; Set vi-like bindings in neotree-mode that don't conflict with evil
  271. (evil-define-key 'normal neotree-mode-map (kbd "TAB") 'neotree-enter)
  272. (evil-define-key 'normal neotree-mode-map (kbd "SPC") 'neotree-enter)
  273. (evil-define-key 'normal neotree-mode-map (kbd "q") 'neotree-hide)
  274. (evil-define-key 'normal neotree-mode-map (kbd "RET") 'neotree-enter)
  275. (evil-define-key 'normal neotree-mode-map (kbd "h") 'neotree-hidden-file-toggle)
  276. ;Every time when the neotree window is opened, let it find current file and jump to node.
  277. (setq neo-smart-open t)
  278. ;; tide/typescript
  279. (setq typescript-indent-level 2)
  280. ;; JavaScript
  281. (setq js-indent-level 2)
  282. ;; geiser
  283. (add-to-list 'auto-mode-alist '("\\.scm\\'" . scheme-mode))
  284. (setq geiser-scheme-implementation 'racket)
  285. ;; editorconfig
  286. (editorconfig-mode 1)
  287. ;;;; System-specific configs
  288. (defun win-setup ()
  289. (add-to-list 'exec-path "C:/Program Files (x86)/Aspell/bin/")
  290. (setq ispell-program-name "aspell")
  291. (defun cmd ()
  292. (interactive)
  293. (make-comint-in-buffer "cmd" nil "cmd" nil)
  294. (switch-to-buffer "*cmd*")))
  295. (defun linux-setup ())
  296. (cond ((eq system-type 'windows-nt) (win-setup))
  297. ((eq system-type 'gnu/linux) (linux-setup))
  298. (t (message "")))
  299. ;;;; Custom
  300. (defconst custom-file (expand-file-name "custom.el" user-emacs-directory))
  301. ;; if no custom file exists, write a default one
  302. (unless (file-exists-p custom-file)
  303. (write-region "(custom-set-faces
  304. ;; custom-set-faces was added by Custom.
  305. ;; If you edit it by hand, you could mess it up, so be careful.
  306. ;; Your init file should contain only one such instance.
  307. ;; If there is more than one, they won't work right.
  308. '(powerline-evil-normal-face ((t (:background \"#859900\")))))
  309. (custom-set-variables
  310. ;; custom-set-variables was added by Custom.
  311. ;; If you edit it by hand, you could mess it up, so be careful.
  312. ;; Your init file should contain only one such instance.
  313. ;; If there is more than one, they won't work right.
  314. '(custom-enabled-themes (quote (monokai)))
  315. '(custom-safe-themes
  316. (quote
  317. (\"c7a9a68bd07e38620a5508fef62ec079d274475c8f92d75ed0c33c45fbe306bc\" default))))
  318. " nil custom-file))
  319. (load custom-file)