2

Is there a way to retrieve the date under the cursor in emacs calendar as the format day.month.year like 05.07.2018 as a clipboard content?

Name
  • 7,689
  • 4
  • 38
  • 84

1 Answers1

3

The following code augments the key sequence M-w such that it copies the date below point with the format "%d.%m.%Y when region is not active ( %d: zero padded day, %m: zero-padded month, %Y: year).

You can change the format with the customization option calendar-copy-as-kill-format.

(defcustom calendar-copy-as-kill-format "%d.%m.%Y"
  "Format string for formatting calendar dates with `format-time-string'."
  :type 'string
  :group 'calendar)

(defun calendar-copy-as-kill ()
  "Copy date at point as kill if region is not active.
Delegate to `kill-ring-save' otherwise."
  (interactive)
  (if (use-region-p)
      (call-interactively #'kill-ring-save)
    (let ((date (calendar-cursor-to-date)))
      (when date
        (setq date (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date)))
        (kill-new (format-time-string calendar-copy-as-kill-format date))))))

(defun my-calendar-mode-hook-fun ()
  "Let \[kill-ring-save] copy the date at point if region is not active."
  (local-set-key [remap kill-ring-save] #'calendar-copy-as-kill))

(add-hook 'calendar-mode-hook #'my-calendar-mode-hook-fun)
Tobias
  • 32,569
  • 1
  • 34
  • 75
  • Wonderful, thank you for this great answer. Let me wait for a while for possible alternative solutions before accepting yours. – Name Jun 12 '18 at 14:42