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