Last active
February 8, 2017 21:47
-
-
Save cute-jumper/c8fb40bb4eebd98d9eb2ed33481149b2 to your computer and use it in GitHub Desktop.
Make it work in other sources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;;; -*- lexical-binding: t; -*- | |
;; see http://cute-jumper.github.io/emacs/2015/11/17/let-helm-support-zshlike-path-expansion | |
(with-eval-after-load 'helm-files | |
;; Let helm support zsh-like path expansion. | |
(defvar helm-ff-expand-valid-only-p t) | |
(defvar helm-ff-sort-expansions-p t) | |
(defvar helm-ff-ignore-case-p t) | |
(defun helm-ff--generate-case-ignore-pattern (pattern) | |
(let (head (ci-pattern "")) | |
(dotimes (i (length pattern) ci-pattern) | |
(setq head (aref pattern i)) | |
(cond | |
((and (<= head ?z) (>= head ?a)) | |
(setq ci-pattern (format "%s[%c%c]" ci-pattern (upcase head) head))) | |
((and (<= head ?Z) (>= head ?A)) | |
(setq ci-pattern (format "%s[%c%c]" ci-pattern head (downcase head)))) | |
(:else | |
(setq ci-pattern (format "%s%c" ci-pattern head))))))) | |
(defun helm-ff-try-expand-fname (candidate) | |
(let ((dirparts (split-string candidate "/")) | |
valid-dir | |
fnames) | |
(catch 'break | |
(while dirparts | |
(if (file-directory-p (concat valid-dir (car dirparts) "/")) | |
(setq valid-dir (concat valid-dir (pop dirparts) "/")) | |
(throw 'break t)))) | |
(setq fnames (cons candidate (helm-ff-try-expand-fname-1 valid-dir dirparts))) | |
(if helm-ff-sort-expansions-p | |
(sort fnames | |
(lambda (f1 f2) (or (file-directory-p f1) | |
(not (file-directory-p f2))))) | |
fnames))) | |
(defun helm-ff-try-expand-fname-1 (parent children) | |
(if children | |
(if (equal children '("")) | |
(and (file-directory-p parent) `(,(concat parent "/"))) | |
(when (file-directory-p parent) | |
(apply 'nconc | |
(mapcar | |
(lambda (f) | |
(or (helm-ff-try-expand-fname-1 f (cdr children)) | |
(unless helm-ff-expand-valid-only-p | |
(and (file-directory-p f) | |
`(,(concat f "/" (mapconcat 'identity | |
(cdr children) | |
"/"))))))) | |
(directory-files parent t | |
(concat "^" | |
(if helm-ff-ignore-case-p | |
(helm-ff--generate-case-ignore-pattern | |
(car children)) | |
(car children)))))))) | |
`(,(concat parent (and (file-directory-p parent) "/"))))) | |
(defun qjp-helm-ff-try-expand-fname (orig-func &rest args) | |
(let* ((candidate (car args)) | |
(collection (helm-ff-try-expand-fname candidate))) | |
(if (and (> (length collection) 1) | |
(not (file-exists-p candidate))) | |
(with-helm-alive-p | |
(when (helm-file-completion-source-p) | |
(helm-set-pattern | |
(helm-comp-read "Expand Path to: " collection :allow-nest t)))) | |
(apply orig-func args)))) | |
(advice-add 'helm-ff-kill-or-find-buffer-fname :around #'qjp-helm-ff-try-expand-fname)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment