cc-cmds.el 167 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727
  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. modeline 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 modeline 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. ;; Objc selectors.
  1650. ((assq 'objc-method-intro (c-guess-basic-syntax))
  1651. (let ((bound (save-excursion (c-end-of-statement) (point)))
  1652. (kw-re (concat "\\(?:" c-symbol-key "\\)?:"))
  1653. (stretches))
  1654. (when (c-syntactic-re-search-forward c-symbol-key bound t t t)
  1655. (push (match-string-no-properties 0) stretches)
  1656. (while (c-syntactic-re-search-forward kw-re bound t t t)
  1657. (push (match-string-no-properties 0) stretches)))
  1658. (apply 'concat (nreverse stretches))))
  1659. (t
  1660. ;; Normal function or initializer.
  1661. (when (c-syntactic-re-search-forward "[{(]" nil t)
  1662. (backward-char)
  1663. (c-backward-syntactic-ws)
  1664. (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ;
  1665. (c-backward-token-2)
  1666. (c-backward-syntactic-ws))
  1667. (setq name-end (point))
  1668. (c-backward-token-2)
  1669. (buffer-substring-no-properties (point) name-end)))))))))
  1670. (defun c-declaration-limits (near)
  1671. ;; Return a cons of the beginning and end positions of the current
  1672. ;; top level declaration or macro. If point is not inside any then
  1673. ;; nil is returned, unless NEAR is non-nil in which case the closest
  1674. ;; following one is chosen instead (if there is any). The end
  1675. ;; position is at the next line, providing there is one before the
  1676. ;; declaration.
  1677. ;;
  1678. ;; This function might do hidden buffer changes.
  1679. (save-excursion
  1680. (save-restriction
  1681. (when (eq c-defun-tactic 'go-outward)
  1682. (c-narrow-to-most-enclosing-decl-block t) ; e.g. class, namespace
  1683. (or (save-restriction
  1684. (c-narrow-to-most-enclosing-decl-block nil)
  1685. ;; Note: Some code duplication in `c-beginning-of-defun' and
  1686. ;; `c-end-of-defun'.
  1687. (catch 'exit
  1688. (let ((start (point))
  1689. (paren-state (c-parse-state))
  1690. lim pos end-pos)
  1691. (unless (c-safe
  1692. (goto-char (c-least-enclosing-brace paren-state))
  1693. ;; If we moved to the outermost enclosing paren
  1694. ;; then we can use c-safe-position to set the
  1695. ;; limit. Can't do that otherwise since the
  1696. ;; earlier paren pair on paren-state might very
  1697. ;; well be part of the declaration we should go
  1698. ;; to.
  1699. (setq lim (c-safe-position (point) paren-state))
  1700. t)
  1701. ;; At top level. Make sure we aren't inside a literal.
  1702. (setq pos (c-literal-limits
  1703. (c-safe-position (point) paren-state)))
  1704. (if pos (goto-char (car pos))))
  1705. (when (c-beginning-of-macro)
  1706. (throw 'exit
  1707. (cons (point)
  1708. (save-excursion
  1709. (c-end-of-macro)
  1710. (forward-line 1)
  1711. (point)))))
  1712. (setq pos (point))
  1713. (when (or (eq (car (c-beginning-of-decl-1 lim)) 'previous)
  1714. (= pos (point)))
  1715. ;; We moved back over the previous defun. Skip to the next
  1716. ;; one. Not using c-forward-syntactic-ws here since we
  1717. ;; should not skip a macro. We can also be directly after
  1718. ;; the block in a `c-opt-block-decls-with-vars-key'
  1719. ;; declaration, but then we won't move significantly far
  1720. ;; here.
  1721. (goto-char pos)
  1722. (c-forward-comments)
  1723. (when (and near (c-beginning-of-macro))
  1724. (throw 'exit
  1725. (cons (point)
  1726. (save-excursion
  1727. (c-end-of-macro)
  1728. (forward-line 1)
  1729. (point))))))
  1730. (if (eobp) (throw 'exit nil))
  1731. ;; Check if `c-beginning-of-decl-1' put us after the block in a
  1732. ;; declaration that doesn't end there. We're searching back and
  1733. ;; forth over the block here, which can be expensive.
  1734. (setq pos (point))
  1735. (if (and c-opt-block-decls-with-vars-key
  1736. (progn
  1737. (c-backward-syntactic-ws)
  1738. (eq (char-before) ?}))
  1739. (eq (car (c-beginning-of-decl-1))
  1740. 'previous)
  1741. (save-excursion
  1742. (c-end-of-decl-1)
  1743. (and (> (point) pos)
  1744. (setq end-pos (point)))))
  1745. nil
  1746. (goto-char pos))
  1747. (if (and (not near) (> (point) start))
  1748. nil
  1749. ;; Try to be line oriented; position the limits at the
  1750. ;; closest preceding boi, and after the next newline, that
  1751. ;; isn't inside a comment, but if we hit a neighboring
  1752. ;; declaration then we instead use the exact declaration
  1753. ;; limit in that direction.
  1754. (cons (progn
  1755. (setq pos (point))
  1756. (while (and (/= (point) (c-point 'boi))
  1757. (c-backward-single-comment)))
  1758. (if (/= (point) (c-point 'boi))
  1759. pos
  1760. (point)))
  1761. (progn
  1762. (if end-pos
  1763. (goto-char end-pos)
  1764. (c-end-of-decl-1))
  1765. (setq pos (point))
  1766. (while (and (not (bolp))
  1767. (not (looking-at "\\s *$"))
  1768. (c-forward-single-comment)))
  1769. (cond ((bolp)
  1770. (point))
  1771. ((looking-at "\\s *$")
  1772. (forward-line 1)
  1773. (point))
  1774. (t
  1775. pos))))))))
  1776. (and (not near)
  1777. (goto-char (point-min))
  1778. (c-forward-decl-or-cast-1 -1 nil nil)
  1779. (eq (char-after) ?\{)
  1780. (cons (point-min) (point-max))))))))
  1781. (defun c-mark-function ()
  1782. "Put mark at end of the current top-level declaration or macro, point at beginning.
  1783. If point is not inside any then the closest following one is
  1784. chosen. Each successive call of this command extends the marked
  1785. region by one function.
  1786. A mark is left where the command started, unless the region is already active
  1787. \(in Transient Mark mode).
  1788. As opposed to \\[c-beginning-of-defun] and \\[c-end-of-defun], this
  1789. function does not require the declaration to contain a brace block."
  1790. (interactive)
  1791. (let (decl-limits case-fold-search)
  1792. (c-save-buffer-state nil
  1793. ;; We try to be line oriented, unless there are several
  1794. ;; declarations on the same line.
  1795. (if (looking-at c-syntactic-eol)
  1796. (c-backward-token-2 1 nil (c-point 'bol)))
  1797. (setq decl-limits (c-declaration-limits t)))
  1798. (if (not decl-limits)
  1799. (error "Cannot find any declaration")
  1800. (let* ((extend-region-p
  1801. (and (eq this-command 'c-mark-function)
  1802. (eq last-command 'c-mark-function)))
  1803. (push-mark-p (and (eq this-command 'c-mark-function)
  1804. (not extend-region-p)
  1805. (not (and transient-mark-mode mark-active)))))
  1806. (if push-mark-p (push-mark (point)))
  1807. (if extend-region-p
  1808. (progn
  1809. (exchange-point-and-mark)
  1810. (setq decl-limits (c-declaration-limits t))
  1811. (when (not decl-limits)
  1812. (exchange-point-and-mark)
  1813. (error "Cannot find any declaration"))
  1814. (goto-char (cdr decl-limits))
  1815. (exchange-point-and-mark))
  1816. (goto-char (car decl-limits))
  1817. (push-mark (cdr decl-limits) nil t))))))
  1818. (defun c-cpp-define-name ()
  1819. "Return the name of the current CPP macro, or NIL if we're not in one."
  1820. (interactive)
  1821. (let (case-fold-search)
  1822. (save-excursion
  1823. (and c-opt-cpp-macro-define-start
  1824. (c-beginning-of-macro)
  1825. (looking-at c-opt-cpp-macro-define-start)
  1826. (match-string-no-properties 1)))))
  1827. ;; Movement by statements.
  1828. (defun c-in-comment-line-prefix-p ()
  1829. ;; Point is within a comment. Is it also within a comment-prefix?
  1830. ;; Space at BOL which precedes a comment-prefix counts as part of it.
  1831. ;;
  1832. ;; This function might do hidden buffer changes.
  1833. (let ((here (point)))
  1834. (save-excursion
  1835. (beginning-of-line)
  1836. (skip-chars-forward " \t")
  1837. (and (looking-at c-current-comment-prefix)
  1838. (/= (match-beginning 0) (match-end 0))
  1839. (< here (match-end 0))))))
  1840. (defun c-narrow-to-comment-innards (range)
  1841. ;; Narrow to the "inside" of the comment (block) defined by range, as
  1842. ;; follows:
  1843. ;;
  1844. ;; A c-style block comment has its opening "/*" and its closing "*/" (if
  1845. ;; present) removed. A c++-style line comment retains its opening "//" but
  1846. ;; has any final NL removed. If POINT is currently outwith these innards,
  1847. ;; move it to the appropriate boundary.
  1848. ;;
  1849. ;; This narrowing simplifies the sentence movement functions, since it
  1850. ;; eliminates awkward things at the boundaries of the comment (block).
  1851. ;;
  1852. ;; This function might do hidden buffer changes.
  1853. (let* ((lit-type (c-literal-type range))
  1854. (beg (if (eq lit-type 'c) (+ (car range) 2) (car range)))
  1855. (end (if (eq lit-type 'c)
  1856. (if (and (eq (char-before (cdr range)) ?/)
  1857. (eq (char-before (1- (cdr range))) ?*))
  1858. (- (cdr range) 2)
  1859. (point-max))
  1860. (if (eq (cdr range) (point-max))
  1861. (point-max)
  1862. (- (cdr range) 1)))))
  1863. (if (> (point) end)
  1864. (goto-char end)) ; This would be done automatically by ...
  1865. (if (< (point) beg)
  1866. (goto-char beg)) ; ... narrow-to-region but is not documented.
  1867. (narrow-to-region beg end)))
  1868. (defun c-beginning-of-sentence-in-comment (range)
  1869. ;; Move backwards to the "beginning of a sentence" within the comment
  1870. ;; defined by RANGE, a cons of its starting and ending positions. If we
  1871. ;; find a BOS, return NIL. Otherwise, move point to just before the start
  1872. ;; of the comment and return T.
  1873. ;;
  1874. ;; The BOS is either text which follows a regexp match of sentence-end,
  1875. ;; or text which is a beginning of "paragraph".
  1876. ;; Comment-prefixes are treated like WS when calculating BOSes or BOPs.
  1877. ;;
  1878. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1879. ;; It is not a general function, but is intended only for calling from
  1880. ;; c-move-over-sentence. Not all preconditions have been explicitly stated.
  1881. ;;
  1882. ;; This function might do hidden buffer changes.
  1883. (save-match-data
  1884. (let ((start-point (point)))
  1885. (save-restriction
  1886. (c-narrow-to-comment-innards range) ; This may move point back.
  1887. (let* ((here (point))
  1888. last
  1889. (here-filler ; matches WS and comment-prefixes at point.
  1890. (concat "\\=\\(^[ \t]*\\(" c-current-comment-prefix "\\)"
  1891. "\\|[ \t\n\r\f]\\)*"))
  1892. (prefix-at-bol-here ; matches WS and prefix at BOL, just before point
  1893. (concat "^[ \t]*\\(" c-current-comment-prefix "\\)[ \t\n\r\f]*\\="))
  1894. ;; First, find the previous paragraph start, if any.
  1895. (par-beg ; point where non-WS/non-prefix text of paragraph starts.
  1896. (save-excursion
  1897. (forward-paragraph -1) ; uses cc-mode values of
  1898. ; paragraph-\(start\|separate\)
  1899. (if (> (re-search-forward here-filler nil t) here)
  1900. (goto-char here))
  1901. (when (>= (point) here)
  1902. (forward-paragraph -2)
  1903. (if (> (re-search-forward here-filler nil t) here)
  1904. (goto-char here)))
  1905. (point))))
  1906. ;; Now seek successively earlier sentence ends between PAR-BEG and
  1907. ;; HERE, until the "start of sentence" following it is earlier than
  1908. ;; HERE, or we hit PAR-BEG. Beware of comment prefixes!
  1909. (while (and (re-search-backward (c-sentence-end) par-beg 'limit)
  1910. (setq last (point))
  1911. (goto-char (match-end 0)) ; tentative beginning of sentence
  1912. (or (>= (point) here)
  1913. (and (not (bolp)) ; Found a non-blank comment-prefix?
  1914. (save-excursion
  1915. (if (re-search-backward prefix-at-bol-here nil t)
  1916. (/= (match-beginning 1) (match-end 1)))))
  1917. (progn ; Skip the crud to find a real b-o-s.
  1918. (if (c-in-comment-line-prefix-p)
  1919. (beginning-of-line))
  1920. (re-search-forward here-filler) ; always succeeds.
  1921. (>= (point) here))))
  1922. (goto-char last))
  1923. (re-search-forward here-filler)))
  1924. (if (< (point) start-point)
  1925. nil
  1926. (goto-char (car range))
  1927. t))))
  1928. (defun c-end-of-sentence-in-comment (range)
  1929. ;; Move forward to the "end of a sentence" within the comment defined by
  1930. ;; RANGE, a cons of its starting and ending positions (enclosing the opening
  1931. ;; comment delimiter and the terminating */ or newline). If we find an EOS,
  1932. ;; return NIL. Otherwise, move point to just after the end of the comment
  1933. ;; and return T.
  1934. ;;
  1935. ;; The EOS is just after the non-WS part of the next match of the regexp
  1936. ;; sentence-end. Typically, this is just after one of [.!?]. If there is
  1937. ;; no sentence-end match following point, any WS before the end of the
  1938. ;; comment will count as EOS, providing we're not already in it.
  1939. ;;
  1940. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  1941. ;; It is not a general function, but is intended only for calling from
  1942. ;; c-move-over-sentence.
  1943. ;;
  1944. ;; This function might do hidden buffer changes.
  1945. (save-match-data
  1946. (let ((start-point (point))
  1947. ;; (lit-type (c-literal-type range)) ; Commented out, 2005/11/23, ACM
  1948. )
  1949. (save-restriction
  1950. (c-narrow-to-comment-innards range) ; This might move point forwards.
  1951. (let* ((here (point))
  1952. (par-end ; EOL position of last text in current/next paragraph.
  1953. (save-excursion
  1954. ;; The cc-mode values of paragraph-\(start\|separate\), set
  1955. ;; in c-setup-paragraph-variables, are used in the
  1956. ;; following.
  1957. (forward-paragraph 1)
  1958. (if (eq (preceding-char) ?\n) (forward-char -1))
  1959. (when (<= (point) here) ; can happen, e.g., when HERE is at EOL.
  1960. (goto-char here)
  1961. (forward-paragraph 2)
  1962. (if (eq (preceding-char) ?\n) (forward-char -1)))
  1963. (point)))
  1964. last
  1965. (prefix-at-bol-here
  1966. (concat "^[ \t]*\\(" c-current-comment-prefix "\\)\\=")))
  1967. ;; Go forward one "comment-prefix which looks like sentence-end"
  1968. ;; each time round the following:
  1969. (while (and (re-search-forward (c-sentence-end) par-end 'limit)
  1970. (progn
  1971. (setq last (point))
  1972. (skip-chars-backward " \t\n")
  1973. (or (and (not (bolp))
  1974. (re-search-backward prefix-at-bol-here nil t)
  1975. (/= (match-beginning 1) (match-end 1)))
  1976. (<= (point) here))))
  1977. (goto-char last))
  1978. ;; Take special action if we're up against the end of a comment (of
  1979. ;; either sort): Leave point just after the last non-ws text.
  1980. (if (eq (point) (point-max))
  1981. (while (or (/= (skip-chars-backward " \t\n") 0)
  1982. (and (re-search-backward prefix-at-bol-here nil t)
  1983. (/= (match-beginning 1) (match-end 1))))))))
  1984. (if (> (point) start-point)
  1985. nil
  1986. (goto-char (cdr range))
  1987. t))))
  1988. (defun c-beginning-of-sentence-in-string (range)
  1989. ;; Move backwards to the "beginning of a sentence" within the string defined
  1990. ;; by RANGE, a cons of its starting and ending positions (enclosing the
  1991. ;; string quotes). If we find a BOS, return NIL. Otherwise, move point to
  1992. ;; just before the start of the string and return T.
  1993. ;;
  1994. ;; The BOS is either the text which follows a regexp match of sentence-end
  1995. ;; or text which is a beginning of "paragraph". For the purposes of
  1996. ;; determining paragraph boundaries, escaped newlines are treated as
  1997. ;; ordinary newlines.
  1998. ;;
  1999. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  2000. ;; It is not a general function, but is intended only for calling from
  2001. ;; c-move-over-sentence.
  2002. ;;
  2003. ;; This function might do hidden buffer changes.
  2004. (save-match-data
  2005. (let* ((here (point)) last
  2006. (end (1- (cdr range)))
  2007. (here-filler ; matches WS and escaped newlines at point.
  2008. "\\=\\([ \t\n\r\f]\\|\\\\[\n\r]\\)*")
  2009. ;; Enhance paragraph-start and paragraph-separate also to recognize
  2010. ;; blank lines terminated by escaped EOLs. IT MAY WELL BE that
  2011. ;; these values should be customizable user options, or something.
  2012. (paragraph-start c-string-par-start)
  2013. (paragraph-separate c-string-par-separate)
  2014. (par-beg ; beginning of current (or previous) paragraph.
  2015. (save-excursion
  2016. (save-restriction
  2017. (narrow-to-region (1+ (car range)) end)
  2018. (forward-paragraph -1) ; uses above values of
  2019. ; paragraph-\(start\|separate\)
  2020. (if (> (re-search-forward here-filler nil t) here)
  2021. (goto-char here))
  2022. (when (>= (point) here)
  2023. (forward-paragraph -2)
  2024. (if (> (re-search-forward here-filler nil t) here)
  2025. (goto-char here)))
  2026. (point)))))
  2027. ;; Now see if we can find a sentence end after PAR-BEG.
  2028. (while (and (re-search-backward c-sentence-end-with-esc-eol par-beg 'limit)
  2029. (setq last (point))
  2030. (goto-char (match-end 0))
  2031. (or (> (point) end)
  2032. (progn
  2033. (re-search-forward
  2034. here-filler end t) ; always succeeds. Use end rather
  2035. ; than here, in case point starts
  2036. ; beyond the closing quote.
  2037. (>= (point) here))))
  2038. (goto-char last))
  2039. (re-search-forward here-filler here t)
  2040. (if (< (point) here)
  2041. nil
  2042. (goto-char (car range))
  2043. t))))
  2044. (defun c-end-of-sentence-in-string (range)
  2045. ;; Move forward to the "end of a sentence" within the string defined by
  2046. ;; RANGE, a cons of its starting and ending positions. If we find an EOS,
  2047. ;; return NIL. Otherwise, move point to just after the end of the string
  2048. ;; and return T.
  2049. ;;
  2050. ;; The EOS is just after the non-WS part of the next match of the regexp
  2051. ;; sentence-end. Typically, this is just after one of [.!?]. If there is
  2052. ;; no sentence-end match following point, any WS before the end of the
  2053. ;; string will count as EOS, providing we're not already in it.
  2054. ;;
  2055. ;; This code was adapted from GNU Emacs's forward-sentence in paragraphs.el.
  2056. ;; It is not a general function, but is intended only for calling from
  2057. ;; c-move-over-sentence.
  2058. ;;
  2059. ;; This function might do hidden buffer changes.
  2060. (save-match-data
  2061. (let* ((here (point))
  2062. last
  2063. ;; Enhance paragraph-start and paragraph-separate to recognize
  2064. ;; blank lines terminated by escaped EOLs.
  2065. (paragraph-start c-string-par-start)
  2066. (paragraph-separate c-string-par-separate)
  2067. (par-end ; EOL position of last text in current/next paragraph.
  2068. (save-excursion
  2069. (save-restriction
  2070. (narrow-to-region (car range) (1- (cdr range)))
  2071. ;; The above values of paragraph-\(start\|separate\) are used
  2072. ;; in the following.
  2073. (forward-paragraph 1)
  2074. (setq last (point))
  2075. ;; (re-search-backward filler-here nil t) would find an empty
  2076. ;; string. Therefore we simulate it by the following:
  2077. (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
  2078. (re-search-backward "\\\\\\($\\)\\=" nil t)))
  2079. (unless (> (point) here)
  2080. (goto-char last)
  2081. (forward-paragraph 1)
  2082. (while (or (/= (skip-chars-backward " \t\n\r\f") 0)
  2083. (re-search-backward "\\\\\\($\\)\\=" nil t))))
  2084. (point)))))
  2085. ;; Try to go forward a sentence.
  2086. (when (re-search-forward c-sentence-end-with-esc-eol par-end 'limit)
  2087. (setq last (point))
  2088. (while (or (/= (skip-chars-backward " \t\n") 0)
  2089. (re-search-backward "\\\\\\($\\)\\=" nil t))))
  2090. ;; Did we move a sentence, or did we hit the end of the string?
  2091. (if (> (point) here)
  2092. nil
  2093. (goto-char (cdr range))
  2094. t))))
  2095. (defun c-ascertain-preceding-literal ()
  2096. ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
  2097. ;; If a literal is the next thing (aside from whitespace) to be found before
  2098. ;; point, return a cons of its start.end positions (enclosing the
  2099. ;; delimiters). Otherwise return NIL.
  2100. ;;
  2101. ;; This function might do hidden buffer changes.
  2102. (save-excursion
  2103. (c-collect-line-comments
  2104. (let ((here (point))
  2105. pos)
  2106. (if (c-backward-single-comment)
  2107. (cons (point) (progn (c-forward-single-comment) (point)))
  2108. (save-restriction
  2109. ;; to prevent `looking-at' seeing a " at point.
  2110. (narrow-to-region (point-min) here)
  2111. (when
  2112. (or
  2113. ;; An EOL can act as an "open string" terminator in AWK.
  2114. (looking-at c-ws*-string-limit-regexp)
  2115. (and (not (bobp))
  2116. (progn (backward-char)
  2117. (looking-at c-string-limit-regexp))))
  2118. (goto-char (match-end 0)) ; just after the string terminator.
  2119. (setq pos (point))
  2120. (c-safe (c-backward-sexp 1) ; move back over the string.
  2121. (cons (point) pos)))))))))
  2122. (defun c-ascertain-following-literal ()
  2123. ;; Point is not in a literal (i.e. comment or string (include AWK regexp)).
  2124. ;; If a literal is the next thing (aside from whitespace) following point,
  2125. ;; return a cons of its start.end positions (enclosing the delimiters).
  2126. ;; Otherwise return NIL.
  2127. ;;
  2128. ;; This function might do hidden buffer changes.
  2129. (save-excursion
  2130. (c-collect-line-comments
  2131. (let (pos)
  2132. (c-skip-ws-forward)
  2133. (if (looking-at c-string-limit-regexp) ; string-delimiter.
  2134. (cons (point) (or (c-safe (progn (c-forward-sexp 1) (point)))
  2135. (point-max)))
  2136. (setq pos (point))
  2137. (if (c-forward-single-comment)
  2138. (cons pos (point))))))))
  2139. (defun c-after-statement-terminator-p () ; Should we pass in LIM here?
  2140. ;; Does point immediately follow a statement "terminator"? A virtual
  2141. ;; semicolon is regarded here as such. So is an opening brace ;-)
  2142. ;;
  2143. ;; This function might do hidden buffer changes.
  2144. (or (save-excursion
  2145. (backward-char)
  2146. (and (looking-at "[;{}]")
  2147. (not (and c-special-brace-lists ; Pike special brace lists.
  2148. (eq (char-after) ?{)
  2149. (c-looking-at-special-brace-list)))))
  2150. (c-at-vsemi-p)
  2151. ;; The following (for macros) is not strict about exactly where we are
  2152. ;; wrt white space at the end of the macro. Doesn't seem to matter too
  2153. ;; much. ACM 2004/3/29.
  2154. (let (eom)
  2155. (save-excursion
  2156. (if (c-beginning-of-macro)
  2157. (setq eom (progn (c-end-of-macro)
  2158. (point)))))
  2159. (when eom
  2160. (save-excursion
  2161. (c-forward-comments)
  2162. (>= (point) eom))))))
  2163. (defun c-back-over-illiterals (macro-start)
  2164. ;; Move backwards over code which isn't a literal (i.e. comment or string),
  2165. ;; stopping before reaching BOB or a literal or the boundary of a
  2166. ;; preprocessor statement or the "beginning of a statement". MACRO-START is
  2167. ;; the position of the '#' beginning the current preprocessor directive, or
  2168. ;; NIL if we're not in such.
  2169. ;;
  2170. ;; Return a cons (A.B), where
  2171. ;; A is NIL if we moved back to a BOS (and know it), T otherwise (we
  2172. ;; didn't move, or we hit a literal, or we're not sure about BOS).
  2173. ;; B is MACRO-BOUNDARY if we are about to cross the boundary out of or
  2174. ;; into a macro, otherwise LITERAL if we've hit a literal, otherwise NIL
  2175. ;;
  2176. ;; The total collection of returned values is as follows:
  2177. ;; (nil . nil): Found a BOS whilst remaining inside the illiterals.
  2178. ;; (t . literal): No BOS found: only a comment/string. We _might_ be at
  2179. ;; a BOS - the caller must check this.
  2180. ;; (nil . macro-boundary): only happens with non-nil macro-start. We've
  2181. ;; moved and reached the opening # of the macro.
  2182. ;; (t . macro-boundary): Every other circumstance in which we're at a
  2183. ;; macro-boundary. We might be at a BOS.
  2184. ;;
  2185. ;; Point is left either at the beginning-of-statement, or at the last non-ws
  2186. ;; code before encountering the literal/BOB or macro-boundary.
  2187. ;;
  2188. ;; Note that this function moves within either preprocessor commands
  2189. ;; (macros) or normal code, but will not cross a boundary between the two,
  2190. ;; or between two distinct preprocessor commands.
  2191. ;;
  2192. ;; Stop before `{' and after `;', `{', `}' and `};' when not followed by `}'
  2193. ;; or `)', but on the other side of the syntactic ws. Move by sexps and
  2194. ;; move into parens. Also stop before `#' when it's at boi on a line.
  2195. ;;
  2196. ;; This function might do hidden buffer changes.
  2197. (save-match-data
  2198. (let ((here (point))
  2199. last) ; marks the position of non-ws code, what'll be BOS if, say, a
  2200. ; semicolon precedes it.
  2201. (catch 'done
  2202. (while t ;; We go back one "token" each iteration of the loop.
  2203. (setq last (point))
  2204. (cond
  2205. ;; Stop at the token after a comment.
  2206. ((c-backward-single-comment) ; Also functions as backwards-ws.
  2207. (goto-char last)
  2208. (throw 'done '(t . literal)))
  2209. ;; If we've gone back over a LF, we might have moved into or out of
  2210. ;; a preprocessor line.
  2211. ((and (save-excursion
  2212. (beginning-of-line)
  2213. (re-search-forward "\\(^\\|[^\\]\\)[\n\r]" last t))
  2214. (if macro-start
  2215. (< (point) macro-start)
  2216. (c-beginning-of-macro)))
  2217. (goto-char last)
  2218. ;; Return a car of NIL ONLY if we've hit the opening # of a macro.
  2219. (throw 'done (cons (or (eq (point) here)
  2220. (not macro-start))
  2221. 'macro-boundary)))
  2222. ;; Have we found a virtual semicolon? If so, stop, unless the next
  2223. ;; statement is where we started from.
  2224. ((and (c-at-vsemi-p)
  2225. (< last here)
  2226. (not (memq (char-after last) '(?\) ?})))) ; we've moved back from ) or }
  2227. (goto-char last)
  2228. (throw 'done '(nil . nil)))
  2229. ;; Hit the beginning of the buffer/region?
  2230. ((bobp)
  2231. (if (/= here last)
  2232. (goto-char last))
  2233. (throw 'done '(nil . nil)))
  2234. ;; Move back a character.
  2235. ((progn (backward-char) nil))
  2236. ;; Stop at "{" (unless it's a PIKE special brace list.)
  2237. ((eq (char-after) ?\{)
  2238. (if (and c-special-brace-lists
  2239. (c-looking-at-special-brace-list))
  2240. (skip-syntax-backward "w_") ; Speedup only.
  2241. (if (/= here last)
  2242. (goto-char last))
  2243. (throw 'done '(nil . nil))))
  2244. ;; Have we reached the start of a macro? This always counts as
  2245. ;; BOS. (N.B. I don't think (eq (point) here) can ever be true
  2246. ;; here. FIXME!!! ACM 2004/3/29)
  2247. ((and macro-start (eq (point) macro-start))
  2248. (throw 'done (cons (eq (point) here) 'macro-boundary)))
  2249. ;; Stop at token just after "}" or ";".
  2250. ((looking-at "[;}]")
  2251. ;; If we've gone back over ;, {, or }, we're done.
  2252. (if (or (= here last)
  2253. (memq (char-after last) '(?\) ?}))) ; we've moved back from ) or }
  2254. (if (and (eq (char-before) ?}) ; If };, treat them as a unit.
  2255. (eq (char-after) ?\;))
  2256. (backward-char))
  2257. (goto-char last) ; To the statement starting after the ; or }.
  2258. (throw 'done '(nil . nil))))
  2259. ;; Stop at the token after a string.
  2260. ((looking-at c-string-limit-regexp) ; Just gone back over a string terminator?
  2261. (goto-char last)
  2262. (throw 'done '(t . literal)))
  2263. ;; Nothing special: go back word characters.
  2264. (t (skip-syntax-backward "w_")) ; Speedup only.
  2265. ))))))
  2266. (defun c-forward-over-illiterals (macro-end allow-early-stop)
  2267. ;; Move forwards over code, stopping before reaching EOB or a literal
  2268. ;; (i.e. a comment/string) or the boundary of a preprocessor statement or
  2269. ;; the "end of a statement". MACRO-END is the position of the EOL/EOB which
  2270. ;; terminates the current preprocessor directive, or NIL if we're not in
  2271. ;; such.
  2272. ;;
  2273. ;; ALLOW-EARLY-STOP is non-nil if it is permissible to return without moving
  2274. ;; forward at all, should we encounter a `{'. This is an ugly kludge, but
  2275. ;; seems unavoidable. Depending on the context this function is called
  2276. ;; from, we _sometimes_ need to stop there. Currently (2004/4/3),
  2277. ;; ALLOW-EARLY-STOP is applied only to open braces, not to virtual
  2278. ;; semicolons, or anything else.
  2279. ;;
  2280. ;; Return a cons (A.B), where
  2281. ;; A is NIL if we moved forward to an EOS, or stay at one (when
  2282. ;; ALLOW-EARLY-STOP is set), T otherwise (we hit a literal).
  2283. ;; B is 'MACRO-BOUNDARY if we are about to cross the boundary out of or
  2284. ;; into a macro, otherwise 'LITERAL if we've hit a literal, otherwise NIL
  2285. ;;
  2286. ;; Point is left either after the end-of-statement, or at the last non-ws
  2287. ;; code before encountering the literal, or the # of the preprocessor
  2288. ;; statement, or at EOB [or just after last non-WS stuff??].
  2289. ;;
  2290. ;; As a clarification of "after the end-of-statement", if a comment or
  2291. ;; whitespace follows a completed AWK statement, that statement is treated
  2292. ;; as ending just after the last non-ws character before the comment.
  2293. ;;
  2294. ;; Note that this function moves within either preprocessor commands
  2295. ;; (macros) or normal code, but not both within the same invocation.
  2296. ;;
  2297. ;; Stop before `{', `}', and `#' when it's at boi on a line, but on the
  2298. ;; other side of the syntactic ws, and after `;', `}' and `};'. Only
  2299. ;; stop before `{' if at top level or inside braces, though. Move by
  2300. ;; sexps and move into parens. Also stop at eol of lines with `#' at
  2301. ;; the boi.
  2302. ;;
  2303. ;; This function might do hidden buffer changes.
  2304. (let ((here (point))
  2305. last)
  2306. (catch 'done
  2307. (while t ;; We go one "token" forward each time round this loop.
  2308. (setq last (point))
  2309. ;; If we've moved forward to a virtual semicolon, we're done.
  2310. (if (and (> last here) ; Should we check ALLOW-EARLY-STOP, here? 2004/4/3
  2311. (c-at-vsemi-p))
  2312. (throw 'done '(nil . nil)))
  2313. (c-skip-ws-forward)
  2314. (cond
  2315. ;; Gone past the end of a macro?
  2316. ((and macro-end (> (point) macro-end))
  2317. (goto-char last)
  2318. (throw 'done (cons (eq (point) here) 'macro-boundary)))
  2319. ;; About to hit a comment?
  2320. ((save-excursion (c-forward-single-comment))
  2321. (goto-char last)
  2322. (throw 'done '(t . literal)))
  2323. ;; End of buffer?
  2324. ((eobp)
  2325. (if (/= here last)
  2326. (goto-char last))
  2327. (throw 'done '(nil . nil)))
  2328. ;; If we encounter a '{', stop just after the previous token.
  2329. ((and (eq (char-after) ?{)
  2330. (not (and c-special-brace-lists
  2331. (c-looking-at-special-brace-list)))
  2332. (or allow-early-stop (/= here last))
  2333. (save-excursion ; Is this a check that we're NOT at top level?
  2334. ;;;; NO! This seems to check that (i) EITHER we're at the top level; OR (ii) The next enclosing
  2335. ;;;; level of bracketing is a '{'. HMM. Doesn't seem to make sense.
  2336. ;;;; 2003/8/8 This might have something to do with the GCC extension "Statement Expressions", e.g.
  2337. ;;;; while ({stmt1 ; stmt2 ; exp ;}). This form excludes such Statement Expressions.
  2338. (or (not (c-safe (up-list -1) t))
  2339. (= (char-after) ?{))))
  2340. (goto-char last)
  2341. (throw 'done '(nil . nil)))
  2342. ;; End of a PIKE special brace list? If so, step over it and continue.
  2343. ((and c-special-brace-lists
  2344. (eq (char-after) ?})
  2345. (save-excursion
  2346. (and (c-safe (up-list -1) t)
  2347. (c-looking-at-special-brace-list))))
  2348. (forward-char)
  2349. (skip-syntax-forward "w_")) ; Speedup only.
  2350. ;; Have we got a '}' after having moved? If so, stop after the
  2351. ;; previous token.
  2352. ((and (eq (char-after) ?})
  2353. (/= here last))
  2354. (goto-char last)
  2355. (throw 'done '(nil . nil)))
  2356. ;; Stop if we encounter a preprocessor line. Continue if we
  2357. ;; hit a naked #
  2358. ((and c-opt-cpp-prefix
  2359. (not macro-end)
  2360. (eq (char-after) ?#)
  2361. (= (point) (c-point 'boi)))
  2362. (if (= (point) here) ; Not a macro, therefore naked #.
  2363. (forward-char)
  2364. (throw 'done '(t . macro-boundary))))
  2365. ;; Stop after a ';', '}', or "};"
  2366. ((looking-at ";\\|};?")
  2367. (goto-char (match-end 0))
  2368. (throw 'done '(nil . nil)))
  2369. ;; Found a string (this subsumes AWK regexps)?
  2370. ((looking-at c-string-limit-regexp)
  2371. (goto-char last)
  2372. (throw 'done '(t . literal)))
  2373. (t
  2374. (forward-char) ; Can't fail - we checked (eobp) earlier on.
  2375. (skip-syntax-forward "w_") ; Speedup only.
  2376. (when (and macro-end (> (point) macro-end))
  2377. (goto-char last)
  2378. (throw 'done (cons (eq (point) here) 'macro-boundary))))
  2379. )))))
  2380. (defun c-one-line-string-p (range)
  2381. ;; Is the literal defined by RANGE a string contained in a single line?
  2382. ;;
  2383. ;; This function might do hidden buffer changes.
  2384. (save-excursion
  2385. (goto-char (car range))
  2386. (and (looking-at c-string-limit-regexp)
  2387. (progn (skip-chars-forward "^\n" (cdr range))
  2388. (eq (point) (cdr range))))))
  2389. (defun c-beginning-of-statement (&optional count lim sentence-flag)
  2390. "Go to the beginning of the innermost C statement.
  2391. With prefix arg, go back N - 1 statements. If already at the
  2392. beginning of a statement then go to the beginning of the closest
  2393. preceding one, moving into nested blocks if necessary (use
  2394. \\[backward-sexp] to skip over a block). If within or next to a
  2395. comment or multiline string, move by sentences instead of statements.
  2396. When called from a program, this function takes 3 optional args: the
  2397. repetition count, a buffer position limit which is the farthest back
  2398. to search for the syntactic context, and a flag saying whether to do
  2399. sentence motion in or near comments and multiline strings.
  2400. Note that for use in programs, `c-beginning-of-statement-1' is
  2401. usually better. It has much better defined semantics than this one,
  2402. which is intended for interactive use, and might therefore change to
  2403. be more \"DWIM:ey\"."
  2404. (interactive (list (prefix-numeric-value current-prefix-arg)
  2405. nil t))
  2406. (if (< count 0)
  2407. (c-end-of-statement (- count) lim sentence-flag)
  2408. (c-save-buffer-state
  2409. ((count (or count 1))
  2410. last ; start point for going back ONE chunk. Updated each chunk movement.
  2411. (macro-fence
  2412. (save-excursion (and (not (bobp)) (c-beginning-of-macro) (point))))
  2413. res ; result from sub-function call
  2414. not-bos ; "not beginning-of-statement"
  2415. (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
  2416. ;; Go back one statement at each iteration of the following loop.
  2417. (while (and (/= count 0)
  2418. (or (not lim) (> (point) lim)))
  2419. ;; Go back one "chunk" each time round the following loop, stopping
  2420. ;; when we reach a statement boundary, etc.
  2421. (setq last (point))
  2422. (while
  2423. (cond ; Each arm of this cond returns NIL on reaching a desired
  2424. ; statement boundary, non-NIL otherwise.
  2425. ((bobp)
  2426. (setq count 0)
  2427. nil)
  2428. (range ; point is within or approaching a literal.
  2429. (cond
  2430. ;; Single line string or sentence-flag is null => skip the
  2431. ;; entire literal.
  2432. ((or (null sentence-flag)
  2433. (c-one-line-string-p range))
  2434. (goto-char (car range))
  2435. (setq range (c-ascertain-preceding-literal))
  2436. ;; N.B. The following is essentially testing for an AWK regexp
  2437. ;; at BOS:
  2438. ;; Was the previous non-ws thing an end of statement?
  2439. (save-excursion
  2440. (if macro-fence
  2441. (c-backward-comments)
  2442. (c-backward-syntactic-ws))
  2443. (not (or (bobp) (c-after-statement-terminator-p)))))
  2444. ;; Comment inside a statement or a multi-line string.
  2445. (t (when (setq res ; returns non-nil when we go out of the literal
  2446. (if (eq (c-literal-type range) 'string)
  2447. (c-beginning-of-sentence-in-string range)
  2448. (c-beginning-of-sentence-in-comment range)))
  2449. (setq range (c-ascertain-preceding-literal)))
  2450. res)))
  2451. ;; Non-literal code.
  2452. (t (setq res (c-back-over-illiterals macro-fence))
  2453. (setq not-bos ; "not reached beginning-of-statement".
  2454. (or (= (point) last)
  2455. (memq (char-after) '(?\) ?\}))
  2456. (and
  2457. (car res)
  2458. ;; We're at a tentative BOS. The next form goes
  2459. ;; back over WS looking for an end of previous
  2460. ;; statement.
  2461. (not (save-excursion
  2462. (if macro-fence
  2463. (c-backward-comments)
  2464. (c-backward-syntactic-ws))
  2465. (or (bobp) (c-after-statement-terminator-p)))))))
  2466. ;; Are we about to move backwards into or out of a
  2467. ;; preprocessor command? If so, locate its beginning.
  2468. (when (eq (cdr res) 'macro-boundary)
  2469. (save-excursion
  2470. (beginning-of-line)
  2471. (setq macro-fence
  2472. (and (not (bobp))
  2473. (progn (c-skip-ws-backward) (c-beginning-of-macro))
  2474. (point)))))
  2475. ;; Are we about to move backwards into a literal?
  2476. (when (memq (cdr res) '(macro-boundary literal))
  2477. (setq range (c-ascertain-preceding-literal)))
  2478. not-bos))
  2479. (setq last (point)))
  2480. (if (/= count 0) (setq count (1- count))))
  2481. (c-keep-region-active))))
  2482. (defun c-end-of-statement (&optional count lim sentence-flag)
  2483. "Go to the end of the innermost C statement.
  2484. With prefix arg, go forward N - 1 statements. Move forward to the end
  2485. of the next statement if already at end, and move into nested blocks
  2486. \(use \\[forward-sexp] to skip over a block). If within or next to a
  2487. comment or multiline string, move by sentences instead of statements.
  2488. When called from a program, this function takes 3 optional args: the
  2489. repetition count, a buffer position limit which is the farthest back
  2490. to search for the syntactic context, and a flag saying whether to do
  2491. sentence motion in or near comments and multiline strings."
  2492. (interactive (list (prefix-numeric-value current-prefix-arg)
  2493. nil t))
  2494. (setq count (or count 1))
  2495. (if (< count 0) (c-beginning-of-statement (- count) lim sentence-flag)
  2496. (c-save-buffer-state
  2497. (here ; start point for going forward ONE statement. Updated each statement.
  2498. (macro-fence
  2499. (save-excursion
  2500. (and (not (eobp)) (c-beginning-of-macro)
  2501. (progn (c-end-of-macro) (point)))))
  2502. res
  2503. (range (c-collect-line-comments (c-literal-limits lim)))) ; (start.end) of current literal or NIL
  2504. ;; Go back/forward one statement at each iteration of the following loop.
  2505. (while (and (/= count 0)
  2506. (or (not lim) (< (point) lim)))
  2507. (setq here (point)) ; ONLY HERE is HERE updated
  2508. ;; Go forward one "chunk" each time round the following loop, stopping
  2509. ;; when we reach a statement boundary, etc.
  2510. (while
  2511. (cond ; Each arm of this cond returns NIL on reaching a desired
  2512. ; statement boundary, non-NIL otherwise.
  2513. ((eobp)
  2514. (setq count 0)
  2515. nil)
  2516. (range ; point is within a literal.
  2517. (cond
  2518. ;; sentence-flag is null => skip the entire literal.
  2519. ;; or a Single line string.
  2520. ((or (null sentence-flag)
  2521. (c-one-line-string-p range))
  2522. (goto-char (cdr range))
  2523. (setq range (c-ascertain-following-literal))
  2524. ;; Is there a virtual semicolon here (e.g. for AWK)?
  2525. (not (c-at-vsemi-p)))
  2526. ;; Comment or multi-line string.
  2527. (t (when (setq res ; gets non-nil when we go out of the literal
  2528. (if (eq (c-literal-type range) 'string)
  2529. (c-end-of-sentence-in-string range)
  2530. (c-end-of-sentence-in-comment range)))
  2531. (setq range (c-ascertain-following-literal)))
  2532. ;; If we've just come forward out of a literal, check for
  2533. ;; vsemi. (N.B. AWK can't have a vsemi after a comment, but
  2534. ;; some other language may do in the future)
  2535. (and res
  2536. (not (c-at-vsemi-p))))))
  2537. ;; Non-literal code.
  2538. (t (setq res (c-forward-over-illiterals macro-fence
  2539. (> (point) here)))
  2540. ;; Are we about to move forward into or out of a
  2541. ;; preprocessor command?
  2542. (when (eq (cdr res) 'macro-boundary)
  2543. (setq macro-fence
  2544. (save-excursion
  2545. (if macro-fence
  2546. (progn
  2547. (end-of-line)
  2548. (and (not (eobp))
  2549. (progn (c-skip-ws-forward)
  2550. (c-beginning-of-macro))
  2551. (progn (c-end-of-macro)
  2552. (point))))
  2553. (and (not (eobp))
  2554. (c-beginning-of-macro)
  2555. (progn (c-end-of-macro) (point)))))))
  2556. ;; Are we about to move forward into a literal?
  2557. (when (memq (cdr res) '(macro-boundary literal))
  2558. (setq range (c-ascertain-following-literal)))
  2559. (car res))))
  2560. (if (/= count 0) (setq count (1- count))))
  2561. (c-keep-region-active))))
  2562. ;; set up electric character functions to work with pending-del,
  2563. ;; (a.k.a. delsel) mode. All symbols get the t value except
  2564. ;; the functions which delete, which gets 'supersede.
  2565. (mapc
  2566. (function
  2567. (lambda (sym)
  2568. (put sym 'delete-selection t) ; for delsel (Emacs)
  2569. (put sym 'pending-delete t))) ; for pending-del (XEmacs)
  2570. '(c-electric-pound
  2571. c-electric-brace
  2572. c-electric-slash
  2573. c-electric-star
  2574. c-electric-semi&comma
  2575. c-electric-lt-gt
  2576. c-electric-colon
  2577. c-electric-paren))
  2578. (put 'c-electric-delete 'delete-selection 'supersede) ; delsel
  2579. (put 'c-electric-delete 'pending-delete 'supersede) ; pending-del
  2580. (put 'c-electric-backspace 'delete-selection 'supersede) ; delsel
  2581. (put 'c-electric-backspace 'pending-delete 'supersede) ; pending-del
  2582. (put 'c-electric-delete-forward 'delete-selection 'supersede) ; delsel
  2583. (put 'c-electric-delete-forward 'pending-delete 'supersede) ; pending-del
  2584. ;; Inserting/indenting comments
  2585. (defun c-calc-comment-indent (entry)
  2586. ;; This function might do hidden buffer changes.
  2587. (if (symbolp entry)
  2588. (setq entry (or (assq entry c-indent-comment-alist)
  2589. (assq 'other c-indent-comment-alist)
  2590. '(default . (column . nil)))))
  2591. (let ((action (car (cdr entry)))
  2592. (value (cdr (cdr entry)))
  2593. (col (current-column)))
  2594. (cond ((eq action 'space)
  2595. (+ col value))
  2596. ((eq action 'column)
  2597. (unless value (setq value comment-column))
  2598. (if (bolp)
  2599. ;; Do not pad with one space if we're at bol.
  2600. value
  2601. (max (1+ col) value)))
  2602. ((eq action 'align)
  2603. (or (save-excursion
  2604. (beginning-of-line)
  2605. (unless (bobp)
  2606. (backward-char)
  2607. (let ((lim (c-literal-limits (c-point 'bol) t)))
  2608. (when (consp lim)
  2609. (goto-char (car lim))
  2610. (when (looking-at "/[/*]") ; FIXME!!! Adapt for AWK! (ACM, 2005/11/18)
  2611. ;; Found comment to align with.
  2612. (if (bolp)
  2613. ;; Do not pad with one space if we're at bol.
  2614. 0
  2615. (max (1+ col) (current-column))))))))
  2616. ;; Recurse to handle value as a new spec.
  2617. (c-calc-comment-indent (cdr entry)))))))
  2618. (defun c-comment-indent ()
  2619. "Used by `indent-for-comment' to create and indent comments.
  2620. See `c-indent-comment-alist' for a description."
  2621. (save-excursion
  2622. (end-of-line)
  2623. (c-save-buffer-state
  2624. ((eot (let ((lim (c-literal-limits (c-point 'bol) t)))
  2625. (or (when (consp lim)
  2626. (goto-char (car lim))
  2627. (when (looking-at "/[/*]")
  2628. (skip-chars-backward " \t")
  2629. (point)))
  2630. (progn
  2631. (skip-chars-backward " \t")
  2632. (point)))))
  2633. (line-type
  2634. (cond ((looking-at "^/[/*]")
  2635. 'anchored-comment)
  2636. ((progn (beginning-of-line)
  2637. (eq (point) eot))
  2638. 'empty-line)
  2639. ((progn (back-to-indentation)
  2640. (and (eq (char-after) ?})
  2641. (eq (point) (1- eot))))
  2642. 'end-block)
  2643. ((and (looking-at "#[ \t]*\\(endif\\|else\\)")
  2644. (eq (match-end 0) eot))
  2645. 'cpp-end-block)
  2646. (t
  2647. 'other)))
  2648. case-fold-search)
  2649. (if (and (memq line-type '(anchored-comment empty-line))
  2650. c-indent-comments-syntactically-p)
  2651. (let ((c-syntactic-context (c-guess-basic-syntax)))
  2652. ;; BOGOSITY ALERT: if we're looking at the eol, its
  2653. ;; because indent-for-comment hasn't put the comment-start
  2654. ;; in the buffer yet. this will screw up the syntactic
  2655. ;; analysis so we kludge in the necessary info. Another
  2656. ;; kludge is that if we're at the bol, then we really want
  2657. ;; to ignore any anchoring as specified by
  2658. ;; c-comment-only-line-offset since it doesn't apply here.
  2659. (if (eolp)
  2660. (c-add-syntax 'comment-intro))
  2661. (let ((c-comment-only-line-offset
  2662. (if (consp c-comment-only-line-offset)
  2663. c-comment-only-line-offset
  2664. (cons c-comment-only-line-offset
  2665. c-comment-only-line-offset))))
  2666. (c-get-syntactic-indentation c-syntactic-context)))
  2667. (goto-char eot)
  2668. (c-calc-comment-indent line-type)))))
  2669. ;; used by outline-minor-mode
  2670. (defun c-outline-level ()
  2671. (let (buffer-invisibility-spec);; This so that `current-column' DTRT
  2672. ;; in otherwise-hidden text.
  2673. (save-excursion
  2674. (skip-chars-forward "\t ")
  2675. (current-column))))
  2676. ;; Movement by CPP conditionals.
  2677. (defun c-up-conditional (count)
  2678. "Move back to the containing preprocessor conditional, leaving mark behind.
  2679. A prefix argument acts as a repeat count. With a negative argument,
  2680. move forward to the end of the containing preprocessor conditional.
  2681. \"#elif\" is treated like \"#else\" followed by \"#if\", so the
  2682. function stops at them when going backward, but not when going
  2683. forward."
  2684. (interactive "p")
  2685. (let ((new-point (c-scan-conditionals (- count) -1)))
  2686. (push-mark)
  2687. (goto-char new-point))
  2688. (c-keep-region-active))
  2689. (defun c-up-conditional-with-else (count)
  2690. "Move back to the containing preprocessor conditional, including \"#else\".
  2691. Just like `c-up-conditional', except it also stops at \"#else\"
  2692. directives."
  2693. (interactive "p")
  2694. (let ((new-point (c-scan-conditionals (- count) -1 t)))
  2695. (push-mark)
  2696. (goto-char new-point))
  2697. (c-keep-region-active))
  2698. (defun c-down-conditional (count)
  2699. "Move forward into the next preprocessor conditional, leaving mark behind.
  2700. A prefix argument acts as a repeat count. With a negative argument,
  2701. move backward into the previous preprocessor conditional.
  2702. \"#elif\" is treated like \"#else\" followed by \"#if\", so the
  2703. function stops at them when going forward, but not when going
  2704. backward."
  2705. (interactive "p")
  2706. (let ((new-point (c-scan-conditionals count 1)))
  2707. (push-mark)
  2708. (goto-char new-point))
  2709. (c-keep-region-active))
  2710. (defun c-down-conditional-with-else (count)
  2711. "Move forward into the next preprocessor conditional, including \"#else\".
  2712. Just like `c-down-conditional', except it also stops at \"#else\"
  2713. directives."
  2714. (interactive "p")
  2715. (let ((new-point (c-scan-conditionals count 1 t)))
  2716. (push-mark)
  2717. (goto-char new-point))
  2718. (c-keep-region-active))
  2719. (defun c-backward-conditional (count &optional target-depth with-else)
  2720. "Move back across a preprocessor conditional, leaving mark behind.
  2721. A prefix argument acts as a repeat count. With a negative argument,
  2722. move forward across a preprocessor conditional.
  2723. The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
  2724. and have the same meanings as in `c-scan-conditionals'. If you
  2725. are calling c-forward-conditional from a program, you might want
  2726. to call `c-scan-conditionals' directly instead."
  2727. (interactive "p")
  2728. (let ((new-point (c-scan-conditionals (- count) target-depth with-else)))
  2729. (push-mark)
  2730. (goto-char new-point))
  2731. (c-keep-region-active))
  2732. (defun c-forward-conditional (count &optional target-depth with-else)
  2733. "Move forward across a preprocessor conditional, leaving mark behind.
  2734. A prefix argument acts as a repeat count. With a negative argument,
  2735. move backward across a preprocessor conditional.
  2736. If there aren't enough conditionals after \(or before) point, an
  2737. error is signaled.
  2738. \"#elif\" is treated like \"#else\" followed by \"#if\", except that
  2739. the nesting level isn't changed when tracking subconditionals.
  2740. The optional arguments TARGET-DEPTH and WITH-ELSE are historical,
  2741. and have the same meanings as in `c-scan-conditionals'. If you
  2742. are calling c-forward-conditional from a program, you might want
  2743. to call `c-scan-conditionals' directly instead."
  2744. (interactive "p")
  2745. (let ((new-point (c-scan-conditionals count target-depth with-else)))
  2746. (push-mark)
  2747. (goto-char new-point)))
  2748. (defun c-scan-conditionals (count &optional target-depth with-else)
  2749. "Scan forward across COUNT preprocessor conditionals.
  2750. With a negative argument, scan backward across preprocessor
  2751. conditionals. Return the end position. Point is not moved.
  2752. If there aren't enough preprocessor conditionals, throw an error.
  2753. \"#elif\" is treated like \"#else\" followed by \"#if\", except that
  2754. the nesting level isn't changed when tracking subconditionals.
  2755. The optional argument TARGET-DEPTH specifies the wanted nesting depth
  2756. after each scan. E.g. if TARGET-DEPTH is -1, the end position will be
  2757. outside the enclosing conditional. A non-integer non-nil TARGET-DEPTH
  2758. counts as -1.
  2759. If the optional argument WITH-ELSE is non-nil, \"#else\" directives
  2760. are treated as conditional clause limits. Normally they are ignored."
  2761. (let* ((forward (> count 0))
  2762. (increment (if forward -1 1))
  2763. (search-function (if forward 're-search-forward 're-search-backward))
  2764. new case-fold-search)
  2765. (unless (integerp target-depth)
  2766. (setq target-depth (if target-depth -1 0)))
  2767. (save-excursion
  2768. (while (/= count 0)
  2769. (let ((depth 0)
  2770. ;; subdepth is the depth in "uninteresting" subtrees,
  2771. ;; i.e. those that takes us farther from the target
  2772. ;; depth instead of closer.
  2773. (subdepth 0)
  2774. found)
  2775. (save-excursion
  2776. ;; Find the "next" significant line in the proper direction.
  2777. (while (and (not found)
  2778. ;; Rather than searching for a # sign that
  2779. ;; comes at the beginning of a line aside from
  2780. ;; whitespace, search first for a string
  2781. ;; starting with # sign. Then verify what
  2782. ;; precedes it. This is faster on account of
  2783. ;; the fastmap feature of the regexp matcher.
  2784. (funcall search-function
  2785. "#[ \t]*\\(if\\|elif\\|endif\\|else\\)"
  2786. nil t))
  2787. (beginning-of-line)
  2788. ;; Now verify it is really a preproc line.
  2789. (if (looking-at "^[ \t]*#[ \t]*\\(if\\|elif\\|endif\\|else\\)")
  2790. (let (dchange (directive (match-string 1)))
  2791. (cond ((string= directive "if")
  2792. (setq dchange (- increment)))
  2793. ((string= directive "endif")
  2794. (setq dchange increment))
  2795. ((= subdepth 0)
  2796. ;; When we're not in an "uninteresting"
  2797. ;; subtree, we might want to act on "elif"
  2798. ;; and "else" too.
  2799. (if (cond (with-else
  2800. ;; Always move toward the target depth.
  2801. (setq dchange
  2802. (if (> target-depth 0) 1 -1)))
  2803. ((string= directive "elif")
  2804. (setq dchange (- increment))))
  2805. ;; Ignore the change if it'd take us
  2806. ;; into an "uninteresting" subtree.
  2807. (if (eq (> dchange 0) (<= target-depth 0))
  2808. (setq dchange nil)))))
  2809. (when dchange
  2810. (when (or (/= subdepth 0)
  2811. (eq (> dchange 0) (<= target-depth 0)))
  2812. (setq subdepth (+ subdepth dchange)))
  2813. (setq depth (+ depth dchange))
  2814. ;; If we are trying to move across, and we find an
  2815. ;; end before we find a beginning, get an error.
  2816. (if (and (< depth target-depth) (< dchange 0))
  2817. (error (if forward
  2818. "No following conditional at this level"
  2819. "No previous conditional at this level"))))
  2820. ;; When searching forward, start from next line so
  2821. ;; that we don't find the same line again.
  2822. (if forward (forward-line 1))
  2823. ;; We found something if we've arrived at the
  2824. ;; target depth.
  2825. (if (and dchange (= depth target-depth))
  2826. (setq found (point))))
  2827. ;; else
  2828. (if forward (forward-line 1)))))
  2829. (or found
  2830. (error "No containing preprocessor conditional"))
  2831. (goto-char (setq new found)))
  2832. (setq count (+ count increment))))
  2833. (c-keep-region-active)
  2834. new))
  2835. ;; commands to indent lines, regions, defuns, and expressions
  2836. (defun c-indent-command (&optional arg)
  2837. "Indent current line as C code, and/or insert some whitespace.
  2838. If `c-tab-always-indent' is t, always just indent the current line.
  2839. If nil, indent the current line only if point is at the left margin or
  2840. in the line's indentation; otherwise insert some whitespace[*]. If
  2841. other than nil or t, then some whitespace[*] is inserted only within
  2842. literals (comments and strings), but the line is always reindented.
  2843. If `c-syntactic-indentation' is t, indentation is done according to
  2844. the syntactic context. A numeric argument, regardless of its value,
  2845. means indent rigidly all the lines of the expression starting after
  2846. point so that this line becomes properly indented. The relative
  2847. indentation among the lines of the expression is preserved.
  2848. If `c-syntactic-indentation' is nil, the line is just indented one
  2849. step according to `c-basic-offset'. In this mode, a numeric argument
  2850. indents a number of such steps, positive or negative, and an empty
  2851. prefix argument is equivalent to -1.
  2852. [*] The amount and kind of whitespace inserted is controlled by the
  2853. variable `c-insert-tab-function', which is called to do the actual
  2854. insertion of whitespace. Normally the function in this variable
  2855. just inserts a tab character, or the equivalent number of spaces,
  2856. depending on the variable `indent-tabs-mode'."
  2857. (interactive "P")
  2858. (let ((indent-function
  2859. (if c-syntactic-indentation
  2860. (symbol-function 'indent-according-to-mode)
  2861. (lambda ()
  2862. (let ((c-macro-start c-macro-start)
  2863. (steps (if (equal arg '(4))
  2864. -1
  2865. (prefix-numeric-value arg))))
  2866. (c-shift-line-indentation (* steps c-basic-offset))
  2867. (when (and c-auto-align-backslashes
  2868. (save-excursion
  2869. (end-of-line)
  2870. (eq (char-before) ?\\))
  2871. (c-query-and-set-macro-start))
  2872. ;; Realign the line continuation backslash if inside a macro.
  2873. (c-backslash-region (point) (point) nil t)))
  2874. ))))
  2875. (if (and c-syntactic-indentation arg)
  2876. ;; If c-syntactic-indentation and got arg, always indent this
  2877. ;; line as C and shift remaining lines of expression the same
  2878. ;; amount.
  2879. (let ((shift-amt (save-excursion
  2880. (back-to-indentation)
  2881. (current-column)))
  2882. beg end)
  2883. (c-indent-line)
  2884. (setq shift-amt (- (save-excursion
  2885. (back-to-indentation)
  2886. (current-column))
  2887. shift-amt))
  2888. (save-excursion
  2889. (if (eq c-tab-always-indent t)
  2890. (beginning-of-line)) ; FIXME!!! What is this here for? ACM 2005/10/31
  2891. (setq beg (point))
  2892. (c-forward-sexp 1)
  2893. (setq end (point))
  2894. (goto-char beg)
  2895. (forward-line 1)
  2896. (setq beg (point)))
  2897. (if (> end beg)
  2898. (indent-code-rigidly beg end shift-amt "#")))
  2899. ;; Else use c-tab-always-indent to determine behavior.
  2900. (cond
  2901. ;; CASE 1: indent when at column zero or in line's indentation,
  2902. ;; otherwise insert a tab
  2903. ((not c-tab-always-indent)
  2904. (if (save-excursion
  2905. (skip-chars-backward " \t")
  2906. (not (bolp)))
  2907. (funcall c-insert-tab-function)
  2908. (funcall indent-function)))
  2909. ;; CASE 2: just indent the line
  2910. ((eq c-tab-always-indent t)
  2911. (funcall indent-function))
  2912. ;; CASE 3: if in a literal, insert a tab, but always indent the
  2913. ;; line
  2914. (t
  2915. (if (c-save-buffer-state () (c-in-literal))
  2916. (funcall c-insert-tab-function))
  2917. (funcall indent-function)
  2918. )))))
  2919. (defun c-indent-exp (&optional shutup-p)
  2920. "Indent each line in the balanced expression following point syntactically.
  2921. If optional SHUTUP-P is non-nil, no errors are signaled if no
  2922. balanced expression is found."
  2923. (interactive "*P")
  2924. (let ((here (point-marker))
  2925. end)
  2926. (set-marker-insertion-type here t)
  2927. (unwind-protect
  2928. (let ((start (save-restriction
  2929. ;; Find the closest following open paren that
  2930. ;; ends on another line.
  2931. (narrow-to-region (point-min) (c-point 'eol))
  2932. (let (beg (end (point)))
  2933. (while (and (setq beg (c-down-list-forward end))
  2934. (setq end (c-up-list-forward beg))))
  2935. (and beg
  2936. (eq (char-syntax (char-before beg)) ?\()
  2937. (1- beg))))))
  2938. ;; sanity check
  2939. (if (not start)
  2940. (unless shutup-p
  2941. (error "Cannot find start of balanced expression to indent"))
  2942. (goto-char start)
  2943. (setq end (c-safe (scan-sexps (point) 1)))
  2944. (if (not end)
  2945. (unless shutup-p
  2946. (error "Cannot find end of balanced expression to indent"))
  2947. (forward-line)
  2948. (if (< (point) end)
  2949. (c-indent-region (point) end)))))
  2950. (goto-char here)
  2951. (set-marker here nil))))
  2952. (defun c-indent-defun ()
  2953. "Indent the current top-level declaration or macro syntactically.
  2954. In the macro case this also has the effect of realigning any line
  2955. continuation backslashes, unless `c-auto-align-backslashes' is nil."
  2956. (interactive "*")
  2957. (let ((here (point-marker)) decl-limits case-fold-search)
  2958. (unwind-protect
  2959. (progn
  2960. (c-save-buffer-state nil
  2961. ;; We try to be line oriented, unless there are several
  2962. ;; declarations on the same line.
  2963. (if (looking-at c-syntactic-eol)
  2964. (c-backward-token-2 1 nil (c-point 'bol))
  2965. (c-forward-token-2 0 nil (c-point 'eol)))
  2966. (setq decl-limits (c-declaration-limits nil)))
  2967. (if decl-limits
  2968. (c-indent-region (car decl-limits)
  2969. (cdr decl-limits))))
  2970. (goto-char here)
  2971. (set-marker here nil))))
  2972. (defun c-indent-region (start end &optional quiet)
  2973. "Indent syntactically every line whose first char is between START
  2974. and END inclusive. If the optional argument QUIET is non-nil then no
  2975. syntactic errors are reported, even if `c-report-syntactic-errors' is
  2976. non-nil."
  2977. (save-excursion
  2978. (goto-char end)
  2979. (skip-chars-backward " \t\n\r\f\v")
  2980. (setq end (point))
  2981. (goto-char start)
  2982. ;; Advance to first nonblank line.
  2983. (beginning-of-line)
  2984. (skip-chars-forward " \t\n\r\f\v")
  2985. (setq start (point))
  2986. (beginning-of-line)
  2987. (setq c-parsing-error
  2988. (or (let ((endmark (copy-marker end))
  2989. (c-parsing-error nil)
  2990. ;; shut up any echo msgs on indiv lines
  2991. (c-echo-syntactic-information-p nil)
  2992. (ml-macro-start ; Start pos of multi-line macro.
  2993. (and (c-save-buffer-state ()
  2994. (save-excursion (c-beginning-of-macro)))
  2995. (eq (char-before (c-point 'eol)) ?\\)
  2996. start))
  2997. (c-fix-backslashes nil)
  2998. syntax)
  2999. (unwind-protect
  3000. (progn
  3001. (c-progress-init start end 'c-indent-region)
  3002. (while (and (bolp) ;; One line each time round the loop.
  3003. (not (eobp))
  3004. (< (point) endmark))
  3005. ;; update progress
  3006. (c-progress-update)
  3007. ;; skip empty lines
  3008. (unless (or (looking-at "\\s *$")
  3009. (and ml-macro-start (looking-at "\\s *\\\\$")))
  3010. ;; Get syntax and indent.
  3011. (c-save-buffer-state nil
  3012. (setq syntax (c-guess-basic-syntax)))
  3013. (c-indent-line syntax t t))
  3014. (if ml-macro-start
  3015. ;; End of current multi-line macro?
  3016. (when (and c-auto-align-backslashes
  3017. (not (eq (char-before (c-point 'eol)) ?\\)))
  3018. ;; Fixup macro backslashes.
  3019. (c-backslash-region ml-macro-start (c-point 'bonl) nil)
  3020. (setq ml-macro-start nil))
  3021. ;; New multi-line macro?
  3022. (if (and (assq 'cpp-macro syntax)
  3023. (eq (char-before (c-point 'eol)) ?\\))
  3024. (setq ml-macro-start (point))))
  3025. (forward-line))
  3026. (if (and ml-macro-start c-auto-align-backslashes)
  3027. (c-backslash-region ml-macro-start (c-point 'bopl) nil t)))
  3028. (set-marker endmark nil)
  3029. (c-progress-fini 'c-indent-region))
  3030. (c-echo-parsing-error quiet))
  3031. c-parsing-error))))
  3032. (defun c-fn-region-is-active-p ()
  3033. ;; Function version of the macro for use in places that aren't
  3034. ;; compiled, e.g. in the menus.
  3035. (c-region-is-active-p))
  3036. (defun c-indent-line-or-region (&optional arg region)
  3037. "Indent active region, current line, or block starting on this line.
  3038. In Transient Mark mode, when the region is active, reindent the region.
  3039. Otherwise, with a prefix argument, rigidly reindent the expression
  3040. starting on the current line.
  3041. Otherwise reindent just the current line."
  3042. (interactive
  3043. (list current-prefix-arg (use-region-p)))
  3044. (if region
  3045. (c-indent-region (region-beginning) (region-end))
  3046. (c-indent-command arg)))
  3047. ;; for progress reporting
  3048. (defvar c-progress-info nil)
  3049. (defun c-progress-init (start end context)
  3050. (cond
  3051. ;; Be silent
  3052. ((not c-progress-interval))
  3053. ;; Start the progress update messages. If this Emacs doesn't have
  3054. ;; a built-in timer, just be dumb about it.
  3055. ((not (fboundp 'current-time))
  3056. (message "Indenting region... (this may take a while)"))
  3057. ;; If progress has already been initialized, do nothing. otherwise
  3058. ;; initialize the counter with a vector of:
  3059. ;; [start end lastsec context]
  3060. (c-progress-info)
  3061. (t (setq c-progress-info (vector start
  3062. (save-excursion
  3063. (goto-char end)
  3064. (point-marker))
  3065. (nth 1 (current-time))
  3066. context))
  3067. (message "Indenting region..."))
  3068. ))
  3069. (defun c-progress-update ()
  3070. (if (not (and c-progress-info c-progress-interval))
  3071. nil
  3072. (let ((now (nth 1 (current-time)))
  3073. (start (aref c-progress-info 0))
  3074. (end (aref c-progress-info 1))
  3075. (lastsecs (aref c-progress-info 2)))
  3076. ;; should we update? currently, update happens every 2 seconds,
  3077. ;; what's the right value?
  3078. (if (< c-progress-interval (- now lastsecs))
  3079. (progn
  3080. (message "Indenting region... (%d%% complete)"
  3081. (/ (* 100 (- (point) start)) (- end start)))
  3082. (aset c-progress-info 2 now)))
  3083. )))
  3084. (defun c-progress-fini (context)
  3085. (if (not c-progress-interval)
  3086. nil
  3087. (if (or (eq context (aref c-progress-info 3))
  3088. (eq context t))
  3089. (progn
  3090. (set-marker (aref c-progress-info 1) nil)
  3091. (setq c-progress-info nil)
  3092. (message "Indenting region... done")))))
  3093. ;;; This page handles insertion and removal of backslashes for C macros.
  3094. (defun c-backslash-region (from to delete-flag &optional line-mode)
  3095. "Insert, align, or delete end-of-line backslashes on the lines in the region.
  3096. With no argument, inserts backslashes and aligns existing backslashes.
  3097. With an argument, deletes the backslashes. The backslash alignment is
  3098. done according to the settings in `c-backslash-column',
  3099. `c-backslash-max-column' and `c-auto-align-backslashes'.
  3100. This function does not modify blank lines at the start of the region.
  3101. If the region ends at the start of a line and the macro doesn't
  3102. continue below it, the backslash (if any) at the end of the previous
  3103. line is deleted.
  3104. You can put the region around an entire macro definition and use this
  3105. command to conveniently insert and align the necessary backslashes."
  3106. (interactive "*r\nP")
  3107. (let ((endmark (make-marker))
  3108. ;; Keep the backslash trimming functions from changing the
  3109. ;; whitespace around point, since in this case it's only the
  3110. ;; position of point that tells the indentation of the line.
  3111. (point-pos (if (save-excursion
  3112. (skip-chars-backward " \t")
  3113. (and (bolp) (looking-at "[ \t]*\\\\?$")))
  3114. (point-marker)
  3115. (point-min)))
  3116. column longest-line-col bs-col-after-end)
  3117. (save-excursion
  3118. (goto-char to)
  3119. (if (and (not line-mode) (bobp))
  3120. ;; Nothing to do if to is at bob, since we should back up
  3121. ;; and there's no line to back up to.
  3122. nil
  3123. (when (and (not line-mode) (bolp))
  3124. ;; Do not back up the to line if line-mode is set, to make
  3125. ;; e.g. c-newline-and-indent consistent regardless whether
  3126. ;; the (newline) call leaves point at bol or not.
  3127. (backward-char)
  3128. (setq to (point)))
  3129. (if delete-flag
  3130. (progn
  3131. (set-marker endmark (point))
  3132. (goto-char from)
  3133. (c-delete-backslashes-forward endmark point-pos))
  3134. ;; Set bs-col-after-end to the column of any backslash
  3135. ;; following the region, or nil if there is none.
  3136. (setq bs-col-after-end
  3137. (and (progn (end-of-line)
  3138. (eq (char-before) ?\\))
  3139. (= (forward-line 1) 0)
  3140. (progn (end-of-line)
  3141. (eq (char-before) ?\\))
  3142. (1- (current-column))))
  3143. (when line-mode
  3144. ;; Back up the to line if line-mode is set, since the line
  3145. ;; after the newly inserted line break should not be
  3146. ;; touched in c-newline-and-indent.
  3147. (setq to (max from (or (c-safe (c-point 'eopl)) from)))
  3148. (unless bs-col-after-end
  3149. ;; Set bs-col-after-end to non-nil in any case, since we
  3150. ;; do not want to delete the backslash at the last line.
  3151. (setq bs-col-after-end t)))
  3152. (if (and line-mode
  3153. (not c-auto-align-backslashes))
  3154. (goto-char from)
  3155. ;; Compute the smallest column number past the ends of all
  3156. ;; the lines.
  3157. (setq longest-line-col 0)
  3158. (goto-char to)
  3159. (if bs-col-after-end
  3160. ;; Include one more line in the max column
  3161. ;; calculation, since the to line will be backslashed
  3162. ;; too.
  3163. (forward-line 1))
  3164. (end-of-line)
  3165. (while (and (>= (point) from)
  3166. (progn
  3167. (if (eq (char-before) ?\\)
  3168. (forward-char -1))
  3169. (skip-chars-backward " \t")
  3170. (setq longest-line-col (max longest-line-col
  3171. (1+ (current-column))))
  3172. (beginning-of-line)
  3173. (not (bobp))))
  3174. (backward-char))
  3175. ;; Try to align with surrounding backslashes.
  3176. (goto-char from)
  3177. (beginning-of-line)
  3178. (if (and (not (bobp))
  3179. (progn (backward-char)
  3180. (eq (char-before) ?\\)))
  3181. (progn
  3182. (setq column (1- (current-column)))
  3183. (if (numberp bs-col-after-end)
  3184. ;; Both a preceding and a following backslash.
  3185. ;; Choose the greatest of them.
  3186. (setq column (max column bs-col-after-end)))
  3187. (goto-char from))
  3188. ;; No preceding backslash. Try to align with one
  3189. ;; following the region. Disregard the backslash at the
  3190. ;; to line since it's likely to be bogus (e.g. when
  3191. ;; called from c-newline-and-indent).
  3192. (if (numberp bs-col-after-end)
  3193. (setq column bs-col-after-end))
  3194. ;; Don't modify blank lines at start of region.
  3195. (goto-char from)
  3196. (while (and (< (point) to) (bolp) (eolp))
  3197. (forward-line 1)))
  3198. (if (and column (< column longest-line-col))
  3199. ;; Don't try to align with surrounding backslashes if
  3200. ;; any line is too long.
  3201. (setq column nil))
  3202. (unless column
  3203. ;; Impose minimum limit and tab width alignment only if
  3204. ;; we can't align with surrounding backslashes.
  3205. (if (> (% longest-line-col tab-width) 0)
  3206. (setq longest-line-col
  3207. (* (/ (+ longest-line-col tab-width -1)
  3208. tab-width)
  3209. tab-width)))
  3210. (setq column (max c-backslash-column
  3211. longest-line-col)))
  3212. ;; Always impose maximum limit.
  3213. (setq column (min column c-backslash-max-column)))
  3214. (if bs-col-after-end
  3215. ;; Add backslashes on all lines if the macro continues
  3216. ;; after the to line.
  3217. (progn
  3218. (set-marker endmark to)
  3219. (c-append-backslashes-forward endmark column point-pos))
  3220. ;; Add backslashes on all lines except the last, and
  3221. ;; remove any on the last line.
  3222. (if (save-excursion
  3223. (goto-char to)
  3224. (beginning-of-line)
  3225. (if (not (bobp))
  3226. (set-marker endmark (1- (point)))))
  3227. (progn
  3228. (c-append-backslashes-forward endmark column point-pos)
  3229. ;; The function above leaves point on the line
  3230. ;; following endmark.
  3231. (set-marker endmark (point)))
  3232. (set-marker endmark to))
  3233. (c-delete-backslashes-forward endmark point-pos)))))
  3234. (set-marker endmark nil)
  3235. (if (markerp point-pos)
  3236. (set-marker point-pos nil))))
  3237. (defun c-append-backslashes-forward (to-mark column point-pos)
  3238. (let ((state (parse-partial-sexp (c-point 'bol) (point))))
  3239. (if column
  3240. (while
  3241. (and
  3242. (<= (point) to-mark)
  3243. (let ((start (point)) (inserted nil) end col)
  3244. (end-of-line)
  3245. (unless (eq (char-before) ?\\)
  3246. (insert ?\\)
  3247. (setq inserted t))
  3248. (setq state (parse-partial-sexp
  3249. start (point) nil nil state))
  3250. (backward-char)
  3251. (setq col (current-column))
  3252. ;; Avoid unnecessary changes of the buffer.
  3253. (cond ((and (not inserted) (nth 3 state))
  3254. ;; Don't realign backslashes in string literals
  3255. ;; since that would change them.
  3256. )
  3257. ((< col column)
  3258. (delete-region
  3259. (point)
  3260. (progn
  3261. (skip-chars-backward
  3262. " \t" (if (>= (point) point-pos) point-pos))
  3263. (point)))
  3264. (indent-to column))
  3265. ((and (= col column)
  3266. (memq (char-before) '(?\ ?\t))))
  3267. ((progn
  3268. (setq end (point))
  3269. (or (/= (skip-chars-backward
  3270. " \t" (if (>= (point) point-pos) point-pos))
  3271. -1)
  3272. (/= (char-after) ?\ )))
  3273. (delete-region (point) end)
  3274. (indent-to column 1)))
  3275. (zerop (forward-line 1)))
  3276. (bolp))) ; forward-line has funny behavior at eob.
  3277. ;; Make sure there are backslashes with at least one space in
  3278. ;; front of them.
  3279. (while
  3280. (and
  3281. (<= (point) to-mark)
  3282. (let ((start (point)))
  3283. (end-of-line)
  3284. (setq state (parse-partial-sexp
  3285. start (point) nil nil state))
  3286. (if (eq (char-before) ?\\)
  3287. (unless (nth 3 state)
  3288. (backward-char)
  3289. (unless (and (memq (char-before) '(?\ ?\t))
  3290. (/= (point) point-pos))
  3291. (insert ?\ )))
  3292. (if (and (memq (char-before) '(?\ ?\t))
  3293. (/= (point) point-pos))
  3294. (insert ?\\)
  3295. (insert ?\ ?\\)))
  3296. (zerop (forward-line 1)))
  3297. (bolp)))))) ; forward-line has funny behavior at eob.
  3298. (defun c-delete-backslashes-forward (to-mark point-pos)
  3299. (while
  3300. (and (<= (point) to-mark)
  3301. (progn
  3302. (end-of-line)
  3303. (if (eq (char-before) ?\\)
  3304. (delete-region
  3305. (point)
  3306. (progn (backward-char)
  3307. (skip-chars-backward " \t" (if (>= (point) point-pos)
  3308. point-pos))
  3309. (point))))
  3310. (zerop (forward-line 1)))
  3311. (bolp)))) ; forward-line has funny behavior at eob.
  3312. ;;; Line breaking and paragraph filling.
  3313. (defvar c-auto-fill-prefix t)
  3314. (defvar c-lit-limits nil)
  3315. (defvar c-lit-type nil)
  3316. ;; The filling code is based on a simple theory; leave the intricacies
  3317. ;; of the text handling to the currently active mode for that
  3318. ;; (e.g. adaptive-fill-mode or filladapt-mode) and do as little as
  3319. ;; possible to make them work correctly wrt the comment and string
  3320. ;; separators, one-line paragraphs etc. Unfortunately, when it comes
  3321. ;; to it, there's quite a lot of special cases to handle which makes
  3322. ;; the code anything but simple. The intention is that it will work
  3323. ;; with any well-written text filling package that preserves a fill
  3324. ;; prefix.
  3325. ;;
  3326. ;; We temporarily mask comment starters and enders as necessary for
  3327. ;; the filling code to do its job on a seemingly normal text block.
  3328. ;; We do _not_ mask the fill prefix, so it's up to the filling code to
  3329. ;; preserve it correctly (especially important when filling C++ style
  3330. ;; line comments). By default, we set up and use adaptive-fill-mode,
  3331. ;; which is standard in all supported Emacs flavors.
  3332. (defun c-guess-fill-prefix (lit-limits lit-type)
  3333. ;; Determine the appropriate comment fill prefix for a block or line
  3334. ;; comment. Return a cons of the prefix string and the column where
  3335. ;; it ends. If fill-prefix is set, it'll override. Note that this
  3336. ;; function also uses the value of point in some heuristics.
  3337. ;;
  3338. ;; This function might do hidden buffer changes.
  3339. (let* ((here (point))
  3340. (prefix-regexp (concat "[ \t]*\\("
  3341. c-current-comment-prefix
  3342. "\\)[ \t]*"))
  3343. (comment-start-regexp (if (eq lit-type 'c++)
  3344. prefix-regexp
  3345. comment-start-skip))
  3346. prefix-line comment-prefix res comment-text-end)
  3347. (cond
  3348. (fill-prefix
  3349. (setq res (cons fill-prefix
  3350. ;; Ugly way of getting the column after the fill
  3351. ;; prefix; it'd be nice with a current-column
  3352. ;; that works on strings..
  3353. (let ((start (point)))
  3354. (unwind-protect
  3355. (progn
  3356. (insert-and-inherit "\n" fill-prefix)
  3357. (current-column))
  3358. (delete-region start (point)))))))
  3359. ((eq lit-type 'c++)
  3360. (save-excursion
  3361. ;; Set fallback for comment-prefix if none is found.
  3362. (setq comment-prefix "// "
  3363. comment-text-end (cdr lit-limits))
  3364. (beginning-of-line)
  3365. (if (> (point) (car lit-limits))
  3366. ;; The current line is not the comment starter, so the
  3367. ;; comment has more than one line, and it can therefore be
  3368. ;; used to find the comment fill prefix.
  3369. (setq prefix-line (point))
  3370. (goto-char (car lit-limits))
  3371. (if (and (= (forward-line 1) 0)
  3372. (< (point) (cdr lit-limits)))
  3373. ;; The line after the comment starter is inside the
  3374. ;; comment, so we can use it.
  3375. (setq prefix-line (point))
  3376. ;; The comment is only one line. Take the comment prefix
  3377. ;; from it and keep the indentation.
  3378. (goto-char (car lit-limits))
  3379. (if (looking-at prefix-regexp)
  3380. (goto-char (match-end 0))
  3381. (forward-char 2)
  3382. (skip-chars-forward " \t"))
  3383. (let (str col)
  3384. (if (eq (c-point 'boi) (car lit-limits))
  3385. ;; There is only whitespace before the comment
  3386. ;; starter; take the prefix straight from this line.
  3387. (setq str (buffer-substring-no-properties
  3388. (c-point 'bol) (point))
  3389. col (current-column))
  3390. ;; There is code before the comment starter, so we
  3391. ;; have to temporarily insert and indent a new line to
  3392. ;; get the right space/tab mix in the indentation.
  3393. (let ((prefix-len (- (point) (car lit-limits)))
  3394. tmp)
  3395. (unwind-protect
  3396. (progn
  3397. (goto-char (car lit-limits))
  3398. (indent-to (prog1 (current-column)
  3399. (insert ?\n)))
  3400. (setq tmp (point))
  3401. (forward-char prefix-len)
  3402. (setq str (buffer-substring-no-properties
  3403. (c-point 'bol) (point))
  3404. col (current-column)))
  3405. (delete-region (car lit-limits) tmp))))
  3406. (setq res
  3407. (if (or (string-match "\\s \\'" str) (not (eolp)))
  3408. (cons str col)
  3409. ;; The prefix ends the line with no whitespace
  3410. ;; after it. Default to a single space.
  3411. (cons (concat str " ") (1+ col))))
  3412. )))))
  3413. (t
  3414. (setq comment-text-end
  3415. (save-excursion
  3416. (goto-char (- (cdr lit-limits) 2))
  3417. (if (looking-at "\\*/") (point) (cdr lit-limits))))
  3418. (save-excursion
  3419. (beginning-of-line)
  3420. (if (and (> (point) (car lit-limits))
  3421. (not (and (looking-at "[ \t]*\\*/")
  3422. (eq (cdr lit-limits) (match-end 0)))))
  3423. ;; The current line is not the comment starter and
  3424. ;; contains more than just the ender, so it's good enough
  3425. ;; to be used for the comment fill prefix.
  3426. (setq prefix-line (point))
  3427. (goto-char (car lit-limits))
  3428. (cond ((or (/= (forward-line 1) 0)
  3429. (>= (point) (cdr lit-limits))
  3430. (and (looking-at "[ \t]*\\*/")
  3431. (eq (cdr lit-limits) (match-end 0)))
  3432. (and (looking-at prefix-regexp)
  3433. (<= (1- (cdr lit-limits)) (match-end 0))))
  3434. ;; The comment is either one line or the next line contains
  3435. ;; just the comment ender. In this case we have no
  3436. ;; information about a suitable comment prefix, so we resort
  3437. ;; to c-block-comment-prefix.
  3438. (setq comment-prefix (or c-block-comment-prefix "")))
  3439. ((< here (point))
  3440. ;; The point was on the comment opener line, so we might want
  3441. ;; to treat this as a not yet closed comment.
  3442. (if (and (match-beginning 1)
  3443. (/= (match-beginning 1) (match-end 1)))
  3444. ;; Above `prefix-regexp' matched a nonempty prefix on the
  3445. ;; second line, so let's use it. Normally it should do
  3446. ;; to set `prefix-line' and let the code below pick up
  3447. ;; the whole prefix, but if there's no text after the
  3448. ;; match then it will probably fall back to no prefix at
  3449. ;; all if the comment isn't closed yet, so in that case
  3450. ;; it's better to force use of the prefix matched now.
  3451. (if (= (match-end 0) (c-point 'eol))
  3452. (setq comment-prefix (match-string 1))
  3453. (setq prefix-line (point)))
  3454. ;; There's no nonempty prefix on the line after the
  3455. ;; comment opener. If the line is empty, or if the
  3456. ;; text on it has less or equal indentation than the
  3457. ;; comment starter we assume it's an unclosed
  3458. ;; comment starter, i.e. that
  3459. ;; `c-block-comment-prefix' should be used.
  3460. ;; Otherwise we assume it's a closed comment where
  3461. ;; the prefix really is the empty string.
  3462. ;; E.g. this is an unclosed comment:
  3463. ;;
  3464. ;; /*
  3465. ;; foo
  3466. ;;
  3467. ;; But this is not:
  3468. ;;
  3469. ;; /*
  3470. ;; foo
  3471. ;; */
  3472. ;;
  3473. ;; (Looking for the presence of the comment closer
  3474. ;; rarely works since it's probably the closer of
  3475. ;; some comment further down when the comment
  3476. ;; really is unclosed.)
  3477. (if (<= (save-excursion (back-to-indentation)
  3478. (current-column))
  3479. (save-excursion (goto-char (car lit-limits))
  3480. (current-column)))
  3481. (setq comment-prefix (or c-block-comment-prefix ""))
  3482. (setq prefix-line (point)))))
  3483. (t
  3484. ;; Otherwise the line after the comment starter is good
  3485. ;; enough to find the prefix in.
  3486. (setq prefix-line (point))))
  3487. (when comment-prefix
  3488. ;; Haven't got the comment prefix on any real line that we
  3489. ;; can take it from, so we have to temporarily insert
  3490. ;; `comment-prefix' on a line and indent it to find the
  3491. ;; correct column and the correct mix of tabs and spaces.
  3492. (setq res
  3493. (let (tmp-pre tmp-post)
  3494. (unwind-protect
  3495. (progn
  3496. (goto-char (car lit-limits))
  3497. (if (looking-at comment-start-regexp)
  3498. (goto-char (min (match-end 0)
  3499. comment-text-end))
  3500. (forward-char 2)
  3501. (skip-chars-forward " \t"))
  3502. (when (eq (char-syntax (char-before)) ?\ )
  3503. ;; If there's ws on the current line, we'll use it
  3504. ;; instead of what's ending comment-prefix.
  3505. (setq comment-prefix
  3506. (concat (substring comment-prefix
  3507. 0 (string-match
  3508. "\\s *\\'"
  3509. comment-prefix))
  3510. (buffer-substring-no-properties
  3511. (save-excursion
  3512. (skip-chars-backward " \t")
  3513. (point))
  3514. (point)))))
  3515. (setq tmp-pre (point-marker))
  3516. ;; We insert an extra non-whitespace character
  3517. ;; before the line break and after comment-prefix in
  3518. ;; case it's "" or ends with whitespace.
  3519. (insert-and-inherit "x\n" comment-prefix "x")
  3520. (setq tmp-post (point-marker))
  3521. (indent-according-to-mode)
  3522. (goto-char (1- tmp-post))
  3523. (cons (buffer-substring-no-properties
  3524. (c-point 'bol) (point))
  3525. (current-column)))
  3526. (when tmp-post
  3527. (delete-region tmp-pre tmp-post)
  3528. (set-marker tmp-pre nil)
  3529. (set-marker tmp-post nil))))))))))
  3530. (or res ; Found a good prefix above.
  3531. (save-excursion
  3532. ;; prefix-line is the bol of a line on which we should try
  3533. ;; to find the prefix.
  3534. (let* (fb-string fb-endpos ; Contains any fallback prefix found.
  3535. (test-line
  3536. (lambda ()
  3537. (when (and (looking-at prefix-regexp)
  3538. (<= (match-end 0) comment-text-end))
  3539. (unless (eq (match-end 0) (c-point 'eol))
  3540. ;; The match is fine if there's text after it.
  3541. (throw 'found (cons (buffer-substring-no-properties
  3542. (match-beginning 0) (match-end 0))
  3543. (progn (goto-char (match-end 0))
  3544. (current-column)))))
  3545. (unless fb-string
  3546. ;; This match is better than nothing, so let's
  3547. ;; remember it in case nothing better is found
  3548. ;; on another line.
  3549. (setq fb-string (buffer-substring-no-properties
  3550. (match-beginning 0) (match-end 0))
  3551. fb-endpos (match-end 0)))
  3552. t))))
  3553. (or (catch 'found
  3554. ;; Search for a line which has text after the prefix
  3555. ;; so that we get the proper amount of whitespace
  3556. ;; after it. We start with the current line, then
  3557. ;; search backwards, then forwards.
  3558. (goto-char prefix-line)
  3559. (when (and (funcall test-line)
  3560. (or (/= (match-end 1) (match-end 0))
  3561. ;; The whitespace is sucked up by the
  3562. ;; first [ \t]* glob if the prefix is empty.
  3563. (and (= (match-beginning 1) (match-end 1))
  3564. (/= (match-beginning 0) (match-end 0)))))
  3565. ;; If the current line doesn't have text but do
  3566. ;; have whitespace after the prefix, we'll use it.
  3567. (throw 'found (cons fb-string
  3568. (progn (goto-char fb-endpos)
  3569. (current-column)))))
  3570. (if (eq lit-type 'c++)
  3571. ;; For line comments we can search up to and
  3572. ;; including the first line.
  3573. (while (and (zerop (forward-line -1))
  3574. (>= (point) (car lit-limits)))
  3575. (funcall test-line))
  3576. ;; For block comments we must stop before the
  3577. ;; block starter.
  3578. (while (and (zerop (forward-line -1))
  3579. (> (point) (car lit-limits)))
  3580. (funcall test-line)))
  3581. (goto-char prefix-line)
  3582. (while (and (zerop (forward-line 1))
  3583. (< (point) (cdr lit-limits)))
  3584. (funcall test-line))
  3585. (goto-char prefix-line)
  3586. nil)
  3587. (when fb-string
  3588. ;; A good line wasn't found, but at least we have a
  3589. ;; fallback that matches the comment prefix regexp.
  3590. (cond ((or (string-match "\\s \\'" fb-string)
  3591. (progn
  3592. (goto-char fb-endpos)
  3593. (not (eolp))))
  3594. ;; There are ws or text after the prefix, so
  3595. ;; let's use it.
  3596. (cons fb-string (current-column)))
  3597. ((progn
  3598. ;; Check if there's any whitespace padding
  3599. ;; on the comment start line that we can
  3600. ;; use after the prefix.
  3601. (goto-char (car lit-limits))
  3602. (if (looking-at comment-start-regexp)
  3603. (goto-char (match-end 0))
  3604. (forward-char 2)
  3605. (skip-chars-forward " \t"))
  3606. (or (not (eolp))
  3607. (eq (char-syntax (char-before)) ?\ )))
  3608. (setq fb-string (buffer-substring-no-properties
  3609. (save-excursion
  3610. (skip-chars-backward " \t")
  3611. (point))
  3612. (point)))
  3613. (goto-char fb-endpos)
  3614. (skip-chars-backward " \t")
  3615. (let ((tmp (point)))
  3616. ;; Got to mess in the buffer once again to
  3617. ;; ensure the column gets correct. :P
  3618. (unwind-protect
  3619. (progn
  3620. (insert-and-inherit fb-string)
  3621. (cons (buffer-substring-no-properties
  3622. (c-point 'bol)
  3623. (point))
  3624. (current-column)))
  3625. (delete-region tmp (point)))))
  3626. (t
  3627. ;; Last resort: Just add a single space after
  3628. ;; the prefix.
  3629. (cons (concat fb-string " ")
  3630. (progn (goto-char fb-endpos)
  3631. (1+ (current-column)))))))
  3632. ;; The line doesn't match the comment prefix regexp.
  3633. (if comment-prefix
  3634. ;; We have a fallback for line comments that we must use.
  3635. (cons (concat (buffer-substring-no-properties
  3636. prefix-line (c-point 'boi))
  3637. comment-prefix)
  3638. (progn (back-to-indentation)
  3639. (+ (current-column) (length comment-prefix))))
  3640. ;; Assume we are dealing with a "free text" block
  3641. ;; comment where the lines doesn't have any comment
  3642. ;; prefix at all and we should just fill it as
  3643. ;; normal text.
  3644. '("" . 0))))))
  3645. ))
  3646. (defun c-mask-paragraph (fill-paragraph apply-outside-literal fun &rest args)
  3647. ;; Calls FUN with ARGS ar arguments while the current paragraph is
  3648. ;; masked to allow adaptive filling to work correctly. That
  3649. ;; includes narrowing the buffer and, if point is inside a comment,
  3650. ;; masking the comment starter and ender appropriately.
  3651. ;;
  3652. ;; FILL-PARAGRAPH is non-nil if called for whole paragraph filling.
  3653. ;; The position of point is then less significant when doing masking
  3654. ;; and narrowing.
  3655. ;;
  3656. ;; If APPLY-OUTSIDE-LITERAL is nil then the function will be called
  3657. ;; only if the point turns out to be inside a comment or a string.
  3658. ;;
  3659. ;; Note that this function does not do any hidden buffer changes.
  3660. (let (fill
  3661. ;; beg and end limit the region to narrow. end is a marker.
  3662. beg end
  3663. ;; tmp-pre and tmp-post mark strings that are temporarily
  3664. ;; inserted at the start and end of the region. tmp-pre is a
  3665. ;; cons of the positions of the prepended string. tmp-post is
  3666. ;; a marker pointing to the single character of the appended
  3667. ;; string.
  3668. tmp-pre tmp-post
  3669. ;; If hang-ender-stuck isn't nil, the comment ender is
  3670. ;; hanging. In that case it's set to the number of spaces
  3671. ;; that should be between the text and the ender.
  3672. hang-ender-stuck
  3673. ;; auto-fill-spaces is the exact sequence of whitespace between a
  3674. ;; comment's last word and the comment ender, temporarily replaced
  3675. ;; with 'x's before calling FUN when FILL-PARAGRAPH is nil.
  3676. auto-fill-spaces
  3677. (here (point))
  3678. (c-lit-limits c-lit-limits)
  3679. (c-lit-type c-lit-type))
  3680. ;; Restore point on undo. It's necessary since we do a lot of
  3681. ;; hidden inserts and deletes below that should be as transparent
  3682. ;; as possible.
  3683. (if (and buffer-undo-list (not (eq buffer-undo-list t)))
  3684. (setq buffer-undo-list (cons (point) buffer-undo-list)))
  3685. ;; Determine the limits and type of the containing literal (if any):
  3686. ;; C-LIT-LIMITS, C-LIT-TYPE; and the limits of the current paragraph:
  3687. ;; BEG and END.
  3688. (c-save-buffer-state ()
  3689. (save-restriction
  3690. ;; Widen to catch comment limits correctly.
  3691. (widen)
  3692. (unless c-lit-limits
  3693. (setq c-lit-limits (c-literal-limits nil fill-paragraph)))
  3694. (setq c-lit-limits (c-collect-line-comments c-lit-limits))
  3695. (unless c-lit-type
  3696. (setq c-lit-type (c-literal-type c-lit-limits))))
  3697. (save-excursion
  3698. (unless (c-safe (backward-char)
  3699. (forward-paragraph)
  3700. (>= (point) here))
  3701. (goto-char here)
  3702. (forward-paragraph))
  3703. (setq end (point-marker)))
  3704. (save-excursion
  3705. (unless (c-safe (forward-char)
  3706. (backward-paragraph)
  3707. (<= (point) here))
  3708. (goto-char here)
  3709. (backward-paragraph))
  3710. (setq beg (point))))
  3711. (unwind-protect
  3712. (progn
  3713. ;; For each of the possible types of text (string, C comment ...)
  3714. ;; determine BEG and END, the region we will narrow to. If we're in
  3715. ;; a literal, constrain BEG and END to the limits of this literal.
  3716. ;;
  3717. ;; For some of these text types, particularly a block comment, we
  3718. ;; may need to massage whitespace near literal delimiters, so that
  3719. ;; these don't get filled inappropriately.
  3720. (cond
  3721. ((eq c-lit-type 'c++) ; Line comment.
  3722. (save-excursion
  3723. ;; Limit to the comment or paragraph end, whichever
  3724. ;; comes first.
  3725. (set-marker end (min end (cdr c-lit-limits)))
  3726. (when (<= beg (car c-lit-limits))
  3727. ;; The region includes the comment starter, so we must
  3728. ;; check it.
  3729. (goto-char (car c-lit-limits))
  3730. (back-to-indentation)
  3731. (if (eq (point) (car c-lit-limits))
  3732. ;; Include the first line in the region.
  3733. (setq beg (c-point 'bol))
  3734. ;; The first line contains code before the
  3735. ;; comment. We must fake a line that doesn't.
  3736. (setq tmp-pre t))))
  3737. (setq apply-outside-literal t))
  3738. ((eq c-lit-type 'c) ; Block comment.
  3739. (when
  3740. (or (> end (cdr c-lit-limits))
  3741. (and (= end (cdr c-lit-limits))
  3742. (eq (char-before end) ?/)
  3743. (eq (char-before (1- end)) ?*)
  3744. ;; disallow "/*/"
  3745. (> (- (cdr c-lit-limits) (car c-lit-limits)) 3)))
  3746. ;; There is a comment ender, and the region includes it. If
  3747. ;; it's on its own line, it stays on its own line. If it's got
  3748. ;; company on the line, it keeps (at least one word of) it.
  3749. ;; "=====*/" counts as a comment ender here, but "===== */"
  3750. ;; doesn't and "foo*/" doesn't.
  3751. (unless
  3752. (save-excursion
  3753. (goto-char (cdr c-lit-limits))
  3754. (beginning-of-line)
  3755. ;; The following conjunct was added to avoid an
  3756. ;; "Invalid search bound (wrong side of point)"
  3757. ;; error in the subsequent re-search. Maybe
  3758. ;; another fix would be needed (2007-12-08).
  3759. ; (or (<= (- (cdr c-lit-limits) 2) (point))
  3760. ; 2010-10-17 Construct removed.
  3761. ; (or (< (- (cdr c-lit-limits) 2) (point))
  3762. (and
  3763. (search-forward-regexp
  3764. (concat "\\=[ \t]*\\(" c-current-comment-prefix "\\)")
  3765. (- (cdr c-lit-limits) 2) t)
  3766. (not (search-forward-regexp
  3767. "\\(\\s \\|\\sw\\)"
  3768. (- (cdr c-lit-limits) 2) 'limit))
  3769. ;; The comment ender IS on its own line. Exclude this
  3770. ;; line from the filling.
  3771. (set-marker end (c-point 'bol))));)
  3772. ;; The comment ender is hanging. Replace all space between it
  3773. ;; and the last word either by one or two 'x's (when
  3774. ;; FILL-PARAGRAPH is non-nil), or a row of x's the same width
  3775. ;; as the whitespace (when auto filling), and include it in
  3776. ;; the region. We'll change them back to whitespace
  3777. ;; afterwards. The effect of this is to glue the comment
  3778. ;; ender to the last word in the comment during filling.
  3779. (let* ((ender-start (save-excursion
  3780. (goto-char (cdr c-lit-limits))
  3781. (skip-syntax-backward "^w ")
  3782. (point)))
  3783. (ender-column (save-excursion
  3784. (goto-char ender-start)
  3785. (current-column)))
  3786. (point-rel (- ender-start here))
  3787. (sentence-ends-comment
  3788. (save-excursion
  3789. (goto-char ender-start)
  3790. (and (search-backward-regexp
  3791. (c-sentence-end) (c-point 'bol) t)
  3792. (goto-char (match-end 0))
  3793. (looking-at "[ \t]*")
  3794. (= (match-end 0) ender-start))))
  3795. spaces)
  3796. (save-excursion
  3797. ;; Insert a CR after the "*/", adjust END
  3798. (goto-char (cdr c-lit-limits))
  3799. (setq tmp-post (point-marker))
  3800. (insert ?\n)
  3801. (set-marker end (point))
  3802. (forward-line -1) ; last line of the comment
  3803. (if (and (looking-at (concat "[ \t]*\\(\\("
  3804. c-current-comment-prefix
  3805. "\\)[ \t]*\\)"))
  3806. (eq ender-start (match-end 0)))
  3807. ;; The comment ender is prefixed by nothing but a
  3808. ;; comment line prefix. IS THIS POSSIBLE? (ACM,
  3809. ;; 2006/4/28). Remove it along with surrounding ws.
  3810. (setq spaces (- (match-end 1) (match-end 2)))
  3811. (goto-char ender-start))
  3812. (skip-chars-backward " \t\r\n") ; Surely this can be
  3813. ; " \t"? "*/" is NOT alone on the line (ACM, 2005/8/18)
  3814. ;; What's being tested here? 2006/4/20. FIXME!!!
  3815. (if (/= (point) ender-start)
  3816. (progn
  3817. (if (<= here (point))
  3818. ;; Don't adjust point below if it's
  3819. ;; before the string we replace.
  3820. (setq point-rel -1))
  3821. ;; Keep one or two spaces between the
  3822. ;; text and the ender, depending on how
  3823. ;; many there are now.
  3824. (unless spaces
  3825. (setq spaces (- ender-column (current-column))))
  3826. (setq auto-fill-spaces (c-delete-and-extract-region
  3827. (point) ender-start))
  3828. ;; paragraph filling condenses multiple spaces to
  3829. ;; single or double spaces. auto-fill doesn't.
  3830. (if fill-paragraph
  3831. (setq spaces
  3832. (max
  3833. (min spaces
  3834. (if (and sentence-ends-comment
  3835. sentence-end-double-space)
  3836. 2 1))
  3837. 1)))
  3838. ;; Insert the filler first to keep marks right.
  3839. (insert-char ?x spaces t)
  3840. (setq hang-ender-stuck spaces)
  3841. (setq point-rel
  3842. (and (>= point-rel 0)
  3843. (- (point) (min point-rel spaces)))))
  3844. (setq point-rel nil)))
  3845. (if point-rel
  3846. ;; Point was in the middle of the string we
  3847. ;; replaced above, so put it back in the same
  3848. ;; relative position, counting from the end.
  3849. (goto-char point-rel)))
  3850. ))
  3851. (when (<= beg (car c-lit-limits))
  3852. ;; The region includes the comment starter.
  3853. (save-excursion
  3854. (goto-char (car c-lit-limits))
  3855. (if (looking-at (concat "\\(" comment-start-skip "\\)$"))
  3856. ;; Begin with the next line.
  3857. (setq beg (c-point 'bonl))
  3858. ;; Fake the fill prefix in the first line.
  3859. (setq tmp-pre t))))
  3860. (setq apply-outside-literal t))
  3861. ((eq c-lit-type 'string) ; String.
  3862. (save-excursion
  3863. (when (>= end (cdr c-lit-limits))
  3864. (goto-char (1- (cdr c-lit-limits)))
  3865. (setq tmp-post (point-marker))
  3866. (insert ?\n)
  3867. (set-marker end (point)))
  3868. (when (<= beg (car c-lit-limits))
  3869. (goto-char (1+ (car c-lit-limits)))
  3870. (setq beg (if (looking-at "\\\\$")
  3871. ;; Leave the start line if it's
  3872. ;; nothing but an escaped newline.
  3873. (1+ (match-end 0))
  3874. (point)))))
  3875. (setq apply-outside-literal t))
  3876. ((eq c-lit-type 'pound) ; Macro
  3877. ;; Narrow to the macro limits if they are nearer than the
  3878. ;; paragraph limits. Don't know if this is necessary but
  3879. ;; do it for completeness sake (doing auto filling at all
  3880. ;; inside macros is bogus to begin with since the line
  3881. ;; continuation backslashes aren't handled).
  3882. (save-excursion
  3883. (c-save-buffer-state ()
  3884. (c-beginning-of-macro)
  3885. (beginning-of-line)
  3886. (if (> (point) beg)
  3887. (setq beg (point)))
  3888. (c-end-of-macro)
  3889. (forward-line)
  3890. (if (< (point) end)
  3891. (set-marker end (point))))))
  3892. (t ; Other code.
  3893. ;; Try to avoid comments and macros in the paragraph to
  3894. ;; avoid that the adaptive fill mode gets the prefix from
  3895. ;; them.
  3896. (c-save-buffer-state nil
  3897. (save-excursion
  3898. (goto-char beg)
  3899. (c-forward-syntactic-ws end)
  3900. (beginning-of-line)
  3901. (setq beg (point))
  3902. (goto-char end)
  3903. (c-backward-syntactic-ws beg)
  3904. (forward-line)
  3905. (set-marker end (point))))))
  3906. (when tmp-pre
  3907. ;; Temporarily insert the fill prefix after the comment
  3908. ;; starter so that the first line looks like any other
  3909. ;; comment line in the narrowed region.
  3910. (setq fill (c-save-buffer-state nil
  3911. (c-guess-fill-prefix c-lit-limits c-lit-type)))
  3912. (unless (string-match (concat "\\`[ \t]*\\("
  3913. c-current-comment-prefix
  3914. "\\)[ \t]*\\'")
  3915. (car fill))
  3916. ;; Oops, the prefix doesn't match the comment prefix
  3917. ;; regexp. This could produce very confusing
  3918. ;; results with adaptive fill packages together with
  3919. ;; the insert prefix magic below, since the prefix
  3920. ;; often doesn't appear at all. So let's warn about
  3921. ;; it.
  3922. (message "\
  3923. Warning: Regexp from `c-comment-prefix-regexp' doesn't match the comment prefix %S"
  3924. (car fill)))
  3925. ;; Find the right spot on the line, break it, insert
  3926. ;; the fill prefix and make sure we're back in the
  3927. ;; same column by temporarily prefixing the first word
  3928. ;; with a number of 'x'.
  3929. (save-excursion
  3930. (goto-char (car c-lit-limits))
  3931. (if (looking-at (if (eq c-lit-type 'c++)
  3932. c-current-comment-prefix
  3933. comment-start-skip))
  3934. (goto-char (match-end 0))
  3935. (forward-char 2)
  3936. (skip-chars-forward " \t"))
  3937. (while (and (< (current-column) (cdr fill))
  3938. (not (eolp)))
  3939. (forward-char 1))
  3940. (let ((col (current-column)))
  3941. (setq beg (1+ (point))
  3942. tmp-pre (list (point)))
  3943. (unwind-protect
  3944. (progn
  3945. (insert-and-inherit "\n" (car fill))
  3946. (insert-char ?x (- col (current-column)) t))
  3947. (setcdr tmp-pre (point))))))
  3948. (when apply-outside-literal
  3949. ;; `apply-outside-literal' is always set to t here if
  3950. ;; we're inside a literal.
  3951. (let ((fill-prefix
  3952. (or fill-prefix
  3953. ;; Kludge: If the function that adapts the fill prefix
  3954. ;; doesn't produce the required comment starter for
  3955. ;; line comments, then force it by setting fill-prefix.
  3956. (when (and (eq c-lit-type 'c++)
  3957. ;; Kludge the kludge: filladapt-mode doesn't
  3958. ;; have this problem, but it currently
  3959. ;; doesn't override fill-context-prefix
  3960. ;; (version 2.12).
  3961. (not (and (boundp 'filladapt-mode)
  3962. filladapt-mode))
  3963. (not (string-match
  3964. "\\`[ \t]*//"
  3965. (or (fill-context-prefix beg end)
  3966. ""))))
  3967. (c-save-buffer-state nil
  3968. (car (or fill (c-guess-fill-prefix
  3969. c-lit-limits c-lit-type)))))))
  3970. ;; Save the relative position of point if it's outside the
  3971. ;; region we're going to narrow. Want to restore it in that
  3972. ;; case, but otherwise it should be moved according to the
  3973. ;; called function.
  3974. (point-rel (cond ((< (point) beg) (- (point) beg))
  3975. ((> (point) end) (- (point) end)))))
  3976. ;; Preparations finally done! Now we can call the
  3977. ;; actual function.
  3978. (prog1
  3979. (save-restriction
  3980. (narrow-to-region beg end)
  3981. (apply fun args))
  3982. (if point-rel
  3983. ;; Restore point if it was outside the region.
  3984. (if (< point-rel 0)
  3985. (goto-char (+ beg point-rel))
  3986. (goto-char (+ end point-rel))))))))
  3987. (when (consp tmp-pre)
  3988. (delete-region (car tmp-pre) (cdr tmp-pre)))
  3989. (when tmp-post
  3990. (save-excursion
  3991. (goto-char tmp-post)
  3992. (delete-char 1))
  3993. (when hang-ender-stuck
  3994. ;; Preserve point even if it's in the middle of the string
  3995. ;; we replace; save-excursion doesn't work in that case.
  3996. (setq here (point))
  3997. (goto-char tmp-post)
  3998. (skip-syntax-backward "^w ")
  3999. (forward-char (- hang-ender-stuck))
  4000. (if (or fill-paragraph (not auto-fill-spaces))
  4001. (insert-char ?\ hang-ender-stuck t)
  4002. (insert auto-fill-spaces))
  4003. (delete-char hang-ender-stuck)
  4004. (goto-char here))
  4005. (set-marker tmp-post nil))
  4006. (set-marker end nil))))
  4007. (defun c-fill-paragraph (&optional arg)
  4008. "Like \\[fill-paragraph] but handles C and C++ style comments.
  4009. If any of the current line is a comment or within a comment, fill the
  4010. comment or the paragraph of it that point is in, preserving the
  4011. comment indentation or line-starting decorations (see the
  4012. `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
  4013. details).
  4014. If point is inside multiline string literal, fill it. This currently
  4015. does not respect escaped newlines, except for the special case when it
  4016. is the very first thing in the string. The intended use for this rule
  4017. is in situations like the following:
  4018. char description[] = \"\\
  4019. A very long description of something that you want to fill to make
  4020. nicely formatted output.\"\;
  4021. If point is in any other situation, i.e. in normal code, do nothing.
  4022. Optional prefix ARG means justify paragraph as well."
  4023. (interactive "*P")
  4024. (let ((fill-paragraph-function
  4025. ;; Avoid infinite recursion.
  4026. (if (not (eq fill-paragraph-function 'c-fill-paragraph))
  4027. fill-paragraph-function)))
  4028. (c-mask-paragraph t nil 'fill-paragraph arg))
  4029. ;; Always return t. This has the effect that if filling isn't done
  4030. ;; above, it isn't done at all, and it's therefore effectively
  4031. ;; disabled in normal code.
  4032. t)
  4033. (defun c-do-auto-fill ()
  4034. ;; Do automatic filling if not inside a context where it should be
  4035. ;; ignored.
  4036. (let ((c-auto-fill-prefix
  4037. ;; The decision whether the line should be broken is actually
  4038. ;; done in c-indent-new-comment-line, which do-auto-fill
  4039. ;; calls to break lines. We just set this special variable
  4040. ;; so that we'll know when we're called from there. It's
  4041. ;; also used to detect whether fill-prefix is user set or
  4042. ;; generated automatically by do-auto-fill.
  4043. fill-prefix))
  4044. (c-mask-paragraph nil t 'do-auto-fill)))
  4045. (defun c-indent-new-comment-line (&optional soft allow-auto-fill)
  4046. "Break line at point and indent, continuing comment or macro if within one.
  4047. If inside a comment and `comment-multi-line' is non-nil, the
  4048. indentation and line prefix are preserved (see the
  4049. `c-comment-prefix-regexp' and `c-block-comment-prefix' variables for
  4050. details). If inside a single line comment and `comment-multi-line' is
  4051. nil, a new comment of the same type is started on the next line and
  4052. indented as appropriate for comments. If inside a macro, a line
  4053. continuation backslash is inserted and aligned as appropriate, and the
  4054. new line is indented according to `c-syntactic-indentation'.
  4055. If a fill prefix is specified, it overrides all the above."
  4056. ;; allow-auto-fill is used from c-context-line-break to allow auto
  4057. ;; filling to break the line more than once. Since this function is
  4058. ;; used from auto-fill itself, that's normally disabled to avoid
  4059. ;; unnecessary recursion.
  4060. (interactive)
  4061. (let ((fill-prefix fill-prefix)
  4062. (do-line-break
  4063. (lambda ()
  4064. (delete-horizontal-space)
  4065. (if soft
  4066. (insert-and-inherit ?\n)
  4067. (newline (if allow-auto-fill nil 1)))))
  4068. ;; Already know the literal type and limits when called from
  4069. ;; c-context-line-break.
  4070. (c-lit-limits c-lit-limits)
  4071. (c-lit-type c-lit-type)
  4072. (c-macro-start c-macro-start))
  4073. (c-save-buffer-state ()
  4074. (when (not (eq c-auto-fill-prefix t))
  4075. ;; Called from do-auto-fill.
  4076. (unless c-lit-limits
  4077. (setq c-lit-limits (c-literal-limits nil nil t)))
  4078. (unless c-lit-type
  4079. (setq c-lit-type (c-literal-type c-lit-limits)))
  4080. (if (memq (cond ((c-query-and-set-macro-start) 'cpp)
  4081. ((null c-lit-type) 'code)
  4082. (t c-lit-type))
  4083. c-ignore-auto-fill)
  4084. (setq fill-prefix t) ; Used as flag in the cond.
  4085. (if (and (null c-auto-fill-prefix)
  4086. (eq c-lit-type 'c)
  4087. (<= (c-point 'bol) (car c-lit-limits)))
  4088. ;; The adaptive fill function has generated a prefix, but
  4089. ;; we're on the first line in a block comment so it'll be
  4090. ;; wrong. Ignore it to guess a better one below.
  4091. (setq fill-prefix nil)
  4092. (when (and (eq c-lit-type 'c++)
  4093. (not (string-match (concat "\\`[ \t]*"
  4094. c-line-comment-starter)
  4095. (or fill-prefix ""))))
  4096. ;; Kludge: If the function that adapted the fill prefix
  4097. ;; doesn't produce the required comment starter for line
  4098. ;; comments, then we ignore it.
  4099. (setq fill-prefix nil)))
  4100. )))
  4101. (cond ((eq fill-prefix t)
  4102. ;; A call from do-auto-fill which should be ignored.
  4103. )
  4104. (fill-prefix
  4105. ;; A fill-prefix overrides anything.
  4106. (funcall do-line-break)
  4107. (insert-and-inherit fill-prefix))
  4108. ((c-save-buffer-state ()
  4109. (unless c-lit-limits
  4110. (setq c-lit-limits (c-literal-limits)))
  4111. (unless c-lit-type
  4112. (setq c-lit-type (c-literal-type c-lit-limits)))
  4113. (memq c-lit-type '(c c++)))
  4114. ;; Some sort of comment.
  4115. (if (or comment-multi-line
  4116. (save-excursion
  4117. (goto-char (car c-lit-limits))
  4118. (end-of-line)
  4119. (< (point) (cdr c-lit-limits))))
  4120. ;; Inside a comment that should be continued.
  4121. (let ((fill (c-save-buffer-state nil
  4122. (c-guess-fill-prefix
  4123. (setq c-lit-limits
  4124. (c-collect-line-comments c-lit-limits))
  4125. c-lit-type)))
  4126. (pos (point))
  4127. (start-col (current-column))
  4128. (comment-text-end
  4129. (or (and (eq c-lit-type 'c)
  4130. (save-excursion
  4131. (goto-char (- (cdr c-lit-limits) 2))
  4132. (if (looking-at "\\*/") (point))))
  4133. (cdr c-lit-limits))))
  4134. ;; Skip forward past the fill prefix in case
  4135. ;; we're standing in it.
  4136. ;;
  4137. ;; FIXME: This doesn't work well in cases like
  4138. ;;
  4139. ;; /* Bla bla bla bla bla
  4140. ;; bla bla
  4141. ;;
  4142. ;; If point is on the 'B' then the line will be
  4143. ;; broken after "Bla b".
  4144. ;;
  4145. ;; If we have an empty comment, /* */, the next
  4146. ;; lot of code pushes point to the */. We fix
  4147. ;; this by never allowing point to end up to the
  4148. ;; right of where it started.
  4149. (while (and (< (current-column) (cdr fill))
  4150. (not (eolp)))
  4151. (forward-char 1))
  4152. (if (and (> (point) comment-text-end)
  4153. (> (c-point 'bol) (car c-lit-limits)))
  4154. (progn
  4155. ;; The skip takes us out of the (block)
  4156. ;; comment; insert the fill prefix at bol
  4157. ;; instead and keep the position.
  4158. (setq pos (copy-marker pos t))
  4159. (beginning-of-line)
  4160. (insert-and-inherit (car fill))
  4161. (if soft (insert-and-inherit ?\n) (newline 1))
  4162. (goto-char pos)
  4163. (set-marker pos nil))
  4164. ;; Don't break in the middle of a comment starter
  4165. ;; or ender.
  4166. (cond ((> (point) comment-text-end)
  4167. (goto-char comment-text-end))
  4168. ((< (point) (+ (car c-lit-limits) 2))
  4169. (goto-char (+ (car c-lit-limits) 2))))
  4170. (funcall do-line-break)
  4171. (insert-and-inherit (car fill))
  4172. (if (> (current-column) start-col)
  4173. (move-to-column start-col)))) ; can this hit the
  4174. ; middle of a TAB?
  4175. ;; Inside a comment that should be broken.
  4176. (let ((comment-start comment-start)
  4177. (comment-end comment-end)
  4178. col)
  4179. (if (eq c-lit-type 'c)
  4180. (unless (string-match "[ \t]*/\\*" comment-start)
  4181. (setq comment-start "/* " comment-end " */"))
  4182. (unless (string-match "[ \t]*//" comment-start)
  4183. (setq comment-start "// " comment-end "")))
  4184. (setq col (save-excursion
  4185. (back-to-indentation)
  4186. (current-column)))
  4187. (funcall do-line-break)
  4188. (when (and comment-end (not (equal comment-end "")))
  4189. (forward-char -1)
  4190. (insert-and-inherit comment-end)
  4191. (forward-char 1))
  4192. ;; c-comment-indent may look at the current
  4193. ;; indentation, so let's start out with the same
  4194. ;; indentation as the previous one.
  4195. (indent-to col)
  4196. (insert-and-inherit comment-start)
  4197. (indent-for-comment))))
  4198. ((c-query-and-set-macro-start)
  4199. ;; In a macro.
  4200. (unless (looking-at "[ \t]*\\\\$")
  4201. ;; Do not clobber the alignment of the line continuation
  4202. ;; slash; c-backslash-region might look at it.
  4203. (delete-horizontal-space))
  4204. ;; Got an asymmetry here: In normal code this command
  4205. ;; doesn't indent the next line syntactically, and otoh a
  4206. ;; normal syntactically indenting newline doesn't continue
  4207. ;; the macro.
  4208. (c-newline-and-indent (if allow-auto-fill nil 1)))
  4209. (t
  4210. ;; Somewhere else in the code.
  4211. (let ((col (save-excursion
  4212. (beginning-of-line)
  4213. (while (and (looking-at "[ \t]*\\\\?$")
  4214. (= (forward-line -1) 0)))
  4215. (current-indentation))))
  4216. (funcall do-line-break)
  4217. (indent-to col))))))
  4218. (defalias 'c-comment-line-break-function 'c-indent-new-comment-line)
  4219. (make-obsolete 'c-comment-line-break-function 'c-indent-new-comment-line "21.1")
  4220. ;; advice for indent-new-comment-line for older Emacsen
  4221. (unless (boundp 'comment-line-break-function)
  4222. (defvar c-inside-line-break-advice nil)
  4223. (defadvice indent-new-comment-line (around c-line-break-advice
  4224. activate preactivate)
  4225. "Call `c-indent-new-comment-line' if in CC Mode."
  4226. (if (or c-inside-line-break-advice
  4227. (not c-buffer-is-cc-mode))
  4228. ad-do-it
  4229. (let ((c-inside-line-break-advice t))
  4230. (c-indent-new-comment-line (ad-get-arg 0))))))
  4231. (defun c-context-line-break ()
  4232. "Do a line break suitable to the context.
  4233. When point is outside a comment or macro, insert a newline and indent
  4234. according to the syntactic context, unless `c-syntactic-indentation'
  4235. is nil, in which case the new line is indented as the previous
  4236. non-empty line instead.
  4237. When point is inside the content of a preprocessor directive, a line
  4238. continuation backslash is inserted before the line break and aligned
  4239. appropriately. The end of the cpp directive doesn't count as inside
  4240. it.
  4241. When point is inside a comment, continue it with the appropriate
  4242. comment prefix (see the `c-comment-prefix-regexp' and
  4243. `c-block-comment-prefix' variables for details). The end of a
  4244. C++-style line comment doesn't count as inside it.
  4245. When point is inside a string, only insert a backslash when it is also
  4246. inside a preprocessor directive."
  4247. (interactive "*")
  4248. (let* (c-lit-limits c-lit-type
  4249. (c-macro-start c-macro-start)
  4250. case-fold-search)
  4251. (c-save-buffer-state ()
  4252. (setq c-lit-limits (c-literal-limits nil nil t)
  4253. c-lit-type (c-literal-type c-lit-limits))
  4254. (when (eq c-lit-type 'c++)
  4255. (setq c-lit-limits (c-collect-line-comments c-lit-limits)))
  4256. (c-query-and-set-macro-start))
  4257. (cond
  4258. ((or (eq c-lit-type 'c)
  4259. (and (eq c-lit-type 'c++) ; C++ comment, but not at the very end of it.
  4260. (< (save-excursion
  4261. (skip-chars-forward " \t")
  4262. (point))
  4263. (1- (cdr c-lit-limits))))
  4264. (and (numberp c-macro-start) ; Macro, but not at the very end of
  4265. ; it, not in a string, and not in the
  4266. ; cpp keyword.
  4267. (not (eq c-lit-type 'string))
  4268. (or (not (looking-at "\\s *$"))
  4269. (eq (char-before) ?\\))
  4270. (<= (save-excursion
  4271. (goto-char c-macro-start)
  4272. (if (looking-at c-opt-cpp-start)
  4273. (goto-char (match-end 0)))
  4274. (point))
  4275. (point))))
  4276. (let ((comment-multi-line t)
  4277. (fill-prefix nil))
  4278. (c-indent-new-comment-line nil t)))
  4279. ((eq c-lit-type 'string)
  4280. (if (and (numberp c-macro-start)
  4281. (not (eq (char-before) ?\\)))
  4282. (insert ?\\))
  4283. (newline))
  4284. (t (delete-horizontal-space)
  4285. (newline)
  4286. ;; c-indent-line may look at the current indentation, so let's
  4287. ;; start out with the same indentation as the previous line.
  4288. (let ((col (save-excursion
  4289. (backward-char)
  4290. (forward-line 0)
  4291. (while (and (looking-at "[ \t]*\\\\?$")
  4292. (= (forward-line -1) 0)))
  4293. (current-indentation))))
  4294. (indent-to col))
  4295. (indent-according-to-mode)))))
  4296. (defun c-context-open-line ()
  4297. "Insert a line break suitable to the context and leave point before it.
  4298. This is the `c-context-line-break' equivalent to `open-line', which is
  4299. normally bound to C-o. See `c-context-line-break' for the details."
  4300. (interactive "*")
  4301. (let ((here (point)))
  4302. (unwind-protect
  4303. (progn
  4304. ;; Temporarily insert a non-whitespace char to keep any
  4305. ;; preceding whitespace intact.
  4306. (insert ?x)
  4307. (c-context-line-break))
  4308. (goto-char here)
  4309. (delete-char 1))))
  4310. (cc-provide 'cc-cmds)
  4311. ;;; cc-cmds.el ends here