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.

394 lines
14 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. ;; ignore magit's commit message files
  111. (add-to-list 'recentf-exclude "COMMIT_EDITMSG\\'")
  112. (setq recentf-auto-cleanup 'never) ;; disable before we start recentf!
  113. (recentf-mode +1)
  114. ;; use shift + arrow keys to switch between visible buffers
  115. (require 'windmove)
  116. (windmove-default-keybindings)
  117. ;; automatically save buffers associated with files on buffer switch
  118. ;; and on windows switch
  119. (defun prelude-auto-save-command ()
  120. "Save the current buffer if `prelude-auto-save' is not nil."
  121. (when (and prelude-auto-save
  122. buffer-file-name
  123. (buffer-modified-p (current-buffer))
  124. (file-writable-p buffer-file-name))
  125. (save-buffer)))
  126. (defmacro advise-commands (advice-name commands &rest body)
  127. "Apply advice named ADVICE-NAME to multiple COMMANDS.
  128. The body of the advice is in BODY."
  129. `(progn
  130. ,@(mapcar (lambda (command)
  131. `(defadvice ,command (before ,(intern (concat (symbol-name command) "-" advice-name)) activate)
  132. ,@body))
  133. commands)))
  134. ;; advise all window switching functions
  135. (advise-commands "auto-save"
  136. (switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
  137. (prelude-auto-save-command))
  138. (add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)
  139. ;; Autosave buffers when focus is lost
  140. (defun prelude-save-all-buffers ()
  141. "Save all modified buffers, without prompts."
  142. (save-some-buffers 'dont-ask))
  143. (when (version<= "24.4" emacs-version)
  144. (add-hook 'focus-out-hook 'prelude-save-all-buffers))
  145. ;; highlight the current line
  146. (global-hl-line-mode +1)
  147. (require 'volatile-highlights)
  148. (volatile-highlights-mode t)
  149. (diminish 'volatile-highlights-mode)
  150. ;; tramp, for sudo access
  151. (require 'tramp)
  152. ;; keep in mind known issues with zsh - see emacs wiki
  153. (setq tramp-default-method "ssh")
  154. (set-default 'imenu-auto-rescan t)
  155. ;; flyspell-mode does spell-checking on the fly as you type
  156. (require 'flyspell)
  157. (setq ispell-program-name "aspell" ; use aspell instead of ispell
  158. ispell-extra-args '("--sug-mode=ultra"))
  159. (defun prelude-enable-flyspell ()
  160. "Enable command `flyspell-mode' if `prelude-flyspell' is not nil."
  161. (when (and prelude-flyspell (executable-find ispell-program-name))
  162. (flyspell-mode +1)))
  163. (defun prelude-cleanup-maybe ()
  164. "Invoke `whitespace-cleanup' if `prelude-clean-whitespace-on-save' is not nil."
  165. (when prelude-clean-whitespace-on-save
  166. (whitespace-cleanup)))
  167. (defun prelude-enable-whitespace ()
  168. "Enable `whitespace-mode' if `prelude-whitespace' is not nil."
  169. (when prelude-whitespace
  170. ;; keep the whitespace decent all the time (in this buffer)
  171. (add-hook 'before-save-hook 'prelude-cleanup-maybe nil t)
  172. (whitespace-mode +1)))
  173. (add-hook 'text-mode-hook 'prelude-enable-flyspell)
  174. (add-hook 'text-mode-hook 'prelude-enable-whitespace)
  175. ;; enable narrowing commands
  176. (put 'narrow-to-region 'disabled nil)
  177. (put 'narrow-to-page 'disabled nil)
  178. (put 'narrow-to-defun 'disabled nil)
  179. ;; enabled change region case commands
  180. (put 'upcase-region 'disabled nil)
  181. (put 'downcase-region 'disabled nil)
  182. ;; enable erase-buffer command
  183. (put 'erase-buffer 'disabled nil)
  184. (require 'expand-region)
  185. ;; bookmarks
  186. (require 'bookmark)
  187. (setq bookmark-default-file (expand-file-name "bookmarks" prelude-savefile-dir)
  188. bookmark-save-flag 1)
  189. ;; projectile is a project management mode
  190. (require 'projectile)
  191. (setq projectile-cache-file (expand-file-name "projectile.cache" prelude-savefile-dir))
  192. (projectile-global-mode t)
  193. ;; anzu-mode enhances isearch by showing total matches and current match position
  194. (require 'anzu)
  195. (diminish 'anzu-mode)
  196. (global-anzu-mode)
  197. ;; shorter aliases for ack-and-a-half commands
  198. (defalias 'ack 'ack-and-a-half)
  199. (defalias 'ack-same 'ack-and-a-half-same)
  200. (defalias 'ack-find-file 'ack-and-a-half-find-file)
  201. (defalias 'ack-find-file-same 'ack-and-a-half-find-file-same)
  202. ;; dired - reuse current buffer by pressing 'a'
  203. (put 'dired-find-alternate-file 'disabled nil)
  204. ;; always delete and copy recursively
  205. (setq dired-recursive-deletes 'always)
  206. (setq dired-recursive-copies 'always)
  207. ;; if there is a dired buffer displayed in the next window, use its
  208. ;; current subdir, instead of the current subdir of this dired buffer
  209. (setq dired-dwim-target t)
  210. ;; enable some really cool extensions like C-x C-j(dired-jump)
  211. (require 'dired-x)
  212. ;; ediff - don't start another frame
  213. (require 'ediff)
  214. (setq ediff-window-setup-function 'ediff-setup-windows-plain)
  215. ;; clean up obsolete buffers automatically
  216. (require 'midnight)
  217. ;; smarter kill-ring navigation
  218. (require 'browse-kill-ring)
  219. (browse-kill-ring-default-keybindings)
  220. (global-set-key (kbd "s-y") 'browse-kill-ring)
  221. ;; automatically indenting yanked text if in programming-modes
  222. (defvar yank-indent-modes
  223. '(LaTeX-mode TeX-mode)
  224. "Modes in which to indent regions that are yanked (or yank-popped).
  225. Only modes that don't derive from `prog-mode' should be listed here.")
  226. (defvar yank-indent-blacklisted-modes
  227. '(python-mode slim-mode haml-mode)
  228. "Modes for which auto-indenting is suppressed.")
  229. (defvar yank-advised-indent-threshold 1000
  230. "Threshold (# chars) over which indentation does not automatically occur.")
  231. (defun yank-advised-indent-function (beg end)
  232. "Do indentation, as long as the region isn't too large."
  233. (if (<= (- end beg) yank-advised-indent-threshold)
  234. (indent-region beg end nil)))
  235. (defadvice yank (after yank-indent activate)
  236. "If current mode is one of 'yank-indent-modes,
  237. indent yanked text (with prefix arg don't indent)."
  238. (if (and (not (ad-get-arg 0))
  239. (not (member major-mode yank-indent-blacklisted-modes))
  240. (or (derived-mode-p 'prog-mode)
  241. (member major-mode yank-indent-modes)))
  242. (let ((transient-mark-mode nil))
  243. (yank-advised-indent-function (region-beginning) (region-end)))))
  244. (defadvice yank-pop (after yank-pop-indent activate)
  245. "If current mode is one of `yank-indent-modes',
  246. indent yanked text (with prefix arg don't indent)."
  247. (when (and (not (ad-get-arg 0))
  248. (not (member major-mode yank-indent-blacklisted-modes))
  249. (or (derived-mode-p 'prog-mode)
  250. (member major-mode yank-indent-modes)))
  251. (let ((transient-mark-mode nil))
  252. (yank-advised-indent-function (region-beginning) (region-end)))))
  253. ;; abbrev config
  254. (add-hook 'text-mode-hook 'abbrev-mode)
  255. ;; make a shell script executable automatically on save
  256. (add-hook 'after-save-hook
  257. 'executable-make-buffer-file-executable-if-script-p)
  258. ;; .zsh file is shell script too
  259. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  260. ;; whitespace-mode config
  261. (require 'whitespace)
  262. (setq whitespace-line-column 80) ;; limit line length
  263. (setq whitespace-style '(face tabs empty trailing lines-tail))
  264. ;; saner regex syntax
  265. (require 're-builder)
  266. (setq reb-re-syntax 'string)
  267. (require 'eshell)
  268. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  269. (setq semanticdb-default-save-directory
  270. (expand-file-name "semanticdb" prelude-savefile-dir))
  271. ;; Compilation from Emacs
  272. (defun prelude-colorize-compilation-buffer ()
  273. "Colorize a compilation mode buffer."
  274. (interactive)
  275. ;; we don't want to mess with child modes such as grep-mode, ack, ag, etc
  276. (when (eq major-mode 'compilation-mode)
  277. (let ((inhibit-read-only t))
  278. (ansi-color-apply-on-region (point-min) (point-max)))))
  279. (require 'compile)
  280. (setq compilation-ask-about-save nil ; Just save before compiling
  281. compilation-always-kill t ; Just kill old compile processes before
  282. ; starting the new one
  283. compilation-scroll-output 'first-error ; Automatically scroll to first
  284. ; error
  285. )
  286. ;; Colorize output of Compilation Mode, see
  287. ;; http://stackoverflow.com/a/3072831/355252
  288. (require 'ansi-color)
  289. (add-hook 'compilation-filter-hook #'prelude-colorize-compilation-buffer)
  290. ;; enable Prelude's keybindings
  291. (prelude-global-mode t)
  292. ;; sensible undo
  293. (global-undo-tree-mode)
  294. (diminish 'undo-tree-mode)
  295. ;; enable winner-mode to manage window configurations
  296. (winner-mode +1)
  297. ;; diff-hl
  298. (global-diff-hl-mode +1)
  299. (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
  300. ;; easy-kill
  301. (global-set-key [remap kill-ring-save] 'easy-kill)
  302. (global-set-key [remap mark-sexp] 'easy-mark)
  303. ;;
  304. (require 'operate-on-number)
  305. (smartrep-define-key global-map "C-c ."
  306. '(("+" . apply-operation-to-number-at-point)
  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. ("'" . operate-on-number-at-point)))
  314. (provide 'prelude-editor)
  315. ;;; prelude-editor.el ends here