Refill some long/short copyright headers.
[bpt/emacs.git] / lisp / org / org-agenda.el
index 3a24430..9adc180 100644 (file)
@@ -1,12 +1,11 @@
 ;;; org-agenda.el --- Dynamic task and appointment lists for Org
 
-;; Copyright (C) 2004, 2005, 2006, 2007, 2008
-;;   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.09a
+;; Version: 7.4
 ;;
 ;; This file is part of GNU Emacs.
 ;;
 
 (require 'org)
 (eval-when-compile
-  (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)
@@ -72,33 +84,77 @@ only needed when the text to be killed contains more than N non-white lines."
   :type '(choice
          (const :tag "Never" nil)
          (const :tag "Always" t)
-         (number :tag "When more than N 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)
 
+(defcustom org-agenda-block-separator ?=
+  "The separator between blocks in the agenda.
+If this is a string, it will be used as the separator, with a newline added.
+If it is a character, it will be repeated to fill the window width."
+  :group 'org-agenda
+  :type '(choice
+         (character)
+         (string)))
+
 (defgroup org-agenda-export nil
  "Options concerning exporting agenda views in Org-mode."
  :tag "Org Agenda Export"
  :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)
 
 (defcustom org-agenda-exporter-settings nil
   "Alist of variable/value pairs that should be active during agenda export.
-This is a good place to set uptions for ps-print and for htmlize."
+This is a good place to set options for ps-print and for htmlize.
+Note that the way this is implemented, the values will be evaluated
+before assigned to the variables.  So make sure to quote values you do
+*not* want evaluated, for example
+
+   (setq org-agenda-exporter-settings
+         '((ps-print-color-p 'black-white)))"
   :group 'org-agenda-export
   :type '(repeat
          (list
           (variable)
           (sexp :tag "Value"))))
 
+(defcustom org-agenda-before-write-hook '(org-agenda-add-entry-text)
+  "Hook run in temporary buffer before writing it to an export file.
+A useful function is `org-agenda-add-entry-text'."
+  :group 'org-agenda-export
+  :type 'hook
+  :options '(org-agenda-add-entry-text))
+
+(defcustom org-agenda-add-entry-text-maxlines 0
+  "Maximum number of entry text lines to be added to agenda.
+This is only relevant when `org-agenda-add-entry-text' is part of
+`org-agenda-before-write-hook', which it is by default.
+When this is 0, nothing will happen.  When it is greater than 0, it
+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 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.
+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."
+  :group 'org-agenda
+  :type 'boolean)
+
 (defcustom org-agenda-export-html-style ""
   "The style specification for exported HTML Agenda files.
 If this variable contains a string, it will replace the default <style>
@@ -119,6 +175,9 @@ the fonts used by the agenda, here is an example:
           color: #cc6666;
           font-weight: bold;
        }
+       .org-agenda-done {
+          color: #339933;
+       }
        .org-done {
           color: #339933;
        }
@@ -138,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"
@@ -150,21 +214,23 @@ you can \"misuse\" it to also add other text to the header.  However,
     (const tag-down) (const tag-up)
     (const priority-up) (const priority-down)
     (const todo-state-up) (const todo-state-down)
-    (const effort-up) (const effort-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.")
 
 (defconst org-agenda-custom-commands-local-options
   `(repeat :tag "Local settings for this command. Remember to quote values"
           (choice :tag "Setting"
-           (list :tag "Any variable"
-                 (variable :tag "Variable")
-                 (sexp :tag "Value"))
+           (list :tag "Heading for this block"
+                 (const org-agenda-overriding-header)
+                 (string :tag "Headline"))
            (list :tag "Files to be searched"
                  (const org-agenda-files)
                  (list
                   (const :format "" quote)
-                  (repeat
-                          (file))))
+                  (repeat (file))))
            (list :tag "Sorting strategy"
                  (const org-agenda-sorting-strategy)
                  (list
@@ -175,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"))
@@ -184,13 +254,26 @@ you can \"misuse\" it to also add other text to the header.  However,
                  (const org-agenda-start-on-weekday)
                  (choice :value 1
                          (const :tag "Today" nil)
-                         (number :tag "Weekday No.")))
+                         (integer :tag "Weekday No.")))
            (list :tag "Include data from diary"
                  (const org-agenda-include-diary)
                  (boolean))
            (list :tag "Deadline Warning days"
                  (const org-deadline-warning-days)
                  (integer :value 1))
