Emacs config utilizing prelude as a base
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

412 lines
14 KiB

13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 years ago
12 years ago
12 years ago
14 years ago
  1. ;;; prelude-editor.el --- Emacs Prelude: enhanced core editing experience.
  2. ;;
  3. ;; Copyright © 2011-2013 Bozhidar Batsov
  4. ;;
  5. ;; Author: Bozhidar Batsov <bozhidar@batsov.com>
  6. ;; URL: https://github.com/bbatsov/prelude
  7. ;; Version: 1.0.0
  8. ;; Keywords: convenience
  9. ;; This file is not part of GNU Emacs.
  10. ;;; Commentary:
  11. ;; Refinements of the core editing experience in Emacs.
  12. ;;; License:
  13. ;; This program is free software; you can redistribute it and/or
  14. ;; modify it under the terms of the GNU General Public License
  15. ;; as published by the Free Software Foundation; either version 3
  16. ;; of the License, or (at your option) any later version.
  17. ;;
  18. ;; This program is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;;
  23. ;; You should have received a copy of the GNU General Public License
  24. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  25. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  26. ;; Boston, MA 02110-1301, USA.
  27. ;;; Code:
  28. ;; customize
  29. (defgroup prelude nil
  30. "Emacs Prelude configuration."
  31. :prefix "prelude-"
  32. :group 'convenience)
  33. (defcustom prelude-auto-save t
  34. "Non-nil values enable Prelude's auto save."
  35. :type 'boolean
  36. :group 'prelude)
  37. (defcustom prelude-guru t
  38. "Non-nil values enable guru-mode."
  39. :type 'boolean
  40. :group 'prelude)
  41. (defcustom prelude-whitespace t
  42. "Non-nil values enable Prelude's whitespace visualization."
  43. :type 'boolean
  44. :group 'prelude)
  45. (defcustom prelude-clean-whitespace-on-save t
  46. "Cleanup whitespace from file before it's saved.
  47. Will only occur if prelude-whitespace is also enabled."
  48. :type 'boolean
  49. :group 'prelude)
  50. (defcustom prelude-flyspell t
  51. "Non-nil values enable Prelude's flyspell support."
  52. :type 'boolean
  53. :group 'prelude)
  54. ;; Death to the tabs! However, tabs historically indent to the next
  55. ;; 8-character offset; specifying anything else will cause *mass*
  56. ;; confusion, as it will change the appearance of every existing file.
  57. ;; In some cases (python), even worse -- it will change the semantics
  58. ;; (meaning) of the program.
  59. ;;
  60. ;; Emacs modes typically provide a standard means to change the
  61. ;; indentation width -- eg. c-basic-offset: use that to adjust your
  62. ;; personal indentation width, while maintaining the style (and
  63. ;; meaning) of any files you load.
  64. (setq-default indent-tabs-mode nil) ;; don't use tabs to indent
  65. (setq-default tab-width 8) ;; but maintain correct appearance
  66. ;; delete the selection with a keypress
  67. (delete-selection-mode t)
  68. ;; store all backup and autosave files in the tmp dir
  69. (setq backup-directory-alist
  70. `((".*" . ,temporary-file-directory)))
  71. (setq auto-save-file-name-transforms
  72. `((".*" ,temporary-file-directory t)))
  73. ;; revert buffers automatically when underlying files are changed externally
  74. (global-auto-revert-mode t)
  75. ;; hippie expand is dabbrev expand on steroids
  76. (setq hippie-expand-try-functions-list '(try-expand-dabbrev
  77. try-expand-dabbrev-all-buffers
  78. try-expand-dabbrev-from-kill
  79. try-complete-file-name-partially
  80. try-complete-file-name
  81. try-expand-all-abbrevs
  82. try-expand-list
  83. try-expand-line
  84. try-complete-lisp-symbol-partially
  85. try-complete-lisp-symbol))
  86. ;; smart tab behavior - indent or complete
  87. (setq tab-always-indent 'complete)
  88. ;; smart pairing for all
  89. (require 'smartparens-config)
  90. (setq sp-base-key-bindings 'paredit)
  91. (setq sp-autoskip-closing-pair 'always)
  92. (setq sp-hybrid-kill-entire-symbol nil)
  93. (sp-use-paredit-bindings)
  94. (show-smartparens-global-mode +1)
  95. (define-key prog-mode-map (kbd "M-(") (prelude-wrap-with "("))
  96. ;; FIXME: pick terminal friendly binding
  97. ;; (define-key prog-mode-map (kbd "M-[") (prelude-wrap-with "["))
  98. (define-key prog-mode-map (kbd "M-\"") (prelude-wrap-with "\""))
  99. ;; disable annoying blink-matching-paren
  100. (setq blink-matching-paren nil)
  101. ;; diminish keeps the modeline tidy
  102. (require 'diminish)
  103. ;; meaningful names for buffers with the same name
  104. (require 'uniquify)
  105. (setq uniquify-buffer-name-style 'forward)
  106. (setq uniquify-separator "/")
  107. (setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
  108. (setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
  109. ;; saveplace remembers your location in a file when saving files
  110. (require 'saveplace)
  111. (setq save-place-file (expand-file-name "saveplace" prelude-savefile-dir))
  112. ;; activate it for all buffers
  113. (setq-default save-place t)
  114. ;; savehist keeps track of some history
  115. (require 'savehist)
  116. (setq savehist-additional-variables
  117. ;; search entries
  118. '(search ring regexp-search-ring)
  119. ;; save every minute
  120. savehist-autosave-interval 60
  121. ;; keep the home clean
  122. savehist-file (expand-file-name "savehist" prelude-savefile-dir))
  123. (savehist-mode +1)
  124. ;; save recent files
  125. (require 'recentf)
  126. (setq recentf-save-file (expand-file-name "recentf" prelude-savefile-dir)
  127. recentf-max-saved-items 500
  128. recentf-max-menu-items 15)
  129. (defun prelude-recentf-exclude-p (file)
  130. "A predicate to decide whether to exclude FILE from recentf."
  131. (let ((file-dir (file-truename (file-name-directory file))))
  132. (-any-p (lambda (dir)
  133. (string-prefix-p dir file-dir))
  134. (mapcar 'file-truename (list prelude-savefile-dir package-user-dir)))))
  135. (add-to-list 'recentf-exclude 'prelude-recentf-exclude-p)
  136. (setq recentf-auto-cleanup 'never) ;; disable before we start recentf!
  137. (recentf-mode +1)
  138. ;; use shift + arrow keys to switch between visible buffers
  139. (require 'windmove)
  140. (windmove-default-keybindings)
  141. ;; automatically save buffers associated with files on buffer switch
  142. ;; and on windows switch
  143. (defun prelude-auto-save-command ()
  144. "Save the current buffer if `prelude-auto-save' is not nil."
  145. (when (and prelude-auto-save
  146. buffer-file-name
  147. (buffer-modified-p (current-buffer))
  148. (file-writable-p buffer-file-name))
  149. (save-buffer)))
  150. (defmacro advise-commands (advice-name commands &rest body)
  151. "Apply advice named ADVICE-NAME to multiple COMMANDS.
  152. The body of the advice is in BODY."
  153. `(progn
  154. ,@(mapcar (lambda (command)
  155. `(defadvice ,command (before ,(intern (concat (symbol-name command) "-" advice-name)) activate)
  156. ,@body))
  157. commands)))
  158. ;; advise all window switching functions
  159. (advise-commands "auto-save"
  160. (switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
  161. (prelude-auto-save-command))
  162. (add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)
  163. ;; Autosave buffers when focus is lost
  164. (defun prelude-save-all-buffers ()
  165. "Save all modified buffers, without prompts."
  166. (save-some-buffers 'dont-ask))
  167. (when (version<= "24.4" emacs-version)
  168. (add-hook 'focus-out-hook 'prelude-save-all-buffers))
  169. ;; highlight the current line
  170. (global-hl-line-mode +1)
  171. (require 'volatile-highlights)
  172. (volatile-highlights-mode t)
  173. (diminish 'volatile-highlights-mode)
  174. ;; tramp, for sudo access
  175. (require 'tramp)
  176. ;; keep in mind known issues with zsh - see emacs wiki
  177. (setq tramp-default-method "ssh")
  178. (set-default 'imenu-auto-rescan t)
  179. ;; flyspell-mode does spell-checking on the fly as you type
  180. (require 'flyspell)
  181. (setq ispell-program-name "aspell" ; use aspell instead of ispell
  182. ispell-extra-args '("--sug-mode=ultra"))
  183. (defun prelude-enable-flyspell ()
  184. "Enable command `flyspell-mode' if `prelude-flyspell' is not nil."
  185. (when (and prelude-flyspell (executable-find ispell-program-name))
  186. (flyspell-mode +1)))
  187. (defun prelude-cleanup-maybe ()
  188. "Invoke `whitespace-cleanup' if `prelude-clean-whitespace-on-save' is not nil."
  189. (when prelude-clean-whitespace-on-save
  190. (whitespace-cleanup)))
  191. (defun prelude-enable-whitespace ()
  192. "Enable `whitespace-mode' if `prelude-whitespace' is not nil."
  193. (when prelude-whitespace
  194. ;; keep the whitespace decent all the time (in this buffer)
  195. (add-hook 'before-save-hook 'prelude-cleanup-maybe nil t)
  196. (whitespace-mode +1)))
  197. (add-hook 'text-mode-hook 'prelude-enable-flyspell)
  198. (add-hook 'text-mode-hook 'prelude-enable-whitespace)
  199. ;; enable narrowing commands
  200. (put 'narrow-to-region 'disabled nil)
  201. (put 'narrow-to-page 'disabled nil)
  202. (put 'narrow-to-defun 'disabled nil)
  203. ;; enabled change region case commands
  204. (put 'upcase-region 'disabled nil)
  205. (put 'downcase-region 'disabled nil)
  206. ;; enable erase-buffer command
  207. (put 'erase-buffer 'disabled nil)
  208. (require 'expand-region)
  209. ;; bookmarks
  210. (require 'bookmark)
  211. (setq bookmark-default-file (expand-file-name "bookmarks" prelude-savefile-dir)
  212. bookmark-save-flag 1)
  213. ;; projectile is a project management mode
  214. (require 'projectile)
  215. (setq projectile-cache-file (expand-file-name "projectile.cache" prelude-savefile-dir))
  216. (projectile-global-mode t)
  217. ;; anzu-mode enhances isearch by showing total matches and current match position
  218. (require 'anzu)
  219. (diminish 'anzu-mode)
  220. (global-anzu-mode)
  221. ;; shorter aliases for ack-and-a-half commands
  222. (defalias 'ack 'ack-and-a-half)
  223. (defalias 'ack-same 'ack-and-a-half-same)
  224. (defalias 'ack-find-file 'ack-and-a-half-find-file)
  225. (defalias 'ack-find-file-same 'ack-and-a-half-find-file-same)
  226. ;; dired - reuse current buffer by pressing 'a'
  227. (put 'dired-find-alternate-file 'disabled nil)
  228. ;; always delete and copy recursively
  229. (setq dired-recursive-deletes 'always)
  230. (setq dired-recursive-copies 'always)
  231. ;; if there is a dired buffer displayed in the next window, use its
  232. ;; current subdir, instead of the current subdir of this dired buffer
  233. (setq dired-dwim-target t)
  234. ;; enable some really cool extensions like C-x C-j(dired-jump)
  235. (require 'dired-x)
  236. ;; ediff - don't start another frame
  237. (require 'ediff)
  238. (setq ediff-window-setup-function 'ediff-setup-windows-plain)
  239. ;; clean up obsolete buffers automatically
  240. (require 'midnight)
  241. ;; smarter kill-ring navigation
  242. (require 'browse-kill-ring)
  243. (browse-kill-ring-default-keybindings)
  244. (global-set-key (kbd "s-y") 'browse-kill-ring)
  245. ;; automatically indenting yanked text if in programming-modes
  246. (defvar yank-indent-modes
  247. '(LaTeX-mode TeX-mode)
  248. "Modes in which to indent regions that are yanked (or yank-popped).
  249. Only modes that don't derive from `prog-mode' should be listed here.")
  250. (defvar yank-indent-blacklisted-modes
  251. '(python-mode slim-mode haml-mode)
  252. "Modes for which auto-indenting is suppressed.")
  253. (defvar yank-advised-indent-threshold 1000
  254. "Threshold (# chars) over which indentation does not automatically occur.")
  255. (defun yank-advised-indent-function (beg end)
  256. "Do indentation, as long as the region isn't too large."
  257. (if (<= (- end beg) yank-advised-indent-threshold)
  258. (indent-region beg end nil)))
  259. (defadvice yank (after yank-indent activate)
  260. "If current mode is one of 'yank-indent-modes,
  261. indent yanked text (with prefix arg don't indent)."
  262. (if (and (not (ad-get-arg 0))
  263. (not (member major-mode yank-indent-blacklisted-modes))
  264. (or (derived-mode-p 'prog-mode)
  265. (member major-mode yank-indent-modes)))
  266. (let ((transient-mark-mode nil))
  267. (yank-advised-indent-function (region-beginning) (region-end)))))
  268. (defadvice yank-pop (after yank-pop-indent activate)
  269. "If current mode is one of `yank-indent-modes',
  270. indent yanked text (with prefix arg don't indent)."
  271. (when (and (not (ad-get-arg 0))
  272. (not (member major-mode yank-indent-blacklisted-modes))
  273. (or (derived-mode-p 'prog-mode)
  274. (member major-mode yank-indent-modes)))
  275. (let ((transient-mark-mode nil))
  276. (yank-advised-indent-function (region-beginning) (region-end)))))
  277. ;; abbrev config
  278. (add-hook 'text-mode-hook 'abbrev-mode)
  279. ;; make a shell script executable automatically on save
  280. (add-hook 'after-save-hook
  281. 'executable-make-buffer-file-executable-if-script-p)
  282. ;; .zsh file is shell script too
  283. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  284. ;; whitespace-mode config
  285. (require 'whitespace)
  286. (setq whitespace-line-column 80) ;; limit line length
  287. (setq whitespace-style '(face tabs empty trailing lines-tail))
  288. ;; saner regex syntax
  289. (require 're-builder)
  290. (setq reb-re-syntax 'string)
  291. (require 'eshell)
  292. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  293. (setq semanticdb-default-save-directory
  294. (expand-file-name "semanticdb" prelude-savefile-dir))
  295. ;; Compilation from Emacs
  296. (defun prelude-colorize-compilation-buffer ()
  297. "Colorize a compilation mode buffer."
  298. (interactive)
  299. ;; we don't want to mess with child modes such as grep-mode, ack, ag, etc
  300. (when (eq major-mode 'compilation-mode)
  301. (let ((inhibit-read-only t))
  302. (ansi-color-apply-on-region (point-min) (point-max)))))
  303. (require 'compile)
  304. (setq compilation-ask-about-save nil ; Just save before compiling
  305. compilation-always-kill t ; Just kill old compile processes before
  306. ; starting the new one
  307. compilation-scroll-output 'first-error ; Automatically scroll to first
  308. ; error
  309. )
  310. ;; Colorize output of Compilation Mode, see
  311. ;; http://stackoverflow.com/a/3072831/355252
  312. (require 'ansi-color)
  313. (add-hook 'compilation-filter-hook #'prelude-colorize-compilation-buffer)
  314. ;; enable Prelude's keybindings
  315. (prelude-global-mode t)
  316. ;; sensible undo
  317. (global-undo-tree-mode)
  318. (diminish 'undo-tree-mode)
  319. ;; enable winner-mode to manage window configurations
  320. (winner-mode +1)
  321. ;; diff-hl
  322. (global-diff-hl-mode +1)
  323. (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
  324. ;; easy-kill
  325. (global-set-key [remap kill-ring-save] 'easy-kill)
  326. (global-set-key [remap mark-sexp] 'easy-mark)
  327. (provide 'prelude-editor)
  328. ;;; prelude-editor.el ends here