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.

403 lines
13 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
13 years ago
14 years ago
13 years ago
14 years ago
14 years ago
14 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 pairing for all
  87. (require 'smartparens-config)
  88. (setq sp-base-key-bindings 'paredit)
  89. (setq sp-autoskip-closing-pair 'always)
  90. (smartparens-global-mode +1)
  91. ;; diminish keeps the modeline tidy
  92. (require 'diminish)
  93. ;; meaningful names for buffers with the same name
  94. (require 'uniquify)
  95. (setq uniquify-buffer-name-style 'forward)
  96. (setq uniquify-separator "/")
  97. (setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
  98. (setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
  99. ;; saveplace remembers your location in a file when saving files
  100. (require 'saveplace)
  101. (setq save-place-file (expand-file-name "saveplace" prelude-savefile-dir))
  102. ;; activate it for all buffers
  103. (setq-default save-place t)
  104. ;; savehist keeps track of some history
  105. (require 'savehist)
  106. (setq savehist-additional-variables
  107. ;; search entries
  108. '(search ring regexp-search-ring)
  109. ;; save every minute
  110. savehist-autosave-interval 60
  111. ;; keep the home clean
  112. savehist-file (expand-file-name "savehist" prelude-savefile-dir))
  113. (savehist-mode +1)
  114. ;; save recent files
  115. (require 'recentf)
  116. (setq recentf-save-file (expand-file-name "recentf" prelude-savefile-dir)
  117. recentf-max-saved-items 500
  118. recentf-max-menu-items 15)
  119. (recentf-mode +1)
  120. ;; use shift + arrow keys to switch between visible buffers
  121. (require 'windmove)
  122. (windmove-default-keybindings)
  123. ;; automatically save buffers associated with files on buffer switch
  124. ;; and on windows switch
  125. (defun prelude-auto-save-command ()
  126. "Save the current buffer if `prelude-auto-save' is not nil."
  127. (when (and prelude-auto-save
  128. buffer-file-name
  129. (buffer-modified-p (current-buffer))
  130. (file-writable-p buffer-file-name))
  131. (save-buffer)))
  132. (defmacro advise-commands (advice-name commands &rest body)
  133. "Apply advice named ADVICE-NAME to multiple COMMANDS.
  134. The body of the advice is in BODY."
  135. `(progn
  136. ,@(mapcar (lambda (command)
  137. `(defadvice ,command (before ,(intern (concat (symbol-name command) "-" advice-name)) activate)
  138. ,@body))
  139. commands)))
  140. ;; advise all window switching functions
  141. (advise-commands "auto-save"
  142. (switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
  143. (prelude-auto-save-command))
  144. (add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)
  145. ;; show-paren-mode: subtle highlighting of matching parens (global-mode)
  146. (require 'paren)
  147. (setq show-paren-style 'parenthesis)
  148. (show-paren-mode +1)
  149. ;; highlight the current line
  150. (global-hl-line-mode +1)
  151. (require 'volatile-highlights)
  152. (volatile-highlights-mode t)
  153. (diminish 'volatile-highlights-mode)
  154. ;; note - this should be after volatile-highlights is required
  155. ;; add the ability to copy and cut the current line, without marking it
  156. (defadvice kill-ring-save (before smart-copy activate compile)
  157. "When called interactively with no active region, copy a single line instead."
  158. (interactive
  159. (if mark-active (list (region-beginning) (region-end))
  160. (message "Copied line")
  161. (list (line-beginning-position)
  162. (line-end-position)))))
  163. (defadvice kill-region (before smart-cut activate compile)
  164. "When called interactively with no active region, kill a single line instead."
  165. (interactive
  166. (if mark-active (list (region-beginning) (region-end))
  167. (list (line-beginning-position)
  168. (line-beginning-position 2)))))
  169. ;; tramp, for sudo access
  170. (require 'tramp)
  171. ;; keep in mind known issues with zsh - see emacs wiki
  172. (setq tramp-default-method "ssh")
  173. ;; ido-mode
  174. (require 'ido)
  175. (require 'ido-ubiquitous)
  176. (require 'flx-ido)
  177. (setq ido-enable-prefix nil
  178. ido-enable-flex-matching t
  179. ido-create-new-buffer 'always
  180. ido-use-filename-at-point 'guess
  181. ido-max-prospects 10
  182. ido-save-directory-list-file (expand-file-name "ido.hist" prelude-savefile-dir)
  183. ido-default-file-method 'selected-window
  184. ido-auto-merge-work-directories-length -1)
  185. (ido-mode +1)
  186. (ido-ubiquitous-mode +1)
  187. ;; smarter fuzzy matching for ido
  188. (flx-ido-mode +1)
  189. ;; disable ido faces to see flx highlights
  190. (setq ido-use-faces nil)
  191. ;; smex, remember recently and most frequently used commands
  192. (require 'smex)
  193. (setq smex-save-file (expand-file-name ".smex-items" prelude-savefile-dir))
  194. (smex-initialize)
  195. (global-set-key (kbd "M-x") 'smex)
  196. (global-set-key (kbd "M-X") 'smex-major-mode-commands)
  197. (set-default 'imenu-auto-rescan t)
  198. ;; flyspell-mode does spell-checking on the fly as you type
  199. (require 'flyspell)
  200. (setq ispell-program-name "aspell" ; use aspell instead of ispell
  201. ispell-extra-args '("--sug-mode=ultra"))
  202. (defun prelude-enable-flyspell ()
  203. "Enable command `flyspell-mode' if `prelude-flyspell' is not nil."
  204. (when (and prelude-flyspell (executable-find ispell-program-name))
  205. (flyspell-mode +1)))
  206. (defun prelude-cleanup-maybe ()
  207. "Invoke `whitespace-cleanup' if `prelude-clean-whitespace-on-save' is not nil."
  208. (when prelude-clean-whitespace-on-save
  209. (whitespace-cleanup)))
  210. (defun prelude-enable-whitespace ()
  211. "Enable `whitespace-mode' if `prelude-whitespace' is not nil."
  212. (when prelude-whitespace
  213. ;; keep the whitespace decent all the time (in this buffer)
  214. (add-hook 'before-save-hook 'prelude-cleanup-maybe nil t)
  215. (whitespace-mode +1)))
  216. (add-hook 'text-mode-hook 'prelude-enable-flyspell)
  217. (add-hook 'text-mode-hook 'prelude-enable-whitespace)
  218. ;; enable narrowing commands
  219. (put 'narrow-to-region 'disabled nil)
  220. (put 'narrow-to-page 'disabled nil)
  221. (put 'narrow-to-defun 'disabled nil)
  222. ;; enabled change region case commands
  223. (put 'upcase-region 'disabled nil)
  224. (put 'downcase-region 'disabled nil)
  225. ;; enable erase-buffer command
  226. (put 'erase-buffer 'disabled nil)
  227. (require 'expand-region)
  228. ;; bookmarks
  229. (require 'bookmark)
  230. (setq bookmark-default-file (expand-file-name "bookmarks" prelude-savefile-dir)
  231. bookmark-save-flag 1)
  232. ;; projectile is a project management mode
  233. (require 'projectile)
  234. (setq projectile-cache-file (expand-file-name "projectile.cache" prelude-savefile-dir))
  235. (projectile-global-mode t)
  236. (diminish 'projectile-mode "Prjl")
  237. (require 'helm-misc)
  238. (require 'helm-projectile)
  239. (defun helm-prelude ()
  240. "Preconfigured `helm'."
  241. (interactive)
  242. (condition-case nil
  243. (if (projectile-project-root)
  244. (helm-projectile)
  245. ;; otherwise fallback to helm-mini
  246. (helm-mini))
  247. ;; fall back to helm mini if an error occurs (usually in projectile-project-root)
  248. (error (helm-mini))))
  249. ;; shorter aliases for ack-and-a-half commands
  250. (defalias 'ack 'ack-and-a-half)
  251. (defalias 'ack-same 'ack-and-a-half-same)
  252. (defalias 'ack-find-file 'ack-and-a-half-find-file)
  253. (defalias 'ack-find-file-same 'ack-and-a-half-find-file-same)
  254. ;; dired - reuse current buffer by pressing 'a'
  255. (put 'dired-find-alternate-file 'disabled nil)
  256. ;; always delete and copy recursively
  257. (setq dired-recursive-deletes 'always)
  258. (setq dired-recursive-copies 'always)
  259. ;; if there is a dired buffer displayed in the next window, use its
  260. ;; current subdir, instead of the current subdir of this dired buffer
  261. (setq dired-dwim-target t)
  262. ;; enable some really cool extensions like C-x C-j(dired-jump)
  263. (require 'dired-x)
  264. ;; ediff - don't start another frame
  265. (require 'ediff)
  266. (setq ediff-window-setup-function 'ediff-setup-windows-plain)
  267. ;; clean up obsolete buffers automatically
  268. (require 'midnight)
  269. ;; automatically indenting yanked text if in programming-modes
  270. (defvar yank-indent-modes
  271. '(LaTeX-mode TeX-mode)
  272. "Modes in which to indent regions that are yanked (or yank-popped).
  273. Only modes that don't derive from `prog-mode' should be listed here.")
  274. (defvar yank-indent-blacklisted-modes
  275. '(python-mode slim-mode haml-mode)
  276. "Modes for which auto-indenting is suppressed.")
  277. (defvar yank-advised-indent-threshold 1000
  278. "Threshold (# chars) over which indentation does not automatically occur.")
  279. (defun yank-advised-indent-function (beg end)
  280. "Do indentation, as long as the region isn't too large."
  281. (if (<= (- end beg) yank-advised-indent-threshold)
  282. (indent-region beg end nil)))
  283. (defadvice yank (after yank-indent activate)
  284. "If current mode is one of 'yank-indent-modes,
  285. indent yanked text (with prefix arg don't indent)."
  286. (if (and (not (ad-get-arg 0))
  287. (not (member major-mode yank-indent-blacklisted-modes))
  288. (or (derived-mode-p 'prog-mode)
  289. (member major-mode yank-indent-modes)))
  290. (let ((transient-mark-mode nil))
  291. (yank-advised-indent-function (region-beginning) (region-end)))))
  292. (defadvice yank-pop (after yank-pop-indent activate)
  293. "If current mode is one of 'yank-indent-modes,
  294. indent yanked text (with prefix arg don't indent)."
  295. (if (and (not (ad-get-arg 0))
  296. (not (member major-mode yank-indent-blacklisted-modes))
  297. (or (derived-mode-p 'prog-mode)
  298. (member major-mode yank-indent-modes)))
  299. (let ((transient-mark-mode nil))
  300. (yank-advised-indent-function (region-beginning) (region-end)))))
  301. ;; abbrev config
  302. (add-hook 'text-mode-hook 'abbrev-mode)
  303. ;; make a shell script executable automatically on save
  304. (add-hook 'after-save-hook
  305. 'executable-make-buffer-file-executable-if-script-p)
  306. ;; .zsh file is shell script too
  307. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  308. ;; whitespace-mode config
  309. (require 'whitespace)
  310. (setq whitespace-line-column 80) ;; limit line length
  311. (setq whitespace-style '(face tabs empty trailing lines-tail))
  312. ;; saner regex syntax
  313. (require 're-builder)
  314. (setq reb-re-syntax 'string)
  315. (require 'eshell)
  316. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  317. (setq semanticdb-default-save-directory
  318. (expand-file-name "semanticdb" prelude-savefile-dir))
  319. ;; enable Prelude's keybindings
  320. (prelude-global-mode t)
  321. ;; sensible undo
  322. (global-undo-tree-mode)
  323. (diminish 'undo-tree-mode)
  324. ;; enable winner-mode to manage window configurations
  325. (winner-mode +1)
  326. (provide 'prelude-editor)
  327. ;;; prelude-editor.el ends here