emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master c84b434 1/8: Nicer behaviour from ack-skel-vc-grep when us


From: João Távora
Subject: [elpa] master c84b434 1/8: Nicer behaviour from ack-skel-vc-grep when used in setup hook
Date: Fri, 15 Mar 2019 18:33:10 -0400 (EDT)

branch: master
commit c84b434c7c731057ec2b0f4fffd71c1a82c58dde
Author: João Távora <address@hidden>
Commit: João Távora <address@hidden>

    Nicer behaviour from ack-skel-vc-grep when used in setup hook
    
    Close #9, close #10.
    
    Previously, if the user had ack-skel-vc-grep in the
    ack-minibuffer-setup-hook and chose a directory without a vc project,
    C-u C-u M-x ack would fail because ack-skel-vc-grep raised an error,
    since it can't work under those circunstances. This commit makes it
    give up in these situations and leave the (presumably ack) prompt
    unchanged.
    
    Additionally, if the user chose a subdirectory *within* a vc project,
    C-u C-u M-x ack would surprisingly change the directory to the vc
    project root.  While this is probably intended in interactive calls to
    ack-skel-vc-grep, it is not when that function is in the
    ack-minibuffer-setup-hook.
    
    * ack.el (ack-skel-vc-grep): Overhaul function. Distinguish
    between interactive and non-interactive uses.
---
 ack.el | 54 +++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 39 insertions(+), 15 deletions(-)

diff --git a/ack.el b/ack.el
index 11c1f93..fa2685a 100644
--- a/ack.el
+++ b/ack.el
@@ -279,21 +279,45 @@ This gets tacked on the end of the generated 
expressions.")
 ;; Work around bug http://debbugs.gnu.org/13811
 (defvar ack--project-root nil)          ; dynamically bound in `ack'
 
-(defun ack-skel-vc-grep ()
-  "Insert a template for vc grep search."
-  (interactive)
-  (let* ((regexp (concat "\\`" (regexp-opt
-                                (mapcar 'car ack-vc-grep-commands))
-                         "\\'"))
-         (root (or (ack-guess-project-root default-directory regexp)
-                   (error "Cannot locate vc project root")))
-         (which (car (directory-files root nil regexp)))
-         (backend (downcase (substring which 1)))
-         (cmd (or (cdr (assoc which ack-vc-grep-commands))
-                  (error "No command provided for `%s grep'" backend))))
-    (setq ack--project-root root)
-    (delete-minibuffer-contents)
-    (skeleton-insert `(nil ,cmd " '" _ "'"))))
+(defun ack-skel-vc-grep (&optional interactive)
+  "Find a vc-controlled dir, insert a template for a vc grep search.
+If called interactively, INTERACTIVE is non-nil and calls to this
+function that cannot locate such a directory will produce an
+error, whereas in non-interactive calls they will silently exit,
+leaving the minibuffer unchanged.
+
+This function is a suitable addition to
+`ack-minibuffer-setup-hook'."
+  (interactive "p")
+  (catch 'giveup
+    (let* ((regexp (concat "\\`" (regexp-opt
+                                  (mapcar 'car ack-vc-grep-commands))
+                           "\\'"))
+           (guessed-root (or (ack-guess-project-root ack--project-root regexp)
+                             (if interactive
+                                 (user-error
+                                  "Cannot locate a vc project root from %s"
+                                  ack--project-root)
+                               (throw 'giveup nil))))
+           (which (progn
+                    (unless (or interactive
+                                (equal
+                                 (file-truename ack--project-root)
+                                 (file-truename guessed-root)))
+                      ;; See github
+                      ;; https://github.com/leoliu/ack-el/issues/10
+                      ;; for the reason for giving up here
+                      ;; non-interactively.
+                      (throw 'giveup nil))
+                    (car (directory-files guessed-root nil regexp))))
+           (backend (downcase (substring which 1)))
+           (cmd (or (cdr (assoc which ack-vc-grep-commands))
+                    (error "No command provided for `%s grep'" backend))))
+      (when interactive
+        (setq ack--project-root guessed-root)
+        (ack-update-minibuffer-prompt))
+      (delete-minibuffer-contents)
+      (skeleton-insert `(nil ,cmd " '" _ "'")))))
 
 (defun ack-yank-symbol-at-point ()
   "Yank the symbol from the window before entering the minibuffer."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]