Function: treemacs--create-file/dir

treemacs--create-file/dir is an interactive and byte-compiled function defined in treemacs-file-management.el.

Signature

(treemacs--create-file/dir IS-FILE\?)

Documentation

Interactively create either a file or directory, depending on IS-FILE.

IS-FILE?: Bool

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/treemacs-20251226.1307/treemacs-file-management.el
(defun treemacs--create-file/dir (is-file?)
  "Interactively create either a file or directory, depending on IS-FILE.

IS-FILE?: Bool"
  (interactive)
  (let* ((curr-path (treemacs--select-file-from-btn
                     (treemacs-current-button)
                     "Create in: " :dir-only))
         (path-to-create (treemacs-canonical-path
                          (read-file-name
                           (if is-file? "Create File: " "Create Directory: ")
                           (treemacs--add-trailing-slash
                            (if (file-directory-p curr-path)
                                curr-path
                              (treemacs--parent-dir curr-path)))))))
    (treemacs-block
     (treemacs-error-return-if (file-exists-p path-to-create)
       "%s already exists." (propertize path-to-create 'face 'font-lock-string-face))
     (treemacs--without-filewatch
      (if is-file?
          (-let [dir (treemacs--parent-dir path-to-create)]
            (unless (file-exists-p dir)
              (make-directory dir t))
            (write-region "" nil path-to-create nil 0))
        (make-directory path-to-create t))
      (run-hook-with-args 'treemacs-create-file-functions path-to-create))
     (-when-let (project (treemacs--find-project-for-path path-to-create))
       (-when-let* ((created-under (treemacs--parent path-to-create))
                    (created-under-btn (treemacs-find-visible-node created-under)))
         ;; update only the part that changed to keep things smooth
         ;; for files that's just their parent, for directories we have to take
         ;; flattening into account
         (-let [path-to-update
                (if (treemacs-button-get created-under-btn :collapsed)
                    (treemacs-button-get (treemacs-button-get created-under-btn :parent) :path)
                  (treemacs-button-get created-under-btn :path))]
           (treemacs-update-node path-to-update)
           (when (treemacs--non-simple-git-mode-enabled)
             (treemacs-update-single-file-git-state path-to-update))))
       (treemacs-goto-file-node path-to-create project)
       (recenter))
     (treemacs-pulse-on-success
         "Created %s." (propertize path-to-create 'face 'font-lock-string-face)))))