Skip to content

Instantly share code, notes, and snippets.

@joonhwan
Created February 6, 2013 23:57
Show Gist options
  • Save joonhwan/4727063 to your computer and use it in GitHub Desktop.
Save joonhwan/4727063 to your computer and use it in GitHub Desktop.
(defun cd (dir)
"Make DIR become the current buffer's default directory.
If your environment includes a `CDPATH' variable, try each one of
that list of directories (separated by occurrences of
`path-separator') when resolving a relative directory name.
The path separator is colon in GNU and GNU-like systems."
(interactive
(list
;; FIXME: There's a subtle bug in the completion below. Seems linked
;; to a fundamental difficulty of implementing `predicate' correctly.
;; The manifestation is that TAB may list non-directories in the case where
;; those files also correspond to valid directories (if your cd-path is (A/
;; B/) and you have A/a a file and B/a a directory, then both `a' and `a/'
;; will be listed as valid completions).
;; This is because `a' (listed because of A/a) is indeed a valid choice
;; (which will lead to the use of B/a).
(minibuffer-with-setup-hook
(lambda ()
(setq minibuffer-completion-table
(apply-partially #'locate-file-completion-table
cd-path nil))
(setq minibuffer-completion-predicate
(lambda (dir)
(locate-file dir cd-path nil
(lambda (f) (and (file-directory-p f) 'dir-ok))))))
(unless cd-path
(setq cd-path (or (parse-colon-path (getenv "CDPATH"))
(list "./"))))
(read-directory-name "Change default directory: "
default-directory default-directory
t))))
(unless cd-path
(setq cd-path (or (parse-colon-path (getenv "CDPATH"))
(list "./"))))
(cd-absolute
(or (locate-file dir cd-path nil
(lambda (f) (and (file-directory-p f) 'dir-ok)))
(error "No such directory found via CDPATH environment variable"))))
@joonhwan
Copy link
Author

joonhwan commented Feb 7, 2013

cd defun cannot handle quoted path of dir.
ex: if dir is like "c:/temp/dir_even_without_space", then the f value in #36 line containing "c:/temp/dir_even_without_space", which in turn signaled error "No such directory found via CDPATH environment variable".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment