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