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