programs.texi 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907
  1. @c -*- coding: utf-8 -*-
  2. @c This is part of the Emacs manual.
  3. @c Copyright (C) 1985-1987, 1993-1995, 1997, 1999-2017 Free Software
  4. @c Foundation, Inc.
  5. @c See file emacs.texi for copying conditions.
  6. @node Programs
  7. @chapter Editing Programs
  8. @cindex Lisp editing
  9. @cindex C editing
  10. @cindex program editing
  11. This chapter describes Emacs features for facilitating editing
  12. programs. Some of the things these features can do are:
  13. @itemize @bullet
  14. @item
  15. Find or move over top-level definitions (@pxref{Defuns}).
  16. @item
  17. Apply the usual indentation conventions of the language
  18. (@pxref{Program Indent}).
  19. @item
  20. Balance parentheses (@pxref{Parentheses}).
  21. @item
  22. Insert, kill or align comments (@pxref{Comments}).
  23. @item
  24. Highlight program syntax (@pxref{Font Lock}).
  25. @end itemize
  26. @menu
  27. * Program Modes:: Major modes for editing programs.
  28. * Defuns:: Commands to operate on major top-level parts
  29. of a program.
  30. * Program Indent:: Adjusting indentation to show the nesting.
  31. * Parentheses:: Commands that operate on parentheses.
  32. * Comments:: Inserting, killing, and aligning comments.
  33. * Documentation:: Getting documentation of functions you plan to call.
  34. * Hideshow:: Displaying blocks selectively.
  35. * Symbol Completion:: Completion on symbol names of your program or language.
  36. * MixedCase Words:: Dealing with identifiersLikeThis.
  37. * Semantic:: Suite of editing tools based on source code parsing.
  38. * Misc for Programs:: Other Emacs features useful for editing programs.
  39. * C Modes:: Special commands of C, C++, Objective-C, Java,
  40. IDL, Pike and AWK modes.
  41. * Asm Mode:: Asm mode and its special features.
  42. @ifnottex
  43. * Fortran:: Fortran mode and its special features.
  44. @end ifnottex
  45. @end menu
  46. @node Program Modes
  47. @section Major Modes for Programming Languages
  48. @cindex modes for programming languages
  49. Emacs has specialized major modes (@pxref{Major Modes}) for many
  50. programming languages. A programming language mode typically
  51. specifies the syntax of expressions, the customary rules for
  52. indentation, how to do syntax highlighting for the language, and how
  53. to find the beginning or end of a function definition. It often has
  54. features for compiling and debugging programs as well. The major mode
  55. for each language is named after the language; for instance, the major
  56. mode for the C programming language is @code{c-mode}.
  57. @cindex Perl mode
  58. @cindex Icon mode
  59. @cindex Makefile mode
  60. @cindex Tcl mode
  61. @cindex CPerl mode
  62. @cindex DSSSL mode
  63. @cindex Octave mode
  64. @cindex Metafont mode
  65. @cindex Modula2 mode
  66. @cindex Prolog mode
  67. @cindex Python mode
  68. @cindex Ruby mode
  69. @cindex Simula mode
  70. @cindex VHDL mode
  71. @cindex M4 mode
  72. @cindex Shell-script mode
  73. @cindex OPascal mode
  74. @cindex PostScript mode
  75. @cindex Conf mode
  76. @cindex DNS mode
  77. @cindex Javascript mode
  78. Emacs has programming language modes for Lisp, Scheme, the
  79. Scheme-based DSSSL expression language, Ada, ASM, AWK, C, C++,
  80. Fortran, Icon, IDL (CORBA), IDLWAVE, Java, Javascript, Metafont
  81. (@TeX{}'s companion for font creation), Modula2, Object Pascal, Objective-C,
  82. Octave, Pascal, Perl, Pike, PostScript, Prolog, Python, Ruby, Simula, Tcl,
  83. and VHDL@. An alternative mode for Perl is called CPerl mode. Modes are
  84. also available for the scripting languages of the common GNU and Unix
  85. shells, and MS-DOS/MS-Windows @samp{BAT} files, and for makefiles,
  86. DNS master files, and various sorts of configuration files.
  87. Ideally, Emacs should have a major mode for each programming
  88. language that you might want to edit. If it doesn't have a mode for
  89. your favorite language, the mode might be implemented in a package not
  90. distributed with Emacs (@pxref{Packages}); or you can contribute one.
  91. @kindex DEL @r{(programming modes)}
  92. @findex backward-delete-char-untabify
  93. In most programming languages, indentation should vary from line to
  94. line to illustrate the structure of the program. Therefore, in most
  95. programming language modes, typing @key{TAB} updates the indentation
  96. of the current line (@pxref{Program Indent}). Furthermore, @key{DEL}
  97. is usually bound to @code{backward-delete-char-untabify}, which
  98. deletes backward treating each tab as if it were the equivalent number
  99. of spaces, so that you can delete one column of indentation without
  100. worrying whether the whitespace consists of spaces or tabs.
  101. @cindex mode hook
  102. @vindex c-mode-hook
  103. @vindex lisp-mode-hook
  104. @vindex emacs-lisp-mode-hook
  105. @vindex lisp-interaction-mode-hook
  106. @vindex scheme-mode-hook
  107. Entering a programming language mode runs the custom Lisp functions
  108. specified in the hook variable @code{prog-mode-hook}, followed by
  109. those specified in the mode's own mode hook (@pxref{Major Modes}).
  110. For instance, entering C mode runs the hooks @code{prog-mode-hook} and
  111. @code{c-mode-hook}. @xref{Hooks}, for information about hooks.
  112. @ifnottex
  113. Separate manuals are available for the modes for Ada (@pxref{Top,,
  114. Ada Mode, ada-mode, Ada Mode}), C/C++/Objective C/Java/Corba
  115. IDL/Pike/AWK (@pxref{Top, , CC Mode, ccmode, CC Mode}), and IDLWAVE
  116. (@pxref{Top,, IDLWAVE, idlwave, IDLWAVE User Manual}).
  117. @end ifnottex
  118. @iftex
  119. The Emacs distribution contains Info manuals for the major modes for
  120. Ada, C/C++/Objective C/Java/Corba IDL/Pike/AWK, and IDLWAVE@. For
  121. Fortran mode, @pxref{Fortran,,, emacs-xtra, Specialized Emacs Features}.
  122. @end iftex
  123. @node Defuns
  124. @section Top-Level Definitions, or Defuns
  125. In Emacs, a major definition at the top level in the buffer, such as
  126. a function, is called a @dfn{defun}. The name comes from Lisp, but in
  127. Emacs we use it for all languages.
  128. @menu
  129. * Left Margin Paren:: An open-paren or similar opening delimiter
  130. starts a defun if it is at the left margin.
  131. * Moving by Defuns:: Commands to move over or mark a major definition.
  132. * Imenu:: Making buffer indexes as menus.
  133. * Which Function:: Which Function mode shows which function you are in.
  134. @end menu
  135. @node Left Margin Paren
  136. @subsection Left Margin Convention
  137. @cindex open-parenthesis in leftmost column
  138. @cindex ( in leftmost column
  139. Many programming-language modes assume by default that any opening
  140. delimiter found at the left margin is the start of a top-level
  141. definition, or defun. Therefore, @strong{don't put an opening
  142. delimiter at the left margin unless it should have that significance}.
  143. For instance, never put an open-parenthesis at the left margin in a
  144. Lisp file unless it is the start of a top-level list.
  145. The convention speeds up many Emacs operations, which would
  146. otherwise have to scan back to the beginning of the buffer to analyze
  147. the syntax of the code.
  148. If you don't follow this convention, not only will you have trouble
  149. when you explicitly use the commands for motion by defuns; other
  150. features that use them will also give you trouble. This includes the
  151. indentation commands (@pxref{Program Indent}) and Font Lock mode
  152. (@pxref{Font Lock}).
  153. The most likely problem case is when you want an opening delimiter
  154. at the start of a line inside a string. To avoid trouble, put an
  155. escape character (@samp{\}, in C and Emacs Lisp, @samp{/} in some
  156. other Lisp dialects) before the opening delimiter. This will not
  157. affect the contents of the string, but will prevent that opening
  158. delimiter from starting a defun. Here's an example:
  159. @example
  160. (insert "Foo:
  161. \(bar)
  162. ")
  163. @end example
  164. To help you catch violations of this convention, Font Lock mode
  165. highlights confusing opening delimiters (those that ought to be
  166. quoted) in bold red.
  167. @vindex open-paren-in-column-0-is-defun-start
  168. If you need to override this convention, you can do so by setting
  169. the variable @code{open-paren-in-column-0-is-defun-start}.
  170. If this user option is set to @code{t} (the default), opening
  171. parentheses or braces at column zero always start defuns. When it is
  172. @code{nil}, defuns are found by searching for parens or braces at the
  173. outermost level.
  174. Usually, you should leave this option at its default value of
  175. @code{t}. If your buffer contains parentheses or braces in column
  176. zero which don't start defuns, and it is somehow impractical to remove
  177. these parentheses or braces, it might be helpful to set the option to
  178. @code{nil}. Be aware that this might make scrolling and display in
  179. large buffers quite sluggish. Furthermore, the parentheses and braces
  180. must be correctly matched throughout the buffer for it to work
  181. properly.
  182. @node Moving by Defuns
  183. @subsection Moving by Defuns
  184. @cindex defuns
  185. These commands move point or set up the region based on top-level
  186. major definitions, also called @dfn{defuns}.
  187. @table @kbd
  188. @item C-M-a
  189. Move to beginning of current or preceding defun
  190. (@code{beginning-of-defun}).
  191. @item C-M-e
  192. Move to end of current or following defun (@code{end-of-defun}).
  193. @item C-M-h
  194. Put region around whole current or following defun (@code{mark-defun}).
  195. @end table
  196. @cindex move to beginning or end of function
  197. @cindex function, move to beginning or end
  198. @kindex C-M-a
  199. @kindex C-M-e
  200. @kindex C-M-h
  201. @findex beginning-of-defun
  202. @findex end-of-defun
  203. @findex mark-defun
  204. The commands to move to the beginning and end of the current defun
  205. are @kbd{C-M-a} (@code{beginning-of-defun}) and @kbd{C-M-e}
  206. (@code{end-of-defun}). If you repeat one of these commands, or use a
  207. positive numeric argument, each repetition moves to the next defun in
  208. the direction of motion.
  209. @kbd{C-M-a} with a negative argument @minus{}@var{n} moves forward
  210. @var{n} times to the next beginning of a defun. This is not exactly
  211. the same place that @kbd{C-M-e} with argument @var{n} would move to;
  212. the end of this defun is not usually exactly the same place as the
  213. beginning of the following defun. (Whitespace, comments, and perhaps
  214. declarations can separate them.) Likewise, @kbd{C-M-e} with a
  215. negative argument moves back to an end of a defun, which is not quite
  216. the same as @kbd{C-M-a} with a positive argument.
  217. @kindex C-M-h @r{(C mode)}
  218. @findex c-mark-function
  219. To operate on the current defun, use @kbd{C-M-h}
  220. (@code{mark-defun}), which sets the mark at the end of the current
  221. defun and puts point at its beginning. @xref{Marking Objects}. This
  222. is the easiest way to get ready to kill the defun in order to move it
  223. to a different place in the file. If the defun is directly preceded
  224. by comments (with no intervening blank lines), they are marked, too.
  225. If you use the command while point is between defuns, it uses the
  226. following defun. If you use the command while the mark is already
  227. active, it extends the end of the region to include one more defun.
  228. With a prefix argument, it marks that many defuns or extends the
  229. region by the appropriate number of defuns. With negative prefix
  230. argument it marks defuns in the opposite direction and also changes
  231. the direction of selecting for subsequent uses of @code{mark-defun}.
  232. In C mode, @kbd{C-M-h} runs the function @code{c-mark-function},
  233. which is almost the same as @code{mark-defun}; the difference is that
  234. it backs up over the argument declarations, function name and returned
  235. data type so that the entire C function is inside the region. This is
  236. an example of how major modes adjust the standard key bindings so that
  237. they do their standard jobs in a way better fitting a particular
  238. language. Other major modes may replace any or all of these key
  239. bindings for that purpose.
  240. @node Imenu
  241. @subsection Imenu
  242. @cindex index of buffer definitions
  243. @cindex buffer definitions index
  244. The Imenu facility offers a way to find the major definitions in
  245. a file by name. It is also useful in text formatter major modes,
  246. where it treats each chapter, section, etc., as a definition.
  247. (@xref{Xref}, for a more powerful feature that handles multiple files
  248. together.)
  249. @findex imenu
  250. If you type @kbd{M-x imenu}, it reads the name of a definition using
  251. the minibuffer, then moves point to that definition. You can use
  252. completion to specify the name; the command always displays the whole
  253. list of valid names.
  254. @findex imenu-add-menubar-index
  255. Alternatively, you can bind the command @code{imenu} to a mouse
  256. click. Then it displays mouse menus for you to select a definition
  257. name. You can also add the buffer's index to the menu bar by calling
  258. @code{imenu-add-menubar-index}. If you want to have this menu bar
  259. item available for all buffers in a certain major mode, you can do
  260. this by adding @code{imenu-add-menubar-index} to its mode hook. But
  261. if you have done that, you will have to wait a little while each time
  262. you visit a file in that mode, while Emacs finds all the definitions
  263. in that buffer.
  264. @vindex imenu-auto-rescan
  265. When you change the contents of a buffer, if you add or delete
  266. definitions, you can update the buffer's index based on the
  267. new contents by invoking the @samp{*Rescan*} item in the menu.
  268. Rescanning happens automatically if you set @code{imenu-auto-rescan} to
  269. a non-@code{nil} value. There is no need to rescan because of small
  270. changes in the text.
  271. @vindex imenu-sort-function
  272. You can customize the way the menus are sorted by setting the
  273. variable @code{imenu-sort-function}. By default, names are ordered as
  274. they occur in the buffer; if you want alphabetic sorting, use the
  275. symbol @code{imenu--sort-by-name} as the value. You can also
  276. define your own comparison function by writing Lisp code.
  277. Imenu provides the information to guide Which Function mode
  278. @ifnottex
  279. (@pxref{Which Function}).
  280. @end ifnottex
  281. @iftex
  282. (see below).
  283. @end iftex
  284. The Speedbar can also use it (@pxref{Speedbar}).
  285. @node Which Function
  286. @subsection Which Function Mode
  287. @cindex current function name in mode line
  288. Which Function mode is a global minor mode (@pxref{Minor Modes})
  289. which displays the current function name in the mode line, updating it
  290. as you move around in a buffer.
  291. @findex which-function-mode
  292. @vindex which-func-modes
  293. To either enable or disable Which Function mode, use the command
  294. @kbd{M-x which-function-mode}. Which Function mode is a global minor
  295. mode. By default, it takes effect in all major modes that
  296. know how to support it (i.e., all the major modes that support
  297. Imenu). You can restrict it to a specific list of major modes by
  298. changing the value of the variable @code{which-func-modes} from
  299. @code{t} (which means to support all available major modes) to a list
  300. of major mode names.
  301. @node Program Indent
  302. @section Indentation for Programs
  303. @cindex indentation for programs
  304. The best way to keep a program properly indented is to use Emacs to
  305. reindent it as you change it. Emacs has commands to indent either a
  306. single line, a specified number of lines, or all of the lines inside a
  307. single parenthetical grouping.
  308. @xref{Indentation}, for general information about indentation. This
  309. section describes indentation features specific to programming
  310. language modes.
  311. @menu
  312. * Basic Indent:: Indenting a single line.
  313. * Multi-line Indent:: Commands to reindent many lines at once.
  314. * Lisp Indent:: Specifying how each Lisp function should be indented.
  315. * C Indent:: Extra features for indenting C and related modes.
  316. * Custom C Indent:: Controlling indentation style for C and related modes.
  317. @end menu
  318. @cindex pretty-printer
  319. Emacs also provides a Lisp pretty-printer in the @code{pp} package,
  320. which reformats Lisp objects with nice-looking indentation.
  321. @node Basic Indent
  322. @subsection Basic Program Indentation Commands
  323. @table @kbd
  324. @item @key{TAB}
  325. Adjust indentation of current line (@code{indent-for-tab-command}).
  326. @item @key{RET}
  327. Insert a newline, then adjust indentation of following line
  328. (@code{newline}).
  329. @end table
  330. @kindex TAB @r{(programming modes)}
  331. @findex c-indent-command
  332. @findex indent-line-function
  333. @findex indent-for-tab-command
  334. The basic indentation command is @key{TAB}
  335. (@code{indent-for-tab-command}), which was documented in
  336. @ref{Indentation}. In programming language modes, @key{TAB} indents
  337. the current line, based on the indentation and syntactic content of
  338. the preceding lines; if the region is active, @key{TAB} indents each
  339. line within the region, not just the current line.
  340. The command @key{RET} (@code{newline}), which was documented in
  341. @ref{Inserting Text}, does the same as @kbd{C-j} followed by
  342. @key{TAB}: it inserts a new line, then adjusts the line's indentation.
  343. When indenting a line that starts within a parenthetical grouping,
  344. Emacs usually places the start of the line under the preceding line
  345. within the group, or under the text after the parenthesis. If you
  346. manually give one of these lines a nonstandard indentation (e.g., for
  347. aesthetic purposes), the lines below will follow it.
  348. The indentation commands for most programming language modes assume
  349. that a open-parenthesis, open-brace or other opening delimiter at the
  350. left margin is the start of a function. If the code you are editing
  351. violates this assumption---even if the delimiters occur in strings or
  352. comments---you must set @code{open-paren-in-column-0-is-defun-start}
  353. to @code{nil} for indentation to work properly. @xref{Left Margin
  354. Paren}.
  355. @node Multi-line Indent
  356. @subsection Indenting Several Lines
  357. Sometimes, you may want to reindent several lines of code at a time.
  358. One way to do this is to use the mark; when the mark is active and the
  359. region is non-empty, @key{TAB} indents every line in the region.
  360. Alternatively, the command @kbd{C-M-\} (@code{indent-region}) indents
  361. every line in the region, whether or not the mark is active
  362. (@pxref{Indentation Commands}).
  363. In addition, Emacs provides the following commands for indenting
  364. large chunks of code:
  365. @table @kbd
  366. @item C-M-q
  367. Reindent all the lines within one parenthetical grouping.
  368. @item C-u @key{TAB}
  369. Shift an entire parenthetical grouping rigidly sideways so that its
  370. first line is properly indented.
  371. @item M-x indent-code-rigidly
  372. Shift all the lines in the region rigidly sideways, but do not alter
  373. lines that start inside comments and strings.
  374. @end table
  375. @kindex C-M-q
  376. @findex indent-pp-sexp
  377. To reindent the contents of a single parenthetical grouping,
  378. position point before the beginning of the grouping and type
  379. @kbd{C-M-q}. This changes the relative indentation within the
  380. grouping, without affecting its overall indentation (i.e., the
  381. indentation of the line where the grouping starts). The function that
  382. @kbd{C-M-q} runs depends on the major mode; it is
  383. @code{indent-pp-sexp} in Lisp mode, @code{c-indent-exp} in C mode,
  384. etc. To correct the overall indentation as well, type @key{TAB}
  385. first.
  386. @kindex C-u TAB
  387. If you like the relative indentation within a grouping but not the
  388. indentation of its first line, move point to that first line and type
  389. @kbd{C-u @key{TAB}}. In Lisp, C, and some other major modes,
  390. @key{TAB} with a numeric argument reindents the current line as usual,
  391. then reindents by the same amount all the lines in the parenthetical
  392. grouping starting on the current line. It is clever, though, and does
  393. not alter lines that start inside strings. Neither does it alter C
  394. preprocessor lines when in C mode, but it does reindent any
  395. continuation lines that may be attached to them.
  396. @findex indent-code-rigidly
  397. The command @kbd{M-x indent-code-rigidly} rigidly shifts all the
  398. lines in the region sideways, like @code{indent-rigidly} does
  399. (@pxref{Indentation Commands}). It doesn't alter the indentation of
  400. lines that start inside a string, unless the region also starts inside
  401. that string. The prefix arg specifies the number of columns to
  402. indent.
  403. @node Lisp Indent
  404. @subsection Customizing Lisp Indentation
  405. @cindex customizing Lisp indentation
  406. The indentation pattern for a Lisp expression can depend on the function
  407. called by the expression. For each Lisp function, you can choose among
  408. several predefined patterns of indentation, or define an arbitrary one with
  409. a Lisp program.
  410. The standard pattern of indentation is as follows: the second line of the
  411. expression is indented under the first argument, if that is on the same
  412. line as the beginning of the expression; otherwise, the second line is
  413. indented underneath the function name. Each following line is indented
  414. under the previous line whose nesting depth is the same.
  415. @vindex lisp-indent-offset
  416. If the variable @code{lisp-indent-offset} is non-@code{nil}, it overrides
  417. the usual indentation pattern for the second line of an expression, so that
  418. such lines are always indented @code{lisp-indent-offset} more columns than
  419. the containing list.
  420. @vindex lisp-body-indent
  421. Certain functions override the standard pattern. Functions whose
  422. names start with @code{def} treat the second lines as the start of
  423. a @dfn{body}, by indenting the second line @code{lisp-body-indent}
  424. additional columns beyond the open-parenthesis that starts the
  425. expression.
  426. @cindex @code{lisp-indent-function} property
  427. You can override the standard pattern in various ways for individual
  428. functions, according to the @code{lisp-indent-function} property of
  429. the function name. This is normally done for macro definitions, using
  430. the @code{declare} construct. @xref{Defining Macros,,, elisp, the
  431. Emacs Lisp Reference Manual}.
  432. @node C Indent
  433. @subsection Commands for C Indentation
  434. Here are special features for indentation in C mode and related modes:
  435. @table @code
  436. @item C-c C-q
  437. @kindex C-c C-q @r{(C mode)}
  438. @findex c-indent-defun
  439. Reindent the current top-level function definition or aggregate type
  440. declaration (@code{c-indent-defun}).
  441. @item C-M-q
  442. @kindex C-M-q @r{(C mode)}
  443. @findex c-indent-exp
  444. Reindent each line in the balanced expression that follows point
  445. (@code{c-indent-exp}). A prefix argument inhibits warning messages
  446. about invalid syntax.
  447. @item @key{TAB}
  448. @findex c-indent-command
  449. Reindent the current line, and/or in some cases insert a tab character
  450. (@code{c-indent-command}).
  451. @vindex c-tab-always-indent
  452. If @code{c-tab-always-indent} is @code{t}, this command always reindents
  453. the current line and does nothing else. This is the default.
  454. If that variable is @code{nil}, this command reindents the current line
  455. only if point is at the left margin or in the line's indentation;
  456. otherwise, it inserts a tab (or the equivalent number of spaces,
  457. if @code{indent-tabs-mode} is @code{nil}).
  458. Any other value (not @code{nil} or @code{t}) means always reindent the
  459. line, and also insert a tab if within a comment or a string.
  460. @end table
  461. To reindent the whole current buffer, type @kbd{C-x h C-M-\}. This
  462. first selects the whole buffer as the region, then reindents that
  463. region.
  464. To reindent the current block, use @kbd{C-M-u C-M-q}. This moves
  465. to the front of the block and then reindents it all.
  466. @node Custom C Indent
  467. @subsection Customizing C Indentation
  468. @cindex style (for indentation)
  469. C mode and related modes use a flexible mechanism for customizing
  470. indentation. C mode indents a source line in two steps: first it
  471. classifies the line syntactically according to its contents and
  472. context; second, it determines the indentation offset associated by
  473. your selected @dfn{style} with the syntactic construct and adds this
  474. onto the indentation of the @dfn{anchor statement}.
  475. @table @kbd
  476. @item C-c . @var{style} @key{RET}
  477. Select a predefined style @var{style} (@code{c-set-style}).
  478. @end table
  479. A @dfn{style} is a named collection of customizations that can be
  480. used in C mode and the related modes. @ref{Styles,,, ccmode, The CC
  481. Mode Manual}, for a complete description. Emacs comes with several
  482. predefined styles, including @code{gnu}, @code{k&r}, @code{bsd},
  483. @code{stroustrup}, @code{linux}, @code{python}, @code{java},
  484. @code{whitesmith}, @code{ellemtel}, and @code{awk}. Some of these
  485. styles are primarily intended for one language, but any of them can be
  486. used with any of the languages supported by these modes. To find out
  487. what a style looks like, select it and reindent some code, e.g., by
  488. typing @kbd{C-M-q} at the start of a function definition.
  489. @kindex C-c . @r{(C mode)}
  490. @findex c-set-style
  491. To choose a style for the current buffer, use the command @w{@kbd{C-c
  492. .}}. Specify a style name as an argument (case is not significant).
  493. This command affects the current buffer only, and it affects only
  494. future invocations of the indentation commands; it does not reindent
  495. the code already in the buffer. To reindent the whole buffer in the
  496. new style, you can type @kbd{C-x h C-M-\}.
  497. @vindex c-default-style
  498. You can also set the variable @code{c-default-style} to specify the
  499. default style for various major modes. Its value should be either the
  500. style's name (a string) or an alist, in which each element specifies
  501. one major mode and which indentation style to use for it. For
  502. example,
  503. @example
  504. (setq c-default-style
  505. '((java-mode . "java")
  506. (awk-mode . "awk")
  507. (other . "gnu")))
  508. @end example
  509. @noindent
  510. specifies explicit choices for Java and AWK modes, and the default
  511. @samp{gnu} style for the other C-like modes. (These settings are
  512. actually the defaults.) This variable takes effect when you select
  513. one of the C-like major modes; thus, if you specify a new default
  514. style for Java mode, you can make it take effect in an existing Java
  515. mode buffer by typing @kbd{M-x java-mode} there.
  516. The @code{gnu} style specifies the formatting recommended by the GNU
  517. Project for C; it is the default, so as to encourage use of our
  518. recommended style.
  519. @xref{Indentation Engine Basics,,, ccmode, the CC Mode Manual}, and
  520. @ref{Customizing Indentation,,, ccmode, the CC Mode Manual}, for more
  521. information on customizing indentation for C and related modes,
  522. including how to override parts of an existing style and how to define
  523. your own styles.
  524. @findex c-guess
  525. @findex c-guess-install
  526. As an alternative to specifying a style, you can tell Emacs to guess
  527. a style by typing @kbd{M-x c-guess} in a sample code buffer. You can
  528. then apply the guessed style to other buffers with @kbd{M-x
  529. c-guess-install}. @xref{Guessing the Style,,, ccmode, the CC Mode
  530. Manual}, for details.
  531. @node Parentheses
  532. @section Commands for Editing with Parentheses
  533. @findex check-parens
  534. @cindex unbalanced parentheses and quotes
  535. This section describes the commands and features that take advantage
  536. of the parenthesis structure in a program, or help you keep it
  537. balanced.
  538. When talking about these facilities, the term ``parenthesis'' also
  539. includes braces, brackets, or whatever delimiters are defined to match
  540. in pairs. The major mode controls which delimiters are significant,
  541. through the syntax table (@pxref{Syntax Tables,, Syntax Tables, elisp,
  542. The Emacs Lisp Reference Manual}). In Lisp, only parentheses count;
  543. in C, these commands apply to braces and brackets too.
  544. You can use @kbd{M-x check-parens} to find any unbalanced
  545. parentheses and unbalanced string quotes in the buffer.
  546. @menu
  547. * Expressions:: Expressions with balanced parentheses.
  548. * Moving by Parens:: Commands for moving up, down and across
  549. in the structure of parentheses.
  550. * Matching:: Insertion of a close-delimiter flashes matching open.
  551. @end menu
  552. @node Expressions
  553. @subsection Expressions with Balanced Parentheses
  554. @cindex sexp
  555. @cindex expression
  556. @cindex balanced expression
  557. Each programming language mode has its own definition of a
  558. @dfn{balanced expression}. Balanced expressions typically include
  559. individual symbols, numbers, and string constants, as well as pieces
  560. of code enclosed in a matching pair of delimiters. The following
  561. commands deal with balanced expressions (in Emacs, such expressions
  562. are referred to internally as @dfn{sexps}@footnote{The word ``sexp''
  563. is used to refer to an expression in Lisp.}).
  564. @table @kbd
  565. @item C-M-f
  566. Move forward over a balanced expression (@code{forward-sexp}).
  567. @item C-M-b
  568. Move backward over a balanced expression (@code{backward-sexp}).
  569. @item C-M-k
  570. Kill balanced expression forward (@code{kill-sexp}).
  571. @item C-M-t
  572. Transpose expressions (@code{transpose-sexps}).
  573. @item C-M-@@
  574. @itemx C-M-@key{SPC}
  575. Put mark after following expression (@code{mark-sexp}).
  576. @end table
  577. @kindex C-M-f
  578. @kindex C-M-b
  579. @findex forward-sexp
  580. @findex backward-sexp
  581. To move forward over a balanced expression, use @kbd{C-M-f}
  582. (@code{forward-sexp}). If the first significant character after point
  583. is an opening delimiter (e.g., @samp{(}, @samp{[} or @samp{@{} in C),
  584. this command moves past the matching closing delimiter. If the
  585. character begins a symbol, string, or number, the command moves over
  586. that.
  587. The command @kbd{C-M-b} (@code{backward-sexp}) moves backward over a
  588. balanced expression---like @kbd{C-M-f}, but in the reverse direction.
  589. If the expression is preceded by any prefix characters (single-quote,
  590. backquote and comma, in Lisp), the command moves back over them as
  591. well.
  592. @kbd{C-M-f} or @kbd{C-M-b} with an argument repeats that operation
  593. the specified number of times; with a negative argument means to move
  594. in the opposite direction. In most modes, these two commands move
  595. across comments as if they were whitespace. Note that their keys,
  596. @kbd{C-M-f} and @kbd{C-M-b}, are analogous to @kbd{C-f} and @kbd{C-b},
  597. which move by characters (@pxref{Moving Point}), and @kbd{M-f} and
  598. @kbd{M-b}, which move by words (@pxref{Words}).
  599. @cindex killing expressions
  600. @kindex C-M-k
  601. @findex kill-sexp
  602. To kill a whole balanced expression, type @kbd{C-M-k}
  603. (@code{kill-sexp}). This kills the text that @kbd{C-M-f} would move
  604. over.
  605. @cindex transposition of expressions
  606. @kindex C-M-t
  607. @findex transpose-sexps
  608. @kbd{C-M-t} (@code{transpose-sexps}) switches the positions of the
  609. previous balanced expression and the next one. It is analogous to the
  610. @kbd{C-t} command, which transposes characters (@pxref{Transpose}).
  611. An argument to @kbd{C-M-t} serves as a repeat count, moving the
  612. previous expression over that many following ones. A negative
  613. argument moves the previous balanced expression backwards across those
  614. before it. An argument of zero, rather than doing nothing, transposes
  615. the balanced expressions ending at or after point and the mark.
  616. @kindex C-M-@@
  617. @kindex C-M-@key{SPC}
  618. @findex mark-sexp
  619. To operate on balanced expressions with a command which acts on the
  620. region, type @kbd{C-M-@key{SPC}} (@code{mark-sexp}). This sets the
  621. mark where @kbd{C-M-f} would move to. While the mark is active, each
  622. successive call to this command extends the region by shifting the
  623. mark by one expression. Positive or negative numeric arguments move
  624. the mark forward or backward by the specified number of expressions.
  625. The alias @kbd{C-M-@@} is equivalent to @kbd{C-M-@key{SPC}}.
  626. @xref{Marking Objects}, for more information about this and related
  627. commands.
  628. In languages that use infix operators, such as C, it is not possible
  629. to recognize all balanced expressions because there can be multiple
  630. possibilities at a given position. For example, C mode does not treat
  631. @samp{foo + bar} as a single expression, even though it @emph{is} one
  632. C expression; instead, it recognizes @samp{foo} as one expression and
  633. @samp{bar} as another, with the @samp{+} as punctuation between them.
  634. However, C mode recognizes @samp{(foo + bar)} as a single expression,
  635. because of the parentheses.
  636. @node Moving by Parens
  637. @subsection Moving in the Parenthesis Structure
  638. @cindex parenthetical groupings
  639. @cindex parentheses, moving across
  640. @cindex matching parenthesis and braces, moving to
  641. @cindex braces, moving across
  642. @cindex list commands
  643. The following commands move over groupings delimited by parentheses
  644. (or whatever else serves as delimiters in the language you are working
  645. with). They ignore strings and comments, including any parentheses
  646. within them, and also ignore parentheses that are quoted with an
  647. escape character. These commands are mainly intended for editing
  648. programs, but can be useful for editing any text containing
  649. parentheses. They are referred to internally as ``list commands''
  650. because in Lisp these groupings are lists.
  651. These commands assume that the starting point is not inside a string
  652. or a comment. If you invoke them from inside a string or comment, the
  653. results are unreliable.
  654. @table @kbd
  655. @item C-M-n
  656. Move forward over a parenthetical group (@code{forward-list}).
  657. @item C-M-p
  658. Move backward over a parenthetical group (@code{backward-list}).
  659. @item C-M-u
  660. Move up in parenthesis structure (@code{backward-up-list}).
  661. @item C-M-d
  662. Move down in parenthesis structure (@code{down-list}).
  663. @end table
  664. @kindex C-M-n
  665. @kindex C-M-p
  666. @findex forward-list
  667. @findex backward-list
  668. The list commands @kbd{C-M-n} (@code{forward-list}) and
  669. @kbd{C-M-p} (@code{backward-list}) move forward or backward over one
  670. (or @var{n}) parenthetical groupings.
  671. @kindex C-M-u
  672. @findex backward-up-list
  673. @kbd{C-M-n} and @kbd{C-M-p} try to stay at the same level in the
  674. parenthesis structure. To move @emph{up} one (or @var{n}) levels, use
  675. @kbd{C-M-u} (@code{backward-up-list}). @kbd{C-M-u} moves backward up
  676. past one unmatched opening delimiter. A positive argument serves as a
  677. repeat count; a negative argument reverses the direction of motion, so
  678. that the command moves forward and up one or more levels.
  679. @kindex C-M-d
  680. @findex down-list
  681. To move @emph{down} in the parenthesis structure, use @kbd{C-M-d}
  682. (@code{down-list}). In Lisp mode, where @samp{(} is the only opening
  683. delimiter, this is nearly the same as searching for a @samp{(}. An
  684. argument specifies the number of levels to go down.
  685. @node Matching
  686. @subsection Matching Parentheses
  687. @cindex matching parentheses
  688. @cindex parentheses, displaying matches
  689. Emacs has a number of @dfn{parenthesis matching} features, which
  690. make it easy to see how and whether parentheses (or other delimiters)
  691. match up.
  692. Whenever you type a self-inserting character that is a closing
  693. delimiter, Emacs briefly indicates the location of the matching
  694. opening delimiter, provided that is on the screen. If it is not on
  695. the screen, Emacs displays some of the text near it in the echo area.
  696. Either way, you can tell which grouping you are closing off. If the
  697. opening delimiter and closing delimiter are mismatched---such as in
  698. @samp{[x)}---a warning message is displayed in the echo area.
  699. @vindex blink-matching-paren
  700. @vindex blink-matching-paren-distance
  701. @vindex blink-matching-delay
  702. Three variables control the display of matching parentheses:
  703. @itemize @bullet
  704. @item
  705. @code{blink-matching-paren} turns the feature on or off: @code{nil}
  706. disables it, but the default is @code{t} to enable it. Set it to
  707. @code{jump} to make indication work by momentarily moving the cursor
  708. to the matching opening delimiter. Set it to @code{jump-offscreen} to
  709. make the cursor jump, even if the opening delimiter is off screen.
  710. @item
  711. @code{blink-matching-delay} says how many seconds to keep indicating
  712. the matching opening delimiter. This may be an integer or
  713. floating-point number; the default is 1.
  714. @item
  715. @code{blink-matching-paren-distance} specifies how many characters
  716. back to search to find the matching opening delimiter. If the match
  717. is not found in that distance, Emacs stops scanning and nothing is
  718. displayed. The default is 102400.
  719. @end itemize
  720. @cindex Show Paren mode
  721. @cindex highlighting matching parentheses
  722. @findex show-paren-mode
  723. Show Paren mode, a global minor mode, provides a more powerful kind
  724. of automatic matching. Whenever point is before an opening delimiter
  725. or after a closing delimiter, the delimiter, its matching delimiter,
  726. and optionally the text between them are highlighted. To toggle Show
  727. Paren mode, type @kbd{M-x show-paren-mode}. To customize it, type
  728. @kbd{M-x customize-group @key{RET} paren-showing}. The customizable
  729. options which control the operation of this mode include:
  730. @itemize @bullet
  731. @item
  732. @code{show-paren-highlight-open-paren} controls whether to highlight
  733. an open paren when point stands just before it, and hence its position
  734. is marked by the cursor anyway. The default is non-@code{nil} (yes).
  735. @item
  736. @code{show-paren-style} controls whether just the two parens, or also
  737. the space between them get highlighted. The valid options here are
  738. @code{parenthesis} (show the matching paren), @code{expression}
  739. (highlight the entire expression enclosed by the parens), and
  740. @code{mixed} (highlight the matching paren if it is visible, the
  741. expression otherwise).
  742. @item
  743. @code{show-paren-when-point-inside-paren}, when non-@code{nil}, causes
  744. highlighting also when point is on the inside of a parenthesis.
  745. @item
  746. @code{show-paren-when-point-in-periphery}, when non-@code{nil}, causes
  747. highlighting also when point is in whitespace at the beginning or end
  748. of a line, and there is a paren at, respectively, the first or last,
  749. or the last, non-whitespace position on the line.
  750. @end itemize
  751. @cindex Electric Pair mode
  752. @cindex inserting matching parentheses
  753. @findex electric-pair-mode
  754. Electric Pair mode, a global minor mode, provides a way to easily
  755. insert matching delimiters. Whenever you insert an opening delimiter,
  756. the matching closing delimiter is automatically inserted as well,
  757. leaving point between the two. Conversely, when you insert a closing
  758. delimiter over an existing one, no inserting takes places and that
  759. position is simply skipped over. These variables control additional
  760. features of Electric Pair mode:
  761. @itemize @bullet
  762. @item
  763. @vindex electric-pair-preserve-balance
  764. @code{electric-pair-preserve-balance}, when non-@code{nil}, makes the
  765. default pairing logic balance out the number of opening and closing
  766. delimiters.
  767. @item
  768. @vindex electric-pair-delete-adjacent-pairs
  769. @code{electric-pair-delete-adjacent-pairs}, when non-@code{nil}, makes
  770. backspacing between two adjacent delimiters also automatically delete
  771. the closing delimiter.
  772. @item
  773. @vindex electric-pair-open-newline-between-pairs
  774. @code{electric-pair-open-newline-between-pairs}, when non-@code{nil},
  775. makes inserting a newline between two adjacent pairs also
  776. automatically open an extra newline after point.
  777. @item
  778. @vindex electric-pair-skip-whitespace
  779. @code{electric-pair-skip-whitespace}, when non-@code{nil}, causes the minor
  780. mode to skip whitespace forward before deciding whether to skip over
  781. the closing delimiter.
  782. @end itemize
  783. To toggle Electric Pair mode, type @kbd{M-x electric-pair-mode}. To
  784. toggle the mode in a single buffer, use @kbd{M-x
  785. electric-pair-local-mode}.
  786. @node Comments
  787. @section Manipulating Comments
  788. @cindex comments
  789. Because comments are such an important part of programming, Emacs
  790. provides special commands for editing and inserting comments. It can
  791. also do spell checking on comments with Flyspell Prog mode
  792. (@pxref{Spelling}).
  793. Some major modes have special rules for indenting different kinds of
  794. comments. For example, in Lisp code, comments starting with two
  795. semicolons are indented as if they were lines of code, while those
  796. starting with three semicolons are supposed to be aligned to the left
  797. margin and are often used for sectioning purposes. Emacs understand
  798. these conventions; for instance, typing @key{TAB} on a comment line
  799. will indent the comment to the appropriate position.
  800. @example
  801. ;; This function is just an example.
  802. ;;; Here either two or three semicolons are appropriate.
  803. (defun foo (x)
  804. ;;; And now, the first part of the function:
  805. ;; The following line adds one.
  806. (1+ x)) ; This line adds one.
  807. @end example
  808. @menu
  809. * Comment Commands:: Inserting, killing, and aligning comments.
  810. * Multi-Line Comments:: Commands for adding and editing multi-line comments.
  811. * Options for Comments::Customizing the comment features.
  812. @end menu
  813. @node Comment Commands
  814. @subsection Comment Commands
  815. @cindex indentation for comments
  816. @cindex alignment for comments
  817. The following commands operate on comments:
  818. @table @asis
  819. @item @kbd{M-;}
  820. Insert or realign comment on current line; if the region is active,
  821. comment or uncomment the region instead (@code{comment-dwim}).
  822. @item @kbd{C-x C-;}
  823. Comment or uncomment the current line (@code{comment-line}).
  824. @item @kbd{C-u M-;}
  825. Kill comment on current line (@code{comment-kill}).
  826. @item @kbd{C-x ;}
  827. Set comment column (@code{comment-set-column}).
  828. @item @kbd{C-M-j}
  829. @itemx @kbd{M-j}
  830. Like @key{RET} followed by inserting and aligning a comment
  831. (@code{comment-indent-new-line}). @xref{Multi-Line Comments}.
  832. @item @kbd{M-x comment-region}
  833. @itemx @kbd{C-c C-c} (in C-like modes)
  834. Add comment delimiters to all the lines in the region.
  835. @end table
  836. @kindex M-;
  837. @findex comment-dwim
  838. The command to create or align a comment is @kbd{M-;}
  839. (@code{comment-dwim}). The word ``dwim'' is an acronym for ``Do What
  840. I Mean''; it indicates that this command can be used for many
  841. different jobs relating to comments, depending on the situation where
  842. you use it.
  843. When a region is active (@pxref{Mark}), @kbd{M-;} either adds
  844. comment delimiters to the region, or removes them. If every line in
  845. the region is already a comment, it uncomments each of those lines
  846. by removing their comment delimiters. Otherwise, it adds comment
  847. delimiters to enclose the text in the region.
  848. If you supply a prefix argument to @kbd{M-;} when a region is
  849. active, that specifies the number of comment delimiters to add or
  850. delete. A positive argument @var{n} adds @var{n} delimiters, while a
  851. negative argument @var{-n} removes @var{n} delimiters.
  852. If the region is not active, and there is no existing comment on the
  853. current line, @kbd{M-;} adds a new comment to the current line. If
  854. the line is blank (i.e., empty or containing only whitespace
  855. characters), the comment is indented to the same position where
  856. @key{TAB} would indent to (@pxref{Basic Indent}). If the line is
  857. non-blank, the comment is placed after the last non-whitespace
  858. character on the line; normally, Emacs tries putting it at the column
  859. specified by the variable @code{comment-column} (@pxref{Options for
  860. Comments}), but if the line already extends past that column, it puts
  861. the comment at some suitable position, usually separated from the
  862. non-comment text by at least one space. In each case, Emacs places
  863. point after the comment's starting delimiter, so that you can start
  864. typing the comment text right away.
  865. You can also use @kbd{M-;} to align an existing comment. If a line
  866. already contains the comment-start string, @kbd{M-;} realigns it to
  867. the conventional alignment and moves point after the comment's
  868. starting delimiter. As an exception, comments starting in column 0
  869. are not moved. Even when an existing comment is properly aligned,
  870. @kbd{M-;} is still useful for moving directly to the start of the
  871. comment text.
  872. @findex comment-line
  873. @kindex C-x C-;
  874. @kbd{C-x C-;} (@code{comment-line}) comments or uncomments complete
  875. lines. When a region is active (@pxref{Mark}), @kbd{C-x C-;} either
  876. comments or uncomments the lines in the region. If the region is not
  877. active, this command comments or uncomments the line point is on.
  878. With a positive prefix argument @var{n}, it operates on @var{n} lines
  879. starting with the current one; with a negative @var{n}, it affects
  880. @var{n} preceding lines. After invoking this command with a negative
  881. argument, successive invocations with a positive argument will operate
  882. on preceding lines as if the argument were negated.
  883. @findex comment-kill
  884. @kindex C-u M-;
  885. @kbd{C-u M-;} (@code{comment-dwim} with a prefix argument) kills any
  886. comment on the current line, along with the whitespace before it.
  887. Since the comment is saved to the kill ring, you can reinsert it on
  888. another line by moving to the end of that line, doing @kbd{C-y}, and
  889. then @kbd{M-;} to realign the comment. You can achieve the same
  890. effect as @kbd{C-u M-;} by typing @kbd{M-x comment-kill}
  891. (@code{comment-dwim} actually calls @code{comment-kill} as a
  892. subroutine when it is given a prefix argument).
  893. @kindex C-c C-c (C mode)
  894. @findex comment-region
  895. @findex uncomment-region
  896. The command @kbd{M-x comment-region} is equivalent to calling
  897. @kbd{M-;} on an active region, except that it always acts on the
  898. region, even if the mark is inactive. In C mode and related modes,
  899. this command is bound to @kbd{C-c C-c}. The command @kbd{M-x
  900. uncomment-region} uncomments each line in the region; a numeric prefix
  901. argument specifies the number of comment delimiters to remove
  902. (negative arguments specify the number of comment to delimiters to
  903. add).
  904. For C-like modes, you can configure the exact effect of @kbd{M-;} by
  905. setting the variables @code{c-indent-comment-alist} and
  906. @code{c-indent-comments-syntactically-p}. For example, on a line
  907. ending in a closing brace, @kbd{M-;} puts the comment one space after
  908. the brace rather than at @code{comment-column}. For full details see
  909. @ref{Comment Commands,,, ccmode, The CC Mode Manual}.
  910. @node Multi-Line Comments
  911. @subsection Multiple Lines of Comments
  912. @kindex C-M-j
  913. @kindex M-j
  914. @cindex blank lines in programs
  915. @findex comment-indent-new-line
  916. @vindex comment-multi-line
  917. If you are typing a comment and wish to continue it to another line,
  918. type @kbd{M-j} or @kbd{C-M-j} (@code{comment-indent-new-line}). This
  919. breaks the current line, and inserts the necessary comment delimiters
  920. and indentation to continue the comment.
  921. For languages with closing comment delimiters (e.g., @samp{*/} in
  922. C), the exact behavior of @kbd{M-j} depends on the value of the
  923. variable @code{comment-multi-line}. If the value is @code{nil}, the
  924. command closes the comment on the old line and starts a new comment on
  925. the new line. Otherwise, it opens a new line within the current
  926. comment delimiters.
  927. When Auto Fill mode is on, going past the fill column while typing a
  928. comment also continues the comment, in the same way as an explicit
  929. invocation of @kbd{M-j}.
  930. To turn existing lines into comment lines, use @kbd{M-;} with the
  931. region active, or use @kbd{M-x comment-region}
  932. @ifinfo
  933. (@pxref{Comment Commands}).
  934. @end ifinfo
  935. @ifnotinfo
  936. as described in the preceding section.
  937. @end ifnotinfo
  938. You can configure C Mode such that when you type a @samp{/} at the
  939. start of a line in a multi-line block comment, this closes the
  940. comment. Enable the @code{comment-close-slash} clean-up for this.
  941. @xref{Clean-ups,,, ccmode, The CC Mode Manual}.
  942. @node Options for Comments
  943. @subsection Options Controlling Comments
  944. @vindex comment-column
  945. @kindex C-x ;
  946. @findex comment-set-column
  947. As mentioned in @ref{Comment Commands}, when the @kbd{M-j} command
  948. adds a comment to a line, it tries to place the comment at the column
  949. specified by the buffer-local variable @code{comment-column}. You can
  950. set either the local value or the default value of this buffer-local
  951. variable in the usual way (@pxref{Locals}). Alternatively, you can
  952. type @kbd{C-x ;} (@code{comment-set-column}) to set the value of
  953. @code{comment-column} in the current buffer to the column where point
  954. is currently located. @kbd{C-u C-x ;} sets the comment column to
  955. match the last comment before point in the buffer, and then does a
  956. @kbd{M-;} to align the current line's comment under the previous one.
  957. @vindex comment-start-skip
  958. The comment commands recognize comments based on the regular
  959. expression that is the value of the variable @code{comment-start-skip}.
  960. Make sure this regexp does not match the null string. It may match more
  961. than the comment starting delimiter in the strictest sense of the word;
  962. for example, in C mode the value of the variable is
  963. @c This stops M-q from breaking the line inside that @code.
  964. @code{@w{"\\(//+\\|/\\*+\\)\\s *"}}, which matches extra stars and
  965. spaces after the @samp{/*} itself, and accepts C++ style comments
  966. also. (Note that @samp{\\} is needed in Lisp syntax to include a
  967. @samp{\} in the string, which is needed to deny the first star its
  968. special meaning in regexp syntax. @xref{Regexp Backslash}.)
  969. @vindex comment-start
  970. @vindex comment-end
  971. When a comment command makes a new comment, it inserts the value of
  972. @code{comment-start} as an opening comment delimiter. It also inserts
  973. the value of @code{comment-end} after point, as a closing comment
  974. delimiter. For example, in Lisp mode, @code{comment-start} is
  975. @samp{";"} and @code{comment-end} is @code{""} (the empty string). In
  976. C mode, @code{comment-start} is @code{"/* "} and @code{comment-end} is
  977. @code{" */"}.
  978. @vindex comment-padding
  979. The variable @code{comment-padding} specifies a string that the
  980. commenting commands should insert between the comment delimiter(s) and
  981. the comment text. The default, @samp{" "}, specifies a single space.
  982. Alternatively, the value can be a number, which specifies that number
  983. of spaces, or @code{nil}, which means no spaces at all.
  984. The variable @code{comment-multi-line} controls how @kbd{M-j} and
  985. Auto Fill mode continue comments over multiple lines.
  986. @xref{Multi-Line Comments}.
  987. @vindex comment-indent-function
  988. The variable @code{comment-indent-function} should contain a function
  989. that will be called to compute the alignment for a newly inserted
  990. comment or for aligning an existing comment. It is set differently by
  991. various major modes. The function is called with no arguments, but with
  992. point at the beginning of the comment, or at the end of a line if a new
  993. comment is to be inserted. It should return the column in which the
  994. comment ought to start. For example, the default hook function bases
  995. its decision on how many comment characters begin an existing comment.
  996. Emacs also tries to align comments on adjacent lines. To override
  997. this, the function may return a cons of two (possibly equal) integers
  998. to indicate an acceptable range of indentation.
  999. @node Documentation
  1000. @section Documentation Lookup
  1001. Emacs provides several features you can use to look up the
  1002. documentation of functions, variables and commands that you plan to
  1003. use in your program.
  1004. @menu
  1005. * Info Lookup:: Looking up library functions and commands in Info files.
  1006. * Man Page:: Looking up man pages of library functions and commands.
  1007. * Lisp Doc:: Looking up Emacs Lisp functions, etc.
  1008. @end menu
  1009. @node Info Lookup
  1010. @subsection Info Documentation Lookup
  1011. @findex info-lookup-symbol
  1012. @findex info-lookup-file
  1013. @kindex C-h S
  1014. For major modes that apply to languages which have documentation in
  1015. Info, you can use @kbd{C-h S} (@code{info-lookup-symbol}) to view the
  1016. Info documentation for a symbol used in the program. You specify the
  1017. symbol with the minibuffer; the default is the symbol appearing in the
  1018. buffer at point. For example, in C mode this looks for the symbol in
  1019. the C Library Manual. The command only works if the appropriate
  1020. manual's Info files are installed.
  1021. The major mode determines where to look for documentation for the
  1022. symbol---which Info files to look in, and which indices to search.
  1023. You can also use @kbd{M-x info-lookup-file} to look for documentation
  1024. for a file name.
  1025. If you use @kbd{C-h S} in a major mode that does not support it,
  1026. it asks you to specify the symbol help mode. You should enter
  1027. a command such as @code{c-mode} that would select a major
  1028. mode which @kbd{C-h S} does support.
  1029. @node Man Page
  1030. @subsection Man Page Lookup
  1031. @cindex man page
  1032. On Unix, the main form of on-line documentation was the @dfn{manual
  1033. page} or @dfn{man page}. In the GNU operating system, we aim to
  1034. replace man pages with better-organized manuals that you can browse
  1035. with Info (@pxref{Misc Help}). This process is not finished, so it is
  1036. still useful to read manual pages.
  1037. @findex man
  1038. You can read the man page for an operating system command, library
  1039. function, or system call, with the @kbd{M-x man} command. This
  1040. prompts for a topic, with completion (@pxref{Completion}), and runs
  1041. the @command{man} program to format the corresponding man page. If
  1042. the system permits, it runs @command{man} asynchronously, so that you
  1043. can keep on editing while the page is being formatted. The result
  1044. goes in a buffer named @file{*Man @var{topic}*}. These buffers use a
  1045. special major mode, Man mode, that facilitates scrolling and jumping
  1046. to other manual pages. For details, type @kbd{C-h m} while in a Man
  1047. mode buffer.
  1048. @cindex sections of manual pages
  1049. Each man page belongs to one of ten or more @dfn{sections}, each
  1050. named by a digit or by a digit and a letter. Sometimes there are man
  1051. pages with the same name in different sections. To read a man page
  1052. from a specific section, type @samp{@var{topic}(@var{section})} or
  1053. @samp{@var{section} @var{topic}} when @kbd{M-x man} prompts for the
  1054. topic. For example, the man page for the C library function
  1055. @code{chmod} is in section 2, but there is a shell command of the same
  1056. name, whose man page is in section 1; to view the former, type
  1057. @w{@kbd{M-x man @key{RET} chmod(2) @key{RET}}}.
  1058. @vindex Man-switches
  1059. @kindex M-n @r{(Man mode)}
  1060. @kindex M-p @r{(Man mode)}
  1061. If you do not specify a section, @kbd{M-x man} normally displays
  1062. only the first man page found. On some systems, the @code{man}
  1063. program accepts a @samp{-a} command-line option, which tells it to
  1064. display all the man pages for the specified topic. To make use of
  1065. this, change the value of the variable @code{Man-switches} to
  1066. @samp{"-a"}. Then, in the Man mode buffer, you can type @kbd{M-n} and
  1067. @kbd{M-p} to switch between man pages in different sections. The mode
  1068. line shows how many manual pages are available.
  1069. @findex woman
  1070. @cindex manual pages, on MS-DOS/MS-Windows
  1071. An alternative way of reading manual pages is the @kbd{M-x woman}
  1072. command. Unlike @kbd{M-x man}, it does not run any external programs
  1073. to format and display the man pages; the formatting is done by Emacs,
  1074. so it works on systems such as MS-Windows where the @command{man}
  1075. program may be unavailable. It prompts for a man page, and displays
  1076. it in a buffer named @file{*WoMan @var{section} @var{topic}}.
  1077. @kbd{M-x woman} computes the completion list for manpages the first
  1078. time you invoke the command. With a numeric argument, it recomputes
  1079. this list; this is useful if you add or delete manual pages.
  1080. If you type a name of a manual page and @kbd{M-x woman} finds that
  1081. several manual pages by the same name exist in different sections, it
  1082. pops up a window with possible candidates asking you to choose one of
  1083. them.
  1084. For more information about setting up and using @kbd{M-x woman}, see
  1085. @ifinfo
  1086. @ref{Top, WoMan, Browse UN*X Manual Pages WithOut Man, woman, The
  1087. WoMan Manual}.
  1088. @end ifinfo
  1089. @ifnotinfo
  1090. the WoMan Info manual, which is distributed with Emacs.
  1091. @end ifnotinfo
  1092. @node Lisp Doc
  1093. @subsection Emacs Lisp Documentation Lookup
  1094. When editing Emacs Lisp code, you can use the commands @kbd{C-h f}
  1095. (@code{describe-function}) and @kbd{C-h v} (@code{describe-variable})
  1096. to view the built-in documentation for the Lisp functions and
  1097. variables that you want to use. @xref{Name Help}.
  1098. @cindex Eldoc mode
  1099. @findex eldoc-mode
  1100. @findex global-eldoc-mode
  1101. Eldoc is a buffer-local minor mode that helps with looking up Lisp
  1102. documentation. When it is enabled, the echo area displays some useful
  1103. information whenever there is a Lisp function or variable at point;
  1104. for a function, it shows the argument list, and for a variable it
  1105. shows the first line of the variable's documentation string. To
  1106. toggle Eldoc mode, type @kbd{M-x eldoc-mode}. There's also a Global
  1107. Eldoc mode, which is turned on by default, and affects buffers, such
  1108. as @samp{*scratch*}, whose major mode is Emacs Lisp or Lisp
  1109. Interaction (@w{@kbd{M-x global-eldoc-mode}} to turn it off globally).
  1110. @node Hideshow
  1111. @section Hideshow minor mode
  1112. @cindex Hideshow mode
  1113. @cindex mode, Hideshow
  1114. @findex hs-minor-mode
  1115. Hideshow mode is a buffer-local minor mode that allows you to
  1116. selectively display portions of a program, which are referred to as
  1117. @dfn{blocks}. Type @kbd{M-x hs-minor-mode} to toggle this minor mode
  1118. (@pxref{Minor Modes}).
  1119. When you use Hideshow mode to hide a block, the block disappears
  1120. from the screen, to be replaced by an ellipsis (three periods in a
  1121. row). Just what constitutes a block depends on the major mode. In C
  1122. mode and related modes, blocks are delimited by braces, while in Lisp
  1123. mode they are delimited by parentheses. Multi-line comments also
  1124. count as blocks.
  1125. Hideshow mode provides the following commands:
  1126. @findex hs-hide-all
  1127. @findex hs-hide-block
  1128. @findex hs-show-all
  1129. @findex hs-show-block
  1130. @findex hs-show-region
  1131. @findex hs-hide-level
  1132. @findex hs-minor-mode
  1133. @kindex C-c @@ C-h
  1134. @kindex C-c @@ C-s
  1135. @kindex C-c @@ C-M-h
  1136. @kindex C-c @@ C-M-s
  1137. @kindex C-c @@ C-r
  1138. @kindex C-c @@ C-l
  1139. @kindex S-mouse-2
  1140. @table @kbd
  1141. @item C-c @@ C-h
  1142. Hide the current block (@code{hs-hide-block}).
  1143. @item C-c @@ C-s
  1144. Show the current block (@code{hs-show-block}).
  1145. @item C-c @@ C-c
  1146. Either hide or show the current block (@code{hs-toggle-hiding}).
  1147. @item S-mouse-2
  1148. Toggle hiding for the block you click on (@code{hs-mouse-toggle-hiding}).
  1149. @item C-c @@ C-M-h
  1150. Hide all top-level blocks (@code{hs-hide-all}).
  1151. @item C-c @@ C-M-s
  1152. Show all blocks in the buffer (@code{hs-show-all}).
  1153. @item C-c @@ C-l
  1154. Hide all blocks @var{n} levels below this block
  1155. (@code{hs-hide-level}).
  1156. @end table
  1157. @vindex hs-hide-comments-when-hiding-all
  1158. @vindex hs-isearch-open
  1159. @vindex hs-special-modes-alist
  1160. These variables can be used to customize Hideshow mode:
  1161. @table @code
  1162. @item hs-hide-comments-when-hiding-all
  1163. If non-@code{nil}, @kbd{C-c @@ C-M-h} (@code{hs-hide-all}) hides
  1164. comments too.
  1165. @item hs-isearch-open
  1166. This variable specifies the conditions under which incremental search
  1167. should unhide a hidden block when matching text occurs within the
  1168. block. Its value should be either @code{code} (unhide only code
  1169. blocks), @code{comment} (unhide only comments), @code{t} (unhide both
  1170. code blocks and comments), or @code{nil} (unhide neither code blocks
  1171. nor comments). The default value is @code{code}.
  1172. @end table
  1173. @node Symbol Completion
  1174. @section Completion for Symbol Names
  1175. @cindex completion (symbol names)
  1176. Completion is normally done in the minibuffer (@pxref{Completion}),
  1177. but you can also complete symbol names in ordinary Emacs buffers.
  1178. @kindex M-TAB
  1179. @kindex C-M-i
  1180. In programming language modes, type @kbd{C-M-i} or @kbd{M-@key{TAB}}
  1181. to complete the partial symbol before point. On graphical displays,
  1182. the @kbd{M-@key{TAB}} key is usually reserved by the window manager
  1183. for switching graphical windows, so you should type @kbd{C-M-i} or
  1184. @kbd{@key{ESC} @key{TAB}} instead.
  1185. @cindex tags-based completion
  1186. @findex completion-at-point
  1187. @cindex Lisp symbol completion
  1188. @cindex completion (Lisp symbols)
  1189. In most programming language modes, @kbd{C-M-i} (or
  1190. @kbd{M-@key{TAB}}) invokes the command @code{completion-at-point},
  1191. which generates its completion list in a flexible way. If Semantic
  1192. mode is enabled, it tries to use the Semantic parser data for
  1193. completion (@pxref{Semantic}). If Semantic mode is not enabled or
  1194. fails at performing completion, it tries to complete using the
  1195. selected tags table (@pxref{Tags Tables}). If in Emacs Lisp mode, it
  1196. performs completion using the function, variable, or property names
  1197. defined in the current Emacs session.
  1198. In all other respects, in-buffer symbol completion behaves like
  1199. minibuffer completion. For instance, if Emacs cannot complete to a
  1200. unique symbol, it displays a list of completion alternatives in
  1201. another window. @xref{Completion}.
  1202. In Text mode and related modes, @kbd{M-@key{TAB}} completes words
  1203. based on the spell-checker's dictionary. @xref{Spelling}.
  1204. @node MixedCase Words
  1205. @section MixedCase Words
  1206. @cindex camel case
  1207. Some programming styles make use of mixed-case (or ``CamelCase'')
  1208. symbols like @samp{unReadableSymbol}. (In the GNU project, we recommend
  1209. using underscores to separate words within an identifier, rather than
  1210. using case distinctions.) Emacs has various features to make it easier
  1211. to deal with such symbols.
  1212. @cindex Glasses mode
  1213. @findex mode, Glasses
  1214. Glasses mode is a buffer-local minor mode that makes it easier to read
  1215. such symbols, by altering how they are displayed. By default, it
  1216. displays extra underscores between each lower-case letter and the
  1217. following capital letter. This does not alter the buffer text, only how
  1218. it is displayed.
  1219. To toggle Glasses mode, type @kbd{M-x glasses-mode} (@pxref{Minor
  1220. Modes}). When Glasses mode is enabled, the minor mode indicator
  1221. @samp{o^o} appears in the mode line. For more information about
  1222. Glasses mode, type @kbd{C-h P glasses @key{RET}}.
  1223. @cindex Subword mode
  1224. @findex subword-mode
  1225. Subword mode is another buffer-local minor mode. In subword mode,
  1226. Emacs's word commands recognize upper case letters in
  1227. @samp{StudlyCapsIdentifiers} as word boundaries. When Subword mode is
  1228. enabled, the minor mode indicator @samp{,} appears in the mode line.
  1229. See also the similar @code{superword-mode} (@pxref{Misc for Programs}).
  1230. @node Semantic
  1231. @section Semantic
  1232. @cindex Semantic package
  1233. Semantic is a package that provides language-aware editing commands
  1234. based on @code{source code parsers}. This section provides a brief
  1235. description of Semantic; for full details,
  1236. @ifnottex
  1237. see @ref{Top, Semantic,, semantic, Semantic}.
  1238. @end ifnottex
  1239. @iftex
  1240. see the Semantic Info manual, which is distributed with Emacs.
  1241. @end iftex
  1242. Most of the language-aware features in Emacs, such as Font Lock
  1243. mode (@pxref{Font Lock}), rely on rules of thumb@footnote{Regular
  1244. expressions and syntax tables.} that usually give good results but are
  1245. never completely exact. In contrast, the parsers used by Semantic
  1246. have an exact understanding of programming language syntax. This
  1247. allows Semantic to provide search, navigation, and completion commands
  1248. that are powerful and precise.
  1249. @cindex Semantic mode
  1250. @cindex mode, Semantic
  1251. To begin using Semantic, type @kbd{M-x semantic-mode} or click on
  1252. the menu item named @samp{Source Code Parsers (Semantic)} in the
  1253. @samp{Tools} menu. This enables Semantic mode, a global minor mode.
  1254. When Semantic mode is enabled, Emacs automatically attempts to
  1255. parse each file you visit. Currently, Semantic understands C, C++,
  1256. Scheme, Javascript, Java, HTML, and Make. Within each parsed buffer,
  1257. the following commands are available:
  1258. @table @kbd
  1259. @item C-c , j
  1260. @kindex C-c , j
  1261. Prompt for the name of a function defined in the current file, and
  1262. move point there (@code{semantic-complete-jump-local}).
  1263. @item C-c , J
  1264. @kindex C-c , J
  1265. Prompt for the name of a function defined in any file Emacs has
  1266. parsed, and move point there (@code{semantic-complete-jump}).
  1267. @item C-c , @key{SPC}
  1268. @kindex C-c , @key{SPC}
  1269. Display a list of possible completions for the symbol at point
  1270. (@code{semantic-complete-analyze-inline}). This also activates a set
  1271. of special key bindings for choosing a completion: @key{RET} accepts
  1272. the current completion, @kbd{M-n} and @kbd{M-p} cycle through possible
  1273. completions, @key{TAB} completes as far as possible and then cycles,
  1274. and @kbd{C-g} or any other key aborts completion.
  1275. @item C-c , l
  1276. @kindex C-c , l
  1277. Display a list of the possible completions of the symbol at point, in
  1278. another window (@code{semantic-analyze-possible-completions}).
  1279. @end table
  1280. @noindent
  1281. In addition to the above commands, the Semantic package provides a
  1282. variety of other ways to make use of parser information. For
  1283. instance, you can use it to display a list of completions when Emacs
  1284. is idle.
  1285. @ifnottex
  1286. @xref{Top, Semantic,, semantic, Semantic}, for details.
  1287. @end ifnottex
  1288. @node Misc for Programs
  1289. @section Other Features Useful for Editing Programs
  1290. Some Emacs commands that aren't designed specifically for editing
  1291. programs are useful for that nonetheless.
  1292. The Emacs commands that operate on words, sentences and paragraphs
  1293. are useful for editing code. Most symbols names contain words
  1294. (@pxref{Words}), while sentences can be found in strings and comments
  1295. (@pxref{Sentences}). As for paragraphs, they are defined in most
  1296. programming language modes to begin and end at blank lines
  1297. (@pxref{Paragraphs}). Therefore, judicious use of blank lines to make
  1298. the program clearer will also provide useful chunks of text for the
  1299. paragraph commands to work on. Auto Fill mode, if enabled in a
  1300. programming language major mode, indents the new lines which it
  1301. creates.
  1302. @findex superword-mode
  1303. Superword mode is a buffer-local minor mode that causes editing and
  1304. motion commands to treat symbols (e.g., @samp{this_is_a_symbol}) as words.
  1305. When Superword mode is enabled, the minor mode indicator
  1306. @iftex
  1307. @samp{@math{^2}}
  1308. @end iftex
  1309. @ifnottex
  1310. @samp{²}
  1311. @end ifnottex
  1312. appears in the mode line. See also the similar @code{subword-mode}
  1313. (@pxref{MixedCase Words}).
  1314. @findex electric-layout-mode
  1315. Electric Layout mode (@kbd{M-x electric-layout-mode}) is a global
  1316. minor mode that automatically inserts newlines when you type certain
  1317. characters; for example, @samp{@{}, @samp{@}} and @samp{;} in Javascript
  1318. mode.
  1319. Apart from Hideshow mode (@pxref{Hideshow}), another way to
  1320. selectively display parts of a program is to use the selective display
  1321. feature (@pxref{Selective Display}). Programming modes often also
  1322. support Outline minor mode (@pxref{Outline Mode}), which can be used
  1323. with the Foldout package (@pxref{Foldout}).
  1324. @ifinfo
  1325. The automatic typing features may be useful for writing programs.
  1326. @xref{Top,,Autotyping, autotype, Autotyping}.
  1327. @end ifinfo
  1328. @findex prettify-symbols-mode
  1329. Prettify Symbols mode is a buffer-local minor mode that replaces
  1330. certain strings with more attractive versions for display purposes.
  1331. For example, in Emacs Lisp mode, it replaces the string @samp{lambda}
  1332. with the Greek lambda character @samp{λ}. In a @TeX{} buffer, it will
  1333. replace @samp{\alpha} @dots{} @samp{\omega} and other math macros with
  1334. their Unicode characters. You may wish to use this in non-programming
  1335. modes as well. You can customize the mode by adding more entries to
  1336. @code{prettify-symbols-alist}. More elaborate customization is
  1337. available via customizing @code{prettify-symbols-compose-predicate} if
  1338. its default value @code{prettify-symbols-default-compose-p} is not
  1339. appropriate. There is also a global version,
  1340. @code{global-prettify-symbols-mode}, which enables the mode in all
  1341. buffers that support it.
  1342. The symbol at point can be shown in its original form. This is
  1343. controlled by the variable @code{prettify-symbols-unprettify-at-point}:
  1344. if non-@code{nil}, the original form of symbol at point will be
  1345. restored for as long as point is at it.
  1346. @node C Modes
  1347. @section C and Related Modes
  1348. @cindex C mode
  1349. @cindex Java mode
  1350. @cindex Pike mode
  1351. @cindex IDL mode
  1352. @cindex CORBA IDL mode
  1353. @cindex Objective C mode
  1354. @cindex C++ mode
  1355. @cindex AWK mode
  1356. @cindex mode, Java
  1357. @cindex mode, C
  1358. @cindex mode, C++
  1359. @cindex mode, Objective C
  1360. @cindex mode, CORBA IDL
  1361. @cindex mode, Pike
  1362. @cindex mode, AWK
  1363. This section gives a brief description of the special features
  1364. available in C, C++, Objective-C, Java, CORBA IDL, Pike and AWK modes.
  1365. (These are called ``C mode and related modes''.)
  1366. @ifinfo
  1367. @xref{Top,, CC Mode, ccmode, CC Mode}, for more details.
  1368. @end ifinfo
  1369. @ifnotinfo
  1370. For more details, see the CC mode Info manual, which is distributed
  1371. with Emacs.
  1372. @end ifnotinfo
  1373. @menu
  1374. * Motion in C:: Commands to move by C statements, etc.
  1375. * Electric C:: Colon and other chars can automatically reindent.
  1376. * Hungry Delete:: A more powerful DEL command.
  1377. * Other C Commands:: Filling comments, viewing expansion of macros,
  1378. and other neat features.
  1379. @end menu
  1380. @node Motion in C
  1381. @subsection C Mode Motion Commands
  1382. This section describes commands for moving point, in C mode and
  1383. related modes.
  1384. @table @code
  1385. @item C-M-a
  1386. @itemx C-M-e
  1387. @findex c-beginning-of-defun
  1388. @findex c-end-of-defun
  1389. Move point to the beginning or end of the current function or
  1390. top-level definition. In languages with enclosing scopes (such as
  1391. C++'s classes) the @dfn{current function} is the immediate one,
  1392. possibly inside a scope. Otherwise it is the one defined by the least
  1393. enclosing braces. (By contrast, @code{beginning-of-defun} and
  1394. @code{end-of-defun} search for braces in column zero.) @xref{Moving
  1395. by Defuns}.
  1396. @item C-c C-u
  1397. @kindex C-c C-u @r{(C mode)}
  1398. @findex c-up-conditional
  1399. Move point back to the containing preprocessor conditional, leaving the
  1400. mark behind. A prefix argument acts as a repeat count. With a negative
  1401. argument, move point forward to the end of the containing
  1402. preprocessor conditional.
  1403. @samp{#elif} is equivalent to @samp{#else} followed by @samp{#if}, so
  1404. the function will stop at a @samp{#elif} when going backward, but not
  1405. when going forward.
  1406. @item C-c C-p
  1407. @kindex C-c C-p @r{(C mode)}
  1408. @findex c-backward-conditional
  1409. Move point back over a preprocessor conditional, leaving the mark
  1410. behind. A prefix argument acts as a repeat count. With a negative
  1411. argument, move forward.
  1412. @item C-c C-n
  1413. @kindex C-c C-n @r{(C mode)}
  1414. @findex c-forward-conditional
  1415. Move point forward across a preprocessor conditional, leaving the mark
  1416. behind. A prefix argument acts as a repeat count. With a negative
  1417. argument, move backward.
  1418. @item M-a
  1419. @kindex M-a (C mode)
  1420. @findex c-beginning-of-statement
  1421. Move point to the beginning of the innermost C statement
  1422. (@code{c-beginning-of-statement}). If point is already at the beginning
  1423. of a statement, move to the beginning of the preceding statement. With
  1424. prefix argument @var{n}, move back @var{n} @minus{} 1 statements.
  1425. In comments or in strings which span more than one line, this command
  1426. moves by sentences instead of statements.
  1427. @item M-e
  1428. @kindex M-e (C mode)
  1429. @findex c-end-of-statement
  1430. Move point to the end of the innermost C statement or sentence; like
  1431. @kbd{M-a} except that it moves in the other direction
  1432. (@code{c-end-of-statement}).
  1433. @end table
  1434. @node Electric C
  1435. @subsection Electric C Characters
  1436. In C mode and related modes, certain printing characters are
  1437. @dfn{electric}---in addition to inserting themselves, they also
  1438. reindent the current line, and optionally also insert newlines. The
  1439. electric characters are @kbd{@{}, @kbd{@}}, @kbd{:}, @kbd{#},
  1440. @kbd{;}, @kbd{,}, @kbd{<}, @kbd{>}, @kbd{/}, @kbd{*}, @kbd{(}, and
  1441. @kbd{)}.
  1442. You might find electric indentation inconvenient if you are editing
  1443. chaotically indented code. If you are new to CC Mode, you might find
  1444. it disconcerting. You can toggle electric action with the command
  1445. @kbd{C-c C-l}; when it is enabled, @samp{/l} appears in the mode line
  1446. after the mode name:
  1447. @table @kbd
  1448. @item C-c C-l
  1449. @kindex C-c C-l @r{(C mode)}
  1450. @findex c-toggle-electric-state
  1451. Toggle electric action (@code{c-toggle-electric-state}). With a
  1452. positive prefix argument, this command enables electric action, with a
  1453. negative one it disables it.
  1454. @end table
  1455. Electric characters insert newlines only when, in addition to the
  1456. electric state, the @dfn{auto-newline} feature is enabled (indicated
  1457. by @samp{/la} in the mode line after the mode name). You can turn
  1458. this feature on or off with the command @kbd{C-c C-a}:
  1459. @table @kbd
  1460. @item C-c C-a
  1461. @kindex C-c C-a @r{(C mode)}
  1462. @findex c-toggle-auto-newline
  1463. Toggle the auto-newline feature (@code{c-toggle-auto-newline}). With a
  1464. prefix argument, this command turns the auto-newline feature on if the
  1465. argument is positive, and off if it is negative.
  1466. @end table
  1467. Usually the CC Mode style configures the exact circumstances in
  1468. which Emacs inserts auto-newlines. You can also configure this
  1469. directly. @xref{Custom Auto-newlines,,, ccmode, The CC Mode Manual}.
  1470. @node Hungry Delete
  1471. @subsection Hungry Delete Feature in C
  1472. @cindex hungry deletion (C Mode)
  1473. If you want to delete an entire block of whitespace at point, you
  1474. can use @dfn{hungry deletion}. This deletes all the contiguous
  1475. whitespace either before point or after point in a single operation.
  1476. @dfn{Whitespace} here includes tabs and newlines, but not comments or
  1477. preprocessor commands.
  1478. @table @kbd
  1479. @item C-c C-@key{DEL}
  1480. @itemx C-c @key{DEL}
  1481. @findex c-hungry-delete-backwards
  1482. @kindex C-c C-@key{DEL} (C Mode)
  1483. @kindex C-c @key{DEL} (C Mode)
  1484. Delete the entire block of whitespace preceding point (@code{c-hungry-delete-backwards}).
  1485. @item C-c C-d
  1486. @itemx C-c C-@key{Delete}
  1487. @itemx C-c @key{Delete}
  1488. @findex c-hungry-delete-forward
  1489. @kindex C-c C-d (C Mode)
  1490. @kindex C-c C-@key{Delete} (C Mode)
  1491. @kindex C-c @key{Delete} (C Mode)
  1492. Delete the entire block of whitespace after point (@code{c-hungry-delete-forward}).
  1493. @end table
  1494. As an alternative to the above commands, you can enable @dfn{hungry
  1495. delete mode}. When this feature is enabled (indicated by @samp{/h} in
  1496. the mode line after the mode name), a single @key{DEL} deletes all
  1497. preceding whitespace, not just one space, and a single @kbd{C-d}
  1498. (but @emph{not} plain @key{Delete}) deletes all following whitespace.
  1499. @table @kbd
  1500. @item M-x c-toggle-hungry-state
  1501. @findex c-toggle-hungry-state
  1502. Toggle the hungry-delete feature
  1503. (@code{c-toggle-hungry-state}). With a prefix argument,
  1504. this command turns the hungry-delete feature on if the argument is
  1505. positive, and off if it is negative.
  1506. @end table
  1507. @vindex c-hungry-delete-key
  1508. The variable @code{c-hungry-delete-key} controls whether the
  1509. hungry-delete feature is enabled.
  1510. @node Other C Commands
  1511. @subsection Other Commands for C Mode
  1512. @table @kbd
  1513. @item M-x c-context-line-break
  1514. @findex c-context-line-break
  1515. This command inserts a line break and indents the new line in a manner
  1516. appropriate to the context. In normal code, it does the work of
  1517. @key{RET} (@code{newline}), in a C preprocessor line it additionally
  1518. inserts a @samp{\} at the line break, and within comments it's like
  1519. @kbd{M-j} (@code{c-indent-new-comment-line}).
  1520. @code{c-context-line-break} isn't bound to a key by default, but it
  1521. needs a binding to be useful. The following code will bind it to
  1522. @key{RET}. We use @code{c-initialization-hook} here to make sure
  1523. the keymap is loaded before we try to change it.
  1524. @example
  1525. (defun my-bind-clb ()
  1526. (define-key c-mode-base-map "\C-m"
  1527. 'c-context-line-break))
  1528. (add-hook 'c-initialization-hook 'my-bind-clb)
  1529. @end example
  1530. @item C-M-h
  1531. Put mark at the end of a function definition, and put point at the
  1532. beginning (@code{c-mark-function}).
  1533. @item M-q
  1534. @kindex M-q @r{(C mode)}
  1535. @findex c-fill-paragraph
  1536. Fill a paragraph, handling C and C++ comments (@code{c-fill-paragraph}).
  1537. If any part of the current line is a comment or within a comment, this
  1538. command fills the comment or the paragraph of it that point is in,
  1539. preserving the comment indentation and comment delimiters.
  1540. @item C-c C-e
  1541. @cindex macro expansion in C
  1542. @cindex expansion of C macros
  1543. @findex c-macro-expand
  1544. @kindex C-c C-e @r{(C mode)}
  1545. Run the C preprocessor on the text in the region, and show the result,
  1546. which includes the expansion of all the macro calls
  1547. (@code{c-macro-expand}). The buffer text before the region is also
  1548. included in preprocessing, for the sake of macros defined there, but the
  1549. output from this part isn't shown.
  1550. When you are debugging C code that uses macros, sometimes it is hard to
  1551. figure out precisely how the macros expand. With this command, you
  1552. don't have to figure it out; you can see the expansions.
  1553. @item C-c C-\
  1554. @findex c-backslash-region
  1555. @kindex C-c C-\ @r{(C mode)}
  1556. Insert or align @samp{\} characters at the ends of the lines of the
  1557. region (@code{c-backslash-region}). This is useful after writing or
  1558. editing a C macro definition.
  1559. If a line already ends in @samp{\}, this command adjusts the amount of
  1560. whitespace before it. Otherwise, it inserts a new @samp{\}. However,
  1561. the last line in the region is treated specially; no @samp{\} is
  1562. inserted on that line, and any @samp{\} there is deleted.
  1563. @item M-x cpp-highlight-buffer
  1564. @cindex preprocessor highlighting
  1565. @findex cpp-highlight-buffer
  1566. Highlight parts of the text according to its preprocessor conditionals.
  1567. This command displays another buffer named @file{*CPP Edit*}, which
  1568. serves as a graphic menu for selecting how to display particular kinds
  1569. of conditionals and their contents. After changing various settings,
  1570. click on @samp{[A]pply these settings} (or go to that buffer and type
  1571. @kbd{a}) to rehighlight the C mode buffer accordingly.
  1572. @item C-c C-s
  1573. @findex c-show-syntactic-information
  1574. @kindex C-c C-s @r{(C mode)}
  1575. Display the syntactic information about the current source line
  1576. (@code{c-show-syntactic-information}). This information directs how
  1577. the line is indented.
  1578. @item M-x cwarn-mode
  1579. @itemx M-x global-cwarn-mode
  1580. @findex cwarn-mode
  1581. @findex global-cwarn-mode
  1582. @vindex global-cwarn-mode
  1583. @cindex CWarn mode
  1584. @cindex suspicious constructions in C, C++
  1585. CWarn minor mode highlights certain suspicious C and C++ constructions:
  1586. @itemize @bullet{}
  1587. @item
  1588. Assignments inside expressions.
  1589. @item
  1590. Semicolon following immediately after @samp{if}, @samp{for}, and @samp{while}
  1591. (except after a @samp{do @dots{} while} statement);
  1592. @item
  1593. C++ functions with reference parameters.
  1594. @end itemize
  1595. @noindent
  1596. You can enable the mode for one buffer with the command @kbd{M-x
  1597. cwarn-mode}, or for all suitable buffers with the command @kbd{M-x
  1598. global-cwarn-mode} or by customizing the variable
  1599. @code{global-cwarn-mode}. You must also enable Font Lock mode to make
  1600. it work.
  1601. @item M-x hide-ifdef-mode
  1602. @findex hide-ifdef-mode
  1603. @cindex Hide-ifdef mode
  1604. @vindex hide-ifdef-shadow
  1605. Hide-ifdef minor mode hides selected code within @samp{#if} and
  1606. @samp{#ifdef} preprocessor blocks. If you change the variable
  1607. @code{hide-ifdef-shadow} to @code{t}, Hide-ifdef minor mode
  1608. shadows preprocessor blocks by displaying them with a less
  1609. prominent face, instead of hiding them entirely. See the
  1610. documentation string of @code{hide-ifdef-mode} for more information.
  1611. @item M-x ff-find-related-file
  1612. @cindex related files
  1613. @findex ff-find-related-file
  1614. @vindex ff-related-file-alist
  1615. Find a file related in a special way to the file visited by the
  1616. current buffer. Typically this will be the header file corresponding
  1617. to a C/C++ source file, or vice versa. The variable
  1618. @code{ff-related-file-alist} specifies how to compute related file
  1619. names.
  1620. @end table
  1621. @node Asm Mode
  1622. @section Asm Mode
  1623. @cindex Asm mode
  1624. @cindex assembler mode
  1625. Asm mode is a major mode for editing files of assembler code. It
  1626. defines these commands:
  1627. @table @kbd
  1628. @item @key{TAB}
  1629. @code{tab-to-tab-stop}.
  1630. @c FIXME: Maybe this should be consistent with other programming modes.
  1631. @item C-j
  1632. Insert a newline and then indent using @code{tab-to-tab-stop}.
  1633. @item :
  1634. Insert a colon and then remove the indentation from before the label
  1635. preceding colon. Then do @code{tab-to-tab-stop}.
  1636. @item ;
  1637. Insert or align a comment.
  1638. @end table
  1639. The variable @code{asm-comment-char} specifies which character
  1640. starts comments in assembler syntax.
  1641. @ifnottex
  1642. @include fortran-xtra.texi
  1643. @end ifnottex