]> code.delx.au - gnu-emacs-elpa/blobdiff - counsel.el
counsel.el (counsel-expression-history): New command
[gnu-emacs-elpa] / counsel.el
index 16cc2971f09c146f7f64628ea8b93326dff0ac50..e0b421d5afe1462f570a4117b6d3fdc253f3c564 100644 (file)
@@ -1289,6 +1289,12 @@ INITIAL-INPUT can be given as the initial minibuffer input."
 
 ;;* Grep
 ;;** `counsel-ag'
+(defvar counsel-ag-map
+  (let ((map (make-sparse-keymap)))
+    (define-key map (kbd "C-l") 'counsel-git-grep-recenter)
+    (define-key map (kbd "M-q") 'counsel-git-grep-query-replace)
+    map))
+
 (defcustom counsel-ag-base-command "ag --nocolor --nogroup %s -- ."
   "Format string to use in `cousel-ag-function' to construct the
 command. %S will be replaced by the regex string. The default is
@@ -1328,6 +1334,7 @@ INITIAL-INPUT can be given as the initial minibuffer input."
             'counsel-ag-function
             :initial-input initial-input
             :dynamic-collection t
+            :keymap counsel-ag-map
             :history 'counsel-git-grep-history
             :action #'counsel-git-grep-action
             :unwind (lambda ()
@@ -1337,11 +1344,14 @@ INITIAL-INPUT can be given as the initial minibuffer input."
 
 (defun counsel-ag-occur ()
   "Generate a custom occur buffer for `counsel-ag'."
-  (ivy-occur-grep-mode)
+  (unless (eq major-mode 'ivy-occur-grep-mode)
+    (ivy-occur-grep-mode))
   (setq default-directory counsel--git-grep-dir)
   (let* ((regex (counsel-unquote-regex-parens
                  (setq ivy--old-re
-                       (ivy--regex ivy-text))))
+                       (ivy--regex
+                        (progn (string-match "\"\\(.*\\)\"" (buffer-name))
+                               (match-string 1 (buffer-name)))))))
          (cands (split-string
                  (shell-command-to-string
                   (format counsel-ag-base-command (shell-quote-argument regex)))
@@ -1373,6 +1383,12 @@ This uses `counsel-ag' with `counsel-pt-base-command' replacing
     (call-interactively 'counsel-ag)))
 
 ;;** `counsel-grep'
+(defcustom counsel-grep-base-command "grep -nE --ignore-case \"%s\" %s"
+  "Format string to use in `cousel-grep-function' to construct
+the command."
+  :type 'string
+  :group 'ivy)
+
 (defun counsel-grep-function (string)
   "Grep in the current directory for STRING."
   (if (< (length string) 2)
@@ -1381,7 +1397,7 @@ This uses `counsel-ag' with `counsel-pt-base-command' replacing
                   (setq ivy--old-re
                         (ivy--regex string)))))
       (counsel--async-command
-       (format "grep -nE --ignore-case \"%s\" %s" regex counsel--git-grep-dir))
+       (format counsel-grep-base-command regex counsel--git-grep-dir))
       nil)))
 
 (defun counsel-grep-action (x)
@@ -1401,6 +1417,34 @@ This uses `counsel-ag' with `counsel-pt-base-command' replacing
                                      (line-end-position))
             (swiper--add-overlays (ivy--regex ivy-text))))))))
 
+(defun counsel-grep-occur ()
+  "Generate a custom occur buffer for `counsel-grep'."
+  (unless (eq major-mode 'ivy-occur-grep-mode)
+    (ivy-occur-grep-mode))
+  (let ((cands
+         (split-string
+          (shell-command-to-string
+           (format counsel-grep-base-command
+                   (counsel-unquote-regex-parens
+                    (setq ivy--old-re
+                          (ivy--regex
+                           (progn (string-match "\"\\(.*\\)\"" (buffer-name))
+                                  (match-string 1 (buffer-name))) t)))
+                   counsel--git-grep-dir))
+          "\n" t))
+        (file (file-name-nondirectory counsel--git-grep-dir)))
+    ;; Need precise number of header lines for `wgrep' to work.
+    (insert (format "-*- mode:grep; default-directory: %S -*-\n\n\n"
+                    default-directory))
+    (insert (format "%d candidates:\n" (length cands)))
+    (ivy--occur-insert-lines
+     (mapcar
+      (lambda (cand) (concat "./" file ":" cand))
+      cands))))
+
+(ivy-set-occur 'counsel-grep 'counsel-grep-occur)
+(counsel-set-async-exit-code 'counsel-grep 1 "")
+
 ;;;###autoload
 (defun counsel-grep ()
   "Grep for a string in the current file."
@@ -1825,6 +1869,12 @@ An extra action allows to switch to the process buffer."
                 :action action
                 :require-match t
                 :caller 'counsel-ace-link))))
+;;** `counsel-expression-history'
+;;;###autoload
+(defun counsel-expression-history ()
+  (interactive)
+  (ivy-read "Expr: " (delete-dups read-expression-history)
+            :action #'insert))
 
 ;;* Misc OS
 ;;** `counsel-rhythmbox'