home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / unix / info / emacs.i07 (.txt) < prev    next >
GNU Info File  |  1993-06-14  |  51KB  |  893 lines

  1. This is Info file ../info/emacs, produced by Makeinfo-1.47 from the
  2. input file emacs.tex.
  3.    This file documents the GNU Emacs editor.
  4.    Copyright (C) 1985, 1986, 1988 Richard M. Stallman.
  5.    Permission is granted to make and distribute verbatim copies of this
  6. manual provided the copyright notice and this permission notice are
  7. preserved on all copies.
  8.    Permission is granted to copy and distribute modified versions of
  9. this manual under the conditions for verbatim copying, provided also
  10. that the sections entitled "The GNU Manifesto", "Distribution" and "GNU
  11. General Public License" are included exactly as in the original, and
  12. provided that the entire resulting derived work is distributed under the
  13. terms of a permission notice identical to this one.
  14.    Permission is granted to copy and distribute translations of this
  15. manual into another language, under the above conditions for modified
  16. versions, except that the sections entitled "The GNU Manifesto",
  17. "Distribution" and "GNU General Public License" may be included in a
  18. translation approved by the author instead of in the original English.
  19. File: emacs,  Node: Paragraphs,  Next: Pages,  Prev: Sentences,  Up: Text
  20. Paragraphs
  21. ==========
  22.    The Emacs commands for manipulating paragraphs are also `Meta-' keys.
  23. `M-['
  24.      Move back to previous paragraph beginning
  25.      (`backward-paragraph').
  26. `M-]'
  27.      Move forward to next paragraph end (`forward-paragraph').
  28. `M-h'
  29.      Put point and mark around this or next paragraph
  30.      (`mark-paragraph').
  31.    `Meta-[' moves to the beginning of the current or previous paragraph,
  32. while `Meta-]' moves to the end of the current or next paragraph. Blank
  33. lines and text formatter command lines separate paragraphs and are not
  34. part of any paragraph.  Also, an indented line starts a new paragraph.
  35.    In major modes for programs (as opposed to Text mode), paragraphs
  36. begin and end only at blank lines.  This makes the paragraph commands
  37. continue to be useful even though there are no paragraphs per se.
  38.    When there is a fill prefix, then paragraphs are delimited by all
  39. lines which don't start with the fill prefix.  *Note Filling::.
  40.    When you wish to operate on a paragraph, you can use the command
  41. `Meta-h' (`mark-paragraph') to set the region around it.  This command
  42. puts point at the beginning and mark at the end of the paragraph point
  43. was in.  If point is between paragraphs (in a run of blank lines, or at
  44. a boundary), the paragraph following point is surrounded by point and
  45. mark.  If there are blank lines preceding the first line of the
  46. paragraph, one of these blank lines is included in the region.  Thus,
  47. for example, `M-h C-w' kills the paragraph around or after point.
  48.    The precise definition of a paragraph boundary is controlled by the
  49. variables `paragraph-separate' and `paragraph-start'.  The value of
  50. `paragraph-start' is a regexp that should match any line that either
  51. starts or separates paragraphs.  The value of `paragraph-separate' is
  52. another regexp that should match only lines that separate paragraphs
  53. without being part of any paragraph.  Lines that start a new paragraph
  54. and are contained in it must match both regexps.  For example, normally
  55. `paragraph-start' is `"^[ \t\n\f]"' and `paragraph-separate' is `"^[
  56. \t\f]*$"'.
  57.    Normally it is desirable for page boundaries to separate paragraphs.
  58. The default values of these variables recognize the usual separator for
  59. pages.
  60. File: emacs,  Node: Pages,  Next: Filling,  Prev: Paragraphs,  Up: Text
  61. Pages
  62. =====
  63.    Files are often thought of as divided into "pages" by the "formfeed"
  64. character (ASCII Control-L, octal code 014).  For example, if a file is
  65. printed on a line printer, each page of the file, in this sense, will
  66. start on a new page of paper.  Emacs treats a page-separator character
  67. just like any other character.  It can be inserted with `C-q C-l', or
  68. deleted with DEL.  Thus, you are free to paginate your file or not. 
  69. However, since pages are often meaningful divisions of the file,
  70. commands are provided to move over them and operate on them.
  71. `C-x ['
  72.      Move point to previous page boundary (`backward-page').
  73. `C-x ]'
  74.      Move point to next page boundary (`forward-page').
  75. `C-x C-p'
  76.      Put point and mark around this page (or another page)
  77.      (`mark-page').
  78. `C-x l'
  79.      Count the lines in this page (`count-lines-page').
  80.    The `C-x [' (`backward-page') command moves point to immediately
  81. after the previous page delimiter.  If point is already right after a
  82. page delimiter, it skips that one and stops at the previous one.  A
  83. numeric argument serves as a repeat count.  The `C-x ]' (`forward-page')
  84. command moves forward past the next page delimiter.
  85.    The `C-x C-p' command (`mark-page') puts point at the beginning of
  86. the current page and the mark at the end.  The page delimiter at the end
  87. is included (the mark follows it).  The page delimiter at the front is
  88. excluded (point follows it).  This command can be followed by `C-w' to
  89. kill a page which is to be moved elsewhere.  If it is inserted after a
  90. page delimiter, at a place where `C-x ]' or `C-x [' would take you, then
  91. the page will be properly delimited before and after once again.
  92.    A numeric argument to `C-x C-p' is used to specify which page to go
  93. to, relative to the current one.  Zero means the current page.  One
  94. means the next page, and -1 means the previous one.
  95.    The `C-x l' command (`count-lines-page') is good for deciding where
  96. to break a page in two.  It prints in the echo area the total number of
  97. lines in the current page, and then divides it up into those preceding
  98. the current line and those following, as in
  99.      Page has 96 (72+25) lines
  100. Notice that the sum is off by one; this is correct if point is not at
  101. the beginning of a line.
  102.    The variable `page-delimiter' should have as its value a regexp that
  103. matches the beginning of a line that separates pages.  This is what
  104. defines where pages begin.  The normal value of this variable is
  105. `"^\f"', which matches a formfeed character at the beginning of a line.
  106. File: emacs,  Node: Filling,  Next: Case,  Prev: Pages,  Up: Text
  107. Filling Text
  108. ============
  109.    With Auto Fill mode, text can be "filled" (broken up into lines that
  110. fit in a specified width) as you insert it.  If you alter existing text
  111. it may no longer be properly filled; then explicit commands for filling
  112. can be used.
  113. * Menu:
  114. * Auto Fill::      Auto Fill mode breaks long lines automatically.
  115. * Fill Commands:: Commands to refill paragraphs and center lines.
  116. * Fill Prefix::   Filling when every line is indented or in a comment, etc.
  117. File: emacs,  Node: Auto Fill,  Next: Fill Commands,  Prev: Filling,  Up: Filling
  118. Auto Fill Mode
  119. --------------
  120.    "Auto Fill" mode is a minor mode in which lines are broken
  121. automatically when they become too wide.  Breaking happens only when
  122. you type a SPC or RET.
  123. `M-x auto-fill-mode'
  124.      Enable or disable Auto Fill mode.
  125. `SPC'
  126. `RET'
  127.      In Auto Fill mode, break lines when appropriate.
  128.    `M-x auto-fill-mode' turns Auto Fill mode on if it was off, or off if
  129. it was on.  With a positive numeric argument it always turns Auto Fill
  130. mode on, and with a negative argument always turns it off.  You can see
  131. when Auto Fill mode is in effect by the presence of the word `Fill' in
  132. the mode line, inside the parentheses.  Auto Fill mode is a minor mode,
  133. turned on or off for each buffer individually.  *Note Minor Modes::.
  134.    In Auto Fill mode, lines are broken automatically at spaces when
  135. they get longer than the desired width.  Line breaking and
  136. rearrangement takes place only when you type SPC or RET.  If you wish
  137. to insert a space or newline without permitting line-breaking, type
  138. `C-q SPC' or `C-q LFD' (recall that a newline is really a linefeed). 
  139. Also, `C-o' inserts a newline without line breaking.
  140.    Auto Fill mode works well with Lisp mode, because when it makes a new
  141. line in Lisp mode it indents that line with TAB.  If a line ending in a
  142. comment gets too long, the text of the comment is split into two
  143. comment lines.  Optionally new comment delimiters are inserted at the
  144. end of the first line and the beginning of the second so that each line
  145. is a separate comment; the variable `comment-multi-line' controls the
  146. choice (*note Comments::.).
  147.    Auto Fill mode does not refill entire paragraphs.  It can break
  148. lines but cannot merge lines.  So editing in the middle of a paragraph
  149. can result in a paragraph that is not correctly filled.  The easiest
  150. way to make the paragraph properly filled again is usually with the
  151. explicit fill commands.
  152.    Many users like Auto Fill mode and want to use it in all text files.
  153. The section on init files says how to arrange this permanently for
  154. yourself. *Note Init File::.
  155. File: emacs,  Node: Fill Commands,  Next: Fill Prefix,  Prev: Auto Fill,  Up: Filling
  156. Explicit Fill Commands
  157. ----------------------
  158. `M-q'
  159.      Fill current paragraph (`fill-paragraph').
  160. `M-g'
  161.      Fill each paragraph in the region (`fill-region').
  162. `C-x f'
  163.      Set the fill column (`set-fill-column').
  164. `M-x fill-region-as-paragraph.'
  165.      Fill the region, considering it as one paragraph.
  166. `M-s'
  167.      Center a line.
  168.    To refill a paragraph, use the command `Meta-q' (`fill-paragraph'). 
  169. It causes the paragraph that point is inside, or the one after point if
  170. point is between paragraphs, to be refilled.  All the line-breaks are
  171. removed, and then new ones are inserted where necessary.  `M-q' can be
  172. undone with `C-_'.  *Note Undo::.
  173.    To refill many paragraphs, use `M-g' (`fill-region'), which divides
  174. the region into paragraphs and fills each of them.
  175.    `Meta-q' and `Meta-g' use the same criteria as `Meta-h' for finding
  176. paragraph boundaries (*note Paragraphs::.).  For more control, you can
  177. use `M-x fill-region-as-paragraph', which refills everything between
  178. point and mark.  This command recognizes only blank lines as paragraph
  179. separators.
  180.    A numeric argument to `M-g' or `M-q' causes it to "justify" the text
  181. as well as filling it.  This means that extra spaces are inserted to
  182. make the right margin line up exactly at the fill column.  To remove the
  183. extra spaces, use `M-q' or `M-g' with no argument.
  184.    The command `Meta-s' (`center-line') centers the current line within
  185. the current fill column.  With an argument, it centers several lines
  186. individually and moves past them.
  187.    The maximum line width for filling is in the variable `fill-column'.
  188. Altering the value of `fill-column' makes it local to the current
  189. buffer; until that time, the default value is in effect.  The default is
  190. initially 70.  *Note Locals::.
  191.    The easiest way to set `fill-column' is to use the command `C-x f'
  192. (`set-fill-column').  With no argument, it sets `fill-column' to the
  193. current horizontal position of point.  With a numeric argument, it uses
  194. that as the new fill column.
  195. File: emacs,  Node: Fill Prefix,  Prev: Fill Commands,  Up: Filling
  196. The Fill Prefix
  197. ---------------
  198.    To fill a paragraph in which each line starts with a special marker
  199. (which might be a few spaces, giving an indented paragraph), use the
  200. "fill prefix" feature.  The fill prefix is a string which Emacs expects
  201. every line to start with, and which is not included in filling.
  202. `C-x .'
  203.      Set the fill prefix (`set-fill-prefix').
  204. `M-q'
  205.      Fill a paragraph using current fill prefix (`fill-paragraph').
  206. `M-x fill-individual-paragraphs'
  207.      Fill the region, considering each change of indentation as
  208.      starting a new paragraph.
  209.    To specify a fill prefix, move to a line that starts with the desired
  210. prefix, put point at the end of the prefix, and give the command
  211. `C-x .' (`set-fill-prefix').  That's a period after the `C-x'.  To turn
  212. off the fill prefix, specify an empty prefix: type `C-x .' with point
  213. at the beginning of a line.
  214.    When a fill prefix is in effect, the fill commands remove the fill
  215. prefix from each line before filling and insert it on each line after
  216. filling. The fill prefix is also inserted on new lines made
  217. automatically by Auto Fill mode.  Lines that do not start with the fill
  218. prefix are considered to start paragraphs, both in `M-q' and the
  219. paragraph commands; this is just right if you are using paragraphs with
  220. hanging indentation (every line indented except the first one).  Lines
  221. which are blank or indented once the prefix is removed also separate or
  222. start paragraphs; this is what you want if you are writing
  223. multi-paragraph comments with a comment delimiter on each line.
  224.    The fill prefix is stored in the variable `fill-prefix'.  Its value
  225. is a string, or `nil' when there is no fill prefix.  This is a
  226. per-buffer variable; altering the variable affects only the current
  227. buffer, but there is a default value which you can change as well. 
  228. *Note Locals::.
  229.    Another way to use fill prefixes is through `M-x
  230. fill-individual-paragraphs'.  This function divides the region into
  231. groups of consecutive lines with the same amount and kind of
  232. indentation and fills each group as a paragraph using its indentation
  233. as a fill prefix.
  234. File: emacs,  Node: Case,  Prev: Filling,  Up: Text
  235. Case Conversion Commands
  236. ========================
  237.    Emacs has commands for converting either a single word or any
  238. arbitrary range of text to upper case or to lower case.
  239. `M-l'
  240.      Convert following word to lower case (`downcase-word').
  241. `M-u'
  242.      Convert following word to upper case (`upcase-word').
  243. `M-c'
  244.      Capitalize the following word (`capitalize-word').
  245. `C-x C-l'
  246.      Convert region to lower case (`downcase-region').
  247. `C-x C-u'
  248.      Convert region to upper case (`upcase-region').
  249.    The word conversion commands are the most useful.  `Meta-l'
  250. (`downcase-word') converts the word after point to lower case, moving
  251. past it.  Thus, repeating `Meta-l' converts successive words. `Meta-u'
  252. (`upcase-word') converts to all capitals instead, while `Meta-c'
  253. (`capitalize-word') puts the first letter of the word into upper case
  254. and the rest into lower case.  All these commands convert several words
  255. at once if given an argument.  They are especially convenient for
  256. converting a large amount of text from all upper case to mixed case,
  257. because you can move through the text using `M-l', `M-u' or `M-c' on
  258. each word as appropriate, occasionally using `M-f' instead to skip a
  259. word.
  260.    When given a negative argument, the word case conversion commands
  261. apply to the appropriate number of words before point, but do not move
  262. point. This is convenient when you have just typed a word in the wrong
  263. case: you can give the case conversion command and continue typing.
  264.    If a word case conversion command is given in the middle of a word,
  265. it applies only to the part of the word which follows point.  This is
  266. just like what `Meta-d' (`kill-word') does.  With a negative argument,
  267. case conversion applies only to the part of the word before point.
  268.    The other case conversion commands are `C-x C-u' (`upcase-region')
  269. and `C-x C-l' (`downcase-region'), which convert everything between
  270. point and mark to the specified case.  Point and mark do not move.
  271. File: emacs,  Node: Programs,  Next: Running,  Prev: Text,  Up: Top
  272. Editing Programs
  273. ****************
  274.    Emacs has many commands designed to understand the syntax of
  275. programming languages such as Lisp and C.  These commands can
  276.    * Move over or kill balanced expressions or "sexps" (*note Lists::.).
  277.    * Move over or mark top-level balanced expressions ("defuns", in
  278.      Lisp; functions, in C).
  279.    * Show how parentheses balance (*note Matching::.).
  280.    * Insert, kill or align comments (*note Comments::.).
  281.    * Follow the usual indentation conventions of the language (*note
  282.      Grinding::.).
  283.    The commands for words, sentences and paragraphs are very useful in
  284. editing code even though their canonical application is for editing
  285. human language text.  Most symbols contain words (*note Words::.);
  286. sentences can be found in strings and comments (*note Sentences::.). 
  287. Paragraphs per se are not present in code, but the paragraph commands
  288. are useful anyway, because Lisp mode and C mode define paragraphs to
  289. begin and end at blank lines (*note Paragraphs::.).  Judicious use of
  290. blank lines to make the program clearer will also provide interesting
  291. chunks of text for the paragraph commands to work on.
  292.    The selective display feature is useful for looking at the overall
  293. structure of a function (*note Selective Display::.).  This feature
  294. causes only the lines that are indented less than a specified amount to
  295. appear on the screen.
  296. * Menu:
  297. * Program Modes::       Major modes for editing programs.
  298. * Lists::               Expressions with balanced parentheses.
  299.                          There are editing commands to operate on them.
  300. * Defuns::              Each program is made up of separate functions.
  301.                          There are editing commands to operate on them.
  302. * Grinding::            Adjusting indentation to show the nesting.
  303. * Matching::            Insertion of a close-delimiter flashes matching open.
  304. * Comments::            Inserting, illing and aligning comments.
  305. * Balanced Editing::    Inserting two matching parentheses at once, etc.
  306. * Lisp Completion::     Completion on symbol names in Lisp code.
  307. * Documentation::       Getting documentation of functions you plan to call.
  308. * Change Log::          Maintaining a change history for your program.
  309. * Tags::                Go direct to any function in your program in one
  310.                          command.  Tags remembers which file it is in.
  311. * Fortran::        Fortran mode and its special features.
  312. File: emacs,  Node: Program Modes,  Next: Lists,  Prev: Programs,  Up: Programs
  313. Major Modes for Programming Languages
  314. =====================================
  315.    Emacs also has major modes for the programming languages Lisp,
  316. Scheme (a variant of Lisp), C, Fortran and Muddle.  Ideally, a major
  317. mode should be implemented for each programming language that you might
  318. want to edit with Emacs; but often the mode for one language can serve
  319. for other syntactically similar languages.  The language modes that
  320. exist are those that someone decided to take the trouble to write.
  321.    There are several forms of Lisp mode, which differ in the way they
  322. interface to Lisp execution.  *Note Lisp Modes::.
  323.    Each of the programming language modes defines the TAB key to run an
  324. indentation function that knows the indentation conventions of that
  325. language and updates the current line's indentation accordingly.  For
  326. example, in C mode TAB is bound to `c-indent-line'.  LFD is normally
  327. defined to do RET followed by TAB; thus, it too indents in a
  328. mode-specific fashion.
  329.    In most programming languages, indentation is likely to vary from
  330. line to line.  So the major modes for those languages rebind DEL to
  331. treat a tab as if it were the equivalent number of spaces (using the
  332. command `backward-delete-char-untabify').  This makes it possible to
  333. rub out indentation one column at a time without worrying whether it is
  334. made up of spaces or tabs.  Use `C-b C-d' to delete a tab character
  335. before point, in these modes.
  336.    Programming language modes define paragraphs to be separated only by
  337. blank lines, so that the paragraph commands remain useful.  Auto Fill
  338. mode, if enabled in a programming language major mode, indents the new
  339. lines which it creates.
  340.    Turning on a major mode calls a user-supplied function called the
  341. "mode hook", which is the value of a Lisp variable.  For example,
  342. turning on C mode calls the value of the variable `c-mode-hook' if that
  343. value exists and is non-`nil'.  Mode hook variables for other
  344. programming language modes include `lisp-mode-hook',
  345. `emacs-lisp-mode-hook', `lisp-interaction-mode-hook',
  346. `scheme-mode-hook' and `muddle-mode-hook'.  The mode hook function
  347. receives no arguments.
  348. File: emacs,  Node: Lists,  Next: Defuns,  Prev: Program Modes,  Up: Programs
  349. Lists and Sexps
  350. ===============
  351.    By convention, Emacs keys for dealing with balanced expressions are
  352. usually `Control-Meta-' characters.  They tend to be analogous in
  353. function to their `Control-' and `Meta-' equivalents.  These commands
  354. are usually thought of as pertaining to expressions in programming
  355. languages, but can be useful with any language in which some sort of
  356. parentheses exist (including English).
  357.    These commands fall into two classes.  Some deal only with "lists"
  358. (parenthetical groupings).  They see nothing except parentheses,
  359. brackets, braces (whichever ones must balance in the language you are
  360. working with), and escape characters that might be used to quote those.
  361.    The other commands deal with expressions or "sexps".  The word `sexp'
  362. is derived from "s-expression", the ancient term for an expression in
  363. Lisp.  But in Emacs, the notion of `sexp' is not limited to Lisp.  It
  364. refers to an expression in whatever language your program is written in.
  365. Each programming language has its own major mode, which customizes the
  366. syntax tables so that expressions in that language count as sexps.
  367.    Sexps typically include symbols, numbers, and string constants, as
  368. well as anything contained in parentheses, brackets or braces.
  369.    In languages that use prefix and infix operators, such as C, it is
  370. not possible for all expressions to be sexps.  For example, C mode does
  371. not recognize `foo + bar' as a sexp, even though it is a C expression;
  372. it recognizes `foo' as one sexp and `bar' as another, with the `+' as
  373. punctuation between them.  This is a fundamental ambiguity: both `foo +
  374. bar' and `foo' are legitimate choices for the sexp to move over if
  375. point is at the `f'.  Note that `(foo + bar)' is a sexp in C mode.
  376.    Some languages have obscure forms of syntax for expressions that
  377. nobody has bothered to make Emacs understand properly.
  378. `C-M-f'
  379.      Move forward over a sexp (`forward-sexp').
  380. `C-M-b'
  381.      Move backward over a sexp (`backward-sexp').
  382. `C-M-k'
  383.      Kill sexp forward (`kill-sexp').
  384. `C-M-u'
  385.      Move up and backward in list structure (`backward-up-list').
  386. `C-M-d'
  387.      Move down and forward in list structure (`down-list').
  388. `C-M-n'
  389.      Move forward over a list (`forward-list').
  390. `C-M-p'
  391.      Move backward over a list (`backward-list').
  392. `C-M-t'
  393.      Transpose expressions (`transpose-sexps').
  394. `C-M-@'
  395.      Put mark after following expression (`mark-sexp').
  396.    To move forward over a sexp, use `C-M-f' (`forward-sexp').  If the
  397. first significant character after point is an opening delimiter (`(' in
  398. Lisp; `(', `[' or `{' in C), `C-M-f' moves past the matching closing
  399. delimiter.  If the character begins a symbol, string, or number,
  400. `C-M-f' moves over that.  If the character after point is a closing
  401. delimiter, `C-M-f' just moves past it.  (This last is not really moving
  402. across a sexp; it is an exception which is included in the definition
  403. of `C-M-f' because it is as useful a behavior as anyone can think of
  404. for that situation.)
  405.    The command `C-M-b' (`backward-sexp') moves backward over a sexp. 
  406. The detailed rules are like those above for `C-M-f', but with
  407. directions reversed.  If there are any prefix characters (singlequote,
  408. backquote and comma, in Lisp) preceding the sexp, `C-M-b' moves back
  409. over them as well.
  410.    `C-M-f' or `C-M-b' with an argument repeats that operation the
  411. specified number of times; with a negative argument, it moves in the
  412. opposite direction.
  413.    The sexp commands move across comments as if they were whitespace, in
  414. languages such as C where the comment-terminator can be recognized.  In
  415. Lisp, and other languages where comments run until the end of a line,
  416. it is very difficult to ignore comments when parsing backwards;
  417. therefore, in such languages the sexp commands treat the text of
  418. comments as if it were code.
  419.    Killing a sexp at a time can be done with `C-M-k' (`kill-sexp').
  420. `C-M-k' kills the characters that `C-M-f' would move over.
  421.    The "list commands" move over lists like the sexp commands but skip
  422. blithely over any number of other kinds of sexps (symbols, strings,
  423. etc). They are `C-M-n' (`forward-list') and `C-M-p' (`backward-list'). 
  424. The main reason they are useful is that they usually ignore comments
  425. (since the comments usually do not contain any lists).
  426.    `C-M-n' and `C-M-p' stay at the same level in parentheses, when
  427. that's possible.  To move up one (or N) levels, use `C-M-u'
  428. (`backward-up-list'). `C-M-u' moves backward up past one unmatched
  429. opening delimiter.  A positive argument serves as a repeat count; a
  430. negative argument reverses direction of motion and also requests
  431. repetition, so it moves forward and up one or more levels.
  432.    To move down in list structure, use `C-M-d' (`down-list').  In Lisp
  433. mode, where `(' is the only opening delimiter, this is nearly the same
  434. as searching for a `('.  An argument specifies the number of levels of
  435. parentheses to go down.
  436.    A somewhat random-sounding command which is nevertheless easy to use
  437. is `C-M-t' (`transpose-sexps'), which drags the previous sexp across
  438. the next one.  An argument serves as a repeat count, and a negative
  439. argument drags backwards (thus canceling out the effect of `C-M-t' with
  440. a positive argument).  An argument of zero, rather than doing nothing,
  441. transposes the sexps ending after point and the mark.
  442.    To make the region be the next sexp in the buffer, use `C-M-@'
  443. (`mark-sexp') which sets mark at the same place that `C-M-f' would move
  444. to.  `C-M-@' takes arguments like `C-M-f'.  In particular, a negative
  445. argument is useful for putting the mark at the beginning of the
  446. previous sexp.
  447.    The list and sexp commands' understanding of syntax is completely
  448. controlled by the syntax table.  Any character can, for example, be
  449. declared to be an opening delimiter and act like an open parenthesis.
  450. *Note Syntax::.
  451. File: emacs,  Node: Defuns,  Next: Grinding,  Prev: Lists,  Up: Programs
  452. Defuns
  453. ======
  454.    In Emacs, a parenthetical grouping at the top level in the buffer is
  455. called a "defun".  The name derives from the fact that most top-level
  456. lists in a Lisp file are instances of the special form `defun', but any
  457. top-level parenthetical grouping counts as a defun in Emacs parlance
  458. regardless of what its contents are, and regardless of the programming
  459. language in use.  For example, in C, the body of a function definition
  460. is a defun.
  461. `C-M-a'
  462.      Move to beginning of current or preceding defun
  463.      (`beginning-of-defun').
  464. `C-M-e'
  465.      Move to end of current or following defun (`end-of-defun').
  466. `C-M-h'
  467.      Put region around whole current or following defun (`mark-defun').
  468.    The commands to move to the beginning and end of the current defun
  469. are `C-M-a' (`beginning-of-defun') and `C-M-e' (`end-of-defun').
  470.    If you wish to operate on the current defun, use `C-M-h'
  471. (`mark-defun') which puts point at the beginning and mark at the end of
  472. the current or next defun.  For example, this is the easiest way to get
  473. ready to move the defun to a different place in the text.  In C mode,
  474. `C-M-h' runs the function `mark-c-function', which is almost the same
  475. as `mark-defun'; the difference is that it backs up over the argument
  476. declarations, function name and returned data type so that the entire C
  477. function is inside the region.
  478.    Emacs assumes that any open-parenthesis found in the leftmost column
  479. is the start of a defun.  Therefore, never put an open-parenthesis at
  480. the left margin in a Lisp file unless it is the start of a top level
  481. list. Never put an open-brace or other opening delimiter at the
  482. beginning of a line of C code unless it starts the body of a function. 
  483. The most likely problem case is when you want an opening delimiter at
  484. the start of a line inside a string.  To avoid trouble, put an escape
  485. character (`\', in C and Emacs Lisp, `/' in some other Lisp dialects)
  486. before the opening delimiter.  It will not affect the contents of the
  487. string.
  488.    In the remotest past, the original Emacs found defuns by moving
  489. upward a level of parentheses until there were no more levels to go up.
  490.  This always required scanning all the way back to the beginning of the
  491. buffer, even for a small function.  To speed up the operation, Emacs
  492. was changed to assume that any `(' (or other character assigned the
  493. syntactic class of opening-delimiter) at the left margin is the start
  494. of a defun.  This heuristic was nearly always right and avoided the
  495. costly scan; however, it mandated the convention described above.
  496. File: emacs,  Node: Grinding,  Next: Matching,  Prev: Defuns,  Up: Programs
  497. Indentation for Programs
  498. ========================
  499.    The best way to keep a program properly indented ("ground") is to use
  500. Emacs to re-indent it as you change it.  Emacs has commands to indent
  501. properly either a single line, a specified number of lines, or all of
  502. the lines inside a single parenthetical grouping.
  503. * Menu:
  504. * Basic Indent::
  505. * Multi-line Indent::   Commands to reindent many lines at once.
  506. * Lisp Indent::        Specifying how each Lisp function should be indented.
  507. * C Indent::        Choosing an indentation style for C code.
  508. File: emacs,  Node: Basic Indent,  Next: Multi-line Indent,  Prev: Grinding,  Up: Grinding
  509. Basic Program Indentation Commands
  510. ----------------------------------
  511. `TAB'
  512.      Adjust indentation of current line.
  513. `LFD'
  514.      Equivalent to RET followed by TAB (`newline-and-indent').
  515.    The basic indentation command is TAB, which gives the current line
  516. the correct indentation as determined from the previous lines.  The
  517. function that TAB runs depends on the major mode; it is
  518. `lisp-indent-line' in Lisp mode, `c-indent-line' in C mode, etc.  These
  519. functions understand different syntaxes for different languages, but
  520. they all do about the same thing.  TAB in any programming language
  521. major mode inserts or deletes whitespace at the beginning of the
  522. current line, independent of where point is in the line.  If point is
  523. inside the whitespace at the beginning of the line, TAB leaves it at
  524. the end of that whitespace; otherwise, TAB leaves point fixed with
  525. respect to the characters around it.
  526.    Use `C-q TAB' to insert a tab at point.
  527.    When entering a large amount of new code, use LFD
  528. (`newline-and-indent'), which is equivalent to a RET followed by a TAB.
  529.  LFD creates a blank line, and then gives it the appropriate
  530. indentation.
  531.    TAB indents the second and following lines of the body of an
  532. parenthetical grouping each under the preceding one; therefore, if you
  533. alter one line's indentation to be nonstandard, the lines below will
  534. tend to follow it.  This is the right behavior in cases where the
  535. standard result of TAB is unaesthetic.
  536.    Remember that an open-parenthesis, open-brace or other opening
  537. delimiter at the left margin is assumed by Emacs (including the
  538. indentation routines) to be the start of a function.  Therefore, you
  539. must never have an opening delimiter in column zero that is not the
  540. beginning of a function, not even inside a string.  This restriction is
  541. vital for making the indentation commands fast; you must simply accept
  542. it.  *Note Defuns::, for more information on this.
  543. File: emacs,  Node: Multi-line Indent,  Next: Lisp Indent,  Prev: Basic Indent,  Up: Grinding
  544. Indenting Several Lines
  545. -----------------------
  546.    When you wish to re-indent several lines of code which have been
  547. altered or moved to a different level in the list structure, you have
  548. several commands available.
  549. `C-M-q'
  550.      Re-indent all the lines within one list (`indent-sexp').
  551. `C-u TAB'
  552.      Shift an entire list rigidly sideways so that its first line is
  553.      properly indented.
  554. `C-M-\'
  555.      Re-indent all lines in the region (`indent-region').
  556.    You can re-indent the contents of a single list by positioning point
  557. before the beginning of it and typing `C-M-q' (`indent-sexp' in Lisp
  558. mode, `indent-c-exp' in C mode; also bound to other suitable functions
  559. in other modes).  The indentation of the line the sexp starts on is not
  560. changed; therefore, only the relative indentation within the list, and
  561. not its position, is changed.  To correct the position as well, type a
  562. TAB before the `C-M-q'.
  563.    If the relative indentation within a list is correct but the
  564. indentation of its beginning is not, go to the line the list begins on
  565. and type `C-u TAB'.  When TAB is given a numeric argument, it moves all
  566. the lines in the grouping starting on the current line sideways the
  567. same amount that the current line moves.  It is clever, though, and
  568. does not move lines that start inside strings, or C preprocessor lines
  569. when in C mode.
  570.    Another way to specify the range to be re-indented is with point and
  571. mark.  The command `C-M-\' (`indent-region') applies TAB to every line
  572. whose first character is between point and mark.
  573. File: emacs,  Node: Lisp Indent,  Next: C Indent,  Prev: Multi-line Indent,  Up: Grinding
  574. Customizing Lisp Indentation
  575. ----------------------------
  576.    The indentation pattern for a Lisp expression can depend on the
  577. function called by the expression.  For each Lisp function, you can
  578. choose among several predefined patterns of indentation, or define an
  579. arbitrary one with a Lisp program.
  580.    The standard pattern of indentation is as follows: the second line
  581. of the expression is indented under the first argument, if that is on
  582. the same line as the beginning of the expression; otherwise, the second
  583. line is indented underneath the function name.  Each following line is
  584. indented under the previous line whose nesting depth is the same.
  585.    If the variable `lisp-indent-offset' is non-`nil', it overrides the
  586. usual indentation pattern for the second line of an expression, so that
  587. such lines are always indented `lisp-indent-offset' more columns than
  588. the containing list.
  589.    The standard pattern is overridded for certain functions.  Functions
  590. whose names start with `def' always indent the second line by
  591. `lisp-body-indention' extra columns beyond the open-parenthesis
  592. starting the expression.
  593.    The standard pattern can be overridden in various ways for individual
  594. functions, according to the `lisp-indent-hook' property of the function
  595. name.  There are four possibilities for this property:
  596. `nil'
  597.      This is the same as no property; the standard indentation pattern
  598.      is used.
  599. `defun'
  600.      The pattern used for function names that start with `def' is used
  601.      for this function also.
  602. a number, NUMBER
  603.      The first NUMBER arguments of the function are "distinguished"
  604.      arguments; the rest are considered the "body" of the expression. 
  605.      A line in the expression is indented according to whether the
  606.      first argument on it is distinguished or not.  If the argument is
  607.      part of the body, the line is indented `lisp-body-indent' more
  608.      columns than the open-parenthesis starting the containing
  609.      expression.  If the argument is distinguished and is either the
  610.      first or second argument, it is indented twice that many extra
  611.      columns. If the argument is distinguished and not the first or
  612.      second argument, the standard pattern is followed for that line.
  613. a symbol, SYMBOL
  614.      SYMBOL should be a function name; that function is called to
  615.      calculate the indentation of a line within this expression.  The
  616.      function receives two arguments:
  617.     STATE
  618.           The value returned by `parse-partial-sexp' (a Lisp primitive
  619.           for indentation and nesting computation) when it parses up to
  620.           the beginning of this line.
  621.     POS
  622.           The position at which the line being indented begins.
  623.      It should return either a number, which is the number of columns of
  624.      indentation for that line, or a list whose car is such a number. 
  625.      The difference between returning a number and returning a list is
  626.      that a number says that all following lines at the same nesting
  627.      level should be indented just like this one; a list says that
  628.      following lines might call for different indentations.  This makes
  629.      a difference when the indentation is being computed by `C-M-q'; if
  630.      the value is a number, `C-M-q' need not recalculate indentation
  631.      for the following lines until the end of the list.
  632. File: emacs,  Node: C Indent,  Prev: Lisp Indent,  Up: Grinding
  633. Customizing C Indentation
  634. -------------------------
  635.    Two variables control which commands perform C indentation and when.
  636.    If `c-auto-newline' is non-`nil', newlines are inserted both before
  637. and after braces that you insert, and after colons and semicolons.
  638. Correct C indentation is done on all the lines that are made this way.
  639.    If `c-tab-always-indent' is non-`nil', the TAB command in C mode
  640. does indentation only if point is at the left margin or within the
  641. line's indentation.  If there is non-whitespace to the left of point,
  642. then TAB just inserts a tab character in the buffer.  Normally, this
  643. variable is `nil', and TAB always reindents the current line.
  644.    C does not have anything analogous to particular function names for
  645. which special forms of indentation are desirable.  However, it has a
  646. different need for customization facilities: many different styles of C
  647. indentation are in common use.
  648.    There are six variables you can set to control the style that Emacs C
  649. mode will use.
  650. `c-indent-level'
  651.      Indentation of C statements within surrounding block.  The
  652.      surrounding block's indentation is the indentation of the line on
  653.      which the open-brace appears.
  654. `c-continued-statement-offset'
  655.      Extra indentation given to a substatement, such as the then-clause
  656.      of an if or body of a while.
  657. `c-brace-offset'
  658.      Extra indentation for line if it starts with an open brace.
  659. `c-brace-imaginary-offset'
  660.      An open brace following other text is treated as if it were this
  661.      far to the right of the start of its line.
  662. `c-argdecl-indent'
  663.      Indentation level of declarations of C function arguments.
  664. `c-label-offset'
  665.      Extra indentation for line that is a label, or case or default.
  666.    The variable `c-indent-level' controls the indentation for C
  667. statements with respect to the surrounding block.  In the example
  668.          {
  669.            foo ();
  670. the difference in indentation between the lines is `c-indent-level'.
  671. Its standard value is 2.
  672.    If the open-brace beginning the compound statement is not at the
  673. beginning of its line, the `c-indent-level' is added to the indentation
  674. of the line, not the column of the open-brace.  For example,
  675.      if (losing) {
  676.        do_this ();
  677. One popular indentation style is that which results from setting
  678. `c-indent-level' to 8 and putting open-braces at the end of a line in
  679. this way.  I prefer to put the open-brace on a separate line.
  680.    In fact, the value of the variable `c-brace-imaginary-offset' is
  681. also added to the indentation of such a statement.  Normally this
  682. variable is zero.  Think of this variable as the imaginary position of
  683. the open brace, relative to the first nonblank character on the line. 
  684. By setting this variable to 4 and `c-indent-level' to 0, you can get
  685. this style:
  686.      if (x == y) {
  687.          do_it ();
  688.          }
  689.    When `c-indent-level' is zero, the statements inside most braces
  690. will line up right under the open brace.  But there is an exception made
  691. for braces in column zero, such as surrounding a function's body.  The
  692. statements just inside it do not go at column zero.  Instead,
  693. `c-brace-offset' and `c-continued-statement-offset' (see below) are
  694. added to produce a typical offset between brace levels, and the
  695. statements are indented that far.
  696.    `c-continued-statement-offset' controls the extra indentation for a
  697. line that starts within a statement (but not within parentheses or
  698. brackets).  These lines are usually statements that are within other
  699. statements, such as the then-clauses of `if' statements and the bodies
  700. of `while' statements.  This parameter is the difference in indentation
  701. between the two lines in
  702.      if (x == y)
  703.        do_it ();
  704. Its standard value is 2.  Some popular indentation styles correspond to
  705. a value of zero for `c-continued-statement-offset'.
  706.    `c-brace-offset' is the extra indentation given to a line that
  707. starts with an open-brace.  Its standard value is zero; compare
  708.      if (x == y)
  709.        {
  710.      if (x == y)
  711.        do_it ();
  712. if `c-brace-offset' were set to 4, the first example would become
  713.      if (x == y)
  714.            {
  715.    `c-argdecl-indent' controls the indentation of declarations of the
  716. arguments of a C function.  It is absolute: argument declarations
  717. receive exactly `c-argdecl-indent' spaces.  The standard value is 5,
  718. resulting in code like this:
  719.      char *
  720.      index (string, char)
  721.           char *string;
  722.           int char;
  723.    `c-label-offset' is the extra indentation given to a line that
  724. contains a label, a case statement, or a `default:' statement.  Its
  725. standard value is -2, resulting in code like this
  726.      switch (c)
  727.        {
  728.        case 'x':
  729. If `c-label-offset' were zero, the same code would be indented as
  730.      switch (c)
  731.        {
  732.          case 'x':
  733. This example assumes that the other variables above also have their
  734. standard values.
  735.    I strongly recommend that you try out the indentation style produced
  736. by the standard settings of these variables, together with putting open
  737. braces on separate lines.  You can see how it looks in all the C source
  738. files of GNU Emacs.
  739. File: emacs,  Node: Matching,  Next: Comments,  Prev: Grinding,  Up: Programs
  740. Automatic Display Of Matching Parentheses
  741. =========================================
  742.    The Emacs parenthesis-matching feature is designed to show
  743. automatically how parentheses match in the text.  Whenever a
  744. self-inserting character that is a closing delimiter is typed, the
  745. cursor moves momentarily to the location of the matching opening
  746. delimiter, provided that is on the screen. If it is not on the screen,
  747. some text starting with that opening delimiter is displayed in the echo
  748. area.  Either way, you can tell what grouping is being closed off.
  749.    In Lisp, automatic matching applies only to parentheses.  In C, it
  750. applies to braces and brackets too.  Emacs knows which characters to
  751. regard as matching delimiters based on the syntax table, which is set
  752. by the major mode.  *Note Syntax::.
  753.    If the opening delimiter and closing delimiter are mismatched--such
  754. as in `[x)'--a warning message is displayed in the echo area.  The
  755. correct matches are specified in the syntax table.
  756.    Two variables control parenthesis match display. 
  757. `blink-matching-paren' turns the feature on or off; `nil' turns it off,
  758. but the default is `t' to turn match display on. 
  759. `blink-matching-paren-distance' specifies how many characters back to
  760. search to find the matching opening delimiter.  If the match is not
  761. found in that far, scanning stops, and nothing is displayed.  This is
  762. to prevent scanning for the matching delimiter from wasting lots of
  763. time when there is no match.  The default is 4000.
  764. File: emacs,  Node: Comments,  Next: Balanced Editing,  Prev: Matching,  Up: Programs
  765. Manipulating Comments
  766. =====================
  767.    The comment commands insert, kill and align comments.
  768. `M-;'
  769.      Insert or align comment (`indent-for-comment').
  770. `C-x ;'
  771.      Set comment column (`set-comment-column').
  772. `C-u - C-x ;'
  773.      Kill comment on current line (`kill-comment').
  774. `M-LFD'
  775.      Like RET followed by inserting and aligning a comment
  776.      (`indent-new-comment-line').
  777.    The command that creates a comment is `Meta-;'
  778. (`indent-for-comment'). If there is no comment already on the line, a
  779. new comment is created, aligned at a specific column called the
  780. "comment column".  The comment is created by inserting the string Emacs
  781. thinks comments should start with (the value of `comment-start'; see
  782. below).  Point is left after that string.  If the text of the line
  783. extends past the comment column, then the indentation is done to a
  784. suitable boundary (usually, at least one space is inserted).  If the
  785. major mode has specified a string to terminate comments, that is
  786. inserted after point, to keep the syntax valid.
  787.    `Meta-;' can also be used to align an existing comment.  If a line
  788. already contains the string that starts comments, then `M-;' just moves
  789. point after it and re-indents it to the conventional place.  Exception:
  790. comments starting in column 0 are not moved.
  791.    Some major modes have special rules for indenting certain kinds of
  792. comments in certain contexts.  For example, in Lisp code, comments which
  793. start with two semicolons are indented as if they were lines of code,
  794. instead of at the comment column.  Comments which start with three
  795. semicolons are supposed to start at the left margin.  Emacs understands
  796. these conventions by indenting a double-semicolon comment using TAB,
  797. and by not changing the indentation of a triple-semicolon comment at
  798.      ;; This function is just an example
  799.      ;;; Here either two or three semicolons are appropriate.
  800.      (defun foo (x)
  801.      ;;; And now, the first part of the function:
  802.        ;; The following line adds one.
  803.        (1+ x))           ; This line adds one.
  804.    In C code, a comment preceded on its line by nothing but whitespace
  805. is indented like a line of code.
  806.    Even when an existing comment is properly aligned, `M-;' is still
  807. useful for moving directly to the start of the comment.
  808.    `C-u - C-x ;' (`kill-comment') kills the comment on the current line,
  809. if there is one.  The indentation before the start of the comment is
  810. killed as well.  If there does not appear to be a comment in the line,
  811. nothing is done.  To reinsert the comment on another line, move to the
  812. end of that line, do `C-y', and then do `M-;' to realign it.  Note that
  813. `C-u - C-x ;' is not a distinct key; it is `C-x ;'
  814. (`set-comment-column') with a negative argument.  That command is
  815. programmed so that when it receives a negative argument it calls
  816. `kill-comment'.  However, `kill-comment' is a valid command which you
  817. could bind directly to a key if you wanted to.
  818. Multiple Lines of Comments
  819. --------------------------
  820.    If you are typing a comment and find that you wish to continue it on
  821. another line, you can use the command `Meta-LFD'
  822. (`indent-new-comment-line'), which terminates the comment you are
  823. typing, creates a new blank line afterward, and begins a new comment
  824. indented under the old one.  When Auto Fill mode is on, going past the
  825. fill column while typing a comment causes the comment to be continued
  826. in just this fashion.  If point is not at the end of the line when
  827. `M-LFD' is typed, the text on the rest of the line becomes part of the
  828. new comment line.
  829. Options Controlling Comments
  830. ----------------------------
  831.    The comment column is stored in the variable `comment-column'.  You
  832. can set it to a number explicitly.  Alternatively, the command `C-x ;'
  833. (`set-comment-column') sets the comment column to the column point is
  834. at.  `C-u C-x ;' sets the comment column to match the last comment
  835. before point in the buffer, and then does a `Meta-;' to align the
  836. current line's comment under the previous one.  Note that `C-u - C-x ;'
  837. runs the function `kill-comment' as described above.
  838.    `comment-column' is a per-buffer variable; altering the variable
  839. affects only the current buffer, but there is a default value which you
  840. can change as well.  *Note Locals::.  Many major modes initialize this
  841. variable for the current buffer.
  842.    The comment commands recognize comments based on the regular
  843. expression that is the value of the variable `comment-start-skip'. 
  844. This regexp should not match the null string.  It may match more than
  845. the comment starting delimiter in the strictest sense of the word; for
  846. example, in C mode the value of the variable is `"/\\*+ *"', which
  847. matches extra stars and spaces after the `/*' itself.  (Note that `\\'
  848. is needed in Lisp syntax to include a `\' in the string, which is needed
  849. to deny the first star its special meaning in regexp syntax.  *Note
  850. Regexps::.)
  851.    When a comment command makes a new comment, it inserts the value of
  852. `comment-start' to begin it.  The value of `comment-end' is inserted
  853. after point, so that it will follow the text that you will insert into
  854. the comment.  In C mode, `comment-start' has the value `"/* "' and
  855. `comment-end' has the value `" */"'.
  856.    `comment-multi-line' controls how `M-LFD' (`indent-new-comment-line')
  857. behaves when used inside a comment.  If `comment-multi-line' is `nil',
  858. as it normally is, then the comment on the starting line is terminated
  859. and a new comment is started on the new following line.  If
  860. `comment-multi-line' is not `nil', then the new following line is set
  861. up as part of the same comment that was found on the starting line.
  862. This is done by not inserting a terminator on the old line, and not
  863. inserting a starter on the new line.  In languages where multi-line
  864. comments work, the choice of value for this variable is a matter of
  865. taste.
  866.    The variable `comment-indent-hook' should contain a function that
  867. will be called to compute the indentation for a newly inserted comment
  868. or for aligning an existing comment.  It is set differently by various
  869. major modes.  The function is called with no arguments, but with point
  870. at the beginning of the comment, or at the end of a line if a new
  871. comment is to be inserted.  It should return the column in which the
  872. comment ought to start. For example, in Lisp mode, the indent hook
  873. function bases its decision on how many semicolons begin an existing
  874. comment, and on the code in the preceding lines.
  875. File: emacs,  Node: Balanced Editing,  Next: Lisp Completion,  Prev: Comments,  Up: Programs
  876. Editing Without Unbalanced Parentheses
  877. ======================================
  878. `M-('
  879.      Put parentheses around next sexp(s) (`insert-parentheses').
  880. `M-)'
  881.      Move past next close parenthesis and re-indent
  882.      (`move-over-close-and-reindent').
  883.    The commands `M-(' (`insert-parentheses') and `M-)'
  884. (`move-over-close-and-reindent') are designed to facilitate a style of
  885. editing which keeps parentheses balanced at all times.  `M-(' inserts a
  886. pair of parentheses, either together as in `()', or, if given an
  887. argument, around the next several sexps, and leaves point after the open
  888. parenthesis.  Instead of typing `( F O O )', you can type `M-( F O O',
  889. which has the same effect except for leaving the cursor before the
  890. close parenthesis.  Then you would type `M-)', which moves past the
  891. close parenthesis, deleting any indentation preceding it (in this
  892. example there is none), and indenting with LFD after it.
  893.