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