Max Nikulin <maniku...@gmail.com> writes: >> + (defmacro org-encode-time (&rest time) >> + (pcase (length time) ; Emacs-29 since d75e2c12eb >> + (1 `(encode-time ,@time)) >> + ((or 6 9) `(encode-time (list ,@time))) >> + (_ (error "`org-encode-time' may be called with 1, 6, or 9 >> arguments but %d given" >> + (length time))))) > > Should it be something like the following? > > (signal 'wrong-type-argument (list '(1 6 9) (length time))) > > or even > > (signal 'wrong-type-argument > (list '(lambda (n-args) (memq n-args) '(1 6 9)) (length time))) > > Usually "wrong type argument" errors give no clue even related to called > function til enabling enter debugger on error and realizing how to > reproduce the problem.
The current error is fine. I'd rather propose Emacs to change the "wrong type argument" message to mention the function name. >> + ;; In Emacs-27 and Emacs-28 `encode-time' does not support 6 >> elements >> + ;; list argument so `org-encode-time' can not be outside of >> `pcase'. >> + (pcase-let >> + ((`(,_ ,_ ,_ ,d ,m ,y ,dow . ,_) (decode-time start))) >> + (pcase step >> + (`day (org-encode-time 0 0 org-extend-today-until (1+ d) >> m y)) >> + (`week >> + (let ((offset (if (= dow week-start) 7 >> + (mod (- week-start dow) 7)))) >> + (org-encode-time 0 0 org-extend-today-until (+ d >> offset) m y))) >> + (`semimonth (org-encode-time 0 0 0 >> + (if (< d 16) 16 1) >> + (if (< d 16) m (1+ m)) y)) >> + (`month (org-encode-time 0 0 0 month-start (1+ m) y)) >> + (`year (org-encode-time 0 0 org-extend-today-until 1 1 >> (1+ y)))))) > > I do not like repeating of `org-encode-time' but do not see another way > till Emacs-29 will become the lowest supported version. This is fine. AFAIK, other parts of time handling code is full of conds and pcases. >> + (org-encode-time >> + (apply #'list >> + (or (car time0) 0) >> + (+ (if (eq timestamp? 'minute) n 0) (nth 1 time0)) >> + (+ (if (eq timestamp? 'hour) n 0) (nth 2 time0)) >> + (+ (if (eq timestamp? 'day) n 0) (nth 3 time0)) >> + (+ (if (eq timestamp? 'month) n 0) (nth 4 time0)) >> + (+ (if (eq timestamp? 'year) n 0) (nth 5 time0)) >> + (nthcdr 6 time0)))) >> (when (and (memq timestamp? '(hour minute)) >> extra >> (string-match "-\\([012][0-9]\\):\\([0-5][0-9]\\)" extra)) > > I am tempting to write something like > > (let* ((ts (copy-sequence time0)) > (ord (memq timestamp? '(year month day hour minute))) > (field (and ord (nthcdr (length ord) ts)))) > (when field > (setcar field (+ (car field) n))) > (org-encode-time ts)) > > but I am afraid it will make the code rather obscure. Yes, the second version is rather hard to understand. The proper solution would be writing (or using) some high-level time handling library and then using it in Org. Then, we would not need to deal with low-level time representations so frequently. >> + (org-encode-time >> + (append '(0) >> + (mapcar >> + (lambda (prop) (or (org-element-property prop timestamp) 0)) >> + (if end '(:minute-end :hour-end :day-end :month-end :year-end) >> + '(:minute-start :hour-start :day-start :month-start >> + :year-start))) >> + '(nil -1 nil)))) >> >> (defun org-timestamp-has-time-p (timestamp) >> "Non-nil when TIMESTAMP has a time specified." > > Hardly may be considered as an example of elegant code. It is ok. You also could do it as `(0 ,@(mapcar (lambda (prop) ...) (if ...)) nil -1 nil) AFAIK, there is nothing much you can improve further without using function composition from dash.el. Best, Ihor