0

I'm using ivy and projectile.

When I search for some file with projectile-find-file, and the file does not exist, I would it to be created.

If it is inside directories that also do not exist, ask if create those directories.

How to do that?

I found that some old code for helm https://www.reddit.com/r/emacs/comments/3m8i5r/helmprojectile_quickly_findcreate_new_file_in/

and that it's already commited https://github.com/bbatsov/helm-projectile/commit/1f0dc42645f006e925718785b9abc6991b7ce2c4.

I have such function for find-file:

(defun my-create-non-existent-directory ()
      (let ((parent-directory (file-name-directory buffer-file-name)))
        (when (and (not (file-exists-p parent-directory))
                   (y-or-n-p (format "Directory `%s' does not exist! Create it?" parent-directory)))
          (make-directory parent-directory t))
          ;; wait until directory is created
          (while (not (file-directory-p parent-directory))
              (sleep-for 1)))
          ;; https://stackoverflow.com/questions/2592095/how-do-i-create-an-empty-file-in-emacs/11990694#11990694
          (shell-command (concat "touch " (shell-quote-argument (buffer-file-name)))))

(add-to-list 'find-file-not-found-functions #'my-create-non-existent-directory)

1 Answer 1

0

It looks like projectile-find-file will run my function.

1
  • 1
    Can you elaborate? Can you tell us why this is an answer? Thx.
    – Drew
    Commented Jun 22, 2018 at 14:33

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.