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.

436 lines
15 KiB

14 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
12 years ago
  1. ;;; prelude-editor.el --- Emacs Prelude: enhanced core editing experience.
  2. ;;
  3. ;; Copyright © 2011-2018 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. (setq save-place-file (expand-file-name "saveplace" prelude-savefile-dir))
  91. ;; activate it for all buffers
  92. (save-place-mode 1)
  93. ;; savehist keeps track of some history
  94. (require 'savehist)
  95. (setq savehist-additional-variables
  96. ;; search entries
  97. '(search-ring regexp-search-ring)
  98. ;; save every minute
  99. savehist-autosave-interval 60
  100. ;; keep the home clean
  101. savehist-file (expand-file-name "savehist" prelude-savefile-dir))
  102. (savehist-mode +1)
  103. ;; save recent files
  104. (require 'recentf)
  105. (setq recentf-save-file (expand-file-name "recentf" prelude-savefile-dir)
  106. recentf-max-saved-items 500
  107. recentf-max-menu-items 15
  108. ;; disable recentf-cleanup on Emacs start, because it can cause
  109. ;; problems with remote files
  110. recentf-auto-cleanup 'never)
  111. (defun prelude-recentf-exclude-p (file)
  112. "A predicate to decide whether to exclude FILE from recentf."
  113. (let ((file-dir (file-truename (file-name-directory file))))
  114. (cl-some (lambda (dir)
  115. (string-prefix-p dir file-dir))
  116. (mapcar 'file-truename (list prelude-savefile-dir package-user-dir)))))
  117. (add-to-list 'recentf-exclude 'prelude-recentf-exclude-p)
  118. (recentf-mode +1)
  119. ;; use shift + arrow keys to switch between visible buffers
  120. (require 'windmove)
  121. (windmove-default-keybindings)
  122. ;; automatically save buffers associated with files on buffer switch
  123. ;; and on windows switch
  124. (defun prelude-auto-save-command ()
  125. "Save the current buffer if `prelude-auto-save' is not nil."
  126. (when (and prelude-auto-save
  127. buffer-file-name
  128. (buffer-modified-p (current-buffer))
  129. (file-writable-p buffer-file-name))
  130. (save-buffer)))
  131. (defmacro advise-commands (advice-name commands class &rest body)
  132. "Apply advice named ADVICE-NAME to multiple COMMANDS.
  133. The body of the advice is in BODY."
  134. `(progn
  135. ,@(mapcar (lambda (command)
  136. `(defadvice ,command (,class ,(intern (concat (symbol-name command) "-" advice-name)) activate)
  137. ,@body))
  138. commands)))
  139. ;; advise all window switching functions
  140. (advise-commands "auto-save"
  141. (switch-to-buffer other-window windmove-up windmove-down windmove-left windmove-right)
  142. before
  143. (prelude-auto-save-command))
  144. (add-hook 'mouse-leave-buffer-hook 'prelude-auto-save-command)
  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. (when (and mode (consp mode))
  151. (setq mode (car mode)))
  152. (with-current-buffer buffer (if mode (funcall mode)))))
  153. ;; highlight the current line
  154. (global-hl-line-mode +1)
  155. (require 'volatile-highlights)
  156. (volatile-highlights-mode t)
  157. (diminish 'volatile-highlights-mode)
  158. ;; note - this should be after volatile-highlights is required
  159. ;; add the ability to cut the current line, without marking it
  160. (require 'rect)
  161. (crux-with-region-or-line kill-region)
  162. ;; tramp, for sudo access
  163. (require 'tramp)
  164. ;; keep in mind known issues with zsh - see emacs wiki
  165. (setq tramp-default-method "ssh")
  166. (set-default 'imenu-auto-rescan t)
  167. ;; flyspell-mode does spell-checking on the fly as you type
  168. (require 'flyspell)
  169. (setq ispell-program-name "aspell" ; use aspell instead of ispell
  170. ispell-extra-args '("--sug-mode=ultra"))
  171. (defun prelude-enable-flyspell ()
  172. "Enable command `flyspell-mode' if `prelude-flyspell' is not nil."
  173. (when (and prelude-flyspell (executable-find ispell-program-name))
  174. (flyspell-mode +1)))
  175. (defun prelude-cleanup-maybe ()
  176. "Invoke `whitespace-cleanup' if `prelude-clean-whitespace-on-save' is not nil."
  177. (when prelude-clean-whitespace-on-save
  178. (whitespace-cleanup)))
  179. (defun prelude-enable-whitespace ()
  180. "Enable `whitespace-mode' if `prelude-whitespace' is not nil."
  181. (when prelude-whitespace
  182. ;; keep the whitespace decent all the time (in this buffer)
  183. (add-hook 'before-save-hook 'prelude-cleanup-maybe nil t)
  184. (whitespace-mode +1)))
  185. (add-hook 'text-mode-hook 'prelude-enable-flyspell)
  186. (add-hook 'text-mode-hook 'prelude-enable-whitespace)
  187. ;; enable narrowing commands
  188. (put 'narrow-to-region 'disabled nil)
  189. (put 'narrow-to-page 'disabled nil)
  190. (put 'narrow-to-defun 'disabled nil)
  191. ;; enabled change region case commands
  192. (put 'upcase-region 'disabled nil)
  193. (put 'downcase-region 'disabled nil)
  194. ;; enable erase-buffer command
  195. (put 'erase-buffer 'disabled nil)
  196. (require 'expand-region)
  197. ;; bookmarks
  198. (require 'bookmark)
  199. (setq bookmark-default-file (expand-file-name "bookmarks" prelude-savefile-dir)
  200. bookmark-save-flag 1)
  201. ;; projectile is a project management mode
  202. (require 'projectile)
  203. (setq projectile-cache-file (expand-file-name "projectile.cache" prelude-savefile-dir))
  204. (projectile-mode t)
  205. ;; avy allows us to effectively navigate to visible things
  206. (require 'avy)
  207. (setq avy-background t)
  208. (setq avy-style 'at-full)
  209. ;; anzu-mode enhances isearch & query-replace by showing total matches and current match position
  210. (require 'anzu)
  211. (diminish 'anzu-mode)
  212. (global-anzu-mode)
  213. (global-set-key (kbd "M-%") 'anzu-query-replace)
  214. (global-set-key (kbd "C-M-%") 'anzu-query-replace-regexp)
  215. ;; dired - reuse current buffer by pressing 'a'
  216. (put 'dired-find-alternate-file 'disabled nil)
  217. ;; always delete and copy recursively
  218. (setq dired-recursive-deletes 'always)
  219. (setq dired-recursive-copies 'always)
  220. ;; if there is a dired buffer displayed in the next window, use its
  221. ;; current subdir, instead of the current subdir of this dired buffer
  222. (setq dired-dwim-target t)
  223. ;; enable some really cool extensions like C-x C-j(dired-jump)
  224. (require 'dired-x)
  225. ;; ediff - don't start another frame
  226. (require 'ediff)
  227. (setq ediff-window-setup-function 'ediff-setup-windows-plain)
  228. ;; clean up obsolete buffers automatically
  229. (require 'midnight)
  230. ;; smarter kill-ring navigation
  231. (require 'browse-kill-ring)
  232. (browse-kill-ring-default-keybindings)
  233. (global-set-key (kbd "s-y") 'browse-kill-ring)
  234. (defadvice exchange-point-and-mark (before deactivate-mark activate compile)
  235. "When called with no active region, do not activate mark."
  236. (interactive
  237. (list (not (region-active-p)))))
  238. (require 'tabify)
  239. (defmacro with-region-or-buffer (func)
  240. "When called with no active region, call FUNC on current buffer."
  241. `(defadvice ,func (before with-region-or-buffer activate compile)
  242. (interactive
  243. (if mark-active
  244. (list (region-beginning) (region-end))
  245. (list (point-min) (point-max))))))
  246. (with-region-or-buffer indent-region)
  247. (with-region-or-buffer untabify)
  248. ;; automatically indenting yanked text if in programming-modes
  249. (defun yank-advised-indent-function (beg end)
  250. "Do indentation, as long as the region isn't too large."
  251. (if (<= (- end beg) prelude-yank-indent-threshold)
  252. (indent-region beg end nil)))
  253. (advise-commands "indent" (yank yank-pop) after
  254. "If current mode is one of `prelude-yank-indent-modes',
  255. indent yanked text (with prefix arg don't indent)."
  256. (if (and (not (ad-get-arg 0))
  257. (not (member major-mode prelude-indent-sensitive-modes))
  258. (or (derived-mode-p 'prog-mode)
  259. (member major-mode prelude-yank-indent-modes)))
  260. (let ((transient-mark-mode nil))
  261. (yank-advised-indent-function (region-beginning) (region-end)))))
  262. ;; abbrev config
  263. (add-hook 'text-mode-hook 'abbrev-mode)
  264. ;; make a shell script executable automatically on save
  265. (add-hook 'after-save-hook
  266. 'executable-make-buffer-file-executable-if-script-p)
  267. ;; .zsh file is shell script too
  268. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  269. ;; whitespace-mode config
  270. (require 'whitespace)
  271. (setq whitespace-line-column 80) ;; limit line length
  272. (setq whitespace-style '(face tabs empty trailing lines-tail))
  273. ;; saner regex syntax
  274. (require 're-builder)
  275. (setq reb-re-syntax 'string)
  276. (require 'eshell)
  277. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  278. (setq semanticdb-default-save-directory
  279. (expand-file-name "semanticdb" prelude-savefile-dir))
  280. ;; Compilation from Emacs
  281. (defun prelude-colorize-compilation-buffer ()
  282. "Colorize a compilation mode buffer."
  283. (interactive)
  284. ;; we don't want to mess with child modes such as grep-mode, ack, ag, etc
  285. (when (eq major-mode 'compilation-mode)
  286. (let ((inhibit-read-only t))
  287. (ansi-color-apply-on-region (point-min) (point-max)))))
  288. (require 'compile)
  289. (setq compilation-ask-about-save nil ; Just save before compiling
  290. compilation-always-kill t ; Just kill old compile processes before
  291. ; starting the new one
  292. compilation-scroll-output 'first-error ; Automatically scroll to first
  293. ; error
  294. )
  295. ;; Colorize output of Compilation Mode, see
  296. ;; http://stackoverflow.com/a/3072831/355252
  297. (require 'ansi-color)
  298. (add-hook 'compilation-filter-hook #'prelude-colorize-compilation-buffer)
  299. ;; enable Prelude's keybindings
  300. (prelude-global-mode t)
  301. ;; sensible undo
  302. (global-undo-tree-mode)
  303. (diminish 'undo-tree-mode)
  304. ;; enable winner-mode to manage window configurations
  305. (winner-mode +1)
  306. ;; diff-hl
  307. (global-diff-hl-mode +1)
  308. (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
  309. (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh)
  310. ;; easy-kill
  311. (global-set-key [remap kill-ring-save] 'easy-kill)
  312. (global-set-key [remap mark-sexp] 'easy-mark)
  313. ;; operate-on-number
  314. (require 'operate-on-number)
  315. (require 'smartrep)
  316. (smartrep-define-key global-map "C-c ."
  317. '(("+" . apply-operation-to-number-at-point)
  318. ("-" . apply-operation-to-number-at-point)
  319. ("*" . apply-operation-to-number-at-point)
  320. ("/" . apply-operation-to-number-at-point)
  321. ("\\" . apply-operation-to-number-at-point)
  322. ("^" . apply-operation-to-number-at-point)
  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. ("'" . operate-on-number-at-point)))
  328. (defadvice server-visit-files (before parse-numbers-in-lines (files proc &optional nowait) activate)
  329. "Open file with emacsclient with cursors positioned on requested line.
  330. Most of console-based utilities prints filename in format
  331. 'filename:linenumber'. So you may wish to open filename in that format.
  332. Just call:
  333. emacsclient filename:linenumber
  334. and file 'filename' will be opened and cursor set on line 'linenumber'"
  335. (ad-set-arg 0
  336. (mapcar (lambda (fn)
  337. (let ((name (car fn)))
  338. (if (string-match "^\\(.*?\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?$" name)
  339. (cons
  340. (match-string 1 name)
  341. (cons (string-to-number (match-string 2 name))
  342. (string-to-number (or (match-string 3 name) ""))))
  343. fn))) files)))
  344. ;; use settings from .editorconfig file when present
  345. (require 'editorconfig)
  346. (editorconfig-mode 1)
  347. (provide 'prelude-editor)
  348. ;;; prelude-editor.el ends here