org-remember.el 37 KB

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