]> code.delx.au - gnu-emacs-elpa/commitdiff
"M-n", "M-p", "M-i" should switch directories when needed
authorOleh Krehel <ohwoeowho@gmail.com>
Thu, 18 Jun 2015 11:32:36 +0000 (13:32 +0200)
committerOleh Krehel <ohwoeowho@gmail.com>
Thu, 18 Jun 2015 11:32:36 +0000 (13:32 +0200)
* ivy.el (ivy-previous-history-element):
(ivy-next-history-element):
(ivy-switch-buffer): Call `ivy--cd-maybe'.
(ivy--cd-maybe): New defun. Check if the current input points to a
different directory than `ivy--directory'. If so, `ivy--cd' there.

Fixes #152

ivy.el

diff --git a/ivy.el b/ivy.el
index b130ec9c37773f4a8e8ce054c414772389d1528c..ad4fe147a6959baec720e3f172c1115f0d56c08f 100644 (file)
--- a/ivy.el
+++ b/ivy.el
@@ -493,6 +493,7 @@ Call the permanent action if possible."
   "Forward to `previous-history-element' with ARG."
   (interactive "p")
   (previous-history-element arg)
+  (ivy--cd-maybe)
   (move-end-of-line 1)
   (ivy--maybe-scroll-history))
 
@@ -500,9 +501,24 @@ Call the permanent action if possible."
   "Forward to `next-history-element' with ARG."
   (interactive "p")
   (next-history-element arg)
+  (ivy--cd-maybe)
   (move-end-of-line 1)
   (ivy--maybe-scroll-history))
 
+(defun ivy--cd-maybe ()
+  "Check if the current input points to a different directory.
+If so, move to that directory, while keeping only the file name."
+  (when ivy--directory
+    (let* ((input (expand-file-name (ivy--input)))
+           (file (file-name-nondirectory input))
+           (dir (expand-file-name (file-name-directory input))))
+      (if (string= dir ivy--directory)
+          (progn
+            (delete-minibuffer-contents)
+            (insert file))
+        (ivy--cd dir)
+        (insert file)))))
+
 (defun ivy--maybe-scroll-history ()
   "If the selected history element has an index, scroll there."
   (let ((idx (ignore-errors
@@ -1367,7 +1383,8 @@ BUFFER may be a string or nil."
 Don't finish completion."
   (interactive)
   (delete-minibuffer-contents)
-  (insert ivy--current))
+  (insert ivy--current)
+  (ivy--cd-maybe))
 
 (defun ivy-toggle-fuzzy ()
   "Toggle the re builder between `ivy--regex-fuzzy' and `ivy--regex-plus'."