org-src: New syntax for source blocks faces

* doc/org.texi (Editing source code): Document changes.
* lisp/org-faces.el (org-block): Update docstring.
  (org-src-block-faces): New defcustom for source-block faces.
* lisp/org-src.el (org-src-font-lock-fontify-block): Use
  org-src-block-faces.
* etc/ORG-NEWS: Update NEWS entry.
This commit is contained in:
Rasmus 2016-09-21 11:02:49 +02:00
parent 582233dc08
commit e03e837168
4 changed files with 38 additions and 17 deletions

View File

@ -14900,21 +14900,20 @@ By default, Org will ask before returning to an open edit buffer. Set this
variable to @code{nil} to switch without asking. variable to @code{nil} to switch without asking.
@end table @end table
To turn on native code fontification in the @emph{Org} buffer, configure the To turn on native code fontification in the @emph{Org} buffer, set the
variable @code{org-src-fontify-natively}. You can also change the appearance variable @code{org-src-fontify-natively} to non-@code{nil}. You can also
of source blocks by customizing the @code{org-block} face or for specific change the appearance of source blocks in general by customizing the
languages, by defining @code{org-block-LANGUAGE} faces. The following @code{org-block} face or @code{org-src-block-faces} for specific languages.
example shades the background of ``ordinary'' blocks while allowing Emacs The following example shades the background of ``ordinary'' blocks while
Lisp source blocks to have a special color. allowing Emacs Lisp and Python source blocks to have a separate colors.
@lisp @lisp
(require 'color) (require 'color)
(set-face-attribute 'org-block nil :background (set-face-attribute 'org-block nil :background
(color-darken-name (color-darken-name
(face-attribute 'default :background) 3)) (face-attribute 'default :background) 3))
(defface org-block-emacs-lisp (setq org-src-block-faces '(("emacs-lisp" (:background "#EEE2FF"))
'((t (:background "#EEE2FF"))) ("python" (:background "#E5FFB8"))))
"Face for Emacs Lisp src blocks")
@end lisp @end lisp
@node Exporting code blocks @node Exporting code blocks

View File

@ -374,7 +374,8 @@ Repeated footnotes are now numbered by referring to a label in the
first footnote. first footnote.
*** The ~org-block~ face is inherited by ~src-blocks~ *** The ~org-block~ face is inherited by ~src-blocks~
This works also when =org-src-fontify-natively= is non-nil. It is also This works also when =org-src-fontify-natively= is non-nil. It is also
possible to specify per-languages faces. See the manual for details. possible to specify per-languages faces. See =org-src-block-faces= and
the manual for details.
*** Links are now customizable *** Links are now customizable
Links can now have custom colors, tooltips, keymaps, display behavior, Links can now have custom colors, tooltips, keymaps, display behavior,
etc... Links are now centralized in ~org-link-parameters~. etc... Links are now centralized in ~org-link-parameters~.

View File

@ -395,11 +395,32 @@ follows a #+DATE:, #+AUTHOR: or #+EMAIL: keyword."
(defface org-block '((t :inherit shadow)) (defface org-block '((t :inherit shadow))
"Face text in #+begin ... #+end blocks. "Face text in #+begin ... #+end blocks.
In addition to this face that take precedence for particular For source-blocks `org-src-block-faces' takes precedence.
languages. For instance, to change the block face for Emacs-Lisp See also `org-fontify-quote-and-verse-blocks'."
blocks define a `org-block-emacs-lisp' face."
:group 'org-faces :group 'org-faces
:version "22.1") :version "25.2")
(defcustom org-src-block-faces nil
"Alist of faces to be used for source-block.
Each element is a cell of the format
(\"language\" FACE)
Where FACE is either a defined face or an anonymous face.
For instance, the following value would color the background of
emacs-lisp source blocks and python source blocks in purple and
green, respectability.
'((\"emacs-lisp\" (:background \"#EEE2FF\"))
(\"python\" (:background \"#e5ffb8\")))"
:group 'org-faces
:type '(repeat (list (string :tag "language")
(choice
(face :tag "Face")
(sexp :tag "Anonymous face"))))
:version "25.2"
:package-version '(Org . "9.0"))
(defface org-block-begin-line '((t (:inherit org-meta-line))) (defface org-block-begin-line '((t (:inherit org-meta-line)))
"Face used for the line delimiting the begin of source blocks." "Face used for the line delimiting the begin of source blocks."

View File

@ -513,9 +513,9 @@ as `org-src-fontify-natively' is non-nil."
org-buffer))) org-buffer)))
(setq pos next)))) (setq pos next))))
;; Add Org faces. ;; Add Org faces.
(let ((face-name (intern (format "org-block-%s" lang)))) (let ((src-face (cadr (assoc-ignore-case lang org-src-block-faces))))
(when (facep face-name) (when (or (facep src-face) (listp))
(font-lock-append-text-property start end 'face face-name)) (font-lock-append-text-property start end 'face src-face))
(font-lock-append-text-property start end 'face 'org-block)) (font-lock-append-text-property start end 'face 'org-block))
(add-text-properties (add-text-properties
start end start end