2015-06-15 09:05:52 +02:00
|
|
|
;;; defuns-evil.el
|
|
|
|
|
2016-09-30 14:37:25 +02:00
|
|
|
;;;###autoload (autoload 'doom/evil-open-folds "defuns-evil" nil t)
|
2016-05-20 22:37:30 -04:00
|
|
|
(evil-define-command doom/evil-open-folds (count)
|
2016-09-30 14:37:25 +02:00
|
|
|
"Instead of `evil-open-folds'; accepts COUNT for dictating fold level."
|
2015-06-15 09:05:52 +02:00
|
|
|
(interactive "P")
|
|
|
|
(unless (bound-and-true-p hs-minor-mode)
|
|
|
|
(hs-minor-mode 1))
|
|
|
|
(if count (hs-hide-level count) (evil-open-folds)))
|
|
|
|
|
2016-09-30 14:37:25 +02:00
|
|
|
;;;###autoload (autoload 'doom/evil-open-folds "defuns-evil" nil t)
|
2016-05-20 22:37:30 -04:00
|
|
|
(evil-define-command doom/evil-close-folds (count)
|
2016-09-30 14:37:25 +02:00
|
|
|
"Instead of `evil-close-folds'; accepts COUNT for dictating fold level."
|
2015-06-15 09:05:52 +02:00
|
|
|
(interactive "P")
|
|
|
|
(unless (bound-and-true-p hs-minor-mode)
|
|
|
|
(hs-minor-mode 1))
|
|
|
|
(if count (hs-hide-level count) (evil-close-folds)))
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom/multi-next-line "defuns-evil" nil t)
|
|
|
|
(evil-define-motion doom/multi-next-line (count)
|
2016-09-30 14:37:25 +02:00
|
|
|
"Move down 6 lines."
|
2016-03-27 18:14:08 -04:00
|
|
|
:type line
|
|
|
|
(let ((line-move-visual visual-line-mode))
|
|
|
|
(evil-line-move (* 6 (or count 1)))))
|
2015-09-30 13:49:37 -04:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom/multi-previous-line "defuns-evil" nil t)
|
|
|
|
(evil-define-motion doom/multi-previous-line (count)
|
2016-09-30 14:37:25 +02:00
|
|
|
"Move up 6 lines."
|
2016-03-27 18:14:08 -04:00
|
|
|
:type line
|
|
|
|
(let ((line-move-visual visual-line-mode))
|
|
|
|
(evil-line-move (- (* 6 (or count 1))))))
|
2015-06-15 09:05:52 +02:00
|
|
|
|
|
|
|
;;;###autoload
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom/evil-visual-line-state-p ()
|
2015-06-15 09:05:52 +02:00
|
|
|
"Returns non-nil if in visual-line mode, nil otherwise."
|
|
|
|
(and (evil-visual-state-p)
|
|
|
|
(eq (evil-visual-type) 'line)))
|
|
|
|
|
|
|
|
;;;###autoload
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom*evil-exchange-off ()
|
2015-06-15 09:05:52 +02:00
|
|
|
(when evil-exchange--overlays
|
|
|
|
(evil-exchange-cancel)))
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom/evil-macro-on-all-lines "defuns-evil" nil t)
|
|
|
|
(evil-define-operator doom/evil-macro-on-all-lines (beg end &optional macro)
|
2015-11-25 06:00:49 -05:00
|
|
|
"Apply macro to each line."
|
|
|
|
:motion nil
|
|
|
|
:move-point nil
|
|
|
|
(interactive "<r><a>")
|
|
|
|
(unless (and beg end)
|
|
|
|
(setq beg (region-beginning)
|
|
|
|
end (region-end)))
|
|
|
|
(evil-ex-normal beg end
|
|
|
|
(concat "@"
|
|
|
|
(single-key-description
|
|
|
|
(or macro (read-char "@-"))))))
|
2015-10-28 17:28:41 -04:00
|
|
|
|
2015-12-11 02:21:49 -05:00
|
|
|
;;; Custom argument handlers
|
2016-05-20 22:37:30 -04:00
|
|
|
(defvar doom-buffer-match-global evil-ex-substitute-global "")
|
|
|
|
(defun doom--evil-ex-match-init (name &optional face update-hook)
|
2015-12-11 02:21:49 -05:00
|
|
|
(with-current-buffer evil-ex-current-buffer
|
|
|
|
(cond
|
|
|
|
((eq flag 'start)
|
|
|
|
(evil-ex-make-hl name
|
|
|
|
:face (or face 'evil-ex-substitute-matches)
|
|
|
|
:update-hook (or update-hook #'evil-ex-pattern-update-ex-info))
|
|
|
|
(setq flag 'update))
|
|
|
|
|
|
|
|
((eq flag 'stop)
|
|
|
|
(evil-ex-delete-hl name)))))
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom--evil-ex-buffer-match (arg &optional hl-name flags beg end)
|
2015-12-11 02:21:49 -05:00
|
|
|
(when (and (eq flag 'update)
|
|
|
|
evil-ex-substitute-highlight-all
|
|
|
|
(not (zerop (length arg))))
|
|
|
|
(condition-case lossage
|
|
|
|
(let ((pattern (evil-ex-make-substitute-pattern
|
|
|
|
(if evil-ex-bang (regexp-quote arg) arg)
|
|
|
|
(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
|
2016-02-18 04:07:59 -05:00
|
|
|
(max (evil-range-beginning range) (window-start))
|
|
|
|
(min (evil-range-end range) (window-end)))
|
2015-12-11 02:21:49 -05:00
|
|
|
(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))))))
|
|
|
|
|
2016-05-20 19:08:02 -04:00
|
|
|
;;;###autoload
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom/evil-ex-buffer-match (flag &optional arg)
|
2016-05-20 19:08:02 -04:00
|
|
|
(let ((hl-name 'evil-ex-buffer-match))
|
|
|
|
(with-selected-window (minibuffer-selected-window)
|
2016-05-20 22:37:30 -04:00
|
|
|
(doom--evil-ex-match-init hl-name)
|
|
|
|
(doom--evil-ex-buffer-match arg hl-name (list (if doom-buffer-match-global ?g))))))
|
2016-05-20 19:08:02 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom/evil-ex-global-match (flag &optional arg)
|
2016-05-20 19:08:02 -04:00
|
|
|
(let ((hl-name 'evil-ex-global-match))
|
|
|
|
(with-selected-window (minibuffer-selected-window)
|
2016-05-20 22:37:30 -04:00
|
|
|
(doom--evil-ex-match-init hl-name)
|
2016-05-20 19:08:02 -04:00
|
|
|
(let ((result (car-safe (evil-ex-parse-global arg))))
|
2016-05-20 22:37:30 -04:00
|
|
|
(doom--evil-ex-buffer-match result hl-name nil (point-min) (point-max))))))
|
2016-05-20 19:08:02 -04:00
|
|
|
|
2016-03-31 03:17:15 -04:00
|
|
|
;;;###autoload
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom/evil-ex-undefine-cmd (cmd)
|
2016-03-31 03:17:15 -04:00
|
|
|
(if (string-match "^[^][]*\\(\\[\\(.*\\)\\]\\)[^][]*$" cmd)
|
|
|
|
(let ((abbrev (replace-match "" nil t cmd 1))
|
|
|
|
(full (replace-match "\\2" nil nil cmd 1)))
|
|
|
|
(setq evil-ex-commands (delq (assoc full evil-ex-commands) evil-ex-commands))
|
|
|
|
(setq evil-ex-commands (delq (assoc abbrev evil-ex-commands) evil-ex-commands)))
|
|
|
|
(setq evil-ex-commands (delq (assoc cmd evil-ex-commands) evil-ex-commands))))
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
(defvar doom:map-maps '())
|
2016-04-04 12:07:32 -04:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:map "defuns-evil" nil t)
|
|
|
|
(evil-define-command doom:map (bang input &optional mode)
|
2016-04-04 12:07:32 -04:00
|
|
|
"Map ex commands to keybindings. INPUT should be in the format [KEY] [EX COMMAND]."
|
|
|
|
(interactive "<!><a>")
|
|
|
|
(let* ((parts (s-split-up-to " " input 2 t))
|
|
|
|
(mode (or mode 'normal))
|
|
|
|
(key (kbd (car parts)))
|
|
|
|
(command (s-join " " (cdr parts)))
|
|
|
|
(map (cl-case mode
|
|
|
|
('normal evil-normal-state-local-map)
|
|
|
|
('insert evil-insert-state-local-map)
|
|
|
|
('visual evil-visual-state-local-map)
|
|
|
|
('motion evil-motion-state-local-map)
|
|
|
|
('operator evil-operator-state-local-map)))
|
|
|
|
(fn `(lambda () (interactive) (evil-ex-eval ,command))))
|
|
|
|
(if bang
|
|
|
|
(evil-define-key mode nil key fn)
|
|
|
|
(define-key map key fn))))
|
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:nmap "defuns-evil" nil t)
|
|
|
|
(evil-define-command doom:nmap (bang input &optional mode)
|
|
|
|
(interactive "<!><a>") (doom:map bang input 'normal))
|
2016-04-04 12:07:32 -04:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:imap "defuns-evil" nil t)
|
|
|
|
(evil-define-command doom:imap (bang input &optional mode)
|
|
|
|
(interactive "<!><a>") (doom:map bang input 'insert))
|
2016-04-04 12:07:32 -04:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:vmap "defuns-evil" nil t)
|
|
|
|
(evil-define-command doom:vmap (bang input &optional mode)
|
|
|
|
(interactive "<!><a>") (doom:map bang input 'visual))
|
2016-04-04 12:07:32 -04:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:mmap "defuns-evil" nil t)
|
|
|
|
(evil-define-command doom:mmap (bang input &optional mode)
|
|
|
|
(interactive "<!><a>") (doom:map bang input 'motion))
|
2016-04-04 12:07:32 -04:00
|
|
|
|
2016-05-20 22:37:30 -04:00
|
|
|
;;;###autoload (autoload 'doom:omap "defuns-evil" nil t)
|
|
|
|
(evil-define-command doom:omap (bang input &optional mode)
|
|
|
|
(interactive "<!><a>") (doom:map bang input 'operator))
|
2016-04-04 12:07:32 -04:00
|
|
|
|
2016-04-10 18:48:05 -04:00
|
|
|
;;;###autoload
|
2016-05-20 22:37:30 -04:00
|
|
|
(defun doom/evil-snipe-easymotion ()
|
2016-04-10 18:48:05 -04:00
|
|
|
(interactive)
|
|
|
|
(require 'evil-easymotion)
|
2016-05-20 22:37:30 -04:00
|
|
|
(call-interactively doom--evil-snipe-repeat-fn))
|
2016-04-10 18:48:05 -04:00
|
|
|
|
2016-06-04 23:39:03 -04:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom/evil-matchit ()
|
|
|
|
(interactive)
|
|
|
|
(if (ignore-errors (hs-already-hidden-p))
|
|
|
|
(hs-toggle-hiding)
|
|
|
|
(call-interactively 'evilmi-jump-items)))
|
|
|
|
|
2016-06-05 23:08:46 -04:00
|
|
|
;;;###autoload
|
|
|
|
(defun doom*evil-command-window (hist cmd-key execute-fn)
|
|
|
|
"The evil command window has a mind of its own (uses `switch-to-buffer'). We
|
|
|
|
monkey patch it to use pop-to-buffer."
|
|
|
|
(when (eq major-mode 'evil-command-window-mode)
|
|
|
|
(user-error "Cannot recursively open command line window"))
|
|
|
|
(dolist (win (window-list))
|
|
|
|
(when (equal (buffer-name (window-buffer win))
|
|
|
|
"*Command Line*")
|
|
|
|
(kill-buffer (window-buffer win))
|
|
|
|
(delete-window win)))
|
|
|
|
(setq evil-command-window-current-buffer (current-buffer))
|
|
|
|
(ignore-errors (kill-buffer "*Command Line*"))
|
|
|
|
(with-current-buffer (pop-to-buffer "*Command Line*")
|
|
|
|
(setq-local evil-command-window-execute-fn execute-fn)
|
|
|
|
(setq-local evil-command-window-cmd-key cmd-key)
|
|
|
|
(evil-command-window-mode)
|
|
|
|
(evil-command-window-insert-commands hist)))
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom*evil-esc-quit ()
|
|
|
|
"Close popups, disable search highlights and quit the minibuffer if open."
|
2016-06-17 23:51:18 -04:00
|
|
|
(let ((minib-p (minibuffer-window-active-p (minibuffer-window)))
|
|
|
|
(evil-hl-p (evil-ex-hl-active-p 'evil-ex-search)))
|
|
|
|
(when minib-p (abort-recursive-edit))
|
|
|
|
(when evil-hl-p (evil-ex-nohighlight))
|
|
|
|
;; Close non-repl popups and clean up `doom-popup-windows'
|
|
|
|
(unless (or minib-p evil-hl-p)
|
|
|
|
(doom/popup-close-all))))
|
2016-06-05 23:08:46 -04:00
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(defun doom*evil-ex-replace-special-filenames (file-name)
|
|
|
|
"Replace special symbols in FILE-NAME."
|
2016-06-06 23:50:10 -04:00
|
|
|
(let ((regexp (concat "\\(?:^\\|[^\\\\]\\)"
|
2016-06-05 23:08:46 -04:00
|
|
|
"\\([#%@]\\)"
|
2016-06-06 23:50:10 -04:00
|
|
|
"\\(\\(?::\\(?:[phtreS~.]\\|g?s[^: $]+\\)\\)*\\)"))
|
|
|
|
case-fold-search)
|
2016-06-05 23:08:46 -04:00
|
|
|
(dolist (match (s-match-strings-all regexp file-name))
|
|
|
|
(let ((flags (split-string (caddr match) ":" t))
|
|
|
|
(path (file-relative-name
|
|
|
|
(pcase (cadr match)
|
|
|
|
("@" (doom/project-root))
|
|
|
|
("%" (buffer-file-name))
|
|
|
|
("#" (and (other-buffer) (buffer-file-name (other-buffer)))))
|
|
|
|
default-directory))
|
|
|
|
flag global)
|
|
|
|
(when path
|
|
|
|
(while flags
|
|
|
|
(setq flag (pop flags))
|
|
|
|
(when (string-suffix-p "\\" flag)
|
|
|
|
(setq flag (concat flag (pop flags))))
|
|
|
|
(when (string-prefix-p "gs" flag)
|
2016-06-06 23:50:10 -04:00
|
|
|
(setq global t
|
|
|
|
flag (string-remove-prefix "g" flag)))
|
2016-06-05 23:08:46 -04:00
|
|
|
(setq path
|
|
|
|
(or (pcase (substring flag 0 1)
|
|
|
|
("p" (expand-file-name path))
|
|
|
|
("~" (file-relative-name path "~"))
|
|
|
|
("." (file-relative-name path default-directory))
|
|
|
|
("h" (directory-file-name path))
|
|
|
|
("t" (file-name-nondirectory (directory-file-name path)))
|
|
|
|
("r" (file-name-sans-extension path))
|
|
|
|
("e" (file-name-extension path))
|
|
|
|
("s" (let* ((args (evil-delimited-arguments (substring flag 1) 2))
|
|
|
|
(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))))
|
|
|
|
("S" (shell-quote-argument path))
|
2016-06-06 23:50:10 -04:00
|
|
|
(_ path))
|
2016-06-05 23:08:46 -04:00
|
|
|
"")))
|
|
|
|
(setq file-name
|
|
|
|
(replace-regexp-in-string (format "\\(?:^\\|[^\\\\]\\)\\(%s\\)"
|
|
|
|
(string-trim-left (car match)))
|
|
|
|
path file-name t t 1)))))
|
|
|
|
(setq file-name (replace-regexp-in-string regexp "\\1" file-name t))))
|
|
|
|
|
2015-06-15 09:05:52 +02:00
|
|
|
(provide 'defuns-evil)
|
|
|
|
;;; defuns-evil.el ends here
|