1

I seem to remember from several years ago, an alternative and better package and/or command to "Time-stamp: <...>". I can't seem to find it now.

Does anyone know of this?

I want to use this in C code, not in the file header.

  • I could have misunderstood your question. My solution won't auto-update the time-stamps on each file save. The solution just inserts the current time-stamp wherever the point is, anywhere in the buffer. If you need auto-updating time-stamps, please let me know so that I can delete that answer. – Kaushal Modi Oct 14 '15 at 2:47
  • 3
    The question as posed so far should be closed as unclear. Please edit it to clarify what you are asking. In what way is the package expected to be an "alternative"? Just what do you mean by "better'? What are you really trying to do? What have you tried so far? What did you expect from what you tried that you did not see? – Drew Oct 14 '15 at 14:52
0

I use the below custom command written to my taste. Try it out to see if it fits your use case:

You can modify the time-stamp format in the let-bound current-date-time-format variable to your liking.

Key points on how this command behaves:

  • By default, the time-stamp + user name is inserted.
  • If the point is not in a comment or string, the comment characters are prefixed automatically.
  • If the major-mode doesn't have comment characters defined, no prefix is inserted.
  • If the point in inside a comment or string, and
    • If the point is immediately after the comment characters, no prefix is inserted.
    • If the point is not immediately after the comment characters, -- prefix is inserted.
  • Documentation in the below command's docstring further explains how the universal prefix arguments can be used to tweak the above default behavior.
(defun modi/insert-time-stamp (option)
  "Insert time-stamp with user name - DWIM.

If the point is not in a comment or string,
  time-stamp + user name is inserted prefixed with `comment-start' characters.

If the point is in a comment or string immediately after `comment-start' chars,
or if the buffer's major-mode has `comment-start' set to nil,
  time-stamp + user name is inserted without any prefix.

If the point is in a comment or string, but *not* immediately after
`comment-start' chars,
  time-stamp + user name is inserted with `--' prefix.

Additional control:

        C-u -> Prefix (`comment-start' or `--') is not auto-inserted.
    C-u C-u -> User name is not inserted.
C-u C-u C-u -> All of the above."
  (interactive "p")
  (let ((current-date-time-format "%a %b %d %H:%M:%S %Z %Y")
        (no-prefix (or (= option 4) ; C-u or C-u C-u C-u
                       (= option 64)))
        (no-user-name (or (= option 16) ; C-u C-u or C-u C-u C-u
                          (= option 64))))
    ;; Insert a space if there is no space to the left of the current point
    ;; and it's not at the beginning of a line
    (when (and (not (looking-back "^ *"))
               (not (looking-back " ")))
      (insert " "))
    ;; Insert prefix only if `comment-start' is defined for the major mode
    (when (stringp comment-start)
      (if (or (nth 3 (syntax-ppss)) ; string
              (nth 4 (syntax-ppss))) ; comment
          ;; If the point is in a comment or string
          (progn
            ;; If the point is not immediately after `comment-start' chars
            ;; (followed by optional space)
            (when (and (not no-prefix)
                       (not (looking-back (concat comment-start " *")))
                       (not (looking-back "^ *")))
              (insert "--")))
        ;; If the point is NOT in a comment or string
        (progn
          (when (not no-prefix)
            (insert comment-start)))))
    ;; Insert a space if there is no space to the left of the current point
    ;; and it's not at the beginning of a line
    (when (and (not (looking-back "^ *"))
               (not (looking-back " ")))
      (insert " "))
    (insert (format-time-string current-date-time-format (current-time)))
    (when (not no-user-name)
      (insert (concat " - " (getenv "USER"))))
    ;; Insert a space after the time stamp if not at the end of the line
    (when (not (looking-at " *$"))
      (insert " "))))

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