Emacs function to open file [current date].tex

自作多情 提交于 2019-12-23 12:24:30

问题


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")))


来源:https://stackoverflow.com/questions/5019581/emacs-function-to-open-file-current-date-tex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!