feature/evil: split autoload.el into autoload/{evil,files}.el
This commit is contained in:
parent
3cfc7d1c0a
commit
57b937cf99
3 changed files with 80 additions and 1 deletions
156
modules/feature/evil/autoload/evil.el
Normal file
156
modules/feature/evil/autoload/evil.el
Normal file
|
@ -0,0 +1,156 @@
|
|||
;;; feature/evil/packages.el
|
||||
|
||||
;;;###autoload
|
||||
(defun +evil*ex-replace-special-filenames (file-name)
|
||||
"Replace special symbols in FILE-NAME. Modified to include other substitution
|
||||
flags."
|
||||
;; TODO Generalize this so I can offer it upstream
|
||||
(let ((regexp (concat "\\(?:^\\|[^\\\\]\\)"
|
||||
"\\([#%@]\\)"
|
||||
"\\(\\(?::\\(?:[phtreS~.]\\|g?s[^: $]+\\)\\)*\\)"))
|
||||
case-fold-search)
|
||||
(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)
|
||||
(setq global t
|
||||
flag (string-remove-prefix "g" flag)))
|
||||
(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))
|
||||
(_ path))
|
||||
"")))
|
||||
(setq file-name
|
||||
(replace-regexp-in-string (format "\\(?:^\\|[^\\\\]\\)\\(%s\\)"
|
||||
(s-trim-left (car match)))
|
||||
path file-name t t 1)))))
|
||||
(setq file-name (replace-regexp-in-string regexp "\\1" file-name t))))
|
||||
|
||||
|
||||
;;
|
||||
;; Custom argument handlers
|
||||
;;
|
||||
|
||||
(defvar +evil--buffer-match-global evil-ex-substitute-global "")
|
||||
|
||||
(defun +evil--ex-match-init (name &optional face update-hook)
|
||||
(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)))))
|
||||
|
||||
(defun +evil--ex-buffer-match (arg &optional hl-name flags beg end)
|
||||
(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
|
||||
(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)
|
||||
(let ((hl-name 'evil-ex-buffer-match))
|
||||
(with-selected-window (minibuffer-selected-window)
|
||||
(+evil--ex-match-init hl-name)
|
||||
(+evil--ex-buffer-match arg hl-name (list (if +evil--buffer-match-global ?g))))))
|
||||
|
||||
;;;###autoload
|
||||
(defun +evil-ex-global-match (flag &optional arg)
|
||||
(let ((hl-name 'evil-ex-global-match))
|
||||
(with-selected-window (minibuffer-selected-window)
|
||||
(+evil--ex-match-init hl-name)
|
||||
(let ((result (car-safe (evil-ex-parse-global arg))))
|
||||
(+evil--ex-buffer-match result hl-name nil (point-min) (point-max))))))
|
||||
|
||||
;;;###autoload
|
||||
(defun +evil-window-move (direction)
|
||||
"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')"
|
||||
(let* ((this-window (get-buffer-window))
|
||||
(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)
|
||||
(doom-popup-p that-window))
|
||||
(setq that-buffer nil that-window nil))
|
||||
(if (not (or that-window (one-window-p t)))
|
||||
(funcall (case 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 (cond ((eq direction 'up) 'above)
|
||||
((eq direction 'down) 'below)
|
||||
(t direction))))
|
||||
(with-selected-window that-window
|
||||
(switch-to-buffer doom-buffer))
|
||||
(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))))
|
||||
|
||||
;;;###autoload (autoload '+evil:macro-on-all-lines "feature/evil/autoload" nil t)
|
||||
(evil-define-operator +evil:macro-on-all-lines (beg end &optional macro)
|
||||
"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 "@-"))))))
|
77
modules/feature/evil/autoload/files.el
Normal file
77
modules/feature/evil/autoload/files.el
Normal file
|
@ -0,0 +1,77 @@
|
|||
;;; feature/evil/autoload/files.el
|
||||
|
||||
;;;###autoload (autoload '+evil:file-delete "feature/evil/autoload/files" nil t)
|
||||
(evil-define-command +evil:file-delete (&optional bang filename)
|
||||
"Delete current buffer's file. If BANG, don't ask for confirmation."
|
||||
:repeat nil
|
||||
(interactive "<!><f>")
|
||||
(let ((filename (f-canonical (or filename (buffer-file-name))))
|
||||
(buf (current-buffer)))
|
||||
(cond ((not (f-exists-p filename))
|
||||
(error "File doesn't exist: %s" filename))
|
||||
|
||||
((not (or bang (y-or-n-p (format "Delete %s?" (f-base filename)))))
|
||||
(message "Aborted"))
|
||||
|
||||
(t
|
||||
(unwind-protect
|
||||
(delete-file filename)
|
||||
(if (f-exists-p filename)
|
||||
(error "Failed to delete %s" (f-relative filename))
|
||||
(doom/previous-real-buffer)
|
||||
(kill-buffer buf)
|
||||
(when (bound-and-true-p save-place-mode)
|
||||
(save-place-forget-unreadable-files))
|
||||
(message "Successfully deleted %s" (f-relative filename))))))))
|
||||
|
||||
(defun doom--save-exit() (save-buffer) (kill-buffer) (remove-hook 'yas-after-exit-snippet-hook '--save-exit))
|
||||
;;;###autoload (autoload '+evil:file-create "feature/evil/autoload/files" nil t)
|
||||
(evil-define-command +evil:file-create (path &optional bang)
|
||||
"Deploy files (and their associated templates) quickly. Will prompt
|
||||
you to fill in each snippet field before buffer closes unless BANG is
|
||||
provided."
|
||||
:repeat nil
|
||||
(interactive "<f><!>")
|
||||
(let ((dir (f-dirname path))
|
||||
(fullpath (f-full path))
|
||||
(is-auto t))
|
||||
(when (and bang (not (f-exists? dir)))
|
||||
(mkdir dir))
|
||||
(if (f-exists? dir)
|
||||
(if (f-exists? fullpath)
|
||||
(error "File already exists: %s" path)
|
||||
(find-file fullpath)
|
||||
(add-hook 'yas-after-exit-snippet-hook 'doom--save-exit)
|
||||
(if bang (doom--save-exit)))
|
||||
(error "Directory doesn't exist: %s" dir))))
|
||||
|
||||
;;;###autoload (autoload '+evil:file-move "feature/evil/autoload/files" nil t)
|
||||
(evil-define-command +evil:file-move (path)
|
||||
"Move current buffer's file to PATH. Replaces %, # and other variables (see
|
||||
`evil-ex-replace-special-filenames')"
|
||||
:repeat nil
|
||||
(interactive "<f>")
|
||||
(let* ((old-path (buffer-file-name))
|
||||
(new-path (cond ((f-dir? path)
|
||||
(f-expand (f-filename old-path) path))
|
||||
((f-dir? (f-dirname path))
|
||||
(f-full path))
|
||||
(t (user-error "Not a valid destination: %s" path))))
|
||||
(project-root (doom-project-root)))
|
||||
;; Move all attachments if in org-mode
|
||||
(when (eq major-mode 'org-mode)
|
||||
(mapc (lambda (file)
|
||||
(when (and (file-exists-p file) (not (f-same? old-path new-path)))
|
||||
(rename-file file (f-expand (f-filename old-path) (f-dirname new-path)) t)))
|
||||
(doom/org-attachments)))
|
||||
(when (buffer-modified-p)
|
||||
(save-buffer))
|
||||
(rename-file old-path new-path 1)
|
||||
(rename-buffer (f-filename new-path))
|
||||
(set-visited-file-name new-path)
|
||||
(set-buffer-modified-p nil)
|
||||
(save-place-forget-unreadable-files)
|
||||
(setq doom--spaceline-file-path nil)
|
||||
(message "File '%s' successfully renamed to '%s'"
|
||||
(f-relative old-path project-root) (f-relative new-path project-root))))
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue