ABu
ABu

Reputation: 12249

Emacs: syntax highlight for non-code files

Let's us suppose I want to create a file (using emacs) to explain something about programming. For example, a mylib-tutorial.txt.

Is there a way to turn on syntax highlight on specific parts of a file containing code?

For example:

Tutorial
---------
This call behaves as follow:

  void foo(&resource);

This call will provoke a huge stack overflow and all of your 
personal files will be permanent lost (a copy to the police
will be sent, though).

Is there is a way to turn on syntax highlight for the code sample? Org-mode perhaps?

Upvotes: 3

Views: 472

Answers (2)

Adobe
Adobe

Reputation: 13467

You can do even better than just fontifying the region: using nxhtml-mode you can enable appropriate mode in the code region.

Here's a basic nxhtml setup:

;; nxhtml-mode:
(load "~/.emacs.d/site-lisp/nxhtml/autostart.el")
;; Mumamo is making emacs 23.3 freak out:
(when (and (equal emacs-major-version 23)
           (equal emacs-minor-version 3))
  (eval-after-load "bytecomp"
    '(add-to-list 'byte-compile-not-obsolete-vars
                  'font-lock-beginning-of-syntax-function))
  ;; tramp-compat.el clobbers this variable!
  (eval-after-load "tramp-compat"
    '(add-to-list 'byte-compile-not-obsolete-vars
                  'font-lock-beginning-of-syntax-function)))
(require 'mumamo)
(require 'mumamo-fun)

For example let's add a css code rule for rst-mode. In reStructuredText css code it demarked with

.. code-block:: css

p { text-align: right; }

Now let's write rules which specify that this should be in css-mode. For this open nxhtml/util/mumamo-fun.el and add there:

(defun rst-bk-mumamo-css-regexp-chunk-start (pos max)
  (let ((where (mumamo-chunk-start-fw-re pos max "\\.\\. code-block:: css\\(.\\|\n\\)*?\n\n")))
    (when where
      (list where 'css-mode))))

(defun rst-bk-mumamo-css-regexp-chunk-end (pos max)
  (save-match-data
    (mumamo-chunk-end-fw-re pos max "\\(^[[:blank:]]+$\\|\n\\)+[^[:blank:]\n]")))

(defun rst-bk-mumamo-css-quick-regexp-chunk (pos
                                  min
                                  max)
  (save-match-data
    (mumamo-possible-chunk-forward pos max 'rst-bk-mumamo-css-regexp-chunk-start
                                           'rst-bk-mumamo-css-regexp-chunk-end)))

(defun rst-bk-mumamo-css-directive (pos min max)
  "Find css chunks. Return range and 'css-mode.
   See `mumamo-find-possible-chunk' for POS, MIN and MAX."
  (rst-bk-mumamo-css-quick-regexp-chunk pos min max))

Now let's regiester that as a new rst-mode:

;;;###autoload
(define-mumamo-multi-major-mode rst-bk-mumamo-mode
  "Turn on multiple major modes for RestructuredText."
  ("ReST" rst-mode (
                    rst-bk-mumamo-css-directive
                    )))

and associate rst files with this new rst mode:

(add-to-list 'auto-mode-alist '("\\.rst\\'" . rst-bk-mumamo-mode))

Now you have you have a css-mode inside a css code-block in rst file:

enter image description here

enter image description here

Upvotes: 0

Chris
Chris

Reputation: 136880

Enable org-src-fontify-natively:

(eval-after-load "org"
  '(setq org-src-fontify-natively t))

And then use org-mode:

* Tutorial

This call behaves as follows:

#+BEGIN_SRC c
  void foo(&resource);
#+END_SRC

This call will provoke a huge stack overflow and all of your 
personal files will be permanent lost (a copy to the police
will be sent, though).

You can also edit the SRC block in c-mode using org-edit-special, bound to C-c ' by default. Use C-c ' again to close the c-mode buffer and update the Org buffer.

Upvotes: 4

Related Questions