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.

399 lines
14 KiB

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