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.

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