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