Fixed link in emacs file.
[docutils.git] / docutils / docs / user / emacs.txt
blob64daf07acbcb46f810af8aa61239fff78a078b32
1 .. -*- coding: utf-8 -*-
3 ========================================
4    Emacs Support for reStructuredText
5 ========================================
7 :Author: Martin Blais <blais@furius.ca>
8 :Date: $Date$
9 :Abstract:
11     High-level description of the existing emacs support for editing
12     reStructuredText text documents.  Suggested setup code and usage
13     instructions are provided.
15 .. contents::
17     1   Introduction
18     2   Basic Setup
19     3   Section Decoration Adjustment
20       3.1  Promoting and Demoting Many Sections
21       3.2  Customizations
22     4   Viewing the Hierarchy of Section Decorations
23     5   Table of Contents
24       5.1  Inserting a Table of Contents
25       5.2  Maintaining the Table of Contents Up-to-date
26     6   Navigating Between the Section Titles
27     7   Shifting Bulleted List Levels
28     8   Major Mode for Editing reStructuredText Documents
29     9   Converting Documents from Emacs
30     10  Other / External Useful Emacs Settings
31       10.1  Settings for Filling Bulleted Lists
32       10.2  ``text-mode`` Settings
33       10.3  Editing Tables: Emacs table mode
34       10.4  Character Processing
35     11  Credits
36     12  Obsolete Files
37     13  Future Work
40 Introduction
41 ============
43 reStructuredText_ is a series of conventions that allows a toolset--docutils--to
44 extract generic document structure from simple text files.  For people who use
45 Emacs_, there is a package that adds some support for the conventions that
46 reStructuredText_ specifies: ``rst.el``.
48 This document describes the most important features that it provides, how to
49 setup your emacs to use them and how to invoke them.
52 Basic Setup
53 ===========
55 The emacs support is completely provided by the ``rst.el`` emacs package.  In
56 order to use these features, you need to put the file in your emacs load-path,
57 and to load it with::
59   (require 'rst)  ;; or (load "rst")
61 Additional configuration variables can be customized and can be found by
62 browsing the source code for ``rst.el``.
64 Then you will want to bind keys to the most common commands it provides.  A
65 standard text-mode hook function is maintained and provided by the package for
66 this use, set it up like this::
68   (add-hook 'text-mode-hook 'rst-text-mode-bindings)
71 Section Decoration Adjustment
72 =============================
74 The rst package does not completely parse all the reStructuredText_ constructs,
75 but it contains the ability to recognize the section decorations and to build
76 the hierarchy of the document.  What we call section decorations or adornments
77 are the underlines or under- and overlines used to mark a section title.
79 There is a function that helps a great deal to maintain these decorations:
80 ``rst-adjust`` (bound on ``C-=`` by default).  This function is a Swiss army 
81 knife that can be invoked repeatedly and whose behaviour depends on context:
83 #. If there is an incomplete underline, e.g.::
85       My Section Title
86       ^^
88    Invocation will complete the section title.  You can simply enter a few
89    characters of the title and invoke the function to complete it.  It can also
90    be used to adjust the length of the existing decoration when you need to edit
91    the title.
93 #. If there is no section decoration, a decoration one level under the last
94    encountered section level is added;
96 #. If there is already a section decoration, it is promoted to the next level.
97    You can invoke it like this repeatedly to cycle the title through the
98    hierarchy of existing decorations.
100 Invoking the function with a negative prefix argument, e.g. ``C-- C-=``, will
101 effectively reverse the direction of decoration cycling.  To alternate between
102 underline-only and over-and-under styles, you can use a regular prefix argument,
103 e.g. ``C-u C-=``.  See the documentation of ``rst-adjust`` for more description
104 of the prefix arguments to alter the behaviour of the function.
107 Promoting and Demoting Many Sections
108 ------------------------------------
110 When you are re-organizing the structure of a document, it can be useful to
111 change the level of a number of section titles.  The same key binding can be
112 used to do that:  if the region is active when the binding is invoked, all the
113 section titles that are within the region are promoted accordingly (or demoted,
114 with negative prefix arg).
117 Customizations
118 --------------
120 You can set the variable ``rst-preferred-decorations`` to a list of the
121 decorations that you like to use for documents.  Everyone has their preference.
122 ``rst-default-indent`` can be set to the number of indent spaces preferred for
123 the over-and-under decoration style.
126 Viewing the Hierarchy of Section Decorations
127 ============================================
129 You can visualize the hierarchy of the section decorations in the current buffer
130 by invoking ``rst-display-decorations-hierarchy``, bound on ``C-u C-x C-=``.  A
131 temporary buffer will appear with fake section titles rendered in the style of
132 the current document.  This can be useful when editing other people's documents
133 to find out which section decorations correspond to which levels.
136 Table of Contents
137 =================
139 When you are editing long documents, it can be a bit difficult to orient
140 yourself in the structure of your text.  To that effect, a function is provided
141 that quickly parses the document and presents a hierarchically indented table of
142 contents of the document in a temporary buffer, in which you can navigate and
143 press ``Return`` to go to a specific section.
145 Invoke this function (``rst-toc``) with ``C-x C-=``.  It should present a
146 temporary buffer that looks something like this::
148   Table of Contents: 
149   Debugging Meta-Techniques
150     Introduction
151     Debugging Solution Patterns
152       Recognize That a Bug Exists
153       Subdivide and Isolate
154       Identify and Verify Assumptions
155       Use a Tool for Introspection
156       Change one thing at a time
157       Learn about the System
158     Understanding a bug
159     The Basic Steps in Debugging
160     Attitude
161       Bad Feelings
162       Good Feelings
163     References
165 When you select a section title, the temporary buffer disappears and you are
166 left with the cursor positioned at the chosen section.
169 Inserting a Table of Contents
170 -----------------------------
172 Oftentimes in long text documents that are meant to be read directly, a Table of
173 Contents is inserted at the beginning of the text.  This is the case for most
174 internet FAQs, for example.  In reStructuredText_ documents, since the table of
175 contents is automatically generated by the parser with the ``.. contents::``
176 directive, people generally have not been adding a text table of contents to
177 their source documents, and partly because it is too much trouble to edit and
178 maintain.
180 The emacs support for reStructuredText_ provides a function to insert such a
181 table of contents in your document.  Since it is not meant to be part of the
182 document text, you should place such a table of contents within a comment, so
183 that it is ignored by the parser.  This is the favoured usage::
185   .. contents::
186   .. 
187       1  Introduction
188       2  Debugging Solution Patterns
189         2.1  Recognize That a Bug Exists
190         2.2  Subdivide and Isolate
191         2.3  Identify and Verify Assumptions
192         2.4  Use a Tool for Introspection
193         2.5  Change one thing at a time
194         2.6  Learn about the System
195       3  Understanding a bug
196       4  The Basic Steps in Debugging
197       5  Attitude
198         5.1  Bad Feelings
199         5.2  Good Feelings
200       6  References
202 Just place the cursor at the top-left corner where you want to insert the TOC
203 and invoke the function with ``C-x +``.  If there is a single top-level section
204 level (i.e. the document title), by default it is ignored.  If you have deep
205 nesting of sections, you can use a numeric prefix argument to limit the depth of
206 rendering of the TOC.
208 You can also customize the look of the TOC by setting the values of the
209 following variables:: ``rst-toc-indent``,
210 ``rst-toc-insert-always-include-top``, ``rst-toc-insert-style``,
211 ``rst-toc-insert-max-level``.
214 Maintaining the Table of Contents Up-to-date
215 --------------------------------------------
217 One issue is that you will probably want to maintain the inserted table of
218 contents up-to-date.  There is a function that will automatically look for the
219 inserted TOC (``rst-toc-insert-update``) and it can be added to a hook on the
220 section decoration adjustment function, so that every time you adjust a section
221 title, the TOC is updated. Add this functionality with the following emacs
222 configuration::
224   (add-hook 'rst-adjust-hook 'rst-toc-insert-update)
227 Navigating Between the Section Titles
228 =====================================
230 You can move the cursor between the different sections by using the
231 ``rst-backward-section`` and ``rst-forward-section`` functions, by default bound
232 to the ``C-M-{`` and ``C-M-}`` keys.
235 Shifting Bulleted List Levels
236 =============================
238 Due to the nature of reStructuredText_, bulleted lists are always indented by
239 two characters (unless they are part of a blockquote), e.g. ::
241    - Fruits
243      - Bananas
244      - Apples
245      - Oranges
247    - Veggies
249      - Zucchini
250      - Chick Peas
252 To this effect, when re-organizing bulleted lists, it can be useful to shift
253 regions of text by indents of two characters.  You can use the ``C-c C-r`` and
254 ``C-c C-l`` to shift the current region.  These bindings are similar to the ones
255 provided by python-mode for editing python code and behave similarly.
258 Major Mode for Editing reStructuredText Documents
259 =================================================
261 There is a major mode available for editing and syntax highlighting
262 reStructuredText_ constructs.  This mode was written by Stefan Merten [#].  It
263 mostly provides lazy syntax coloring for many of the constructs that
264 reStructuredText_ prescribes.
266 To enable this mode, type ``M-x rst-mode`` or you can set up an
267 ``auto-mode-alist`` to automatically turn it on whenever you visit
268 reStructuredText_ documents::
270    (add-to-list 'auto-mode-alist '("\\.rst$" . rst-mode) )
272 If have local variables enabled (see ``enable-local-variables`` in the Emacs
273 manual), you can also add the following at the top of your documents to trigger
274 rst-mode::
276    .. -*- mode: rst -*-
278 Or add this at the end of your documents::
280    ..
281       Local Variables:
282       mode: rst
283       End:
285 By default, the font-lock colouring is performed lazily.  If you don't like
286 this, you can turn this off by setting the value of ``rst-mode-lazy``.  You can
287 also change the various colours (see the source file for the whole list of
288 customizable faces).
290 .. [#]  This mode used to be provided by the file ``rst-mode.el`` and has now
291         been integrated with the rest of the emacs code.
294 Converting Documents from Emacs
295 ===============================
297 At the moment there is minimal support for calling the conversion tools from
298 within Emacs.  You can add a key binding like this to invoke it::
300   (local-set-key [(control c)(?9)] 'rst-compile)
302 This function basically creates a compilation command with the correct output
303 name for the current buffer and then invokes Emacs' compile function.  It also
304 looks for the presence of a ``docutils.conf`` configuration file in the parent
305 directories and adds it to the cmdline options.  You can customize which tool is
306 used to perform the conversion and some standard options to always be added as
307 well.
309 Invocation uses the toolset indicated by ``rst-compile-primary-toolset``
310 (default is ``'html``).  Invocation with a prefix argument uses
311 ``rst-compile-secondary-toolset``.
313 .. note:: 
315    In general it is preferred to use a Makefile to automate the conversion of
316    many documents or a hierarchy of documents.  The functionality presented
317    above is meant to be convenient for use on single files.
320 Other / External Useful Emacs Settings
321 ======================================
323 This section covers general emacs text-mode settings that are useful in the
324 context of reStructuredText_ conventions.  These are not provided by ``rst.el``
325 but you may find them useful specifically for reStructuredText_ documents.
328 Settings for Filling Bulleted Lists
329 -----------------------------------
331 One problem with the default text-mode settings is that *filling* long lines in
332 bulleted lists that do not have an empty line between them merges them together,
333 e.g.::
335      - Bananas;
336      - One Apple a day keeps the doctor away, and eating more keeps the pirates at bay;
337      - Oranges;
339 Becomes::
341      - Bananas; One Apple a day keeps the doctor away, and
342      - eating more keeps the pirates at bay; Oranges;
344 This is usually not what you want. What you want is this::
346      - Bananas;
347      - One Apple a day keeps the doctor away, and eating more
348        keeps the pirates at bay;
349      - Oranges;
351 The problem is that emacs does not recognize the various consecutive items as
352 forming paragraph boundaries.  You can fix this easily by changing the global
353 value of the parapraph boundary detection to recognize such lists, like this::
355    (setq paragraph-start (concat paragraph-start "\\|[ \t]*[-+*] "))
358 ``text-mode`` Settings
359 ----------------------
361 Consult the Emacs manual for more text-mode customizations.  In particular, you
362 may be interested in setting the following variables, functions and modes that
363 pertain somewhat to text-mode:
365 - indent-tabs-mode
366 - colon-double-space
367 - auto-fill-mode
368 - auto-mode-alist
369 - fill-region
372 Editing Tables: Emacs table mode
373 --------------------------------
375 You may want to check out `Emacs table mode`_ to create an edit tables, it
376 allows creating ascii tables compatible with reStructuredText_.
378 .. _Emacs table mode: http://table.sourceforge.net/
381 Character Processing
382 --------------------
384 Since reStructuredText punts on the issue of character processing,
385 here are some useful resources for Emacs users in the Unicode world:
387 * `xmlunicode.el and unichars.el from Norman Walsh
388   <http://nwalsh.com/emacs/xmlchars/index.html>`__
390 * `An essay by Tim Bray, with example code
391   <http://www.tbray.org/ongoing/When/200x/2003/09/27/UniEmacs>`__
393 * For Emacs users on Mac OS X, here are some useful useful additions
394   to your .emacs file.
396   - To get direct keyboard input of non-ASCII characters (like
397     "option-e e" resulting in "é" [eacute]), first enable the option
398     key by setting the command key as your meta key::
400         (setq mac-command-key-is-meta t) ;; nil for option key
402     Next, use one of these lines::
404         (set-keyboard-coding-system 'mac-roman)
405         (setq mac-keyboard-text-encoding kTextEncodingISOLatin1)
407     I prefer the first line, because it enables non-Latin-1 characters
408     as well (em-dash, curly quotes, etc.).
410   - To enable the display of all characters in the Mac-Roman charset,
411     first create a fontset listing the fonts to use for each range of
412     characters using charsets that Emacs understands::
414       (create-fontset-from-fontset-spec
415        "-apple-monaco-medium-r-normal--10-*-*-*-*-*-fontset-monaco,
416         ascii:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman,
417         latin-iso8859-1:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman,
418         mule-unicode-0100-24ff:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman")
420     Latin-1 doesn't cover characters like em-dash and curly quotes, so
421     "mule-unicode-0100-24ff" is needed.
423     Next, use that fontset::
425         (set-frame-font "fontset-monaco")
427   - To enable cooperation between the system clipboard and the Emacs
428     kill ring, add this line::
430         (set-clipboard-coding-system 'mac-roman)
432   Other useful resources are in `Andrew Choi's Emacs 21 for Mac OS X
433   FAQ <http://members.shaw.ca/akochoi-emacs/stories/faq.html>`__.
435 No matter what platform (or editor) you're using, I recommend the
436 ProFont__ programmer's font.  It's monospaced, small but readable,
437 similar characters are visually distinctive (like "I1l|", "0O", "ao",
438 and ".,:;"), and free.
440 __ http://www.tobias-jung.de/seekingprofont/
443 Credits
444 =======
446 - The automatic section adjustment and table of contents features were written
447   by Martin Blais;
448 - ``rst-mode`` and its syntax highlighting was implemented by Stefan Merten;
449 - Various other functions were implemented by David Goodger.
452 Obsolete Files
453 ==============
455 On 2005-10-30, ``restructuredtext.el``, ``rst-html.el`` and ``rst-mode.el`` were
456 merged to form the new ``rst.el``.  You can consider the old files obsolete and
457 remove them.
460 Future Work
461 ===========
463 Here are some features and ideas that will be worked on in the future, in those
464 frenzied mornings of excitement over the virtues of the one-true-way kitchen
465 sink of editors:
467 - It would be nice to differentiate between text files using reStructuredText_
468   and other general text files.  If we had a function to automatically guess
469   whether a .txt file is following the reStructuredText_ conventions, we could
470   trigger rst-mode without having to hard-code this in every text file, nor
471   forcing the user to add a local mode variable at the top of the file.  
473   We could perform this guessing by searching for a valid decoration at the top
474   of the document or searching for reStructuredText_ directives further on.
476 - We would like to support local table of contents insertion.
478 - The suggested decorations when adjusting should not have to cycle below one
479   below the last section decoration level preceding the cursor.  We need to fix
480   that.
483 .. _Emacs: http://www.gnu.org/software/emacs/emacs.html
484 .. _reStructuredText: http://docutils.sf.net/rst.html
488    Local Variables:
489    mode: indented-text
490    indent-tabs-mode: nil
491    sentence-end-double-space: t
492    fill-column: 70
493    End: