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.

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