| 1 |
.. -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
======================================== |
| 4 |
Emacs Support for reStructuredText |
| 5 |
======================================== |
| 6 |
|
| 7 |
:Authors: Stefan Merten <[email protected]>, Martin Blais |
| 8 |
<[email protected]> |
| 9 |
:Version: ``rst.el`` V1.4.1 |
| 10 |
:Abstract: |
| 11 |
|
| 12 |
High-level description of the existing Emacs_ support for editing |
| 13 |
reStructuredText_ text documents. Suggested setup code and usage |
| 14 |
instructions are provided. |
| 15 |
|
| 16 |
.. contents:: |
| 17 |
|
| 18 |
Introduction |
| 19 |
============ |
| 20 |
|
| 21 |
reStructuredText_ is a syntax for simple text files that allows a |
| 22 |
tool set - docutils_ - to extract generic document structure. For |
| 23 |
people who use Emacs_, there is a package that adds a major mode that |
| 24 |
supports editing the syntax of reStructuredText_: ``rst.el``. This |
| 25 |
document describes the features it provides, and how to setup your |
| 26 |
Emacs_ to use them and how to invoke them. |
| 27 |
|
| 28 |
Installation |
| 29 |
============ |
| 30 |
|
| 31 |
Emacs_ support for reStructuredText_ is implemented as an Emacs_ major |
| 32 |
mode (``rst-mode``) provided by the ``rst.el`` Emacs_ package. |
| 33 |
|
| 34 |
Emacs_ distributions contain ``rst.el`` since version V23.1. However, |
| 35 |
a significantly updated version of ``rst.el`` is contained in Emacs_ |
| 36 |
V24.3. This document describes the version of ``rst.el`` contained in |
| 37 |
Emacs_ V24.3 and later versions. This version of ``rst.el`` has the |
| 38 |
internal version V1.4.1. |
| 39 |
|
| 40 |
If you have Emacs_ V24.3 or later you do not need to install anything |
| 41 |
to get reST support. If you have an Emacs_ between V23.1 and V24.2 you |
| 42 |
may use the version of ``rst.el`` installed with Emacs_ or install a |
| 43 |
more recent one locally_ (recommended). In other cases you need to |
| 44 |
install ``rst.el`` locally_ to get reST support. |
| 45 |
|
| 46 |
Checking situation |
| 47 |
------------------ |
| 48 |
|
| 49 |
Here are some steps to check your situation: |
| 50 |
|
| 51 |
#. In Emacs_ switch to an empty buffer and try :: |
| 52 |
|
| 53 |
M-x rst-mode |
| 54 |
|
| 55 |
If this works you have ``rst.el`` installed somewhere. You can see |
| 56 |
that it works if you find a string ``ReST`` in Emacs' modeline of |
| 57 |
the current buffer. If this doesn't work you need to install |
| 58 |
``rst.el`` yourself locally_. |
| 59 |
|
| 60 |
#. In the buffer you just switched to ``rst-mode`` try :: |
| 61 |
|
| 62 |
C-h v rst-version |
| 63 |
|
| 64 |
If this fails you have a version of ``rst.el`` older than |
| 65 |
V1.1.0. Either you have an old ``rst.el`` locally or you are using |
| 66 |
an Emacs_ between V23.1 and V24.2. In this case it is recommended |
| 67 |
that you install a more recent version of ``rst.el`` locally_. |
| 68 |
|
| 69 |
You may also try :: |
| 70 |
|
| 71 |
C-h v emacs-version |
| 72 |
|
| 73 |
to find out your Emacs_ version. |
| 74 |
|
| 75 |
#. Check the version of ``rst.el`` |
| 76 |
|
| 77 |
The content of ``rst-version`` gives you the internal version of |
| 78 |
``rst.el``. The version contained in Emacs_ V24.3 and described here |
| 79 |
is V1.4.0. If you have an older version you may or may not install |
| 80 |
a more recent version of ``rst.el`` locally_. |
| 81 |
|
| 82 |
.. _locally: |
| 83 |
|
| 84 |
Local installation |
| 85 |
------------------ |
| 86 |
|
| 87 |
If you decided to install locally please follow these steps. |
| 88 |
|
| 89 |
#. Download ``rst.el`` |
| 90 |
|
| 91 |
Download the most recent published version of ``rst.el`` from |
| 92 |
https://sourceforge.net/p/docutils/code/HEAD/tree/trunk/docutils/tools/editors/emacs/rst.el |
| 93 |
|
| 94 |
#. Put ``rst.el`` to a directory in ``load-path`` |
| 95 |
|
| 96 |
Use :: |
| 97 |
|
| 98 |
C-h v load-path |
| 99 |
|
| 100 |
If in the resulting list you find a directory in your home |
| 101 |
directory put ``rst.el`` in this directory. |
| 102 |
|
| 103 |
Make sure the directory is one of the first entries in |
| 104 |
``load-path``. Otherwise a version of ``rst.el`` which came with |
| 105 |
Emacs_ may be found before your local version. |
| 106 |
|
| 107 |
In Emacs_ see the info node ``Init File Examples`` for more |
| 108 |
information on how to set up your Emacs_ initialization |
| 109 |
machinery. Try :: |
| 110 |
|
| 111 |
C-h i |
| 112 |
mEmacs<Return> |
| 113 |
sInit File Examples<Return> |
| 114 |
|
| 115 |
#. Enable ``rst-mode`` |
| 116 |
|
| 117 |
Add the following to your Emacs_ initialization setup :: |
| 118 |
|
| 119 |
(require 'rst) |
| 120 |
|
| 121 |
After you restarted Emacs_ ``rst.el`` is loaded and ready to be |
| 122 |
used. |
| 123 |
|
| 124 |
Switching ``rst-mode`` on |
| 125 |
------------------------- |
| 126 |
|
| 127 |
By default ``rst-mode`` is switched on for files ending in ``.rst`` or |
| 128 |
``.rest``. If in a buffer you want to switch ``rst-mode`` on manually |
| 129 |
use :: |
| 130 |
|
| 131 |
M-x rst-mode |
| 132 |
|
| 133 |
If you want to use ``rst-mode`` in files with other extensions modify |
| 134 |
``auto-mode-alist`` to automatically turn it on whenever you visit |
| 135 |
reStructuredText_ documents:: |
| 136 |
|
| 137 |
(setq auto-mode-alist |
| 138 |
(append '(("\\.txt\\'" . rst-mode) |
| 139 |
("\\.rst\\'" . rst-mode) |
| 140 |
("\\.rest\\'" . rst-mode)) auto-mode-alist)) |
| 141 |
|
| 142 |
Put the extensions you want in the correct place in the example |
| 143 |
above. Add more lines if needed. |
| 144 |
|
| 145 |
If have local variables enabled (try ``C-h v enable-local-variables`` |
| 146 |
to find out), you can also add the following at the top of your |
| 147 |
documents to trigger rst-mode:: |
| 148 |
|
| 149 |
.. -*- mode: rst -*- |
| 150 |
|
| 151 |
Or this at the end of your documents:: |
| 152 |
|
| 153 |
.. |
| 154 |
Local Variables: |
| 155 |
mode: rst |
| 156 |
End: |
| 157 |
|
| 158 |
Key bindings |
| 159 |
============ |
| 160 |
|
| 161 |
``rst-mode`` automatically binds several keys for invoking special |
| 162 |
functions for editing reStructuredText_. Since ``rst-mode`` contains a |
| 163 |
lot of functionality most key bindings consist of three |
| 164 |
keystrokes. |
| 165 |
|
| 166 |
Following the Emacs_ conventions for major modes the key bindings of |
| 167 |
``rst-mode`` start with ``C-c C-<letter>``. The second key stroke |
| 168 |
selects a group of key bindings: |
| 169 |
|
| 170 |
C-c C-a |
| 171 |
Commands to adjust the section headers and work with the hierarchy |
| 172 |
they build. |
| 173 |
|
| 174 |
C-c C-c |
| 175 |
Commands to compile the current reStructuredText_ document to |
| 176 |
various output formats. |
| 177 |
|
| 178 |
C-c C-l |
| 179 |
Commands to work with lists of various kinds. |
| 180 |
|
| 181 |
C-c C-r |
| 182 |
Commands to manipulate the current region. |
| 183 |
|
| 184 |
C-c C-t |
| 185 |
Commands to create and manipulate a table of contents. |
| 186 |
|
| 187 |
At any stage of typing you may use ``C-h`` to get help on the |
| 188 |
available key bindings. I.e. ``C-c C-h`` gives you help on all key |
| 189 |
bindings while ``C-c C-r C-h`` gives you help on the commands for |
| 190 |
regions. This is handy if you forgot a certain key binding. |
| 191 |
|
| 192 |
Additional key bindings which have a certain meaning in other Emacs_ |
| 193 |
modes are reused in ``rst-mode`` so you don't have to learn a |
| 194 |
different set of key bindings for editing reStructuredText_. |
| 195 |
|
| 196 |
In ``rst-mode`` try :: |
| 197 |
|
| 198 |
C-h m |
| 199 |
|
| 200 |
to list all mode specific key bindings. Most of the key bindings are |
| 201 |
described in this tutorial. |
| 202 |
|
| 203 |
.. note:: The key bindings have been completely revamped in ``rst.el`` |
| 204 |
V1.0.0. This was necessary to make room for new |
| 205 |
functionality. Some of the old bindings still work but give |
| 206 |
a warning to use the new binding. In the output of ``C-h m`` |
| 207 |
these bindings show up as ``rst-deprecated-...``. The old |
| 208 |
bindings will be removed completely in a later version. |
| 209 |
|
| 210 |
Section Adornments |
| 211 |
================== |
| 212 |
|
| 213 |
``rst-mode`` recognizes the section adornments building the section |
| 214 |
hierarchy of the document. Section adornments are the underlines or |
| 215 |
under- and overlines used to mark a section title. There are a couple |
| 216 |
of commands to work with section adornments. These commands are bound |
| 217 |
to key bindings starting with ``C-c C-a``. |
| 218 |
|
| 219 |
Adjusting a Section Title |
| 220 |
------------------------- |
| 221 |
|
| 222 |
There is a function that helps a great deal to maintain these |
| 223 |
adornments: ``rst-adjust`` (bound to ``C-c C-a C-a``, ``C-c C-=``, and |
| 224 |
``C-=``). This function is a Swiss army knife that can be invoked |
| 225 |
repeatedly and whose behavior depends on context: |
| 226 |
|
| 227 |
#. If there is an incomplete adornment, e.g. :: |
| 228 |
|
| 229 |
My Section Title |
| 230 |
== |
| 231 |
|
| 232 |
invocation will complete the adornment. It can also be used to |
| 233 |
adjust the length of the existing adornment when you need to edit |
| 234 |
the title. |
| 235 |
|
| 236 |
#. If there is no section adornment at all, by default an adornment of |
| 237 |
the same level as the last encountered section level is added. You |
| 238 |
can simply enter a few characters of the title and invoke the |
| 239 |
function to create the section adornment. |
| 240 |
|
| 241 |
The variable ``rst-new-adornment-down`` can be customized to create |
| 242 |
one level lower adornments than the previous section title instead |
| 243 |
of keeping the level. |
| 244 |
|
| 245 |
#. If there is already a section adornment, it is promoted one level |
| 246 |
up. You can invoke it like this repeatedly to cycle the title |
| 247 |
through the hierarchy of existing adornments. |
| 248 |
|
| 249 |
Invoking the function with a negative prefix argument, e.g. ``C-- |
| 250 |
C-=``, will effectively reverse the direction of adornment cycling. |
| 251 |
To alternate between underline-only and over-and-under styles, you can |
| 252 |
use a regular prefix argument, e.g. ``C-u C-=``. See the |
| 253 |
documentation of ``rst-adjust`` for more description of the prefix |
| 254 |
arguments to alter the behavior of the function. |
| 255 |
|
| 256 |
Promoting and Demoting Many Sections |
| 257 |
------------------------------------ |
| 258 |
|
| 259 |
When you are re-organizing the structure of a document, it can be |
| 260 |
useful to change the level of a number of section titles. The same |
| 261 |
key binding can be used to do that: if the region is active when the |
| 262 |
binding is invoked, all the section titles that are within the region |
| 263 |
are promoted accordingly (or demoted, with negative prefix argument). |
| 264 |
|
| 265 |
Redoing All the Adornments to Your Taste |
| 266 |
---------------------------------------- |
| 267 |
|
| 268 |
If you open someone else's file and the adornments it contains are |
| 269 |
unfamiliar, you may want to readjust them to fit your own preferred |
| 270 |
hierarchy of adornments. This can be difficult to perform by hand. |
| 271 |
However, you can do this easily by invoking |
| 272 |
``rst-straighten-adornments`` (``C-c C-a C-s``), which operates on the |
| 273 |
entire buffer. |
| 274 |
|
| 275 |
Customizations for Adornments |
| 276 |
----------------------------- |
| 277 |
|
| 278 |
You can customize the variable ``rst-preferred-adornments`` to a list |
| 279 |
of the adornments that you like to use for documents. |
| 280 |
|
| 281 |
If you prefer adornments according to |
| 282 |
http://sphinx-doc.org/rest.html#sections you may customize it to end |
| 283 |
up with a value like this:: |
| 284 |
|
| 285 |
((35 over-and-under 0) ; ?# |
| 286 |
(42 over-and-under 0) ; ?* |
| 287 |
(61 simple 0) ; ?= |
| 288 |
(45 simple 0) ; ?- |
| 289 |
(94 simple 0) ; ?^ |
| 290 |
(34 simple 0)) ; ?" |
| 291 |
|
| 292 |
This will become the default in a later version of ``rst.el``. |
| 293 |
|
| 294 |
If you set ``rst-preferred-adornments`` to nil resembling the empty |
| 295 |
list only the section adornment found in the buffer will be used. |
| 296 |
|
| 297 |
Viewing the Hierarchy of Section Adornments |
| 298 |
------------------------------------------- |
| 299 |
|
| 300 |
You can visualize the hierarchy of the section adornments in the |
| 301 |
current buffer by invoking ``rst-display-adornments-hierarchy``, bound |
| 302 |
on ``C-c C-a C-d``. A temporary buffer will appear with fake section |
| 303 |
titles rendered in the style of the current document. This can be |
| 304 |
useful when editing other people's documents to find out which section |
| 305 |
adornments correspond to which levels. |
| 306 |
|
| 307 |
Movement and Selection |
| 308 |
====================== |
| 309 |
|
| 310 |
Movement and Selection for Sections |
| 311 |
----------------------------------- |
| 312 |
|
| 313 |
You can move the cursor between the different section titles by using |
| 314 |
the ``rst-backward-section`` (``C-M-a``) and ``rst-forward-section`` |
| 315 |
(``C-M-e``). To mark the section that cursor lies in, use |
| 316 |
``rst-mark-section`` (``C-M-h``). |
| 317 |
|
| 318 |
The key bindings are modeled after other modes with similar |
| 319 |
functionality. |
| 320 |
|
| 321 |
Movements and Selection for Text Blocks |
| 322 |
--------------------------------------- |
| 323 |
|
| 324 |
The understanding of reStructuredText_ of ``rst-mode`` is used to set |
| 325 |
all the variables influencing Emacs' understanding of paragraphs. Thus |
| 326 |
all operations on paragraphs work as usual. For instance |
| 327 |
``forward-paragraph`` (``M-}``) works as usual. |
| 328 |
|
| 329 |
Indenting and Filling |
| 330 |
===================== |
| 331 |
|
| 332 |
Indentation of text plays a major role in the syntax of |
| 333 |
reStructuredText_. It is tedious to maintain the indentation |
| 334 |
manually. ``rst-mode`` understands most of the structure of |
| 335 |
reStructuredText_ allowing for sophisticated indentation and filling |
| 336 |
support described in this section. |
| 337 |
|
| 338 |
Indenting Text Blocks |
| 339 |
--------------------- |
| 340 |
|
| 341 |
``rst-mode`` supports indentation of text blocks by the command |
| 342 |
``rst-shift-region`` (``C-c C-r TAB``). Mark a region and use ``C-c |
| 343 |
C-r TAB`` to indent all blocks one tab to the right. Use ``M-- C-c C-r |
| 344 |
TAB`` to indent the region one tab to the left. |
| 345 |
|
| 346 |
You may use arbitrary prefix arguments such as ``M-2`` or ``M-- 2`` to |
| 347 |
determine the number of tabs you want to indent. A prefix of ``M-0`` |
| 348 |
removes all indentation in the active region. |
| 349 |
|
| 350 |
A tab is an indentation making sense for the block at hand in |
| 351 |
reStructuredText_ syntax. In some cases the exact indentation depends |
| 352 |
on personal taste. You may customize a couple of variables ``M-x |
| 353 |
customize-group<RET> rst-indent<RET>`` to match your taste. |
| 354 |
|
| 355 |
Indenting Lines While Typing |
| 356 |
---------------------------- |
| 357 |
|
| 358 |
In Emacs_ the ``TAB`` key is often used for indenting the current |
| 359 |
line. ``rst-mode`` implements this for the sophisticated indentation |
| 360 |
rules of reStructuredText_. Pressing ``TAB`` cycles through the |
| 361 |
possible tabs for the current line. In the same manner |
| 362 |
``newline-and-indent`` (``C-j``) indents the new line properly. |
| 363 |
|
| 364 |
This is very handy while writing lists. Consider this |
| 365 |
reStructuredText_ bullet list with the cursor at ``@``:: |
| 366 |
|
| 367 |
* Level 1 |
| 368 |
|
| 369 |
* Level 2@ |
| 370 |
|
| 371 |
Type ``C-j`` twice to get this:: |
| 372 |
|
| 373 |
* Level 1 |
| 374 |
|
| 375 |
* Level 2 |
| 376 |
|
| 377 |
@ |
| 378 |
|
| 379 |
Now you an enter text at this level, or start a new list item by |
| 380 |
typing another ``*``. Or you may type ``TAB`` to reduce the |
| 381 |
indentation once:: |
| 382 |
|
| 383 |
* Level 1 |
| 384 |
|
| 385 |
* Level 2 |
| 386 |
|
| 387 |
@ |
| 388 |
|
| 389 |
Typing another ``TAB`` gets you to the first level:: |
| 390 |
|
| 391 |
* Level 1 |
| 392 |
|
| 393 |
* Level 2 |
| 394 |
|
| 395 |
@ |
| 396 |
|
| 397 |
.. note:: Since Emacs_ V24.4 ``electric-indent-mode`` is globally on. |
| 398 |
This breaks indentation in ``rst-mode`` and renders |
| 399 |
``rst-mode`` mostly useless. This is fixed in V1.4.1 of |
| 400 |
``rst-mode``. |
| 401 |
|
| 402 |
A quick fix for older versions of ``rst.el`` is to add the |
| 403 |
following line at the end of the ``(define-derived-mode |
| 404 |
rst-mode ...`` block in your copy of ``rst.el``:: |
| 405 |
|
| 406 |
(setq electric-indent-inhibit t) |
| 407 |
|
| 408 |
You may also install V1.4.1 or newer locally_. |
| 409 |
|
| 410 |
Filling |
| 411 |
------- |
| 412 |
|
| 413 |
``rst-mode`` understanding the indentation rules of reStructuredText_ |
| 414 |
also supports filling paragraphs. Just use ``fill-paragraph`` |
| 415 |
(``M-q``) as you do in other modes. |
| 416 |
|
| 417 |
Operating on Lists |
| 418 |
================== |
| 419 |
|
| 420 |
Lists are supported in various flavors in reStructuredText_. |
| 421 |
``rst-mode`` understands reStructuredText_ lists and offers some |
| 422 |
support for operating on lists. Key bindings for commands for |
| 423 |
operating on lists start with ``C-c C-l``. |
| 424 |
|
| 425 |
Please note that so far definition lists are not explicitly supported |
| 426 |
by ``rst-mode``. |
| 427 |
|
| 428 |
Bulleted and Enumerated Lists |
| 429 |
----------------------------- |
| 430 |
|
| 431 |
If you have a couple of plain lines you want to turn into an |
| 432 |
enumerated list you can invoke ``rst-enumerate-region`` (``C-c C-l |
| 433 |
C-e``). For example, the following region :: |
| 434 |
|
| 435 |
Apples |
| 436 |
|
| 437 |
Oranges |
| 438 |
|
| 439 |
Bananas |
| 440 |
|
| 441 |
becomes :: |
| 442 |
|
| 443 |
1. Apples |
| 444 |
|
| 445 |
2. Oranges |
| 446 |
|
| 447 |
3. Bananas |
| 448 |
|
| 449 |
``rst-bullet-list-region`` (``C-c C-l C-b``) does the same, but |
| 450 |
results in a bullet list :: |
| 451 |
|
| 452 |
* Apples |
| 453 |
|
| 454 |
* Oranges |
| 455 |
|
| 456 |
* Bananas |
| 457 |
|
| 458 |
By default, each paragraph starting on the leftmost line in the |
| 459 |
highlighted region will be taken to be a single list or enumeration |
| 460 |
item, for example, enumerating the following:: |
| 461 |
|
| 462 |
An apple a day |
| 463 |
keeps the doctor away. |
| 464 |
|
| 465 |
But oranges |
| 466 |
are tastier than apples. |
| 467 |
|
| 468 |
If you preferred bananas |
| 469 |
you may be |
| 470 |
a monkey. |
| 471 |
|
| 472 |
Will result in:: |
| 473 |
|
| 474 |
1. An apple a day |
| 475 |
keeps the doctor away. |
| 476 |
|
| 477 |
2. But oranges |
| 478 |
are tastier than apples. |
| 479 |
|
| 480 |
3. If you preferred bananas |
| 481 |
you may be |
| 482 |
a monkey. |
| 483 |
|
| 484 |
If you would like to enumerate each of the lines, use a prefix |
| 485 |
argument on the preceding commands, e.g.:: |
| 486 |
|
| 487 |
Apples |
| 488 |
Oranges |
| 489 |
Bananas |
| 490 |
|
| 491 |
becomes:: |
| 492 |
|
| 493 |
* Apples |
| 494 |
* Oranges |
| 495 |
* Bananas |
| 496 |
|
| 497 |
Straightening Existing Bullet List Hierarchies |
| 498 |
---------------------------------------------- |
| 499 |
|
| 500 |
If you invoke ``rst-straighten-bullets-region`` (``C-c C-l C-s``), the |
| 501 |
existing bullets in the active region will be replaced to reflect |
| 502 |
their respective level. This does not make a difference in the |
| 503 |
document structure that reStructuredText_ defines, but looks better |
| 504 |
in, for example, if all of the top-level bullet items use the |
| 505 |
character ``-``, and all of the 2nd level items use ``*``, etc. |
| 506 |
|
| 507 |
Inserting a List Item |
| 508 |
--------------------- |
| 509 |
|
| 510 |
To start a new list you may invoke ``rst-insert-list`` (``C-c C-l |
| 511 |
C-i``). You may choose from an item style supported by |
| 512 |
reStructuredText_. |
| 513 |
|
| 514 |
You may also invoke ``rst-insert-list`` at the end of a list item. In |
| 515 |
this case it inserts a new line containing the markup for the a list |
| 516 |
item on the same level. |
| 517 |
|
| 518 |
Operating on Other Text Blocks |
| 519 |
============================== |
| 520 |
|
| 521 |
Creating and Removing Line Blocks |
| 522 |
--------------------------------- |
| 523 |
|
| 524 |
To create line blocks, first select the region to convert and invoke |
| 525 |
``rst-line-block-region`` ``C-c C-r C-l``. For example, the following |
| 526 |
:: |
| 527 |
|
| 528 |
Apples |
| 529 |
Oranges |
| 530 |
Bananas |
| 531 |
|
| 532 |
becomes :: |
| 533 |
|
| 534 |
| Apples |
| 535 |
| Oranges |
| 536 |
| Bananas |
| 537 |
|
| 538 |
This works even if the region is indented. To remove line blocks, |
| 539 |
select a region and invoke with a prefix argument. |
| 540 |
|
| 541 |
Commenting a Region of Text |
| 542 |
--------------------------- |
| 543 |
|
| 544 |
``rst-mode`` understands reStructuredText_ comments. Use |
| 545 |
``comment-dwim`` (``M-;``) to work on comments as usual:: |
| 546 |
|
| 547 |
Apples |
| 548 |
Oranges |
| 549 |
Bananas |
| 550 |
|
| 551 |
becomes:: |
| 552 |
|
| 553 |
.. |
| 554 |
Apples |
| 555 |
Oranges |
| 556 |
Bananas |
| 557 |
|
| 558 |
To remove a comment you have to tell this to ``comment-dwim`` |
| 559 |
explicitly by using a prefix argument (``C-u M-;``). |
| 560 |
|
| 561 |
Please note that only indented comments are supported properly by the |
| 562 |
parts of ``comment-dwim`` working on regions. |
| 563 |
|
| 564 |
.. _Conversion: |
| 565 |
|
| 566 |
Converting Documents from Emacs |
| 567 |
=============================== |
| 568 |
|
| 569 |
``rst-mode`` provides a number of functions for running documents |
| 570 |
being edited through the docutils tools. The key bindings for these |
| 571 |
commands start with ``C-c C-c``. |
| 572 |
|
| 573 |
The main generic function is ``rst-compile`` (``C-c C-c C-c``). It |
| 574 |
invokes a compilation command with the correct output name for the |
| 575 |
current buffer and then invokes Emacs' compile function. It also looks |
| 576 |
for the presence of a ``docutils.conf`` configuration file in the |
| 577 |
parent directories and adds it to the command line options. There is also |
| 578 |
``rst-compile-alt-toolset`` (``C-c C-c C-a``) in case you often need |
| 579 |
run your document in a second toolset. |
| 580 |
|
| 581 |
You can customize the commands being used by setting |
| 582 |
``rst-compile-primary-toolset`` and ``rst-compile-secondary-toolset``. |
| 583 |
|
| 584 |
Other commands are available for other formats: |
| 585 |
|
| 586 |
* ``rst-compile-pseudo-region`` (``C-c C-c C-x``) |
| 587 |
|
| 588 |
When crafting documents, it is often convenient to view which data |
| 589 |
structures docutils will parse them into. You can use to run the |
| 590 |
active region through ``rst2pseudoxml.py`` and have the output |
| 591 |
automatically be displayed in a new buffer. |
| 592 |
|
| 593 |
* ``rst-compile-pdf-preview`` (``C-c C-c C-p``) |
| 594 |
|
| 595 |
Convert the current document to PDF and launch a viewer on the |
| 596 |
results. |
| 597 |
|
| 598 |
* ``rst-compile-slides-preview`` (``C-c C-c C-s``): Convert the |
| 599 |
current document to S5 slides and view in a web browser. |
| 600 |
|
| 601 |
Imenu Support |
| 602 |
============= |
| 603 |
|
| 604 |
Using Imenu |
| 605 |
----------- |
| 606 |
|
| 607 |
Emacs_ has a package called ``imenu``. ``rst-mode`` supports Imenu by |
| 608 |
adding a function to convert the structure of a reStructuredText_ |
| 609 |
buffer to an Imenu index. Thus you can use invoke ``imenu`` (``M-x |
| 610 |
imenu``) to navigate through the section index or invoke |
| 611 |
``imenu-add-to-menubar`` (``M-x imenu-add-to-menubar``) to add an |
| 612 |
Imenu menu entry to Emacs' menu bar. |
| 613 |
|
| 614 |
Using which function |
| 615 |
-------------------- |
| 616 |
|
| 617 |
As a side effect of Imenu support the ``which-func`` package is also |
| 618 |
supported. Invoke ``which-function-mode`` (``M-x |
| 619 |
which-function-mode``) to add the name of the current section to the |
| 620 |
mode line. This is especially useful if you navigate through documents |
| 621 |
with long sections which do not fit on a single screen. |
| 622 |
|
| 623 |
Using the Table of Contents |
| 624 |
=========================== |
| 625 |
|
| 626 |
The sections in a reStructuredText_ document can be used to form a |
| 627 |
table of contents. ``rst-mode`` can work with such a table of contents |
| 628 |
in various forms. Key bindings for these commands start with ``C-c |
| 629 |
C-t``. |
| 630 |
|
| 631 |
Navigating Using the Table of Contents |
| 632 |
-------------------------------------- |
| 633 |
|
| 634 |
When you are editing long documents, it can be a bit difficult to |
| 635 |
orient yourself in the structure of your text. To that effect, a |
| 636 |
function is provided that presents a hierarchically indented table of |
| 637 |
contents of the document in a temporary buffer, in which you can |
| 638 |
navigate and press ``Return`` to go to a specific section. |
| 639 |
|
| 640 |
Invoke ``rst-toc`` (``C-c C-t C-t``). It presents a temporary buffer |
| 641 |
that looks something like this:: |
| 642 |
|
| 643 |
Table of Contents: |
| 644 |
Debugging Meta-Techniques |
| 645 |
Introduction |
| 646 |
Debugging Solution Patterns |
| 647 |
Recognize That a Bug Exists |
| 648 |
Subdivide and Isolate |
| 649 |
Identify and Verify Assumptions |
| 650 |
Use a Tool for Introspection |
| 651 |
Change one thing at a time |
| 652 |
Learn about the System |
| 653 |
Understanding a bug |
| 654 |
The Basic Steps in Debugging |
| 655 |
Attitude |
| 656 |
Bad Feelings |
| 657 |
Good Feelings |
| 658 |
References |
| 659 |
|
| 660 |
When you move the cursor to a section title and press ``RET`` or ``f`` |
| 661 |
or click with ``button1`` on a section title, the temporary buffer |
| 662 |
disappears and you are left with the cursor positioned at the chosen |
| 663 |
section. Clicking with ``button2`` jumps to the respective section but |
| 664 |
keeps the toc buffer. You can use this to look at the various section |
| 665 |
headers quickly. Use ``q`` in this buffer to just quit it without |
| 666 |
moving the cursor in the original document. Use ``z`` to zap the |
| 667 |
buffer altogether. |
| 668 |
|
| 669 |
Inserting a Table of Contents |
| 670 |
----------------------------- |
| 671 |
|
| 672 |
Oftentimes in long text documents that are meant to be read directly, |
| 673 |
a table of contents is inserted at the beginning of the text. In |
| 674 |
reStructuredText_ documents, since the table of contents is |
| 675 |
automatically generated by the parser with the ``.. contents::`` |
| 676 |
directive, people generally have not been adding an explicit table of |
| 677 |
contents to their source documents, and partly because it is too much |
| 678 |
trouble to edit and maintain. |
| 679 |
|
| 680 |
The Emacs_ support for reStructuredText_ provides a function to insert |
| 681 |
such a table of contents in your document. Since it is not meant to |
| 682 |
be part of the document text, you should place such a table of |
| 683 |
contents within a comment, so that it is ignored by the parser. This |
| 684 |
is the favored usage:: |
| 685 |
|
| 686 |
.. contents:: |
| 687 |
.. |
| 688 |
1 Introduction |
| 689 |
2 Debugging Solution Patterns |
| 690 |
2.1 Recognize That a Bug Exists |
| 691 |
2.2 Subdivide and Isolate |
| 692 |
2.3 Identify and Verify Assumptions |
| 693 |
2.4 Use a Tool for Introspection |
| 694 |
2.5 Change one thing at a time |
| 695 |
2.6 Learn about the System |
| 696 |
3 Understanding a bug |
| 697 |
4 The Basic Steps in Debugging |
| 698 |
5 Attitude |
| 699 |
5.1 Bad Feelings |
| 700 |
5.2 Good Feelings |
| 701 |
6 References |
| 702 |
|
| 703 |
Just place the cursor at the top-left corner where you want to insert |
| 704 |
the TOC and invoke the function ``rst-toc-insert`` with ``C-c C-t |
| 705 |
C-i``. The table of contents will display all the section titles that |
| 706 |
are under the location where the insertion occurs. This way you can |
| 707 |
insert local table of contents by placing them in the appropriate |
| 708 |
location. |
| 709 |
|
| 710 |
You can use a numeric prefix argument to limit the depth of rendering |
| 711 |
of the TOC. |
| 712 |
|
| 713 |
You can customize the look of the TOC by setting the values of the |
| 714 |
following variables: ``rst-toc-indent``, ``rst-toc-insert-style``, |
| 715 |
``rst-toc-insert-max-level``. |
| 716 |
|
| 717 |
Maintaining the Table of Contents Up-to-date |
| 718 |
-------------------------------------------- |
| 719 |
|
| 720 |
One issue is that you will probably want to maintain the inserted |
| 721 |
table of contents up-to-date. ``rst-toc-update`` (``C-c C-t C-u``) |
| 722 |
will automatically update an inserted table of contents following a |
| 723 |
``.. contents::`` directive laid out like the example above. |
| 724 |
|
| 725 |
Syntax Highlighting via Font-Lock |
| 726 |
================================= |
| 727 |
|
| 728 |
``rst-mode`` provides syntax highlighting for nearly all to |
| 729 |
reStructuredText_ constructs. |
| 730 |
|
| 731 |
Use ``customize-group rst-faces`` to customize the faces used for |
| 732 |
font-locking. |
| 733 |
|
| 734 |
Customization |
| 735 |
============= |
| 736 |
|
| 737 |
Some aspects of ``rst-mode`` can be configured through the |
| 738 |
customization feature of Emacs_. Try :: |
| 739 |
|
| 740 |
M-x customize-group<RETURN>rst |
| 741 |
|
| 742 |
for all customizations or use the respective menu entry. Those |
| 743 |
customizations which are useful for many people are described in this |
| 744 |
section. |
| 745 |
|
| 746 |
Customizing Section Title Formatting |
| 747 |
------------------------------------ |
| 748 |
|
| 749 |
For a couple of things the reStructuredText_ syntax offers a choice of |
| 750 |
options on how to do things exactly. Some of these choices influence |
| 751 |
the operation of ``rst.el`` and thus can be configured. The |
| 752 |
customizations are contained in the ``rst-adjust`` group. |
| 753 |
|
| 754 |
Among these things is the exact layout of section adornments. In fact |
| 755 |
reStructuredText_ prescribes only the characters and how these |
| 756 |
characters must be used but the exact use of concrete adornments may |
| 757 |
be different in every source file. Using the customization option |
| 758 |
``rst-preferred-adornments`` you can tell ``rst-mode`` on the exact |
| 759 |
sequence of adornments you prefer to markup the different levels of |
| 760 |
sections headers. |
| 761 |
|
| 762 |
Finally the title text of over-and-under adornments may be indented in |
| 763 |
reStructuredText_. ``rst-default-indent`` tells ``rst-mode`` how many |
| 764 |
positions a over-and-under adornment should be indented when toggling |
| 765 |
from simple adornment and in case a consistent indentation throughout |
| 766 |
the whole buffer for such adornment is needed. |
| 767 |
|
| 768 |
Customizing Indentation |
| 769 |
----------------------- |
| 770 |
|
| 771 |
reStructuredText_ uses indentation a lot to signify a certain meaning. |
| 772 |
In some cases the exact amount of indentation is prescribed by the |
| 773 |
syntax while in some cases the exact indentation is not fixed. The |
| 774 |
customization group ``rst-indent`` allows to customize the amount of |
| 775 |
indentation in these cases. |
| 776 |
|
| 777 |
In field lists the content of a field needs to be indented relative to |
| 778 |
the field label. ``rst-indent-field`` tells ``rst-mode`` the amount of |
| 779 |
indentation to use for field content. A value of zero always indents |
| 780 |
according to the content after the field label. |
| 781 |
|
| 782 |
The indentation of literal blocks is controlled by |
| 783 |
``rst-indent-literal-normal`` and ``rst-indent-literal-minimized``. |
| 784 |
The first is used when the leading literal tag (``::``) appears alone |
| 785 |
on a line. The second is used when the minimized style is used where |
| 786 |
the literal tag follows some text. |
| 787 |
|
| 788 |
The indentation of comments is controlled by ``rst-indent-comment``. |
| 789 |
Of course this makes only sense for the indented comments of |
| 790 |
reStructuredText_. |
| 791 |
|
| 792 |
Customization option ``rst-indent-width`` gives the default |
| 793 |
indentation when there are no other hints on what amount of |
| 794 |
indentation to use. |
| 795 |
|
| 796 |
Customizing Faces |
| 797 |
----------------- |
| 798 |
|
| 799 |
The faces used for font-locking can be defined in the ``rst-faces`` |
| 800 |
customization group. The customization options ending in ``-face`` are |
| 801 |
only there for backward compatibility so please leave them as they |
| 802 |
are. |
| 803 |
|
| 804 |
reStructuredText_ sets no limit on the nesting of sections. By default |
| 805 |
there are six levels of fontification defined. Section titles deeper |
| 806 |
than six level have no special fontification - only the adornments are |
| 807 |
fontified. The exact mapping from a level to a face is done by by |
| 808 |
``rst-adornment-faces-alist``, however. So if you need fontification |
| 809 |
deeper than six levels you may want to customize this option. You may |
| 810 |
also want to customize it if you like the general idea of section |
| 811 |
title fontification in ``rst-mode`` but for instance prefer a reversed |
| 812 |
order. |
| 813 |
|
| 814 |
Customizing Conversion |
| 815 |
---------------------- |
| 816 |
|
| 817 |
Conversion_ can be customized by the customization options in the |
| 818 |
customization group ``rst-compile``. |
| 819 |
|
| 820 |
If some conversion does not work as expected please check |
| 821 |
the variable ``rst-compile-toolsets`` :: |
| 822 |
|
| 823 |
M-x customize-option<RETURN>rst-compile-toolsets |
| 824 |
|
| 825 |
This variable defines the commands and other details used for |
| 826 |
conversion. In case of problems please check that the commands are |
| 827 |
either available or customize them to what is available in your |
| 828 |
environment. |
| 829 |
|
| 830 |
.. note:: There are some options in V1.4.1 of ``rst.el`` which should |
| 831 |
be customization options but are not yet. Customization |
| 832 |
support will be added in a later version. |
| 833 |
|
| 834 |
.. note:: Please note that there is a package ``rst2pdf`` based on the |
| 835 |
ReportLab library. Please note that the command of this |
| 836 |
package requires an additional ``-o`` for naming the output |
| 837 |
file. This breaks the usual conventions employed by Docutils |
| 838 |
tools. ``rst-mode`` V1.4.1 does not support this directly. |
| 839 |
|
| 840 |
Other Customizations |
| 841 |
-------------------- |
| 842 |
|
| 843 |
``rst-preferred-bullets`` can be customized to hold your preferred set |
| 844 |
of bullets to use for bulleted lists. |
| 845 |
|
| 846 |
``rst-mode-hook`` is a normal major mode hook which may be customized. |
| 847 |
It is run if you enter ``rst-mode``. |
| 848 |
|
| 849 |
Related aspects |
| 850 |
=============== |
| 851 |
|
| 852 |
This section covers some general aspects using Emacs_ for editing |
| 853 |
reStructuredText_ source. They are not directly related to |
| 854 |
``rst-mode`` but may enhance your experience. |
| 855 |
|
| 856 |
``text-mode`` Settings |
| 857 |
---------------------- |
| 858 |
|
| 859 |
Consult the Emacs_ manual for more ``text-mode`` customizations. In |
| 860 |
particular, you may be interested in setting the following variables, |
| 861 |
functions and modes that pertain somewhat to ``text-mode``: |
| 862 |
|
| 863 |
* ``indent-tabs-mode`` |
| 864 |
* ``colon-double-space`` |
| 865 |
* ``sentence-end-double-space`` |
| 866 |
* ``auto-fill-mode`` |
| 867 |
* ``auto-mode-alist`` |
| 868 |
|
| 869 |
Editing Tables: Emacs table mode |
| 870 |
-------------------------------- |
| 871 |
|
| 872 |
You may want to check out `Emacs table mode`_ to create an edit |
| 873 |
tables, it allows creating ASCII tables compatible with |
| 874 |
reStructuredText_. |
| 875 |
|
| 876 |
.. _Emacs table mode: http://table.sourceforge.net/ |
| 877 |
|
| 878 |
Character Processing |
| 879 |
-------------------- |
| 880 |
|
| 881 |
Since reStructuredText punts on the issue of character processing, |
| 882 |
here are some useful resources for Emacs_ users in the Unicode world: |
| 883 |
|
| 884 |
* `xmlunicode.el and unichars.el from Norman Walsh |
| 885 |
<http://nwalsh.com/emacs/xmlchars/index.html>`__ |
| 886 |
|
| 887 |
* `An essay by Tim Bray, with example code |
| 888 |
<http://www.tbray.org/ongoing/When/200x/2003/09/27/UniEmacs>`__ |
| 889 |
|
| 890 |
* For Emacs_ users on Mac OS X, here are some useful useful additions |
| 891 |
to your .emacs file. |
| 892 |
|
| 893 |
- To get direct keyboard input of non-ASCII characters (like |
| 894 |
"option-e e" resulting in "é" [eacute]), first enable the option |
| 895 |
key by setting the command key as your meta key:: |
| 896 |
|
| 897 |
(setq mac-command-key-is-meta t) ;; nil for option key |
| 898 |
|
| 899 |
Next, use one of these lines:: |
| 900 |
|
| 901 |
(set-keyboard-coding-system 'mac-roman) |
| 902 |
(setq mac-keyboard-text-encoding kTextEncodingISOLatin1) |
| 903 |
|
| 904 |
I prefer the first line, because it enables non-Latin-1 characters |
| 905 |
as well (em-dash, curly quotes, etc.). |
| 906 |
|
| 907 |
- To enable the display of all characters in the Mac-Roman charset, |
| 908 |
first create a fontset listing the fonts to use for each range of |
| 909 |
characters using charsets that Emacs_ understands:: |
| 910 |
|
| 911 |
(create-fontset-from-fontset-spec |
| 912 |
"-apple-monaco-medium-r-normal--10-*-*-*-*-*-fontset-monaco, |
| 913 |
ascii:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman, |
| 914 |
latin-iso8859-1:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman, |
| 915 |
mule-unicode-0100-24ff:-apple-monaco-medium-r-normal--10-100-75-75-m-100-mac-roman") |
| 916 |
|
| 917 |
Latin-1 doesn't cover characters like em-dash and curly quotes, so |
| 918 |
"mule-unicode-0100-24ff" is needed. |
| 919 |
|
| 920 |
Next, use that fontset:: |
| 921 |
|
| 922 |
(set-frame-font "fontset-monaco") |
| 923 |
|
| 924 |
- To enable cooperation between the system clipboard and the Emacs_ |
| 925 |
kill ring, add this line:: |
| 926 |
|
| 927 |
(set-clipboard-coding-system 'mac-roman) |
| 928 |
|
| 929 |
Other useful resources are in `Andrew Choi's Emacs 21 for Mac OS X |
| 930 |
FAQ <http://members.shaw.ca/akochoi-emacs/stories/faq.html>`__. |
| 931 |
|
| 932 |
Credits |
| 933 |
======= |
| 934 |
|
| 935 |
Part of the original code of ``rst.el`` has been written by Martin |
| 936 |
Blais and David Goodger and Wei-Wei Guo. The font-locking came from |
| 937 |
Stefan Merten. |
| 938 |
|
| 939 |
Most of the code has been modified, enhanced and extended by Stefan |
| 940 |
Merten who also is the current maintainer of ``rst.el``. |
| 941 |
|
| 942 |
.. _Emacs: https://www.gnu.org/software/emacs/emacs.html |
| 943 |
.. _reStructuredText: https://docutils.sourceforge.io/rst.html |
| 944 |
.. _Docutils: https://docutils.sourceforge.io/ |
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
.. LocalWords: reST utf Merten Blais rst el docutils modeline emacs |
| 949 |
.. LocalWords: Init mEmacs sInit alist setq txt overlines RET nd py |
| 950 |
.. LocalWords: dwim conf toolset pseudoxml pdf Imenu imenu menubar |
| 951 |
.. LocalWords: func toc xmlunicode unichars eacute charset fontset |
| 952 |
.. LocalWords: kTextEncodingISOLatin charsets monaco ascii latin |
| 953 |
.. LocalWords: iso unicode Choi's Goodger Guo |
| 954 |
|
| 955 |
.. |
| 956 |
Local Variables: |
| 957 |
mode: rst |
| 958 |
indent-tabs-mode: nil |
| 959 |
fill-column: 70 |
| 960 |
End: |