2017-11-04 22:11:55 +01:00
|
|
|
;; feature/evil/autoload/evil.el -*- lexical-binding: t; -*-
|
|
|
|
;;;###if (featurep! :feature evil)
|
2017-01-31 19:50:02 -05:00
|
|
|
|
2017-02-19 18:14:46 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +evil/visual-indent ()
|
|
|
|
"vnoremap < <gv"
|
|
|
|
(interactive)
|
|
|
|
(evil-shift-right (region-beginning) (region-end))
|
|
|
|
(evil-normal-state)
|
|
|
|
(evil-visual-restore))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +evil/visual-dedent ()
|
|
|
|
"vnoremap > >gv"
|
|
|
|
(interactive)
|
|
|
|
(evil-shift-left (region-beginning) (region-end))
|
|
|
|
(evil-normal-state)
|
|
|
|
(evil-visual-restore))
|
|
|
|
|
2017-03-02 11:42:09 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +evil/reselect-paste ()
|
|
|
|
"Go back into visual mode and reselect the last pasted region."
|
|
|
|
(interactive)
|
2017-06-25 02:04:50 +02:00
|
|
|
(cl-destructuring-bind (_ _ _ beg end &optional _)
|
2017-06-22 20:03:27 +02:00
|
|
|
evil-last-paste
|
2017-06-07 16:14:05 +02:00
|
|
|
(evil-visual-make-selection
|
|
|
|
(save-excursion (goto-char beg) (point-marker))
|
|
|
|
end)))
|
2017-03-02 11:42:09 -05:00
|
|
|
|
2017-05-15 13:52:22 +02:00
|
|
|
(defun +evil--window-swap (direction)
|
2017-01-31 19:50:02 -05:00
|
|
|
"Move current window to the next window in DIRECTION. If there are no windows
|
|
|
|
there and there is only one window, split in that direction and place this
|
|
|
|
window there. If there are no windows and this isn't the only window, use
|
|
|
|
evil-window-move-* (e.g. `evil-window-move-far-left')"
|
2018-01-06 01:23:22 -05:00
|
|
|
(when (window-dedicated-p)
|
|
|
|
(user-error "Cannot swap a dedicated window"))
|
|
|
|
(let* ((this-window (selected-window))
|
2017-09-25 13:02:20 +02:00
|
|
|
(this-buffer (current-buffer))
|
|
|
|
(that-window (windmove-find-other-window direction nil this-window))
|
|
|
|
(that-buffer (window-buffer that-window)))
|
|
|
|
(when (or (minibufferp that-buffer)
|
2018-01-06 01:23:22 -05:00
|
|
|
(window-dedicated-p this-window))
|
2017-09-25 13:02:20 +02:00
|
|
|
(setq that-buffer nil that-window nil))
|
|
|
|
(if (not (or that-window (one-window-p t)))
|
|
|
|
(funcall (pcase direction
|
|
|
|
('left #'evil-window-move-far-left)
|
|
|
|
('right #'evil-window-move-far-right)
|
|
|
|
('up #'evil-window-move-very-top)
|
|
|
|
('down #'evil-window-move-very-bottom)))
|
|
|
|
(unless that-window
|
|
|
|
(setq that-window
|
|
|
|
(split-window this-window nil
|
|
|
|
(pcase direction
|
|
|
|
('up 'above)
|
|
|
|
('down 'below)
|
|
|
|
(_ direction))))
|
2017-01-31 19:50:02 -05:00
|
|
|
(with-selected-window that-window
|
2017-10-07 20:32:48 +02:00
|
|
|
(switch-to-buffer (doom-fallback-buffer)))
|
2017-09-25 13:02:20 +02:00
|
|
|
(setq that-buffer (window-buffer that-window)))
|
|
|
|
(with-selected-window this-window
|
|
|
|
(switch-to-buffer that-buffer))
|
|
|
|
(with-selected-window that-window
|
|
|
|
(switch-to-buffer this-buffer))
|
|
|
|
(select-window that-window))))
|
2017-01-31 19:50:02 -05:00
|
|
|
|
2017-02-19 18:14:46 -05:00
|
|
|
;;;###autoload
|
2017-06-19 00:30:41 +02:00
|
|
|
(defun +evil/window-move-left () "See `+evil--window-swap'" (interactive) (+evil--window-swap 'left))
|
2017-02-19 18:14:46 -05:00
|
|
|
;;;###autoload
|
2017-06-19 00:30:41 +02:00
|
|
|
(defun +evil/window-move-right () "See `+evil--window-swap'" (interactive) (+evil--window-swap 'right))
|
2017-02-19 18:14:46 -05:00
|
|
|
;;;###autoload
|
2017-06-19 00:30:41 +02:00
|
|
|
(defun +evil/window-move-up () "See `+evil--window-swap'" (interactive) (+evil--window-swap 'up))
|
2017-02-19 18:14:46 -05:00
|
|
|
;;;###autoload
|
2017-06-19 00:30:41 +02:00
|
|
|
(defun +evil/window-move-down () "See `+evil--window-swap'" (interactive) (+evil--window-swap 'down))
|
2017-02-19 18:14:46 -05:00
|
|
|
|
|
|
|
;;;###autoload (autoload '+evil:macro-on-all-lines "feature/evil/autoload/evil" nil t)
|
2018-03-24 07:11:15 -04:00
|
|
|
(evil-define-command +evil:macro-on-all-lines (beg end macro)
|
2017-02-03 19:24:04 -05:00
|
|
|
"Apply macro to each line."
|
2018-03-24 07:11:15 -04:00
|
|
|
:move-point t
|
|
|
|
:keep-visual t
|
|
|
|
(interactive
|
|
|
|
(let (macro register)
|
|
|
|
(setq register (or evil-this-register (read-char)))
|
|
|
|
(cond
|
|
|
|
((or (and (eq register ?@) (eq evil-last-register ?:))
|
|
|
|
(eq register ?:))
|
|
|
|
(setq macro (lambda () (evil-ex-repeat nil))
|
|
|
|
evil-last-register ?:))
|
|
|
|
((eq register ?@)
|
|
|
|
(unless evil-last-register
|
|
|
|
(user-error "No previously executed keyboard macro."))
|
|
|
|
(setq macro (evil-get-register evil-last-register t)))
|
|
|
|
(t
|
|
|
|
(setq macro (evil-get-register register t)
|
|
|
|
evil-last-register register)))
|
|
|
|
(list evil-visual-beginning evil-visual-end macro)))
|
|
|
|
(evil-change-state 'normal)
|
|
|
|
(evil-with-single-undo
|
|
|
|
(apply-macro-to-region-lines beg end macro)))
|
2017-02-19 18:14:46 -05:00
|
|
|
|
2017-05-15 13:52:22 +02:00
|
|
|
;;;###autoload (autoload '+evil:retab "feature/evil/autoload/evil" nil t)
|
|
|
|
(evil-define-operator +evil:retab (&optional beg end)
|
|
|
|
"Wrapper around `doom/retab'."
|
|
|
|
:motion nil :move-point nil :type line
|
|
|
|
(interactive "<r>")
|
|
|
|
(doom/retab beg end))
|
|
|
|
|
|
|
|
;;;###autoload (autoload '+evil:narrow-buffer "feature/evil/autoload/evil" nil t)
|
|
|
|
(evil-define-command +evil:narrow-buffer (beg end &optional bang)
|
|
|
|
"Wrapper around `doom-narrow-buffer'."
|
|
|
|
:move-point nil
|
|
|
|
(interactive "<r><!>")
|
|
|
|
(doom-narrow-buffer beg end bang))
|
|
|
|
|
|
|
|
|
|
|
|
;; --- custom arg handlers ----------------
|
|
|
|
|
2017-06-08 11:47:56 +02:00
|
|
|
(defvar +evil--flag nil)
|
|
|
|
|
2017-05-15 13:52:22 +02:00
|
|
|
(defun +evil--ex-match-init (name &optional face update-hook)
|
|
|
|
(with-current-buffer evil-ex-current-buffer
|
|
|
|
(cond
|
2017-06-08 11:47:56 +02:00
|
|
|
((eq +evil--flag 'start)
|
2017-05-15 13:52:22 +02:00
|
|
|
(evil-ex-make-hl name
|
|
|
|
:face (or face 'evil-ex-substitute-matches)
|
|
|
|
:update-hook (or update-hook #'evil-ex-pattern-update-ex-info))
|
2017-06-08 11:47:56 +02:00
|
|
|
(setq +evil--flag 'update))
|
2017-05-15 13:52:22 +02:00
|
|
|
|
2017-06-08 11:47:56 +02:00
|
|
|
((eq +evil--flag 'stop)
|
2017-05-15 13:52:22 +02:00
|
|
|
(evil-ex-delete-hl name)))))
|
|
|
|
|
|
|
|
(defun +evil--ex-buffer-match (arg &optional hl-name flags beg end)
|
2017-06-08 11:47:56 +02:00
|
|
|
(when (and (eq +evil--flag 'update)
|
2017-05-15 13:52:22 +02:00
|
|
|
evil-ex-substitute-highlight-all
|
|
|
|
(not (zerop (length arg))))
|
|
|
|
(condition-case lossage
|
|
|
|
(let ((pattern (evil-ex-make-substitute-pattern
|
2018-03-20 23:50:03 -04:00
|
|
|
arg
|
2017-05-15 13:52:22 +02:00
|
|
|
(or flags (list))))
|
|
|
|
(range (or (evil-copy-range evil-ex-range)
|
|
|
|
(evil-range (or beg (line-beginning-position))
|
|
|
|
(or end (line-end-position))
|
|
|
|
'line
|
|
|
|
:expanded t))))
|
|
|
|
(evil-expand-range range)
|
|
|
|
(evil-ex-hl-set-region hl-name
|
|
|
|
(max (evil-range-beginning range) (window-start))
|
|
|
|
(min (evil-range-end range) (window-end)))
|
|
|
|
(evil-ex-hl-change hl-name pattern))
|
|
|
|
(end-of-file
|
|
|
|
(evil-ex-pattern-update-ex-info nil "incomplete replacement"))
|
|
|
|
(user-error
|
|
|
|
(evil-ex-pattern-update-ex-info nil (format "?%s" lossage))))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +evil-ex-buffer-match (flag &optional arg)
|
2017-06-08 11:47:56 +02:00
|
|
|
(let ((hl-name 'evil-ex-buffer-match)
|
|
|
|
(+evil--flag flag))
|
2017-05-15 13:52:22 +02:00
|
|
|
(with-selected-window (minibuffer-selected-window)
|
|
|
|
(+evil--ex-match-init hl-name)
|
2017-05-27 18:57:46 +02:00
|
|
|
(+evil--ex-buffer-match arg hl-name (list (if evil-ex-substitute-global ?g))))))
|
2017-05-15 13:52:22 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +evil-ex-global-match (flag &optional arg)
|
2017-06-08 11:47:56 +02:00
|
|
|
(let ((hl-name 'evil-ex-global-match)
|
|
|
|
(+evil--flag flag))
|
2017-05-15 13:52:22 +02:00
|
|
|
(with-selected-window (minibuffer-selected-window)
|
|
|
|
(+evil--ex-match-init hl-name)
|
2017-05-27 18:57:46 +02:00
|
|
|
(+evil--ex-buffer-match arg hl-name nil (point-min) (point-max)))))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +evil-ex-global-delim-match (flag &optional arg)
|
2017-06-08 11:47:56 +02:00
|
|
|
(let ((hl-name 'evil-ex-global-delim-match)
|
|
|
|
(+evil--flag flag))
|
2017-05-27 18:57:46 +02:00
|
|
|
(with-selected-window (minibuffer-selected-window)
|
|
|
|
(+evil--ex-match-init hl-name)
|
|
|
|
(let ((result (car-safe (evil-delimited-arguments arg 2))))
|
2017-05-15 13:52:22 +02:00
|
|
|
(+evil--ex-buffer-match result hl-name nil (point-min) (point-max))))))
|
2017-05-17 18:24:16 +02:00
|
|
|
|
2017-05-27 18:57:46 +02:00
|
|
|
;;;###autoload (autoload '+evil:align "feature/evil/autoload/evil" nil t)
|
|
|
|
(evil-define-operator +evil:align (beg end pattern &optional bang)
|
2018-03-20 23:55:24 -04:00
|
|
|
"Ex interface to `align-regexp'. PATTERN is a vim-style regexp. If BANG,
|
|
|
|
repeat the alignment for all matches (otherwise just the first match on each
|
|
|
|
line)."
|
|
|
|
(interactive "<r><//g><!>")
|
2017-05-27 18:57:46 +02:00
|
|
|
(align-regexp
|
|
|
|
beg end
|
2018-03-20 23:55:24 -04:00
|
|
|
(concat "\\(\\s-*\\)" (evil-transform-vim-style-regexp pattern))
|
|
|
|
1 1 bang))
|
2017-05-27 18:57:46 +02:00
|
|
|
|
2018-03-20 23:55:33 -04:00
|
|
|
;;;###autoload (autoload '+evil:align-right "feature/evil/autoload/evil" nil t)
|
|
|
|
(evil-define-operator +evil:align-right (beg end pattern &optional bang)
|
|
|
|
"Like `+evil:align', except alignments are right-justified. PATTERN is a
|
|
|
|
vim-style regexp. If BANG, repeat the alignment for all matches (otherwise just
|
|
|
|
the first match on each line)."
|
|
|
|
(interactive "<r><//g><!>")
|
|
|
|
(align-regexp
|
|
|
|
beg end
|
|
|
|
(concat "\\(" (evil-transform-vim-style-regexp pattern) "\\)")
|
|
|
|
-1 1 bang))
|
|
|
|
|
2017-05-17 18:24:16 +02:00
|
|
|
|
|
|
|
;; --- wgrep ------------------------------
|
|
|
|
|
|
|
|
;;;###autoload (autoload '+evil-delete "feature/evil/autoload/evil" nil t)
|
|
|
|
(evil-define-operator +evil-delete (beg end type register yank-handler)
|
|
|
|
"A wrapper around `evil-delete' for `wgrep' buffers that will invoke
|
|
|
|
`wgrep-mark-deletion' on lines you try to delete."
|
|
|
|
(interactive "<R><x><y>")
|
2017-06-08 11:47:56 +02:00
|
|
|
(condition-case _ex
|
2017-05-17 18:24:16 +02:00
|
|
|
(evil-delete beg end type register yank-handler)
|
|
|
|
('text-read-only
|
|
|
|
(evil-apply-on-block
|
|
|
|
(lambda (beg _)
|
|
|
|
(goto-char beg)
|
|
|
|
(call-interactively #'wgrep-mark-deletion))
|
|
|
|
beg (1- end) nil))))
|
2018-01-06 18:50:49 -05:00
|
|
|
|
|
|
|
|
|
|
|
;;
|
|
|
|
;; Advice
|
|
|
|
;;
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +evil*static-reindent (orig-fn &rest args)
|
|
|
|
"Don't move cursor on indent."
|
|
|
|
(save-excursion (apply orig-fn args)))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-02-02 14:52:18 -05:00
|
|
|
(defun +evil*restore-initial-state-on-windmove (orig-fn &rest args)
|
|
|
|
"Revert buffer to its initial state when switching to another window. This
|
|
|
|
prevents states from bleeding into other modes across windows."
|
|
|
|
(let ((initial-state (evil-initial-state major-mode 'normal)))
|
|
|
|
(unless (eq evil-state initial-state)
|
|
|
|
(evil-change-state initial-state)))
|
2018-01-06 18:50:49 -05:00
|
|
|
(apply orig-fn args))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun +evil*resolve-vim-path (file-name)
|
|
|
|
"Take a path and resolve any vim-like filename modifiers in it. This adds
|
|
|
|
support for most vim file modifiers, as well as:
|
|
|
|
|
|
|
|
%:P Resolves to `doom-project-root'.
|
|
|
|
|
|
|
|
See http://vimdoc.sourceforge.net/htmldoc/cmdline.html#filename-modifiers for
|
|
|
|
more information on modifiers."
|
|
|
|
(let* (case-fold-search
|
|
|
|
(regexp (concat "\\(?:^\\|[^\\\\]\\)"
|
|
|
|
"\\([#%]\\)"
|
|
|
|
"\\(\\(?::\\(?:[PphtreS~.]\\|g?s[^:\t\n ]+\\)\\)*\\)"))
|
|
|
|
(matches
|
|
|
|
(cl-loop with i = 0
|
|
|
|
while (and (< i (length file-name))
|
|
|
|
(string-match regexp file-name i))
|
|
|
|
do (setq i (1+ (match-beginning 0)))
|
|
|
|
and collect
|
|
|
|
(cl-loop for j to (/ (length (match-data)) 2)
|
|
|
|
collect (match-string j file-name)))))
|
|
|
|
(dolist (match matches)
|
|
|
|
(let ((flags (split-string (car (cdr (cdr match))) ":" t))
|
|
|
|
(path (and buffer-file-name
|
|
|
|
(pcase (car (cdr match))
|
|
|
|
("%" (file-relative-name buffer-file-name))
|
|
|
|
("#" (save-excursion (other-window 1) (file-relative-name buffer-file-name))))))
|
|
|
|
flag global)
|
|
|
|
(if (not path)
|
|
|
|
(setq path "")
|
|
|
|
(while flags
|
|
|
|
(setq flag (pop flags))
|
|
|
|
(when (string-suffix-p "\\" flag)
|
|
|
|
(setq flag (concat flag (pop flags))))
|
|
|
|
(when (string-prefix-p "gs" flag)
|
|
|
|
(setq global t
|
|
|
|
flag (substring flag 1)))
|
|
|
|
(setq path
|
|
|
|
(or (pcase (substring flag 0 1)
|
|
|
|
("p" (expand-file-name path))
|
|
|
|
("~" (concat "~/" (file-relative-name path "~")))
|
|
|
|
("." (file-relative-name path default-directory))
|
|
|
|
("t" (file-name-nondirectory (directory-file-name path)))
|
|
|
|
("r" (file-name-sans-extension path))
|
|
|
|
("e" (file-name-extension path))
|
|
|
|
("S" (shell-quote-argument path))
|
|
|
|
("h"
|
|
|
|
(let ((parent (file-name-directory (expand-file-name path))))
|
|
|
|
(unless (equal (file-truename path)
|
|
|
|
(file-truename parent))
|
|
|
|
(if (file-name-absolute-p path)
|
|
|
|
(directory-file-name parent)
|
|
|
|
(file-relative-name parent)))))
|
|
|
|
("s"
|
|
|
|
(if (featurep 'evil)
|
|
|
|
(when-let* ((args (evil-delimited-arguments (substring flag 1) 2)))
|
|
|
|
(let ((pattern (evil-transform-vim-style-regexp (car args)))
|
|
|
|
(replace (cadr args)))
|
|
|
|
(replace-regexp-in-string
|
|
|
|
(if global pattern (concat "\\(" pattern "\\).*\\'"))
|
|
|
|
(evil-transform-vim-style-regexp replace) path t t
|
|
|
|
(unless global 1))))
|
|
|
|
path))
|
|
|
|
("P"
|
|
|
|
(let ((default-directory (file-name-directory (expand-file-name path))))
|
|
|
|
(abbreviate-file-name (doom-project-root))))
|
|
|
|
(_ path))
|
|
|
|
"")))
|
|
|
|
;; strip trailing slash, if applicable
|
|
|
|
(when (and (not (string= path "")) (equal (substring path -1) "/"))
|
|
|
|
(setq path (substring path 0 -1))))
|
|
|
|
(setq file-name
|
|
|
|
(replace-regexp-in-string (format "\\(?:^\\|[^\\\\]\\)\\(%s\\)"
|
|
|
|
(regexp-quote (string-trim-left (car match))))
|
|
|
|
path file-name t t 1))))
|
|
|
|
(replace-regexp-in-string regexp "\\1" file-name t)))
|