0

I want to write a mode for ledger. The first thing I want to do is to be able to insert date stamps. There is a very nice mode in Emacs called calendar. I want to be able to select a date within it.

How do I register a call-back or something that will be executed when the user has select a date (by pressing enter or any other way that calendar mode already uses).

Drew
  • 75,699
  • 9
  • 109
  • 225
Jenia Ivanov
  • 427
  • 4
  • 14
  • Check out the function `org-read-date` -- user selects date on calendar and the date is automatically inserted and the calendar buffer closes. – lawlist Aug 27 '15 at 02:02
  • yea but I'll have to enable org-mode. I prefer to do it using the basic elisp functionality like `calendar` and some other function that will allow me to simply read the user's selection. – Jenia Ivanov Aug 27 '15 at 12:16
  • It is not always necessary to enable a major mode in the working buffer to use a function from a particular library. In particular, you can evaluate `(require 'org) (org-read-date)` in your `*Scratch*` buffer and it works no matter what major-mode that buffer is in. It is only necessary to `require` a library one time per session, and can be placed at the top of the library you are building. – lawlist Aug 27 '15 at 15:04
  • There is also a variable to control whether the calendar buffer pops open or not: `org-read-date-popup-calendar`. You can certainly reinvent the wheel, however, my example was essentially mentioned to raise the *strong* possibility that the features you are contemplating creating have already been invented and just need to be custom tailored to suit your needs. – lawlist Aug 27 '15 at 15:12
  • 4
    Possible duplicate of [How do I pick a date in my new major mode?](http://emacs.stackexchange.com/questions/17923/how-do-i-pick-a-date-in-my-new-major-mode) – lawlist Nov 07 '15 at 22:08
  • 1
    See here: [inserting a calendar date at point](https://emacs.stackexchange.com/questions/42529/insert-date-using-a-calendar) – RichieHH Feb 09 '20 at 22:26

1 Answers1

0

My solution is

(defvar *calendar-entry-buffer* nil)

(defun select-date ()
  (interactive)
  (destructuring-bind (month day year) (calendar-cursor-to-date)
    (if *calendar-entry-buffer*
        (progn
          (save-current-buffer
            (set-buffer *calendar-entry-buffer*)
            (insert (format "%d-%02d-%02d\n" year month day)))
          (setf *calendar-entry-buffer* nil)
          (calendar-exit)))))


(define-key calendar-mode-map (kbd "RET") 'select-date)

(defun insert-date-at-point ()
  (interactive)
  (setf *calendar-entry-buffer* (current-buffer))
  (calendar))
Faried Nawaz
  • 191
  • 7