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.

415 lines
14 KiB

13 years ago
14 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
  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 & query-replace by showing total matches and current match position
  200. (require 'anzu)
  201. (diminish 'anzu-mode)
  202. (global-anzu-mode)
  203. (global-set-key (kbd "M-%") 'anzu-query-replace)
  204. (global-set-key (kbd "C-M-%") 'anzu-query-replace-regexp)
  205. ;; dired - reuse current buffer by pressing 'a'
  206. (put 'dired-find-alternate-file 'disabled nil)
  207. ;; always delete and copy recursively
  208. (setq dired-recursive-deletes 'always)
  209. (setq dired-recursive-copies 'always)
  210. ;; if there is a dired buffer displayed in the next window, use its
  211. ;; current subdir, instead of the current subdir of this dired buffer
  212. (setq dired-dwim-target t)
  213. ;; enable some really cool extensions like C-x C-j(dired-jump)
  214. (require 'dired-x)
  215. ;; ediff - don't start another frame
  216. (require 'ediff)
  217. (setq ediff-window-setup-function 'ediff-setup-windows-plain)
  218. ;; clean up obsolete buffers automatically
  219. (require 'midnight)
  220. ;; smarter kill-ring navigation
  221. (require 'browse-kill-ring)
  222. (browse-kill-ring-default-keybindings)
  223. (global-set-key (kbd "s-y") 'browse-kill-ring)
  224. (defadvice exchange-point-and-mark (before deactivate-mark activate compile)
  225. "When called with no active region, do not activate mark."
  226. (interactive
  227. (list (not (region-active-p)))))
  228. (defmacro with-region-or-buffer (func)
  229. "When called with no active region, call FUNC on current buffer."
  230. `(defadvice ,func (before with-region-or-buffer activate compile)
  231. (interactive
  232. (if mark-active
  233. (list (region-beginning) (region-end))
  234. (list (point-min) (point-max))))))
  235. (with-region-or-buffer indent-region)
  236. (with-region-or-buffer untabify)
  237. ;; automatically indenting yanked text if in programming-modes
  238. (defun yank-advised-indent-function (beg end)
  239. "Do indentation, as long as the region isn't too large."
  240. (if (<= (- end beg) prelude-yank-indent-threshold)
  241. (indent-region beg end nil)))
  242. (advise-commands "indent" (yank yank-pop) after
  243. "If current mode is one of `prelude-yank-indent-modes',
  244. indent yanked text (with prefix arg don't indent)."
  245. (if (and (not (ad-get-arg 0))
  246. (not (member major-mode prelude-indent-sensitive-modes))
  247. (or (derived-mode-p 'prog-mode)
  248. (member major-mode prelude-yank-indent-modes)))
  249. (let ((transient-mark-mode nil))
  250. (yank-advised-indent-function (region-beginning) (region-end)))))
  251. ;; abbrev config
  252. (add-hook 'text-mode-hook 'abbrev-mode)
  253. ;; make a shell script executable automatically on save
  254. (add-hook 'after-save-hook
  255. 'executable-make-buffer-file-executable-if-script-p)
  256. ;; .zsh file is shell script too
  257. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  258. ;; whitespace-mode config
  259. (require 'whitespace)
  260. (setq whitespace-line-column 80) ;; limit line length
  261. (setq whitespace-style '(face tabs empty trailing lines-tail))
  262. ;; saner regex syntax
  263. (require 're-builder)
  264. (setq reb-re-syntax 'string)
  265. (require 'eshell)
  266. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  267. (setq semanticdb-default-save-directory
  268. (expand-file-name "semanticdb" prelude-savefile-dir))
  269. ;; Compilation from Emacs
  270. (defun prelude-colorize-compilation-buffer ()
  271. "Colorize a compilation mode buffer."
  272. (interactive)
  273. ;; we don't want to mess with child modes such as grep-mode, ack, ag, etc
  274. (when (eq major-mode 'compilation-mode)
  275. (let ((inhibit-read-only t))
  276. (ansi-color-apply-on-region (point-min) (point-max)))))
  277. (require 'compile)
  278. (setq compilation-ask-about-save nil ; Just save before compiling
  279. compilation-always-kill t ; Just kill old compile processes before
  280. ; starting the new one
  281. compilation-scroll-output 'first-error ; Automatically scroll to first
  282. ; error
  283. )
  284. ;; Colorize output of Compilation Mode, see
  285. ;; http://stackoverflow.com/a/3072831/355252
  286. (require 'ansi-color)
  287. (add-hook 'compilation-filter-hook #'prelude-colorize-compilation-buffer)
  288. ;; enable Prelude's keybindings
  289. (prelude-global-mode t)
  290. ;; sensible undo
  291. (global-undo-tree-mode)
  292. (diminish 'undo-tree-mode)
  293. ;; enable winner-mode to manage window configurations
  294. (winner-mode +1)
  295. ;; diff-hl
  296. (global-diff-hl-mode +1)
  297. (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
  298. ;; easy-kill
  299. (global-set-key [remap kill-ring-save] 'easy-kill)
  300. (global-set-key [remap mark-sexp] 'easy-mark)
  301. ;; operate-on-number
  302. (require 'operate-on-number)
  303. (smartrep-define-key global-map "C-c ."
  304. '(("+" . apply-operation-to-number-at-point)
  305. ("-" . apply-operation-to-number-at-point)
  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. ("%" . apply-operation-to-number-at-point)
  314. ("'" . operate-on-number-at-point)))
  315. (defadvice server-visit-files (before parse-numbers-in-lines (files proc &optional nowait) activate)
  316. "Open file with emacsclient with cursors positioned on requested line.
  317. Most of console-based utilities prints filename in format
  318. 'filename:linenumber'. So you may wish to open filename in that format.
  319. Just call:
  320. emacsclient filename:linenumber
  321. and file 'filename' will be opened and cursor set on line 'linenumber'"
  322. (ad-set-arg 0
  323. (mapcar (lambda (fn)
  324. (let ((name (car fn)))
  325. (if (string-match "^\\(.*?\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?$" name)
  326. (cons
  327. (match-string 1 name)
  328. (cons (string-to-number (match-string 2 name))
  329. (string-to-number (or (match-string 3 name) ""))))
  330. fn))) files)))
  331. (provide 'prelude-editor)
  332. ;;; prelude-editor.el ends here