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.

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