├── README.org ├── ya-org-capture-screehcast.gif └── ya-org-capture.el /README.org: -------------------------------------------------------------------------------- 1 | * README 2 | :PROPERTIES: 3 | :CREATED: [2020-07-28 Tue 16:34] 4 | :END: 5 | 6 | [[file:ya-org-capture-screehcast.gif][screencast]] 7 | [[https://ag91.github.io/blog/2020/07/28/how-to-integrate-yasnippet-and-yankpad-with-org-capture/][blog]] 8 | 9 | This is an integration between the marvellous [[https://orgmode.org/manual/Capture.html][org-capture]] and the 10 | amazing [[https://github.com/joaotavora/yasnippet][YASnippet]] and the fabulous [[https://github.com/Kungsgeten/yankpad][Yankpad]]. 11 | 12 | Just put the =.el= file in your lisp scripts directory and load it 13 | like this: 14 | 15 | #+begin_src elisp 16 | (use-package ya-org-capture 17 | :after yankpad 18 | :load-path "~/.emacs.d/lisp" 19 | :config 20 | (ya-org-capture/setup)) 21 | #+end_src 22 | 23 | Now any time you define a org-capture template, you can integrate the 24 | powerful templating systems of Yankpad and YASnippet. 25 | 26 | This is an example of capture template: 27 | 28 | #+begin_src elisp 29 | (setq org-capture-templates 30 | `(("i" "some yasnippet capture template" entry (file "/tmp/test.org") "* TODO %^{Some title} \n%(ya-org-capture/make-snippet \"img_\")" :empty-lines 2) 31 | ("s" "some yankpad capture template" entry (file "/tmp/test.org") "* TODO %^{Some title} \n%(ya-org-capture/make-snippet \"st\")" :empty-lines 2))) 32 | #+end_src 33 | 34 | Please check out these components docs ([[https://orgmode.org/manual/Using-capture.html#Using-capture][here]], [[https://kungsgeten.github.io/yankpad.html][here]], and [[http://joaotavora.github.io/yasnippet/][here]]) and 35 | authors because their work may give you great benefits! 36 | -------------------------------------------------------------------------------- /ya-org-capture-screehcast.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ag91/ya-org-capture/fd9ebeb176d74708e57b313479b3f7579104bbb4/ya-org-capture-screehcast.gif -------------------------------------------------------------------------------- /ya-org-capture.el: -------------------------------------------------------------------------------- 1 | ;;; ya-org-capture.el --- Integrate YASnippet and Yankpad in your Org Capture workflow. 2 | 3 | ;; Copyright (C) 2020 Andrea Giugliano 4 | 5 | ;; Author: Andrea Giugliano 6 | ;; Version: 0.1.1 7 | ;; Package-Version: 20200802.000 8 | ;; Keywords: org-mode org-capture yasnippet yankpad 9 | 10 | ;; This program 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 | 15 | ;; This program is distributed in the hope that it will be useful, 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | ;; GNU General Public License for more details. 19 | 20 | ;; You should have received a copy of the GNU General Public License 21 | ;; along with this program. If not, see . 22 | 23 | ;;; Commentary: 24 | 25 | ;; Integrate YASnippet and Yankpad in your Org Capture workflow. 26 | ;; 27 | ;; You may want to capture templates for your org-mode notes using the 28 | ;; features of the yasnippet and yankpad marvellous template engines. 29 | ;; If you load this mode you can do this simply by adding the 30 | ;; following lines to your init: 31 | ;; 32 | ;; 33 | ;; And defining your org-capture template like this: 34 | ;; 35 | ;; 36 | ;; See documentation on https://github.com/ag91/easy-org-ensime.el 37 | 38 | ;;; Code: 39 | 40 | (require 'cl-lib) 41 | 42 | (defgroup ya-org-capture nil 43 | "Options specific to ya-org-capture." 44 | :tag "ya-org-capture" 45 | :group 'ya-org-capture) 46 | 47 | (defcustom ya-org-capture/ya-prefix "YA-ORG-CAPTURE-PREFIX-- " 48 | "Prefix used to tag." 49 | :group 'ya-org-capture 50 | :type 'string) 51 | 52 | (defcustom ya-org-capture/expand-snippet-functions (list 'yankpad-expand 'yas-expand) 53 | "Functions used to expand the snippet at point. Order is 54 | important: if the functions are able to expand a snippet with 55 | the same key, the first function of the list takes precedence 56 | over the second." 57 | :group 'ya-org-capture 58 | :type 'list) 59 | 60 | (defun ya-org-capture/or-else (&rest fs) 61 | "Compose partial functions FS until one of them produces a result or there are no more FS available." 62 | `(lambda (i) 63 | (cl-reduce 64 | (lambda (acc f) (or acc (when (fboundp f) (funcall f i)))) 65 | ',fs 66 | :initial-value nil))) 67 | 68 | (defun ya-org-capture/org-capture-fill-template () 69 | "Post-process `org-mode' snippet to expand `org-capture' syntax. This only works for YASnippet." 70 | (let ((template (buffer-substring-no-properties yas-snippet-beg yas-snippet-end)) 71 | (front-text (buffer-substring-no-properties (point-min) yas-snippet-beg)) 72 | (back-text (buffer-substring-no-properties yas-snippet-end (point-max))) 73 | (ya-org-capture--temp-buffer (get-buffer-create "ya-org-capture-temp"))) 74 | (with-current-buffer ya-org-capture--temp-buffer 75 | (insert front-text 76 | (org-capture-fill-template template) 77 | back-text)) 78 | (replace-buffer-contents ya-org-capture--temp-buffer) 79 | (kill-buffer ya-org-capture--temp-buffer))) 80 | 81 | (defun ya-org-capture/snippet-expand () 82 | "Try to expand snippet at point with `yankdpad-expand' and then with `yas-expand'." 83 | (interactive) 84 | (funcall (apply 'ya-org-capture/or-else ya-org-capture/expand-snippet-functions) nil)) 85 | 86 | (defun ya-org-capture/support-org-syntax-for-yasnippets () 87 | "Allow `org-capture' to expand its syntax for YASnippets." 88 | (add-hook 'yas-after-exit-snippet-hook 'ya-org-capture/org-capture-fill-template nil t)) 89 | 90 | (defun ya-org-capture/expand-snippets () 91 | "Expand `ya-org-capture/ya-prefix'." 92 | (when (search-forward ya-org-capture/ya-prefix nil t) 93 | (replace-match "") 94 | (ya-org-capture/support-org-syntax-for-yasnippets) 95 | (end-of-line) 96 | (ya-org-capture/snippet-expand))) 97 | 98 | ;;;###autoload 99 | (defun ya-org-capture/make-snippet (snippet-name &optional yp-category) 100 | "Concatenate prefix to SNIPPET-NAME for substitution in `org-capture' flow. 101 | Optionally set `yankpad-category' to YP-CATEGORY." 102 | (if yp-category (yankpad-set-local-category yp-category)) 103 | (cl-concatenate 'string ya-org-capture/ya-prefix snippet-name "\n")) 104 | 105 | (defun ya-org-capture/setup () 106 | "Setup integration between org-capture and yasnippet/yankpad." 107 | (interactive) 108 | (add-hook 'org-capture-mode-hook 'ya-org-capture/expand-snippets)) 109 | 110 | 111 | (defun ya-org-capture/teardown () 112 | "Teardown integration between org-capture and yasnippet/yankpad." 113 | (interactive) 114 | (remove-hook 'org-capture-mode-hook 'ya-org-capture/expand-snippets)) 115 | 116 | (provide 'ya-org-capture) 117 | ;;; ya-org-capture ends here 118 | 119 | ;; Local Variables: 120 | ;; time-stamp-pattern: "10/Version:\\\\?[ \t]+1.%02y%02m%02d\\\\?\n" 121 | ;; End: 122 | --------------------------------------------------------------------------------