90 lines
3.4 KiB
EmacsLisp
90 lines
3.4 KiB
EmacsLisp
;;; sphinx-src.el --- Native code block support.
|
|
|
|
;; Copyright (C) 2016 Matúš Goljer
|
|
|
|
;; Author: Matúš Goljer <matus.goljer@gmail.com>
|
|
;; Maintainer: Matúš Goljer <matus.goljer@gmail.com>
|
|
;; Created: 11th September 2016
|
|
;; Keywords: languages
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
;; modify it under the terms of the GNU General Public License
|
|
;; as published by the Free Software Foundation; either version 3
|
|
;; of the License, or (at your option) any later version.
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
;; GNU General Public License for more details.
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
;;; Commentary:
|
|
|
|
;;; Code:
|
|
|
|
;; This is copy-pasted from org-src.el
|
|
(defcustom sphinx-src-lang-modes
|
|
'(("ocaml" . tuareg) ("elisp" . emacs-lisp) ("ditaa" . artist)
|
|
("asymptote" . asy) ("dot" . fundamental) ("sqlite" . sql)
|
|
("calc" . fundamental) ("C" . c) ("cpp" . c++) ("C++" . c++)
|
|
("screen" . shell-script) ("shell" . sh) ("bash" . sh))
|
|
"Alist mapping languages to their major mode.
|
|
The key is the language name, the value is the string that should
|
|
be inserted as the name of the major mode. For many languages this is
|
|
simple, but for language where this is not the case, this variable
|
|
provides a way to simplify things on the user side.
|
|
For example, there is no ocaml-mode in Emacs, but the mode to use is
|
|
`tuareg-mode'."
|
|
:group 'sphinx
|
|
:type '(repeat
|
|
(cons
|
|
(string "Language name")
|
|
(symbol "Major mode"))))
|
|
|
|
;; This is copy-pasted from org-src.el
|
|
(defun sphinx-src--get-lang-mode (lang)
|
|
"Return major mode that should be used for LANG.
|
|
LANG is a string, and the returned major mode is a symbol."
|
|
(intern
|
|
(concat
|
|
(let ((l (or (cdr (assoc lang sphinx-src-lang-modes)) lang)))
|
|
(if (symbolp l) (symbol-name l) l))
|
|
"-mode")))
|
|
|
|
;; This is copy-pasted from org-src.el
|
|
(defun sphinx-src-font-lock-fontify-block (lang start end)
|
|
"Fontify code block.
|
|
|
|
LANG is the language used in the block.
|
|
|
|
START and END specify the block position."
|
|
(let ((lang-mode (sphinx-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)))
|
|
(add-text-properties
|
|
start end
|
|
'(font-lock-fontified t fontified t font-lock-multiline t))
|
|
(set-buffer-modified-p modified)))))
|
|
|
|
(provide 'sphinx-src)
|
|
;;; sphinx-src.el ends here
|