[Org] Neat capture template for code snippets

from http://ul.io/nb/2018/04/30/better-code-snippets-with-org-capture/, inspired
by http://irreal.org/blog/?p=7207.
This commit is contained in:
Daniel - 2018-08-18 16:18:14 +02:00
parent 0e1caca454
commit b6bb05eb3f
Signed by: dbo
GPG Key ID: 4F63DB96D45AA9C6
1 changed files with 31 additions and 0 deletions

View File

@ -734,6 +734,37 @@ _y_: ?y? year _q_: quit _L__l__c_: ?l?
(difference (db/org-timestamp-difference starting ending)))
(format "CLOCK: %s--%s => %s" starting ending difference)))
;; Capture Code Snippets
;; from http://ul.io/nb/2018/04/30/better-code-snippets-with-org-capture/
(defun db/org-capture-code-snippet (filename)
"Format Org mode entry for capturing code at point in file FILENAMe."
(with-current-buffer (find-buffer-visiting filename)
(let ((code-snippet (buffer-substring-no-properties (mark) (- (point) 1)))
(func-name (which-function))
(file-name (buffer-file-name))
(line-number (line-number-at-pos (region-beginning)))
(org-src-mode (let ((mm (intern (replace-regexp-in-string
"-mode" "" (format "%s" major-mode)))))
(or (car (rassoc mm org-src-lang-modes))
(format "%s" mm)))))
(format
"file:%s::%s
In ~%s~:
#+BEGIN_SRC %s
%s
#+END_SRC"
file-name
line-number
func-name
org-src-mode
code-snippet))))
(add-to-list 'org-capture-templates
'("s" "Code Snippet" entry (file db/org-default-refile-file)
"* %?\n%(db/org-capture-code-snippet \"%F\")")
t)
;;; Refiling