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