org-remember.el 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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.13pre02
  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. template 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-before-finalize-hook nil
  176. "Hook that is run right before a remember process is finalized.
  177. The remember buffer is still current when this hook runs."
  178. :group 'org-remember
  179. :type 'hook)
  180. (defvar org-remember-mode-map (make-sparse-keymap)
  181. "Keymap for org-remember-mode, a minor mode.
  182. Use this map to set additional keybindings for when Org-mode is used
  183. for a Remember buffer.")
  184. (defvar org-remember-mode-hook nil
  185. "Hook for the minor `org-remember-mode'.")
  186. (define-minor-mode org-remember-mode
  187. "Minor mode for special key bindings in a remember buffer."
  188. nil " Rem" org-remember-mode-map
  189. (run-hooks 'org-remember-mode-hook))
  190. (define-key org-remember-mode-map "\C-c\C-c" 'org-remember-finalize)
  191. (define-key org-remember-mode-map "\C-c\C-k" 'org-remember-kill)
  192. (defcustom org-remember-clock-out-on-exit 'query
  193. "Non-nil means, stop the clock when exiting a clocking remember buffer.
  194. This only applies if the clock is running in the remember buffer. If the
  195. clock is not stopped, it continues to run in the storage location.
  196. Instead of nil or t, this may also be the symbol `query' to prompt the
  197. user each time a remember buffer with a running clock is filed away. "
  198. :group 'org-remember
  199. :type '(choice
  200. (const :tag "Never" nil)
  201. (const :tag "Always" t)
  202. (const :tag "Query user" query)))
  203. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  204. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  205. ;;;###autoload
  206. (defun org-remember-insinuate ()
  207. "Setup remember.el for use with Org-mode."
  208. (org-require-remember)
  209. (setq remember-annotation-functions '(org-remember-annotation))
  210. (setq remember-handler-functions '(org-remember-handler))
  211. (add-hook 'remember-mode-hook 'org-remember-apply-template))
  212. ;;;###autoload
  213. (defun org-remember-annotation ()
  214. "Return a link to the current location as an annotation for remember.el.
  215. If you are using Org-mode files as target for data storage with
  216. remember.el, then the annotations should include a link compatible with the
  217. conventions in Org-mode. This function returns such a link."
  218. (org-store-link nil))
  219. (defconst org-remember-help
  220. "Select a destination location for the note.
  221. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  222. RET on headline -> Store as sublevel entry to current headline
  223. RET at beg-of-buf -> Append to file as level 2 headline
  224. <left>/<right> -> before/after current headline, same headings level")
  225. (defvar org-jump-to-target-location nil)
  226. (defvar org-remember-previous-location nil)
  227. (defvar org-force-remember-template-char) ;; dynamically scoped
  228. ;; Save the major mode of the buffer we called remember from
  229. (defvar org-select-template-temp-major-mode nil)
  230. ;; Temporary store the buffer where remember was called from
  231. (defvar org-select-template-original-buffer nil)
  232. (defun org-select-remember-template (&optional use-char)
  233. (when org-remember-templates
  234. (let* ((pre-selected-templates
  235. (mapcar
  236. (lambda (tpl)
  237. (let ((ctxt (nth 5 tpl))
  238. (mode org-select-template-temp-major-mode)
  239. (buf org-select-template-original-buffer))
  240. (and (or (not ctxt) (eq ctxt t)
  241. (and (listp ctxt) (memq mode ctxt))
  242. (and (functionp ctxt)
  243. (with-current-buffer buf
  244. ;; Protect the user-defined function from error
  245. (condition-case nil (funcall ctxt) (error nil)))))
  246. tpl)))
  247. org-remember-templates))
  248. ;; If no template at this point, add the default templates:
  249. (pre-selected-templates1
  250. (if (not (delq nil pre-selected-templates))
  251. (mapcar (lambda(x) (if (not (nth 5 x)) x))
  252. org-remember-templates)
  253. pre-selected-templates))
  254. ;; Then unconditionnally add template for any contexts
  255. (pre-selected-templates2
  256. (append (mapcar (lambda(x) (if (eq (nth 5 x) t) x))
  257. org-remember-templates)
  258. (delq nil pre-selected-templates1)))
  259. (templates (mapcar (lambda (x)
  260. (if (stringp (car x))
  261. (append (list (nth 1 x) (car x)) (cddr x))
  262. (append (list (car x) "") (cdr x))))
  263. (delq nil pre-selected-templates2)))
  264. (char (or use-char
  265. (cond
  266. ((= (length templates) 1)
  267. (caar templates))
  268. ((and (boundp 'org-force-remember-template-char)
  269. org-force-remember-template-char)
  270. (if (stringp org-force-remember-template-char)
  271. (string-to-char org-force-remember-template-char)
  272. org-force-remember-template-char))
  273. (t
  274. (message "Select template: %s"
  275. (mapconcat
  276. (lambda (x)
  277. (cond
  278. ((not (string-match "\\S-" (nth 1 x)))
  279. (format "[%c]" (car x)))
  280. ((equal (downcase (car x))
  281. (downcase (aref (nth 1 x) 0)))
  282. (format "[%c]%s" (car x)
  283. (substring (nth 1 x) 1)))
  284. (t (format "[%c]%s" (car x) (nth 1 x)))))
  285. templates " "))
  286. (let ((inhibit-quit t) (char0 (read-char-exclusive)))
  287. (when (equal char0 ?\C-g)
  288. (jump-to-register remember-register)
  289. (kill-buffer remember-buffer))
  290. char0))))))
  291. (cddr (assoc char templates)))))
  292. (defun org-get-x-clipboard (value)
  293. "Get the value of the x clibboard, compatible with XEmacs, and GNU Emacs 21."
  294. (if (eq window-system 'x)
  295. (let ((x (org-get-x-clipboard-compat value)))
  296. (if x (org-no-properties x)))))
  297. ;;;###autoload
  298. (defun org-remember-apply-template (&optional use-char skip-interactive)
  299. "Initialize *remember* buffer with template, invoke `org-mode'.
  300. This function should be placed into `remember-mode-hook' and in fact requires
  301. to be run from that hook to function properly."
  302. (when (and (boundp 'initial) (stringp initial))
  303. (setq initial (org-no-properties initial))
  304. (remove-text-properties 0 (length initial) '(read-only t) initial))
  305. (if org-remember-templates
  306. (let* ((entry (org-select-remember-template use-char))
  307. (ct (or org-overriding-default-time (org-current-time)))
  308. (dct (decode-time ct))
  309. (ct1
  310. (if (< (nth 2 dct) org-extend-today-until)
  311. (encode-time 0 59 23 (1- (nth 3 dct)) (nth 4 dct) (nth 5 dct))
  312. ct))
  313. (tpl (car entry))
  314. (plist-p (if org-store-link-plist t nil))
  315. (file (if (and (nth 1 entry)
  316. (or (and (stringp (nth 1 entry))
  317. (string-match "\\S-" (nth 1 entry)))
  318. (functionp (nth 1 entry))))
  319. (nth 1 entry)
  320. org-default-notes-file))
  321. (headline (nth 2 entry))
  322. (v-c (and (> (length kill-ring) 0) (current-kill 0)))
  323. (v-x (or (org-get-x-clipboard 'PRIMARY)
  324. (org-get-x-clipboard 'CLIPBOARD)
  325. (org-get-x-clipboard 'SECONDARY)))
  326. (v-t (format-time-string (car org-time-stamp-formats) ct))
  327. (v-T (format-time-string (cdr org-time-stamp-formats) ct))
  328. (v-u (concat "[" (substring v-t 1 -1) "]"))
  329. (v-U (concat "[" (substring v-T 1 -1) "]"))
  330. ;; `initial' and `annotation' are bound in `remember'
  331. (v-i (if (boundp 'initial) initial))
  332. (v-a (if (and (boundp 'annotation) annotation)
  333. (if (equal annotation "[[]]") "" annotation)
  334. ""))
  335. (clipboards (remove nil (list v-i
  336. (org-get-x-clipboard 'PRIMARY)
  337. (org-get-x-clipboard 'CLIPBOARD)
  338. (org-get-x-clipboard 'SECONDARY)
  339. v-c)))
  340. (v-A (if (and v-a
  341. (string-match "\\[\\(\\[.*?\\]\\)\\(\\[.*?\\]\\)?\\]" v-a))
  342. (replace-match "[\\1[%^{Link description}]]" nil nil v-a)
  343. v-a))
  344. (v-n user-full-name)
  345. (v-k (if (marker-buffer org-clock-marker)
  346. (substring-no-properties org-clock-heading)))
  347. (v-K (if (marker-buffer org-clock-marker)
  348. (org-make-link-string
  349. (buffer-file-name (marker-buffer org-clock-marker))
  350. org-clock-heading)))
  351. v-I
  352. (org-startup-folded nil)
  353. (org-inhibit-startup t)
  354. org-time-was-given org-end-time-was-given x
  355. prompt completions char time pos default histvar)
  356. (when (functionp file)
  357. (setq file (funcall file)))
  358. (when (and file (not (file-name-absolute-p file)))
  359. (setq file (expand-file-name file org-directory)))
  360. (setq org-store-link-plist
  361. (append (list :annotation v-a :initial v-i)
  362. org-store-link-plist))
  363. (unless tpl (setq tpl "") (message "No template") (ding) (sit-for 1))
  364. (erase-buffer)
  365. (insert (substitute-command-keys
  366. (format
  367. "## %s \"%s\" -> \"* %s\"
  368. ## C-u C-c C-c like C-c C-c, and immediately visit note at target location
  369. ## C-0 C-c C-c \"%s\" -> \"* %s\"
  370. ## %s to select file and header location interactively.
  371. ## C-2 C-c C-c as child of the currently clocked item
  372. ## To switch templates, use `\\[org-remember]'. To abort use `C-c C-k'.\n\n"
  373. (if org-remember-store-without-prompt " C-c C-c" " C-1 C-c C-c")
  374. (abbreviate-file-name (or file org-default-notes-file))
  375. (or headline "")
  376. (or (car org-remember-previous-location) "???")
  377. (or (cdr org-remember-previous-location) "???")
  378. (if org-remember-store-without-prompt "C-1 C-c C-c" " C-c C-c"))))
  379. (insert tpl)
  380. (goto-char (point-min))
  381. ;; Simple %-escapes
  382. (while (re-search-forward "%\\([tTuUaiAcxkKI]\\)" nil t)
  383. (when (and initial (equal (match-string 0) "%i"))
  384. (save-match-data
  385. (let* ((lead (buffer-substring
  386. (point-at-bol) (match-beginning 0))))
  387. (setq v-i (mapconcat 'identity
  388. (org-split-string initial "\n")
  389. (concat "\n" lead))))))
  390. (replace-match
  391. (or (eval (intern (concat "v-" (match-string 1)))) "")
  392. t t))
  393. ;; %[] Insert contents of a file.
  394. (goto-char (point-min))
  395. (while (re-search-forward "%\\[\\(.+\\)\\]" nil t)
  396. (let ((start (match-beginning 0))
  397. (end (match-end 0))
  398. (filename (expand-file-name (match-string 1))))
  399. (goto-char start)
  400. (delete-region start end)
  401. (condition-case error
  402. (insert-file-contents filename)
  403. (error (insert (format "%%![Couldn't insert %s: %s]"
  404. filename error))))))
  405. ;; %() embedded elisp
  406. (goto-char (point-min))
  407. (while (re-search-forward "%\\((.+)\\)" nil t)
  408. (goto-char (match-beginning 0))
  409. (let ((template-start (point)))
  410. (forward-char 1)
  411. (let ((result
  412. (condition-case error
  413. (eval (read (current-buffer)))
  414. (error (format "%%![Error: %s]" error)))))
  415. (delete-region template-start (point))
  416. (insert result))))
  417. ;; From the property list
  418. (when plist-p
  419. (goto-char (point-min))
  420. (while (re-search-forward "%\\(:[-a-zA-Z]+\\)" nil t)
  421. (and (setq x (or (plist-get org-store-link-plist
  422. (intern (match-string 1))) ""))
  423. (replace-match x t t))))
  424. ;; Turn on org-mode in the remember buffer, set local variables
  425. (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1))
  426. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  427. (org-set-local 'org-default-notes-file file))
  428. (if headline
  429. (org-set-local 'org-remember-default-headline headline))
  430. ;; Interactive template entries
  431. (goto-char (point-min))
  432. (while (re-search-forward "%^\\({\\([^}]*\\)}\\)?\\([gGtTuUCLp]\\)?" nil t)
  433. (setq char (if (match-end 3) (match-string 3))
  434. prompt (if (match-end 2) (match-string 2)))
  435. (goto-char (match-beginning 0))
  436. (replace-match "")
  437. (setq completions nil default nil)
  438. (when prompt
  439. (setq completions (org-split-string prompt "|")
  440. prompt (pop completions)
  441. default (car completions)
  442. histvar (intern (concat
  443. "org-remember-template-prompt-history::"
  444. (or prompt "")))
  445. completions (mapcar 'list completions)))
  446. (cond
  447. ((member char '("G" "g"))
  448. (let* ((org-last-tags-completion-table
  449. (org-global-tags-completion-table
  450. (if (equal char "G") (org-agenda-files) (and file (list file)))))
  451. (org-add-colon-after-tag-completion t)
  452. (ins (org-ido-completing-read
  453. (if prompt (concat prompt ": ") "Tags: ")
  454. 'org-tags-completion-function nil nil nil
  455. 'org-tags-history)))
  456. (setq ins (mapconcat 'identity
  457. (org-split-string ins (org-re "[^[:alnum:]_@]+"))
  458. ":"))
  459. (when (string-match "\\S-" ins)
  460. (or (equal (char-before) ?:) (insert ":"))
  461. (insert ins)
  462. (or (equal (char-after) ?:) (insert ":")))))
  463. ((equal char "C")
  464. (cond ((= (length clipboards) 1) (insert (car clipboards)))
  465. ((> (length clipboards) 1)
  466. (insert (read-string "Clipboard/kill value: "
  467. (car clipboards) '(clipboards . 1)
  468. (car clipboards))))))
  469. ((equal char "L")
  470. (cond ((= (length clipboards) 1)
  471. (org-insert-link 0 (car clipboards)))
  472. ((> (length clipboards) 1)
  473. (org-insert-link 0 (read-string "Clipboard/kill value: "
  474. (car clipboards)
  475. '(clipboards . 1)
  476. (car clipboards))))))
  477. ((equal char "p")
  478. (let*
  479. ((prop (substring-no-properties prompt))
  480. (pall (concat prop "_ALL"))
  481. (allowed
  482. (with-current-buffer
  483. (get-buffer (file-name-nondirectory file))
  484. (or (cdr (assoc pall org-file-properties))
  485. (cdr (assoc pall org-global-properties))
  486. (cdr (assoc pall org-global-properties-fixed)))))
  487. (existing (with-current-buffer
  488. (get-buffer (file-name-nondirectory file))
  489. (mapcar 'list (org-property-values prop))))
  490. (propprompt (concat "Value for " prop ": "))
  491. (val (if allowed
  492. (org-completing-read
  493. propprompt
  494. (mapcar 'list (org-split-string allowed "[ \t]+"))
  495. nil 'req-match)
  496. (org-completing-read propprompt existing nil nil
  497. "" nil ""))))
  498. (org-set-property prop val)))
  499. (char
  500. ;; These are the date/time related ones
  501. (setq org-time-was-given (equal (upcase char) char))
  502. (setq time (org-read-date (equal (upcase char) "U") t nil
  503. prompt))
  504. (org-insert-time-stamp time org-time-was-given
  505. (member char '("u" "U"))
  506. nil nil (list org-end-time-was-given)))
  507. (t
  508. (let (org-completion-use-ido)
  509. (insert (org-completing-read
  510. (concat (if prompt prompt "Enter string")
  511. (if default (concat " [" default "]"))
  512. ": ")
  513. completions nil nil nil histvar default))))))
  514. (goto-char (point-min))
  515. (if (re-search-forward "%\\?" nil t)
  516. (replace-match "")
  517. (and (re-search-forward "^[^#\n]" nil t) (backward-char 1))))
  518. (let ((org-inhibit-startup t)) (org-mode) (org-remember-mode 1)))
  519. (when (save-excursion
  520. (goto-char (point-min))
  521. (re-search-forward "%&" nil t))
  522. (replace-match "")
  523. (org-set-local 'org-jump-to-target-location t))
  524. (when (save-excursion
  525. (goto-char (point-min))
  526. (re-search-forward "%!" nil t))
  527. (replace-match "")
  528. (add-hook 'post-command-hook 'org-remember-finish-immediately 'append)))
  529. (defun org-remember-finish-immediately ()
  530. "File remember note immediately.
  531. This should be run in `post-command-hook' and will remove itself
  532. from that hook."
  533. (remove-hook 'post-command-hook 'org-remember-finish-immediately)
  534. (org-remember-finalize))
  535. (defun org-remember-visit-immediately ()
  536. "File remember note immediately.
  537. This should be run in `post-command-hook' and will remove itself
  538. from that hook."
  539. (org-remember '(16))
  540. (goto-char (or (text-property-any
  541. (point) (save-excursion (org-end-of-subtree t t))
  542. 'org-position-cursor t)
  543. (point)))
  544. (message "%s"
  545. (format
  546. (substitute-command-keys
  547. "Restore window configuration with \\[jump-to-register] %c")
  548. remember-register)))
  549. (defvar org-clock-marker) ; Defined in org.el
  550. (defun org-remember-finalize ()
  551. "Finalize the remember process."
  552. (interactive)
  553. (unless org-remember-mode
  554. (error "This does not seem to be a remember buffer for Org-mode"))
  555. (run-hooks 'org-remember-before-finalize-hook)
  556. (unless (fboundp 'remember-finalize)
  557. (defalias 'remember-finalize 'remember-buffer))
  558. (when (and org-clock-marker
  559. (equal (marker-buffer org-clock-marker) (current-buffer)))
  560. ;; the clock is running in this buffer.
  561. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  562. (or (eq org-remember-clock-out-on-exit t)
  563. (and org-remember-clock-out-on-exit
  564. (y-or-n-p "The clock is running in this buffer. Clock out now? "))))
  565. (let (org-log-note-clock-out) (org-clock-out))))
  566. (when buffer-file-name
  567. (save-buffer)
  568. (setq buffer-file-name nil))
  569. (remember-finalize))
  570. (defun org-remember-kill ()
  571. "Abort the current remember process."
  572. (interactive)
  573. (let ((org-note-abort t))
  574. (org-remember-finalize)))
  575. ;;;###autoload
  576. (defun org-remember (&optional goto org-force-remember-template-char)
  577. "Call `remember'. If this is already a remember buffer, re-apply template.
  578. If there is an active region, make sure remember uses it as initial content
  579. of the remember buffer.
  580. When called interactively with a `C-u' prefix argument GOTO, don't remember
  581. anything, just go to the file/headline where the selected template usually
  582. stores its notes. With a double prefix arg `C-u C-u', go to the last
  583. note stored by remember.
  584. Lisp programs can set ORG-FORCE-REMEMBER-TEMPLATE-CHAR to a character
  585. associated with a template in `org-remember-templates'."
  586. (interactive "P")
  587. (org-require-remember)
  588. (cond
  589. ((equal goto '(4)) (org-go-to-remember-target))
  590. ((equal goto '(16)) (org-remember-goto-last-stored))
  591. (t
  592. ;; set temporary variables that will be needed in
  593. ;; `org-select-remember-template'
  594. (setq org-select-template-temp-major-mode major-mode)
  595. (setq org-select-template-original-buffer (current-buffer))
  596. (if org-remember-mode
  597. (progn
  598. (when (< (length org-remember-templates) 2)
  599. (error "No other template available"))
  600. (erase-buffer)
  601. (let ((annotation (plist-get org-store-link-plist :annotation))
  602. (initial (plist-get org-store-link-plist :initial)))
  603. (org-remember-apply-template))
  604. (message "Press C-c C-c to remember data"))
  605. (if (org-region-active-p)
  606. (org-do-remember (buffer-substring (point) (mark)))
  607. (org-do-remember))))))
  608. (defvar org-remember-last-stored-marker (make-marker)
  609. "Marker pointing to the entry most recently stored with `org-remember'.")
  610. (defun org-remember-goto-last-stored ()
  611. "Go to the location where the last remember note was stored."
  612. (interactive)
  613. (org-goto-marker-or-bmk org-remember-last-stored-marker
  614. "org-remember-last-stored")
  615. (message "This is the last note stored by remember"))
  616. (defun org-go-to-remember-target (&optional template-key)
  617. "Go to the target location of a remember template.
  618. The user is queried for the template."
  619. (interactive)
  620. (let* (org-select-template-temp-major-mode
  621. (entry (org-select-remember-template template-key))
  622. (file (nth 1 entry))
  623. (heading (nth 2 entry))
  624. visiting)
  625. (unless (and file (stringp file) (string-match "\\S-" file))
  626. (setq file org-default-notes-file))
  627. (when (and file (not (file-name-absolute-p file)))
  628. (setq file (expand-file-name file org-directory)))
  629. (unless (and heading (stringp heading) (string-match "\\S-" heading))
  630. (setq heading org-remember-default-headline))
  631. (setq visiting (org-find-base-buffer-visiting file))
  632. (if (not visiting) (find-file-noselect file))
  633. (switch-to-buffer (or visiting (get-file-buffer file)))
  634. (widen)
  635. (goto-char (point-min))
  636. (if (re-search-forward
  637. (concat "^\\*+[ \t]+" (regexp-quote heading)
  638. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  639. nil t)
  640. (goto-char (match-beginning 0))
  641. (error "Target headline not found: %s" heading))))
  642. ;;;###autoload
  643. (defun org-remember-handler ()
  644. "Store stuff from remember.el into an org file.
  645. When the template has specified a file and a headline, the entry is filed
  646. there, or in the location defined by `org-default-notes-file' and
  647. `org-remember-default-headline'.
  648. If no defaults have been defined, or if the current prefix argument
  649. is 1 (so you must use `C-1 C-c C-c' to exit remember), an interactive
  650. process is used to select the target location.
  651. When the prefix is 0 (i.e. when remember is exited with `C-0 C-c C-c'),
  652. the entry is filed to the same location as the previous note.
  653. When the prefix is 2 (i.e. when remember is exited with `C-2 C-c C-c'),
  654. the entry is fild as a subentry of the entry where the clock is
  655. currently running.
  656. When `C-u' has been used as prefix argument, the note is stored and emacs
  657. moves point to the new location of the note, so that editing can be
  658. continued there (smilar to inserting \"%&\" into the tempate).
  659. Before storing the note, the function ensures that the text has an
  660. org-mode-style headline, i.e. a first line that starts with
  661. a \"*\". If not, a headline is constructed from the current date and
  662. some additional data.
  663. If the variable `org-adapt-indentation' is non-nil, the entire text is
  664. also indented so that it starts in the same column as the headline
  665. \(i.e. after the stars).
  666. See also the variable `org-reverse-note-order'."
  667. (when (and (equal current-prefix-arg 2)
  668. (not (marker-buffer org-clock-marker)))
  669. (error "No running clock"))
  670. (when (org-bound-and-true-p org-jump-to-target-location)
  671. (let* ((end (min (point-max) (1+ (point))))
  672. (beg (point)))
  673. (if (= end beg) (setq beg (1- beg)))
  674. (put-text-property beg end 'org-position-cursor t)))
  675. (goto-char (point-min))
  676. (while (looking-at "^[ \t]*\n\\|^##.*\n")
  677. (replace-match ""))
  678. (goto-char (point-max))
  679. (beginning-of-line 1)
  680. (while (looking-at "[ \t]*$\\|##.*")
  681. (delete-region (1- (point)) (point-max))
  682. (beginning-of-line 1))
  683. (catch 'quit
  684. (if org-note-abort (throw 'quit nil))
  685. (let* ((visitp (org-bound-and-true-p org-jump-to-target-location))
  686. (previousp (and (member current-prefix-arg '((16) 0))
  687. org-remember-previous-location))
  688. (clockp (equal current-prefix-arg 2))
  689. (fastp (org-xor (equal current-prefix-arg 1)
  690. org-remember-store-without-prompt))
  691. (file (cond
  692. (fastp org-default-notes-file)
  693. ((and (eq org-remember-interactive-interface 'refile)
  694. org-refile-targets)
  695. org-default-notes-file)
  696. ((not previousp)
  697. (org-get-org-file))))
  698. (heading org-remember-default-headline)
  699. (visiting (and file (org-find-base-buffer-visiting file)))
  700. (org-startup-folded nil)
  701. (org-startup-align-all-tables nil)
  702. (org-goto-start-pos 1)
  703. spos exitcmd level reversed txt)
  704. (when (equal current-prefix-arg '(4))
  705. (setq visitp t))
  706. (when previousp
  707. (setq file (car org-remember-previous-location)
  708. visiting (and file (org-find-base-buffer-visiting file))
  709. heading (cdr org-remember-previous-location)
  710. fastp t))
  711. (when clockp
  712. (setq file (buffer-file-name (marker-buffer org-clock-marker))
  713. visiting (and file (org-find-base-buffer-visiting file))
  714. heading org-clock-heading-for-remember
  715. fastp t))
  716. (setq current-prefix-arg nil)
  717. ;; Modify text so that it becomes a nice subtree which can be inserted
  718. ;; into an org tree.
  719. (goto-char (point-min))
  720. (if (re-search-forward "[ \t\n]+\\'" nil t)
  721. ;; remove empty lines at end
  722. (replace-match ""))
  723. (goto-char (point-min))
  724. (unless (looking-at org-outline-regexp)
  725. ;; add a headline
  726. (insert (concat "* " (current-time-string)
  727. " (" (remember-buffer-desc) ")\n"))
  728. (backward-char 1)
  729. (when org-adapt-indentation
  730. (while (re-search-forward "^" nil t)
  731. (insert " "))))
  732. (goto-char (point-min))
  733. (if (re-search-forward "\n[ \t]*\n[ \t\n]*\\'" nil t)
  734. (replace-match "\n\n")
  735. (if (re-search-forward "[ \t\n]*\\'")
  736. (replace-match "\n")))
  737. (goto-char (point-min))
  738. (setq txt (buffer-string))
  739. (org-save-markers-in-region (point-min) (point-max))
  740. (when (and (eq org-remember-interactive-interface 'refile)
  741. (not fastp))
  742. (org-refile nil (or visiting (find-file-noselect file)))
  743. (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
  744. (save-excursion
  745. (bookmark-jump "org-refile-last-stored")
  746. (bookmark-set "org-remember-last-stored")
  747. (move-marker org-remember-last-stored-marker (point)))
  748. (throw 'quit t))
  749. ;; Find the file
  750. (if (not visiting) (find-file-noselect file))
  751. (with-current-buffer (or visiting (get-file-buffer file))
  752. (unless (org-mode-p)
  753. (error "Target files for remember notes must be in Org-mode"))
  754. (save-excursion
  755. (save-restriction
  756. (widen)
  757. (and (goto-char (point-min))
  758. (not (re-search-forward "^\\* " nil t))
  759. (insert "\n* " (or (and (stringp heading) heading)
  760. "Notes") "\n"))
  761. (setq reversed (org-notes-order-reversed-p))
  762. ;; Find the default location
  763. (when heading
  764. (cond
  765. ((eq heading 'top)
  766. (goto-char (point-min))
  767. (or (looking-at org-outline-regexp)
  768. (re-search-forward org-outline-regexp nil t))
  769. (setq org-goto-start-pos (or (match-beginning 0) (point-min))))
  770. ((eq heading 'bottom)
  771. (goto-char (point-max))
  772. (or (bolp) (newline))
  773. (setq org-goto-start-pos (point)))
  774. ((and (stringp heading) (string-match "\\S-" heading))
  775. (goto-char (point-min))
  776. (if (re-search-forward
  777. (concat "^\\*+[ \t]+" (regexp-quote heading)
  778. (org-re "\\([ \t]+:[[:alnum:]@_:]*\\)?[ \t]*$"))
  779. nil t)
  780. (setq org-goto-start-pos (match-beginning 0))
  781. (when fastp
  782. (goto-char (point-max))
  783. (unless (bolp) (newline))
  784. (insert "* " heading "\n")
  785. (setq org-goto-start-pos (point-at-bol 0)))))
  786. (t (goto-char (point-min)) (setq org-goto-start-pos (point)
  787. heading 'top))))
  788. ;; Ask the User for a location, using the appropriate interface
  789. (cond
  790. ((and fastp (memq heading '(top bottom)))
  791. (setq spos org-goto-start-pos
  792. exitcmd (if (eq heading 'top) 'left nil)))
  793. (fastp (setq spos org-goto-start-pos
  794. exitcmd 'return))
  795. ((eq org-remember-interactive-interface 'outline)
  796. (setq spos (org-get-location (current-buffer)
  797. org-remember-help)
  798. exitcmd (cdr spos)
  799. spos (car spos)))
  800. ((eq org-remember-interactive-interface 'outline-path-completion)
  801. (let ((org-refile-targets '((nil . (:maxlevel . 10))))
  802. (org-refile-use-outline-path t))
  803. (setq spos (org-refile-get-location "Heading: ")
  804. exitcmd 'return
  805. spos (nth 3 spos))))
  806. (t (error "This should not happen")))
  807. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  808. ; not handle this note
  809. (and visitp (run-with-idle-timer 0.01 nil 'org-remember-visit-immediately))
  810. (goto-char spos)
  811. (cond ((org-on-heading-p t)
  812. (org-back-to-heading t)
  813. (setq level (funcall outline-level))
  814. (cond
  815. ((eq exitcmd 'return)
  816. ;; sublevel of current
  817. (setq org-remember-previous-location
  818. (cons (abbreviate-file-name file)
  819. (org-get-heading 'notags)))
  820. (if reversed
  821. (outline-next-heading)
  822. (org-end-of-subtree t)
  823. (if (not (bolp))
  824. (if (looking-at "[ \t]*\n")
  825. (beginning-of-line 2)
  826. (end-of-line 1)
  827. (insert "\n"))))
  828. (org-paste-subtree (org-get-valid-level level 1) txt)
  829. (and org-auto-align-tags (org-set-tags nil t))
  830. (bookmark-set "org-remember-last-stored")
  831. (move-marker org-remember-last-stored-marker (point)))
  832. ((eq exitcmd 'left)
  833. ;; before current
  834. (org-paste-subtree level txt)
  835. (and org-auto-align-tags (org-set-tags nil t))
  836. (bookmark-set "org-remember-last-stored")
  837. (move-marker org-remember-last-stored-marker (point)))
  838. ((eq exitcmd 'right)
  839. ;; after current
  840. (org-end-of-subtree t)
  841. (org-paste-subtree level 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. (t (error "This should not happen"))))
  846. ((eq heading 'bottom)
  847. (org-paste-subtree 1 txt)
  848. (and org-auto-align-tags (org-set-tags nil t))
  849. (bookmark-set "org-remember-last-stored")
  850. (move-marker org-remember-last-stored-marker (point)))
  851. ((and (bobp) (not reversed))
  852. ;; Put it at the end, one level below level 1
  853. (save-restriction
  854. (widen)
  855. (goto-char (point-max))
  856. (if (not (bolp)) (newline))
  857. (org-paste-subtree (org-get-valid-level 1 1) txt)
  858. (and org-auto-align-tags (org-set-tags nil t))
  859. (bookmark-set "org-remember-last-stored")
  860. (move-marker org-remember-last-stored-marker (point))))
  861. ((and (bobp) reversed)
  862. ;; Put it at the start, as level 1
  863. (save-restriction
  864. (widen)
  865. (goto-char (point-min))
  866. (re-search-forward "^\\*+ " nil t)
  867. (beginning-of-line 1)
  868. (org-paste-subtree 1 txt)
  869. (and org-auto-align-tags (org-set-tags nil t))
  870. (bookmark-set "org-remember-last-stored")
  871. (move-marker org-remember-last-stored-marker (point))))
  872. (t
  873. ;; Put it right there, with automatic level determined by
  874. ;; org-paste-subtree or from prefix arg
  875. (org-paste-subtree
  876. (if (numberp current-prefix-arg) current-prefix-arg)
  877. txt)
  878. (and org-auto-align-tags (org-set-tags nil t))
  879. (bookmark-set "org-remember-last-stored")
  880. (move-marker org-remember-last-stored-marker (point))))
  881. (when remember-save-after-remembering
  882. (save-buffer)
  883. (if (and (not visiting)
  884. (not (equal (marker-buffer org-clock-marker)
  885. (current-buffer))))
  886. (kill-buffer (current-buffer)))))))))
  887. t) ;; return t to indicate that we took care of this note.
  888. (defun org-do-remember (&optional initial)
  889. "Call remember."
  890. (remember initial))
  891. (defun org-require-remember ()
  892. "Make sure remember is loaded, or install our own emergency version of it."
  893. (condition-case nil
  894. (require 'remember)
  895. (error
  896. ;; Lets install our own micro version of remember
  897. (defvar remember-register ?R)
  898. (defvar remember-mode-hook nil)
  899. (defvar remember-handler-functions nil)
  900. (defvar remember-buffer "*Remember*")
  901. (defvar remember-save-after-remembering t)
  902. (defvar remember-annotation-functions '(buffer-file-name))
  903. (defun remember-finalize ()
  904. (run-hook-with-args-until-success 'remember-handler-functions)
  905. (when (equal remember-buffer (buffer-name))
  906. (kill-buffer (current-buffer))
  907. (jump-to-register remember-register)))
  908. (defun remember-mode ()
  909. (fundamental-mode)
  910. (setq mode-name "Remember")
  911. (run-hooks 'remember-mode-hook))
  912. (defun remember (&optional initial)
  913. (window-configuration-to-register remember-register)
  914. (let* ((annotation (run-hook-with-args-until-success
  915. 'remember-annotation-functions)))
  916. (switch-to-buffer-other-window (get-buffer-create remember-buffer))
  917. (remember-mode)))
  918. (defun remember-buffer-desc ()
  919. (buffer-substring (point-min) (save-excursion (goto-char (point-min))
  920. (point-at-eol)))))))
  921. (provide 'org-remember)
  922. ;; arch-tag: 497f30d0-4bc3-4097-8622-2d27ac5f2698
  923. ;;; org-remember.el ends here