Skip to content

Instantly share code, notes, and snippets.

@vonHabsi
Last active July 23, 2017 10:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vonHabsi/d8c6fdbcd5a5a0b44af0abdb7d347ecb to your computer and use it in GitHub Desktop.
Save vonHabsi/d8c6fdbcd5a5a0b44af0abdb7d347ecb to your computer and use it in GitHub Desktop.
sample helm find files
(defun ap/helm-find-files ()
;; from gottabeme https://www.reddit.com/r/emacs/comments/6ogkp3/how_to_add_more_source_to_helmfindfiles_or/
;; https://stackoverflow.com/questions/11403862/how-to-have-emacs-helm-list-offer-files-in-current-directory-as-options
(interactive)
;; From helm-buffers-list in helm-buffers.el This wasn't necessary
;; until I updated helm...then suddenly helm-source-buffers-list
;; stopped working. And this is why I am reluctant to update
;; things. SIGH.
(unless helm-source-buffers-list
(setq helm-source-buffers-list
(helm-make-source " Buffers" 'helm-source-buffers)))
;; From file:elpa/helm-20160401.1302/helm-files.el::(with-helm-temp-hook%20'helm-after-initialize-hook
;; This lets me bring up results from locate without having to
;; exit and run a separate command. Now I just have to remember
;; to use it...
(with-helm-temp-hook 'helm-after-initialize-hook
(define-key helm-map (kbd "C-x C-l")
'helm-multi-files-toggle-to-locate))
(helm :sources '(ap/helm-source-ivy-views
ap/helm-source-current-file-other-buffers
helm-source-buffers-list
ap/helm-source-files-in-current-dir
helm-source-org-recent-headings
helm-source-bookmarks
ap/helm-source-recentf
ap/helm-source-bindir
ap/helm-source-projectile-files-list)
:buffer " * ap/helm-find-files *"))
(defvar ap/helm-source-files-in-current-dir
(helm-make-source " Files in current directory" 'helm-files-in-current-dir-source
:candidate-number-limit 10))
(defvar ap/helm-source-bindir
`((name . "~/.bin")
(candidates . ,(lambda ()
(directory-files "~/.bin" t directory-files-no-dot-files-regexp)))
(action . ,(lambda (x)
(find-file x)))
(candidate-number-limit . 10)))
(defvar ap/helm-source-projectile-files-list
;; Copied from helm-source-projectile-files-list
(helm-build-sync-source "Projectile files"
:candidates (lambda ()
(condition-case nil
(cl-loop with root = (projectile-project-root)
for display in (projectile-current-project-files)
collect (cons display (expand-file-name display root)))
(error nil)))
:fuzzy-match helm-projectile-fuzzy-match
:keymap helm-projectile-find-file-map
:help-message 'helm-ff-help-message
:mode-line helm-read-file-name-mode-line-string
:action helm-projectile-file-actions
:persistent-action #'helm-projectile-file-persistent-action
:persistent-help "Preview file"
:candidate-number-limit 10)
"Helm source definition for Projectile files.")
(defvar ap/helm-source-ivy-views
(helm-build-sync-source " Ivy views"
:candidates (lambda ()
(cl-loop for view in ivy-views
collect (cons (car view)
(cadr view))))
:action (list (cons "Set view"
(lambda (view)
(delete-other-windows)
(ivy-set-view-recur view)))
(cons "Pop view"
(lambda (view)
(ivy-pop-view-action (rassoc (list view) ivy-views))))))
"Helm source for `ivy-views'.")
(defvar ap/helm-source-current-file-other-buffers
(helm-build-sync-source " Current file's other buffers"
:action #'switch-to-buffer
:candidates (lambda ()
(mapcar 'buffer-name
(-when-let (indirect-buffers (ap/current-file-indirect-buffers helm-current-buffer))
(-when-let (base-buffer (buffer-base-buffer helm-current-buffer))
;; Add the base buffer if current buffer is indirect
(push base-buffer indirect-buffers))
;; Remove current-buffer from list
(--remove (eq it helm-current-buffer) indirect-buffers))))))
(defun ap/current-file-indirect-buffers (&optional buffer)
"Return list of indirect buffers backed by BUFFER's file.
If BUFFER is nil, the current buffer is used."
(let ((buffer (or buffer (current-buffer))))
(ap/indirect-buffers-for-base-buffer (or (buffer-base-buffer buffer)
buffer))))
(defun ap/indirect-buffers-for-base-buffer (base-buffer)
"Return list of indirect buffers whose base buffer is BASE-BUFFER."
(cl-loop for buffer in (buffer-list)
when (eq (buffer-base-buffer buffer) base-buffer)
collect buffer into buffers
finally return (-uniq buffers)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment