1

I want to insert a date in the current buffer.

I know the function calendar-cursor-to-date that when invoked gives back the date at which the cursor is.

But how do I get that info back into my program?

Here is what I've got so far:

(defun insert-new-entry ()
   (interactive)
   (save-excursion
      (goto-char (point-max))
      ;;next comes mostly pseudocode except *calendar-cursor-to-date*

      (calendar)
       (let ((x (bind-key (kdb "enter) 'calendar-mode calendar-cursor-to-date)))
            (insert x))))

So in english it's:

  1. go to the last location in the buffer
  2. open calendar
  3. store in x whatever is returned where enter is pressed
  4. insert it into buffer.

The problem is step 3. How do I store into x whatever is returned from calendar-cursor-to-date?

Thanks

1
  • Thanks for pointing out the interesting function, calendar-cursor-to-date. But I was confused by the documentation, which doesn't clarify that it only works within the calendar buffer. I want a function that will work in any text buffer (not the calendar buffer), and look near the cursor for a date string, and parse and return the date corresponding to that string. See How to parse calendar date or timestamp at cursor from elisp?
    – nealmcb
    Commented Jan 8, 2016 at 16:30

2 Answers 2

3

As stated in a comment of mine underneath a similar / duplicate question by this original poster -- how to detect selection in date mode? -- the function org-read-date is the best choice:

(insert (org-read-date))

The following example function demonstrates proposed usage of org-read-date based on the outline of the original poster:

(defun insert-date-at-eob ()
"Go to the end of the buffer, insert a date using the function `org-read-date',
and return to the original position in the buffer."
(interactive)
  (save-excursion
    (goto-char (point-max))
    (insert (org-read-date))))

For more information on the function org-read-date, type M-x describe-function RET org-read-date RET

0

It's not as clean as I'd like, but it works:

(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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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