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.

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