From f05819c85f04398be04c02210b86136daf3a2dfd Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Wed, 10 May 2017 05:20:54 +0200 Subject: [PATCH] completion/ivy: refactor ag other-window action; replace def-counsel-action macro --- TODO.org | 10 +++-- modules/completion/ivy/autoload/ivy.el | 11 ++++++ modules/completion/ivy/config.el | 53 ++++++++------------------ 3 files changed, 32 insertions(+), 42 deletions(-) diff --git a/TODO.org b/TODO.org index 734d38b8c..2621b390e 100644 --- a/TODO.org +++ b/TODO.org @@ -5,7 +5,7 @@ + [-] Work-in-progress + [X] Complete -** Planned [5/80] +** Planned [5/78] + [0/10] Potential plugins + [ ] [[https://github.com/emacs-lsp/lsp-mode][lsp-mode]]: client for MS Language Server Protocol, keep an eye on this + [ ] lang/javascript: [[https://github.com/codesuki/add-node-modules-path][add-node-modules-path]] (adds node_modules to ~exec-path~) @@ -92,15 +92,17 @@ + [ ] lang/org: restore cursor to sane location after prepending/appending new table cells. + [ ] lang/org: don't move cursor when realigning tables when exiting insert state. + [ ] lang/org: typing certain characters and keys while in a table causes the cell to shrink (like smartparens pairs and keys not bound to self-insert-command) -+ [ ] completion/ivy: refactor ~def-counsel-action!~ into a setting (~def-setting!~) -+ [ ] completion/ivy: refactor ag-open-in-other-window (messy/hackish) + [ ] Fix invisible buffer-info in modeline for terminal Emacs + [ ] tools/upload: add ~+upload/open-remote-file~ command to open current file on the remote (with TRAMP) -** 2.0.2 [55/58] +** 2.0.2 [57/60] + [ ] Update screenshots + [ ] send-to-REPL workflow: does it still work? (see ~:repl~ & ~+eval/repl-send-region~) + [ ] Fix ~0/0~ displaying in modeline (caused by leftover anzu state) ++ [X] completion/ivy: refactor ~def-counsel-action!~ +into a setting (~def-setting!~)+ + Refactored into ~ivy-do-action!~ macro instead ++ [X] completion/ivy: refactor ag-open-in-other-window (messy/hackish) + Now ~+ivy-git-grep-other-window-action~ + [X] Fix ~+eval/region~ failing only on first invocation. Caused by ~+eval-runners-alist~ not being populated until quickrun is loaded. + [X] Fix: Neotree forgets it's a neotree window when switching perspectives diff --git a/modules/completion/ivy/autoload/ivy.el b/modules/completion/ivy/autoload/ivy.el index d2fe27ddf..5ad3dacea 100644 --- a/modules/completion/ivy/autoload/ivy.el +++ b/modules/completion/ivy/autoload/ivy.el @@ -143,3 +143,14 @@ interferes with my custom :[ar]g ex command `+ivy:file-search'." ;;;###autoload (defun +ivy-yas-prompt (prompt choices &optional display-fn) (yas-completing-prompt prompt choices display-fn #'ivy-completing-read)) + +;;;###autoload +(defun +ivy-git-grep-other-window-action (x) + "Opens the current candidate in another window." + (let (dest-window) + (cl-letf (((symbol-function 'find-file) + (lambda (filename) + (find-file-other-window filename) + (setq dest-window (selected-window))))) + (counsel-git-grep-action x) + (select-window dest-window)))) diff --git a/modules/completion/ivy/config.el b/modules/completion/ivy/config.el index 1eac4b8d3..74f6976fb 100644 --- a/modules/completion/ivy/config.el +++ b/modules/completion/ivy/config.el @@ -1,19 +1,12 @@ ;;; completion/ivy/packages.el -;; Ivy is my completion backend of choice. With counsel's help, I get: -;; -;; + Project-wide search with `counsel-rg' (and `+ivy:file-search') -;; + Project-wide replace if you press in the ag occur buffer. -;; + An Atom/Sublime-Text Command-T implementation with `counsel-find-file' and -;; `counsel-projectile-find-file'. -;; + Ido-like completion for a slew of functions, like `counsel-M-x' and -;; `counsel-imenu'. - -;; TODO Make this a setting -(defmacro def-counsel-action! (name &rest forms) - `(defun ,(intern (format "+ivy/counsel-%s" (symbol-name name))) () +(defmacro ivy-do-action! (action) + "A factory function that returns an interactive lamba that sets the current +ivy action and immediately runs it on the current candidate (ending the ivy +session)." + `(lambda () (interactive) - (ivy-set-action ',@forms) + (ivy-set-action ,action) (setq ivy-exit 'done) (exit-minibuffer))) @@ -84,37 +77,21 @@ (def-package! counsel :after ivy :config - (setq counsel-find-file-ignore-regexp "\\(?:^[#.]\\)\\|\\(?:[#~]$\\)\\|\\(?:^Icon?\\)") - - (set! :popup "^\\*ivy-occur counsel-[ar]g" :size 25 :regexp t :autokill t) - (require 'counsel-projectile) - ;; FIXME Messy workaround, refactor this - (def-counsel-action! ag-open-in-other-window - (lambda (x) - (when (string-match "\\`\\(.*?\\):\\([0-9]+\\):\\(.*\\)\\'" x) - (let ((file-name (match-string-no-properties 1 x)) - (line-number (match-string-no-properties 2 x)) - dest-win) - (with-ivy-window - (find-file-other-window (expand-file-name file-name counsel--git-grep-dir)) - (setq dest-win (selected-window)) - (forward-line (1- (string-to-number line-number))) - (re-search-forward (ivy--regex ivy-text t) (line-end-position) t) - (recenter) - (swiper--ensure-visible) - (run-hooks 'counsel-grep-post-action-hook) - (unless (eq ivy-exit 'done) - (swiper--cleanup) - (swiper--add-overlays (ivy--regex ivy-text)))) - (when dest-win - (select-window dest-win)))))) + (setq counsel-find-file-ignore-regexp "\\(?:^[#.]\\)\\|\\(?:[#~]$\\)\\|\\(?:^Icon?\\)") + + ;; Configure `counsel-rg'/`counsel-ag' + (set! :popup "^\\*ivy-occur counsel-[ar]g" :size 25 :regexp t :autokill t) + + (ivy-add-actions + 'counsel-rg + '(("O" +ivy-git-grep-other-window-action "open in other window"))) (map! :map counsel-ag-map ; applies to counsel-rg too [backtab] #'+ivy/wgrep-occur ; search/replace on results "C-SPC" #'counsel-git-grep-recenter ; preview - "M-RET" #'+ivy/counsel-ag-open-in-other-window) + "M-RET" (ivy-do-action! #'+ivy-git-grep-other-window-action)) ;; 1) Gets rid of the character limit from `counsel-ag-function' and ;; 2) Disables ivy's over-zealous parentheses quoting behavior