Function: project-prompt-project-name

project-prompt-project-name is a byte-compiled function defined in project.el.gz.

Signature

(project-prompt-project-name &optional PROMPT PREDICATE REQUIRE-KNOWN ALLOW-EMPTY)

Documentation

Prompt the user for a project, by name, that is one of the known project roots.

The project is chosen among projects known from the project list, see project-list-file. If PROMPT is non-nil, use it as the prompt string. If PREDICATE is non-nil, filter possible project choices using this function; see project-prompter for more details. Unless REQUIRE-KNOWN is non-nil, it's also possible to enter an arbitrary directory not in the list of known projects. If ALLOW-EMPTY is non-nil, it is possible to exit with no input.

Source Code

;; Defined in /usr/src/emacs/lisp/progmodes/project.el.gz
(defun project-prompt-project-name
    (&optional prompt predicate require-known allow-empty)
  "Prompt the user for a project, by name, that is one of the known project roots.
The project is chosen among projects known from the project list,
see `project-list-file'.
If PROMPT is non-nil, use it as the prompt string.
If PREDICATE is non-nil, filter possible project choices using this
function; see `project-prompter' for more details.
Unless REQUIRE-KNOWN is non-nil, it's also possible to enter an
arbitrary directory not in the list of known projects.
If ALLOW-EMPTY is non-nil, it is possible to exit with no input."
  (if-let* ((pred (alist-get 'prompt project-prune-zombie-projects))
            (inhibit-message t))
      (project--delete-zombie-projects pred))
  (let* ((dir-choice "... (choose a dir)")
         project--name-history
         (choices
          (let (ret)
            ;; Iterate in reverse order so project--name-history is in
            ;; the same order as project--list.
            (dolist (dir (reverse (project-known-project-roots)))
              ;; We filter out directories that no longer map to a project,
              ;; since they don't have a clean project-name.
              (when-let* (((or (not predicate) (funcall predicate dir)))
                          (proj (project--find-in-directory dir))
                          (name (project-name proj)))
                (push name project--name-history)
                (push (cons name proj) ret)))
            (reverse ret)))
         ;; XXX: Just using this for the category (for the substring
         ;; completion style).
         (table (project--file-completion-table
                 (reverse (if require-known choices
                            (cons dir-choice choices)))))
         pr-name)
    (cl-loop
     do (setq pr-name
              (let (history-add-new-input)
                (completing-read (if prompt
                                     (format "%s: " prompt)
                                   "Select project: ")
                                 table nil t nil 'project--name-history)))
     ;; If the user simply pressed RET, do this again until they don't.
     while (and (not allow-empty) (equal pr-name "")))
    (pcase pr-name
      ("" "")
      ((pred (equal dir-choice)) (read-directory-name "Select directory: "
                                                      default-directory nil t))
      (_ (let ((proj (assoc pr-name choices)))
           (if (stringp proj) proj (project-root (cdr proj))))))))