ein-multilang-fontify.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. ;;; ein-multilang-fontify.el --- Syntax highlighting for multiple-languages
  2. ;; Copyright (C) 2012 Takafumi Arakaki
  3. ;; Author: Takafumi Arakaki <aka.tkf at gmail.com>
  4. ;; This file is NOT part of GNU Emacs.
  5. ;; ein-multilang-fontify.el is free software: you can redistribute it
  6. ;; and/or modify it under the terms of the GNU General Public License
  7. ;; as published by the Free Software Foundation, either version 3 of
  8. ;; the License, or (at your option) any later version.
  9. ;; ein-multilang-fontify.el is distributed in the hope that it will be
  10. ;; useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  11. ;; of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with ein-multilang-fontify.el.
  15. ;; If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;;; Code:
  19. ;; It would be nice if org-src is available, but this module should
  20. ;; work without org-src. Data on `org-src-lang-modes' is used
  21. ;; if this variable is bound.
  22. (require 'org-src nil t)
  23. (defun ein:mlf-get-lang-mode (lang)
  24. "Return major mode for LANG.
  25. Modified version of `org-src-get-lang-mode'."
  26. (when (symbolp lang)
  27. (setq lang (symbol-name lang)))
  28. (intern
  29. (format "%s-mode"
  30. (or (and (bound-and-true-p org-src-lang-modes)
  31. (cdr (assoc lang org-src-lang-modes)))
  32. lang))))
  33. (defun ein:mlf-font-lock-fontify-block (lang start end)
  34. "Patched version of `org-src-font-lock-fontify-block'."
  35. (let ((lang-mode (ein:mlf-get-lang-mode lang)))
  36. (if (fboundp lang-mode)
  37. (let ((string (buffer-substring-no-properties start end))
  38. (modified (buffer-modified-p))
  39. (orig-buffer (current-buffer))
  40. pos
  41. next)
  42. (remove-text-properties start end '(face nil))
  43. (with-current-buffer
  44. (get-buffer-create
  45. (concat " ein:mlf-fontification:" (symbol-name lang-mode)))
  46. (delete-region (point-min) (point-max))
  47. (insert (concat string " ")) ;; so there's a final property change
  48. (unless (eq major-mode lang-mode) (funcall lang-mode))
  49. (font-lock-fontify-buffer)
  50. (setq pos (point-min))
  51. (while (setq next (next-single-property-change pos 'face))
  52. (put-text-property
  53. ;; `font-lock-face' property is used instead of `font'.
  54. ;; This is the only difference from org-src.
  55. (+ start (1- pos)) (+ start next) 'font-lock-face
  56. (get-text-property pos 'face) orig-buffer)
  57. (setq pos next)))
  58. (add-text-properties
  59. start end
  60. '(font-lock-fontified t fontified t font-lock-multiline t))
  61. (set-buffer-modified-p modified)))))
  62. (provide 'ein-multilang-fontify)
  63. ;;; ein-multilang-fontify.el ends here