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.

438 lines
15 KiB

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-2016 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. ;; autosave the undo-tree history
  50. (setq undo-tree-history-directory-alist
  51. `((".*" . ,temporary-file-directory)))
  52. (setq undo-tree-auto-save-history t)
  53. ;; revert buffers automatically when underlying files are changed externally
  54. (global-auto-revert-mode t)
  55. ;; hippie expand is dabbrev expand on steroids
  56. (setq hippie-expand-try-functions-list '(try-expand-dabbrev
  57. try-expand-dabbrev-all-buffers
  58. try-expand-dabbrev-from-kill
  59. try-complete-file-name-partially
  60. try-complete-file-name
  61. try-expand-all-abbrevs
  62. try-expand-list
  63. try-expand-line
  64. try-complete-lisp-symbol-partially
  65. try-complete-lisp-symbol))
  66. ;; smart tab behavior - indent or complete
  67. (setq tab-always-indent 'complete)
  68. ;; smart pairing for all
  69. (require 'smartparens-config)
  70. (setq sp-base-key-bindings 'paredit)
  71. (setq sp-autoskip-closing-pair 'always)
  72. (setq sp-hybrid-kill-entire-symbol nil)
  73. (sp-use-paredit-bindings)
  74. (show-smartparens-global-mode +1)
  75. (define-key prog-mode-map (kbd "M-(") (prelude-wrap-with "("))
  76. ;; FIXME: pick terminal friendly binding
  77. ;; (define-key prog-mode-map (kbd "M-[") (prelude-wrap-with "["))
  78. (define-key prog-mode-map (kbd "M-\"") (prelude-wrap-with "\""))
  79. ;; disable annoying blink-matching-paren
  80. (setq blink-matching-paren nil)
  81. ;; diminish keeps the modeline tidy
  82. (require 'diminish)
  83. ;; meaningful names for buffers with the same name
  84. (require 'uniquify)
  85. (setq uniquify-buffer-name-style 'forward)
  86. (setq uniquify-separator "/")
  87. (setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
  88. (setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
  89. ;; saveplace remembers your location in a file when saving files
  90. (require 'saveplace)
  91. (setq save-place-file (expand-file-name "saveplace" prelude-savefile-dir))
  92. ;; activate it for all buffers
  93. (setq-default save-place t)
  94. ;; savehist keeps track of some history
  95. (require 'savehist)
  96. (setq savehist-additional-variables
  97. ;; search entries
  98. '(search-ring regexp-search-ring)
  99. ;; save every minute
  100. savehist-autosave-interval 60
  101. ;; keep the home clean
  102. savehist-file (expand-file-name "savehist" prelude-savefile-dir))
  103. (savehist-mode +1)
  104. ;; save recent files
  105. (require 'recentf)
  106. (setq recentf-save-file (expand-file-name "recentf" prelude-savefile-dir)
  107. recentf-max-saved-items 500
  108. recentf-max-menu-items 15
  109. ;; disable recentf-cleanup on Emacs start, because it can cause
  110. ;; problems with remote files
  111. recentf-auto-cleanup 'never)
  112. (defun prelude-recentf-exclude-p (file)
  113. "A predicate to decide whether to exclude FILE from recentf."
  114. (let ((file-dir (file-truename (file-name-directory file))))
  115. (-any-p (lambda (dir)
  116. (string-prefix-p dir file-dir))
  117. (mapcar 'file-truename (list prelude-savefile-dir package-user-dir)))))
  118. (add-to-list 'recentf-exclude 'prelude-recentf-exclude-p)
  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 class &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 (,class ,(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. before
  144. (prelude-auto-save-command))
  145. (add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)
  146. (when (version<= "24.4" emacs-version)
  147. (add-hook 'focus-out-hook 'prelude-auto-save-command))
  148. (defadvice set-buffer-major-mode (after set-major-mode activate compile)
  149. "Set buffer major mode according to `auto-mode-alist'."
  150. (let* ((name (buffer-name buffer))
  151. (mode (assoc-default name auto-mode-alist 'string-match)))
  152. (when (and mode (consp mode))
  153. (setq mode (car mode)))
  154. (with-current-buffer buffer (if mode (funcall mode)))))
  155. ;; highlight the current line
  156. (global-hl-line-mode +1)
  157. (require 'volatile-highlights)
  158. (volatile-highlights-mode t)
  159. (diminish 'volatile-highlights-mode)
  160. ;; note - this should be after volatile-highlights is required
  161. ;; add the ability to cut the current line, without marking it
  162. (require 'rect)
  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) rectangle-mark-mode)
  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. (set-default 'imenu-auto-rescan t)
  174. ;; flyspell-mode does spell-checking on the fly as you type
  175. (require 'flyspell)
  176. (setq ispell-program-name "aspell" ; use aspell instead of ispell
  177. ispell-extra-args '("--sug-mode=ultra"))
  178. (defun prelude-enable-flyspell ()
  179. "Enable command `flyspell-mode' if `prelude-flyspell' is not nil."
  180. (when (and prelude-flyspell (executable-find ispell-program-name))
  181. (flyspell-mode +1)))
  182. (defun prelude-cleanup-maybe ()
  183. "Invoke `whitespace-cleanup' if `prelude-clean-whitespace-on-save' is not nil."
  184. (when prelude-clean-whitespace-on-save
  185. (whitespace-cleanup)))
  186. (defun prelude-enable-whitespace ()
  187. "Enable `whitespace-mode' if `prelude-whitespace' is not nil."
  188. (when prelude-whitespace
  189. ;; keep the whitespace decent all the time (in this buffer)
  190. (add-hook 'before-save-hook 'prelude-cleanup-maybe nil t)
  191. (whitespace-mode +1)))
  192. (add-hook 'text-mode-hook 'prelude-enable-flyspell)
  193. (add-hook 'text-mode-hook 'prelude-enable-whitespace)
  194. ;; enable narrowing commands
  195. (put 'narrow-to-region 'disabled nil)
  196. (put 'narrow-to-page 'disabled nil)
  197. (put 'narrow-to-defun 'disabled nil)
  198. ;; enabled change region case commands
  199. (put 'upcase-region 'disabled nil)
  200. (put 'downcase-region 'disabled nil)
  201. ;; enable erase-buffer command
  202. (put 'erase-buffer 'disabled nil)
  203. (require 'expand-region)
  204. ;; bookmarks
  205. (require 'bookmark)
  206. (setq bookmark-default-file (expand-file-name "bookmarks" prelude-savefile-dir)
  207. bookmark-save-flag 1)
  208. ;; projectile is a project management mode
  209. (require 'projectile)
  210. (setq projectile-cache-file (expand-file-name "projectile.cache" prelude-savefile-dir))
  211. (projectile-global-mode t)
  212. ;; avy allows us to effectively navigate to visible things
  213. (require 'avy)
  214. (setq avy-background t)
  215. (setq avy-style 'at-full)
  216. ;; anzu-mode enhances isearch & query-replace by showing total matches and current match position
  217. (require 'anzu)
  218. (diminish 'anzu-mode)
  219. (global-anzu-mode)
  220. (global-set-key (kbd "M-%") 'anzu-query-replace)
  221. (global-set-key (kbd "C-M-%") 'anzu-query-replace-regexp)
  222. ;; dired - reuse current buffer by pressing 'a'
  223. (put 'dired-find-alternate-file 'disabled nil)
  224. ;; always delete and copy recursively
  225. (setq dired-recursive-deletes 'always)
  226. (setq dired-recursive-copies 'always)
  227. ;; if there is a dired buffer displayed in the next window, use its
  228. ;; current subdir, instead of the current subdir of this dired buffer
  229. (setq dired-dwim-target t)
  230. ;; enable some really cool extensions like C-x C-j(dired-jump)
  231. (require 'dired-x)
  232. ;; ediff - don't start another frame
  233. (require 'ediff)
  234. (setq ediff-window-setup-function 'ediff-setup-windows-plain)
  235. ;; clean up obsolete buffers automatically
  236. (require 'midnight)
  237. ;; smarter kill-ring navigation
  238. (require 'browse-kill-ring)
  239. (browse-kill-ring-default-keybindings)
  240. (global-set-key (kbd "s-y") 'browse-kill-ring)
  241. (defadvice exchange-point-and-mark (before deactivate-mark activate compile)
  242. "When called with no active region, do not activate mark."
  243. (interactive
  244. (list (not (region-active-p)))))
  245. (require 'tabify)
  246. (defmacro with-region-or-buffer (func)
  247. "When called with no active region, call FUNC on current buffer."
  248. `(defadvice ,func (before with-region-or-buffer activate compile)
  249. (interactive
  250. (if mark-active
  251. (list (region-beginning) (region-end))
  252. (list (point-min) (point-max))))))
  253. (with-region-or-buffer indent-region)
  254. (with-region-or-buffer untabify)
  255. ;; automatically indenting yanked text if in programming-modes
  256. (defun yank-advised-indent-function (beg end)
  257. "Do indentation, as long as the region isn't too large."
  258. (if (<= (- end beg) prelude-yank-indent-threshold)
  259. (indent-region beg end nil)))
  260. (advise-commands "indent" (yank yank-pop) after
  261. "If current mode is one of `prelude-yank-indent-modes',
  262. indent yanked text (with prefix arg don't indent)."
  263. (if (and (not (ad-get-arg 0))
  264. (not (member major-mode prelude-indent-sensitive-modes))
  265. (or (derived-mode-p 'prog-mode)
  266. (member major-mode prelude-yank-indent-modes)))
  267. (let ((transient-mark-mode nil))
  268. (yank-advised-indent-function (region-beginning) (region-end)))))
  269. ;; abbrev config
  270. (add-hook 'text-mode-hook 'abbrev-mode)
  271. ;; make a shell script executable automatically on save
  272. (add-hook 'after-save-hook
  273. 'executable-make-buffer-file-executable-if-script-p)
  274. ;; .zsh file is shell script too
  275. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  276. ;; whitespace-mode config
  277. (require 'whitespace)
  278. (setq whitespace-line-column 80) ;; limit line length
  279. (setq whitespace-style '(face tabs empty trailing lines-tail))
  280. ;; saner regex syntax
  281. (require 're-builder)
  282. (setq reb-re-syntax 'string)
  283. (require 'eshell)
  284. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  285. (setq semanticdb-default-save-directory
  286. (expand-file-name "semanticdb" prelude-savefile-dir))
  287. ;; Compilation from Emacs
  288. (defun prelude-colorize-compilation-buffer ()
  289. "Colorize a compilation mode buffer."
  290. (interactive)
  291. ;; we don't want to mess with child modes such as grep-mode, ack, ag, etc
  292. (when (eq major-mode 'compilation-mode)
  293. (let ((inhibit-read-only t))
  294. (ansi-color-apply-on-region (point-min) (point-max)))))
  295. (require 'compile)
  296. (setq compilation-ask-about-save nil ; Just save before compiling
  297. compilation-always-kill t ; Just kill old compile processes before
  298. ; starting the new one
  299. compilation-scroll-output 'first-error ; Automatically scroll to first
  300. ; error
  301. )
  302. ;; Colorize output of Compilation Mode, see
  303. ;; http://stackoverflow.com/a/3072831/355252
  304. (require 'ansi-color)
  305. (add-hook 'compilation-filter-hook #'prelude-colorize-compilation-buffer)
  306. ;; enable Prelude's keybindings
  307. (prelude-global-mode t)
  308. ;; sensible undo
  309. (global-undo-tree-mode)
  310. (diminish 'undo-tree-mode)
  311. ;; enable winner-mode to manage window configurations
  312. (winner-mode +1)
  313. ;; diff-hl
  314. (global-diff-hl-mode +1)
  315. (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
  316. ;; easy-kill
  317. (global-set-key [remap kill-ring-save] 'easy-kill)
  318. (global-set-key [remap mark-sexp] 'easy-mark)
  319. ;; operate-on-number
  320. (require 'operate-on-number)
  321. (require 'smartrep)
  322. (smartrep-define-key global-map "C-c ."
  323. '(("+" . apply-operation-to-number-at-point)
  324. ("-" . apply-operation-to-number-at-point)
  325. ("*" . apply-operation-to-number-at-point)
  326. ("/" . apply-operation-to-number-at-point)
  327. ("\\" . apply-operation-to-number-at-point)
  328. ("^" . apply-operation-to-number-at-point)
  329. ("<" . apply-operation-to-number-at-point)
  330. (">" . apply-operation-to-number-at-point)
  331. ("#" . apply-operation-to-number-at-point)
  332. ("%" . apply-operation-to-number-at-point)
  333. ("'" . operate-on-number-at-point)))
  334. (defadvice server-visit-files (before parse-numbers-in-lines (files proc &optional nowait) activate)
  335. "Open file with emacsclient with cursors positioned on requested line.
  336. Most of console-based utilities prints filename in format
  337. 'filename:linenumber'. So you may wish to open filename in that format.
  338. Just call:
  339. emacsclient filename:linenumber
  340. and file 'filename' will be opened and cursor set on line 'linenumber'"
  341. (ad-set-arg 0
  342. (mapcar (lambda (fn)
  343. (let ((name (car fn)))
  344. (if (string-match "^\\(.*?\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?$" name)
  345. (cons
  346. (match-string 1 name)
  347. (cons (string-to-number (match-string 2 name))
  348. (string-to-number (or (match-string 3 name) ""))))
  349. fn))) files)))
  350. (provide 'prelude-editor)
  351. ;;; prelude-editor.el ends here