doomemacs/core/core-editor.el

316 lines
12 KiB
EmacsLisp
Raw Normal View History

2015-06-15 15:05:52 +08:00
;;; core-editor.el
2015-06-05 06:23:21 +08:00
2015-06-06 18:40:33 +08:00
(setq-default
;; spaces instead of tabs
indent-tabs-mode nil
tab-always-indent t
tab-width 4
2016-03-04 04:04:14 +08:00
2015-06-06 18:40:33 +08:00
require-final-newline t
delete-trailing-lines nil
2015-12-11 05:15:09 +08:00
fill-column 90
line-spacing 0
word-wrap t
truncate-lines t
truncate-partial-width-windows 50
2015-06-06 18:40:33 +08:00
2016-02-26 13:08:41 +08:00
visual-fill-column-center-text nil
confirm-nonexistent-file-or-buffer nil
2016-02-26 13:08:41 +08:00
2015-06-06 18:40:33 +08:00
;; Sane scroll settings
2015-06-15 15:05:52 +08:00
scroll-margin 0
2015-11-07 13:19:21 +08:00
scroll-conservatively 1001
2015-06-06 18:40:33 +08:00
scroll-preserve-screen-position t
2016-03-01 15:05:04 +08:00
hscroll-step 1
2016-01-31 10:16:10 +08:00
hscroll-margin 1
2015-11-11 07:10:32 +08:00
2016-03-04 04:04:14 +08:00
shift-select-mode t
2015-07-23 07:34:05 +08:00
tabify-regexp "^\t* [ \t]+"
2016-03-10 10:39:47 +08:00
whitespace-style '(face tabs tab-mark trailing newline newline-mark indentation lines-tail)
whitespace-line-column fill-column
2015-06-06 18:40:33 +08:00
whitespace-display-mappings
2015-08-06 18:46:29 +08:00
'((tab-mark ?\t [?> ?\t])
(newline-mark 10 [36 10])))
2015-06-06 18:40:33 +08:00
2015-11-27 16:40:10 +08:00
(require 'saveplace)
2016-02-01 13:18:36 +08:00
(setq-default
save-place-file (concat narf-temp-dir "saveplace")
save-place t)
(when (>= emacs-major-version 25)
(save-place-mode +1))
2015-11-27 16:40:10 +08:00
2015-06-06 18:40:33 +08:00
2016-03-04 04:04:14 +08:00
;;
;; Automatic minor modes
;;
2015-06-06 18:40:33 +08:00
2015-10-01 01:47:57 +08:00
(defvar narf-auto-minor-mode-alist '()
"Alist of filename patterns vs corresponding minor mode functions, see
`auto-mode-alist'. All elements of this alist are checked, meaning you can
enable multiple minor modes for the same regexp.")
2015-06-06 18:40:33 +08:00
(defun narf|enable-minor-mode-maybe ()
2015-06-15 15:05:52 +08:00
"Check file name against `narf-auto-minor-mode-alist'."
2015-06-06 18:40:33 +08:00
(when buffer-file-name
(let ((name buffer-file-name)
(remote-id (file-remote-p buffer-file-name))
2015-06-15 15:05:52 +08:00
(alist narf-auto-minor-mode-alist))
2015-06-06 18:40:33 +08:00
;; Remove backup-suffixes from file name.
(setq name (file-name-sans-versions name))
;; Remove remote file name identification.
(when (and (stringp remote-id)
(string-match-p (regexp-quote remote-id) name))
(setq name (substring name (match-end 0))))
(while (and alist (caar alist) (cdar alist))
(if (string-match (caar alist) name)
(funcall (cdar alist) 1))
(setq alist (cdr alist))))))
2015-06-15 15:05:52 +08:00
(add-hook! find-file 'narf|enable-minor-mode-maybe)
2016-03-04 04:04:14 +08:00
;;
;; Modes 'n hooks
;;
2016-01-31 10:16:10 +08:00
2015-12-11 05:15:09 +08:00
(associate! emacs-lisp-mode :match "\\(/Cask\\|\\.\\(el\\|gz\\)\\)$")
2015-06-15 15:05:52 +08:00
(associate! makefile-gmake-mode :match "/Makefile$")
(associate! nxml-mode :match "\\.plist$")
(associate! conf-mode :match "/\\.?editorconfig$")
2015-06-15 15:05:52 +08:00
2015-11-02 05:10:11 +08:00
(add-hook! help-mode 'visual-line-mode)
2015-11-24 08:41:08 +08:00
(add-hook! special-mode (setq truncate-lines nil))
2015-12-11 05:15:09 +08:00
(add-hook! python-mode 'electric-indent-local-mode)
(add-hook! change-major-mode-hook
(when indent-tabs-mode (whitespace-mode +1)))
2015-06-15 15:05:52 +08:00
2015-11-10 09:49:33 +08:00
(defadvice delete-trailing-whitespace
(around delete-trailing-whitespace-ignore-line activate)
2015-07-29 18:31:42 +08:00
"Don't delete trailing whitespace on current line, if in insert mode."
2015-08-01 20:38:41 +08:00
(let ((spaces (1- (current-column)))
2015-11-10 09:49:33 +08:00
(linestr (buffer-substring-no-properties
(line-beginning-position)
(line-end-position))))
2015-07-29 18:31:42 +08:00
ad-do-it
2015-08-01 20:38:41 +08:00
(when (string-match-p "^[\s\t]*$" linestr)
(insert linestr))))
2015-07-29 18:31:42 +08:00
2015-06-15 15:05:52 +08:00
;; If file is oversized...
(add-hook! find-file
2015-11-27 16:40:10 +08:00
(when (> (buffer-size) 1048576)
2015-06-15 15:05:52 +08:00
(setq buffer-read-only t)
(buffer-disable-undo)
(fundamental-mode)
(visual-line-mode)))
2016-03-04 04:04:14 +08:00
;;
2015-06-15 15:05:52 +08:00
;; (global-whitespace-mode 1) ; Show whitespace
2015-11-27 16:40:10 +08:00
;; (global-font-lock-mode t) ; Enable syntax highlighting for older emacs
2015-11-10 09:49:33 +08:00
(electric-indent-mode -1) ; on by default
2015-12-23 15:40:41 +08:00
(global-auto-revert-mode 1) ; revert buffers for changed files
2015-06-15 15:05:52 +08:00
;; window config undo/redo
2015-07-17 01:44:34 +08:00
(setq winner-dont-bind-my-keys t)
2015-06-15 15:05:52 +08:00
(winner-mode 1)
2015-11-27 16:40:10 +08:00
(add-hook! after-init
(setq winner-boring-buffers narf-ignore-buffers))
2015-06-06 18:40:33 +08:00
2016-03-04 04:04:14 +08:00
;;
;; Extra modes
;;
(use-package vimrc-mode :mode ("/\\.?g?vimrc$" "\\.vim$" "/\\.vim/rc/.+$"))
;; Data formats
(use-package yaml-mode :mode "\\.ya?ml$")
(use-package toml-mode :mode "\\.toml$")
(use-package json-mode :mode "\\.js\\(on\\|hintrc\\)$")
2016-02-27 07:12:19 +08:00
;; Configuration formats
2016-02-01 13:18:44 +08:00
(use-package dockerfile-mode :mode "/Dockerfile$"
:config
2016-02-21 04:30:30 +08:00
(define-docset! dockerfile-mode "docker")
2016-02-01 13:18:44 +08:00
(define-builder! dockerfile-mode dockerfile-build-buffer "Dockerfile"))
2015-06-06 18:40:33 +08:00
2016-03-04 04:04:14 +08:00
;;
;; Plugins
;;
2015-06-06 18:40:33 +08:00
2015-06-24 21:34:46 +08:00
(use-package avy
:commands (avy-goto-char-2 avy-goto-line)
:config (setq avy-all-windows nil
avy-background t))
2015-06-06 18:40:33 +08:00
(use-package ace-window
:commands ace-window
:config (setq aw-keys '(?a ?s ?d ?f ?g ?h ?j ?k ?l)
aw-scope 'frame
2015-06-24 21:34:46 +08:00
aw-background t))
2015-06-06 18:40:33 +08:00
2015-12-27 15:50:44 +08:00
(use-package editorconfig
:config
;; Don't affect lisp indentation (just `tab-width')
(setq editorconfig-indentation-alist (delq (assq 'emacs-lisp-mode editorconfig-indentation-alist) editorconfig-indentation-alist))
(editorconfig-mode +1))
2015-12-27 15:50:44 +08:00
2015-07-19 06:43:29 +08:00
(use-package emr
:commands (emr-initialize emr-show-refactor-menu emr-declare-command)
2015-10-28 15:31:51 +08:00
:config (define-key popup-menu-keymap [escape] 'keyboard-quit))
2015-06-06 18:40:33 +08:00
(use-package expand-region
:commands (er/expand-region er/contract-region er/mark-symbol er/mark-word))
(use-package goto-last-change
:commands goto-last-change)
2015-06-06 18:40:33 +08:00
2016-02-24 02:07:21 +08:00
(use-package hideshow
:commands (hs-minor-mode hs-toggle-hiding hs-already-hidden-p)
:config (setq hs-isearch-open t)
:init
(advice-add 'evil-toggle-fold :before 'narf*load-hs-minor-mode)
;; Prettify code folding in emacs ;;;;;;
(define-fringe-bitmap 'hs-marker [16 48 112 240 112 48 16] nil nil 'center)
(defface hs-face '((t (:background "#ff8")))
"Face to hightlight the ... area of hidden regions"
:group 'hideshow)
(defface hs-fringe-face '((t (:foreground "#888")))
"Face used to highlight the fringe on folded regions"
:group 'hideshow)
(setq hs-set-up-overlay
(lambda (ov)
(when (eq 'code (overlay-get ov 'hs))
(let* ((marker-string "*fringe-dummy*")
(marker-length (length marker-string))
(display-string (format " ... " (count-lines (overlay-start ov)
(overlay-end ov)))))
(put-text-property 0 marker-length 'display
(list 'right-fringe 'hs-marker 'hs-fringe-face) marker-string)
(put-text-property 0 (length display-string) 'face 'hs-face display-string)
(overlay-put ov 'before-string marker-string)
(overlay-put ov 'display display-string))))))
(use-package rotate-text
:commands (rotate-text rotate-text-backward)
:init
(add-hook! (emacs-lisp-mode lisp-mode)
(setq rotate-text-local-symbols
'(("t" "nil")
("let" "let*")
("when" "unless")
2016-01-31 10:16:10 +08:00
("append" "prepend")
("add-hook" "add-hook!" "remove-hook"))))
:config
(add-to-list 'rotate-text-words '("true" "false")))
2015-06-06 18:40:33 +08:00
2015-06-15 15:05:52 +08:00
(use-package smart-forward :commands (smart-up smart-down smart-left smart-right))
2015-06-06 18:40:33 +08:00
2015-06-05 06:23:21 +08:00
(use-package smartparens
2015-06-15 15:05:52 +08:00
:functions sp-insert-pair
2015-06-05 06:23:21 +08:00
:config
2015-10-07 08:27:18 +08:00
(setq sp-autowrap-region nil ; let evil-surround handle this
2015-06-15 15:05:52 +08:00
sp-highlight-pair-overlay nil
2015-12-23 15:40:41 +08:00
sp-cancel-autoskip-on-backward-movement nil
2015-06-15 15:05:52 +08:00
sp-show-pair-delay 0)
2015-08-03 00:24:57 +08:00
(smartparens-global-mode 1)
2015-06-15 15:05:52 +08:00
(require 'smartparens-config)
2015-12-23 15:40:41 +08:00
;; Smartparens interferes with Replace mode
(add-hook 'evil-replace-state-entry-hook 'turn-off-smartparens-mode)
2016-03-19 16:26:20 +08:00
(add-hook 'evil-replace-state-exit-hook 'turn-on-smartparens-mode)
2015-12-23 15:40:41 +08:00
;; Auto-close more conservatively
2016-01-18 14:44:15 +08:00
(sp-pair "{" nil :post-handlers '(("||\n[i]" "RET") ("| " " "))
2015-08-03 00:24:57 +08:00
:unless '(sp-point-before-word-p sp-point-before-same-p))
2016-01-18 14:44:15 +08:00
(sp-pair "(" nil :post-handlers '(("||\n[i]" "RET") ("| " " "))
2015-08-03 00:24:57 +08:00
:unless '(sp-point-before-word-p sp-point-before-same-p))
2016-01-18 14:44:15 +08:00
(sp-pair "[" nil :post-handlers '(("| " " "))
2015-12-23 15:40:41 +08:00
:unless '(sp-point-before-word-p sp-point-before-same-p))
2015-07-25 19:50:30 +08:00
2016-03-19 16:26:20 +08:00
(defun sp-point-in-string-p (id action context)
(when (eq action 'insert)
(sp-point-in-string)))
2015-12-23 15:40:41 +08:00
(defun sp-insert-yasnippet (id action context)
(forward-char -1)
(if (sp-point-after-bol-p id action context)
(yas-expand-from-trigger-key)
(forward-char)))
2016-03-19 16:26:20 +08:00
(defun sp--org-skip-> (ms mb me)
(or (and (= (line-beginning-position) mb)
(eq ?> (char-after (1+ mb))))
(and (= (1+ (line-beginning-position)) me)
(eq ?> (char-after me)))))
(sp-local-pair '(sh-mode markdown-mode) "`" "`" :unless '(sp-point-before-word-p sp-point-before-same-p))
(sp-local-pair 'markdown-mode "```" "```" :post-handlers '(("||\n[i]" "RET")) :unless '(sp-point-before-word-p sp-point-before-same-p))
(sp-local-pair '(scss-mode css-mode) "/*" "*/" :post-handlers '(("[d-3]||\n[i]" "RET") ("| " "SPC")))
2015-12-23 15:40:41 +08:00
(sp-with-modes '(sh-mode)
(sp-local-pair "case" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert))
(sp-local-pair "if" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert))
(sp-local-pair "for" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert))
(sp-local-pair "elif" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert))
(sp-local-pair "while" "" :when '(("SPC")) :post-handlers '((:add sp-insert-yasnippet)) :actions '(insert)))
(sp-with-modes '(c-mode c++-mode objc-mode php-mode java-mode)
(sp-local-pair "/*" "" :post-handlers '(("||\n[i]*/" "RET") ("| */" "SPC")))
;; Doxygen blocks
(sp-local-pair "/**" "*/" :post-handlers '(("||\n[i]" "RET") ("||\n[i]" "SPC")))
(sp-local-pair "/*!" "*/" :post-handlers '(("||\n[i]" "RET") ("[d-1]< | " "SPC"))))
2016-03-19 16:26:20 +08:00
(defun sp--org-skip-asterisk (ms mb me)
(or (and (= (line-beginning-position) mb)
(eq 32 (char-after (1+ mb))))
(and (= (1+ (line-beginning-position)) me)
(eq 32 (char-after me)))))
2015-11-24 04:21:24 +08:00
(sp-with-modes '(org-mode)
2016-03-19 16:26:20 +08:00
(sp-local-pair "*" "*" :unless '(sp-point-after-word-p sp-point-at-bol-p) :skip-match 'sp--org-skip-asterisk)
(sp-local-pair "_" "_" :unless '(sp-point-before-word-p sp-point-after-word-p))
(sp-local-pair "/" "/" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC")))
(sp-local-pair "~" "~" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC")))
(sp-local-pair "=" "=" :unless '(sp-point-before-word-p sp-point-after-word-p) :post-handlers '(("[d1]" "SPC")))
2015-12-23 15:40:41 +08:00
(sp-local-pair "\\[" "\\]" :post-handlers '(("| " "SPC")))
(sp-local-pair "\\(" "\\)" :post-handlers '(("| " "SPC")))
2016-03-19 16:26:20 +08:00
(sp-local-pair "$$" "$$" :post-handlers '((:add " | ")) :unless '(sp-point-at-bol-p))
2015-11-24 04:21:24 +08:00
(sp-local-pair "{" nil))
2015-06-15 15:05:52 +08:00
2015-12-23 15:40:41 +08:00
;; Markup languages
(sp-with-modes '(xml-mode nxml-mode php-mode)
(sp-local-pair "<!--" "-->" :post-handlers '(("| " "SPC"))))
(sp-with-modes '(web-mode php-mode)
(sp-local-pair "<?" "?>" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))
(sp-local-pair "<?php " " ?>" :post-handlers '(("||\n[i]" "RET") ("| " "SPC"))))
(sp-with-modes '(web-mode)
(sp-local-pair "{{!--" "--}}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))
(sp-local-pair "<%" "%>" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))
(sp-local-pair "{!!" "!!}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))
(sp-local-pair "{#" "#}" :post-handlers '(("||\n[i]" "RET") ("| " "SPC")))))
2015-06-05 06:23:21 +08:00
2015-10-28 15:31:51 +08:00
(use-package help-fns+ ; Improved help commands
:commands (describe-buffer describe-command describe-file
describe-keymap describe-option describe-option-of-type))
(use-package re-builder
:commands (re-builder reb-mode-buffer-p)
:init (add-hook! reb-mode 'narf|reb-cleanup)
:config
(setq reb-re-syntax 'string)
(evil-set-initial-state 'reb-mode 'insert)
(map! :map rxt-help-mode-map :n [escape] 'kill-buffer-and-window)
(map! :map reb-mode-map
:n "C-g" 'reb-quit
:n [escape] 'reb-quit
:n [backtab] 'reb-change-syntax))
2015-06-05 06:23:21 +08:00
(provide 'core-editor)
;;; core-editor.el ends here