7

I have a few symbols in prettify-symbols-alist that improve the appearance of elisp code in my configuration file. I am migrating my configuration file from Elisp to Org-mode and I would like to keep the prettified symbols. Is this possible to achieve in Org mode 8.3?

4

If you want the symbols to be prettified in the whole org file, just define prettify-symbols-alist in the buffer and enable prettify-symbols-mode.

But a better solution would ensure that these symbols are prettified only in the src blocks (and according to the language mode). Note that they are when editing the source block through org-edit-src-code (since the src block is copied in a buffer in the corresponding major mode).

Looking at how src block fontification works (function org-src-font-lock-fontify-block in file org-src.el:

  1. extract block as a string
  2. insert it in a dedicated buffer
  3. set language major mode
  4. call font-lock-fontify-buffer
  5. copy 'face properties from buffer to org buffer
  6. mark text in org buffer as font-lock-fontified

And seeing (function enter prettify-symbols-mode in file prog-mode.el) that symbol prettification relies on 'composition properties, one can deduce we just need to change org-src-font-lock-fontify-block to make it copy 'composition properties as well.

Here is the modified function (see marked 'Addition' part):

(defun org-src-font-lock-fontify-block (lang start end)
  "Fontify code block.
This function is called by emacs automatic fontification, as long
as `org-src-fontify-natively' is non-nil."
  (let ((lang-mode (org-src--get-lang-mode lang)))
    (when (fboundp lang-mode)
      (let ((string (buffer-substring-no-properties start end))
        (modified (buffer-modified-p))
        (org-buffer (current-buffer)) pos next)
    (remove-text-properties start end '(face nil))
    (with-current-buffer
        (get-buffer-create
         (concat " org-src-fontification:" (symbol-name lang-mode)))
      (delete-region (point-min) (point-max))
      (insert string " ") ;; so there's a final property change
      (unless (eq major-mode lang-mode) (funcall lang-mode))
      ;; Avoid `font-lock-ensure', which does not display fonts in
      ;; source block.
      (font-lock-fontify-buffer)
      (setq pos (point-min))
      (while (setq next (next-single-property-change pos 'face))
        (put-text-property
         (+ start (1- pos)) (1- (+ start next)) 'face
         (get-text-property pos 'face) org-buffer)
        (setq pos next))
      ;; Addition: also copy 'composition info for prettified symbols
      (setq pos (point-min))
      (while (setq next (next-single-property-change pos 'composition))
        (put-text-property
         (+ start (1- pos)) (1- (+ start next)) 'composition
         (get-text-property pos 'composition) org-buffer)
        (setq pos next))
      ;; End addition
      )
    (add-text-properties
     start end
     '(font-lock-fontified t fontified t font-lock-multiline t))
    (set-buffer-modified-p modified)))))

You have to ensure this is loaded after the definition in org-src.el.

1

Org has native pretty symbol mode that you can enable with C-c C-x C-\.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.