Skip to content

Instantly share code, notes, and snippets.

@Superbil
Last active August 29, 2015 14:14
Show Gist options
  • Save Superbil/6e7966e1362fd21c1237 to your computer and use it in GitHub Desktop.
Save Superbil/6e7966e1362fd21c1237 to your computer and use it in GitHub Desktop.
;;; Code:
;;; Copy from https://gist.github.com/kanru/1a7c4dc58fda760b4bb4
(eval-when-compile (require 'cl))
(require 'calendar)
(defcustom insert-date-format "%Y-%m-%d"
"This will use for `insert-date'"
:type 'string
:group 'calendar)
(defun calendar-date->time (calendar-date)
"Convert a date as returned from the calendar to a time"
(encode-time 0 0 0 ; second, minute, hour
(nth 1 calendar-date) ; day
(nth 0 calendar-date) ; month
(nth 2 calendar-date))) ; year
(defun date-from-calendar ()
"Insert date from `calendar', it will use `insert-date-format' to format date-string."
(lexical-let ((date-string nil))
(set-transient-map
(let ((map (make-sparse-keymap)))
(set-keymap-parent map calendar-mode-map)
(define-key map (kbd "RET")
(lambda ()
(interactive)
(switch-to-buffer calendar-buffer)
(setf date-string (format-time-string insert-date-format
(calendar-date->time
(calendar-cursor-to-date t))))
(calendar-exit)
(insert date-string)))
map)
(lambda () (null date-string)))
(save-excursion
(calendar))))
(defun insert-date (prefix)
"Insert the current date. With prefix-argument, use `format-time-string' by variable
`insert-date-format'. With two prefix arguments, use `date-from-calendar' to chose date."
(interactive "P")
(cond
((not prefix)
(insert (format-time-string insert-date-format)))
((equal prefix '(4))
(date-from-calendar))))
(global-set-key (kbd "C-c c d") 'insert-date)
(provide 'init-calendar)
;;; init-calendar.el ends here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment