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