org capture auto refile

一直一来用 org capture 做未读网页收集,得空加了一个自动 refile,另外结合了 @zmonster 博客中选中网页内容追加到同一个 headline 的函数。

相关代码:

(setq org-capture-web-link-key "ll"
      org-capture-auto-refile-rules
      `(("https?://arxiv\\.org" ,org-inbox-file "arXiv")))

(defun org-capture-goto-link ()
  (let ((file (nth 1 (org-capture-get :target)))
        (headline (plist-get org-store-link-plist :description))
        (link (plist-get org-store-link-plist :link)))
    (org-capture-put :target (list 'file+headline file headline))
    (widen)
    (goto-char (point-min))
    (let (case-fold-search)
      (if (re-search-forward
           (format org-complex-heading-regexp-format
                   (regexp-quote headline)) nil t)
          (org-end-of-subtree)
        (org-capture-put :flag t)
        (goto-char (point-max))
        (or (bolp) (insert "\n"))
        (insert "* " headline "\n")
        (insert "[[" link "]]\n")))))

(defun org-refile-to (file headline)
  "`org-refile' to exact HEADLINE in FILE."
  (if-let ((pos (save-excursion
                  (org-find-exact-headline-in-buffer
                   headline (or (find-buffer-visiting file)
                                (find-file-noselect file)) t))))
      (org-refile nil nil (list headline file nil pos))
    (user-error (format "There is not headline \"%s\" in %s." headline file))))

(defun org-capture-auto-refile ()
  (when (and (string= (org-capture-get :key) org-capture-web-link-key)
             (org-capture-get :flag))
    (catch 'break
      (dolist (rule org-capture-auto-refile-rules)
        (let ((regexp   (nth 0 rule))
              (file     (nth 1 rule))
              (headline (nth 2 rule))
              (link     (plist-get org-store-link-plist :link)))
          (when (string-match-p regexp link)
            (let ((base (or (buffer-base-buffer) (current-buffer)))
                  (pos (make-marker)))
              (set-marker pos (save-excursion (org-back-to-heading t) (point)) base)
              (save-window-excursion
                (with-current-buffer base
                  (org-with-point-at pos
                    (org-refile-to file headline)))))
            (throw 'break t)))))))

(add-hook 'org-capture-before-finalize-hook #'org-capture-auto-refile)

(when (memq window-system '(mac ns))
  (defun org-capture-after-finalize ()
    (when (string= (org-capture-get :key) org-capture-web-link-key)
      (run-at-time 0.25 nil #'macos-switch-back-to-previous-application)))

  (add-hook 'org-capture-after-finalize-hook #'org-capture-after-finalize))

org-capture-template:

("ll" "Web link" plain (file+function org-inbox-file org-capture-goto-link)
 "%i\n" :empty-lines 1 :immediate-finish t)

调用 org-protocol 的浏览器书签:

javascript:location.href='org-protocol://capture?template=ll&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)+'&body='+encodeURIComponent(function()%7Bvar%20html%20=%20%22%22;var%20sel%20=%20window.getSelection();if%20(sel.rangeCount)%20%7Bvar%20container%20=%20document.createElement(%22div%22);for%20(var%20i%20=%200,%20len%20=%20sel.rangeCount;%20i%20%3C%20len;%20++i)%20%7Bcontainer.appendChild(sel.getRangeAt(i).cloneContents());%7Dhtml%20=%20container.innerHTML;%7Dvar%20dataDom%20=%20document.createElement('div');dataDom.innerHTML%20=%20html;dataDom.querySelectorAll('a').forEach(function(item,%20idx)%20%7Bconsole.log('find%20a%20link');var%20url%20=%20new%20URL(item.href,%20window.location.href).href;var%20content%20=%20item.innerText;item.innerText%20=%20'%5B%5B'+url+'%5D%5B'+content+'%5D%5D';%7D);%5B'p',%20'h1',%20'h2',%20'h3',%20'h4'%5D.forEach(function(tag,%20idx)%7BdataDom.querySelectorAll(tag).forEach(function(item,%20index)%20%7Bvar%20content%20=%20item.innerHTML.trim();if%20(content.length%20%3E%200)%20%7Bitem.innerHTML%20=%20content%20+%20'&%2313;&%2310;';%7D%7D);%7D);return%20dataDom.innerText.trim();%7D())

在论坛里一直都是伸手党,第一次分享自己写的小东西,求轻拍~

3 个赞