Emacs Custom macro/key-binding to add initials and datetime to code comment

拈花ヽ惹草 提交于 2019-12-24 20:13:53

问题


I'm looking for the cleanest way to have a language agnostic keybinding that will insert my initials + formatted timestamp e.g.:

--<my initials> (28 Oct 2013 11:38:20 AM)

into an Emacs buffer, so that I can initial my comments. I know I could create a function and create a keybinding to call it, but I feel like Emacs probably has a de facto way to do stuff like this, presumedly through the use of macros.


回答1:


F3 (start recording macro)

--AA C-u M-! date C-e (insert text, insert output from date command)

F4 (stop recording macro)

C-x C-k n my-initial-timestamp (name the macro)

C-x C-k b ... (bind the macro to an appropriate key combination)

And finally, open ~/.emacs and type C-u M-x insert-kbd-macro RET my-initial-timestamp, which will insert the Lisp code to recreate the macro and bind it to the same key at startup.




回答2:


Emacs does not have such a function, AFAIK. There is function time-stamp, which updates time-stamp templates in a buffer, but that is not what you want here.

This command gives you what you requested. Other time/date formats are available - see format-time-string. You can also use functions such as user-full-name and user-login-name, if you want your name instead of your initials. Here, I've just hard-coded your initials, NF. This should work for any buffer where a comment syntax is defined (comment-start).

(defun my-timestamp-comment ()
  (interactive)
  (insert (format "%s NF (%s)" comment-start
                  (format-time-string "%b %e %Y %r" (current-time)))))


来源:https://stackoverflow.com/questions/19639185/emacs-custom-macro-key-binding-to-add-initials-and-datetime-to-code-comment

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