+           (list :tag "Tags filter preset"
+                 (const org-agenda-filter-preset)
+                 (list
+                  (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)
@@ -198,7 +281,7 @@ you can \"misuse\" it to also add other text to the header.  However,
                   (const :format "" quote)
                   (list
                    (choice
-                    :tag "Skiping range"
+                    :tag "Skipping range"
                     (const :tag "Skip entry" org-agenda-skip-entry-if)
                     (const :tag "Skip subtree" org-agenda-skip-subtree-if))
                    (repeat :inline t :tag "Conditions for skipping"
@@ -206,14 +289,37 @@ 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)
-                            (const :tag "no deadline" 'notdeadline))))))
+                            (const :tag "no deadline" 'notdeadline)
+                            (const :tag "timestamp" 'timestamp)
+                            (const :tag "no timestamp" 'nottimestamp))))))
            (list :tag "Non-standard skipping condition"
                  :value (org-agenda-skip-function)
                  (const org-agenda-skip-function)
-                 (sexp :tag "Function or form (quoted!)"))))
+                 (sexp :tag "Function or form (quoted!)"))
+           (list :tag "Any variable"
+                 (variable :tag "Variable")
+                 (sexp :tag "Value (sexp)"))))
   "Selection of examples for agenda command settings.
 This will be spliced into the custom type of
 `org-agenda-custom-commands'.")
@@ -253,7 +359,7 @@ files     A list of files file to write the produced agenda buffer to
           with the command `org-store-agenda-views'.
           If a file name ends in \".html\", an HTML version of the buffer
           is written out.  If it ends in \".ps\", a postscript version is
-          produced.  Otherwide, only the plain text is written to the file.
+          produced.  Otherwise, only the plain text is written to the file.
 
 You can also define a set of commands, to create a composite agenda buffer.
 In this case, an entry looks like this:
@@ -298,8 +404,8 @@ should provide a description for the prefix, like
                  (const :tag "TODO list" alltodo)
                  (const :tag "Search words" search)
                  (const :tag "Stuck projects" stuck)
-                 (const :tag "Tags search (all agenda files)" tags)
-                 (const :tag "Tags search of TODO entries (all agenda files)" tags-todo)
+                 (const :tag "Tags/Property match (all agenda files)" tags)
+                 (const :tag "Tags/Property match of TODO entries (all agenda files)" tags-todo)
                  (const :tag "TODO keyword search (all agenda files)" todo)
                  (const :tag "Tags sparse tree (current buffer)" tags-tree)
                  (const :tag "TODO keyword tree (current buffer)" todo-tree)
@@ -356,7 +462,7 @@ should provide a description for the prefix, like
 
 (defcustom org-agenda-query-register ?o
   "The register holding the current query string.
-The prupose of this is that if you construct a query string interactively,
+The purpose of this is that if you construct a query string interactively,
 you can then use it to define a custom command."
   :group 'org-agenda-custom-commands
   :type 'character)
@@ -365,7 +471,8 @@ you can then use it to define a custom command."
   '("+LEVEL=2/-DONE" ("TODO" "NEXT" "NEXTACTION") nil "")
   "How to identify stuck projects.
 This is a list of four items:
-1. A tags/todo matcher string that is used to identify a project.
+1. A tags/todo/property matcher string that is used to identify a project.
+   See the manual for a description of tag and property searches.
    The entire tree below a headline matched by this is considered one project.
 2. A list of TODO keywords identifying non-stuck projects.
    If the project subtree contains any headline with one of these todo
@@ -374,9 +481,18 @@ This is a list of four items:
 3. A list of tags identifying non-stuck projects.
    If the project subtree contains any headline with one of these tags,
    the project is considered to be not stuck.  If you specify \"*\" as
-   a tag, any tag will mark the project unstuck.
+   a tag, any tag will mark the project unstuck.  Note that this is about
+   the explicit presence of a tag somewhere in the subtree, inherited
+   tags to not count here.  If inherited tags make a project not stuck,
+   use \"-TAG\" in the tags part of the matcher under (1.) above.
 4. An arbitrary regular expression matching non-stuck projects.
 
+If the project turns out to be not stuck, search continues also in the
+subtree to see if any of the subtasks have project status.
+
+See also the variable `org-tags-match-list-sublevels' which applies
+to projects matched by this search as well.
+
 After defining this variable, you may use \\[org-agenda-list-stuck-projects]
 or `C-c a #' to produce the list."
   :group 'org-agenda-custom-commands
@@ -384,62 +500,178 @@ or `C-c a #' to produce the list."
          (string :tag "Tags/TODO match to identify a project")
          (repeat :tag "Projects are *not* stuck if they have an entry with TODO keyword any of" (string))
          (repeat :tag "Projects are *not* stuck if they have an entry with TAG being any of" (string))
-         (regexp :tag "Projects are *not* stuck if this regexp matches\ninside the subtree")))
+         (regexp :tag "Projects are *not* stuck if this regexp matches inside the subtree")))
 
+(defcustom org-agenda-filter-effort-default-operator "<"
+  "The default operator for effort estimate filtering.
+If you select an effort estimate limit without first pressing an operator,
+this one will be used."
+  :group 'org-agenda-custom-commands
+  :type '(choice (const :tag "less or equal" "<")
+                (const :tag "greater or equal"">")
+                (const :tag "equal" "=")))
 
 (defgroup org-agenda-skip nil
  "Options concerning skipping parts of agenda files."
  :tag "Org Agenda Skip"
  :group 'org-agenda)
+(defgroup org-agenda-daily/weekly nil
+  "Options concerning the daily/weekly agenda."
+  :tag "Org Agenda Daily/Weekly"
+  :group 'org-agenda)
+(defgroup org-agenda-todo-list nil
+  "Options concerning the global todo list agenda view."
+  :tag "Org Agenda Todo List"
+  :group 'org-agenda)
+(defgroup org-agenda-match-view nil
+  "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 teh COMMENT keyword.
-When nil, these trees are also scand by agenda commands."
+  "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
-  :group 'org-todo
+  :group 'org-agenda-todo-list
   :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
 of `org-agenda-todo-ignore-scheduled' and `org-agenda-todo-ignore-deadlines'
-will be ignored."
+will be ignored.
+See also the variable `org-agenda-tags-todo-honor-ignore-options'."
   :group 'org-agenda-skip
-  :group 'org-todo
+  :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.
-See also `org-agenda-todo-ignore-with-date'."
+  "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' if you want
+to make his option also apply to the tags-todo list."
   :group 'org-agenda-skip
-  :group 'org-todo
-  :type 'boolean)
+  :group 'org-agenda-todo-list
+  :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.
-See also `org-agenda-todo-ignore-with-date'."
+  "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' 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 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.
+The variables
+   `org-agenda-todo-ignore-with-date',
+   `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
+tags-todo search, which is the general tags/property matcher
+restricted to unfinished TODO entries only."
   :group 'org-agenda-skip
-  :group 'org-todo
+  :group 'org-agenda-todo-list
+  :group 'org-agenda-match-view
   :type 'boolean)
 
 (defcustom org-agenda-skip-scheduled-if-done nil
@@ -449,24 +681,91 @@ it applies only to the actual date of the scheduling.  Warnings about
 an item with a past scheduling dates are always turned off when the item
 is DONE."
   :group 'org-agenda-skip
+  :group 'org-agenda-daily/weekly
   :type 'boolean)
 
+(defcustom org-agenda-skip-scheduled-if-deadline-is-shown nil
+  "Non-nil means skip scheduling line if same entry shows because of deadline.
+In the agenda of today, an entry can show up multiple times because
+it is both scheduled and has a nearby deadline, and maybe a plain time
+stamp as well.
+When this variable is t, then only the deadline is shown and the fact that
+the entry is scheduled today or was scheduled previously is not shown.
+When this variable is nil, the entry will be shown several times.  When
+the variable is the symbol `not-today', then skip scheduled previously,
+but not scheduled today."
+  :group 'org-agenda-skip
+  :group 'org-agenda-daily/weekly
+  :type '(choice
+         (const :tag "Never" nil)
+         (const :tag "Always" t)
+         (const :tag "Not when scheduled today" not-today)))
+
 (defcustom org-agenda-skip-deadline-if-done nil
-  "Non-nil means don't show deadines when the corresponding item is done.
+  "Non-nil means don't show deadlines when the corresponding item is done.
 When nil, the deadline is still shown and should give you a happy feeling.
 This is relevant for the daily/weekly agenda.  And it applied only to the
-actualy date of the deadline.  Warnings about approching and past-due
+actually date of the deadline.  Warnings about approaching and past-due
 deadlines are always turned off when the item is DONE."
+  :group 'org-agenda-skip
+  :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
+entry, the rest of the entry will not be searched."
   :group 'org-agenda-skip
   :type 'boolean)
 
 (defcustom org-agenda-skip-timestamp-if-done nil
   "Non-nil means don't select item by timestamp or -range if it is DONE."
   :group 'org-agenda-skip
+  :group 'org-agenda-daily/weekly
   :type 'boolean)
 
+(defcustom org-agenda-dim-blocked-tasks t
+  "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 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
+not show up in the agenda views, making this task invisible you remove any
+trace from agenda views that there is something to do.  Therefore, a task
+that is blocked because of checkboxes will never be made invisible, it
+will only be dimmed."
+  :group 'org-agenda-daily/weekly
+  :group 'org-agenda-todo-list
+  :type '(choice
+         (const :tag "Do not dim" nil)
+         (const :tag "Dim to a grey face" t)
+         (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
@@ -475,32 +774,82 @@ N days, just insert a special line indicating the size of the gap."
   :type '(choice
          (const :tag "None" nil)
          (const :tag "All" t)
-         (number :tag "at most")))
+         (integer :tag "at most")))
 
 (defgroup org-agenda-startup nil
   "Options concerning initial settings in the Agenda in Org Mode."
   :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)
+
+(defcustom org-agenda-start-with-entry-text-mode nil
+  "The initial value of entry-text-mode in a newly created agenda window."
   :group 'org-agenda-startup
   :type 'boolean)
 
+(defcustom org-agenda-entry-text-maxlines 5
+  "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
+`org-agenda-add-entry-ext-maxlines'."
+  :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."
@@ -512,10 +861,11 @@ Needs to be set before org.el is loaded."
 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
@@ -532,36 +882,46 @@ 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)
 
-(defgroup org-agenda-daily/weekly nil
-  "Options concerning the daily/weekly agenda."
-  :tag "Org Agenda Daily/Weekly"
-  :group 'org-agenda)
+(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)
 
-(defcustom org-agenda-ndays 7
+(make-obsolete-variable 'org-agenda-ndays 'org-agenda-span "24.1")
+
+(defcustom org-agenda-span 'week
   "Number of days to include in overview display.
-Should be 1 or 7."
+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 'number)
+  :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."
+When nil, always start on the current day.
+Custom commands can set this variable in the options section."
   :group 'org-agenda-daily/weekly
   :type '(choice (const :tag "Today" nil)
-                (number :tag "Weekday No.")))
+                (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)
@@ -600,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
@@ -615,20 +1010,28 @@ and timeline buffers."
              (const :tag "Sunday" 0)))
 
 (defcustom org-agenda-include-diary nil
-  "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
+  "If non-nil, include in the agenda entries from the Emacs Calendar's diary.
+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
-the entries for specific days."
+the entries for specific days.
+This option is deprecated, it is better to define a block agenda instead."
   :group 'org-agenda-daily/weekly
   :type 'boolean)
 
 (defcustom org-agenda-repeating-timestamp-show-all t
-  "Non-nil means, show all occurences of a repeating stamp in the agenda.
-When nil, only one occurence is shown, either today or the
+  "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
   :type 'boolean)
@@ -639,7 +1042,33 @@ When an item is scheduled on a date, it shows up in the agenda on this
 day and will be listed until it is marked done for the number of days
 given here."
   :group 'org-agenda-daily/weekly
-  :type 'number)
+  :type 'integer)
+
+(defcustom org-agenda-log-mode-items '(closed clock)
+  "List of items that should be shown in agenda log mode.
+This list may contain the following symbols:
+
+  closed    Show entries that have been closed on that day.
+  clock     Show entries that have received clocked time on that day.
+  state     Show all logged state changes.
+Note that instead of changing this variable, you can also press `C-u l' in
+the agenda to display all available LOG items temporarily."
+  :group 'org-agenda-daily/weekly
+  :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.
+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."
+  :group 'org-agenda-daily/weekly
+  :type 'boolean)
+
+(defcustom org-agenda-start-with-log-mode nil
+  "The initial value of log-mode in a newly created agenda window."
+  :group 'org-agenda-startup
+  :group 'org-agenda-daily/weekly
+  :type 'boolean)
 
 (defcustom org-agenda-start-with-clockreport-mode nil
   "The initial value of clockreport-mode in a newly created agenda window."
@@ -658,14 +1087,61 @@ 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."
   :tag "Org Agenda Time Grid"
   :group 'org-agenda)
 
+(defcustom org-agenda-search-headline-for-time t
+  "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
+time or so, and then this produces an unwanted side effect.  If this is
+the case for your, use this variable to turn off searching the headline
+for a time."
+  :group 'org-agenda-time-grid
+  :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.
@@ -712,29 +1188,35 @@ a grid line."
   :group 'org-agenda)
 
 (defcustom org-agenda-sorting-strategy
-  '((agenda time-up category-keep priority-down)
-    (todo category-keep priority-down)
-    (tags category-keep priority-down)
+  '((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
 if an entry should be listed before another entry.  The following
 symbols are recognized:
 
-time-up         Put entries with time-of-day indications first, early first
-time-down       Put entries with time-of-day indications first, late first
-category-keep   Keep the default order of categories, corresponding to the
-               sequence in `org-agenda-files'.
-category-up     Sort alphabetically by category, A-Z.
-category-down   Sort alphabetically by category, Z-A.
-tag-up          Sort alphabetically by last tag, A-Z.
-tag-down        Sort alphabetically by last tag, Z-A.
-priority-up     Sort numerically by priority, high priority last.
-priority-down   Sort numerically by priority, high priority first.
-todo-state-up   Sort by todo state, tasks that are done last.
-todo-state-down Sort by todo state, tasks that are done first.
-effort-up       Sort numerically by estimated effort, high effort last.
-effort-down     Sort numerically by estimated effort, high effort first.
+time-up            Put entries with time-of-day indications first, early first
+time-down          Put entries with time-of-day indications first, late first
+category-keep      Keep the default order of categories, corresponding to the
+                  sequence in `org-agenda-files'.
+category-up        Sort alphabetically by category, A-Z.
+category-down      Sort alphabetically by category, Z-A.
+tag-up             Sort alphabetically by last tag, A-Z.
+tag-down           Sort alphabetically by last tag, Z-A.
+priority-up        Sort numerically by priority, high priority last.
+priority-down      Sort numerically by priority, high priority first.
+todo-state-up      Sort by todo state, tasks that are done last.
+todo-state-down    Sort by todo state, tasks that are done first.
+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
@@ -751,7 +1233,9 @@ 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
   :type `(choice
          (repeat :tag "General" ,org-sorting-choice)
@@ -761,10 +1245,22 @@ contents, with a context symbol in the car of the list, any of
                (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
+  "A function to define the comparison `user-defined'.
+This function must receive two arguments, agenda entry a and b.
+If a>b, return +1.  If a<b, return -1.  If they are equal as seen by
+the user comparison, return nil.
+When this is defined, you can make `user-defined-up' and `user-defined-down'
+part of an agenda sorting strategy."
+  :group 'org-agenda-sorting
+  :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
@@ -774,7 +1270,10 @@ 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.
 When nil, such items are sorted as 0 minutes effort."
   :group 'org-agenda-sorting
   :type 'boolean)
@@ -785,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
@@ -798,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
@@ -820,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
@@ -837,7 +1338,9 @@ the prefix, you could use:
   (setq org-agenda-prefix-format \"  %-11:c% s\")
 
 See also the variables `org-agenda-remove-times-when-in-prefix' and
-`org-agenda-remove-tags'."
+`org-agenda-remove-tags'.
+
+Custom commands can set this variable in the options section."
   :type '(choice
          (string :tag "General format")
          (list :greedy t :tag "View dependent"
@@ -859,8 +1362,22 @@ to occupy a fixed space in the agenda display."
   :group 'org-agenda-line-format
   :type 'string)
 
+(defcustom org-agenda-timerange-leaders '("" "(%d/%d): ")
+  "Text preceding timerange entries in the agenda view.
+This is a list with two strings.  The first applies when the range
+is entirely on one day.  The second applies if the range spans several days.
+The strings may have two \"%d\" format specifiers which will be filled
+with the sequence number of the days, and the total number of days in the
+range, respectively."
+  :group 'org-agenda-line-format
+  :type '(list
+         (string :tag "Deadline today   ")
+         (choice :tag "Deadline relative"
+                 (string :tag "Format string")
+                 (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
@@ -872,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
@@ -886,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
@@ -894,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
@@ -902,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.
@@ -912,9 +1442,23 @@ When non-nil, this must be the number of minutes, e.g. 60 for one hour."
          (integer :tag "Minutes")
          (const :tag "No default duration")))
 
+(defcustom org-agenda-show-inherited-tags t
+  "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
@@ -927,7 +1471,7 @@ When this is the symbol `prefix', only remove tags when
     (defvaralias 'org-agenda-remove-tags-when-in-prefix
       'org-agenda-remove-tags))
 
-(defcustom org-agenda-tags-column -80
+(defcustom org-agenda-tags-column (if (featurep 'xemacs) -79 -80)
   "Shift tags in agenda items to this column.
 If this number is positive, it specifies the column.  If it is negative,
 it means that the tags should be flushright to that column.  For example,
@@ -938,22 +1482,75 @@ it means that the tags should be flushright to that column.  For example,
 (if (fboundp 'defvaralias)
     (defvaralias 'org-agenda-align-tags-to-column 'org-agenda-tags-column))
 
-(defcustom org-agenda-fontify-priorities t
-  "Non-nil means, highlight low and high priorities in agenda.
+(defcustom org-agenda-fontify-priorities 'cookies
+  "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.
+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 names 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)
          (const :tag "Defaults" t)
+         (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."
@@ -961,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
@@ -976,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
@@ -987,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
@@ -996,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
@@ -1013,16 +1634,22 @@ 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.
 (defvar org-agenda-follow-mode nil)
+(defvar org-agenda-entry-text-mode nil)
 (defvar org-agenda-clockreport-mode nil)
 (defvar org-agenda-show-log nil)
 (defvar org-agenda-redo-command nil)
 (defvar org-agenda-query-string nil)
-(defvar org-agenda-mode-hook nil)
+(defvar org-agenda-mode-hook nil
+  "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
 
 (defun org-agenda-mode ()
   "Mode for time-sorted view on action items in Org-mode files.
@@ -1033,7 +1660,8 @@ The following commands are available:
   (interactive)
   (kill-all-local-variables)
   (setq org-agenda-undo-list nil
-       org-agenda-pending-undo-list nil)
+       org-agenda-pending-undo-list nil
+       org-agenda-bulk-marked-entries nil)
   (setq major-mode 'org-agenda-mode)
   ;; Keep global-font-lock-mode from turning on font-lock-mode
   (org-set-local 'font-lock-global-modes (list 'not major-mode))
@@ -1041,6 +1669,7 @@ The following commands are available:
   (use-local-map org-agenda-mode-map)
   (easy-menu-add org-agenda-menu)
   (if org-startup-truncated (setq truncate-lines t))
+  (org-set-local 'line-move-visual nil)
   (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
   (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
   ;; Make sure properties are removed when copying text
@@ -1051,8 +1680,10 @@ The following commands are available:
                         buffer-substring-filters)))
   (unless org-agenda-keep-modes
     (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
+         org-agenda-entry-text-mode org-agenda-start-with-entry-text-mode
          org-agenda-clockreport-mode org-agenda-start-with-clockreport-mode
-         org-agenda-show-log nil))
+         org-agenda-show-log org-agenda-start-with-log-mode))
+
   (easy-menu-change
    '("Agenda") "Agenda Files"
    (append
@@ -1076,36 +1707,45 @@ The following commands are available:
 (org-defkey org-agenda-mode-map [(tab)]    'org-agenda-goto)
 (org-defkey org-agenda-mode-map "\C-m"     'org-agenda-switch-to)
 (org-defkey org-agenda-mode-map "\C-k"     'org-agenda-kill)
-(org-defkey org-agenda-mode-map "\C-c$"    'org-agenda-archive)
+(org-defkey org-agenda-mode-map "\C-c\C-w" 'org-agenda-refile)
+(org-defkey org-agenda-mode-map "m"        'org-agenda-bulk-mark)
+(org-defkey org-agenda-mode-map "u"        'org-agenda-bulk-unmark)
+(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\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 "\C-c\C-t" 'org-agenda-todo)
+(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)
-(org-defkey org-agenda-mode-map "b"        'org-agenda-tree-to-indirect-buffer)
 (org-defkey org-agenda-mode-map "o"        'delete-other-windows)
 (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)
 (org-defkey org-agenda-mode-map "j"        'org-agenda-goto-date)
 (org-defkey org-agenda-mode-map "d"        'org-agenda-day-view)
 (org-defkey org-agenda-mode-map "w"        'org-agenda-week-view)
-(org-defkey org-agenda-mode-map "m"        'org-agenda-month-view)
 (org-defkey org-agenda-mode-map "y"        'org-agenda-year-view)
 (org-defkey org-agenda-mode-map "\C-c\C-z" 'org-agenda-add-note)
 (org-defkey org-agenda-mode-map "z"        'org-agenda-add-note)
 (org-defkey org-agenda-mode-map "k"        'org-agenda-action)
 (org-defkey org-agenda-mode-map "\C-c\C-x\C-k" 'org-agenda-action)
-(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-date-later)
-(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-date-earlier)
-(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
-(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
+(org-defkey org-agenda-mode-map [(shift right)] 'org-agenda-do-date-later)
+(org-defkey org-agenda-mode-map [(shift left)] 'org-agenda-do-date-earlier)
+(org-defkey org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-do-date-later)
+(org-defkey org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-do-date-earlier)
 
 (org-defkey org-agenda-mode-map ">" 'org-agenda-date-prompt)
 (org-defkey org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
@@ -1114,24 +1754,34 @@ The following commands are available:
   (while l (org-defkey org-agenda-mode-map
             (int-to-string (pop l)) 'digit-argument)))
 
-(org-defkey org-agenda-mode-map "f" 'org-agenda-follow-mode)
+(org-defkey org-agenda-mode-map "F" 'org-agenda-follow-mode)
 (org-defkey org-agenda-mode-map "R" 'org-agenda-clockreport-mode)
+(org-defkey org-agenda-mode-map "E" 'org-agenda-entry-text-mode)
 (org-defkey org-agenda-mode-map "l" 'org-agenda-log-mode)
-(org-defkey org-agenda-mode-map "v" 'org-agenda-archives-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)
-(org-defkey org-agenda-mode-map "e" 'org-agenda-execute)
+(org-defkey org-agenda-mode-map "e" 'org-agenda-set-effort)
+(org-defkey org-agenda-mode-map "\C-c\C-xe" 'org-agenda-set-effort)
+(org-defkey org-agenda-mode-map "\C-c\C-x\C-e"
+           'org-clock-modify-effort-estimate)
+(org-defkey org-agenda-mode-map "\C-c\C-xp" 'org-agenda-set-property)
 (org-defkey org-agenda-mode-map "q" 'org-agenda-quit)
 (org-defkey org-agenda-mode-map "x" 'org-agenda-exit)
 (org-defkey org-agenda-mode-map "\C-x\C-w" 'org-write-agenda)
-(org-defkey org-agenda-mode-map "s" 'org-save-all-org-buffers)
 (org-defkey org-agenda-mode-map "\C-x\C-s" 'org-save-all-org-buffers)
+(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)
@@ -1151,55 +1801,121 @@ 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)
 (org-defkey org-agenda-mode-map [(shift down)] 'org-agenda-priority-down)
 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
 (org-defkey org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
-(org-defkey org-agenda-mode-map [(right)] 'org-agenda-later)
-(org-defkey org-agenda-mode-map [(left)] 'org-agenda-earlier)
+(org-defkey org-agenda-mode-map "f" 'org-agenda-later)
+(org-defkey org-agenda-mode-map "b" 'org-agenda-earlier)
 (org-defkey org-agenda-mode-map "\C-c\C-x\C-c" 'org-agenda-columns)
+(org-defkey org-agenda-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
 
 (org-defkey org-agenda-mode-map "[" 'org-agenda-manipulate-query-add)
 (org-defkey org-agenda-mode-map "]" 'org-agenda-manipulate-query-subtract)
 (org-defkey org-agenda-mode-map "{" 'org-agenda-manipulate-query-add-re)
 (org-defkey org-agenda-mode-map "}" 'org-agenda-manipulate-query-subtract-re)
 (org-defkey org-agenda-mode-map "/" 'org-agenda-filter-by-tag)
-
-(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 "\\" '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)
+
+(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"
+    ("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])
-    ["Delete subtree" org-agenda-kill t]
-    ["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)]
+     ["Archive subtree" org-agenda-archive t]
+     "--"
+     ["Refile" org-agenda-refile t]
+     "--"
+     ["Delete subtree" org-agenda-kill t])
+    ("Bulk action"
+     ["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"])
     "--"
     ("Tags and Properties"
      ["Show all Tags" org-agenda-show-tags t]
@@ -1207,7 +1923,7 @@ The following commands are available:
      ["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]
      "--"
@@ -1218,12 +1934,20 @@ The following commands are available:
      "--"
      ["Change Date +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
      ["Change Date -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
+     ["Change Time +1 hour" org-agenda-do-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-right"]
+     ["Change Time -1 hour" org-agenda-do-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u S-left"]
+     ["Change Time +  min" org-agenda-date-later :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-right"]
+     ["Change Time -  min" org-agenda-date-earlier :active (org-agenda-check-type nil 'agenda 'timeline) :keys "C-u C-u S-left"]
      ["Change Date to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
-    ("Clock"
+    ("Clock and Effort"
      ["Clock in" org-agenda-clock-in t]
      ["Clock out" org-agenda-clock-out t]
      ["Clock cancel" org-agenda-clock-cancel t]
-     ["Goto running clock" org-clock-goto t])
+     ["Goto running clock" org-clock-goto t]
+     "--"
+     ["Set Effort" org-agenda-set-effort t]
+     ["Change clocked effort" org-clock-modify-effort-estimate
+      (org-clock-is-active)])
     ("Priority"
      ["Set Priority" org-agenda-priority t]
      ["Increase Priority" org-agenda-priority-up t]
@@ -1237,43 +1961,18 @@ 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])
-    "--"
-    ("View"
-     ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (equal org-agenda-ndays 1)]
-     ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (equal org-agenda-ndays 7)]
-     ["Month View" org-agenda-month-view :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (member org-agenda-ndays '(28 29 30 31))]
-     ["Year View" org-agenda-year-view :active (org-agenda-check-type nil 'agenda)
-      :style radio :selected (member org-agenda-ndays '(365 366))]
-     "--"
-     ["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 Logbook entries" org-agenda-log-mode
-      :style toggle :selected org-agenda-show-log
-      :active (org-agenda-check-type nil 'agenda 'timeline)]
-     ["Include archived trees" org-agenda-archives-mode 
-      :style toggle :selected org-agenda-archives-mode :active t]
-     ["Include archive files" (org-agenda-archives-mode t)
-      :style toggle :selected (eq org-agenda-archives-mode t) :active t
-      :keys "C-u v"])
-    ["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]
+     ["Create iCalendar File" org-export-icalendar-combine-agenda-files t])
     "--"
     ["Undo Remote Editing" org-agenda-undo org-agenda-undo-list]
     "--"
+    ("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]
+     "--"
+     ["Setup" (progn (require 'org-mobile) (customize-group 'org-mobile)) t])
+    "--"
     ["Quit" org-agenda-quit t]
     ["Exit and Release Buffers" org-agenda-exit t]
     ))
@@ -1281,13 +1980,13 @@ 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
   "Buffers that have already seen `undo-start' in the current undo sequence.")
 (defvar org-agenda-pending-undo-list nil
-  "In a series of undo commands, this is the list of remaning undo items.")
+  "In a series of undo commands, this is the list of remaining undo items.")
 
 
 (defun org-agenda-undo ()
@@ -1296,7 +1995,7 @@ This undoes changes both in the agenda buffer and in the remote buffer
 that have been changed along."
   (interactive)
   (or org-agenda-allow-remote-undo
-      (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo."))
+      (error "Check the variable `org-agenda-allow-remote-undo' to activate remote undo"))
   (if (not (eq this-command last-command))
       (setq org-agenda-undo-has-started-in nil
            org-agenda-pending-undo-list org-agenda-undo-list))
@@ -1321,7 +2020,7 @@ that have been changed along."
                            (not (car pending-undo-list)))
                  (pop pending-undo-list))
                (undo-more 1))))))
-    (goto-line line)
+    (org-goto-line line)
     (message "`%s' undone (buffer %s)" cmd (buffer-name rembuf))))
 
 (defun org-verify-change-for-undo (l1 l2)
@@ -1339,7 +2038,7 @@ that have been changed along."
 (defvar org-agenda-overriding-restriction nil)
 
 ;;;###autoload
-(defun org-agenda (arg &optional keys restriction)
+(defun org-agenda (&optional arg keys restriction)
   "Dispatch agenda commands to collect entries to the agenda buffer.
 Prompts for a command to execute.  Any prefix arg will be passed
 on to the selected command.  The default selections are:
@@ -1353,6 +2052,15 @@ m     Call `org-tags-view' to display headlines with tags matching
 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 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.
+>     Remove a previous restriction.
+#     List \"stuck\" projects.
+!     Configure what \"stuck\" means.
+C     Configure custom agenda commands.
 
 More commands can be added by configuring the variable
 `org-agenda-custom-commands'.  In particular, specific tags and TODO keyword
@@ -1366,6 +2074,10 @@ Pressing `<' twice means to restrict to the current subtree or region
   (interactive "P")
   (catch 'exit
     (let* ((prefix-descriptions nil)
+          (org-agenda-window-setup (if (equal (buffer-name)
+                                              org-agenda-buffer-name)
+                                       'current-window
+                                     org-agenda-window-setup))
           (org-agenda-custom-commands-orig org-agenda-custom-commands)
           (org-agenda-custom-commands
            ;; normalize different versions
@@ -1382,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))
@@ -1396,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
@@ -1412,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)
@@ -1438,7 +2152,7 @@ Pressing `<' twice means to restrict to the current subtree or region
                (org-let lprops '(org-todo-list match)))
               ((eq type 'tags-tree)
                (org-check-for-org-mode)
-               (org-let lprops '(org-tags-sparse-tree current-prefix-arg match)))
+               (org-let lprops '(org-match-sparse-tree current-prefix-arg match)))
               ((eq type 'todo-tree)
                (org-check-for-org-mode)
                (org-let lprops
@@ -1463,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"))
@@ -1493,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*")
@@ -1508,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
@@ -1522,7 +2253,7 @@ s   Search for keywords                 C   Configure custom agenda commands
        (while t
          (setq custom1 custom)
          (when (eq rmheader t)
-           (goto-line 1)
+           (org-goto-line 1)
            (re-search-forward ":" nil t)
            (delete-region (match-end 0) (point-at-eol))
            (forward-char 1)
@@ -1531,55 +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))
+                 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)
-                 (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))
-         (when (fboundp 'fit-window-to-buffer)
-           (if second-time
-               (if (not (pos-visible-in-window-p (point-max)))
-                   (fit-window-to-buffer))
-             (setq second-time t)
-             (fit-window-to-buffer)))
+         (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
@@ -1626,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)
@@ -1636,7 +2403,7 @@ s   Search for keywords                 C   Configure custom agenda commands
           (t (error "Invalid key %c" c))))))))
 
 (defun org-run-agenda-series (name series)
-  (org-prepare-agenda name)
+  (org-let (nth 1 series) '(org-prepare-agenda name))
   (let* ((org-agenda-multi t)
         (redo (list 'org-run-agenda-series name (list 'quote series)))
         (cmds (car series))
@@ -1644,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
@@ -1674,7 +2441,8 @@ s   Search for keywords                 C   Configure custom agenda commands
     (widen)
     (setq org-agenda-redo-command redo)
     (goto-char (point-min)))
-  (org-finalize-agenda))
+  (org-fit-agenda-window)
+  (org-let (nth 1 series) '(org-finalize-agenda)))
 
 ;;;###autoload
 (defmacro org-batch-agenda (cmd-key &rest parameters)
@@ -1682,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
@@ -1694,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)
 
@@ -1707,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
@@ -1756,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 ":"))))
@@ -1835,64 +2606,238 @@ so the export commands can easily use it."
          (while files
            (eval (list 'let (append org-agenda-exporter-settings opts pars)
                        (list 'org-write-agenda
-                             (expand-file-name (pop files) dir) t))))
+                             (expand-file-name (pop files) dir) nil t))))
          (and (get-buffer org-agenda-buffer-name)
               (kill-buffer org-agenda-buffer-name)))))))
 
-(defun org-write-agenda (file &optional nosettings)
+(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),
 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 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,
 higher priority settings."
-  (interactive "FWrite agenda to file: ")
+  (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
-        (cond
-         ((string-match "\\.html?\\'" file)
-          (set-buffer (htmlize-buffer (current-buffer)))
-
-          (when (and org-agenda-export-html-style
-                     (string-match "<style>" org-agenda-export-html-style))
-            ;; replace <style> section with org-agenda-export-html-style
-            (goto-char (point-min))
-            (kill-region (- (search-forward "<style") 6)
-                         (search-forward "</style>"))
-            (insert org-agenda-export-html-style))
-          (write-file file)
-          (kill-buffer (current-buffer))
-          (message "HTML written to %s" file))
-         ((string-match "\\.ps\\'" file)
-          (ps-print-buffer-with-faces file)
-          (message "Postscript written to %s" file))
-         ((string-match "\\.ics\\'" file)
-          (let ((org-agenda-marker-table
-                 (org-create-marker-find-array
-                  (org-agenda-collect-markers)))
-                (org-icalendar-verify-function 'org-check-agenda-marker-table)
-                (org-combined-agenda-icalendar-file file))
-            (apply 'org-export-icalendar 'combine
-                   (org-agenda-files nil 'ifmode))))
-         (t
-          (let ((bs (buffer-string)))
-            (find-file file)
-            (erase-buffer)
+        (org-agenda-mark-filtered-text)
+        (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)
-            (save-buffer 0)
-            (kill-buffer (current-buffer))
-            (message "Plain text written to %s" file))))))
-    (set-buffer org-agenda-buffer-name)))
+            (org-agenda-remove-marked-text 'org-filtered)
+            (while (setq beg (text-property-any (point-min) (point-max)
+                                                'org-filtered t))
+              (delete-region
+               beg (or (next-single-property-change beg 'org-filtered)
+                       (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
+                         (string-match "<style>" org-agenda-export-html-style))
+                ;; replace <style> section with org-agenda-export-html-style
+                (goto-char (point-min))
+                (kill-region (- (search-forward "<style") 6)
+                             (search-forward "</style>"))
+                (insert org-agenda-export-html-style))
+              (write-file file)
+              (kill-buffer (current-buffer))
+              (message "HTML written to %s" file))
+             ((string-match "\\.ps\\'" file)
+              (require 'ps-print)
+              (ps-print-buffer-with-faces file)
+              (message "Postscript written to %s" file))
+             ((string-match "\\.pdf\\'" file)
+              (require 'ps-print)
+              (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)
+              (let ((org-agenda-marker-table
+                     (org-create-marker-find-array
+                      (org-agenda-collect-markers)))
+                    (org-icalendar-verify-function 'org-check-agenda-marker-table)
+                    (org-combined-agenda-icalendar-file file))
+                (apply 'org-export-icalendar 'combine
+                       (org-agenda-files nil 'ifmode))))
+             (t
+              (let ((bs (buffer-string)))
+                (find-file file)
+                (erase-buffer)
+                (insert bs)
+                (save-buffer 0)
+                (kill-buffer (current-buffer))
+                (message "Plain text written to %s" file))))))))
+    (set-buffer org-agenda-buffer-name))
+  (when open (org-open-file file)))
+
+(defvar org-agenda-filter-overlays nil)
+
+(defun org-agenda-mark-filtered-text ()
+  "Mark all text hidden by filtering with a text property."
+  (let ((inhibit-read-only t))
+    (mapc
+     (lambda (o)
+       (when (equal (overlay-buffer o) (current-buffer))
+        (put-text-property
+         (overlay-start o) (overlay-end o)
+         'org-filtered t)))
+     org-agenda-filter-overlays)))
+
+(defun org-agenda-unmark-filtered-text ()
+  "Remove the filtering text property."
+  (let ((inhibit-read-only t))
+    (remove-text-properties (point-min) (point-max) '(org-filtered t))))
+
+(defun org-agenda-remove-marked-text (property &optional value)
+  "Delete all text marked with VALUE of PROPERTY.
+VALUE defaults to t."
+  (let (beg)
+    (setq value (or value t))
+    (while (setq beg (text-property-any (point-min) (point-max)
+                                       property value))
+      (delete-region
+       beg (or (next-single-property-change beg 'org-filtered)
+              (point-max))))))
+
+(defun org-agenda-add-entry-text ()
+  "Add entry text to agenda lines.
+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 (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 (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 "    > "))
+         (end-of-line 1)
+         (if (string-match "\\S-" txt)
+             (insert "\n" txt)
+           (or (eobp) (forward-char 1))))))))
+
+(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.
+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)
+       (if (not (org-mode-p))
+           (setq txt "")
+         (save-excursion
+           (save-restriction
+             (widen)
+             (goto-char marker)
+             (end-of-line 1)
+             (setq txt (buffer-substring
+                        (min (1+ (point)) (point-max))
+                        (progn (outline-next-heading) (point)))
+                   drawer-re org-drawer-regexp
+                   kwd-time-re (concat "^[ \t]*" org-keyword-time-regexp
+                                       ".*\n?"))
+             (with-temp-buffer
+               (insert txt)
+               (when org-agenda-add-entry-text-descriptive-links
+                 (goto-char (point-min))
+                 (while (org-activate-bracket-links (point-max))
+                   (add-text-properties (match-beginning 0) (match-end 0)
+                                        '(face org-link))))
+               (goto-char (point-min))
+               (while (re-search-forward org-bracket-link-regexp (point-max) t)
+                 (set-text-properties (match-beginning 0) (match-end 0)
+                                      nil))
+               (goto-char (point-min))
+               (while (re-search-forward drawer-re nil t)
+                 (delete-region
+                  (match-beginning 0)
+                  (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))
+               (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))
+               (while (not (eobp))
+                 (unless (looking-at "[ \t]*$")
+                   (setq ind (min ind (org-get-indentation))))
+                 (beginning-of-line 2))
+               (goto-char (point-min))
+               (while (not (eobp))
+                 (unless (looking-at "[ \t]*$")
+                   (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))
+               (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))
+               (when (> (org-current-line)
+                        n-lines)
+                 (org-goto-line (1+ n-lines))
+                 (backward-char 1))
+               (setq txt (buffer-substring (point-min) (point)))))))))
+    txt))
 
 (defun org-agenda-collect-markers ()
   "Collect the markers pointing to entries in the agenda buffer."
@@ -1900,8 +2845,8 @@ higher priority settings."
     (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)))
@@ -1920,7 +2865,7 @@ higher priority settings."
     (mapcar (lambda (x) (setcdr x (sort (copy-sequence (cdr x)) '<)) x)
            tbl)))
 
-(defvar org-agenda-marker-table nil) ; dyamically scoped parameter
+(defvar org-agenda-marker-table nil) ; dynamically scoped parameter
 (defun org-check-agenda-marker-table ()
   "Check of the current entry is on the marker list."
   (let ((file (buffer-file-name (or (buffer-base-buffer) (current-buffer))))
@@ -1934,34 +2879,54 @@ higher priority settings."
 (defun org-check-for-org-mode ()
   "Make sure current buffer is in org-mode.  Error if not."
   (or (org-mode-p)
-      (error "Cannot execute org-mode agenda command on buffer in %s."
+      (error "Cannot execute org-mode agenda command on buffer in %s"
             major-mode)))
 
 (defun org-fit-agenda-window ()
   "Fit the window to the buffer size."
   (and (memq org-agenda-window-setup '(reorganize-frame))
        (fboundp 'fit-window-to-buffer)
-       (fit-window-to-buffer
+       (org-fit-window-to-buffer
        nil
        (floor (* (frame-height) (cdr org-agenda-window-frame-fractions)))
        (floor (* (frame-height) (car org-agenda-window-frame-fractions))))))
 
 ;;; Agenda prepare and finalize
 
-(defvar org-agenda-multi nil)  ; dynammically scoped
+(defvar org-agenda-multi nil)  ; dynamically scoped
 (defvar org-agenda-buffer-name "*Org Agenda*")
 (defvar org-pre-agenda-window-conf nil)
 (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 preceded
+by \"+\" or \"-\".
+This variable should not be set directly, but agenda custom commands can
+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-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
        (setq buffer-read-only nil)
        (goto-char (point-max))
        (unless (or (bobp) org-agenda-compact-blocks)
-         (insert "\n" (make-string (window-width) ?=) "\n"))
+         (insert "\n"
+                 (if (stringp org-agenda-block-separator)
+                     org-agenda-block-separator
+                   (make-string (window-width) org-agenda-block-separator))
+                 "\n"))
        (narrow-to-region (point) (point-max)))
     (org-agenda-reset-markers)
     (setq org-agenda-contributing-files nil)
@@ -1971,6 +2936,7 @@ higher priority settings."
          (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
@@ -1985,7 +2951,11 @@ higher priority settings."
        (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)
@@ -2005,25 +2975,55 @@ higher priority settings."
        (org-agenda-align-tags)
        (unless org-agenda-with-colors
          (remove-text-properties (point-min) (point-max) '(face nil))))
-      (if (and (boundp 'org-overriding-columns-format)
-              org-overriding-columns-format)
-         (org-set-local 'org-overriding-columns-format
-                        org-overriding-columns-format))
+      (if (and (boundp 'org-agenda-overriding-columns-format)
+              org-agenda-overriding-columns-format)
+         (org-set-local 'org-agenda-overriding-columns-format
+                        org-agenda-overriding-columns-format))
       (if (and (boundp 'org-agenda-view-columns-initially)
               org-agenda-view-columns-initially)
          (org-agenda-columns))
       (when org-agenda-fontify-priorities
-       (org-fontify-priorities))
+       (org-agenda-fontify-priorities))
+      (when (and org-agenda-dim-blocked-tasks org-blocker-hook)
+       (org-agenda-dim-blocked-tasks))
+      (org-agenda-mark-clocking-task)
+      (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))
+      (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-fontify-priorities ()
+(defun org-agenda-mark-clocking-task ()
+  "Mark the current clock entry in the agenda if it is present."
+  (mapc (lambda (o)
+         (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 (org-get-at-bol 'org-hd-marker)
+                      org-clock-hd-marker)
+           (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)
@@ -2034,16 +3034,62 @@ higher priority settings."
              l (or (get-char-property (point) 'org-lowest-priority)
                    org-lowest-priority)
              p (string-to-char (match-string 1))
-             b (match-beginning 0) e (point-at-eol)
-             ov (org-make-overlay b e))
-       (org-overlay-put
+             b (match-beginning 0)
+             e (if (eq org-agenda-fontify-priorities 'cookies)
+                   (match-end 0)
+                 (point-at-eol))
+             ov (make-overlay b e))
+       (overlay-put
         ov 'face
-        (cond ((listp org-agenda-fontify-priorities)
-               (cdr (assoc p org-agenda-fontify-priorities)))
+        (cond ((org-face-from-face-or-color
+                'priority nil
+                (cdr (assoc p org-priority-faces))))
+              ((and (listp 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 (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)
+         (invis (eq org-agenda-dim-blocked-tasks 'invisible))
+         org-blocked-by-checkboxes
+         invis1 b e p ov h l)
+      (goto-char (point-min))
+      (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 (org-get-at-bol 'org-hd-marker)))
+         (when (and marker
+                    (not (with-current-buffer (marker-buffer marker)
+                           (save-excursion
+                             (goto-char marker)
+                             (if (org-entry-get nil "NOBLOCKING")
+                                 t ;; Never block this entry
+                               (run-hook-with-args-until-failure
+                                'org-blocker-hook
+                                (list :type 'todo-state-change
+                                      :position marker
+                                      :from 'todo
+                                      :to 'done)))))))
+           (if org-blocked-by-checkboxes (setq invis1 nil))
+           (setq b (if invis1
+                       (max (point-min) (1- (point-at-bol)))
+                     (point-at-bol))
+                 e (point-at-eol)
+                 ov (make-overlay b e))
+           (if invis1
+               (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.
@@ -2054,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.
@@ -2082,7 +3128,7 @@ continue from there."
 
 (defvar org-agenda-markers nil
   "List of all currently active markers created by `org-agenda'.")
-(defvar org-agenda-last-marker-time (time-to-seconds (current-time))
+(defvar org-agenda-last-marker-time (org-float-time)
   "Creation time of the last agenda marker.")
 
 (defun org-agenda-new-marker (&optional pos)
@@ -2090,7 +3136,7 @@ continue from there."
 Org-mode keeps a list of these markers and resets them when they are
 no longer in use."
   (let ((m (copy-marker (or pos (point)))))
-    (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
+    (setq org-agenda-last-marker-time (org-float-time))
     (push m org-agenda-markers)
     m))
 
@@ -2104,6 +3150,53 @@ no longer in use."
   (mapc (lambda (m) (org-check-and-save-marker m beg end))
        org-agenda-markers))
 
+;;; Entry text mode
+
+(defun org-agenda-entry-text-show-here ()
+  "Add some text from the entry as context to the current line."
+  (let (m txt o)
+    (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 "    > "))))
+    (when (string-match "\\S-" 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."
+  (interactive)
+  (save-excursion
+    (goto-char (point-max))
+    (beginning-of-line 1)
+    (while (not (bobp))
+      (when (org-get-at-bol 'org-hd-marker)
+       (org-agenda-entry-text-show-here))
+      (beginning-of-line 0))))
+
+(defun org-agenda-entry-text-hide ()
+  "Remove any shown entry context."
+  (delq nil
+       (mapcar (lambda (o)
+                 (if (eq (overlay-get o 'org-overlay-type)
+                         'agenda-entry-content)
+                     (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
 
 (defvar org-agenda-only-exact-dates nil) ; dynamically scoped
@@ -2116,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)))
@@ -2131,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))
@@ -2144,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
@@ -2165,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
@@ -2180,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"))
@@ -2207,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)
@@ -2240,16 +3334,50 @@ When EMPTY is non-nil, also include days without any entries."
 ;;; Agenda Daily/Weekly
 
 (defvar org-agenda-overriding-arguments nil) ; dynamically scoped parameter
-(defvar org-agenda-start-day nil) ; dynamically scoped parameter
+(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.
@@ -2260,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)))
@@ -2301,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
@@ -2331,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))
@@ -2341,19 +3464,22 @@ given in `org-agenda-start-on-weekday'."
             (w1 (org-days-to-iso-week d1))
             (w2 (org-days-to-iso-week d2)))
        (setq s (point))
-       (insert (capitalize (symbol-name (org-agenda-ndays-to-span nd)))
-               "-agenda"
-               (if (< (- d2 d1) 350)
-                   (if (= w1 w2)
-                       (format " (W%02d)" w1)
-                     (format " (W%02d-W%02d)" w1 w2))
-                 "")
-               ":\n"))
+       (if org-agenda-overriding-header
+           (insert (org-add-props (copy-sequence org-agenda-overriding-header)
+                       nil 'face 'org-agenda-structure) "\n")
+         (insert (org-agenda-span-name span)
+                 "-agenda"
+                 (if (< (- d2 d1) 350)
+                     (if (= w1 w2)
+                         (format " (W%02d)" w1)
+                       (format " (W%02d-W%02d)" w1 w2))
+                   "")
+                 ":\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)))
@@ -2365,16 +3491,25 @@ given in `org-agenda-start-on-weekday'."
       (while (setq file (pop files))
        (catch 'nextfile
          (org-check-agenda-file file)
-         (if org-agenda-show-log
+         (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
-                        :deadline :scheduled :timestamp :sexp :closed))
-           (setq rtn (org-agenda-get-day-entries
-                      file date
-                      :deadline :scheduled :sexp :timestamp)))
+                        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
-         (progn
+         (let ((org-agenda-search-headline-for-time t))
            (require 'diary-lib)
            (setq rtn (org-get-entries-from-diary date))
            (setq rtnall (append rtnall rtn))))
@@ -2388,16 +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)
-           (if todayp (put-text-property s (1- (point)) 'org-today t))
+           (when todayp
+             (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))))
@@ -2410,10 +3545,19 @@ 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))
-    (org-fit-agenda-window)
+    (or org-agenda-multi (org-fit-agenda-window))
     (unless (and (pos-visible-in-window-p (point-min))
                 (pos-visible-in-window-p (point-max)))
       (goto-char (1- (point-max)))
@@ -2429,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
 
@@ -2439,7 +3607,7 @@ given in `org-agenda-start-on-weekday'."
 (defvar org-search-syntax-table nil
   "Special syntax table for org-mode search.
 In this table, we have single quotes not as word constituents, to
-that when \"+Ameli\" is searchd as a work, it will also match \"Ameli's\"")
+that when \"+Ameli\" is searched as a work, it will also match \"Ameli's\"")
 
 (defun org-search-syntax-table ()
   (unless org-search-syntax-table
@@ -2448,11 +3616,11 @@ that when \"+Ameli\" is searchd 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
@@ -2460,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'."
@@ -2481,24 +3667,28 @@ in `org-agenda-text-search-extra-files'."
   (org-set-sorting-strategy 'search)
   (org-prepare-agenda "SEARCH")
   (let* ((props (list 'face nil
-                     'done-face 'org-done
+                     'done-face 'org-agenda-done
                      'org-not-done-regexp org-not-done-regexp
                      '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 priority 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
@@ -2512,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))
@@ -2620,10 +3850,11 @@ 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))
-    (org-fit-agenda-window)
+    (or org-agenda-multi (org-fit-agenda-window))
     (add-text-properties (point-min) (point-max) '(org-agenda-type search))
     (org-finalize-agenda)
     (setq buffer-read-only t)))
@@ -2635,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)
@@ -2656,7 +3887,7 @@ for a keyword.  A numeric prefix directly selects the Nth keyword in
         rtn rtnall files file pos)
     (when (equal arg '(4))
       (setq org-select-this-todo-keyword
-           (completing-read "Keyword (or KWD1|K2D2|...): "
+           (org-icompleting-read "Keyword (or KWD1|K2D2|...): "
                             (mapcar 'list kwds) nil nil)))
     (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
     (org-set-local 'org-last-arg arg)
@@ -2674,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))
@@ -2690,10 +3925,11 @@ 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))
-    (org-fit-agenda-window)
+    (or org-agenda-multi (org-fit-agenda-window))
     (add-text-properties (point-min) (point-max) '(org-agenda-type todo))
     (org-finalize-agenda)
     (setq buffer-read-only t)))
@@ -2708,10 +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))
@@ -2728,7 +3966,7 @@ The prefix arg TODO-ONLY limits the search to TODO entries."
                         (org-get-agenda-file-buffer file)
                       (error "No such file %s" file)))
        (if (not buffer)
-           ;; If file does not exist, merror message to agenda
+           ;; If file does not exist, error message to agenda
            (setq rtn (list
                       (format "ORG-AGENDA-ERROR: No such org-file %s" file))
                  rtnall (append rtnall rtn))
@@ -2748,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))
@@ -2756,10 +3996,11 @@ 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))
-    (org-fit-agenda-window)
+    (or org-agenda-multi (org-fit-agenda-window))
     (add-text-properties (point-min) (point-max) '(org-agenda-type tags))
     (org-finalize-agenda)
     (setq buffer-read-only t)))
@@ -2772,10 +4013,24 @@ This is basically a temporary global variable that can be set and then
 used by user-defined selections using `org-agenda-skip-function'.")
 
 (defvar org-agenda-overriding-header nil
-  "When this is set during todo and tags searches, will replace header.")
+  "When this is set during todo and tags searches, will replace header.
+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 ()
+  "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
+of a command."
+  (let ((end (save-excursion (org-end-of-subtree t)))
+       skip)
+    (save-excursion
+      (setq skip (re-search-forward org-agenda-skip-regexp end t)))
+    (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."
@@ -2785,6 +4040,20 @@ to skip this subtree.  This is a function that can be put into
       (setq skip (re-search-forward org-agenda-skip-regexp end t)))
     (and skip end)))
 
+(defun org-agenda-skip-entry-when-regexp-matches-in-subtree ()
+  "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
+`org-agenda-skip-function' for the duration of a command.  An important
+use of this function is for the stuck project list."
+  (let ((end (save-excursion (org-end-of-subtree t)))
+       (entry-end (save-excursion (outline-next-heading) (1- (point))))
+       skip)
+    (save-excursion
+      (setq skip (re-search-forward org-agenda-skip-regexp end t)))
+    (and skip entry-end)))
+
 (defun org-agenda-skip-entry-if (&rest conditions)
   "Skip entry if any of CONDITIONS is true.
 See `org-agenda-skip-if' for details."
@@ -2807,12 +4076,30 @@ scheduled     Check if there is a scheduled cookie
 notscheduled  Check if there is no scheduled cookie
 deadline      Check if there is a deadline
 notdeadline   Check if there is no deadline
+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."
@@ -2833,25 +4120,66 @@ that can be put into `org-agenda-skip-function' for the duration of a command."
           (re-search-forward org-deadline-time-regexp end t))
       (and (memq 'notdeadline conditions)
           (not (re-search-forward org-deadline-time-regexp end t)))
+      (and (memq 'timestamp conditions)
+          (re-search-forward org-ts-regexp end t))
+      (and (memq 'nottimestamp conditions)
+          (not (re-search-forward org-ts-regexp end t)))
       (and (setq m (memq 'regexp conditions))
           (stringp (nth 1 m))
           (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-subtree-when-regexp-matches)
+  (let* ((org-agenda-skip-function
+         'org-agenda-skip-entry-when-regexp-matches-in-subtree)
         ;; We could have used org-agenda-skip-if here.
-        (org-agenda-overriding-header "List of stuck projects: ")
+        (org-agenda-overriding-header
+         (or org-agenda-overriding-header "List of stuck projects: "))
         (matcher (nth 0 org-stuck-projects))
         (todo (nth 1 org-stuck-projects))
         (todo-wds (if (member "*" todo)
@@ -2867,10 +4195,11 @@ MATCH is being ignored."
                          "\\)\\>"))
         (tags (nth 2 org-stuck-projects))
         (tags-re (if (member "*" tags)
-                     (org-re "^\\*+ .*:[[:alnum:]_@]+:[ \t]*$")
-                   (concat "^\\*+ .*:\\("
-                           (mapconcat 'identity tags "\\|")
-                           (org-re "\\):[[:alnum:]_@:]*[ \t]*$"))))
+                     (org-re "^\\*+ .*:[[:alnum:]_@#%]+:[ \t]*$")
+                   (if tags
+                       (concat "^\\*+ .*:\\("
+                               (mapconcat 'identity tags "\\|")
+                               (org-re "\\):[[:alnum:]_@#%:]*[ \t]*$")))))
         (gen-re (nth 3 org-stuck-projects))
         (re-list
          (delq nil
@@ -2898,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)
@@ -2936,9 +4264,12 @@ 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
+  "Hook run when the fancy diary buffer is cleaned up.")
+
 (defun org-agenda-cleanup-fancy-diary ()
   "Remove unwanted stuff in buffer created by `fancy-diary-display'.
 This gets rid of the date, the underline under the date, and
@@ -2958,14 +4289,15 @@ date.  It also removes lines that contain only whitespace."
     (replace-match ""))
   (goto-char (point-min))
   (if (re-search-forward "^Org-mode dummy\n?" nil t)
-      (replace-match "")))
+      (replace-match ""))
+  (run-hooks 'org-agenda-cleanup-fancy-diary-hook))
 
 ;; Make sure entries from the diary have the right text properties.
 (eval-after-load "diary-lib"
   '(if (boundp 'diary-modify-entry-list-string-function)
        ;; We can rely on the hook, nothing to do
        nil
-     ;; Hook not avaiable, must use advice to make this work
+     ;; Hook not available, must use advice to make this work
      (defadvice add-to-diary-list (before org-mark-diary-entry activate)
        "Make the position visible."
        (if (and org-disable-agenda-to-diary  ;; called from org-agenda
@@ -2977,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))
@@ -3000,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:
 
@@ -3046,7 +4360,7 @@ So the example above may also be written as
 The function expects the lisp variables `entry' and `date' to be provided
 by the caller, because this is how the calendar works.  Don't use this
 function from a program - use `org-agenda-get-day-entries' instead."
-  (when (> (- (time-to-seconds (current-time))
+  (when (> (- (org-float-time)
              org-agenda-last-marker-time)
           5)
     (org-agenda-reset-markers))
@@ -3056,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))
@@ -3081,7 +4401,7 @@ the documentation of `org-diary'."
         (buffer (if (file-exists-p file)
                     (org-get-agenda-file-buffer file)
                   (error "No such file %s" file)))
-        arg results rtn)
+        arg results rtn deadline-results)
     (if (not buffer)
        ;; If file does not exist, make sure an error message ends up in diary
        (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
@@ -3111,25 +4431,25 @@ the documentation of `org-diary'."
                  (setq rtn (org-agenda-get-sexps))
                  (setq results (append results rtn)))
                 ((eq arg :scheduled)
-                 (setq rtn (org-agenda-get-scheduled))
+                 (setq rtn (org-agenda-get-scheduled deadline-results))
                  (setq results (append results rtn)))
                 ((eq arg :closed)
-                 (setq rtn (org-agenda-get-closed))
+                 (setq rtn (org-agenda-get-progress))
                  (setq results (append results rtn)))
                 ((eq arg :deadline)
                  (setq rtn (org-agenda-get-deadlines))
+                 (setq deadline-results (copy-sequence rtn))
                  (setq results (append results rtn))))))))
        results))))
 
 (defun org-agenda-get-todos ()
   "Return the TODO information for agenda display."
   (let* ((props (list 'face nil
-                     'done-face 'org-done
+                     'done-face 'org-agenda-done
                      'org-not-done-regexp org-not-done-regexp
                      '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))))
@@ -3149,14 +4469,8 @@ the documentation of `org-diary'."
       (catch :skip
        (save-match-data
          (beginning-of-line)
-         (setq beg (point) end (progn (outline-next-heading) (point)))
-         (when (or (and org-agenda-todo-ignore-with-date (goto-char beg)
-                        (re-search-forward org-ts-regexp end t))
-                   (and org-agenda-todo-ignore-scheduled (goto-char beg)
-                        (re-search-forward org-scheduled-time-regexp end t))
-                   (and org-agenda-todo-ignore-deadlines (goto-char beg)
-                        (re-search-forward org-deadline-time-regexp end t)
-                        (org-deadline-close (match-string 1))))
+         (setq beg (point) end (save-excursion (outline-next-heading) (point)))
+         (when (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item end)
            (goto-char (1+ beg))
            (or org-agenda-todo-list-sublevels (org-end-of-subtree 'invisible))
            (throw :skip nil)))
@@ -3165,8 +4479,9 @@ the documentation of `org-diary'."
        (goto-char (match-beginning 1))
        (setq marker (org-agenda-new-marker (match-beginning 0))
              category (org-get-category)
+             txt (match-string 1)
              tags (org-get-tags-at (point))
-             txt (org-format-agenda-item "" (match-string 1) category tags)
+             txt (org-format-agenda-item "" txt category tags)
              priority (1+ (org-get-priority txt))
              todo-state (org-get-todo-state))
        (org-add-props txt props
@@ -3179,6 +4494,60 @@ the documentation of `org-diary'."
          (org-end-of-subtree 'invisible))))
     (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?"
+  (when (or org-agenda-todo-ignore-with-date
+           org-agenda-todo-ignore-scheduled
+           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)
+              (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)
+              (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.")
 
@@ -3189,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))))
@@ -3215,9 +4583,9 @@ the documentation of `org-diary'."
           "\\|\\(<%%\\(([^>\n]+)\\)>\\)"))
         marker hdmarker deadlinep scheduledp clockp closedp inactivep
         donep tmp priority category ee txt timestr tags b0 b3 e3 head
-        todo-state)
+        todo-state end-of-match)
     (goto-char (point-min))
-    (while (re-search-forward regexp nil t)
+    (while (setq end-of-match (re-search-forward regexp nil t))
       (setq b0 (match-beginning 0)
            b3 (match-beginning 3) e3 (match-end 3))
       (catch :skip
@@ -3231,9 +4599,7 @@ the documentation of `org-diary'."
        (if (and e3
                 (not (org-diary-sexp-entry (buffer-substring b3 e3) "" date)))
            (throw :skip nil))
-       (setq marker (org-agenda-new-marker b0)
-             category (org-get-category b0)
-             tmp (buffer-substring (max (point-min)
+       (setq tmp (buffer-substring (max (point-min)
                                         (- b0 org-ds-keyword-length))
                                    b0)
              timestr (if b3 "" (buffer-substring b0 (point-at-eol)))
@@ -3245,27 +4611,28 @@ 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)
+       (if (or scheduledp deadlinep closedp clockp
+               (and donep org-agenda-skip-timestamp-if-done))
            (throw :skip t))
        (if (string-match ">" timestr)
            ;; substring should only run to end of time stamp
            (setq timestr (substring timestr 0 (match-end 0))))
+       (setq marker (org-agenda-new-marker b0)
+             category (org-get-category b0))
        (save-excursion
-         (if (re-search-backward "^\\*+ " nil t)
-             (progn
-               (goto-char (match-beginning 0))
-               (setq hdmarker (org-agenda-new-marker)
-                     tags (org-get-tags-at))
-               (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
-               (setq head (match-string 1))
-               (and org-agenda-skip-timestamp-if-done donep (throw :skip t))
-               (setq txt (org-format-agenda-item
-                          (if inactivep "[" nil)
-                          head category tags timestr nil
-                          remove-re)))
-           (setq txt org-agenda-no-heading-message))
+         (if (not (re-search-backward "^\\*+ " nil t))
+             (setq txt org-agenda-no-heading-message)
+           (goto-char (match-beginning 0))
+           (setq hdmarker (org-agenda-new-marker)
+                 tags (org-get-tags-at))
+           (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
+           (setq head (match-string 1))
+           (setq txt (org-format-agenda-item
+                      (if inactivep org-agenda-inactive-leader nil)
+                      head category tags timestr nil
+                      remove-re)))
          (setq priority (org-get-priority txt))
          (org-add-props txt props
            'org-marker marker 'org-hd-marker hdmarker)
@@ -3274,7 +4641,9 @@ the documentation of `org-diary'."
                         'todo-state todo-state
                         'type "timestamp")
          (push txt ee))
-       (outline-next-heading)))
+       (if org-agenda-skip-additional-timestamps-same-entry
+           (outline-next-heading)
+         (goto-char end-of-match))))
     (nreverse ee)))
 
 (defun org-agenda-get-sexps ()
@@ -3282,12 +4651,12 @@ 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))))
         (regexp "^&?%%(")
-        marker category ee txt tags entry result beg b sexp sexp-entry)
+        marker category ee txt tags entry result beg b sexp sexp-entry
+        todo-state)
     (goto-char (point-min))
     (while (re-search-forward regexp nil t)
       (catch :skip
@@ -3303,33 +4672,73 @@ the documentation of `org-diary'."
        (setq result (org-diary-sexp-entry sexp sexp-entry date))
        (when result
          (setq marker (org-agenda-new-marker beg)
-               category (org-get-category beg))
-
-         (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
-           'type "sexp")
-         (push txt ee))))
+               category (org-get-category beg)
+               todo-state (org-get-todo-state))
+
+         (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-agenda-get-closed ()
+(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."
   (let* ((props (list 'mouse-face 'highlight
                      '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))))
+        (items (if (consp org-agenda-show-log)
+                   org-agenda-show-log
+                 org-agenda-log-mode-items))
+        (parts
+         (delq nil
+               (list
+                (if (memq 'closed items) (concat "\\<" org-closed-string))
+                (if (memq 'clock items) (concat "\\<" org-clock-string))
+                (if (memq 'state items) "- State \"\\([a-zA-Z0-9]+\\)\".*?"))))
+        (parts-re (if parts (mapconcat 'identity parts "\\|")
+                    (error "`org-agenda-log-mode-items' is empty")))
         (regexp (concat
-                 "\\<\\(" org-closed-string "\\|" org-clock-string "\\) *\\["
+                 "\\(" parts-re "\\)"
+                 " *\\["
                  (regexp-quote
                   (substring
                    (format-time-string
@@ -3337,46 +4746,65 @@ the documentation of `org-diary'."
                     (apply 'encode-time  ; DATE bound by calendar
                            (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
                    1 11))))
-        marker hdmarker priority category tags closedp
-        ee txt timestr rest clocked)
+        (org-agenda-search-headline-for-time nil)
+        marker hdmarker priority category tags closedp statep clockp state
+        ee txt extra timestr rest clocked)
     (goto-char (point-min))
     (while (re-search-forward regexp nil t)
       (catch :skip
        (org-agenda-skip)
        (setq marker (org-agenda-new-marker (match-beginning 0))
              closedp (equal (match-string 1) org-closed-string)
+             statep (equal (string-to-char (match-string 1)) ?-)
+             clockp (not (or closedp statep))
+             state (and statep (match-string 2))
              category (org-get-category (match-beginning 0))
              timestr (buffer-substring (match-beginning 0) (point-at-eol))
-             ;; donep (org-entry-is-done-p)
              )
        (when (string-match "\\]" timestr)
          ;; substring should only run to end of time stamp
          (setq rest (substring timestr (match-end 0))
                timestr (substring timestr 0 (match-end 0)))
-         (if (and (not closedp)
-                  (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
+         (if (and (not closedp) (not statep)
+                  (string-match "\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)\\].*?\\([0-9]\\{1,2\\}:[0-9]\\{2\\}\\)" rest))
              (progn (setq timestr (concat (substring timestr 0 -1)
                                           "-" (match-string 1 rest) "]"))
                     (setq clocked (match-string 2 rest)))
-           (setq clocked "-")))
-       (save-excursion
-         (if (re-search-backward "^\\*+ " nil t)
-             (progn
-               (goto-char (match-beginning 0))
-               (setq hdmarker (org-agenda-new-marker)
-                     tags (org-get-tags-at))
-               (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
-               (setq txt (org-format-agenda-item
-                          (if closedp "Closed:    "
-                            (concat "Clocked:   (" clocked  ")"))
-                          (match-string 1) category tags timestr)))
-           (setq txt org-agenda-no-heading-message))
+           (setq clocked "-")))
+       (save-excursion
+         (setq extra nil)
+         (cond
+          ((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)))))
+         (if (not (re-search-backward "^\\*+ " nil t))
+             (setq txt org-agenda-no-heading-message)
+           (goto-char (match-beginning 0))
+           (setq hdmarker (org-agenda-new-marker)
+                 tags (org-get-tags-at))
+           (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
+           (setq txt (match-string 1))
+           (when extra
+             (if (string-match "\\([ \t]+\\)\\(:[^ \n\t]*?:\\)[ \t]*$" txt)
+                 (setq txt (concat (substring txt 0 (match-beginning 1))
+                                   " - " extra " " (match-string 2 txt)))
+               (setq txt (concat txt " - " extra))))
+           (setq txt (org-format-agenda-item
+                      (cond
+                       (closedp "Closed:    ")
+                           (statep (concat "State:     (" state ")"))
+                           (t (concat "Clocked:   (" clocked  ")")))
+                      txt category tags timestr)))
          (setq priority 100000)
          (org-add-props txt props
-           'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
+           'org-marker marker 'org-hd-marker hdmarker 'face 'org-agenda-done
            'priority priority 'org-category category
            'type "closed" 'date date
-           'undone-face 'org-warning 'done-face 'org-done)
+           'undone-face 'org-warning 'done-face 'org-agenda-done)
          (push txt ee))
        (goto-char (point-at-eol))))
     (nreverse ee)))
@@ -3387,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))))
@@ -3395,57 +4822,74 @@ 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))
              d2 (org-time-string-to-absolute
                  (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 category (org-get-category))
              (setq todo-state (org-get-todo-state))
-             (if (re-search-backward "^\\*+[ \t]+" nil t)
-                 (progn
-                   (goto-char (match-end 0))
-                   (setq pos1 (match-beginning 0))
-                   (setq tags (org-get-tags-at pos1))
-                   (setq head (buffer-substring-no-properties
-                               (point)
-                               (progn (skip-chars-forward "^\r\n")
-                                      (point))))
-                   (setq donep (member todo-state org-done-keywords))
-                   (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
-                       (setq timestr
-                             (concat (substring s (match-beginning 1)) " "))
-                     (setq timestr 'time))
-                   (if (and donep
-                            (or org-agenda-skip-deadline-if-done
-                                (not (= diff 0))))
-                       (setq txt nil)
-                     (setq txt (org-format-agenda-item
-                                (if (= diff 0)
-                                    (car org-agenda-deadline-leaders)
-                                  (if (functionp (nth 1 org-agenda-deadline-leaders))
-                                      (funcall (nth 1 org-agenda-deadline-leaders) diff date)
-                                    (format (nth 1 org-agenda-deadline-leaders)
-                                            diff)))
-                                head category tags
-                                (if (not (= diff 0)) nil timestr)))))
-               (setq txt org-agenda-no-heading-message))
+             (setq donep (member todo-state org-done-keywords))
+             (if (and donep
+                      (or org-agenda-skip-deadline-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))
+                 (setq tags (org-get-tags-at pos1))
+                 (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-deadline-leaders)
+                              (if (functionp
+                                   (nth 1 org-agenda-deadline-leaders))
+                                  (funcall
+                                   (nth 1 org-agenda-deadline-leaders)
+                                   diff date)
+                                (format (nth 1 org-agenda-deadline-leaders)
+                                        diff)))
+                            head category tags
+                            (if (not (= diff 0)) nil timestr)))))
              (when txt
                (setq face (org-agenda-deadline-face dfrac wdays))
                (org-add-props txt props
@@ -3457,8 +4901,8 @@ the documentation of `org-diary'."
                  'todo-state todo-state
                  'type (if upcomingp "upcoming-deadline" "deadline")
                  'date (if upcomingp date d2)
-                 'face (if donep 'org-done face)
-                 'undone-face face 'done-face 'org-done)
+                 'face (if donep 'org-agenda-done face)
+                 'undone-face face 'done-face 'org-agenda-done)
                (push txt ee))))))
     (nreverse ee)))
 
@@ -3471,27 +4915,33 @@ FRACTION is what fraction of the head-warning time has passed."
       (while (setq f (pop faces))
        (if (>= fraction (car f)) (throw 'exit (cdr f)))))))
 
-(defun org-agenda-get-scheduled ()
+(defun org-agenda-get-scheduled (&optional deadline-results)
   "Return the scheduled information for agenda display."
   (let* ((props (list 'org-not-done-regexp org-not-done-regexp
                      'org-todo-regexp org-todo-regexp
                      'org-complex-heading-regexp org-complex-heading-regexp
-                     'done-face 'org-done
+                     '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))))
         (regexp org-scheduled-time-regexp)
         (todayp (org-agenda-todayp date)) ; DATE bound by calendar
         (d1 (calendar-absolute-from-gregorian date))  ; DATE bound by calendar
+        mm
+        (deadline-position-alist
+         (mapcar (lambda (a) (and (setq mm (get-text-property
+                                       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
        (org-agenda-skip)
        (setq s (match-string 1)
+             txt nil
              pos (1- (match-beginning 1))
              d2 (org-time-string-to-absolute
                  (match-string 1) d1 'past
@@ -3500,55 +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
+       (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))
-             (setq todo-state (org-get-todo-state))
-             (if (re-search-backward "^\\*+[ \t]+" nil t)
-                 (progn
-                   (goto-char (match-end 0))
-                   (setq pos1 (match-beginning 0))
-                   (setq tags (org-get-tags-at))
-                   (setq head (buffer-substring-no-properties
-                               (point)
-                               (progn (skip-chars-forward "^\r\n") (point))))
-                   (setq donep (member todo-state org-done-keywords))
-                   (if (string-match " \\([012]?[0-9]:[0-9][0-9]\\)" s)
-                       (setq timestr
-                             (concat (substring s (match-beginning 1)) " "))
-                     (setq timestr 'time))
-                   (if (and donep
-                            (or org-agenda-skip-scheduled-if-done
-                                (not (= diff 0))))
-                       (setq txt nil)
-                     (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)))))
-               (setq txt org-agenda-no-heading-message))
-             (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-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))))))
+             (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)
+                          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 ()
@@ -3558,14 +5026,13 @@ 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))))
         (regexp org-tr-regexp)
         (d0 (calendar-absolute-from-gregorian date))
         marker hdmarker ee txt d1 d2 s1 s2 timestr category todo-state tags pos
-        donep head)
+        head donep)
     (goto-char (point-min))
     (while (re-search-forward regexp nil t)
       (catch :skip
@@ -3580,25 +5047,33 @@ FRACTION is what fraction of the head-warning time has passed."
            ;; Only allow days between the limits, because the normal
            ;; date stamps will catch the limits.
            (save-excursion
+             (setq todo-state (org-get-todo-state))
+             (setq donep (member todo-state org-done-keywords))
+             (if (and donep org-agenda-skip-timestamp-if-done)
+                 (throw :skip t))
              (setq marker (org-agenda-new-marker (point)))
              (setq category (org-get-category))
-             (setq todo-state (org-get-todo-state))
-             (if (re-search-backward "^\\*+ " nil t)
-                 (progn
-                   (goto-char (match-beginning 0))
-                   (setq hdmarker (org-agenda-new-marker (point)))
-                   (setq tags (org-get-tags-at))
-                   (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
-                   (setq head (match-string 1))
-                   (and org-agenda-skip-timestamp-if-done
-                        (org-entry-is-done-p)
-                        (throw :skip t))
-                   (setq txt (org-format-agenda-item
-                              (format (if (= d1 d2) "" "(%d/%d): ")
-                                      (1+ (- d0 d1)) (1+ (- d2 d1)))
-                              head category tags
-                              (if (= d0 d1) timestr))))
-               (setq txt org-agenda-no-heading-message))
+             (if (not (re-search-backward "^\\*+ " nil t))
+                 (setq txt org-agenda-no-heading-message)
+               (goto-char (match-beginning 0))
+               (setq hdmarker (org-agenda-new-marker (point)))
+               (setq tags (org-get-tags-at))
+               (looking-at "\\*+[ \t]+\\([^\r\n]+\\)")
+               (setq head (match-string 1))
+               (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
@@ -3620,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.
@@ -3638,19 +5125,33 @@ 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)))
+
+    ;; 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
-          (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
+          (ts (if dotime (concat
+                          (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)
@@ -3697,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)
@@ -3717,7 +5218,7 @@ Any match of REMOVE-RE will be removed from TXT."
                (error nil)))
        (when effort
          (setq neffort (org-hh:mm-string-to-minutes effort)
-               effort (setq effort (concat "[" effort"]" )))))
+               effort (setq effort (concat "[" effort "]" )))))
 
       (when remove-re
        (while (string-match remove-re txt)
@@ -3727,17 +5228,40 @@ 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) 'tags tags
+       '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
        'prefix-length (- (length rtn) (length txt))
@@ -3750,6 +5274,44 @@ Any match of REMOVE-RE will be removed from TXT."
        'extra extra
        'dotime dotime))))
 
+(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)))
+    (setq s (downcase s))
+    (add-text-properties 0 (length s) props s)
+    s))
+
 (defvar org-agenda-sorting-strategy) ;; because the def is in a let form
 (defvar org-agenda-sorting-strategy-selected nil)
 
@@ -3773,13 +5335,13 @@ Any match of REMOVE-RE will be removed from TXT."
          (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)))))
@@ -3789,7 +5351,7 @@ Any match of REMOVE-RE will be removed from TXT."
 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)
@@ -3798,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)))
@@ -3810,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))
@@ -3851,28 +5421,53 @@ 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 0)
+           (add-text-properties (match-beginning 0) (match-end 1)
                                 (list 'face (org-get-todo-face 1)))
            (let ((s (buffer-substring (match-beginning 1) (match-end 1))))
              (delete-region (match-beginning 1) (1- (match-end 0)))
@@ -3887,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)
@@ -3921,11 +5517,20 @@ HH:MM."
 
 (defsubst org-cmp-todo-state (a b)
   "Compare the todo states of strings A and B."
-  (let* ((ta (or (get-text-property 1 'todo-state a) ""))
+  (let* ((ma (or (get-text-property 1 'org-marker a)
+                (get-text-property 1 'org-hd-marker a)))
+        (mb (or (get-text-property 1 'org-marker b)
+                (get-text-property 1 'org-hd-marker b)))
+        (fa (and ma (marker-buffer ma)))
+        (fb (and mb (marker-buffer mb)))
+        (todo-kwds
+         (or (and fa (with-current-buffer fa org-todo-keywords-1))
+             (and fb (with-current-buffer fb org-todo-keywords-1))))
+        (ta (or (get-text-property 1 'todo-state a) ""))
         (tb (or (get-text-property 1 'todo-state b) ""))
-        (la (- (length (member ta org-todo-keywords-for-agenda))))
-        (lb (- (length (member tb org-todo-keywords-for-agenda))))
-        (donepa (member ta org-done-keywords-for-agenda)) 
+        (la (- (length (member ta todo-kwds))))
+        (lb (- (length (member tb todo-kwds))))
+        (donepa (member ta org-done-keywords-for-agenda))
         (donepb (member tb org-done-keywords-for-agenda)))
     (cond ((and donepa (not donepb)) -1)
          ((and (not donepa) donepb) +1)
@@ -3933,8 +5538,30 @@ 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 categories of strings A and B."
+  "Compare the string values of the first tags of A and B."
   (let ((ta (car (last (get-text-property 1 'tags a))))
        (tb (car (last (get-text-property 1 'tags b)))))
     (cond ((not ta) +1)
@@ -3952,35 +5579,66 @@ 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))
-        (todo-state-down (if todo-state-up (- todo-state-up) nil)))
+  (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 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)
+             user-defined-down (if user-defined-up (- user-defined-up) nil)))
     (cdr (assoc
          (eval (cons 'or org-agenda-sorting-strategy-selected))
          '((-1 . t) (1 . nil) (nil . nil))))))
 
 ;;; 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)
@@ -4003,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)))
@@ -4062,11 +5720,20 @@ If ERROR is non-nil, throw an error, otherwise just return nil."
   (if org-agenda-columns-active
       (org-columns-quit)
     (let ((buf (current-buffer)))
-      (if (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))
@@ -4083,127 +5750,264 @@ 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)))
 
-(defun org-save-all-org-buffers ()
-  "Save all Org-mode buffers without user confirmation."
-  (interactive)
-  (message "Saving all Org-mode buffers...")
-  (save-some-buffers t 'org-mode-p)
-  (message "Saving all Org-mode buffers... done"))
-
 (defun org-agenda-redo ()
   "Rebuild Agenda.
 When this is the global TODO list, a prefix argument will be interpreted."
   (interactive)
   (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))))
         (lprops (get 'org-agenda-redo-command 'org-lprops)))
+    (put 'org-agenda-filter :preset-filter nil)
     (and cols (org-columns-quit))
     (message "Rebuilding agenda buffer...")
     (org-let lprops '(eval org-agenda-redo-command))
     (setq org-agenda-undo-list nil
          org-agenda-pending-undo-list nil)
     (message "Rebuilding agenda buffer...done")
+    (put 'org-agenda-filter :preset-filter preset)
+    (and (or filter preset) (org-agenda-filter-apply filter))
     (and cols (interactive-p) (org-agenda-columns))
-    (goto-line line)
+    (org-goto-line line)
     (recenter window-line)))
 
+
 (defvar org-global-tags-completion-table nil)
-(defun org-agenda-filter-by-tag (strip &optional char)
+(defvar org-agenda-filter-form nil)
+(defun org-agenda-filter-by-tag (strip &optional char narrow)
   "Keep only those lines in the agenda buffer that have a specific tag.
 The tag is selected with its fast selection letter, as configured.
-With prefix argument STRIP, remove all lines that do have the tag."
+With prefix argument STRIP, remove all lines that do have the tag.
+A lisp caller can specify CHAR.  NARROW means that the new tag should be
+used to narrow the search - the interactive user can also press `-' or `+'
+to switch to narrowing."
   (interactive "P")
-  (let (char a tag tags (inhibit-read-only t))
-      (message "Select tag [%s] or no tag [ ], [TAB] to complete, [/] to restore: "
-              (mapconcat
-               (lambda (x) (if (cdr x) (char-to-string (cdr x)) ""))
-               org-tag-alist-for-agenda ""))
-      (setq char (read-char))
-      (when (equal char ?\t)
-       (unless (local-variable-p 'org-global-tags-completion-table)
-         (org-set-local 'org-global-tags-completion-table
-                        (org-global-tags-completion-table)))
-       (let ((completion-ignore-case t))
-         (setq tag (completing-read
-                    "Tag: " org-global-tags-completion-table))))
-      (cond
-       ((equal char ?/) (org-agenda-filter-by-tag-show-all))
-       ((or (equal char ?\ )
-           (setq a (rassoc char org-tag-alist-for-agenda))
-           (and tag (setq a (cons tag nil))))
-       (org-agenda-filter-by-tag-show-all)
-       (setq tag (car a))
-       (save-excursion
-         (goto-char (point-min))
-         (while (not (eobp))
-           (if (get-text-property (point) 'org-marker)
-               (progn
-                 (setq tags (get-text-property (point) 'tags))
-                 (if (not tag)
-                     (if (or (and strip (not tags))
-                             (and (not strip) tags))
-                         (org-agenda-filter-by-tag-hide-line))
-                   (if (or (and (member tag tags) strip)
-                           (and (not (member tag tags)) (not strip)))
-                       (org-agenda-filter-by-tag-hide-line)))
-                 (beginning-of-line 2))
-             (beginning-of-line 2)))))
-       (t (error "Invalid tag selection character %c" char)))))
-
-(defvar org-agenda-filter-overlays nil)
+  (let* ((alist org-tag-alist-for-agenda)
+        (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], %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
+      (cond ((equal char ?-) (setq strip t narrow t))
+           ((equal char ?+) (setq strip nil narrow t)))
+      (message
+       "Narrow by tag [%s ], [TAB], [/]:off, [>=<]:effort: " tag-chars)
+      (setq char (read-char)))
+    (when (member char '(?< ?> ?= ??))
+      ;; An effort operator
+      (setq effort-op (char-to-string char))
+      (setq alist nil) ; to make sure it will be interpreted as effort.
+      (unless (equal char ??)
+       (loop for i from 0 to 9 do
+             (setq effort-prompt
+                   (concat
+                    effort-prompt " ["
+                    (if (= i 9) "0" (int-to-string (1+ i)))
+                    "]" (nth i efforts))))
+       (message "Effort%s: %s " effort-op effort-prompt)
+       (setq char (read-char))
+       (when (or (< char ?0) (> char ?9))
+         (error "Need 1-9,0 to select effort" ))))
+    (when (equal char ?\t)
+      (unless (local-variable-p 'org-global-tags-completion-table (current-buffer))
+       (org-set-local 'org-global-tags-completion-table
+                      (org-global-tags-completion-table)))
+      (let ((completion-ignore-case t))
+       (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)
+       (org-agenda-filter-apply org-agenda-filter)))
+     ((or (equal char ?\ )
+         (setq a (rassoc char alist))
+         (and (>= char ?0) (<= char ?9)
+              (setq n (if (= char ?0) 9 (- char ?0 1))
+                    tag (concat effort-op (nth n efforts))
+                    a (cons tag nil)))
+         (and (= char ??)
+              (setq tag "?eff")
+              a (cons tag nil))
+         (and tag (setq a (cons tag nil))))
+      (org-agenda-filter-by-tag-show-all)
+      (setq tag (car a))
+      (setq org-agenda-filter
+           (cons (concat (if strip "-" "+") tag)
+                 (if narrow current nil)))
+      (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")
+  (org-agenda-filter-by-tag strip char 'refine))
+
+(defun org-agenda-filter-make-matcher ()
+  "Create the form that tests a line for the agenda filter."
+  (let (f f1)
+    (dolist (x (append (get 'org-agenda-filter :preset-filter)
+                      org-agenda-filter))
+      (if (member x '("-" "+"))
+         (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)))
+       (if (equal (string-to-char x) ?-)
+           (setq f1 (list 'not f1))))
+      (push f1 f))
+    (cons 'and (nreverse f))))
+
+(defun org-agenda-filter-effort-form (e)
+  "Return the form to compare the effort of the current line with what E says.
+E looks like \"+<2:25\"."
+  (let (op)
+    (setq e (substring e 1))
+    (setq op (string-to-char e) e (substring e 1))
+    (setq op (cond ((equal op ?<) '<=)
+                  ((equal op ?>) '>=)
+                  ((equal op ??) op)
+                  (t '=)))
+    (list 'org-agenda-compare-effort (list 'quote op)
+         (org-hh:mm-string-to-minutes e))))
+
+(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 (org-get-at-bol 'effort-minutes)))
+    (if (equal op ??)
+       (not eff)
+      (funcall op (or eff (if org-sort-agenda-noeffort-is-high 32767 0))
+              value))))
+
+(defun org-agenda-filter-apply (filter)
+  "Set FILTER as the new agenda filter and apply it."
+  (let (tags)
+    (setq org-agenda-filter filter
+         org-agenda-filter-form (org-agenda-filter-make-matcher))
+    (org-agenda-set-mode-name)
+    (save-excursion
+      (goto-char (point-min))
+      (while (not (eobp))
+       (if (org-get-at-bol 'org-marker)
+           (progn
+             (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))))
+    (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 (overlays-at pos))
+      (when (and (overlay-get ov 'invisible)
+                (eq (overlay-get ov 'type) 'tags-filter))
+       (goto-char pos)
+       (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)
-  (setq org-agenda-filter-overlays nil))
+  (mapc 'delete-overlay org-agenda-filter-overlays)
+  (setq org-agenda-filter-overlays nil)
+  (setq org-agenda-filter nil)
+  (setq org-agenda-filter-form nil)
+  (org-agenda-set-mode-name))
 
 (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)
   (cond
    ((memq org-agenda-type '(timeline agenda))
-    (if (y-or-n-p "Re-display with inactive time stamps included? ")
-       (let ((org-agenda-include-inactive-timestamps t))
-         (org-agenda-redo))
-      (error "Abort")))
+    (let ((org-agenda-include-inactive-timestamps t))
+      (org-agenda-redo))
+    (message "Display now includes inactive timestamps as well"))
    ((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
@@ -4220,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 ()
@@ -4231,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")))))
@@ -4254,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))))
 
@@ -4285,11 +6088,39 @@ With prefix ARG, go backward that many times the current span."
   (interactive "p")
   (org-agenda-later (- arg)))
 
+(defun org-agenda-view-mode-dispatch ()
+  "Call one of the view mode commands."
+  (interactive)
+  (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))
+      (?w (call-interactively 'org-agenda-week-view))
+      (?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))
+      ((?R ?r) (call-interactively 'org-agenda-clockreport-mode))
+      ((?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))
+          (message "Display now includes inactive timestamps as well"))
+      (?q (message "Abort"))
+      (otherwise (error "Invalid key" )))))
+
 (defun org-agenda-day-view (&optional day-of-year)
   "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.
@@ -4299,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.
@@ -4324,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."
@@ -4411,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)
@@ -4438,31 +6258,60 @@ 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-clockreport-mode ()
-  "Toggle clocktable mode in an agenda buffer."
-  (interactive)
+(defun org-agenda-entry-text-mode (&optional arg)
+  "Toggle entry text mode in an agenda buffer."
+  (interactive "P")
+  (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
+             (if (integerp arg) arg org-agenda-entry-text-maxlines)))
+        (org-agenda-entry-text-show)))
+  (org-agenda-set-mode-name)
+  (message "Entry text mode is %s.  Maximum number of lines is %d"
+          (if org-agenda-entry-text-mode "on" "off")
+          (if (integerp arg) arg org-agenda-entry-text-maxlines)))
+
+(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"
           (if org-agenda-clockreport-mode "on" "off")))
 
-(defun org-agenda-log-mode ()
-  "Toggle log mode in an agenda buffer."
-  (interactive)
+(defun org-agenda-log-mode (&optional special)
+  "Toggle log mode in an agenda buffer.
+With argument SPECIAL, show all possible log items, not only the ones
+configured in `org-agenda-log-mode-items'.
+With a double `C-u' prefix arg, show *only* log items, nothing else."
+  (interactive "P")
   (org-agenda-check-type t 'agenda 'timeline)
-  (setq org-agenda-show-log (not org-agenda-show-log))
+  (setq org-agenda-show-log
+       (if (equal special '(16))
+           'only
+         (if special '(closed clock state)
+           (not org-agenda-show-log))))
   (org-agenda-set-mode-name)
   (org-agenda-redo)
   (message "Log mode is %s"
           (if org-agenda-show-log "on" "off")))
 
 (defun org-agenda-archives-mode (&optional with-files)
-  "Toggle log mode in an agenda buffer."
+  "Toggle inclusion of items in trees marked with :ARCHIVE:.
+When called with a prefix argument, include all archive files as well."
   (interactive "P")
   (setq org-agenda-archives-mode
        (if with-files t (if org-agenda-archives-mode nil 'trees)))
@@ -4489,6 +6338,16 @@ so that the date SD will be in that range."
   (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)
@@ -4502,43 +6361,77 @@ so that the date SD will be in that range."
 (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-include-diary   " Diary"  "")
-               (if org-agenda-use-time-grid   " Grid"   "")
-               (if org-agenda-show-log        " Log"    "")
-               (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 ":")))
@@ -4547,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)
@@ -4571,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
@@ -4600,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)
@@ -4652,27 +6554,67 @@ 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)
-                    (<= p end))
+                    (< p end))
            (let ((inhibit-read-only t))
              (delete-region (point-at-bol) (1+ (point-at-eol)))))
          (beginning-of-line 0))))))
 
-(defun org-agenda-open-link ()
-  "Follow the link in the current line, if any."
-  (interactive)
-  (org-agenda-copy-local-variable 'org-link-abbrev-alist-local)
-  (save-excursion
-    (save-restriction
-      (narrow-to-region (point-at-bol) (point-at-eol))
-      (org-open-at-point))))
+(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 (org-get-at-bol 'org-hd-marker)
+                      (org-agenda-error)))
+          (buffer (marker-buffer marker))
+          (pos (marker-position marker))
+          (rfloc (or rfloc
+                     (org-refile-get-location
+                      (if goto "Goto: " "Refile to: ") buffer
+                      org-refile-allow-creating-parent-nodes))))
+      (with-current-buffer buffer
+       (save-excursion
+         (save-restriction
+           (widen)
+           (goto-char marker)
+           (org-remove-subtree-entries-from-agenda)
+           (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 line in the agenda.  It also looks
+at the text of the entry itself."
+  (interactive "P")
+  (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))))))
@@ -4680,19 +6622,23 @@ If this information is not given, the function uses the tree at point."
 (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."
@@ -4700,13 +6646,126 @@ If this information is not given, the function uses the tree at point."
   (mouse-set-point ev)
   (org-agenda-goto))
 
-(defun org-agenda-show ()
-  "Display the Org-mode file which contains the item at point."
+(defun org-agenda-show (&optional full-entry)
+  "Display the Org-mode file which contains the item at point.
+With prefix argument FULL-ENTRY, make the entire entry visible
+if it was hidden in the outline."
+  (interactive "P")
+  (let ((win (selected-window)))
+    (if full-entry
+       (let ((org-show-entry-below t))
+         (org-agenda-goto t))
+      (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 selects the amount of information to display:
+
+0   hide the subtree
+1   just show the entry according to defaults.
+2   show the children view
+3   show the subtree view
+4   show the entire subtree and any LOGBOOK drawers
+5   show the entire subtree and any drawers
+With prefix argument FULL-ENTRY, make the entire entry visible
+if it was hidden in the outline."
+  (interactive "p")
   (let ((win (selected-window)))
     (org-agenda-goto t)
+    (org-recenter-heading 1)
+    (cond
+     ((= more 0)
+      (hide-subtree)
+      (save-excursion
+       (org-back-to-heading)
+       (run-hook-with-args 'org-cycle-hook 'folded))
+      (message "Remote: FOLDED"))
+     ((and (interactive-p) (= more 1))
+      (message "Remote: show with default settings"))
+     ((= more 2)
+      (show-entry)
+      (show-children)
+      (save-excursion
+       (org-back-to-heading)
+       (run-hook-with-args 'org-cycle-hook 'children))
+      (message "Remote: CHILDREN"))
+     ((= more 3)
+      (show-subtree)
+      (save-excursion
+       (org-back-to-heading)
+       (run-hook-with-args 'org-cycle-hook 'subtree))
+      (message "Remote: SUBTREE"))
+     ((= more 4)
+      (let* ((org-drawers (delete "LOGBOOK" (copy-sequence org-drawers)))
+            (org-drawer-regexp
+             (concat "^[ \t]*:\\("
+                     (mapconcat 'regexp-quote org-drawers "\\|")
+                     "\\):[ \t]*$")))
+       (show-subtree)
+       (save-excursion
+         (org-back-to-heading)
+         (org-cycle-hide-drawers 'subtree)))
+      (message "Remote: SUBTREE AND LOGBOOK"))
+     ((> more 4)
+      (show-subtree)
+      (message "Remote: SUBTREE AND ALL DRAWERS")))
     (select-window win)))
 
+(defun org-recenter-heading (n)
+  (save-excursion
+    (org-back-to-heading)
+    (recenter n)))
+
+(defvar org-agenda-cycle-counter nil)
+(defun org-agenda-cycle-show (&optional n)
+  "Show the current entry in another window, with default settings.
+Default settings are taken from `org-show-hierarchy-above' and siblings.
+When use repeatedly in immediate succession, the remote entry will cycle
+through visibility
+
+children -> subtree -> folded
+
+When called with a numeric prefix arg, that arg will be passed through to
+`org-agenda-show-1'.  For the interpretation of that argument, see the
+docstring of `org-agenda-show-1'."
+  (interactive "P")
+  (if (integerp n)
+      (setq org-agenda-cycle-counter n)
+    (if (not (eq last-command this-command))
+       (setq org-agenda-cycle-counter 1)
+      (if (equal org-agenda-cycle-counter 0)
+         (setq org-agenda-cycle-counter 2)
+       (setq org-agenda-cycle-counter (1+ org-agenda-cycle-counter))
+       (if (> org-agenda-cycle-counter 3)
+           (setq org-agenda-cycle-counter 0)))))
+  (org-agenda-show-1 org-agenda-cycle-counter))
+
 (defun org-agenda-recenter (arg)
   "Display the Org-mode file which contains the item at point and recenter."
   (interactive "P")
@@ -4723,7 +6782,7 @@ If this information is not given, the function uses the tree at point."
 
 (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 ()
@@ -4734,11 +6793,11 @@ If this information is not given, the function uses the tree at point."
 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)))
@@ -4768,13 +6827,14 @@ 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))
+        (hdmarker (org-get-at-bol 'org-hd-marker))
+        (todayp (org-agenda-todayp (org-get-at-bol 'day)))
         (inhibit-read-only t)
-        newhead)
+        org-agenda-headline-snapshot-before-repeat newhead just-one)
     (org-with-remote-undo buffer
       (with-current-buffer buffer
        (widen)
@@ -4783,26 +6843,34 @@ the same tree node, and the headline of the tree node in the Org-mode file."
        (save-excursion
          (and (outline-next-heading)
               (org-flag-heading nil)))   ; show the next heading
-       (org-todo arg)
+       (let ((current-prefix-arg arg))
+         (call-interactively 'org-todo))
        (and (bolp) (forward-char 1))
        (setq newhead (org-get-heading))
+       (when (and (org-bound-and-true-p
+                   org-agenda-headline-snapshot-before-repeat)
+                  (not (equal org-agenda-headline-snapshot-before-repeat
+                              newhead))
+                  todayp)
+         (setq newhead org-agenda-headline-snapshot-before-repeat
+               just-one t))
        (save-excursion
          (org-back-to-heading)
          (move-marker org-last-heading-marker (point))))
       (beginning-of-line 1)
       (save-excursion
-       (org-agenda-change-all-lines newhead hdmarker 'fixface))
+       (org-agenda-change-all-lines newhead hdmarker 'fixface just-one))
       (org-move-to-column col))))
 
 (defun org-agenda-add-note (&optional arg)
   "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)
@@ -4813,31 +6881,41 @@ the same tree node, and the headline of the tree node in the Org-mode file."
             (org-flag-heading nil)))   ; show the next heading
       (org-add-note))))
 
-(defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
+(defun org-agenda-change-all-lines (newhead hdmarker
+                                           &optional fixface just-this)
   "Change all lines in the agenda buffer which match HDMARKER.
 The new content of the line will be NEWHEAD (as modified by
 `org-format-agenda-item').  HDMARKER is checked with
 `equal' against all `org-hd-marker' text properties in the file.
-If FIXFACE is non-nil, the face of each item is modified acording to
-the new TODO state."
+If FIXFACE is non-nil, the face of each item is modified according to
+the new TODO state.
+If JUST-THIS is non-nil, change just the current line, not all.
+If FORCE-TAGS is non nil, the car of it returns the new tags."
   (let* ((inhibit-read-only t)
+        (line (org-current-line))
+        (thetags (with-current-buffer (marker-buffer hdmarker)
+                   (save-excursion (save-restriction (widen)
+                                                     (goto-char hdmarker)
+                                                     (org-get-tags-at)))))
         props m pl undone-face done-face finish new dotime cat tags)
     (save-excursion
       (goto-char (point-max))
       (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)
-               tags (get-text-property (point) 'tags)
+               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)
@@ -4863,16 +6941,16 @@ the new TODO state."
   (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)
-        (list 'face (delq nil (adjoin 'org-tag
-                                       (let ((prop (get-text-property
-                                                    (match-beginning 2) 'face)))
-                                         (if (listp prop)
-                                             prop
-                                           (list prop)))))))
+        (list 'face (delq nil (let ((prop (get-text-property
+                                           (match-beginning 2) 'face)))
+                                (or (listp prop) (setq prop (list prop)))
+                                (if (memq 'org-tag prop)
+                                    prop
+                                  (cons 'org-tag prop))))))
        (setq l (- (match-end 2) (match-beginning 2))
              c (if (< org-agenda-tags-column 0)
                    (- (abs org-agenda-tags-column) l)
@@ -4881,7 +6959,10 @@ the new TODO state."
        (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)))))
 
 (defun org-agenda-priority-up ()
   "Increase the priority of line at point, also in Org-mode file."
@@ -4898,10 +6979,12 @@ the new TODO state."
 This changes the line at point, all other lines in the agenda referring to
 the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive)
+  (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)
@@ -4921,14 +7004,14 @@ the same tree node, and the headline of the tree node in the Org-mode file."
       (beginning-of-line 1))))
 
 ;; FIXME: should fix the tags property of the agenda line.
-(defun org-agenda-set-tags ()
+(defun org-agenda-set-tags (&optional tag onoff)
   "Set tags for the current headline."
   (interactive)
   (org-agenda-check-no-diary)
   (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))
@@ -4944,18 +7027,67 @@ the same tree node, and the headline of the tree node in the Org-mode file."
            (and (outline-next-heading)
                 (org-flag-heading nil)))   ; show the next heading
          (goto-char pos)
-         (call-interactively 'org-set-tags)
+         (if tag
+             (org-toggle-tag tag onoff)
+           (call-interactively 'org-set-tags))
          (end-of-line 1)
          (setq newhead (org-get-heading)))
        (org-agenda-change-all-lines newhead hdmarker)
        (beginning-of-line 1)))))
 
+(defun org-agenda-set-property ()
+  "Set a property for the current headline."
+  (interactive)
+  (org-agenda-check-no-diary)
+  (org-agenda-show)   ;;; FIXME This is a stupid hack and should not be needed
+  (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
+                      (org-agenda-error)))
+        (buffer (marker-buffer hdmarker))
+        (pos (marker-position hdmarker))
+        (inhibit-read-only t)
+        newhead)
+    (org-with-remote-undo buffer
+      (with-current-buffer buffer
+       (widen)
+       (goto-char pos)
+       (save-excursion
+         (org-show-context 'agenda))
+       (save-excursion
+         (and (outline-next-heading)
+              (org-flag-heading nil)))   ; show the next heading
+       (goto-char pos)
+       (call-interactively 'org-set-property)))))
+
+(defun org-agenda-set-effort ()
+  "Set the effort property for the current headline."
+  (interactive)
+  (org-agenda-check-no-diary)
+  (org-agenda-show)   ;;; FIXME This is a stupid hack and should not be needed
+  (let* ((hdmarker (or (org-get-at-bol 'org-hd-marker)
+                      (org-agenda-error)))
+        (buffer (marker-buffer hdmarker))
+        (pos (marker-position hdmarker))
+        (inhibit-read-only t)
+        newhead)
+    (org-with-remote-undo buffer
+      (with-current-buffer buffer
+       (widen)
+       (goto-char pos)
+       (save-excursion
+         (org-show-context 'agenda))
+       (save-excursion
+         (and (outline-next-heading)
+              (org-flag-heading nil)))   ; show the next heading
+       (goto-char pos)
+       (call-interactively 'org-set-effort)
+       (end-of-line 1)))))
+
 (defun org-agenda-toggle-archive-tag ()
   "Toggle the archive tag for the current entry."
   (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))
@@ -4975,12 +7107,44 @@ the same tree node, and the headline of the tree node in the Org-mode file."
       (org-agenda-change-all-lines newhead hdmarker)
       (beginning-of-line 1))))
 
+(defun org-agenda-do-date-later (arg)
+  (interactive "P")
+  (cond
+   ((or (equal arg '(16))
+       (memq last-command
+             '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
+    (setq this-command 'org-agenda-date-later-minutes)
+    (org-agenda-date-later-minutes 1))
+   ((or (equal arg '(4))
+       (memq last-command
+             '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
+    (setq this-command 'org-agenda-date-later-hours)
+    (org-agenda-date-later-hours 1))
+   (t
+    (org-agenda-date-later (prefix-numeric-value arg)))))
+
+(defun org-agenda-do-date-earlier (arg)
+  (interactive "P")
+  (cond
+   ((or (equal arg '(16))
+       (memq last-command
+             '(org-agenda-date-later-minutes org-agenda-date-earlier-minutes)))
+    (setq this-command 'org-agenda-date-earlier-minutes)
+    (org-agenda-date-earlier-minutes 1))
+   ((or (equal arg '(4))
+       (memq last-command
+             '(org-agenda-date-later-hours org-agenda-date-earlier-hours)))
+    (setq this-command 'org-agenda-date-earlier-hours)
+    (org-agenda-date-earlier-hours 1))
+   (t
+    (org-agenda-date-earlier (prefix-numeric-value arg)))))
+
 (defun org-agenda-date-later (arg &optional what)
   "Change the date of this item to one day later."
   (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)))
@@ -4999,16 +7163,40 @@ the same tree node, and the headline of the tree node in the Org-mode file."
   (interactive "p")
   (org-agenda-date-later (- arg) what))
 
+(defun org-agenda-date-later-minutes (arg)
+  "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
+  (interactive "p")
+  (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
+  (org-agenda-date-later arg 'minute))
+
+(defun org-agenda-date-earlier-minutes (arg)
+  "Change the time of this item, in units of `org-time-stamp-rounding-minutes'."
+  (interactive "p")
+  (setq arg (* arg (cadr org-time-stamp-rounding-minutes)))
+  (org-agenda-date-earlier arg 'minute))
+
+(defun org-agenda-date-later-hours (arg)
+  "Change the time of this item, in hour steps."
+  (interactive "p")
+  (org-agenda-date-later arg 'hour))
+
+(defun org-agenda-date-earlier-hours (arg)
+  "Change the time of this item, in hour steps."
+  (interactive "p")
+  (org-agenda-date-earlier arg 'hour))
+
 (defun org-agenda-show-new-time (marker stamp &optional prefix)
   "Show new date stamp via text properties."
   ;; We use text properties to make this undoable
-  (let ((inhibit-read-only t))
+  (let ((inhibit-read-only t)
+       (buffer-invisibility-spec))
     (setq stamp (concat " " prefix " => " stamp))
     (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)
              ;; Use `duplicable' property to trigger undo recording
               (let ((ex (make-extent nil nil))
@@ -5031,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)))
@@ -5039,24 +7227,25 @@ 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))
         (pos (marker-position marker))
         (org-insert-labeled-timestamps-at-point nil)
         ts)
-    (when type (message "%s" type) (sit-for 3))
     (set-marker-insertion-type marker t)
     (org-with-remote-undo buffer
       (with-current-buffer buffer
@@ -5067,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))
@@ -5082,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 ()
@@ -5093,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 (pos ans)
-    (message "Select action: [m]ark | [s]chedule [d]eadline [r]emember [ ]show")
+  (let (ans)
+    (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
@@ -5120,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
@@ -5140,23 +7333,23 @@ 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
              (widen)
              (goto-char org-agenda-action-marker)
              (eval form))))))))
-  
+
 (defun org-agenda-clock-in (&optional arg)
   "Start the clock on the currently selected item."
   (interactive "P")
   (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)
@@ -5169,15 +7362,26 @@ The cursor may be at a date in the calendar, or in the Org agenda."
          (org-cycle-hide-drawers 'children)
          (org-clock-in arg)
          (setq newhead (org-get-heading)))
-       (org-agenda-change-all-lines newhead hdmarker t)))))
+       (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"))
-  (org-with-remote-undo (marker-buffer org-clock-marker)
-    (org-clock-out)))
+  (let ((marker (make-marker)) newhead)
+    (org-with-remote-undo (marker-buffer org-clock-marker)
+      (with-current-buffer (marker-buffer org-clock-marker)
+       (save-excursion
+         (save-restriction
+           (widen)
+           (goto-char org-clock-marker)
+           (org-back-to-heading t)
+           (move-marker marker (point))
+           (org-clock-out)
+           (setq newhead (org-get-heading))))))
+    (org-agenda-change-all-lines newhead marker)
+    (move-marker marker nil)))
 
 (defun org-agenda-clock-cancel (&optional arg)
   "Cancel the currently running clock."
@@ -5187,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
@@ -5288,11 +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)
-        (calendar-view-diary-initially-flag nil)
-        (calendar-view-holidays-initially-flag nil)
-        (view-diary-entries-initially nil))
+        (calendar-view-diary-initially-flag nil))
     (calendar)
     (calendar-goto-date date)))
 
@@ -5331,8 +7729,250 @@ This is a command that has to be installed in `calendar-mode-map'."
             "Chinese:    " (calendar-chinese-date-string date) "\n"))
     (with-output-to-temp-buffer "*Dates*"
       (princ s))
-    (if (fboundp 'fit-window-to-buffer)
-       (fit-window-to-buffer (get-buffer-window "*Dates*")))))
+    (org-fit-window-to-buffer (get-buffer-window "*Dates*"))))
+
+;;; Bulk commands
+
+(defvar org-agenda-bulk-marked-entries nil
+  "List of markers that refer to marked entries in the agenda.")
+
+(defun org-agenda-bulk-marked-p ()
+  (eq (get-char-property (point-at-bol) 'type)
+      'org-marked-entry-overlay))
+
+(defun org-agenda-bulk-mark (&optional arg)
+  "Mark the entry at point for future bulk action."
+  (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."
+  (interactive)
+  (when (org-agenda-bulk-marked-p)
+    (org-agenda-bulk-remove-overlays
+     (point-at-bol) (+ 2 (point-at-bol)))
+    (setq org-agenda-bulk-marked-entries
+         (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)))
+
+(defun org-agenda-bulk-toggle ()
+ "Toggle marking the entry at point for bulk action."
+ (interactive)
+ (if (org-agenda-bulk-marked-p)
+     (org-agenda-bulk-unmark)
+   (org-agenda-bulk-mark)))
+
+(defun org-agenda-bulk-remove-overlays (&optional beg end)
+  "Remove the mark overlays between BEG and END in the agenda buffer.
+BEG and END default to the buffer limits.
+
+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 (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.
+This will remove the markers, and the overlays."
+  (interactive)
+  (mapc (lambda (m) (move-marker m nil)) org-agenda-bulk-marked-entries)
+  (setq org-agenda-bulk-marked-entries nil)
+  (org-agenda-bulk-remove-overlays (point-min) (point-max)))
+
+(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 [$]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 ?$)
+      (setq cmd '(org-agenda-archive)))
+
+     ((equal action ?A)
+      (setq cmd '(org-agenda-archive-to-archive-sibling)))
+
+     ((member action '(?r ?w))
+      (setq rfloc (org-refile-get-location
+                  "Refile to: "
+                  (marker-buffer (car org-agenda-bulk-marked-entries))
+                  org-refile-allow-creating-parent-nodes))
+      (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) t)
+           redo-at-end t))
+
+     ((equal action ?t)
+      (setq state (org-icompleting-read
+                  "Todo state: "
+                  (with-current-buffer (marker-buffer (car entries))
+                    (mapcar 'list org-todo-keywords-1))))
+      (setq cmd `(let ((org-inhibit-blocking t)
+                      (org-inhibit-logging 'note))
+                  (org-agenda-todo ,state))))
+
+     ((memq action '(?- ?+))
+      (setq tag (org-icompleting-read
+                (format "Tag to %s: " (if (eq action ?+) "add" "remove"))
+                (with-current-buffer (marker-buffer (car entries))
+                  (delq nil
+                        (mapcar (lambda (x)
+                                  (if (stringp (car x)) x)) org-tag-alist)))))
+      (setq cmd `(org-agenda-set-tags ,tag ,(if (eq action ?+) ''on ''off))))
+
+     ((memq action '(?s ?d))
+      (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))
+                          (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
+    (setq entries (sort entries
+                       (lambda (a b)
+                         (cond
+                          ((equal (marker-buffer a) (marker-buffer b))
+                           (< (marker-position a) (marker-position b)))
+                          (t
+                           (string< (buffer-name (marker-buffer a))
+                                    (buffer-name (marker-buffer b))))))))
+
+    ;; Now loop over all markers and apply cmd
+    (while (setq e (pop entries))
+      (setq pos (text-property-any (point-min) (point-max) 'org-hd-marker e))
+      (if (not pos)
+         (progn (message "Skipping removed entry at %s" e)
+                (setq cntskip (1+ cntskip)))
+       (goto-char pos)
+       (eval cmd)
+       (setq org-agenda-bulk-marked-entries
+             (delete e org-agenda-bulk-marked-entries))
+       (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)
+                ""
+              (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
 
@@ -5342,7 +7982,7 @@ This is a command that has to be installed in `calendar-mode-map'."
 (defun org-agenda-to-appt (&optional refresh filter)
   "Activate appointments found in `org-agenda-files'.
 With a \\[universal-argument] prefix, refresh the list of
-appointements.
+appointments.
 
 If FILTER is t, interactively prompt the user for a regular
 expression, and filter out entries that don't match it.
@@ -5359,17 +7999,21 @@ 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)
         (files (org-agenda-files 'unrestricted)) entries file)
     ;; Get all entries which may contain an appt
+    (org-prepare-agenda-buffers files)
     (while (setq file (pop files))
       (setq entries
            (append entries
@@ -5406,18 +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
-