0

Could you please tell me how I can write a function which creates and visits a buffer in a particular directory (a default directory name is hard coded, but it would be nice to have an opportunity to change the directory) with some predefined prefix (current date in the format yyyymmdd) and then offers to finish the title (eg 20170106EMACS_QUESTION.tex)?

Thus, when the function is called, it would do the following in order:

1) ask for a directory path of the buffer (if nil, autocomplete with the hard coded default)

2) ask for a file name, with the timestamp yyyymmdd already filled in at the beginning.

3) create and visit a buffer with the above metadata

completing-read, read-string and find-file seem to be useful.

You can also find the following code helpful, which I do not know how to change to comply with the description:

(defun add-note (d)
  (interactive "d")
  (let ((default-directory d))
    (find-file (format-time-string "%Y%m%d-CUSTOM-TITLE"
    (current-time))))) 
0

What do you mean by "create a file"? Do you mean visit a file buffer, so that you can save the buffer as a file?

I think you are just looking for function read-file-name. It reads a file name, prompting:

(read-file-name PROMPT &optional DIR DEFAULT-FILENAME MUSTMATCH INITIAL PREDICATE)

C-h f read-file-name tells you more. But basically DIR is the directory for completion and DEFAULT-FILENAME is the default (relative) file name.

If you are looking for more than this or something different, please clarify your question a bit.

After reading the file name, you can visit a buffer for it. See the code that defines find-file for using find-file-noselect etc.

Or you could just use find-file. It uses read-file-name (see find-file-read-args). But it does not let you provide a default file name. If you do that, bind default-directory to the directory you want, before you call find-file.


Update after your comment:

If you want a file-name prefix automatically inserted then provide that as the INITIAL argument to read-file-name. That's what it's for. Please read the doc: C-h f read-file-name.

Fifth arg INITIAL specifies text to start with.

and

If DEFAULT-FILENAME is omitted or
nil, then if INITIAL is non-nil, the default is DIR combined with
INITIAL; otherwise, if the current buffer is visiting a file,
that file serves as the default; otherwise, the default is simply
the string inserted into the minibuffer.
  • Drew, thank you! By creating a file I mean visiting a file buffer, sorry for the ambiguity. read-file-name is close in the sense of having an option to point to a default DIR, but the idea is to have a prefix (timestamp yyyymmdd) in the filename which can then be completed. – sdll Jan 6 '17 at 12:50
  • I have edited the question with further clarification. – sdll Jan 6 '17 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.