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