Subject: bug#36979: closed (Re: bug#36979: Calendar: mention how to copy date)

From: Deus Max <[email-masked]>
Date: Sun, 11 Aug 2019 14:13:25 +0300

On Sat, Aug 10 2019, Tomas Nordin wrote:

>
> I can add my sympathy for the desire of functionality to add date under
> point to the kill ring. I was searching for such a feature at some point
> (didn't find it) and wrote this function which I bound to RET in
> calendar-mode-map:
>
> (defun tn-calendar-kill-date (&optional arg)
>   "Kill new a string based on point in calendar buffer in iso format
>
> With no prefix ARG, kill the date as an iso date.
> With one prefix arg ('C-u'), kill the date as an iso week.
> with two prefix arg ('C-u C-u'), kill as both the iso week and date."
>   (interactive "p")
>   (let* ((date (calendar-cursor-to-date))
>          (encoded-time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date)))
>          (date-string (format-time-string "%Y-%m-%d" encoded-time))
>          (iso-week-string (format-time-string "%gW%V" encoded-time))
>          kill-string)
>
>     (cond
>      ((= arg 4)
>       (setq kill-string iso-week-string))
>      ((= arg 16)
>       (setq kill-string (format "%s %s" iso-week-string date-string)))
>      (t
>       (setq kill-string date-string)))
>
>     (kill-new kill-string)
>     (message "Put %s to kill-ring" kill-string)))
>
> Best regards
> --
> Tomas

Hi Tomas,

Great function for it can be very useful thank you.

I slightly modified it, so it doesn't blow up when the cursor is not on
a date and provides an informative message.

;; Copied from bug#36979 by Tomas Nordin, emacs-bugs maillist.
(defun tn-calendar-kill-date (&optional arg)
  "Kill new a string based on point in calendar buffer in iso format

With no prefix ARG, kill the date as an iso date.
With one prefix arg ('C-u'), kill the date as an iso week.
with two prefix arg ('C-u C-u'), kill as both the iso week and date."
  (interactive "p")
  (let ((date (calendar-cursor-to-date t)))
    (if date
        ;; date is valid
        (let* ((encoded-time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date)))
               (date-string (format-time-string "%Y-%m-%d" encoded-time))
               (iso-week-string (format-time-string "%gW%V" encoded-time))
               kill-string)
          (cond
           ((= arg 4)
            (setq kill-string iso-week-string))
           ((= arg 16)
            (setq kill-string (format "%s %s" iso-week-string date-string)))
           (t
            (setq kill-string date-string)))
          ;; add to kill ring
          (kill-new kill-string)
          (message "Put %s to kill-ring" kill-string))
      ;; date is not valid - cursor is not on a date.
      (message "Not on a date."))))