1 ;;; howm-date.el --- Wiki-like note-taking tool
2 ;;; Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2015, 2016
3 ;;; HIRAOKA Kazuyuki <khi@users.sourceforge.jp>
4 ;;; $Id: howm-date.el,v 1.35 2011-12-31 15:07:29 hira Exp $
6 ;;; This program is free software; you can redistribute it and/or modify
7 ;;; it under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 1, or (at your option)
11 ;;; This program is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; The GNU General Public License is available by anonymouse ftp from
17 ;;; prep.ai.mit.edu in pub/gnu/COPYING. Alternately, you can write to
18 ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139,
20 ;;--------------------------------------------------------------------
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;; insert & action-lock
28 (defvar howm-insert-date-pass-through nil)
30 (defun howm-insert-date ()
32 (let ((date (format-time-string howm-date-format)))
33 (insert (format howm-insert-date-format date))
34 (howm-action-lock-date date t howm-insert-date-future)))
36 (defun howm-insert-dtime ()
38 (insert (format-time-string howm-dtime-format)))
40 ;; Sorry for ugly behavior around "new" to keep backward compatibility.
41 (defun howm-action-lock-date (date &optional new future-p)
42 (let* ((pass-through (and new howm-insert-date-pass-through))
43 (prompt (howm-action-lock-date-prompt date new pass-through))
44 (immediate-chars (if pass-through "" "."))
45 (c (howm-read-string prompt immediate-chars "+-~0123456789"
46 pass-through pass-through)))
48 ((null c) nil) ;; pass through
52 (howm-action-lock-date-search date)))
53 ((string-match "^[-+][0-9]+$" c)
54 (howm-action-lock-date-shift (string-to-number c) date))
55 ((string-match "^[0-9]+$" c)
56 (howm-action-lock-date-set c date future-p))
57 ((string-match "^~\\([0-9]+\\)$" c)
58 (howm-action-lock-date-repeat (match-string-no-properties 1 c) date))
59 ((string-match "^[.]$" c)
60 (howm-datestr-replace (howm-time-to-datestr)))
61 ((and (string-match "^[-+~]$" c) pass-through)
63 (t (error (format "Can't understand %s." c))))))
65 (defun howm-action-lock-date-prompt (date new pass-through)
66 (let* ((dow (howm-datestr-day-of-week date))
67 (common-help "+num(shift), yymmdd(set), ~yymmdd(repeat)")
68 (today-help ", .(today)")
69 (help (cond ((and new pass-through)
71 ((and new (not pass-through))
72 (concat "RET(ok), " common-help today-help))
74 (concat "RET(list), " common-help today-help))
76 (error "Can't happen.")))))
77 (format "[%s] %s: " dow help)))
79 (defvar howm-date-current nil)
80 (make-variable-buffer-local 'howm-date-current)
82 (defun howm-action-lock-date-search (date)
83 (howm-set-command 'howm-action-lock-date-search)
86 (howm-action-lock-forward-escape)
87 (setq howm-date-current date)))
89 (defun howm-search-today ()
93 (defun howm-search-past (&optional days-before)
95 (let* ((n (or days-before 0))
96 (today (format-time-string howm-date-format))
97 (target (howm-datestr-shift today 0 0 (- n))))
98 (howm-action-lock-date-search target)))
100 (defun howm-action-lock-date-shift (n date)
101 (howm-datestr-replace (howm-datestr-shift date 0 0 n)))
103 (defun howm-action-lock-date-set (val date &optional future-p)
104 (howm-datestr-replace (howm-datestr-expand val date future-p)))
106 (defvar howm-action-lock-date-repeat-max 200)
107 (defun howm-action-lock-date-repeat (until date)
108 (let ((every (read-from-minibuffer "Every? [RET(all), num(days), w(week), m(month), y(year)] ")))
109 (let ((max-d (howm-datestr-expand until date t))
110 (offset-y (if (string= every "y") 1 0))
111 (offset-m (if (string= every "m") 1 0))
112 (offset-d (or (cdr (assoc every '(("y" . 0) ("m" . 0) ("w" . 7))))
113 (max (string-to-number every) 1))))
119 (setq d (howm-datestr-shift d offset-y offset-m offset-d))
120 (howm-datestr<= d max-d))
121 (when (and check (>= i howm-action-lock-date-repeat-max))
122 (if (y-or-n-p (format "More than %d lines. Continue? " i))
124 (throw 'too-many nil)))
125 (howm-duplicate-line)
126 (howm-datestr-replace d)
127 (setq i (+ i 1))))))))
129 (defun howm-make-datestr (y m d)
130 (let ((ti (encode-time 0 0 0 d m y)))
131 (format-time-string howm-date-format ti)))
133 (defun howm-datestr-parse (date)
134 (string-match howm-date-regexp date)
135 (mapcar (lambda (pos)
136 (string-to-number (match-string-no-properties pos date)))
137 (list howm-date-regexp-year-pos
138 howm-date-regexp-month-pos
139 howm-date-regexp-day-pos)))
141 (defun howm-datestr-to-time (date)
142 (let* ((ymd (howm-datestr-parse date))
146 (encode-time 0 0 0 d m y)))
148 (defun howm-time-to-datestr (&optional time)
149 (let ((x (decode-time time)))
150 (howm-make-datestr (nth 5 x) (nth 4 x) (nth 3 x))))
152 (defun howm-datestr-day-of-week (date)
153 (format-time-string "%a" (howm-datestr-to-time date)))
155 (defun howm-datestr-expand (date base &optional future-p)
157 (howm-datestr-expand-future date base)
158 (howm-datestr-expand-general date base future-p)))
160 (defun howm-datestr-expand-future (date base)
161 (let ((raw (howm-datestr-expand-general date base nil))
162 (future (howm-datestr-expand-general date base t)))
163 (when (not (string= raw future))
164 (message "Future date"))
167 (defun howm-datestr-expand-general (date base &optional future-p)
168 (let* ((base-ymd (howm-datestr-parse base))
169 (nval (format "%8s" date))
170 (given-ymd-str (mapcar (lambda (r)
171 (substring nval (car r) (cadr r)))
172 '((0 4) (4 6) (6 8))))
173 (ys (car given-ymd-str))
174 (ms (cadr given-ymd-str))
175 (ds (cl-caddr given-ymd-str)))
176 (when (string-match "^ +0+$" ys)
178 (let* ((given-ymd (mapcar #'string-to-number (list ys ms ds)))
179 (carry nil) ;; to force future date
180 (dmy (cl-mapcar (lambda (ox nx)
182 (when (and carry (= nx 0))
190 (reverse base-ymd) (reverse given-ymd)))
194 (howm-make-datestr (if (<= y 99) (+ y 2000) y) m d))))
196 (defun howm-datestr-shift (date y m d)
197 (let* ((ymd (howm-datestr-parse date))
201 (howm-make-datestr (+ oy y) (+ om m) (+ od d))))
203 (defun howm-datestr<= (date1 date2)
204 (or (string< date1 date2)
205 (string= date1 date2)))
207 (defun howm-datestr-replace (date)
209 (while (not (looking-at howm-date-regexp))
211 (replace-match date t t)
214 (defun howm-duplicate-line ()
215 (let ((c (current-column))
216 (s (buffer-substring (line-beginning-position) (line-end-position))))
221 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
222 ;; search for next/previous date
224 (defvar howm-date-forward-ymd-msg "Searching %s...")
225 (defvar howm-date-forward-ymd-limit 35)
226 (defun howm-date-forward-ymd (y m d)
227 (when (not howm-date-current)
228 (error "Not in date search."))
229 (let* ((new-date (howm-datestr-shift howm-date-current y m d))
231 (step (if (> (+ y m d) 0) +1 -1))
235 (when (howm-action-lock-date-search new-date)
237 (< c howm-date-forward-ymd-limit))
238 (setq new-date (howm-datestr-shift new-date 0 0 step))
240 (when howm-date-forward-ymd-msg
241 (format howm-date-forward-ymd-msg new-date)))
242 (error "Not found within %d days." howm-date-forward-ymd-limit))
243 (when (not (eq (current-buffer) b))
244 (with-current-buffer b
245 (howm-view-kill-buffer)))
246 (howm-view-summary-check t))))
248 (defmacro howm-date-defun-f/b (func y m d)
249 `(defun ,func (&optional k)
252 (howm-date-forward-ymd ,y ,m ,d))))
254 (howm-date-defun-f/b howm-date-forward 0 0 n)
255 (howm-date-defun-f/b howm-date-forward-month 0 n 0)
256 (howm-date-defun-f/b howm-date-forward-year n 0 0)
257 (howm-date-defun-f/b howm-date-backward 0 0 (- n))
258 (howm-date-defun-f/b howm-date-backward-month 0 (- n) 0)
259 (howm-date-defun-f/b howm-date-backward-year (- n) 0 0)
261 (let ((m howm-view-summary-mode-map))
262 (define-key m "+" 'howm-date-forward)
263 (define-key m "-" 'howm-date-backward)
264 (define-key m ")" 'howm-date-forward)
265 (define-key m "(" 'howm-date-backward)
266 (define-key m "}" 'howm-date-forward-month)
267 (define-key m "{" 'howm-date-backward-month)
268 (define-key m "]" 'howm-date-forward-year)
269 (define-key m "[" 'howm-date-backward-year)
272 ;;; howm-date.el ends here