Permalink
Browse files

Introduce multi occur.

* helm-buffers.el
(helm-multi-occur-as-action): Multi occur action for buffers
(helm-buffers-run-multi-occur): To run by key.
Use new action in buffers attributes.
* helm-regexp.el
New functions and source for multi occur.
  • Loading branch information...
1 parent 255defa commit 7288a9a518be61d5b676a49a109df968577ac192 @thierryvolpiatto thierryvolpiatto committed Aug 17, 2012
Showing with 88 additions and 0 deletions.
  1. +11 −0 helm-buffers.el
  2. +77 −0 helm-regexp.el
View
@@ -22,6 +22,7 @@
(require 'helm-utils)
(require 'helm-elscreen)
(require 'helm-grep)
+(require 'helm-regexp)
(declare-function ido-make-buffer-list "ido" (default))
@@ -82,6 +83,7 @@ filtered from the list of candidates if the
;; So use zgrep for both as it is capable to handle non--compressed files.
(define-key map (kbd "M-g s") 'helm-buffer-run-zgrep)
(define-key map (kbd "C-s") 'helm-buffer-run-zgrep)
+ (define-key map (kbd "C-c s") 'helm-buffers-run-multi-occur)
(define-key map (kbd "C-c o") 'helm-buffer-switch-other-window)
(define-key map (kbd "C-c C-o") 'helm-buffer-switch-other-frame)
(define-key map (kbd "C-c =") 'helm-buffer-run-ediff)
@@ -460,6 +462,14 @@ With optional arg MERGE call `ediff-merge-buffers'."
See `helm-ediff-marked-buffers'."
(helm-ediff-marked-buffers candidate t))
+(defun helm-multi-occur-as-action (_candidate)
+ (let ((buffers (helm-marked-candidates)))
+ (helm-multi-occur-1 buffers)))
+
+(defun helm-buffers-run-multi-occur ()
+ (interactive)
+ (helm-c-quit-and-execute-action 'helm-multi-occur-as-action))
+
;;; Candidate Transformers
;;
;;
@@ -496,6 +506,7 @@ displayed with the `file-name-shadow' face if available."
("View buffer" . view-buffer)
("Display buffer" . display-buffer)
("Grep buffers (C-u grep all buffers)" . helm-c-zgrep-buffers)
+ ("Multi occur buffer(s)" . helm-multi-occur-as-action)
("Revert buffer(s)" . helm-revert-marked-buffers)
("Insert buffer" . insert-buffer)
("Kill buffer(s)" . helm-kill-marked-buffers)
View
@@ -57,6 +57,15 @@ to a specific `major-mode'."
map)
"Keymap for `helm-occur'.")
+(defvar helm-c-moccur-map
+ (let ((map (make-sparse-keymap)))
+ (set-keymap-parent map helm-map)
+ (define-key map (kbd "M-<down>") 'helm-c-goto-next-file)
+ (define-key map (kbd "M-<up>") 'helm-c-goto-precedent-file)
+ (define-key map (kbd "C-w") 'helm-yank-text-at-point)
+ (delq nil map))
+ "Keymap used in Moccur source.")
+
(defvar helm-build-regexp-history nil)
(defun helm-c-query-replace-regexp (candidate)
"Query replace regexp from `helm-regexp'.
@@ -187,6 +196,65 @@ i.e Don't replace inside a word, regexp is surrounded with \\bregexp\\b."
(delayed)))
+;;; Multi occur
+;;
+;;
+(defun helm-m-occur-init (buffers)
+ "Create the initial helm multi occur buffer.
+If region is active use region as buffer contents
+instead of whole buffer."
+ (helm-init-candidates-in-buffer
+ "*hmoccur*"
+ (loop for buf in buffers
+ for bufstr = (with-current-buffer buf (buffer-string))
+ do (add-text-properties
+ 0 (length bufstr)
+ `(buffer-name ,(buffer-name (get-buffer buf)))
+ bufstr)
+ concat bufstr)))
+
+(defun helm-m-occur-get-line (s e)
+ (format "%s:%d:%s"
+ (get-text-property (point-at-bol) 'buffer-name)
+ (save-restriction
+ (narrow-to-region (previous-property-change (point))
+ (next-property-change (point)))
+ (line-number-at-pos s))
+ (buffer-substring s e)))
+
+(defun* helm-m-occur-action (candidate
+ &optional (method (quote buffer)))
+ (let* ((split (split-string candidate ":" t))
+ (buf (car split))
+ (lineno (string-to-number (nth 1 split))))
+ (case method
+ (buffer (switch-to-buffer buf))
+ (buffer-other-window (switch-to-buffer-other-window buf))
+ (buffer-other-frame (switch-to-buffer-other-frame buf)))
+ (helm-goto-line lineno)))
+
+(defun helm-m-occur-goto-line (candidate)
+ (helm-m-occur-action candidate))
+
+(defvar helm-c-source-moccur
+ `((name . "Moccur")
+ (init . (lambda ()
+ (helm-m-occur-init buffers)))
+ (candidates-in-buffer)
+ (get-line . helm-m-occur-get-line)
+ (action . (("Go to Line" . helm-m-occur-goto-line)))
+ (recenter)
+ (candidate-number-limit . 9999)
+ (mode-line . helm-occur-mode-line)
+ (keymap . ,helm-c-moccur-map)
+ (requires-pattern . 3)))
+
+(defun helm-multi-occur-1 (buffers)
+ (declare (special buffers))
+ (helm :sources 'helm-c-source-moccur
+ :buffer "*helm moccur*"))
+
+
;;; Helm browse code.
(defun helm-c-browse-code-get-line (beg end)
"Select line if it match the regexp corresponding to current `major-mode'.
@@ -286,6 +354,15 @@ otherwise search in whole buffer."
:buffer "*Helm Occur*"
:history 'helm-c-grep-history)))
+;;;###autoload
+(defun helm-multi-occur ()
+ "Preconfigured for helm multi occur."
+ (interactive)
+ (let ((buffers (helm-comp-read
+ "Buffers: " (helm-c-buffer-list)
+ :marked-candidates t)))
+ (helm-multi-occur-1 buffers)))
+
;;;###autoload
(defun helm-browse-code ()
"Preconfigured helm to browse code."

0 comments on commit 7288a9a

Please sign in to comment.