aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--emacs/.emacs.d/init/ycp-org.el7
-rw-r--r--emacs/.emacs.d/lisp/org-nldate/nldate.el437
-rw-r--r--emacs/.emacs.d/lisp/org-nldate/org-nldate.el214
3 files changed, 657 insertions, 1 deletions
diff --git a/emacs/.emacs.d/init/ycp-org.el b/emacs/.emacs.d/init/ycp-org.el
index 3774b35..f6b9c59 100644
--- a/emacs/.emacs.d/init/ycp-org.el
+++ b/emacs/.emacs.d/init/ycp-org.el
@@ -468,7 +468,6 @@
(my-keybind global-map
"<f6>" #'my-org-open-or-cycle-notes)
(my-keybind org-mode-map
- "C-c 1" #'my-org-insert-date-range
"C-c ns" #'my-org-substitute-gnus-link-after-archiving
"C-x w" #'my-org-copy-link-at-point
"C-'" #'my-org-store-link-and-return
@@ -544,5 +543,11 @@
(with-eval-after-load 'nov
(org-remark-nov-mode +1)))
+(my-package org-nldate
+ (:delay 60)
+ (my-keybind org-mode-map
+ "C-c 1" #'org-nldate-time-stamp)
+ )
+
(provide 'ycp-org)
;;; ycp-org.el ends here
diff --git a/emacs/.emacs.d/lisp/org-nldate/nldate.el b/emacs/.emacs.d/lisp/org-nldate/nldate.el
new file mode 100644
index 0000000..2f14709
--- /dev/null
+++ b/emacs/.emacs.d/lisp/org-nldate/nldate.el
@@ -0,0 +1,437 @@
+;;; nldate.el --- Parse natural-language dates and recurrences -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2026 Free Software Foundation.
+
+;; Version: 0.3
+;; Package-Requires: ((emacs "28.1") (peg "1.0"))
+;; Keywords: calendar
+
+;;; Commentary:
+
+;; `nldate-parse' turns an English description of dates into plain data:
+;;
+;; (nldate-parse "Friday to Sunday from 11am to 10pm over the weekends
+;; of 13-15 and 20-22 November 2026")
+;; => (:dates ((11 13 2026) (11 14 2026) (11 15 2026)
+;; (11 20 2026) (11 21 2026) (11 22 2026))
+;; :time (660 . 1320))
+;;
+;; (nldate-parse "last Saturday of each month")
+;; => (:recurrence (:float t 6 -1) :time nil)
+;;
+;; Dates are calendar.el's (MONTH DAY YEAR) lists; times are minutes past
+;; midnight. `nldate-diary-sexp' turns a recurrence into a diary sexp
+;; such as (diary-float t 6 -1), with date arguments ordered according to
+;; `calendar-date-style'. Nothing here depends on Org; see org-nldate.el
+;; for Org timestamps.
+;;
+;; The input is parsed with a PEG grammar (see `nldate--parse') into a
+;; flat list of items; words the grammar does not know are skipped.
+;; `nldate-parse' then interprets those items.
+
+;; TODO: A diary front end, e.g. `nldate-diary-insert-entry', which reads
+;; a description in the minibuffer and adds it to `diary-file' like
+;; `diary-insert-entry': one "Nov 13, 2026 11:00-22:00 TEXT" line per
+;; date (using `calendar-date-string'), or "%%(SEXP) TEXT" for a
+;; recurrence.
+
+;;; Code:
+
+(require 'calendar)
+(require 'cl-lib)
+(require 'peg)
+(require 'subr-x)
+
+(defconst nldate--weekdays
+ '(("sunday" . 0) ("sun" . 0) ("monday" . 1) ("mon" . 1)
+ ("tuesday" . 2) ("tues" . 2) ("tue" . 2) ("wednesday" . 3) ("wed" . 3)
+ ("thursday" . 4) ("thurs" . 4) ("thur" . 4) ("thu" . 4)
+ ("friday" . 5) ("fri" . 5) ("saturday" . 6) ("sat" . 6)))
+
+(defconst nldate--months
+ '(("january" . 1) ("jan" . 1) ("february" . 2) ("feb" . 2)
+ ("march" . 3) ("mar" . 3) ("april" . 4) ("apr" . 4) ("may" . 5)
+ ("june" . 6) ("jun" . 6) ("july" . 7) ("jul" . 7) ("august" . 8)
+ ("aug" . 8) ("september" . 9) ("sept" . 9) ("sep" . 9)
+ ("october" . 10) ("oct" . 10) ("november" . 11) ("nov" . 11)
+ ("december" . 12) ("dec" . 12)))
+
+;;;; Helpers
+
+(defun nldate--lookup (word table)
+ "Look WORD up in TABLE, also trying it without a plural \"s\"."
+ (or (cdr (assoc word table))
+ (and (string-suffix-p "s" word)
+ (cdr (assoc (substring word 0 -1) table)))))
+
+(defun nldate--word-in-p (table)
+ "Non-nil if the word at point is in TABLE. Used as a PEG guard."
+ (and (looking-at "[a-z]+")
+ (nldate--lookup (match-string 0) table)))
+
+(defun nldate--today () (calendar-absolute-from-gregorian (calendar-current-date)))
+(defun nldate--abs (m d y) (calendar-absolute-from-gregorian (list m d y)))
+(defun nldate--greg (abs) (calendar-gregorian-from-absolute abs))
+(defun nldate--dow (abs) (calendar-day-of-week (nldate--greg abs)))
+
+(defun nldate--ep (abs)
+ "Endpoint (DAY MONTH YEAR) for absolute date ABS."
+ (pcase-let ((`(,m ,d ,y) (nldate--greg abs))) (list d m y)))
+
+(defun nldate--valid-p (m d y)
+ (and (<= 1 m 12) (<= 1 d (calendar-last-day-of-month m y))))
+
+(defun nldate--weekday-span (a b)
+ "Weekdays from A to B inclusive, wrapping past Saturday."
+ (let (r (i a))
+ (while (progn (push i r) (/= i b)) (setq i (% (1+ i) 7)))
+ (nreverse r)))
+
+(defun nldate--hm (h m ampm)
+ "Minutes past midnight for H:M with AMPM (nil, `am' or `pm')."
+ (when (eq ampm 'pm) (unless (= h 12) (setq h (+ h 12))))
+ (when (and (eq ampm 'am) (= h 12)) (setq h 0))
+ (unless (and (< h 24) (< m 60)) (user-error "Invalid time %d:%02d" h m))
+ (+ (* 60 h) m))
+
+(defun nldate--bare-start (h end)
+ "Start time for bare hour H before END, as in \"7-10pm\" or \"11-10pm\"."
+ (if (and (< h 12) (>= end 720) (<= (* 60 (+ h 12)) end))
+ (* 60 (+ h 12))
+ (nldate--hm h 0 nil)))
+
+;;;; Grammar
+
+(defun nldate--parse (string)
+ "Parse STRING into a list of items.
+Items are (:time START END), (:wd DAYS), (:except DAYS), (:filter DAYS),
+\(:date EP), (:range EP EP), (:float MONTH WEEKDAY N), (:monthday DAY),
+\(:cycle DAYS) and (:every). EP is (DAY MONTH YEAR), MONTH and YEAR
+possibly nil."
+ (with-temp-buffer
+ (insert (replace-regexp-in-string "[–—]" "-" (downcase string)))
+ (goto-char (point-min))
+ (with-peg-rules
+ ((top (* [space]) (list (* (and item (* [space])))) (not (any))) ; (eob) upsets the byte compiler
+ (item (or rec-nth rec-monthday rec-cycle rec-every
+ time-span time-single
+ wd-range except wd-keyword
+ between date-term wd-single
+ skip))
+ (sp (+ [space]))
+ (s (* [space]))
+ (end (not [a-z 0-9]))
+ (skip (or (+ [a-z 0-9]) (any)))
+
+ ;; Words
+ (weekday (guard (nldate--word-in-p nldate--weekdays))
+ (substring (+ [a-z]))
+ `(w -- (nldate--lookup w nldate--weekdays)))
+ (month (guard (nldate--word-in-p nldate--months))
+ (substring (+ [a-z])) (opt ".")
+ `(w -- (nldate--lookup w nldate--months)))
+ (ordinal (or (and "second" sp "last" `(-- -2))
+ (and "penultimate" `(-- -2))
+ (and "first" `(-- 1)) (and "second" `(-- 2))
+ (and "third" `(-- 3)) (and "fourth" `(-- 4))
+ (and "fifth" `(-- 5)) (and "last" `(-- -1))
+ (and (substring [1-5]) (or "st" "nd" "rd" "th")
+ `(n -- (string-to-number n))))
+ end)
+ (count (or (and (substring (+ [0-9])) `(n -- (string-to-number n)))
+ (and (or "a" "one") end `(-- 1)) (and "two" end `(-- 2))
+ (and "three" end `(-- 3)) (and "other" end `(-- 2))))
+ (unit (or (and "day" `(-- 1)) (and "week" `(-- 7))
+ (and "fortnight" `(-- 14)))
+ (opt "s") end)
+ (each (or "each" "every") end)
+
+ ;; Recurrences
+ (rec-nth ordinal sp weekday sp (or "of" "in") sp
+ (or (and (or each (and "the" end)) s "month" end `(-- t))
+ (and month (opt sp each s "year" end)))
+ `(n w m -- (list :float m w n)))
+ (rec-monthday (opt "the" sp) day sp "of" sp each s "month" end
+ `(d -- (list :monthday d)))
+ (rec-cycle (or (and "every" sp (or (and count sp unit `(n u -- (* n u)))
+ unit))
+ (and "daily" end `(-- 1)) (and "weekly" end `(-- 7))
+ (and "fortnightly" end `(-- 14)))
+ `(n -- (list :cycle n)))
+ (rec-every "every" end `(-- (list :every)))
+
+ ;; Times
+ (hour (substring [0-9] (opt [0-9])) `(h -- (string-to-number h)))
+ (ampm (or (and "am" `(-- 'am)) (and "pm" `(-- 'pm))) end)
+ (time (or (and "noon" end `(-- 720))
+ (and "midday" end `(-- 720))
+ (and "midnight" end `(-- 0))
+ (and hour [":."] (substring [0-9] [0-9]) s (or ampm (and end `(-- nil)))
+ `(h m a -- (nldate--hm h (string-to-number m) a)))
+ (and hour s ampm `(h a -- (nldate--hm h 0 a)))
+ (and hour "h" end `(h -- (nldate--hm h 0 nil)))))
+ (time-sep s (or "-" (and (or "to" "till" "until") end)) s)
+ (time-span (or (and time time-sep time)
+ (and hour time-sep time
+ `(h e -- (nldate--bare-start h e) e)))
+ `(a b -- (list :time a b)))
+ (time-single time `(a -- (list :time a nil)))
+
+ ;; Weekdays
+ (wd-sep s (or "-" (and (or "to" "through" "thru" "till" "until") end)) s)
+ (wd-range weekday wd-sep weekday
+ `(a b -- (list :wd (nldate--weekday-span a b))))
+ (except (or "except" "excluding" (and "but" sp "not") "not") sp
+ (opt "on" sp)
+ (or (and weekday wd-sep weekday `(a b -- (nldate--weekday-span a b)))
+ (and weekday `(w -- (list w))))
+ `(ds -- (list :except ds)))
+ (wd-keyword (or (and "weekends" sp "of" end) ; filler, pushes nothing
+ (and "weekdays" end `(-- (list :filter '(1 2 3 4 5))))
+ (and "weekend" (opt "s") end `(-- (list :filter '(6 0))))))
+ (wd-single weekday `(w -- (list :wd (list w))))
+
+ ;; Dates
+ (day (substring [0-9] (opt [0-9])) (opt (or "st" "nd" "rd" "th"))
+ (not [":."] [0-9]) (not s ampm) end
+ `(d -- (string-to-number d)))
+ (year (substring [0-9] [0-9] [0-9] [0-9]) end `(y -- (string-to-number y)))
+ (short-year (substring [0-9] [0-9]) end `(y -- (+ 2000 (string-to-number y))))
+ (no-value `(-- nil))
+ ;; A weekday in front of a date, as in "Monday 4th", is checked
+ ;; against it rather than used as a filter.
+ (endpoint (or (and weekday s (opt "," s) plain-endpoint
+ `(w ep -- (append ep (list w))))
+ plain-endpoint))
+ (plain-endpoint
+ (or (and year "-" (substring [0-9] (opt [0-9])) "-" (substring [0-9] (opt [0-9]))
+ end `(y m d -- (list (string-to-number d) (string-to-number m) y)))
+ ;; Day/month[/year], British order.
+ (and (substring [0-9] (opt [0-9])) "/" (substring [0-9] (opt [0-9]))
+ (or (and "/" (or year short-year)) (and end no-value))
+ `(d m y -- (list (string-to-number d) (string-to-number m) y)))
+ (and "today" end `(-- (nldate--ep (nldate--today))))
+ (and "tomorrow" end `(-- (nldate--ep (1+ (nldate--today)))))
+ (and "in" sp count sp unit
+ `(n u -- (nldate--ep (+ (nldate--today) (* n u)))))
+ ;; "13 November 2026", "13th of Nov", "13"
+ (and day (or (and s (opt "of" sp) month) no-value)
+ (or (and s (opt "," s) year) no-value)
+ `(d m y -- (list d m y)))
+ ;; "November 13, 2026"
+ (and month s day (or (and s (opt "," s) year) no-value)
+ `(m d y -- (list d m y)))))
+ (date-sep s (or "-" (and (or "to" "till" "until" "through" "thru") end)) s)
+ (between "between" sp endpoint sp "and" sp endpoint
+ `(a b -- (list :range a b)))
+ (date-term (or (and endpoint date-sep endpoint `(a b -- (list :range a b)))
+ (and endpoint `(a -- (list :date a))))))
+ (car (peg-run (peg top)
+ (lambda (_) (user-error "Cannot parse \"%s\"" string)))))))
+
+;;;; Interpretation
+
+(defun nldate--fill (eps)
+ "Fill missing months and years in endpoints EPS from the right, in place.
+A year still missing afterwards means \"the next one\"."
+ (let (m y)
+ (dolist (ep (reverse eps))
+ (if (nth 1 ep) (setq m (nth 1 ep)) (setf (nth 1 ep) m))
+ (if (nth 2 ep) (setq y (nth 2 ep)) (setf (nth 2 ep) y))
+ (unless (nth 1 ep)
+ (user-error "No month given for day %d" (car ep))))))
+
+(defun nldate--resolve (ep &optional after)
+ "Absolute day of endpoint EP; when the year is unknown pick one >= AFTER."
+ (pcase-let ((`(,d ,m ,y . ,_) ep))
+ (if y
+ (if (nldate--valid-p m d y)
+ (nldate--abs m d y)
+ (user-error "Invalid date: %d/%d/%d" d m y))
+ (let* ((after (or after (nldate--today)))
+ (y (nth 2 (nldate--greg after))))
+ (while (not (and (nldate--valid-p m d y)
+ (>= (nldate--abs m d y) after)))
+ (setq y (1+ y))
+ (when (> y (+ 10 (nth 2 (nldate--greg after))))
+ (user-error "Invalid date: %d/%d" d m)))
+ (nldate--abs m d y)))))
+
+(defun nldate--check-weekday (ep abs)
+ "Signal an error unless ABS falls on endpoint EP's weekday, if it has one."
+ (let ((w (nth 3 ep)))
+ (when (and w (/= w (nldate--dow abs)))
+ (user-error "%s is a %s, not a %s"
+ (calendar-date-string (nldate--greg abs) t t)
+ (calendar-day-name (nldate--greg abs))
+ (aref calendar-day-name-array w)))))
+
+(defun nldate--terms (items)
+ "Resolve the date items in ITEMS into a list of (START . END) absolute days."
+ (let ((terms (cl-remove-if-not (lambda (i) (memq (car i) '(:date :range))) items)))
+ (let* ((eps (mapcan (lambda (i) (copy-sequence (cdr i))) terms))
+ (given (mapcar (lambda (ep) (cons ep (nth 2 ep))) eps)))
+ (nldate--fill eps)
+ (mapcar
+ (lambda (i)
+ (let* ((ea (nth 1 i)) (eb (nth 2 i))
+ (a (nldate--resolve ea))
+ (b (if eb (nldate--resolve eb a) a)))
+ ;; "28 Dec - 3 Jan 2027": the start year was copied from the end.
+ (when (and (< b a) (null (cdr (assq ea given))))
+ (setf (nth 2 ea) (1- (nth 2 ea))
+ a (nldate--resolve ea)))
+ (when (< b a) (user-error "Range ends before it starts"))
+ (nldate--check-weekday ea a)
+ (when eb (nldate--check-weekday eb b))
+ (cons a b)))
+ terms))))
+
+;;;; Diary sexps
+
+(defun nldate--date-args (date)
+ "Return calendar DATE's parts ordered for diary sexps per `calendar-date-style'."
+ (pcase-let ((`(,m ,d ,y) date))
+ (pcase calendar-date-style
+ ('european (list d m y))
+ ('iso (list y m d))
+ (_ (list m d y)))))
+
+(defun nldate--wd-cond (days)
+ (if (cdr days)
+ `(memq (calendar-day-of-week date) ',days)
+ `(= (calendar-day-of-week date) ,(car days))))
+
+(defun nldate--cyclic (n date)
+ `(diary-cyclic ,n ,@(nldate--date-args date)))
+
+(defun nldate--block (a b)
+ `(diary-block ,@(nldate--date-args a) ,@(nldate--date-args b)))
+
+(defun nldate-diary-sexp (recurrence)
+ "Return a diary sexp for RECURRENCE, as returned by `nldate-parse'.
+Date arguments follow `calendar-date-style'."
+ (pcase recurrence
+ (`(:float ,m ,w ,n) `(diary-float ,m ,w ,n))
+ (`(:monthday ,d) `(diary-date ,@(nldate--date-args (list t d t))))
+ (`(:cyclic ,n ,start ,end)
+ (if end `(and ,(nldate--cyclic n start) ,(nldate--block start end))
+ (nldate--cyclic n start)))
+ (`(:weekly ,days ,start ,end)
+ (let ((wc (nldate--wd-cond days)))
+ (cond
+ ((and start end) `(and ,(nldate--block start end) ,wc))
+ ((and start (null (cdr days)))
+ (let ((a (calendar-absolute-from-gregorian start)))
+ (nldate--cyclic 7 (nldate--greg
+ (+ a (mod (- (car days) (nldate--dow a)) 7))))))
+ (start `(and (>= (calendar-absolute-from-gregorian date)
+ ,(calendar-absolute-from-gregorian start))
+ ,wc))
+ (t wc))))
+ (_ (error "Unknown recurrence %S" recurrence))))
+
+;;;; Entry points
+
+(defvar nldate-warning-function
+ (lambda (message) (display-warning 'nldate message))
+ "Function called with a message when `nldate-parse' finds something odd.
+For example, a weekday that does not match the date given. Bind it to
+collect the messages instead, as a live preview does.")
+
+
+(defun nldate--recurrence (rec terms days)
+ "Recurrence for item REC, bounded by the first of TERMS, on weekdays DAYS."
+ (let* ((term (car terms))
+ (a (and term (nldate--greg (car term))))
+ (b (and term (/= (car term) (cdr term)) (nldate--greg (cdr term)))))
+ (pcase rec
+ (`(:float . ,_) rec)
+ (`(:monthday . ,_) rec)
+ (`(:cycle ,n) (list :cyclic n (or a (calendar-current-date)) b))
+ (`(:every)
+ (unless days (user-error "\"every\" needs weekdays or a period"))
+ (list :weekly days a b)))))
+
+(defun nldate-format-time (minutes)
+ "Format MINUTES past midnight as HH:MM."
+ (format "%02d:%02d" (/ minutes 60) (% minutes 60)))
+
+(defun nldate--merge-ranges (terms)
+ "Merge overlapping (START . END) absolute-day TERMS into calendar date pairs."
+ (let (merged)
+ (dolist (tm (sort (copy-sequence terms) (lambda (a b) (< (car a) (car b)))))
+ (if (and merged (<= (car tm) (cdar merged)))
+ (setcdr (car merged) (max (cdr tm) (cdar merged)))
+ (push (cons (car tm) (cdr tm)) merged)))
+ (mapcar (lambda (r) (cons (nldate--greg (car r)) (nldate--greg (cdr r))))
+ (nreverse merged))))
+
+(defun nldate-parse (string)
+ "Parse the English date description STRING.
+Return either (:dates DATES :ranges RANGES :time TIME)
+or (:recurrence REC :time TIME).
+
+DATES is a sorted list of calendar dates (MONTH DAY YEAR), every day
+described. RANGES gives the same days as a list of (START . END) date
+pairs, one per date or range in STRING, with overlapping ones merged; a
+single date is (DATE . DATE). RANGES is nil when a weekday filter such
+as \"Friday to Sunday\" or \"weekends\" picked days out of the ranges.
+TIME is nil or (START . END), minutes past midnight, END being nil for
+a single time.
+REC is one of
+ (:float MONTH WEEKDAY N) as for `diary-float', MONTH t for every month
+ (:monthday DAY) that day of every month
+ (:cyclic N START END) every N days from START, until END if non-nil
+ (:weekly WEEKDAYS START END) on WEEKDAYS (0 = Sunday), START and END
+ optional
+with START and END calendar dates or nil. Signal `user-error' if
+STRING cannot be understood."
+ (let* ((items (nldate--parse string))
+ (get (lambda (k) (cl-remove-if-not (lambda (i) (eq (car i) k)) items)))
+ (tm (car (funcall get :time)))
+ (time (and tm (cons (nth 1 tm) (nth 2 tm))))
+ (explicit (delete-dups (mapcan (lambda (i) (copy-sequence (nth 1 i)))
+ (funcall get :wd))))
+ (except (mapcan (lambda (i) (copy-sequence (nth 1 i))) (funcall get :except)))
+ (days (or explicit (nth 1 (car (funcall get :filter)))))
+ (days (if except
+ (cl-set-difference (or days '(0 1 2 3 4 5 6)) except)
+ days))
+ (rec (cl-find-if (lambda (i) (memq (car i) '(:float :monthday :cycle))) items))
+ (rec (or rec (car (funcall get :every))))
+ (terms (nldate--terms items)))
+ (if rec
+ (list :recurrence (nldate--recurrence rec terms days) :time time)
+ (let ((all (sort (delete-dups
+ (mapcan (lambda (tm)
+ (when (> (- (cdr tm) (car tm)) 3660)
+ (user-error "Range too long"))
+ (number-sequence (car tm) (cdr tm)))
+ terms))
+ #'<)))
+ (cond
+ ((and (null all) days)
+ ;; Bare weekdays: next occurrence of each, after today.
+ (let ((tod (nldate--today)))
+ (setq all (sort (mapcar (lambda (w)
+ (+ tod 1 (mod (- w (nldate--dow (1+ tod))) 7)))
+ days)
+ #'<))))
+ (days
+ (when explicit
+ (dolist (e (delete-dups (mapcan (lambda (tm) (list (car tm) (cdr tm))) terms)))
+ (unless (memq (nldate--dow e) days)
+ (funcall nldate-warning-function
+ (format "%s is not one of the weekdays given"
+ (calendar-date-string (nldate--greg e)))))))
+ (setq all (cl-remove-if-not (lambda (d) (memq (nldate--dow d) days)) all))
+ (unless all
+ (user-error "No date in \"%s\" falls on the weekdays given" string))))
+ (unless all (user-error "Could not find any date in \"%s\"" string))
+ (list :dates (mapcar #'nldate--greg all)
+ :ranges (and (null days) (nldate--merge-ranges terms))
+ :time time)))))
+
+(provide 'nldate)
+;;; nldate.el ends here
diff --git a/emacs/.emacs.d/lisp/org-nldate/org-nldate.el b/emacs/.emacs.d/lisp/org-nldate/org-nldate.el
new file mode 100644
index 0000000..dbc2feb
--- /dev/null
+++ b/emacs/.emacs.d/lisp/org-nldate/org-nldate.el
@@ -0,0 +1,214 @@
+;;; org-nldate.el --- Natural-language dates to Org timestamps -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2026 Free Software Foundation.
+
+;; Version: 0.3
+;; Package-Requires: ((emacs "28.1") (org "9.5") (nldate "0.3"))
+;; Keywords: calendar, outlines
+
+;;; Commentary:
+
+;; `org-nldate-time-stamp' reads an English description of dates in the
+;; minibuffer and inserts Org timestamps at point, one per line, e.g.
+;;
+;; Friday to Sunday from 11am to 10pm over the weekends of 13-15 and
+;; 20-22 November 2026
+;;
+;; becomes six timestamps <2026-11-13 Fri 11:00-22:00> ... and
+;;
+;; last Saturday of each month -> <%%(diary-float t 6 -1)>
+;; every 10 days from 13 Nov -> <%%(diary-cyclic 10 ...)>
+;;
+;; Parsing is done by nldate.el; this file only formats the result.
+;;
+;; Org before 9.7 does not understand a time inside a diary-sexp
+;; timestamp, so there the time goes at the start of the heading
+;; instead: "* 11:00-22:00 Market". See `org-nldate-time-in-sexp'.
+
+;; TODO: Org has variants of the diary functions that always take
+;; year, month, day in that order whatever `calendar-date-style' says:
+;; `org-cyclic', `org-block', `org-date' and `org-anniversary' (in
+;; org-agenda.el). `diary-cyclic' etc. read their arguments in the
+;; order set by `calendar-date-style', so <%%(diary-cyclic 10 11 13 2026)>
+;; written with the default American style means a different date for
+;; someone using the European style. Emitting <%%(org-cyclic 10 2026 11
+;; 13)> instead would make timestamps portable between setups. That
+;; needs a variant of `nldate-diary-sexp' here, as `diary-float' has no
+;; date arguments and needs no Org counterpart.
+
+;;; Code:
+
+(require 'nldate)
+(require 'org)
+(require 'subr-x)
+
+(defun org-nldate--date-format ()
+ "Org's date-only timestamp format, without brackets."
+ ;; Org 9.7 renamed `org-time-stamp-formats' and dropped the brackets.
+ (string-trim (car (if (boundp 'org-timestamp-formats)
+ org-timestamp-formats
+ org-time-stamp-formats))
+ "[<[]" "[]>]"))
+
+(defun org-nldate--time-string (time)
+ "Org time or time range for TIME, (START . END) as from `nldate-parse'."
+ (and time
+ (concat (nldate-format-time (car time))
+ (and (cdr time) (concat "-" (nldate-format-time (cdr time)))))))
+
+(defun org-nldate--stamp (date time)
+ "Org timestamp for calendar DATE with TIME (a string or nil)."
+ (pcase-let ((`(,m ,d ,y) date))
+ (format "<%s%s>"
+ (format-time-string (org-nldate--date-format) (encode-time 0 0 12 d m y))
+ (if time (concat " " time) ""))))
+
+(defun org-nldate--sexp-stamp (sexp time)
+ "Org diary-sexp timestamp for SEXP with TIME (a string or nil)."
+ (format "<%%%%%s%s>" (prin1-to-string sexp) (if time (concat " " time) "")))
+
+;;;; Times in diary-sexp timestamps
+
+(defcustom org-nldate-time-in-sexp 'auto
+ "Whether a time may go inside a diary-sexp timestamp.
+Org 9.7 added timestamps like <%%(diary-float t 6 -1) 11:00-22:00>.
+Older versions do not merely ignore the time: the agenda drops the
+whole entry. They do pick up a time at the start of the heading, so
+when this is nil, `org-nldate-time-stamp' writes the time there.
+
+t means always inside the timestamp, nil always in the heading, and
+`auto' decides by `org-version'."
+ :type '(choice (const :tag "Decide by Org version" auto)
+ (const :tag "Inside the timestamp" t)
+ (const :tag "At the start of the heading" nil))
+ :group 'org-time)
+
+(defun org-nldate--time-in-sexp-p ()
+ (if (eq org-nldate-time-in-sexp 'auto)
+ (version<= "9.7" (org-release))
+ org-nldate-time-in-sexp))
+
+(defun org-nldate--format (result)
+ "Format RESULT of `nldate-parse' as (STAMPS . HEADING-TIME).
+HEADING-TIME is a time string that belongs in the heading instead of
+the timestamp, or nil."
+ (let ((time (org-nldate--time-string (plist-get result :time))))
+ (if-let ((rec (plist-get result :recurrence)))
+ (let ((sexp (nldate-diary-sexp rec)))
+ (if (or (null time) (org-nldate--time-in-sexp-p))
+ (list (list (org-nldate--sexp-stamp sexp time)))
+ (cons (list (org-nldate--sexp-stamp sexp nil)) time)))
+ (let ((ranges (plist-get result :ranges)))
+ (list
+ (if (and ranges (null time))
+ ;; "4th to 9th January": one Org range, <...>--<...>. With a
+ ;; time, as in "13-15 Nov 11am-5pm", the hours are daily, which
+ ;; an Org range cannot say, so each day gets its own timestamp.
+ (mapcar (lambda (r)
+ (if (equal (car r) (cdr r))
+ (org-nldate--stamp (car r) nil)
+ (concat (org-nldate--stamp (car r) nil) "--"
+ (org-nldate--stamp (cdr r) nil))))
+ ranges)
+ (mapcar (lambda (date) (org-nldate--stamp date time))
+ (plist-get result :dates))))))))
+
+(defun org-nldate-parse (string)
+ "Return a list of Org timestamp strings described by STRING.
+A time that cannot go inside a diary-sexp timestamp (see
+`org-nldate-time-in-sexp') is left out."
+ (car (org-nldate--format (nldate-parse string))))
+
+(defconst org-nldate--leading-time-re
+ "\\`[0-9]\\{1,2\\}:[0-9]\\{2\\}\\(?:-[0-9]\\{1,2\\}:[0-9]\\{2\\}\\)?\\(?:\\s-+\\|\\'\\)"
+ "Matches a time or time range at the start of a heading title.")
+
+(defun org-nldate--put-time-in-heading (time)
+ "Put TIME at the start of the current entry's heading title.
+A time already there is replaced. Return nil if there is no heading."
+ (save-excursion
+ (unless (org-before-first-heading-p)
+ (org-back-to-heading t)
+ (let ((title (or (org-get-heading t t t t) "")))
+ (org-edit-headline
+ (string-trim-right
+ (concat time " " (replace-regexp-in-string
+ org-nldate--leading-time-re "" title t t)))))
+ t)))
+
+;;;; Minibuffer preview
+
+(defvar org-nldate-history nil)
+
+(defvar-local org-nldate--preview-overlay nil)
+
+(defun org-nldate--preview-stamps (stamps)
+ "Shorten STAMPS for display: at most three, with a count when more."
+ (if (<= (length stamps) 3)
+ (string-join stamps " ")
+ (format "%s %s ... %s (%d dates)"
+ (nth 0 stamps) (nth 1 stamps) (car (last stamps)) (length stamps))))
+
+(defun org-nldate-preview-text (string)
+ "Text previewing how STRING will be inserted, like Org's date prompt.
+Return nil for blank input."
+ (unless (string-blank-p string)
+ (let* ((warnings ())
+ (nldate-warning-function (lambda (msg) (push msg warnings)))
+ (text
+ (condition-case err
+ (pcase-let ((`(,stamps . ,heading-time)
+ (org-nldate--format (nldate-parse string))))
+ (concat (org-nldate--preview-stamps stamps)
+ (and heading-time
+ (format " (%s goes in the heading)" heading-time))))
+ (error (propertize (error-message-string err) 'face 'shadow)))))
+ (concat "=> " text
+ (mapconcat (lambda (w) (propertize (format " [%s]" w) 'face 'warning))
+ (nreverse warnings) "")))))
+
+(defun org-nldate--preview ()
+ "Show the interpretation of the minibuffer contents after the input."
+ (when org-nldate--preview-overlay
+ (delete-overlay org-nldate--preview-overlay))
+ (when-let ((text (org-nldate-preview-text (minibuffer-contents-no-properties))))
+ (setq org-nldate--preview-overlay (make-overlay (point-max) (point-max)))
+ (setq text (copy-sequence text))
+ (add-face-text-property 0 (length text) 'secondary-selection t text)
+ (overlay-put org-nldate--preview-overlay 'after-string
+ ;; The `cursor' property keeps the cursor at the end of
+ ;; the input, before the preview. Three spaces as in Org.
+ (concat (propertize " " 'cursor t) text))))
+
+(defun org-nldate-read-string (prompt)
+ "Read a date description with PROMPT, previewing it live.
+The preview follows `org-read-date-display-live'."
+ (minibuffer-with-setup-hook
+ (lambda ()
+ (when org-read-date-display-live
+ (add-hook 'post-command-hook #'org-nldate--preview nil t)))
+ (read-string prompt nil 'org-nldate-history)))
+
+;;;; Command
+
+;;;###autoload
+(defun org-nldate-time-stamp (string)
+ "Read a natural-language date description and insert Org timestamps at point.
+Several dates are inserted one per line, aligned with the current column.
+For a recurrence with a time on Org before 9.7, the time goes at the
+start of the heading instead; see `org-nldate-time-in-sexp'."
+ (interactive (list (org-nldate-read-string "Date(s): ")))
+ (pcase-let ((`(,stamps . ,heading-time) (org-nldate--format (nldate-parse string)))
+ (indent (make-string (current-column) ?\s)))
+ (insert (string-join stamps (concat "\n" indent)))
+ (when heading-time
+ (if (org-nldate--put-time-in-heading heading-time)
+ (message "Org %s cannot keep a time in a diary timestamp; put %s in the heading"
+ (org-release) heading-time)
+ (display-warning
+ 'org-nldate
+ (format "Org %s cannot keep a time in a diary timestamp, and there is no heading to put %s in"
+ (org-release) heading-time))))))
+
+(provide 'org-nldate)
+;;; org-nldate.el ends here