From 109fb7decd31f026c21ee5f7f607d3c8ad15c1c1 Mon Sep 17 00:00:00 2001 From: Oleh Krehel Date: Sat, 2 May 2015 17:50:17 +0200 Subject: [PATCH] Add unwind argument to ivy-read * ivy.el (ivy-state): Add `unwind' field. (ivy-resume): Update. (ivy-read): Call the `unwind' argument in the unwind form. * swiper.el (swiper--ivy): Use `unwind' for `ivy-read'. --- ivy.el | 25 +++++++++++++++++-------- swiper.el | 4 ++-- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/ivy.el b/ivy.el index 05509f8d..667186e5 100644 --- a/ivy.el +++ b/ivy.el @@ -124,7 +124,8 @@ Only \"./\" and \"../\" apply here. They appear in reverse order." history preselect keymap update-fn sort ;; The window in which `ivy-read' was called window - action) + action + unwind) (defvar ivy-last nil "The last parameters passed to `ivy-read'.") @@ -310,7 +311,8 @@ candidate." :keymap (ivy-state-keymap ivy-last) :update-fn (ivy-state-update-fn ivy-last) :sort (ivy-state-sort ivy-last) - :action (ivy-state-action ivy-last))) + :action (ivy-state-action ivy-last) + :unwind (ivy-state-unwind ivy-last))) (defun ivy-beginning-of-buffer () "Select the first completion candidate." @@ -511,9 +513,9 @@ Directories come first." ;;** Entry Point (cl-defun ivy-read (prompt collection - &key predicate require-match initial-input - history preselect keymap update-fn sort - action) + &key predicate require-match initial-input + history preselect keymap update-fn sort + action unwind) "Read a string in the minibuffer, with completion. PROMPT is a string to prompt with; normally it ends in a colon @@ -532,7 +534,11 @@ the ones that match INITIAL-INPUT. UPDATE-FN is called each time the current candidate(s) is changed. -When SORT is t, refer to `ivy-sort-functions-alist' for sorting." +When SORT is t, refer to `ivy-sort-functions-alist' for sorting. + +ACTION is a lambda to call after a result was selected. + +UNWIND is a lambda to call before exiting." (setq ivy-last (make-ivy-state :prompt prompt @@ -546,7 +552,8 @@ When SORT is t, refer to `ivy-sort-functions-alist' for sorting." :update-fn update-fn :sort sort :action action - :window (selected-window))) + :window (selected-window) + :unwind unwind)) (setq ivy--directory nil) (setq ivy--regex-function (or (and (functionp collection) @@ -646,7 +653,9 @@ When SORT is t, refer to `ivy-sort-functions-alist' for sorting." (delete ivy-text (cdr (symbol-value hist))))) res))) - (remove-hook 'post-command-hook #'ivy--exhibit)) + (remove-hook 'post-command-hook #'ivy--exhibit) + (when (setq unwind (ivy-state-unwind ivy-last)) + (funcall unwind))) (when (setq action (ivy-state-action ivy-last)) (funcall action))))) diff --git a/swiper.el b/swiper.el index c7272890..d77e96b0 100644 --- a/swiper.el +++ b/swiper.el @@ -195,8 +195,8 @@ Please remove it and update the \"swiper\" package.")) :keymap swiper-map :preselect preselect :require-match t - :update-fn #'swiper--update-input-ivy)) - (swiper--cleanup) + :update-fn #'swiper--update-input-ivy + :unwind #'swiper--cleanup)) (if (null ivy-exit) (goto-char swiper--opoint) (swiper--action res ivy-text)))))