home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / usr / lib / emacs / info / emacs-7 (.txt) < prev    next >
GNU Info File  |  1992-10-30  |  48KB  |  860 lines

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