Add code snippet capturing functions

This commit is contained in:
Gergely Polonkai 2019-12-03 09:25:31 +01:00
parent 9953cb1f49
commit c2c0d7dab8
No known key found for this signature in database
GPG Key ID: 38F402C8471DDE93

View File

@ -749,6 +749,47 @@ To temporarily hide the mode line.
(nth 4 (syntax-ppss))) (nth 4 (syntax-ppss)))
#+END_SRC #+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 ** ~python-mode~ related
*** Add a docstring to the current thing *** Add a docstring to the current thing