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