Fix error with %e agenda prefix format when there is no effort set
[org-mode.git] / lisp / org-agenda.el
blob27e73291fb6e1cc33a07c43114def0290f9b2663
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.6
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 (declare-function org-pop-to-buffer-same-window "org-compat"
65 (&optional buffer-or-name norecord label))
67 (defvar calendar-mode-map)
68 (defvar org-clock-current-task) ; defined in org-clock.el
69 (defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
70 (defvar org-habit-show-habits)
71 (defvar org-habit-show-habits-only-for-today)
73 ;; Defined somewhere in this file, but used before definition.
74 (defvar org-agenda-buffer-name)
75 (defvar org-agenda-overriding-header)
76 (defvar org-agenda-title-append nil)
77 (defvar entry)
78 (defvar date)
79 (defvar org-agenda-undo-list)
80 (defvar org-agenda-pending-undo-list)
81 (defvar original-date) ; dynamically scoped, calendar.el does scope this
83 (defcustom org-agenda-confirm-kill 1
84 "When set, remote killing from the agenda buffer needs confirmation.
85 When t, a confirmation is always needed. When a number N, confirmation is
86 only needed when the text to be killed contains more than N non-white lines."
87 :group 'org-agenda
88 :type '(choice
89 (const :tag "Never" nil)
90 (const :tag "Always" t)
91 (integer :tag "When more than N lines")))
93 (defcustom org-agenda-compact-blocks nil
94 "Non-nil means make the block agenda more compact.
95 This is done globally by leaving out lines like the agenda span
96 name and week number or the separator lines."
97 :group 'org-agenda
98 :type 'boolean)
100 (defcustom org-agenda-block-separator ?=
101 "The separator between blocks in the agenda.
102 If this is a string, it will be used as the separator, with a newline added.
103 If it is a character, it will be repeated to fill the window width.
104 If nil the separator is disabled. In `org-agenda-custom-commands' this
105 addresses the separator between the current and the previous block."
106 :group 'org-agenda
107 :type '(choice
108 (const :tag "Disabled" nil)
109 (character)
110 (string)))
112 (defgroup org-agenda-export nil
113 "Options concerning exporting agenda views in Org-mode."
114 :tag "Org Agenda Export"
115 :group 'org-agenda)
117 (defcustom org-agenda-with-colors t
118 "Non-nil means use colors in agenda views."
119 :group 'org-agenda-export
120 :type 'boolean)
122 (defcustom org-agenda-exporter-settings nil
123 "Alist of variable/value pairs that should be active during agenda export.
124 This is a good place to set options for ps-print and for htmlize.
125 Note that the way this is implemented, the values will be evaluated
126 before assigned to the variables. So make sure to quote values you do
127 *not* want evaluated, for example
129 (setq org-agenda-exporter-settings
130 '((ps-print-color-p 'black-white)))"
131 :group 'org-agenda-export
132 :type '(repeat
133 (list
134 (variable)
135 (sexp :tag "Value"))))
137 (defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
138 "Hook run in temporary buffer before writing it to an export file.
139 A useful function is `org-agenda-add-entry-text'."
140 :group 'org-agenda-export
141 :type 'hook
142 :options '(org-agenda-add-entry-text))
144 (defcustom org-agenda-add-entry-text-maxlines 0
145 "Maximum number of entry text lines to be added to agenda.
146 This is only relevant when `org-agenda-add-entry-text' is part of
147 `org-agenda-before-write-hook', which it is by default.
148 When this is 0, nothing will happen. When it is greater than 0, it
149 specifies the maximum number of lines that will be added for each entry
150 that is listed in the agenda view.
152 Note that this variable is not used during display, only when exporting
153 the agenda. For agenda display, see the variables `org-agenda-entry-text-mode'
154 and `org-agenda-entry-text-maxlines'."
155 :group 'org-agenda
156 :type 'integer)
158 (defcustom org-agenda-add-entry-text-descriptive-links t
159 "Non-nil means export org-links as descriptive links in agenda added text.
160 This variable applies to the text added to the agenda when
161 `org-agenda-add-entry-text-maxlines' is larger than 0.
162 When this variable nil, the URL will (also) be shown."
163 :group 'org-agenda
164 :type 'boolean)
166 (defcustom org-agenda-export-html-style ""
167 "The style specification for exported HTML Agenda files.
168 If this variable contains a string, it will replace the default <style>
169 section as produced by `htmlize'.
170 Since there are different ways of setting style information, this variable
171 needs to contain the full HTML structure to provide a style, including the
172 surrounding HTML tags. The style specifications should include definitions
173 the fonts used by the agenda, here is an example:
175 <style type=\"text/css\">
176 p { font-weight: normal; color: gray; }
177 .org-agenda-structure {
178 font-size: 110%;
179 color: #003399;
180 font-weight: 600;
182 .org-todo {
183 color: #cc6666;
184 font-weight: bold;
186 .org-agenda-done {
187 color: #339933;
189 .org-done {
190 color: #339933;
192 .title { text-align: center; }
193 .todo, .deadline { color: red; }
194 .done { color: green; }
195 </style>
197 or, if you want to keep the style in a file,
199 <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
201 As the value of this option simply gets inserted into the HTML <head> header,
202 you can \"misuse\" it to also add other text to the header. However,
203 <style>...</style> is required, if not present the variable will be ignored."
204 :group 'org-agenda-export
205 :group 'org-export-html
206 :type 'string)
208 (defcustom org-agenda-persistent-filter nil
209 "When set, keep filters from one agenda view to the next."
210 :group 'org-agenda
211 :type 'boolean)
213 (defgroup org-agenda-custom-commands nil
214 "Options concerning agenda views in Org-mode."
215 :tag "Org Agenda Custom Commands"
216 :group 'org-agenda)
218 (defconst org-sorting-choice
219 '(choice
220 (const time-up) (const time-down)
221 (const category-keep) (const category-up) (const category-down)
222 (const tag-down) (const tag-up)
223 (const priority-up) (const priority-down)
224 (const todo-state-up) (const todo-state-down)
225 (const effort-up) (const effort-down)
226 (const habit-up) (const habit-down)
227 (const alpha-up) (const alpha-down)
228 (const user-defined-up) (const user-defined-down))
229 "Sorting choices.")
231 (defconst org-agenda-custom-commands-local-options
232 `(repeat :tag "Local settings for this command. Remember to quote values"
233 (choice :tag "Setting"
234 (list :tag "Heading for this block"
235 (const org-agenda-overriding-header)
236 (string :tag "Headline"))
237 (list :tag "Files to be searched"
238 (const org-agenda-files)
239 (list
240 (const :format "" quote)
241 (repeat (file))))
242 (list :tag "Sorting strategy"
243 (const org-agenda-sorting-strategy)
244 (list
245 (const :format "" quote)
246 (repeat
247 ,org-sorting-choice)))
248 (list :tag "Prefix format"
249 (const org-agenda-prefix-format :value " %-12:c%?-12t% s")
250 (string))
251 (list :tag "Number of days in agenda"
252 (const org-agenda-span)
253 (choice (const :tag "Day" 'day)
254 (const :tag "Week" 'week)
255 (const :tag "Month" 'month)
256 (const :tag "Year" 'year)
257 (integer :tag "Custom")))
258 (list :tag "Fixed starting date"
259 (const org-agenda-start-day)
260 (string :value "2007-11-01"))
261 (list :tag "Start on day of week"
262 (const org-agenda-start-on-weekday)
263 (choice :value 1
264 (const :tag "Today" nil)
265 (integer :tag "Weekday No.")))
266 (list :tag "Include data from diary"
267 (const org-agenda-include-diary)
268 (boolean))
269 (list :tag "Deadline Warning days"
270 (const org-deadline-warning-days)
271 (integer :value 1))
272 (list :tag "Tags filter preset"
273 (const org-agenda-filter-preset)
274 (list
275 (const :format "" quote)
276 (repeat
277 (string :tag "+tag or -tag"))))
278 (list :tag "Set daily/weekly entry types"
279 (const org-agenda-entry-types)
280 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
281 (const :deadline)
282 (const :scheduled)
283 (const :timestamp)
284 (const :sexp)))
285 (list :tag "Standard skipping condition"
286 :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
287 (const org-agenda-skip-function)
288 (list
289 (const :format "" quote)
290 (list
291 (choice
292 :tag "Skipping range"
293 (const :tag "Skip entry" org-agenda-skip-entry-if)
294 (const :tag "Skip subtree" org-agenda-skip-subtree-if))
295 (repeat :inline t :tag "Conditions for skipping"
296 (choice
297 :tag "Condition type"
298 (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
299 (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
300 (list :tag "TODO state is" :inline t
301 (const 'todo)
302 (choice
303 (const :tag "any not-done state" 'todo)
304 (const :tag "any done state" 'done)
305 (const :tag "any state" 'any)
306 (list :tag "Keyword list"
307 (const :format "" quote)
308 (repeat (string :tag "Keyword")))))
309 (list :tag "TODO state is not" :inline t
310 (const 'nottodo)
311 (choice
312 (const :tag "any not-done state" 'todo)
313 (const :tag "any done state" 'done)
314 (const :tag "any state" 'any)
315 (list :tag "Keyword list"
316 (const :format "" quote)
317 (repeat (string :tag "Keyword")))))
318 (const :tag "scheduled" 'scheduled)
319 (const :tag "not scheduled" 'notscheduled)
320 (const :tag "deadline" 'deadline)
321 (const :tag "no deadline" 'notdeadline)
322 (const :tag "timestamp" 'timestamp)
323 (const :tag "no timestamp" 'nottimestamp))))))
324 (list :tag "Non-standard skipping condition"
325 :value (org-agenda-skip-function)
326 (const org-agenda-skip-function)
327 (sexp :tag "Function or form (quoted!)"))
328 (list :tag "Any variable"
329 (variable :tag "Variable")
330 (sexp :tag "Value (sexp)"))))
331 "Selection of examples for agenda command settings.
332 This will be spliced into the custom type of
333 `org-agenda-custom-commands'.")
336 (defcustom org-agenda-custom-commands nil
337 "Custom commands for the agenda.
338 These commands will be offered on the splash screen displayed by the
339 agenda dispatcher \\[org-agenda]. Each entry is a list like this:
341 (key desc type match settings files)
343 key The key (one or more characters as a string) to be associated
344 with the command.
345 desc A description of the command, when omitted or nil, a default
346 description is built using MATCH.
347 type The command type, any of the following symbols:
348 agenda The daily/weekly agenda.
349 todo Entries with a specific TODO keyword, in all agenda files.
350 search Entries containing search words entry or headline.
351 tags Tags/Property/TODO match in all agenda files.
352 tags-todo Tags/P/T match in all agenda files, TODO entries only.
353 todo-tree Sparse tree of specific TODO keyword in *current* file.
354 tags-tree Sparse tree with all tags matches in *current* file.
355 occur-tree Occur sparse tree for *current* file.
356 ... A user-defined function.
357 match What to search for:
358 - a single keyword for TODO keyword searches
359 - a tags match expression for tags searches
360 - a word search expression for text searches.
361 - a regular expression for occur searches
362 For all other commands, this should be the empty string.
363 settings A list of option settings, similar to that in a let form, so like
364 this: ((opt1 val1) (opt2 val2) ...). The values will be
365 evaluated at the moment of execution, so quote them when needed.
366 files A list of files file to write the produced agenda buffer to
367 with the command `org-store-agenda-views'.
368 If a file name ends in \".html\", an HTML version of the buffer
369 is written out. If it ends in \".ps\", a postscript version is
370 produced. Otherwise, only the plain text is written to the file.
372 You can also define a set of commands, to create a composite agenda buffer.
373 In this case, an entry looks like this:
375 (key desc (cmd1 cmd2 ...) general-settings-for-whole-set files)
377 where
379 desc A description string to be displayed in the dispatcher menu.
380 cmd An agenda command, similar to the above. However, tree commands
381 are no allowed, but instead you can get agenda and global todo list.
382 So valid commands for a set are:
383 (agenda \"\" settings)
384 (alltodo \"\" settings)
385 (stuck \"\" settings)
386 (todo \"match\" settings files)
387 (search \"match\" settings files)
388 (tags \"match\" settings files)
389 (tags-todo \"match\" settings files)
391 Each command can carry a list of options, and another set of options can be
392 given for the whole set of commands. Individual command options take
393 precedence over the general options.
395 When using several characters as key to a command, the first characters
396 are prefix commands. For the dispatcher to display useful information, you
397 should provide a description for the prefix, like
399 (setq org-agenda-custom-commands
400 '((\"h\" . \"HOME + Name tag searches\") ; describe prefix \"h\"
401 (\"hl\" tags \"+HOME+Lisa\")
402 (\"hp\" tags \"+HOME+Peter\")
403 (\"hk\" tags \"+HOME+Kim\")))"
404 :group 'org-agenda-custom-commands
405 :type `(repeat
406 (choice :value ("x" "Describe command here" tags "" nil)
407 (list :tag "Single command"
408 (string :tag "Access Key(s) ")
409 (option (string :tag "Description"))
410 (choice
411 (const :tag "Agenda" agenda)
412 (const :tag "TODO list" alltodo)
413 (const :tag "Search words" search)
414 (const :tag "Stuck projects" stuck)
415 (const :tag "Tags/Property match (all agenda files)" tags)
416 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
417 (const :tag "TODO keyword search (all agenda files)" todo)
418 (const :tag "Tags sparse tree (current buffer)" tags-tree)
419 (const :tag "TODO keyword tree (current buffer)" todo-tree)
420 (const :tag "Occur tree (current buffer)" occur-tree)
421 (sexp :tag "Other, user-defined function"))
422 (string :tag "Match (only for some commands)")
423 ,org-agenda-custom-commands-local-options
424 (option (repeat :tag "Export" (file :tag "Export to"))))
425 (list :tag "Command series, all agenda files"
426 (string :tag "Access Key(s)")
427 (string :tag "Description ")
428 (repeat :tag "Component"
429 (choice
430 (list :tag "Agenda"
431 (const :format "" agenda)
432 (const :tag "" :format "" "")
433 ,org-agenda-custom-commands-local-options)
434 (list :tag "TODO list (all keywords)"
435 (const :format "" alltodo)
436 (const :tag "" :format "" "")
437 ,org-agenda-custom-commands-local-options)
438 (list :tag "Search words"
439 (const :format "" search)
440 (string :tag "Match")
441 ,org-agenda-custom-commands-local-options)
442 (list :tag "Stuck projects"
443 (const :format "" stuck)
444 (const :tag "" :format "" "")
445 ,org-agenda-custom-commands-local-options)
446 (list :tag "Tags search"
447 (const :format "" tags)
448 (string :tag "Match")
449 ,org-agenda-custom-commands-local-options)
450 (list :tag "Tags search, TODO entries only"
451 (const :format "" tags-todo)
452 (string :tag "Match")
453 ,org-agenda-custom-commands-local-options)
454 (list :tag "TODO keyword search"
455 (const :format "" todo)
456 (string :tag "Match")
457 ,org-agenda-custom-commands-local-options)
458 (list :tag "Other, user-defined function"
459 (symbol :tag "function")
460 (string :tag "Match")
461 ,org-agenda-custom-commands-local-options)))
463 (repeat :tag "Settings for entire command set"
464 (list (variable :tag "Any variable")
465 (sexp :tag "Value")))
466 (option (repeat :tag "Export" (file :tag "Export to"))))
467 (cons :tag "Prefix key documentation"
468 (string :tag "Access Key(s)")
469 (string :tag "Description ")))))
471 (defcustom org-agenda-query-register ?o
472 "The register holding the current query string.
473 The purpose of this is that if you construct a query string interactively,
474 you can then use it to define a custom command."
475 :group 'org-agenda-custom-commands
476 :type 'character)
478 (defcustom org-stuck-projects
479 '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
480 "How to identify stuck projects.
481 This is a list of four items:
482 1. A tags/todo/property matcher string that is used to identify a project.
483 See the manual for a description of tag and property searches.
484 The entire tree below a headline matched by this is considered one project.
485 2. A list of TODO keywords identifying non-stuck projects.
486 If the project subtree contains any headline with one of these todo
487 keywords, the project is considered to be not stuck. If you specify
488 \"*\" as a keyword, any TODO keyword will mark the project unstuck.
489 3. A list of tags identifying non-stuck projects.
490 If the project subtree contains any headline with one of these tags,
491 the project is considered to be not stuck. If you specify \"*\" as
492 a tag, any tag will mark the project unstuck. Note that this is about
493 the explicit presence of a tag somewhere in the subtree, inherited
494 tags to not count here. If inherited tags make a project not stuck,
495 use \"-TAG\" in the tags part of the matcher under (1.) above.
496 4. An arbitrary regular expression matching non-stuck projects.
498 If the project turns out to be not stuck, search continues also in the
499 subtree to see if any of the subtasks have project status.
501 See also the variable `org-tags-match-list-sublevels' which applies
502 to projects matched by this search as well.
504 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
505 or `C-c a #' to produce the list."
506 :group 'org-agenda-custom-commands
507 :type '(list
508 (string :tag "Tags/TODO match to identify a project")
509 (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
510 (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
511 (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
513 (defcustom org-agenda-filter-effort-default-operator "<"
514 "The default operator for effort estimate filtering.
515 If you select an effort estimate limit without first pressing an operator,
516 this one will be used."
517 :group 'org-agenda-custom-commands
518 :type '(choice (const :tag "less or equal" "<")
519 (const :tag "greater or equal"">")
520 (const :tag "equal" "=")))
522 (defgroup org-agenda-skip nil
523 "Options concerning skipping parts of agenda files."
524 :tag "Org Agenda Skip"
525 :group 'org-agenda)
527 (defcustom org-agenda-skip-function-global nil
528 "Function to be called at each match during agenda construction.
529 If this function returns nil, the current match should not be skipped.
530 If the function decided to skip an agenda match, is must return the
531 buffer position from which the search should be continued.
532 This may also be a Lisp form, which will be evaluated.
534 This variable will be applied to every agenda match, including
535 tags/property searches and TODO lists. So try to make the test function
536 do its checking as efficiently as possible. To implement a skipping
537 condition just for specific agenda commands, use the variable
538 `org-agenda-skip-function' which can be set in the options section
539 of custom agenda commands."
540 :group 'org-agenda-skip
541 :type 'sexp)
543 (defgroup org-agenda-daily/weekly nil
544 "Options concerning the daily/weekly agenda."
545 :tag "Org Agenda Daily/Weekly"
546 :group 'org-agenda)
547 (defgroup org-agenda-todo-list nil
548 "Options concerning the global todo list agenda view."
549 :tag "Org Agenda Todo List"
550 :group 'org-agenda)
551 (defgroup org-agenda-match-view nil
552 "Options concerning the general tags/property/todo match agenda view."
553 :tag "Org Agenda Match View"
554 :group 'org-agenda)
555 (defgroup org-agenda-search-view nil
556 "Options concerning the general tags/property/todo match agenda view."
557 :tag "Org Agenda Match View"
558 :group 'org-agenda)
560 (defvar org-agenda-archives-mode nil
561 "Non-nil means the agenda will include archived items.
562 If this is the symbol `trees', trees in the selected agenda scope
563 that are marked with the ARCHIVE tag will be included anyway. When this is
564 t, also all archive files associated with the current selection of agenda
565 files will be included.")
567 (defcustom org-agenda-skip-comment-trees t
568 "Non-nil means skip trees that start with the COMMENT keyword.
569 When nil, these trees are also scanned by agenda commands."
570 :group 'org-agenda-skip
571 :type 'boolean)
573 (defcustom org-agenda-todo-list-sublevels t
574 "Non-nil means check also the sublevels of a TODO entry for TODO entries.
575 When nil, the sublevels of a TODO entry are not checked, resulting in
576 potentially much shorter TODO lists."
577 :group 'org-agenda-skip
578 :group 'org-agenda-todo-list
579 :type 'boolean)
581 (defcustom org-agenda-todo-ignore-with-date nil
582 "Non-nil means don't show entries with a date in the global todo list.
583 You can use this if you prefer to mark mere appointments with a TODO keyword,
584 but don't want them to show up in the TODO list.
585 When this is set, it also covers deadlines and scheduled items, the settings
586 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
587 will be ignored.
588 See also the variable `org-agenda-tags-todo-honor-ignore-options'."
589 :group 'org-agenda-skip
590 :group 'org-agenda-todo-list
591 :type 'boolean)
593 (defcustom org-agenda-todo-ignore-timestamp nil
594 "Non-nil means don't show entries with a timestamp.
595 This applies when creating the global todo list.
596 Valid values are:
598 past Don't show entries for today or in the past.
600 future Don't show entries with a timestamp in the future.
601 The idea behind this is that if it has a future
602 timestamp, you don't want to think about it until the
603 date.
605 all Don't show any entries with a timestamp in the global todo list.
606 The idea behind this is that by setting a timestamp, you
607 have already \"taken care\" of this item.
609 This variable can also have an integer as a value. If positive (N),
610 todos with a timestamp N or more days in the future will be ignored. If
611 negative (-N), todos with a timestamp N or more days in the past will be
612 ignored. If 0, todos with a timestamp either today or in the future will
613 be ignored. For example, a value of -1 will exclude todos with a
614 timestamp in the past (yesterday or earlier), while a value of 7 will
615 exclude todos with a timestamp a week or more in the future.
617 See also `org-agenda-todo-ignore-with-date'.
618 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
619 to make his option also apply to the tags-todo list."
620 :group 'org-agenda-skip
621 :group 'org-agenda-todo-list
622 :type '(choice
623 (const :tag "Ignore future timestamp todos" future)
624 (const :tag "Ignore past or present timestamp todos" past)
625 (const :tag "Ignore all timestamp todos" all)
626 (const :tag "Show timestamp todos" nil)
627 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
629 (defcustom org-agenda-todo-ignore-scheduled nil
630 "Non-nil means, ignore some scheduled TODO items when making TODO list.
631 This applies when creating the global todo list.
632 Valid values are:
634 past Don't show entries scheduled today or in the past.
636 future Don't show entries scheduled in the future.
637 The idea behind this is that by scheduling it, you don't want to
638 think about it until the scheduled date.
640 all Don't show any scheduled entries in the global todo list.
641 The idea behind this is that by scheduling it, you have already
642 \"taken care\" of this item.
644 t Same as `all', for backward compatibility.
646 This variable can also have an integer as a value. See
647 `org-agenda-todo-ignore-timestamp' for more details.
649 See also `org-agenda-todo-ignore-with-date'.
650 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
651 to make his option also apply to the tags-todo list."
652 :group 'org-agenda-skip
653 :group 'org-agenda-todo-list
654 :type '(choice
655 (const :tag "Ignore future-scheduled todos" future)
656 (const :tag "Ignore past- or present-scheduled todos" past)
657 (const :tag "Ignore all scheduled todos" all)
658 (const :tag "Ignore all scheduled todos (compatibility)" t)
659 (const :tag "Show scheduled todos" nil)
660 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
662 (defcustom org-agenda-todo-ignore-deadlines nil
663 "Non-nil means ignore some deadlined TODO items when making TODO list.
664 There are different motivations for using different values, please think
665 carefully when configuring this variable.
667 This applies when creating the global todo list.
668 Valid values are:
670 near Don't show near deadline entries. A deadline is near when it is
671 closer than `org-deadline-warning-days' days. The idea behind this
672 is that such items will appear in the agenda anyway.
674 far Don't show TODO entries where a deadline has been defined, but
675 the deadline is not near. This is useful if you don't want to
676 use the todo list to figure out what to do now.
678 past Don't show entries with a deadline timestamp for today or in the past.
680 future Don't show entries with a deadline timestamp in the future, not even
681 when they become `near' ones. Use it with caution.
683 all Ignore all TODO entries that do have a deadline.
685 t Same as `near', for backward compatibility.
687 This variable can also have an integer as a value. See
688 `org-agenda-todo-ignore-timestamp' for more details.
690 See also `org-agenda-todo-ignore-with-date'.
691 See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
692 to make his option also apply to the tags-todo list."
693 :group 'org-agenda-skip
694 :group 'org-agenda-todo-list
695 :type '(choice
696 (const :tag "Ignore near deadlines" near)
697 (const :tag "Ignore near deadlines (compatibility)" t)
698 (const :tag "Ignore far deadlines" far)
699 (const :tag "Ignore all TODOs with a deadlines" all)
700 (const :tag "Show all TODOs, even if they have a deadline" nil)
701 (integer :tag "Ignore if N or more days in past(-) or future(+).")))
703 (defcustom org-agenda-tags-todo-honor-ignore-options nil
704 "Non-nil means honor todo-list ...ignore options also in tags-todo search.
705 The variables
706 `org-agenda-todo-ignore-with-date',
707 `org-agenda-todo-ignore-timestamp',
708 `org-agenda-todo-ignore-scheduled',
709 `org-agenda-todo-ignore-deadlines'
710 make the global TODO list skip entries that have time stamps of certain
711 kinds. If this option is set, the same options will also apply for the
712 tags-todo search, which is the general tags/property matcher
713 restricted to unfinished TODO entries only."
714 :group 'org-agenda-skip
715 :group 'org-agenda-todo-list
716 :group 'org-agenda-match-view
717 :type 'boolean)
719 (defcustom org-agenda-skip-scheduled-if-done nil
720 "Non-nil means don't show scheduled items in agenda when they are done.
721 This is relevant for the daily/weekly agenda, not for the TODO list. And
722 it applies only to the actual date of the scheduling. Warnings about
723 an item with a past scheduling dates are always turned off when the item
724 is DONE."
725 :group 'org-agenda-skip
726 :group 'org-agenda-daily/weekly
727 :type 'boolean)
729 (defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
730 "Non-nil means skip scheduling line if same entry shows because of deadline.
731 In the agenda of today, an entry can show up multiple times because
732 it is both scheduled and has a nearby deadline, and maybe a plain time
733 stamp as well.
734 When this variable is t, then only the deadline is shown and the fact that
735 the entry is scheduled today or was scheduled previously is not shown.
736 When this variable is nil, the entry will be shown several times. When
737 the variable is the symbol `not-today', then skip scheduled previously,
738 but not scheduled today."
739 :group 'org-agenda-skip
740 :group 'org-agenda-daily/weekly
741 :type '(choice
742 (const :tag "Never" nil)
743 (const :tag "Always" t)
744 (const :tag "Not when scheduled today" not-today)))
746 (defcustom org-agenda-skip-deadline-if-done nil
747 "Non-nil means don't show deadlines when the corresponding item is done.
748 When nil, the deadline is still shown and should give you a happy feeling.
749 This is relevant for the daily/weekly agenda. And it applied only to the
750 actually date of the deadline. Warnings about approaching and past-due
751 deadlines are always turned off when the item is DONE."
752 :group 'org-agenda-skip
753 :group 'org-agenda-daily/weekly
754 :type 'boolean)
756 (defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
757 "Non-nil means skip deadline prewarning when entry is also scheduled.
758 This will apply on all days where a prewarning for the deadline would
759 be shown, but not at the day when the entry is actually due. On that day,
760 the deadline will be shown anyway.
761 This variable may be set to nil, t, or a number which will then give
762 the number of days before the actual deadline when the prewarnings
763 should resume.
764 This can be used in a workflow where the first showing of the deadline will
765 trigger you to schedule it, and then you don't want to be reminded of it
766 because you will take care of it on the day when scheduled."
767 :group 'org-agenda-skip
768 :group 'org-agenda-daily/weekly
769 :type '(choice
770 (const :tag "Alwas show prewarning" nil)
771 (const :tag "Remove prewarning if entry is scheduled" t)
772 (integer :tag "Restart prewarning N days before deadline")))
774 (defcustom org-agenda-skip-additional-timestamps-same-entry t
775 "When nil, multiple same-day timestamps in entry make multiple agenda lines.
776 When non-nil, after the search for timestamps has matched once in an
777 entry, the rest of the entry will not be searched."
778 :group 'org-agenda-skip
779 :type 'boolean)
781 (defcustom org-agenda-skip-timestamp-if-done nil
782 "Non-nil means don't select item by timestamp or -range if it is DONE."
783 :group 'org-agenda-skip
784 :group 'org-agenda-daily/weekly
785 :type 'boolean)
787 (defcustom org-agenda-dim-blocked-tasks t
788 "Non-nil means dim blocked tasks in the agenda display.
789 This causes some overhead during agenda construction, but if you
790 have turned on `org-enforce-todo-dependencies',
791 `org-enforce-todo-checkbox-dependencies', or any other blocking
792 mechanism, this will create useful feedback in the agenda.
794 Instead of t, this variable can also have the value `invisible'.
795 Then blocked tasks will be invisible and only become visible when
796 they become unblocked. An exemption to this behavior is when a task is
797 blocked because of unchecked checkboxes below it. Since checkboxes do
798 not show up in the agenda views, making this task invisible you remove any
799 trace from agenda views that there is something to do. Therefore, a task
800 that is blocked because of checkboxes will never be made invisible, it
801 will only be dimmed."
802 :group 'org-agenda-daily/weekly
803 :group 'org-agenda-todo-list
804 :type '(choice
805 (const :tag "Do not dim" nil)
806 (const :tag "Dim to a grey face" t)
807 (const :tag "Make invisible" invisible)))
809 (defcustom org-timeline-show-empty-dates 3
810 "Non-nil means `org-timeline' also shows dates without an entry.
811 When nil, only the days which actually have entries are shown.
812 When t, all days between the first and the last date are shown.
813 When an integer, show also empty dates, but if there is a gap of more than
814 N days, just insert a special line indicating the size of the gap."
815 :group 'org-agenda-skip
816 :type '(choice
817 (const :tag "None" nil)
818 (const :tag "All" t)
819 (integer :tag "at most")))
821 (defgroup org-agenda-startup nil
822 "Options concerning initial settings in the Agenda in Org Mode."
823 :tag "Org Agenda Startup"
824 :group 'org-agenda)
826 (defcustom org-agenda-menu-show-matcher t
827 "Non-nil means show the match string in the agenda dispatcher menu.
828 When nil, the matcher string is not shown, but is put into the help-echo
829 property so than moving the mouse over the command shows it.
830 Setting it to nil is good if matcher strings are very long and/or if
831 you want to use two-column display (see `org-agenda-menu-two-column')."
832 :group 'org-agenda
833 :type 'boolean)
835 (defcustom org-agenda-menu-two-column nil
836 "Non-nil means, use two columns to show custom commands in the dispatcher.
837 If you use this, you probably want to set `org-agenda-menu-show-matcher'
838 to nil."
839 :group 'org-agenda
840 :type 'boolean)
842 (defcustom org-finalize-agenda-hook nil
843 "Hook run just before displaying an agenda buffer."
844 :group 'org-agenda-startup
845 :type 'hook)
847 (defcustom org-agenda-mouse-1-follows-link nil
848 "Non-nil means mouse-1 on a link will follow the link in the agenda.
849 A longer mouse click will still set point. Does not work on XEmacs.
850 Needs to be set before org.el is loaded."
851 :group 'org-agenda-startup
852 :type 'boolean)
854 (defcustom org-agenda-start-with-follow-mode nil
855 "The initial value of follow mode in a newly created agenda window."
856 :group 'org-agenda-startup
857 :type 'boolean)
859 (defcustom org-agenda-show-outline-path t
860 "Non-nil means show outline path in echo area after line motion."
861 :group 'org-agenda-startup
862 :type 'boolean)
864 (defcustom org-agenda-start-with-entry-text-mode nil
865 "The initial value of entry-text-mode in a newly created agenda window."
866 :group 'org-agenda-startup
867 :type 'boolean)
869 (defcustom org-agenda-entry-text-maxlines 5
870 "Number of text lines to be added when `E' is pressed in the agenda.
872 Note that this variable only used during agenda display. Add add entry text
873 when exporting the agenda, configure the variable
874 `org-agenda-add-entry-ext-maxlines'."
875 :group 'org-agenda
876 :type 'integer)
878 (defcustom org-agenda-entry-text-exclude-regexps nil
879 "List of regular expressions to clean up entry text.
880 The complete matches of all regular expressions in this list will be
881 removed from entry text before it is shown in the agenda."
882 :group 'org-agenda
883 :type '(repeat (regexp)))
885 (defvar org-agenda-entry-text-cleanup-hook nil
886 "Hook that is run after basic cleanup of entry text to be shown in agenda.
887 This cleanup is done in a temporary buffer, so the function may inspect and
888 change the entire buffer.
889 Some default stuff like drawers and scheduling/deadline dates will already
890 have been removed when this is called, as will any matches for regular
891 expressions listed in `org-agenda-entry-text-exclude-regexps'.")
893 (defvar org-agenda-include-inactive-timestamps nil
894 "Non-nil means include inactive time stamps in agenda and timeline.")
896 (defgroup org-agenda-windows nil
897 "Options concerning the windows used by the Agenda in Org Mode."
898 :tag "Org Agenda Windows"
899 :group 'org-agenda)
901 (defcustom org-agenda-window-setup 'reorganize-frame
902 "How the agenda buffer should be displayed.
903 Possible values for this option are:
905 current-window Show agenda in the current window, keeping all other windows.
906 other-window Use `switch-to-buffer-other-window' to display agenda.
907 reorganize-frame Show only two windows on the current frame, the current
908 window and the agenda.
909 other-frame Use `switch-to-buffer-other-frame' to display agenda.
910 Also, when exiting the agenda, kill that frame.
911 See also the variable `org-agenda-restore-windows-after-quit'."
912 :group 'org-agenda-windows
913 :type '(choice
914 (const current-window)
915 (const other-frame)
916 (const other-window)
917 (const reorganize-frame)))
919 (defcustom org-agenda-window-frame-fractions '(0.5 . 0.75)
920 "The min and max height of the agenda window as a fraction of frame height.
921 The value of the variable is a cons cell with two numbers between 0 and 1.
922 It only matters if `org-agenda-window-setup' is `reorganize-frame'."
923 :group 'org-agenda-windows
924 :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
926 (defcustom org-agenda-restore-windows-after-quit nil
927 "Non-nil means restore window configuration upon exiting agenda.
928 Before the window configuration is changed for displaying the agenda,
929 the current status is recorded. When the agenda is exited with
930 `q' or `x' and this option is set, the old state is restored. If
931 `org-agenda-window-setup' is `other-frame', the value of this
932 option will be ignored."
933 :group 'org-agenda-windows
934 :type 'boolean)
936 (defcustom org-agenda-ndays nil
937 "Number of days to include in overview display.
938 Should be 1 or 7.
939 Obsolete, see `org-agenda-span'."
940 :group 'org-agenda-daily/weekly
941 :type 'integer)
943 (make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
945 (defcustom org-agenda-span 'week
946 "Number of days to include in overview display.
947 Can be day, week, month, year, or any number of days.
948 Custom commands can set this variable in the options section."
949 :group 'org-agenda-daily/weekly
950 :type '(choice (const :tag "Day" day)
951 (const :tag "Week" week)
952 (const :tag "Month" month)
953 (const :tag "Year" year)
954 (integer :tag "Custom")))
956 (defcustom org-agenda-start-on-weekday 1
957 "Non-nil means start the overview always on the specified weekday.
958 0 denotes Sunday, 1 denotes Monday etc.
959 When nil, always start on the current day.
960 Custom commands can set this variable in the options section."
961 :group 'org-agenda-daily/weekly
962 :type '(choice (const :tag "Today" nil)
963 (integer :tag "Weekday No.")))
965 (defcustom org-agenda-show-all-dates t
966 "Non-nil means `org-agenda' shows every day in the selected range.
967 When nil, only the days which actually have entries are shown."
968 :group 'org-agenda-daily/weekly
969 :type 'boolean)
971 (defcustom org-agenda-format-date 'org-agenda-format-date-aligned
972 "Format string for displaying dates in the agenda.
973 Used by the daily/weekly agenda and by the timeline. This should be
974 a format string understood by `format-time-string', or a function returning
975 the formatted date as a string. The function must take a single argument,
976 a calendar-style date list like (month day year)."
977 :group 'org-agenda-daily/weekly
978 :type '(choice
979 (string :tag "Format string")
980 (function :tag "Function")))
982 (defun org-agenda-format-date-aligned (date)
983 "Format a date string for display in the daily/weekly agenda, or timeline.
984 This function makes sure that dates are aligned for easy reading."
985 (require 'cal-iso)
986 (let* ((dayname (calendar-day-name date))
987 (day (cadr date))
988 (day-of-week (calendar-day-of-week date))
989 (month (car date))
990 (monthname (calendar-month-name month))
991 (year (nth 2 date))
992 (iso-week (org-days-to-iso-week
993 (calendar-absolute-from-gregorian date)))
994 (weekyear (cond ((and (= month 1) (>= iso-week 52))
995 (1- year))
996 ((and (= month 12) (<= iso-week 1))
997 (1+ year))
998 (t year)))
999 (weekstring (if (= day-of-week 1)
1000 (format " W%02d" iso-week)
1001 "")))
1002 (format "%-10s %2d %s %4d%s"
1003 dayname day monthname year weekstring)))
1005 (defcustom org-agenda-time-leading-zero nil
1006 "Non-nil means use leading zero for military times in agenda.
1007 For example, 9:30am would become 09:30 rather than 9:30."
1008 :group 'org-agenda-daily/weekly
1009 :type 'boolean)
1011 (defcustom org-agenda-timegrid-use-ampm nil
1012 "When set, show AM/PM style timestamps on the timegrid."
1013 :group 'org-agenda
1014 :type 'boolean)
1016 (defun org-agenda-time-of-day-to-ampm (time)
1017 "Convert TIME of a string like '13:45' to an AM/PM style time string."
1018 (let* ((hour-number (string-to-number (substring time 0 -3)))
1019 (minute (substring time -2))
1020 (ampm "am"))
1021 (cond
1022 ((equal hour-number 12)
1023 (setq ampm "pm"))
1024 ((> hour-number 12)
1025 (setq ampm "pm")
1026 (setq hour-number (- hour-number 12))))
1027 (concat
1028 (if org-agenda-time-leading-zero
1029 (format "%02d" hour-number)
1030 (format "%02s" (number-to-string hour-number)))
1031 ":" minute ampm)))
1033 (defun org-agenda-time-of-day-to-ampm-maybe (time)
1034 "Conditionally convert TIME to AM/PM format
1035 based on `org-agenda-timegrid-use-ampm'"
1036 (if org-agenda-timegrid-use-ampm
1037 (org-agenda-time-of-day-to-ampm time)
1038 time))
1040 (defcustom org-agenda-weekend-days '(6 0)
1041 "Which days are weekend?
1042 These days get the special face `org-agenda-date-weekend' in the agenda
1043 and timeline buffers."
1044 :group 'org-agenda-daily/weekly
1045 :type '(set :greedy t
1046 (const :tag "Monday" 1)
1047 (const :tag "Tuesday" 2)
1048 (const :tag "Wednesday" 3)
1049 (const :tag "Thursday" 4)
1050 (const :tag "Friday" 5)
1051 (const :tag "Saturday" 6)
1052 (const :tag "Sunday" 0)))
1054 (defcustom org-agenda-include-diary nil
1055 "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
1056 Custom commands can set this variable in the options section."
1057 :group 'org-agenda-daily/weekly
1058 :type 'boolean)
1060 (defcustom org-agenda-include-deadlines t
1061 "If non-nil, include entries within their deadline warning period.
1062 Custom commands can set this variable in the options section."
1063 :group 'org-agenda-daily/weekly
1064 :type 'boolean)
1066 (defcustom org-agenda-include-all-todo nil
1067 "Set means weekly/daily agenda will always contain all TODO entries.
1068 The TODO entries will be listed at the top of the agenda, before
1069 the entries for specific days.
1070 This option is deprecated, it is better to define a block agenda instead."
1071 :group 'org-agenda-daily/weekly
1072 :type 'boolean)
1074 (defcustom org-agenda-repeating-timestamp-show-all t
1075 "Non-nil means show all occurrences of a repeating stamp in the agenda.
1076 When set to a list of strings, only show occurrences of repeating
1077 stamps for these TODO keywords. When nil, only one occurrence is
1078 shown, either today or the nearest into the future."
1079 :group 'org-agenda-daily/weekly
1080 :type '(choice
1081 (const :tag "Show repeating stamps" t)
1082 (repeat :tag "Show repeating stamps for these TODO keywords"
1083 (string :tag "TODO Keyword"))
1084 (const :tag "Don't show repeating stamps" nil)))
1086 (defcustom org-scheduled-past-days 10000
1087 "No. of days to continue listing scheduled items that are not marked DONE.
1088 When an item is scheduled on a date, it shows up in the agenda on this
1089 day and will be listed until it is marked done for the number of days
1090 given here."
1091 :group 'org-agenda-daily/weekly
1092 :type 'integer)
1094 (defcustom org-agenda-log-mode-items '(closed clock)
1095 "List of items that should be shown in agenda log mode.
1096 This list may contain the following symbols:
1098 closed Show entries that have been closed on that day.
1099 clock Show entries that have received clocked time on that day.
1100 state Show all logged state changes.
1101 Note that instead of changing this variable, you can also press `C-u l' in
1102 the agenda to display all available LOG items temporarily."
1103 :group 'org-agenda-daily/weekly
1104 :type '(set :greedy t (const closed) (const clock) (const state)))
1106 (defcustom org-agenda-clock-consistency-checks
1107 '(:max-duration "10:00" :min-duration 0 :max-gap "0:05"
1108 :gap-ok-around ("4:00")
1109 :default-face ((:background "DarkRed") (:foreground "white"))
1110 :overlap-face nil :gap-face nil :no-end-time-face nil
1111 :long-face nil :short-face nil)
1112 "This is a property list, with the following keys:
1114 :max-duration Mark clocking chunks that are longer than this time.
1115 This is a time string like \"HH:MM\", or the number
1116 of minutes as an integer.
1118 :min-duration Mark clocking chunks that are shorter that this.
1119 This is a time string like \"HH:MM\", or the number
1120 of minutes as an integer.
1122 :max-gap Mark gaps between clocking chunks that are longer than
1123 this duration. A number of minutes, or a string
1124 like \"HH:MM\".
1126 :gap-ok-around List of times during the day which are usually not working
1127 times. When a gap is detected, but the gap contains any
1128 of these times, the gap is *not* reported. For example,
1129 if this is (\"4:00\" \"13:00\") then gaps that contain
1130 4:00 in the morning (i.e. the night) and 13:00
1131 (i.e. a typical lunch time) do not cause a warning.
1132 You should have at least one time during the night in this
1133 list, or otherwise the first task each morning will trigger
1134 a warning because it follows a long gap.
1136 Furthermore, the following properties can be used to define faces for
1137 issue display.
1139 :default-face the default face, if the specific face is undefined
1140 :overlap-face face for overlapping clocks
1141 :gap-face face for gaps between clocks
1142 :no-end-time-face face for incomplete clocks
1143 :long-face face for clock intervals that are too long
1144 :short-face face for clock intervals that are too short"
1145 :group 'org-agenda-daily/weekly
1146 :group 'org-clock
1147 :type 'plist)
1149 (defcustom org-agenda-log-mode-add-notes t
1150 "Non-nil means add first line of notes to log entries in agenda views.
1151 If a log item like a state change or a clock entry is associated with
1152 notes, the first line of these notes will be added to the entry in the
1153 agenda display."
1154 :group 'org-agenda-daily/weekly
1155 :type 'boolean)
1157 (defcustom org-agenda-start-with-log-mode nil
1158 "The initial value of log-mode in a newly created agenda window."
1159 :group 'org-agenda-startup
1160 :group 'org-agenda-daily/weekly
1161 :type 'boolean)
1163 (defcustom org-agenda-start-with-clockreport-mode nil
1164 "The initial value of clockreport-mode in a newly created agenda window."
1165 :group 'org-agenda-startup
1166 :group 'org-agenda-daily/weekly
1167 :type 'boolean)
1169 (defcustom org-agenda-clockreport-parameter-plist '(:link t :maxlevel 2)
1170 "Property list with parameters for the clocktable in clockreport mode.
1171 This is the display mode that shows a clock table in the daily/weekly
1172 agenda, the properties for this dynamic block can be set here.
1173 The usual clocktable parameters are allowed here, but you cannot set
1174 the properties :name, :tstart, :tend, :block, and :scope - these will
1175 be overwritten to make sure the content accurately reflects the
1176 current display in the agenda."
1177 :group 'org-agenda-daily/weekly
1178 :type 'plist)
1180 (defcustom org-agenda-search-view-always-boolean nil
1181 "Non-nil means the search string is interpreted as individual parts.
1183 The search string for search view can either be interpreted as a phrase,
1184 or as a list of snippets that define a boolean search for a number of
1185 strings.
1187 When this is non-nil, the string will be split on whitespace, and each
1188 snippet will be searched individually, and all must match in order to
1189 select an entry. A snippet is then a single string of non-white
1190 characters, or a string in double quotes, or a regexp in {} braces.
1191 If a snippet is preceded by \"-\", the snippet must *not* match.
1192 \"+\" is syntactic sugar for positive selection. Each snippet may
1193 be found as a full word or a partial word, but see the variable
1194 `org-agenda-search-view-force-full-words'.
1196 When this is nil, search will look for the entire search phrase as one,
1197 with each space character matching any amount of whitespace, including
1198 line breaks.
1200 Even when this is nil, you can still switch to Boolean search dynamically
1201 by preceding the first snippet with \"+\" or \"-\". If the first snippet
1202 is a regexp marked with braces like \"{abc}\", this will also switch to
1203 boolean search."
1204 :group 'org-agenda-search-view
1205 :type 'boolean)
1207 (if (fboundp 'defvaralias)
1208 (defvaralias 'org-agenda-search-view-search-words-only
1209 'org-agenda-search-view-always-boolean))
1211 (defcustom org-agenda-search-view-force-full-words nil
1212 "Non-nil means, search words must be matches as complete words.
1213 When nil, they may also match part of a word."
1214 :group 'org-agenda-search-view
1215 :type 'boolean)
1217 (defgroup org-agenda-time-grid nil
1218 "Options concerning the time grid in the Org-mode Agenda."
1219 :tag "Org Agenda Time Grid"
1220 :group 'org-agenda)
1222 (defcustom org-agenda-search-headline-for-time t
1223 "Non-nil means search headline for a time-of-day.
1224 If the headline contains a time-of-day in one format or another, it will
1225 be used to sort the entry into the time sequence of items for a day.
1226 Some people have time stamps in the headline that refer to the creation
1227 time or so, and then this produces an unwanted side effect. If this is
1228 the case for your, use this variable to turn off searching the headline
1229 for a time."
1230 :group 'org-agenda-time-grid
1231 :type 'boolean)
1233 (defcustom org-agenda-use-time-grid t
1234 "Non-nil means show a time grid in the agenda schedule.
1235 A time grid is a set of lines for specific times (like every two hours between
1236 8:00 and 20:00). The items scheduled for a day at specific times are
1237 sorted in between these lines.
1238 For details about when the grid will be shown, and what it will look like, see
1239 the variable `org-agenda-time-grid'."
1240 :group 'org-agenda-time-grid
1241 :type 'boolean)
1243 (defcustom org-agenda-time-grid
1244 '((daily today require-timed)
1245 "----------------"
1246 (800 1000 1200 1400 1600 1800 2000))
1248 "The settings for time grid for agenda display.
1249 This is a list of three items. The first item is again a list. It contains
1250 symbols specifying conditions when the grid should be displayed:
1252 daily if the agenda shows a single day
1253 weekly if the agenda shows an entire week
1254 today show grid on current date, independent of daily/weekly display
1255 require-timed show grid only if at least one item has a time specification
1257 The second item is a string which will be placed behind the grid time.
1259 The third item is a list of integers, indicating the times that should have
1260 a grid line."
1261 :group 'org-agenda-time-grid
1262 :type
1263 '(list
1264 (set :greedy t :tag "Grid Display Options"
1265 (const :tag "Show grid in single day agenda display" daily)
1266 (const :tag "Show grid in weekly agenda display" weekly)
1267 (const :tag "Always show grid for today" today)
1268 (const :tag "Show grid only if any timed entries are present"
1269 require-timed)
1270 (const :tag "Skip grid times already present in an entry"
1271 remove-match))
1272 (string :tag "Grid String")
1273 (repeat :tag "Grid Times" (integer :tag "Time"))))
1275 (defcustom org-agenda-show-current-time-in-grid t
1276 "Non-nil means show the current time in the time grid."
1277 :group 'org-agenda-time-grid
1278 :type 'boolean)
1280 (defcustom org-agenda-current-time-string
1281 "now - - - - - - - - - - - - - - - - - - - - - - - - -"
1282 "The string for the current time marker in the agenda."
1283 :group 'org-agenda-time-grid
1284 :type 'string)
1286 (defgroup org-agenda-sorting nil
1287 "Options concerning sorting in the Org-mode Agenda."
1288 :tag "Org Agenda Sorting"
1289 :group 'org-agenda)
1291 (defcustom org-agenda-sorting-strategy
1292 '((agenda habit-down time-up priority-down category-keep)
1293 (todo priority-down category-keep)
1294 (tags priority-down category-keep)
1295 (search category-keep))
1296 "Sorting structure for the agenda items of a single day.
1297 This is a list of symbols which will be used in sequence to determine
1298 if an entry should be listed before another entry. The following
1299 symbols are recognized:
1301 time-up Put entries with time-of-day indications first, early first
1302 time-down Put entries with time-of-day indications first, late first
1303 category-keep Keep the default order of categories, corresponding to the
1304 sequence in `org-agenda-files'.
1305 category-up Sort alphabetically by category, A-Z.
1306 category-down Sort alphabetically by category, Z-A.
1307 tag-up Sort alphabetically by last tag, A-Z.
1308 tag-down Sort alphabetically by last tag, Z-A.
1309 priority-up Sort numerically by priority, high priority last.
1310 priority-down Sort numerically by priority, high priority first.
1311 todo-state-up Sort by todo state, tasks that are done last.
1312 todo-state-down Sort by todo state, tasks that are done first.
1313 effort-up Sort numerically by estimated effort, high effort last.
1314 effort-down Sort numerically by estimated effort, high effort first.
1315 user-defined-up Sort according to `org-agenda-cmp-user-defined', high last.
1316 user-defined-down Sort according to `org-agenda-cmp-user-defined', high first.
1317 habit-up Put entries that are habits first
1318 habit-down Put entries that are habits last
1319 alpha-up Sort headlines alphabetically
1320 alpha-down Sort headlines alphabetically, reversed
1322 The different possibilities will be tried in sequence, and testing stops
1323 if one comparison returns a \"not-equal\". For example, the default
1324 '(time-up category-keep priority-down)
1325 means: Pull out all entries having a specified time of day and sort them,
1326 in order to make a time schedule for the current day the first thing in the
1327 agenda listing for the day. Of the entries without a time indication, keep
1328 the grouped in categories, don't sort the categories, but keep them in
1329 the sequence given in `org-agenda-files'. Within each category sort by
1330 priority.
1332 Leaving out `category-keep' would mean that items will be sorted across
1333 categories by priority.
1335 Instead of a single list, this can also be a set of list for specific
1336 contents, with a context symbol in the car of the list, any of
1337 `agenda', `todo', `tags', `search' for the corresponding agenda views.
1339 Custom commands can bind this variable in the options section."
1340 :group 'org-agenda-sorting
1341 :type `(choice
1342 (repeat :tag "General" ,org-sorting-choice)
1343 (list :tag "Individually"
1344 (cons (const :tag "Strategy for Weekly/Daily agenda" agenda)
1345 (repeat ,org-sorting-choice))
1346 (cons (const :tag "Strategy for TODO lists" todo)
1347 (repeat ,org-sorting-choice))
1348 (cons (const :tag "Strategy for Tags matches" tags)
1349 (repeat ,org-sorting-choice))
1350 (cons (const :tag "Strategy for search matches" search)
1351 (repeat ,org-sorting-choice)))))
1353 (defcustom org-agenda-cmp-user-defined nil
1354 "A function to define the comparison `user-defined'.
1355 This function must receive two arguments, agenda entry a and b.
1356 If a>b, return +1. If a<b, return -1. If they are equal as seen by
1357 the user comparison, return nil.
1358 When this is defined, you can make `user-defined-up' and `user-defined-down'
1359 part of an agenda sorting strategy."
1360 :group 'org-agenda-sorting
1361 :type 'symbol)
1363 (defcustom org-sort-agenda-notime-is-late t
1364 "Non-nil means items without time are considered late.
1365 This is only relevant for sorting. When t, items which have no explicit
1366 time like 15:30 will be considered as 99:01, i.e. later than any items which
1367 do have a time. When nil, the default time is before 0:00. You can use this
1368 option to decide if the schedule for today should come before or after timeless
1369 agenda entries."
1370 :group 'org-agenda-sorting
1371 :type 'boolean)
1373 (defcustom org-sort-agenda-noeffort-is-high t
1374 "Non-nil means items without effort estimate are sorted as high effort.
1375 This also applies when filtering an agenda view with respect to the
1376 < or > effort operator. Then, tasks with no effort defined will be treated
1377 as tasks with high effort.
1378 When nil, such items are sorted as 0 minutes effort."
1379 :group 'org-agenda-sorting
1380 :type 'boolean)
1382 (defgroup org-agenda-line-format nil
1383 "Options concerning the entry prefix in the Org-mode agenda display."
1384 :tag "Org Agenda Line Format"
1385 :group 'org-agenda)
1387 (defcustom org-agenda-prefix-format
1388 '((agenda . " %i %-12:c%?-12t% s")
1389 (timeline . " % s")
1390 (todo . " %i %-12:c")
1391 (tags . " %i %-12:c")
1392 (search . " %i %-12:c"))
1393 "Format specifications for the prefix of items in the agenda views.
1394 An alist with five entries, each for the different agenda types. The
1395 keys of the sublists are `agenda', `timeline', `todo', `search' and `tags'.
1396 The values are format strings.
1398 This format works similar to a printf format, with the following meaning:
1400 %c the category of the item, \"Diary\" for entries from the diary,
1401 or as given by the CATEGORY keyword or derived from the file name
1402 %i the icon category of the item, see `org-agenda-category-icon-alist'
1403 %T the last tag of the item (ignore inherited tags, which come first)
1404 %t the HH:MM time-of-day specification if one applies to the entry
1405 %s Scheduling/Deadline information, a short string
1406 %(expression) Eval EXPRESSION and replace the control string
1407 by the result
1409 All specifiers work basically like the standard `%s' of printf, but may
1410 contain two additional characters: a question mark just after the `%'
1411 and a whitespace/punctuation character just before the final letter.
1413 If the first character after `%' is a question mark, the entire field
1414 will only be included if the corresponding value applies to the current
1415 entry. This is useful for fields which should have fixed width when
1416 present, but zero width when absent. For example, \"%?-12t\" will
1417 result in a 12 character time field if a time of the day is specified,
1418 but will completely disappear in entries which do not contain a time.
1420 If there is punctuation or whitespace character just before the final
1421 format letter, this character will be appended to the field value if
1422 the value is not empty. For example, the format \"%-12:c\" leads to
1423 \"Diary: \" if the category is \"Diary\". If the category were be
1424 empty, no additional colon would be inserted.
1426 The default value for the agenda sublist is \" %-12:c%?-12t% s\",
1427 which means:
1429 - Indent the line with two space characters
1430 - Give the category a 12 chars wide field, padded with whitespace on
1431 the right (because of `-'). Append a colon if there is a category
1432 (because of `:').
1433 - If there is a time-of-day, put it into a 12 chars wide field. If no
1434 time, don't put in an empty field, just skip it (because of '?').
1435 - Finally, put the scheduling information.
1437 See also the variables `org-agenda-remove-times-when-in-prefix' and
1438 `org-agenda-remove-tags'.
1440 Custom commands can set this variable in the options section."
1441 :type '(choice
1442 (string :tag "General format")
1443 (list :greedy t :tag "View dependent"
1444 (cons (const agenda) (string :tag "Format"))
1445 (cons (const timeline) (string :tag "Format"))
1446 (cons (const todo) (string :tag "Format"))
1447 (cons (const tags) (string :tag "Format"))
1448 (cons (const search) (string :tag "Format"))))
1449 :group 'org-agenda-line-format)
1451 (defvar org-prefix-format-compiled nil
1452 "The compiled version of the most recently used prefix format.
1453 See the variable `org-agenda-prefix-format'.")
1455 (defcustom org-agenda-todo-keyword-format "%-1s"
1456 "Format for the TODO keyword in agenda lines.
1457 Set this to something like \"%-12s\" if you want all TODO keywords
1458 to occupy a fixed space in the agenda display."
1459 :group 'org-agenda-line-format
1460 :type 'string)
1462 (defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
1463 "Text preceding timerange entries in the agenda view.
1464 This is a list with two strings. The first applies when the range
1465 is entirely on one day. The second applies if the range spans several days.
1466 The strings may have two \"%d\" format specifiers which will be filled
1467 with the sequence number of the days, and the total number of days in the
1468 range, respectively."
1469 :group 'org-agenda-line-format
1470 :type '(list
1471 (string :tag "Deadline today ")
1472 (choice :tag "Deadline relative"
1473 (string :tag "Format string")
1474 (function))))
1476 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
1477 "Text preceding scheduled items in the agenda view.
1478 This is a list with two strings. The first applies when the item is
1479 scheduled on the current day. The second applies when it has been scheduled
1480 previously, it may contain a %d indicating that this is the nth time that
1481 this item is scheduled, due to automatic rescheduling of unfinished items
1482 for the following day. So this number is one larger than the number of days
1483 that passed since this item was scheduled first."
1484 :group 'org-agenda-line-format
1485 :type '(list
1486 (string :tag "Scheduled today ")
1487 (string :tag "Scheduled previously")))
1489 (defcustom org-agenda-inactive-leader "["
1490 "Text preceding item pulled into the agenda by inactive time stamps.
1491 These entries are added to the agenda when pressing \"[\"."
1492 :group 'org-agenda-line-format
1493 :type '(list
1494 (string :tag "Scheduled today ")
1495 (string :tag "Scheduled previously")))
1497 (defcustom org-agenda-deadline-leaders '("Deadline: " "In %3d d.: ")
1498 "Text preceding deadline items in the agenda view.
1499 This is a list with two strings. The first applies when the item has its
1500 deadline on the current day. The second applies when it is in the past or
1501 in the future, it may contain %d to capture how many days away the deadline
1502 is (was)."
1503 :group 'org-agenda-line-format
1504 :type '(list
1505 (string :tag "Deadline today ")
1506 (choice :tag "Deadline relative"
1507 (string :tag "Format string")
1508 (function))))
1510 (defcustom org-agenda-remove-times-when-in-prefix t
1511 "Non-nil means remove duplicate time specifications in agenda items.
1512 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
1513 time-of-day specification in a headline or diary entry is extracted and
1514 placed into the prefix. If this option is non-nil, the original specification
1515 \(a timestamp or -range, or just a plain time(range) specification like
1516 11:30-4pm) will be removed for agenda display. This makes the agenda less
1517 cluttered.
1518 The option can be t or nil. It may also be the symbol `beg', indicating
1519 that the time should only be removed when it is located at the beginning of
1520 the headline/diary entry."
1521 :group 'org-agenda-line-format
1522 :type '(choice
1523 (const :tag "Always" t)
1524 (const :tag "Never" nil)
1525 (const :tag "When at beginning of entry" beg)))
1527 (defcustom org-agenda-remove-timeranges-from-blocks nil
1528 "Non-nil means remove time ranges specifications in agenda
1529 items that span on several days."
1530 :group 'org-agenda-line-format
1531 :type 'boolean)
1533 (defcustom org-agenda-default-appointment-duration nil
1534 "Default duration for appointments that only have a starting time.
1535 When nil, no duration is specified in such cases.
1536 When non-nil, this must be the number of minutes, e.g. 60 for one hour."
1537 :group 'org-agenda-line-format
1538 :type '(choice
1539 (integer :tag "Minutes")
1540 (const :tag "No default duration")))
1542 (defcustom org-agenda-show-inherited-tags t
1543 "Non-nil means show inherited tags in each agenda line."
1544 :group 'org-agenda-line-format
1545 :type 'boolean)
1547 (defcustom org-agenda-hide-tags-regexp nil
1548 "Regular expression used to filter away specific tags in agenda views.
1549 This means that these tags will be present, but not be shown in the agenda
1550 line. Secondary filtering will still work on the hidden tags.
1551 Nil means don't hide any tags."
1552 :group 'org-agenda-line-format
1553 :type '(choice
1554 (const :tag "Hide none" nil)
1555 (string :tag "Regexp ")))
1557 (defcustom org-agenda-remove-tags nil
1558 "Non-nil means remove the tags from the headline copy in the agenda.
1559 When this is the symbol `prefix', only remove tags when
1560 `org-agenda-prefix-format' contains a `%T' specifier."
1561 :group 'org-agenda-line-format
1562 :type '(choice
1563 (const :tag "Always" t)
1564 (const :tag "Never" nil)
1565 (const :tag "When prefix format contains %T" prefix)))
1567 (if (fboundp 'defvaralias)
1568 (defvaralias 'org-agenda-remove-tags-when-in-prefix
1569 'org-agenda-remove-tags))
1571 (defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
1572 "Shift tags in agenda items to this column.
1573 If this number is positive, it specifies the column. If it is negative,
1574 it means that the tags should be flushright to that column. For example,
1575 -80 works well for a normal 80 character screen."
1576 :group 'org-agenda-line-format
1577 :type 'integer)
1579 (if (fboundp 'defvaralias)
1580 (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
1582 (defcustom org-agenda-fontify-priorities 'cookies
1583 "Non-nil means highlight low and high priorities in agenda.
1584 When t, the highest priority entries are bold, lowest priority italic.
1585 However, settings in `org-priority-faces' will overrule these faces.
1586 When this variable is the symbol `cookies', only fontify the
1587 cookies, not the entire task.
1588 This may also be an association list of priority faces, whose
1589 keys are the character values of `org-highest-priority',
1590 `org-default-priority', and `org-lowest-priority' (the default values
1591 are ?A, ?B, and ?C, respectively). The face may be a named face, a
1592 color as a string, or a list like `(:background \"Red\")'.
1593 If it is a color, the variable `org-faces-easy-properties'
1594 determines if it is a foreground or a background color."
1595 :group 'org-agenda-line-format
1596 :type '(choice
1597 (const :tag "Never" nil)
1598 (const :tag "Defaults" t)
1599 (const :tag "Cookies only" cookies)
1600 (repeat :tag "Specify"
1601 (list (character :tag "Priority" :value ?A)
1602 (choice :tag "Face "
1603 (string :tag "Color")
1604 (sexp :tag "Face"))))))
1606 (defcustom org-agenda-day-face-function nil
1607 "Function called to determine what face should be used to display a day.
1608 The only argument passed to that function is the day. It should
1609 returns a face, or nil if does not want to specify a face and let
1610 the normal rules apply."
1611 :group 'org-agenda-line-format
1612 :type 'function)
1614 (defcustom org-agenda-category-icon-alist nil
1615 "Alist of category icon to be displayed in agenda views.
1617 Each entry should have the following format:
1619 (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
1621 Where CATEGORY-REGEXP is a regexp matching the categories where
1622 the icon should be displayed.
1623 FILE-OR-DATA either a file path or a string containing image data.
1625 The other fields can be omited safely if not needed:
1626 TYPE indicates the image type.
1627 DATA-P is a boolean indicating whether the FILE-OR-DATA string is
1628 image data.
1629 PROPS are additional image attributes to assign to the image,
1630 like, e.g. `:ascent center'.
1632 (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
1634 If you want to set the display properties yourself, just put a
1635 list as second element:
1637 (CATEGORY-REGEXP (MY PROPERTY LIST))
1639 For example, to display a 16px horizontal space for Emacs
1640 category, you can use:
1642 (\"Emacs\" '(space . (:width (16))))"
1643 :group 'org-agenda-line-format
1644 :type '(alist :key-type (string :tag "Regexp matching category")
1645 :value-type (choice (list :tag "Icon"
1646 (string :tag "File or data")
1647 (symbol :tag "Type")
1648 (boolean :tag "Data?")
1649 (repeat :tag "Extra image properties" :inline t symbol))
1650 (list :tag "Display properties" sexp))))
1652 (defgroup org-agenda-column-view nil
1653 "Options concerning column view in the agenda."
1654 :tag "Org Agenda Column View"
1655 :group 'org-agenda)
1657 (defcustom org-agenda-columns-show-summaries t
1658 "Non-nil means show summaries for columns displayed in the agenda view."
1659 :group 'org-agenda-column-view
1660 :type 'boolean)
1662 (defcustom org-agenda-columns-compute-summary-properties t
1663 "Non-nil means recompute all summary properties before column view.
1664 When column view in the agenda is listing properties that have a summary
1665 operator, it can go to all relevant buffers and recompute the summaries
1666 there. This can mean overhead for the agenda column view, but is necessary
1667 to have thing up to date.
1668 As a special case, a CLOCKSUM property also makes sure that the clock
1669 computations are current."
1670 :group 'org-agenda-column-view
1671 :type 'boolean)
1673 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
1674 "Non-nil means the duration of an appointment will add to day effort.
1675 The property to which appointment durations will be added is the one given
1676 in the option `org-effort-property'. If an appointment does not have
1677 an end time, `org-agenda-default-appointment-duration' will be used. If that
1678 is not set, an appointment without end time will not contribute to the time
1679 estimate."
1680 :group 'org-agenda-column-view
1681 :type 'boolean)
1683 (defcustom org-agenda-auto-exclude-function nil
1684 "A function called with a tag to decide if it is filtered on '/ RET'.
1685 The sole argument to the function, which is called once for each
1686 possible tag, is a string giving the name of the tag. The
1687 function should return either nil if the tag should be included
1688 as normal, or \"-<TAG>\" to exclude the tag.
1689 Note that for the purpose of tag filtering, only the lower-case version of
1690 all tags will be considered, so that this function will only ever see
1691 the lower-case version of all tags."
1692 :group 'org-agenda
1693 :type 'function)
1695 (defcustom org-agenda-bulk-custom-functions nil
1696 "Alist of characters and custom functions for bulk actions.
1697 For example, this value makes those two functions available:
1699 '((?R set-category)
1700 (?C bulk-cut))
1702 With selected entries in an agenda buffer, `B R' will call
1703 the custom function `set-category' on the selected entries.
1704 Note that functions in this alist don't need to be quoted."
1705 :type 'alist
1706 :group 'org-agenda)
1708 (eval-when-compile
1709 (require 'cl))
1710 (require 'org)
1712 (defmacro org-agenda-with-point-at-orig-entry (string &rest body)
1713 "Execute BODY with point at location given by `org-hd-marker' property.
1714 If STRING is non-nil, the text property will be fetched from position 0
1715 in that string. If STRING is nil, it will be fetched from the beginning
1716 of the current line."
1717 `(let ((marker (get-text-property (if string 0 (point-at-bol))
1718 'org-hd-marker string)))
1719 (with-current-buffer (marker-buffer marker)
1720 (save-excursion
1721 (goto-char marker)
1722 ,@body))))
1724 (defun org-add-agenda-custom-command (entry)
1725 "Replace or add a command in `org-agenda-custom-commands'.
1726 This is mostly for hacking and trying a new command - once the command
1727 works you probably want to add it to `org-agenda-custom-commands' for good."
1728 (let ((ass (assoc (car entry) org-agenda-custom-commands)))
1729 (if ass
1730 (setcdr ass (cdr entry))
1731 (push entry org-agenda-custom-commands))))
1733 ;;; Define the Org-agenda-mode
1735 (defvar org-agenda-mode-map (make-sparse-keymap)
1736 "Keymap for `org-agenda-mode'.")
1737 (if (fboundp 'defvaralias)
1738 (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
1740 (defvar org-agenda-menu) ; defined later in this file.
1741 (defvar org-agenda-restrict) ; defined later in this file.
1742 (defvar org-agenda-follow-mode nil)
1743 (defvar org-agenda-entry-text-mode nil)
1744 (defvar org-agenda-clockreport-mode nil)
1745 (defvar org-agenda-show-log nil)
1746 (defvar org-agenda-redo-command nil)
1747 (defvar org-agenda-query-string nil)
1748 (defvar org-agenda-mode-hook nil
1749 "Hook for `org-agenda-mode', run after the mode is turned on.")
1750 (defvar org-agenda-type nil)
1751 (defvar org-agenda-force-single-file nil)
1752 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
1754 (defun org-agenda-mode ()
1755 "Mode for time-sorted view on action items in Org-mode files.
1757 The following commands are available:
1759 \\{org-agenda-mode-map}"
1760 (interactive)
1761 (kill-all-local-variables)
1762 (setq org-agenda-undo-list nil
1763 org-agenda-pending-undo-list nil
1764 org-agenda-bulk-marked-entries nil)
1765 (setq major-mode 'org-agenda-mode)
1766 ;; Keep global-font-lock-mode from turning on font-lock-mode
1767 (org-set-local 'font-lock-global-modes (list 'not major-mode))
1768 (setq mode-name "Org-Agenda")
1769 (use-local-map org-agenda-mode-map)
1770 (easy-menu-add org-agenda-menu)
1771 (if org-startup-truncated (setq truncate-lines t))
1772 (org-set-local 'line-move-visual nil)
1773 (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
1774 (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
1775 ;; Make sure properties are removed when copying text
1776 (when (boundp 'buffer-substring-filters)
1777 (org-set-local 'buffer-substring-filters
1778 (cons (lambda (x)
1779 (set-text-properties 0 (length x) nil x) x)
1780 buffer-substring-filters)))
1781 (unless org-agenda-keep-modes
1782 (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
1783 org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
1784 org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
1785 org-agenda-show-log org-agenda-start-with-log-mode))
1787 (easy-menu-change
1788 '("Agenda") "Agenda Files"
1789 (append
1790 (list
1791 (vector
1792 (if (get 'org-agenda-files 'org-restrict)
1793 "Restricted to single file"
1794 "Edit File List")
1795 '(org-edit-agenda-file-list)
1796 (not (get 'org-agenda-files 'org-restrict)))
1797 "--")
1798 (mapcar 'org-file-menu-entry (org-agenda-files))))
1799 (org-agenda-set-mode-name)
1800 (apply
1801 (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
1802 (list 'org-agenda-mode-hook)))
1804 (substitute-key-definition 'undo 'org-agenda-undo
1805 org-agenda-mode-map global-map)
1806 (org-defkey org-agenda-mode-map "\C-i" 'org-agenda-goto)
1807 (org-defkey org-agenda-mode-map [(tab)] 'org-agenda-goto)
1808 (org-defkey org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
1809 (org-defkey org-agenda-mode-map "\C-k" 'org-agenda-kill)
1810 (org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
1811 (org-defkey org-agenda-mode-map "m" 'org-agenda-bulk-mark)
1812 (org-defkey org-agenda-mode-map "%" 'org-agenda-bulk-mark-regexp)
1813 (org-defkey org-agenda-mode-map "u" 'org-agenda-bulk-unmark)
1814 (org-defkey org-agenda-mode-map "U" 'org-agenda-bulk-remove-all-marks)
1815 (org-defkey org-agenda-mode-map "A" 'org-agenda-append-agenda)
1816 (org-defkey org-agenda-mode-map "B" 'org-agenda-bulk-action)
1817 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
1818 (org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
1819 (org-defkey org-agenda-mode-map "\C-c\C-xa" 'org-agenda-toggle-archive-tag)
1820 (org-defkey org-agenda-mode-map "\C-c\C-xA" 'org-agenda-archive-to-archive-sibling)
1821 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
1822 (org-defkey org-agenda-mode-map "\C-c$" 'org-agenda-archive)
1823 (org-defkey org-agenda-mode-map "$" 'org-agenda-archive)
1824 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
1825 (org-defkey org-agenda-mode-map " " 'org-agenda-show-and-scroll-up)
1826 (org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
1827 (org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
1828 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
1829 (org-defkey org-agenda-mode-map [(control shift left)] 'org-agenda-todo-previousset)
1830 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
1831 (org-defkey org-agenda-mode-map "o" 'delete-other-windows)
1832 (org-defkey org-agenda-mode-map "L" 'org-agenda-recenter)
1833 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
1834 (org-defkey org-agenda-mode-map "t" 'org-agenda-todo)
1835 (org-defkey org-agenda-mode-map "a" 'org-agenda-archive-default-with-confirmation)
1836 (org-defkey org-agenda-mode-map ":" 'org-agenda-set-tags)
1837 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
1838 (org-defkey org-agenda-mode-map "." 'org-agenda-goto-today)
1839 (org-defkey org-agenda-mode-map "j" 'org-agenda-goto-date)
1840 (org-defkey org-agenda-mode-map "d" 'org-agenda-day-view)
1841 (org-defkey org-agenda-mode-map "w" 'org-agenda-week-view)
1842 (org-defkey org-agenda-mode-map "y" 'org-agenda-year-view)
1843 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
1844 (org-defkey org-agenda-mode-map "z" 'org-agenda-add-note)
1845 (org-defkey org-agenda-mode-map "k" 'org-agenda-action)
1846 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
1847 (org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
1848 (org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
1849 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
1850 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
1852 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
1853 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
1854 (org-defkey org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
1855 (let ((l '(1 2 3 4 5 6 7 8 9 0)))
1856 (while l (org-defkey org-agenda-mode-map
1857 (int-to-string (pop l)) 'digit-argument)))
1859 (org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
1860 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
1861 (org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
1862 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
1863 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
1864 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
1865 (org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
1866 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
1867 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
1868 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
1869 (org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
1870 (org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
1871 (org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
1872 'org-clock-modify-effort-estimate)
1873 (org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
1874 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
1875 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
1876 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
1877 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
1878 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
1879 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
1880 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
1881 (org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
1882 (org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
1883 (substitute-key-definition 'next-line 'org-agenda-next-line
1884 org-agenda-mode-map global-map)
1885 (substitute-key-definition 'previous-line 'org-agenda-previous-line
1886 org-agenda-mode-map global-map)
1887 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
1888 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
1889 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
1890 (org-defkey org-agenda-mode-map "," 'org-agenda-priority)
1891 (org-defkey org-agenda-mode-map "\C-c," 'org-agenda-priority)
1892 (org-defkey org-agenda-mode-map "i" 'org-agenda-diary-entry)
1893 (org-defkey org-agenda-mode-map "c" 'org-agenda-goto-calendar)
1894 (org-defkey org-agenda-mode-map "C" 'org-agenda-convert-date)
1895 (org-defkey org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
1896 (org-defkey org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
1897 (org-defkey org-agenda-mode-map "h" 'org-agenda-holidays)
1898 (org-defkey org-agenda-mode-map "H" 'org-agenda-holidays)
1899 (org-defkey org-agenda-mode-map "\C-c\C-x\C-i" 'org-agenda-clock-in)
1900 (org-defkey org-agenda-mode-map "I" 'org-agenda-clock-in)
1901 (org-defkey org-agenda-mode-map "\C-c\C-x\C-o" 'org-agenda-clock-out)
1902 (org-defkey org-agenda-mode-map "O" 'org-agenda-clock-out)
1903 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
1904 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
1905 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
1906 (org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
1907 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
1908 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
1909 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
1910 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
1911 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
1912 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
1913 (org-defkey org-agenda-mode-map "f" 'org-agenda-later)
1914 (org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
1915 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
1916 (org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
1918 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
1919 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
1920 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
1921 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
1922 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
1923 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
1924 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
1925 (define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
1926 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mg" 'org-mobile-pull)
1927 (org-defkey org-agenda-mode-map "\C-c\C-x\C-mp" 'org-mobile-push)
1929 (org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
1930 (org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
1931 (when org-agenda-mouse-1-follows-link
1932 (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
1933 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
1934 '("Agenda"
1935 ("Agenda Files")
1936 "--"
1937 ("Agenda Dates"
1938 ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
1939 ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
1940 ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
1941 ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
1942 "--"
1943 ("View"
1944 ["Day View" org-agenda-day-view
1945 :active (org-agenda-check-type nil 'agenda)
1946 :style radio :selected (eq org-agenda-current-span 'day)
1947 :keys "v d (or just d)"]
1948 ["Week View" org-agenda-week-view
1949 :active (org-agenda-check-type nil 'agenda)
1950 :style radio :selected (eq org-agenda-current-span 'week)
1951 :keys "v w (or just w)"]
1952 ["Month View" org-agenda-month-view
1953 :active (org-agenda-check-type nil 'agenda)
1954 :style radio :selected (eq org-agenda-current-span 'month)
1955 :keys "v m"]
1956 ["Year View" org-agenda-year-view
1957 :active (org-agenda-check-type nil 'agenda)
1958 :style radio :selected (eq org-agenda-current-span 'year)
1959 :keys "v y"]
1960 "--"
1961 ["Include Diary" org-agenda-toggle-diary
1962 :style toggle :selected org-agenda-include-diary
1963 :active (org-agenda-check-type nil 'agenda)]
1964 ["Include Deadlines" org-agenda-toggle-deadlines
1965 :style toggle :selected org-agenda-include-deadlines
1966 :active (org-agenda-check-type nil 'agenda)]
1967 ["Use Time Grid" org-agenda-toggle-time-grid
1968 :style toggle :selected org-agenda-use-time-grid
1969 :active (org-agenda-check-type nil 'agenda)]
1970 "--"
1971 ["Show clock report" org-agenda-clockreport-mode
1972 :style toggle :selected org-agenda-clockreport-mode
1973 :active (org-agenda-check-type nil 'agenda)]
1974 ["Show some entry text" org-agenda-entry-text-mode
1975 :style toggle :selected org-agenda-entry-text-mode
1976 :active t]
1977 "--"
1978 ["Show Logbook entries" org-agenda-log-mode
1979 :style toggle :selected org-agenda-show-log
1980 :active (org-agenda-check-type nil 'agenda 'timeline)
1981 :keys "v l (or just l)"]
1982 ["Include archived trees" org-agenda-archives-mode
1983 :style toggle :selected org-agenda-archives-mode :active t
1984 :keys "v a"]
1985 ["Include archive files" (org-agenda-archives-mode t)
1986 :style toggle :selected (eq org-agenda-archives-mode t) :active t
1987 :keys "v A"]
1988 "--"
1989 ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
1990 ["Write view to file" org-write-agenda t]
1991 ["Rebuild buffer" org-agenda-redo t]
1992 ["Save all Org-mode Buffers" org-save-all-org-buffers t]
1993 "--"
1994 ["Show original entry" org-agenda-show t]
1995 ["Go To (other window)" org-agenda-goto t]
1996 ["Go To (this window)" org-agenda-switch-to t]
1997 ["Follow Mode" org-agenda-follow-mode
1998 :style toggle :selected org-agenda-follow-mode :active t]
1999 ; ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
2000 "--"
2001 ("TODO"
2002 ["Cycle TODO" org-agenda-todo t]
2003 ["Next TODO set" org-agenda-todo-nextset t]
2004 ["Previous TODO set" org-agenda-todo-previousset t]
2005 ["Add note" org-agenda-add-note t])
2006 ("Archive/Refile/Delete"
2007 ["Archive default" org-agenda-archive-default t]
2008 ["Archive default" org-agenda-archive-default-with-confirmation t]
2009 ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
2010 ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
2011 ["Archive subtree" org-agenda-archive t]
2012 "--"
2013 ["Refile" org-agenda-refile t]
2014 "--"
2015 ["Delete subtree" org-agenda-kill t])
2016 ("Bulk action"
2017 ["Mark entry" org-agenda-bulk-mark t]
2018 ["Mark matching regexp" org-agenda-bulk-mark-regexp t]
2019 ["Unmark entry" org-agenda-bulk-unmark t]
2020 ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
2021 ["Act on all marked" org-agenda-bulk-action t]
2022 "--"
2023 ("Tags and Properties"
2024 ["Show all Tags" org-agenda-show-tags t]
2025 ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
2026 ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
2027 "--"
2028 ["Column View" org-columns t])
2029 ("Deadline/Schedule"
2030 ["Schedule" org-agenda-schedule t]
2031 ["Set Deadline" org-agenda-deadline t]
2032 "--"
2033 ["Mark item" org-agenda-action :active t :keys "k m"]
2034 ["Show mark item" org-agenda-action :active t :keys "k v"]
2035 ["Schedule marked item" org-agenda-action :active t :keys "k s"]
2036 ["Set Deadline for marked item" org-agenda-action :active t :keys "k d"]
2037 "--"
2038 ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
2039 ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
2040 ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
2041 ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
2042 ["Change Time + min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
2043 ["Change Time - min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
2044 ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
2045 ("Clock and Effort"
2046 ["Clock in" org-agenda-clock-in t]
2047 ["Clock out" org-agenda-clock-out t]
2048 ["Clock cancel" org-agenda-clock-cancel t]
2049 ["Goto running clock" org-clock-goto t]
2050 "--"
2051 ["Set Effort" org-agenda-set-effort t]
2052 ["Change clocked effort" org-clock-modify-effort-estimate
2053 (org-clock-is-active)])
2054 ("Priority"
2055 ["Set Priority" org-agenda-priority t]
2056 ["Increase Priority" org-agenda-priority-up t]
2057 ["Decrease Priority" org-agenda-priority-down t]
2058 ["Show Priority" org-agenda-show-priority t])
2059 ("Calendar/Diary"
2060 ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
2061 ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
2062 ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
2063 ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
2064 ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
2065 ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
2066 "--"
2067 ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
2068 "--"
2069 ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
2070 "--"
2071 ("MobileOrg"
2072 ["Push Files and Views" org-mobile-push t]
2073 ["Get Captured and Flagged" org-mobile-pull t]
2074 ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
2075 ["Show note / unflag" org-agenda-show-the-flagging-note t]
2076 "--"
2077 ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
2078 "--"
2079 ["Quit" org-agenda-quit t]
2080 ["Exit and Release Buffers" org-agenda-exit t]
2083 ;;; Agenda undo
2085 (defvar org-agenda-allow-remote-undo t
2086 "Non-nil means allow remote undo from the agenda buffer.")
2087 (defvar org-agenda-undo-list nil
2088 "List of undoable operations in the agenda since last refresh.")
2089 (defvar org-agenda-undo-has-started-in nil
2090 "Buffers that have already seen `undo-start' in the current undo sequence.")
2091 (defvar org-agenda-pending-undo-list nil
2092 "In a series of undo commands, this is the list of remaining undo items.")
2094 (defun org-agenda-undo ()
2095 "Undo a remote editing step in the agenda.
2096 This undoes changes both in the agenda buffer and in the remote buffer
2097 that have been changed along."
2098 (interactive)
2099 (or org-agenda-allow-remote-undo
2100 (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
2101 (if (not (eq this-command last-command))
2102 (setq org-agenda-undo-has-started-in nil
2103 org-agenda-pending-undo-list org-agenda-undo-list))
2104 (if (not org-agenda-pending-undo-list)
2105 (error "No further undo information"))
2106 (let* ((entry (pop org-agenda-pending-undo-list))
2107 buf line cmd rembuf)
2108 (setq cmd (pop entry) line (pop entry))
2109 (setq rembuf (nth 2 entry))
2110 (org-with-remote-undo rembuf
2111 (while (bufferp (setq buf (pop entry)))
2112 (if (pop entry)
2113 (with-current-buffer buf
2114 (let ((last-undo-buffer buf)
2115 (inhibit-read-only t))
2116 (unless (memq buf org-agenda-undo-has-started-in)
2117 (push buf org-agenda-undo-has-started-in)
2118 (make-local-variable 'pending-undo-list)
2119 (undo-start))
2120 (while (and pending-undo-list
2121 (listp pending-undo-list)
2122 (not (car pending-undo-list)))
2123 (pop pending-undo-list))
2124 (undo-more 1))))))
2125 (org-goto-line line)
2126 (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
2128 (defun org-verify-change-for-undo (l1 l2)
2129 "Verify that a real change occurred between the undo lists L1 and L2."
2130 (while (and l1 (listp l1) (null (car l1))) (pop l1))
2131 (while (and l2 (listp l2) (null (car l2))) (pop l2))
2132 (not (eq l1 l2)))
2134 ;;; Agenda dispatch
2136 (defvar org-agenda-restrict nil)
2137 (defvar org-agenda-restrict-begin (make-marker))
2138 (defvar org-agenda-restrict-end (make-marker))
2139 (defvar org-agenda-last-dispatch-buffer nil)
2140 (defvar org-agenda-overriding-restriction nil)
2142 ;;;###autoload
2143 (defun org-agenda (&optional arg keys restriction)
2144 "Dispatch agenda commands to collect entries to the agenda buffer.
2145 Prompts for a command to execute. Any prefix arg will be passed
2146 on to the selected command. The default selections are:
2148 a Call `org-agenda-list' to display the agenda for current day or week.
2149 t Call `org-todo-list' to display the global todo list.
2150 T Call `org-todo-list' to display the global todo list, select only
2151 entries with a specific TODO keyword (the user gets a prompt).
2152 m Call `org-tags-view' to display headlines with tags matching
2153 a condition (the user is prompted for the condition).
2154 M Like `m', but select only TODO entries, no ordinary headlines.
2155 L Create a timeline for the current buffer.
2156 e Export views to associated files.
2157 s Search entries for keywords.
2158 / Multi occur across all agenda files and also files listed
2159 in `org-agenda-text-search-extra-files'.
2160 < Restrict agenda commands to buffer, subtree, or region.
2161 Press several times to get the desired effect.
2162 > Remove a previous restriction.
2163 # List \"stuck\" projects.
2164 ! Configure what \"stuck\" means.
2165 C Configure custom agenda commands.
2167 More commands can be added by configuring the variable
2168 `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
2169 searches can be pre-defined in this way.
2171 If the current buffer is in Org-mode and visiting a file, you can also
2172 first press `<' once to indicate that the agenda should be temporarily
2173 \(until the next use of \\[org-agenda]) restricted to the current file.
2174 Pressing `<' twice means to restrict to the current subtree or region
2175 \(if active)."
2176 (interactive "P")
2177 (catch 'exit
2178 (let* ((prefix-descriptions nil)
2179 (org-agenda-window-setup (if (equal (buffer-name)
2180 org-agenda-buffer-name)
2181 'current-window
2182 org-agenda-window-setup))
2183 (org-agenda-custom-commands-orig org-agenda-custom-commands)
2184 (org-agenda-custom-commands
2185 ;; normalize different versions
2186 (delq nil
2187 (mapcar
2188 (lambda (x)
2189 (cond ((stringp (cdr x))
2190 (push x prefix-descriptions)
2191 nil)
2192 ((stringp (nth 1 x)) x)
2193 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2194 (t (cons (car x) (cons "" (cdr x))))))
2195 org-agenda-custom-commands)))
2196 (buf (current-buffer))
2197 (bfn (buffer-file-name (buffer-base-buffer)))
2198 entry key type match lprops ans)
2199 ;; Turn off restriction unless there is an overriding one,
2200 (unless org-agenda-overriding-restriction
2201 (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
2202 ;; There is a request to keep the file list in place
2203 (put 'org-agenda-files 'org-restrict nil))
2204 (setq org-agenda-restrict nil)
2205 (move-marker org-agenda-restrict-begin nil)
2206 (move-marker org-agenda-restrict-end nil))
2207 ;; Delete old local properties
2208 (put 'org-agenda-redo-command 'org-lprops nil)
2209 ;; Delete previously set last-arguments
2210 (put 'org-agenda-redo-command 'last-args nil)
2211 ;; Remember where this call originated
2212 (setq org-agenda-last-dispatch-buffer (current-buffer))
2213 (unless keys
2214 (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
2215 keys (car ans)
2216 restriction (cdr ans)))
2217 ;; Establish the restriction, if any
2218 (when (and (not org-agenda-overriding-restriction) restriction)
2219 (put 'org-agenda-files 'org-restrict (list bfn))
2220 (cond
2221 ((eq restriction 'region)
2222 (setq org-agenda-restrict t)
2223 (move-marker org-agenda-restrict-begin (region-beginning))
2224 (move-marker org-agenda-restrict-end (region-end)))
2225 ((eq restriction 'subtree)
2226 (save-excursion
2227 (setq org-agenda-restrict t)
2228 (org-back-to-heading t)
2229 (move-marker org-agenda-restrict-begin (point))
2230 (move-marker org-agenda-restrict-end
2231 (progn (org-end-of-subtree t)))))))
2233 ;; For example the todo list should not need it (but does...)
2234 (cond
2235 ((setq entry (assoc keys org-agenda-custom-commands))
2236 (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
2237 (progn
2238 (setq type (nth 2 entry) match (eval (nth 3 entry))
2239 lprops (nth 4 entry))
2240 (put 'org-agenda-redo-command 'org-lprops lprops)
2241 (cond
2242 ((eq type 'agenda)
2243 (org-let lprops '(org-agenda-list current-prefix-arg)))
2244 ((eq type 'alltodo)
2245 (org-let lprops '(org-todo-list current-prefix-arg)))
2246 ((eq type 'search)
2247 (org-let lprops '(org-search-view current-prefix-arg match nil)))
2248 ((eq type 'stuck)
2249 (org-let lprops '(org-agenda-list-stuck-projects
2250 current-prefix-arg)))
2251 ((eq type 'tags)
2252 (org-let lprops '(org-tags-view current-prefix-arg match)))
2253 ((eq type 'tags-todo)
2254 (org-let lprops '(org-tags-view '(4) match)))
2255 ((eq type 'todo)
2256 (org-let lprops '(org-todo-list match)))
2257 ((eq type 'tags-tree)
2258 (org-check-for-org-mode)
2259 (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
2260 ((eq type 'todo-tree)
2261 (org-check-for-org-mode)
2262 (org-let lprops
2263 '(org-occur (concat "^" org-outline-regexp "[ \t]*"
2264 (regexp-quote match) "\\>"))))
2265 ((eq type 'occur-tree)
2266 (org-check-for-org-mode)
2267 (org-let lprops '(org-occur match)))
2268 ((functionp type)
2269 (org-let lprops '(funcall type match)))
2270 ((fboundp type)
2271 (org-let lprops '(funcall type match)))
2272 (t (error "Invalid custom agenda command type %s" type))))
2273 (org-agenda-run-series (nth 1 entry) (cddr entry))))
2274 ((equal keys "C")
2275 (setq org-agenda-custom-commands org-agenda-custom-commands-orig)
2276 (customize-variable 'org-agenda-custom-commands))
2277 ((equal keys "a") (call-interactively 'org-agenda-list))
2278 ((equal keys "s") (call-interactively 'org-search-view))
2279 ((equal keys "t") (call-interactively 'org-todo-list))
2280 ((equal keys "T") (org-call-with-arg 'org-todo-list (or arg '(4))))
2281 ((equal keys "m") (call-interactively 'org-tags-view))
2282 ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
2283 ((equal keys "e") (call-interactively 'org-store-agenda-views))
2284 ((equal keys "?") (org-tags-view nil "+FLAGGED")
2285 (org-add-hook
2286 'post-command-hook
2287 (lambda ()
2288 (unless (current-message)
2289 (let* ((m (org-agenda-get-any-marker))
2290 (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
2291 (when note
2292 (message (concat
2293 "FLAGGING-NOTE ([?] for more info): "
2294 (org-add-props
2295 (replace-regexp-in-string
2296 "\\\\n" "//"
2297 (copy-sequence note))
2298 nil 'face 'org-warning)))))))
2299 t t))
2300 ((equal keys "L")
2301 (unless (org-mode-p)
2302 (error "This is not an Org-mode file"))
2303 (unless restriction
2304 (put 'org-agenda-files 'org-restrict (list bfn))
2305 (org-call-with-arg 'org-timeline arg)))
2306 ((equal keys "#") (call-interactively 'org-agenda-list-stuck-projects))
2307 ((equal keys "/") (call-interactively 'org-occur-in-agenda-files))
2308 ((equal keys "!") (customize-variable 'org-stuck-projects))
2309 (t (error "Invalid agenda key"))))))
2311 (defun org-agenda-append-agenda ()
2312 "Append another agenda view to the current one.
2313 This function allows interactive building of block agendas.
2314 Agenda views are separated by `org-agenda-block-separator'."
2315 (interactive)
2316 (unless (string= (buffer-name) org-agenda-buffer-name)
2317 (error "Can only append from within agenda buffer"))
2318 (let ((org-agenda-multi t))
2319 (org-agenda)
2320 (widen)))
2322 (defun org-agenda-normalize-custom-commands (cmds)
2323 (delq nil
2324 (mapcar
2325 (lambda (x)
2326 (cond ((stringp (cdr x)) nil)
2327 ((stringp (nth 1 x)) x)
2328 ((not (nth 1 x)) (cons (car x) (cons "" (cddr x))))
2329 (t (cons (car x) (cons "" (cdr x))))))
2330 cmds)))
2332 (defun org-agenda-get-restriction-and-command (prefix-descriptions)
2333 "The user interface for selecting an agenda command."
2334 (catch 'exit
2335 (let* ((bfn (buffer-file-name (buffer-base-buffer)))
2336 (restrict-ok (and bfn (org-mode-p)))
2337 (region-p (org-region-active-p))
2338 (custom org-agenda-custom-commands)
2339 (selstring "")
2340 restriction second-time
2341 c entry key type match prefixes rmheader header-end custom1 desc
2342 line lines left right n n1)
2343 (save-window-excursion
2344 (delete-other-windows)
2345 (org-switch-to-buffer-other-window " *Agenda Commands*")
2346 (erase-buffer)
2347 (insert (eval-when-compile
2348 (let ((header
2350 Press key for an agenda command: < Buffer, subtree/region restriction
2351 -------------------------------- > Remove restriction
2352 a Agenda for current week or day e Export agenda views
2353 t List of all TODO entries T Entries with special TODO kwd
2354 m Match a TAGS/PROP/TODO query M Like m, but only TODO entries
2355 L Timeline for current buffer # List stuck projects (!=configure)
2356 s Search for keywords C Configure custom agenda commands
2357 / Multi-occur ? Find :FLAGGED: entries
2359 (start 0))
2360 (while (string-match
2361 "\\(^\\| \\|(\\)\\(\\S-\\)\\( \\|=\\)"
2362 header start)
2363 (setq start (match-end 0))
2364 (add-text-properties (match-beginning 2) (match-end 2)
2365 '(face bold) header))
2366 header)))
2367 (setq header-end (move-marker (make-marker) (point)))
2368 (while t
2369 (setq custom1 custom)
2370 (when (eq rmheader t)
2371 (org-goto-line 1)
2372 (re-search-forward ":" nil t)
2373 (delete-region (match-end 0) (point-at-eol))
2374 (forward-char 1)
2375 (looking-at "-+")
2376 (delete-region (match-end 0) (point-at-eol))
2377 (move-marker header-end (match-end 0)))
2378 (goto-char header-end)
2379 (delete-region (point) (point-max))
2381 ;; Produce all the lines that describe custom commands and prefixes
2382 (setq lines nil)
2383 (while (setq entry (pop custom1))
2384 (setq key (car entry) desc (nth 1 entry)
2385 type (nth 2 entry)
2386 match (nth 3 entry))
2387 (if (> (length key) 1)
2388 (add-to-list 'prefixes (string-to-char key))
2389 (setq line
2390 (format
2391 "%-4s%-14s"
2392 (org-add-props (copy-sequence key)
2393 '(face bold))
2394 (cond
2395 ((string-match "\\S-" desc) desc)
2396 ((eq type 'agenda) "Agenda for current week or day")
2397 ((eq type 'alltodo) "List of all TODO entries")
2398 ((eq type 'search) "Word search")
2399 ((eq type 'stuck) "List of stuck projects")
2400 ((eq type 'todo) "TODO keyword")
2401 ((eq type 'tags) "Tags query")
2402 ((eq type 'tags-todo) "Tags (TODO)")
2403 ((eq type 'tags-tree) "Tags tree")
2404 ((eq type 'todo-tree) "TODO kwd tree")
2405 ((eq type 'occur-tree) "Occur tree")
2406 ((functionp type) (if (symbolp type)
2407 (symbol-name type)
2408 "Lambda expression"))
2409 (t "???"))))
2410 (if org-agenda-menu-show-matcher
2411 (setq line
2412 (concat line ": "
2413 (cond
2414 ((stringp match)
2415 (setq match (copy-sequence match))
2416 (org-add-props match nil 'face 'org-warning))
2417 (match
2418 (format "set of %d commands" (length match)))
2419 (t ""))))
2420 (if (org-string-nw-p match)
2421 (add-text-properties
2422 0 (length line) (list 'help-echo
2423 (concat "Matcher: "match)) line)))
2424 (push line lines)))
2425 (setq lines (nreverse lines))
2426 (when prefixes
2427 (mapc (lambda (x)
2428 (push
2429 (format "%s %s"
2430 (org-add-props (char-to-string x)
2431 nil 'face 'bold)
2432 (or (cdr (assoc (concat selstring
2433 (char-to-string x))
2434 prefix-descriptions))
2435 "Prefix key"))
2436 lines))
2437 prefixes))
2439 ;; Check if we should display in two columns
2440 (if org-agenda-menu-two-column
2441 (progn
2442 (setq n (length lines)
2443 n1 (+ (/ n 2) (mod n 2))
2444 right (nthcdr n1 lines)
2445 left (copy-sequence lines))
2446 (setcdr (nthcdr (1- n1) left) nil))
2447 (setq left lines right nil))
2448 (while left
2449 (insert "\n" (pop left))
2450 (when right
2451 (if (< (current-column) 40)
2452 (move-to-column 40 t)
2453 (insert " "))
2454 (insert (pop right))))
2456 ;; Make the window the right size
2457 (goto-char (point-min))
2458 (if second-time
2459 (if (not (pos-visible-in-window-p (point-max)))
2460 (org-fit-window-to-buffer))
2461 (setq second-time t)
2462 (org-fit-window-to-buffer))
2464 ;; Ask for selection
2465 (message "Press key for agenda command%s:"
2466 (if (or restrict-ok org-agenda-overriding-restriction)
2467 (if org-agenda-overriding-restriction
2468 " (restriction lock active)"
2469 (if restriction
2470 (format " (restricted to %s)" restriction)
2471 " (unrestricted)"))
2472 ""))
2473 (setq c (read-char-exclusive))
2474 (message "")
2475 (cond
2476 ((assoc (char-to-string c) custom)
2477 (setq selstring (concat selstring (char-to-string c)))
2478 (throw 'exit (cons selstring restriction)))
2479 ((memq c prefixes)
2480 (setq selstring (concat selstring (char-to-string c))
2481 prefixes nil
2482 rmheader (or rmheader t)
2483 custom (delq nil (mapcar
2484 (lambda (x)
2485 (if (or (= (length (car x)) 1)
2486 (/= (string-to-char (car x)) c))
2488 (cons (substring (car x) 1) (cdr x))))
2489 custom))))
2490 ((and (not restrict-ok) (memq c '(?1 ?0 ?<)))
2491 (message "Restriction is only possible in Org-mode buffers")
2492 (ding) (sit-for 1))
2493 ((eq c ?1)
2494 (org-agenda-remove-restriction-lock 'noupdate)
2495 (setq restriction 'buffer))
2496 ((eq c ?0)
2497 (org-agenda-remove-restriction-lock 'noupdate)
2498 (setq restriction (if region-p 'region 'subtree)))
2499 ((eq c ?<)
2500 (org-agenda-remove-restriction-lock 'noupdate)
2501 (setq restriction
2502 (cond
2503 ((eq restriction 'buffer)
2504 (if region-p 'region 'subtree))
2505 ((memq restriction '(subtree region))
2506 nil)
2507 (t 'buffer))))
2508 ((eq c ?>)
2509 (org-agenda-remove-restriction-lock 'noupdate)
2510 (setq restriction nil))
2511 ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
2512 (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
2513 ((and (> (length selstring) 0) (eq c ?\d))
2514 (delete-window)
2515 (org-agenda-get-restriction-and-command prefix-descriptions))
2517 ((equal c ?q) (error "Abort"))
2518 (t (error "Invalid key %c" c))))))))
2520 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
2521 (defvar org-agenda-last-arguments nil
2522 "The arguments of the previous call to `org-agenda'.")
2523 (defun org-agenda-run-series (name series)
2524 (org-let (nth 1 series) '(org-prepare-agenda name))
2525 (let* ((org-agenda-multi t)
2526 (redo (list 'org-agenda-run-series name (list 'quote series)))
2527 (org-agenda-overriding-arguments
2528 (or org-agenda-overriding-arguments
2529 (unless (null (delq nil (get 'org-agenda-redo-command 'last-args)))
2530 (get 'org-agenda-redo-command 'last-args))))
2531 (cmds (car series))
2532 (gprops (nth 1 series))
2533 match ;; The byte compiler incorrectly complains about this. Keep it!
2534 cmd type lprops)
2535 (while (setq cmd (pop cmds))
2536 (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
2537 (cond
2538 ((eq type 'agenda)
2539 (org-let2 gprops lprops
2540 '(call-interactively 'org-agenda-list)))
2541 ((eq type 'alltodo)
2542 (org-let2 gprops lprops
2543 '(call-interactively 'org-todo-list)))
2544 ((eq type 'search)
2545 (org-let2 gprops lprops
2546 '(org-search-view current-prefix-arg match nil)))
2547 ((eq type 'stuck)
2548 (org-let2 gprops lprops
2549 '(call-interactively 'org-agenda-list-stuck-projects)))
2550 ((eq type 'tags)
2551 (org-let2 gprops lprops
2552 '(org-tags-view current-prefix-arg match)))
2553 ((eq type 'tags-todo)
2554 (org-let2 gprops lprops
2555 '(org-tags-view '(4) match)))
2556 ((eq type 'todo)
2557 (org-let2 gprops lprops
2558 '(org-todo-list match)))
2559 ((fboundp type)
2560 (org-let2 gprops lprops
2561 '(funcall type match)))
2562 (t (error "Invalid type in command series"))))
2563 (widen)
2564 (setq org-agenda-redo-command redo)
2565 (put 'org-agenda-redo-command 'last-args org-agenda-last-arguments)
2566 (goto-char (point-min)))
2567 (org-fit-agenda-window)
2568 (org-let (nth 1 series) '(org-finalize-agenda)))
2570 ;;;###autoload
2571 (defmacro org-batch-agenda (cmd-key &rest parameters)
2572 "Run an agenda command in batch mode and send the result to STDOUT.
2573 If CMD-KEY is a string of length 1, it is used as a key in
2574 `org-agenda-custom-commands' and triggers this command. If it is a
2575 longer string it is used as a tags/todo match string.
2576 Parameters are alternating variable names and values that will be bound
2577 before running the agenda command."
2578 (let (pars)
2579 (while parameters
2580 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2581 (if (> (length cmd-key) 2)
2582 (eval (list 'let (nreverse pars)
2583 (list 'org-tags-view nil cmd-key)))
2584 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2585 (set-buffer org-agenda-buffer-name)
2586 (princ (org-encode-for-stdout (buffer-string)))))
2588 ;(defun org-encode-for-stdout (string)
2589 ; (if (fboundp 'encode-coding-string)
2590 ; (encode-coding-string string buffer-file-coding-system)
2591 ; string))
2593 (defun org-encode-for-stdout (string)
2594 string)
2596 (defvar org-agenda-info nil)
2598 ;;;###autoload
2599 (defmacro org-batch-agenda-csv (cmd-key &rest parameters)
2600 "Run an agenda command in batch mode and send the result to STDOUT.
2601 If CMD-KEY is a string of length 1, it is used as a key in
2602 `org-agenda-custom-commands' and triggers this command. If it is a
2603 longer string it is used as a tags/todo match string.
2604 Parameters are alternating variable names and values that will be bound
2605 before running the agenda command.
2607 The output gives a line for each selected agenda item. Each
2608 item is a list of comma-separated values, like this:
2610 category,head,type,todo,tags,date,time,extra,priority-l,priority-n
2612 category The category of the item
2613 head The headline, without TODO kwd, TAGS and PRIORITY
2614 type The type of the agenda entry, can be
2615 todo selected in TODO match
2616 tagsmatch selected in tags match
2617 diary imported from diary
2618 deadline a deadline on given date
2619 scheduled scheduled on given date
2620 timestamp entry has timestamp on given date
2621 closed entry was closed on given date
2622 upcoming-deadline warning about deadline
2623 past-scheduled forwarded scheduled item
2624 block entry has date block including g. date
2625 todo The todo keyword, if any
2626 tags All tags including inherited ones, separated by colons
2627 date The relevant date, like 2007-2-14
2628 time The time, like 15:00-16:50
2629 extra Sting with extra planning info
2630 priority-l The priority letter if any was given
2631 priority-n The computed numerical priority
2632 agenda-day The day in the agenda where this is listed"
2634 (let (pars)
2635 (while parameters
2636 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2637 (push (list 'org-agenda-remove-tags t) pars)
2638 (if (> (length cmd-key) 2)
2639 (eval (list 'let (nreverse pars)
2640 (list 'org-tags-view nil cmd-key)))
2641 (eval (list 'let (nreverse pars) (list 'org-agenda nil cmd-key))))
2642 (set-buffer org-agenda-buffer-name)
2643 (let* ((lines (org-split-string (buffer-string) "\n"))
2644 line)
2645 (while (setq line (pop lines))
2646 (catch 'next
2647 (if (not (get-text-property 0 'org-category line)) (throw 'next nil))
2648 (setq org-agenda-info
2649 (org-fix-agenda-info (text-properties-at 0 line)))
2650 (princ
2651 (org-encode-for-stdout
2652 (mapconcat 'org-agenda-export-csv-mapper
2653 '(org-category txt type todo tags date time extra
2654 priority-letter priority agenda-day)
2655 ",")))
2656 (princ "\n"))))))
2658 (defun org-fix-agenda-info (props)
2659 "Make sure all properties on an agenda item have a canonical form.
2660 This ensures the export commands can easily use it."
2661 (let (tmp re)
2662 (when (setq tmp (plist-get props 'tags))
2663 (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
2664 (when (setq tmp (plist-get props 'date))
2665 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2666 (let ((calendar-date-display-form '(year "-" month "-" day)))
2667 '((format "%4d, %9s %2s, %4s" dayname monthname day year))
2669 (setq tmp (calendar-date-string tmp)))
2670 (setq props (plist-put props 'date tmp)))
2671 (when (setq tmp (plist-get props 'day))
2672 (if (integerp tmp) (setq tmp (calendar-gregorian-from-absolute tmp)))
2673 (let ((calendar-date-display-form '(year "-" month "-" day)))
2674 (setq tmp (calendar-date-string tmp)))
2675 (setq props (plist-put props 'day tmp))
2676 (setq props (plist-put props 'agenda-day tmp)))
2677 (when (setq tmp (plist-get props 'txt))
2678 (when (string-match "\\[#\\([A-Z0-9]\\)\\] ?" tmp)
2679 (plist-put props 'priority-letter (match-string 1 tmp))
2680 (setq tmp (replace-match "" t t tmp)))
2681 (when (and (setq re (plist-get props 'org-todo-regexp))
2682 (setq re (concat "\\`\\.*" re " ?"))
2683 (string-match re tmp))
2684 (plist-put props 'todo (match-string 1 tmp))
2685 (setq tmp (replace-match "" t t tmp)))
2686 (plist-put props 'txt tmp)))
2687 props)
2689 (defun org-agenda-export-csv-mapper (prop)
2690 (let ((res (plist-get org-agenda-info prop)))
2691 (setq res
2692 (cond
2693 ((not res) "")
2694 ((stringp res) res)
2695 (t (prin1-to-string res))))
2696 (while (string-match "," res)
2697 (setq res (replace-match ";" t t res)))
2698 (org-trim res)))
2701 ;;;###autoload
2702 (defun org-store-agenda-views (&rest parameters)
2703 (interactive)
2704 (eval (list 'org-batch-store-agenda-views)))
2706 ;; FIXME, why is this a macro?????
2707 ;;;###autoload
2708 (defmacro org-batch-store-agenda-views (&rest parameters)
2709 "Run all custom agenda commands that have a file argument."
2710 (let ((cmds (org-agenda-normalize-custom-commands org-agenda-custom-commands))
2711 (pop-up-frames nil)
2712 (dir default-directory)
2713 pars cmd thiscmdkey files opts cmd-or-set)
2714 (while parameters
2715 (push (list (pop parameters) (if parameters (pop parameters))) pars))
2716 (setq pars (reverse pars))
2717 (save-window-excursion
2718 (while cmds
2719 (setq cmd (pop cmds)
2720 thiscmdkey (car cmd)
2721 cmd-or-set (nth 2 cmd)
2722 opts (nth (if (listp cmd-or-set) 3 4) cmd)
2723 files (nth (if (listp cmd-or-set) 4 5) cmd))
2724 (if (stringp files) (setq files (list files)))
2725 (when files
2726 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2727 (list 'org-agenda nil thiscmdkey)))
2728 (set-buffer org-agenda-buffer-name)
2729 (while files
2730 (eval (list 'let (append org-agenda-exporter-settings opts pars)
2731 (list 'org-write-agenda
2732 (expand-file-name (pop files) dir) nil t))))
2733 (and (get-buffer org-agenda-buffer-name)
2734 (kill-buffer org-agenda-buffer-name)))))))
2736 (defun org-agenda-mark-header-line (pos)
2737 "Mark the line at POS as an agenda structure header."
2738 (save-excursion
2739 (goto-char pos)
2740 (put-text-property (point-at-bol) (point-at-eol)
2741 'org-agenda-structural-header t)
2742 (when org-agenda-title-append
2743 (put-text-property (point-at-bol) (point-at-eol)
2744 'org-agenda-title-append org-agenda-title-append))))
2746 (defvar org-mobile-creating-agendas)
2747 (defun org-write-agenda (file &optional open nosettings)
2748 "Write the current buffer (an agenda view) as a file.
2749 Depending on the extension of the file name, plain text (.txt),
2750 HTML (.html or .htm) or Postscript (.ps) is produced.
2751 If the extension is .ics, run icalendar export over all files used
2752 to construct the agenda and limit the export to entries listed in the
2753 agenda now.
2754 With prefix argument OPEN, open the new file immediately.
2755 If NOSETTINGS is given, do not scope the settings of
2756 `org-agenda-exporter-settings' into the export commands. This is used when
2757 the settings have already been scoped and we do not wish to overrule other,
2758 higher priority settings."
2759 (interactive "FWrite agenda to file: \nP")
2760 (if (not (file-writable-p file))
2761 (error "Cannot write agenda to file %s" file))
2762 (org-let (if nosettings nil org-agenda-exporter-settings)
2763 '(save-excursion
2764 (save-window-excursion
2765 (org-agenda-mark-filtered-text)
2766 (let ((bs (copy-sequence (buffer-string))) beg)
2767 (org-agenda-unmark-filtered-text)
2768 (with-temp-buffer
2769 (rename-buffer "Agenda View" t)
2770 (set-buffer-modified-p nil)
2771 (insert bs)
2772 (org-agenda-remove-marked-text 'org-filtered)
2773 (while (setq beg (text-property-any (point-min) (point-max)
2774 'org-filtered t))
2775 (delete-region
2776 beg (or (next-single-property-change beg 'org-filtered)
2777 (point-max))))
2778 (run-hooks 'org-agenda-before-write-hook)
2779 (cond
2780 ((org-bound-and-true-p org-mobile-creating-agendas)
2781 (org-mobile-write-agenda-for-mobile file))
2782 ((string-match "\\.html?\\'" file)
2783 (require 'htmlize)
2784 (set-buffer (htmlize-buffer (current-buffer)))
2786 (when (and org-agenda-export-html-style
2787 (string-match "<style>" org-agenda-export-html-style))
2788 ;; replace <style> section with org-agenda-export-html-style
2789 (goto-char (point-min))
2790 (kill-region (- (search-forward "<style") 6)
2791 (search-forward "</style>"))
2792 (insert org-agenda-export-html-style))
2793 (write-file file)
2794 (kill-buffer (current-buffer))
2795 (message "HTML written to %s" file))
2796 ((string-match "\\.ps\\'" file)
2797 (require 'ps-print)
2798 (ps-print-buffer-with-faces file)
2799 (message "Postscript written to %s" file))
2800 ((string-match "\\.pdf\\'" file)
2801 (require 'ps-print)
2802 (ps-print-buffer-with-faces
2803 (concat (file-name-sans-extension file) ".ps"))
2804 (call-process "ps2pdf" nil nil nil
2805 (expand-file-name
2806 (concat (file-name-sans-extension file) ".ps"))
2807 (expand-file-name file))
2808 (delete-file (concat (file-name-sans-extension file) ".ps"))
2809 (message "PDF written to %s" file))
2810 ((string-match "\\.ics\\'" file)
2811 (require 'org-icalendar)
2812 (let ((org-agenda-marker-table
2813 (org-create-marker-find-array
2814 (org-agenda-collect-markers)))
2815 (org-icalendar-verify-function 'org-check-agenda-marker-table)
2816 (org-combined-agenda-icalendar-file file))
2817 (apply 'org-export-icalendar 'combine
2818 (org-agenda-files nil 'ifmode))))
2820 (let ((bs (buffer-string)))
2821 (find-file file)
2822 (erase-buffer)
2823 (insert bs)
2824 (save-buffer 0)
2825 (kill-buffer (current-buffer))
2826 (message "Plain text written to %s" file))))))))
2827 (set-buffer org-agenda-buffer-name))
2828 (when open (org-open-file file)))
2830 (defvar org-agenda-filter-overlays nil)
2832 (defun org-agenda-mark-filtered-text ()
2833 "Mark all text hidden by filtering with a text property."
2834 (let ((inhibit-read-only t))
2835 (mapc
2836 (lambda (o)
2837 (when (equal (overlay-buffer o) (current-buffer))
2838 (put-text-property
2839 (overlay-start o) (overlay-end o)
2840 'org-filtered t)))
2841 org-agenda-filter-overlays)))
2843 (defun org-agenda-unmark-filtered-text ()
2844 "Remove the filtering text property."
2845 (let ((inhibit-read-only t))
2846 (remove-text-properties (point-min) (point-max) '(org-filtered t))))
2848 (defun org-agenda-remove-marked-text (property &optional value)
2849 "Delete all text marked with VALUE of PROPERTY.
2850 VALUE defaults to t."
2851 (let (beg)
2852 (setq value (or value t))
2853 (while (setq beg (text-property-any (point-min) (point-max)
2854 property value))
2855 (delete-region
2856 beg (or (next-single-property-change beg 'org-filtered)
2857 (point-max))))))
2859 (defun org-agenda-add-entry-text ()
2860 "Add entry text to agenda lines.
2861 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
2862 entry text following headings shown in the agenda.
2863 Drawers will be excluded, also the line with scheduling/deadline info."
2864 (when (and (> org-agenda-add-entry-text-maxlines 0)
2865 (not (org-bound-and-true-p org-mobile-creating-agendas)))
2866 (let (m txt)
2867 (goto-char (point-min))
2868 (while (not (eobp))
2869 (if (not (setq m (org-get-at-bol 'org-hd-marker)))
2870 (beginning-of-line 2)
2871 (setq txt (org-agenda-get-some-entry-text
2872 m org-agenda-add-entry-text-maxlines " > "))
2873 (end-of-line 1)
2874 (if (string-match "\\S-" txt)
2875 (insert "\n" txt)
2876 (or (eobp) (forward-char 1))))))))
2878 (defun org-agenda-get-some-entry-text (marker n-lines &optional indent
2879 &rest keep)
2880 "Extract entry text from MARKER, at most N-LINES lines.
2881 This will ignore drawers etc, just get the text.
2882 If INDENT is given, prefix every line with this string. If KEEP is
2883 given, it is a list of symbols, defining stuff that should not be
2884 removed from the entry content. Currently only `planning' is allowed here."
2885 (let (txt drawer-re kwd-time-re ind)
2886 (save-excursion
2887 (with-current-buffer (marker-buffer marker)
2888 (if (not (org-mode-p))
2889 (setq txt "")
2890 (save-excursion
2891 (save-restriction
2892 (widen)
2893 (goto-char marker)
2894 (end-of-line 1)
2895 (setq txt (buffer-substring
2896 (min (1+ (point)) (point-max))
2897 (progn (outline-next-heading) (point)))
2898 drawer-re org-drawer-regexp
2899 kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
2900 ".*\n?"))
2901 (with-temp-buffer
2902 (insert txt)
2903 (when org-agenda-add-entry-text-descriptive-links
2904 (goto-char (point-min))
2905 (while (org-activate-bracket-links (point-max))
2906 (add-text-properties (match-beginning 0) (match-end 0)
2907 '(face org-link))))
2908 (goto-char (point-min))
2909 (while (re-search-forward org-bracket-link-regexp (point-max) t)
2910 (set-text-properties (match-beginning 0) (match-end 0)
2911 nil))
2912 (goto-char (point-min))
2913 (while (re-search-forward drawer-re nil t)
2914 (delete-region
2915 (match-beginning 0)
2916 (progn (re-search-forward
2917 "^[ \t]*:END:.*\n?" nil 'move)
2918 (point))))
2919 (unless (member 'planning keep)
2920 (goto-char (point-min))
2921 (while (re-search-forward kwd-time-re nil t)
2922 (replace-match "")))
2923 (goto-char (point-min))
2924 (when org-agenda-entry-text-exclude-regexps
2925 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
2926 (while (setq re (pop re-list))
2927 (goto-char (point-min))
2928 (while (re-search-forward re nil t)
2929 (replace-match "")))))
2930 (goto-char (point-max))
2931 (skip-chars-backward " \t\n")
2932 (if (looking-at "[ \t\n]+\\'") (replace-match ""))
2934 ;; find and remove min common indentation
2935 (goto-char (point-min))
2936 (untabify (point-min) (point-max))
2937 (setq ind (org-get-indentation))
2938 (while (not (eobp))
2939 (unless (looking-at "[ \t]*$")
2940 (setq ind (min ind (org-get-indentation))))
2941 (beginning-of-line 2))
2942 (goto-char (point-min))
2943 (while (not (eobp))
2944 (unless (looking-at "[ \t]*$")
2945 (move-to-column ind)
2946 (delete-region (point-at-bol) (point)))
2947 (beginning-of-line 2))
2949 (run-hooks 'org-agenda-entry-text-cleanup-hook)
2951 (goto-char (point-min))
2952 (when indent
2953 (while (and (not (eobp)) (re-search-forward "^" nil t))
2954 (replace-match indent t t)))
2955 (goto-char (point-min))
2956 (while (looking-at "[ \t]*\n") (replace-match ""))
2957 (goto-char (point-max))
2958 (when (> (org-current-line)
2959 n-lines)
2960 (org-goto-line (1+ n-lines))
2961 (backward-char 1))
2962 (setq txt (buffer-substring (point-min) (point)))))))))
2963 txt))
2965 (defun org-agenda-collect-markers ()
2966 "Collect the markers pointing to entries in the agenda buffer."
2967 (let (m markers)
2968 (save-excursion
2969 (goto-char (point-min))
2970 (while (not (eobp))
2971 (when (setq m (or (org-get-at-bol 'org-hd-marker)
2972 (org-get-at-bol 'org-marker)))
2973 (push m markers))
2974 (beginning-of-line 2)))
2975 (nreverse markers)))
2977 (defun org-create-marker-find-array (marker-list)
2978 "Create a alist of files names with all marker positions in that file."
2979 (let (f tbl m a p)
2980 (while (setq m (pop marker-list))
2981 (setq p (marker-position m)
2982 f (buffer-file-name (or (buffer-base-buffer
2983 (marker-buffer m))
2984 (marker-buffer m))))
2985 (if (setq a (assoc f tbl))
2986 (push (marker-position m) (cdr a))
2987 (push (list f p) tbl)))
2988 (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
2989 tbl)))
2991 (defvar org-agenda-marker-table nil) ; dynamically scoped parameter
2992 (defun org-check-agenda-marker-table ()
2993 "Check of the current entry is on the marker list."
2994 (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
2996 (and (setq a (assoc file org-agenda-marker-table))
2997 (save-match-data
2998 (save-excursion
2999 (org-back-to-heading t)
3000 (member (point) (cdr a)))))))
3002 (defun org-check-for-org-mode ()
3003 "Make sure current buffer is in org-mode. Error if not."
3004 (or (org-mode-p)
3005 (error "Cannot execute org-mode agenda command on buffer in %s"
3006 major-mode)))
3008 (defun org-fit-agenda-window ()
3009 "Fit the window to the buffer size."
3010 (and (memq org-agenda-window-setup '(reorganize-frame))
3011 (fboundp 'fit-window-to-buffer)
3012 (org-fit-window-to-buffer
3014 (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
3015 (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
3017 ;;; Agenda prepare and finalize
3019 (defvar org-agenda-multi nil) ; dynamically scoped
3020 (defvar org-agenda-buffer-name "*Org Agenda*")
3021 (defvar org-pre-agenda-window-conf nil)
3022 (defvar org-agenda-columns-active nil)
3023 (defvar org-agenda-name nil)
3024 (defvar org-agenda-filter nil)
3025 (defvar org-agenda-filter-while-redo nil)
3026 (defvar org-agenda-filter-preset nil
3027 "A preset of the tags filter used for secondary agenda filtering.
3028 This must be a list of strings, each string must be a single tag preceded
3029 by \"+\" or \"-\".
3030 This variable should not be set directly, but agenda custom commands can
3031 bind it in the options section. The preset filter is a global property of
3032 the entire agenda view. In a block agenda, it will not work reliably to
3033 define a filter for one of the individual blocks. You need to set it in
3034 the global options and expect it to be applied to the entire view.")
3036 (defun org-prepare-agenda (&optional name)
3037 (setq org-todo-keywords-for-agenda nil)
3038 (setq org-done-keywords-for-agenda nil)
3039 (setq org-drawers-for-agenda nil)
3040 (unless org-agenda-persistent-filter
3041 (setq org-agenda-filter nil))
3042 (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
3043 (if org-agenda-multi
3044 (progn
3045 (setq buffer-read-only nil)
3046 (goto-char (point-max))
3047 (unless (or (bobp) org-agenda-compact-blocks
3048 (not org-agenda-block-separator))
3049 (insert "\n"
3050 (if (stringp org-agenda-block-separator)
3051 org-agenda-block-separator
3052 (make-string (window-width) org-agenda-block-separator))
3053 "\n"))
3054 (narrow-to-region (point) (point-max)))
3055 (org-agenda-reset-markers)
3056 (setq org-agenda-contributing-files nil)
3057 (setq org-agenda-columns-active nil)
3058 (org-prepare-agenda-buffers (org-agenda-files nil 'ifmode))
3059 (setq org-todo-keywords-for-agenda
3060 (org-uniquify org-todo-keywords-for-agenda))
3061 (setq org-done-keywords-for-agenda
3062 (org-uniquify org-done-keywords-for-agenda))
3063 (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
3064 (let* ((abuf (get-buffer-create org-agenda-buffer-name))
3065 (awin (get-buffer-window abuf)))
3066 (cond
3067 ((equal (current-buffer) abuf) nil)
3068 (awin (select-window awin))
3069 ((not (setq org-pre-agenda-window-conf (current-window-configuration))))
3070 ((equal org-agenda-window-setup 'current-window)
3071 (org-pop-to-buffer-same-window abuf))
3072 ((equal org-agenda-window-setup 'other-window)
3073 (org-switch-to-buffer-other-window abuf))
3074 ((equal org-agenda-window-setup 'other-frame)
3075 (switch-to-buffer-other-frame abuf))
3076 ((equal org-agenda-window-setup 'reorganize-frame)
3077 (delete-other-windows)
3078 (org-switch-to-buffer-other-window abuf)))
3079 ;; additional test in case agenda is invoked from within agenda
3080 ;; buffer via elisp link
3081 (unless (equal (current-buffer) abuf)
3082 (org-pop-to-buffer-same-window abuf)))
3083 (setq buffer-read-only nil)
3084 (let ((inhibit-read-only t)) (erase-buffer))
3085 (org-agenda-mode)
3086 (and name (not org-agenda-name)
3087 (org-set-local 'org-agenda-name name)))
3088 (setq buffer-read-only nil))
3090 (defun org-finalize-agenda ()
3091 "Finishing touch for the agenda buffer, called just before displaying it."
3092 (unless org-agenda-multi
3093 (save-excursion
3094 (let ((inhibit-read-only t))
3095 (goto-char (point-min))
3096 (while (org-activate-bracket-links (point-max))
3097 (add-text-properties (match-beginning 0) (match-end 0)
3098 '(face org-link)))
3099 (org-agenda-align-tags)
3100 (unless org-agenda-with-colors
3101 (remove-text-properties (point-min) (point-max) '(face nil))))
3102 (if (and (boundp 'org-agenda-overriding-columns-format)
3103 org-agenda-overriding-columns-format)
3104 (org-set-local 'org-agenda-overriding-columns-format
3105 org-agenda-overriding-columns-format))
3106 (if (and (boundp 'org-agenda-view-columns-initially)
3107 org-agenda-view-columns-initially)
3108 (org-agenda-columns))
3109 (when org-agenda-fontify-priorities
3110 (org-agenda-fontify-priorities))
3111 (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
3112 (org-agenda-dim-blocked-tasks))
3113 (org-agenda-mark-clocking-task)
3114 (when org-agenda-entry-text-mode
3115 (org-agenda-entry-text-hide)
3116 (org-agenda-entry-text-show))
3117 (if (functionp 'org-habit-insert-consistency-graphs)
3118 (org-habit-insert-consistency-graphs))
3119 (run-hooks 'org-finalize-agenda-hook)
3120 (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
3121 (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
3122 (org-agenda-filter-apply org-agenda-filter))
3125 (defun org-agenda-mark-clocking-task ()
3126 "Mark the current clock entry in the agenda if it is present."
3127 (mapc (lambda (o)
3128 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
3129 (delete-overlay o)))
3130 (overlays-in (point-min) (point-max)))
3131 (when (marker-buffer org-clock-hd-marker)
3132 (save-excursion
3133 (goto-char (point-min))
3134 (let (s ov)
3135 (while (setq s (next-single-property-change (point) 'org-hd-marker))
3136 (goto-char s)
3137 (when (equal (org-get-at-bol 'org-hd-marker)
3138 org-clock-hd-marker)
3139 (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
3140 (overlay-put ov 'type 'org-agenda-clocking)
3141 (overlay-put ov 'face 'org-agenda-clocking)
3142 (overlay-put ov 'help-echo
3143 "The clock is running in this item")))))))
3145 (defun org-agenda-fontify-priorities ()
3146 "Make highest priority lines bold, and lowest italic."
3147 (interactive)
3148 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
3149 (delete-overlay o)))
3150 (overlays-in (point-min) (point-max)))
3151 (save-excursion
3152 (let ((inhibit-read-only t)
3153 b e p ov h l)
3154 (goto-char (point-min))
3155 (while (re-search-forward "\\[#\\(.\\)\\]" nil t)
3156 (setq h (or (get-char-property (point) 'org-highest-priority)
3157 org-highest-priority)
3158 l (or (get-char-property (point) 'org-lowest-priority)
3159 org-lowest-priority)
3160 p (string-to-char (match-string 1))
3161 b (match-beginning 0)
3162 e (if (eq org-agenda-fontify-priorities 'cookies)
3163 (match-end 0)
3164 (point-at-eol))
3165 ov (make-overlay b e))
3166 (overlay-put
3167 ov 'face
3168 (cond ((org-face-from-face-or-color
3169 'priority nil
3170 (cdr (assoc p org-priority-faces))))
3171 ((and (listp org-agenda-fontify-priorities)
3172 (org-face-from-face-or-color
3173 'priority nil
3174 (cdr (assoc p org-agenda-fontify-priorities)))))
3175 ((equal p l) 'italic)
3176 ((equal p h) 'bold)))
3177 (overlay-put ov 'org-type 'org-priority)))))
3179 (defun org-agenda-dim-blocked-tasks ()
3180 "Dim currently blocked TODO's in the agenda display."
3181 (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
3182 (delete-overlay o)))
3183 (overlays-in (point-min) (point-max)))
3184 (save-excursion
3185 (let ((inhibit-read-only t)
3186 (org-depend-tag-blocked nil)
3187 (invis (eq org-agenda-dim-blocked-tasks 'invisible))
3188 org-blocked-by-checkboxes
3189 invis1 b e p ov h l)
3190 (goto-char (point-min))
3191 (while (let ((pos (next-single-property-change (point) 'todo-state)))
3192 (and pos (goto-char (1+ pos))))
3193 (setq org-blocked-by-checkboxes nil invis1 invis)
3194 (let ((marker (org-get-at-bol 'org-hd-marker)))
3195 (when (and marker
3196 (not (with-current-buffer (marker-buffer marker)
3197 (save-excursion
3198 (goto-char marker)
3199 (if (org-entry-get nil "NOBLOCKING")
3200 t ;; Never block this entry
3201 (run-hook-with-args-until-failure
3202 'org-blocker-hook
3203 (list :type 'todo-state-change
3204 :position marker
3205 :from 'todo
3206 :to 'done)))))))
3207 (if org-blocked-by-checkboxes (setq invis1 nil))
3208 (setq b (if invis1
3209 (max (point-min) (1- (point-at-bol)))
3210 (point-at-bol))
3211 e (point-at-eol)
3212 ov (make-overlay b e))
3213 (if invis1
3214 (overlay-put ov 'invisible t)
3215 (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
3216 (overlay-put ov 'org-type 'org-blocked-todo)))))))
3218 (defvar org-agenda-skip-function nil
3219 "Function to be called at each match during agenda construction.
3220 If this function returns nil, the current match should not be skipped.
3221 Otherwise, the function must return a position from where the search
3222 should be continued.
3223 This may also be a Lisp form, it will be evaluated.
3224 Never set this variable using `setq' or so, because then it will apply
3225 to all future agenda commands. If you do want a global skipping condition,
3226 use the option `org-agenda-skip-function-global' instead.
3227 The correct usage for `org-agenda-skip-function' is to bind it with
3228 `let' to scope it dynamically into the agenda-constructing command.
3229 A good way to set it is through options in `org-agenda-custom-commands'.")
3231 (defun org-agenda-skip ()
3232 "Throw to `:skip' in places that should be skipped.
3233 Also moves point to the end of the skipped region, so that search can
3234 continue from there."
3235 (let ((p (point-at-bol)) to)
3236 (and org-agenda-skip-archived-trees (not org-agenda-archives-mode)
3237 (get-text-property p :org-archived)
3238 (org-end-of-subtree t)
3239 (throw :skip t))
3240 (and org-agenda-skip-comment-trees
3241 (get-text-property p :org-comment)
3242 (org-end-of-subtree t)
3243 (throw :skip t))
3244 (if (equal (char-after p) ?#) (throw :skip t))
3245 (when (setq to (or (org-agenda-skip-eval org-agenda-skip-function-global)
3246 (org-agenda-skip-eval org-agenda-skip-function)))
3247 (goto-char to)
3248 (throw :skip t))))
3250 (defun org-agenda-skip-eval (form)
3251 "If FORM is a function or a list, call (or eval) is and return result.
3252 `save-excursion' and `save-match-data' are wrapped around the call, so point
3253 and match data are returned to the previous state no matter what these
3254 functions do."
3255 (let (fp)
3256 (and form
3257 (or (setq fp (functionp form))
3258 (consp form))
3259 (save-excursion
3260 (save-match-data
3261 (if fp
3262 (funcall form)
3263 (eval form)))))))
3265 (defvar org-agenda-markers nil
3266 "List of all currently active markers created by `org-agenda'.")
3267 (defvar org-agenda-last-marker-time (org-float-time)
3268 "Creation time of the last agenda marker.")
3270 (defun org-agenda-new-marker (&optional pos)
3271 "Return a new agenda marker.
3272 Org-mode keeps a list of these markers and resets them when they are
3273 no longer in use."
3274 (let ((m (copy-marker (or pos (point)))))
3275 (setq org-agenda-last-marker-time (org-float-time))
3276 (push m org-agenda-markers)
3279 (defun org-agenda-reset-markers ()
3280 "Reset markers created by `org-agenda'."
3281 (while org-agenda-markers
3282 (move-marker (pop org-agenda-markers) nil)))
3284 (defun org-agenda-save-markers-for-cut-and-paste (beg end)
3285 "Save relative positions of markers in region."
3286 (mapc (lambda (m) (org-check-and-save-marker m beg end))
3287 org-agenda-markers))
3289 ;;; Entry text mode
3291 (defun org-agenda-entry-text-show-here ()
3292 "Add some text from the entry as context to the current line."
3293 (let (m txt o)
3294 (setq m (org-get-at-bol 'org-hd-marker))
3295 (unless (marker-buffer m)
3296 (error "No marker points to an entry here"))
3297 (setq txt (concat "\n" (org-no-properties
3298 (org-agenda-get-some-entry-text
3299 m org-agenda-entry-text-maxlines " > "))))
3300 (when (string-match "\\S-" txt)
3301 (setq o (make-overlay (point-at-bol) (point-at-eol)))
3302 (overlay-put o 'evaporate t)
3303 (overlay-put o 'org-overlay-type 'agenda-entry-content)
3304 (overlay-put o 'after-string txt))))
3306 (defun org-agenda-entry-text-show ()
3307 "Add entry context for all agenda lines."
3308 (interactive)
3309 (save-excursion
3310 (goto-char (point-max))
3311 (beginning-of-line 1)
3312 (while (not (bobp))
3313 (when (org-get-at-bol 'org-hd-marker)
3314 (org-agenda-entry-text-show-here))
3315 (beginning-of-line 0))))
3317 (defun org-agenda-entry-text-hide ()
3318 "Remove any shown entry context."
3319 (delq nil
3320 (mapcar (lambda (o)
3321 (if (eq (overlay-get o 'org-overlay-type)
3322 'agenda-entry-content)
3323 (progn (delete-overlay o) t)))
3324 (overlays-in (point-min) (point-max)))))
3326 (defun org-agenda-get-day-face (date)
3327 "Return the face DATE should be displayed with."
3328 (or (and (functionp org-agenda-day-face-function)
3329 (funcall org-agenda-day-face-function date))
3330 (cond ((org-agenda-todayp date)
3331 'org-agenda-date-today)
3332 ((member (calendar-day-of-week date) org-agenda-weekend-days)
3333 'org-agenda-date-weekend)
3334 (t 'org-agenda-date))))
3336 ;;; Agenda timeline
3338 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
3340 (defun org-timeline (&optional include-all)
3341 "Show a time-sorted view of the entries in the current org file.
3342 Only entries with a time stamp of today or later will be listed. With
3343 \\[universal-argument] prefix, all unfinished TODO items will also be shown,
3344 under the current date.
3345 If the buffer contains an active region, only check the region for
3346 dates."
3347 (interactive "P")
3348 (org-compile-prefix-format 'timeline)
3349 (org-set-sorting-strategy 'timeline)
3350 (let* ((dopast t)
3351 (dotodo include-all)
3352 (doclosed org-agenda-show-log)
3353 (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
3354 (current-buffer))))
3355 (date (calendar-current-date))
3356 (beg (if (org-region-active-p) (region-beginning) (point-min)))
3357 (end (if (org-region-active-p) (region-end) (point-max)))
3358 (day-numbers (org-get-all-dates beg end 'no-ranges
3359 t doclosed ; always include today
3360 org-timeline-show-empty-dates))
3361 (org-deadline-warning-days 0)
3362 (org-agenda-only-exact-dates t)
3363 (today (org-today))
3364 (past t)
3365 args
3366 s e rtn d emptyp)
3367 (setq org-agenda-redo-command
3368 (list 'progn
3369 (list 'org-switch-to-buffer-other-window (current-buffer))
3370 (list 'org-timeline (list 'quote include-all))))
3371 (if (not dopast)
3372 ;; Remove past dates from the list of dates.
3373 (setq day-numbers (delq nil (mapcar (lambda(x)
3374 (if (>= x today) x nil))
3375 day-numbers))))
3376 (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
3377 (if doclosed (push :closed args))
3378 (push :timestamp args)
3379 (push :deadline args)
3380 (push :scheduled args)
3381 (push :sexp args)
3382 (if dotodo (push :todo args))
3383 (insert "Timeline of file " entry "\n")
3384 (add-text-properties (point-min) (point)
3385 (list 'face 'org-agenda-structure))
3386 (org-agenda-mark-header-line (point-min))
3387 (while (setq d (pop day-numbers))
3388 (if (and (listp d) (eq (car d) :omitted))
3389 (progn
3390 (setq s (point))
3391 (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
3392 (put-text-property s (1- (point)) 'face 'org-agenda-structure))
3393 (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
3394 (if (and (>= d today)
3395 dopast
3396 past)
3397 (progn
3398 (setq past nil)
3399 (insert (make-string 79 ?-) "\n")))
3400 (setq date (calendar-gregorian-from-absolute d))
3401 (setq s (point))
3402 (setq rtn (and (not emptyp)
3403 (apply 'org-agenda-get-day-entries entry
3404 date args)))
3405 (if (or rtn (equal d today) org-timeline-show-empty-dates)
3406 (progn
3407 (insert
3408 (if (stringp org-agenda-format-date)
3409 (format-time-string org-agenda-format-date
3410 (org-time-from-absolute date))
3411 (funcall org-agenda-format-date date))
3412 "\n")
3413 (put-text-property s (1- (point)) 'face
3414 (org-agenda-get-day-face date))
3415 (put-text-property s (1- (point)) 'org-date-line t)
3416 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3417 (if (equal d today)
3418 (put-text-property s (1- (point)) 'org-today t))
3419 (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
3420 (put-text-property s (1- (point)) 'day d)))))
3421 (goto-char (point-min))
3422 (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
3423 (point-min)))
3424 (add-text-properties (point-min) (point-max) '(org-agenda-type timeline))
3425 (org-finalize-agenda)
3426 (setq buffer-read-only t)))
3428 (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty pre-re)
3429 "Return a list of all relevant day numbers from BEG to END buffer positions.
3430 If NO-RANGES is non-nil, include only the start and end dates of a range,
3431 not every single day in the range. If FORCE-TODAY is non-nil, make
3432 sure that TODAY is included in the list. If INACTIVE is non-nil, also
3433 inactive time stamps (those in square brackets) are included.
3434 When EMPTY is non-nil, also include days without any entries."
3435 (let ((re (concat
3436 (if pre-re pre-re "")
3437 (if inactive org-ts-regexp-both org-ts-regexp)))
3438 dates dates1 date day day1 day2 ts1 ts2)
3439 (if force-today
3440 (setq dates (list (org-today))))
3441 (save-excursion
3442 (goto-char beg)
3443 (while (re-search-forward re end t)
3444 (setq day (time-to-days (org-time-string-to-time
3445 (substring (match-string 1) 0 10))))
3446 (or (memq day dates) (push day dates)))
3447 (unless no-ranges
3448 (goto-char beg)
3449 (while (re-search-forward org-tr-regexp end t)
3450 (setq ts1 (substring (match-string 1) 0 10)
3451 ts2 (substring (match-string 2) 0 10)
3452 day1 (time-to-days (org-time-string-to-time ts1))
3453 day2 (time-to-days (org-time-string-to-time ts2)))
3454 (while (< (setq day1 (1+ day1)) day2)
3455 (or (memq day1 dates) (push day1 dates)))))
3456 (setq dates (sort dates '<))
3457 (when empty
3458 (while (setq day (pop dates))
3459 (setq day2 (car dates))
3460 (push day dates1)
3461 (when (and day2 empty)
3462 (if (or (eq empty t)
3463 (and (numberp empty) (<= (- day2 day) empty)))
3464 (while (< (setq day (1+ day)) day2)
3465 (push (list day) dates1))
3466 (push (cons :omitted (- day2 day)) dates1))))
3467 (setq dates (nreverse dates1)))
3468 dates)))
3470 ;;; Agenda Daily/Weekly
3472 (defvar org-agenda-start-day nil ; dynamically scoped parameter
3473 "Start day for the agenda view.
3474 Custom commands can set this variable in the options section.")
3475 (defvar org-starting-day nil) ; local variable in the agenda buffer
3476 (defvar org-agenda-current-span nil
3477 "The current span used in the agenda view.") ; local variable in the agenda buffer
3478 (defvar org-include-all-loc nil) ; local variable
3480 (defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
3481 "List of types searched for when creating the daily/weekly agenda.
3482 This variable is a list of symbols that controls the types of
3483 items that appear in the daily/weekly agenda. Allowed symbols in this
3484 list are are
3486 :timestamp List items containing a date stamp or date range matching
3487 the selected date. This includes sexp entries in
3488 angular brackets.
3490 :sexp List entries resulting from plain diary-like sexps.
3492 :deadline List deadline due on that date. When the date is today,
3493 also list any deadlines past due, or due within
3494 `org-deadline-warning-days'. `:deadline' must appear before
3495 `:scheduled' if the setting of
3496 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
3497 any effect.
3499 :scheduled List all items which are scheduled for the given date.
3500 The diary for *today* also contains items which were
3501 scheduled earlier and are not yet marked DONE.
3503 By default, all four types are turned on.
3505 Never set this variable globally using `setq', because then it
3506 will apply to all future agenda commands. Instead, bind it with
3507 `let' to scope it dynamically into the agenda-constructing
3508 command. A good way to set it is through options in
3509 `org-agenda-custom-commands'. For a more flexible (though
3510 somewhat less efficient) way of determining what is included in
3511 the daily/weekly agenda, see `org-agenda-skip-function'.")
3513 ;;;###autoload
3514 (defun org-agenda-list (&optional include-all start-day span)
3515 "Produce a daily/weekly view from all files in variable `org-agenda-files'.
3516 The view will be for the current day or week, but from the overview buffer
3517 you will be able to go to other days/weeks.
3519 With a numeric prefix argument in an interactive call, the agenda will
3520 span INCLUDE-ALL days. Lisp programs should instead specify SPAN to change
3521 the number of days. SPAN defaults to `org-agenda-span'.
3523 START-DAY defaults to TODAY, or to the most recent match for the weekday
3524 given in `org-agenda-start-on-weekday'."
3525 (interactive "P")
3526 (if (and (integerp include-all) (> include-all 0))
3527 (setq span include-all include-all nil))
3528 (setq start-day (or start-day org-agenda-start-day))
3529 (if org-agenda-overriding-arguments
3530 (setq include-all (car org-agenda-overriding-arguments)
3531 start-day (nth 1 org-agenda-overriding-arguments)
3532 span (nth 2 org-agenda-overriding-arguments)))
3533 (if (stringp start-day)
3534 ;; Convert to an absolute day number
3535 (setq start-day (time-to-days (org-read-date nil t start-day))))
3536 (setq org-agenda-last-arguments (list include-all start-day span))
3537 (org-compile-prefix-format 'agenda)
3538 (org-set-sorting-strategy 'agenda)
3539 (let* ((span (org-agenda-ndays-to-span
3540 (or span org-agenda-ndays org-agenda-span)))
3541 (today (org-today))
3542 (sd (or start-day today))
3543 (ndays (org-agenda-span-to-ndays span sd))
3544 (org-agenda-start-on-weekday
3545 (if (eq ndays 7)
3546 org-agenda-start-on-weekday))
3547 (thefiles (org-agenda-files nil 'ifmode))
3548 (files thefiles)
3549 (start (if (or (null org-agenda-start-on-weekday)
3550 (< ndays 7))
3552 (let* ((nt (calendar-day-of-week
3553 (calendar-gregorian-from-absolute sd)))
3554 (n1 org-agenda-start-on-weekday)
3555 (d (- nt n1)))
3556 (- sd (+ (if (< d 0) 7 0) d)))))
3557 (day-numbers (list start))
3558 (day-cnt 0)
3559 (inhibit-redisplay (not debug-on-error))
3560 s e rtn rtnall file date d start-pos end-pos todayp
3561 clocktable-start clocktable-end filter)
3562 (setq org-agenda-redo-command
3563 (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
3564 (dotimes (n (1- ndays))
3565 (push (1+ (car day-numbers)) day-numbers))
3566 (setq day-numbers (nreverse day-numbers))
3567 (setq clocktable-start (car day-numbers)
3568 clocktable-end (1+ (or (org-last day-numbers) 0)))
3569 (org-prepare-agenda "Day/Week")
3570 (org-set-local 'org-starting-day (car day-numbers))
3571 (org-set-local 'org-include-all-loc include-all)
3572 (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
3573 (unless org-agenda-compact-blocks
3574 (let* ((d1 (car day-numbers))
3575 (d2 (org-last day-numbers))
3576 (w1 (org-days-to-iso-week d1))
3577 (w2 (org-days-to-iso-week d2)))
3578 (setq s (point))
3579 (if org-agenda-overriding-header
3580 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3581 nil 'face 'org-agenda-structure) "\n")
3582 (insert (org-agenda-span-name span)
3583 "-agenda"
3584 (if (< (- d2 d1) 350)
3585 (if (= w1 w2)
3586 (format " (W%02d)" w1)
3587 (format " (W%02d-W%02d)" w1 w2))
3589 ":\n")))
3590 (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
3591 'org-date-line t))
3592 (org-agenda-mark-header-line s))
3593 (while (setq d (pop day-numbers))
3594 (setq date (calendar-gregorian-from-absolute d)
3595 s (point))
3596 (if (or (setq todayp (= d today))
3597 (and (not start-pos) (= d sd)))
3598 (setq start-pos (point))
3599 (if (and start-pos (not end-pos))
3600 (setq end-pos (point))))
3601 (setq files thefiles
3602 rtnall nil)
3603 (while (setq file (pop files))
3604 (catch 'nextfile
3605 (org-check-agenda-file file)
3606 (let ((org-agenda-entry-types org-agenda-entry-types))
3607 (unless org-agenda-include-deadlines
3608 (setq org-agenda-entry-types
3609 (delq :deadline org-agenda-entry-types)))
3610 (cond
3611 ((memq org-agenda-show-log '(only clockcheck))
3612 (setq rtn (org-agenda-get-day-entries
3613 file date :closed)))
3614 (org-agenda-show-log
3615 (setq rtn (apply 'org-agenda-get-day-entries
3616 file date
3617 (append '(:closed) org-agenda-entry-types))))
3619 (setq rtn (apply 'org-agenda-get-day-entries
3620 file date
3621 org-agenda-entry-types)))))
3622 (setq rtnall (append rtnall rtn))))
3623 (if org-agenda-include-diary
3624 (let ((org-agenda-search-headline-for-time t))
3625 (require 'diary-lib)
3626 (setq rtn (org-get-entries-from-diary date))
3627 (setq rtnall (append rtnall rtn))))
3628 (if (or rtnall org-agenda-show-all-dates)
3629 (progn
3630 (setq day-cnt (1+ day-cnt))
3631 (insert
3632 (if (stringp org-agenda-format-date)
3633 (format-time-string org-agenda-format-date
3634 (org-time-from-absolute date))
3635 (funcall org-agenda-format-date date))
3636 "\n")
3637 (put-text-property s (1- (point)) 'face
3638 (org-agenda-get-day-face date))
3639 (put-text-property s (1- (point)) 'org-date-line t)
3640 (put-text-property s (1- (point)) 'org-agenda-date-header t)
3641 (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
3642 (when todayp
3643 (put-text-property s (1- (point)) 'org-today t))
3644 (if rtnall (insert
3645 (org-finalize-agenda-entries
3646 (org-agenda-add-time-grid-maybe
3647 rtnall ndays todayp))
3648 "\n"))
3649 (put-text-property s (1- (point)) 'day d)
3650 (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
3651 (when (and org-agenda-clockreport-mode clocktable-start)
3652 (let ((org-agenda-files (org-agenda-files nil 'ifmode))
3653 ;; the above line is to ensure the restricted range!
3654 (p (copy-sequence org-agenda-clockreport-parameter-plist))
3655 tbl)
3656 (setq p (org-plist-delete p :block))
3657 (setq p (plist-put p :tstart clocktable-start))
3658 (setq p (plist-put p :tend clocktable-end))
3659 (setq p (plist-put p :scope 'agenda))
3660 (when (and (eq org-agenda-clockreport-mode 'with-filter)
3661 (setq filter (or org-agenda-filter-while-redo
3662 (get 'org-agenda-filter :preset-filter))))
3663 (setq p (plist-put p :tags (mapconcat (lambda (x)
3664 (if (string-match "[<>=]" x)
3667 filter ""))))
3668 (setq tbl (apply 'org-get-clocktable p))
3669 (insert tbl)))
3670 (goto-char (point-min))
3671 (or org-agenda-multi (org-fit-agenda-window))
3672 (unless (and (pos-visible-in-window-p (point-min))
3673 (pos-visible-in-window-p (point-max)))
3674 (goto-char (1- (point-max)))
3675 (recenter -1)
3676 (if (not (pos-visible-in-window-p (or start-pos 1)))
3677 (progn
3678 (goto-char (or start-pos 1))
3679 (recenter 1))))
3680 (goto-char (or start-pos 1))
3681 (add-text-properties (point-min) (point-max) '(org-agenda-type agenda))
3682 (if (eq org-agenda-show-log 'clockcheck)
3683 (org-agenda-show-clocking-issues))
3684 (org-finalize-agenda)
3685 (setq buffer-read-only t)
3686 (message "")))
3688 (defun org-agenda-ndays-to-span (n)
3689 "Return a span symbol for a span of N days, or N if none matches."
3690 (cond ((symbolp n) n)
3691 ((= n 1) 'day)
3692 ((= n 7) 'week)
3693 (t n)))
3695 (defun org-agenda-span-to-ndays (span start-day)
3696 "Return ndays from SPAN starting at START-DAY."
3697 (cond ((numberp span) span)
3698 ((eq span 'day) 1)
3699 ((eq span 'week) 7)
3700 ((eq span 'month)
3701 (let ((date (calendar-gregorian-from-absolute start-day)))
3702 (calendar-last-day-of-month (car date) (caddr date))))
3703 ((eq span 'year)
3704 (let ((date (calendar-gregorian-from-absolute start-day)))
3705 (if (calendar-leap-year-p (caddr date)) 366 365)))))
3707 (defun org-agenda-span-name (span)
3708 "Return a SPAN name."
3709 (if (null span)
3711 (if (symbolp span)
3712 (capitalize (symbol-name span))
3713 (format "%d days" span))))
3715 ;;; Agenda word search
3717 (defvar org-agenda-search-history nil)
3718 (defvar org-todo-only nil)
3720 (defvar org-search-syntax-table nil
3721 "Special syntax table for org-mode search.
3722 In this table, we have single quotes not as word constituents, to
3723 that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
3725 (defun org-search-syntax-table ()
3726 (unless org-search-syntax-table
3727 (setq org-search-syntax-table (copy-syntax-table org-mode-syntax-table))
3728 (modify-syntax-entry ?' "." org-search-syntax-table)
3729 (modify-syntax-entry ?` "." org-search-syntax-table))
3730 org-search-syntax-table)
3732 (defvar org-agenda-last-search-view-search-was-boolean nil)
3734 ;;;###autoload
3735 (defun org-search-view (&optional todo-only string edit-at)
3736 "Show all entries that contain a phrase or words or regular expressions.
3738 With optional prefix argument TODO-ONLY, only consider entries that are
3739 TODO entries. The argument STRING can be used to pass a default search
3740 string into this function. If EDIT-AT is non-nil, it means that the
3741 user should get a chance to edit this string, with cursor at position
3742 EDIT-AT.
3744 The search string can be viewed either as a phrase that should be found as
3745 is, or it can be broken into a number of snippets, each of which must match
3746 in a Boolean way to select an entry. The default depends on the variable
3747 `org-agenda-search-view-always-boolean'.
3748 Even if this is turned off (the default) you can always switch to
3749 Boolean search dynamically by preceding the first word with \"+\" or \"-\".
3751 The default is a direct search of the whole phrase, where each space in
3752 the search string can expand to an arbitrary amount of whitespace,
3753 including newlines.
3755 If using a Boolean search, the search string is split on whitespace and
3756 each snippet is searched separately, with logical AND to select an entry.
3757 Words prefixed with a minus must *not* occur in the entry. Words without
3758 a prefix or prefixed with a plus must occur in the entry. Matching is
3759 case-insensitive. Words are enclosed by word delimiters (i.e. they must
3760 match whole words, not parts of a word) if
3761 `org-agenda-search-view-force-full-words' is set (default is nil).
3763 Boolean search snippets enclosed by curly braces are interpreted as
3764 regular expressions that must or (when preceded with \"-\") must not
3765 match in the entry. Snippets enclosed into double quotes will be taken
3766 as a whole, to include whitespace.
3768 - If the search string starts with an asterisk, search only in headlines.
3769 - If (possibly after the leading star) the search string starts with an
3770 exclamation mark, this also means to look at TODO entries only, an effect
3771 that can also be achieved with a prefix argument.
3772 - If (possibly after star and exclamation mark) the search string starts
3773 with a colon, this will mean that the (non-regexp) snippets of the
3774 Boolean search must match as full words.
3776 This command searches the agenda files, and in addition the files listed
3777 in `org-agenda-text-search-extra-files'."
3778 (interactive "P")
3779 (org-compile-prefix-format 'search)
3780 (org-set-sorting-strategy 'search)
3781 (org-prepare-agenda "SEARCH")
3782 (let* ((props (list 'face nil
3783 'done-face 'org-agenda-done
3784 'org-not-done-regexp org-not-done-regexp
3785 'org-todo-regexp org-todo-regexp
3786 'org-complex-heading-regexp org-complex-heading-regexp
3787 'mouse-face 'highlight
3788 'help-echo (format "mouse-2 or RET jump to location")))
3789 (full-words org-agenda-search-view-force-full-words)
3790 (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
3791 regexp rtn rtnall files file pos
3792 marker category tags c neg re boolean
3793 ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
3794 (unless (and (not edit-at)
3795 (stringp string)
3796 (string-match "\\S-" string))
3797 (setq string (read-string
3798 (if org-agenda-search-view-always-boolean
3799 "[+-]Word/{Regexp} ...: "
3800 "Phrase, or [+-]Word/{Regexp} ...: ")
3801 (cond
3802 ((integerp edit-at) (cons string edit-at))
3803 (edit-at string))
3804 'org-agenda-search-history)))
3805 (org-set-local 'org-todo-only todo-only)
3806 (setq org-agenda-redo-command
3807 (list 'org-search-view (if todo-only t nil) string
3808 '(if current-prefix-arg 1 nil)))
3809 (setq org-agenda-query-string string)
3811 (if (equal (string-to-char string) ?*)
3812 (setq hdl-only t
3813 words (substring string 1))
3814 (setq words string))
3815 (when (equal (string-to-char words) ?!)
3816 (setq todo-only t
3817 words (substring words 1)))
3818 (when (equal (string-to-char words) ?:)
3819 (setq full-words t
3820 words (substring words 1)))
3821 (if (or org-agenda-search-view-always-boolean
3822 (member (string-to-char words) '(?- ?+ ?\{)))
3823 (setq boolean t))
3824 (setq words (org-split-string words))
3825 (let (www w)
3826 (while (setq w (pop words))
3827 (while (and (string-match "\\\\\\'" w) words)
3828 (setq w (concat (substring w 0 -1) " " (pop words))))
3829 (push w www))
3830 (setq words (nreverse www) www nil)
3831 (while (setq w (pop words))
3832 (when (and (string-match "\\`[-+]?{" w)
3833 (not (string-match "}\\'" w)))
3834 (while (and words (not (string-match "}\\'" (car words))))
3835 (setq w (concat w " " (pop words))))
3836 (setq w (concat w " " (pop words))))
3837 (push w www))
3838 (setq words (nreverse www)))
3839 (setq org-agenda-last-search-view-search-was-boolean boolean)
3840 (when boolean
3841 (let (wds w)
3842 (while (setq w (pop words))
3843 (if (or (equal (substring w 0 1) "\"")
3844 (and (> (length w) 1)
3845 (member (substring w 0 1) '("+" "-"))
3846 (equal (substring w 1 2) "\"")))
3847 (while (and words (not (equal (substring w -1) "\"")))
3848 (setq w (concat w " " (pop words)))))
3849 (and (string-match "\\`\\([-+]?\\)\"" w)
3850 (setq w (replace-match "\\1" nil nil w)))
3851 (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
3852 (push w wds))
3853 (setq words (nreverse wds))))
3854 (if boolean
3855 (mapc (lambda (w)
3856 (setq c (string-to-char w))
3857 (if (equal c ?-)
3858 (setq neg t w (substring w 1))
3859 (if (equal c ?+)
3860 (setq neg nil w (substring w 1))
3861 (setq neg nil)))
3862 (if (string-match "\\`{.*}\\'" w)
3863 (setq re (substring w 1 -1))
3864 (if full-words
3865 (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
3866 (setq re (regexp-quote (downcase w)))))
3867 (if neg (push re regexps-) (push re regexps+)))
3868 words)
3869 (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
3870 regexps+))
3871 (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
3872 (if (not regexps+)
3873 (setq regexp org-outline-regexp-bol)
3874 (setq regexp (pop regexps+))
3875 (if hdl-only (setq regexp (concat "^" org-outline-regexp ".*?"
3876 regexp))))
3877 (setq files (org-agenda-files nil 'ifmode))
3878 (when (eq (car org-agenda-text-search-extra-files) 'agenda-archives)
3879 (pop org-agenda-text-search-extra-files)
3880 (setq files (org-add-archive-files files)))
3881 (setq files (append files org-agenda-text-search-extra-files)
3882 rtnall nil)
3883 (while (setq file (pop files))
3884 (setq ee nil)
3885 (catch 'nextfile
3886 (org-check-agenda-file file)
3887 (setq buffer (if (file-exists-p file)
3888 (org-get-agenda-file-buffer file)
3889 (error "No such file %s" file)))
3890 (if (not buffer)
3891 ;; If file does not exist, make sure an error message is sent
3892 (setq rtn (list (format "ORG-AGENDA-ERROR: No such org-file %s"
3893 file))))
3894 (with-current-buffer buffer
3895 (with-syntax-table (org-search-syntax-table)
3896 (unless (org-mode-p)
3897 (error "Agenda file %s is not in `org-mode'" file))
3898 (let ((case-fold-search t))
3899 (save-excursion
3900 (save-restriction
3901 (if org-agenda-restrict
3902 (narrow-to-region org-agenda-restrict-begin
3903 org-agenda-restrict-end)
3904 (widen))
3905 (goto-char (point-min))
3906 (unless (or (org-on-heading-p)
3907 (outline-next-heading))
3908 (throw 'nextfile t))
3909 (goto-char (max (point-min) (1- (point))))
3910 (while (re-search-forward regexp nil t)
3911 (org-back-to-heading t)
3912 (skip-chars-forward "* ")
3913 (setq beg (point-at-bol)
3914 beg1 (point)
3915 end (progn (outline-next-heading) (point)))
3916 (catch :skip
3917 (goto-char beg)
3918 (org-agenda-skip)
3919 (setq str (buffer-substring-no-properties
3920 (point-at-bol)
3921 (if hdl-only (point-at-eol) end)))
3922 (mapc (lambda (wr) (when (string-match wr str)
3923 (goto-char (1- end))
3924 (throw :skip t)))
3925 regexps-)
3926 (mapc (lambda (wr) (unless (string-match wr str)
3927 (goto-char (1- end))
3928 (throw :skip t)))
3929 (if todo-only
3930 (cons (concat "^\*+[ \t]+" org-not-done-regexp)
3931 regexps+)
3932 regexps+))
3933 (goto-char beg)
3934 (setq marker (org-agenda-new-marker (point))
3935 category (org-get-category)
3936 tags (org-get-tags-at (point))
3937 txt (org-format-agenda-item
3939 (buffer-substring-no-properties
3940 beg1 (point-at-eol))
3941 category tags))
3942 (org-add-props txt props
3943 'org-marker marker 'org-hd-marker marker
3944 'org-todo-regexp org-todo-regexp
3945 'org-complex-heading-regexp org-complex-heading-regexp
3946 'priority 1000 'org-category category
3947 'type "search")
3948 (push txt ee)
3949 (goto-char (1- end))))))))))
3950 (setq rtn (nreverse ee))
3951 (setq rtnall (append rtnall rtn)))
3952 (if org-agenda-overriding-header
3953 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
3954 nil 'face 'org-agenda-structure) "\n")
3955 (insert "Search words: ")
3956 (add-text-properties (point-min) (1- (point))
3957 (list 'face 'org-agenda-structure))
3958 (setq pos (point))
3959 (insert string "\n")
3960 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
3961 (setq pos (point))
3962 (unless org-agenda-multi
3963 (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
3964 (add-text-properties pos (1- (point))
3965 (list 'face 'org-agenda-structure))))
3966 (org-agenda-mark-header-line (point-min))
3967 (when rtnall
3968 (insert (org-finalize-agenda-entries rtnall) "\n"))
3969 (goto-char (point-min))
3970 (or org-agenda-multi (org-fit-agenda-window))
3971 (add-text-properties (point-min) (point-max) '(org-agenda-type search))
3972 (org-finalize-agenda)
3973 (setq buffer-read-only t)))
3975 ;;; Agenda TODO list
3977 (defvar org-select-this-todo-keyword nil)
3978 (defvar org-last-arg nil)
3980 ;;;###autoload
3981 (defun org-todo-list (arg)
3982 "Show all (not done) TODO entries from all agenda file in a single list.
3983 The prefix arg can be used to select a specific TODO keyword and limit
3984 the list to these. When using \\[universal-argument], you will be prompted
3985 for a keyword. A numeric prefix directly selects the Nth keyword in
3986 `org-todo-keywords-1'."
3987 (interactive "P")
3988 (org-compile-prefix-format 'todo)
3989 (org-set-sorting-strategy 'todo)
3990 (org-prepare-agenda "TODO")
3991 (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
3992 (let* ((today (org-today))
3993 (date (calendar-gregorian-from-absolute today))
3994 (kwds org-todo-keywords-for-agenda)
3995 (completion-ignore-case t)
3996 (org-select-this-todo-keyword
3997 (if (stringp arg) arg
3998 (and arg (integerp arg) (> arg 0)
3999 (nth (1- arg) kwds))))
4000 rtn rtnall files file pos)
4001 (when (equal arg '(4))
4002 (setq org-select-this-todo-keyword
4003 (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
4004 (mapcar 'list kwds) nil nil)))
4005 (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
4006 (org-set-local 'org-last-arg arg)
4007 (setq org-agenda-redo-command
4008 '(org-todo-list (or current-prefix-arg org-last-arg)))
4009 (setq files (org-agenda-files nil 'ifmode)
4010 rtnall nil)
4011 (while (setq file (pop files))
4012 (catch 'nextfile
4013 (org-check-agenda-file file)
4014 (setq rtn (org-agenda-get-day-entries file date :todo))
4015 (setq rtnall (append rtnall rtn))))
4016 (if org-agenda-overriding-header
4017 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4018 nil 'face 'org-agenda-structure) "\n")
4019 (insert "Global list of TODO items of type: ")
4020 (add-text-properties (point-min) (1- (point))
4021 (list 'face 'org-agenda-structure
4022 'short-heading
4023 (concat "ToDo: "
4024 (or org-select-this-todo-keyword "ALL"))))
4025 (org-agenda-mark-header-line (point-min))
4026 (setq pos (point))
4027 (insert (or org-select-this-todo-keyword "ALL") "\n")
4028 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4029 (setq pos (point))
4030 (unless org-agenda-multi
4031 (insert "Available with `N r': (0)ALL")
4032 (let ((n 0) s)
4033 (mapc (lambda (x)
4034 (setq s (format "(%d)%s" (setq n (1+ n)) x))
4035 (if (> (+ (current-column) (string-width s) 1) (frame-width))
4036 (insert "\n "))
4037 (insert " " s))
4038 kwds))
4039 (insert "\n"))
4040 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4041 (org-agenda-mark-header-line (point-min))
4042 (when rtnall
4043 (insert (org-finalize-agenda-entries rtnall) "\n"))
4044 (goto-char (point-min))
4045 (or org-agenda-multi (org-fit-agenda-window))
4046 (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
4047 (org-finalize-agenda)
4048 (setq buffer-read-only t)))
4050 ;;; Agenda tags match
4052 ;;;###autoload
4053 (defun org-tags-view (&optional todo-only match)
4054 "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
4055 The prefix arg TODO-ONLY limits the search to TODO entries."
4056 (interactive "P")
4057 (org-compile-prefix-format 'tags)
4058 (org-set-sorting-strategy 'tags)
4059 (let* ((org-tags-match-list-sublevels
4060 org-tags-match-list-sublevels)
4061 (completion-ignore-case t)
4062 rtn rtnall files file pos matcher
4063 buffer)
4064 (when (and (stringp match) (not (string-match "\\S-" match)))
4065 (setq match nil))
4066 (setq matcher (org-make-tags-matcher match)
4067 match (car matcher) matcher (cdr matcher))
4068 (org-prepare-agenda (concat "TAGS " match))
4069 (setq org-agenda-query-string match)
4070 (setq org-agenda-redo-command
4071 (list 'org-tags-view (list 'quote todo-only)
4072 (list 'if 'current-prefix-arg nil 'org-agenda-query-string)))
4073 (setq files (org-agenda-files nil 'ifmode)
4074 rtnall nil)
4075 (while (setq file (pop files))
4076 (catch 'nextfile
4077 (org-check-agenda-file file)
4078 (setq buffer (if (file-exists-p file)
4079 (org-get-agenda-file-buffer file)
4080 (error "No such file %s" file)))
4081 (if (not buffer)
4082 ;; If file does not exist, error message to agenda
4083 (setq rtn (list
4084 (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4085 rtnall (append rtnall rtn))
4086 (with-current-buffer buffer
4087 (unless (org-mode-p)
4088 (error "Agenda file %s is not in `org-mode'" file))
4089 (save-excursion
4090 (save-restriction
4091 (if org-agenda-restrict
4092 (narrow-to-region org-agenda-restrict-begin
4093 org-agenda-restrict-end)
4094 (widen))
4095 (setq rtn (org-scan-tags 'agenda matcher todo-only))
4096 (setq rtnall (append rtnall rtn))))))))
4097 (if org-agenda-overriding-header
4098 (insert (org-add-props (copy-sequence org-agenda-overriding-header)
4099 nil 'face 'org-agenda-structure) "\n")
4100 (insert "Headlines with TAGS match: ")
4101 (add-text-properties (point-min) (1- (point))
4102 (list 'face 'org-agenda-structure
4103 'short-heading
4104 (concat "Match: " match)))
4105 (setq pos (point))
4106 (insert match "\n")
4107 (add-text-properties pos (1- (point)) (list 'face 'org-warning))
4108 (setq pos (point))
4109 (unless org-agenda-multi
4110 (insert "Press `C-u r' to search again with new search string\n"))
4111 (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
4112 (org-agenda-mark-header-line (point-min))
4113 (when rtnall
4114 (insert (org-finalize-agenda-entries rtnall) "\n"))
4115 (goto-char (point-min))
4116 (or org-agenda-multi (org-fit-agenda-window))
4117 (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
4118 (org-finalize-agenda)
4119 (setq buffer-read-only t)))
4121 ;;; Agenda Finding stuck projects
4123 (defvar org-agenda-skip-regexp nil
4124 "Regular expression used in skipping subtrees for the agenda.
4125 This is basically a temporary global variable that can be set and then
4126 used by user-defined selections using `org-agenda-skip-function'.")
4128 (defvar org-agenda-overriding-header nil
4129 "When set during agenda, todo and tags searches it replaces the header.
4130 This variable should not be set directly, but custom commands can bind it
4131 in the options section.")
4133 (defun org-agenda-skip-entry-when-regexp-matches ()
4134 "Check if the current entry contains match for `org-agenda-skip-regexp'.
4135 If yes, it returns the end position of this entry, causing agenda commands
4136 to skip the entry but continuing the search in the subtree. This is a
4137 function that can be put into `org-agenda-skip-function' for the duration
4138 of a command."
4139 (let ((end (save-excursion (org-end-of-subtree t)))
4140 skip)
4141 (save-excursion
4142 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4143 (and skip end)))
4145 (defun org-agenda-skip-subtree-when-regexp-matches ()
4146 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4147 If yes, it returns the end position of this tree, causing agenda commands
4148 to skip this subtree. This is a function that can be put into
4149 `org-agenda-skip-function' for the duration of a command."
4150 (let ((end (save-excursion (org-end-of-subtree t)))
4151 skip)
4152 (save-excursion
4153 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4154 (and skip end)))
4156 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
4157 "Check if the current subtree contains match for `org-agenda-skip-regexp'.
4158 If yes, it returns the end position of the current entry (NOT the tree),
4159 causing agenda commands to skip the entry but continuing the search in
4160 the subtree. This is a function that can be put into
4161 `org-agenda-skip-function' for the duration of a command. An important
4162 use of this function is for the stuck project list."
4163 (let ((end (save-excursion (org-end-of-subtree t)))
4164 (entry-end (save-excursion (outline-next-heading) (1- (point))))
4165 skip)
4166 (save-excursion
4167 (setq skip (re-search-forward org-agenda-skip-regexp end t)))
4168 (and skip entry-end)))
4170 (defun org-agenda-skip-entry-if (&rest conditions)
4171 "Skip entry if any of CONDITIONS is true.
4172 See `org-agenda-skip-if' for details."
4173 (org-agenda-skip-if nil conditions))
4175 (defun org-agenda-skip-subtree-if (&rest conditions)
4176 "Skip entry if any of CONDITIONS is true.
4177 See `org-agenda-skip-if' for details."
4178 (org-agenda-skip-if t conditions))
4180 (defun org-agenda-skip-if (subtree conditions)
4181 "Checks current entity for CONDITIONS.
4182 If SUBTREE is non-nil, the entire subtree is checked. Otherwise, only
4183 the entry, i.e. the text before the next heading is checked.
4185 CONDITIONS is a list of symbols, boolean OR is used to combine the results
4186 from different tests. Valid conditions are:
4188 scheduled Check if there is a scheduled cookie
4189 notscheduled Check if there is no scheduled cookie
4190 deadline Check if there is a deadline
4191 notdeadline Check if there is no deadline
4192 timestamp Check if there is a timestamp (also deadline or scheduled)
4193 nottimestamp Check if there is no timestamp (also deadline or scheduled)
4194 regexp Check if regexp matches
4195 notregexp Check if regexp does not match.
4196 todo Check if TODO keyword matches
4197 nottodo Check if TODO keyword does not match
4199 The regexp is taken from the conditions list, it must come right after
4200 the `regexp' or `notregexp' element.
4202 `todo' and `nottodo' accept as an argument a list of todo
4203 keywords, which may include \"*\" to match any todo keyword.
4205 (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
4207 would skip all entries with \"TODO\" or \"WAITING\" keywords.
4209 Instead of a list a keyword class may be given
4211 (org-agenda-skip-entry-if 'nottodo 'done)
4213 would skip entries that haven't been marked with any of \"DONE\"
4214 keywords. Possible classes are: `todo', `done', `any'.
4216 If any of these conditions is met, this function returns the end point of
4217 the entity, causing the search to continue from there. This is a function
4218 that can be put into `org-agenda-skip-function' for the duration of a command."
4219 (let (beg end m)
4220 (org-back-to-heading t)
4221 (setq beg (point)
4222 end (if subtree
4223 (progn (org-end-of-subtree t) (point))
4224 (progn (outline-next-heading) (1- (point)))))
4225 (goto-char beg)
4226 (and
4228 (and (memq 'scheduled conditions)
4229 (re-search-forward org-scheduled-time-regexp end t))
4230 (and (memq 'notscheduled conditions)
4231 (not (re-search-forward org-scheduled-time-regexp end t)))
4232 (and (memq 'deadline conditions)
4233 (re-search-forward org-deadline-time-regexp end t))
4234 (and (memq 'notdeadline conditions)
4235 (not (re-search-forward org-deadline-time-regexp end t)))
4236 (and (memq 'timestamp conditions)
4237 (re-search-forward org-ts-regexp end t))
4238 (and (memq 'nottimestamp conditions)
4239 (not (re-search-forward org-ts-regexp end t)))
4240 (and (setq m (memq 'regexp conditions))
4241 (stringp (nth 1 m))
4242 (re-search-forward (nth 1 m) end t))
4243 (and (setq m (memq 'notregexp conditions))
4244 (stringp (nth 1 m))
4245 (not (re-search-forward (nth 1 m) end t)))
4246 (and (or
4247 (setq m (memq 'todo conditions))
4248 (setq m (memq 'nottodo conditions)))
4249 (org-agenda-skip-if-todo m end)))
4250 end)))
4252 (defun org-agenda-skip-if-todo (args end)
4253 "Helper function for `org-agenda-skip-if', do not use it directly.
4254 ARGS is a list with first element either `todo' or `nottodo'.
4255 The remainder is either a list of TODO keywords, or a state symbol
4256 `todo' or `done' or `any'."
4257 (let ((kw (car args))
4258 (arg (cadr args))
4259 todo-wds todo-re)
4260 (setq todo-wds
4261 (org-uniquify
4262 (cond
4263 ((listp arg) ;; list of keywords
4264 (if (member "*" arg)
4265 (mapcar 'substring-no-properties org-todo-keywords-1)
4266 arg))
4267 ((symbolp arg) ;; keyword class name
4268 (cond
4269 ((eq arg 'todo)
4270 (org-delete-all org-done-keywords
4271 (mapcar 'substring-no-properties
4272 org-todo-keywords-1)))
4273 ((eq arg 'done) org-done-keywords)
4274 ((eq arg 'any)
4275 (mapcar 'substring-no-properties org-todo-keywords-1)))))))
4276 (setq todo-re
4277 (concat "^\\*+[ \t]+\\<\\("
4278 (mapconcat 'identity todo-wds "\\|")
4279 "\\)\\>"))
4280 (if (eq kw 'todo)
4281 (re-search-forward todo-re end t)
4282 (not (re-search-forward todo-re end t)))))
4284 ;;;###autoload
4285 (defun org-agenda-list-stuck-projects (&rest ignore)
4286 "Create agenda view for projects that are stuck.
4287 Stuck projects are project that have no next actions. For the definitions
4288 of what a project is and how to check if it stuck, customize the variable
4289 `org-stuck-projects'."
4290 (interactive)
4291 (let* ((org-agenda-skip-function
4292 'org-agenda-skip-entry-when-regexp-matches-in-subtree)
4293 ;; We could have used org-agenda-skip-if here.
4294 (org-agenda-overriding-header
4295 (or org-agenda-overriding-header "List of stuck projects: "))
4296 (matcher (nth 0 org-stuck-projects))
4297 (todo (nth 1 org-stuck-projects))
4298 (todo-wds (if (member "*" todo)
4299 (progn
4300 (org-prepare-agenda-buffers (org-agenda-files
4301 nil 'ifmode))
4302 (org-delete-all
4303 org-done-keywords-for-agenda
4304 (copy-sequence org-todo-keywords-for-agenda)))
4305 todo))
4306 (todo-re (concat "^\\*+[ \t]+\\("
4307 (mapconcat 'identity todo-wds "\\|")
4308 "\\)\\>"))
4309 (tags (nth 2 org-stuck-projects))
4310 (tags-re (if (member "*" tags)
4311 (org-re (concat org-outline-regexp-bol
4312 ".*:[[:alnum:]_@#%]+:[ \t]*$"))
4313 (if tags
4314 (concat org-outline-regexp-bol
4315 ".*:\\("
4316 (mapconcat 'identity tags "\\|")
4317 (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
4318 (gen-re (nth 3 org-stuck-projects))
4319 (re-list
4320 (delq nil
4321 (list
4322 (if todo todo-re)
4323 (if tags tags-re)
4324 (and gen-re (stringp gen-re) (string-match "\\S-" gen-re)
4325 gen-re)))))
4326 (setq org-agenda-skip-regexp
4327 (if re-list
4328 (mapconcat 'identity re-list "\\|")
4329 (error "No information how to identify unstuck projects")))
4330 (org-tags-view nil matcher)
4331 (with-current-buffer org-agenda-buffer-name
4332 (setq org-agenda-redo-command
4333 '(org-agenda-list-stuck-projects
4334 (or current-prefix-arg org-last-arg))))))
4336 ;;; Diary integration
4338 (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
4339 (defvar list-diary-entries-hook)
4340 (defvar diary-time-regexp)
4341 (defun org-get-entries-from-diary (date)
4342 "Get the (Emacs Calendar) diary entries for DATE."
4343 (require 'diary-lib)
4344 (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
4345 (diary-display-hook '(fancy-diary-display))
4346 (diary-display-function 'fancy-diary-display)
4347 (pop-up-frames nil)
4348 (list-diary-entries-hook
4349 (cons 'org-diary-default-entry list-diary-entries-hook))
4350 (diary-file-name-prefix-function nil) ; turn this feature off
4351 (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
4352 entries
4353 (org-disable-agenda-to-diary t))
4354 (save-excursion
4355 (save-window-excursion
4356 (funcall (if (fboundp 'diary-list-entries)
4357 'diary-list-entries 'list-diary-entries)
4358 date 1)))
4359 (if (not (get-buffer diary-fancy-buffer))
4360 (setq entries nil)
4361 (with-current-buffer diary-fancy-buffer
4362 (setq buffer-read-only nil)
4363 (if (zerop (buffer-size))
4364 ;; No entries
4365 (setq entries nil)
4366 ;; Omit the date and other unnecessary stuff
4367 (org-agenda-cleanup-fancy-diary)
4368 ;; Add prefix to each line and extend the text properties
4369 (if (zerop (buffer-size))
4370 (setq entries nil)
4371 (setq entries (buffer-substring (point-min) (- (point-max) 1)))
4372 (setq entries
4373 (with-temp-buffer
4374 (insert entries) (goto-char (point-min))
4375 (while (re-search-forward "\n[ \t]+\\(.+\\)$" nil t)
4376 (unless (save-match-data (string-match diary-time-regexp (match-string 1)))
4377 (replace-match (concat "; " (match-string 1)))))
4378 (buffer-string)))))
4379 (set-buffer-modified-p nil)
4380 (kill-buffer diary-fancy-buffer)))
4381 (when entries
4382 (setq entries (org-split-string entries "\n"))
4383 (setq entries
4384 (mapcar
4385 (lambda (x)
4386 (setq x (org-format-agenda-item "" x "Diary" nil 'time))
4387 ;; Extend the text properties to the beginning of the line
4388 (org-add-props x (text-properties-at (1- (length x)) x)
4389 'type "diary" 'date date 'face 'org-agenda-diary))
4390 entries)))))
4392 (defvar org-agenda-cleanup-fancy-diary-hook nil
4393 "Hook run when the fancy diary buffer is cleaned up.")
4395 (defun org-agenda-cleanup-fancy-diary ()
4396 "Remove unwanted stuff in buffer created by `fancy-diary-display'.
4397 This gets rid of the date, the underline under the date, and
4398 the dummy entry installed by `org-mode' to ensure non-empty diary for each
4399 date. It also removes lines that contain only whitespace."
4400 (goto-char (point-min))
4401 (if (looking-at ".*?:[ \t]*")
4402 (progn
4403 (replace-match "")
4404 (re-search-forward "\n=+$" nil t)
4405 (replace-match "")
4406 (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
4407 (re-search-forward "\n=+$" nil t)
4408 (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
4409 (goto-char (point-min))
4410 (while (re-search-forward "^ +\n" nil t)
4411 (replace-match ""))
4412 (goto-char (point-min))
4413 (if (re-search-forward "^Org-mode dummy\n?" nil t)
4414 (replace-match ""))
4415 (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
4417 ;; Make sure entries from the diary have the right text properties.
4418 (eval-after-load "diary-lib"
4419 '(if (boundp 'diary-modify-entry-list-string-function)
4420 ;; We can rely on the hook, nothing to do
4422 ;; Hook not available, must use advice to make this work
4423 (defadvice add-to-diary-list (before org-mark-diary-entry activate)
4424 "Make the position visible."
4425 (if (and org-disable-agenda-to-diary ;; called from org-agenda
4426 (stringp string)
4427 buffer-file-name)
4428 (setq string (org-modify-diary-entry-string string))))))
4430 (defun org-modify-diary-entry-string (string)
4431 "Add text properties to string, allowing org-mode to act on it."
4432 (org-add-props string nil
4433 'mouse-face 'highlight
4434 'help-echo (if buffer-file-name
4435 (format "mouse-2 or RET jump to diary file %s"
4436 (abbreviate-file-name buffer-file-name))
4438 'org-agenda-diary-link t
4439 'org-marker (org-agenda-new-marker (point-at-bol))))
4441 (defun org-diary-default-entry ()
4442 "Add a dummy entry to the diary.
4443 Needed to avoid empty dates which mess up holiday display."
4444 ;; Catch the error if dealing with the new add-to-diary-alist
4445 (when org-disable-agenda-to-diary
4446 (condition-case nil
4447 (org-add-to-diary-list original-date "Org-mode dummy" "")
4448 (error
4449 (org-add-to-diary-list original-date "Org-mode dummy" "" nil)))))
4451 (defun org-add-to-diary-list (&rest args)
4452 (if (fboundp 'diary-add-to-list)
4453 (apply 'diary-add-to-list args)
4454 (apply 'add-to-diary-list args)))
4456 (defvar org-diary-last-run-time nil)
4458 ;;;###autoload
4459 (defun org-diary (&rest args)
4460 "Return diary information from org-files.
4461 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
4462 It accesses org files and extracts information from those files to be
4463 listed in the diary. The function accepts arguments specifying what
4464 items should be listed. For a list of arguments allowed here, see the
4465 variable `org-agenda-entry-types'.
4467 The call in the diary file should look like this:
4469 &%%(org-diary) ~/path/to/some/orgfile.org
4471 Use a separate line for each org file to check. Or, if you omit the file name,
4472 all files listed in `org-agenda-files' will be checked automatically:
4474 &%%(org-diary)
4476 If you don't give any arguments (as in the example above), the default
4477 arguments (:deadline :scheduled :timestamp :sexp) are used.
4478 So the example above may also be written as
4480 &%%(org-diary :deadline :timestamp :sexp :scheduled)
4482 The function expects the lisp variables `entry' and `date' to be provided
4483 by the caller, because this is how the calendar works. Don't use this
4484 function from a program - use `org-agenda-get-day-entries' instead."
4485 (when (> (- (org-float-time)
4486 org-agenda-last-marker-time)
4488 (org-agenda-reset-markers))
4489 (org-compile-prefix-format 'agenda)
4490 (org-set-sorting-strategy 'agenda)
4491 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4492 (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
4493 (list entry)
4494 (org-agenda-files t)))
4495 (time (org-float-time))
4496 file rtn results)
4497 (when (or (not org-diary-last-run-time)
4498 (> (- time
4499 org-diary-last-run-time)
4501 (org-prepare-agenda-buffers files))
4502 (setq org-diary-last-run-time time)
4503 ;; If this is called during org-agenda, don't return any entries to
4504 ;; the calendar. Org Agenda will list these entries itself.
4505 (if org-disable-agenda-to-diary (setq files nil))
4506 (while (setq file (pop files))
4507 (setq rtn (apply 'org-agenda-get-day-entries file date args))
4508 (setq results (append results rtn)))
4509 (if results
4510 (concat (org-finalize-agenda-entries results) "\n"))))
4512 ;;; Agenda entry finders
4514 (defun org-agenda-get-day-entries (file date &rest args)
4515 "Does the work for `org-diary' and `org-agenda'.
4516 FILE is the path to a file to be checked for entries. DATE is date like
4517 the one returned by `calendar-current-date'. ARGS are symbols indicating
4518 which kind of entries should be extracted. For details about these, see
4519 the documentation of `org-diary'."
4520 (setq args (or args '(:deadline :scheduled :timestamp :sexp)))
4521 (let* ((org-startup-folded nil)
4522 (org-startup-align-all-tables nil)
4523 (buffer (if (file-exists-p file)
4524 (org-get-agenda-file-buffer file)
4525 (error "No such file %s" file)))
4526 arg results rtn deadline-results)
4527 (if (not buffer)
4528 ;; If file does not exist, make sure an error message ends up in diary
4529 (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
4530 (with-current-buffer buffer
4531 (unless (org-mode-p)
4532 (error "Agenda file %s is not in `org-mode'" file))
4533 (let ((case-fold-search nil))
4534 (save-excursion
4535 (save-restriction
4536 (if org-agenda-restrict
4537 (narrow-to-region org-agenda-restrict-begin
4538 org-agenda-restrict-end)
4539 (widen))
4540 ;; The way we repeatedly append to `results' makes it O(n^2) :-(
4541 (while (setq arg (pop args))
4542 (cond
4543 ((and (eq arg :todo)
4544 (equal date (calendar-gregorian-from-absolute
4545 (org-today))))
4546 (setq rtn (org-agenda-get-todos))
4547 (setq results (append results rtn)))
4548 ((eq arg :timestamp)
4549 (setq rtn (org-agenda-get-blocks))
4550 (setq results (append results rtn))
4551 (setq rtn (org-agenda-get-timestamps))
4552 (setq results (append results rtn)))
4553 ((eq arg :sexp)
4554 (setq rtn (org-agenda-get-sexps))
4555 (setq results (append results rtn)))
4556 ((eq arg :scheduled)
4557 (setq rtn (org-agenda-get-scheduled deadline-results))
4558 (setq results (append results rtn)))
4559 ((eq arg :closed)
4560 (setq rtn (org-agenda-get-progress))
4561 (setq results (append results rtn)))
4562 ((eq arg :deadline)
4563 (setq rtn (org-agenda-get-deadlines))
4564 (setq deadline-results (copy-sequence rtn))
4565 (setq results (append results rtn))))))))
4566 results))))
4568 (defun org-agenda-get-todos ()
4569 "Return the TODO information for agenda display."
4570 (let* ((props (list 'face nil
4571 'done-face 'org-agenda-done
4572 'org-not-done-regexp org-not-done-regexp
4573 'org-todo-regexp org-todo-regexp
4574 'org-complex-heading-regexp org-complex-heading-regexp
4575 'mouse-face 'highlight
4576 'help-echo
4577 (format "mouse-2 or RET jump to org file %s"
4578 (abbreviate-file-name buffer-file-name))))
4579 (regexp (concat "^\\*+[ \t]+\\("
4580 (if org-select-this-todo-keyword
4581 (if (equal org-select-this-todo-keyword "*")
4582 org-todo-regexp
4583 (concat "\\<\\("
4584 (mapconcat 'identity (org-split-string org-select-this-todo-keyword "|") "\\|")
4585 "\\)\\>"))
4586 org-not-done-regexp)
4587 "[^\n\r]*\\)"))
4588 marker priority category tags todo-state
4589 ee txt beg end)
4590 (goto-char (point-min))
4591 (while (re-search-forward regexp nil t)
4592 (catch :skip
4593 (save-match-data
4594 (beginning-of-line)
4595 (org-agenda-skip)
4596 (setq beg (point) end (save-excursion (outline-next-heading) (point)))
4597 (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
4598 (goto-char (1+ beg))
4599 (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
4600 (throw :skip nil)))
4601 (goto-char (match-beginning 1))
4602 (setq marker (org-agenda-new-marker (match-beginning 0))
4603 category (org-get-category)
4604 txt (match-string 1)
4605 tags (org-get-tags-at (point))
4606 txt (org-format-agenda-item "" txt category tags)
4607 priority (1+ (org-get-priority txt))
4608 todo-state (org-get-todo-state))
4609 (org-add-props txt props
4610 'org-marker marker 'org-hd-marker marker
4611 'priority priority 'org-category category
4612 'type "todo" 'todo-state todo-state)
4613 (push txt ee)
4614 (if org-agenda-todo-list-sublevels
4615 (goto-char (match-end 1))
4616 (org-end-of-subtree 'invisible))))
4617 (nreverse ee)))
4619 (defun org-agenda-todo-custom-ignore-p (time n)
4620 "Check whether timestamp is farther away then n number of days.
4621 This function is invoked if `org-agenda-todo-ignore-deadlines',
4622 `org-agenda-todo-ignore-scheduled' or
4623 `org-agenda-todo-ignore-timestamp' is set to an integer."
4624 (let ((days (org-days-to-time time)))
4625 (if (>= n 0)
4626 (>= days n)
4627 (<= days n))))
4629 ;;;###autoload
4630 (defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
4631 (&optional end)
4632 "Do we have a reason to ignore this TODO entry because it has a time stamp?"
4633 (when (or org-agenda-todo-ignore-with-date
4634 org-agenda-todo-ignore-scheduled
4635 org-agenda-todo-ignore-deadlines
4636 org-agenda-todo-ignore-timestamp)
4637 (setq end (or end (save-excursion (outline-next-heading) (point))))
4638 (save-excursion
4639 (or (and org-agenda-todo-ignore-with-date
4640 (re-search-forward org-ts-regexp end t))
4641 (and org-agenda-todo-ignore-scheduled
4642 (re-search-forward org-scheduled-time-regexp end t)
4643 (cond
4644 ((eq org-agenda-todo-ignore-scheduled 'future)
4645 (> (org-days-to-time (match-string 1)) 0))
4646 ((eq org-agenda-todo-ignore-scheduled 'past)
4647 (<= (org-days-to-time (match-string 1)) 0))
4648 ((numberp org-agenda-todo-ignore-scheduled)
4649 (org-agenda-todo-custom-ignore-p
4650 (match-string 1) org-agenda-todo-ignore-scheduled))
4651 (t)))
4652 (and org-agenda-todo-ignore-deadlines
4653 (re-search-forward org-deadline-time-regexp end t)
4654 (cond
4655 ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
4656 ((eq org-agenda-todo-ignore-deadlines 'far)
4657 (not (org-deadline-close (match-string 1))))
4658 ((eq org-agenda-todo-ignore-deadlines 'future)
4659 (> (org-days-to-time (match-string 1)) 0))
4660 ((eq org-agenda-todo-ignore-deadlines 'past)
4661 (<= (org-days-to-time (match-string 1)) 0))
4662 ((numberp org-agenda-todo-ignore-deadlines)
4663 (org-agenda-todo-custom-ignore-p
4664 (match-string 1) org-agenda-todo-ignore-deadlines))
4665 (t (org-deadline-close (match-string 1)))))
4666 (and org-agenda-todo-ignore-timestamp
4667 (let ((buffer (current-buffer))
4668 (regexp
4669 (concat
4670 org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
4671 (start (point)))
4672 ;; Copy current buffer into a temporary one
4673 (with-temp-buffer
4674 (insert-buffer-substring buffer start end)
4675 (goto-char (point-min))
4676 ;; Delete SCHEDULED and DEADLINE items
4677 (while (re-search-forward regexp end t)
4678 (delete-region (match-beginning 0) (match-end 0)))
4679 (goto-char (point-min))
4680 ;; No search for timestamp left
4681 (when (re-search-forward org-ts-regexp nil t)
4682 (cond
4683 ((eq org-agenda-todo-ignore-timestamp 'future)
4684 (> (org-days-to-time (match-string 1)) 0))
4685 ((eq org-agenda-todo-ignore-timestamp 'past)
4686 (<= (org-days-to-time (match-string 1)) 0))
4687 ((numberp org-agenda-todo-ignore-timestamp)
4688 (org-agenda-todo-custom-ignore-p
4689 (match-string 1) org-agenda-todo-ignore-timestamp))
4690 (t))))))))))
4692 (defconst org-agenda-no-heading-message
4693 "No heading for this item in buffer or region.")
4695 (defun org-agenda-get-timestamps ()
4696 "Return the date stamp information for agenda display."
4697 (let* ((props (list 'face nil
4698 'org-not-done-regexp org-not-done-regexp
4699 'org-todo-regexp org-todo-regexp
4700 'org-complex-heading-regexp org-complex-heading-regexp
4701 'mouse-face 'highlight
4702 'help-echo
4703 (format "mouse-2 or RET jump to org file %s"
4704 (abbreviate-file-name buffer-file-name))))
4705 (d1 (calendar-absolute-from-gregorian date))
4706 (remove-re
4707 (concat
4708 (regexp-quote
4709 (format-time-string
4710 "<%Y-%m-%d"
4711 (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
4712 ".*?>"))
4713 (regexp
4714 (concat
4715 (if org-agenda-include-inactive-timestamps "[[<]" "<")
4716 (regexp-quote
4717 (substring
4718 (format-time-string
4719 (car org-time-stamp-formats)
4720 (apply 'encode-time ; DATE bound by calendar
4721 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4722 1 11))
4723 "\\|\\(<[0-9]+-[0-9]+-[0-9]+[^>\n]+?\\+[0-9]+[dwmy]>\\)"
4724 "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
4725 marker hdmarker deadlinep scheduledp clockp closedp inactivep
4726 donep tmp priority category ee txt timestr tags b0 b3 e3 head
4727 todo-state end-of-match show-all)
4728 (goto-char (point-min))
4729 (while (setq end-of-match (re-search-forward regexp nil t))
4730 (setq b0 (match-beginning 0)
4731 b3 (match-beginning 3) e3 (match-end 3)
4732 todo-state (save-match-data (ignore-errors (org-get-todo-state)))
4733 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
4734 (member todo-state
4735 org-agenda-repeating-timestamp-show-all)))
4736 (catch :skip
4737 (and (org-at-date-range-p) (throw :skip nil))
4738 (org-agenda-skip)
4739 (if (and (match-end 1)
4740 (not (= d1 (org-time-string-to-absolute
4741 (match-string 1) d1 nil show-all))))
4742 (throw :skip nil))
4743 (if (and e3
4744 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
4745 (throw :skip nil))
4746 (setq tmp (buffer-substring (max (point-min)
4747 (- b0 org-ds-keyword-length))
4749 timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
4750 inactivep (= (char-after b0) ?\[)
4751 deadlinep (string-match org-deadline-regexp tmp)
4752 scheduledp (string-match org-scheduled-regexp tmp)
4753 closedp (and org-agenda-include-inactive-timestamps
4754 (string-match org-closed-string tmp))
4755 clockp (and org-agenda-include-inactive-timestamps
4756 (or (string-match org-clock-string tmp)
4757 (string-match "]-+\\'" tmp)))
4758 donep (member todo-state org-done-keywords))
4759 (if (or scheduledp deadlinep closedp clockp
4760 (and donep org-agenda-skip-timestamp-if-done))
4761 (throw :skip t))
4762 (if (string-match ">" timestr)
4763 ;; substring should only run to end of time stamp
4764 (setq timestr (substring timestr 0 (match-end 0))))
4765 (setq marker (org-agenda-new-marker b0)
4766 category (org-get-category b0))
4767 (save-excursion
4768 (if (not (re-search-backward org-outline-regexp-bol nil t))
4769 (setq txt org-agenda-no-heading-message)
4770 (goto-char (match-beginning 0))
4771 (setq hdmarker (org-agenda-new-marker)
4772 tags (org-get-tags-at))
4773 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4774 (setq head (or (match-string 1) ""))
4775 (setq txt (org-format-agenda-item
4776 (if inactivep org-agenda-inactive-leader nil)
4777 head category tags timestr
4778 remove-re)))
4779 (setq priority (org-get-priority txt))
4780 (org-add-props txt props
4781 'org-marker marker 'org-hd-marker hdmarker)
4782 (org-add-props txt nil 'priority priority
4783 'org-category category 'date date
4784 'todo-state todo-state
4785 'type "timestamp")
4786 (push txt ee))
4787 (if org-agenda-skip-additional-timestamps-same-entry
4788 (outline-next-heading)
4789 (goto-char end-of-match))))
4790 (nreverse ee)))
4792 (defun org-agenda-get-sexps ()
4793 "Return the sexp information for agenda display."
4794 (require 'diary-lib)
4795 (let* ((props (list 'mouse-face 'highlight
4796 'help-echo
4797 (format "mouse-2 or RET jump to org file %s"
4798 (abbreviate-file-name buffer-file-name))))
4799 (regexp "^&?%%(")
4800 marker category ee txt tags entry result beg b sexp sexp-entry
4801 todo-state)
4802 (goto-char (point-min))
4803 (while (re-search-forward regexp nil t)
4804 (catch :skip
4805 (org-agenda-skip)
4806 (setq beg (match-beginning 0))
4807 (goto-char (1- (match-end 0)))
4808 (setq b (point))
4809 (forward-sexp 1)
4810 (setq sexp (buffer-substring b (point)))
4811 (setq sexp-entry (if (looking-at "[ \t]*\\(\\S-.*\\)")
4812 (org-trim (match-string 1))
4813 ""))
4814 (setq result (org-diary-sexp-entry sexp sexp-entry date))
4815 (when result
4816 (setq marker (org-agenda-new-marker beg)
4817 category (org-get-category beg)
4818 todo-state (org-get-todo-state))
4820 (dolist (r (if (stringp result)
4821 (list result)
4822 result)) ;; we expect a list here
4823 (if (string-match "\\S-" r)
4824 (setq txt r)
4825 (setq txt "SEXP entry returned empty string"))
4827 (setq txt (org-format-agenda-item
4828 "" txt category tags 'time))
4829 (org-add-props txt props 'org-marker marker)
4830 (org-add-props txt nil
4831 'org-category category 'date date 'todo-state todo-state
4832 'type "sexp")
4833 (push txt ee)))))
4834 (nreverse ee)))
4836 ;; Calendar sanity: define some functions that are independent of
4837 ;; `calendar-date-style'.
4838 ;; Normally I would like to use ISO format when calling the diary functions,
4839 ;; but to make sure we still have Emacs 22 compatibility we bind
4840 ;; also `european-calendar-style' and use european format
4841 (defun org-anniversary (year month day &optional mark)
4842 "Like `diary-anniversary', but with fixed (ISO) order of arguments."
4843 (org-no-warnings
4844 (let ((calendar-date-style 'european) (european-calendar-style t))
4845 (diary-anniversary day month year mark))))
4846 (defun org-cyclic (N year month day &optional mark)
4847 "Like `diary-cyclic', but with fixed (ISO) order of arguments."
4848 (org-no-warnings
4849 (let ((calendar-date-style 'european) (european-calendar-style t))
4850 (diary-cyclic N day month year mark))))
4851 (defun org-block (Y1 M1 D1 Y2 M2 D2 &optional mark)
4852 "Like `diary-block', but with fixed (ISO) order of arguments."
4853 (org-no-warnings
4854 (let ((calendar-date-style 'european) (european-calendar-style t))
4855 (diary-block D1 M1 Y1 D2 M2 Y2 mark))))
4856 (defun org-date (year month day &optional mark)
4857 "Like `diary-date', but with fixed (ISO) order of arguments."
4858 (org-no-warnings
4859 (let ((calendar-date-style 'european) (european-calendar-style t))
4860 (diary-date day month year mark))))
4861 (defalias 'org-float 'diary-float)
4863 ;; Define the` org-class' function
4864 (defun org-class (y1 m1 d1 y2 m2 d2 dayname &rest skip-weeks)
4865 "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
4866 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4867 is any number of ISO weeks in the block period for which the item should
4868 be skipped."
4869 (let* ((date1 (calendar-absolute-from-gregorian (list m1 d1 y1)))
4870 (date2 (calendar-absolute-from-gregorian (list m2 d2 y2)))
4871 (d (calendar-absolute-from-gregorian date)))
4872 (and
4873 (<= date1 d)
4874 (<= d date2)
4875 (= (calendar-day-of-week date) dayname)
4876 (or (not skip-weeks)
4877 (progn
4878 (require 'cal-iso)
4879 (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
4880 entry)))
4882 (defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
4883 "Like `org-class', but honor `calendar-date-style'.
4884 The order of the first 2 times 3 arguments depends on the variable
4885 `calendar-date-style' or, if that is not defined, on `european-calendar-style'.
4886 So for American calendars, give this as MONTH DAY YEAR, for European as
4887 DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
4888 DAYNAME is a number between 0 (Sunday) and 6 (Saturday). SKIP-WEEKS
4889 is any number of ISO weeks in the block period for which the item should
4890 be skipped.
4892 This function is here only for backward compatibility and it is deprecated,
4893 please use `org-class' instead."
4894 (let* ((date1 (org-order-calendar-date-args m1 d1 y1))
4895 (date2 (org-order-calendar-date-args m2 d2 y2)))
4896 (org-class
4897 (nth 2 date1) (car date1) (nth 1 date1)
4898 (nth 2 date2) (car date2) (nth 1 date2)
4899 dayname skip-weeks)))
4901 (defalias 'org-get-closed 'org-agenda-get-progress)
4902 (defun org-agenda-get-progress ()
4903 "Return the logged TODO entries for agenda display."
4904 (let* ((props (list 'mouse-face 'highlight
4905 'org-not-done-regexp org-not-done-regexp
4906 'org-todo-regexp org-todo-regexp
4907 'org-complex-heading-regexp org-complex-heading-regexp
4908 'help-echo
4909 (format "mouse-2 or RET jump to org file %s"
4910 (abbreviate-file-name buffer-file-name))))
4911 (items (if (consp org-agenda-show-log)
4912 org-agenda-show-log
4913 (if (eq org-agenda-show-log 'clockcheck)
4914 '(clock)
4915 org-agenda-log-mode-items)))
4916 (parts
4917 (delq nil
4918 (list
4919 (if (memq 'closed items) (concat "\\<" org-closed-string))
4920 (if (memq 'clock items) (concat "\\<" org-clock-string))
4921 (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
4922 (parts-re (if parts (mapconcat 'identity parts "\\|")
4923 (error "`org-agenda-log-mode-items' is empty")))
4924 (regexp (concat
4925 "\\(" parts-re "\\)"
4926 " *\\["
4927 (regexp-quote
4928 (substring
4929 (format-time-string
4930 (car org-time-stamp-formats)
4931 (apply 'encode-time ; DATE bound by calendar
4932 (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
4933 1 11))))
4934 (org-agenda-search-headline-for-time nil)
4935 marker hdmarker priority category tags closedp statep clockp state
4936 ee txt extra timestr rest clocked)
4937 (goto-char (point-min))
4938 (while (re-search-forward regexp nil t)
4939 (catch :skip
4940 (org-agenda-skip)
4941 (setq marker (org-agenda-new-marker (match-beginning 0))
4942 closedp (equal (match-string 1) org-closed-string)
4943 statep (equal (string-to-char (match-string 1)) ?-)
4944 clockp (not (or closedp statep))
4945 state (and statep (match-string 2))
4946 category (org-get-category (match-beginning 0))
4947 timestr (buffer-substring (match-beginning 0) (point-at-eol))
4949 (when (string-match "\\]" timestr)
4950 ;; substring should only run to end of time stamp
4951 (setq rest (substring timestr (match-end 0))
4952 timestr (substring timestr 0 (match-end 0)))
4953 (if (and (not closedp) (not statep)
4954 (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
4955 (progn (setq timestr (concat (substring timestr 0 -1)
4956 "-" (match-string 1 rest) "]"))
4957 (setq clocked (match-string 2 rest)))
4958 (setq clocked "-")))
4959 (save-excursion
4960 (setq extra
4961 (cond
4962 ((not org-agenda-log-mode-add-notes) nil)
4963 (statep
4964 (and (looking-at ".*\\\\\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
4965 (match-string 1)))
4966 (clockp
4967 (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
4968 (match-string 1)))))
4969 (if (not (re-search-backward org-outline-regexp-bol nil t))
4970 (setq txt org-agenda-no-heading-message)
4971 (goto-char (match-beginning 0))
4972 (setq hdmarker (org-agenda-new-marker)
4973 tags (org-get-tags-at))
4974 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
4975 (setq txt (match-string 1))
4976 (when extra
4977 (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
4978 (setq txt (concat (substring txt 0 (match-beginning 1))
4979 " - " extra " " (match-string 2 txt)))
4980 (setq txt (concat txt " - " extra))))
4981 (setq txt (org-format-agenda-item
4982 (cond
4983 (closedp "Closed: ")
4984 (statep (concat "State: (" state ")"))
4985 (t (concat "Clocked: (" clocked ")")))
4986 txt category tags timestr)))
4987 (setq priority 100000)
4988 (org-add-props txt props
4989 'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
4990 'priority priority 'org-category category
4991 'type "closed" 'date date
4992 'undone-face 'org-warning 'done-face 'org-agenda-done)
4993 (push txt ee))
4994 (goto-char (point-at-eol))))
4995 (nreverse ee)))
4997 (defun org-agenda-show-clocking-issues ()
4998 "Add overlays, showing issues with clocking.
4999 See also the user option `org-agenda-clock-consistency-checks'."
5000 (interactive)
5001 (let* ((pl org-agenda-clock-consistency-checks)
5002 (re (concat "^[ \t]*"
5003 org-clock-string
5004 "[ \t]+"
5005 "\\(\\[.*?\\]\\)" ; group 1 is first stamp
5006 "\\(-\\{1,3\\}\\(\\[.*?\\]\\)\\)?")) ; group 3 is second
5007 (tlstart 0.)
5008 (tlend 0.)
5009 (maxtime (org-hh:mm-string-to-minutes
5010 (or (plist-get pl :max-duration) "24:00")))
5011 (mintime (org-hh:mm-string-to-minutes
5012 (or (plist-get pl :min-duration) 0)))
5013 (maxgap (org-hh:mm-string-to-minutes
5014 ;; default 30:00 means never complain
5015 (or (plist-get pl :max-gap) "30:00")))
5016 (gapok (mapcar 'org-hh:mm-string-to-minutes
5017 (plist-get pl :gap-ok-around)))
5018 (def-face (or (plist-get pl :default-face)
5019 '((:background "DarkRed") (:foreground "white"))))
5020 issue face m te ts dt ov)
5021 (goto-char (point-min))
5022 (while (re-search-forward " Clocked: +(-\\|\\([0-9]+:[0-9]+\\))" nil t)
5023 (setq issue nil face def-face)
5024 (catch 'next
5025 (setq m (org-get-at-bol 'org-marker)
5026 te nil ts nil)
5027 (unless (and m (markerp m))
5028 (setq issue "No valid clock line") (throw 'next t))
5029 (org-with-point-at m
5030 (save-excursion
5031 (goto-char (point-at-bol))
5032 (unless (looking-at re)
5033 (error "No valid Clock line")
5034 (throw 'next t))
5035 (unless (match-end 3)
5036 (setq issue "No end time"
5037 face (or (plist-get pl :no-end-time-face) face))
5038 (throw 'next t))
5039 (setq ts (match-string 1)
5040 te (match-string 3)
5041 ts (org-float-time
5042 (apply 'encode-time (org-parse-time-string ts)))
5043 te (org-float-time
5044 (apply 'encode-time (org-parse-time-string te)))
5045 dt (- te ts))))
5046 (cond
5047 ((> dt (* 60 maxtime))
5048 ;; a very long clocking chunk
5049 (setq issue (format "Clocking interval is very long: %s"
5050 (org-minutes-to-hh:mm-string
5051 (floor (/ (float dt) 60.))))
5052 face (or (plist-get pl :long-face) face)))
5053 ((< dt (* 60 mintime))
5054 ;; a very short clocking chunk
5055 (setq issue (format "Clocking interval is very short: %s"
5056 (org-minutes-to-hh:mm-string
5057 (floor (/ (float dt) 60.))))
5058 face (or (plist-get pl :short-face) face)))
5059 ((and (> tlend 0) (< ts tlend))
5060 ;; Two clock entries are overlapping
5061 (setq issue (format "Clocking overlap: %d minutes"
5062 (/ (- tlend ts) 60))
5063 face (or (plist-get pl :overlap-face) face)))
5064 ((and (> tlend 0) (> ts (+ tlend (* 60 maxgap))))
5065 ;; There is a gap, lets see if we need to report it
5066 (unless (org-agenda-check-clock-gap tlend ts gapok)
5067 (setq issue (format "Clocking gap: %d minutes"
5068 (/ (- ts tlend) 60))
5069 face (or (plist-get pl :gap-face) face))))
5070 (t nil)))
5071 (setq tlend (or te tlend) tlstart (or ts tlstart))
5072 (when issue
5073 ;; OK, there was some issue, add an overlay to show the issue
5074 (setq ov (make-overlay (point-at-bol) (point-at-eol)))
5075 (overlay-put ov 'before-string
5076 (concat
5077 (org-add-props
5078 (format "%-43s" (concat " " issue))
5080 'face face)
5081 "\n"))
5082 (overlay-put ov 'evaporate t)))))
5084 (defun org-agenda-check-clock-gap (t1 t2 ok-list)
5085 "Check if gap T1 -> T2 contains one of the OK-LIST time-of-day values."
5086 (catch 'exit
5087 (unless ok-list
5088 ;; there are no OK times for gaps...
5089 (throw 'exit nil))
5090 (if (> (- (/ t2 36000) (/ t1 36000)) 24)
5091 ;; This is more than 24 hours, so it is OK.
5092 ;; because we have at least one OK time, that must be in the
5093 ;; 24 hour interval.
5094 (throw 'exit t))
5095 ;; We have a shorter gap.
5096 ;; Now we have to get the minute of the day when these times are
5097 (let* ((t1dec (decode-time (seconds-to-time t1)))
5098 (t2dec (decode-time (seconds-to-time t2)))
5099 ;; compute the minute on the day
5100 (min1 (+ (nth 1 t1dec) (* 60 (nth 2 t1dec))))
5101 (min2 (+ (nth 1 t2dec) (* 60 (nth 2 t2dec)))))
5102 (when (< min2 min1)
5103 ;; if min2 is smaller than min1, this means it is on the next day.
5104 ;; Wrap it to after midnight.
5105 (setq min2 (+ min2 1440)))
5106 ;; Now check if any of the OK times is in the gap
5107 (mapc (lambda (x)
5108 ;; Wrap the time to after midnight if necessary
5109 (if (< x min1) (setq x (+ x 1440)))
5110 ;; Check if in interval
5111 (and (<= min1 x) (>= min2 x) (throw 'exit t)))
5112 ok-list)
5113 ;; Nope, this gap is not OK
5114 nil)))
5116 (defun org-agenda-get-deadlines ()
5117 "Return the deadline information for agenda display."
5118 (let* ((props (list 'mouse-face 'highlight
5119 'org-not-done-regexp org-not-done-regexp
5120 'org-todo-regexp org-todo-regexp
5121 'org-complex-heading-regexp org-complex-heading-regexp
5122 'help-echo
5123 (format "mouse-2 or RET jump to org file %s"
5124 (abbreviate-file-name buffer-file-name))))
5125 (regexp org-deadline-time-regexp)
5126 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5127 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5128 d2 diff dfrac wdays pos pos1 category tags
5129 suppress-prewarning
5130 ee txt head face s todo-state show-all upcomingp donep timestr)
5131 (goto-char (point-min))
5132 (while (re-search-forward regexp nil t)
5133 (setq suppress-prewarning nil)
5134 (catch :skip
5135 (org-agenda-skip)
5136 (when (and org-agenda-skip-deadline-prewarning-if-scheduled
5137 (save-match-data
5138 (string-match org-scheduled-time-regexp
5139 (buffer-substring (point-at-bol)
5140 (point-at-eol)))))
5141 (setq suppress-prewarning
5142 (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
5143 org-agenda-skip-deadline-prewarning-if-scheduled
5144 0)))
5145 (setq s (match-string 1)
5146 txt nil
5147 pos (1- (match-beginning 1))
5148 todo-state (save-match-data (org-get-todo-state))
5149 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5150 (member todo-state
5151 org-agenda-repeating-timestamp-show-all))
5152 d2 (org-time-string-to-absolute
5153 (match-string 1) d1 'past show-all)
5154 diff (- d2 d1)
5155 wdays (if suppress-prewarning
5156 (let ((org-deadline-warning-days suppress-prewarning))
5157 (org-get-wdays s))
5158 (org-get-wdays s))
5159 dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
5160 upcomingp (and todayp (> diff 0)))
5161 ;; When to show a deadline in the calendar:
5162 ;; If the expiration is within wdays warning time.
5163 ;; Past-due deadlines are only shown on the current date
5164 (if (and (or (and (<= diff wdays)
5165 (and todayp (not org-agenda-only-exact-dates)))
5166 (= diff 0)))
5167 (save-excursion
5168 ;; (setq todo-state (org-get-todo-state))
5169 (setq donep (member todo-state org-done-keywords))
5170 (if (and donep
5171 (or org-agenda-skip-deadline-if-done
5172 (not (= diff 0))))
5173 (setq txt nil)
5174 (setq category (org-get-category))
5175 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5176 (setq txt org-agenda-no-heading-message)
5177 (goto-char (match-end 0))
5178 (setq pos1 (match-beginning 0))
5179 (setq tags (org-get-tags-at pos1))
5180 (setq head (buffer-substring-no-properties
5181 (point)
5182 (progn (skip-chars-forward "^\r\n")
5183 (point))))
5184 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5185 (setq timestr
5186 (concat (substring s (match-beginning 1)) " "))
5187 (setq timestr 'time))
5188 (setq txt (org-format-agenda-item
5189 (if (= diff 0)
5190 (car org-agenda-deadline-leaders)
5191 (if (functionp
5192 (nth 1 org-agenda-deadline-leaders))
5193 (funcall
5194 (nth 1 org-agenda-deadline-leaders)
5195 diff date)
5196 (format (nth 1 org-agenda-deadline-leaders)
5197 diff)))
5198 head category tags
5199 (if (not (= diff 0)) nil timestr)))))
5200 (when txt
5201 (setq face (org-agenda-deadline-face dfrac wdays))
5202 (org-add-props txt props
5203 'org-marker (org-agenda-new-marker pos)
5204 'org-hd-marker (org-agenda-new-marker pos1)
5205 'priority (+ (- diff)
5206 (org-get-priority txt))
5207 'org-category category
5208 'todo-state todo-state
5209 'type (if upcomingp "upcoming-deadline" "deadline")
5210 'date (if upcomingp date d2)
5211 'face (if donep 'org-agenda-done face)
5212 'undone-face face 'done-face 'org-agenda-done)
5213 (push txt ee))))))
5214 (nreverse ee)))
5216 (defun org-agenda-deadline-face (fraction &optional wdays)
5217 "Return the face to displaying a deadline item.
5218 FRACTION is what fraction of the head-warning time has passed."
5219 (if (equal wdays 0) (setq fraction 1.))
5220 (let ((faces org-agenda-deadline-faces) f)
5221 (catch 'exit
5222 (while (setq f (pop faces))
5223 (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
5225 (defun org-agenda-get-scheduled (&optional deadline-results)
5226 "Return the scheduled information for agenda display."
5227 (let* ((props (list 'org-not-done-regexp org-not-done-regexp
5228 'org-todo-regexp org-todo-regexp
5229 'org-complex-heading-regexp org-complex-heading-regexp
5230 'done-face 'org-agenda-done
5231 'mouse-face 'highlight
5232 'help-echo
5233 (format "mouse-2 or RET jump to org file %s"
5234 (abbreviate-file-name buffer-file-name))))
5235 (regexp org-scheduled-time-regexp)
5236 (todayp (org-agenda-todayp date)) ; DATE bound by calendar
5237 (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
5239 (deadline-position-alist
5240 (mapcar (lambda (a) (and (setq mm (get-text-property
5241 0 'org-hd-marker a))
5242 (cons (marker-position mm) a)))
5243 deadline-results))
5244 d2 diff pos pos1 category tags donep
5245 ee txt head pastschedp todo-state face timestr s habitp show-all)
5246 (goto-char (point-min))
5247 (while (re-search-forward regexp nil t)
5248 (catch :skip
5249 (org-agenda-skip)
5250 (setq s (match-string 1)
5251 txt nil
5252 pos (1- (match-beginning 1))
5253 todo-state (save-match-data (org-get-todo-state))
5254 show-all (or (eq org-agenda-repeating-timestamp-show-all t)
5255 (member todo-state
5256 org-agenda-repeating-timestamp-show-all))
5257 d2 (org-time-string-to-absolute
5258 (match-string 1) d1 'past show-all)
5259 diff (- d2 d1))
5260 (setq pastschedp (and todayp (< diff 0)))
5261 ;; When to show a scheduled item in the calendar:
5262 ;; If it is on or past the date.
5263 (when (or (and (< diff 0)
5264 (< (abs diff) org-scheduled-past-days)
5265 (and todayp (not org-agenda-only-exact-dates)))
5266 (= diff 0))
5267 (save-excursion
5268 (setq donep (member todo-state org-done-keywords))
5269 (if (and donep
5270 (or org-agenda-skip-scheduled-if-done
5271 (not (= diff 0))
5272 (and (functionp 'org-is-habit-p)
5273 (org-is-habit-p))))
5274 (setq txt nil)
5275 (setq habitp (and (functionp 'org-is-habit-p)
5276 (org-is-habit-p)))
5277 (setq category (org-get-category))
5278 (if (not (re-search-backward "^\\*+[ \t]+" nil t))
5279 (setq txt org-agenda-no-heading-message)
5280 (goto-char (match-end 0))
5281 (setq pos1 (match-beginning 0))
5282 (if habitp
5283 (if (or (not org-habit-show-habits)
5284 (and (not todayp)
5285 org-habit-show-habits-only-for-today))
5286 (throw :skip nil))
5287 (if (and
5288 (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
5289 (and org-agenda-skip-scheduled-if-deadline-is-shown
5290 pastschedp))
5291 (setq mm (assoc pos1 deadline-position-alist)))
5292 (throw :skip nil)))
5293 (setq tags (org-get-tags-at))
5294 (setq head (buffer-substring-no-properties
5295 (point)
5296 (progn (skip-chars-forward "^\r\n") (point))))
5297 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
5298 (setq timestr
5299 (concat (substring s (match-beginning 1)) " "))
5300 (setq timestr 'time))
5301 (setq txt (org-format-agenda-item
5302 (if (= diff 0)
5303 (car org-agenda-scheduled-leaders)
5304 (format (nth 1 org-agenda-scheduled-leaders)
5305 (- 1 diff)))
5306 head category tags
5307 (if (not (= diff 0)) nil timestr)
5308 nil habitp))))
5309 (when txt
5310 (setq face
5311 (cond
5312 ((and (not habitp) pastschedp)
5313 'org-scheduled-previously)
5314 (todayp 'org-scheduled-today)
5315 (t 'org-scheduled))
5316 habitp (and habitp (org-habit-parse-todo)))
5317 (org-add-props txt props
5318 'undone-face face
5319 'face (if donep 'org-agenda-done face)
5320 'org-marker (org-agenda-new-marker pos)
5321 'org-hd-marker (org-agenda-new-marker pos1)
5322 'type (if pastschedp "past-scheduled" "scheduled")
5323 'date (if pastschedp d2 date)
5324 'priority (if habitp
5325 (org-habit-get-priority habitp)
5326 (+ 94 (- 5 diff) (org-get-priority txt)))
5327 'org-category category
5328 'org-habit-p habitp
5329 'todo-state todo-state)
5330 (push txt ee))))))
5331 (nreverse ee)))
5333 (defun org-agenda-get-blocks ()
5334 "Return the date-range information for agenda display."
5335 (let* ((props (list 'face nil
5336 'org-not-done-regexp org-not-done-regexp
5337 'org-todo-regexp org-todo-regexp
5338 'org-complex-heading-regexp org-complex-heading-regexp
5339 'mouse-face 'highlight
5340 'help-echo
5341 (format "mouse-2 or RET jump to org file %s"
5342 (abbreviate-file-name buffer-file-name))))
5343 (regexp org-tr-regexp)
5344 (d0 (calendar-absolute-from-gregorian date))
5345 marker hdmarker ee txt d1 d2 s1 s2 category todo-state tags pos
5346 head donep)
5347 (goto-char (point-min))
5348 (while (re-search-forward regexp nil t)
5349 (catch :skip
5350 (org-agenda-skip)
5351 (setq pos (point))
5352 (let ((start-time (match-string 1))
5353 (end-time (match-string 2)))
5354 (setq s1 (match-string 1)
5355 s2 (match-string 2)
5356 d1 (time-to-days (org-time-string-to-time s1))
5357 d2 (time-to-days (org-time-string-to-time s2)))
5358 (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
5359 ;; Only allow days between the limits, because the normal
5360 ;; date stamps will catch the limits.
5361 (save-excursion
5362 (setq todo-state (org-get-todo-state))
5363 (setq donep (member todo-state org-done-keywords))
5364 (if (and donep org-agenda-skip-timestamp-if-done)
5365 (throw :skip t))
5366 (setq marker (org-agenda-new-marker (point)))
5367 (setq category (org-get-category))
5368 (if (not (re-search-backward org-outline-regexp-bol nil t))
5369 (setq txt org-agenda-no-heading-message)
5370 (goto-char (match-beginning 0))
5371 (setq hdmarker (org-agenda-new-marker (point)))
5372 (setq tags (org-get-tags-at))
5373 (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
5374 (setq head (match-string 1))
5375 (let ((remove-re
5376 (if org-agenda-remove-timeranges-from-blocks
5377 (concat
5378 "<" (regexp-quote s1) ".*?>"
5379 "--"
5380 "<" (regexp-quote s2) ".*?>")
5381 nil)))
5382 (setq txt (org-format-agenda-item
5383 (format
5384 (nth (if (= d1 d2) 0 1)
5385 org-agenda-timerange-leaders)
5386 (1+ (- d0 d1)) (1+ (- d2 d1)))
5387 head category tags
5388 (cond ((= d1 d0)
5389 (concat "<" start-time ">"))
5390 ((= d2 d0)
5391 (concat "<" end-time ">"))
5392 (t nil))
5393 remove-re))))
5394 (org-add-props txt props
5395 'org-marker marker 'org-hd-marker hdmarker
5396 'type "block" 'date date
5397 'todo-state todo-state
5398 'priority (org-get-priority txt) 'org-category category)
5399 (push txt ee))))
5400 (goto-char pos)))
5401 ;; Sort the entries by expiration date.
5402 (nreverse ee)))
5404 ;;; Agenda presentation and sorting
5406 (defvar org-prefix-has-time nil
5407 "A flag, set by `org-compile-prefix-format'.
5408 The flag is set if the currently compiled format contains a `%t'.")
5409 (defvar org-prefix-has-tag nil
5410 "A flag, set by `org-compile-prefix-format'.
5411 The flag is set if the currently compiled format contains a `%T'.")
5412 (defvar org-prefix-has-effort nil
5413 "A flag, set by `org-compile-prefix-format'.
5414 The flag is set if the currently compiled format contains a `%e'.")
5415 (defvar org-prefix-category-length nil
5416 "Used by `org-compile-prefix-format' to remember the category field width.")
5417 (defvar org-prefix-category-max-length nil
5418 "Used by `org-compile-prefix-format' to remember the category field width.")
5420 (defun org-agenda-get-category-icon (category)
5421 "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
5422 (dolist (entry org-agenda-category-icon-alist)
5423 (when (org-string-match-p (car entry) category)
5424 (if (listp (cadr entry))
5425 (return (cadr entry))
5426 (return (apply 'create-image (cdr entry)))))))
5428 (defun org-format-agenda-item (extra txt &optional category tags dotime
5429 remove-re habitp)
5430 "Format TXT to be inserted into the agenda buffer.
5431 In particular, it adds the prefix and corresponding text properties. EXTRA
5432 must be a string and replaces the `%s' specifier in the prefix format.
5433 CATEGORY (string, symbol or nil) may be used to overrule the default
5434 category taken from local variable or file name. It will replace the `%c'
5435 specifier in the format. DOTIME, when non-nil, indicates that a
5436 time-of-day should be extracted from TXT for sorting of this entry, and for
5437 the `%t' specifier in the format. When DOTIME is a string, this string is
5438 searched for a time before TXT is. TAGS can be the tags of the headline.
5439 Any match of REMOVE-RE will be removed from TXT."
5440 (save-match-data
5441 ;; Diary entries sometimes have extra whitespace at the beginning
5442 (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
5444 ;; Fix the tags part in txt
5445 (setq txt (org-agenda-fix-displayed-tags
5446 txt tags
5447 org-agenda-show-inherited-tags
5448 org-agenda-hide-tags-regexp))
5449 (let* ((category (or category
5450 (if (stringp org-category)
5451 org-category
5452 (and org-category (symbol-name org-category)))
5453 (if buffer-file-name
5454 (file-name-sans-extension
5455 (file-name-nondirectory buffer-file-name))
5456 "")))
5457 (category-icon (org-agenda-get-category-icon category))
5458 (category-icon (if category-icon
5459 (propertize " " 'display category-icon)
5460 ""))
5461 ;; time, tag, effort are needed for the eval of the prefix format
5462 (tag (if tags (nth (1- (length tags)) tags) ""))
5463 time effort neffort
5464 (ts (if dotime (concat
5465 (if (stringp dotime) dotime "")
5466 (and org-agenda-search-headline-for-time txt))))
5467 (time-of-day (and dotime (org-get-time-of-day ts)))
5468 stamp plain s0 s1 s2 rtn srp l
5469 duration thecategory)
5470 (and (org-mode-p) buffer-file-name
5471 (add-to-list 'org-agenda-contributing-files buffer-file-name))
5472 (when (and dotime time-of-day)
5473 ;; Extract starting and ending time and move them to prefix
5474 (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
5475 (setq plain (string-match org-plain-time-of-day-regexp ts)))
5476 (setq s0 (match-string 0 ts)
5477 srp (and stamp (match-end 3))
5478 s1 (match-string (if plain 1 2) ts)
5479 s2 (match-string (if plain 8 (if srp 4 6)) ts))
5481 ;; If the times are in TXT (not in DOTIMES), and the prefix will list
5482 ;; them, we might want to remove them there to avoid duplication.
5483 ;; The user can turn this off with a variable.
5484 (if (and org-prefix-has-time
5485 org-agenda-remove-times-when-in-prefix (or stamp plain)
5486 (string-match (concat (regexp-quote s0) " *") txt)
5487 (not (equal ?\] (string-to-char (substring txt (match-end 0)))))
5488 (if (eq org-agenda-remove-times-when-in-prefix 'beg)
5489 (= (match-beginning 0) 0)
5491 (setq txt (replace-match "" nil nil txt))))
5492 ;; Normalize the time(s) to 24 hour
5493 (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
5494 (if s2 (setq s2 (org-get-time-of-day s2 'string t)))
5496 ;; Try to set s2 if s1 and `org-agenda-default-appointment-duration' are set
5497 (when (and s1 (not s2) org-agenda-default-appointment-duration)
5498 (setq s2
5499 (org-minutes-to-hh:mm-string
5500 (+ (org-hh:mm-string-to-minutes s1) org-agenda-default-appointment-duration))))
5502 ;; Compute the duration
5503 (when s2
5504 (setq duration (- (org-hh:mm-string-to-minutes s2)
5505 (org-hh:mm-string-to-minutes s1)))))
5507 (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
5508 txt)
5509 ;; Tags are in the string
5510 (if (or (eq org-agenda-remove-tags t)
5511 (and org-agenda-remove-tags
5512 org-prefix-has-tag))
5513 (setq txt (replace-match "" t t txt))
5514 (setq txt (replace-match
5515 (concat (make-string (max (- 50 (length txt)) 1) ?\ )
5516 (match-string 2 txt))
5517 t t txt))))
5518 (when (org-mode-p)
5519 (setq effort
5520 (condition-case nil
5521 (org-get-effort
5522 (or (get-text-property 0 'org-hd-marker txt)
5523 (get-text-property 0 'org-marker txt)))
5524 (error nil)))
5525 (when effort
5526 (setq neffort (org-duration-string-to-minutes effort)
5527 effort (setq effort (concat "[" effort "]")))))
5528 ;; prevent erroring out with %e format when there is no effort
5529 (or effort (setq effort ""))
5531 (when remove-re
5532 (while (string-match remove-re txt)
5533 (setq txt (replace-match "" t t txt))))
5535 ;; Set org-heading property on `txt' to mark the start of the
5536 ;; heading.
5537 (add-text-properties 0 (length txt) '(org-heading t) txt)
5539 ;; Prepare the variables needed in the eval of the compiled format
5540 (setq time (cond (s2 (concat
5541 (org-agenda-time-of-day-to-ampm-maybe s1)
5542 "-" (org-agenda-time-of-day-to-ampm-maybe s2)
5543 (if org-agenda-timegrid-use-ampm " ")))
5544 (s1 (concat
5545 (org-agenda-time-of-day-to-ampm-maybe s1)
5546 (if org-agenda-timegrid-use-ampm
5547 "........ "
5548 "......")))
5549 (t ""))
5550 extra (or (and (not habitp) extra) "")
5551 category (if (symbolp category) (symbol-name category) category)
5552 thecategory (copy-sequence category))
5553 (if (string-match org-bracket-link-regexp category)
5554 (progn
5555 (setq l (if (match-end 3)
5556 (- (match-end 3) (match-beginning 3))
5557 (- (match-end 1) (match-beginning 1))))
5558 (when (< l (or org-prefix-category-length 0))
5559 (setq category (copy-sequence category))
5560 (org-add-props category nil
5561 'extra-space (make-string
5562 (- org-prefix-category-length l 1) ?\ ))))
5563 (if (and org-prefix-category-max-length
5564 (>= (length category) org-prefix-category-max-length))
5565 (setq category (substring category 0 (1- org-prefix-category-max-length)))))
5566 ;; Evaluate the compiled format
5567 (assert effort)
5568 (setq rtn (concat (eval org-prefix-format-compiled) txt))
5570 ;; And finally add the text properties
5571 (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
5572 (org-add-props rtn nil
5573 'org-category (if thecategory (downcase thecategory) category)
5574 'tags (mapcar 'org-downcase-keep-props tags)
5575 'org-highest-priority org-highest-priority
5576 'org-lowest-priority org-lowest-priority
5577 'time-of-day time-of-day
5578 'duration duration
5579 'effort effort
5580 'effort-minutes neffort
5581 'txt txt
5582 'time time
5583 'extra extra
5584 'dotime dotime))))
5586 (defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
5587 "Remove tags string from TXT, and add a modified list of tags.
5588 The modified list may contain inherited tags, and tags matched by
5589 `org-agenda-hide-tags-regexp' will be removed."
5590 (when (or add-inherited hide-re)
5591 (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
5592 (setq txt (substring txt 0 (match-beginning 0))))
5593 (setq tags
5594 (delq nil
5595 (mapcar (lambda (tg)
5596 (if (or (and hide-re (string-match hide-re tg))
5597 (and (not add-inherited)
5598 (get-text-property 0 'inherited tg)))
5600 tg))
5601 tags)))
5602 (when tags
5603 (let ((have-i (get-text-property 0 'inherited (car tags)))
5605 (setq txt (concat txt " :"
5606 (mapconcat
5607 (lambda (x)
5608 (setq i (get-text-property 0 'inherited x))
5609 (if (and have-i (not i))
5610 (progn
5611 (setq have-i nil)
5612 (concat ":" x))
5614 tags ":")
5615 (if have-i "::" ":"))))))
5616 txt)
5618 (defun org-downcase-keep-props (s)
5619 (let ((props (text-properties-at 0 s)))
5620 (setq s (downcase s))
5621 (add-text-properties 0 (length s) props s)
5624 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
5625 (defvar org-agenda-sorting-strategy-selected nil)
5627 (defun org-agenda-add-time-grid-maybe (list ndays todayp)
5628 (catch 'exit
5629 (cond ((not org-agenda-use-time-grid) (throw 'exit list))
5630 ((and todayp (member 'today (car org-agenda-time-grid))))
5631 ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
5632 ((member 'weekly (car org-agenda-time-grid)))
5633 (t (throw 'exit list)))
5634 (let* ((have (delq nil (mapcar
5635 (lambda (x) (get-text-property 1 'time-of-day x))
5636 list)))
5637 (string (nth 1 org-agenda-time-grid))
5638 (gridtimes (nth 2 org-agenda-time-grid))
5639 (req (car org-agenda-time-grid))
5640 (remove (member 'remove-match req))
5641 new time)
5642 (if (and (member 'require-timed req) (not have))
5643 ;; don't show empty grid
5644 (throw 'exit list))
5645 (while (setq time (pop gridtimes))
5646 (unless (and remove (member time have))
5647 (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
5648 (push (org-format-agenda-item
5649 nil string "" nil
5650 (concat (substring time 0 -2) ":" (substring time -2)))
5651 new)
5652 (put-text-property
5653 2 (length (car new)) 'face 'org-time-grid (car new))))
5654 (when (and todayp org-agenda-show-current-time-in-grid)
5655 (push (org-format-agenda-item
5657 org-agenda-current-time-string
5658 "" nil
5659 (format-time-string "%H:%M "))
5660 new)
5661 (put-text-property
5662 2 (length (car new)) 'face 'org-agenda-current-time (car new)))
5664 (if (member 'time-up org-agenda-sorting-strategy-selected)
5665 (append new list)
5666 (append list new)))))
5668 (defun org-compile-prefix-format (key)
5669 "Compile the prefix format into a Lisp form that can be evaluated.
5670 The resulting form is returned and stored in the variable
5671 `org-prefix-format-compiled'."
5672 (setq org-prefix-has-time nil org-prefix-has-tag nil
5673 org-prefix-category-length nil org-prefix-has-effort nil)
5674 (let ((s (cond
5675 ((stringp org-agenda-prefix-format)
5676 org-agenda-prefix-format)
5677 ((assq key org-agenda-prefix-format)
5678 (cdr (assq key org-agenda-prefix-format)))
5679 (t " %-12:c%?-12t% s")))
5680 (start 0)
5681 varform vars var e c f opt)
5682 (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\|(.+)\\)"
5683 s start)
5684 (setq var (or (cdr (assoc (match-string 4 s)
5685 '(("c" . category) ("t" . time) ("s" . extra)
5686 ("i" . category-icon) ("T" . tag) ("e" . effort))))
5687 'eval)
5688 c (or (match-string 3 s) "")
5689 opt (match-beginning 1)
5690 start (1+ (match-beginning 0)))
5691 (if (equal var 'time) (setq org-prefix-has-time t))
5692 (if (equal var 'tag) (setq org-prefix-has-tag t))
5693 (if (equal var 'effort) (setq org-prefix-has-effort t))
5694 (setq f (concat "%" (match-string 2 s) "s"))
5695 (when (equal var 'category)
5696 (setq org-prefix-category-length
5697 (floor (abs (string-to-number (match-string 2 s)))))
5698 (setq org-prefix-category-max-length
5699 (let ((x (match-string 2 s)))
5700 (save-match-data
5701 (if (string-match "\\.[0-9]+" x)
5702 (string-to-number (substring (match-string 0 x) 1)))))))
5703 (if (eq var 'eval)
5704 (setq varform `(format ,f (org-eval ,(read (match-string 4 s)))))
5705 (if opt
5706 (setq varform
5707 `(if (equal "" ,var)
5709 (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
5710 (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var)))))))
5711 (setq s (replace-match "%s" t nil s))
5712 (push varform vars))
5713 (setq vars (nreverse vars))
5714 (setq org-prefix-format-compiled `(format ,s ,@vars))))
5716 (defun org-set-sorting-strategy (key)
5717 (if (symbolp (car org-agenda-sorting-strategy))
5718 ;; the old format
5719 (setq org-agenda-sorting-strategy-selected org-agenda-sorting-strategy)
5720 (setq org-agenda-sorting-strategy-selected
5721 (or (cdr (assq key org-agenda-sorting-strategy))
5722 (cdr (assq 'agenda org-agenda-sorting-strategy))
5723 '(time-up category-keep priority-down)))))
5725 (defun org-get-time-of-day (s &optional string mod24)
5726 "Check string S for a time of day.
5727 If found, return it as a military time number between 0 and 2400.
5728 If not found, return nil.
5729 The optional STRING argument forces conversion into a 5 character wide string
5730 HH:MM."
5731 (save-match-data
5732 (when
5733 (or (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
5734 (string-match "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
5735 (let* ((h (string-to-number (match-string 1 s)))
5736 (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
5737 (ampm (if (match-end 4) (downcase (match-string 4 s))))
5738 (am-p (equal ampm "am"))
5739 (h1 (cond ((not ampm) h)
5740 ((= h 12) (if am-p 0 12))
5741 (t (+ h (if am-p 0 12)))))
5742 (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
5743 (mod h1 24) h1))
5744 (t0 (+ (* 100 h2) m))
5745 (t1 (concat (if (>= h1 24) "+" " ")
5746 (if (and org-agenda-time-leading-zero
5747 (< t0 1000)) "0" "")
5748 (if (< t0 100) "0" "")
5749 (if (< t0 10) "0" "")
5750 (int-to-string t0))))
5751 (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
5753 (defvar org-agenda-before-sorting-filter-function nil
5754 "Function to be applied to agenda items prior to sorting.
5755 Prior to sorting also means just before they are inserted into the agenda.
5757 To aid sorting, you may revisit the original entries and add more text
5758 properties which will later be used by the sorting functions.
5760 The function should take a string argument, an agenda line.
5761 It has access to the text properties in that line, which contain among
5762 other things, the property `org-hd-marker' that points to the entry
5763 where the line comes from. Note that not all lines going into the agenda
5764 have this property, only most.
5766 The function should return the modified string. It is probably best
5767 to ONLY change text properties.
5769 You can also use this function as a filter, by returning nil for lines
5770 you don't want to have in the agenda at all. For this application, you
5771 could bind the variable in the options section of a custom command.")
5773 (defun org-finalize-agenda-entries (list &optional nosort)
5774 "Sort and concatenate the agenda items."
5775 (setq list (mapcar 'org-agenda-highlight-todo list))
5776 (if nosort
5777 list
5778 (when org-agenda-before-sorting-filter-function
5779 (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
5780 (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
5782 (defun org-agenda-highlight-todo (x)
5783 (let ((org-done-keywords org-done-keywords-for-agenda)
5784 (case-fold-search nil)
5786 (if (eq x 'line)
5787 (save-excursion
5788 (beginning-of-line 1)
5789 (setq re (org-get-at-bol 'org-todo-regexp))
5790 (goto-char (or (text-property-any (point-at-bol) (point-at-eol) 'org-heading t) (point)))
5791 (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
5792 (add-text-properties (match-beginning 0) (match-end 1)
5793 (list 'face (org-get-todo-face 1)))
5794 (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
5795 (delete-region (match-beginning 1) (1- (match-end 0)))
5796 (goto-char (match-beginning 1))
5797 (insert (format org-agenda-todo-keyword-format s)))))
5798 (let ((pl (text-property-any 0 (length x) 'org-heading t x)))
5799 (setq re (get-text-property 0 'org-todo-regexp x))
5800 (when (and re
5801 (equal (string-match (concat "\\(\\.*\\)" re "\\( +\\)")
5802 x (or pl 0)) pl))
5803 (add-text-properties
5804 (or (match-end 1) (match-end 0)) (match-end 0)
5805 (list 'face (org-get-todo-face (match-string 2 x)))
5807 (when (match-end 1)
5808 (setq x (concat (substring x 0 (match-end 1))
5809 (format org-agenda-todo-keyword-format
5810 (match-string 2 x))
5811 (org-add-props " " (text-properties-at 0 x))
5812 (substring x (match-end 3)))))))
5813 x)))
5815 (defsubst org-cmp-priority (a b)
5816 "Compare the priorities of string A and B."
5817 (let ((pa (or (get-text-property 1 'priority a) 0))
5818 (pb (or (get-text-property 1 'priority b) 0)))
5819 (cond ((> pa pb) +1)
5820 ((< pa pb) -1)
5821 (t nil))))
5823 (defsubst org-cmp-effort (a b)
5824 "Compare the priorities of string A and B."
5825 (let* ((def (if org-sort-agenda-noeffort-is-high 32767 -1))
5826 (ea (or (get-text-property 1 'effort-minutes a) def))
5827 (eb (or (get-text-property 1 'effort-minutes b) def)))
5828 (cond ((> ea eb) +1)
5829 ((< ea eb) -1)
5830 (t nil))))
5832 (defsubst org-cmp-category (a b)
5833 "Compare the string values of categories of strings A and B."
5834 (let ((ca (or (get-text-property 1 'org-category a) ""))
5835 (cb (or (get-text-property 1 'org-category b) "")))
5836 (cond ((string-lessp ca cb) -1)
5837 ((string-lessp cb ca) +1)
5838 (t nil))))
5840 (defsubst org-cmp-todo-state (a b)
5841 "Compare the todo states of strings A and B."
5842 (let* ((ma (or (get-text-property 1 'org-marker a)
5843 (get-text-property 1 'org-hd-marker a)))
5844 (mb (or (get-text-property 1 'org-marker b)
5845 (get-text-property 1 'org-hd-marker b)))
5846 (fa (and ma (marker-buffer ma)))
5847 (fb (and mb (marker-buffer mb)))
5848 (todo-kwds
5849 (or (and fa (with-current-buffer fa org-todo-keywords-1))
5850 (and fb (with-current-buffer fb org-todo-keywords-1))))
5851 (ta (or (get-text-property 1 'todo-state a) ""))
5852 (tb (or (get-text-property 1 'todo-state b) ""))
5853 (la (- (length (member ta todo-kwds))))
5854 (lb (- (length (member tb todo-kwds))))
5855 (donepa (member ta org-done-keywords-for-agenda))
5856 (donepb (member tb org-done-keywords-for-agenda)))
5857 (cond ((and donepa (not donepb)) -1)
5858 ((and (not donepa) donepb) +1)
5859 ((< la lb) -1)
5860 ((< lb la) +1)
5861 (t nil))))
5863 (defsubst org-cmp-alpha (a b)
5864 "Compare the headlines, alphabetically."
5865 (let* ((pla (text-property-any 0 (length a) 'org-heading t a))
5866 (plb (text-property-any 0 (length b) 'org-heading t b))
5867 (ta (and pla (substring a pla)))
5868 (tb (and plb (substring b plb))))
5869 (when pla
5870 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
5871 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
5872 (setq ta (substring ta (match-end 0))))
5873 (setq ta (downcase ta)))
5874 (when plb
5875 (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
5876 "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
5877 (setq tb (substring tb (match-end 0))))
5878 (setq tb (downcase tb)))
5879 (cond ((not ta) +1)
5880 ((not tb) -1)
5881 ((string-lessp ta tb) -1)
5882 ((string-lessp tb ta) +1)
5883 (t nil))))
5885 (defsubst org-cmp-tag (a b)
5886 "Compare the string values of the first tags of A and B."
5887 (let ((ta (car (last (get-text-property 1 'tags a))))
5888 (tb (car (last (get-text-property 1 'tags b)))))
5889 (cond ((not ta) +1)
5890 ((not tb) -1)
5891 ((string-lessp ta tb) -1)
5892 ((string-lessp tb ta) +1)
5893 (t nil))))
5895 (defsubst org-cmp-time (a b)
5896 "Compare the time-of-day values of strings A and B."
5897 (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
5898 (ta (or (get-text-property 1 'time-of-day a) def))
5899 (tb (or (get-text-property 1 'time-of-day b) def)))
5900 (cond ((< ta tb) -1)
5901 ((< tb ta) +1)
5902 (t nil))))
5904 (defsubst org-cmp-habit-p (a b)
5905 "Compare the todo states of strings A and B."
5906 (let ((ha (get-text-property 1 'org-habit-p a))
5907 (hb (get-text-property 1 'org-habit-p b)))
5908 (cond ((and ha (not hb)) -1)
5909 ((and (not ha) hb) +1)
5910 (t nil))))
5912 (defsubst org-em (x y list) (or (memq x list) (memq y list)))
5914 (defun org-entries-lessp (a b)
5915 "Predicate for sorting agenda entries."
5916 ;; The following variables will be used when the form is evaluated.
5917 ;; So even though the compiler complains, keep them.
5918 (let* ((ss org-agenda-sorting-strategy-selected)
5919 (time-up (and (org-em 'time-up 'time-down ss)
5920 (org-cmp-time a b)))
5921 (time-down (if time-up (- time-up) nil))
5922 (priority-up (and (org-em 'priority-up 'priority-down ss)
5923 (org-cmp-priority a b)))
5924 (priority-down (if priority-up (- priority-up) nil))
5925 (effort-up (and (org-em 'effort-up 'effort-down ss)
5926 (org-cmp-effort a b)))
5927 (effort-down (if effort-up (- effort-up) nil))
5928 (category-up (and (or (org-em 'category-up 'category-down ss)
5929 (memq 'category-keep ss))
5930 (org-cmp-category a b)))
5931 (category-down (if category-up (- category-up) nil))
5932 (category-keep (if category-up +1 nil))
5933 (tag-up (and (org-em 'tag-up 'tag-down ss)
5934 (org-cmp-tag a b)))
5935 (tag-down (if tag-up (- tag-up) nil))
5936 (todo-state-up (and (org-em 'todo-state-up 'todo-state-down ss)
5937 (org-cmp-todo-state a b)))
5938 (todo-state-down (if todo-state-up (- todo-state-up) nil))
5939 (habit-up (and (org-em 'habit-up 'habit-down ss)
5940 (org-cmp-habit-p a b)))
5941 (habit-down (if habit-up (- habit-up) nil))
5942 (alpha-up (and (org-em 'alpha-up 'alpha-down ss)
5943 (org-cmp-alpha a b)))
5944 (alpha-down (if alpha-up (- alpha-up) nil))
5945 (need-user-cmp (org-em 'user-defined-up 'user-defined-down ss))
5946 user-defined-up user-defined-down)
5947 (if (and need-user-cmp org-agenda-cmp-user-defined
5948 (functionp org-agenda-cmp-user-defined))
5949 (setq user-defined-up
5950 (funcall org-agenda-cmp-user-defined a b)
5951 user-defined-down (if user-defined-up (- user-defined-up) nil)))
5952 (cdr (assoc
5953 (eval (cons 'or org-agenda-sorting-strategy-selected))
5954 '((-1 . t) (1 . nil) (nil . nil))))))
5956 ;;; Agenda restriction lock
5958 (defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
5959 "Overlay to mark the headline to which agenda commands are restricted.")
5960 (overlay-put org-agenda-restriction-lock-overlay
5961 'face 'org-agenda-restriction-lock)
5962 (overlay-put org-agenda-restriction-lock-overlay
5963 'help-echo "Agendas are currently limited to this subtree.")
5964 (org-detach-overlay org-agenda-restriction-lock-overlay)
5966 (defun org-agenda-set-restriction-lock (&optional type)
5967 "Set restriction lock for agenda, to current subtree or file.
5968 Restriction will be the file if TYPE is `file', or if type is the
5969 universal prefix '(4), or if the cursor is before the first headline
5970 in the file. Otherwise, restriction will be to the current subtree."
5971 (interactive "P")
5972 (and (equal type '(4)) (setq type 'file))
5973 (setq type (cond
5974 (type type)
5975 ((org-at-heading-p) 'subtree)
5976 ((condition-case nil (org-back-to-heading t) (error nil))
5977 'subtree)
5978 (t 'file)))
5979 (if (eq type 'subtree)
5980 (progn
5981 (setq org-agenda-restrict t)
5982 (setq org-agenda-overriding-restriction 'subtree)
5983 (put 'org-agenda-files 'org-restrict
5984 (list (buffer-file-name (buffer-base-buffer))))
5985 (org-back-to-heading t)
5986 (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
5987 (move-marker org-agenda-restrict-begin (point))
5988 (move-marker org-agenda-restrict-end
5989 (save-excursion (org-end-of-subtree t)))
5990 (message "Locking agenda restriction to subtree"))
5991 (put 'org-agenda-files 'org-restrict
5992 (list (buffer-file-name (buffer-base-buffer))))
5993 (setq org-agenda-restrict nil)
5994 (setq org-agenda-overriding-restriction 'file)
5995 (move-marker org-agenda-restrict-begin nil)
5996 (move-marker org-agenda-restrict-end nil)
5997 (message "Locking agenda restriction to file"))
5998 (setq current-prefix-arg nil)
5999 (org-agenda-maybe-redo))
6001 (defun org-agenda-remove-restriction-lock (&optional noupdate)
6002 "Remove the agenda restriction lock."
6003 (interactive "P")
6004 (org-detach-overlay org-agenda-restriction-lock-overlay)
6005 (org-detach-overlay org-speedbar-restriction-lock-overlay)
6006 (setq org-agenda-overriding-restriction nil)
6007 (setq org-agenda-restrict nil)
6008 (put 'org-agenda-files 'org-restrict nil)
6009 (move-marker org-agenda-restrict-begin nil)
6010 (move-marker org-agenda-restrict-end nil)
6011 (setq current-prefix-arg nil)
6012 (message "Agenda restriction lock removed")
6013 (or noupdate (org-agenda-maybe-redo)))
6015 (defun org-agenda-maybe-redo ()
6016 "If there is any window showing the agenda view, update it."
6017 (let ((w (get-buffer-window org-agenda-buffer-name t))
6018 (w0 (selected-window)))
6019 (when w
6020 (select-window w)
6021 (org-agenda-redo)
6022 (select-window w0)
6023 (if org-agenda-overriding-restriction
6024 (message "Agenda view shifted to new %s restriction"
6025 org-agenda-overriding-restriction)
6026 (message "Agenda restriction lock removed")))))
6028 ;;; Agenda commands
6030 (defun org-agenda-check-type (error &rest types)
6031 "Check if agenda buffer is of allowed type.
6032 If ERROR is non-nil, throw an error, otherwise just return nil."
6033 (if (memq org-agenda-type types)
6035 (if error
6036 (error "Not allowed in %s-type agenda buffers" org-agenda-type)
6037 nil)))
6039 (defun org-agenda-quit ()
6040 "Exit agenda by removing the window or the buffer."
6041 (interactive)
6042 (if org-agenda-columns-active
6043 (org-columns-quit)
6044 (let ((buf (current-buffer)))
6045 (if (eq org-agenda-window-setup 'other-frame)
6046 (progn
6047 (kill-buffer buf)
6048 (org-agenda-reset-markers)
6049 (org-columns-remove-overlays)
6050 (setq org-agenda-archives-mode nil)
6051 (delete-frame))
6052 (and (not (eq org-agenda-window-setup 'current-window))
6053 (not (one-window-p))
6054 (delete-window))
6055 (kill-buffer buf)
6056 (org-agenda-reset-markers)
6057 (org-columns-remove-overlays)
6058 (setq org-agenda-archives-mode nil)))
6059 ;; Maybe restore the pre-agenda window configuration.
6060 (and org-agenda-restore-windows-after-quit
6061 (not (eq org-agenda-window-setup 'other-frame))
6062 org-pre-agenda-window-conf
6063 (set-window-configuration org-pre-agenda-window-conf))))
6065 (defun org-agenda-exit ()
6066 "Exit agenda by removing the window or the buffer.
6067 Also kill all Org-mode buffers which have been loaded by `org-agenda'.
6068 Org-mode buffers visited directly by the user will not be touched."
6069 (interactive)
6070 (org-release-buffers org-agenda-new-buffers)
6071 (setq org-agenda-new-buffers nil)
6072 (org-agenda-quit))
6074 (defun org-agenda-execute (arg)
6075 "Execute another agenda command, keeping same window.
6076 So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
6077 in the agenda."
6078 (interactive "P")
6079 (let ((org-agenda-window-setup 'current-window))
6080 (org-agenda arg)))
6082 (defun org-agenda-redo ()
6083 "Rebuild Agenda.
6084 When this is the global TODO list, a prefix argument will be interpreted."
6085 (interactive)
6086 (let* ((org-agenda-keep-modes t)
6087 (filter org-agenda-filter)
6088 (preset (get 'org-agenda-filter :preset-filter))
6089 (org-agenda-filter-while-redo (or filter preset))
6090 (cols org-agenda-columns-active)
6091 (line (org-current-line))
6092 (window-line (- line (org-current-line (window-start))))
6093 (lprops (get 'org-agenda-redo-command 'org-lprops)))
6094 (put 'org-agenda-filter :preset-filter nil)
6095 (and cols (org-columns-quit))
6096 (message "Rebuilding agenda buffer...")
6097 (org-let lprops '(eval org-agenda-redo-command))
6098 (setq org-agenda-undo-list nil
6099 org-agenda-pending-undo-list nil)
6100 (message "Rebuilding agenda buffer...done")
6101 (put 'org-agenda-filter :preset-filter preset)
6102 (and (or filter preset) (org-agenda-filter-apply filter))
6103 (and cols (org-called-interactively-p 'any) (org-agenda-columns))
6104 (org-goto-line line)
6105 (recenter window-line)))
6108 (defvar org-global-tags-completion-table nil)
6109 (defvar org-agenda-filter-form nil)
6110 (defun org-agenda-filter-by-tag (strip &optional char narrow)
6111 "Keep only those lines in the agenda buffer that have a specific tag.
6112 The tag is selected with its fast selection letter, as configured.
6113 With prefix argument STRIP, remove all lines that do have the tag.
6114 A lisp caller can specify CHAR. NARROW means that the new tag should be
6115 used to narrow the search - the interactive user can also press `-' or `+'
6116 to switch to narrowing."
6117 (interactive "P")
6118 (let* ((alist org-tag-alist-for-agenda)
6119 (tag-chars (mapconcat
6120 (lambda (x) (if (and (not (symbolp (car x)))
6121 (cdr x))
6122 (char-to-string (cdr x))
6123 ""))
6124 alist ""))
6125 (efforts (org-split-string
6126 (or (cdr (assoc (concat org-effort-property "_ALL")
6127 org-global-properties))
6128 "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"
6129 "")))
6130 (effort-op org-agenda-filter-effort-default-operator)
6131 (effort-prompt "")
6132 (inhibit-read-only t)
6133 (current org-agenda-filter)
6134 maybe-refresh a n tag)
6135 (unless char
6136 (message
6137 "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
6138 (if narrow "Narrow" "Filter") tag-chars
6139 (if org-agenda-auto-exclude-function "[RET], " ""))
6140 (setq char (read-char)))
6141 (when (member char '(?+ ?-))
6142 ;; Narrowing down
6143 (cond ((equal char ?-) (setq strip t narrow t))
6144 ((equal char ?+) (setq strip nil narrow t)))
6145 (message
6146 "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
6147 (setq char (read-char)))
6148 (when (member char '(?< ?> ?= ??))
6149 ;; An effort operator
6150 (setq effort-op (char-to-string char))
6151 (setq alist nil) ; to make sure it will be interpreted as effort.
6152 (unless (equal char ??)
6153 (loop for i from 0 to 9 do
6154 (setq effort-prompt
6155 (concat
6156 effort-prompt " ["
6157 (if (= i 9) "0" (int-to-string (1+ i)))
6158 "]" (nth i efforts))))
6159 (message "Effort%s: %s " effort-op effort-prompt)
6160 (setq char (read-char))
6161 (when (or (< char ?0) (> char ?9))
6162 (error "Need 1-9,0 to select effort" ))))
6163 (when (equal char ?\t)
6164 (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
6165 (org-set-local 'org-global-tags-completion-table
6166 (org-global-tags-completion-table)))
6167 (let ((completion-ignore-case t))
6168 (setq tag (org-icompleting-read
6169 "Tag: " org-global-tags-completion-table))))
6170 (cond
6171 ((equal char ?\r)
6172 (org-agenda-filter-by-tag-show-all)
6173 (when org-agenda-auto-exclude-function
6174 (setq org-agenda-filter '())
6175 (dolist (tag (org-agenda-get-represented-tags))
6176 (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
6177 (if modifier
6178 (push modifier org-agenda-filter))))
6179 (if (not (null org-agenda-filter))
6180 (org-agenda-filter-apply org-agenda-filter)))
6181 (setq maybe-refresh t))
6182 ((equal char ?/)
6183 (org-agenda-filter-by-tag-show-all)
6184 (when (get 'org-agenda-filter :preset-filter)
6185 (org-agenda-filter-apply org-agenda-filter))
6186 (setq maybe-refresh t))
6187 ((or (equal char ?\ )
6188 (setq a (rassoc char alist))
6189 (and (>= char ?0) (<= char ?9)
6190 (setq n (if (= char ?0) 9 (- char ?0 1))
6191 tag (concat effort-op (nth n efforts))
6192 a (cons tag nil)))
6193 (and (= char ??)
6194 (setq tag "?eff")
6195 a (cons tag nil))
6196 (and tag (setq a (cons tag nil))))
6197 (org-agenda-filter-by-tag-show-all)
6198 (setq tag (car a))
6199 (setq org-agenda-filter
6200 (cons (concat (if strip "-" "+") tag)
6201 (if narrow current nil)))
6202 (org-agenda-filter-apply org-agenda-filter)
6203 (setq maybe-refresh t))
6204 (t (error "Invalid tag selection character %c" char)))
6205 (when (and maybe-refresh
6206 (eq org-agenda-clockreport-mode 'with-filter))
6207 (org-agenda-redo))))
6209 (defun org-agenda-get-represented-tags ()
6210 "Get a list of all tags currently represented in the agenda."
6211 (let (p tags)
6212 (save-excursion
6213 (goto-char (point-min))
6214 (while (setq p (next-single-property-change (point) 'tags))
6215 (goto-char p)
6216 (mapc (lambda (x) (add-to-list 'tags x))
6217 (get-text-property (point) 'tags))))
6218 tags))
6220 (defun org-agenda-filter-by-tag-refine (strip &optional char)
6221 "Refine the current filter. See `org-agenda-filter-by-tag."
6222 (interactive "P")
6223 (org-agenda-filter-by-tag strip char 'refine))
6225 (defun org-agenda-filter-make-matcher ()
6226 "Create the form that tests a line for the agenda filter."
6227 (let (f f1)
6228 (dolist (x (append (get 'org-agenda-filter :preset-filter)
6229 org-agenda-filter))
6230 (if (member x '("-" "+"))
6231 (setq f1 (if (equal x "-") 'tags '(not tags)))
6232 (if (string-match "[<=>?]" x)
6233 (setq f1 (org-agenda-filter-effort-form x))
6234 (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
6235 (if (equal (string-to-char x) ?-)
6236 (setq f1 (list 'not f1))))
6237 (push f1 f))
6238 (cons 'and (nreverse f))))
6240 (defun org-agenda-filter-effort-form (e)
6241 "Return the form to compare the effort of the current line with what E says.
6242 E looks like \"+<2:25\"."
6243 (let (op)
6244 (setq e (substring e 1))
6245 (setq op (string-to-char e) e (substring e 1))
6246 (setq op (cond ((equal op ?<) '<=)
6247 ((equal op ?>) '>=)
6248 ((equal op ??) op)
6249 (t '=)))
6250 (list 'org-agenda-compare-effort (list 'quote op)
6251 (org-duration-string-to-minutes e))))
6253 (defun org-agenda-compare-effort (op value)
6254 "Compare the effort of the current line with VALUE, using OP.
6255 If the line does not have an effort defined, return nil."
6256 (let ((eff (org-get-at-bol 'effort-minutes)))
6257 (if (equal op ??)
6258 (not eff)
6259 (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
6260 value))))
6262 (defun org-agenda-filter-apply (filter)
6263 "Set FILTER as the new agenda filter and apply it."
6264 (let (tags)
6265 (setq org-agenda-filter filter
6266 org-agenda-filter-form (org-agenda-filter-make-matcher))
6267 (org-agenda-set-mode-name)
6268 (save-excursion
6269 (goto-char (point-min))
6270 (while (not (eobp))
6271 (if (org-get-at-bol 'org-marker)
6272 (progn
6273 (setq tags (org-get-at-bol 'tags)) ; used in eval
6274 (if (not (eval org-agenda-filter-form))
6275 (org-agenda-filter-by-tag-hide-line))
6276 (beginning-of-line 2))
6277 (beginning-of-line 2))))
6278 (if (get-char-property (point) 'invisible)
6279 (org-agenda-previous-line))))
6281 (defun org-agenda-filter-by-tag-hide-line ()
6282 (let (ov)
6283 (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
6284 (point-at-eol)))
6285 (overlay-put ov 'invisible t)
6286 (overlay-put ov 'type 'tags-filter)
6287 (push ov org-agenda-filter-overlays)))
6289 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
6290 (setq pos (or pos (point)))
6291 (save-excursion
6292 (dolist (ov (overlays-at pos))
6293 (when (and (overlay-get ov 'invisible)
6294 (eq (overlay-get ov 'type) 'tags-filter))
6295 (goto-char pos)
6296 (if (< (overlay-start ov) (point-at-eol))
6297 (move-overlay ov (point-at-eol)
6298 (overlay-end ov)))))))
6300 (defun org-agenda-filter-by-tag-show-all ()
6301 (mapc 'delete-overlay org-agenda-filter-overlays)
6302 (setq org-agenda-filter-overlays nil)
6303 (setq org-agenda-filter nil)
6304 (setq org-agenda-filter-form nil)
6305 (org-agenda-set-mode-name))
6307 (defun org-agenda-manipulate-query-add ()
6308 "Manipulate the query by adding a search term with positive selection.
6309 Positive selection means the term must be matched for selection of an entry."
6310 (interactive)
6311 (org-agenda-manipulate-query ?\[))
6312 (defun org-agenda-manipulate-query-subtract ()
6313 "Manipulate the query by adding a search term with negative selection.
6314 Negative selection means term must not be matched for selection of an entry."
6315 (interactive)
6316 (org-agenda-manipulate-query ?\]))
6317 (defun org-agenda-manipulate-query-add-re ()
6318 "Manipulate the query by adding a search regexp with positive selection.
6319 Positive selection means the regexp must match for selection of an entry."
6320 (interactive)
6321 (org-agenda-manipulate-query ?\{))
6322 (defun org-agenda-manipulate-query-subtract-re ()
6323 "Manipulate the query by adding a search regexp with negative selection.
6324 Negative selection means regexp must not match for selection of an entry."
6325 (interactive)
6326 (org-agenda-manipulate-query ?\}))
6327 (defun org-agenda-manipulate-query (char)
6328 (cond
6329 ((memq org-agenda-type '(timeline agenda))
6330 (let ((org-agenda-include-inactive-timestamps t))
6331 (org-agenda-redo))
6332 (message "Display now includes inactive timestamps as well"))
6333 ((eq org-agenda-type 'search)
6334 (org-add-to-string
6335 'org-agenda-query-string
6336 (if org-agenda-last-search-view-search-was-boolean
6337 (cdr (assoc char '((?\[ . " +") (?\] . " -")
6338 (?\{ . " +{}") (?\} . " -{}"))))
6339 " "))
6340 (setq org-agenda-redo-command
6341 (list 'org-search-view
6342 org-todo-only
6343 org-agenda-query-string
6344 (+ (length org-agenda-query-string)
6345 (if (member char '(?\{ ?\})) 0 1))))
6346 (set-register org-agenda-query-register org-agenda-query-string)
6347 (org-agenda-redo))
6348 (t (error "Cannot manipulate query for %s-type agenda buffers"
6349 org-agenda-type))))
6351 (defun org-add-to-string (var string)
6352 (set var (concat (symbol-value var) string)))
6354 (defun org-agenda-goto-date (date)
6355 "Jump to DATE in agenda."
6356 (interactive (list (let ((org-read-date-prefer-future
6357 (eval org-agenda-jump-prefer-future)))
6358 (org-read-date))))
6359 (org-agenda-list nil date))
6361 (defun org-agenda-goto-today ()
6362 "Go to today."
6363 (interactive)
6364 (org-agenda-check-type t 'timeline 'agenda)
6365 (let ((tdpos (text-property-any (point-min) (point-max) 'org-today t)))
6366 (cond
6367 (tdpos (goto-char tdpos))
6368 ((eq org-agenda-type 'agenda)
6369 (let* ((sd (org-agenda-compute-starting-span
6370 (org-today) (or org-agenda-current-span org-agenda-ndays org-agenda-span)))
6371 (org-agenda-overriding-arguments org-agenda-last-arguments))
6372 (setf (nth 1 org-agenda-overriding-arguments) sd)
6373 (org-agenda-redo)
6374 (org-agenda-find-same-or-today-or-agenda)))
6375 (t (error "Cannot find today")))))
6377 (defun org-agenda-find-same-or-today-or-agenda (&optional cnt)
6378 (goto-char
6379 (or (and cnt (text-property-any (point-min) (point-max) 'org-day-cnt cnt))
6380 (text-property-any (point-min) (point-max) 'org-today t)
6381 (text-property-any (point-min) (point-max) 'org-agenda-type 'agenda)
6382 (point-min))))
6384 (defun org-agenda-later (arg)
6385 "Go forward in time by thee current span.
6386 With prefix ARG, go forward that many times the current span."
6387 (interactive "p")
6388 (org-agenda-check-type t 'agenda)
6389 (let* ((span org-agenda-current-span)
6390 (sd org-starting-day)
6391 (greg (calendar-gregorian-from-absolute sd))
6392 (cnt (org-get-at-bol 'org-day-cnt))
6393 greg2)
6394 (cond
6395 ((eq span 'day)
6396 (setq sd (+ arg sd)))
6397 ((eq span 'week)
6398 (setq sd (+ (* 7 arg) sd)))
6399 ((eq span 'month)
6400 (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
6401 sd (calendar-absolute-from-gregorian greg2))
6402 (setcar greg2 (1+ (car greg2))))
6403 ((eq span 'year)
6404 (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
6405 sd (calendar-absolute-from-gregorian greg2))
6406 (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
6408 (setq sd (+ (* span arg) sd))))
6409 (let ((org-agenda-overriding-arguments
6410 (list (car org-agenda-last-arguments) sd span t)))
6411 (org-agenda-redo)
6412 (org-agenda-find-same-or-today-or-agenda cnt))))
6414 (defun org-agenda-earlier (arg)
6415 "Go backward in time by the current span.
6416 With prefix ARG, go backward that many times the current span."
6417 (interactive "p")
6418 (org-agenda-later (- arg)))
6420 (defun org-agenda-view-mode-dispatch ()
6421 "Call one of the view mode commands."
6422 (interactive)
6423 (message "View: [d]ay [w]eek [m]onth [y]ear [SPC]reset [q]uit/abort
6424 time[G]rid [[]inactive [f]ollow [l]og [L]og-all [c]lockcheck
6425 [a]rch-trees [A]rch-files clock[R]eport include[D]iary
6426 [E]ntryText")
6427 (let ((a (read-char-exclusive)))
6428 (case a
6429 (?\ (call-interactively 'org-agenda-reset-view))
6430 (?d (call-interactively 'org-agenda-day-view))
6431 (?w (call-interactively 'org-agenda-week-view))
6432 (?m (call-interactively 'org-agenda-month-view))
6433 (?y (call-interactively 'org-agenda-year-view))
6434 (?l (call-interactively 'org-agenda-log-mode))
6435 (?L (org-agenda-log-mode '(4)))
6436 (?c (org-agenda-log-mode 'clockcheck))
6437 ((?F ?f) (call-interactively 'org-agenda-follow-mode))
6438 (?a (call-interactively 'org-agenda-archives-mode))
6439 (?A (org-agenda-archives-mode 'files))
6440 ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
6441 ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
6442 (?G (call-interactively 'org-agenda-toggle-time-grid))
6443 (?D (call-interactively 'org-agenda-toggle-diary))
6444 (?\! (call-interactively 'org-agenda-toggle-deadlines))
6445 (?\[ (let ((org-agenda-include-inactive-timestamps t))
6446 (org-agenda-check-type t 'timeline 'agenda)
6447 (org-agenda-redo))
6448 (message "Display now includes inactive timestamps as well"))
6449 (?q (message "Abort"))
6450 (otherwise (error "Invalid key" )))))
6452 (defun org-agenda-reset-view ()
6453 "Switch to default view for agenda."
6454 (interactive)
6455 (org-agenda-change-time-span (or org-agenda-ndays org-agenda-span)))
6456 (defun org-agenda-day-view (&optional day-of-year)
6457 "Switch to daily view for agenda.
6458 With argument DAY-OF-YEAR, switch to that day of the year."
6459 (interactive "P")
6460 (org-agenda-change-time-span 'day day-of-year))
6461 (defun org-agenda-week-view (&optional iso-week)
6462 "Switch to daily view for agenda.
6463 With argument ISO-WEEK, switch to the corresponding ISO week.
6464 If ISO-WEEK has more then 2 digits, only the last two encode the
6465 week. Any digits before this encode a year. So 200712 means
6466 week 12 of year 2007. Years in the range 1938-2037 can also be
6467 written as 2-digit years."
6468 (interactive "P")
6469 (org-agenda-change-time-span 'week iso-week))
6470 (defun org-agenda-month-view (&optional month)
6471 "Switch to monthly view for agenda.
6472 With argument MONTH, switch to that month."
6473 (interactive "P")
6474 (org-agenda-change-time-span 'month month))
6475 (defun org-agenda-year-view (&optional year)
6476 "Switch to yearly view for agenda.
6477 With argument YEAR, switch to that year.
6478 If MONTH has more then 2 digits, only the last two encode the
6479 month. Any digits before this encode a year. So 200712 means
6480 December year 2007. Years in the range 1938-2037 can also be
6481 written as 2-digit years."
6482 (interactive "P")
6483 (when year
6484 (setq year (org-small-year-to-year year)))
6485 (if (y-or-n-p "Are you sure you want to compute the agenda for an entire year? ")
6486 (org-agenda-change-time-span 'year year)
6487 (error "Abort")))
6489 (defun org-agenda-change-time-span (span &optional n)
6490 "Change the agenda view to SPAN.
6491 SPAN may be `day', `week', `month', `year'."
6492 (org-agenda-check-type t 'agenda)
6493 (if (and (not n) (equal org-agenda-current-span span))
6494 (error "Viewing span is already \"%s\"" span))
6495 (let* ((sd (or (org-get-at-bol 'day)
6496 org-starting-day))
6497 (sd (org-agenda-compute-starting-span sd span n))
6498 (org-agenda-overriding-arguments
6499 (or org-agenda-overriding-arguments
6500 (list (car org-agenda-last-arguments) sd span t))))
6501 (org-agenda-redo)
6502 (org-agenda-find-same-or-today-or-agenda))
6503 (org-agenda-set-mode-name)
6504 (message "Switched to %s view" span))
6506 (defun org-agenda-compute-starting-span (sd span &optional n)
6507 "Compute starting date for agenda.
6508 SPAN may be `day', `week', `month', `year'. The return value
6509 is a cons cell with the starting date and the number of days,
6510 so that the date SD will be in that range."
6511 (let* ((greg (calendar-gregorian-from-absolute sd))
6512 (dg (nth 1 greg))
6513 (mg (car greg))
6514 (yg (nth 2 greg)))
6515 (cond
6516 ((eq span 'day)
6517 (when n
6518 (setq sd (+ (calendar-absolute-from-gregorian
6519 (list mg 1 yg))
6520 n -1))))
6521 ((eq span 'week)
6522 (let* ((nt (calendar-day-of-week
6523 (calendar-gregorian-from-absolute sd)))
6524 (d (if org-agenda-start-on-weekday
6525 (- nt org-agenda-start-on-weekday)
6528 (setq sd (- sd (+ (if (< d 0) 7 0) d)))
6529 (when n
6530 (require 'cal-iso)
6531 (when (> n 99)
6532 (setq y1 (org-small-year-to-year (/ n 100))
6533 n (mod n 100)))
6534 (setq sd
6535 (calendar-absolute-from-iso
6536 (list n 1
6537 (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
6538 ((eq span 'month)
6539 (let (y1)
6540 (when (and n (> n 99))
6541 (setq y1 (org-small-year-to-year (/ n 100))
6542 n (mod n 100)))
6543 (setq sd (calendar-absolute-from-gregorian
6544 (list (or n mg) 1 (or y1 yg))))))
6545 ((eq span 'year)
6546 (setq sd (calendar-absolute-from-gregorian
6547 (list 1 1 (or n yg))))))
6548 sd))
6550 (defun org-agenda-next-date-line (&optional arg)
6551 "Jump to the next line indicating a date in agenda buffer."
6552 (interactive "p")
6553 (org-agenda-check-type t 'agenda 'timeline)
6554 (beginning-of-line 1)
6555 ;; This does not work if user makes date format that starts with a blank
6556 (if (looking-at "^\\S-") (forward-char 1))
6557 (if (not (re-search-forward "^\\S-" nil t arg))
6558 (progn
6559 (backward-char 1)
6560 (error "No next date after this line in this buffer")))
6561 (goto-char (match-beginning 0)))
6563 (defun org-agenda-previous-date-line (&optional arg)
6564 "Jump to the previous line indicating a date in agenda buffer."
6565 (interactive "p")
6566 (org-agenda-check-type t 'agenda 'timeline)
6567 (beginning-of-line 1)
6568 (if (not (re-search-backward "^\\S-" nil t arg))
6569 (error "No previous date before this line in this buffer")))
6571 ;; Initialize the highlight
6572 (defvar org-hl (make-overlay 1 1))
6573 (overlay-put org-hl 'face 'highlight)
6575 (defun org-highlight (begin end &optional buffer)
6576 "Highlight a region with overlay."
6577 (move-overlay org-hl begin end (or buffer (current-buffer))))
6579 (defun org-unhighlight ()
6580 "Detach overlay INDEX."
6581 (org-detach-overlay org-hl))
6583 ;; FIXME this is currently not used.
6584 (defun org-highlight-until-next-command (beg end &optional buffer)
6585 "Move the highlight overlay to BEG/END, remove it before the next command."
6586 (org-highlight beg end buffer)
6587 (add-hook 'pre-command-hook 'org-unhighlight-once))
6588 (defun org-unhighlight-once ()
6589 "Remove the highlight from its position, and this function from the hook."
6590 (remove-hook 'pre-command-hook 'org-unhighlight-once)
6591 (org-unhighlight))
6593 (defun org-agenda-follow-mode ()
6594 "Toggle follow mode in an agenda buffer."
6595 (interactive)
6596 (setq org-agenda-follow-mode (not org-agenda-follow-mode))
6597 (org-agenda-set-mode-name)
6598 (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
6599 (org-agenda-show))
6600 (message "Follow mode is %s"
6601 (if org-agenda-follow-mode "on" "off")))
6603 (defun org-agenda-entry-text-mode (&optional arg)
6604 "Toggle entry text mode in an agenda buffer."
6605 (interactive "P")
6606 (setq org-agenda-entry-text-mode (or (integerp arg)
6607 (not org-agenda-entry-text-mode)))
6608 (org-agenda-entry-text-hide)
6609 (and org-agenda-entry-text-mode
6610 (let ((org-agenda-entry-text-maxlines
6611 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6612 (org-agenda-entry-text-show)))
6613 (org-agenda-set-mode-name)
6614 (message "Entry text mode is %s. Maximum number of lines is %d"
6615 (if org-agenda-entry-text-mode "on" "off")
6616 (if (integerp arg) arg org-agenda-entry-text-maxlines)))
6618 (defun org-agenda-clockreport-mode (&optional with-filter)
6619 "Toggle clocktable mode in an agenda buffer.
6620 With prefix arg WITH-FILTER, make the clocktable respect the current
6621 agenda filter."
6622 (interactive "P")
6623 (org-agenda-check-type t 'agenda)
6624 (if with-filter
6625 (setq org-agenda-clockreport-mode 'with-filter)
6626 (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
6627 (org-agenda-set-mode-name)
6628 (org-agenda-redo)
6629 (message "Clocktable mode is %s"
6630 (if org-agenda-clockreport-mode "on" "off")))
6632 (defun org-agenda-log-mode (&optional special)
6633 "Toggle log mode in an agenda buffer.
6634 With argument SPECIAL, show all possible log items, not only the ones
6635 configured in `org-agenda-log-mode-items'.
6636 With a double `C-u' prefix arg, show *only* log items, nothing else."
6637 (interactive "P")
6638 (org-agenda-check-type t 'agenda 'timeline)
6639 (setq org-agenda-show-log
6640 (cond
6641 ((equal special '(16)) 'only)
6642 ((eq special 'clockcheck)
6643 (if (eq org-agenda-show-log 'clockcheck)
6644 nil 'clockcheck))
6645 (special '(closed clock state))
6646 (t (not org-agenda-show-log))))
6647 (org-agenda-set-mode-name)
6648 (org-agenda-redo)
6649 (message "Log mode is %s"
6650 (if org-agenda-show-log "on" "off")))
6652 (defun org-agenda-archives-mode (&optional with-files)
6653 "Toggle inclusion of items in trees marked with :ARCHIVE:.
6654 When called with a prefix argument, include all archive files as well."
6655 (interactive "P")
6656 (setq org-agenda-archives-mode
6657 (if with-files t (if org-agenda-archives-mode nil 'trees)))
6658 (org-agenda-set-mode-name)
6659 (org-agenda-redo)
6660 (message
6661 "%s"
6662 (cond
6663 ((eq org-agenda-archives-mode nil)
6664 "No archives are included")
6665 ((eq org-agenda-archives-mode 'trees)
6666 (format "Trees with :%s: tag are included" org-archive-tag))
6667 ((eq org-agenda-archives-mode t)
6668 (format "Trees with :%s: tag and all active archive files are included"
6669 org-archive-tag)))))
6671 (defun org-agenda-toggle-diary ()
6672 "Toggle diary inclusion in an agenda buffer."
6673 (interactive)
6674 (org-agenda-check-type t 'agenda)
6675 (setq org-agenda-include-diary (not org-agenda-include-diary))
6676 (org-agenda-redo)
6677 (org-agenda-set-mode-name)
6678 (message "Diary inclusion turned %s"
6679 (if org-agenda-include-diary "on" "off")))
6681 (defun org-agenda-toggle-deadlines ()
6682 "Toggle inclusion of entries with a deadline in an agenda buffer."
6683 (interactive)
6684 (org-agenda-check-type t 'agenda)
6685 (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
6686 (org-agenda-redo)
6687 (org-agenda-set-mode-name)
6688 (message "Deadlines inclusion turned %s"
6689 (if org-agenda-include-deadlines "on" "off")))
6691 (defun org-agenda-toggle-time-grid ()
6692 "Toggle time grid in an agenda buffer."
6693 (interactive)
6694 (org-agenda-check-type t 'agenda)
6695 (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
6696 (org-agenda-redo)
6697 (org-agenda-set-mode-name)
6698 (message "Time-grid turned %s"
6699 (if org-agenda-use-time-grid "on" "off")))
6701 (defun org-agenda-set-mode-name ()
6702 "Set the mode name to indicate all the small mode settings."
6703 (setq mode-name
6704 (list "Org-Agenda"
6705 (if (get 'org-agenda-files 'org-restrict) " []" "")
6707 '(:eval (org-agenda-span-name org-agenda-current-span))
6708 (if org-agenda-follow-mode " Follow" "")
6709 (if org-agenda-entry-text-mode " ETxt" "")
6710 (if org-agenda-include-diary " Diary" "")
6711 (if org-agenda-include-deadlines " Ddl" "")
6712 (if org-agenda-use-time-grid " Grid" "")
6713 (if (and (boundp 'org-habit-show-habits)
6714 org-habit-show-habits) " Habit" "")
6715 (cond
6716 ((consp org-agenda-show-log) " LogAll")
6717 ((eq org-agenda-show-log 'clockcheck) " ClkCk")
6718 (org-agenda-show-log " Log")
6719 (t ""))
6720 (if (or org-agenda-filter (get 'org-agenda-filter
6721 :preset-filter))
6722 (concat " {" (mapconcat
6723 'identity
6724 (append (get 'org-agenda-filter
6725 :preset-filter)
6726 org-agenda-filter) "") "}")
6728 (if org-agenda-archives-mode
6729 (if (eq org-agenda-archives-mode t)
6730 " Archives"
6731 (format " :%s:" org-archive-tag))
6733 (if org-agenda-clockreport-mode
6734 (if (eq org-agenda-clockreport-mode 'with-filter)
6735 " Clock{}" " Clock")
6736 "")))
6737 (force-mode-line-update))
6739 (defun org-agenda-post-command-hook ()
6740 (setq org-agenda-type
6741 (or (get-text-property (point) 'org-agenda-type)
6742 (get-text-property (max (point-min) (1- (point)))
6743 'org-agenda-type))))
6745 (defun org-agenda-next-line ()
6746 "Move cursor to the next line, and show if follow mode is active."
6747 (interactive)
6748 (call-interactively 'next-line)
6749 (org-agenda-do-context-action))
6751 (defun org-agenda-previous-line ()
6752 "Move cursor to the previous line, and show if follow-mode is active."
6753 (interactive)
6754 (call-interactively 'previous-line)
6755 (org-agenda-do-context-action))
6757 (defun org-agenda-do-context-action ()
6758 "Show outline path and, maybe, follow mode window."
6759 (let ((m (org-get-at-bol 'org-marker)))
6760 (if (and org-agenda-follow-mode m)
6761 (org-agenda-show))
6762 (if (and m org-agenda-show-outline-path)
6763 (org-with-point-at m
6764 (org-display-outline-path t)))))
6766 (defun org-agenda-show-priority ()
6767 "Show the priority of the current item.
6768 This priority is composed of the main priority given with the [#A] cookies,
6769 and by additional input from the age of a schedules or deadline entry."
6770 (interactive)
6771 (let* ((pri (org-get-at-bol 'priority)))
6772 (message "Priority is %d" (if pri pri -1000))))
6774 (defun org-agenda-show-tags ()
6775 "Show the tags applicable to the current item."
6776 (interactive)
6777 (let* ((tags (org-get-at-bol 'tags)))
6778 (if tags
6779 (message "Tags are :%s:"
6780 (org-no-properties (mapconcat 'identity tags ":")))
6781 (message "No tags associated with this line"))))
6783 (defun org-agenda-goto (&optional highlight)
6784 "Go to the Org-mode file which contains the item at point."
6785 (interactive)
6786 (let* ((marker (or (org-get-at-bol 'org-marker)
6787 (org-agenda-error)))
6788 (buffer (marker-buffer marker))
6789 (pos (marker-position marker)))
6790 (switch-to-buffer-other-window buffer)
6791 (widen)
6792 (push-mark)
6793 (goto-char pos)
6794 (when (org-mode-p)
6795 (org-show-context 'agenda)
6796 (save-excursion
6797 (and (outline-next-heading)
6798 (org-flag-heading nil)))) ; show the next heading
6799 (when (outline-invisible-p)
6800 (show-entry)) ; display invisible text
6801 (recenter (/ (window-height) 2))
6802 (run-hooks 'org-agenda-after-show-hook)
6803 (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
6805 (defvar org-agenda-after-show-hook nil
6806 "Normal hook run after an item has been shown from the agenda.
6807 Point is in the buffer where the item originated.")
6809 (defun org-agenda-kill ()
6810 "Kill the entry or subtree belonging to the current agenda entry."
6811 (interactive)
6812 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6813 (let* ((marker (or (org-get-at-bol 'org-marker)
6814 (org-agenda-error)))
6815 (buffer (marker-buffer marker))
6816 (pos (marker-position marker))
6817 (type (org-get-at-bol 'type))
6818 dbeg dend (n 0) conf)
6819 (org-with-remote-undo buffer
6820 (with-current-buffer buffer
6821 (save-excursion
6822 (goto-char pos)
6823 (if (and (org-mode-p) (not (member type '("sexp"))))
6824 (setq dbeg (progn (org-back-to-heading t) (point))
6825 dend (org-end-of-subtree t t))
6826 (setq dbeg (point-at-bol)
6827 dend (min (point-max) (1+ (point-at-eol)))))
6828 (goto-char dbeg)
6829 (while (re-search-forward "^[ \t]*\\S-" dend t) (setq n (1+ n)))))
6830 (setq conf (or (eq t org-agenda-confirm-kill)
6831 (and (numberp org-agenda-confirm-kill)
6832 (> n org-agenda-confirm-kill))))
6833 (and conf
6834 (not (y-or-n-p
6835 (format "Delete entry with %d lines in buffer \"%s\"? "
6836 n (buffer-name buffer))))
6837 (error "Abort"))
6838 (org-remove-subtree-entries-from-agenda buffer dbeg dend)
6839 (with-current-buffer buffer (delete-region dbeg dend))
6840 (message "Agenda item and source killed"))))
6842 (defvar org-archive-default-command)
6843 (defun org-agenda-archive-default ()
6844 "Archive the entry or subtree belonging to the current agenda entry."
6845 (interactive)
6846 (require 'org-archive)
6847 (org-agenda-archive-with org-archive-default-command))
6849 (defun org-agenda-archive-default-with-confirmation ()
6850 "Archive the entry or subtree belonging to the current agenda entry."
6851 (interactive)
6852 (require 'org-archive)
6853 (org-agenda-archive-with org-archive-default-command 'confirm))
6855 (defun org-agenda-archive ()
6856 "Archive the entry or subtree belonging to the current agenda entry."
6857 (interactive)
6858 (org-agenda-archive-with 'org-archive-subtree))
6860 (defun org-agenda-archive-to-archive-sibling ()
6861 "Move the entry to the archive sibling."
6862 (interactive)
6863 (org-agenda-archive-with 'org-archive-to-archive-sibling))
6865 (defun org-agenda-archive-with (cmd &optional confirm)
6866 "Move the entry to the archive sibling."
6867 (interactive)
6868 (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
6869 (let* ((marker (or (org-get-at-bol 'org-marker)
6870 (org-agenda-error)))
6871 (buffer (marker-buffer marker))
6872 (pos (marker-position marker)))
6873 (org-with-remote-undo buffer
6874 (with-current-buffer buffer
6875 (if (org-mode-p)
6876 (if (and confirm
6877 (not (y-or-n-p "Archive this subtree or entry? ")))
6878 (error "Abort")
6879 (save-excursion
6880 (goto-char pos)
6881 (org-remove-subtree-entries-from-agenda)
6882 (org-back-to-heading t)
6883 (funcall cmd)))
6884 (error "Archiving works only in Org-mode files"))))))
6886 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
6887 "Remove all lines in the agenda that correspond to a given subtree.
6888 The subtree is the one in buffer BUF, starting at BEG and ending at END.
6889 If this information is not given, the function uses the tree at point."
6890 (let ((buf (or buf (current-buffer))) m p)
6891 (save-excursion
6892 (unless (and beg end)
6893 (org-back-to-heading t)
6894 (setq beg (point))
6895 (org-end-of-subtree t)
6896 (setq end (point)))
6897 (set-buffer (get-buffer org-agenda-buffer-name))
6898 (save-excursion
6899 (goto-char (point-max))
6900 (beginning-of-line 1)
6901 (while (not (bobp))
6902 (when (and (setq m (org-get-at-bol 'org-marker))
6903 (equal buf (marker-buffer m))
6904 (setq p (marker-position m))
6905 (>= p beg)
6906 (< p end))
6907 (let ((inhibit-read-only t))
6908 (delete-region (point-at-bol) (1+ (point-at-eol)))))
6909 (beginning-of-line 0))))))
6911 (defun org-agenda-refile (&optional goto rfloc no-update)
6912 "Refile the item at point."
6913 (interactive "P")
6914 (if (equal goto '(16))
6915 (org-refile-goto-last-stored)
6916 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6917 (org-agenda-error)))
6918 (buffer (marker-buffer marker))
6919 (pos (marker-position marker))
6920 (rfloc (or rfloc
6921 (org-refile-get-location
6922 (if goto "Goto" "Refile to") buffer
6923 org-refile-allow-creating-parent-nodes))))
6924 (with-current-buffer buffer
6925 (save-excursion
6926 (save-restriction
6927 (widen)
6928 (goto-char marker)
6929 (org-remove-subtree-entries-from-agenda)
6930 (org-refile goto buffer rfloc)))))
6931 (unless no-update (org-agenda-redo))))
6933 (defun org-agenda-open-link (&optional arg)
6934 "Follow the link in the current line, if any.
6935 This looks for a link in the displayed line in the agenda. It also looks
6936 at the text of the entry itself."
6937 (interactive "P")
6938 (let* ((marker (or (org-get-at-bol 'org-hd-marker)
6939 (org-get-at-bol 'org-marker)))
6940 (buffer (and marker (marker-buffer marker)))
6941 (prefix (buffer-substring
6942 (point-at-bol) (point-at-eol))))
6943 (cond
6944 (buffer
6945 (with-current-buffer buffer
6946 (save-excursion
6947 (save-restriction
6948 (widen)
6949 (goto-char marker)
6950 (org-offer-links-in-entry arg prefix)))))
6951 ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
6952 (save-excursion
6953 (beginning-of-line 1)
6954 (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
6955 (org-open-link-from-string (match-string 1)))
6956 (t (error "No link to open here")))))
6958 (defun org-agenda-copy-local-variable (var)
6959 "Get a variable from a referenced buffer and install it here."
6960 (let ((m (org-get-at-bol 'org-marker)))
6961 (when (and m (buffer-live-p (marker-buffer m)))
6962 (org-set-local var (with-current-buffer (marker-buffer m)
6963 (symbol-value var))))))
6965 (defun org-agenda-switch-to (&optional delete-other-windows)
6966 "Go to the Org-mode file which contains the item at point."
6967 (interactive)
6968 (if (and org-return-follows-link
6969 (not (org-get-at-bol 'org-marker))
6970 (org-in-regexp org-bracket-link-regexp))
6971 (org-open-link-from-string (match-string 0))
6972 (let* ((marker (or (org-get-at-bol 'org-marker)
6973 (org-agenda-error)))
6974 (buffer (marker-buffer marker))
6975 (pos (marker-position marker)))
6976 (org-pop-to-buffer-same-window buffer)
6977 (and delete-other-windows (delete-other-windows))
6978 (widen)
6979 (goto-char pos)
6980 (when (org-mode-p)
6981 (org-show-context 'agenda)
6982 (save-excursion
6983 (and (outline-next-heading)
6984 (org-flag-heading nil))) ; show the next heading
6985 (when (outline-invisible-p)
6986 (show-entry)))))) ; display invisible text
6988 (defun org-agenda-goto-mouse (ev)
6989 "Go to the Org-mode file which contains the item at the mouse click."
6990 (interactive "e")
6991 (mouse-set-point ev)
6992 (org-agenda-goto))
6994 (defun org-agenda-show (&optional full-entry)
6995 "Display the Org-mode file which contains the item at point.
6996 With prefix argument FULL-ENTRY, make the entire entry visible
6997 if it was hidden in the outline."
6998 (interactive "P")
6999 (let ((win (selected-window)))
7000 (if full-entry
7001 (let ((org-show-entry-below t))
7002 (org-agenda-goto t))
7003 (org-agenda-goto t))
7004 (select-window win)))
7006 (defvar org-agenda-show-window nil)
7007 (defun org-agenda-show-and-scroll-up ()
7008 "Display the Org-mode file which contains the item at point.
7009 When called repeatedly, scroll the window that is displaying the buffer."
7010 (interactive)
7011 (let ((win (selected-window)))
7012 (if (and (window-live-p org-agenda-show-window)
7013 (eq this-command last-command))
7014 (progn
7015 (select-window org-agenda-show-window)
7016 (ignore-errors (scroll-up)))
7017 (org-agenda-goto t)
7018 (show-subtree)
7019 (setq org-agenda-show-window (selected-window)))
7020 (select-window win)))
7022 (defun org-agenda-show-scroll-down ()
7023 "Scroll down the window showing the agenda."
7024 (interactive)
7025 (let ((win (selected-window)))
7026 (when (window-live-p org-agenda-show-window)
7027 (select-window org-agenda-show-window)
7028 (ignore-errors (scroll-down))
7029 (select-window win))))
7031 (defun org-agenda-show-1 (&optional more)
7032 "Display the Org-mode file which contains the item at point.
7033 The prefix arg selects the amount of information to display:
7035 0 hide the subtree
7036 1 just show the entry according to defaults.
7037 2 show the children view
7038 3 show the subtree view
7039 4 show the entire subtree and any LOGBOOK drawers
7040 5 show the entire subtree and any drawers
7041 With prefix argument FULL-ENTRY, make the entire entry visible
7042 if it was hidden in the outline."
7043 (interactive "p")
7044 (let ((win (selected-window)))
7045 (org-agenda-goto t)
7046 (org-recenter-heading 1)
7047 (cond
7048 ((= more 0)
7049 (hide-subtree)
7050 (save-excursion
7051 (org-back-to-heading)
7052 (run-hook-with-args 'org-cycle-hook 'folded))
7053 (message "Remote: FOLDED"))
7054 ((and (org-called-interactively-p 'any) (= more 1))
7055 (message "Remote: show with default settings"))
7056 ((= more 2)
7057 (show-entry)
7058 (show-children)
7059 (save-excursion
7060 (org-back-to-heading)
7061 (run-hook-with-args 'org-cycle-hook 'children))
7062 (message "Remote: CHILDREN"))
7063 ((= more 3)
7064 (show-subtree)
7065 (save-excursion
7066 (org-back-to-heading)
7067 (run-hook-with-args 'org-cycle-hook 'subtree))
7068 (message "Remote: SUBTREE"))
7069 ((= more 4)
7070 (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
7071 (org-drawer-regexp
7072 (concat "^[ \t]*:\\("
7073 (mapconcat 'regexp-quote org-drawers "\\|")
7074 "\\):[ \t]*$")))
7075 (show-subtree)
7076 (save-excursion
7077 (org-back-to-heading)
7078 (org-cycle-hide-drawers 'subtree)))
7079 (message "Remote: SUBTREE AND LOGBOOK"))
7080 ((> more 4)
7081 (show-subtree)
7082 (message "Remote: SUBTREE AND ALL DRAWERS")))
7083 (select-window win)))
7085 (defun org-recenter-heading (n)
7086 (save-excursion
7087 (org-back-to-heading)
7088 (recenter n)))
7090 (defvar org-agenda-cycle-counter nil)
7091 (defun org-agenda-cycle-show (&optional n)
7092 "Show the current entry in another window, with default settings.
7093 Default settings are taken from `org-show-hierarchy-above' and siblings.
7094 When use repeatedly in immediate succession, the remote entry will cycle
7095 through visibility
7097 children -> subtree -> folded
7099 When called with a numeric prefix arg, that arg will be passed through to
7100 `org-agenda-show-1'. For the interpretation of that argument, see the
7101 docstring of `org-agenda-show-1'."
7102 (interactive "P")
7103 (if (integerp n)
7104 (setq org-agenda-cycle-counter n)
7105 (if (not (eq last-command this-command))
7106 (setq org-agenda-cycle-counter 1)
7107 (if (equal org-agenda-cycle-counter 0)
7108 (setq org-agenda-cycle-counter 2)
7109 (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
7110 (if (> org-agenda-cycle-counter 3)
7111 (setq org-agenda-cycle-counter 0)))))
7112 (org-agenda-show-1 org-agenda-cycle-counter))
7114 (defun org-agenda-recenter (arg)
7115 "Display the Org-mode file which contains the item at point and recenter."
7116 (interactive "P")
7117 (let ((win (selected-window)))
7118 (org-agenda-goto t)
7119 (recenter arg)
7120 (select-window win)))
7122 (defun org-agenda-show-mouse (ev)
7123 "Display the Org-mode file which contains the item at the mouse click."
7124 (interactive "e")
7125 (mouse-set-point ev)
7126 (org-agenda-show))
7128 (defun org-agenda-check-no-diary ()
7129 "Check if the entry is a diary link and abort if yes."
7130 (if (org-get-at-bol 'org-agenda-diary-link)
7131 (org-agenda-error)))
7133 (defun org-agenda-error ()
7134 (error "Command not allowed in this line"))
7136 (defun org-agenda-tree-to-indirect-buffer ()
7137 "Show the subtree corresponding to the current entry in an indirect buffer.
7138 This calls the command `org-tree-to-indirect-buffer' from the original
7139 Org-mode buffer.
7140 With numerical prefix arg ARG, go up to this level and then take that tree.
7141 With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
7142 use the dedicated frame)."
7143 (interactive)
7144 (org-agenda-check-no-diary)
7145 (let* ((marker (or (org-get-at-bol 'org-marker)
7146 (org-agenda-error)))
7147 (buffer (marker-buffer marker))
7148 (pos (marker-position marker)))
7149 (with-current-buffer buffer
7150 (save-excursion
7151 (goto-char pos)
7152 (call-interactively 'org-tree-to-indirect-buffer)))))
7154 (defvar org-last-heading-marker (make-marker)
7155 "Marker pointing to the headline that last changed its TODO state
7156 by a remote command from the agenda.")
7158 (defun org-agenda-todo-nextset ()
7159 "Switch TODO entry to next sequence."
7160 (interactive)
7161 (org-agenda-todo 'nextset))
7163 (defun org-agenda-todo-previousset ()
7164 "Switch TODO entry to previous sequence."
7165 (interactive)
7166 (org-agenda-todo 'previousset))
7168 (defun org-agenda-todo (&optional arg)
7169 "Cycle TODO state of line at point, also in Org-mode file.
7170 This changes the line at point, all other lines in the agenda referring to
7171 the same tree node, and the headline of the tree node in the Org-mode file."
7172 (interactive "P")
7173 (org-agenda-check-no-diary)
7174 (let* ((col (current-column))
7175 (marker (or (org-get-at-bol 'org-marker)
7176 (org-agenda-error)))
7177 (buffer (marker-buffer marker))
7178 (pos (marker-position marker))
7179 (hdmarker (org-get-at-bol 'org-hd-marker))
7180 (todayp (org-agenda-todayp (org-get-at-bol 'day)))
7181 (inhibit-read-only t)
7182 org-agenda-headline-snapshot-before-repeat newhead just-one)
7183 (org-with-remote-undo buffer
7184 (with-current-buffer buffer
7185 (widen)
7186 (goto-char pos)
7187 (org-show-context 'agenda)
7188 (save-excursion
7189 (and (outline-next-heading)
7190 (org-flag-heading nil))) ; show the next heading
7191 (let ((current-prefix-arg arg))
7192 (call-interactively 'org-todo))
7193 (and (bolp) (forward-char 1))
7194 (setq newhead (org-get-heading))
7195 (when (and (org-bound-and-true-p
7196 org-agenda-headline-snapshot-before-repeat)
7197 (not (equal org-agenda-headline-snapshot-before-repeat
7198 newhead))
7199 todayp)
7200 (setq newhead org-agenda-headline-snapshot-before-repeat
7201 just-one t))
7202 (save-excursion
7203 (org-back-to-heading)
7204 (move-marker org-last-heading-marker (point))))
7205 (beginning-of-line 1)
7206 (save-excursion
7207 (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
7208 (org-move-to-column col))))
7210 (defun org-agenda-add-note (&optional arg)
7211 "Add a time-stamped note to the entry at point."
7212 (interactive "P")
7213 (org-agenda-check-no-diary)
7214 (let* ((marker (or (org-get-at-bol 'org-marker)
7215 (org-agenda-error)))
7216 (buffer (marker-buffer marker))
7217 (pos (marker-position marker))
7218 (hdmarker (org-get-at-bol 'org-hd-marker))
7219 (inhibit-read-only t))
7220 (with-current-buffer buffer
7221 (widen)
7222 (goto-char pos)
7223 (org-show-context 'agenda)
7224 (save-excursion
7225 (and (outline-next-heading)
7226 (org-flag-heading nil))) ; show the next heading
7227 (org-add-note))))
7229 (defun org-agenda-change-all-lines (newhead hdmarker
7230 &optional fixface just-this)
7231 "Change all lines in the agenda buffer which match HDMARKER.
7232 The new content of the line will be NEWHEAD (as modified by
7233 `org-format-agenda-item'). HDMARKER is checked with
7234 `equal' against all `org-hd-marker' text properties in the file.
7235 If FIXFACE is non-nil, the face of each item is modified according to
7236 the new TODO state.
7237 If JUST-THIS is non-nil, change just the current line, not all.
7238 If FORCE-TAGS is non nil, the car of it returns the new tags."
7239 (let* ((inhibit-read-only t)
7240 (line (org-current-line))
7241 (thetags (with-current-buffer (marker-buffer hdmarker)
7242 (save-excursion (save-restriction (widen)
7243 (goto-char hdmarker)
7244 (org-get-tags-at)))))
7245 props m pl undone-face done-face finish new dotime cat tags)
7246 (save-excursion
7247 (goto-char (point-max))
7248 (beginning-of-line 1)
7249 (while (not finish)
7250 (setq finish (bobp))
7251 (when (and (setq m (org-get-at-bol 'org-hd-marker))
7252 (or (not just-this) (= (org-current-line) line))
7253 (equal m hdmarker))
7254 (setq props (text-properties-at (point))
7255 dotime (org-get-at-bol 'dotime)
7256 cat (org-get-at-bol 'org-category)
7257 tags thetags
7258 new (org-format-agenda-item (org-get-at-bol 'extra)
7259 newhead cat tags dotime)
7260 pl (text-property-any (point-at-bol) (point-at-eol) 'org-heading t)
7261 undone-face (org-get-at-bol 'undone-face)
7262 done-face (org-get-at-bol 'done-face))
7263 (beginning-of-line 1)
7264 (cond
7265 ((equal new "")
7266 (and (looking-at ".*\n?") (replace-match "")))
7267 ((looking-at ".*")
7268 (replace-match new t t)
7269 (beginning-of-line 1)
7270 (add-text-properties (point-at-bol) (point-at-eol) props)
7271 (when fixface
7272 (add-text-properties
7273 (point-at-bol) (point-at-eol)
7274 (list 'face
7275 (if org-last-todo-state-is-todo
7276 undone-face done-face))))
7277 (org-agenda-highlight-todo 'line)
7278 (beginning-of-line 1))
7279 (t (error "Line update did not work"))))
7280 (beginning-of-line 0)))
7281 (org-finalize-agenda)))
7283 (defun org-agenda-align-tags (&optional line)
7284 "Align all tags in agenda items to `org-agenda-tags-column'."
7285 (let ((inhibit-read-only t) l c)
7286 (save-excursion
7287 (goto-char (if line (point-at-bol) (point-min)))
7288 (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
7289 (if line (point-at-eol) nil) t)
7290 (add-text-properties
7291 (match-beginning 2) (match-end 2)
7292 (list 'face (delq nil (let ((prop (get-text-property
7293 (match-beginning 2) 'face)))
7294 (or (listp prop) (setq prop (list prop)))
7295 (if (memq 'org-tag prop)
7296 prop
7297 (cons 'org-tag prop))))))
7298 (setq l (- (match-end 2) (match-beginning 2))
7299 c (if (< org-agenda-tags-column 0)
7300 (- (abs org-agenda-tags-column) l)
7301 org-agenda-tags-column))
7302 (delete-region (match-beginning 1) (match-end 1))
7303 (goto-char (match-beginning 1))
7304 (insert (org-add-props
7305 (make-string (max 1 (- c (current-column))) ?\ )
7306 (plist-put (copy-sequence (text-properties-at (point)))
7307 'face nil))))
7308 (goto-char (point-min))
7309 (org-font-lock-add-tag-faces (point-max)))))
7311 (defun org-agenda-priority-up ()
7312 "Increase the priority of line at point, also in Org-mode file."
7313 (interactive)
7314 (org-agenda-priority 'up))
7316 (defun org-agenda-priority-down ()
7317 "Decrease the priority of line at point, also in Org-mode file."
7318 (interactive)
7319 (org-agenda-priority 'down))
7321 (defun org-agenda-priority (&optional force-direction)
7322 "Set the priority of line at point, also in Org-mode file.
7323 This changes the line at point, all other lines in the agenda referring to
7324 the same tree node, and the headline of the tree node in the Org-mode file."
7325 (interactive)
7326 (unless org-enable-priority-commands
7327 (error "Priority commands are disabled"))
7328 (org-agenda-check-no-diary)
7329 (let* ((marker (or (org-get-at-bol 'org-marker)
7330 (org-agenda-error)))
7331 (hdmarker (org-get-at-bol 'org-hd-marker))
7332 (buffer (marker-buffer hdmarker))
7333 (pos (marker-position hdmarker))
7334 (inhibit-read-only t)
7335 newhead)
7336 (org-with-remote-undo buffer
7337 (with-current-buffer buffer
7338 (widen)
7339 (goto-char pos)
7340 (org-show-context 'agenda)
7341 (save-excursion
7342 (and (outline-next-heading)
7343 (org-flag-heading nil))) ; show the next heading
7344 (funcall 'org-priority force-direction)
7345 (end-of-line 1)
7346 (setq newhead (org-get-heading)))
7347 (org-agenda-change-all-lines newhead hdmarker)
7348 (beginning-of-line 1))))
7350 ;; FIXME: should fix the tags property of the agenda line.
7351 (defun org-agenda-set-tags (&optional tag onoff)
7352 "Set tags for the current headline."
7353 (interactive)
7354 (org-agenda-check-no-diary)
7355 (if (and (org-region-active-p) (org-called-interactively-p 'any))
7356 (call-interactively 'org-change-tag-in-region)
7357 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7358 (org-agenda-error)))
7359 (buffer (marker-buffer hdmarker))
7360 (pos (marker-position hdmarker))
7361 (inhibit-read-only t)
7362 newhead)
7363 (org-with-remote-undo buffer
7364 (with-current-buffer buffer
7365 (widen)
7366 (goto-char pos)
7367 (save-excursion
7368 (org-show-context 'agenda))
7369 (save-excursion
7370 (and (outline-next-heading)
7371 (org-flag-heading nil))) ; show the next heading
7372 (goto-char pos)
7373 (if tag
7374 (org-toggle-tag tag onoff)
7375 (call-interactively 'org-set-tags))
7376 (end-of-line 1)
7377 (setq newhead (org-get-heading)))
7378 (org-agenda-change-all-lines newhead hdmarker)
7379 (beginning-of-line 1)))))
7381 (defun org-agenda-set-property ()
7382 "Set a property for the current headline."
7383 (interactive)
7384 (org-agenda-check-no-diary)
7385 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7386 (org-agenda-error)))
7387 (buffer (marker-buffer hdmarker))
7388 (pos (marker-position hdmarker))
7389 (inhibit-read-only t)
7390 newhead)
7391 (org-with-remote-undo buffer
7392 (with-current-buffer buffer
7393 (widen)
7394 (goto-char pos)
7395 (save-excursion
7396 (org-show-context 'agenda))
7397 (save-excursion
7398 (and (outline-next-heading)
7399 (org-flag-heading nil))) ; show the next heading
7400 (goto-char pos)
7401 (call-interactively 'org-set-property)))))
7403 (defun org-agenda-set-effort ()
7404 "Set the effort property for the current headline."
7405 (interactive)
7406 (org-agenda-check-no-diary)
7407 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7408 (org-agenda-error)))
7409 (buffer (marker-buffer hdmarker))
7410 (pos (marker-position hdmarker))
7411 (inhibit-read-only t)
7412 newhead)
7413 (org-with-remote-undo buffer
7414 (with-current-buffer buffer
7415 (widen)
7416 (goto-char pos)
7417 (save-excursion
7418 (org-show-context 'agenda))
7419 (save-excursion
7420 (and (outline-next-heading)
7421 (org-flag-heading nil))) ; show the next heading
7422 (goto-char pos)
7423 (call-interactively 'org-set-effort)
7424 (end-of-line 1)))))
7426 (defun org-agenda-toggle-archive-tag ()
7427 "Toggle the archive tag for the current entry."
7428 (interactive)
7429 (org-agenda-check-no-diary)
7430 (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
7431 (org-agenda-error)))
7432 (buffer (marker-buffer hdmarker))
7433 (pos (marker-position hdmarker))
7434 (inhibit-read-only t)
7435 newhead)
7436 (org-with-remote-undo buffer
7437 (with-current-buffer buffer
7438 (widen)
7439 (goto-char pos)
7440 (org-show-context 'agenda)
7441 (save-excursion
7442 (and (outline-next-heading)
7443 (org-flag-heading nil))) ; show the next heading
7444 (call-interactively 'org-toggle-archive-tag)
7445 (end-of-line 1)
7446 (setq newhead (org-get-heading)))
7447 (org-agenda-change-all-lines newhead hdmarker)
7448 (beginning-of-line 1))))
7450 (defun org-agenda-do-date-later (arg)
7451 (interactive "P")
7452 (cond
7453 ((or (equal arg '(16))
7454 (memq last-command
7455 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7456 (setq this-command 'org-agenda-date-later-minutes)
7457 (org-agenda-date-later-minutes 1))
7458 ((or (equal arg '(4))
7459 (memq last-command
7460 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7461 (setq this-command 'org-agenda-date-later-hours)
7462 (org-agenda-date-later-hours 1))
7464 (org-agenda-date-later (prefix-numeric-value arg)))))
7466 (defun org-agenda-do-date-earlier (arg)
7467 (interactive "P")
7468 (cond
7469 ((or (equal arg '(16))
7470 (memq last-command
7471 '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
7472 (setq this-command 'org-agenda-date-earlier-minutes)
7473 (org-agenda-date-earlier-minutes 1))
7474 ((or (equal arg '(4))
7475 (memq last-command
7476 '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
7477 (setq this-command 'org-agenda-date-earlier-hours)
7478 (org-agenda-date-earlier-hours 1))
7480 (org-agenda-date-earlier (prefix-numeric-value arg)))))
7482 (defun org-agenda-date-later (arg &optional what)
7483 "Change the date of this item to ARG day(s) later."
7484 (interactive "p")
7485 (org-agenda-check-type t 'agenda 'timeline)
7486 (org-agenda-check-no-diary)
7487 (let* ((marker (or (org-get-at-bol 'org-marker)
7488 (org-agenda-error)))
7489 (buffer (marker-buffer marker))
7490 (pos (marker-position marker)))
7491 (org-with-remote-undo buffer
7492 (with-current-buffer buffer
7493 (widen)
7494 (goto-char pos)
7495 (if (not (org-at-timestamp-p))
7496 (error "Cannot find time stamp"))
7497 (org-timestamp-change arg (or what 'day)))
7498 (org-agenda-show-new-time marker org-last-changed-timestamp))
7499 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7501 (defun org-agenda-date-earlier (arg &optional what)
7502 "Change the date of this item to ARG day(s) earlier."
7503 (interactive "p")
7504 (org-agenda-date-later (- arg) what))
7506 (defun org-agenda-date-later-minutes (arg)
7507 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7508 (interactive "p")
7509 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7510 (org-agenda-date-later arg 'minute))
7512 (defun org-agenda-date-earlier-minutes (arg)
7513 "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
7514 (interactive "p")
7515 (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
7516 (org-agenda-date-earlier arg 'minute))
7518 (defun org-agenda-date-later-hours (arg)
7519 "Change the time of this item, in hour steps."
7520 (interactive "p")
7521 (org-agenda-date-later arg 'hour))
7523 (defun org-agenda-date-earlier-hours (arg)
7524 "Change the time of this item, in hour steps."
7525 (interactive "p")
7526 (org-agenda-date-earlier arg 'hour))
7528 (defun org-agenda-show-new-time (marker stamp &optional prefix)
7529 "Show new date stamp via text properties."
7530 ;; We use text properties to make this undoable
7531 (let ((inhibit-read-only t)
7532 (buffer-invisibility-spec))
7533 (setq stamp (concat " " prefix " => " stamp))
7534 (save-excursion
7535 (goto-char (point-max))
7536 (while (not (bobp))
7537 (when (equal marker (org-get-at-bol 'org-marker))
7538 (org-move-to-column (- (window-width) (length stamp)) t)
7539 (org-agenda-fix-tags-filter-overlays-at (point))
7540 (if (featurep 'xemacs)
7541 ;; Use `duplicable' property to trigger undo recording
7542 (let ((ex (make-extent nil nil))
7543 (gl (make-glyph stamp)))
7544 (set-glyph-face gl 'secondary-selection)
7545 (set-extent-properties
7546 ex (list 'invisible t 'end-glyph gl 'duplicable t))
7547 (insert-extent ex (1- (point)) (point-at-eol)))
7548 (add-text-properties
7549 (1- (point)) (point-at-eol)
7550 (list 'display (org-add-props stamp nil
7551 'face 'secondary-selection))))
7552 (beginning-of-line 1))
7553 (beginning-of-line 0)))))
7555 (defun org-agenda-date-prompt (arg)
7556 "Change the date of this item. Date is prompted for, with default today.
7557 The prefix ARG is passed to the `org-time-stamp' command and can therefore
7558 be used to request time specification in the time stamp."
7559 (interactive "P")
7560 (org-agenda-check-type t 'agenda 'timeline)
7561 (org-agenda-check-no-diary)
7562 (let* ((marker (or (org-get-at-bol 'org-marker)
7563 (org-agenda-error)))
7564 (buffer (marker-buffer marker))
7565 (pos (marker-position marker)))
7566 (org-with-remote-undo buffer
7567 (with-current-buffer buffer
7568 (widen)
7569 (goto-char pos)
7570 (if (not (org-at-timestamp-p t))
7571 (error "Cannot find time stamp"))
7572 (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
7573 (org-agenda-show-new-time marker org-last-changed-timestamp))
7574 (message "Time stamp changed to %s" org-last-changed-timestamp)))
7576 (defun org-agenda-schedule (arg &optional time)
7577 "Schedule the item at point.
7578 ARG is passed through to `org-schedule'."
7579 (interactive "P")
7580 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7581 (org-agenda-check-no-diary)
7582 (let* ((marker (or (org-get-at-bol 'org-marker)
7583 (org-agenda-error)))
7584 (type (marker-insertion-type marker))
7585 (buffer (marker-buffer marker))
7586 (pos (marker-position marker))
7587 (org-insert-labeled-timestamps-at-point nil)
7589 (set-marker-insertion-type marker t)
7590 (org-with-remote-undo buffer
7591 (with-current-buffer buffer
7592 (widen)
7593 (goto-char pos)
7594 (setq ts (org-schedule arg time)))
7595 (org-agenda-show-new-time marker ts "S"))
7596 (message "Item scheduled for %s" ts)))
7598 (defun org-agenda-deadline (arg &optional time)
7599 "Schedule the item at point.
7600 ARG is passed through to `org-deadline'."
7601 (interactive "P")
7602 (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
7603 (org-agenda-check-no-diary)
7604 (let* ((marker (or (org-get-at-bol 'org-marker)
7605 (org-agenda-error)))
7606 (buffer (marker-buffer marker))
7607 (pos (marker-position marker))
7608 (org-insert-labeled-timestamps-at-point nil)
7610 (org-with-remote-undo buffer
7611 (with-current-buffer buffer
7612 (widen)
7613 (goto-char pos)
7614 (setq ts (org-deadline arg time)))
7615 (org-agenda-show-new-time marker ts "D"))
7616 (message "Deadline for this item set to %s" ts)))
7618 (defun org-agenda-action ()
7619 "Select entry for agenda action, or execute an agenda action.
7620 This command prompts for another letter. Valid inputs are:
7622 m Mark the entry at point for an agenda action
7623 s Schedule the marked entry to the date at the cursor
7624 d Set the deadline of the marked entry to the date at the cursor
7625 r Call `org-remember' with cursor date as the default date
7626 c Call `org-capture' with cursor date as the default date
7627 SPC Show marked entry in other window
7628 TAB Visit marked entry in other window
7630 The cursor may be at a date in the calendar, or in the Org agenda."
7631 (interactive)
7632 (let (ans)
7633 (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
7634 (setq ans (read-char-exclusive))
7635 (cond
7636 ((equal ans ?m)
7637 ;; Mark this entry
7638 (if (eq major-mode 'org-agenda-mode)
7639 (let ((m (or (org-get-at-bol 'org-hd-marker)
7640 (org-get-at-bol 'org-marker))))
7641 (if m
7642 (progn
7643 (move-marker org-agenda-action-marker
7644 (marker-position m) (marker-buffer m))
7645 (message "Entry marked for action; press `k' at desired date in agenda or calendar"))
7646 (error "Don't know which entry to mark")))
7647 (error "This command works only in the agenda")))
7648 ((equal ans ?s)
7649 (org-agenda-do-action '(org-schedule nil org-overriding-default-time)))
7650 ((equal ans ?d)
7651 (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
7652 ((equal ans ?r)
7653 (org-agenda-do-action '(org-remember) t))
7654 ((equal ans ?c)
7655 (org-agenda-do-action '(org-capture) t))
7656 ((equal ans ?\ )
7657 (let ((cw (selected-window)))
7658 (org-switch-to-buffer-other-window
7659 (marker-buffer org-agenda-action-marker))
7660 (goto-char org-agenda-action-marker)
7661 (org-show-context 'agenda)
7662 (select-window cw)))
7663 ((equal ans ?\C-i)
7664 (org-switch-to-buffer-other-window
7665 (marker-buffer org-agenda-action-marker))
7666 (goto-char org-agenda-action-marker)
7667 (org-show-context 'agenda))
7668 (t (error "Invalid agenda action %c" ans)))))
7670 (defun org-agenda-do-action (form &optional current-buffer)
7671 "Evaluate FORM at the entry pointed to by `org-agenda-action-marker'."
7672 (let ((org-overriding-default-time (org-get-cursor-date)))
7673 (if current-buffer
7674 (eval form)
7675 (if (not (marker-buffer org-agenda-action-marker))
7676 (error "No entry has been selected for agenda action")
7677 (with-current-buffer (marker-buffer org-agenda-action-marker)
7678 (save-excursion
7679 (save-restriction
7680 (widen)
7681 (goto-char org-agenda-action-marker)
7682 (eval form))))))))
7684 (defun org-agenda-clock-in (&optional arg)
7685 "Start the clock on the currently selected item."
7686 (interactive "P")
7687 (org-agenda-check-no-diary)
7688 (if (equal arg '(4))
7689 (org-clock-in arg)
7690 (let* ((marker (or (org-get-at-bol 'org-marker)
7691 (org-agenda-error)))
7692 (hdmarker (or (org-get-at-bol 'org-hd-marker)
7693 marker))
7694 (pos (marker-position marker))
7695 newhead)
7696 (org-with-remote-undo (marker-buffer marker)
7697 (with-current-buffer (marker-buffer marker)
7698 (widen)
7699 (goto-char pos)
7700 (org-show-context 'agenda)
7701 (org-show-entry)
7702 (org-cycle-hide-drawers 'children)
7703 (org-clock-in arg)
7704 (setq newhead (org-get-heading)))
7705 (org-agenda-change-all-lines newhead hdmarker)))))
7707 (defun org-agenda-clock-out ()
7708 "Stop the currently running clock."
7709 (interactive)
7710 (unless (marker-buffer org-clock-marker)
7711 (error "No running clock"))
7712 (let ((marker (make-marker)) newhead)
7713 (org-with-remote-undo (marker-buffer org-clock-marker)
7714 (with-current-buffer (marker-buffer org-clock-marker)
7715 (save-excursion
7716 (save-restriction
7717 (widen)
7718 (goto-char org-clock-marker)
7719 (org-back-to-heading t)
7720 (move-marker marker (point))
7721 (org-clock-out)
7722 (setq newhead (org-get-heading))))))
7723 (org-agenda-change-all-lines newhead marker)
7724 (move-marker marker nil)))
7726 (defun org-agenda-clock-cancel (&optional arg)
7727 "Cancel the currently running clock."
7728 (interactive "P")
7729 (unless (marker-buffer org-clock-marker)
7730 (error "No running clock"))
7731 (org-with-remote-undo (marker-buffer org-clock-marker)
7732 (org-clock-cancel)))
7734 (defun org-agenda-clock-goto ()
7735 "Jump to the currently clocked in task within the agenda.
7736 If the currently clocked in task is not listed in the agenda
7737 buffer, display it in another window."
7738 (interactive)
7739 (let (pos)
7740 (mapc (lambda (o)
7741 (if (eq (overlay-get o 'type) 'org-agenda-clocking)
7742 (setq pos (overlay-start o))))
7743 (overlays-in (point-min) (point-max)))
7744 (cond (pos (goto-char pos))
7745 ;; If the currently clocked entry is not in the agenda
7746 ;; buffer, we visit it in another window:
7747 (org-clock-current-task
7748 (org-switch-to-buffer-other-window (org-clock-goto)))
7749 (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
7751 (defun org-agenda-diary-entry-in-org-file ()
7752 "Make a diary entry in the file `org-agenda-diary-file'."
7753 (let (d1 d2 char (text "") dp1 dp2)
7754 (if (equal (buffer-name) "*Calendar*")
7755 (setq d1 (calendar-cursor-to-date t)
7756 d2 (car calendar-mark-ring))
7757 (setq dp1 (get-text-property (point-at-bol) 'day))
7758 (unless dp1 (error "No date defined in current line"))
7759 (setq d1 (calendar-gregorian-from-absolute dp1)
7760 d2 (and (ignore-errors (mark))
7761 (save-excursion
7762 (goto-char (mark))
7763 (setq dp2 (get-text-property (point-at-bol) 'day)))
7764 (calendar-gregorian-from-absolute dp2))))
7765 (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
7766 (setq char (read-char-exclusive))
7767 (cond
7768 ((equal char ?d)
7769 (setq text (read-string "Day entry: "))
7770 (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
7771 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7772 ((equal char ?a)
7773 (setq d1 (list (car d1) (nth 1 d1)
7774 (read-number (format "Reference year [%d]: " (nth 2 d1))
7775 (nth 2 d1))))
7776 (setq text (read-string "Anniversary (use %d to show years): "))
7777 (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
7778 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7779 ((equal char ?b)
7780 (setq text (read-string "Block entry: "))
7781 (unless (and d1 d2 (not (equal d1 d2)))
7782 (error "No block of days selected"))
7783 (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
7784 (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
7785 ((equal char ?j)
7786 (org-switch-to-buffer-other-window
7787 (find-file-noselect org-agenda-diary-file))
7788 (require 'org-datetree)
7789 (org-datetree-find-date-create d1)
7790 (org-reveal t))
7791 (t (error "Invalid selection character `%c'" char)))))
7793 (defcustom org-agenda-insert-diary-strategy 'date-tree
7794 "Where in `org-agenda-diary-file' should new entries be added?
7795 Valid values:
7797 date-tree in the date tree, as child of the date
7798 top-level as top-level entries at the end of the file."
7799 :group 'org-agenda
7800 :type '(choice
7801 (const :tag "in a date tree" date-tree)
7802 (const :tag "as top level at end of file" top-level)))
7804 (defcustom org-agenda-insert-diary-extract-time nil
7805 "Non-nil means extract any time specification from the diary entry."
7806 :group 'org-agenda
7807 :type 'boolean)
7809 (defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
7810 "Add a diary entry with TYPE to `org-agenda-diary-file'.
7811 If TEXT is not empty, it will become the headline of the new entry, and
7812 the resulting entry will not be shown. When TEXT is empty, switch to
7813 `org-agenda-diary-file' and let the user finish the entry there."
7814 (let ((cw (current-window-configuration)))
7815 (org-switch-to-buffer-other-window
7816 (find-file-noselect org-agenda-diary-file))
7817 (widen)
7818 (goto-char (point-min))
7819 (cond
7820 ((eq type 'anniversary)
7821 (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
7822 (progn
7823 (or (org-on-heading-p t)
7824 (progn
7825 (outline-next-heading)
7826 (insert "* Anniversaries\n\n")
7827 (beginning-of-line -1)))))
7828 (outline-next-heading)
7829 (org-back-over-empty-lines)
7830 (backward-char 1)
7831 (insert "\n")
7832 (insert (format "%%%%(org-anniversary %d %2d %2d) %s"
7833 (nth 2 d1) (car d1) (nth 1 d1) text)))
7834 ((eq type 'day)
7835 (let ((org-prefix-has-time t)
7836 (org-agenda-time-leading-zero t)
7837 fmt time time2)
7838 (if org-agenda-insert-diary-extract-time
7839 ;; Use org-format-agenda-item to parse text for a time-range and
7840 ;; remove it. FIXME: This is a hack, we should refactor
7841 ;; that function to make time extraction available separately
7842 (setq fmt (org-format-agenda-item nil text nil nil t)
7843 time (get-text-property 0 'time fmt)
7844 time2 (if (> (length time) 0)
7845 ;; split-string removes trailing ...... if
7846 ;; no end time given. First space
7847 ;; separates time from date.
7848 (concat " " (car (split-string time "\\.")))
7849 nil)
7850 text (get-text-property 0 'txt fmt)))
7851 (if (eq org-agenda-insert-diary-strategy 'top-level)
7852 (org-agenda-insert-diary-as-top-level text)
7853 (require 'org-datetree)
7854 (org-datetree-find-date-create d1)
7855 (org-agenda-insert-diary-make-new-entry text))
7856 (org-insert-time-stamp (org-time-from-absolute
7857 (calendar-absolute-from-gregorian d1))
7858 nil nil nil nil time2))
7859 (end-of-line 0))
7860 ((eq type 'block)
7861 (if (> (calendar-absolute-from-gregorian d1)
7862 (calendar-absolute-from-gregorian d2))
7863 (setq d1 (prog1 d2 (setq d2 d1))))
7864 (if (eq org-agenda-insert-diary-strategy 'top-level)
7865 (org-agenda-insert-diary-as-top-level text)
7866 (require 'org-datetree)
7867 (org-datetree-find-date-create d1)
7868 (org-agenda-insert-diary-make-new-entry text))
7869 (org-insert-time-stamp (org-time-from-absolute
7870 (calendar-absolute-from-gregorian d1)))
7871 (insert "--")
7872 (org-insert-time-stamp (org-time-from-absolute
7873 (calendar-absolute-from-gregorian d2)))
7874 (end-of-line 0)))
7875 (if (string-match "\\S-" text)
7876 (progn
7877 (set-window-configuration cw)
7878 (message "%s entry added to %s"
7879 (capitalize (symbol-name type))
7880 (abbreviate-file-name org-agenda-diary-file)))
7881 (org-reveal t)
7882 (message "Please finish entry here"))))
7884 (defun org-agenda-insert-diary-as-top-level (text)
7885 "Make new entry as a top-level entry at the end of the file.
7886 Add TEXT as headline, and position the cursor in the second line so that
7887 a timestamp can be added there."
7888 (widen)
7889 (goto-char (point-max))
7890 (or (bolp) (insert "\n"))
7891 (insert "* " text "\n")
7892 (if org-adapt-indentation (org-indent-to-column 2)))
7894 (defun org-agenda-insert-diary-make-new-entry (text)
7895 "Make new entry as last child of current entry.
7896 Add TEXT as headline, and position the cursor in the second line so that
7897 a timestamp can be added there."
7898 (let ((org-show-following-heading t)
7899 (org-show-siblings t)
7900 (org-show-hierarchy-above t)
7901 (org-show-entry-below t)
7902 col)
7903 (outline-next-heading)
7904 (org-back-over-empty-lines)
7905 (or (looking-at "[ \t]*$")
7906 (progn (insert "\n") (backward-char 1)))
7907 (org-insert-heading nil t)
7908 (org-do-demote)
7909 (setq col (current-column))
7910 (insert text "\n")
7911 (if org-adapt-indentation (org-indent-to-column col))
7912 (let ((org-show-following-heading t)
7913 (org-show-siblings t)
7914 (org-show-hierarchy-above t)
7915 (org-show-entry-below t))
7916 (org-show-context))))
7918 (defun org-agenda-diary-entry ()
7919 "Make a diary entry, like the `i' command from the calendar.
7920 All the standard commands work: block, weekly etc.
7921 When `org-agenda-diary-file' points to a file,
7922 `org-agenda-diary-entry-in-org-file' is called instead to create
7923 entries in that Org-mode file."
7924 (interactive)
7925 (org-agenda-check-type t 'agenda 'timeline)
7926 (if (not (eq org-agenda-diary-file 'diary-file))
7927 (org-agenda-diary-entry-in-org-file)
7928 (require 'diary-lib)
7929 (let* ((char (progn
7930 (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
7931 (read-char-exclusive)))
7932 (cmd (cdr (assoc char
7933 '((?d . insert-diary-entry)
7934 (?w . insert-weekly-diary-entry)
7935 (?m . insert-monthly-diary-entry)
7936 (?y . insert-yearly-diary-entry)
7937 (?a . insert-anniversary-diary-entry)
7938 (?b . insert-block-diary-entry)
7939 (?c . insert-cyclic-diary-entry)))))
7940 (oldf (symbol-function 'calendar-cursor-to-date))
7941 ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
7942 (point (point))
7943 (mark (or (mark t) (point))))
7944 (unless cmd
7945 (error "No command associated with <%c>" char))
7946 (unless (and (get-text-property point 'day)
7947 (or (not (equal ?b char))
7948 (get-text-property mark 'day)))
7949 (error "Don't know which date to use for diary entry"))
7950 ;; We implement this by hacking the `calendar-cursor-to-date' function
7951 ;; and the `calendar-mark-ring' variable. Saves a lot of code.
7952 (let ((calendar-mark-ring
7953 (list (calendar-gregorian-from-absolute
7954 (or (get-text-property mark 'day)
7955 (get-text-property point 'day))))))
7956 (unwind-protect
7957 (progn
7958 (fset 'calendar-cursor-to-date
7959 (lambda (&optional error dummy)
7960 (calendar-gregorian-from-absolute
7961 (get-text-property point 'day))))
7962 (call-interactively cmd))
7963 (fset 'calendar-cursor-to-date oldf))))))
7965 (defun org-agenda-execute-calendar-command (cmd)
7966 "Execute a calendar command from the agenda, with the date associated to
7967 the cursor position."
7968 (org-agenda-check-type t 'agenda 'timeline)
7969 (require 'diary-lib)
7970 (unless (get-text-property (point) 'day)
7971 (error "Don't know which date to use for calendar command"))
7972 (let* ((oldf (symbol-function 'calendar-cursor-to-date))
7973 (point (point))
7974 (date (calendar-gregorian-from-absolute
7975 (get-text-property point 'day)))
7976 ;; the following 2 vars are needed in the calendar
7977 (displayed-month (car date))
7978 (displayed-year (nth 2 date)))
7979 (unwind-protect
7980 (progn
7981 (fset 'calendar-cursor-to-date
7982 (lambda (&optional error dummy)
7983 (calendar-gregorian-from-absolute
7984 (get-text-property point 'day))))
7985 (call-interactively cmd))
7986 (fset 'calendar-cursor-to-date oldf))))
7988 (defun org-agenda-phases-of-moon ()
7989 "Display the phases of the moon for the 3 months around the cursor date."
7990 (interactive)
7991 (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
7993 (defun org-agenda-holidays ()
7994 "Display the holidays for the 3 months around the cursor date."
7995 (interactive)
7996 (org-agenda-execute-calendar-command 'list-calendar-holidays))
7998 (defvar calendar-longitude)
7999 (defvar calendar-latitude)
8000 (defvar calendar-location-name)
8002 (defun org-agenda-sunrise-sunset (arg)
8003 "Display sunrise and sunset for the cursor date.
8004 Latitude and longitude can be specified with the variables
8005 `calendar-latitude' and `calendar-longitude'. When called with prefix
8006 argument, latitude and longitude will be prompted for."
8007 (interactive "P")
8008 (require 'solar)
8009 (let ((calendar-longitude (if arg nil calendar-longitude))
8010 (calendar-latitude (if arg nil calendar-latitude))
8011 (calendar-location-name
8012 (if arg "the given coordinates" calendar-location-name)))
8013 (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
8015 (defun org-agenda-goto-calendar ()
8016 "Open the Emacs calendar with the date at the cursor."
8017 (interactive)
8018 (org-agenda-check-type t 'agenda 'timeline)
8019 (let* ((day (or (get-text-property (point) 'day)
8020 (error "Don't know which date to open in calendar")))
8021 (date (calendar-gregorian-from-absolute day))
8022 (calendar-move-hook nil)
8023 (calendar-view-holidays-initially-flag nil)
8024 (calendar-view-diary-initially-flag nil))
8025 (calendar)
8026 (calendar-goto-date date)))
8028 ;;;###autoload
8029 (defun org-calendar-goto-agenda ()
8030 "Compute the Org-mode agenda for the calendar date displayed at the cursor.
8031 This is a command that has to be installed in `calendar-mode-map'."
8032 (interactive)
8033 (org-agenda-list nil (calendar-absolute-from-gregorian
8034 (calendar-cursor-to-date))
8035 nil))
8037 (defun org-agenda-convert-date ()
8038 (interactive)
8039 (org-agenda-check-type t 'agenda 'timeline)
8040 (let ((day (get-text-property (point) 'day))
8041 date s)
8042 (unless day
8043 (error "Don't know which date to convert"))
8044 (setq date (calendar-gregorian-from-absolute day))
8045 (setq s (concat
8046 "Gregorian: " (calendar-date-string date) "\n"
8047 "ISO: " (calendar-iso-date-string date) "\n"
8048 "Day of Yr: " (calendar-day-of-year-string date) "\n"
8049 "Julian: " (calendar-julian-date-string date) "\n"
8050 "Astron. JD: " (calendar-astro-date-string date)
8051 " (Julian date number at noon UTC)\n"
8052 "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
8053 "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
8054 "French: " (calendar-french-date-string date) "\n"
8055 "Baha'i: " (calendar-bahai-date-string date) " (until sunset)\n"
8056 "Mayan: " (calendar-mayan-date-string date) "\n"
8057 "Coptic: " (calendar-coptic-date-string date) "\n"
8058 "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
8059 "Persian: " (calendar-persian-date-string date) "\n"
8060 "Chinese: " (calendar-chinese-date-string date) "\n"))
8061 (with-output-to-temp-buffer "*Dates*"
8062 (princ s))
8063 (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
8065 ;;; Bulk commands
8067 (defvar org-agenda-bulk-marked-entries nil
8068 "List of markers that refer to marked entries in the agenda.")
8070 (defun org-agenda-bulk-marked-p ()
8071 (eq (get-char-property (point-at-bol) 'type)
8072 'org-marked-entry-overlay))
8074 (defun org-agenda-bulk-mark (&optional arg)
8075 "Mark the entry at point for future bulk action."
8076 (interactive "p")
8077 (dotimes (i (max arg 1))
8078 (unless (org-get-at-bol 'org-agenda-diary-link)
8079 (let* ((m (org-get-at-bol 'org-hd-marker))
8081 (unless (org-agenda-bulk-marked-p)
8082 (unless m (error "Nothing to mark at point"))
8083 (push m org-agenda-bulk-marked-entries)
8084 (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
8085 (org-overlay-display ov "> "
8086 (org-get-todo-face "TODO")
8087 'evaporate)
8088 (overlay-put ov 'type 'org-marked-entry-overlay))
8089 (beginning-of-line 2)
8090 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8091 (beginning-of-line 2))
8092 (message "%d entries marked for bulk action"
8093 (length org-agenda-bulk-marked-entries))))))
8095 (defun org-agenda-bulk-mark-regexp (regexp)
8096 "Mark entries match REGEXP."
8097 (interactive "sMark entries matching regexp: ")
8098 (let (entries-marked)
8099 (save-excursion
8100 (goto-char (point-min))
8101 (goto-char (next-single-property-change (point) 'txt))
8102 (while (re-search-forward regexp nil t)
8103 (when (string-match regexp (get-text-property (point) 'txt))
8104 (setq entries-marked (+ entries-marked 1))
8105 (call-interactively 'org-agenda-bulk-mark))))
8106 (if (not entries-marked)
8107 (message "No entry matching this regexp."))))
8109 (defun org-agenda-bulk-unmark ()
8110 "Unmark the entry at point for future bulk action."
8111 (interactive)
8112 (when (org-agenda-bulk-marked-p)
8113 (org-agenda-bulk-remove-overlays
8114 (point-at-bol) (+ 2 (point-at-bol)))
8115 (setq org-agenda-bulk-marked-entries
8116 (delete (org-get-at-bol 'org-hd-marker)
8117 org-agenda-bulk-marked-entries)))
8118 (beginning-of-line 2)
8119 (while (and (get-char-property (point) 'invisible) (not (eobp)))
8120 (beginning-of-line 2))
8121 (message "%d entries marked for bulk action"
8122 (length org-agenda-bulk-marked-entries)))
8124 (defun org-agenda-bulk-toggle ()
8125 "Toggle marking the entry at point for bulk action."
8126 (interactive)
8127 (if (org-agenda-bulk-marked-p)
8128 (org-agenda-bulk-unmark)
8129 (org-agenda-bulk-mark)))
8131 (defun org-agenda-bulk-remove-overlays (&optional beg end)
8132 "Remove the mark overlays between BEG and END in the agenda buffer.
8133 BEG and END default to the buffer limits.
8135 This only removes the overlays, it does not remove the markers
8136 from the list in `org-agenda-bulk-marked-entries'."
8137 (interactive)
8138 (mapc (lambda (ov)
8139 (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
8140 (delete-overlay ov)))
8141 (overlays-in (or beg (point-min)) (or end (point-max)))))
8143 (defun org-agenda-bulk-remove-all-marks ()
8144 "Remove all marks in the agenda buffer.
8145 This will remove the markers, and the overlays."
8146 (interactive)
8147 (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
8148 (setq org-agenda-bulk-marked-entries nil)
8149 (org-agenda-bulk-remove-overlays (point-min) (point-max)))
8151 (defun org-agenda-bulk-action (&optional arg)
8152 "Execute an remote-editing action on all marked entries.
8153 The prefix arg is passed through to the command if possible."
8154 (interactive "P")
8155 ;; Make sure we have markers, and only valid ones
8156 (unless org-agenda-bulk-marked-entries (error "No entries are marked"))
8157 (mapc
8158 (lambda (m)
8159 (unless (and (markerp m)
8160 (marker-buffer m)
8161 (buffer-live-p (marker-buffer m))
8162 (marker-position m))
8163 (error "Marker %s for bulk command is invalid" m)))
8164 org-agenda-bulk-marked-entries)
8166 ;; Prompt for the bulk command
8167 (message (concat "Bulk: [r]efile [$]arch [A]rch->sib [t]odo"
8168 " [+/-]tag [s]chd [S]catter [d]eadline [f]unction"
8169 (when org-agenda-bulk-custom-functions
8170 (concat " Custom: ["
8171 (mapconcat (lambda(f) (char-to-string (car f)))
8172 org-agenda-bulk-custom-functions "")
8173 "]"))))
8174 (let* ((action (read-char-exclusive))
8175 (org-log-refile (if org-log-refile 'time nil))
8176 (entries (reverse org-agenda-bulk-marked-entries))
8177 redo-at-end
8178 cmd rfloc state e tag pos (cnt 0) (cntskip 0))
8179 (cond
8180 ((equal action ?$)
8181 (setq cmd '(org-agenda-archive)))
8183 ((equal action ?A)
8184 (setq cmd '(org-agenda-archive-to-archive-sibling)))
8186 ((member action '(?r ?w))
8187 (setq rfloc (org-refile-get-location
8188 "Refile to"
8189 (marker-buffer (car org-agenda-bulk-marked-entries))
8190 org-refile-allow-creating-parent-nodes))
8191 (if (nth 3 rfloc)
8192 (setcar (nthcdr 3 rfloc)
8193 (move-marker (make-marker) (nth 3 rfloc)
8194 (or (get-file-buffer (nth 1 rfloc))
8195 (find-buffer-visiting (nth 1 rfloc))
8196 (error "This should not happen")))))
8198 (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
8199 redo-at-end t))
8201 ((equal action ?t)
8202 (setq state (org-icompleting-read
8203 "Todo state: "
8204 (with-current-buffer (marker-buffer (car entries))
8205 (mapcar 'list org-todo-keywords-1))))
8206 (setq cmd `(let ((org-inhibit-blocking t)
8207 (org-inhibit-logging 'note))
8208 (org-agenda-todo ,state))))
8210 ((memq action '(?- ?+))
8211 (setq tag (org-icompleting-read
8212 (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
8213 (with-current-buffer (marker-buffer (car entries))
8214 (delq nil
8215 (mapcar (lambda (x)
8216 (if (stringp (car x)) x)) org-tag-alist)))))
8217 (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
8219 ((memq action '(?s ?d))
8220 (let* ((date (unless arg
8221 (org-read-date
8222 nil nil nil
8223 (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
8224 (ans (if arg nil org-read-date-final-answer))
8225 (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
8226 (setq cmd `(let* ((bound (fboundp 'read-string))
8227 (old (and bound (symbol-function 'read-string))))
8228 (unwind-protect
8229 (progn
8230 (fset 'read-string (lambda (&rest ignore) ,ans))
8231 (eval '(,c1 arg)))
8232 (if bound
8233 (fset 'read-string old)
8234 (fmakunbound 'read-string)))))))
8236 ((equal action ?S)
8237 (if (not (org-agenda-check-type nil 'agenda 'timeline 'todo))
8238 (error "Can't scatter tasks in \"%s\" agenda view" org-agenda-type)
8239 (let ((days (read-number
8240 (format "Scatter tasks across how many %sdays: "
8241 (if arg "week" "")) 7)))
8242 (setq cmd
8243 `(let ((distance (1+ (random ,days))))
8244 (if arg
8245 (let ((dist distance)
8246 (day-of-week
8247 (calendar-day-of-week
8248 (calendar-gregorian-from-absolute (org-today)))))
8249 (dotimes (i (1+ dist))
8250 (while (member day-of-week org-agenda-weekend-days)
8251 (incf distance)
8252 (incf day-of-week)
8253 (if (= day-of-week 7)
8254 (setq day-of-week 0)))
8255 (incf day-of-week)
8256 (if (= day-of-week 7)
8257 (setq day-of-week 0)))))
8258 ;; silently fail when try to replan a sexp entry
8259 (condition-case nil
8260 (let* ((date (calendar-gregorian-from-absolute
8261 (+ (org-today) distance)))
8262 (time (encode-time 0 0 0 (nth 1 date) (nth 0 date)
8263 (nth 2 date))))
8264 (org-agenda-schedule nil time))
8265 (error nil)))))))
8267 ((assoc action org-agenda-bulk-custom-functions)
8268 (setq cmd (list (cadr (assoc action org-agenda-bulk-custom-functions)))
8269 redo-at-end t))
8271 ((equal action ?f)
8272 (setq cmd (list (intern
8273 (org-icompleting-read "Function: "
8274 obarray 'fboundp t nil nil)))))
8276 (t (error "Invalid bulk action")))
8278 ;; Sort the markers, to make sure that parents are handled before children
8279 (setq entries (sort entries
8280 (lambda (a b)
8281 (cond
8282 ((equal (marker-buffer a) (marker-buffer b))
8283 (< (marker-position a) (marker-position b)))
8285 (string< (buffer-name (marker-buffer a))
8286 (buffer-name (marker-buffer b))))))))
8288 ;; Now loop over all markers and apply cmd
8289 (while (setq e (pop entries))
8290 (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
8291 (if (not pos)
8292 (progn (message "Skipping removed entry at %s" e)
8293 (setq cntskip (1+ cntskip)))
8294 (goto-char pos)
8295 (eval cmd)
8296 (setq org-agenda-bulk-marked-entries
8297 (delete e org-agenda-bulk-marked-entries))
8298 (setq cnt (1+ cnt))))
8299 (setq org-agenda-bulk-marked-entries nil)
8300 (org-agenda-bulk-remove-all-marks)
8301 (when redo-at-end (org-agenda-redo))
8302 (message "Acted on %d entries%s"
8304 (if (= cntskip 0)
8306 (format ", skipped %d (disappeared before their turn)"
8307 cntskip)))))
8309 ;;; Flagging notes
8311 (defun org-agenda-show-the-flagging-note ()
8312 "Display the flagging note in the other window.
8313 When called a second time in direct sequence, offer to remove the FLAGGING
8314 tag and (if present) the flagging note."
8315 (interactive)
8316 (let ((hdmarker (org-get-at-bol 'org-hd-marker))
8317 (win (selected-window))
8318 note heading newhead)
8319 (unless hdmarker
8320 (error "No linked entry at point"))
8321 (if (and (eq this-command last-command)
8322 (y-or-n-p "Unflag and remove any flagging note? "))
8323 (progn
8324 (org-agenda-remove-flag hdmarker)
8325 (let ((win (get-buffer-window "*Flagging Note*")))
8326 (and win (delete-window win)))
8327 (message "Entry unflaged"))
8328 (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
8329 (unless note
8330 (error "No flagging note"))
8331 (org-kill-new note)
8332 (org-switch-to-buffer-other-window "*Flagging Note*")
8333 (erase-buffer)
8334 (insert note)
8335 (goto-char (point-min))
8336 (while (re-search-forward "\\\\n" nil t)
8337 (replace-match "\n" t t))
8338 (goto-char (point-min))
8339 (select-window win)
8340 (message "Flagging note pushed to kill ring. Press [?] again to remove tag and note"))))
8342 (defun org-agenda-remove-flag (marker)
8343 "Remove the FLAGGED tag and any flagging note in the entry."
8344 (let (newhead)
8345 (org-with-point-at marker
8346 (org-toggle-tag "FLAGGED" 'off)
8347 (org-entry-delete nil "THEFLAGGINGNOTE")
8348 (setq newhead (org-get-heading)))
8349 (org-agenda-change-all-lines newhead marker)
8350 (message "Entry unflaged")))
8352 (defun org-agenda-get-any-marker (&optional pos)
8353 (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
8354 (get-text-property (or pos (point-at-bol)) 'org-marker)))
8356 ;;; Appointment reminders
8358 (defvar appt-time-msg-list)
8360 ;;;###autoload
8361 (defun org-agenda-to-appt (&optional refresh filter)
8362 "Activate appointments found in `org-agenda-files'.
8363 With a \\[universal-argument] prefix, refresh the list of
8364 appointments.
8366 If FILTER is t, interactively prompt the user for a regular
8367 expression, and filter out entries that don't match it.
8369 If FILTER is a string, use this string as a regular expression
8370 for filtering entries out.
8372 FILTER can also be an alist with the car of each cell being
8373 either 'headline or 'category. For example:
8375 '((headline \"IMPORTANT\")
8376 (category \"Work\"))
8378 will only add headlines containing IMPORTANT or headlines
8379 belonging to the \"Work\" category."
8380 (interactive "P")
8381 (if refresh (setq appt-time-msg-list nil))
8382 (if (eq filter t)
8383 (setq filter (read-from-minibuffer "Regexp filter: ")))
8384 (let* ((cnt 0) ; count added events
8385 (org-agenda-new-buffers nil)
8386 (org-deadline-warning-days 0)
8387 ;; Do not use `org-today' here because appt only takes
8388 ;; time and without date as argument, so it may pass wrong
8389 ;; information otherwise
8390 (today (org-date-to-gregorian
8391 (time-to-days (current-time))))
8392 (org-agenda-restrict nil)
8393 (files (org-agenda-files 'unrestricted)) entries file)
8394 ;; Get all entries which may contain an appt
8395 (org-prepare-agenda-buffers files)
8396 (while (setq file (pop files))
8397 (setq entries
8398 (append entries
8399 (org-agenda-get-day-entries
8400 file today :timestamp :scheduled :deadline))))
8401 (setq entries (delq nil entries))
8402 ;; Map thru entries and find if we should filter them out
8403 (mapc
8404 (lambda(x)
8405 (let* ((evt (org-trim (or (get-text-property 1 'txt x) "")))
8406 (cat (get-text-property 1 'org-category x))
8407 (tod (get-text-property 1 'time-of-day x))
8408 (ok (or (null filter)
8409 (and (stringp filter) (string-match filter evt))
8410 (and (listp filter)
8411 (or (string-match
8412 (cadr (assoc 'category filter)) cat)
8413 (string-match
8414 (cadr (assoc 'headline filter)) evt))))))
8415 ;; FIXME: Shall we remove text-properties for the appt text?
8416 ;; (setq evt (set-text-properties 0 (length evt) nil evt))
8417 (when (and ok tod)
8418 (setq tod (concat "00" (number-to-string tod))
8419 tod (when (string-match
8420 "\\([0-9]\\{1,2\\}\\)\\([0-9]\\{2\\}\\)\\'" tod)
8421 (concat (match-string 1 tod) ":"
8422 (match-string 2 tod))))
8423 (appt-add tod evt)
8424 (setq cnt (1+ cnt))))) entries)
8425 (org-release-buffers org-agenda-new-buffers)
8426 (if (eq cnt 0)
8427 (message "No event to add")
8428 (message "Added %d event%s for today" cnt (if (> cnt 1) "s" "")))))
8430 (defun org-agenda-todayp (date)
8431 "Does DATE mean today, when considering `org-extend-today-until'?"
8432 (let ((today (org-today))
8433 (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
8434 date)))
8435 (eq date today)))
8437 (provide 'org-agenda)
8439 ;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
8441 ;;; org-agenda.el ends here