From c2c0d7dab8e34c326a9b071cbc4ad9d54acf5d1a Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Tue, 3 Dec 2019 09:25:31 +0100 Subject: [PATCH] Add code snippet capturing functions --- configuration.org | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/configuration.org b/configuration.org index 34f1696..59f7d11 100644 --- a/configuration.org +++ b/configuration.org @@ -749,6 +749,47 @@ To temporarily hide the mode line. (nth 4 (syntax-ppss))) #+END_SRC +*** Add code references to Org documents +:PROPERTIES: +:SOURCE: http://www.howardism.org/Technical/Emacs/capturing-content.html +:END: + +#+BEGIN_SRC emacs-lisp +(defun ha/org-capture-fileref-snippet (f type headers func-name) + (let* ((code-snippet + (buffer-substring-no-properties (mark) (- (point) 1))) + (file-name (buffer-file-name)) + (file-base (file-name-nondirectory file-name)) + (line-number (line-number-at-pos (region-beginning))) + (initial-txt (if (null func-name) + (format "From [[file:%s::%s][%s]]:" + file-name line-number file-base) + (format "From ~%s~ (in [[file:%s::%s][%s]]):" + func-name file-name line-number + file-base)))) + (format " + %s + + ,#+BEGIN_%s %s +%s + ,#+END_%s" initial-txt type headers code-snippet type))) + +(defun ha/org-capture-clip-snippet (f) + "Given a file, F, this captures the currently selected text +within an Org EXAMPLE block and a backlink to the file." + (with-current-buffer (find-buffer-visiting f) + (ha/org-capture-fileref-snippet f "EXAMPLE" "" nil))) + +(defun ha/org-capture-code-snippet (f) + "Given a file, F, this captures the currently selected text +within an Org SRC block with a language based on the current mode +and a backlink to the function and the file." + (with-current-buffer (find-buffer-visiting f) + (let ((org-src-mode (replace-regexp-in-string "-mode" "" (format "%s" major-mode))) + (func-name (which-function))) + (ha/org-capture-fileref-snippet f "SRC" org-src-mode func-name)))) +#+END_SRC + ** ~python-mode~ related *** Add a docstring to the current thing