Emacs personal configuration
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.

340 lines
12KB

  1. (require-package 'unfill)
  2. (when (fboundp 'electric-pair-mode)
  3. (electric-pair-mode))
  4. (when (fboundp 'electric-indent-mode)
  5. (electric-indent-mode))
  6. ;;----------------------------------------------------------------------------
  7. ;; Some basic preferences
  8. ;;----------------------------------------------------------------------------
  9. (setq-default
  10. blink-cursor-delay 0
  11. blink-cursor-interval 0.4
  12. bookmark-default-file (expand-file-name ".bookmarks.el" user-emacs-directory)
  13. buffers-menu-max-size 30
  14. case-fold-search t
  15. column-number-mode t
  16. delete-selection-mode t
  17. ediff-split-window-function 'split-window-horizontally
  18. ediff-window-setup-function 'ediff-setup-windows-plain
  19. indent-tabs-mode nil
  20. make-backup-files nil
  21. mouse-yank-at-point t
  22. save-interprogram-paste-before-kill t
  23. scroll-preserve-screen-position 'always
  24. set-mark-command-repeat-pop t
  25. show-trailing-whitespace t
  26. tooltip-delay 1.5
  27. truncate-lines nil
  28. truncate-partial-width-windows nil
  29. visible-bell t)
  30. (global-auto-revert-mode)
  31. (setq global-auto-revert-non-file-buffers t
  32. auto-revert-verbose nil)
  33. (transient-mark-mode t)
  34. ;;; Whitespace
  35. (defun sanityinc/no-trailing-whitespace ()
  36. "Turn off display of trailing whitespace in this buffer."
  37. (setq show-trailing-whitespace nil))
  38. ;; But don't show trailing whitespace in SQLi, inf-ruby etc.
  39. (dolist (hook '(special-mode-hook
  40. eww-mode-hook
  41. term-mode-hook
  42. comint-mode-hook
  43. compilation-mode-hook
  44. twittering-mode-hook
  45. minibuffer-setup-hook))
  46. (add-hook hook #'sanityinc/no-trailing-whitespace))
  47. (require-package 'whitespace-cleanup-mode)
  48. (global-whitespace-cleanup-mode t)
  49. (when (eval-when-compile (string< "24.3.1" emacs-version))
  50. ;; https://github.com/purcell/emacs.d/issues/138
  51. (after-load 'subword
  52. (diminish 'subword-mode)))
  53. (when (fboundp 'global-prettify-symbols-mode)
  54. (global-prettify-symbols-mode))
  55. (require-package 'undo-tree)
  56. (global-undo-tree-mode)
  57. (diminish 'undo-tree-mode)
  58. (require-package 'highlight-symbol)
  59. (dolist (hook '(prog-mode-hook html-mode-hook css-mode-hook))
  60. (add-hook hook 'highlight-symbol-mode)
  61. (add-hook hook 'highlight-symbol-nav-mode))
  62. (eval-after-load 'highlight-symbol
  63. '(diminish 'highlight-symbol-mode))
  64. (global-set-key "\M-p" 'highlight-symbol-prev)
  65. (global-set-key "\M-n" 'highlight-symbol-next)
  66. ;;----------------------------------------------------------------------------
  67. ;; Zap *up* to char is a handy pair for zap-to-char
  68. ;;----------------------------------------------------------------------------
  69. (autoload 'zap-up-to-char "misc" "Kill up to, but not including ARGth occurrence of CHAR.")
  70. (global-set-key (kbd "M-Z") 'zap-up-to-char)
  71. (require-package 'browse-kill-ring)
  72. ;;----------------------------------------------------------------------------
  73. ;; Don't disable narrowing commands
  74. ;;----------------------------------------------------------------------------
  75. (put 'narrow-to-region 'disabled nil)
  76. (put 'narrow-to-page 'disabled nil)
  77. (put 'narrow-to-defun 'disabled nil)
  78. ;;----------------------------------------------------------------------------
  79. ;; Show matching parens
  80. ;;----------------------------------------------------------------------------
  81. (show-paren-mode 1)
  82. ;;----------------------------------------------------------------------------
  83. ;; Expand region
  84. ;;----------------------------------------------------------------------------
  85. (require-package 'expand-region)
  86. (global-set-key (kbd "C-=") 'er/expand-region)
  87. ;;----------------------------------------------------------------------------
  88. ;; Don't disable case-change functions
  89. ;;----------------------------------------------------------------------------
  90. (put 'upcase-region 'disabled nil)
  91. (put 'downcase-region 'disabled nil)
  92. ;;----------------------------------------------------------------------------
  93. ;; Rectangle selections, and overwrite text when the selection is active
  94. ;;----------------------------------------------------------------------------
  95. (cua-selection-mode t) ; for rectangles, CUA is nice
  96. ;;----------------------------------------------------------------------------
  97. ;; Handy key bindings
  98. ;;----------------------------------------------------------------------------
  99. ;; To be able to M-x without meta
  100. (global-set-key (kbd "C-x C-m") 'execute-extended-command)
  101. ;; Vimmy alternatives to M-^ and C-u M-^
  102. (global-set-key (kbd "C-c j") 'join-line)
  103. (global-set-key (kbd "C-c J") (lambda () (interactive) (join-line 1)))
  104. (global-set-key (kbd "C-.") 'set-mark-command)
  105. (global-set-key (kbd "C-x C-.") 'pop-global-mark)
  106. (require-package 'ace-jump-mode)
  107. (global-set-key (kbd "C-;") 'ace-jump-mode)
  108. (global-set-key (kbd "C-:") 'ace-jump-word-mode)
  109. (require-package 'multiple-cursors)
  110. ;; multiple-cursors
  111. (global-set-key (kbd "C-<") 'mc/mark-previous-like-this)
  112. (global-set-key (kbd "C->") 'mc/mark-next-like-this)
  113. (global-set-key (kbd "C-+") 'mc/mark-next-like-this)
  114. (global-set-key (kbd "C-c C-<") 'mc/mark-all-like-this)
  115. ;; From active region to multiple cursors:
  116. (global-set-key (kbd "C-c c r") 'set-rectangular-region-anchor)
  117. (global-set-key (kbd "C-c c c") 'mc/edit-lines)
  118. (global-set-key (kbd "C-c c e") 'mc/edit-ends-of-lines)
  119. (global-set-key (kbd "C-c c a") 'mc/edit-beginnings-of-lines)
  120. ;; Train myself to use M-f and M-b instead
  121. (global-unset-key [M-left])
  122. (global-unset-key [M-right])
  123. (defun kill-back-to-indentation ()
  124. "Kill from point back to the first non-whitespace character on the line."
  125. (interactive)
  126. (let ((prev-pos (point)))
  127. (back-to-indentation)
  128. (kill-region (point) prev-pos)))
  129. (global-set-key (kbd "C-M-<backspace>") 'kill-back-to-indentation)
  130. ;;----------------------------------------------------------------------------
  131. ;; Page break lines
  132. ;;----------------------------------------------------------------------------
  133. (require-package 'page-break-lines)
  134. (global-page-break-lines-mode)
  135. (diminish 'page-break-lines-mode)
  136. ;;----------------------------------------------------------------------------
  137. ;; Fill column indicator
  138. ;;----------------------------------------------------------------------------
  139. (when (eval-when-compile (> emacs-major-version 23))
  140. (require-package 'fill-column-indicator)
  141. (defun sanityinc/prog-mode-fci-settings ()
  142. (turn-on-fci-mode)
  143. (when show-trailing-whitespace
  144. (set (make-local-variable 'whitespace-style) '(face trailing))
  145. (whitespace-mode 1)))
  146. ;;(add-hook 'prog-mode-hook 'sanityinc/prog-mode-fci-settings)
  147. (defun sanityinc/fci-enabled-p ()
  148. (and (boundp 'fci-mode) fci-mode))
  149. (defvar sanityinc/fci-mode-suppressed nil)
  150. (defadvice popup-create (before suppress-fci-mode activate)
  151. "Suspend fci-mode while popups are visible"
  152. (let ((fci-enabled (sanityinc/fci-enabled-p)))
  153. (when fci-enabled
  154. (set (make-local-variable 'sanityinc/fci-mode-suppressed) fci-enabled)
  155. (turn-off-fci-mode))))
  156. (defadvice popup-delete (after restore-fci-mode activate)
  157. "Restore fci-mode when all popups have closed"
  158. (when (and sanityinc/fci-mode-suppressed
  159. (null popup-instances))
  160. (setq sanityinc/fci-mode-suppressed nil)
  161. (turn-on-fci-mode)))
  162. ;; Regenerate fci-mode line images after switching themes
  163. (defadvice enable-theme (after recompute-fci-face activate)
  164. (dolist (buffer (buffer-list))
  165. (with-current-buffer buffer
  166. (when (sanityinc/fci-enabled-p)
  167. (turn-on-fci-mode))))))
  168. ;;----------------------------------------------------------------------------
  169. ;; Shift lines up and down with M-up and M-down. When paredit is enabled,
  170. ;; it will use those keybindings. For this reason, you might prefer to
  171. ;; use M-S-up and M-S-down, which will work even in lisp modes.
  172. ;;----------------------------------------------------------------------------
  173. (require-package 'move-dup)
  174. (global-set-key [M-up] 'md/move-lines-up)
  175. (global-set-key [M-down] 'md/move-lines-down)
  176. (global-set-key [M-S-up] 'md/move-lines-up)
  177. (global-set-key [M-S-down] 'md/move-lines-down)
  178. (global-set-key (kbd "C-c p") 'md/duplicate-down)
  179. (global-set-key (kbd "C-c P") 'md/duplicate-up)
  180. ;;----------------------------------------------------------------------------
  181. ;; Fix backward-up-list to understand quotes, see http://bit.ly/h7mdIL
  182. ;;----------------------------------------------------------------------------
  183. (defun backward-up-sexp (arg)
  184. "Jump up to the start of the ARG'th enclosing sexp."
  185. (interactive "p")
  186. (let ((ppss (syntax-ppss)))
  187. (cond ((elt ppss 3)
  188. (goto-char (elt ppss 8))
  189. (backward-up-sexp (1- arg)))
  190. ((backward-up-list arg)))))
  191. (global-set-key [remap backward-up-list] 'backward-up-sexp) ; C-M-u, C-M-up
  192. ;;----------------------------------------------------------------------------
  193. ;; Cut/copy the current line if no region is active
  194. ;;----------------------------------------------------------------------------
  195. (require-package 'whole-line-or-region)
  196. (whole-line-or-region-global-mode t)
  197. (diminish 'whole-line-or-region-global-mode)
  198. (make-variable-buffer-local 'whole-line-or-region-global-mode)
  199. (defun suspend-mode-during-cua-rect-selection (mode-name)
  200. "Add an advice to suspend `MODE-NAME' while selecting a CUA rectangle."
  201. (let ((flagvar (intern (format "%s-was-active-before-cua-rectangle" mode-name)))
  202. (advice-name (intern (format "suspend-%s" mode-name))))
  203. (eval-after-load 'cua-rect
  204. `(progn
  205. (defvar ,flagvar nil)
  206. (make-variable-buffer-local ',flagvar)
  207. (defadvice cua--activate-rectangle (after ,advice-name activate)
  208. (setq ,flagvar (and (boundp ',mode-name) ,mode-name))
  209. (when ,flagvar
  210. (,mode-name 0)))
  211. (defadvice cua--deactivate-rectangle (after ,advice-name activate)
  212. (when ,flagvar
  213. (,mode-name 1)))))))
  214. (suspend-mode-during-cua-rect-selection 'whole-line-or-region-global-mode)
  215. (defun sanityinc/open-line-with-reindent (n)
  216. "A version of `open-line' which reindents the start and end positions.
  217. If there is a fill prefix and/or a `left-margin', insert them
  218. on the new line if the line would have been blank.
  219. With arg N, insert N newlines."
  220. (interactive "*p")
  221. (let* ((do-fill-prefix (and fill-prefix (bolp)))
  222. (do-left-margin (and (bolp) (> (current-left-margin) 0)))
  223. (loc (point-marker))
  224. ;; Don't expand an abbrev before point.
  225. (abbrev-mode nil))
  226. (delete-horizontal-space t)
  227. (newline n)
  228. (indent-according-to-mode)
  229. (when (eolp)
  230. (delete-horizontal-space t))
  231. (goto-char loc)
  232. (while (> n 0)
  233. (cond ((bolp)
  234. (if do-left-margin (indent-to (current-left-margin)))
  235. (if do-fill-prefix (insert-and-inherit fill-prefix))))
  236. (forward-line 1)
  237. (setq n (1- n)))
  238. (goto-char loc)
  239. (end-of-line)
  240. (indent-according-to-mode)))
  241. (global-set-key (kbd "C-o") 'sanityinc/open-line-with-reindent)
  242. ;;----------------------------------------------------------------------------
  243. ;; Random line sorting
  244. ;;----------------------------------------------------------------------------
  245. (defun sort-lines-random (beg end)
  246. "Sort lines in region randomly."
  247. (interactive "r")
  248. (save-excursion
  249. (save-restriction
  250. (narrow-to-region beg end)
  251. (goto-char (point-min))
  252. (let ;; To make `end-of-line' and etc. to ignore fields.
  253. ((inhibit-field-text-motion t))
  254. (sort-subr nil 'forward-line 'end-of-line nil nil
  255. (lambda (s1 s2) (eq (random 2) 0)))))))
  256. (require-package 'highlight-escape-sequences)
  257. (hes-mode)
  258. (require-package 'guide-key)
  259. (setq guide-key/guide-key-sequence '("C-x r" "C-x 4" "C-x 5" "C-c ;" "C-c ; f" "C-c ' f" "C-x n"))
  260. (guide-key-mode 1)
  261. (diminish 'guide-key-mode)
  262. (provide 'init-editing-utils)