2

Suppose I have a path like /path/to/file/ in the buffer. I'd like to put point over that path and invoke a command that will either open it if it exists or create it if it doesn't.

Edit 1 Thanks to @Drew for pointing out that /path/to/file/ ends with a slash. I meant /path/to/file which is a file, not a directory.

Also, in my case M-x ffap on /tmp/non-existing-file will wait for user input at the minibuffer with the following prompt Find file or URL: /tmp/

enter image description here

Edit 2

Here's what I tried

(defun ffap-create-file ()
  "find or create file at point"
  (interactive)
  (setq filename (ffap-string-at-point))
  (ffap filename))

This code works, but it doesn't prompt. The "usual" behavior of ffap is to prompt the user with the filename already populated in the minibuffer.

ychaouche
  • 207
  • 1
  • 7

1 Answers1

1

Just M-x ffap does what you want, no? If so, just bind it to some key:

(global-set-key `C-o` #'ffap)

That puts you in a buffer for the new file (or new directory, if, as you show, you use a final slash). Just do what you want in that buffer, then save it, if it's for a file. Or if it's for a directory then (as the message tells you when you create the buffer), use M-x make-directory RET RET to create the directory.

This command will take care of creating the directory as well:

(defun my-ffap (file/dir)
  "..."
  (interactive (progn (require 'ffap) (list (ffap-prompter))))
  (when (directory-name-p file/dir) (make-directory file/dir t))
  (find-file file/dir))
Drew
  • 75,699
  • 9
  • 109
  • 225
  • In my case `M-x ffap` on `/tmp/non-existing-file` will wait for user input at the minibuffer with the following prompt `Find file or URL: /tmp/` – ychaouche Apr 26 '22 at 09:11
  • Did you try `my-ffap`? For me, it fills the minibuffer with the file name the cursor is on. – Drew Apr 26 '22 at 15:51
  • same with `my-ffap`, no automatic entry in the minibuffer. See https://imgur.com/a/rsunUdL. It could be that my emacs version is too old (24.3.1) – ychaouche Apr 27 '22 at 10:21
  • same if I use code of ffap-prompter from [emacs github mirror](https://github.com/emacs-mirror/emacs/blob/3af9e84ff59811734dcbb5d55e04e1fdb7051e77/lisp/ffap.el) – ychaouche Apr 27 '22 at 10:57
  • Here's what I tried https://gist.github.com/ychaouche/0428f902f00dd3c30ec1d74e5ac76c9c. It works, except it doesn't prompt. – ychaouche Apr 27 '22 at 11:05
  • If you're using Emacs 24.3.1 then function `directory-name-p` isn't defined - you'll get an error in that case. Define a replacement for it, that tests whether the file-name string ends in `/`, and use that. – Drew Apr 27 '22 at 15:19
  • I didn't get that error, the prompt showed just fine as mentioned in my [previous comment](https://emacs.stackexchange.com/questions/71506/create-file-with-ffap-if-it-doesnt-exist#comment115546_71510), although I did not hit enter to validate, because the prompt didn't get populated with the full path to the desired file. – ychaouche Apr 28 '22 at 08:42