From efcde0e1208d5adce084fdc2347579a2d21d9279 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Wed, 8 Mar 2017 20:20:28 +0100 Subject: [PATCH 1/6] helm-imenu-in-all-buffers in separate sources (#1705). * helm-imenu.el (helm-imenu-in-all-buffers-separate-sources): New user var. (helm-imenu-collect-sources-from-all-buffers): New fn. (helm-imenu-in-all-buffers): Use it. --- helm-imenu.el | 55 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 13 deletions(-) diff --git a/helm-imenu.el b/helm-imenu.el index 5347c92f9..b062b94cf 100644 --- a/helm-imenu.el +++ b/helm-imenu.el @@ -53,6 +53,12 @@ The alist is bidirectional, i.e no need to add '((foo . bar) (bar . foo)) only '((foo . bar)) is needed." :type '(alist :key-type symbol :value-type symbol) :group 'helm-imenu) + +(defcustom helm-imenu-in-all-buffers-separate-sources t + "Display imenu index of each buffer in its own sources when non-nil." + :type 'boolean + :group 'helm-imenu) + ;;; keymap (defvar helm-imenu-map @@ -191,6 +197,25 @@ only '((foo . bar)) is needed." (helm-imenu-candidates b))) (progress-reporter-done progress-reporter)))) +(defun helm-imenu-collect-sources-from-all-buffers () + (let* ((lst (buffer-list)) + (progress-reporter (make-progress-reporter + "Imenu indexing buffers..." 1 (length lst)))) + (prog1 + (cl-loop with cur-buf = (current-buffer) + for b in lst + when (and (with-current-buffer b + (derived-mode-p 'prog-mode)) + (with-current-buffer b + (helm-same-major-mode-p cur-buf + helm-imenu-all-buffer-assoc))) + collect (helm-make-source (format "Imenu in %s" (buffer-name b)) + 'helm-imenu-source + :candidates (with-current-buffer b + (helm-imenu-candidates b)) + :fuzzy-match helm-imenu-fuzzy-match)) + (progress-reporter-done progress-reporter)))) + (defun helm-imenu--candidates-1 (alist) (cl-loop for elm in alist nconc (cond @@ -270,22 +295,26 @@ only '((foo . bar)) is needed." A mode is similar as current if it is the same, it is derived i.e `derived-mode-p' or it have an association in `helm-imenu-all-buffer-assoc'." (interactive) - (unless helm-source-imenu-all - (setq helm-source-imenu-all - (helm-make-source "Imenu in all buffers" 'helm-imenu-source - :init (lambda () - ;; Use a cache to avoid repeatedly sending - ;; progress-reporter message when updating - ;; (Issue #1704). - (setq helm-imenu--in-all-buffers-cache - (helm-imenu-candidates-in-all-buffers))) - :candidates 'helm-imenu--in-all-buffers-cache - :fuzzy-match helm-imenu-fuzzy-match))) + (unless helm-imenu-in-all-buffers-separate-sources + (unless helm-source-imenu-all + (setq helm-source-imenu-all + (helm-make-source "Imenu in all buffers" 'helm-imenu-source + :init (lambda () + ;; Use a cache to avoid repeatedly sending + ;; progress-reporter message when updating + ;; (Issue #1704). + (setq helm-imenu--in-all-buffers-cache + (helm-imenu-candidates-in-all-buffers))) + :candidates 'helm-imenu--in-all-buffers-cache + :fuzzy-match helm-imenu-fuzzy-match)))) (let ((imenu-auto-rescan t) (str (thing-at-point 'symbol)) (helm-execute-action-at-once-if-one - helm-imenu-execute-action-at-once-if-one)) - (helm :sources 'helm-source-imenu-all + helm-imenu-execute-action-at-once-if-one) + (sources (if helm-imenu-in-all-buffers-separate-sources + (helm-imenu-collect-sources-from-all-buffers) + '(helm-source-imenu-all)))) + (helm :sources sources :default (list (concat "\\_<" str "\\_>") str) :preselect (unless (memq 'helm-source-imenu-all helm-sources-using-default-as-input) From b9b25ee3e528e4b8ce9b9eac816acd49a150facd Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Wed, 8 Mar 2017 20:27:14 +0100 Subject: [PATCH 2/6] Fix reporter (#1705). * helm-imenu.el (helm-imenu-collect-sources-from-all-buffers): Do it. --- helm-imenu.el | 3 +++ 1 file changed, 3 insertions(+) diff --git a/helm-imenu.el b/helm-imenu.el index b062b94cf..c3afe9416 100644 --- a/helm-imenu.el +++ b/helm-imenu.el @@ -204,11 +204,14 @@ only '((foo . bar)) is needed." (prog1 (cl-loop with cur-buf = (current-buffer) for b in lst + for count from 1 when (and (with-current-buffer b (derived-mode-p 'prog-mode)) (with-current-buffer b (helm-same-major-mode-p cur-buf helm-imenu-all-buffer-assoc))) + do (progress-reporter-update progress-reporter count) + and collect (helm-make-source (format "Imenu in %s" (buffer-name b)) 'helm-imenu-source :candidates (with-current-buffer b From b44a8ebe1559ed707bce6d82f52a8aa6392b3dfd Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Wed, 8 Mar 2017 22:27:55 +0100 Subject: [PATCH 3/6] Fix helm-sources-using-default-as-input for imenu-all with sep sources (#1705). * helm-imenu.el (helm-imenu-in-all-buffers): Do it. --- helm-imenu.el | 3 +++ 1 file changed, 3 insertions(+) diff --git a/helm-imenu.el b/helm-imenu.el index c3afe9416..fbb124cba 100644 --- a/helm-imenu.el +++ b/helm-imenu.el @@ -314,6 +314,9 @@ or it have an association in `helm-imenu-all-buffer-assoc'." (str (thing-at-point 'symbol)) (helm-execute-action-at-once-if-one helm-imenu-execute-action-at-once-if-one) + (helm--maybe-use-default-as-input + (not (null (memq 'helm-source-imenu-all + helm-sources-using-default-as-input)))) (sources (if helm-imenu-in-all-buffers-separate-sources (helm-imenu-collect-sources-from-all-buffers) '(helm-source-imenu-all)))) From 9b8fd93b7f9b880d3e5e239ab93492f8e755b07f Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Thu, 9 Mar 2017 06:09:40 +0100 Subject: [PATCH 4/6] Keep only one function to build sources and collect imenu cands (#1705). * helm-imenu.el (helm-imenu-candidates-in-all-buffers): Take now one optional arg. (helm-imenu-in-all-buffers): Use it. --- helm-imenu.el | 42 ++++++++++++++---------------------------- 1 file changed, 14 insertions(+), 28 deletions(-) diff --git a/helm-imenu.el b/helm-imenu.el index fbb124cba..7cad360f7 100644 --- a/helm-imenu.el +++ b/helm-imenu.el @@ -178,45 +178,31 @@ only '((foo . bar)) is needed." (delete (assoc "*Rescan*" index) index)))) (setq helm-cached-imenu-tick tick)))))) -(defun helm-imenu-candidates-in-all-buffers () +(defun helm-imenu-candidates-in-all-buffers (&optional build-sources) (let* ((lst (buffer-list)) (progress-reporter (make-progress-reporter "Imenu indexing buffers..." 1 (length lst)))) (prog1 - (cl-loop for b in lst - for count from 1 - when - (and (with-current-buffer b - (derived-mode-p 'prog-mode)) - (with-current-buffer b - (helm-same-major-mode-p helm-current-buffer - helm-imenu-all-buffer-assoc))) - do (progress-reporter-update progress-reporter count) - and - append (with-current-buffer b - (helm-imenu-candidates b))) - (progress-reporter-done progress-reporter)))) - -(defun helm-imenu-collect-sources-from-all-buffers () - (let* ((lst (buffer-list)) - (progress-reporter (make-progress-reporter - "Imenu indexing buffers..." 1 (length lst)))) - (prog1 - (cl-loop with cur-buf = (current-buffer) + (cl-loop with cur-buf = (if build-sources + (current-buffer) helm-current-buffer) for b in lst for count from 1 when (and (with-current-buffer b (derived-mode-p 'prog-mode)) (with-current-buffer b - (helm-same-major-mode-p cur-buf - helm-imenu-all-buffer-assoc))) - do (progress-reporter-update progress-reporter count) - and - collect (helm-make-source (format "Imenu in %s" (buffer-name b)) + (helm-same-major-mode-p + cur-buf helm-imenu-all-buffer-assoc))) + if build-sources + collect (helm-make-source + (format "Imenu in %s" (buffer-name b)) 'helm-imenu-source :candidates (with-current-buffer b (helm-imenu-candidates b)) - :fuzzy-match helm-imenu-fuzzy-match)) + :fuzzy-match helm-imenu-fuzzy-match) + else + append (with-current-buffer b + (helm-imenu-candidates b)) + do (progress-reporter-update progress-reporter count)) (progress-reporter-done progress-reporter)))) (defun helm-imenu--candidates-1 (alist) @@ -318,7 +304,7 @@ or it have an association in `helm-imenu-all-buffer-assoc'." (not (null (memq 'helm-source-imenu-all helm-sources-using-default-as-input)))) (sources (if helm-imenu-in-all-buffers-separate-sources - (helm-imenu-collect-sources-from-all-buffers) + (helm-imenu-candidates-in-all-buffers 'build-sources) '(helm-source-imenu-all)))) (helm :sources sources :default (list (concat "\\_<" str "\\_>") str) From 26e343e83a113618a90043be1a2fca0e8c143d03 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Thu, 9 Mar 2017 06:29:34 +0100 Subject: [PATCH 5/6] Bind M-left/right to next/previous source in imenu-map. * helm-imenu.el (helm-imenu-map): Do it. --- helm-imenu.el | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/helm-imenu.el b/helm-imenu.el index 7cad360f7..7b406e04b 100644 --- a/helm-imenu.el +++ b/helm-imenu.el @@ -67,8 +67,10 @@ only '((foo . bar)) is needed." (define-key map (kbd "M-") 'helm-imenu-next-section) (define-key map (kbd "M-") 'helm-imenu-previous-section) (when helm-imenu-lynx-style-map - (define-key map (kbd "") 'helm-maybe-exit-minibuffer) - (define-key map (kbd "") 'helm-execute-persistent-action)) + (define-key map (kbd "") 'helm-maybe-exit-minibuffer) + (define-key map (kbd "") 'helm-execute-persistent-action) + (define-key map (kbd "M-") 'helm-previous-source) + (define-key map (kbd "M-") 'helm-next-source)) (delq nil map))) (defun helm-imenu-next-or-previous-section (n) From 74ec1fc7eaa5060115101009ce3cc416f2470bf2 Mon Sep 17 00:00:00 2001 From: Thierry Volpiatto Date: Thu, 9 Mar 2017 06:31:15 +0100 Subject: [PATCH 6/6] Bind M-o to previous source in helm-map. * helm.el (helm-map): Do it. --- helm.el | 1 + 1 file changed, 1 insertion(+) diff --git a/helm.el b/helm.el index 30ba002e3..84165ff0e 100644 --- a/helm.el +++ b/helm.el @@ -188,6 +188,7 @@ NOTE: SUBKEY and OTHER-SUBKEYS bindings support char syntax only (define-key map (kbd "C-z") 'helm-execute-persistent-action) (define-key map (kbd "C-j") 'helm-execute-persistent-action) (define-key map (kbd "C-o") 'helm-next-source) + (define-key map (kbd "M-o") 'helm-previous-source) (define-key map (kbd "C-l") 'helm-recenter-top-bottom-other-window) (define-key map (kbd "M-C-l") 'helm-reposition-window-other-window) (define-key map (kbd "C-M-v") 'helm-scroll-other-window)