Skip to content

Instantly share code, notes, and snippets.

@etyurkin
Last active September 2, 2020 23:51
Show Gist options
  • Save etyurkin/e7c860c3ee688c2f9cf45d4f29a68fc1 to your computer and use it in GitHub Desktop.
Save etyurkin/e7c860c3ee688c2f9cf45d4f29a68fc1 to your computer and use it in GitHub Desktop.
C-c C-e to evaluate org source blocks to a separate buffer.
(defun kwarks--org-babel-get-result ()
"Get the result of the current source block."
(let ((location (org-babel-where-is-src-block-result)))
(when location
(save-excursion
(goto-char location)
(when (looking-at (concat org-babel-result-regexp ".*$"))
(buffer-substring
(save-excursion
(skip-chars-backward " \r\t\n")
(line-beginning-position 2))
(progn (forward-line) (org-babel-result-end))))))))
(defun kwarks/org-babel-to-buffer ()
"Open babel code block results in a separate buffer"
(interactive)
(org-babel-execute-src-block)
(let ((result (kwarks--org-babel-get-result))
(result-buffer (get-buffer-create "*Org-Babel Results*")))
(org-babel-remove-result)
(with-current-buffer result-buffer
(erase-buffer)
(insert result)
(goto-char (point-min))
(org-mode))
(display-buffer result-buffer)))
(defun kwarks/org-mode-config ()
"To use with `org-mode-hook'"
(local-set-key (kbd "C-c C-e") 'kwarks/org-babel-to-buffer))
(add-hook 'org-mode-hook 'kwarks/org-mode-config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment