1 .. -*- coding: utf-8 -*-
3 ========================================
4 Emacs Support for reStructuredText
5 ========================================
7 :Author: Martin Blais <blais@furius.ca>
11 High-level description of the existing emacs support for editing
12 reStructuredText text documents. Suggested setup code and usage
13 instructions are provided.
20 3.1 Adjusting a Section Title
21 3.2 Promoting and Demoting Many Sections
22 3.3 Redoing All the Decorations to Your Taste
23 3.4 Customizations for Decorations
24 3.5 Viewing the Hierarchy of Section Decorations
25 4 Section Movement and Selection
26 5 Operating on Blocks of Text
27 5.1 Shifting Text Horizontally Intelligently
28 5.2 Bulleting and Enumerating Lists
29 5.2.1 Straightening Existing Bullet List Hierarchies
30 5.3 Creating and Removing Line Blocks
31 5.4 Commenting a Region of Text
32 6 Converting Documents from Emacs
33 7 Table-of-Contents Features
34 7.1 Inserting a Table of Contents
35 7.2 Maintaining the Table of Contents Up-to-date
36 8 Syntax Highlighting via Font-Lock
37 8.1 Face Customization
39 9 Other Useful Settings
40 9.1 ``text-mode`` Settings
41 9.2 Editing Tables: Emacs table mode
42 9.3 Character Processing
51 reStructuredText_ is a series of conventions that allows a
52 toolset--docutils--to extract generic document structure from simple
53 text files. For people who use Emacs_, there is a package that adds a
54 major mode that supports editing in the conventions of
55 reStructuredText_: ``rst.el``. This document describes the features it
56 provides, and how to setup your emacs to use them and how to invoke
59 ``rst.el`` can be found in the ``tools/editors/emacs/`` directory of
60 the snapshot or repository. `A copy can also be downloaded here.
61 <http://docutils.sourceforge.net/tools/editors/emacs/rst.el>`__
67 The emacs support is implemented as an Emacs major mode (``rst-mode``)
68 provided by the ``rst.el`` emacs package. In order to use
69 ``rst-mode``, you need to put the ``rst.el`` in a directory located in
70 your emacs ``load-path`` (use ``C-h v load-path`` to check), and load
75 To enable ``rst-mode``, simple type ``M-x rst-mode``. Alternatively,
76 you can modify ``auto-mode-alist`` to automatically turn it on
77 whenever you visit reStructuredText_ documents::
80 (append '(("\\.txt$" . rst-mode)
81 ("\\.rst$" . rst-mode)
82 ("\\.rest$" . rst-mode)) auto-mode-alist))
84 If have local variables enabled (see ``enable-local-variables`` in the
85 Emacs manual), you can also add the following at the top of your
86 documents to trigger rst-mode::
90 Or this at the end of your documents::
97 ``rst-mode`` automatically binds several keys for invoking special
98 handy functions for editing ReStructuredText. As is the custom for
99 Emacs major modes, most keys are bound to ``C-c C-LETTER``.
101 If you insert an inline table-of-contents at the top of the document,
102 you may want to add a hook to automatically update it everytime you
103 adjust a section title::
105 (add-hook 'rst-adjust-hook 'rst-toc-update)
107 Additional configuration variables can be customized and can be found
108 by browsing the source code for ``rst.el``.
114 The rst package does not completely parse all the reStructuredText_
115 constructs, but it contains the ability to recognize the section
116 decorations and to build the hierarchy of the document. What we call
117 section decorations or adornments are the underlines or under- and
118 overlines used to mark a section title.
120 Adjusting a Section Title
121 -------------------------
123 There is a function that helps a great deal to maintain these
124 decorations: ``rst-adjust`` (bound to ``C-c C-a``, or ``C-=`` by
125 default). This function is a Swiss army knife that can be invoked
126 repeatedly and whose behaviour depends on context:
128 #. If there is an incomplete underline, e.g.::
133 Invocation will complete the section title. You can simply enter a
134 few characters of the title and invoke the function to complete it.
135 It can also be used to adjust the length of the existing decoration
136 when you need to edit the title.
138 #. If there is no section decoration, a decoration one level under the
139 last encountered section level is added;
141 #. If there is already a section decoration, it is promoted to the
142 next level. You can invoke it like this repeatedly to cycle the
143 title through the hierarchy of existing decorations.
145 Invoking the function with a negative prefix argument, e.g. ``C--
146 C-=``, will effectively reverse the direction of decoration cycling.
147 To alternate between underline-only and over-and-under styles, you can
148 use a regular prefix argument, e.g. ``C-u C-=``. See the
149 documentation of ``rst-adjust`` for more description of the prefix
150 arguments to alter the behaviour of the function.
152 Promoting and Demoting Many Sections
153 ------------------------------------
155 When you are re-organizing the structure of a document, it can be
156 useful to change the level of a number of section titles. The same
157 key binding can be used to do that: if the region is active when the
158 binding is invoked, all the section titles that are within the region
159 are promoted accordingly (or demoted, with negative prefix arg).
161 Redoing All the Decorations to Your Taste
162 -----------------------------------------
164 If you open someone else's file and the decorations it contains are
165 unfamiliar, you may want to readjust them to fit your own preferred
166 hierarchy of decorations. This can be difficult to perform by hand.
167 However, you can do this easily by invoking
168 ``rst-straighten-decorations`` (``C-c C-s``), which operates on the
171 Customizations for Decorations
172 ------------------------------
174 You can set the variable ``rst-preferred-decorations`` to a list of
175 the decorations that you like to use for documents. Everyone has
176 their preference. ``rst-default-indent`` can be set to the number of
177 indent spaces preferred for the over-and-under decoration style.
179 Viewing the Hierarchy of Section Decorations
180 --------------------------------------------
182 You can visualize the hierarchy of the section decorations in the
183 current buffer by invoking ``rst-display-decorations-hierarchy``,
184 bound on ``C-c C-h``. A temporary buffer will appear with fake
185 section titles rendered in the style of the current document. This
186 can be useful when editing other people's documents to find out which
187 section decorations correspond to which levels.
190 Section Movement and Selection
191 ==============================
193 You can move the cursor between the different section titles by using
194 the ``rst-backward-section`` and ``rst-forward-section`` functions, by
195 default bound to the ``C-c C-p`` and ``C-c C-n`` keys.
197 To mark the section that cursor lies in, use ``rst-mark-section``
202 Operating on Blocks of Text
203 ===========================
205 Shifting Text Horizontally Intelligently
206 ----------------------------------------
208 Due to the nature of reStructuredText_, lists are indented by two or
209 three characters, e.g. bulleted lists use two chars::
222 while enumerated lists are indented by 3 or more characters ::
232 To this effect, when shifting text, it can be useful to have functions
233 which understand which indent to use by using the context around the
234 region. Those functions are ``rst-shift-region-right`` and
235 ``rst-shift-region-left``.
237 You can use ``C-c C-r`` and ``C-c C-l`` to shift the active region.
238 These bindings are similar to the ones provided by python-mode for
239 editing python code and behave similarly. They automatically inspect
240 the lines of text before the currently selected region to determine
241 what the appropriate column positions are.
244 Bulleting and Enumerating Lists
245 -------------------------------
247 Sometimes it can be useful to insert bullet list markers enumeration
248 number before a number of lines or paragraphs. You can do this easily
249 by invoking ``rst-enumerate-region`` (``C-c C-e``), for example, the
266 ``rst-listify-region`` (``C-c C-b``) does the same, but only adds
267 bullet list markers, e.g.::
284 By default, each paragraph starting on the leftmost line in the
285 highlighted region will be taken to be a single list or enumeration
286 item, for example, enumerating the following::
289 keeps the doctor away.
292 are tastier than apples.
294 If you preferred bananas
301 keeps the doctor away.
304 are tastier than apples.
306 3. If you preferred bananas
310 If you would like to enumerate each of the lines, use a prefix
311 argument on the preceding commands, e.g.::
323 Straightening Existing Bullet List Hierarchies
324 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
326 If you invoke ``rst-straighten-bullets-region`` (C-c C-w), the
327 existing bullets in the highlighted region will be replaced to reflect
328 their respective level. This does not make a difference in the
329 document structure that reStructuredText_ defines, but looks better in
330 the input file, for example, if all of the top-level bullet items use
331 the character ``-``, and all of the 2nd level items use ``*``, etc.
334 Creating and Removing Line Blocks
335 ---------------------------------
337 To create line blocks, first select the region to convert and invoke
338 ``rst-toggle-line-block`` with ``C-c C-d``, for example, the
351 This works even if the region is indented. To remove line blocks,
352 select a region and invoke with a prefix argument.
355 Commenting a Region of Text
356 ---------------------------
358 If you use the Emacs ``comment-region`` function (bound to ``C-c
359 C-c``), the appropriate comment syntax will be added to the active
374 Converting Documents from Emacs
375 ===============================
377 The major mode provides a number of functions for running documents
378 being edited through the docutils tools.
380 The main generic function is ``rst-compile`` (``C-c 1``). This
381 function basically creates a compilation command with the correct
382 output name for the current buffer and then invokes Emacs' compile
383 function. It also looks for the presence of a ``docutils.conf``
384 configuration file in the parent directories and adds it to the
385 cmdline options. There is also an alternative function in case you
386 often need run your document in a second toolset (``C-c 2``).
388 You can customize the commands being used by setting
389 ``rst-compile-primary-toolset`` and ``rst-compile-secondary-toolset``.
391 Other commands are available for other formats:
393 - ``rst-compile-pseudo-region`` (``C-c 3``): When crafting documents,
394 it is often convenient to view which data structures docutils will
395 parse them into. You can use to run the active region through
396 ``rst2pseudoxml.py`` and have the output automatically be displayed
399 - ``rst-compile-pdf-preview`` (``C-c 4``): Convert the current
400 document to PDF and launch a viewer on the results.
402 - ``rst-compile-slides-preview`` (``C-c 5``): Convert the current
403 document to S5 slides and view in a web browser.
406 Table-of-Contents Features
407 ==========================
409 When you are editing long documents, it can be a bit difficult to
410 orient yourself in the structure of your text. To that effect, a
411 function is provided that quickly parses the document and presents a
412 hierarchically indented table of contents of the document in a
413 temporary buffer, in which you can navigate and press ``Return`` to go
414 to a specific section.
416 Invoke this function (``rst-toc``) with ``C-c C-t``. It should
417 present a temporary buffer that looks something like this::
420 Debugging Meta-Techniques
422 Debugging Solution Patterns
423 Recognize That a Bug Exists
424 Subdivide and Isolate
425 Identify and Verify Assumptions
426 Use a Tool for Introspection
427 Change one thing at a time
428 Learn about the System
430 The Basic Steps in Debugging
436 When you select a section title (press ``RET``), the temporary buffer
437 disappears and you are left with the cursor positioned at the chosen
441 Inserting a Table of Contents
442 -----------------------------
444 Oftentimes in long text documents that are meant to be read directly,
445 a Table of Contents is inserted at the beginning of the text. This is
446 the case for most internet FAQs, for example. In reStructuredText_
447 documents, since the table of contents is automatically generated by
448 the parser with the ``.. contents::`` directive, people generally have
449 not been adding a text table of contents to their source documents,
450 and partly because it is too much trouble to edit and maintain.
452 The emacs support for reStructuredText_ provides a function to insert
453 such a table of contents in your document. Since it is not meant to
454 be part of the document text, you should place such a table of
455 contents within a comment, so that it is ignored by the parser. This
456 is the favoured usage::
461 2 Debugging Solution Patterns
462 2.1 Recognize That a Bug Exists
463 2.2 Subdivide and Isolate
464 2.3 Identify and Verify Assumptions
465 2.4 Use a Tool for Introspection
466 2.5 Change one thing at a time
467 2.6 Learn about the System
468 3 Understanding a bug
469 4 The Basic Steps in Debugging
475 Just place the cursor at the top-left corner where you want to insert
476 the TOC and invoke the function with ``C-c C-i``. The table of
477 contents will display all the section titles that are under the
478 location where the insertion occurs. This way you can insert local
479 table of contents by placing them in the appropriate location.
481 If you have deep nesting of sections, you can use a numeric prefix
482 argument to limit the depth of rendering of the TOC.
484 You can also customize the look of the TOC by setting the values of
485 the following variables:: ``rst-toc-indent``,
486 ``rst-toc-insert-style``, ``rst-toc-insert-max-level``.
490 The table-of-contents inserted by ``rst-mode`` has text properties
491 added to it so that if you type ``C-c C-f`` while the cursor is on
492 one of its entries, the cursor will jump to the corresponding
493 section in the document.
496 Maintaining the Table of Contents Up-to-date
497 --------------------------------------------
499 One issue is that you will probably want to maintain the inserted
500 table of contents up-to-date. There is a function that will
501 automatically look for the inserted TOC (``rst-toc-update``)
502 and it can be added to a hook on the section decoration adjustment
503 function, so that every time you adjust a section title, the TOC is
504 updated. Add this functionality with the following emacs
507 (add-hook 'rst-adjust-hook 'rst-toc-update)
509 You can invoke the update on the current buffer with ``C-c C-u``.
512 Syntax Highlighting via Font-Lock
513 =================================
515 ``rst-mode`` also provides syntax highlighting to reStructuredText_
516 constructs. (This mode was written by Stefan Merten.)
518 Lazy syntax coloring is implemented for many of the constructs that
519 reStructuredText_ prescribes. By default, the font-lock colouring is
520 performed lazily. If you don't like this, you can turn this off by
521 setting the value of ``rst-mode-lazy``. You can also change the
522 various colours (see the source file for the whole list of
525 ``font-lock`` syntax highlighting is enabled by default. If you prefer
526 to turn off syntax highlighting (on some machines it can slow down
527 editing a little bit), you can use the following in your Emacs
530 (setq font-lock-global-modes '(not rst-mode))
536 The ``rst-faces`` group contains all necessary for customizing
537 fonts. The default settings use standard ``font-lock-*-face`` so if
538 you set these to your liking they are probably good in rst-mode also.
540 The group is contained in the faces group as well as in the rst group.
546 The ``rst-faces-defaults`` group contains all necessary for
547 customizing the default fonts used for section title faces.
549 The general idea for section title faces is to have a non-default
550 background but do not change the background. The section level is
551 shown by the lightness of the background color. If you like this
552 general idea of generating faces for section titles but do not like
553 the details this group is the point where you can customize the
554 details. If you do not like the general idea, however, you should
555 customize the faces used in ``rst-adornment-faces-alist``.
557 Note: If you are using a dark background please make sure the variable
558 ``frame-background-mode`` is set to the symbol dark. This triggers
559 some default values which are probably right for you.
561 The group is contained in the ``rst-faces`` group.
563 All customizable features have a comment explaining their
564 meaning. Refer to the customization of your Emacs (try ``M-x
569 Other Useful Settings
570 =====================
572 This section covers general emacs text-mode settings that are useful
573 in the context of reStructuredText_ conventions. These are not
574 provided by ``rst.el`` but you may find them useful specifically for
575 reStructuredText_ documents.
577 ``text-mode`` Settings
578 ----------------------
580 Consult the Emacs manual for more text-mode customizations. In
581 particular, you may be interested in setting the following variables,
582 functions and modes that pertain somewhat to text-mode:
590 Editing Tables: Emacs table mode
591 --------------------------------
593 You may want to check out `Emacs table mode`_ to create an edit
594 tables, it allows creating ascii tables compatible with
597 .. _Emacs table mode: http://table.sourceforge.net/
603 Since reStructuredText punts on the issue of character processing,
604 here are some useful resources for Emacs users in the Unicode world:
606 * `xmlunicode.el and unichars.el from Norman Walsh
607 <http://nwalsh.com/emacs/xmlchars/index.html>`__
609 * `An essay by Tim Bray, with example code
610 <http://www.tbray.org/ongoing/When/200x/2003/09/27/UniEmacs>`__
612 * For Emacs users on Mac OS X, here are some useful useful additions
615 - To get direct keyboard input of non-ASCII characters (like
616 "option-e e" resulting in "é" [eacute]), first enable the option
617 key by setting the command key as your meta key::
619 (setq mac-command-key-is-meta t) ;; nil for option key
621 Next, use one of these lines::
623 (set-keyboard-coding-system 'mac-roman)
624 (setq mac-keyboard-text-encoding kTextEncodingISOLatin1)
626 I prefer the first line, because it enables non-Latin-1 characters
627 as well (em-dash, curly quotes, etc.).
629 - To enable the display of all characters in the Mac-Roman charset,
630 first create a fontset listing the fonts to use for each range of
631 characters using charsets that Emacs understands::
633 (create-fontset-from-fontset-spec
634 "-apple-monaco-medium-r-normal--10-*-*-*-*-*-fontset-monaco,
635 ascii:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman,
636 latin-iso8859-1:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman,
637 mule-unicode-0100-24ff:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman")
639 Latin-1 doesn't cover characters like em-dash and curly quotes, so
640 "mule-unicode-0100-24ff" is needed.
642 Next, use that fontset::
644 (set-frame-font "fontset-monaco")
646 - To enable cooperation between the system clipboard and the Emacs
647 kill ring, add this line::
649 (set-clipboard-coding-system 'mac-roman)
651 Other useful resources are in `Andrew Choi's Emacs 21 for Mac OS X
652 FAQ <http://members.shaw.ca/akochoi-emacs/stories/faq.html>`__.
654 No matter what platform (or editor) you're using, I recommend the
655 ProFont__ programmer's font. It's monospaced, small but readable,
656 similar characters are visually distinctive (like "I1l|", "0O", "ao",
657 and ".,:;"), and free.
659 __ http://www.tobias-jung.de/seekingprofont/
666 - The automatic section adjustment and table of contents features were
667 written by Martin Blais;
668 - Syntax highlighting was implemented by Stefan Merten;
669 - Various other functions were implemented by David Goodger.
674 On 2005-10-30, ``rst.el`` integrated and replaced the contents of the
677 - ``restructuredtext.el``
683 .. _Emacs: http://www.gnu.org/software/emacs/emacs.html
684 .. _reStructuredText: http://docutils.sf.net/rst.html
690 indent-tabs-mode: nil
691 sentence-end-double-space: t