As explained here, by

(setq exec-path (append exec-path '("/sw/bin")))

one can append the directory name /sw/bin to exec-path.

Is it possible to append to exec-path the absolute file names of all directories on the local machine that match a certain regular expression? (for example to append (full) directory names that begin with some string to exec-path)?

  • The directories where emacs should look for executables. Every directory has a path. Of course all directories on the local machine.
    – Name
    Aug 25, 2015 at 16:30
  • It doesn't give you the full power of regexps, but you might consider using file-expand-wildcards.
    – npostavs
    Aug 25, 2015 at 19:30
  • @npostavs what is the correct syntax to use file-expand-wildcards in this situation? I tried something (setq exec-path (append exec-path '(file-expand-wildcards "C:/*/SeaMonkey"))) without success. If you are comfortable please consider to convert your comment to an answer.
    – Name
    Aug 25, 2015 at 19:56
  • don't quote the call. (setq exec-path (append exec-path (file-expand-wildcards "C:/*/SeaMonkey"))). I don't see a need to make a new answer for every possible function that can produce a list of directories.
    – npostavs
    Aug 25, 2015 at 21:36
  • @npostavs I confirm that this now works. Thanks for this solution.
    – Name
    Aug 25, 2015 at 22:02

1 Answer 1

  1. Load library Dired+ (`dired+.el).

  2. Use this command:

;; Change ROOT definition if you want a different Windows drive as root.
;; (Windows does not have a single root.)
(defun foo (regexp)
  "Append the accessible directories that match REGEXP to `exec-path'.
You are prompted for the REGEXP."
  (interactive (list (read-regexp "Regexp: ")))
  (message "Gathering directories...")
  (let* ((root         (if (eq system-type 'windows-nt) "C:/" "/"))
         (dirs-to-add  (diredp-directories-within
                         (lambda (dir) (string-match-p regexp dir)))))
    (setq exec-path  (append exec-path dirs-to-add))))

FYI, this is diredp-directories-within, but the definition of diredp-files-within is too long to show here. See `dired+.el for it.

(defun diredp-directories-within (&optional directory no-symlinks-p predicate)
  "List of accessible directories within DIRECTORY.
Directories in `icicle-ignored-directories' are skipped, if you use
Icicles.  Otherwise, directories in `vc-directory-exclusion-list' are

Optional arg DIRECTORY defaults to the value of `default-directory'.
Non-nil optional arg NO-SYMLINKS-P means do not follow symbolic links.
Non-nil optional arg PREDICATE must be a function that accepts a
 file-name argument.  Only directories that satisfy PREDICATE are
 included in the result."
  (unless directory (setq directory  default-directory))
  (let ((dirs  (diredp-files-within (directory-files directory 'FULL diredp-re-no-dot)
                                    () no-symlinks-p 'INCLUDE-DIRS-P
    (if predicate (diredp-remove-if-not predicate dirs) dirs)))

FYI2: See also Emacs bug #21346, which I just reported. It might affect the use of this command.

  • Actually I also get the error from the bug which you have reported (Opening directory: permission denied, C:/$RECYCLE.BIN/....). This is despite the fact that C:/$RECY.. does not match my regexp. Is there a way to bypass this error? (Emacs version 24.5, Windows).
    – Name
    Aug 25, 2015 at 18:38
  • 1
    I imagine you saw Eli's reply in the bug thread, and my reply to him. You could try using ignore-errors. Or you could try filtering out that particular directory, but there might be others that are also problematic. When I get some time I'll try to see if I can finesse this properly in diredp-directories-within. There may be problems, including performance, with (ignoring errors while) actually trying to access each directory in order to test it for accessibility.
    – Drew
    Aug 25, 2015 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.