]> code.delx.au - gnu-emacs/blob - lisp/org/org-agenda.el
a14a38a2f65168ef314a7122f17c4df44a7b7224
[gnu-emacs] / lisp / org / org-agenda.el
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
2
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.15a
10 ;;
11 ;; This file is part of GNU Emacs.
12 ;;
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
26 ;;
27 ;;; Commentary:
28
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
30
31 ;;; Code:
32
33 (require 'org)
34 (eval-when-compile
35 (require 'cl)
36 (require 'calendar))
37
38 (declare-function diary-add-to-list "diary-lib"
39 (date string specifier &optional marker globcolor literal))
40 (declare-function calendar-absolute-from-iso "cal-iso" (date))
41 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
42 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
43 (declare-function calendar-check-holidays "holidays" (date))
44 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
45 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
47 (declare-function calendar-french-date-string "cal-french" (&optional date))
48 (declare-function calendar-goto-date "cal-move" (date))
49 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
50 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
51 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
52 (declare-function calendar-iso-from-absolute "cal-iso" (date))
53 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
54 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
55 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
56 (declare-function org-columns-quit "org-colview" ())
57 (defvar calendar-mode-map)
58
59 ;; Defined somewhere in this file, but used before definition.
60 (defvar org-agenda-buffer-name)
61 (defvar org-agenda-overriding-header)
62 (defvar entry)
63 (defvar date)
64 (defvar org-agenda-undo-list)
65 (defvar org-agenda-pending-undo-list)
66 (defvar original-date) ; dynamically scoped, calendar.el does scope this
67
68 (defcustom org-agenda-confirm-kill 1
69 "When set, remote killing from the agenda buffer needs confirmation.
70 When t, a confirmation is always needed. When a number N, confirmation is
71 only needed when the text to be killed contains more than N non-white lines."
72 :group 'org-agenda
73 :type '(choice
74 (const :tag "Never" nil)
75 (const :tag "Always" t)
76 (number :tag "When more than N lines")))
77
78 (defcustom org-agenda-compact-blocks nil
79 "Non-nil means, make the block agenda more compact.
80 This is done by leaving out unnecessary lines."
81 :group 'org-agenda
82 :type 'boolean)
83
84 (defgroup org-agenda-export nil
85 "Options concerning exporting agenda views in Org-mode."
86 :tag "Org Agenda Export"
87 :group 'org-agenda)
88
89 (defcustom org-agenda-with-colors t
90 "Non-nil means, use colors in agenda views."
91 :group 'org-agenda-export
92 :type 'boolean)
93
94 (defcustom org-agenda-exporter-settings nil
95 "Alist of variable/value pairs that should be active during agenda export.
96 This is a good place to set uptions for ps-print and for htmlize."
97 :group 'org-agenda-export
98 :type '(repeat
99 (list
100 (variable)
101 (sexp :tag "Value"))))
102
103 (defcustom org-agenda-export-html-style ""
104 "The style specification for exported HTML Agenda files.
105 If this variable contains a string, it will replace the default <style>
106 section as produced by `htmlize'.
107 Since there are different ways of setting style information, this variable
108 needs to contain the full HTML structure to provide a style, including the
109 surrounding HTML tags. The style specifications should include definitions
110 the fonts used by the agenda, here is an example:
111
112 <style type=\"text/css\">
113 p { font-weight: normal; color: gray; }
114 .org-agenda-structure {
115 font-size: 110%;
116 color: #003399;
117 font-weight: 600;
118 }
119 .org-todo {
120 color: #cc6666;
121 font-weight: bold;
122 }
123 .org-done {
124 color: #339933;
125 }
126 .title { text-align: center; }
127 .todo, .deadline { color: red; }
128 .done { color: green; }
129 </style>
130
131 or, if you want to keep the style in a file,
132
133 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
134
135 As the value of this option simply gets inserted into the HTML <head> header,
136 you can \"misuse\" it to also add other text to the header. However,
137 <style>...</style> is required, if not present the variable will be ignored."
138 :group 'org-agenda-export
139 :group 'org-export-html
140 :type 'string)
141
142 (defgroup org-agenda-custom-commands nil
143 "Options concerning agenda views in Org-mode."
144 :tag "Org Agenda Custom Commands"
145 :group 'org-agenda)
146
147 (defconst org-sorting-choice
148 '(choice
149 (const time-up) (const time-down)
150 (const category-keep) (const category-up) (const category-down)
151 (const tag-down) (const tag-up)
152 (const priority-up) (const priority-down)
153 (const todo-state-up) (const todo-state-down)
154 (const effort-up) (const effort-down))
155 "Sorting choices.")
156
157 (defconst org-agenda-custom-commands-local-options
158 `(repeat :tag "Local settings for this command. Remember to quote values"
159 (choice :tag "Setting"
160 (list :tag "Any variable"
161 (variable :tag "Variable")
162 (sexp :tag "Value"))
163 (list :tag "Files to be searched"
164 (const org-agenda-files)
165 (list
166 (const :format "" quote)
167 (repeat
168 (file))))
169 (list :tag "Sorting strategy"
170 (const org-agenda-sorting-strategy)
171 (list
172 (const :format "" quote)
173 (repeat
174 ,org-sorting-choice)))
175 (list :tag "Prefix format"
176 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
177 (string))
178 (list :tag "Number of days in agenda"
179 (const org-agenda-ndays)
180 (integer :value 1))
181 (list :tag "Fixed starting date"
182 (const org-agenda-start-day)
183 (string :value "2007-11-01"))
184 (list :tag "Start on day of week"
185 (const org-agenda-start-on-weekday)
186 (choice :value 1
187 (const :tag "Today" nil)
188 (number :tag "Weekday No.")))
189 (list :tag "Include data from diary"
190 (const org-agenda-include-diary)
191 (boolean))
192 (list :tag "Deadline Warning days"
193 (const org-deadline-warning-days)
194 (integer :value 1))
195 (list :tag "Standard skipping condition"
196 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
197 (const org-agenda-skip-function)
198 (list
199 (const :format "" quote)
200 (list
201 (choice
202 :tag "Skiping range"
203 (const :tag "Skip entry" org-agenda-skip-entry-if)
204 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
205 (repeat :inline t :tag "Conditions for skipping"
206 (choice
207 :tag "Condition type"
208 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
209 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
210 (const :tag "scheduled" 'scheduled)
211 (const :tag "not scheduled" 'notscheduled)
212 (const :tag "deadline" 'deadline)
213 (const :tag "no deadline" 'notdeadline))))))
214 (list :tag "Non-standard skipping condition"
215 :value (org-agenda-skip-function)
216 (const org-agenda-skip-function)
217 (sexp :tag "Function or form (quoted!)"))))
218 "Selection of examples for agenda command settings.
219 This will be spliced into the custom type of
220 `org-agenda-custom-commands'.")
221
222
223 (defcustom org-agenda-custom-commands nil
224 "Custom commands for the agenda.
225 These commands will be offered on the splash screen displayed by the
226 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
227
228 (key desc type match settings files)
229
230 key The key (one or more characters as a string) to be associated
231 with the command.
232 desc A description of the command, when omitted or nil, a default
233 description is built using MATCH.
234 type The command type, any of the following symbols:
235 agenda The daily/weekly agenda.
236 todo Entries with a specific TODO keyword, in all agenda files.
237 search Entries containing search words entry or headline.
238 tags Tags/Property/TODO match in all agenda files.
239 tags-todo Tags/P/T match in all agenda files, TODO entries only.
240 todo-tree Sparse tree of specific TODO keyword in *current* file.
241 tags-tree Sparse tree with all tags matches in *current* file.
242 occur-tree Occur sparse tree for *current* file.
243 ... A user-defined function.
244 match What to search for:
245 - a single keyword for TODO keyword searches
246 - a tags match expression for tags searches
247 - a word search expression for text searches.
248 - a regular expression for occur searches
249 For all other commands, this should be the empty string.
250 settings A list of option settings, similar to that in a let form, so like
251 this: ((opt1 val1) (opt2 val2) ...). The values will be
252 evaluated at the moment of execution, so quote them when needed.
253 files A list of files file to write the produced agenda buffer to
254 with the command `org-store-agenda-views'.
255 If a file name ends in \".html\", an HTML version of the buffer
256 is written out. If it ends in \".ps\", a postscript version is
257 produced. Otherwide, only the plain text is written to the file.
258
259 You can also define a set of commands, to create a composite agenda buffer.
260 In this case, an entry looks like this:
261
262 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
263
264 where
265
266 desc A description string to be displayed in the dispatcher menu.
267 cmd An agenda command, similar to the above. However, tree commands
268 are no allowed, but instead you can get agenda and global todo list.
269 So valid commands for a set are:
270 (agenda \"\" settings)
271 (alltodo \"\" settings)
272 (stuck \"\" settings)
273 (todo \"match\" settings files)
274 (search \"match\" settings files)
275 (tags \"match\" settings files)
276 (tags-todo \"match\" settings files)
277
278 Each command can carry a list of options, and another set of options can be
279 given for the whole set of commands. Individual command options take
280 precedence over the general options.
281
282 When using several characters as key to a command, the first characters
283 are prefix commands. For the dispatcher to display useful information, you
284 should provide a description for the prefix, like
285
286 (setq org-agenda-custom-commands
287 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
288 (\"hl\" tags \"+HOME+Lisa\")
289 (\"hp\" tags \"+HOME+Peter\")
290 (\"hk\" tags \"+HOME+Kim\")))"
291 :group 'org-agenda-custom-commands
292 :type `(repeat
293 (choice :value ("x" "Describe command here" tags "" nil)
294 (list :tag "Single command"
295 (string :tag "Access Key(s) ")
296 (option (string :tag "Description"))
297 (choice
298 (const :tag "Agenda" agenda)
299 (const :tag "TODO list" alltodo)
300 (const :tag "Search words" search)
301 (const :tag "Stuck projects" stuck)
302 (const :tag "Tags search (all agenda files)" tags)
303 (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
304 (const :tag "TODO keyword search (all agenda files)" todo)
305 (const :tag "Tags sparse tree (current buffer)" tags-tree)
306 (const :tag "TODO keyword tree (current buffer)" todo-tree)
307 (const :tag "Occur tree (current buffer)" occur-tree)
308 (sexp :tag "Other, user-defined function"))
309 (string :tag "Match (only for some commands)")
310 ,org-agenda-custom-commands-local-options
311 (option (repeat :tag "Export" (file :tag "Export to"))))
312 (list :tag "Command series, all agenda files"
313 (string :tag "Access Key(s)")
314 (string :tag "Description ")
315 (repeat :tag "Component"
316 (choice
317 (list :tag "Agenda"
318 (const :format "" agenda)
319 (const :tag "" :format "" "")
320 ,org-agenda-custom-commands-local-options)
321 (list :tag "TODO list (all keywords)"
322 (const :format "" alltodo)
323 (const :tag "" :format "" "")
324 ,org-agenda-custom-commands-local-options)
325 (list :tag "Search words"
326 (const :format "" search)
327 (string :tag "Match")
328 ,org-agenda-custom-commands-local-options)
329 (list :tag "Stuck projects"
330 (const :format "" stuck)
331 (const :tag "" :format "" "")
332 ,org-agenda-custom-commands-local-options)
333 (list :tag "Tags search"
334 (const :format "" tags)
335 (string :tag "Match")
336 ,org-agenda-custom-commands-local-options)
337 (list :tag "Tags search, TODO entries only"
338 (const :format "" tags-todo)
339 (string :tag "Match")
340 ,org-agenda-custom-commands-local-options)
341 (list :tag "TODO keyword search"
342 (const :format "" todo)
343 (string :tag "Match")
344 ,org-agenda-custom-commands-local-options)
345 (list :tag "Other, user-defined function"
346 (symbol :tag "function")
347 (string :tag "Match")
348 ,org-agenda-custom-commands-local-options)))
349
350 (repeat :tag "Settings for entire command set"
351 (list (variable :tag "Any variable")
352 (sexp :tag "Value")))
353 (option (repeat :tag "Export" (file :tag "Export to"))))
354 (cons :tag "Prefix key documentation"
355 (string :tag "Access Key(s)")
356 (string :tag "Description ")))))
357
358 (defcustom org-agenda-query-register ?o
359 "The register holding the current query string.
360 The prupose of this is that if you construct a query string interactively,
361 you can then use it to define a custom command."
362 :group 'org-agenda-custom-commands
363 :type 'character)
364
365 (defcustom org-stuck-projects
366 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
367 "How to identify stuck projects.
368 This is a list of four items:
369 1. A tags/todo matcher string that is used to identify a project.
370 The entire tree below a headline matched by this is considered one project.
371 2. A list of TODO keywords identifying non-stuck projects.
372 If the project subtree contains any headline with one of these todo
373 keywords, the project is considered to be not stuck. If you specify
374 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
375 3. A list of tags identifying non-stuck projects.
376 If the project subtree contains any headline with one of these tags,
377 the project is considered to be not stuck. If you specify \"*\" as
378 a tag, any tag will mark the project unstuck.
379 4. An arbitrary regular expression matching non-stuck projects.
380
381 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
382 or `C-c a #' to produce the list."
383 :group 'org-agenda-custom-commands
384 :type '(list
385 (string :tag "Tags/TODO match to identify a project")
386 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
387 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
388 (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
389
390 (defcustom org-agenda-filter-effort-default-operator "<"
391 "The default operator for effort estimate filtering.
392 If you select an effort estimate limit without first pressing an operator,
393 this one will be used."
394 :group 'org-agenda-custom-commands
395 :type '(choice (const :tag "less or equal" "<")
396 (const :tag "greater or equal"">")
397 (const :tag "equal" "=")))
398
399 (defgroup org-agenda-skip nil
400 "Options concerning skipping parts of agenda files."
401 :tag "Org Agenda Skip"
402 :group 'org-agenda)
403
404 (defvar org-agenda-archives-mode nil
405 "Non-nil means, the agenda will include archived items.
406 If this is the symbol `trees', trees in the selected agenda scope
407 that are marked with the ARCHIVE tag will be included anyway. When this is
408 t, also all archive files associated with the current selection of agenda
409 files will be included.")
410
411 (defcustom org-agenda-skip-comment-trees t
412 "Non-nil means, skip trees that start with the COMMENT keyword.
413 When nil, these trees are also scand by agenda commands."
414 :group 'org-agenda-skip
415 :type 'boolean)
416
417 (defcustom org-agenda-todo-list-sublevels t
418 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
419 When nil, the sublevels of a TODO entry are not checked, resulting in
420 potentially much shorter TODO lists."
421 :group 'org-agenda-skip
422 :group 'org-todo
423 :type 'boolean)
424
425 (defcustom org-agenda-todo-ignore-with-date nil
426 "Non-nil means, don't show entries with a date in the global todo list.
427 You can use this if you prefer to mark mere appointments with a TODO keyword,
428 but don't want them to show up in the TODO list.
429 When this is set, it also covers deadlines and scheduled items, the settings
430 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
431 will be ignored."
432 :group 'org-agenda-skip
433 :group 'org-todo
434 :type 'boolean)
435
436 (defcustom org-agenda-todo-ignore-scheduled nil
437 "Non-nil means, don't show scheduled entries in the global todo list.
438 The idea behind this is that by scheduling it, you have already taken care
439 of this item.
440 See also `org-agenda-todo-ignore-with-date'."
441 :group 'org-agenda-skip
442 :group 'org-todo
443 :type 'boolean)
444
445 (defcustom org-agenda-todo-ignore-deadlines nil
446 "Non-nil means, don't show near deadline entries in the global todo list.
447 Near means closer than `org-deadline-warning-days' days.
448 The idea behind this is that such items will appear in the agenda anyway.
449 See also `org-agenda-todo-ignore-with-date'."
450 :group 'org-agenda-skip
451 :group 'org-todo
452 :type 'boolean)
453
454 (defcustom org-agenda-skip-scheduled-if-done nil
455 "Non-nil means don't show scheduled items in agenda when they are done.
456 This is relevant for the daily/weekly agenda, not for the TODO list. And
457 it applies only to the actual date of the scheduling. Warnings about
458 an item with a past scheduling dates are always turned off when the item
459 is DONE."
460 :group 'org-agenda-skip
461 :type 'boolean)
462
463 (defcustom org-agenda-skip-deadline-if-done nil
464 "Non-nil means don't show deadines when the corresponding item is done.
465 When nil, the deadline is still shown and should give you a happy feeling.
466 This is relevant for the daily/weekly agenda. And it applied only to the
467 actualy date of the deadline. Warnings about approching and past-due
468 deadlines are always turned off when the item is DONE."
469 :group 'org-agenda-skip
470 :type 'boolean)
471
472 (defcustom org-agenda-skip-timestamp-if-done nil
473 "Non-nil means don't select item by timestamp or -range if it is DONE."
474 :group 'org-agenda-skip
475 :type 'boolean)
476
477 (defcustom org-timeline-show-empty-dates 3
478 "Non-nil means, `org-timeline' also shows dates without an entry.
479 When nil, only the days which actually have entries are shown.
480 When t, all days between the first and the last date are shown.
481 When an integer, show also empty dates, but if there is a gap of more than
482 N days, just insert a special line indicating the size of the gap."
483 :group 'org-agenda-skip
484 :type '(choice
485 (const :tag "None" nil)
486 (const :tag "All" t)
487 (number :tag "at most")))
488
489 (defgroup org-agenda-startup nil
490 "Options concerning initial settings in the Agenda in Org Mode."
491 :tag "Org Agenda Startup"
492 :group 'org-agenda)
493
494 (defcustom org-finalize-agenda-hook nil
495 "Hook run just before displaying an agenda buffer."
496 :group 'org-agenda-startup
497 :type 'hook)
498
499 (defcustom org-agenda-mouse-1-follows-link nil
500 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
501 A longer mouse click will still set point. Does not work on XEmacs.
502 Needs to be set before org.el is loaded."
503 :group 'org-agenda-startup
504 :type 'boolean)
505
506 (defcustom org-agenda-start-with-follow-mode nil
507 "The initial value of follow-mode in a newly created agenda window."
508 :group 'org-agenda-startup
509 :type 'boolean)
510
511 (defvar org-agenda-include-inactive-timestamps nil
512 "Non-nil means, include inactive time stamps in agenda and timeline.")
513
514 (defgroup org-agenda-windows nil
515 "Options concerning the windows used by the Agenda in Org Mode."
516 :tag "Org Agenda Windows"
517 :group 'org-agenda)
518
519 (defcustom org-agenda-window-setup 'reorganize-frame
520 "How the agenda buffer should be displayed.
521 Possible values for this option are:
522
523 current-window Show agenda in the current window, keeping all other windows.
524 other-frame Use `switch-to-buffer-other-frame' to display agenda.
525 other-window Use `switch-to-buffer-other-window' to display agenda.
526 reorganize-frame Show only two windows on the current frame, the current
527 window and the agenda.
528 See also the variable `org-agenda-restore-windows-after-quit'."
529 :group 'org-agenda-windows
530 :type '(choice
531 (const current-window)
532 (const other-frame)
533 (const other-window)
534 (const reorganize-frame)))
535
536 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
537 "The min and max height of the agenda window as a fraction of frame height.
538 The value of the variable is a cons cell with two numbers between 0 and 1.
539 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
540 :group 'org-agenda-windows
541 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
542
543 (defcustom org-agenda-restore-windows-after-quit nil
544 "Non-nil means, restore window configuration open exiting agenda.
545 Before the window configuration is changed for displaying the agenda,
546 the current status is recorded. When the agenda is exited with
547 `q' or `x' and this option is set, the old state is restored. If
548 `org-agenda-window-setup' is `other-frame', the value of this
549 option will be ignored.."
550 :group 'org-agenda-windows
551 :type 'boolean)
552
553 (defgroup org-agenda-daily/weekly nil
554 "Options concerning the daily/weekly agenda."
555 :tag "Org Agenda Daily/Weekly"
556 :group 'org-agenda)
557
558 (defcustom org-agenda-ndays 7
559 "Number of days to include in overview display.
560 Should be 1 or 7."
561 :group 'org-agenda-daily/weekly
562 :type 'number)
563
564 (defcustom org-agenda-start-on-weekday 1
565 "Non-nil means, start the overview always on the specified weekday.
566 0 denotes Sunday, 1 denotes Monday etc.
567 When nil, always start on the current day."
568 :group 'org-agenda-daily/weekly
569 :type '(choice (const :tag "Today" nil)
570 (number :tag "Weekday No.")))
571
572 (defcustom org-agenda-show-all-dates t
573 "Non-nil means, `org-agenda' shows every day in the selected range.
574 When nil, only the days which actually have entries are shown."
575 :group 'org-agenda-daily/weekly
576 :type 'boolean)
577
578 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
579 "Format string for displaying dates in the agenda.
580 Used by the daily/weekly agenda and by the timeline. This should be
581 a format string understood by `format-time-string', or a function returning
582 the formatted date as a string. The function must take a single argument,
583 a calendar-style date list like (month day year)."
584 :group 'org-agenda-daily/weekly
585 :type '(choice
586 (string :tag "Format string")
587 (function :tag "Function")))
588
589 (defun org-agenda-format-date-aligned (date)
590 "Format a date string for display in the daily/weekly agenda, or timeline.
591 This function makes sure that dates are aligned for easy reading."
592 (require 'cal-iso)
593 (let* ((dayname (calendar-day-name date))
594 (day (cadr date))
595 (day-of-week (calendar-day-of-week date))
596 (month (car date))
597 (monthname (calendar-month-name month))
598 (year (nth 2 date))
599 (iso-week (org-days-to-iso-week
600 (calendar-absolute-from-gregorian date)))
601 (weekyear (cond ((and (= month 1) (>= iso-week 52))
602 (1- year))
603 ((and (= month 12) (<= iso-week 1))
604 (1+ year))
605 (t year)))
606 (weekstring (if (= day-of-week 1)
607 (format " W%02d" iso-week)
608 "")))
609 (format "%-10s %2d %s %4d%s"
610 dayname day monthname year weekstring)))
611
612 (defcustom org-agenda-weekend-days '(6 0)
613 "Which days are weekend?
614 These days get the special face `org-agenda-date-weekend' in the agenda
615 and timeline buffers."
616 :group 'org-agenda-daily/weekly
617 :type '(set :greedy t
618 (const :tag "Monday" 1)
619 (const :tag "Tuesday" 2)
620 (const :tag "Wednesday" 3)
621 (const :tag "Thursday" 4)
622 (const :tag "Friday" 5)
623 (const :tag "Saturday" 6)
624 (const :tag "Sunday" 0)))
625
626 (defcustom org-agenda-include-diary nil
627 "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
628 :group 'org-agenda-daily/weekly
629 :type 'boolean)
630
631 (defcustom org-agenda-include-all-todo nil
632 "Set means weekly/daily agenda will always contain all TODO entries.
633 The TODO entries will be listed at the top of the agenda, before
634 the entries for specific days."
635 :group 'org-agenda-daily/weekly
636 :type 'boolean)
637
638 (defcustom org-agenda-repeating-timestamp-show-all t
639 "Non-nil means, show all occurences of a repeating stamp in the agenda.
640 When nil, only one occurence is shown, either today or the
641 nearest into the future."
642 :group 'org-agenda-daily/weekly
643 :type 'boolean)
644
645 (defcustom org-scheduled-past-days 10000
646 "No. of days to continue listing scheduled items that are not marked DONE.
647 When an item is scheduled on a date, it shows up in the agenda on this
648 day and will be listed until it is marked done for the number of days
649 given here."
650 :group 'org-agenda-daily/weekly
651 :type 'number)
652
653 (defcustom org-agenda-log-mode-items '(closed clock)
654 "List of items that should be shown in agenda log mode.
655 This list may contain the following symbols:
656
657 closed Show entries that have been closed on that day.
658 clock Show entries that have received clocked time on that day.
659 state Show all logged state changes."
660 :group 'org-agenda-daily/weekly
661 :type '(set :greedy t (const closed) (const clock) (const state)))
662
663 (defcustom org-agenda-start-with-clockreport-mode nil
664 "The initial value of clockreport-mode in a newly created agenda window."
665 :group 'org-agenda-startup
666 :group 'org-agenda-daily/weekly
667 :type 'boolean)
668
669 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
670 "Property list with parameters for the clocktable in clockreport mode.
671 This is the display mode that shows a clock table in the daily/weekly
672 agenda, the properties for this dynamic block can be set here.
673 The usual clocktable parameters are allowed here, but you cannot set
674 the properties :name, :tstart, :tend, :block, and :scope - these will
675 be overwritten to make sure the content accurately reflects the
676 current display in the agenda."
677 :group 'org-agenda-daily/weekly
678 :type 'plist)
679
680
681 (defgroup org-agenda-time-grid nil
682 "Options concerning the time grid in the Org-mode Agenda."
683 :tag "Org Agenda Time Grid"
684 :group 'org-agenda)
685
686 (defcustom org-agenda-use-time-grid t
687 "Non-nil means, show a time grid in the agenda schedule.
688 A time grid is a set of lines for specific times (like every two hours between
689 8:00 and 20:00). The items scheduled for a day at specific times are
690 sorted in between these lines.
691 For details about when the grid will be shown, and what it will look like, see
692 the variable `org-agenda-time-grid'."
693 :group 'org-agenda-time-grid
694 :type 'boolean)
695
696 (defcustom org-agenda-time-grid
697 '((daily today require-timed)
698 "----------------"
699 (800 1000 1200 1400 1600 1800 2000))
700
701 "The settings for time grid for agenda display.
702 This is a list of three items. The first item is again a list. It contains
703 symbols specifying conditions when the grid should be displayed:
704
705 daily if the agenda shows a single day
706 weekly if the agenda shows an entire week
707 today show grid on current date, independent of daily/weekly display
708 require-timed show grid only if at least one item has a time specification
709
710 The second item is a string which will be placed behind the grid time.
711
712 The third item is a list of integers, indicating the times that should have
713 a grid line."
714 :group 'org-agenda-time-grid
715 :type
716 '(list
717 (set :greedy t :tag "Grid Display Options"
718 (const :tag "Show grid in single day agenda display" daily)
719 (const :tag "Show grid in weekly agenda display" weekly)
720 (const :tag "Always show grid for today" today)
721 (const :tag "Show grid only if any timed entries are present"
722 require-timed)
723 (const :tag "Skip grid times already present in an entry"
724 remove-match))
725 (string :tag "Grid String")
726 (repeat :tag "Grid Times" (integer :tag "Time"))))
727
728 (defgroup org-agenda-sorting nil
729 "Options concerning sorting in the Org-mode Agenda."
730 :tag "Org Agenda Sorting"
731 :group 'org-agenda)
732
733 (defcustom org-agenda-sorting-strategy
734 '((agenda time-up category-keep priority-down)
735 (todo category-keep priority-down)
736 (tags category-keep priority-down)
737 (search category-keep))
738 "Sorting structure for the agenda items of a single day.
739 This is a list of symbols which will be used in sequence to determine
740 if an entry should be listed before another entry. The following
741 symbols are recognized:
742
743 time-up Put entries with time-of-day indications first, early first
744 time-down Put entries with time-of-day indications first, late first
745 category-keep Keep the default order of categories, corresponding to the
746 sequence in `org-agenda-files'.
747 category-up Sort alphabetically by category, A-Z.
748 category-down Sort alphabetically by category, Z-A.
749 tag-up Sort alphabetically by last tag, A-Z.
750 tag-down Sort alphabetically by last tag, Z-A.
751 priority-up Sort numerically by priority, high priority last.
752 priority-down Sort numerically by priority, high priority first.
753 todo-state-up Sort by todo state, tasks that are done last.
754 todo-state-down Sort by todo state, tasks that are done first.
755 effort-up Sort numerically by estimated effort, high effort last.
756 effort-down Sort numerically by estimated effort, high effort first.
757
758 The different possibilities will be tried in sequence, and testing stops
759 if one comparison returns a \"not-equal\". For example, the default
760 '(time-up category-keep priority-down)
761 means: Pull out all entries having a specified time of day and sort them,
762 in order to make a time schedule for the current day the first thing in the
763 agenda listing for the day. Of the entries without a time indication, keep
764 the grouped in categories, don't sort the categories, but keep them in
765 the sequence given in `org-agenda-files'. Within each category sort by
766 priority.
767
768 Leaving out `category-keep' would mean that items will be sorted across
769 categories by priority.
770
771 Instead of a single list, this can also be a set of list for specific
772 contents, with a context symbol in the car of the list, any of
773 `agenda', `todo', `tags' for the corresponding agenda views."
774 :group 'org-agenda-sorting
775 :type `(choice
776 (repeat :tag "General" ,org-sorting-choice)
777 (list :tag "Individually"
778 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
779 (repeat ,org-sorting-choice))
780 (cons (const :tag "Strategy for TODO lists" todo)
781 (repeat ,org-sorting-choice))
782 (cons (const :tag "Strategy for Tags matches" tags)
783 (repeat ,org-sorting-choice)))))
784
785 (defcustom org-sort-agenda-notime-is-late t
786 "Non-nil means, items without time are considered late.
787 This is only relevant for sorting. When t, items which have no explicit
788 time like 15:30 will be considered as 99:01, i.e. later than any items which
789 do have a time. When nil, the default time is before 0:00. You can use this
790 option to decide if the schedule for today should come before or after timeless
791 agenda entries."
792 :group 'org-agenda-sorting
793 :type 'boolean)
794
795 (defcustom org-sort-agenda-noeffort-is-high t
796 "Non-nil means, items without effort estimate are sorted as high effort.
797 When nil, such items are sorted as 0 minutes effort."
798 :group 'org-agenda-sorting
799 :type 'boolean)
800
801 (defgroup org-agenda-line-format nil
802 "Options concerning the entry prefix in the Org-mode agenda display."
803 :tag "Org Agenda Line Format"
804 :group 'org-agenda)
805
806 (defcustom org-agenda-prefix-format
807 '((agenda . " %-12:c%?-12t% s")
808 (timeline . " % s")
809 (todo . " %-12:c")
810 (tags . " %-12:c")
811 (search . " %-12:c"))
812 "Format specifications for the prefix of items in the agenda views.
813 An alist with four entries, for the different agenda types. The keys to the
814 sublists are `agenda', `timeline', `todo', and `tags'. The values
815 are format strings.
816 This format works similar to a printf format, with the following meaning:
817
818 %c the category of the item, \"Diary\" for entries from the diary, or
819 as given by the CATEGORY keyword or derived from the file name.
820 %T the *last* tag of the item. Last because inherited tags come
821 first in the list.
822 %t the time-of-day specification if one applies to the entry, in the
823 format HH:MM
824 %s Scheduling/Deadline information, a short string
825
826 All specifiers work basically like the standard `%s' of printf, but may
827 contain two additional characters: A question mark just after the `%' and
828 a whitespace/punctuation character just before the final letter.
829
830 If the first character after `%' is a question mark, the entire field
831 will only be included if the corresponding value applies to the
832 current entry. This is useful for fields which should have fixed
833 width when present, but zero width when absent. For example,
834 \"%?-12t\" will result in a 12 character time field if a time of the
835 day is specified, but will completely disappear in entries which do
836 not contain a time.
837
838 If there is punctuation or whitespace character just before the final
839 format letter, this character will be appended to the field value if
840 the value is not empty. For example, the format \"%-12:c\" leads to
841 \"Diary: \" if the category is \"Diary\". If the category were be
842 empty, no additional colon would be interted.
843
844 The default value of this option is \" %-12:c%?-12t% s\", meaning:
845 - Indent the line with two space characters
846 - Give the category in a 12 chars wide field, padded with whitespace on
847 the right (because of `-'). Append a colon if there is a category
848 (because of `:').
849 - If there is a time-of-day, put it into a 12 chars wide field. If no
850 time, don't put in an empty field, just skip it (because of '?').
851 - Finally, put the scheduling information and append a whitespace.
852
853 As another example, if you don't want the time-of-day of entries in
854 the prefix, you could use:
855
856 (setq org-agenda-prefix-format \" %-11:c% s\")
857
858 See also the variables `org-agenda-remove-times-when-in-prefix' and
859 `org-agenda-remove-tags'."
860 :type '(choice
861 (string :tag "General format")
862 (list :greedy t :tag "View dependent"
863 (cons (const agenda) (string :tag "Format"))
864 (cons (const timeline) (string :tag "Format"))
865 (cons (const todo) (string :tag "Format"))
866 (cons (const tags) (string :tag "Format"))
867 (cons (const search) (string :tag "Format"))))
868 :group 'org-agenda-line-format)
869
870 (defvar org-prefix-format-compiled nil
871 "The compiled version of the most recently used prefix format.
872 See the variable `org-agenda-prefix-format'.")
873
874 (defcustom org-agenda-todo-keyword-format "%-1s"
875 "Format for the TODO keyword in agenda lines.
876 Set this to something like \"%-12s\" if you want all TODO keywords
877 to occupy a fixed space in the agenda display."
878 :group 'org-agenda-line-format
879 :type 'string)
880
881 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
882 "Text preceding timerange entries in the agenda view.
883 This is a list with two strings. The first applies when the range
884 is entirely on one day. The second applies if the range spans several days.
885 The strings may have two \"%d\" format specifiers which will be filled
886 with the sequence number of the days, and the total number of days in the
887 range, respectively."
888 :group 'org-agenda-line-format
889 :type '(list
890 (string :tag "Deadline today ")
891 (choice :tag "Deadline relative"
892 (string :tag "Format string")
893 (function))))
894
895 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
896 "Text preceeding scheduled items in the agenda view.
897 This is a list with two strings. The first applies when the item is
898 scheduled on the current day. The second applies when it has been scheduled
899 previously, it may contain a %d indicating that this is the nth time that
900 this item is scheduled, due to automatic rescheduling of unfinished items
901 for the following day. So this number is one larger than the number of days
902 that passed since this item was scheduled first."
903 :group 'org-agenda-line-format
904 :type '(list
905 (string :tag "Scheduled today ")
906 (string :tag "Scheduled previously")))
907
908 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
909 "Text preceeding deadline items in the agenda view.
910 This is a list with two strings. The first applies when the item has its
911 deadline on the current day. The second applies when it is in the past or
912 in the future, it may contain %d to capture how many days away the deadline
913 is (was)."
914 :group 'org-agenda-line-format
915 :type '(list
916 (string :tag "Deadline today ")
917 (choice :tag "Deadline relative"
918 (string :tag "Format string")
919 (function))))
920
921 (defcustom org-agenda-remove-times-when-in-prefix t
922 "Non-nil means, remove duplicate time specifications in agenda items.
923 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
924 time-of-day specification in a headline or diary entry is extracted and
925 placed into the prefix. If this option is non-nil, the original specification
926 \(a timestamp or -range, or just a plain time(range) specification like
927 11:30-4pm) will be removed for agenda display. This makes the agenda less
928 cluttered.
929 The option can be t or nil. It may also be the symbol `beg', indicating
930 that the time should only be removed what it is located at the beginning of
931 the headline/diary entry."
932 :group 'org-agenda-line-format
933 :type '(choice
934 (const :tag "Always" t)
935 (const :tag "Never" nil)
936 (const :tag "When at beginning of entry" beg)))
937
938
939 (defcustom org-agenda-default-appointment-duration nil
940 "Default duration for appointments that only have a starting time.
941 When nil, no duration is specified in such cases.
942 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
943 :group 'org-agenda-line-format
944 :type '(choice
945 (integer :tag "Minutes")
946 (const :tag "No default duration")))
947
948 (defcustom org-agenda-show-inherited-tags t
949 "Non-nil means, show inherited tags in each agenda line."
950 :group 'org-agenda-line-format
951 :type 'boolean)
952
953 (defcustom org-agenda-remove-tags nil
954 "Non-nil means, remove the tags from the headline copy in the agenda.
955 When this is the symbol `prefix', only remove tags when
956 `org-agenda-prefix-format' contains a `%T' specifier."
957 :group 'org-agenda-line-format
958 :type '(choice
959 (const :tag "Always" t)
960 (const :tag "Never" nil)
961 (const :tag "When prefix format contains %T" prefix)))
962
963 (if (fboundp 'defvaralias)
964 (defvaralias 'org-agenda-remove-tags-when-in-prefix
965 'org-agenda-remove-tags))
966
967 (defcustom org-agenda-tags-column -80
968 "Shift tags in agenda items to this column.
969 If this number is positive, it specifies the column. If it is negative,
970 it means that the tags should be flushright to that column. For example,
971 -80 works well for a normal 80 character screen."
972 :group 'org-agenda-line-format
973 :type 'integer)
974
975 (if (fboundp 'defvaralias)
976 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
977
978 (defcustom org-agenda-fontify-priorities t
979 "Non-nil means, highlight low and high priorities in agenda.
980 When t, the highest priority entries are bold, lowest priority italic.
981 This may also be an association list of priority faces, whose
982 keys are the character values of `org-highest-priority',
983 `org-default-priority', and `org-lowest-priority' (the default values
984 are ?A, ?B, and ?C, respectively). The face may be a names face,
985 or a list like `(:background \"Red\")'."
986 :group 'org-agenda-line-format
987 :type '(choice
988 (const :tag "Never" nil)
989 (const :tag "Defaults" t)
990 (repeat :tag "Specify"
991 (list (character :tag "Priority" :value ?A)
992 (sexp :tag "face")))))
993
994
995 (defgroup org-agenda-column-view nil
996 "Options concerning column view in the agenda."
997 :tag "Org Agenda Column View"
998 :group 'org-agenda)
999
1000 (defcustom org-agenda-columns-show-summaries t
1001 "Non-nil means, show summaries for columns displayed in the agenda view."
1002 :group 'org-agenda-column-view
1003 :type 'boolean)
1004
1005 (defcustom org-agenda-columns-remove-prefix-from-item t
1006 "Non-nil means, remove the prefix from a headline for agenda column view.
1007 The special ITEM field in the columns format contains the current line, with
1008 all information shown in other columns (like the TODO state or a tag).
1009 When this variable is non-nil, also the agenda prefix will be removed from
1010 the content of the ITEM field, to make sure as much as possible of the
1011 headline can be shown in the limited width of the field."
1012 :group 'org-agenda
1013 :type 'boolean)
1014
1015 (defcustom org-agenda-columns-compute-summary-properties t
1016 "Non-nil means, recompute all summary properties before column view.
1017 When column view in the agenda is listing properties that have a summary
1018 operator, it can go to all relevant buffers and recompute the summaries
1019 there. This can mean overhead for the agenda column view, but is necessary
1020 to have thing up to date.
1021 As a special case, a CLOCKSUM property also makes sure that the clock
1022 computations are current."
1023 :group 'org-agenda-column-view
1024 :type 'boolean)
1025
1026 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1027 "Non-nil means, the duration of an appointment will add to day effort.
1028 The property to which appointment durations will be added is the one given
1029 in the option `org-effort-property'. If an appointment does not have
1030 an end time, `org-agenda-default-appointment-duration' will be used. If that
1031 is not set, an appointment without end time will not contribute to the time
1032 estimate."
1033 :group 'org-agenda-column-view
1034 :type 'boolean)
1035
1036 (eval-when-compile
1037 (require 'cl))
1038 (require 'org)
1039
1040 (defun org-add-agenda-custom-command (entry)
1041 "Replace or add a command in `org-agenda-custom-commands'.
1042 This is mostly for hacking and trying a new command - once the command
1043 works you probably want to add it to `org-agenda-custom-commands' for good."
1044 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1045 (if ass
1046 (setcdr ass (cdr entry))
1047 (push entry org-agenda-custom-commands))))
1048
1049 ;;; Define the Org-agenda-mode
1050
1051 (defvar org-agenda-mode-map (make-sparse-keymap)
1052 "Keymap for `org-agenda-mode'.")
1053
1054 (defvar org-agenda-menu) ; defined later in this file.
1055 (defvar org-agenda-follow-mode nil)
1056 (defvar org-agenda-clockreport-mode nil)
1057 (defvar org-agenda-show-log nil)
1058 (defvar org-agenda-redo-command nil)
1059 (defvar org-agenda-query-string nil)
1060 (defvar org-agenda-mode-hook nil)
1061 (defvar org-agenda-type nil)
1062 (defvar org-agenda-force-single-file nil)
1063
1064 (defun org-agenda-mode ()
1065 "Mode for time-sorted view on action items in Org-mode files.
1066
1067 The following commands are available:
1068
1069 \\{org-agenda-mode-map}"
1070 (interactive)
1071 (kill-all-local-variables)
1072 (setq org-agenda-undo-list nil
1073 org-agenda-pending-undo-list nil)
1074 (setq major-mode 'org-agenda-mode)
1075 ;; Keep global-font-lock-mode from turning on font-lock-mode
1076 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1077 (setq mode-name "Org-Agenda")
1078 (use-local-map org-agenda-mode-map)
1079 (easy-menu-add org-agenda-menu)
1080 (if org-startup-truncated (setq truncate-lines t))
1081 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1082 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1083 ;; Make sure properties are removed when copying text
1084 (when (boundp 'buffer-substring-filters)
1085 (org-set-local 'buffer-substring-filters
1086 (cons (lambda (x)
1087 (set-text-properties 0 (length x) nil x) x)
1088 buffer-substring-filters)))
1089 (unless org-agenda-keep-modes
1090 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1091 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1092 org-agenda-show-log nil))
1093 (easy-menu-change
1094 '("Agenda") "Agenda Files"
1095 (append
1096 (list
1097 (vector
1098 (if (get 'org-agenda-files 'org-restrict)
1099 "Restricted to single file"
1100 "Edit File List")
1101 '(org-edit-agenda-file-list)
1102 (not (get 'org-agenda-files 'org-restrict)))
1103 "--")
1104 (mapcar 'org-file-menu-entry (org-agenda-files))))
1105 (org-agenda-set-mode-name)
1106 (apply
1107 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1108 (list 'org-agenda-mode-hook)))
1109
1110 (substitute-key-definition 'undo 'org-agenda-undo
1111 org-agenda-mode-map global-map)
1112 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1113 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1114 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1115 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1116 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1117 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1118 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1119 (org-defkey org-agenda-mode-map "A" 'org-agenda-archive-to-archive-sibling)
1120 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1121 (org-defkey org-agenda-mode-map " " 'org-agenda-show)
1122 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1123 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1124 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1125 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1126 (org-defkey org-agenda-mode-map "b" 'org-agenda-tree-to-indirect-buffer)
1127 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1128 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1129 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1130 (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
1131 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1132 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1133 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1134 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1135 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1136 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1137 (org-defkey org-agenda-mode-map "m" 'org-agenda-month-view)
1138 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1139 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1140 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1141 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1142 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1143 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
1144 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
1145 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
1146 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
1147
1148 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1149 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1150 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1151 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1152 (while l (org-defkey org-agenda-mode-map
1153 (int-to-string (pop l)) 'digit-argument)))
1154
1155 (org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
1156 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1157 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1158 (org-defkey org-agenda-mode-map "v" 'org-agenda-archives-mode)
1159 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1160 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1161 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1162 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1163 (org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
1164 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1165 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1166 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1167 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1168 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1169 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1170 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1171 (org-defkey org-agenda-mode-map "n" 'next-line)
1172 (org-defkey org-agenda-mode-map "p" 'previous-line)
1173 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1174 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1175 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1176 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1177 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1178 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1179 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1180 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1181 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1182 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1183 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1184 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1185 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1186 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1187 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1188 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1189 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1190 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1191 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1192 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1193 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1194 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1195 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1196 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1197 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1198 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1199 (org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
1200 (org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
1201 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1202
1203 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1204 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1205 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1206 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1207 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1208 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1209
1210 (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
1211 "Local keymap for agenda entries from Org-mode.")
1212
1213 (org-defkey org-agenda-keymap
1214 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1215 (org-defkey org-agenda-keymap
1216 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1217 (when org-agenda-mouse-1-follows-link
1218 (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
1219 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1220 '("Agenda"
1221 ("Agenda Files")
1222 "--"
1223 ["Show" org-agenda-show t]
1224 ["Go To (other window)" org-agenda-goto t]
1225 ["Go To (this window)" org-agenda-switch-to t]
1226 ["Follow Mode" org-agenda-follow-mode
1227 :style toggle :selected org-agenda-follow-mode :active t]
1228 ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1229 "--"
1230 ["Cycle TODO" org-agenda-todo t]
1231 ("Archive"
1232 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1233 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1234 ["Archive subtree" org-agenda-archive t])
1235 ["Delete subtree" org-agenda-kill t]
1236 ["Add note" org-agenda-add-note t]
1237 "--"
1238 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1239 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1240 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1241 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
1242 "--"
1243 ("Tags and Properties"
1244 ["Show all Tags" org-agenda-show-tags t]
1245 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1246 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1247 "--"
1248 ["Column View" org-columns t])
1249 ("Date/Schedule"
1250 ["Schedule" org-agenda-schedule t]
1251 ["Set Deadline" org-agenda-deadline t]
1252 "--"
1253 ["Mark item" org-agenda-action :active t :keys "k m"]
1254 ["Show mark item" org-agenda-action :active t :keys "k v"]
1255 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1256 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1257 "--"
1258 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1259 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1260 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1261 ("Clock"
1262 ["Clock in" org-agenda-clock-in t]
1263 ["Clock out" org-agenda-clock-out t]
1264 ["Clock cancel" org-agenda-clock-cancel t]
1265 ["Goto running clock" org-clock-goto t])
1266 ("Priority"
1267 ["Set Priority" org-agenda-priority t]
1268 ["Increase Priority" org-agenda-priority-up t]
1269 ["Decrease Priority" org-agenda-priority-down t]
1270 ["Show Priority" org-agenda-show-priority t])
1271 ("Calendar/Diary"
1272 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1273 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1274 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1275 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1276 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1277 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1278 "--"
1279 ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
1280 "--"
1281 ("View"
1282 ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
1283 :style radio :selected (equal org-agenda-ndays 1)]
1284 ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
1285 :style radio :selected (equal org-agenda-ndays 7)]
1286 ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
1287 :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
1288 ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
1289 :style radio :selected (member org-agenda-ndays '(365 366))]
1290 "--"
1291 ["Include Diary" org-agenda-toggle-diary
1292 :style toggle :selected org-agenda-include-diary
1293 :active (org-agenda-check-type nil 'agenda)]
1294 ["Use Time Grid" org-agenda-toggle-time-grid
1295 :style toggle :selected org-agenda-use-time-grid
1296 :active (org-agenda-check-type nil 'agenda)]
1297 "--"
1298 ["Show clock report" org-agenda-clockreport-mode
1299 :style toggle :selected org-agenda-clockreport-mode
1300 :active (org-agenda-check-type nil 'agenda)]
1301 "--"
1302 ["Show Logbook entries" org-agenda-log-mode
1303 :style toggle :selected org-agenda-show-log
1304 :active (org-agenda-check-type nil 'agenda 'timeline)]
1305 ["Include archived trees" org-agenda-archives-mode
1306 :style toggle :selected org-agenda-archives-mode :active t]
1307 ["Include archive files" (org-agenda-archives-mode t)
1308 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1309 :keys "C-u v"])
1310 ["Write view to file" org-write-agenda t]
1311 ["Rebuild buffer" org-agenda-redo t]
1312 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1313 "--"
1314 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1315 "--"
1316 ["Quit" org-agenda-quit t]
1317 ["Exit and Release Buffers" org-agenda-exit t]
1318 ))
1319
1320 ;;; Agenda undo
1321
1322 (defvar org-agenda-allow-remote-undo t
1323 "Non-nil means, allow remote undo from the agenda buffer.")
1324 (defvar org-agenda-undo-list nil
1325 "List of undoable operations in the agenda since last refresh.")
1326 (defvar org-agenda-undo-has-started-in nil
1327 "Buffers that have already seen `undo-start' in the current undo sequence.")
1328 (defvar org-agenda-pending-undo-list nil
1329 "In a series of undo commands, this is the list of remaning undo items.")
1330
1331
1332 (defun org-agenda-undo ()
1333 "Undo a remote editing step in the agenda.
1334 This undoes changes both in the agenda buffer and in the remote buffer
1335 that have been changed along."
1336 (interactive)
1337 (or org-agenda-allow-remote-undo
1338 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
1339 (if (not (eq this-command last-command))
1340 (setq org-agenda-undo-has-started-in nil
1341 org-agenda-pending-undo-list org-agenda-undo-list))
1342 (if (not org-agenda-pending-undo-list)
1343 (error "No further undo information"))
1344 (let* ((entry (pop org-agenda-pending-undo-list))
1345 buf line cmd rembuf)
1346 (setq cmd (pop entry) line (pop entry))
1347 (setq rembuf (nth 2 entry))
1348 (org-with-remote-undo rembuf
1349 (while (bufferp (setq buf (pop entry)))
1350 (if (pop entry)
1351 (with-current-buffer buf
1352 (let ((last-undo-buffer buf)
1353 (inhibit-read-only t))
1354 (unless (memq buf org-agenda-undo-has-started-in)
1355 (push buf org-agenda-undo-has-started-in)
1356 (make-local-variable 'pending-undo-list)
1357 (undo-start))
1358 (while (and pending-undo-list
1359 (listp pending-undo-list)
1360 (not (car pending-undo-list)))
1361 (pop pending-undo-list))
1362 (undo-more 1))))))
1363 (goto-line line)
1364 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1365
1366 (defun org-verify-change-for-undo (l1 l2)
1367 "Verify that a real change occurred between the undo lists L1 and L2."
1368 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1369 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1370 (not (eq l1 l2)))
1371
1372 ;;; Agenda dispatch
1373
1374 (defvar org-agenda-restrict nil)
1375 (defvar org-agenda-restrict-begin (make-marker))
1376 (defvar org-agenda-restrict-end (make-marker))
1377 (defvar org-agenda-last-dispatch-buffer nil)
1378 (defvar org-agenda-overriding-restriction nil)
1379
1380 ;;;###autoload
1381 (defun org-agenda (arg &optional keys restriction)
1382 "Dispatch agenda commands to collect entries to the agenda buffer.
1383 Prompts for a command to execute. Any prefix arg will be passed
1384 on to the selected command. The default selections are:
1385
1386 a Call `org-agenda-list' to display the agenda for current day or week.
1387 t Call `org-todo-list' to display the global todo list.
1388 T Call `org-todo-list' to display the global todo list, select only
1389 entries with a specific TODO keyword (the user gets a prompt).
1390 m Call `org-tags-view' to display headlines with tags matching
1391 a condition (the user is prompted for the condition).
1392 M Like `m', but select only TODO entries, no ordinary headlines.
1393 L Create a timeline for the current buffer.
1394 e Export views to associated files.
1395
1396 More commands can be added by configuring the variable
1397 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1398 searches can be pre-defined in this way.
1399
1400 If the current buffer is in Org-mode and visiting a file, you can also
1401 first press `<' once to indicate that the agenda should be temporarily
1402 \(until the next use of \\[org-agenda]) restricted to the current file.
1403 Pressing `<' twice means to restrict to the current subtree or region
1404 \(if active)."
1405 (interactive "P")
1406 (catch 'exit
1407 (let* ((prefix-descriptions nil)
1408 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1409 (org-agenda-custom-commands
1410 ;; normalize different versions
1411 (delq nil
1412 (mapcar
1413 (lambda (x)
1414 (cond ((stringp (cdr x))
1415 (push x prefix-descriptions)
1416 nil)
1417 ((stringp (nth 1 x)) x)
1418 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1419 (t (cons (car x) (cons "" (cdr x))))))
1420 org-agenda-custom-commands)))
1421 (buf (current-buffer))
1422 (bfn (buffer-file-name (buffer-base-buffer)))
1423 entry key type match lprops ans)
1424 ;; Turn off restriction unless there is an overriding one
1425 (unless org-agenda-overriding-restriction
1426 (put 'org-agenda-files 'org-restrict nil)
1427 (setq org-agenda-restrict nil)
1428 (move-marker org-agenda-restrict-begin nil)
1429 (move-marker org-agenda-restrict-end nil))
1430 ;; Delete old local properties
1431 (put 'org-agenda-redo-command 'org-lprops nil)
1432 ;; Remember where this call originated
1433 (setq org-agenda-last-dispatch-buffer (current-buffer))
1434 (unless keys
1435 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1436 keys (car ans)
1437 restriction (cdr ans)))
1438 ;; Estabish the restriction, if any
1439 (when (and (not org-agenda-overriding-restriction) restriction)
1440 (put 'org-agenda-files 'org-restrict (list bfn))
1441 (cond
1442 ((eq restriction 'region)
1443 (setq org-agenda-restrict t)
1444 (move-marker org-agenda-restrict-begin (region-beginning))
1445 (move-marker org-agenda-restrict-end (region-end)))
1446 ((eq restriction 'subtree)
1447 (save-excursion
1448 (setq org-agenda-restrict t)
1449 (org-back-to-heading t)
1450 (move-marker org-agenda-restrict-begin (point))
1451 (move-marker org-agenda-restrict-end
1452 (progn (org-end-of-subtree t)))))))
1453
1454 (require 'calendar) ; FIXME: can we avoid this for some commands?
1455 ;; For example the todo list should not need it (but does...)
1456 (cond
1457 ((setq entry (assoc keys org-agenda-custom-commands))
1458 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1459 (progn
1460 (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
1461 (put 'org-agenda-redo-command 'org-lprops lprops)
1462 (cond
1463 ((eq type 'agenda)
1464 (org-let lprops '(org-agenda-list current-prefix-arg)))
1465 ((eq type 'alltodo)
1466 (org-let lprops '(org-todo-list current-prefix-arg)))
1467 ((eq type 'search)
1468 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1469 ((eq type 'stuck)
1470 (org-let lprops '(org-agenda-list-stuck-projects
1471 current-prefix-arg)))
1472 ((eq type 'tags)
1473 (org-let lprops '(org-tags-view current-prefix-arg match)))
1474 ((eq type 'tags-todo)
1475 (org-let lprops '(org-tags-view '(4) match)))
1476 ((eq type 'todo)
1477 (org-let lprops '(org-todo-list match)))
1478 ((eq type 'tags-tree)
1479 (org-check-for-org-mode)
1480 (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
1481 ((eq type 'todo-tree)
1482 (org-check-for-org-mode)
1483 (org-let lprops
1484 '(org-occur (concat "^" outline-regexp "[ \t]*"
1485 (regexp-quote match) "\\>"))))
1486 ((eq type 'occur-tree)
1487 (org-check-for-org-mode)
1488 (org-let lprops '(org-occur match)))
1489 ((functionp type)
1490 (org-let lprops '(funcall type match)))
1491 ((fboundp type)
1492 (org-let lprops '(funcall type match)))
1493 (t (error "Invalid custom agenda command type %s" type))))
1494 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1495 ((equal keys "C")
1496 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1497 (customize-variable 'org-agenda-custom-commands))
1498 ((equal keys "a") (call-interactively 'org-agenda-list))
1499 ((equal keys "s") (call-interactively 'org-search-view))
1500 ((equal keys "t") (call-interactively 'org-todo-list))
1501 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1502 ((equal keys "m") (call-interactively 'org-tags-view))
1503 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1504 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1505 ((equal keys "L")
1506 (unless (org-mode-p)
1507 (error "This is not an Org-mode file"))
1508 (unless restriction
1509 (put 'org-agenda-files 'org-restrict (list bfn))
1510 (org-call-with-arg 'org-timeline arg)))
1511 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1512 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1513 ((equal keys "!") (customize-variable 'org-stuck-projects))
1514 (t (error "Invalid agenda key"))))))
1515
1516 (defun org-agenda-normalize-custom-commands (cmds)
1517 (delq nil
1518 (mapcar
1519 (lambda (x)
1520 (cond ((stringp (cdr x)) nil)
1521 ((stringp (nth 1 x)) x)
1522 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1523 (t (cons (car x) (cons "" (cdr x))))))
1524 cmds)))
1525
1526 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1527 "The user interface for selecting an agenda command."
1528 (catch 'exit
1529 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1530 (restrict-ok (and bfn (org-mode-p)))
1531 (region-p (org-region-active-p))
1532 (custom org-agenda-custom-commands)
1533 (selstring "")
1534 restriction second-time
1535 c entry key type match prefixes rmheader header-end custom1 desc)
1536 (save-window-excursion
1537 (delete-other-windows)
1538 (org-switch-to-buffer-other-window " *Agenda Commands*")
1539 (erase-buffer)
1540 (insert (eval-when-compile
1541 (let ((header
1542 "
1543 Press key for an agenda command: < Buffer, subtree/region restriction
1544 -------------------------------- > Remove restriction
1545 a Agenda for current week or day e Export agenda views
1546 t List of all TODO entries T Entries with special TODO kwd
1547 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1548 L Timeline for current buffer # List stuck projects (!=configure)
1549 s Search for keywords C Configure custom agenda commands
1550 / Multi-occur
1551 ")
1552 (start 0))
1553 (while (string-match
1554 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1555 header start)
1556 (setq start (match-end 0))
1557 (add-text-properties (match-beginning 2) (match-end 2)
1558 '(face bold) header))
1559 header)))
1560 (setq header-end (move-marker (make-marker) (point)))
1561 (while t
1562 (setq custom1 custom)
1563 (when (eq rmheader t)
1564 (goto-line 1)
1565 (re-search-forward ":" nil t)
1566 (delete-region (match-end 0) (point-at-eol))
1567 (forward-char 1)
1568 (looking-at "-+")
1569 (delete-region (match-end 0) (point-at-eol))
1570 (move-marker header-end (match-end 0)))
1571 (goto-char header-end)
1572 (delete-region (point) (point-max))
1573 (while (setq entry (pop custom1))
1574 (setq key (car entry) desc (nth 1 entry)
1575 type (nth 2 entry) match (nth 3 entry))
1576 (if (> (length key) 1)
1577 (add-to-list 'prefixes (string-to-char key))
1578 (insert
1579 (format
1580 "\n%-4s%-14s: %s"
1581 (org-add-props (copy-sequence key)
1582 '(face bold))
1583 (cond
1584 ((string-match "\\S-" desc) desc)
1585 ((eq type 'agenda) "Agenda for current week or day")
1586 ((eq type 'alltodo) "List of all TODO entries")
1587 ((eq type 'search) "Word search")
1588 ((eq type 'stuck) "List of stuck projects")
1589 ((eq type 'todo) "TODO keyword")
1590 ((eq type 'tags) "Tags query")
1591 ((eq type 'tags-todo) "Tags (TODO)")
1592 ((eq type 'tags-tree) "Tags tree")
1593 ((eq type 'todo-tree) "TODO kwd tree")
1594 ((eq type 'occur-tree) "Occur tree")
1595 ((functionp type) (if (symbolp type)
1596 (symbol-name type)
1597 "Lambda expression"))
1598 (t "???"))
1599 (cond
1600 ((stringp match)
1601 (org-add-props match nil 'face 'org-warning))
1602 (match
1603 (format "set of %d commands" (length match)))
1604 (t ""))))))
1605 (when prefixes
1606 (mapc (lambda (x)
1607 (insert
1608 (format "\n%s %s"
1609 (org-add-props (char-to-string x)
1610 nil 'face 'bold)
1611 (or (cdr (assoc (concat selstring (char-to-string x))
1612 prefix-descriptions))
1613 "Prefix key"))))
1614 prefixes))
1615 (goto-char (point-min))
1616 (if second-time
1617 (if (not (pos-visible-in-window-p (point-max)))
1618 (org-fit-window-to-buffer))
1619 (setq second-time t)
1620 (org-fit-window-to-buffer))
1621 (message "Press key for agenda command%s:"
1622 (if (or restrict-ok org-agenda-overriding-restriction)
1623 (if org-agenda-overriding-restriction
1624 " (restriction lock active)"
1625 (if restriction
1626 (format " (restricted to %s)" restriction)
1627 " (unrestricted)"))
1628 ""))
1629 (setq c (read-char-exclusive))
1630 (message "")
1631 (cond
1632 ((assoc (char-to-string c) custom)
1633 (setq selstring (concat selstring (char-to-string c)))
1634 (throw 'exit (cons selstring restriction)))
1635 ((memq c prefixes)
1636 (setq selstring (concat selstring (char-to-string c))
1637 prefixes nil
1638 rmheader (or rmheader t)
1639 custom (delq nil (mapcar
1640 (lambda (x)
1641 (if (or (= (length (car x)) 1)
1642 (/= (string-to-char (car x)) c))
1643 nil
1644 (cons (substring (car x) 1) (cdr x))))
1645 custom))))
1646 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
1647 (message "Restriction is only possible in Org-mode buffers")
1648 (ding) (sit-for 1))
1649 ((eq c ?1)
1650 (org-agenda-remove-restriction-lock 'noupdate)
1651 (setq restriction 'buffer))
1652 ((eq c ?0)
1653 (org-agenda-remove-restriction-lock 'noupdate)
1654 (setq restriction (if region-p 'region 'subtree)))
1655 ((eq c ?<)
1656 (org-agenda-remove-restriction-lock 'noupdate)
1657 (setq restriction
1658 (cond
1659 ((eq restriction 'buffer)
1660 (if region-p 'region 'subtree))
1661 ((memq restriction '(subtree region))
1662 nil)
1663 (t 'buffer))))
1664 ((eq c ?>)
1665 (org-agenda-remove-restriction-lock 'noupdate)
1666 (setq restriction nil))
1667 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
1668 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
1669 ((and (> (length selstring) 0) (eq c ?\d))
1670 (delete-window)
1671 (org-agenda-get-restriction-and-command prefix-descriptions))
1672
1673 ((equal c ?q) (error "Abort"))
1674 (t (error "Invalid key %c" c))))))))
1675
1676 (defun org-run-agenda-series (name series)
1677 (org-prepare-agenda name)
1678 (let* ((org-agenda-multi t)
1679 (redo (list 'org-run-agenda-series name (list 'quote series)))
1680 (cmds (car series))
1681 (gprops (nth 1 series))
1682 match ;; The byte compiler incorrectly complains about this. Keep it!
1683 cmd type lprops)
1684 (while (setq cmd (pop cmds))
1685 (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
1686 (cond
1687 ((eq type 'agenda)
1688 (org-let2 gprops lprops
1689 '(call-interactively 'org-agenda-list)))
1690 ((eq type 'alltodo)
1691 (org-let2 gprops lprops
1692 '(call-interactively 'org-todo-list)))
1693 ((eq type 'search)
1694 (org-let2 gprops lprops
1695 '(org-search-view current-prefix-arg match nil)))
1696 ((eq type 'stuck)
1697 (org-let2 gprops lprops
1698 '(call-interactively 'org-agenda-list-stuck-projects)))
1699 ((eq type 'tags)
1700 (org-let2 gprops lprops
1701 '(org-tags-view current-prefix-arg match)))
1702 ((eq type 'tags-todo)
1703 (org-let2 gprops lprops
1704 '(org-tags-view '(4) match)))
1705 ((eq type 'todo)
1706 (org-let2 gprops lprops
1707 '(org-todo-list match)))
1708 ((fboundp type)
1709 (org-let2 gprops lprops
1710 '(funcall type match)))
1711 (t (error "Invalid type in command series"))))
1712 (widen)
1713 (setq org-agenda-redo-command redo)
1714 (goto-char (point-min)))
1715 (org-finalize-agenda))
1716
1717 ;;;###autoload
1718 (defmacro org-batch-agenda (cmd-key &rest parameters)
1719 "Run an agenda command in batch mode and send the result to STDOUT.
1720 If CMD-KEY is a string of length 1, it is used as a key in
1721 `org-agenda-custom-commands' and triggers this command. If it is a
1722 longer string it is used as a tags/todo match string.
1723 Paramters are alternating variable names and values that will be bound
1724 before running the agenda command."
1725 (let (pars)
1726 (while parameters
1727 (push (list (pop parameters) (if parameters (pop parameters))) pars))
1728 (if (> (length cmd-key) 2)
1729 (eval (list 'let (nreverse pars)
1730 (list 'org-tags-view nil cmd-key)))
1731 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
1732 (set-buffer org-agenda-buffer-name)
1733 (princ (org-encode-for-stdout (buffer-string)))))
1734
1735 (defun org-encode-for-stdout (string)
1736 (if (fboundp 'encode-coding-string)
1737 (encode-coding-string string buffer-file-coding-system)
1738 string))
1739
1740 (defvar org-agenda-info nil)
1741
1742 ;;;###autoload
1743 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
1744 "Run an agenda command in batch mode and send the result to STDOUT.
1745 If CMD-KEY is a string of length 1, it is used as a key in
1746 `org-agenda-custom-commands' and triggers this command. If it is a
1747 longer string it is used as a tags/todo match string.
1748 Paramters are alternating variable names and values that will be bound
1749 before running the agenda command.
1750
1751 The output gives a line for each selected agenda item. Each
1752 item is a list of comma-separated values, like this:
1753
1754 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
1755
1756 category The category of the item
1757 head The headline, without TODO kwd, TAGS and PRIORITY
1758 type The type of the agenda entry, can be
1759 todo selected in TODO match
1760 tagsmatch selected in tags match
1761 diary imported from diary
1762 deadline a deadline on given date
1763 scheduled scheduled on given date
1764 timestamp entry has timestamp on given date
1765 closed entry was closed on given date
1766 upcoming-deadline warning about deadline
1767 past-scheduled forwarded scheduled item
1768 block entry has date block including g. date
1769 todo The todo keyword, if any
1770 tags All tags including inherited ones, separated by colons
1771 date The relevant date, like 2007-2-14
1772 time The time, like 15:00-16:50
1773 extra Sting with extra planning info
1774 priority-l The priority letter if any was given
1775 priority-n The computed numerical priority
1776 agenda-day The day in the agenda where this is listed"
1777
1778 (let (pars)
1779 (while parameters
1780 (push (list (pop parameters) (if parameters (pop parameters))) pars))
1781 (push (list 'org-agenda-remove-tags t) pars)
1782 (if (> (length cmd-key) 2)
1783 (eval (list 'let (nreverse pars)
1784 (list 'org-tags-view nil cmd-key)))
1785 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
1786 (set-buffer org-agenda-buffer-name)
1787 (let* ((lines (org-split-string (buffer-string) "\n"))
1788 line)
1789 (while (setq line (pop lines))
1790 (catch 'next
1791 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
1792 (setq org-agenda-info
1793 (org-fix-agenda-info (text-properties-at 0 line)))
1794 (princ
1795 (org-encode-for-stdout
1796 (mapconcat 'org-agenda-export-csv-mapper
1797 '(org-category txt type todo tags date time-of-day extra
1798 priority-letter priority agenda-day)
1799 ",")))
1800 (princ "\n"))))))
1801
1802 (defun org-fix-agenda-info (props)
1803 "Make sure all properties on an agenda item have a canonical form,
1804 so the export commands can easily use it."
1805 (let (tmp re)
1806 (when (setq tmp (plist-get props 'tags))
1807 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
1808 (when (setq tmp (plist-get props 'date))
1809 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
1810 (let ((calendar-date-display-form '(year "-" month "-" day)))
1811 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
1812
1813 (setq tmp (calendar-date-string tmp)))
1814 (setq props (plist-put props 'date tmp)))
1815 (when (setq tmp (plist-get props 'day))
1816 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
1817 (let ((calendar-date-display-form '(year "-" month "-" day)))
1818 (setq tmp (calendar-date-string tmp)))
1819 (setq props (plist-put props 'day tmp))
1820 (setq props (plist-put props 'agenda-day tmp)))
1821 (when (setq tmp (plist-get props 'txt))
1822 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
1823 (plist-put props 'priority-letter (match-string 1 tmp))
1824 (setq tmp (replace-match "" t t tmp)))
1825 (when (and (setq re (plist-get props 'org-todo-regexp))
1826 (setq re (concat "\\`\\.*" re " ?"))
1827 (string-match re tmp))
1828 (plist-put props 'todo (match-string 1 tmp))
1829 (setq tmp (replace-match "" t t tmp)))
1830 (plist-put props 'txt tmp)))
1831 props)
1832
1833 (defun org-agenda-export-csv-mapper (prop)
1834 (let ((res (plist-get org-agenda-info prop)))
1835 (setq res
1836 (cond
1837 ((not res) "")
1838 ((stringp res) res)
1839 (t (prin1-to-string res))))
1840 (while (string-match "," res)
1841 (setq res (replace-match ";" t t res)))
1842 (org-trim res)))
1843
1844
1845 ;;;###autoload
1846 (defun org-store-agenda-views (&rest parameters)
1847 (interactive)
1848 (eval (list 'org-batch-store-agenda-views)))
1849
1850 ;; FIXME, why is this a macro?????
1851 ;;;###autoload
1852 (defmacro org-batch-store-agenda-views (&rest parameters)
1853 "Run all custom agenda commands that have a file argument."
1854 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
1855 (pop-up-frames nil)
1856 (dir default-directory)
1857 pars cmd thiscmdkey files opts cmd-or-set)
1858 (while parameters
1859 (push (list (pop parameters) (if parameters (pop parameters))) pars))
1860 (setq pars (reverse pars))
1861 (save-window-excursion
1862 (while cmds
1863 (setq cmd (pop cmds)
1864 thiscmdkey (car cmd)
1865 cmd-or-set (nth 2 cmd)
1866 opts (nth (if (listp cmd-or-set) 3 4) cmd)
1867 files (nth (if (listp cmd-or-set) 4 5) cmd))
1868 (if (stringp files) (setq files (list files)))
1869 (when files
1870 (eval (list 'let (append org-agenda-exporter-settings opts pars)
1871 (list 'org-agenda nil thiscmdkey)))
1872 (set-buffer org-agenda-buffer-name)
1873 (while files
1874 (eval (list 'let (append org-agenda-exporter-settings opts pars)
1875 (list 'org-write-agenda
1876 (expand-file-name (pop files) dir) t))))
1877 (and (get-buffer org-agenda-buffer-name)
1878 (kill-buffer org-agenda-buffer-name)))))))
1879
1880 (defun org-write-agenda (file &optional nosettings)
1881 "Write the current buffer (an agenda view) as a file.
1882 Depending on the extension of the file name, plain text (.txt),
1883 HTML (.html or .htm) or Postscript (.ps) is produced.
1884 If the extension is .ics, run icalendar export over all files used
1885 to construct the agenda and limit the export to entries listed in the
1886 agenda now.
1887 If NOSETTINGS is given, do not scope the settings of
1888 `org-agenda-exporter-settings' into the export commands. This is used when
1889 the settings have already been scoped and we do not wish to overrule other,
1890 higher priority settings."
1891 (interactive "FWrite agenda to file: ")
1892 (if (not (file-writable-p file))
1893 (error "Cannot write agenda to file %s" file))
1894 (cond
1895 ((string-match "\\.html?\\'" file) (require 'htmlize))
1896 ((string-match "\\.ps\\'" file) (require 'ps-print)))
1897 (org-let (if nosettings nil org-agenda-exporter-settings)
1898 '(save-excursion
1899 (save-window-excursion
1900 (org-agenda-mark-filtered-text)
1901 (let ((bs (copy-sequence (buffer-string))) beg)
1902 (org-agenda-unmark-filtered-text)
1903 (with-temp-buffer
1904 (insert bs)
1905 (org-agenda-remove-marked-text 'org-filtered)
1906 (while (setq beg (text-property-any (point-min) (point-max)
1907 'org-filtered t))
1908 (delete-region
1909 beg (or (next-single-property-change beg 'org-filtered)
1910 (point-max))))
1911 (cond
1912 ((string-match "\\.html?\\'" file)
1913 (set-buffer (htmlize-buffer (current-buffer)))
1914
1915 (when (and org-agenda-export-html-style
1916 (string-match "<style>" org-agenda-export-html-style))
1917 ;; replace <style> section with org-agenda-export-html-style
1918 (goto-char (point-min))
1919 (kill-region (- (search-forward "<style") 6)
1920 (search-forward "</style>"))
1921 (insert org-agenda-export-html-style))
1922 (write-file file)
1923 (kill-buffer (current-buffer))
1924 (message "HTML written to %s" file))
1925 ((string-match "\\.ps\\'" file)
1926 (ps-print-buffer-with-faces file)
1927 (message "Postscript written to %s" file))
1928 ((string-match "\\.ics\\'" file)
1929 (let ((org-agenda-marker-table
1930 (org-create-marker-find-array
1931 (org-agenda-collect-markers)))
1932 (org-icalendar-verify-function 'org-check-agenda-marker-table)
1933 (org-combined-agenda-icalendar-file file))
1934 (apply 'org-export-icalendar 'combine
1935 (org-agenda-files nil 'ifmode))))
1936 (t
1937 (let ((bs (buffer-string)))
1938 (find-file file)
1939 (erase-buffer)
1940 (insert bs)
1941 (save-buffer 0)
1942 (kill-buffer (current-buffer))
1943 (message "Plain text written to %s" file))))))))
1944 (set-buffer org-agenda-buffer-name)))
1945 (defvar org-agenda-filter-overlays nil)
1946
1947 (defun org-agenda-mark-filtered-text ()
1948 "Mark all text hidden by filtering with a text property."
1949 (let ((inhibit-read-only t))
1950 (mapc
1951 (lambda (o)
1952 (when (equal (org-overlay-buffer o) (current-buffer))
1953 (put-text-property
1954 (org-overlay-start o) (org-overlay-end o)
1955 'org-filtered t)))
1956 org-agenda-filter-overlays)))
1957
1958 (defun org-agenda-unmark-filtered-text ()
1959 "Remove the filtering text property."
1960 (let ((inhibit-read-only t))
1961 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
1962
1963 (defun org-agenda-remove-marked-text (property &optional value)
1964 "Delete all text marked with VALUE of PROPERTY.
1965 VALUE defaults to t."
1966 (let (beg)
1967 (setq value (or value t))
1968 (while (setq beg (text-property-any (point-min) (point-max)
1969 property value))
1970 (delete-region
1971 beg (or (next-single-property-change beg 'org-filtered)
1972 (point-max))))))
1973
1974 (defun org-agenda-collect-markers ()
1975 "Collect the markers pointing to entries in the agenda buffer."
1976 (let (m markers)
1977 (save-excursion
1978 (goto-char (point-min))
1979 (while (not (eobp))
1980 (when (setq m (or (get-text-property (point) 'org-hd-marker)
1981 (get-text-property (point) 'org-marker)))
1982 (push m markers))
1983 (beginning-of-line 2)))
1984 (nreverse markers)))
1985
1986 (defun org-create-marker-find-array (marker-list)
1987 "Create a alist of files names with all marker positions in that file."
1988 (let (f tbl m a p)
1989 (while (setq m (pop marker-list))
1990 (setq p (marker-position m)
1991 f (buffer-file-name (or (buffer-base-buffer
1992 (marker-buffer m))
1993 (marker-buffer m))))
1994 (if (setq a (assoc f tbl))
1995 (push (marker-position m) (cdr a))
1996 (push (list f p) tbl)))
1997 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
1998 tbl)))
1999
2000 (defvar org-agenda-marker-table nil) ; dyamically scoped parameter
2001 (defun org-check-agenda-marker-table ()
2002 "Check of the current entry is on the marker list."
2003 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2004 a)
2005 (and (setq a (assoc file org-agenda-marker-table))
2006 (save-match-data
2007 (save-excursion
2008 (org-back-to-heading t)
2009 (member (point) (cdr a)))))))
2010
2011 (defun org-check-for-org-mode ()
2012 "Make sure current buffer is in org-mode. Error if not."
2013 (or (org-mode-p)
2014 (error "Cannot execute org-mode agenda command on buffer in %s."
2015 major-mode)))
2016
2017 (defun org-fit-agenda-window ()
2018 "Fit the window to the buffer size."
2019 (and (memq org-agenda-window-setup '(reorganize-frame))
2020 (fboundp 'fit-window-to-buffer)
2021 (org-fit-window-to-buffer
2022 nil
2023 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2024 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2025
2026 ;;; Agenda prepare and finalize
2027
2028 (defvar org-agenda-multi nil) ; dynammically scoped
2029 (defvar org-agenda-buffer-name "*Org Agenda*")
2030 (defvar org-pre-agenda-window-conf nil)
2031 (defvar org-agenda-columns-active nil)
2032 (defvar org-agenda-name nil)
2033 (defvar org-agenda-filter nil)
2034 (defun org-prepare-agenda (&optional name)
2035 (setq org-todo-keywords-for-agenda nil)
2036 (setq org-done-keywords-for-agenda nil)
2037 (setq org-agenda-filter nil)
2038 (if org-agenda-multi
2039 (progn
2040 (setq buffer-read-only nil)
2041 (goto-char (point-max))
2042 (unless (or (bobp) org-agenda-compact-blocks)
2043 (insert "\n" (make-string (window-width) ?=) "\n"))
2044 (narrow-to-region (point) (point-max)))
2045 (org-agenda-reset-markers)
2046 (setq org-agenda-contributing-files nil)
2047 (setq org-agenda-columns-active nil)
2048 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2049 (setq org-todo-keywords-for-agenda
2050 (org-uniquify org-todo-keywords-for-agenda))
2051 (setq org-done-keywords-for-agenda
2052 (org-uniquify org-done-keywords-for-agenda))
2053 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2054 (awin (get-buffer-window abuf)))
2055 (cond
2056 ((equal (current-buffer) abuf) nil)
2057 (awin (select-window awin))
2058 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2059 ((equal org-agenda-window-setup 'current-window)
2060 (switch-to-buffer abuf))
2061 ((equal org-agenda-window-setup 'other-window)
2062 (org-switch-to-buffer-other-window abuf))
2063 ((equal org-agenda-window-setup 'other-frame)
2064 (switch-to-buffer-other-frame abuf))
2065 ((equal org-agenda-window-setup 'reorganize-frame)
2066 (delete-other-windows)
2067 (org-switch-to-buffer-other-window abuf))))
2068 (setq buffer-read-only nil)
2069 (let ((inhibit-read-only t)) (erase-buffer))
2070 (org-agenda-mode)
2071 (and name (not org-agenda-name)
2072 (org-set-local 'org-agenda-name name)))
2073 (setq buffer-read-only nil))
2074
2075 (defun org-finalize-agenda ()
2076 "Finishing touch for the agenda buffer, called just before displaying it."
2077 (unless org-agenda-multi
2078 (save-excursion
2079 (let ((inhibit-read-only t))
2080 (goto-char (point-min))
2081 (while (org-activate-bracket-links (point-max))
2082 (add-text-properties (match-beginning 0) (match-end 0)
2083 '(face org-link)))
2084 (org-agenda-align-tags)
2085 (unless org-agenda-with-colors
2086 (remove-text-properties (point-min) (point-max) '(face nil))))
2087 (if (and (boundp 'org-overriding-columns-format)
2088 org-overriding-columns-format)
2089 (org-set-local 'org-overriding-columns-format
2090 org-overriding-columns-format))
2091 (if (and (boundp 'org-agenda-view-columns-initially)
2092 org-agenda-view-columns-initially)
2093 (org-agenda-columns))
2094 (when org-agenda-fontify-priorities
2095 (org-fontify-priorities))
2096 (run-hooks 'org-finalize-agenda-hook)
2097 (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
2098 )))
2099
2100 (defun org-fontify-priorities ()
2101 "Make highest priority lines bold, and lowest italic."
2102 (interactive)
2103 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2104 (org-delete-overlay o)))
2105 (org-overlays-in (point-min) (point-max)))
2106 (save-excursion
2107 (let ((inhibit-read-only t)
2108 b e p ov h l)
2109 (goto-char (point-min))
2110 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2111 (setq h (or (get-char-property (point) 'org-highest-priority)
2112 org-highest-priority)
2113 l (or (get-char-property (point) 'org-lowest-priority)
2114 org-lowest-priority)
2115 p (string-to-char (match-string 1))
2116 b (match-beginning 0) e (point-at-eol)
2117 ov (org-make-overlay b e))
2118 (org-overlay-put
2119 ov 'face
2120 (cond ((listp org-agenda-fontify-priorities)
2121 (cdr (assoc p org-agenda-fontify-priorities)))
2122 ((equal p l) 'italic)
2123 ((equal p h) 'bold)))
2124 (org-overlay-put ov 'org-type 'org-priority)))))
2125
2126
2127 (defvar org-agenda-skip-function nil
2128 "Function to be called at each match during agenda construction.
2129 If this function returns nil, the current match should not be skipped.
2130 Otherwise, the function must return a position from where the search
2131 should be continued.
2132 This may also be a Lisp form, it will be evaluated.
2133 Never set this variable using `setq' or so, because then it will apply
2134 to all future agenda commands. Instead, bind it with `let' to scope
2135 it dynamically into the agenda-constructing command. A good way to set
2136 it is through options in org-agenda-custom-commands.")
2137
2138 (defun org-agenda-skip ()
2139 "Throw to `:skip' in places that should be skipped.
2140 Also moves point to the end of the skipped region, so that search can
2141 continue from there."
2142 (let ((p (point-at-bol)) to fp)
2143 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2144 (get-text-property p :org-archived)
2145 (org-end-of-subtree t)
2146 (throw :skip t))
2147 (and org-agenda-skip-comment-trees
2148 (get-text-property p :org-comment)
2149 (org-end-of-subtree t)
2150 (throw :skip t))
2151 (if (equal (char-after p) ?#) (throw :skip t))
2152 (when (and (or (setq fp (functionp org-agenda-skip-function))
2153 (consp org-agenda-skip-function))
2154 (setq to (save-excursion
2155 (save-match-data
2156 (if fp
2157 (funcall org-agenda-skip-function)
2158 (eval org-agenda-skip-function))))))
2159 (goto-char to)
2160 (throw :skip t))))
2161
2162 (defvar org-agenda-markers nil
2163 "List of all currently active markers created by `org-agenda'.")
2164 (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
2165 "Creation time of the last agenda marker.")
2166
2167 (defun org-agenda-new-marker (&optional pos)
2168 "Return a new agenda marker.
2169 Org-mode keeps a list of these markers and resets them when they are
2170 no longer in use."
2171 (let ((m (copy-marker (or pos (point)))))
2172 (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
2173 (push m org-agenda-markers)
2174 m))
2175
2176 (defun org-agenda-reset-markers ()
2177 "Reset markers created by `org-agenda'."
2178 (while org-agenda-markers
2179 (move-marker (pop org-agenda-markers) nil)))
2180
2181 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2182 "Save relative positions of markers in region."
2183 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2184 org-agenda-markers))
2185
2186 ;;; Agenda timeline
2187
2188 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2189
2190 (defun org-timeline (&optional include-all)
2191 "Show a time-sorted view of the entries in the current org file.
2192 Only entries with a time stamp of today or later will be listed. With
2193 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2194 under the current date.
2195 If the buffer contains an active region, only check the region for
2196 dates."
2197 (interactive "P")
2198 (require 'calendar)
2199 (org-compile-prefix-format 'timeline)
2200 (org-set-sorting-strategy 'timeline)
2201 (let* ((dopast t)
2202 (dotodo include-all)
2203 (doclosed org-agenda-show-log)
2204 (entry buffer-file-name)
2205 (date (calendar-current-date))
2206 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2207 (end (if (org-region-active-p) (region-end) (point-max)))
2208 (day-numbers (org-get-all-dates beg end 'no-ranges
2209 t doclosed ; always include today
2210 org-timeline-show-empty-dates))
2211 (org-deadline-warning-days 0)
2212 (org-agenda-only-exact-dates t)
2213 (today (time-to-days (current-time)))
2214 (past t)
2215 args
2216 s e rtn d emptyp wd)
2217 (setq org-agenda-redo-command
2218 (list 'progn
2219 (list 'org-switch-to-buffer-other-window (current-buffer))
2220 (list 'org-timeline (list 'quote include-all))))
2221 (if (not dopast)
2222 ;; Remove past dates from the list of dates.
2223 (setq day-numbers (delq nil (mapcar (lambda(x)
2224 (if (>= x today) x nil))
2225 day-numbers))))
2226 (org-prepare-agenda (concat "Timeline "
2227 (file-name-nondirectory buffer-file-name)))
2228 (if doclosed (push :closed args))
2229 (push :timestamp args)
2230 (push :deadline args)
2231 (push :scheduled args)
2232 (push :sexp args)
2233 (if dotodo (push :todo args))
2234 (while (setq d (pop day-numbers))
2235 (if (and (listp d) (eq (car d) :omitted))
2236 (progn
2237 (setq s (point))
2238 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2239 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2240 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2241 (if (and (>= d today)
2242 dopast
2243 past)
2244 (progn
2245 (setq past nil)
2246 (insert (make-string 79 ?-) "\n")))
2247 (setq date (calendar-gregorian-from-absolute d)
2248 wd (calendar-day-of-week date))
2249 (setq s (point))
2250 (setq rtn (and (not emptyp)
2251 (apply 'org-agenda-get-day-entries entry
2252 date args)))
2253 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2254 (progn
2255 (insert
2256 (if (stringp org-agenda-format-date)
2257 (format-time-string org-agenda-format-date
2258 (org-time-from-absolute date))
2259 (funcall org-agenda-format-date date))
2260 "\n")
2261 (put-text-property s (1- (point)) 'face
2262 (if (member wd org-agenda-weekend-days)
2263 'org-agenda-date-weekend
2264 'org-agenda-date))
2265 (put-text-property s (1- (point)) 'org-date-line t)
2266 (if (equal d today)
2267 (put-text-property s (1- (point)) 'org-today t))
2268 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2269 (put-text-property s (1- (point)) 'day d)))))
2270 (goto-char (point-min))
2271 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2272 (point-min)))
2273 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2274 (org-finalize-agenda)
2275 (setq buffer-read-only t)))
2276
2277 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2278 "Return a list of all relevant day numbers from BEG to END buffer positions.
2279 If NO-RANGES is non-nil, include only the start and end dates of a range,
2280 not every single day in the range. If FORCE-TODAY is non-nil, make
2281 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2282 inactive time stamps (those in square brackets) are included.
2283 When EMPTY is non-nil, also include days without any entries."
2284 (let ((re (concat
2285 (if pre-re pre-re "")
2286 (if inactive org-ts-regexp-both org-ts-regexp)))
2287 dates dates1 date day day1 day2 ts1 ts2)
2288 (if force-today
2289 (setq dates (list (time-to-days (current-time)))))
2290 (save-excursion
2291 (goto-char beg)
2292 (while (re-search-forward re end t)
2293 (setq day (time-to-days (org-time-string-to-time
2294 (substring (match-string 1) 0 10))))
2295 (or (memq day dates) (push day dates)))
2296 (unless no-ranges
2297 (goto-char beg)
2298 (while (re-search-forward org-tr-regexp end t)
2299 (setq ts1 (substring (match-string 1) 0 10)
2300 ts2 (substring (match-string 2) 0 10)
2301 day1 (time-to-days (org-time-string-to-time ts1))
2302 day2 (time-to-days (org-time-string-to-time ts2)))
2303 (while (< (setq day1 (1+ day1)) day2)
2304 (or (memq day1 dates) (push day1 dates)))))
2305 (setq dates (sort dates '<))
2306 (when empty
2307 (while (setq day (pop dates))
2308 (setq day2 (car dates))
2309 (push day dates1)
2310 (when (and day2 empty)
2311 (if (or (eq empty t)
2312 (and (numberp empty) (<= (- day2 day) empty)))
2313 (while (< (setq day (1+ day)) day2)
2314 (push (list day) dates1))
2315 (push (cons :omitted (- day2 day)) dates1))))
2316 (setq dates (nreverse dates1)))
2317 dates)))
2318
2319 ;;; Agenda Daily/Weekly
2320
2321 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2322 (defvar org-agenda-start-day nil) ; dynamically scoped parameter
2323 (defvar org-agenda-last-arguments nil
2324 "The arguments of the previous call to org-agenda")
2325 (defvar org-starting-day nil) ; local variable in the agenda buffer
2326 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2327 (defvar org-include-all-loc nil) ; local variable
2328 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2329
2330 ;;;###autoload
2331 (defun org-agenda-list (&optional include-all start-day ndays)
2332 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2333 The view will be for the current day or week, but from the overview buffer
2334 you will be able to go to other days/weeks.
2335
2336 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2337 all unfinished TODO items will also be shown, before the agenda.
2338 This feature is considered obsolete, please use the TODO list or a block
2339 agenda instead.
2340
2341 With a numeric prefix argument in an interactive call, the agenda will
2342 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2343 the number of days. NDAYS defaults to `org-agenda-ndays'.
2344
2345 START-DAY defaults to TODAY, or to the most recent match for the weekday
2346 given in `org-agenda-start-on-weekday'."
2347 (interactive "P")
2348 (if (and (integerp include-all) (> include-all 0))
2349 (setq ndays include-all include-all nil))
2350 (setq ndays (or ndays org-agenda-ndays)
2351 start-day (or start-day org-agenda-start-day))
2352 (if org-agenda-overriding-arguments
2353 (setq include-all (car org-agenda-overriding-arguments)
2354 start-day (nth 1 org-agenda-overriding-arguments)
2355 ndays (nth 2 org-agenda-overriding-arguments)))
2356 (if (stringp start-day)
2357 ;; Convert to an absolute day number
2358 (setq start-day (time-to-days (org-read-date nil t start-day))))
2359 (setq org-agenda-last-arguments (list include-all start-day ndays))
2360 (org-compile-prefix-format 'agenda)
2361 (org-set-sorting-strategy 'agenda)
2362 (require 'calendar)
2363 (let* ((org-agenda-start-on-weekday
2364 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
2365 org-agenda-start-on-weekday nil))
2366 (thefiles (org-agenda-files nil 'ifmode))
2367 (files thefiles)
2368 (today (time-to-days
2369 (time-subtract (current-time)
2370 (list 0 (* 3600 org-extend-today-until) 0))))
2371 (sd (or start-day today))
2372 (start (if (or (null org-agenda-start-on-weekday)
2373 (< org-agenda-ndays 7))
2374 sd
2375 (let* ((nt (calendar-day-of-week
2376 (calendar-gregorian-from-absolute sd)))
2377 (n1 org-agenda-start-on-weekday)
2378 (d (- nt n1)))
2379 (- sd (+ (if (< d 0) 7 0) d)))))
2380 (day-numbers (list start))
2381 (day-cnt 0)
2382 (inhibit-redisplay (not debug-on-error))
2383 s e rtn rtnall file date d start-pos end-pos todayp nd wd
2384 clocktable-start clocktable-end)
2385 (setq org-agenda-redo-command
2386 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
2387 ;; Make the list of days
2388 (setq ndays (or ndays org-agenda-ndays)
2389 nd ndays)
2390 (while (> ndays 1)
2391 (push (1+ (car day-numbers)) day-numbers)
2392 (setq ndays (1- ndays)))
2393 (setq day-numbers (nreverse day-numbers))
2394 (setq clocktable-start (car day-numbers)
2395 clocktable-end (1+ (or (org-last day-numbers) 0)))
2396 (org-prepare-agenda "Day/Week")
2397 (org-set-local 'org-starting-day (car day-numbers))
2398 (org-set-local 'org-include-all-loc include-all)
2399 (org-set-local 'org-agenda-span
2400 (org-agenda-ndays-to-span nd))
2401 (when (and (or include-all org-agenda-include-all-todo)
2402 (member today day-numbers))
2403 (setq files thefiles
2404 rtnall nil)
2405 (while (setq file (pop files))
2406 (catch 'nextfile
2407 (org-check-agenda-file file)
2408 (setq date (calendar-gregorian-from-absolute today)
2409 rtn (org-agenda-get-day-entries
2410 file date :todo))
2411 (setq rtnall (append rtnall rtn))))
2412 (when rtnall
2413 (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
2414 (add-text-properties (point-min) (1- (point))
2415 (list 'face 'org-agenda-structure))
2416 (insert (org-finalize-agenda-entries rtnall) "\n")))
2417 (unless org-agenda-compact-blocks
2418 (let* ((d1 (car day-numbers))
2419 (d2 (org-last day-numbers))
2420 (w1 (org-days-to-iso-week d1))
2421 (w2 (org-days-to-iso-week d2)))
2422 (setq s (point))
2423 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
2424 "-agenda"
2425 (if (< (- d2 d1) 350)
2426 (if (= w1 w2)
2427 (format " (W%02d)" w1)
2428 (format " (W%02d-W%02d)" w1 w2))
2429 "")
2430 ":\n"))
2431 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
2432 'org-date-line t)))
2433 (while (setq d (pop day-numbers))
2434 (setq date (calendar-gregorian-from-absolute d)
2435 wd (calendar-day-of-week date)
2436 s (point))
2437 (if (or (setq todayp (= d today))
2438 (and (not start-pos) (= d sd)))
2439 (setq start-pos (point))
2440 (if (and start-pos (not end-pos))
2441 (setq end-pos (point))))
2442 (setq files thefiles
2443 rtnall nil)
2444 (while (setq file (pop files))
2445 (catch 'nextfile
2446 (org-check-agenda-file file)
2447 (cond
2448 ((eq org-agenda-show-log 'only)
2449 (setq rtn (org-agenda-get-day-entries
2450 file date :closed)))
2451 (org-agenda-show-log
2452 (setq rtn (org-agenda-get-day-entries
2453 file date
2454 :deadline :scheduled :timestamp :sexp :closed)))
2455 (t
2456 (setq rtn (org-agenda-get-day-entries
2457 file date
2458 :deadline :scheduled :sexp :timestamp))))
2459 (setq rtnall (append rtnall rtn))))
2460 (if org-agenda-include-diary
2461 (progn
2462 (require 'diary-lib)
2463 (setq rtn (org-get-entries-from-diary date))
2464 (setq rtnall (append rtnall rtn))))
2465 (if (or rtnall org-agenda-show-all-dates)
2466 (progn
2467 (setq day-cnt (1+ day-cnt))
2468 (insert
2469 (if (stringp org-agenda-format-date)
2470 (format-time-string org-agenda-format-date
2471 (org-time-from-absolute date))
2472 (funcall org-agenda-format-date date))
2473 "\n")
2474 (put-text-property s (1- (point)) 'face
2475 (if (member wd org-agenda-weekend-days)
2476 'org-agenda-date-weekend
2477 'org-agenda-date))
2478 (put-text-property s (1- (point)) 'org-date-line t)
2479 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
2480 (if todayp (put-text-property s (1- (point)) 'org-today t))
2481 (if rtnall (insert
2482 (org-finalize-agenda-entries
2483 (org-agenda-add-time-grid-maybe
2484 rtnall nd todayp))
2485 "\n"))
2486 (put-text-property s (1- (point)) 'day d)
2487 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
2488 (when (and org-agenda-clockreport-mode clocktable-start)
2489 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
2490 ;; the above line is to ensure the restricted range!
2491 (p org-agenda-clockreport-parameter-plist)
2492 tbl)
2493 (setq p (org-plist-delete p :block))
2494 (setq p (plist-put p :tstart clocktable-start))
2495 (setq p (plist-put p :tend clocktable-end))
2496 (setq p (plist-put p :scope 'agenda))
2497 (setq tbl (apply 'org-get-clocktable p))
2498 (insert tbl)))
2499 (goto-char (point-min))
2500 (org-fit-agenda-window)
2501 (unless (and (pos-visible-in-window-p (point-min))
2502 (pos-visible-in-window-p (point-max)))
2503 (goto-char (1- (point-max)))
2504 (recenter -1)
2505 (if (not (pos-visible-in-window-p (or start-pos 1)))
2506 (progn
2507 (goto-char (or start-pos 1))
2508 (recenter 1))))
2509 (goto-char (or start-pos 1))
2510 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
2511 (org-finalize-agenda)
2512 (setq buffer-read-only t)
2513 (message "")))
2514
2515 (defun org-agenda-ndays-to-span (n)
2516 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
2517
2518 ;;; Agenda word search
2519
2520 (defvar org-agenda-search-history nil)
2521 (defvar org-todo-only nil)
2522
2523 (defvar org-search-syntax-table nil
2524 "Special syntax table for org-mode search.
2525 In this table, we have single quotes not as word constituents, to
2526 that when \"+Ameli\" is searchd as a work, it will also match \"Ameli's\"")
2527
2528 (defun org-search-syntax-table ()
2529 (unless org-search-syntax-table
2530 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
2531 (modify-syntax-entry ?' "." org-search-syntax-table)
2532 (modify-syntax-entry ?` "." org-search-syntax-table))
2533 org-search-syntax-table)
2534
2535 ;;;###autoload
2536 (defun org-search-view (&optional todo-only string edit-at)
2537 "Show all entries that contain words or regular expressions.
2538 If the first character of the search string is an asterisks,
2539 search only the headlines.
2540
2541 With optional prefix argument TODO-ONLY, only consider entries that are
2542 TODO entries. The argument STRING can be used to pass a default search
2543 string into this function. If EDIT-AT is non-nil, it means that the
2544 user should get a chance to edit this string, with cursor at position
2545 EDIT-AT.
2546
2547 The search string is broken into \"words\" by splitting at whitespace.
2548 The individual words are then interpreted as a boolean expression with
2549 logical AND. Words prefixed with a minus must not occur in the entry.
2550 Words without a prefix or prefixed with a plus must occur in the entry.
2551 Matching is case-insensitive and the words are enclosed by word delimiters.
2552
2553 Words enclosed by curly braces are interpreted as regular expressions
2554 that must or must not match in the entry.
2555
2556 If the search string starts with an asterisk, search only in headlines.
2557 If (possibly after the leading star) the search string starts with an
2558 exclamation mark, this also means to look at TODO entries only, an effect
2559 that can also be achieved with a prefix argument.
2560
2561 This command searches the agenda files, and in addition the files listed
2562 in `org-agenda-text-search-extra-files'."
2563 (interactive "P")
2564 (org-compile-prefix-format 'search)
2565 (org-set-sorting-strategy 'search)
2566 (org-prepare-agenda "SEARCH")
2567 (let* ((props (list 'face nil
2568 'done-face 'org-done
2569 'org-not-done-regexp org-not-done-regexp
2570 'org-todo-regexp org-todo-regexp
2571 'org-complex-heading-regexp org-complex-heading-regexp
2572 'mouse-face 'highlight
2573 'keymap org-agenda-keymap
2574 'help-echo (format "mouse-2 or RET jump to location")))
2575 regexp rtn rtnall files file pos
2576 marker priority category tags c neg re
2577 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
2578 (unless (and (not edit-at)
2579 (stringp string)
2580 (string-match "\\S-" string))
2581 (setq string (read-string "[+-]Word/{Regexp} ...: "
2582 (cond
2583 ((integerp edit-at) (cons string edit-at))
2584 (edit-at string))
2585 'org-agenda-search-history)))
2586 (org-set-local 'org-todo-only todo-only)
2587 (setq org-agenda-redo-command
2588 (list 'org-search-view (if todo-only t nil) string
2589 '(if current-prefix-arg 1 nil)))
2590 (setq org-agenda-query-string string)
2591
2592 (if (equal (string-to-char string) ?*)
2593 (setq hdl-only t
2594 words (substring string 1))
2595 (setq words string))
2596 (when (equal (string-to-char words) ?!)
2597 (setq todo-only t
2598 words (substring words 1)))
2599 (setq words (org-split-string words))
2600 (mapc (lambda (w)
2601 (setq c (string-to-char w))
2602 (if (equal c ?-)
2603 (setq neg t w (substring w 1))
2604 (if (equal c ?+)
2605 (setq neg nil w (substring w 1))
2606 (setq neg nil)))
2607 (if (string-match "\\`{.*}\\'" w)
2608 (setq re (substring w 1 -1))
2609 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
2610 (if neg (push re regexps-) (push re regexps+)))
2611 words)
2612 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
2613 (if (not regexps+)
2614 (setq regexp (concat "^" org-outline-regexp))
2615 (setq regexp (pop regexps+))
2616 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
2617 regexp))))
2618 (setq files (org-agenda-files nil 'ifmode))
2619 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
2620 (pop org-agenda-text-search-extra-files)
2621 (setq files (org-add-archive-files files)))
2622 (setq files (append files org-agenda-text-search-extra-files)
2623 rtnall nil)
2624 (while (setq file (pop files))
2625 (setq ee nil)
2626 (catch 'nextfile
2627 (org-check-agenda-file file)
2628 (setq buffer (if (file-exists-p file)
2629 (org-get-agenda-file-buffer file)
2630 (error "No such file %s" file)))
2631 (if (not buffer)
2632 ;; If file does not exist, make sure an error message is sent
2633 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
2634 file))))
2635 (with-current-buffer buffer
2636 (with-syntax-table (org-search-syntax-table)
2637 (unless (org-mode-p)
2638 (error "Agenda file %s is not in `org-mode'" file))
2639 (let ((case-fold-search t))
2640 (save-excursion
2641 (save-restriction
2642 (if org-agenda-restrict
2643 (narrow-to-region org-agenda-restrict-begin
2644 org-agenda-restrict-end)
2645 (widen))
2646 (goto-char (point-min))
2647 (unless (or (org-on-heading-p)
2648 (outline-next-heading))
2649 (throw 'nextfile t))
2650 (goto-char (max (point-min) (1- (point))))
2651 (while (re-search-forward regexp nil t)
2652 (org-back-to-heading t)
2653 (skip-chars-forward "* ")
2654 (setq beg (point-at-bol)
2655 beg1 (point)
2656 end (progn (outline-next-heading) (point)))
2657 (catch :skip
2658 (goto-char beg)
2659 (org-agenda-skip)
2660 (setq str (buffer-substring-no-properties
2661 (point-at-bol)
2662 (if hdl-only (point-at-eol) end)))
2663 (mapc (lambda (wr) (when (string-match wr str)
2664 (goto-char (1- end))
2665 (throw :skip t)))
2666 regexps-)
2667 (mapc (lambda (wr) (unless (string-match wr str)
2668 (goto-char (1- end))
2669 (throw :skip t)))
2670 (if todo-only
2671 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
2672 regexps+)
2673 regexps+))
2674 (goto-char beg)
2675 (setq marker (org-agenda-new-marker (point))
2676 category (org-get-category)
2677 tags (org-get-tags-at (point))
2678 txt (org-format-agenda-item
2679 ""
2680 (buffer-substring-no-properties
2681 beg1 (point-at-eol))
2682 category tags))
2683 (org-add-props txt props
2684 'org-marker marker 'org-hd-marker marker
2685 'org-todo-regexp org-todo-regexp
2686 'org-complex-heading-regexp org-complex-heading-regexp
2687 'priority 1000 'org-category category
2688 'type "search")
2689 (push txt ee)
2690 (goto-char (1- end))))))))))
2691 (setq rtn (nreverse ee))
2692 (setq rtnall (append rtnall rtn)))
2693 (if org-agenda-overriding-header
2694 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2695 nil 'face 'org-agenda-structure) "\n")
2696 (insert "Search words: ")
2697 (add-text-properties (point-min) (1- (point))
2698 (list 'face 'org-agenda-structure))
2699 (setq pos (point))
2700 (insert string "\n")
2701 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2702 (setq pos (point))
2703 (unless org-agenda-multi
2704 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
2705 (add-text-properties pos (1- (point))
2706 (list 'face 'org-agenda-structure))))
2707 (when rtnall
2708 (insert (org-finalize-agenda-entries rtnall) "\n"))
2709 (goto-char (point-min))
2710 (org-fit-agenda-window)
2711 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
2712 (org-finalize-agenda)
2713 (setq buffer-read-only t)))
2714
2715 ;;; Agenda TODO list
2716
2717 (defvar org-select-this-todo-keyword nil)
2718 (defvar org-last-arg nil)
2719
2720 ;;;###autoload
2721 (defun org-todo-list (arg)
2722 "Show all TODO entries from all agenda file in a single list.
2723 The prefix arg can be used to select a specific TODO keyword and limit
2724 the list to these. When using \\[universal-argument], you will be prompted
2725 for a keyword. A numeric prefix directly selects the Nth keyword in
2726 `org-todo-keywords-1'."
2727 (interactive "P")
2728 (require 'calendar)
2729 (org-compile-prefix-format 'todo)
2730 (org-set-sorting-strategy 'todo)
2731 (org-prepare-agenda "TODO")
2732 (let* ((today (time-to-days (current-time)))
2733 (date (calendar-gregorian-from-absolute today))
2734 (kwds org-todo-keywords-for-agenda)
2735 (completion-ignore-case t)
2736 (org-select-this-todo-keyword
2737 (if (stringp arg) arg
2738 (and arg (integerp arg) (> arg 0)
2739 (nth (1- arg) kwds))))
2740 rtn rtnall files file pos)
2741 (when (equal arg '(4))
2742 (setq org-select-this-todo-keyword
2743 (org-ido-completing-read "Keyword (or KWD1|K2D2|...): "
2744 (mapcar 'list kwds) nil nil)))
2745 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
2746 (org-set-local 'org-last-arg arg)
2747 (setq org-agenda-redo-command
2748 '(org-todo-list (or current-prefix-arg org-last-arg)))
2749 (setq files (org-agenda-files nil 'ifmode)
2750 rtnall nil)
2751 (while (setq file (pop files))
2752 (catch 'nextfile
2753 (org-check-agenda-file file)
2754 (setq rtn (org-agenda-get-day-entries file date :todo))
2755 (setq rtnall (append rtnall rtn))))
2756 (if org-agenda-overriding-header
2757 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2758 nil 'face 'org-agenda-structure) "\n")
2759 (insert "Global list of TODO items of type: ")
2760 (add-text-properties (point-min) (1- (point))
2761 (list 'face 'org-agenda-structure))
2762 (setq pos (point))
2763 (insert (or org-select-this-todo-keyword "ALL") "\n")
2764 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2765 (setq pos (point))
2766 (unless org-agenda-multi
2767 (insert "Available with `N r': (0)ALL")
2768 (let ((n 0) s)
2769 (mapc (lambda (x)
2770 (setq s (format "(%d)%s" (setq n (1+ n)) x))
2771 (if (> (+ (current-column) (string-width s) 1) (frame-width))
2772 (insert "\n "))
2773 (insert " " s))
2774 kwds))
2775 (insert "\n"))
2776 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
2777 (when rtnall
2778 (insert (org-finalize-agenda-entries rtnall) "\n"))
2779 (goto-char (point-min))
2780 (org-fit-agenda-window)
2781 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
2782 (org-finalize-agenda)
2783 (setq buffer-read-only t)))
2784
2785 ;;; Agenda tags match
2786
2787 ;;;###autoload
2788 (defun org-tags-view (&optional todo-only match)
2789 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
2790 The prefix arg TODO-ONLY limits the search to TODO entries."
2791 (interactive "P")
2792 (org-compile-prefix-format 'tags)
2793 (org-set-sorting-strategy 'tags)
2794 (let* ((org-tags-match-list-sublevels
2795 (if todo-only t org-tags-match-list-sublevels))
2796 (completion-ignore-case t)
2797 rtn rtnall files file pos matcher
2798 buffer)
2799 (setq matcher (org-make-tags-matcher match)
2800 match (car matcher) matcher (cdr matcher))
2801 (org-prepare-agenda (concat "TAGS " match))
2802 (setq org-agenda-query-string match)
2803 (setq org-agenda-redo-command
2804 (list 'org-tags-view (list 'quote todo-only)
2805 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
2806 (setq files (org-agenda-files nil 'ifmode)
2807 rtnall nil)
2808 (while (setq file (pop files))
2809 (catch 'nextfile
2810 (org-check-agenda-file file)
2811 (setq buffer (if (file-exists-p file)
2812 (org-get-agenda-file-buffer file)
2813 (error "No such file %s" file)))
2814 (if (not buffer)
2815 ;; If file does not exist, merror message to agenda
2816 (setq rtn (list
2817 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
2818 rtnall (append rtnall rtn))
2819 (with-current-buffer buffer
2820 (unless (org-mode-p)
2821 (error "Agenda file %s is not in `org-mode'" file))
2822 (save-excursion
2823 (save-restriction
2824 (if org-agenda-restrict
2825 (narrow-to-region org-agenda-restrict-begin
2826 org-agenda-restrict-end)
2827 (widen))
2828 (setq rtn (org-scan-tags 'agenda matcher todo-only))
2829 (setq rtnall (append rtnall rtn))))))))
2830 (if org-agenda-overriding-header
2831 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
2832 nil 'face 'org-agenda-structure) "\n")
2833 (insert "Headlines with TAGS match: ")
2834 (add-text-properties (point-min) (1- (point))
2835 (list 'face 'org-agenda-structure))
2836 (setq pos (point))
2837 (insert match "\n")
2838 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
2839 (setq pos (point))
2840 (unless org-agenda-multi
2841 (insert "Press `C-u r' to search again with new search string\n"))
2842 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
2843 (when rtnall
2844 (insert (org-finalize-agenda-entries rtnall) "\n"))
2845 (goto-char (point-min))
2846 (org-fit-agenda-window)
2847 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
2848 (org-finalize-agenda)
2849 (setq buffer-read-only t)))
2850
2851 ;;; Agenda Finding stuck projects
2852
2853 (defvar org-agenda-skip-regexp nil
2854 "Regular expression used in skipping subtrees for the agenda.
2855 This is basically a temporary global variable that can be set and then
2856 used by user-defined selections using `org-agenda-skip-function'.")
2857
2858 (defvar org-agenda-overriding-header nil
2859 "When this is set during todo and tags searches, will replace header.")
2860
2861 (defun org-agenda-skip-subtree-when-regexp-matches ()
2862 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
2863 If yes, it returns the end position of this tree, causing agenda commands
2864 to skip this subtree. This is a function that can be put into
2865 `org-agenda-skip-function' for the duration of a command."
2866 (let ((end (save-excursion (org-end-of-subtree t)))
2867 skip)
2868 (save-excursion
2869 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
2870 (and skip end)))
2871
2872 (defun org-agenda-skip-entry-if (&rest conditions)
2873 "Skip entry if any of CONDITIONS is true.
2874 See `org-agenda-skip-if' for details."
2875 (org-agenda-skip-if nil conditions))
2876
2877 (defun org-agenda-skip-subtree-if (&rest conditions)
2878 "Skip entry if any of CONDITIONS is true.
2879 See `org-agenda-skip-if' for details."
2880 (org-agenda-skip-if t conditions))
2881
2882 (defun org-agenda-skip-if (subtree conditions)
2883 "Checks current entity for CONDITIONS.
2884 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
2885 the entry, i.e. the text before the next heading is checked.
2886
2887 CONDITIONS is a list of symbols, boolean OR is used to combine the results
2888 from different tests. Valid conditions are:
2889
2890 scheduled Check if there is a scheduled cookie
2891 notscheduled Check if there is no scheduled cookie
2892 deadline Check if there is a deadline
2893 notdeadline Check if there is no deadline
2894 regexp Check if regexp matches
2895 notregexp Check if regexp does not match.
2896
2897 The regexp is taken from the conditions list, it must come right after
2898 the `regexp' or `notregexp' element.
2899
2900 If any of these conditions is met, this function returns the end point of
2901 the entity, causing the search to continue from there. This is a function
2902 that can be put into `org-agenda-skip-function' for the duration of a command."
2903 (let (beg end m)
2904 (org-back-to-heading t)
2905 (setq beg (point)
2906 end (if subtree
2907 (progn (org-end-of-subtree t) (point))
2908 (progn (outline-next-heading) (1- (point)))))
2909 (goto-char beg)
2910 (and
2911 (or
2912 (and (memq 'scheduled conditions)
2913 (re-search-forward org-scheduled-time-regexp end t))
2914 (and (memq 'notscheduled conditions)
2915 (not (re-search-forward org-scheduled-time-regexp end t)))
2916 (and (memq 'deadline conditions)
2917 (re-search-forward org-deadline-time-regexp end t))
2918 (and (memq 'notdeadline conditions)
2919 (not (re-search-forward org-deadline-time-regexp end t)))
2920 (and (setq m (memq 'regexp conditions))
2921 (stringp (nth 1 m))
2922 (re-search-forward (nth 1 m) end t))
2923 (and (setq m (memq 'notregexp conditions))
2924 (stringp (nth 1 m))
2925 (not (re-search-forward (nth 1 m) end t))))
2926 end)))
2927
2928 ;;;###autoload
2929 (defun org-agenda-list-stuck-projects (&rest ignore)
2930 "Create agenda view for projects that are stuck.
2931 Stuck projects are project that have no next actions. For the definitions
2932 of what a project is and how to check if it stuck, customize the variable
2933 `org-stuck-projects'.
2934 MATCH is being ignored."
2935 (interactive)
2936 (let* ((org-agenda-skip-function 'org-agenda-skip-subtree-when-regexp-matches)
2937 ;; We could have used org-agenda-skip-if here.
2938 (org-agenda-overriding-header "List of stuck projects: ")
2939 (matcher (nth 0 org-stuck-projects))
2940 (todo (nth 1 org-stuck-projects))
2941 (todo-wds (if (member "*" todo)
2942 (progn
2943 (org-prepare-agenda-buffers (org-agenda-files
2944 nil 'ifmode))
2945 (org-delete-all
2946 org-done-keywords-for-agenda
2947 (copy-sequence org-todo-keywords-for-agenda)))
2948 todo))
2949 (todo-re (concat "^\\*+[ \t]+\\("
2950 (mapconcat 'identity todo-wds "\\|")
2951 "\\)\\>"))
2952 (tags (nth 2 org-stuck-projects))
2953 (tags-re (if (member "*" tags)
2954 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
2955 (concat "^\\*+ .*:\\("
2956 (mapconcat 'identity tags "\\|")
2957 (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
2958 (gen-re (nth 3 org-stuck-projects))
2959 (re-list
2960 (delq nil
2961 (list
2962 (if todo todo-re)
2963 (if tags tags-re)
2964 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
2965 gen-re)))))
2966 (setq org-agenda-skip-regexp
2967 (if re-list
2968 (mapconcat 'identity re-list "\\|")
2969 (error "No information how to identify unstuck projects")))
2970 (org-tags-view nil matcher)
2971 (with-current-buffer org-agenda-buffer-name
2972 (setq org-agenda-redo-command
2973 '(org-agenda-list-stuck-projects
2974 (or current-prefix-arg org-last-arg))))))
2975
2976 ;;; Diary integration
2977
2978 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
2979 (defvar list-diary-entries-hook)
2980
2981 (defun org-get-entries-from-diary (date)
2982 "Get the (Emacs Calendar) diary entries for DATE."
2983 (require 'diary-lib)
2984 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
2985 (fancy-diary-buffer diary-fancy-buffer)
2986 (diary-display-hook '(fancy-diary-display))
2987 (diary-display-function 'fancy-diary-display)
2988 (pop-up-frames nil)
2989 (list-diary-entries-hook
2990 (cons 'org-diary-default-entry list-diary-entries-hook))
2991 (diary-file-name-prefix-function nil) ; turn this feature off
2992 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
2993 entries
2994 (org-disable-agenda-to-diary t))
2995 (save-excursion
2996 (save-window-excursion
2997 (funcall (if (fboundp 'diary-list-entries)
2998 'diary-list-entries 'list-diary-entries)
2999 date 1)))
3000 (if (not (get-buffer diary-fancy-buffer))
3001 (setq entries nil)
3002 (with-current-buffer diary-fancy-buffer
3003 (setq buffer-read-only nil)
3004 (if (zerop (buffer-size))
3005 ;; No entries
3006 (setq entries nil)
3007 ;; Omit the date and other unnecessary stuff
3008 (org-agenda-cleanup-fancy-diary)
3009 ;; Add prefix to each line and extend the text properties
3010 (if (zerop (buffer-size))
3011 (setq entries nil)
3012 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3013 (set-buffer-modified-p nil)
3014 (kill-buffer diary-fancy-buffer)))
3015 (when entries
3016 (setq entries (org-split-string entries "\n"))
3017 (setq entries
3018 (mapcar
3019 (lambda (x)
3020 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3021 ;; Extend the text properties to the beginning of the line
3022 (org-add-props x (text-properties-at (1- (length x)) x)
3023 'type "diary" 'date date))
3024 entries)))))
3025
3026 (defun org-agenda-cleanup-fancy-diary ()
3027 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3028 This gets rid of the date, the underline under the date, and
3029 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3030 date. It also removes lines that contain only whitespace."
3031 (goto-char (point-min))
3032 (if (looking-at ".*?:[ \t]*")
3033 (progn
3034 (replace-match "")
3035 (re-search-forward "\n=+$" nil t)
3036 (replace-match "")
3037 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3038 (re-search-forward "\n=+$" nil t)
3039 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3040 (goto-char (point-min))
3041 (while (re-search-forward "^ +\n" nil t)
3042 (replace-match ""))
3043 (goto-char (point-min))
3044 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3045 (replace-match "")))
3046
3047 ;; Make sure entries from the diary have the right text properties.
3048 (eval-after-load "diary-lib"
3049 '(if (boundp 'diary-modify-entry-list-string-function)
3050 ;; We can rely on the hook, nothing to do
3051 nil
3052 ;; Hook not avaiable, must use advice to make this work
3053 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3054 "Make the position visible."
3055 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3056 (stringp string)
3057 buffer-file-name)
3058 (setq string (org-modify-diary-entry-string string))))))
3059
3060 (defun org-modify-diary-entry-string (string)
3061 "Add text properties to string, allowing org-mode to act on it."
3062 (org-add-props string nil
3063 'mouse-face 'highlight
3064 'keymap org-agenda-keymap
3065 'help-echo (if buffer-file-name
3066 (format "mouse-2 or RET jump to diary file %s"
3067 (abbreviate-file-name buffer-file-name))
3068 "")
3069 'org-agenda-diary-link t
3070 'org-marker (org-agenda-new-marker (point-at-bol))))
3071
3072 (defun org-diary-default-entry ()
3073 "Add a dummy entry to the diary.
3074 Needed to avoid empty dates which mess up holiday display."
3075 ;; Catch the error if dealing with the new add-to-diary-alist
3076 (when org-disable-agenda-to-diary
3077 (condition-case nil
3078 (org-add-to-diary-list original-date "Org-mode dummy" "")
3079 (error
3080 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3081
3082 (defun org-add-to-diary-list (&rest args)
3083 (if (fboundp 'diary-add-to-list)
3084 (apply 'diary-add-to-list args)
3085 (apply 'add-to-diary-list args)))
3086
3087 ;;;###autoload
3088 (defun org-diary (&rest args)
3089 "Return diary information from org-files.
3090 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3091 It accesses org files and extracts information from those files to be
3092 listed in the diary. The function accepts arguments specifying what
3093 items should be listed. The following arguments are allowed:
3094
3095 :timestamp List the headlines of items containing a date stamp or
3096 date range matching the selected date. Deadlines will
3097 also be listed, on the expiration day.
3098
3099 :sexp List entries resulting from diary-like sexps.
3100
3101 :deadline List any deadlines past due, or due within
3102 `org-deadline-warning-days'. The listing occurs only
3103 in the diary for *today*, not at any other date. If
3104 an entry is marked DONE, it is no longer listed.
3105
3106 :scheduled List all items which are scheduled for the given date.
3107 The diary for *today* also contains items which were
3108 scheduled earlier and are not yet marked DONE.
3109
3110 :todo List all TODO items from the org-file. This may be a
3111 long list - so this is not turned on by default.
3112 Like deadlines, these entries only show up in the
3113 diary for *today*, not at any other date.
3114
3115 The call in the diary file should look like this:
3116
3117 &%%(org-diary) ~/path/to/some/orgfile.org
3118
3119 Use a separate line for each org file to check. Or, if you omit the file name,
3120 all files listed in `org-agenda-files' will be checked automatically:
3121
3122 &%%(org-diary)
3123
3124 If you don't give any arguments (as in the example above), the default
3125 arguments (:deadline :scheduled :timestamp :sexp) are used.
3126 So the example above may also be written as
3127
3128 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3129
3130 The function expects the lisp variables `entry' and `date' to be provided
3131 by the caller, because this is how the calendar works. Don't use this
3132 function from a program - use `org-agenda-get-day-entries' instead."
3133 (when (> (- (time-to-seconds (current-time))
3134 org-agenda-last-marker-time)
3135 5)
3136 (org-agenda-reset-markers))
3137 (org-compile-prefix-format 'agenda)
3138 (org-set-sorting-strategy 'agenda)
3139 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3140 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3141 (list entry)
3142 (org-agenda-files t)))
3143 file rtn results)
3144 (org-prepare-agenda-buffers files)
3145 ;; If this is called during org-agenda, don't return any entries to
3146 ;; the calendar. Org Agenda will list these entries itself.
3147 (if org-disable-agenda-to-diary (setq files nil))
3148 (while (setq file (pop files))
3149 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3150 (setq results (append results rtn)))
3151 (if results
3152 (concat (org-finalize-agenda-entries results) "\n"))))
3153
3154 ;;; Agenda entry finders
3155
3156 (defun org-agenda-get-day-entries (file date &rest args)
3157 "Does the work for `org-diary' and `org-agenda'.
3158 FILE is the path to a file to be checked for entries. DATE is date like
3159 the one returned by `calendar-current-date'. ARGS are symbols indicating
3160 which kind of entries should be extracted. For details about these, see
3161 the documentation of `org-diary'."
3162 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3163 (let* ((org-startup-folded nil)
3164 (org-startup-align-all-tables nil)
3165 (buffer (if (file-exists-p file)
3166 (org-get-agenda-file-buffer file)
3167 (error "No such file %s" file)))
3168 arg results rtn)
3169 (if (not buffer)
3170 ;; If file does not exist, make sure an error message ends up in diary
3171 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3172 (with-current-buffer buffer
3173 (unless (org-mode-p)
3174 (error "Agenda file %s is not in `org-mode'" file))
3175 (let ((case-fold-search nil))
3176 (save-excursion
3177 (save-restriction
3178 (if org-agenda-restrict
3179 (narrow-to-region org-agenda-restrict-begin
3180 org-agenda-restrict-end)
3181 (widen))
3182 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3183 (while (setq arg (pop args))
3184 (cond
3185 ((and (eq arg :todo)
3186 (equal date (calendar-current-date)))
3187 (setq rtn (org-agenda-get-todos))
3188 (setq results (append results rtn)))
3189 ((eq arg :timestamp)
3190 (setq rtn (org-agenda-get-blocks))
3191 (setq results (append results rtn))
3192 (setq rtn (org-agenda-get-timestamps))
3193 (setq results (append results rtn)))
3194 ((eq arg :sexp)
3195 (setq rtn (org-agenda-get-sexps))
3196 (setq results (append results rtn)))
3197 ((eq arg :scheduled)
3198 (setq rtn (org-agenda-get-scheduled))
3199 (setq results (append results rtn)))
3200 ((eq arg :closed)
3201 (setq rtn (org-agenda-get-progress))
3202 (setq results (append results rtn)))
3203 ((eq arg :deadline)
3204 (setq rtn (org-agenda-get-deadlines))
3205 (setq results (append results rtn))))))))
3206 results))))
3207
3208 (defun org-agenda-get-todos ()
3209 "Return the TODO information for agenda display."
3210 (let* ((props (list 'face nil
3211 'done-face 'org-done
3212 'org-not-done-regexp org-not-done-regexp
3213 'org-todo-regexp org-todo-regexp
3214 'org-complex-heading-regexp org-complex-heading-regexp
3215 'mouse-face 'highlight
3216 'keymap org-agenda-keymap
3217 'help-echo
3218 (format "mouse-2 or RET jump to org file %s"
3219 (abbreviate-file-name buffer-file-name))))
3220 (regexp (concat "^\\*+[ \t]+\\("
3221 (if org-select-this-todo-keyword
3222 (if (equal org-select-this-todo-keyword "*")
3223 org-todo-regexp
3224 (concat "\\<\\("
3225 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3226 "\\)\\>"))
3227 org-not-done-regexp)
3228 "[^\n\r]*\\)"))
3229 marker priority category tags todo-state
3230 ee txt beg end)
3231 (goto-char (point-min))
3232 (while (re-search-forward regexp nil t)
3233 (catch :skip
3234 (save-match-data
3235 (beginning-of-line)
3236 (setq beg (point) end (progn (outline-next-heading) (point)))
3237 (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
3238 (re-search-forward org-ts-regexp end t))
3239 (and org-agenda-todo-ignore-scheduled (goto-char beg)
3240 (re-search-forward org-scheduled-time-regexp end t))
3241 (and org-agenda-todo-ignore-deadlines (goto-char beg)
3242 (re-search-forward org-deadline-time-regexp end t)
3243 (org-deadline-close (match-string 1))))
3244 (goto-char (1+ beg))
3245 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3246 (throw :skip nil)))
3247 (goto-char beg)
3248 (org-agenda-skip)
3249 (goto-char (match-beginning 1))
3250 (setq marker (org-agenda-new-marker (match-beginning 0))
3251 category (org-get-category)
3252 tags (org-get-tags-at (point))
3253 txt (org-format-agenda-item "" (match-string 1) category tags)
3254 priority (1+ (org-get-priority txt))
3255 todo-state (org-get-todo-state))
3256 (org-add-props txt props
3257 'org-marker marker 'org-hd-marker marker
3258 'priority priority 'org-category category
3259 'type "todo" 'todo-state todo-state)
3260 (push txt ee)
3261 (if org-agenda-todo-list-sublevels
3262 (goto-char (match-end 1))
3263 (org-end-of-subtree 'invisible))))
3264 (nreverse ee)))
3265
3266 (defconst org-agenda-no-heading-message
3267 "No heading for this item in buffer or region.")
3268
3269 (defun org-agenda-get-timestamps ()
3270 "Return the date stamp information for agenda display."
3271 (let* ((props (list 'face nil
3272 'org-not-done-regexp org-not-done-regexp
3273 'org-todo-regexp org-todo-regexp
3274 'org-complex-heading-regexp org-complex-heading-regexp
3275 'mouse-face 'highlight
3276 'keymap org-agenda-keymap
3277 'help-echo
3278 (format "mouse-2 or RET jump to org file %s"
3279 (abbreviate-file-name buffer-file-name))))
3280 (d1 (calendar-absolute-from-gregorian date))
3281 (remove-re
3282 (concat
3283 (regexp-quote
3284 (format-time-string
3285 "<%Y-%m-%d"
3286 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3287 ".*?>"))
3288 (regexp
3289 (concat
3290 (if org-agenda-include-inactive-timestamps "[[<]" "<")
3291 (regexp-quote
3292 (substring
3293 (format-time-string
3294 (car org-time-stamp-formats)
3295 (apply 'encode-time ; DATE bound by calendar
3296 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3297 1 11))
3298 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
3299 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
3300 marker hdmarker deadlinep scheduledp clockp closedp inactivep
3301 donep tmp priority category ee txt timestr tags b0 b3 e3 head
3302 todo-state)
3303 (goto-char (point-min))
3304 (while (re-search-forward regexp nil t)
3305 (setq b0 (match-beginning 0)
3306 b3 (match-beginning 3) e3 (match-end 3))
3307 (catch :skip
3308 (and (org-at-date-range-p) (throw :skip nil))
3309 (org-agenda-skip)
3310 (if (and (match-end 1)
3311 (not (= d1 (org-time-string-to-absolute
3312 (match-string 1) d1 nil
3313 org-agenda-repeating-timestamp-show-all))))
3314 (throw :skip nil))
3315 (if (and e3
3316 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
3317 (throw :skip nil))
3318 (setq marker (org-agenda-new-marker b0)
3319 category (org-get-category b0)
3320 tmp (buffer-substring (max (point-min)
3321 (- b0 org-ds-keyword-length))
3322 b0)
3323 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
3324 inactivep (= (char-after b0) ?\[)
3325 deadlinep (string-match org-deadline-regexp tmp)
3326 scheduledp (string-match org-scheduled-regexp tmp)
3327 closedp (and org-agenda-include-inactive-timestamps
3328 (string-match org-closed-string tmp))
3329 clockp (and org-agenda-include-inactive-timestamps
3330 (or (string-match org-clock-string tmp)
3331 (string-match "]-+\\'" tmp)))
3332 todo-state (org-get-todo-state)
3333 donep (member todo-state org-done-keywords))
3334 (if (or scheduledp deadlinep closedp clockp)
3335 (throw :skip t))
3336 (if (string-match ">" timestr)
3337 ;; substring should only run to end of time stamp
3338 (setq timestr (substring timestr 0 (match-end 0))))
3339 (save-excursion
3340 (if (re-search-backward "^\\*+ " nil t)
3341 (progn
3342 (goto-char (match-beginning 0))
3343 (setq hdmarker (org-agenda-new-marker)
3344 tags (org-get-tags-at))
3345 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3346 (setq head (match-string 1))
3347 (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
3348 (setq txt (org-format-agenda-item
3349 (if inactivep "[" nil)
3350 head category tags timestr nil
3351 remove-re)))
3352 (setq txt org-agenda-no-heading-message))
3353 (setq priority (org-get-priority txt))
3354 (org-add-props txt props
3355 'org-marker marker 'org-hd-marker hdmarker)
3356 (org-add-props txt nil 'priority priority
3357 'org-category category 'date date
3358 'todo-state todo-state
3359 'type "timestamp")
3360 (push txt ee))
3361 (outline-next-heading)))
3362 (nreverse ee)))
3363
3364 (defun org-agenda-get-sexps ()
3365 "Return the sexp information for agenda display."
3366 (require 'diary-lib)
3367 (let* ((props (list 'face nil
3368 'mouse-face 'highlight
3369 'keymap org-agenda-keymap
3370 'help-echo
3371 (format "mouse-2 or RET jump to org file %s"
3372 (abbreviate-file-name buffer-file-name))))
3373 (regexp "^&?%%(")
3374 marker category ee txt tags entry result beg b sexp sexp-entry)
3375 (goto-char (point-min))
3376 (while (re-search-forward regexp nil t)
3377 (catch :skip
3378 (org-agenda-skip)
3379 (setq beg (match-beginning 0))
3380 (goto-char (1- (match-end 0)))
3381 (setq b (point))
3382 (forward-sexp 1)
3383 (setq sexp (buffer-substring b (point)))
3384 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
3385 (org-trim (match-string 1))
3386 ""))
3387 (setq result (org-diary-sexp-entry sexp sexp-entry date))
3388 (when result
3389 (setq marker (org-agenda-new-marker beg)
3390 category (org-get-category beg))
3391
3392 (if (string-match "\\S-" result)
3393 (setq txt result)
3394 (setq txt "SEXP entry returned empty string"))
3395
3396 (setq txt (org-format-agenda-item
3397 "" txt category tags 'time))
3398 (org-add-props txt props 'org-marker marker)
3399 (org-add-props txt nil
3400 'org-category category 'date date
3401 'type "sexp")
3402 (push txt ee))))
3403 (nreverse ee)))
3404
3405 (defalias 'org-get-closed 'org-agenda-get-progress)
3406 (defun org-agenda-get-progress ()
3407 "Return the logged TODO entries for agenda display."
3408 (let* ((props (list 'mouse-face 'highlight
3409 'org-not-done-regexp org-not-done-regexp
3410 'org-todo-regexp org-todo-regexp
3411 'org-complex-heading-regexp org-complex-heading-regexp
3412 'keymap org-agenda-keymap
3413 'help-echo
3414 (format "mouse-2 or RET jump to org file %s"
3415 (abbreviate-file-name buffer-file-name))))
3416 (items (if (consp org-agenda-show-log)
3417 org-agenda-show-log
3418 org-agenda-log-mode-items))
3419 (parts
3420 (delq nil
3421 (list
3422 (if (memq 'closed items) (concat "\\<" org-closed-string))
3423 (if (memq 'clock items) (concat "\\<" org-clock-string))
3424 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\""))))
3425 (parts-re (if parts (mapconcat 'identity parts "\\|")
3426 (error "`org-agenda-log-mode-items' is empty")))
3427 (regexp (concat
3428 "\\(" parts-re "\\)"
3429 " *\\["
3430 (regexp-quote
3431 (substring
3432 (format-time-string
3433 (car org-time-stamp-formats)
3434 (apply 'encode-time ; DATE bound by calendar
3435 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
3436 1 11))))
3437 marker hdmarker priority category tags closedp statep state
3438 ee txt timestr rest clocked)
3439 (goto-char (point-min))
3440 (while (re-search-forward regexp nil t)
3441 (catch :skip
3442 (org-agenda-skip)
3443 (setq marker (org-agenda-new-marker (match-beginning 0))
3444 closedp (equal (match-string 1) org-closed-string)
3445 statep (equal (string-to-char (match-string 1)) ?-)
3446 state (and statep (match-string 2))
3447 category (org-get-category (match-beginning 0))
3448 timestr (buffer-substring (match-beginning 0) (point-at-eol))
3449 ;; donep (org-entry-is-done-p)
3450 )
3451 (when (string-match "\\]" timestr)
3452 ;; substring should only run to end of time stamp
3453 (setq rest (substring timestr (match-end 0))
3454 timestr (substring timestr 0 (match-end 0)))
3455 (if (and (not closedp) (not statep)
3456 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
3457 (progn (setq timestr (concat (substring timestr 0 -1)
3458 "-" (match-string 1 rest) "]"))
3459 (setq clocked (match-string 2 rest)))
3460 (setq clocked "-")))
3461 (save-excursion
3462 (if (re-search-backward "^\\*+ " nil t)
3463 (progn
3464 (goto-char (match-beginning 0))
3465 (setq hdmarker (org-agenda-new-marker)
3466 tags (org-get-tags-at))
3467 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3468 (setq txt (org-format-agenda-item
3469 (cond
3470 (closedp "Closed: ")
3471 (statep (concat "State: (" state ")"))
3472 (t (concat "Clocked: (" clocked ")")))
3473 (match-string 1) category tags timestr)))
3474 (setq txt org-agenda-no-heading-message))
3475 (setq priority 100000)
3476 (org-add-props txt props
3477 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
3478 'priority priority 'org-category category
3479 'type "closed" 'date date
3480 'undone-face 'org-warning 'done-face 'org-done)
3481 (push txt ee))
3482 (goto-char (point-at-eol))))
3483 (nreverse ee)))
3484
3485 (defun org-agenda-get-deadlines ()
3486 "Return the deadline information for agenda display."
3487 (let* ((props (list 'mouse-face 'highlight
3488 'org-not-done-regexp org-not-done-regexp
3489 'org-todo-regexp org-todo-regexp
3490 'org-complex-heading-regexp org-complex-heading-regexp
3491 'keymap org-agenda-keymap
3492 'help-echo
3493 (format "mouse-2 or RET jump to org file %s"
3494 (abbreviate-file-name buffer-file-name))))
3495 (regexp org-deadline-time-regexp)
3496 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3497 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3498 d2 diff dfrac wdays pos pos1 category tags
3499 ee txt head face s todo-state upcomingp donep timestr)
3500 (goto-char (point-min))
3501 (while (re-search-forward regexp nil t)
3502 (catch :skip
3503 (org-agenda-skip)
3504 (setq s (match-string 1)
3505 pos (1- (match-beginning 1))
3506 d2 (org-time-string-to-absolute
3507 (match-string 1) d1 'past
3508 org-agenda-repeating-timestamp-show-all)
3509 diff (- d2 d1)
3510 wdays (org-get-wdays s)
3511 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
3512 upcomingp (and todayp (> diff 0)))
3513 ;; When to show a deadline in the calendar:
3514 ;; If the expiration is within wdays warning time.
3515 ;; Past-due deadlines are only shown on the current date
3516 (if (or (and (<= diff wdays)
3517 (and todayp (not org-agenda-only-exact-dates)))
3518 (= diff 0))
3519 (save-excursion
3520 (setq category (org-get-category))
3521 (setq todo-state (org-get-todo-state))
3522 (if (re-search-backward "^\\*+[ \t]+" nil t)
3523 (progn
3524 (goto-char (match-end 0))
3525 (setq pos1 (match-beginning 0))
3526 (setq tags (org-get-tags-at pos1))
3527 (setq head (buffer-substring-no-properties
3528 (point)
3529 (progn (skip-chars-forward "^\r\n")
3530 (point))))
3531 (setq donep (member todo-state org-done-keywords))
3532 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3533 (setq timestr
3534 (concat (substring s (match-beginning 1)) " "))
3535 (setq timestr 'time))
3536 (if (and donep
3537 (or org-agenda-skip-deadline-if-done
3538 (not (= diff 0))))
3539 (setq txt nil)
3540 (setq txt (org-format-agenda-item
3541 (if (= diff 0)
3542 (car org-agenda-deadline-leaders)
3543 (if (functionp (nth 1 org-agenda-deadline-leaders))
3544 (funcall (nth 1 org-agenda-deadline-leaders) diff date)
3545 (format (nth 1 org-agenda-deadline-leaders)
3546 diff)))
3547 head category tags
3548 (if (not (= diff 0)) nil timestr)))))
3549 (setq txt org-agenda-no-heading-message))
3550 (when txt
3551 (setq face (org-agenda-deadline-face dfrac wdays))
3552 (org-add-props txt props
3553 'org-marker (org-agenda-new-marker pos)
3554 'org-hd-marker (org-agenda-new-marker pos1)
3555 'priority (+ (- diff)
3556 (org-get-priority txt))
3557 'org-category category
3558 'todo-state todo-state
3559 'type (if upcomingp "upcoming-deadline" "deadline")
3560 'date (if upcomingp date d2)
3561 'face (if donep 'org-done face)
3562 'undone-face face 'done-face 'org-done)
3563 (push txt ee))))))
3564 (nreverse ee)))
3565
3566 (defun org-agenda-deadline-face (fraction &optional wdays)
3567 "Return the face to displaying a deadline item.
3568 FRACTION is what fraction of the head-warning time has passed."
3569 (if (equal wdays 0) (setq fraction 1.))
3570 (let ((faces org-agenda-deadline-faces) f)
3571 (catch 'exit
3572 (while (setq f (pop faces))
3573 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
3574
3575 (defun org-agenda-get-scheduled ()
3576 "Return the scheduled information for agenda display."
3577 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
3578 'org-todo-regexp org-todo-regexp
3579 'org-complex-heading-regexp org-complex-heading-regexp
3580 'done-face 'org-done
3581 'mouse-face 'highlight
3582 'keymap org-agenda-keymap
3583 'help-echo
3584 (format "mouse-2 or RET jump to org file %s"
3585 (abbreviate-file-name buffer-file-name))))
3586 (regexp org-scheduled-time-regexp)
3587 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
3588 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
3589 d2 diff pos pos1 category tags donep
3590 ee txt head pastschedp todo-state face timestr s)
3591 (goto-char (point-min))
3592 (while (re-search-forward regexp nil t)
3593 (catch :skip
3594 (org-agenda-skip)
3595 (setq s (match-string 1)
3596 pos (1- (match-beginning 1))
3597 d2 (org-time-string-to-absolute
3598 (match-string 1) d1 'past
3599 org-agenda-repeating-timestamp-show-all)
3600 diff (- d2 d1))
3601 (setq pastschedp (and todayp (< diff 0)))
3602 ;; When to show a scheduled item in the calendar:
3603 ;; If it is on or past the date.
3604 (if (or (and (< diff 0)
3605 (< (abs diff) org-scheduled-past-days)
3606 (and todayp (not org-agenda-only-exact-dates)))
3607 (= diff 0))
3608 (save-excursion
3609 (setq category (org-get-category))
3610 (setq todo-state (org-get-todo-state))
3611 (if (re-search-backward "^\\*+[ \t]+" nil t)
3612 (progn
3613 (goto-char (match-end 0))
3614 (setq pos1 (match-beginning 0))
3615 (setq tags (org-get-tags-at))
3616 (setq head (buffer-substring-no-properties
3617 (point)
3618 (progn (skip-chars-forward "^\r\n") (point))))
3619 (setq donep (member todo-state org-done-keywords))
3620 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
3621 (setq timestr
3622 (concat (substring s (match-beginning 1)) " "))
3623 (setq timestr 'time))
3624 (if (and donep
3625 (or org-agenda-skip-scheduled-if-done
3626 (not (= diff 0))))
3627 (setq txt nil)
3628 (setq txt (org-format-agenda-item
3629 (if (= diff 0)
3630 (car org-agenda-scheduled-leaders)
3631 (format (nth 1 org-agenda-scheduled-leaders)
3632 (- 1 diff)))
3633 head category tags
3634 (if (not (= diff 0)) nil timestr)))))
3635 (setq txt org-agenda-no-heading-message))
3636 (when txt
3637 (setq face
3638 (cond
3639 (pastschedp 'org-scheduled-previously)
3640 (todayp 'org-scheduled-today)
3641 (t 'org-scheduled)))
3642 (org-add-props txt props
3643 'undone-face face
3644 'face (if donep 'org-done face)
3645 'org-marker (org-agenda-new-marker pos)
3646 'org-hd-marker (org-agenda-new-marker pos1)
3647 'type (if pastschedp "past-scheduled" "scheduled")
3648 'date (if pastschedp d2 date)
3649 'priority (+ 94 (- 5 diff) (org-get-priority txt))
3650 'org-category category
3651 'todo-state todo-state)
3652 (push txt ee))))))
3653 (nreverse ee)))
3654
3655 (defun org-agenda-get-blocks ()
3656 "Return the date-range information for agenda display."
3657 (let* ((props (list 'face nil
3658 'org-not-done-regexp org-not-done-regexp
3659 'org-todo-regexp org-todo-regexp
3660 'org-complex-heading-regexp org-complex-heading-regexp
3661 'mouse-face 'highlight
3662 'keymap org-agenda-keymap
3663 'help-echo
3664 (format "mouse-2 or RET jump to org file %s"
3665 (abbreviate-file-name buffer-file-name))))
3666 (regexp org-tr-regexp)
3667 (d0 (calendar-absolute-from-gregorian date))
3668 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
3669 donep head)
3670 (goto-char (point-min))
3671 (while (re-search-forward regexp nil t)
3672 (catch :skip
3673 (org-agenda-skip)
3674 (setq pos (point))
3675 (setq timestr (match-string 0)
3676 s1 (match-string 1)
3677 s2 (match-string 2)
3678 d1 (time-to-days (org-time-string-to-time s1))
3679 d2 (time-to-days (org-time-string-to-time s2)))
3680 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
3681 ;; Only allow days between the limits, because the normal
3682 ;; date stamps will catch the limits.
3683 (save-excursion
3684 (setq marker (org-agenda-new-marker (point)))
3685 (setq category (org-get-category))
3686 (setq todo-state (org-get-todo-state))
3687 (if (re-search-backward "^\\*+ " nil t)
3688 (progn
3689 (goto-char (match-beginning 0))
3690 (setq hdmarker (org-agenda-new-marker (point)))
3691 (setq tags (org-get-tags-at))
3692 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
3693 (setq head (match-string 1))
3694 (and org-agenda-skip-timestamp-if-done
3695 (org-entry-is-done-p)
3696 (throw :skip t))
3697 (setq txt (org-format-agenda-item
3698 (format
3699 (nth (if (= d1 d2) 0 1)
3700 org-agenda-timerange-leaders)
3701 (1+ (- d0 d1)) (1+ (- d2 d1)))
3702 head category tags
3703 (if (= d0 d1) timestr))))
3704 (setq txt org-agenda-no-heading-message))
3705 (org-add-props txt props
3706 'org-marker marker 'org-hd-marker hdmarker
3707 'type "block" 'date date
3708 'todo-state todo-state
3709 'priority (org-get-priority txt) 'org-category category)
3710 (push txt ee)))
3711 (goto-char pos)))
3712 ;; Sort the entries by expiration date.
3713 (nreverse ee)))
3714
3715 ;;; Agenda presentation and sorting
3716
3717 (defvar org-prefix-has-time nil
3718 "A flag, set by `org-compile-prefix-format'.
3719 The flag is set if the currently compiled format contains a `%t'.")
3720 (defvar org-prefix-has-tag nil
3721 "A flag, set by `org-compile-prefix-format'.
3722 The flag is set if the currently compiled format contains a `%T'.")
3723 (defvar org-prefix-has-effort nil
3724 "A flag, set by `org-compile-prefix-format'.
3725 The flag is set if the currently compiled format contains a `%e'.")
3726
3727 (defun org-format-agenda-item (extra txt &optional category tags dotime
3728 noprefix remove-re)
3729 "Format TXT to be inserted into the agenda buffer.
3730 In particular, it adds the prefix and corresponding text properties. EXTRA
3731 must be a string and replaces the `%s' specifier in the prefix format.
3732 CATEGORY (string, symbol or nil) may be used to overrule the default
3733 category taken from local variable or file name. It will replace the `%c'
3734 specifier in the format. DOTIME, when non-nil, indicates that a
3735 time-of-day should be extracted from TXT for sorting of this entry, and for
3736 the `%t' specifier in the format. When DOTIME is a string, this string is
3737 searched for a time before TXT is. NOPREFIX is a flag and indicates that
3738 only the correctly processes TXT should be returned - this is used by
3739 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
3740 Any match of REMOVE-RE will be removed from TXT."
3741 (save-match-data
3742 ;; Diary entries sometimes have extra whitespace at the beginning
3743 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
3744 (when org-agenda-show-inherited-tags
3745 ;; Fix the tags part in txt
3746 (setq txt (org-agenda-add-inherited-tags txt tags)))
3747 (let* ((category (or category
3748 org-category
3749 (if buffer-file-name
3750 (file-name-sans-extension
3751 (file-name-nondirectory buffer-file-name))
3752 "")))
3753 ;; time, tag, effort are needed for the eval of the prefix format
3754 (tag (if tags (nth (1- (length tags)) tags) ""))
3755 time effort neffort
3756 (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
3757 (time-of-day (and dotime (org-get-time-of-day ts)))
3758 stamp plain s0 s1 s2 t1 t2 rtn srp
3759 duration)
3760 (and (org-mode-p) buffer-file-name
3761 (add-to-list 'org-agenda-contributing-files buffer-file-name))
3762 (when (and dotime time-of-day)
3763 ;; Extract starting and ending time and move them to prefix
3764 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
3765 (setq plain (string-match org-plain-time-of-day-regexp ts)))
3766 (setq s0 (match-string 0 ts)
3767 srp (and stamp (match-end 3))
3768 s1 (match-string (if plain 1 2) ts)
3769 s2 (match-string (if plain 8 (if srp 4 6)) ts))
3770
3771 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
3772 ;; them, we might want to remove them there to avoid duplication.
3773 ;; The user can turn this off with a variable.
3774 (if (and org-prefix-has-time
3775 org-agenda-remove-times-when-in-prefix (or stamp plain)
3776 (string-match (concat (regexp-quote s0) " *") txt)
3777 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
3778 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
3779 (= (match-beginning 0) 0)
3780 t))
3781 (setq txt (replace-match "" nil nil txt))))
3782 ;; Normalize the time(s) to 24 hour
3783 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
3784 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
3785 ;; Compute the duration
3786 (when s1
3787 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
3788 (string-to-number (substring s1 3)))
3789 t2 (cond
3790 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
3791 (string-to-number (substring s2 3))))
3792 (org-agenda-default-appointment-duration
3793 (+ t1 org-agenda-default-appointment-duration))
3794 (t nil)))
3795 (setq duration (if t2 (- t2 t1)))))
3796
3797 (when (and s1 (not s2) org-agenda-default-appointment-duration
3798 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
3799 (let ((m (+ (string-to-number (match-string 2 s1))
3800 (* 60 (string-to-number (match-string 1 s1)))
3801 org-agenda-default-appointment-duration))
3802 h)
3803 (setq h (/ m 60) m (- m (* h 60)))
3804 (setq s2 (format "%02d:%02d" h m))))
3805
3806 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
3807 txt)
3808 ;; Tags are in the string
3809 (if (or (eq org-agenda-remove-tags t)
3810 (and org-agenda-remove-tags
3811 org-prefix-has-tag))
3812 (setq txt (replace-match "" t t txt))
3813 (setq txt (replace-match
3814 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
3815 (match-string 2 txt))
3816 t t txt))))
3817 (when (org-mode-p)
3818 (setq effort
3819 (condition-case nil
3820 (org-get-effort
3821 (or (get-text-property 0 'org-hd-marker txt)
3822 (get-text-property 0 'org-marker txt)))
3823 (error nil)))
3824 (when effort
3825 (setq neffort (org-hh:mm-string-to-minutes effort)
3826 effort (setq effort (concat "[" effort"]" )))))
3827
3828 (when remove-re
3829 (while (string-match remove-re txt)
3830 (setq txt (replace-match "" t t txt))))
3831
3832 ;; Create the final string
3833 (if noprefix
3834 (setq rtn txt)
3835 ;; Prepare the variables needed in the eval of the compiled format
3836 (setq time (cond (s2 (concat s1 "-" s2))
3837 (s1 (concat s1 "......"))
3838 (t ""))
3839 extra (or extra "")
3840 category (if (symbolp category) (symbol-name category) category))
3841 ;; Evaluate the compiled format
3842 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
3843
3844 ;; And finally add the text properties
3845 (org-add-props rtn nil
3846 'org-category (downcase category)
3847 'tags (mapcar 'org-downcase-keep-props tags)
3848 'org-highest-priority org-highest-priority
3849 'org-lowest-priority org-lowest-priority
3850 'prefix-length (- (length rtn) (length txt))
3851 'time-of-day time-of-day
3852 'duration duration
3853 'effort effort
3854 'effort-minutes neffort
3855 'txt txt
3856 'time time
3857 'extra extra
3858 'dotime dotime))))
3859
3860 (defun org-agenda-add-inherited-tags (txt tags)
3861 "Remove tags string from TXT, and add complete list of tags.
3862 The new list includes inherited tags. If any inherited tags are present,
3863 a double colon separates inherited tags from local tags."
3864 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
3865 (setq txt (substring txt 0 (match-beginning 0))))
3866 (when tags
3867 (let ((have-i (get-text-property 0 'inherited (car tags)))
3868 i)
3869 (setq txt (concat txt " :"
3870 (mapconcat
3871 (lambda (x)
3872 (setq i (get-text-property 0 'inherited x))
3873 (if (and have-i (not i))
3874 (progn
3875 (setq have-i nil)
3876 (concat ":" x))
3877 x))
3878 tags ":")
3879 (if have-i "::" ":")))))
3880 txt)
3881
3882 (defun org-downcase-keep-props (s)
3883 (let ((props (text-properties-at 0 s)))
3884 (setq s (downcase s))
3885 (add-text-properties 0 (length s) props s)
3886 s))
3887
3888 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
3889 (defvar org-agenda-sorting-strategy-selected nil)
3890
3891 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
3892 (catch 'exit
3893 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
3894 ((and todayp (member 'today (car org-agenda-time-grid))))
3895 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
3896 ((member 'weekly (car org-agenda-time-grid)))
3897 (t (throw 'exit list)))
3898 (let* ((have (delq nil (mapcar
3899 (lambda (x) (get-text-property 1 'time-of-day x))
3900 list)))
3901 (string (nth 1 org-agenda-time-grid))
3902 (gridtimes (nth 2 org-agenda-time-grid))
3903 (req (car org-agenda-time-grid))
3904 (remove (member 'remove-match req))
3905 new time)
3906 (if (and (member 'require-timed req) (not have))
3907 ;; don't show empty grid
3908 (throw 'exit list))
3909 (while (setq time (pop gridtimes))
3910 (unless (and remove (member time have))
3911 (setq time (int-to-string time))
3912 (push (org-format-agenda-item
3913 nil string "" nil
3914 (concat (substring time 0 -2) ":" (substring time -2)))
3915 new)
3916 (put-text-property
3917 1 (length (car new)) 'face 'org-time-grid (car new))))
3918 (if (member 'time-up org-agenda-sorting-strategy-selected)
3919 (append new list)
3920 (append list new)))))
3921
3922 (defun org-compile-prefix-format (key)
3923 "Compile the prefix format into a Lisp form that can be evaluated.
3924 The resulting form is returned and stored in the variable
3925 `org-prefix-format-compiled'."
3926 (setq org-prefix-has-time nil org-prefix-has-tag nil
3927 org-prefix-has-effort nil)
3928 (let ((s (cond
3929 ((stringp org-agenda-prefix-format)
3930 org-agenda-prefix-format)
3931 ((assq key org-agenda-prefix-format)
3932 (cdr (assq key org-agenda-prefix-format)))
3933 (t " %-12:c%?-12t% s")))
3934 (start 0)
3935 varform vars var e c f opt)
3936 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
3937 s start)
3938 (setq var (cdr (assoc (match-string 4 s)
3939 '(("c" . category) ("t" . time) ("s" . extra)
3940 ("T" . tag) ("e" . effort))))
3941 c (or (match-string 3 s) "")
3942 opt (match-beginning 1)
3943 start (1+ (match-beginning 0)))
3944 (if (equal var 'time) (setq org-prefix-has-time t))
3945 (if (equal var 'tag) (setq org-prefix-has-tag t))
3946 (if (equal var 'effort) (setq org-prefix-has-effort t))
3947 (setq f (concat "%" (match-string 2 s) "s"))
3948 (if opt
3949 (setq varform
3950 `(if (equal "" ,var)
3951 ""
3952 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
3953 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
3954 (setq s (replace-match "%s" t nil s))
3955 (push varform vars))
3956 (setq vars (nreverse vars))
3957 (setq org-prefix-format-compiled `(format ,s ,@vars))))
3958
3959 (defun org-set-sorting-strategy (key)
3960 (if (symbolp (car org-agenda-sorting-strategy))
3961 ;; the old format
3962 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
3963 (setq org-agenda-sorting-strategy-selected
3964 (or (cdr (assq key org-agenda-sorting-strategy))
3965 (cdr (assq 'agenda org-agenda-sorting-strategy))
3966 '(time-up category-keep priority-down)))))
3967
3968 (defun org-get-time-of-day (s &optional string mod24)
3969 "Check string S for a time of day.
3970 If found, return it as a military time number between 0 and 2400.
3971 If not found, return nil.
3972 The optional STRING argument forces conversion into a 5 character wide string
3973 HH:MM."
3974 (save-match-data
3975 (when
3976 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
3977 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
3978 (let* ((h (string-to-number (match-string 1 s)))
3979 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
3980 (ampm (if (match-end 4) (downcase (match-string 4 s))))
3981 (am-p (equal ampm "am"))
3982 (h1 (cond ((not ampm) h)
3983 ((= h 12) (if am-p 0 12))
3984 (t (+ h (if am-p 0 12)))))
3985 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
3986 (mod h1 24) h1))
3987 (t0 (+ (* 100 h2) m))
3988 (t1 (concat (if (>= h1 24) "+" " ")
3989 (if (< t0 100) "0" "")
3990 (if (< t0 10) "0" "")
3991 (int-to-string t0))))
3992 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
3993
3994 (defun org-finalize-agenda-entries (list &optional nosort)
3995 "Sort and concatenate the agenda items."
3996 (setq list (mapcar 'org-agenda-highlight-todo list))
3997 (if nosort
3998 list
3999 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4000
4001 (defun org-agenda-highlight-todo (x)
4002 (let ((org-done-keywords org-done-keywords-for-agenda)
4003 re pl)
4004 (if (eq x 'line)
4005 (save-excursion
4006 (beginning-of-line 1)
4007 (setq re (get-text-property (point) 'org-todo-regexp))
4008 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
4009 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4010 (add-text-properties (match-beginning 0) (match-end 0)
4011 (list 'face (org-get-todo-face 1)))
4012 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4013 (delete-region (match-beginning 1) (1- (match-end 0)))
4014 (goto-char (match-beginning 1))
4015 (insert (format org-agenda-todo-keyword-format s)))))
4016 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4017 pl (get-text-property 0 'prefix-length x))
4018 (when (and re
4019 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4020 x (or pl 0)) pl))
4021 (add-text-properties
4022 (or (match-end 1) (match-end 0)) (match-end 0)
4023 (list 'face (org-get-todo-face (match-string 2 x)))
4024 x)
4025 (setq x (concat (substring x 0 (match-end 1))
4026 (format org-agenda-todo-keyword-format
4027 (match-string 2 x))
4028 " "
4029 (substring x (match-end 3)))))
4030 x)))
4031
4032 (defsubst org-cmp-priority (a b)
4033 "Compare the priorities of string A and B."
4034 (let ((pa (or (get-text-property 1 'priority a) 0))
4035 (pb (or (get-text-property 1 'priority b) 0)))
4036 (cond ((> pa pb) +1)
4037 ((< pa pb) -1)
4038 (t nil))))
4039
4040 (defsubst org-cmp-effort (a b)
4041 "Compare the priorities of string A and B."
4042 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4043 (ea (or (get-text-property 1 'effort-minutes a) def))
4044 (eb (or (get-text-property 1 'effort-minutes b) def)))
4045 (cond ((> ea eb) +1)
4046 ((< ea eb) -1)
4047 (t nil))))
4048
4049 (defsubst org-cmp-category (a b)
4050 "Compare the string values of categories of strings A and B."
4051 (let ((ca (or (get-text-property 1 'org-category a) ""))
4052 (cb (or (get-text-property 1 'org-category b) "")))
4053 (cond ((string-lessp ca cb) -1)
4054 ((string-lessp cb ca) +1)
4055 (t nil))))
4056
4057 (defsubst org-cmp-todo-state (a b)
4058 "Compare the todo states of strings A and B."
4059 (let* ((ta (or (get-text-property 1 'todo-state a) ""))
4060 (tb (or (get-text-property 1 'todo-state b) ""))
4061 (la (- (length (member ta org-todo-keywords-for-agenda))))
4062 (lb (- (length (member tb org-todo-keywords-for-agenda))))
4063 (donepa (member ta org-done-keywords-for-agenda))
4064 (donepb (member tb org-done-keywords-for-agenda)))
4065 (cond ((and donepa (not donepb)) -1)
4066 ((and (not donepa) donepb) +1)
4067 ((< la lb) -1)
4068 ((< lb la) +1)
4069 (t nil))))
4070
4071 (defsubst org-cmp-tag (a b)
4072 "Compare the string values of the first tags of A and B."
4073 (let ((ta (car (last (get-text-property 1 'tags a))))
4074 (tb (car (last (get-text-property 1 'tags b)))))
4075 (cond ((not ta) +1)
4076 ((not tb) -1)
4077 ((string-lessp ta tb) -1)
4078 ((string-lessp tb ta) +1)
4079 (t nil))))
4080
4081 (defsubst org-cmp-time (a b)
4082 "Compare the time-of-day values of strings A and B."
4083 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4084 (ta (or (get-text-property 1 'time-of-day a) def))
4085 (tb (or (get-text-property 1 'time-of-day b) def)))
4086 (cond ((< ta tb) -1)
4087 ((< tb ta) +1)
4088 (t nil))))
4089
4090 (defun org-entries-lessp (a b)
4091 "Predicate for sorting agenda entries."
4092 ;; The following variables will be used when the form is evaluated.
4093 ;; So even though the compiler complains, keep them.
4094 (let* ((time-up (org-cmp-time a b))
4095 (time-down (if time-up (- time-up) nil))
4096 (priority-up (org-cmp-priority a b))
4097 (priority-down (if priority-up (- priority-up) nil))
4098 (effort-up (org-cmp-effort a b))
4099 (effort-down (if effort-up (- effort-up) nil))
4100 (category-up (org-cmp-category a b))
4101 (category-down (if category-up (- category-up) nil))
4102 (category-keep (if category-up +1 nil))
4103 (tag-up (org-cmp-tag a b))
4104 (tag-down (if tag-up (- tag-up) nil))
4105 (todo-state-up (org-cmp-todo-state a b))
4106 (todo-state-down (if todo-state-up (- todo-state-up) nil)))
4107 (cdr (assoc
4108 (eval (cons 'or org-agenda-sorting-strategy-selected))
4109 '((-1 . t) (1 . nil) (nil . nil))))))
4110
4111 ;;; Agenda restriction lock
4112
4113 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4114 "Overlay to mark the headline to which arenda commands are restricted.")
4115 (org-overlay-put org-agenda-restriction-lock-overlay
4116 'face 'org-agenda-restriction-lock)
4117 (org-overlay-put org-agenda-restriction-lock-overlay
4118 'help-echo "Agendas are currently limited to this subtree.")
4119 (org-detach-overlay org-agenda-restriction-lock-overlay)
4120
4121 (defun org-agenda-set-restriction-lock (&optional type)
4122 "Set restriction lock for agenda, to current subtree or file.
4123 Restriction will be the file if TYPE is `file', or if type is the
4124 universal prefix '(4), or if the cursor is before the first headline
4125 in the file. Otherwise, restriction will be to the current subtree."
4126 (interactive "P")
4127 (and (equal type '(4)) (setq type 'file))
4128 (setq type (cond
4129 (type type)
4130 ((org-at-heading-p) 'subtree)
4131 ((condition-case nil (org-back-to-heading t) (error nil))
4132 'subtree)
4133 (t 'file)))
4134 (if (eq type 'subtree)
4135 (progn
4136 (setq org-agenda-restrict t)
4137 (setq org-agenda-overriding-restriction 'subtree)
4138 (put 'org-agenda-files 'org-restrict
4139 (list (buffer-file-name (buffer-base-buffer))))
4140 (org-back-to-heading t)
4141 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4142 (move-marker org-agenda-restrict-begin (point))
4143 (move-marker org-agenda-restrict-end
4144 (save-excursion (org-end-of-subtree t)))
4145 (message "Locking agenda restriction to subtree"))
4146 (put 'org-agenda-files 'org-restrict
4147 (list (buffer-file-name (buffer-base-buffer))))
4148 (setq org-agenda-restrict nil)
4149 (setq org-agenda-overriding-restriction 'file)
4150 (move-marker org-agenda-restrict-begin nil)
4151 (move-marker org-agenda-restrict-end nil)
4152 (message "Locking agenda restriction to file"))
4153 (setq current-prefix-arg nil)
4154 (org-agenda-maybe-redo))
4155
4156 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4157 "Remove the agenda restriction lock."
4158 (interactive "P")
4159 (org-detach-overlay org-agenda-restriction-lock-overlay)
4160 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4161 (setq org-agenda-overriding-restriction nil)
4162 (setq org-agenda-restrict nil)
4163 (put 'org-agenda-files 'org-restrict nil)
4164 (move-marker org-agenda-restrict-begin nil)
4165 (move-marker org-agenda-restrict-end nil)
4166 (setq current-prefix-arg nil)
4167 (message "Agenda restriction lock removed")
4168 (or noupdate (org-agenda-maybe-redo)))
4169
4170 (defun org-agenda-maybe-redo ()
4171 "If there is any window showing the agenda view, update it."
4172 (let ((w (get-buffer-window org-agenda-buffer-name t))
4173 (w0 (selected-window)))
4174 (when w
4175 (select-window w)
4176 (org-agenda-redo)
4177 (select-window w0)
4178 (if org-agenda-overriding-restriction
4179 (message "Agenda view shifted to new %s restriction"
4180 org-agenda-overriding-restriction)
4181 (message "Agenda restriction lock removed")))))
4182
4183 ;;; Agenda commands
4184
4185 (defun org-agenda-check-type (error &rest types)
4186 "Check if agenda buffer is of allowed type.
4187 If ERROR is non-nil, throw an error, otherwise just return nil."
4188 (if (memq org-agenda-type types)
4189 t
4190 (if error
4191 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
4192 nil)))
4193
4194 (defun org-agenda-quit ()
4195 "Exit agenda by removing the window or the buffer."
4196 (interactive)
4197 (if org-agenda-columns-active
4198 (org-columns-quit)
4199 (let ((buf (current-buffer)))
4200 (if (not (one-window-p)) (delete-window))
4201 (kill-buffer buf)
4202 (org-agenda-reset-markers)
4203 (org-columns-remove-overlays)
4204 (setq org-agenda-archives-mode nil))
4205 ;; Maybe restore the pre-agenda window configuration.
4206 (and org-agenda-restore-windows-after-quit
4207 (not (eq org-agenda-window-setup 'other-frame))
4208 org-pre-agenda-window-conf
4209 (set-window-configuration org-pre-agenda-window-conf))))
4210
4211 (defun org-agenda-exit ()
4212 "Exit agenda by removing the window or the buffer.
4213 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
4214 Org-mode buffers visited directly by the user will not be touched."
4215 (interactive)
4216 (org-release-buffers org-agenda-new-buffers)
4217 (setq org-agenda-new-buffers nil)
4218 (org-agenda-quit))
4219
4220 (defun org-agenda-execute (arg)
4221 "Execute another agenda command, keeping same window.\\<global-map>
4222 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
4223 (interactive "P")
4224 (let ((org-agenda-window-setup 'current-window))
4225 (org-agenda arg)))
4226
4227 (defun org-save-all-org-buffers ()
4228 "Save all Org-mode buffers without user confirmation."
4229 (interactive)
4230 (message "Saving all Org-mode buffers...")
4231 (save-some-buffers t 'org-mode-p)
4232 (message "Saving all Org-mode buffers... done"))
4233
4234 (defun org-agenda-redo ()
4235 "Rebuild Agenda.
4236 When this is the global TODO list, a prefix argument will be interpreted."
4237 (interactive)
4238 (let* ((org-agenda-keep-modes t)
4239 (filter org-agenda-filter)
4240 (cols org-agenda-columns-active)
4241 (line (org-current-line))
4242 (window-line (- line (org-current-line (window-start))))
4243 (lprops (get 'org-agenda-redo-command 'org-lprops)))
4244 (and cols (org-columns-quit))
4245 (message "Rebuilding agenda buffer...")
4246 (org-let lprops '(eval org-agenda-redo-command))
4247 (setq org-agenda-undo-list nil
4248 org-agenda-pending-undo-list nil)
4249 (message "Rebuilding agenda buffer...done")
4250 (and filter (org-agenda-filter-apply filter))
4251 (and cols (interactive-p) (org-agenda-columns))
4252 (goto-line line)
4253 (recenter window-line)))
4254
4255
4256 (defvar org-global-tags-completion-table nil)
4257 (defvar org-agenda-filter-form nil)
4258 (defun org-agenda-filter-by-tag (strip &optional char narrow)
4259 "Keep only those lines in the agenda buffer that have a specific tag.
4260 The tag is selected with its fast selection letter, as configured.
4261 With prefix argument STRIP, remove all lines that do have the tag.
4262 A lisp caller can specify CHAR. NARROW means that the new tag should be
4263 used to narrow the search - the interactive user can also press `-' or `+'
4264 to switch to narrowing."
4265 (interactive "P")
4266 (let* ((alist org-tag-alist-for-agenda)
4267 (tag-chars (mapconcat
4268 (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
4269 alist ""))
4270 (efforts (org-split-string
4271 (or (cdr (assoc (concat org-effort-property "_ALL")
4272 org-global-properties))
4273 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
4274 (effort-op org-agenda-filter-effort-default-operator)
4275 (effort-prompt "")
4276 (inhibit-read-only t)
4277 (current org-agenda-filter)
4278 char a n tag tags)
4279 (unless char
4280 (message
4281 "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<]:effort: "
4282 (if narrow "Narrow" "Filter") tag-chars)
4283 (setq char (read-char)))
4284 (when (member char '(?+ ?-))
4285 ;; Narrowing down
4286 (cond ((equal char ?-) (setq strip t narrow t))
4287 ((equal char ?+) (setq strip nil narrow t)))
4288 (message
4289 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
4290 (setq char (read-char)))
4291 (when (member char '(?< ?> ?=))
4292 ;; An effort operator
4293 (setq effort-op (char-to-string char))
4294 (loop for i from 0 to 9 do
4295 (setq effort-prompt
4296 (concat
4297 effort-prompt " ["
4298 (if (= i 9) "0" (int-to-string (1+ i)))
4299 "]" (nth i efforts))))
4300 (setq alist nil) ; to make sure it will be interpreted as effort.
4301 (message "Effort%s: %s " effort-op effort-prompt)
4302 (setq char (read-char))
4303 (when (or (< char ?0) (> char ?9))
4304 (error "Need 1-9,0 to select effort" )))
4305 (when (equal char ?\t)
4306 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
4307 (org-set-local 'org-global-tags-completion-table
4308 (org-global-tags-completion-table)))
4309 (let ((completion-ignore-case t))
4310 (setq tag (org-ido-completing-read
4311 "Tag: " org-global-tags-completion-table))))
4312 (cond
4313 ((equal char ?/) (org-agenda-filter-by-tag-show-all))
4314 ((or (equal char ?\ )
4315 (setq a (rassoc char alist))
4316 (and (>= char ?0) (<= char ?9)
4317 (setq n (if (= char ?0) 9 (- char ?0 1))
4318 tag (concat effort-op (nth n efforts))
4319 a (cons tag nil)))
4320 (and tag (setq a (cons tag nil))))
4321 (org-agenda-filter-by-tag-show-all)
4322 (setq tag (car a))
4323 (setq org-agenda-filter
4324 (cons (concat (if strip "-" "+") tag)
4325 (if narrow current nil)))
4326 (org-agenda-filter-apply org-agenda-filter))
4327 (t (error "Invalid tag selection character %c" char)))))
4328
4329 (defun org-agenda-filter-by-tag-refine (strip &optional char)
4330 "Refine the current filter. See `org-agenda-filter-by-tag."
4331 (interactive "P")
4332 (org-agenda-filter-by-tag strip char 'refine))
4333
4334 (defun org-agenda-filter-make-matcher ()
4335 "Create the form that tests a line for the agenda filter."
4336 (let (f f1)
4337 (dolist (x org-agenda-filter)
4338 (if (member x '("-" "+"))
4339 (setq f1 '(not tags))
4340 (if (string-match "[<=>]" x)
4341 (setq f1 (org-agenda-filter-effort-form x))
4342 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
4343 (if (equal (string-to-char x) ?-)
4344 (setq f1 (list 'not f1))))
4345 (push f1 f))
4346 (cons 'and (nreverse f))))
4347
4348 (defun org-agenda-filter-effort-form (e)
4349 "Return the form to compare the effort of the current line with what E says.
4350 E looks line \"+<2:25\"."
4351 (let (op)
4352 (setq e (substring e 1))
4353 (setq op (string-to-char e) e (substring e 1))
4354 (setq op (if (equal op ?<) '<= (if (equal op ?>) '>= '=)))
4355 (list 'org-agenda-compare-effort (list 'quote op)
4356 (org-hh:mm-string-to-minutes e))))
4357
4358 (defun org-agenda-compare-effort (op value)
4359 "Compare the effort of the current line with VALUE, using OP.
4360 If the line does not have an effort defined, return nil."
4361 (let ((eff (get-text-property (point) 'effort-minutes)))
4362 (if (not eff)
4363 0 ; we don't have an effort defined, treat as 0
4364 (funcall op eff value))))
4365
4366 (defun org-agenda-filter-apply (filter)
4367 "Set FILTER as the new agenda filter and apply it."
4368 (let (tags)
4369 (setq org-agenda-filter filter
4370 org-agenda-filter-form (org-agenda-filter-make-matcher))
4371 (org-agenda-set-mode-name)
4372 (save-excursion
4373 (goto-char (point-min))
4374 (while (not (eobp))
4375 (if (get-text-property (point) 'org-marker)
4376 (progn
4377 (setq tags (get-text-property (point) 'tags))
4378 (if (not (eval org-agenda-filter-form))
4379 (org-agenda-filter-by-tag-hide-line))
4380 (beginning-of-line 2))
4381 (beginning-of-line 2))))))
4382
4383 (defun org-agenda-filter-by-tag-hide-line ()
4384 (let (ov)
4385 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
4386 (point-at-eol)))
4387 (org-overlay-put ov 'invisible t)
4388 (org-overlay-put ov 'type 'tags-filter)
4389 (push ov org-agenda-filter-overlays)))
4390
4391 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
4392 (setq pos (or pos (point)))
4393 (save-excursion
4394 (dolist (ov (org-overlays-at pos))
4395 (when (and (org-overlay-get ov 'invisible)
4396 (eq (org-overlay-get ov 'type) 'tags-filter))
4397 (goto-char pos)
4398 (if (< (org-overlay-start ov) (point-at-eol))
4399 (org-move-overlay ov (point-at-eol)
4400 (org-overlay-end ov)))))))
4401
4402 (defun org-agenda-filter-by-tag-show-all ()
4403 (mapc 'org-delete-overlay org-agenda-filter-overlays)
4404 (setq org-agenda-filter-overlays nil)
4405 (setq org-agenda-filter nil)
4406 (setq org-agenda-filter-form nil)
4407 (org-agenda-set-mode-name))
4408
4409 (defun org-agenda-manipulate-query-add ()
4410 "Manipulate the query by adding a search term with positive selection.
4411 Positive selection means, the term must be matched for selection of an entry."
4412 (interactive)
4413 (org-agenda-manipulate-query ?\[))
4414 (defun org-agenda-manipulate-query-subtract ()
4415 "Manipulate the query by adding a search term with negative selection.
4416 Negative selection means, term must not be matched for selection of an entry."
4417 (interactive)
4418 (org-agenda-manipulate-query ?\]))
4419 (defun org-agenda-manipulate-query-add-re ()
4420 "Manipulate the query by adding a search regexp with positive selection.
4421 Positive selection means, the regexp must match for selection of an entry."
4422 (interactive)
4423 (org-agenda-manipulate-query ?\{))
4424 (defun org-agenda-manipulate-query-subtract-re ()
4425 "Manipulate the query by adding a search regexp with negative selection.
4426 Negative selection means, regexp must not match for selection of an entry."
4427 (interactive)
4428 (org-agenda-manipulate-query ?\}))
4429 (defun org-agenda-manipulate-query (char)
4430 (cond
4431 ((memq org-agenda-type '(timeline agenda))
4432 (if (y-or-n-p "Re-display with inactive time stamps included? ")
4433 (let ((org-agenda-include-inactive-timestamps t))
4434 (org-agenda-redo))
4435 (error "Abort")))
4436 ((eq org-agenda-type 'search)
4437 (org-add-to-string
4438 'org-agenda-query-string
4439 (cdr (assoc char '((?\[ . " +") (?\] . " -")
4440 (?\{ . " +{}") (?\} . " -{}")))))
4441 (setq org-agenda-redo-command
4442 (list 'org-search-view
4443 org-todo-only
4444 org-agenda-query-string
4445 (+ (length org-agenda-query-string)
4446 (if (member char '(?\{ ?\})) 0 1))))
4447 (set-register org-agenda-query-register org-agenda-query-string)
4448 (org-agenda-redo))
4449 (t (error "Cannot manipulate query for %s-type agenda buffers"
4450 org-agenda-type))))
4451
4452 (defun org-add-to-string (var string)
4453 (set var (concat (symbol-value var) string)))
4454
4455 (defun org-agenda-goto-date (date)
4456 "Jump to DATE in agenda."
4457 (interactive (list (org-read-date)))
4458 (org-agenda-list nil date))
4459
4460 (defun org-agenda-goto-today ()
4461 "Go to today."
4462 (interactive)
4463 (org-agenda-check-type t 'timeline 'agenda)
4464 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
4465 (cond
4466 (tdpos (goto-char tdpos))
4467 ((eq org-agenda-type 'agenda)
4468 (let* ((sd (time-to-days
4469 (time-subtract (current-time)
4470 (list 0 (* 3600 org-extend-today-until) 0))))
4471 (comp (org-agenda-compute-time-span sd org-agenda-span))
4472 (org-agenda-overriding-arguments org-agenda-last-arguments))
4473 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
4474 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
4475 (org-agenda-redo)
4476 (org-agenda-find-same-or-today-or-agenda)))
4477 (t (error "Cannot find today")))))
4478
4479 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
4480 (goto-char
4481 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
4482 (text-property-any (point-min) (point-max) 'org-today t)
4483 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
4484 (point-min))))
4485
4486 (defun org-agenda-later (arg)
4487 "Go forward in time by thee current span.
4488 With prefix ARG, go forward that many times the current span."
4489 (interactive "p")
4490 (org-agenda-check-type t 'agenda)
4491 (let* ((span org-agenda-span)
4492 (sd org-starting-day)
4493 (greg (calendar-gregorian-from-absolute sd))
4494 (cnt (get-text-property (point) 'org-day-cnt))
4495 greg2 nd)
4496 (cond
4497 ((eq span 'day)
4498 (setq sd (+ arg sd) nd 1))
4499 ((eq span 'week)
4500 (setq sd (+ (* 7 arg) sd) nd 7))
4501 ((eq span 'month)
4502 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
4503 sd (calendar-absolute-from-gregorian greg2))
4504 (setcar greg2 (1+ (car greg2)))
4505 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
4506 ((eq span 'year)
4507 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
4508 sd (calendar-absolute-from-gregorian greg2))
4509 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
4510 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
4511 (let ((org-agenda-overriding-arguments
4512 (list (car org-agenda-last-arguments) sd nd t)))
4513 (org-agenda-redo)
4514 (org-agenda-find-same-or-today-or-agenda cnt))))
4515
4516 (defun org-agenda-earlier (arg)
4517 "Go backward in time by the current span.
4518 With prefix ARG, go backward that many times the current span."
4519 (interactive "p")
4520 (org-agenda-later (- arg)))
4521
4522 (defun org-agenda-day-view (&optional day-of-year)
4523 "Switch to daily view for agenda.
4524 With argument DAY-OF-YEAR, switch to that day of the year."
4525 (interactive "P")
4526 (setq org-agenda-ndays 1)
4527 (org-agenda-change-time-span 'day day-of-year))
4528 (defun org-agenda-week-view (&optional iso-week)
4529 "Switch to daily view for agenda.
4530 With argument ISO-WEEK, switch to the corresponding ISO week.
4531 If ISO-WEEK has more then 2 digits, only the last two encode the
4532 week. Any digits before this encode a year. So 200712 means
4533 week 12 of year 2007. Years in the range 1938-2037 can also be
4534 written as 2-digit years."
4535 (interactive "P")
4536 (setq org-agenda-ndays 7)
4537 (org-agenda-change-time-span 'week iso-week))
4538 (defun org-agenda-month-view (&optional month)
4539 "Switch to monthly view for agenda.
4540 With argument MONTH, switch to that month."
4541 (interactive "P")
4542 (org-agenda-change-time-span 'month month))
4543 (defun org-agenda-year-view (&optional year)
4544 "Switch to yearly view for agenda.
4545 With argument YEAR, switch to that year.
4546 If MONTH has more then 2 digits, only the last two encode the
4547 month. Any digits before this encode a year. So 200712 means
4548 December year 2007. Years in the range 1938-2037 can also be
4549 written as 2-digit years."
4550 (interactive "P")
4551 (when year
4552 (setq year (org-small-year-to-year year)))
4553 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
4554 (org-agenda-change-time-span 'year year)
4555 (error "Abort")))
4556
4557 (defun org-agenda-change-time-span (span &optional n)
4558 "Change the agenda view to SPAN.
4559 SPAN may be `day', `week', `month', `year'."
4560 (org-agenda-check-type t 'agenda)
4561 (if (and (not n) (equal org-agenda-span span))
4562 (error "Viewing span is already \"%s\"" span))
4563 (let* ((sd (or (get-text-property (point) 'day)
4564 org-starting-day))
4565 (computed (org-agenda-compute-time-span sd span n))
4566 (org-agenda-overriding-arguments
4567 (list (car org-agenda-last-arguments)
4568 (car computed) (cdr computed) t)))
4569 (org-agenda-redo)
4570 (org-agenda-find-same-or-today-or-agenda))
4571 (org-agenda-set-mode-name)
4572 (message "Switched to %s view" span))
4573
4574 (defun org-agenda-compute-time-span (sd span &optional n)
4575 "Compute starting date and number of days for agenda.
4576 SPAN may be `day', `week', `month', `year'. The return value
4577 is a cons cell with the starting date and the number of days,
4578 so that the date SD will be in that range."
4579 (let* ((greg (calendar-gregorian-from-absolute sd))
4580 (dg (nth 1 greg))
4581 (mg (car greg))
4582 (yg (nth 2 greg))
4583 nd w1 y1 m1 thisweek)
4584 (cond
4585 ((eq span 'day)
4586 (when n
4587 (setq sd (+ (calendar-absolute-from-gregorian
4588 (list mg 1 yg))
4589 n -1)))
4590 (setq nd 1))
4591 ((eq span 'week)
4592 (let* ((nt (calendar-day-of-week
4593 (calendar-gregorian-from-absolute sd)))
4594 (d (if org-agenda-start-on-weekday
4595 (- nt org-agenda-start-on-weekday)
4596 0)))
4597 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
4598 (when n
4599 (require 'cal-iso)
4600 (setq thisweek (car (calendar-iso-from-absolute sd)))
4601 (when (> n 99)
4602 (setq y1 (org-small-year-to-year (/ n 100))
4603 n (mod n 100)))
4604 (setq sd
4605 (calendar-absolute-from-iso
4606 (list n 1
4607 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
4608 (setq nd 7)))
4609 ((eq span 'month)
4610 (when (and n (> n 99))
4611 (setq y1 (org-small-year-to-year (/ n 100))
4612 n (mod n 100)))
4613 (setq sd (calendar-absolute-from-gregorian
4614 (list (or n mg) 1 (or y1 yg)))
4615 nd (- (calendar-absolute-from-gregorian
4616 (list (1+ (or n mg)) 1 (or y1 yg)))
4617 sd)))
4618 ((eq span 'year)
4619 (setq sd (calendar-absolute-from-gregorian
4620 (list 1 1 (or n yg)))
4621 nd (- (calendar-absolute-from-gregorian
4622 (list 1 1 (1+ (or n yg))))
4623 sd))))
4624 (cons sd nd)))
4625
4626 (defun org-agenda-next-date-line (&optional arg)
4627 "Jump to the next line indicating a date in agenda buffer."
4628 (interactive "p")
4629 (org-agenda-check-type t 'agenda 'timeline)
4630 (beginning-of-line 1)
4631 ;; This does not work if user makes date format that starts with a blank
4632 (if (looking-at "^\\S-") (forward-char 1))
4633 (if (not (re-search-forward "^\\S-" nil t arg))
4634 (progn
4635 (backward-char 1)
4636 (error "No next date after this line in this buffer")))
4637 (goto-char (match-beginning 0)))
4638
4639 (defun org-agenda-previous-date-line (&optional arg)
4640 "Jump to the previous line indicating a date in agenda buffer."
4641 (interactive "p")
4642 (org-agenda-check-type t 'agenda 'timeline)
4643 (beginning-of-line 1)
4644 (if (not (re-search-backward "^\\S-" nil t arg))
4645 (error "No previous date before this line in this buffer")))
4646
4647 ;; Initialize the highlight
4648 (defvar org-hl (org-make-overlay 1 1))
4649 (org-overlay-put org-hl 'face 'highlight)
4650
4651 (defun org-highlight (begin end &optional buffer)
4652 "Highlight a region with overlay."
4653 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
4654 org-hl begin end (or buffer (current-buffer))))
4655
4656 (defun org-unhighlight ()
4657 "Detach overlay INDEX."
4658 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
4659
4660 ;; FIXME this is currently not used.
4661 (defun org-highlight-until-next-command (beg end &optional buffer)
4662 "Move the highlight overlay to BEG/END, remove it before the next command."
4663 (org-highlight beg end buffer)
4664 (add-hook 'pre-command-hook 'org-unhighlight-once))
4665 (defun org-unhighlight-once ()
4666 "Remove the highlight from its position, and this function from the hook."
4667 (remove-hook 'pre-command-hook 'org-unhighlight-once)
4668 (org-unhighlight))
4669
4670 (defun org-agenda-follow-mode ()
4671 "Toggle follow mode in an agenda buffer."
4672 (interactive)
4673 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
4674 (org-agenda-set-mode-name)
4675 (message "Follow mode is %s"
4676 (if org-agenda-follow-mode "on" "off")))
4677
4678 (defun org-agenda-clockreport-mode ()
4679 "Toggle clocktable mode in an agenda buffer."
4680 (interactive)
4681 (org-agenda-check-type t 'agenda)
4682 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
4683 (org-agenda-set-mode-name)
4684 (org-agenda-redo)
4685 (message "Clocktable mode is %s"
4686 (if org-agenda-clockreport-mode "on" "off")))
4687
4688 (defun org-agenda-log-mode (&optional special)
4689 "Toggle log mode in an agenda buffer.
4690 With argument SPECIAL, show all possible log items, not only the ones
4691 configured in `org-agenda-log-mode-items'.
4692 With a double `C-u' prefix arg, show *only* log items, nothing else."
4693 (interactive "P")
4694 (org-agenda-check-type t 'agenda 'timeline)
4695 (setq org-agenda-show-log
4696 (if (equal special '(16))
4697 'only
4698 (if special '(closed clock state)
4699 (not org-agenda-show-log))))
4700 (org-agenda-set-mode-name)
4701 (org-agenda-redo)
4702 (message "Log mode is %s"
4703 (if org-agenda-show-log "on" "off")))
4704
4705 (defun org-agenda-archives-mode (&optional with-files)
4706 "Toggle log mode in an agenda buffer."
4707 (interactive "P")
4708 (setq org-agenda-archives-mode
4709 (if with-files t (if org-agenda-archives-mode nil 'trees)))
4710 (org-agenda-set-mode-name)
4711 (org-agenda-redo)
4712 (message
4713 "%s"
4714 (cond
4715 ((eq org-agenda-archives-mode nil)
4716 "No archives are included")
4717 ((eq org-agenda-archives-mode 'trees)
4718 (format "Trees with :%s: tag are included" org-archive-tag))
4719 ((eq org-agenda-archives-mode t)
4720 (format "Trees with :%s: tag and all active archive files are included"
4721 org-archive-tag)))))
4722
4723 (defun org-agenda-toggle-diary ()
4724 "Toggle diary inclusion in an agenda buffer."
4725 (interactive)
4726 (org-agenda-check-type t 'agenda)
4727 (setq org-agenda-include-diary (not org-agenda-include-diary))
4728 (org-agenda-redo)
4729 (org-agenda-set-mode-name)
4730 (message "Diary inclusion turned %s"
4731 (if org-agenda-include-diary "on" "off")))
4732
4733 (defun org-agenda-toggle-time-grid ()
4734 "Toggle time grid in an agenda buffer."
4735 (interactive)
4736 (org-agenda-check-type t 'agenda)
4737 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
4738 (org-agenda-redo)
4739 (org-agenda-set-mode-name)
4740 (message "Time-grid turned %s"
4741 (if org-agenda-use-time-grid "on" "off")))
4742
4743 (defun org-agenda-set-mode-name ()
4744 "Set the mode name to indicate all the small mode settings."
4745 (setq mode-name
4746 (concat "Org-Agenda"
4747 (if (equal org-agenda-ndays 1) " Day" "")
4748 (if (equal org-agenda-ndays 7) " Week" "")
4749 (if org-agenda-follow-mode " Follow" "")
4750 (if org-agenda-include-diary " Diary" "")
4751 (if org-agenda-use-time-grid " Grid" "")
4752 (if (consp org-agenda-show-log) " LogAll"
4753 (if org-agenda-show-log " Log" ""))
4754 (if org-agenda-filter
4755 (concat " {" (mapconcat 'identity org-agenda-filter "") "}")
4756 "")
4757 (if org-agenda-archives-mode
4758 (if (eq org-agenda-archives-mode t)
4759 " Archives"
4760 (format " :%s:" org-archive-tag))
4761 "")
4762 (if org-agenda-clockreport-mode " Clock" "")))
4763 (force-mode-line-update))
4764
4765 (defun org-agenda-post-command-hook ()
4766 (and (eolp) (not (bolp)) (backward-char 1))
4767 (setq org-agenda-type
4768 (or (get-text-property (point) 'org-agenda-type)
4769 (get-text-property (max (point-min) (1- (point)))
4770 'org-agenda-type)))
4771 (if (and org-agenda-follow-mode
4772 (get-text-property (point) 'org-marker))
4773 (org-agenda-show)))
4774
4775 (defun org-agenda-show-priority ()
4776 "Show the priority of the current item.
4777 This priority is composed of the main priority given with the [#A] cookies,
4778 and by additional input from the age of a schedules or deadline entry."
4779 (interactive)
4780 (let* ((pri (get-text-property (point-at-bol) 'priority)))
4781 (message "Priority is %d" (if pri pri -1000))))
4782
4783 (defun org-agenda-show-tags ()
4784 "Show the tags applicable to the current item."
4785 (interactive)
4786 (let* ((tags (get-text-property (point-at-bol) 'tags)))
4787 (if tags
4788 (message "Tags are :%s:"
4789 (org-no-properties (mapconcat 'identity tags ":")))
4790 (message "No tags associated with this line"))))
4791
4792 (defun org-agenda-goto (&optional highlight)
4793 "Go to the Org-mode file which contains the item at point."
4794 (interactive)
4795 (let* ((marker (or (get-text-property (point) 'org-marker)
4796 (org-agenda-error)))
4797 (buffer (marker-buffer marker))
4798 (pos (marker-position marker)))
4799 (switch-to-buffer-other-window buffer)
4800 (widen)
4801 (goto-char pos)
4802 (when (org-mode-p)
4803 (org-show-context 'agenda)
4804 (save-excursion
4805 (and (outline-next-heading)
4806 (org-flag-heading nil)))) ; show the next heading
4807 (recenter (/ (window-height) 2))
4808 (run-hooks 'org-agenda-after-show-hook)
4809 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
4810
4811 (defvar org-agenda-after-show-hook nil
4812 "Normal hook run after an item has been shown from the agenda.
4813 Point is in the buffer where the item originated.")
4814
4815 (defun org-agenda-kill ()
4816 "Kill the entry or subtree belonging to the current agenda entry."
4817 (interactive)
4818 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
4819 (let* ((marker (or (get-text-property (point) 'org-marker)
4820 (org-agenda-error)))
4821 (buffer (marker-buffer marker))
4822 (pos (marker-position marker))
4823 (type (get-text-property (point) 'type))
4824 dbeg dend (n 0) conf)
4825 (org-with-remote-undo buffer
4826 (with-current-buffer buffer
4827 (save-excursion
4828 (goto-char pos)
4829 (if (and (org-mode-p) (not (member type '("sexp"))))
4830 (setq dbeg (progn (org-back-to-heading t) (point))
4831 dend (org-end-of-subtree t t))
4832 (setq dbeg (point-at-bol)
4833 dend (min (point-max) (1+ (point-at-eol)))))
4834 (goto-char dbeg)
4835 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
4836 (setq conf (or (eq t org-agenda-confirm-kill)
4837 (and (numberp org-agenda-confirm-kill)
4838 (> n org-agenda-confirm-kill))))
4839 (and conf
4840 (not (y-or-n-p
4841 (format "Delete entry with %d lines in buffer \"%s\"? "
4842 n (buffer-name buffer))))
4843 (error "Abort"))
4844 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
4845 (with-current-buffer buffer (delete-region dbeg dend))
4846 (message "Agenda item and source killed"))))
4847
4848 (defun org-agenda-archive ()
4849 "Archive the entry or subtree belonging to the current agenda entry."
4850 (interactive)
4851 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
4852 (let* ((marker (or (get-text-property (point) 'org-marker)
4853 (org-agenda-error)))
4854 (buffer (marker-buffer marker))
4855 (pos (marker-position marker)))
4856 (org-with-remote-undo buffer
4857 (with-current-buffer buffer
4858 (if (org-mode-p)
4859 (save-excursion
4860 (goto-char pos)
4861 (org-remove-subtree-entries-from-agenda)
4862 (org-back-to-heading t)
4863 (org-archive-subtree))
4864 (error "Archiving works only in Org-mode files"))))))
4865
4866 (defun org-agenda-archive-to-archive-sibling ()
4867 "Move the entry to the archive sibling."
4868 (interactive)
4869 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
4870 (let* ((marker (or (get-text-property (point) 'org-marker)
4871 (org-agenda-error)))
4872 (buffer (marker-buffer marker))
4873 (pos (marker-position marker)))
4874 (org-with-remote-undo buffer
4875 (with-current-buffer buffer
4876 (if (org-mode-p)
4877 (save-excursion
4878 (goto-char pos)
4879 (org-remove-subtree-entries-from-agenda)
4880 (org-back-to-heading t)
4881 (org-archive-to-archive-sibling))
4882 (error "Archiving works only in Org-mode files"))))))
4883
4884 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
4885 "Remove all lines in the agenda that correspond to a given subtree.
4886 The subtree is the one in buffer BUF, starting at BEG and ending at END.
4887 If this information is not given, the function uses the tree at point."
4888 (let ((buf (or buf (current-buffer))) m p)
4889 (save-excursion
4890 (unless (and beg end)
4891 (org-back-to-heading t)
4892 (setq beg (point))
4893 (org-end-of-subtree t)
4894 (setq end (point)))
4895 (set-buffer (get-buffer org-agenda-buffer-name))
4896 (save-excursion
4897 (goto-char (point-max))
4898 (beginning-of-line 1)
4899 (while (not (bobp))
4900 (when (and (setq m (get-text-property (point) 'org-marker))
4901 (equal buf (marker-buffer m))
4902 (setq p (marker-position m))
4903 (>= p beg)
4904 (<= p end))
4905 (let ((inhibit-read-only t))
4906 (delete-region (point-at-bol) (1+ (point-at-eol)))))
4907 (beginning-of-line 0))))))
4908
4909 (defun org-agenda-open-link ()
4910 "Follow the link in the current line, if any."
4911 (interactive)
4912 (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
4913 (save-excursion
4914 (save-restriction
4915 (narrow-to-region (point-at-bol) (point-at-eol))
4916 (org-open-at-point))))
4917
4918 (defun org-agenda-copy-local-variable (var)
4919 "Get a variable from a referenced buffer and install it here."
4920 (let ((m (get-text-property (point) 'org-marker)))
4921 (when (and m (buffer-live-p (marker-buffer m)))
4922 (org-set-local var (with-current-buffer (marker-buffer m)
4923 (symbol-value var))))))
4924
4925 (defun org-agenda-switch-to (&optional delete-other-windows)
4926 "Go to the Org-mode file which contains the item at point."
4927 (interactive)
4928 (let* ((marker (or (get-text-property (point) 'org-marker)
4929 (org-agenda-error)))
4930 (buffer (marker-buffer marker))
4931 (pos (marker-position marker)))
4932 (switch-to-buffer buffer)
4933 (and delete-other-windows (delete-other-windows))
4934 (widen)
4935 (goto-char pos)
4936 (when (org-mode-p)
4937 (org-show-context 'agenda)
4938 (save-excursion
4939 (and (outline-next-heading)
4940 (org-flag-heading nil)))))) ; show the next heading
4941
4942 (defun org-agenda-goto-mouse (ev)
4943 "Go to the Org-mode file which contains the item at the mouse click."
4944 (interactive "e")
4945 (mouse-set-point ev)
4946 (org-agenda-goto))
4947
4948 (defun org-agenda-show ()
4949 "Display the Org-mode file which contains the item at point."
4950 (interactive)
4951 (let ((win (selected-window)))
4952 (org-agenda-goto t)
4953 (select-window win)))
4954
4955 (defun org-agenda-recenter (arg)
4956 "Display the Org-mode file which contains the item at point and recenter."
4957 (interactive "P")
4958 (let ((win (selected-window)))
4959 (org-agenda-goto t)
4960 (recenter arg)
4961 (select-window win)))
4962
4963 (defun org-agenda-show-mouse (ev)
4964 "Display the Org-mode file which contains the item at the mouse click."
4965 (interactive "e")
4966 (mouse-set-point ev)
4967 (org-agenda-show))
4968
4969 (defun org-agenda-check-no-diary ()
4970 "Check if the entry is a diary link and abort if yes."
4971 (if (get-text-property (point) 'org-agenda-diary-link)
4972 (org-agenda-error)))
4973
4974 (defun org-agenda-error ()
4975 (error "Command not allowed in this line"))
4976
4977 (defun org-agenda-tree-to-indirect-buffer ()
4978 "Show the subtree corresponding to the current entry in an indirect buffer.
4979 This calls the command `org-tree-to-indirect-buffer' from the original
4980 Org-mode buffer.
4981 With numerical prefix arg ARG, go up to this level and then take that tree.
4982 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
4983 dedicated frame)."
4984 (interactive)
4985 (org-agenda-check-no-diary)
4986 (let* ((marker (or (get-text-property (point) 'org-marker)
4987 (org-agenda-error)))
4988 (buffer (marker-buffer marker))
4989 (pos (marker-position marker)))
4990 (with-current-buffer buffer
4991 (save-excursion
4992 (goto-char pos)
4993 (call-interactively 'org-tree-to-indirect-buffer)))))
4994
4995 (defvar org-last-heading-marker (make-marker)
4996 "Marker pointing to the headline that last changed its TODO state
4997 by a remote command from the agenda.")
4998
4999 (defun org-agenda-todo-nextset ()
5000 "Switch TODO entry to next sequence."
5001 (interactive)
5002 (org-agenda-todo 'nextset))
5003
5004 (defun org-agenda-todo-previousset ()
5005 "Switch TODO entry to previous sequence."
5006 (interactive)
5007 (org-agenda-todo 'previousset))
5008
5009 (defun org-agenda-todo (&optional arg)
5010 "Cycle TODO state of line at point, also in Org-mode file.
5011 This changes the line at point, all other lines in the agenda referring to
5012 the same tree node, and the headline of the tree node in the Org-mode file."
5013 (interactive "P")
5014 (org-agenda-check-no-diary)
5015 (let* ((col (current-column))
5016 (marker (or (get-text-property (point) 'org-marker)
5017 (org-agenda-error)))
5018 (buffer (marker-buffer marker))
5019 (pos (marker-position marker))
5020 (hdmarker (get-text-property (point) 'org-hd-marker))
5021 (todayp (equal (get-text-property (point) 'day)
5022 (time-to-days (current-time))))
5023 (inhibit-read-only t)
5024 org-agenda-headline-snapshot-before-repeat newhead just-one)
5025 (org-with-remote-undo buffer
5026 (with-current-buffer buffer
5027 (widen)
5028 (goto-char pos)
5029 (org-show-context 'agenda)
5030 (save-excursion
5031 (and (outline-next-heading)
5032 (org-flag-heading nil))) ; show the next heading
5033 (org-todo arg)
5034 (and (bolp) (forward-char 1))
5035 (setq newhead (org-get-heading))
5036 (when (and (org-bound-and-true-p
5037 org-agenda-headline-snapshot-before-repeat)
5038 (not (equal org-agenda-headline-snapshot-before-repeat
5039 newhead))
5040 todayp)
5041 (setq newhead org-agenda-headline-snapshot-before-repeat
5042 just-one t))
5043 (save-excursion
5044 (org-back-to-heading)
5045 (move-marker org-last-heading-marker (point))))
5046 (beginning-of-line 1)
5047 (save-excursion
5048 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
5049 (org-move-to-column col))))
5050
5051 (defun org-agenda-add-note (&optional arg)
5052 "Add a time-stamped note to the entry at point."
5053 (interactive "P")
5054 (org-agenda-check-no-diary)
5055 (let* ((marker (or (get-text-property (point) 'org-marker)
5056 (org-agenda-error)))
5057 (buffer (marker-buffer marker))
5058 (pos (marker-position marker))
5059 (hdmarker (get-text-property (point) 'org-hd-marker))
5060 (inhibit-read-only t))
5061 (with-current-buffer buffer
5062 (widen)
5063 (goto-char pos)
5064 (org-show-context 'agenda)
5065 (save-excursion
5066 (and (outline-next-heading)
5067 (org-flag-heading nil))) ; show the next heading
5068 (org-add-note))))
5069
5070 (defun org-agenda-change-all-lines (newhead hdmarker
5071 &optional fixface just-this
5072 force-tags)
5073 "Change all lines in the agenda buffer which match HDMARKER.
5074 The new content of the line will be NEWHEAD (as modified by
5075 `org-format-agenda-item'). HDMARKER is checked with
5076 `equal' against all `org-hd-marker' text properties in the file.
5077 If FIXFACE is non-nil, the face of each item is modified acording to
5078 the new TODO state.
5079 If JUST-THIS is non-nil, change just the current line, not all.
5080 If FORCE-TAGS is non nil, the car of it ar the new tags."
5081 (let* ((inhibit-read-only t)
5082 (line (org-current-line))
5083 props m pl undone-face done-face finish new dotime cat tags)
5084 (save-excursion
5085 (goto-char (point-max))
5086 (beginning-of-line 1)
5087 (while (not finish)
5088 (setq finish (bobp))
5089 (when (and (setq m (get-text-property (point) 'org-hd-marker))
5090 (or (not just-this) (= (org-current-line) line))
5091 (equal m hdmarker))
5092 (setq props (text-properties-at (point))
5093 dotime (get-text-property (point) 'dotime)
5094 cat (get-text-property (point) 'org-category)
5095 tags (if force-tags
5096 (car force-tags)
5097 (get-text-property (point) 'tags))
5098 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
5099 pl (get-text-property (point) 'prefix-length)
5100 undone-face (get-text-property (point) 'undone-face)
5101 done-face (get-text-property (point) 'done-face))
5102 (org-move-to-column pl)
5103 (cond
5104 ((equal new "")
5105 (beginning-of-line 1)
5106 (and (looking-at ".*\n?") (replace-match "")))
5107 ((looking-at ".*")
5108 (replace-match new t t)
5109 (beginning-of-line 1)
5110 (add-text-properties (point-at-bol) (point-at-eol) props)
5111 (when fixface
5112 (add-text-properties
5113 (point-at-bol) (point-at-eol)
5114 (list 'face
5115 (if org-last-todo-state-is-todo
5116 undone-face done-face))))
5117 (org-agenda-highlight-todo 'line)
5118 (beginning-of-line 1))
5119 (t (error "Line update did not work"))))
5120 (beginning-of-line 0)))
5121 (org-finalize-agenda)))
5122
5123 (defun org-agenda-align-tags (&optional line)
5124 "Align all tags in agenda items to `org-agenda-tags-column'."
5125 (let ((inhibit-read-only t) l c)
5126 (save-excursion
5127 (goto-char (if line (point-at-bol) (point-min)))
5128 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
5129 (if line (point-at-eol) nil) t)
5130 (add-text-properties
5131 (match-beginning 2) (match-end 2)
5132 (list 'face (delq nil (let ((prop (get-text-property
5133 (match-beginning 2) 'face)))
5134 (or (listp prop) (setq prop (list prop)))
5135 (if (memq 'org-tag prop)
5136 prop
5137 (cons 'org-tag prop))))))
5138 (setq l (- (match-end 2) (match-beginning 2))
5139 c (if (< org-agenda-tags-column 0)
5140 (- (abs org-agenda-tags-column) l)
5141 org-agenda-tags-column))
5142 (delete-region (match-beginning 1) (match-end 1))
5143 (goto-char (match-beginning 1))
5144 (insert (org-add-props
5145 (make-string (max 1 (- c (current-column))) ?\ )
5146 (text-properties-at (point)))))
5147 (goto-char (point-min))
5148 (org-font-lock-add-tag-faces (point-max)))))
5149
5150 (defun org-agenda-priority-up ()
5151 "Increase the priority of line at point, also in Org-mode file."
5152 (interactive)
5153 (org-agenda-priority 'up))
5154
5155 (defun org-agenda-priority-down ()
5156 "Decrease the priority of line at point, also in Org-mode file."
5157 (interactive)
5158 (org-agenda-priority 'down))
5159
5160 (defun org-agenda-priority (&optional force-direction)
5161 "Set the priority of line at point, also in Org-mode file.
5162 This changes the line at point, all other lines in the agenda referring to
5163 the same tree node, and the headline of the tree node in the Org-mode file."
5164 (interactive)
5165 (org-agenda-check-no-diary)
5166 (let* ((marker (or (get-text-property (point) 'org-marker)
5167 (org-agenda-error)))
5168 (hdmarker (get-text-property (point) 'org-hd-marker))
5169 (buffer (marker-buffer hdmarker))
5170 (pos (marker-position hdmarker))
5171 (inhibit-read-only t)
5172 newhead)
5173 (org-with-remote-undo buffer
5174 (with-current-buffer buffer
5175 (widen)
5176 (goto-char pos)
5177 (org-show-context 'agenda)
5178 (save-excursion
5179 (and (outline-next-heading)
5180 (org-flag-heading nil))) ; show the next heading
5181 (funcall 'org-priority force-direction)
5182 (end-of-line 1)
5183 (setq newhead (org-get-heading)))
5184 (org-agenda-change-all-lines newhead hdmarker)
5185 (beginning-of-line 1))))
5186
5187 ;; FIXME: should fix the tags property of the agenda line.
5188 (defun org-agenda-set-tags ()
5189 "Set tags for the current headline."
5190 (interactive)
5191 (org-agenda-check-no-diary)
5192 (if (and (org-region-active-p) (interactive-p))
5193 (call-interactively 'org-change-tag-in-region)
5194 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5195 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5196 (org-agenda-error)))
5197 (buffer (marker-buffer hdmarker))
5198 (pos (marker-position hdmarker))
5199 (inhibit-read-only t)
5200 newhead tags)
5201 (org-with-remote-undo buffer
5202 (with-current-buffer buffer
5203 (widen)
5204 (goto-char pos)
5205 (save-excursion
5206 (org-show-context 'agenda))
5207 (save-excursion
5208 (and (outline-next-heading)
5209 (org-flag-heading nil))) ; show the next heading
5210 (goto-char pos)
5211 (call-interactively 'org-set-tags)
5212 (setq tags (org-get-tags-at))
5213 (end-of-line 1)
5214 (setq newhead (org-get-heading)))
5215 (org-agenda-change-all-lines newhead hdmarker nil nil (list tags))
5216 (beginning-of-line 1)))))
5217
5218 (defun org-agenda-toggle-archive-tag ()
5219 "Toggle the archive tag for the current entry."
5220 (interactive)
5221 (org-agenda-check-no-diary)
5222 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
5223 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
5224 (org-agenda-error)))
5225 (buffer (marker-buffer hdmarker))
5226 (pos (marker-position hdmarker))
5227 (inhibit-read-only t)
5228 newhead)
5229 (org-with-remote-undo buffer
5230 (with-current-buffer buffer
5231 (widen)
5232 (goto-char pos)
5233 (org-show-context 'agenda)
5234 (save-excursion
5235 (and (outline-next-heading)
5236 (org-flag-heading nil))) ; show the next heading
5237 (call-interactively 'org-toggle-archive-tag)
5238 (end-of-line 1)
5239 (setq newhead (org-get-heading)))
5240 (org-agenda-change-all-lines newhead hdmarker)
5241 (beginning-of-line 1))))
5242
5243 (defun org-agenda-date-later (arg &optional what)
5244 "Change the date of this item to one day later."
5245 (interactive "p")
5246 (org-agenda-check-type t 'agenda 'timeline)
5247 (org-agenda-check-no-diary)
5248 (let* ((marker (or (get-text-property (point) 'org-marker)
5249 (org-agenda-error)))
5250 (buffer (marker-buffer marker))
5251 (pos (marker-position marker)))
5252 (org-with-remote-undo buffer
5253 (with-current-buffer buffer
5254 (widen)
5255 (goto-char pos)
5256 (if (not (org-at-timestamp-p))
5257 (error "Cannot find time stamp"))
5258 (org-timestamp-change arg (or what 'day)))
5259 (org-agenda-show-new-time marker org-last-changed-timestamp))
5260 (message "Time stamp changed to %s" org-last-changed-timestamp)))
5261
5262 (defun org-agenda-date-earlier (arg &optional what)
5263 "Change the date of this item to one day earlier."
5264 (interactive "p")
5265 (org-agenda-date-later (- arg) what))
5266
5267 (defun org-agenda-show-new-time (marker stamp &optional prefix)
5268 "Show new date stamp via text properties."
5269 ;; We use text properties to make this undoable
5270 (let ((inhibit-read-only t)
5271 (buffer-invisibility-spec))
5272 (setq stamp (concat " " prefix " => " stamp))
5273 (save-excursion
5274 (goto-char (point-max))
5275 (while (not (bobp))
5276 (when (equal marker (get-text-property (point) 'org-marker))
5277 (org-move-to-column (- (window-width) (length stamp)) t)
5278 (org-agenda-fix-tags-filter-overlays-at (point))
5279 (if (featurep 'xemacs)
5280 ;; Use `duplicable' property to trigger undo recording
5281 (let ((ex (make-extent nil nil))
5282 (gl (make-glyph stamp)))
5283 (set-glyph-face gl 'secondary-selection)
5284 (set-extent-properties
5285 ex (list 'invisible t 'end-glyph gl 'duplicable t))
5286 (insert-extent ex (1- (point)) (point-at-eol)))
5287 (add-text-properties
5288 (1- (point)) (point-at-eol)
5289 (list 'display (org-add-props stamp nil
5290 'face 'secondary-selection))))
5291 (beginning-of-line 1))
5292 (beginning-of-line 0)))))
5293
5294 (defun org-agenda-date-prompt (arg)
5295 "Change the date of this item. Date is prompted for, with default today.
5296 The prefix ARG is passed to the `org-time-stamp' command and can therefore
5297 be used to request time specification in the time stamp."
5298 (interactive "P")
5299 (org-agenda-check-type t 'agenda 'timeline)
5300 (org-agenda-check-no-diary)
5301 (let* ((marker (or (get-text-property (point) 'org-marker)
5302 (org-agenda-error)))
5303 (buffer (marker-buffer marker))
5304 (pos (marker-position marker)))
5305 (org-with-remote-undo buffer
5306 (with-current-buffer buffer
5307 (widen)
5308 (goto-char pos)
5309 (if (not (org-at-timestamp-p))
5310 (error "Cannot find time stamp"))
5311 (org-time-stamp arg)
5312 (message "Time stamp changed to %s" org-last-changed-timestamp)))))
5313
5314 (defun org-agenda-schedule (arg)
5315 "Schedule the item at point."
5316 (interactive "P")
5317 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5318 (org-agenda-check-no-diary)
5319 (let* ((marker (or (get-text-property (point) 'org-marker)
5320 (org-agenda-error)))
5321 (type (marker-insertion-type marker))
5322 (buffer (marker-buffer marker))
5323 (pos (marker-position marker))
5324 (org-insert-labeled-timestamps-at-point nil)
5325 ts)
5326 (when type (message "%s" type) (sit-for 3))
5327 (set-marker-insertion-type marker t)
5328 (org-with-remote-undo buffer
5329 (with-current-buffer buffer
5330 (widen)
5331 (goto-char pos)
5332 (setq ts (org-schedule arg)))
5333 (org-agenda-show-new-time marker ts "S"))
5334 (message "Item scheduled for %s" ts)))
5335
5336 (defun org-agenda-deadline (arg)
5337 "Schedule the item at point."
5338 (interactive "P")
5339 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
5340 (org-agenda-check-no-diary)
5341 (let* ((marker (or (get-text-property (point) 'org-marker)
5342 (org-agenda-error)))
5343 (buffer (marker-buffer marker))
5344 (pos (marker-position marker))
5345 (org-insert-labeled-timestamps-at-point nil)
5346 ts)
5347 (org-with-remote-undo buffer
5348 (with-current-buffer buffer
5349 (widen)
5350 (goto-char pos)
5351 (setq ts (org-deadline arg)))
5352 (org-agenda-show-new-time marker ts "S"))
5353 (message "Deadline for this item set to %s" ts)))
5354
5355 (defun org-agenda-action ()
5356 "Select entry for agenda action, or execute an agenda action.
5357 This command prompts for another letter. Valid inputs are:
5358
5359 m Mark the entry at point for an agenda action
5360 s Schedule the marked entry to the date at the cursor
5361 d Set the deadline of the marked entry to the date at the cursor
5362 r Call `org-remember' with cursor date as the default date
5363 SPC Show marked entry in other window
5364 TAB Visit marked entry in other window
5365
5366 The cursor may be at a date in the calendar, or in the Org agenda."
5367 (interactive)
5368 (let (pos ans)
5369 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
5370 (setq ans (read-char-exclusive))
5371 (cond
5372 ((equal ans ?m)
5373 ;; Mark this entry
5374 (if (eq major-mode 'org-agenda-mode)
5375 (let ((m (or (get-text-property (point) 'org-hd-marker)
5376 (get-text-property (point) 'org-marker))))
5377 (if m
5378 (progn
5379 (move-marker org-agenda-action-marker
5380 (marker-position m) (marker-buffer m))
5381 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
5382 (error "Don't know which entry to mark")))
5383 (error "This command works only in the agenda")))
5384 ((equal ans ?s)
5385 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
5386 ((equal ans ?d)
5387 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
5388 ((equal ans ?r)
5389 (org-agenda-do-action '(org-remember) t))
5390 ((equal ans ?\ )
5391 (let ((cw (selected-window)))
5392 (org-switch-to-buffer-other-window
5393 (marker-buffer org-agenda-action-marker))
5394 (goto-char org-agenda-action-marker)
5395 (org-show-context 'agenda)
5396 (select-window cw)))
5397 ((equal ans ?\C-i)
5398 (org-switch-to-buffer-other-window
5399 (marker-buffer org-agenda-action-marker))
5400 (goto-char org-agenda-action-marker)
5401 (org-show-context 'agenda))
5402 (t (error "Invalid agenda action %c" ans)))))
5403
5404 (defun org-agenda-do-action (form &optional current-buffer)
5405 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
5406 (let ((org-overriding-default-time (org-get-cursor-date)))
5407 (if current-buffer
5408 (eval form)
5409 (if (not (marker-buffer org-agenda-action-marker))
5410 (error "No entry has bee selected for agenda action")
5411 (with-current-buffer (marker-buffer org-agenda-action-marker)
5412 (save-excursion
5413 (save-restriction
5414 (widen)
5415 (goto-char org-agenda-action-marker)
5416 (eval form))))))))
5417
5418 (defun org-agenda-clock-in (&optional arg)
5419 "Start the clock on the currently selected item."
5420 (interactive "P")
5421 (org-agenda-check-no-diary)
5422 (if (equal arg '(4))
5423 (org-clock-in arg)
5424 (let* ((marker (or (get-text-property (point) 'org-marker)
5425 (org-agenda-error)))
5426 (hdmarker (or (get-text-property (point) 'org-hd-marker)
5427 marker))
5428 (pos (marker-position marker))
5429 newhead)
5430 (org-with-remote-undo (marker-buffer marker)
5431 (with-current-buffer (marker-buffer marker)
5432 (widen)
5433 (goto-char pos)
5434 (org-show-context 'agenda)
5435 (org-show-entry)
5436 (org-cycle-hide-drawers 'children)
5437 (org-clock-in arg)
5438 (setq newhead (org-get-heading)))
5439 (org-agenda-change-all-lines newhead hdmarker t)))))
5440
5441 (defun org-agenda-clock-out (&optional arg)
5442 "Stop the currently running clock."
5443 (interactive "P")
5444 (unless (marker-buffer org-clock-marker)
5445 (error "No running clock"))
5446 (org-with-remote-undo (marker-buffer org-clock-marker)
5447 (org-clock-out)))
5448
5449 (defun org-agenda-clock-cancel (&optional arg)
5450 "Cancel the currently running clock."
5451 (interactive "P")
5452 (unless (marker-buffer org-clock-marker)
5453 (error "No running clock"))
5454 (org-with-remote-undo (marker-buffer org-clock-marker)
5455 (org-clock-cancel)))
5456
5457 (defun org-agenda-diary-entry ()
5458 "Make a diary entry, like the `i' command from the calendar.
5459 All the standard commands work: block, weekly etc."
5460 (interactive)
5461 (org-agenda-check-type t 'agenda 'timeline)
5462 (require 'diary-lib)
5463 (let* ((char (progn
5464 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
5465 (read-char-exclusive)))
5466 (cmd (cdr (assoc char
5467 '((?d . insert-diary-entry)
5468 (?w . insert-weekly-diary-entry)
5469 (?m . insert-monthly-diary-entry)
5470 (?y . insert-yearly-diary-entry)
5471 (?a . insert-anniversary-diary-entry)
5472 (?b . insert-block-diary-entry)
5473 (?c . insert-cyclic-diary-entry)))))
5474 (oldf (symbol-function 'calendar-cursor-to-date))
5475 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
5476 (point (point))
5477 (mark (or (mark t) (point))))
5478 (unless cmd
5479 (error "No command associated with <%c>" char))
5480 (unless (and (get-text-property point 'day)
5481 (or (not (equal ?b char))
5482 (get-text-property mark 'day)))
5483 (error "Don't know which date to use for diary entry"))
5484 ;; We implement this by hacking the `calendar-cursor-to-date' function
5485 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
5486 (let ((calendar-mark-ring
5487 (list (calendar-gregorian-from-absolute
5488 (or (get-text-property mark 'day)
5489 (get-text-property point 'day))))))
5490 (unwind-protect
5491 (progn
5492 (fset 'calendar-cursor-to-date
5493 (lambda (&optional error dummy)
5494 (calendar-gregorian-from-absolute
5495 (get-text-property point 'day))))
5496 (call-interactively cmd))
5497 (fset 'calendar-cursor-to-date oldf)))))
5498
5499 (defun org-agenda-execute-calendar-command (cmd)
5500 "Execute a calendar command from the agenda, with the date associated to
5501 the cursor position."
5502 (org-agenda-check-type t 'agenda 'timeline)
5503 (require 'diary-lib)
5504 (unless (get-text-property (point) 'day)
5505 (error "Don't know which date to use for calendar command"))
5506 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
5507 (point (point))
5508 (date (calendar-gregorian-from-absolute
5509 (get-text-property point 'day)))
5510 ;; the following 2 vars are needed in the calendar
5511 (displayed-month (car date))
5512 (displayed-year (nth 2 date)))
5513 (unwind-protect
5514 (progn
5515 (fset 'calendar-cursor-to-date
5516 (lambda (&optional error dummy)
5517 (calendar-gregorian-from-absolute
5518 (get-text-property point 'day))))
5519 (call-interactively cmd))
5520 (fset 'calendar-cursor-to-date oldf))))
5521
5522 (defun org-agenda-phases-of-moon ()
5523 "Display the phases of the moon for the 3 months around the cursor date."
5524 (interactive)
5525 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
5526
5527 (defun org-agenda-holidays ()
5528 "Display the holidays for the 3 months around the cursor date."
5529 (interactive)
5530 (org-agenda-execute-calendar-command 'list-calendar-holidays))
5531
5532 (defvar calendar-longitude)
5533 (defvar calendar-latitude)
5534 (defvar calendar-location-name)
5535
5536 (defun org-agenda-sunrise-sunset (arg)
5537 "Display sunrise and sunset for the cursor date.
5538 Latitude and longitude can be specified with the variables
5539 `calendar-latitude' and `calendar-longitude'. When called with prefix
5540 argument, latitude and longitude will be prompted for."
5541 (interactive "P")
5542 (require 'solar)
5543 (let ((calendar-longitude (if arg nil calendar-longitude))
5544 (calendar-latitude (if arg nil calendar-latitude))
5545 (calendar-location-name
5546 (if arg "the given coordinates" calendar-location-name)))
5547 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
5548
5549 (defun org-agenda-goto-calendar ()
5550 "Open the Emacs calendar with the date at the cursor."
5551 (interactive)
5552 (org-agenda-check-type t 'agenda 'timeline)
5553 (let* ((day (or (get-text-property (point) 'day)
5554 (error "Don't know which date to open in calendar")))
5555 (date (calendar-gregorian-from-absolute day))
5556 (calendar-move-hook nil)
5557 (calendar-view-holidays-initially-flag nil)
5558 (calendar-view-diary-initially-flag nil)
5559 (view-calendar-holidays-initially nil)
5560 (view-diary-entries-initially nil))
5561 (calendar)
5562 (calendar-goto-date date)))
5563
5564 ;;;###autoload
5565 (defun org-calendar-goto-agenda ()
5566 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
5567 This is a command that has to be installed in `calendar-mode-map'."
5568 (interactive)
5569 (org-agenda-list nil (calendar-absolute-from-gregorian
5570 (calendar-cursor-to-date))
5571 nil))
5572
5573 (defun org-agenda-convert-date ()
5574 (interactive)
5575 (org-agenda-check-type t 'agenda 'timeline)
5576 (let ((day (get-text-property (point) 'day))
5577 date s)
5578 (unless day
5579 (error "Don't know which date to convert"))
5580 (setq date (calendar-gregorian-from-absolute day))
5581 (setq s (concat
5582 "Gregorian: " (calendar-date-string date) "\n"
5583 "ISO: " (calendar-iso-date-string date) "\n"
5584 "Day of Yr: " (calendar-day-of-year-string date) "\n"
5585 "Julian: " (calendar-julian-date-string date) "\n"
5586 "Astron. JD: " (calendar-astro-date-string date)
5587 " (Julian date number at noon UTC)\n"
5588 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
5589 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
5590 "French: " (calendar-french-date-string date) "\n"
5591 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
5592 "Mayan: " (calendar-mayan-date-string date) "\n"
5593 "Coptic: " (calendar-coptic-date-string date) "\n"
5594 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
5595 "Persian: " (calendar-persian-date-string date) "\n"
5596 "Chinese: " (calendar-chinese-date-string date) "\n"))
5597 (with-output-to-temp-buffer "*Dates*"
5598 (princ s))
5599 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
5600
5601 ;;; Appointment reminders
5602
5603 (defvar appt-time-msg-list)
5604
5605 ;;;###autoload
5606 (defun org-agenda-to-appt (&optional refresh filter)
5607 "Activate appointments found in `org-agenda-files'.
5608 With a \\[universal-argument] prefix, refresh the list of
5609 appointements.
5610
5611 If FILTER is t, interactively prompt the user for a regular
5612 expression, and filter out entries that don't match it.
5613
5614 If FILTER is a string, use this string as a regular expression
5615 for filtering entries out.
5616
5617 FILTER can also be an alist with the car of each cell being
5618 either 'headline or 'category. For example:
5619
5620 '((headline \"IMPORTANT\")
5621 (category \"Work\"))
5622
5623 will only add headlines containing IMPORTANT or headlines
5624 belonging to the \"Work\" category."
5625 (interactive "P")
5626 (require 'calendar)
5627 (if refresh (setq appt-time-msg-list nil))
5628 (if (eq filter t)
5629 (setq filter (read-from-minibuffer "Regexp filter: ")))
5630 (let* ((cnt 0) ; count added events
5631 (org-agenda-new-buffers nil)
5632 (org-deadline-warning-days 0)
5633 (today (org-date-to-gregorian
5634 (time-to-days (current-time))))
5635 (files (org-agenda-files 'unrestricted)) entries file)
5636 ;; Get all entries which may contain an appt
5637 (org-prepare-agenda-buffers files)
5638 (while (setq file (pop files))
5639 (setq entries
5640 (append entries
5641 (org-agenda-get-day-entries
5642 file today :timestamp :scheduled :deadline))))
5643 (setq entries (delq nil entries))
5644 ;; Map thru entries and find if we should filter them out
5645 (mapc
5646 (lambda(x)
5647 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
5648 (cat (get-text-property 1 'org-category x))
5649 (tod (get-text-property 1 'time-of-day x))
5650 (ok (or (null filter)
5651 (and (stringp filter) (string-match filter evt))
5652 (and (listp filter)
5653 (or (string-match
5654 (cadr (assoc 'category filter)) cat)
5655 (string-match
5656 (cadr (assoc 'headline filter)) evt))))))
5657 ;; FIXME: Shall we remove text-properties for the appt text?
5658 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
5659 (when (and ok tod)
5660 (setq tod (concat "00" (number-to-string tod))
5661 tod (when (string-match
5662 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
5663 (concat (match-string 1 tod) ":"
5664 (match-string 2 tod))))
5665 (appt-add tod evt)
5666 (setq cnt (1+ cnt))))) entries)
5667 (org-release-buffers org-agenda-new-buffers)
5668 (if (eq cnt 0)
5669 (message "No event to add")
5670 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
5671
5672 (defun org-agenda-todayp (date)
5673 "Does DATE mean today, when considering `org-extend-today-until'?"
5674 (let (today h)
5675 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
5676 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
5677 (setq h (nth 2 (decode-time (current-time))))
5678 (or (and (>= h org-extend-today-until)
5679 (= date today))
5680 (and (< h org-extend-today-until)
5681 (= date (1- today))))))
5682
5683 (provide 'org-agenda)
5684
5685 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
5686
5687 ;;; org-agenda.el ends here
5688