Function: mh-prompt-for-folder

mh-prompt-for-folder is an autoloaded and byte-compiled function defined in mh-utils.el.gz.

Signature

(mh-prompt-for-folder PROMPT DEFAULT CAN-CREATE &optional DEFAULT-STRING ALLOW-ROOT-FOLDER-FLAG)

Documentation

Prompt for a folder name with PROMPT.

Returns the folder's name as a string. DEFAULT is used if the folder exists and the user types return. If the CAN-CREATE flag is t, then a folder is created if it doesn't already exist. If optional argument DEFAULT-STRING is non-nil, use it in the prompt instead of DEFAULT. If ALLOW-ROOT-FOLDER-FLAG is non-nil then the function will accept the folder +, which means all folders when used in searching.

Source Code

;; Defined in /usr/src/emacs/lisp/mh-e/mh-utils.el.gz
;;;###mh-autoload
(defun mh-prompt-for-folder (prompt default can-create
                             &optional default-string allow-root-folder-flag)
  "Prompt for a folder name with PROMPT.
Returns the folder's name as a string. DEFAULT is used if the
folder exists and the user types return. If the CAN-CREATE flag
is t, then a folder is created if it doesn't already exist. If
optional argument DEFAULT-STRING is non-nil, use it in the prompt
instead of DEFAULT. If ALLOW-ROOT-FOLDER-FLAG is non-nil then the
function will accept the folder +, which means all folders when
used in searching."
  (if (null default)
      (setq default ""))
  (let* ((default-string (or default-string
                             (if (equal default "") nil default)))
         (prompt (format-prompt "%s folder" default-string prompt))
         (mh-current-folder-name mh-current-folder)
         read-name folder-name)
    (while (and (setq read-name (mh-folder-completing-read
                                 prompt default allow-root-folder-flag))
                (equal read-name "")
                (equal default "")))
    (cond ((or (equal read-name "")
               (and (equal read-name "+") (not allow-root-folder-flag)))
           (setq read-name default))
          ((not (mh-folder-name-p read-name))
           (setq read-name (format "+%s" read-name))))
    (if (or (not read-name) (equal "" read-name))
        (error "No folder specified"))
    (setq folder-name read-name)
    (cond ((and (> (length folder-name) 0)
                (eq (aref folder-name (1- (length folder-name))) ?/))
           (setq folder-name (substring folder-name 0 -1))))
    (let* ((last-slash (mh-search-from-end ?/ folder-name))
           (parent (and last-slash (substring folder-name 0 last-slash)))
           (child (if last-slash
                      (substring folder-name (1+ last-slash))
                    (substring folder-name 1))))
      (unless (member child
                      (mapcar #'car (gethash parent mh-sub-folders-cache)))
        (mh-remove-from-sub-folders-cache folder-name)))
    (let ((new-file-flag
           (not (file-exists-p (mh-expand-file-name folder-name)))))
      (cond ((and new-file-flag
                  can-create
                  (y-or-n-p
                   (format "Folder %s does not exist.  Create it? "
                           folder-name)))
             (message "Creating %s" folder-name)
             (mh-exec-cmd-error nil "folder" folder-name)
             (mh-remove-from-sub-folders-cache folder-name)
             (when (and (boundp 'speedbar-buffer) speedbar-buffer)
               (mh-speed-add-folder folder-name))
             (message "Creating %s...done" folder-name))
            (new-file-flag
             (error "Folder %s does not exist" folder-name))
            ((not (file-directory-p (mh-expand-file-name folder-name)))
             (error "%s is not a directory"
                    (mh-expand-file-name folder-name)))))
    folder-name))