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.

379 lines
13 KiB

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