programs.texi 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066
  1. @node Programs, Running, Text, Top
  2. @chapter Editing Programs
  3. @cindex Lisp
  4. @cindex C
  5. Emacs has many commands designed to understand the syntax of programming
  6. languages such as Lisp and C. These commands can:
  7. @itemize @bullet
  8. @item
  9. Move over or kill balanced expressions or @dfn{sexps} (@pxref{Lists}).
  10. @item
  11. Move over or mark top-level balanced expressions (@dfn{defuns}, in Lisp;
  12. functions, in C).
  13. @item
  14. Show how parentheses balance (@pxref{Matching}).
  15. @item
  16. Insert, kill, or align comments (@pxref{Comments}).
  17. @item
  18. Follow the usual indentation conventions of the language
  19. (@pxref{Grinding}).
  20. @end itemize
  21. The commands available for words, sentences, and paragraphs are useful in
  22. editing code even though their canonical application is for editing human
  23. language text. Most symbols contain words (@pxref{Words}); sentences can
  24. be found in strings and comments (@pxref{Sentences}). Paragraphs per se
  25. are not present in code, but the paragraph commands are useful anyway,
  26. because Lisp mode and C mode define paragraphs to begin and end at blank
  27. lines (@pxref{Paragraphs}). Judicious use of blank lines to make the
  28. program clearer also provides interesting chunks of text for the
  29. paragraph commands to work on.
  30. The selective display feature is useful for looking at the overall
  31. structure of a function (@pxref{Selective Display}). This feature causes
  32. only the lines that are indented less than a specified amount to appear
  33. on the screen.
  34. @menu
  35. * Program Modes:: Major modes for editing programs.
  36. * Lists:: Expressions with balanced parentheses.
  37. There are editing commands to operate on them.
  38. * Defuns:: Each program is made up of separate functions.
  39. There are editing commands to operate on them.
  40. * Grinding:: Adjusting indentation to show the nesting.
  41. * Matching:: Insertion of a close-delimiter flashes matching open.
  42. * Comments:: Inserting, filling and aligning comments.
  43. * Balanced Editing:: Inserting two matching parentheses at once, etc.
  44. * Lisp Completion:: Completion on symbol names in Lisp code.
  45. * Documentation:: Getting documentation of functions you plan to call.
  46. * Change Log:: Maintaining a change history for your program.
  47. * Tags:: Go direct to any function in your program in one
  48. command. Tags remembers which file it is in.
  49. * Fortran:: Fortran mode and its special features.
  50. * Asm Mode:: Asm mode and its special features.
  51. @end menu
  52. @node Program Modes, Lists, Programs, Programs
  53. @section Major Modes for Programming Languages
  54. @cindex Lisp mode
  55. @cindex C mode
  56. @cindex Scheme mode
  57. Emacs has several major modes for the programming languages Lisp, Scheme (a
  58. variant of Lisp), C, Fortran, and Muddle. Ideally, a major mode should be
  59. implemented for each programming language you might want to edit with
  60. Emacs; but often the mode for one language can serve for other
  61. syntactically similar languages. The language modes that exist are those
  62. that someone decided to take the trouble to write.
  63. There are several variants of Lisp mode, which differ in the way they
  64. interface to Lisp execution. @xref{Lisp Modes}.
  65. Each of the programming language modes defines the @key{TAB} key to run
  66. an indentation function that knows the indentation conventions of that
  67. language and updates the current line's indentation accordingly. For
  68. example, in C mode @key{TAB} is bound to @code{c-indent-line}. @key{LFD}
  69. is normally defined to do @key{RET} followed by @key{TAB}; thus it, too,
  70. indents in a mode-specific fashion.
  71. @kindex DEL
  72. @findex backward-delete-char-untabify
  73. In most programming languages, indentation is likely to vary from line to
  74. line. So the major modes for those languages rebind @key{DEL} to treat a
  75. tab as if it were the equivalent number of spaces (using the command
  76. @code{backward-delete-char-untabify}). This makes it possible to rub out
  77. indentation one column at a time without worrying whether it is made up of
  78. spaces or tabs. In these modes, use @kbd{C-b C-d} to delete a tab
  79. character before point.
  80. Programming language modes define paragraphs to be separated only by
  81. blank lines, so that the paragraph commands remain useful. Auto Fill mode,
  82. if enabled in a programming language major mode, indents the new lines
  83. which it creates.
  84. @cindex mode hook
  85. @vindex c-mode-hook
  86. @vindex lisp-mode-hook
  87. @vindex emacs-lisp-mode-hook
  88. @vindex lisp-interaction-mode-hook
  89. @vindex scheme-mode-hook
  90. @vindex muddle-mode-hook
  91. Turning on a major mode calls a user-supplied function called the
  92. @dfn{mode hook}, which is the value of a Lisp variable. For example,
  93. turning on C mode calls the value of the variable @code{c-mode-hook} if
  94. that value exists and is non-@code{nil}. Mode hook variables for other
  95. programming language modes include @code{lisp-mode-hook},
  96. @code{emacs-lisp-mode-hook}, @code{lisp-interaction-mode-hook},
  97. @code{scheme-mode-hook}, and @code{muddle-mode-hook}. The mode hook
  98. function receives no arguments.@refill
  99. @node Lists, Defuns, Program Modes, Programs
  100. @section Lists and Sexps
  101. @cindex Control-Meta
  102. By convention, Emacs keys for dealing with balanced expressions are
  103. usually @kbd{Control-Meta-} characters. They tend to be analogous in
  104. function to their @kbd{Control-} and @kbd{Meta-} equivalents. These commands
  105. are usually thought of as pertaining to expressions in programming
  106. languages, but can be useful with any language in which some sort of
  107. parentheses exist (including English).
  108. @cindex list
  109. @cindex sexp
  110. @cindex expression
  111. The commands fall into two classes. Some commands deal only with
  112. @dfn{lists} (parenthetical groupings). They see nothing except
  113. parentheses, brackets, braces (depending on what must balance in the
  114. language you are working with), and escape characters that might be used
  115. to quote those.
  116. The other commands deal with expressions or @dfn{sexps}. The word `sexp'
  117. is derived from @dfn{s-expression}, the term for a symbolic expression in
  118. Lisp. In Emacs, the notion of `sexp' is not limited to Lisp. It
  119. refers to an expression in the language your program is written in.
  120. Each programming language has its own major mode, which customizes the
  121. syntax tables so that expressions in that language count as sexps.
  122. Sexps typically include symbols, numbers, and string constants, as well
  123. as anything contained in parentheses, brackets, or braces.
  124. In languages that use prefix and infix operators, such as C, it is not
  125. possible for all expressions to be sexps. For example, C mode does not
  126. recognize @samp{foo + bar} as an sexp, even though it @i{is} a C expression;
  127. it recognizes @samp{foo} as one sexp and @samp{bar} as another, with the
  128. @samp{+} as punctuation between them. This is a fundamental ambiguity:
  129. both @samp{foo + bar} and @samp{foo} are legitimate choices for the sexp to
  130. move over if point is at the @samp{f}. Note that @samp{(foo + bar)} is a
  131. sexp in C mode.
  132. Some languages have obscure forms of syntax for expressions that nobody
  133. has bothered to make Emacs understand properly.
  134. @c doublewidecommands
  135. @table @kbd
  136. @item C-M-f
  137. Move forward over an sexp (@code{forward-sexp}).
  138. @item C-M-b
  139. Move backward over an sexp (@code{backward-sexp}).
  140. @item C-M-k
  141. Kill sexp forward (@code{kill-sexp}).
  142. @item C-M-u
  143. Move up and backward in list structure (@code{backward-up-list}).
  144. @item C-M-d
  145. Move down and forward in list structure (@code{down-list}).
  146. @item C-M-n
  147. Move forward over a list (@code{forward-list}).
  148. @item C-M-p
  149. Move backward over a list (@code{backward-list}).
  150. @item C-M-t
  151. Transpose expressions (@code{transpose-sexps}).
  152. @item C-M-@@
  153. Put mark after following expression (@code{mark-sexp}).
  154. @end table
  155. @kindex C-M-f
  156. @kindex C-M-b
  157. @findex forward-sexp
  158. @findex backward-sexp
  159. To move forward over an sexp, use @kbd{C-M-f} (@code{forward-sexp}). If
  160. the first significant character after point is an opening delimiter
  161. (@samp{(} in Lisp; @samp{(}, @samp{[}, or @samp{@{} in C), @kbd{C-M-f}
  162. moves past the matching closing delimiter. If the character begins a
  163. symbol, string, or number, @kbd{C-M-f} moves over that. If the character
  164. after point is a closing delimiter, @kbd{C-M-f} just moves past it. (This
  165. last is not really moving across an sexp; it is an exception which is
  166. included in the definition of @kbd{C-M-f} because it is as useful a
  167. behavior as anyone can think of for that situation.)@refill
  168. The command @kbd{C-M-b} (@code{backward-sexp}) moves backward over a
  169. sexp. The detailed rules are like those above for @kbd{C-M-f}, but with
  170. directions reversed. If there are any prefix characters (single quote,
  171. back quote, and comma, in Lisp) preceding the sexp, @kbd{C-M-b} moves back
  172. over them as well.
  173. @kbd{C-M-f} or @kbd{C-M-b} with an argument repeats that operation the
  174. specified number of times; with a negative argument, it moves in the
  175. opposite direction.
  176. In languages such as C where the comment-terminator can be recognized,
  177. the sexp commands move across comments as if they were whitespace. In
  178. Lisp and other languages where comments run until the end of a line, it
  179. is very difficult to ignore comments when parsing backwards; therefore,
  180. in such languages the sexp commands treat the text of comments as if it
  181. were code.
  182. @kindex C-M-k
  183. @findex kill-sexp
  184. Killing an sexp at a time can be done with @kbd{C-M-k} (@code{kill-sexp}).
  185. @kbd{C-M-k} kills the characters that @kbd{C-M-f} would move over.
  186. @kindex C-M-n
  187. @kindex C-M-p
  188. @findex forward-list
  189. @findex backward-list
  190. The @dfn{list commands}, @kbd{C-M-n} (@code{forward-list}) and
  191. @kbd{C-M-p} (@code{backward-list}), move over lists like the sexp
  192. commands but skip over any number of other kinds of sexps (symbols,
  193. strings, etc). In some situations, these commands are useful because
  194. they usually ignore comments, since the comments usually do not contain
  195. any lists.@refill
  196. @kindex C-M-u
  197. @kindex C-M-d
  198. @findex backward-up-list
  199. @findex down-list
  200. @kbd{C-M-n} and @kbd{C-M-p} stay at the same level in parentheses, when
  201. that is possible. To move @i{up} one (or @var{n}) levels, use @kbd{C-M-u}
  202. (@code{backward-up-list}).
  203. @kbd{C-M-u} moves backward up past one unmatched opening delimiter. A
  204. positive argument serves as a repeat count; a negative argument reverses
  205. direction of motion and also requests repetition, so it moves forward and
  206. up one or more levels.@refill
  207. To move @i{down} in list structure, use @kbd{C-M-d}
  208. (@code{down-list}). In Lisp mode, where @samp{(} is the only opening
  209. delimiter, this is nearly the same as searching for a @samp{(}. An
  210. argument specifies the number of levels of parentheses to go down.
  211. @cindex transposition
  212. @kindex C-M-t
  213. @findex transpose-sexps
  214. @kbd{C-M-t} (@code{transpose-sexps}) drags the previous sexp across
  215. the next one. An argument serves as a repeat count, and a negative
  216. argument drags backwards (thus canceling out the effect of @kbd{C-M-t} with
  217. a positive argument). An argument of zero, rather than doing nothing,
  218. transposes the sexps ending after point and the mark.
  219. @kindex C-M-@@
  220. @findex mark-sexp
  221. To make the region be the next sexp in the buffer, use @kbd{C-M-@@}
  222. (@code{mark-sexp}) which sets the mark at the same place that
  223. @kbd{C-M-f} would move to. @kbd{C-M-@@} takes arguments like
  224. @kbd{C-M-f}. In particular, a negative argument is useful for putting
  225. the mark at the beginning of the previous sexp.
  226. The list and sexp commands' understanding of syntax is completely
  227. controlled by the syntax table. Any character can, for example, be
  228. declared to be an opening delimiter and act like an open parenthesis.
  229. @xref{Syntax}.
  230. @node Defuns, Grinding, Lists, Programs
  231. @section Defuns
  232. @cindex defuns
  233. In Emacs, a parenthetical grouping at the top level in the buffer is
  234. called a @dfn{defun}. The name derives from the fact that most
  235. top-level lists in Lisp are instances of the special form
  236. @code{defun}, but Emacs calls any top-level parenthetical
  237. grouping counts a defun regardless of its contents or
  238. the programming language. For example, in C, the body of a
  239. function definition is a defun.
  240. @c doublewidecommands
  241. @table @kbd
  242. @item C-M-a
  243. Move to beginning of current or preceding defun
  244. (@code{beginning-of-defun}).
  245. @item C-M-e
  246. Move to end of current or following defun (@code{end-of-defun}).
  247. @item C-M-h
  248. Put region around whole current or following defun (@code{mark-defun}).
  249. @end table
  250. @kindex C-M-a
  251. @kindex C-M-e
  252. @kindex C-M-h
  253. @findex beginning-of-defun
  254. @findex end-of-defun
  255. @findex mark-defun
  256. The commands to move to the beginning and end of the current defun are
  257. @kbd{C-M-a} (@code{beginning-of-defun}) and @kbd{C-M-e} (@code{end-of-defun}).
  258. To operate on the current defun, use @kbd{C-M-h} (@code{mark-defun})
  259. which puts point at the beginning and the mark at the end of the current
  260. or next defun. This is the easiest way to prepare for moving the defun
  261. to a different place. In C mode, @kbd{C-M-h} runs the function
  262. @code{mark-c-function}, which is almost the same as @code{mark-defun},
  263. but which backs up over the argument declarations, function name, and
  264. returned data type so that the entire C function is inside the region.
  265. @findex compile-defun
  266. To compile and evaluate the current defun, use @kbd{M-x compile-defun}.
  267. This function prints the results in the minibuffer. If you include an
  268. argument, it inserts the value in the current buffer after the defun.
  269. Emacs assumes that any open-parenthesis found in the leftmost column is
  270. the start of a defun. Therefore, @i{never put an open-parenthesis at the
  271. left margin in a Lisp file unless it is the start of a top level list.
  272. Never put an open-brace or other opening delimiter at the beginning of a
  273. line of C code unless it starts the body of a function.} The most likely
  274. problem case is when you want an opening delimiter at the start of a line
  275. inside a string. To avoid trouble, put an escape character (@samp{\} in C
  276. and Emacs Lisp, @samp{/} in some other Lisp dialects) before the opening
  277. delimiter. It will not affect the contents of the string.
  278. The original Emacs found defuns by moving upward a
  279. level of parentheses until there were no more levels to go up. This
  280. required scanning back to the beginning of the buffer for every
  281. function. To speed this up, Emacs was changed to assume
  282. that any @samp{(} (or other character assigned the syntactic class of
  283. opening-delimiter) at the left margin is the start of a defun. This
  284. heuristic is nearly always right; however, it mandates the convention
  285. described above.
  286. @node Grinding, Matching, Defuns, Programs
  287. @section Indentation for Programs
  288. @cindex indentation
  289. @cindex grinding
  290. The best way to keep a program properly indented (``ground'') is to
  291. use Emacs to re-indent it as you change the program. Emacs has commands
  292. to indent properly either a single line, a specified number of lines, or
  293. all of the lines inside a single parenthetical grouping.
  294. @menu
  295. * Basic Indent::
  296. * Multi-line Indent:: Commands to reindent many lines at once.
  297. * Lisp Indent:: Specifying how each Lisp function should be indented.
  298. * C Indent:: Choosing an indentation style for C code.
  299. @end menu
  300. @node Basic Indent, Multi-line Indent, Grinding, Grinding
  301. @subsection Basic Program Indentation Commands
  302. @c WideCommands
  303. @table @kbd
  304. @item @key{TAB}
  305. Adjust indentation of current line.
  306. @item @key{LFD}
  307. Equivalent to @key{RET} followed by @key{TAB} (@code{newline-and-indent}).
  308. @end table
  309. @kindex TAB
  310. @findex c-indent-line
  311. @findex lisp-indent-line
  312. The basic indentation command is @key{TAB}, which gives the current
  313. line the correct indentation as determined from the previous lines. The
  314. function that @key{TAB} runs depends on the major mode; it is
  315. @code{lisp-indent-line} in Lisp mode, @code{c-indent-line} in C mode,
  316. etc. These functions understand different syntaxes for different
  317. languages, but they all do about the same thing. @key{TAB} in any
  318. programming language major mode inserts or deletes whitespace at the
  319. beginning of the current line, independent of where point is in the
  320. line. If point is inside the whitespace at the beginning of the line,
  321. @key{TAB} leaves it at the end of that whitespace; otherwise, @key{TAB}
  322. leaves point fixed with respect to the characters around it.
  323. Use @kbd{C-q @key{TAB}} to insert a tab at point.
  324. @kindex LFD
  325. @findex newline-and-indent
  326. When entering a large amount of new code, use @key{LFD}
  327. (@code{newline-and-indent}), which is equivalent to a @key{RET} followed
  328. by a @key{TAB}. @key{LFD} creates a blank line, then gives it the
  329. appropriate indentation.
  330. @key{TAB} indents the second and following lines of the body of a
  331. parenthetical grouping each under the preceding one; therefore, if you
  332. alter one line's indentation to be nonstandard, the lines below tend
  333. to follow it. This is the right behavior in cases where the standard
  334. result of @key{TAB} does not look good.
  335. Remember that Emacs assumes that an open-parenthesis, open-brace, or
  336. other opening delimiter at the left margin (including the indentation
  337. routines) is the start of a function. You should therefore never have
  338. an opening delimiter in column zero that is not the beginning of a
  339. function, not even inside a string. This restriction is vital for
  340. making the indentation commands fast. @xref{Defuns}, for more
  341. information on this behavior.
  342. @node Multi-line Indent, Lisp Indent, Basic Indent, Grinding
  343. @subsection Indenting Several Lines
  344. Several commands are available to re-indent several lines of code
  345. which have been altered or moved to a different level in a list
  346. structure.
  347. @table @kbd
  348. @item C-M-q
  349. Re-indent all the lines within one list (@code{indent-sexp}).
  350. @item C-u @key{TAB}
  351. Shift an entire list rigidly sideways so that its first line
  352. is properly indented.
  353. @item C-M-\
  354. Re-indent all lines in the region (@code{indent-region}).
  355. @end table
  356. @kindex C-M-q
  357. @findex indent-sexp
  358. @findex indent-c-exp
  359. To re-indent the contents of a single list, position point before the
  360. beginning of it and type @kbd{C-M-q}. This key is bound to
  361. @code{indent-sexp} in Lisp mode, @code{indent-c-exp} in C mode, and
  362. bound to other suitable functions in other modes. The indentation of
  363. the line the sexp starts on is not changed; therefore, only the relative
  364. indentation within the list, and not its position, is changed. To
  365. correct the position as well, type a @key{TAB} before @kbd{C-M-q}.
  366. @kindex C-u TAB
  367. If the relative indentation within a list is correct but the
  368. indentation of its beginning is not, go to the line on which the list
  369. begins and type @kbd{C-u @key{TAB}}. When you give @key{TAB} a numeric
  370. argument, it moves all the lines in the group, starting on the current
  371. line, sideways the same amount that the current line moves. The command
  372. does not move lines that start inside strings, or C
  373. preprocessor lines when in C mode.
  374. @kindex C-M-\
  375. @findex indent-region
  376. Another way to specify a range to be re-indented is with point and
  377. mark. The command @kbd{C-M-\} (@code{indent-region}) applies @key{TAB}
  378. to every line whose first character is between point and mark.
  379. @node Lisp Indent, C Indent, Multi-line Indent, Grinding
  380. @subsection Customizing Lisp Indentation
  381. @cindex customization
  382. The indentation pattern for a Lisp expression can depend on the function
  383. called by the expression. For each Lisp function, you can choose among
  384. several predefined patterns of indentation, or define an arbitrary one with
  385. a Lisp program.
  386. The standard pattern of indentation is as follows: the second line of the
  387. expression is indented under the first argument, if that is on the same
  388. line as the beginning of the expression; otherwise, the second line is
  389. indented underneath the function name. Each following line is indented
  390. under the previous line whose nesting depth is the same.
  391. @vindex lisp-indent-offset
  392. If the variable @code{lisp-indent-offset} is non-@code{nil}, it overrides
  393. the usual indentation pattern for the second line of an expression, so that
  394. such lines are always indented @code{lisp-indent-offset} more columns than
  395. the containing list.
  396. @vindex lisp-body-indention
  397. Certain functions override the standard pattern. Functions
  398. whose names start with @code{def} always indent the second line by
  399. @code{lisp-body-indention} extra columns beyond the open-parenthesis
  400. starting the expression.
  401. Individual functions can override the standard pattern in various
  402. ways, according to the @code{lisp-indent-function} property of the
  403. function name. (Note: @code{lisp-indent-function} was formerly called
  404. @code{lisp-indent-hook}). There are four possibilities for this
  405. property:
  406. @table @asis
  407. @item @code{nil}
  408. This is the same as no property; the standard indentation pattern is used.
  409. @item @code{defun}
  410. The pattern used for function names that start with @code{def} is used for
  411. this function also.
  412. @item a number, @var{number}
  413. The first @var{number} arguments of the function are
  414. @dfn{distinguished} arguments; the rest are considered the @dfn{body}
  415. of the expression. A line in the expression is indented according to
  416. whether the first argument on it is distinguished or not. If the
  417. argument is part of the body, the line is indented @code{lisp-body-indent}
  418. more columns than the open-parenthesis starting the containing
  419. expression. If the argument is distinguished and is either the first
  420. or second argument, it is indented @i{twice} that many extra columns.
  421. If the argument is distinguished and not the first or second argument,
  422. the standard pattern is followed for that line.
  423. @item a symbol, @var{symbol}
  424. @var{symbol} should be a function name; that function is called to
  425. calculate the indentation of a line within this expression. The
  426. function receives two arguments:
  427. @table @asis
  428. @item @var{state}
  429. The value returned by @code{parse-partial-sexp} (a Lisp primitive for
  430. indentation and nesting computation) when it parses up to the
  431. beginning of this line.
  432. @item @var{pos}
  433. The position at which the line being indented begins.
  434. @end table
  435. @noindent
  436. It should return either a number, which is the number of columns of
  437. indentation for that line, or a list whose first element is such a
  438. number. The difference between returning a number and returning a list
  439. is that a number says that all following lines at the same nesting level
  440. should be indented just like this one; a list says that following lines
  441. might call for different indentations. This makes a difference when the
  442. indentation is computed by @kbd{C-M-q}; if the value is a number,
  443. @kbd{C-M-q} need not recalculate indentation for the following lines
  444. until the end of the list.
  445. @end table
  446. @node C Indent, , Lisp Indent, Grinding
  447. @subsection Customizing C Indentation
  448. Two variables control which commands perform C indentation and when.
  449. @vindex c-auto-newline
  450. If @code{c-auto-newline} is non-@code{nil}, newlines are inserted both
  451. before and after braces that you insert and after colons and semicolons.
  452. Correct C indentation is done on all the lines that are made this way.
  453. @vindex c-tab-always-indent
  454. If @code{c-tab-always-indent} is non-@code{nil}, the @key{TAB} command
  455. in C mode does indentation only if point is at the left margin or within
  456. the line's indentation. If there is non-whitespace to the left of point,
  457. @key{TAB} just inserts a tab character in the buffer. Normally,
  458. this variable is @code{nil}, and @key{TAB} always reindents the current line.
  459. C does not have anything analogous to particular function names for which
  460. special forms of indentation are desirable. However, it has a different
  461. need for customization facilities: many different styles of C indentation
  462. are in common use.
  463. There are six variables you can set to control the style that Emacs C
  464. mode will use.
  465. @table @code
  466. @item c-indent-level
  467. Indentation of C statements within surrounding block. The surrounding
  468. block's indentation is the indentation of the line on which the
  469. open-brace appears.
  470. @item c-continued-statement-offset
  471. Extra indentation given to a substatement, such as the then-clause of
  472. an @code{if} or body of a @code{while}.
  473. @item c-brace-offset
  474. Extra indentation for lines that start with an open brace.
  475. @item c-brace-imaginary-offset
  476. An open brace following other text is treated as if it were this far
  477. to the right of the start of its line.
  478. @item c-argdecl-indent
  479. Indentation level of declarations of C function arguments.
  480. @item c-label-offset
  481. Extra indentation for a line that is a label, case, or default.
  482. @end table
  483. @vindex c-indent-level
  484. The variable @code{c-indent-level} controls the indentation for C
  485. statements with respect to the surrounding block. In the example:
  486. @example
  487. @{
  488. foo ();
  489. @end example
  490. @noindent
  491. the difference in indentation between the lines is @code{c-indent-level}.
  492. Its standard value is 2.
  493. If the open-brace beginning the compound statement is not at the beginning
  494. of its line, the @code{c-indent-level} is added to the indentation of the
  495. line, not the column of the open-brace. For example,
  496. @example
  497. if (losing) @{
  498. do_this ();
  499. @end example
  500. @noindent
  501. One popular indentation style is that which results from setting
  502. @code{c-indent-level} to 8 and putting open-braces at the end of a line
  503. in this way. Another popular style prefers to put the open-brace on a
  504. separate line.
  505. @vindex c-brace-imaginary-offset
  506. In fact, the value of the variable @code{c-brace-imaginary-offset} is
  507. also added to the indentation of such a statement. Normally this variable
  508. is zero. Think of this variable as the imaginary position of the open
  509. brace, relative to the first non-blank character on the line. By setting
  510. the variable to 4 and @code{c-indent-level} to 0, you can get this style:
  511. @example
  512. if (x == y) @{
  513. do_it ();
  514. @}
  515. @end example
  516. When @code{c-indent-level} is zero, the statements inside most braces
  517. line up exactly under the open brace. An exception are braces in column
  518. zero, like those surrounding a function's body. The statements inside
  519. those braces are not placed at column zero. Instead,
  520. @code{c-brace-offset} and @code{c-continued-statement-offset} (see
  521. below) are added to produce a typical offset between brace levels, and
  522. the statements are indented that far.
  523. @vindex c-continued-statement-offset
  524. @code{c-continued-statement-offset} controls the extra indentation for
  525. a line that starts within a statement (but not within parentheses or
  526. brackets). These lines are usually statements inside other statements,
  527. like the then-clauses of @code{if} statements and the bodies of
  528. @code{while} statements. The @code{c-continued-statement-offset}
  529. parameter determines the difference in indentation between the two lines in:
  530. @example
  531. if (x == y)
  532. do_it ();
  533. @end example
  534. @noindent
  535. The default value for @code{c-continued-statement-offset} is 2. Some
  536. popular indentation styles correspond to a value of zero for
  537. @code{c-continued-statement-offset}.
  538. @vindex c-brace-offset
  539. @code{c-brace-offset} is the extra indentation given to a line that
  540. starts with an open-brace. Its standard value is zero;
  541. compare:
  542. @example
  543. if (x == y)
  544. @{
  545. @end example
  546. @noindent
  547. with:
  548. @example
  549. if (x == y)
  550. do_it ();
  551. @end example
  552. @noindent
  553. If you set @code{c-brace-offset} to 4, the first example becomes:
  554. @example
  555. if (x == y)
  556. @{
  557. @end example
  558. @vindex c-argdecl-indent
  559. @code{c-argdecl-indent} controls the indentation of declarations of the
  560. arguments of a C function. It is absolute: argument declarations receive
  561. exactly @code{c-argdecl-indent} spaces. The standard value is 5 and
  562. results in code like this:
  563. @example
  564. char *
  565. index (string, char)
  566. char *string;
  567. int char;
  568. @end example
  569. @vindex c-label-offset
  570. @code{c-label-offset} is the extra indentation given to a line that
  571. contains a label, a case statement, or a @code{default:} statement. Its
  572. standard value is @minus{}2 and results in code like this:
  573. @example
  574. switch (c)
  575. @{
  576. case 'x':
  577. @end example
  578. @noindent
  579. If @code{c-label-offset} were zero, the same code would be indented as:
  580. @example
  581. switch (c)
  582. @{
  583. case 'x':
  584. @end example
  585. @noindent
  586. This example assumes that the other variables above also have their
  587. default values.
  588. Using the indentation style produced by the default settings of the
  589. variables just discussed and putting open braces on separate lines
  590. produces clear and readable files. For an example, look at any of the C
  591. source files of XEmacs.
  592. @node Matching, Comments, Grinding, Programs
  593. @section Automatic Display of Matching Parentheses
  594. @cindex matching parentheses
  595. @cindex parentheses
  596. The Emacs parenthesis-matching feature shows you automatically how
  597. parentheses match in the text. Whenever a self-inserting character that
  598. is a closing delimiter is typed, the cursor moves momentarily to the
  599. location of the matching opening delimiter, provided that is visible on
  600. the screen. If it is not on the screen, some text starting with that
  601. opening delimiter is displayed in the echo area. Either way, you see
  602. the grouping you are closing off.
  603. In Lisp, automatic matching applies only to parentheses. In C, it
  604. also applies to braces and brackets. Emacs knows which characters to regard
  605. as matching delimiters based on the syntax table set by the major
  606. mode. @xref{Syntax}.
  607. If the opening delimiter and closing delimiter are mismatched---as
  608. in @samp{[x)}---the echo area displays a warning message. The
  609. correct matches are specified in the syntax table.
  610. @vindex blink-matching-paren
  611. @vindex blink-matching-paren-distance
  612. Two variables control parenthesis matching displays.
  613. @code{blink-matching-paren} turns the feature on or off. The default is
  614. @code{t} (match display is on); @code{nil} turns it off.
  615. @code{blink-matching-paren-distance} specifies how many characters back
  616. Emacs searches to find a matching opening delimiter. If the match is
  617. not found in the specified region, scanning stops, and nothing is
  618. displayed. This prevents wasting lots of time scanning when there is no
  619. match. The default is 4000.
  620. @node Comments, Balanced Editing, Matching, Programs
  621. @section Manipulating Comments
  622. @cindex comments
  623. @kindex M-;
  624. @cindex indentation
  625. @findex indent-for-comment
  626. The comment commands insert, kill and align comments.
  627. @c WideCommands
  628. @table @kbd
  629. @item M-;
  630. Insert or align comment (@code{indent-for-comment}).
  631. @item C-x ;
  632. Set comment column (@code{set-comment-column}).
  633. @item C-u - C-x ;
  634. Kill comment on current line (@code{kill-comment}).
  635. @item M-@key{LFD}
  636. Like @key{RET} followed by inserting and aligning a comment
  637. (@code{indent-new-comment-line}).
  638. @end table
  639. The command that creates a comment is @kbd{Meta-;}
  640. (@code{indent-for-comment}). If there is no comment already on the
  641. line, a new comment is created and aligned at a specific column called
  642. the @dfn{comment column}. Emacs creates the comment by inserting the
  643. string at the value of @code{comment-start}; see below. Point is left
  644. after that string. If the text of the line extends past the comment
  645. column, indentation is done to a suitable boundary (usually, at least
  646. one space is inserted). If the major mode has specified a string to
  647. terminate comments, that string is inserted after point, to keep the
  648. syntax valid.
  649. You can also use @kbd{Meta-;} to align an existing comment. If a line
  650. already contains the string that starts comments, @kbd{M-;} just moves
  651. point after it and re-indents it to the conventional place. Exception:
  652. comments starting in column 0 are not moved.
  653. Some major modes have special rules for indenting certain kinds of
  654. comments in certain contexts. For example, in Lisp code, comments which
  655. start with two semicolons are indented as if they were lines of code,
  656. instead of at the comment column. Comments which start with three
  657. semicolons are supposed to start at the left margin. Emacs understands
  658. these conventions by indenting a double-semicolon comment using @key{TAB}
  659. and by not changing the indentation of a triple-semicolon comment at all.
  660. @example
  661. ;; This function is just an example.
  662. ;;; Here either two or three semicolons are appropriate.
  663. (defun foo (x)
  664. ;;; And now, the first part of the function:
  665. ;; The following line adds one.
  666. (1+ x)) ; This line adds one.
  667. @end example
  668. In C code, a comment preceded on its line by nothing but whitespace
  669. is indented like a line of code.
  670. Even when an existing comment is properly aligned, @kbd{M-;} is still
  671. useful for moving directly to the start of the comment.
  672. @kindex C-u - C-x ;
  673. @findex kill-comment
  674. @kbd{C-u - C-x ;} (@code{kill-comment}) kills the comment on the
  675. current line, if there is one. The indentation before the start of the
  676. comment is killed as well. If there does not appear to be a comment in
  677. the line, nothing happens. To reinsert the comment on another line,
  678. move to the end of that line, type first @kbd{C-y}, and then @kbd{M-;}
  679. to realign the comment. Note that @kbd{C-u - C-x ;} is not a distinct
  680. key; it is @kbd{C-x ;} (@code{set-comment-column}) with a negative
  681. argument. That command is programmed to call @code{kill-comment} when
  682. called with a negative argument. However, @code{kill-comment} is a
  683. valid command which you could bind directly to a key if you wanted to.
  684. @subsection Multiple Lines of Comments
  685. @kindex M-LFD
  686. @cindex blank lines
  687. @cindex Auto Fill mode
  688. @findex indent-new-comment-line
  689. If you are typing a comment and want to continue it on another line,
  690. use the command @kbd{Meta-@key{LFD}} (@code{indent-new-comment-line}),
  691. which terminates the comment you are typing, creates a new blank line
  692. afterward, and begins a new comment indented under the old one. If
  693. Auto Fill mode is on and you go past the fill column while typing, the
  694. comment is continued in just this fashion. If point is
  695. not at the end of the line when you type @kbd{M-@key{LFD}}, the text on
  696. the rest of the line becomes part of the new comment line.
  697. @subsection Options Controlling Comments
  698. @vindex comment-column
  699. @kindex C-x ;
  700. @findex set-comment-column
  701. The comment column is stored in the variable @code{comment-column}. You
  702. can explicitly set it to a number. Alternatively, the command @kbd{C-x ;}
  703. (@code{set-comment-column}) sets the comment column to the column point is
  704. at. @kbd{C-u C-x ;} sets the comment column to match the last comment
  705. before point in the buffer, and then calls @kbd{Meta-;} to align the
  706. current line's comment under the previous one. Note that @kbd{C-u - C-x ;}
  707. runs the function @code{kill-comment} as described above.
  708. @code{comment-column} is a per-buffer variable; altering the variable
  709. affects only the current buffer. You can also change the default value.
  710. @xref{Locals}. Many major modes initialize this variable
  711. for the current buffer.
  712. @vindex comment-start-skip
  713. The comment commands recognize comments based on the regular expression
  714. that is the value of the variable @code{comment-start-skip}. This regexp
  715. should not match the null string. It may match more than the comment
  716. starting delimiter in the strictest sense of the word; for example, in C
  717. mode the value of the variable is @code{@t{"/\\*+ *"}}, which matches extra
  718. stars and spaces after the @samp{/*} itself. (Note that @samp{\\} is
  719. needed in Lisp syntax to include a @samp{\} in the string, which is needed
  720. to deny the first star its special meaning in regexp syntax. @xref{Regexps}.)
  721. @vindex comment-start
  722. @vindex comment-end
  723. When a comment command makes a new comment, it inserts the value of
  724. @code{comment-start} to begin it. The value of @code{comment-end} is
  725. inserted after point and will follow the text you will insert
  726. into the comment. In C mode, @code{comment-start} has the value
  727. @w{@code{"/* "}} and @code{comment-end} has the value @w{@code{" */"}}.
  728. @vindex comment-multi-line
  729. @code{comment-multi-line} controls how @kbd{M-@key{LFD}}
  730. (@code{indent-new-comment-line}) behaves when used inside a comment. If
  731. @code{comment-multi-line} is @code{nil}, as it normally is, then
  732. @kbd{M-@key{LFD}} terminates the comment on the starting line and starts
  733. a new comment on the new following line. If @code{comment-multi-line}
  734. is not @code{nil}, then @kbd{M-@key{LFD}} sets up the new following line
  735. as part of the same comment that was found on the starting line. This
  736. is done by not inserting a terminator on the old line and not inserting
  737. a starter on the new line. In languages where multi-line comments are legal,
  738. the value you choose for this variable is a matter of taste.
  739. @vindex comment-indent-hook
  740. The variable @code{comment-indent-hook} should contain a function that
  741. is called to compute the indentation for a newly inserted comment or for
  742. aligning an existing comment. Major modes set this variable differently.
  743. The function is called with no arguments, but with point at the
  744. beginning of the comment, or at the end of a line if a new comment is to
  745. be inserted. The function should return the column in which the comment
  746. ought to start. For example, in Lisp mode, the indent hook function
  747. bases its decision on the number of semicolons that begin an existing
  748. comment and on the code in the preceding lines.
  749. @node Balanced Editing, Lisp Completion, Comments, Programs
  750. @section Editing Without Unbalanced Parentheses
  751. @table @kbd
  752. @item M-(
  753. Put parentheses around next sexp(s) (@code{insert-parentheses}).
  754. @item M-)
  755. Move past next close parenthesis and re-indent
  756. (@code{move-over-close-and-reindent}).
  757. @end table
  758. @kindex M-(
  759. @kindex M-)
  760. @findex insert-parentheses
  761. @findex move-over-close-and-reindent
  762. The commands @kbd{M-(} (@code{insert-parentheses}) and @kbd{M-)}
  763. (@code{move-over-close-and-reindent}) are designed to facilitate a style of
  764. editing which keeps parentheses balanced at all times. @kbd{M-(} inserts a
  765. pair of parentheses, either together as in @samp{()}, or, if given an
  766. argument, around the next several sexps, and leaves point after the open
  767. parenthesis. Instead of typing @kbd{( F O O )}, you can type @kbd{M-( F O
  768. O}, which has the same effect except for leaving the cursor before the
  769. close parenthesis. You can then type @kbd{M-)}, which moves past the
  770. close parenthesis, deletes any indentation preceding it (in this example
  771. there is none), and indents with @key{LFD} after it.
  772. @node Lisp Completion, Documentation, Balanced Editing, Programs
  773. @section Completion for Lisp Symbols
  774. @cindex completion (symbol names)
  775. Completion usually happens in the minibuffer. An exception is
  776. completion for Lisp symbol names, which is available in all buffers.
  777. @kindex M-TAB
  778. @findex lisp-complete-symbol
  779. The command @kbd{M-@key{TAB}} (@code{lisp-complete-symbol}) takes the
  780. partial Lisp symbol before point to be an abbreviation, and compares it
  781. against all non-trivial Lisp symbols currently known to Emacs. Any
  782. additional characters that they all have in common are inserted at point.
  783. Non-trivial symbols are those that have function definitions, values, or
  784. properties.
  785. If there is an open-parenthesis immediately before the beginning of
  786. the partial symbol, only symbols with function definitions are considered
  787. as completions.
  788. If the partial name in the buffer has more than one possible completion
  789. and they have no additional characters in common, a list of all possible
  790. completions is displayed in another window.
  791. @node Documentation, Change Log, Lisp Completion, Programs
  792. @section Documentation Commands
  793. @kindex C-h f
  794. @findex describe-function
  795. @kindex C-h v
  796. @findex describe-variable
  797. As you edit Lisp code to be run in Emacs, you can use the commands
  798. @kbd{C-h f} (@code{describe-function}) and @kbd{C-h v}
  799. (@code{describe-variable}) to print documentation of functions and
  800. variables you want to call. These commands use the minibuffer to
  801. read the name of a function or variable to document, and display the
  802. documentation in a window.
  803. For extra convenience, these commands provide default arguments based on
  804. the code in the neighborhood of point. @kbd{C-h f} sets the default to the
  805. function called in the innermost list containing point. @kbd{C-h v} uses
  806. the symbol name around or adjacent to point as its default.
  807. @findex manual-entry
  808. The @kbd{M-x manual-entry} command gives you access to documentation
  809. on Unix commands, system calls, and libraries. The command reads a
  810. topic as an argument, and displays the Unix manual page for that topic.
  811. @code{manual-entry} always searches all 8 sections of the
  812. manual and concatenates all the entries it finds. For example,
  813. the topic @samp{termcap} finds the description of the termcap library
  814. from section 3, followed by the description of the termcap data base
  815. from section 5.
  816. @node Change Log, Tags, Documentation, Programs
  817. @section Change Logs
  818. @cindex change log
  819. @findex add-change-log-entry
  820. The Emacs command @kbd{M-x add-change-log-entry} helps you keep a record
  821. of when and why you have changed a program. It assumes that you have a
  822. file in which you write a chronological sequence of entries describing
  823. individual changes. The default is to store the change entries in a file
  824. called @file{ChangeLog} in the same directory as the file you are editing.
  825. The same @file{ChangeLog} file therefore records changes for all the files
  826. in a directory.
  827. A change log entry starts with a header line that contains your name
  828. and the current date. Except for these header lines, every line in the
  829. change log starts with a tab. One entry can describe several changes;
  830. each change starts with a line starting with a tab and a star. @kbd{M-x
  831. add-change-log-entry} visits the change log file and creates a new entry
  832. unless the most recent entry is for today's date and your name. In
  833. either case, it adds a new line to start the description of another
  834. change just after the header line of the entry. When @kbd{M-x
  835. add-change-log-entry} is finished, all is prepared for you to edit in
  836. the description of what you changed and how. You must then save the
  837. change log file yourself.
  838. The change log file is always visited in Indented Text mode, which means
  839. that @key{LFD} and auto-filling indent each new line like the previous
  840. line. This is convenient for entering the contents of an entry, which must
  841. be indented. @xref{Text Mode}.
  842. Here is an example of the formatting conventions used in the change log
  843. for Emacs:
  844. @smallexample
  845. Wed Jun 26 19:29:32 1985 Richard M. Stallman (rms at mit-prep)
  846. * xdisp.c (try_window_id):
  847. If C-k is done at end of next-to-last line,
  848. this fn updates window_end_vpos and cannot leave
  849. window_end_pos nonnegative (it is zero, in fact).
  850. If display is preempted before lines are output,
  851. this is inconsistent. Fix by setting
  852. blank_end_of_window to nonzero.
  853. Tue Jun 25 05:25:33 1985 Richard M. Stallman (rms at mit-prep)
  854. * cmds.c (Fnewline):
  855. Call the auto fill hook if appropriate.
  856. * xdisp.c (try_window_id):
  857. If point is found by compute_motion after xp, record that
  858. permanently. If display_text_line sets point position wrong
  859. (case where line is killed, point is at eob and that line is
  860. not displayed), set it again in final compute_motion.
  861. @end smallexample
  862. @node Tags, Fortran, Change Log, Programs
  863. @section Tags Tables
  864. @cindex tags table
  865. A @dfn{tags table} is a description of how a multi-file program is
  866. broken up into files. It lists the names of the component files and the
  867. names and positions of the functions (or other named subunits) in each
  868. file. Grouping the related files makes it possible to search or replace
  869. through all the files with one command. Recording the function names
  870. and positions makes possible the @kbd{M-.} command which finds the
  871. definition of a function by looking up which of the files it is in.
  872. Tags tables are stored in files called @dfn{tags table files}. The
  873. conventional name for a tags table file is @file{TAGS}.
  874. Each entry in the tags table records the name of one tag, the name of the
  875. file that the tag is defined in (implicitly), and the position in that file
  876. of the tag's definition.
  877. Just what names from the described files are recorded in the tags table
  878. depends on the programming language of the described file. They
  879. normally include all functions and subroutines, and may also include
  880. global variables, data types, and anything else convenient. Each name
  881. recorded is called a @dfn{tag}.
  882. @cindex C++ class browser, tags
  883. @cindex tags, C++
  884. @cindex class browser, C++
  885. @cindex Ebrowse
  886. The Ebrowse is a separate facility tailored for C++, with tags and a
  887. class browser. @xref{Top,,, ebrowse, Ebrowse User's Manual}.
  888. @menu
  889. * Tag Syntax:: Tag syntax for various types of code and text files.
  890. * Create Tags Table:: Creating a tags table with @code{etags}.
  891. * Etags Regexps:: Create arbitrary tags using regular expressions.
  892. * Select Tags Table:: How to visit a tags table.
  893. * Find Tag:: Commands to find the definition of a specific tag.
  894. * Tags Search:: Using a tags table for searching and replacing.
  895. * List Tags:: Listing and finding tags defined in a file.
  896. @end menu
  897. @node Tag Syntax
  898. @subsection Source File Tag Syntax
  899. Here is how tag syntax is defined for the most popular languages:
  900. @itemize @bullet
  901. @item
  902. In C code, any C function or typedef is a tag, and so are definitions of
  903. @code{struct}, @code{union} and @code{enum}. You can tag function
  904. declarations and external variables in addition to function definitions
  905. by giving the @samp{--declarations} option to @code{etags}.
  906. @code{#define} macro definitions and @code{enum} constants are also
  907. tags, unless you specify @samp{--no-defines} when making the tags table.
  908. Similarly, global variables are tags, unless you specify
  909. @samp{--no-globals}. Use of @samp{--no-globals} and @samp{--no-defines}
  910. can make the tags table file much smaller.
  911. @item
  912. In C++ code, in addition to all the tag constructs of C code, member
  913. functions are also recognized, and optionally member variables if you
  914. use the @samp{--members} option. Tags for variables and functions in
  915. classes are named @samp{@var{class}::@var{variable}} and
  916. @samp{@var{class}::@var{function}}. @code{operator} functions tags are
  917. named, for example @samp{operator+}.
  918. @item
  919. In Java code, tags include all the constructs recognized in C++, plus
  920. the @code{interface}, @code{extends} and @code{implements} constructs.
  921. Tags for variables and functions in classes are named
  922. @samp{@var{class}.@var{variable}} and @samp{@var{class}.@var{function}}.
  923. @item
  924. In La@TeX{} text, the argument of any of the commands @code{\chapter},
  925. @code{\section}, @code{\subsection}, @code{\subsubsection},
  926. @code{\eqno}, @code{\label}, @code{\ref}, @code{\cite}, @code{\bibitem},
  927. @code{\part}, @code{\appendix}, @code{\entry}, or @code{\index}, is a
  928. tag.@refill
  929. Other commands can make tags as well, if you specify them in the
  930. environment variable @code{TEXTAGS} before invoking @code{etags}. The
  931. value of this environment variable should be a colon-separated list of
  932. command names. For example,
  933. @example
  934. TEXTAGS="def:newcommand:newenvironment"
  935. export TEXTAGS
  936. @end example
  937. @noindent
  938. specifies (using Bourne shell syntax) that the commands @samp{\def},
  939. @samp{\newcommand} and @samp{\newenvironment} also define tags.
  940. @item
  941. In Lisp code, any function defined with @code{defun}, any variable
  942. defined with @code{defvar} or @code{defconst}, and in general the first
  943. argument of any expression that starts with @samp{(def} in column zero, is
  944. a tag.
  945. @item
  946. In Scheme code, tags include anything defined with @code{def} or with a
  947. construct whose name starts with @samp{def}. They also include variables
  948. set with @code{set!} at top level in the file.
  949. @end itemize
  950. Several other languages are also supported:
  951. @itemize @bullet
  952. @item
  953. In Ada code, functions, procedures, packages, tasks, and types are
  954. tags. Use the @samp{--packages-only} option to create tags for
  955. packages only.
  956. With Ada, it is possible to have the same name used for different
  957. entity kinds (e.g.@: the same name for a procedure and a function). Also,
  958. for things like packages, procedures and functions, there is the spec
  959. (i.e.@: the interface) and the body (i.e.@: the implementation). To
  960. facilitate the choice to the user, a tag value is appended with a
  961. qualifier:
  962. @table @asis
  963. @item function
  964. @kbd{/f}
  965. @item procedure
  966. @kbd{/p}
  967. @item package spec
  968. @kbd{/s}
  969. @item package body
  970. @kbd{/b}
  971. @item type
  972. @kbd{/t}
  973. @item task
  974. @kbd{/k}
  975. @end table
  976. So, as an example, @kbd{M-x find-tag bidule/b} will go directly to the
  977. body of the package @var{bidule} while @kbd{M-x find-tag bidule} will
  978. just search for any tag @var{bidule}.
  979. @item
  980. In assembler code, labels appearing at the beginning of a line,
  981. followed by a colon, are tags.
  982. @item
  983. In Bison or Yacc input files, each rule defines as a tag the nonterminal
  984. it constructs. The portions of the file that contain C code are parsed
  985. as C code.
  986. @item
  987. In Cobol code, tags are paragraph names; that is, any word starting in
  988. column 8 and followed by a period.
  989. @item
  990. In Erlang code, the tags are the functions, records, and macros defined
  991. in the file.
  992. @item
  993. In Fortran code, functions, subroutines and blockdata are tags.
  994. @item
  995. In makefiles, targets are tags.
  996. @item
  997. In Objective C code, tags include Objective C definitions for classes,
  998. class categories, methods, and protocols.
  999. @item
  1000. In Pascal code, the tags are the functions and procedures defined in
  1001. the file.
  1002. @item
  1003. In Perl code, the tags are the procedures defined by the @code{sub},
  1004. @code{my} and @code{local} keywords. Use @samp{--globals} if you want
  1005. to tag global variables.
  1006. @item
  1007. In PostScript code, the tags are the functions.
  1008. @item
  1009. In Prolog code, a tag name appears at the left margin.
  1010. @item
  1011. In Python code, @code{def} or @code{class} at the beginning of a line
  1012. generate a tag.
  1013. @end itemize
  1014. You can also generate tags based on regexp matching (@pxref{Etags
  1015. Regexps}) to handle other formats and languages.
  1016. @node Create Tags Table
  1017. @subsection Creating Tags Tables
  1018. @cindex @code{etags} program
  1019. The @code{etags} program is used to create a tags table file. It knows
  1020. the syntax of several languages, as described in
  1021. @iftex
  1022. the previous section.
  1023. @end iftex
  1024. @ifinfo
  1025. @ref{Tag Syntax}.
  1026. @end ifinfo
  1027. Here is how to run @code{etags}:
  1028. @example
  1029. etags @var{inputfiles}@dots{}
  1030. @end example
  1031. @noindent
  1032. The @code{etags} program reads the specified files, and writes a tags
  1033. table named @file{TAGS} in the current working directory. You can
  1034. intermix compressed and plain text source file names. @code{etags}
  1035. knows about the most common compression formats, and does the right
  1036. thing. So you can compress all your source files and have @code{etags}
  1037. look for compressed versions of its file name arguments, if it does not
  1038. find uncompressed versions. Under MS-DOS, @code{etags} also looks for
  1039. file names like @samp{mycode.cgz} if it is given @samp{mycode.c} on the
  1040. command line and @samp{mycode.c} does not exist.
  1041. @code{etags} recognizes the language used in an input file based on
  1042. its file name and contents. You can specify the language with the
  1043. @samp{--language=@var{name}} option, described below.
  1044. If the tags table data become outdated due to changes in the files
  1045. described in the table, the way to update the tags table is the same way it
  1046. was made in the first place. It is not necessary to do this often.
  1047. If the tags table fails to record a tag, or records it for the wrong
  1048. file, then Emacs cannot possibly find its definition. However, if the
  1049. position recorded in the tags table becomes a little bit wrong (due to
  1050. some editing in the file that the tag definition is in), the only
  1051. consequence is a slight delay in finding the tag. Even if the stored
  1052. position is very wrong, Emacs will still find the tag, but it must
  1053. search the entire file for it.
  1054. So you should update a tags table when you define new tags that you want
  1055. to have listed, or when you move tag definitions from one file to another,
  1056. or when changes become substantial. Normally there is no need to update
  1057. the tags table after each edit, or even every day.
  1058. One tags table can effectively include another. Specify the included
  1059. tags file name with the @samp{--include=@var{file}} option when creating
  1060. the file that is to include it. The latter file then acts as if it
  1061. contained all the files specified in the included file, as well as the
  1062. files it directly contains.
  1063. If you specify the source files with relative file names when you run
  1064. @code{etags}, the tags file will contain file names relative to the
  1065. directory where the tags file was initially written. This way, you can
  1066. move an entire directory tree containing both the tags file and the
  1067. source files, and the tags file will still refer correctly to the source
  1068. files.
  1069. If you specify absolute file names as arguments to @code{etags}, then
  1070. the tags file will contain absolute file names. This way, the tags file
  1071. will still refer to the same files even if you move it, as long as the
  1072. source files remain in the same place. Absolute file names start with
  1073. @samp{/}, or with @samp{@var{device}:/} on MS-DOS and MS-Windows.
  1074. When you want to make a tags table from a great number of files, you
  1075. may have problems listing them on the command line, because some systems
  1076. have a limit on its length. The simplest way to circumvent this limit
  1077. is to tell @code{etags} to read the file names from its standard input,
  1078. by typing a dash in place of the file names, like this:
  1079. @smallexample
  1080. find . -name "*.[chCH]" -print | etags -
  1081. @end smallexample
  1082. Use the option @samp{--language=@var{name}} to specify the language
  1083. explicitly. You can intermix these options with file names; each one
  1084. applies to the file names that follow it. Specify
  1085. @samp{--language=auto} to tell @code{etags} to resume guessing the
  1086. language from the file names and file contents. Specify
  1087. @samp{--language=none} to turn off language-specific processing
  1088. entirely; then @code{etags} recognizes tags by regexp matching alone
  1089. (@pxref{Etags Regexps}).
  1090. @samp{etags --help} prints the list of the languages @code{etags}
  1091. knows, and the file name rules for guessing the language. It also prints
  1092. a list of all the available @code{etags} options, together with a short
  1093. explanation.
  1094. @node Etags Regexps
  1095. @subsection Etags Regexps
  1096. The @samp{--regex} option provides a general way of recognizing tags
  1097. based on regexp matching. You can freely intermix it with file names.
  1098. Each @samp{--regex} option adds to the preceding ones, and applies only
  1099. to the following files. The syntax is:
  1100. @smallexample
  1101. --regex=/@var{tagregexp}[/@var{nameregexp}]/
  1102. @end smallexample
  1103. @noindent
  1104. where @var{tagregexp} is used to match the lines to tag. It is always
  1105. anchored, that is, it behaves as if preceded by @samp{^}. If you want
  1106. to account for indentation, just match any initial number of blanks by
  1107. beginning your regular expression with @samp{[ \t]*}. In the regular
  1108. expressions, @samp{\} quotes the next character, and @samp{\t} stands
  1109. for the tab character. Note that @code{etags} does not handle the other
  1110. C escape sequences for special characters.
  1111. @cindex interval operator (in regexps)
  1112. The syntax of regular expressions in @code{etags} is the same as in
  1113. Emacs, augmented with the @dfn{interval operator}, which works as in
  1114. @code{grep} and @code{ed}. The syntax of an interval operator is
  1115. @samp{\@{@var{m},@var{n}\@}}, and its meaning is to match the preceding
  1116. expression at least @var{m} times and up to @var{n} times.
  1117. You should not match more characters with @var{tagregexp} than that
  1118. needed to recognize what you want to tag. If the match is such that
  1119. more characters than needed are unavoidably matched by @var{tagregexp}
  1120. (as will usually be the case), you should add a @var{nameregexp}, to
  1121. pick out just the tag. This will enable Emacs to find tags more
  1122. accurately and to do completion on tag names more reliably. You can
  1123. find some examples below.
  1124. The option @samp{--ignore-case-regex} (or @samp{-c}) is like
  1125. @samp{--regex}, except that the regular expression provided will be
  1126. matched without regard to case, which is appropriate for various
  1127. programming languages.
  1128. The @samp{-R} option deletes all the regexps defined with
  1129. @samp{--regex} options. It applies to the file names following it, as
  1130. you can see from the following example:
  1131. @smallexample
  1132. etags --regex=/@var{reg1}/ voo.doo --regex=/@var{reg2}/ \
  1133. bar.ber -R --lang=lisp los.er
  1134. @end smallexample
  1135. @noindent
  1136. Here @code{etags} chooses the parsing language for @file{voo.doo} and
  1137. @file{bar.ber} according to their contents. @code{etags} also uses
  1138. @var{reg1} to recognize additional tags in @file{voo.doo}, and both
  1139. @var{reg1} and @var{reg2} to recognize additional tags in
  1140. @file{bar.ber}. @code{etags} uses the Lisp tags rules, and no regexp
  1141. matching, to recognize tags in @file{los.er}.
  1142. A regular expression can be bound to a given language, by prepending
  1143. it with @samp{@{lang@}}. When you do this, @code{etags} will use the
  1144. regular expression only for files of that language. @samp{etags --help}
  1145. prints the list of languages recognised by @code{etags}. The following
  1146. example tags the @code{DEFVAR} macros in the Emacs source files.
  1147. @code{etags} applies this regular expression to C files only:
  1148. @smallexample
  1149. --regex='@{c@}/[ \t]*DEFVAR_[A-Z_ \t(]+"\([^"]+\)"/'
  1150. @end smallexample
  1151. @noindent
  1152. This feature is particularly useful when storing a list of regular
  1153. expressions in a file. The following option syntax instructs
  1154. @code{etags} to read two files of regular expressions. The regular
  1155. expressions contained in the second file are matched without regard to
  1156. case.
  1157. @smallexample
  1158. --regex=@@first-file --ignore-case-regex=@@second-file
  1159. @end smallexample
  1160. @noindent
  1161. A regex file contains one regular expressions per line. Empty lines,
  1162. and lines beginning with space or tab are ignored. When the first
  1163. character in a line is @samp{@@}, @code{etags} assumes that the rest of
  1164. the line is the name of a file of regular expressions. This means that
  1165. such files can be nested. All the other lines are taken to be regular
  1166. expressions. For example, one can create a file called
  1167. @samp{emacs.tags} with the following contents (the first line in the
  1168. file is a comment):
  1169. @smallexample
  1170. -- This is for GNU Emacs source files
  1171. @{c@}/[ \t]*DEFVAR_[A-Z_ \t(]+"\([^"]+\)"/\1/
  1172. @end smallexample
  1173. @noindent
  1174. and then use it like this:
  1175. @smallexample
  1176. etags --regex=@@emacs.tags *.[ch] */*.[ch]
  1177. @end smallexample
  1178. Here are some more examples. The regexps are quoted to protect them
  1179. from shell interpretation.
  1180. @itemize @bullet
  1181. @item
  1182. Tag Octave files:
  1183. @smallexample
  1184. etags --language=none \
  1185. --regex='/[ \t]*function.*=[ \t]*\([^ \t]*\)[ \t]*(/\1/' \
  1186. --regex='/###key \(.*\)/\1/' \
  1187. --regex='/[ \t]*global[ \t].*/' \
  1188. *.m
  1189. @end smallexample
  1190. @noindent
  1191. Note that tags are not generated for scripts so that you have to add a
  1192. line by yourself of the form `###key <script-name>' if you want to jump
  1193. to it.
  1194. @item
  1195. Tag Tcl files:
  1196. @smallexample
  1197. etags --language=none --regex='/proc[ \t]+\([^ \t]+\)/\1/' *.tcl
  1198. @end smallexample
  1199. @item
  1200. Tag VHDL files:
  1201. @smallexample
  1202. --language=none \
  1203. --regex='/[ \t]*\(ARCHITECTURE\|CONFIGURATION\) +[^ ]* +OF/' \
  1204. --regex='/[ \t]*\(ATTRIBUTE\|ENTITY\|FUNCTION\|PACKAGE\
  1205. \( BODY\)?\|PROCEDURE\|PROCESS\|TYPE\)[ \t]+\([^ \t(]+\)/\3/'
  1206. @end smallexample
  1207. @end itemize
  1208. @node Select Tags Table, Find Tag, Etags Regexps, Tags
  1209. @subsection Selecting a Tags Table
  1210. @vindex tag-table-alist
  1211. At any time Emacs has one @dfn{selected} tags table, and all the commands
  1212. for working with tags tables use the selected one. To select a tags table,
  1213. use the variable @code{tag-table-alist}.
  1214. The value of @code{tag-table-alist} is a list that determines which
  1215. @code{TAGS} files should be active for a given buffer. This is not
  1216. really an association list, in that all elements are checked. The car
  1217. of each element of this list is a pattern against which the buffers file
  1218. name is compared; if it matches, then the cdr of the list should be the
  1219. name of the tags table to use. If more than one element of this list
  1220. matches the buffers file name, all of the associated tags tables are
  1221. used. Earlier ones are searched first.
  1222. If the car of elements of this list are strings, they are treated
  1223. as regular-expressions against which the file is compared (like the
  1224. @code{auto-mode-alist}). If they are not strings, they are evaluated.
  1225. If they evaluate to non-@code{nil}, the current buffer is considered to
  1226. match.
  1227. If the cdr of the elements of this list are strings, they are
  1228. assumed to name a tags file. If they name a directory, the string
  1229. @file{tags} is appended to them to get the file name. If they are not
  1230. strings, they are evaluated and must return an appropriate string.
  1231. For example:
  1232. @example
  1233. (setq tag-table-alist
  1234. '(("/usr/src/public/perl/" . "/usr/src/public/perl/perl-3.0/")
  1235. ("\\.el$" . "/usr/local/emacs/src/")
  1236. ("/jbw/gnu/" . "/usr15/degree/stud/jbw/gnu/")
  1237. ("" . "/usr/local/emacs/src/")
  1238. ))
  1239. @end example
  1240. The example defines the tags table alist in the following way:
  1241. @itemize @bullet
  1242. @item
  1243. Anything in the directory @file{/usr/src/public/perl/}
  1244. should use the @file{TAGS} file @file{/usr/src/public/perl/perl-3.0/TAGS}.
  1245. @item
  1246. Files ending in @file{.el} should use the @file{TAGS} file
  1247. @file{/usr/local/emacs/src/TAGS}.
  1248. @item
  1249. Anything in or below the directory @file{/jbw/gnu/} should use the
  1250. @file{TAGS} file @file{/usr15/degree/stud/jbw/gnu/TAGS}.
  1251. @end itemize
  1252. If you had a file called @file{/usr/jbw/foo.el}, it would use both
  1253. @file{TAGS} files, @* @file{/usr/local/emacs/src/TAGS} and
  1254. @file{/usr15/degree/stud/jbw/gnu/TAGS} (in that order), because it
  1255. matches both patterns.
  1256. If the buffer-local variable @code{buffer-tag-table} is set, it names a
  1257. tags table that is searched before all others when @code{find-tag} is
  1258. executed from this buffer.
  1259. If there is a file called @file{TAGS} in the same directory as the file
  1260. in question, then that tags file will always be used as well (after the
  1261. @code{buffer-tag-table} but before the tables specified by this list).
  1262. If the variable @code{tags-file-name} is set, the @file{TAGS} file it names
  1263. will apply to all buffers (for backwards compatibility.) It is searched
  1264. first.
  1265. @vindex tags-always-build-completion-table
  1266. If the value of the variable @code{tags-always-build-completion-table}
  1267. is @code{t}, the tags file will always be added to the completion table
  1268. without asking first, regardless of the size of the tags file.
  1269. @vindex tags-file-name
  1270. @findex visit-tags-table
  1271. The function @kbd{M-x visit-tags-table}, is largely made obsolete by
  1272. the variable @code{tag-table-alist}, tells tags commands to use the tags
  1273. table file @var{file} first. The @var{file} should be the name of a
  1274. file created with the @code{etags} program. A directory name is also
  1275. acceptable; it means the file @file{TAGS} in that directory. The
  1276. function only stores the file name you provide in the variable
  1277. @code{tags-file-name}. Emacs does not actually read in the tags table
  1278. contents until you try to use them. You can set the variable explicitly
  1279. instead of using @code{visit-tags-table}. The value of the variable
  1280. @code{tags-file-name} is the name of the tags table used by all buffers.
  1281. This is for backward compatibility, and is largely supplanted by the
  1282. variable @code{tag-table-alist}.
  1283. @node Find Tag, Tags Search, Select Tags Table, Tags
  1284. @subsection Finding a Tag
  1285. The most important thing that a tags table enables you to do is to find
  1286. the definition of a specific tag.
  1287. @table @kbd
  1288. @item M-.@: @var{tag &optional other-window}
  1289. Find first definition of @var{tag} (@code{find-tag}).
  1290. @item C-u M-.
  1291. Find next alternate definition of last tag specified.
  1292. @item C-x 4 . @var{tag}
  1293. Find first definition of @var{tag}, but display it in another window
  1294. (@code{find-tag-other-window}).
  1295. @end table
  1296. @kindex M-.
  1297. @findex find-tag
  1298. @kbd{M-.}@: (@code{find-tag}) is the command to find the definition of
  1299. a specified tag. It searches through the tags table for that tag, as a
  1300. string, then uses the tags table information to determine the file in
  1301. which the definition is used and the approximate character position of
  1302. the definition in the file. Then @code{find-tag} visits the file,
  1303. moves point to the approximate character position, and starts searching
  1304. ever-increasing distances away for the text that should appear at
  1305. the beginning of the definition.
  1306. If an empty argument is given (by typing @key{RET}), the sexp in the
  1307. buffer before or around point is used as the name of the tag to find.
  1308. @xref{Lists}, for information on sexps.
  1309. The argument to @code{find-tag} need not be the whole tag name; it can
  1310. be a substring of a tag name. However, there can be many tag names
  1311. containing the substring you specify. Since @code{find-tag} works by
  1312. searching the text of the tags table, it finds the first tag in the table
  1313. that the specified substring appears in. To find other tags that match
  1314. the substring, give @code{find-tag} a numeric argument, as in @kbd{C-u
  1315. M-.}. This does not read a tag name, but continues searching the tag
  1316. table's text for another tag containing the same substring last used.
  1317. If your keyboard has a real @key{META} key, @kbd{M-0 M-.}@: is an easier
  1318. alternative to @kbd{C-u M-.}.
  1319. If the optional second argument @var{other-window} is non-@code{nil}, it uses
  1320. another window to display the tag.
  1321. Multiple active tags tables and completion are supported.
  1322. Variables of note include the following:
  1323. @vindex tag-table-alist
  1324. @vindex tags-file-name
  1325. @vindex tags-build-completion-table
  1326. @vindex buffer-tag-table
  1327. @vindex make-tags-files-invisible
  1328. @vindex tag-mark-stack-max
  1329. @table @kbd
  1330. @item tag-table-alist
  1331. Controls which tables apply to which buffers.
  1332. @item tags-file-name
  1333. Stores a default tags table.
  1334. @item tags-build-completion-table
  1335. Controls completion behavior.
  1336. @item buffer-tag-table
  1337. Specifies a buffer-local table.
  1338. @item make-tags-files-invisible
  1339. Sets whether tags tables should be very hidden.
  1340. @item tag-mark-stack-max
  1341. Specifies how many tags-based hops to remember.
  1342. @end table
  1343. @kindex C-x 4 .
  1344. @findex find-tag-other-window
  1345. Like most commands that can switch buffers, @code{find-tag} has another
  1346. similar command that displays the new buffer in another window. @kbd{C-x 4
  1347. .}@: invokes the function @code{find-tag-other-window}. (This key sequence
  1348. ends with a period.)
  1349. Emacs comes with a tags table file @file{TAGS} (in the directory
  1350. containing Lisp libraries) that includes all the Lisp libraries and all
  1351. the C sources of Emacs. By specifying this file with @code{visit-tags-table}
  1352. and then using @kbd{M-.}@: you can quickly look at the source of any Emacs
  1353. function.
  1354. @node Tags Search, List Tags, Find Tag, Tags
  1355. @subsection Searching and Replacing with Tags Tables
  1356. The commands in this section visit and search all the files listed in the
  1357. selected tags table, one by one. For these commands, the tags table serves
  1358. only to specify a sequence of files to search. A related command is
  1359. @kbd{M-x grep} (@pxref{Compilation}).
  1360. @table @kbd
  1361. @item M-x tags-search @key{RET} @var{regexp} @key{RET}
  1362. Search for @var{regexp} through the files in the selected tags
  1363. table.
  1364. @item M-x tags-query-replace @key{RET} @var{regexp} @key{RET} @var{replacement} @key{RET}
  1365. Perform a @code{query-replace-regexp} on each file in the selected tags table.
  1366. @item M-,
  1367. Restart one of the commands above, from the current location of point
  1368. (@code{tags-loop-continue}).
  1369. @end table
  1370. @findex tags-search
  1371. @kbd{M-x tags-search} reads a regexp using the minibuffer, then
  1372. searches for matches in all the files in the selected tags table, one
  1373. file at a time. It displays the name of the file being searched so you
  1374. can follow its progress. As soon as it finds an occurrence,
  1375. @code{tags-search} returns.
  1376. @kindex M-,
  1377. @findex tags-loop-continue
  1378. Having found one match, you probably want to find all the rest. To find
  1379. one more match, type @kbd{M-,} (@code{tags-loop-continue}) to resume the
  1380. @code{tags-search}. This searches the rest of the current buffer, followed
  1381. by the remaining files of the tags table.@refill
  1382. @findex tags-query-replace
  1383. @kbd{M-x tags-query-replace} performs a single
  1384. @code{query-replace-regexp} through all the files in the tags table. It
  1385. reads a regexp to search for and a string to replace with, just like
  1386. ordinary @kbd{M-x query-replace-regexp}. It searches much like @kbd{M-x
  1387. tags-search}, but repeatedly, processing matches according to your
  1388. input. @xref{Replace}, for more information on query replace.
  1389. It is possible to get through all the files in the tags table with a
  1390. single invocation of @kbd{M-x tags-query-replace}. But often it is
  1391. useful to exit temporarily, which you can do with any input event that
  1392. has no special query replace meaning. You can resume the query replace
  1393. subsequently by typing @kbd{M-,}; this command resumes the last tags
  1394. search or replace command that you did.
  1395. The commands in this section carry out much broader searches than the
  1396. @code{find-tag} family. The @code{find-tag} commands search only for
  1397. definitions of tags that match your substring or regexp. The commands
  1398. @code{tags-search} and @code{tags-query-replace} find every occurrence
  1399. of the regexp, as ordinary search commands and replace commands do in
  1400. the current buffer.
  1401. These commands create buffers only temporarily for the files that they
  1402. have to search (those which are not already visited in Emacs buffers).
  1403. Buffers in which no match is found are quickly killed; the others
  1404. continue to exist.
  1405. It may have struck you that @code{tags-search} is a lot like
  1406. @code{grep}. You can also run @code{grep} itself as an inferior of
  1407. Emacs and have Emacs show you the matching lines one by one. This works
  1408. much like running a compilation; finding the source locations of the
  1409. @code{grep} matches works like finding the compilation errors.
  1410. @xref{Compilation}.
  1411. If you wish to process all the files in a selected tags table, but
  1412. @kbd{M-x tags-search} and @kbd{M-x tags-query-replace} are not giving
  1413. you the desired result, you can use @kbd{M-x next-file}.
  1414. @table @kbd
  1415. @item C-u M-x next-file
  1416. With a numeric argument, regardless of its value, visit the first
  1417. file in the tags table and prepare to advance sequentially by files.
  1418. @item M-x next-file
  1419. Visit the next file in the selected tags table.
  1420. @end table
  1421. @node List Tags, , Tags Search, Tags
  1422. @subsection Tags Table Inquiries
  1423. @table @kbd
  1424. @item M-x list-tags
  1425. Display a list of the tags defined in a specific program file.
  1426. @item M-x tags-apropos
  1427. Display a list of all tags matching a specified regexp.
  1428. @end table
  1429. @findex list-tags
  1430. @kbd{M-x list-tags} reads the name of one of the files described by the
  1431. selected tags table, and displays a list of all the tags defined in that
  1432. file. The ``file name'' argument is really just a string to compare
  1433. against the names recorded in the tags table; it is read as a string rather
  1434. than a file name. Therefore, completion and defaulting are not
  1435. available, and you must enter the string the same way it appears in the tag
  1436. table. Do not include a directory as part of the file name unless the file
  1437. name recorded in the tags table contains that directory.
  1438. @findex tags-apropos
  1439. @kbd{M-x tags-apropos} is like @code{apropos} for tags. It reads a regexp,
  1440. then finds all the tags in the selected tags table whose entries match that
  1441. regexp, and displays the tag names found.
  1442. @node Fortran, Asm Mode, Tags, Programs
  1443. @section Fortran Mode
  1444. @cindex Fortran mode
  1445. Fortran mode provides special motion commands for Fortran statements and
  1446. subprograms, and indentation commands that understand Fortran conventions
  1447. of nesting, line numbers, and continuation statements.
  1448. Special commands for comments are provided because Fortran comments are
  1449. unlike those of other languages.
  1450. Built-in abbrevs optionally save typing when you insert Fortran keywords.
  1451. @findex fortran-mode
  1452. Use @kbd{M-x fortran-mode} to switch to this major mode. Doing so calls
  1453. the value of @code{fortran-mode-hook} as a function of no arguments if
  1454. that variable has a non-@code{nil} value.
  1455. @menu
  1456. * Motion: Fortran Motion. Moving point by statements or subprograms.
  1457. * Indent: Fortran Indent. Indentation commands for Fortran.
  1458. * Comments: Fortran Comments. Inserting and aligning comments.
  1459. * Columns: Fortran Columns. Measuring columns for valid Fortran.
  1460. * Abbrev: Fortran Abbrev. Built-in abbrevs for Fortran keywords.
  1461. @end menu
  1462. Fortran mode was contributed by Michael Prange.
  1463. @node Fortran Motion, Fortran Indent, Fortran, Fortran
  1464. @subsection Motion Commands
  1465. Fortran mode provides special commands to move by subprograms (functions
  1466. and subroutines) and by statements. There is also a command to put the
  1467. region around one subprogram, which is convenient for killing it or moving it.
  1468. @kindex C-M-a (Fortran mode)
  1469. @kindex C-M-e (Fortran mode)
  1470. @kindex C-M-h (Fortran mode)
  1471. @kindex C-c C-p (Fortran mode)
  1472. @kindex C-c C-n (Fortran mode)
  1473. @findex beginning-of-fortran-subprogram
  1474. @findex end-of-fortran-subprogram
  1475. @findex mark-fortran-subprogram
  1476. @findex fortran-previous-statement
  1477. @findex fortran-next-statement
  1478. @table @kbd
  1479. @item C-M-a
  1480. Move to beginning of subprogram@*
  1481. (@code{beginning-of-fortran-subprogram}).
  1482. @item C-M-e
  1483. Move to end of subprogram (@code{end-of-fortran-subprogram}).
  1484. @item C-M-h
  1485. Put point at beginning of subprogram and mark at end
  1486. (@code{mark-fortran-subprogram}).
  1487. @item C-c C-n
  1488. Move to beginning of current or next statement
  1489. (@code{fortran-next-@*statement}).
  1490. @item C-c C-p
  1491. Move to beginning of current or previous statement
  1492. (@code{fortran-@*previous-statement}).
  1493. @end table
  1494. @node Fortran Indent, Fortran Comments, Fortran Motion, Fortran
  1495. @subsection Fortran Indentation
  1496. Special commands and features are available for indenting Fortran
  1497. code. They make sure various syntactic entities (line numbers, comment line
  1498. indicators, and continuation line flags) appear in the columns that are
  1499. required for standard Fortran.
  1500. @menu
  1501. * Commands: ForIndent Commands. Commands for indenting Fortran.
  1502. * Numbers: ForIndent Num. How line numbers auto-indent.
  1503. * Conv: ForIndent Conv. Conventions you must obey to avoid trouble.
  1504. * Vars: ForIndent Vars. Variables controlling Fortran indent style.
  1505. @end menu
  1506. @node ForIndent Commands, ForIndent Num, Fortran Indent, Fortran Indent
  1507. @subsubsection Fortran Indentation Commands
  1508. @table @kbd
  1509. @item @key{TAB}
  1510. Indent the current line (@code{fortran-indent-line}).
  1511. @item M-@key{LFD}
  1512. Break the current line and set up a continuation line.
  1513. @item C-M-q
  1514. Indent all the lines of the subprogram point is in
  1515. (@code{fortran-indent-subprogram}).
  1516. @end table
  1517. @findex fortran-indent-line
  1518. @key{TAB} is redefined by Fortran mode to reindent the current line for
  1519. Fortran (@code{fortran-indent-line}). Line numbers and continuation
  1520. markers are indented to their required columns, and the body of the
  1521. statement is independently indented, based on its nesting in the program.
  1522. @kindex C-M-q (Fortran mode)
  1523. @findex fortran-indent-subprogram
  1524. The key @kbd{C-M-q} is redefined as @code{fortran-indent-subprogram}, a
  1525. command that reindents all the lines of the Fortran subprogram (function or
  1526. subroutine) containing point.
  1527. @kindex M-LFD (Fortran mode)
  1528. @findex fortran-split-line
  1529. The key @kbd{M-@key{LFD}} is redefined as @code{fortran-split-line}, a
  1530. command to split a line in the appropriate fashion for Fortran. In a
  1531. non-comment line, the second half becomes a continuation line and is
  1532. indented accordingly. In a comment line, both halves become separate
  1533. comment lines.
  1534. @node ForIndent Num, ForIndent Conv, ForIndent Commands, Fortran Indent
  1535. @subsubsection Line Numbers and Continuation
  1536. If a number is the first non-whitespace in the line, it is assumed to be
  1537. a line number and is moved to columns 0 through 4. (Columns are always
  1538. counted from 0 in XEmacs.) If the text on the line starts with the
  1539. conventional Fortran continuation marker @samp{$}, it is moved to column 5.
  1540. If the text begins with any non whitespace character in column 5, it is
  1541. assumed to be an unconventional continuation marker and remains in column
  1542. 5.
  1543. @vindex fortran-line-number-indent
  1544. Line numbers of four digits or less are normally indented one space.
  1545. This amount is controlled by the variable @code{fortran-line-number-indent},
  1546. which is the maximum indentation a line number can have. Line numbers
  1547. are indented to right-justify them to end in column 4 unless that would
  1548. require more than the maximum indentation. The default value of the
  1549. variable is 1.
  1550. @vindex fortran-electric-line-number
  1551. Simply inserting a line number is enough to indent it according to these
  1552. rules. As each digit is inserted, the indentation is recomputed. To turn
  1553. off this feature, set the variable @code{fortran-electric-line-number} to
  1554. @code{nil}. Then inserting line numbers is like inserting anything else.
  1555. @node ForIndent Conv, ForIndent Vars, ForIndent Num, Fortran Indent
  1556. @subsubsection Syntactic Conventions
  1557. Fortran mode assumes that you follow certain conventions that simplify
  1558. the task of understanding a Fortran program well enough to indent it
  1559. properly:
  1560. @vindex fortran-continuation-char
  1561. @itemize @bullet
  1562. @item
  1563. Two nested @samp{do} loops never share a @samp{continue} statement.
  1564. @item
  1565. The same character appears in column 5 of all continuation lines. It
  1566. is the value of the variable @code{fortran-continuation-char}.
  1567. By default, this character is @samp{$}.
  1568. @end itemize
  1569. @noindent
  1570. If you fail to follow these conventions, the indentation commands may
  1571. indent some lines unaesthetically. However, a correct Fortran program will
  1572. retain its meaning when reindented even if the conventions are not
  1573. followed.
  1574. @node ForIndent Vars, , ForIndent Conv, Fortran Indent
  1575. @subsubsection Variables for Fortran Indentation
  1576. @vindex fortran-do-indent
  1577. @vindex fortran-if-indent
  1578. @vindex fortran-continuation-indent
  1579. @vindex fortran-check-all-num-for-matching-do
  1580. @vindex fortran-minimum-statement-indent
  1581. Several additional variables control how Fortran indentation works.
  1582. @table @code
  1583. @item fortran-do-indent
  1584. Extra indentation within each level of @samp{do} statement (the default is 3).
  1585. @item fortran-if-indent
  1586. Extra indentation within each level of @samp{if} statement (the default is 3).
  1587. @item fortran-continuation-indent
  1588. Extra indentation for bodies of continuation lines (the default is 5).
  1589. @item fortran-check-all-num-for-matching-do
  1590. If this is @code{nil}, indentation assumes that each @samp{do}
  1591. statement ends on a @samp{continue} statement. Therefore, when
  1592. computing indentation for a statement other than @samp{continue}, it
  1593. can save time by not checking for a @samp{do} statement ending there.
  1594. If this is non-@code{nil}, indenting any numbered statement must check
  1595. for a @samp{do} that ends there. The default is @code{nil}.
  1596. @item fortran-minimum-statement-indent
  1597. Minimum indentation for Fortran statements. For standard Fortran,
  1598. this is 6. Statement bodies are always indented at least this much.
  1599. @end table
  1600. @node Fortran Comments, Fortran Columns, Fortran Indent, Fortran
  1601. @subsection Comments
  1602. The usual Emacs comment commands assume that a comment can follow a line
  1603. of code. In Fortran, the standard comment syntax requires an entire line
  1604. to be just a comment. Therefore, Fortran mode replaces the standard Emacs
  1605. comment commands and defines some new variables.
  1606. Fortran mode can also handle a non-standard comment syntax where comments
  1607. start with @samp{!} and can follow other text. Because only some Fortran
  1608. compilers accept this syntax, Fortran mode will not insert such comments
  1609. unless you have specified to do so in advance by setting the variable
  1610. @code{comment-start} to @samp{"!"} (@pxref{Variables}).
  1611. @table @kbd
  1612. @item M-;
  1613. Align comment or insert new comment (@code{fortran-comment-indent}).
  1614. @item C-x ;
  1615. Applies to nonstandard @samp{!} comments only.
  1616. @item C-c ;
  1617. Turn all lines of the region into comments, or (with arg)
  1618. turn them back into real code (@code{fortran-comment-region}).
  1619. @end table
  1620. @kbd{M-;} in Fortran mode is redefined as the command
  1621. @code{fortran-comment-indent}. Like the usual @kbd{M-;} command,
  1622. it recognizes an existing comment and aligns its text appropriately.
  1623. If there is no existing comment, a comment is inserted and aligned.
  1624. Inserting and aligning comments is not the same in Fortran mode as in
  1625. other modes. When a new comment must be inserted, a full-line comment is
  1626. inserted if the current line is blank. On a non-blank line, a
  1627. non-standard @samp{!} comment is inserted if you previously specified
  1628. you wanted to use them. Otherwise a full-line comment is inserted on a
  1629. new line before the current line.
  1630. Non-standard @samp{!} comments are aligned like comments in other
  1631. languages, but full-line comments are aligned differently. In a
  1632. standard full-line comment, the comment delimiter itself must always
  1633. appear in column zero. What can be aligned is the text within the
  1634. comment. You can choose from three styles of alignment by setting the
  1635. variable @code{fortran-comment-indent-style} to one of these values:
  1636. @vindex fortran-comment-indent-style
  1637. @vindex fortran-comment-line-column
  1638. @table @code
  1639. @item fixed
  1640. The text is aligned at a fixed column, which is the value of
  1641. @code{fortran-comment-line-column}. This is the default.
  1642. @item relative
  1643. The text is aligned as if it were a line of code, but with an
  1644. additional @code{fortran-comment-line-column} columns of indentation.
  1645. @item nil
  1646. Text in full-line columns is not moved automatically.
  1647. @end table
  1648. @vindex fortran-comment-indent-char
  1649. You can also specify the character to be used to indent within
  1650. full-line comments by setting the variable @code{fortran-comment-indent-char}
  1651. to the character you want to use.
  1652. @vindex comment-line-start
  1653. @vindex comment-line-start-skip
  1654. Fortran mode introduces two variables @code{comment-line-start} and
  1655. @code{comment-line-start-skip}, which do for full-line comments what
  1656. @code{comment-start} and @code{comment-start-skip} do for
  1657. ordinary text-following comments. Normally these are set properly by
  1658. Fortran mode, so you do not need to change them.
  1659. The normal Emacs comment command @kbd{C-x ;} has not been redefined.
  1660. It can therefore be used if you use @samp{!} comments, but is useless in
  1661. Fortran mode otherwise.
  1662. @kindex C-c ; (Fortran mode)
  1663. @findex fortran-comment-region
  1664. @vindex fortran-comment-region
  1665. The command @kbd{C-c ;} (@code{fortran-comment-region}) turns all the
  1666. lines of the region into comments by inserting the string @samp{C$$$} at
  1667. the front of each one. With a numeric arg, the region is turned back into
  1668. live code by deleting @samp{C$$$} from the front of each line. You can
  1669. control the string used for the comments by setting the variable
  1670. @code{fortran-comment-region}. Note that here we have an example of a
  1671. command and a variable with the same name; the two uses of the name never
  1672. conflict because in Lisp and in Emacs it is always clear from the context
  1673. which one is referred to.
  1674. @node Fortran Columns, Fortran Abbrev, Fortran Comments, Fortran
  1675. @subsection Columns
  1676. @table @kbd
  1677. @item C-c C-r
  1678. Displays a ``column ruler'' momentarily above the current line
  1679. (@code{fortran-column-ruler}).
  1680. @item C-c C-w
  1681. Splits the current window horizontally so that it is 72 columns wide.
  1682. This may help you avoid going over that limit (@code{fortran-window-create}).
  1683. @end table
  1684. @kindex C-c C-r (Fortran mode)
  1685. @findex fortran-column-ruler
  1686. The command @kbd{C-c C-r} (@code{fortran-column-ruler}) shows a column
  1687. ruler above the current line. The comment ruler consists of two lines
  1688. of text that show you the locations of columns with special significance
  1689. in Fortran programs. Square brackets show the limits of the columns for
  1690. line numbers, and curly brackets show the limits of the columns for the
  1691. statement body. Column numbers appear above them.
  1692. Note that the column numbers count from zero, as always in XEmacs. As
  1693. a result, the numbers may not be those you are familiar with; but the
  1694. actual positions in the line are standard Fortran.
  1695. The text used to display the column ruler is the value of the variable
  1696. @code{fortran-comment-ruler}. By changing this variable, you can change
  1697. the display.
  1698. @kindex C-c C-w (Fortran mode)
  1699. @findex fortran-window-create
  1700. For even more help, use @kbd{C-c C-w} (@code{fortran-window-create}), a
  1701. command which splits the current window horizontally, resulting in a window 72
  1702. columns wide. When you edit in this window, you can immediately see
  1703. when a line gets too wide to be correct Fortran.
  1704. @node Fortran Abbrev, , Fortran Columns, Fortran
  1705. @subsection Fortran Keyword Abbrevs
  1706. Fortran mode provides many built-in abbrevs for common keywords and
  1707. declarations. These are the same sort of abbrevs that you can define
  1708. yourself. To use them, you must turn on Abbrev mode. @pxref{Abbrevs}.
  1709. The built-in abbrevs are unusual in one way: they all start with a
  1710. semicolon. You cannot normally use semicolon in an abbrev, but Fortran
  1711. mode makes this possible by changing the syntax of semicolon to ``word
  1712. constituent''.
  1713. For example, one built-in Fortran abbrev is @samp{;c} for
  1714. @samp{continue}. If you insert @samp{;c} and then insert a punctuation
  1715. character such as a space or a newline, the @samp{;c} changes
  1716. automatically to @samp{continue}, provided Abbrev mode is enabled.@refill
  1717. Type @samp{;?} or @samp{;C-h} to display a list of all built-in
  1718. Fortran abbrevs and what they stand for.
  1719. @node Asm Mode, , Fortran, Programs
  1720. @section Asm Mode
  1721. @cindex Asm mode
  1722. Asm mode is a major mode for editing files of assembler code. It
  1723. defines these commands:
  1724. @table @kbd
  1725. @item @key{TAB}
  1726. @code{tab-to-tab-stop}.
  1727. @item @key{LFD}
  1728. Insert a newline and then indent using @code{tab-to-tab-stop}.
  1729. @item :
  1730. Insert a colon and then remove the indentation from before the label
  1731. preceding colon. Then do @code{tab-to-tab-stop}.
  1732. @item ;
  1733. Insert or align a comment.
  1734. @end table
  1735. The variable @code{asm-comment-char} specifies which character
  1736. starts comments in assembler syntax.