Implement a new agenda view for finding and working on flagged entries
[org-mode.git] / lisp / org-agenda.el
blob095f3b8e209b6c7b4fcb587cfead188d30e1719b
1 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
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.30trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the code for creating and using the Agenda for Org-mode.
31 ;;; Code:
33 (require 'org)
34 (eval-when-compile
35 (require 'cl)
36 (require 'calendar))
38 (declare-function diary-add-to-list "diary-lib"
39 (date string specifier &optional marker globcolor literal))
40 (declare-function calendar-absolute-from-iso "cal-iso" (date))
41 (declare-function calendar-astro-date-string "cal-julian" (&optional date))
42 (declare-function calendar-bahai-date-string "cal-bahai" (&optional date))
43 (declare-function calendar-check-holidays "holidays" (date))
44 (declare-function calendar-chinese-date-string "cal-china" (&optional date))
45 (declare-function calendar-coptic-date-string "cal-coptic" (&optional date))
46 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
47 (declare-function calendar-french-date-string "cal-french" (&optional date))
48 (declare-function calendar-goto-date "cal-move" (date))
49 (declare-function calendar-hebrew-date-string "cal-hebrew" (&optional date))
50 (declare-function calendar-islamic-date-string "cal-islam" (&optional date))
51 (declare-function calendar-iso-date-string "cal-iso" (&optional date))
52 (declare-function calendar-iso-from-absolute "cal-iso" (date))
53 (declare-function calendar-julian-date-string "cal-julian" (&optional date))
54 (declare-function calendar-mayan-date-string "cal-mayan" (&optional date))
55 (declare-function calendar-persian-date-string "cal-persia" (&optional date))
56 (declare-function org-columns-quit "org-colview" ())
57 (defvar calendar-mode-map)
58 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
60 ;; Defined somewhere in this file, but used before definition.
61 (defvar org-agenda-buffer-name)
62 (defvar org-agenda-overriding-header)
63 (defvar org-agenda-title-append nil)
64 (defvar entry)
65 (defvar date)
66 (defvar org-agenda-undo-list)
67 (defvar org-agenda-pending-undo-list)
68 (defvar original-date) ; dynamically scoped, calendar.el does scope this
70 (defcustom org-agenda-confirm-kill 1
71 "When set, remote killing from the agenda buffer needs confirmation.
72 When t, a confirmation is always needed. When a number N, confirmation is
73 only needed when the text to be killed contains more than N non-white lines."
74 :group 'org-agenda
75 :type '(choice
76 (const :tag "Never" nil)
77 (const :tag "Always" t)
78 (integer :tag "When more than N lines")))
80 (defcustom org-agenda-compact-blocks nil
81 "Non-nil means, make the block agenda more compact.
82 This is done by leaving out unnecessary lines."
83 :group 'org-agenda
84 :type 'boolean)
86 (defcustom org-agenda-block-separator ?=
87 "The separator between blocks in the agenda.
88 If this is a string, it will be used as the separator, with a newline added.
89 If it is a character, it will be repeated to fill the window width."
90 :group 'org-agenda
91 :type '(choice
92 (character)
93 (string)))
95 (defgroup org-agenda-export nil
96 "Options concerning exporting agenda views in Org-mode."
97 :tag "Org Agenda Export"
98 :group 'org-agenda)
100 (defcustom org-agenda-with-colors t
101 "Non-nil means, use colors in agenda views."
102 :group 'org-agenda-export
103 :type 'boolean)
105 (defcustom org-agenda-exporter-settings nil
106 "Alist of variable/value pairs that should be active during agenda export.
107 This is a good place to set options for ps-print and for htmlize.
108 Note that the way this is implemented, the values will be evaluated
109 before assigned to the variables. So make sure to quote values you do
110 *not* want evaluated, for example
112 (setq org-agenda-exporter-settings
113 '((ps-print-color-p 'black-white)))"
114 :group 'org-agenda-export
115 :type '(repeat
116 (list
117 (variable)
118 (sexp :tag "Value"))))
120 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
121 "Hook run in temporary buffer before writing it to an export file.
122 A useful function is `org-agenda-add-entry-text'."
123 :group 'org-agenda-export
124 :type 'hook
125 :options '(org-agenda-add-entry-text))
127 (defcustom org-agenda-add-entry-text-maxlines 0
128 "Maximum number of entry text lines to be added to agenda.
129 This is only relevant when `org-agenda-add-entry-text' is part of
130 `org-agenda-before-write-hook', which it is by default.
131 When this is 0, nothing will happen. When it is greater than 0, it
132 specifies the maximum number of lines that will be added for each entry
133 that is listed in the agenda view.
135 Note that this variable is not used during display, only when exporting
136 the agenda. For agenda display, see org-agenda-entry-text-mode and the
137 variable `org-agenda-entry-text-maxlines'."
138 :group 'org-agenda
139 :type 'integer)
141 (defcustom org-agenda-add-entry-text-descriptive-links t
142 "Non-nil means, export org-links as descriptive links in agenda added text.
143 This variable applies to the text added to the agenda when
144 `org-agenda-add-entry-text-maxlines' is larger than 0.
145 When this variable nil, the URL will (also) be shown."
146 :group 'org-agenda
147 :type 'boolean)
149 (defcustom org-agenda-export-html-style ""
150 "The style specification for exported HTML Agenda files.
151 If this variable contains a string, it will replace the default <style>
152 section as produced by `htmlize'.
153 Since there are different ways of setting style information, this variable
154 needs to contain the full HTML structure to provide a style, including the
155 surrounding HTML tags. The style specifications should include definitions
156 the fonts used by the agenda, here is an example:
158 <style type=\"text/css\">
159 p { font-weight: normal; color: gray; }
160 .org-agenda-structure {
161 font-size: 110%;
162 color: #003399;
163 font-weight: 600;
165 .org-todo {
166 color: #cc6666;
167 font-weight: bold;
169 .org-agenda-done {
170 color: #339933;
172 .org-done {
173 color: #339933;
175 .title { text-align: center; }
176 .todo, .deadline { color: red; }
177 .done { color: green; }
178 </style>
180 or, if you want to keep the style in a file,
182 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
184 As the value of this option simply gets inserted into the HTML <head> header,
185 you can \"misuse\" it to also add other text to the header. However,
186 <style>...</style> is required, if not present the variable will be ignored."
187 :group 'org-agenda-export
188 :group 'org-export-html
189 :type 'string)
191 (defgroup org-agenda-custom-commands nil
192 "Options concerning agenda views in Org-mode."
193 :tag "Org Agenda Custom Commands"
194 :group 'org-agenda)
196 (defconst org-sorting-choice
197 '(choice
198 (const time-up) (const time-down)
199 (const category-keep) (const category-up) (const category-down)
200 (const tag-down) (const tag-up)
201 (const priority-up) (const priority-down)
202 (const todo-state-up) (const todo-state-down)
203 (const effort-up) (const effort-down)
204 (const user-defined-up) (const user-defined-down))
205 "Sorting choices.")
207 (defconst org-agenda-custom-commands-local-options
208 `(repeat :tag "Local settings for this command. Remember to quote values"
209 (choice :tag "Setting"
210 (list :tag "Heading for this block"
211 (const org-agenda-overriding-header)
212 (string :tag "Headline"))
213 (list :tag "Files to be searched"
214 (const org-agenda-files)
215 (list
216 (const :format "" quote)
217 (repeat (file))))
218 (list :tag "Sorting strategy"
219 (const org-agenda-sorting-strategy)
220 (list
221 (const :format "" quote)
222 (repeat
223 ,org-sorting-choice)))
224 (list :tag "Prefix format"
225 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
226 (string))
227 (list :tag "Number of days in agenda"
228 (const org-agenda-ndays)
229 (integer :value 1))
230 (list :tag "Fixed starting date"
231 (const org-agenda-start-day)
232 (string :value "2007-11-01"))
233 (list :tag "Start on day of week"
234 (const org-agenda-start-on-weekday)
235 (choice :value 1
236 (const :tag "Today" nil)
237 (integer :tag "Weekday No.")))
238 (list :tag "Include data from diary"
239 (const org-agenda-include-diary)
240 (boolean))
241 (list :tag "Deadline Warning days"
242 (const org-deadline-warning-days)
243 (integer :value 1))
244 (list :tag "Tags filter preset"
245 (const org-agenda-filter-preset)
246 (list
247 (const :format "" quote)
248 (repeat
249 (string :tag "+tag or -tag"))))
250 (list :tag "Standard skipping condition"
251 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
252 (const org-agenda-skip-function)
253 (list
254 (const :format "" quote)
255 (list
256 (choice
257 :tag "Skipping range"
258 (const :tag "Skip entry" org-agenda-skip-entry-if)
259 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
260 (repeat :inline t :tag "Conditions for skipping"
261 (choice
262 :tag "Condition type"
263 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
264 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
265 (const :tag "scheduled" 'scheduled)
266 (const :tag "not scheduled" 'notscheduled)
267 (const :tag "deadline" 'deadline)
268 (const :tag "no deadline" 'notdeadline)
269 (const :tag "timestamp" 'timestamp)
270 (const :tag "no timestamp" 'nottimestamp))))))
271 (list :tag "Non-standard skipping condition"
272 :value (org-agenda-skip-function)
273 (const org-agenda-skip-function)
274 (sexp :tag "Function or form (quoted!)"))
275 (list :tag "Any variable"
276 (variable :tag "Variable")
277 (sexp :tag "Value (sexp)"))))
278 "Selection of examples for agenda command settings.
279 This will be spliced into the custom type of
280 `org-agenda-custom-commands'.")
283 (defcustom org-agenda-custom-commands nil
284 "Custom commands for the agenda.
285 These commands will be offered on the splash screen displayed by the
286 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
288 (key desc type match settings files)
290 key The key (one or more characters as a string) to be associated
291 with the command.
292 desc A description of the command, when omitted or nil, a default
293 description is built using MATCH.
294 type The command type, any of the following symbols:
295 agenda The daily/weekly agenda.
296 todo Entries with a specific TODO keyword, in all agenda files.
297 search Entries containing search words entry or headline.
298 tags Tags/Property/TODO match in all agenda files.
299 tags-todo Tags/P/T match in all agenda files, TODO entries only.
300 todo-tree Sparse tree of specific TODO keyword in *current* file.
301 tags-tree Sparse tree with all tags matches in *current* file.
302 occur-tree Occur sparse tree for *current* file.
303 ... A user-defined function.
304 match What to search for:
305 - a single keyword for TODO keyword searches
306 - a tags match expression for tags searches
307 - a word search expression for text searches.
308 - a regular expression for occur searches
309 For all other commands, this should be the empty string.
310 settings A list of option settings, similar to that in a let form, so like
311 this: ((opt1 val1) (opt2 val2) ...). The values will be
312 evaluated at the moment of execution, so quote them when needed.
313 files A list of files file to write the produced agenda buffer to
314 with the command `org-store-agenda-views'.
315 If a file name ends in \".html\", an HTML version of the buffer
316 is written out. If it ends in \".ps\", a postscript version is
317 produced. Otherwise, only the plain text is written to the file.
319 You can also define a set of commands, to create a composite agenda buffer.
320 In this case, an entry looks like this:
322 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
324 where
326 desc A description string to be displayed in the dispatcher menu.
327 cmd An agenda command, similar to the above. However, tree commands
328 are no allowed, but instead you can get agenda and global todo list.
329 So valid commands for a set are:
330 (agenda \"\" settings)
331 (alltodo \"\" settings)
332 (stuck \"\" settings)
333 (todo \"match\" settings files)
334 (search \"match\" settings files)
335 (tags \"match\" settings files)
336 (tags-todo \"match\" settings files)
338 Each command can carry a list of options, and another set of options can be
339 given for the whole set of commands. Individual command options take
340 precedence over the general options.
342 When using several characters as key to a command, the first characters
343 are prefix commands. For the dispatcher to display useful information, you
344 should provide a description for the prefix, like
346 (setq org-agenda-custom-commands
347 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
348 (\"hl\" tags \"+HOME+Lisa\")
349 (\"hp\" tags \"+HOME+Peter\")
350 (\"hk\" tags \"+HOME+Kim\")))"
351 :group 'org-agenda-custom-commands
352 :type `(repeat
353 (choice :value ("x" "Describe command here" tags "" nil)
354 (list :tag "Single command"
355 (string :tag "Access Key(s) ")
356 (option (string :tag "Description"))
357 (choice
358 (const :tag "Agenda" agenda)
359 (const :tag "TODO list" alltodo)
360 (const :tag "Search words" search)
361 (const :tag "Stuck projects" stuck)
362 (const :tag "Tags/Property match (all agenda files)" tags)
363 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
364 (const :tag "TODO keyword search (all agenda files)" todo)
365 (const :tag "Tags sparse tree (current buffer)" tags-tree)
366 (const :tag "TODO keyword tree (current buffer)" todo-tree)
367 (const :tag "Occur tree (current buffer)" occur-tree)
368 (sexp :tag "Other, user-defined function"))
369 (string :tag "Match (only for some commands)")
370 ,org-agenda-custom-commands-local-options
371 (option (repeat :tag "Export" (file :tag "Export to"))))
372 (list :tag "Command series, all agenda files"
373 (string :tag "Access Key(s)")
374 (string :tag "Description ")
375 (repeat :tag "Component"
376 (choice
377 (list :tag "Agenda"
378 (const :format "" agenda)
379 (const :tag "" :format "" "")
380 ,org-agenda-custom-commands-local-options)
381 (list :tag "TODO list (all keywords)"
382 (const :format "" alltodo)
383 (const :tag "" :format "" "")
384 ,org-agenda-custom-commands-local-options)
385 (list :tag "Search words"
386 (const :format "" search)
387 (string :tag "Match")
388 ,org-agenda-custom-commands-local-options)
389 (list :tag "Stuck projects"
390 (const :format "" stuck)
391 (const :tag "" :format "" "")
392 ,org-agenda-custom-commands-local-options)
393 (list :tag "Tags search"
394 (const :format "" tags)
395 (string :tag "Match")
396 ,org-agenda-custom-commands-local-options)
397 (list :tag "Tags search, TODO entries only"
398 (const :format "" tags-todo)
399 (string :tag "Match")
400 ,org-agenda-custom-commands-local-options)
401 (list :tag "TODO keyword search"
402 (const :format "" todo)
403 (string :tag "Match")
404 ,org-agenda-custom-commands-local-options)
405 (list :tag "Other, user-defined function"
406 (symbol :tag "function")
407 (string :tag "Match")
408 ,org-agenda-custom-commands-local-options)))
410 (repeat :tag "Settings for entire command set"
411 (list (variable :tag "Any variable")
412 (sexp :tag "Value")))
413 (option (repeat :tag "Export" (file :tag "Export to"))))
414 (cons :tag "Prefix key documentation"
415 (string :tag "Access Key(s)")
416 (string :tag "Description ")))))
418 (defcustom org-agenda-query-register ?o
419 "The register holding the current query string.
420 The purpose of this is that if you construct a query string interactively,
421 you can then use it to define a custom command."
422 :group 'org-agenda-custom-commands
423 :type 'character)
425 (defcustom org-stuck-projects
426 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
427 "How to identify stuck projects.
428 This is a list of four items:
429 1. A tags/todo/property matcher string that is used to identify a project.
430 See the manual for a description of tag and property searches.
431 The entire tree below a headline matched by this is considered one project.
432 2. A list of TODO keywords identifying non-stuck projects.
433 If the project subtree contains any headline with one of these todo
434 keywords, the project is considered to be not stuck. If you specify
435 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
436 3. A list of tags identifying non-stuck projects.
437 If the project subtree contains any headline with one of these tags,
438 the project is considered to be not stuck. If you specify \"*\" as
439 a tag, any tag will mark the project unstuck. Note that this is about
440 the explicit presence of a tag somewhere in the subtree, inherited
441 tags to not count here. If inherited tags make a project not stuck,
442 use \"-TAG\" in the tags part of the matcher under (1.) above.
443 4. An arbitrary regular expression matching non-stuck projects.
445 If the project turns out to be not stuck, search continues also in the
446 subtree to see if any of the subtasks have project status.
448 See also the variable `org-tags-match-list-sublevels' which applies
449 to projects matched by this search as well.
451 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
452 or `C-c a #' to produce the list."
453 :group 'org-agenda-custom-commands
454 :type '(list
455 (string :tag "Tags/TODO match to identify a project")
456 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
457 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
458 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
460 (defcustom org-agenda-filter-effort-default-operator "<"
461 "The default operator for effort estimate filtering.
462 If you select an effort estimate limit without first pressing an operator,
463 this one will be used."
464 :group 'org-agenda-custom-commands
465 :type '(choice (const :tag "less or equal" "<")
466 (const :tag "greater or equal"">")
467 (const :tag "equal" "=")))
469 (defgroup org-agenda-skip nil
470 "Options concerning skipping parts of agenda files."
471 :tag "Org Agenda Skip"
472 :group 'org-agenda)
473 (defgroup org-agenda-daily/weekly nil
474 "Options concerning the daily/weekly agenda."
475 :tag "Org Agenda Daily/Weekly"
476 :group 'org-agenda)
477 (defgroup org-agenda-todo-list nil
478 "Options concerning the global todo list agenda view."
479 :tag "Org Agenda Todo List"
480 :group 'org-agenda)
481 (defgroup org-agenda-match-view nil
482 "Options concerning the general tags/property/todo match agenda view."
483 :tag "Org Agenda Match View"
484 :group 'org-agenda)
486 (defvar org-agenda-archives-mode nil
487 "Non-nil means, the agenda will include archived items.
488 If this is the symbol `trees', trees in the selected agenda scope
489 that are marked with the ARCHIVE tag will be included anyway. When this is
490 t, also all archive files associated with the current selection of agenda
491 files will be included.")
493 (defcustom org-agenda-skip-comment-trees t
494 "Non-nil means, skip trees that start with the COMMENT keyword.
495 When nil, these trees are also scanned by agenda commands."
496 :group 'org-agenda-skip
497 :type 'boolean)
499 (defcustom org-agenda-todo-list-sublevels t
500 "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
501 When nil, the sublevels of a TODO entry are not checked, resulting in
502 potentially much shorter TODO lists."
503 :group 'org-agenda-skip
504 :group 'org-agenda-todo-list
505 :type 'boolean)
507 (defcustom org-agenda-todo-ignore-with-date nil
508 "Non-nil means, don't show entries with a date in the global todo list.
509 You can use this if you prefer to mark mere appointments with a TODO keyword,
510 but don't want them to show up in the TODO list.
511 When this is set, it also covers deadlines and scheduled items, the settings
512 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
513 will be ignored.
514 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
515 :group 'org-agenda-skip
516 :group 'org-agenda-todo-list
517 :type 'boolean)
519 (defcustom org-agenda-todo-ignore-scheduled nil
520 "Non-nil means, don't show scheduled entries in the global todo list.
521 The idea behind this is that by scheduling it, you have already taken care
522 of this item.
523 See also `org-agenda-todo-ignore-with-date'.
524 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
525 :group 'org-agenda-skip
526 :group 'org-agenda-todo-list
527 :type 'boolean)
529 (defcustom org-agenda-todo-ignore-deadlines nil
530 "Non-nil means, don't show near deadline entries in the global todo list.
531 Near means closer than `org-deadline-warning-days' days.
532 The idea behind this is that such items will appear in the agenda anyway.
533 See also `org-agenda-todo-ignore-with-date'.
534 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
535 :group 'org-agenda-skip
536 :group 'org-agenda-todo-list
537 :type 'boolean)
539 (defcustom org-agenda-tags-todo-honor-ignore-options nil
540 "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
541 The variables
542 `org-agenda-todo-ignore-with-date',
543 `org-agenda-todo-ignore-scheduled'
544 `org-agenda-todo-ignore-deadlines'
545 make the global TODO list skip entries that have time stamps of certain
546 kinds. If this option is set, the same options will also apply for the
547 tags-todo search, which is the general tags/property matcher
548 restricted to unfinished TODO entries only."
549 :group 'org-agenda-skip
550 :group 'org-agenda-todo-list
551 :group 'org-agenda-match-view
552 :type 'boolean)
554 (defcustom org-agenda-skip-scheduled-if-done nil
555 "Non-nil means don't show scheduled items in agenda when they are done.
556 This is relevant for the daily/weekly agenda, not for the TODO list. And
557 it applies only to the actual date of the scheduling. Warnings about
558 an item with a past scheduling dates are always turned off when the item
559 is DONE."
560 :group 'org-agenda-skip
561 :group 'org-agenda-daily/weekly
562 :type 'boolean)
564 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
565 "Non-nil means skip scheduling line if same entry shows because of deadline.
566 In the agenda of today, an entry can show up multiple times because
567 it is both scheduled and has a nearby deadline, and maybe a plain time
568 stamp as well.
569 When this variable is t, then only the deadline is shown and the fact that
570 the entry is scheduled today or was scheduled previously is not shown.
571 When this variable is nil, the entry will be shown several times. When
572 the variable is the symbol `not-today', then skip scheduled previously,
573 but not scheduled today."
574 :group 'org-agenda-skip
575 :group 'org-agenda-daily/weekly
576 :type '(choice
577 (const :tag "Never" nil)
578 (const :tag "Always" t)
579 (const :tag "Not when scheduled today" not-today)))
581 (defcustom org-agenda-skip-deadline-if-done nil
582 "Non-nil means don't show deadlines when the corresponding item is done.
583 When nil, the deadline is still shown and should give you a happy feeling.
584 This is relevant for the daily/weekly agenda. And it applied only to the
585 actually date of the deadline. Warnings about approaching and past-due
586 deadlines are always turned off when the item is DONE."
587 :group 'org-agenda-skip
588 :group 'org-agenda-daily/weekly
589 :type 'boolean)
591 (defcustom org-agenda-skip-additional-timestamps-same-entry t
592 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
593 When non-nil, after the search for timestamps has matched once in an
594 entry, the rest of the entry will not be searched."
595 :group 'org-agenda-skip
596 :type 'boolean)
598 (defcustom org-agenda-skip-timestamp-if-done nil
599 "Non-nil means don't select item by timestamp or -range if it is DONE."
600 :group 'org-agenda-skip
601 :group 'org-agenda-daily/weekly
602 :type 'boolean)
604 (defcustom org-agenda-dim-blocked-tasks t
605 "Non-nil means, dim blocked tasks in the agenda display.
606 This causes some overhead during agenda construction, but if you
607 have turned on `org-enforce-todo-dependencies',
608 `org-enforce-todo-checkbox-dependencies', or any other blocking
609 mechanism, this will create useful feedback in the agenda.
611 Instead ot t, this variable can also have the value `invisible'.
612 Then blocked tasks will be invisible and only become visible when
613 they become unblocked. An exemption to this behavior is when a task is
614 blocked because of unchecked checkboxes below it. Since checkboxes do
615 not show up in the agenda views, making this task invisible you remove any
616 trace from agenda views that there is something to do. Therefore, a task
617 that is blocked because of checkboxes will never be made invisible, it
618 will only be dimmed."
619 :group 'org-agenda-daily/weekly
620 :group 'org-agenda-todo-list
621 :type '(choice
622 (const :tag "Do not dim" nil)
623 (const :tag "Dim to a grey face" t)
624 (const :tag "Make invisibe" invisible)))
626 (defcustom org-timeline-show-empty-dates 3
627 "Non-nil means, `org-timeline' also shows dates without an entry.
628 When nil, only the days which actually have entries are shown.
629 When t, all days between the first and the last date are shown.
630 When an integer, show also empty dates, but if there is a gap of more than
631 N days, just insert a special line indicating the size of the gap."
632 :group 'org-agenda-skip
633 :type '(choice
634 (const :tag "None" nil)
635 (const :tag "All" t)
636 (integer :tag "at most")))
638 (defgroup org-agenda-startup nil
639 "Options concerning initial settings in the Agenda in Org Mode."
640 :tag "Org Agenda Startup"
641 :group 'org-agenda)
643 (defcustom org-finalize-agenda-hook nil
644 "Hook run just before displaying an agenda buffer."
645 :group 'org-agenda-startup
646 :type 'hook)
648 (defcustom org-agenda-mouse-1-follows-link nil
649 "Non-nil means, mouse-1 on a link will follow the link in the agenda.
650 A longer mouse click will still set point. Does not work on XEmacs.
651 Needs to be set before org.el is loaded."
652 :group 'org-agenda-startup
653 :type 'boolean)
655 (defcustom org-agenda-start-with-follow-mode nil
656 "The initial value of follow-mode in a newly created agenda window."
657 :group 'org-agenda-startup
658 :type 'boolean)
660 (defcustom org-agenda-start-with-entry-text-mode nil
661 "The initial value of entry-text-mode in a newly created agenda window."
662 :group 'org-agenda-startup
663 :type 'boolean)
665 (defcustom org-agenda-entry-text-maxlines 5
666 "Number of text lines to be added when `E' is presed in the agenda.
668 Note that this variable only used during agenda display. Add add entry text
669 when exporting the agenda, configure the variable
670 `org-agenda-add-entry-ext-maxlines'."
671 :group 'org-agenda
672 :type 'integer)
674 (defcustom org-agenda-entry-text-exclude-regexps nil
675 "List of regular expressions to clean up entry text.
676 The complete matches of all regular expressions in this list will be
677 removed from entry text before it is shown in the agenda."
678 :group 'org-agenda
679 :type '(repeat (regexp)))
681 (defvar org-agenda-entry-text-cleanup-hook nil
682 "Hook that is run after basic cleanup of entry text to be shown in agenda.
683 This cleanup is done in a temporary buffer, so the function may inspect and
684 change the entire buffer.
685 Some default stuff like drawers and scheduling/deadline dates will already
686 have been removed when this is called, as will any matches for regular
687 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
689 (defvar org-agenda-include-inactive-timestamps nil
690 "Non-nil means, include inactive time stamps in agenda and timeline.")
692 (defgroup org-agenda-windows nil
693 "Options concerning the windows used by the Agenda in Org Mode."
694 :tag "Org Agenda Windows"
695 :group 'org-agenda)
697 (defcustom org-agenda-window-setup 'reorganize-frame
698 "How the agenda buffer should be displayed.
699 Possible values for this option are:
701 current-window Show agenda in the current window, keeping all other windows.
702 other-frame Use `switch-to-buffer-other-frame' to display agenda.
703 other-window Use `switch-to-buffer-other-window' to display agenda.
704 reorganize-frame Show only two windows on the current frame, the current
705 window and the agenda.
706 See also the variable `org-agenda-restore-windows-after-quit'."
707 :group 'org-agenda-windows
708 :type '(choice
709 (const current-window)
710 (const other-frame)
711 (const other-window)
712 (const reorganize-frame)))
714 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
715 "The min and max height of the agenda window as a fraction of frame height.
716 The value of the variable is a cons cell with two numbers between 0 and 1.
717 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
718 :group 'org-agenda-windows
719 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
721 (defcustom org-agenda-restore-windows-after-quit nil
722 "Non-nil means, restore window configuration open exiting agenda.
723 Before the window configuration is changed for displaying the agenda,
724 the current status is recorded. When the agenda is exited with
725 `q' or `x' and this option is set, the old state is restored. If
726 `org-agenda-window-setup' is `other-frame', the value of this
727 option will be ignored.."
728 :group 'org-agenda-windows
729 :type 'boolean)
731 (defcustom org-agenda-ndays 7
732 "Number of days to include in overview display.
733 Should be 1 or 7.
734 Custom commands can set this variable in the options section."
735 :group 'org-agenda-daily/weekly
736 :type 'integer)
738 (defcustom org-agenda-start-on-weekday 1
739 "Non-nil means, start the overview always on the specified weekday.
740 0 denotes Sunday, 1 denotes Monday etc.
741 When nil, always start on the current day.
742 Custom commands can set this variable in the options section."
743 :group 'org-agenda-daily/weekly
744 :type '(choice (const :tag "Today" nil)
745 (integer :tag "Weekday No.")))
747 (defcustom org-agenda-show-all-dates t
748 "Non-nil means, `org-agenda' shows every day in the selected range.
749 When nil, only the days which actually have entries are shown."
750 :group 'org-agenda-daily/weekly
751 :type 'boolean)
753 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
754 "Format string for displaying dates in the agenda.
755 Used by the daily/weekly agenda and by the timeline. This should be
756 a format string understood by `format-time-string', or a function returning
757 the formatted date as a string. The function must take a single argument,
758 a calendar-style date list like (month day year)."
759 :group 'org-agenda-daily/weekly
760 :type '(choice
761 (string :tag "Format string")
762 (function :tag "Function")))
764 (defun org-agenda-format-date-aligned (date)
765 "Format a date string for display in the daily/weekly agenda, or timeline.
766 This function makes sure that dates are aligned for easy reading."
767 (require 'cal-iso)
768 (let* ((dayname (calendar-day-name date))
769 (day (cadr date))
770 (day-of-week (calendar-day-of-week date))
771 (month (car date))
772 (monthname (calendar-month-name month))
773 (year (nth 2 date))
774 (iso-week (org-days-to-iso-week
775 (calendar-absolute-from-gregorian date)))
776 (weekyear (cond ((and (= month 1) (>= iso-week 52))
777 (1- year))
778 ((and (= month 12) (<= iso-week 1))
779 (1+ year))
780 (t year)))
781 (weekstring (if (= day-of-week 1)
782 (format " W%02d" iso-week)
783 "")))
784 (format "%-10s %2d %s %4d%s"
785 dayname day monthname year weekstring)))
787 (defcustom org-agenda-weekend-days '(6 0)
788 "Which days are weekend?
789 These days get the special face `org-agenda-date-weekend' in the agenda
790 and timeline buffers."
791 :group 'org-agenda-daily/weekly
792 :type '(set :greedy t
793 (const :tag "Monday" 1)
794 (const :tag "Tuesday" 2)
795 (const :tag "Wednesday" 3)
796 (const :tag "Thursday" 4)
797 (const :tag "Friday" 5)
798 (const :tag "Saturday" 6)
799 (const :tag "Sunday" 0)))
801 (defcustom org-agenda-include-diary nil
802 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
803 Custom commands can set this variable in the options section."
804 :group 'org-agenda-daily/weekly
805 :type 'boolean)
807 (defcustom org-agenda-include-all-todo nil
808 "Set means weekly/daily agenda will always contain all TODO entries.
809 The TODO entries will be listed at the top of the agenda, before
810 the entries for specific days.
811 This option is deprecated, it is better to define a block agenda instead."
812 :group 'org-agenda-daily/weekly
813 :type 'boolean)
815 (defcustom org-agenda-repeating-timestamp-show-all t
816 "Non-nil means, show all occurrences of a repeating stamp in the agenda.
817 When nil, only one occurrence is shown, either today or the
818 nearest into the future."
819 :group 'org-agenda-daily/weekly
820 :type 'boolean)
822 (defcustom org-scheduled-past-days 10000
823 "No. of days to continue listing scheduled items that are not marked DONE.
824 When an item is scheduled on a date, it shows up in the agenda on this
825 day and will be listed until it is marked done for the number of days
826 given here."
827 :group 'org-agenda-daily/weekly
828 :type 'integer)
830 (defcustom org-agenda-log-mode-items '(closed clock)
831 "List of items that should be shown in agenda log mode.
832 This list may contain the following symbols:
834 closed Show entries that have been closed on that day.
835 clock Show entries that have received clocked time on that day.
836 state Show all logged state changes.
837 Note that instead of changing this variable, you can also press `C-u l' in
838 the agenda to display all available LOG items temporarily."
839 :group 'org-agenda-daily/weekly
840 :type '(set :greedy t (const closed) (const clock) (const state)))
842 (defcustom org-agenda-log-mode-add-notes t
843 "Non-nil means, add first line of notes to log entries in agenda views.
844 If a log item like a state change or a clock entry is associated with
845 notes, the first line of these notes will be added to the entry in the
846 agenda display."
847 :group 'org-agenda-daily/weekly
848 :type 'boolean)
850 (defcustom org-agenda-start-with-log-mode nil
851 "The initial value of log-mode in a newly created agenda window."
852 :group 'org-agenda-startup
853 :group 'org-agenda-daily/weekly
854 :type 'boolean)
856 (defcustom org-agenda-start-with-clockreport-mode nil
857 "The initial value of clockreport-mode in a newly created agenda window."
858 :group 'org-agenda-startup
859 :group 'org-agenda-daily/weekly
860 :type 'boolean)
862 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
863 "Property list with parameters for the clocktable in clockreport mode.
864 This is the display mode that shows a clock table in the daily/weekly
865 agenda, the properties for this dynamic block can be set here.
866 The usual clocktable parameters are allowed here, but you cannot set
867 the properties :name, :tstart, :tend, :block, and :scope - these will
868 be overwritten to make sure the content accurately reflects the
869 current display in the agenda."
870 :group 'org-agenda-daily/weekly
871 :type 'plist)
874 (defgroup org-agenda-time-grid nil
875 "Options concerning the time grid in the Org-mode Agenda."
876 :tag "Org Agenda Time Grid"
877 :group 'org-agenda)
879 (defcustom org-agenda-search-headline-for-time t
880 "Non-nil means, search headline for a time-of-day.
881 If the headline contains a time-of-day in one format or another, it will
882 be used to sort the entry into the time sequence of items for a day.
883 Some people have time stamps in the headline that refer to the creation
884 time or so, and then this produces an unwanted side effect. If this is
885 the case for your, use this variable to turn off searching the headline
886 for a time."
887 :group 'org-agenda-time-grid
888 :type 'boolean)
890 (defcustom org-agenda-use-time-grid t
891 "Non-nil means, show a time grid in the agenda schedule.
892 A time grid is a set of lines for specific times (like every two hours between
893 8:00 and 20:00). The items scheduled for a day at specific times are
894 sorted in between these lines.
895 For details about when the grid will be shown, and what it will look like, see
896 the variable `org-agenda-time-grid'."
897 :group 'org-agenda-time-grid
898 :type 'boolean)
900 (defcustom org-agenda-time-grid
901 '((daily today require-timed)
902 "----------------"
903 (800 1000 1200 1400 1600 1800 2000))
905 "The settings for time grid for agenda display.
906 This is a list of three items. The first item is again a list. It contains
907 symbols specifying conditions when the grid should be displayed:
909 daily if the agenda shows a single day
910 weekly if the agenda shows an entire week
911 today show grid on current date, independent of daily/weekly display
912 require-timed show grid only if at least one item has a time specification
914 The second item is a string which will be placed behind the grid time.
916 The third item is a list of integers, indicating the times that should have
917 a grid line."
918 :group 'org-agenda-time-grid
919 :type
920 '(list
921 (set :greedy t :tag "Grid Display Options"
922 (const :tag "Show grid in single day agenda display" daily)
923 (const :tag "Show grid in weekly agenda display" weekly)
924 (const :tag "Always show grid for today" today)
925 (const :tag "Show grid only if any timed entries are present"
926 require-timed)
927 (const :tag "Skip grid times already present in an entry"
928 remove-match))
929 (string :tag "Grid String")
930 (repeat :tag "Grid Times" (integer :tag "Time"))))
932 (defgroup org-agenda-sorting nil
933 "Options concerning sorting in the Org-mode Agenda."
934 :tag "Org Agenda Sorting"
935 :group 'org-agenda)
937 (defcustom org-agenda-sorting-strategy
938 '((agenda time-up priority-down category-keep)
939 (todo priority-down category-keep)
940 (tags priority-down category-keep)
941 (search category-keep))
942 "Sorting structure for the agenda items of a single day.
943 This is a list of symbols which will be used in sequence to determine
944 if an entry should be listed before another entry. The following
945 symbols are recognized:
947 time-up Put entries with time-of-day indications first, early first
948 time-down Put entries with time-of-day indications first, late first
949 category-keep Keep the default order of categories, corresponding to the
950 sequence in `org-agenda-files'.
951 category-up Sort alphabetically by category, A-Z.
952 category-down Sort alphabetically by category, Z-A.
953 tag-up Sort alphabetically by last tag, A-Z.
954 tag-down Sort alphabetically by last tag, Z-A.
955 priority-up Sort numerically by priority, high priority last.
956 priority-down Sort numerically by priority, high priority first.
957 todo-state-up Sort by todo state, tasks that are done last.
958 todo-state-down Sort by todo state, tasks that are done first.
959 effort-up Sort numerically by estimated effort, high effort last.
960 effort-down Sort numerically by estimated effort, high effort first.
961 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
962 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
964 The different possibilities will be tried in sequence, and testing stops
965 if one comparison returns a \"not-equal\". For example, the default
966 '(time-up category-keep priority-down)
967 means: Pull out all entries having a specified time of day and sort them,
968 in order to make a time schedule for the current day the first thing in the
969 agenda listing for the day. Of the entries without a time indication, keep
970 the grouped in categories, don't sort the categories, but keep them in
971 the sequence given in `org-agenda-files'. Within each category sort by
972 priority.
974 Leaving out `category-keep' would mean that items will be sorted across
975 categories by priority.
977 Instead of a single list, this can also be a set of list for specific
978 contents, with a context symbol in the car of the list, any of
979 `agenda', `todo', `tags' for the corresponding agenda views.
981 Custom commands can bind this variable in the options section."
982 :group 'org-agenda-sorting
983 :type `(choice
984 (repeat :tag "General" ,org-sorting-choice)
985 (list :tag "Individually"
986 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
987 (repeat ,org-sorting-choice))
988 (cons (const :tag "Strategy for TODO lists" todo)
989 (repeat ,org-sorting-choice))
990 (cons (const :tag "Strategy for Tags matches" tags)
991 (repeat ,org-sorting-choice)))))
993 (defcustom org-agenda-cmp-user-defined nil
994 "A function to define the comparison `user-defined'.
995 This function must receive two arguments, agenda entry a and b.
996 If a>b, return +1. If a<b, return -1. If they are equal as seen by
997 the user comparison, return nil.
998 When this is defined, you can make `user-defined-up' and `user-defined-down'
999 part of an agenda sorting strategy."
1000 :group 'org-agenda-sorting
1001 :type 'symbol)
1003 (defcustom org-sort-agenda-notime-is-late t
1004 "Non-nil means, items without time are considered late.
1005 This is only relevant for sorting. When t, items which have no explicit
1006 time like 15:30 will be considered as 99:01, i.e. later than any items which
1007 do have a time. When nil, the default time is before 0:00. You can use this
1008 option to decide if the schedule for today should come before or after timeless
1009 agenda entries."
1010 :group 'org-agenda-sorting
1011 :type 'boolean)
1013 (defcustom org-sort-agenda-noeffort-is-high t
1014 "Non-nil means, items without effort estimate are sorted as high effort.
1015 This also applies when filtering an agenda view with respect to the
1016 < or > effort operator. Then, tasks with no effort defined will be treated
1017 as tasks with high effort.
1018 When nil, such items are sorted as 0 minutes effort."
1019 :group 'org-agenda-sorting
1020 :type 'boolean)
1022 (defgroup org-agenda-line-format nil
1023 "Options concerning the entry prefix in the Org-mode agenda display."
1024 :tag "Org Agenda Line Format"
1025 :group 'org-agenda)
1027 (defcustom org-agenda-prefix-format
1028 '((agenda . " %-12:c%?-12t% s")
1029 (timeline . " % s")
1030 (todo . " %-12:c")
1031 (tags . " %-12:c")
1032 (search . " %-12:c"))
1033 "Format specifications for the prefix of items in the agenda views.
1034 An alist with four entries, for the different agenda types. The keys to the
1035 sublists are `agenda', `timeline', `todo', and `tags'. The values
1036 are format strings.
1037 This format works similar to a printf format, with the following meaning:
1039 %c the category of the item, \"Diary\" for entries from the diary, or
1040 as given by the CATEGORY keyword or derived from the file name.
1041 %T the *last* tag of the item. Last because inherited tags come
1042 first in the list.
1043 %t the time-of-day specification if one applies to the entry, in the
1044 format HH:MM
1045 %s Scheduling/Deadline information, a short string
1047 All specifiers work basically like the standard `%s' of printf, but may
1048 contain two additional characters: A question mark just after the `%' and
1049 a whitespace/punctuation character just before the final letter.
1051 If the first character after `%' is a question mark, the entire field
1052 will only be included if the corresponding value applies to the
1053 current entry. This is useful for fields which should have fixed
1054 width when present, but zero width when absent. For example,
1055 \"%?-12t\" will result in a 12 character time field if a time of the
1056 day is specified, but will completely disappear in entries which do
1057 not contain a time.
1059 If there is punctuation or whitespace character just before the final
1060 format letter, this character will be appended to the field value if
1061 the value is not empty. For example, the format \"%-12:c\" leads to
1062 \"Diary: \" if the category is \"Diary\". If the category were be
1063 empty, no additional colon would be interted.
1065 The default value of this option is \" %-12:c%?-12t% s\", meaning:
1066 - Indent the line with two space characters
1067 - Give the category in a 12 chars wide field, padded with whitespace on
1068 the right (because of `-'). Append a colon if there is a category
1069 (because of `:').
1070 - If there is a time-of-day, put it into a 12 chars wide field. If no
1071 time, don't put in an empty field, just skip it (because of '?').
1072 - Finally, put the scheduling information and append a whitespace.
1074 As another example, if you don't want the time-of-day of entries in
1075 the prefix, you could use:
1077 (setq org-agenda-prefix-format \" %-11:c% s\")
1079 See also the variables `org-agenda-remove-times-when-in-prefix' and
1080 `org-agenda-remove-tags'.
1082 Custom commands can set this variable in the options section."
1083 :type '(choice
1084 (string :tag "General format")
1085 (list :greedy t :tag "View dependent"
1086 (cons (const agenda) (string :tag "Format"))
1087 (cons (const timeline) (string :tag "Format"))
1088 (cons (const todo) (string :tag "Format"))
1089 (cons (const tags) (string :tag "Format"))
1090 (cons (const search) (string :tag "Format"))))
1091 :group 'org-agenda-line-format)
1093 (defvar org-prefix-format-compiled nil
1094 "The compiled version of the most recently used prefix format.
1095 See the variable `org-agenda-prefix-format'.")
1097 (defcustom org-agenda-todo-keyword-format "%-1s"
1098 "Format for the TODO keyword in agenda lines.
1099 Set this to something like \"%-12s\" if you want all TODO keywords
1100 to occupy a fixed space in the agenda display."
1101 :group 'org-agenda-line-format
1102 :type 'string)
1104 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1105 "Text preceding timerange entries in the agenda view.
1106 This is a list with two strings. The first applies when the range
1107 is entirely on one day. The second applies if the range spans several days.
1108 The strings may have two \"%d\" format specifiers which will be filled
1109 with the sequence number of the days, and the total number of days in the
1110 range, respectively."
1111 :group 'org-agenda-line-format
1112 :type '(list
1113 (string :tag "Deadline today ")
1114 (choice :tag "Deadline relative"
1115 (string :tag "Format string")
1116 (function))))
1118 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1119 "Text preceeding scheduled items in the agenda view.
1120 This is a list with two strings. The first applies when the item is
1121 scheduled on the current day. The second applies when it has been scheduled
1122 previously, it may contain a %d indicating that this is the nth time that
1123 this item is scheduled, due to automatic rescheduling of unfinished items
1124 for the following day. So this number is one larger than the number of days
1125 that passed since this item was scheduled first."
1126 :group 'org-agenda-line-format
1127 :type '(list
1128 (string :tag "Scheduled today ")
1129 (string :tag "Scheduled previously")))
1131 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1132 "Text preceeding deadline items in the agenda view.
1133 This is a list with two strings. The first applies when the item has its
1134 deadline on the current day. The second applies when it is in the past or
1135 in the future, it may contain %d to capture how many days away the deadline
1136 is (was)."
1137 :group 'org-agenda-line-format
1138 :type '(list
1139 (string :tag "Deadline today ")
1140 (choice :tag "Deadline relative"
1141 (string :tag "Format string")
1142 (function))))
1144 (defcustom org-agenda-remove-times-when-in-prefix t
1145 "Non-nil means, remove duplicate time specifications in agenda items.
1146 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1147 time-of-day specification in a headline or diary entry is extracted and
1148 placed into the prefix. If this option is non-nil, the original specification
1149 \(a timestamp or -range, or just a plain time(range) specification like
1150 11:30-4pm) will be removed for agenda display. This makes the agenda less
1151 cluttered.
1152 The option can be t or nil. It may also be the symbol `beg', indicating
1153 that the time should only be removed what it is located at the beginning of
1154 the headline/diary entry."
1155 :group 'org-agenda-line-format
1156 :type '(choice
1157 (const :tag "Always" t)
1158 (const :tag "Never" nil)
1159 (const :tag "When at beginning of entry" beg)))
1162 (defcustom org-agenda-default-appointment-duration nil
1163 "Default duration for appointments that only have a starting time.
1164 When nil, no duration is specified in such cases.
1165 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1166 :group 'org-agenda-line-format
1167 :type '(choice
1168 (integer :tag "Minutes")
1169 (const :tag "No default duration")))
1171 (defcustom org-agenda-show-inherited-tags t
1172 "Non-nil means, show inherited tags in each agenda line."
1173 :group 'org-agenda-line-format
1174 :type 'boolean)
1176 (defcustom org-agenda-remove-tags nil
1177 "Non-nil means, remove the tags from the headline copy in the agenda.
1178 When this is the symbol `prefix', only remove tags when
1179 `org-agenda-prefix-format' contains a `%T' specifier."
1180 :group 'org-agenda-line-format
1181 :type '(choice
1182 (const :tag "Always" t)
1183 (const :tag "Never" nil)
1184 (const :tag "When prefix format contains %T" prefix)))
1186 (if (fboundp 'defvaralias)
1187 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1188 'org-agenda-remove-tags))
1190 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1191 "Shift tags in agenda items to this column.
1192 If this number is positive, it specifies the column. If it is negative,
1193 it means that the tags should be flushright to that column. For example,
1194 -80 works well for a normal 80 character screen."
1195 :group 'org-agenda-line-format
1196 :type 'integer)
1198 (if (fboundp 'defvaralias)
1199 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1201 (defcustom org-agenda-fontify-priorities 'cookies
1202 "Non-nil means, highlight low and high priorities in agenda.
1203 When t, the highest priority entries are bold, lowest priority italic.
1204 However, settings in org-priority-faces will overrule these faces.
1205 When this variable is the symbol `cookies', only fontify the
1206 cookies, not the entire task.
1207 This may also be an association list of priority faces, whose
1208 keys are the character values of `org-highest-priority',
1209 `org-default-priority', and `org-lowest-priority' (the default values
1210 are ?A, ?B, and ?C, respectively). The face may be a named face,
1211 or a list like `(:background \"Red\")'."
1212 :group 'org-agenda-line-format
1213 :type '(choice
1214 (const :tag "Never" nil)
1215 (const :tag "Defaults" t)
1216 (const :tag "Cookies only" cookies)
1217 (repeat :tag "Specify"
1218 (list (character :tag "Priority" :value ?A)
1219 (sexp :tag "face")))))
1221 (defgroup org-agenda-column-view nil
1222 "Options concerning column view in the agenda."
1223 :tag "Org Agenda Column View"
1224 :group 'org-agenda)
1226 (defcustom org-agenda-columns-show-summaries t
1227 "Non-nil means, show summaries for columns displayed in the agenda view."
1228 :group 'org-agenda-column-view
1229 :type 'boolean)
1231 (defcustom org-agenda-columns-remove-prefix-from-item t
1232 "Non-nil means, remove the prefix from a headline for agenda column view.
1233 The special ITEM field in the columns format contains the current line, with
1234 all information shown in other columns (like the TODO state or a tag).
1235 When this variable is non-nil, also the agenda prefix will be removed from
1236 the content of the ITEM field, to make sure as much as possible of the
1237 headline can be shown in the limited width of the field."
1238 :group 'org-agenda
1239 :type 'boolean)
1241 (defcustom org-agenda-columns-compute-summary-properties t
1242 "Non-nil means, recompute all summary properties before column view.
1243 When column view in the agenda is listing properties that have a summary
1244 operator, it can go to all relevant buffers and recompute the summaries
1245 there. This can mean overhead for the agenda column view, but is necessary
1246 to have thing up to date.
1247 As a special case, a CLOCKSUM property also makes sure that the clock
1248 computations are current."
1249 :group 'org-agenda-column-view
1250 :type 'boolean)
1252 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1253 "Non-nil means, the duration of an appointment will add to day effort.
1254 The property to which appointment durations will be added is the one given
1255 in the option `org-effort-property'. If an appointment does not have
1256 an end time, `org-agenda-default-appointment-duration' will be used. If that
1257 is not set, an appointment without end time will not contribute to the time
1258 estimate."
1259 :group 'org-agenda-column-view
1260 :type 'boolean)
1262 (eval-when-compile
1263 (require 'cl))
1264 (require 'org)
1266 (defun org-add-agenda-custom-command (entry)
1267 "Replace or add a command in `org-agenda-custom-commands'.
1268 This is mostly for hacking and trying a new command - once the command
1269 works you probably want to add it to `org-agenda-custom-commands' for good."
1270 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1271 (if ass
1272 (setcdr ass (cdr entry))
1273 (push entry org-agenda-custom-commands))))
1275 ;;; Define the Org-agenda-mode
1277 (defvar org-agenda-mode-map (make-sparse-keymap)
1278 "Keymap for `org-agenda-mode'.")
1280 (defvar org-agenda-menu) ; defined later in this file.
1281 (defvar org-agenda-restrict) ; defined later in this file.
1282 (defvar org-agenda-follow-mode nil)
1283 (defvar org-agenda-entry-text-mode nil)
1284 (defvar org-agenda-clockreport-mode nil)
1285 (defvar org-agenda-show-log nil)
1286 (defvar org-agenda-redo-command nil)
1287 (defvar org-agenda-query-string nil)
1288 (defvar org-agenda-mode-hook nil
1289 "Hook for org-agenda-mode, run after the mode is turned on.")
1290 (defvar org-agenda-type nil)
1291 (defvar org-agenda-force-single-file nil)
1292 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1294 (defun org-agenda-mode ()
1295 "Mode for time-sorted view on action items in Org-mode files.
1297 The following commands are available:
1299 \\{org-agenda-mode-map}"
1300 (interactive)
1301 (kill-all-local-variables)
1302 (setq org-agenda-undo-list nil
1303 org-agenda-pending-undo-list nil
1304 org-agenda-bulk-marked-entries nil)
1305 (setq major-mode 'org-agenda-mode)
1306 ;; Keep global-font-lock-mode from turning on font-lock-mode
1307 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1308 (setq mode-name "Org-Agenda")
1309 (use-local-map org-agenda-mode-map)
1310 (easy-menu-add org-agenda-menu)
1311 (if org-startup-truncated (setq truncate-lines t))
1312 (org-set-local 'line-move-visual nil)
1313 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1314 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1315 ;; Make sure properties are removed when copying text
1316 (when (boundp 'buffer-substring-filters)
1317 (org-set-local 'buffer-substring-filters
1318 (cons (lambda (x)
1319 (set-text-properties 0 (length x) nil x) x)
1320 buffer-substring-filters)))
1321 (unless org-agenda-keep-modes
1322 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1323 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1324 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1325 org-agenda-show-log org-agenda-start-with-log-mode))
1327 (easy-menu-change
1328 '("Agenda") "Agenda Files"
1329 (append
1330 (list
1331 (vector
1332 (if (get 'org-agenda-files 'org-restrict)
1333 "Restricted to single file"
1334 "Edit File List")
1335 '(org-edit-agenda-file-list)
1336 (not (get 'org-agenda-files 'org-restrict)))
1337 "--")
1338 (mapcar 'org-file-menu-entry (org-agenda-files))))
1339 (org-agenda-set-mode-name)
1340 (apply
1341 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1342 (list 'org-agenda-mode-hook)))
1344 (substitute-key-definition 'undo 'org-agenda-undo
1345 org-agenda-mode-map global-map)
1346 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1347 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1348 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1349 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1350 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1351 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1352 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1353 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1354 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1355 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1356 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1357 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1358 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1359 (org-defkey org-agenda-mode-map "A" 'org-agenda-archive-to-archive-sibling)
1360 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1361 (org-defkey org-agenda-mode-map " " 'org-agenda-show)
1362 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1363 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1364 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1365 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1366 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1367 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1368 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1369 (org-defkey org-agenda-mode-map "a" 'org-agenda-toggle-archive-tag)
1370 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1371 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1372 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1373 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1374 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1375 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1376 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1377 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1378 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1379 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1380 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1381 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1382 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1383 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1384 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1386 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1387 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1388 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1389 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1390 (while l (org-defkey org-agenda-mode-map
1391 (int-to-string (pop l)) 'digit-argument)))
1393 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1394 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1395 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1396 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1397 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1398 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1399 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1400 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1401 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1402 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1403 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1404 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1405 'org-clock-modify-effort-estimate)
1406 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1407 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1408 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1409 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1410 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1411 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1412 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1413 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1414 (org-defkey org-agenda-mode-map "n" 'next-line)
1415 (org-defkey org-agenda-mode-map "p" 'previous-line)
1416 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1417 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1418 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1419 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1420 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1421 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1422 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1423 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1424 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1425 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1426 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1427 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1428 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1429 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1430 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1431 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1432 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1433 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1434 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1435 (org-defkey org-agenda-mode-map "J" 'org-clock-goto)
1436 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1437 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1438 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1439 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1440 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1441 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1442 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1443 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1444 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1445 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1447 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1448 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1449 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1450 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1451 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1452 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1453 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1454 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1456 (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
1457 "Local keymap for agenda entries from Org-mode.")
1459 (org-defkey org-agenda-keymap
1460 (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
1461 (org-defkey org-agenda-keymap
1462 (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
1463 (when org-agenda-mouse-1-follows-link
1464 (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
1465 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1466 '("Agenda"
1467 ("Agenda Files")
1468 "--"
1469 ("Agenda Dates"
1470 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1471 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1472 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1473 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1474 "--"
1475 ("View"
1476 ["Day View" org-agenda-day-view
1477 :active (org-agenda-check-type nil 'agenda)
1478 :style radio :selected (equal org-agenda-ndays 1)
1479 :keys "v d (or just d)"]
1480 ["Week View" org-agenda-week-view
1481 :active (org-agenda-check-type nil 'agenda)
1482 :style radio :selected (equal org-agenda-ndays 7)
1483 :keys "v w (or just w)"]
1484 ["Month View" org-agenda-month-view
1485 :active (org-agenda-check-type nil 'agenda)
1486 :style radio :selected (member org-agenda-ndays '(28 29 30 31))
1487 :keys "v m"]
1488 ["Year View" org-agenda-year-view
1489 :active (org-agenda-check-type nil 'agenda)
1490 :style radio :selected (member org-agenda-ndays '(365 366))
1491 :keys "v y"]
1492 "--"
1493 ["Include Diary" org-agenda-toggle-diary
1494 :style toggle :selected org-agenda-include-diary
1495 :active (org-agenda-check-type nil 'agenda)]
1496 ["Use Time Grid" org-agenda-toggle-time-grid
1497 :style toggle :selected org-agenda-use-time-grid
1498 :active (org-agenda-check-type nil 'agenda)]
1499 "--"
1500 ["Show clock report" org-agenda-clockreport-mode
1501 :style toggle :selected org-agenda-clockreport-mode
1502 :active (org-agenda-check-type nil 'agenda)]
1503 ["Show some entry text" org-agenda-entry-text-mode
1504 :style toggle :selected org-agenda-entry-text-mode
1505 :active t]
1506 "--"
1507 ["Show Logbook entries" org-agenda-log-mode
1508 :style toggle :selected org-agenda-show-log
1509 :active (org-agenda-check-type nil 'agenda 'timeline)
1510 :keys "v l (or just l)"]
1511 ["Include archived trees" org-agenda-archives-mode
1512 :style toggle :selected org-agenda-archives-mode :active t
1513 :keys "v a"]
1514 ["Include archive files" (org-agenda-archives-mode t)
1515 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1516 :keys "v A"]
1517 "--"
1518 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1519 ["Write view to file" org-write-agenda t]
1520 ["Rebuild buffer" org-agenda-redo t]
1521 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1522 "--"
1523 ["Show original entry" org-agenda-show t]
1524 ["Go To (other window)" org-agenda-goto t]
1525 ["Go To (this window)" org-agenda-switch-to t]
1526 ["Follow Mode" org-agenda-follow-mode
1527 :style toggle :selected org-agenda-follow-mode :active t]
1528 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
1529 "--"
1530 ("TODO"
1531 ["Cycle TODO" org-agenda-todo t]
1532 ["Next TODO set" org-agenda-todo-nextset t]
1533 ["Previous TODO set" org-agenda-todo-previousset t]
1534 ["Add note" org-agenda-add-note t])
1535 ("Archive/Refile/Delete"
1536 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
1537 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
1538 ["Archive subtree" org-agenda-archive t]
1539 "--"
1540 ["Refile" org-agenda-refile t]
1541 "--"
1542 ["Delete subtree" org-agenda-kill t])
1543 ("Bulk action"
1544 ["Mark entry" org-agenda-bulk-mark t]
1545 ["Unmark entry" org-agenda-bulk-unmark t]
1546 ["Act on all marked" org-agenda-bulk-action t]
1547 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
1548 "--"
1549 ("Tags and Properties"
1550 ["Show all Tags" org-agenda-show-tags t]
1551 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
1552 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
1553 "--"
1554 ["Column View" org-columns t])
1555 ("Deadline/Schedule"
1556 ["Schedule" org-agenda-schedule t]
1557 ["Set Deadline" org-agenda-deadline t]
1558 "--"
1559 ["Mark item" org-agenda-action :active t :keys "k m"]
1560 ["Show mark item" org-agenda-action :active t :keys "k v"]
1561 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
1562 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
1563 "--"
1564 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
1565 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
1566 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
1567 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
1568 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
1569 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
1570 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
1571 ("Clock and Effort"
1572 ["Clock in" org-agenda-clock-in t]
1573 ["Clock out" org-agenda-clock-out t]
1574 ["Clock cancel" org-agenda-clock-cancel t]
1575 ["Goto running clock" org-clock-goto t]
1576 "--"
1577 ["Set Effort" org-agenda-set-effort t]
1578 ["Change clocked effort" org-clock-modify-effort-estimate
1579 (org-clock-is-active)])
1580 ("Priority"
1581 ["Set Priority" org-agenda-priority t]
1582 ["Increase Priority" org-agenda-priority-up t]
1583 ["Decrease Priority" org-agenda-priority-down t]
1584 ["Show Priority" org-agenda-show-priority t])
1585 ("Calendar/Diary"
1586 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
1587 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
1588 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
1589 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
1590 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
1591 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
1592 "--"
1593 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
1594 "--"
1595 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
1596 "--"
1597 ("MobileOrg"
1598 ["Push Files and Views" org-mobile-push t]
1599 ["Pull Captured and Flagged" org-mobile-pull t]
1600 ["Find FLAGGED Tasks" (org-agenda nil "?") t]
1601 ["Show note / unflag" org-agenda-show-the-flagging-note t]
1602 "--"
1603 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
1604 "--"
1605 ["Quit" org-agenda-quit t]
1606 ["Exit and Release Buffers" org-agenda-exit t]
1609 ;;; Agenda undo
1611 (defvar org-agenda-allow-remote-undo t
1612 "Non-nil means, allow remote undo from the agenda buffer.")
1613 (defvar org-agenda-undo-list nil
1614 "List of undoable operations in the agenda since last refresh.")
1615 (defvar org-agenda-undo-has-started-in nil
1616 "Buffers that have already seen `undo-start' in the current undo sequence.")
1617 (defvar org-agenda-pending-undo-list nil
1618 "In a series of undo commands, this is the list of remaining undo items.")
1621 (defun org-agenda-undo ()
1622 "Undo a remote editing step in the agenda.
1623 This undoes changes both in the agenda buffer and in the remote buffer
1624 that have been changed along."
1625 (interactive)
1626 (or org-agenda-allow-remote-undo
1627 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
1628 (if (not (eq this-command last-command))
1629 (setq org-agenda-undo-has-started-in nil
1630 org-agenda-pending-undo-list org-agenda-undo-list))
1631 (if (not org-agenda-pending-undo-list)
1632 (error "No further undo information"))
1633 (let* ((entry (pop org-agenda-pending-undo-list))
1634 buf line cmd rembuf)
1635 (setq cmd (pop entry) line (pop entry))
1636 (setq rembuf (nth 2 entry))
1637 (org-with-remote-undo rembuf
1638 (while (bufferp (setq buf (pop entry)))
1639 (if (pop entry)
1640 (with-current-buffer buf
1641 (let ((last-undo-buffer buf)
1642 (inhibit-read-only t))
1643 (unless (memq buf org-agenda-undo-has-started-in)
1644 (push buf org-agenda-undo-has-started-in)
1645 (make-local-variable 'pending-undo-list)
1646 (undo-start))
1647 (while (and pending-undo-list
1648 (listp pending-undo-list)
1649 (not (car pending-undo-list)))
1650 (pop pending-undo-list))
1651 (undo-more 1))))))
1652 (org-goto-line line)
1653 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
1655 (defun org-verify-change-for-undo (l1 l2)
1656 "Verify that a real change occurred between the undo lists L1 and L2."
1657 (while (and l1 (listp l1) (null (car l1))) (pop l1))
1658 (while (and l2 (listp l2) (null (car l2))) (pop l2))
1659 (not (eq l1 l2)))
1661 ;;; Agenda dispatch
1663 (defvar org-agenda-restrict nil)
1664 (defvar org-agenda-restrict-begin (make-marker))
1665 (defvar org-agenda-restrict-end (make-marker))
1666 (defvar org-agenda-last-dispatch-buffer nil)
1667 (defvar org-agenda-overriding-restriction nil)
1669 ;;;###autoload
1670 (defun org-agenda (&optional arg keys restriction)
1671 "Dispatch agenda commands to collect entries to the agenda buffer.
1672 Prompts for a command to execute. Any prefix arg will be passed
1673 on to the selected command. The default selections are:
1675 a Call `org-agenda-list' to display the agenda for current day or week.
1676 t Call `org-todo-list' to display the global todo list.
1677 T Call `org-todo-list' to display the global todo list, select only
1678 entries with a specific TODO keyword (the user gets a prompt).
1679 m Call `org-tags-view' to display headlines with tags matching
1680 a condition (the user is prompted for the condition).
1681 M Like `m', but select only TODO entries, no ordinary headlines.
1682 L Create a timeline for the current buffer.
1683 e Export views to associated files.
1684 s Search entries for keywords.
1685 / Multi occur accros all agenda files and also files listed
1686 in `org-agenda-text-search-extra-files'.
1687 < Restrict agenda commands to buffer, subtree, or region.
1688 Press several times to get the desired effect.
1689 > Remove a previous restriction.
1690 # List \"stuck\" projects.
1691 ! Configure what \"stuck\" means.
1692 C Configure custom agenda commands.
1694 More commands can be added by configuring the variable
1695 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
1696 searches can be pre-defined in this way.
1698 If the current buffer is in Org-mode and visiting a file, you can also
1699 first press `<' once to indicate that the agenda should be temporarily
1700 \(until the next use of \\[org-agenda]) restricted to the current file.
1701 Pressing `<' twice means to restrict to the current subtree or region
1702 \(if active)."
1703 (interactive "P")
1704 (catch 'exit
1705 (let* ((prefix-descriptions nil)
1706 (org-agenda-window-setup (if (equal (buffer-name)
1707 org-agenda-buffer-name)
1708 'current-window
1709 org-agenda-window-setup))
1710 (org-agenda-custom-commands-orig org-agenda-custom-commands)
1711 (org-agenda-custom-commands
1712 ;; normalize different versions
1713 (delq nil
1714 (mapcar
1715 (lambda (x)
1716 (cond ((stringp (cdr x))
1717 (push x prefix-descriptions)
1718 nil)
1719 ((stringp (nth 1 x)) x)
1720 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1721 (t (cons (car x) (cons "" (cdr x))))))
1722 org-agenda-custom-commands)))
1723 (buf (current-buffer))
1724 (bfn (buffer-file-name (buffer-base-buffer)))
1725 entry key type match lprops ans)
1726 ;; Turn off restriction unless there is an overriding one,
1727 (unless org-agenda-overriding-restriction
1728 (unless (org-bound-and-true-p org-agenda-keep-restriced-file-list)
1729 ;; There is a request to keep the file list in place
1730 (put 'org-agenda-files 'org-restrict nil))
1731 (setq org-agenda-restrict nil)
1732 (move-marker org-agenda-restrict-begin nil)
1733 (move-marker org-agenda-restrict-end nil))
1734 ;; Delete old local properties
1735 (put 'org-agenda-redo-command 'org-lprops nil)
1736 ;; Remember where this call originated
1737 (setq org-agenda-last-dispatch-buffer (current-buffer))
1738 (unless keys
1739 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
1740 keys (car ans)
1741 restriction (cdr ans)))
1742 ;; Estabish the restriction, if any
1743 (when (and (not org-agenda-overriding-restriction) restriction)
1744 (put 'org-agenda-files 'org-restrict (list bfn))
1745 (cond
1746 ((eq restriction 'region)
1747 (setq org-agenda-restrict t)
1748 (move-marker org-agenda-restrict-begin (region-beginning))
1749 (move-marker org-agenda-restrict-end (region-end)))
1750 ((eq restriction 'subtree)
1751 (save-excursion
1752 (setq org-agenda-restrict t)
1753 (org-back-to-heading t)
1754 (move-marker org-agenda-restrict-begin (point))
1755 (move-marker org-agenda-restrict-end
1756 (progn (org-end-of-subtree t)))))))
1758 (require 'calendar) ; FIXME: can we avoid this for some commands?
1759 ;; For example the todo list should not need it (but does...)
1760 (cond
1761 ((setq entry (assoc keys org-agenda-custom-commands))
1762 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
1763 (progn
1764 (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
1765 (put 'org-agenda-redo-command 'org-lprops lprops)
1766 (cond
1767 ((eq type 'agenda)
1768 (org-let lprops '(org-agenda-list current-prefix-arg)))
1769 ((eq type 'alltodo)
1770 (org-let lprops '(org-todo-list current-prefix-arg)))
1771 ((eq type 'search)
1772 (org-let lprops '(org-search-view current-prefix-arg match nil)))
1773 ((eq type 'stuck)
1774 (org-let lprops '(org-agenda-list-stuck-projects
1775 current-prefix-arg)))
1776 ((eq type 'tags)
1777 (org-let lprops '(org-tags-view current-prefix-arg match)))
1778 ((eq type 'tags-todo)
1779 (org-let lprops '(org-tags-view '(4) match)))
1780 ((eq type 'todo)
1781 (org-let lprops '(org-todo-list match)))
1782 ((eq type 'tags-tree)
1783 (org-check-for-org-mode)
1784 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
1785 ((eq type 'todo-tree)
1786 (org-check-for-org-mode)
1787 (org-let lprops
1788 '(org-occur (concat "^" outline-regexp "[ \t]*"
1789 (regexp-quote match) "\\>"))))
1790 ((eq type 'occur-tree)
1791 (org-check-for-org-mode)
1792 (org-let lprops '(org-occur match)))
1793 ((functionp type)
1794 (org-let lprops '(funcall type match)))
1795 ((fboundp type)
1796 (org-let lprops '(funcall type match)))
1797 (t (error "Invalid custom agenda command type %s" type))))
1798 (org-run-agenda-series (nth 1 entry) (cddr entry))))
1799 ((equal keys "C")
1800 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
1801 (customize-variable 'org-agenda-custom-commands))
1802 ((equal keys "a") (call-interactively 'org-agenda-list))
1803 ((equal keys "s") (call-interactively 'org-search-view))
1804 ((equal keys "t") (call-interactively 'org-todo-list))
1805 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
1806 ((equal keys "m") (call-interactively 'org-tags-view))
1807 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
1808 ((equal keys "e") (call-interactively 'org-store-agenda-views))
1809 ((equal keys "?") (org-tags-view nil "+FLAGGED")
1810 (org-add-hook
1811 'post-command-hook
1812 (lambda ()
1813 (unless (current-message)
1814 (let* ((m (org-agenda-get-any-marker))
1815 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
1816 (when note
1817 (message (concat
1818 "FLAGGING-NOTE ([?] for more info): "
1819 (org-add-props
1820 (replace-regexp-in-string
1821 "\\\\n" "//"
1822 (copy-sequence note))
1823 nil 'face 'org-warning)))))))
1824 t t))
1825 ((equal keys "L")
1826 (unless (org-mode-p)
1827 (error "This is not an Org-mode file"))
1828 (unless restriction
1829 (put 'org-agenda-files 'org-restrict (list bfn))
1830 (org-call-with-arg 'org-timeline arg)))
1831 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
1832 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
1833 ((equal keys "!") (customize-variable 'org-stuck-projects))
1834 (t (error "Invalid agenda key"))))))
1836 (defun org-agenda-normalize-custom-commands (cmds)
1837 (delq nil
1838 (mapcar
1839 (lambda (x)
1840 (cond ((stringp (cdr x)) nil)
1841 ((stringp (nth 1 x)) x)
1842 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
1843 (t (cons (car x) (cons "" (cdr x))))))
1844 cmds)))
1846 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
1847 "The user interface for selecting an agenda command."
1848 (catch 'exit
1849 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
1850 (restrict-ok (and bfn (org-mode-p)))
1851 (region-p (org-region-active-p))
1852 (custom org-agenda-custom-commands)
1853 (selstring "")
1854 restriction second-time
1855 c entry key type match prefixes rmheader header-end custom1 desc)
1856 (save-window-excursion
1857 (delete-other-windows)
1858 (org-switch-to-buffer-other-window " *Agenda Commands*")
1859 (erase-buffer)
1860 (insert (eval-when-compile
1861 (let ((header
1863 Press key for an agenda command: < Buffer, subtree/region restriction
1864 -------------------------------- > Remove restriction
1865 a Agenda for current week or day e Export agenda views
1866 t List of all TODO entries T Entries with special TODO kwd
1867 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
1868 L Timeline for current buffer # List stuck projects (!=configure)
1869 s Search for keywords C Configure custom agenda commands
1870 / Multi-occur ? Find :FLAGGED: entries
1872 (start 0))
1873 (while (string-match
1874 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
1875 header start)
1876 (setq start (match-end 0))
1877 (add-text-properties (match-beginning 2) (match-end 2)
1878 '(face bold) header))
1879 header)))
1880 (setq header-end (move-marker (make-marker) (point)))
1881 (while t
1882 (setq custom1 custom)
1883 (when (eq rmheader t)
1884 (org-goto-line 1)
1885 (re-search-forward ":" nil t)
1886 (delete-region (match-end 0) (point-at-eol))
1887 (forward-char 1)
1888 (looking-at "-+")
1889 (delete-region (match-end 0) (point-at-eol))
1890 (move-marker header-end (match-end 0)))
1891 (goto-char header-end)
1892 (delete-region (point) (point-max))
1893 (while (setq entry (pop custom1))
1894 (setq key (car entry) desc (nth 1 entry)
1895 type (nth 2 entry)
1896 match (nth 3 entry))
1897 (if (> (length key) 1)
1898 (add-to-list 'prefixes (string-to-char key))
1899 (insert
1900 (format
1901 "\n%-4s%-14s: %s"
1902 (org-add-props (copy-sequence key)
1903 '(face bold))
1904 (cond
1905 ((string-match "\\S-" desc) desc)
1906 ((eq type 'agenda) "Agenda for current week or day")
1907 ((eq type 'alltodo) "List of all TODO entries")
1908 ((eq type 'search) "Word search")
1909 ((eq type 'stuck) "List of stuck projects")
1910 ((eq type 'todo) "TODO keyword")
1911 ((eq type 'tags) "Tags query")
1912 ((eq type 'tags-todo) "Tags (TODO)")
1913 ((eq type 'tags-tree) "Tags tree")
1914 ((eq type 'todo-tree) "TODO kwd tree")
1915 ((eq type 'occur-tree) "Occur tree")
1916 ((functionp type) (if (symbolp type)
1917 (symbol-name type)
1918 "Lambda expression"))
1919 (t "???"))
1920 (cond
1921 ((stringp match)
1922 (setq match (copy-sequence match))
1923 (org-add-props match nil 'face 'org-warning))
1924 (match
1925 (format "set of %d commands" (length match)))
1926 (t ""))))))
1927 (when prefixes
1928 (mapc (lambda (x)
1929 (insert
1930 (format "\n%s %s"
1931 (org-add-props (char-to-string x)
1932 nil 'face 'bold)
1933 (or (cdr (assoc (concat selstring (char-to-string x))
1934 prefix-descriptions))
1935 "Prefix key"))))
1936 prefixes))
1937 (goto-char (point-min))
1938 (if second-time
1939 (if (not (pos-visible-in-window-p (point-max)))
1940 (org-fit-window-to-buffer))
1941 (setq second-time t)
1942 (org-fit-window-to-buffer))
1943 (message "Press key for agenda command%s:"
1944 (if (or restrict-ok org-agenda-overriding-restriction)
1945 (if org-agenda-overriding-restriction
1946 " (restriction lock active)"
1947 (if restriction
1948 (format " (restricted to %s)" restriction)
1949 " (unrestricted)"))
1950 ""))
1951 (setq c (read-char-exclusive))
1952 (message "")
1953 (cond
1954 ((assoc (char-to-string c) custom)
1955 (setq selstring (concat selstring (char-to-string c)))
1956 (throw 'exit (cons selstring restriction)))
1957 ((memq c prefixes)
1958 (setq selstring (concat selstring (char-to-string c))
1959 prefixes nil
1960 rmheader (or rmheader t)
1961 custom (delq nil (mapcar
1962 (lambda (x)
1963 (if (or (= (length (car x)) 1)
1964 (/= (string-to-char (car x)) c))
1966 (cons (substring (car x) 1) (cdr x))))
1967 custom))))
1968 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
1969 (message "Restriction is only possible in Org-mode buffers")
1970 (ding) (sit-for 1))
1971 ((eq c ?1)
1972 (org-agenda-remove-restriction-lock 'noupdate)
1973 (setq restriction 'buffer))
1974 ((eq c ?0)
1975 (org-agenda-remove-restriction-lock 'noupdate)
1976 (setq restriction (if region-p 'region 'subtree)))
1977 ((eq c ?<)
1978 (org-agenda-remove-restriction-lock 'noupdate)
1979 (setq restriction
1980 (cond
1981 ((eq restriction 'buffer)
1982 (if region-p 'region 'subtree))
1983 ((memq restriction '(subtree region))
1984 nil)
1985 (t 'buffer))))
1986 ((eq c ?>)
1987 (org-agenda-remove-restriction-lock 'noupdate)
1988 (setq restriction nil))
1989 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
1990 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
1991 ((and (> (length selstring) 0) (eq c ?\d))
1992 (delete-window)
1993 (org-agenda-get-restriction-and-command prefix-descriptions))
1995 ((equal c ?q) (error "Abort"))
1996 (t (error "Invalid key %c" c))))))))
1998 (defun org-run-agenda-series (name series)
1999 (org-let (nth 1 series) '(org-prepare-agenda name))
2000 (let* ((org-agenda-multi t)
2001 (redo (list 'org-run-agenda-series name (list 'quote series)))
2002 (cmds (car series))
2003 (gprops (nth 1 series))
2004 match ;; The byte compiler incorrectly complains about this. Keep it!
2005 cmd type lprops)
2006 (while (setq cmd (pop cmds))
2007 (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
2008 (cond
2009 ((eq type 'agenda)
2010 (org-let2 gprops lprops
2011 '(call-interactively 'org-agenda-list)))
2012 ((eq type 'alltodo)
2013 (org-let2 gprops lprops
2014 '(call-interactively 'org-todo-list)))
2015 ((eq type 'search)
2016 (org-let2 gprops lprops
2017 '(org-search-view current-prefix-arg match nil)))
2018 ((eq type 'stuck)
2019 (org-let2 gprops lprops
2020 '(call-interactively 'org-agenda-list-stuck-projects)))
2021 ((eq type 'tags)
2022 (org-let2 gprops lprops
2023 '(org-tags-view current-prefix-arg match)))
2024 ((eq type 'tags-todo)
2025 (org-let2 gprops lprops
2026 '(org-tags-view '(4) match)))
2027 ((eq type 'todo)
2028 (org-let2 gprops lprops
2029 '(org-todo-list match)))
2030 ((fboundp type)
2031 (org-let2 gprops lprops
2032 '(funcall type match)))
2033 (t (error "Invalid type in command series"))))
2034 (widen)
2035 (setq org-agenda-redo-command redo)
2036 (goto-char (point-min)))
2037 (org-fit-agenda-window)
2038 (org-let (nth 1 series) '(org-finalize-agenda)))
2040 ;;;###autoload
2041 (defmacro org-batch-agenda (cmd-key &rest parameters)
2042 "Run an agenda command in batch mode and send the result to STDOUT.
2043 If CMD-KEY is a string of length 1, it is used as a key in
2044 `org-agenda-custom-commands' and triggers this command. If it is a
2045 longer string it is used as a tags/todo match string.
2046 Paramters are alternating variable names and values that will be bound
2047 before running the agenda command."
2048 (let (pars)
2049 (while parameters
2050 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2051 (if (> (length cmd-key) 2)
2052 (eval (list 'let (nreverse pars)
2053 (list 'org-tags-view nil cmd-key)))
2054 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2055 (set-buffer org-agenda-buffer-name)
2056 (princ (org-encode-for-stdout (buffer-string)))))
2058 (defun org-encode-for-stdout (string)
2059 (if (fboundp 'encode-coding-string)
2060 (encode-coding-string string buffer-file-coding-system)
2061 string))
2063 (defvar org-agenda-info nil)
2065 ;;;###autoload
2066 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2067 "Run an agenda command in batch mode and send the result to STDOUT.
2068 If CMD-KEY is a string of length 1, it is used as a key in
2069 `org-agenda-custom-commands' and triggers this command. If it is a
2070 longer string it is used as a tags/todo match string.
2071 Paramters are alternating variable names and values that will be bound
2072 before running the agenda command.
2074 The output gives a line for each selected agenda item. Each
2075 item is a list of comma-separated values, like this:
2077 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2079 category The category of the item
2080 head The headline, without TODO kwd, TAGS and PRIORITY
2081 type The type of the agenda entry, can be
2082 todo selected in TODO match
2083 tagsmatch selected in tags match
2084 diary imported from diary
2085 deadline a deadline on given date
2086 scheduled scheduled on given date
2087 timestamp entry has timestamp on given date
2088 closed entry was closed on given date
2089 upcoming-deadline warning about deadline
2090 past-scheduled forwarded scheduled item
2091 block entry has date block including g. date
2092 todo The todo keyword, if any
2093 tags All tags including inherited ones, separated by colons
2094 date The relevant date, like 2007-2-14
2095 time The time, like 15:00-16:50
2096 extra Sting with extra planning info
2097 priority-l The priority letter if any was given
2098 priority-n The computed numerical priority
2099 agenda-day The day in the agenda where this is listed"
2101 (let (pars)
2102 (while parameters
2103 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2104 (push (list 'org-agenda-remove-tags t) pars)
2105 (if (> (length cmd-key) 2)
2106 (eval (list 'let (nreverse pars)
2107 (list 'org-tags-view nil cmd-key)))
2108 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2109 (set-buffer org-agenda-buffer-name)
2110 (let* ((lines (org-split-string (buffer-string) "\n"))
2111 line)
2112 (while (setq line (pop lines))
2113 (catch 'next
2114 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2115 (setq org-agenda-info
2116 (org-fix-agenda-info (text-properties-at 0 line)))
2117 (princ
2118 (org-encode-for-stdout
2119 (mapconcat 'org-agenda-export-csv-mapper
2120 '(org-category txt type todo tags date time-of-day extra
2121 priority-letter priority agenda-day)
2122 ",")))
2123 (princ "\n"))))))
2125 (defun org-fix-agenda-info (props)
2126 "Make sure all properties on an agenda item have a canonical form,
2127 so the export commands can easily use it."
2128 (let (tmp re)
2129 (when (setq tmp (plist-get props 'tags))
2130 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2131 (when (setq tmp (plist-get props 'date))
2132 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2133 (let ((calendar-date-display-form '(year "-" month "-" day)))
2134 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2136 (setq tmp (calendar-date-string tmp)))
2137 (setq props (plist-put props 'date tmp)))
2138 (when (setq tmp (plist-get props 'day))
2139 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2140 (let ((calendar-date-display-form '(year "-" month "-" day)))
2141 (setq tmp (calendar-date-string tmp)))
2142 (setq props (plist-put props 'day tmp))
2143 (setq props (plist-put props 'agenda-day tmp)))
2144 (when (setq tmp (plist-get props 'txt))
2145 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2146 (plist-put props 'priority-letter (match-string 1 tmp))
2147 (setq tmp (replace-match "" t t tmp)))
2148 (when (and (setq re (plist-get props 'org-todo-regexp))
2149 (setq re (concat "\\`\\.*" re " ?"))
2150 (string-match re tmp))
2151 (plist-put props 'todo (match-string 1 tmp))
2152 (setq tmp (replace-match "" t t tmp)))
2153 (plist-put props 'txt tmp)))
2154 props)
2156 (defun org-agenda-export-csv-mapper (prop)
2157 (let ((res (plist-get org-agenda-info prop)))
2158 (setq res
2159 (cond
2160 ((not res) "")
2161 ((stringp res) res)
2162 (t (prin1-to-string res))))
2163 (while (string-match "," res)
2164 (setq res (replace-match ";" t t res)))
2165 (org-trim res)))
2168 ;;;###autoload
2169 (defun org-store-agenda-views (&rest parameters)
2170 (interactive)
2171 (eval (list 'org-batch-store-agenda-views)))
2173 ;; FIXME, why is this a macro?????
2174 ;;;###autoload
2175 (defmacro org-batch-store-agenda-views (&rest parameters)
2176 "Run all custom agenda commands that have a file argument."
2177 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2178 (pop-up-frames nil)
2179 (dir default-directory)
2180 pars cmd thiscmdkey files opts cmd-or-set)
2181 (while parameters
2182 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2183 (setq pars (reverse pars))
2184 (save-window-excursion
2185 (while cmds
2186 (setq cmd (pop cmds)
2187 thiscmdkey (car cmd)
2188 cmd-or-set (nth 2 cmd)
2189 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2190 files (nth (if (listp cmd-or-set) 4 5) cmd))
2191 (if (stringp files) (setq files (list files)))
2192 (when files
2193 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2194 (list 'org-agenda nil thiscmdkey)))
2195 (set-buffer org-agenda-buffer-name)
2196 (while files
2197 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2198 (list 'org-write-agenda
2199 (expand-file-name (pop files) dir) nil t))))
2200 (and (get-buffer org-agenda-buffer-name)
2201 (kill-buffer org-agenda-buffer-name)))))))
2203 (defun org-agenda-mark-header-line (pos)
2204 "Mark the line at POS as an agenda structure header."
2205 (save-excursion
2206 (goto-char pos)
2207 (put-text-property (point-at-bol) (point-at-eol)
2208 'org-agenda-structural-header t)
2209 (when org-agenda-title-append
2210 (put-text-property (point-at-bol) (point-at-eol)
2211 'org-agenda-title-append org-agenda-title-append))))
2214 (defun org-write-agenda (file &optional open nosettings)
2215 "Write the current buffer (an agenda view) as a file.
2216 Depending on the extension of the file name, plain text (.txt),
2217 HTML (.html or .htm) or Postscript (.ps) is produced.
2218 If the extension is .ics, run icalendar export over all files used
2219 to construct the agenda and limit the export to entries listed in the
2220 agenda now.
2221 With prefic argument OPEN, open the new file immediately.
2222 If NOSETTINGS is given, do not scope the settings of
2223 `org-agenda-exporter-settings' into the export commands. This is used when
2224 the settings have already been scoped and we do not wish to overrule other,
2225 higher priority settings."
2226 (interactive "FWrite agenda to file: \nP")
2227 (if (not (file-writable-p file))
2228 (error "Cannot write agenda to file %s" file))
2229 (cond
2230 ((string-match "\\.html?\\'" file) (require 'htmlize))
2231 ((string-match "\\.ps\\'" file) (require 'ps-print)))
2232 (org-let (if nosettings nil org-agenda-exporter-settings)
2233 '(save-excursion
2234 (save-window-excursion
2235 (org-agenda-mark-filtered-text)
2236 (let ((bs (copy-sequence (buffer-string))) beg app)
2237 (org-agenda-unmark-filtered-text)
2238 (with-temp-buffer
2239 (insert bs)
2240 (org-agenda-remove-marked-text 'org-filtered)
2241 (while (setq beg (text-property-any (point-min) (point-max)
2242 'org-filtered t))
2243 (delete-region
2244 beg (or (next-single-property-change beg 'org-filtered)
2245 (point-max))))
2246 (run-hooks 'org-agenda-before-write-hook)
2247 (cond
2248 ((string-match "\\.html?\\'" file)
2249 (set-buffer (htmlize-buffer (current-buffer)))
2251 (when (and org-agenda-export-html-style
2252 (string-match "<style>" org-agenda-export-html-style))
2253 ;; replace <style> section with org-agenda-export-html-style
2254 (goto-char (point-min))
2255 (kill-region (- (search-forward "<style") 6)
2256 (search-forward "</style>"))
2257 (insert org-agenda-export-html-style))
2258 (write-file file)
2259 (kill-buffer (current-buffer))
2260 (message "HTML written to %s" file))
2261 ((string-match "\\.ps\\'" file)
2262 (require 'ps-print)
2263 (flet ((ps-get-buffer-name () "Agenda View"))
2264 (ps-print-buffer-with-faces file))
2265 (message "Postscript written to %s" file))
2266 ((string-match "\\.pdf\\'" file)
2267 (require 'ps-print)
2268 (flet ((ps-get-buffer-name () "Agenda View"))
2269 (ps-print-buffer-with-faces
2270 (concat (file-name-sans-extension file) ".ps")))
2271 (call-process "ps2pdf" nil nil nil
2272 (expand-file-name
2273 (concat (file-name-sans-extension file) ".ps"))
2274 (expand-file-name file))
2275 (message "PDF written to %s" file))
2276 ((string-match "\\.org\\'" file)
2277 (let ((all (buffer-string)) in-date id pl prefix line)
2278 (with-temp-file file
2279 (org-mode)
2280 (insert all)
2281 (goto-char (point-min))
2282 (while (not (eobp))
2283 (cond
2284 ((looking-at "[ \t]*$")) ; keep empty lines
2285 ((looking-at "=+$")
2286 ;; remove underlining
2287 (delete-region (point) (point-at-eol)))
2288 ((get-text-property (point) 'org-agenda-structural-header)
2289 (setq in-date nil)
2290 (setq app (get-text-property (point)
2291 'org-agenda-title-append))
2292 (setq short (get-text-property (point)
2293 'short-heading))
2294 (when (and short (looking-at ".+"))
2295 (replace-match short)
2296 (beginning-of-line 1))
2297 (when app
2298 (end-of-line 1)
2299 (insert app)
2300 (beginning-of-line 1))
2301 (insert "* "))
2302 ((get-text-property (point) 'org-agenda-date-header)
2303 (setq in-date t)
2304 (insert "** "))
2305 ((setq m (or (get-text-property (point) 'org-hd-marker)
2306 (get-text-property (point) 'org-marker)))
2307 (if (setq pl (get-text-property (point) 'prefix-length))
2308 (progn
2309 (setq prefix (org-trim (buffer-substring
2310 (point) (+ (point) pl)))
2311 line (org-trim (buffer-substring
2312 (+ (point) pl)
2313 (point-at-eol))))
2314 (delete-region (point-at-bol) (point-at-eol))
2315 (insert line "<break>" prefix)
2316 (beginning-of-line 1))
2317 (and (looking-at "[ \t]+") (replace-match "")))
2318 (insert (if in-date "*** " "** "))
2319 (end-of-line 1)
2320 (insert "\n")
2321 (insert (org-agenda-get-some-entry-text
2322 m 10 " " 'planning)
2323 "\n")
2324 (when (setq id
2325 (if (org-bound-and-true-p
2326 org-mobile-force-id-on-agenda-items)
2327 (org-id-get m 'create)
2328 (org-entry-get m "ID")))
2329 (insert " :PROPERTIES:\n :ORIGINAL_ID: " id
2330 "\n :END:\n"))))
2331 (beginning-of-line 2)))
2332 (message "Agenda written to Org file %s" file)))
2333 ((string-match "\\.ics\\'" file)
2334 (require 'org-icalendar)
2335 (let ((org-agenda-marker-table
2336 (org-create-marker-find-array
2337 (org-agenda-collect-markers)))
2338 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2339 (org-combined-agenda-icalendar-file file))
2340 (apply 'org-export-icalendar 'combine
2341 (org-agenda-files nil 'ifmode))))
2343 (let ((bs (buffer-string)))
2344 (find-file file)
2345 (erase-buffer)
2346 (insert bs)
2347 (save-buffer 0)
2348 (kill-buffer (current-buffer))
2349 (message "Plain text written to %s" file))))))))
2350 (set-buffer org-agenda-buffer-name))
2351 (when open (org-open-file file)))
2353 (defvar org-agenda-filter-overlays nil)
2355 (defun org-agenda-mark-filtered-text ()
2356 "Mark all text hidden by filtering with a text property."
2357 (let ((inhibit-read-only t))
2358 (mapc
2359 (lambda (o)
2360 (when (equal (org-overlay-buffer o) (current-buffer))
2361 (put-text-property
2362 (org-overlay-start o) (org-overlay-end o)
2363 'org-filtered t)))
2364 org-agenda-filter-overlays)))
2366 (defun org-agenda-unmark-filtered-text ()
2367 "Remove the filtering text property."
2368 (let ((inhibit-read-only t))
2369 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2371 (defun org-agenda-remove-marked-text (property &optional value)
2372 "Delete all text marked with VALUE of PROPERTY.
2373 VALUE defaults to t."
2374 (let (beg)
2375 (setq value (or value t))
2376 (while (setq beg (text-property-any (point-min) (point-max)
2377 property value))
2378 (delete-region
2379 beg (or (next-single-property-change beg 'org-filtered)
2380 (point-max))))))
2382 (defun org-agenda-add-entry-text ()
2383 "Add entry text to agenda lines.
2384 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2385 entry text following headings shown in the agenda.
2386 Drawers will be excluded, also the line with scheduling/deadline info."
2387 (when (> org-agenda-add-entry-text-maxlines 0)
2388 (let (m txt)
2389 (goto-char (point-min))
2390 (while (not (eobp))
2391 (if (not (setq m (get-text-property (point) 'org-hd-marker)))
2392 (beginning-of-line 2)
2393 (setq txt (org-agenda-get-some-entry-text
2394 m org-agenda-add-entry-text-maxlines " > "))
2395 (end-of-line 1)
2396 (if (string-match "\\S-" txt) (insert "\n" txt)))))))
2398 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2399 &rest keep)
2400 "Extract entry text from MARKER, at most N-LINES lines.
2401 This will ignore drawers etc, just get the text.
2402 If INDENT is given, prefix every line with this string. If KEEP is
2403 given, it is a list of symbols, defining stuff that hould not be
2404 removed from the entry content. Currently only `planning' is allowed here."
2405 (let (txt drawer-re kwd-time-re ind)
2406 (save-excursion
2407 (with-current-buffer (marker-buffer marker)
2408 (if (not (org-mode-p))
2409 (setq txt "")
2410 (save-excursion
2411 (save-restriction
2412 (widen)
2413 (goto-char marker)
2414 (end-of-line 1)
2415 (setq txt (buffer-substring
2416 (min (1+ (point)) (point-max))
2417 (progn (outline-next-heading) (point)))
2418 drawer-re org-drawer-regexp
2419 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2420 ".*\n?"))
2421 (with-temp-buffer
2422 (insert txt)
2423 (when org-agenda-add-entry-text-descriptive-links
2424 (goto-char (point-min))
2425 (while (org-activate-bracket-links (point-max))
2426 (add-text-properties (match-beginning 0) (match-end 0)
2427 '(face org-link))))
2428 (goto-char (point-min))
2429 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2430 (set-text-properties (match-beginning 0) (match-end 0)
2431 nil))
2432 (goto-char (point-min))
2433 (while (re-search-forward drawer-re nil t)
2434 (delete-region
2435 (match-beginning 0)
2436 (progn (re-search-forward
2437 "^[ \t]*:END:.*\n?" nil 'move)
2438 (point))))
2439 (unless (member 'planning keep)
2440 (goto-char (point-min))
2441 (while (re-search-forward kwd-time-re nil t)
2442 (replace-match "")))
2443 (goto-char (point-min))
2444 (when org-agenda-entry-text-exclude-regexps
2445 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2446 (while (setq re (pop re-list))
2447 (goto-char (point-min))
2448 (while (re-search-forward re nil t)
2449 (replace-match "")))))
2450 (goto-char (point-max))
2451 (skip-chars-backward " \t\n")
2452 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2454 ;; find and remove min common indentation
2455 (goto-char (point-min))
2456 (untabify (point-min) (point-max))
2457 (setq ind (org-get-indentation))
2458 (while (not (eobp))
2459 (unless (looking-at "[ \t]*$")
2460 (setq ind (min ind (org-get-indentation))))
2461 (beginning-of-line 2))
2462 (goto-char (point-min))
2463 (while (not (eobp))
2464 (unless (looking-at "[ \t]*$")
2465 (move-to-column ind)
2466 (delete-region (point-at-bol) (point)))
2467 (beginning-of-line 2))
2469 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2471 (goto-char (point-min))
2472 (when indent
2473 (while (and (not (eobp)) (re-search-forward "^" nil t))
2474 (replace-match indent t t)))
2475 (goto-char (point-min))
2476 (while (looking-at "[ \t]*\n") (replace-match ""))
2477 (goto-char (point-max))
2478 (when (> (org-current-line)
2479 n-lines)
2480 (org-goto-line (1+ n-lines))
2481 (backward-char 1))
2482 (setq txt (buffer-substring (point-min) (point)))))))))
2483 txt))
2485 (defun org-agenda-collect-markers ()
2486 "Collect the markers pointing to entries in the agenda buffer."
2487 (let (m markers)
2488 (save-excursion
2489 (goto-char (point-min))
2490 (while (not (eobp))
2491 (when (setq m (or (get-text-property (point) 'org-hd-marker)
2492 (get-text-property (point) 'org-marker)))
2493 (push m markers))
2494 (beginning-of-line 2)))
2495 (nreverse markers)))
2497 (defun org-create-marker-find-array (marker-list)
2498 "Create a alist of files names with all marker positions in that file."
2499 (let (f tbl m a p)
2500 (while (setq m (pop marker-list))
2501 (setq p (marker-position m)
2502 f (buffer-file-name (or (buffer-base-buffer
2503 (marker-buffer m))
2504 (marker-buffer m))))
2505 (if (setq a (assoc f tbl))
2506 (push (marker-position m) (cdr a))
2507 (push (list f p) tbl)))
2508 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2509 tbl)))
2511 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2512 (defun org-check-agenda-marker-table ()
2513 "Check of the current entry is on the marker list."
2514 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2516 (and (setq a (assoc file org-agenda-marker-table))
2517 (save-match-data
2518 (save-excursion
2519 (org-back-to-heading t)
2520 (member (point) (cdr a)))))))
2522 (defun org-check-for-org-mode ()
2523 "Make sure current buffer is in org-mode. Error if not."
2524 (or (org-mode-p)
2525 (error "Cannot execute org-mode agenda command on buffer in %s."
2526 major-mode)))
2528 (defun org-fit-agenda-window ()
2529 "Fit the window to the buffer size."
2530 (and (memq org-agenda-window-setup '(reorganize-frame))
2531 (fboundp 'fit-window-to-buffer)
2532 (org-fit-window-to-buffer
2534 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
2535 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
2537 ;;; Agenda prepare and finalize
2539 (defvar org-agenda-multi nil) ; dynamically scoped
2540 (defvar org-agenda-buffer-name "*Org Agenda*")
2541 (defvar org-pre-agenda-window-conf nil)
2542 (defvar org-agenda-columns-active nil)
2543 (defvar org-agenda-name nil)
2544 (defvar org-agenda-filter nil)
2545 (defvar org-agenda-filter-preset nil
2546 "A preset of the tags filter used for secondary agenda filtering.
2547 This must be a list of strings, each string must be a single tag preceeded
2548 by \"+\" or \"-\".
2549 This variable should not be set directly, but agenda custom commands can
2550 bind it in the options section.")
2552 (defun org-prepare-agenda (&optional name)
2553 (setq org-todo-keywords-for-agenda nil)
2554 (setq org-done-keywords-for-agenda nil)
2555 (setq org-agenda-filter nil)
2556 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
2557 (if org-agenda-multi
2558 (progn
2559 (setq buffer-read-only nil)
2560 (goto-char (point-max))
2561 (unless (or (bobp) org-agenda-compact-blocks)
2562 (insert "\n"
2563 (if (stringp org-agenda-block-separator)
2564 org-agenda-block-separator
2565 (make-string (window-width) org-agenda-block-separator))
2566 "\n"))
2567 (narrow-to-region (point) (point-max)))
2568 (org-agenda-reset-markers)
2569 (setq org-agenda-contributing-files nil)
2570 (setq org-agenda-columns-active nil)
2571 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
2572 (setq org-todo-keywords-for-agenda
2573 (org-uniquify org-todo-keywords-for-agenda))
2574 (setq org-done-keywords-for-agenda
2575 (org-uniquify org-done-keywords-for-agenda))
2576 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
2577 (awin (get-buffer-window abuf)))
2578 (cond
2579 ((equal (current-buffer) abuf) nil)
2580 (awin (select-window awin))
2581 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
2582 ((equal org-agenda-window-setup 'current-window)
2583 (switch-to-buffer abuf))
2584 ((equal org-agenda-window-setup 'other-window)
2585 (org-switch-to-buffer-other-window abuf))
2586 ((equal org-agenda-window-setup 'other-frame)
2587 (switch-to-buffer-other-frame abuf)
2588 (set-window-dedicated-p (selected-window) t))
2589 ((equal org-agenda-window-setup 'reorganize-frame)
2590 (delete-other-windows)
2591 (org-switch-to-buffer-other-window abuf))))
2592 (setq buffer-read-only nil)
2593 (let ((inhibit-read-only t)) (erase-buffer))
2594 (org-agenda-mode)
2595 (and name (not org-agenda-name)
2596 (org-set-local 'org-agenda-name name)))
2597 (setq buffer-read-only nil))
2599 (defun org-finalize-agenda ()
2600 "Finishing touch for the agenda buffer, called just before displaying it."
2601 (unless org-agenda-multi
2602 (save-excursion
2603 (let ((inhibit-read-only t))
2604 (goto-char (point-min))
2605 (while (org-activate-bracket-links (point-max))
2606 (add-text-properties (match-beginning 0) (match-end 0)
2607 '(face org-link)))
2608 (org-agenda-align-tags)
2609 (unless org-agenda-with-colors
2610 (remove-text-properties (point-min) (point-max) '(face nil))))
2611 (if (and (boundp 'org-agenda-overriding-columns-format)
2612 org-agenda-overriding-columns-format)
2613 (org-set-local 'org-agenda-overriding-columns-format
2614 org-agenda-overriding-columns-format))
2615 (if (and (boundp 'org-agenda-view-columns-initially)
2616 org-agenda-view-columns-initially)
2617 (org-agenda-columns))
2618 (when org-agenda-fontify-priorities
2619 (org-agenda-fontify-priorities))
2620 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
2621 (org-agenda-dim-blocked-tasks))
2622 (org-agenda-mark-clocking-task)
2623 (when org-agenda-entry-text-mode
2624 (org-agenda-entry-text-hide)
2625 (org-agenda-entry-text-show))
2626 (run-hooks 'org-finalize-agenda-hook)
2627 (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
2628 (when (get 'org-agenda-filter :preset-filter)
2629 (org-agenda-filter-apply org-agenda-filter))
2632 (defun org-agenda-mark-clocking-task ()
2633 "Mark the current clock entry in the agenda if it is present."
2634 (mapc (lambda (o)
2635 (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
2636 (org-delete-overlay o)))
2637 (org-overlays-in (point-min) (point-max)))
2638 (when (marker-buffer org-clock-hd-marker)
2639 (save-excursion
2640 (goto-char (point-min))
2641 (let (s ov)
2642 (while (setq s (next-single-property-change (point) 'org-hd-marker))
2643 (goto-char s)
2644 (when (equal (get-text-property (point) 'org-hd-marker)
2645 org-clock-hd-marker)
2646 (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
2647 (org-overlay-put ov 'type 'org-agenda-clocking)
2648 (org-overlay-put ov 'face 'org-agenda-clocking)
2649 (org-overlay-put ov 'help-echo
2650 "The clock is running in this item")))))))
2652 (defun org-agenda-fontify-priorities ()
2653 "Make highest priority lines bold, and lowest italic."
2654 (interactive)
2655 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
2656 (org-delete-overlay o)))
2657 (org-overlays-in (point-min) (point-max)))
2658 (save-excursion
2659 (let ((inhibit-read-only t)
2660 b e p ov h l)
2661 (goto-char (point-min))
2662 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
2663 (setq h (or (get-char-property (point) 'org-highest-priority)
2664 org-highest-priority)
2665 l (or (get-char-property (point) 'org-lowest-priority)
2666 org-lowest-priority)
2667 p (string-to-char (match-string 1))
2668 b (match-beginning 0)
2669 e (if (eq org-agenda-fontify-priorities 'cookies)
2670 (match-end 0)
2671 (point-at-eol))
2672 ov (org-make-overlay b e))
2673 (org-overlay-put
2674 ov 'face
2675 (cond ((cdr (assoc p org-priority-faces)))
2676 ((and (listp org-agenda-fontify-priorities)
2677 (cdr (assoc p org-agenda-fontify-priorities))))
2678 ((equal p l) 'italic)
2679 ((equal p h) 'bold)))
2680 (org-overlay-put ov 'org-type 'org-priority)))))
2682 (defun org-agenda-dim-blocked-tasks ()
2683 "Dim currently blocked TODO's in the agenda display."
2684 (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
2685 (org-delete-overlay o)))
2686 (org-overlays-in (point-min) (point-max)))
2687 (save-excursion
2688 (let ((inhibit-read-only t)
2689 (org-depend-tag-blocked nil)
2690 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
2691 org-blocked-by-checkboxes
2692 invis1 b e p ov h l)
2693 (goto-char (point-min))
2694 (while (let ((pos (next-single-property-change (point) 'todo-state)))
2695 (and pos (goto-char (1+ pos))))
2696 (setq org-blocked-by-checkboxes nil invis1 invis)
2697 (let ((marker (get-text-property (point) 'org-hd-marker)))
2698 (when (and marker
2699 (not (with-current-buffer (marker-buffer marker)
2700 (save-excursion
2701 (goto-char marker)
2702 (if (org-entry-get nil "NOBLOCKING")
2703 t ;; Never block this entry
2704 (run-hook-with-args-until-failure
2705 'org-blocker-hook
2706 (list :type 'todo-state-change
2707 :position marker
2708 :from 'todo
2709 :to 'done)))))))
2710 (if org-blocked-by-checkboxes (setq invis1 nil))
2711 (setq b (if invis1
2712 (max (point-min) (1- (point-at-bol)))
2713 (point-at-bol))
2714 e (point-at-eol)
2715 ov (org-make-overlay b e))
2716 (if invis1
2717 (org-overlay-put ov 'invisible t)
2718 (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
2719 (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
2721 (defvar org-agenda-skip-function nil
2722 "Function to be called at each match during agenda construction.
2723 If this function returns nil, the current match should not be skipped.
2724 Otherwise, the function must return a position from where the search
2725 should be continued.
2726 This may also be a Lisp form, it will be evaluated.
2727 Never set this variable using `setq' or so, because then it will apply
2728 to all future agenda commands. Instead, bind it with `let' to scope
2729 it dynamically into the agenda-constructing command. A good way to set
2730 it is through options in org-agenda-custom-commands.")
2732 (defun org-agenda-skip ()
2733 "Throw to `:skip' in places that should be skipped.
2734 Also moves point to the end of the skipped region, so that search can
2735 continue from there."
2736 (let ((p (point-at-bol)) to fp)
2737 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
2738 (get-text-property p :org-archived)
2739 (org-end-of-subtree t)
2740 (throw :skip t))
2741 (and org-agenda-skip-comment-trees
2742 (get-text-property p :org-comment)
2743 (org-end-of-subtree t)
2744 (throw :skip t))
2745 (if (equal (char-after p) ?#) (throw :skip t))
2746 (when (and (or (setq fp (functionp org-agenda-skip-function))
2747 (consp org-agenda-skip-function))
2748 (setq to (save-excursion
2749 (save-match-data
2750 (if fp
2751 (funcall org-agenda-skip-function)
2752 (eval org-agenda-skip-function))))))
2753 (goto-char to)
2754 (throw :skip t))))
2756 (defvar org-agenda-markers nil
2757 "List of all currently active markers created by `org-agenda'.")
2758 (defvar org-agenda-last-marker-time (org-float-time)
2759 "Creation time of the last agenda marker.")
2761 (defun org-agenda-new-marker (&optional pos)
2762 "Return a new agenda marker.
2763 Org-mode keeps a list of these markers and resets them when they are
2764 no longer in use."
2765 (let ((m (copy-marker (or pos (point)))))
2766 (setq org-agenda-last-marker-time (org-float-time))
2767 (push m org-agenda-markers)
2770 (defun org-agenda-reset-markers ()
2771 "Reset markers created by `org-agenda'."
2772 (while org-agenda-markers
2773 (move-marker (pop org-agenda-markers) nil)))
2775 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
2776 "Save relative positions of markers in region."
2777 (mapc (lambda (m) (org-check-and-save-marker m beg end))
2778 org-agenda-markers))
2780 ;;; Entry text mode
2782 (defun org-agenda-entry-text-show-here ()
2783 "Add some text from te entry as context to the current line."
2784 (let (m txt o)
2785 (setq m (get-text-property (point) 'org-hd-marker))
2786 (unless (marker-buffer m)
2787 (error "No marker points to an entry here"))
2788 (setq txt (concat "\n" (org-no-properties
2789 (org-agenda-get-some-entry-text
2790 m org-agenda-entry-text-maxlines " > "))))
2791 (when (string-match "\\S-" txt)
2792 (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
2793 (org-overlay-put o 'evaporate t)
2794 (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
2795 (org-overlay-put o 'after-string txt))))
2797 (defun org-agenda-entry-text-show ()
2798 "Add entry context for all agenda lines."
2799 (interactive)
2800 (save-excursion
2801 (goto-char (point-max))
2802 (beginning-of-line 1)
2803 (while (not (bobp))
2804 (when (get-text-property (point) 'org-hd-marker)
2805 (org-agenda-entry-text-show-here))
2806 (beginning-of-line 0))))
2808 (defun org-agenda-entry-text-hide ()
2809 "Remove any shown entry context."
2810 (delq nil
2811 (mapcar (lambda (o)
2812 (if (eq (org-overlay-get o 'org-overlay-type)
2813 'agenda-entry-content)
2814 (progn (org-delete-overlay o) t)))
2815 (org-overlays-in (point-min) (point-max)))))
2817 ;;; Agenda timeline
2819 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
2821 (defun org-timeline (&optional include-all)
2822 "Show a time-sorted view of the entries in the current org file.
2823 Only entries with a time stamp of today or later will be listed. With
2824 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
2825 under the current date.
2826 If the buffer contains an active region, only check the region for
2827 dates."
2828 (interactive "P")
2829 (require 'calendar)
2830 (org-compile-prefix-format 'timeline)
2831 (org-set-sorting-strategy 'timeline)
2832 (let* ((dopast t)
2833 (dotodo include-all)
2834 (doclosed org-agenda-show-log)
2835 (entry buffer-file-name)
2836 (date (calendar-current-date))
2837 (beg (if (org-region-active-p) (region-beginning) (point-min)))
2838 (end (if (org-region-active-p) (region-end) (point-max)))
2839 (day-numbers (org-get-all-dates beg end 'no-ranges
2840 t doclosed ; always include today
2841 org-timeline-show-empty-dates))
2842 (org-deadline-warning-days 0)
2843 (org-agenda-only-exact-dates t)
2844 (today (time-to-days (current-time)))
2845 (past t)
2846 args
2847 s e rtn d emptyp wd)
2848 (setq org-agenda-redo-command
2849 (list 'progn
2850 (list 'org-switch-to-buffer-other-window (current-buffer))
2851 (list 'org-timeline (list 'quote include-all))))
2852 (if (not dopast)
2853 ;; Remove past dates from the list of dates.
2854 (setq day-numbers (delq nil (mapcar (lambda(x)
2855 (if (>= x today) x nil))
2856 day-numbers))))
2857 (org-prepare-agenda (concat "Timeline "
2858 (file-name-nondirectory buffer-file-name)))
2859 (if doclosed (push :closed args))
2860 (push :timestamp args)
2861 (push :deadline args)
2862 (push :scheduled args)
2863 (push :sexp args)
2864 (if dotodo (push :todo args))
2865 (insert "Timeline of file " entry "\n")
2866 (add-text-properties (point-min) (point)
2867 (list 'face 'org-agenda-structure))
2868 (org-agenda-mark-header-line (point-min))
2869 (while (setq d (pop day-numbers))
2870 (if (and (listp d) (eq (car d) :omitted))
2871 (progn
2872 (setq s (point))
2873 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
2874 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
2875 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
2876 (if (and (>= d today)
2877 dopast
2878 past)
2879 (progn
2880 (setq past nil)
2881 (insert (make-string 79 ?-) "\n")))
2882 (setq date (calendar-gregorian-from-absolute d)
2883 wd (calendar-day-of-week date))
2884 (setq s (point))
2885 (setq rtn (and (not emptyp)
2886 (apply 'org-agenda-get-day-entries entry
2887 date args)))
2888 (if (or rtn (equal d today) org-timeline-show-empty-dates)
2889 (progn
2890 (insert
2891 (if (stringp org-agenda-format-date)
2892 (format-time-string org-agenda-format-date
2893 (org-time-from-absolute date))
2894 (funcall org-agenda-format-date date))
2895 "\n")
2896 (put-text-property s (1- (point)) 'face
2897 (if (member wd org-agenda-weekend-days)
2898 'org-agenda-date-weekend
2899 'org-agenda-date))
2900 (put-text-property s (1- (point)) 'org-date-line t)
2901 (put-text-property s (1- (point)) 'org-agenda-date-header t)
2902 (if (equal d today)
2903 (put-text-property s (1- (point)) 'org-today t))
2904 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
2905 (put-text-property s (1- (point)) 'day d)))))
2906 (goto-char (point-min))
2907 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
2908 (point-min)))
2909 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
2910 (org-finalize-agenda)
2911 (setq buffer-read-only t)))
2913 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
2914 "Return a list of all relevant day numbers from BEG to END buffer positions.
2915 If NO-RANGES is non-nil, include only the start and end dates of a range,
2916 not every single day in the range. If FORCE-TODAY is non-nil, make
2917 sure that TODAY is included in the list. If INACTIVE is non-nil, also
2918 inactive time stamps (those in square brackets) are included.
2919 When EMPTY is non-nil, also include days without any entries."
2920 (let ((re (concat
2921 (if pre-re pre-re "")
2922 (if inactive org-ts-regexp-both org-ts-regexp)))
2923 dates dates1 date day day1 day2 ts1 ts2)
2924 (if force-today
2925 (setq dates (list (time-to-days (current-time)))))
2926 (save-excursion
2927 (goto-char beg)
2928 (while (re-search-forward re end t)
2929 (setq day (time-to-days (org-time-string-to-time
2930 (substring (match-string 1) 0 10))))
2931 (or (memq day dates) (push day dates)))
2932 (unless no-ranges
2933 (goto-char beg)
2934 (while (re-search-forward org-tr-regexp end t)
2935 (setq ts1 (substring (match-string 1) 0 10)
2936 ts2 (substring (match-string 2) 0 10)
2937 day1 (time-to-days (org-time-string-to-time ts1))
2938 day2 (time-to-days (org-time-string-to-time ts2)))
2939 (while (< (setq day1 (1+ day1)) day2)
2940 (or (memq day1 dates) (push day1 dates)))))
2941 (setq dates (sort dates '<))
2942 (when empty
2943 (while (setq day (pop dates))
2944 (setq day2 (car dates))
2945 (push day dates1)
2946 (when (and day2 empty)
2947 (if (or (eq empty t)
2948 (and (numberp empty) (<= (- day2 day) empty)))
2949 (while (< (setq day (1+ day)) day2)
2950 (push (list day) dates1))
2951 (push (cons :omitted (- day2 day)) dates1))))
2952 (setq dates (nreverse dates1)))
2953 dates)))
2955 ;;; Agenda Daily/Weekly
2957 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2958 (defvar org-agenda-start-day nil ; dynamically scoped parameter
2959 "Custom commands can set this variable in the options section.")
2960 (defvar org-agenda-last-arguments nil
2961 "The arguments of the previous call to org-agenda")
2962 (defvar org-starting-day nil) ; local variable in the agenda buffer
2963 (defvar org-agenda-span nil) ; local variable in the agenda buffer
2964 (defvar org-include-all-loc nil) ; local variable
2965 (defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
2967 ;;;###autoload
2968 (defun org-agenda-list (&optional include-all start-day ndays)
2969 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
2970 The view will be for the current day or week, but from the overview buffer
2971 you will be able to go to other days/weeks.
2973 With one \\[universal-argument] prefix argument INCLUDE-ALL,
2974 all unfinished TODO items will also be shown, before the agenda.
2975 This feature is considered obsolete, please use the TODO list or a block
2976 agenda instead.
2978 With a numeric prefix argument in an interactive call, the agenda will
2979 span INCLUDE-ALL days. Lisp programs should instead specify NDAYS to change
2980 the number of days. NDAYS defaults to `org-agenda-ndays'.
2982 START-DAY defaults to TODAY, or to the most recent match for the weekday
2983 given in `org-agenda-start-on-weekday'."
2984 (interactive "P")
2985 (if (and (integerp include-all) (> include-all 0))
2986 (setq ndays include-all include-all nil))
2987 (setq ndays (or ndays org-agenda-ndays)
2988 start-day (or start-day org-agenda-start-day))
2989 (if org-agenda-overriding-arguments
2990 (setq include-all (car org-agenda-overriding-arguments)
2991 start-day (nth 1 org-agenda-overriding-arguments)
2992 ndays (nth 2 org-agenda-overriding-arguments)))
2993 (if (stringp start-day)
2994 ;; Convert to an absolute day number
2995 (setq start-day (time-to-days (org-read-date nil t start-day))))
2996 (setq org-agenda-last-arguments (list include-all start-day ndays))
2997 (org-compile-prefix-format 'agenda)
2998 (org-set-sorting-strategy 'agenda)
2999 (require 'calendar)
3000 (let* ((org-agenda-start-on-weekday
3001 (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
3002 org-agenda-start-on-weekday nil))
3003 (thefiles (org-agenda-files nil 'ifmode))
3004 (files thefiles)
3005 (today (time-to-days
3006 (time-subtract (current-time)
3007 (list 0 (* 3600 org-extend-today-until) 0))))
3008 (sd (or start-day today))
3009 (start (if (or (null org-agenda-start-on-weekday)
3010 (< org-agenda-ndays 7))
3012 (let* ((nt (calendar-day-of-week
3013 (calendar-gregorian-from-absolute sd)))
3014 (n1 org-agenda-start-on-weekday)
3015 (d (- nt n1)))
3016 (- sd (+ (if (< d 0) 7 0) d)))))
3017 (day-numbers (list start))
3018 (day-cnt 0)
3019 (inhibit-redisplay (not debug-on-error))
3020 s e rtn rtnall file date d start-pos end-pos todayp nd wd
3021 clocktable-start clocktable-end)
3022 (setq org-agenda-redo-command
3023 (list 'org-agenda-list (list 'quote include-all) start-day ndays))
3024 ;; Make the list of days
3025 (setq ndays (or ndays org-agenda-ndays)
3026 nd ndays)
3027 (while (> ndays 1)
3028 (push (1+ (car day-numbers)) day-numbers)
3029 (setq ndays (1- ndays)))
3030 (setq day-numbers (nreverse day-numbers))
3031 (setq clocktable-start (car day-numbers)
3032 clocktable-end (1+ (or (org-last day-numbers) 0)))
3033 (org-prepare-agenda "Day/Week")
3034 (org-set-local 'org-starting-day (car day-numbers))
3035 (org-set-local 'org-include-all-loc include-all)
3036 (org-set-local 'org-agenda-span
3037 (org-agenda-ndays-to-span nd))
3038 (when (and (or include-all org-agenda-include-all-todo)
3039 (member today day-numbers))
3040 (setq files thefiles
3041 rtnall nil)
3042 (while (setq file (pop files))
3043 (catch 'nextfile
3044 (org-check-agenda-file file)
3045 (setq date (calendar-gregorian-from-absolute today)
3046 rtn (org-agenda-get-day-entries
3047 file date :todo))
3048 (setq rtnall (append rtnall rtn))))
3049 (when rtnall
3050 (insert "All currently open TODO items:\n")
3051 (add-text-properties (point-min) (1- (point))
3052 (list 'face 'org-agenda-structure
3053 'short-heading "All TODO items"))
3054 (insert (org-finalize-agenda-entries rtnall) "\n")))
3055 (unless org-agenda-compact-blocks
3056 (let* ((d1 (car day-numbers))
3057 (d2 (org-last day-numbers))
3058 (w1 (org-days-to-iso-week d1))
3059 (w2 (org-days-to-iso-week d2)))
3060 (setq s (point))
3061 (if org-agenda-overriding-header
3062 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3063 nil 'face 'org-agenda-structure) "\n")
3064 (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
3065 "-agenda"
3066 (if (< (- d2 d1) 350)
3067 (if (= w1 w2)
3068 (format " (W%02d)" w1)
3069 (format " (W%02d-W%02d)" w1 w2))
3071 ":\n")))
3072 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3073 'org-date-line t))
3074 (org-agenda-mark-header-line s))
3075 (while (setq d (pop day-numbers))
3076 (setq date (calendar-gregorian-from-absolute d)
3077 wd (calendar-day-of-week date)
3078 s (point))
3079 (if (or (setq todayp (= d today))
3080 (and (not start-pos) (= d sd)))
3081 (setq start-pos (point))
3082 (if (and start-pos (not end-pos))
3083 (setq end-pos (point))))
3084 (setq files thefiles
3085 rtnall nil)
3086 (while (setq file (pop files))
3087 (catch 'nextfile
3088 (org-check-agenda-file file)
3089 (cond
3090 ((eq org-agenda-show-log 'only)
3091 (setq rtn (org-agenda-get-day-entries
3092 file date :closed)))
3093 (org-agenda-show-log
3094 (setq rtn (org-agenda-get-day-entries
3095 file date
3096 :deadline :scheduled :timestamp :sexp :closed)))
3098 (setq rtn (org-agenda-get-day-entries
3099 file date
3100 :deadline :scheduled :sexp :timestamp))))
3101 (setq rtnall (append rtnall rtn))))
3102 (if org-agenda-include-diary
3103 (let ((org-agenda-search-headline-for-time t))
3104 (require 'diary-lib)
3105 (setq rtn (org-get-entries-from-diary date))
3106 (setq rtnall (append rtnall rtn))))
3107 (if (or rtnall org-agenda-show-all-dates)
3108 (progn
3109 (setq day-cnt (1+ day-cnt))
3110 (insert
3111 (if (stringp org-agenda-format-date)
3112 (format-time-string org-agenda-format-date
3113 (org-time-from-absolute date))
3114 (funcall org-agenda-format-date date))
3115 "\n")
3116 (put-text-property s (1- (point)) 'face
3117 (if (member wd org-agenda-weekend-days)
3118 'org-agenda-date-weekend
3119 'org-agenda-date))
3120 (put-text-property s (1- (point)) 'org-date-line t)
3121 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3122 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3123 (when todayp
3124 (put-text-property s (1- (point)) 'org-today t)
3125 (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
3126 (if rtnall (insert
3127 (org-finalize-agenda-entries
3128 (org-agenda-add-time-grid-maybe
3129 rtnall nd todayp))
3130 "\n"))
3131 (put-text-property s (1- (point)) 'day d)
3132 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3133 (when (and org-agenda-clockreport-mode clocktable-start)
3134 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3135 ;; the above line is to ensure the restricted range!
3136 (p org-agenda-clockreport-parameter-plist)
3137 tbl)
3138 (setq p (org-plist-delete p :block))
3139 (setq p (plist-put p :tstart clocktable-start))
3140 (setq p (plist-put p :tend clocktable-end))
3141 (setq p (plist-put p :scope 'agenda))
3142 (setq tbl (apply 'org-get-clocktable p))
3143 (insert tbl)))
3144 (goto-char (point-min))
3145 (or org-agenda-multi (org-fit-agenda-window))
3146 (unless (and (pos-visible-in-window-p (point-min))
3147 (pos-visible-in-window-p (point-max)))
3148 (goto-char (1- (point-max)))
3149 (recenter -1)
3150 (if (not (pos-visible-in-window-p (or start-pos 1)))
3151 (progn
3152 (goto-char (or start-pos 1))
3153 (recenter 1))))
3154 (goto-char (or start-pos 1))
3155 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3156 (org-finalize-agenda)
3157 (setq buffer-read-only t)
3158 (message "")))
3160 (defun org-agenda-ndays-to-span (n)
3161 (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
3163 ;;; Agenda word search
3165 (defvar org-agenda-search-history nil)
3166 (defvar org-todo-only nil)
3168 (defvar org-search-syntax-table nil
3169 "Special syntax table for org-mode search.
3170 In this table, we have single quotes not as word constituents, to
3171 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3173 (defun org-search-syntax-table ()
3174 (unless org-search-syntax-table
3175 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3176 (modify-syntax-entry ?' "." org-search-syntax-table)
3177 (modify-syntax-entry ?` "." org-search-syntax-table))
3178 org-search-syntax-table)
3180 ;;;###autoload
3181 (defun org-search-view (&optional todo-only string edit-at)
3182 "Show all entries that contain words or regular expressions.
3183 If the first character of the search string is an asterisks,
3184 search only the headlines.
3186 With optional prefix argument TODO-ONLY, only consider entries that are
3187 TODO entries. The argument STRING can be used to pass a default search
3188 string into this function. If EDIT-AT is non-nil, it means that the
3189 user should get a chance to edit this string, with cursor at position
3190 EDIT-AT.
3192 The search string is broken into \"words\" by splitting at whitespace.
3193 The individual words are then interpreted as a boolean expression with
3194 logical AND. Words prefixed with a minus must not occur in the entry.
3195 Words without a prefix or prefixed with a plus must occur in the entry.
3196 Matching is case-insensitive and the words are enclosed by word delimiters.
3198 Words enclosed by curly braces are interpreted as regular expressions
3199 that must or must not match in the entry.
3201 If the search string starts with an asterisk, search only in headlines.
3202 If (possibly after the leading star) the search string starts with an
3203 exclamation mark, this also means to look at TODO entries only, an effect
3204 that can also be achieved with a prefix argument.
3206 This command searches the agenda files, and in addition the files listed
3207 in `org-agenda-text-search-extra-files'."
3208 (interactive "P")
3209 (org-compile-prefix-format 'search)
3210 (org-set-sorting-strategy 'search)
3211 (org-prepare-agenda "SEARCH")
3212 (let* ((props (list 'face nil
3213 'done-face 'org-agenda-done
3214 'org-not-done-regexp org-not-done-regexp
3215 'org-todo-regexp org-todo-regexp
3216 'org-complex-heading-regexp org-complex-heading-regexp
3217 'mouse-face 'highlight
3218 'keymap org-agenda-keymap
3219 'help-echo (format "mouse-2 or RET jump to location")))
3220 regexp rtn rtnall files file pos
3221 marker category tags c neg re
3222 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3223 (unless (and (not edit-at)
3224 (stringp string)
3225 (string-match "\\S-" string))
3226 (setq string (read-string "[+-]Word/{Regexp} ...: "
3227 (cond
3228 ((integerp edit-at) (cons string edit-at))
3229 (edit-at string))
3230 'org-agenda-search-history)))
3231 (org-set-local 'org-todo-only todo-only)
3232 (setq org-agenda-redo-command
3233 (list 'org-search-view (if todo-only t nil) string
3234 '(if current-prefix-arg 1 nil)))
3235 (setq org-agenda-query-string string)
3237 (if (equal (string-to-char string) ?*)
3238 (setq hdl-only t
3239 words (substring string 1))
3240 (setq words string))
3241 (when (equal (string-to-char words) ?!)
3242 (setq todo-only t
3243 words (substring words 1)))
3244 (setq words (org-split-string words))
3245 (mapc (lambda (w)
3246 (setq c (string-to-char w))
3247 (if (equal c ?-)
3248 (setq neg t w (substring w 1))
3249 (if (equal c ?+)
3250 (setq neg nil w (substring w 1))
3251 (setq neg nil)))
3252 (if (string-match "\\`{.*}\\'" w)
3253 (setq re (substring w 1 -1))
3254 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
3255 (if neg (push re regexps-) (push re regexps+)))
3256 words)
3257 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3258 (if (not regexps+)
3259 (setq regexp (concat "^" org-outline-regexp))
3260 (setq regexp (pop regexps+))
3261 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3262 regexp))))
3263 (setq files (org-agenda-files nil 'ifmode))
3264 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3265 (pop org-agenda-text-search-extra-files)
3266 (setq files (org-add-archive-files files)))
3267 (setq files (append files org-agenda-text-search-extra-files)
3268 rtnall nil)
3269 (while (setq file (pop files))
3270 (setq ee nil)
3271 (catch 'nextfile
3272 (org-check-agenda-file file)
3273 (setq buffer (if (file-exists-p file)
3274 (org-get-agenda-file-buffer file)
3275 (error "No such file %s" file)))
3276 (if (not buffer)
3277 ;; If file does not exist, make sure an error message is sent
3278 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3279 file))))
3280 (with-current-buffer buffer
3281 (with-syntax-table (org-search-syntax-table)
3282 (unless (org-mode-p)
3283 (error "Agenda file %s is not in `org-mode'" file))
3284 (let ((case-fold-search t))
3285 (save-excursion
3286 (save-restriction
3287 (if org-agenda-restrict
3288 (narrow-to-region org-agenda-restrict-begin
3289 org-agenda-restrict-end)
3290 (widen))
3291 (goto-char (point-min))
3292 (unless (or (org-on-heading-p)
3293 (outline-next-heading))
3294 (throw 'nextfile t))
3295 (goto-char (max (point-min) (1- (point))))
3296 (while (re-search-forward regexp nil t)
3297 (org-back-to-heading t)
3298 (skip-chars-forward "* ")
3299 (setq beg (point-at-bol)
3300 beg1 (point)
3301 end (progn (outline-next-heading) (point)))
3302 (catch :skip
3303 (goto-char beg)
3304 (org-agenda-skip)
3305 (setq str (buffer-substring-no-properties
3306 (point-at-bol)
3307 (if hdl-only (point-at-eol) end)))
3308 (mapc (lambda (wr) (when (string-match wr str)
3309 (goto-char (1- end))
3310 (throw :skip t)))
3311 regexps-)
3312 (mapc (lambda (wr) (unless (string-match wr str)
3313 (goto-char (1- end))
3314 (throw :skip t)))
3315 (if todo-only
3316 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3317 regexps+)
3318 regexps+))
3319 (goto-char beg)
3320 (setq marker (org-agenda-new-marker (point))
3321 category (org-get-category)
3322 tags (org-get-tags-at (point))
3323 txt (org-format-agenda-item
3325 (buffer-substring-no-properties
3326 beg1 (point-at-eol))
3327 category tags))
3328 (org-add-props txt props
3329 'org-marker marker 'org-hd-marker marker
3330 'org-todo-regexp org-todo-regexp
3331 'org-complex-heading-regexp org-complex-heading-regexp
3332 'priority 1000 'org-category category
3333 'type "search")
3334 (push txt ee)
3335 (goto-char (1- end))))))))))
3336 (setq rtn (nreverse ee))
3337 (setq rtnall (append rtnall rtn)))
3338 (if org-agenda-overriding-header
3339 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3340 nil 'face 'org-agenda-structure) "\n")
3341 (insert "Search words: ")
3342 (add-text-properties (point-min) (1- (point))
3343 (list 'face 'org-agenda-structure))
3344 (setq pos (point))
3345 (insert string "\n")
3346 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3347 (setq pos (point))
3348 (unless org-agenda-multi
3349 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3350 (add-text-properties pos (1- (point))
3351 (list 'face 'org-agenda-structure))))
3352 (org-agenda-mark-header-line (point-min))
3353 (when rtnall
3354 (insert (org-finalize-agenda-entries rtnall) "\n"))
3355 (goto-char (point-min))
3356 (or org-agenda-multi (org-fit-agenda-window))
3357 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3358 (org-finalize-agenda)
3359 (setq buffer-read-only t)))
3361 ;;; Agenda TODO list
3363 (defvar org-select-this-todo-keyword nil)
3364 (defvar org-last-arg nil)
3366 ;;;###autoload
3367 (defun org-todo-list (arg)
3368 "Show all TODO entries from all agenda file in a single list.
3369 The prefix arg can be used to select a specific TODO keyword and limit
3370 the list to these. When using \\[universal-argument], you will be prompted
3371 for a keyword. A numeric prefix directly selects the Nth keyword in
3372 `org-todo-keywords-1'."
3373 (interactive "P")
3374 (require 'calendar)
3375 (org-compile-prefix-format 'todo)
3376 (org-set-sorting-strategy 'todo)
3377 (org-prepare-agenda "TODO")
3378 (let* ((today (time-to-days (current-time)))
3379 (date (calendar-gregorian-from-absolute today))
3380 (kwds org-todo-keywords-for-agenda)
3381 (completion-ignore-case t)
3382 (org-select-this-todo-keyword
3383 (if (stringp arg) arg
3384 (and arg (integerp arg) (> arg 0)
3385 (nth (1- arg) kwds))))
3386 rtn rtnall files file pos)
3387 (when (equal arg '(4))
3388 (setq org-select-this-todo-keyword
3389 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
3390 (mapcar 'list kwds) nil nil)))
3391 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
3392 (org-set-local 'org-last-arg arg)
3393 (setq org-agenda-redo-command
3394 '(org-todo-list (or current-prefix-arg org-last-arg)))
3395 (setq files (org-agenda-files nil 'ifmode)
3396 rtnall nil)
3397 (while (setq file (pop files))
3398 (catch 'nextfile
3399 (org-check-agenda-file file)
3400 (setq rtn (org-agenda-get-day-entries file date :todo))
3401 (setq rtnall (append rtnall rtn))))
3402 (if org-agenda-overriding-header
3403 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3404 nil 'face 'org-agenda-structure) "\n")
3405 (insert "Global list of TODO items of type: ")
3406 (add-text-properties (point-min) (1- (point))
3407 (list 'face 'org-agenda-structure
3408 'short-heading
3409 (concat "ToDo: "
3410 (or org-select-this-todo-keyword "ALL"))))
3411 (org-agenda-mark-header-line (point-min))
3412 (setq pos (point))
3413 (insert (or org-select-this-todo-keyword "ALL") "\n")
3414 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3415 (setq pos (point))
3416 (unless org-agenda-multi
3417 (insert "Available with `N r': (0)ALL")
3418 (let ((n 0) s)
3419 (mapc (lambda (x)
3420 (setq s (format "(%d)%s" (setq n (1+ n)) x))
3421 (if (> (+ (current-column) (string-width s) 1) (frame-width))
3422 (insert "\n "))
3423 (insert " " s))
3424 kwds))
3425 (insert "\n"))
3426 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3427 (org-agenda-mark-header-line (point-min))
3428 (when rtnall
3429 (insert (org-finalize-agenda-entries rtnall) "\n"))
3430 (goto-char (point-min))
3431 (or org-agenda-multi (org-fit-agenda-window))
3432 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
3433 (org-finalize-agenda)
3434 (setq buffer-read-only t)))
3436 ;;; Agenda tags match
3438 ;;;###autoload
3439 (defun org-tags-view (&optional todo-only match)
3440 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
3441 The prefix arg TODO-ONLY limits the search to TODO entries."
3442 (interactive "P")
3443 (org-compile-prefix-format 'tags)
3444 (org-set-sorting-strategy 'tags)
3445 (let* ((org-tags-match-list-sublevels
3446 ;?????? (if todo-only t org-tags-match-list-sublevels))
3447 org-tags-match-list-sublevels)
3448 (completion-ignore-case t)
3449 rtn rtnall files file pos matcher
3450 buffer)
3451 (setq matcher (org-make-tags-matcher match)
3452 match (car matcher) matcher (cdr matcher))
3453 (org-prepare-agenda (concat "TAGS " match))
3454 (setq org-agenda-query-string match)
3455 (setq org-agenda-redo-command
3456 (list 'org-tags-view (list 'quote todo-only)
3457 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
3458 (setq files (org-agenda-files nil 'ifmode)
3459 rtnall nil)
3460 (while (setq file (pop files))
3461 (catch 'nextfile
3462 (org-check-agenda-file file)
3463 (setq buffer (if (file-exists-p file)
3464 (org-get-agenda-file-buffer file)
3465 (error "No such file %s" file)))
3466 (if (not buffer)
3467 ;; If file does not exist, error message to agenda
3468 (setq rtn (list
3469 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3470 rtnall (append rtnall rtn))
3471 (with-current-buffer buffer
3472 (unless (org-mode-p)
3473 (error "Agenda file %s is not in `org-mode'" file))
3474 (save-excursion
3475 (save-restriction
3476 (if org-agenda-restrict
3477 (narrow-to-region org-agenda-restrict-begin
3478 org-agenda-restrict-end)
3479 (widen))
3480 (setq rtn (org-scan-tags 'agenda matcher todo-only))
3481 (setq rtnall (append rtnall rtn))))))))
3482 (if org-agenda-overriding-header
3483 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3484 nil 'face 'org-agenda-structure) "\n")
3485 (insert "Headlines with TAGS match: ")
3486 (add-text-properties (point-min) (1- (point))
3487 (list 'face 'org-agenda-structure
3488 'short-heading
3489 (concat "Match: " match)))
3490 (setq pos (point))
3491 (insert match "\n")
3492 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3493 (setq pos (point))
3494 (unless org-agenda-multi
3495 (insert "Press `C-u r' to search again with new search string\n"))
3496 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
3497 (org-agenda-mark-header-line (point-min))
3498 (when rtnall
3499 (insert (org-finalize-agenda-entries rtnall) "\n"))
3500 (goto-char (point-min))
3501 (or org-agenda-multi (org-fit-agenda-window))
3502 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
3503 (org-finalize-agenda)
3504 (setq buffer-read-only t)))
3506 ;;; Agenda Finding stuck projects
3508 (defvar org-agenda-skip-regexp nil
3509 "Regular expression used in skipping subtrees for the agenda.
3510 This is basically a temporary global variable that can be set and then
3511 used by user-defined selections using `org-agenda-skip-function'.")
3513 (defvar org-agenda-overriding-header nil
3514 "When this is set during todo and tags searches, will replace header.
3515 This variable should not be set directly, but custom commands can bind it
3516 in the options section.")
3518 (defun org-agenda-skip-entry-when-regexp-matches ()
3519 "Checks if the current entry contains match for `org-agenda-skip-regexp'.
3520 If yes, it returns the end position of this entry, causing agenda commands
3521 to skip the entry but continuing the search in the subtree. This is a
3522 function that can be put into `org-agenda-skip-function' for the duration
3523 of a command."
3524 (let ((end (save-excursion (org-end-of-subtree t)))
3525 skip)
3526 (save-excursion
3527 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3528 (and skip end)))
3530 (defun org-agenda-skip-subtree-when-regexp-matches ()
3531 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3532 If yes, it returns the end position of this tree, causing agenda commands
3533 to skip this subtree. This is a function that can be put into
3534 `org-agenda-skip-function' for the duration of a command."
3535 (let ((end (save-excursion (org-end-of-subtree t)))
3536 skip)
3537 (save-excursion
3538 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3539 (and skip end)))
3541 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
3542 "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
3543 If yes, it returns the end position of the current entry (NOT the tree),
3544 causing agenda commands to skip the entry but continuing the search in
3545 the subtree. This is a function that can be put into
3546 `org-agenda-skip-function' for the duration of a command. An important
3547 use of this function is for the stuck project list."
3548 (let ((end (save-excursion (org-end-of-subtree t)))
3549 (entry-end (save-excursion (outline-next-heading) (1- (point))))
3550 skip)
3551 (save-excursion
3552 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
3553 (and skip entry-end)))
3555 (defun org-agenda-skip-entry-if (&rest conditions)
3556 "Skip entry if any of CONDITIONS is true.
3557 See `org-agenda-skip-if' for details."
3558 (org-agenda-skip-if nil conditions))
3560 (defun org-agenda-skip-subtree-if (&rest conditions)
3561 "Skip entry if any of CONDITIONS is true.
3562 See `org-agenda-skip-if' for details."
3563 (org-agenda-skip-if t conditions))
3565 (defun org-agenda-skip-if (subtree conditions)
3566 "Checks current entity for CONDITIONS.
3567 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
3568 the entry, i.e. the text before the next heading is checked.
3570 CONDITIONS is a list of symbols, boolean OR is used to combine the results
3571 from different tests. Valid conditions are:
3573 scheduled Check if there is a scheduled cookie
3574 notscheduled Check if there is no scheduled cookie
3575 deadline Check if there is a deadline
3576 notdeadline Check if there is no deadline
3577 timestamp Check if there is a timestamp (also deadline or scheduled)
3578 nottimestamp Check if there is no timestamp (also deadline or scheduled)
3579 regexp Check if regexp matches
3580 notregexp Check if regexp does not match.
3582 The regexp is taken from the conditions list, it must come right after
3583 the `regexp' or `notregexp' element.
3585 If any of these conditions is met, this function returns the end point of
3586 the entity, causing the search to continue from there. This is a function
3587 that can be put into `org-agenda-skip-function' for the duration of a command."
3588 (let (beg end m)
3589 (org-back-to-heading t)
3590 (setq beg (point)
3591 end (if subtree
3592 (progn (org-end-of-subtree t) (point))
3593 (progn (outline-next-heading) (1- (point)))))
3594 (goto-char beg)
3595 (and
3597 (and (memq 'scheduled conditions)
3598 (re-search-forward org-scheduled-time-regexp end t))
3599 (and (memq 'notscheduled conditions)
3600 (not (re-search-forward org-scheduled-time-regexp end t)))
3601 (and (memq 'deadline conditions)
3602 (re-search-forward org-deadline-time-regexp end t))
3603 (and (memq 'notdeadline conditions)
3604 (not (re-search-forward org-deadline-time-regexp end t)))
3605 (and (memq 'timestamp conditions)
3606 (re-search-forward org-ts-regexp end t))
3607 (and (memq 'nottimestamp conditions)
3608 (not (re-search-forward org-ts-regexp end t)))
3609 (and (setq m (memq 'regexp conditions))
3610 (stringp (nth 1 m))
3611 (re-search-forward (nth 1 m) end t))
3612 (and (setq m (memq 'notregexp conditions))
3613 (stringp (nth 1 m))
3614 (not (re-search-forward (nth 1 m) end t))))
3615 end)))
3617 ;;;###autoload
3618 (defun org-agenda-list-stuck-projects (&rest ignore)
3619 "Create agenda view for projects that are stuck.
3620 Stuck projects are project that have no next actions. For the definitions
3621 of what a project is and how to check if it stuck, customize the variable
3622 `org-stuck-projects'.
3623 MATCH is being ignored."
3624 (interactive)
3625 (let* ((org-agenda-skip-function
3626 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
3627 ;; We could have used org-agenda-skip-if here.
3628 (org-agenda-overriding-header
3629 (or org-agenda-overriding-header "List of stuck projects: "))
3630 (matcher (nth 0 org-stuck-projects))
3631 (todo (nth 1 org-stuck-projects))
3632 (todo-wds (if (member "*" todo)
3633 (progn
3634 (org-prepare-agenda-buffers (org-agenda-files
3635 nil 'ifmode))
3636 (org-delete-all
3637 org-done-keywords-for-agenda
3638 (copy-sequence org-todo-keywords-for-agenda)))
3639 todo))
3640 (todo-re (concat "^\\*+[ \t]+\\("
3641 (mapconcat 'identity todo-wds "\\|")
3642 "\\)\\>"))
3643 (tags (nth 2 org-stuck-projects))
3644 (tags-re (if (member "*" tags)
3645 (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
3646 (if tags
3647 (concat "^\\*+ .*:\\("
3648 (mapconcat 'identity tags "\\|")
3649 (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
3650 (gen-re (nth 3 org-stuck-projects))
3651 (re-list
3652 (delq nil
3653 (list
3654 (if todo todo-re)
3655 (if tags tags-re)
3656 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
3657 gen-re)))))
3658 (setq org-agenda-skip-regexp
3659 (if re-list
3660 (mapconcat 'identity re-list "\\|")
3661 (error "No information how to identify unstuck projects")))
3662 (org-tags-view nil matcher)
3663 (with-current-buffer org-agenda-buffer-name
3664 (setq org-agenda-redo-command
3665 '(org-agenda-list-stuck-projects
3666 (or current-prefix-arg org-last-arg))))))
3668 ;;; Diary integration
3670 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
3671 (defvar list-diary-entries-hook)
3673 (defun org-get-entries-from-diary (date)
3674 "Get the (Emacs Calendar) diary entries for DATE."
3675 (require 'diary-lib)
3676 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
3677 (fancy-diary-buffer diary-fancy-buffer)
3678 (diary-display-hook '(fancy-diary-display))
3679 (diary-display-function 'fancy-diary-display)
3680 (pop-up-frames nil)
3681 (list-diary-entries-hook
3682 (cons 'org-diary-default-entry list-diary-entries-hook))
3683 (diary-file-name-prefix-function nil) ; turn this feature off
3684 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
3685 entries
3686 (org-disable-agenda-to-diary t))
3687 (save-excursion
3688 (save-window-excursion
3689 (funcall (if (fboundp 'diary-list-entries)
3690 'diary-list-entries 'list-diary-entries)
3691 date 1)))
3692 (if (not (get-buffer diary-fancy-buffer))
3693 (setq entries nil)
3694 (with-current-buffer diary-fancy-buffer
3695 (setq buffer-read-only nil)
3696 (if (zerop (buffer-size))
3697 ;; No entries
3698 (setq entries nil)
3699 ;; Omit the date and other unnecessary stuff
3700 (org-agenda-cleanup-fancy-diary)
3701 ;; Add prefix to each line and extend the text properties
3702 (if (zerop (buffer-size))
3703 (setq entries nil)
3704 (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
3705 (set-buffer-modified-p nil)
3706 (kill-buffer diary-fancy-buffer)))
3707 (when entries
3708 (setq entries (org-split-string entries "\n"))
3709 (setq entries
3710 (mapcar
3711 (lambda (x)
3712 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
3713 ;; Extend the text properties to the beginning of the line
3714 (org-add-props x (text-properties-at (1- (length x)) x)
3715 'type "diary" 'date date))
3716 entries)))))
3718 (defvar org-agenda-cleanup-fancy-diary-hook nil
3719 "Hook run when the fancy diary buffer is cleaned up.")
3721 (defun org-agenda-cleanup-fancy-diary ()
3722 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
3723 This gets rid of the date, the underline under the date, and
3724 the dummy entry installed by `org-mode' to ensure non-empty diary for each
3725 date. It also removes lines that contain only whitespace."
3726 (goto-char (point-min))
3727 (if (looking-at ".*?:[ \t]*")
3728 (progn
3729 (replace-match "")
3730 (re-search-forward "\n=+$" nil t)
3731 (replace-match "")
3732 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
3733 (re-search-forward "\n=+$" nil t)
3734 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
3735 (goto-char (point-min))
3736 (while (re-search-forward "^ +\n" nil t)
3737 (replace-match ""))
3738 (goto-char (point-min))
3739 (if (re-search-forward "^Org-mode dummy\n?" nil t)
3740 (replace-match ""))
3741 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
3743 ;; Make sure entries from the diary have the right text properties.
3744 (eval-after-load "diary-lib"
3745 '(if (boundp 'diary-modify-entry-list-string-function)
3746 ;; We can rely on the hook, nothing to do
3748 ;; Hook not available, must use advice to make this work
3749 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
3750 "Make the position visible."
3751 (if (and org-disable-agenda-to-diary ;; called from org-agenda
3752 (stringp string)
3753 buffer-file-name)
3754 (setq string (org-modify-diary-entry-string string))))))
3756 (defun org-modify-diary-entry-string (string)
3757 "Add text properties to string, allowing org-mode to act on it."
3758 (org-add-props string nil
3759 'mouse-face 'highlight
3760 'keymap org-agenda-keymap
3761 'help-echo (if buffer-file-name
3762 (format "mouse-2 or RET jump to diary file %s"
3763 (abbreviate-file-name buffer-file-name))
3765 'org-agenda-diary-link t
3766 'org-marker (org-agenda-new-marker (point-at-bol))))
3768 (defun org-diary-default-entry ()
3769 "Add a dummy entry to the diary.
3770 Needed to avoid empty dates which mess up holiday display."
3771 ;; Catch the error if dealing with the new add-to-diary-alist
3772 (when org-disable-agenda-to-diary
3773 (condition-case nil
3774 (org-add-to-diary-list original-date "Org-mode dummy" "")
3775 (error
3776 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
3778 (defun org-add-to-diary-list (&rest args)
3779 (if (fboundp 'diary-add-to-list)
3780 (apply 'diary-add-to-list args)
3781 (apply 'add-to-diary-list args)))
3783 ;;;###autoload
3784 (defun org-diary (&rest args)
3785 "Return diary information from org-files.
3786 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
3787 It accesses org files and extracts information from those files to be
3788 listed in the diary. The function accepts arguments specifying what
3789 items should be listed. The following arguments are allowed:
3791 :timestamp List the headlines of items containing a date stamp or
3792 date range matching the selected date. Deadlines will
3793 also be listed, on the expiration day.
3795 :sexp List entries resulting from diary-like sexps.
3797 :deadline List any deadlines past due, or due within
3798 `org-deadline-warning-days'. The listing occurs only
3799 in the diary for *today*, not at any other date. If
3800 an entry is marked DONE, it is no longer listed.
3802 :scheduled List all items which are scheduled for the given date.
3803 The diary for *today* also contains items which were
3804 scheduled earlier and are not yet marked DONE.
3806 :todo List all TODO items from the org-file. This may be a
3807 long list - so this is not turned on by default.
3808 Like deadlines, these entries only show up in the
3809 diary for *today*, not at any other date.
3811 The call in the diary file should look like this:
3813 &%%(org-diary) ~/path/to/some/orgfile.org
3815 Use a separate line for each org file to check. Or, if you omit the file name,
3816 all files listed in `org-agenda-files' will be checked automatically:
3818 &%%(org-diary)
3820 If you don't give any arguments (as in the example above), the default
3821 arguments (:deadline :scheduled :timestamp :sexp) are used.
3822 So the example above may also be written as
3824 &%%(org-diary :deadline :timestamp :sexp :scheduled)
3826 The function expects the lisp variables `entry' and `date' to be provided
3827 by the caller, because this is how the calendar works. Don't use this
3828 function from a program - use `org-agenda-get-day-entries' instead."
3829 (when (> (- (org-float-time)
3830 org-agenda-last-marker-time)
3832 (org-agenda-reset-markers))
3833 (org-compile-prefix-format 'agenda)
3834 (org-set-sorting-strategy 'agenda)
3835 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3836 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
3837 (list entry)
3838 (org-agenda-files t)))
3839 file rtn results)
3840 (org-prepare-agenda-buffers files)
3841 ;; If this is called during org-agenda, don't return any entries to
3842 ;; the calendar. Org Agenda will list these entries itself.
3843 (if org-disable-agenda-to-diary (setq files nil))
3844 (while (setq file (pop files))
3845 (setq rtn (apply 'org-agenda-get-day-entries file date args))
3846 (setq results (append results rtn)))
3847 (if results
3848 (concat (org-finalize-agenda-entries results) "\n"))))
3850 ;;; Agenda entry finders
3852 (defun org-agenda-get-day-entries (file date &rest args)
3853 "Does the work for `org-diary' and `org-agenda'.
3854 FILE is the path to a file to be checked for entries. DATE is date like
3855 the one returned by `calendar-current-date'. ARGS are symbols indicating
3856 which kind of entries should be extracted. For details about these, see
3857 the documentation of `org-diary'."
3858 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
3859 (let* ((org-startup-folded nil)
3860 (org-startup-align-all-tables nil)
3861 (buffer (if (file-exists-p file)
3862 (org-get-agenda-file-buffer file)
3863 (error "No such file %s" file)))
3864 arg results rtn deadline-results)
3865 (if (not buffer)
3866 ;; If file does not exist, make sure an error message ends up in diary
3867 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
3868 (with-current-buffer buffer
3869 (unless (org-mode-p)
3870 (error "Agenda file %s is not in `org-mode'" file))
3871 (let ((case-fold-search nil))
3872 (save-excursion
3873 (save-restriction
3874 (if org-agenda-restrict
3875 (narrow-to-region org-agenda-restrict-begin
3876 org-agenda-restrict-end)
3877 (widen))
3878 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
3879 (while (setq arg (pop args))
3880 (cond
3881 ((and (eq arg :todo)
3882 (equal date (calendar-current-date)))
3883 (setq rtn (org-agenda-get-todos))
3884 (setq results (append results rtn)))
3885 ((eq arg :timestamp)
3886 (setq rtn (org-agenda-get-blocks))
3887 (setq results (append results rtn))
3888 (setq rtn (org-agenda-get-timestamps))
3889 (setq results (append results rtn)))
3890 ((eq arg :sexp)
3891 (setq rtn (org-agenda-get-sexps))
3892 (setq results (append results rtn)))
3893 ((eq arg :scheduled)
3894 (setq rtn (org-agenda-get-scheduled deadline-results))
3895 (setq results (append results rtn)))
3896 ((eq arg :closed)
3897 (setq rtn (org-agenda-get-progress))
3898 (setq results (append results rtn)))
3899 ((eq arg :deadline)
3900 (setq rtn (org-agenda-get-deadlines))
3901 (setq deadline-results (copy-sequence rtn))
3902 (setq results (append results rtn))))))))
3903 results))))
3905 (defun org-agenda-get-todos ()
3906 "Return the TODO information for agenda display."
3907 (let* ((props (list 'face nil
3908 'done-face 'org-agenda-done
3909 'org-not-done-regexp org-not-done-regexp
3910 'org-todo-regexp org-todo-regexp
3911 'org-complex-heading-regexp org-complex-heading-regexp
3912 'mouse-face 'highlight
3913 'keymap org-agenda-keymap
3914 'help-echo
3915 (format "mouse-2 or RET jump to org file %s"
3916 (abbreviate-file-name buffer-file-name))))
3917 (regexp (concat "^\\*+[ \t]+\\("
3918 (if org-select-this-todo-keyword
3919 (if (equal org-select-this-todo-keyword "*")
3920 org-todo-regexp
3921 (concat "\\<\\("
3922 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
3923 "\\)\\>"))
3924 org-not-done-regexp)
3925 "[^\n\r]*\\)"))
3926 marker priority category tags todo-state
3927 ee txt beg end)
3928 (goto-char (point-min))
3929 (while (re-search-forward regexp nil t)
3930 (catch :skip
3931 (save-match-data
3932 (beginning-of-line)
3933 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
3934 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
3935 (goto-char (1+ beg))
3936 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
3937 (throw :skip nil)))
3938 (goto-char beg)
3939 (org-agenda-skip)
3940 (goto-char (match-beginning 1))
3941 (setq marker (org-agenda-new-marker (match-beginning 0))
3942 category (org-get-category)
3943 txt (match-string 1)
3944 tags (org-get-tags-at (point))
3945 txt (org-format-agenda-item "" txt category tags)
3946 priority (1+ (org-get-priority txt))
3947 todo-state (org-get-todo-state))
3948 (org-add-props txt props
3949 'org-marker marker 'org-hd-marker marker
3950 'priority priority 'org-category category
3951 'type "todo" 'todo-state todo-state)
3952 (push txt ee)
3953 (if org-agenda-todo-list-sublevels
3954 (goto-char (match-end 1))
3955 (org-end-of-subtree 'invisible))))
3956 (nreverse ee)))
3958 ;;;###autoload
3959 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
3960 "Do we have a reason to ignore this todo entry because it has a time stamp?"
3961 (when (or org-agenda-todo-ignore-with-date
3962 org-agenda-todo-ignore-scheduled
3963 org-agenda-todo-ignore-deadlines)
3964 (setq end (or end (save-excursion (outline-next-heading) (point))))
3965 (save-excursion
3966 (or (and org-agenda-todo-ignore-with-date
3967 (re-search-forward org-ts-regexp end t))
3968 (and org-agenda-todo-ignore-scheduled
3969 (re-search-forward org-scheduled-time-regexp end t))
3970 (and org-agenda-todo-ignore-deadlines
3971 (re-search-forward org-deadline-time-regexp end t)
3972 (org-deadline-close (match-string 1)))))))
3974 (defconst org-agenda-no-heading-message
3975 "No heading for this item in buffer or region.")
3977 (defun org-agenda-get-timestamps ()
3978 "Return the date stamp information for agenda display."
3979 (let* ((props (list 'face nil
3980 'org-not-done-regexp org-not-done-regexp
3981 'org-todo-regexp org-todo-regexp
3982 'org-complex-heading-regexp org-complex-heading-regexp
3983 'mouse-face 'highlight
3984 'keymap org-agenda-keymap
3985 'help-echo
3986 (format "mouse-2 or RET jump to org file %s"
3987 (abbreviate-file-name buffer-file-name))))
3988 (d1 (calendar-absolute-from-gregorian date))
3989 (remove-re
3990 (concat
3991 (regexp-quote
3992 (format-time-string
3993 "<%Y-%m-%d"
3994 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
3995 ".*?>"))
3996 (regexp
3997 (concat
3998 (if org-agenda-include-inactive-timestamps "[[<]" "<")
3999 (regexp-quote
4000 (substring
4001 (format-time-string
4002 (car org-time-stamp-formats)
4003 (apply 'encode-time ; DATE bound by calendar
4004 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4005 1 11))
4006 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4007 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4008 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4009 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4010 todo-state end-of-match)
4011 (goto-char (point-min))
4012 (while (setq end-of-match (re-search-forward regexp nil t))
4013 (setq b0 (match-beginning 0)
4014 b3 (match-beginning 3) e3 (match-end 3))
4015 (catch :skip
4016 (and (org-at-date-range-p) (throw :skip nil))
4017 (org-agenda-skip)
4018 (if (and (match-end 1)
4019 (not (= d1 (org-time-string-to-absolute
4020 (match-string 1) d1 nil
4021 org-agenda-repeating-timestamp-show-all))))
4022 (throw :skip nil))
4023 (if (and e3
4024 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4025 (throw :skip nil))
4026 (setq tmp (buffer-substring (max (point-min)
4027 (- b0 org-ds-keyword-length))
4029 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4030 inactivep (= (char-after b0) ?\[)
4031 deadlinep (string-match org-deadline-regexp tmp)
4032 scheduledp (string-match org-scheduled-regexp tmp)
4033 closedp (and org-agenda-include-inactive-timestamps
4034 (string-match org-closed-string tmp))
4035 clockp (and org-agenda-include-inactive-timestamps
4036 (or (string-match org-clock-string tmp)
4037 (string-match "]-+\\'" tmp)))
4038 todo-state (org-get-todo-state)
4039 donep (member todo-state org-done-keywords))
4040 (if (or scheduledp deadlinep closedp clockp
4041 (and donep org-agenda-skip-timestamp-if-done))
4042 (throw :skip t))
4043 (if (string-match ">" timestr)
4044 ;; substring should only run to end of time stamp
4045 (setq timestr (substring timestr 0 (match-end 0))))
4046 (setq marker (org-agenda-new-marker b0)
4047 category (org-get-category b0))
4048 (save-excursion
4049 (if (not (re-search-backward "^\\*+ " nil t))
4050 (setq txt org-agenda-no-heading-message)
4051 (goto-char (match-beginning 0))
4052 (setq hdmarker (org-agenda-new-marker)
4053 tags (org-get-tags-at))
4054 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4055 (setq head (match-string 1))
4056 (setq txt (org-format-agenda-item
4057 (if inactivep "[" nil)
4058 head category tags timestr nil
4059 remove-re)))
4060 (setq priority (org-get-priority txt))
4061 (org-add-props txt props
4062 'org-marker marker 'org-hd-marker hdmarker)
4063 (org-add-props txt nil 'priority priority
4064 'org-category category 'date date
4065 'todo-state todo-state
4066 'type "timestamp")
4067 (push txt ee))
4068 (if org-agenda-skip-additional-timestamps-same-entry
4069 (outline-next-heading)
4070 (goto-char end-of-match))))
4071 (nreverse ee)))
4073 (defun org-agenda-get-sexps ()
4074 "Return the sexp information for agenda display."
4075 (require 'diary-lib)
4076 (let* ((props (list 'face nil
4077 'mouse-face 'highlight
4078 'keymap org-agenda-keymap
4079 'help-echo
4080 (format "mouse-2 or RET jump to org file %s"
4081 (abbreviate-file-name buffer-file-name))))
4082 (regexp "^&?%%(")
4083 marker category ee txt tags entry result beg b sexp sexp-entry
4084 todo-state)
4085 (goto-char (point-min))
4086 (while (re-search-forward regexp nil t)
4087 (catch :skip
4088 (org-agenda-skip)
4089 (setq beg (match-beginning 0))
4090 (goto-char (1- (match-end 0)))
4091 (setq b (point))
4092 (forward-sexp 1)
4093 (setq sexp (buffer-substring b (point)))
4094 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4095 (org-trim (match-string 1))
4096 ""))
4097 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4098 (when result
4099 (setq marker (org-agenda-new-marker beg)
4100 category (org-get-category beg)
4101 todo-state (org-get-todo-state))
4103 (if (string-match "\\S-" result)
4104 (setq txt result)
4105 (setq txt "SEXP entry returned empty string"))
4107 (setq txt (org-format-agenda-item
4108 "" txt category tags 'time))
4109 (org-add-props txt props 'org-marker marker)
4110 (org-add-props txt nil
4111 'org-category category 'date date 'todo-state todo-state
4112 'type "sexp")
4113 (push txt ee))))
4114 (nreverse ee)))
4116 (defalias 'org-get-closed 'org-agenda-get-progress)
4117 (defun org-agenda-get-progress ()
4118 "Return the logged TODO entries for agenda display."
4119 (let* ((props (list 'mouse-face 'highlight
4120 'org-not-done-regexp org-not-done-regexp
4121 'org-todo-regexp org-todo-regexp
4122 'org-complex-heading-regexp org-complex-heading-regexp
4123 'keymap org-agenda-keymap
4124 'help-echo
4125 (format "mouse-2 or RET jump to org file %s"
4126 (abbreviate-file-name buffer-file-name))))
4127 (items (if (consp org-agenda-show-log)
4128 org-agenda-show-log
4129 org-agenda-log-mode-items))
4130 (parts
4131 (delq nil
4132 (list
4133 (if (memq 'closed items) (concat "\\<" org-closed-string))
4134 (if (memq 'clock items) (concat "\\<" org-clock-string))
4135 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4136 (parts-re (if parts (mapconcat 'identity parts "\\|")
4137 (error "`org-agenda-log-mode-items' is empty")))
4138 (regexp (concat
4139 "\\(" parts-re "\\)"
4140 " *\\["
4141 (regexp-quote
4142 (substring
4143 (format-time-string
4144 (car org-time-stamp-formats)
4145 (apply 'encode-time ; DATE bound by calendar
4146 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4147 1 11))))
4148 (org-agenda-search-headline-for-time nil)
4149 marker hdmarker priority category tags closedp statep clockp state
4150 ee txt extra timestr rest clocked)
4151 (goto-char (point-min))
4152 (while (re-search-forward regexp nil t)
4153 (catch :skip
4154 (org-agenda-skip)
4155 (setq marker (org-agenda-new-marker (match-beginning 0))
4156 closedp (equal (match-string 1) org-closed-string)
4157 statep (equal (string-to-char (match-string 1)) ?-)
4158 clockp (not (or closedp statep))
4159 state (and statep (match-string 2))
4160 category (org-get-category (match-beginning 0))
4161 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4163 (when (string-match "\\]" timestr)
4164 ;; substring should only run to end of time stamp
4165 (setq rest (substring timestr (match-end 0))
4166 timestr (substring timestr 0 (match-end 0)))
4167 (if (and (not closedp) (not statep)
4168 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4169 (progn (setq timestr (concat (substring timestr 0 -1)
4170 "-" (match-string 1 rest) "]"))
4171 (setq clocked (match-string 2 rest)))
4172 (setq clocked "-")))
4173 (save-excursion
4174 (cond
4175 ((not org-agenda-log-mode-add-notes) (setq extra nil))
4176 (statep
4177 (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4178 (setq extra (match-string 1))))
4179 (clockp
4180 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4181 (setq extra (match-string 1))))
4182 (t (setq extra nil)))
4183 (if (not (re-search-backward "^\\*+ " nil t))
4184 (setq txt org-agenda-no-heading-message)
4185 (goto-char (match-beginning 0))
4186 (setq hdmarker (org-agenda-new-marker)
4187 tags (org-get-tags-at))
4188 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4189 (setq txt (match-string 1))
4190 (when extra
4191 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4192 (setq txt (concat (substring txt 0 (match-beginning 1))
4193 " - " extra " " (match-string 2 txt)))
4194 (setq txt (concat txt " - " extra))))
4195 (setq txt (org-format-agenda-item
4196 (cond
4197 (closedp "Closed: ")
4198 (statep (concat "State: (" state ")"))
4199 (t (concat "Clocked: (" clocked ")")))
4200 txt category tags timestr)))
4201 (setq priority 100000)
4202 (org-add-props txt props
4203 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4204 'priority priority 'org-category category
4205 'type "closed" 'date date
4206 'undone-face 'org-warning 'done-face 'org-agenda-done)
4207 (push txt ee))
4208 (goto-char (point-at-eol))))
4209 (nreverse ee)))
4211 (defun org-agenda-get-deadlines ()
4212 "Return the deadline information for agenda display."
4213 (let* ((props (list 'mouse-face 'highlight
4214 'org-not-done-regexp org-not-done-regexp
4215 'org-todo-regexp org-todo-regexp
4216 'org-complex-heading-regexp org-complex-heading-regexp
4217 'keymap org-agenda-keymap
4218 'help-echo
4219 (format "mouse-2 or RET jump to org file %s"
4220 (abbreviate-file-name buffer-file-name))))
4221 (regexp org-deadline-time-regexp)
4222 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4223 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4224 d2 diff dfrac wdays pos pos1 category tags
4225 ee txt head face s todo-state upcomingp donep timestr)
4226 (goto-char (point-min))
4227 (while (re-search-forward regexp nil t)
4228 (catch :skip
4229 (org-agenda-skip)
4230 (setq s (match-string 1)
4231 txt nil
4232 pos (1- (match-beginning 1))
4233 d2 (org-time-string-to-absolute
4234 (match-string 1) d1 'past
4235 org-agenda-repeating-timestamp-show-all)
4236 diff (- d2 d1)
4237 wdays (org-get-wdays s)
4238 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
4239 upcomingp (and todayp (> diff 0)))
4240 ;; When to show a deadline in the calendar:
4241 ;; If the expiration is within wdays warning time.
4242 ;; Past-due deadlines are only shown on the current date
4243 (if (or (and (<= diff wdays)
4244 (and todayp (not org-agenda-only-exact-dates)))
4245 (= diff 0))
4246 (save-excursion
4247 (setq todo-state (org-get-todo-state))
4248 (setq donep (member todo-state org-done-keywords))
4249 (if (and donep
4250 (or org-agenda-skip-deadline-if-done
4251 (not (= diff 0))))
4252 (setq txt nil)
4253 (setq category (org-get-category))
4254 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4255 (setq txt org-agenda-no-heading-message)
4256 (goto-char (match-end 0))
4257 (setq pos1 (match-beginning 0))
4258 (setq tags (org-get-tags-at pos1))
4259 (setq head (buffer-substring-no-properties
4260 (point)
4261 (progn (skip-chars-forward "^\r\n")
4262 (point))))
4263 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4264 (setq timestr
4265 (concat (substring s (match-beginning 1)) " "))
4266 (setq timestr 'time))
4267 (setq txt (org-format-agenda-item
4268 (if (= diff 0)
4269 (car org-agenda-deadline-leaders)
4270 (if (functionp
4271 (nth 1 org-agenda-deadline-leaders))
4272 (funcall
4273 (nth 1 org-agenda-deadline-leaders)
4274 diff date)
4275 (format (nth 1 org-agenda-deadline-leaders)
4276 diff)))
4277 head category tags
4278 (if (not (= diff 0)) nil timestr)))))
4279 (when txt
4280 (setq face (org-agenda-deadline-face dfrac wdays))
4281 (org-add-props txt props
4282 'org-marker (org-agenda-new-marker pos)
4283 'org-hd-marker (org-agenda-new-marker pos1)
4284 'priority (+ (- diff)
4285 (org-get-priority txt))
4286 'org-category category
4287 'todo-state todo-state
4288 'type (if upcomingp "upcoming-deadline" "deadline")
4289 'date (if upcomingp date d2)
4290 'face (if donep 'org-agenda-done face)
4291 'undone-face face 'done-face 'org-agenda-done)
4292 (push txt ee))))))
4293 (nreverse ee)))
4295 (defun org-agenda-deadline-face (fraction &optional wdays)
4296 "Return the face to displaying a deadline item.
4297 FRACTION is what fraction of the head-warning time has passed."
4298 (if (equal wdays 0) (setq fraction 1.))
4299 (let ((faces org-agenda-deadline-faces) f)
4300 (catch 'exit
4301 (while (setq f (pop faces))
4302 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
4304 (defun org-agenda-get-scheduled (&optional deadline-results)
4305 "Return the scheduled information for agenda display."
4306 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
4307 'org-todo-regexp org-todo-regexp
4308 'org-complex-heading-regexp org-complex-heading-regexp
4309 'done-face 'org-agenda-done
4310 'mouse-face 'highlight
4311 'keymap org-agenda-keymap
4312 'help-echo
4313 (format "mouse-2 or RET jump to org file %s"
4314 (abbreviate-file-name buffer-file-name))))
4315 (regexp org-scheduled-time-regexp)
4316 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
4317 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
4319 (deadline-position-alist
4320 (mapcar (lambda (a) (and (setq mm (get-text-property
4321 0 'org-hd-marker a))
4322 (cons (marker-position mm) a)))
4323 deadline-results))
4324 d2 diff pos pos1 category tags donep
4325 ee txt head pastschedp todo-state face timestr s)
4326 (goto-char (point-min))
4327 (while (re-search-forward regexp nil t)
4328 (catch :skip
4329 (org-agenda-skip)
4330 (setq s (match-string 1)
4331 txt nil
4332 pos (1- (match-beginning 1))
4333 d2 (org-time-string-to-absolute
4334 (match-string 1) d1 'past
4335 org-agenda-repeating-timestamp-show-all)
4336 diff (- d2 d1))
4337 (setq pastschedp (and todayp (< diff 0)))
4338 ;; When to show a scheduled item in the calendar:
4339 ;; If it is on or past the date.
4340 (if (or (and (< diff 0)
4341 (< (abs diff) org-scheduled-past-days)
4342 (and todayp (not org-agenda-only-exact-dates)))
4343 (= diff 0))
4344 (save-excursion
4345 (setq todo-state (org-get-todo-state))
4346 (setq donep (member todo-state org-done-keywords))
4347 (if (and donep
4348 (or org-agenda-skip-scheduled-if-done
4349 (not (= diff 0))))
4350 (setq txt nil)
4351 (setq category (org-get-category))
4352 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
4353 (setq txt org-agenda-no-heading-message)
4354 (goto-char (match-end 0))
4355 (setq pos1 (match-beginning 0))
4356 (if (and
4357 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
4358 (and org-agenda-skip-scheduled-if-deadline-is-shown
4359 pastschedp))
4360 (setq mm (assoc pos1 deadline-position-alist)))
4361 (throw :skip nil))
4362 (setq tags (org-get-tags-at))
4363 (setq head (buffer-substring-no-properties
4364 (point)
4365 (progn (skip-chars-forward "^\r\n") (point))))
4366 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
4367 (setq timestr
4368 (concat (substring s (match-beginning 1)) " "))
4369 (setq timestr 'time))
4370 (setq txt (org-format-agenda-item
4371 (if (= diff 0)
4372 (car org-agenda-scheduled-leaders)
4373 (format (nth 1 org-agenda-scheduled-leaders)
4374 (- 1 diff)))
4375 head category tags
4376 (if (not (= diff 0)) nil timestr)))))
4377 (when txt
4378 (setq face
4379 (cond
4380 (pastschedp 'org-scheduled-previously)
4381 (todayp 'org-scheduled-today)
4382 (t 'org-scheduled)))
4383 (org-add-props txt props
4384 'undone-face face
4385 'face (if donep 'org-agenda-done face)
4386 'org-marker (org-agenda-new-marker pos)
4387 'org-hd-marker (org-agenda-new-marker pos1)
4388 'type (if pastschedp "past-scheduled" "scheduled")
4389 'date (if pastschedp d2 date)
4390 'priority (+ 94 (- 5 diff) (org-get-priority txt))
4391 'org-category category
4392 'todo-state todo-state)
4393 (push txt ee))))))
4394 (nreverse ee)))
4396 (defun org-agenda-get-blocks ()
4397 "Return the date-range information for agenda display."
4398 (let* ((props (list 'face nil
4399 'org-not-done-regexp org-not-done-regexp
4400 'org-todo-regexp org-todo-regexp
4401 'org-complex-heading-regexp org-complex-heading-regexp
4402 'mouse-face 'highlight
4403 'keymap org-agenda-keymap
4404 'help-echo
4405 (format "mouse-2 or RET jump to org file %s"
4406 (abbreviate-file-name buffer-file-name))))
4407 (regexp org-tr-regexp)
4408 (d0 (calendar-absolute-from-gregorian date))
4409 marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
4410 head donep)
4411 (goto-char (point-min))
4412 (while (re-search-forward regexp nil t)
4413 (catch :skip
4414 (org-agenda-skip)
4415 (setq pos (point))
4416 (setq timestr (match-string 0)
4417 s1 (match-string 1)
4418 s2 (match-string 2)
4419 d1 (time-to-days (org-time-string-to-time s1))
4420 d2 (time-to-days (org-time-string-to-time s2)))
4421 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
4422 ;; Only allow days between the limits, because the normal
4423 ;; date stamps will catch the limits.
4424 (save-excursion
4425 (setq todo-state (org-get-todo-state))
4426 (setq donep (member todo-state org-done-keywords))
4427 (if (and donep org-agenda-skip-timestamp-if-done)
4428 (throw :skip t))
4429 (setq marker (org-agenda-new-marker (point)))
4430 (setq category (org-get-category))
4431 (if (not (re-search-backward "^\\*+ " nil t))
4432 (setq txt org-agenda-no-heading-message)
4433 (goto-char (match-beginning 0))
4434 (setq hdmarker (org-agenda-new-marker (point)))
4435 (setq tags (org-get-tags-at))
4436 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4437 (setq head (match-string 1))
4438 (setq txt (org-format-agenda-item
4439 (format
4440 (nth (if (= d1 d2) 0 1)
4441 org-agenda-timerange-leaders)
4442 (1+ (- d0 d1)) (1+ (- d2 d1)))
4443 head category tags
4444 (if (= d0 d1) timestr))))
4445 (org-add-props txt props
4446 'org-marker marker 'org-hd-marker hdmarker
4447 'type "block" 'date date
4448 'todo-state todo-state
4449 'priority (org-get-priority txt) 'org-category category)
4450 (push txt ee)))
4451 (goto-char pos)))
4452 ;; Sort the entries by expiration date.
4453 (nreverse ee)))
4455 ;;; Agenda presentation and sorting
4457 (defvar org-prefix-has-time nil
4458 "A flag, set by `org-compile-prefix-format'.
4459 The flag is set if the currently compiled format contains a `%t'.")
4460 (defvar org-prefix-has-tag nil
4461 "A flag, set by `org-compile-prefix-format'.
4462 The flag is set if the currently compiled format contains a `%T'.")
4463 (defvar org-prefix-has-effort nil
4464 "A flag, set by `org-compile-prefix-format'.
4465 The flag is set if the currently compiled format contains a `%e'.")
4466 (defvar org-prefix-category-length nil
4467 "Used by `org-compile-prefix-format' to remember the category field widh.")
4469 (defun org-format-agenda-item (extra txt &optional category tags dotime
4470 noprefix remove-re)
4471 "Format TXT to be inserted into the agenda buffer.
4472 In particular, it adds the prefix and corresponding text properties. EXTRA
4473 must be a string and replaces the `%s' specifier in the prefix format.
4474 CATEGORY (string, symbol or nil) may be used to overrule the default
4475 category taken from local variable or file name. It will replace the `%c'
4476 specifier in the format. DOTIME, when non-nil, indicates that a
4477 time-of-day should be extracted from TXT for sorting of this entry, and for
4478 the `%t' specifier in the format. When DOTIME is a string, this string is
4479 searched for a time before TXT is. NOPREFIX is a flag and indicates that
4480 only the correctly processes TXT should be returned - this is used by
4481 `org-agenda-change-all-lines'. TAGS can be the tags of the headline.
4482 Any match of REMOVE-RE will be removed from TXT."
4483 (save-match-data
4484 ;; Diary entries sometimes have extra whitespace at the beginning
4485 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
4486 (when org-agenda-show-inherited-tags
4487 ;; Fix the tags part in txt
4488 (setq txt (org-agenda-add-inherited-tags txt tags)))
4489 (let* ((category (or category
4490 org-category
4491 (if buffer-file-name
4492 (file-name-sans-extension
4493 (file-name-nondirectory buffer-file-name))
4494 "")))
4495 ;; time, tag, effort are needed for the eval of the prefix format
4496 (tag (if tags (nth (1- (length tags)) tags) ""))
4497 time effort neffort
4498 (ts (if dotime (concat
4499 (if (stringp dotime) dotime "")
4500 (and org-agenda-search-headline-for-time txt))))
4501 (time-of-day (and dotime (org-get-time-of-day ts)))
4502 stamp plain s0 s1 s2 t1 t2 rtn srp l
4503 duration)
4504 (and (org-mode-p) buffer-file-name
4505 (add-to-list 'org-agenda-contributing-files buffer-file-name))
4506 (when (and dotime time-of-day)
4507 ;; Extract starting and ending time and move them to prefix
4508 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
4509 (setq plain (string-match org-plain-time-of-day-regexp ts)))
4510 (setq s0 (match-string 0 ts)
4511 srp (and stamp (match-end 3))
4512 s1 (match-string (if plain 1 2) ts)
4513 s2 (match-string (if plain 8 (if srp 4 6)) ts))
4515 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
4516 ;; them, we might want to remove them there to avoid duplication.
4517 ;; The user can turn this off with a variable.
4518 (if (and org-prefix-has-time
4519 org-agenda-remove-times-when-in-prefix (or stamp plain)
4520 (string-match (concat (regexp-quote s0) " *") txt)
4521 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
4522 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
4523 (= (match-beginning 0) 0)
4525 (setq txt (replace-match "" nil nil txt))))
4526 ;; Normalize the time(s) to 24 hour
4527 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
4528 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
4529 ;; Compute the duration
4530 (when s1
4531 (setq t1 (+ (* 60 (string-to-number (substring s1 0 2)))
4532 (string-to-number (substring s1 3)))
4533 t2 (cond
4534 (s2 (+ (* 60 (string-to-number (substring s2 0 2)))
4535 (string-to-number (substring s2 3))))
4536 (org-agenda-default-appointment-duration
4537 (+ t1 org-agenda-default-appointment-duration))
4538 (t nil)))
4539 (setq duration (if t2 (- t2 t1)))))
4541 (when (and s1 (not s2) org-agenda-default-appointment-duration
4542 (string-match "\\([0-9]+\\):\\([0-9]+\\)" s1))
4543 (let ((m (+ (string-to-number (match-string 2 s1))
4544 (* 60 (string-to-number (match-string 1 s1)))
4545 org-agenda-default-appointment-duration))
4547 (setq h (/ m 60) m (- m (* h 60)))
4548 (setq s2 (format "%02d:%02d" h m))))
4550 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
4551 txt)
4552 ;; Tags are in the string
4553 (if (or (eq org-agenda-remove-tags t)
4554 (and org-agenda-remove-tags
4555 org-prefix-has-tag))
4556 (setq txt (replace-match "" t t txt))
4557 (setq txt (replace-match
4558 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
4559 (match-string 2 txt))
4560 t t txt))))
4561 (when (org-mode-p)
4562 (setq effort
4563 (condition-case nil
4564 (org-get-effort
4565 (or (get-text-property 0 'org-hd-marker txt)
4566 (get-text-property 0 'org-marker txt)))
4567 (error nil)))
4568 (when effort
4569 (setq neffort (org-hh:mm-string-to-minutes effort)
4570 effort (setq effort (concat "[" effort "]" )))))
4572 (when remove-re
4573 (while (string-match remove-re txt)
4574 (setq txt (replace-match "" t t txt))))
4576 ;; Create the final string
4577 (if noprefix
4578 (setq rtn txt)
4579 ;; Prepare the variables needed in the eval of the compiled format
4580 (setq time (cond (s2 (concat s1 "-" s2))
4581 (s1 (concat s1 "......"))
4582 (t ""))
4583 extra (or extra "")
4584 category (if (symbolp category) (symbol-name category) category))
4585 (when (string-match org-bracket-link-regexp category)
4586 (setq l (if (match-end 3)
4587 (- (match-end 3) (match-beginning 3))
4588 (- (match-end 1) (match-beginning 1))))
4589 (when (< l (or org-prefix-category-length 0))
4590 (setq category (copy-sequence category))
4591 (org-add-props category nil
4592 'extra-space (make-string
4593 (- org-prefix-category-length l 1) ?\ ))))
4594 ;; Evaluate the compiled format
4595 (setq rtn (concat (eval org-prefix-format-compiled) txt)))
4597 ;; And finally add the text properties
4598 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
4599 (org-add-props rtn nil
4600 'org-category (downcase category)
4601 'tags (mapcar 'org-downcase-keep-props tags)
4602 'org-highest-priority org-highest-priority
4603 'org-lowest-priority org-lowest-priority
4604 'prefix-length (- (length rtn) (length txt))
4605 'time-of-day time-of-day
4606 'duration duration
4607 'effort effort
4608 'effort-minutes neffort
4609 'txt txt
4610 'time time
4611 'extra extra
4612 'dotime dotime))))
4614 (defun org-agenda-add-inherited-tags (txt tags)
4615 "Remove tags string from TXT, and add complete list of tags.
4616 The new list includes inherited tags. If any inherited tags are present,
4617 a double colon separates inherited tags from local tags."
4618 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
4619 (setq txt (substring txt 0 (match-beginning 0))))
4620 (when tags
4621 (let ((have-i (get-text-property 0 'inherited (car tags)))
4623 (setq txt (concat txt " :"
4624 (mapconcat
4625 (lambda (x)
4626 (setq i (get-text-property 0 'inherited x))
4627 (if (and have-i (not i))
4628 (progn
4629 (setq have-i nil)
4630 (concat ":" x))
4632 tags ":")
4633 (if have-i "::" ":")))))
4634 txt)
4636 (defun org-downcase-keep-props (s)
4637 (let ((props (text-properties-at 0 s)))
4638 (setq s (downcase s))
4639 (add-text-properties 0 (length s) props s)
4642 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
4643 (defvar org-agenda-sorting-strategy-selected nil)
4645 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
4646 (catch 'exit
4647 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
4648 ((and todayp (member 'today (car org-agenda-time-grid))))
4649 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
4650 ((member 'weekly (car org-agenda-time-grid)))
4651 (t (throw 'exit list)))
4652 (let* ((have (delq nil (mapcar
4653 (lambda (x) (get-text-property 1 'time-of-day x))
4654 list)))
4655 (string (nth 1 org-agenda-time-grid))
4656 (gridtimes (nth 2 org-agenda-time-grid))
4657 (req (car org-agenda-time-grid))
4658 (remove (member 'remove-match req))
4659 new time)
4660 (if (and (member 'require-timed req) (not have))
4661 ;; don't show empty grid
4662 (throw 'exit list))
4663 (while (setq time (pop gridtimes))
4664 (unless (and remove (member time have))
4665 (setq time (int-to-string time))
4666 (push (org-format-agenda-item
4667 nil string "" nil
4668 (concat (substring time 0 -2) ":" (substring time -2)))
4669 new)
4670 (put-text-property
4671 1 (length (car new)) 'face 'org-time-grid (car new))))
4672 (if (member 'time-up org-agenda-sorting-strategy-selected)
4673 (append new list)
4674 (append list new)))))
4676 (defun org-compile-prefix-format (key)
4677 "Compile the prefix format into a Lisp form that can be evaluated.
4678 The resulting form is returned and stored in the variable
4679 `org-prefix-format-compiled'."
4680 (setq org-prefix-has-time nil org-prefix-has-tag nil
4681 org-prefix-category-length nil org-prefix-has-effort nil)
4682 (let ((s (cond
4683 ((stringp org-agenda-prefix-format)
4684 org-agenda-prefix-format)
4685 ((assq key org-agenda-prefix-format)
4686 (cdr (assq key org-agenda-prefix-format)))
4687 (t " %-12:c%?-12t% s")))
4688 (start 0)
4689 varform vars var e c f opt)
4690 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
4691 s start)
4692 (setq var (cdr (assoc (match-string 4 s)
4693 '(("c" . category) ("t" . time) ("s" . extra)
4694 ("T" . tag) ("e" . effort))))
4695 c (or (match-string 3 s) "")
4696 opt (match-beginning 1)
4697 start (1+ (match-beginning 0)))
4698 (if (equal var 'time) (setq org-prefix-has-time t))
4699 (if (equal var 'tag) (setq org-prefix-has-tag t))
4700 (if (equal var 'effort) (setq org-prefix-has-effort t))
4701 (if (equal var 'category)
4702 (setq org-prefix-category-length
4703 (abs (string-to-number (match-string 2 s)))))
4704 (setq f (concat "%" (match-string 2 s) "s"))
4705 (if opt
4706 (setq varform
4707 `(if (equal "" ,var)
4709 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
4710 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
4711 (setq s (replace-match "%s" t nil s))
4712 (push varform vars))
4713 (setq vars (nreverse vars))
4714 (setq org-prefix-format-compiled `(format ,s ,@vars))))
4716 (defun org-set-sorting-strategy (key)
4717 (if (symbolp (car org-agenda-sorting-strategy))
4718 ;; the old format
4719 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
4720 (setq org-agenda-sorting-strategy-selected
4721 (or (cdr (assq key org-agenda-sorting-strategy))
4722 (cdr (assq 'agenda org-agenda-sorting-strategy))
4723 '(time-up category-keep priority-down)))))
4725 (defun org-get-time-of-day (s &optional string mod24)
4726 "Check string S for a time of day.
4727 If found, return it as a military time number between 0 and 2400.
4728 If not found, return nil.
4729 The optional STRING argument forces conversion into a 5 character wide string
4730 HH:MM."
4731 (save-match-data
4732 (when
4733 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
4734 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
4735 (let* ((h (string-to-number (match-string 1 s)))
4736 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
4737 (ampm (if (match-end 4) (downcase (match-string 4 s))))
4738 (am-p (equal ampm "am"))
4739 (h1 (cond ((not ampm) h)
4740 ((= h 12) (if am-p 0 12))
4741 (t (+ h (if am-p 0 12)))))
4742 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
4743 (mod h1 24) h1))
4744 (t0 (+ (* 100 h2) m))
4745 (t1 (concat (if (>= h1 24) "+" " ")
4746 (if (< t0 100) "0" "")
4747 (if (< t0 10) "0" "")
4748 (int-to-string t0))))
4749 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
4751 (defun org-finalize-agenda-entries (list &optional nosort)
4752 "Sort and concatenate the agenda items."
4753 (setq list (mapcar 'org-agenda-highlight-todo list))
4754 (if nosort
4755 list
4756 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
4758 (defun org-agenda-highlight-todo (x)
4759 (let ((org-done-keywords org-done-keywords-for-agenda)
4760 re pl)
4761 (if (eq x 'line)
4762 (save-excursion
4763 (beginning-of-line 1)
4764 (setq re (get-text-property (point) 'org-todo-regexp))
4765 (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
4766 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
4767 (add-text-properties (match-beginning 0) (match-end 1)
4768 (list 'face (org-get-todo-face 1)))
4769 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
4770 (delete-region (match-beginning 1) (1- (match-end 0)))
4771 (goto-char (match-beginning 1))
4772 (insert (format org-agenda-todo-keyword-format s)))))
4773 (setq re (concat (get-text-property 0 'org-todo-regexp x))
4774 pl (get-text-property 0 'prefix-length x))
4775 (when (and re
4776 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
4777 x (or pl 0)) pl))
4778 (add-text-properties
4779 (or (match-end 1) (match-end 0)) (match-end 0)
4780 (list 'face (org-get-todo-face (match-string 2 x)))
4782 (setq x (concat (substring x 0 (match-end 1))
4783 (format org-agenda-todo-keyword-format
4784 (match-string 2 x))
4786 (substring x (match-end 3)))))
4787 x)))
4789 (defsubst org-cmp-priority (a b)
4790 "Compare the priorities of string A and B."
4791 (let ((pa (or (get-text-property 1 'priority a) 0))
4792 (pb (or (get-text-property 1 'priority b) 0)))
4793 (cond ((> pa pb) +1)
4794 ((< pa pb) -1)
4795 (t nil))))
4797 (defsubst org-cmp-effort (a b)
4798 "Compare the priorities of string A and B."
4799 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
4800 (ea (or (get-text-property 1 'effort-minutes a) def))
4801 (eb (or (get-text-property 1 'effort-minutes b) def)))
4802 (cond ((> ea eb) +1)
4803 ((< ea eb) -1)
4804 (t nil))))
4806 (defsubst org-cmp-category (a b)
4807 "Compare the string values of categories of strings A and B."
4808 (let ((ca (or (get-text-property 1 'org-category a) ""))
4809 (cb (or (get-text-property 1 'org-category b) "")))
4810 (cond ((string-lessp ca cb) -1)
4811 ((string-lessp cb ca) +1)
4812 (t nil))))
4814 (defsubst org-cmp-todo-state (a b)
4815 "Compare the todo states of strings A and B."
4816 (let* ((ma (or (get-text-property 1 'org-marker a)
4817 (get-text-property 1 'org-hd-marker a)))
4818 (mb (or (get-text-property 1 'org-marker b)
4819 (get-text-property 1 'org-hd-marker b)))
4820 (fa (and ma (marker-buffer ma)))
4821 (fb (and mb (marker-buffer mb)))
4822 (todo-kwds
4823 (or (and fa (with-current-buffer fa org-todo-keywords-1))
4824 (and fb (with-current-buffer fb org-todo-keywords-1))))
4825 (ta (or (get-text-property 1 'todo-state a) ""))
4826 (tb (or (get-text-property 1 'todo-state b) ""))
4827 (la (- (length (member ta todo-kwds))))
4828 (lb (- (length (member tb todo-kwds))))
4829 (donepa (member ta org-done-keywords-for-agenda))
4830 (donepb (member tb org-done-keywords-for-agenda)))
4831 (cond ((and donepa (not donepb)) -1)
4832 ((and (not donepa) donepb) +1)
4833 ((< la lb) -1)
4834 ((< lb la) +1)
4835 (t nil))))
4837 (defsubst org-cmp-tag (a b)
4838 "Compare the string values of the first tags of A and B."
4839 (let ((ta (car (last (get-text-property 1 'tags a))))
4840 (tb (car (last (get-text-property 1 'tags b)))))
4841 (cond ((not ta) +1)
4842 ((not tb) -1)
4843 ((string-lessp ta tb) -1)
4844 ((string-lessp tb ta) +1)
4845 (t nil))))
4847 (defsubst org-cmp-time (a b)
4848 "Compare the time-of-day values of strings A and B."
4849 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
4850 (ta (or (get-text-property 1 'time-of-day a) def))
4851 (tb (or (get-text-property 1 'time-of-day b) def)))
4852 (cond ((< ta tb) -1)
4853 ((< tb ta) +1)
4854 (t nil))))
4856 (defun org-entries-lessp (a b)
4857 "Predicate for sorting agenda entries."
4858 ;; The following variables will be used when the form is evaluated.
4859 ;; So even though the compiler complains, keep them.
4860 (let* ((time-up (org-cmp-time a b))
4861 (time-down (if time-up (- time-up) nil))
4862 (priority-up (org-cmp-priority a b))
4863 (priority-down (if priority-up (- priority-up) nil))
4864 (effort-up (org-cmp-effort a b))
4865 (effort-down (if effort-up (- effort-up) nil))
4866 (category-up (org-cmp-category a b))
4867 (category-down (if category-up (- category-up) nil))
4868 (category-keep (if category-up +1 nil))
4869 (tag-up (org-cmp-tag a b))
4870 (tag-down (if tag-up (- tag-up) nil))
4871 (todo-state-up (org-cmp-todo-state a b))
4872 (todo-state-down (if todo-state-up (- todo-state-up) nil))
4873 user-defined-up user-defined-down)
4874 (if (and org-agenda-cmp-user-defined
4875 (functionp org-agenda-cmp-user-defined))
4876 (setq user-defined-up
4877 (funcall org-agenda-cmp-user-defined a b)
4878 user-defined-down (if user-defined-up (- user-defined-up) nil)))
4879 (cdr (assoc
4880 (eval (cons 'or org-agenda-sorting-strategy-selected))
4881 '((-1 . t) (1 . nil) (nil . nil))))))
4883 ;;; Agenda restriction lock
4885 (defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
4886 "Overlay to mark the headline to which arenda commands are restricted.")
4887 (org-overlay-put org-agenda-restriction-lock-overlay
4888 'face 'org-agenda-restriction-lock)
4889 (org-overlay-put org-agenda-restriction-lock-overlay
4890 'help-echo "Agendas are currently limited to this subtree.")
4891 (org-detach-overlay org-agenda-restriction-lock-overlay)
4893 (defun org-agenda-set-restriction-lock (&optional type)
4894 "Set restriction lock for agenda, to current subtree or file.
4895 Restriction will be the file if TYPE is `file', or if type is the
4896 universal prefix '(4), or if the cursor is before the first headline
4897 in the file. Otherwise, restriction will be to the current subtree."
4898 (interactive "P")
4899 (and (equal type '(4)) (setq type 'file))
4900 (setq type (cond
4901 (type type)
4902 ((org-at-heading-p) 'subtree)
4903 ((condition-case nil (org-back-to-heading t) (error nil))
4904 'subtree)
4905 (t 'file)))
4906 (if (eq type 'subtree)
4907 (progn
4908 (setq org-agenda-restrict t)
4909 (setq org-agenda-overriding-restriction 'subtree)
4910 (put 'org-agenda-files 'org-restrict
4911 (list (buffer-file-name (buffer-base-buffer))))
4912 (org-back-to-heading t)
4913 (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
4914 (move-marker org-agenda-restrict-begin (point))
4915 (move-marker org-agenda-restrict-end
4916 (save-excursion (org-end-of-subtree t)))
4917 (message "Locking agenda restriction to subtree"))
4918 (put 'org-agenda-files 'org-restrict
4919 (list (buffer-file-name (buffer-base-buffer))))
4920 (setq org-agenda-restrict nil)
4921 (setq org-agenda-overriding-restriction 'file)
4922 (move-marker org-agenda-restrict-begin nil)
4923 (move-marker org-agenda-restrict-end nil)
4924 (message "Locking agenda restriction to file"))
4925 (setq current-prefix-arg nil)
4926 (org-agenda-maybe-redo))
4928 (defun org-agenda-remove-restriction-lock (&optional noupdate)
4929 "Remove the agenda restriction lock."
4930 (interactive "P")
4931 (org-detach-overlay org-agenda-restriction-lock-overlay)
4932 (org-detach-overlay org-speedbar-restriction-lock-overlay)
4933 (setq org-agenda-overriding-restriction nil)
4934 (setq org-agenda-restrict nil)
4935 (put 'org-agenda-files 'org-restrict nil)
4936 (move-marker org-agenda-restrict-begin nil)
4937 (move-marker org-agenda-restrict-end nil)
4938 (setq current-prefix-arg nil)
4939 (message "Agenda restriction lock removed")
4940 (or noupdate (org-agenda-maybe-redo)))
4942 (defun org-agenda-maybe-redo ()
4943 "If there is any window showing the agenda view, update it."
4944 (let ((w (get-buffer-window org-agenda-buffer-name t))
4945 (w0 (selected-window)))
4946 (when w
4947 (select-window w)
4948 (org-agenda-redo)
4949 (select-window w0)
4950 (if org-agenda-overriding-restriction
4951 (message "Agenda view shifted to new %s restriction"
4952 org-agenda-overriding-restriction)
4953 (message "Agenda restriction lock removed")))))
4955 ;;; Agenda commands
4957 (defun org-agenda-check-type (error &rest types)
4958 "Check if agenda buffer is of allowed type.
4959 If ERROR is non-nil, throw an error, otherwise just return nil."
4960 (if (memq org-agenda-type types)
4962 (if error
4963 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
4964 nil)))
4966 (defun org-agenda-quit ()
4967 "Exit agenda by removing the window or the buffer."
4968 (interactive)
4969 (if org-agenda-columns-active
4970 (org-columns-quit)
4971 (if (window-dedicated-p (selected-window)) (delete-other-windows))
4972 (let ((buf (current-buffer)))
4973 (and (not (eq org-agenda-window-setup 'current-window))
4974 (not (one-window-p))
4975 (delete-window))
4976 (kill-buffer buf)
4977 (org-agenda-reset-markers)
4978 (org-columns-remove-overlays)
4979 (setq org-agenda-archives-mode nil))
4980 ;; Maybe restore the pre-agenda window configuration.
4981 (and org-agenda-restore-windows-after-quit
4982 (not (eq org-agenda-window-setup 'other-frame))
4983 org-pre-agenda-window-conf
4984 (set-window-configuration org-pre-agenda-window-conf))))
4986 (defun org-agenda-exit ()
4987 "Exit agenda by removing the window or the buffer.
4988 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
4989 Org-mode buffers visited directly by the user will not be touched."
4990 (interactive)
4991 (org-release-buffers org-agenda-new-buffers)
4992 (setq org-agenda-new-buffers nil)
4993 (org-agenda-quit))
4995 (defun org-agenda-execute (arg)
4996 "Execute another agenda command, keeping same window.\\<global-map>
4997 So this is just a shortcut for `\\[org-agenda]', available in the agenda."
4998 (interactive "P")
4999 (let ((org-agenda-window-setup 'current-window))
5000 (org-agenda arg)))
5002 (defun org-agenda-redo ()
5003 "Rebuild Agenda.
5004 When this is the global TODO list, a prefix argument will be interpreted."
5005 (interactive)
5006 (let* ((org-agenda-keep-modes t)
5007 (filter org-agenda-filter)
5008 (preset (get 'org-agenda-filter :preset-filter))
5009 (cols org-agenda-columns-active)
5010 (line (org-current-line))
5011 (window-line (- line (org-current-line (window-start))))
5012 (lprops (get 'org-agenda-redo-command 'org-lprops)))
5013 (put 'org-agenda-filter :preset-filter nil)
5014 (and cols (org-columns-quit))
5015 (message "Rebuilding agenda buffer...")
5016 (org-let lprops '(eval org-agenda-redo-command))
5017 (setq org-agenda-undo-list nil
5018 org-agenda-pending-undo-list nil)
5019 (message "Rebuilding agenda buffer...done")
5020 (put 'org-agenda-filter :preset-filter preset)
5021 (and (or filter preset) (org-agenda-filter-apply filter))
5022 (and cols (interactive-p) (org-agenda-columns))
5023 (org-goto-line line)
5024 (recenter window-line)))
5027 (defvar org-global-tags-completion-table nil)
5028 (defvar org-agenda-filter-form nil)
5029 (defun org-agenda-filter-by-tag (strip &optional char narrow)
5030 "Keep only those lines in the agenda buffer that have a specific tag.
5031 The tag is selected with its fast selection letter, as configured.
5032 With prefix argument STRIP, remove all lines that do have the tag.
5033 A lisp caller can specify CHAR. NARROW means that the new tag should be
5034 used to narrow the search - the interactive user can also press `-' or `+'
5035 to switch to narrowing."
5036 (interactive "P")
5037 (let* ((alist org-tag-alist-for-agenda)
5038 (tag-chars (mapconcat
5039 (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
5040 alist ""))
5041 (efforts (org-split-string
5042 (or (cdr (assoc (concat org-effort-property "_ALL")
5043 org-global-properties))
5044 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00" "")))
5045 (effort-op org-agenda-filter-effort-default-operator)
5046 (effort-prompt "")
5047 (inhibit-read-only t)
5048 (current org-agenda-filter)
5049 char a n tag)
5050 (unless char
5051 (message
5052 "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<?]:effort: "
5053 (if narrow "Narrow" "Filter") tag-chars)
5054 (setq char (read-char)))
5055 (when (member char '(?+ ?-))
5056 ;; Narrowing down
5057 (cond ((equal char ?-) (setq strip t narrow t))
5058 ((equal char ?+) (setq strip nil narrow t)))
5059 (message
5060 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
5061 (setq char (read-char)))
5062 (when (member char '(?< ?> ?= ??))
5063 ;; An effort operator
5064 (setq effort-op (char-to-string char))
5065 (setq alist nil) ; to make sure it will be interpreted as effort.
5066 (unless (equal char ??)
5067 (loop for i from 0 to 9 do
5068 (setq effort-prompt
5069 (concat
5070 effort-prompt " ["
5071 (if (= i 9) "0" (int-to-string (1+ i)))
5072 "]" (nth i efforts))))
5073 (message "Effort%s: %s " effort-op effort-prompt)
5074 (setq char (read-char))
5075 (when (or (< char ?0) (> char ?9))
5076 (error "Need 1-9,0 to select effort" ))))
5077 (when (equal char ?\t)
5078 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
5079 (org-set-local 'org-global-tags-completion-table
5080 (org-global-tags-completion-table)))
5081 (let ((completion-ignore-case t))
5082 (setq tag (org-icompleting-read
5083 "Tag: " org-global-tags-completion-table))))
5084 (cond
5085 ((equal char ?/)
5086 (org-agenda-filter-by-tag-show-all)
5087 (when (get 'org-agenda-filter :preset-filter)
5088 (org-agenda-filter-apply org-agenda-filter)))
5089 ((or (equal char ?\ )
5090 (setq a (rassoc char alist))
5091 (and (>= char ?0) (<= char ?9)
5092 (setq n (if (= char ?0) 9 (- char ?0 1))
5093 tag (concat effort-op (nth n efforts))
5094 a (cons tag nil)))
5095 (and (= char ??)
5096 (setq tag "?eff")
5097 a (cons tag nil))
5098 (and tag (setq a (cons tag nil))))
5099 (org-agenda-filter-by-tag-show-all)
5100 (setq tag (car a))
5101 (setq org-agenda-filter
5102 (cons (concat (if strip "-" "+") tag)
5103 (if narrow current nil)))
5104 (org-agenda-filter-apply org-agenda-filter))
5105 (t (error "Invalid tag selection character %c" char)))))
5107 (defun org-agenda-filter-by-tag-refine (strip &optional char)
5108 "Refine the current filter. See `org-agenda-filter-by-tag."
5109 (interactive "P")
5110 (org-agenda-filter-by-tag strip char 'refine))
5112 (defun org-agenda-filter-make-matcher ()
5113 "Create the form that tests a line for the agenda filter."
5114 (let (f f1)
5115 (dolist (x (append (get 'org-agenda-filter :preset-filter)
5116 org-agenda-filter))
5117 (if (member x '("-" "+"))
5118 (setq f1 '(not tags))
5119 (if (string-match "[<=>?]" x)
5120 (setq f1 (org-agenda-filter-effort-form x))
5121 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
5122 (if (equal (string-to-char x) ?-)
5123 (setq f1 (list 'not f1))))
5124 (push f1 f))
5125 (cons 'and (nreverse f))))
5127 (defun org-agenda-filter-effort-form (e)
5128 "Return the form to compare the effort of the current line with what E says.
5129 E looks line \"+<2:25\"."
5130 (let (op)
5131 (setq e (substring e 1))
5132 (setq op (string-to-char e) e (substring e 1))
5133 (setq op (cond ((equal op ?<) '<=)
5134 ((equal op ?>) '>=)
5135 ((equal op ??) op)
5136 (t '=)))
5137 (list 'org-agenda-compare-effort (list 'quote op)
5138 (org-hh:mm-string-to-minutes e))))
5140 (defun org-agenda-compare-effort (op value)
5141 "Compare the effort of the current line with VALUE, using OP.
5142 If the line does not have an effort defined, return nil."
5143 (let ((eff (get-text-property (point) 'effort-minutes)))
5144 (if (equal op ??)
5145 (not eff)
5146 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
5147 value))))
5149 (defun org-agenda-filter-apply (filter)
5150 "Set FILTER as the new agenda filter and apply it."
5151 (let (tags)
5152 (setq org-agenda-filter filter
5153 org-agenda-filter-form (org-agenda-filter-make-matcher))
5154 (org-agenda-set-mode-name)
5155 (save-excursion
5156 (goto-char (point-min))
5157 (while (not (eobp))
5158 (if (get-text-property (point) 'org-marker)
5159 (progn
5160 (setq tags (get-text-property (point) 'tags)) ; used in eval
5161 (if (not (eval org-agenda-filter-form))
5162 (org-agenda-filter-by-tag-hide-line))
5163 (beginning-of-line 2))
5164 (beginning-of-line 2))))))
5166 (defun org-agenda-filter-by-tag-hide-line ()
5167 (let (ov)
5168 (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
5169 (point-at-eol)))
5170 (org-overlay-put ov 'invisible t)
5171 (org-overlay-put ov 'type 'tags-filter)
5172 (push ov org-agenda-filter-overlays)))
5174 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
5175 (setq pos (or pos (point)))
5176 (save-excursion
5177 (dolist (ov (org-overlays-at pos))
5178 (when (and (org-overlay-get ov 'invisible)
5179 (eq (org-overlay-get ov 'type) 'tags-filter))
5180 (goto-char pos)
5181 (if (< (org-overlay-start ov) (point-at-eol))
5182 (org-move-overlay ov (point-at-eol)
5183 (org-overlay-end ov)))))))
5185 (defun org-agenda-filter-by-tag-show-all ()
5186 (mapc 'org-delete-overlay org-agenda-filter-overlays)
5187 (setq org-agenda-filter-overlays nil)
5188 (setq org-agenda-filter nil)
5189 (setq org-agenda-filter-form nil)
5190 (org-agenda-set-mode-name))
5192 (defun org-agenda-manipulate-query-add ()
5193 "Manipulate the query by adding a search term with positive selection.
5194 Positive selection means, the term must be matched for selection of an entry."
5195 (interactive)
5196 (org-agenda-manipulate-query ?\[))
5197 (defun org-agenda-manipulate-query-subtract ()
5198 "Manipulate the query by adding a search term with negative selection.
5199 Negative selection means, term must not be matched for selection of an entry."
5200 (interactive)
5201 (org-agenda-manipulate-query ?\]))
5202 (defun org-agenda-manipulate-query-add-re ()
5203 "Manipulate the query by adding a search regexp with positive selection.
5204 Positive selection means, the regexp must match for selection of an entry."
5205 (interactive)
5206 (org-agenda-manipulate-query ?\{))
5207 (defun org-agenda-manipulate-query-subtract-re ()
5208 "Manipulate the query by adding a search regexp with negative selection.
5209 Negative selection means, regexp must not match for selection of an entry."
5210 (interactive)
5211 (org-agenda-manipulate-query ?\}))
5212 (defun org-agenda-manipulate-query (char)
5213 (cond
5214 ((memq org-agenda-type '(timeline agenda))
5215 (let ((org-agenda-include-inactive-timestamps t))
5216 (org-agenda-redo))
5217 (message "Display now includes inactive timestamps as well"))
5218 ((eq org-agenda-type 'search)
5219 (org-add-to-string
5220 'org-agenda-query-string
5221 (cdr (assoc char '((?\[ . " +") (?\] . " -")
5222 (?\{ . " +{}") (?\} . " -{}")))))
5223 (setq org-agenda-redo-command
5224 (list 'org-search-view
5225 org-todo-only
5226 org-agenda-query-string
5227 (+ (length org-agenda-query-string)
5228 (if (member char '(?\{ ?\})) 0 1))))
5229 (set-register org-agenda-query-register org-agenda-query-string)
5230 (org-agenda-redo))
5231 (t (error "Cannot manipulate query for %s-type agenda buffers"
5232 org-agenda-type))))
5234 (defun org-add-to-string (var string)
5235 (set var (concat (symbol-value var) string)))
5237 (defun org-agenda-goto-date (date)
5238 "Jump to DATE in agenda."
5239 (interactive (list (org-read-date)))
5240 (org-agenda-list nil date))
5242 (defun org-agenda-goto-today ()
5243 "Go to today."
5244 (interactive)
5245 (org-agenda-check-type t 'timeline 'agenda)
5246 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
5247 (cond
5248 (tdpos (goto-char tdpos))
5249 ((eq org-agenda-type 'agenda)
5250 (let* ((sd (time-to-days
5251 (time-subtract (current-time)
5252 (list 0 (* 3600 org-extend-today-until) 0))))
5253 (comp (org-agenda-compute-time-span sd org-agenda-span))
5254 (org-agenda-overriding-arguments org-agenda-last-arguments))
5255 (setf (nth 1 org-agenda-overriding-arguments) (car comp))
5256 (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
5257 (org-agenda-redo)
5258 (org-agenda-find-same-or-today-or-agenda)))
5259 (t (error "Cannot find today")))))
5261 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
5262 (goto-char
5263 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
5264 (text-property-any (point-min) (point-max) 'org-today t)
5265 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
5266 (point-min))))
5268 (defun org-agenda-later (arg)
5269 "Go forward in time by thee current span.
5270 With prefix ARG, go forward that many times the current span."
5271 (interactive "p")
5272 (org-agenda-check-type t 'agenda)
5273 (let* ((span org-agenda-span)
5274 (sd org-starting-day)
5275 (greg (calendar-gregorian-from-absolute sd))
5276 (cnt (get-text-property (point) 'org-day-cnt))
5277 greg2 nd)
5278 (cond
5279 ((eq span 'day)
5280 (setq sd (+ arg sd) nd 1))
5281 ((eq span 'week)
5282 (setq sd (+ (* 7 arg) sd) nd 7))
5283 ((eq span 'month)
5284 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
5285 sd (calendar-absolute-from-gregorian greg2))
5286 (setcar greg2 (1+ (car greg2)))
5287 (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
5288 ((eq span 'year)
5289 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
5290 sd (calendar-absolute-from-gregorian greg2))
5291 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
5292 (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
5293 (let ((org-agenda-overriding-arguments
5294 (list (car org-agenda-last-arguments) sd nd t)))
5295 (org-agenda-redo)
5296 (org-agenda-find-same-or-today-or-agenda cnt))))
5298 (defun org-agenda-earlier (arg)
5299 "Go backward in time by the current span.
5300 With prefix ARG, go backward that many times the current span."
5301 (interactive "p")
5302 (org-agenda-later (- arg)))
5304 (defun org-agenda-view-mode-dispatch ()
5305 "Call one of the view mode commands."
5306 (interactive)
5307 (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
5308 clock[R]eport time[G]rid [[]inactive [E]ntryText include[D]iary")
5309 (let ((a (read-char-exclusive)))
5310 (case a
5311 (?d (call-interactively 'org-agenda-day-view))
5312 (?w (call-interactively 'org-agenda-week-view))
5313 (?m (call-interactively 'org-agenda-month-view))
5314 (?y (call-interactively 'org-agenda-year-view))
5315 (?l (call-interactively 'org-agenda-log-mode))
5316 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
5317 (?a (call-interactively 'org-agenda-archives-mode))
5318 (?A (org-agenda-archives-mode 'files))
5319 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
5320 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
5321 (?G (call-interactively 'org-agenda-toggle-time-grid))
5322 (?D (call-interactively 'org-agenda-toggle-diary))
5323 (?\[ (let ((org-agenda-include-inactive-timestamps t))
5324 (org-agenda-check-type t 'timeline 'agenda)
5325 (org-agenda-redo))
5326 (message "Display now includes inactive timestamps as well"))
5327 (?q (message "Abort"))
5328 (otherwise (error "Invalid key" )))))
5330 (defun org-agenda-day-view (&optional day-of-year)
5331 "Switch to daily view for agenda.
5332 With argument DAY-OF-YEAR, switch to that day of the year."
5333 (interactive "P")
5334 (setq org-agenda-ndays 1)
5335 (org-agenda-change-time-span 'day day-of-year))
5336 (defun org-agenda-week-view (&optional iso-week)
5337 "Switch to daily view for agenda.
5338 With argument ISO-WEEK, switch to the corresponding ISO week.
5339 If ISO-WEEK has more then 2 digits, only the last two encode the
5340 week. Any digits before this encode a year. So 200712 means
5341 week 12 of year 2007. Years in the range 1938-2037 can also be
5342 written as 2-digit years."
5343 (interactive "P")
5344 (setq org-agenda-ndays 7)
5345 (org-agenda-change-time-span 'week iso-week))
5346 (defun org-agenda-month-view (&optional month)
5347 "Switch to monthly view for agenda.
5348 With argument MONTH, switch to that month."
5349 (interactive "P")
5350 (org-agenda-change-time-span 'month month))
5351 (defun org-agenda-year-view (&optional year)
5352 "Switch to yearly view for agenda.
5353 With argument YEAR, switch to that year.
5354 If MONTH has more then 2 digits, only the last two encode the
5355 month. Any digits before this encode a year. So 200712 means
5356 December year 2007. Years in the range 1938-2037 can also be
5357 written as 2-digit years."
5358 (interactive "P")
5359 (when year
5360 (setq year (org-small-year-to-year year)))
5361 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
5362 (org-agenda-change-time-span 'year year)
5363 (error "Abort")))
5365 (defun org-agenda-change-time-span (span &optional n)
5366 "Change the agenda view to SPAN.
5367 SPAN may be `day', `week', `month', `year'."
5368 (org-agenda-check-type t 'agenda)
5369 (if (and (not n) (equal org-agenda-span span))
5370 (error "Viewing span is already \"%s\"" span))
5371 (let* ((sd (or (get-text-property (point) 'day)
5372 org-starting-day))
5373 (computed (org-agenda-compute-time-span sd span n))
5374 (org-agenda-overriding-arguments
5375 (list (car org-agenda-last-arguments)
5376 (car computed) (cdr computed) t)))
5377 (org-agenda-redo)
5378 (org-agenda-find-same-or-today-or-agenda))
5379 (org-agenda-set-mode-name)
5380 (message "Switched to %s view" span))
5382 (defun org-agenda-compute-time-span (sd span &optional n)
5383 "Compute starting date and number of days for agenda.
5384 SPAN may be `day', `week', `month', `year'. The return value
5385 is a cons cell with the starting date and the number of days,
5386 so that the date SD will be in that range."
5387 (let* ((greg (calendar-gregorian-from-absolute sd))
5388 (dg (nth 1 greg))
5389 (mg (car greg))
5390 (yg (nth 2 greg))
5391 nd w1 y1 m1 thisweek)
5392 (cond
5393 ((eq span 'day)
5394 (when n
5395 (setq sd (+ (calendar-absolute-from-gregorian
5396 (list mg 1 yg))
5397 n -1)))
5398 (setq nd 1))
5399 ((eq span 'week)
5400 (let* ((nt (calendar-day-of-week
5401 (calendar-gregorian-from-absolute sd)))
5402 (d (if org-agenda-start-on-weekday
5403 (- nt org-agenda-start-on-weekday)
5404 0)))
5405 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
5406 (when n
5407 (require 'cal-iso)
5408 (setq thisweek (car (calendar-iso-from-absolute sd)))
5409 (when (> n 99)
5410 (setq y1 (org-small-year-to-year (/ n 100))
5411 n (mod n 100)))
5412 (setq sd
5413 (calendar-absolute-from-iso
5414 (list n 1
5415 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
5416 (setq nd 7)))
5417 ((eq span 'month)
5418 (when (and n (> n 99))
5419 (setq y1 (org-small-year-to-year (/ n 100))
5420 n (mod n 100)))
5421 (setq sd (calendar-absolute-from-gregorian
5422 (list (or n mg) 1 (or y1 yg)))
5423 nd (- (calendar-absolute-from-gregorian
5424 (list (1+ (or n mg)) 1 (or y1 yg)))
5425 sd)))
5426 ((eq span 'year)
5427 (setq sd (calendar-absolute-from-gregorian
5428 (list 1 1 (or n yg)))
5429 nd (- (calendar-absolute-from-gregorian
5430 (list 1 1 (1+ (or n yg))))
5431 sd))))
5432 (cons sd nd)))
5434 (defun org-agenda-next-date-line (&optional arg)
5435 "Jump to the next line indicating a date in agenda buffer."
5436 (interactive "p")
5437 (org-agenda-check-type t 'agenda 'timeline)
5438 (beginning-of-line 1)
5439 ;; This does not work if user makes date format that starts with a blank
5440 (if (looking-at "^\\S-") (forward-char 1))
5441 (if (not (re-search-forward "^\\S-" nil t arg))
5442 (progn
5443 (backward-char 1)
5444 (error "No next date after this line in this buffer")))
5445 (goto-char (match-beginning 0)))
5447 (defun org-agenda-previous-date-line (&optional arg)
5448 "Jump to the previous line indicating a date in agenda buffer."
5449 (interactive "p")
5450 (org-agenda-check-type t 'agenda 'timeline)
5451 (beginning-of-line 1)
5452 (if (not (re-search-backward "^\\S-" nil t arg))
5453 (error "No previous date before this line in this buffer")))
5455 ;; Initialize the highlight
5456 (defvar org-hl (org-make-overlay 1 1))
5457 (org-overlay-put org-hl 'face 'highlight)
5459 (defun org-highlight (begin end &optional buffer)
5460 "Highlight a region with overlay."
5461 (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
5462 org-hl begin end (or buffer (current-buffer))))
5464 (defun org-unhighlight ()
5465 "Detach overlay INDEX."
5466 (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
5468 ;; FIXME this is currently not used.
5469 (defun org-highlight-until-next-command (beg end &optional buffer)
5470 "Move the highlight overlay to BEG/END, remove it before the next command."
5471 (org-highlight beg end buffer)
5472 (add-hook 'pre-command-hook 'org-unhighlight-once))
5473 (defun org-unhighlight-once ()
5474 "Remove the highlight from its position, and this function from the hook."
5475 (remove-hook 'pre-command-hook 'org-unhighlight-once)
5476 (org-unhighlight))
5478 (defun org-agenda-follow-mode ()
5479 "Toggle follow mode in an agenda buffer."
5480 (interactive)
5481 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
5482 (org-agenda-set-mode-name)
5483 (message "Follow mode is %s"
5484 (if org-agenda-follow-mode "on" "off")))
5486 (defun org-agenda-entry-text-mode (&optional arg)
5487 "Toggle entry text mode in an agenda buffer."
5488 (interactive "P")
5489 (if (integerp arg)
5490 (setq org-agenda-entry-text-mode t)
5491 (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
5492 (org-agenda-entry-text-hide)
5493 (and org-agenda-entry-text-mode
5494 (let ((org-agenda-entry-text-maxlines
5495 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5496 (org-agenda-entry-text-show)))
5497 (org-agenda-set-mode-name)
5498 (message "Entry text mode is %s. Maximum number of lines is %d"
5499 (if org-agenda-entry-text-mode "on" "off")
5500 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
5502 (defun org-agenda-clockreport-mode ()
5503 "Toggle clocktable mode in an agenda buffer."
5504 (interactive)
5505 (org-agenda-check-type t 'agenda)
5506 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
5507 (org-agenda-set-mode-name)
5508 (org-agenda-redo)
5509 (message "Clocktable mode is %s"
5510 (if org-agenda-clockreport-mode "on" "off")))
5512 (defun org-agenda-log-mode (&optional special)
5513 "Toggle log mode in an agenda buffer.
5514 With argument SPECIAL, show all possible log items, not only the ones
5515 configured in `org-agenda-log-mode-items'.
5516 With a double `C-u' prefix arg, show *only* log items, nothing else."
5517 (interactive "P")
5518 (org-agenda-check-type t 'agenda 'timeline)
5519 (setq org-agenda-show-log
5520 (if (equal special '(16))
5521 'only
5522 (if special '(closed clock state)
5523 (not org-agenda-show-log))))
5524 (org-agenda-set-mode-name)
5525 (org-agenda-redo)
5526 (message "Log mode is %s"
5527 (if org-agenda-show-log "on" "off")))
5529 (defun org-agenda-archives-mode (&optional with-files)
5530 "Toggle inclusion of items in trees marked with :ARCHIVE:.
5531 When called with a prefix argument, include all archive files as well."
5532 (interactive "P")
5533 (setq org-agenda-archives-mode
5534 (if with-files t (if org-agenda-archives-mode nil 'trees)))
5535 (org-agenda-set-mode-name)
5536 (org-agenda-redo)
5537 (message
5538 "%s"
5539 (cond
5540 ((eq org-agenda-archives-mode nil)
5541 "No archives are included")
5542 ((eq org-agenda-archives-mode 'trees)
5543 (format "Trees with :%s: tag are included" org-archive-tag))
5544 ((eq org-agenda-archives-mode t)
5545 (format "Trees with :%s: tag and all active archive files are included"
5546 org-archive-tag)))))
5548 (defun org-agenda-toggle-diary ()
5549 "Toggle diary inclusion in an agenda buffer."
5550 (interactive)
5551 (org-agenda-check-type t 'agenda)
5552 (setq org-agenda-include-diary (not org-agenda-include-diary))
5553 (org-agenda-redo)
5554 (org-agenda-set-mode-name)
5555 (message "Diary inclusion turned %s"
5556 (if org-agenda-include-diary "on" "off")))
5558 (defun org-agenda-toggle-time-grid ()
5559 "Toggle time grid in an agenda buffer."
5560 (interactive)
5561 (org-agenda-check-type t 'agenda)
5562 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
5563 (org-agenda-redo)
5564 (org-agenda-set-mode-name)
5565 (message "Time-grid turned %s"
5566 (if org-agenda-use-time-grid "on" "off")))
5568 (defun org-agenda-set-mode-name ()
5569 "Set the mode name to indicate all the small mode settings."
5570 (setq mode-name
5571 (concat "Org-Agenda"
5572 (if (equal org-agenda-ndays 1) " Day" "")
5573 (if (equal org-agenda-ndays 7) " Week" "")
5574 (if org-agenda-follow-mode " Follow" "")
5575 (if org-agenda-entry-text-mode " ETxt" "")
5576 (if org-agenda-include-diary " Diary" "")
5577 (if org-agenda-use-time-grid " Grid" "")
5578 (if (consp org-agenda-show-log) " LogAll"
5579 (if org-agenda-show-log " Log" ""))
5580 (if (or org-agenda-filter (get 'org-agenda-filter
5581 :preset-filter))
5582 (concat " {" (mapconcat
5583 'identity
5584 (append (get 'org-agenda-filter
5585 :preset-filter)
5586 org-agenda-filter) "") "}")
5588 (if org-agenda-archives-mode
5589 (if (eq org-agenda-archives-mode t)
5590 " Archives"
5591 (format " :%s:" org-archive-tag))
5593 (if org-agenda-clockreport-mode " Clock" "")))
5594 (force-mode-line-update))
5596 (defun org-agenda-post-command-hook ()
5597 (and (eolp) (not (bolp)) (backward-char 1))
5598 (setq org-agenda-type
5599 (or (get-text-property (point) 'org-agenda-type)
5600 (get-text-property (max (point-min) (1- (point)))
5601 'org-agenda-type)))
5602 (if (and org-agenda-follow-mode
5603 (get-text-property (point) 'org-marker))
5604 (org-agenda-show)))
5606 (defun org-agenda-show-priority ()
5607 "Show the priority of the current item.
5608 This priority is composed of the main priority given with the [#A] cookies,
5609 and by additional input from the age of a schedules or deadline entry."
5610 (interactive)
5611 (let* ((pri (get-text-property (point-at-bol) 'priority)))
5612 (message "Priority is %d" (if pri pri -1000))))
5614 (defun org-agenda-show-tags ()
5615 "Show the tags applicable to the current item."
5616 (interactive)
5617 (let* ((tags (get-text-property (point-at-bol) 'tags)))
5618 (if tags
5619 (message "Tags are :%s:"
5620 (org-no-properties (mapconcat 'identity tags ":")))
5621 (message "No tags associated with this line"))))
5623 (defun org-agenda-goto (&optional highlight)
5624 "Go to the Org-mode file which contains the item at point."
5625 (interactive)
5626 (let* ((marker (or (get-text-property (point) 'org-marker)
5627 (org-agenda-error)))
5628 (buffer (marker-buffer marker))
5629 (pos (marker-position marker)))
5630 (switch-to-buffer-other-window buffer)
5631 (widen)
5632 (goto-char pos)
5633 (when (org-mode-p)
5634 (org-show-context 'agenda)
5635 (save-excursion
5636 (and (outline-next-heading)
5637 (org-flag-heading nil)))) ; show the next heading
5638 (recenter (/ (window-height) 2))
5639 (run-hooks 'org-agenda-after-show-hook)
5640 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
5642 (defvar org-agenda-after-show-hook nil
5643 "Normal hook run after an item has been shown from the agenda.
5644 Point is in the buffer where the item originated.")
5646 (defun org-agenda-kill ()
5647 "Kill the entry or subtree belonging to the current agenda entry."
5648 (interactive)
5649 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5650 (let* ((marker (or (get-text-property (point) 'org-marker)
5651 (org-agenda-error)))
5652 (buffer (marker-buffer marker))
5653 (pos (marker-position marker))
5654 (type (get-text-property (point) 'type))
5655 dbeg dend (n 0) conf)
5656 (org-with-remote-undo buffer
5657 (with-current-buffer buffer
5658 (save-excursion
5659 (goto-char pos)
5660 (if (and (org-mode-p) (not (member type '("sexp"))))
5661 (setq dbeg (progn (org-back-to-heading t) (point))
5662 dend (org-end-of-subtree t t))
5663 (setq dbeg (point-at-bol)
5664 dend (min (point-max) (1+ (point-at-eol)))))
5665 (goto-char dbeg)
5666 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
5667 (setq conf (or (eq t org-agenda-confirm-kill)
5668 (and (numberp org-agenda-confirm-kill)
5669 (> n org-agenda-confirm-kill))))
5670 (and conf
5671 (not (y-or-n-p
5672 (format "Delete entry with %d lines in buffer \"%s\"? "
5673 n (buffer-name buffer))))
5674 (error "Abort"))
5675 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
5676 (with-current-buffer buffer (delete-region dbeg dend))
5677 (message "Agenda item and source killed"))))
5679 (defun org-agenda-archive ()
5680 "Archive the entry or subtree belonging to the current agenda entry."
5681 (interactive)
5682 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5683 (let* ((marker (or (get-text-property (point) 'org-marker)
5684 (org-agenda-error)))
5685 (buffer (marker-buffer marker))
5686 (pos (marker-position marker)))
5687 (org-with-remote-undo buffer
5688 (with-current-buffer buffer
5689 (if (org-mode-p)
5690 (save-excursion
5691 (goto-char pos)
5692 (org-remove-subtree-entries-from-agenda)
5693 (org-back-to-heading t)
5694 (org-archive-subtree))
5695 (error "Archiving works only in Org-mode files"))))))
5697 (defun org-agenda-archive-to-archive-sibling ()
5698 "Move the entry to the archive sibling."
5699 (interactive)
5700 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
5701 (let* ((marker (or (get-text-property (point) 'org-marker)
5702 (org-agenda-error)))
5703 (buffer (marker-buffer marker))
5704 (pos (marker-position marker)))
5705 (org-with-remote-undo buffer
5706 (with-current-buffer buffer
5707 (if (org-mode-p)
5708 (save-excursion
5709 (goto-char pos)
5710 (org-remove-subtree-entries-from-agenda)
5711 (org-back-to-heading t)
5712 (org-archive-to-archive-sibling))
5713 (error "Archiving works only in Org-mode files"))))))
5715 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
5716 "Remove all lines in the agenda that correspond to a given subtree.
5717 The subtree is the one in buffer BUF, starting at BEG and ending at END.
5718 If this information is not given, the function uses the tree at point."
5719 (let ((buf (or buf (current-buffer))) m p)
5720 (save-excursion
5721 (unless (and beg end)
5722 (org-back-to-heading t)
5723 (setq beg (point))
5724 (org-end-of-subtree t)
5725 (setq end (point)))
5726 (set-buffer (get-buffer org-agenda-buffer-name))
5727 (save-excursion
5728 (goto-char (point-max))
5729 (beginning-of-line 1)
5730 (while (not (bobp))
5731 (when (and (setq m (get-text-property (point) 'org-marker))
5732 (equal buf (marker-buffer m))
5733 (setq p (marker-position m))
5734 (>= p beg)
5735 (< p end))
5736 (let ((inhibit-read-only t))
5737 (delete-region (point-at-bol) (1+ (point-at-eol)))))
5738 (beginning-of-line 0))))))
5740 (defun org-agenda-refile (&optional goto rfloc)
5741 "Refile the item at point."
5742 (interactive "P")
5743 (if (equal goto '(16))
5744 (org-refile-goto-last-stored)
5745 (let* ((marker (or (get-text-property (point) 'org-hd-marker)
5746 (org-agenda-error)))
5747 (buffer (marker-buffer marker))
5748 (pos (marker-position marker))
5749 (rfloc (or rfloc
5750 (org-refile-get-location
5751 (if goto "Goto: " "Refile to: ") buffer
5752 org-refile-allow-creating-parent-nodes))))
5753 (with-current-buffer buffer
5754 (save-excursion
5755 (save-restriction
5756 (widen)
5757 (goto-char marker)
5758 (org-remove-subtree-entries-from-agenda)
5759 (org-refile goto buffer rfloc)))))))
5761 (defun org-agenda-open-link (&optional arg)
5762 "Follow the link in the current line, if any.
5763 This looks for a link in the displayed lin in the agenda. It also looks
5764 at the text of the entry itself."
5765 (interactive "P")
5766 (let* ((marker (or (get-text-property (point) 'org-hd-marker)
5767 (get-text-property (point) 'org-marker)))
5768 (buffer (and marker (marker-buffer marker)))
5769 (prefix (buffer-substring
5770 (point-at-bol)
5771 (+ (point-at-bol)
5772 (get-text-property (point) 'prefix-length)))))
5773 (unless buffer (error "Don't know where to look for links"))
5774 (with-current-buffer buffer
5775 (save-excursion
5776 (save-restriction
5777 (widen)
5778 (goto-char marker)
5779 (org-offer-links-in-entry arg prefix))))))
5781 (defun org-agenda-copy-local-variable (var)
5782 "Get a variable from a referenced buffer and install it here."
5783 (let ((m (get-text-property (point) 'org-marker)))
5784 (when (and m (buffer-live-p (marker-buffer m)))
5785 (org-set-local var (with-current-buffer (marker-buffer m)
5786 (symbol-value var))))))
5788 (defun org-agenda-switch-to (&optional delete-other-windows)
5789 "Go to the Org-mode file which contains the item at point."
5790 (interactive)
5791 (let* ((marker (or (get-text-property (point) 'org-marker)
5792 (org-agenda-error)))
5793 (buffer (marker-buffer marker))
5794 (pos (marker-position marker)))
5795 (switch-to-buffer buffer)
5796 (and delete-other-windows (delete-other-windows))
5797 (widen)
5798 (goto-char pos)
5799 (when (org-mode-p)
5800 (org-show-context 'agenda)
5801 (save-excursion
5802 (and (outline-next-heading)
5803 (org-flag-heading nil)))))) ; show the next heading
5805 (defun org-agenda-goto-mouse (ev)
5806 "Go to the Org-mode file which contains the item at the mouse click."
5807 (interactive "e")
5808 (mouse-set-point ev)
5809 (org-agenda-goto))
5811 (defun org-agenda-show (&optional full-entry)
5812 "Display the Org-mode file which contains the item at point.
5813 With prefix argument FULL-ENTRY, make the entire entry visible
5814 if it was hidden in the outline."
5815 (interactive "P")
5816 (let ((win (selected-window)))
5817 (if full-entry
5818 (let ((org-show-entry-below t))
5819 (org-agenda-goto t))
5820 (org-agenda-goto t))
5821 (select-window win)))
5823 (defun org-agenda-show-1 (&optional more)
5824 "Display the Org-mode file which contains the item at point.
5825 The prefix arg causes further revieling:
5827 0 hide the subtree
5828 1 just show the entry according to defaults.
5829 2 show the children view
5830 3 show the subtree view
5831 4 show the entire subtree and any LOGBOOK drawers
5832 5 show the entire subtree and any drawers
5833 With prefix argument FULL-ENTRY, make the entire entry visible
5834 if it was hidden in the outline."
5835 (interactive "p")
5836 (let ((win (selected-window)))
5837 (org-agenda-goto t)
5838 (org-recenter-heading 1)
5839 (cond
5840 ((= more 0)
5841 (hide-subtree)
5842 (save-excursion
5843 (org-back-to-heading)
5844 (run-hook-with-args 'org-cycle-hook 'folded))
5845 (message "Remote: FOLDED"))
5846 ((and (interactive-p) (= more 1))
5847 (message "Remote: show with default settings"))
5848 ((= more 2)
5849 (show-entry)
5850 (show-children)
5851 (save-excursion
5852 (org-back-to-heading)
5853 (run-hook-with-args 'org-cycle-hook 'children))
5854 (message "Remote: CHILDREN"))
5855 ((= more 3)
5856 (show-subtree)
5857 (save-excursion
5858 (org-back-to-heading)
5859 (run-hook-with-args 'org-cycle-hook 'subtree))
5860 (message "Remote: SUBTREE"))
5861 ((= more 4)
5862 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
5863 (org-drawer-regexp
5864 (concat "^[ \t]*:\\("
5865 (mapconcat 'regexp-quote org-drawers "\\|")
5866 "\\):[ \t]*$")))
5867 (show-subtree)
5868 (save-excursion
5869 (org-back-to-heading)
5870 (org-cycle-hide-drawers 'subtree)))
5871 (message "Remote: SUBTREE AND LOGBOOK"))
5872 ((> more 4)
5873 (show-subtree)
5874 (message "Remote: SUBTREE AND ALL DRAWERS")))
5875 (select-window win)))
5877 (defun org-recenter-heading (n)
5878 (save-excursion
5879 (org-back-to-heading)
5880 (recenter n)))
5882 (defvar org-agenda-cycle-counter nil)
5883 (defun org-agenda-cycle-show (&optional n)
5884 "Show the current entry in another window, with default settings.
5885 Default settings are taken from `org-show-hierarchy-above' and siblings.
5886 When use repeatedly in immediate succession, the remote entry will cycle
5887 through visibility
5889 children -> subtree -> folded
5891 When called with a numeric prefix arg, that arg will be passed through to
5892 `org-agenda-show-1'. For the interpretation of that argument, see the
5893 docstring of `org-agenda-show-1'."
5894 (interactive "P")
5895 (if (integerp n)
5896 (setq org-agenda-cycle-counter n)
5897 (if (not (eq last-command this-command))
5898 (setq org-agenda-cycle-counter 1)
5899 (if (equal org-agenda-cycle-counter 0)
5900 (setq org-agenda-cycle-counter 2)
5901 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
5902 (if (> org-agenda-cycle-counter 3)
5903 (setq org-agenda-cycle-counter 0)))))
5904 (org-agenda-show-1 org-agenda-cycle-counter))
5906 (defun org-agenda-recenter (arg)
5907 "Display the Org-mode file which contains the item at point and recenter."
5908 (interactive "P")
5909 (let ((win (selected-window)))
5910 (org-agenda-goto t)
5911 (recenter arg)
5912 (select-window win)))
5914 (defun org-agenda-show-mouse (ev)
5915 "Display the Org-mode file which contains the item at the mouse click."
5916 (interactive "e")
5917 (mouse-set-point ev)
5918 (org-agenda-show))
5920 (defun org-agenda-check-no-diary ()
5921 "Check if the entry is a diary link and abort if yes."
5922 (if (get-text-property (point) 'org-agenda-diary-link)
5923 (org-agenda-error)))
5925 (defun org-agenda-error ()
5926 (error "Command not allowed in this line"))
5928 (defun org-agenda-tree-to-indirect-buffer ()
5929 "Show the subtree corresponding to the current entry in an indirect buffer.
5930 This calls the command `org-tree-to-indirect-buffer' from the original
5931 Org-mode buffer.
5932 With numerical prefix arg ARG, go up to this level and then take that tree.
5933 With a C-u prefix, make a separate frame for this tree (i.e. don't use the
5934 dedicated frame)."
5935 (interactive)
5936 (org-agenda-check-no-diary)
5937 (let* ((marker (or (get-text-property (point) 'org-marker)
5938 (org-agenda-error)))
5939 (buffer (marker-buffer marker))
5940 (pos (marker-position marker)))
5941 (with-current-buffer buffer
5942 (save-excursion
5943 (goto-char pos)
5944 (call-interactively 'org-tree-to-indirect-buffer)))))
5946 (defvar org-last-heading-marker (make-marker)
5947 "Marker pointing to the headline that last changed its TODO state
5948 by a remote command from the agenda.")
5950 (defun org-agenda-todo-nextset ()
5951 "Switch TODO entry to next sequence."
5952 (interactive)
5953 (org-agenda-todo 'nextset))
5955 (defun org-agenda-todo-previousset ()
5956 "Switch TODO entry to previous sequence."
5957 (interactive)
5958 (org-agenda-todo 'previousset))
5960 (defun org-agenda-todo (&optional arg)
5961 "Cycle TODO state of line at point, also in Org-mode file.
5962 This changes the line at point, all other lines in the agenda referring to
5963 the same tree node, and the headline of the tree node in the Org-mode file."
5964 (interactive "P")
5965 (org-agenda-check-no-diary)
5966 (let* ((col (current-column))
5967 (marker (or (get-text-property (point) 'org-marker)
5968 (org-agenda-error)))
5969 (buffer (marker-buffer marker))
5970 (pos (marker-position marker))
5971 (hdmarker (get-text-property (point) 'org-hd-marker))
5972 (todayp (equal (get-text-property (point) 'day)
5973 (time-to-days (current-time))))
5974 (inhibit-read-only t)
5975 org-agenda-headline-snapshot-before-repeat newhead just-one)
5976 (org-with-remote-undo buffer
5977 (with-current-buffer buffer
5978 (widen)
5979 (goto-char pos)
5980 (org-show-context 'agenda)
5981 (save-excursion
5982 (and (outline-next-heading)
5983 (org-flag-heading nil))) ; show the next heading
5984 (let ((current-prefix-arg arg))
5985 (call-interactively 'org-todo))
5986 (and (bolp) (forward-char 1))
5987 (setq newhead (org-get-heading))
5988 (when (and (org-bound-and-true-p
5989 org-agenda-headline-snapshot-before-repeat)
5990 (not (equal org-agenda-headline-snapshot-before-repeat
5991 newhead))
5992 todayp)
5993 (setq newhead org-agenda-headline-snapshot-before-repeat
5994 just-one t))
5995 (save-excursion
5996 (org-back-to-heading)
5997 (move-marker org-last-heading-marker (point))))
5998 (beginning-of-line 1)
5999 (save-excursion
6000 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
6001 (org-move-to-column col))))
6003 (defun org-agenda-add-note (&optional arg)
6004 "Add a time-stamped note to the entry at point."
6005 (interactive "P")
6006 (org-agenda-check-no-diary)
6007 (let* ((marker (or (get-text-property (point) 'org-marker)
6008 (org-agenda-error)))
6009 (buffer (marker-buffer marker))
6010 (pos (marker-position marker))
6011 (hdmarker (get-text-property (point) 'org-hd-marker))
6012 (inhibit-read-only t))
6013 (with-current-buffer buffer
6014 (widen)
6015 (goto-char pos)
6016 (org-show-context 'agenda)
6017 (save-excursion
6018 (and (outline-next-heading)
6019 (org-flag-heading nil))) ; show the next heading
6020 (org-add-note))))
6022 (defun org-agenda-change-all-lines (newhead hdmarker
6023 &optional fixface just-this)
6024 "Change all lines in the agenda buffer which match HDMARKER.
6025 The new content of the line will be NEWHEAD (as modified by
6026 `org-format-agenda-item'). HDMARKER is checked with
6027 `equal' against all `org-hd-marker' text properties in the file.
6028 If FIXFACE is non-nil, the face of each item is modified according to
6029 the new TODO state.
6030 If JUST-THIS is non-nil, change just the current line, not all.
6031 If FORCE-TAGS is non nil, the car of it returns the new tags."
6032 (let* ((inhibit-read-only t)
6033 (line (org-current-line))
6034 (thetags (with-current-buffer (marker-buffer hdmarker)
6035 (save-excursion (save-restriction (widen)
6036 (goto-char hdmarker)
6037 (org-get-tags-at)))))
6038 props m pl undone-face done-face finish new dotime cat tags)
6039 (save-excursion
6040 (goto-char (point-max))
6041 (beginning-of-line 1)
6042 (while (not finish)
6043 (setq finish (bobp))
6044 (when (and (setq m (get-text-property (point) 'org-hd-marker))
6045 (or (not just-this) (= (org-current-line) line))
6046 (equal m hdmarker))
6047 (setq props (text-properties-at (point))
6048 dotime (get-text-property (point) 'dotime)
6049 cat (get-text-property (point) 'org-category)
6050 tags thetags
6051 new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
6052 pl (get-text-property (point) 'prefix-length)
6053 undone-face (get-text-property (point) 'undone-face)
6054 done-face (get-text-property (point) 'done-face))
6055 (org-move-to-column pl)
6056 (cond
6057 ((equal new "")
6058 (beginning-of-line 1)
6059 (and (looking-at ".*\n?") (replace-match "")))
6060 ((looking-at ".*")
6061 (replace-match new t t)
6062 (beginning-of-line 1)
6063 (add-text-properties (point-at-bol) (point-at-eol) props)
6064 (when fixface
6065 (add-text-properties
6066 (point-at-bol) (point-at-eol)
6067 (list 'face
6068 (if org-last-todo-state-is-todo
6069 undone-face done-face))))
6070 (org-agenda-highlight-todo 'line)
6071 (beginning-of-line 1))
6072 (t (error "Line update did not work"))))
6073 (beginning-of-line 0)))
6074 (org-finalize-agenda)))
6076 (defun org-agenda-align-tags (&optional line)
6077 "Align all tags in agenda items to `org-agenda-tags-column'."
6078 (let ((inhibit-read-only t) l c)
6079 (save-excursion
6080 (goto-char (if line (point-at-bol) (point-min)))
6081 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
6082 (if line (point-at-eol) nil) t)
6083 (add-text-properties
6084 (match-beginning 2) (match-end 2)
6085 (list 'face (delq nil (let ((prop (get-text-property
6086 (match-beginning 2) 'face)))
6087 (or (listp prop) (setq prop (list prop)))
6088 (if (memq 'org-tag prop)
6089 prop
6090 (cons 'org-tag prop))))))
6091 (setq l (- (match-end 2) (match-beginning 2))
6092 c (if (< org-agenda-tags-column 0)
6093 (- (abs org-agenda-tags-column) l)
6094 org-agenda-tags-column))
6095 (delete-region (match-beginning 1) (match-end 1))
6096 (goto-char (match-beginning 1))
6097 (insert (org-add-props
6098 (make-string (max 1 (- c (current-column))) ?\ )
6099 (text-properties-at (point)))))
6100 (goto-char (point-min))
6101 (org-font-lock-add-tag-faces (point-max)))))
6103 (defun org-agenda-priority-up ()
6104 "Increase the priority of line at point, also in Org-mode file."
6105 (interactive)
6106 (org-agenda-priority 'up))
6108 (defun org-agenda-priority-down ()
6109 "Decrease the priority of line at point, also in Org-mode file."
6110 (interactive)
6111 (org-agenda-priority 'down))
6113 (defun org-agenda-priority (&optional force-direction)
6114 "Set the priority of line at point, also in Org-mode file.
6115 This changes the line at point, all other lines in the agenda referring to
6116 the same tree node, and the headline of the tree node in the Org-mode file."
6117 (interactive)
6118 (unless org-enable-priority-commands
6119 (error "Priority commands are disabled"))
6120 (org-agenda-check-no-diary)
6121 (let* ((marker (or (get-text-property (point) 'org-marker)
6122 (org-agenda-error)))
6123 (hdmarker (get-text-property (point) 'org-hd-marker))
6124 (buffer (marker-buffer hdmarker))
6125 (pos (marker-position hdmarker))
6126 (inhibit-read-only t)
6127 newhead)
6128 (org-with-remote-undo buffer
6129 (with-current-buffer buffer
6130 (widen)
6131 (goto-char pos)
6132 (org-show-context 'agenda)
6133 (save-excursion
6134 (and (outline-next-heading)
6135 (org-flag-heading nil))) ; show the next heading
6136 (funcall 'org-priority force-direction)
6137 (end-of-line 1)
6138 (setq newhead (org-get-heading)))
6139 (org-agenda-change-all-lines newhead hdmarker)
6140 (beginning-of-line 1))))
6142 ;; FIXME: should fix the tags property of the agenda line.
6143 (defun org-agenda-set-tags (&optional tag onoff)
6144 "Set tags for the current headline."
6145 (interactive)
6146 (org-agenda-check-no-diary)
6147 (if (and (org-region-active-p) (interactive-p))
6148 (call-interactively 'org-change-tag-in-region)
6149 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6150 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
6151 (org-agenda-error)))
6152 (buffer (marker-buffer hdmarker))
6153 (pos (marker-position hdmarker))
6154 (inhibit-read-only t)
6155 newhead)
6156 (org-with-remote-undo buffer
6157 (with-current-buffer buffer
6158 (widen)
6159 (goto-char pos)
6160 (save-excursion
6161 (org-show-context 'agenda))
6162 (save-excursion
6163 (and (outline-next-heading)
6164 (org-flag-heading nil))) ; show the next heading
6165 (goto-char pos)
6166 (if tag
6167 (org-toggle-tag tag onoff)
6168 (call-interactively 'org-set-tags))
6169 (end-of-line 1)
6170 (setq newhead (org-get-heading)))
6171 (org-agenda-change-all-lines newhead hdmarker)
6172 (beginning-of-line 1)))))
6174 (defun org-agenda-set-property ()
6175 "Set a property for the current headline."
6176 (interactive)
6177 (org-agenda-check-no-diary)
6178 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6179 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
6180 (org-agenda-error)))
6181 (buffer (marker-buffer hdmarker))
6182 (pos (marker-position hdmarker))
6183 (inhibit-read-only t)
6184 newhead)
6185 (org-with-remote-undo buffer
6186 (with-current-buffer buffer
6187 (widen)
6188 (goto-char pos)
6189 (save-excursion
6190 (org-show-context 'agenda))
6191 (save-excursion
6192 (and (outline-next-heading)
6193 (org-flag-heading nil))) ; show the next heading
6194 (goto-char pos)
6195 (call-interactively 'org-set-property)))))
6197 (defun org-agenda-set-effort ()
6198 "Set the effort property for the current headline."
6199 (interactive)
6200 (org-agenda-check-no-diary)
6201 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6202 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
6203 (org-agenda-error)))
6204 (buffer (marker-buffer hdmarker))
6205 (pos (marker-position hdmarker))
6206 (inhibit-read-only t)
6207 newhead)
6208 (org-with-remote-undo buffer
6209 (with-current-buffer buffer
6210 (widen)
6211 (goto-char pos)
6212 (save-excursion
6213 (org-show-context 'agenda))
6214 (save-excursion
6215 (and (outline-next-heading)
6216 (org-flag-heading nil))) ; show the next heading
6217 (goto-char pos)
6218 (call-interactively 'org-set-effort)
6219 (end-of-line 1)))))
6221 (defun org-agenda-toggle-archive-tag ()
6222 "Toggle the archive tag for the current entry."
6223 (interactive)
6224 (org-agenda-check-no-diary)
6225 (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
6226 (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
6227 (org-agenda-error)))
6228 (buffer (marker-buffer hdmarker))
6229 (pos (marker-position hdmarker))
6230 (inhibit-read-only t)
6231 newhead)
6232 (org-with-remote-undo buffer
6233 (with-current-buffer buffer
6234 (widen)
6235 (goto-char pos)
6236 (org-show-context 'agenda)
6237 (save-excursion
6238 (and (outline-next-heading)
6239 (org-flag-heading nil))) ; show the next heading
6240 (call-interactively 'org-toggle-archive-tag)
6241 (end-of-line 1)
6242 (setq newhead (org-get-heading)))
6243 (org-agenda-change-all-lines newhead hdmarker)
6244 (beginning-of-line 1))))
6246 (defun org-agenda-do-date-later (arg)
6247 (interactive "P")
6248 (cond
6249 ((or (equal arg '(16))
6250 (memq last-command
6251 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6252 (setq this-command 'org-agenda-date-later-minutes)
6253 (org-agenda-date-later-minutes 1))
6254 ((or (equal arg '(4))
6255 (memq last-command
6256 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6257 (setq this-command 'org-agenda-date-later-hours)
6258 (org-agenda-date-later-hours 1))
6260 (org-agenda-date-later (prefix-numeric-value arg)))))
6262 (defun org-agenda-do-date-earlier (arg)
6263 (interactive "P")
6264 (cond
6265 ((or (equal arg '(16))
6266 (memq last-command
6267 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
6268 (setq this-command 'org-agenda-date-earlier-minutes)
6269 (org-agenda-date-earlier-minutes 1))
6270 ((or (equal arg '(4))
6271 (memq last-command
6272 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
6273 (setq this-command 'org-agenda-date-earlier-hours)
6274 (org-agenda-date-earlier-hours 1))
6276 (org-agenda-date-earlier (prefix-numeric-value arg)))))
6278 (defun org-agenda-date-later (arg &optional what)
6279 "Change the date of this item to one day later."
6280 (interactive "p")
6281 (org-agenda-check-type t 'agenda 'timeline)
6282 (org-agenda-check-no-diary)
6283 (let* ((marker (or (get-text-property (point) 'org-marker)
6284 (org-agenda-error)))
6285 (buffer (marker-buffer marker))
6286 (pos (marker-position marker)))
6287 (org-with-remote-undo buffer
6288 (with-current-buffer buffer
6289 (widen)
6290 (goto-char pos)
6291 (if (not (org-at-timestamp-p))
6292 (error "Cannot find time stamp"))
6293 (org-timestamp-change arg (or what 'day)))
6294 (org-agenda-show-new-time marker org-last-changed-timestamp))
6295 (message "Time stamp changed to %s" org-last-changed-timestamp)))
6297 (defun org-agenda-date-earlier (arg &optional what)
6298 "Change the date of this item to one day earlier."
6299 (interactive "p")
6300 (org-agenda-date-later (- arg) what))
6302 (defun org-agenda-date-later-minutes (arg)
6303 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6304 (interactive "p")
6305 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6306 (org-agenda-date-later arg 'minute))
6308 (defun org-agenda-date-earlier-minutes (arg)
6309 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
6310 (interactive "p")
6311 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
6312 (org-agenda-date-earlier arg 'minute))
6314 (defun org-agenda-date-later-hours (arg)
6315 "Change the time of this item, in hour steps."
6316 (interactive "p")
6317 (org-agenda-date-later arg 'hour))
6319 (defun org-agenda-date-earlier-hours (arg)
6320 "Change the time of this item, in hour steps."
6321 (interactive "p")
6322 (org-agenda-date-earlier arg 'hour))
6324 (defun org-agenda-show-new-time (marker stamp &optional prefix)
6325 "Show new date stamp via text properties."
6326 ;; We use text properties to make this undoable
6327 (let ((inhibit-read-only t)
6328 (buffer-invisibility-spec))
6329 (setq stamp (concat " " prefix " => " stamp))
6330 (save-excursion
6331 (goto-char (point-max))
6332 (while (not (bobp))
6333 (when (equal marker (get-text-property (point) 'org-marker))
6334 (org-move-to-column (- (window-width) (length stamp)) t)
6335 (org-agenda-fix-tags-filter-overlays-at (point))
6336 (if (featurep 'xemacs)
6337 ;; Use `duplicable' property to trigger undo recording
6338 (let ((ex (make-extent nil nil))
6339 (gl (make-glyph stamp)))
6340 (set-glyph-face gl 'secondary-selection)
6341 (set-extent-properties
6342 ex (list 'invisible t 'end-glyph gl 'duplicable t))
6343 (insert-extent ex (1- (point)) (point-at-eol)))
6344 (add-text-properties
6345 (1- (point)) (point-at-eol)
6346 (list 'display (org-add-props stamp nil
6347 'face 'secondary-selection))))
6348 (beginning-of-line 1))
6349 (beginning-of-line 0)))))
6351 (defun org-agenda-date-prompt (arg)
6352 "Change the date of this item. Date is prompted for, with default today.
6353 The prefix ARG is passed to the `org-time-stamp' command and can therefore
6354 be used to request time specification in the time stamp."
6355 (interactive "P")
6356 (org-agenda-check-type t 'agenda 'timeline)
6357 (org-agenda-check-no-diary)
6358 (let* ((marker (or (get-text-property (point) 'org-marker)
6359 (org-agenda-error)))
6360 (buffer (marker-buffer marker))
6361 (pos (marker-position marker)))
6362 (org-with-remote-undo buffer
6363 (with-current-buffer buffer
6364 (widen)
6365 (goto-char pos)
6366 (if (not (org-at-timestamp-p))
6367 (error "Cannot find time stamp"))
6368 (org-time-stamp arg)
6369 (message "Time stamp changed to %s" org-last-changed-timestamp)))))
6371 (defun org-agenda-schedule (arg)
6372 "Schedule the item at point."
6373 (interactive "P")
6374 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6375 (org-agenda-check-no-diary)
6376 (let* ((marker (or (get-text-property (point) 'org-marker)
6377 (org-agenda-error)))
6378 (type (marker-insertion-type marker))
6379 (buffer (marker-buffer marker))
6380 (pos (marker-position marker))
6381 (org-insert-labeled-timestamps-at-point nil)
6383 (set-marker-insertion-type marker t)
6384 (org-with-remote-undo buffer
6385 (with-current-buffer buffer
6386 (widen)
6387 (goto-char pos)
6388 (setq ts (org-schedule arg)))
6389 (org-agenda-show-new-time marker ts "S"))
6390 (message "Item scheduled for %s" ts)))
6392 (defun org-agenda-deadline (arg)
6393 "Schedule the item at point."
6394 (interactive "P")
6395 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
6396 (org-agenda-check-no-diary)
6397 (let* ((marker (or (get-text-property (point) 'org-marker)
6398 (org-agenda-error)))
6399 (buffer (marker-buffer marker))
6400 (pos (marker-position marker))
6401 (org-insert-labeled-timestamps-at-point nil)
6403 (org-with-remote-undo buffer
6404 (with-current-buffer buffer
6405 (widen)
6406 (goto-char pos)
6407 (setq ts (org-deadline arg)))
6408 (org-agenda-show-new-time marker ts "S"))
6409 (message "Deadline for this item set to %s" ts)))
6411 (defun org-agenda-action ()
6412 "Select entry for agenda action, or execute an agenda action.
6413 This command prompts for another letter. Valid inputs are:
6415 m Mark the entry at point for an agenda action
6416 s Schedule the marked entry to the date at the cursor
6417 d Set the deadline of the marked entry to the date at the cursor
6418 r Call `org-remember' with cursor date as the default date
6419 SPC Show marked entry in other window
6420 TAB Visit marked entry in other window
6422 The cursor may be at a date in the calendar, or in the Org agenda."
6423 (interactive)
6424 (let (ans)
6425 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
6426 (setq ans (read-char-exclusive))
6427 (cond
6428 ((equal ans ?m)
6429 ;; Mark this entry
6430 (if (eq major-mode 'org-agenda-mode)
6431 (let ((m (or (get-text-property (point) 'org-hd-marker)
6432 (get-text-property (point) 'org-marker))))
6433 (if m
6434 (progn
6435 (move-marker org-agenda-action-marker
6436 (marker-position m) (marker-buffer m))
6437 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
6438 (error "Don't know which entry to mark")))
6439 (error "This command works only in the agenda")))
6440 ((equal ans ?s)
6441 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
6442 ((equal ans ?d)
6443 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
6444 ((equal ans ?r)
6445 (org-agenda-do-action '(org-remember) t))
6446 ((equal ans ?\ )
6447 (let ((cw (selected-window)))
6448 (org-switch-to-buffer-other-window
6449 (marker-buffer org-agenda-action-marker))
6450 (goto-char org-agenda-action-marker)
6451 (org-show-context 'agenda)
6452 (select-window cw)))
6453 ((equal ans ?\C-i)
6454 (org-switch-to-buffer-other-window
6455 (marker-buffer org-agenda-action-marker))
6456 (goto-char org-agenda-action-marker)
6457 (org-show-context 'agenda))
6458 (t (error "Invalid agenda action %c" ans)))))
6460 (defun org-agenda-do-action (form &optional current-buffer)
6461 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
6462 (let ((org-overriding-default-time (org-get-cursor-date)))
6463 (if current-buffer
6464 (eval form)
6465 (if (not (marker-buffer org-agenda-action-marker))
6466 (error "No entry has bee selected for agenda action")
6467 (with-current-buffer (marker-buffer org-agenda-action-marker)
6468 (save-excursion
6469 (save-restriction
6470 (widen)
6471 (goto-char org-agenda-action-marker)
6472 (eval form))))))))
6474 (defun org-agenda-clock-in (&optional arg)
6475 "Start the clock on the currently selected item."
6476 (interactive "P")
6477 (org-agenda-check-no-diary)
6478 (if (equal arg '(4))
6479 (org-clock-in arg)
6480 (let* ((marker (or (get-text-property (point) 'org-marker)
6481 (org-agenda-error)))
6482 (hdmarker (or (get-text-property (point) 'org-hd-marker)
6483 marker))
6484 (pos (marker-position marker))
6485 newhead)
6486 (org-with-remote-undo (marker-buffer marker)
6487 (with-current-buffer (marker-buffer marker)
6488 (widen)
6489 (goto-char pos)
6490 (org-show-context 'agenda)
6491 (org-show-entry)
6492 (org-cycle-hide-drawers 'children)
6493 (org-clock-in arg)
6494 (setq newhead (org-get-heading)))
6495 (org-agenda-change-all-lines newhead hdmarker)))))
6497 (defun org-agenda-clock-out (&optional arg)
6498 "Stop the currently running clock."
6499 (interactive "P")
6500 (unless (marker-buffer org-clock-marker)
6501 (error "No running clock"))
6502 (let ((marker (make-marker)) newhead)
6503 (org-with-remote-undo (marker-buffer org-clock-marker)
6504 (with-current-buffer (marker-buffer org-clock-marker)
6505 (save-excursion
6506 (save-restriction
6507 (widen)
6508 (goto-char org-clock-marker)
6509 (org-back-to-heading t)
6510 (move-marker marker (point))
6511 (org-clock-out)
6512 (setq newhead (org-get-heading))))))
6513 (org-agenda-change-all-lines newhead marker)
6514 (move-marker marker nil)))
6516 (defun org-agenda-clock-cancel (&optional arg)
6517 "Cancel the currently running clock."
6518 (interactive "P")
6519 (unless (marker-buffer org-clock-marker)
6520 (error "No running clock"))
6521 (org-with-remote-undo (marker-buffer org-clock-marker)
6522 (org-clock-cancel)))
6524 (defun org-agenda-diary-entry ()
6525 "Make a diary entry, like the `i' command from the calendar.
6526 All the standard commands work: block, weekly etc."
6527 (interactive)
6528 (org-agenda-check-type t 'agenda 'timeline)
6529 (require 'diary-lib)
6530 (let* ((char (progn
6531 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
6532 (read-char-exclusive)))
6533 (cmd (cdr (assoc char
6534 '((?d . insert-diary-entry)
6535 (?w . insert-weekly-diary-entry)
6536 (?m . insert-monthly-diary-entry)
6537 (?y . insert-yearly-diary-entry)
6538 (?a . insert-anniversary-diary-entry)
6539 (?b . insert-block-diary-entry)
6540 (?c . insert-cyclic-diary-entry)))))
6541 (oldf (symbol-function 'calendar-cursor-to-date))
6542 ; (buf (get-file-buffer (substitute-in-file-name diary-file)))
6543 (point (point))
6544 (mark (or (mark t) (point))))
6545 (unless cmd
6546 (error "No command associated with <%c>" char))
6547 (unless (and (get-text-property point 'day)
6548 (or (not (equal ?b char))
6549 (get-text-property mark 'day)))
6550 (error "Don't know which date to use for diary entry"))
6551 ;; We implement this by hacking the `calendar-cursor-to-date' function
6552 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
6553 (let ((calendar-mark-ring
6554 (list (calendar-gregorian-from-absolute
6555 (or (get-text-property mark 'day)
6556 (get-text-property point 'day))))))
6557 (unwind-protect
6558 (progn
6559 (fset 'calendar-cursor-to-date
6560 (lambda (&optional error dummy)
6561 (calendar-gregorian-from-absolute
6562 (get-text-property point 'day))))
6563 (call-interactively cmd))
6564 (fset 'calendar-cursor-to-date oldf)))))
6566 (defun org-agenda-execute-calendar-command (cmd)
6567 "Execute a calendar command from the agenda, with the date associated to
6568 the cursor position."
6569 (org-agenda-check-type t 'agenda 'timeline)
6570 (require 'diary-lib)
6571 (unless (get-text-property (point) 'day)
6572 (error "Don't know which date to use for calendar command"))
6573 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
6574 (point (point))
6575 (date (calendar-gregorian-from-absolute
6576 (get-text-property point 'day)))
6577 ;; the following 2 vars are needed in the calendar
6578 (displayed-month (car date))
6579 (displayed-year (nth 2 date)))
6580 (unwind-protect
6581 (progn
6582 (fset 'calendar-cursor-to-date
6583 (lambda (&optional error dummy)
6584 (calendar-gregorian-from-absolute
6585 (get-text-property point 'day))))
6586 (call-interactively cmd))
6587 (fset 'calendar-cursor-to-date oldf))))
6589 (defun org-agenda-phases-of-moon ()
6590 "Display the phases of the moon for the 3 months around the cursor date."
6591 (interactive)
6592 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
6594 (defun org-agenda-holidays ()
6595 "Display the holidays for the 3 months around the cursor date."
6596 (interactive)
6597 (org-agenda-execute-calendar-command 'list-calendar-holidays))
6599 (defvar calendar-longitude)
6600 (defvar calendar-latitude)
6601 (defvar calendar-location-name)
6603 (defun org-agenda-sunrise-sunset (arg)
6604 "Display sunrise and sunset for the cursor date.
6605 Latitude and longitude can be specified with the variables
6606 `calendar-latitude' and `calendar-longitude'. When called with prefix
6607 argument, latitude and longitude will be prompted for."
6608 (interactive "P")
6609 (require 'solar)
6610 (let ((calendar-longitude (if arg nil calendar-longitude))
6611 (calendar-latitude (if arg nil calendar-latitude))
6612 (calendar-location-name
6613 (if arg "the given coordinates" calendar-location-name)))
6614 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
6616 (defun org-agenda-goto-calendar ()
6617 "Open the Emacs calendar with the date at the cursor."
6618 (interactive)
6619 (org-agenda-check-type t 'agenda 'timeline)
6620 (let* ((day (or (get-text-property (point) 'day)
6621 (error "Don't know which date to open in calendar")))
6622 (date (calendar-gregorian-from-absolute day))
6623 (calendar-move-hook nil)
6624 (calendar-view-holidays-initially-flag nil)
6625 (calendar-view-diary-initially-flag nil)
6626 (view-calendar-holidays-initially nil)
6627 (view-diary-entries-initially nil))
6628 (calendar)
6629 (calendar-goto-date date)))
6631 ;;;###autoload
6632 (defun org-calendar-goto-agenda ()
6633 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
6634 This is a command that has to be installed in `calendar-mode-map'."
6635 (interactive)
6636 (org-agenda-list nil (calendar-absolute-from-gregorian
6637 (calendar-cursor-to-date))
6638 nil))
6640 (defun org-agenda-convert-date ()
6641 (interactive)
6642 (org-agenda-check-type t 'agenda 'timeline)
6643 (let ((day (get-text-property (point) 'day))
6644 date s)
6645 (unless day
6646 (error "Don't know which date to convert"))
6647 (setq date (calendar-gregorian-from-absolute day))
6648 (setq s (concat
6649 "Gregorian: " (calendar-date-string date) "\n"
6650 "ISO: " (calendar-iso-date-string date) "\n"
6651 "Day of Yr: " (calendar-day-of-year-string date) "\n"
6652 "Julian: " (calendar-julian-date-string date) "\n"
6653 "Astron. JD: " (calendar-astro-date-string date)
6654 " (Julian date number at noon UTC)\n"
6655 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
6656 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
6657 "French: " (calendar-french-date-string date) "\n"
6658 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
6659 "Mayan: " (calendar-mayan-date-string date) "\n"
6660 "Coptic: " (calendar-coptic-date-string date) "\n"
6661 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
6662 "Persian: " (calendar-persian-date-string date) "\n"
6663 "Chinese: " (calendar-chinese-date-string date) "\n"))
6664 (with-output-to-temp-buffer "*Dates*"
6665 (princ s))
6666 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
6668 ;;; Bulk commands
6670 (defvar org-agenda-bulk-marked-entries nil
6671 "List of markers that refer to marked entries in the agenda.")
6673 (defun org-agenda-bulk-marked-p ()
6674 (eq (get-char-property (point-at-bol) 'type)
6675 'org-marked-entry-overlay))
6677 (defun org-agenda-bulk-mark ()
6678 "Mark the entry at point for future bulk action."
6679 (interactive)
6680 (org-agenda-check-no-diary)
6681 (let* ((m (get-text-property (point) 'org-hd-marker))
6683 (unless (org-agenda-bulk-marked-p)
6684 (unless m (error "Nothing to mark at point"))
6685 (push m org-agenda-bulk-marked-entries)
6686 (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
6687 (org-overlay-display ov "> "
6688 (org-get-todo-face "TODO")
6689 'evaporate)
6690 (org-overlay-put ov 'type 'org-marked-entry-overlay))
6691 (beginning-of-line 2)
6692 (message "%d entries marked for bulk action"
6693 (length org-agenda-bulk-marked-entries))))
6695 (defun org-agenda-bulk-unmark ()
6696 "Unmark the entry at point for future bulk action."
6697 (interactive)
6698 (when (org-agenda-bulk-marked-p)
6699 (org-agenda-bulk-remove-overlays
6700 (point-at-bol) (+ 2 (point-at-bol)))
6701 (setq org-agenda-bulk-marked-entries
6702 (delete (get-text-property (point-at-bol) 'org-hd-marker)
6703 org-agenda-bulk-marked-entries)))
6704 (beginning-of-line 2)
6705 (message "%d entries marked for bulk action"
6706 (length org-agenda-bulk-marked-entries)))
6708 (defun org-agenda-bulk-toggle ()
6709 "Toggle marking the entry at point for bulk action."
6710 (interactive)
6711 (if (org-agenda-bulk-marked-p)
6712 (org-agenda-bulk-unmark)
6713 (org-agenda-bulk-mark)))
6715 (defun org-agenda-bulk-remove-overlays (&optional beg end)
6716 "Remove the mark overlays between BEG and END in the agenda buffer.
6717 BEG and END default to the buffer limits.
6719 This only removes the overlays, it does not remove the markers
6720 from the list in `org-agenda-bulk-marked-entries'."
6721 (interactive)
6722 (mapc (lambda (ov)
6723 (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
6724 (org-delete-overlay ov)))
6725 (org-overlays-in (or beg (point-min)) (or end (point-max)))))
6727 (defun org-agenda-bulk-remove-all-marks ()
6728 "Remove all marks in the agenda buffer.
6729 This will remove the markers, and the overlays."
6730 (interactive)
6731 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
6732 (setq org-agenda-bulk-marked-entries nil)
6733 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
6735 (defun org-agenda-bulk-action ()
6736 "Execute an remote-editing action on all marked entries."
6737 (interactive)
6738 (unless org-agenda-bulk-marked-entries
6739 (error "No entries are marked"))
6740 (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
6741 (let* ((action (read-char-exclusive))
6742 (entries (reverse org-agenda-bulk-marked-entries))
6743 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
6744 (cond
6745 ((equal action ?$)
6746 (setq cmd '(org-agenda-archive)))
6748 ((equal action ?A)
6749 (setq cmd '(org-agenda-archive-to-archive-sibling)))
6751 ((member action '(?r ?w))
6752 (setq rfloc (org-refile-get-location
6753 "Refile to: "
6754 (marker-buffer (car org-agenda-bulk-marked-entries))
6755 org-refile-allow-creating-parent-nodes))
6756 (setcar (nthcdr 3 rfloc)
6757 (move-marker (make-marker) (nth 3 rfloc)
6758 (or (get-file-buffer (nth 1 rfloc))
6759 (find-buffer-visiting (nth 1 rfloc))
6760 (error "This should not happen"))))
6762 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
6764 ((equal action ?t)
6765 (setq state (org-icompleting-read
6766 "Todo state: "
6767 (with-current-buffer (marker-buffer (car entries))
6768 (mapcar 'list org-todo-keywords-1))))
6769 (setq cmd `(let ((org-inhibit-blocking t)
6770 (org-inhibit-logging 'note))
6771 (org-agenda-todo ,state))))
6773 ((memq action '(?- ?+))
6774 (setq tag (org-icompleting-read
6775 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
6776 (with-current-buffer (marker-buffer (car entries))
6777 (delq nil
6778 (mapcar (lambda (x)
6779 (if (stringp (car x)) x)) org-tag-alist)))))
6780 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
6782 ((memq action '(?s ?d))
6783 (let* ((date (org-read-date
6784 nil nil nil
6785 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
6786 (ans org-read-date-final-answer)
6787 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
6788 (setq cmd `(let* ((bound (fboundp 'read-string))
6789 (old (and bound (symbol-function 'read-string))))
6790 (unwind-protect
6791 (progn
6792 (fset 'read-string (lambda (&rest ignore) ,ans))
6793 (call-interactively ',c1))
6794 (if bound
6795 (fset 'read-string old)
6796 (fmakunbound 'read-string)))))))
6797 (t (error "Invalid bulk action")))
6799 ;; Sort the markers, to make sure that parents are handled before children
6800 (setq entries (sort entries
6801 (lambda (a b)
6802 (cond
6803 ((equal (marker-buffer a) (marker-buffer b))
6804 (< (marker-position a) (marker-position b)))
6806 (string< (buffer-name (marker-buffer a))
6807 (buffer-name (marker-buffer b))))))))
6809 ;; Now loop over all markers and apply cmd
6810 (while (setq e (pop entries))
6811 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
6812 (if (not pos)
6813 (progn (message "Skipping removed entry at %s" e)
6814 (setq cntskip (1+ cntskip)))
6815 (goto-char pos)
6816 (eval cmd)
6817 (setq org-agenda-bulk-marked-entries
6818 (delete e org-agenda-bulk-marked-entries))
6819 (setq cnt (1+ cnt))))
6820 (setq org-agenda-bulk-marked-entries nil)
6821 (org-agenda-bulk-remove-all-marks)
6822 (message "Acted on %d entries%s"
6824 (if (= cntskip 0)
6826 (format ", skipped %d (disappeared before their turn)"
6827 cntskip)))))
6829 ;;; Flagging notes
6831 (defun org-agenda-show-the-flagging-note ()
6832 "Display the flagging note in the other window.
6833 When called a second time in direct sequence, offer to remove the FLAGGING
6834 tag and (if present) the flagging note."
6835 (interactive)
6836 (let ((hdmarker (get-text-property (point) 'org-hd-marker))
6837 (win (selected-window))
6838 note heading newhead)
6839 (unless hdmarker
6840 (error "No linked entry at point"))
6841 (if (and (eq this-command last-command)
6842 (y-or-n-p "Unflag and remove any flagging note? "))
6843 (progn
6844 (org-agenda-remove-flag hdmarker)
6845 (let ((win (get-buffer-window "*Flagging Note*")))
6846 (and win (delete-window win)))
6847 (message "Entry unflaged"))
6848 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
6849 (unless note
6850 (error "No flagging note"))
6851 (org-kill-new note)
6852 (org-switch-to-buffer-other-window "*Flagging Note*")
6853 (erase-buffer)
6854 (insert note)
6855 (goto-char (point-min))
6856 (while (re-search-forward "\\\\n" nil t)
6857 (replace-match "\n" t t))
6858 (goto-char (point-min))
6859 (select-window win)
6860 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
6862 (defun org-agenda-remove-flag (marker)
6863 "Remove the FLAGGED tag and any flaging note in the entry."
6864 (let (newhead)
6865 (org-with-point-at marker
6866 (org-toggle-tag "FLAGGED" 'off)
6867 (org-entry-delete nil "THEFLAGGINGNOTE")
6868 (setq newhead (org-get-heading)))
6869 (org-agenda-change-all-lines newhead marker)
6870 (message "Entry unflaged")))
6872 (defun org-agenda-get-any-marker (&optional pos)
6873 (or (get-text-property (or pos (point)) 'org-hd-marker)
6874 (get-text-property (or pos (point)) 'org-marker)))
6876 ;;; Appointment reminders
6878 (defvar appt-time-msg-list)
6880 ;;;###autoload
6881 (defun org-agenda-to-appt (&optional refresh filter)
6882 "Activate appointments found in `org-agenda-files'.
6883 With a \\[universal-argument] prefix, refresh the list of
6884 appointments.
6886 If FILTER is t, interactively prompt the user for a regular
6887 expression, and filter out entries that don't match it.
6889 If FILTER is a string, use this string as a regular expression
6890 for filtering entries out.
6892 FILTER can also be an alist with the car of each cell being
6893 either 'headline or 'category. For example:
6895 '((headline \"IMPORTANT\")
6896 (category \"Work\"))
6898 will only add headlines containing IMPORTANT or headlines
6899 belonging to the \"Work\" category."
6900 (interactive "P")
6901 (require 'calendar)
6902 (if refresh (setq appt-time-msg-list nil))
6903 (if (eq filter t)
6904 (setq filter (read-from-minibuffer "Regexp filter: ")))
6905 (let* ((cnt 0) ; count added events
6906 (org-agenda-new-buffers nil)
6907 (org-deadline-warning-days 0)
6908 (today (org-date-to-gregorian
6909 (time-to-days (current-time))))
6910 (org-agenda-restrict nil)
6911 (files (org-agenda-files 'unrestricted)) entries file)
6912 ;; Get all entries which may contain an appt
6913 (org-prepare-agenda-buffers files)
6914 (while (setq file (pop files))
6915 (setq entries
6916 (append entries
6917 (org-agenda-get-day-entries
6918 file today :timestamp :scheduled :deadline))))
6919 (setq entries (delq nil entries))
6920 ;; Map thru entries and find if we should filter them out
6921 (mapc
6922 (lambda(x)
6923 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
6924 (cat (get-text-property 1 'org-category x))
6925 (tod (get-text-property 1 'time-of-day x))
6926 (ok (or (null filter)
6927 (and (stringp filter) (string-match filter evt))
6928 (and (listp filter)
6929 (or (string-match
6930 (cadr (assoc 'category filter)) cat)
6931 (string-match
6932 (cadr (assoc 'headline filter)) evt))))))
6933 ;; FIXME: Shall we remove text-properties for the appt text?
6934 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
6935 (when (and ok tod)
6936 (setq tod (concat "00" (number-to-string tod))
6937 tod (when (string-match
6938 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
6939 (concat (match-string 1 tod) ":"
6940 (match-string 2 tod))))
6941 (appt-add tod evt)
6942 (setq cnt (1+ cnt))))) entries)
6943 (org-release-buffers org-agenda-new-buffers)
6944 (if (eq cnt 0)
6945 (message "No event to add")
6946 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
6948 (defun org-agenda-todayp (date)
6949 "Does DATE mean today, when considering `org-extend-today-until'?"
6950 (let (today h)
6951 (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
6952 (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
6953 (setq h (nth 2 (decode-time (current-time))))
6954 (or (and (>= h org-extend-today-until)
6955 (= date today))
6956 (and (< h org-extend-today-until)
6957 (= date (1- today))))))
6959 (provide 'org-agenda)
6961 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
6963 ;;; org-agenda.el ends here