summaryrefslogtreecommitdiff
path: root/modules/org-capture-config.el
blob: 4fa7af4186b64ac7f3fa56f86adbf0f29de839bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
;;; org-capture-config.el --- Org Capture Configuration  -*- lexical-binding: t; -*-

;;; Commentary:
;; Customizations related to org-capture and org-refile is here.
;; This includes 'cj/org-webpage-clipper' functionality.

;; To ensure the code below is only loaded after org-mode, all code is wrapped in an
;; eval-after-load function.


;;; Code:


(with-eval-after-load 'org

  ;; ---------------------- Org-Webpage-Clipper ----------------------
  ;; Saves a copy of the page eww is visiting in the 'articles'-file for offline
  ;; reading. In other words, it's a "Poor Man's Pocket/Instapaper"

  (defun cj/org-webpage-clipper ()
    "Capture a web page for later viewing in an org-file.
Encodes all links and marks that may interfere with org mode
display, then inserts the content into a file for later offline use.
This is meant to be used in coordination with an org-capture-template.

Example Template:

,@
(\"w\" \"Website\" plain (function cj/org-webpage-clipper)
\"* %a\\nArticle Link: %L\\nCaptured On: %U\\n\\n\" :immediate-finish t)
'@"
    (interactive)

    ;; Ensure valid major mode before encoding
    (cond
     ((eq major-mode 'w3m-mode)
      (org-w3m-copy-for-org-mode))
     ((eq major-mode 'eww-mode)
      (org-eww-copy-for-org-mode))
     (t
      (error "Not valid -- must be in w3m or eww mode")))

    ;; Check for full path to the archive file. Create missing directories.
    (unless (file-exists-p article-file)
      (let ((dir (file-name-directory article-file)))
        (unless (file-exists-p dir)
          (make-directory dir))))

    ;; Move to end of file and insert blank line for org-capture to add timestamp, etc.
    (find-file article-file)
    (goto-char (point-max))
    (insert "\n\n\n\n\n")

    ;; Insert the web content keeping our place.
    (save-excursion (yank))

    ;; Remove page info from kill ring. Also, fix the yank pointer.
    (setq kill-ring (cdr kill-ring))
    (setq kill-ring-yank-pointer kill-ring)

    ;; Final repositioning.
    (forward-line -1))

;;;; --------------------------------- Functions -------------------------------

  (defun org-capture-pdf-active-region ()
    "Capture the active region of the pdf-view buffer.
Intended to be called within an org capture template."
    (let* ((pdf-buf-name (plist-get org-capture-plist :original-buffer))
           (pdf-buf (get-buffer pdf-buf-name)))
      (if (buffer-live-p pdf-buf)
          (with-current-buffer pdf-buf
            (car (pdf-view-active-region-text)))
        (user-error "Buffer %S not alive" pdf-buf-name))))

;;;; --------------------------- Org-Capture Templates -------------------------

  ;; ORG-CAPTURE TEMPLATES
  (setq org-protocol-default-template-key "L")
  (setq org-capture-templates
        '(("t" "Task" entry (file+headline inbox-file "Inbox")
           "* TODO %?" :prepend t)

          ("e" "Event" entry (file+headline schedule-file "Scheduled Events")
           "* %?\nWHEN: %^t" :prepend t)

          ("E" "Epub Text" entry (file+headline inbox-file "Inbox")
           "* %?
#+BEGIN_QUOTE\n %i\n#+END_QUOTE
Source: [[%:link][%(buffer-name (org-capture-get :original-buffer))]]
Captured On: %U" :prepend t)

          ("P" "PDF Text" entry (file+headline inbox-file "Inbox")
           "* %?
#+BEGIN_QUOTE\n%(org-capture-pdf-active-region)\n#+END_QUOTE
Source:[[%L][%(buffer-name (org-capture-get :original-buffer))]]
Captured On: %U" :prepend t)

          ("p" "Link with Selection" entry (file+headline inbox-file "Inbox")
           "* %?%:description
#+BEGIN_QUOTE\n%i\n#+END_QUOTE
[[%:link][%:description]]
Captured On: %U\n" :prepend t)

          ("L" "Link" entry (file+headline inbox-file "Inbox")
           "* %?%:description
[[%:link][%:description]]\nCaptured On: %U" :prepend t :immediate-finish t)

          ("m" "Mu4e Email" entry (file+headline inbox-file "Inbox")
           "* TODO %?
%(if (string= \"%i\" \"\") \"\" \"\n#+BEGIN_QUOTE\n%i\n#+END_QUOTE\")
[[%:link][%:description]]
Captured On: %U"
           :prepend t)

          ("g" "Grocery List Item" item
           (file+headline inbox-file "Grocery List") "%?")

          ("s" "Shopping List Entry" entry
           (file+headline inbox-file "Shopping List") "* %?")

          ("w" "Web Page Clipper" plain
           (function cj/org-webpage-clipper)
           "* %a\nArticle Link: %L\nCaptured On: %U\n\n" :immediate-finish t)))

  ) ;; end with-eval-after-load 'org

;; ---------------------------- Simple Task Capture ----------------------------
;; the simplest way to capture a task. Also a simple way to write this function.

(define-key global-map (kbd "C-S-t") (kbd "C-c c t"))

(provide 'org-capture-config)
;;; org-capture-config.el ends here.