Fix white-space errors
[org-mode/org-tableheadings.git] / lisp / org-remember.el
blob2f6f7d9190deb7b9dabdc3954d31e506c6db7d52
1 ;;; org-remember.el --- Fast note taking in Org-mode
3 ;; Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
6 ;; Author: Carsten Dominik <carsten at orgmode dot org>
7 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; Homepage: http://orgmode.org
9 ;; Version: 6.28trans
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
27 ;;; Commentary:
29 ;; This file contains the system to take fast notes with Org-mode.
30 ;; This system is used together with John Wiegley's `remember.el'.
32 ;;; Code:
34 (eval-when-compile
35 (require 'cl))
36 (require 'org)
38 (declare-function remember-mode "remember" ())
39 (declare-function remember "remember" (&optional initial))
40 (declare-function remember-buffer-desc "remember" ())
41 (declare-function remember-finalize "remember" ())
42 (defvar remember-save-after-remembering)
43 (defvar remember-register)
44 (defvar remember-buffer)
45 (defvar remember-handler-functions)
46 (defvar remember-annotation-functions)
47 (defvar org-clock-heading)
48 (defvar org-clock-heading-for-remember)
50 (defgroup org-remember nil
51 "Options concerning interaction with remember.el."
52 :tag "Org Remember"
53 :group 'org)
55 (defcustom org-remember-store-without-prompt t
56 "Non-nil means, `C-c C-c' stores remember note without further prompts.
57 It then uses the file and headline specified by the template or (if the
58 template does not specify them) by the variables `org-default-notes-file'
59 and `org-remember-default-headline'. To force prompting anyway, use
60 `C-u C-c C-c' to file the note.
62 When this variable is nil, `C-c C-c' gives you the prompts, and
63 `C-u C-c C-c' triggers the fasttrack."
64 :group 'org-remember
65 :type 'boolean)
67 (defcustom org-remember-interactive-interface 'refile
68 "The interface to be used for interactive filing of remember notes.
69 This is only used when the interactive mode for selecting a filing
70 location is used (see the variable `org-remember-store-without-prompt').
71 Allowed values are:
72 outline The interface shows an outline of the relevant file
73 and the correct heading is found by moving through
74 the outline or by searching with incremental search.
75 outline-path-completion Headlines in the current buffer are offered via
76 completion.
77 refile Use the refile interface, and offer headlines,
78 possibly from different buffers."
79 :group 'org-remember
80 :type '(choice
81 (const :tag "Refile" refile)
82 (const :tag "Outline" outline)
83 (const :tag "Outline-path-completion" outline-path-completion)))
85 (defcustom org-remember-default-headline ""
86 "The headline that should be the default location in the notes file.
87 When filing remember notes, the cursor will start at that position.
88 You can set this on a per-template basis with the variable
89 `org-remember-templates'."
90 :group 'org-remember
91 :type 'string)
93 (defcustom org-remember-templates nil
94 "Templates for the creation of remember buffers.
95 When nil, just let remember make the buffer.
96 When non-nil, this is a list of 5-element lists. In each entry, the first
97 element is the name of the template, which should be a single short word.
98 The second element is a character, a unique key to select this template.
99 The third element is the template.
101 The fourth element is optional and can specify a destination file for
102 remember items created with this template. The default file is given
103 by `org-default-notes-file'. If the file name is not an absolute path,
104 it will be interpreted relative to `org-directory'.
106 An optional fifth element can specify the headline in that file that should
107 be offered first when the user is asked to file the entry. The default
108 headline is given in the variable `org-remember-default-headline'. When
109 this element is `top' or `bottom', the note will be placed as a level-1
110 entry at the beginning or end of the file, respectively.
112 An optional sixth element specifies the contexts in which the template
113 will be offered to the user. This element can be a list of major modes
114 or a function, and the template will only be offered if `org-remember'
115 is called from a mode in the list, or if the function returns t.
116 Templates that specify t or nil for the context will be always be added
117 to the list of selectable templates.
119 The template specifies the structure of the remember buffer. It should have
120 a first line starting with a star, to act as the org-mode headline.
121 Furthermore, the following %-escapes will be replaced with content:
123 %^{prompt} Prompt the user for a string and replace this sequence with it.
124 A default value and a completion table ca be specified like this:
125 %^{prompt|default|completion2|completion3|...}
126 %t time stamp, date only
127 %T time stamp with date and time
128 %u, %U like the above, but inactive time stamps
129 %^t like %t, but prompt for date. Similarly %^T, %^u, %^U.
130 You may define a prompt like %^{Please specify birthday
131 %n user name (taken from `user-full-name')
132 %a annotation, normally the link created with org-store-link
133 %i initial content, the region active. If %i is indented,
134 the entire inserted text will be indented as well.
135 %c current kill ring head
136 %x content of the X clipboard
137 %^C Interactive selection of which kill or clip to use
138 %^L Like %^C, but insert as link
139 %k title of currently clocked task
140 %K link to currently clocked task
141 %^g prompt for tags, with completion on tags in target file
142 %^G prompt for tags, with completion all tags in all agenda files
143 %^{prop}p Prompt the user for a value for property `prop'
144 %:keyword specific information for certain link types, see below
145 %[pathname] insert the contents of the file given by `pathname'
146 %(sexp) evaluate elisp `(sexp)' and replace with the result
147 %! Store this note immediately after filling the template
148 %& Visit note immediately after storing it
150 %? After completing the template, position cursor here.
152 Apart from these general escapes, you can access information specific to the
153 link type that is created. For example, calling `remember' in emails or gnus
154 will record the author and the subject of the message, which you can access
155 with %:author and %:subject, respectively. Here is a complete list of what
156 is recorded for each link type.
158 Link type | Available information
159 -------------------+------------------------------------------------------
160 bbdb | %:type %:name %:company
161 vm, wl, mh, rmail | %:type %:subject %:message-id
162 | %:from %:fromname %:fromaddress
163 | %:to %:toname %:toaddress
164 | %:fromto (either \"to NAME\" or \"from NAME\")
165 gnus | %:group, for messages also all email fields
166 w3, w3m | %:type %:url
167 info | %:type %:file %:node
168 calendar | %:type %:date"
169 :group 'org-remember
170 :get (lambda (var) ; Make sure all entries have at least 5 elements
171 (mapcar (lambda (x)
172 (if (not (stringp (car x))) (setq x (cons "" x)))
173 (cond ((= (length x) 4) (append x '(nil)))
174 ((= (length x) 3) (append x '(nil nil)))
175 (t x)))
176 (default-value var)))
177 :type '(repeat
178 :tag "enabled"
179 (list :value ("" ?a "\n" nil nil nil)
180 (string :tag "Name")
181 (character :tag "Selection Key")
182 (string :tag "Template")
183 (choice :tag "Destination file"
184 (file :tag "Specify")
185 (function :tag "Function")
186 (const :tag "Use `org-default-notes-file'" nil))
187 (choice :tag "Destin. headline"
188 (string :tag "Specify")
189 (function :tag "Function")
190 (const :tag "Use `org-remember-default-headline'" nil)
191 (const :tag "At beginning of file" top)
192 (const :tag "At end of file" bottom))
193 (choice :tag "Context"
194 (const :tag "Use in all contexts" nil)
195 (const :tag "Use in all contexts" t)
196 (repeat :tag "Use only if in major mode"
197 (symbol :tag "Major mode"))
198 (function :tag "Perform a check against function")))))
200 (defcustom org-remember-delete-empty-lines-at-end t
201 "Non-nil means clean up final empty lines in remember buffer."
202 :group 'org-remember
203 :type 'boolean)
205 (defcustom org-remember-before-finalize-hook nil
206 "Hook that is run right before a remember process is finalized.
207 The remember buffer is still current when this hook runs."
208 :group 'org-remember
209 :type 'hook)
211 (defvar org-remember-mode-map (make-sparse-keymap)
212 "Keymap for org-remember-mode, a minor mode.
213 Use this map to set additional keybindings for when Org-mode is used
214 for a Remember buffer.")
215 (defvar org-remember-mode-hook nil
216 "Hook for the minor `org-remember-mode'.")
218 (define-minor-mode org-remember-mode
219 "Minor mode for special key bindings in a remember buffer."
220 nil " Rem" org-remember-mode-map
221 (run-hooks 'org-remember-mode-hook))
222 (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
223 (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
225 (defcustom org-remember-clock-out-on-exit 'query
226 "Non-nil means, stop the clock when exiting a clocking remember buffer.
227 This only applies if the clock is running in the remember buffer. If the
228 clock is not stopped, it continues to run in the storage location.
229 Instead of nil or t, this may also be the symbol `query' to prompt the
230 user each time a remember buffer with a running clock is filed away. "
231 :group 'org-remember
232 :type '(choice
233 (const :tag "Never" nil)
234 (const :tag "Always" t)
235 (const :tag "Query user" query)))
237 (defcustom org-remember-backup-directory nil
238 "Directory where to store all remember buffers, for backup purposes.
239 After a remember buffer has been stored successfully, the backup file
240 will be removed. However, if you forget to finish the remember process,
241 the file will remain there.
242 See also `org-remember-auto-remove-backup-files'."
243 :group 'org-remember
244 :type '(choice
245 (const :tag "No backups" nil)
246 (directory :tag "Directory")))
248 (defcustom org-remember-auto-remove-backup-files t
249 "Non-nil means, remove remember backup files after successfully storage.
250 When remember is finished successfully, with storing the note at the
251 desired target, remove the backup files related to this remember process
252 and show a message about remaining backup files, from previous, unfinished
253 remember sessions.
254 Backup files will only be made at all, when `org-remember-backup-directory'
255 is set."
256 :group 'org-remember
257 :type 'boolean)
259 (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
260 (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
262 ;;;###autoload
263 (defun org-remember-insinuate ()
264 "Setup remember.el for use with Org-mode."
265 (org-require-remember)
266 (setq remember-annotation-functions '(org-remember-annotation))
267 (setq remember-handler-functions '(org-remember-handler))
268 (add-hook 'remember-mode-hook 'org-remember-apply-template))
270 ;;;###autoload
271 (defun org-remember-annotation ()
272 "Return a link to the current location as an annotation for remember.el.
273 If you are using Org-mode files as target for data storage with
274 remember.el, then the annotations should include a link compatible with the
275 conventions in Org-mode. This function returns such a link."
276 (org-store-link nil))
278 (defconst org-remember-help
279 "Select a destination location for the note.
280 UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
281 RET on headline -> Store as sublevel entry to current headline
282 RET at beg-of-buf -> Append to file as level 2 headline
283 <left>/<right> -> before/after current headline, same headings level")
285 (defvar org-jump-to-target-location nil)
286 (defvar org-remember-previous-location nil)
287 (defvar org-force-remember-template-char) ;; dynamically scoped
289 ;; Save the major mode of the buffer we called remember from
290 (defvar org-select-template-temp-major-mode nil)
292 ;; Temporary store the buffer where remember was called from
293 (defvar org-select-template-original-buffer nil)
295 (defun org-select-remember-template (&optional use-char)
296 (when org-remember-templates
297 (let* ((pre-selected-templates
298 (mapcar
299 (lambda (tpl)
300 (let ((ctxt (nth 5 tpl))
301 (mode org-select-template-temp-major-mode)
302 (buf org-select-template-original-buffer))
303 (and (or (not ctxt) (eq ctxt t)
304 (and (listp ctxt) (memq mode ctxt))
305 (and (functionp ctxt)
306 (with-current-buffer buf
307 ;; Protect the user-defined function from error
308 (condition-case nil (funcall ctxt) (error nil)))))
309 tpl)))
310 org-remember-templates))
311 ;; If no template at this point, add the default templates:
312 (pre-selected-templates1
313 (if (not (delq nil pre-selected-templates))
314 (mapcar (lambda(x) (if (not (nth 5 x)) x))
315 org-remember-templates)
316 pre-selected-templates))
317 ;; Then unconditionally add template for any contexts
318 (pre-selected-templates2
319 (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
320 org-remember-templates)
321 (delq nil pre-selected-templates1)))
322 (templates (mapcar (lambda (x)
323 (if (stringp (car x))
324 (append (list (nth 1 x) (car x)) (cddr x))
325 (append (list (car x) "") (cdr x))))
326 (delq nil pre-selected-templates2)))
328 (char (or use-char
329 (cond
330 ((= (length templates) 1)
331 (caar templates))
332 ((and (boundp 'org-force-remember-template-char)
333 org-force-remember-template-char)
334 (if (stringp org-force-remember-template-char)
335 (string-to-char org-force-remember-template-char)
336 org-force-remember-template-char))
338 (setq msg (format
339 "Select template: %s"
340 (mapconcat
341 (lambda (x)
342 (cond
343 ((not (string-match "\\S-" (nth 1 x)))
344 (format "[%c]" (car x)))
345 ((equal (downcase (car x))
346 (downcase (aref (nth 1 x) 0)))
347 (format "[%c]%s" (car x)
348 (substring (nth 1 x) 1)))
349 (t (format "[%c]%s" (car x) (nth 1 x)))))
350 templates " ")))
351 (let ((inhibit-quit t) char0)
352 (while (not char0)
353 (message msg)
354 (setq char0 (read-char-exclusive))
355 (when (and (not (assoc char0 templates))
356 (not (equal char0 ?\C-g)))
357 (message "No such template \"%c\"" char0)
358 (ding) (sit-for 1)
359 (setq char0 nil)))
360 (when (equal char0 ?\C-g)
361 (jump-to-register remember-register)
362 (kill-buffer remember-buffer)
363 (error "Abort"))
364 char0))))))
365 (cddr (assoc char templates)))))
367 (defun org-get-x-clipboard (value)
368 "Get the value of the x clipboard, compatible with XEmacs, and GNU Emacs 21."
369 (if (eq window-system 'x)
370 (let ((x (org-get-x-clipboard-compat value)))
371 (if x (org-no-properties x)))))
373 ;;;###autoload
374 (defun org-remember-apply-template (&optional use-char skip-interactive)
375 "Initialize *remember* buffer with template, invoke `org-mode'.
376 This function should be placed into `remember-mode-hook' and in fact requires
377 to be run from that hook to function properly."
378 (when (and (boundp 'initial) (stringp initial))
379 (setq initial (org-no-properties initial))
380 (remove-text-properties 0 (length initial) '(read-only t) initial))
381 (if org-remember-templates
382 (let* ((entry (org-select-remember-template use-char))
383 (ct (or org-overriding-default-time (org-current-time)))
384 (dct (decode-time ct))
385 (ct1
386 (if (< (nth 2 dct) org-extend-today-until)
387 (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
388 ct))
389 (tpl (car entry))
390 (plist-p (if org-store-link-plist t nil))
391 (file (if (and (nth 1 entry)
392 (or (and (stringp (nth 1 entry))
393 (string-match "\\S-" (nth 1 entry)))
394 (functionp (nth 1 entry))))
395 (nth 1 entry)
396 org-default-notes-file))
397 (headline (nth 2 entry))
398 (v-c (and (> (length kill-ring) 0) (current-kill 0)))
399 (v-x (or (org-get-x-clipboard 'PRIMARY)
400 (org-get-x-clipboard 'CLIPBOARD)
401 (org-get-x-clipboard 'SECONDARY)))
402 (v-t (format-time-string (car org-time-stamp-formats) ct))
403 (v-T (format-time-string (cdr org-time-stamp-formats) ct))
404 (v-u (concat "[" (substring v-t 1 -1) "]"))
405 (v-U (concat "[" (substring v-T 1 -1) "]"))
406 ;; `initial' and `annotation' are bound in `remember'.
407 ;; But if the property list has them, we prefer those values
408 (v-i (or (plist-get org-store-link-plist :initial)
409 (and (boundp 'initial) initial)
410 ""))
411 (v-a (or (plist-get org-store-link-plist :annotation)
412 (and (boundp 'annotation) annotation)
413 ""))
414 ;; Is the link empty? Then we do not want it...
415 (v-a (if (equal v-a "[[]]") "" v-a))
416 (clipboards (remove nil (list v-i
417 (org-get-x-clipboard 'PRIMARY)
418 (org-get-x-clipboard 'CLIPBOARD)
419 (org-get-x-clipboard 'SECONDARY)
420 v-c)))
421 (v-A (if (and v-a
422 (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
423 (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
424 v-a))
425 (v-n user-full-name)
426 (v-k (if (marker-buffer org-clock-marker)
427 (org-substring-no-properties org-clock-heading)))
428 (v-K (if (marker-buffer org-clock-marker)
429 (org-make-link-string
430 (buffer-file-name (marker-buffer org-clock-marker))
431 org-clock-heading)))
433 (org-startup-folded nil)
434 (org-inhibit-startup t)
435 org-time-was-given org-end-time-was-given x
436 prompt completions char time pos default histvar)
438 (when (functionp file)
439 (setq file (funcall file)))
440 (when (functionp headline)
441 (setq headline (funcall headline)))
442 (when (and file (not (file-name-absolute-p file)))
443 (setq file (expand-file-name file org-directory)))
445 (setq org-store-link-plist
446 (plist-put org-store-link-plist :annotation v-a)
447 org-store-link-plist
448 (plist-put org-store-link-plist :initial v-i))
450 (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
451 (erase-buffer)
452 (insert (substitute-command-keys
453 (format
454 "## %s \"%s\" -> \"* %s\"
455 ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
456 ## C-0 C-c C-c \"%s\" -> \"* %s\"
457 ## %s to select file and header location interactively.
458 ## C-2 C-c C-c as child of the currently clocked item
459 ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
460 (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
461 (abbreviate-file-name (or file org-default-notes-file))
462 (or headline "")
463 (or (car org-remember-previous-location) "???")
464 (or (cdr org-remember-previous-location) "???")
465 (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
466 (insert tpl)
467 (goto-char (point-min))
469 ;; Simple %-escapes
470 (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
471 (when (and initial (equal (match-string 0) "%i"))
472 (save-match-data
473 (let* ((lead (buffer-substring
474 (point-at-bol) (match-beginning 0))))
475 (setq v-i (mapconcat 'identity
476 (org-split-string initial "\n")
477 (concat "\n" lead))))))
478 (replace-match
479 (or (eval (intern (concat "v-" (match-string 1)))) "")
480 t t))
482 ;; %[] Insert contents of a file.
483 (goto-char (point-min))
484 (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
485 (let ((start (match-beginning 0))
486 (end (match-end 0))
487 (filename (expand-file-name (match-string 1))))
488 (goto-char start)
489 (delete-region start end)
490 (condition-case error
491 (insert-file-contents filename)
492 (error (insert (format "%%![Couldn't insert %s: %s]"
493 filename error))))))
494 ;; %() embedded elisp
495 (goto-char (point-min))
496 (while (re-search-forward "%\\((.+)\\)" nil t)
497 (goto-char (match-beginning 0))
498 (let ((template-start (point)))
499 (forward-char 1)
500 (let ((result
501 (condition-case error
502 (eval (read (current-buffer)))
503 (error (format "%%![Error: %s]" error)))))
504 (delete-region template-start (point))
505 (insert result))))
507 ;; From the property list
508 (when plist-p
509 (goto-char (point-min))
510 (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
511 (and (setq x (or (plist-get org-store-link-plist
512 (intern (match-string 1))) ""))
513 (replace-match x t t))))
515 ;; Turn on org-mode in the remember buffer, set local variables
516 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
517 (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
518 (org-set-local 'org-default-notes-file file))
519 (if headline
520 (org-set-local 'org-remember-default-headline headline))
521 ;; Interactive template entries
522 (goto-char (point-min))
523 (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
524 (setq char (if (match-end 3) (match-string 3))
525 prompt (if (match-end 2) (match-string 2)))
526 (goto-char (match-beginning 0))
527 (replace-match "")
528 (setq completions nil default nil)
529 (when prompt
530 (setq completions (org-split-string prompt "|")
531 prompt (pop completions)
532 default (car completions)
533 histvar (intern (concat
534 "org-remember-template-prompt-history::"
535 (or prompt "")))
536 completions (mapcar 'list completions)))
537 (cond
538 ((member char '("G" "g"))
539 (let* ((org-last-tags-completion-table
540 (org-global-tags-completion-table
541 (if (equal char "G") (org-agenda-files) (and file (list file)))))
542 (org-add-colon-after-tag-completion t)
543 (ins (org-ido-completing-read
544 (if prompt (concat prompt ": ") "Tags: ")
545 'org-tags-completion-function nil nil nil
546 'org-tags-history)))
547 (setq ins (mapconcat 'identity
548 (org-split-string ins (org-re "[^[:alnum:]_@]+"))
549 ":"))
550 (when (string-match "\\S-" ins)
551 (or (equal (char-before) ?:) (insert ":"))
552 (insert ins)
553 (or (equal (char-after) ?:) (insert ":")))))
554 ((equal char "C")
555 (cond ((= (length clipboards) 1) (insert (car clipboards)))
556 ((> (length clipboards) 1)
557 (insert (read-string "Clipboard/kill value: "
558 (car clipboards) '(clipboards . 1)
559 (car clipboards))))))
560 ((equal char "L")
561 (cond ((= (length clipboards) 1)
562 (org-insert-link 0 (car clipboards)))
563 ((> (length clipboards) 1)
564 (org-insert-link 0 (read-string "Clipboard/kill value: "
565 (car clipboards)
566 '(clipboards . 1)
567 (car clipboards))))))
568 ((equal char "p")
569 (let*
570 ((prop (org-substring-no-properties prompt))
571 (pall (concat prop "_ALL"))
572 (allowed
573 (with-current-buffer
574 (get-buffer (file-name-nondirectory file))
575 (or (cdr (assoc pall org-file-properties))
576 (cdr (assoc pall org-global-properties))
577 (cdr (assoc pall org-global-properties-fixed)))))
578 (existing (with-current-buffer
579 (get-buffer (file-name-nondirectory file))
580 (mapcar 'list (org-property-values prop))))
581 (propprompt (concat "Value for " prop ": "))
582 (val (if allowed
583 (org-completing-read
584 propprompt
585 (mapcar 'list (org-split-string allowed "[ \t]+"))
586 nil 'req-match)
587 (org-completing-read-no-ido propprompt existing nil nil
588 "" nil ""))))
589 (org-set-property prop val)))
590 (char
591 ;; These are the date/time related ones
592 (setq org-time-was-given (equal (upcase char) char))
593 (setq time (org-read-date (equal (upcase char) "U") t nil
594 prompt))
595 (org-insert-time-stamp time org-time-was-given
596 (member char '("u" "U"))
597 nil nil (list org-end-time-was-given)))
599 (let (org-completion-use-ido)
600 (insert (org-completing-read-no-ido
601 (concat (if prompt prompt "Enter string")
602 (if default (concat " [" default "]"))
603 ": ")
604 completions nil nil nil histvar default))))))
605 (goto-char (point-min))
606 (if (re-search-forward "%\\?" nil t)
607 (replace-match "")
608 (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
609 (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
610 (when (save-excursion
611 (goto-char (point-min))
612 (re-search-forward "%&" nil t))
613 (replace-match "")
614 (org-set-local 'org-jump-to-target-location t))
615 (when org-remember-backup-directory
616 (unless (file-directory-p org-remember-backup-directory)
617 (make-directory org-remember-backup-directory))
618 (org-set-local 'auto-save-file-name-transforms nil)
619 (setq buffer-file-name
620 (expand-file-name
621 (format-time-string "remember-%Y-%m-%d-%H-%M-%S")
622 org-remember-backup-directory))
623 (save-buffer)
624 (org-set-local 'auto-save-visited-file-name t)
625 (auto-save-mode 1))
626 (when (save-excursion
627 (goto-char (point-min))
628 (re-search-forward "%!" nil t))
629 (replace-match "")
630 (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
632 (defun org-remember-finish-immediately ()
633 "File remember note immediately.
634 This should be run in `post-command-hook' and will remove itself
635 from that hook."
636 (remove-hook 'post-command-hook 'org-remember-finish-immediately)
637 (org-remember-finalize))
639 (defun org-remember-visit-immediately ()
640 "File remember note immediately.
641 This should be run in `post-command-hook' and will remove itself
642 from that hook."
643 (org-remember '(16))
644 (goto-char (or (text-property-any
645 (point) (save-excursion (org-end-of-subtree t t))
646 'org-position-cursor t)
647 (point)))
648 (message "%s"
649 (format
650 (substitute-command-keys
651 "Restore window configuration with \\[jump-to-register] %c")
652 remember-register)))
654 (defvar org-clock-marker) ; Defined in org.el
655 (defun org-remember-finalize ()
656 "Finalize the remember process."
657 (interactive)
658 (unless org-remember-mode
659 (error "This does not seem to be a remember buffer for Org-mode"))
660 (run-hooks 'org-remember-before-finalize-hook)
661 (unless (fboundp 'remember-finalize)
662 (defalias 'remember-finalize 'remember-buffer))
663 (when (and org-clock-marker
664 (equal (marker-buffer org-clock-marker) (current-buffer)))
665 ;; the clock is running in this buffer.
666 (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
667 (or (eq org-remember-clock-out-on-exit t)
668 (and org-remember-clock-out-on-exit
669 (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
670 (let (org-log-note-clock-out) (org-clock-out))))
671 (when buffer-file-name
672 (save-buffer))
673 (remember-finalize))
675 (defun org-remember-kill ()
676 "Abort the current remember process."
677 (interactive)
678 (let ((org-note-abort t))
679 (org-remember-finalize)))
681 ;;;###autoload
682 (defun org-remember (&optional goto org-force-remember-template-char)
683 "Call `remember'. If this is already a remember buffer, re-apply template.
684 If there is an active region, make sure remember uses it as initial content
685 of the remember buffer.
687 When called interactively with a `C-u' prefix argument GOTO, don't remember
688 anything, just go to the file/headline where the selected template usually
689 stores its notes. With a double prefix arg `C-u C-u', go to the last
690 note stored by remember.
692 Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
693 associated with a template in `org-remember-templates'."
694 (interactive "P")
695 (org-require-remember)
696 (cond
697 ((equal goto '(4)) (org-go-to-remember-target))
698 ((equal goto '(16)) (org-remember-goto-last-stored))
700 ;; set temporary variables that will be needed in
701 ;; `org-select-remember-template'
702 (setq org-select-template-temp-major-mode major-mode)
703 (setq org-select-template-original-buffer (current-buffer))
704 (if org-remember-mode
705 (progn
706 (when (< (length org-remember-templates) 2)
707 (error "No other template available"))
708 (erase-buffer)
709 (let ((annotation (plist-get org-store-link-plist :annotation))
710 (initial (plist-get org-store-link-plist :initial)))
711 (org-remember-apply-template))
712 (message "Press C-c C-c to remember data"))
713 (if (org-region-active-p)
714 (org-do-remember (buffer-substring (point) (mark)))
715 (org-do-remember))))))
717 (defvar org-remember-last-stored-marker (make-marker)
718 "Marker pointing to the entry most recently stored with `org-remember'.")
720 (defun org-remember-goto-last-stored ()
721 "Go to the location where the last remember note was stored."
722 (interactive)
723 (org-goto-marker-or-bmk org-remember-last-stored-marker
724 "org-remember-last-stored")
725 (message "This is the last note stored by remember"))
727 (defun org-go-to-remember-target (&optional template-key)
728 "Go to the target location of a remember template.
729 The user is queried for the template."
730 (interactive)
731 (let* (org-select-template-temp-major-mode
732 (entry (org-select-remember-template template-key))
733 (file (nth 1 entry))
734 (heading (nth 2 entry))
735 visiting)
736 (unless (and file (stringp file) (string-match "\\S-" file))
737 (setq file org-default-notes-file))
738 (when (and file (not (file-name-absolute-p file)))
739 (setq file (expand-file-name file org-directory)))
740 (unless (and heading (stringp heading) (string-match "\\S-" heading))
741 (setq heading org-remember-default-headline))
742 (setq visiting (org-find-base-buffer-visiting file))
743 (if (not visiting) (find-file-noselect file))
744 (switch-to-buffer (or visiting (get-file-buffer file)))
745 (widen)
746 (goto-char (point-min))
747 (if (re-search-forward
748 (concat "^\\*+[ \t]+" (regexp-quote heading)
749 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
750 nil t)
751 (goto-char (match-beginning 0))
752 (error "Target headline not found: %s" heading))))
754 ;; FIXME (bzg): let's clean up of final empty lines happen only once
755 ;; (see the org-remember-delete-empty-lines-at-end option below)
756 ;;;###autoload
757 (defun org-remember-handler ()
758 "Store stuff from remember.el into an org file.
759 When the template has specified a file and a headline, the entry is filed
760 there, or in the location defined by `org-default-notes-file' and
761 `org-remember-default-headline'.
763 If no defaults have been defined, or if the current prefix argument
764 is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
765 process is used to select the target location.
767 When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
768 the entry is filed to the same location as the previous note.
770 When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
771 the entry is filed as a subentry of the entry where the clock is
772 currently running.
774 When `C-u' has been used as prefix argument, the note is stored and emacs
775 moves point to the new location of the note, so that editing can be
776 continued there (similar to inserting \"%&\" into the template).
778 Before storing the note, the function ensures that the text has an
779 org-mode-style headline, i.e. a first line that starts with
780 a \"*\". If not, a headline is constructed from the current date and
781 some additional data.
783 If the variable `org-adapt-indentation' is non-nil, the entire text is
784 also indented so that it starts in the same column as the headline
785 \(i.e. after the stars).
787 See also the variable `org-reverse-note-order'."
788 (when (and (equal current-prefix-arg 2)
789 (not (marker-buffer org-clock-marker)))
790 (error "No running clock"))
791 (when (org-bound-and-true-p org-jump-to-target-location)
792 (let* ((end (min (point-max) (1+ (point))))
793 (beg (point)))
794 (if (= end beg) (setq beg (1- beg)))
795 (put-text-property beg end 'org-position-cursor t)))
796 (goto-char (point-min))
797 (while (looking-at "^[ \t]*\n\\|^##.*\n")
798 (replace-match ""))
799 (when org-remember-delete-empty-lines-at-end
800 (goto-char (point-max))
801 (beginning-of-line 1)
802 (while (and (looking-at "[ \t]*$\\|##.*") (> (point) 1))
803 (delete-region (1- (point)) (point-max))
804 (beginning-of-line 1)))
805 (catch 'quit
806 (if org-note-abort (throw 'quit t))
807 (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
808 (backup-file
809 (and buffer-file-name
810 (equal (file-name-directory buffer-file-name)
811 (file-name-as-directory
812 (expand-file-name org-remember-backup-directory)))
813 (string-match "^remember-[0-9]\\{4\\}"
814 (file-name-nondirectory buffer-file-name))
815 buffer-file-name))
817 (dummy
818 (unless (string-match "\\S-" (buffer-string))
819 (message "Nothing to remember")
820 (and backup-file
821 (ignore-errors
822 (delete-file backup-file)
823 (delete-file (concat backup-file "~"))))
824 (set-buffer-modified-p nil)
825 (throw 'quit t)))
826 (previousp (and (member current-prefix-arg '((16) 0))
827 org-remember-previous-location))
828 (clockp (equal current-prefix-arg 2))
829 (fastp (org-xor (equal current-prefix-arg 1)
830 org-remember-store-without-prompt))
831 (file (cond
832 (fastp org-default-notes-file)
833 ((and (eq org-remember-interactive-interface 'refile)
834 org-refile-targets)
835 org-default-notes-file)
836 ((not previousp)
837 (org-get-org-file))))
838 (heading org-remember-default-headline)
839 (visiting (and file (org-find-base-buffer-visiting file)))
840 (org-startup-folded nil)
841 (org-startup-align-all-tables nil)
842 (org-goto-start-pos 1)
843 spos exitcmd level reversed txt text-before-node-creation)
844 (when (equal current-prefix-arg '(4))
845 (setq visitp t))
846 (when previousp
847 (setq file (car org-remember-previous-location)
848 visiting (and file (org-find-base-buffer-visiting file))
849 heading (cdr org-remember-previous-location)
850 fastp t))
851 (when clockp
852 (setq file (buffer-file-name (marker-buffer org-clock-marker))
853 visiting (and file (org-find-base-buffer-visiting file))
854 heading org-clock-heading-for-remember
855 fastp t))
856 (setq current-prefix-arg nil)
857 ;; Modify text so that it becomes a nice subtree which can be inserted
858 ;; into an org tree.
859 (when org-remember-delete-empty-lines-at-end
860 (goto-char (point-min))
861 (if (re-search-forward "[ \t\n]+\\'" nil t)
862 ;; remove empty lines at end
863 (replace-match "")))
864 (goto-char (point-min))
865 (unless (looking-at org-outline-regexp)
866 ;; add a headline
867 (setq text-before-node-creation (buffer-string))
868 (insert (concat "* " (current-time-string)
869 " (" (remember-buffer-desc) ")\n"))
870 (backward-char 1)
871 (when org-adapt-indentation
872 (while (re-search-forward "^" nil t)
873 (insert " "))))
874 ;; Delete final empty lines
875 (when org-remember-delete-empty-lines-at-end
876 (goto-char (point-min))
877 (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
878 (replace-match "\n\n")
879 (if (re-search-forward "[ \t\n]*\\'")
880 (replace-match "\n"))))
881 (goto-char (point-min))
882 (setq txt (buffer-string))
883 (org-save-markers-in-region (point-min) (point-max))
884 (set-buffer-modified-p nil)
885 (when (and (eq org-remember-interactive-interface 'refile)
886 (not fastp))
887 (org-refile nil (or visiting (find-file-noselect file)))
888 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
889 (save-excursion
890 (bookmark-jump "org-refile-last-stored")
891 (bookmark-set "org-remember-last-stored")
892 (move-marker org-remember-last-stored-marker (point)))
893 (throw 'quit t))
894 ;; Find the file
895 (with-current-buffer (or visiting (find-file-noselect file))
896 (unless (or (org-mode-p) (member heading '(top bottom)))
897 (error "Target files for notes must be in Org-mode if not filing to top/bottom"))
898 (save-excursion
899 (save-restriction
900 (widen)
901 (setq reversed (org-notes-order-reversed-p))
903 ;; Find the default location
904 (when heading
905 (cond
906 ((not (org-mode-p))
907 (if (eq heading 'top)
908 (goto-char (point-min))
909 (goto-char (point-max))
910 (or (bolp) (newline)))
911 (insert text-before-node-creation)
912 (when remember-save-after-remembering
913 (save-buffer)
914 (if (not visiting) (kill-buffer (current-buffer))))
915 (throw 'quit t))
916 ((eq heading 'top)
917 (goto-char (point-min))
918 (or (looking-at org-outline-regexp)
919 (re-search-forward org-outline-regexp nil t))
920 (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
921 ((eq heading 'bottom)
922 (goto-char (point-max))
923 (or (bolp) (newline))
924 (setq org-goto-start-pos (point)))
925 ((and (stringp heading) (string-match "\\S-" heading))
926 (goto-char (point-min))
927 (if (re-search-forward
928 (concat "^\\*+[ \t]+" (regexp-quote heading)
929 (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
930 nil t)
931 (setq org-goto-start-pos (match-beginning 0))
932 (when fastp
933 (goto-char (point-max))
934 (unless (bolp) (newline))
935 (insert "* " heading "\n")
936 (setq org-goto-start-pos (point-at-bol 0)))))
937 (t (goto-char (point-min)) (setq org-goto-start-pos (point)
938 heading 'top))))
940 ;; Ask the User for a location, using the appropriate interface
941 (cond
942 ((and fastp (memq heading '(top bottom)))
943 (setq spos org-goto-start-pos
944 exitcmd (if (eq heading 'top) 'left nil)))
945 (fastp (setq spos org-goto-start-pos
946 exitcmd 'return))
947 ((eq org-remember-interactive-interface 'outline)
948 (setq spos (org-get-location (current-buffer)
949 org-remember-help)
950 exitcmd (cdr spos)
951 spos (car spos)))
952 ((eq org-remember-interactive-interface 'outline-path-completion)
953 (let ((org-refile-targets '((nil . (:maxlevel . 10))))
954 (org-refile-use-outline-path t))
955 (setq spos (org-refile-get-location "Heading: ")
956 exitcmd 'return
957 spos (nth 3 spos))))
958 (t (error "This should not happen")))
959 (if (not spos) (throw 'quit nil)) ; return nil to show we did
960 ; not handle this note
961 (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
962 (goto-char spos)
963 (cond ((org-on-heading-p t)
964 (org-back-to-heading t)
965 (setq level (funcall outline-level))
966 (cond
967 ((eq exitcmd 'return)
968 ;; sublevel of current
969 (setq org-remember-previous-location
970 (cons (abbreviate-file-name file)
971 (org-get-heading 'notags)))
972 (if reversed
973 (outline-next-heading)
974 (org-end-of-subtree t)
975 (if (not (bolp))
976 (if (looking-at "[ \t]*\n")
977 (beginning-of-line 2)
978 (end-of-line 1)
979 (insert "\n"))))
980 (org-paste-subtree (org-get-valid-level level 1) txt)
981 (and org-auto-align-tags (org-set-tags nil t))
982 (bookmark-set "org-remember-last-stored")
983 (move-marker org-remember-last-stored-marker (point)))
984 ((eq exitcmd 'left)
985 ;; before current
986 (org-paste-subtree level txt)
987 (and org-auto-align-tags (org-set-tags nil t))
988 (bookmark-set "org-remember-last-stored")
989 (move-marker org-remember-last-stored-marker (point)))
990 ((eq exitcmd 'right)
991 ;; after current
992 (org-end-of-subtree t)
993 (org-paste-subtree level txt)
994 (and org-auto-align-tags (org-set-tags nil t))
995 (bookmark-set "org-remember-last-stored")
996 (move-marker org-remember-last-stored-marker (point)))
997 (t (error "This should not happen"))))
999 ((eq heading 'bottom)
1000 (org-paste-subtree 1 txt)
1001 (and org-auto-align-tags (org-set-tags nil t))
1002 (bookmark-set "org-remember-last-stored")
1003 (move-marker org-remember-last-stored-marker (point)))
1005 ((and (bobp) (not reversed))
1006 ;; Put it at the end, one level below level 1
1007 (save-restriction
1008 (widen)
1009 (goto-char (point-max))
1010 (if (not (bolp)) (newline))
1011 (org-paste-subtree (org-get-valid-level 1 1) txt)
1012 (and org-auto-align-tags (org-set-tags nil t))
1013 (bookmark-set "org-remember-last-stored")
1014 (move-marker org-remember-last-stored-marker (point))))
1016 ((and (bobp) reversed)
1017 ;; Put it at the start, as level 1
1018 (save-restriction
1019 (widen)
1020 (goto-char (point-min))
1021 (re-search-forward "^\\*+ " nil t)
1022 (beginning-of-line 1)
1023 (org-paste-subtree 1 txt)
1024 (and org-auto-align-tags (org-set-tags nil t))
1025 (bookmark-set "org-remember-last-stored")
1026 (move-marker org-remember-last-stored-marker (point))))
1028 ;; Put it right there, with automatic level determined by
1029 ;; org-paste-subtree or from prefix arg
1030 (org-paste-subtree
1031 (if (numberp current-prefix-arg) current-prefix-arg)
1032 txt)
1033 (and org-auto-align-tags (org-set-tags nil t))
1034 (bookmark-set "org-remember-last-stored")
1035 (move-marker org-remember-last-stored-marker (point))))
1037 (when remember-save-after-remembering
1038 (save-buffer)
1039 (if (and (not visiting)
1040 (not (equal (marker-buffer org-clock-marker)
1041 (current-buffer))))
1042 (kill-buffer (current-buffer))))
1043 (when org-remember-auto-remove-backup-files
1044 (when backup-file
1045 (ignore-errors
1046 (delete-file backup-file)
1047 (delete-file (concat backup-file "~"))))
1048 (when org-remember-backup-directory
1049 (let ((n (length
1050 (directory-files
1051 org-remember-backup-directory nil
1052 "^remember-.*[0-9]$"))))
1053 (when (> n 0)
1054 (message
1055 "%d backup files (unfinished remember calls) in %s"
1056 n org-remember-backup-directory))))))))))
1058 t) ;; return t to indicate that we took care of this note.
1060 (defun org-do-remember (&optional initial)
1061 "Call remember."
1062 (remember initial))
1064 (defun org-require-remember ()
1065 "Make sure remember is loaded, or install our own emergency version of it."
1066 (condition-case nil
1067 (require 'remember)
1068 (error
1069 ;; Lets install our own micro version of remember
1070 (defvar remember-register ?R)
1071 (defvar remember-mode-hook nil)
1072 (defvar remember-handler-functions nil)
1073 (defvar remember-buffer "*Remember*")
1074 (defvar remember-save-after-remembering t)
1075 (defvar remember-annotation-functions '(buffer-file-name))
1076 (defun remember-finalize ()
1077 (run-hook-with-args-until-success 'remember-handler-functions)
1078 (when (equal remember-buffer (buffer-name))
1079 (kill-buffer (current-buffer))
1080 (jump-to-register remember-register)))
1081 (defun remember-mode ()
1082 (fundamental-mode)
1083 (setq mode-name "Remember")
1084 (run-hooks 'remember-mode-hook))
1085 (defun remember (&optional initial)
1086 (window-configuration-to-register remember-register)
1087 (let* ((annotation (run-hook-with-args-until-success
1088 'remember-annotation-functions)))
1089 (switch-to-buffer-other-window (get-buffer-create remember-buffer))
1090 (remember-mode)))
1091 (defun remember-buffer-desc ()
1092 (buffer-substring (point-min) (save-excursion (goto-char (point-min))
1093 (point-at-eol)))))))
1095 (provide 'org-remember)
1097 ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
1099 ;;; org-remember.el ends here