~ubuntu-branches/ubuntu/karmic/python-docutils/karmic

« back to all changes in this revision

Viewing changes to docs/user/emacs.txt

  • Committer: Bazaar Package Importer
  • Author(s): martin f. krafft
  • Date: 2006-07-10 11:45:05 UTC
  • mfrom: (2.1.4 edgy)
  • Revision ID: james.westby@ubuntu.com-20060710114505-otkhqcslevewxmz5
Tags: 0.4-3
Added build dependency on python-central (closes: #377580).

Show diffs side-by-side

added added

removed removed

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