C-c C-c has the key-binding org-ctrl-c-ctrl-c
which runs org-babel-exec-src-block
if point
is inside a source block.
C-x C-e runs eval-last-sexp
which has nothing to do with Orgmode.
org-babel-execute-src-block
uses org-babel-insert-result
for inserting the result into the Org buffer. There, org-no-properties
removes the text properties from the result string.
I assume that they remove text properties because many of the properties would immediately be overwritten by font-lock
fontification anyway.
The following advice of org-babel-insert-result
defines a new :result
flag props
(or properties
).
Source blocks where the :results
header argument contains that flag keep the text properties on the result string.
(require 'find-func)
(with-current-buffer
(find-file-noselect (find-library-name "ob-core.el"))
;; This is a hack.
;; `org-no-properties' is defined by `defsubst' and the byte-compiler replaces the symbol with the lambda.
;; We need the definition of `org-babel-insert-result' containing the symbol `org-no-properties'.
;; Therefore, we eval the defun for `org-babel-insert-result' in the source file.
(goto-char (point-min))
(re-search-forward "(defun org-babel-insert-result")
(eval-defun nil))
(defun org+-babel-insert-result-with-props (fun result result-params &rest args)
"Avoid removing text properties in `org-babel-insert-result'.
Add the new result type \"raw-props\" to Org source blocks.
With this result type text properties are not removed from the result.
This is an :around advice for `org-babel-insert-result' as FUN.
RESULT, RESULT-PARAMS, and ARGS are passed to FUN."
(if (or (member "props" result-params)
(member "properties" result-params))
(cl-letf* (((symbol-function 'org-no-properties)
(lambda (str &rest _args) str)))
(apply fun result
(delete "properties" (remove "props" result-params))
args))
(apply fun result result-params args)))
(advice-add 'org-babel-insert-result :around #'org+-babel-insert-result-with-props)
But that only works reliably with :result
type raw
.
For an example the result string of the following source block is colored red:
#+BEGIN_SRC emacs-lisp :results raw drawer props
(propertize "string" 'font-lock-face 'hi-red-b)
#+END_SRC