Refill some long/short copyright headers.
[bpt/emacs.git] / lisp / org / org-agenda.el
index 9e71143..9adc180 100644 (file)
@@ -1,12 +1,11 @@
 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
 
-;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
-;;   Free Software Foundation, Inc.
+;; Copyright (C) 2004-2011  Free Software Foundation, Inc.
 
 ;; Author: Carsten Dominik <carsten at orgmode dot org>
 ;; Keywords: outlines, hypermedia, calendar, wp
 ;; Homepage: http://orgmode.org
-;; Version: 6.30c
+;; Version: 7.4
 ;;
 ;; This file is part of GNU Emacs.
 ;;
 
 (require 'org)
 (eval-when-compile
-  (require 'cl)
-  (require 'calendar))
+  (require 'cl))
 
 (declare-function diary-add-to-list "diary-lib"
                   (date string specifier &optional marker globcolor literal))
 (declare-function calendar-absolute-from-iso    "cal-iso"    (date))
 (declare-function calendar-astro-date-string    "cal-julian" (&optional date))
 (declare-function calendar-bahai-date-string    "cal-bahai"  (&optional date))
-(declare-function calendar-check-holidays       "holidays"   (date))
 (declare-function calendar-chinese-date-string  "cal-china"  (&optional date))
 (declare-function calendar-coptic-date-string   "cal-coptic" (&optional date))
 (declare-function calendar-ethiopic-date-string "cal-coptic" (&optional date))
 (declare-function calendar-julian-date-string   "cal-julian" (&optional date))
 (declare-function calendar-mayan-date-string    "cal-mayan"  (&optional date))
 (declare-function calendar-persian-date-string  "cal-persia" (&optional date))
+(declare-function org-datetree-find-date-create "org-datetree"
+                 (date &optional keep-restriction))
 (declare-function org-columns-quit              "org-colview" ())
+(declare-function diary-date-display-form       "diary-lib"  (&optional type))
+(declare-function org-mobile-write-agenda-for-mobile "org-mobile" (file))
+(declare-function org-habit-insert-consistency-graphs
+                 "org-habit" (&optional line))
+(declare-function org-is-habit-p "org-habit" (&optional pom))
+(declare-function org-habit-parse-todo "org-habit" (&optional pom))
+(declare-function org-habit-get-priority "org-habit" (habit &optional moment))
 (defvar calendar-mode-map)
+(defvar org-clock-current-task) ; defined in org-clock.el
+(defvar org-mobile-force-id-on-agenda-items) ; defined in org-mobile.el
+(defvar org-habit-show-habits)
+(defvar org-habit-show-habits-only-for-today)
 
 ;; Defined somewhere in this file, but used before definition.
 (defvar org-agenda-buffer-name)
 (defvar org-agenda-overriding-header)
+(defvar org-agenda-title-append nil)
 (defvar entry)
 (defvar date)
 (defvar org-agenda-undo-list)
@@ -76,7 +87,7 @@ only needed when the text to be killed contains more than N non-white lines."
          (integer :tag "When more than N lines")))
 
 (defcustom org-agenda-compact-blocks nil
-  "Non-nil means, make the block agenda more compact.
+  "Non-nil means make the block agenda more compact.
 This is done by leaving out unnecessary lines."
   :group 'org-agenda
   :type 'boolean)
@@ -96,7 +107,7 @@ If it is a character, it will be repeated to fill the window width."
  :group 'org-agenda)
 
 (defcustom org-agenda-with-colors t
-  "Non-nil means, use colors in agenda views."
+  "Non-nil means use colors in agenda views."
   :group 'org-agenda-export
   :type 'boolean)
 
@@ -131,13 +142,13 @@ specifies the maximum number of lines that will be added for each entry
 that is listed in the agenda view.
 
 Note that this variable is not used during display, only when exporting
-the agenda.  For agenda display, see org-agenda-entry-text-mode and the
-variable `org-agenda-entry-text-maxlines'."
+the agenda.  For agenda display, see the variables `org-agenda-entry-text-mode'
+and `org-agenda-entry-text-maxlines'."
   :group 'org-agenda
   :type 'integer)
 
 (defcustom org-agenda-add-entry-text-descriptive-links t
-  "Non-nil means, export org-links as descriptive links in agenda added text.
+  "Non-nil means export org-links as descriptive links in agenda added text.
 This variable applies to the text added to the agenda when
 `org-agenda-add-entry-text-maxlines' is larger than 0.
 When this variable nil, the URL will (also) be shown."
@@ -186,6 +197,11 @@ you can \"misuse\" it to also add other text to the header.  However,
   :group 'org-export-html
   :type 'string)
 
+(defcustom org-agenda-persistent-filter nil
+  "When set, keep filters from one agenda view to the next."
+  :group 'org-agenda
+  :type 'boolean)
+
 (defgroup org-agenda-custom-commands nil
  "Options concerning agenda views in Org-mode."
  :tag "Org Agenda Custom Commands"
@@ -199,6 +215,8 @@ you can \"misuse\" it to also add other text to the header.  However,
     (const priority-up) (const priority-down)
     (const todo-state-up) (const todo-state-down)
     (const effort-up) (const effort-down)
+    (const habit-up) (const habit-down)
+    (const alpha-up) (const alpha-down)
     (const user-defined-up) (const user-defined-down))
   "Sorting choices.")
 
@@ -223,8 +241,12 @@ you can \"misuse\" it to also add other text to the header.  However,
                  (const org-agenda-prefix-format :value "  %-12:c%?-12t% s")
                  (string))
            (list :tag "Number of days in agenda"
-                 (const org-agenda-ndays)
-                 (integer :value 1))
+                 (const org-agenda-span)
+                 (choice (const :tag "Day" 'day)
+                         (const :tag "Week" 'week)
+                         (const :tag "Month" 'month)
+                         (const :tag "Year" 'year)
+                         (integer :tag "Custom")))
            (list :tag "Fixed starting date"
                  (const org-agenda-start-day)
                  (string :value "2007-11-01"))
@@ -245,6 +267,13 @@ you can \"misuse\" it to also add other text to the header.  However,
                   (const :format "" quote)
                   (repeat
                    (string :tag "+tag or -tag"))))
+           (list :tag "Set daily/weekly entry types"
+                 (const org-agenda-entry-types)
+                 (set :greedy t :value (:deadline :scheduled :timestamp :sexp)
+                      (const :deadline)
+                      (const :scheduled)
+                      (const :timestamp)
+                      (const :sexp)))
            (list :tag "Standard skipping condition"
                  :value (org-agenda-skip-function '(org-agenda-skip-entry-if))
                  (const org-agenda-skip-function)
@@ -260,6 +289,24 @@ you can \"misuse\" it to also add other text to the header.  However,
                             :tag "Condition type"
                             (list :tag "Regexp matches" :inline t (const :format "" 'regexp) (regexp))
                             (list :tag "Regexp does not match" :inline t (const :format "" 'notregexp) (regexp))
+                            (list :tag "TODO state is" :inline t
+                                  (const 'todo)
+                                  (choice
+                                   (const :tag "any not-done state" 'todo)
+                                   (const :tag "any done state" 'done)
+                                   (const :tag "any state" 'any)
+                                   (list :tag "Keyword list"
+                                         (const :format "" quote)
+                                         (repeat (string :tag "Keyword")))))
+                            (list :tag "TODO state is not" :inline t
+                                  (const 'nottodo)
+                                  (choice
+                                   (const :tag "any not-done state" 'todo)
+                                   (const :tag "any done state" 'done)
+                                   (const :tag "any state" 'any)
+                                   (list :tag "Keyword list"
+                                         (const :format "" quote)
+                                         (repeat (string :tag "Keyword")))))
                             (const :tag "scheduled" 'scheduled)
                             (const :tag "not scheduled" 'notscheduled)
                             (const :tag "deadline" 'deadline)
@@ -480,22 +527,26 @@ this one will be used."
   "Options concerning the general tags/property/todo match agenda view."
   :tag "Org Agenda Match View"
   :group 'org-agenda)
+(defgroup org-agenda-search-view nil
+  "Options concerning the general tags/property/todo match agenda view."
+  :tag "Org Agenda Match View"
+  :group 'org-agenda)
 
 (defvar org-agenda-archives-mode nil
-  "Non-nil means, the agenda will include archived items.
+  "Non-nil means the agenda will include archived items.
 If this is the symbol `trees', trees in the selected agenda scope
 that are marked with the ARCHIVE tag will be included anyway.  When this is
 t, also all archive files associated with the current selection of agenda
 files will be included.")
 
 (defcustom org-agenda-skip-comment-trees t
-  "Non-nil means, skip trees that start with the COMMENT keyword.
+  "Non-nil means skip trees that start with the COMMENT keyword.
 When nil, these trees are also scanned by agenda commands."
   :group 'org-agenda-skip
   :type 'boolean)
 
 (defcustom org-agenda-todo-list-sublevels t
-  "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
+  "Non-nil means check also the sublevels of a TODO entry for TODO entries.
 When nil, the sublevels of a TODO entry are not checked, resulting in
 potentially much shorter TODO lists."
   :group 'org-agenda-skip
@@ -503,7 +554,7 @@ potentially much shorter TODO lists."
   :type 'boolean)
 
 (defcustom org-agenda-todo-ignore-with-date nil
-  "Non-nil means, don't show entries with a date in the global todo list.
+  "Non-nil means don't show entries with a date in the global todo list.
 You can use this if you prefer to mark mere appointments with a TODO keyword,
 but don't want them to show up in the TODO list.
 When this is set, it also covers deadlines and scheduled items, the settings
@@ -514,31 +565,105 @@ See also the variable `org-agenda-tags-todo-honor-ignore-options'."
   :group 'org-agenda-todo-list
   :type 'boolean)
 
+(defcustom org-agenda-todo-ignore-timestamp nil
+  "Non-nil means don't show entries with a timestamp.
+This applies when creating the global todo list.
+Valid values are:
+
+past     Don't show entries for today or in the past.
+
+future   Don't show entries with a timestamp in the future.
+         The idea behind this is that if it has a future
+         timestamp, you don't want to think about it until the
+         date.
+
+all      Don't show any entries with a timestamp in the global todo list.
+         The idea behind this is that by setting a timestamp, you
+         have already \"taken care\" of this item.
+
+See also `org-agenda-todo-ignore-with-date'.
+See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
+to make his option also apply to the tags-todo list."
+  :group 'org-agenda-skip
+  :group 'org-agenda-todo-list
+  :type '(choice
+         (const :tag "Ignore future timestamp todos" future)
+         (const :tag "Ignore past or present timestamp todos" past)
+         (const :tag "Ignore all timestamp todos" all)
+         (const :tag "Show timestamp todos" nil)))
+
 (defcustom org-agenda-todo-ignore-scheduled nil
-  "Non-nil means, don't show scheduled entries in the global todo list.
-The idea behind this is that by scheduling it, you have already taken care
-of this item.
+  "Non-nil means, ignore some scheduled TODO items when making TODO list.
+This applies when creating the global todo list.
+Valid values are:
+
+past     Don't show entries scheduled today or in the past.
+
+future   Don't show entries scheduled in the future.
+         The idea behind this is that by scheduling it, you don't want to
+         think about it until the scheduled date.
+
+all      Don't show any scheduled entries in the global todo list.
+         The idea behind this is that by scheduling it, you have already
+         \"taken care\" of this item.
+
+t        Same as `all', for backward compatibility.
+
 See also `org-agenda-todo-ignore-with-date'.
-See also the variable `org-agenda-tags-todo-honor-ignore-options'."
+See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
+to make his option also apply to the tags-todo list."
   :group 'org-agenda-skip
   :group 'org-agenda-todo-list
-  :type 'boolean)
+  :type '(choice
+         (const :tag "Ignore future-scheduled todos" future)
+         (const :tag "Ignore past- or present-scheduled todos" past)
+         (const :tag "Ignore all scheduled todos" all)
+         (const :tag "Ignore all scheduled todos (compatibility)" t)
+         (const :tag "Show scheduled todos" nil)))
 
 (defcustom org-agenda-todo-ignore-deadlines nil
-  "Non-nil means, don't show near deadline entries in the global todo list.
-Near means closer than `org-deadline-warning-days' days.
-The idea behind this is that such items will appear in the agenda anyway.
+  "Non-nil means ignore some deadlined TODO items when making TODO list.
+There are different motivations for using different values, please think
+carefully when configuring this variable.
+
+This applies when creating the global todo list.
+Valid values are:
+
+near    Don't show near deadline entries.  A deadline is near when it is
+        closer than `org-deadline-warning-days' days.  The idea behind this
+        is that such items will appear in the agenda anyway.
+
+far     Don't show TODO entries where a deadline has been defined, but
+        the deadline is not near.  This is useful if you don't want to
+        use the todo list to figure out what to do now.
+
+past    Don't show entries with a deadline timestamp for today or in the past.
+
+future  Don't show entries with a deadline timestamp in the future, not even
+        when they become `near' ones.  Use it with caution.
+
+all     Ignore all TODO entries that do have a deadline.
+
+t       Same as `near', for backward compatibility.
+
 See also `org-agenda-todo-ignore-with-date'.
-See also the variable `org-agenda-tags-todo-honor-ignore-options'."
+See also the variable `org-agenda-tags-todo-honor-ignore-options' if you want
+to make his option also apply to the tags-todo list."
   :group 'org-agenda-skip
   :group 'org-agenda-todo-list
-  :type 'boolean)
+  :type '(choice
+         (const :tag "Ignore near deadlines" near)
+         (const :tag "Ignore near deadlines (compatibility)" t)
+         (const :tag "Ignore far deadlines" far)
+         (const :tag "Ignore all TODOs with a deadlines" all)
+         (const :tag "Show all TODOs, even if they have a deadline" nil)))
 
 (defcustom org-agenda-tags-todo-honor-ignore-options nil
-  "Non-nil means, honor todo-list ...ignore options also in tags-todo search.
+  "Non-nil means honor todo-list ...ignore options also in tags-todo search.
 The variables
    `org-agenda-todo-ignore-with-date',
-   `org-agenda-todo-ignore-scheduled'
+   `org-agenda-todo-ignore-timestamp',
+   `org-agenda-todo-ignore-scheduled',
    `org-agenda-todo-ignore-deadlines'
 make the global TODO list skip entries that have time stamps of certain
 kinds.  If this option is set, the same options will also apply for the
@@ -586,6 +711,24 @@ deadlines are always turned off when the item is DONE."
   :group 'org-agenda-daily/weekly
   :type 'boolean)
 
+(defcustom org-agenda-skip-deadline-prewarning-if-scheduled nil
+  "Non-nil means skip deadline prewarning when entry is also scheduled.
+This will apply on all days where a prewarning for the deadline would
+be shown, but not at the day when the entry is actually due.  On that day,
+the deadline will be shown anyway.
+This variable may be set to nil, t, or a number which will then give
+the number of days before the actual deadline when the prewarnings
+should resume.
+This can be used in a workflow where the first showing of the deadline will
+trigger you to schedule it, and then you don't want to be reminded of it
+because you will take care of it on the day when scheduled."
+  :group 'org-agenda-skip
+  :group 'org-agenda-daily/weekly
+  :type '(choice
+         (const :tag "Alwas show prewarning" nil)
+         (const :tag "Remove prewarning if entry is scheduled" t)
+         (integer :tag "Restart prewarning N days before deadline")))
+
 (defcustom org-agenda-skip-additional-timestamps-same-entry t
   "When nil, multiple same-day timestamps in entry make multiple agenda lines.
 When non-nil, after the search for timestamps has matched once in an
@@ -600,13 +743,13 @@ entry, the rest of the entry will not be searched."
   :type 'boolean)
 
 (defcustom org-agenda-dim-blocked-tasks t
-  "Non-nil means, dim blocked tasks in the agenda display.
+  "Non-nil means dim blocked tasks in the agenda display.
 This causes some overhead during agenda construction, but if you
 have turned on `org-enforce-todo-dependencies',
 `org-enforce-todo-checkbox-dependencies', or any other blocking
 mechanism, this will create useful feedback in the agenda.
 
-Instead ot t, this variable can also have the value `invisible'.
+Instead of t, this variable can also have the value `invisible'.
 Then blocked tasks will be invisible and only become visible when
 they become unblocked.  An exemption to this behavior is when a task is
 blocked because of unchecked checkboxes below it.  Since checkboxes do
@@ -619,10 +762,10 @@ will only be dimmed."
   :type '(choice
          (const :tag "Do not dim" nil)
          (const :tag "Dim to a grey face" t)
-         (const :tag "Make invisibe" invisible)))
+         (const :tag "Make invisible" invisible)))
 
 (defcustom org-timeline-show-empty-dates 3
-  "Non-nil means, `org-timeline' also shows dates without an entry.
+  "Non-nil means `org-timeline' also shows dates without an entry.
 When nil, only the days which actually have entries are shown.
 When t, all days between the first and the last date are shown.
 When an integer, show also empty dates, but if there is a gap of more than
@@ -638,20 +781,41 @@ N days, just insert a special line indicating the size of the gap."
   :tag "Org Agenda Startup"
   :group 'org-agenda)
 
+(defcustom org-agenda-menu-show-matcher t
+  "Non-nil menas show the match string in the agenda dispatcher menu.
+When nil, the matcher string is not shown, but is put into the help-echo
+property so than moving the mouse over the command shows it.
+Setting it to nil is good if matcher strings are very long and/or if
+you wnat to use two-column display (see `org-agenda-menu-two-column')."
+  :group 'org-agenda
+  :type 'boolean)
+
+(defcustom org-agenda-menu-two-column nil
+  "Non-nil means, use two columns to show custom commands in the dispatcher.
+If you use this, you probably want to set `org-agenda-menu-show-matcher'
+to nil."
+  :group 'org-agenda
+  :type 'boolean)
+
 (defcustom org-finalize-agenda-hook nil
   "Hook run just before displaying an agenda buffer."
   :group 'org-agenda-startup
   :type 'hook)
 
 (defcustom org-agenda-mouse-1-follows-link nil
-  "Non-nil means, mouse-1 on a link will follow the link in the agenda.
+  "Non-nil means mouse-1 on a link will follow the link in the agenda.
 A longer mouse click will still set point.  Does not work on XEmacs.
 Needs to be set before org.el is loaded."
   :group 'org-agenda-startup
   :type 'boolean)
 
 (defcustom org-agenda-start-with-follow-mode nil
-  "The initial value of follow-mode in a newly created agenda window."
+  "The initial value of follow mode in a newly created agenda window."
+  :group 'org-agenda-startup
+  :type 'boolean)
+
+(defcustom org-agenda-show-outline-path t
+  "Non-nil means show outline path in echo area after line motion."
   :group 'org-agenda-startup
   :type 'boolean)
 
@@ -661,7 +825,7 @@ Needs to be set before org.el is loaded."
   :type 'boolean)
 
 (defcustom org-agenda-entry-text-maxlines 5
-  "Number of text lines to be added when `E' is presed in the agenda.
+  "Number of text lines to be added when `E' is pressed in the agenda.
 
 Note that this variable only used during agenda display.  Add add entry text
 when exporting the agenda, configure the variable
@@ -669,8 +833,23 @@ when exporting the agenda, configure the variable
   :group 'org-agenda
   :type 'integer)
 
+(defcustom org-agenda-entry-text-exclude-regexps nil
+  "List of regular expressions to clean up entry text.
+The complete matches of all regular expressions in this list will be
+removed from entry text before it is shown in the agenda."
+  :group 'org-agenda
+  :type '(repeat (regexp)))
+
+(defvar org-agenda-entry-text-cleanup-hook nil
+  "Hook that is run after basic cleanup of entry text to be shown in agenda.
+This cleanup is done in a temporary buffer, so the function may inspect and
+change the entire buffer.
+Some default stuff like drawers and scheduling/deadline dates will already
+have been removed when this is called, as will any matches for regular
+expressions listed in `org-agenda-entry-text-exclude-regexps'.")
+
 (defvar org-agenda-include-inactive-timestamps nil
-  "Non-nil means, include inactive time stamps in agenda and timeline.")
+  "Non-nil means include inactive time stamps in agenda and timeline.")
 
 (defgroup org-agenda-windows nil
   "Options concerning the windows used by the Agenda in Org Mode."
@@ -682,10 +861,11 @@ when exporting the agenda, configure the variable
 Possible values for this option are:
 
 current-window    Show agenda in the current window, keeping all other windows.
-other-frame       Use `switch-to-buffer-other-frame' to display agenda.
 other-window      Use `switch-to-buffer-other-window' to display agenda.
 reorganize-frame  Show only two windows on the current frame, the current
                   window and the agenda.
+other-frame       Use `switch-to-buffer-other-frame' to display agenda.
+                  Also, when exiting the agenda, kill that frame.
 See also the variable `org-agenda-restore-windows-after-quit'."
   :group 'org-agenda-windows
   :type '(choice
@@ -702,24 +882,37 @@ It only matters if `org-agenda-window-setup' is `reorganize-frame'."
   :type '(cons (number :tag "Minimum") (number :tag "Maximum")))
 
 (defcustom org-agenda-restore-windows-after-quit nil
-  "Non-nil means, restore window configuration open exiting agenda.
+  "Non-nil means restore window configuration open exiting agenda.
 Before the window configuration is changed for displaying the agenda,
 the current status is recorded.  When the agenda is exited with
 `q' or `x' and this option is set, the old state is restored.  If
 `org-agenda-window-setup' is `other-frame', the value of this
-option will be ignored.."
+option will be ignored."
   :group 'org-agenda-windows
   :type 'boolean)
 
-(defcustom org-agenda-ndays 7
-  "Number of days to include in overview display.
+(defcustom org-agenda-ndays nil
+   "Number of days to include in overview display.
 Should be 1 or 7.
+Obsolete, see `org-agenda-span'."
+   :group 'org-agenda-daily/weekly
+   :type 'integer)
+
+(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
+
+(defcustom org-agenda-span 'week
+  "Number of days to include in overview display.
+Can be day, week, month, year, or any number of days.
 Custom commands can set this variable in the options section."
   :group 'org-agenda-daily/weekly
-  :type 'integer)
+  :type '(choice (const :tag "Day" day)
+                (const :tag "Week" week)
+                (const :tag "Month" month)
+                (const :tag "Year" year)
+                (integer :tag "Custom")))
 
 (defcustom org-agenda-start-on-weekday 1
-  "Non-nil means, start the overview always on the specified weekday.
+  "Non-nil means start the overview always on the specified weekday.
 0 denotes Sunday, 1 denotes Monday etc.
 When nil, always start on the current day.
 Custom commands can set this variable in the options section."
@@ -728,7 +921,7 @@ Custom commands can set this variable in the options section."
                 (integer :tag "Weekday No.")))
 
 (defcustom org-agenda-show-all-dates t
-  "Non-nil means, `org-agenda' shows every day in the selected range.
+  "Non-nil means `org-agenda' shows every day in the selected range.
 When nil, only the days which actually have entries are shown."
   :group 'org-agenda-daily/weekly
   :type 'boolean)
@@ -767,6 +960,41 @@ This function makes sure that dates are aligned for easy reading."
     (format "%-10s %2d %s %4d%s"
            dayname day monthname year weekstring)))
 
+(defcustom org-agenda-time-leading-zero nil
+  "Non-nil means use leading zero for military times in agenda.
+For example, 9:30am would become 09:30 rather than  9:30."
+  :group 'org-agenda-daily/weekly
+  :type 'boolean)
+
+(defcustom org-agenda-timegrid-use-ampm nil
+  "When set, show AM/PM style timestamps on the timegrid."
+  :group 'org-agenda
+  :type 'boolean)
+
+(defun org-agenda-time-of-day-to-ampm (time)
+  "Convert TIME of a string like '13:45' to an AM/PM style time string."
+  (let* ((hour-number (string-to-number (substring time 0 -3)))
+         (minute (substring time -2))
+         (ampm "am"))
+    (cond
+     ((equal hour-number 12)
+      (setq ampm "pm"))
+     ((> hour-number 12)
+      (setq ampm "pm")
+      (setq hour-number (- hour-number 12))))
+    (concat
+     (if org-agenda-time-leading-zero
+        (format "%02d" hour-number)
+       (format "%02s" (number-to-string hour-number)))
+     ":" minute ampm)))
+
+(defun org-agenda-time-of-day-to-ampm-maybe (time)
+  "Conditionally convert TIME to AM/PM format
+based on `org-agenda-timegrid-use-ampm'"
+  (if org-agenda-timegrid-use-ampm
+      (org-agenda-time-of-day-to-ampm time)
+    time))
+
 (defcustom org-agenda-weekend-days '(6 0)
   "Which days are weekend?
 These days get the special face `org-agenda-date-weekend' in the agenda
@@ -787,6 +1015,12 @@ Custom commands can set this variable in the options section."
   :group 'org-agenda-daily/weekly
   :type 'boolean)
 
+(defcustom org-agenda-include-deadlines t
+  "If non-nil, include entries within their deadline warning period.
+Custom commands can set this variable in the options section."
+  :group 'org-agenda-daily/weekly
+  :type 'boolean)
+
 (defcustom org-agenda-include-all-todo nil
   "Set  means weekly/daily agenda will always contain all TODO entries.
 The TODO entries will be listed at the top of the agenda, before
@@ -796,7 +1030,7 @@ This option is deprecated, it is better to define a block agenda instead."
   :type 'boolean)
 
 (defcustom org-agenda-repeating-timestamp-show-all t
-  "Non-nil means, show all occurrences of a repeating stamp in the agenda.
+  "Non-nil means show all occurrences of a repeating stamp in the agenda.
 When nil, only one occurrence is shown, either today or the
 nearest into the future."
   :group 'org-agenda-daily/weekly
@@ -823,7 +1057,7 @@ the agenda to display all available LOG items temporarily."
   :type '(set :greedy t (const closed) (const clock) (const state)))
 
 (defcustom org-agenda-log-mode-add-notes t
-  "Non-nil means, add first line of notes to log entries in agenda views.
+  "Non-nil means add first line of notes to log entries in agenda views.
 If a log item like a state change or a clock entry is associated with
 notes, the first line of these notes will be added to the entry in the
 agenda display."
@@ -853,6 +1087,42 @@ current display in the agenda."
   :group 'org-agenda-daily/weekly
   :type 'plist)
 
+(defcustom org-agenda-search-view-always-boolean nil
+  "Non-nil means the search string is interpreted as individual parts.
+
+The search string for search view can either be interpreted as a phrase,
+or as a list of snippets that define a boolean search for a number of
+strings.
+
+When this is non-nil, the string will be split on whitespace, and each
+snippet will be searched individually, and all must match in order to
+select an entry.  A snippet is then a single string of non-white
+characters, or a string in double quotes, or a regexp in {} braces.
+If a snippet is preceded by \"-\", the snippet must *not* match.
+\"+\" is syntactic sugar for positive selection.  Each snippet may
+be found as a full word or a partial word, but see the variable
+`org-agenda-search-view-force-full-words'.
+
+When this is nil, search will look for the entire search phrase as one,
+with each space character matching any amount of whitespace, including
+line breaks.
+
+Even when this is nil, you can still switch to Boolean search dynamically
+by preceding the first snippet with \"+\" or \"-\".  If the first snippet
+is a regexp marked with braces like \"{abc}\", this will also switch to
+boolean search."
+  :group 'org-agenda-search-view
+  :type 'boolean)
+
+(if (fboundp 'defvaralias)
+    (defvaralias 'org-agenda-search-view-search-words-only
+      'org-agenda-search-view-always-boolean))
+
+(defcustom org-agenda-search-view-force-full-words nil
+  "Non-nil means, search words must be matches as complete words.
+When nil, they may also match part of a word."
+  :group 'org-agenda-search-view
+  :type 'boolean)
 
 (defgroup org-agenda-time-grid nil
   "Options concerning the time grid in the Org-mode Agenda."
@@ -860,7 +1130,7 @@ current display in the agenda."
   :group 'org-agenda)
 
 (defcustom org-agenda-search-headline-for-time t
-  "Non-nil means, search headline for a time-of-day.
+  "Non-nil means search headline for a time-of-day.
 If the headline contains a time-of-day in one format or another, it will
 be used to sort the entry into the time sequence of items for a day.
 Some people have time stamps in the headline that refer to the creation
@@ -871,7 +1141,7 @@ for a time."
   :type 'boolean)
 
 (defcustom org-agenda-use-time-grid t
-  "Non-nil means, show a time grid in the agenda schedule.
+  "Non-nil means show a time grid in the agenda schedule.
 A time grid is a set of lines for specific times (like every two hours between
 8:00 and 20:00).  The items scheduled for a day at specific times are
 sorted in between these lines.
@@ -918,9 +1188,9 @@ a grid line."
   :group 'org-agenda)
 
 (defcustom org-agenda-sorting-strategy
-  '((agenda time-up        priority-down category-keep)
-    (todo   priority-down  category-keep)
-    (tags   priority-down  category-keep)
+  '((agenda habit-down time-up priority-down category-keep)
+    (todo   priority-down category-keep)
+    (tags   priority-down category-keep)
     (search category-keep))
   "Sorting structure for the agenda items of a single day.
 This is a list of symbols which will be used in sequence to determine
@@ -943,6 +1213,10 @@ effort-up          Sort numerically by estimated effort, high effort last.
 effort-down        Sort numerically by estimated effort, high effort first.
 user-defined-up    Sort according to `org-agenda-cmp-user-defined', high last.
 user-defined-down  Sort according to `org-agenda-cmp-user-defined', high first.
+habit-up           Put entries that are habits first
+habit-down         Put entries that are habits last
+alpha-up           Sort headlines alphabetically
+alpha-down         Sort headlines alphabetically, reversed
 
 The different possibilities will be tried in sequence, and testing stops
 if one comparison returns a \"not-equal\".  For example, the default
@@ -959,7 +1233,7 @@ categories by priority.
 
 Instead of a single list, this can also be a set of list for specific
 contents, with a context symbol in the car of the list, any of
-`agenda', `todo', `tags' for the corresponding agenda views.
+`agenda', `todo', `tags', `search' for the corresponding agenda views.
 
 Custom commands can bind this variable in the options section."
   :group 'org-agenda-sorting
@@ -971,6 +1245,8 @@ Custom commands can bind this variable in the options section."
                (cons (const :tag "Strategy for TODO lists" todo)
                      (repeat ,org-sorting-choice))
                (cons (const :tag "Strategy for Tags matches" tags)
+                     (repeat ,org-sorting-choice))
+               (cons (const :tag "Strategy for search matches" search)
                      (repeat ,org-sorting-choice)))))
 
 (defcustom org-agenda-cmp-user-defined nil
@@ -984,7 +1260,7 @@ part of an agenda sorting strategy."
   :type 'symbol)
 
 (defcustom org-sort-agenda-notime-is-late t
-  "Non-nil means, items without time are considered late.
+  "Non-nil means items without time are considered late.
 This is only relevant for sorting.  When t, items which have no explicit
 time like 15:30 will be considered as 99:01, i.e. later than any items which
 do have a time.  When nil, the default time is before 0:00.  You can use this
@@ -994,7 +1270,7 @@ agenda entries."
   :type 'boolean)
 
 (defcustom org-sort-agenda-noeffort-is-high t
-  "Non-nil means, items without effort estimate are sorted as high effort.
+  "Non-nil means items without effort estimate are sorted as high effort.
 This also applies when filtering an agenda view with respect to the
 < or > effort operator.  Then, tasks with no effort defined will be treated
 as tasks with high effort.
@@ -1008,11 +1284,11 @@ When nil, such items are sorted as 0 minutes effort."
   :group 'org-agenda)
 
 (defcustom org-agenda-prefix-format
-  '((agenda  . "  %-12:c%?-12t% s")
+  '((agenda  . " %i %-12:c%?-12t% s")
     (timeline  . "  % s")
-    (todo  . "  %-12:c")
-    (tags  . "  %-12:c")
-    (search . "  %-12:c"))
+    (todo  . " %i %-12:c")
+    (tags  . " %i %-12:c")
+    (search . " %i %-12:c"))
   "Format specifications for the prefix of items in the agenda views.
 An alist with four entries, for the different agenda types.  The keys to the
 sublists are `agenda', `timeline', `todo', and `tags'.  The values
@@ -1021,6 +1297,8 @@ This format works similar to a printf format, with the following meaning:
 
   %c   the category of the item, \"Diary\" for entries from the diary, or
        as given by the CATEGORY keyword or derived from the file name.
+  %i   the icon category of the item, as give in
+       `org-agenda-category-icon-alist'.
   %T   the *last* tag of the item.  Last because inherited tags come
        first in the list.
   %t   the time-of-day specification if one applies to the entry, in the
@@ -1043,7 +1321,7 @@ If there is punctuation or whitespace character just before the final
 format letter, this character will be appended to the field value if
 the value is not empty.  For example, the format \"%-12:c\" leads to
 \"Diary: \" if the category is \"Diary\".  If the category were be
-empty, no additional colon would be interted.
+empty, no additional colon would be inserted.
 
 The default value of this option is \"  %-12:c%?-12t% s\", meaning:
 - Indent the line with two space characters
@@ -1099,7 +1377,7 @@ range, respectively."
                  (function))))
 
 (defcustom org-agenda-scheduled-leaders '("Scheduled: " "Sched.%2dx: ")
-  "Text preceeding scheduled items in the agenda view.
+  "Text preceding scheduled items in the agenda view.
 This is a list with two strings.  The first applies when the item is
 scheduled on the current day.  The second applies when it has been scheduled
 previously, it may contain a %d indicating that this is the nth time that
@@ -1111,8 +1389,16 @@ that passed since this item was scheduled first."
          (string :tag "Scheduled today     ")
          (string :tag "Scheduled previously")))
 
+(defcustom org-agenda-inactive-leader "["
+  "Text preceding item pulled into the agenda by inactive time stamps.
+These entries are added to the agenda when pressing \"[\"."
+  :group 'org-agenda-line-format
+  :type '(list
+         (string :tag "Scheduled today     ")
+         (string :tag "Scheduled previously")))
+
 (defcustom org-agenda-deadline-leaders '("Deadline:  " "In %3d d.: ")
-  "Text preceeding deadline items in the agenda view.
+  "Text preceding deadline items in the agenda view.
 This is a list with two strings.  The first applies when the item has its
 deadline on the current day.  The second applies when it is in the past or
 in the future, it may contain %d to capture how many days away the deadline
@@ -1125,7 +1411,7 @@ is (was)."
                  (function))))
 
 (defcustom org-agenda-remove-times-when-in-prefix t
-  "Non-nil means, remove duplicate time specifications in agenda items.
+  "Non-nil means remove duplicate time specifications in agenda items.
 When the format `org-agenda-prefix-format' contains a `%t' specifier, a
 time-of-day specification in a headline or diary entry is extracted and
 placed into the prefix.  If this option is non-nil, the original specification
@@ -1133,7 +1419,7 @@ placed into the prefix.  If this option is non-nil, the original specification
 11:30-4pm) will be removed for agenda display.  This makes the agenda less
 cluttered.
 The option can be t or nil.  It may also be the symbol `beg', indicating
-that the time should only be removed what it is located at the beginning of
+that the time should only be removed when it is located at the beginning of
 the headline/diary entry."
   :group 'org-agenda-line-format
   :type '(choice
@@ -1141,6 +1427,11 @@ the headline/diary entry."
          (const :tag "Never" nil)
          (const :tag "When at beginning of entry" beg)))
 
+(defcustom org-agenda-remove-timeranges-from-blocks nil
+  "Non-nil means remove time ranges specifications in agenda
+items that span on several days."
+  :group 'org-agenda-line-format
+  :type 'boolean)
 
 (defcustom org-agenda-default-appointment-duration nil
   "Default duration for appointments that only have a starting time.
@@ -1152,12 +1443,22 @@ When non-nil, this must be the number of minutes, e.g. 60 for one hour."
          (const :tag "No default duration")))
 
 (defcustom org-agenda-show-inherited-tags t
-  "Non-nil means, show inherited tags in each agenda line."
+  "Non-nil means show inherited tags in each agenda line."
   :group 'org-agenda-line-format
   :type 'boolean)
 
+(defcustom org-agenda-hide-tags-regexp nil
+  "Regular expression used to filter away specific tags in agenda views.
+This means that these tags will be present, but not be shown in the agenda
+line.  Secondary filtering will still work on the hidden tags.
+Nil means don't hide any tags."
+  :group 'org-agenda-line-format
+  :type '(choice
+         (const  :tag "Hide none" nil)
+         (string :tag "Regexp   ")))
+
 (defcustom org-agenda-remove-tags nil
-  "Non-nil means, remove the tags from the headline copy in the agenda.
+  "Non-nil means remove the tags from the headline copy in the agenda.
 When this is the symbol `prefix', only remove tags when
 `org-agenda-prefix-format' contains a `%T' specifier."
   :group 'org-agenda-line-format
@@ -1182,16 +1483,18 @@ it means that the tags should be flushright to that column.  For example,
     (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
 
 (defcustom org-agenda-fontify-priorities 'cookies
-  "Non-nil means, highlight low and high priorities in agenda.
+  "Non-nil means highlight low and high priorities in agenda.
 When t, the highest priority entries are bold, lowest priority italic.
-However, settings in org-priority-faces will overrule these faces.
+However, settings in `org-priority-faces' will overrule these faces.
 When this variable is the symbol `cookies', only fontify the
 cookies, not the entire task.
 This may also be an association list of priority faces, whose
 keys are the character values of `org-highest-priority',
 `org-default-priority', and `org-lowest-priority' (the default values
-are ?A, ?B, and ?C, respectively).  The face may be a named face,
-or a list like `(:background \"Red\")'."
+are ?A, ?B, and ?C, respectively).  The face may be a named face, a
+color as a string, or a list like `(:background \"Red\")'.
+If it is a color, the variable `org-faces-easy-properties'
+determines if it is a foreground or a background color."
   :group 'org-agenda-line-format
   :type '(choice
          (const :tag "Never" nil)
@@ -1199,7 +1502,55 @@ or a list like `(:background \"Red\")'."
          (const :tag "Cookies only" cookies)
          (repeat :tag "Specify"
                  (list (character :tag "Priority" :value ?A)
-                       (sexp :tag "face")))))
+                       (choice    :tag "Face    "
+                                  (string :tag "Color")
+                                  (sexp :tag "Face"))))))
+
+(defcustom org-agenda-day-face-function nil
+  "Function called to determine what face should be used to display a day.
+The only argument passed to that function is the day. It should
+returns a face, or nil if does not want to specify a face and let
+the normal rules apply."
+  :group 'org-agenda-line-format
+  :type 'function)
+
+(defcustom org-agenda-category-icon-alist nil
+  "Alist of category icon to be displayed in agenda views.
+
+Each entry should have the following format:
+
+  (CATEGORY-REGEXP FILE-OR-DATA TYPE DATA-P PROPS)
+
+Where CATEGORY-REGEXP is a regexp matching the categories where
+the icon should be displayed.
+FILE-OR-DATA either a file path or a string containing image data.
+
+The other fields can be ommited safely if not needed:
+TYPE indicates the image type.
+DATA-P is a boolean indicating whether the FILE-OR-DATA string is
+image data.
+PROPS are additional image attributes to assign to the image,
+like, e.g. `:ascent center'.
+
+   (\"Org\" \"/path/to/icon.png\" nil nil :ascent center)
+
+If you want to set the display properties yourself, just put a
+list as second element:
+
+  (CATEGORY-REGEXP (MY PROPERTY LIST))
+
+For example, to display a 16px horizontal space for Emacs
+category, you can use:
+
+  (\"Emacs\" '(space . (:width (16))))"
+  :group 'org-agenda-line-format
+  :type '(alist :key-type (string :tag "Regexp matching category")
+               :value-type (choice (list :tag "Icon"
+                                         (string :tag "File or data")
+                                         (symbol :tag "Type")
+                                         (boolean :tag "Data?")
+                                         (repeat :tag "Extra image properties" :inline t symbol))
+                                   (list :tag "Display properties" sexp))))
 
 (defgroup org-agenda-column-view nil
   "Options concerning column view in the agenda."
@@ -1207,12 +1558,12 @@ or a list like `(:background \"Red\")'."
   :group 'org-agenda)
 
 (defcustom org-agenda-columns-show-summaries t
-  "Non-nil means, show summaries for columns displayed in the agenda view."
+  "Non-nil means show summaries for columns displayed in the agenda view."
   :group 'org-agenda-column-view
   :type 'boolean)
 
 (defcustom org-agenda-columns-remove-prefix-from-item t
-  "Non-nil means, remove the prefix from a headline for agenda column view.
+  "Non-nil means remove the prefix from a headline for agenda column view.
 The special ITEM field in the columns format contains the current line, with
 all information shown in other columns (like the TODO state or a tag).
 When this variable is non-nil, also the agenda prefix will be removed from
@@ -1222,7 +1573,7 @@ headline can be shown in the limited width of the field."
   :type 'boolean)
 
 (defcustom org-agenda-columns-compute-summary-properties t
-  "Non-nil means, recompute all summary properties before column view.
+  "Non-nil means recompute all summary properties before column view.
 When column view in the agenda is listing properties that have a summary
 operator, it can go to all relevant buffers and recompute the summaries
 there.  This can mean overhead for the agenda column view, but is necessary
@@ -1233,7 +1584,7 @@ computations are current."
   :type 'boolean)
 
 (defcustom org-agenda-columns-add-appointments-to-effort-sum nil
-  "Non-nil means, the duration of an appointment will add to day effort.
+  "Non-nil means the duration of an appointment will add to day effort.
 The property to which appointment durations will be added is the one given
 in the option `org-effort-property'.  If an appointment does not have
 an end time, `org-agenda-default-appointment-duration' will be used.  If that
@@ -1242,10 +1593,34 @@ estimate."
   :group 'org-agenda-column-view
   :type 'boolean)
 
+(defcustom org-agenda-auto-exclude-function nil
+  "A function called with a tag to decide if it is filtered on '/ RET'.
+The sole argument to the function, which is called once for each
+possible tag, is a string giving the name of the tag.  The
+function should return either nil if the tag should be included
+as normal, or \"-<TAG>\" to exclude the tag.
+Note that for the purpose of tag filtering, only the lower-case version of
+all tags will be considered, so that this function will only ever see
+the lower-case version of all tags."
+  :group 'org-agenda
+  :type 'function)
+
 (eval-when-compile
   (require 'cl))
 (require 'org)
 
+(defmacro org-agenda-with-point-at-orig-entry (string &rest body)
+  "Execute BODY with point at location given by `org-hd-marker' property.
+If STRING is non-nil, the text property will be fetched from position 0
+in that string.  If STRING is nil, it will be fetched from the beginning
+of the current line."
+  `(let ((marker (get-text-property (if string 0 (point-at-bol))
+                                   'org-hd-marker string)))
+     (with-current-buffer (marker-buffer marker)
+       (save-excursion
+        (goto-char marker)
+        ,@body))))
+
 (defun org-add-agenda-custom-command (entry)
   "Replace or add a command in `org-agenda-custom-commands'.
 This is mostly for hacking and trying a new command - once the command
@@ -1259,6 +1634,8 @@ works you probably want to add it to `org-agenda-custom-commands' for good."
 
 (defvar org-agenda-mode-map (make-sparse-keymap)
   "Keymap for `org-agenda-mode'.")
+(if (fboundp 'defvaralias)
+    (defvaralias 'org-agenda-keymap 'org-agenda-mode-map))
 
 (defvar org-agenda-menu) ; defined later in this file.
 (defvar org-agenda-restrict) ; defined later in this file.
@@ -1269,7 +1646,7 @@ works you probably want to add it to `org-agenda-custom-commands' for good."
 (defvar org-agenda-redo-command nil)
 (defvar org-agenda-query-string nil)
 (defvar org-agenda-mode-hook nil
-  "Hook for org-agenda-mode, run after the mode is turned on.")
+  "Hook for `org-agenda-mode', run after the mode is turned on.")
 (defvar org-agenda-type nil)
 (defvar org-agenda-force-single-file nil)
 (defvar org-agenda-bulk-marked-entries) ;; Defined further down in this file
@@ -1336,12 +1713,16 @@ The following commands are available:
 (org-defkey org-agenda-mode-map "U"        'org-agenda-bulk-remove-all-marks)
 (org-defkey org-agenda-mode-map "B"        'org-agenda-bulk-action)
 (org-defkey org-agenda-mode-map "\C-c\C-x!" 'org-reload)
-(org-defkey org-agenda-mode-map "\C-c$"    'org-agenda-archive)
+(org-defkey org-agenda-mode-map "\C-c\C-x\C-a" 'org-agenda-archive-default)
+(org-defkey org-agenda-mode-map "\C-c\C-xa"    'org-agenda-toggle-archive-tag)
+(org-defkey org-agenda-mode-map "\C-c\C-xA"    'org-agenda-archive-to-archive-sibling)
 (org-defkey org-agenda-mode-map "\C-c\C-x\C-s" 'org-agenda-archive)
+(org-defkey org-agenda-mode-map "\C-c$"        'org-agenda-archive)
 (org-defkey org-agenda-mode-map "$"        'org-agenda-archive)
-(org-defkey org-agenda-mode-map "A"        'org-agenda-archive-to-archive-sibling)
 (org-defkey org-agenda-mode-map "\C-c\C-o" 'org-agenda-open-link)
-(org-defkey org-agenda-mode-map " "        'org-agenda-show)
+(org-defkey org-agenda-mode-map " "        'org-agenda-show-and-scroll-up)
+(org-defkey org-agenda-mode-map [backspace] 'org-agenda-show-scroll-down)
+(org-defkey org-agenda-mode-map "\d" 'org-agenda-show-scroll-down)
 (org-defkey org-agenda-mode-map [(control shift right)] 'org-agenda-todo-nextset)
 (org-defkey org-agenda-mode-map [(control shift left)]  'org-agenda-todo-previousset)
 (org-defkey org-agenda-mode-map "\C-c\C-xb" 'org-agenda-tree-to-indirect-buffer)
@@ -1349,7 +1730,7 @@ The following commands are available:
 (org-defkey org-agenda-mode-map "L"        'org-agenda-recenter)
 (org-defkey org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
 (org-defkey org-agenda-mode-map "t"        'org-agenda-todo)
-(org-defkey org-agenda-mode-map "a"        'org-agenda-toggle-archive-tag)
+(org-defkey org-agenda-mode-map "a"        'org-agenda-archive-default-with-confirmation)
 (org-defkey org-agenda-mode-map ":"        'org-agenda-set-tags)
 (org-defkey org-agenda-mode-map "\C-c\C-q" 'org-agenda-set-tags)
 (org-defkey org-agenda-mode-map "."        'org-agenda-goto-today)
@@ -1379,6 +1760,7 @@ The following commands are available:
 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
 (org-defkey org-agenda-mode-map "v" 'org-agenda-view-mode-dispatch)
 (org-defkey org-agenda-mode-map "D" 'org-agenda-toggle-diary)
+(org-defkey org-agenda-mode-map "!" 'org-agenda-toggle-deadlines)
 (org-defkey org-agenda-mode-map "G" 'org-agenda-toggle-time-grid)
 (org-defkey org-agenda-mode-map "r" 'org-agenda-redo)
 (org-defkey org-agenda-mode-map "g" 'org-agenda-redo)
@@ -1394,8 +1776,12 @@ The following commands are available:
 (org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
 (org-defkey org-agenda-mode-map "P" 'org-agenda-show-priority)
 (org-defkey org-agenda-mode-map "T" 'org-agenda-show-tags)
-(org-defkey org-agenda-mode-map "n" 'next-line)
-(org-defkey org-agenda-mode-map "p" 'previous-line)
+(org-defkey org-agenda-mode-map "n" 'org-agenda-next-line)
+(org-defkey org-agenda-mode-map "p" 'org-agenda-previous-line)
+(substitute-key-definition 'next-line 'org-agenda-next-line
+                          org-agenda-mode-map global-map)
+(substitute-key-definition 'previous-line 'org-agenda-previous-line
+                          org-agenda-mode-map global-map)
 (org-defkey org-agenda-mode-map "\C-c\C-a" 'org-attach)
 (org-defkey org-agenda-mode-map "\C-c\C-n" 'org-agenda-next-date-line)
 (org-defkey org-agenda-mode-map "\C-c\C-p" 'org-agenda-previous-date-line)
@@ -1415,7 +1801,7 @@ The following commands are available:
 (org-defkey org-agenda-mode-map "\C-c\C-x\C-x" 'org-agenda-clock-cancel)
 (org-defkey org-agenda-mode-map "X" 'org-agenda-clock-cancel)
 (org-defkey org-agenda-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
-(org-defkey org-agenda-mode-map "J" 'org-clock-goto)
+(org-defkey org-agenda-mode-map "J" 'org-agenda-clock-goto)
 (org-defkey org-agenda-mode-map "+" 'org-agenda-priority-up)
 (org-defkey org-agenda-mode-map "-" 'org-agenda-priority-down)
 (org-defkey org-agenda-mode-map [(shift up)] 'org-agenda-priority-up)
@@ -1434,53 +1820,110 @@ The following commands are available:
 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
 (org-defkey org-agenda-mode-map "\\" 'org-agenda-filter-by-tag-refine)
 (org-defkey org-agenda-mode-map ";" 'org-timer-set-timer)
+(define-key org-agenda-mode-map "?" 'org-agenda-show-the-flagging-note)
+(org-defkey org-agenda-mode-map "\C-c\C-x\C-mg"    'org-mobile-pull)
+(org-defkey org-agenda-mode-map "\C-c\C-x\C-mp"    'org-mobile-push)
 
-(defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
-  "Local keymap for agenda entries from Org-mode.")
-
-(org-defkey org-agenda-keymap
-  (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
-(org-defkey org-agenda-keymap
-  (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
+(org-defkey org-agenda-mode-map [mouse-2] 'org-agenda-goto-mouse)
+(org-defkey org-agenda-mode-map [mouse-3] 'org-agenda-show-mouse)
 (when org-agenda-mouse-1-follows-link
-  (org-defkey org-agenda-keymap [follow-link] 'mouse-face))
+  (org-defkey org-agenda-mode-map [follow-link] 'mouse-face))
 (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
   '("Agenda"
     ("Agenda Files")
     "--"
-    ["Show" org-agenda-show t]
+    ("Agenda Dates"
+     ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
+     ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
+     ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
+     ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)])
+    "--"
+    ("View"
+     ["Day View" org-agenda-day-view
+      :active (org-agenda-check-type nil 'agenda)
+      :style radio :selected (eq org-agenda-current-span 'day)
+      :keys "v d  (or just d)"]
+     ["Week View" org-agenda-week-view
+      :active (org-agenda-check-type nil 'agenda)
+      :style radio :selected (eq org-agenda-current-span 'week)
+      :keys "v w  (or just w)"]
+     ["Month View" org-agenda-month-view
+      :active (org-agenda-check-type nil 'agenda)
+      :style radio :selected (eq org-agenda-current-span 'month)
+      :keys "v m"]
+     ["Year View" org-agenda-year-view
+      :active (org-agenda-check-type nil 'agenda)
+      :style radio :selected (eq org-agenda-current-span 'year)
+      :keys "v y"]
+     "--"
+     ["Include Diary" org-agenda-toggle-diary
+      :style toggle :selected org-agenda-include-diary
+      :active (org-agenda-check-type nil 'agenda)]
+     ["Include Deadlines" org-agenda-toggle-deadlines
+      :style toggle :selected org-agenda-include-deadlines
+      :active (org-agenda-check-type nil 'agenda)]
+     ["Use Time Grid" org-agenda-toggle-time-grid
+      :style toggle :selected org-agenda-use-time-grid
+      :active (org-agenda-check-type nil 'agenda)]
+     "--"
+     ["Show clock report" org-agenda-clockreport-mode
+      :style toggle :selected org-agenda-clockreport-mode
+      :active (org-agenda-check-type nil 'agenda)]
+     ["Show some entry text" org-agenda-entry-text-mode
+      :style toggle :selected org-agenda-entry-text-mode
+      :active t]
+    "--"
+     ["Show Logbook entries" org-agenda-log-mode
+      :style toggle :selected org-agenda-show-log
+      :active (org-agenda-check-type nil 'agenda 'timeline)
+      :keys "v l (or just l)"]
+     ["Include archived trees" org-agenda-archives-mode
+      :style toggle :selected org-agenda-archives-mode :active t
+      :keys "v a"]
+     ["Include archive files" (org-agenda-archives-mode t)
+      :style toggle :selected (eq org-agenda-archives-mode t) :active t
+      :keys "v A"]
+     "--"
+     ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
+    ["Write view to file" org-write-agenda t]
+    ["Rebuild buffer" org-agenda-redo t]
+    ["Save all Org-mode Buffers" org-save-all-org-buffers t]
+    "--"
+    ["Show original entry" org-agenda-show t]
     ["Go To (other window)" org-agenda-goto t]
     ["Go To (this window)" org-agenda-switch-to t]
     ["Follow Mode" org-agenda-follow-mode
      :style toggle :selected org-agenda-follow-mode :active t]
-    ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
+;    ["Tree to indirect frame" org-agenda-tree-to-indirect-buffer t]
     "--"
-    ["Cycle TODO" org-agenda-todo t]
-    ("Archive and Refile"
+    ("TODO"
+     ["Cycle TODO" org-agenda-todo t]
+     ["Next TODO set" org-agenda-todo-nextset t]
+     ["Previous TODO set" org-agenda-todo-previousset t]
+     ["Add note" org-agenda-add-note t])
+    ("Archive/Refile/Delete"
+     ["Archive default" org-agenda-archive-default t]
+     ["Archive default" org-agenda-archive-default-with-confirmation t]
      ["Toggle ARCHIVE tag" org-agenda-toggle-archive-tag t]
      ["Move to archive sibling" org-agenda-archive-to-archive-sibling t]
      ["Archive subtree" org-agenda-archive t]
-     ["Refile" org-agenda-refile t])
-    ["Delete subtree" org-agenda-kill t]
+     "--"
+     ["Refile" org-agenda-refile t]
+     "--"
+     ["Delete subtree" org-agenda-kill t])
     ("Bulk action"
-     ["Toggle mark entry" org-agenda-bulk-mark t]
+     ["Mark entry" org-agenda-bulk-mark t]
+     ["Unmark entry" org-agenda-bulk-unmark t]
      ["Act on all marked" org-agenda-bulk-action t]
      ["Unmark all entries" org-agenda-bulk-remove-all-marks :active t :keys "C-u s"])
     "--"
-    ["Add note" org-agenda-add-note t]
-    "--"
-    ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
-    ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
-    ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
-    ["Jump to date" org-agenda-goto-date (org-agenda-check-type nil 'agenda)]
-    "--"
     ("Tags and Properties"
      ["Show all Tags" org-agenda-show-tags t]
      ["Set Tags current line" org-agenda-set-tags (not (org-region-active-p))]
      ["Change tag in region" org-agenda-set-tags (org-region-active-p)]
      "--"
      ["Column View" org-columns t])
-    ("Date/Schedule"
+    ("Deadline/Schedule"
      ["Schedule" org-agenda-schedule t]
      ["Set Deadline" org-agenda-deadline t]
      "--"
@@ -1518,57 +1961,17 @@ The following commands are available:
      ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
      ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)]
      "--"
-     ["Create iCalendar file" org-export-icalendar-combine-agenda-files t])
+     ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
     "--"
-    ("View"
-     ["Day View" org-agenda-day-view
-      :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (equal org-agenda-ndays 1)
-      :keys "v d  (or just d)"]
-     ["Week View" org-agenda-week-view
-      :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (equal org-agenda-ndays 7)
-      :keys "v w  (or just w)"]
-     ["Month View" org-agenda-month-view
-      :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (member org-agenda-ndays '(28 29 30 31))
-      :keys "v m"]
-     ["Year View" org-agenda-year-view
-      :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (member org-agenda-ndays '(365 366))
-      :keys "v y"]
-     "--"
-     ["Include Diary" org-agenda-toggle-diary
-      :style toggle :selected org-agenda-include-diary
-      :active (org-agenda-check-type nil 'agenda)]
-     ["Use Time Grid" org-agenda-toggle-time-grid
-      :style toggle :selected org-agenda-use-time-grid
-      :active (org-agenda-check-type nil 'agenda)]
-     "--"
-     ["Show clock report" org-agenda-clockreport-mode
-      :style toggle :selected org-agenda-clockreport-mode
-      :active (org-agenda-check-type nil 'agenda)]
-     ["Show some entry text" org-agenda-entry-text-mode
-      :style toggle :selected org-agenda-entry-text-mode
-      :active t]
+    ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
     "--"
-     ["Show Logbook entries" org-agenda-log-mode
-      :style toggle :selected org-agenda-show-log
-      :active (org-agenda-check-type nil 'agenda 'timeline)
-      :keys "v l (or just l)"]
-     ["Include archived trees" org-agenda-archives-mode
-      :style toggle :selected org-agenda-archives-mode :active t
-      :keys "v a"]
-     ["Include archive files" (org-agenda-archives-mode t)
-      :style toggle :selected (eq org-agenda-archives-mode t) :active t
-      :keys "v A"]
+    ("MobileOrg"
+     ["Push Files and Views" org-mobile-push t]
+     ["Get Captured and Flagged" org-mobile-pull t]
+     ["Find FLAGGED Tasks" (org-agenda nil "?") :active t :keys "C-c a ?"]
+     ["Show note / unflag" org-agenda-show-the-flagging-note t]
      "--"
-     ["Remove Restriction" org-agenda-remove-restriction-lock org-agenda-restrict])
-    ["Write view to file" org-write-agenda t]
-    ["Rebuild buffer" org-agenda-redo t]
-    ["Save all Org-mode Buffers" org-save-all-org-buffers t]
-    "--"
-    ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
+     ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
     "--"
     ["Quit" org-agenda-quit t]
     ["Exit and Release Buffers" org-agenda-exit t]
@@ -1577,7 +1980,7 @@ The following commands are available:
 ;;; Agenda undo
 
 (defvar org-agenda-allow-remote-undo t
-  "Non-nil means, allow remote undo from the agenda buffer.")
+  "Non-nil means allow remote undo from the agenda buffer.")
 (defvar org-agenda-undo-list nil
   "List of undoable operations in the agenda since last refresh.")
 (defvar org-agenda-undo-has-started-in nil
@@ -1650,7 +2053,7 @@ M     Like `m', but select only TODO entries, no ordinary headlines.
 L     Create a timeline for the current buffer.
 e     Export views to associated files.
 s     Search entries for keywords.
-/     Multi occur accros all agenda files and also files listed
+/     Multi occur across all agenda files and also files listed
       in `org-agenda-text-search-extra-files'.
 <     Restrict agenda commands to buffer, subtree, or region.
       Press several times to get the desired effect.
@@ -1691,9 +2094,11 @@ Pressing `<' twice means to restrict to the current subtree or region
           (buf (current-buffer))
           (bfn (buffer-file-name (buffer-base-buffer)))
           entry key type match lprops ans)
-      ;; Turn off restriction unless there is an overriding one
+      ;; Turn off restriction unless there is an overriding one,
       (unless org-agenda-overriding-restriction
-       (put 'org-agenda-files 'org-restrict nil)
+       (unless (org-bound-and-true-p org-agenda-keep-restricted-file-list)
+         ;; There is a request to keep the file list in place
+         (put 'org-agenda-files 'org-restrict nil))
        (setq org-agenda-restrict nil)
        (move-marker org-agenda-restrict-begin nil)
        (move-marker org-agenda-restrict-end nil))
@@ -1705,7 +2110,7 @@ Pressing `<' twice means to restrict to the current subtree or region
        (setq ans (org-agenda-get-restriction-and-command prefix-descriptions)
              keys (car ans)
              restriction (cdr ans)))
-      ;; Estabish the restriction, if any
+      ;; Establish the restriction, if any
       (when (and (not org-agenda-overriding-restriction) restriction)
        (put 'org-agenda-files 'org-restrict (list bfn))
        (cond
@@ -1721,13 +2126,13 @@ Pressing `<' twice means to restrict to the current subtree or region
            (move-marker org-agenda-restrict-end
                         (progn (org-end-of-subtree t)))))))
 
-      (require 'calendar)  ; FIXME: can we avoid this for some commands?
       ;; For example the todo list should not need it (but does...)
       (cond
        ((setq entry (assoc keys org-agenda-custom-commands))
        (if (or (symbolp (nth 2 entry)) (functionp (nth 2 entry)))
            (progn
-             (setq type (nth 2 entry) match (nth 3 entry) lprops (nth 4 entry))
+             (setq type (nth 2 entry) match (eval (nth 3 entry))
+                   lprops (nth 4 entry))
              (put 'org-agenda-redo-command 'org-lprops lprops)
              (cond
               ((eq type 'agenda)
@@ -1772,6 +2177,22 @@ Pressing `<' twice means to restrict to the current subtree or region
        ((equal keys "m") (call-interactively 'org-tags-view))
        ((equal keys "M") (org-call-with-arg 'org-tags-view (or arg '(4))))
        ((equal keys "e") (call-interactively 'org-store-agenda-views))
+       ((equal keys "?") (org-tags-view nil "+FLAGGED")
+       (org-add-hook
+        'post-command-hook
+        (lambda ()
+          (unless (current-message)
+            (let* ((m (org-agenda-get-any-marker))
+                   (note (and m (org-entry-get m "THEFLAGGINGNOTE"))))
+              (when note
+                (message (concat
+                          "FLAGGING-NOTE ([?] for more info): "
+                          (org-add-props
+                              (replace-regexp-in-string
+                               "\\\\n" "//"
+                               (copy-sequence note))
+                              nil 'face 'org-warning)))))))
+        t t))
        ((equal keys "L")
        (unless (org-mode-p)
          (error "This is not an Org-mode file"))
@@ -1802,7 +2223,8 @@ Pressing `<' twice means to restrict to the current subtree or region
           (custom org-agenda-custom-commands)
           (selstring "")
           restriction second-time
-          c entry key type match prefixes rmheader header-end custom1 desc)
+          c entry key type match prefixes rmheader header-end custom1 desc
+          line lines left right n n1)
       (save-window-excursion
        (delete-other-windows)
        (org-switch-to-buffer-other-window " *Agenda Commands*")
@@ -1817,7 +2239,7 @@ t   List of all TODO entries            T   Entries with special TODO kwd
 m   Match a TAGS/PROP/TODO query        M   Like m, but only TODO entries
 L   Timeline for current buffer         #   List stuck projects (!=configure)
 s   Search for keywords                 C   Configure custom agenda commands
-/   Multi-occur
+/   Multi-occur                         ?   Find :FLAGGED: entries
 ")
                        (start 0))
                    (while (string-match
@@ -1840,56 +2262,91 @@ s   Search for keywords                 C   Configure custom agenda commands
            (move-marker header-end (match-end 0)))
          (goto-char header-end)
          (delete-region (point) (point-max))
+
+         ;; Produce all the lines that describe custom commands and prefixes
+         (setq lines nil)
          (while (setq entry (pop custom1))
            (setq key (car entry) desc (nth 1 entry)
                  type (nth 2 entry)
                  match (nth 3 entry))
            (if (> (length key) 1)
                (add-to-list 'prefixes (string-to-char key))
-             (insert
-              (format
-               "\n%-4s%-14s: %s"
-               (org-add-props (copy-sequence key)
-                   '(face bold))
-               (cond
-                ((string-match "\\S-" desc) desc)
-                ((eq type 'agenda) "Agenda for current week or day")
-                ((eq type 'alltodo) "List of all TODO entries")
-                ((eq type 'search) "Word search")
-                ((eq type 'stuck) "List of stuck projects")
-                ((eq type 'todo) "TODO keyword")
-                ((eq type 'tags) "Tags query")
-                ((eq type 'tags-todo) "Tags (TODO)")
-                ((eq type 'tags-tree) "Tags tree")
-                ((eq type 'todo-tree) "TODO kwd tree")
-                ((eq type 'occur-tree) "Occur tree")
-                ((functionp type) (if (symbolp type)
-                                      (symbol-name type)
-                                    "Lambda expression"))
-                (t "???"))
-               (cond
-                ((stringp match)
-                 (setq match (copy-sequence match))
-                 (org-add-props match nil 'face 'org-warning))
-                (match
-                 (format "set of %d commands" (length match)))
-                (t ""))))))
+             (setq line
+                   (format
+                    "%-4s%-14s"
+                    (org-add-props (copy-sequence key)
+                        '(face bold))
+                    (cond
+                     ((string-match "\\S-" desc) desc)
+                     ((eq type 'agenda) "Agenda for current week or day")
+                     ((eq type 'alltodo) "List of all TODO entries")
+                     ((eq type 'search) "Word search")
+                     ((eq type 'stuck) "List of stuck projects")
+                     ((eq type 'todo) "TODO keyword")
+                     ((eq type 'tags) "Tags query")
+                     ((eq type 'tags-todo) "Tags (TODO)")
+                     ((eq type 'tags-tree) "Tags tree")
+                     ((eq type 'todo-tree) "TODO kwd tree")
+                     ((eq type 'occur-tree) "Occur tree")
+                     ((functionp type) (if (symbolp type)
+                                           (symbol-name type)
+                                         "Lambda expression"))
+                     (t "???"))))
+             (if org-agenda-menu-show-matcher
+                 (setq line
+                       (concat line ": "
+                               (cond
+                                ((stringp match)
+                                 (setq match (copy-sequence match))
+                                 (org-add-props match nil 'face 'org-warning))
+                                (match
+                                 (format "set of %d commands" (length match)))
+                                (t ""))))
+               (if (org-string-nw-p match)
+                   (add-text-properties
+                    0 (length line) (list 'help-echo
+                                          (concat "Matcher: "match)) line)))
+             (push line lines)))
+         (setq lines (nreverse lines))
          (when prefixes
            (mapc (lambda (x)
-                   (insert
-                    (format "\n%s   %s"
+                   (push
+                    (format "%s   %s"
                             (org-add-props (char-to-string x)
-                                           nil 'face 'bold)
-                            (or (cdr (assoc (concat selstring (char-to-string x))
+                                nil 'face 'bold)
+                            (or (cdr (assoc (concat selstring
+                                                    (char-to-string x))
                                             prefix-descriptions))
-                                "Prefix key"))))
+                                "Prefix key"))
+                    lines))
                  prefixes))
+
+         ;; Check if we should display in two columns
+         (if org-agenda-menu-two-column
+             (progn
+               (setq n (length lines)
+                     n1 (+ (/ n 2) (mod n 2))
+                     right (nthcdr n1 lines)
+                     left (copy-sequence lines))
+               (setcdr (nthcdr (1- n1) left) nil))
+           (setq left lines right nil))
+         (while left
+           (insert "\n" (pop left))
+           (when right
+             (if (< (current-column) 40)
+                 (move-to-column 40 t)
+               (insert "   "))
+             (insert (pop right))))
+
+         ;; Make the window the right size
          (goto-char (point-min))
          (if second-time
              (if (not (pos-visible-in-window-p (point-max)))
                  (org-fit-window-to-buffer))
            (setq second-time t)
            (org-fit-window-to-buffer))
+
+         ;; Ask for selection
          (message "Press key for agenda command%s:"
                   (if (or restrict-ok org-agenda-overriding-restriction)
                       (if org-agenda-overriding-restriction
@@ -1936,7 +2393,7 @@ s   Search for keywords                 C   Configure custom agenda commands
           ((eq c ?>)
            (org-agenda-remove-restriction-lock 'noupdate)
            (setq restriction nil))
-          ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/)))
+          ((and (equal selstring "") (memq c '(?s ?a ?t ?m ?L ?C ?e ?T ?M ?# ?! ?/ ??)))
            (throw 'exit (cons (setq selstring (char-to-string c)) restriction)))
            ((and (> (length selstring) 0) (eq c ?\d))
             (delete-window)
@@ -1954,7 +2411,7 @@ s   Search for keywords                 C   Configure custom agenda commands
         match ;; The byte compiler incorrectly complains about this.  Keep it!
         cmd type lprops)
     (while (setq cmd (pop cmds))
-      (setq type (car cmd) match (nth 1 cmd) lprops (nth 2 cmd))
+      (setq type (car cmd) match (eval (nth 1 cmd)) lprops (nth 2 cmd))
       (cond
        ((eq type 'agenda)
        (org-let2 gprops lprops
@@ -1993,7 +2450,7 @@ s   Search for keywords                 C   Configure custom agenda commands
 If CMD-KEY is a string of length 1, it is used as a key in
 `org-agenda-custom-commands' and triggers this command.  If it is a
 longer string it is used as a tags/todo match string.
-Paramters are alternating variable names and values that will be bound
+Parameters are alternating variable names and values that will be bound
 before running the agenda command."
   (let (pars)
     (while parameters
@@ -2005,10 +2462,13 @@ before running the agenda command."
     (set-buffer org-agenda-buffer-name)
     (princ (org-encode-for-stdout (buffer-string)))))
 
+;(defun org-encode-for-stdout (string)
+;  (if (fboundp 'encode-coding-string)
+;      (encode-coding-string string buffer-file-coding-system)
+;    string))
+
 (defun org-encode-for-stdout (string)
-  (if (fboundp 'encode-coding-string)
-      (encode-coding-string string buffer-file-coding-system)
-    string))
+  string)
 
 (defvar org-agenda-info nil)
 
@@ -2018,7 +2478,7 @@ before running the agenda command."
 If CMD-KEY is a string of length 1, it is used as a key in
 `org-agenda-custom-commands' and triggers this command.  If it is a
 longer string it is used as a tags/todo match string.
-Paramters are alternating variable names and values that will be bound
+Parameters are alternating variable names and values that will be bound
 before running the agenda command.
 
 The output gives a line for each selected agenda item.  Each
@@ -2067,14 +2527,14 @@ agenda-day   The day in the agenda where this is listed"
          (princ
           (org-encode-for-stdout
            (mapconcat 'org-agenda-export-csv-mapper
-                      '(org-category txt type todo tags date time-of-day extra
+                      '(org-category txt type todo tags date time extra
                                      priority-letter priority agenda-day)
                      ",")))
          (princ "\n"))))))
 
 (defun org-fix-agenda-info (props)
-  "Make sure all properties on an agenda item have a canonical form,
-so the export commands can easily use it."
+  "Make sure all properties on an agenda item have a canonical form.
+This ensures the export commands can easily use it."
   (let (tmp re)
     (when (setq tmp (plist-get props 'tags))
       (setq props (plist-put props 'tags (mapconcat 'identity tmp ":"))))
@@ -2150,6 +2610,17 @@ so the export commands can easily use it."
          (and (get-buffer org-agenda-buffer-name)
               (kill-buffer org-agenda-buffer-name)))))))
 
+(defun org-agenda-mark-header-line (pos)
+  "Mark the line at POS as an agenda structure header."
+  (save-excursion
+    (goto-char pos)
+    (put-text-property (point-at-bol) (point-at-eol)
+                      'org-agenda-structural-header t)
+    (when org-agenda-title-append
+      (put-text-property (point-at-bol) (point-at-eol)
+                        'org-agenda-title-append org-agenda-title-append))))
+
+(defvar org-mobile-creating-agendas)
 (defun org-write-agenda (file &optional open nosettings)
   "Write the current buffer (an agenda view) as a file.
 Depending on the extension of the file name, plain text (.txt),
@@ -2157,7 +2628,7 @@ HTML (.html or .htm) or Postscript (.ps) is produced.
 If the extension is .ics, run icalendar export over all files used
 to construct the agenda and limit the export to entries listed in the
 agenda now.
-With prefic argument OPEN, open the new file immediately.
+With prefix argument OPEN, open the new file immediately.
 If NOSETTINGS is given, do not scope the settings of
 `org-agenda-exporter-settings' into the export commands.  This is used when
 the settings have already been scoped and we do not wish to overrule other,
@@ -2165,9 +2636,6 @@ higher priority settings."
   (interactive "FWrite agenda to file: \nP")
   (if (not (file-writable-p file))
       (error "Cannot write agenda to file %s" file))
-  (cond
-   ((string-match "\\.html?\\'" file) (require 'htmlize))
-   ((string-match "\\.ps\\'" file) (require 'ps-print)))
   (org-let (if nosettings nil org-agenda-exporter-settings)
     '(save-excursion
        (save-window-excursion
@@ -2175,6 +2643,8 @@ higher priority settings."
         (let ((bs (copy-sequence (buffer-string))) beg)
           (org-agenda-unmark-filtered-text)
           (with-temp-buffer
+            (rename-buffer "Agenda View" t)
+            (set-buffer-modified-p nil)
             (insert bs)
             (org-agenda-remove-marked-text 'org-filtered)
             (while (setq beg (text-property-any (point-min) (point-max)
@@ -2184,7 +2654,10 @@ higher priority settings."
                        (point-max))))
             (run-hooks 'org-agenda-before-write-hook)
             (cond
+             ((org-bound-and-true-p org-mobile-creating-agendas)
+              (org-mobile-write-agenda-for-mobile file))
              ((string-match "\\.html?\\'" file)
+              (require 'htmlize)
               (set-buffer (htmlize-buffer (current-buffer)))
 
               (when (and org-agenda-export-html-style
@@ -2199,18 +2672,17 @@ higher priority settings."
               (message "HTML written to %s" file))
              ((string-match "\\.ps\\'" file)
               (require 'ps-print)
-              (flet ((ps-get-buffer-name () "Agenda View"))
-                (ps-print-buffer-with-faces file))
+              (ps-print-buffer-with-faces file)
               (message "Postscript written to %s" file))
              ((string-match "\\.pdf\\'" file)
               (require 'ps-print)
-              (flet ((ps-get-buffer-name () "Agenda View"))
-                (ps-print-buffer-with-faces
-                 (concat (file-name-sans-extension file) ".ps")))
+              (ps-print-buffer-with-faces
+               (concat (file-name-sans-extension file) ".ps"))
               (call-process "ps2pdf" nil nil nil
                             (expand-file-name
                              (concat (file-name-sans-extension file) ".ps"))
                             (expand-file-name file))
+              (delete-file (concat (file-name-sans-extension file) ".ps"))
               (message "PDF written to %s" file))
              ((string-match "\\.ics\\'" file)
               (require 'org-icalendar)
@@ -2239,9 +2711,9 @@ higher priority settings."
   (let ((inhibit-read-only t))
     (mapc
      (lambda (o)
-       (when (equal (org-overlay-buffer o) (current-buffer))
+       (when (equal (overlay-buffer o) (current-buffer))
         (put-text-property
-         (org-overlay-start o) (org-overlay-end o)
+         (overlay-start o) (overlay-end o)
          'org-filtered t)))
      org-agenda-filter-overlays)))
 
@@ -2266,20 +2738,27 @@ VALUE defaults to t."
 This will add a maximum of `org-agenda-add-entry-text-maxlines' lines of the
 entry text following headings shown in the agenda.
 Drawers will be excluded, also the line with scheduling/deadline info."
-  (when (> org-agenda-add-entry-text-maxlines 0)
+  (when (and (> org-agenda-add-entry-text-maxlines 0)
+            (not (org-bound-and-true-p org-mobile-creating-agendas)))
     (let (m txt)
       (goto-char (point-min))
       (while (not (eobp))
-       (if (not (setq m (get-text-property (point) 'org-hd-marker)))
+       (if (not (setq m (org-get-at-bol 'org-hd-marker)))
            (beginning-of-line 2)
          (setq txt (org-agenda-get-some-entry-text
-                    m org-agenda-add-entry-text-maxlines))
+                    m org-agenda-add-entry-text-maxlines "    > "))
          (end-of-line 1)
-         (if (string-match "\\S-" txt) (insert "\n" txt)))))))
+         (if (string-match "\\S-" txt)
+             (insert "\n" txt)
+           (or (eobp) (forward-char 1))))))))
 
-(defun org-agenda-get-some-entry-text (marker n-lines)
+(defun org-agenda-get-some-entry-text (marker n-lines &optional indent
+                                             &rest keep)
   "Extract entry text from MARKER, at most N-LINES lines.
-This will ignore drawers etc, just get the text."
+This will ignore drawers etc, just get the text.
+If INDENT is given, prefix every line with this string.  If KEEP is
+given, it is a list of symbols, defining stuff that should not be
+removed from the entry content.  Currently only `planning' is allowed here."
   (let (txt drawer-re kwd-time-re ind)
     (save-excursion
       (with-current-buffer (marker-buffer marker)
@@ -2289,9 +2768,9 @@ This will ignore drawers etc, just get the text."
            (save-restriction
              (widen)
              (goto-char marker)
-             (beginning-of-line 2)
+             (end-of-line 1)
              (setq txt (buffer-substring
-                        (point)
+                        (min (1+ (point)) (point-max))
                         (progn (outline-next-heading) (point)))
                    drawer-re org-drawer-regexp
                    kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
@@ -2314,13 +2793,22 @@ This will ignore drawers etc, just get the text."
                   (progn (re-search-forward
                           "^[ \t]*:END:.*\n?" nil 'move)
                          (point))))
+               (unless (member 'planning keep)
+                 (goto-char (point-min))
+                 (while (re-search-forward kwd-time-re nil t)
+                   (replace-match "")))
                (goto-char (point-min))
-               (while (re-search-forward kwd-time-re nil t)
-                 (replace-match ""))
-               (if (re-search-forward "[ \t\n]+\\'" nil t)
-                   (replace-match ""))
-               (goto-char (point-min))
-               ;; find min indentation
+               (when org-agenda-entry-text-exclude-regexps
+                 (let ((re-list org-agenda-entry-text-exclude-regexps) re)
+                   (while (setq re (pop re-list))
+                     (goto-char (point-min))
+                     (while (re-search-forward re nil t)
+                       (replace-match "")))))
+               (goto-char (point-max))
+               (skip-chars-backward " \t\n")
+               (if (looking-at "[ \t\n]+\\'") (replace-match ""))
+
+               ;; find and remove min common indentation
                (goto-char (point-min))
                (untabify (point-min) (point-max))
                (setq ind (org-get-indentation))
@@ -2334,9 +2822,13 @@ This will ignore drawers etc, just get the text."
                    (move-to-column ind)
                    (delete-region (point-at-bol) (point)))
                  (beginning-of-line 2))
+
+               (run-hooks 'org-agenda-entry-text-cleanup-hook)
+
                (goto-char (point-min))
-               (while (and (not (eobp)) (re-search-forward "^" nil t))
-                 (replace-match "    > "))
+               (when indent
+                 (while (and (not (eobp)) (re-search-forward "^" nil t))
+                   (replace-match indent t t)))
                (goto-char (point-min))
                (while (looking-at "[ \t]*\n") (replace-match ""))
                (goto-char (point-max))
@@ -2353,8 +2845,8 @@ This will ignore drawers etc, just get the text."
     (save-excursion
       (goto-char (point-min))
       (while (not (eobp))
-       (when (setq m (or (get-text-property (point) 'org-hd-marker)
-                         (get-text-property (point) 'org-marker)))
+       (when (setq m (or (org-get-at-bol 'org-hd-marker)
+                         (org-get-at-bol 'org-marker)))
          (push m markers))
        (beginning-of-line 2)))
     (nreverse markers)))
@@ -2407,17 +2899,23 @@ This will ignore drawers etc, just get the text."
 (defvar org-agenda-columns-active nil)
 (defvar org-agenda-name nil)
 (defvar org-agenda-filter nil)
+(defvar org-agenda-filter-while-redo nil)
 (defvar org-agenda-filter-preset nil
   "A preset of the tags filter used for secondary agenda filtering.
-This must be a list of strings, each string must be a single tag preceeded
+This must be a list of strings, each string must be a single tag preceded
 by \"+\" or \"-\".
 This variable should not be set directly, but agenda custom commands can
-bind it in the options section.")
+bind it in the options section.  The preset filter is a global property of
+the entire agenda view.  In a block agenda, it will not work reliably to
+define a filter for one of the individual blocks.  You need to set it in
+the global options and expect it to be applied to the entire view.")
 
 (defun org-prepare-agenda (&optional name)
   (setq org-todo-keywords-for-agenda nil)
   (setq org-done-keywords-for-agenda nil)
-  (setq org-agenda-filter nil)
+  (setq org-drawers-for-agenda nil)
+  (unless org-agenda-persistent-filter
+    (setq org-agenda-filter nil))
   (put 'org-agenda-filter :preset-filter org-agenda-filter-preset)
   (if org-agenda-multi
       (progn
@@ -2438,6 +2936,7 @@ bind it in the options section.")
          (org-uniquify org-todo-keywords-for-agenda))
     (setq org-done-keywords-for-agenda
          (org-uniquify org-done-keywords-for-agenda))
+    (setq org-drawers-for-agenda (org-uniquify org-drawers-for-agenda))
     (let* ((abuf (get-buffer-create org-agenda-buffer-name))
           (awin (get-buffer-window abuf)))
       (cond
@@ -2449,11 +2948,14 @@ bind it in the options section.")
        ((equal org-agenda-window-setup 'other-window)
        (org-switch-to-buffer-other-window abuf))
        ((equal org-agenda-window-setup 'other-frame)
-       (switch-to-buffer-other-frame abuf)
-       (set-window-dedicated-p (selected-window) t))
+       (switch-to-buffer-other-frame abuf))
        ((equal org-agenda-window-setup 'reorganize-frame)
        (delete-other-windows)
-       (org-switch-to-buffer-other-window abuf))))
+       (org-switch-to-buffer-other-window abuf)))
+      ;; additional test in case agenda is invoked from within agenda
+      ;; buffer via elisp link
+      (unless (equal (current-buffer) abuf)
+       (switch-to-buffer abuf)))
     (setq buffer-read-only nil)
     (let ((inhibit-read-only t)) (erase-buffer))
     (org-agenda-mode)
@@ -2488,38 +2990,40 @@ bind it in the options section.")
       (when org-agenda-entry-text-mode
        (org-agenda-entry-text-hide)
        (org-agenda-entry-text-show))
+      (if (functionp 'org-habit-insert-consistency-graphs)
+         (org-habit-insert-consistency-graphs))
       (run-hooks 'org-finalize-agenda-hook)
-      (setq org-agenda-type (get-text-property (point) 'org-agenda-type))
-      (when (get 'org-agenda-filter :preset-filter)
+      (setq org-agenda-type (org-get-at-bol 'org-agenda-type))
+      (when (or org-agenda-filter (get 'org-agenda-filter :preset-filter))
        (org-agenda-filter-apply org-agenda-filter))
       )))
 
 (defun org-agenda-mark-clocking-task ()
   "Mark the current clock entry in the agenda if it is present."
   (mapc (lambda (o)
-         (if (eq (org-overlay-get o 'type) 'org-agenda-clocking)
-             (org-delete-overlay o)))
-       (org-overlays-in (point-min) (point-max)))
+         (if (eq (overlay-get o 'type) 'org-agenda-clocking)
+             (delete-overlay o)))
+       (overlays-in (point-min) (point-max)))
   (when (marker-buffer org-clock-hd-marker)
     (save-excursion
       (goto-char (point-min))
       (let (s ov)
        (while (setq s (next-single-property-change (point) 'org-hd-marker))
          (goto-char s)
-         (when (equal (get-text-property (point) 'org-hd-marker)
+         (when (equal (org-get-at-bol 'org-hd-marker)
                       org-clock-hd-marker)
-           (setq ov (org-make-overlay (point-at-bol) (1+ (point-at-eol))))
-           (org-overlay-put ov 'type 'org-agenda-clocking)
-           (org-overlay-put ov 'face 'org-agenda-clocking)
-           (org-overlay-put ov 'help-echo
+           (setq ov (make-overlay (point-at-bol) (1+ (point-at-eol))))
+           (overlay-put ov 'type 'org-agenda-clocking)
+           (overlay-put ov 'face 'org-agenda-clocking)
+           (overlay-put ov 'help-echo
                             "The clock is running in this item")))))))
 
 (defun org-agenda-fontify-priorities ()
   "Make highest priority lines bold, and lowest italic."
   (interactive)
-  (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-priority)
-                       (org-delete-overlay o)))
-       (org-overlays-in (point-min) (point-max)))
+  (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-priority)
+                       (delete-overlay o)))
+       (overlays-in (point-min) (point-max)))
   (save-excursion
     (let ((inhibit-read-only t)
          b e p ov h l)
@@ -2534,21 +3038,25 @@ bind it in the options section.")
              e (if (eq org-agenda-fontify-priorities 'cookies)
                    (match-end 0)
                  (point-at-eol))
-             ov (org-make-overlay b e))
-       (org-overlay-put
+             ov (make-overlay b e))
+       (overlay-put
         ov 'face
-        (cond ((cdr (assoc p org-priority-faces)))
+        (cond ((org-face-from-face-or-color
+                'priority nil
+                (cdr (assoc p org-priority-faces))))
               ((and (listp org-agenda-fontify-priorities)
-                    (cdr (assoc p org-agenda-fontify-priorities))))
+                    (org-face-from-face-or-color
+                     'priority nil
+                     (cdr (assoc p org-agenda-fontify-priorities)))))
               ((equal p l) 'italic)
               ((equal p h) 'bold)))
-       (org-overlay-put ov 'org-type 'org-priority)))))
+       (overlay-put ov 'org-type 'org-priority)))))
 
 (defun org-agenda-dim-blocked-tasks ()
   "Dim currently blocked TODO's in the agenda display."
-  (mapc (lambda (o) (if (eq (org-overlay-get o 'org-type) 'org-blocked-todo)
-                       (org-delete-overlay o)))
-       (org-overlays-in (point-min) (point-max)))
+  (mapc (lambda (o) (if (eq (overlay-get o 'org-type) 'org-blocked-todo)
+                       (delete-overlay o)))
+       (overlays-in (point-min) (point-max)))
   (save-excursion
     (let ((inhibit-read-only t)
          (org-depend-tag-blocked nil)
@@ -2559,7 +3067,7 @@ bind it in the options section.")
       (while (let ((pos (next-single-property-change (point) 'todo-state)))
               (and pos (goto-char (1+ pos))))
        (setq org-blocked-by-checkboxes nil invis1 invis)
-       (let ((marker (get-text-property (point) 'org-hd-marker)))
+       (let ((marker (org-get-at-bol 'org-hd-marker)))
          (when (and marker
                     (not (with-current-buffer (marker-buffer marker)
                            (save-excursion
@@ -2577,11 +3085,11 @@ bind it in the options section.")
                        (max (point-min) (1- (point-at-bol)))
                      (point-at-bol))
                  e (point-at-eol)
-                 ov (org-make-overlay b e))
+                 ov (make-overlay b e))
            (if invis1
-               (org-overlay-put ov 'invisible t)
-             (org-overlay-put ov 'face 'org-agenda-dimmed-todo-face))
-           (org-overlay-put ov 'org-type 'org-blocked-todo)))))))
+               (overlay-put ov 'invisible t)
+             (overlay-put ov 'face 'org-agenda-dimmed-todo-face))
+           (overlay-put ov 'org-type 'org-blocked-todo)))))))
 
 (defvar org-agenda-skip-function nil
   "Function to be called at each match during agenda construction.
@@ -2592,7 +3100,7 @@ This may also be a Lisp form, it will be evaluated.
 Never set this variable using `setq' or so, because then it will apply
 to all future agenda commands.  Instead, bind it with `let' to scope
 it dynamically into the agenda-constructing command.  A good way to set
-it is through options in org-agenda-custom-commands.")
+it is through options in `org-agenda-custom-commands'.")
 
 (defun org-agenda-skip ()
   "Throw to `:skip' in places that should be skipped.
@@ -2645,19 +3153,19 @@ no longer in use."
 ;;; Entry text mode
 
 (defun org-agenda-entry-text-show-here ()
-  "Add some text from te entry as context to the current line."
+  "Add some text from the entry as context to the current line."
   (let (m txt o)
-    (setq m (get-text-property (point) 'org-hd-marker))
+    (setq m (org-get-at-bol 'org-hd-marker))
     (unless (marker-buffer m)
       (error "No marker points to an entry here"))
     (setq txt (concat "\n" (org-no-properties
                            (org-agenda-get-some-entry-text
-                            m org-agenda-entry-text-maxlines))))
+                            m org-agenda-entry-text-maxlines "    > "))))
     (when (string-match "\\S-" txt)
-      (setq o (org-make-overlay (point-at-bol) (point-at-eol)))
-      (org-overlay-put o 'evaporate t)
-      (org-overlay-put o 'org-overlay-type 'agenda-entry-content)
-      (org-overlay-put o 'after-string txt))))
+      (setq o (make-overlay (point-at-bol) (point-at-eol)))
+      (overlay-put o 'evaporate t)
+      (overlay-put o 'org-overlay-type 'agenda-entry-content)
+      (overlay-put o 'after-string txt))))
 
 (defun org-agenda-entry-text-show ()
   "Add entry context for all agenda lines."
@@ -2666,7 +3174,7 @@ no longer in use."
     (goto-char (point-max))
     (beginning-of-line 1)
     (while (not (bobp))
-      (when (get-text-property (point) 'org-hd-marker)
+      (when (org-get-at-bol 'org-hd-marker)
        (org-agenda-entry-text-show-here))
       (beginning-of-line 0))))
 
@@ -2674,10 +3182,20 @@ no longer in use."
   "Remove any shown entry context."
   (delq nil
        (mapcar (lambda (o)
-                 (if (eq (org-overlay-get o 'org-overlay-type)
+                 (if (eq (overlay-get o 'org-overlay-type)
                          'agenda-entry-content)
-                     (progn (org-delete-overlay o) t)))
-               (org-overlays-in (point-min) (point-max)))))
+                     (progn (delete-overlay o) t)))
+               (overlays-in (point-min) (point-max)))))
+
+(defun org-agenda-get-day-face (date)
+  "Return the face DATE should be displayed with."
+  (or (and (functionp org-agenda-day-face-function)
+          (funcall org-agenda-day-face-function date))
+      (cond ((org-agenda-todayp date)
+            'org-agenda-date-today)
+           ((member (calendar-day-of-week date) org-agenda-weekend-days)
+            'org-agenda-date-weekend)
+           (t 'org-agenda-date))))
 
 ;;; Agenda timeline
 
@@ -2691,13 +3209,13 @@ under the current date.
 If the buffer contains an active region, only check the region for
 dates."
   (interactive "P")
-  (require 'calendar)
   (org-compile-prefix-format 'timeline)
   (org-set-sorting-strategy 'timeline)
   (let* ((dopast t)
         (dotodo include-all)
         (doclosed org-agenda-show-log)
-        (entry buffer-file-name)
+        (entry (buffer-file-name (or (buffer-base-buffer (current-buffer))
+                                     (current-buffer))))
         (date (calendar-current-date))
         (beg (if (org-region-active-p) (region-beginning) (point-min)))
         (end (if (org-region-active-p) (region-end) (point-max)))
@@ -2706,10 +3224,10 @@ dates."
                                         org-timeline-show-empty-dates))
         (org-deadline-warning-days 0)
         (org-agenda-only-exact-dates t)
-        (today (time-to-days (current-time)))
+        (today (org-today))
         (past t)
         args
-        s e rtn d emptyp wd)
+        s e rtn d emptyp)
     (setq org-agenda-redo-command
          (list 'progn
                (list 'org-switch-to-buffer-other-window (current-buffer))
@@ -2719,14 +3237,17 @@ dates."
        (setq day-numbers (delq nil (mapcar (lambda(x)
                                              (if (>= x today) x nil))
                                            day-numbers))))
-    (org-prepare-agenda (concat "Timeline "
-                               (file-name-nondirectory buffer-file-name)))
+    (org-prepare-agenda (concat "Timeline " (file-name-nondirectory entry)))
     (if doclosed (push :closed args))
     (push :timestamp args)
     (push :deadline args)
     (push :scheduled args)
     (push :sexp args)
     (if dotodo (push :todo args))
+    (insert "Timeline of file " entry "\n")
+    (add-text-properties (point-min) (point)
+                        (list 'face 'org-agenda-structure))
+    (org-agenda-mark-header-line (point-min))
     (while (setq d (pop day-numbers))
       (if (and (listp d) (eq (car d) :omitted))
          (progn
@@ -2740,8 +3261,7 @@ dates."
            (progn
              (setq past nil)
              (insert (make-string 79 ?-) "\n")))
-       (setq date (calendar-gregorian-from-absolute d)
-             wd (calendar-day-of-week date))
+       (setq date (calendar-gregorian-from-absolute d))
        (setq s (point))
        (setq rtn (and (not emptyp)
                       (apply 'org-agenda-get-day-entries entry
@@ -2755,10 +3275,9 @@ dates."
                 (funcall org-agenda-format-date date))
               "\n")
              (put-text-property s (1- (point)) 'face
-                                (if (member wd org-agenda-weekend-days)
-                                    'org-agenda-date-weekend
-                                  'org-agenda-date))
+                                (org-agenda-get-day-face date))
              (put-text-property s (1- (point)) 'org-date-line t)
+             (put-text-property s (1- (point)) 'org-agenda-date-header t)
              (if (equal d today)
                  (put-text-property s (1- (point)) 'org-today t))
              (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
@@ -2782,7 +3301,7 @@ When EMPTY is non-nil, also include days without any entries."
             (if inactive org-ts-regexp-both org-ts-regexp)))
         dates dates1 date day day1 day2 ts1 ts2)
     (if force-today
-       (setq dates (list (time-to-days (current-time)))))
+       (setq dates (list (org-today))))
     (save-excursion
       (goto-char beg)
       (while (re-search-forward re end t)
@@ -2818,14 +3337,47 @@ When EMPTY is non-nil, also include days without any entries."
 (defvar org-agenda-start-day nil  ; dynamically scoped parameter
 "Custom commands can set this variable in the options section.")
 (defvar org-agenda-last-arguments nil
-  "The arguments of the previous call to org-agenda")
+  "The arguments of the previous call to `org-agenda'.")
 (defvar org-starting-day nil) ; local variable in the agenda buffer
-(defvar org-agenda-span nil) ; local variable in the agenda buffer
+(defvar org-agenda-current-span nil
+  "The current span used in the agenda view.") ; local variable in the agenda buffer
 (defvar org-include-all-loc nil) ; local variable
-(defvar org-agenda-remove-date nil) ; dynamically scoped FIXME: not used???
+
+(defvar org-agenda-entry-types '(:deadline :scheduled :timestamp :sexp)
+  "List of types searched for when creating the daily/weekly agenda.
+This variable is a list of symbols that controls the types of
+items that appear in the daily/weekly agenda.  Allowed symbols in this
+list are are
+
+   :timestamp    List items containing a date stamp or date range matching
+                 the selected date.  This includes sexp entries in
+                 angular brackets.
+
+   :sexp         List entries resulting from plain diary-like sexps.
+
+   :deadline     List deadline due on that date.  When the date is today,
+                 also list any deadlines past due, or due within
+                `org-deadline-warning-days'.  `:deadline' must appear before
+                 `:scheduled' if the setting of
+                 `org-agenda-skip-scheduled-if-deadline-is-shown' is to have
+                 any effect.
+
+   :scheduled    List all items which are scheduled for the given date.
+                The diary for *today* also contains items which were
+                scheduled earlier and are not yet marked DONE.
+
+By default, all four types are turned on.
+
+Never set this variable globally using `setq', because then it
+will apply to all future agenda commands.  Instead, bind it with
+`let' to scope it dynamically into the the agenda-constructing
+command.  A good way to set it is through options in
+`org-agenda-custom-commands'.  For a more flexible (though
+somewhat less efficient) way of determining what is included in
+the daily/weekly agenda, see `org-agenda-skip-function'.")
 
 ;;;###autoload
-(defun org-agenda-list (&optional include-all start-day ndays)
+(defun org-agenda-list (&optional include-all start-day span)
   "Produce a daily/weekly view from all files in variable `org-agenda-files'.
 The view will be for the current day or week, but from the overview buffer
 you will be able to go to other days/weeks.
@@ -2836,38 +3388,36 @@ This feature is considered obsolete, please use the TODO list or a block
 agenda instead.
 
 With a numeric prefix argument in an interactive call, the agenda will
-span INCLUDE-ALL days.  Lisp programs should instead specify NDAYS to change
-the number of days.  NDAYS defaults to `org-agenda-ndays'.
+span INCLUDE-ALL days.  Lisp programs should instead specify SPAN to change
+the number of days.  SPAN defaults to `org-agenda-span'.
 
 START-DAY defaults to TODAY, or to the most recent match for the weekday
 given in `org-agenda-start-on-weekday'."
   (interactive "P")
   (if (and (integerp include-all) (> include-all 0))
-      (setq ndays include-all include-all nil))
-  (setq ndays (or ndays org-agenda-ndays)
-       start-day (or start-day org-agenda-start-day))
+      (setq span include-all include-all nil))
+  (setq start-day (or start-day org-agenda-start-day))
   (if org-agenda-overriding-arguments
       (setq include-all (car org-agenda-overriding-arguments)
            start-day (nth 1 org-agenda-overriding-arguments)
-           ndays (nth 2 org-agenda-overriding-arguments)))
+           span (nth 2 org-agenda-overriding-arguments)))
   (if (stringp start-day)
       ;; Convert to an absolute day number
       (setq start-day (time-to-days (org-read-date nil t start-day))))
-  (setq org-agenda-last-arguments (list include-all start-day ndays))
+  (setq org-agenda-last-arguments (list include-all start-day span))
   (org-compile-prefix-format 'agenda)
   (org-set-sorting-strategy 'agenda)
-  (require 'calendar)
-  (let* ((org-agenda-start-on-weekday
-         (if (or (equal ndays 7) (and (null ndays) (equal 7 org-agenda-ndays)))
-             org-agenda-start-on-weekday nil))
+  (let* ((span (org-agenda-ndays-to-span (or span org-agenda-ndays org-agenda-span)))
+        (today (org-today))
+        (sd (or start-day today))
+        (ndays (org-agenda-span-to-ndays span sd))
+        (org-agenda-start-on-weekday
+         (if (eq ndays 7)
+             org-agenda-start-on-weekday))
         (thefiles (org-agenda-files nil 'ifmode))
         (files thefiles)
-        (today (time-to-days
-                (time-subtract (current-time)
-                               (list 0 (* 3600 org-extend-today-until) 0))))
-        (sd (or start-day today))
         (start (if (or (null org-agenda-start-on-weekday)
-                       (< org-agenda-ndays 7))
+                       (< ndays 7))
                    sd
                  (let* ((nt (calendar-day-of-week
                              (calendar-gregorian-from-absolute sd)))
@@ -2877,24 +3427,19 @@ given in `org-agenda-start-on-weekday'."
         (day-numbers (list start))
         (day-cnt 0)
         (inhibit-redisplay (not debug-on-error))
-        s e rtn rtnall file date d start-pos end-pos todayp nd wd
-        clocktable-start clocktable-end)
+        s e rtn rtnall file date d start-pos end-pos todayp
+        clocktable-start clocktable-end filter)
     (setq org-agenda-redo-command
-         (list 'org-agenda-list (list 'quote include-all) start-day ndays))
-    ;; Make the list of days
-    (setq ndays (or ndays org-agenda-ndays)
-         nd ndays)
-    (while (> ndays 1)
-      (push (1+ (car day-numbers)) day-numbers)
-      (setq ndays (1- ndays)))
+         (list 'org-agenda-list (list 'quote include-all) start-day (list 'quote span)))
+    (dotimes (n (1- ndays))
+      (push (1+ (car day-numbers)) day-numbers))
     (setq day-numbers (nreverse day-numbers))
     (setq clocktable-start (car day-numbers)
          clocktable-end (1+ (or (org-last day-numbers) 0)))
     (org-prepare-agenda "Day/Week")
     (org-set-local 'org-starting-day (car day-numbers))
     (org-set-local 'org-include-all-loc include-all)
-    (org-set-local 'org-agenda-span
-                  (org-agenda-ndays-to-span nd))
+    (org-set-local 'org-agenda-current-span (org-agenda-ndays-to-span span))
     (when (and (or include-all org-agenda-include-all-todo)
               (member today day-numbers))
       (setq files thefiles
@@ -2907,9 +3452,11 @@ given in `org-agenda-start-on-weekday'."
                     file date :todo))
          (setq rtnall (append rtnall rtn))))
       (when rtnall
-       (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
+       (insert "All currently open TODO items:\n")
        (add-text-properties (point-min) (1- (point))
-                            (list 'face 'org-agenda-structure))
+                            (list 'face 'org-agenda-structure
+                                  'short-heading "All TODO items"))
+       (org-agenda-mark-header-line (point-min))
        (insert (org-finalize-agenda-entries rtnall) "\n")))
     (unless org-agenda-compact-blocks
       (let* ((d1 (car day-numbers))
@@ -2920,7 +3467,7 @@ given in `org-agenda-start-on-weekday'."
        (if org-agenda-overriding-header
            (insert (org-add-props (copy-sequence org-agenda-overriding-header)
                        nil 'face 'org-agenda-structure) "\n")
-         (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
+         (insert (org-agenda-span-name span)
                  "-agenda"
                  (if (< (- d2 d1) 350)
                      (if (= w1 w2)
@@ -2929,10 +3476,10 @@ given in `org-agenda-start-on-weekday'."
                    "")
                  ":\n")))
       (add-text-properties s (1- (point)) (list 'face 'org-agenda-structure
-                                               'org-date-line t)))
+                                               'org-date-line t))
+      (org-agenda-mark-header-line s))
     (while (setq d (pop day-numbers))
       (setq date (calendar-gregorian-from-absolute d)
-           wd (calendar-day-of-week date)
            s (point))
       (if (or (setq todayp (= d today))
              (and (not start-pos) (= d sd)))
@@ -2944,18 +3491,22 @@ given in `org-agenda-start-on-weekday'."
       (while (setq file (pop files))
        (catch 'nextfile
          (org-check-agenda-file file)
-         (cond
-          ((eq org-agenda-show-log 'only)
-           (setq rtn (org-agenda-get-day-entries
-                      file date :closed)))
-          (org-agenda-show-log
-           (setq rtn (org-agenda-get-day-entries
-                      file date
-                      :deadline :scheduled :timestamp :sexp :closed)))
-          (t
-           (setq rtn (org-agenda-get-day-entries
-                      file date
-                      :deadline :scheduled :sexp :timestamp))))
+         (let ((org-agenda-entry-types org-agenda-entry-types))
+           (unless org-agenda-include-deadlines
+             (setq org-agenda-entry-types
+                   (delq :deadline org-agenda-entry-types)))
+           (cond
+            ((eq org-agenda-show-log 'only)
+             (setq rtn (org-agenda-get-day-entries
+                        file date :closed)))
+            (org-agenda-show-log
+             (setq rtn (apply 'org-agenda-get-day-entries
+                              file date
+                              (append '(:closed) org-agenda-entry-types))))
+            (t
+             (setq rtn (apply 'org-agenda-get-day-entries
+                              file date
+                              org-agenda-entry-types)))))
          (setq rtnall (append rtnall rtn))))
       (if org-agenda-include-diary
          (let ((org-agenda-search-headline-for-time t))
@@ -2972,18 +3523,16 @@ given in `org-agenda-start-on-weekday'."
               (funcall org-agenda-format-date date))
             "\n")
            (put-text-property s (1- (point)) 'face
-                              (if (member wd org-agenda-weekend-days)
-                                  'org-agenda-date-weekend
-                                'org-agenda-date))
+                              (org-agenda-get-day-face date))
            (put-text-property s (1- (point)) 'org-date-line t)
+           (put-text-property s (1- (point)) 'org-agenda-date-header t)
            (put-text-property s (1- (point)) 'org-day-cnt day-cnt)
            (when todayp
-             (put-text-property s (1- (point)) 'org-today t)
-             (put-text-property s (1- (point)) 'face 'org-agenda-date-today))
+             (put-text-property s (1- (point)) 'org-today t))
            (if rtnall (insert
                        (org-finalize-agenda-entries
                         (org-agenda-add-time-grid-maybe
-                         rtnall nd todayp))
+                         rtnall ndays todayp))
                        "\n"))
            (put-text-property s (1- (point)) 'day d)
            (put-text-property s (1- (point)) 'org-day-cnt day-cnt))))
@@ -2996,6 +3545,15 @@ given in `org-agenda-start-on-weekday'."
        (setq p (plist-put p :tstart clocktable-start))
        (setq p (plist-put p :tend clocktable-end))
        (setq p (plist-put p :scope 'agenda))
+       (when (and (eq org-agenda-clockreport-mode 'with-filter)
+                  (setq filter (or org-agenda-filter-while-redo
+                                   (get 'org-agenda-filter :preset-filter))))
+         (setq p (plist-put p :tags (mapconcat (lambda (x)
+                                                 (if (string-match "[<>=]" x)
+                                                     ""
+                                                   x))
+                                               filter ""))))
+       (message "%s" (plist-get p :tags)) (sit-for 2)
        (setq tbl (apply 'org-get-clocktable p))
        (insert tbl)))
     (goto-char (point-min))
@@ -3015,7 +3573,31 @@ given in `org-agenda-start-on-weekday'."
     (message "")))
 
 (defun org-agenda-ndays-to-span (n)
-  (cond ((< n 7) 'day) ((= n 7) 'week) ((< n 32) 'month) (t 'year)))
+  "Return a span symbol for a span of N days, or N if none matches."
+  (cond ((symbolp n) n)
+       ((= n 1) 'day)
+       ((= n 7) 'week)
+       (t n)))
+
+(defun org-agenda-span-to-ndays (span start-day)
+  "Return ndays from SPAN starting at START-DAY."
+  (cond ((numberp span) span)
+       ((eq span 'day) 1)
+       ((eq span 'week) 7)
+       ((eq span 'month)
+        (let ((date (calendar-gregorian-from-absolute start-day)))
+          (calendar-last-day-of-month (car date) (caddr date))))
+       ((eq span 'year)
+        (let ((date (calendar-gregorian-from-absolute start-day)))
+          (if (calendar-leap-year-p (caddr date)) 366 365)))))
+
+(defun org-agenda-span-name (span)
+  "Return a SPAN name."
+  (if (null span)
+      ""
+    (if (symbolp span)
+       (capitalize (symbol-name span))
+      (format "%d days" span))))
 
 ;;; Agenda word search
 
@@ -3034,11 +3616,11 @@ that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
     (modify-syntax-entry ?` "." org-search-syntax-table))
   org-search-syntax-table)
 
+(defvar org-agenda-last-search-view-search-was-boolean nil)
+
 ;;;###autoload
 (defun org-search-view (&optional todo-only string edit-at)
-  "Show all entries that contain words or regular expressions.
-If the first character of the search string is an asterisks,
-search only the headlines.
+  "Show all entries that contain a phrase or words or regular expressions.
 
 With optional prefix argument TODO-ONLY, only consider entries that are
 TODO entries.  The argument STRING can be used to pass a default search
@@ -3046,19 +3628,37 @@ string into this function.  If EDIT-AT is non-nil, it means that the
 user should get a chance to edit this string, with cursor at position
 EDIT-AT.
 
-The search string is broken into \"words\" by splitting at whitespace.
-The individual words are then interpreted as a boolean expression with
-logical AND.  Words prefixed with a minus must not occur in the entry.
-Words without a prefix or prefixed with a plus must occur in the entry.
-Matching is case-insensitive and the words are enclosed by word delimiters.
-
-Words enclosed by curly braces are interpreted as regular expressions
-that must or must not match in the entry.
-
-If the search string starts with an asterisk, search only in headlines.
-If (possibly after the leading star) the search string starts with an
-exclamation mark, this also means to look at TODO entries only, an effect
-that can also be achieved with a prefix argument.
+The search string can be viewed either as a phrase that should be found as
+is, or it can be broken into a number of snippets, each of which must match
+in a Boolean way to select an entry.  The default depends on the variable
+`org-agenda-search-view-always-boolean'.
+Even if this is turned off (the default) you can always switch to
+Boolean search dynamically by preceding the first word with  \"+\" or \"-\".
+
+The default is a direct search of the whole phrase, where each space in
+the search string can expand to an arbitrary amount of whitespace,
+including newlines.
+
+If using a Boolean search, the search string is split on whitespace and
+each snippet is searched separately, with logical AND to select an entry.
+Words prefixed with a minus must *not* occur in the entry.  Words without
+a prefix or prefixed with a plus must occur in the entry.  Matching is
+case-insensitive.  Words are enclosed by word delimiters (i.e. they must
+match whole words, not parts of a word) if
+`org-agenda-search-view-force-full-words' is set (default is nil).
+
+Boolean search snippets enclosed by curly braces are interpreted as
+regular expressions that must or (when preceded with \"-\") must not
+match in the entry.  Snippets enclosed into double quotes will be taken
+as a whole, to include whitespace.
+
+- If the search string starts with an asterisk, search only in headlines.
+- If (possibly after the leading star) the search string starts with an
+  exclamation mark, this also means to look at TODO entries only, an effect
+  that can also be achieved with a prefix argument.
+- If (possibly after star and exclamation mark) the search string starts
+  with a colon, this will mean that the (non-regexp) snippets of the
+  Boolean search must match as full words.
 
 This command searches the agenda files, and in addition the files listed
 in `org-agenda-text-search-extra-files'."
@@ -3072,19 +3672,23 @@ in `org-agenda-text-search-extra-files'."
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
                      'mouse-face 'highlight
-                     'keymap org-agenda-keymap
                      'help-echo (format "mouse-2 or RET jump to location")))
+        (full-words org-agenda-search-view-force-full-words)
+        (org-agenda-text-search-extra-files org-agenda-text-search-extra-files)
         regexp rtn rtnall files file pos
-        marker category tags c neg re
+        marker category tags c neg re boolean
         ee txt beg end words regexps+ regexps- hdl-only buffer beg1 str)
     (unless (and (not edit-at)
                 (stringp string)
                 (string-match "\\S-" string))
-      (setq string (read-string "[+-]Word/{Regexp} ...: "
-                               (cond
-                                ((integerp edit-at) (cons string edit-at))
-                                (edit-at string))
-                               'org-agenda-search-history)))
+      (setq string (read-string
+                   (if org-agenda-search-view-always-boolean
+                       "[+-]Word/{Regexp} ...: "
+                     "Phrase, or [+-]Word/{Regexp} ...: ")
+                   (cond
+                    ((integerp edit-at) (cons string edit-at))
+                    (edit-at string))
+                   'org-agenda-search-history)))
     (org-set-local 'org-todo-only todo-only)
     (setq org-agenda-redo-command
          (list 'org-search-view (if todo-only t nil) string
@@ -3098,19 +3702,59 @@ in `org-agenda-text-search-extra-files'."
     (when (equal (string-to-char words) ?!)
       (setq todo-only t
            words (substring words 1)))
+    (when (equal (string-to-char words) ?:)
+      (setq full-words t
+           words (substring words 1)))
+    (if (or org-agenda-search-view-always-boolean
+           (member (string-to-char words) '(?- ?+ ?\{)))
+       (setq boolean t))
     (setq words (org-split-string words))
-    (mapc (lambda (w)
-           (setq c (string-to-char w))
-           (if (equal c ?-)
-               (setq neg t w (substring w 1))
-             (if (equal c ?+)
-                 (setq neg nil w (substring w 1))
-               (setq neg nil)))
-           (if (string-match "\\`{.*}\\'" w)
-               (setq re (substring w 1 -1))
-             (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>")))
-           (if neg (push re regexps-) (push re regexps+)))
-         words)
+    (let (www w)
+      (while (setq w (pop words))
+       (while (and (string-match "\\\\\\'" w) words)
+         (setq w (concat (substring w 0 -1) " " (pop words))))
+       (push w www))
+      (setq words (nreverse www) www nil)
+      (while (setq w (pop words))
+       (when (and (string-match "\\`[-+]?{" w)
+                  (not (string-match "}\\'" w)))
+         (while (and words (not (string-match "}\\'" (car words))))
+           (setq w (concat w " " (pop words))))
+         (setq w (concat w " " (pop words))))
+       (push w www))
+      (setq words (nreverse www)))
+    (setq org-agenda-last-search-view-search-was-boolean boolean)
+    (when boolean
+      (let (wds w)
+       (while (setq w (pop words))
+         (if (or (equal (substring w 0 1) "\"")
+                 (and (> (length w) 1)
+                      (member (substring w 0 1) '("+" "-"))
+                      (equal (substring w 1 2) "\"")))
+             (while (and words (not (equal (substring w -1) "\"")))
+               (setq w (concat w " " (pop words)))))
+         (and (string-match "\\`\\([-+]?\\)\"" w)
+              (setq w (replace-match "\\1" nil nil w)))
+         (and (equal (substring w -1) "\"") (setq w (substring w 0 -1)))
+         (push w wds))
+       (setq words (nreverse wds))))
+    (if boolean
+       (mapc (lambda (w)
+               (setq c (string-to-char w))
+               (if (equal c ?-)
+                   (setq neg t w (substring w 1))
+                 (if (equal c ?+)
+                     (setq neg nil w (substring w 1))
+                   (setq neg nil)))
+               (if (string-match "\\`{.*}\\'" w)
+                   (setq re (substring w 1 -1))
+                 (if full-words
+                     (setq re (concat "\\<" (regexp-quote (downcase w)) "\\>"))
+                   (setq re (regexp-quote (downcase w)))))
+               (if neg (push re regexps-) (push re regexps+)))
+             words)
+      (push (mapconcat (lambda (w) (regexp-quote w)) words "\\s-+")
+           regexps+))
     (setq regexps+ (sort regexps+ (lambda (a b) (> (length a) (length b)))))
     (if (not regexps+)
        (setq regexp (concat "^" org-outline-regexp))
@@ -3206,6 +3850,7 @@ in `org-agenda-text-search-extra-files'."
        (insert "Press `[', `]' to add/sub word, `{', `}' to add/sub regexp, `C-u r' to edit\n")
        (add-text-properties pos (1- (point))
                             (list 'face 'org-agenda-structure))))
+    (org-agenda-mark-header-line (point-min))
     (when rtnall
       (insert (org-finalize-agenda-entries rtnall) "\n"))
     (goto-char (point-min))
@@ -3221,17 +3866,17 @@ in `org-agenda-text-search-extra-files'."
 
 ;;;###autoload
 (defun org-todo-list (arg)
-  "Show all TODO entries from all agenda file in a single list.
+  "Show all (not done) TODO entries from all agenda file in a single list.
 The prefix arg can be used to select a specific TODO keyword and limit
 the list to these.  When using \\[universal-argument], you will be prompted
 for a keyword.  A numeric prefix directly selects the Nth keyword in
 `org-todo-keywords-1'."
   (interactive "P")
-  (require 'calendar)
   (org-compile-prefix-format 'todo)
   (org-set-sorting-strategy 'todo)
   (org-prepare-agenda "TODO")
-  (let* ((today (time-to-days (current-time)))
+  (if (and (stringp arg) (not (string-match "\\S-" arg))) (setq arg nil))
+  (let* ((today (org-today))
         (date (calendar-gregorian-from-absolute today))
         (kwds org-todo-keywords-for-agenda)
         (completion-ignore-case t)
@@ -3260,7 +3905,11 @@ for a keyword.  A numeric prefix directly selects the Nth keyword in
                    nil 'face 'org-agenda-structure) "\n")
       (insert "Global list of TODO items of type: ")
       (add-text-properties (point-min) (1- (point))
-                          (list 'face 'org-agenda-structure))
+                          (list 'face 'org-agenda-structure
+                                'short-heading
+                                (concat "ToDo: "
+                                        (or org-select-this-todo-keyword "ALL"))))
+      (org-agenda-mark-header-line (point-min))
       (setq pos (point))
       (insert (or org-select-this-todo-keyword "ALL") "\n")
       (add-text-properties pos (1- (point)) (list 'face 'org-warning))
@@ -3276,6 +3925,7 @@ for a keyword.  A numeric prefix directly selects the Nth keyword in
                kwds))
        (insert "\n"))
       (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
+    (org-agenda-mark-header-line (point-min))
     (when rtnall
       (insert (org-finalize-agenda-entries rtnall) "\n"))
     (goto-char (point-min))
@@ -3294,11 +3944,12 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
   (org-compile-prefix-format 'tags)
   (org-set-sorting-strategy 'tags)
   (let* ((org-tags-match-list-sublevels
-;??????          (if todo-only t org-tags-match-list-sublevels))
          org-tags-match-list-sublevels)
         (completion-ignore-case t)
         rtn rtnall files file pos matcher
         buffer)
+    (when (and (stringp match) (not (string-match "\\S-" match)))
+      (setq match nil))
     (setq matcher (org-make-tags-matcher match)
          match (car matcher) matcher (cdr matcher))
     (org-prepare-agenda (concat "TAGS " match))
@@ -3335,7 +3986,9 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
                    nil 'face 'org-agenda-structure) "\n")
       (insert "Headlines with TAGS match: ")
       (add-text-properties (point-min) (1- (point))
-                          (list 'face 'org-agenda-structure))
+                          (list 'face 'org-agenda-structure
+                                'short-heading
+                                (concat "Match: " match)))
       (setq pos (point))
       (insert match "\n")
       (add-text-properties pos (1- (point)) (list 'face 'org-warning))
@@ -3343,6 +3996,7 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
       (unless org-agenda-multi
        (insert "Press `C-u r' to search again with new search string\n"))
       (add-text-properties pos (1- (point)) (list 'face 'org-agenda-structure)))
+    (org-agenda-mark-header-line (point-min))
     (when rtnall
       (insert (org-finalize-agenda-entries rtnall) "\n"))
     (goto-char (point-min))
@@ -3364,7 +4018,7 @@ This variable should not be set directly, but custom commands can bind it
 in the options section.")
 
 (defun org-agenda-skip-entry-when-regexp-matches ()
-  "Checks if the current entry contains match for `org-agenda-skip-regexp'.
+  "Check if the current entry contains match for `org-agenda-skip-regexp'.
 If yes, it returns the end position of this entry, causing agenda commands
 to skip the entry but continuing the search in the subtree.  This is a
 function that can be put into `org-agenda-skip-function' for the duration
@@ -3376,7 +4030,7 @@ of a command."
     (and skip end)))
 
 (defun org-agenda-skip-subtree-when-regexp-matches ()
-  "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
+  "Check if the current subtree contains match for `org-agenda-skip-regexp'.
 If yes, it returns the end position of this tree, causing agenda commands
 to skip this subtree.  This is a function that can be put into
 `org-agenda-skip-function' for the duration of a command."
@@ -3387,7 +4041,7 @@ to skip this subtree.  This is a function that can be put into
     (and skip end)))
 
 (defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
-  "Checks if the current subtree contains match for `org-agenda-skip-regexp'.
+  "Check if the current subtree contains match for `org-agenda-skip-regexp'.
 If yes, it returns the end position of the current entry (NOT the tree),
 causing agenda commands to skip the entry but continuing the search in
 the subtree.  This is a function that can be put into
@@ -3426,10 +4080,26 @@ timestamp     Check if there is a timestamp (also deadline or scheduled)
 nottimestamp  Check if there is no timestamp (also deadline or scheduled)
 regexp        Check if regexp matches
 notregexp     Check if regexp does not match.
+todo          Check if TODO keyword matches
+nottodo       Check if TODO keyword does not match
 
 The regexp is taken from the conditions list, it must come right after
 the `regexp' or `notregexp' element.
 
+`todo' and `nottodo' accept as an argument a list of todo
+keywords, which may include \"*\" to match any todo keyword.
+
+    (org-agenda-skip-entry-if 'todo '(\"TODO\" \"WAITING\"))
+
+would skip all entries with \"TODO\" or \"WAITING\" keywords.
+
+Instead of a list a keyword class may be given
+
+    (org-agenda-skip-entry-if 'nottodo 'done)
+
+would skip entries that haven't been marked with any of \"DONE\"
+keywords. Possible classes are: `todo', `done', `any'.
+
 If any of these conditions is met, this function returns the end point of
 the entity, causing the search to continue from there.  This is a function
 that can be put into `org-agenda-skip-function' for the duration of a command."
@@ -3459,16 +4129,51 @@ that can be put into `org-agenda-skip-function' for the duration of a command."
           (re-search-forward (nth 1 m) end t))
       (and (setq m (memq 'notregexp conditions))
           (stringp (nth 1 m))
-          (not (re-search-forward (nth 1 m) end t))))
+          (not (re-search-forward (nth 1 m) end t)))
+      (and (or
+           (setq m (memq 'todo conditions))
+           (setq m (memq 'nottodo conditions)))
+          (org-agenda-skip-if-todo m end)))
      end)))
 
+(defun org-agenda-skip-if-todo (args end)
+  "Helper function for `org-agenda-skip-if', do not use it directly.
+ARGS is a list with first element either `todo' or `nottodo'.
+The remainder is either a list of TODO keywords, or a state symbol
+`todo' or `done' or `any'."
+  (let ((kw (car args))
+       (arg (cadr args))
+       todo-wds todo-re)
+    (setq todo-wds
+         (org-uniquify
+          (cond
+           ((listp arg)   ;; list of keywords
+            (if (member "*" arg)
+                (mapcar 'substring-no-properties org-todo-keywords-1)
+              arg))
+           ((symbolp arg) ;; keyword class name
+            (cond
+             ((eq arg 'todo)
+              (org-delete-all org-done-keywords
+                              (mapcar 'substring-no-properties
+                                      org-todo-keywords-1)))
+             ((eq arg 'done) org-done-keywords)
+             ((eq arg 'any)
+              (mapcar 'substring-no-properties org-todo-keywords-1)))))))
+    (setq todo-re
+         (concat "^\\*+[ \t]+\\<\\("
+                 (mapconcat 'identity todo-wds  "\\|")
+                 "\\)\\>"))
+    (if (eq kw 'todo)
+       (re-search-forward todo-re end t)
+      (not (re-search-forward todo-re end t)))))
+
 ;;;###autoload
 (defun org-agenda-list-stuck-projects (&rest ignore)
   "Create agenda view for projects that are stuck.
 Stuck projects are project that have no next actions.  For the definitions
 of what a project is and how to check if it stuck, customize the variable
-`org-stuck-projects'.
-MATCH is being ignored."
+`org-stuck-projects'."
   (interactive)
   (let* ((org-agenda-skip-function
          'org-agenda-skip-entry-when-regexp-matches-in-subtree)
@@ -3490,11 +4195,11 @@ MATCH is being ignored."
                          "\\)\\>"))
         (tags (nth 2 org-stuck-projects))
         (tags-re (if (member "*" tags)
-                     (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
+                     (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
                    (if tags
                        (concat "^\\*+ .*:\\("
                                (mapconcat 'identity tags "\\|")
-                               (org-re "\\):[[:alnum:]_@:]*[ \t]*$")))))
+                               (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
         (gen-re (nth 3 org-stuck-projects))
         (re-list
          (delq nil
@@ -3522,7 +4227,6 @@ MATCH is being ignored."
   "Get the (Emacs Calendar) diary entries for DATE."
   (require 'diary-lib)
   (let* ((diary-fancy-buffer "*temporary-fancy-diary-buffer*")
-        (fancy-diary-buffer diary-fancy-buffer)
         (diary-display-hook '(fancy-diary-display))
         (diary-display-function 'fancy-diary-display)
         (pop-up-frames nil)
@@ -3560,7 +4264,7 @@ MATCH is being ignored."
               (setq x (org-format-agenda-item "" x "Diary" nil 'time))
               ;; Extend the text properties to the beginning of the line
               (org-add-props x (text-properties-at (1- (length x)) x)
-                'type "diary" 'date date))
+                'type "diary" 'date date 'face 'org-agenda-diary))
             entries)))))
 
 (defvar org-agenda-cleanup-fancy-diary-hook nil
@@ -3605,7 +4309,6 @@ date.  It also removes lines that contain only whitespace."
   "Add text properties to string, allowing org-mode to act on it."
   (org-add-props string nil
     'mouse-face 'highlight
-    'keymap org-agenda-keymap
     'help-echo (if buffer-file-name
                   (format "mouse-2 or RET jump to diary file %s"
                           (abbreviate-file-name buffer-file-name))
@@ -3628,33 +4331,16 @@ Needed to avoid empty dates which mess up holiday display."
       (apply 'diary-add-to-list args)
     (apply 'add-to-diary-list args)))
 
+(defvar org-diary-last-run-time nil)
+
 ;;;###autoload
 (defun org-diary (&rest args)
   "Return diary information from org-files.
 This function can be used in a \"sexp\" diary entry in the Emacs calendar.
 It accesses org files and extracts information from those files to be
 listed in the diary.  The function accepts arguments specifying what
-items should be listed.  The following arguments are allowed:
-
-   :timestamp    List the headlines of items containing a date stamp or
-                date range matching the selected date.  Deadlines will
-                also be listed, on the expiration day.
-
-   :sexp         List entries resulting from diary-like sexps.
-
-   :deadline     List any deadlines past due, or due within
-                `org-deadline-warning-days'.  The listing occurs only
-                in the diary for *today*, not at any other date.  If
-                an entry is marked DONE, it is no longer listed.
-
-   :scheduled    List all items which are scheduled for the given date.
-                The diary for *today* also contains items which were
-                scheduled earlier and are not yet marked DONE.
-
-   :todo         List all TODO items from the org-file.  This may be a
-                long list - so this is not turned on by default.
-                Like deadlines, these entries only show up in the
-                diary for *today*, not at any other date.
+items should be listed.  For a list of arguments allowed here, see the
+variable `org-agenda-entry-types'.
 
 The call in the diary file should look like this:
 
@@ -3684,8 +4370,14 @@ function from a program - use `org-agenda-get-day-entries' instead."
   (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
                    (list entry)
                  (org-agenda-files t)))
+        (time (org-float-time))
         file rtn results)
-    (org-prepare-agenda-buffers files)
+    (when (or (not org-diary-last-run-time)
+             (> (- time
+                   org-diary-last-run-time)
+                3))
+      (org-prepare-agenda-buffers files))
+    (setq org-diary-last-run-time time)
     ;; If this is called during org-agenda, don't return any entries to
     ;; the calendar.  Org Agenda will list these entries itself.
     (if org-disable-agenda-to-diary (setq files nil))
@@ -3758,7 +4450,6 @@ the documentation of `org-diary'."
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
                      'mouse-face 'highlight
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -3804,20 +4495,58 @@ the documentation of `org-diary'."
     (nreverse ee)))
 
 ;;;###autoload
-(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item (&optional end)
-  "Do we have a reason to ignore this todo entry because it has a time stamp?"
+(defun org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
+  (&optional end)
+  "Do we have a reason to ignore this TODO entry because it has a time stamp?"
   (when (or org-agenda-todo-ignore-with-date
            org-agenda-todo-ignore-scheduled
-           org-agenda-todo-ignore-deadlines)
+           org-agenda-todo-ignore-deadlines
+           org-agenda-todo-ignore-timestamp)
     (setq end (or end (save-excursion (outline-next-heading) (point))))
     (save-excursion
       (or (and org-agenda-todo-ignore-with-date
               (re-search-forward org-ts-regexp end t))
          (and org-agenda-todo-ignore-scheduled
-              (re-search-forward org-scheduled-time-regexp end t))
+              (re-search-forward org-scheduled-time-regexp end t)
+              (cond
+               ((eq org-agenda-todo-ignore-scheduled 'future)
+                (> (org-days-to-time (match-string 1)) 0))
+               ((eq org-agenda-todo-ignore-scheduled 'past)
+                (<= (org-days-to-time (match-string 1)) 0))
+               (t)))
          (and org-agenda-todo-ignore-deadlines
               (re-search-forward org-deadline-time-regexp end t)
-              (org-deadline-close (match-string 1)))))))
+              (cond
+               ((memq org-agenda-todo-ignore-deadlines '(t all)) t)
+               ((eq org-agenda-todo-ignore-deadlines 'far)
+                (not (org-deadline-close (match-string 1))))
+               ((eq org-agenda-todo-ignore-deadlines 'future)
+                (> (org-days-to-time (match-string 1)) 0))
+               ((eq org-agenda-todo-ignore-deadlines 'past)
+                (<= (org-days-to-time (match-string 1)) 0))
+               (t (org-deadline-close (match-string 1)))))
+         (and org-agenda-todo-ignore-timestamp
+              (let ((buffer (current-buffer))
+                    (regexp
+                     (concat
+                      org-scheduled-time-regexp "\\|" org-deadline-time-regexp))
+                    (start (point)))
+                ;; Copy current buffer into a temporary one
+                (with-temp-buffer
+                  (insert-buffer-substring buffer start end)
+                  (goto-char (point-min))
+                  ;; Delete SCHEDULED and DEADLINE items
+                  (while (re-search-forward regexp end t)
+                    (delete-region (match-beginning 0) (match-end 0)))
+                  (goto-char (point-min))
+                  ;; No search for timestamp left
+                  (when (re-search-forward org-ts-regexp nil t)
+                    (cond
+                     ((eq org-agenda-todo-ignore-timestamp 'future)
+                      (> (org-days-to-time (match-string 1)) 0))
+                     ((eq org-agenda-todo-ignore-timestamp 'past)
+                      (<= (org-days-to-time (match-string 1)) 0))
+                     (t))))))))))
 
 (defconst org-agenda-no-heading-message
   "No heading for this item in buffer or region.")
@@ -3829,7 +4558,6 @@ the documentation of `org-diary'."
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
                      'mouse-face 'highlight
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -3883,7 +4611,7 @@ the documentation of `org-diary'."
              clockp (and org-agenda-include-inactive-timestamps
                          (or (string-match org-clock-string tmp)
                              (string-match "]-+\\'" tmp)))
-             todo-state (org-get-todo-state)
+             todo-state (ignore-errors (org-get-todo-state))
              donep (member todo-state org-done-keywords))
        (if (or scheduledp deadlinep closedp clockp
                (and donep org-agenda-skip-timestamp-if-done))
@@ -3902,7 +4630,7 @@ the documentation of `org-diary'."
            (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
            (setq head (match-string 1))
            (setq txt (org-format-agenda-item
-                      (if inactivep "[" nil)
+                      (if inactivep org-agenda-inactive-leader nil)
                       head category tags timestr nil
                       remove-re)))
          (setq priority (org-get-priority txt))
@@ -3923,7 +4651,6 @@ the documentation of `org-diary'."
   (require 'diary-lib)
   (let* ((props (list 'face nil
                      'mouse-face 'highlight
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -3948,19 +4675,46 @@ the documentation of `org-diary'."
                category (org-get-category beg)
                todo-state (org-get-todo-state))
 
-         (if (string-match "\\S-" result)
-             (setq txt result)
-           (setq txt "SEXP entry returned empty string"))
-
-         (setq txt (org-format-agenda-item
-                     "" txt category tags 'time))
-         (org-add-props txt props 'org-marker marker)
-         (org-add-props txt nil
-           'org-category category 'date date 'todo-state todo-state
-           'type "sexp")
-         (push txt ee))))
+         (dolist (r (if (stringp result)
+                        (list result)
+                      result)) ;; we expect a list here
+           (if (string-match "\\S-" r)
+               (setq txt r)
+             (setq txt "SEXP entry returned empty string"))
+
+           (setq txt (org-format-agenda-item
+                      "" txt category tags 'time))
+           (org-add-props txt props 'org-marker marker)
+           (org-add-props txt nil
+             'org-category category 'date date 'todo-state todo-state
+             'type "sexp")
+           (push txt ee)))))
     (nreverse ee)))
 
+(defun org-diary-class (m1 d1 y1 m2 d2 y2 dayname &rest skip-weeks)
+  "Entry applies if date is between dates on DAYNAME, but skips SKIP-WEEKS.
+The order of the first 2 times 3 arguments depends on the variable
+`calendar-date-style' or, if that is not defined, on `european-calendar-style'.
+So for American calendars, give this as MONTH DAY YEAR, for European as
+DAY MONTH YEAR, and for ISO as YEAR MONTH DAY.
+DAYNAME is a number between 0 (Sunday) and 6 (Saturday).  SKIP-WEEKS
+is any number of ISO weeks in the block period for which the item should
+be skipped."
+  (let* ((date1 (calendar-absolute-from-gregorian
+                (org-order-calendar-date-args m1 d1 y1)))
+        (date2 (calendar-absolute-from-gregorian
+                (org-order-calendar-date-args m2 d2 y2)))
+        (d (calendar-absolute-from-gregorian date)))
+    (and
+     (<= date1 d)
+     (<= d date2)
+     (= (calendar-day-of-week date) dayname)
+     (or (not skip-weeks)
+        (progn
+          (require 'cal-iso)
+          (not (member (car (calendar-iso-from-absolute d)) skip-weeks))))
+     entry)))
+
 (defalias 'org-get-closed 'org-agenda-get-progress)
 (defun org-agenda-get-progress ()
   "Return the logged TODO entries for agenda display."
@@ -3968,7 +4722,6 @@ the documentation of `org-diary'."
                      'org-not-done-regexp org-not-done-regexp
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -4019,15 +4772,15 @@ the documentation of `org-diary'."
                     (setq clocked (match-string 2 rest)))
            (setq clocked "-")))
        (save-excursion
+         (setq extra nil)
          (cond
-          ((not org-agenda-log-mode-add-notes) (setq extra nil))
+          ((not org-agenda-log-mode-add-notes))
           (statep
            (and (looking-at ".*\n[ \t]*\\([^-\n \t].*?\\)[ \t]*$")
                 (setq extra (match-string 1))))
           (clockp
            (and (looking-at ".*\n[ \t]*-[ \t]+\\([^-\n \t].*?\\)[ \t]*$")
-                (setq extra (match-string 1))))
-          (t (setq extra nil)))
+                (setq extra (match-string 1)))))
          (if (not (re-search-backward "^\\*+ " nil t))
              (setq txt org-agenda-no-heading-message)
            (goto-char (match-beginning 0))
@@ -4062,7 +4815,6 @@ the documentation of `org-diary'."
                      'org-not-done-regexp org-not-done-regexp
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -4070,11 +4822,22 @@ the documentation of `org-diary'."
         (todayp (org-agenda-todayp date)) ; DATE bound by calendar
         (d1 (calendar-absolute-from-gregorian date))  ; DATE bound by calendar
         d2 diff dfrac wdays pos pos1 category tags
+        suppress-prewarning
         ee txt head face s todo-state upcomingp donep timestr)
     (goto-char (point-min))
     (while (re-search-forward regexp nil t)
+      (setq suppress-prewarning nil)
       (catch :skip
        (org-agenda-skip)
+       (when (and org-agenda-skip-deadline-prewarning-if-scheduled
+                  (save-match-data
+                    (string-match org-scheduled-time-regexp
+                                  (buffer-substring (point-at-bol)
+                                                    (point-at-eol)))))
+         (setq suppress-prewarning
+               (if (integerp org-agenda-skip-deadline-prewarning-if-scheduled)
+                   org-agenda-skip-deadline-prewarning-if-scheduled
+                 0)))
        (setq s (match-string 1)
              txt nil
              pos (1- (match-beginning 1))
@@ -4082,15 +4845,18 @@ the documentation of `org-diary'."
                  (match-string 1) d1 'past
                  org-agenda-repeating-timestamp-show-all)
              diff (- d2 d1)
-             wdays (org-get-wdays s)
+             wdays (if suppress-prewarning
+                       (let ((org-deadline-warning-days suppress-prewarning))
+                         (org-get-wdays s))
+                     (org-get-wdays s))
              dfrac (/ (* 1.0 (- wdays diff)) (max wdays 1))
              upcomingp (and todayp (> diff 0)))
        ;; When to show a deadline in the calendar:
        ;; If the expiration is within wdays warning time.
        ;; Past-due deadlines are only shown on the current date
-       (if (or (and (<= diff wdays)
-                    (and todayp (not org-agenda-only-exact-dates)))
-               (= diff 0))
+       (if (and (or (and (<= diff wdays)
+                         (and todayp (not org-agenda-only-exact-dates)))
+                    (= diff 0)))
            (save-excursion
              (setq todo-state (org-get-todo-state))
              (setq donep (member todo-state org-done-keywords))
@@ -4156,7 +4922,6 @@ FRACTION is what fraction of the head-warning time has passed."
                      'org-complex-heading-regexp org-complex-heading-regexp
                      'done-face 'org-agenda-done
                      'mouse-face 'highlight
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -4166,11 +4931,11 @@ FRACTION is what fraction of the head-warning time has passed."
         mm
         (deadline-position-alist
          (mapcar (lambda (a) (and (setq mm (get-text-property
-                                            0 'org-hd-marker a))
-                                  (cons (marker-position mm) a)))
+                                       0 'org-hd-marker a))
+                             (cons (marker-position mm) a)))
                  deadline-results))
         d2 diff pos pos1 category tags donep
-        ee txt head pastschedp todo-state face timestr s)
+        ee txt head pastschedp todo-state face timestr s habitp)
     (goto-char (point-min))
     (while (re-search-forward regexp nil t)
       (catch :skip
@@ -4185,60 +4950,73 @@ FRACTION is what fraction of the head-warning time has passed."
        (setq pastschedp (and todayp (< diff 0)))
        ;; When to show a scheduled item in the calendar:
        ;; If it is on or past the date.
-       (if (or (and (< diff 0)
-                    (< (abs diff) org-scheduled-past-days)
-                    (and todayp (not org-agenda-only-exact-dates)))
-               (= diff 0))
-           (save-excursion
-             (setq todo-state (org-get-todo-state))
-             (setq donep (member todo-state org-done-keywords))
-             (if (and donep
-                      (or org-agenda-skip-scheduled-if-done
-                          (not (= diff 0))))
-                 (setq txt nil)
-               (setq category (org-get-category))
-               (if (not (re-search-backward "^\\*+[ \t]+" nil t))
-                   (setq txt org-agenda-no-heading-message)
-                 (goto-char (match-end 0))
-                 (setq pos1 (match-beginning 0))
+       (when (or (and (< diff 0)
+                      (< (abs diff) org-scheduled-past-days)
+                      (and todayp (not org-agenda-only-exact-dates)))
+                 (= diff 0))
+         (save-excursion
+           (setq todo-state (org-get-todo-state))
+           (setq donep (member todo-state org-done-keywords))
+           (setq habitp (and (functionp 'org-is-habit-p)
+                             (org-is-habit-p)))
+           (if (and donep
+                    (or habitp org-agenda-skip-scheduled-if-done
+                        (not (= diff 0))))
+               (setq txt nil)
+             (setq category (org-get-category))
+             (if (not (re-search-backward "^\\*+[ \t]+" nil t))
+                 (setq txt org-agenda-no-heading-message)
+               (goto-char (match-end 0))
+               (setq pos1 (match-beginning 0))
+               (if habitp
+                   (if (or (not org-habit-show-habits)
+                           (and (not todayp)
+                                org-habit-show-habits-only-for-today))
+                       (throw :skip nil))
                  (if (and
                       (or (eq t org-agenda-skip-scheduled-if-deadline-is-shown)
                           (and org-agenda-skip-scheduled-if-deadline-is-shown
                                pastschedp))
                       (setq mm (assoc pos1 deadline-position-alist)))
-                     (throw :skip nil))
-                 (setq tags (org-get-tags-at))
-                 (setq head (buffer-substring-no-properties
-                             (point)
-                             (progn (skip-chars-forward "^\r\n") (point))))
-                 (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
-                     (setq timestr
-                           (concat (substring s (match-beginning 1)) " "))
-                   (setq timestr 'time))
-                 (setq txt (org-format-agenda-item
-                            (if (= diff 0)
-                                (car org-agenda-scheduled-leaders)
-                              (format (nth 1 org-agenda-scheduled-leaders)
-                                      (- 1 diff)))
-                            head category tags
-                            (if (not (= diff 0)) nil timestr)))))
-             (when txt
-               (setq face
-                     (cond
-                      (pastschedp 'org-scheduled-previously)
-                      (todayp 'org-scheduled-today)
-                      (t 'org-scheduled)))
-               (org-add-props txt props
-                 'undone-face face
-                 'face (if donep 'org-agenda-done face)
-                 'org-marker (org-agenda-new-marker pos)
-                 'org-hd-marker (org-agenda-new-marker pos1)
-                 'type (if pastschedp "past-scheduled" "scheduled")
-                 'date (if pastschedp d2 date)
-                 'priority (+ 94 (- 5 diff) (org-get-priority txt))
-                 'org-category category
-                 'todo-state todo-state)
-               (push txt ee))))))
+                     (throw :skip nil)))
+               (setq tags (org-get-tags-at))
+               (setq head (buffer-substring-no-properties
+                           (point)
+                           (progn (skip-chars-forward "^\r\n") (point))))
+               (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
+                   (setq timestr
+                         (concat (substring s (match-beginning 1)) " "))
+                 (setq timestr 'time))
+               (setq txt (org-format-agenda-item
+                          (if (= diff 0)
+                              (car org-agenda-scheduled-leaders)
+                            (format (nth 1 org-agenda-scheduled-leaders)
+                                    (- 1 diff)))
+                          head category tags
+                          (if (not (= diff 0)) nil timestr)
+                          nil nil habitp))))
+           (when txt
+             (setq face
+                   (cond
+                    ((and (not habitp) pastschedp)
+                     'org-scheduled-previously)
+                    (todayp 'org-scheduled-today)
+                    (t 'org-scheduled))
+                   habitp (and habitp (org-habit-parse-todo)))
+             (org-add-props txt props
+               'undone-face face
+               'face (if donep 'org-agenda-done face)
+               'org-marker (org-agenda-new-marker pos)
+               'org-hd-marker (org-agenda-new-marker pos1)
+               'type (if pastschedp "past-scheduled" "scheduled")
+               'date (if pastschedp d2 date)
+               'priority (if habitp
+                             (org-habit-get-priority habitp)
+                           (+ 94 (- 5 diff) (org-get-priority txt)))
+               'org-category category
+               'org-habit-p habitp
+               'todo-state todo-state)
+             (push txt ee))))))
     (nreverse ee)))
 
 (defun org-agenda-get-blocks ()
@@ -4248,7 +5026,6 @@ FRACTION is what fraction of the head-warning time has passed."
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
                      'mouse-face 'highlight
-                     'keymap org-agenda-keymap
                      'help-echo
                      (format "mouse-2 or RET jump to org file %s"
                              (abbreviate-file-name buffer-file-name))))
@@ -4283,13 +5060,20 @@ FRACTION is what fraction of the head-warning time has passed."
                (setq tags (org-get-tags-at))
                (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
                (setq head (match-string 1))
-               (setq txt (org-format-agenda-item
-                          (format
-                           (nth (if (= d1 d2) 0 1)
-                                org-agenda-timerange-leaders)
-                           (1+ (- d0 d1)) (1+ (- d2 d1)))
-                          head category tags
-                          (if (= d0 d1) timestr))))
+               (let ((remove-re
+                      (if org-agenda-remove-timeranges-from-blocks
+                          (concat
+                           "<" (regexp-quote s1) ".*?>"
+                           "--"
+                           "<" (regexp-quote s2) ".*?>")
+                        nil)))
+                 (setq txt (org-format-agenda-item
+                            (format
+                             (nth (if (= d1 d2) 0 1)
+                                  org-agenda-timerange-leaders)
+                             (1+ (- d0 d1)) (1+ (- d2 d1)))
+                            head category tags
+                            timestr nil remove-re))))
              (org-add-props txt props
                'org-marker marker 'org-hd-marker hdmarker
                'type "block" 'date date
@@ -4311,9 +5095,21 @@ The flag is set if the currently compiled format contains a `%T'.")
 (defvar org-prefix-has-effort nil
   "A flag, set by `org-compile-prefix-format'.
 The flag is set if the currently compiled format contains a `%e'.")
+(defvar org-prefix-category-length nil
+  "Used by `org-compile-prefix-format' to remember the category field width.")
+(defvar org-prefix-category-max-length nil
+  "Used by `org-compile-prefix-format' to remember the category field width.")
+
+(defun org-agenda-get-category-icon (category)
+  "Return an image for CATEGORY according to `org-agenda-category-icon-alist'."
+  (dolist (entry org-agenda-category-icon-alist)
+    (when (org-string-match-p (car entry) category)
+      (if (listp (cadr entry))
+         (return (cadr entry))
+      (return (apply 'create-image (cdr entry)))))))
 
 (defun org-format-agenda-item (extra txt &optional category tags dotime
-                                    noprefix remove-re)
+                                    noprefix remove-re habitp)
   "Format TXT to be inserted into the agenda buffer.
 In particular, it adds the prefix and corresponding text properties.  EXTRA
 must be a string and replaces the `%s' specifier in the prefix format.
@@ -4329,15 +5125,24 @@ Any match of REMOVE-RE will be removed from TXT."
   (save-match-data
     ;; Diary entries sometimes have extra whitespace at the beginning
     (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
-    (when org-agenda-show-inherited-tags
-      ;; Fix the tags part in txt
-      (setq txt (org-agenda-add-inherited-tags txt tags)))
+
+    ;; Fix the tags part in txt
+    (setq txt (org-agenda-fix-displayed-tags
+              txt tags
+              org-agenda-show-inherited-tags
+              org-agenda-hide-tags-regexp))
     (let* ((category (or category
-                        org-category
+                        (if (stringp org-category)
+                            org-category
+                          (and org-category (symbol-name org-category)))
                         (if buffer-file-name
                             (file-name-sans-extension
                              (file-name-nondirectory buffer-file-name))
                           "")))
+          (category-icon (org-agenda-get-category-icon category))
+          (category-icon (if category-icon
+                             (propertize " " 'display category-icon)
+                           ""))
           ;; time, tag, effort are needed for the eval of the prefix format
           (tag (if tags (nth (1- (length tags)) tags) ""))
           time effort neffort
@@ -4345,8 +5150,8 @@ Any match of REMOVE-RE will be removed from TXT."
                           (if (stringp dotime) dotime "")
                           (and org-agenda-search-headline-for-time txt))))
           (time-of-day (and dotime (org-get-time-of-day ts)))
-          stamp plain s0 s1 s2 t1 t2 rtn srp
-          duration)
+          stamp plain s0 s1 s2 t1 t2 rtn srp l
+          duration thecategory)
       (and (org-mode-p) buffer-file-name
           (add-to-list 'org-agenda-contributing-files buffer-file-name))
       (when (and dotime time-of-day)
@@ -4393,7 +5198,7 @@ Any match of REMOVE-RE will be removed from TXT."
          (setq h (/ m 60) m (- m (* h 60)))
          (setq s2 (format "%02d:%02d" h m))))
 
-      (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
+      (when (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
                          txt)
        ;; Tags are in the string
        (if (or (eq org-agenda-remove-tags t)
@@ -4423,18 +5228,39 @@ Any match of REMOVE-RE will be removed from TXT."
       (if noprefix
          (setq rtn txt)
        ;; Prepare the variables needed in the eval of the compiled format
-       (setq time (cond (s2 (concat s1 "-" s2))
-                        (s1 (concat s1 "......"))
+       (setq time (cond (s2 (concat
+                             (org-agenda-time-of-day-to-ampm-maybe s1)
+                             "-" (org-agenda-time-of-day-to-ampm-maybe s2)
+                             (if org-agenda-timegrid-use-ampm " ")))
+                        (s1 (concat
+                             (org-agenda-time-of-day-to-ampm-maybe s1)
+                             (if org-agenda-timegrid-use-ampm
+                                 "........ "
+                               "......")))
                         (t ""))
-             extra (or extra "")
-             category (if (symbolp category) (symbol-name category) category))
+             extra (or (and (not habitp) extra) "")
+             category (if (symbolp category) (symbol-name category) category)
+             thecategory (copy-sequence category))
+       (if (string-match org-bracket-link-regexp category)
+           (progn
+             (setq l (if (match-end 3)
+                         (- (match-end 3) (match-beginning 3))
+                       (- (match-end 1) (match-beginning 1))))
+             (when (< l (or org-prefix-category-length 0))
+               (setq category (copy-sequence category))
+               (org-add-props category nil
+                 'extra-space (make-string
+                               (- org-prefix-category-length l 1) ?\ ))))
+         (if (and org-prefix-category-max-length
+                  (>= (length category) org-prefix-category-max-length))
+             (setq category (substring category 0 (1- org-prefix-category-max-length)))))
        ;; Evaluate the compiled format
        (setq rtn (concat (eval org-prefix-format-compiled) txt)))
 
       ;; And finally add the text properties
       (remove-text-properties 0 (length rtn) '(line-prefix t wrap-prefix t) rtn)
       (org-add-props rtn nil
-       'org-category (downcase category)
+       'org-category (if thecategory (downcase thecategory) category)
        'tags (mapcar 'org-downcase-keep-props tags)
        'org-highest-priority org-highest-priority
        'org-lowest-priority org-lowest-priority
@@ -4448,27 +5274,37 @@ Any match of REMOVE-RE will be removed from TXT."
        'extra extra
        'dotime dotime))))
 
-(defun org-agenda-add-inherited-tags (txt tags)
-  "Remove tags string from TXT, and add complete list of tags.
-The new list includes inherited tags.  If any inherited tags are present,
-a double colon separates inherited tags from local tags."
-  (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$") txt)
-      (setq txt (substring txt 0 (match-beginning 0))))
-  (when tags
-    (let ((have-i (get-text-property 0 'inherited (car tags)))
-         i)
-      (setq txt (concat txt " :"
-                       (mapconcat
-                        (lambda (x)
-                          (setq i (get-text-property 0 'inherited x))
-                          (if (and have-i (not i))
-                              (progn
-                                (setq have-i nil)
-                                (concat ":" x))
-                            x))
-                        tags ":")
-                       (if have-i "::" ":")))))
-  txt)
+(defun org-agenda-fix-displayed-tags (txt tags add-inherited hide-re)
+  "Remove tags string from TXT, and add a modified list of tags.
+The modified list may contain inherited tags, and tags matched by
+`org-agenda-hide-tags-regexp' will be removed."
+  (when (or add-inherited hide-re)
+    (if (string-match (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") txt)
+       (setq txt (substring txt 0 (match-beginning 0))))
+    (setq tags
+         (delq nil
+               (mapcar (lambda (tg)
+                         (if (or (and hide-re (string-match hide-re tg))
+                                 (and (not add-inherited)
+                                      (get-text-property 0 'inherited tg)))
+                             nil
+                           tg))
+                       tags)))
+    (when tags
+      (let ((have-i (get-text-property 0 'inherited (car tags)))
+           i)
+       (setq txt (concat txt " :"
+                         (mapconcat
+                          (lambda (x)
+                            (setq i (get-text-property 0 'inherited x))
+                            (if (and have-i (not i))
+                                (progn
+                                  (setq have-i nil)
+                                  (concat ":" x))
+                              x))
+                          tags ":")
+                         (if have-i "::" ":"))))))
+    txt)
 
 (defun org-downcase-keep-props (s)
   (let ((props (text-properties-at 0 s)))
@@ -4499,13 +5335,13 @@ a double colon separates inherited tags from local tags."
          (throw 'exit list))
       (while (setq time (pop gridtimes))
        (unless (and remove (member time have))
-         (setq time (int-to-string time))
+         (setq time (replace-regexp-in-string " " "0" (format "%04s" time)))
          (push (org-format-agenda-item
                 nil string "" nil
                 (concat (substring time 0 -2) ":" (substring time -2)))
                new)
          (put-text-property
-          1 (length (car new)) 'face 'org-time-grid (car new))))
+          2 (length (car new)) 'face 'org-time-grid (car new))))
       (if (member 'time-up org-agenda-sorting-strategy-selected)
          (append new list)
        (append list new)))))
@@ -4515,7 +5351,7 @@ a double colon separates inherited tags from local tags."
 The resulting form is returned and stored in the variable
 `org-prefix-format-compiled'."
   (setq org-prefix-has-time nil org-prefix-has-tag nil
-       org-prefix-has-effort nil)
+       org-prefix-category-length nil  org-prefix-has-effort nil)
   (let ((s (cond
            ((stringp org-agenda-prefix-format)
             org-agenda-prefix-format)
@@ -4524,11 +5360,11 @@ The resulting form is returned and stored in the variable
            (t "  %-12:c%?-12t% s")))
        (start 0)
        varform vars var e c f opt)
-    (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctse]\\)"
+    (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([ctsei]\\)"
                         s start)
       (setq var (cdr (assoc (match-string 4 s)
                            '(("c" . category) ("t" . time) ("s" . extra)
-                             ("T" . tag) ("e" . effort))))
+                             ("i" . category-icon) ("T" . tag) ("e" . effort))))
            c (or (match-string 3 s) "")
            opt (match-beginning 1)
            start (1+ (match-beginning 0)))
@@ -4536,12 +5372,20 @@ The resulting form is returned and stored in the variable
       (if (equal var 'tag)  (setq org-prefix-has-tag  t))
       (if (equal var 'effort) (setq org-prefix-has-effort t))
       (setq f (concat "%" (match-string 2 s) "s"))
+      (when (equal var 'category)
+       (setq org-prefix-category-length
+             (floor (abs (string-to-number (match-string 2 s)))))
+       (setq org-prefix-category-max-length
+             (let ((x (match-string 2 s)))
+               (save-match-data
+                 (if (string-match "\\.[0-9]+" x)
+                     (string-to-number (substring (match-string 0 x) 1)))))))
       (if opt
          (setq varform
                `(if (equal "" ,var)
                     ""
                   (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
-       (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
+       (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c (get-text-property 0 'extra-space ,var))))))
       (setq s (replace-match "%s" t nil s))
       (push varform vars))
     (setq vars (nreverse vars))
@@ -4577,26 +5421,51 @@ HH:MM."
                    (mod h1 24) h1))
            (t0 (+ (* 100 h2) m))
            (t1 (concat (if (>= h1 24) "+" " ")
+                       (if (and org-agenda-time-leading-zero
+                                (< t0 1000)) "0" "")
                        (if (< t0 100) "0" "")
                        (if (< t0 10)  "0" "")
                        (int-to-string t0))))
        (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
 
+(defvar org-agenda-before-sorting-filter-function nil
+  "Function to be applied to agenda items prior to sorting.
+Prior to sorting also means just before they are inserted into the agenda.
+
+To aid sorting, you may revisit the original entries and add more text
+properties which will later be used by the sorting functions.
+
+The function should take a string argument, an agenda line.
+It has access to the text properties in that line, which contain among
+other things, the property `org-hd-marker' that points to the entry
+where the line comes from.  Note that not all lines going into the agenda
+have this property, only most.
+
+The function should return the modified string.  It is probably best
+to ONLY change text properties.
+
+You can also use this function as a filter, by returning nil for lines
+you don't want to have in the agenda at all.  For this application, you
+could bind the variable in the options section of a custom command.")
+
 (defun org-finalize-agenda-entries (list &optional nosort)
   "Sort and concatenate the agenda items."
   (setq list (mapcar 'org-agenda-highlight-todo list))
   (if nosort
       list
+    (when org-agenda-before-sorting-filter-function
+      (setq list (delq nil (mapcar org-agenda-before-sorting-filter-function list))))
     (mapconcat 'identity (sort list 'org-entries-lessp) "\n")))
 
 (defun org-agenda-highlight-todo (x)
   (let ((org-done-keywords org-done-keywords-for-agenda)
+       (case-fold-search nil)
         re pl)
     (if (eq x 'line)
        (save-excursion
          (beginning-of-line 1)
-         (setq re (get-text-property (point) 'org-todo-regexp))
-         (goto-char (+ (point) (or (get-text-property (point) 'prefix-length) 0)))
+         (setq re (org-get-at-bol 'org-todo-regexp))
+         (goto-char (+ (point) (or (org-get-at-bol 'prefix-length) 0)))
          (when (looking-at (concat "[ \t]*\\.*\\(" re "\\) +"))
            (add-text-properties (match-beginning 0) (match-end 1)
                                 (list 'face (org-get-todo-face 1)))
@@ -4613,11 +5482,12 @@ HH:MM."
         (or (match-end 1) (match-end 0)) (match-end 0)
         (list 'face (org-get-todo-face (match-string 2 x)))
         x)
-       (setq x (concat (substring x 0 (match-end 1))
-                       (format org-agenda-todo-keyword-format
-                               (match-string 2 x))
-                       " "
-                       (substring x (match-end 3)))))
+       (when (match-end 1)
+         (setq x (concat (substring x 0 (match-end 1))
+                         (format org-agenda-todo-keyword-format
+                                 (match-string 2 x))
+                         (org-add-props " " (text-properties-at 0 x))
+                         (substring x (match-end 3))))))
       x)))
 
 (defsubst org-cmp-priority (a b)
@@ -4668,6 +5538,28 @@ HH:MM."
          ((< lb la) +1)
          (t nil))))
 
+(defsubst org-cmp-alpha (a b)
+  "Compare the headlines, alphabetically."
+  (let* ((pla (get-text-property 0 'prefix-length a))
+        (plb (get-text-property 0 'prefix-length b))
+        (ta (and pla (substring a pla)))
+        (tb (and plb (substring b plb))))
+    (when pla
+      (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp a) "")
+                               "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") ta)
+         (setq ta (substring ta (match-end 0))))
+      (setq ta (downcase ta)))
+    (when plb
+      (if (string-match (concat "\\`[ \t]*" (or (get-text-property 0 'org-todo-regexp b) "")
+                               "\\([ \t]*\\[[a-zA-Z0-9]\\]\\)? *") tb)
+         (setq tb (substring tb (match-end 0))))
+      (setq tb (downcase tb)))
+    (cond ((not ta) +1)
+         ((not tb) -1)
+         ((string-lessp ta tb) -1)
+         ((string-lessp tb ta) +1)
+         (t nil))))
+
 (defsubst org-cmp-tag (a b)
   "Compare the string values of the first tags of A and B."
   (let ((ta (car (last (get-text-property 1 'tags a))))
@@ -4687,25 +5579,50 @@ HH:MM."
          ((< tb ta) +1)
          (t nil))))
 
+(defsubst org-cmp-habit-p (a b)
+  "Compare the todo states of strings A and B."
+  (let ((ha (get-text-property 1 'org-habit-p a))
+       (hb (get-text-property 1 'org-habit-p b)))
+    (cond ((and ha (not hb)) -1)
+         ((and (not ha) hb) +1)
+         (t nil))))
+
+(defsubst org-em (x y list) (or (memq x list) (memq y list)))
+
 (defun org-entries-lessp (a b)
   "Predicate for sorting agenda entries."
   ;; The following variables will be used when the form is evaluated.
   ;; So even though the compiler complains, keep them.
-  (let* ((time-up (org-cmp-time a b))
-        (time-down (if time-up (- time-up) nil))
-        (priority-up (org-cmp-priority a b))
-        (priority-down (if priority-up (- priority-up) nil))
-        (effort-up (org-cmp-effort a b))
-        (effort-down (if effort-up (- effort-up) nil))
-        (category-up (org-cmp-category a b))
-        (category-down (if category-up (- category-up) nil))
-        (category-keep (if category-up +1 nil))
-        (tag-up (org-cmp-tag a b))
-        (tag-down (if tag-up (- tag-up) nil))
-        (todo-state-up (org-cmp-todo-state a b))
+  (let* ((ss org-agenda-sorting-strategy-selected)
+        (time-up         (and (org-em 'time-up 'time-down ss)
+                              (org-cmp-time a b)))
+        (time-down       (if time-up (- time-up) nil))
+        (priority-up     (and (org-em 'priority-up 'priority-down ss)
+                              (org-cmp-priority a b)))
+        (priority-down   (if priority-up (- priority-up) nil))
+        (effort-up       (and (org-em 'effort-up 'effort-down ss)
+                              (org-cmp-effort a b)))
+        (effort-down     (if effort-up (- effort-up) nil))
+        (category-up     (and (or (org-em 'category-up 'category-down ss)
+                                  (memq 'category-keep ss))
+                              (org-cmp-category a b)))
+        (category-down   (if category-up (- category-up) nil))
+        (category-keep   (if category-up +1 nil))
+        (tag-up          (and (org-em 'tag-up 'tag-down ss)
+                              (org-cmp-tag a b)))
+        (tag-down        (if tag-up (- tag-up) nil))
+        (todo-state-up   (and (org-em 'todo-state-up 'todo-state-down ss)
+                              (org-cmp-todo-state a b)))
         (todo-state-down (if todo-state-up (- todo-state-up) nil))
+        (habit-up        (and (org-em 'habit-up 'habit-down ss)
+                              (org-cmp-habit-p a b)))
+        (habit-down      (if habit-up (- habit-up) nil))
+        (alpha-up        (and (org-em 'alpha-up 'alpha-down ss)
+                              (org-cmp-alpha a b)))
+        (alpha-down      (if alpha-up (- alpha-up) nil))
+        (need-user-cmp   (org-em 'user-defined-up 'user-defined-down ss))
         user-defined-up user-defined-down)
-    (if (and org-agenda-cmp-user-defined
+    (if (and need-user-cmp org-agenda-cmp-user-defined
             (functionp org-agenda-cmp-user-defined))
        (setq user-defined-up
              (funcall org-agenda-cmp-user-defined a b)
@@ -4716,12 +5633,12 @@ HH:MM."
 
 ;;; Agenda restriction lock
 
-(defvar org-agenda-restriction-lock-overlay (org-make-overlay 1 1)
-  "Overlay to mark the headline to which arenda commands are restricted.")
-(org-overlay-put org-agenda-restriction-lock-overlay
-                'face 'org-agenda-restriction-lock)
-(org-overlay-put org-agenda-restriction-lock-overlay
-                'help-echo "Agendas are currently limited to this subtree.")
+(defvar org-agenda-restriction-lock-overlay (make-overlay 1 1)
+  "Overlay to mark the headline to which agenda commands are restricted.")
+(overlay-put org-agenda-restriction-lock-overlay
+            'face 'org-agenda-restriction-lock)
+(overlay-put org-agenda-restriction-lock-overlay
+            'help-echo "Agendas are currently limited to this subtree.")
 (org-detach-overlay org-agenda-restriction-lock-overlay)
 
 (defun org-agenda-set-restriction-lock (&optional type)
@@ -4744,7 +5661,7 @@ in the file.  Otherwise, restriction will be to the current subtree."
        (put 'org-agenda-files 'org-restrict
             (list (buffer-file-name (buffer-base-buffer))))
        (org-back-to-heading t)
-       (org-move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
+       (move-overlay org-agenda-restriction-lock-overlay (point) (point-at-eol))
        (move-marker org-agenda-restrict-begin (point))
        (move-marker org-agenda-restrict-end
                     (save-excursion (org-end-of-subtree t)))
@@ -4802,15 +5719,21 @@ If ERROR is non-nil, throw an error, otherwise just return nil."
   (interactive)
   (if org-agenda-columns-active
       (org-columns-quit)
-    (if (window-dedicated-p) (delete-other-windows))
     (let ((buf (current-buffer)))
-      (and (not (eq org-agenda-window-setup 'current-window))
-           (not (one-window-p))
-           (delete-window))
-      (kill-buffer buf)
-      (org-agenda-reset-markers)
-      (org-columns-remove-overlays)
-      (setq org-agenda-archives-mode nil))
+      (if (eq org-agenda-window-setup 'other-frame)
+         (progn
+           (kill-buffer buf)
+           (org-agenda-reset-markers)
+           (org-columns-remove-overlays)
+           (setq org-agenda-archives-mode nil)
+           (delete-frame))
+       (and (not (eq org-agenda-window-setup 'current-window))
+            (not (one-window-p))
+            (delete-window))
+       (kill-buffer buf)
+       (org-agenda-reset-markers)
+       (org-columns-remove-overlays)
+       (setq org-agenda-archives-mode nil)))
     ;; Maybe restore the pre-agenda window configuration.
     (and org-agenda-restore-windows-after-quit
         (not (eq org-agenda-window-setup 'other-frame))
@@ -4827,8 +5750,9 @@ Org-mode buffers visited directly by the user will not be touched."
   (org-agenda-quit))
 
 (defun org-agenda-execute (arg)
-  "Execute another agenda command, keeping same window.\\<global-map>
-So this is just a shortcut for `\\[org-agenda]', available in the agenda."
+  "Execute another agenda command, keeping same window.
+So this is just a shortcut for \\<global-map>`\\[org-agenda]', available
+in the agenda."
   (interactive "P")
   (let ((org-agenda-window-setup 'current-window))
     (org-agenda arg)))
@@ -4840,6 +5764,7 @@ When this is the global TODO list, a prefix argument will be interpreted."
   (let* ((org-agenda-keep-modes t)
         (filter org-agenda-filter)
         (preset (get 'org-agenda-filter :preset-filter))
+        (org-agenda-filter-while-redo (or filter preset))
         (cols org-agenda-columns-active)
         (line (org-current-line))
         (window-line (- line (org-current-line (window-start))))
@@ -4869,22 +5794,26 @@ used to narrow the search - the interactive user can also press `-' or `+'
 to switch to narrowing."
   (interactive "P")
   (let* ((alist org-tag-alist-for-agenda)
-       (tag-chars (mapconcat
-                   (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
-                   alist ""))
-       (efforts (org-split-string
-                 (or (cdr (assoc (concat org-effort-property "_ALL")
-                                 org-global-properties))
-                     "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"                   "")))
-       (effort-op org-agenda-filter-effort-default-operator)
-       (effort-prompt "")
-       (inhibit-read-only t)
-       (current org-agenda-filter)
-       char a n tag)
+        (tag-chars (mapconcat
+                    (lambda (x) (if (and (not (symbolp (car x)))
+                                         (cdr x))
+                                    (char-to-string (cdr x))
+                                  ""))
+                    alist ""))
+        (efforts (org-split-string
+                  (or (cdr (assoc (concat org-effort-property "_ALL")
+                                  org-global-properties))
+                      "0 0:10 0:30 1:00 2:00 3:00 4:00 5:00 6:00 7:00 8:00"                  "")))
+        (effort-op org-agenda-filter-effort-default-operator)
+        (effort-prompt "")
+        (inhibit-read-only t)
+        (current org-agenda-filter)
+        a n tag)
     (unless char
       (message
-       "%s by tag [%s ], [TAB], [/]:off, [+-]:narrow, [>=<?]:effort: "
-       (if narrow "Narrow" "Filter") tag-chars)
+       "%s by tag [%s ], [TAB], %s[/]:off, [+-]:narrow, [>=<?]:effort: "
+       (if narrow "Narrow" "Filter") tag-chars
+       (if org-agenda-auto-exclude-function "[RET], " ""))
       (setq char (read-char)))
     (when (member char '(?+ ?-))
       ;; Narrowing down
@@ -4916,6 +5845,16 @@ to switch to narrowing."
        (setq tag (org-icompleting-read
                   "Tag: " org-global-tags-completion-table))))
     (cond
+     ((equal char ?\r)
+      (org-agenda-filter-by-tag-show-all)
+      (when org-agenda-auto-exclude-function
+       (setq org-agenda-filter '())
+       (dolist (tag (org-agenda-get-represented-tags))
+         (let ((modifier (funcall org-agenda-auto-exclude-function tag)))
+           (if modifier
+               (push modifier org-agenda-filter))))
+       (if (not (null org-agenda-filter))
+           (org-agenda-filter-apply org-agenda-filter))))
      ((equal char ?/)
       (org-agenda-filter-by-tag-show-all)
       (when (get 'org-agenda-filter :preset-filter)
@@ -4938,6 +5877,17 @@ to switch to narrowing."
       (org-agenda-filter-apply org-agenda-filter))
      (t (error "Invalid tag selection character %c" char)))))
 
+(defun org-agenda-get-represented-tags ()
+  "Get a list of all tags currently represented in the agenda."
+  (let (p tags)
+    (save-excursion
+      (goto-char (point-min))
+      (while (setq p (next-single-property-change (point) 'tags))
+       (goto-char p)
+       (mapc (lambda (x) (add-to-list 'tags x))
+             (get-text-property (point) 'tags))))
+    tags))
+
 (defun org-agenda-filter-by-tag-refine (strip &optional char)
   "Refine the current filter.  See `org-agenda-filter-by-tag."
   (interactive "P")
@@ -4949,7 +5899,7 @@ to switch to narrowing."
     (dolist (x (append (get 'org-agenda-filter :preset-filter)
                       org-agenda-filter))
       (if (member x '("-" "+"))
-         (setq f1 '(not tags))
+         (setq f1 (if (equal x "-") 'tags '(not tags)))
        (if (string-match "[<=>?]" x)
            (setq f1 (org-agenda-filter-effort-form x))
          (setq f1 (list 'member (downcase (substring x 1)) 'tags)))
@@ -4960,7 +5910,7 @@ to switch to narrowing."
 
 (defun org-agenda-filter-effort-form (e)
   "Return the form to compare the effort of the current line with what E says.
-E looks line \"+<2:25\"."
+E looks like \"+<2:25\"."
   (let (op)
     (setq e (substring e 1))
     (setq op (string-to-char e) e (substring e 1))
@@ -4974,7 +5924,7 @@ E looks line \"+<2:25\"."
 (defun org-agenda-compare-effort (op value)
   "Compare the effort of the current line with VALUE, using OP.
 If the line does not have an effort defined, return nil."
-  (let ((eff (get-text-property (point) 'effort-minutes)))
+  (let ((eff (org-get-at-bol 'effort-minutes)))
     (if (equal op ??)
        (not eff)
       (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
@@ -4989,35 +5939,37 @@ If the line does not have an effort defined, return nil."
     (save-excursion
       (goto-char (point-min))
       (while (not (eobp))
-       (if (get-text-property (point) 'org-marker)
+       (if (org-get-at-bol 'org-marker)
            (progn
-             (setq tags (get-text-property (point) 'tags)) ; used in eval
+             (setq tags (org-get-at-bol 'tags)) ; used in eval
              (if (not (eval org-agenda-filter-form))
                  (org-agenda-filter-by-tag-hide-line))
              (beginning-of-line 2))
-         (beginning-of-line 2))))))
+         (beginning-of-line 2))))
+    (if (get-char-property (point) 'invisible)
+       (org-agenda-previous-line))))
 
 (defun org-agenda-filter-by-tag-hide-line ()
   (let (ov)
-    (setq ov (org-make-overlay (max (point-min) (1- (point-at-bol)))
+    (setq ov (make-overlay (max (point-min) (1- (point-at-bol)))
                               (point-at-eol)))
-    (org-overlay-put ov 'invisible t)
-    (org-overlay-put ov 'type 'tags-filter)
+    (overlay-put ov 'invisible t)
+    (overlay-put ov 'type 'tags-filter)
     (push ov org-agenda-filter-overlays)))
 
 (defun org-agenda-fix-tags-filter-overlays-at (&optional pos)
   (setq pos (or pos (point)))
   (save-excursion
-    (dolist (ov (org-overlays-at pos))
-      (when (and (org-overlay-get ov 'invisible)
-                (eq (org-overlay-get ov 'type) 'tags-filter))
+    (dolist (ov (overlays-at pos))
+      (when (and (overlay-get ov 'invisible)
+                (eq (overlay-get ov 'type) 'tags-filter))
        (goto-char pos)
-       (if (< (org-overlay-start ov) (point-at-eol))
-           (org-move-overlay ov (point-at-eol)
-                             (org-overlay-end ov)))))))
+       (if (< (overlay-start ov) (point-at-eol))
+           (move-overlay ov (point-at-eol)
+                             (overlay-end ov)))))))
 
 (defun org-agenda-filter-by-tag-show-all ()
-  (mapc 'org-delete-overlay org-agenda-filter-overlays)
+  (mapc 'delete-overlay org-agenda-filter-overlays)
   (setq org-agenda-filter-overlays nil)
   (setq org-agenda-filter nil)
   (setq org-agenda-filter-form nil)
@@ -5025,22 +5977,22 @@ If the line does not have an effort defined, return nil."
 
 (defun org-agenda-manipulate-query-add ()
   "Manipulate the query by adding a search term with positive selection.
-Positive selection means, the term must be matched for selection of an entry."
+Positive selection means the term must be matched for selection of an entry."
   (interactive)
   (org-agenda-manipulate-query ?\[))
 (defun org-agenda-manipulate-query-subtract ()
   "Manipulate the query by adding a search term with negative selection.
-Negative selection means, term must not be matched for selection of an entry."
+Negative selection means term must not be matched for selection of an entry."
   (interactive)
   (org-agenda-manipulate-query ?\]))
 (defun org-agenda-manipulate-query-add-re ()
   "Manipulate the query by adding a search regexp with positive selection.
-Positive selection means, the regexp must match for selection of an entry."
+Positive selection means the regexp must match for selection of an entry."
   (interactive)
   (org-agenda-manipulate-query ?\{))
 (defun org-agenda-manipulate-query-subtract-re ()
   "Manipulate the query by adding a search regexp with negative selection.
-Negative selection means, regexp must not match for selection of an entry."
+Negative selection means regexp must not match for selection of an entry."
   (interactive)
   (org-agenda-manipulate-query ?\}))
 (defun org-agenda-manipulate-query (char)
@@ -5052,8 +6004,10 @@ Negative selection means, regexp must not match for selection of an entry."
    ((eq org-agenda-type 'search)
     (org-add-to-string
      'org-agenda-query-string
-     (cdr (assoc char '((?\[ . " +") (?\] . " -")
-                       (?\{ . " +{}") (?\} . " -{}")))))
+     (if org-agenda-last-search-view-search-was-boolean
+        (cdr (assoc char '((?\[ . " +") (?\] . " -")
+                           (?\{ . " +{}") (?\} . " -{}"))))
+       " "))
     (setq org-agenda-redo-command
          (list 'org-search-view
                org-todo-only
@@ -5070,7 +6024,9 @@ Negative selection means, regexp must not match for selection of an entry."
 
 (defun org-agenda-goto-date (date)
   "Jump to DATE in agenda."
-  (interactive (list (org-read-date)))
+  (interactive (list (let ((org-read-date-prefer-future
+                           (eval org-agenda-jump-prefer-future)))
+                      (org-read-date))))
   (org-agenda-list nil date))
 
 (defun org-agenda-goto-today ()
@@ -5081,13 +6037,10 @@ Negative selection means, regexp must not match for selection of an entry."
     (cond
      (tdpos (goto-char tdpos))
      ((eq org-agenda-type 'agenda)
-      (let* ((sd (time-to-days
-                 (time-subtract (current-time)
-                                (list 0 (* 3600 org-extend-today-until) 0))))
-            (comp (org-agenda-compute-time-span sd org-agenda-span))
+      (let* ((sd (org-agenda-compute-starting-span
+                 (org-today) (or org-agenda-ndays org-agenda-span)))
             (org-agenda-overriding-arguments org-agenda-last-arguments))
-       (setf (nth 1 org-agenda-overriding-arguments) (car comp))
-       (setf (nth 2 org-agenda-overriding-arguments) (cdr comp))
+       (setf (nth 1 org-agenda-overriding-arguments) sd)
        (org-agenda-redo)
        (org-agenda-find-same-or-today-or-agenda)))
      (t (error "Cannot find today")))))
@@ -5104,28 +6057,28 @@ Negative selection means, regexp must not match for selection of an entry."
 With prefix ARG, go forward that many times the current span."
   (interactive "p")
   (org-agenda-check-type t 'agenda)
-  (let* ((span org-agenda-span)
+  (let* ((span org-agenda-current-span)
         (sd org-starting-day)
         (greg (calendar-gregorian-from-absolute sd))
-        (cnt (get-text-property (point) 'org-day-cnt))
-        greg2 nd)
+        (cnt (org-get-at-bol 'org-day-cnt))
+        greg2)
     (cond
      ((eq span 'day)
-      (setq sd (+ arg sd) nd 1))
+      (setq sd (+ arg sd)))
      ((eq span 'week)
-      (setq sd (+ (* 7 arg) sd) nd 7))
+      (setq sd (+ (* 7 arg) sd)))
      ((eq span 'month)
       (setq greg2 (list (+ (car greg) arg) (nth 1 greg) (nth 2 greg))
            sd (calendar-absolute-from-gregorian greg2))
-      (setcar greg2 (1+ (car greg2)))
-      (setq nd (- (calendar-absolute-from-gregorian greg2) sd)))
+      (setcar greg2 (1+ (car greg2))))
      ((eq span 'year)
       (setq greg2 (list (car greg) (nth 1 greg) (+ arg (nth 2 greg)))
            sd (calendar-absolute-from-gregorian greg2))
-      (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2)))
-      (setq nd (- (calendar-absolute-from-gregorian greg2) sd))))
+      (setcar (nthcdr 2 greg2) (1+ (nth 2 greg2))))
+     (t
+      (setq sd (+ (* span arg) sd))))
     (let ((org-agenda-overriding-arguments
-          (list (car org-agenda-last-arguments) sd nd t)))
+          (list (car org-agenda-last-arguments) sd span t)))
       (org-agenda-redo)
       (org-agenda-find-same-or-today-or-agenda cnt))))
 
@@ -5138,8 +6091,9 @@ With prefix ARG, go backward that many times the current span."
 (defun org-agenda-view-mode-dispatch ()
   "Call one of the view mode commands."
   (interactive)
-  (message "View: [d]ay [w]eek [m]onth [y]ear [l]og [L]og-all [a]rch-trees [A]rch-files
-       clock[R]eport   time[G]rid   [[]inactive  [E]ntryText   include[D]iary")
+  (message "View: [d]ay [w]eek [m]onth [y]ear                         [q]uit/abort
+      time[G]rid     [[]inactive [f]ollow [l]og [L]og-all   [E]ntryText
+      [a]rch-trees   [A]rch-files    clock[R]eport   include[D]iary")
   (let ((a (read-char-exclusive)))
     (case a
       (?d (call-interactively 'org-agenda-day-view))
@@ -5147,6 +6101,7 @@ With prefix ARG, go backward that many times the current span."
       (?m (call-interactively 'org-agenda-month-view))
       (?y (call-interactively 'org-agenda-year-view))
       (?l (call-interactively 'org-agenda-log-mode))
+      (?L (org-agenda-log-mode '(4)))
       ((?F ?f) (call-interactively 'org-agenda-follow-mode))
       (?a (call-interactively 'org-agenda-archives-mode))
       (?A (org-agenda-archives-mode 'files))
@@ -5154,6 +6109,7 @@ With prefix ARG, go backward that many times the current span."
       ((?E ?e) (call-interactively 'org-agenda-entry-text-mode))
       (?G (call-interactively 'org-agenda-toggle-time-grid))
       (?D (call-interactively 'org-agenda-toggle-diary))
+      (?\! (call-interactively 'org-agenda-toggle-deadlines))
       (?\[ (let ((org-agenda-include-inactive-timestamps t))
             (org-agenda-check-type t 'timeline 'agenda)
             (org-agenda-redo))
@@ -5165,7 +6121,6 @@ With prefix ARG, go backward that many times the current span."
   "Switch to daily view for agenda.
 With argument DAY-OF-YEAR, switch to that day of the year."
   (interactive "P")
-  (setq org-agenda-ndays 1)
   (org-agenda-change-time-span 'day day-of-year))
 (defun org-agenda-week-view (&optional iso-week)
   "Switch to daily view for agenda.
@@ -5175,7 +6130,6 @@ week.  Any digits before this encode a year.  So 200712 means
 week 12 of year 2007.  Years in the range 1938-2037 can also be
 written as 2-digit years."
   (interactive "P")
-  (setq org-agenda-ndays 7)
   (org-agenda-change-time-span 'week iso-week))
 (defun org-agenda-month-view (&optional month)
   "Switch to monthly view for agenda.
@@ -5200,70 +6154,61 @@ written as 2-digit years."
   "Change the agenda view to SPAN.
 SPAN may be `day', `week', `month', `year'."
   (org-agenda-check-type t 'agenda)
-  (if (and (not n) (equal org-agenda-span span))
+  (if (and (not n) (equal org-agenda-current-span span))
       (error "Viewing span is already \"%s\"" span))
-  (let* ((sd (or (get-text-property (point) 'day)
+  (let* ((sd (or (org-get-at-bol 'day)
                org-starting-day))
-        (computed (org-agenda-compute-time-span sd span n))
+        (sd (org-agenda-compute-starting-span sd span n))
         (org-agenda-overriding-arguments
-         (list (car org-agenda-last-arguments)
-               (car computed) (cdr computed) t)))
+         (list (car org-agenda-last-arguments) sd span t)))
     (org-agenda-redo)
     (org-agenda-find-same-or-today-or-agenda))
   (org-agenda-set-mode-name)
   (message "Switched to %s view" span))
 
-(defun org-agenda-compute-time-span (sd span &optional n)
-  "Compute starting date and number of days for agenda.
+(defun org-agenda-compute-starting-span (sd span &optional n)
+  "Compute starting date for agenda.
 SPAN may be `day', `week', `month', `year'.  The return value
 is a cons cell with the starting date and the number of days,
 so that the date SD will be in that range."
   (let* ((greg (calendar-gregorian-from-absolute sd))
         (dg (nth 1 greg))
         (mg (car greg))
-        (yg (nth 2 greg))
-        nd w1 y1 m1 thisweek)
+        (yg (nth 2 greg)))
     (cond
      ((eq span 'day)
       (when n
        (setq sd (+ (calendar-absolute-from-gregorian
                     (list mg 1 yg))
-                   n -1)))
-      (setq nd 1))
+                   n -1))))
      ((eq span 'week)
       (let* ((nt (calendar-day-of-week
                  (calendar-gregorian-from-absolute sd)))
             (d (if org-agenda-start-on-weekday
                    (- nt org-agenda-start-on-weekday)
-                 0)))
+                 0))
+            y1)
        (setq sd (- sd (+ (if (< d 0) 7 0) d)))
        (when n
          (require 'cal-iso)
-         (setq thisweek (car (calendar-iso-from-absolute sd)))
          (when (> n 99)
            (setq y1 (org-small-year-to-year (/ n 100))
                  n (mod n 100)))
          (setq sd
                (calendar-absolute-from-iso
                 (list n 1
-                      (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))
-       (setq nd 7)))
+                      (or y1 (nth 2 (calendar-iso-from-absolute sd)))))))))
      ((eq span 'month)
-      (when (and n (> n 99))
-       (setq y1 (org-small-year-to-year (/ n 100))
-             n (mod n 100)))
-      (setq sd (calendar-absolute-from-gregorian
-               (list (or n mg) 1 (or y1 yg)))
-           nd (- (calendar-absolute-from-gregorian
-                  (list (1+ (or n mg)) 1 (or y1 yg)))
-                 sd)))
+      (let (y1)
+       (when (and n (> n 99))
+         (setq y1 (org-small-year-to-year (/ n 100))
+               n (mod n 100)))
+       (setq sd (calendar-absolute-from-gregorian
+                 (list (or n mg) 1 (or y1 yg))))))
      ((eq span 'year)
       (setq sd (calendar-absolute-from-gregorian
-               (list 1 1 (or n yg)))
-           nd (- (calendar-absolute-from-gregorian
-                  (list 1 1 (1+ (or n yg))))
-                 sd))))
-    (cons sd nd)))
+               (list 1 1 (or n yg))))))
+    sd))
 
 (defun org-agenda-next-date-line (&optional arg)
   "Jump to the next line indicating a date in agenda buffer."
@@ -5287,17 +6232,16 @@ so that the date SD will be in that range."
       (error "No previous date before this line in this buffer")))
 
 ;; Initialize the highlight
-(defvar org-hl (org-make-overlay 1 1))
-(org-overlay-put org-hl 'face 'highlight)
+(defvar org-hl (make-overlay 1 1))
+(overlay-put org-hl 'face 'highlight)
 
 (defun org-highlight (begin end &optional buffer)
   "Highlight a region with overlay."
-  (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
-          org-hl begin end (or buffer (current-buffer))))
+  (move-overlay org-hl begin end (or buffer (current-buffer))))
 
 (defun org-unhighlight ()
   "Detach overlay INDEX."
-  (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
+  (org-detach-overlay org-hl))
 
 ;; FIXME this is currently not used.
 (defun org-highlight-until-next-command (beg end &optional buffer)
@@ -5314,15 +6258,16 @@ so that the date SD will be in that range."
   (interactive)
   (setq org-agenda-follow-mode (not org-agenda-follow-mode))
   (org-agenda-set-mode-name)
+  (if (and org-agenda-follow-mode (org-get-at-bol 'org-marker))
+      (org-agenda-show))
   (message "Follow mode is %s"
           (if org-agenda-follow-mode "on" "off")))
 
 (defun org-agenda-entry-text-mode (&optional arg)
   "Toggle entry text mode in an agenda buffer."
   (interactive "P")
-  (if (integerp arg)
-      (setq org-agenda-entry-text-mode t)
-    (setq org-agenda-entry-text-mode (not org-agenda-entry-text-mode)))
+  (setq org-agenda-entry-text-mode (or (integerp arg)
+                                       (not org-agenda-entry-text-mode)))
   (org-agenda-entry-text-hide)
   (and org-agenda-entry-text-mode
        (let ((org-agenda-entry-text-maxlines
@@ -5333,11 +6278,15 @@ so that the date SD will be in that range."
           (if org-agenda-entry-text-mode "on" "off")
           (if (integerp arg) arg org-agenda-entry-text-maxlines)))
 
-(defun org-agenda-clockreport-mode ()
-  "Toggle clocktable mode in an agenda buffer."
-  (interactive)
+(defun org-agenda-clockreport-mode (&optional with-filter)
+  "Toggle clocktable mode in an agenda buffer.
+With prefix arg WITH-FILTER, make the clocktable respect the current
+agenda filter."
+  (interactive "P")
   (org-agenda-check-type t 'agenda)
-  (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode))
+  (if with-filter
+      (setq org-agenda-clockreport-mode 'with-filter)
+    (setq org-agenda-clockreport-mode (not org-agenda-clockreport-mode)))
   (org-agenda-set-mode-name)
   (org-agenda-redo)
   (message "Clocktable mode is %s"
@@ -5389,6 +6338,16 @@ When called with a prefix argument, include all archive files as well."
   (message "Diary inclusion turned %s"
           (if org-agenda-include-diary "on" "off")))
 
+(defun org-agenda-toggle-deadlines ()
+  "Toggle inclusion of entries with a deadline in an agenda buffer."
+  (interactive)
+  (org-agenda-check-type t 'agenda)
+  (setq org-agenda-include-deadlines (not org-agenda-include-deadlines))
+  (org-agenda-redo)
+  (org-agenda-set-mode-name)
+  (message "Deadlines inclusion turned %s"
+          (if org-agenda-include-deadlines "on" "off")))
+
 (defun org-agenda-toggle-time-grid ()
   "Toggle time grid in an agenda buffer."
   (interactive)
@@ -5402,53 +6361,77 @@ When called with a prefix argument, include all archive files as well."
 (defun org-agenda-set-mode-name ()
   "Set the mode name to indicate all the small mode settings."
   (setq mode-name
-       (concat "Org-Agenda"
-               (if (equal org-agenda-ndays 1) " Day"    "")
-               (if (equal org-agenda-ndays 7) " Week"   "")
-               (if org-agenda-follow-mode     " Follow" "")
-               (if org-agenda-entry-text-mode " ETxt"   "")
-               (if org-agenda-include-diary   " Diary"  "")
-               (if org-agenda-use-time-grid   " Grid"   "")
-               (if (consp org-agenda-show-log) " LogAll"
-                   (if org-agenda-show-log " Log" ""))
-               (if (or org-agenda-filter (get 'org-agenda-filter
-                                              :preset-filter))
-                   (concat " {" (mapconcat
-                                 'identity
-                                 (append (get 'org-agenda-filter
-                                              :preset-filter)
-                                         org-agenda-filter) "") "}")
-                 "")
-               (if org-agenda-archives-mode
-                   (if (eq org-agenda-archives-mode t)
-                       " Archives"
-                     (format " :%s:" org-archive-tag))
-                 "")
-               (if org-agenda-clockreport-mode " Clock"   "")))
+       (list "Org-Agenda"
+             (if (get 'org-agenda-files 'org-restrict) " []" "")
+             " "
+             '(:eval (org-agenda-span-name org-agenda-current-span))
+             (if org-agenda-follow-mode     " Follow" "")
+             (if org-agenda-entry-text-mode " ETxt"   "")
+             (if org-agenda-include-diary   " Diary"  "")
+             (if org-agenda-include-deadlines " Ddl"  "")
+             (if org-agenda-use-time-grid   " Grid"   "")
+             (if (and (boundp 'org-habit-show-habits)
+                      org-habit-show-habits) " Habit"   "")
+             (if (consp org-agenda-show-log) " LogAll"
+               (if org-agenda-show-log " Log" ""))
+             (if (or org-agenda-filter (get 'org-agenda-filter
+                                            :preset-filter))
+                 (concat " {" (mapconcat
+                               'identity
+                               (append (get 'org-agenda-filter
+                                            :preset-filter)
+                                       org-agenda-filter) "") "}")
+               "")
+             (if org-agenda-archives-mode
+                 (if (eq org-agenda-archives-mode t)
+                     " Archives"
+                   (format " :%s:" org-archive-tag))
+               "")
+             (if org-agenda-clockreport-mode
+                 (if (eq org-agenda-clockreport-mode 'with-filter)
+                     " Clock{}" " Clock")
+               "")))
   (force-mode-line-update))
 
 (defun org-agenda-post-command-hook ()
-  (and (eolp) (not (bolp)) (backward-char 1))
   (setq org-agenda-type
        (or (get-text-property (point) 'org-agenda-type)
            (get-text-property (max (point-min) (1- (point)))
-                              'org-agenda-type)))
-  (if (and org-agenda-follow-mode
-          (get-text-property (point) 'org-marker))
-      (org-agenda-show)))
+                              'org-agenda-type))))
+
+(defun org-agenda-next-line ()
+  "Move cursor to the next line, and show if follow mode is active."
+  (interactive)
+  (call-interactively 'next-line)
+  (org-agenda-do-context-action))
+
+(defun org-agenda-previous-line ()
+  "Move cursor to the previous line, and show if follow-mode is active."
+  (interactive)
+  (call-interactively 'previous-line)
+  (org-agenda-do-context-action))
+
+(defun org-agenda-do-context-action ()
+  "Show outline path and, maybe, follow mode window."
+  (let ((m (org-get-at-bol 'org-marker)))
+    (if (and org-agenda-follow-mode m)
+       (org-agenda-show))
+    (if (and m org-agenda-show-outline-path)
+       (org-with-point-at m
+         (org-display-outline-path t)))))
 
 (defun org-agenda-show-priority ()
   "Show the priority of the current item.
 This priority is composed of the main priority given with the [#A] cookies,
 and by additional input from the age of a schedules or deadline entry."
   (interactive)
-  (let* ((pri (get-text-property (point-at-bol) 'priority)))
+  (let* ((pri (org-get-at-bol 'priority)))
     (message "Priority is %d" (if pri pri -1000))))
 
 (defun org-agenda-show-tags ()
   "Show the tags applicable to the current item."
   (interactive)
-  (let* ((tags (get-text-property (point-at-bol) 'tags)))
+  (let* ((tags (org-get-at-bol 'tags)))
     (if tags
        (message "Tags are :%s:"
                 (org-no-properties (mapconcat 'identity tags ":")))
@@ -5457,12 +6440,13 @@ and by additional input from the age of a schedules or deadline entry."
 (defun org-agenda-goto (&optional highlight)
   "Go to the Org-mode file which contains the item at point."
   (interactive)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker)))
     (switch-to-buffer-other-window buffer)
     (widen)
+    (push-mark)
     (goto-char pos)
     (when (org-mode-p)
       (org-show-context 'agenda)
@@ -5481,11 +6465,11 @@ Point is in the buffer where the item originated.")
   "Kill the entry or subtree belonging to the current agenda entry."
   (interactive)
   (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker))
-        (type (get-text-property (point) 'type))
+        (type (org-get-at-bol 'type))
         dbeg dend (n 0) conf)
     (org-with-remote-undo buffer
      (with-current-buffer buffer
@@ -5510,40 +6494,48 @@ Point is in the buffer where the item originated.")
      (with-current-buffer buffer (delete-region dbeg dend))
      (message "Agenda item and source killed"))))
 
+(defvar org-archive-default-command)
+(defun org-agenda-archive-default ()
+  "Archive the entry or subtree belonging to the current agenda entry."
+  (interactive)
+  (require 'org-archive)
+  (org-agenda-archive-with org-archive-default-command))
+
+(defun org-agenda-archive-default-with-confirmation ()
+  "Archive the entry or subtree belonging to the current agenda entry."
+  (interactive)
+  (require 'org-archive)
+  (org-agenda-archive-with org-archive-default-command 'confirm))
+
 (defun org-agenda-archive ()
   "Archive the entry or subtree belonging to the current agenda entry."
   (interactive)
-  (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
-  (let* ((marker (or (get-text-property (point) 'org-marker)
-                    (org-agenda-error)))
-        (buffer (marker-buffer marker))
-        (pos (marker-position marker)))
-    (org-with-remote-undo buffer
-      (with-current-buffer buffer
-       (if (org-mode-p)
-           (save-excursion
-             (goto-char pos)
-             (org-remove-subtree-entries-from-agenda)
-             (org-back-to-heading t)
-             (org-archive-subtree))
-         (error "Archiving works only in Org-mode files"))))))
+  (org-agenda-archive-with 'org-archive-subtree))
 
 (defun org-agenda-archive-to-archive-sibling ()
+  "Move the entry to the archive sibling."
+  (interactive)
+  (org-agenda-archive-with 'org-archive-to-archive-sibling))
+
+(defun org-agenda-archive-with (cmd &optional confirm)
   "Move the entry to the archive sibling."
   (interactive)
   (or (eq major-mode 'org-agenda-mode) (error "Not in agenda"))
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker)))
     (org-with-remote-undo buffer
       (with-current-buffer buffer
        (if (org-mode-p)
-           (save-excursion
-             (goto-char pos)
-             (org-remove-subtree-entries-from-agenda)
-             (org-back-to-heading t)
-             (org-archive-to-archive-sibling))
+           (if (and confirm
+                    (not (y-or-n-p "Archive this subtree or entry? ")))
+               (error "Abort")
+             (save-excursion
+               (goto-char pos)
+               (org-remove-subtree-entries-from-agenda)
+               (org-back-to-heading t)
+               (funcall cmd)))
          (error "Archiving works only in Org-mode files"))))))
 
 (defun org-remove-subtree-entries-from-agenda (&optional buf beg end)
@@ -5562,7 +6554,7 @@ If this information is not given, the function uses the tree at point."
        (goto-char (point-max))
        (beginning-of-line 1)
        (while (not (bobp))
-         (when (and (setq m (get-text-property (point) 'org-marker))
+         (when (and (setq m (org-get-at-bol 'org-marker))
                     (equal buf (marker-buffer m))
                     (setq p (marker-position m))
                     (>= p beg)
@@ -5571,12 +6563,12 @@ If this information is not given, the function uses the tree at point."
              (delete-region (point-at-bol) (1+ (point-at-eol)))))
          (beginning-of-line 0))))))
 
-(defun org-agenda-refile (&optional goto rfloc)
+(defun org-agenda-refile (&optional goto rfloc no-update)
   "Refile the item at point."
   (interactive "P")
   (if (equal goto '(16))
       (org-refile-goto-last-stored)
-    (let* ((marker (or (get-text-property (point) 'org-hd-marker)
+    (let* ((marker (or (org-get-at-bol 'org-hd-marker)
                       (org-agenda-error)))
           (buffer (marker-buffer marker))
           (pos (marker-position marker))
@@ -5590,27 +6582,39 @@ If this information is not given, the function uses the tree at point."
            (widen)
            (goto-char marker)
            (org-remove-subtree-entries-from-agenda)
-           (org-refile goto buffer rfloc)))))))
+           (org-refile goto buffer rfloc)))))
+    (unless no-update (org-agenda-redo))))
 
 (defun org-agenda-open-link (&optional arg)
   "Follow the link in the current line, if any.
-This looks for a link in the displayed lin in the agenda.  It also looks
+This looks for a link in the displayed line in the agenda.  It also looks
 at the text of the entry itself."
   (interactive "P")
-  (let* ((marker (or (get-text-property (point) 'org-hd-marker)
-                    (get-text-property (point) 'org-marker)))
-        (buffer (and marker (marker-buffer marker))))
-    (unless buffer (error "Don't know where to look for links"))
-    (with-current-buffer buffer
-      (save-excursion
-       (save-restriction
-         (widen)
-         (goto-char marker)
-         (org-offer-links-in-entry arg))))))
+  (let* ((marker (or (org-get-at-bol 'org-hd-marker)
+                    (org-get-at-bol 'org-marker)))
+        (buffer (and marker (marker-buffer marker)))
+        (prefix (buffer-substring
+                 (point-at-bol)
+                 (+ (point-at-bol)
+                    (or (org-get-at-bol 'prefix-length) 0)))))
+    (cond
+     (buffer
+      (with-current-buffer buffer
+       (save-excursion
+         (save-restriction
+           (widen)
+           (goto-char marker)
+           (org-offer-links-in-entry arg prefix)))))
+     ((or (org-in-regexp (concat "\\(" org-bracket-link-regexp "\\)"))
+         (save-excursion
+           (beginning-of-line 1)
+           (looking-at (concat ".*?\\(" org-bracket-link-regexp "\\)"))))
+      (org-open-link-from-string (match-string 1)))
+     (t (error "No link to open here")))))
 
 (defun org-agenda-copy-local-variable (var)
   "Get a variable from a referenced buffer and install it here."
-  (let ((m (get-text-property (point) 'org-marker)))
+  (let ((m (org-get-at-bol 'org-marker)))
     (when (and m (buffer-live-p (marker-buffer m)))
       (org-set-local var (with-current-buffer (marker-buffer m)
                           (symbol-value var))))))
@@ -5618,19 +6622,23 @@ at the text of the entry itself."
 (defun org-agenda-switch-to (&optional delete-other-windows)
   "Go to the Org-mode file which contains the item at point."
   (interactive)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
-                    (org-agenda-error)))
-        (buffer (marker-buffer marker))
-        (pos (marker-position marker)))
-    (switch-to-buffer buffer)
-    (and delete-other-windows (delete-other-windows))
-    (widen)
-    (goto-char pos)
-    (when (org-mode-p)
-      (org-show-context 'agenda)
-      (save-excursion
-       (and (outline-next-heading)
-            (org-flag-heading nil))))))  ; show the next heading
+  (if (and org-return-follows-link
+          (not (org-get-at-bol 'org-marker))
+          (org-in-regexp org-bracket-link-regexp))
+      (org-open-link-from-string (match-string 0))
+    (let* ((marker (or (org-get-at-bol 'org-marker)
+                      (org-agenda-error)))
+          (buffer (marker-buffer marker))
+          (pos (marker-position marker)))
+      (switch-to-buffer buffer)
+      (and delete-other-windows (delete-other-windows))
+      (widen)
+      (goto-char pos)
+      (when (org-mode-p)
+       (org-show-context 'agenda)
+       (save-excursion
+         (and (outline-next-heading)
+              (org-flag-heading nil)))))))  ; show the next heading
 
 (defun org-agenda-goto-mouse (ev)
   "Go to the Org-mode file which contains the item at the mouse click."
@@ -5650,9 +6658,34 @@ if it was hidden in the outline."
       (org-agenda-goto t))
     (select-window win)))
 
+(defvar org-agenda-show-window nil)
+(defun org-agenda-show-and-scroll-up ()
+  "Display the Org-mode file which contains the item at point.
+When called repeatedly, scroll the window that is displaying the buffer."
+  (interactive)
+  (let ((win (selected-window)))
+    (if (and (window-live-p org-agenda-show-window)
+            (eq this-command last-command))
+       (progn
+         (select-window org-agenda-show-window)
+         (ignore-errors (scroll-up)))
+      (org-agenda-goto t)
+      (show-subtree)
+      (setq org-agenda-show-window (selected-window)))
+    (select-window win)))
+
+(defun org-agenda-show-scroll-down ()
+  "Scroll down the window showing the agenda."
+  (interactive)
+  (let ((win (selected-window)))
+    (when (window-live-p org-agenda-show-window)
+      (select-window org-agenda-show-window)
+      (ignore-errors (scroll-down))
+      (select-window win))))
+
 (defun org-agenda-show-1 (&optional more)
   "Display the Org-mode file which contains the item at point.
-The prefix arg causes further revieling:
+The prefix arg selects the amount of information to display:
 
 0   hide the subtree
 1   just show the entry according to defaults.
@@ -5749,7 +6782,7 @@ docstring of `org-agenda-show-1'."
 
 (defun org-agenda-check-no-diary ()
   "Check if the entry is a diary link and abort if yes."
-  (if (get-text-property (point) 'org-agenda-diary-link)
+  (if (org-get-at-bol 'org-agenda-diary-link)
       (org-agenda-error)))
 
 (defun org-agenda-error ()
@@ -5760,11 +6793,11 @@ docstring of `org-agenda-show-1'."
 This calls the command `org-tree-to-indirect-buffer' from the original
 Org-mode buffer.
 With numerical prefix arg ARG, go up to this level and then take that tree.
-With a C-u prefix, make a separate frame for this tree (i.e. don't use the
-dedicated frame)."
+With a \\[universal-argument] prefix, make a separate frame for this tree (i.e. don't
+use the dedicated frame)."
   (interactive)
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker)))
@@ -5794,13 +6827,12 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive "P")
   (org-agenda-check-no-diary)
   (let* ((col (current-column))
-        (marker (or (get-text-property (point) 'org-marker)
+        (marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker))
-        (hdmarker (get-text-property (point) 'org-hd-marker))
-        (todayp (equal (get-text-property (point) 'day)
-                       (time-to-days (current-time))))
+        (hdmarker (org-get-at-bol 'org-hd-marker))
+        (todayp (org-agenda-todayp (org-get-at-bol 'day)))
         (inhibit-read-only t)
         org-agenda-headline-snapshot-before-repeat newhead just-one)
     (org-with-remote-undo buffer
@@ -5834,11 +6866,11 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   "Add a time-stamped note to the entry at point."
   (interactive "P")
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker))
-        (hdmarker (get-text-property (point) 'org-hd-marker))
+        (hdmarker (org-get-at-bol 'org-hd-marker))
         (inhibit-read-only t))
     (with-current-buffer buffer
       (widen)
@@ -5871,18 +6903,19 @@ If FORCE-TAGS is non nil, the car of it returns the new tags."
       (beginning-of-line 1)
       (while (not finish)
        (setq finish (bobp))
-       (when (and (setq m (get-text-property (point) 'org-hd-marker))
+       (when (and (setq m (org-get-at-bol 'org-hd-marker))
                   (or (not just-this) (= (org-current-line) line))
                   (equal m hdmarker))
          (setq props (text-properties-at (point))
-               dotime (get-text-property (point) 'dotime)
-               cat (get-text-property (point) 'org-category)
+               dotime (org-get-at-bol 'dotime)
+               cat (org-get-at-bol 'org-category)
                tags thetags
                new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
-               pl (get-text-property (point) 'prefix-length)
-               undone-face (get-text-property (point) 'undone-face)
-               done-face (get-text-property (point) 'done-face))
-         (org-move-to-column pl)
+               pl (org-get-at-bol 'prefix-length)
+               undone-face (org-get-at-bol 'undone-face)
+               done-face (org-get-at-bol 'done-face))
+         (goto-char (+ (point) pl))
+         ;; (org-move-to-column pl)  FIXME: does the above line work correctly?
          (cond
           ((equal new "")
            (beginning-of-line 1)
@@ -5908,7 +6941,7 @@ If FORCE-TAGS is non nil, the car of it returns the new tags."
   (let ((inhibit-read-only t) l c)
     (save-excursion
       (goto-char (if line (point-at-bol) (point-min)))
-      (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$")
+      (while (re-search-forward (org-re "\\([ \t]+\\)\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")
                                (if line (point-at-eol) nil) t)
        (add-text-properties
         (match-beginning 2) (match-end 2)
@@ -5926,7 +6959,8 @@ If FORCE-TAGS is non nil, the car of it returns the new tags."
        (goto-char (match-beginning 1))
        (insert (org-add-props
                    (make-string (max 1 (- c (current-column))) ?\ )
-                   (text-properties-at (point)))))
+                   (plist-put (copy-sequence (text-properties-at (point)))
+                              'face nil))))
       (goto-char (point-min))
       (org-font-lock-add-tag-faces (point-max)))))
 
@@ -5948,9 +6982,9 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (unless org-enable-priority-commands
     (error "Priority commands are disabled"))
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
-        (hdmarker (get-text-property (point) 'org-hd-marker))
+        (hdmarker (org-get-at-bol 'org-hd-marker))
         (buffer (marker-buffer hdmarker))
         (pos (marker-position hdmarker))
         (inhibit-read-only t)
@@ -5977,7 +7011,7 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (if (and (org-region-active-p) (interactive-p))
       (call-interactively 'org-change-tag-in-region)
     (org-agenda-show)   ;;; FIXME This is a stupid hack and should not be needed
-    (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
+    (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
                         (org-agenda-error)))
           (buffer (marker-buffer hdmarker))
           (pos (marker-position hdmarker))
@@ -6006,7 +7040,7 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive)
   (org-agenda-check-no-diary)
   (org-agenda-show)   ;;; FIXME This is a stupid hack and should not be needed
-  (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
+  (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
                       (org-agenda-error)))
         (buffer (marker-buffer hdmarker))
         (pos (marker-position hdmarker))
@@ -6029,7 +7063,7 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive)
   (org-agenda-check-no-diary)
   (org-agenda-show)   ;;; FIXME This is a stupid hack and should not be needed
-  (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
+  (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
                       (org-agenda-error)))
         (buffer (marker-buffer hdmarker))
         (pos (marker-position hdmarker))
@@ -6053,7 +7087,7 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive)
   (org-agenda-check-no-diary)
   (org-agenda-show)   ;;; FIXME This is a stupid hack and should not be needed
-  (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
+  (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
                        (org-agenda-error)))
         (buffer (marker-buffer hdmarker))
         (pos (marker-position hdmarker))
@@ -6110,7 +7144,7 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive "p")
   (org-agenda-check-type t 'agenda 'timeline)
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker)))
@@ -6160,7 +7194,7 @@ the same tree node, and the headline of the tree node in the Org-mode file."
     (save-excursion
       (goto-char (point-max))
       (while (not (bobp))
-       (when (equal marker (get-text-property (point) 'org-marker))
+       (when (equal marker (org-get-at-bol 'org-marker))
          (org-move-to-column (- (window-width) (length stamp)) t)
          (org-agenda-fix-tags-filter-overlays-at (point))
           (if (featurep 'xemacs)
@@ -6185,7 +7219,7 @@ be used to request time specification in the time stamp."
   (interactive "P")
   (org-agenda-check-type t 'agenda 'timeline)
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker)))
@@ -6193,17 +7227,19 @@ be used to request time specification in the time stamp."
       (with-current-buffer buffer
        (widen)
        (goto-char pos)
-       (if (not (org-at-timestamp-p))
+       (if (not (org-at-timestamp-p t))
            (error "Cannot find time stamp"))
-       (org-time-stamp arg)
-       (message "Time stamp changed to %s" org-last-changed-timestamp)))))
+       (org-time-stamp arg (equal (char-after (match-beginning 0)) ?\[)))
+      (org-agenda-show-new-time marker org-last-changed-timestamp))
+    (message "Time stamp changed to %s" org-last-changed-timestamp)))
 
 (defun org-agenda-schedule (arg)
-  "Schedule the item at point."
+  "Schedule the item at point.
+Arg is passed through to `org-schedule'."
   (interactive "P")
   (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (type (marker-insertion-type marker))
         (buffer (marker-buffer marker))
@@ -6220,11 +7256,12 @@ be used to request time specification in the time stamp."
     (message "Item scheduled for %s" ts)))
 
 (defun org-agenda-deadline (arg)
-  "Schedule the item at point."
+  "Schedule the item at point.
+Arg is passed through to `org-deadline'."
   (interactive "P")
   (org-agenda-check-type t 'agenda 'timeline 'todo 'tags 'search)
   (org-agenda-check-no-diary)
-  (let* ((marker (or (get-text-property (point) 'org-marker)
+  (let* ((marker (or (org-get-at-bol 'org-marker)
                     (org-agenda-error)))
         (buffer (marker-buffer marker))
         (pos (marker-position marker))
@@ -6235,7 +7272,7 @@ be used to request time specification in the time stamp."
        (widen)
        (goto-char pos)
        (setq ts (org-deadline arg)))
-      (org-agenda-show-new-time marker ts "S"))
+      (org-agenda-show-new-time marker ts "D"))
        (message "Deadline for this item set to %s" ts)))
 
 (defun org-agenda-action ()
@@ -6246,20 +7283,21 @@ m     Mark the entry at point for an agenda action
 s     Schedule the marked entry to the date at the cursor
 d     Set the deadline of the marked entry to the date at the cursor
 r     Call `org-remember' with cursor date as the default date
+c     Call `org-capture' with cursor date as the default date
 SPC   Show marked entry in other window
 TAB   Visit marked entry in other window
 
 The cursor may be at a date in the calendar, or in the Org agenda."
   (interactive)
   (let (ans)
-    (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
+    (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [c]apture [ ]show")
     (setq ans (read-char-exclusive))
     (cond
      ((equal ans ?m)
       ;; Mark this entry
       (if (eq major-mode 'org-agenda-mode)
-         (let ((m (or (get-text-property (point) 'org-hd-marker)
-                      (get-text-property (point) 'org-marker))))
+         (let ((m (or (org-get-at-bol 'org-hd-marker)
+                      (org-get-at-bol 'org-marker))))
            (if m
                (progn
                  (move-marker org-agenda-action-marker
@@ -6273,6 +7311,8 @@ The cursor may be at a date in the calendar, or in the Org agenda."
       (org-agenda-do-action '(org-deadline nil org-overriding-default-time)))
      ((equal ans ?r)
       (org-agenda-do-action '(org-remember) t))
+     ((equal ans ?c)
+      (org-agenda-do-action '(org-capture) t))
      ((equal ans ?\ )
       (let ((cw (selected-window)))
        (org-switch-to-buffer-other-window
@@ -6293,7 +7333,7 @@ The cursor may be at a date in the calendar, or in the Org agenda."
     (if current-buffer
        (eval form)
       (if (not (marker-buffer org-agenda-action-marker))
-         (error "No entry has bee selected for agenda action")
+         (error "No entry has been selected for agenda action")
        (with-current-buffer (marker-buffer org-agenda-action-marker)
          (save-excursion
            (save-restriction
@@ -6307,9 +7347,9 @@ The cursor may be at a date in the calendar, or in the Org agenda."
   (org-agenda-check-no-diary)
   (if (equal arg '(4))
       (org-clock-in arg)
-    (let* ((marker (or (get-text-property (point) 'org-marker)
+    (let* ((marker (or (org-get-at-bol 'org-marker)
                       (org-agenda-error)))
-          (hdmarker (or (get-text-property (point) 'org-hd-marker)
+          (hdmarker (or (org-get-at-bol 'org-hd-marker)
                         marker))
           (pos (marker-position marker))
           newhead)
@@ -6324,9 +7364,9 @@ The cursor may be at a date in the calendar, or in the Org agenda."
          (setq newhead (org-get-heading)))
        (org-agenda-change-all-lines newhead hdmarker)))))
 
-(defun org-agenda-clock-out (&optional arg)
+(defun org-agenda-clock-out ()
   "Stop the currently running clock."
-  (interactive "P")
+  (interactive)
   (unless (marker-buffer org-clock-marker)
     (error "No running clock"))
   (let ((marker (make-marker)) newhead)
@@ -6351,47 +7391,245 @@ The cursor may be at a date in the calendar, or in the Org agenda."
   (org-with-remote-undo (marker-buffer org-clock-marker)
     (org-clock-cancel)))
 
+(defun org-agenda-clock-goto ()
+  "Jump to the currently clocked in task within the agenda.
+If the currently clocked in task is not listed in the agenda
+buffer, display it in another window."
+  (interactive)
+  (let (pos)
+    (mapc (lambda (o)
+           (if (eq (overlay-get o 'type) 'org-agenda-clocking)
+               (setq pos (overlay-start o))))
+         (overlays-in (point-min) (point-max)))
+    (cond (pos (goto-char pos))
+         ;; If the currently clocked entry is not in the agenda
+         ;; buffer, we visit it in another window:
+         (org-clock-current-task
+          (org-switch-to-buffer-other-window (org-clock-goto)))
+         (t (message "No running clock, use `C-c C-x C-j' to jump to the most recent one")))))
+
+(defun org-agenda-diary-entry-in-org-file ()
+  "Make a diary entry in the file `org-agenda-diary-file'."
+  (let (d1 d2 char (text "") dp1 dp2)
+    (if (equal (buffer-name) "*Calendar*")
+       (setq d1 (calendar-cursor-to-date t)
+             d2 (car calendar-mark-ring))
+      (setq dp1 (get-text-property (point-at-bol) 'day))
+      (unless dp1 (error "No date defined in current line"))
+      (setq d1 (calendar-gregorian-from-absolute dp1)
+           d2 (and (ignore-errors (mark))
+                   (save-excursion
+                     (goto-char (mark))
+                     (setq dp2 (get-text-property (point-at-bol) 'day)))
+                   (calendar-gregorian-from-absolute dp2))))
+    (message "Diary entry: [d]ay [a]nniversary [b]lock [j]ump to date tree")
+    (setq char (read-char-exclusive))
+    (cond
+     ((equal char ?d)
+      (setq text (read-string "Day entry: "))
+      (org-agenda-add-entry-to-org-agenda-diary-file 'day text d1)
+      (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
+     ((equal char ?a)
+      (setq d1 (list (car d1) (nth 1 d1)
+                    (read-number (format "Reference year [%d]: " (nth 2 d1))
+                                 (nth 2 d1))))
+      (setq text (read-string "Anniversary (use %d to show years): "))
+      (org-agenda-add-entry-to-org-agenda-diary-file 'anniversary text d1)
+      (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
+     ((equal char ?b)
+      (setq text (read-string "Block entry: "))
+      (unless (and d1 d2 (not (equal d1 d2)))
+       (error "No block of days selected"))
+      (org-agenda-add-entry-to-org-agenda-diary-file 'block text d1 d2)
+      (and (equal (buffer-name) org-agenda-buffer-name) (org-agenda-redo)))
+     ((equal char ?j)
+      (org-switch-to-buffer-other-window
+       (find-file-noselect org-agenda-diary-file))
+      (require 'org-datetree)
+      (org-datetree-find-date-create d1)
+      (org-reveal t))
+     (t (error "Invalid selection character `%c'" char)))))
+
+(defcustom org-agenda-insert-diary-strategy 'date-tree
+  "Where in `org-agenda-diary-file' should new entries be added?
+Valid values:
+
+date-tree    in the date tree, as child of the date
+top-level    as top-level entries at the end of the file."
+  :group 'org-agenda
+  :type '(choice
+         (const :tag "in a date tree" date-tree)
+         (const :tag "as top level at end of file" top-level)))
+
+(defcustom org-agenda-insert-diary-extract-time nil
+  "Non-nil means extract any time specification from the diary entry."
+  :group 'org-agenda
+  :type 'boolean)
+
+(defun org-agenda-add-entry-to-org-agenda-diary-file (type text &optional d1 d2)
+  "Add a diary entry with TYPE to `org-agenda-diary-file'.
+If TEXT is not empty, it will become the headline of the new entry, and
+the resulting entry will not be shown.  When TEXT is empty, switch to
+`org-agenda-diary-file' and let the user finish the entry there."
+  (let ((cw (current-window-configuration)))
+    (org-switch-to-buffer-other-window
+     (find-file-noselect org-agenda-diary-file))
+    (widen)
+    (goto-char (point-min))
+    (cond
+     ((eq type 'anniversary)
+      (or (re-search-forward "^*[ \t]+Anniversaries" nil t)
+       (progn
+         (or (org-on-heading-p t)
+             (progn
+               (outline-next-heading)
+               (insert "* Anniversaries\n\n")
+               (beginning-of-line -1)))))
+      (outline-next-heading)
+      (org-back-over-empty-lines)
+      (backward-char 1)
+      (insert "\n")
+      (require 'diary-lib)
+      (let ((calendar-date-display-form
+            (if (if (boundp 'calendar-date-style)
+                    (eq calendar-date-style 'european)
+                  (with-no-warnings ;; european-calendar-style is obsolete as of version 23.1
+                    (org-bound-and-true-p european-calendar-style))) ; Emacs 22
+                '(day " " month " " year)
+              '(month " " day " " year))))
+
+       (insert (format "%%%%(diary-anniversary %s) %s"
+                       (calendar-date-string d1 nil t) text))))
+     ((eq type 'day)
+      (let ((org-prefix-has-time t)
+           (org-agenda-time-leading-zero t)
+           fmt time time2)
+       (if org-agenda-insert-diary-extract-time
+           ;; Use org-format-agenda-item to parse text for a time-range and
+           ;; remove it.  FIXME: This is a hack, we should refactor
+           ;; that function to make time extraction available separately
+           (setq fmt (org-format-agenda-item nil text nil nil t)
+                 time (get-text-property 0 'time fmt)
+                 time2 (if (> (length time) 0)
+                           ;; split-string removes trailing ...... if
+                           ;; no end time given.  First space
+                           ;; separates time from date.
+                           (concat " " (car (split-string time "\\.")))
+                         nil)
+                 text (get-text-property 0 'txt fmt)))
+       (if (eq org-agenda-insert-diary-strategy 'top-level)
+           (org-agenda-insert-diary-as-top-level text)
+         (require 'org-datetree)
+         (org-datetree-find-date-create d1)
+         (org-agenda-insert-diary-make-new-entry text))
+       (org-insert-time-stamp (org-time-from-absolute
+                               (calendar-absolute-from-gregorian d1))
+                              nil nil nil nil time2))
+      (end-of-line 0))
+     ((eq type 'block)
+      (if (> (calendar-absolute-from-gregorian d1)
+            (calendar-absolute-from-gregorian d2))
+         (setq d1 (prog1 d2 (setq d2 d1))))
+      (if (eq org-agenda-insert-diary-strategy 'top-level)
+         (org-agenda-insert-diary-as-top-level text)
+       (require 'org-datetree)
+       (org-datetree-find-date-create d1)
+       (org-agenda-insert-diary-make-new-entry text))
+      (org-insert-time-stamp (org-time-from-absolute
+                             (calendar-absolute-from-gregorian d1)))
+      (insert "--")
+      (org-insert-time-stamp (org-time-from-absolute
+                             (calendar-absolute-from-gregorian d2)))
+      (end-of-line 0)))
+    (if (string-match "\\S-" text)
+       (progn
+         (set-window-configuration cw)
+         (message "%s entry added to %s"
+                  (capitalize (symbol-name type))
+                  (abbreviate-file-name org-agenda-diary-file)))
+      (org-reveal t)
+      (message "Please finish entry here"))))
+
+(defun org-agenda-insert-diary-as-top-level (text)
+  "Make new entry as a top-level entry at the end of the file.
+Add TEXT as headline, and position the cursor in the second line so that
+a timestamp can be added there."
+  (widen)
+  (goto-char (point-max))
+  (or (bolp) (insert "\n"))
+  (insert "* " text "\n")
+  (if org-adapt-indentation (org-indent-to-column 2)))
+
+(defun org-agenda-insert-diary-make-new-entry (text)
+  "Make new entry as last child of current entry.
+Add TEXT as headline, and position the cursor in the second line so that
+a timestamp can be added there."
+  (let ((org-show-following-heading t)
+       (org-show-siblings t)
+       (org-show-hierarchy-above t)
+       (org-show-entry-below t)
+       col)
+    (outline-next-heading)
+    (org-back-over-empty-lines)
+    (or (looking-at "[ \t]*$")
+       (progn (insert "\n") (backward-char 1)))
+    (org-insert-heading nil t)
+    (org-do-demote)
+    (setq col (current-column))
+    (insert text "\n")
+    (if org-adapt-indentation (org-indent-to-column col))
+    (let ((org-show-following-heading t)
+         (org-show-siblings t)
+         (org-show-hierarchy-above t)
+         (org-show-entry-below t))
+      (org-show-context))))
+
 (defun org-agenda-diary-entry ()
   "Make a diary entry, like the `i' command from the calendar.
-All the standard commands work: block, weekly etc."
+All the standard commands work: block, weekly etc.
+When `org-agenda-diary-file' points to a file,
+`org-agenda-diary-entry-in-org-file' is called instead to create
+entries in that Org-mode file."
   (interactive)
   (org-agenda-check-type t 'agenda 'timeline)
-  (require 'diary-lib)
-  (let* ((char (progn
-                (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
-                (read-char-exclusive)))
-        (cmd (cdr (assoc char
-                         '((?d . insert-diary-entry)
-                           (?w . insert-weekly-diary-entry)
-                           (?m . insert-monthly-diary-entry)
-                           (?y . insert-yearly-diary-entry)
-                           (?a . insert-anniversary-diary-entry)
-                           (?b . insert-block-diary-entry)
-                           (?c . insert-cyclic-diary-entry)))))
-        (oldf (symbol-function 'calendar-cursor-to-date))
-;       (buf (get-file-buffer (substitute-in-file-name diary-file)))
-        (point (point))
-        (mark (or (mark t) (point))))
-    (unless cmd
-      (error "No command associated with <%c>" char))
-    (unless (and (get-text-property point 'day)
-                (or (not (equal ?b char))
-                    (get-text-property mark 'day)))
-      (error "Don't know which date to use for diary entry"))
-    ;; We implement this by hacking the `calendar-cursor-to-date' function
-    ;; and the `calendar-mark-ring' variable.  Saves a lot of code.
-    (let ((calendar-mark-ring
-          (list (calendar-gregorian-from-absolute
-                 (or (get-text-property mark 'day)
-                     (get-text-property point 'day))))))
-      (unwind-protect
-         (progn
-           (fset 'calendar-cursor-to-date
-                 (lambda (&optional error dummy)
-                   (calendar-gregorian-from-absolute
-                    (get-text-property point 'day))))
+  (if (not (eq org-agenda-diary-file 'diary-file))
+      (org-agenda-diary-entry-in-org-file)
+    (require 'diary-lib)
+    (let* ((char (progn
+                  (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
+                  (read-char-exclusive)))
+          (cmd (cdr (assoc char
+                           '((?d . insert-diary-entry)
+                             (?w . insert-weekly-diary-entry)
+                             (?m . insert-monthly-diary-entry)
+                             (?y . insert-yearly-diary-entry)
+                             (?a . insert-anniversary-diary-entry)
+                             (?b . insert-block-diary-entry)
+                             (?c . insert-cyclic-diary-entry)))))
+          (oldf (symbol-function 'calendar-cursor-to-date))
+          ;; (buf (get-file-buffer (substitute-in-file-name diary-file)))
+          (point (point))
+          (mark (or (mark t) (point))))
+      (unless cmd
+       (error "No command associated with <%c>" char))
+      (unless (and (get-text-property point 'day)
+                  (or (not (equal ?b char))
+                      (get-text-property mark 'day)))
+       (error "Don't know which date to use for diary entry"))
+      ;; We implement this by hacking the `calendar-cursor-to-date' function
+      ;; and the `calendar-mark-ring' variable.  Saves a lot of code.
+      (let ((calendar-mark-ring
+            (list (calendar-gregorian-from-absolute
+                   (or (get-text-property mark 'day)
+                       (get-text-property point 'day))))))
+       (unwind-protect
+           (progn
+             (fset 'calendar-cursor-to-date
+                   (lambda (&optional error dummy)
+                     (calendar-gregorian-from-absolute
+                      (get-text-property point 'day))))
              (call-interactively cmd))
-       (fset 'calendar-cursor-to-date oldf)))))
+         (fset 'calendar-cursor-to-date oldf))))))
 
 (defun org-agenda-execute-calendar-command (cmd)
   "Execute a calendar command from the agenda, with the date associated to
@@ -6452,9 +7690,7 @@ argument, latitude and longitude will be prompted for."
         (date (calendar-gregorian-from-absolute day))
         (calendar-move-hook nil)
         (calendar-view-holidays-initially-flag nil)
-        (calendar-view-diary-initially-flag nil)
-        (view-calendar-holidays-initially nil)
-        (view-diary-entries-initially nil))
+        (calendar-view-diary-initially-flag nil))
     (calendar)
     (calendar-goto-date date)))
 
@@ -6504,23 +7740,26 @@ This is a command that has to be installed in `calendar-mode-map'."
   (eq (get-char-property (point-at-bol) 'type)
       'org-marked-entry-overlay))
 
-(defun org-agenda-bulk-mark ()
+(defun org-agenda-bulk-mark (&optional arg)
   "Mark the entry at point for future bulk action."
-  (interactive)
-  (org-agenda-check-no-diary)
-  (let* ((m (get-text-property (point) 'org-hd-marker))
-        ov)
-    (unless (org-agenda-bulk-marked-p)
-      (unless m (error "Nothing to mark at point"))
-      (push m org-agenda-bulk-marked-entries)
-      (setq ov (org-make-overlay (point-at-bol) (+ 2 (point-at-bol))))
-      (org-overlay-display ov ">>"
-                          (org-get-todo-face "TODO")
-                          'evaporate)
-      (org-overlay-put ov 'type 'org-marked-entry-overlay))
-    (beginning-of-line 2)
-    (message "%d entries marked for bulk action"
-            (length org-agenda-bulk-marked-entries))))
+  (interactive "p")
+  (dotimes (i (max arg 1))
+    (unless (org-get-at-bol 'org-agenda-diary-link)
+      (let* ((m (org-get-at-bol 'org-hd-marker))
+            ov)
+       (unless (org-agenda-bulk-marked-p)
+         (unless m (error "Nothing to mark at point"))
+         (push m org-agenda-bulk-marked-entries)
+         (setq ov (make-overlay (point-at-bol) (+ 2 (point-at-bol))))
+         (org-overlay-display ov "> "
+                              (org-get-todo-face "TODO")
+                              'evaporate)
+         (overlay-put ov 'type 'org-marked-entry-overlay))
+       (beginning-of-line 2)
+       (while (and (get-char-property (point) 'invisible) (not (eobp)))
+         (beginning-of-line 2))
+       (message "%d entries marked for bulk action"
+                (length org-agenda-bulk-marked-entries))))))
 
 (defun org-agenda-bulk-unmark ()
   "Unmark the entry at point for future bulk action."
@@ -6529,9 +7768,11 @@ This is a command that has to be installed in `calendar-mode-map'."
     (org-agenda-bulk-remove-overlays
      (point-at-bol) (+ 2 (point-at-bol)))
     (setq org-agenda-bulk-marked-entries
-         (delete (get-text-property (point-at-bol) 'org-hd-marker)
+         (delete (org-get-at-bol 'org-hd-marker)
                  org-agenda-bulk-marked-entries)))
   (beginning-of-line 2)
+  (while (and (get-char-property (point) 'invisible) (not (eobp)))
+    (beginning-of-line 2))
   (message "%d entries marked for bulk action"
           (length org-agenda-bulk-marked-entries)))
 
@@ -6550,9 +7791,9 @@ This only removes the overlays, it does not remove the markers
 from the list in `org-agenda-bulk-marked-entries'."
   (interactive)
   (mapc (lambda (ov)
-         (and (eq (org-overlay-get ov 'type) 'org-marked-entry-overlay)
-              (org-delete-overlay ov)))
-       (org-overlays-in (or beg (point-min)) (or end (point-max)))))
+         (and (eq (overlay-get ov 'type) 'org-marked-entry-overlay)
+              (delete-overlay ov)))
+       (overlays-in (or beg (point-min)) (or end (point-max)))))
 
 (defun org-agenda-bulk-remove-all-marks ()
   "Remove all marks in the agenda buffer.
@@ -6562,14 +7803,17 @@ This will remove the markers, and the overlays."
   (setq org-agenda-bulk-marked-entries nil)
   (org-agenda-bulk-remove-overlays (point-min) (point-max)))
 
-(defun org-agenda-bulk-action ()
-  "Execute an remote-editing action on all marked entries."
-  (interactive)
+(defun org-agenda-bulk-action (&optional arg)
+  "Execute an remote-editing action on all marked entries.
+The prefix arg is passed through to the command if possible."
+  (interactive "P")
   (unless org-agenda-bulk-marked-entries
     (error "No entries are marked"))
-  (message "Bulk: [r]efile [$]archive [A]rch->sib [t]odo [+/-]tag [s]chedule [d]eadline")
+  (message "Bulk: [r]efile [$]arch [A]rch->sib [t]odo [+/-]tag [s]chd [S]catter [d]eadline")
   (let* ((action (read-char-exclusive))
+        (org-log-refile (if org-log-refile 'time nil))
         (entries (reverse org-agenda-bulk-marked-entries))
+        redo-at-end
         cmd rfloc state e tag pos (cnt 0) (cntskip 0))
     (cond
      ((equal action ?$)
@@ -6583,13 +7827,15 @@ This will remove the markers, and the overlays."
                   "Refile to: "
                   (marker-buffer (car org-agenda-bulk-marked-entries))
                   org-refile-allow-creating-parent-nodes))
-      (setcar (nthcdr 3 rfloc)
-             (move-marker (make-marker) (nth 3 rfloc)
-                          (or (get-file-buffer (nth 1 rfloc))
-                              (find-buffer-visiting (nth 1 rfloc))
-                              (error "This should not happen"))))
+      (if (nth 3 rfloc)
+         (setcar (nthcdr 3 rfloc)
+                 (move-marker (make-marker) (nth 3 rfloc)
+                              (or (get-file-buffer (nth 1 rfloc))
+                                  (find-buffer-visiting (nth 1 rfloc))
+                                  (error "This should not happen")))))
 
-      (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc))))
+      (setq cmd (list 'org-agenda-refile nil (list 'quote rfloc) t)
+           redo-at-end t))
 
      ((equal action ?t)
       (setq state (org-icompleting-read
@@ -6610,20 +7856,44 @@ This will remove the markers, and the overlays."
       (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
 
      ((memq action '(?s ?d))
-      (let* ((date (org-read-date
-                   nil nil nil
-                   (if (eq action ?s) "(Re)Schedule to" "Set Deadline to")))
-            (ans org-read-date-final-answer)
+      (let* ((date (unless arg
+                    (org-read-date
+                     nil nil nil
+                     (if (eq action ?s) "(Re)Schedule to" "Set Deadline to"))))
+            (ans (if arg nil org-read-date-final-answer))
             (c1 (if (eq action ?s) 'org-agenda-schedule 'org-agenda-deadline)))
        (setq cmd `(let* ((bound (fboundp 'read-string))
                          (old (and bound (symbol-function 'read-string))))
                     (unwind-protect
                         (progn
                           (fset 'read-string (lambda (&rest ignore) ,ans))
-                          (call-interactively ',c1))
+                          (eval '(,c1 arg)))
                       (if bound
                           (fset 'read-string old)
                         (fmakunbound 'read-string)))))))
+
+     ((eq action '?S)
+      (let ((days (read-number
+                  (format "Scatter tasks across how many %sdays: "
+                          (if arg "week" "")) 7)))
+       (setq cmd
+             `(let ((distance (random ,(1+ days))))
+                (if arg
+                    (let ((dist distance)
+                          (day-of-week
+                           (calendar-day-of-week
+                            (calendar-gregorian-from-absolute (org-today)))))
+                      (dotimes (i (1+ dist))
+                        (while (member day-of-week org-agenda-weekend-days)
+                          (incf distance)
+                          (incf day-of-week)
+                          (if (= day-of-week 7)
+                              (setq day-of-week 0)))
+                        (incf day-of-week)
+                        (if (= day-of-week 7)
+                            (setq day-of-week 0)))))
+                (org-agenda-date-later distance)))))
+
      (t (error "Invalid bulk action")))
 
     ;; Sort the markers, to make sure that parents are handled before children
@@ -6649,6 +7919,7 @@ This will remove the markers, and the overlays."
        (setq cnt (1+ cnt))))
     (setq org-agenda-bulk-marked-entries nil)
     (org-agenda-bulk-remove-all-marks)
+    (when redo-at-end (org-agenda-redo))
     (message "Acted on %d entries%s"
             cnt
             (if (= cntskip 0)
@@ -6656,6 +7927,53 @@ This will remove the markers, and the overlays."
               (format ", skipped %d (disappeared before their turn)"
                       cntskip)))))
 
+;;; Flagging notes
+
+(defun org-agenda-show-the-flagging-note ()
+  "Display the flagging note in the other window.
+When called a second time in direct sequence, offer to remove the FLAGGING
+tag and (if present) the flagging note."
+  (interactive)
+  (let ((hdmarker (org-get-at-bol 'org-hd-marker))
+       (win (selected-window))
+       note heading newhead)
+    (unless hdmarker
+      (error "No linked entry at point"))
+    (if (and (eq this-command last-command)
+            (y-or-n-p "Unflag and remove any flagging note? "))
+       (progn
+         (org-agenda-remove-flag hdmarker)
+         (let ((win (get-buffer-window "*Flagging Note*")))
+           (and win (delete-window win)))
+         (message "Entry unflaged"))
+      (setq note (org-entry-get hdmarker "THEFLAGGINGNOTE"))
+      (unless note
+       (error "No flagging note"))
+      (org-kill-new note)
+      (org-switch-to-buffer-other-window "*Flagging Note*")
+      (erase-buffer)
+      (insert note)
+      (goto-char (point-min))
+      (while (re-search-forward "\\\\n" nil t)
+       (replace-match "\n" t t))
+      (goto-char (point-min))
+      (select-window win)
+      (message "Flagging note pushed to kill ring.  Press [?] again to remove tag and note"))))
+
+(defun org-agenda-remove-flag (marker)
+  "Remove the FLAGGED tag and any flagging note in the entry."
+  (let (newhead)
+    (org-with-point-at marker
+      (org-toggle-tag "FLAGGED" 'off)
+      (org-entry-delete nil "THEFLAGGINGNOTE")
+      (setq newhead (org-get-heading)))
+    (org-agenda-change-all-lines newhead marker)
+    (message "Entry unflaged")))
+
+(defun org-agenda-get-any-marker (&optional pos)
+  (or (get-text-property (or pos (point-at-bol)) 'org-hd-marker)
+      (get-text-property (or pos (point-at-bol)) 'org-marker)))
+
 ;;; Appointment reminders
 
 (defvar appt-time-msg-list)
@@ -6681,13 +7999,15 @@ either 'headline or 'category.  For example:
 will only add headlines containing IMPORTANT or headlines
 belonging to the \"Work\" category."
   (interactive "P")
-  (require 'calendar)
   (if refresh (setq appt-time-msg-list nil))
   (if (eq filter t)
       (setq filter (read-from-minibuffer "Regexp filter: ")))
   (let* ((cnt 0) ; count added events
         (org-agenda-new-buffers nil)
         (org-deadline-warning-days 0)
+        ;; Do not use `org-today' here because appt only takes
+        ;; time and without date as argument, so it may pass wrong
+        ;; information otherwise
         (today (org-date-to-gregorian
                 (time-to-days (current-time))))
         (org-agenda-restrict nil)
@@ -6730,17 +8050,12 @@ belonging to the \"Work\" category."
 
 (defun org-agenda-todayp (date)
   "Does DATE mean today, when considering `org-extend-today-until'?"
-  (let (today h)
-    (if (listp date) (setq date (calendar-absolute-from-gregorian date)))
-    (setq today (calendar-absolute-from-gregorian (calendar-current-date)))
-    (setq h (nth 2 (decode-time (current-time))))
-    (or (and (>= h org-extend-today-until)
-            (= date today))
-       (and (< h org-extend-today-until)
-            (= date (1- today))))))
+  (let ((today (org-today))
+       (date (if (and date (listp date)) (calendar-absolute-from-gregorian date)
+               date)))
+    (eq date today)))
 
 (provide 'org-agenda)
 
-;; arch-tag: 77f7565d-7c4b-44af-a2df-9f6f7070cff1
 
 ;;; org-agenda.el ends here