Emacs function to open file [current date].tex

2019-06-28 06:05发布

问题:

I'm trying to write an emacs function that uses the current date to create a file. I'm new to emacs and so I'm having trouble with variables and syntax. Here's what I have:

(defun daily ()
    (interactive)
    (let daily-name (format-time-string "%T"))
    (find-file (daily-name)))

I don't understand how emacs uses variables well enough to get it to set the time string as a variable and feed that variable into the find-file function. Any help is appreciated.

回答1:

To build on what others are saying:

(defun daily-tex-file ()
  (interactive)
  (let ((daily-name (format-time-string "%Y-%m-%d")))
    (find-file (expand-file-name (concat "~/" daily-name ".tex")))))

Main differences:

  • Different format string, which gives date instead of time (which is what you want, I think)
  • specifying the directory (~/) -- if you don't put this, you'll get files all over the place, depending on what the current working directory is at the moment you invoke the function
  • better function name


回答2:

(defun daily ()
  (interactive)
  (let ((daily-name (format-time-string "%T")))
    (find-file (format "%s.tex" daily-name))))

Calling M-x daily now opens a file "12:34:56.tex".



回答3:

(defun daily ()     
  (interactive)     
  (let ((daily-name (format-time-string "%T")))
      (find-file (concat daily-name ".tex"))))


回答4:

You have too few parentheses in some places, and too many in others. This is the corrected version of your function:

(defun daily ()
  (interactive)
  (let ((daily-name (format-time-string "%T")))
    (find-file daily-name)))

Note in particular that the expression (daily-name) attempts to call a function by that name; to access the value of the variable daily-name, just write its name on its own, without parentheses.

Also note that in this particular case, you can do without a variable entirely:

(defun daily ()
  (interactive)
  (find-file (format-time-string "%T")))