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