Reputation: 157
This is related to this question:
how-to-have-emacs-helm-list-offer-files-in-current-directory-as-options
but rather than add files from the current directory, I'd like to be able to have a fixed list of directories that helm-mini would always offer the files from. Ideally, I would like to be able to just have the files with a particular extension, and I'd like this to be done recursively (only one layer deep, in fact).
Upvotes: 4
Views: 1172
Reputation: 10804
Here you go. I am using this code to list all org files in a particular directory. If you want to list all the files, just remove the candidate-transformer line in the source, and remove the emagician/helm-ct-is-org-file.
You'll likely want to rename the source/variable/function too. ;)
edit: Fixed, thanks to peeking at helm-cmd-t
note: This is my first real crack at making a helm source, and this implementation likely sucks. It also specifically solves my problem (finding all org files in one directly only) rather then the more generalized problem (building a helm source based on files from a particular directory).
(defvar emagician/helm-c-source-files
`((name . "Find Emagician Files")
(header-name . (lambda (_)))
(candidates . ,(lambda ()
(when (file-accessible-directory-p emagician-dir)
(directory-files emagician-dir t))))
(match helm-c-match-on-file-name helm-c-match-on-directory-name)
(keymap . ,helm-generic-files-map)
(candidate-transformer . emagician/helm-ct-is-org-file)
(help-message . helm-generic-file-help-message)
(mode-line . ,helm-generic-file-mode-line-string)
(type . file)))
(defun emagician/helm-ct-is-org-file (candidates)
(remove-if-not (lambda (c)
(and (string= (substring c -4) ".org")
(not (string= (substring (file-name-nondirectory c) 0 2) ".#"))))
candidates))
(defun emagician/helm-emagician-dir ()
"List all the org files in the Emagician dir"
(interactive)
(helm :sources emagician/helm-c-source-files
:candidate-number-limit 40
:buffer "*emagician-|-+-|-files*"))
(global-set-key (kbd "S-<f3>") 'emagician/helm-emagician-dir)
Upvotes: 1
Reputation: 17707
Here is a slightly different take that prefilters for the org extension.
(require 'helm-cmd-t)
(defvar my-org-folders (list "~/org")
"my permanent folders for helm-mini")
(defun helm-my-org (&optional arg)
"Use C-u arg to work with repos."
(interactive "P")
(if (consp arg)
(call-interactively 'helm-cmd-t-repos)
(let ((helm-ff-transformer-show-only-basename nil))
(helm :sources (mapcar (lambda (dir)
(helm-cmd-t-get-create-source-dir dir))
my-org-folders)
:candidate-number-limit 20
:buffer "*helm-my-org:*"
:input "org$ "))))
Upvotes: 3
Reputation: 17707
You can better solve it by leveraging the helm-cmd-t library. It packages a directory recursively as repositories that you can use as a source.
It understands how to read lots of DVCS repos very fast.
The default functionality is not exactly what you are after here, but you can easily leverage the machinery to fill all your requirements.
For example, here I define a command that adds two repos to the default helm-mini sources.
(require 'helm-cmd-t)
(defvar my-mini-folders (list "~/src/ember/data" "~/src/ember/ember.js")
"my permanent folders for helm-mini")
(defun helm-my-mini (&optional arg)
"my helm-mini. Use C-u arg to work with repos."
(interactive "P")
(if (consp arg)
(call-interactively 'helm-cmd-t-repos)
(let ((helm-ff-transformer-show-only-basename nil))
(helm :sources (nconc (list
helm-c-source-buffers-list
helm-c-source-recentf
helm-c-source-buffer-not-found)
(mapcar (lambda (dir)
(helm-cmd-t-get-create-source-dir dir))
my-mini-folders))
:candidate-number-limit 20
:buffer "*helm-my-mini:*"))))
Upvotes: 2