cc-cmds.el 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716
  1. ;;; cc-cmds.el --- user level commands for CC Mode
  2. ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
  3. ;; Authors: 2003- Alan Mackenzie
  4. ;; 1998- Martin Stjernholm
  5. ;; 1992-1999 Barry A. Warsaw
  6. ;; 1987 Dave Detlefs
  7. ;; 1987 Stewart Clamen
  8. ;; 1985 Richard M. Stallman
  9. ;; Maintainer: bug-cc-mode@gnu.org
  10. ;; Created: 22-Apr-1997 (split from cc-mode.el)
  11. ;; Keywords: c languages
  12. ;; Package: cc-mode
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;;; Code:
  26. (eval-when-compile
  27. (let ((load-path
  28. (if (and (boundp 'byte-compile-dest-file)
  29. (stringp byte-compile-dest-file))
  30. (cons (file-name-directory byte-compile-dest-file) load-path)
  31. load-path)))
  32. (load "cc-bytecomp" nil t)))
  33. (cc-require 'cc-defs)
  34. (cc-require 'cc-vars)
  35. (cc-require 'cc-engine)
  36. ;; Silence the compiler.
  37. (cc-bytecomp-defun delete-forward-p) ; XEmacs
  38. (cc-bytecomp-defvar filladapt-mode) ; c-fill-paragraph contains a kludge
  39. ; which looks at this.
  40. ;; Indentation / Display syntax functions
  41. (defvar c-fix-backslashes t)
  42. (defun c-indent-line (&optional syntax quiet ignore-point-pos)
  43. "Indent the current line according to the syntactic context,
  44. if `c-syntactic-indentation' is non-nil. Optional SYNTAX is the
  45. syntactic information for the current line. Be silent about syntactic
  46. errors if the optional argument QUIET is non-nil, even if
  47. `c-report-syntactic-errors' is non-nil. Normally the position of
  48. point is used to decide where the old indentation is on a lines that
  49. is otherwise empty \(ignoring any line continuation backslash), but
  50. that's not done if IGNORE-POINT-POS is non-nil. Returns the amount of
  51. indentation change \(in columns)."
  52. (let ((line-cont-backslash (save-excursion
  53. (end-of-line)
  54. (eq (char-before) ?\\)))
  55. (c-fix-backslashes c-fix-backslashes)
  56. bs-col
  57. shift-amt)
  58. (when (and (not ignore-point-pos)
  59. (save-excursion
  60. (beginning-of-line)
  61. (looking-at (if line-cont-backslash
  62. ;; Don't use "\\s " - ^L doesn't count as WS
  63. ;; here
  64. "\\([ \t]*\\)\\\\$"
  65. "\\([ \t]*\\)$")))
  66. (<= (point) (match-end 1)))
  67. ;; Delete all whitespace after point if there's only whitespace
  68. ;; on the line, so that any code that does back-to-indentation
  69. ;; or similar gets the current column in this case. If this
  70. ;; removes a line continuation backslash it'll be restored
  71. ;; at the end.
  72. (unless c-auto-align-backslashes
  73. ;; Should try to keep the backslash alignment
  74. ;; in this case.
  75. (save-excursion
  76. (goto-char (match-end 0))
  77. (setq bs-col (1- (current-column)))))
  78. (delete-region (point) (match-end 0))
  79. (setq c-fix-backslashes t))
  80. (if c-syntactic-indentation
  81. (setq c-parsing-error
  82. (or (let ((c-parsing-error nil)
  83. (c-syntactic-context
  84. (or syntax
  85. (and (boundp 'c-syntactic-context)
  86. c-syntactic-context))))
  87. (c-save-buffer-state (indent)
  88. (unless c-syntactic-context
  89. (setq c-syntactic-context (c-guess-basic-syntax)))
  90. (setq indent (c-get-syntactic-indentation
  91. c-syntactic-context))
  92. (and (not (c-echo-parsing-error quiet))
  93. c-echo-syntactic-information-p
  94. (message "syntax: %s, indent: %d"
  95. c-syntactic-context indent))
  96. (setq shift-amt (- indent (current-indentation))))
  97. (c-shift-line-indentation shift-amt)
  98. (run-hooks 'c-special-indent-hook)
  99. c-parsing-error)
  100. c-parsing-error))
  101. (let ((indent 0))
  102. (save-excursion
  103. (while (and (= (forward-line -1) 0)
  104. (if (looking-at "\\s *\\\\?$")
  105. t
  106. (setq indent (current-indentation))
  107. nil))))
  108. (setq shift-amt (- indent (current-indentation)))
  109. (c-shift-line-indentation shift-amt)))
  110. (when (and c-fix-backslashes line-cont-backslash)
  111. (if bs-col
  112. (save-excursion
  113. (indent-to bs-col)
  114. (insert ?\\))
  115. (when c-auto-align-backslashes
  116. ;; Realign the line continuation backslash.
  117. (c-backslash-region (point) (point) nil t))))
  118. shift-amt))
  119. (defun c-newline-and-indent (&optional newline-arg)
  120. "Insert a newline and indent the new line.
  121. This function fixes line continuation backslashes if inside a macro,
  122. and takes care to set the indentation before calling
  123. `indent-according-to-mode', so that lineup functions like
  124. `c-lineup-dont-change' works better."
  125. ;; TODO: Backslashes before eol in comments and literals aren't
  126. ;; kept intact.
  127. (let ((c-macro-start (c-query-macro-start))
  128. ;; Avoid calling c-backslash-region from c-indent-line if it's
  129. ;; called during the newline call, which can happen due to
  130. ;; c-electric-continued-statement, for example. We also don't
  131. ;; want any backslash alignment from indent-according-to-mode.
  132. (c-fix-backslashes nil)
  133. has-backslash insert-backslash
  134. start col)
  135. (save-excursion
  136. (beginning-of-line)
  137. (setq start (point))
  138. (while (and (looking-at "[ \t]*\\\\?$")
  139. (= (forward-line -1) 0)))
  140. (setq col (current-indentation)))
  141. (when c-macro-start
  142. (if (and (eolp) (eq (char-before) ?\\))
  143. (setq insert-backslash t
  144. has-backslash t)
  145. (setq has-backslash (eq (char-before (c-point 'eol)) ?\\))))
  146. (newline newline-arg)
  147. (indent-to col)
  148. (when c-macro-start
  149. (if insert-backslash
  150. (progn
  151. ;; The backslash stayed on the previous line. Insert one
  152. ;; before calling c-backslash-region, so that
  153. ;; bs-col-after-end in it works better. Fixup the
  154. ;; backslashes on the newly inserted line.
  155. (insert ?\\)
  156. (backward-char)
  157. (c-backslash-region (point) (point) nil t))
  158. ;; The backslash moved to the new line, if there was any. Let
  159. ;; c-backslash-region fix a backslash on the previous line,
  160. ;; and the one that might be on the new line.
  161. ;; c-auto-align-backslashes is intentionally ignored here;
  162. ;; maybe the moved backslash should be left alone if it's set,
  163. ;; but we fix both lines on the grounds that the old backslash
  164. ;; has been moved anyway and is now in a different context.
  165. (c-backslash-region start (if has-backslash (point) start) nil t)))
  166. (when c-syntactic-indentation
  167. ;; Reindent syntactically. The indentation done above is not
  168. ;; wasted, since c-indent-line might look at the current
  169. ;; indentation.
  170. (let ((c-syntactic-context (c-save-buffer-state nil
  171. (c-guess-basic-syntax))))
  172. ;; We temporarily insert another line break, so that the
  173. ;; lineup functions will see the line as empty. That makes
  174. ;; e.g. c-lineup-cpp-define more intuitive since it then
  175. ;; proceeds to the preceding line in this case.
  176. (insert ?\n)
  177. (delete-horizontal-space)
  178. (setq start (- (point-max) (point)))
  179. (unwind-protect
  180. (progn
  181. (backward-char)
  182. (indent-according-to-mode))
  183. (goto-char (- (point-max) start))
  184. (delete-char -1)))
  185. (when has-backslash
  186. ;; Must align the backslash again after reindentation. The
  187. ;; c-backslash-region call above can't be optimized to ignore
  188. ;; this line, since it then won't align correctly with the
  189. ;; lines below if the first line in the macro is broken.
  190. (c-backslash-region (point) (point) nil t)))))
  191. (defun c-show-syntactic-information (arg)
  192. "Show syntactic information for current line.
  193. With universal argument, inserts the analysis as a comment on that line."
  194. (interactive "P")
  195. (let* ((c-parsing-error nil)
  196. (syntax (if (boundp 'c-syntactic-context)
  197. ;; Use `c-syntactic-context' in the same way as
  198. ;; `c-indent-line', to be consistent.
  199. c-syntactic-context
  200. (c-save-buffer-state nil
  201. (c-guess-basic-syntax)))))
  202. (if (not (consp arg))
  203. (let (elem pos ols)
  204. (message "Syntactic analysis: %s" syntax)
  205. (unwind-protect
  206. (progn
  207. (while syntax
  208. (setq elem (pop syntax))
  209. (when (setq pos (c-langelem-pos elem))
  210. (push (c-put-overlay pos (1+ pos)
  211. 'face 'highlight)
  212. ols))
  213. (when (setq pos (c-langelem-2nd-pos elem))
  214. (push (c-put-overlay pos (1+ pos)
  215. 'face 'secondary-selection)
  216. ols)))
  217. (sit-for 10))
  218. (while ols
  219. (c-delete-overlay (pop ols)))))
  220. (indent-for-comment)
  221. (insert-and-inherit (format "%s" syntax))
  222. ))
  223. (c-keep-region-active))
  224. (defun c-syntactic-information-on-region (from to)
  225. "Insert a comment with the syntactic analysis on every line in the region."
  226. (interactive "*r")
  227. (save-excursion
  228. (save-restriction
  229. (narrow-to-region from to)
  230. (goto-char (point-min))
  231. (while (not (eobp))
  232. (c-show-syntactic-information '(0))
  233. (forward-line)))))
  234. ;; Minor mode functions.
  235. (defun c-update-modeline ()
  236. (let ((fmt (format "/%s%s%s%s"
  237. (if c-electric-flag "l" "")
  238. (if (and c-electric-flag c-auto-newline)
  239. "a" "")
  240. (if c-hungry-delete-key "h" "")
  241. (if (and
  242. ;; subword might not be loaded.
  243. (boundp 'subword-mode)
  244. (symbol-value 'subword-mode))
  245. "w"
  246. "")))
  247. ;; FIXME: Derived modes might want to use something else
  248. ;; than a string for `mode-name'.
  249. (bare-mode-name (if (string-match "\\(^[^/]*\\)/" mode-name)
  250. (match-string 1 mode-name)
  251. mode-name)))
  252. ;; (setq c-submode-indicators
  253. ;; (if (> (length fmt) 1)
  254. ;; fmt))
  255. (setq mode-name
  256. (if (> (length fmt) 1)
  257. (concat bare-mode-name fmt)
  258. bare-mode-name))
  259. (force-mode-line-update)))
  260. (defun c-toggle-syntactic-indentation (&optional arg)
  261. "Toggle syntactic indentation.
  262. Optional numeric ARG, if supplied, turns on syntactic indentation when
  263. positive, turns it off when negative, and just toggles it when zero or
  264. left out.
  265. When syntactic indentation is turned on (the default), the indentation
  266. functions and the electric keys indent according to the syntactic
  267. context keys, when applicable.
  268. When it's turned off, the electric keys don't reindent, the indentation
  269. functions indents every new line to the same level as the previous
  270. nonempty line, and \\[c-indent-command] adjusts the indentation in steps
  271. specified by `c-basic-offset'. The indentation style has no effect in
  272. this mode, nor any of the indentation associated variables,
  273. e.g. `c-special-indent-hook'.
  274. This command sets the variable `c-syntactic-indentation'."
  275. (interactive "P")
  276. (setq c-syntactic-indentation
  277. (c-calculate-state arg c-syntactic-indentation))
  278. (c-keep-region-active))
  279. (defun c-toggle-auto-newline (&optional arg)
  280. "Toggle auto-newline feature.
  281. Optional numeric ARG, if supplied, turns on auto-newline when
  282. positive, turns it off when negative, and just toggles it when zero or
  283. left out.
  284. Turning on auto-newline automatically enables electric indentation.
  285. When the auto-newline feature is enabled (indicated by \"/la\" on the
  286. mode line after the mode name) newlines are automatically inserted
  287. after special characters such as brace, comma, semi-colon, and colon."
  288. (interactive "P")
  289. (setq c-auto-newline
  290. (c-calculate-state arg (and c-auto-newline c-electric-flag)))
  291. (if c-auto-newline (setq c-electric-flag t))
  292. (c-update-modeline)
  293. (c-keep-region-active))
  294. (defalias 'c-toggle-auto-state 'c-toggle-auto-newline)
  295. (make-obsolete 'c-toggle-auto-state 'c-toggle-auto-newline "22.1")
  296. (defun c-toggle-hungry-state (&optional arg)
  297. "Toggle hungry-delete-key feature.
  298. Optional numeric ARG, if supplied, turns on hungry-delete when
  299. positive, turns it off when negative, and just toggles it when zero or
  300. left out.
  301. When the hungry-delete-key feature is enabled (indicated by \"/h\" on
  302. the mode line after the mode name) the delete key gobbles all preceding
  303. whitespace in one fell swoop."
  304. (interactive "P")
  305. (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
  306. (c-update-modeline)
  307. (c-keep-region-active))
  308. (defun c-toggle-auto-hungry-state (&optional arg)
  309. "Toggle auto-newline and hungry-delete-key features.
  310. Optional numeric ARG, if supplied, turns on auto-newline and
  311. hungry-delete when positive, turns them off when negative, and just
  312. toggles them when zero or left out.
  313. See `c-toggle-auto-newline' and `c-toggle-hungry-state' for details."
  314. (interactive "P")
  315. (setq c-auto-newline (c-calculate-state arg c-auto-newline))
  316. (setq c-hungry-delete-key (c-calculate-state arg c-hungry-delete-key))
  317. (c-update-modeline)
  318. (c-keep-region-active))
  319. (defun c-toggle-electric-state (&optional arg)
  320. "Toggle the electric indentation feature.
  321. Optional numeric ARG, if supplied, turns on electric indentation when
  322. positive, turns it off when negative, and just toggles it when zero or
  323. left out."
  324. (interactive "P")
  325. (setq c-electric-flag (c-calculate-state arg c-electric-flag))
  326. (c-update-modeline)
  327. (c-keep-region-active))
  328. ;; Electric keys
  329. (defun c-electric-backspace (arg)
  330. "Delete the preceding character or whitespace.
  331. If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
  332. line) then all preceding whitespace is consumed. If however a prefix
  333. argument is supplied, or `c-hungry-delete-key' is nil, or point is
  334. inside a literal then the function in the variable
  335. `c-backspace-function' is called."
  336. (interactive "*P")
  337. (if (c-save-buffer-state ()
  338. (or (not c-hungry-delete-key)
  339. arg
  340. (c-in-literal)))
  341. (funcall c-backspace-function (prefix-numeric-value arg))
  342. (c-hungry-delete-backwards)))
  343. (defun c-hungry-delete-backwards ()
  344. "Delete the preceding character or all preceding whitespace
  345. back to the previous non-whitespace character.
  346. See also \\[c-hungry-delete-forward]."
  347. (interactive)
  348. (let ((here (point)))
  349. (c-skip-ws-backward)
  350. (if (/= (point) here)
  351. (delete-region (point) here)
  352. (funcall c-backspace-function 1))))
  353. (defalias 'c-hungry-backspace 'c-hungry-delete-backwards)
  354. (defun c-electric-delete-forward (arg)
  355. "Delete the following character or whitespace.
  356. If `c-hungry-delete-key' is non-nil (indicated by \"/h\" on the mode
  357. line) then all following whitespace is consumed. If however a prefix
  358. argument is supplied, or `c-hungry-delete-key' is nil, or point is
  359. inside a literal then the function in the variable `c-delete-function'
  360. is called."
  361. (interactive "*P")
  362. (if (c-save-buffer-state ()
  363. (or (not c-hungry-delete-key)
  364. arg
  365. (c-in-literal)))
  366. (funcall c-delete-function (prefix-numeric-value arg))
  367. (c-hungry-delete-forward)))
  368. (defun c-hungry-delete-forward ()
  369. "Delete the following character or all following whitespace
  370. up to the next non-whitespace character.
  371. See also \\[c-hungry-delete-backwards]."
  372. (interactive)
  373. (let ((here (point)))
  374. (c-skip-ws-forward)
  375. (if (/= (point) here)
  376. (delete-region (point) here)
  377. (funcall c-delete-function 1))))
  378. ;; This function is only used in XEmacs.
  379. (defun c-electric-delete (arg)
  380. "Deletes preceding or following character or whitespace.
  381. This function either deletes forward as `c-electric-delete-forward' or
  382. backward as `c-electric-backspace', depending on the configuration: If
  383. the function `delete-forward-p' is defined and returns non-nil, it
  384. deletes forward. Otherwise it deletes backward.
  385. Note: This is the way in XEmacs to choose the correct action for the
  386. \[delete] key, whichever key that means. Other flavors don't use this
  387. function to control that."
  388. (interactive "*P")
  389. (if (and (fboundp 'delete-forward-p)
  390. (delete-forward-p))
  391. (c-electric-delete-forward arg)
  392. (c-electric-backspace arg)))
  393. ;; This function is only used in XEmacs.
  394. (defun c-hungry-delete ()
  395. "Delete a non-whitespace char, or all whitespace up to the next non-whitespace char.
  396. The direction of deletion depends on the configuration: If the
  397. function `delete-forward-p' is defined and returns non-nil, it deletes
  398. forward using `c-hungry-delete-forward'. Otherwise it deletes
  399. backward using `c-hungry-backspace'.
  400. Note: This is the way in XEmacs to choose the correct action for the
  401. \[delete] key, whichever key that means. Other flavors don't use this
  402. function to control that."
  403. (interactive)
  404. (if (and (fboundp 'delete-forward-p)
  405. (delete-forward-p))
  406. (c-hungry-delete-forward)
  407. (c-hungry-delete-backwards)))
  408. (defun c-electric-pound (arg)
  409. "Insert a \"#\".
  410. If `c-electric-flag' is set, handle it specially according to the variable
  411. `c-electric-pound-behavior'. If a numeric ARG is supplied, or if point is
  412. inside a literal or a macro, nothing special happens."
  413. (interactive "*P")
  414. (if (c-save-buffer-state ()
  415. (or arg
  416. (not c-electric-flag)
  417. (not (memq 'alignleft c-electric-pound-behavior))
  418. (save-excursion
  419. (skip-chars-backward " \t")
  420. (not (bolp)))
  421. (save-excursion
  422. (and (= (forward-line -1) 0)
  423. (progn (end-of-line)
  424. (eq (char-before) ?\\))))
  425. (c-in-literal)))
  426. ;; do nothing special
  427. (self-insert-command (prefix-numeric-value arg))
  428. ;; place the pound character at the left edge
  429. (let ((pos (- (point-max) (point)))
  430. (bolp (bolp)))
  431. (beginning-of-line)
  432. (delete-horizontal-space)
  433. (insert last-command-event)
  434. (and (not bolp)
  435. (goto-char (- (point-max) pos)))
  436. )))
  437. (defun c-point-syntax ()
  438. ;; Return the syntactic context of the construct at point. (This is NOT
  439. ;; nec. the same as the s.c. of the line point is on). N.B. This won't work
  440. ;; between the `#' of a cpp thing and what follows (see c-opt-cpp-prefix).
  441. (c-save-buffer-state (;; shut this up too
  442. (c-echo-syntactic-information-p nil)
  443. syntax)
  444. (c-tentative-buffer-changes
  445. ;; insert a newline to isolate the construct at point for syntactic
  446. ;; analysis.
  447. (insert-char ?\n 1)
  448. ;; In AWK (etc.) or in a macro, make sure this CR hasn't changed
  449. ;; the syntax. (There might already be an escaped NL there.)
  450. (when (or (c-at-vsemi-p (1- (point)))
  451. (let ((pt (point)))
  452. (save-excursion
  453. (backward-char)
  454. (and (c-beginning-of-macro)
  455. (progn (c-end-of-macro)
  456. (< (point) pt))))))
  457. (backward-char)
  458. (insert-char ?\\ 1)
  459. (forward-char))
  460. (let ((c-syntactic-indentation-in-macros t)
  461. (c-auto-newline-analysis t))
  462. ;; Turn on syntactic macro analysis to help with auto
  463. ;; newlines only.
  464. (setq syntax (c-guess-basic-syntax))
  465. nil))
  466. syntax))
  467. (defun c-brace-newlines (syntax)
  468. ;; A brace stands at point. SYNTAX is the syntactic context of this brace
  469. ;; (not necessarily the same as the S.C. of the line it is on). Return
  470. ;; NEWLINES, the list containing some combination of the symbols `before'
  471. ;; and `after' saying where newlines should be inserted.
  472. (c-save-buffer-state
  473. ((syms
  474. ;; This is the list of brace syntactic symbols that can hang.
  475. ;; If any new ones are added to c-offsets-alist, they should be
  476. ;; added here as well.
  477. ;;
  478. ;; The order of this list is important; if SYNTAX has several
  479. ;; elements, the element that "wins" is the earliest in SYMS.
  480. '(arglist-cont-nonempty ; e.g. an array literal.
  481. class-open class-close defun-open defun-close
  482. inline-open inline-close
  483. brace-list-open brace-list-close
  484. brace-list-intro brace-entry-open
  485. block-open block-close
  486. substatement-open statement-case-open
  487. extern-lang-open extern-lang-close
  488. namespace-open namespace-close
  489. module-open module-close
  490. composition-open composition-close
  491. inexpr-class-open inexpr-class-close
  492. ;; `statement-cont' is here for the case with a brace
  493. ;; list opener inside a statement. C.f. CASE B.2 in
  494. ;; `c-guess-continued-construct'.
  495. statement-cont))
  496. ;; shut this up too
  497. (c-echo-syntactic-information-p nil)
  498. symb-newlines) ; e.g. (substatement-open . (after))
  499. (setq symb-newlines
  500. ;; Do not try to insert newlines around a special
  501. ;; (Pike-style) brace list.
  502. (if (and c-special-brace-lists
  503. (save-excursion
  504. (c-safe (if (= (char-before) ?{)
  505. (forward-char -1)
  506. (c-forward-sexp -1))
  507. (c-looking-at-special-brace-list))))
  508. nil
  509. ;; Seek the matching entry in c-hanging-braces-alist.
  510. (or (c-lookup-lists
  511. syms
  512. ;; Substitute inexpr-class and class-open or
  513. ;; class-close with inexpr-class-open or
  514. ;; inexpr-class-close.
  515. (if (assq 'inexpr-class syntax)
  516. (cond ((assq 'class-open syntax)
  517. '((inexpr-class-open)))
  518. ((assq 'class-close syntax)
  519. '((inexpr-class-close)))
  520. (t syntax))
  521. syntax)
  522. c-hanging-braces-alist)
  523. '(ignore before after)))) ; Default, when not in c-h-b-l.
  524. ;; If syntax is a function symbol, then call it using the
  525. ;; defined semantics.
  526. (if (and (not (consp (cdr symb-newlines)))
  527. (functionp (cdr symb-newlines)))
  528. (let ((c-syntactic-context syntax))
  529. (funcall (cdr symb-newlines)
  530. (car symb-newlines)
  531. (point)))
  532. (cdr symb-newlines))))
  533. (defun c-try-one-liner ()
  534. ;; Point is just after a newly inserted }. If the non-whitespace
  535. ;; content of the braces is a single line of code, compact the whole
  536. ;; construct to a single line, if this line isn't too long. The Right
  537. ;; Thing is done with comments.
  538. ;;
  539. ;; Point will be left after the }, regardless of whether the clean-up is
  540. ;; done. Return NON-NIL if the clean-up happened, NIL if it didn't.
  541. (let ((here (point))
  542. (pos (- (point-max) (point)))
  543. mbeg1 mend1 mbeg4 mend4
  544. eol-col cmnt-pos cmnt-col cmnt-gap)
  545. (when
  546. (save-excursion
  547. (save-restriction
  548. ;; Avoid backtracking over a very large block. The one we
  549. ;; deal with here can never be more than three lines.
  550. (narrow-to-region (save-excursion
  551. (forward-line -2)
  552. (point))
  553. (point))
  554. (and (c-safe (c-backward-sexp))
  555. (progn
  556. (forward-char)
  557. (narrow-to-region (point) (1- here)) ; innards of {.}
  558. (looking-at
  559. (cc-eval-when-compile
  560. (concat
  561. "\\(" ; (match-beginning 1)
  562. "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
  563. "\\)" ; (match-end 1)
  564. "[^ \t\r\n]+\\([ \t]+[^ \t\r\n]+\\)*" ; non-WS
  565. "\\(" ; (match-beginning 4)
  566. "[ \t]*\\([\r\n][ \t]*\\)?" ; WS with opt. NL
  567. "\\)\\'"))))))) ; (match-end 4) at EOB.
  568. (if (c-tentative-buffer-changes
  569. (setq mbeg1 (match-beginning 1) mend1 (match-end 1)
  570. mbeg4 (match-beginning 4) mend4 (match-end 4))
  571. (backward-char) ; back over the `}'
  572. (save-excursion
  573. (setq cmnt-pos (and (c-backward-single-comment)
  574. (- (point) (- mend1 mbeg1)))))
  575. (delete-region mbeg4 mend4)
  576. (delete-region mbeg1 mend1)
  577. (setq eol-col (save-excursion (end-of-line) (current-column)))
  578. ;; Necessary to put the closing brace before any line
  579. ;; oriented comment to keep it syntactically significant.
  580. ;; This isn't necessary for block comments, but the result
  581. ;; looks nicer anyway.
  582. (when cmnt-pos
  583. (delete-char 1) ; the `}' has blundered into a comment
  584. (goto-char cmnt-pos)
  585. (setq cmnt-col (1+ (current-column)))
  586. (setq cmnt-pos (1+ cmnt-pos)) ; we're inserting a `}'
  587. (c-skip-ws-backward)
  588. (insert-char ?\} 1) ; reinsert the `}' before the comment.
  589. (setq cmnt-gap (- cmnt-col (current-column)))
  590. (when (zerop cmnt-gap)
  591. (insert-char ?\ 1) ; Put a space before a bare comment.
  592. (setq cmnt-gap 1)))
  593. (or (null c-max-one-liner-length)
  594. (zerop c-max-one-liner-length)
  595. (<= eol-col c-max-one-liner-length)
  596. ;; Can we trim space before comment to make the line fit?
  597. (and cmnt-gap
  598. (< (- eol-col cmnt-gap) c-max-one-liner-length)
  599. (progn (goto-char cmnt-pos)
  600. (backward-delete-char-untabify
  601. (- eol-col c-max-one-liner-length))
  602. t))))
  603. (goto-char (- (point-max) pos))))))
  604. (defun c-electric-brace (arg)
  605. "Insert a brace.
  606. If `c-electric-flag' is non-nil, the brace is not inside a literal and a
  607. numeric ARG hasn't been supplied, the command performs several electric
  608. actions:
  609. \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
  610. the mode line) newlines are inserted before and after the brace as
  611. directed by the settings in `c-hanging-braces-alist'.
  612. \(b) Any auto-newlines are indented. The original line is also
  613. reindented unless `c-syntactic-indentation' is nil.
  614. \(c) If auto-newline is turned on, various newline cleanups based on the
  615. settings of `c-cleanup-list' are done."
  616. (interactive "*P")
  617. (let (safepos literal
  618. ;; We want to inhibit blinking the paren since this would be
  619. ;; most disruptive. We'll blink it ourselves later on.
  620. (old-blink-paren blink-paren-function)
  621. blink-paren-function case-fold-search)
  622. (c-save-buffer-state ()
  623. (setq safepos (c-safe-position (point) (c-parse-state))
  624. literal (c-in-literal safepos)))
  625. ;; Insert the brace. Note that expand-abbrev might reindent
  626. ;; the line here if there's a preceding "else" or something.
  627. (self-insert-command (prefix-numeric-value arg))
  628. (when (and c-electric-flag (not literal) (not arg))
  629. (if (not (looking-at "[ \t]*\\\\?$"))
  630. (if c-syntactic-indentation
  631. (indent-according-to-mode))
  632. (let ( ;; shut this up too
  633. (c-echo-syntactic-information-p nil)
  634. newlines
  635. ln-syntax br-syntax syntax) ; Syntactic context of the original line,
  636. ; of the brace itself, of the line the brace ends up on.
  637. (c-save-buffer-state ((c-syntactic-indentation-in-macros t)
  638. (c-auto-newline-analysis t))
  639. (setq ln-syntax (c-guess-basic-syntax)))
  640. (if c-syntactic-indentation
  641. (c-indent-line ln-syntax))
  642. (when c-auto-newline
  643. (backward-char)
  644. (setq br-syntax (c-point-syntax)
  645. newlines (c-brace-newlines br-syntax))
  646. ;; Insert the BEFORE newline, if wanted, and reindent the newline.
  647. (if (and (memq 'before newlines)
  648. (> (current-column) (current-indentation)))
  649. (if c-syntactic-indentation
  650. ;; Only a plain newline for now - it's indented
  651. ;; after the cleanups when the line has its final
  652. ;; appearance.
  653. (newline)
  654. (c-newline-and-indent)))
  655. (forward-char)
  656. ;; `syntax' is the syntactic context of the line which ends up
  657. ;; with the brace on it.
  658. (setq syntax (if (memq 'before newlines) br-syntax ln-syntax))
  659. ;; Do all appropriate clean ups
  660. (let ((here (point))
  661. (pos (- (point-max) (point)))
  662. mbeg mend
  663. )
  664. ;; `}': clean up empty defun braces
  665. (when (c-save-buffer-state ()
  666. (and (memq 'empty-defun-braces c-cleanup-list)
  667. (eq last-command-event ?\})
  668. (c-intersect-lists '(defun-close class-close inline-close)
  669. syntax)
  670. (progn
  671. (forward-char -1)
  672. (c-skip-ws-backward)
  673. (eq (char-before) ?\{))
  674. ;; make sure matching open brace isn't in a comment
  675. (not (c-in-literal))))
  676. (delete-region (point) (1- here))
  677. (setq here (- (point-max) pos)))
  678. (goto-char here)
  679. ;; `}': compact to a one-liner defun?
  680. (save-match-data
  681. (when
  682. (and (eq last-command-event ?\})
  683. (memq 'one-liner-defun c-cleanup-list)
  684. (c-intersect-lists '(defun-close) syntax)
  685. (c-try-one-liner))
  686. (setq here (- (point-max) pos))))
  687. ;; `{': clean up brace-else-brace and brace-elseif-brace
  688. (when (eq last-command-event ?\{)
  689. (cond
  690. ((and (memq 'brace-else-brace c-cleanup-list)
  691. (re-search-backward
  692. (concat "}"
  693. "\\([ \t\n]\\|\\\\\n\\)*"
  694. "else"
  695. "\\([ \t\n]\\|\\\\\n\\)*"
  696. "{"
  697. "\\=")
  698. nil t))
  699. (delete-region (match-beginning 0) (match-end 0))
  700. (insert-and-inherit "} else {"))
  701. ((and (memq 'brace-elseif-brace c-cleanup-list)
  702. (progn
  703. (goto-char (1- here))
  704. (setq mend (point))
  705. (c-skip-ws-backward)
  706. (setq mbeg (point))
  707. (eq (char-before) ?\)))
  708. (zerop (c-save-buffer-state nil (c-backward-token-2 1 t)))
  709. (eq (char-after) ?\()
  710. ; (progn
  711. ; (setq tmp (point))
  712. (re-search-backward
  713. (concat "}"
  714. "\\([ \t\n]\\|\\\\\n\\)*"
  715. "else"
  716. "\\([ \t\n]\\|\\\\\n\\)+"
  717. "if"
  718. "\\([ \t\n]\\|\\\\\n\\)*"
  719. "\\=")
  720. nil t);)
  721. ;(eq (match-end 0) tmp);
  722. )
  723. (delete-region mbeg mend)
  724. (goto-char mbeg)
  725. (insert ?\ ))))
  726. (goto-char (- (point-max) pos))
  727. ;; Indent the line after the cleanups since it might
  728. ;; very well indent differently due to them, e.g. if
  729. ;; c-indent-one-line-block is used together with the
  730. ;; one-liner-defun cleanup.
  731. (when c-syntactic-indentation
  732. (c-indent-line)))
  733. ;; does a newline go after the brace?
  734. (if (memq 'after newlines)
  735. (c-newline-and-indent))
  736. ))))
  737. ;; blink the paren
  738. (and (eq last-command-event ?\})
  739. (not executing-kbd-macro)
  740. old-blink-paren
  741. (save-excursion
  742. (c-save-buffer-state nil
  743. (c-backward-syntactic-ws safepos))
  744. (funcall old-blink-paren)))))
  745. (defun c-electric-slash (arg)
  746. "Insert a slash character.
  747. If the slash is inserted immediately after the comment prefix in a c-style
  748. comment, the comment might get closed by removing whitespace and possibly
  749. inserting a \"*\". See the variable `c-cleanup-list'.
  750. Indent the line as a comment, if:
  751. 1. The slash is second of a \"//\" line oriented comment introducing
  752. token and we are on a comment-only-line, or
  753. 2. The slash is part of a \"*/\" token that closes a block oriented
  754. comment.
  755. If a numeric ARG is supplied, point is inside a literal, or
  756. `c-syntactic-indentation' is nil or `c-electric-flag' is nil, indentation
  757. is inhibited."
  758. (interactive "*P")
  759. (let ((literal (c-save-buffer-state () (c-in-literal)))
  760. indentp
  761. ;; shut this up
  762. (c-echo-syntactic-information-p nil))
  763. ;; comment-close-slash cleanup? This DOESN'T need `c-electric-flag' or
  764. ;; `c-syntactic-indentation' set.
  765. (when (and (not arg)
  766. (eq literal 'c)
  767. (memq 'comment-close-slash c-cleanup-list)
  768. (eq last-command-event ?/)
  769. (looking-at (concat "[ \t]*\\("
  770. (regexp-quote comment-end) "\\)?$"))
  771. ; (eq c-block-comment-ender "*/") ; C-style comments ALWAYS end in */
  772. (save-excursion
  773. (save-restriction
  774. (narrow-to-region (point-min) (point))
  775. (back-to-indentation)
  776. (looking-at (concat c-current-comment-prefix "[ \t]*$")))))
  777. (delete-region (progn (forward-line 0) (point))
  778. (progn (end-of-line) (point)))
  779. (insert-char ?* 1)) ; the / comes later. ; Do I need a t (retain sticky properties) here?
  780. (setq indentp (and (not arg)
  781. c-syntactic-indentation
  782. c-electric-flag
  783. (eq last-command-event ?/)
  784. (eq (char-before) (if literal ?* ?/))))
  785. (self-insert-command (prefix-numeric-value arg))
  786. (if indentp
  787. (indent-according-to-mode))))
  788. (defun c-electric-star (arg)
  789. "Insert a star character.
  790. If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, and
  791. the star is the second character of a C style comment starter on a
  792. comment-only-line, indent the line as a comment. If a numeric ARG is
  793. supplied, point is inside a literal, or `c-syntactic-indentation' is nil,
  794. this indentation is inhibited."
  795. (interactive "*P")
  796. (self-insert-command (prefix-numeric-value arg))
  797. ;; if we are in a literal, or if arg is given do not reindent the
  798. ;; current line, unless this star introduces a comment-only line.
  799. (if (c-save-buffer-state ()
  800. (and c-syntactic-indentation
  801. c-electric-flag
  802. (not arg)
  803. (eq (c-in-literal) 'c)
  804. (eq (char-before) ?*)
  805. (save-excursion
  806. (forward-char -1)
  807. (skip-chars-backward "*")
  808. (if (eq (char-before) ?/)
  809. (forward-char -1))
  810. (skip-chars-backward " \t")
  811. (bolp))))
  812. (let (c-echo-syntactic-information-p) ; shut this up
  813. (indent-according-to-mode))
  814. ))
  815. (defun c-electric-semi&comma (arg)
  816. "Insert a comma or semicolon.
  817. If `c-electric-flag' is non-nil, point isn't inside a literal and a
  818. numeric ARG hasn't been supplied, the command performs several electric
  819. actions:
  820. \(a) When the auto-newline feature is turned on (indicated by \"/la\" on
  821. the mode line) a newline might be inserted. See the variable
  822. `c-hanging-semi&comma-criteria' for how newline insertion is determined.
  823. \(b) Any auto-newlines are indented. The original line is also
  824. reindented unless `c-syntactic-indentation' is nil.
  825. \(c) If auto-newline is turned on, a comma following a brace list or a
  826. semicolon following a defun might be cleaned up, depending on the
  827. settings of `c-cleanup-list'."
  828. (interactive "*P")
  829. (let* (lim literal c-syntactic-context
  830. (here (point))
  831. ;; shut this up
  832. (c-echo-syntactic-information-p nil))
  833. (c-save-buffer-state ()
  834. (setq lim (c-most-enclosing-brace (c-parse-state))
  835. literal (c-in-literal lim)))
  836. (self-insert-command (prefix-numeric-value arg))
  837. (if (and c-electric-flag (not literal) (not arg))
  838. ;; do all cleanups and newline insertions if c-auto-newline is on.
  839. (if (or (not c-auto-newline)
  840. (not (looking-at "[ \t]*\\\\?$")))
  841. (if c-syntactic-indentation
  842. (c-indent-line))
  843. ;; clean ups: list-close-comma or defun-close-semi
  844. (let ((pos (- (point-max) (point))))
  845. (if (c-save-buffer-state ()
  846. (and (or (and
  847. (eq last-command-event ?,)
  848. (memq 'list-close-comma c-cleanup-list))
  849. (and
  850. (eq last-command-event ?\;)
  851. (memq 'defun-close-semi c-cleanup-list)))
  852. (progn
  853. (forward-char -1)
  854. (c-skip-ws-backward)
  855. (eq (char-before) ?}))
  856. ;; make sure matching open brace isn't in a comment
  857. (not (c-in-literal lim))))
  858. (delete-region (point) here))
  859. (goto-char (- (point-max) pos)))
  860. ;; reindent line
  861. (when c-syntactic-indentation
  862. (setq c-syntactic-context (c-guess-basic-syntax))
  863. (c-indent-line c-syntactic-context))
  864. ;; check to see if a newline should be added
  865. (let ((criteria c-hanging-semi&comma-criteria)
  866. answer add-newline-p)
  867. (while criteria
  868. (setq answer (funcall (car criteria)))
  869. ;; only nil value means continue checking
  870. (if (not answer)
  871. (setq criteria (cdr criteria))
  872. (setq criteria nil)
  873. ;; only 'stop specifically says do not add a newline
  874. (setq add-newline-p (not (eq answer 'stop)))
  875. ))
  876. (if add-newline-p
  877. (c-newline-and-indent))
  878. )))))
  879. (defun c-electric-colon (arg)
  880. "Insert a colon.
  881. If `c-electric-flag' is non-nil, the colon is not inside a literal and a
  882. numeric ARG hasn't been supplied, the command performs several electric
  883. actions:
  884. \(a) If the auto-newline feature is turned on (indicated by \"/la\" on
  885. the mode line) newlines are inserted before and after the colon based on
  886. the settings in `c-hanging-colons-alist'.
  887. \(b) Any auto-newlines are indented. The original line is also
  888. reindented unless `c-syntactic-indentation' is nil.
  889. \(c) If auto-newline is turned on, whitespace between two colons will be
  890. \"cleaned up\" leaving a scope operator, if this action is set in
  891. `c-cleanup-list'."
  892. (interactive "*P")
  893. (let* ((bod (c-point 'bod))
  894. (literal (c-save-buffer-state () (c-in-literal bod)))
  895. newlines is-scope-op
  896. ;; shut this up
  897. (c-echo-syntactic-information-p nil))
  898. (self-insert-command (prefix-numeric-value arg))
  899. ;; Any electric action?
  900. (if (and c-electric-flag (not literal) (not arg))
  901. ;; Unless we're at EOL, only re-indentation happens.
  902. (if (not (looking-at "[ \t]*\\\\?$"))
  903. (if c-syntactic-indentation
  904. (indent-according-to-mode))
  905. ;; scope-operator clean-up?
  906. (let ((pos (- (point-max) (point)))
  907. (here (point)))
  908. (if (c-save-buffer-state () ; Why do we need this? [ACM, 2003-03-12]
  909. (and c-auto-newline
  910. (memq 'scope-operator c-cleanup-list)
  911. (eq (char-before) ?:)
  912. (progn
  913. (forward-char -1)
  914. (c-skip-ws-backward)
  915. (eq (char-before) ?:))
  916. (not (c-in-literal))
  917. (not (eq (char-after (- (point) 2)) ?:))))
  918. (progn
  919. (delete-region (point) (1- here))
  920. (setq is-scope-op t)))
  921. (goto-char (- (point-max) pos)))
  922. ;; indent the current line if it's done syntactically.
  923. (if c-syntactic-indentation
  924. ;; Cannot use the same syntax analysis as we find below,
  925. ;; since that's made with c-syntactic-indentation-in-macros
  926. ;; always set to t.
  927. (indent-according-to-mode))
  928. ;; Calculate where, if anywhere, we want newlines.
  929. (c-save-buffer-state
  930. ((c-syntactic-indentation-in-macros t)
  931. (c-auto-newline-analysis t)
  932. ;; Turn on syntactic macro analysis to help with auto newlines
  933. ;; only.
  934. (syntax (c-guess-basic-syntax))
  935. (elem syntax))
  936. ;; Translate substatement-label to label for this operation.
  937. (while elem
  938. (if (eq (car (car elem)) 'substatement-label)
  939. (setcar (car elem) 'label))
  940. (setq elem (cdr elem)))
  941. ;; some language elements can only be determined by checking
  942. ;; the following line. Let's first look for ones that can be
  943. ;; found when looking on the line with the colon
  944. (setq newlines
  945. (and c-auto-newline
  946. (or (c-lookup-lists '(case-label label access-label)
  947. syntax c-hanging-colons-alist)
  948. (c-lookup-lists '(member-init-intro inher-intro)
  949. (progn
  950. (insert ?\n)
  951. (unwind-protect
  952. (c-guess-basic-syntax)
  953. (delete-char -1)))
  954. c-hanging-colons-alist)))))
  955. ;; does a newline go before the colon? Watch out for already
  956. ;; non-hung colons. However, we don't unhang them because that
  957. ;; would be a cleanup (and anti-social).
  958. (if (and (memq 'before newlines)
  959. (not is-scope-op)
  960. (save-excursion
  961. (skip-chars-backward ": \t")
  962. (not (bolp))))
  963. (let ((pos (- (point-max) (point))))
  964. (forward-char -1)
  965. (c-newline-and-indent)
  966. (goto-char (- (point-max) pos))))
  967. ;; does a newline go after the colon?
  968. (if (and (memq 'after (cdr-safe newlines))
  969. (not is-scope-op))
  970. (c-newline-and-indent))
  971. ))))
  972. (defun c-electric-lt-gt (arg)
  973. "Insert a \"<\" or \">\" character.
  974. If the current language uses angle bracket parens (e.g. template
  975. arguments in C++), try to find out if the inserted character is a
  976. paren and give it paren syntax if appropriate.
  977. If `c-electric-flag' and `c-syntactic-indentation' are both non-nil, the
  978. line will be reindented if the inserted character is a paren or if it
  979. finishes a C++ style stream operator in C++ mode. Exceptions are when a
  980. numeric argument is supplied, or the point is inside a literal."
  981. (interactive "*P")
  982. (let ((c-echo-syntactic-information-p nil)
  983. final-pos close-paren-inserted found-delim case-fold-search)
  984. (self-insert-command (prefix-numeric-value arg))
  985. (setq final-pos (point))
  986. ;;;; 2010-01-31: There used to be code here to put a syntax-table text
  987. ;;;; property on the new < or > and its mate (if any) when they are template
  988. ;;;; parens. This is now done in an after-change function.
  989. ;; Indent the line if appropriate.
  990. (when (and c-electric-flag c-syntactic-indentation c-recognize-<>-arglists)
  991. (setq found-delim
  992. (if (eq last-command-event ?<)
  993. ;; If a <, basically see if it's got "template" before it .....
  994. (or (and (progn
  995. (backward-char)
  996. (= (point)
  997. (progn (c-beginning-of-current-token) (point))))
  998. (progn
  999. (c-backward-token-2)
  1000. (looking-at c-opt-<>-sexp-key)))
  1001. ;; ..... or is a C++ << operator.
  1002. (and (c-major-mode-is 'c++-mode)
  1003. (progn
  1004. (goto-char (1- final-pos))
  1005. (c-beginning-of-current-token)
  1006. (looking-at "<<"))
  1007. (>= (match-end 0) final-pos)))
  1008. ;; It's a >. Either a C++ >> operator. ......
  1009. (or (and (c-major-mode-is 'c++-mode)
  1010. (progn
  1011. (goto-char (1- final-pos))
  1012. (c-beginning-of-current-token)
  1013. (looking-at ">>"))
  1014. (>= (match-end 0) final-pos))
  1015. ;; ...., or search back for a < which isn't already marked as an
  1016. ;; opening template delimiter.
  1017. (save-restriction
  1018. (widen)
  1019. ;; Narrow to avoid `c-forward-<>-arglist' below searching past
  1020. ;; our position.
  1021. (narrow-to-region (point-min) final-pos)
  1022. (goto-char final-pos)
  1023. (while
  1024. (and
  1025. (progn
  1026. (c-syntactic-skip-backward "^<;}" nil t)
  1027. (eq (char-before) ?<))
  1028. (progn
  1029. (backward-char)
  1030. (looking-at "\\s\("))))
  1031. (and (eq (char-after) ?<)
  1032. (not (looking-at "\\s\("))
  1033. (progn (c-backward-syntactic-ws)
  1034. (c-simple-skip-symbol-backward))
  1035. (or (looking-at c-opt-<>-sexp-key)
  1036. (not (looking-at c-keywords-regexp)))))))))
  1037. (goto-char final-pos)
  1038. (when found-delim
  1039. (indent-according-to-mode)
  1040. (when (and (eq (char-before) ?>)
  1041. (not executing-kbd-macro)
  1042. blink-paren-function)
  1043. ;; Note: Most paren blink functions, such as the standard
  1044. ;; `blink-matching-open', currently doesn't handle paren chars
  1045. ;; marked with text properties very well. Maybe we should avoid
  1046. ;; this call for the time being?
  1047. (funcall blink-paren-function)))))
  1048. (defun c-electric-paren (arg)
  1049. "Insert a parenthesis.
  1050. If `c-syntactic-indentation' and `c-electric-flag' are both non-nil, the
  1051. line is reindented unless a numeric ARG is supplied, or the parenthesis
  1052. is inserted inside a literal.
  1053. Whitespace between a function name and the parenthesis may get added or
  1054. removed; see the variable `c-cleanup-list'.
  1055. Also, if `c-electric-flag' and `c-auto-newline' are both non-nil, some
  1056. newline cleanups are done if appropriate; see the variable `c-cleanup-list'."
  1057. (interactive "*P")
  1058. (let ((literal (c-save-buffer-state () (c-in-literal)))
  1059. ;; shut this up
  1060. (c-echo-syntactic-information-p nil)
  1061. case-fold-search)
  1062. (self-insert-command (prefix-numeric-value arg))
  1063. (if (and (not arg) (not literal))
  1064. (let* ( ;; We want to inhibit blinking the paren since this will
  1065. ;; be most disruptive. We'll blink it ourselves
  1066. ;; afterwards.
  1067. (old-blink-paren blink-paren-function)
  1068. blink-paren-function)
  1069. (if (and c-syntactic-indentation c-electric-flag)
  1070. (indent-according-to-mode))
  1071. ;; If we're at EOL, check for new-line clean-ups.
  1072. (when (and c-electric-flag c-auto-newline
  1073. (looking-at "[ \t]*\\\\?$"))
  1074. ;; clean up brace-elseif-brace
  1075. (when
  1076. (and (memq 'brace-elseif-brace c-cleanup-list)
  1077. (eq last-command-event ?\()
  1078. (re-search-backward
  1079. (concat "}"
  1080. "\\([ \t\n]\\|\\\\\n\\)*"
  1081. "else"
  1082. "\\([ \t\n]\\|\\\\\n\\)+"
  1083. "if"
  1084. "\\([ \t\n]\\|\\\\\n\\)*"
  1085. "("
  1086. "\\=")
  1087. nil t)
  1088. (not (c-save-buffer-state () (c-in-literal))))
  1089. (delete-region (match-beginning 0) (match-end 0))
  1090. (insert-and-inherit "} else if ("))
  1091. ;; clean up brace-catch-brace
  1092. (when
  1093. (and (memq 'brace-catch-brace c-cleanup-list)
  1094. (eq last-command-event ?\()
  1095. (re-search-backward
  1096. (concat "}"
  1097. "\\([ \t\n]\\|\\\\\n\\)*"
  1098. "catch"
  1099. "\\([ \t\n]\\|\\\\\n\\)*"
  1100. "("
  1101. "\\=")
  1102. nil t)
  1103. (not (c-save-buffer-state () (c-in-literal))))
  1104. (delete-region (match-beginning 0) (match-end 0))
  1105. (insert-and-inherit "} catch (")))
  1106. ;; Check for clean-ups at function calls. These two DON'T need
  1107. ;; `c-electric-flag' or `c-syntactic-indentation' set.
  1108. ;; Point is currently just after the inserted paren.
  1109. (let (beg (end (1- (point))))
  1110. (cond
  1111. ;; space-before-funcall clean-up?
  1112. ((and (memq 'space-before-funcall c-cleanup-list)
  1113. (eq last-command-event ?\()
  1114. (save-excursion
  1115. (backward-char)
  1116. (skip-chars-backward " \t")
  1117. (setq beg (point))
  1118. (and (c-save-buffer-state () (c-on-identifier))
  1119. ;; Don't add a space into #define FOO()....
  1120. (not (and (c-beginning-of-macro)
  1121. (c-forward-over-cpp-define-id)
  1122. (eq (point) beg))))))
  1123. (save-excursion
  1124. (delete-region beg end)
  1125. (goto-char beg)
  1126. (insert ?\ )))
  1127. ;; compact-empty-funcall clean-up?
  1128. ((c-save-buffer-state ()
  1129. (and (memq 'compact-empty-funcall c-cleanup-list)
  1130. (eq last-command-event ?\))
  1131. (save-excursion
  1132. (c-safe (backward-char 2))
  1133. (when (looking-at "()")
  1134. (setq end (point))
  1135. (skip-chars-backward " \t")
  1136. (setq beg (point))
  1137. (c-on-identifier)))))
  1138. (delete-region beg end))))
  1139. (and (eq last-input-event ?\))
  1140. (not executing-kbd-macro)
  1141. old-blink-paren
  1142. (funcall old-blink-paren))))))
  1143. (defun c-electric-continued-statement ()
  1144. "Reindent the current line if appropriate.
  1145. This function is used to reindent the line after a keyword which
  1146. continues an earlier statement is typed, e.g. an \"else\" or the
  1147. \"while\" in a do-while block.
  1148. The line is reindented if there is nothing but whitespace before the
  1149. keyword on the line, the keyword is not inserted inside a literal, and
  1150. `c-electric-flag' and `c-syntactic-indentation' are both non-nil."
  1151. (let (;; shut this up
  1152. (c-echo-syntactic-information-p nil))
  1153. (when (c-save-buffer-state ()
  1154. (and c-electric-flag
  1155. c-syntactic-indentation
  1156. (not (eq last-command-event ?_))
  1157. (= (save-excursion
  1158. (skip-syntax-backward "w")
  1159. (point))
  1160. (c-point 'boi))
  1161. (not (c-in-literal (c-point 'bod)))))
  1162. ;; Have to temporarily insert a space so that
  1163. ;; c-guess-basic-syntax recognizes the keyword. Follow the
  1164. ;; space with a nonspace to avoid messing up any whitespace
  1165. ;; sensitive meddling that might be done, e.g. by
  1166. ;; `c-backslash-region'.
  1167. (insert-and-inherit " x")
  1168. (unwind-protect
  1169. (indent-according-to-mode)
  1170. (delete-char -2)))))
  1171. (declare-function subword-forward "subword" (&optional arg))
  1172. (declare-function subword-backward "subword" (&optional arg))
  1173. ;; "nomenclature" functions + c-scope-operator.
  1174. (defun c-forward-into-nomenclature (&optional arg)
  1175. "Compatibility alias for `c-forward-subword'."
  1176. (interactive "p")
  1177. (require 'subword)
  1178. (subword-forward arg))
  1179. (make-obsolete 'c-forward-into-nomenclature 'subword-forward "23.2")
  1180. (defun c-backward-into-nomenclature (&optional arg)
  1181. "Compatibility alias for `c-backward-subword'."
  1182. (interactive "p")
  1183. (require 'subword)
  1184. (subword-backward arg))
  1185. (make-obsolete 'c-backward-into-nomenclature 'subword-backward "23.2")
  1186. (defun c-scope-operator ()
  1187. "Insert a double colon scope operator at point.
  1188. No indentation or other \"electric\" behavior is performed."
  1189. (interactive "*")
  1190. (insert-and-inherit "::"))
  1191. ;; Movement (etc.) by defuns.
  1192. (defun c-in-function-trailer-p (&optional lim)
  1193. ;; Return non-nil if point is between the closing brace and the semicolon of
  1194. ;; a brace construct which needs a semicolon, e.g. within the "variables"
  1195. ;; portion of a declaration like "struct foo {...} bar ;".
  1196. ;;
  1197. ;; Return the position of the main declaration. Otherwise, return nil.
  1198. ;; Point is assumed to be at the top level and outside of any macro or
  1199. ;; literal.
  1200. ;;
  1201. ;; If LIM is non-nil, it is the bound on a the backward search for the
  1202. ;; beginning of the declaration.
  1203. ;;
  1204. ;; This function might do hidden buffer changes.
  1205. (and c-opt-block-decls-with-vars-key
  1206. (save-excursion
  1207. (c-syntactic-skip-backward "^;}" lim)
  1208. (let ((eo-block (point))
  1209. bod)
  1210. (and (eq (char-before) ?\})
  1211. (eq (car (c-beginning-of-decl-1 lim)) 'previous)
  1212. (setq bod (point))
  1213. ;; Look for struct or union or ... If we find one, it might
  1214. ;; be the return type of a function, or the like. Exclude
  1215. ;; this case.
  1216. (c-syntactic-re-search-forward
  1217. (concat "[;=\(\[{]\\|\\("
  1218. c-opt-block-decls-with-vars-key
  1219. "\\)")
  1220. eo-block t t t)
  1221. (match-beginning 1) ; Is there a "struct" etc., somewhere?
  1222. (not (eq (char-before) ?_))
  1223. (c-syntactic-re-search-forward "[;=\(\[{]" eo-block t t t)
  1224. (eq (char-before) ?\{)
  1225. bod)))))
  1226. (defun c-where-wrt-brace-construct ()
  1227. ;; Determine where we are with respect to functions (or other brace
  1228. ;; constructs, included in the term "function" in the rest of this comment).
  1229. ;; Point is assumed to be outside any macro or literal.
  1230. ;; This is used by c-\(beginning\|end\)-of-defun.
  1231. ;;
  1232. ;; Return one of these symbols:
  1233. ;; at-header : we're at the start of a function's header.
  1234. ;; in-header : we're inside a function's header, this extending right
  1235. ;; up to the brace. This bit includes any k&r declarations.
  1236. ;; in-block : we're inside a function's brace block.
  1237. ;; in-trailer : we're in the area between the "}" and ";" of something
  1238. ;; like "struct foo {...} bar, baz;".
  1239. ;; at-function-end : we're just after the closing brace (or semicolon) that
  1240. ;; terminates the function.
  1241. ;; outwith-function: we're not at or in any function. Being inside a
  1242. ;; non-brace construct also counts as 'outwith-function'.
  1243. ;;
  1244. ;; This function might do hidden buffer changes.
  1245. (save-excursion
  1246. (let* (kluge-start
  1247. decl-result brace-decl-p
  1248. (start (point))
  1249. (paren-state (c-parse-state))
  1250. (least-enclosing (c-least-enclosing-brace paren-state)))
  1251. (cond
  1252. ((and least-enclosing
  1253. (eq (char-after least-enclosing) ?\{))
  1254. 'in-block)
  1255. ((c-in-function-trailer-p)
  1256. 'in-trailer)
  1257. ((and (not least-enclosing)
  1258. (consp paren-state)
  1259. (consp (car paren-state))
  1260. (eq start (cdar paren-state)))
  1261. 'at-function-end)
  1262. (t
  1263. ;; Find the start of the current declaration. NOTE: If we're in the
  1264. ;; variables after a "struct/eval" type block, we don't get to the
  1265. ;; real declaration here - we detect and correct for this later.
  1266. ;;If we're in the parameters' parens, move back out of them.
  1267. (if least-enclosing (goto-char least-enclosing))
  1268. ;; Kluge so that c-beginning-of-decl-1 won't go back if we're already
  1269. ;; at a declaration.
  1270. (if (or (and (eolp) (not (eobp))) ; EOL is matched by "\\s>"
  1271. (not (looking-at
  1272. "\\([;#]\\|\\'\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s$\\|\\s<\\|\\s>\\|\\s!\\)")))
  1273. (forward-char))
  1274. (setq kluge-start (point))
  1275. (setq decl-result
  1276. (car (c-beginning-of-decl-1
  1277. ;; NOTE: If we're in a K&R region, this might be the start
  1278. ;; of a parameter declaration, not the actual function.
  1279. (and least-enclosing ; LIMIT for c-b-of-decl-1
  1280. (c-safe-position least-enclosing paren-state)))))
  1281. ;; Has the declaration we've gone back to got braces?
  1282. (setq brace-decl-p
  1283. (save-excursion
  1284. (and (c-syntactic-re-search-forward "[;{]" nil t t)
  1285. (or (eq (char-before) ?\{)
  1286. (and c-recognize-knr-p
  1287. ;; Might have stopped on the
  1288. ;; ';' in a K&R argdecl. In
  1289. ;; that case the declaration
  1290. ;; should contain a block.
  1291. (c-in-knr-argdecl))))))
  1292. (cond
  1293. ((= (point) kluge-start) ; might be BOB or unbalanced parens.
  1294. 'outwith-function)
  1295. ((eq decl-result 'same)
  1296. (if brace-decl-p
  1297. (if (eq (point) start)
  1298. 'at-header
  1299. 'in-header)
  1300. 'outwith-function))
  1301. ((eq decl-result 'previous)
  1302. (if (and (not brace-decl-p)
  1303. (c-in-function-trailer-p))
  1304. 'at-function-end
  1305. 'outwith-function))
  1306. (t (error
  1307. "c-where-wrt-brace-construct: c-beginning-of-decl-1 returned %s"
  1308. decl-result))))))))
  1309. (defun c-backward-to-nth-BOF-{ (n where)
  1310. ;; Skip to the opening brace of the Nth function before point. If
  1311. ;; point is inside a function, this counts as the first. Point must be
  1312. ;; outside any comment/string or macro.
  1313. ;;
  1314. ;; N must be strictly positive.
  1315. ;; WHERE describes the position of point, one of the symbols `at-header',
  1316. ;; `in-header', `in-block', `in-trailer', `at-function-end',
  1317. ;; `outwith-function' as returned by c-where-wrt-brace-construct.
  1318. ;;
  1319. ;; If we run out of functions, leave point at BOB. Return zero on success,
  1320. ;; otherwise the number of {s still to go.
  1321. ;;
  1322. ;; This function may do hidden buffer changes
  1323. (cond
  1324. ;; What we do to go back the first defun depends on where we start.
  1325. ((bobp))
  1326. ((eq where 'in-block)
  1327. (goto-char (c-least-enclosing-brace (c-parse-state)))
  1328. (setq n (1- n)))
  1329. ((eq where 'in-header)
  1330. (c-syntactic-re-search-forward "{")
  1331. (backward-char)
  1332. (setq n (1- n)))
  1333. ((memq where '(at-header outwith-function at-function-end in-trailer))
  1334. (c-syntactic-skip-backward "^}")
  1335. (when (eq (char-before) ?\})
  1336. (backward-sexp)
  1337. (setq n (1- n))))
  1338. (t (error "Unknown `where' %s in c-backward-to-nth-EOF-{" where)))
  1339. ;; Each time round the loop, go back to a "{" at the outermost level.
  1340. (while (and (> n 0) (not (bobp)))
  1341. (c-parse-state) ; This call speeds up the following one
  1342. ; by a factor of ~6. Hmmm. 2006/4/5.
  1343. (c-syntactic-skip-backward "^}")
  1344. (when (eq (char-before) ?\})
  1345. (backward-sexp)
  1346. (setq n (1- n))))
  1347. n)
  1348. (defun c-narrow-to-most-enclosing-decl-block (&optional inclusive)
  1349. ;; If we are inside a decl-block (in the sense of c-looking-at-decl-block),
  1350. ;; i.e. something like namespace{} or extern{}, narrow to the insides of
  1351. ;; that block (NOT including the enclosing braces) if INCLUSIVE is nil,
  1352. ;; otherwise include the braces. If the closing brace is missing,
  1353. ;; (point-max) is used instead.
  1354. (let ((paren-state (c-parse-state))
  1355. encl-decl)
  1356. (setq encl-decl (and paren-state (c-most-enclosing-decl-block paren-state)))
  1357. (if encl-decl
  1358. (save-excursion
  1359. (narrow-to-region
  1360. (if inclusive
  1361. (progn (goto-char encl-decl)
  1362. (c-beginning-of-decl-1)
  1363. (point))
  1364. (1+ encl-decl))
  1365. (progn
  1366. (goto-char encl-decl)
  1367. (or (c-safe (forward-list)
  1368. (if inclusive
  1369. (point)
  1370. (1- (point))))
  1371. (point-max))))))))
  1372. (defun c-widen-to-enclosing-decl-scope (paren-state orig-point-min orig-point-max)
  1373. ;; Narrow the buffer to the innermost declaration scope (e.g. a class, a
  1374. ;; namespace or the "whole buffer") recorded in PAREN-STATE, the bounding
  1375. ;; braces NOT being included in the resulting region. On no account may the
  1376. ;; final region exceed that bounded by ORIG-POINT-MIN, ORIG-POINT-MAX.
  1377. ;; PAREN-STATE is a list of buffer positions in the style of
  1378. ;; (c-parse-state), one of which will be that of the desired opening brace,
  1379. ;; if there is one.
  1380. ;;
  1381. ;; Return the position of the enclosing opening brace, or nil
  1382. (let (encl-decl) ; putative position of decl-scope's opening brace.
  1383. (save-restriction
  1384. (narrow-to-region orig-point-min orig-point-max)
  1385. (setq encl-decl (and paren-state
  1386. (c-most-enclosing-decl-block paren-state))))
  1387. (if encl-decl
  1388. (progn
  1389. (widen)
  1390. (narrow-to-region (1+ encl-decl)
  1391. (save-excursion
  1392. (goto-char encl-decl)
  1393. (or (c-safe (forward-list)
  1394. (1- (point)))
  1395. orig-point-max)))
  1396. encl-decl)
  1397. (narrow-to-region orig-point-min orig-point-max)
  1398. nil)))
  1399. (eval-and-compile
  1400. (defmacro c-while-widening-to-decl-block (condition)
  1401. ;; Repeatedly evaluate CONDITION until it returns nil. After each
  1402. ;; evaluation, if `c-defun-tactic' is set appropriately, widen to innards
  1403. ;; of the next enclosing declaration block (e.g. namespace, class), or the
  1404. ;; buffer's original restriction.
  1405. ;;
  1406. ;; This is a very special purpose macro, which assumes the existence of
  1407. ;; several variables. It is for use only in c-beginning-of-defun and
  1408. ;; c-end-of-defun.
  1409. `(while
  1410. (and ,condition
  1411. (eq c-defun-tactic 'go-outward)
  1412. lim)
  1413. (setq paren-state (c-whack-state-after lim paren-state))
  1414. (setq lim (c-widen-to-enclosing-decl-scope
  1415. paren-state orig-point-min orig-point-max))
  1416. (setq where 'in-block))))
  1417. (defun c-beginning-of-defun (&optional arg)
  1418. "Move backward to the beginning of a defun.
  1419. Every top level declaration that contains a brace paren block is
  1420. considered to be a defun.
  1421. With a positive argument, move backward that many defuns. A negative
  1422. argument -N means move forward to the Nth following beginning. Return
  1423. t unless search stops due to beginning or end of buffer.
  1424. Unlike the built-in `beginning-of-defun' this tries to be smarter
  1425. about finding the char with open-parenthesis syntax that starts the
  1426. defun."
  1427. (interactive "p")
  1428. (or arg (setq arg 1))
  1429. (or (not (eq this-command 'c-beginning-of-defun))
  1430. (eq last-command 'c-beginning-of-defun)
  1431. (and transient-mark-mode mark-active)
  1432. (push-mark))
  1433. (c-save-buffer-state
  1434. (beginning-of-defun-function end-of-defun-function
  1435. (start (point))
  1436. (paren-state (copy-tree (c-parse-state))) ; This must not share list
  1437. ; structure with other users of c-state-cache.
  1438. (orig-point-min (point-min)) (orig-point-max (point-max))
  1439. lim ; Position of { which has been widened to.
  1440. where pos case-fold-search)
  1441. (save-restriction
  1442. (if (eq c-defun-tactic 'go-outward)
  1443. (setq lim (c-widen-to-enclosing-decl-scope ; e.g. class, namespace.
  1444. paren-state orig-point-min orig-point-max)))
  1445. ;; Move back out of any macro/comment/string we happen to be in.
  1446. (c-beginning-of-macro)
  1447. (setq pos (c-literal-limits))
  1448. (if pos (goto-char (car pos)))
  1449. (setq where (c-where-wrt-brace-construct))
  1450. (if (< arg 0)
  1451. ;; Move forward to the closing brace of a function.
  1452. (progn
  1453. (if (memq where '(at-function-end outwith-function))
  1454. (setq arg (1+ arg)))
  1455. (if (< arg 0)
  1456. (c-while-widening-to-decl-block
  1457. (< (setq arg (- (c-forward-to-nth-EOF-} (- arg) where))) 0)))
  1458. ;; Move forward to the next opening brace....
  1459. (when (and (= arg 0)
  1460. (progn
  1461. (c-while-widening-to-decl-block
  1462. (not (c-syntactic-re-search-forward "{" nil 'eob)))
  1463. (eq (char-before) ?{)))
  1464. (backward-char)
  1465. ;; ... and backward to the function header.
  1466. (c-beginning-of-decl-1)
  1467. t))
  1468. ;; Move backward to the opening brace of a function, making successively
  1469. ;; larger portions of the buffer visible as necessary.
  1470. (when (> arg 0)
  1471. (c-while-widening-to-decl-block
  1472. (> (setq arg (c-backward-to-nth-BOF-{ arg where)) 0)))
  1473. (when (eq arg 0)
  1474. ;; Go backward to this function's header.
  1475. (c-beginning-of-decl-1)
  1476. (setq pos (point))
  1477. ;; We're now there, modulo comments and whitespace.
  1478. ;; Try to be line oriented; position point at the closest
  1479. ;; preceding boi that isn't inside a comment, but if we hit
  1480. ;; the previous declaration then we use the current point
  1481. ;; instead.
  1482. (while (and (/= (point) (c-point 'boi))
  1483. (c-backward-single-comment)))
  1484. (if (/= (point) (c-point 'boi))
  1485. (goto-char pos)))
  1486. (c-keep-region-active)
  1487. (= arg 0)))))
  1488. (defun c-forward-to-nth-EOF-} (n where)
  1489. ;; Skip to the closing brace of the Nth function after point. If
  1490. ;; point is inside a function, this counts as the first. Point must be
  1491. ;; outside any comment/string or macro.
  1492. ;;
  1493. ;; N must be strictly positive.
  1494. ;; WHERE describes the position of point, one of the symbols `at-header',
  1495. ;; `in-header', `in-block', `in-trailer', `at-function-end',
  1496. ;; `outwith-function' as returned by c-where-wrt-brace-construct.
  1497. ;;
  1498. ;; If we run out of functions, leave point at EOB. Return zero on success,
  1499. ;; otherwise the number of }s still to go.
  1500. ;;
  1501. ;; This function may do hidden buffer changes.
  1502. (cond
  1503. ;; What we do to go forward over the first defun depends on where we
  1504. ;; start. We go to the closing brace of that defun, even when we go
  1505. ;; backwards to it (in a "struct foo {...} bar ;").
  1506. ((eobp))
  1507. ((eq where 'in-block)
  1508. (goto-char (c-least-enclosing-brace (c-parse-state)))
  1509. (forward-sexp)
  1510. (setq n (1- n)))
  1511. ((eq where 'in-trailer)
  1512. (c-syntactic-skip-backward "^}")
  1513. (setq n (1- n)))
  1514. ((memq where '(at-function-end outwith-function at-header in-header))
  1515. (when (c-syntactic-re-search-forward "{" nil 'eob)
  1516. (backward-char)
  1517. (forward-sexp)
  1518. (setq n (1- n))))
  1519. (t (error "c-forward-to-nth-EOF-}: `where' is %s" where)))
  1520. ;; Each time round the loop, go forward to a "}" at the outermost level.
  1521. (while (and (> n 0) (not (eobp)))
  1522. ;(c-parse-state) ; This call speeds up the following one by a factor
  1523. ; of ~6. Hmmm. 2006/4/5.
  1524. (when (c-syntactic-re-search-forward "{" nil 'eob)
  1525. (backward-char)
  1526. (forward-sexp))
  1527. (setq n (1- n)))
  1528. n)
  1529. (defun c-end-of-defun (&optional arg)
  1530. "Move forward to the end of a top level declaration.
  1531. With argument, do it that many times. Negative argument -N means move
  1532. back to Nth preceding end. Returns t unless search stops due to
  1533. beginning or end of buffer.
  1534. An end of a defun occurs right after the close-parenthesis that matches
  1535. the open-parenthesis that starts a defun; see `beginning-of-defun'."
  1536. (interactive "p")
  1537. (or arg (setq arg 1))
  1538. (or (not (eq this-command 'c-end-of-defun))
  1539. (eq last-command 'c-end-of-defun)
  1540. (and transient-mark-mode mark-active)
  1541. (push-mark))
  1542. (c-save-buffer-state
  1543. (beginning-of-defun-function end-of-defun-function
  1544. (start (point))
  1545. (paren-state (copy-tree (c-parse-state))) ; This must not share list
  1546. ; structure with other users of c-state-cache.
  1547. (orig-point-min (point-min)) (orig-point-max (point-max))
  1548. lim
  1549. where pos case-fold-search)
  1550. (save-restriction
  1551. (if (eq c-defun-tactic 'go-outward)
  1552. (setq lim (c-widen-to-enclosing-decl-scope ; e.g. class, namespace
  1553. paren-state orig-point-min orig-point-max)))
  1554. ;; Move back out of any macro/comment/string we happen to be in.
  1555. (c-beginning-of-macro)
  1556. (setq pos (c-literal-limits))
  1557. (if pos (goto-char (car pos)))
  1558. (setq where (c-where-wrt-brace-construct))
  1559. (if (< arg 0)
  1560. ;; Move backwards to the } of a function
  1561. (progn
  1562. (if (memq where '(at-header outwith-function))
  1563. (setq arg (1+ arg)))
  1564. (if (< arg 0)
  1565. (c-while-widening-to-decl-block
  1566. (< (setq arg (- (c-backward-to-nth-BOF-{ (- arg) where))) 0)))
  1567. (if (= arg 0)
  1568. (c-while-widening-to-decl-block
  1569. (progn (c-syntactic-skip-backward "^}")
  1570. (not (eq (char-before) ?}))))))
  1571. ;; Move forward to the } of a function
  1572. (if (> arg 0)
  1573. (c-while-widening-to-decl-block
  1574. (> (setq arg (c-forward-to-nth-EOF-} arg where)) 0))))
  1575. ;; Do we need to move forward from the brace to the semicolon?
  1576. (when (eq arg 0)
  1577. (if (c-in-function-trailer-p) ; after "}" of struct/enum, etc.
  1578. (c-syntactic-re-search-forward ";"))
  1579. (setq pos (point))
  1580. ;; We're there now, modulo comments and whitespace.
  1581. ;; Try to be line oriented; position point after the next
  1582. ;; newline that isn't inside a comment, but if we hit the
  1583. ;; next declaration then we use the current point instead.
  1584. (while (and (not (bolp))
  1585. (not (looking-at "\\s *$"))
  1586. (c-forward-single-comment)))
  1587. (cond ((bolp))
  1588. ((looking-at "\\s *$")
  1589. (forward-line 1))
  1590. (t
  1591. (goto-char pos))))
  1592. (c-keep-region-active)
  1593. (= arg 0))))
  1594. (defun c-defun-name ()
  1595. "Return the name of the current defun, or NIL if there isn't one.
  1596. \"Defun\" here means a function, or other top level construct
  1597. with a brace block."
  1598. (interactive)
  1599. (c-save-buffer-state
  1600. (beginning-of-defun-function end-of-defun-function
  1601. where pos name-end case-fold-search)
  1602. (save-restriction
  1603. (widen)
  1604. (save-excursion
  1605. ;; Move back out of any macro/comment/string we happen to be in.
  1606. (c-beginning-of-macro)
  1607. (setq pos (c-literal-limits))
  1608. (if pos (goto-char (car pos)))
  1609. (setq where (c-where-wrt-brace-construct))
  1610. ;; Move to the beginning of the current defun, if any, if we're not
  1611. ;; already there.
  1612. (if (eq where 'outwith-function)
  1613. nil
  1614. (unless (eq where 'at-header)
  1615. (c-backward-to-nth-BOF-{ 1 where)
  1616. (c-beginning-of-decl-1))
  1617. ;; Pick out the defun name, according to the type of defun.
  1618. (cond
  1619. ;; struct, union, enum, or similar:
  1620. ((and (looking-at c-type-prefix-key)
  1621. (progn (c-forward-token-2 2) ; over "struct foo "
  1622. (or (eq (char-after) ?\{)
  1623. (looking-at c-symbol-key)))) ; "struct foo bar ..."
  1624. (save-match-data (c-forward-token-2))
  1625. (when (eq (char-after) ?\{)
  1626. (c-backward-token-2)
  1627. (looking-at c-symbol-key))
  1628. (match-string-no-properties 0))
  1629. ((looking-at "DEFUN\\_>")
  1630. ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory
  1631. ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK
  1632. (down-list 1)
  1633. (c-forward-syntactic-ws)
  1634. (when (eq (char-after) ?\")
  1635. (forward-sexp 1)
  1636. (c-forward-token-2)) ; over the comma and following WS.
  1637. (buffer-substring-no-properties
  1638. (point)
  1639. (progn
  1640. (c-forward-token-2)
  1641. (when (looking-at ":") ; CLISP: DEFUN(PACKAGE:LISP-SYMBOL,...)
  1642. (skip-chars-forward "^,"))
  1643. (c-backward-syntactic-ws)
  1644. (point))))
  1645. ((looking-at "DEF[a-zA-Z0-9_]* *( *\\([^, ]*\\) *,")
  1646. ;; DEFCHECKER(sysconf_arg,prefix=_SC,default=, ...) ==> sysconf_arg
  1647. ;; DEFFLAGSET(syslog_opt_flags,LOG_PID ...) ==> syslog_opt_flags
  1648. (match-string-no-properties 1))
  1649. (t
  1650. ;; Normal function or initializer.
  1651. (when (c-syntactic-re-search-forward "[{(]" nil t)
  1652. (backward-char)
  1653. (c-backward-syntactic-ws)
  1654. (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
  1655. (c-backward-token-2)
  1656. (c-backward-syntactic-ws))
  1657. (setq name-end (point))
  1658. (c-backward-token-2)
  1659. (buffer-substring-no-properties (point) name-end)))))))))
  1660. (defun c-declaration-limits (near)
  1661. ;; Return a cons of the beginning and end positions of the current
  1662. ;; top level declaration or macro. If point is not inside any then
  1663. ;; nil is returned, unless NEAR is non-nil in which case the closest
  1664. ;; following one is chosen instead (if there is any). The end
  1665. ;; position is at the next line, providing there is one before the
  1666. ;; declaration.
  1667. ;;
  1668. ;; This function might do hidden buffer changes.
  1669. (save-excursion
  1670. (save-restriction
  1671. (when (eq c-defun-tactic 'go-outward)
  1672. (c-narrow-to-most-enclosing-decl-block t) ; e.g. class, namespace
  1673. (or (save-restriction
  1674. (c-narrow-to-most-enclosing-decl-block nil)
  1675. ;; Note: Some code duplication in `c-beginning-of-defun' and
  1676. ;; `c-end-of-defun'.
  1677. (catch 'exit
  1678. (let ((start (point))
  1679. (paren-state (c-parse-state))
  1680. lim pos end-pos)
  1681. (unless (c-safe
  1682. (goto-char (c-least-enclosing-brace paren-state))
  1683. ;; If we moved to the outermost enclosing paren
  1684. ;; then we can use c-safe-position to set the
  1685. ;; limit. Can't do that otherwise since the
  1686. ;; earlier paren pair on paren-state might very
  1687. ;; well be part of the declaration we should go
  1688. ;; to.
  1689. (setq lim (c-safe-position (point) paren-state))
  1690. t)
  1691. ;; At top level. Make sure we aren't inside a literal.
  1692. (setq pos (c-literal-limits
  1693. (c-safe-position (point) paren-state)))
  1694. (if pos (goto-char (car pos))))
  1695. (when (c-beginning-of-macro)
  1696. (throw 'exit
  1697. (cons (point)
  1698. (save-excursion
  1699. (c-end-of-macro)
  1700. (forward-line 1)
  1701. (point)))))
  1702. (setq pos (point))
  1703. (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
  1704. (= pos (point)))
  1705. ;; We moved back over the previous defun. Skip to the next
  1706. ;; one. Not using c-forward-syntactic-ws here since we
  1707. ;; should not skip a macro. We can also be directly after
  1708. ;; the block in a `c-opt-block-decls-with-vars-key'
  1709. ;; declaration, but then we won't move significantly far
  1710. ;; here.
  1711. (goto-char pos)
  1712. (c-forward-comments)
  1713. (when (and near (c-beginning-of-macro))
  1714. (throw 'exit
  1715. (cons (point)
  1716. (save-excursion
  1717. (c-end-of-macro)
  1718. (forward-line 1)
  1719. (point))))))
  1720. (if (eobp) (throw 'exit nil))
  1721. ;; Check if `c-beginning-of-decl-1' put us after the block in a
  1722. ;; declaration that doesn't end there. We're searching back and
  1723. ;; forth over the block here, which can be expensive.
  1724. (setq pos (point))
  1725. (if (and c-opt-block-decls-with-vars-key
  1726. (progn
  1727. (c-backward-syntactic-ws)
  1728. (eq (char-before) ?}))
  1729. (eq (car (c-beginning-of-decl-1))
  1730. 'previous)
  1731. (save-excursion
  1732. (c-end-of-decl-1)
  1733. (and (> (point) pos)
  1734. (setq end-pos (point)))))
  1735. nil
  1736. (goto-char pos))
  1737. (if (and (not near) (> (point) start))
  1738. nil
  1739. ;; Try to be line oriented; position the limits at the
  1740. ;; closest preceding boi, and after the next newline, that
  1741. ;; isn't inside a comment, but if we hit a neighboring
  1742. ;; declaration then we instead use the exact declaration
  1743. ;; limit in that direction.
  1744. (cons (progn
  1745. (setq pos (point))
  1746. (while (and (/= (point) (c-point 'boi))
  1747. (c-backward-single-comment)))
  1748. (if (/= (point) (c-point 'boi))
  1749. pos
  1750. (point)))
  1751. (progn
  1752. (if end-pos
  1753. (goto-char end-pos)
  1754. (c-end-of-decl-1))
  1755. (setq pos (point))
  1756. (while (and (not (bolp))
  1757. (not (looking-at "\\s *$"))
  1758. (c-forward-single-comment)))
  1759. (cond ((bolp)
  1760. (point))
  1761. ((looking-at "\\s *$")
  1762. (forward-line 1)
  1763. (point))
  1764. (t
  1765. pos))))))))
  1766. (and (not near)
  1767. (goto-char (point-min))
  1768. (c-forward-decl-or-cast-1 -1 nil nil)
  1769. (eq (char-after) ?\{)
  1770. (cons (point-min) (point-max))))))))
  1771. (defun c-mark-function ()
  1772. "Put mark at end of the current top-level declaration or macro, point at beginning.
  1773. If point is not inside any then the closest following one is
  1774. chosen. Each successive call of this command extends the marked
  1775. region by one function.
  1776. A mark is left where the command started, unless the region is already active
  1777. \(in Transient Mark mode).
  1778. As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
  1779. function does not require the declaration to contain a brace block."
  1780. (interactive)
  1781. (let (decl-limits case-fold-search)
  1782. (c-save-buffer-state nil
  1783. ;; We try to be line oriented, unless there are several
  1784. ;; declarations on the same line.
  1785. (if (looking-at c-syntactic-eol)
  1786. (c-backward-token-2 1 nil (c-point 'bol)))
  1787. (setq decl-limits (c-declaration-limits t)))
  1788. (if (not decl-limits)
  1789. (error "Cannot find any declaration")
  1790. (let* ((extend-region-p
  1791. (and (eq this-command 'c-mark-function)
  1792. (eq last-command 'c-mark-function)))
  1793. (push-mark-p (and (eq this-command 'c-mark-function)
  1794. (not extend-region-p)
  1795. (not (and transient-mark-mode mark-active)))))
  1796. (if push-mark-p (push-mark (point)))
  1797. (if extend-region-p
  1798. (progn
  1799. (exchange-point-and-mark)
  1800. (setq decl-limits (c-declaration-limits t))
  1801. (when (not decl-limits)
  1802. (exchange-point-and-mark)
  1803. (error "Cannot find any declaration"))
  1804. (goto-char (cdr decl-limits))
  1805. (exchange-point-and-mark))
  1806. (goto-char (car decl-limits))
  1807. (push-mark (cdr decl-limits) nil t))))))
  1808. (defun c-cpp-define-name ()
  1809. "Return the name of the current CPP macro, or NIL if we're not in one."
  1810. (interactive)
  1811. (let (case-fold-search)
  1812. (save-excursion
  1813. (and c-opt-cpp-macro-define-start
  1814. (c-beginning-of-macro)
  1815. (looking-at c-opt-cpp-macro-define-start)
  1816. (match-string-no-properties 1)))))
  1817. ;; Movement by statements.
  1818. (defun c-in-comment-line-prefix-p ()
  1819. ;; Point is within a comment. Is it also within a comment-prefix?
  1820. ;; Space at BOL which precedes a comment-prefix counts as part of it.
  1821. ;;
  1822. ;; This function might do hidden buffer changes.
  1823. (let ((here (point)))
  1824. (save-excursion
  1825. (beginning-of-line)
  1826. (skip-chars-forward " \t")
  1827. (and (looking-at c-current-comment-prefix)
  1828. (/= (match-beginning 0) (match-end 0))
  1829. (< here (match-end 0))))))
  1830. (defun c-narrow-to-comment-innards (range)
  1831. ;; Narrow to the "inside" of the comment (block) defined by range, as
  1832. ;; follows:
  1833. ;;
  1834. ;; A c-style block comment has its opening "/*" and its closing "*/" (if
  1835. ;; present) removed. A c++-style line comment retains its opening "//" but
  1836. ;; has any final NL removed. If POINT is currently outwith these innards,
  1837. ;; move it to the appropriate boundary.
  1838. ;;
  1839. ;; This narrowing simplifies the sentence movement functions, since it
  1840. ;; eliminates awkward things at the boundaries of the comment (block).
  1841. ;;
  1842. ;; This function might do hidden buffer changes.
  1843. (let* ((lit-type (c-literal-type range))
  1844. (beg (if (eq lit-type 'c) (+ (car range) 2) (car range)))
  1845. (end (if (eq lit-type 'c)
  1846. (if (and (eq (char-before (cdr range)) ?/)
  1847. (eq (char-before (1- (cdr range))) ?*))
  1848. (- (cdr range) 2)
  1849. (point-max))
  1850. (if (eq (cdr range) (point-max))
  1851. (point-max)
  1852. (- (cdr range) 1)))))
  1853. (if (> (point) end)
  1854. (goto-char end)) ; This would be done automatically by ...
  1855. (if (< (point) beg)
  1856. (goto-char beg)) ; ... narrow-to-region but is not documented.
  1857. (narrow-to-region beg end)))
  1858. (defun c-beginning-of-sentence-in-comment (range)
  1859. ;; Move backwards to the "beginning of a sentence" within the comment
  1860. ;; defined by RANGE, a cons of its starting and ending positions. If we
  1861. ;; find a BOS, return NIL. Otherwise, move point to just before the start
  1862. ;; of the comment and return T.
  1863. ;;
  1864. ;; The BOS is either text which follows a regexp match of sentence-end,
  1865. ;; or text which is a beginning of "paragraph".
  1866. ;; Comment-prefixes are treated like WS when calculating BOSes or BOPs.
  1867. ;;
  1868. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1869. ;; It is not a general function, but is intended only for calling from
  1870. ;; c-move-over-sentence. Not all preconditions have been explicitly stated.
  1871. ;;
  1872. ;; This function might do hidden buffer changes.
  1873. (save-match-data
  1874. (let ((start-point (point)))
  1875. (save-restriction
  1876. (c-narrow-to-comment-innards range) ; This may move point back.
  1877. (let* ((here (point))
  1878. last
  1879. (here-filler ; matches WS and comment-prefixes at point.
  1880. (concat "\\=\\(^[ \t]*\\(" c-current-comment-prefix "\\)"
  1881. "\\|[ \t\n\r\f]\\)*"))
  1882. (prefix-at-bol-here ; matches WS and prefix at BOL, just before point
  1883. (concat "^[ \t]*\\(" c-current-comment-prefix "\\)[ \t\n\r\f]*\\="))
  1884. ;; First, find the previous paragraph start, if any.
  1885. (par-beg ; point where non-WS/non-prefix text of paragraph starts.
  1886. (save-excursion
  1887. (forward-paragraph -1) ; uses cc-mode values of
  1888. ; paragraph-\(start\|separate\)
  1889. (if (> (re-search-forward here-filler nil t) here)
  1890. (goto-char here))
  1891. (when (>= (point) here)
  1892. (forward-paragraph -2)
  1893. (if (> (re-search-forward here-filler nil t) here)
  1894. (goto-char here)))
  1895. (point))))
  1896. ;; Now seek successively earlier sentence ends between PAR-BEG and
  1897. ;; HERE, until the "start of sentence" following it is earlier than
  1898. ;; HERE, or we hit PAR-BEG. Beware of comment prefixes!
  1899. (while (and (re-search-backward (c-sentence-end) par-beg 'limit)
  1900. (setq last (point))
  1901. (goto-char (match-end 0)) ; tentative beginning of sentence
  1902. (or (>= (point) here)
  1903. (and (not (bolp)) ; Found a non-blank comment-prefix?
  1904. (save-excursion
  1905. (if (re-search-backward prefix-at-bol-here nil t)
  1906. (/= (match-beginning 1) (match-end 1)))))
  1907. (progn ; Skip the crud to find a real b-o-s.
  1908. (if (c-in-comment-line-prefix-p)
  1909. (beginning-of-line))
  1910. (re-search-forward here-filler) ; always succeeds.
  1911. (>= (point) here))))
  1912. (goto-char last))
  1913. (re-search-forward here-filler)))
  1914. (if (< (point) start-point)
  1915. nil
  1916. (goto-char (car range))
  1917. t))))
  1918. (defun c-end-of-sentence-in-comment (range)
  1919. ;; Move forward to the "end of a sentence" within the comment defined by
  1920. ;; RANGE, a cons of its starting and ending positions (enclosing the opening
  1921. ;; comment delimiter and the terminating */ or newline). If we find an EOS,
  1922. ;; return NIL. Otherwise, move point to just after the end of the comment
  1923. ;; and return T.
  1924. ;;
  1925. ;; The EOS is just after the non-WS part of the next match of the regexp
  1926. ;; sentence-end. Typically, this is just after one of [.!?]. If there is
  1927. ;; no sentence-end match following point, any WS before the end of the
  1928. ;; comment will count as EOS, providing we're not already in it.
  1929. ;;
  1930. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1931. ;; It is not a general function, but is intended only for calling from
  1932. ;; c-move-over-sentence.
  1933. ;;
  1934. ;; This function might do hidden buffer changes.
  1935. (save-match-data
  1936. (let ((start-point (point))
  1937. ;; (lit-type (c-literal-type range)) ; Commented out, 2005/11/23, ACM
  1938. )
  1939. (save-restriction
  1940. (c-narrow-to-comment-innards range) ; This might move point forwards.
  1941. (let* ((here (point))
  1942. (par-end ; EOL position of last text in current/next paragraph.
  1943. (save-excursion
  1944. ;; The cc-mode values of paragraph-\(start\|separate\), set
  1945. ;; in c-setup-paragraph-variables, are used in the
  1946. ;; following.
  1947. (forward-paragraph 1)
  1948. (if (eq (preceding-char) ?\n) (forward-char -1))
  1949. (when (<= (point) here) ; can happen, e.g., when HERE is at EOL.
  1950. (goto-char here)
  1951. (forward-paragraph 2)
  1952. (if (eq (preceding-char) ?\n) (forward-char -1)))
  1953. (point)))
  1954. last
  1955. (prefix-at-bol-here
  1956. (concat "^[ \t]*\\(" c-current-comment-prefix "\\)\\=")))
  1957. ;; Go forward one "comment-prefix which looks like sentence-end"
  1958. ;; each time round the following:
  1959. (while (and (re-search-forward (c-sentence-end) par-end 'limit)
  1960. (progn
  1961. (setq last (point))
  1962. (skip-chars-backward " \t\n")
  1963. (or (and (not (bolp))
  1964. (re-search-backward prefix-at-bol-here nil t)
  1965. (/= (match-beginning 1) (match-end 1)))
  1966. (<= (point) here))))
  1967. (goto-char last))
  1968. ;; Take special action if we're up against the end of a comment (of
  1969. ;; either sort): Leave point just after the last non-ws text.
  1970. (if (eq (point) (point-max))
  1971. (while (or (/= (skip-chars-backward " \t\n") 0)
  1972. (and (re-search-backward prefix-at-bol-here nil t)
  1973. (/= (match-beginning 1) (match-end 1))))))))
  1974. (if (> (point) start-point)
  1975. nil
  1976. (goto-char (cdr range))
  1977. t))))
  1978. (defun c-beginning-of-sentence-in-string (range)
  1979. ;; Move backwards to the "beginning of a sentence" within the string defined
  1980. ;; by RANGE, a cons of its starting and ending positions (enclosing the
  1981. ;; string quotes). If we find a BOS, return NIL. Otherwise, move point to
  1982. ;; just before the start of the string and return T.
  1983. ;;
  1984. ;; The BOS is either the text which follows a regexp match of sentence-end
  1985. ;; or text which is a beginning of "paragraph". For the purposes of
  1986. ;; determining paragraph boundaries, escaped newlines are treated as
  1987. ;; ordinary newlines.
  1988. ;;
  1989. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1990. ;; It is not a general function, but is intended only for calling from
  1991. ;; c-move-over-sentence.
  1992. ;;
  1993. ;; This function might do hidden buffer changes.
  1994. (save-match-data
  1995. (let* ((here (point)) last
  1996. (end (1- (cdr range)))
  1997. (here-filler ; matches WS and escaped newlines at point.
  1998. "\\=\\([ \t\n\r\f]\\|\\\\[\n\r]\\)*")
  1999. ;; Enhance paragraph-start and paragraph-separate also to recognize
  2000. ;; blank lines terminated by escaped EOLs. IT MAY WELL BE that
  2001. ;; these values should be customizable user options, or something.
  2002. (paragraph-start c-string-par-start)
  2003. (paragraph-separate c-string-par-separate)
  2004. (par-beg ; beginning of current (or previous) paragraph.
  2005. (save-excursion
  2006. (save-restriction
  2007. (narrow-to-region (1+ (car range)) end)
  2008. (forward-paragraph -1) ; uses above values of
  2009. ; paragraph-\(start\|separate\)
  2010. (if (> (re-search-forward here-filler nil t) here)
  2011. (goto-char here))
  2012. (when (>= (point) here)
  2013. (forward-paragraph -2)
  2014. (if (> (re-search-forward here-filler nil t) here)
  2015. (goto-char here)))
  2016. (point)))))
  2017. ;; Now see if we can find a sentence end after PAR-BEG.
  2018. (while (and (re-search-backward c-sentence-end-with-esc-eol par-beg 'limit)
  2019. (setq last (point))
  2020. (goto-char (match-end 0))
  2021. (or (> (point) end)
  2022. (progn
  2023. (re-search-forward
  2024. here-filler end t) ; always succeeds. Use end rather
  2025. ; than here, in case point starts
  2026. ; beyond the closing quote.
  2027. (>= (point) here))))
  2028. (goto-char last))
  2029. (re-search-forward here-filler here t)
  2030. (if (< (point) here)
  2031. nil
  2032. (goto-char (car range))
  2033. t))))
  2034. (defun c-end-of-sentence-in-string (range)
  2035. ;; Move forward to the "end of a sentence" within the string defined by
  2036. ;; RANGE, a cons of its starting and ending positions. If we find an EOS,
  2037. ;; return NIL. Otherwise, move point to just after the end of the string
  2038. ;; and return T.
  2039. ;;
  2040. ;; The EOS is just after the non-WS part of the next match of the regexp
  2041. ;; sentence-end. Typically, this is just after one of [.!?]. If there is
  2042. ;; no sentence-end match following point, any WS before the end of the
  2043. ;; string will count as EOS, providing we're not already in it.
  2044. ;;
  2045. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  2046. ;; It is not a general function, but is intended only for calling from
  2047. ;; c-move-over-sentence.
  2048. ;;
  2049. ;; This function might do hidden buffer changes.
  2050. (save-match-data
  2051. (let* ((here (point))
  2052. last
  2053. ;; Enhance paragraph-start and paragraph-separate to recognize
  2054. ;; blank lines terminated by escaped EOLs.
  2055. (paragraph-start c-string-par-start)
  2056. (paragraph-separate c-string-par-separate)
  2057. (par-end ; EOL position of last text in current/next paragraph.
  2058. (save-excursion
  2059. (save-restriction
  2060. (narrow-to-region (car range) (1- (cdr range)))
  2061. ;; The above values of paragraph-\(start\|separate\) are used
  2062. ;; in the following.
  2063. (forward-paragraph 1)
  2064. (setq last (point))
  2065. ;; (re-search-backward filler-here nil t) would find an empty
  2066. ;; string. Therefore we simulate it by the following:
  2067. (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
  2068. (re-search-backward "\\\\\\($\\)\\=" nil t)))
  2069. (unless (> (point) here)
  2070. (goto-char last)
  2071. (forward-paragraph 1)
  2072. (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
  2073. (re-search-backward "\\\\\\($\\)\\=" nil t))))
  2074. (point)))))
  2075. ;; Try to go forward a sentence.
  2076. (when (re-search-forward c-sentence-end-with-esc-eol par-end 'limit)
  2077. (setq last (point))
  2078. (while (or (/= (skip-chars-backward " \t\n") 0)
  2079. (re-search-backward "\\\\\\($\\)\\=" nil t))))
  2080. ;; Did we move a sentence, or did we hit the end of the string?
  2081. (if (> (point) here)
  2082. nil
  2083. (goto-char (cdr range))
  2084. t))))
  2085. (defun c-ascertain-preceding-literal ()
  2086. ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
  2087. ;; If a literal is the next thing (aside from whitespace) to be found before
  2088. ;; point, return a cons of its start.end positions (enclosing the
  2089. ;; delimiters). Otherwise return NIL.
  2090. ;;
  2091. ;; This function might do hidden buffer changes.
  2092. (save-excursion
  2093. (c-collect-line-comments
  2094. (let ((here (point))
  2095. pos)
  2096. (if (c-backward-single-comment)
  2097. (cons (point) (progn (c-forward-single-comment) (point)))
  2098. (save-restriction
  2099. ;; to prevent `looking-at' seeing a " at point.
  2100. (narrow-to-region (point-min) here)
  2101. (when
  2102. (or
  2103. ;; An EOL can act as an "open string" terminator in AWK.
  2104. (looking-at c-ws*-string-limit-regexp)
  2105. (and (not (bobp))
  2106. (progn (backward-char)
  2107. (looking-at c-string-limit-regexp))))
  2108. (goto-char (match-end 0)) ; just after the string terminator.
  2109. (setq pos (point))
  2110. (c-safe (c-backward-sexp 1) ; move back over the string.
  2111. (cons (point) pos)))))))))
  2112. (defun c-ascertain-following-literal ()
  2113. ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
  2114. ;; If a literal is the next thing (aside from whitespace) following point,
  2115. ;; return a cons of its start.end positions (enclosing the delimiters).
  2116. ;; Otherwise return NIL.
  2117. ;;
  2118. ;; This function might do hidden buffer changes.
  2119. (save-excursion
  2120. (c-collect-line-comments
  2121. (let (pos)
  2122. (c-skip-ws-forward)
  2123. (if (looking-at c-string-limit-regexp) ; string-delimiter.
  2124. (cons (point) (or (c-safe (progn (c-forward-sexp 1) (point)))
  2125. (point-max)))
  2126. (setq pos (point))
  2127. (if (c-forward-single-comment)
  2128. (cons pos (point))))))))
  2129. (defun c-after-statement-terminator-p () ; Should we pass in LIM here?
  2130. ;; Does point immediately follow a statement "terminator"? A virtual
  2131. ;; semicolon is regarded here as such. So is an opening brace ;-)
  2132. ;;
  2133. ;; This function might do hidden buffer changes.
  2134. (or (save-excursion
  2135. (backward-char)
  2136. (and (looking-at "[;{}]")
  2137. (not (and c-special-brace-lists ; Pike special brace lists.
  2138. (eq (char-after) ?{)
  2139. (c-looking-at-special-brace-list)))))
  2140. (c-at-vsemi-p)
  2141. ;; The following (for macros) is not strict about exactly where we are
  2142. ;; wrt white space at the end of the macro. Doesn't seem to matter too
  2143. ;; much. ACM 2004/3/29.
  2144. (let (eom)
  2145. (save-excursion
  2146. (if (c-beginning-of-macro)
  2147. (setq eom (progn (c-end-of-macro)
  2148. (point)))))
  2149. (when eom
  2150. (save-excursion
  2151. (c-forward-comments)
  2152. (>= (point) eom))))))
  2153. (defun c-back-over-illiterals (macro-start)
  2154. ;; Move backwards over code which isn't a literal (i.e. comment or string),
  2155. ;; stopping before reaching BOB or a literal or the boundary of a
  2156. ;; preprocessor statement or the "beginning of a statement". MACRO-START is
  2157. ;; the position of the '#' beginning the current preprocessor directive, or
  2158. ;; NIL if we're not in such.
  2159. ;;
  2160. ;; Return a cons (A.B), where
  2161. ;; A is NIL if we moved back to a BOS (and know it), T otherwise (we
  2162. ;; didn't move, or we hit a literal, or we're not sure about BOS).
  2163. ;; B is MACRO-BOUNDARY if we are about to cross the boundary out of or
  2164. ;; into a macro, otherwise LITERAL if we've hit a literal, otherwise NIL
  2165. ;;
  2166. ;; The total collection of returned values is as follows:
  2167. ;; (nil . nil): Found a BOS whilst remaining inside the illiterals.
  2168. ;; (t . literal): No BOS found: only a comment/string. We _might_ be at
  2169. ;; a BOS - the caller must check this.
  2170. ;; (nil . macro-boundary): only happens with non-nil macro-start. We've
  2171. ;; moved and reached the opening # of the macro.
  2172. ;; (t . macro-boundary): Every other circumstance in which we're at a
  2173. ;; macro-boundary. We might be at a BOS.
  2174. ;;
  2175. ;; Point is left either at the beginning-of-statement, or at the last non-ws
  2176. ;; code before encountering the literal/BOB or macro-boundary.
  2177. ;;
  2178. ;; Note that this function moves within either preprocessor commands
  2179. ;; (macros) or normal code, but will not cross a boundary between the two,
  2180. ;; or between two distinct preprocessor commands.
  2181. ;;
  2182. ;; Stop before `{' and after `;', `{', `}' and `};' when not followed by `}'
  2183. ;; or `)', but on the other side of the syntactic ws. Move by sexps and
  2184. ;; move into parens. Also stop before `#' when it's at boi on a line.
  2185. ;;
  2186. ;; This function might do hidden buffer changes.
  2187. (save-match-data
  2188. (let ((here (point))
  2189. last) ; marks the position of non-ws code, what'll be BOS if, say, a
  2190. ; semicolon precedes it.
  2191. (catch 'done
  2192. (while t ;; We go back one "token" each iteration of the loop.
  2193. (setq last (point))
  2194. (cond
  2195. ;; Stop at the token after a comment.
  2196. ((c-backward-single-comment) ; Also functions as backwards-ws.
  2197. (goto-char last)
  2198. (throw 'done '(t . literal)))
  2199. ;; If we've gone back over a LF, we might have moved into or out of
  2200. ;; a preprocessor line.
  2201. ((and (save-excursion
  2202. (beginning-of-line)
  2203. (re-search-forward "\\(^\\|[^\\]\\)[\n\r]" last t))
  2204. (if macro-start
  2205. (< (point) macro-start)
  2206. (c-beginning-of-macro)))
  2207. (goto-char last)
  2208. ;; Return a car of NIL ONLY if we've hit the opening # of a macro.
  2209. (throw 'done (cons (or (eq (point) here)
  2210. (not macro-start))
  2211. 'macro-boundary)))
  2212. ;; Have we found a virtual semicolon? If so, stop, unless the next
  2213. ;; statement is where we started from.
  2214. ((and (c-at-vsemi-p)
  2215. (< last here)
  2216. (not (memq (char-after last) '(?\) ?})))) ; we've moved back from ) or }
  2217. (goto-char last)
  2218. (throw 'done '(nil . nil)))
  2219. ;; Hit the beginning of the buffer/region?
  2220. ((bobp)
  2221. (if (/= here last)
  2222. (goto-char last))
  2223. (throw 'done '(nil . nil)))
  2224. ;; Move back a character.
  2225. ((progn (backward-char) nil))
  2226. ;; Stop at "{" (unless it's a PIKE special brace list.)
  2227. ((eq (char-after) ?\{)
  2228. (if (and c-special-brace-lists
  2229. (c-looking-at-special-brace-list))
  2230. (skip-syntax-backward "w_") ; Speedup only.
  2231. (if (/= here last)
  2232. (goto-char last))
  2233. (throw 'done '(nil . nil))))
  2234. ;; Have we reached the start of a macro? This always counts as
  2235. ;; BOS. (N.B. I don't think (eq (point) here) can ever be true
  2236. ;; here. FIXME!!! ACM 2004/3/29)
  2237. ((and macro-start (eq (point) macro-start))
  2238. (throw 'done (cons (eq (point) here) 'macro-boundary)))
  2239. ;; Stop at token just after "}" or ";".
  2240. ((looking-at "[;}]")
  2241. ;; If we've gone back over ;, {, or }, we're done.
  2242. (if (or (= here last)
  2243. (memq (char-after last) '(?\) ?}))) ; we've moved back from ) or }
  2244. (if (and (eq (char-before) ?}) ; If };, treat them as a unit.
  2245. (eq (char-after) ?\;))
  2246. (backward-char))
  2247. (goto-char last) ; To the statement starting after the ; or }.
  2248. (throw 'done '(nil . nil))))
  2249. ;; Stop at the token after a string.
  2250. ((looking-at c-string-limit-regexp) ; Just gone back over a string terminator?
  2251. (goto-char last)
  2252. (throw 'done '(t . literal)))
  2253. ;; Nothing special: go back word characters.
  2254. (t (skip-syntax-backward "w_")) ; Speedup only.
  2255. ))))))
  2256. (defun c-forward-over-illiterals (macro-end allow-early-stop)
  2257. ;; Move forwards over code, stopping before reaching EOB or a literal
  2258. ;; (i.e. a comment/string) or the boundary of a preprocessor statement or
  2259. ;; the "end of a statement". MACRO-END is the position of the EOL/EOB which
  2260. ;; terminates the current preprocessor directive, or NIL if we're not in
  2261. ;; such.
  2262. ;;
  2263. ;; ALLOW-EARLY-STOP is non-nil if it is permissible to return without moving
  2264. ;; forward at all, should we encounter a `{'. This is an ugly kludge, but
  2265. ;; seems unavoidable. Depending on the context this function is called
  2266. ;; from, we _sometimes_ need to stop there. Currently (2004/4/3),
  2267. ;; ALLOW-EARLY-STOP is applied only to open braces, not to virtual
  2268. ;; semicolons, or anything else.
  2269. ;;
  2270. ;; Return a cons (A.B), where
  2271. ;; A is NIL if we moved forward to an EOS, or stay at one (when
  2272. ;; ALLOW-EARLY-STOP is set), T otherwise (we hit a literal).
  2273. ;; B is 'MACRO-BOUNDARY if we are about to cross the boundary out of or
  2274. ;; into a macro, otherwise 'LITERAL if we've hit a literal, otherwise NIL
  2275. ;;
  2276. ;; Point is left either after the end-of-statement, or at the last non-ws
  2277. ;; code before encountering the literal, or the # of the preprocessor
  2278. ;; statement, or at EOB [or just after last non-WS stuff??].
  2279. ;;
  2280. ;; As a clarification of "after the end-of-statement", if a comment or
  2281. ;; whitespace follows a completed AWK statement, that statement is treated
  2282. ;; as ending just after the last non-ws character before the comment.
  2283. ;;
  2284. ;; Note that this function moves within either preprocessor commands
  2285. ;; (macros) or normal code, but not both within the same invocation.
  2286. ;;
  2287. ;; Stop before `{', `}', and `#' when it's at boi on a line, but on the
  2288. ;; other side of the syntactic ws, and after `;', `}' and `};'. Only
  2289. ;; stop before `{' if at top level or inside braces, though. Move by
  2290. ;; sexps and move into parens. Also stop at eol of lines with `#' at
  2291. ;; the boi.
  2292. ;;
  2293. ;; This function might do hidden buffer changes.
  2294. (let ((here (point))
  2295. last)
  2296. (catch 'done
  2297. (while t ;; We go one "token" forward each time round this loop.
  2298. (setq last (point))
  2299. ;; If we've moved forward to a virtual semicolon, we're done.
  2300. (if (and (> last here) ; Should we check ALLOW-EARLY-STOP, here? 2004/4/3
  2301. (c-at-vsemi-p))
  2302. (throw 'done '(nil . nil)))
  2303. (c-skip-ws-forward)
  2304. (cond
  2305. ;; Gone past the end of a macro?
  2306. ((and macro-end (> (point) macro-end))
  2307. (goto-char last)
  2308. (throw 'done (cons (eq (point) here) 'macro-boundary)))
  2309. ;; About to hit a comment?
  2310. ((save-excursion (c-forward-single-comment))
  2311. (goto-char last)
  2312. (throw 'done '(t . literal)))
  2313. ;; End of buffer?
  2314. ((eobp)
  2315. (if (/= here last)
  2316. (goto-char last))
  2317. (throw 'done '(nil . nil)))
  2318. ;; If we encounter a '{', stop just after the previous token.
  2319. ((and (eq (char-after) ?{)
  2320. (not (and c-special-brace-lists
  2321. (c-looking-at-special-brace-list)))
  2322. (or allow-early-stop (/= here last))
  2323. (save-excursion ; Is this a check that we're NOT at top level?
  2324. ;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing
  2325. ;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense.
  2326. ;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g.
  2327. ;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions.
  2328. (or (not (c-safe (up-list -1) t))
  2329. (= (char-after) ?{))))
  2330. (goto-char last)
  2331. (throw 'done '(nil . nil)))
  2332. ;; End of a PIKE special brace list? If so, step over it and continue.
  2333. ((and c-special-brace-lists
  2334. (eq (char-after) ?})
  2335. (save-excursion
  2336. (and (c-safe (up-list -1) t)
  2337. (c-looking-at-special-brace-list))))
  2338. (forward-char)
  2339. (skip-syntax-forward "w_")) ; Speedup only.
  2340. ;; Have we got a '}' after having moved? If so, stop after the
  2341. ;; previous token.
  2342. ((and (eq (char-after) ?})
  2343. (/= here last))
  2344. (goto-char last)
  2345. (throw 'done '(nil . nil)))
  2346. ;; Stop if we encounter a preprocessor line. Continue if we
  2347. ;; hit a naked #
  2348. ((and c-opt-cpp-prefix
  2349. (not macro-end)
  2350. (eq (char-after) ?#)
  2351. (= (point) (c-point 'boi)))
  2352. (if (= (point) here) ; Not a macro, therefore naked #.
  2353. (forward-char)
  2354. (throw 'done '(t . macro-boundary))))
  2355. ;; Stop after a ';', '}', or "};"
  2356. ((looking-at ";\\|};?")
  2357. (goto-char (match-end 0))
  2358. (throw 'done '(nil . nil)))
  2359. ;; Found a string (this subsumes AWK regexps)?
  2360. ((looking-at c-string-limit-regexp)
  2361. (goto-char last)
  2362. (throw 'done '(t . literal)))
  2363. (t
  2364. (forward-char) ; Can't fail - we checked (eobp) earlier on.
  2365. (skip-syntax-forward "w_") ; Speedup only.
  2366. (when (and macro-end (> (point) macro-end))
  2367. (goto-char last)
  2368. (throw 'done (cons (eq (point) here) 'macro-boundary))))
  2369. )))))
  2370. (defun c-one-line-string-p (range)
  2371. ;; Is the literal defined by RANGE a string contained in a single line?
  2372. ;;
  2373. ;; This function might do hidden buffer changes.
  2374. (save-excursion
  2375. (goto-char (car range))
  2376. (and (looking-at c-string-limit-regexp)
  2377. (progn (skip-chars-forward "^\n" (cdr range))
  2378. (eq (point) (cdr range))))))
  2379. (defun c-beginning-of-statement (&optional count lim sentence-flag)
  2380. "Go to the beginning of the innermost C statement.
  2381. With prefix arg, go back N - 1 statements. If already at the
  2382. beginning of a statement then go to the beginning of the closest
  2383. preceding one, moving into nested blocks if necessary (use
  2384. \\[backward-sexp] to skip over a block). If within or next to a
  2385. comment or multiline string, move by sentences instead of statements.
  2386. When called from a program, this function takes 3 optional args: the
  2387. repetition count, a buffer position limit which is the farthest back
  2388. to search for the syntactic context, and a flag saying whether to do
  2389. sentence motion in or near comments and multiline strings.
  2390. Note that for use in programs, `c-beginning-of-statement-1' is
  2391. usually better. It has much better defined semantics than this one,
  2392. which is intended for interactive use, and might therefore change to
  2393. be more \"DWIM:ey\"."
  2394. (interactive (list (prefix-numeric-value current-prefix-arg)
  2395. nil t))
  2396. (if (< count 0)
  2397. (c-end-of-statement (- count) lim sentence-flag)
  2398. (c-save-buffer-state
  2399. ((count (or count 1))
  2400. last ; start point for going back ONE chunk. Updated each chunk movement.
  2401. (macro-fence
  2402. (save-excursion (and (not (bobp)) (c-beginning-of-macro) (point))))
  2403. res ; result from sub-function call
  2404. not-bos ; "not beginning-of-statement"
  2405. (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
  2406. ;; Go back one statement at each iteration of the following loop.
  2407. (while (and (/= count 0)
  2408. (or (not lim) (> (point) lim)))
  2409. ;; Go back one "chunk" each time round the following loop, stopping
  2410. ;; when we reach a statement boundary, etc.
  2411. (setq last (point))
  2412. (while
  2413. (cond ; Each arm of this cond returns NIL on reaching a desired
  2414. ; statement boundary, non-NIL otherwise.
  2415. ((bobp)
  2416. (setq count 0)
  2417. nil)
  2418. (range ; point is within or approaching a literal.
  2419. (cond
  2420. ;; Single line string or sentence-flag is null => skip the
  2421. ;; entire literal.
  2422. ((or (null sentence-flag)
  2423. (c-one-line-string-p range))
  2424. (goto-char (car range))
  2425. (setq range (c-ascertain-preceding-literal))
  2426. ;; N.B. The following is essentially testing for an AWK regexp
  2427. ;; at BOS:
  2428. ;; Was the previous non-ws thing an end of statement?
  2429. (save-excursion
  2430. (if macro-fence
  2431. (c-backward-comments)
  2432. (c-backward-syntactic-ws))
  2433. (not (or (bobp) (c-after-statement-terminator-p)))))
  2434. ;; Comment inside a statement or a multi-line string.
  2435. (t (when (setq res ; returns non-nil when we go out of the literal
  2436. (if (eq (c-literal-type range) 'string)
  2437. (c-beginning-of-sentence-in-string range)
  2438. (c-beginning-of-sentence-in-comment range)))
  2439. (setq range (c-ascertain-preceding-literal)))
  2440. res)))
  2441. ;; Non-literal code.
  2442. (t (setq res (c-back-over-illiterals macro-fence))
  2443. (setq not-bos ; "not reached beginning-of-statement".
  2444. (or (= (point) last)
  2445. (memq (char-after) '(?\) ?\}))
  2446. (and
  2447. (car res)
  2448. ;; We're at a tentative BOS. The next form goes
  2449. ;; back over WS looking for an end of previous
  2450. ;; statement.
  2451. (not (save-excursion
  2452. (if macro-fence
  2453. (c-backward-comments)
  2454. (c-backward-syntactic-ws))
  2455. (or (bobp) (c-after-statement-terminator-p)))))))
  2456. ;; Are we about to move backwards into or out of a
  2457. ;; preprocessor command? If so, locate its beginning.
  2458. (when (eq (cdr res) 'macro-boundary)
  2459. (save-excursion
  2460. (beginning-of-line)
  2461. (setq macro-fence
  2462. (and (not (bobp))
  2463. (progn (c-skip-ws-backward) (c-beginning-of-macro))
  2464. (point)))))
  2465. ;; Are we about to move backwards into a literal?
  2466. (when (memq (cdr res) '(macro-boundary literal))
  2467. (setq range (c-ascertain-preceding-literal)))
  2468. not-bos))
  2469. (setq last (point)))
  2470. (if (/= count 0) (setq count (1- count))))
  2471. (c-keep-region-active))))
  2472. (defun c-end-of-statement (&optional count lim sentence-flag)
  2473. "Go to the end of the innermost C statement.
  2474. With prefix arg, go forward N - 1 statements. Move forward to the end
  2475. of the next statement if already at end, and move into nested blocks
  2476. \(use \\[forward-sexp] to skip over a block). If within or next to a
  2477. comment or multiline string, move by sentences instead of statements.
  2478. When called from a program, this function takes 3 optional args: the
  2479. repetition count, a buffer position limit which is the farthest back
  2480. to search for the syntactic context, and a flag saying whether to do
  2481. sentence motion in or near comments and multiline strings."
  2482. (interactive (list (prefix-numeric-value current-prefix-arg)
  2483. nil t))
  2484. (setq count (or count 1))
  2485. (if (< count 0) (c-beginning-of-statement (- count) lim sentence-flag)
  2486. (c-save-buffer-state
  2487. (here ; start point for going forward ONE statement. Updated each statement.
  2488. (macro-fence
  2489. (save-excursion
  2490. (and (not (eobp)) (c-beginning-of-macro)
  2491. (progn (c-end-of-macro) (point)))))
  2492. res
  2493. (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
  2494. ;; Go back/forward one statement at each iteration of the following loop.
  2495. (while (and (/= count 0)
  2496. (or (not lim) (< (point) lim)))
  2497. (setq here (point)) ; ONLY HERE is HERE updated
  2498. ;; Go forward one "chunk" each time round the following loop, stopping
  2499. ;; when we reach a statement boundary, etc.
  2500. (while
  2501. (cond ; Each arm of this cond returns NIL on reaching a desired
  2502. ; statement boundary, non-NIL otherwise.
  2503. ((eobp)
  2504. (setq count 0)
  2505. nil)
  2506. (range ; point is within a literal.
  2507. (cond
  2508. ;; sentence-flag is null => skip the entire literal.
  2509. ;; or a Single line string.
  2510. ((or (null sentence-flag)
  2511. (c-one-line-string-p range))
  2512. (goto-char (cdr range))
  2513. (setq range (c-ascertain-following-literal))
  2514. ;; Is there a virtual semicolon here (e.g. for AWK)?
  2515. (not (c-at-vsemi-p)))
  2516. ;; Comment or multi-line string.
  2517. (t (when (setq res ; gets non-nil when we go out of the literal
  2518. (if (eq (c-literal-type range) 'string)
  2519. (c-end-of-sentence-in-string range)
  2520. (c-end-of-sentence-in-comment range)))
  2521. (setq range (c-ascertain-following-literal)))
  2522. ;; If we've just come forward out of a literal, check for
  2523. ;; vsemi. (N.B. AWK can't have a vsemi after a comment, but
  2524. ;; some other language may do in the future)
  2525. (and res
  2526. (not (c-at-vsemi-p))))))
  2527. ;; Non-literal code.
  2528. (t (setq res (c-forward-over-illiterals macro-fence
  2529. (> (point) here)))
  2530. ;; Are we about to move forward into or out of a
  2531. ;; preprocessor command?
  2532. (when (eq (cdr res) 'macro-boundary)
  2533. (setq macro-fence
  2534. (save-excursion
  2535. (if macro-fence
  2536. (progn
  2537. (end-of-line)
  2538. (and (not (eobp))
  2539. (progn (c-skip-ws-forward)
  2540. (c-beginning-of-macro))
  2541. (progn (c-end-of-macro)
  2542. (point))))
  2543. (and (not (eobp))
  2544. (c-beginning-of-macro)
  2545. (progn (c-end-of-macro) (point)))))))
  2546. ;; Are we about to move forward into a literal?
  2547. (when (memq (cdr res) '(macro-boundary literal))
  2548. (setq range (c-ascertain-following-literal)))
  2549. (car res))))
  2550. (if (/= count 0) (setq count (1- count))))
  2551. (c-keep-region-active))))
  2552. ;; set up electric character functions to work with pending-del,
  2553. ;; (a.k.a. delsel) mode. All symbols get the t value except
  2554. ;; the functions which delete, which gets 'supersede.
  2555. (mapc
  2556. (function
  2557. (lambda (sym)
  2558. (put sym 'delete-selection t) ; for delsel (Emacs)
  2559. (put sym 'pending-delete t))) ; for pending-del (XEmacs)
  2560. '(c-electric-pound
  2561. c-electric-brace
  2562. c-electric-slash
  2563. c-electric-star
  2564. c-electric-semi&comma
  2565. c-electric-lt-gt
  2566. c-electric-colon
  2567. c-electric-paren))
  2568. (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
  2569. (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
  2570. (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
  2571. (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
  2572. (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
  2573. (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
  2574. ;; Inserting/indenting comments
  2575. (defun c-calc-comment-indent (entry)
  2576. ;; This function might do hidden buffer changes.
  2577. (if (symbolp entry)
  2578. (setq entry (or (assq entry c-indent-comment-alist)
  2579. (assq 'other c-indent-comment-alist)
  2580. '(default . (column . nil)))))
  2581. (let ((action (car (cdr entry)))
  2582. (value (cdr (cdr entry)))
  2583. (col (current-column)))
  2584. (cond ((eq action 'space)
  2585. (+ col value))
  2586. ((eq action 'column)
  2587. (unless value (setq value comment-column))
  2588. (if (bolp)
  2589. ;; Do not pad with one space if we're at bol.
  2590. value
  2591. (max (1+ col) value)))
  2592. ((eq action 'align)
  2593. (or (save-excursion
  2594. (beginning-of-line)
  2595. (unless (bobp)
  2596. (backward-char)
  2597. (let ((lim (c-literal-limits (c-point 'bol) t)))
  2598. (when (consp lim)
  2599. (goto-char (car lim))
  2600. (when (looking-at "/[/*]") ; FIXME!!! Adapt for AWK! (ACM, 2005/11/18)
  2601. ;; Found comment to align with.
  2602. (if (bolp)
  2603. ;; Do not pad with one space if we're at bol.
  2604. 0
  2605. (max (1+ col) (current-column))))))))
  2606. ;; Recurse to handle value as a new spec.
  2607. (c-calc-comment-indent (cdr entry)))))))
  2608. (defun c-comment-indent ()
  2609. "Used by `indent-for-comment' to create and indent comments.
  2610. See `c-indent-comment-alist' for a description."
  2611. (save-excursion
  2612. (end-of-line)
  2613. (c-save-buffer-state
  2614. ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
  2615. (or (when (consp lim)
  2616. (goto-char (car lim))
  2617. (when (looking-at "/[/*]")
  2618. (skip-chars-backward " \t")
  2619. (point)))
  2620. (progn
  2621. (skip-chars-backward " \t")
  2622. (point)))))
  2623. (line-type
  2624. (cond ((looking-at "^/[/*]")
  2625. 'anchored-comment)
  2626. ((progn (beginning-of-line)
  2627. (eq (point) eot))
  2628. 'empty-line)
  2629. ((progn (back-to-indentation)
  2630. (and (eq (char-after) ?})
  2631. (eq (point) (1- eot))))
  2632. 'end-block)
  2633. ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
  2634. (eq (match-end 0) eot))
  2635. 'cpp-end-block)
  2636. (t
  2637. 'other)))
  2638. case-fold-search)
  2639. (if (and (memq line-type '(anchored-comment empty-line))
  2640. c-indent-comments-syntactically-p)
  2641. (let ((c-syntactic-context (c-guess-basic-syntax)))
  2642. ;; BOGOSITY ALERT: if we're looking at the eol, its
  2643. ;; because indent-for-comment hasn't put the comment-start
  2644. ;; in the buffer yet. this will screw up the syntactic
  2645. ;; analysis so we kludge in the necessary info. Another
  2646. ;; kludge is that if we're at the bol, then we really want
  2647. ;; to ignore any anchoring as specified by
  2648. ;; c-comment-only-line-offset since it doesn't apply here.
  2649. (if (eolp)
  2650. (c-add-syntax 'comment-intro))
  2651. (let ((c-comment-only-line-offset
  2652. (if (consp c-comment-only-line-offset)
  2653. c-comment-only-line-offset
  2654. (cons c-comment-only-line-offset
  2655. c-comment-only-line-offset))))
  2656. (c-get-syntactic-indentation c-syntactic-context)))
  2657. (goto-char eot)
  2658. (c-calc-comment-indent line-type)))))
  2659. ;; used by outline-minor-mode
  2660. (defun c-outline-level ()
  2661. (let (buffer-invisibility-spec);; This so that `current-column' DTRT
  2662. ;; in otherwise-hidden text.
  2663. (save-excursion
  2664. (skip-chars-forward "\t ")
  2665. (current-column))))
  2666. ;; Movement by CPP conditionals.
  2667. (defun c-up-conditional (count)
  2668. "Move back to the containing preprocessor conditional, leaving mark behind.
  2669. A prefix argument acts as a repeat count. With a negative argument,
  2670. move forward to the end of the containing preprocessor conditional.
  2671. \"#elif\" is treated like \"#else\" followed by \"#if\", so the
  2672. function stops at them when going backward, but not when going
  2673. forward."
  2674. (interactive "p")
  2675. (let ((new-point (c-scan-conditionals (- count) -1)))
  2676. (push-mark)
  2677. (goto-char new-point))
  2678. (c-keep-region-active))
  2679. (defun c-up-conditional-with-else (count)
  2680. "Move back to the containing preprocessor conditional, including \"#else\".
  2681. Just like `c-up-conditional', except it also stops at \"#else\"
  2682. directives."
  2683. (interactive "p")
  2684. (let ((new-point (c-scan-conditionals (- count) -1 t)))
  2685. (push-mark)
  2686. (goto-char new-point))
  2687. (c-keep-region-active))
  2688. (defun c-down-conditional (count)
  2689. "Move forward into the next preprocessor conditional, leaving mark behind.
  2690. A prefix argument acts as a repeat count. With a negative argument,
  2691. move backward into the previous preprocessor conditional.
  2692. \"#elif\" is treated like \"#else\" followed by \"#if\", so the
  2693. function stops at them when going forward, but not when going
  2694. backward."
  2695. (interactive "p")
  2696. (let ((new-point (c-scan-conditionals count 1)))
  2697. (push-mark)
  2698. (goto-char new-point))
  2699. (c-keep-region-active))
  2700. (defun c-down-conditional-with-else (count)
  2701. "Move forward into the next preprocessor conditional, including \"#else\".
  2702. Just like `c-down-conditional', except it also stops at \"#else\"
  2703. directives."
  2704. (interactive "p")
  2705. (let ((new-point (c-scan-conditionals count 1 t)))
  2706. (push-mark)
  2707. (goto-char new-point))
  2708. (c-keep-region-active))
  2709. (defun c-backward-conditional (count &optional target-depth with-else)
  2710. "Move back across a preprocessor conditional, leaving mark behind.
  2711. A prefix argument acts as a repeat count. With a negative argument,
  2712. move forward across a preprocessor conditional.
  2713. The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
  2714. and have the same meanings as in `c-scan-conditionals'. If you
  2715. are calling c-forward-conditional from a program, you might want
  2716. to call `c-scan-conditionals' directly instead."
  2717. (interactive "p")
  2718. (let ((new-point (c-scan-conditionals (- count) target-depth with-else)))
  2719. (push-mark)
  2720. (goto-char new-point))
  2721. (c-keep-region-active))
  2722. (defun c-forward-conditional (count &optional target-depth with-else)
  2723. "Move forward across a preprocessor conditional, leaving mark behind.
  2724. A prefix argument acts as a repeat count. With a negative argument,
  2725. move backward across a preprocessor conditional.
  2726. If there aren't enough conditionals after \(or before) point, an
  2727. error is signaled.
  2728. \"#elif\" is treated like \"#else\" followed by \"#if\", except that
  2729. the nesting level isn't changed when tracking subconditionals.
  2730. The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
  2731. and have the same meanings as in `c-scan-conditionals'. If you
  2732. are calling c-forward-conditional from a program, you might want
  2733. to call `c-scan-conditionals' directly instead."
  2734. (interactive "p")
  2735. (let ((new-point (c-scan-conditionals count target-depth with-else)))
  2736. (push-mark)
  2737. (goto-char new-point)))
  2738. (defun c-scan-conditionals (count &optional target-depth with-else)
  2739. "Scan forward across COUNT preprocessor conditionals.
  2740. With a negative argument, scan backward across preprocessor
  2741. conditionals. Return the end position. Point is not moved.
  2742. If there aren't enough preprocessor conditionals, throw an error.
  2743. \"#elif\" is treated like \"#else\" followed by \"#if\", except that
  2744. the nesting level isn't changed when tracking subconditionals.
  2745. The optional argument TARGET-DEPTH specifies the wanted nesting depth
  2746. after each scan. E.g. if TARGET-DEPTH is -1, the end position will be
  2747. outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH
  2748. counts as -1.
  2749. If the optional argument WITH-ELSE is non-nil, \"#else\" directives
  2750. are treated as conditional clause limits. Normally they are ignored."
  2751. (let* ((forward (> count 0))
  2752. (increment (if forward -1 1))
  2753. (search-function (if forward 're-search-forward 're-search-backward))
  2754. new case-fold-search)
  2755. (unless (integerp target-depth)
  2756. (setq target-depth (if target-depth -1 0)))
  2757. (save-excursion
  2758. (while (/= count 0)
  2759. (let ((depth 0)
  2760. ;; subdepth is the depth in "uninteresting" subtrees,
  2761. ;; i.e. those that takes us farther from the target
  2762. ;; depth instead of closer.
  2763. (subdepth 0)
  2764. found)
  2765. (save-excursion
  2766. ;; Find the "next" significant line in the proper direction.
  2767. (while (and (not found)
  2768. ;; Rather than searching for a # sign that
  2769. ;; comes at the beginning of a line aside from
  2770. ;; whitespace, search first for a string
  2771. ;; starting with # sign. Then verify what
  2772. ;; precedes it. This is faster on account of
  2773. ;; the fastmap feature of the regexp matcher.
  2774. (funcall search-function
  2775. "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
  2776. nil t))
  2777. (beginning-of-line)
  2778. ;; Now verify it is really a preproc line.
  2779. (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
  2780. (let (dchange (directive (match-string 1)))
  2781. (cond ((string= directive "if")
  2782. (setq dchange (- increment)))
  2783. ((string= directive "endif")
  2784. (setq dchange increment))
  2785. ((= subdepth 0)
  2786. ;; When we're not in an "uninteresting"
  2787. ;; subtree, we might want to act on "elif"
  2788. ;; and "else" too.
  2789. (if (cond (with-else
  2790. ;; Always move toward the target depth.
  2791. (setq dchange
  2792. (if (> target-depth 0) 1 -1)))
  2793. ((string= directive "elif")
  2794. (setq dchange (- increment))))
  2795. ;; Ignore the change if it'd take us
  2796. ;; into an "uninteresting" subtree.
  2797. (if (eq (> dchange 0) (<= target-depth 0))
  2798. (setq dchange nil)))))
  2799. (when dchange
  2800. (when (or (/= subdepth 0)
  2801. (eq (> dchange 0) (<= target-depth 0)))
  2802. (setq subdepth (+ subdepth dchange)))
  2803. (setq depth (+ depth dchange))
  2804. ;; If we are trying to move across, and we find an
  2805. ;; end before we find a beginning, get an error.
  2806. (if (and (< depth target-depth) (< dchange 0))
  2807. (error (if forward
  2808. "No following conditional at this level"
  2809. "No previous conditional at this level"))))
  2810. ;; When searching forward, start from next line so
  2811. ;; that we don't find the same line again.
  2812. (if forward (forward-line 1))
  2813. ;; We found something if we've arrived at the
  2814. ;; target depth.
  2815. (if (and dchange (= depth target-depth))
  2816. (setq found (point))))
  2817. ;; else
  2818. (if forward (forward-line 1)))))
  2819. (or found
  2820. (error "No containing preprocessor conditional"))
  2821. (goto-char (setq new found)))
  2822. (setq count (+ count increment))))
  2823. (c-keep-region-active)
  2824. new))
  2825. ;; commands to indent lines, regions, defuns, and expressions
  2826. (defun c-indent-command (&optional arg)
  2827. "Indent current line as C code, and/or insert some whitespace.
  2828. If `c-tab-always-indent' is t, always just indent the current line.
  2829. If nil, indent the current line only if point is at the left margin or
  2830. in the line's indentation; otherwise insert some whitespace[*]. If
  2831. other than nil or t, then some whitespace[*] is inserted only within
  2832. literals (comments and strings), but the line is always reindented.
  2833. If `c-syntactic-indentation' is t, indentation is done according to
  2834. the syntactic context. A numeric argument, regardless of its value,
  2835. means indent rigidly all the lines of the expression starting after
  2836. point so that this line becomes properly indented. The relative
  2837. indentation among the lines of the expression is preserved.
  2838. If `c-syntactic-indentation' is nil, the line is just indented one
  2839. step according to `c-basic-offset'. In this mode, a numeric argument
  2840. indents a number of such steps, positive or negative, and an empty
  2841. prefix argument is equivalent to -1.
  2842. [*] The amount and kind of whitespace inserted is controlled by the
  2843. variable `c-insert-tab-function', which is called to do the actual
  2844. insertion of whitespace. Normally the function in this variable
  2845. just inserts a tab character, or the equivalent number of spaces,
  2846. depending on the variable `indent-tabs-mode'."
  2847. (interactive "P")
  2848. (let ((indent-function
  2849. (if c-syntactic-indentation
  2850. (symbol-function 'indent-according-to-mode)
  2851. (lambda ()
  2852. (let ((c-macro-start c-macro-start)
  2853. (steps (if (equal arg '(4))
  2854. -1
  2855. (prefix-numeric-value arg))))
  2856. (c-shift-line-indentation (* steps c-basic-offset))
  2857. (when (and c-auto-align-backslashes
  2858. (save-excursion
  2859. (end-of-line)
  2860. (eq (char-before) ?\\))
  2861. (c-query-and-set-macro-start))
  2862. ;; Realign the line continuation backslash if inside a macro.
  2863. (c-backslash-region (point) (point) nil t)))
  2864. ))))
  2865. (if (and c-syntactic-indentation arg)
  2866. ;; If c-syntactic-indentation and got arg, always indent this
  2867. ;; line as C and shift remaining lines of expression the same
  2868. ;; amount.
  2869. (let ((shift-amt (save-excursion
  2870. (back-to-indentation)
  2871. (current-column)))
  2872. beg end)
  2873. (c-indent-line)
  2874. (setq shift-amt (- (save-excursion
  2875. (back-to-indentation)
  2876. (current-column))
  2877. shift-amt))
  2878. (save-excursion
  2879. (if (eq c-tab-always-indent t)
  2880. (beginning-of-line)) ; FIXME!!! What is this here for? ACM 2005/10/31
  2881. (setq beg (point))
  2882. (c-forward-sexp 1)
  2883. (setq end (point))
  2884. (goto-char beg)
  2885. (forward-line 1)
  2886. (setq beg (point)))
  2887. (if (> end beg)
  2888. (indent-code-rigidly beg end shift-amt "#")))
  2889. ;; Else use c-tab-always-indent to determine behavior.
  2890. (cond
  2891. ;; CASE 1: indent when at column zero or in line's indentation,
  2892. ;; otherwise insert a tab
  2893. ((not c-tab-always-indent)
  2894. (if (save-excursion
  2895. (skip-chars-backward " \t")
  2896. (not (bolp)))
  2897. (funcall c-insert-tab-function)
  2898. (funcall indent-function)))
  2899. ;; CASE 2: just indent the line
  2900. ((eq c-tab-always-indent t)
  2901. (funcall indent-function))
  2902. ;; CASE 3: if in a literal, insert a tab, but always indent the
  2903. ;; line
  2904. (t
  2905. (if (c-save-buffer-state () (c-in-literal))
  2906. (funcall c-insert-tab-function))
  2907. (funcall indent-function)
  2908. )))))
  2909. (defun c-indent-exp (&optional shutup-p)
  2910. "Indent each line in the balanced expression following point syntactically.
  2911. If optional SHUTUP-P is non-nil, no errors are signaled if no
  2912. balanced expression is found."
  2913. (interactive "*P")
  2914. (let ((here (point-marker))
  2915. end)
  2916. (set-marker-insertion-type here t)
  2917. (unwind-protect
  2918. (let ((start (save-restriction
  2919. ;; Find the closest following open paren that
  2920. ;; ends on another line.
  2921. (narrow-to-region (point-min) (c-point 'eol))
  2922. (let (beg (end (point)))
  2923. (while (and (setq beg (c-down-list-forward end))
  2924. (setq end (c-up-list-forward beg))))
  2925. (and beg
  2926. (eq (char-syntax (char-before beg)) ?\()
  2927. (1- beg))))))
  2928. ;; sanity check
  2929. (if (not start)
  2930. (unless shutup-p
  2931. (error "Cannot find start of balanced expression to indent"))
  2932. (goto-char start)
  2933. (setq end (c-safe (scan-sexps (point) 1)))
  2934. (if (not end)
  2935. (unless shutup-p
  2936. (error "Cannot find end of balanced expression to indent"))
  2937. (forward-line)
  2938. (if (< (point) end)
  2939. (c-indent-region (point) end)))))
  2940. (goto-char here)
  2941. (set-marker here nil))))
  2942. (defun c-indent-defun ()
  2943. "Indent the current top-level declaration or macro syntactically.
  2944. In the macro case this also has the effect of realigning any line
  2945. continuation backslashes, unless `c-auto-align-backslashes' is nil."
  2946. (interactive "*")
  2947. (let ((here (point-marker)) decl-limits case-fold-search)
  2948. (unwind-protect
  2949. (progn
  2950. (c-save-buffer-state nil
  2951. ;; We try to be line oriented, unless there are several
  2952. ;; declarations on the same line.
  2953. (if (looking-at c-syntactic-eol)
  2954. (c-backward-token-2 1 nil (c-point 'bol))
  2955. (c-forward-token-2 0 nil (c-point 'eol)))
  2956. (setq decl-limits (c-declaration-limits nil)))
  2957. (if decl-limits
  2958. (c-indent-region (car decl-limits)
  2959. (cdr decl-limits))))
  2960. (goto-char here)
  2961. (set-marker here nil))))
  2962. (defun c-indent-region (start end &optional quiet)
  2963. "Indent syntactically every line whose first char is between START
  2964. and END inclusive. If the optional argument QUIET is non-nil then no
  2965. syntactic errors are reported, even if `c-report-syntactic-errors' is
  2966. non-nil."
  2967. (save-excursion
  2968. (goto-char end)
  2969. (skip-chars-backward " \t\n\r\f\v")
  2970. (setq end (point))
  2971. (goto-char start)
  2972. ;; Advance to first nonblank line.
  2973. (beginning-of-line)
  2974. (skip-chars-forward " \t\n\r\f\v")
  2975. (setq start (point))
  2976. (beginning-of-line)
  2977. (setq c-parsing-error
  2978. (or (let ((endmark (copy-marker end))
  2979. (c-parsing-error nil)
  2980. ;; shut up any echo msgs on indiv lines
  2981. (c-echo-syntactic-information-p nil)
  2982. (ml-macro-start ; Start pos of multi-line macro.
  2983. (and (c-save-buffer-state ()
  2984. (save-excursion (c-beginning-of-macro)))
  2985. (eq (char-before (c-point 'eol)) ?\\)
  2986. start))
  2987. (c-fix-backslashes nil)
  2988. syntax)
  2989. (unwind-protect
  2990. (progn
  2991. (c-progress-init start end 'c-indent-region)
  2992. (while (and (bolp) ;; One line each time round the loop.
  2993. (not (eobp))
  2994. (< (point) endmark))
  2995. ;; update progress
  2996. (c-progress-update)
  2997. ;; skip empty lines
  2998. (unless (or (looking-at "\\s *$")
  2999. (and ml-macro-start (looking-at "\\s *\\\\$")))
  3000. ;; Get syntax and indent.
  3001. (c-save-buffer-state nil
  3002. (setq syntax (c-guess-basic-syntax)))
  3003. (c-indent-line syntax t t))
  3004. (if ml-macro-start
  3005. ;; End of current multi-line macro?
  3006. (when (and c-auto-align-backslashes
  3007. (not (eq (char-before (c-point 'eol)) ?\\)))
  3008. ;; Fixup macro backslashes.
  3009. (c-backslash-region ml-macro-start (c-point 'bonl) nil)
  3010. (setq ml-macro-start nil))
  3011. ;; New multi-line macro?
  3012. (if (and (assq 'cpp-macro syntax)
  3013. (eq (char-before (c-point 'eol)) ?\\))
  3014. (setq ml-macro-start (point))))
  3015. (forward-line))
  3016. (if (and ml-macro-start c-auto-align-backslashes)
  3017. (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
  3018. (set-marker endmark nil)
  3019. (c-progress-fini 'c-indent-region))
  3020. (c-echo-parsing-error quiet))
  3021. c-parsing-error))))
  3022. (defun c-fn-region-is-active-p ()
  3023. ;; Function version of the macro for use in places that aren't
  3024. ;; compiled, e.g. in the menus.
  3025. (c-region-is-active-p))
  3026. (defun c-indent-line-or-region (&optional arg region)
  3027. "Indent active region, current line, or block starting on this line.
  3028. In Transient Mark mode, when the region is active, reindent the region.
  3029. Otherwise, with a prefix argument, rigidly reindent the expression
  3030. starting on the current line.
  3031. Otherwise reindent just the current line."
  3032. (interactive
  3033. (list current-prefix-arg (use-region-p)))
  3034. (if region
  3035. (c-indent-region (region-beginning) (region-end))
  3036. (c-indent-command arg)))
  3037. ;; for progress reporting
  3038. (defvar c-progress-info nil)
  3039. (defun c-progress-init (start end context)
  3040. (cond
  3041. ;; Be silent
  3042. ((not c-progress-interval))
  3043. ;; Start the progress update messages. If this Emacs doesn't have
  3044. ;; a built-in timer, just be dumb about it.
  3045. ((not (fboundp 'current-time))
  3046. (message "Indenting region... (this may take a while)"))
  3047. ;; If progress has already been initialized, do nothing. otherwise
  3048. ;; initialize the counter with a vector of:
  3049. ;; [start end lastsec context]
  3050. (c-progress-info)
  3051. (t (setq c-progress-info (vector start
  3052. (save-excursion
  3053. (goto-char end)
  3054. (point-marker))
  3055. (nth 1 (current-time))
  3056. context))
  3057. (message "Indenting region..."))
  3058. ))
  3059. (defun c-progress-update ()
  3060. (if (not (and c-progress-info c-progress-interval))
  3061. nil
  3062. (let ((now (nth 1 (current-time)))
  3063. (start (aref c-progress-info 0))
  3064. (end (aref c-progress-info 1))
  3065. (lastsecs (aref c-progress-info 2)))
  3066. ;; should we update? currently, update happens every 2 seconds,
  3067. ;; what's the right value?
  3068. (if (< c-progress-interval (- now lastsecs))
  3069. (progn
  3070. (message "Indenting region... (%d%% complete)"
  3071. (/ (* 100 (- (point) start)) (- end start)))
  3072. (aset c-progress-info 2 now)))
  3073. )))
  3074. (defun c-progress-fini (context)
  3075. (if (not c-progress-interval)
  3076. nil
  3077. (if (or (eq context (aref c-progress-info 3))
  3078. (eq context t))
  3079. (progn
  3080. (set-marker (aref c-progress-info 1) nil)
  3081. (setq c-progress-info nil)
  3082. (message "Indenting region... done")))))
  3083. ;;; This page handles insertion and removal of backslashes for C macros.
  3084. (defun c-backslash-region (from to delete-flag &optional line-mode)
  3085. "Insert, align, or delete end-of-line backslashes on the lines in the region.
  3086. With no argument, inserts backslashes and aligns existing backslashes.
  3087. With an argument, deletes the backslashes. The backslash alignment is
  3088. done according to the settings in `c-backslash-column',
  3089. `c-backslash-max-column' and `c-auto-align-backslashes'.
  3090. This function does not modify blank lines at the start of the region.
  3091. If the region ends at the start of a line and the macro doesn't
  3092. continue below it, the backslash (if any) at the end of the previous
  3093. line is deleted.
  3094. You can put the region around an entire macro definition and use this
  3095. command to conveniently insert and align the necessary backslashes."
  3096. (interactive "*r\nP")
  3097. (let ((endmark (make-marker))
  3098. ;; Keep the backslash trimming functions from changing the
  3099. ;; whitespace around point, since in this case it's only the
  3100. ;; position of point that tells the indentation of the line.
  3101. (point-pos (if (save-excursion
  3102. (skip-chars-backward " \t")
  3103. (and (bolp) (looking-at "[ \t]*\\\\?$")))
  3104. (point-marker)
  3105. (point-min)))
  3106. column longest-line-col bs-col-after-end)
  3107. (save-excursion
  3108. (goto-char to)
  3109. (if (and (not line-mode) (bobp))
  3110. ;; Nothing to do if to is at bob, since we should back up
  3111. ;; and there's no line to back up to.
  3112. nil
  3113. (when (and (not line-mode) (bolp))
  3114. ;; Do not back up the to line if line-mode is set, to make
  3115. ;; e.g. c-newline-and-indent consistent regardless whether
  3116. ;; the (newline) call leaves point at bol or not.
  3117. (backward-char)
  3118. (setq to (point)))
  3119. (if delete-flag
  3120. (progn
  3121. (set-marker endmark (point))
  3122. (goto-char from)
  3123. (c-delete-backslashes-forward endmark point-pos))
  3124. ;; Set bs-col-after-end to the column of any backslash
  3125. ;; following the region, or nil if there is none.
  3126. (setq bs-col-after-end
  3127. (and (progn (end-of-line)
  3128. (eq (char-before) ?\\))
  3129. (= (forward-line 1) 0)
  3130. (progn (end-of-line)
  3131. (eq (char-before) ?\\))
  3132. (1- (current-column))))
  3133. (when line-mode
  3134. ;; Back up the to line if line-mode is set, since the line
  3135. ;; after the newly inserted line break should not be
  3136. ;; touched in c-newline-and-indent.
  3137. (setq to (max from (or (c-safe (c-point 'eopl)) from)))
  3138. (unless bs-col-after-end
  3139. ;; Set bs-col-after-end to non-nil in any case, since we
  3140. ;; do not want to delete the backslash at the last line.
  3141. (setq bs-col-after-end t)))
  3142. (if (and line-mode
  3143. (not c-auto-align-backslashes))
  3144. (goto-char from)
  3145. ;; Compute the smallest column number past the ends of all
  3146. ;; the lines.
  3147. (setq longest-line-col 0)
  3148. (goto-char to)
  3149. (if bs-col-after-end
  3150. ;; Include one more line in the max column
  3151. ;; calculation, since the to line will be backslashed
  3152. ;; too.
  3153. (forward-line 1))
  3154. (end-of-line)
  3155. (while (and (>= (point) from)
  3156. (progn
  3157. (if (eq (char-before) ?\\)
  3158. (forward-char -1))
  3159. (skip-chars-backward " \t")
  3160. (setq longest-line-col (max longest-line-col
  3161. (1+ (current-column))))
  3162. (beginning-of-line)
  3163. (not (bobp))))
  3164. (backward-char))
  3165. ;; Try to align with surrounding backslashes.
  3166. (goto-char from)
  3167. (beginning-of-line)
  3168. (if (and (not (bobp))
  3169. (progn (backward-char)
  3170. (eq (char-before) ?\\)))
  3171. (progn
  3172. (setq column (1- (current-column)))
  3173. (if (numberp bs-col-after-end)
  3174. ;; Both a preceding and a following backslash.
  3175. ;; Choose the greatest of them.
  3176. (setq column (max column bs-col-after-end)))
  3177. (goto-char from))
  3178. ;; No preceding backslash. Try to align with one
  3179. ;; following the region. Disregard the backslash at the
  3180. ;; to line since it's likely to be bogus (e.g. when
  3181. ;; called from c-newline-and-indent).
  3182. (if (numberp bs-col-after-end)
  3183. (setq column bs-col-after-end))
  3184. ;; Don't modify blank lines at start of region.
  3185. (goto-char from)
  3186. (while (and (< (point) to) (bolp) (eolp))
  3187. (forward-line 1)))
  3188. (if (and column (< column longest-line-col))
  3189. ;; Don't try to align with surrounding backslashes if
  3190. ;; any line is too long.
  3191. (setq column nil))
  3192. (unless column
  3193. ;; Impose minimum limit and tab width alignment only if
  3194. ;; we can't align with surrounding backslashes.
  3195. (if (> (% longest-line-col tab-width) 0)
  3196. (setq longest-line-col
  3197. (* (/ (+ longest-line-col tab-width -1)
  3198. tab-width)
  3199. tab-width)))
  3200. (setq column (max c-backslash-column
  3201. longest-line-col)))
  3202. ;; Always impose maximum limit.
  3203. (setq column (min column c-backslash-max-column)))
  3204. (if bs-col-after-end
  3205. ;; Add backslashes on all lines if the macro continues
  3206. ;; after the to line.
  3207. (progn
  3208. (set-marker endmark to)
  3209. (c-append-backslashes-forward endmark column point-pos))
  3210. ;; Add backslashes on all lines except the last, and
  3211. ;; remove any on the last line.
  3212. (if (save-excursion
  3213. (goto-char to)
  3214. (beginning-of-line)
  3215. (if (not (bobp))
  3216. (set-marker endmark (1- (point)))))
  3217. (progn
  3218. (c-append-backslashes-forward endmark column point-pos)
  3219. ;; The function above leaves point on the line
  3220. ;; following endmark.
  3221. (set-marker endmark (point)))
  3222. (set-marker endmark to))
  3223. (c-delete-backslashes-forward endmark point-pos)))))
  3224. (set-marker endmark nil)
  3225. (if (markerp point-pos)
  3226. (set-marker point-pos nil))))
  3227. (defun c-append-backslashes-forward (to-mark column point-pos)
  3228. (let ((state (parse-partial-sexp (c-point 'bol) (point))))
  3229. (if column
  3230. (while
  3231. (and
  3232. (<= (point) to-mark)
  3233. (let ((start (point)) (inserted nil) end col)
  3234. (end-of-line)
  3235. (unless (eq (char-before) ?\\)
  3236. (insert ?\\)
  3237. (setq inserted t))
  3238. (setq state (parse-partial-sexp
  3239. start (point) nil nil state))
  3240. (backward-char)
  3241. (setq col (current-column))
  3242. ;; Avoid unnecessary changes of the buffer.
  3243. (cond ((and (not inserted) (nth 3 state))
  3244. ;; Don't realign backslashes in string literals
  3245. ;; since that would change them.
  3246. )
  3247. ((< col column)
  3248. (delete-region
  3249. (point)
  3250. (progn
  3251. (skip-chars-backward
  3252. " \t" (if (>= (point) point-pos) point-pos))
  3253. (point)))
  3254. (indent-to column))
  3255. ((and (= col column)
  3256. (memq (char-before) '(?\ ?\t))))
  3257. ((progn
  3258. (setq end (point))
  3259. (or (/= (skip-chars-backward
  3260. " \t" (if (>= (point) point-pos) point-pos))
  3261. -1)
  3262. (/= (char-after) ?\ )))
  3263. (delete-region (point) end)
  3264. (indent-to column 1)))
  3265. (zerop (forward-line 1)))
  3266. (bolp))) ; forward-line has funny behavior at eob.
  3267. ;; Make sure there are backslashes with at least one space in
  3268. ;; front of them.
  3269. (while
  3270. (and
  3271. (<= (point) to-mark)
  3272. (let ((start (point)))
  3273. (end-of-line)
  3274. (setq state (parse-partial-sexp
  3275. start (point) nil nil state))
  3276. (if (eq (char-before) ?\\)
  3277. (unless (nth 3 state)
  3278. (backward-char)
  3279. (unless (and (memq (char-before) '(?\ ?\t))
  3280. (/= (point) point-pos))
  3281. (insert ?\ )))
  3282. (if (and (memq (char-before) '(?\ ?\t))
  3283. (/= (point) point-pos))
  3284. (insert ?\\)
  3285. (insert ?\ ?\\)))
  3286. (zerop (forward-line 1)))
  3287. (bolp)))))) ; forward-line has funny behavior at eob.
  3288. (defun c-delete-backslashes-forward (to-mark point-pos)
  3289. (while
  3290. (and (<= (point) to-mark)
  3291. (progn
  3292. (end-of-line)
  3293. (if (eq (char-before) ?\\)
  3294. (delete-region
  3295. (point)
  3296. (progn (backward-char)
  3297. (skip-chars-backward " \t" (if (>= (point) point-pos)
  3298. point-pos))
  3299. (point))))
  3300. (zerop (forward-line 1)))
  3301. (bolp)))) ; forward-line has funny behavior at eob.
  3302. ;;; Line breaking and paragraph filling.
  3303. (defvar c-auto-fill-prefix t)
  3304. (defvar c-lit-limits nil)
  3305. (defvar c-lit-type nil)
  3306. ;; The filling code is based on a simple theory; leave the intricacies
  3307. ;; of the text handling to the currently active mode for that
  3308. ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
  3309. ;; possible to make them work correctly wrt the comment and string
  3310. ;; separators, one-line paragraphs etc. Unfortunately, when it comes
  3311. ;; to it, there's quite a lot of special cases to handle which makes
  3312. ;; the code anything but simple. The intention is that it will work
  3313. ;; with any well-written text filling package that preserves a fill
  3314. ;; prefix.
  3315. ;;
  3316. ;; We temporarily mask comment starters and enders as necessary for
  3317. ;; the filling code to do its job on a seemingly normal text block.
  3318. ;; We do _not_ mask the fill prefix, so it's up to the filling code to
  3319. ;; preserve it correctly (especially important when filling C++ style
  3320. ;; line comments). By default, we set up and use adaptive-fill-mode,
  3321. ;; which is standard in all supported Emacs flavors.
  3322. (defun c-guess-fill-prefix (lit-limits lit-type)
  3323. ;; Determine the appropriate comment fill prefix for a block or line
  3324. ;; comment. Return a cons of the prefix string and the column where
  3325. ;; it ends. If fill-prefix is set, it'll override. Note that this
  3326. ;; function also uses the value of point in some heuristics.
  3327. ;;
  3328. ;; This function might do hidden buffer changes.
  3329. (let* ((here (point))
  3330. (prefix-regexp (concat "[ \t]*\\("
  3331. c-current-comment-prefix
  3332. "\\)[ \t]*"))
  3333. (comment-start-regexp (if (eq lit-type 'c++)
  3334. prefix-regexp
  3335. comment-start-skip))
  3336. prefix-line comment-prefix res comment-text-end)
  3337. (cond
  3338. (fill-prefix
  3339. (setq res (cons fill-prefix
  3340. ;; Ugly way of getting the column after the fill
  3341. ;; prefix; it'd be nice with a current-column
  3342. ;; that works on strings..
  3343. (let ((start (point)))
  3344. (unwind-protect
  3345. (progn
  3346. (insert-and-inherit "\n" fill-prefix)
  3347. (current-column))
  3348. (delete-region start (point)))))))
  3349. ((eq lit-type 'c++)
  3350. (save-excursion
  3351. ;; Set fallback for comment-prefix if none is found.
  3352. (setq comment-prefix "// "
  3353. comment-text-end (cdr lit-limits))
  3354. (beginning-of-line)
  3355. (if (> (point) (car lit-limits))
  3356. ;; The current line is not the comment starter, so the
  3357. ;; comment has more than one line, and it can therefore be
  3358. ;; used to find the comment fill prefix.
  3359. (setq prefix-line (point))
  3360. (goto-char (car lit-limits))
  3361. (if (and (= (forward-line 1) 0)
  3362. (< (point) (cdr lit-limits)))
  3363. ;; The line after the comment starter is inside the
  3364. ;; comment, so we can use it.
  3365. (setq prefix-line (point))
  3366. ;; The comment is only one line. Take the comment prefix
  3367. ;; from it and keep the indentation.
  3368. (goto-char (car lit-limits))
  3369. (if (looking-at prefix-regexp)
  3370. (goto-char (match-end 0))
  3371. (forward-char 2)
  3372. (skip-chars-forward " \t"))
  3373. (let (str col)
  3374. (if (eq (c-point 'boi) (car lit-limits))
  3375. ;; There is only whitespace before the comment
  3376. ;; starter; take the prefix straight from this line.
  3377. (setq str (buffer-substring-no-properties
  3378. (c-point 'bol) (point))
  3379. col (current-column))
  3380. ;; There is code before the comment starter, so we
  3381. ;; have to temporarily insert and indent a new line to
  3382. ;; get the right space/tab mix in the indentation.
  3383. (let ((prefix-len (- (point) (car lit-limits)))
  3384. tmp)
  3385. (unwind-protect
  3386. (progn
  3387. (goto-char (car lit-limits))
  3388. (indent-to (prog1 (current-column)
  3389. (insert ?\n)))
  3390. (setq tmp (point))
  3391. (forward-char prefix-len)
  3392. (setq str (buffer-substring-no-properties
  3393. (c-point 'bol) (point))
  3394. col (current-column)))
  3395. (delete-region (car lit-limits) tmp))))
  3396. (setq res
  3397. (if (or (string-match "\\s \\'" str) (not (eolp)))
  3398. (cons str col)
  3399. ;; The prefix ends the line with no whitespace
  3400. ;; after it. Default to a single space.
  3401. (cons (concat str " ") (1+ col))))
  3402. )))))
  3403. (t
  3404. (setq comment-text-end
  3405. (save-excursion
  3406. (goto-char (- (cdr lit-limits) 2))
  3407. (if (looking-at "\\*/") (point) (cdr lit-limits))))
  3408. (save-excursion
  3409. (beginning-of-line)
  3410. (if (and (> (point) (car lit-limits))
  3411. (not (and (looking-at "[ \t]*\\*/")
  3412. (eq (cdr lit-limits) (match-end 0)))))
  3413. ;; The current line is not the comment starter and
  3414. ;; contains more than just the ender, so it's good enough
  3415. ;; to be used for the comment fill prefix.
  3416. (setq prefix-line (point))
  3417. (goto-char (car lit-limits))
  3418. (cond ((or (/= (forward-line 1) 0)
  3419. (>= (point) (cdr lit-limits))
  3420. (and (looking-at "[ \t]*\\*/")
  3421. (eq (cdr lit-limits) (match-end 0)))
  3422. (and (looking-at prefix-regexp)
  3423. (<= (1- (cdr lit-limits)) (match-end 0))))
  3424. ;; The comment is either one line or the next line contains
  3425. ;; just the comment ender. In this case we have no
  3426. ;; information about a suitable comment prefix, so we resort
  3427. ;; to c-block-comment-prefix.
  3428. (setq comment-prefix (or c-block-comment-prefix "")))
  3429. ((< here (point))
  3430. ;; The point was on the comment opener line, so we might want
  3431. ;; to treat this as a not yet closed comment.
  3432. (if (and (match-beginning 1)
  3433. (/= (match-beginning 1) (match-end 1)))
  3434. ;; Above `prefix-regexp' matched a nonempty prefix on the
  3435. ;; second line, so let's use it. Normally it should do
  3436. ;; to set `prefix-line' and let the code below pick up
  3437. ;; the whole prefix, but if there's no text after the
  3438. ;; match then it will probably fall back to no prefix at
  3439. ;; all if the comment isn't closed yet, so in that case
  3440. ;; it's better to force use of the prefix matched now.
  3441. (if (= (match-end 0) (c-point 'eol))
  3442. (setq comment-prefix (match-string 1))
  3443. (setq prefix-line (point)))
  3444. ;; There's no nonempty prefix on the line after the
  3445. ;; comment opener. If the line is empty, or if the
  3446. ;; text on it has less or equal indentation than the
  3447. ;; comment starter we assume it's an unclosed
  3448. ;; comment starter, i.e. that
  3449. ;; `c-block-comment-prefix' should be used.
  3450. ;; Otherwise we assume it's a closed comment where
  3451. ;; the prefix really is the empty string.
  3452. ;; E.g. this is an unclosed comment:
  3453. ;;
  3454. ;; /*
  3455. ;; foo
  3456. ;;
  3457. ;; But this is not:
  3458. ;;
  3459. ;; /*
  3460. ;; foo
  3461. ;; */
  3462. ;;
  3463. ;; (Looking for the presence of the comment closer
  3464. ;; rarely works since it's probably the closer of
  3465. ;; some comment further down when the comment
  3466. ;; really is unclosed.)
  3467. (if (<= (save-excursion (back-to-indentation)
  3468. (current-column))
  3469. (save-excursion (goto-char (car lit-limits))
  3470. (current-column)))
  3471. (setq comment-prefix (or c-block-comment-prefix ""))
  3472. (setq prefix-line (point)))))
  3473. (t
  3474. ;; Otherwise the line after the comment starter is good
  3475. ;; enough to find the prefix in.
  3476. (setq prefix-line (point))))
  3477. (when comment-prefix
  3478. ;; Haven't got the comment prefix on any real line that we
  3479. ;; can take it from, so we have to temporarily insert
  3480. ;; `comment-prefix' on a line and indent it to find the
  3481. ;; correct column and the correct mix of tabs and spaces.
  3482. (setq res
  3483. (let (tmp-pre tmp-post)
  3484. (unwind-protect
  3485. (progn
  3486. (goto-char (car lit-limits))
  3487. (if (looking-at comment-start-regexp)
  3488. (goto-char (min (match-end 0)
  3489. comment-text-end))
  3490. (forward-char 2)
  3491. (skip-chars-forward " \t"))
  3492. (when (eq (char-syntax (char-before)) ?\ )
  3493. ;; If there's ws on the current line, we'll use it
  3494. ;; instead of what's ending comment-prefix.
  3495. (setq comment-prefix
  3496. (concat (substring comment-prefix
  3497. 0 (string-match
  3498. "\\s *\\'"
  3499. comment-prefix))
  3500. (buffer-substring-no-properties
  3501. (save-excursion
  3502. (skip-chars-backward " \t")
  3503. (point))
  3504. (point)))))
  3505. (setq tmp-pre (point-marker))
  3506. ;; We insert an extra non-whitespace character
  3507. ;; before the line break and after comment-prefix in
  3508. ;; case it's "" or ends with whitespace.
  3509. (insert-and-inherit "x\n" comment-prefix "x")
  3510. (setq tmp-post (point-marker))
  3511. (indent-according-to-mode)
  3512. (goto-char (1- tmp-post))
  3513. (cons (buffer-substring-no-properties
  3514. (c-point 'bol) (point))
  3515. (current-column)))
  3516. (when tmp-post
  3517. (delete-region tmp-pre tmp-post)
  3518. (set-marker tmp-pre nil)
  3519. (set-marker tmp-post nil))))))))))
  3520. (or res ; Found a good prefix above.
  3521. (save-excursion
  3522. ;; prefix-line is the bol of a line on which we should try
  3523. ;; to find the prefix.
  3524. (let* (fb-string fb-endpos ; Contains any fallback prefix found.
  3525. (test-line
  3526. (lambda ()
  3527. (when (and (looking-at prefix-regexp)
  3528. (<= (match-end 0) comment-text-end))
  3529. (unless (eq (match-end 0) (c-point 'eol))
  3530. ;; The match is fine if there's text after it.
  3531. (throw 'found (cons (buffer-substring-no-properties
  3532. (match-beginning 0) (match-end 0))
  3533. (progn (goto-char (match-end 0))
  3534. (current-column)))))
  3535. (unless fb-string
  3536. ;; This match is better than nothing, so let's
  3537. ;; remember it in case nothing better is found
  3538. ;; on another line.
  3539. (setq fb-string (buffer-substring-no-properties
  3540. (match-beginning 0) (match-end 0))
  3541. fb-endpos (match-end 0)))
  3542. t))))
  3543. (or (catch 'found
  3544. ;; Search for a line which has text after the prefix
  3545. ;; so that we get the proper amount of whitespace
  3546. ;; after it. We start with the current line, then
  3547. ;; search backwards, then forwards.
  3548. (goto-char prefix-line)
  3549. (when (and (funcall test-line)
  3550. (or (/= (match-end 1) (match-end 0))
  3551. ;; The whitespace is sucked up by the
  3552. ;; first [ \t]* glob if the prefix is empty.
  3553. (and (= (match-beginning 1) (match-end 1))
  3554. (/= (match-beginning 0) (match-end 0)))))
  3555. ;; If the current line doesn't have text but do
  3556. ;; have whitespace after the prefix, we'll use it.
  3557. (throw 'found (cons fb-string
  3558. (progn (goto-char fb-endpos)
  3559. (current-column)))))
  3560. (if (eq lit-type 'c++)
  3561. ;; For line comments we can search up to and
  3562. ;; including the first line.
  3563. (while (and (zerop (forward-line -1))
  3564. (>= (point) (car lit-limits)))
  3565. (funcall test-line))
  3566. ;; For block comments we must stop before the
  3567. ;; block starter.
  3568. (while (and (zerop (forward-line -1))
  3569. (> (point) (car lit-limits)))
  3570. (funcall test-line)))
  3571. (goto-char prefix-line)
  3572. (while (and (zerop (forward-line 1))
  3573. (< (point) (cdr lit-limits)))
  3574. (funcall test-line))
  3575. (goto-char prefix-line)
  3576. nil)
  3577. (when fb-string
  3578. ;; A good line wasn't found, but at least we have a
  3579. ;; fallback that matches the comment prefix regexp.
  3580. (cond ((or (string-match "\\s \\'" fb-string)
  3581. (progn
  3582. (goto-char fb-endpos)
  3583. (not (eolp))))
  3584. ;; There are ws or text after the prefix, so
  3585. ;; let's use it.
  3586. (cons fb-string (current-column)))
  3587. ((progn
  3588. ;; Check if there's any whitespace padding
  3589. ;; on the comment start line that we can
  3590. ;; use after the prefix.
  3591. (goto-char (car lit-limits))
  3592. (if (looking-at comment-start-regexp)
  3593. (goto-char (match-end 0))
  3594. (forward-char 2)
  3595. (skip-chars-forward " \t"))
  3596. (or (not (eolp))
  3597. (eq (char-syntax (char-before)) ?\ )))
  3598. (setq fb-string (buffer-substring-no-properties
  3599. (save-excursion
  3600. (skip-chars-backward " \t")
  3601. (point))
  3602. (point)))
  3603. (goto-char fb-endpos)
  3604. (skip-chars-backward " \t")
  3605. (let ((tmp (point)))
  3606. ;; Got to mess in the buffer once again to
  3607. ;; ensure the column gets correct. :P
  3608. (unwind-protect
  3609. (progn
  3610. (insert-and-inherit fb-string)
  3611. (cons (buffer-substring-no-properties
  3612. (c-point 'bol)
  3613. (point))
  3614. (current-column)))
  3615. (delete-region tmp (point)))))
  3616. (t
  3617. ;; Last resort: Just add a single space after
  3618. ;; the prefix.
  3619. (cons (concat fb-string " ")
  3620. (progn (goto-char fb-endpos)
  3621. (1+ (current-column)))))))
  3622. ;; The line doesn't match the comment prefix regexp.
  3623. (if comment-prefix
  3624. ;; We have a fallback for line comments that we must use.
  3625. (cons (concat (buffer-substring-no-properties
  3626. prefix-line (c-point 'boi))
  3627. comment-prefix)
  3628. (progn (back-to-indentation)
  3629. (+ (current-column) (length comment-prefix))))
  3630. ;; Assume we are dealing with a "free text" block
  3631. ;; comment where the lines doesn't have any comment
  3632. ;; prefix at all and we should just fill it as
  3633. ;; normal text.
  3634. '("" . 0))))))
  3635. ))
  3636. (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
  3637. ;; Calls FUN with ARGS ar arguments while the current paragraph is
  3638. ;; masked to allow adaptive filling to work correctly. That
  3639. ;; includes narrowing the buffer and, if point is inside a comment,
  3640. ;; masking the comment starter and ender appropriately.
  3641. ;;
  3642. ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
  3643. ;; The position of point is then less significant when doing masking
  3644. ;; and narrowing.
  3645. ;;
  3646. ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
  3647. ;; only if the point turns out to be inside a comment or a string.
  3648. ;;
  3649. ;; Note that this function does not do any hidden buffer changes.
  3650. (let (fill
  3651. ;; beg and end limit the region to narrow. end is a marker.
  3652. beg end
  3653. ;; tmp-pre and tmp-post mark strings that are temporarily
  3654. ;; inserted at the start and end of the region. tmp-pre is a
  3655. ;; cons of the positions of the prepended string. tmp-post is
  3656. ;; a marker pointing to the single character of the appended
  3657. ;; string.
  3658. tmp-pre tmp-post
  3659. ;; If hang-ender-stuck isn't nil, the comment ender is
  3660. ;; hanging. In that case it's set to the number of spaces
  3661. ;; that should be between the text and the ender.
  3662. hang-ender-stuck
  3663. ;; auto-fill-spaces is the exact sequence of whitespace between a
  3664. ;; comment's last word and the comment ender, temporarily replaced
  3665. ;; with 'x's before calling FUN when FILL-PARAGRAPH is nil.
  3666. auto-fill-spaces
  3667. (here (point))
  3668. (c-lit-limits c-lit-limits)
  3669. (c-lit-type c-lit-type))
  3670. ;; Restore point on undo. It's necessary since we do a lot of
  3671. ;; hidden inserts and deletes below that should be as transparent
  3672. ;; as possible.
  3673. (if (and buffer-undo-list (not (eq buffer-undo-list t)))
  3674. (setq buffer-undo-list (cons (point) buffer-undo-list)))
  3675. ;; Determine the limits and type of the containing literal (if any):
  3676. ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph:
  3677. ;; BEG and END.
  3678. (c-save-buffer-state ()
  3679. (save-restriction
  3680. ;; Widen to catch comment limits correctly.
  3681. (widen)
  3682. (unless c-lit-limits
  3683. (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
  3684. (setq c-lit-limits (c-collect-line-comments c-lit-limits))
  3685. (unless c-lit-type
  3686. (setq c-lit-type (c-literal-type c-lit-limits))))
  3687. (save-excursion
  3688. (unless (c-safe (backward-char)
  3689. (forward-paragraph)
  3690. (>= (point) here))
  3691. (goto-char here)
  3692. (forward-paragraph))
  3693. (setq end (point-marker)))
  3694. (save-excursion
  3695. (unless (c-safe (forward-char)
  3696. (backward-paragraph)
  3697. (<= (point) here))
  3698. (goto-char here)
  3699. (backward-paragraph))
  3700. (setq beg (point))))
  3701. (unwind-protect
  3702. (progn
  3703. ;; For each of the possible types of text (string, C comment ...)
  3704. ;; determine BEG and END, the region we will narrow to. If we're in
  3705. ;; a literal, constrain BEG and END to the limits of this literal.
  3706. ;;
  3707. ;; For some of these text types, particularly a block comment, we
  3708. ;; may need to massage whitespace near literal delimiters, so that
  3709. ;; these don't get filled inappropriately.
  3710. (cond
  3711. ((eq c-lit-type 'c++) ; Line comment.
  3712. (save-excursion
  3713. ;; Limit to the comment or paragraph end, whichever
  3714. ;; comes first.
  3715. (set-marker end (min end (cdr c-lit-limits)))
  3716. (when (<= beg (car c-lit-limits))
  3717. ;; The region includes the comment starter, so we must
  3718. ;; check it.
  3719. (goto-char (car c-lit-limits))
  3720. (back-to-indentation)
  3721. (if (eq (point) (car c-lit-limits))
  3722. ;; Include the first line in the region.
  3723. (setq beg (c-point 'bol))
  3724. ;; The first line contains code before the
  3725. ;; comment. We must fake a line that doesn't.
  3726. (setq tmp-pre t))))
  3727. (setq apply-outside-literal t))
  3728. ((eq c-lit-type 'c) ; Block comment.
  3729. (when
  3730. (or (> end (cdr c-lit-limits))
  3731. (and (= end (cdr c-lit-limits))
  3732. (eq (char-before end) ?/)
  3733. (eq (char-before (1- end)) ?*)
  3734. ;; disallow "/*/"
  3735. (> (- (cdr c-lit-limits) (car c-lit-limits)) 3)))
  3736. ;; There is a comment ender, and the region includes it. If
  3737. ;; it's on its own line, it stays on its own line. If it's got
  3738. ;; company on the line, it keeps (at least one word of) it.
  3739. ;; "=====*/" counts as a comment ender here, but "===== */"
  3740. ;; doesn't and "foo*/" doesn't.
  3741. (unless
  3742. (save-excursion
  3743. (goto-char (cdr c-lit-limits))
  3744. (beginning-of-line)
  3745. ;; The following conjunct was added to avoid an
  3746. ;; "Invalid search bound (wrong side of point)"
  3747. ;; error in the subsequent re-search. Maybe
  3748. ;; another fix would be needed (2007-12-08).
  3749. ; (or (<= (- (cdr c-lit-limits) 2) (point))
  3750. ; 2010-10-17 Construct removed.
  3751. ; (or (< (- (cdr c-lit-limits) 2) (point))
  3752. (and
  3753. (search-forward-regexp
  3754. (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)")
  3755. (- (cdr c-lit-limits) 2) t)
  3756. (not (search-forward-regexp
  3757. "\\(\\s \\|\\sw\\)"
  3758. (- (cdr c-lit-limits) 2) 'limit))
  3759. ;; The comment ender IS on its own line. Exclude this
  3760. ;; line from the filling.
  3761. (set-marker end (c-point 'bol))));)
  3762. ;; The comment ender is hanging. Replace all space between it
  3763. ;; and the last word either by one or two 'x's (when
  3764. ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width
  3765. ;; as the whitespace (when auto filling), and include it in
  3766. ;; the region. We'll change them back to whitespace
  3767. ;; afterwards. The effect of this is to glue the comment
  3768. ;; ender to the last word in the comment during filling.
  3769. (let* ((ender-start (save-excursion
  3770. (goto-char (cdr c-lit-limits))
  3771. (skip-syntax-backward "^w ")
  3772. (point)))
  3773. (ender-column (save-excursion
  3774. (goto-char ender-start)
  3775. (current-column)))
  3776. (point-rel (- ender-start here))
  3777. (sentence-ends-comment
  3778. (save-excursion
  3779. (goto-char ender-start)
  3780. (and (search-backward-regexp
  3781. (c-sentence-end) (c-point 'bol) t)
  3782. (goto-char (match-end 0))
  3783. (looking-at "[ \t]*")
  3784. (= (match-end 0) ender-start))))
  3785. spaces)
  3786. (save-excursion
  3787. ;; Insert a CR after the "*/", adjust END
  3788. (goto-char (cdr c-lit-limits))
  3789. (setq tmp-post (point-marker))
  3790. (insert ?\n)
  3791. (set-marker end (point))
  3792. (forward-line -1) ; last line of the comment
  3793. (if (and (looking-at (concat "[ \t]*\\(\\("
  3794. c-current-comment-prefix
  3795. "\\)[ \t]*\\)"))
  3796. (eq ender-start (match-end 0)))
  3797. ;; The comment ender is prefixed by nothing but a
  3798. ;; comment line prefix. IS THIS POSSIBLE? (ACM,
  3799. ;; 2006/4/28). Remove it along with surrounding ws.
  3800. (setq spaces (- (match-end 1) (match-end 2)))
  3801. (goto-char ender-start))
  3802. (skip-chars-backward " \t\r\n") ; Surely this can be
  3803. ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18)
  3804. ;; What's being tested here? 2006/4/20. FIXME!!!
  3805. (if (/= (point) ender-start)
  3806. (progn
  3807. (if (<= here (point))
  3808. ;; Don't adjust point below if it's
  3809. ;; before the string we replace.
  3810. (setq point-rel -1))
  3811. ;; Keep one or two spaces between the
  3812. ;; text and the ender, depending on how
  3813. ;; many there are now.
  3814. (unless spaces
  3815. (setq spaces (- ender-column (current-column))))
  3816. (setq auto-fill-spaces (c-delete-and-extract-region
  3817. (point) ender-start))
  3818. ;; paragraph filling condenses multiple spaces to
  3819. ;; single or double spaces. auto-fill doesn't.
  3820. (if fill-paragraph
  3821. (setq spaces
  3822. (max
  3823. (min spaces
  3824. (if (and sentence-ends-comment
  3825. sentence-end-double-space)
  3826. 2 1))
  3827. 1)))
  3828. ;; Insert the filler first to keep marks right.
  3829. (insert-char ?x spaces t)
  3830. (setq hang-ender-stuck spaces)
  3831. (setq point-rel
  3832. (and (>= point-rel 0)
  3833. (- (point) (min point-rel spaces)))))
  3834. (setq point-rel nil)))
  3835. (if point-rel
  3836. ;; Point was in the middle of the string we
  3837. ;; replaced above, so put it back in the same
  3838. ;; relative position, counting from the end.
  3839. (goto-char point-rel)))
  3840. ))
  3841. (when (<= beg (car c-lit-limits))
  3842. ;; The region includes the comment starter.
  3843. (save-excursion
  3844. (goto-char (car c-lit-limits))
  3845. (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
  3846. ;; Begin with the next line.
  3847. (setq beg (c-point 'bonl))
  3848. ;; Fake the fill prefix in the first line.
  3849. (setq tmp-pre t))))
  3850. (setq apply-outside-literal t))
  3851. ((eq c-lit-type 'string) ; String.
  3852. (save-excursion
  3853. (when (>= end (cdr c-lit-limits))
  3854. (goto-char (1- (cdr c-lit-limits)))
  3855. (setq tmp-post (point-marker))
  3856. (insert ?\n)
  3857. (set-marker end (point)))
  3858. (when (<= beg (car c-lit-limits))
  3859. (goto-char (1+ (car c-lit-limits)))
  3860. (setq beg (if (looking-at "\\\\$")
  3861. ;; Leave the start line if it's
  3862. ;; nothing but an escaped newline.
  3863. (1+ (match-end 0))
  3864. (point)))))
  3865. (setq apply-outside-literal t))
  3866. ((eq c-lit-type 'pound) ; Macro
  3867. ;; Narrow to the macro limits if they are nearer than the
  3868. ;; paragraph limits. Don't know if this is necessary but
  3869. ;; do it for completeness sake (doing auto filling at all
  3870. ;; inside macros is bogus to begin with since the line
  3871. ;; continuation backslashes aren't handled).
  3872. (save-excursion
  3873. (c-save-buffer-state ()
  3874. (c-beginning-of-macro)
  3875. (beginning-of-line)
  3876. (if (> (point) beg)
  3877. (setq beg (point)))
  3878. (c-end-of-macro)
  3879. (forward-line)
  3880. (if (< (point) end)
  3881. (set-marker end (point))))))
  3882. (t ; Other code.
  3883. ;; Try to avoid comments and macros in the paragraph to
  3884. ;; avoid that the adaptive fill mode gets the prefix from
  3885. ;; them.
  3886. (c-save-buffer-state nil
  3887. (save-excursion
  3888. (goto-char beg)
  3889. (c-forward-syntactic-ws end)
  3890. (beginning-of-line)
  3891. (setq beg (point))
  3892. (goto-char end)
  3893. (c-backward-syntactic-ws beg)
  3894. (forward-line)
  3895. (set-marker end (point))))))
  3896. (when tmp-pre
  3897. ;; Temporarily insert the fill prefix after the comment
  3898. ;; starter so that the first line looks like any other
  3899. ;; comment line in the narrowed region.
  3900. (setq fill (c-save-buffer-state nil
  3901. (c-guess-fill-prefix c-lit-limits c-lit-type)))
  3902. (unless (string-match (concat "\\`[ \t]*\\("
  3903. c-current-comment-prefix
  3904. "\\)[ \t]*\\'")
  3905. (car fill))
  3906. ;; Oops, the prefix doesn't match the comment prefix
  3907. ;; regexp. This could produce very confusing
  3908. ;; results with adaptive fill packages together with
  3909. ;; the insert prefix magic below, since the prefix
  3910. ;; often doesn't appear at all. So let's warn about
  3911. ;; it.
  3912. (message "\
  3913. Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
  3914. (car fill)))
  3915. ;; Find the right spot on the line, break it, insert
  3916. ;; the fill prefix and make sure we're back in the
  3917. ;; same column by temporarily prefixing the first word
  3918. ;; with a number of 'x'.
  3919. (save-excursion
  3920. (goto-char (car c-lit-limits))
  3921. (if (looking-at (if (eq c-lit-type 'c++)
  3922. c-current-comment-prefix
  3923. comment-start-skip))
  3924. (goto-char (match-end 0))
  3925. (forward-char 2)
  3926. (skip-chars-forward " \t"))
  3927. (while (and (< (current-column) (cdr fill))
  3928. (not (eolp)))
  3929. (forward-char 1))
  3930. (let ((col (current-column)))
  3931. (setq beg (1+ (point))
  3932. tmp-pre (list (point)))
  3933. (unwind-protect
  3934. (progn
  3935. (insert-and-inherit "\n" (car fill))
  3936. (insert-char ?x (- col (current-column)) t))
  3937. (setcdr tmp-pre (point))))))
  3938. (when apply-outside-literal
  3939. ;; `apply-outside-literal' is always set to t here if
  3940. ;; we're inside a literal.
  3941. (let ((fill-prefix
  3942. (or fill-prefix
  3943. ;; Kludge: If the function that adapts the fill prefix
  3944. ;; doesn't produce the required comment starter for
  3945. ;; line comments, then force it by setting fill-prefix.
  3946. (when (and (eq c-lit-type 'c++)
  3947. ;; Kludge the kludge: filladapt-mode doesn't
  3948. ;; have this problem, but it currently
  3949. ;; doesn't override fill-context-prefix
  3950. ;; (version 2.12).
  3951. (not (and (boundp 'filladapt-mode)
  3952. filladapt-mode))
  3953. (not (string-match
  3954. "\\`[ \t]*//"
  3955. (or (fill-context-prefix beg end)
  3956. ""))))
  3957. (c-save-buffer-state nil
  3958. (car (or fill (c-guess-fill-prefix
  3959. c-lit-limits c-lit-type)))))))
  3960. ;; Save the relative position of point if it's outside the
  3961. ;; region we're going to narrow. Want to restore it in that
  3962. ;; case, but otherwise it should be moved according to the
  3963. ;; called function.
  3964. (point-rel (cond ((< (point) beg) (- (point) beg))
  3965. ((> (point) end) (- (point) end)))))
  3966. ;; Preparations finally done! Now we can call the
  3967. ;; actual function.
  3968. (prog1
  3969. (save-restriction
  3970. (narrow-to-region beg end)
  3971. (apply fun args))
  3972. (if point-rel
  3973. ;; Restore point if it was outside the region.
  3974. (if (< point-rel 0)
  3975. (goto-char (+ beg point-rel))
  3976. (goto-char (+ end point-rel))))))))
  3977. (when (consp tmp-pre)
  3978. (delete-region (car tmp-pre) (cdr tmp-pre)))
  3979. (when tmp-post
  3980. (save-excursion
  3981. (goto-char tmp-post)
  3982. (delete-char 1))
  3983. (when hang-ender-stuck
  3984. ;; Preserve point even if it's in the middle of the string
  3985. ;; we replace; save-excursion doesn't work in that case.
  3986. (setq here (point))
  3987. (goto-char tmp-post)
  3988. (skip-syntax-backward "^w ")
  3989. (forward-char (- hang-ender-stuck))
  3990. (if (or fill-paragraph (not auto-fill-spaces))
  3991. (insert-char ?\ hang-ender-stuck t)
  3992. (insert auto-fill-spaces))
  3993. (delete-char hang-ender-stuck)
  3994. (goto-char here))
  3995. (set-marker tmp-post nil))
  3996. (set-marker end nil))))
  3997. (defun c-fill-paragraph (&optional arg)
  3998. "Like \\[fill-paragraph] but handles C and C++ style comments.
  3999. If any of the current line is a comment or within a comment, fill the
  4000. comment or the paragraph of it that point is in, preserving the
  4001. comment indentation or line-starting decorations (see the
  4002. `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
  4003. details).
  4004. If point is inside multiline string literal, fill it. This currently
  4005. does not respect escaped newlines, except for the special case when it
  4006. is the very first thing in the string. The intended use for this rule
  4007. is in situations like the following:
  4008. char description[] = \"\\
  4009. A very long description of something that you want to fill to make
  4010. nicely formatted output.\"\;
  4011. If point is in any other situation, i.e. in normal code, do nothing.
  4012. Optional prefix ARG means justify paragraph as well."
  4013. (interactive "*P")
  4014. (let ((fill-paragraph-function
  4015. ;; Avoid infinite recursion.
  4016. (if (not (eq fill-paragraph-function 'c-fill-paragraph))
  4017. fill-paragraph-function)))
  4018. (c-mask-paragraph t nil 'fill-paragraph arg))
  4019. ;; Always return t. This has the effect that if filling isn't done
  4020. ;; above, it isn't done at all, and it's therefore effectively
  4021. ;; disabled in normal code.
  4022. t)
  4023. (defun c-do-auto-fill ()
  4024. ;; Do automatic filling if not inside a context where it should be
  4025. ;; ignored.
  4026. (let ((c-auto-fill-prefix
  4027. ;; The decision whether the line should be broken is actually
  4028. ;; done in c-indent-new-comment-line, which do-auto-fill
  4029. ;; calls to break lines. We just set this special variable
  4030. ;; so that we'll know when we're called from there. It's
  4031. ;; also used to detect whether fill-prefix is user set or
  4032. ;; generated automatically by do-auto-fill.
  4033. fill-prefix))
  4034. (c-mask-paragraph nil t 'do-auto-fill)))
  4035. (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
  4036. "Break line at point and indent, continuing comment or macro if within one.
  4037. If inside a comment and `comment-multi-line' is non-nil, the
  4038. indentation and line prefix are preserved (see the
  4039. `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
  4040. details). If inside a single line comment and `comment-multi-line' is
  4041. nil, a new comment of the same type is started on the next line and
  4042. indented as appropriate for comments. If inside a macro, a line
  4043. continuation backslash is inserted and aligned as appropriate, and the
  4044. new line is indented according to `c-syntactic-indentation'.
  4045. If a fill prefix is specified, it overrides all the above."
  4046. ;; allow-auto-fill is used from c-context-line-break to allow auto
  4047. ;; filling to break the line more than once. Since this function is
  4048. ;; used from auto-fill itself, that's normally disabled to avoid
  4049. ;; unnecessary recursion.
  4050. (interactive)
  4051. (let ((fill-prefix fill-prefix)
  4052. (do-line-break
  4053. (lambda ()
  4054. (delete-horizontal-space)
  4055. (if soft
  4056. (insert-and-inherit ?\n)
  4057. (newline (if allow-auto-fill nil 1)))))
  4058. ;; Already know the literal type and limits when called from
  4059. ;; c-context-line-break.
  4060. (c-lit-limits c-lit-limits)
  4061. (c-lit-type c-lit-type)
  4062. (c-macro-start c-macro-start))
  4063. (c-save-buffer-state ()
  4064. (when (not (eq c-auto-fill-prefix t))
  4065. ;; Called from do-auto-fill.
  4066. (unless c-lit-limits
  4067. (setq c-lit-limits (c-literal-limits nil nil t)))
  4068. (unless c-lit-type
  4069. (setq c-lit-type (c-literal-type c-lit-limits)))
  4070. (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
  4071. ((null c-lit-type) 'code)
  4072. (t c-lit-type))
  4073. c-ignore-auto-fill)
  4074. (setq fill-prefix t) ; Used as flag in the cond.
  4075. (if (and (null c-auto-fill-prefix)
  4076. (eq c-lit-type 'c)
  4077. (<= (c-point 'bol) (car c-lit-limits)))
  4078. ;; The adaptive fill function has generated a prefix, but
  4079. ;; we're on the first line in a block comment so it'll be
  4080. ;; wrong. Ignore it to guess a better one below.
  4081. (setq fill-prefix nil)
  4082. (when (and (eq c-lit-type 'c++)
  4083. (not (string-match (concat "\\`[ \t]*"
  4084. c-line-comment-starter)
  4085. (or fill-prefix ""))))
  4086. ;; Kludge: If the function that adapted the fill prefix
  4087. ;; doesn't produce the required comment starter for line
  4088. ;; comments, then we ignore it.
  4089. (setq fill-prefix nil)))
  4090. )))
  4091. (cond ((eq fill-prefix t)
  4092. ;; A call from do-auto-fill which should be ignored.
  4093. )
  4094. (fill-prefix
  4095. ;; A fill-prefix overrides anything.
  4096. (funcall do-line-break)
  4097. (insert-and-inherit fill-prefix))
  4098. ((c-save-buffer-state ()
  4099. (unless c-lit-limits
  4100. (setq c-lit-limits (c-literal-limits)))
  4101. (unless c-lit-type
  4102. (setq c-lit-type (c-literal-type c-lit-limits)))
  4103. (memq c-lit-type '(c c++)))
  4104. ;; Some sort of comment.
  4105. (if (or comment-multi-line
  4106. (save-excursion
  4107. (goto-char (car c-lit-limits))
  4108. (end-of-line)
  4109. (< (point) (cdr c-lit-limits))))
  4110. ;; Inside a comment that should be continued.
  4111. (let ((fill (c-save-buffer-state nil
  4112. (c-guess-fill-prefix
  4113. (setq c-lit-limits
  4114. (c-collect-line-comments c-lit-limits))
  4115. c-lit-type)))
  4116. (pos (point))
  4117. (start-col (current-column))
  4118. (comment-text-end
  4119. (or (and (eq c-lit-type 'c)
  4120. (save-excursion
  4121. (goto-char (- (cdr c-lit-limits) 2))
  4122. (if (looking-at "\\*/") (point))))
  4123. (cdr c-lit-limits))))
  4124. ;; Skip forward past the fill prefix in case
  4125. ;; we're standing in it.
  4126. ;;
  4127. ;; FIXME: This doesn't work well in cases like
  4128. ;;
  4129. ;; /* Bla bla bla bla bla
  4130. ;; bla bla
  4131. ;;
  4132. ;; If point is on the 'B' then the line will be
  4133. ;; broken after "Bla b".
  4134. ;;
  4135. ;; If we have an empty comment, /* */, the next
  4136. ;; lot of code pushes point to the */. We fix
  4137. ;; this by never allowing point to end up to the
  4138. ;; right of where it started.
  4139. (while (and (< (current-column) (cdr fill))
  4140. (not (eolp)))
  4141. (forward-char 1))
  4142. (if (and (> (point) comment-text-end)
  4143. (> (c-point 'bol) (car c-lit-limits)))
  4144. (progn
  4145. ;; The skip takes us out of the (block)
  4146. ;; comment; insert the fill prefix at bol
  4147. ;; instead and keep the position.
  4148. (setq pos (copy-marker pos t))
  4149. (beginning-of-line)
  4150. (insert-and-inherit (car fill))
  4151. (if soft (insert-and-inherit ?\n) (newline 1))
  4152. (goto-char pos)
  4153. (set-marker pos nil))
  4154. ;; Don't break in the middle of a comment starter
  4155. ;; or ender.
  4156. (cond ((> (point) comment-text-end)
  4157. (goto-char comment-text-end))
  4158. ((< (point) (+ (car c-lit-limits) 2))
  4159. (goto-char (+ (car c-lit-limits) 2))))
  4160. (funcall do-line-break)
  4161. (insert-and-inherit (car fill))
  4162. (if (> (current-column) start-col)
  4163. (move-to-column start-col)))) ; can this hit the
  4164. ; middle of a TAB?
  4165. ;; Inside a comment that should be broken.
  4166. (let ((comment-start comment-start)
  4167. (comment-end comment-end)
  4168. col)
  4169. (if (eq c-lit-type 'c)
  4170. (unless (string-match "[ \t]*/\\*" comment-start)
  4171. (setq comment-start "/* " comment-end " */"))
  4172. (unless (string-match "[ \t]*//" comment-start)
  4173. (setq comment-start "// " comment-end "")))
  4174. (setq col (save-excursion
  4175. (back-to-indentation)
  4176. (current-column)))
  4177. (funcall do-line-break)
  4178. (when (and comment-end (not (equal comment-end "")))
  4179. (forward-char -1)
  4180. (insert-and-inherit comment-end)
  4181. (forward-char 1))
  4182. ;; c-comment-indent may look at the current
  4183. ;; indentation, so let's start out with the same
  4184. ;; indentation as the previous one.
  4185. (indent-to col)
  4186. (insert-and-inherit comment-start)
  4187. (indent-for-comment))))
  4188. ((c-query-and-set-macro-start)
  4189. ;; In a macro.
  4190. (unless (looking-at "[ \t]*\\\\$")
  4191. ;; Do not clobber the alignment of the line continuation
  4192. ;; slash; c-backslash-region might look at it.
  4193. (delete-horizontal-space))
  4194. ;; Got an asymmetry here: In normal code this command
  4195. ;; doesn't indent the next line syntactically, and otoh a
  4196. ;; normal syntactically indenting newline doesn't continue
  4197. ;; the macro.
  4198. (c-newline-and-indent (if allow-auto-fill nil 1)))
  4199. (t
  4200. ;; Somewhere else in the code.
  4201. (let ((col (save-excursion
  4202. (beginning-of-line)
  4203. (while (and (looking-at "[ \t]*\\\\?$")
  4204. (= (forward-line -1) 0)))
  4205. (current-indentation))))
  4206. (funcall do-line-break)
  4207. (indent-to col))))))
  4208. (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
  4209. (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line "21.1")
  4210. ;; advice for indent-new-comment-line for older Emacsen
  4211. (unless (boundp 'comment-line-break-function)
  4212. (defvar c-inside-line-break-advice nil)
  4213. (defadvice indent-new-comment-line (around c-line-break-advice
  4214. activate preactivate)
  4215. "Call `c-indent-new-comment-line' if in CC Mode."
  4216. (if (or c-inside-line-break-advice
  4217. (not c-buffer-is-cc-mode))
  4218. ad-do-it
  4219. (let ((c-inside-line-break-advice t))
  4220. (c-indent-new-comment-line (ad-get-arg 0))))))
  4221. (defun c-context-line-break ()
  4222. "Do a line break suitable to the context.
  4223. When point is outside a comment or macro, insert a newline and indent
  4224. according to the syntactic context, unless `c-syntactic-indentation'
  4225. is nil, in which case the new line is indented as the previous
  4226. non-empty line instead.
  4227. When point is inside the content of a preprocessor directive, a line
  4228. continuation backslash is inserted before the line break and aligned
  4229. appropriately. The end of the cpp directive doesn't count as inside
  4230. it.
  4231. When point is inside a comment, continue it with the appropriate
  4232. comment prefix (see the `c-comment-prefix-regexp' and
  4233. `c-block-comment-prefix' variables for details). The end of a
  4234. C++-style line comment doesn't count as inside it.
  4235. When point is inside a string, only insert a backslash when it is also
  4236. inside a preprocessor directive."
  4237. (interactive "*")
  4238. (let* (c-lit-limits c-lit-type
  4239. (c-macro-start c-macro-start)
  4240. case-fold-search)
  4241. (c-save-buffer-state ()
  4242. (setq c-lit-limits (c-literal-limits nil nil t)
  4243. c-lit-type (c-literal-type c-lit-limits))
  4244. (when (eq c-lit-type 'c++)
  4245. (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
  4246. (c-query-and-set-macro-start))
  4247. (cond
  4248. ((or (eq c-lit-type 'c)
  4249. (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
  4250. (< (save-excursion
  4251. (skip-chars-forward " \t")
  4252. (point))
  4253. (1- (cdr c-lit-limits))))
  4254. (and (numberp c-macro-start) ; Macro, but not at the very end of
  4255. ; it, not in a string, and not in the
  4256. ; cpp keyword.
  4257. (not (eq c-lit-type 'string))
  4258. (or (not (looking-at "\\s *$"))
  4259. (eq (char-before) ?\\))
  4260. (<= (save-excursion
  4261. (goto-char c-macro-start)
  4262. (if (looking-at c-opt-cpp-start)
  4263. (goto-char (match-end 0)))
  4264. (point))
  4265. (point))))
  4266. (let ((comment-multi-line t)
  4267. (fill-prefix nil))
  4268. (c-indent-new-comment-line nil t)))
  4269. ((eq c-lit-type 'string)
  4270. (if (and (numberp c-macro-start)
  4271. (not (eq (char-before) ?\\)))
  4272. (insert ?\\))
  4273. (newline))
  4274. (t (delete-horizontal-space)
  4275. (newline)
  4276. ;; c-indent-line may look at the current indentation, so let's
  4277. ;; start out with the same indentation as the previous line.
  4278. (let ((col (save-excursion
  4279. (backward-char)
  4280. (forward-line 0)
  4281. (while (and (looking-at "[ \t]*\\\\?$")
  4282. (= (forward-line -1) 0)))
  4283. (current-indentation))))
  4284. (indent-to col))
  4285. (indent-according-to-mode)))))
  4286. (defun c-context-open-line ()
  4287. "Insert a line break suitable to the context and leave point before it.
  4288. This is the `c-context-line-break' equivalent to `open-line', which is
  4289. normally bound to C-o. See `c-context-line-break' for the details."
  4290. (interactive "*")
  4291. (let ((here (point)))
  4292. (unwind-protect
  4293. (progn
  4294. ;; Temporarily insert a non-whitespace char to keep any
  4295. ;; preceding whitespace intact.
  4296. (insert ?x)
  4297. (c-context-line-break))
  4298. (goto-char here)
  4299. (delete-char 1))))
  4300. (cc-provide 'cc-cmds)
  4301. ;;; cc-cmds.el ends here