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.

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