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