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.

421 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-2020 Bozhidar Batsov
  4. ;;
  5. ;; Author: Bozhidar Batsov <bozhidar@batsov.com>
  6. ;; URL: https://github.com/bbatsov/prelude
  7. ;; This file is not part of GNU Emacs.
  8. ;;; Commentary:
  9. ;; Refinements of the core editing experience in Emacs.
  10. ;;; License:
  11. ;; This program is free software; you can redistribute it and/or
  12. ;; modify it under the terms of the GNU General Public License
  13. ;; as published by the Free Software Foundation; either version 3
  14. ;; of the License, or (at your option) any later version.
  15. ;;
  16. ;; This program is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;;
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  23. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  24. ;; Boston, MA 02110-1301, USA.
  25. ;;; Code:
  26. ;; Death to the tabs! However, tabs historically indent to the next
  27. ;; 8-character offset; specifying anything else will cause *mass*
  28. ;; confusion, as it will change the appearance of every existing file.
  29. ;; In some cases (python), even worse -- it will change the semantics
  30. ;; (meaning) of the program.
  31. ;;
  32. ;; Emacs modes typically provide a standard means to change the
  33. ;; indentation width -- eg. c-basic-offset: use that to adjust your
  34. ;; personal indentation width, while maintaining the style (and
  35. ;; meaning) of any files you load.
  36. (setq-default indent-tabs-mode nil) ;; don't use tabs to indent
  37. (setq-default tab-width 8) ;; but maintain correct appearance
  38. ;; Newline at end of file
  39. (setq require-final-newline t)
  40. ;; delete the selection with a keypress
  41. (delete-selection-mode t)
  42. ;; store all backup and autosave files in the tmp dir
  43. (setq backup-directory-alist
  44. `((".*" . ,temporary-file-directory)))
  45. (setq auto-save-file-name-transforms
  46. `((".*" ,temporary-file-directory t)))
  47. ;; autosave the undo-tree history
  48. (setq undo-tree-history-directory-alist
  49. `((".*" . ,temporary-file-directory)))
  50. (setq undo-tree-auto-save-history t)
  51. ;; revert buffers automatically when underlying files are changed externally
  52. (global-auto-revert-mode t)
  53. ;; hippie expand is dabbrev expand on steroids
  54. (setq hippie-expand-try-functions-list '(try-expand-dabbrev
  55. try-expand-dabbrev-all-buffers
  56. try-expand-dabbrev-from-kill
  57. try-complete-file-name-partially
  58. try-complete-file-name
  59. try-expand-all-abbrevs
  60. try-expand-list
  61. try-expand-line
  62. try-complete-lisp-symbol-partially
  63. try-complete-lisp-symbol))
  64. ;; smart tab behavior - indent or complete
  65. (setq tab-always-indent 'complete)
  66. ;; smart pairing for all
  67. (require 'smartparens-config)
  68. (setq sp-base-key-bindings 'paredit)
  69. (setq sp-autoskip-closing-pair 'always)
  70. (setq sp-hybrid-kill-entire-symbol nil)
  71. (sp-use-paredit-bindings)
  72. (show-smartparens-global-mode +1)
  73. (define-key prog-mode-map (kbd "M-(") (prelude-wrap-with "("))
  74. ;; FIXME: pick terminal friendly binding
  75. ;; (define-key prog-mode-map (kbd "M-[") (prelude-wrap-with "["))
  76. (define-key prog-mode-map (kbd "M-\"") (prelude-wrap-with "\""))
  77. ;; disable annoying blink-matching-paren
  78. (setq blink-matching-paren nil)
  79. ;; diminish keeps the modeline tidy
  80. (require 'diminish)
  81. ;; meaningful names for buffers with the same name
  82. (require 'uniquify)
  83. (setq uniquify-buffer-name-style 'forward)
  84. (setq uniquify-separator "/")
  85. (setq uniquify-after-kill-buffer-p t) ; rename after killing uniquified
  86. (setq uniquify-ignore-buffers-re "^\\*") ; don't muck with special buffers
  87. ;; saveplace remembers your location in a file when saving files
  88. (setq save-place-file (expand-file-name "saveplace" prelude-savefile-dir))
  89. ;; activate it for all buffers
  90. (save-place-mode 1)
  91. ;; savehist keeps track of some history
  92. (require 'savehist)
  93. (setq savehist-additional-variables
  94. ;; search entries
  95. '(search-ring regexp-search-ring)
  96. ;; save every minute
  97. savehist-autosave-interval 60
  98. ;; keep the home clean
  99. savehist-file (expand-file-name "savehist" prelude-savefile-dir))
  100. (savehist-mode +1)
  101. ;; save recent files
  102. (require 'recentf)
  103. (setq recentf-save-file (expand-file-name "recentf" prelude-savefile-dir)
  104. recentf-max-saved-items 500
  105. recentf-max-menu-items 15
  106. ;; disable recentf-cleanup on Emacs start, because it can cause
  107. ;; problems with remote files
  108. recentf-auto-cleanup 'never)
  109. (defun prelude-recentf-exclude-p (file)
  110. "A predicate to decide whether to exclude FILE from recentf."
  111. (let ((file-dir (file-truename (file-name-directory file))))
  112. (cl-some (lambda (dir)
  113. (string-prefix-p dir file-dir))
  114. (mapcar 'file-truename (list prelude-savefile-dir package-user-dir)))))
  115. (add-to-list 'recentf-exclude 'prelude-recentf-exclude-p)
  116. (recentf-mode +1)
  117. ;; use shift + arrow keys to switch between visible buffers
  118. (require 'windmove)
  119. (windmove-default-keybindings)
  120. ;; automatically save buffers associated with files on buffer switch
  121. ;; and on windows switch
  122. (require 'super-save)
  123. ;; add integration with ace-window
  124. (add-to-list 'super-save-triggers 'ace-window)
  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. (defmacro advise-commands (advice-name commands class &rest body)
  234. "Apply advice named ADVICE-NAME to multiple COMMANDS.
  235. The body of the advice is in BODY."
  236. `(progn
  237. ,@(mapcar (lambda (command)
  238. `(defadvice ,command (,class ,(intern (concat (symbol-name command) "-" advice-name)) activate)
  239. ,@body))
  240. commands)))
  241. (advise-commands "indent" (yank yank-pop) after
  242. "If current mode is one of `prelude-yank-indent-modes',
  243. indent yanked text (with prefix arg don't indent)."
  244. (if (and (not (ad-get-arg 0))
  245. (not (member major-mode prelude-indent-sensitive-modes))
  246. (or (derived-mode-p 'prog-mode)
  247. (member major-mode prelude-yank-indent-modes)))
  248. (let ((transient-mark-mode nil))
  249. (yank-advised-indent-function (region-beginning) (region-end)))))
  250. ;; abbrev config
  251. (add-hook 'text-mode-hook 'abbrev-mode)
  252. ;; make a shell script executable automatically on save
  253. (add-hook 'after-save-hook
  254. 'executable-make-buffer-file-executable-if-script-p)
  255. ;; .zsh file is shell script too
  256. (add-to-list 'auto-mode-alist '("\\.zsh\\'" . shell-script-mode))
  257. ;; whitespace-mode config
  258. (require 'whitespace)
  259. (setq whitespace-line-column 80) ;; limit line length
  260. (setq whitespace-style '(face tabs empty trailing lines-tail))
  261. ;; saner regex syntax
  262. (require 're-builder)
  263. (setq reb-re-syntax 'string)
  264. (require 'eshell)
  265. (setq eshell-directory-name (expand-file-name "eshell" prelude-savefile-dir))
  266. (setq semanticdb-default-save-directory
  267. (expand-file-name "semanticdb" prelude-savefile-dir))
  268. ;; Compilation from Emacs
  269. (defun prelude-colorize-compilation-buffer ()
  270. "Colorize a compilation mode buffer."
  271. (interactive)
  272. ;; we don't want to mess with child modes such as grep-mode, ack, ag, etc
  273. (when (eq major-mode 'compilation-mode)
  274. (let ((inhibit-read-only t))
  275. (ansi-color-apply-on-region (point-min) (point-max)))))
  276. (require 'compile)
  277. (setq compilation-ask-about-save nil ; Just save before compiling
  278. compilation-always-kill t ; Just kill old compile processes before
  279. ; starting the new one
  280. compilation-scroll-output 'first-error ; Automatically scroll to first
  281. ; error
  282. )
  283. ;; Colorize output of Compilation Mode, see
  284. ;; http://stackoverflow.com/a/3072831/355252
  285. (require 'ansi-color)
  286. (add-hook 'compilation-filter-hook #'prelude-colorize-compilation-buffer)
  287. ;; enable Prelude's keybindings
  288. (prelude-mode t)
  289. ;; sensible undo
  290. (global-undo-tree-mode)
  291. (diminish 'undo-tree-mode)
  292. ;; enable winner-mode to manage window configurations
  293. (winner-mode +1)
  294. ;; diff-hl
  295. (global-diff-hl-mode +1)
  296. (add-hook 'dired-mode-hook 'diff-hl-dired-mode)
  297. (add-hook 'magit-post-refresh-hook 'diff-hl-magit-post-refresh)
  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. (require 'smartrep)
  304. (smartrep-define-key global-map "C-c ."
  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. ("%" . apply-operation-to-number-at-point)
  315. ("'" . operate-on-number-at-point)))
  316. (defadvice server-visit-files (before parse-numbers-in-lines (files proc &optional nowait) activate)
  317. "Open file with emacsclient with cursors positioned on requested line.
  318. Most of console-based utilities prints filename in format
  319. 'filename:linenumber'. So you may wish to open filename in that format.
  320. Just call:
  321. emacsclient filename:linenumber
  322. and file 'filename' will be opened and cursor set on line 'linenumber'"
  323. (ad-set-arg 0
  324. (mapcar (lambda (fn)
  325. (let ((name (car fn)))
  326. (if (string-match "^\\(.*?\\):\\([0-9]+\\)\\(?::\\([0-9]+\\)\\)?$" name)
  327. (cons
  328. (match-string 1 name)
  329. (cons (string-to-number (match-string 2 name))
  330. (string-to-number (or (match-string 3 name) ""))))
  331. fn))) files)))
  332. ;; use settings from .editorconfig file when present
  333. (require 'editorconfig)
  334. (editorconfig-mode 1)
  335. (provide 'prelude-editor)
  336. ;;; prelude-editor.el ends here