org-remember.el 36 KB

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