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