~ubuntu-branches/ubuntu/karmic/emacs-snapshot/karmic

« back to all changes in this revision

Viewing changes to lisp/org/org.el

  • Committer: Bazaar Package Importer
  • Author(s): Reinhard Tartler
  • Date: 2009-04-05 09:14:30 UTC
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: james.westby@ubuntu.com-20090405091430-nw07lynn2arotjbe
Tags: upstream-20090320
ImportĀ upstreamĀ versionĀ 20090320

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
;;; org.el --- Outline-based notes management and organizer
2
2
;; Carstens outline-mode for keeping track of everything.
3
 
;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
3
;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
 
4
;;   Free Software Foundation, Inc.
4
5
;;
5
6
;; Author: Carsten Dominik <carsten at orgmode dot org>
6
7
;; Keywords: outlines, hypermedia, calendar, wp
7
8
;; Homepage: http://orgmode.org
8
 
;; Version: 6.09a
 
9
;; Version: 6.21b
9
10
;;
10
11
;; This file is part of GNU Emacs.
11
12
;;
87
88
(require 'org-compat)
88
89
(require 'org-faces)
89
90
(require 'org-list)
 
91
(require 'org-footnote)
90
92
 
91
93
;;;; Customization variables
92
94
 
93
95
;;; Version
94
96
 
95
 
(defconst org-version "6.09a"
 
97
(defconst org-version "6.21b"
96
98
  "The version number of the file org.el.")
97
99
 
98
100
(defun org-version (&optional here)
125
127
  "Have the modules been loaded already?")
126
128
 
127
129
(defun org-load-modules-maybe (&optional force)
128
 
  "Load all extensions listed in `org-default-extensions'."
 
130
  "Load all extensions listed in `org-modules'."
129
131
  (when (or force (not org-modules-loaded))
130
132
    (mapc (lambda (ext)
131
133
            (condition-case nil (require ext)
143
145
  (let ((a (member 'org-infojs org-modules)))
144
146
    (and a (setcar a 'org-jsinfo))))
145
147
 
146
 
(defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
 
148
(defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
147
149
  "Modules that should always be loaded together with org.el.
148
150
If a description starts with <C>, the file is not part of Emacs
149
151
and loading it will require that you have downloaded and properly installed
162
164
        (const :tag "   bbdb:              Links to BBDB entries" org-bbdb)
163
165
        (const :tag "   bibtex:            Links to BibTeX entries" org-bibtex)
164
166
        (const :tag "   gnus:              Links to GNUS folders/messages" org-gnus)
165
 
        (const :tag "   id:                Global id's for identifying entries" org-id)
 
167
        (const :tag "   id:                Global IDs for identifying entries" org-id)
166
168
        (const :tag "   info:              Links to Info nodes" org-info)
167
169
        (const :tag "   jsinfo:            Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
168
170
        (const :tag "   irc:               Links to IRC/ERC chat sessions" org-irc)
172
174
        (const :tag "   rmail:             Links to RMAIL folders/messages" org-rmail)
173
175
        (const :tag "   vm:                Links to VM folders/messages" org-vm)
174
176
        (const :tag "   wl:                Links to Wanderlust folders/messages" org-wl)
 
177
        (const :tag "   w3m:               Special cut/past from w3m to Org." org-w3m)
175
178
        (const :tag "   mouse:             Additional mouse support" org-mouse)
176
179
 
177
180
        (const :tag "C  annotate-file:     Annotate a file with org syntax" org-annotate-file)
178
 
        (const :tag "C  annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
 
181
        (const :tag "C  annotation-helper: Call Remember directly from Browser" org-annotation-helper)
179
182
        (const :tag "C  bookmark:          Org links to bookmarks" org-bookmark)
 
183
        (const :tag "C  browser-url:       Store link, directly from Browser" org-browser-url)
180
184
        (const :tag "C  depend:            TODO dependencies for Org-mode" org-depend)
181
185
        (const :tag "C  elisp-symbol:      Org links to emacs-lisp symbols" org-elisp-symbol)
182
186
        (const :tag "C  eval:              Include command output as text" org-eval)
 
187
        (const :tag "C  eval-light:        Evaluate inbuffer-code on demand" org-eval-light)
183
188
        (const :tag "C  expiry:            Expiry mechanism for Org entries" org-expiry)
184
 
        (const :tag "C  id:                Global id's for identifying entries" org-id)
 
189
        (const :tag "C  exp-blocks:        Pre-process blocks for export" org-exp-blocks)
185
190
        (const :tag "C  interactive-query: Interactive modification of tags query" org-interactive-query)
186
191
        (const :tag "C  mairix:            Hook mairix search into Org for different MUAs" org-mairix)
187
192
        (const :tag "C  man:               Support for links to manpages in Org-mode" org-man)
194
199
        (const :tag "C  sqlinsert:         Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
195
200
        (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
196
201
 
 
202
(defcustom org-support-shift-select nil
 
203
  "Non-nil means, make shift-cursor commands select text when possible.
 
204
 
 
205
In Emacs 23, when `shift-select-mode' is on, shifted cursor keys start
 
206
selecting a region, or enlarge thusly regions started in this way.
 
207
In Org-mode, in special contexts, these same keys are used for other
 
208
purposes, important enough to compete with shift selection.  Org tries
 
209
to balance these needs by supporting `shift-select-mode' outside these
 
210
special contexts, under control of this variable.
 
211
 
 
212
The default of this variable is nil, to avoid confusing behavior.  Shifted
 
213
cursor keys will then execute Org commands in the following contexts:
 
214
- on a headline, changing TODO state (left/right) and priority (up/down)
 
215
- on a time stamp, changing the time
 
216
- in a plain list item, changing the bullet type
 
217
- in a property definition line, switching between allowed values
 
218
- in the BEGIN line of a clock table (changing the time block).
 
219
Outside these contexts, the commands will throw an error.
 
220
 
 
221
When this variable is t and the cursor is not in a special context,
 
222
Org-mode will support shift-selection for making and enlarging regions.
 
223
To make this more effective, the bullet cycling will no longer happen
 
224
anywhere in an item line, but only if the cursor is exactly on the bullet.
 
225
 
 
226
If you set this variable to the symbol `always', then the keys
 
227
will not be special in headlines, property lines, and item lines, to make
 
228
shift selection work there as well.  If this is what you want, you can
 
229
use the following alternative commands: `C-c C-t' and `C-c ,' to
 
230
change TODO state and priority, `C-u C-u C-c C-t' can be used to switch
 
231
TODO sets, `C-c -' to cycle item bullet types, and properties can be
 
232
edited by hand or in column view.
 
233
 
 
234
However, when the cursor is on a timestamp, shift-cursor commands
 
235
will still edit the time stamp - this is just too good to give up.
 
236
 
 
237
XEmacs user should have this variable set to nil, because shift-select-mode
 
238
is Emacs 23 only."
 
239
  :group 'org
 
240
  :type '(choice
 
241
          (const :tag "Never" nil)
 
242
          (const :tag "When outside special context" t)
 
243
          (const :tag "Everywhere except timestamps" always)))
197
244
 
198
245
(defgroup org-startup nil
199
246
  "Options concerning startup of Org-mode."
315
362
(defcustom org-ellipsis nil
316
363
  "The ellipsis to use in the Org-mode outline.
317
364
When nil, just use the standard three dots.  When a string, use that instead,
318
 
When a face, use the standart 3 dots, but with the specified face.
 
365
When a face, use the standard 3 dots, but with the specified face.
319
366
The change affects only Org-mode (which will then use its own display table).
320
367
Changing this requires executing `M-x org-mode' in a buffer to become
321
368
effective."
516
563
  "Cycle globally if cursor is at beginning of buffer and not at a headline.
517
564
This makes it possible to do global cycling without having to use S-TAB or
518
565
C-u TAB.  For this special case to work, the first line of the buffer
519
 
must not be a headline - it may be empty ot some other text.  When used in
 
566
must not be a headline - it may be empty or some other text.  When used in
520
567
this way, `org-cycle-hook' is disables temporarily, to make sure the
521
568
cursor stays at the beginning of the buffer.
522
569
When this option is nil, don't do anything special at the beginning
637
684
  "Non-nil means, when yanking subtrees, fold them.
638
685
If the kill is a single subtree, or a sequence of subtrees, i.e. if
639
686
it starts with a heading and all other headings in it are either children
640
 
or siblings, then fold all the subtrees."
 
687
or siblings, then fold all the subtrees.  However, do this only if no
 
688
text after the yank would be swallowed into a folded tree by this action."
 
689
  :group 'org-edit-structure
 
690
  :type 'boolean)
 
691
 
 
692
(defcustom org-yank-adjusted-subtrees nil
 
693
  "Non-nil means, when yanking subtrees, adjust the level.
 
694
With this setting, `org-paste-subtree' is used to insert the subtree, see
 
695
this function for details."
641
696
  :group 'org-edit-structure
642
697
  :type 'boolean)
643
698
 
677
732
  :group 'org-structure
678
733
  :type 'boolean)
679
734
 
680
 
(defcustom org-blank-before-new-entry '((heading . nil)
681
 
                                        (plain-list-item . nil))
 
735
(defcustom org-blank-before-new-entry '((heading . auto)
 
736
                                        (plain-list-item . auto))
682
737
  "Should `org-insert-heading' leave a blank line before new heading/item?
683
738
The value is an alist, with `heading' and `plain-list-item' as car,
684
739
and a boolean flag as cdr."
685
740
  :group 'org-edit-structure
686
741
  :type '(list
687
 
          (cons (const heading) (boolean))
688
 
          (cons (const plain-list-item) (boolean))))
 
742
          (cons (const heading)
 
743
                (choice (const :tag "Never" nil)
 
744
                        (const :tag "Always" t)
 
745
                        (const :tag "Auto" auto)))
 
746
          (cons (const plain-list-item)
 
747
                (choice (const :tag "Never" nil)
 
748
                        (const :tag "Always" t)
 
749
                        (const :tag "Auto" auto)))))
689
750
 
690
751
(defcustom org-insert-heading-hook nil
691
752
  "Hook being run after inserting a new heading."
717
778
                   (const :tag "from `lang' element")
718
779
                   (const :tag "from `style' element")))))
719
780
 
 
781
(defcustom org-coderef-label-format "(ref:%s)"
 
782
  "The default coderef format.
 
783
This format string will be used to search for coderef labels in literal
 
784
examples (EXAMPLE and SRC blocks).  The format can be overwritten
 
785
an individual literal example with the -f option, like
 
786
 
 
787
#+BEGIN_SRC pascal +n -r -l \"((%s))\"
 
788
...
 
789
#+END_SRC
 
790
 
 
791
If you want to use this for HTML export, make sure that the format does
 
792
not introduce special font-locking, and avoid the HTML special
 
793
characters `<', `>', and `&'.  The reason for this restriction is that
 
794
the labels are searched for only after htmlize has done its job."
 
795
  :group 'org-edit-structure ; FIXME this is not in the right group
 
796
  :type 'string)
 
797
 
720
798
(defcustom org-edit-fixed-width-region-mode 'artist-mode
721
799
  "The mode that should be used to edit fixed-width regions.
722
800
These are the regions where each line starts with a colon."
835
913
     [[linkkey:tag][description]]
836
914
 
837
915
If REPLACE is a string, the tag will simply be appended to create the link.
838
 
If the string contains \"%s\", the tag will be inserted there.
 
916
If the string contains \"%s\", the tag will be inserted there.  Alternatively,
 
917
the placeholder \"%h\" will cause a url-encoded version of the tag to
 
918
be inserted at that point (see the function `url-hexify-string').
839
919
 
840
920
REPLACE may also be a function that will be called with the tag as the
841
921
only argument to create the link, which should be returned as a string.
842
922
 
843
923
See the manual for examples."
844
924
  :group 'org-link
845
 
  :type 'alist)
 
925
  :type '(repeat
 
926
          (cons
 
927
           (string :tag "Protocol")
 
928
           (choice
 
929
            (string :tag "Format")
 
930
            (function)))))
846
931
 
847
932
(defcustom org-descriptive-links t
848
933
  "Non-nil means, hide link part and only show description of bracket links.
849
 
Bracket links are like [[link][descritpion]].  This variable sets the initial
 
934
Bracket links are like [[link][description]].  This variable sets the initial
850
935
state in new org-mode buffers.  The setting can then be toggled on a
851
936
per-buffer basis from the Org->Hyperlinks menu."
852
937
  :group 'org-link
869
954
          (const noabbrev)
870
955
          (const adaptive)))
871
956
 
872
 
(defcustom org-activate-links '(bracket angle plain radio tag date)
 
957
(defcustom org-activate-links '(bracket angle plain radio tag date footnote)
873
958
  "Types of links that should be activated in Org-mode files.
874
959
This is a list of symbols, each leading to the activation of a certain link
875
960
type.  In principle, it does not hurt to turn on most link types - there may
876
961
be a small gain when turning off unused link types.  The types are:
877
962
 
878
963
bracket   The recommended [[link][description]] or [[link]] links with hiding.
879
 
angular   Links in angular brackes that may contain whitespace like
 
964
angular   Links in angular brackets that may contain whitespace like
880
965
          <bbdb:Carsten Dominik>.
881
966
plain     Plain links in normal text, no whitespace, like http://google.com.
882
967
radio     Text that is matched by a radio target, see manual for details.
883
968
tag       Tag settings in a headline (link to tag search).
884
969
date      Time stamps (link to calendar).
 
970
footnote  Footnote labels.
885
971
 
886
972
Changing this variable requires a restart of Emacs to become effective."
887
973
  :group 'org-link
888
 
  :type '(set (const :tag "Double bracket links (new style)" bracket)
 
974
  :type '(set :greedy t
 
975
              (const :tag "Double bracket links (new style)" bracket)
889
976
              (const :tag "Angular bracket links (old style)" angular)
890
977
              (const :tag "Plain text links" plain)
891
978
              (const :tag "Radio target matches" radio)
892
979
              (const :tag "Tags" tag)
893
 
              (const :tag "Timestamps" date)))
 
980
              (const :tag "Timestamps" date)
 
981
              (const :tag "Footnotes" footnote)))
894
982
 
895
983
(defcustom org-make-link-description-function nil
896
984
  "Function to use to generate link descriptions from links. If
908
996
 
909
997
(defcustom org-email-link-description-format "Email %c: %.30s"
910
998
  "Format of the description part of a link to an email or usenet message.
911
 
The following %-excapes will be replaced by corresponding information:
 
999
The following %-escapes will be replaced by corresponding information:
912
1000
 
913
1001
%F   full \"From\" field
914
1002
%f   name, taken from \"From\" field, address if no name
915
1003
%T   full \"To\" field
916
1004
%t   first name in \"To\" field, address if no name
917
 
%c   correspondent.  Unually \"from NAME\", but if you sent it yourself, it
 
1005
%c   correspondent.  Usually \"from NAME\", but if you sent it yourself, it
918
1006
     will be \"to NAME\".  See also the variable `org-from-is-user-regexp'.
919
1007
%s   subject
920
1008
%m   message-id.
933
1021
    (when (and user-full-name (not (string= user-full-name "")))
934
1022
      (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
935
1023
    (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
936
 
  "Regexp mached against the \"From:\" header of an email or usenet message.
 
1024
  "Regexp matched against the \"From:\" header of an email or usenet message.
937
1025
It should match if the message is from the user him/herself."
938
1026
  :group 'org-link-store
939
1027
  :type 'regexp)
940
1028
 
 
1029
(defcustom org-link-to-org-use-id 'create-if-interactive
 
1030
  "Non-nil means, storing a link to an Org file will use entry IDs.
 
1031
 
 
1032
Note that before this variable is even considered, org-id must be loaded,
 
1033
to please customize `org-modules' and turn it on.
 
1034
 
 
1035
The variable can have the following values:
 
1036
 
 
1037
t     Create an ID if needed to make a link to the current entry.
 
1038
 
 
1039
create-if-interactive
 
1040
      If `org-store-link' is called directly (interactively, as a user
 
1041
      command), do create an ID to support the link.  But when doing the
 
1042
      job for remember, only use the ID if it already exists.  The
 
1043
      purpose of this setting is to avoid proliferation of unwanted
 
1044
      IDs, just because you happen to be in an Org file when you
 
1045
      call `org-remember' that automatically and preemptively
 
1046
      creates a link.  If you do want to get an ID link in a remember
 
1047
      template to an entry not having an ID, create it first by
 
1048
      explicitly creating a link to it, using `C-c C-l' first.
 
1049
 
 
1050
use-existing
 
1051
      Use existing ID, do not create one.
 
1052
 
 
1053
nil   Never use an ID to make a link, instead link using a text search for
 
1054
      the headline text."
 
1055
  :group 'org-link-store
 
1056
  :type '(choice
 
1057
          (const :tag "Create ID to make link" t)
 
1058
          (const :tag "Create if string link interactively"
 
1059
                 'create-if-interactive)
 
1060
          (const :tag "Only use existing" 'use-existing)
 
1061
          (const :tag "Do not use ID to create link" nil)))
 
1062
 
941
1063
(defcustom org-context-in-file-links t
942
1064
  "Non-nil means, file links from `org-store-link' contain context.
943
1065
A search string will be added to the file name with :: as separator and
966
1088
  :tag "Org Follow Link"
967
1089
  :group 'org-link)
968
1090
 
 
1091
(defcustom org-link-translation-function nil
 
1092
  "Function to translate links with different syntax to Org syntax.
 
1093
This can be used to translate links created for example by the Planner
 
1094
or emacs-wiki packages to Org syntax.
 
1095
The function must accept two parameters, a TYPE containing the link
 
1096
protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
 
1097
which is everything after the link protocol.  It should return a cons
 
1098
with possibly modified values of type and path.
 
1099
Org contains a function for this, so if you set this variable to
 
1100
`org-translate-link-from-planner', you should be able follow many
 
1101
links created by planner."
 
1102
  :group 'org-link-follow
 
1103
  :type 'function)
 
1104
 
969
1105
(defcustom org-follow-link-hook nil
970
1106
  "Hook that is run after a link has been followed."
971
1107
  :group 'org-link-follow
995
1131
  "Number of different positions to be recorded in the ring
996
1132
Changing this requires a restart of Emacs to work correctly."
997
1133
  :group 'org-link-follow
998
 
  :type 'interger)
 
1134
  :type 'integer)
999
1135
 
1000
1136
(defcustom org-link-frame-setup
1001
1137
  '((vm . vm-visit-folder-other-frame)
1011
1147
For Gnus, use any of
1012
1148
    `gnus'
1013
1149
    `gnus-other-frame'
 
1150
    `org-gnus-no-new-news'
1014
1151
For FILE, use any of
1015
1152
    `find-file'
1016
1153
    `find-file-other-window'
1028
1165
          (cons (const gnus)
1029
1166
                (choice
1030
1167
                 (const gnus)
1031
 
                 (const gnus-other-frame)))
 
1168
                 (const gnus-other-frame)
 
1169
                 (const org-gnus-no-new-news)))
1032
1170
          (cons (const file)
1033
1171
                (choice
1034
1172
                 (const find-file)
1108
1246
 
1109
1247
(defconst org-file-apps-defaults-gnu
1110
1248
  '((remote . emacs)
 
1249
    (system . mailcap)
1111
1250
    (t . mailcap))
1112
1251
  "Default file applications on a UNIX or GNU/Linux system.
1113
1252
See `org-file-apps'.")
1115
1254
(defconst org-file-apps-defaults-macosx
1116
1255
  '((remote . emacs)
1117
1256
    (t . "open %s")
 
1257
    (system . "open %s")
1118
1258
    ("ps.gz"  . "gv %s")
1119
1259
    ("eps.gz" . "gv %s")
1120
1260
    ("dvi"    . "xdvi %s")
1131
1271
         (list (if (featurep 'xemacs)
1132
1272
                   'mswindows-shell-execute
1133
1273
                 'w32-shell-execute)
 
1274
               "open" 'file))
 
1275
   (cons 'system
 
1276
         (list (if (featurep 'xemacs)
 
1277
                   'mswindows-shell-execute
 
1278
                 'w32-shell-execute)
1134
1279
               "open" 'file)))
1135
1280
  "Default file applications on a Windows NT system.
1136
1281
The system \"open\" is used for most files.
1140
1285
  '(
1141
1286
    (auto-mode . emacs)
1142
1287
    ("\\.x?html?\\'" . default)
 
1288
    ("\\.pdf\\'" . default)
1143
1289
    )
1144
1290
  "External applications for opening `file:path' items in a document.
1145
1291
Org-mode uses system defaults for different file types, but
1154
1300
 `remote'      Matches a remote file, accessible through tramp or efs.
1155
1301
               Remote files most likely should be visited through Emacs
1156
1302
               because external applications cannot handle such paths.
1157
 
`auto-mode'    Matches files that are mached by any entry in `auto-mode-alist',
1158
 
               so all files Emacs knows how to handle.  Useing this with
 
1303
`auto-mode'    Matches files that are matched by any entry in `auto-mode-alist',
 
1304
               so all files Emacs knows how to handle.  Using this with
1159
1305
               command `emacs' will open most files in Emacs.  Beware that this
1160
 
               will also open html files insite Emacs, unless you add
 
1306
               will also open html files inside Emacs, unless you add
1161
1307
               (\"html\" . default) to the list as well.
1162
1308
 t             Default for files not matched by any of the other options.
 
1309
 `system'      The system command to open files, like `open' on Windows
 
1310
               and Mac OS X, and mailcap under GNU/Linux.  This is the command
 
1311
               that will be selected if you call `C-c C-o' with a double
 
1312
               `C-u C-u' prefix.
1163
1313
 
1164
1314
Possible values for the command are:
1165
1315
 `emacs'       The file will be visited by the current Emacs process.
1166
1316
 `default'     Use the default application for this file type, which is the
1167
1317
               association for t in the list, most likely in the system-specific
1168
1318
               part.
1169
 
               This can be used to overrule an unwanted seting in the
 
1319
               This can be used to overrule an unwanted setting in the
1170
1320
               system-specific variable.
 
1321
 `system'      Use the system command for opening files, like \"open\".
 
1322
               This command is specified by the entry whose car is `system'.
 
1323
               Most likely, the system-specific version of this variable
 
1324
               does define this command, but you can overrule/replace it
 
1325
               here.
1171
1326
 string        A command to be executed by a shell; %s will be replaced
1172
1327
               by the path to the file.
1173
1328
 sexp          A Lisp form which will be evaluated.  The file path will
1180
1335
  :type '(repeat
1181
1336
          (cons (choice :value ""
1182
1337
                        (string :tag "Extension")
 
1338
                        (const :tag "System command to open files" system)
1183
1339
                        (const :tag "Default for unrecognized files" t)
1184
1340
                        (const :tag "Remote file" remote)
1185
1341
                        (const :tag "Links to a directory" directory)
1187
1343
                               auto-mode))
1188
1344
                (choice :value ""
1189
1345
                        (const :tag "Visit with Emacs" emacs)
1190
 
                        (const :tag "Use system default" default)
 
1346
                        (const :tag "Use default" default)
 
1347
                        (const :tag "Use the system command" system)
1191
1348
                        (string :tag "Command")
1192
1349
                        (sexp :tag "Lisp form")))))
1193
1350
 
1194
1351
(defgroup org-refile nil
1195
1352
  "Options concerning refiling entries in Org-mode."
1196
 
  :tag "Org Remember"
 
1353
  :tag "Org Refile"
1197
1354
  :group 'org)
1198
1355
 
1199
1356
(defcustom org-directory "~/org"
1204
1361
  :group 'org-remember
1205
1362
  :type 'directory)
1206
1363
 
1207
 
(defcustom org-default-notes-file "~/.notes"
 
1364
(defcustom org-default-notes-file (convert-standard-filename "~/.notes")
1208
1365
  "Default target for storing notes.
1209
1366
Used by the hooks for remember.el.  This can be a string, or nil to mean
1210
1367
the value of `remember-data-file'.
1218
1375
 
1219
1376
(defcustom org-goto-interface 'outline
1220
1377
  "The default interface to be used for `org-goto'.
1221
 
Allowed vaues are:
 
1378
Allowed values are:
1222
1379
outline                  The interface shows an outline of the relevant file
1223
1380
                         and the correct heading is found by moving through
1224
1381
                         the outline or by searching with incremental search.
1225
1382
outline-path-completion  Headlines in the current buffer are offered via
1226
 
                         completion."
 
1383
                         completion.  This is the interface also used by
 
1384
                         the refile command."
1227
1385
  :group 'org-refile
1228
1386
  :type '(choice
1229
1387
          (const :tag "Outline" outline)
1230
1388
          (const :tag "Outline-path-completion" outline-path-completion)))
1231
1389
 
 
1390
(defcustom org-goto-max-level 5
 
1391
  "Maximum level to be considered when running org-goto with refile interface."
 
1392
  :group 'org-refile
 
1393
  :type 'number)
 
1394
 
1232
1395
(defcustom org-reverse-note-order nil
1233
1396
  "Non-nil means, store new notes at the beginning of a file or entry.
1234
1397
When nil, new notes will be filed to the end of a file or entry.
1235
1398
This can also be a list with cons cells of regular expressions that
1236
1399
are matched against file names, and values."
1237
1400
  :group 'org-remember
 
1401
  :group 'org-refile
1238
1402
  :type '(choice
1239
1403
          (const :tag "Reverse always" t)
1240
1404
          (const :tag "Reverse never" nil)
1246
1410
This is list of cons cells.  Each cell contains:
1247
1411
- a specification of the files to be considered, either a list of files,
1248
1412
  or a symbol whose function or variable value will be used to retrieve
1249
 
  a file name or a list of file names.  Nil means, refile to a different
1250
 
  heading in the current buffer.
1251
 
- A specification of how to find candidate refile targets.  This may be
1252
 
  any of
 
1413
  a file name or a list of file names.  If you use `org-agenda-files' for
 
1414
  that, all agenda files will be scanned for targets.  Nil means, consider
 
1415
  headings in the current buffer.
 
1416
- A specification of how to select find candidate refile targets.  This
 
1417
  may be any of
1253
1418
  - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
1254
1419
    This tag has to be present in all target headlines, inheritance will
1255
1420
    not be considered.
1261
1426
  - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
1262
1427
 
1263
1428
When this variable is nil, all top-level headlines in the current buffer
1264
 
are used, equivalent to the vlaue `((nil . (:level . 1))'."
1265
 
  :group 'org-remember
 
1429
are used, equivalent to the value `((nil . (:level . 1))'."
 
1430
  :group 'org-refile
1266
1431
  :type '(repeat
1267
1432
          (cons
1268
1433
           (choice :value org-agenda-files
1270
1435
                   (const :tag "Current buffer" nil)
1271
1436
                   (function) (variable) (file))
1272
1437
           (choice :tag "Identify target headline by"
1273
 
            (cons :tag "Specific tag" (const :tag) (string))
1274
 
            (cons :tag "TODO keyword" (const :todo) (string))
1275
 
            (cons :tag "Regular expression" (const :regexp) (regexp))
1276
 
            (cons :tag "Level number" (const :level) (integer))
1277
 
            (cons :tag "Max Level number" (const :maxlevel) (integer))))))
 
1438
            (cons :tag "Specific tag" (const :value :tag) (string))
 
1439
            (cons :tag "TODO keyword" (const :value :todo) (string))
 
1440
            (cons :tag "Regular expression" (const :value :regexp) (regexp))
 
1441
            (cons :tag "Level number" (const :value :level) (integer))
 
1442
            (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
1278
1443
 
1279
1444
(defcustom org-refile-use-outline-path nil
1280
1445
  "Non-nil means, provide refile targets as paths.
1281
1446
So a level 3 headline will be available as level1/level2/level3.
1282
1447
When the value is `file', also include the file name (without directory)
1283
1448
into the path.  When `full-file-path', include the full file path."
1284
 
  :group 'org-remember
 
1449
  :group 'org-refile
1285
1450
  :type '(choice
1286
1451
          (const :tag "Not" nil)
1287
1452
          (const :tag "Yes" t)
1288
1453
          (const :tag "Start with file name" file)
1289
1454
          (const :tag "Start with full file path" full-file-path)))
1290
1455
 
 
1456
(defcustom org-outline-path-complete-in-steps t
 
1457
  "Non-nil means, complete the outline path in hierarchical steps.
 
1458
When Org-mode uses the refile interface to select an outline path
 
1459
\(see variable `org-refile-use-outline-path'), the completion of
 
1460
the path can be done is a single go, or if can be done in steps down
 
1461
the headline hierarchy.  Going in steps is probably the best if you
 
1462
do not use a special completion package like `ido' or `icicles'.
 
1463
However, when using these packages, going in one step can be very
 
1464
fast, while still showing the whole path to the entry."
 
1465
  :group 'org-refile
 
1466
  :type 'boolean)
 
1467
 
1291
1468
(defgroup org-todo nil
1292
1469
  "Options concerning TODO items in Org-mode."
1293
1470
  :tag "Org TODO"
1336
1513
so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
1337
1514
 
1338
1515
For backward compatibility, this variable may also be just a list
1339
 
of keywords - in this case the interptetation (sequence or type) will be
 
1516
of keywords - in this case the interpretation (sequence or type) will be
1340
1517
taken from the (otherwise obsolete) variable `org-todo-interpretation'."
1341
1518
  :group 'org-todo
1342
1519
  :group 'org-keywords
1388
1565
  :type '(choice (const sequence)
1389
1566
                 (const type)))
1390
1567
 
1391
 
(defcustom org-use-fast-todo-selection 'prefix
 
1568
(defcustom org-use-fast-todo-selection t
1392
1569
  "Non-nil means, use the fast todo selection scheme with C-c C-t.
1393
1570
This variable describes if and under what circumstances the cycling
1394
1571
mechanism for TODO keywords will be replaced by a single-key, direct
1426
1603
  :group 'org-todo
1427
1604
  :type 'hook)
1428
1605
 
 
1606
(defvar org-blocker-hook nil
 
1607
  "Hook for functions that are allowed to block a state change.
 
1608
 
 
1609
Each function gets as its single argument a property list, see
 
1610
`org-trigger-hook' for more information about this list.
 
1611
 
 
1612
If any of the functions in this hook returns nil, the state change
 
1613
is blocked.")
 
1614
 
 
1615
(defvar org-trigger-hook nil
 
1616
  "Hook for functions that are triggered by a state change.
 
1617
 
 
1618
Each function gets as its single argument a property list with at least
 
1619
the following elements:
 
1620
 
 
1621
 (:type type-of-change :position pos-at-entry-start
 
1622
  :from old-state :to new-state)
 
1623
 
 
1624
Depending on the type, more properties may be present.
 
1625
 
 
1626
This mechanism is currently implemented for:
 
1627
 
 
1628
TODO state changes
 
1629
------------------
 
1630
:type  todo-state-change
 
1631
:from  previous state (keyword as a string), or nil, or a symbol
 
1632
       'todo' or 'done', to indicate the general type of state.
 
1633
:to    new state, like in :from")
 
1634
 
 
1635
(defcustom org-enforce-todo-dependencies nil
 
1636
  "Non-nil means, undone TODO entries will block switching the parent to DONE.
 
1637
Also, if a parent has an :ORDERED: property, switching an entry to DONE will
 
1638
be blocked if any prior sibling is not yet done.
 
1639
This variable needs to be set before org.el is loaded, and you need to
 
1640
restart Emacs after a change to make the change effective.  The only way
 
1641
to change is while Emacs is running is through the customize interface."
 
1642
  :set (lambda (var val)
 
1643
         (set var val)
 
1644
         (if val
 
1645
             (add-hook 'org-blocker-hook
 
1646
                       'org-block-todo-from-children-or-siblings)
 
1647
           (remove-hook 'org-blocker-hook
 
1648
                        'org-block-todo-from-children-or-siblings)))
 
1649
  :group 'org-todo
 
1650
  :type 'boolean)
 
1651
 
 
1652
(defcustom org-enforce-todo-checkbox-dependencies nil
 
1653
  "Non-nil means, unchecked boxes will block switching the parent to DONE.
 
1654
When this is nil, checkboxes have no influence on switching TODO states.
 
1655
When non-nil, you first need to check off all check boxes before the TODO
 
1656
entry can be switched to DONE.
 
1657
This variable needs to be set before org.el is loaded, and you need to
 
1658
restart Emacs after a change to make the change effective.  The only way
 
1659
to change is while Emacs is running is through the customize interface."
 
1660
  :set (lambda (var val)
 
1661
         (set var val)
 
1662
         (if val
 
1663
             (add-hook 'org-blocker-hook
 
1664
                       'org-block-todo-from-checkboxes)
 
1665
           (remove-hook 'org-blocker-hook
 
1666
                        'org-block-todo-from-checkboxes)))
 
1667
  :group 'org-todo
 
1668
  :type 'boolean)
 
1669
 
 
1670
(defcustom org-todo-state-tags-triggers nil
 
1671
  "Tag changes that should be triggered by TODO state changes.
 
1672
This is a list.  Each entry is
 
1673
 
 
1674
  (state-change (tag . flag) .......)
 
1675
 
 
1676
State-change can be a string with a state, and empty string to indicate the
 
1677
state that has no TODO keyword, or it can be one of the symbols `todo'
 
1678
or `done', meaning any not-done or done state, respectively."
 
1679
  :group 'org-todo
 
1680
  :group 'org-tags
 
1681
  :type '(repeat
 
1682
          (cons (choice :tag "When changing to"
 
1683
                 (const :tag "Not-done state" todo)
 
1684
                 (const :tag "Done state" done)
 
1685
                 (string :tag "State"))
 
1686
                (repeat
 
1687
                 (cons :tag "Tag action"
 
1688
                       (string :tag "Tag")
 
1689
                       (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
 
1690
 
1429
1691
(defcustom org-log-done nil
1430
 
  "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
1431
 
When equal to the list (done), also prompt for a closing note.
1432
 
This can also be configured on a per-file basis by adding one of
1433
 
the following lines anywhere in the buffer:
1434
 
 
 
1692
  "Information to record when a task moves to the DONE state.
 
1693
 
 
1694
Possible values are:
 
1695
 
 
1696
nil     Don't add anything, just change the keyword
 
1697
time    Add a time stamp to the task
 
1698
note    Prompt a closing note and add it with template `org-log-note-headings'
 
1699
 
 
1700
This option can also be set with on a per-file-basis with
 
1701
 
 
1702
   #+STARTUP: nologdone
1435
1703
   #+STARTUP: logdone
1436
1704
   #+STARTUP: lognotedone
1437
 
   #+STARTUP: nologdone"
 
1705
 
 
1706
You can have local logging settings for a subtree by setting the LOGGING
 
1707
property to one or more of these keywords."
1438
1708
  :group 'org-todo
1439
1709
  :group 'org-progress
1440
1710
  :type '(choice
1491
1761
(unless (assq 'note org-log-note-headings)
1492
1762
  (push '(note . "%t") org-log-note-headings))
1493
1763
 
 
1764
(defcustom org-log-state-notes-insert-after-drawers nil
 
1765
  "Non-nil means, insert state change notes after any drawers in entry.
 
1766
Only the drawers that *immediately* follow the headline and the
 
1767
deadline/scheduled line are skipped.
 
1768
When nil, insert notes right after the heading and perhaps the line
 
1769
with deadline/scheduling if present."
 
1770
  :group 'org-todo
 
1771
  :group 'org-progress
 
1772
  :type 'boolean)
 
1773
 
1494
1774
(defcustom org-log-states-order-reversed t
1495
1775
  "Non-nil means, the latest state change note will be directly after heading.
1496
1776
When nil, the notes will be orderer according to time."
1634
1914
org-mode generates a time duration."
1635
1915
  :group 'org-time
1636
1916
  :type 'string)
1637
 
  
 
1917
 
1638
1918
(defcustom org-deadline-warning-days 14
1639
1919
  "No. of days before expiration during which a deadline becomes active.
1640
1920
This variable governs the display in sparse trees and in the agenda.
1690
1970
 
1691
1971
IMPORTANT:  This is a feature whose implementation is and likely will
1692
1972
remain incomplete.  Really, it is only here because past midnight seems to
1693
 
ne the favorite working time of John Wiegley :-)"
 
1973
be the favorite working time of John Wiegley :-)"
1694
1974
  :group 'org-time
1695
1975
  :type 'number)
1696
1976
 
1765
2045
  "Non-nil means, fast tags selection interface will also offer TODO states.
1766
2046
This is an undocumented feature, you should not rely on it.")
1767
2047
 
1768
 
(defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
 
2048
(defcustom org-tags-column (if (featurep 'xemacs) -76 -77)
1769
2049
  "The column to which tags should be indented in a headline.
1770
2050
If this number is positive, it specifies the column.  If it is negative,
1771
2051
it means that the tags should be flushright to that column.  For example,
1784
2064
(defcustom org-use-tag-inheritance t
1785
2065
  "Non-nil means, tags in levels apply also for sublevels.
1786
2066
When nil, only the tags directly given in a specific line apply there.
 
2067
This may also be a list of tags that should be inherited, or a regexp that
 
2068
matches tags that should be inherited.  Additional control is possible
 
2069
with the variable  `org-tags-exclude-from-inheritance' which gives an
 
2070
explicit list of tags to be excluded from inheritance., even if the value of
 
2071
`org-use-tag-inheritance' would select it for inheritance.
 
2072
 
1787
2073
If this option is t, a match early-on in a tree can lead to a large
1788
 
number of matches in the subtree.  If you only want to see the first
1789
 
match in a tree during a search, check out the variable
1790
 
`org-tags-match-list-sublevels'.
1791
 
 
1792
 
This may also be a list of tags that should be inherited, or a regexp that
1793
 
matches tags that should be inherited."
 
2074
number of matches in the subtree when constructing the agenda or creating
 
2075
a sparse tree.  If you only want to see the first match in a tree during
 
2076
a search, check out the variable `org-tags-match-list-sublevels'."
1794
2077
  :group 'org-tags
1795
2078
  :type '(choice
1796
2079
          (const :tag "Not" nil)
1798
2081
          (repeat :tag "Specific tags" (string :tag "Tag"))
1799
2082
          (regexp :tag "Tags matched by regexp")))
1800
2083
 
 
2084
(defcustom org-tags-exclude-from-inheritance nil
 
2085
  "List of tags that should never be inherited.
 
2086
This is a way to exclude a few tags from inheritance.  For way to do
 
2087
the opposite, to actively allow inheritance for selected tags,
 
2088
see the variable `org-use-tag-inheritance'."
 
2089
  :group 'org-tags
 
2090
  :type '(repeat (string :tag "Tag")))
 
2091
 
1801
2092
(defun org-tag-inherit-p (tag)
1802
2093
  "Check if TAG is one that should be inherited."
1803
2094
  (cond
 
2095
   ((member tag org-tags-exclude-from-inheritance) nil)
1804
2096
   ((eq org-use-tag-inheritance t) t)
1805
2097
   ((not org-use-tag-inheritance) nil)
1806
2098
   ((stringp org-use-tag-inheritance)
1820
2112
 
1821
2113
As a special case, if the tag search is restricted to TODO items, the
1822
2114
value of this variable is ignored and sublevels are always checked, to
1823
 
make sure all corresponding TODO items find their way into the list."
 
2115
make sure all corresponding TODO items find their way into the list.
 
2116
 
 
2117
This variable is semi-obsolete and probably should always be true.  It
 
2118
is better to limit inheritance to certain tags using the variables
 
2119
`org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
1824
2120
  :group 'org-tags
1825
2121
  :type 'boolean)
1826
2122
 
1906
2202
(defcustom org-columns-modify-value-for-display-function nil
1907
2203
  "Function that modifies values for display in column view.
1908
2204
For example, it can be used to cut out a certain part from a time stamp.
1909
 
The function must take 2 argments:
 
2205
The function must take 2 arguments:
1910
2206
 
1911
 
column-title    The tite of the column (*not* the property name)
 
2207
column-title    The title of the column (*not* the property name)
1912
2208
value           The value that should be modified.
1913
2209
 
1914
2210
The function should return the value that should be displayed,
1931
2227
 
1932
2228
(defcustom org-global-properties nil
1933
2229
  "List of property/value pairs that can be inherited by any entry.
1934
 
You can set buffer-local values for this by adding lines like
 
2230
You can set buffer-local values for the same purpose in the variable
 
2231
`org-file-properties' this by adding lines like
1935
2232
 
1936
2233
#+PROPERTY: NAME VALUE"
1937
2234
  :group 'org-properties
2045
2342
(defcustom org-format-latex-options
2046
2343
  '(:foreground default :background default :scale 1.0
2047
2344
    :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
2048
 
    :matchers ("begin" "$" "$$" "\\(" "\\["))
 
2345
    :matchers ("begin" "$1" "$" "$$" "\\(" "\\["))
2049
2346
  "Options for creating images from LaTeX fragments.
2050
2347
This is a property list with the following properties:
2051
2348
:foreground  the foreground color for images embedded in Emacs, e.g. \"Black\".
2058
2355
:matchers    a list indicating which matchers should be used to
2059
2356
             find LaTeX fragments.  Valid members of this list are:
2060
2357
             \"begin\"  find environments
 
2358
             \"$1\"     find single characters surrounded by $.$
2061
2359
             \"$\"      find math expressions surrounded by $...$
2062
2360
             \"$$\"     find math expressions surrounded by $$....$$
2063
2361
             \"\\(\"     find math expressions surrounded by \\(...\\)
2149
2447
           (border (nth 2 e))
2150
2448
           (body (nth 3 e))
2151
2449
           (nl (nth 4 e))
2152
 
           (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
2153
2450
           (body1 (concat body "*?"))
2154
2451
           (markers (mapconcat 'car org-emphasis-alist ""))
2155
2452
           (vmarkers (mapconcat
2169
2466
                              (int-to-string nl) "\\}")))
2170
2467
      ;; Make the regexp
2171
2468
      (setq org-emph-re
2172
 
            (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
 
2469
            (concat "\\([" pre "]\\|^\\)"
2173
2470
                    "\\("
2174
2471
                    "\\([" markers "]\\)"
2175
2472
                    "\\("
2176
2473
                    "[^" border "]\\|"
2177
 
                    "[^" border (if (and nil stacked) markers) "]"
 
2474
                    "[^" border "]"
2178
2475
                    body1
2179
 
                    "[^" border (if (and nil stacked) markers) "]"
 
2476
                    "[^" border "]"
2180
2477
                    "\\)"
2181
2478
                    "\\3\\)"
2182
 
                    "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
 
2479
                    "\\([" post "]\\|$\\)"))
2183
2480
      (setq org-verbatim-re
2184
2481
            (concat "\\([" pre "]\\|^\\)"
2185
2482
                    "\\("
2223
2520
(defcustom org-emphasis-alist
2224
2521
  `(("*" bold "<b>" "</b>")
2225
2522
    ("/" italic "<i>" "</i>")
2226
 
    ("_" underline "<u>" "</u>")
 
2523
    ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
2227
2524
    ("=" org-code "<code>" "</code>" verbatim)
2228
 
    ("~" org-verbatim "" "" verbatim)
 
2525
    ("~" org-verbatim "<code>" "</code>" verbatim)
2229
2526
    ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
2230
2527
     "<del>" "</del>")
2231
2528
    )
2254
2551
  :tag "Org Completion"
2255
2552
  :group 'org)
2256
2553
 
 
2554
(defcustom org-completion-use-ido nil
 
2555
  "Non-nil means, use ido completion wherever possible.
 
2556
Note that `ido-mode' must be active for this variable to be relevant.
 
2557
If you decide to turn this variable on, you might well want to turn off
 
2558
`org-outline-path-complete-in-steps'."
 
2559
  :group 'org-completion
 
2560
  :type 'boolean)
 
2561
 
2257
2562
(defcustom org-completion-fallback-command 'hippie-expand
2258
2563
  "The expansion command called by \\[org-complete] in normal context.
2259
2564
Normal means, no org-mode-specific context."
2287
2592
(declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
2288
2593
(defvar iswitchb-temp-buflist)
2289
2594
(declare-function org-gnus-follow-link "org-gnus" (&optional group article))
 
2595
(defvar org-agenda-tags-todo-honor-ignore-options)
2290
2596
(declare-function org-agenda-skip "org-agenda" ())
2291
2597
(declare-function org-format-agenda-item "org-agenda"
2292
2598
                  (extra txt &optional category tags dotime noprefix remove-re))
2293
2599
(declare-function org-agenda-new-marker "org-agenda" (&optional pos))
2294
2600
(declare-function org-agenda-change-all-lines "org-agenda"
2295
 
                  (newhead hdmarker &optional fixface))
 
2601
                  (newhead hdmarker &optional fixface just-this))
2296
2602
(declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
2297
2603
(declare-function org-agenda-maybe-redo "org-agenda" ())
2298
2604
(declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
2299
2605
                  (beg end))
 
2606
(declare-function org-agenda-copy-local-variable "org-agenda" (var))
 
2607
(declare-function org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item
 
2608
                  "org-agenda" (&optional end))
 
2609
 
2300
2610
(declare-function parse-time-string "parse-time" (string))
2301
2611
(declare-function remember "remember" (&optional initial))
2302
2612
(declare-function remember-buffer-desc "remember" ())
2451
2761
  (org-autoload "org-agenda"
2452
2762
                '(org-agenda org-agenda-list org-search-view
2453
2763
   org-todo-list org-tags-view org-agenda-list-stuck-projects
2454
 
   org-diary org-agenda-to-appt)))
 
2764
   org-diary org-agenda-to-appt
 
2765
   org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))
2455
2766
 
2456
2767
;; Autoload org-remember
2457
2768
 
2465
2776
 
2466
2777
(declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
2467
2778
                  (beg end))
2468
 
(declare-function org-update-mode-line "org-clock" ())
 
2779
(declare-function org-clock-update-mode-line "org-clock" ())
2469
2780
(defvar org-clock-start-time)
2470
2781
(defvar org-clock-marker (make-marker)
2471
2782
  "Marker recording the last clock-in.")
2475
2786
   "org-clock"
2476
2787
   '(org-clock-in org-clock-out org-clock-cancel
2477
2788
                  org-clock-goto org-clock-sum org-clock-display
2478
 
                  org-remove-clock-overlays org-clock-report
 
2789
                  org-clock-remove-overlays org-clock-report
2479
2790
                  org-clocktable-shift org-dblock-write:clocktable
2480
2791
                  org-get-clocktable)))
2481
2792
 
2490
2801
      (let ((re (concat "[ \t]*" org-clock-string
2491
2802
                        " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
2492
2803
                        "\\([ \t]*=>.*\\)?\\)?"))
2493
 
            ts te h m s)
 
2804
            ts te h m s neg)
2494
2805
        (cond
2495
2806
         ((not (looking-at re))
2496
2807
          nil)
2500
2811
                     (<= org-clock-marker (point-at-eol)))
2501
2812
            ;; The clock is running here
2502
2813
            (setq org-clock-start-time
2503
 
                  (apply 'encode-time 
 
2814
                  (apply 'encode-time
2504
2815
                         (org-parse-time-string (match-string 1))))
2505
 
            (org-update-mode-line)))
 
2816
            (org-clock-update-mode-line)))
2506
2817
         (t
2507
2818
          (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
2508
2819
          (end-of-line 1)
2512
2823
                      (apply 'encode-time (org-parse-time-string te)))
2513
2824
                     (time-to-seconds
2514
2825
                      (apply 'encode-time (org-parse-time-string ts))))
 
2826
                neg (< s 0)
 
2827
                s (abs s)
2515
2828
                h (floor (/ s 3600))
2516
2829
                s (- s (* 3600 h))
2517
2830
                m (floor (/ s 60))
2518
2831
                s (- s (* 60 s)))
2519
 
          (insert " => " (format "%2d:%02d" h m))
 
2832
          (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
2520
2833
          t))))))
2521
2834
 
2522
2835
(defun org-check-running-clock ()
2534
2847
  (when (org-match-line "#\\+BEGIN: clocktable\\>")
2535
2848
    (org-clocktable-shift dir n)))
2536
2849
 
 
2850
;; Autoload org-timer.el
 
2851
 
 
2852
;(declare-function org-timer "org-timer")
 
2853
 
 
2854
(eval-and-compile
 
2855
  (org-autoload
 
2856
   "org-timer"
 
2857
   '(org-timer-start org-timer org-timer-item
 
2858
                     org-timer-change-times-in-region)))
 
2859
 
 
2860
 
2537
2861
;; Autoload archiving code
2538
2862
;; The stuff that is needed for cycling and tags has to be defined here.
2539
2863
 
2545
2869
(defcustom org-archive-location "%s_archive::"
2546
2870
  "The location where subtrees should be archived.
2547
2871
 
2548
 
Otherwise, the value of this variable is a string, consisting of two
2549
 
parts, separated by a double-colon.
2550
 
 
2551
 
The first part is a file name - when omitted, archiving happens in the same
2552
 
file.  %s will be replaced by the current file name (without directory part).
2553
 
Archiving to a different file is useful to keep archived entries from
2554
 
contributing to the Org-mode Agenda.
2555
 
 
2556
 
The part after the double colon is a headline.  The archived entries will be
2557
 
filed under that headline.  When omitted, the subtrees are simply filed away
2558
 
at the end of the file, as top-level entries.
 
2872
The value of this variable is a string, consisting of two parts,
 
2873
separated by a double-colon.  The first part is a filename and
 
2874
the second part is a headline.
 
2875
 
 
2876
When the filename is omitted, archiving happens in the same file.
 
2877
%s in the filename will be replaced by the current file
 
2878
name (without the directory part).  Archiving to a different file
 
2879
is useful to keep archived entries from contributing to the
 
2880
Org-mode Agenda.
 
2881
 
 
2882
The archived entries will be filed as subtrees of the specified
 
2883
headline.  When the headline is omitted, the subtrees are simply
 
2884
filed away at the end of the file, as top-level entries.  Also in
 
2885
the heading you can use %s to represent the file name, this can be
 
2886
useful when using the same archive for a number of different files.
2559
2887
 
2560
2888
Here are a few examples:
2561
2889
\"%s_archive::\"
2569
2897
\"~/org/archive.org::\"
2570
2898
        Archive in file ~/org/archive.org (absolute path), as top-level trees.
2571
2899
 
 
2900
\"~/org/archive.org::From %s\"
 
2901
        Archive in file ~/org/archive.org (absolute path), und headlines
 
2902
        \"From FILENAME\" where file name is the current file name.
 
2903
 
2572
2904
\"basement::** Finished Tasks\"
2573
2905
        Archive in file ./basement (relative path), as level 3 trees
2574
2906
        below the level 2 heading \"** Finished Tasks\".
2670
3002
 
2671
3003
;; Autoload ID code
2672
3004
 
 
3005
(declare-function org-id-store-link "org-id")
2673
3006
(org-autoload "org-id"
2674
 
 '(org-id-get-create org-id-new org-id-copy org-id-get 
2675
 
   org-id-get-with-outline-path-completion 
 
3007
 '(org-id-get-create org-id-new org-id-copy org-id-get
 
3008
   org-id-get-with-outline-path-completion
2676
3009
   org-id-get-with-outline-drilling
2677
 
   org-id-goto org-id-find))
 
3010
   org-id-goto org-id-find org-id-store-link))
2678
3011
 
2679
3012
;;; Variables for pre-computed regular expressions, all buffer local
2680
3013
 
2804
3137
    ("logrepeat" org-log-repeat state)
2805
3138
    ("lognoterepeat" org-log-repeat note)
2806
3139
    ("nologrepeat" org-log-repeat nil)
 
3140
    ("fninline" org-footnote-define-inline t)
 
3141
    ("nofninline" org-footnote-define-inline nil)
 
3142
    ("fnlocal" org-footnote-section nil)
 
3143
    ("fnauto" org-footnote-auto-label t)
 
3144
    ("fnprompt" org-footnote-auto-label nil)
 
3145
    ("fnconfirm" org-footnote-auto-label confirm)
 
3146
    ("fnplain" org-footnote-auto-label plain)
2807
3147
    ("constcgs" constants-unit-system cgs)
2808
3148
    ("constSI" constants-unit-system SI))
2809
3149
  "Variable associated with STARTUP options for org-mode.
3017
3357
                    (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3018
3358
                    "\\)\\>\\)?[ \t]*\\(.*\\)")
3019
3359
            org-complex-heading-regexp
3020
 
            (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
 
3360
            (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
3021
3361
                    (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
3022
3362
                    "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
3023
3363
                    "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
3160
3500
;;;; Define the Org-mode
3161
3501
 
3162
3502
(if (and (not (keymapp outline-mode-map)) (featurep 'allout))
3163
 
    (error "Conflict with outdated version of allout.el.  Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
 
3503
    (error "Conflict with outdated version of allout.el.  Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
3164
3504
 
3165
3505
 
3166
3506
;; We use a before-change function to check if a table might need
3173
3513
  "Every change indicates that a table might need an update."
3174
3514
  (setq org-table-may-need-update t))
3175
3515
(defvar org-mode-map)
3176
 
(defvar org-mode-hook nil)
 
3516
(defvar org-mode-hook nil
 
3517
  "Mode hook for Org-mode, run after the mode was turned on.")
3177
3518
(defvar org-inhibit-startup nil)        ; Dynamically-scoped param.
3178
3519
(defvar org-agenda-keep-modes nil)      ; Dynamically-scoped param.
3179
3520
(defvar org-table-buffer-is-an nil)
3236
3577
               (if (stringp org-ellipsis) org-ellipsis "..."))))
3237
3578
    (setq buffer-display-table org-display-table))
3238
3579
  (org-set-regexps-and-options)
 
3580
  (when (and org-tag-faces (not org-tags-special-faces-re))
 
3581
    ;; tag faces set outside customize.... force initialization.
 
3582
    (org-set-tag-faces 'org-tag-faces org-tag-faces))
3239
3583
  ;; Calc embedded
3240
3584
  (org-set-local 'calc-embedded-open-mode "# ")
3241
3585
  (modify-syntax-entry ?# "<")
3253
3597
  (org-set-autofill-regexps)
3254
3598
  (setq indent-line-function 'org-indent-line-function)
3255
3599
  (org-update-radio-target-regexp)
 
3600
  ;; Make sure dependence stuff works reliably, even for users who set it
 
3601
  ;; too late :-(
 
3602
  (if org-enforce-todo-dependencies
 
3603
      (add-hook 'org-blocker-hook
 
3604
                'org-block-todo-from-children-or-siblings)
 
3605
    (remove-hook 'org-blocker-hook
 
3606
                 'org-block-todo-from-children-or-siblings))
 
3607
  (if org-enforce-todo-checkbox-dependencies
 
3608
      (add-hook 'org-blocker-hook
 
3609
                'org-block-todo-from-checkboxes)
 
3610
    (remove-hook 'org-blocker-hook
 
3611
                 'org-block-todo-from-checkboxes))
3256
3612
 
3257
3613
  ;; Comment characters
3258
3614
;  (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
3330
3686
   "Matches a link with spaces, optional angular brackets around it.")
3331
3687
(defvar org-link-re-with-space2 nil
3332
3688
   "Matches a link with spaces, optional angular brackets around it.")
 
3689
(defvar org-link-re-with-space3 nil
 
3690
   "Matches a link with spaces, only for internal part in bracket links.")
3333
3691
(defvar org-angle-link-re nil
3334
3692
   "Matches link with angular brackets, spaces are allowed.")
3335
3693
(defvar org-plain-link-re nil
3344
3702
3: path
3345
3703
4: [desc]
3346
3704
5: desc")
 
3705
(defvar org-bracket-link-analytic-regexp++ nil
 
3706
  "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
3347
3707
(defvar org-any-link-re nil
3348
3708
  "Regular expression matching any link.")
3349
3709
 
3363
3723
        (concat
3364
3724
         "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3365
3725
         "\\([^" org-non-link-chars " ]"
3366
 
         "[^]\t\n\r]*"
 
3726
         "[^\t\n\r]*"
3367
3727
         "[^" org-non-link-chars " ]\\)>?")
 
3728
        org-link-re-with-space3
 
3729
        (concat
 
3730
         "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
 
3731
         "\\([^" org-non-link-chars " ]"
 
3732
         "[^\t\n\r]*\\)")
3368
3733
        org-angle-link-re
3369
3734
        (concat
3370
3735
         "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
3385
3750
         "\\]"
3386
3751
         "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
3387
3752
         "\\]")
 
3753
        org-bracket-link-analytic-regexp++
 
3754
        (concat
 
3755
         "\\[\\["
 
3756
         "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
 
3757
         "\\([^]]+\\)"
 
3758
         "\\]"
 
3759
         "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
 
3760
         "\\]")
3388
3761
        org-any-link-re
3389
3762
        (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
3390
3763
                org-angle-link-re "\\)\\|\\("
3469
3842
      (push (cons c (string-to-char (car e))) det)
3470
3843
      (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
3471
3844
                                          (substring tag 1)))))
 
3845
    (setq det (nreverse det))
3472
3846
    (unless char
3473
3847
      (message "%s" (concat "Emphasis marker or tag:" prompt))
3474
3848
      (setq char (read-char-exclusive)))
3532
3906
                                   ))
3533
3907
        t)))
3534
3908
 
 
3909
(defun org-activate-footnote-links (limit)
 
3910
  "Run through the buffer and add overlays to links."
 
3911
  (if (re-search-forward "\\(^\\|[^][]\\)\\(\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)\\)" 
 
3912
                         limit t)
 
3913
      (progn
 
3914
        (add-text-properties (match-beginning 2) (match-end 2)
 
3915
                             (list 'mouse-face 'highlight
 
3916
                                   'rear-nonsticky org-nonsticky-props
 
3917
                                   'keymap org-mouse-map
 
3918
                                   'help-echo
 
3919
                                   (if (= (point-at-bol) (match-beginning 2))
 
3920
                                       "Footnote definition"
 
3921
                                     "Footnote reference")
 
3922
                                   ))
 
3923
        t)))
 
3924
 
3535
3925
(defun org-activate-bracket-links (limit)
3536
3926
  "Run through the buffer and add overlays to bracketed links."
3537
3927
  (if (re-search-forward org-bracket-link-regexp limit t)
3577
3967
 
3578
3968
(defvar org-target-link-regexp nil
3579
3969
  "Regular expression matching radio targets in plain text.")
 
3970
(make-variable-buffer-local 'org-target-link-regexp)
3580
3971
(defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
3581
3972
  "Regular expression matching a link target.")
3582
3973
(defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
3785
4176
           (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
3786
4177
           (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
3787
4178
           (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
 
4179
           (if (memq 'footnote lk) '(org-activate-footnote-links
 
4180
                                     (2 'org-footnote t)))
3788
4181
           '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
3789
4182
           '(org-hide-wide-columns (0 nil append))
3790
4183
           ;; TODO lines
3799
4192
             nil)
3800
4193
           ;; Priorities
3801
4194
           (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
 
4195
           ;; Tags
 
4196
           '(org-font-lock-add-tag-faces)
3802
4197
           ;; Special keywords
3803
4198
           (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
3804
4199
           (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
3841
4236
(defvar org-l nil)
3842
4237
(defvar org-f nil)
3843
4238
(defun org-get-level-face (n)
3844
 
  "Get the right face for match N in font-lock matching of healdines."
 
4239
  "Get the right face for match N in font-lock matching of headlines."
3845
4240
  (setq org-l (- (match-end 2) (match-beginning 1) 1))
3846
4241
  (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
3847
4242
  (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
3858
4253
      (and (member kwd org-done-keywords) 'org-done)
3859
4254
      'org-todo))
3860
4255
 
 
4256
(defun org-font-lock-add-tag-faces (limit)
 
4257
  "Add the special tag faces."
 
4258
  (when (and org-tag-faces org-tags-special-faces-re)
 
4259
    (while (re-search-forward org-tags-special-faces-re limit t)
 
4260
      (add-text-properties (match-beginning 1) (match-end 1)
 
4261
                           (list 'face (org-get-tag-face 1)
 
4262
                                 'font-lock-fontified t))
 
4263
      (backward-char 1))))
 
4264
 
 
4265
(defun org-get-tag-face (kwd)
 
4266
  "Get the right face for a TODO keyword KWD.
 
4267
If KWD is a number, get the corresponding match group."
 
4268
  (if (numberp kwd) (setq kwd (match-string kwd)))
 
4269
  (or (cdr (assoc kwd org-tag-faces))
 
4270
      'org-tag))
 
4271
 
3861
4272
(defun org-unfontify-region (beg end &optional maybe_loudly)
3862
4273
  "Remove fontification and activation overlays from links."
3863
4274
  (font-lock-default-unfontify-region beg end)
4110
4521
(defun org-set-visibility-according-to-property (&optional no-cleanup)
4111
4522
  "Switch subtree visibilities according to :VISIBILITY: property."
4112
4523
  (interactive)
4113
 
  (let (state)
 
4524
  (let (org-show-entry-below state)
4114
4525
    (save-excursion
4115
4526
      (goto-char (point-min))
4116
4527
      (while (re-search-forward
4140
4551
        (org-cycle-show-empty-lines 'all)))))
4141
4552
 
4142
4553
(defun org-overview ()
4143
 
  "Switch to overview mode, shoing only top-level headlines.
 
4554
  "Switch to overview mode, showing only top-level headlines.
4144
4555
Really, this shows all headlines with level equal or greater than the level
4145
4556
of the first headline in the buffer.  This is important, because if the
4146
4557
first headline is not level one, then (hide-sublevels 1) gives confusing
4188
4599
     ((eq state 'subtree)  (or (org-subtree-end-visible-p) (recenter 1))))))
4189
4600
 
4190
4601
(defun org-compact-display-after-subtree-move ()
4191
 
  (let (beg end)
4192
 
    (save-excursion
4193
 
      (if (org-up-heading-safe)
4194
 
          (progn
4195
 
            (hide-subtree)
4196
 
            (show-entry)
4197
 
            (show-children)
4198
 
            (org-cycle-show-empty-lines 'children)
4199
 
            (org-cycle-hide-drawers 'children))
4200
 
        (org-overview)))))
 
4602
  "Show a compacter version of the tree of the entry's parent."
 
4603
  (save-excursion
 
4604
    (if (org-up-heading-safe)
 
4605
        (progn
 
4606
          (hide-subtree)
 
4607
          (show-entry)
 
4608
          (show-children)
 
4609
          (org-cycle-show-empty-lines 'children)
 
4610
          (org-cycle-hide-drawers 'children))
 
4611
      (org-overview))))
4201
4612
 
4202
4613
(defun org-cycle-show-empty-lines (state)
4203
4614
  "Show empty lines above all visible headlines.
4204
4615
The region to be covered depends on STATE when called through
4205
4616
`org-cycle-hook'.  Lisp program can use t for STATE to get the
4206
4617
entire buffer covered.  Note that an empty line is only shown if there
4207
 
are at least `org-cycle-separator-lines' empty lines before the headeline."
 
4618
are at least `org-cycle-separator-lines' empty lines before the headline."
4208
4619
  (when (> org-cycle-separator-lines 0)
4209
4620
    (save-excursion
4210
4621
      (let* ((n org-cycle-separator-lines)
4341
4752
the location selected in the indirect buffer and expose the
4342
4753
the headline hierarchy above."
4343
4754
  (interactive "P")
4344
 
  (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
 
4755
  (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
4345
4756
         (org-refile-use-outline-path t)
4346
4757
         (interface
4347
4758
          (if (not alternative-interface)
4385
4796
           (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
4386
4797
        (with-output-to-temp-buffer "*Help*"
4387
4798
          (princ help))
4388
 
        (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
 
4799
        (org-fit-window-to-buffer (get-buffer-window "*Help*"))
4389
4800
        (setq buffer-read-only nil)
4390
4801
        (let ((org-startup-truncated t)
4391
4802
              (org-startup-folded nil)
4401
4812
              (goto-char org-goto-start-pos)
4402
4813
              (and (org-invisible-p) (org-show-context)))
4403
4814
          (goto-char (point-min)))
4404
 
        (org-beginning-of-line)
 
4815
        (let (org-special-ctrl-a/e) (org-beginning-of-line))
4405
4816
        (message "Select location and press RET")
4406
4817
        (use-local-map org-goto-map)
4407
4818
        (recursive-edit)
4556
4967
 
4557
4968
;;; Inserting headlines
4558
4969
 
 
4970
(defun org-previous-line-empty-p ()
 
4971
  (save-excursion
 
4972
    (and (not (bobp))
 
4973
         (or (beginning-of-line 0) t)
 
4974
         (save-match-data
 
4975
           (looking-at "[ \t]*$")))))
 
4976
    
4559
4977
(defun org-insert-heading (&optional force-heading)
4560
4978
  "Insert a new heading or item with same depth at point.
4561
4979
If point is in a plain list and FORCE-HEADING is nil, create a new list item.
4562
4980
If point is at the beginning of a headline, insert a sibling before the
4563
4981
current headline.  If point is not at the beginning, do not split the line,
4564
 
but create the new hedline after the current line."
 
4982
but create the new headline after the current line."
4565
4983
  (interactive "P")
4566
4984
  (if (= (buffer-size) 0)
4567
4985
      (insert "\n* ")
4568
4986
    (when (or force-heading (not (org-insert-item)))
4569
 
      (let* ((head (save-excursion
 
4987
      (let* ((empty-line-p nil)
 
4988
             (head (save-excursion
4570
4989
                     (condition-case nil
4571
4990
                         (progn
4572
4991
                           (org-back-to-heading)
 
4992
                           (setq empty-line-p (org-previous-line-empty-p))
4573
4993
                           (match-string 0))
4574
4994
                       (error "*"))))
4575
 
             (blank (cdr (assq 'heading org-blank-before-new-entry)))
4576
 
             pos)
 
4995
             (blank-a (cdr (assq 'heading org-blank-before-new-entry)))
 
4996
             (blank (if (eq blank-a 'auto) empty-line-p blank-a))
 
4997
             pos hide-previous previous-pos)
4577
4998
        (cond
4578
4999
         ((and (org-on-heading-p) (bolp)
4579
5000
               (or (bobp)
4587
5008
          ;; insert right here
4588
5009
          nil)
4589
5010
         (t
4590
 
          ;; in the middle of the line
4591
 
          (org-show-entry)
 
5011
          ;; somewhere in the line
 
5012
          (save-excursion
 
5013
            (setq previous-pos (point-at-bol))
 
5014
            (end-of-line)
 
5015
            (setq hide-previous (org-invisible-p)))
 
5016
          (and org-insert-heading-respect-content (org-show-subtree))
4592
5017
          (let ((split
4593
 
                 (org-get-alist-option org-M-RET-may-split-line 'headline))
 
5018
                 (and (org-get-alist-option org-M-RET-may-split-line 'headline)
 
5019
                      (save-excursion
 
5020
                        (let ((p (point)))
 
5021
                          (goto-char (point-at-bol))
 
5022
                          (and (looking-at org-complex-heading-regexp)
 
5023
                               (> p (match-beginning 4)))))))
4594
5024
                tags pos)
4595
5025
            (cond
4596
5026
             (org-insert-heading-respect-content
4597
5027
              (org-end-of-subtree nil t)
 
5028
              (or (bolp) (newline))
 
5029
              (or (org-previous-line-empty-p)
 
5030
                  (and blank (newline)))
4598
5031
              (open-line 1))
4599
5032
             ((org-on-heading-p)
 
5033
              (when hide-previous
 
5034
                (show-children)
 
5035
                (org-show-entry))
4600
5036
              (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
4601
5037
              (setq tags (and (match-end 2) (match-string 2)))
4602
5038
              (and (match-end 1)
4618
5054
        (setq pos (point))
4619
5055
        (end-of-line 1)
4620
5056
        (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
 
5057
        (when (and org-insert-heading-respect-content hide-previous)
 
5058
          (save-excursion
 
5059
            (goto-char previous-pos)
 
5060
            (hide-subtree)))
4621
5061
        (run-hooks 'org-insert-heading-hook)))))
4622
5062
 
4623
5063
(defun org-get-heading (&optional no-tags)
4630
5070
           "\\*+[ \t]+\\([^\r\n]*\\)"))
4631
5071
        (match-string 1) "")))
4632
5072
 
 
5073
(defun org-heading-components ()
 
5074
  "Return the components of the current heading.
 
5075
This is a list with the following elements:
 
5076
- the level as an integer
 
5077
- the reduced level, different if `org-odd-levels-only' is set.
 
5078
- the TODO keyword, or nil
 
5079
- the priority character, like ?A, or nil if no priority is given
 
5080
- the headline text itself, or the tags string if no headline text
 
5081
- the tags string, or nil."
 
5082
  (save-excursion
 
5083
    (org-back-to-heading t)
 
5084
    (if (looking-at org-complex-heading-regexp)
 
5085
        (list (length (match-string 1))
 
5086
              (org-reduced-level (length (match-string 1)))
 
5087
              (org-match-string-no-properties 2)
 
5088
              (and (match-end 3) (aref (match-string 3) 2))
 
5089
              (org-match-string-no-properties 4)
 
5090
              (org-match-string-no-properties 5)))))
 
5091
 
4633
5092
(defun org-insert-heading-after-current ()
4634
5093
  "Insert a new heading with same level as current, after current subtree."
4635
5094
  (interactive)
4641
5100
(defun org-insert-heading-respect-content ()
4642
5101
  (interactive)
4643
5102
  (let ((org-insert-heading-respect-content t))
4644
 
    (call-interactively 'org-insert-heading)))
 
5103
    (org-insert-heading t)))
4645
5104
 
4646
 
(defun org-insert-todo-heading-respect-content ()
4647
 
  (interactive)
 
5105
(defun org-insert-todo-heading-respect-content (&optional force-state)
 
5106
  (interactive "P")
4648
5107
  (let ((org-insert-heading-respect-content t))
4649
 
    (call-interactively 'org-insert-todo-todo-heading)))
 
5108
    (org-insert-todo-heading force-state t)))
4650
5109
 
4651
 
(defun org-insert-todo-heading (arg)
 
5110
(defun org-insert-todo-heading (arg &optional force-heading)
4652
5111
  "Insert a new heading with the same level and TODO state as current heading.
4653
5112
If the heading has no TODO state, or if the state is DONE, use the first
4654
5113
state (TODO by default).  Also with prefix arg, force first state."
4655
5114
  (interactive "P")
4656
 
  (when (not (org-insert-item 'checkbox))
4657
 
    (org-insert-heading)
 
5115
  (when (or force-heading (not (org-insert-item 'checkbox)))
 
5116
    (org-insert-heading force-heading)
4658
5117
    (save-excursion
4659
5118
      (org-back-to-heading)
4660
5119
      (outline-previous-heading)
4738
5197
            ((equal (char-after) ?\ ) (forward-char 1))))))
4739
5198
 
4740
5199
(defun org-reduced-level (l)
 
5200
  "Compute the effective level of a heading.
 
5201
This takes into account the setting of `org-odd-levels-only'."
4741
5202
  (if org-odd-levels-only (1+ (floor (/ l 2))) l))
4742
5203
 
4743
5204
(defun org-get-valid-level (level &optional change)
4830
5291
          (goto-char (match-end 0))
4831
5292
          (setq col (current-column))
4832
5293
          (if (< diff 0) (replace-match ""))
4833
 
          (indent-to (+ diff col))))
 
5294
          (org-indent-to-column (+ diff col))))
4834
5295
      (move-marker end nil))))
4835
5296
 
4836
5297
(defun org-convert-to-odd-levels ()
4927
5388
    (setq txt (buffer-substring beg end))
4928
5389
    (org-save-markers-in-region beg end)
4929
5390
    (delete-region beg end)
4930
 
    (outline-flag-region (1- beg) beg nil)
4931
 
    (outline-flag-region (1- (point)) (point) nil)
 
5391
    (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
 
5392
    (or (bobp) (outline-flag-region (1- (point)) (point) nil))
4932
5393
    (let ((bbb (point)))
4933
5394
      (insert-before-markers txt)
4934
5395
      (org-reinstall-markers-in-region bbb)
5006
5467
               (if cut "Cut" "Copied")
5007
5468
               (length org-subtree-clip)))))
5008
5469
 
5009
 
(defun org-paste-subtree (&optional level tree)
 
5470
(defun org-paste-subtree (&optional level tree for-yank)
5010
5471
  "Paste the clipboard as a subtree, with modification of headline level.
5011
5472
The entire subtree is promoted or demoted in order to match a new headline
5012
 
level.  By default, the new level is derived from the visible headings
 
5473
level.
 
5474
 
 
5475
If the cursor is at the beginning of a headline, the same level as
 
5476
that headline is used to paste the tree
 
5477
 
 
5478
If not, the new level is derived from the *visible* headings
5013
5479
before and after the insertion point, and taken to be the inferior headline
5014
5480
level of the two.  So if the previous visible heading is level 3 and the
5015
5481
next is level 4 (or vice versa), level 4 will be used for insertion.
5020
5486
argument, or by inserting the heading marker by hand.  For example, if the
5021
5487
cursor is after \"*****\", then the tree will be shifted to level 5.
5022
5488
 
5023
 
If you want to insert the tree as is, just use \\[yank].
 
5489
If optional TREE is given, use this text instead of the kill ring.
5024
5490
 
5025
 
If optional TREE is given, use this text instead of the kill ring."
 
5491
When FOR-YANK is set, this is called by `org-yank'.  In this case, do not
 
5492
move back over whitespace before inserting, and move point to the end of
 
5493
the inserted text when done."
5026
5494
  (interactive "P")
5027
5495
  (unless (org-kill-is-subtree-p tree)
5028
5496
    (error "%s"
5038
5506
                        (- (match-end 0) (match-beginning 0) 1)
5039
5507
                      -1))
5040
5508
         (force-level (cond (level (prefix-numeric-value level))
5041
 
                            ((string-match
5042
 
                              ^re_ (buffer-substring (point-at-bol) (point)))
 
5509
                            ((and (looking-at "[ \t]*$")
 
5510
                                  (string-match
 
5511
                                   ^re_ (buffer-substring
 
5512
                                         (point-at-bol) (point))))
5043
5513
                             (- (match-end 1) (match-beginning 1)))
 
5514
                            ((and (bolp)
 
5515
                                  (looking-at org-outline-regexp))
 
5516
                             (- (match-end 0) (point) 1))
5044
5517
                            (t nil)))
5045
5518
         (previous-level (save-excursion
5046
5519
                           (condition-case nil
5068
5541
         (delta (if (> shift 0) -1 1))
5069
5542
         (func (if (> shift 0) 'org-demote 'org-promote))
5070
5543
         (org-odd-levels-only nil)
5071
 
         beg end)
 
5544
         beg end newend)
5072
5545
    ;; Remove the forced level indicator
5073
5546
    (if force-level
5074
5547
        (delete-region (point-at-bol) (point)))
5075
5548
    ;; Paste
5076
5549
    (beginning-of-line 1)
5077
 
    (org-back-over-empty-lines)
 
5550
    (unless for-yank (org-back-over-empty-lines))
5078
5551
    (setq beg (point))
 
5552
    (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
5079
5553
    (insert-before-markers txt)
5080
5554
    (unless (string-match "\n\\'" txt) (insert "\n"))
 
5555
    (setq newend (point))
5081
5556
    (org-reinstall-markers-in-region beg)
5082
5557
    (setq end (point))
5083
5558
    (goto-char beg)
5092
5567
        (while (not (= shift 0))
5093
5568
          (org-map-region func (point-min) (point-max))
5094
5569
          (setq shift (+ delta shift)))
5095
 
        (goto-char (point-min))))
5096
 
    (when (interactive-p)
 
5570
        (goto-char (point-min))
 
5571
        (setq newend (point-max))))
 
5572
    (when (or (interactive-p) for-yank)
5097
5573
      (message "Clipboard pasted as level %d subtree" new-level))
5098
 
    (if (and kill-ring
 
5574
    (if (and (not for-yank) ; in this case, org-yank will decide about folding
 
5575
             kill-ring
5099
5576
             (eq org-subtree-clip (current-kill 0))
5100
5577
             org-subtree-clip-folded)
5101
5578
        ;; The tree was folded before it was killed/copied
5102
 
        (hide-subtree))))
 
5579
        (hide-subtree))
 
5580
    (and for-yank (goto-char newend))))
5103
5581
 
5104
5582
(defun org-kill-is-subtree-p (&optional txt)
5105
5583
  "Check if the current kill is an outline subtree, or a set of trees.
5189
5667
 
5190
5668
(defvar org-priority-regexp) ; defined later in the file
5191
5669
 
5192
 
(defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
 
5670
(defun org-sort-entries-or-items
 
5671
  (&optional with-case sorting-type getkey-func compare-func property)
5193
5672
  "Sort entries on a certain level of an outline tree.
5194
5673
If there is an active region, the entries in the region are sorted.
5195
5674
Else, if the cursor is before the first entry, sort the top-level items.
5268
5747
 
5269
5748
      (and (= (downcase sorting-type) ?f)
5270
5749
           (setq getkey-func
5271
 
                 (completing-read "Sort using function: "
 
5750
                 (org-ido-completing-read "Sort using function: "
5272
5751
                                  obarray 'fboundp t nil nil))
5273
5752
           (setq getkey-func (intern getkey-func)))
5274
5753
 
5275
5754
      (and (= (downcase sorting-type) ?r)
5276
5755
           (setq property
5277
 
                 (completing-read "Property: "
 
5756
                 (org-ido-completing-read "Property: "
5278
5757
                                  (mapcar 'list (org-buffer-property-keys t))
5279
5758
                                  nil t))))
5280
5759
 
5369
5848
         (cond
5370
5849
          ((= dcst ?a) 'string<)
5371
5850
          ((= dcst ?t) 'time-less-p)
 
5851
          ((= dcst ?f) compare-func)
5372
5852
          (t nil)))))
5373
5853
    (message "Sorting entries...done")))
5374
5854
 
5435
5915
              "Edit, then exit with C-c ' (C-c and single quote)"))
5436
5916
        (info (org-edit-src-find-region-and-lang))
5437
5917
        (org-mode-p (eq major-mode 'org-mode))
5438
 
        beg end lang lang-f single)
 
5918
        beg end lang lang-f single lfmt)
5439
5919
    (if (not info)
5440
5920
        nil
5441
5921
      (setq beg (nth 0 info)
5442
5922
            end (nth 1 info)
5443
5923
            lang (nth 2 info)
5444
5924
            single (nth 3 info)
 
5925
            lfmt (nth 4 info)
5445
5926
            lang-f (intern (concat lang "-mode")))
5446
5927
      (unless (functionp lang-f)
5447
5928
        (error "No such language mode: %s" lang-f))
5457
5938
        (funcall lang-f))
5458
5939
      (set (make-local-variable 'org-edit-src-force-single-line) single)
5459
5940
      (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
 
5941
      (when lfmt
 
5942
        (set (make-local-variable 'org-coderef-label-format) lfmt))
5460
5943
      (when org-mode-p
5461
5944
        (goto-char (point-min))
5462
5945
        (while (re-search-forward "^," nil t)
5469
5952
 
5470
5953
(defun org-edit-fixed-width-region ()
5471
5954
  "Edit the fixed-width ascii drawing at point.
5472
 
This must be a region where each line starts with ca colon followed by
 
5955
This must be a region where each line starts with a colon followed by
5473
5956
a space character.
5474
5957
An indirect buffer is created, and that buffer is then narrowed to the
5475
5958
example at point and switched to artist-mode.  When done,
5480
5963
        (msg (substitute-command-keys
5481
5964
              "Edit, then exit with C-c ' (C-c and single quote)"))
5482
5965
        (org-mode-p (eq major-mode 'org-mode))
5483
 
        beg end lang lang-f)
 
5966
        beg end)
5484
5967
    (beginning-of-line 1)
5485
5968
    (if (looking-at "[ \t]*[^:\n \t]")
5486
5969
        nil
5487
 
      (if (looking-at "[ \t]*\\(\n\\|\\'\\)]")
5488
 
          (setq beg (point) end (match-end 0))
 
5970
      (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
 
5971
          (setq beg (point) end beg)
5489
5972
        (save-excursion
5490
5973
          (if (re-search-backward "^[ \t]*[^:]" nil 'move)
5491
5974
              (setq beg (point-at-bol 2))
5494
5977
          (if (re-search-forward "^[ \t]*[^:]" nil 'move)
5495
5978
              (setq end (1- (match-beginning 0)))
5496
5979
            (setq end (point))))
5497
 
        (goto-line line)
5498
 
        (if (get-buffer "*Org Edit Picture*")
5499
 
            (kill-buffer "*Org Edit Picture*"))
5500
 
        (switch-to-buffer (make-indirect-buffer (current-buffer)
5501
 
                                                "*Org Edit Picture*"))
5502
 
        (narrow-to-region beg end)
5503
 
        (remove-text-properties beg end '(display nil invisible nil
5504
 
                                                  intangible nil))
5505
 
        (when (fboundp 'font-lock-unfontify-region)
5506
 
          (font-lock-unfontify-region (point-min) (point-max)))
5507
 
        (cond
5508
 
         ((eq org-edit-fixed-width-region-mode 'artist-mode)
5509
 
          (fundamental-mode)
5510
 
          (artist-mode 1))
5511
 
         (t (funcall org-edit-fixed-width-region-mode)))
5512
 
        (set (make-local-variable 'org-edit-src-force-single-line) nil)
5513
 
        (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
5514
 
        (set (make-local-variable 'org-edit-src-picture) t)
5515
 
        (goto-char (point-min))
5516
 
        (while (re-search-forward "^[ \t]*: " nil t)
5517
 
          (replace-match ""))
5518
 
        (goto-line line)
5519
 
        (org-exit-edit-mode)
5520
 
        (org-set-local 'header-line-format msg)
5521
 
        (message "%s" msg)
5522
 
        t))))
 
5980
        (goto-line line))
 
5981
      (if (get-buffer "*Org Edit Picture*")
 
5982
          (kill-buffer "*Org Edit Picture*"))
 
5983
      (switch-to-buffer (make-indirect-buffer (current-buffer)
 
5984
                                              "*Org Edit Picture*"))
 
5985
      (narrow-to-region beg end)
 
5986
      (remove-text-properties beg end '(display nil invisible nil
 
5987
                                                intangible nil))
 
5988
      (when (fboundp 'font-lock-unfontify-region)
 
5989
        (font-lock-unfontify-region (point-min) (point-max)))
 
5990
      (cond
 
5991
       ((eq org-edit-fixed-width-region-mode 'artist-mode)
 
5992
        (fundamental-mode)
 
5993
        (artist-mode 1))
 
5994
       (t (funcall org-edit-fixed-width-region-mode)))
 
5995
      (set (make-local-variable 'org-edit-src-force-single-line) nil)
 
5996
      (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
 
5997
      (set (make-local-variable 'org-edit-src-picture) t)
 
5998
      (goto-char (point-min))
 
5999
      (while (re-search-forward "^[ \t]*: ?" nil t)
 
6000
        (replace-match ""))
 
6001
      (goto-line line)
 
6002
      (org-exit-edit-mode)
 
6003
      (org-set-local 'header-line-format msg)
 
6004
      (message "%s" msg)
 
6005
      t)))
5523
6006
 
5524
6007
 
5525
6008
(defun org-edit-src-find-region-and-lang ()
5547
6030
            ("^#\\+ascii:" "\n" "ascii" single-line)
5548
6031
            )))
5549
6032
        (pos (point))
5550
 
        re re1 re2 single beg end lang)
 
6033
        re1 re2 single beg end lang lfmt match-re1)
5551
6034
    (catch 'exit
5552
6035
      (while (setq entry (pop re-list))
5553
6036
        (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
5556
6039
          (if (or (looking-at re1)
5557
6040
                  (re-search-backward re1 nil t))
5558
6041
              (progn
5559
 
                (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
 
6042
                (setq match-re1 (match-string 0))
 
6043
                (setq beg (match-end 0)
 
6044
                      lang (org-edit-src-get-lang lang)
 
6045
                      lfmt (org-edit-src-get-label-format match-re1))
5560
6046
                (if (and (re-search-forward re2 nil t)
5561
6047
                         (>= (match-end 0) pos))
5562
 
                    (throw 'exit (list beg (match-beginning 0) lang single))))
 
6048
                    (throw 'exit (list beg (match-beginning 0)
 
6049
                                       lang single lfmt))))
5563
6050
            (if (or (looking-at re2)
5564
6051
                    (re-search-forward re2 nil t))
5565
6052
                (progn
5566
6053
                  (setq end (match-beginning 0))
5567
6054
                  (if (and (re-search-backward re1 nil t)
5568
6055
                           (<= (match-beginning 0) pos))
5569
 
                      (throw 'exit
5570
 
                             (list (match-end 0) end
5571
 
                                   (org-edit-src-get-lang lang) single)))))))))))
 
6056
                      (progn
 
6057
                        (setq lfmt (org-edit-src-get-label-format
 
6058
                                    (match-string 0)))
 
6059
                        (throw 'exit
 
6060
                               (list (match-end 0) end
 
6061
                                     (org-edit-src-get-lang lang)
 
6062
                                     single lfmt))))))))))))
5572
6063
 
5573
6064
(defun org-edit-src-get-lang (lang)
5574
6065
  "Extract the src language."
5583
6074
           (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
5584
6075
      (match-string 1 m))
5585
6076
     (t "fundamental"))))
5586
 
      
 
6077
 
 
6078
(defun org-edit-src-get-label-format (s)
 
6079
  "Extract the label format."
 
6080
  (save-match-data
 
6081
    (if (string-match "-l[ \t]+\\\\?\"\\([^\t\r\n\"]+\\)\\\\?\"" s)
 
6082
        (match-string 1 s))))
 
6083
 
5587
6084
(defun org-edit-src-exit ()
5588
6085
  "Exit special edit and protect problematic lines."
5589
6086
  (interactive)
5610
6107
      (font-lock-unfontify-region (point-min) (point-max)))
5611
6108
    (put-text-property (point-min) (point-max) 'font-lock-fontified t))
5612
6109
  (when (org-bound-and-true-p org-edit-src-picture)
 
6110
    (untabify (point-min) (point-max))
5613
6111
    (goto-char (point-min))
5614
6112
    (while (re-search-forward "^" nil t)
5615
6113
      (replace-match ": "))
5670
6168
C-c -       Cycle list bullet
5671
6169
TAB         Cycle item visibility
5672
6170
M-RET       Insert new heading/item
5673
 
S-M-RET     Insert new TODO heading / Chekbox item
 
6171
S-M-RET     Insert new TODO heading / Checkbox item
5674
6172
C-c C-c     Set tags / toggle checkbox"
5675
6173
  nil " OrgStruct" nil
5676
6174
  (org-load-modules-maybe)
5718
6216
          '([(meta shift right)]  org-shiftmetaright)
5719
6217
          '([(shift up)]          org-shiftup)
5720
6218
          '([(shift down)]        org-shiftdown)
 
6219
          '([(shift left)]        org-shiftleft)
 
6220
          '([(shift right)]       org-shiftright)
5721
6221
          '("\C-c\C-c"            org-ctrl-c-ctrl-c)
5722
6222
          '("\M-q"                fill-paragraph)
5723
6223
          '("\C-c^"               org-sort)
5874
6374
          (cond
5875
6375
           ((symbolp rpl) (funcall rpl tag))
5876
6376
           ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
 
6377
           ((string-match "%h" rpl)
 
6378
            (replace-match (url-hexify-string (or tag "")) t t rpl))
5877
6379
           (t (concat rpl tag)))))
5878
6380
    link))
5879
6381
 
5925
6427
It should be a function accepting three arguments:
5926
6428
 
5927
6429
  path    the path of the link, the text after the prefix (like \"http:\")
5928
 
  desc    the description of the link, if any, nil if there was no descripton
 
6430
  desc    the description of the link, if any, nil if there was no description
5929
6431
  format  the export format, a symbol like `html' or `latex'.
5930
6432
 
5931
6433
The function may use the FORMAT information to return different values
5940
6442
      (setcdr (assoc type org-link-protocols) (list follow export))
5941
6443
    (push (list type follow export) org-link-protocols)))
5942
6444
 
5943
 
 
5944
6445
;;;###autoload
5945
6446
(defun org-store-link (arg)
5946
6447
  "\\<org-mode-map>Store an org-link to the current location.
5948
6449
into an org-buffer with \\[org-insert-link].
5949
6450
 
5950
6451
For some link types, a prefix arg is interpreted:
5951
 
For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
 
6452
For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
5952
6453
For file links, arg negates `org-context-in-file-links'."
5953
6454
  (interactive "P")
5954
6455
  (org-load-modules-maybe)
5960
6461
      (setq link (plist-get org-store-link-plist :link)
5961
6462
            desc (or (plist-get org-store-link-plist :description) link)))
5962
6463
 
 
6464
     ((equal (buffer-name) "*Org Edit Src Example*")
 
6465
      (let (label gc)
 
6466
        (while (or (not label)
 
6467
                   (save-excursion
 
6468
                     (save-restriction
 
6469
                       (widen)
 
6470
                       (goto-char (point-min))
 
6471
                       (re-search-forward
 
6472
                        (regexp-quote (format org-coderef-label-format label))
 
6473
                        nil t))))
 
6474
          (when label (message "Label exists already") (sit-for 2))
 
6475
          (setq label (read-string "Code line label: " label)))
 
6476
        (end-of-line 1)
 
6477
        (setq link (format org-coderef-label-format label))
 
6478
        (setq gc (- 79 (length link)))
 
6479
        (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
 
6480
        (insert link)
 
6481
        (setq link (concat "(" label ")") desc nil)))
 
6482
 
5963
6483
     ((eq major-mode 'calendar-mode)
5964
6484
      (let ((cd (calendar-cursor-to-date)))
5965
6485
        (setq link
6001
6521
            link (org-make-link cpltxt)))
6002
6522
 
6003
6523
     ((and buffer-file-name (org-mode-p))
6004
 
      ;; Just link to current headline
6005
 
      (setq cpltxt (concat "file:"
6006
 
                           (abbreviate-file-name buffer-file-name)))
6007
 
      ;; Add a context search string
6008
 
      (when (org-xor org-context-in-file-links arg)
6009
 
        ;; Check if we are on a target
6010
 
        (if (org-in-regexp "<<\\(.*?\\)>>")
6011
 
            (setq cpltxt (concat cpltxt "::" (match-string 1)))
 
6524
      (cond
 
6525
       ((org-in-regexp "<<\\(.*?\\)>>")
 
6526
        (setq cpltxt
 
6527
              (concat "file:"
 
6528
                      (abbreviate-file-name buffer-file-name)
 
6529
                      "::" (match-string 1))
 
6530
              link (org-make-link cpltxt)))
 
6531
       ((and (featurep 'org-id)
 
6532
             (or (eq org-link-to-org-use-id t)
 
6533
                 (and (eq org-link-to-org-use-id 'create-if-interactive)
 
6534
                      (interactive-p))
 
6535
                 (and org-link-to-org-use-id
 
6536
                      (condition-case nil
 
6537
                          (org-entry-get nil "ID")
 
6538
                        (error nil)))))
 
6539
        ;; We can make a link using the ID.
 
6540
        (setq link (condition-case nil
 
6541
                       (prog1 (org-id-store-link)
 
6542
                         (setq desc (plist-get org-store-link-plist
 
6543
                                               :description)))
 
6544
                     (error
 
6545
                      ;; probably before first headline, link to file only
 
6546
                      (concat "file:"
 
6547
                              (abbreviate-file-name buffer-file-name))))))
 
6548
       (t
 
6549
        ;; Just link to current headline
 
6550
        (setq cpltxt (concat "file:"
 
6551
                             (abbreviate-file-name buffer-file-name)))
 
6552
        ;; Add a context search string
 
6553
        (when (org-xor org-context-in-file-links arg)
6012
6554
          (setq txt (cond
6013
6555
                     ((org-on-heading-p) nil)
6014
6556
                     ((org-region-active-p)
6020
6562
                          (condition-case nil
6021
6563
                              (org-make-org-heading-search-string txt)
6022
6564
                            (error "")))
6023
 
                  desc "NONE"))))
6024
 
      (if (string-match "::\\'" cpltxt)
6025
 
          (setq cpltxt (substring cpltxt 0 -2)))
6026
 
      (setq link (org-make-link cpltxt)))
 
6565
                  desc "NONE")))
 
6566
        (if (string-match "::\\'" cpltxt)
 
6567
            (setq cpltxt (substring cpltxt 0 -2)))
 
6568
        (setq link (org-make-link cpltxt)))))
6027
6569
 
6028
6570
     ((buffer-file-name (buffer-base-buffer))
6029
6571
      ;; Just link to this file here.
6064
6606
  (let (x adr)
6065
6607
    (when (setq x (plist-get plist :from))
6066
6608
      (setq adr (mail-extract-address-components x))
6067
 
      (plist-put plist :fromname (car adr))
6068
 
      (plist-put plist :fromaddress (nth 1 adr)))
 
6609
      (setq plist (plist-put plist :fromname (car adr)))
 
6610
      (setq plist (plist-put plist :fromaddress (nth 1 adr))))
6069
6611
    (when (setq x (plist-get plist :to))
6070
6612
      (setq adr (mail-extract-address-components x))
6071
 
      (plist-put plist :toname (car adr))
6072
 
      (plist-put plist :toaddress (nth 1 adr))))
 
6613
      (setq plist (plist-put plist :toname (car adr)))
 
6614
      (setq plist (plist-put plist :toaddress (nth 1 adr)))))
6073
6615
  (let ((from (plist-get plist :from))
6074
6616
        (to (plist-get plist :to)))
6075
6617
    (when (and from to org-from-is-user-regexp)
6076
 
      (plist-put plist :fromto
6077
 
                 (if (string-match org-from-is-user-regexp from)
6078
 
                     (concat "to %t")
6079
 
                   (concat "from %f")))))
 
6618
      (setq plist
 
6619
            (plist-put plist :fromto
 
6620
                       (if (string-match org-from-is-user-regexp from)
 
6621
                           (concat "to %t")
 
6622
                         (concat "from %f"))))))
6080
6623
  (setq org-store-link-plist plist))
6081
6624
 
6082
6625
(defun org-add-link-props (&rest plist)
6185
6728
This is the list that is used before handing over to the browser.")
6186
6729
 
6187
6730
(defun org-link-escape (text &optional table)
6188
 
  "Escape charaters in TEXT that are problematic for links."
 
6731
  "Escape characters in TEXT that are problematic for links."
6189
6732
  (setq table (or table org-link-escape-chars))
6190
6733
  (when text
6191
6734
    (let ((re (mapconcat (lambda (x) (regexp-quote
6216
6759
  "Exclusive or."
6217
6760
  (if a (not b) b))
6218
6761
 
6219
 
(defun org-get-header (header)
6220
 
  "Find a header field in the current buffer."
6221
 
  (save-excursion
6222
 
    (goto-char (point-min))
6223
 
    (let ((case-fold-search t) s)
6224
 
      (cond
6225
 
       ((eq header 'from)
6226
 
        (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
6227
 
            (setq s (match-string 1)))
6228
 
        (while (string-match "\"" s)
6229
 
          (setq s (replace-match "" t t s)))
6230
 
        (if (string-match "[<(].*" s)
6231
 
            (setq s (replace-match "" t t s))))
6232
 
       ((eq header 'message-id)
6233
 
        (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
6234
 
            (setq s (match-string 1))))
6235
 
       ((eq header 'subject)
6236
 
        (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
6237
 
            (setq s (match-string 1)))))
6238
 
      (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
6239
 
      (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
6240
 
      s)))
6241
 
 
6242
 
 
6243
6762
(defun org-fixup-message-id-for-http (s)
6244
6763
  "Replace special characters in a message id, so it can be used in an http query."
6245
6764
  (while (string-match "<" s)
6261
6780
(defun org-insert-link (&optional complete-file link-location)
6262
6781
  "Insert a link.  At the prompt, enter the link.
6263
6782
 
6264
 
Completion can be used to select a link previously stored with
 
6783
Completion can be used to insert any of the link protocol prefixes like
 
6784
http or ftp in use.
 
6785
 
 
6786
The history can be used to select a link previously stored with
6265
6787
`org-store-link'.  When the empty string is entered (i.e. if you just
6266
6788
press RET at the prompt), the link defaults to the most recently
6267
6789
stored link.  As SPC triggers completion in the minibuffer, you need to
6277
6799
be selected using completion. The path to the file will be relative to the
6278
6800
current directory if the file is in the current directory or a subdirectory.
6279
6801
Otherwise, the link will be the absolute path as completed in the minibuffer
6280
 
\(i.e. normally ~/path/to/file).
 
6802
\(i.e. normally ~/path/to/file).  You can configure this behavior using the
 
6803
option `org-link-file-path-type'.
6281
6804
 
6282
6805
With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
6283
 
the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
6284
 
of `org-keep-stored-link-after-insertion'.
 
6806
the current directory or below.
 
6807
 
 
6808
With three \\[universal-argument] prefixes, negate the meaning of
 
6809
`org-keep-stored-link-after-insertion'.
6285
6810
 
6286
6811
If `org-make-link-description-function' is non-nil, this function will be
6287
6812
called with the link target, and the result will be the default
6313
6838
      (setq remove (list (match-beginning 0) (match-end 0))
6314
6839
            link (read-string "Link: "
6315
6840
                              (org-remove-angle-brackets (match-string 0)))))
6316
 
     ((equal complete-file '(4))
 
6841
     ((member complete-file '((4) (16)))
6317
6842
      ;; Completing read for file names.
6318
6843
      (setq file (read-file-name "File: "))
6319
6844
      (let ((pwd (file-name-as-directory (expand-file-name ".")))
6343
6868
                  (reverse org-stored-links) "\n"))))
6344
6869
      (let ((cw (selected-window)))
6345
6870
        (select-window (get-buffer-window "*Org Links*"))
6346
 
        (shrink-window-if-larger-than-buffer)
6347
6871
        (setq truncate-lines t)
 
6872
        (org-fit-window-to-buffer)
6348
6873
        (select-window cw))
6349
6874
      ;; Fake a link history, containing the stored links.
6350
6875
      (setq tmphist (append (mapcar 'car org-stored-links)
6351
6876
                            org-insert-link-history))
6352
6877
      (unwind-protect
6353
 
          (setq link (org-completing-read
6354
 
                      "Link: "
6355
 
                      (append
6356
 
                       (mapcar (lambda (x) (list (concat (car x) ":")))
6357
 
                               (append org-link-abbrev-alist-local org-link-abbrev-alist))
6358
 
                       (mapcar (lambda (x) (list (concat x ":")))
6359
 
                               org-link-types))
6360
 
                      nil nil nil
6361
 
                      'tmphist
6362
 
                      (or (car (car org-stored-links)))))
 
6878
          (setq link
 
6879
                (let ((org-completion-use-ido nil))
 
6880
                  (org-completing-read
 
6881
                   "Link: "
 
6882
                   (append
 
6883
                    (mapcar (lambda (x) (list (concat (car x) ":")))
 
6884
                            (append org-link-abbrev-alist-local org-link-abbrev-alist))
 
6885
                    (mapcar (lambda (x) (list (concat x ":")))
 
6886
                            org-link-types))
 
6887
                   nil nil nil
 
6888
                   'tmphist
 
6889
                   (or (car (car org-stored-links))))))
6363
6890
        (set-window-configuration wcf)
6364
6891
        (kill-buffer "*Org Links*"))
6365
6892
      (setq entry (assoc link org-stored-links))
6377
6904
    ;; Check if we are linking to the current file with a search option
6378
6905
    ;; If yes, simplify the link by using only the search option.
6379
6906
    (when (and buffer-file-name
6380
 
               (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
 
6907
               (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
6381
6908
      (let* ((path (match-string 1 link))
6382
6909
             (case-fold-search nil)
6383
6910
             (search (match-string 2 link)))
6387
6914
              (setq link search)))))
6388
6915
 
6389
6916
    ;; Check if we can/should use a relative path.  If yes, simplify the link
6390
 
    (when (string-match "\\<file:\\(.*\\)" link)
 
6917
    (when (string-match "^file:\\(.*\\)" link)
6391
6918
      (let* ((path (match-string 1 link))
6392
6919
             (origpath path)
6393
6920
             (case-fold-search nil))
6394
6921
        (cond
6395
 
         ((eq org-link-file-path-type 'absolute)
 
6922
         ((or (eq org-link-file-path-type 'absolute)
 
6923
              (equal complete-file '(16)))
6396
6924
          (setq path (abbreviate-file-name (expand-file-name path))))
6397
6925
         ((eq org-link-file-path-type 'noabbrev)
6398
6926
          (setq path (expand-file-name path)))
6406
6934
                              (expand-file-name path))
6407
6935
                ;; We are linking a file with relative path name.
6408
6936
                (setq path (substring (expand-file-name path)
6409
 
                                      (match-end 0)))))))
 
6937
                                      (match-end 0)))
 
6938
              (setq path (abbreviate-file-name (expand-file-name path)))))))
6410
6939
        (setq link (concat "file:" path))
6411
6940
        (if (equal desc origpath)
6412
6941
            (setq desc path))))
6420
6949
    (insert (org-make-link-string link desc))))
6421
6950
 
6422
6951
(defun org-completing-read (&rest args)
 
6952
  "Completing-read with SPACE being a normal character."
6423
6953
  (let ((minibuffer-local-completion-map
6424
6954
         (copy-keymap minibuffer-local-completion-map)))
6425
6955
    (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
 
6956
    (org-defkey minibuffer-local-completion-map "?" 'self-insert-command)
 
6957
    (apply 'org-ido-completing-read args)))
 
6958
 
 
6959
(defun org-completing-read-no-ido (&rest args)
 
6960
  (let (org-completion-use-ido)
 
6961
    (apply 'org-completing-read args)))
 
6962
 
 
6963
(defun org-ido-completing-read (&rest args)
 
6964
  "Completing-read using `ido-mode' speedups if available"
 
6965
  (if (and org-completion-use-ido
 
6966
           (fboundp 'ido-completing-read)
 
6967
           (boundp 'ido-mode) ido-mode
 
6968
           (listp (second args)))
 
6969
      (apply 'ido-completing-read (concat (car args)) (cdr args))
6426
6970
    (apply 'completing-read args)))
6427
6971
 
6428
6972
(defun org-extract-attributes (s)
6435
6979
          (setq key (match-string 1 a) value (match-string 2 a)
6436
6980
                start (match-end 0)
6437
6981
                attr (plist-put attr (intern key) value))))
6438
 
      (org-add-props s nil 'org-attributes attr))
 
6982
      (org-add-props s nil 'org-attr attr))
6439
6983
    s))
6440
6984
 
6441
6985
(defun org-attributes-to-string (plist)
6443
6987
  (let ((s "") key value)
6444
6988
    (while plist
6445
6989
      (setq key (pop plist) value (pop plist))
6446
 
      (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
 
6990
      (and value
 
6991
           (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
6447
6992
    s))
6448
6993
 
6449
6994
;;; Opening/following a link
6490
7035
      (setq org-link-search-failed t)
6491
7036
      (error "No further link found"))))
6492
7037
 
 
7038
(defun org-translate-link (s)
 
7039
  "Translate a link string if a translation function has been defined."
 
7040
  (if (and org-link-translation-function
 
7041
           (fboundp org-link-translation-function)
 
7042
           (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
 
7043
      (progn
 
7044
        (setq s (funcall org-link-translation-function
 
7045
                         (match-string 1) (match-string 2)))
 
7046
        (concat (car s) ":" (cdr s)))
 
7047
    s))
 
7048
 
 
7049
(defun org-translate-link-from-planner (type path)
 
7050
  "Translate a link from Emacs Planner syntax so that Org can follow it.
 
7051
This is still an experimental function, your mileage may vary."
 
7052
 (cond
 
7053
  ((member type '("http" "https" "news" "ftp"))
 
7054
   ;; standard Internet links are the same.
 
7055
   nil)
 
7056
  ((and (equal type "irc") (string-match "^//" path))
 
7057
   ;; Planner has two / at the beginning of an irc link, we have 1.
 
7058
   ;; We should have zero, actually....
 
7059
   (setq path (substring path 1)))
 
7060
  ((and (equal type "lisp") (string-match "^/" path))
 
7061
   ;; Planner has a slash, we do not.
 
7062
   (setq type "elisp" path (substring path 1)))
 
7063
  ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
 
7064
   ;; A typical message link.  Planner has the id after the fina slash,
 
7065
   ;; we separate it with a hash mark
 
7066
   (setq path (concat (match-string 1 path) "#"
 
7067
                      (org-remove-angle-brackets (match-string 2 path)))))
 
7068
  )
 
7069
 (cons type path))
 
7070
 
6493
7071
(defun org-find-file-at-mouse (ev)
6494
7072
  "Open file link or URL at mouse."
6495
7073
  (interactive "e")
6500
7078
  "Open file link or URL at mouse."
6501
7079
  (interactive "e")
6502
7080
  (mouse-set-point ev)
 
7081
  (if (eq major-mode 'org-agenda-mode)
 
7082
      (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
6503
7083
  (org-open-at-point))
6504
7084
 
6505
7085
(defvar org-window-config-before-follow-link nil
6533
7113
If there is no link at point, this function will search forward up to
6534
7114
the end of the current subtree.
6535
7115
Normally, files will be opened by an appropriate application.  If the
6536
 
optional argument IN-EMACS is non-nil, Emacs will visit the file."
 
7116
optional argument IN-EMACS is non-nil, Emacs will visit the file.
 
7117
With a double prefix argument, try to open outside of Emacs, in the
 
7118
application the system uses for this file type."
6537
7119
  (interactive "P")
6538
7120
  (org-load-modules-maybe)
6539
7121
  (move-marker org-open-link-marker (point))
6540
7122
  (setq org-window-config-before-follow-link (current-window-configuration))
6541
7123
  (org-remove-occur-highlights nil nil t)
6542
 
  (if (org-at-timestamp-p t)
6543
 
      (org-follow-timestamp-link)
 
7124
  (cond
 
7125
   ((org-at-timestamp-p t) (org-follow-timestamp-link))
 
7126
   ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
 
7127
    (org-footnote-action))
 
7128
   (t 
6544
7129
    (let (type path link line search (pos (point)))
6545
7130
      (catch 'match
6546
7131
        (save-excursion
6555
7140
             ((or (file-name-absolute-p link)
6556
7141
                  (string-match "^\\.\\.?/" link))
6557
7142
              (setq type "file" path link))
6558
 
             ((string-match org-link-re-with-space2 link)
 
7143
             ((string-match org-link-re-with-space3 link)
6559
7144
              (setq type (match-string 1 link) path (match-string 2 link)))
6560
7145
             (t (setq type "thisfile" path link)))
6561
7146
            (throw 'match t)))
6590
7175
      ;; Remove any trailing spaces in path
6591
7176
      (if (string-match " +\\'" path)
6592
7177
          (setq path (replace-match "" t t path)))
 
7178
      (if (and org-link-translation-function
 
7179
               (fboundp org-link-translation-function))
 
7180
          ;; Check if we need to translate the link
 
7181
          (let ((tmp (funcall org-link-translation-function type path)))
 
7182
            (setq type (car tmp) path (cdr tmp))))
6593
7183
 
6594
7184
      (cond
6595
7185
 
6674
7264
                           (format "Execute \"%s\" as elisp? "
6675
7265
                                   (org-add-props cmd nil
6676
7266
                                     'face 'org-warning))))
6677
 
              (message "%s => %s" cmd (eval (read cmd)))
 
7267
              (message "%s => %s" cmd
 
7268
                       (if (equal (string-to-char cmd) ?\()
 
7269
                           (eval (read cmd))
 
7270
                         (call-interactively (read cmd))))
6678
7271
            (error "Abort"))))
6679
7272
 
6680
7273
       (t
6681
 
        (browse-url-at-point)))))
 
7274
        (browse-url-at-point))))))
6682
7275
  (move-marker org-open-link-marker nil)
6683
7276
  (run-hook-with-args 'org-follow-link-hook))
6684
7277
 
6763
7356
               pos (match-beginning 0))))
6764
7357
      ;; There is an exact target for this
6765
7358
      (goto-char pos))
 
7359
     ((and (string-match "^(\\(.*\\))$" s0)
 
7360
           (save-excursion
 
7361
             (goto-char (point-min))
 
7362
             (and
 
7363
              (re-search-forward
 
7364
               (concat "[^[]" (regexp-quote
 
7365
                               (format org-coderef-label-format
 
7366
                                       (match-string 1 s0))))
 
7367
               nil t)
 
7368
              (setq type 'dedicated
 
7369
                    pos (1+ (match-beginning 0))))))
 
7370
      ;; There is a coderef target for this
 
7371
      (goto-char pos))
6766
7372
     ((string-match "^/\\(.*\\)/$" s)
6767
7373
      ;; A regular expression
6768
7374
      (cond
6962
7568
First, this expands any special file name abbreviations.  Then the
6963
7569
configuration variable `org-file-apps' is checked if it contains an
6964
7570
entry for this file type, and if yes, the corresponding command is launched.
 
7571
 
6965
7572
If no application is found, Emacs simply visits the file.
6966
 
With optional argument IN-EMACS, Emacs will visit the file.
 
7573
 
 
7574
With optional prefix argument IN-EMACS, Emacs will visit the file.
 
7575
With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
 
7576
and o use an external application to visit the file.
 
7577
 
6967
7578
Optional LINE specifies a line to go to, optional SEARCH a string to
6968
7579
search for.  If LINE or SEARCH is given, the file will always be
6969
7580
opened in Emacs.
6988
7599
        (setq ext (match-string 1 dfile))
6989
7600
      (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
6990
7601
          (setq ext (match-string 1 dfile))))
6991
 
    (if in-emacs
6992
 
        (setq cmd 'emacs)
 
7602
    (cond
 
7603
     ((equal in-emacs '(16))
 
7604
      (setq cmd (cdr (assoc 'system apps))))
 
7605
     (in-emacs (setq cmd 'emacs))
 
7606
     (t
6993
7607
      (setq cmd (or (and remp (cdr (assoc 'remote apps)))
6994
7608
                    (and dirp (cdr (assoc 'directory apps)))
6995
7609
                    (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
6996
7610
                                   'string-match)
6997
7611
                    (cdr (assoc ext apps))
6998
 
                    (cdr (assoc t apps)))))
 
7612
                    (cdr (assoc t apps))))))
 
7613
    (when (eq cmd 'system)
 
7614
      (setq cmd (cdr (assoc 'system apps))))
6999
7615
    (when (eq cmd 'default)
7000
7616
      (setq cmd (cdr (assoc t apps))))
7001
7617
    (when (eq cmd 'mailcap)
7117
7733
(defun org-get-refile-targets (&optional default-buffer)
7118
7734
  "Produce a table with refile targets."
7119
7735
  (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
7120
 
        targets txt re files f desc descre)
 
7736
        targets txt re files f desc descre fast-path-p level)
 
7737
    (message "Getting targets...")
7121
7738
    (with-current-buffer (or default-buffer (current-buffer))
7122
7739
      (while (setq entry (pop entries))
7123
7740
        (setq files (car entry) desc (cdr entry))
 
7741
        (setq fast-path-p nil)
7124
7742
        (cond
7125
7743
         ((null files) (setq files (list (current-buffer))))
7126
7744
         ((eq files 'org-agenda-files)
7144
7762
                                            (cdr desc)))
7145
7763
                               "\\}[ \t]")))
7146
7764
         ((eq (car desc) :maxlevel)
 
7765
          (setq fast-path-p t)
7147
7766
          (setq descre (concat "^\\*\\{1," (number-to-string
7148
7767
                                            (if org-odd-levels-only
7149
7768
                                                (1- (* 2 (cdr desc)))
7154
7773
          (save-excursion
7155
7774
            (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
7156
7775
            (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
 
7776
            (setq f (expand-file-name f))
7157
7777
            (save-excursion
7158
7778
              (save-restriction
7159
7779
                (widen)
7161
7781
                (while (re-search-forward descre nil t)
7162
7782
                  (goto-char (point-at-bol))
7163
7783
                  (when (looking-at org-complex-heading-regexp)
7164
 
                    (setq txt (org-link-display-format (match-string 4))
 
7784
                    (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
 
7785
                          txt (org-link-display-format (match-string 4))
7165
7786
                          re (concat "^" (regexp-quote
7166
7787
                                          (buffer-substring (match-beginning 1)
7167
7788
                                                            (match-end 4)))))
7177
7798
                                                       (buffer-file-name (buffer-base-buffer))))
7178
7799
                                              (if (eq org-refile-use-outline-path 'full-file-path)
7179
7800
                                                  (list (buffer-file-name (buffer-base-buffer)))))
7180
 
                                            (org-get-outline-path)
 
7801
                                            (org-get-outline-path fast-path-p level txt)
7181
7802
                                            (list txt))
7182
7803
                                           "/")))
7183
7804
                    (push (list txt f re (point)) targets))
7184
7805
                  (goto-char (point-at-eol))))))))
7185
 
      (nreverse targets))))
 
7806
    (message "Getting targets...done")
 
7807
    (nreverse targets))))
7186
7808
 
7187
7809
(defun org-protect-slash (s)
7188
7810
  (while (string-match "/" s)
7189
7811
    (setq s (replace-match "\\" t t s)))
7190
7812
  s)
7191
 
    
7192
 
(defun org-get-outline-path ()
 
7813
 
 
7814
(defvar org-olpa (make-vector 20 nil))
 
7815
 
 
7816
(defun org-get-outline-path (&optional fastp level heading)
7193
7817
  "Return the outline path to the current entry, as a list."
7194
 
  (let (rtn)
7195
 
    (save-excursion
7196
 
      (while (org-up-heading-safe)
7197
 
        (when (looking-at org-complex-heading-regexp)
7198
 
          (push (org-match-string-no-properties 4) rtn)))
7199
 
      rtn)))
 
7818
  (if fastp
 
7819
      (progn
 
7820
        (if (> level 19)
 
7821
            (error "Outline path failure, more than 19 levels."))
 
7822
        (loop for i from level upto 19 do
 
7823
              (aset org-olpa i nil))
 
7824
        (prog1
 
7825
            (delq nil (append org-olpa nil))
 
7826
          (aset org-olpa level heading)))
 
7827
    (let (rtn)
 
7828
      (save-excursion
 
7829
        (while (org-up-heading-safe)
 
7830
          (when (looking-at org-complex-heading-regexp)
 
7831
            (push (org-match-string-no-properties 4) rtn)))
 
7832
        rtn))))
7200
7833
 
7201
7834
(defvar org-refile-history nil
7202
7835
  "History for refiling operations.")
7209
7842
 
7210
7843
At the target location, the entry is filed as a subitem of the target heading.
7211
7844
Depending on `org-reverse-note-order', the new subitem will either be the
7212
 
first of the last subitem.
 
7845
first or the last subitem.
 
7846
 
 
7847
If there is an active region, all entries in that region will be moved.
 
7848
However, the region must fulfil the requirement that the first heading
 
7849
is the first one sets the top-level of the moved text - at most siblings
 
7850
below it are allowed.
7213
7851
 
7214
7852
With prefix arg GOTO, the command will only visit the target location,
7215
7853
not actually move anything.
7217
7855
operation has put the subtree."
7218
7856
  (interactive "P")
7219
7857
  (let* ((cbuf (current-buffer))
 
7858
         (regionp (org-region-active-p))
 
7859
         (region-start (and regionp (region-beginning)))
 
7860
         (region-end (and regionp (region-end)))
 
7861
         (region-length (and regionp (- region-end region-start)))
7220
7862
         (filename (buffer-file-name (buffer-base-buffer cbuf)))
7221
7863
         pos it nbuf file re level reversed)
 
7864
    (when regionp (goto-char region-start)
 
7865
          (unless (org-kill-is-subtree-p
 
7866
                   (buffer-substring region-start region-end))
 
7867
            (error "The region is not a (sequence of) subtree(s)")))
7222
7868
    (if (equal goto '(16))
7223
7869
        (org-refile-goto-last-stored)
7224
7870
      (when (setq it (org-refile-get-location
7226
7872
        (setq file (nth 1 it)
7227
7873
              re (nth 2 it)
7228
7874
              pos (nth 3 it))
 
7875
        (if (and (equal (buffer-file-name) file)
 
7876
                 (if regionp
 
7877
                     (and (>= pos region-start)
 
7878
                          (<= pos region-end))
 
7879
                   (and (>= pos (point))
 
7880
                        (< pos (save-excursion
 
7881
                                 (org-end-of-subtree t t))))))
 
7882
            (error "Cannot refile to position inside the tree or region"))
 
7883
                 
7229
7884
        (setq nbuf (or (find-buffer-visiting file)
7230
7885
                       (find-file-noselect file)))
7231
7886
        (if goto
7233
7888
              (switch-to-buffer nbuf)
7234
7889
              (goto-char pos)
7235
7890
              (org-show-context 'org-goto))
7236
 
          (org-copy-subtree 1 nil t)
 
7891
          (if regionp
 
7892
              (progn
 
7893
                (kill-new (buffer-substring region-start region-end))
 
7894
                (org-save-markers-in-region region-start region-end))
 
7895
            (org-copy-subtree 1 nil t))
7237
7896
          (save-excursion
7238
7897
            (set-buffer (setq nbuf (or (find-buffer-visiting file)
7239
7898
                                       (find-file-noselect file))))
7253
7912
                (if (not (bolp)) (newline))
7254
7913
                (bookmark-set "org-refile-last-stored")
7255
7914
                (org-paste-subtree level))))
7256
 
          (org-cut-subtree)
 
7915
          (if regionp
 
7916
              (delete-region (point) (+ (point) region-length))
 
7917
            (org-cut-subtree))
7257
7918
          (setq org-markers-to-move nil)
7258
 
          (message "Entry refiled to \"%s\"" (car it)))))))
 
7919
          (message "Refiled to \"%s\"" (car it)))))))
7259
7920
 
7260
7921
(defun org-refile-goto-last-stored ()
7261
7922
  "Go to the location where the last refile was stored."
7271
7932
  (unless org-refile-target-table
7272
7933
    (error "No refile targets"))
7273
7934
  (let* ((cbuf (current-buffer))
7274
 
         (cfunc (if org-refile-use-outline-path
 
7935
         (cfn (buffer-file-name (buffer-base-buffer cbuf)))
 
7936
         (cfunc (if (and org-refile-use-outline-path
 
7937
                         org-outline-path-complete-in-steps)
7275
7938
                    'org-olpath-completing-read
7276
 
                  'completing-read))
 
7939
                  'org-ido-completing-read))
7277
7940
         (extra (if org-refile-use-outline-path "/" ""))
7278
 
         (filename (buffer-file-name (buffer-base-buffer cbuf)))
7279
 
         (fname (and filename (file-truename filename)))
 
7941
         (filename (and cfn (expand-file-name cfn)))
7280
7942
         (tbl (mapcar
7281
7943
               (lambda (x)
7282
 
                 (if (not (equal fname (file-truename (nth 1 x))))
 
7944
                 (if (not (equal filename (nth 1 x)))
7283
7945
                     (cons (concat (car x) extra " ("
7284
7946
                                   (file-name-nondirectory (nth 1 x)) ")")
7285
7947
                           (cdr x))
7292
7954
(defun org-olpath-completing-read (prompt collection &rest args)
7293
7955
  "Read an outline path like a file name."
7294
7956
  (let ((thetable collection))
7295
 
    (apply 
7296
 
     'completing-read prompt
 
7957
    (apply
 
7958
     'org-ido-completing-read prompt
7297
7959
     (lambda (string predicate &optional flag)
7298
 
       (let (rtn r s f (l (length string)))
 
7960
       (let (rtn r f (l (length string)))
7299
7961
         (cond
7300
7962
          ((eq flag nil)
7301
7963
           ;; try completion
7338
8000
  "Matches the startline of a dynamic block, with parameters.")
7339
8001
 
7340
8002
(defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
7341
 
  "Matches the end of a dyhamic block.")
 
8003
  "Matches the end of a dynamic block.")
7342
8004
 
7343
8005
(defun org-create-dblock (plist)
7344
8006
  "Create a dynamic block section, with parameters taken from PLIST.
7345
 
PLIST must containe a :name entry which is used as name of the block."
 
8007
PLIST must contain a :name entry which is used as name of the block."
7346
8008
  (unless (bolp) (newline))
7347
8009
  (let ((name (plist-get plist :name)))
7348
8010
    (insert "#+BEGIN: " name)
7418
8080
 
7419
8081
(defun org-beginning-of-dblock ()
7420
8082
  "Find the beginning of the dynamic block at point.
7421
 
Error if there is no scuh block at point."
 
8083
Error if there is no such block at point."
7422
8084
  (let ((pos (point))
7423
8085
        beg)
7424
8086
    (end-of-line 1)
7445
8107
    "BEGIN_EXAMPLE" "END_EXAMPLE"
7446
8108
    "BEGIN_QUOTE" "END_QUOTE"
7447
8109
    "BEGIN_VERSE" "END_VERSE"
7448
 
    "BEGIN_SRC" "END_SRC"))
 
8110
    "BEGIN_SRC" "END_SRC"
 
8111
    "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
7449
8112
 
7450
8113
(defcustom org-structure-template-alist
7451
8114
  '(
7452
 
    ("s" "#+begin_src ?\n\n#+end_src" 
 
8115
    ("s" "#+begin_src ?\n\n#+end_src"
7453
8116
         "<src lang=\"?\">\n\n</src>")
7454
8117
    ("e" "#+begin_example\n?\n#+end_example"
7455
8118
         "<example>\n?\n</example>")
7474
8137
This is a list of abbreviation keys and values.  The value gets inserted
7475
8138
it you type @samp{.} followed by the key and then the completion key,
7476
8139
usually `M-TAB'.  %file will be replaced by a file name after prompting
7477
 
for the file uning completion.
 
8140
for the file using completion.
7478
8141
There are two templates for each key, the first uses the original Org syntax,
7479
8142
the second uses Emacs Muse-like syntax tags.  These Muse-like tags become
7480
8143
the default when the /org-mtags.el/ module has been loaded. See also the
7481
 
variable `org-mtags-prefere-muse-templates'.
 
8144
variable `org-mtags-prefer-muse-templates'.
7482
8145
This is an experimental feature, it is undecided if it is going to stay in."
7483
8146
  :group 'org-completion
7484
8147
  :type '(repeat
7501
8164
 
7502
8165
(defun org-complete-expand-structure-template (start cell)
7503
8166
  "Expand a structure template."
7504
 
  (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
 
8167
  (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
7505
8168
         (rpl (nth (if musep 2 1) cell)))
7506
8169
    (delete-region start (point))
7507
8170
    (when (string-match "\\`#\\+" rpl)
7512
8175
       (t (newline))))
7513
8176
    (setq start (point))
7514
8177
    (if (string-match "%file" rpl)
7515
 
        (setq rpl (replace-match 
 
8178
        (setq rpl (replace-match
7516
8179
                   (concat
7517
8180
                    "\""
7518
8181
                    (save-match-data
7521
8184
                   t t rpl)))
7522
8185
    (insert rpl)
7523
8186
    (if (re-search-backward "\\?" start t) (delete-char 1))))
7524
 
    
 
8187
 
7525
8188
 
7526
8189
(defun org-complete (&optional arg)
7527
8190
  "Perform completion on word at point.
7666
8329
If the last change removed the TODO tag or switched to DONE, then
7667
8330
this is nil.")
7668
8331
 
7669
 
(defvar org-setting-tags nil) ; dynamically skiped
 
8332
(defvar org-setting-tags nil) ; dynamically skipped
7670
8333
 
7671
8334
(defun org-parse-local-options (string var)
7672
8335
  "Parse STRING for startup setting relevant for variable VAR."
7689
8352
              (push (nth 2 e) rtn)))
7690
8353
          rtn)))))
7691
8354
 
7692
 
(defvar org-blocker-hook nil
7693
 
  "Hook for functions that are allowed to block a state change.
7694
 
 
7695
 
Each function gets as its single argument a property list, see
7696
 
`org-trigger-hook' for more information about this list.
7697
 
 
7698
 
If any of the functions in this hook returns nil, the state change
7699
 
is blocked.")
7700
 
 
7701
 
(defvar org-trigger-hook nil
7702
 
  "Hook for functions that are triggered by a state change.
7703
 
 
7704
 
Each function gets as its single argument a property list with at least
7705
 
the following elements:
7706
 
 
7707
 
 (:type type-of-change :position pos-at-entry-start
7708
 
  :from old-state :to new-state)
7709
 
 
7710
 
Depending on the type, more properties may be present.
7711
 
 
7712
 
This mechanism is currently implemented for:
7713
 
 
7714
 
TODO state changes
7715
 
------------------
7716
 
:type  todo-state-change
7717
 
:from  previous state (keyword as a string), or nil
7718
 
:to    new state (keyword as a string), or nil")
7719
 
 
7720
 
 
 
8355
(defvar org-agenda-headline-snapshot-before-repeat)
7721
8356
(defun org-todo (&optional arg)
7722
8357
  "Change the TODO state of an item.
7723
8358
The state of an item is given by a keyword at the start of the heading,
7733
8368
 
7734
8369
With C-u prefix arg, use completion to determine the new state.
7735
8370
With numeric prefix arg, switch to that state.
 
8371
With a double C-u prefix, switch to the next set of TODO keywords (nextset).
 
8372
With a tripple C-u prefix, circumvent any state blocking.
7736
8373
 
7737
8374
For calling through lisp, arg is also interpreted in the following way:
7738
8375
'none             -> empty state
7743
8380
\"WAITING\"         -> switch to the specified keyword, but only if it
7744
8381
                     really is a member of `org-todo-keywords'."
7745
8382
  (interactive "P")
7746
 
  (save-excursion
7747
 
    (catch 'exit
7748
 
      (org-back-to-heading)
7749
 
      (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
7750
 
      (or (looking-at (concat " +" org-todo-regexp " *"))
7751
 
          (looking-at " *"))
7752
 
      (let* ((match-data (match-data))
7753
 
             (startpos (point-at-bol))
7754
 
             (logging (save-match-data (org-entry-get nil "LOGGING" t)))
7755
 
             (org-log-done org-log-done)
7756
 
             (org-log-repeat org-log-repeat)
7757
 
             (org-todo-log-states org-todo-log-states)
7758
 
             (this (match-string 1))
7759
 
             (hl-pos (match-beginning 0))
7760
 
             (head (org-get-todo-sequence-head this))
7761
 
             (ass (assoc head org-todo-kwd-alist))
7762
 
             (interpret (nth 1 ass))
7763
 
             (done-word (nth 3 ass))
7764
 
             (final-done-word (nth 4 ass))
7765
 
             (last-state (or this ""))
7766
 
             (completion-ignore-case t)
7767
 
             (member (member this org-todo-keywords-1))
7768
 
             (tail (cdr member))
7769
 
             (state (cond
7770
 
                     ((and org-todo-key-trigger
7771
 
                           (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
7772
 
                               (and (not arg) org-use-fast-todo-selection
7773
 
                                    (not (eq org-use-fast-todo-selection 'prefix)))))
7774
 
                      ;; Use fast selection
7775
 
                      (org-fast-todo-selection))
7776
 
                     ((and (equal arg '(4))
7777
 
                           (or (not org-use-fast-todo-selection)
7778
 
                               (not org-todo-key-trigger)))
7779
 
                      ;; Read a state with completion
7780
 
                      (completing-read "State: " (mapcar (lambda(x) (list x))
7781
 
                                                         org-todo-keywords-1)
7782
 
                                       nil t))
7783
 
                     ((eq arg 'right)
7784
 
                      (if this
7785
 
                          (if tail (car tail) nil)
7786
 
                        (car org-todo-keywords-1)))
7787
 
                     ((eq arg 'left)
7788
 
                      (if (equal member org-todo-keywords-1)
7789
 
                          nil
 
8383
  (if (equal arg '(16)) (setq arg 'nextset))
 
8384
  (let ((org-blocker-hook org-blocker-hook))
 
8385
    (when (equal arg '(64))
 
8386
      (setq arg nil org-blocker-hook nil))
 
8387
    (save-excursion
 
8388
      (catch 'exit
 
8389
        (org-back-to-heading)
 
8390
        (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
 
8391
        (or (looking-at (concat " +" org-todo-regexp " *"))
 
8392
            (looking-at " *"))
 
8393
        (let* ((match-data (match-data))
 
8394
               (startpos (point-at-bol))
 
8395
               (logging (save-match-data (org-entry-get nil "LOGGING" t)))
 
8396
               (org-log-done org-log-done)
 
8397
               (org-log-repeat org-log-repeat)
 
8398
               (org-todo-log-states org-todo-log-states)
 
8399
               (this (match-string 1))
 
8400
               (hl-pos (match-beginning 0))
 
8401
               (head (org-get-todo-sequence-head this))
 
8402
               (ass (assoc head org-todo-kwd-alist))
 
8403
               (interpret (nth 1 ass))
 
8404
               (done-word (nth 3 ass))
 
8405
               (final-done-word (nth 4 ass))
 
8406
               (last-state (or this ""))
 
8407
               (completion-ignore-case t)
 
8408
               (member (member this org-todo-keywords-1))
 
8409
               (tail (cdr member))
 
8410
               (state (cond
 
8411
                       ((and org-todo-key-trigger
 
8412
                             (or (and (equal arg '(4))
 
8413
                                      (eq org-use-fast-todo-selection 'prefix))
 
8414
                                 (and (not arg) org-use-fast-todo-selection
 
8415
                                      (not (eq org-use-fast-todo-selection
 
8416
                                               'prefix)))))
 
8417
                        ;; Use fast selection
 
8418
                        (org-fast-todo-selection))
 
8419
                       ((and (equal arg '(4))
 
8420
                             (or (not org-use-fast-todo-selection)
 
8421
                                 (not org-todo-key-trigger)))
 
8422
                        ;; Read a state with completion
 
8423
                        (org-ido-completing-read
 
8424
                         "State: " (mapcar (lambda(x) (list x))
 
8425
                                           org-todo-keywords-1)
 
8426
                         nil t))
 
8427
                       ((eq arg 'right)
7790
8428
                        (if this
7791
 
                            (nth (- (length org-todo-keywords-1) (length tail) 2)
7792
 
                                 org-todo-keywords-1)
7793
 
                          (org-last org-todo-keywords-1))))
7794
 
                     ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
7795
 
                           (setq arg nil))) ; hack to fall back to cycling
7796
 
                     (arg
7797
 
                      ;; user or caller requests a specific state
7798
 
                      (cond
7799
 
                       ((equal arg "") nil)
7800
 
                       ((eq arg 'none) nil)
7801
 
                       ((eq arg 'done) (or done-word (car org-done-keywords)))
7802
 
                       ((eq arg 'nextset)
7803
 
                        (or (car (cdr (member head org-todo-heads)))
7804
 
                            (car org-todo-heads)))
7805
 
                       ((eq arg 'previousset)
7806
 
                        (let ((org-todo-heads (reverse org-todo-heads)))
 
8429
                            (if tail (car tail) nil)
 
8430
                          (car org-todo-keywords-1)))
 
8431
                       ((eq arg 'left)
 
8432
                        (if (equal member org-todo-keywords-1)
 
8433
                            nil
 
8434
                          (if this
 
8435
                              (nth (- (length org-todo-keywords-1)
 
8436
                                      (length tail) 2)
 
8437
                                   org-todo-keywords-1)
 
8438
                            (org-last org-todo-keywords-1))))
 
8439
                       ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
 
8440
                             (setq arg nil))) ; hack to fall back to cycling
 
8441
                       (arg
 
8442
                        ;; user or caller requests a specific state
 
8443
                        (cond
 
8444
                         ((equal arg "") nil)
 
8445
                         ((eq arg 'none) nil)
 
8446
                         ((eq arg 'done) (or done-word (car org-done-keywords)))
 
8447
                         ((eq arg 'nextset)
7807
8448
                          (or (car (cdr (member head org-todo-heads)))
7808
 
                              (car org-todo-heads))))
7809
 
                       ((car (member arg org-todo-keywords-1)))
7810
 
                       ((nth (1- (prefix-numeric-value arg))
7811
 
                             org-todo-keywords-1))))
7812
 
                     ((null member) (or head (car org-todo-keywords-1)))
7813
 
                     ((equal this final-done-word) nil) ;; -> make empty
7814
 
                     ((null tail) nil) ;; -> first entry
7815
 
                     ((eq interpret 'sequence)
7816
 
                      (car tail))
7817
 
                     ((memq interpret '(type priority))
7818
 
                      (if (eq this-command last-command)
7819
 
                          (car tail)
7820
 
                        (if (> (length tail) 0)
7821
 
                            (or done-word (car org-done-keywords))
7822
 
                          nil)))
7823
 
                     (t nil)))
7824
 
             (next (if state (concat " " state " ") " "))
7825
 
             (change-plist (list :type 'todo-state-change :from this :to state
7826
 
                                 :position startpos))
7827
 
             dolog now-done-p)
7828
 
        (when org-blocker-hook
7829
 
          (unless (save-excursion
7830
 
                    (save-match-data
7831
 
                      (run-hook-with-args-until-failure
7832
 
                       'org-blocker-hook change-plist)))
7833
 
            (if (interactive-p)
7834
 
                (error "TODO state change from %s to %s blocked" this state)
7835
 
              ;; fail silently
7836
 
              (message "TODO state change from %s to %s blocked" this state)
7837
 
              (throw 'exit nil))))
7838
 
        (store-match-data match-data)
7839
 
        (replace-match next t t)
7840
 
        (unless (pos-visible-in-window-p hl-pos)
7841
 
          (message "TODO state changed to %s" (org-trim next)))
7842
 
        (unless head
7843
 
          (setq head (org-get-todo-sequence-head state)
7844
 
                ass (assoc head org-todo-kwd-alist)
7845
 
                interpret (nth 1 ass)
7846
 
                done-word (nth 3 ass)
7847
 
                final-done-word (nth 4 ass)))
7848
 
        (when (memq arg '(nextset previousset))
7849
 
          (message "Keyword-Set %d/%d: %s"
7850
 
                   (- (length org-todo-sets) -1
7851
 
                      (length (memq (assoc state org-todo-sets) org-todo-sets)))
7852
 
                   (length org-todo-sets)
7853
 
                   (mapconcat 'identity (assoc state org-todo-sets) " ")))
7854
 
        (setq org-last-todo-state-is-todo
7855
 
              (not (member state org-done-keywords)))
7856
 
        (setq now-done-p (and (member state org-done-keywords)
7857
 
                              (not (member this org-done-keywords))))
7858
 
        (and logging (org-local-logging logging))
7859
 
        (when (and (or org-todo-log-states org-log-done)
7860
 
                   (not (memq arg '(nextset previousset))))
7861
 
          ;; we need to look at recording a time and note
7862
 
          (setq dolog (or (nth 1 (assoc state org-todo-log-states))
7863
 
                          (nth 2 (assoc this org-todo-log-states))))
7864
 
          (when (and state
7865
 
                     (member state org-not-done-keywords)
7866
 
                     (not (member this org-not-done-keywords)))
7867
 
            ;; This is now a todo state and was not one before
7868
 
            ;; If there was a CLOSED time stamp, get rid of it.
7869
 
            (org-add-planning-info nil nil 'closed))
7870
 
          (when (and now-done-p org-log-done)
7871
 
            ;; It is now done, and it was not done before
7872
 
            (org-add-planning-info 'closed (org-current-time))
7873
 
            (if (and (not dolog) (eq 'note org-log-done))
7874
 
                (org-add-log-setup 'done state 'findpos 'note)))
7875
 
          (when (and state dolog)
7876
 
            ;; This is a non-nil state, and we need to log it
7877
 
            (org-add-log-setup 'state state 'findpos dolog)))
7878
 
        ;; Fixup tag positioning
7879
 
        (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
7880
 
        (when org-provide-todo-statistics
7881
 
          (org-update-parent-todo-statistics))
7882
 
        (run-hooks 'org-after-todo-state-change-hook)
7883
 
        (if (and arg (not (member state org-done-keywords)))
7884
 
            (setq head (org-get-todo-sequence-head state)))
7885
 
        (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
7886
 
        ;; Do we need to trigger a repeat?
7887
 
        (when now-done-p (org-auto-repeat-maybe state))
7888
 
        ;; Fixup cursor location if close to the keyword
7889
 
        (if (and (outline-on-heading-p)
7890
 
                 (not (bolp))
7891
 
                 (save-excursion (beginning-of-line 1)
7892
 
                                 (looking-at org-todo-line-regexp))
7893
 
                 (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
7894
 
            (progn
7895
 
              (goto-char (or (match-end 2) (match-end 1)))
7896
 
              (just-one-space)))
7897
 
        (when org-trigger-hook
7898
 
          (save-excursion
7899
 
            (run-hook-with-args 'org-trigger-hook change-plist)))))))
 
8449
                              (car org-todo-heads)))
 
8450
                         ((eq arg 'previousset)
 
8451
                          (let ((org-todo-heads (reverse org-todo-heads)))
 
8452
                            (or (car (cdr (member head org-todo-heads)))
 
8453
                                (car org-todo-heads))))
 
8454
                         ((car (member arg org-todo-keywords-1)))
 
8455
                         ((nth (1- (prefix-numeric-value arg))
 
8456
                               org-todo-keywords-1))))
 
8457
                       ((null member) (or head (car org-todo-keywords-1)))
 
8458
                       ((equal this final-done-word) nil) ;; -> make empty
 
8459
                       ((null tail) nil) ;; -> first entry
 
8460
                       ((eq interpret 'sequence)
 
8461
                        (car tail))
 
8462
                       ((memq interpret '(type priority))
 
8463
                        (if (eq this-command last-command)
 
8464
                            (car tail)
 
8465
                          (if (> (length tail) 0)
 
8466
                              (or done-word (car org-done-keywords))
 
8467
                            nil)))
 
8468
                       (t nil)))
 
8469
               (next (if state (concat " " state " ") " "))
 
8470
               (change-plist (list :type 'todo-state-change :from this :to state
 
8471
                                   :position startpos))
 
8472
               dolog now-done-p)
 
8473
          (when org-blocker-hook
 
8474
            (setq org-last-todo-state-is-todo
 
8475
                  (not (member this org-done-keywords)))
 
8476
            (unless (save-excursion
 
8477
                      (save-match-data
 
8478
                        (run-hook-with-args-until-failure
 
8479
                         'org-blocker-hook change-plist)))
 
8480
              (if (interactive-p)
 
8481
                  (error "TODO state change from %s to %s blocked" this state)
 
8482
                ;; fail silently
 
8483
                (message "TODO state change from %s to %s blocked" this state)
 
8484
                (throw 'exit nil))))
 
8485
          (store-match-data match-data)
 
8486
          (replace-match next t t)
 
8487
          (unless (pos-visible-in-window-p hl-pos)
 
8488
            (message "TODO state changed to %s" (org-trim next)))
 
8489
          (unless head
 
8490
            (setq head (org-get-todo-sequence-head state)
 
8491
                  ass (assoc head org-todo-kwd-alist)
 
8492
                  interpret (nth 1 ass)
 
8493
                  done-word (nth 3 ass)
 
8494
                  final-done-word (nth 4 ass)))
 
8495
          (when (memq arg '(nextset previousset))
 
8496
            (message "Keyword-Set %d/%d: %s"
 
8497
                     (- (length org-todo-sets) -1
 
8498
                        (length (memq (assoc state org-todo-sets) org-todo-sets)))
 
8499
                     (length org-todo-sets)
 
8500
                     (mapconcat 'identity (assoc state org-todo-sets) " ")))
 
8501
          (setq org-last-todo-state-is-todo
 
8502
                (not (member state org-done-keywords)))
 
8503
          (setq now-done-p (and (member state org-done-keywords)
 
8504
                                (not (member this org-done-keywords))))
 
8505
          (and logging (org-local-logging logging))
 
8506
          (when (and (or org-todo-log-states org-log-done)
 
8507
                     (not (memq arg '(nextset previousset))))
 
8508
            ;; we need to look at recording a time and note
 
8509
            (setq dolog (or (nth 1 (assoc state org-todo-log-states))
 
8510
                            (nth 2 (assoc this org-todo-log-states))))
 
8511
            (when (and state
 
8512
                       (member state org-not-done-keywords)
 
8513
                       (not (member this org-not-done-keywords)))
 
8514
              ;; This is now a todo state and was not one before
 
8515
              ;; If there was a CLOSED time stamp, get rid of it.
 
8516
              (org-add-planning-info nil nil 'closed))
 
8517
            (when (and now-done-p org-log-done)
 
8518
              ;; It is now done, and it was not done before
 
8519
              (org-add-planning-info 'closed (org-current-time))
 
8520
              (if (and (not dolog) (eq 'note org-log-done))
 
8521
                  (org-add-log-setup 'done state 'findpos 'note)))
 
8522
            (when (and state dolog)
 
8523
              ;; This is a non-nil state, and we need to log it
 
8524
              (org-add-log-setup 'state state 'findpos dolog)))
 
8525
          ;; Fixup tag positioning
 
8526
          (org-todo-trigger-tag-changes state)
 
8527
          (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
 
8528
          (when org-provide-todo-statistics
 
8529
            (org-update-parent-todo-statistics))
 
8530
          (run-hooks 'org-after-todo-state-change-hook)
 
8531
          (if (and arg (not (member state org-done-keywords)))
 
8532
              (setq head (org-get-todo-sequence-head state)))
 
8533
          (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
 
8534
          ;; Do we need to trigger a repeat?
 
8535
          (when now-done-p
 
8536
            (when (boundp 'org-agenda-headline-snapshot-before-repeat)
 
8537
              ;; This is for the agenda, take a snapshot of the headline.
 
8538
              (save-match-data
 
8539
                (setq org-agenda-headline-snapshot-before-repeat
 
8540
                      (org-get-heading))))
 
8541
            (org-auto-repeat-maybe state))
 
8542
          ;; Fixup cursor location if close to the keyword
 
8543
          (if (and (outline-on-heading-p)
 
8544
                   (not (bolp))
 
8545
                   (save-excursion (beginning-of-line 1)
 
8546
                                   (looking-at org-todo-line-regexp))
 
8547
                   (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
 
8548
              (progn
 
8549
                (goto-char (or (match-end 2) (match-end 1)))
 
8550
                (just-one-space)))
 
8551
          (when org-trigger-hook
 
8552
            (save-excursion
 
8553
              (run-hook-with-args 'org-trigger-hook change-plist))))))))
 
8554
 
 
8555
(defun org-block-todo-from-children-or-siblings (change-plist)
 
8556
  "Block turning an entry into a TODO, using the hierarchy.
 
8557
This checks whether the current task should be blocked from state
 
8558
changes.  Such blocking occurs when:
 
8559
 
 
8560
  1. The task has children which are not all in a completed state.
 
8561
 
 
8562
  2. A task has a parent with the property :ORDERED:, and there
 
8563
     are siblings prior to the current task with incomplete
 
8564
     status."
 
8565
  (catch 'dont-block
 
8566
    ;; If this is not a todo state change, or if this entry is already DONE,
 
8567
    ;; do not block
 
8568
    (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
 
8569
              (member (plist-get change-plist :from)
 
8570
                      (cons 'done org-done-keywords))
 
8571
              (member (plist-get change-plist :to)
 
8572
                      (cons 'todo org-not-done-keywords)))
 
8573
      (throw 'dont-block t))
 
8574
    ;; If this task has children, and any are undone, it's blocked
 
8575
    (save-excursion
 
8576
      (org-back-to-heading t)
 
8577
      (let ((this-level (funcall outline-level)))
 
8578
        (outline-next-heading)
 
8579
        (let ((child-level (funcall outline-level)))
 
8580
          (while (and (not (eobp))
 
8581
                      (> child-level this-level))
 
8582
            ;; this todo has children, check whether they are all
 
8583
            ;; completed
 
8584
            (if (and (not (org-entry-is-done-p))
 
8585
                     (org-entry-is-todo-p))
 
8586
                (throw 'dont-block nil))
 
8587
            (outline-next-heading)
 
8588
            (setq child-level (funcall outline-level))))))
 
8589
    ;; Otherwise, if the task's parent has the :ORDERED: property, and
 
8590
    ;; any previous siblings are undone, it's blocked
 
8591
    (save-excursion
 
8592
      (org-back-to-heading t)
 
8593
      (when (save-excursion
 
8594
              (ignore-errors
 
8595
                (org-up-heading-all 1)
 
8596
                (org-entry-get (point) "ORDERED")))
 
8597
        (let* ((this-level (funcall outline-level))
 
8598
               (current-level this-level))
 
8599
          (while (and (not (bobp))
 
8600
                      (= current-level this-level))
 
8601
            (outline-previous-heading)
 
8602
            (setq current-level (funcall outline-level))
 
8603
            (if (= current-level this-level)
 
8604
                ;; this todo has children, check whether they are all
 
8605
                ;; completed
 
8606
                (if (and (not (org-entry-is-done-p))
 
8607
                         (org-entry-is-todo-p))
 
8608
                    (throw 'dont-block nil)))))))
 
8609
    t))                                 ; don't block
 
8610
 
 
8611
(defun org-toggle-ordered-property ()
 
8612
  "Toggle the ORDERED property of the current entry."
 
8613
  (interactive)
 
8614
  (save-excursion
 
8615
    (org-back-to-heading)
 
8616
    (if (org-entry-get nil "ORDERED")
 
8617
        (progn
 
8618
          (org-delete-property "ORDERED")
 
8619
          (message "Subtasks can be completed in arbitrary order or parallel"))
 
8620
      (org-entry-put nil "ORDERED" "t")
 
8621
      (message "Subtasks must be completed in sequence"))))
 
8622
 
 
8623
(defun org-block-todo-from-checkboxes (change-plist)
 
8624
  "Block turning an entry into a TODO, using checkboxes.
 
8625
This checks whether the current task should be blocked from state
 
8626
changes because there are uncheckd boxes in this entry."
 
8627
  (catch 'dont-block
 
8628
    ;; If this is not a todo state change, or if this entry is already DONE,
 
8629
    ;; do not block
 
8630
    (when (or (not (eq (plist-get change-plist :type) 'todo-state-change))
 
8631
              (member (plist-get change-plist :from)
 
8632
                      (cons 'done org-done-keywords))
 
8633
              (member (plist-get change-plist :to)
 
8634
                      (cons 'todo org-not-done-keywords)))
 
8635
      (throw 'dont-block t))
 
8636
    ;; If this task has checkboxes that are not checked, it's blocked
 
8637
    (save-excursion
 
8638
      (org-back-to-heading t)
 
8639
      (let ((beg (point)) end)
 
8640
        (outline-next-heading)
 
8641
        (setq end (point))
 
8642
        (goto-char beg)
 
8643
        (if (re-search-forward "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\)[ \t]+\\[[- ]\\]"
 
8644
                               end t)
 
8645
            (throw 'dont-block nil))))
 
8646
    t)) ; do not block
7900
8647
 
7901
8648
(defun org-update-parent-todo-statistics ()
7902
8649
  "Update any statistics cookie in the parent of the current headline."
7903
8650
  (interactive)
7904
8651
  (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
7905
 
        level (cnt-all 0) (cnt-done 0) is-percent kwd)
 
8652
        level (cnt-all 0) (cnt-done 0) is-percent kwd cookie-present)
7906
8653
    (catch 'exit
7907
8654
      (save-excursion
7908
8655
        (setq level (org-up-heading-safe))
7909
 
        (unless (and level
7910
 
                     (re-search-forward box-re (point-at-eol) t))
 
8656
        (unless level
7911
8657
          (throw 'exit nil))
7912
 
        (setq is-percent (match-end 2))
7913
 
        (save-match-data
7914
 
          (unless (outline-next-heading) (throw 'exit nil))
7915
 
          (while (looking-at org-todo-line-regexp)
7916
 
            (setq kwd (match-string 2))
7917
 
            (and kwd (setq cnt-all (1+ cnt-all)))
7918
 
            (and (member kwd org-done-keywords)
7919
 
                 (setq cnt-done (1+ cnt-done)))
7920
 
            (condition-case nil
7921
 
                (org-forward-same-level 1)
7922
 
              (error (end-of-line 1)))))
7923
 
        (replace-match 
7924
 
         (if is-percent
7925
 
             (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
7926
 
           (format "[%d/%d]" cnt-done cnt-all)))
7927
 
        (run-hook-with-args 'org-after-todo-statistics-hook
7928
 
                            cnt-done (- cnt-all cnt-done))))))
 
8658
        (while (re-search-forward box-re (point-at-eol) t)
 
8659
          (setq cnt-all 0 cnt-done 0 cookie-present t)
 
8660
          (setq is-percent (match-end 2))
 
8661
          (save-match-data
 
8662
            (unless (outline-next-heading) (throw 'exit nil))
 
8663
            (while (looking-at org-todo-line-regexp)
 
8664
              (setq kwd (match-string 2))
 
8665
              (and kwd (setq cnt-all (1+ cnt-all)))
 
8666
              (and (member kwd org-done-keywords)
 
8667
                   (setq cnt-done (1+ cnt-done)))
 
8668
              (condition-case nil
 
8669
                  (org-forward-same-level 1)
 
8670
                (error (end-of-line 1)))))
 
8671
          (replace-match
 
8672
           (if is-percent
 
8673
               (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
 
8674
             (format "[%d/%d]" cnt-done cnt-all))))
 
8675
        (when cookie-present
 
8676
          (run-hook-with-args 'org-after-todo-statistics-hook
 
8677
                              cnt-done (- cnt-all cnt-done)))))))
7929
8678
 
7930
8679
(defvar org-after-todo-statistics-hook nil
7931
8680
  "Hook that is called after a TODO statistics cookie has been updated.
7942
8691
   (let (org-log-done org-log-states)   ; turn off logging
7943
8692
     (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
7944
8693
")
7945
 
         
 
8694
 
 
8695
(defun org-todo-trigger-tag-changes (state)
 
8696
  "Apply the changes defined in `org-todo-state-tags-triggers'."
 
8697
  (let ((l org-todo-state-tags-triggers)
 
8698
        changes)
 
8699
    (when (or (not state) (equal state ""))
 
8700
      (setq changes (append changes (cdr (assoc "" l)))))
 
8701
    (when (and (stringp state) (> (length state) 0))
 
8702
      (setq changes (append changes (cdr (assoc state l)))))
 
8703
    (when (member state org-not-done-keywords)
 
8704
      (setq changes (append changes (cdr (assoc 'todo l)))))
 
8705
    (when (member state org-done-keywords)
 
8706
      (setq changes (append changes (cdr (assoc 'done l)))))
 
8707
    (dolist (c changes)
 
8708
      (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
 
8709
 
7946
8710
(defun org-local-logging (value)
7947
8711
  "Get logging settings from a property VALUE."
7948
8712
  (let* (words w a)
7990
8754
         (ncol (/ (- (window-width) 4) fwidth))
7991
8755
         tg cnt e c tbl
7992
8756
         groups ingroup)
7993
 
    (save-window-excursion
7994
 
      (if expert
7995
 
          (set-buffer (get-buffer-create " *Org todo*"))
7996
 
        (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
7997
 
      (erase-buffer)
7998
 
      (org-set-local 'org-done-keywords done-keywords)
7999
 
      (setq tbl fulltable cnt 0)
8000
 
      (while (setq e (pop tbl))
 
8757
    (save-excursion
 
8758
      (save-window-excursion
 
8759
        (if expert
 
8760
            (set-buffer (get-buffer-create " *Org todo*"))
 
8761
          (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
 
8762
        (erase-buffer)
 
8763
        (org-set-local 'org-done-keywords done-keywords)
 
8764
        (setq tbl fulltable cnt 0)
 
8765
        (while (setq e (pop tbl))
 
8766
          (cond
 
8767
           ((equal e '(:startgroup))
 
8768
            (push '() groups) (setq ingroup t)
 
8769
            (when (not (= cnt 0))
 
8770
              (setq cnt 0)
 
8771
              (insert "\n"))
 
8772
            (insert "{ "))
 
8773
           ((equal e '(:endgroup))
 
8774
            (setq ingroup nil cnt 0)
 
8775
            (insert "}\n"))
 
8776
           (t
 
8777
            (setq tg (car e) c (cdr e))
 
8778
            (if ingroup (push tg (car groups)))
 
8779
            (setq tg (org-add-props tg nil 'face
 
8780
                                    (org-get-todo-face tg)))
 
8781
            (if (and (= cnt 0) (not ingroup)) (insert "  "))
 
8782
            (insert "[" c "] " tg (make-string
 
8783
                                   (- fwidth 4 (length tg)) ?\ ))
 
8784
            (when (= (setq cnt (1+ cnt)) ncol)
 
8785
              (insert "\n")
 
8786
              (if ingroup (insert "  "))
 
8787
              (setq cnt 0)))))
 
8788
        (insert "\n")
 
8789
        (goto-char (point-min))
 
8790
        (if (not expert) (org-fit-window-to-buffer))
 
8791
        (message "[a-z..]:Set [SPC]:clear")
 
8792
        (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8001
8793
        (cond
8002
 
         ((equal e '(:startgroup))
8003
 
          (push '() groups) (setq ingroup t)
8004
 
          (when (not (= cnt 0))
8005
 
            (setq cnt 0)
8006
 
            (insert "\n"))
8007
 
          (insert "{ "))
8008
 
         ((equal e '(:endgroup))
8009
 
          (setq ingroup nil cnt 0)
8010
 
          (insert "}\n"))
8011
 
         (t
8012
 
          (setq tg (car e) c (cdr e))
8013
 
          (if ingroup (push tg (car groups)))
8014
 
          (setq tg (org-add-props tg nil 'face
8015
 
                                  (org-get-todo-face tg)))
8016
 
          (if (and (= cnt 0) (not ingroup)) (insert "  "))
8017
 
          (insert "[" c "] " tg (make-string
8018
 
                                 (- fwidth 4 (length tg)) ?\ ))
8019
 
          (when (= (setq cnt (1+ cnt)) ncol)
8020
 
            (insert "\n")
8021
 
            (if ingroup (insert "  "))
8022
 
            (setq cnt 0)))))
8023
 
      (insert "\n")
8024
 
      (goto-char (point-min))
8025
 
      (if (and (not expert) (fboundp 'fit-window-to-buffer))
8026
 
          (fit-window-to-buffer))
8027
 
      (message "[a-z..]:Set [SPC]:clear")
8028
 
      (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
8029
 
      (cond
8030
 
       ((or (= c ?\C-g)
8031
 
            (and (= c ?q) (not (rassoc c fulltable))))
8032
 
        (setq quit-flag t))
8033
 
       ((= c ?\ ) nil)
8034
 
       ((setq e (rassoc c fulltable) tg (car e))
8035
 
        tg)
8036
 
       (t (setq quit-flag t))))))
 
8794
         ((or (= c ?\C-g)
 
8795
              (and (= c ?q) (not (rassoc c fulltable))))
 
8796
          (setq quit-flag t))
 
8797
         ((= c ?\ ) nil)
 
8798
         ((setq e (rassoc c fulltable) tg (car e))
 
8799
          tg)
 
8800
         (t (setq quit-flag t)))))))
8037
8801
 
8038
8802
(defun org-entry-is-todo-p ()
8039
8803
  (member (org-get-todo-state) org-not-done-keywords))
8096
8860
         (org-log-done nil)
8097
8861
         (org-todo-log-states nil)
8098
8862
         (nshiftmax 10) (nshift 0)
8099
 
         re type n what ts mb0 time)
 
8863
         re type n what ts time)
8100
8864
    (when repeat
8101
8865
      (if (eq org-log-repeat t) (setq org-log-repeat 'state))
8102
8866
      (org-todo (if (eq interpret 'type) last-state head))
8119
8883
              re (save-excursion (outline-next-heading) (point)) t)
8120
8884
        (setq type (if (match-end 1) org-scheduled-string
8121
8885
                     (if (match-end 3) org-deadline-string "Plain:"))
8122
 
              ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
8123
 
              mb0 (match-beginning 0))
 
8886
              ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0))))
8124
8887
        (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
8125
8888
          (setq n (string-to-number (match-string 2 ts))
8126
8889
                what (match-string 3 ts))
8166
8929
        (kwd-re
8167
8930
         (cond ((null arg) org-not-done-regexp)
8168
8931
               ((equal arg '(4))
8169
 
                (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
 
8932
                (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
8170
8933
                                            (mapcar 'list org-todo-keywords-1))))
8171
8934
                  (concat "\\("
8172
8935
                          (mapconcat 'identity (org-split-string kwd "|") "\\|")
8344
9107
    (save-excursion
8345
9108
      (when findpos
8346
9109
        (org-back-to-heading t)
8347
 
        (narrow-to-region (point) (save-excursion 
 
9110
        (narrow-to-region (point) (save-excursion
8348
9111
                                    (outline-next-heading) (point)))
8349
 
        (while (re-search-forward
8350
 
                (concat "\\(" org-drawer-regexp "\\|" org-property-end-re "\\)")
8351
 
                (point-max) t) (forward-line))
8352
9112
        (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
8353
9113
                            "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
8354
9114
                            "[^\r\n]*\\)?"))
8355
9115
        (goto-char (match-end 0))
 
9116
        (when (and org-log-state-notes-insert-after-drawers
 
9117
                   (save-excursion
 
9118
                     (forward-line) (looking-at org-drawer-regexp)))
 
9119
            (progn (forward-line)
 
9120
                   (while (looking-at org-drawer-regexp)
 
9121
                     (goto-char (match-end 0))
 
9122
                     (re-search-forward org-property-end-re (point-max) t)
 
9123
                     (forward-line))
 
9124
                   (forward-line -1)))
8356
9125
        (unless org-log-states-order-reversed
8357
9126
          (and (= (char-after) ?\n) (forward-char 1))
8358
9127
          (org-skip-over-state-notes)
8383
9152
  (org-switch-to-buffer-other-window "*Org Note*")
8384
9153
  (erase-buffer)
8385
9154
  (if (memq org-log-note-how '(time state))
8386
 
      (org-store-log-note)
 
9155
      (let (current-prefix-arg) (org-store-log-note))
8387
9156
    (let ((org-inhibit-startup t)) (org-mode))
8388
9157
    (insert (format "# Insert note for %s.
8389
9158
# Finish with C-c C-c, or cancel with C-c C-k.\n\n"
8472
9241
     ((equal ans ?T)
8473
9242
      (call-interactively 'org-tags-sparse-tree))
8474
9243
     ((member ans '(?p ?P))
8475
 
      (setq kwd (completing-read "Property: "
 
9244
      (setq kwd (org-ido-completing-read "Property: "
8476
9245
                                 (mapcar 'list (org-buffer-property-keys))))
8477
 
      (setq value (completing-read "Value: "
 
9246
      (setq value (org-ido-completing-read "Value: "
8478
9247
                                   (mapcar 'list (org-property-values kwd))))
8479
9248
      (unless (string-match "\\`{.*}\\'" value)
8480
9249
        (setq value (concat "\"" value "\"")))
8626
9395
  (setq action (or action 'set))
8627
9396
  (let (current new news have remove)
8628
9397
    (save-excursion
8629
 
      (org-back-to-heading)
 
9398
      (org-back-to-heading t)
8630
9399
      (if (looking-at org-priority-regexp)
8631
9400
          (setq current (string-to-char (match-string 2))
8632
9401
                have t)
8702
9471
MATCHER is a Lisp form to be evaluated, testing if a given set of tags
8703
9472
qualifies a headline for inclusion.  When TODO-ONLY is non-nil,
8704
9473
only lines with a TODO keyword are included in the output."
 
9474
  (require 'org-agenda)
8705
9475
  (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
8706
9476
                     (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
8707
9477
                     (org-re
8751
9521
          ;; compile tags for current headline
8752
9522
          (setq tags-list
8753
9523
                (if org-use-tag-inheritance
8754
 
                    (apply 'append (mapcar 'cdr tags-alist))
 
9524
                    (apply 'append (mapcar 'cdr (reverse tags-alist)))
8755
9525
                  tags))
 
9526
          (when org-use-tag-inheritance
 
9527
            (setcdr (car tags-alist)
 
9528
                    (mapcar (lambda (x)
 
9529
                              (setq x (copy-sequence x))
 
9530
                              (org-add-prop-inherited x))
 
9531
                            (cdar tags-alist))))
8756
9532
          (when (and tags org-use-tag-inheritance
8757
9533
                     (not (eq t org-use-tag-inheritance)))
8758
9534
            ;; selective inheritance, remove uninherited ones
8759
9535
            (setcdr (car tags-alist)
8760
9536
                    (org-remove-uniherited-tags (cdar tags-alist))))
8761
 
          (when (and (or (not todo-only) (member todo org-not-done-keywords))
 
9537
          (when (and (or (not todo-only)
 
9538
                         (and (member todo org-not-done-keywords)
 
9539
                              (or (not org-agenda-tags-todo-honor-ignore-options)
 
9540
                                  (not (org-agenda-check-for-timestamp-as-reason-to-ignore-todo-item)))))
8762
9541
                     (let ((case-fold-search t)) (eval matcher))
8763
9542
                     (or
8764
9543
                      (not (member org-archive-tag tags-list))
8783
9562
                          (if org-tags-match-list-sublevels
8784
9563
                              (make-string (1- level) ?.) "")
8785
9564
                          (org-get-heading))
8786
 
                         category tags-list)
 
9565
                         category (org-get-tags-at))
8787
9566
                    priority (org-get-priority txt))
8788
9567
              (goto-char lspos)
8789
9568
              (setq marker (org-agenda-new-marker))
8808
9587
(defun org-remove-uniherited-tags (tags)
8809
9588
  "Remove all tags that are not inherited from the list TAGS."
8810
9589
  (cond
8811
 
   ((eq org-use-tag-inheritance t) tags)
 
9590
   ((eq org-use-tag-inheritance t)
 
9591
    (if org-tags-exclude-from-inheritance
 
9592
        (org-delete-all org-tags-exclude-from-inheritance tags)
 
9593
      tags))
8812
9594
   ((not org-use-tag-inheritance) nil)
8813
9595
   ((stringp org-use-tag-inheritance)
8814
9596
    (delq nil (mapcar
8815
 
               (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
 
9597
               (lambda (x)
 
9598
                 (if (and (string-match org-use-tag-inheritance x)
 
9599
                          (not (member x org-tags-exclude-from-inheritance)))
 
9600
                     x nil))
8816
9601
               tags)))
8817
9602
   ((listp org-use-tag-inheritance)
8818
9603
    (delq nil (mapcar
8819
 
               (lambda (x) (if (member x org-use-tag-inheritance) x nil))
 
9604
               (lambda (x)
 
9605
                 (if (member x org-use-tag-inheritance) x nil))
8820
9606
               tags)))))
8821
9607
 
8822
9608
(defvar todo-only) ;; dynamically scoped
8823
9609
 
8824
9610
(defun org-tags-sparse-tree (&optional todo-only match)
8825
 
  "Create a sparse tree according to tags  string MATCH.
 
9611
  "Create a sparse tree according to tags string MATCH.
8826
9612
MATCH can contain positive and negative selection of tags, like
8827
9613
\"+WORK+URGENT-WITHBOSS\".
8828
 
If optional argument TODO_ONLY is non-nil, only select lines that are
 
9614
If optional argument TODO-ONLY is non-nil, only select lines that are
8829
9615
also TODO lines."
8830
9616
  (interactive "P")
8831
9617
  (org-prepare-agenda-buffers (list (current-buffer)))
8865
9651
(defun org-make-tags-matcher (match)
8866
9652
  "Create the TAGS//TODO matcher form for the selection string MATCH."
8867
9653
  ;; todo-only is scoped dynamically into this function, and the function
8868
 
  ;; may change it it the matcher asksk for it.
 
9654
  ;; may change it if the matcher asks for it.
8869
9655
  (unless match
8870
9656
    ;; Get a new match request, with completion
8871
9657
    (let ((org-last-tags-completion-table
8872
9658
           (org-global-tags-completion-table)))
8873
 
      (setq match (completing-read
 
9659
      (setq match (org-completing-read-no-ido
8874
9660
                   "Match: " 'org-tags-completion-function nil nil nil
8875
9661
                   'org-tags-history))))
8876
9662
 
8880
9666
        minus tag mm
8881
9667
        tagsmatch todomatch tagsmatcher todomatcher kwd matcher
8882
9668
        orterms term orlist re-p str-p level-p level-op time-p
8883
 
        prop-p pn pv po cat-p gv)
 
9669
        prop-p pn pv po cat-p gv rest)
8884
9670
    (if (string-match "/+" match)
8885
9671
        ;; match contains also a todo-matching request
8886
9672
        (progn
8901
9687
        (while (and (equal (substring term -1) "\\") orterms)
8902
9688
          (setq term (concat term "|" (pop orterms)))) ; repair bad split
8903
9689
        (while (string-match re term)
8904
 
          (setq minus (and (match-end 1)
 
9690
          (setq rest (substring term (match-end 0))
 
9691
                minus (and (match-end 1)
8905
9692
                           (equal (match-string 1 term) "-"))
8906
9693
                tag (match-string 2 term)
8907
9694
                re-p (equal (string-to-char tag) ?{)
8920
9707
                           cat-p (equal pn "CATEGORY")
8921
9708
                           re-p (equal (string-to-char pv) ?{)
8922
9709
                           str-p (equal (string-to-char pv) ?\")
8923
 
                           time-p (save-match-data (string-match "^\"<.*>\"$" pv))
 
9710
                           time-p (save-match-data
 
9711
                                    (string-match "^\"[[<].*[]>]\"$" pv))
8924
9712
                           pv (if (or re-p str-p) (substring pv 1 -1) pv))
8925
9713
                     (if time-p (setq pv (org-matcher-time pv)))
8926
9714
                     (setq po (org-op-to-function po (if time-p 'time str-p)))
8927
 
                     (if (equal pn "CATEGORY")
8928
 
                         (setq gv '(get-text-property (point) 'org-category))
8929
 
                       (setq gv `(org-cached-entry-get nil ,pn)))
 
9715
                     (cond
 
9716
                      ((equal pn "CATEGORY")
 
9717
                       (setq gv '(get-text-property (point) 'org-category)))
 
9718
                      ((equal pn "TODO")
 
9719
                       (setq gv 'todo))
 
9720
                      (t
 
9721
                       (setq gv `(org-cached-entry-get nil ,pn))))
8930
9722
                     (if re-p
8931
9723
                         (if (eq po 'org<>)
8932
9724
                             `(not (string-match ,pv (or ,gv "")))
8937
9729
                               ,(string-to-number pv) ))))
8938
9730
                    (t `(member ,(downcase tag) tags-list)))
8939
9731
                mm (if minus (list 'not mm) mm)
8940
 
                term (substring term (match-end 0)))
 
9732
                term rest)
8941
9733
          (push mm tagsmatcher))
8942
9734
        (push (if (> (length tagsmatcher) 1)
8943
9735
                  (cons 'and tagsmatcher)
8994
9786
(defun org-string>= (a b) (not (string< a b)))
8995
9787
(defun org-string>  (a b) (and (not (string= a b)) (not (string< a b))))
8996
9788
(defun org-string<> (a b) (not (string= a b)))
8997
 
(defun org-time=  (a b) (=     (org-2ft a) (org-2ft b)))
8998
 
(defun org-time<  (a b) (<     (org-2ft a) (org-2ft b)))
8999
 
(defun org-time<= (a b) (<=    (org-2ft a) (org-2ft b)))
9000
 
(defun org-time>  (a b) (>     (org-2ft a) (org-2ft b)))
9001
 
(defun org-time>= (a b) (>=    (org-2ft a) (org-2ft b)))
9002
 
(defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
 
9789
(defun org-time=  (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (=     a b)))
 
9790
(defun org-time<  (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<     a b)))
 
9791
(defun org-time<= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (<=    a b)))
 
9792
(defun org-time>  (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>     a b)))
 
9793
(defun org-time>= (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (>=    a b)))
 
9794
(defun org-time<> (a b) (setq a (org-2ft a) b (org-2ft b)) (and (> a 0) (> b 0) (org<> a b)))
9003
9795
(defun org-2ft (s)
9004
9796
  "Convert S to a floating point time.
9005
9797
If S is already a number, just return it.  If it is a string, parse
9006
 
it as a time string and apply `float-time' to it.  f S is nil, just return 0."
 
9798
it as a time string and apply `float-time' to it.  If S is nil, just return 0."
9007
9799
  (cond
9008
9800
   ((numberp s) s)
9009
9801
   ((stringp s)
9012
9804
      (error 0.)))
9013
9805
   (t 0.)))
9014
9806
 
 
9807
(defun org-time-today ()
 
9808
  "Time in seconds today at 0:00.
 
9809
Returns the float number of seconds since the beginning of the
 
9810
epoch to the beginning of today (00:00)."
 
9811
  (float-time (apply 'encode-time
 
9812
                     (append '(0 0 0) (nthcdr 3 (decode-time))))))
 
9813
 
9015
9814
(defun org-matcher-time (s)
9016
 
  (cond
9017
 
   ((equal s "<now>") (float-time))
9018
 
   ((equal s "<today>")
9019
 
    (float-time (append '(0 0 0) (nthcdr 3 (decode-time)))))
9020
 
   (t (org-2ft s))))
 
9815
  "Interpret a time comparison value."
 
9816
  (save-match-data
 
9817
    (cond
 
9818
     ((string= s "<now>") (float-time))
 
9819
     ((string= s "<today>") (org-time-today))
 
9820
     ((string= s "<tomorrow>")   (+ 86400.0 (org-time-today)))
 
9821
     ((string= s "<yesterday>")  (- (org-time-today) 86400.0))
 
9822
     ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
 
9823
      (+ (org-time-today)
 
9824
         (* (string-to-number (match-string 1 s))
 
9825
            (cdr (assoc (match-string 2 s)
 
9826
                        '(("d" . 86400.0)   ("w" . 604800.0)
 
9827
                          ("m" . 2678400.0) ("y" . 31557600.0)))))))
 
9828
     (t (org-2ft s)))))
9021
9829
 
9022
9830
(defun org-match-any-p (re list)
9023
9831
  "Does re match any element of list?"
9024
9832
  (setq list (mapcar (lambda (x) (string-match re x)) list))
9025
9833
  (delq nil list))
9026
9834
 
9027
 
(defvar org-add-colon-after-tag-completion nil)  ;; dynamically skoped param
 
9835
(defvar org-add-colon-after-tag-completion nil)  ;; dynamically scoped param
9028
9836
(defvar org-tags-overlay (org-make-overlay 1 1))
9029
9837
(org-detach-overlay org-tags-overlay)
9030
9838
 
9059
9867
                    (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
9060
9868
                      (setq ltags (org-split-string
9061
9869
                                   (org-match-string-no-properties 1) ":"))
 
9870
                      (when parent
 
9871
                        (setq ltags (mapcar 'org-add-prop-inherited ltags)))
9062
9872
                      (setq tags (append
9063
9873
                                  (if parent
9064
9874
                                      (org-remove-uniherited-tags ltags)
9071
9881
              (error nil)))))
9072
9882
      (append (org-remove-uniherited-tags org-file-tags) tags))))
9073
9883
 
 
9884
(defun org-add-prop-inherited (s)
 
9885
  (add-text-properties 0 (length s) '(inherited t) s)
 
9886
  s)
 
9887
 
9074
9888
(defun org-toggle-tag (tag &optional onoff)
9075
9889
  "Toggle the tag TAG for the current line.
9076
9890
If ONOFF is `on' or `off', don't toggle but set to this state."
9077
 
  (unless (org-on-heading-p t) (error "Not on headling"))
9078
9891
  (let (res current)
9079
9892
    (save-excursion
9080
 
      (beginning-of-line)
 
9893
      (org-back-to-heading t)
9081
9894
      (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
9082
9895
                             (point-at-eol) t)
9083
9896
          (progn
9127
9940
          (org-move-to-column (min ncol col) t))
9128
9941
      (goto-char pos))))
9129
9942
 
 
9943
(defun org-set-tags-command (&optional arg just-align)
 
9944
  "Call the set-tags command for the current entry."
 
9945
  (interactive "P")
 
9946
  (if (org-on-heading-p)
 
9947
      (org-set-tags arg just-align)
 
9948
    (save-excursion
 
9949
      (org-back-to-heading t)
 
9950
      (org-set-tags arg just-align))))
 
9951
 
9130
9952
(defun org-set-tags (&optional arg just-align)
9131
9953
  "Set the tags for the current headline.
9132
9954
With prefix ARG, realign all tags in headings in the current buffer."
9165
9987
                  (let ((org-add-colon-after-tag-completion t))
9166
9988
                    (org-trim
9167
9989
                     (org-without-partial-completion
9168
 
                      (completing-read "Tags: " 'org-tags-completion-function
 
9990
                      (org-ido-completing-read "Tags: " 'org-tags-completion-function
9169
9991
                                       nil nil current 'org-tags-history)))))))
9170
9992
        (while (string-match "[-+&]+" tags)
9171
9993
          ;; No boolean logic, just a list
9208
10030
                (if (org-mode-p)
9209
10031
                    (org-get-buffer-tags)
9210
10032
                  (org-global-tags-completion-table))))
9211
 
           (completing-read
 
10033
           (org-ido-completing-read
9212
10034
            "Tag: " 'org-tags-completion-function nil nil nil
9213
10035
            'org-tags-history))
9214
10036
         (progn
9266
10088
    ))
9267
10089
 
9268
10090
(defun org-fast-tag-insert (kwd tags face &optional end)
9269
 
  "Insert KDW, and the TAGS, the latter with face FACE.  Also inser END."
 
10091
  "Insert KDW, and the TAGS, the latter with face FACE.  Also insert END."
9270
10092
  (insert (format "%-12s" (concat kwd ":"))
9271
10093
          (org-add-props (mapconcat 'identity tags " ") nil 'face face)
9272
10094
          (or end "")))
9387
10209
      (setq ntable (nreverse ntable))
9388
10210
      (insert "\n")
9389
10211
      (goto-char (point-min))
9390
 
      (if (and (not expert) (fboundp 'fit-window-to-buffer))
9391
 
          (fit-window-to-buffer))
 
10212
      (if (not expert) (org-fit-window-to-buffer))
9392
10213
      (setq rtn
9393
10214
            (catch 'exit
9394
10215
              (while t
9410
10231
                    (delete-other-windows)
9411
10232
                    (split-window-vertically)
9412
10233
                    (org-switch-to-buffer-other-window " *Org tags*")
9413
 
                    (and (fboundp 'fit-window-to-buffer)
9414
 
                         (fit-window-to-buffer))))
 
10234
                    (org-fit-window-to-buffer)))
9415
10235
                 ((or (= c ?\C-g)
9416
10236
                      (and (= c ?q) (not (rassoc c ntable))))
9417
10237
                  (org-detach-overlay org-tags-overlay)
9421
10241
                  (if exit-after-next (setq exit-after-next 'now)))
9422
10242
                 ((= c ?\t)
9423
10243
                  (condition-case nil
9424
 
                      (setq tg (completing-read
 
10244
                      (setq tg (org-ido-completing-read
9425
10245
                                "Tag: "
9426
10246
                                (or buffer-tags
9427
10247
                                    (with-current-buffer buf
9546
10366
         (org-agenda-skip-function
9547
10367
          (car (org-delete-all '(comment archive) skip)))
9548
10368
         (org-tags-match-list-sublevels t)
9549
 
         matcher pos file
 
10369
         matcher file res
9550
10370
         org-todo-keywords-for-agenda
9551
10371
         org-done-keywords-for-agenda
9552
10372
         org-todo-keyword-alist-for-agenda
9555
10375
    (cond
9556
10376
     ((eq match t)   (setq matcher t))
9557
10377
     ((eq match nil) (setq matcher t))
9558
 
     (t (setq matcher (if match (org-make-tags-matcher match) t))))
9559
 
    
9560
 
    (when (eq scope 'tree)
9561
 
      (org-back-to-heading t)
9562
 
      (org-narrow-to-subtree)
9563
 
      (setq scope nil))
9564
 
    
9565
 
    (if (not scope)
9566
 
        (progn
9567
 
          (org-prepare-agenda-buffers
9568
 
           (list (buffer-file-name (current-buffer))))
9569
 
          (org-scan-tags func matcher))
9570
 
      ;; Get the right scope
9571
 
      (setq pos (point))
9572
 
      (cond
9573
 
       ((and scope (listp scope) (symbolp (car scope)))
9574
 
        (setq scope (eval scope)))
9575
 
       ((eq scope 'agenda)
9576
 
        (setq scope (org-agenda-files t)))
9577
 
       ((eq scope 'agenda-with-archives)
9578
 
        (setq scope (org-agenda-files t))
9579
 
        (setq scope (org-add-archive-files scope)))
9580
 
       ((eq scope 'file)
9581
 
        (setq scope (list (buffer-file-name))))
9582
 
       ((eq scope 'file-with-archives)
9583
 
        (setq scope (org-add-archive-files (list (buffer-file-name))))))
9584
 
      (org-prepare-agenda-buffers scope)
9585
 
      (while (setq file (pop scope))
9586
 
        (with-current-buffer (org-find-base-buffer-visiting file)
9587
 
          (save-excursion
9588
 
            (save-restriction
9589
 
              (widen)
9590
 
              (goto-char (point-min))
9591
 
              (org-scan-tags func matcher))))))))
 
10378
     (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
 
10379
 
 
10380
    (save-excursion
 
10381
      (save-restriction
 
10382
        (when (eq scope 'tree)
 
10383
          (org-back-to-heading t)
 
10384
          (org-narrow-to-subtree)
 
10385
          (setq scope nil))
 
10386
 
 
10387
        (if (not scope)
 
10388
            (progn
 
10389
              (org-prepare-agenda-buffers
 
10390
               (list (buffer-file-name (current-buffer))))
 
10391
              (setq res (org-scan-tags func matcher)))
 
10392
          ;; Get the right scope
 
10393
          (cond
 
10394
           ((and scope (listp scope) (symbolp (car scope)))
 
10395
            (setq scope (eval scope)))
 
10396
           ((eq scope 'agenda)
 
10397
            (setq scope (org-agenda-files t)))
 
10398
           ((eq scope 'agenda-with-archives)
 
10399
            (setq scope (org-agenda-files t))
 
10400
            (setq scope (org-add-archive-files scope)))
 
10401
           ((eq scope 'file)
 
10402
            (setq scope (list (buffer-file-name))))
 
10403
           ((eq scope 'file-with-archives)
 
10404
            (setq scope (org-add-archive-files (list (buffer-file-name))))))
 
10405
          (org-prepare-agenda-buffers scope)
 
10406
          (while (setq file (pop scope))
 
10407
            (with-current-buffer (org-find-base-buffer-visiting file)
 
10408
              (save-excursion
 
10409
                (save-restriction
 
10410
                  (widen)
 
10411
                  (goto-char (point-min))
 
10412
                  (setq res (append res (org-scan-tags func matcher))))))))))
 
10413
    res))
9592
10414
 
9593
10415
;;;; Properties
9594
10416
 
9595
10417
;;; Setting and retrieving properties
9596
10418
 
9597
10419
(defconst org-special-properties
9598
 
  '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
 
10420
  '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
9599
10421
    "TIMESTAMP" "TIMESTAMP_IA")
9600
10422
  "The special properties valid in Org-mode.
9601
10423
 
9606
10428
  '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
9607
10429
    "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
9608
10430
    "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
9609
 
    "EXPORT_FILE_NAME" "EXPORT_TITLE")
 
10431
    "EXPORT_FILE_NAME" "EXPORT_TITLE" "ORDERED")
9610
10432
  "Some properties that are used by Org-mode for various purposes.
9611
10433
Being in this list makes sure that they are offered for completion.")
9612
10434
 
9703
10525
          (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
9704
10526
          beg end range props sum-props key value string clocksum)
9705
10527
      (save-excursion
9706
 
        (when (condition-case nil (org-back-to-heading t) (error nil))
 
10528
        (when (condition-case nil
 
10529
                  (and (org-mode-p) (org-back-to-heading t))
 
10530
                (error nil))
9707
10531
          (setq beg (point))
9708
10532
          (setq sum-props (get-text-property (point) 'org-summaries))
9709
10533
          (setq clocksum (get-text-property (point) :org-clock-minutes))
9756
10580
                          (org-columns-number-to-string (/ (float clocksum) 60.)
9757
10581
                                                       'add_times))
9758
10582
                    props))
 
10583
          (unless (assoc "CATEGORY" props)
 
10584
            (setq value (or (org-get-category)
 
10585
                            (progn (org-refresh-category-properties)
 
10586
                                   (org-get-category))))
 
10587
            (push (cons "CATEGORY" value) props))
9759
10588
          (append sum-props (nreverse props)))))))
9760
10589
 
9761
10590
(defun org-entry-get (pom property &optional inherit)
9778
10607
          (if (and range
9779
10608
                   (goto-char (car range))
9780
10609
                   (re-search-forward
9781
 
                    (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
 
10610
                    (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
9782
10611
                    (cdr range) t))
9783
10612
              ;; Found the property, return it.
9784
10613
              (if (match-end 1)
9802
10631
        (if (and range
9803
10632
                 (goto-char (car range))
9804
10633
                 (re-search-forward
9805
 
                  (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
 
10634
                  (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
9806
10635
                  (cdr range) t))
9807
10636
            (progn
9808
10637
              (delete-region (match-beginning 0) (1+ (point-at-eol)))
9870
10699
  s)
9871
10700
 
9872
10701
(defvar org-entry-property-inherited-from (make-marker)
9873
 
  "Marker pointing to the entry from where a proerty was inherited.
 
10702
  "Marker pointing to the entry from where a property was inherited.
9874
10703
Each call to `org-entry-get-with-inheritance' will set this marker to the
9875
 
location of the entry where the inheriance search matched.  If there was
 
10704
location of the entry where the inheritance search matched.  If there was
9876
10705
no match, the marker will point nowhere.
9877
10706
Note that also `org-entry-get' calls this function, if the INHERIT flag
9878
10707
is set.")
9891
10720
              (move-marker org-entry-property-inherited-from (point))
9892
10721
              (throw 'ex tmp))
9893
10722
            (or (org-up-heading-safe) (throw 'ex nil)))))
9894
 
      (or tmp 
 
10723
      (or tmp
9895
10724
          (cdr (assoc property org-file-properties))
9896
10725
          (cdr (assoc property org-global-properties))
9897
10726
          (cdr (assoc property org-global-properties-fixed))))))
9952
10781
 
9953
10782
(defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
9954
10783
  "Get all property keys in the current buffer.
9955
 
With INCLUDE-SPECIALS, also list the special properties that relect things
 
10784
With INCLUDE-SPECIALS, also list the special properties that reflect things
9956
10785
like tags and TODO state.
9957
10786
With INCLUDE-DEFAULTS, also include properties that has special meaning
9958
10787
internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
9959
10788
With INCLUDE-COLUMNS, also include property names given in COLUMN
9960
10789
formats in the current buffer."
9961
 
  (let (rtn range cfmt cols s p)
 
10790
  (let (rtn range cfmt s p)
9962
10791
    (save-excursion
9963
10792
      (save-restriction
9964
10793
        (widen)
10046
10875
  "In the current entry, set PROPERTY to VALUE.
10047
10876
When called interactively, this will prompt for a property name, offering
10048
10877
completion on existing and default properties.  And then it will prompt
10049
 
for a value, offering competion either on allowed values (via an inherited
 
10878
for a value, offering completion either on allowed values (via an inherited
10050
10879
xxx_ALL property) or on existing values in other instances of this property
10051
10880
in the current file."
10052
10881
  (interactive
10053
10882
   (let* ((completion-ignore-case t)
10054
10883
          (keys (org-buffer-property-keys nil t t))
10055
 
          (prop0 (completing-read "Property: " (mapcar 'list keys)))
 
10884
          (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
10056
10885
          (prop (if (member prop0 keys)
10057
10886
                    prop0
10058
10887
                  (or (cdr (assoc (downcase prop0)
10064
10893
          (existing (mapcar 'list (org-property-values prop)))
10065
10894
          (val (if allowed
10066
10895
                   (org-completing-read "Value: " allowed nil 'req-match)
10067
 
                 (org-completing-read
 
10896
                 (org-completing-read-no-ido
10068
10897
                  (concat "Value" (if (and cur (string-match "\\S-" cur))
10069
10898
                                      (concat "[" cur "]") "")
10070
10899
                          ": ")
10077
10906
  "In the current entry, delete PROPERTY."
10078
10907
  (interactive
10079
10908
   (let* ((completion-ignore-case t)
10080
 
          (prop (completing-read
 
10909
          (prop (org-ido-completing-read
10081
10910
                 "Property: " (org-entry-properties nil 'standard))))
10082
10911
     (list prop)))
10083
10912
  (message "Property %s %s" property
10089
10918
  "Remove PROPERTY globally, from all entries."
10090
10919
  (interactive
10091
10920
   (let* ((completion-ignore-case t)
10092
 
          (prop (completing-read
 
10921
          (prop (org-ido-completing-read
10093
10922
                 "Globally remove property: "
10094
10923
                 (mapcar 'list (org-buffer-property-keys)))))
10095
10924
     (list prop)))
10110
10939
(defun org-compute-property-at-point ()
10111
10940
  "Compute the property at point.
10112
10941
This looks for an enclosing column format, extracts the operator and
10113
 
then applies it to the proerty in the column format's scope."
 
10942
then applies it to the property in the column format's scope."
10114
10943
  (interactive)
10115
10944
  (unless (org-at-property-p)
10116
10945
    (error "Not at a property"))
10183
11012
IDENT can be a string, a symbol or a number, this function will search for
10184
11013
the string representation of it.
10185
11014
Return the position where this entry starts, or nil if there is no such entry."
 
11015
  (interactive "sID: ")
10186
11016
  (let ((id (cond
10187
11017
             ((stringp ident) ident)
10188
11018
             ((symbol-name ident) (symbol-name ident))
10452
11282
(defvar defdecode)
10453
11283
(defvar with-time)
10454
11284
(defun org-read-date-display ()
10455
 
  "Display the currrent date prompt interpretation in the minibuffer."
 
11285
  "Display the current date prompt interpretation in the minibuffer."
10456
11286
  (when org-read-date-display-live
10457
11287
    (when org-read-date-overlay
10458
11288
      (org-delete-overlay org-read-date-overlay))
10485
11315
      (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
10486
11316
 
10487
11317
(defun org-read-date-analyze (ans def defdecode)
10488
 
  "Analyze the combined answer of the date prompt."
 
11318
  "Analyse the combined answer of the date prompt."
10489
11319
  ;; FIXME: cleanup and comment
10490
11320
  (let (delta deltan deltaw deltadef year month day
10491
11321
              hour minute second wday pm h2 m2 tl wday1
10501
11331
            deltadef (nth 2 delta)))
10502
11332
 
10503
11333
    ;; Check if there is an iso week date in there
10504
 
    ;; If yes, sore the info and ostpone interpreting it until the rest
 
11334
    ;; If yes, sore the info and postpone interpreting it until the rest
10505
11335
    ;; of the parsing is done
10506
11336
    (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
10507
11337
      (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
10942
11772
  "Convert a time stamp to an absolute day number.
10943
11773
If there is a specifyer for a cyclic time stamp, get the closest date to
10944
11774
DAYNR.
10945
 
PREFER and SHOW_ALL are passed through to `org-closest-date'."
 
11775
PREFER and SHOW-ALL are passed through to `org-closest-date'."
10946
11776
  (cond
10947
11777
   ((and daynr (string-match "\\`%%\\((.*)\\)" s))
10948
11778
    (if (org-diary-sexp-entry (match-string 1 s) "" date)
10962
11792
(defun org-small-year-to-year (year)
10963
11793
  "Convert 2-digit years into 4-digit years.
10964
11794
38-99 are mapped into 1938-1999.  1-37 are mapped into 2001-2007.
10965
 
The year 2000 cannot be abbreviated.  Any year lager than 99
10966
 
is retrned unchanged."
 
11795
The year 2000 cannot be abbreviated.  Any year larger than 99
 
11796
is returned unchanged."
10967
11797
  (if (< year 38)
10968
11798
      (setq year (+ 2000 year))
10969
11799
    (if (< year 100)
11033
11863
  "Find the date closest to CURRENT that is consistent with START and CHANGE.
11034
11864
When PREFER is `past' return a date that is either CURRENT or past.
11035
11865
When PREFER is `future', return a date that is either CURRENT or future.
11036
 
When SHOW-ALL is nil, only return the current occurence of a time stamp."
 
11866
When SHOW-ALL is nil, only return the current occurrence of a time stamp."
11037
11867
  ;; Make the proper lists from the dates
11038
11868
  (catch 'exit
11039
11869
    (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
11040
 
          dn dw sday cday n1 n2
 
11870
          dn dw sday cday n1 n2 n0
11041
11871
          d m y y1 y2 date1 date2 nmonths nm ny m2)
11042
11872
 
11043
11873
      (setq start (org-date-to-gregorian start)
11086
11916
          (setq m2 (+ m dn) y2 y)
11087
11917
          (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
11088
11918
          (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
 
11919
      ;; Make sure n1 is the earlier date
 
11920
      (setq n0 n1  n1 (min n1 n2)  n2 (max n0 n2))
11089
11921
      (if show-all
11090
11922
          (cond
11091
11923
           ((eq prefer 'past) n1)
11190
12022
  "Toggle the type (<active> or [inactive]) of a time stamp."
11191
12023
  (interactive)
11192
12024
  (when (org-at-timestamp-p t)
11193
 
    (save-excursion
11194
 
      (goto-char (match-beginning 0))
11195
 
      (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
11196
 
      (goto-char (1- (match-end 0)))
11197
 
      (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
11198
 
    (message "Timestamp is now %sactive"
11199
 
             (if (equal (char-before) ?>) "in" ""))))
 
12025
    (let ((beg (match-beginning 0)) (end (match-end 0))
 
12026
          (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
 
12027
      (save-excursion
 
12028
        (goto-char beg)
 
12029
        (while (re-search-forward "[][<>]" end t)
 
12030
          (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
 
12031
                         t t)))
 
12032
      (message "Timestamp is now %sactive"
 
12033
               (if (equal (char-after beg) ?<) "" "in")))))
11200
12034
 
11201
12035
(defun org-timestamp-change (n &optional what)
11202
12036
  "Change the date in the time stamp at point.
11391
12225
           "Switch-to: " nil t))
11392
12226
         (or enabled (iswitchb-mode -1))))))
11393
12227
 
 
12228
;;;###autoload
 
12229
(defun org-ido-switchb (&optional arg)
 
12230
  "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
 
12231
With a prefix argument, restrict available to files.
 
12232
With two prefix arguments, restrict available buffers to agenda files."
 
12233
  (interactive "P")
 
12234
  (let ((blist (cond ((equal arg '(4))  (org-buffer-list 'files))
 
12235
                     ((equal arg '(16)) (org-buffer-list 'agenda))
 
12236
                     (t                 (org-buffer-list)))))
 
12237
    (switch-to-buffer
 
12238
     (org-ido-completing-read "Org buffer: "
 
12239
                              (mapcar 'buffer-name blist)
 
12240
                              nil t))))
 
12241
 
11394
12242
(defun org-buffer-list (&optional predicate exclude-tmp)
11395
12243
  "Return a list of Org buffers.
11396
12244
PREDICATE can be `export', `files' or `agenda'.
11411
12259
            (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
11412
12260
           ((eq predicate 'agenda)
11413
12261
            (lambda (b)
11414
 
              (with-current-buffer b 
 
12262
              (with-current-buffer b
11415
12263
                (and (eq major-mode 'org-mode)
11416
12264
                     (setq bfn (buffer-file-name b))
11417
12265
                     (member (file-truename bfn) agenda-files)))))
11418
 
           (t (lambda (b) (with-current-buffer b 
 
12266
           (t (lambda (b) (with-current-buffer b
11419
12267
                            (or (eq major-mode 'org-mode)
11420
12268
                                (string-match "\*Org .*Export"
11421
12269
                                              (buffer-name b)))))))))
11484
12332
    (customize-variable 'org-agenda-files)))
11485
12333
 
11486
12334
(defun org-store-new-agenda-file-list (list)
11487
 
  "Set new value for the agenda file list and save it correcly."
 
12335
  "Set new value for the agenda file list and save it correctly."
11488
12336
  (if (stringp org-agenda-files)
11489
12337
      (let ((f org-agenda-files) b)
11490
12338
        (while (setq b (find-buffer-visiting f)) (kill-buffer b))
11633
12481
                (append org-done-keywords-for-agenda org-done-keywords))
11634
12482
          (setq org-todo-keyword-alist-for-agenda
11635
12483
                (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
11636
 
          (setq org-tag-alist-for-agenda 
 
12484
          (setq org-tag-alist-for-agenda
11637
12485
                (append org-tag-alist-for-agenda org-tag-alist))
11638
12486
 
11639
12487
          (save-excursion
11680
12528
      "Always return t in org-mode buffers.
11681
12529
This is because we want to insert math symbols without dollars even outside
11682
12530
the LaTeX math segments.  If Orgmode thinks that point is actually inside
11683
 
en embedded LaTeX fragement, let texmathp do its job.
 
12531
an embedded LaTeX fragment, let texmathp do its job.
11684
12532
\\[org-cdlatex-mode-map]"
11685
12533
      (interactive)
11686
12534
      (let (p)
11834
12682
  '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
11835
12683
    ;; ("$" "\\([       (]\\|^\\)\\(\\(\\([$]\\)\\([^   \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^       \r\n,.$]\\)\\4\\)\\)\\([        .,?;:'\")]\\|$\\)" 2 nil)
11836
12684
    ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
11837
 
    ("$" "\\([^$]\\)\\(\\(\\$\\([^      \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^        \r\n,.$]\\)\\$\\)\\)\\([        .,?;:'\")\000]\\|$\\)" 2 nil)
 
12685
    ("$1" "\\([^$]\\)\\(\\$[^   \r\n,;.$]\\$\\)\\([-    .,?;:'\")\000]\\|$\\)" 2 nil)
 
12686
    ("$" "\\([^$]\\)\\(\\(\\$\\([^      \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^        \r\n,.$]\\)\\$\\)\\)\\([-       .,?;:'\")\000]\\|$\\)" 2 nil)
11838
12687
    ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
11839
12688
    ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
11840
12689
    ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
11864
12713
      (when (member m matchers)
11865
12714
        (goto-char (point-min))
11866
12715
        (while (re-search-forward re nil t)
11867
 
          (when (or (not at) (equal (cdr at) (match-beginning n)))
 
12716
          (when (and (or (not at) (equal (cdr at) (match-beginning n)))
 
12717
                     (not (get-text-property (match-beginning n)
 
12718
                                             'org-protected)))
11868
12719
            (setq txt (match-string n)
11869
12720
                  beg (match-beginning n) end (match-end n)
11870
12721
                  cnt (1+ cnt)
11955
12806
  "Return string to be used as color value for an RGB component."
11956
12807
  (format "%g" (/ value 65535.0)))
11957
12808
 
11958
 
 
11959
12809
;;;; Key bindings
11960
12810
 
11961
12811
;; Make `C-c C-x' a prefix key
12042
12892
(org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
12043
12893
(org-defkey org-mode-map "\C-c\C-j" 'org-goto)
12044
12894
(org-defkey org-mode-map "\C-c\C-t" 'org-todo)
 
12895
(org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
12045
12896
(org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
12046
12897
(org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
12047
12898
(org-defkey org-mode-map "\C-c;"    'org-toggle-comment)
12095
12946
(org-defkey org-mode-map "\C-c\C-e" 'org-export)
12096
12947
(org-defkey org-mode-map "\C-c:"    'org-toggle-fixed-width-section)
12097
12948
(org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
 
12949
(org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
12098
12950
 
12099
12951
(org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
12100
12952
(org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
12112
12964
(org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
12113
12965
(org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
12114
12966
(org-defkey org-mode-map "\C-c\C-xp"    'org-set-property)
 
12967
(org-defkey org-mode-map "\C-c\C-xo"    'org-toggle-ordered-property)
12115
12968
(org-defkey org-mode-map "\C-c\C-xi"    'org-insert-columns-dblock)
12116
12969
 
 
12970
(org-defkey org-mode-map "\C-c\C-x."    'org-timer)
 
12971
(org-defkey org-mode-map "\C-c\C-x-"    'org-timer-item)
 
12972
(org-defkey org-mode-map "\C-c\C-x0"    'org-timer-start)
 
12973
(org-defkey org-mode-map "\C-c\C-x,"    'org-timer-pause-or-continue)
 
12974
 
12117
12975
(define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
12118
12976
 
12119
12977
(when (featurep 'xemacs)
12208
13066
(put 'orgtbl-self-insert-command 'delete-selection t)
12209
13067
(put 'org-delete-char 'delete-selection 'supersede)
12210
13068
(put 'org-delete-backward-char 'delete-selection 'supersede)
 
13069
(put 'org-yank 'delete-selection 'yank)
12211
13070
 
12212
13071
;; Make `flyspell-mode' delay after some commands
12213
13072
(put 'org-self-insert-command 'flyspell-delayed t)
12217
13076
 
12218
13077
;; Make pabbrev-mode expand after org-mode commands
12219
13078
(put 'org-self-insert-command 'pabbrev-expand-after-command t)
12220
 
(put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
 
13079
(put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
12221
13080
 
12222
13081
;; How to do this: Measure non-white length of current string
12223
13082
;; If equal to column width, we should realign.
12241
13100
             'delete-backward-char 'org-delete-backward-char)
12242
13101
  (org-defkey org-mode-map "|" 'org-force-self-insert))
12243
13102
 
12244
 
(defun org-shiftcursor-error ()
 
13103
(defun org-modifier-cursor-error ()
 
13104
  "Throw an error, a modified cursor command was applied in wrong context."
 
13105
  (error "This command is active in special context like tables, headlines or items"))
 
13106
 
 
13107
(defun org-shiftselect-error ()
12245
13108
  "Throw an error because Shift-Cursor command was applied in wrong context."
12246
 
  (error "This command is active in special context like tables, headlines or timestamps"))
 
13109
  (if (and (boundp 'shift-select-mode) shift-select-mode)
 
13110
      (error "To use shift-selection with Org-mode, customize `org-support-shift-select'.")
 
13111
    (error "This command works only in special context like headlines or timestamps.")))
 
13112
 
 
13113
(defun org-call-for-shift-select (cmd)
 
13114
  (let ((this-command-keys-shift-translated t))
 
13115
    (call-interactively cmd)))
12247
13116
 
12248
13117
(defun org-shifttab (&optional arg)
12249
13118
  "Global visibility cycling or move to previous table field.
12269
13138
   ((org-at-table-p) (call-interactively 'org-table-delete-column))
12270
13139
   ((org-on-heading-p) (call-interactively 'org-promote-subtree))
12271
13140
   ((org-at-item-p) (call-interactively 'org-outdent-item))
12272
 
   (t (org-shiftcursor-error))))
 
13141
   (t (org-modifier-cursor-error))))
12273
13142
 
12274
13143
(defun org-shiftmetaright ()
12275
13144
  "Demote subtree or insert table column.
12281
13150
   ((org-at-table-p) (call-interactively 'org-table-insert-column))
12282
13151
   ((org-on-heading-p) (call-interactively 'org-demote-subtree))
12283
13152
   ((org-at-item-p) (call-interactively 'org-indent-item))
12284
 
   (t (org-shiftcursor-error))))
 
13153
   (t (org-modifier-cursor-error))))
12285
13154
 
12286
13155
(defun org-shiftmetaup (&optional arg)
12287
13156
  "Move subtree up or kill table row.
12293
13162
   ((org-at-table-p) (call-interactively 'org-table-kill-row))
12294
13163
   ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
12295
13164
   ((org-at-item-p) (call-interactively 'org-move-item-up))
12296
 
   (t (org-shiftcursor-error))))
 
13165
   (t (org-modifier-cursor-error))))
12297
13166
(defun org-shiftmetadown (&optional arg)
12298
13167
  "Move subtree down or insert table row.
12299
13168
Calls `org-move-subtree-down' or `org-table-insert-row' or
12304
13173
   ((org-at-table-p) (call-interactively 'org-table-insert-row))
12305
13174
   ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
12306
13175
   ((org-at-item-p) (call-interactively 'org-move-item-down))
12307
 
   (t (org-shiftcursor-error))))
 
13176
   (t (org-modifier-cursor-error))))
12308
13177
 
12309
13178
(defun org-metaleft (&optional arg)
12310
13179
  "Promote heading or move table column to left.
12362
13231
depending on context.  See the individual commands for more information."
12363
13232
  (interactive "P")
12364
13233
  (cond
 
13234
   ((and org-support-shift-select (org-region-active-p))
 
13235
    (org-call-for-shift-select 'previous-line))
12365
13236
   ((org-at-timestamp-p t)
12366
13237
    (call-interactively (if org-edit-timestamp-down-means-later
12367
13238
                            'org-timestamp-down 'org-timestamp-up)))
12368
 
   ((org-on-heading-p) (call-interactively 'org-priority-up))
12369
 
   ((org-at-item-p) (call-interactively 'org-previous-item))
 
13239
   ((and (not (eq org-support-shift-select 'always))
 
13240
         (org-on-heading-p))
 
13241
    (call-interactively 'org-priority-up))
 
13242
   ((and (not org-support-shift-select) (org-at-item-p))
 
13243
    (call-interactively 'org-previous-item))
12370
13244
   ((org-clocktable-try-shift 'up arg))
12371
 
   (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
 
13245
   (org-support-shift-select
 
13246
    (org-call-for-shift-select 'previous-line))
 
13247
   (t (org-shiftselect-error))))
12372
13248
 
12373
13249
(defun org-shiftdown (&optional arg)
12374
13250
  "Decrease item in timestamp or decrease priority of current headline.
12376
13252
depending on context.  See the individual commands for more information."
12377
13253
  (interactive "P")
12378
13254
  (cond
 
13255
   ((and org-support-shift-select (org-region-active-p))
 
13256
    (org-call-for-shift-select 'next-line))
12379
13257
   ((org-at-timestamp-p t)
12380
13258
    (call-interactively (if org-edit-timestamp-down-means-later
12381
13259
                            'org-timestamp-up 'org-timestamp-down)))
12382
 
   ((org-on-heading-p) (call-interactively 'org-priority-down))
 
13260
   ((and (not (eq org-support-shift-select 'always))
 
13261
         (org-on-heading-p))
 
13262
    (call-interactively 'org-priority-down))
 
13263
   ((and (not org-support-shift-select) (org-at-item-p))
 
13264
    (call-interactively 'org-next-item))
12383
13265
   ((org-clocktable-try-shift 'down arg))
12384
 
   (t (call-interactively 'org-next-item))))
 
13266
   (org-support-shift-select 
 
13267
    (org-call-for-shift-select 'next-line))
 
13268
   (t (org-shiftselect-error))))
12385
13269
 
12386
13270
(defun org-shiftright (&optional arg)
12387
 
  "Next TODO keyword or timestamp one day later, depending on context."
 
13271
  "Cycle the thing at point or in the current line, depending on context.
 
13272
Depending on context, this does one of the following:
 
13273
 
 
13274
- switch a timestamp at point one day into the future
 
13275
- on a headline, switch to the next TODO keyword.
 
13276
- on an item, switch entire list to the next bullet type
 
13277
- on a property line, switch to the next allowed value
 
13278
- on a clocktable definition line, move time block into the future"
12388
13279
  (interactive "P")
12389
13280
  (cond
 
13281
   ((and org-support-shift-select (org-region-active-p))
 
13282
    (org-call-for-shift-select 'forward-char))
12390
13283
   ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
12391
 
   ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
12392
 
   ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
12393
 
   ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
 
13284
   ((and (not (eq org-support-shift-select 'always))
 
13285
         (org-on-heading-p))
 
13286
    (org-call-with-arg 'org-todo 'right))
 
13287
   ((or (and org-support-shift-select
 
13288
             (not (eq org-support-shift-select 'always))
 
13289
             (org-at-item-bullet-p))
 
13290
        (and (not org-support-shift-select) (org-at-item-p)))
 
13291
    (org-call-with-arg 'org-cycle-list-bullet nil))
 
13292
   ((and (not (eq org-support-shift-select 'always))
 
13293
         (org-at-property-p))
 
13294
    (call-interactively 'org-property-next-allowed-value))
12394
13295
   ((org-clocktable-try-shift 'right arg))
12395
 
   (t (org-shiftcursor-error))))
 
13296
   (org-support-shift-select 
 
13297
    (org-call-for-shift-select 'forward-char))
 
13298
   (t (org-shiftselect-error))))
12396
13299
 
12397
13300
(defun org-shiftleft (&optional arg)
12398
 
  "Previous TODO keyword or timestamp one day earlier, depending on context."
 
13301
  "Cycle the thing at point or in the current line, depending on context.
 
13302
Depending on context, this does one of the following:
 
13303
 
 
13304
- switch a timestamp at point one day into the past
 
13305
- on a headline, switch to the previous TODO keyword.
 
13306
- on an item, switch entire list to the previous bullet type
 
13307
- on a property line, switch to the previous allowed value
 
13308
- on a clocktable definition line, move time block into the past"
12399
13309
  (interactive "P")
12400
13310
  (cond
 
13311
   ((and org-support-shift-select (org-region-active-p))
 
13312
    (org-call-for-shift-select 'backward-char))
12401
13313
   ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
12402
 
   ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
12403
 
   ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
12404
 
   ((org-at-property-p)
 
13314
   ((and (not (eq org-support-shift-select 'always))
 
13315
         (org-on-heading-p))
 
13316
    (org-call-with-arg 'org-todo 'left))
 
13317
   ((or (and org-support-shift-select
 
13318
             (not (eq org-support-shift-select 'always))
 
13319
             (org-at-item-bullet-p))
 
13320
        (and (not org-support-shift-select) (org-at-item-p)))
 
13321
    (org-call-with-arg 'org-cycle-list-bullet 'previous))
 
13322
   ((and (not (eq org-support-shift-select 'always))
 
13323
         (org-at-property-p))
12405
13324
    (call-interactively 'org-property-previous-allowed-value))
12406
13325
   ((org-clocktable-try-shift 'left arg))
12407
 
   (t (org-shiftcursor-error))))
 
13326
   (org-support-shift-select 
 
13327
    (org-call-for-shift-select 'backward-char))
 
13328
   (t (org-shiftselect-error))))
12408
13329
 
12409
13330
(defun org-shiftcontrolright ()
12410
13331
  "Switch to next TODO set."
12411
13332
  (interactive)
12412
13333
  (cond
12413
 
   ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
12414
 
   (t (org-shiftcursor-error))))
 
13334
   ((and org-support-shift-select (org-region-active-p))
 
13335
    (org-call-for-shift-select 'forward-word))
 
13336
   ((and (not (eq org-support-shift-select 'always))
 
13337
         (org-on-heading-p))
 
13338
    (org-call-with-arg 'org-todo 'nextset))
 
13339
   (org-support-shift-select
 
13340
    (org-call-for-shift-select 'forward-word))
 
13341
   (t (org-shiftselect-error))))
12415
13342
 
12416
13343
(defun org-shiftcontrolleft ()
12417
13344
  "Switch to previous TODO set."
12418
13345
  (interactive)
12419
13346
  (cond
12420
 
   ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
12421
 
   (t (org-shiftcursor-error))))
 
13347
   ((and org-support-shift-select (org-region-active-p))
 
13348
    (org-call-for-shift-select 'backward-word))
 
13349
   ((and (not (eq org-support-shift-select 'always))
 
13350
         (org-on-heading-p))
 
13351
    (org-call-with-arg 'org-todo 'previousset))
 
13352
   (org-support-shift-select
 
13353
    (org-call-for-shift-select 'backward-word))
 
13354
   (t (org-shiftselect-error))))
12422
13355
 
12423
13356
(defun org-ctrl-c-ret ()
12424
13357
  "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
12489
13422
- If the cursor is on a #+TBLFM line, re-apply the formulas to
12490
13423
  the entire table.
12491
13424
 
 
13425
- If the cursor is at a footnote reference or definition, jump to
 
13426
  the corresponding definition or references, respectively.
 
13427
 
12492
13428
- If the cursor is a the beginning of a dynamic block, update it.
12493
13429
 
12494
13430
- If the cursor is inside a table created by the table.el package,
12495
13431
  activate that table.
12496
13432
 
12497
 
- If the current buffer is a remember buffer, close note and file it.
12498
 
  with a prefix argument, file it without further interaction to the default
12499
 
  location.
 
13433
- If the current buffer is a remember buffer, close note and file
 
13434
  it.  A prefix argument of 1 files to the default location
 
13435
  without further interaction.  A prefix argument of 2 files to
 
13436
  the currently clocking task.
12500
13437
 
12501
13438
- If the cursor is on a <<<target>>>, update radio targets and corresponding
12502
13439
  links in this buffer.
12511
13448
     ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
12512
13449
          org-occur-highlights
12513
13450
          org-latex-fragment-image-overlays)
12514
 
      (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
 
13451
      (and (boundp 'org-clock-overlays) (org-clock-remove-overlays))
12515
13452
      (org-remove-occur-highlights)
12516
13453
      (org-remove-latex-fragment-image-overlays)
12517
13454
      (message "Temporary highlights/overlays removed from current buffer"))
12533
13470
          (call-interactively 'org-table-recalculate)
12534
13471
        (org-table-maybe-recalculate-line))
12535
13472
      (call-interactively 'org-table-align))
 
13473
     ((or (org-footnote-at-reference-p)
 
13474
          (org-footnote-at-definition-p))
 
13475
      (call-interactively 'org-footnote-action))
12536
13476
     ((org-at-item-checkbox-p)
12537
13477
      (call-interactively 'org-toggle-checkbox))
12538
13478
     ((org-at-item-p)
12599
13539
 
12600
13540
(defun org-ctrl-c-star ()
12601
13541
  "Compute table, or change heading status of lines.
12602
 
Calls `org-table-recalculate' or `org-toggle-region-headings',
12603
 
depending on context.  This will also turn a plain list item or a normal
12604
 
line into a subheading."
 
13542
Calls `org-table-recalculate' or `org-toggle-heading',
 
13543
depending on context."
12605
13544
  (interactive)
12606
13545
  (cond
12607
13546
   ((org-at-table-p)
12608
13547
    (call-interactively 'org-table-recalculate))
12609
 
   ((org-region-active-p)
 
13548
   (t
12610
13549
    ;; Convert all lines in region to list items
12611
 
    (call-interactively 'org-toggle-region-headings))
12612
 
   ((org-on-heading-p)
12613
 
    (org-toggle-region-headings (point-at-bol)
12614
 
                                (min (1+ (point-at-eol)) (point-max))))
12615
 
   ((org-at-item-p)
12616
 
    ;; Convert to heading
12617
 
    (let ((level (save-match-data
12618
 
                   (save-excursion
12619
 
                     (condition-case nil
12620
 
                         (progn
12621
 
                           (org-back-to-heading t)
12622
 
                           (funcall outline-level))
12623
 
                       (error 0))))))
12624
 
      (replace-match
12625
 
       (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
12626
 
   (t (org-toggle-region-headings (point-at-bol)
12627
 
                                  (min (1+ (point-at-eol)) (point-max))))))
 
13550
    (call-interactively 'org-toggle-heading))))
12628
13551
 
12629
13552
(defun org-ctrl-c-minus ()
12630
13553
  "Insert separator line in table or modify bullet status of line.
12631
13554
Also turns a plain line or a region of lines into list items.
12632
 
Calls `org-table-insert-hline', `org-toggle-region-items', or
 
13555
Calls `org-table-insert-hline', `org-toggle-item', or
12633
13556
`org-cycle-list-bullet', depending on context."
12634
13557
  (interactive)
12635
13558
  (cond
12636
13559
   ((org-at-table-p)
12637
13560
    (call-interactively 'org-table-insert-hline))
12638
 
   ((org-on-heading-p)
12639
 
    ;; Convert to item
12640
 
    (save-excursion
12641
 
      (beginning-of-line 1)
12642
 
      (if (looking-at "\\*+ ")
12643
 
          (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
12644
13561
   ((org-region-active-p)
12645
 
    ;; Convert all lines in region to list items
12646
 
    (call-interactively 'org-toggle-region-items))
 
13562
    (call-interactively 'org-toggle-item))
12647
13563
   ((org-in-item-p)
12648
13564
    (call-interactively 'org-cycle-list-bullet))
12649
 
   (t (org-toggle-region-items (point-at-bol)
12650
 
                               (min (1+ (point-at-eol)) (point-max))))))
12651
 
 
12652
 
(defun org-toggle-region-items (beg end)
12653
 
  "Convert all lines in region to list items.
12654
 
If the first line is already an item, convert all list items in the region
12655
 
to normal lines."
12656
 
  (interactive "r")
12657
 
  (let (l2 l)
 
13565
   (t
 
13566
    (call-interactively 'org-toggle-item))))
 
13567
 
 
13568
(defun org-toggle-item ()
 
13569
  "Convert headings or normal lines to items, items to normal lines.
 
13570
If there is no active region, only the current line is considered.
 
13571
 
 
13572
If the first line in the region is a headline, convert all headlines to items.
 
13573
 
 
13574
If the first line in the region is an item, convert all items to normal lines.
 
13575
 
 
13576
If the first line is normal text, add an item bullet to each line."
 
13577
  (interactive)
 
13578
  (let (l2 l beg end)
 
13579
    (if (org-region-active-p)
 
13580
        (setq beg (region-beginning) end (region-end))
 
13581
      (setq beg (point-at-bol)
 
13582
            end (min (1+ (point-at-eol)) (point-max))))
12658
13583
    (save-excursion
12659
13584
      (goto-char end)
12660
13585
      (setq l2 (org-current-line))
12669
13594
              (delete-region (match-beginning 2) (match-end 2))
12670
13595
              (and (looking-at "[ \t]+") (replace-match "")))
12671
13596
            (beginning-of-line 2))
12672
 
        (while (< (setq l (1+ l)) l2)
12673
 
          (unless (org-at-item-p)
12674
 
            (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
12675
 
                (replace-match "\\1- \\2")))
12676
 
          (beginning-of-line 2))))))
12677
 
 
12678
 
(defun org-toggle-region-headings (beg end)
12679
 
  "Convert all lines in region to list items.
12680
 
If the first line is already an item, convert all list items in the region
12681
 
to normal lines."
12682
 
  (interactive "r")
12683
 
  (let (l2 l)
 
13597
        (if (org-on-heading-p)
 
13598
            ;; Headings, convert to items
 
13599
            (while (< (setq l (1+ l)) l2)
 
13600
              (if (looking-at org-outline-regexp)
 
13601
                  (replace-match "- " t t))
 
13602
              (beginning-of-line 2))
 
13603
          ;; normal lines, turn them into items
 
13604
          (while (< (setq l (1+ l)) l2)
 
13605
            (unless (org-at-item-p)
 
13606
              (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
 
13607
                  (replace-match "\\1- \\2")))
 
13608
            (beginning-of-line 2)))))))
 
13609
 
 
13610
(defun org-toggle-heading (&optional nstars)
 
13611
  "Convert headings to normal text, or items or text to headings.
 
13612
If there is no active region, only the current line is considered.
 
13613
 
 
13614
If the first line is a heading, remove the stars from all headlines
 
13615
in the region.
 
13616
 
 
13617
If the first line is a plain list item, turn all plain list items into
 
13618
headings.
 
13619
 
 
13620
If the first line is a normal line, turn each and every line in the region
 
13621
into a heading.
 
13622
 
 
13623
When converting a line into a heading, the number of stars is chosen
 
13624
such that the lines become children of the current entry.  However, when
 
13625
a prefix argument is given, its value determines the number of stars to add."
 
13626
  (interactive "P")
 
13627
  (let (l2 l itemp beg end)
 
13628
    (if (org-region-active-p)
 
13629
        (setq beg (region-beginning) end (region-end))
 
13630
      (setq beg (point-at-bol)
 
13631
            end (min (1+ (point-at-eol)) (point-max))))
12684
13632
    (save-excursion
12685
13633
      (goto-char end)
12686
13634
      (setq l2 (org-current-line))
12693
13641
            (when (org-on-heading-p t)
12694
13642
              (and (looking-at outline-regexp) (replace-match "")))
12695
13643
            (beginning-of-line 2))
12696
 
        (let* ((stars (save-excursion
12697
 
                        (re-search-backward org-complex-heading-regexp nil t)
12698
 
                        (or (match-string 1) "*")))
12699
 
               (add-stars (if org-odd-levels-only "**" "*"))
12700
 
               (rpl (concat stars add-stars " \\2")))
 
13644
        (setq itemp (org-at-item-p))
 
13645
        (let* ((stars
 
13646
                (if nstars
 
13647
                    (make-string (prefix-numeric-value current-prefix-arg)
 
13648
                                 ?*)
 
13649
                  (save-excursion
 
13650
                    (re-search-backward org-complex-heading-regexp nil t)
 
13651
                    (or (match-string 1) "*"))))
 
13652
               (add-stars (if nstars "" (if org-odd-levels-only "**" "*")))
 
13653
               (rpl (concat stars add-stars " ")))
12701
13654
          (while (< (setq l (1+ l)) l2)
12702
 
            (unless (org-on-heading-p)
12703
 
              (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
12704
 
                  (replace-match rpl)))
 
13655
            (if itemp
 
13656
                (and (org-at-item-p) (replace-match rpl t t))
 
13657
              (unless (org-on-heading-p)
 
13658
                (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
 
13659
                    (replace-match (concat rpl (match-string 2))))))
12705
13660
            (beginning-of-line 2)))))))
12706
13661
 
12707
13662
(defun org-meta-return (&optional arg)
12816
13771
     ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
12817
13772
    ("Editing"
12818
13773
     ["Emphasis..." org-emphasize t]
12819
 
     ["Edit Source Example" org-edit-special t])
 
13774
     ["Edit Source Example" org-edit-special t]
 
13775
     "--"
 
13776
     ["Footnote new/jump" org-footnote-action t]
 
13777
     ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
12820
13778
    ("Archive"
12821
13779
     ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
12822
13780
;     ["Check and Tag Children" (org-toggle-archive-tag (4))
12846
13804
     ["Show TODO Tree" org-show-todo-tree t]
12847
13805
     ["Global TODO list" org-todo-list t]
12848
13806
     "--"
 
13807
     ["Enforce dependencies" (customize-variable 'org-enforce-todo-dependencies)
 
13808
      :selected org-enforce-todo-dependencies :style toggle :active t]
 
13809
     "Settings for tree at point"
 
13810
     ["Do Children sequentially" org-toggle-ordered-property :style radio
 
13811
      :selected (ignore-errors (org-entry-get nil "ORDERED"))
 
13812
      :active org-enforce-todo-dependencies :keys "C-c C-x o"]
 
13813
     ["Do Children parallel" org-toggle-ordered-property :style radio
 
13814
      :selected (ignore-errors (not (org-entry-get nil "ORDERED")))
 
13815
      :active org-enforce-todo-dependencies :keys "C-c C-x o"]
 
13816
     "--"
12849
13817
     ["Set Priority" org-priority t]
12850
13818
     ["Priority Up" org-shiftup t]
12851
13819
     ["Priority Down" org-shiftdown t])
12852
13820
    ("TAGS and Properties"
12853
 
     ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
12854
 
     ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
 
13821
     ["Set Tags" org-set-tags-command t]
 
13822
     ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
12855
13823
     "--"
12856
 
     ["Set property" 'org-set-property t]
 
13824
     ["Set property" org-set-property t]
12857
13825
     ["Column view of properties" org-columns t]
12858
13826
     ["Insert Column View DBlock" org-insert-columns-dblock t])
12859
13827
    ("Dates and Scheduling"
12872
13840
      :style radio :selected org-display-custom-times]
12873
13841
     "--"
12874
13842
     ["Goto Calendar" org-goto-calendar t]
12875
 
     ["Date from Calendar" org-date-from-calendar t])
 
13843
     ["Date from Calendar" org-date-from-calendar t]
 
13844
     "--"
 
13845
     ["Start/Restart Timer" org-timer-start t]
 
13846
     ["Pause/Continue Timer" org-timer-pause-or-continue t]
 
13847
     ["Stop Timer" org-timer-pause-or-continue :active t :keys "C-u C-c C-x ,"]
 
13848
     ["Insert Timer String" org-timer t]
 
13849
     ["Insert Timer Item" org-timer-item t])
12876
13850
    ("Logging work"
12877
13851
     ["Clock in" org-clock-in t]
12878
13852
     ["Clock out" org-clock-out t]
12974
13948
(defun org-require-autoloaded-modules ()
12975
13949
  (interactive)
12976
13950
  (mapc 'require
12977
 
        '(org-agenda org-archive org-clock org-colview
 
13951
        '(org-agenda org-archive org-attach org-clock org-colview
12978
13952
                     org-exp org-id org-export-latex org-publish
12979
 
                     org-remember org-table)))
 
13953
                     org-remember org-table org-timer)))
12980
13954
 
12981
13955
;;;###autoload
12982
13956
(defun org-customize ()
13010
13984
 
13011
13985
;;; Generally useful functions
13012
13986
 
 
13987
(defun org-find-text-property-in-string (prop s)
 
13988
  "Return the first non-nil value of property PROP in string S."
 
13989
  (or (get-text-property 0 prop s)
 
13990
      (get-text-property (or (next-single-property-change 0 prop s) 0)
 
13991
                         prop s)))
 
13992
 
13013
13993
(defun org-display-warning (message) ;; Copied from Emacs-Muse
13014
13994
  "Display the given MESSAGE as a warning."
13015
13995
  (if (fboundp 'display-warning)
13034
14014
        (switch-to-buffer (marker-buffer marker))
13035
14015
        (if (or (> marker (point-max)) (< marker (point-min)))
13036
14016
            (widen))
13037
 
        (goto-char marker))
 
14017
        (goto-char marker)
 
14018
        (org-show-context 'org-goto))
13038
14019
    (if bookmark
13039
14020
        (bookmark-jump bookmark)
13040
14021
      (error "Cannot find location"))))
13108
14089
(defun org-fix-indentation (line ind)
13109
14090
  "Fix indentation in LINE.
13110
14091
IND is a cons cell with target and minimum indentation.
13111
 
If the current indenation in LINE is smaller than the minimum,
 
14092
If the current indentation in LINE is smaller than the minimum,
13112
14093
leave it alone.  If it is larger than ind, set it to the target."
13113
14094
  (let* ((l (org-remove-tabs line))
13114
14095
         (i (org-get-indentation l))
13375
14356
  list)
13376
14357
 
13377
14358
(defun org-back-over-empty-lines ()
13378
 
  "Move backwards over witespace, to the beginning of the first empty line.
 
14359
  "Move backwards over whitespace, to the beginning of the first empty line.
13379
14360
Returns the number of empty lines passed."
13380
14361
  (let ((pos (point)))
13381
14362
    (skip-chars-backward " \t\n\r")
13480
14461
        (or (buffer-base-buffer buf) buf)
13481
14462
      nil)))
13482
14463
 
13483
 
(defun org-image-file-name-regexp ()
13484
 
  "Return regexp matching the file names of images."
13485
 
  (if (fboundp 'image-file-name-regexp)
 
14464
(defun org-image-file-name-regexp (&optional extensions)
 
14465
  "Return regexp matching the file names of images.
 
14466
If EXTENSIONS is given, only match these."
 
14467
  (if (and (not extensions) (fboundp 'image-file-name-regexp))
13486
14468
      (image-file-name-regexp)
13487
14469
    (let ((image-file-name-extensions
13488
 
           '("png" "jpeg" "jpg" "gif" "tiff" "tif"
13489
 
             "xbm" "xpm" "pbm" "pgm" "ppm")))
 
14470
           (or extensions
 
14471
               '("png" "jpeg" "jpg" "gif" "tiff" "tif"
 
14472
                 "xbm" "xpm" "pbm" "pgm" "ppm"))))
13490
14473
      (concat "\\."
13491
14474
              (regexp-opt (nconc (mapcar 'upcase
13492
14475
                                         image-file-name-extensions)
13494
14477
                          t)
13495
14478
              "\\'"))))
13496
14479
 
13497
 
(defun org-file-image-p (file)
 
14480
(defun org-file-image-p (file &optional extensions)
13498
14481
  "Return non-nil if FILE is an image."
13499
14482
  (save-match-data
13500
 
    (string-match (org-image-file-name-regexp) file)))
 
14483
    (string-match (org-image-file-name-regexp extensions) file)))
13501
14484
 
13502
14485
(defun org-get-cursor-date ()
13503
14486
  "Return the date at cursor in as a time.
13654
14637
               (goto-char (+ (match-beginning 1) 5))
13655
14638
             (goto-char (match-end 0)))
13656
14639
           (make-string (current-column) ?\ )))
13657
 
        ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
 
14640
        ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)]  ?\\)?")
13658
14641
         (save-excursion
13659
14642
           (goto-char (match-end 0))
13660
14643
           (make-string (current-column) ?\ )))
13718
14701
  (interactive "P")
13719
14702
  (let ((pos (point)) refpos)
13720
14703
    (beginning-of-line 1)
13721
 
    (if (bobp)
13722
 
        nil
13723
 
      (backward-char 1)
13724
 
      (if (org-invisible-p)
13725
 
          (while (and (not (bobp)) (org-invisible-p))
13726
 
            (backward-char 1)
13727
 
            (beginning-of-line 1))
13728
 
        (forward-char 1)))
 
14704
    (if (and arg (fboundp 'move-beginning-of-line))
 
14705
        (call-interactively 'move-beginning-of-line)
 
14706
      (if (bobp)
 
14707
          nil
 
14708
        (backward-char 1)
 
14709
        (if (org-invisible-p)
 
14710
            (while (and (not (bobp)) (org-invisible-p))
 
14711
              (backward-char 1)
 
14712
              (beginning-of-line 1))
 
14713
          (forward-char 1))))
13729
14714
    (when org-special-ctrl-a/e
13730
14715
      (cond
13731
14716
       ((and (looking-at org-complex-heading-regexp)
13759
14744
beyond the end of the headline."
13760
14745
  (interactive "P")
13761
14746
  (if (or (not org-special-ctrl-a/e)
13762
 
          (not (org-on-heading-p)))
13763
 
      (end-of-line arg)
 
14747
          (not (org-on-heading-p))
 
14748
          arg)
 
14749
      (call-interactively (if (fboundp 'move-end-of-line)
 
14750
                              'move-end-of-line
 
14751
                            'end-of-line))
13764
14752
    (let ((pos (point)))
13765
14753
      (beginning-of-line 1)
13766
14754
      (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
13772
14760
            (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
13773
14761
                (goto-char (match-end 0))
13774
14762
              (goto-char (match-beginning 1))))
13775
 
        (end-of-line arg))))
 
14763
        (call-interactively (if (fboundp 'move-end-of-line)
 
14764
                                'move-end-of-line
 
14765
                              'end-of-line)))))
13776
14766
  (org-no-warnings
13777
14767
   (and (featurep 'xemacs) (setq zmacs-region-stays t))))
13778
14768
 
13779
 
 
13780
14769
(define-key org-mode-map "\C-a" 'org-beginning-of-line)
13781
14770
(define-key org-mode-map "\C-e" 'org-end-of-line)
13782
14771
 
13793
14782
    (org-set-tags nil t))
13794
14783
   (t (kill-region (point) (point-at-eol)))))
13795
14784
 
13796
 
 
13797
14785
(define-key org-mode-map "\C-k" 'org-kill-line)
13798
14786
 
13799
 
(defun org-yank ()
13800
 
  "Yank, and if the yanked text is a single subtree, fold it.
13801
 
In fact, if the yanked text is a sequence of subtrees, fold all of them."
13802
 
  (interactive)
13803
 
  (if org-yank-folded-subtrees
13804
 
      (let ((beg (point)) end)
13805
 
        (call-interactively 'yank)
13806
 
        (setq end (point))
13807
 
        (goto-char beg)
13808
 
        (when (and (bolp)
13809
 
                   (org-kill-is-subtree-p))
13810
 
          (or (looking-at outline-regexp)
13811
 
              (re-search-forward (concat "^" outline-regexp) end t))
13812
 
          (while (and (< (point) end) (looking-at outline-regexp))
13813
 
            (hide-subtree)
13814
 
            (org-cycle-show-empty-lines 'folded)
13815
 
            (condition-case nil
13816
 
                (outline-forward-same-level 1)
13817
 
              (error (goto-char end)))))
13818
 
        (goto-char end)
13819
 
        (skip-chars-forward " \t\n\r"))
13820
 
    (call-interactively 'yank)))
 
14787
(defun org-yank (&optional arg)
 
14788
  "Yank.  If the kill is a subtree, treat it specially.
 
14789
This command will look at the current kill and check if is a single
 
14790
subtree, or a series of subtrees[1].  If it passes the test, and if the
 
14791
cursor is at the beginning of a line or after the stars of a currently
 
14792
empty headline, then the yank is handled specially.  How exactly depends
 
14793
on the value of the following variables, both set by default.
 
14794
 
 
14795
org-yank-folded-subtrees
 
14796
    When set, the subtree(s) will be folded after insertion, but only
 
14797
    if doing so would now swallow text after the yanked text.
 
14798
 
 
14799
org-yank-adjusted-subtrees
 
14800
    When set, the subtree will be promoted or demoted in order to
 
14801
    fit into the local outline tree structure, which means that the level
 
14802
    will be adjusted so that it becomes the smaller one of the two
 
14803
    *visible* surrounding headings.
 
14804
 
 
14805
Any prefix to this command will cause `yank' to be called directly with
 
14806
no special treatment.  In particular, a simple `C-u' prefix will just
 
14807
plainly yank the text as it is.
 
14808
 
 
14809
\[1] Basically, the test checks if the first non-white line is a heading
 
14810
    and if there are no other headings with fewer stars."
 
14811
  (interactive "P")
 
14812
  (setq this-command 'yank)
 
14813
  (if arg
 
14814
      (call-interactively 'yank)
 
14815
    (let ((subtreep ; is kill a subtree, and the yank position appropriate?
 
14816
           (and (org-kill-is-subtree-p)
 
14817
                (or (bolp)
 
14818
                    (and (looking-at "[ \t]*$")
 
14819
                         (string-match
 
14820
                          "\\`\\*+\\'"
 
14821
                          (buffer-substring (point-at-bol) (point)))))))
 
14822
          swallowp)
 
14823
      (cond
 
14824
       ((and subtreep org-yank-folded-subtrees)
 
14825
        (let ((beg (point))
 
14826
              end)
 
14827
          (if (and subtreep org-yank-adjusted-subtrees)
 
14828
              (org-paste-subtree nil nil 'for-yank)
 
14829
            (call-interactively 'yank))
 
14830
          (setq end (point))
 
14831
          (goto-char beg)
 
14832
          (when (and (bolp) subtreep
 
14833
                     (not (setq swallowp
 
14834
                                (org-yank-folding-would-swallow-text beg end))))
 
14835
            (or (looking-at outline-regexp)
 
14836
                (re-search-forward (concat "^" outline-regexp) end t))
 
14837
            (while (and (< (point) end) (looking-at outline-regexp))
 
14838
              (hide-subtree)
 
14839
              (org-cycle-show-empty-lines 'folded)
 
14840
              (condition-case nil
 
14841
                  (outline-forward-same-level 1)
 
14842
                (error (goto-char end)))))
 
14843
          (when swallowp
 
14844
            (message
 
14845
             "Yanked text not folded because that would swallow text"))
 
14846
          (goto-char end)
 
14847
          (skip-chars-forward " \t\n\r")
 
14848
          (beginning-of-line 1)
 
14849
          (push-mark beg 'nomsg)))
 
14850
       ((and subtreep org-yank-adjusted-subtrees)
 
14851
        (let ((beg (point-at-bol)))
 
14852
          (org-paste-subtree nil nil 'for-yank)
 
14853
          (push-mark beg 'nomsg)))
 
14854
       (t
 
14855
        (call-interactively 'yank))))))
 
14856
 
 
14857
(defun org-yank-folding-would-swallow-text (beg end)
 
14858
  "Would hide-subtree at BEG swallow any text after END?"
 
14859
  (let (level)
 
14860
    (save-excursion
 
14861
      (goto-char beg)
 
14862
      (when (or (looking-at outline-regexp)
 
14863
                (re-search-forward (concat "^" outline-regexp) end t))
 
14864
        (setq level (org-outline-level)))
 
14865
      (goto-char end)
 
14866
      (skip-chars-forward " \t\r\n\v\f")
 
14867
      (if (or (eobp)
 
14868
              (and (bolp) (looking-at org-outline-regexp)
 
14869
                   (<= (org-outline-level) level)))
 
14870
          nil ; Nothing would be swallowed
 
14871
        t)))) ; something would swallow
13821
14872
 
13822
14873
(define-key org-mode-map "\C-y" 'org-yank)
13823
14874
 
13837
14888
        (outline-invisible-p)
13838
14889
      (get-char-property (point) 'invisible))))
13839
14890
 
13840
 
(defalias 'org-back-to-heading 'outline-back-to-heading)
 
14891
(defun org-back-to-heading (&optional invisible-ok)
 
14892
  "Call `outline-back-to-heading', but provide a better error message."
 
14893
  (condition-case nil
 
14894
      (outline-back-to-heading invisible-ok)
 
14895
    (error (error "Before first headline at position %d in buffer %s"
 
14896
                  (point) (current-buffer)))))
 
14897
 
 
14898
(defun org-before-first-heading-p ()
 
14899
  "Before first heading?"
 
14900
  (save-excursion
 
14901
    (null (re-search-backward "^\\*+ " nil t))))
 
14902
 
13841
14903
(defalias 'org-on-heading-p 'outline-on-heading-p)
13842
14904
(defalias 'org-at-heading-p 'outline-on-heading-p)
13843
14905
(defun org-at-heading-or-item-p ()
13859
14921
  "Move to the heading line of which the present line is a subheading.
13860
14922
This version will not throw an error.  It will return the level of the
13861
14923
headline found, or nil if no higher level is found."
13862
 
  (let ((pos (point)) start-level level
13863
 
        (re (concat "^" outline-regexp)))
13864
 
    (catch 'exit
13865
 
      (outline-back-to-heading t)
13866
 
      (setq start-level (funcall outline-level))
13867
 
      (if (equal start-level 1) (throw 'exit nil))
13868
 
      (while (re-search-backward re nil t)
13869
 
        (setq level (funcall outline-level))
13870
 
        (if (< level start-level) (throw 'exit level)))
13871
 
      nil)))
 
14924
  (let (start-level re)
 
14925
    (org-back-to-heading t)
 
14926
    (setq start-level (funcall outline-level))
 
14927
    (if (equal start-level 1)
 
14928
        nil
 
14929
      (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
 
14930
      (if (re-search-backward re nil t)
 
14931
          (funcall outline-level)))))
13872
14932
 
13873
14933
(defun org-first-sibling-p ()
13874
14934
  "Is this heading the first child of its parents?"
13938
14998
Stop at the first and last subheadings of a superior heading.
13939
14999
This is like outline-forward-same-level, but invisible headings are ok."
13940
15000
  (interactive "p")
13941
 
  (outline-back-to-heading t)
 
15001
  (org-back-to-heading t)
13942
15002
  (while (> arg 0)
13943
15003
    (let ((point-to-move-to (save-excursion
13944
15004
                              (org-get-next-sibling))))
14064
15124
         (re (concat "^" outline-regexp))
14065
15125
         (subs (make-vector (1+ n) nil))
14066
15126
         (last-level 0)
14067
 
         m tree level head)
 
15127
         m level head)
14068
15128
    (save-excursion
14069
15129
      (save-restriction
14070
15130
        (widen)
14117
15177
To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
14118
15178
  (interactive)
14119
15179
  (require 'org-agenda)
14120
 
  (let (p m tp np dir txt w)
 
15180
  (let (p m tp np dir txt)
14121
15181
    (cond
14122
15182
     ((setq p (text-property-any (point-at-bol) (point-at-eol)
14123
15183
                                 'org-imenu t))
14158
15218
     (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
14159
15219
     (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
14160
15220
     (add-hook 'speedbar-visiting-tag-hook
14161
 
               (lambda () (org-show-context 'org-goto)))))
 
15221
               (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
14162
15222
 
14163
15223
 
14164
15224
;;; Fixes and Hacks for problems with other packages
14178
15238
       "Make the position visible."
14179
15239
       (org-bookmark-jump-unhide))))
14180
15240
 
 
15241
;; Make sure saveplace show the location if it was hidden
 
15242
(eval-after-load "saveplace"
 
15243
  '(defadvice save-place-find-file-hook (after org-make-visible activate)
 
15244
     "Make the position visible."
 
15245
     (org-bookmark-jump-unhide)))
 
15246
 
14181
15247
(defun org-bookmark-jump-unhide ()
14182
15248
  "Unhide the current position, to show the bookmark location."
14183
15249
  (and (org-mode-p)