Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

helm-imenu-in-all-buffers in separate sources (#1705). #1707

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
75 changes: 49 additions & 26 deletions helm-imenu.el
Expand Up @@ -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
Expand All @@ -61,8 +67,10 @@ only '((foo . bar)) is needed."
(define-key map (kbd "M-<down>") 'helm-imenu-next-section)
(define-key map (kbd "M-<up>") 'helm-imenu-previous-section)
(when helm-imenu-lynx-style-map
(define-key map (kbd "<left>") 'helm-maybe-exit-minibuffer)
(define-key map (kbd "<right>") 'helm-execute-persistent-action))
(define-key map (kbd "<left>") 'helm-maybe-exit-minibuffer)
(define-key map (kbd "<right>") 'helm-execute-persistent-action)
(define-key map (kbd "M-<left>") 'helm-previous-source)
(define-key map (kbd "M-<right>") 'helm-next-source))
(delq nil map)))

(defun helm-imenu-next-or-previous-section (n)
Expand Down Expand Up @@ -172,23 +180,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
(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 helm-current-buffer
helm-imenu-all-buffer-assoc)))
do (progress-reporter-update progress-reporter count)
and
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)))
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)
else
append (with-current-buffer b
(helm-imenu-candidates b)))
(helm-imenu-candidates b))
do (progress-reporter-update progress-reporter count))
(progress-reporter-done progress-reporter))))

(defun helm-imenu--candidates-1 (alist)
Expand Down Expand Up @@ -270,22 +286,29 @@ 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)
(helm--maybe-use-default-as-input
(not (null (memq 'helm-source-imenu-all
Copy link
Member

@xiongtx xiongtx Mar 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is (not (null... necessary? Isn't the generalized boolean from memq sufficient?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

memq is not returning a boolean so it is cleaner to return a boolean, though yes it would work also of course with a list instead of t.

helm-sources-using-default-as-input))))
(sources (if helm-imenu-in-all-buffers-separate-sources
(helm-imenu-candidates-in-all-buffers 'build-sources)
'(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)
Expand Down
1 change: 1 addition & 0 deletions helm.el
Expand Up @@ -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)
Expand Down