cc-engine.el 392 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845
  1. ;;; cc-engine.el --- core syntax guessing engine for CC mode
  2. ;; Copyright (C) 1985, 1987, 1992-2012 Free Software Foundation, Inc.
  3. ;; Authors: 2001- 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. ;; The functions which have docstring documentation can be considered
  26. ;; part of an API which other packages can use in CC Mode buffers.
  27. ;; Otoh, undocumented functions and functions with the documentation
  28. ;; in comments are considered purely internal and can change semantics
  29. ;; or even disappear in the future.
  30. ;;
  31. ;; (This policy applies to CC Mode as a whole, not just this file. It
  32. ;; probably also applies to many other Emacs packages, but here it's
  33. ;; clearly spelled out.)
  34. ;; Hidden buffer changes
  35. ;;
  36. ;; Various functions in CC Mode use text properties for caching and
  37. ;; syntactic markup purposes, and those of them that might modify such
  38. ;; properties but still don't modify the buffer in a visible way are
  39. ;; said to do "hidden buffer changes". They should be used within
  40. ;; `c-save-buffer-state' or a similar function that saves and restores
  41. ;; buffer modifiedness, disables buffer change hooks, etc.
  42. ;;
  43. ;; Interactive functions are assumed to not do hidden buffer changes,
  44. ;; except in the specific parts of them that do real changes.
  45. ;;
  46. ;; Lineup functions are assumed to do hidden buffer changes. They
  47. ;; must not do real changes, though.
  48. ;;
  49. ;; All other functions that do hidden buffer changes have that noted
  50. ;; in their doc string or comment.
  51. ;;
  52. ;; The intention with this system is to avoid wrapping every leaf
  53. ;; function that do hidden buffer changes inside
  54. ;; `c-save-buffer-state'. It should be used as near the top of the
  55. ;; interactive functions as possible.
  56. ;;
  57. ;; Functions called during font locking are allowed to do hidden
  58. ;; buffer changes since the font-lock package run them in a context
  59. ;; similar to `c-save-buffer-state' (in fact, that function is heavily
  60. ;; inspired by `save-buffer-state' in the font-lock package).
  61. ;; Use of text properties
  62. ;;
  63. ;; CC Mode uses several text properties internally to mark up various
  64. ;; positions, e.g. to improve speed and to eliminate glitches in
  65. ;; interactive refontification.
  66. ;;
  67. ;; Note: This doc is for internal use only. Other packages should not
  68. ;; assume that these text properties are used as described here.
  69. ;;
  70. ;; 'category
  71. ;; Used for "indirection". With its help, some other property can
  72. ;; be cheaply and easily switched on or off everywhere it occurs.
  73. ;;
  74. ;; 'syntax-table
  75. ;; Used to modify the syntax of some characters. It is used to
  76. ;; mark the "<" and ">" of angle bracket parens with paren syntax, and
  77. ;; to "hide" obtrusive characters in preprocessor lines.
  78. ;;
  79. ;; This property is used on single characters and is therefore
  80. ;; always treated as front and rear nonsticky (or start and end open
  81. ;; in XEmacs vocabulary). It's therefore installed on
  82. ;; `text-property-default-nonsticky' if that variable exists (Emacs
  83. ;; >= 21).
  84. ;;
  85. ;; 'c-is-sws and 'c-in-sws
  86. ;; Used by `c-forward-syntactic-ws' and `c-backward-syntactic-ws' to
  87. ;; speed them up. See the comment blurb before `c-put-is-sws'
  88. ;; below for further details.
  89. ;;
  90. ;; 'c-type
  91. ;; This property is used on single characters to mark positions with
  92. ;; special syntactic relevance of various sorts. Its primary use is
  93. ;; to avoid glitches when multiline constructs are refontified
  94. ;; interactively (on font lock decoration level 3). It's cleared in
  95. ;; a region before it's fontified and is then put on relevant chars
  96. ;; in that region as they are encountered during the fontification.
  97. ;; The value specifies the kind of position:
  98. ;;
  99. ;; 'c-decl-arg-start
  100. ;; Put on the last char of the token preceding each declaration
  101. ;; inside a declaration style arglist (typically in a function
  102. ;; prototype).
  103. ;;
  104. ;; 'c-decl-end
  105. ;; Put on the last char of the token preceding a declaration.
  106. ;; This is used in cases where declaration boundaries can't be
  107. ;; recognized simply by looking for a token like ";" or "}".
  108. ;; `c-type-decl-end-used' must be set if this is used (see also
  109. ;; `c-find-decl-spots').
  110. ;;
  111. ;; 'c-<>-arg-sep
  112. ;; Put on the commas that separate arguments in angle bracket
  113. ;; arglists like C++ template arglists.
  114. ;;
  115. ;; 'c-decl-id-start and 'c-decl-type-start
  116. ;; Put on the last char of the token preceding each declarator
  117. ;; in the declarator list of a declaration. They are also used
  118. ;; between the identifiers cases like enum declarations.
  119. ;; 'c-decl-type-start is used when the declarators are types,
  120. ;; 'c-decl-id-start otherwise.
  121. ;;
  122. ;; 'c-awk-NL-prop
  123. ;; Used in AWK mode to mark the various kinds of newlines. See
  124. ;; cc-awk.el.
  125. ;;; Code:
  126. (eval-when-compile
  127. (let ((load-path
  128. (if (and (boundp 'byte-compile-dest-file)
  129. (stringp byte-compile-dest-file))
  130. (cons (file-name-directory byte-compile-dest-file) load-path)
  131. load-path)))
  132. (load "cc-bytecomp" nil t)))
  133. (cc-require 'cc-defs)
  134. (cc-require-when-compile 'cc-langs)
  135. (cc-require 'cc-vars)
  136. ;; Silence the compiler.
  137. (cc-bytecomp-defun buffer-syntactic-context) ; XEmacs
  138. ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
  139. (defmacro c-declare-lang-variables ()
  140. `(progn
  141. ,@(apply 'nconc
  142. (mapcar (lambda (init)
  143. `(,(if (elt init 2)
  144. `(defvar ,(car init) nil ,(elt init 2))
  145. `(defvar ,(car init) nil))
  146. (make-variable-buffer-local ',(car init))))
  147. (cdr c-lang-variable-inits)))))
  148. (c-declare-lang-variables)
  149. ;;; Internal state variables.
  150. ;; Internal state of hungry delete key feature
  151. (defvar c-hungry-delete-key nil)
  152. (make-variable-buffer-local 'c-hungry-delete-key)
  153. ;; The electric flag (toggled by `c-toggle-electric-state').
  154. ;; If t, electric actions (like automatic reindentation, and (if
  155. ;; c-auto-newline is also set) auto newlining) will happen when an electric
  156. ;; key like `{' is pressed (or an electric keyword like `else').
  157. (defvar c-electric-flag t)
  158. (make-variable-buffer-local 'c-electric-flag)
  159. ;; Internal state of auto newline feature.
  160. (defvar c-auto-newline nil)
  161. (make-variable-buffer-local 'c-auto-newline)
  162. ;; Included in the mode line to indicate the active submodes.
  163. ;; (defvar c-submode-indicators nil)
  164. ;; (make-variable-buffer-local 'c-submode-indicators)
  165. (defun c-calculate-state (arg prevstate)
  166. ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
  167. ;; arg is nil or zero, toggle the state. If arg is negative, turn
  168. ;; the state off, and if arg is positive, turn the state on
  169. (if (or (not arg)
  170. (zerop (setq arg (prefix-numeric-value arg))))
  171. (not prevstate)
  172. (> arg 0)))
  173. ;; Basic handling of preprocessor directives.
  174. ;; This is a dynamically bound cache used together with
  175. ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
  176. ;; works as long as point doesn't cross a macro boundary.
  177. (defvar c-macro-start 'unknown)
  178. (defsubst c-query-and-set-macro-start ()
  179. (if (symbolp c-macro-start)
  180. (setq c-macro-start (save-excursion
  181. (c-save-buffer-state ()
  182. (and (c-beginning-of-macro)
  183. (point)))))
  184. c-macro-start))
  185. (defsubst c-query-macro-start ()
  186. (if (symbolp c-macro-start)
  187. (save-excursion
  188. (c-save-buffer-state ()
  189. (and (c-beginning-of-macro)
  190. (point))))
  191. c-macro-start))
  192. ;; One element macro cache to cope with continual movement within very large
  193. ;; CPP macros.
  194. (defvar c-macro-cache nil)
  195. (make-variable-buffer-local 'c-macro-cache)
  196. ;; Nil or cons of the bounds of the most recent CPP form probed by
  197. ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
  198. ;; The cdr will be nil if we know only the start of the CPP form.
  199. (defvar c-macro-cache-start-pos nil)
  200. (make-variable-buffer-local 'c-macro-cache-start-pos)
  201. ;; The starting position from where we determined `c-macro-cache'.
  202. (defvar c-macro-cache-syntactic nil)
  203. (make-variable-buffer-local 'c-macro-cache-syntactic)
  204. ;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a
  205. ;; syntactic end of macro, not merely an apparent one.
  206. (defun c-invalidate-macro-cache (beg end)
  207. ;; Called from a before-change function. If the change region is before or
  208. ;; in the macro characterized by `c-macro-cache' etc., nullify it
  209. ;; appropriately. BEG and END are the standard before-change-functions
  210. ;; parameters. END isn't used.
  211. (cond
  212. ((null c-macro-cache))
  213. ((< beg (car c-macro-cache))
  214. (setq c-macro-cache nil
  215. c-macro-cache-start-pos nil
  216. c-macro-cache-syntactic nil))
  217. ((and (cdr c-macro-cache)
  218. (< beg (cdr c-macro-cache)))
  219. (setcdr c-macro-cache nil)
  220. (setq c-macro-cache-start-pos beg
  221. c-macro-cache-syntactic nil))))
  222. (defun c-beginning-of-macro (&optional lim)
  223. "Go to the beginning of a preprocessor directive.
  224. Leave point at the beginning of the directive and return t if in one,
  225. otherwise return nil and leave point unchanged.
  226. Note that this function might do hidden buffer changes. See the
  227. comment at the start of cc-engine.el for more info."
  228. (let ((here (point)))
  229. (when c-opt-cpp-prefix
  230. (if (and (car c-macro-cache)
  231. (>= (point) (car c-macro-cache))
  232. (or (and (cdr c-macro-cache)
  233. (<= (point) (cdr c-macro-cache)))
  234. (<= (point) c-macro-cache-start-pos)))
  235. (unless (< (car c-macro-cache) (or lim (point-min)))
  236. (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
  237. (setq c-macro-cache-start-pos
  238. (max c-macro-cache-start-pos here))
  239. t))
  240. (setq c-macro-cache nil
  241. c-macro-cache-start-pos nil
  242. c-macro-cache-syntactic nil)
  243. (save-restriction
  244. (if lim (narrow-to-region lim (point-max)))
  245. (beginning-of-line)
  246. (while (eq (char-before (1- (point))) ?\\)
  247. (forward-line -1))
  248. (back-to-indentation)
  249. (if (and (<= (point) here)
  250. (looking-at c-opt-cpp-start))
  251. (progn
  252. (setq c-macro-cache (cons (point) nil)
  253. c-macro-cache-start-pos here)
  254. t)
  255. (goto-char here)
  256. nil))))))
  257. (defun c-end-of-macro ()
  258. "Go to the end of a preprocessor directive.
  259. More accurately, move the point to the end of the closest following
  260. line that doesn't end with a line continuation backslash - no check is
  261. done that the point is inside a cpp directive to begin with.
  262. Note that this function might do hidden buffer changes. See the
  263. comment at the start of cc-engine.el for more info."
  264. (if (and (cdr c-macro-cache)
  265. (<= (point) (cdr c-macro-cache))
  266. (>= (point) (car c-macro-cache)))
  267. (goto-char (cdr c-macro-cache))
  268. (unless (and (car c-macro-cache)
  269. (<= (point) c-macro-cache-start-pos)
  270. (>= (point) (car c-macro-cache)))
  271. (setq c-macro-cache nil
  272. c-macro-cache-start-pos nil
  273. c-macro-cache-syntactic nil))
  274. (while (progn
  275. (end-of-line)
  276. (when (and (eq (char-before) ?\\)
  277. (not (eobp)))
  278. (forward-char)
  279. t)))
  280. (when (car c-macro-cache)
  281. (setcdr c-macro-cache (point)))))
  282. (defun c-syntactic-end-of-macro ()
  283. ;; Go to the end of a CPP directive, or a "safe" pos just before.
  284. ;;
  285. ;; This is normally the end of the next non-escaped line. A "safe"
  286. ;; position is one not within a string or comment. (The EOL on a line
  287. ;; comment is NOT "safe").
  288. ;;
  289. ;; This function must only be called from the beginning of a CPP construct.
  290. ;;
  291. ;; Note that this function might do hidden buffer changes. See the comment
  292. ;; at the start of cc-engine.el for more info.
  293. (let* ((here (point))
  294. (there (progn (c-end-of-macro) (point)))
  295. s)
  296. (unless c-macro-cache-syntactic
  297. (setq s (parse-partial-sexp here there))
  298. (while (and (or (nth 3 s) ; in a string
  299. (nth 4 s)) ; in a comment (maybe at end of line comment)
  300. (> there here)) ; No infinite loops, please.
  301. (setq there (1- (nth 8 s)))
  302. (setq s (parse-partial-sexp here there)))
  303. (setq c-macro-cache-syntactic (car c-macro-cache)))
  304. (point)))
  305. (defun c-forward-over-cpp-define-id ()
  306. ;; Assuming point is at the "#" that introduces a preprocessor
  307. ;; directive, it's moved forward to the end of the identifier which is
  308. ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
  309. ;; is returned in this case, in all other cases nil is returned and
  310. ;; point isn't moved.
  311. ;;
  312. ;; This function might do hidden buffer changes.
  313. (when (and c-opt-cpp-macro-define-id
  314. (looking-at c-opt-cpp-macro-define-id))
  315. (goto-char (match-end 0))))
  316. (defun c-forward-to-cpp-define-body ()
  317. ;; Assuming point is at the "#" that introduces a preprocessor
  318. ;; directive, it's moved forward to the start of the definition body
  319. ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
  320. ;; specifies). Non-nil is returned in this case, in all other cases
  321. ;; nil is returned and point isn't moved.
  322. ;;
  323. ;; This function might do hidden buffer changes.
  324. (when (and c-opt-cpp-macro-define-start
  325. (looking-at c-opt-cpp-macro-define-start)
  326. (not (= (match-end 0) (c-point 'eol))))
  327. (goto-char (match-end 0))))
  328. ;;; Basic utility functions.
  329. (defun c-syntactic-content (from to paren-level)
  330. ;; Return the given region as a string where all syntactic
  331. ;; whitespace is removed or, where necessary, replaced with a single
  332. ;; space. If PAREN-LEVEL is given then all parens in the region are
  333. ;; collapsed to "()", "[]" etc.
  334. ;;
  335. ;; This function might do hidden buffer changes.
  336. (save-excursion
  337. (save-restriction
  338. (narrow-to-region from to)
  339. (goto-char from)
  340. (let* ((parts (list nil)) (tail parts) pos in-paren)
  341. (while (re-search-forward c-syntactic-ws-start to t)
  342. (goto-char (setq pos (match-beginning 0)))
  343. (c-forward-syntactic-ws)
  344. (if (= (point) pos)
  345. (forward-char)
  346. (when paren-level
  347. (save-excursion
  348. (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
  349. pos (point))))
  350. (if (and (> pos from)
  351. (< (point) to)
  352. (looking-at "\\w\\|\\s_")
  353. (save-excursion
  354. (goto-char (1- pos))
  355. (looking-at "\\w\\|\\s_")))
  356. (progn
  357. (setcdr tail (list (buffer-substring-no-properties from pos)
  358. " "))
  359. (setq tail (cddr tail)))
  360. (setcdr tail (list (buffer-substring-no-properties from pos)))
  361. (setq tail (cdr tail)))
  362. (when in-paren
  363. (when (= (car (parse-partial-sexp pos to -1)) -1)
  364. (setcdr tail (list (buffer-substring-no-properties
  365. (1- (point)) (point))))
  366. (setq tail (cdr tail))))
  367. (setq from (point))))
  368. (setcdr tail (list (buffer-substring-no-properties from to)))
  369. (apply 'concat (cdr parts))))))
  370. (defun c-shift-line-indentation (shift-amt)
  371. ;; Shift the indentation of the current line with the specified
  372. ;; amount (positive inwards). The buffer is modified only if
  373. ;; SHIFT-AMT isn't equal to zero.
  374. (let ((pos (- (point-max) (point)))
  375. (c-macro-start c-macro-start)
  376. tmp-char-inserted)
  377. (if (zerop shift-amt)
  378. nil
  379. ;; If we're on an empty line inside a macro, we take the point
  380. ;; to be at the current indentation and shift it to the
  381. ;; appropriate column. This way we don't treat the extra
  382. ;; whitespace out to the line continuation as indentation.
  383. (when (and (c-query-and-set-macro-start)
  384. (looking-at "[ \t]*\\\\$")
  385. (save-excursion
  386. (skip-chars-backward " \t")
  387. (bolp)))
  388. (insert ?x)
  389. (backward-char)
  390. (setq tmp-char-inserted t))
  391. (unwind-protect
  392. (let ((col (current-indentation)))
  393. (delete-region (c-point 'bol) (c-point 'boi))
  394. (beginning-of-line)
  395. (indent-to (+ col shift-amt)))
  396. (when tmp-char-inserted
  397. (delete-char 1))))
  398. ;; If initial point was within line's indentation and we're not on
  399. ;; a line with a line continuation in a macro, position after the
  400. ;; indentation. Else stay at same point in text.
  401. (if (and (< (point) (c-point 'boi))
  402. (not tmp-char-inserted))
  403. (back-to-indentation)
  404. (if (> (- (point-max) pos) (point))
  405. (goto-char (- (point-max) pos))))))
  406. (defsubst c-keyword-sym (keyword)
  407. ;; Return non-nil if the string KEYWORD is a known keyword. More
  408. ;; precisely, the value is the symbol for the keyword in
  409. ;; `c-keywords-obarray'.
  410. (intern-soft keyword c-keywords-obarray))
  411. (defsubst c-keyword-member (keyword-sym lang-constant)
  412. ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
  413. ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
  414. ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
  415. ;; nil then the result is nil.
  416. (get keyword-sym lang-constant))
  417. ;; String syntax chars, suitable for skip-syntax-(forward|backward).
  418. (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
  419. "\"|"
  420. "\""))
  421. ;; Regexp matching string limit syntax.
  422. (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
  423. "\\s\"\\|\\s|"
  424. "\\s\""))
  425. ;; Regexp matching WS followed by string limit syntax.
  426. (defconst c-ws*-string-limit-regexp
  427. (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
  428. ;; Holds formatted error strings for the few cases where parse errors
  429. ;; are reported.
  430. (defvar c-parsing-error nil)
  431. (make-variable-buffer-local 'c-parsing-error)
  432. (defun c-echo-parsing-error (&optional quiet)
  433. (when (and c-report-syntactic-errors c-parsing-error (not quiet))
  434. (c-benign-error "%s" c-parsing-error))
  435. c-parsing-error)
  436. ;; Faces given to comments and string literals. This is used in some
  437. ;; situations to speed up recognition; it isn't mandatory that font
  438. ;; locking is in use. This variable is extended with the face in
  439. ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
  440. (defvar c-literal-faces
  441. (append '(font-lock-comment-face font-lock-string-face)
  442. (when (facep 'font-lock-comment-delimiter-face)
  443. ;; New in Emacs 22.
  444. '(font-lock-comment-delimiter-face))))
  445. (defsubst c-put-c-type-property (pos value)
  446. ;; Put a c-type property with the given value at POS.
  447. (c-put-char-property pos 'c-type value))
  448. (defun c-clear-c-type-property (from to value)
  449. ;; Remove all occurrences of the c-type property that has the given
  450. ;; value in the region between FROM and TO. VALUE is assumed to not
  451. ;; be nil.
  452. ;;
  453. ;; Note: This assumes that c-type is put on single chars only; it's
  454. ;; very inefficient if matching properties cover large regions.
  455. (save-excursion
  456. (goto-char from)
  457. (while (progn
  458. (when (eq (get-text-property (point) 'c-type) value)
  459. (c-clear-char-property (point) 'c-type))
  460. (goto-char (next-single-property-change (point) 'c-type nil to))
  461. (< (point) to)))))
  462. ;; Some debug tools to visualize various special positions. This
  463. ;; debug code isn't as portable as the rest of CC Mode.
  464. (cc-bytecomp-defun overlays-in)
  465. (cc-bytecomp-defun overlay-get)
  466. (cc-bytecomp-defun overlay-start)
  467. (cc-bytecomp-defun overlay-end)
  468. (cc-bytecomp-defun delete-overlay)
  469. (cc-bytecomp-defun overlay-put)
  470. (cc-bytecomp-defun make-overlay)
  471. (defun c-debug-add-face (beg end face)
  472. (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
  473. (while overlays
  474. (setq overlay (car overlays)
  475. overlays (cdr overlays))
  476. (when (eq (overlay-get overlay 'face) face)
  477. (setq beg (min beg (overlay-start overlay))
  478. end (max end (overlay-end overlay)))
  479. (delete-overlay overlay)))
  480. (overlay-put (make-overlay beg end) 'face face)))
  481. (defun c-debug-remove-face (beg end face)
  482. (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
  483. (ol-beg beg) (ol-end end))
  484. (while overlays
  485. (setq overlay (car overlays)
  486. overlays (cdr overlays))
  487. (when (eq (overlay-get overlay 'face) face)
  488. (setq ol-beg (min ol-beg (overlay-start overlay))
  489. ol-end (max ol-end (overlay-end overlay)))
  490. (delete-overlay overlay)))
  491. (when (< ol-beg beg)
  492. (overlay-put (make-overlay ol-beg beg) 'face face))
  493. (when (> ol-end end)
  494. (overlay-put (make-overlay end ol-end) 'face face))))
  495. ;; `c-beginning-of-statement-1' and accompanying stuff.
  496. ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
  497. ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
  498. ;; better way should be implemented, but this will at least shut up
  499. ;; the byte compiler.
  500. (defvar c-maybe-labelp)
  501. ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
  502. ;; Macros used internally in c-beginning-of-statement-1 for the
  503. ;; automaton actions.
  504. (defmacro c-bos-push-state ()
  505. '(setq stack (cons (cons state saved-pos)
  506. stack)))
  507. (defmacro c-bos-pop-state (&optional do-if-done)
  508. `(if (setq state (car (car stack))
  509. saved-pos (cdr (car stack))
  510. stack (cdr stack))
  511. t
  512. ,do-if-done
  513. (throw 'loop nil)))
  514. (defmacro c-bos-pop-state-and-retry ()
  515. '(throw 'loop (setq state (car (car stack))
  516. saved-pos (cdr (car stack))
  517. ;; Throw nil if stack is empty, else throw non-nil.
  518. stack (cdr stack))))
  519. (defmacro c-bos-save-pos ()
  520. '(setq saved-pos (vector pos tok ptok pptok)))
  521. (defmacro c-bos-restore-pos ()
  522. '(unless (eq (elt saved-pos 0) start)
  523. (setq pos (elt saved-pos 0)
  524. tok (elt saved-pos 1)
  525. ptok (elt saved-pos 2)
  526. pptok (elt saved-pos 3))
  527. (goto-char pos)
  528. (setq sym nil)))
  529. (defmacro c-bos-save-error-info (missing got)
  530. `(setq saved-pos (vector pos ,missing ,got)))
  531. (defmacro c-bos-report-error ()
  532. '(unless noerror
  533. (setq c-parsing-error
  534. (format "No matching `%s' found for `%s' on line %d"
  535. (elt saved-pos 1)
  536. (elt saved-pos 2)
  537. (1+ (count-lines (point-min)
  538. (c-point 'bol (elt saved-pos 0))))))))
  539. (defun c-beginning-of-statement-1 (&optional lim ignore-labels
  540. noerror comma-delim)
  541. "Move to the start of the current statement or declaration, or to
  542. the previous one if already at the beginning of one. Only
  543. statements/declarations on the same level are considered, i.e. don't
  544. move into or out of sexps (not even normal expression parentheses).
  545. If point is already at the earliest statement within braces or parens,
  546. this function doesn't move back into any whitespace preceding it; it
  547. returns 'same in this case.
  548. Stop at statement continuation tokens like \"else\", \"catch\",
  549. \"finally\" and the \"while\" in \"do ... while\" if the start point
  550. is within the continuation. If starting at such a token, move to the
  551. corresponding statement start. If at the beginning of a statement,
  552. move to the closest containing statement if there is any. This might
  553. also stop at a continuation clause.
  554. Labels are treated as part of the following statements if
  555. IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
  556. statement start keyword.) Otherwise, each label is treated as a
  557. separate statement.
  558. Macros are ignored \(i.e. skipped over) unless point is within one, in
  559. which case the content of the macro is treated as normal code. Aside
  560. from any normal statement starts found in it, stop at the first token
  561. of the content in the macro, i.e. the expression of an \"#if\" or the
  562. start of the definition in a \"#define\". Also stop at start of
  563. macros before leaving them.
  564. Return:
  565. 'label if stopped at a label or \"case...:\" or \"default:\";
  566. 'same if stopped at the beginning of the current statement;
  567. 'up if stepped to a containing statement;
  568. 'previous if stepped to a preceding statement;
  569. 'beginning if stepped from a statement continuation clause to
  570. its start clause; or
  571. 'macro if stepped to a macro start.
  572. Note that 'same and not 'label is returned if stopped at the same
  573. label without crossing the colon character.
  574. LIM may be given to limit the search. If the search hits the limit,
  575. point will be left at the closest following token, or at the start
  576. position if that is less ('same is returned in this case).
  577. NOERROR turns off error logging to `c-parsing-error'.
  578. Normally only ';' and virtual semicolons are considered to delimit
  579. statements, but if COMMA-DELIM is non-nil then ',' is treated
  580. as a delimiter too.
  581. Note that this function might do hidden buffer changes. See the
  582. comment at the start of cc-engine.el for more info."
  583. ;; The bulk of this function is a pushdown automaton that looks at statement
  584. ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
  585. ;; purpose is to keep track of nested statements, ensuring that such
  586. ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
  587. ;; does with nested braces/brackets/parentheses).
  588. ;;
  589. ;; Note: The position of a boundary is the following token.
  590. ;;
  591. ;; Beginning with the current token (the one following point), move back one
  592. ;; sexp at a time (where a sexp is, more or less, either a token or the
  593. ;; entire contents of a brace/bracket/paren pair). Each time a statement
  594. ;; boundary is crossed or a "while"-like token is found, update the state of
  595. ;; the PDA. Stop at the beginning of a statement when the stack (holding
  596. ;; nested statement info) is empty and the position has been moved.
  597. ;;
  598. ;; The following variables constitute the PDA:
  599. ;;
  600. ;; sym: This is either the "while"-like token (e.g. 'for) we've just
  601. ;; scanned back over, 'boundary if we've just gone back over a
  602. ;; statement boundary, or nil otherwise.
  603. ;; state: takes one of the values (nil else else-boundary while
  604. ;; while-boundary catch catch-boundary).
  605. ;; nil means "no "while"-like token yet scanned".
  606. ;; 'else, for example, means "just gone back over an else".
  607. ;; 'else-boundary means "just gone back over a statement boundary
  608. ;; immediately after having gone back over an else".
  609. ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
  610. ;; of error reporting information.
  611. ;; stack: The stack onto which the PDA pushes its state. Each entry
  612. ;; consists of a saved value of state and saved-pos. An entry is
  613. ;; pushed when we move back over a "continuation" token (e.g. else)
  614. ;; and popped when we encounter the corresponding opening token
  615. ;; (e.g. if).
  616. ;;
  617. ;;
  618. ;; The following diagram briefly outlines the PDA.
  619. ;;
  620. ;; Common state:
  621. ;; "else": Push state, goto state `else'.
  622. ;; "while": Push state, goto state `while'.
  623. ;; "catch" or "finally": Push state, goto state `catch'.
  624. ;; boundary: Pop state.
  625. ;; other: Do nothing special.
  626. ;;
  627. ;; State `else':
  628. ;; boundary: Goto state `else-boundary'.
  629. ;; other: Error, pop state, retry token.
  630. ;;
  631. ;; State `else-boundary':
  632. ;; "if": Pop state.
  633. ;; boundary: Error, pop state.
  634. ;; other: See common state.
  635. ;;
  636. ;; State `while':
  637. ;; boundary: Save position, goto state `while-boundary'.
  638. ;; other: Pop state, retry token.
  639. ;;
  640. ;; State `while-boundary':
  641. ;; "do": Pop state.
  642. ;; boundary: Restore position if it's not at start, pop state. [*see below]
  643. ;; other: See common state.
  644. ;;
  645. ;; State `catch':
  646. ;; boundary: Goto state `catch-boundary'.
  647. ;; other: Error, pop state, retry token.
  648. ;;
  649. ;; State `catch-boundary':
  650. ;; "try": Pop state.
  651. ;; "catch": Goto state `catch'.
  652. ;; boundary: Error, pop state.
  653. ;; other: See common state.
  654. ;;
  655. ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
  656. ;; searching for a "do" which would have opened a do-while. If we didn't
  657. ;; find it, we discard the analysis done since the "while", go back to this
  658. ;; token in the buffer and restart the scanning there, this time WITHOUT
  659. ;; pushing the 'while state onto the stack.
  660. ;;
  661. ;; In addition to the above there is some special handling of labels
  662. ;; and macros.
  663. (let ((case-fold-search nil)
  664. (start (point))
  665. macro-start
  666. (delims (if comma-delim '(?\; ?,) '(?\;)))
  667. (c-stmt-delim-chars (if comma-delim
  668. c-stmt-delim-chars-with-comma
  669. c-stmt-delim-chars))
  670. c-in-literal-cache c-maybe-labelp after-case:-pos saved
  671. ;; Current position.
  672. pos
  673. ;; Position of last stmt boundary character (e.g. ;).
  674. boundary-pos
  675. ;; The position of the last sexp or bound that follows the
  676. ;; first found colon, i.e. the start of the nonlabel part of
  677. ;; the statement. It's `start' if a colon is found just after
  678. ;; the start.
  679. after-labels-pos
  680. ;; Like `after-labels-pos', but the first such position inside
  681. ;; a label, i.e. the start of the last label before the start
  682. ;; of the nonlabel part of the statement.
  683. last-label-pos
  684. ;; The last position where a label is possible provided the
  685. ;; statement started there. It's nil as long as no invalid
  686. ;; label content has been found (according to
  687. ;; `c-nonlabel-token-key'). It's `start' if no valid label
  688. ;; content was found in the label. Note that we might still
  689. ;; regard it a label if it starts with `c-label-kwds'.
  690. label-good-pos
  691. ;; Putative positions of the components of a bitfield declaration,
  692. ;; e.g. "int foo : NUM_FOO_BITS ;"
  693. bitfield-type-pos bitfield-id-pos bitfield-size-pos
  694. ;; Symbol just scanned back over (e.g. 'while or 'boundary).
  695. ;; See above.
  696. sym
  697. ;; Current state in the automaton. See above.
  698. state
  699. ;; Current saved positions. See above.
  700. saved-pos
  701. ;; Stack of conses (state . saved-pos).
  702. stack
  703. ;; Regexp which matches "for", "if", etc.
  704. (cond-key (or c-opt-block-stmt-key
  705. "\\<\\>")) ; Matches nothing.
  706. ;; Return value.
  707. (ret 'same)
  708. ;; Positions of the last three sexps or bounds we've stopped at.
  709. tok ptok pptok)
  710. (save-restriction
  711. (if lim (narrow-to-region lim (point-max)))
  712. (if (save-excursion
  713. (and (c-beginning-of-macro)
  714. (/= (point) start)))
  715. (setq macro-start (point)))
  716. ;; Try to skip back over unary operator characters, to register
  717. ;; that we've moved.
  718. (while (progn
  719. (setq pos (point))
  720. (c-backward-syntactic-ws)
  721. ;; Protect post-++/-- operators just before a virtual semicolon.
  722. (and (not (c-at-vsemi-p))
  723. (/= (skip-chars-backward "-+!*&~@`#") 0))))
  724. ;; Skip back over any semicolon here. If it was a bare semicolon, we're
  725. ;; done. Later on we ignore the boundaries for statements that don't
  726. ;; contain any sexp. The only thing that is affected is that the error
  727. ;; checking is a little less strict, and we really don't bother.
  728. (if (and (memq (char-before) delims)
  729. (progn (forward-char -1)
  730. (setq saved (point))
  731. (c-backward-syntactic-ws)
  732. (or (memq (char-before) delims)
  733. (memq (char-before) '(?: nil))
  734. (eq (char-syntax (char-before)) ?\()
  735. (c-at-vsemi-p))))
  736. (setq ret 'previous
  737. pos saved)
  738. ;; Begin at start and not pos to detect macros if we stand
  739. ;; directly after the #.
  740. (goto-char start)
  741. (if (looking-at "\\<\\|\\W")
  742. ;; Record this as the first token if not starting inside it.
  743. (setq tok start))
  744. ;; The following while loop goes back one sexp (balanced parens,
  745. ;; etc. with contents, or symbol or suchlike) each iteration. This
  746. ;; movement is accomplished with a call to c-backward-sexp approx 170
  747. ;; lines below.
  748. ;;
  749. ;; The loop is exited only by throwing nil to the (catch 'loop ...):
  750. ;; 1. On reaching the start of a macro;
  751. ;; 2. On having passed a stmt boundary with the PDA stack empty;
  752. ;; 3. On reaching the start of an Objective C method def;
  753. ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
  754. ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
  755. (while
  756. (catch 'loop ;; Throw nil to break, non-nil to continue.
  757. (cond
  758. ;; Are we in a macro, just after the opening #?
  759. ((save-excursion
  760. (and macro-start ; Always NIL for AWK.
  761. (progn (skip-chars-backward " \t")
  762. (eq (char-before) ?#))
  763. (progn (setq saved (1- (point)))
  764. (beginning-of-line)
  765. (not (eq (char-before (1- (point))) ?\\)))
  766. (looking-at c-opt-cpp-start)
  767. (progn (skip-chars-forward " \t")
  768. (eq (point) saved))))
  769. (goto-char saved)
  770. (if (and (c-forward-to-cpp-define-body)
  771. (progn (c-forward-syntactic-ws start)
  772. (< (point) start)))
  773. ;; Stop at the first token in the content of the macro.
  774. (setq pos (point)
  775. ignore-labels t) ; Avoid the label check on exit.
  776. (setq pos saved
  777. ret 'macro
  778. ignore-labels t))
  779. (throw 'loop nil)) ; 1. Start of macro.
  780. ;; Do a round through the automaton if we've just passed a
  781. ;; statement boundary or passed a "while"-like token.
  782. ((or sym
  783. (and (looking-at cond-key)
  784. (setq sym (intern (match-string 1)))))
  785. (when (and (< pos start) (null stack))
  786. (throw 'loop nil)) ; 2. Statement boundary.
  787. ;; The PDA state handling.
  788. ;;
  789. ;; Refer to the description of the PDA in the opening
  790. ;; comments. In the following OR form, the first leaf
  791. ;; attempts to handles one of the specific actions detailed
  792. ;; (e.g., finding token "if" whilst in state `else-boundary').
  793. ;; We drop through to the second leaf (which handles common
  794. ;; state) if no specific handler is found in the first cond.
  795. ;; If a parsing error is detected (e.g. an "else" with no
  796. ;; preceding "if"), we throw to the enclosing catch.
  797. ;;
  798. ;; Note that the (eq state 'else) means
  799. ;; "we've just passed an else", NOT "we're looking for an
  800. ;; else".
  801. (or (cond
  802. ((eq state 'else)
  803. (if (eq sym 'boundary)
  804. (setq state 'else-boundary)
  805. (c-bos-report-error)
  806. (c-bos-pop-state-and-retry)))
  807. ((eq state 'else-boundary)
  808. (cond ((eq sym 'if)
  809. (c-bos-pop-state (setq ret 'beginning)))
  810. ((eq sym 'boundary)
  811. (c-bos-report-error)
  812. (c-bos-pop-state))))
  813. ((eq state 'while)
  814. (if (and (eq sym 'boundary)
  815. ;; Since this can cause backtracking we do a
  816. ;; little more careful analysis to avoid it:
  817. ;; If there's a label in front of the while
  818. ;; it can't be part of a do-while.
  819. (not after-labels-pos))
  820. (progn (c-bos-save-pos)
  821. (setq state 'while-boundary))
  822. (c-bos-pop-state-and-retry))) ; Can't be a do-while
  823. ((eq state 'while-boundary)
  824. (cond ((eq sym 'do)
  825. (c-bos-pop-state (setq ret 'beginning)))
  826. ((eq sym 'boundary) ; isn't a do-while
  827. (c-bos-restore-pos) ; the position of the while
  828. (c-bos-pop-state)))) ; no longer searching for do.
  829. ((eq state 'catch)
  830. (if (eq sym 'boundary)
  831. (setq state 'catch-boundary)
  832. (c-bos-report-error)
  833. (c-bos-pop-state-and-retry)))
  834. ((eq state 'catch-boundary)
  835. (cond
  836. ((eq sym 'try)
  837. (c-bos-pop-state (setq ret 'beginning)))
  838. ((eq sym 'catch)
  839. (setq state 'catch))
  840. ((eq sym 'boundary)
  841. (c-bos-report-error)
  842. (c-bos-pop-state)))))
  843. ;; This is state common. We get here when the previous
  844. ;; cond statement found no particular state handler.
  845. (cond ((eq sym 'boundary)
  846. ;; If we have a boundary at the start
  847. ;; position we push a frame to go to the
  848. ;; previous statement.
  849. (if (>= pos start)
  850. (c-bos-push-state)
  851. (c-bos-pop-state)))
  852. ((eq sym 'else)
  853. (c-bos-push-state)
  854. (c-bos-save-error-info 'if 'else)
  855. (setq state 'else))
  856. ((eq sym 'while)
  857. ;; Is this a real while, or a do-while?
  858. ;; The next `when' triggers unless we are SURE that
  859. ;; the `while' is not the tail end of a `do-while'.
  860. (when (or (not pptok)
  861. (memq (char-after pptok) delims)
  862. ;; The following kludge is to prevent
  863. ;; infinite recursion when called from
  864. ;; c-awk-after-if-for-while-condition-p,
  865. ;; or the like.
  866. (and (eq (point) start)
  867. (c-vsemi-status-unknown-p))
  868. (c-at-vsemi-p pptok))
  869. ;; Since this can cause backtracking we do a
  870. ;; little more careful analysis to avoid it: If
  871. ;; the while isn't followed by a (possibly
  872. ;; virtual) semicolon it can't be a do-while.
  873. (c-bos-push-state)
  874. (setq state 'while)))
  875. ((memq sym '(catch finally))
  876. (c-bos-push-state)
  877. (c-bos-save-error-info 'try sym)
  878. (setq state 'catch))))
  879. (when c-maybe-labelp
  880. ;; We're either past a statement boundary or at the
  881. ;; start of a statement, so throw away any label data
  882. ;; for the previous one.
  883. (setq after-labels-pos nil
  884. last-label-pos nil
  885. c-maybe-labelp nil))))
  886. ;; Step to the previous sexp, but not if we crossed a
  887. ;; boundary, since that doesn't consume an sexp.
  888. (if (eq sym 'boundary)
  889. (setq ret 'previous)
  890. ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
  891. ;; BACKWARDS THROUGH THE SOURCE.
  892. (c-backward-syntactic-ws)
  893. (let ((before-sws-pos (point))
  894. ;; The end position of the area to search for statement
  895. ;; barriers in this round.
  896. (maybe-after-boundary-pos pos))
  897. ;; Go back over exactly one logical sexp, taking proper
  898. ;; account of macros and escaped EOLs.
  899. (while
  900. (progn
  901. (unless (c-safe (c-backward-sexp) t)
  902. ;; Give up if we hit an unbalanced block. Since the
  903. ;; stack won't be empty the code below will report a
  904. ;; suitable error.
  905. (throw 'loop nil))
  906. (cond
  907. ;; Have we moved into a macro?
  908. ((and (not macro-start)
  909. (c-beginning-of-macro))
  910. ;; Have we crossed a statement boundary? If not,
  911. ;; keep going back until we find one or a "real" sexp.
  912. (and
  913. (save-excursion
  914. (c-end-of-macro)
  915. (not (c-crosses-statement-barrier-p
  916. (point) maybe-after-boundary-pos)))
  917. (setq maybe-after-boundary-pos (point))))
  918. ;; Have we just gone back over an escaped NL? This
  919. ;; doesn't count as a sexp.
  920. ((looking-at "\\\\$")))))
  921. ;; Have we crossed a statement boundary?
  922. (setq boundary-pos
  923. (cond
  924. ;; Are we at a macro beginning?
  925. ((and (not macro-start)
  926. c-opt-cpp-prefix
  927. (looking-at c-opt-cpp-prefix))
  928. (save-excursion
  929. (c-end-of-macro)
  930. (c-crosses-statement-barrier-p
  931. (point) maybe-after-boundary-pos)))
  932. ;; Just gone back over a brace block?
  933. ((and
  934. (eq (char-after) ?{)
  935. (not (c-looking-at-inexpr-block lim nil t)))
  936. (save-excursion
  937. (c-forward-sexp) (point)))
  938. ;; Just gone back over some paren block?
  939. ((looking-at "\\s\(")
  940. (save-excursion
  941. (goto-char (1+ (c-down-list-backward
  942. before-sws-pos)))
  943. (c-crosses-statement-barrier-p
  944. (point) maybe-after-boundary-pos)))
  945. ;; Just gone back over an ordinary symbol of some sort?
  946. (t (c-crosses-statement-barrier-p
  947. (point) maybe-after-boundary-pos))))
  948. (when boundary-pos
  949. (setq pptok ptok
  950. ptok tok
  951. tok boundary-pos
  952. sym 'boundary)
  953. ;; Like a C "continue". Analyze the next sexp.
  954. (throw 'loop t))))
  955. ;; ObjC method def?
  956. (when (and c-opt-method-key
  957. (setq saved (c-in-method-def-p)))
  958. (setq pos saved
  959. ignore-labels t) ; Avoid the label check on exit.
  960. (throw 'loop nil)) ; 3. ObjC method def.
  961. ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
  962. (if c-has-bitfields
  963. (cond
  964. ;; The : <size> and <id> fields?
  965. ((and (numberp c-maybe-labelp)
  966. (not bitfield-size-pos)
  967. (save-excursion
  968. (goto-char (or tok start))
  969. (not (looking-at c-keywords-regexp)))
  970. (not (looking-at c-keywords-regexp))
  971. (not (c-punctuation-in (point) c-maybe-labelp)))
  972. (setq bitfield-size-pos (or tok start)
  973. bitfield-id-pos (point)))
  974. ;; The <type> field?
  975. ((and bitfield-id-pos
  976. (not bitfield-type-pos))
  977. (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
  978. (not (looking-at c-not-primitive-type-keywords-regexp))
  979. (not (c-punctuation-in (point) tok)))
  980. (setq bitfield-type-pos (point))
  981. (setq bitfield-size-pos nil
  982. bitfield-id-pos nil)))))
  983. ;; Handle labels.
  984. (unless (eq ignore-labels t)
  985. (when (numberp c-maybe-labelp)
  986. ;; `c-crosses-statement-barrier-p' has found a colon, so we
  987. ;; might be in a label now. Have we got a real label
  988. ;; (including a case label) or something like C++'s "public:"?
  989. ;; A case label might use an expression rather than a token.
  990. (setq after-case:-pos (or tok start))
  991. (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
  992. ;; Catch C++'s inheritance construct "class foo : bar".
  993. (save-excursion
  994. (and
  995. (c-safe (c-backward-sexp) t)
  996. (looking-at c-nonlabel-token-2-key))))
  997. (setq c-maybe-labelp nil)
  998. (if after-labels-pos ; Have we already encountered a label?
  999. (if (not last-label-pos)
  1000. (setq last-label-pos (or tok start)))
  1001. (setq after-labels-pos (or tok start)))
  1002. (setq c-maybe-labelp t
  1003. label-good-pos nil))) ; bogus "label"
  1004. (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
  1005. ; been found.
  1006. (looking-at c-nonlabel-token-key)) ; e.g. "while :"
  1007. ;; We're in a potential label and it's the first
  1008. ;; time we've found something that isn't allowed in
  1009. ;; one.
  1010. (setq label-good-pos (or tok start))))
  1011. ;; We've moved back by a sexp, so update the token positions.
  1012. (setq sym nil
  1013. pptok ptok
  1014. ptok tok
  1015. tok (point)
  1016. pos tok) ; always non-nil
  1017. ) ; end of (catch loop ....)
  1018. ) ; end of sexp-at-a-time (while ....)
  1019. ;; If the stack isn't empty there might be errors to report.
  1020. (while stack
  1021. (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
  1022. (c-bos-report-error))
  1023. (setq saved-pos (cdr (car stack))
  1024. stack (cdr stack)))
  1025. (when (and (eq ret 'same)
  1026. (not (memq sym '(boundary ignore nil))))
  1027. ;; Need to investigate closer whether we've crossed
  1028. ;; between a substatement and its containing statement.
  1029. (if (setq saved (if (looking-at c-block-stmt-1-key)
  1030. ptok
  1031. pptok))
  1032. (cond ((> start saved) (setq pos saved))
  1033. ((= start saved) (setq ret 'up)))))
  1034. (when (and (not ignore-labels)
  1035. (eq c-maybe-labelp t)
  1036. (not (eq ret 'beginning))
  1037. after-labels-pos
  1038. (not bitfield-type-pos) ; Bitfields take precedence over labels.
  1039. (or (not label-good-pos)
  1040. (<= label-good-pos pos)
  1041. (progn
  1042. (goto-char (if (and last-label-pos
  1043. (< last-label-pos start))
  1044. last-label-pos
  1045. pos))
  1046. (looking-at c-label-kwds-regexp))))
  1047. ;; We're in a label. Maybe we should step to the statement
  1048. ;; after it.
  1049. (if (< after-labels-pos start)
  1050. (setq pos after-labels-pos)
  1051. (setq ret 'label)
  1052. (if (and last-label-pos (< last-label-pos start))
  1053. ;; Might have jumped over several labels. Go to the last one.
  1054. (setq pos last-label-pos)))))
  1055. ;; Have we got "case <expression>:"?
  1056. (goto-char pos)
  1057. (when (and after-case:-pos
  1058. (not (eq ret 'beginning))
  1059. (looking-at c-case-kwds-regexp))
  1060. (if (< after-case:-pos start)
  1061. (setq pos after-case:-pos))
  1062. (if (eq ret 'same)
  1063. (setq ret 'label)))
  1064. ;; Skip over the unary operators that can start the statement.
  1065. (while (progn
  1066. (c-backward-syntactic-ws)
  1067. ;; protect AWK post-inc/decrement operators, etc.
  1068. (and (not (c-at-vsemi-p (point)))
  1069. (/= (skip-chars-backward "-+!*&~@`#") 0)))
  1070. (setq pos (point)))
  1071. (goto-char pos)
  1072. ret)))
  1073. (defun c-punctuation-in (from to)
  1074. "Return non-nil if there is a non-comment non-macro punctuation character
  1075. between FROM and TO. FROM must not be in a string or comment. The returned
  1076. value is the position of the first such character."
  1077. (save-excursion
  1078. (goto-char from)
  1079. (let ((pos (point)))
  1080. (while (progn (skip-chars-forward c-symbol-chars to)
  1081. (c-forward-syntactic-ws to)
  1082. (> (point) pos))
  1083. (setq pos (point))))
  1084. (and (< (point) to) (point))))
  1085. (defun c-crosses-statement-barrier-p (from to)
  1086. "Return non-nil if buffer positions FROM to TO cross one or more
  1087. statement or declaration boundaries. The returned value is actually
  1088. the position of the earliest boundary char. FROM must not be within
  1089. a string or comment.
  1090. The variable `c-maybe-labelp' is set to the position of the first `:' that
  1091. might start a label (i.e. not part of `::' and not preceded by `?'). If a
  1092. single `?' is found, then `c-maybe-labelp' is cleared.
  1093. For AWK, a statement which is terminated by an EOL (not a \; or a }) is
  1094. regarded as having a \"virtual semicolon\" immediately after the last token on
  1095. the line. If this virtual semicolon is _at_ from, the function recognizes it.
  1096. Note that this function might do hidden buffer changes. See the
  1097. comment at the start of cc-engine.el for more info."
  1098. (let* ((skip-chars
  1099. ;; If the current language has CPP macros, insert # into skip-chars.
  1100. (if c-opt-cpp-symbol
  1101. (concat (substring c-stmt-delim-chars 0 1) ; "^"
  1102. c-opt-cpp-symbol ; usually "#"
  1103. (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
  1104. c-stmt-delim-chars))
  1105. (non-skip-list
  1106. (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
  1107. lit-range vsemi-pos)
  1108. (save-restriction
  1109. (widen)
  1110. (save-excursion
  1111. (catch 'done
  1112. (goto-char from)
  1113. (while (progn (skip-chars-forward
  1114. skip-chars
  1115. (min to (c-point 'bonl)))
  1116. (< (point) to))
  1117. (cond
  1118. ;; Virtual semicolon?
  1119. ((and (bolp)
  1120. (save-excursion
  1121. (progn
  1122. (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
  1123. (goto-char (car lit-range)))
  1124. (c-backward-syntactic-ws) ; ? put a limit here, maybe?
  1125. (setq vsemi-pos (point))
  1126. (c-at-vsemi-p))))
  1127. (throw 'done vsemi-pos))
  1128. ;; In a string/comment?
  1129. ((setq lit-range (c-literal-limits from))
  1130. (goto-char (cdr lit-range)))
  1131. ((eq (char-after) ?:)
  1132. (forward-char)
  1133. (if (and (eq (char-after) ?:)
  1134. (< (point) to))
  1135. ;; Ignore scope operators.
  1136. (forward-char)
  1137. (setq c-maybe-labelp (1- (point)))))
  1138. ((eq (char-after) ??)
  1139. ;; A question mark. Can't be a label, so stop
  1140. ;; looking for more : and ?.
  1141. (setq c-maybe-labelp nil
  1142. skip-chars (substring c-stmt-delim-chars 0 -2)))
  1143. ;; At a CPP construct?
  1144. ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol)
  1145. (save-excursion
  1146. (forward-line 0)
  1147. (looking-at c-opt-cpp-prefix)))
  1148. (c-end-of-macro))
  1149. ((memq (char-after) non-skip-list)
  1150. (throw 'done (point)))))
  1151. ;; In trailing space after an as yet undetected virtual semicolon?
  1152. (c-backward-syntactic-ws from)
  1153. (if (and (< (point) to)
  1154. (c-at-vsemi-p))
  1155. (point)
  1156. nil))))))
  1157. (defun c-at-statement-start-p ()
  1158. "Return non-nil if the point is at the first token in a statement
  1159. or somewhere in the syntactic whitespace before it.
  1160. A \"statement\" here is not restricted to those inside code blocks.
  1161. Any kind of declaration-like construct that occur outside function
  1162. bodies is also considered a \"statement\".
  1163. Note that this function might do hidden buffer changes. See the
  1164. comment at the start of cc-engine.el for more info."
  1165. (save-excursion
  1166. (let ((end (point))
  1167. c-maybe-labelp)
  1168. (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
  1169. (or (bobp)
  1170. (eq (char-before) ?})
  1171. (and (eq (char-before) ?{)
  1172. (not (and c-special-brace-lists
  1173. (progn (backward-char)
  1174. (c-looking-at-special-brace-list)))))
  1175. (c-crosses-statement-barrier-p (point) end)))))
  1176. (defun c-at-expression-start-p ()
  1177. "Return non-nil if the point is at the first token in an expression or
  1178. statement, or somewhere in the syntactic whitespace before it.
  1179. An \"expression\" here is a bit different from the normal language
  1180. grammar sense: It's any sequence of expression tokens except commas,
  1181. unless they are enclosed inside parentheses of some kind. Also, an
  1182. expression never continues past an enclosing parenthesis, but it might
  1183. contain parenthesis pairs of any sort except braces.
  1184. Since expressions never cross statement boundaries, this function also
  1185. recognizes statement beginnings, just like `c-at-statement-start-p'.
  1186. Note that this function might do hidden buffer changes. See the
  1187. comment at the start of cc-engine.el for more info."
  1188. (save-excursion
  1189. (let ((end (point))
  1190. (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
  1191. c-maybe-labelp)
  1192. (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
  1193. (or (bobp)
  1194. (memq (char-before) '(?{ ?}))
  1195. (save-excursion (backward-char)
  1196. (looking-at "\\s("))
  1197. (c-crosses-statement-barrier-p (point) end)))))
  1198. ;; A set of functions that covers various idiosyncrasies in
  1199. ;; implementations of `forward-comment'.
  1200. ;; Note: Some emacsen considers incorrectly that any line comment
  1201. ;; ending with a backslash continues to the next line. I can't think
  1202. ;; of any way to work around that in a reliable way without changing
  1203. ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
  1204. ;; changing the syntax for backslash doesn't work since we must treat
  1205. ;; escapes in string literals correctly.)
  1206. (defun c-forward-single-comment ()
  1207. "Move forward past whitespace and the closest following comment, if any.
  1208. Return t if a comment was found, nil otherwise. In either case, the
  1209. point is moved past the following whitespace. Line continuations,
  1210. i.e. a backslashes followed by line breaks, are treated as whitespace.
  1211. The line breaks that end line comments are considered to be the
  1212. comment enders, so the point will be put on the beginning of the next
  1213. line if it moved past a line comment.
  1214. This function does not do any hidden buffer changes."
  1215. (let ((start (point)))
  1216. (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
  1217. (goto-char (match-end 0)))
  1218. (when (forward-comment 1)
  1219. (if (eobp)
  1220. ;; Some emacsen (e.g. XEmacs 21) return t when moving
  1221. ;; forwards at eob.
  1222. nil
  1223. ;; Emacs includes the ending newline in a b-style (c++)
  1224. ;; comment, but XEmacs doesn't. We depend on the Emacs
  1225. ;; behavior (which also is symmetric).
  1226. (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
  1227. (condition-case nil (forward-char 1)))
  1228. t))))
  1229. (defsubst c-forward-comments ()
  1230. "Move forward past all following whitespace and comments.
  1231. Line continuations, i.e. a backslashes followed by line breaks, are
  1232. treated as whitespace.
  1233. Note that this function might do hidden buffer changes. See the
  1234. comment at the start of cc-engine.el for more info."
  1235. (while (or
  1236. ;; If forward-comment in at least XEmacs 21 is given a large
  1237. ;; positive value, it'll loop all the way through if it hits
  1238. ;; eob.
  1239. (and (forward-comment 5)
  1240. ;; Some emacsen (e.g. XEmacs 21) return t when moving
  1241. ;; forwards at eob.
  1242. (not (eobp)))
  1243. (when (looking-at "\\\\[\n\r]")
  1244. (forward-char 2)
  1245. t))))
  1246. (defun c-backward-single-comment ()
  1247. "Move backward past whitespace and the closest preceding comment, if any.
  1248. Return t if a comment was found, nil otherwise. In either case, the
  1249. point is moved past the preceding whitespace. Line continuations,
  1250. i.e. a backslashes followed by line breaks, are treated as whitespace.
  1251. The line breaks that end line comments are considered to be the
  1252. comment enders, so the point cannot be at the end of the same line to
  1253. move over a line comment.
  1254. This function does not do any hidden buffer changes."
  1255. (let ((start (point)))
  1256. ;; When we got newline terminated comments, forward-comment in all
  1257. ;; supported emacsen so far will stop at eol of each line not
  1258. ;; ending with a comment when moving backwards. This corrects for
  1259. ;; that, and at the same time handles line continuations.
  1260. (while (progn
  1261. (skip-chars-backward " \t\n\r\f\v")
  1262. (and (looking-at "[\n\r]")
  1263. (eq (char-before) ?\\)))
  1264. (backward-char))
  1265. (if (bobp)
  1266. ;; Some emacsen (e.g. Emacs 19.34) return t when moving
  1267. ;; backwards at bob.
  1268. nil
  1269. ;; Leave point after the closest following newline if we've
  1270. ;; backed up over any above, since forward-comment won't move
  1271. ;; backward over a line comment if point is at the end of the
  1272. ;; same line.
  1273. (re-search-forward "\\=\\s *[\n\r]" start t)
  1274. (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
  1275. (if (eolp)
  1276. ;; If forward-comment above succeeded and we're at eol
  1277. ;; then the newline we moved over above didn't end a
  1278. ;; line comment, so we give it another go.
  1279. (let (open-paren-in-column-0-is-defun-start)
  1280. (forward-comment -1))
  1281. t))
  1282. ;; Emacs <= 20 and XEmacs move back over the closer of a
  1283. ;; block comment that lacks an opener.
  1284. (if (looking-at "\\*/")
  1285. (progn (forward-char 2) nil)
  1286. t)))))
  1287. (defsubst c-backward-comments ()
  1288. "Move backward past all preceding whitespace and comments.
  1289. Line continuations, i.e. a backslashes followed by line breaks, are
  1290. treated as whitespace. The line breaks that end line comments are
  1291. considered to be the comment enders, so the point cannot be at the end
  1292. of the same line to move over a line comment. Unlike
  1293. c-backward-syntactic-ws, this function doesn't move back over
  1294. preprocessor directives.
  1295. Note that this function might do hidden buffer changes. See the
  1296. comment at the start of cc-engine.el for more info."
  1297. (let ((start (point)))
  1298. (while (and
  1299. ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
  1300. ;; return t when moving backwards at bob.
  1301. (not (bobp))
  1302. (if (let (open-paren-in-column-0-is-defun-start)
  1303. (forward-comment -1))
  1304. (if (looking-at "\\*/")
  1305. ;; Emacs <= 20 and XEmacs move back over the
  1306. ;; closer of a block comment that lacks an opener.
  1307. (progn (forward-char 2) nil)
  1308. t)
  1309. ;; XEmacs treats line continuations as whitespace but
  1310. ;; only in the backward direction, which seems a bit
  1311. ;; odd. Anyway, this is necessary for Emacs.
  1312. (when (and (looking-at "[\n\r]")
  1313. (eq (char-before) ?\\)
  1314. (< (point) start))
  1315. (backward-char)
  1316. t))))))
  1317. ;; Tools for skipping over syntactic whitespace.
  1318. ;; The following functions use text properties to cache searches over
  1319. ;; large regions of syntactic whitespace. It works as follows:
  1320. ;;
  1321. ;; o If a syntactic whitespace region contains anything but simple
  1322. ;; whitespace (i.e. space, tab and line breaks), the text property
  1323. ;; `c-in-sws' is put over it. At places where we have stopped
  1324. ;; within that region there's also a `c-is-sws' text property.
  1325. ;; That since there typically are nested whitespace inside that
  1326. ;; must be handled separately, e.g. whitespace inside a comment or
  1327. ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
  1328. ;; to jump to another point with that property within the same
  1329. ;; `c-in-sws' region. It can be likened to a ladder where
  1330. ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
  1331. ;;
  1332. ;; o The `c-is-sws' property is put on the simple whitespace chars at
  1333. ;; a "rung position" and also maybe on the first following char.
  1334. ;; As many characters as can be conveniently found in this range
  1335. ;; are marked, but no assumption can be made that the whole range
  1336. ;; is marked (it could be clobbered by later changes, for
  1337. ;; instance).
  1338. ;;
  1339. ;; Note that some part of the beginning of a sequence of simple
  1340. ;; whitespace might be part of the end of a preceding line comment
  1341. ;; or cpp directive and must not be considered part of the "rung".
  1342. ;; Such whitespace is some amount of horizontal whitespace followed
  1343. ;; by a newline. In the case of cpp directives it could also be
  1344. ;; two newlines with horizontal whitespace between them.
  1345. ;;
  1346. ;; The reason to include the first following char is to cope with
  1347. ;; "rung positions" that doesn't have any ordinary whitespace. If
  1348. ;; `c-is-sws' is put on a token character it does not have
  1349. ;; `c-in-sws' set simultaneously. That's the only case when that
  1350. ;; can occur, and the reason for not extending the `c-in-sws'
  1351. ;; region to cover it is that the `c-in-sws' region could then be
  1352. ;; accidentally merged with a following one if the token is only
  1353. ;; one character long.
  1354. ;;
  1355. ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
  1356. ;; removed in the changed region. If the change was inside
  1357. ;; syntactic whitespace that means that the "ladder" is broken, but
  1358. ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
  1359. ;; parts on either side and use an ordinary search only to "repair"
  1360. ;; the gap.
  1361. ;;
  1362. ;; Special care needs to be taken if a region is removed: If there
  1363. ;; are `c-in-sws' on both sides of it which do not connect inside
  1364. ;; the region then they can't be joined. If e.g. a marked macro is
  1365. ;; broken, syntactic whitespace inside the new text might be
  1366. ;; marked. If those marks would become connected with the old
  1367. ;; `c-in-sws' range around the macro then we could get a ladder
  1368. ;; with one end outside the macro and the other at some whitespace
  1369. ;; within it.
  1370. ;;
  1371. ;; The main motivation for this system is to increase the speed in
  1372. ;; skipping over the large whitespace regions that can occur at the
  1373. ;; top level in e.g. header files that contain a lot of comments and
  1374. ;; cpp directives. For small comments inside code it's probably
  1375. ;; slower than using `forward-comment' straightforwardly, but speed is
  1376. ;; not a significant factor there anyway.
  1377. ; (defface c-debug-is-sws-face
  1378. ; '((t (:background "GreenYellow")))
  1379. ; "Debug face to mark the `c-is-sws' property.")
  1380. ; (defface c-debug-in-sws-face
  1381. ; '((t (:underline t)))
  1382. ; "Debug face to mark the `c-in-sws' property.")
  1383. ; (defun c-debug-put-sws-faces ()
  1384. ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
  1385. ; ;; properties in the buffer.
  1386. ; (interactive)
  1387. ; (save-excursion
  1388. ; (c-save-buffer-state (in-face)
  1389. ; (goto-char (point-min))
  1390. ; (setq in-face (if (get-text-property (point) 'c-is-sws)
  1391. ; (point)))
  1392. ; (while (progn
  1393. ; (goto-char (next-single-property-change
  1394. ; (point) 'c-is-sws nil (point-max)))
  1395. ; (if in-face
  1396. ; (progn
  1397. ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
  1398. ; (setq in-face nil))
  1399. ; (setq in-face (point)))
  1400. ; (not (eobp))))
  1401. ; (goto-char (point-min))
  1402. ; (setq in-face (if (get-text-property (point) 'c-in-sws)
  1403. ; (point)))
  1404. ; (while (progn
  1405. ; (goto-char (next-single-property-change
  1406. ; (point) 'c-in-sws nil (point-max)))
  1407. ; (if in-face
  1408. ; (progn
  1409. ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
  1410. ; (setq in-face nil))
  1411. ; (setq in-face (point)))
  1412. ; (not (eobp)))))))
  1413. (defmacro c-debug-sws-msg (&rest args)
  1414. ;;`(message ,@args)
  1415. )
  1416. (defmacro c-put-is-sws (beg end)
  1417. ;; This macro does a hidden buffer change.
  1418. `(let ((beg ,beg) (end ,end))
  1419. (put-text-property beg end 'c-is-sws t)
  1420. ,@(when (facep 'c-debug-is-sws-face)
  1421. `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
  1422. (defmacro c-put-in-sws (beg end)
  1423. ;; This macro does a hidden buffer change.
  1424. `(let ((beg ,beg) (end ,end))
  1425. (put-text-property beg end 'c-in-sws t)
  1426. ,@(when (facep 'c-debug-is-sws-face)
  1427. `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
  1428. (defmacro c-remove-is-sws (beg end)
  1429. ;; This macro does a hidden buffer change.
  1430. `(let ((beg ,beg) (end ,end))
  1431. (remove-text-properties beg end '(c-is-sws nil))
  1432. ,@(when (facep 'c-debug-is-sws-face)
  1433. `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
  1434. (defmacro c-remove-in-sws (beg end)
  1435. ;; This macro does a hidden buffer change.
  1436. `(let ((beg ,beg) (end ,end))
  1437. (remove-text-properties beg end '(c-in-sws nil))
  1438. ,@(when (facep 'c-debug-is-sws-face)
  1439. `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
  1440. (defmacro c-remove-is-and-in-sws (beg end)
  1441. ;; This macro does a hidden buffer change.
  1442. `(let ((beg ,beg) (end ,end))
  1443. (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
  1444. ,@(when (facep 'c-debug-is-sws-face)
  1445. `((c-debug-remove-face beg end 'c-debug-is-sws-face)
  1446. (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
  1447. (defsubst c-invalidate-sws-region-after (beg end)
  1448. ;; Called from `after-change-functions'. Note that if
  1449. ;; `c-forward-sws' or `c-backward-sws' are used outside
  1450. ;; `c-save-buffer-state' or similar then this will remove the cache
  1451. ;; properties right after they're added.
  1452. ;;
  1453. ;; This function does hidden buffer changes.
  1454. (save-excursion
  1455. ;; Adjust the end to remove the properties in any following simple
  1456. ;; ws up to and including the next line break, if there is any
  1457. ;; after the changed region. This is necessary e.g. when a rung
  1458. ;; marked empty line is converted to a line comment by inserting
  1459. ;; "//" before the line break. In that case the line break would
  1460. ;; keep the rung mark which could make a later `c-backward-sws'
  1461. ;; move into the line comment instead of over it.
  1462. (goto-char end)
  1463. (skip-chars-forward " \t\f\v")
  1464. (when (and (eolp) (not (eobp)))
  1465. (setq end (1+ (point)))))
  1466. (when (and (= beg end)
  1467. (get-text-property beg 'c-in-sws)
  1468. (> beg (point-min))
  1469. (get-text-property (1- beg) 'c-in-sws))
  1470. ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
  1471. ;; safe to keep a range that was continuous before the change. E.g:
  1472. ;;
  1473. ;; #define foo
  1474. ;; \
  1475. ;; bar
  1476. ;;
  1477. ;; There can be a "ladder" between "#" and "b". Now, if the newline
  1478. ;; after "foo" is removed then "bar" will become part of the cpp
  1479. ;; directive instead of a syntactically relevant token. In that
  1480. ;; case there's no longer syntactic ws from "#" to "b".
  1481. (setq beg (1- beg)))
  1482. (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
  1483. (c-remove-is-and-in-sws beg end))
  1484. (defun c-forward-sws ()
  1485. ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
  1486. ;;
  1487. ;; This function might do hidden buffer changes.
  1488. (let (;; `rung-pos' is set to a position as early as possible in the
  1489. ;; unmarked part of the simple ws region.
  1490. (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
  1491. rung-is-marked next-rung-is-marked simple-ws-end
  1492. ;; `safe-start' is set when it's safe to cache the start position.
  1493. ;; It's not set if we've initially skipped over comments and line
  1494. ;; continuations since we might have gone out through the end of a
  1495. ;; macro then. This provision makes `c-forward-sws' not populate the
  1496. ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
  1497. ;; more common.
  1498. safe-start)
  1499. ;; Skip simple ws and do a quick check on the following character to see
  1500. ;; if it's anything that can't start syntactic ws, so we can bail out
  1501. ;; early in the majority of cases when there just are a few ws chars.
  1502. (skip-chars-forward " \t\n\r\f\v")
  1503. (when (looking-at c-syntactic-ws-start)
  1504. (setq rung-end-pos (min (1+ (point)) (point-max)))
  1505. (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
  1506. 'c-is-sws t))
  1507. ;; Find the last rung position to avoid setting properties in all
  1508. ;; the cases when the marked rung is complete.
  1509. ;; (`next-single-property-change' is certain to move at least one
  1510. ;; step forward.)
  1511. (setq rung-pos (1- (next-single-property-change
  1512. rung-is-marked 'c-is-sws nil rung-end-pos)))
  1513. ;; Got no marked rung here. Since the simple ws might have started
  1514. ;; inside a line comment or cpp directive we must set `rung-pos' as
  1515. ;; high as possible.
  1516. (setq rung-pos (point)))
  1517. (while
  1518. (progn
  1519. (while
  1520. (when (and rung-is-marked
  1521. (get-text-property (point) 'c-in-sws))
  1522. ;; The following search is the main reason that `c-in-sws'
  1523. ;; and `c-is-sws' aren't combined to one property.
  1524. (goto-char (next-single-property-change
  1525. (point) 'c-in-sws nil (point-max)))
  1526. (unless (get-text-property (point) 'c-is-sws)
  1527. ;; If the `c-in-sws' region extended past the last
  1528. ;; `c-is-sws' char we have to go back a bit.
  1529. (or (get-text-property (1- (point)) 'c-is-sws)
  1530. (goto-char (previous-single-property-change
  1531. (point) 'c-is-sws)))
  1532. (backward-char))
  1533. (c-debug-sws-msg
  1534. "c-forward-sws cached move %s -> %s (max %s)"
  1535. rung-pos (point) (point-max))
  1536. (setq rung-pos (point))
  1537. (and (> (skip-chars-forward " \t\n\r\f\v") 0)
  1538. (not (eobp))))
  1539. ;; We'll loop here if there is simple ws after the last rung.
  1540. ;; That means that there's been some change in it and it's
  1541. ;; possible that we've stepped into another ladder, so extend
  1542. ;; the previous one to join with it if there is one, and try to
  1543. ;; use the cache again.
  1544. (c-debug-sws-msg
  1545. "c-forward-sws extending rung with [%s..%s] (max %s)"
  1546. (1+ rung-pos) (1+ (point)) (point-max))
  1547. (unless (get-text-property (point) 'c-is-sws)
  1548. ;; Remove any `c-in-sws' property from the last char of
  1549. ;; the rung before we mark it with `c-is-sws', so that we
  1550. ;; won't connect with the remains of a broken "ladder".
  1551. (c-remove-in-sws (point) (1+ (point))))
  1552. (c-put-is-sws (1+ rung-pos)
  1553. (1+ (point)))
  1554. (c-put-in-sws rung-pos
  1555. (setq rung-pos (point)
  1556. last-put-in-sws-pos rung-pos)))
  1557. (setq simple-ws-end (point))
  1558. (c-forward-comments)
  1559. (cond
  1560. ((/= (point) simple-ws-end)
  1561. ;; Skipped over comments. Don't cache at eob in case the buffer
  1562. ;; is narrowed.
  1563. (not (eobp)))
  1564. ((save-excursion
  1565. (and c-opt-cpp-prefix
  1566. (looking-at c-opt-cpp-start)
  1567. (progn (skip-chars-backward " \t")
  1568. (bolp))
  1569. (or (bobp)
  1570. (progn (backward-char)
  1571. (not (eq (char-before) ?\\))))))
  1572. ;; Skip a preprocessor directive.
  1573. (end-of-line)
  1574. (while (and (eq (char-before) ?\\)
  1575. (= (forward-line 1) 0))
  1576. (end-of-line))
  1577. (forward-line 1)
  1578. (setq safe-start t)
  1579. ;; Don't cache at eob in case the buffer is narrowed.
  1580. (not (eobp)))))
  1581. ;; We've searched over a piece of non-white syntactic ws. See if this
  1582. ;; can be cached.
  1583. (setq next-rung-pos (point))
  1584. (skip-chars-forward " \t\n\r\f\v")
  1585. (setq rung-end-pos (min (1+ (point)) (point-max)))
  1586. (if (or
  1587. ;; Cache if we haven't skipped comments only, and if we started
  1588. ;; either from a marked rung or from a completely uncached
  1589. ;; position.
  1590. (and safe-start
  1591. (or rung-is-marked
  1592. (not (get-text-property simple-ws-end 'c-in-sws))))
  1593. ;; See if there's a marked rung in the encountered simple ws. If
  1594. ;; so then we can cache, unless `safe-start' is nil. Even then
  1595. ;; we need to do this to check if the cache can be used for the
  1596. ;; next step.
  1597. (and (setq next-rung-is-marked
  1598. (text-property-any next-rung-pos rung-end-pos
  1599. 'c-is-sws t))
  1600. safe-start))
  1601. (progn
  1602. (c-debug-sws-msg
  1603. "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
  1604. rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
  1605. (point-max))
  1606. ;; Remove the properties for any nested ws that might be cached.
  1607. ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
  1608. ;; anyway.
  1609. (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
  1610. (unless (and rung-is-marked (= rung-pos simple-ws-end))
  1611. (c-put-is-sws rung-pos
  1612. (1+ simple-ws-end))
  1613. (setq rung-is-marked t))
  1614. (c-put-in-sws rung-pos
  1615. (setq rung-pos (point)
  1616. last-put-in-sws-pos rung-pos))
  1617. (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
  1618. ;; Remove any `c-in-sws' property from the last char of
  1619. ;; the rung before we mark it with `c-is-sws', so that we
  1620. ;; won't connect with the remains of a broken "ladder".
  1621. (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
  1622. (c-put-is-sws next-rung-pos
  1623. rung-end-pos))
  1624. (c-debug-sws-msg
  1625. "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
  1626. rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
  1627. (point-max))
  1628. ;; Set `rung-pos' for the next rung. It's the same thing here as
  1629. ;; initially, except that the rung position is set as early as
  1630. ;; possible since we can't be in the ending ws of a line comment or
  1631. ;; cpp directive now.
  1632. (if (setq rung-is-marked next-rung-is-marked)
  1633. (setq rung-pos (1- (next-single-property-change
  1634. rung-is-marked 'c-is-sws nil rung-end-pos)))
  1635. (setq rung-pos next-rung-pos))
  1636. (setq safe-start t)))
  1637. ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
  1638. ;; another one after the point (which might occur when editing inside a
  1639. ;; comment or macro).
  1640. (when (eq last-put-in-sws-pos (point))
  1641. (cond ((< last-put-in-sws-pos (point-max))
  1642. (c-debug-sws-msg
  1643. "c-forward-sws clearing at %s for cache separation"
  1644. last-put-in-sws-pos)
  1645. (c-remove-in-sws last-put-in-sws-pos
  1646. (1+ last-put-in-sws-pos)))
  1647. (t
  1648. ;; If at eob we have to clear the last character before the end
  1649. ;; instead since the buffer might be narrowed and there might
  1650. ;; be a `c-in-sws' after (point-max). In this case it's
  1651. ;; necessary to clear both properties.
  1652. (c-debug-sws-msg
  1653. "c-forward-sws clearing thoroughly at %s for cache separation"
  1654. (1- last-put-in-sws-pos))
  1655. (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
  1656. last-put-in-sws-pos))))
  1657. )))
  1658. (defun c-backward-sws ()
  1659. ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
  1660. ;;
  1661. ;; This function might do hidden buffer changes.
  1662. (let (;; `rung-pos' is set to a position as late as possible in the unmarked
  1663. ;; part of the simple ws region.
  1664. (rung-pos (point)) next-rung-pos last-put-in-sws-pos
  1665. rung-is-marked simple-ws-beg cmt-skip-pos)
  1666. ;; Skip simple horizontal ws and do a quick check on the preceding
  1667. ;; character to see if it's anything that can't end syntactic ws, so we can
  1668. ;; bail out early in the majority of cases when there just are a few ws
  1669. ;; chars. Newlines are complicated in the backward direction, so we can't
  1670. ;; skip over them.
  1671. (skip-chars-backward " \t\f")
  1672. (when (and (not (bobp))
  1673. (save-excursion
  1674. (backward-char)
  1675. (looking-at c-syntactic-ws-end)))
  1676. ;; Try to find a rung position in the simple ws preceding point, so that
  1677. ;; we can get a cache hit even if the last bit of the simple ws has
  1678. ;; changed recently.
  1679. (setq simple-ws-beg (point))
  1680. (skip-chars-backward " \t\n\r\f\v")
  1681. (if (setq rung-is-marked (text-property-any
  1682. (point) (min (1+ rung-pos) (point-max))
  1683. 'c-is-sws t))
  1684. ;; `rung-pos' will be the earliest marked position, which means that
  1685. ;; there might be later unmarked parts in the simple ws region.
  1686. ;; It's not worth the effort to fix that; the last part of the
  1687. ;; simple ws is also typically edited often, so it could be wasted.
  1688. (goto-char (setq rung-pos rung-is-marked))
  1689. (goto-char simple-ws-beg))
  1690. (while
  1691. (progn
  1692. (while
  1693. (when (and rung-is-marked
  1694. (not (bobp))
  1695. (get-text-property (1- (point)) 'c-in-sws))
  1696. ;; The following search is the main reason that `c-in-sws'
  1697. ;; and `c-is-sws' aren't combined to one property.
  1698. (goto-char (previous-single-property-change
  1699. (point) 'c-in-sws nil (point-min)))
  1700. (unless (get-text-property (point) 'c-is-sws)
  1701. ;; If the `c-in-sws' region extended past the first
  1702. ;; `c-is-sws' char we have to go forward a bit.
  1703. (goto-char (next-single-property-change
  1704. (point) 'c-is-sws)))
  1705. (c-debug-sws-msg
  1706. "c-backward-sws cached move %s <- %s (min %s)"
  1707. (point) rung-pos (point-min))
  1708. (setq rung-pos (point))
  1709. (if (and (< (min (skip-chars-backward " \t\f\v")
  1710. (progn
  1711. (setq simple-ws-beg (point))
  1712. (skip-chars-backward " \t\n\r\f\v")))
  1713. 0)
  1714. (setq rung-is-marked
  1715. (text-property-any (point) rung-pos
  1716. 'c-is-sws t)))
  1717. t
  1718. (goto-char simple-ws-beg)
  1719. nil))
  1720. ;; We'll loop here if there is simple ws before the first rung.
  1721. ;; That means that there's been some change in it and it's
  1722. ;; possible that we've stepped into another ladder, so extend
  1723. ;; the previous one to join with it if there is one, and try to
  1724. ;; use the cache again.
  1725. (c-debug-sws-msg
  1726. "c-backward-sws extending rung with [%s..%s] (min %s)"
  1727. rung-is-marked rung-pos (point-min))
  1728. (unless (get-text-property (1- rung-pos) 'c-is-sws)
  1729. ;; Remove any `c-in-sws' property from the last char of
  1730. ;; the rung before we mark it with `c-is-sws', so that we
  1731. ;; won't connect with the remains of a broken "ladder".
  1732. (c-remove-in-sws (1- rung-pos) rung-pos))
  1733. (c-put-is-sws rung-is-marked
  1734. rung-pos)
  1735. (c-put-in-sws rung-is-marked
  1736. (1- rung-pos))
  1737. (setq rung-pos rung-is-marked
  1738. last-put-in-sws-pos rung-pos))
  1739. (c-backward-comments)
  1740. (setq cmt-skip-pos (point))
  1741. (cond
  1742. ((and c-opt-cpp-prefix
  1743. (/= cmt-skip-pos simple-ws-beg)
  1744. (c-beginning-of-macro))
  1745. ;; Inside a cpp directive. See if it should be skipped over.
  1746. (let ((cpp-beg (point)))
  1747. ;; Move back over all line continuations in the region skipped
  1748. ;; over by `c-backward-comments'. If we go past it then we
  1749. ;; started inside the cpp directive.
  1750. (goto-char simple-ws-beg)
  1751. (beginning-of-line)
  1752. (while (and (> (point) cmt-skip-pos)
  1753. (progn (backward-char)
  1754. (eq (char-before) ?\\)))
  1755. (beginning-of-line))
  1756. (if (< (point) cmt-skip-pos)
  1757. ;; Don't move past the cpp directive if we began inside
  1758. ;; it. Note that the position at the end of the last line
  1759. ;; of the macro is also considered to be within it.
  1760. (progn (goto-char cmt-skip-pos)
  1761. nil)
  1762. ;; It's worthwhile to spend a little bit of effort on finding
  1763. ;; the end of the macro, to get a good `simple-ws-beg'
  1764. ;; position for the cache. Note that `c-backward-comments'
  1765. ;; could have stepped over some comments before going into
  1766. ;; the macro, and then `simple-ws-beg' must be kept on the
  1767. ;; same side of those comments.
  1768. (goto-char simple-ws-beg)
  1769. (skip-chars-backward " \t\n\r\f\v")
  1770. (if (eq (char-before) ?\\)
  1771. (forward-char))
  1772. (forward-line 1)
  1773. (if (< (point) simple-ws-beg)
  1774. ;; Might happen if comments after the macro were skipped
  1775. ;; over.
  1776. (setq simple-ws-beg (point)))
  1777. (goto-char cpp-beg)
  1778. t)))
  1779. ((/= (save-excursion
  1780. (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
  1781. (setq next-rung-pos (point)))
  1782. simple-ws-beg)
  1783. ;; Skipped over comments. Must put point at the end of
  1784. ;; the simple ws at point since we might be after a line
  1785. ;; comment or cpp directive that's been partially
  1786. ;; narrowed out, and we can't risk marking the simple ws
  1787. ;; at the end of it.
  1788. (goto-char next-rung-pos)
  1789. t)))
  1790. ;; We've searched over a piece of non-white syntactic ws. See if this
  1791. ;; can be cached.
  1792. (setq next-rung-pos (point))
  1793. (skip-chars-backward " \t\f\v")
  1794. (if (or
  1795. ;; Cache if we started either from a marked rung or from a
  1796. ;; completely uncached position.
  1797. rung-is-marked
  1798. (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
  1799. ;; Cache if there's a marked rung in the encountered simple ws.
  1800. (save-excursion
  1801. (skip-chars-backward " \t\n\r\f\v")
  1802. (text-property-any (point) (min (1+ next-rung-pos) (point-max))
  1803. 'c-is-sws t)))
  1804. (progn
  1805. (c-debug-sws-msg
  1806. "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
  1807. (point) (1+ next-rung-pos)
  1808. simple-ws-beg (min (1+ rung-pos) (point-max))
  1809. (point-min))
  1810. ;; Remove the properties for any nested ws that might be cached.
  1811. ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
  1812. ;; anyway.
  1813. (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
  1814. (unless (and rung-is-marked (= simple-ws-beg rung-pos))
  1815. (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
  1816. (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
  1817. ;; Remove any `c-in-sws' property from the last char of
  1818. ;; the rung before we mark it with `c-is-sws', so that we
  1819. ;; won't connect with the remains of a broken "ladder".
  1820. (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
  1821. (c-put-is-sws simple-ws-beg
  1822. rung-end-pos)
  1823. (setq rung-is-marked t)))
  1824. (c-put-in-sws (setq simple-ws-beg (point)
  1825. last-put-in-sws-pos simple-ws-beg)
  1826. rung-pos)
  1827. (c-put-is-sws (setq rung-pos simple-ws-beg)
  1828. (1+ next-rung-pos)))
  1829. (c-debug-sws-msg
  1830. "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
  1831. (point) (1+ next-rung-pos)
  1832. simple-ws-beg (min (1+ rung-pos) (point-max))
  1833. (point-min))
  1834. (setq rung-pos next-rung-pos
  1835. simple-ws-beg (point))
  1836. ))
  1837. ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
  1838. ;; another one before the point (which might occur when editing inside a
  1839. ;; comment or macro).
  1840. (when (eq last-put-in-sws-pos (point))
  1841. (cond ((< (point-min) last-put-in-sws-pos)
  1842. (c-debug-sws-msg
  1843. "c-backward-sws clearing at %s for cache separation"
  1844. (1- last-put-in-sws-pos))
  1845. (c-remove-in-sws (1- last-put-in-sws-pos)
  1846. last-put-in-sws-pos))
  1847. ((> (point-min) 1)
  1848. ;; If at bob and the buffer is narrowed, we have to clear the
  1849. ;; character we're standing on instead since there might be a
  1850. ;; `c-in-sws' before (point-min). In this case it's necessary
  1851. ;; to clear both properties.
  1852. (c-debug-sws-msg
  1853. "c-backward-sws clearing thoroughly at %s for cache separation"
  1854. last-put-in-sws-pos)
  1855. (c-remove-is-and-in-sws last-put-in-sws-pos
  1856. (1+ last-put-in-sws-pos)))))
  1857. )))
  1858. ;; Other whitespace tools
  1859. (defun c-partial-ws-p (beg end)
  1860. ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
  1861. ;; region? This is a "heuristic" function. .....
  1862. ;;
  1863. ;; The motivation for the second bit is to check whether removing this
  1864. ;; region would coalesce two symbols.
  1865. ;;
  1866. ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
  1867. ;; careful about using this function for, e.g. AWK. (2007/3/7)
  1868. (save-excursion
  1869. (let ((end+1 (min (1+ end) (point-max))))
  1870. (or (progn (goto-char (max (point-min) (1- beg)))
  1871. (c-skip-ws-forward end)
  1872. (eq (point) end))
  1873. (progn (goto-char beg)
  1874. (c-skip-ws-forward end+1)
  1875. (eq (point) end+1))))))
  1876. ;; A system for finding noteworthy parens before the point.
  1877. (defconst c-state-cache-too-far 5000)
  1878. ;; A maximum comfortable scanning distance, e.g. between
  1879. ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
  1880. ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
  1881. ;; the cache and starting again from point-min or a beginning of defun. This
  1882. ;; value can be tuned for efficiency or set to a lower value for testing.
  1883. (defvar c-state-cache nil)
  1884. (make-variable-buffer-local 'c-state-cache)
  1885. ;; The state cache used by `c-parse-state' to cut down the amount of
  1886. ;; searching. It's the result from some earlier `c-parse-state' call. See
  1887. ;; `c-parse-state''s doc string for details of its structure.
  1888. ;;
  1889. ;; The use of the cached info is more effective if the next
  1890. ;; `c-parse-state' call is on a line close by the one the cached state
  1891. ;; was made at; the cache can actually slow down a little if the
  1892. ;; cached state was made very far back in the buffer. The cache is
  1893. ;; most effective if `c-parse-state' is used on each line while moving
  1894. ;; forward.
  1895. (defvar c-state-cache-good-pos 1)
  1896. (make-variable-buffer-local 'c-state-cache-good-pos)
  1897. ;; This is a position where `c-state-cache' is known to be correct, or
  1898. ;; nil (see below). It's a position inside one of the recorded unclosed
  1899. ;; parens or the top level, but not further nested inside any literal or
  1900. ;; subparen that is closed before the last recorded position.
  1901. ;;
  1902. ;; The exact position is chosen to try to be close to yet earlier than
  1903. ;; the position where `c-state-cache' will be called next. Right now
  1904. ;; the heuristic is to set it to the position after the last found
  1905. ;; closing paren (of any type) before the line on which
  1906. ;; `c-parse-state' was called. That is chosen primarily to work well
  1907. ;; with refontification of the current line.
  1908. ;;
  1909. ;; 2009-07-28: When `c-state-point-min' and the last position where
  1910. ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
  1911. ;; both in the same literal, there is no such "good position", and
  1912. ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
  1913. ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
  1914. ;;
  1915. ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
  1916. ;; the middle of the desert, as long as it is not within a brace pair
  1917. ;; recorded in `c-state-cache' or a paren/bracket pair.
  1918. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1919. ;; We maintain a simple cache of positions which aren't in a literal, so as to
  1920. ;; speed up testing for non-literality.
  1921. (defconst c-state-nonlit-pos-interval 3000)
  1922. ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
  1923. (defvar c-state-nonlit-pos-cache nil)
  1924. (make-variable-buffer-local 'c-state-nonlit-pos-cache)
  1925. ;; A list of buffer positions which are known not to be in a literal or a cpp
  1926. ;; construct. This is ordered with higher positions at the front of the list.
  1927. ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
  1928. (defvar c-state-nonlit-pos-cache-limit 1)
  1929. (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
  1930. ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
  1931. ;; reduced by buffer changes, and increased by invocations of
  1932. ;; `c-state-literal-at'.
  1933. (defvar c-state-semi-nonlit-pos-cache nil)
  1934. (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
  1935. ;; A list of buffer positions which are known not to be in a literal. This is
  1936. ;; ordered with higher positions at the front of the list. Only those which
  1937. ;; are less than `c-state-semi-nonlit-pos-cache-limit' are valid.
  1938. (defvar c-state-semi-nonlit-pos-cache-limit 1)
  1939. (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
  1940. ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This is
  1941. ;; reduced by buffer changes, and increased by invocations of
  1942. ;; `c-state-literal-at'. FIXME!!!
  1943. (defsubst c-state-pp-to-literal (from to)
  1944. ;; Do a parse-partial-sexp from FROM to TO, returning either
  1945. ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
  1946. ;; (STATE) otherwise,
  1947. ;; where STATE is the parsing state at TO, TYPE is the type of the literal
  1948. ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
  1949. ;;
  1950. ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
  1951. ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
  1952. ;; STATE are valid.
  1953. (save-excursion
  1954. (let ((s (parse-partial-sexp from to))
  1955. ty)
  1956. (when (or (nth 3 s) (nth 4 s)) ; in a string or comment
  1957. (setq ty (cond
  1958. ((nth 3 s) 'string)
  1959. ((eq (nth 7 s) t) 'c++)
  1960. (t 'c)))
  1961. (parse-partial-sexp (point) (point-max)
  1962. nil ; TARGETDEPTH
  1963. nil ; STOPBEFORE
  1964. s ; OLDSTATE
  1965. 'syntax-table)) ; stop at end of literal
  1966. (if ty
  1967. `(,s ,ty (,(nth 8 s) . ,(point)))
  1968. `(,s)))))
  1969. (defun c-state-safe-place (here)
  1970. ;; Return a buffer position before HERE which is "safe", i.e. outside any
  1971. ;; string, comment, or macro.
  1972. ;;
  1973. ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
  1974. ;; MAY NOT contain any positions within macros, since macros are frequently
  1975. ;; turned into comments by use of the `c-cpp-delimiter' category properties.
  1976. ;; We cannot rely on this mechanism whilst determining a cache pos since
  1977. ;; this function is also called from outwith `c-parse-state'.
  1978. (save-restriction
  1979. (widen)
  1980. (save-excursion
  1981. (let ((c c-state-nonlit-pos-cache)
  1982. pos npos high-pos lit macro-beg macro-end)
  1983. ;; Trim the cache to take account of buffer changes.
  1984. (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
  1985. (setq c (cdr c)))
  1986. (setq c-state-nonlit-pos-cache c)
  1987. (while (and c (> (car c) here))
  1988. (setq high-pos (car c))
  1989. (setq c (cdr c)))
  1990. (setq pos (or (car c) (point-min)))
  1991. (unless high-pos
  1992. (while
  1993. ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
  1994. (and
  1995. (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
  1996. ;; Test for being in a literal. If so, go to after it.
  1997. (progn
  1998. (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
  1999. (or (null lit)
  2000. (prog1 (<= (cdr lit) here)
  2001. (setq npos (cdr lit)))))
  2002. ;; Test for being in a macro. If so, go to after it.
  2003. (progn
  2004. (goto-char npos)
  2005. (setq macro-beg
  2006. (and (c-beginning-of-macro) (/= (point) npos) (point)))
  2007. (when macro-beg
  2008. (c-syntactic-end-of-macro)
  2009. (or (eobp) (forward-char))
  2010. (setq macro-end (point)))
  2011. (or (null macro-beg)
  2012. (prog1 (<= macro-end here)
  2013. (setq npos macro-end)))))
  2014. (setq pos npos)
  2015. (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
  2016. ;; Add one extra element above HERE so as to to avoid the previous
  2017. ;; expensive calculation when the next call is close to the current
  2018. ;; one. This is especially useful when inside a large macro.
  2019. (setq c-state-nonlit-pos-cache (cons npos c-state-nonlit-pos-cache)))
  2020. (if (> pos c-state-nonlit-pos-cache-limit)
  2021. (setq c-state-nonlit-pos-cache-limit pos))
  2022. pos))))
  2023. (defun c-state-semi-safe-place (here)
  2024. ;; Return a buffer position before HERE which is "safe", i.e. outside any
  2025. ;; string or comment. It may be in a macro.
  2026. (save-restriction
  2027. (widen)
  2028. (save-excursion
  2029. (let ((c c-state-semi-nonlit-pos-cache)
  2030. pos npos high-pos lit macro-beg macro-end)
  2031. ;; Trim the cache to take account of buffer changes.
  2032. (while (and c (> (car c) c-state-semi-nonlit-pos-cache-limit))
  2033. (setq c (cdr c)))
  2034. (setq c-state-semi-nonlit-pos-cache c)
  2035. (while (and c (> (car c) here))
  2036. (setq high-pos (car c))
  2037. (setq c (cdr c)))
  2038. (setq pos (or (car c) (point-min)))
  2039. (unless high-pos
  2040. (while
  2041. ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
  2042. (and
  2043. (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
  2044. ;; Test for being in a literal. If so, go to after it.
  2045. (progn
  2046. (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
  2047. (or (null lit)
  2048. (prog1 (<= (cdr lit) here)
  2049. (setq npos (cdr lit))))))
  2050. (setq pos npos)
  2051. (setq c-state-semi-nonlit-pos-cache
  2052. (cons pos c-state-semi-nonlit-pos-cache))))
  2053. (if (> pos c-state-semi-nonlit-pos-cache-limit)
  2054. (setq c-state-semi-nonlit-pos-cache-limit pos))
  2055. pos))))
  2056. (defun c-state-literal-at (here)
  2057. ;; If position HERE is inside a literal, return (START . END), the
  2058. ;; boundaries of the literal (which may be outside the accessible bit of the
  2059. ;; buffer). Otherwise, return nil.
  2060. ;;
  2061. ;; This function is almost the same as `c-literal-limits'. Previously, it
  2062. ;; differed in that it was a lower level function, and that it rigorously
  2063. ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
  2064. ;; virtually identical to this function.
  2065. (save-restriction
  2066. (widen)
  2067. (save-excursion
  2068. (let ((pos (c-state-safe-place here)))
  2069. (car (cddr (c-state-pp-to-literal pos here)))))))
  2070. (defsubst c-state-lit-beg (pos)
  2071. ;; Return the start of the literal containing POS, or POS itself.
  2072. (or (car (c-state-literal-at pos))
  2073. pos))
  2074. (defsubst c-state-cache-non-literal-place (pos state)
  2075. ;; Return a position outside of a string/comment/macro at or before POS.
  2076. ;; STATE is the parse-partial-sexp state at POS.
  2077. (let ((res (if (or (nth 3 state) ; in a string?
  2078. (nth 4 state)) ; in a comment?
  2079. (nth 8 state)
  2080. pos)))
  2081. (save-excursion
  2082. (goto-char res)
  2083. (if (c-beginning-of-macro)
  2084. (point)
  2085. res))))
  2086. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2087. ;; Stuff to do with point-min, and coping with any literal there.
  2088. (defvar c-state-point-min 1)
  2089. (make-variable-buffer-local 'c-state-point-min)
  2090. ;; This is (point-min) when `c-state-cache' was last calculated. A change of
  2091. ;; narrowing is likely to affect the parens that are visible before the point.
  2092. (defvar c-state-point-min-lit-type nil)
  2093. (make-variable-buffer-local 'c-state-point-min-lit-type)
  2094. (defvar c-state-point-min-lit-start nil)
  2095. (make-variable-buffer-local 'c-state-point-min-lit-start)
  2096. ;; These two variables define the literal, if any, containing point-min.
  2097. ;; Their values are, respectively, 'string, c, or c++, and the start of the
  2098. ;; literal. If there's no literal there, they're both nil.
  2099. (defvar c-state-min-scan-pos 1)
  2100. (make-variable-buffer-local 'c-state-min-scan-pos)
  2101. ;; This is the earliest buffer-pos from which scanning can be done. It is
  2102. ;; either the end of the literal containing point-min, or point-min itself.
  2103. ;; It becomes nil if the buffer is changed earlier than this point.
  2104. (defun c-state-get-min-scan-pos ()
  2105. ;; Return the lowest valid scanning pos. This will be the end of the
  2106. ;; literal enclosing point-min, or point-min itself.
  2107. (or c-state-min-scan-pos
  2108. (save-restriction
  2109. (save-excursion
  2110. (widen)
  2111. (goto-char c-state-point-min-lit-start)
  2112. (if (eq c-state-point-min-lit-type 'string)
  2113. (forward-sexp)
  2114. (forward-comment 1))
  2115. (setq c-state-min-scan-pos (point))))))
  2116. (defun c-state-mark-point-min-literal ()
  2117. ;; Determine the properties of any literal containing POINT-MIN, setting the
  2118. ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
  2119. ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
  2120. (let ((p-min (point-min))
  2121. lit)
  2122. (save-restriction
  2123. (widen)
  2124. (setq lit (c-state-literal-at p-min))
  2125. (if lit
  2126. (setq c-state-point-min-lit-type
  2127. (save-excursion
  2128. (goto-char (car lit))
  2129. (cond
  2130. ((looking-at c-block-comment-start-regexp) 'c)
  2131. ((looking-at c-line-comment-starter) 'c++)
  2132. (t 'string)))
  2133. c-state-point-min-lit-start (car lit)
  2134. c-state-min-scan-pos (cdr lit))
  2135. (setq c-state-point-min-lit-type nil
  2136. c-state-point-min-lit-start nil
  2137. c-state-min-scan-pos p-min)))))
  2138. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2139. ;; A variable which signals a brace dessert - helpful for reducing the number
  2140. ;; of fruitless backward scans.
  2141. (defvar c-state-brace-pair-desert nil)
  2142. (make-variable-buffer-local 'c-state-brace-pair-desert)
  2143. ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
  2144. ;; that defun has searched backwards for a brace pair and not found one. Its
  2145. ;; value is either nil or a cons (PA . FROM), where PA is the position of the
  2146. ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
  2147. ;; nil when at top level) and FROM is where the backward search started. It
  2148. ;; is reset to nil in `c-invalidate-state-cache'.
  2149. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2150. ;; Lowish level functions/macros which work directly on `c-state-cache', or a
  2151. ;; list of like structure.
  2152. (defmacro c-state-cache-top-lparen (&optional cache)
  2153. ;; Return the address of the top left brace/bracket/paren recorded in CACHE
  2154. ;; (default `c-state-cache') (or nil).
  2155. (let ((cash (or cache 'c-state-cache)))
  2156. `(if (consp (car ,cash))
  2157. (caar ,cash)
  2158. (car ,cash))))
  2159. (defmacro c-state-cache-top-paren (&optional cache)
  2160. ;; Return the address of the latest brace/bracket/paren (whether left or
  2161. ;; right) recorded in CACHE (default `c-state-cache') or nil.
  2162. (let ((cash (or cache 'c-state-cache)))
  2163. `(if (consp (car ,cash))
  2164. (cdar ,cash)
  2165. (car ,cash))))
  2166. (defmacro c-state-cache-after-top-paren (&optional cache)
  2167. ;; Return the position just after the latest brace/bracket/paren (whether
  2168. ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
  2169. (let ((cash (or cache 'c-state-cache)))
  2170. `(if (consp (car ,cash))
  2171. (cdar ,cash)
  2172. (and (car ,cash)
  2173. (1+ (car ,cash))))))
  2174. (defun c-get-cache-scan-pos (here)
  2175. ;; From the state-cache, determine the buffer position from which we might
  2176. ;; scan forward to HERE to update this cache. This position will be just
  2177. ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
  2178. ;; return the earliest position in the accessible region which isn't within
  2179. ;; a literal. If the visible portion of the buffer is entirely within a
  2180. ;; literal, return NIL.
  2181. (let ((c c-state-cache) elt)
  2182. ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
  2183. (while (and c
  2184. (>= (c-state-cache-top-lparen c) here))
  2185. (setq c (cdr c)))
  2186. (setq elt (car c))
  2187. (cond
  2188. ((consp elt)
  2189. (if (> (cdr elt) here)
  2190. (1+ (car elt))
  2191. (cdr elt)))
  2192. (elt (1+ elt))
  2193. ((<= (c-state-get-min-scan-pos) here)
  2194. (c-state-get-min-scan-pos))
  2195. (t nil))))
  2196. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2197. ;; Variables which keep track of preprocessor constructs.
  2198. (defvar c-state-old-cpp-beg nil)
  2199. (make-variable-buffer-local 'c-state-old-cpp-beg)
  2200. (defvar c-state-old-cpp-end nil)
  2201. (make-variable-buffer-local 'c-state-old-cpp-end)
  2202. ;; These are the limits of the macro containing point at the previous call of
  2203. ;; `c-parse-state', or nil.
  2204. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2205. ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
  2206. (defun c-get-fallback-scan-pos (here)
  2207. ;; Return a start position for building `c-state-cache' from
  2208. ;; scratch. This will be at the top level, 2 defuns back.
  2209. (save-excursion
  2210. ;; Go back 2 bods, but ignore any bogus positions returned by
  2211. ;; beginning-of-defun (i.e. open paren in column zero).
  2212. (goto-char here)
  2213. (let ((cnt 2))
  2214. (while (not (or (bobp) (zerop cnt)))
  2215. (c-beginning-of-defun-1) ; Pure elisp BOD.
  2216. (if (eq (char-after) ?\{)
  2217. (setq cnt (1- cnt)))))
  2218. (point)))
  2219. (defun c-state-balance-parens-backwards (here- here+ top)
  2220. ;; Return the position of the opening paren/brace/bracket before HERE- which
  2221. ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
  2222. ;; there's a macro, HERE- and HERE+ are the same. Like this:
  2223. ;;
  2224. ;; ............................................
  2225. ;; | |
  2226. ;; ( [ ( .........#macro.. ) ( ) ] )
  2227. ;; ^ ^ ^ ^
  2228. ;; | | | |
  2229. ;; return HERE- HERE+ TOP
  2230. ;;
  2231. ;; If there aren't enough opening paren/brace/brackets, return the position
  2232. ;; of the outermost one found, or HERE- if there are none. If there are no
  2233. ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
  2234. ;; must not be inside literals. Only the accessible portion of the buffer
  2235. ;; will be scanned.
  2236. ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
  2237. ;; `here'. Go round the next loop each time we pass over such a ")". These
  2238. ;; probably match "("s before `here-'.
  2239. (let (pos pa ren+1 lonely-rens)
  2240. (save-excursion
  2241. (save-restriction
  2242. (narrow-to-region (point-min) top) ; This can move point, sometimes.
  2243. (setq pos here+)
  2244. (c-safe
  2245. (while
  2246. (setq ren+1 (scan-lists pos 1 1)) ; might signal
  2247. (setq lonely-rens (cons ren+1 lonely-rens)
  2248. pos ren+1)))))
  2249. ;; PART 2: Scan back before `here-' searching for the "("s
  2250. ;; matching/mismatching the ")"s found above. We only need to direct the
  2251. ;; caller to scan when we've encountered unmatched right parens.
  2252. (setq pos here-)
  2253. (when lonely-rens
  2254. (c-safe
  2255. (while
  2256. (and lonely-rens ; actual values aren't used.
  2257. (setq pa (scan-lists pos -1 1)))
  2258. (setq pos pa)
  2259. (setq lonely-rens (cdr lonely-rens)))))
  2260. pos))
  2261. (defun c-parse-state-get-strategy (here good-pos)
  2262. ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
  2263. ;; to minimize the amount of scanning. HERE is the pertinent position in
  2264. ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
  2265. ;; its head trimmed) is known to be good, or nil if there is no such
  2266. ;; position.
  2267. ;;
  2268. ;; The return value is a list, one of the following:
  2269. ;;
  2270. ;; o - ('forward CACHE-POS START-POINT) - scan forward from START-POINT,
  2271. ;; which is not less than CACHE-POS.
  2272. ;; o - ('backward CACHE-POS nil) - scan backwards (from HERE).
  2273. ;; o - ('BOD nil START-POINT) - scan forwards from START-POINT, which is at the
  2274. ;; top level.
  2275. ;; o - ('IN-LIT nil nil) - point is inside the literal containing point-min.
  2276. ;; , where CACHE-POS is the highest position recorded in `c-state-cache' at
  2277. ;; or below HERE.
  2278. (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
  2279. BOD-pos ; position of 2nd BOD before HERE.
  2280. strategy ; 'forward, 'backward, 'BOD, or 'IN-LIT.
  2281. start-point
  2282. how-far) ; putative scanning distance.
  2283. (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
  2284. (cond
  2285. ((< here (c-state-get-min-scan-pos))
  2286. (setq strategy 'IN-LIT
  2287. start-point nil
  2288. cache-pos nil
  2289. how-far 0))
  2290. ((<= good-pos here)
  2291. (setq strategy 'forward
  2292. start-point (max good-pos cache-pos)
  2293. how-far (- here start-point)))
  2294. ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
  2295. (setq strategy 'backward
  2296. how-far (- good-pos here)))
  2297. (t
  2298. (setq strategy 'forward
  2299. how-far (- here cache-pos)
  2300. start-point cache-pos)))
  2301. ;; Might we be better off starting from the top level, two defuns back,
  2302. ;; instead?
  2303. (when (> how-far c-state-cache-too-far)
  2304. (setq BOD-pos (c-get-fallback-scan-pos here)) ; somewhat EXPENSIVE!!!
  2305. (if (< (- here BOD-pos) how-far)
  2306. (setq strategy 'BOD
  2307. start-point BOD-pos)))
  2308. (list
  2309. strategy
  2310. (and (memq strategy '(forward backward)) cache-pos)
  2311. (and (memq strategy '(forward BOD)) start-point))))
  2312. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2313. ;; Routines which change `c-state-cache' and associated values.
  2314. (defun c-renarrow-state-cache ()
  2315. ;; The region (more precisely, point-min) has changed since we
  2316. ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
  2317. (if (< (point-min) c-state-point-min)
  2318. ;; If point-min has MOVED BACKWARDS then we drop the state completely.
  2319. ;; It would be possible to do a better job here and recalculate the top
  2320. ;; only.
  2321. (progn
  2322. (c-state-mark-point-min-literal)
  2323. (setq c-state-cache nil
  2324. c-state-cache-good-pos c-state-min-scan-pos
  2325. c-state-brace-pair-desert nil))
  2326. ;; point-min has MOVED FORWARD.
  2327. ;; Is the new point-min inside a (different) literal?
  2328. (unless (and c-state-point-min-lit-start ; at prev. point-min
  2329. (< (point-min) (c-state-get-min-scan-pos)))
  2330. (c-state-mark-point-min-literal))
  2331. ;; Cut off a bit of the tail from `c-state-cache'.
  2332. (let ((ptr (cons nil c-state-cache))
  2333. pa)
  2334. (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
  2335. (>= pa (point-min)))
  2336. (setq ptr (cdr ptr)))
  2337. (when (consp ptr)
  2338. (if (eq (cdr ptr) c-state-cache)
  2339. (setq c-state-cache nil
  2340. c-state-cache-good-pos c-state-min-scan-pos)
  2341. (setcdr ptr nil)
  2342. (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
  2343. )))
  2344. (setq c-state-point-min (point-min)))
  2345. (defun c-append-lower-brace-pair-to-state-cache (from &optional upper-lim)
  2346. ;; If there is a brace pair preceding FROM in the buffer, at the same level
  2347. ;; of nesting (not necessarily immediately preceding), push a cons onto
  2348. ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
  2349. ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
  2350. ;; UPPER-LIM.
  2351. ;;
  2352. ;; Return non-nil when this has been done.
  2353. ;;
  2354. ;; The situation it copes with is this transformation:
  2355. ;;
  2356. ;; OLD: { (.) {...........}
  2357. ;; ^ ^
  2358. ;; FROM HERE
  2359. ;;
  2360. ;; NEW: { {....} (.) {.........
  2361. ;; ^ ^ ^
  2362. ;; LOWER BRACE PAIR HERE or HERE
  2363. ;;
  2364. ;; This routine should be fast. Since it can get called a LOT, we maintain
  2365. ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
  2366. ;; reduce the time wasted in repeated fruitless searches in brace deserts.
  2367. (save-excursion
  2368. (save-restriction
  2369. (let ((bra from) ce ; Positions of "{" and "}".
  2370. new-cons
  2371. (cache-pos (c-state-cache-top-lparen)) ; might be nil.
  2372. (macro-start-or-from
  2373. (progn (goto-char from)
  2374. (c-beginning-of-macro)
  2375. (point))))
  2376. (or upper-lim (setq upper-lim from))
  2377. ;; If we're essentially repeating a fruitless search, just give up.
  2378. (unless (and c-state-brace-pair-desert
  2379. (eq cache-pos (car c-state-brace-pair-desert))
  2380. (<= from (cdr c-state-brace-pair-desert)))
  2381. ;; DESERT-LIM. Only search what we absolutely need to,
  2382. (let ((desert-lim
  2383. (and c-state-brace-pair-desert
  2384. (eq cache-pos (car c-state-brace-pair-desert))
  2385. (cdr c-state-brace-pair-desert)))
  2386. ;; CACHE-LIM. This limit will be necessary when an opening
  2387. ;; paren at `cache-pos' has just had its matching close paren
  2388. ;; inserted. `cache-pos' continues to be a search bound, even
  2389. ;; though the algorithm below would skip over the new paren
  2390. ;; pair.
  2391. (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
  2392. (narrow-to-region
  2393. (cond
  2394. ((and desert-lim cache-lim)
  2395. (max desert-lim cache-lim))
  2396. (desert-lim)
  2397. (cache-lim)
  2398. ((point-min)))
  2399. (point-max)))
  2400. ;; In the next pair of nested loops, the inner one moves back past a
  2401. ;; pair of (mis-)matching parens or brackets; the outer one moves
  2402. ;; back over a sequence of unmatched close brace/paren/bracket each
  2403. ;; time round.
  2404. (while
  2405. (progn
  2406. (c-safe
  2407. (while
  2408. (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
  2409. (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
  2410. (or (> ce upper-lim)
  2411. (not (eq (char-after bra) ?\{))
  2412. (and (goto-char bra)
  2413. (c-beginning-of-macro)
  2414. (< (point) macro-start-or-from))))))
  2415. (and ce (< ce bra)))
  2416. (setq bra ce)) ; If we just backed over an unbalanced closing
  2417. ; brace, ignore it.
  2418. (if (and ce (< bra ce) (eq (char-after bra) ?\{))
  2419. ;; We've found the desired brace-pair.
  2420. (progn
  2421. (setq new-cons (cons bra (1+ ce)))
  2422. (cond
  2423. ((consp (car c-state-cache))
  2424. (setcar c-state-cache new-cons))
  2425. ((and (numberp (car c-state-cache)) ; probably never happens
  2426. (< ce (car c-state-cache)))
  2427. (setcdr c-state-cache
  2428. (cons new-cons (cdr c-state-cache))))
  2429. (t (setq c-state-cache (cons new-cons c-state-cache)))))
  2430. ;; We haven't found a brace pair. Record this in the cache.
  2431. (setq c-state-brace-pair-desert (cons cache-pos from))))))))
  2432. (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
  2433. ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
  2434. ;; following a {, and that brace has a (mis-)matching } (or ]), and we
  2435. ;; "push" "a" brace pair onto `c-state-cache'.
  2436. ;;
  2437. ;; Here "push" means overwrite the top element if it's itself a brace-pair,
  2438. ;; otherwise push it normally.
  2439. ;;
  2440. ;; The brace pair we push is normally the one surrounding BRA+1, but if the
  2441. ;; latter is inside a macro, not being a macro containing
  2442. ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
  2443. ;; base pair. This latter case is assumed to be rare.
  2444. ;;
  2445. ;; Note: POINT is not preserved in this routine.
  2446. (if bra+1
  2447. (if (or (> bra+1 macro-start-or-here)
  2448. (progn (goto-char bra+1)
  2449. (not (c-beginning-of-macro))))
  2450. (setq c-state-cache
  2451. (cons (cons (1- bra+1)
  2452. (scan-lists bra+1 1 1))
  2453. (if (consp (car c-state-cache))
  2454. (cdr c-state-cache)
  2455. c-state-cache)))
  2456. ;; N.B. This defsubst codes one method for the simple, normal case,
  2457. ;; and a more sophisticated, slower way for the general case. Don't
  2458. ;; eliminate this defsubst - it's a speed optimization.
  2459. (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
  2460. (defun c-append-to-state-cache (from)
  2461. ;; Scan the buffer from FROM to (point-max), adding elements into
  2462. ;; `c-state-cache' for braces etc. Return a candidate for
  2463. ;; `c-state-cache-good-pos'.
  2464. ;;
  2465. ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
  2466. ;; any. Typically, it is immediately after it. It must not be inside a
  2467. ;; literal.
  2468. (let ((here-bol (c-point 'bol (point-max)))
  2469. (macro-start-or-here
  2470. (save-excursion (goto-char (point-max))
  2471. (if (c-beginning-of-macro)
  2472. (point)
  2473. (point-max))))
  2474. pa+1 ; pos just after an opening PAren (or brace).
  2475. (ren+1 from) ; usually a pos just after an closing paREN etc.
  2476. ; Is actually the pos. to scan for a (/{/[ from,
  2477. ; which sometimes is after a silly )/}/].
  2478. paren+1 ; Pos after some opening or closing paren.
  2479. paren+1s ; A list of `paren+1's; used to determine a
  2480. ; good-pos.
  2481. bra+1 ce+1 ; just after L/R bra-ces.
  2482. bra+1s ; list of OLD values of bra+1.
  2483. mstart) ; start of a macro.
  2484. (save-excursion
  2485. ;; Each time round the following loop, we enter a successively deeper
  2486. ;; level of brace/paren nesting. (Except sometimes we "continue at
  2487. ;; the existing level".) `pa+1' is a pos inside an opening
  2488. ;; brace/paren/bracket, usually just after it.
  2489. (while
  2490. (progn
  2491. ;; Each time round the next loop moves forward over an opening then
  2492. ;; a closing brace/bracket/paren. This loop is white hot, so it
  2493. ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
  2494. ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
  2495. ;; call of `scan-lists' signals an error, which happens when there
  2496. ;; are no more b/b/p's to scan.
  2497. (c-safe
  2498. (while t
  2499. (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
  2500. paren+1s (cons pa+1 paren+1s))
  2501. (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
  2502. (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
  2503. (setq bra+1 pa+1))
  2504. (setcar paren+1s ren+1)))
  2505. (if (and pa+1 (> pa+1 ren+1))
  2506. ;; We've just entered a deeper nesting level.
  2507. (progn
  2508. ;; Insert the brace pair (if present) and the single open
  2509. ;; paren/brace/bracket into `c-state-cache' It cannot be
  2510. ;; inside a macro, except one around point, because of what
  2511. ;; `c-neutralize-syntax-in-CPP' has done.
  2512. (c-state-push-any-brace-pair bra+1 macro-start-or-here)
  2513. ;; Insert the opening brace/bracket/paren position.
  2514. (setq c-state-cache (cons (1- pa+1) c-state-cache))
  2515. ;; Clear admin stuff for the next more nested part of the scan.
  2516. (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
  2517. t) ; Carry on the loop
  2518. ;; All open p/b/b's at this nesting level, if any, have probably
  2519. ;; been closed by matching/mismatching ones. We're probably
  2520. ;; finished - we just need to check for having found an
  2521. ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
  2522. ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
  2523. (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
  2524. ;; Record the final, innermost, brace-pair if there is one.
  2525. (c-state-push-any-brace-pair bra+1 macro-start-or-here)
  2526. ;; Determine a good pos
  2527. (while (and (setq paren+1 (car paren+1s))
  2528. (> (if (> paren+1 macro-start-or-here)
  2529. paren+1
  2530. (goto-char paren+1)
  2531. (setq mstart (and (c-beginning-of-macro)
  2532. (point)))
  2533. (or mstart paren+1))
  2534. here-bol))
  2535. (setq paren+1s (cdr paren+1s)))
  2536. (cond
  2537. ((and paren+1 mstart)
  2538. (min paren+1 mstart))
  2539. (paren+1)
  2540. (t from)))))
  2541. (defun c-remove-stale-state-cache (good-pos pps-point)
  2542. ;; Remove stale entries from the `c-cache-state', i.e. those which will
  2543. ;; not be in it when it is amended for position (point-max).
  2544. ;; Additionally, the "outermost" open-brace entry before (point-max)
  2545. ;; will be converted to a cons if the matching close-brace is scanned.
  2546. ;;
  2547. ;; GOOD-POS is a "maximal" "safe position" - there must be no open
  2548. ;; parens/braces/brackets between GOOD-POS and (point-max).
  2549. ;;
  2550. ;; As a second thing, calculate the result of parse-partial-sexp at
  2551. ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
  2552. ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
  2553. ;; adjust it to get outside a string/comment. (Sorry about this! The code
  2554. ;; needs to be FAST).
  2555. ;;
  2556. ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
  2557. ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
  2558. ;; to be good (we aim for this to be as high as possible);
  2559. ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
  2560. ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
  2561. ;; position to scan backwards from.
  2562. ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
  2563. (save-restriction
  2564. (narrow-to-region 1 (point-max))
  2565. (save-excursion
  2566. (let* ((in-macro-start ; start of macro containing (point-max) or nil.
  2567. (save-excursion
  2568. (goto-char (point-max))
  2569. (and (c-beginning-of-macro)
  2570. (point))))
  2571. (good-pos-actual-macro-start ; Start of macro containing good-pos
  2572. ; or nil
  2573. (and (< good-pos (point-max))
  2574. (save-excursion
  2575. (goto-char good-pos)
  2576. (and (c-beginning-of-macro)
  2577. (point)))))
  2578. (good-pos-actual-macro-end ; End of this macro, (maybe
  2579. ; (point-max)), or nil.
  2580. (and good-pos-actual-macro-start
  2581. (save-excursion
  2582. (goto-char good-pos-actual-macro-start)
  2583. (c-end-of-macro)
  2584. (point))))
  2585. pps-state ; Will be 9 or 10 elements long.
  2586. pos
  2587. upper-lim ; ,beyond which `c-state-cache' entries are removed
  2588. scan-back-pos
  2589. pair-beg pps-point-state target-depth)
  2590. ;; Remove entries beyond (point-max). Also remove any entries inside
  2591. ;; a macro, unless (point-max) is in the same macro.
  2592. (setq upper-lim
  2593. (if (or (null c-state-old-cpp-beg)
  2594. (and (> (point-max) c-state-old-cpp-beg)
  2595. (< (point-max) c-state-old-cpp-end)))
  2596. (point-max)
  2597. (min (point-max) c-state-old-cpp-beg)))
  2598. (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
  2599. (setq c-state-cache (cdr c-state-cache)))
  2600. ;; If `upper-lim' is inside the last recorded brace pair, remove its
  2601. ;; RBrace and indicate we'll need to search backwards for a previous
  2602. ;; brace pair.
  2603. (when (and c-state-cache
  2604. (consp (car c-state-cache))
  2605. (> (cdar c-state-cache) upper-lim))
  2606. (setcar c-state-cache (caar c-state-cache))
  2607. (setq scan-back-pos (car c-state-cache)))
  2608. ;; The next loop jumps forward out of a nested level of parens each
  2609. ;; time round; the corresponding elements in `c-state-cache' are
  2610. ;; removed. `pos' is just after the brace-pair or the open paren at
  2611. ;; (car c-state-cache). There can be no open parens/braces/brackets
  2612. ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
  2613. ;; due to the interface spec to this function.
  2614. (setq pos (if (and good-pos-actual-macro-end
  2615. (not (eq good-pos-actual-macro-start
  2616. in-macro-start)))
  2617. (1+ good-pos-actual-macro-end) ; get outside the macro as
  2618. ; marked by a `category' text property.
  2619. good-pos))
  2620. (goto-char pos)
  2621. (while (and c-state-cache
  2622. (< (point) (point-max)))
  2623. (cond
  2624. ((null pps-state) ; first time through
  2625. (setq target-depth -1))
  2626. ((eq (car pps-state) target-depth) ; found closing ),},]
  2627. (setq target-depth (1- (car pps-state))))
  2628. ;; Do nothing when we've merely reached pps-point.
  2629. )
  2630. ;; Scan!
  2631. (setq pps-state
  2632. (parse-partial-sexp
  2633. (point) (if (< (point) pps-point) pps-point (point-max))
  2634. target-depth
  2635. nil pps-state))
  2636. (if (= (point) pps-point)
  2637. (setq pps-point-state pps-state))
  2638. (when (eq (car pps-state) target-depth)
  2639. (setq pos (point)) ; POS is now just after an R-paren/brace.
  2640. (cond
  2641. ((and (consp (car c-state-cache))
  2642. (eq (point) (cdar c-state-cache)))
  2643. ;; We've just moved out of the paren pair containing the brace-pair
  2644. ;; at (car c-state-cache). `pair-beg' is where the open paren is,
  2645. ;; and is potentially where the open brace of a cons in
  2646. ;; c-state-cache will be.
  2647. (setq pair-beg (car-safe (cdr c-state-cache))
  2648. c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
  2649. ((numberp (car c-state-cache))
  2650. (setq pair-beg (car c-state-cache)
  2651. c-state-cache (cdr c-state-cache))) ; remove this
  2652. ; containing Lparen
  2653. ((numberp (cadr c-state-cache))
  2654. (setq pair-beg (cadr c-state-cache)
  2655. c-state-cache (cddr c-state-cache))) ; Remove a paren pair
  2656. ; together with enclosed brace pair.
  2657. ;; (t nil) ; Ignore an unmated Rparen.
  2658. )))
  2659. (if (< (point) pps-point)
  2660. (setq pps-state (parse-partial-sexp (point) pps-point
  2661. nil nil ; TARGETDEPTH, STOPBEFORE
  2662. pps-state)))
  2663. ;; If the last paren pair we moved out of was actually a brace pair,
  2664. ;; insert it into `c-state-cache'.
  2665. (when (and pair-beg (eq (char-after pair-beg) ?{))
  2666. (if (consp (car-safe c-state-cache))
  2667. (setq c-state-cache (cdr c-state-cache)))
  2668. (setq c-state-cache (cons (cons pair-beg pos)
  2669. c-state-cache)))
  2670. (list pos scan-back-pos pps-state)))))
  2671. (defun c-remove-stale-state-cache-backwards (here cache-pos)
  2672. ;; Strip stale elements of `c-state-cache' by moving backwards through the
  2673. ;; buffer, and inform the caller of the scenario detected.
  2674. ;;
  2675. ;; HERE is the position we're setting `c-state-cache' for.
  2676. ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
  2677. ;; before HERE, or a position at or near point-min which isn't in a
  2678. ;; literal.
  2679. ;;
  2680. ;; This function must only be called only when (> `c-state-cache-good-pos'
  2681. ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
  2682. ;; optimized to eliminate (or minimize) scanning between these two
  2683. ;; positions.
  2684. ;;
  2685. ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
  2686. ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
  2687. ;; could become so after missing elements are inserted into
  2688. ;; `c-state-cache'. This is JUST AFTER an opening or closing
  2689. ;; brace/paren/bracket which is already in `c-state-cache' or just before
  2690. ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
  2691. ;; before `here''s line, or the start of the literal containing it.
  2692. ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
  2693. ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
  2694. ;; to scan backwards from.
  2695. ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
  2696. ;; POS and HERE which aren't recorded in `c-state-cache'.
  2697. ;;
  2698. ;; The comments in this defun use "paren" to mean parenthesis or square
  2699. ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
  2700. ;;
  2701. ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
  2702. ;; | | | | | |
  2703. ;; CP E here D C good
  2704. (let ((pos c-state-cache-good-pos)
  2705. pa ren ; positions of "(" and ")"
  2706. dropped-cons ; whether the last element dropped from `c-state-cache'
  2707. ; was a cons (representing a brace-pair)
  2708. good-pos ; see above.
  2709. lit ; (START . END) of a literal containing some point.
  2710. here-lit-start here-lit-end ; bounds of literal containing `here'
  2711. ; or `here' itself.
  2712. here- here+ ; start/end of macro around HERE, or HERE
  2713. (here-bol (c-point 'bol here))
  2714. (too-far-back (max (- here c-state-cache-too-far) (point-min))))
  2715. ;; Remove completely irrelevant entries from `c-state-cache'.
  2716. (while (and c-state-cache
  2717. (>= (setq pa (c-state-cache-top-lparen)) here))
  2718. (setq dropped-cons (consp (car c-state-cache)))
  2719. (setq c-state-cache (cdr c-state-cache))
  2720. (setq pos pa))
  2721. ;; At this stage, (> pos here);
  2722. ;; (< (c-state-cache-top-lparen) here) (or is nil).
  2723. (cond
  2724. ((and (consp (car c-state-cache))
  2725. (> (cdar c-state-cache) here))
  2726. ;; CASE 1: The top of the cache is a brace pair which now encloses
  2727. ;; `here'. As good-pos, return the address. of the "{". Since we've no
  2728. ;; knowledge of what's inside these braces, we have no alternative but
  2729. ;; to direct the caller to scan the buffer from the opening brace.
  2730. (setq pos (caar c-state-cache))
  2731. (setcar c-state-cache pos)
  2732. (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
  2733. ; entry into a { entry, so the caller needs to
  2734. ; search for a brace pair before the {.
  2735. ;; `here' might be inside a literal. Check for this.
  2736. ((progn
  2737. (setq lit (c-state-literal-at here)
  2738. here-lit-start (or (car lit) here)
  2739. here-lit-end (or (cdr lit) here))
  2740. ;; Has `here' just "newly entered" a macro?
  2741. (save-excursion
  2742. (goto-char here-lit-start)
  2743. (if (and (c-beginning-of-macro)
  2744. (or (null c-state-old-cpp-beg)
  2745. (not (= (point) c-state-old-cpp-beg))))
  2746. (progn
  2747. (setq here- (point))
  2748. (c-end-of-macro)
  2749. (setq here+ (point)))
  2750. (setq here- here-lit-start
  2751. here+ here-lit-end)))
  2752. ;; `here' might be nested inside any depth of parens (or brackets but
  2753. ;; not braces). Scan backwards to find the outermost such opening
  2754. ;; paren, if there is one. This will be the scan position to return.
  2755. (save-restriction
  2756. (narrow-to-region cache-pos (point-max))
  2757. (setq pos (c-state-balance-parens-backwards here- here+ pos)))
  2758. nil)) ; for the cond
  2759. ((< pos here-lit-start)
  2760. ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
  2761. ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
  2762. ;; a brace pair preceding this, it will already be in `c-state-cache',
  2763. ;; unless there was a brace pair after it, i.e. there'll only be one to
  2764. ;; scan for if we've just deleted one.
  2765. (list pos (and dropped-cons pos) t)) ; Return value.
  2766. ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
  2767. ;; Further forward scanning isn't needed, but we still need to find a
  2768. ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
  2769. ((progn
  2770. (save-restriction
  2771. (narrow-to-region here-bol (point-max))
  2772. (setq pos here-lit-start)
  2773. (c-safe (while (setq pa (scan-lists pos -1 1))
  2774. (setq pos pa)))) ; might signal
  2775. nil)) ; for the cond
  2776. ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
  2777. ;; CASE 3: After a }/)/] before `here''s BOL.
  2778. (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
  2779. (t
  2780. ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
  2781. ;; literal containing it.
  2782. (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
  2783. (list good-pos (and dropped-cons good-pos) nil)))))
  2784. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2785. ;; Externally visible routines.
  2786. (defun c-state-cache-init ()
  2787. (setq c-state-cache nil
  2788. c-state-cache-good-pos 1
  2789. c-state-nonlit-pos-cache nil
  2790. c-state-nonlit-pos-cache-limit 1
  2791. c-state-brace-pair-desert nil
  2792. c-state-point-min 1
  2793. c-state-point-min-lit-type nil
  2794. c-state-point-min-lit-start nil
  2795. c-state-min-scan-pos 1
  2796. c-state-old-cpp-beg nil
  2797. c-state-old-cpp-end nil)
  2798. (c-state-mark-point-min-literal))
  2799. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2800. ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
  2801. ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
  2802. ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
  2803. ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
  2804. ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
  2805. ;; (defun c-state-dump ()
  2806. ;; ;; For debugging.
  2807. ;; ;(message
  2808. ;; (concat
  2809. ;; (c-sc-qde c-state-cache)
  2810. ;; (c-sc-de c-state-cache-good-pos)
  2811. ;; (c-sc-qde c-state-nonlit-pos-cache)
  2812. ;; (c-sc-de c-state-nonlit-pos-cache-limit)
  2813. ;; (c-sc-qde c-state-brace-pair-desert)
  2814. ;; (c-sc-de c-state-point-min)
  2815. ;; (c-sc-de c-state-point-min-lit-type)
  2816. ;; (c-sc-de c-state-point-min-lit-start)
  2817. ;; (c-sc-de c-state-min-scan-pos)
  2818. ;; (c-sc-de c-state-old-cpp-beg)
  2819. ;; (c-sc-de c-state-old-cpp-end)))
  2820. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2821. (defun c-invalidate-state-cache-1 (here)
  2822. ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
  2823. ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
  2824. ;; left in a consistent state.
  2825. ;;
  2826. ;; This is much like `c-whack-state-after', but it never changes a paren
  2827. ;; pair element into an open paren element. Doing that would mean that the
  2828. ;; new open paren wouldn't have the required preceding paren pair element.
  2829. ;;
  2830. ;; This function is called from c-after-change.
  2831. ;; The caches of non-literals:
  2832. (if (< here c-state-nonlit-pos-cache-limit)
  2833. (setq c-state-nonlit-pos-cache-limit here))
  2834. (if (< here c-state-semi-nonlit-pos-cache-limit)
  2835. (setq c-state-semi-nonlit-pos-cache-limit here))
  2836. ;; `c-state-cache':
  2837. ;; Case 1: if `here' is in a literal containing point-min, everything
  2838. ;; becomes (or is already) nil.
  2839. (if (or (null c-state-cache-good-pos)
  2840. (< here (c-state-get-min-scan-pos)))
  2841. (setq c-state-cache nil
  2842. c-state-cache-good-pos nil
  2843. c-state-min-scan-pos nil)
  2844. ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
  2845. ;; below `here'. To maintain its consistency, we may need to insert a new
  2846. ;; brace pair.
  2847. (let ((here-bol (c-point 'bol here))
  2848. too-high-pa ; recorded {/(/[ next above here, or nil.
  2849. dropped-cons ; was the last removed element a brace pair?
  2850. pa)
  2851. ;; The easy bit - knock over-the-top bits off `c-state-cache'.
  2852. (while (and c-state-cache
  2853. (>= (setq pa (c-state-cache-top-paren)) here))
  2854. (setq dropped-cons (consp (car c-state-cache))
  2855. too-high-pa (c-state-cache-top-lparen)
  2856. c-state-cache (cdr c-state-cache)))
  2857. ;; Do we need to add in an earlier brace pair, having lopped one off?
  2858. (if (and dropped-cons
  2859. (< too-high-pa (+ here c-state-cache-too-far)))
  2860. (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
  2861. (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
  2862. (c-state-get-min-scan-pos)))))
  2863. ;; The brace-pair desert marker:
  2864. (when (car c-state-brace-pair-desert)
  2865. (if (< here (car c-state-brace-pair-desert))
  2866. (setq c-state-brace-pair-desert nil)
  2867. (if (< here (cdr c-state-brace-pair-desert))
  2868. (setcdr c-state-brace-pair-desert here)))))
  2869. (defun c-parse-state-1 ()
  2870. ;; Find and record all noteworthy parens between some good point earlier in
  2871. ;; the file and point. That good point is at least the beginning of the
  2872. ;; top-level construct we are in, or the beginning of the preceding
  2873. ;; top-level construct if we aren't in one.
  2874. ;;
  2875. ;; The returned value is a list of the noteworthy parens with the last one
  2876. ;; first. If an element in the list is an integer, it's the position of an
  2877. ;; open paren (of any type) which has not been closed before the point. If
  2878. ;; an element is a cons, it gives the position of a closed BRACE paren
  2879. ;; pair[*]; the car is the start brace position and the cdr is the position
  2880. ;; following the closing brace. Only the last closed brace paren pair
  2881. ;; before each open paren and before the point is recorded, and thus the
  2882. ;; state never contains two cons elements in succession. When a close brace
  2883. ;; has no matching open brace (e.g., the matching brace is outside the
  2884. ;; visible region), it is not represented in the returned value.
  2885. ;;
  2886. ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
  2887. ;; This defun explicitly treats mismatching parens/braces/brackets as
  2888. ;; matching. It is the open brace which makes it a "brace" pair.
  2889. ;;
  2890. ;; If POINT is within a macro, open parens and brace pairs within
  2891. ;; THIS macro MIGHT be recorded. This depends on whether their
  2892. ;; syntactic properties have been suppressed by
  2893. ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
  2894. ;;
  2895. ;; Currently no characters which are given paren syntax with the
  2896. ;; syntax-table property are recorded, i.e. angle bracket arglist
  2897. ;; parens are never present here. Note that this might change.
  2898. ;;
  2899. ;; BUG: This function doesn't cope entirely well with unbalanced
  2900. ;; parens in macros. (2008-12-11: this has probably been resolved
  2901. ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
  2902. ;; following case the brace before the macro isn't balanced with the
  2903. ;; one after it:
  2904. ;;
  2905. ;; {
  2906. ;; #define X {
  2907. ;; }
  2908. ;;
  2909. ;; Note to maintainers: this function DOES get called with point
  2910. ;; within comments and strings, so don't assume it doesn't!
  2911. ;;
  2912. ;; This function might do hidden buffer changes.
  2913. (let* ((here (point))
  2914. (here-bopl (c-point 'bopl))
  2915. strategy ; 'forward, 'backward etc..
  2916. ;; Candidate positions to start scanning from:
  2917. cache-pos ; highest position below HERE already existing in
  2918. ; cache (or 1).
  2919. good-pos
  2920. start-point
  2921. bopl-state
  2922. res
  2923. scan-backward-pos scan-forward-p) ; used for 'backward.
  2924. ;; If POINT-MIN has changed, adjust the cache
  2925. (unless (= (point-min) c-state-point-min)
  2926. (c-renarrow-state-cache))
  2927. ;; Strategy?
  2928. (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
  2929. strategy (car res)
  2930. cache-pos (cadr res)
  2931. start-point (nth 2 res))
  2932. (when (eq strategy 'BOD)
  2933. (setq c-state-cache nil
  2934. c-state-cache-good-pos start-point))
  2935. ;; SCAN!
  2936. (save-restriction
  2937. (cond
  2938. ((memq strategy '(forward BOD))
  2939. (narrow-to-region (point-min) here)
  2940. (setq res (c-remove-stale-state-cache start-point here-bopl))
  2941. (setq cache-pos (car res)
  2942. scan-backward-pos (cadr res)
  2943. bopl-state (car (cddr res))) ; will be nil if (< here-bopl
  2944. ; start-point)
  2945. (if scan-backward-pos
  2946. (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
  2947. (setq good-pos
  2948. (c-append-to-state-cache cache-pos))
  2949. (setq c-state-cache-good-pos
  2950. (if (and bopl-state
  2951. (< good-pos (- here c-state-cache-too-far)))
  2952. (c-state-cache-non-literal-place here-bopl bopl-state)
  2953. good-pos)))
  2954. ((eq strategy 'backward)
  2955. (setq res (c-remove-stale-state-cache-backwards here cache-pos)
  2956. good-pos (car res)
  2957. scan-backward-pos (cadr res)
  2958. scan-forward-p (car (cddr res)))
  2959. (if scan-backward-pos
  2960. (c-append-lower-brace-pair-to-state-cache
  2961. scan-backward-pos))
  2962. (setq c-state-cache-good-pos
  2963. (if scan-forward-p
  2964. (progn (narrow-to-region (point-min) here)
  2965. (c-append-to-state-cache good-pos))
  2966. good-pos)))
  2967. (t ; (eq strategy 'IN-LIT)
  2968. (setq c-state-cache nil
  2969. c-state-cache-good-pos nil)))))
  2970. c-state-cache)
  2971. (defun c-invalidate-state-cache (here)
  2972. ;; This is a wrapper over `c-invalidate-state-cache-1'.
  2973. ;;
  2974. ;; It suppresses the syntactic effect of the < and > (template) brackets and
  2975. ;; of all parens in preprocessor constructs, except for any such construct
  2976. ;; containing point. We can then call `c-invalidate-state-cache-1' without
  2977. ;; worrying further about macros and template delimiters.
  2978. (c-with-<->-as-parens-suppressed
  2979. (if (and c-state-old-cpp-beg
  2980. (< c-state-old-cpp-beg here))
  2981. (c-with-all-but-one-cpps-commented-out
  2982. c-state-old-cpp-beg
  2983. (min c-state-old-cpp-end here)
  2984. (c-invalidate-state-cache-1 here))
  2985. (c-with-cpps-commented-out
  2986. (c-invalidate-state-cache-1 here)))))
  2987. (defun c-parse-state ()
  2988. ;; This is a wrapper over `c-parse-state-1'. See that function for a
  2989. ;; description of the functionality and return value.
  2990. ;;
  2991. ;; It suppresses the syntactic effect of the < and > (template) brackets and
  2992. ;; of all parens in preprocessor constructs, except for any such construct
  2993. ;; containing point. We can then call `c-parse-state-1' without worrying
  2994. ;; further about macros and template delimiters.
  2995. (let (here-cpp-beg here-cpp-end)
  2996. (save-excursion
  2997. (when (c-beginning-of-macro)
  2998. (setq here-cpp-beg (point))
  2999. (unless
  3000. (> (setq here-cpp-end (c-syntactic-end-of-macro))
  3001. here-cpp-beg)
  3002. (setq here-cpp-beg nil here-cpp-end nil))))
  3003. ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
  3004. ;; subsystem.
  3005. (prog1
  3006. (c-with-<->-as-parens-suppressed
  3007. (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
  3008. (c-with-all-but-one-cpps-commented-out
  3009. here-cpp-beg here-cpp-end
  3010. (c-parse-state-1))
  3011. (c-with-cpps-commented-out
  3012. (c-parse-state-1))))
  3013. (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
  3014. c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
  3015. )))
  3016. ;; Debug tool to catch cache inconsistencies. This is called from
  3017. ;; 000tests.el.
  3018. (defvar c-debug-parse-state nil)
  3019. (unless (fboundp 'c-real-parse-state)
  3020. (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
  3021. (cc-bytecomp-defun c-real-parse-state)
  3022. (defvar c-parse-state-state nil)
  3023. (defun c-record-parse-state-state ()
  3024. (setq c-parse-state-state
  3025. (mapcar
  3026. (lambda (arg)
  3027. (cons arg (symbol-value arg)))
  3028. '(c-state-cache
  3029. c-state-cache-good-pos
  3030. c-state-nonlit-pos-cache
  3031. c-state-nonlit-pos-cache-limit
  3032. c-state-brace-pair-desert
  3033. c-state-point-min
  3034. c-state-point-min-lit-type
  3035. c-state-point-min-lit-start
  3036. c-state-min-scan-pos
  3037. c-state-old-cpp-beg
  3038. c-state-old-cpp-end))))
  3039. (defun c-replay-parse-state-state ()
  3040. (message
  3041. (concat "(setq "
  3042. (mapconcat
  3043. (lambda (arg)
  3044. (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
  3045. c-parse-state-state " ")
  3046. ")")))
  3047. (defun c-debug-parse-state ()
  3048. (let ((here (point)) (res1 (c-real-parse-state)) res2)
  3049. (let ((c-state-cache nil)
  3050. (c-state-cache-good-pos 1)
  3051. (c-state-nonlit-pos-cache nil)
  3052. (c-state-nonlit-pos-cache-limit 1)
  3053. (c-state-brace-pair-desert nil)
  3054. (c-state-point-min 1)
  3055. (c-state-point-min-lit-type nil)
  3056. (c-state-point-min-lit-start nil)
  3057. (c-state-min-scan-pos 1)
  3058. (c-state-old-cpp-beg nil)
  3059. (c-state-old-cpp-end nil))
  3060. (setq res2 (c-real-parse-state)))
  3061. (unless (equal res1 res2)
  3062. ;; The cache can actually go further back due to the ad-hoc way
  3063. ;; the first paren is found, so try to whack off a bit of its
  3064. ;; start before complaining.
  3065. ;; (save-excursion
  3066. ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
  3067. ;; (c-beginning-of-defun-1)
  3068. ;; (while (not (or (bobp) (eq (char-after) ?{)))
  3069. ;; (c-beginning-of-defun-1))
  3070. ;; (unless (equal (c-whack-state-before (point) res1) res2)
  3071. ;; (message (concat "c-parse-state inconsistency at %s: "
  3072. ;; "using cache: %s, from scratch: %s")
  3073. ;; here res1 res2)))
  3074. (message (concat "c-parse-state inconsistency at %s: "
  3075. "using cache: %s, from scratch: %s")
  3076. here res1 res2)
  3077. (message "Old state:")
  3078. (c-replay-parse-state-state))
  3079. (c-record-parse-state-state)
  3080. res1))
  3081. (defun c-toggle-parse-state-debug (&optional arg)
  3082. (interactive "P")
  3083. (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
  3084. (fset 'c-parse-state (symbol-function (if c-debug-parse-state
  3085. 'c-debug-parse-state
  3086. 'c-real-parse-state)))
  3087. (c-keep-region-active))
  3088. (when c-debug-parse-state
  3089. (c-toggle-parse-state-debug 1))
  3090. (defun c-whack-state-before (bufpos paren-state)
  3091. ;; Whack off any state information from PAREN-STATE which lies
  3092. ;; before BUFPOS. Not destructive on PAREN-STATE.
  3093. (let* ((newstate (list nil))
  3094. (ptr newstate)
  3095. car)
  3096. (while paren-state
  3097. (setq car (car paren-state)
  3098. paren-state (cdr paren-state))
  3099. (if (< (if (consp car) (car car) car) bufpos)
  3100. (setq paren-state nil)
  3101. (setcdr ptr (list car))
  3102. (setq ptr (cdr ptr))))
  3103. (cdr newstate)))
  3104. (defun c-whack-state-after (bufpos paren-state)
  3105. ;; Whack off any state information from PAREN-STATE which lies at or
  3106. ;; after BUFPOS. Not destructive on PAREN-STATE.
  3107. (catch 'done
  3108. (while paren-state
  3109. (let ((car (car paren-state)))
  3110. (if (consp car)
  3111. ;; just check the car, because in a balanced brace
  3112. ;; expression, it must be impossible for the corresponding
  3113. ;; close brace to be before point, but the open brace to
  3114. ;; be after.
  3115. (if (<= bufpos (car car))
  3116. nil ; whack it off
  3117. (if (< bufpos (cdr car))
  3118. ;; its possible that the open brace is before
  3119. ;; bufpos, but the close brace is after. In that
  3120. ;; case, convert this to a non-cons element. The
  3121. ;; rest of the state is before bufpos, so we're
  3122. ;; done.
  3123. (throw 'done (cons (car car) (cdr paren-state)))
  3124. ;; we know that both the open and close braces are
  3125. ;; before bufpos, so we also know that everything else
  3126. ;; on state is before bufpos.
  3127. (throw 'done paren-state)))
  3128. (if (<= bufpos car)
  3129. nil ; whack it off
  3130. ;; it's before bufpos, so everything else should too.
  3131. (throw 'done paren-state)))
  3132. (setq paren-state (cdr paren-state)))
  3133. nil)))
  3134. (defun c-most-enclosing-brace (paren-state &optional bufpos)
  3135. ;; Return the bufpos of the innermost enclosing open paren before
  3136. ;; bufpos, or nil if none was found.
  3137. (let (enclosingp)
  3138. (or bufpos (setq bufpos 134217727))
  3139. (while paren-state
  3140. (setq enclosingp (car paren-state)
  3141. paren-state (cdr paren-state))
  3142. (if (or (consp enclosingp)
  3143. (>= enclosingp bufpos))
  3144. (setq enclosingp nil)
  3145. (setq paren-state nil)))
  3146. enclosingp))
  3147. (defun c-least-enclosing-brace (paren-state)
  3148. ;; Return the bufpos of the outermost enclosing open paren, or nil
  3149. ;; if none was found.
  3150. (let (pos elem)
  3151. (while paren-state
  3152. (setq elem (car paren-state)
  3153. paren-state (cdr paren-state))
  3154. (if (integerp elem)
  3155. (setq pos elem)))
  3156. pos))
  3157. (defun c-safe-position (bufpos paren-state)
  3158. ;; Return the closest "safe" position recorded on PAREN-STATE that
  3159. ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
  3160. ;; contain any. Return nil if BUFPOS is nil, which is useful to
  3161. ;; find the closest limit before a given limit that might be nil.
  3162. ;;
  3163. ;; A "safe" position is a position at or after a recorded open
  3164. ;; paren, or after a recorded close paren. The returned position is
  3165. ;; thus either the first position after a close brace, or the first
  3166. ;; position after an enclosing paren, or at the enclosing paren in
  3167. ;; case BUFPOS is immediately after it.
  3168. (when bufpos
  3169. (let (elem)
  3170. (catch 'done
  3171. (while paren-state
  3172. (setq elem (car paren-state))
  3173. (if (consp elem)
  3174. (cond ((< (cdr elem) bufpos)
  3175. (throw 'done (cdr elem)))
  3176. ((< (car elem) bufpos)
  3177. ;; See below.
  3178. (throw 'done (min (1+ (car elem)) bufpos))))
  3179. (if (< elem bufpos)
  3180. ;; elem is the position at and not after the opening paren, so
  3181. ;; we can go forward one more step unless it's equal to
  3182. ;; bufpos. This is useful in some cases avoid an extra paren
  3183. ;; level between the safe position and bufpos.
  3184. (throw 'done (min (1+ elem) bufpos))))
  3185. (setq paren-state (cdr paren-state)))))))
  3186. (defun c-beginning-of-syntax ()
  3187. ;; This is used for `font-lock-beginning-of-syntax-function'. It
  3188. ;; goes to the closest previous point that is known to be outside
  3189. ;; any string literal or comment. `c-state-cache' is used if it has
  3190. ;; a position in the vicinity.
  3191. (let* ((paren-state c-state-cache)
  3192. elem
  3193. (pos (catch 'done
  3194. ;; Note: Similar code in `c-safe-position'. The
  3195. ;; difference is that we accept a safe position at
  3196. ;; the point and don't bother to go forward past open
  3197. ;; parens.
  3198. (while paren-state
  3199. (setq elem (car paren-state))
  3200. (if (consp elem)
  3201. (cond ((<= (cdr elem) (point))
  3202. (throw 'done (cdr elem)))
  3203. ((<= (car elem) (point))
  3204. (throw 'done (car elem))))
  3205. (if (<= elem (point))
  3206. (throw 'done elem)))
  3207. (setq paren-state (cdr paren-state)))
  3208. (point-min))))
  3209. (if (> pos (- (point) 4000))
  3210. (goto-char pos)
  3211. ;; The position is far back. Try `c-beginning-of-defun-1'
  3212. ;; (although we can't be entirely sure it will go to a position
  3213. ;; outside a comment or string in current emacsen). FIXME:
  3214. ;; Consult `syntax-ppss' here.
  3215. (c-beginning-of-defun-1)
  3216. (if (< (point) pos)
  3217. (goto-char pos)))))
  3218. ;; Tools for scanning identifiers and other tokens.
  3219. (defun c-on-identifier ()
  3220. "Return non-nil if the point is on or directly after an identifier.
  3221. Keywords are recognized and not considered identifiers. If an
  3222. identifier is detected, the returned value is its starting position.
  3223. If an identifier ends at the point and another begins at it \(can only
  3224. happen in Pike) then the point for the preceding one is returned.
  3225. Note that this function might do hidden buffer changes. See the
  3226. comment at the start of cc-engine.el for more info."
  3227. ;; FIXME: Shouldn't this function handle "operator" in C++?
  3228. (save-excursion
  3229. (skip-syntax-backward "w_")
  3230. (or
  3231. ;; Check for a normal (non-keyword) identifier.
  3232. (and (looking-at c-symbol-start)
  3233. (not (looking-at c-keywords-regexp))
  3234. (point))
  3235. (when (c-major-mode-is 'pike-mode)
  3236. ;; Handle the `<operator> syntax in Pike.
  3237. (let ((pos (point)))
  3238. (skip-chars-backward "-!%&*+/<=>^|~[]()")
  3239. (and (if (< (skip-chars-backward "`") 0)
  3240. t
  3241. (goto-char pos)
  3242. (eq (char-after) ?\`))
  3243. (looking-at c-symbol-key)
  3244. (>= (match-end 0) pos)
  3245. (point))))
  3246. ;; Handle the "operator +" syntax in C++.
  3247. (when (and c-overloadable-operators-regexp
  3248. (= (c-backward-token-2 0) 0))
  3249. (cond ((and (looking-at c-overloadable-operators-regexp)
  3250. (or (not c-opt-op-identifier-prefix)
  3251. (and (= (c-backward-token-2 1) 0)
  3252. (looking-at c-opt-op-identifier-prefix))))
  3253. (point))
  3254. ((save-excursion
  3255. (and c-opt-op-identifier-prefix
  3256. (looking-at c-opt-op-identifier-prefix)
  3257. (= (c-forward-token-2 1) 0)
  3258. (looking-at c-overloadable-operators-regexp)))
  3259. (point))))
  3260. )))
  3261. (defsubst c-simple-skip-symbol-backward ()
  3262. ;; If the point is at the end of a symbol then skip backward to the
  3263. ;; beginning of it. Don't move otherwise. Return non-nil if point
  3264. ;; moved.
  3265. ;;
  3266. ;; This function might do hidden buffer changes.
  3267. (or (< (skip-syntax-backward "w_") 0)
  3268. (and (c-major-mode-is 'pike-mode)
  3269. ;; Handle the `<operator> syntax in Pike.
  3270. (let ((pos (point)))
  3271. (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
  3272. (< (skip-chars-backward "`") 0)
  3273. (looking-at c-symbol-key)
  3274. (>= (match-end 0) pos))
  3275. t
  3276. (goto-char pos)
  3277. nil)))))
  3278. (defun c-beginning-of-current-token (&optional back-limit)
  3279. ;; Move to the beginning of the current token. Do not move if not
  3280. ;; in the middle of one. BACK-LIMIT may be used to bound the
  3281. ;; backward search; if given it's assumed to be at the boundary
  3282. ;; between two tokens. Return non-nil if the point is moved, nil
  3283. ;; otherwise.
  3284. ;;
  3285. ;; This function might do hidden buffer changes.
  3286. (let ((start (point)))
  3287. (if (looking-at "\\w\\|\\s_")
  3288. (skip-syntax-backward "w_" back-limit)
  3289. (when (< (skip-syntax-backward ".()" back-limit) 0)
  3290. (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
  3291. (match-end 0))
  3292. ;; `c-nonsymbol-token-regexp' should always match
  3293. ;; since we've skipped backward over punctuator
  3294. ;; or paren syntax, but consume one char in case
  3295. ;; it doesn't so that we don't leave point before
  3296. ;; some earlier incorrect token.
  3297. (1+ (point)))))
  3298. (if (<= pos start)
  3299. (goto-char pos))))))
  3300. (< (point) start)))
  3301. (defun c-end-of-current-token (&optional back-limit)
  3302. ;; Move to the end of the current token. Do not move if not in the
  3303. ;; middle of one. BACK-LIMIT may be used to bound the backward
  3304. ;; search; if given it's assumed to be at the boundary between two
  3305. ;; tokens. Return non-nil if the point is moved, nil otherwise.
  3306. ;;
  3307. ;; This function might do hidden buffer changes.
  3308. (let ((start (point)))
  3309. (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
  3310. (skip-syntax-forward "w_"))
  3311. ((< (skip-syntax-backward ".()" back-limit) 0)
  3312. (while (progn
  3313. (if (looking-at c-nonsymbol-token-regexp)
  3314. (goto-char (match-end 0))
  3315. ;; `c-nonsymbol-token-regexp' should always match since
  3316. ;; we've skipped backward over punctuator or paren
  3317. ;; syntax, but move forward in case it doesn't so that
  3318. ;; we don't leave point earlier than we started with.
  3319. (forward-char))
  3320. (< (point) start)))))
  3321. (> (point) start)))
  3322. (defconst c-jump-syntax-balanced
  3323. (if (memq 'gen-string-delim c-emacs-features)
  3324. "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
  3325. "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
  3326. (defconst c-jump-syntax-unbalanced
  3327. (if (memq 'gen-string-delim c-emacs-features)
  3328. "\\w\\|\\s_\\|\\s\"\\|\\s|"
  3329. "\\w\\|\\s_\\|\\s\""))
  3330. (defun c-forward-token-2 (&optional count balanced limit)
  3331. "Move forward by tokens.
  3332. A token is defined as all symbols and identifiers which aren't
  3333. syntactic whitespace \(note that multicharacter tokens like \"==\" are
  3334. treated properly). Point is always either left at the beginning of a
  3335. token or not moved at all. COUNT specifies the number of tokens to
  3336. move; a negative COUNT moves in the opposite direction. A COUNT of 0
  3337. moves to the next token beginning only if not already at one. If
  3338. BALANCED is true, move over balanced parens, otherwise move into them.
  3339. Also, if BALANCED is true, never move out of an enclosing paren.
  3340. LIMIT sets the limit for the movement and defaults to the point limit.
  3341. The case when LIMIT is set in the middle of a token, comment or macro
  3342. is handled correctly, i.e. the point won't be left there.
  3343. Return the number of tokens left to move \(positive or negative). If
  3344. BALANCED is true, a move over a balanced paren counts as one. Note
  3345. that if COUNT is 0 and no appropriate token beginning is found, 1 will
  3346. be returned. Thus, a return value of 0 guarantees that point is at
  3347. the requested position and a return value less \(without signs) than
  3348. COUNT guarantees that point is at the beginning of some token.
  3349. Note that this function might do hidden buffer changes. See the
  3350. comment at the start of cc-engine.el for more info."
  3351. (or count (setq count 1))
  3352. (if (< count 0)
  3353. (- (c-backward-token-2 (- count) balanced limit))
  3354. (let ((jump-syntax (if balanced
  3355. c-jump-syntax-balanced
  3356. c-jump-syntax-unbalanced))
  3357. (last (point))
  3358. (prev (point)))
  3359. (if (zerop count)
  3360. ;; If count is zero we should jump if in the middle of a token.
  3361. (c-end-of-current-token))
  3362. (save-restriction
  3363. (if limit (narrow-to-region (point-min) limit))
  3364. (if (/= (point)
  3365. (progn (c-forward-syntactic-ws) (point)))
  3366. ;; Skip whitespace. Count this as a move if we did in
  3367. ;; fact move.
  3368. (setq count (max (1- count) 0)))
  3369. (if (eobp)
  3370. ;; Moved out of bounds. Make sure the returned count isn't zero.
  3371. (progn
  3372. (if (zerop count) (setq count 1))
  3373. (goto-char last))
  3374. ;; Use `condition-case' to avoid having the limit tests
  3375. ;; inside the loop.
  3376. (condition-case nil
  3377. (while (and
  3378. (> count 0)
  3379. (progn
  3380. (setq last (point))
  3381. (cond ((looking-at jump-syntax)
  3382. (goto-char (scan-sexps (point) 1))
  3383. t)
  3384. ((looking-at c-nonsymbol-token-regexp)
  3385. (goto-char (match-end 0))
  3386. t)
  3387. ;; `c-nonsymbol-token-regexp' above should always
  3388. ;; match if there are correct tokens. Try to
  3389. ;; widen to see if the limit was set in the
  3390. ;; middle of one, else fall back to treating
  3391. ;; the offending thing as a one character token.
  3392. ((and limit
  3393. (save-restriction
  3394. (widen)
  3395. (looking-at c-nonsymbol-token-regexp)))
  3396. nil)
  3397. (t
  3398. (forward-char)
  3399. t))))
  3400. (c-forward-syntactic-ws)
  3401. (setq prev last
  3402. count (1- count)))
  3403. (error (goto-char last)))
  3404. (when (eobp)
  3405. (goto-char prev)
  3406. (setq count (1+ count)))))
  3407. count)))
  3408. (defun c-backward-token-2 (&optional count balanced limit)
  3409. "Move backward by tokens.
  3410. See `c-forward-token-2' for details."
  3411. (or count (setq count 1))
  3412. (if (< count 0)
  3413. (- (c-forward-token-2 (- count) balanced limit))
  3414. (or limit (setq limit (point-min)))
  3415. (let ((jump-syntax (if balanced
  3416. c-jump-syntax-balanced
  3417. c-jump-syntax-unbalanced))
  3418. (last (point)))
  3419. (if (zerop count)
  3420. ;; The count is zero so try to skip to the beginning of the
  3421. ;; current token.
  3422. (if (> (point)
  3423. (progn (c-beginning-of-current-token) (point)))
  3424. (if (< (point) limit)
  3425. ;; The limit is inside the same token, so return 1.
  3426. (setq count 1))
  3427. ;; We're not in the middle of a token. If there's
  3428. ;; whitespace after the point then we must move backward,
  3429. ;; so set count to 1 in that case.
  3430. (and (looking-at c-syntactic-ws-start)
  3431. ;; If we're looking at a '#' that might start a cpp
  3432. ;; directive then we have to do a more elaborate check.
  3433. (or (/= (char-after) ?#)
  3434. (not c-opt-cpp-prefix)
  3435. (save-excursion
  3436. (and (= (point)
  3437. (progn (beginning-of-line)
  3438. (looking-at "[ \t]*")
  3439. (match-end 0)))
  3440. (or (bobp)
  3441. (progn (backward-char)
  3442. (not (eq (char-before) ?\\)))))))
  3443. (setq count 1))))
  3444. ;; Use `condition-case' to avoid having to check for buffer
  3445. ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
  3446. (condition-case nil
  3447. (while (and
  3448. (> count 0)
  3449. (progn
  3450. (c-backward-syntactic-ws)
  3451. (backward-char)
  3452. (if (looking-at jump-syntax)
  3453. (goto-char (scan-sexps (1+ (point)) -1))
  3454. ;; This can be very inefficient if there's a long
  3455. ;; sequence of operator tokens without any separation.
  3456. ;; That doesn't happen in practice, anyway.
  3457. (c-beginning-of-current-token))
  3458. (>= (point) limit)))
  3459. (setq last (point)
  3460. count (1- count)))
  3461. (error (goto-char last)))
  3462. (if (< (point) limit)
  3463. (goto-char last))
  3464. count)))
  3465. (defun c-forward-token-1 (&optional count balanced limit)
  3466. "Like `c-forward-token-2' but doesn't treat multicharacter operator
  3467. tokens like \"==\" as single tokens, i.e. all sequences of symbol
  3468. characters are jumped over character by character. This function is
  3469. for compatibility only; it's only a wrapper over `c-forward-token-2'."
  3470. (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
  3471. (c-forward-token-2 count balanced limit)))
  3472. (defun c-backward-token-1 (&optional count balanced limit)
  3473. "Like `c-backward-token-2' but doesn't treat multicharacter operator
  3474. tokens like \"==\" as single tokens, i.e. all sequences of symbol
  3475. characters are jumped over character by character. This function is
  3476. for compatibility only; it's only a wrapper over `c-backward-token-2'."
  3477. (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
  3478. (c-backward-token-2 count balanced limit)))
  3479. ;; Tools for doing searches restricted to syntactically relevant text.
  3480. (defun c-syntactic-re-search-forward (regexp &optional bound noerror
  3481. paren-level not-inside-token
  3482. lookbehind-submatch)
  3483. "Like `re-search-forward', but only report matches that are found
  3484. in syntactically significant text. I.e. matches in comments, macros
  3485. or string literals are ignored. The start point is assumed to be
  3486. outside any comment, macro or string literal, or else the content of
  3487. that region is taken as syntactically significant text.
  3488. If PAREN-LEVEL is non-nil, an additional restriction is added to
  3489. ignore matches in nested paren sexps. The search will also not go
  3490. outside the current list sexp, which has the effect that if the point
  3491. should be moved to BOUND when no match is found \(i.e. NOERROR is
  3492. neither nil nor t), then it will be at the closing paren if the end of
  3493. the current list sexp is encountered first.
  3494. If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
  3495. ignored. Things like multicharacter operators and special symbols
  3496. \(e.g. \"`()\" in Pike) are handled but currently not floating point
  3497. constants.
  3498. If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
  3499. subexpression in REGEXP. The end of that submatch is used as the
  3500. position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
  3501. isn't used or if that subexpression didn't match then the start
  3502. position of the whole match is used instead. The \"look behind\"
  3503. subexpression is never tested before the starting position, so it
  3504. might be a good idea to include \\=\\= as a match alternative in it.
  3505. Optimization note: Matches might be missed if the \"look behind\"
  3506. subexpression can match the end of nonwhite syntactic whitespace,
  3507. i.e. the end of comments or cpp directives. This since the function
  3508. skips over such things before resuming the search. It's on the other
  3509. hand not safe to assume that the \"look behind\" subexpression never
  3510. matches syntactic whitespace.
  3511. Bug: Unbalanced parens inside cpp directives are currently not handled
  3512. correctly \(i.e. they don't get ignored as they should) when
  3513. PAREN-LEVEL is set.
  3514. Note that this function might do hidden buffer changes. See the
  3515. comment at the start of cc-engine.el for more info."
  3516. (or bound (setq bound (point-max)))
  3517. (if paren-level (setq paren-level -1))
  3518. ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
  3519. (let ((start (point))
  3520. tmp
  3521. ;; Start position for the last search.
  3522. search-pos
  3523. ;; The `parse-partial-sexp' state between the start position
  3524. ;; and the point.
  3525. state
  3526. ;; The current position after the last state update. The next
  3527. ;; `parse-partial-sexp' continues from here.
  3528. (state-pos (point))
  3529. ;; The position at which to check the state and the state
  3530. ;; there. This is separate from `state-pos' since we might
  3531. ;; need to back up before doing the next search round.
  3532. check-pos check-state
  3533. ;; Last position known to end a token.
  3534. (last-token-end-pos (point-min))
  3535. ;; Set when a valid match is found.
  3536. found)
  3537. (condition-case err
  3538. (while
  3539. (and
  3540. (progn
  3541. (setq search-pos (point))
  3542. (re-search-forward regexp bound noerror))
  3543. (progn
  3544. (setq state (parse-partial-sexp
  3545. state-pos (match-beginning 0) paren-level nil state)
  3546. state-pos (point))
  3547. (if (setq check-pos (and lookbehind-submatch
  3548. (or (not paren-level)
  3549. (>= (car state) 0))
  3550. (match-end lookbehind-submatch)))
  3551. (setq check-state (parse-partial-sexp
  3552. state-pos check-pos paren-level nil state))
  3553. (setq check-pos state-pos
  3554. check-state state))
  3555. ;; NOTE: If we got a look behind subexpression and get
  3556. ;; an insignificant match in something that isn't
  3557. ;; syntactic whitespace (i.e. strings or in nested
  3558. ;; parentheses), then we can never skip more than a
  3559. ;; single character from the match start position
  3560. ;; (i.e. `state-pos' here) before continuing the
  3561. ;; search. That since the look behind subexpression
  3562. ;; might match the end of the insignificant region in
  3563. ;; the next search.
  3564. (cond
  3565. ((elt check-state 7)
  3566. ;; Match inside a line comment. Skip to eol. Use
  3567. ;; `re-search-forward' instead of `skip-chars-forward' to get
  3568. ;; the right bound behavior.
  3569. (re-search-forward "[\n\r]" bound noerror))
  3570. ((elt check-state 4)
  3571. ;; Match inside a block comment. Skip to the '*/'.
  3572. (search-forward "*/" bound noerror))
  3573. ((and (not (elt check-state 5))
  3574. (eq (char-before check-pos) ?/)
  3575. (not (c-get-char-property (1- check-pos) 'syntax-table))
  3576. (memq (char-after check-pos) '(?/ ?*)))
  3577. ;; Match in the middle of the opener of a block or line
  3578. ;; comment.
  3579. (if (= (char-after check-pos) ?/)
  3580. (re-search-forward "[\n\r]" bound noerror)
  3581. (search-forward "*/" bound noerror)))
  3582. ;; The last `parse-partial-sexp' above might have
  3583. ;; stopped short of the real check position if the end
  3584. ;; of the current sexp was encountered in paren-level
  3585. ;; mode. The checks above are always false in that
  3586. ;; case, and since they can do better skipping in
  3587. ;; lookbehind-submatch mode, we do them before
  3588. ;; checking the paren level.
  3589. ((and paren-level
  3590. (/= (setq tmp (car check-state)) 0))
  3591. ;; Check the paren level first since we're short of the
  3592. ;; syntactic checking position if the end of the
  3593. ;; current sexp was encountered by `parse-partial-sexp'.
  3594. (if (> tmp 0)
  3595. ;; Inside a nested paren sexp.
  3596. (if lookbehind-submatch
  3597. ;; See the NOTE above.
  3598. (progn (goto-char state-pos) t)
  3599. ;; Skip out of the paren quickly.
  3600. (setq state (parse-partial-sexp state-pos bound 0 nil state)
  3601. state-pos (point)))
  3602. ;; Have exited the current paren sexp.
  3603. (if noerror
  3604. (progn
  3605. ;; The last `parse-partial-sexp' call above
  3606. ;; has left us just after the closing paren
  3607. ;; in this case, so we can modify the bound
  3608. ;; to leave the point at the right position
  3609. ;; upon return.
  3610. (setq bound (1- (point)))
  3611. nil)
  3612. (signal 'search-failed (list regexp)))))
  3613. ((setq tmp (elt check-state 3))
  3614. ;; Match inside a string.
  3615. (if (or lookbehind-submatch
  3616. (not (integerp tmp)))
  3617. ;; See the NOTE above.
  3618. (progn (goto-char state-pos) t)
  3619. ;; Skip to the end of the string before continuing.
  3620. (let ((ender (make-string 1 tmp)) (continue t))
  3621. (while (if (search-forward ender bound noerror)
  3622. (progn
  3623. (setq state (parse-partial-sexp
  3624. state-pos (point) nil nil state)
  3625. state-pos (point))
  3626. (elt state 3))
  3627. (setq continue nil)))
  3628. continue)))
  3629. ((save-excursion
  3630. (save-match-data
  3631. (c-beginning-of-macro start)))
  3632. ;; Match inside a macro. Skip to the end of it.
  3633. (c-end-of-macro)
  3634. (cond ((<= (point) bound) t)
  3635. (noerror nil)
  3636. (t (signal 'search-failed (list regexp)))))
  3637. ((and not-inside-token
  3638. (or (< check-pos last-token-end-pos)
  3639. (< check-pos
  3640. (save-excursion
  3641. (goto-char check-pos)
  3642. (save-match-data
  3643. (c-end-of-current-token last-token-end-pos))
  3644. (setq last-token-end-pos (point))))))
  3645. ;; Inside a token.
  3646. (if lookbehind-submatch
  3647. ;; See the NOTE above.
  3648. (goto-char state-pos)
  3649. (goto-char (min last-token-end-pos bound))))
  3650. (t
  3651. ;; A real match.
  3652. (setq found t)
  3653. nil)))
  3654. ;; Should loop to search again, but take care to avoid
  3655. ;; looping on the same spot.
  3656. (or (/= search-pos (point))
  3657. (if (= (point) bound)
  3658. (if noerror
  3659. nil
  3660. (signal 'search-failed (list regexp)))
  3661. (forward-char)
  3662. t))))
  3663. (error
  3664. (goto-char start)
  3665. (signal (car err) (cdr err))))
  3666. ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
  3667. (if found
  3668. (progn
  3669. (goto-char (match-end 0))
  3670. (match-end 0))
  3671. ;; Search failed. Set point as appropriate.
  3672. (if (eq noerror t)
  3673. (goto-char start)
  3674. (goto-char bound))
  3675. nil)))
  3676. (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
  3677. (defsubst c-ssb-lit-begin ()
  3678. ;; Return the start of the literal point is in, or nil.
  3679. ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
  3680. ;; bound in the caller.
  3681. ;; Use `parse-partial-sexp' from a safe position down to the point to check
  3682. ;; if it's outside comments and strings.
  3683. (save-excursion
  3684. (let ((pos (point)) safe-pos state pps-end-pos)
  3685. ;; Pick a safe position as close to the point as possible.
  3686. ;;
  3687. ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
  3688. ;; position.
  3689. (while (and safe-pos-list
  3690. (> (car safe-pos-list) (point)))
  3691. (setq safe-pos-list (cdr safe-pos-list)))
  3692. (unless (setq safe-pos (car-safe safe-pos-list))
  3693. (setq safe-pos (max (or (c-safe-position
  3694. (point) (or c-state-cache
  3695. (c-parse-state)))
  3696. 0)
  3697. (point-min))
  3698. safe-pos-list (list safe-pos)))
  3699. ;; Cache positions along the way to use if we have to back up more. We
  3700. ;; cache every closing paren on the same level. If the paren cache is
  3701. ;; relevant in this region then we're typically already on the same
  3702. ;; level as the target position. Note that we might cache positions
  3703. ;; after opening parens in case safe-pos is in a nested list. That's
  3704. ;; both uncommon and harmless.
  3705. (while (progn
  3706. (setq state (parse-partial-sexp
  3707. safe-pos pos 0))
  3708. (< (point) pos))
  3709. (setq safe-pos (point)
  3710. safe-pos-list (cons safe-pos safe-pos-list)))
  3711. ;; If the state contains the start of the containing sexp we cache that
  3712. ;; position too, so that parse-partial-sexp in the next run has a bigger
  3713. ;; chance of starting at the same level as the target position and thus
  3714. ;; will get more good safe positions into the list.
  3715. (if (elt state 1)
  3716. (setq safe-pos (1+ (elt state 1))
  3717. safe-pos-list (cons safe-pos safe-pos-list)))
  3718. (if (or (elt state 3) (elt state 4))
  3719. ;; Inside string or comment. Continue search at the
  3720. ;; beginning of it.
  3721. (elt state 8)))))
  3722. (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
  3723. "Like `skip-chars-backward' but only look at syntactically relevant chars,
  3724. i.e. don't stop at positions inside syntactic whitespace or string
  3725. literals. Preprocessor directives are also ignored, with the exception
  3726. of the one that the point starts within, if any. If LIMIT is given,
  3727. it's assumed to be at a syntactically relevant position.
  3728. If PAREN-LEVEL is non-nil, the function won't stop in nested paren
  3729. sexps, and the search will also not go outside the current paren sexp.
  3730. However, if LIMIT or the buffer limit is reached inside a nested paren
  3731. then the point will be left at the limit.
  3732. Non-nil is returned if the point moved, nil otherwise.
  3733. Note that this function might do hidden buffer changes. See the
  3734. comment at the start of cc-engine.el for more info."
  3735. (let ((start (point))
  3736. state-2
  3737. ;; A list of syntactically relevant positions in descending
  3738. ;; order. It's used to avoid scanning repeatedly over
  3739. ;; potentially large regions with `parse-partial-sexp' to verify
  3740. ;; each position. Used in `c-ssb-lit-begin'
  3741. safe-pos-list
  3742. ;; The result from `c-beginning-of-macro' at the start position or the
  3743. ;; start position itself if it isn't within a macro. Evaluated on
  3744. ;; demand.
  3745. start-macro-beg
  3746. ;; The earliest position after the current one with the same paren
  3747. ;; level. Used only when `paren-level' is set.
  3748. lit-beg
  3749. (paren-level-pos (point)))
  3750. (while
  3751. (progn
  3752. ;; The next loop "tries" to find the end point each time round,
  3753. ;; loops when it hasn't succeeded.
  3754. (while
  3755. (and
  3756. (< (skip-chars-backward skip-chars limit) 0)
  3757. (let ((pos (point)) state-2 pps-end-pos)
  3758. (cond
  3759. ;; Don't stop inside a literal
  3760. ((setq lit-beg (c-ssb-lit-begin))
  3761. (goto-char lit-beg)
  3762. t)
  3763. ((and paren-level
  3764. (save-excursion
  3765. (setq state-2 (parse-partial-sexp
  3766. pos paren-level-pos -1)
  3767. pps-end-pos (point))
  3768. (/= (car state-2) 0)))
  3769. ;; Not at the right level.
  3770. (if (and (< (car state-2) 0)
  3771. ;; We stop above if we go out of a paren.
  3772. ;; Now check whether it precedes or is
  3773. ;; nested in the starting sexp.
  3774. (save-excursion
  3775. (setq state-2
  3776. (parse-partial-sexp
  3777. pps-end-pos paren-level-pos
  3778. nil nil state-2))
  3779. (< (car state-2) 0)))
  3780. ;; We've stopped short of the starting position
  3781. ;; so the hit was inside a nested list. Go up
  3782. ;; until we are at the right level.
  3783. (condition-case nil
  3784. (progn
  3785. (goto-char (scan-lists pos -1
  3786. (- (car state-2))))
  3787. (setq paren-level-pos (point))
  3788. (if (and limit (>= limit paren-level-pos))
  3789. (progn
  3790. (goto-char limit)
  3791. nil)
  3792. t))
  3793. (error
  3794. (goto-char (or limit (point-min)))
  3795. nil))
  3796. ;; The hit was outside the list at the start
  3797. ;; position. Go to the start of the list and exit.
  3798. (goto-char (1+ (elt state-2 1)))
  3799. nil))
  3800. ((c-beginning-of-macro limit)
  3801. ;; Inside a macro.
  3802. (if (< (point)
  3803. (or start-macro-beg
  3804. (setq start-macro-beg
  3805. (save-excursion
  3806. (goto-char start)
  3807. (c-beginning-of-macro limit)
  3808. (point)))))
  3809. t
  3810. ;; It's inside the same macro we started in so it's
  3811. ;; a relevant match.
  3812. (goto-char pos)
  3813. nil))))))
  3814. (> (point)
  3815. (progn
  3816. ;; Skip syntactic ws afterwards so that we don't stop at the
  3817. ;; end of a comment if `skip-chars' is something like "^/".
  3818. (c-backward-syntactic-ws)
  3819. (point)))))
  3820. ;; We might want to extend this with more useful return values in
  3821. ;; the future.
  3822. (/= (point) start)))
  3823. ;; The following is an alternative implementation of
  3824. ;; `c-syntactic-skip-backward' that uses backward movement to keep
  3825. ;; track of the syntactic context. It turned out to be generally
  3826. ;; slower than the one above which uses forward checks from earlier
  3827. ;; safe positions.
  3828. ;;
  3829. ;;(defconst c-ssb-stop-re
  3830. ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
  3831. ;; ;; stop at to avoid going into comments and literals.
  3832. ;; (concat
  3833. ;; ;; Match comment end syntax and string literal syntax. Also match
  3834. ;; ;; '/' for block comment endings (not covered by comment end
  3835. ;; ;; syntax).
  3836. ;; "\\s>\\|/\\|\\s\""
  3837. ;; (if (memq 'gen-string-delim c-emacs-features)
  3838. ;; "\\|\\s|"
  3839. ;; "")
  3840. ;; (if (memq 'gen-comment-delim c-emacs-features)
  3841. ;; "\\|\\s!"
  3842. ;; "")))
  3843. ;;
  3844. ;;(defconst c-ssb-stop-paren-re
  3845. ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
  3846. ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
  3847. ;;
  3848. ;;(defconst c-ssb-sexp-end-re
  3849. ;; ;; Regexp matching the ending syntax of a complex sexp.
  3850. ;; (concat c-string-limit-regexp "\\|\\s)"))
  3851. ;;
  3852. ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
  3853. ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
  3854. ;;i.e. don't stop at positions inside syntactic whitespace or string
  3855. ;;literals. Preprocessor directives are also ignored. However, if the
  3856. ;;point is within a comment, string literal or preprocessor directory to
  3857. ;;begin with, its contents is treated as syntactically relevant chars.
  3858. ;;If LIMIT is given, it limits the backward search and the point will be
  3859. ;;left there if no earlier position is found.
  3860. ;;
  3861. ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
  3862. ;;sexps, and the search will also not go outside the current paren sexp.
  3863. ;;However, if LIMIT or the buffer limit is reached inside a nested paren
  3864. ;;then the point will be left at the limit.
  3865. ;;
  3866. ;;Non-nil is returned if the point moved, nil otherwise.
  3867. ;;
  3868. ;;Note that this function might do hidden buffer changes. See the
  3869. ;;comment at the start of cc-engine.el for more info."
  3870. ;;
  3871. ;; (save-restriction
  3872. ;; (when limit
  3873. ;; (narrow-to-region limit (point-max)))
  3874. ;;
  3875. ;; (let ((start (point)))
  3876. ;; (catch 'done
  3877. ;; (while (let ((last-pos (point))
  3878. ;; (stop-pos (progn
  3879. ;; (skip-chars-backward skip-chars)
  3880. ;; (point))))
  3881. ;;
  3882. ;; ;; Skip back over the same region as
  3883. ;; ;; `skip-chars-backward' above, but keep to
  3884. ;; ;; syntactically relevant positions.
  3885. ;; (goto-char last-pos)
  3886. ;; (while (and
  3887. ;; ;; `re-search-backward' with a single char regexp
  3888. ;; ;; should be fast.
  3889. ;; (re-search-backward
  3890. ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
  3891. ;; stop-pos 'move)
  3892. ;;
  3893. ;; (progn
  3894. ;; (cond
  3895. ;; ((looking-at "\\s(")
  3896. ;; ;; `paren-level' is set and we've found the
  3897. ;; ;; start of the containing paren.
  3898. ;; (forward-char)
  3899. ;; (throw 'done t))
  3900. ;;
  3901. ;; ((looking-at c-ssb-sexp-end-re)
  3902. ;; ;; We're at the end of a string literal or paren
  3903. ;; ;; sexp (if `paren-level' is set).
  3904. ;; (forward-char)
  3905. ;; (condition-case nil
  3906. ;; (c-backward-sexp)
  3907. ;; (error
  3908. ;; (goto-char limit)
  3909. ;; (throw 'done t))))
  3910. ;;
  3911. ;; (t
  3912. ;; (forward-char)
  3913. ;; ;; At the end of some syntactic ws or possibly
  3914. ;; ;; after a plain '/' operator.
  3915. ;; (let ((pos (point)))
  3916. ;; (c-backward-syntactic-ws)
  3917. ;; (if (= pos (point))
  3918. ;; ;; Was a plain '/' operator. Go past it.
  3919. ;; (backward-char)))))
  3920. ;;
  3921. ;; (> (point) stop-pos))))
  3922. ;;
  3923. ;; ;; Now the point is either at `stop-pos' or at some
  3924. ;; ;; position further back if `stop-pos' was at a
  3925. ;; ;; syntactically irrelevant place.
  3926. ;;
  3927. ;; ;; Skip additional syntactic ws so that we don't stop
  3928. ;; ;; at the end of a comment if `skip-chars' is
  3929. ;; ;; something like "^/".
  3930. ;; (c-backward-syntactic-ws)
  3931. ;;
  3932. ;; (< (point) stop-pos))))
  3933. ;;
  3934. ;; ;; We might want to extend this with more useful return values
  3935. ;; ;; in the future.
  3936. ;; (/= (point) start))))
  3937. ;; Tools for handling comments and string literals.
  3938. (defun c-in-literal (&optional lim detect-cpp)
  3939. "Return the type of literal point is in, if any.
  3940. The return value is `c' if in a C-style comment, `c++' if in a C++
  3941. style comment, `string' if in a string literal, `pound' if DETECT-CPP
  3942. is non-nil and in a preprocessor line, or nil if somewhere else.
  3943. Optional LIM is used as the backward limit of the search. If omitted,
  3944. or nil, `c-beginning-of-defun' is used.
  3945. The last point calculated is cached if the cache is enabled, i.e. if
  3946. `c-in-literal-cache' is bound to a two element vector.
  3947. Note that this function might do hidden buffer changes. See the
  3948. comment at the start of cc-engine.el for more info."
  3949. (save-restriction
  3950. (widen)
  3951. (let* ((safe-place (c-state-semi-safe-place (point)))
  3952. (lit (c-state-pp-to-literal safe-place (point))))
  3953. (or (cadr lit)
  3954. (and detect-cpp
  3955. (save-excursion (c-beginning-of-macro))
  3956. 'pound)))))
  3957. (defun c-literal-limits (&optional lim near not-in-delimiter)
  3958. "Return a cons of the beginning and end positions of the comment or
  3959. string surrounding point (including both delimiters), or nil if point
  3960. isn't in one. If LIM is non-nil, it's used as the \"safe\" position
  3961. to start parsing from. If NEAR is non-nil, then the limits of any
  3962. literal next to point is returned. \"Next to\" means there's only
  3963. spaces and tabs between point and the literal. The search for such a
  3964. literal is done first in forward direction. If NOT-IN-DELIMITER is
  3965. non-nil, the case when point is inside a starting delimiter won't be
  3966. recognized. This only has effect for comments which have starting
  3967. delimiters with more than one character.
  3968. Note that this function might do hidden buffer changes. See the
  3969. comment at the start of cc-engine.el for more info."
  3970. (save-excursion
  3971. (let* ((pos (point))
  3972. (lim (or lim (c-state-semi-safe-place pos)))
  3973. (pp-to-lit (save-restriction
  3974. (widen)
  3975. (c-state-pp-to-literal lim pos)))
  3976. (state (car pp-to-lit))
  3977. (lit-limits (car (cddr pp-to-lit))))
  3978. (cond
  3979. (lit-limits)
  3980. ((and (not not-in-delimiter)
  3981. (not (elt state 5))
  3982. (eq (char-before) ?/)
  3983. (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
  3984. ;; We're standing in a comment starter.
  3985. (backward-char 1)
  3986. (cons (point) (progn (c-forward-single-comment) (point))))
  3987. (near
  3988. (goto-char pos)
  3989. ;; Search forward for a literal.
  3990. (skip-chars-forward " \t")
  3991. (cond
  3992. ((looking-at c-string-limit-regexp) ; String.
  3993. (cons (point) (or (c-safe (c-forward-sexp 1) (point))
  3994. (point-max))))
  3995. ((looking-at c-comment-start-regexp) ; Line or block comment.
  3996. (cons (point) (progn (c-forward-single-comment) (point))))
  3997. (t
  3998. ;; Search backward.
  3999. (skip-chars-backward " \t")
  4000. (let ((end (point)) beg)
  4001. (cond
  4002. ((save-excursion
  4003. (< (skip-syntax-backward c-string-syntax) 0)) ; String.
  4004. (setq beg (c-safe (c-backward-sexp 1) (point))))
  4005. ((and (c-safe (forward-char -2) t)
  4006. (looking-at "*/"))
  4007. ;; Block comment. Due to the nature of line
  4008. ;; comments, they will always be covered by the
  4009. ;; normal case above.
  4010. (goto-char end)
  4011. (c-backward-single-comment)
  4012. ;; If LIM is bogus, beg will be bogus.
  4013. (setq beg (point))))
  4014. (if beg (cons beg end))))))
  4015. ))))
  4016. ;; In case external callers use this; it did have a docstring.
  4017. (defalias 'c-literal-limits-fast 'c-literal-limits)
  4018. (defun c-collect-line-comments (range)
  4019. "If the argument is a cons of two buffer positions (such as returned by
  4020. `c-literal-limits'), and that range contains a C++ style line comment,
  4021. then an extended range is returned that contains all adjacent line
  4022. comments (i.e. all comments that starts in the same column with no
  4023. empty lines or non-whitespace characters between them). Otherwise the
  4024. argument is returned.
  4025. Note that this function might do hidden buffer changes. See the
  4026. comment at the start of cc-engine.el for more info."
  4027. (save-excursion
  4028. (condition-case nil
  4029. (if (and (consp range) (progn
  4030. (goto-char (car range))
  4031. (looking-at c-line-comment-starter)))
  4032. (let ((col (current-column))
  4033. (beg (point))
  4034. (bopl (c-point 'bopl))
  4035. (end (cdr range)))
  4036. ;; Got to take care in the backward direction to handle
  4037. ;; comments which are preceded by code.
  4038. (while (and (c-backward-single-comment)
  4039. (>= (point) bopl)
  4040. (looking-at c-line-comment-starter)
  4041. (= col (current-column)))
  4042. (setq beg (point)
  4043. bopl (c-point 'bopl)))
  4044. (goto-char end)
  4045. (while (and (progn (skip-chars-forward " \t")
  4046. (looking-at c-line-comment-starter))
  4047. (= col (current-column))
  4048. (prog1 (zerop (forward-line 1))
  4049. (setq end (point)))))
  4050. (cons beg end))
  4051. range)
  4052. (error range))))
  4053. (defun c-literal-type (range)
  4054. "Convenience function that given the result of `c-literal-limits',
  4055. returns nil or the type of literal that the range surrounds, one
  4056. of the symbols 'c, 'c++ or 'string. It's much faster than using
  4057. `c-in-literal' and is intended to be used when you need both the
  4058. type of a literal and its limits.
  4059. Note that this function might do hidden buffer changes. See the
  4060. comment at the start of cc-engine.el for more info."
  4061. (if (consp range)
  4062. (save-excursion
  4063. (goto-char (car range))
  4064. (cond ((looking-at c-string-limit-regexp) 'string)
  4065. ((or (looking-at "//") ; c++ line comment
  4066. (and (looking-at "\\s<") ; comment starter
  4067. (looking-at "#"))) ; awk comment.
  4068. 'c++)
  4069. (t 'c))) ; Assuming the range is valid.
  4070. range))
  4071. (defsubst c-determine-limit-get-base (start try-size)
  4072. ;; Get a "safe place" approximately TRY-SIZE characters before START.
  4073. ;; This doesn't preserve point.
  4074. (let* ((pos (max (- start try-size) (point-min)))
  4075. (base (c-state-semi-safe-place pos))
  4076. (s (parse-partial-sexp base pos)))
  4077. (if (or (nth 4 s) (nth 3 s)) ; comment or string
  4078. (nth 8 s)
  4079. (point))))
  4080. (defun c-determine-limit (how-far-back &optional start try-size)
  4081. ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
  4082. ;; (default point). This is done by going back further in the buffer then
  4083. ;; searching forward for literals. The position found won't be in a
  4084. ;; literal. We start searching for the sought position TRY-SIZE (default
  4085. ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
  4086. ;; :-)
  4087. (save-excursion
  4088. (let* ((start (or start (point)))
  4089. (try-size (or try-size (* 2 how-far-back)))
  4090. (base (c-determine-limit-get-base start try-size))
  4091. (pos base)
  4092. (s (parse-partial-sexp pos pos)) ; null state.
  4093. stack elt size
  4094. (count 0))
  4095. (while (< pos start)
  4096. ;; Move forward one literal each time round this loop.
  4097. ;; Move forward to the start of a comment or string.
  4098. (setq s (parse-partial-sexp
  4099. pos
  4100. start
  4101. nil ; target-depth
  4102. nil ; stop-before
  4103. s ; state
  4104. 'syntax-table)) ; stop-comment
  4105. ;; Gather details of the non-literal-bit - starting pos and size.
  4106. (setq size (- (if (or (nth 4 s) (nth 3 s))
  4107. (nth 8 s)
  4108. (point))
  4109. pos))
  4110. (if (> size 0)
  4111. (setq stack (cons (cons pos size) stack)))
  4112. ;; Move forward to the end of the comment/string.
  4113. (if (or (nth 4 s) (nth 3 s))
  4114. (setq s (parse-partial-sexp
  4115. (point)
  4116. start
  4117. nil ; target-depth
  4118. nil ; stop-before
  4119. s ; state
  4120. 'syntax-table))) ; stop-comment
  4121. (setq pos (point)))
  4122. ;; Now try and find enough non-literal characters recorded on the stack.
  4123. ;; Go back one recorded literal each time round this loop.
  4124. (while (and (< count how-far-back)
  4125. stack)
  4126. (setq elt (car stack)
  4127. stack (cdr stack))
  4128. (setq count (+ count (cdr elt))))
  4129. ;; Have we found enough yet?
  4130. (cond
  4131. ((>= count how-far-back)
  4132. (+ (car elt) (- count how-far-back)))
  4133. ((eq base (point-min))
  4134. (point-min))
  4135. (t
  4136. (c-determine-limit (- how-far-back count) base try-size))))))
  4137. (defun c-determine-+ve-limit (how-far &optional start-pos)
  4138. ;; Return a buffer position about HOW-FAR non-literal characters forward
  4139. ;; from START-POS (default point), which must not be inside a literal.
  4140. (save-excursion
  4141. (let ((pos (or start-pos (point)))
  4142. (count how-far)
  4143. (s (parse-partial-sexp (point) (point)))) ; null state
  4144. (while (and (not (eobp))
  4145. (> count 0))
  4146. ;; Scan over counted characters.
  4147. (setq s (parse-partial-sexp
  4148. pos
  4149. (min (+ pos count) (point-max))
  4150. nil ; target-depth
  4151. nil ; stop-before
  4152. s ; state
  4153. 'syntax-table)) ; stop-comment
  4154. (setq count (- count (- (point) pos) 1)
  4155. pos (point))
  4156. ;; Scan over literal characters.
  4157. (if (nth 8 s)
  4158. (setq s (parse-partial-sexp
  4159. pos
  4160. (point-max)
  4161. nil ; target-depth
  4162. nil ; stop-before
  4163. s ; state
  4164. 'syntax-table) ; stop-comment
  4165. pos (point))))
  4166. (point))))
  4167. ;; `c-find-decl-spots' and accompanying stuff.
  4168. ;; Variables used in `c-find-decl-spots' to cache the search done for
  4169. ;; the first declaration in the last call. When that function starts,
  4170. ;; it needs to back up over syntactic whitespace to look at the last
  4171. ;; token before the region being searched. That can sometimes cause
  4172. ;; moves back and forth over a quite large region of comments and
  4173. ;; macros, which would be repeated for each changed character when
  4174. ;; we're called during fontification, since font-lock refontifies the
  4175. ;; current line for each change. Thus it's worthwhile to cache the
  4176. ;; first match.
  4177. ;;
  4178. ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
  4179. ;; the syntactic whitespace less or equal to some start position.
  4180. ;; There's no cached value if it's nil.
  4181. ;;
  4182. ;; `c-find-decl-match-pos' is the match position if
  4183. ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
  4184. ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
  4185. (defvar c-find-decl-syntactic-pos nil)
  4186. (make-variable-buffer-local 'c-find-decl-syntactic-pos)
  4187. (defvar c-find-decl-match-pos nil)
  4188. (make-variable-buffer-local 'c-find-decl-match-pos)
  4189. (defsubst c-invalidate-find-decl-cache (change-min-pos)
  4190. (and c-find-decl-syntactic-pos
  4191. (< change-min-pos c-find-decl-syntactic-pos)
  4192. (setq c-find-decl-syntactic-pos nil)))
  4193. ; (defface c-debug-decl-spot-face
  4194. ; '((t (:background "Turquoise")))
  4195. ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
  4196. ; (defface c-debug-decl-sws-face
  4197. ; '((t (:background "Khaki")))
  4198. ; "Debug face to mark the syntactic whitespace between the declaration
  4199. ; spots and the preceding token end.")
  4200. (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
  4201. (when (facep 'c-debug-decl-spot-face)
  4202. `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
  4203. (c-debug-add-face (max match-pos (point-min)) decl-pos
  4204. 'c-debug-decl-sws-face)
  4205. (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
  4206. 'c-debug-decl-spot-face))))
  4207. (defmacro c-debug-remove-decl-spot-faces (beg end)
  4208. (when (facep 'c-debug-decl-spot-face)
  4209. `(c-save-buffer-state ()
  4210. (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
  4211. (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
  4212. (defmacro c-find-decl-prefix-search ()
  4213. ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
  4214. ;; but it contains lots of free variables that refer to things
  4215. ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
  4216. ;; if there is a match, otherwise at `cfd-limit'.
  4217. ;;
  4218. ;; This macro might do hidden buffer changes.
  4219. '(progn
  4220. ;; Find the next property match position if we haven't got one already.
  4221. (unless cfd-prop-match
  4222. (save-excursion
  4223. (while (progn
  4224. (goto-char (next-single-property-change
  4225. (point) 'c-type nil cfd-limit))
  4226. (and (< (point) cfd-limit)
  4227. (not (eq (c-get-char-property (1- (point)) 'c-type)
  4228. 'c-decl-end)))))
  4229. (setq cfd-prop-match (point))))
  4230. ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
  4231. ;; got one already.
  4232. (unless cfd-re-match
  4233. (if (> cfd-re-match-end (point))
  4234. (goto-char cfd-re-match-end))
  4235. (while (if (setq cfd-re-match-end
  4236. (re-search-forward c-decl-prefix-or-start-re
  4237. cfd-limit 'move))
  4238. ;; Match. Check if it's inside a comment or string literal.
  4239. (c-got-face-at
  4240. (if (setq cfd-re-match (match-end 1))
  4241. ;; Matched the end of a token preceding a decl spot.
  4242. (progn
  4243. (goto-char cfd-re-match)
  4244. (1- cfd-re-match))
  4245. ;; Matched a token that start a decl spot.
  4246. (goto-char (match-beginning 0))
  4247. (point))
  4248. c-literal-faces)
  4249. ;; No match. Finish up and exit the loop.
  4250. (setq cfd-re-match cfd-limit)
  4251. nil)
  4252. ;; Skip out of comments and string literals.
  4253. (while (progn
  4254. (goto-char (next-single-property-change
  4255. (point) 'face nil cfd-limit))
  4256. (and (< (point) cfd-limit)
  4257. (c-got-face-at (point) c-literal-faces)))))
  4258. ;; If we matched at the decl start, we have to back up over the
  4259. ;; preceding syntactic ws to set `cfd-match-pos' and to catch
  4260. ;; any decl spots in the syntactic ws.
  4261. (unless cfd-re-match
  4262. (c-backward-syntactic-ws)
  4263. (setq cfd-re-match (point))))
  4264. ;; Choose whichever match is closer to the start.
  4265. (if (< cfd-re-match cfd-prop-match)
  4266. (setq cfd-match-pos cfd-re-match
  4267. cfd-re-match nil)
  4268. (setq cfd-match-pos cfd-prop-match
  4269. cfd-prop-match nil))
  4270. (goto-char cfd-match-pos)
  4271. (when (< cfd-match-pos cfd-limit)
  4272. ;; Skip forward past comments only so we don't skip macros.
  4273. (c-forward-comments)
  4274. ;; Set the position to continue at. We can avoid going over
  4275. ;; the comments skipped above a second time, but it's possible
  4276. ;; that the comment skipping has taken us past `cfd-prop-match'
  4277. ;; since the property might be used inside comments.
  4278. (setq cfd-continue-pos (if cfd-prop-match
  4279. (min cfd-prop-match (point))
  4280. (point))))))
  4281. (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
  4282. ;; Call CFD-FUN for each possible spot for a declaration, cast or
  4283. ;; label from the point to CFD-LIMIT.
  4284. ;;
  4285. ;; CFD-FUN is called with point at the start of the spot. It's passed two
  4286. ;; arguments: The first is the end position of the token preceding the spot,
  4287. ;; or 0 for the implicit match at bob. The second is a flag that is t when
  4288. ;; the match is inside a macro. Point should be moved forward by at least
  4289. ;; one token.
  4290. ;;
  4291. ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
  4292. ;; it should return non-nil to ensure that the next search will find them.
  4293. ;;
  4294. ;; Such a spot is:
  4295. ;; o The first token after bob.
  4296. ;; o The first token after the end of submatch 1 in
  4297. ;; `c-decl-prefix-or-start-re' when that submatch matches.
  4298. ;; o The start of each `c-decl-prefix-or-start-re' match when
  4299. ;; submatch 1 doesn't match.
  4300. ;; o The first token after the end of each occurrence of the
  4301. ;; `c-type' text property with the value `c-decl-end', provided
  4302. ;; `c-type-decl-end-used' is set.
  4303. ;;
  4304. ;; Only a spot that match CFD-DECL-RE and whose face is in the
  4305. ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
  4306. ;; check is disabled if CFD-FACE-CHECKLIST is nil.
  4307. ;;
  4308. ;; If the match is inside a macro then the buffer is narrowed to the
  4309. ;; end of it, so that CFD-FUN can investigate the following tokens
  4310. ;; without matching something that begins inside a macro and ends
  4311. ;; outside it. It's to avoid this work that the CFD-DECL-RE and
  4312. ;; CFD-FACE-CHECKLIST checks exist.
  4313. ;;
  4314. ;; The spots are visited approximately in order from top to bottom.
  4315. ;; It's however the positions where `c-decl-prefix-or-start-re'
  4316. ;; matches and where `c-decl-end' properties are found that are in
  4317. ;; order. Since the spots often are at the following token, they
  4318. ;; might be visited out of order insofar as more spots are reported
  4319. ;; later on within the syntactic whitespace between the match
  4320. ;; positions and their spots.
  4321. ;;
  4322. ;; It's assumed that comments and strings are fontified in the
  4323. ;; searched range.
  4324. ;;
  4325. ;; This is mainly used in fontification, and so has an elaborate
  4326. ;; cache to handle repeated calls from the same start position; see
  4327. ;; the variables above.
  4328. ;;
  4329. ;; All variables in this function begin with `cfd-' to avoid name
  4330. ;; collision with the (dynamically bound) variables used in CFD-FUN.
  4331. ;;
  4332. ;; This function might do hidden buffer changes.
  4333. (let ((cfd-start-pos (point))
  4334. (cfd-buffer-end (point-max))
  4335. ;; The end of the token preceding the decl spot last found
  4336. ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
  4337. ;; no match.
  4338. cfd-re-match
  4339. ;; The end position of the last `c-decl-prefix-or-start-re'
  4340. ;; match. If this is greater than `cfd-continue-pos', the
  4341. ;; next regexp search is started here instead.
  4342. (cfd-re-match-end (point-min))
  4343. ;; The end of the last `c-decl-end' found by
  4344. ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
  4345. ;; match. If searching for the property isn't needed then we
  4346. ;; disable it by setting it to `cfd-limit' directly.
  4347. (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
  4348. ;; The end of the token preceding the decl spot last found by
  4349. ;; `c-find-decl-prefix-search'. 0 for the implicit match at
  4350. ;; bob. `cfd-limit' if there's no match. In other words,
  4351. ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
  4352. (cfd-match-pos cfd-limit)
  4353. ;; The position to continue searching at.
  4354. cfd-continue-pos
  4355. ;; The position of the last "real" token we've stopped at.
  4356. ;; This can be greater than `cfd-continue-pos' when we get
  4357. ;; hits inside macros or at `c-decl-end' positions inside
  4358. ;; comments.
  4359. (cfd-token-pos 0)
  4360. ;; The end position of the last entered macro.
  4361. (cfd-macro-end 0))
  4362. ;; Initialize by finding a syntactically relevant start position
  4363. ;; before the point, and do the first `c-decl-prefix-or-start-re'
  4364. ;; search unless we're at bob.
  4365. (let (start-in-literal start-in-macro syntactic-pos)
  4366. ;; Must back up a bit since we look for the end of the previous
  4367. ;; statement or declaration, which is earlier than the first
  4368. ;; returned match.
  4369. (cond
  4370. ;; First we need to move to a syntactically relevant position.
  4371. ;; Begin by backing out of comment or string literals.
  4372. ((and
  4373. (when (c-got-face-at (point) c-literal-faces)
  4374. ;; Try to use the faces to back up to the start of the
  4375. ;; literal. FIXME: What if the point is on a declaration
  4376. ;; inside a comment?
  4377. (while (and (not (bobp))
  4378. (c-got-face-at (1- (point)) c-literal-faces))
  4379. (goto-char (previous-single-property-change
  4380. (point) 'face nil (point-min))))
  4381. ;; XEmacs doesn't fontify the quotes surrounding string
  4382. ;; literals.
  4383. (and (featurep 'xemacs)
  4384. (eq (get-text-property (point) 'face)
  4385. 'font-lock-string-face)
  4386. (not (bobp))
  4387. (progn (backward-char)
  4388. (not (looking-at c-string-limit-regexp)))
  4389. (forward-char))
  4390. ;; Don't trust the literal to contain only literal faces
  4391. ;; (the font lock package might not have fontified the
  4392. ;; start of it at all, for instance) so check that we have
  4393. ;; arrived at something that looks like a start or else
  4394. ;; resort to `c-literal-limits'.
  4395. (unless (looking-at c-literal-start-regexp)
  4396. (let ((range (c-literal-limits)))
  4397. (if range (goto-char (car range)))))
  4398. (setq start-in-literal (point)))
  4399. ;; The start is in a literal. If the limit is in the same
  4400. ;; one we don't have to find a syntactic position etc. We
  4401. ;; only check that if the limit is at or before bonl to save
  4402. ;; time; it covers the by far most common case when font-lock
  4403. ;; refontifies the current line only.
  4404. (<= cfd-limit (c-point 'bonl cfd-start-pos))
  4405. (save-excursion
  4406. (goto-char cfd-start-pos)
  4407. (while (progn
  4408. (goto-char (next-single-property-change
  4409. (point) 'face nil cfd-limit))
  4410. (and (< (point) cfd-limit)
  4411. (c-got-face-at (point) c-literal-faces))))
  4412. (= (point) cfd-limit)))
  4413. ;; Completely inside a literal. Set up variables to trig the
  4414. ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
  4415. ;; find a suitable start position.
  4416. (setq cfd-continue-pos start-in-literal))
  4417. ;; Check if the region might be completely inside a macro, to
  4418. ;; optimize that like the completely-inside-literal above.
  4419. ((save-excursion
  4420. (and (= (forward-line 1) 0)
  4421. (bolp) ; forward-line has funny behavior at eob.
  4422. (>= (point) cfd-limit)
  4423. (progn (backward-char)
  4424. (eq (char-before) ?\\))))
  4425. ;; (Maybe) completely inside a macro. Only need to trig the
  4426. ;; (< cfd-continue-pos cfd-start-pos) case below to make it
  4427. ;; set things up.
  4428. (setq cfd-continue-pos (1- cfd-start-pos)
  4429. start-in-macro t))
  4430. (t
  4431. ;; Back out of any macro so we don't miss any declaration
  4432. ;; that could follow after it.
  4433. (when (c-beginning-of-macro)
  4434. (setq start-in-macro t))
  4435. ;; Now we're at a proper syntactically relevant position so we
  4436. ;; can use the cache. But first clear it if it applied
  4437. ;; further down.
  4438. (c-invalidate-find-decl-cache cfd-start-pos)
  4439. (setq syntactic-pos (point))
  4440. (unless (eq syntactic-pos c-find-decl-syntactic-pos)
  4441. ;; Don't have to do this if the cache is relevant here,
  4442. ;; typically if the same line is refontified again. If
  4443. ;; we're just some syntactic whitespace further down we can
  4444. ;; still use the cache to limit the skipping.
  4445. (c-backward-syntactic-ws c-find-decl-syntactic-pos))
  4446. ;; If we hit `c-find-decl-syntactic-pos' and
  4447. ;; `c-find-decl-match-pos' is set then we install the cached
  4448. ;; values. If we hit `c-find-decl-syntactic-pos' and
  4449. ;; `c-find-decl-match-pos' is nil then we know there's no decl
  4450. ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
  4451. ;; and so we can continue the search from this point. If we
  4452. ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
  4453. ;; the right spot to begin searching anyway.
  4454. (if (and (eq (point) c-find-decl-syntactic-pos)
  4455. c-find-decl-match-pos)
  4456. (setq cfd-match-pos c-find-decl-match-pos
  4457. cfd-continue-pos syntactic-pos)
  4458. (setq c-find-decl-syntactic-pos syntactic-pos)
  4459. (when (if (bobp)
  4460. ;; Always consider bob a match to get the first
  4461. ;; declaration in the file. Do this separately instead of
  4462. ;; letting `c-decl-prefix-or-start-re' match bob, so that
  4463. ;; regexp always can consume at least one character to
  4464. ;; ensure that we won't get stuck in an infinite loop.
  4465. (setq cfd-re-match 0)
  4466. (backward-char)
  4467. (c-beginning-of-current-token)
  4468. (< (point) cfd-limit))
  4469. ;; Do an initial search now. In the bob case above it's
  4470. ;; only done to search for a `c-decl-end' spot.
  4471. (c-find-decl-prefix-search))
  4472. (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
  4473. cfd-match-pos)))))
  4474. ;; Advance `cfd-continue-pos' if it's before the start position.
  4475. ;; The closest continue position that might have effect at or
  4476. ;; after the start depends on what we started in. This also
  4477. ;; finds a suitable start position in the special cases when the
  4478. ;; region is completely within a literal or macro.
  4479. (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
  4480. (cond
  4481. (start-in-macro
  4482. ;; If we're in a macro then it's the closest preceding token
  4483. ;; in the macro. Check this before `start-in-literal',
  4484. ;; since if we're inside a literal in a macro, the preceding
  4485. ;; token is earlier than any `c-decl-end' spot inside the
  4486. ;; literal (comment).
  4487. (goto-char (or start-in-literal cfd-start-pos))
  4488. ;; The only syntactic ws in macros are comments.
  4489. (c-backward-comments)
  4490. (backward-char)
  4491. (c-beginning-of-current-token))
  4492. (start-in-literal
  4493. ;; If we're in a comment it can only be the closest
  4494. ;; preceding `c-decl-end' position within that comment, if
  4495. ;; any. Go back to the beginning of such a property so that
  4496. ;; `c-find-decl-prefix-search' will find the end of it.
  4497. ;; (Can't stop at the end and install it directly on
  4498. ;; `cfd-prop-match' since that variable might be cleared
  4499. ;; after `cfd-fun' below.)
  4500. ;;
  4501. ;; Note that if the literal is a string then the property
  4502. ;; search will simply skip to the beginning of it right
  4503. ;; away.
  4504. (if (not c-type-decl-end-used)
  4505. (goto-char start-in-literal)
  4506. (goto-char cfd-start-pos)
  4507. (while (progn
  4508. (goto-char (previous-single-property-change
  4509. (point) 'c-type nil start-in-literal))
  4510. (and (> (point) start-in-literal)
  4511. (not (eq (c-get-char-property (point) 'c-type)
  4512. 'c-decl-end))))))
  4513. (when (= (point) start-in-literal)
  4514. ;; Didn't find any property inside the comment, so we can
  4515. ;; skip it entirely. (This won't skip past a string, but
  4516. ;; that'll be handled quickly by the next
  4517. ;; `c-find-decl-prefix-search' anyway.)
  4518. (c-forward-single-comment)
  4519. (if (> (point) cfd-limit)
  4520. (goto-char cfd-limit))))
  4521. (t
  4522. ;; If we started in normal code, the only match that might
  4523. ;; apply before the start is what we already got in
  4524. ;; `cfd-match-pos' so we can continue at the start position.
  4525. ;; (Note that we don't get here if the first match is below
  4526. ;; it.)
  4527. (goto-char cfd-start-pos)))
  4528. ;; Delete found matches if they are before our new continue
  4529. ;; position, so that `c-find-decl-prefix-search' won't back up
  4530. ;; to them later on.
  4531. (setq cfd-continue-pos (point))
  4532. (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
  4533. (setq cfd-re-match nil))
  4534. (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
  4535. (setq cfd-prop-match nil)))
  4536. (if syntactic-pos
  4537. ;; This is the normal case and we got a proper syntactic
  4538. ;; position. If there's a match then it's always outside
  4539. ;; macros and comments, so advance to the next token and set
  4540. ;; `cfd-token-pos'. The loop below will later go back using
  4541. ;; `cfd-continue-pos' to fix declarations inside the
  4542. ;; syntactic ws.
  4543. (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
  4544. (goto-char syntactic-pos)
  4545. (c-forward-syntactic-ws)
  4546. (and cfd-continue-pos
  4547. (< cfd-continue-pos (point))
  4548. (setq cfd-token-pos (point))))
  4549. ;; Have one of the special cases when the region is completely
  4550. ;; within a literal or macro. `cfd-continue-pos' is set to a
  4551. ;; good start position for the search, so do it.
  4552. (c-find-decl-prefix-search)))
  4553. ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
  4554. (while (progn
  4555. (while (and
  4556. (< cfd-match-pos cfd-limit)
  4557. (or
  4558. ;; Kludge to filter out matches on the "<" that
  4559. ;; aren't open parens, for the sake of languages
  4560. ;; that got `c-recognize-<>-arglists' set.
  4561. (and (eq (char-before cfd-match-pos) ?<)
  4562. (not (c-get-char-property (1- cfd-match-pos)
  4563. 'syntax-table)))
  4564. ;; If `cfd-continue-pos' is less or equal to
  4565. ;; `cfd-token-pos', we've got a hit inside a macro
  4566. ;; that's in the syntactic whitespace before the last
  4567. ;; "real" declaration we've checked. If they're equal
  4568. ;; we've arrived at the declaration a second time, so
  4569. ;; there's nothing to do.
  4570. (= cfd-continue-pos cfd-token-pos)
  4571. (progn
  4572. ;; If `cfd-continue-pos' is less than `cfd-token-pos'
  4573. ;; we're still searching for declarations embedded in
  4574. ;; the syntactic whitespace. In that case we need
  4575. ;; only to skip comments and not macros, since they
  4576. ;; can't be nested, and that's already been done in
  4577. ;; `c-find-decl-prefix-search'.
  4578. (when (> cfd-continue-pos cfd-token-pos)
  4579. (c-forward-syntactic-ws)
  4580. (setq cfd-token-pos (point)))
  4581. ;; Continue if the following token fails the
  4582. ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
  4583. (when (or (>= (point) cfd-limit)
  4584. (not (looking-at cfd-decl-re))
  4585. (and cfd-face-checklist
  4586. (not (c-got-face-at
  4587. (point) cfd-face-checklist))))
  4588. (goto-char cfd-continue-pos)
  4589. t)))
  4590. (< (point) cfd-limit))
  4591. (c-find-decl-prefix-search))
  4592. (< (point) cfd-limit))
  4593. (when (and
  4594. (>= (point) cfd-start-pos)
  4595. (progn
  4596. ;; Narrow to the end of the macro if we got a hit inside
  4597. ;; one, to avoid recognizing things that start inside the
  4598. ;; macro and end outside it.
  4599. (when (> cfd-match-pos cfd-macro-end)
  4600. ;; Not in the same macro as in the previous round.
  4601. (save-excursion
  4602. (goto-char cfd-match-pos)
  4603. (setq cfd-macro-end
  4604. (if (save-excursion (and (c-beginning-of-macro)
  4605. (< (point) cfd-match-pos)))
  4606. (progn (c-end-of-macro)
  4607. (point))
  4608. 0))))
  4609. (if (zerop cfd-macro-end)
  4610. t
  4611. (if (> cfd-macro-end (point))
  4612. (progn (narrow-to-region (point-min) cfd-macro-end)
  4613. t)
  4614. ;; The matched token was the last thing in the macro,
  4615. ;; so the whole match is bogus.
  4616. (setq cfd-macro-end 0)
  4617. nil))))
  4618. (c-debug-put-decl-spot-faces cfd-match-pos (point))
  4619. (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
  4620. (setq cfd-prop-match nil))
  4621. (when (/= cfd-macro-end 0)
  4622. ;; Restore limits if we did macro narrowing above.
  4623. (narrow-to-region (point-min) cfd-buffer-end)))
  4624. (goto-char cfd-continue-pos)
  4625. (if (= cfd-continue-pos cfd-limit)
  4626. (setq cfd-match-pos cfd-limit)
  4627. (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
  4628. ; cfd-match-pos, etc.
  4629. ;; A cache for found types.
  4630. ;; Buffer local variable that contains an obarray with the types we've
  4631. ;; found. If a declaration is recognized somewhere we record the
  4632. ;; fully qualified identifier in it to recognize it as a type
  4633. ;; elsewhere in the file too. This is not accurate since we do not
  4634. ;; bother with the scoping rules of the languages, but in practice the
  4635. ;; same name is seldom used as both a type and something else in a
  4636. ;; file, and we only use this as a last resort in ambiguous cases (see
  4637. ;; `c-forward-decl-or-cast-1').
  4638. ;;
  4639. ;; Not every type need be in this cache. However, things which have
  4640. ;; ceased to be types must be removed from it.
  4641. ;;
  4642. ;; Template types in C++ are added here too but with the template
  4643. ;; arglist replaced with "<>" in references or "<" for the one in the
  4644. ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
  4645. ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
  4646. ;; template specs can be fairly sized programs in themselves) and
  4647. ;; improves the hit ratio (it's a type regardless of the template
  4648. ;; args; it's just not the same type, but we're only interested in
  4649. ;; recognizing types, not telling distinct types apart). Note that
  4650. ;; template types in references are added here too; from the example
  4651. ;; above there will also be an entry "Foo<".
  4652. (defvar c-found-types nil)
  4653. (make-variable-buffer-local 'c-found-types)
  4654. (defsubst c-clear-found-types ()
  4655. ;; Clears `c-found-types'.
  4656. (setq c-found-types (make-vector 53 0)))
  4657. (defun c-add-type (from to)
  4658. ;; Add the given region as a type in `c-found-types'. If the region
  4659. ;; doesn't match an existing type but there is a type which is equal
  4660. ;; to the given one except that the last character is missing, then
  4661. ;; the shorter type is removed. That's done to avoid adding all
  4662. ;; prefixes of a type as it's being entered and font locked. This
  4663. ;; doesn't cover cases like when characters are removed from a type
  4664. ;; or added in the middle. We'd need the position of point when the
  4665. ;; font locking is invoked to solve this well.
  4666. ;;
  4667. ;; This function might do hidden buffer changes.
  4668. (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
  4669. (unless (intern-soft type c-found-types)
  4670. (unintern (substring type 0 -1) c-found-types)
  4671. (intern type c-found-types))))
  4672. (defun c-unfind-type (name)
  4673. ;; Remove the "NAME" from c-found-types, if present.
  4674. (unintern name c-found-types))
  4675. (defsubst c-check-type (from to)
  4676. ;; Return non-nil if the given region contains a type in
  4677. ;; `c-found-types'.
  4678. ;;
  4679. ;; This function might do hidden buffer changes.
  4680. (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
  4681. c-found-types))
  4682. (defun c-list-found-types ()
  4683. ;; Return all the types in `c-found-types' as a sorted list of
  4684. ;; strings.
  4685. (let (type-list)
  4686. (mapatoms (lambda (type)
  4687. (setq type-list (cons (symbol-name type)
  4688. type-list)))
  4689. c-found-types)
  4690. (sort type-list 'string-lessp)))
  4691. ;; Shut up the byte compiler.
  4692. (defvar c-maybe-stale-found-type)
  4693. (defun c-trim-found-types (beg end old-len)
  4694. ;; An after change function which, in conjunction with the info in
  4695. ;; c-maybe-stale-found-type (set in c-before-change), removes a type
  4696. ;; from `c-found-types', should this type have become stale. For
  4697. ;; example, this happens to "foo" when "foo \n bar();" becomes
  4698. ;; "foo(); \n bar();". Such stale types, if not removed, foul up
  4699. ;; the fontification.
  4700. ;;
  4701. ;; Have we, perhaps, added non-ws characters to the front/back of a found
  4702. ;; type?
  4703. (when (> end beg)
  4704. (save-excursion
  4705. (when (< end (point-max))
  4706. (goto-char end)
  4707. (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
  4708. (progn (goto-char end)
  4709. (c-end-of-current-token)))
  4710. (c-unfind-type (buffer-substring-no-properties
  4711. end (point)))))
  4712. (when (> beg (point-min))
  4713. (goto-char beg)
  4714. (if (and (c-end-of-current-token) ; only moves when we started in the middle
  4715. (progn (goto-char beg)
  4716. (c-beginning-of-current-token)))
  4717. (c-unfind-type (buffer-substring-no-properties
  4718. (point) beg))))))
  4719. (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
  4720. (cond
  4721. ;; Changing the amount of (already existing) whitespace - don't do anything.
  4722. ((and (c-partial-ws-p beg end)
  4723. (or (= beg end) ; removal of WS
  4724. (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
  4725. ;; The syntactic relationship which defined a "found type" has been
  4726. ;; destroyed.
  4727. ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
  4728. (c-unfind-type (cadr c-maybe-stale-found-type)))
  4729. ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
  4730. )))
  4731. ;; Setting and removing syntax properties on < and > in languages (C++
  4732. ;; and Java) where they can be template/generic delimiters as well as
  4733. ;; their normal meaning of "less/greater than".
  4734. ;; Normally, < and > have syntax 'punctuation'. When they are found to
  4735. ;; be delimiters, they are marked as such with the category properties
  4736. ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
  4737. ;; STRATEGY:
  4738. ;;
  4739. ;; It is impossible to determine with certainty whether a <..> pair in
  4740. ;; C++ is two comparison operators or is template delimiters, unless
  4741. ;; one duplicates a lot of a C++ compiler. For example, the following
  4742. ;; code fragment:
  4743. ;;
  4744. ;; foo (a < b, c > d) ;
  4745. ;;
  4746. ;; could be a function call with two integer parameters (each a
  4747. ;; relational expression), or it could be a constructor for class foo
  4748. ;; taking one parameter d of templated type "a < b, c >". They are
  4749. ;; somewhat easier to distinguish in Java.
  4750. ;;
  4751. ;; The strategy now (2010-01) adopted is to mark and unmark < and
  4752. ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
  4753. ;; individually when their context so indicated. This gave rise to
  4754. ;; intractable problems when one of a matching pair was deleted, or
  4755. ;; pulled into a literal.]
  4756. ;;
  4757. ;; At each buffer change, the syntax-table properties are removed in a
  4758. ;; before-change function and reapplied, when needed, in an
  4759. ;; after-change function. It is far more important that the
  4760. ;; properties get removed when they they are spurious than that they
  4761. ;; be present when wanted.
  4762. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4763. (defun c-clear-<-pair-props (&optional pos)
  4764. ;; POS (default point) is at a < character. If it is marked with
  4765. ;; open paren syntax-table text property, remove the property,
  4766. ;; together with the close paren property on the matching > (if
  4767. ;; any).
  4768. (save-excursion
  4769. (if pos
  4770. (goto-char pos)
  4771. (setq pos (point)))
  4772. (when (equal (c-get-char-property (point) 'syntax-table)
  4773. c-<-as-paren-syntax)
  4774. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4775. (c-go-list-forward))
  4776. (when (equal (c-get-char-property (1- (point)) 'syntax-table)
  4777. c->-as-paren-syntax) ; should always be true.
  4778. (c-clear-char-property (1- (point)) 'category))
  4779. (c-clear-char-property pos 'category))))
  4780. (defun c-clear->-pair-props (&optional pos)
  4781. ;; POS (default point) is at a > character. If it is marked with
  4782. ;; close paren syntax-table property, remove the property, together
  4783. ;; with the open paren property on the matching < (if any).
  4784. (save-excursion
  4785. (if pos
  4786. (goto-char pos)
  4787. (setq pos (point)))
  4788. (when (equal (c-get-char-property (point) 'syntax-table)
  4789. c->-as-paren-syntax)
  4790. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4791. (c-go-up-list-backward))
  4792. (when (equal (c-get-char-property (point) 'syntax-table)
  4793. c-<-as-paren-syntax) ; should always be true.
  4794. (c-clear-char-property (point) 'category))
  4795. (c-clear-char-property pos 'category))))
  4796. (defun c-clear-<>-pair-props (&optional pos)
  4797. ;; POS (default point) is at a < or > character. If it has an
  4798. ;; open/close paren syntax-table property, remove this property both
  4799. ;; from the current character and its partner (which will also be
  4800. ;; thusly marked).
  4801. (cond
  4802. ((eq (char-after) ?\<)
  4803. (c-clear-<-pair-props pos))
  4804. ((eq (char-after) ?\>)
  4805. (c-clear->-pair-props pos))
  4806. (t (c-benign-error
  4807. "c-clear-<>-pair-props called from wrong position"))))
  4808. (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
  4809. ;; POS (default point) is at a < character. If it is both marked
  4810. ;; with open/close paren syntax-table property, and has a matching >
  4811. ;; (also marked) which is after LIM, remove the property both from
  4812. ;; the current > and its partner. Return t when this happens, nil
  4813. ;; when it doesn't.
  4814. (save-excursion
  4815. (if pos
  4816. (goto-char pos)
  4817. (setq pos (point)))
  4818. (when (equal (c-get-char-property (point) 'syntax-table)
  4819. c-<-as-paren-syntax)
  4820. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4821. (c-go-list-forward))
  4822. (when (and (>= (point) lim)
  4823. (equal (c-get-char-property (1- (point)) 'syntax-table)
  4824. c->-as-paren-syntax)) ; should always be true.
  4825. (c-unmark-<->-as-paren (1- (point)))
  4826. (c-unmark-<->-as-paren pos))
  4827. t)))
  4828. (defun c-clear->-pair-props-if-match-before (lim &optional pos)
  4829. ;; POS (default point) is at a > character. If it is both marked
  4830. ;; with open/close paren syntax-table property, and has a matching <
  4831. ;; (also marked) which is before LIM, remove the property both from
  4832. ;; the current < and its partner. Return t when this happens, nil
  4833. ;; when it doesn't.
  4834. (save-excursion
  4835. (if pos
  4836. (goto-char pos)
  4837. (setq pos (point)))
  4838. (when (equal (c-get-char-property (point) 'syntax-table)
  4839. c->-as-paren-syntax)
  4840. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4841. (c-go-up-list-backward))
  4842. (when (and (<= (point) lim)
  4843. (equal (c-get-char-property (point) 'syntax-table)
  4844. c-<-as-paren-syntax)) ; should always be true.
  4845. (c-unmark-<->-as-paren (point))
  4846. (c-unmark-<->-as-paren pos))
  4847. t)))
  4848. ;; Set by c-common-init in cc-mode.el.
  4849. (defvar c-new-BEG)
  4850. (defvar c-new-END)
  4851. (defun c-before-change-check-<>-operators (beg end)
  4852. ;; Unmark certain pairs of "< .... >" which are currently marked as
  4853. ;; template/generic delimiters. (This marking is via syntax-table
  4854. ;; text properties).
  4855. ;;
  4856. ;; These pairs are those which are in the current "statement" (i.e.,
  4857. ;; the region between the {, }, or ; before BEG and the one after
  4858. ;; END), and which enclose any part of the interval (BEG END).
  4859. ;;
  4860. ;; Note that in C++ (?and Java), template/generic parens cannot
  4861. ;; enclose a brace or semicolon, so we use these as bounds on the
  4862. ;; region we must work on.
  4863. ;;
  4864. ;; This function is called from before-change-functions (via
  4865. ;; c-get-state-before-change-functions). Thus the buffer is widened,
  4866. ;; and point is undefined, both at entry and exit.
  4867. ;;
  4868. ;; FIXME!!! This routine ignores the possibility of macros entirely.
  4869. ;; 2010-01-29.
  4870. (save-excursion
  4871. (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
  4872. (end-lit-limits (progn (goto-char end) (c-literal-limits)))
  4873. new-beg new-end need-new-beg need-new-end)
  4874. ;; Locate the barrier before the changed region
  4875. (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
  4876. (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
  4877. (setq new-beg (point))
  4878. ;; Remove the syntax-table properties from each pertinent <...> pair.
  4879. ;; Firsly, the ones with the < before beg and > after beg.
  4880. (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
  4881. (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
  4882. (setq need-new-beg t)))
  4883. ;; Locate the barrier after END.
  4884. (goto-char (if end-lit-limits (cdr end-lit-limits) end))
  4885. (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
  4886. (setq new-end (point))
  4887. ;; Remove syntax-table properties from the remaining pertinent <...>
  4888. ;; pairs, those with a > after end and < before end.
  4889. (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
  4890. (if (c-clear->-pair-props-if-match-before end)
  4891. (setq need-new-end t)))
  4892. ;; Extend the fontification region, if needed.
  4893. (when need-new-beg
  4894. (goto-char new-beg)
  4895. (c-forward-syntactic-ws)
  4896. (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
  4897. (when need-new-end
  4898. (and (> new-end c-new-END) (setq c-new-END new-end))))))
  4899. (defun c-after-change-check-<>-operators (beg end)
  4900. ;; This is called from `after-change-functions' when
  4901. ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
  4902. ;; chars with paren syntax become part of another operator like "<<"
  4903. ;; or ">=".
  4904. ;;
  4905. ;; This function might do hidden buffer changes.
  4906. (save-excursion
  4907. (goto-char beg)
  4908. (when (or (looking-at "[<>]")
  4909. (< (skip-chars-backward "<>") 0))
  4910. (goto-char beg)
  4911. (c-beginning-of-current-token)
  4912. (when (and (< (point) beg)
  4913. (looking-at c-<>-multichar-token-regexp)
  4914. (< beg (setq beg (match-end 0))))
  4915. (while (progn (skip-chars-forward "^<>" beg)
  4916. (< (point) beg))
  4917. (c-clear-<>-pair-props)
  4918. (forward-char))))
  4919. (when (< beg end)
  4920. (goto-char end)
  4921. (when (or (looking-at "[<>]")
  4922. (< (skip-chars-backward "<>") 0))
  4923. (goto-char end)
  4924. (c-beginning-of-current-token)
  4925. (when (and (< (point) end)
  4926. (looking-at c-<>-multichar-token-regexp)
  4927. (< end (setq end (match-end 0))))
  4928. (while (progn (skip-chars-forward "^<>" end)
  4929. (< (point) end))
  4930. (c-clear-<>-pair-props)
  4931. (forward-char)))))))
  4932. ;; Handling of small scale constructs like types and names.
  4933. ;; Dynamically bound variable that instructs `c-forward-type' to also
  4934. ;; treat possible types (i.e. those that it normally returns 'maybe or
  4935. ;; 'found for) as actual types (and always return 'found for them).
  4936. ;; This means that it records them in `c-record-type-identifiers' if
  4937. ;; that is set, and that it adds them to `c-found-types'.
  4938. (defvar c-promote-possible-types nil)
  4939. ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
  4940. ;; mark up successfully parsed arglists with paren syntax properties on
  4941. ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
  4942. ;; `c-type' property of each argument separating comma.
  4943. ;;
  4944. ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
  4945. ;; all arglists for side effects (i.e. recording types), otherwise it
  4946. ;; exploits any existing paren syntax properties to quickly jump to the
  4947. ;; end of already parsed arglists.
  4948. ;;
  4949. ;; Marking up the arglists is not the default since doing that correctly
  4950. ;; depends on a proper value for `c-restricted-<>-arglists'.
  4951. (defvar c-parse-and-markup-<>-arglists nil)
  4952. ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
  4953. ;; not accept arglists that contain binary operators.
  4954. ;;
  4955. ;; This is primarily used to handle C++ template arglists. C++
  4956. ;; disambiguates them by checking whether the preceding name is a
  4957. ;; template or not. We can't do that, so we assume it is a template
  4958. ;; if it can be parsed as one. That usually works well since
  4959. ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
  4960. ;; in almost all cases would be pointless.
  4961. ;;
  4962. ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
  4963. ;; should let the comma separate the function arguments instead. And
  4964. ;; in a context where the value of the expression is taken, e.g. in
  4965. ;; "if (a < b || c > d)", it's probably not a template.
  4966. (defvar c-restricted-<>-arglists nil)
  4967. ;; Dynamically bound variables that instructs
  4968. ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
  4969. ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
  4970. ;; `c-forward-label' to record the ranges of all the type and
  4971. ;; reference identifiers they encounter. They will build lists on
  4972. ;; these variables where each element is a cons of the buffer
  4973. ;; positions surrounding each identifier. This recording is only
  4974. ;; activated when `c-record-type-identifiers' is non-nil.
  4975. ;;
  4976. ;; All known types that can't be identifiers are recorded, and also
  4977. ;; other possible types if `c-promote-possible-types' is set.
  4978. ;; Recording is however disabled inside angle bracket arglists that
  4979. ;; are encountered inside names and other angle bracket arglists.
  4980. ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
  4981. ;; instead.
  4982. ;;
  4983. ;; Only the names in C++ template style references (e.g. "tmpl" in
  4984. ;; "tmpl<a,b>::foo") are recorded as references, other references
  4985. ;; aren't handled here.
  4986. ;;
  4987. ;; `c-forward-label' records the label identifier(s) on
  4988. ;; `c-record-ref-identifiers'.
  4989. (defvar c-record-type-identifiers nil)
  4990. (defvar c-record-ref-identifiers nil)
  4991. ;; This variable will receive a cons cell of the range of the last
  4992. ;; single identifier symbol stepped over by `c-forward-name' if it's
  4993. ;; successful. This is the range that should be put on one of the
  4994. ;; record lists above by the caller. It's assigned nil if there's no
  4995. ;; such symbol in the name.
  4996. (defvar c-last-identifier-range nil)
  4997. (defmacro c-record-type-id (range)
  4998. (if (eq (car-safe range) 'cons)
  4999. ;; Always true.
  5000. `(setq c-record-type-identifiers
  5001. (cons ,range c-record-type-identifiers))
  5002. `(let ((range ,range))
  5003. (if range
  5004. (setq c-record-type-identifiers
  5005. (cons range c-record-type-identifiers))))))
  5006. (defmacro c-record-ref-id (range)
  5007. (if (eq (car-safe range) 'cons)
  5008. ;; Always true.
  5009. `(setq c-record-ref-identifiers
  5010. (cons ,range c-record-ref-identifiers))
  5011. `(let ((range ,range))
  5012. (if range
  5013. (setq c-record-ref-identifiers
  5014. (cons range c-record-ref-identifiers))))))
  5015. ;; Dynamically bound variable that instructs `c-forward-type' to
  5016. ;; record the ranges of types that only are found. Behaves otherwise
  5017. ;; like `c-record-type-identifiers'.
  5018. (defvar c-record-found-types nil)
  5019. (defmacro c-forward-keyword-prefixed-id (type)
  5020. ;; Used internally in `c-forward-keyword-clause' to move forward
  5021. ;; over a type (if TYPE is 'type) or a name (otherwise) which
  5022. ;; possibly is prefixed by keywords and their associated clauses.
  5023. ;; Try with a type/name first to not trip up on those that begin
  5024. ;; with a keyword. Return t if a known or found type is moved
  5025. ;; over. The point is clobbered if nil is returned. If range
  5026. ;; recording is enabled, the identifier is recorded on as a type
  5027. ;; if TYPE is 'type or as a reference if TYPE is 'ref.
  5028. ;;
  5029. ;; This macro might do hidden buffer changes.
  5030. `(let (res)
  5031. (while (if (setq res ,(if (eq type 'type)
  5032. `(c-forward-type)
  5033. `(c-forward-name)))
  5034. nil
  5035. (and (looking-at c-keywords-regexp)
  5036. (c-forward-keyword-clause 1))))
  5037. (when (memq res '(t known found prefix))
  5038. ,(when (eq type 'ref)
  5039. `(when c-record-type-identifiers
  5040. (c-record-ref-id c-last-identifier-range)))
  5041. t)))
  5042. (defmacro c-forward-id-comma-list (type update-safe-pos)
  5043. ;; Used internally in `c-forward-keyword-clause' to move forward
  5044. ;; over a comma separated list of types or names using
  5045. ;; `c-forward-keyword-prefixed-id'.
  5046. ;;
  5047. ;; This macro might do hidden buffer changes.
  5048. `(while (and (progn
  5049. ,(when update-safe-pos
  5050. `(setq safe-pos (point)))
  5051. (eq (char-after) ?,))
  5052. (progn
  5053. (forward-char)
  5054. (c-forward-syntactic-ws)
  5055. (c-forward-keyword-prefixed-id ,type)))))
  5056. (defun c-forward-keyword-clause (match)
  5057. ;; Submatch MATCH in the current match data is assumed to surround a
  5058. ;; token. If it's a keyword, move over it and any immediately
  5059. ;; following clauses associated with it, stopping at the start of
  5060. ;; the next token. t is returned in that case, otherwise the point
  5061. ;; stays and nil is returned. The kind of clauses that are
  5062. ;; recognized are those specified by `c-type-list-kwds',
  5063. ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
  5064. ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
  5065. ;; and `c-<>-arglist-kwds'.
  5066. ;;
  5067. ;; This function records identifier ranges on
  5068. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5069. ;; `c-record-type-identifiers' is non-nil.
  5070. ;;
  5071. ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
  5072. ;; apply directly after the keyword, the type list is moved over
  5073. ;; only when there is no unaccounted token before it (i.e. a token
  5074. ;; that isn't moved over due to some other keyword list). The
  5075. ;; identifier ranges in the list are still recorded if that should
  5076. ;; be done, though.
  5077. ;;
  5078. ;; This function might do hidden buffer changes.
  5079. (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
  5080. ;; The call to `c-forward-<>-arglist' below is made after
  5081. ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
  5082. ;; are angle bracket arglists and `c-restricted-<>-arglists'
  5083. ;; should therefore be nil.
  5084. (c-parse-and-markup-<>-arglists t)
  5085. c-restricted-<>-arglists)
  5086. (when kwd-sym
  5087. (goto-char (match-end match))
  5088. (c-forward-syntactic-ws)
  5089. (setq safe-pos (point))
  5090. (cond
  5091. ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
  5092. (c-forward-keyword-prefixed-id type))
  5093. ;; There's a type directly after a keyword in `c-type-list-kwds'.
  5094. (c-forward-id-comma-list type t))
  5095. ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
  5096. (c-forward-keyword-prefixed-id ref))
  5097. ;; There's a name directly after a keyword in `c-ref-list-kwds'.
  5098. (c-forward-id-comma-list ref t))
  5099. ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
  5100. (eq (char-after) ?\())
  5101. ;; There's an open paren after a keyword in `c-paren-any-kwds'.
  5102. (forward-char)
  5103. (when (and (setq pos (c-up-list-forward))
  5104. (eq (char-before pos) ?\)))
  5105. (when (and c-record-type-identifiers
  5106. (c-keyword-member kwd-sym 'c-paren-type-kwds))
  5107. ;; Use `c-forward-type' on every identifier we can find
  5108. ;; inside the paren, to record the types.
  5109. (while (c-syntactic-re-search-forward c-symbol-start pos t)
  5110. (goto-char (match-beginning 0))
  5111. (unless (c-forward-type)
  5112. (looking-at c-symbol-key) ; Always matches.
  5113. (goto-char (match-end 0)))))
  5114. (goto-char pos)
  5115. (c-forward-syntactic-ws)
  5116. (setq safe-pos (point))))
  5117. ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
  5118. (eq (char-after) ?<)
  5119. (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
  5120. (c-forward-syntactic-ws)
  5121. (setq safe-pos (point)))
  5122. ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
  5123. (not (looking-at c-symbol-start))
  5124. (c-safe (c-forward-sexp) t))
  5125. (c-forward-syntactic-ws)
  5126. (setq safe-pos (point))))
  5127. (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
  5128. (if (eq (char-after) ?:)
  5129. ;; If we are at the colon already, we move over the type
  5130. ;; list after it.
  5131. (progn
  5132. (forward-char)
  5133. (c-forward-syntactic-ws)
  5134. (when (c-forward-keyword-prefixed-id type)
  5135. (c-forward-id-comma-list type t)))
  5136. ;; Not at the colon, so stop here. But the identifier
  5137. ;; ranges in the type list later on should still be
  5138. ;; recorded.
  5139. (and c-record-type-identifiers
  5140. (progn
  5141. ;; If a keyword matched both one of the types above and
  5142. ;; this one, we match `c-colon-type-list-re' after the
  5143. ;; clause matched above.
  5144. (goto-char safe-pos)
  5145. (looking-at c-colon-type-list-re))
  5146. (progn
  5147. (goto-char (match-end 0))
  5148. (c-forward-syntactic-ws)
  5149. (c-forward-keyword-prefixed-id type))
  5150. ;; There's a type after the `c-colon-type-list-re' match
  5151. ;; after a keyword in `c-colon-type-list-kwds'.
  5152. (c-forward-id-comma-list type nil))))
  5153. (goto-char safe-pos)
  5154. t)))
  5155. ;; cc-mode requires cc-fonts.
  5156. (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
  5157. (defun c-forward-<>-arglist (all-types)
  5158. ;; The point is assumed to be at a "<". Try to treat it as the open
  5159. ;; paren of an angle bracket arglist and move forward to the
  5160. ;; corresponding ">". If successful, the point is left after the
  5161. ;; ">" and t is returned, otherwise the point isn't moved and nil is
  5162. ;; returned. If ALL-TYPES is t then all encountered arguments in
  5163. ;; the arglist that might be types are treated as found types.
  5164. ;;
  5165. ;; The variable `c-parse-and-markup-<>-arglists' controls how this
  5166. ;; function handles text properties on the angle brackets and argument
  5167. ;; separating commas.
  5168. ;;
  5169. ;; `c-restricted-<>-arglists' controls how lenient the template
  5170. ;; arglist recognition should be.
  5171. ;;
  5172. ;; This function records identifier ranges on
  5173. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5174. ;; `c-record-type-identifiers' is non-nil.
  5175. ;;
  5176. ;; This function might do hidden buffer changes.
  5177. (let ((start (point))
  5178. ;; If `c-record-type-identifiers' is set then activate
  5179. ;; recording of any found types that constitute an argument in
  5180. ;; the arglist.
  5181. (c-record-found-types (if c-record-type-identifiers t)))
  5182. (if (catch 'angle-bracket-arglist-escape
  5183. (setq c-record-found-types
  5184. (c-forward-<>-arglist-recur all-types)))
  5185. (progn
  5186. (when (consp c-record-found-types)
  5187. (setq c-record-type-identifiers
  5188. ;; `nconc' doesn't mind that the tail of
  5189. ;; `c-record-found-types' is t.
  5190. (nconc c-record-found-types c-record-type-identifiers)))
  5191. (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
  5192. t)
  5193. (goto-char start)
  5194. nil)))
  5195. (defun c-forward-<>-arglist-recur (all-types)
  5196. ;; Recursive part of `c-forward-<>-arglist'.
  5197. ;;
  5198. ;; This function might do hidden buffer changes.
  5199. (let ((start (point)) res pos tmp
  5200. ;; Cover this so that any recorded found type ranges are
  5201. ;; automatically lost if it turns out to not be an angle
  5202. ;; bracket arglist. It's propagated through the return value
  5203. ;; on successful completion.
  5204. (c-record-found-types c-record-found-types)
  5205. ;; List that collects the positions after the argument
  5206. ;; separating ',' in the arglist.
  5207. arg-start-pos)
  5208. ;; If the '<' has paren open syntax then we've marked it as an angle
  5209. ;; bracket arglist before, so skip to the end.
  5210. (if (and (not c-parse-and-markup-<>-arglists)
  5211. (c-get-char-property (point) 'syntax-table))
  5212. (progn
  5213. (forward-char)
  5214. (if (and (c-go-up-list-forward)
  5215. (eq (char-before) ?>))
  5216. t
  5217. ;; Got unmatched paren angle brackets. We don't clear the paren
  5218. ;; syntax properties and retry, on the basis that it's very
  5219. ;; unlikely that paren angle brackets become operators by code
  5220. ;; manipulation. It's far more likely that it doesn't match due
  5221. ;; to narrowing or some temporary change.
  5222. (goto-char start)
  5223. nil))
  5224. (forward-char) ; Forward over the opening '<'.
  5225. (unless (looking-at c-<-op-cont-regexp)
  5226. ;; go forward one non-alphanumeric character (group) per iteration of
  5227. ;; this loop.
  5228. (while (and
  5229. (progn
  5230. (c-forward-syntactic-ws)
  5231. (let ((orig-record-found-types c-record-found-types))
  5232. (when (or (and c-record-type-identifiers all-types)
  5233. (c-major-mode-is 'java-mode))
  5234. ;; All encountered identifiers are types, so set the
  5235. ;; promote flag and parse the type.
  5236. (progn
  5237. (c-forward-syntactic-ws)
  5238. (if (looking-at "\\?")
  5239. (forward-char)
  5240. (when (looking-at c-identifier-start)
  5241. (let ((c-promote-possible-types t)
  5242. (c-record-found-types t))
  5243. (c-forward-type))))
  5244. (c-forward-syntactic-ws)
  5245. (when (or (looking-at "extends")
  5246. (looking-at "super"))
  5247. (forward-word)
  5248. (c-forward-syntactic-ws)
  5249. (let ((c-promote-possible-types t)
  5250. (c-record-found-types t))
  5251. (c-forward-type)
  5252. (c-forward-syntactic-ws))))))
  5253. (setq pos (point)) ; e.g. first token inside the '<'
  5254. ;; Note: These regexps exploit the match order in \| so
  5255. ;; that "<>" is matched by "<" rather than "[^>:-]>".
  5256. (c-syntactic-re-search-forward
  5257. ;; Stop on ',', '|', '&', '+' and '-' to catch
  5258. ;; common binary operators that could be between
  5259. ;; two comparison expressions "a<b" and "c>d".
  5260. "[<;{},|+&-]\\|[>)]"
  5261. nil t t))
  5262. (cond
  5263. ((eq (char-before) ?>)
  5264. ;; Either an operator starting with '>' or the end of
  5265. ;; the angle bracket arglist.
  5266. (if (looking-at c->-op-cont-regexp)
  5267. (progn
  5268. (goto-char (match-end 0))
  5269. t) ; Continue the loop.
  5270. ;; The angle bracket arglist is finished.
  5271. (when c-parse-and-markup-<>-arglists
  5272. (while arg-start-pos
  5273. (c-put-c-type-property (1- (car arg-start-pos))
  5274. 'c-<>-arg-sep)
  5275. (setq arg-start-pos (cdr arg-start-pos)))
  5276. (c-mark-<-as-paren start)
  5277. (c-mark->-as-paren (1- (point))))
  5278. (setq res t)
  5279. nil)) ; Exit the loop.
  5280. ((eq (char-before) ?<)
  5281. ;; Either an operator starting with '<' or a nested arglist.
  5282. (setq pos (point))
  5283. (let (id-start id-end subres keyword-match)
  5284. (cond
  5285. ;; The '<' begins a multi-char operator.
  5286. ((looking-at c-<-op-cont-regexp)
  5287. (setq tmp (match-end 0))
  5288. (goto-char (match-end 0)))
  5289. ;; We're at a nested <.....>
  5290. ((progn
  5291. (setq tmp pos)
  5292. (backward-char) ; to the '<'
  5293. (and
  5294. (save-excursion
  5295. ;; There's always an identifier before an angle
  5296. ;; bracket arglist, or a keyword in `c-<>-type-kwds'
  5297. ;; or `c-<>-arglist-kwds'.
  5298. (c-backward-syntactic-ws)
  5299. (setq id-end (point))
  5300. (c-simple-skip-symbol-backward)
  5301. (when (or (setq keyword-match
  5302. (looking-at c-opt-<>-sexp-key))
  5303. (not (looking-at c-keywords-regexp)))
  5304. (setq id-start (point))))
  5305. (setq subres
  5306. (let ((c-promote-possible-types t)
  5307. (c-record-found-types t))
  5308. (c-forward-<>-arglist-recur
  5309. (and keyword-match
  5310. (c-keyword-member
  5311. (c-keyword-sym (match-string 1))
  5312. 'c-<>-type-kwds)))))))
  5313. ;; It was an angle bracket arglist.
  5314. (setq c-record-found-types subres)
  5315. ;; Record the identifier before the template as a type
  5316. ;; or reference depending on whether the arglist is last
  5317. ;; in a qualified identifier.
  5318. (when (and c-record-type-identifiers
  5319. (not keyword-match))
  5320. (if (and c-opt-identifier-concat-key
  5321. (progn
  5322. (c-forward-syntactic-ws)
  5323. (looking-at c-opt-identifier-concat-key)))
  5324. (c-record-ref-id (cons id-start id-end))
  5325. (c-record-type-id (cons id-start id-end)))))
  5326. ;; At a "less than" operator.
  5327. (t
  5328. (forward-char)
  5329. )))
  5330. t) ; carry on looping.
  5331. ((and (not c-restricted-<>-arglists)
  5332. (or (and (eq (char-before) ?&)
  5333. (not (eq (char-after) ?&)))
  5334. (eq (char-before) ?,)))
  5335. ;; Just another argument. Record the position. The
  5336. ;; type check stuff that made us stop at it is at
  5337. ;; the top of the loop.
  5338. (setq arg-start-pos (cons (point) arg-start-pos)))
  5339. (t
  5340. ;; Got a character that can't be in an angle bracket
  5341. ;; arglist argument. Abort using `throw', since
  5342. ;; it's useless to try to find a surrounding arglist
  5343. ;; if we're nested.
  5344. (throw 'angle-bracket-arglist-escape nil))))))
  5345. (if res
  5346. (or c-record-found-types t)))))
  5347. (defun c-backward-<>-arglist (all-types &optional limit)
  5348. ;; The point is assumed to be directly after a ">". Try to treat it
  5349. ;; as the close paren of an angle bracket arglist and move back to
  5350. ;; the corresponding "<". If successful, the point is left at
  5351. ;; the "<" and t is returned, otherwise the point isn't moved and
  5352. ;; nil is returned. ALL-TYPES is passed on to
  5353. ;; `c-forward-<>-arglist'.
  5354. ;;
  5355. ;; If the optional LIMIT is given, it bounds the backward search.
  5356. ;; It's then assumed to be at a syntactically relevant position.
  5357. ;;
  5358. ;; This is a wrapper around `c-forward-<>-arglist'. See that
  5359. ;; function for more details.
  5360. (let ((start (point)))
  5361. (backward-char)
  5362. (if (and (not c-parse-and-markup-<>-arglists)
  5363. (c-get-char-property (point) 'syntax-table))
  5364. (if (and (c-go-up-list-backward)
  5365. (eq (char-after) ?<))
  5366. t
  5367. ;; See corresponding note in `c-forward-<>-arglist'.
  5368. (goto-char start)
  5369. nil)
  5370. (while (progn
  5371. (c-syntactic-skip-backward "^<;{}" limit t)
  5372. (and
  5373. (if (eq (char-before) ?<)
  5374. t
  5375. ;; Stopped at bob or a char that isn't allowed in an
  5376. ;; arglist, so we've failed.
  5377. (goto-char start)
  5378. nil)
  5379. (if (> (point)
  5380. (progn (c-beginning-of-current-token)
  5381. (point)))
  5382. ;; If we moved then the "<" was part of some
  5383. ;; multicharacter token.
  5384. t
  5385. (backward-char)
  5386. (let ((beg-pos (point)))
  5387. (if (c-forward-<>-arglist all-types)
  5388. (cond ((= (point) start)
  5389. ;; Matched the arglist. Break the while.
  5390. (goto-char beg-pos)
  5391. nil)
  5392. ((> (point) start)
  5393. ;; We started from a non-paren ">" inside an
  5394. ;; arglist.
  5395. (goto-char start)
  5396. nil)
  5397. (t
  5398. ;; Matched a shorter arglist. Can be a nested
  5399. ;; one so continue looking.
  5400. (goto-char beg-pos)
  5401. t))
  5402. t))))))
  5403. (/= (point) start))))
  5404. (defun c-forward-name ()
  5405. ;; Move forward over a complete name if at the beginning of one,
  5406. ;; stopping at the next following token. A keyword, as such,
  5407. ;; doesn't count as a name. If the point is not at something that
  5408. ;; is recognized as a name then it stays put.
  5409. ;;
  5410. ;; A name could be something as simple as "foo" in C or something as
  5411. ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
  5412. ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
  5413. ;; int>::*volatile const" in C++ (this function is actually little
  5414. ;; more than a `looking-at' call in all modes except those that,
  5415. ;; like C++, have `c-recognize-<>-arglists' set).
  5416. ;;
  5417. ;; Return
  5418. ;; o - nil if no name is found;
  5419. ;; o - 'template if it's an identifier ending with an angle bracket
  5420. ;; arglist;
  5421. ;; o - 'operator of it's an operator identifier;
  5422. ;; o - t if it's some other kind of name.
  5423. ;;
  5424. ;; This function records identifier ranges on
  5425. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5426. ;; `c-record-type-identifiers' is non-nil.
  5427. ;;
  5428. ;; This function might do hidden buffer changes.
  5429. (let ((pos (point)) (start (point)) res id-start id-end
  5430. ;; Turn off `c-promote-possible-types' here since we might
  5431. ;; call `c-forward-<>-arglist' and we don't want it to promote
  5432. ;; every suspect thing in the arglist to a type. We're
  5433. ;; typically called from `c-forward-type' in this case, and
  5434. ;; the caller only wants the top level type that it finds to
  5435. ;; be promoted.
  5436. c-promote-possible-types)
  5437. (while
  5438. (and
  5439. (looking-at c-identifier-key)
  5440. (progn
  5441. ;; Check for keyword. We go to the last symbol in
  5442. ;; `c-identifier-key' first.
  5443. (goto-char (setq id-end (match-end 0)))
  5444. (c-simple-skip-symbol-backward)
  5445. (setq id-start (point))
  5446. (if (looking-at c-keywords-regexp)
  5447. (when (and (c-major-mode-is 'c++-mode)
  5448. (looking-at
  5449. (cc-eval-when-compile
  5450. (concat "\\(operator\\|\\(template\\)\\)"
  5451. "\\(" (c-lang-const c-nonsymbol-key c++)
  5452. "\\|$\\)")))
  5453. (if (match-beginning 2)
  5454. ;; "template" is only valid inside an
  5455. ;; identifier if preceded by "::".
  5456. (save-excursion
  5457. (c-backward-syntactic-ws)
  5458. (and (c-safe (backward-char 2) t)
  5459. (looking-at "::")))
  5460. t))
  5461. ;; Handle a C++ operator or template identifier.
  5462. (goto-char id-end)
  5463. (c-forward-syntactic-ws)
  5464. (cond ((eq (char-before id-end) ?e)
  5465. ;; Got "... ::template".
  5466. (let ((subres (c-forward-name)))
  5467. (when subres
  5468. (setq pos (point)
  5469. res subres))))
  5470. ((looking-at c-identifier-start)
  5471. ;; Got a cast operator.
  5472. (when (c-forward-type)
  5473. (setq pos (point)
  5474. res 'operator)
  5475. ;; Now we should match a sequence of either
  5476. ;; '*', '&' or a name followed by ":: *",
  5477. ;; where each can be followed by a sequence
  5478. ;; of `c-opt-type-modifier-key'.
  5479. (while (cond ((looking-at "[*&]")
  5480. (goto-char (match-end 0))
  5481. t)
  5482. ((looking-at c-identifier-start)
  5483. (and (c-forward-name)
  5484. (looking-at "::")
  5485. (progn
  5486. (goto-char (match-end 0))
  5487. (c-forward-syntactic-ws)
  5488. (eq (char-after) ?*))
  5489. (progn
  5490. (forward-char)
  5491. t))))
  5492. (while (progn
  5493. (c-forward-syntactic-ws)
  5494. (setq pos (point))
  5495. (looking-at c-opt-type-modifier-key))
  5496. (goto-char (match-end 1))))))
  5497. ((looking-at c-overloadable-operators-regexp)
  5498. ;; Got some other operator.
  5499. (setq c-last-identifier-range
  5500. (cons (point) (match-end 0)))
  5501. (goto-char (match-end 0))
  5502. (c-forward-syntactic-ws)
  5503. (setq pos (point)
  5504. res 'operator)))
  5505. nil)
  5506. ;; `id-start' is equal to `id-end' if we've jumped over
  5507. ;; an identifier that doesn't end with a symbol token.
  5508. ;; That can occur e.g. for Java import directives on the
  5509. ;; form "foo.bar.*".
  5510. (when (and id-start (/= id-start id-end))
  5511. (setq c-last-identifier-range
  5512. (cons id-start id-end)))
  5513. (goto-char id-end)
  5514. (c-forward-syntactic-ws)
  5515. (setq pos (point)
  5516. res t)))
  5517. (progn
  5518. (goto-char pos)
  5519. (when (or c-opt-identifier-concat-key
  5520. c-recognize-<>-arglists)
  5521. (cond
  5522. ((and c-opt-identifier-concat-key
  5523. (looking-at c-opt-identifier-concat-key))
  5524. ;; Got a concatenated identifier. This handles the
  5525. ;; cases with tricky syntactic whitespace that aren't
  5526. ;; covered in `c-identifier-key'.
  5527. (goto-char (match-end 0))
  5528. (c-forward-syntactic-ws)
  5529. t)
  5530. ((and c-recognize-<>-arglists
  5531. (eq (char-after) ?<))
  5532. ;; Maybe an angle bracket arglist.
  5533. (when (let ((c-record-type-identifiers t)
  5534. (c-record-found-types t))
  5535. (c-forward-<>-arglist nil))
  5536. (c-add-type start (1+ pos))
  5537. (c-forward-syntactic-ws)
  5538. (setq pos (point)
  5539. c-last-identifier-range nil)
  5540. (if (and c-opt-identifier-concat-key
  5541. (looking-at c-opt-identifier-concat-key))
  5542. ;; Continue if there's an identifier concatenation
  5543. ;; operator after the template argument.
  5544. (progn
  5545. (when (and c-record-type-identifiers id-start)
  5546. (c-record-ref-id (cons id-start id-end)))
  5547. (forward-char 2)
  5548. (c-forward-syntactic-ws)
  5549. t)
  5550. (when (and c-record-type-identifiers id-start)
  5551. (c-record-type-id (cons id-start id-end)))
  5552. (setq res 'template)
  5553. nil)))
  5554. )))))
  5555. (goto-char pos)
  5556. res))
  5557. (defun c-forward-type (&optional brace-block-too)
  5558. ;; Move forward over a type spec if at the beginning of one,
  5559. ;; stopping at the next following token. The keyword "typedef"
  5560. ;; isn't part of a type spec here.
  5561. ;;
  5562. ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
  5563. ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
  5564. ;; The current (2009-03-10) intention is to convert all uses of
  5565. ;; `c-forward-type' to call with this parameter set, then to
  5566. ;; eliminate it.
  5567. ;;
  5568. ;; Return
  5569. ;; o - t if it's a known type that can't be a name or other
  5570. ;; expression;
  5571. ;; o - 'known if it's an otherwise known type (according to
  5572. ;; `*-font-lock-extra-types');
  5573. ;; o - 'prefix if it's a known prefix of a type;
  5574. ;; o - 'found if it's a type that matches one in `c-found-types';
  5575. ;; o - 'maybe if it's an identifier that might be a type; or
  5576. ;; o - nil if it can't be a type (the point isn't moved then).
  5577. ;;
  5578. ;; The point is assumed to be at the beginning of a token.
  5579. ;;
  5580. ;; Note that this function doesn't skip past the brace definition
  5581. ;; that might be considered part of the type, e.g.
  5582. ;; "enum {a, b, c} foo".
  5583. ;;
  5584. ;; This function records identifier ranges on
  5585. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5586. ;; `c-record-type-identifiers' is non-nil.
  5587. ;;
  5588. ;; This function might do hidden buffer changes.
  5589. (when (and c-recognize-<>-arglists
  5590. (looking-at "<"))
  5591. (c-forward-<>-arglist t)
  5592. (c-forward-syntactic-ws))
  5593. (let ((start (point)) pos res name-res id-start id-end id-range)
  5594. ;; Skip leading type modifiers. If any are found we know it's a
  5595. ;; prefix of a type.
  5596. (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
  5597. (while (looking-at c-opt-type-modifier-key)
  5598. (goto-char (match-end 1))
  5599. (c-forward-syntactic-ws)
  5600. (setq res 'prefix)))
  5601. (cond
  5602. ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
  5603. ; "typedef".
  5604. (goto-char (match-end 1))
  5605. (c-forward-syntactic-ws)
  5606. (setq pos (point))
  5607. (setq name-res (c-forward-name))
  5608. (setq res (not (null name-res)))
  5609. (when (eq name-res t)
  5610. ;; In many languages the name can be used without the
  5611. ;; prefix, so we add it to `c-found-types'.
  5612. (c-add-type pos (point))
  5613. (when (and c-record-type-identifiers
  5614. c-last-identifier-range)
  5615. (c-record-type-id c-last-identifier-range)))
  5616. (when (and brace-block-too
  5617. (memq res '(t nil))
  5618. (eq (char-after) ?\{)
  5619. (save-excursion
  5620. (c-safe
  5621. (progn (c-forward-sexp)
  5622. (c-forward-syntactic-ws)
  5623. (setq pos (point))))))
  5624. (goto-char pos)
  5625. (setq res t))
  5626. (unless res (goto-char start))) ; invalid syntax
  5627. ((progn
  5628. (setq pos nil)
  5629. (if (looking-at c-identifier-start)
  5630. (save-excursion
  5631. (setq id-start (point)
  5632. name-res (c-forward-name))
  5633. (when name-res
  5634. (setq id-end (point)
  5635. id-range c-last-identifier-range))))
  5636. (and (cond ((looking-at c-primitive-type-key)
  5637. (setq res t))
  5638. ((c-with-syntax-table c-identifier-syntax-table
  5639. (looking-at c-known-type-key))
  5640. (setq res 'known)))
  5641. (or (not id-end)
  5642. (>= (save-excursion
  5643. (save-match-data
  5644. (goto-char (match-end 1))
  5645. (c-forward-syntactic-ws)
  5646. (setq pos (point))))
  5647. id-end)
  5648. (setq res nil))))
  5649. ;; Looking at a primitive or known type identifier. We've
  5650. ;; checked for a name first so that we don't go here if the
  5651. ;; known type match only is a prefix of another name.
  5652. (setq id-end (match-end 1))
  5653. (when (and c-record-type-identifiers
  5654. (or c-promote-possible-types (eq res t)))
  5655. (c-record-type-id (cons (match-beginning 1) (match-end 1))))
  5656. (if (and c-opt-type-component-key
  5657. (save-match-data
  5658. (looking-at c-opt-type-component-key)))
  5659. ;; There might be more keywords for the type.
  5660. (let (safe-pos)
  5661. (c-forward-keyword-clause 1)
  5662. (while (progn
  5663. (setq safe-pos (point))
  5664. (looking-at c-opt-type-component-key))
  5665. (when (and c-record-type-identifiers
  5666. (looking-at c-primitive-type-key))
  5667. (c-record-type-id (cons (match-beginning 1)
  5668. (match-end 1))))
  5669. (c-forward-keyword-clause 1))
  5670. (if (looking-at c-primitive-type-key)
  5671. (progn
  5672. (when c-record-type-identifiers
  5673. (c-record-type-id (cons (match-beginning 1)
  5674. (match-end 1))))
  5675. (c-forward-keyword-clause 1)
  5676. (setq res t))
  5677. (goto-char safe-pos)
  5678. (setq res 'prefix)))
  5679. (unless (save-match-data (c-forward-keyword-clause 1))
  5680. (if pos
  5681. (goto-char pos)
  5682. (goto-char (match-end 1))
  5683. (c-forward-syntactic-ws)))))
  5684. (name-res
  5685. (cond ((eq name-res t)
  5686. ;; A normal identifier.
  5687. (goto-char id-end)
  5688. (if (or res c-promote-possible-types)
  5689. (progn
  5690. (c-add-type id-start id-end)
  5691. (when (and c-record-type-identifiers id-range)
  5692. (c-record-type-id id-range))
  5693. (unless res
  5694. (setq res 'found)))
  5695. (setq res (if (c-check-type id-start id-end)
  5696. ;; It's an identifier that has been used as
  5697. ;; a type somewhere else.
  5698. 'found
  5699. ;; It's an identifier that might be a type.
  5700. 'maybe))))
  5701. ((eq name-res 'template)
  5702. ;; A template is a type.
  5703. (goto-char id-end)
  5704. (setq res t))
  5705. (t
  5706. ;; Otherwise it's an operator identifier, which is not a type.
  5707. (goto-char start)
  5708. (setq res nil)))))
  5709. (when res
  5710. ;; Skip trailing type modifiers. If any are found we know it's
  5711. ;; a type.
  5712. (when c-opt-type-modifier-key
  5713. (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
  5714. (goto-char (match-end 1))
  5715. (c-forward-syntactic-ws)
  5716. (setq res t)))
  5717. ;; Step over any type suffix operator. Do not let the existence
  5718. ;; of these alter the classification of the found type, since
  5719. ;; these operators typically are allowed in normal expressions
  5720. ;; too.
  5721. (when c-opt-type-suffix-key
  5722. (while (looking-at c-opt-type-suffix-key)
  5723. (goto-char (match-end 1))
  5724. (c-forward-syntactic-ws)))
  5725. (when c-opt-type-concat-key ; Only/mainly for pike.
  5726. ;; Look for a trailing operator that concatenates the type
  5727. ;; with a following one, and if so step past that one through
  5728. ;; a recursive call. Note that we don't record concatenated
  5729. ;; types in `c-found-types' - it's the component types that
  5730. ;; are recorded when appropriate.
  5731. (setq pos (point))
  5732. (let* ((c-promote-possible-types (or (memq res '(t known))
  5733. c-promote-possible-types))
  5734. ;; If we can't promote then set `c-record-found-types' so that
  5735. ;; we can merge in the types from the second part afterwards if
  5736. ;; it turns out to be a known type there.
  5737. (c-record-found-types (and c-record-type-identifiers
  5738. (not c-promote-possible-types)))
  5739. subres)
  5740. (if (and (looking-at c-opt-type-concat-key)
  5741. (progn
  5742. (goto-char (match-end 1))
  5743. (c-forward-syntactic-ws)
  5744. (setq subres (c-forward-type))))
  5745. (progn
  5746. ;; If either operand certainly is a type then both are, but we
  5747. ;; don't let the existence of the operator itself promote two
  5748. ;; uncertain types to a certain one.
  5749. (cond ((eq res t))
  5750. ((eq subres t)
  5751. (unless (eq name-res 'template)
  5752. (c-add-type id-start id-end))
  5753. (when (and c-record-type-identifiers id-range)
  5754. (c-record-type-id id-range))
  5755. (setq res t))
  5756. ((eq res 'known))
  5757. ((eq subres 'known)
  5758. (setq res 'known))
  5759. ((eq res 'found))
  5760. ((eq subres 'found)
  5761. (setq res 'found))
  5762. (t
  5763. (setq res 'maybe)))
  5764. (when (and (eq res t)
  5765. (consp c-record-found-types))
  5766. ;; Merge in the ranges of any types found by the second
  5767. ;; `c-forward-type'.
  5768. (setq c-record-type-identifiers
  5769. ;; `nconc' doesn't mind that the tail of
  5770. ;; `c-record-found-types' is t.
  5771. (nconc c-record-found-types
  5772. c-record-type-identifiers))))
  5773. (goto-char pos))))
  5774. (when (and c-record-found-types (memq res '(known found)) id-range)
  5775. (setq c-record-found-types
  5776. (cons id-range c-record-found-types))))
  5777. ;;(message "c-forward-type %s -> %s: %s" start (point) res)
  5778. res))
  5779. (defun c-forward-annotation ()
  5780. ;; Used for Java code only at the moment. Assumes point is on the
  5781. ;; @, moves forward an annotation. returns nil if there is no
  5782. ;; annotation at point.
  5783. (and (looking-at "@")
  5784. (progn (forward-char) t)
  5785. (c-forward-type)
  5786. (progn (c-forward-syntactic-ws) t)
  5787. (if (looking-at "(")
  5788. (c-go-list-forward)
  5789. t)))
  5790. ;; Handling of large scale constructs like statements and declarations.
  5791. ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
  5792. ;; defsubst or perhaps even a defun, but it contains lots of free
  5793. ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
  5794. (defmacro c-fdoc-shift-type-backward (&optional short)
  5795. ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
  5796. ;; of types when parsing a declaration, which means that it
  5797. ;; sometimes consumes the identifier in the declaration as a type.
  5798. ;; This is used to "backtrack" and make the last type be treated as
  5799. ;; an identifier instead.
  5800. `(progn
  5801. ,(unless short
  5802. ;; These identifiers are bound only in the inner let.
  5803. '(setq identifier-type at-type
  5804. identifier-start type-start
  5805. got-parens nil
  5806. got-identifier t
  5807. got-suffix t
  5808. got-suffix-after-parens id-start
  5809. paren-depth 0))
  5810. (if (setq at-type (if (eq backup-at-type 'prefix)
  5811. t
  5812. backup-at-type))
  5813. (setq type-start backup-type-start
  5814. id-start backup-id-start)
  5815. (setq type-start start-pos
  5816. id-start start-pos))
  5817. ;; When these flags already are set we've found specifiers that
  5818. ;; unconditionally signal these attributes - backtracking doesn't
  5819. ;; change that. So keep them set in that case.
  5820. (or at-type-decl
  5821. (setq at-type-decl backup-at-type-decl))
  5822. (or maybe-typeless
  5823. (setq maybe-typeless backup-maybe-typeless))
  5824. ,(unless short
  5825. ;; This identifier is bound only in the inner let.
  5826. '(setq start id-start))))
  5827. (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
  5828. ;; Move forward over a declaration or a cast if at the start of one.
  5829. ;; The point is assumed to be at the start of some token. Nil is
  5830. ;; returned if no declaration or cast is recognized, and the point
  5831. ;; is clobbered in that case.
  5832. ;;
  5833. ;; If a declaration is parsed:
  5834. ;;
  5835. ;; The point is left at the first token after the first complete
  5836. ;; declarator, if there is one. The return value is a cons where
  5837. ;; the car is the position of the first token in the declarator. (See
  5838. ;; below for the cdr.)
  5839. ;; Some examples:
  5840. ;;
  5841. ;; void foo (int a, char *b) stuff ...
  5842. ;; car ^ ^ point
  5843. ;; float (*a)[], b;
  5844. ;; car ^ ^ point
  5845. ;; unsigned int a = c_style_initializer, b;
  5846. ;; car ^ ^ point
  5847. ;; unsigned int a (cplusplus_style_initializer), b;
  5848. ;; car ^ ^ point (might change)
  5849. ;; class Foo : public Bar {}
  5850. ;; car ^ ^ point
  5851. ;; class PikeClass (int a, string b) stuff ...
  5852. ;; car ^ ^ point
  5853. ;; enum bool;
  5854. ;; car ^ ^ point
  5855. ;; enum bool flag;
  5856. ;; car ^ ^ point
  5857. ;; void cplusplus_function (int x) throw (Bad);
  5858. ;; car ^ ^ point
  5859. ;; Foo::Foo (int b) : Base (b) {}
  5860. ;; car ^ ^ point
  5861. ;;
  5862. ;; The cdr of the return value is non-nil when a
  5863. ;; `c-typedef-decl-kwds' specifier is found in the declaration.
  5864. ;; Specifically it is a dotted pair (A . B) where B is t when a
  5865. ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
  5866. ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
  5867. ;; specifier is present. I.e., (some of) the declared
  5868. ;; identifier(s) are types.
  5869. ;;
  5870. ;; If a cast is parsed:
  5871. ;;
  5872. ;; The point is left at the first token after the closing paren of
  5873. ;; the cast. The return value is `cast'. Note that the start
  5874. ;; position must be at the first token inside the cast parenthesis
  5875. ;; to recognize it.
  5876. ;;
  5877. ;; PRECEDING-TOKEN-END is the first position after the preceding
  5878. ;; token, i.e. on the other side of the syntactic ws from the point.
  5879. ;; Use a value less than or equal to (point-min) if the point is at
  5880. ;; the first token in (the visible part of) the buffer.
  5881. ;;
  5882. ;; CONTEXT is a symbol that describes the context at the point:
  5883. ;; 'decl In a comma-separated declaration context (typically
  5884. ;; inside a function declaration arglist).
  5885. ;; '<> In an angle bracket arglist.
  5886. ;; 'arglist Some other type of arglist.
  5887. ;; nil Some other context or unknown context. Includes
  5888. ;; within the parens of an if, for, ... construct.
  5889. ;;
  5890. ;; LAST-CAST-END is the first token after the closing paren of a
  5891. ;; preceding cast, or nil if none is known. If
  5892. ;; `c-forward-decl-or-cast-1' is used in succession, it should be
  5893. ;; the position after the closest preceding call where a cast was
  5894. ;; matched. In that case it's used to discover chains of casts like
  5895. ;; "(a) (b) c".
  5896. ;;
  5897. ;; This function records identifier ranges on
  5898. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5899. ;; `c-record-type-identifiers' is non-nil.
  5900. ;;
  5901. ;; This function might do hidden buffer changes.
  5902. (let (;; `start-pos' is used below to point to the start of the
  5903. ;; first type, i.e. after any leading specifiers. It might
  5904. ;; also point at the beginning of the preceding syntactic
  5905. ;; whitespace.
  5906. (start-pos (point))
  5907. ;; Set to the result of `c-forward-type'.
  5908. at-type
  5909. ;; The position of the first token in what we currently
  5910. ;; believe is the type in the declaration or cast, after any
  5911. ;; specifiers and their associated clauses.
  5912. type-start
  5913. ;; The position of the first token in what we currently
  5914. ;; believe is the declarator for the first identifier. Set
  5915. ;; when the type is found, and moved forward over any
  5916. ;; `c-decl-hangon-kwds' and their associated clauses that
  5917. ;; occurs after the type.
  5918. id-start
  5919. ;; These store `at-type', `type-start' and `id-start' of the
  5920. ;; identifier before the one in those variables. The previous
  5921. ;; identifier might turn out to be the real type in a
  5922. ;; declaration if the last one has to be the declarator in it.
  5923. ;; If `backup-at-type' is nil then the other variables have
  5924. ;; undefined values.
  5925. backup-at-type backup-type-start backup-id-start
  5926. ;; Set if we've found a specifier (apart from "typedef") that makes
  5927. ;; the defined identifier(s) types.
  5928. at-type-decl
  5929. ;; Set if we've a "typedef" keyword.
  5930. at-typedef
  5931. ;; Set if we've found a specifier that can start a declaration
  5932. ;; where there's no type.
  5933. maybe-typeless
  5934. ;; If a specifier is found that also can be a type prefix,
  5935. ;; these flags are set instead of those above. If we need to
  5936. ;; back up an identifier, they are copied to the real flag
  5937. ;; variables. Thus they only take effect if we fail to
  5938. ;; interpret it as a type.
  5939. backup-at-type-decl backup-maybe-typeless
  5940. ;; Whether we've found a declaration or a cast. We might know
  5941. ;; this before we've found the type in it. It's 'ids if we've
  5942. ;; found two consecutive identifiers (usually a sure sign, but
  5943. ;; we should allow that in labels too), and t if we've found a
  5944. ;; specifier keyword (a 100% sure sign).
  5945. at-decl-or-cast
  5946. ;; Set when we need to back up to parse this as a declaration
  5947. ;; but not as a cast.
  5948. backup-if-not-cast
  5949. ;; For casts, the return position.
  5950. cast-end
  5951. ;; Save `c-record-type-identifiers' and
  5952. ;; `c-record-ref-identifiers' since ranges are recorded
  5953. ;; speculatively and should be thrown away if it turns out
  5954. ;; that it isn't a declaration or cast.
  5955. (save-rec-type-ids c-record-type-identifiers)
  5956. (save-rec-ref-ids c-record-ref-identifiers))
  5957. (while (c-forward-annotation)
  5958. (c-forward-syntactic-ws))
  5959. ;; Check for a type. Unknown symbols are treated as possible
  5960. ;; types, but they could also be specifiers disguised through
  5961. ;; macros like __INLINE__, so we recognize both types and known
  5962. ;; specifiers after them too.
  5963. (while
  5964. (let* ((start (point)) kwd-sym kwd-clause-end found-type)
  5965. ;; Look for a specifier keyword clause.
  5966. (when (or (looking-at c-prefix-spec-kwds-re)
  5967. (and (c-major-mode-is 'java-mode)
  5968. (looking-at "@[A-Za-z0-9]+")))
  5969. (if (looking-at c-typedef-key)
  5970. (setq at-typedef t))
  5971. (setq kwd-sym (c-keyword-sym (match-string 1)))
  5972. (save-excursion
  5973. (c-forward-keyword-clause 1)
  5974. (setq kwd-clause-end (point))))
  5975. (when (setq found-type (c-forward-type t)) ; brace-block-too
  5976. ;; Found a known or possible type or a prefix of a known type.
  5977. (when at-type
  5978. ;; Got two identifiers with nothing but whitespace
  5979. ;; between them. That can only happen in declarations.
  5980. (setq at-decl-or-cast 'ids)
  5981. (when (eq at-type 'found)
  5982. ;; If the previous identifier is a found type we
  5983. ;; record it as a real one; it might be some sort of
  5984. ;; alias for a prefix like "unsigned".
  5985. (save-excursion
  5986. (goto-char type-start)
  5987. (let ((c-promote-possible-types t))
  5988. (c-forward-type)))))
  5989. (setq backup-at-type at-type
  5990. backup-type-start type-start
  5991. backup-id-start id-start
  5992. at-type found-type
  5993. type-start start
  5994. id-start (point)
  5995. ;; The previous ambiguous specifier/type turned out
  5996. ;; to be a type since we've parsed another one after
  5997. ;; it, so clear these backup flags.
  5998. backup-at-type-decl nil
  5999. backup-maybe-typeless nil))
  6000. (if kwd-sym
  6001. (progn
  6002. ;; Handle known specifier keywords and
  6003. ;; `c-decl-hangon-kwds' which can occur after known
  6004. ;; types.
  6005. (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
  6006. ;; It's a hang-on keyword that can occur anywhere.
  6007. (progn
  6008. (setq at-decl-or-cast t)
  6009. (if at-type
  6010. ;; Move the identifier start position if
  6011. ;; we've passed a type.
  6012. (setq id-start kwd-clause-end)
  6013. ;; Otherwise treat this as a specifier and
  6014. ;; move the fallback position.
  6015. (setq start-pos kwd-clause-end))
  6016. (goto-char kwd-clause-end))
  6017. ;; It's an ordinary specifier so we know that
  6018. ;; anything before this can't be the type.
  6019. (setq backup-at-type nil
  6020. start-pos kwd-clause-end)
  6021. (if found-type
  6022. ;; It's ambiguous whether this keyword is a
  6023. ;; specifier or a type prefix, so set the backup
  6024. ;; flags. (It's assumed that `c-forward-type'
  6025. ;; moved further than `c-forward-keyword-clause'.)
  6026. (progn
  6027. (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
  6028. (setq backup-at-type-decl t))
  6029. (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
  6030. (setq backup-maybe-typeless t)))
  6031. (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
  6032. ;; This test only happens after we've scanned a type.
  6033. ;; So, with valid syntax, kwd-sym can't be 'typedef.
  6034. (setq at-type-decl t))
  6035. (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
  6036. (setq maybe-typeless t))
  6037. ;; Haven't matched a type so it's an unambiguous
  6038. ;; specifier keyword and we know we're in a
  6039. ;; declaration.
  6040. (setq at-decl-or-cast t)
  6041. (goto-char kwd-clause-end))))
  6042. ;; If the type isn't known we continue so that we'll jump
  6043. ;; over all specifiers and type identifiers. The reason
  6044. ;; to do this for a known type prefix is to make things
  6045. ;; like "unsigned INT16" work.
  6046. (and found-type (not (eq found-type t))))))
  6047. (cond
  6048. ((eq at-type t)
  6049. ;; If a known type was found, we still need to skip over any
  6050. ;; hangon keyword clauses after it. Otherwise it has already
  6051. ;; been done in the loop above.
  6052. (while (looking-at c-decl-hangon-key)
  6053. (c-forward-keyword-clause 1))
  6054. (setq id-start (point)))
  6055. ((eq at-type 'prefix)
  6056. ;; A prefix type is itself a primitive type when it's not
  6057. ;; followed by another type.
  6058. (setq at-type t))
  6059. ((not at-type)
  6060. ;; Got no type but set things up to continue anyway to handle
  6061. ;; the various cases when a declaration doesn't start with a
  6062. ;; type.
  6063. (setq id-start start-pos))
  6064. ((and (eq at-type 'maybe)
  6065. (c-major-mode-is 'c++-mode))
  6066. ;; If it's C++ then check if the last "type" ends on the form
  6067. ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
  6068. ;; (con|de)structor.
  6069. (save-excursion
  6070. (let (name end-2 end-1)
  6071. (goto-char id-start)
  6072. (c-backward-syntactic-ws)
  6073. (setq end-2 (point))
  6074. (when (and
  6075. (c-simple-skip-symbol-backward)
  6076. (progn
  6077. (setq name
  6078. (buffer-substring-no-properties (point) end-2))
  6079. ;; Cheating in the handling of syntactic ws below.
  6080. (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
  6081. (progn
  6082. (setq end-1 (point))
  6083. (c-simple-skip-symbol-backward))
  6084. (>= (point) type-start)
  6085. (equal (buffer-substring-no-properties (point) end-1)
  6086. name))
  6087. ;; It is a (con|de)structor name. In that case the
  6088. ;; declaration is typeless so zap out any preceding
  6089. ;; identifier(s) that we might have taken as types.
  6090. (goto-char type-start)
  6091. (setq at-type nil
  6092. backup-at-type nil
  6093. id-start type-start))))))
  6094. ;; Check for and step over a type decl expression after the thing
  6095. ;; that is or might be a type. This can't be skipped since we
  6096. ;; need the correct end position of the declarator for
  6097. ;; `max-type-decl-end-*'.
  6098. (let ((start (point)) (paren-depth 0) pos
  6099. ;; True if there's a non-open-paren match of
  6100. ;; `c-type-decl-prefix-key'.
  6101. got-prefix
  6102. ;; True if the declarator is surrounded by a parenthesis pair.
  6103. got-parens
  6104. ;; True if there is an identifier in the declarator.
  6105. got-identifier
  6106. ;; True if there's a non-close-paren match of
  6107. ;; `c-type-decl-suffix-key'.
  6108. got-suffix
  6109. ;; True if there's a prefix match outside the outermost
  6110. ;; paren pair that surrounds the declarator.
  6111. got-prefix-before-parens
  6112. ;; True if there's a suffix match outside the outermost
  6113. ;; paren pair that surrounds the declarator. The value is
  6114. ;; the position of the first suffix match.
  6115. got-suffix-after-parens
  6116. ;; True if we've parsed the type decl to a token that is
  6117. ;; known to end declarations in this context.
  6118. at-decl-end
  6119. ;; The earlier values of `at-type' and `type-start' if we've
  6120. ;; shifted the type backwards.
  6121. identifier-type identifier-start
  6122. ;; If `c-parse-and-markup-<>-arglists' is set we need to
  6123. ;; turn it off during the name skipping below to avoid
  6124. ;; getting `c-type' properties that might be bogus. That
  6125. ;; can happen since we don't know if
  6126. ;; `c-restricted-<>-arglists' will be correct inside the
  6127. ;; arglist paren that gets entered.
  6128. c-parse-and-markup-<>-arglists)
  6129. (goto-char id-start)
  6130. ;; Skip over type decl prefix operators. (Note similar code in
  6131. ;; `c-font-lock-declarators'.)
  6132. (while (and (looking-at c-type-decl-prefix-key)
  6133. (if (and (c-major-mode-is 'c++-mode)
  6134. (match-beginning 3))
  6135. ;; If the second submatch matches in C++ then
  6136. ;; we're looking at an identifier that's a
  6137. ;; prefix only if it specifies a member pointer.
  6138. (when (setq got-identifier (c-forward-name))
  6139. (if (looking-at "\\(::\\)")
  6140. ;; We only check for a trailing "::" and
  6141. ;; let the "*" that should follow be
  6142. ;; matched in the next round.
  6143. (progn (setq got-identifier nil) t)
  6144. ;; It turned out to be the real identifier,
  6145. ;; so stop.
  6146. nil))
  6147. t))
  6148. (if (eq (char-after) ?\()
  6149. (progn
  6150. (setq paren-depth (1+ paren-depth))
  6151. (forward-char))
  6152. (unless got-prefix-before-parens
  6153. (setq got-prefix-before-parens (= paren-depth 0)))
  6154. (setq got-prefix t)
  6155. (goto-char (match-end 1)))
  6156. (c-forward-syntactic-ws))
  6157. (setq got-parens (> paren-depth 0))
  6158. ;; Skip over an identifier.
  6159. (or got-identifier
  6160. (and (looking-at c-identifier-start)
  6161. (setq got-identifier (c-forward-name))))
  6162. ;; Skip over type decl suffix operators.
  6163. (while (if (looking-at c-type-decl-suffix-key)
  6164. (if (eq (char-after) ?\))
  6165. (when (> paren-depth 0)
  6166. (setq paren-depth (1- paren-depth))
  6167. (forward-char)
  6168. t)
  6169. (when (if (save-match-data (looking-at "\\s\("))
  6170. (c-safe (c-forward-sexp 1) t)
  6171. (goto-char (match-end 1))
  6172. t)
  6173. (when (and (not got-suffix-after-parens)
  6174. (= paren-depth 0))
  6175. (setq got-suffix-after-parens (match-beginning 0)))
  6176. (setq got-suffix t)))
  6177. ;; No suffix matched. We might have matched the
  6178. ;; identifier as a type and the open paren of a
  6179. ;; function arglist as a type decl prefix. In that
  6180. ;; case we should "backtrack": Reinterpret the last
  6181. ;; type as the identifier, move out of the arglist and
  6182. ;; continue searching for suffix operators.
  6183. ;;
  6184. ;; Do this even if there's no preceding type, to cope
  6185. ;; with old style function declarations in K&R C,
  6186. ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
  6187. ;; style declarations. That isn't applicable in an
  6188. ;; arglist context, though.
  6189. (when (and (= paren-depth 1)
  6190. (not got-prefix-before-parens)
  6191. (not (eq at-type t))
  6192. (or backup-at-type
  6193. maybe-typeless
  6194. backup-maybe-typeless
  6195. (when c-recognize-typeless-decls
  6196. (not context)))
  6197. (setq pos (c-up-list-forward (point)))
  6198. (eq (char-before pos) ?\)))
  6199. (c-fdoc-shift-type-backward)
  6200. (goto-char pos)
  6201. t))
  6202. (c-forward-syntactic-ws))
  6203. (when (and (or maybe-typeless backup-maybe-typeless)
  6204. (not got-identifier)
  6205. (not got-prefix)
  6206. at-type)
  6207. ;; Have found no identifier but `c-typeless-decl-kwds' has
  6208. ;; matched so we know we're inside a declaration. The
  6209. ;; preceding type must be the identifier instead.
  6210. (c-fdoc-shift-type-backward))
  6211. (setq
  6212. at-decl-or-cast
  6213. (catch 'at-decl-or-cast
  6214. ;; CASE 1
  6215. (when (> paren-depth 0)
  6216. ;; Encountered something inside parens that isn't matched by
  6217. ;; the `c-type-decl-*' regexps, so it's not a type decl
  6218. ;; expression. Try to skip out to the same paren depth to
  6219. ;; not confuse the cast check below.
  6220. (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
  6221. ;; If we've found a specifier keyword then it's a
  6222. ;; declaration regardless.
  6223. (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
  6224. (setq at-decl-end
  6225. (looking-at (cond ((eq context '<>) "[,>]")
  6226. (context "[,\)]")
  6227. (t "[,;]"))))
  6228. ;; Now we've collected info about various characteristics of
  6229. ;; the construct we're looking at. Below follows a decision
  6230. ;; tree based on that. It's ordered to check more certain
  6231. ;; signs before less certain ones.
  6232. (if got-identifier
  6233. (progn
  6234. ;; CASE 2
  6235. (when (and (or at-type maybe-typeless)
  6236. (not (or got-prefix got-parens)))
  6237. ;; Got another identifier directly after the type, so it's a
  6238. ;; declaration.
  6239. (throw 'at-decl-or-cast t))
  6240. (when (and got-parens
  6241. (not got-prefix)
  6242. (not got-suffix-after-parens)
  6243. (or backup-at-type
  6244. maybe-typeless
  6245. backup-maybe-typeless))
  6246. ;; Got a declaration of the form "foo bar (gnu);" where we've
  6247. ;; recognized "bar" as the type and "gnu" as the declarator.
  6248. ;; In this case it's however more likely that "bar" is the
  6249. ;; declarator and "gnu" a function argument or initializer (if
  6250. ;; `c-recognize-paren-inits' is set), since the parens around
  6251. ;; "gnu" would be superfluous if it's a declarator. Shift the
  6252. ;; type one step backward.
  6253. (c-fdoc-shift-type-backward)))
  6254. ;; Found no identifier.
  6255. (if backup-at-type
  6256. (progn
  6257. ;; CASE 3
  6258. (when (= (point) start)
  6259. ;; Got a plain list of identifiers. If a colon follows it's
  6260. ;; a valid label, or maybe a bitfield. Otherwise the last
  6261. ;; one probably is the declared identifier and we should
  6262. ;; back up to the previous type, providing it isn't a cast.
  6263. (if (and (eq (char-after) ?:)
  6264. (not (c-major-mode-is 'java-mode)))
  6265. (cond
  6266. ;; If we've found a specifier keyword then it's a
  6267. ;; declaration regardless.
  6268. ((eq at-decl-or-cast t)
  6269. (throw 'at-decl-or-cast t))
  6270. ((and c-has-bitfields
  6271. (eq at-decl-or-cast 'ids)) ; bitfield.
  6272. (setq backup-if-not-cast t)
  6273. (throw 'at-decl-or-cast t)))
  6274. (setq backup-if-not-cast t)
  6275. (throw 'at-decl-or-cast t)))
  6276. ;; CASE 4
  6277. (when (and got-suffix
  6278. (not got-prefix)
  6279. (not got-parens))
  6280. ;; Got a plain list of identifiers followed by some suffix.
  6281. ;; If this isn't a cast then the last identifier probably is
  6282. ;; the declared one and we should back up to the previous
  6283. ;; type.
  6284. (setq backup-if-not-cast t)
  6285. (throw 'at-decl-or-cast t)))
  6286. ;; CASE 5
  6287. (when (eq at-type t)
  6288. ;; If the type is known we know that there can't be any
  6289. ;; identifier somewhere else, and it's only in declarations in
  6290. ;; e.g. function prototypes and in casts that the identifier may
  6291. ;; be left out.
  6292. (throw 'at-decl-or-cast t))
  6293. (when (= (point) start)
  6294. ;; Only got a single identifier (parsed as a type so far).
  6295. ;; CASE 6
  6296. (if (and
  6297. ;; Check that the identifier isn't at the start of an
  6298. ;; expression.
  6299. at-decl-end
  6300. (cond
  6301. ((eq context 'decl)
  6302. ;; Inside an arglist that contains declarations. If K&R
  6303. ;; style declarations and parenthesis style initializers
  6304. ;; aren't allowed then the single identifier must be a
  6305. ;; type, else we require that it's known or found
  6306. ;; (primitive types are handled above).
  6307. (or (and (not c-recognize-knr-p)
  6308. (not c-recognize-paren-inits))
  6309. (memq at-type '(known found))))
  6310. ((eq context '<>)
  6311. ;; Inside a template arglist. Accept known and found
  6312. ;; types; other identifiers could just as well be
  6313. ;; constants in C++.
  6314. (memq at-type '(known found)))))
  6315. (throw 'at-decl-or-cast t)
  6316. ;; CASE 7
  6317. ;; Can't be a valid declaration or cast, but if we've found a
  6318. ;; specifier it can't be anything else either, so treat it as
  6319. ;; an invalid/unfinished declaration or cast.
  6320. (throw 'at-decl-or-cast at-decl-or-cast))))
  6321. (if (and got-parens
  6322. (not got-prefix)
  6323. (not context)
  6324. (not (eq at-type t))
  6325. (or backup-at-type
  6326. maybe-typeless
  6327. backup-maybe-typeless
  6328. (when c-recognize-typeless-decls
  6329. (or (not got-suffix)
  6330. (not (looking-at
  6331. c-after-suffixed-type-maybe-decl-key))))))
  6332. ;; Got an empty paren pair and a preceding type that probably
  6333. ;; really is the identifier. Shift the type backwards to make
  6334. ;; the last one the identifier. This is analogous to the
  6335. ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
  6336. ;; above.
  6337. ;;
  6338. ;; Exception: In addition to the conditions in that
  6339. ;; "backtracking" code, do not shift backward if we're not
  6340. ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
  6341. ;; Since there's no preceding type, the shift would mean that
  6342. ;; the declaration is typeless. But if the regexp doesn't match
  6343. ;; then we will simply fall through in the tests below and not
  6344. ;; recognize it at all, so it's better to try it as an abstract
  6345. ;; declarator instead.
  6346. (c-fdoc-shift-type-backward)
  6347. ;; Still no identifier.
  6348. ;; CASE 8
  6349. (when (and got-prefix (or got-parens got-suffix))
  6350. ;; Require `got-prefix' together with either `got-parens' or
  6351. ;; `got-suffix' to recognize it as an abstract declarator:
  6352. ;; `got-parens' only is probably an empty function call.
  6353. ;; `got-suffix' only can build an ordinary expression together
  6354. ;; with the preceding identifier which we've taken as a type.
  6355. ;; We could actually accept on `got-prefix' only, but that can
  6356. ;; easily occur temporarily while writing an expression so we
  6357. ;; avoid that case anyway. We could do a better job if we knew
  6358. ;; the point when the fontification was invoked.
  6359. (throw 'at-decl-or-cast t))
  6360. ;; CASE 9
  6361. (when (and at-type
  6362. (not got-prefix)
  6363. (not got-parens)
  6364. got-suffix-after-parens
  6365. (eq (char-after got-suffix-after-parens) ?\())
  6366. ;; Got a type, no declarator but a paren suffix. I.e. it's a
  6367. ;; normal function call after all (or perhaps a C++ style object
  6368. ;; instantiation expression).
  6369. (throw 'at-decl-or-cast nil))))
  6370. ;; CASE 10
  6371. (when at-decl-or-cast
  6372. ;; By now we've located the type in the declaration that we know
  6373. ;; we're in.
  6374. (throw 'at-decl-or-cast t))
  6375. ;; CASE 11
  6376. (when (and got-identifier
  6377. (not context)
  6378. (looking-at c-after-suffixed-type-decl-key)
  6379. (if (and got-parens
  6380. (not got-prefix)
  6381. (not got-suffix)
  6382. (not (eq at-type t)))
  6383. ;; Shift the type backward in the case that there's a
  6384. ;; single identifier inside parens. That can only
  6385. ;; occur in K&R style function declarations so it's
  6386. ;; more likely that it really is a function call.
  6387. ;; Therefore we only do this after
  6388. ;; `c-after-suffixed-type-decl-key' has matched.
  6389. (progn (c-fdoc-shift-type-backward) t)
  6390. got-suffix-after-parens))
  6391. ;; A declaration according to `c-after-suffixed-type-decl-key'.
  6392. (throw 'at-decl-or-cast t))
  6393. ;; CASE 12
  6394. (when (and (or got-prefix (not got-parens))
  6395. (memq at-type '(t known)))
  6396. ;; It's a declaration if a known type precedes it and it can't be a
  6397. ;; function call.
  6398. (throw 'at-decl-or-cast t))
  6399. ;; If we get here we can't tell if this is a type decl or a normal
  6400. ;; expression by looking at it alone. (That's under the assumption
  6401. ;; that normal expressions always can look like type decl expressions,
  6402. ;; which isn't really true but the cases where it doesn't hold are so
  6403. ;; uncommon (e.g. some placements of "const" in C++) it's not worth
  6404. ;; the effort to look for them.)
  6405. (unless (or at-decl-end (looking-at "=[^=]"))
  6406. ;; If this is a declaration it should end here or its initializer(*)
  6407. ;; should start here, so check for allowed separation tokens. Note
  6408. ;; that this rule doesn't work e.g. with a K&R arglist after a
  6409. ;; function header.
  6410. ;;
  6411. ;; *) Don't check for C++ style initializers using parens
  6412. ;; since those already have been matched as suffixes.
  6413. ;;
  6414. ;; If `at-decl-or-cast' is then we've found some other sign that
  6415. ;; it's a declaration or cast, so then it's probably an
  6416. ;; invalid/unfinished one.
  6417. (throw 'at-decl-or-cast at-decl-or-cast))
  6418. ;; Below are tests that only should be applied when we're certain to
  6419. ;; not have parsed halfway through an expression.
  6420. ;; CASE 14
  6421. (when (memq at-type '(t known))
  6422. ;; The expression starts with a known type so treat it as a
  6423. ;; declaration.
  6424. (throw 'at-decl-or-cast t))
  6425. ;; CASE 15
  6426. (when (and (c-major-mode-is 'c++-mode)
  6427. ;; In C++ we check if the identifier is a known type, since
  6428. ;; (con|de)structors use the class name as identifier.
  6429. ;; We've always shifted over the identifier as a type and
  6430. ;; then backed up again in this case.
  6431. identifier-type
  6432. (or (memq identifier-type '(found known))
  6433. (and (eq (char-after identifier-start) ?~)
  6434. ;; `at-type' probably won't be 'found for
  6435. ;; destructors since the "~" is then part of the
  6436. ;; type name being checked against the list of
  6437. ;; known types, so do a check without that
  6438. ;; operator.
  6439. (or (save-excursion
  6440. (goto-char (1+ identifier-start))
  6441. (c-forward-syntactic-ws)
  6442. (c-with-syntax-table
  6443. c-identifier-syntax-table
  6444. (looking-at c-known-type-key)))
  6445. (save-excursion
  6446. (goto-char (1+ identifier-start))
  6447. ;; We have already parsed the type earlier,
  6448. ;; so it'd be possible to cache the end
  6449. ;; position instead of redoing it here, but
  6450. ;; then we'd need to keep track of another
  6451. ;; position everywhere.
  6452. (c-check-type (point)
  6453. (progn (c-forward-type)
  6454. (point))))))))
  6455. (throw 'at-decl-or-cast t))
  6456. (if got-identifier
  6457. (progn
  6458. ;; CASE 16
  6459. (when (and got-prefix-before-parens
  6460. at-type
  6461. (or at-decl-end (looking-at "=[^=]"))
  6462. (not context)
  6463. (not got-suffix))
  6464. ;; Got something like "foo * bar;". Since we're not inside an
  6465. ;; arglist it would be a meaningless expression because the
  6466. ;; result isn't used. We therefore choose to recognize it as
  6467. ;; a declaration. Do not allow a suffix since it could then
  6468. ;; be a function call.
  6469. (throw 'at-decl-or-cast t))
  6470. ;; CASE 17
  6471. (when (and (or got-suffix-after-parens
  6472. (looking-at "=[^=]"))
  6473. (eq at-type 'found)
  6474. (not (eq context 'arglist)))
  6475. ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
  6476. ;; be an odd expression or it could be a declaration. Treat
  6477. ;; it as a declaration if "a" has been used as a type
  6478. ;; somewhere else (if it's a known type we won't get here).
  6479. (throw 'at-decl-or-cast t)))
  6480. ;; CASE 18
  6481. (when (and context
  6482. (or got-prefix
  6483. (and (eq context 'decl)
  6484. (not c-recognize-paren-inits)
  6485. (or got-parens got-suffix))))
  6486. ;; Got a type followed by an abstract declarator. If `got-prefix'
  6487. ;; is set it's something like "a *" without anything after it. If
  6488. ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
  6489. ;; or similar, which we accept only if the context rules out
  6490. ;; expressions.
  6491. (throw 'at-decl-or-cast t)))
  6492. ;; If we had a complete symbol table here (which rules out
  6493. ;; `c-found-types') we should return t due to the disambiguation rule
  6494. ;; (in at least C++) that anything that can be parsed as a declaration
  6495. ;; is a declaration. Now we're being more defensive and prefer to
  6496. ;; highlight things like "foo (bar);" as a declaration only if we're
  6497. ;; inside an arglist that contains declarations.
  6498. (eq context 'decl))))
  6499. ;; The point is now after the type decl expression.
  6500. (cond
  6501. ;; Check for a cast.
  6502. ((save-excursion
  6503. (and
  6504. c-cast-parens
  6505. ;; Should be the first type/identifier in a cast paren.
  6506. (> preceding-token-end (point-min))
  6507. (memq (char-before preceding-token-end) c-cast-parens)
  6508. ;; The closing paren should follow.
  6509. (progn
  6510. (c-forward-syntactic-ws)
  6511. (looking-at "\\s\)"))
  6512. ;; There should be a primary expression after it.
  6513. (let (pos)
  6514. (forward-char)
  6515. (c-forward-syntactic-ws)
  6516. (setq cast-end (point))
  6517. (and (looking-at c-primary-expr-regexp)
  6518. (progn
  6519. (setq pos (match-end 0))
  6520. (or
  6521. ;; Check if the expression begins with a prefix keyword.
  6522. (match-beginning 2)
  6523. (if (match-beginning 1)
  6524. ;; Expression begins with an ambiguous operator. Treat
  6525. ;; it as a cast if it's a type decl or if we've
  6526. ;; recognized the type somewhere else.
  6527. (or at-decl-or-cast
  6528. (memq at-type '(t known found)))
  6529. ;; Unless it's a keyword, it's the beginning of a primary
  6530. ;; expression.
  6531. (not (looking-at c-keywords-regexp)))))
  6532. ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
  6533. ;; that it matched a whole one so that we don't e.g. confuse
  6534. ;; the operator '-' with '->'. It's ok if it matches further,
  6535. ;; though, since it e.g. can match the float '.5' while the
  6536. ;; operator regexp only matches '.'.
  6537. (or (not (looking-at c-nonsymbol-token-regexp))
  6538. (<= (match-end 0) pos))))
  6539. ;; There should either be a cast before it or something that isn't an
  6540. ;; identifier or close paren.
  6541. (> preceding-token-end (point-min))
  6542. (progn
  6543. (goto-char (1- preceding-token-end))
  6544. (or (eq (point) last-cast-end)
  6545. (progn
  6546. (c-backward-syntactic-ws)
  6547. (if (< (skip-syntax-backward "w_") 0)
  6548. ;; It's a symbol. Accept it only if it's one of the
  6549. ;; keywords that can precede an expression (without
  6550. ;; surrounding parens).
  6551. (looking-at c-simple-stmt-key)
  6552. (and
  6553. ;; Check that it isn't a close paren (block close is ok,
  6554. ;; though).
  6555. (not (memq (char-before) '(?\) ?\])))
  6556. ;; Check that it isn't a nonsymbol identifier.
  6557. (not (c-on-identifier)))))))))
  6558. ;; Handle the cast.
  6559. (when (and c-record-type-identifiers at-type (not (eq at-type t)))
  6560. (let ((c-promote-possible-types t))
  6561. (goto-char type-start)
  6562. (c-forward-type)))
  6563. (goto-char cast-end)
  6564. 'cast)
  6565. (at-decl-or-cast
  6566. ;; We're at a declaration. Highlight the type and the following
  6567. ;; declarators.
  6568. (when backup-if-not-cast
  6569. (c-fdoc-shift-type-backward t))
  6570. (when (and (eq context 'decl) (looking-at ","))
  6571. ;; Make sure to propagate the `c-decl-arg-start' property to
  6572. ;; the next argument if it's set in this one, to cope with
  6573. ;; interactive refontification.
  6574. (c-put-c-type-property (point) 'c-decl-arg-start))
  6575. (when (and c-record-type-identifiers at-type (not (eq at-type t)))
  6576. (let ((c-promote-possible-types t))
  6577. (save-excursion
  6578. (goto-char type-start)
  6579. (c-forward-type))))
  6580. (cons id-start
  6581. (and (or at-type-decl at-typedef)
  6582. (cons at-type-decl at-typedef))))
  6583. (t
  6584. ;; False alarm. Restore the recorded ranges.
  6585. (setq c-record-type-identifiers save-rec-type-ids
  6586. c-record-ref-identifiers save-rec-ref-ids)
  6587. nil))))
  6588. (defun c-forward-label (&optional assume-markup preceding-token-end limit)
  6589. ;; Assuming that point is at the beginning of a token, check if it starts a
  6590. ;; label and if so move over it and return non-nil (t in default situations,
  6591. ;; specific symbols (see below) for interesting situations), otherwise don't
  6592. ;; move and return nil. "Label" here means "most things with a colon".
  6593. ;;
  6594. ;; More precisely, a "label" is regarded as one of:
  6595. ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
  6596. ;; (ii) A case label - either the entire construct "case FOO:", or just the
  6597. ;; bare "case", should the colon be missing. We return t;
  6598. ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
  6599. ;; return t;
  6600. ;; (iv) One of QT's "extended" C++ variants of
  6601. ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
  6602. ;; Returns the symbol `qt-2kwds-colon'.
  6603. ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
  6604. ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
  6605. ;; colon). Currently (2006-03), this applies only to Objective C's
  6606. ;; keywords "@private", "@protected", and "@public". Returns t.
  6607. ;;
  6608. ;; One of the things which will NOT be recognized as a label is a bit-field
  6609. ;; element of a struct, something like "int foo:5".
  6610. ;;
  6611. ;; The end of the label is taken to be just after the colon, or the end of
  6612. ;; the first submatch in `c-opt-extra-label-key'. The point is directly
  6613. ;; after the end on return. The terminating char gets marked with
  6614. ;; `c-decl-end' to improve recognition of the following declaration or
  6615. ;; statement.
  6616. ;;
  6617. ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
  6618. ;; label, if any, has already been marked up like that.
  6619. ;;
  6620. ;; If PRECEDING-TOKEN-END is given, it should be the first position
  6621. ;; after the preceding token, i.e. on the other side of the
  6622. ;; syntactic ws from the point. Use a value less than or equal to
  6623. ;; (point-min) if the point is at the first token in (the visible
  6624. ;; part of) the buffer.
  6625. ;;
  6626. ;; The optional LIMIT limits the forward scan for the colon.
  6627. ;;
  6628. ;; This function records the ranges of the label symbols on
  6629. ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
  6630. ;; non-nil.
  6631. ;;
  6632. ;; This function might do hidden buffer changes.
  6633. (let ((start (point))
  6634. label-end
  6635. qt-symbol-idx
  6636. macro-start ; if we're in one.
  6637. label-type
  6638. kwd)
  6639. (cond
  6640. ;; "case" or "default" (Doesn't apply to AWK).
  6641. ((looking-at c-label-kwds-regexp)
  6642. (let ((kwd-end (match-end 1)))
  6643. ;; Record only the keyword itself for fontification, since in
  6644. ;; case labels the following is a constant expression and not
  6645. ;; a label.
  6646. (when c-record-type-identifiers
  6647. (c-record-ref-id (cons (match-beginning 1) kwd-end)))
  6648. ;; Find the label end.
  6649. (goto-char kwd-end)
  6650. (setq label-type
  6651. (if (and (c-syntactic-re-search-forward
  6652. ;; Stop on chars that aren't allowed in expressions,
  6653. ;; and on operator chars that would be meaningless
  6654. ;; there. FIXME: This doesn't cope with ?: operators.
  6655. "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
  6656. limit t t nil 1)
  6657. (match-beginning 2))
  6658. (progn ; there's a proper :
  6659. (goto-char (match-beginning 2)) ; just after the :
  6660. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6661. t)
  6662. ;; It's an unfinished label. We consider the keyword enough
  6663. ;; to recognize it as a label, so that it gets fontified.
  6664. ;; Leave the point at the end of it, but don't put any
  6665. ;; `c-decl-end' marker.
  6666. (goto-char kwd-end)
  6667. t))))
  6668. ;; @private, @protected, @public, in Objective C, or similar.
  6669. ((and c-opt-extra-label-key
  6670. (looking-at c-opt-extra-label-key))
  6671. ;; For a `c-opt-extra-label-key' match, we record the whole
  6672. ;; thing for fontification. That's to get the leading '@' in
  6673. ;; Objective-C protection labels fontified.
  6674. (goto-char (match-end 1))
  6675. (when c-record-type-identifiers
  6676. (c-record-ref-id (cons (match-beginning 1) (point))))
  6677. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6678. (setq label-type t))
  6679. ;; All other cases of labels.
  6680. ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
  6681. ;; A colon label must have something before the colon.
  6682. (not (eq (char-after) ?:))
  6683. ;; Check that we're not after a token that can't precede a label.
  6684. (or
  6685. ;; Trivially succeeds when there's no preceding token.
  6686. ;; Succeeds when we're at a virtual semicolon.
  6687. (if preceding-token-end
  6688. (<= preceding-token-end (point-min))
  6689. (save-excursion
  6690. (c-backward-syntactic-ws)
  6691. (setq preceding-token-end (point))
  6692. (or (bobp)
  6693. (c-at-vsemi-p))))
  6694. ;; Check if we're after a label, if we're after a closing
  6695. ;; paren that belong to statement, and with
  6696. ;; `c-label-prefix-re'. It's done in different order
  6697. ;; depending on `assume-markup' since the checks have
  6698. ;; different expensiveness.
  6699. (if assume-markup
  6700. (or
  6701. (eq (c-get-char-property (1- preceding-token-end) 'c-type)
  6702. 'c-decl-end)
  6703. (save-excursion
  6704. (goto-char (1- preceding-token-end))
  6705. (c-beginning-of-current-token)
  6706. (or (looking-at c-label-prefix-re)
  6707. (looking-at c-block-stmt-1-key)))
  6708. (and (eq (char-before preceding-token-end) ?\))
  6709. (c-after-conditional)))
  6710. (or
  6711. (save-excursion
  6712. (goto-char (1- preceding-token-end))
  6713. (c-beginning-of-current-token)
  6714. (or (looking-at c-label-prefix-re)
  6715. (looking-at c-block-stmt-1-key)))
  6716. (cond
  6717. ((eq (char-before preceding-token-end) ?\))
  6718. (c-after-conditional))
  6719. ((eq (char-before preceding-token-end) ?:)
  6720. ;; Might be after another label, so check it recursively.
  6721. (save-restriction
  6722. (save-excursion
  6723. (goto-char (1- preceding-token-end))
  6724. ;; Essentially the same as the
  6725. ;; `c-syntactic-re-search-forward' regexp below.
  6726. (setq macro-start
  6727. (save-excursion (and (c-beginning-of-macro)
  6728. (point))))
  6729. (if macro-start (narrow-to-region macro-start (point-max)))
  6730. (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
  6731. ;; Note: the following should work instead of the
  6732. ;; narrow-to-region above. Investigate why not,
  6733. ;; sometime. ACM, 2006-03-31.
  6734. ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
  6735. ;; macro-start t)
  6736. (let ((pte (point))
  6737. ;; If the caller turned on recording for us,
  6738. ;; it shouldn't apply when we check the
  6739. ;; preceding label.
  6740. c-record-type-identifiers)
  6741. ;; A label can't start at a cpp directive. Check for
  6742. ;; this, since c-forward-syntactic-ws would foul up on it.
  6743. (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
  6744. (c-forward-syntactic-ws)
  6745. (c-forward-label nil pte start))))))))))
  6746. ;; Point is still at the beginning of the possible label construct.
  6747. ;;
  6748. ;; Check that the next nonsymbol token is ":", or that we're in one
  6749. ;; of QT's "slots" declarations. Allow '(' for the sake of macro
  6750. ;; arguments. FIXME: Should build this regexp from the language
  6751. ;; constants.
  6752. (cond
  6753. ;; public: protected: private:
  6754. ((and
  6755. (c-major-mode-is 'c++-mode)
  6756. (search-forward-regexp
  6757. "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
  6758. (progn (backward-char)
  6759. (c-forward-syntactic-ws limit)
  6760. (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
  6761. (forward-char)
  6762. (setq label-type t))
  6763. ;; QT double keyword like "protected slots:" or goto target.
  6764. ((progn (goto-char start) nil))
  6765. ((when (c-syntactic-re-search-forward
  6766. "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
  6767. (backward-char)
  6768. (setq label-end (point))
  6769. (setq qt-symbol-idx
  6770. (and (c-major-mode-is 'c++-mode)
  6771. (string-match
  6772. "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
  6773. (buffer-substring start (point)))))
  6774. (c-forward-syntactic-ws limit)
  6775. (cond
  6776. ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
  6777. (forward-char)
  6778. (setq label-type
  6779. (if (or (string= "signals" ; Special QT macro
  6780. (setq kwd (buffer-substring-no-properties start label-end)))
  6781. (string= "Q_SIGNALS" kwd))
  6782. 'qt-1kwd-colon
  6783. 'goto-target)))
  6784. ((and qt-symbol-idx
  6785. (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
  6786. (progn (c-forward-syntactic-ws limit)
  6787. (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
  6788. (forward-char)
  6789. (setq label-type 'qt-2kwds-colon)))))))
  6790. (save-restriction
  6791. (narrow-to-region start (point))
  6792. ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
  6793. (catch 'check-label
  6794. (goto-char start)
  6795. (while (progn
  6796. (when (looking-at c-nonlabel-token-key)
  6797. (goto-char start)
  6798. (setq label-type nil)
  6799. (throw 'check-label nil))
  6800. (and (c-safe (c-forward-sexp)
  6801. (c-forward-syntactic-ws)
  6802. t)
  6803. (not (eobp)))))
  6804. ;; Record the identifiers in the label for fontification, unless
  6805. ;; it begins with `c-label-kwds' in which case the following
  6806. ;; identifiers are part of a (constant) expression that
  6807. ;; shouldn't be fontified.
  6808. (when (and c-record-type-identifiers
  6809. (progn (goto-char start)
  6810. (not (looking-at c-label-kwds-regexp))))
  6811. (while (c-syntactic-re-search-forward c-symbol-key nil t)
  6812. (c-record-ref-id (cons (match-beginning 0)
  6813. (match-end 0)))))
  6814. (c-put-c-type-property (1- (point-max)) 'c-decl-end)
  6815. (goto-char (point-max)))))
  6816. (t
  6817. ;; Not a label.
  6818. (goto-char start)))
  6819. label-type))
  6820. (defun c-forward-objc-directive ()
  6821. ;; Assuming the point is at the beginning of a token, try to move
  6822. ;; forward to the end of the Objective-C directive that starts
  6823. ;; there. Return t if a directive was fully recognized, otherwise
  6824. ;; the point is moved as far as one could be successfully parsed and
  6825. ;; nil is returned.
  6826. ;;
  6827. ;; This function records identifier ranges on
  6828. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  6829. ;; `c-record-type-identifiers' is non-nil.
  6830. ;;
  6831. ;; This function might do hidden buffer changes.
  6832. (let ((start (point))
  6833. start-char
  6834. (c-promote-possible-types t)
  6835. lim
  6836. ;; Turn off recognition of angle bracket arglists while parsing
  6837. ;; types here since the protocol reference list might then be
  6838. ;; considered part of the preceding name or superclass-name.
  6839. c-recognize-<>-arglists)
  6840. (if (or
  6841. (when (looking-at
  6842. (eval-when-compile
  6843. (c-make-keywords-re t
  6844. (append (c-lang-const c-protection-kwds objc)
  6845. '("@end"))
  6846. 'objc-mode)))
  6847. (goto-char (match-end 1))
  6848. t)
  6849. (and
  6850. (looking-at
  6851. (eval-when-compile
  6852. (c-make-keywords-re t
  6853. '("@interface" "@implementation" "@protocol")
  6854. 'objc-mode)))
  6855. ;; Handle the name of the class itself.
  6856. (progn
  6857. ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
  6858. ; at EOB.
  6859. (goto-char (match-end 0))
  6860. (setq lim (point))
  6861. (c-skip-ws-forward)
  6862. (c-forward-type))
  6863. (catch 'break
  6864. ;; Look for ": superclass-name" or "( category-name )".
  6865. (when (looking-at "[:\(]")
  6866. (setq start-char (char-after))
  6867. (forward-char)
  6868. (c-forward-syntactic-ws)
  6869. (unless (c-forward-type) (throw 'break nil))
  6870. (when (eq start-char ?\()
  6871. (unless (eq (char-after) ?\)) (throw 'break nil))
  6872. (forward-char)
  6873. (c-forward-syntactic-ws)))
  6874. ;; Look for a protocol reference list.
  6875. (if (eq (char-after) ?<)
  6876. (let ((c-recognize-<>-arglists t)
  6877. (c-parse-and-markup-<>-arglists t)
  6878. c-restricted-<>-arglists)
  6879. (c-forward-<>-arglist t))
  6880. t))))
  6881. (progn
  6882. (c-backward-syntactic-ws lim)
  6883. (c-clear-c-type-property start (1- (point)) 'c-decl-end)
  6884. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6885. t)
  6886. (c-clear-c-type-property start (point) 'c-decl-end)
  6887. nil)))
  6888. (defun c-beginning-of-inheritance-list (&optional lim)
  6889. ;; Go to the first non-whitespace after the colon that starts a
  6890. ;; multiple inheritance introduction. Optional LIM is the farthest
  6891. ;; back we should search.
  6892. ;;
  6893. ;; This function might do hidden buffer changes.
  6894. (c-with-syntax-table c++-template-syntax-table
  6895. (c-backward-token-2 0 t lim)
  6896. (while (and (or (looking-at c-symbol-start)
  6897. (looking-at "[<,]\\|::"))
  6898. (zerop (c-backward-token-2 1 t lim))))))
  6899. (defun c-in-method-def-p ()
  6900. ;; Return nil if we aren't in a method definition, otherwise the
  6901. ;; position of the initial [+-].
  6902. ;;
  6903. ;; This function might do hidden buffer changes.
  6904. (save-excursion
  6905. (beginning-of-line)
  6906. (and c-opt-method-key
  6907. (looking-at c-opt-method-key)
  6908. (point))
  6909. ))
  6910. ;; Contributed by Kevin Ryde <user42@zip.com.au>.
  6911. (defun c-in-gcc-asm-p ()
  6912. ;; Return non-nil if point is within a gcc \"asm\" block.
  6913. ;;
  6914. ;; This should be called with point inside an argument list.
  6915. ;;
  6916. ;; Only one level of enclosing parentheses is considered, so for
  6917. ;; instance `nil' is returned when in a function call within an asm
  6918. ;; operand.
  6919. ;;
  6920. ;; This function might do hidden buffer changes.
  6921. (and c-opt-asm-stmt-key
  6922. (save-excursion
  6923. (beginning-of-line)
  6924. (backward-up-list 1)
  6925. (c-beginning-of-statement-1 (point-min) nil t)
  6926. (looking-at c-opt-asm-stmt-key))))
  6927. (defun c-at-toplevel-p ()
  6928. "Return a determination as to whether point is \"at the top level\".
  6929. Informally, \"at the top level\" is anywhere where you can write
  6930. a function.
  6931. More precisely, being at the top-level means that point is either
  6932. outside any enclosing block (such as a function definition), or
  6933. directly inside a class, namespace or other block that contains
  6934. another declaration level.
  6935. If point is not at the top-level (e.g. it is inside a method
  6936. definition), then nil is returned. Otherwise, if point is at a
  6937. top-level not enclosed within a class definition, t is returned.
  6938. Otherwise, a 2-vector is returned where the zeroth element is the
  6939. buffer position of the start of the class declaration, and the first
  6940. element is the buffer position of the enclosing class's opening
  6941. brace.
  6942. Note that this function might do hidden buffer changes. See the
  6943. comment at the start of cc-engine.el for more info."
  6944. (let ((paren-state (c-parse-state)))
  6945. (or (not (c-most-enclosing-brace paren-state))
  6946. (c-search-uplist-for-classkey paren-state))))
  6947. (defun c-just-after-func-arglist-p (&optional lim)
  6948. ;; Return non-nil if the point is in the region after the argument
  6949. ;; list of a function and its opening brace (or semicolon in case it
  6950. ;; got no body). If there are K&R style argument declarations in
  6951. ;; that region, the point has to be inside the first one for this
  6952. ;; function to recognize it.
  6953. ;;
  6954. ;; If successful, the point is moved to the first token after the
  6955. ;; function header (see `c-forward-decl-or-cast-1' for details) and
  6956. ;; the position of the opening paren of the function arglist is
  6957. ;; returned.
  6958. ;;
  6959. ;; The point is clobbered if not successful.
  6960. ;;
  6961. ;; LIM is used as bound for backward buffer searches.
  6962. ;;
  6963. ;; This function might do hidden buffer changes.
  6964. (let ((beg (point)) end id-start)
  6965. (and
  6966. (eq (c-beginning-of-statement-1 lim) 'same)
  6967. (not (and (c-major-mode-is 'objc-mode)
  6968. (c-forward-objc-directive)))
  6969. (setq id-start
  6970. (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
  6971. (< id-start beg)
  6972. ;; There should not be a '=' or ',' between beg and the
  6973. ;; start of the declaration since that means we were in the
  6974. ;; "expression part" of the declaration.
  6975. (or (> (point) beg)
  6976. (not (looking-at "[=,]")))
  6977. (save-excursion
  6978. ;; Check that there's an arglist paren in the
  6979. ;; declaration.
  6980. (goto-char id-start)
  6981. (cond ((eq (char-after) ?\()
  6982. ;; The declarator is a paren expression, so skip past it
  6983. ;; so that we don't get stuck on that instead of the
  6984. ;; function arglist.
  6985. (c-forward-sexp))
  6986. ((and c-opt-op-identifier-prefix
  6987. (looking-at c-opt-op-identifier-prefix))
  6988. ;; Don't trip up on "operator ()".
  6989. (c-forward-token-2 2 t)))
  6990. (and (< (point) beg)
  6991. (c-syntactic-re-search-forward "(" beg t t)
  6992. (1- (point)))))))
  6993. (defun c-in-knr-argdecl (&optional lim)
  6994. ;; Return the position of the first argument declaration if point is
  6995. ;; inside a K&R style argument declaration list, nil otherwise.
  6996. ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
  6997. ;; position that bounds the backward search for the argument list.
  6998. ;;
  6999. ;; Point must be within a possible K&R region, e.g. just before a top-level
  7000. ;; "{". It must be outside of parens and brackets. The test can return
  7001. ;; false positives otherwise.
  7002. ;;
  7003. ;; This function might do hidden buffer changes.
  7004. (save-excursion
  7005. (save-restriction
  7006. ;; If we're in a macro, our search range is restricted to it. Narrow to
  7007. ;; the searchable range.
  7008. (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
  7009. (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
  7010. (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
  7011. before-lparen after-rparen
  7012. (pp-count-out 20)) ; Max number of paren/brace constructs before
  7013. ; we give up
  7014. (narrow-to-region low-lim (or macro-end (point-max)))
  7015. ;; Search backwards for the defun's argument list. We give up if we
  7016. ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
  7017. ;; a knr region) or BOB.
  7018. ;;
  7019. ;; The criterion for a paren structure being the arg list is:
  7020. ;; o - there is non-WS stuff after it but before any "{"; AND
  7021. ;; o - the token after it isn't a ";" AND
  7022. ;; o - it is preceded by either an identifier (the function name) or
  7023. ;; a macro expansion like "DEFUN (...)"; AND
  7024. ;; o - its content is a non-empty comma-separated list of identifiers
  7025. ;; (an empty arg list won't have a knr region).
  7026. ;;
  7027. ;; The following snippet illustrates these rules:
  7028. ;; int foo (bar, baz, yuk)
  7029. ;; int bar [] ;
  7030. ;; int (*baz) (my_type) ;
  7031. ;; int (*) (void) (*yuk) (void) ;
  7032. ;; {
  7033. (catch 'knr
  7034. (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
  7035. (setq pp-count-out (1- pp-count-out))
  7036. (c-syntactic-skip-backward "^)]}=")
  7037. (cond ((eq (char-before) ?\))
  7038. (setq after-rparen (point)))
  7039. ((eq (char-before) ?\])
  7040. (setq after-rparen nil))
  7041. (t ; either } (hit previous defun) or = or no more
  7042. ; parens/brackets.
  7043. (throw 'knr nil)))
  7044. (if after-rparen
  7045. ;; We're inside a paren. Could it be our argument list....?
  7046. (if
  7047. (and
  7048. (progn
  7049. (goto-char after-rparen)
  7050. (unless (c-go-list-backward) (throw 'knr nil)) ;
  7051. ;; FIXME!!! What about macros between the parens? 2007/01/20
  7052. (setq before-lparen (point)))
  7053. ;; It can't be the arg list if next token is ; or {
  7054. (progn (goto-char after-rparen)
  7055. (c-forward-syntactic-ws)
  7056. (not (memq (char-after) '(?\; ?\{ ?\=))))
  7057. ;; Is the thing preceding the list an identifier (the
  7058. ;; function name), or a macro expansion?
  7059. (progn
  7060. (goto-char before-lparen)
  7061. (eq (c-backward-token-2) 0)
  7062. (or (eq (c-on-identifier) (point))
  7063. (and (eq (char-after) ?\))
  7064. (c-go-up-list-backward)
  7065. (eq (c-backward-token-2) 0)
  7066. (eq (c-on-identifier) (point)))))
  7067. ;; Have we got a non-empty list of comma-separated
  7068. ;; identifiers?
  7069. (progn
  7070. (goto-char before-lparen)
  7071. (c-forward-token-2) ; to first token inside parens
  7072. (and
  7073. (c-on-identifier)
  7074. (c-forward-token-2)
  7075. (catch 'id-list
  7076. (while (eq (char-after) ?\,)
  7077. (c-forward-token-2)
  7078. (unless (c-on-identifier) (throw 'id-list nil))
  7079. (c-forward-token-2))
  7080. (eq (char-after) ?\))))))
  7081. ;; ...Yes. We've identified the function's argument list.
  7082. (throw 'knr
  7083. (progn (goto-char after-rparen)
  7084. (c-forward-syntactic-ws)
  7085. (point)))
  7086. ;; ...No. The current parens aren't the function's arg list.
  7087. (goto-char before-lparen))
  7088. (or (c-go-list-backward) ; backwards over [ .... ]
  7089. (throw 'knr nil)))))))))
  7090. (defun c-skip-conditional ()
  7091. ;; skip forward over conditional at point, including any predicate
  7092. ;; statements in parentheses. No error checking is performed.
  7093. ;;
  7094. ;; This function might do hidden buffer changes.
  7095. (c-forward-sexp (cond
  7096. ;; else if()
  7097. ((looking-at (concat "\\<else"
  7098. "\\([ \t\n]\\|\\\\\n\\)+"
  7099. "if\\>\\([^_]\\|$\\)"))
  7100. 3)
  7101. ;; do, else, try, finally
  7102. ((looking-at (concat "\\<\\("
  7103. "do\\|else\\|try\\|finally"
  7104. "\\)\\>\\([^_]\\|$\\)"))
  7105. 1)
  7106. ;; for, if, while, switch, catch, synchronized, foreach
  7107. (t 2))))
  7108. (defun c-after-conditional (&optional lim)
  7109. ;; If looking at the token after a conditional then return the
  7110. ;; position of its start, otherwise return nil.
  7111. ;;
  7112. ;; This function might do hidden buffer changes.
  7113. (save-excursion
  7114. (and (zerop (c-backward-token-2 1 t lim))
  7115. (or (looking-at c-block-stmt-1-key)
  7116. (and (eq (char-after) ?\()
  7117. (zerop (c-backward-token-2 1 t lim))
  7118. (looking-at c-block-stmt-2-key)))
  7119. (point))))
  7120. (defun c-after-special-operator-id (&optional lim)
  7121. ;; If the point is after an operator identifier that isn't handled
  7122. ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
  7123. ;; position of the start of that identifier is returned. nil is
  7124. ;; returned otherwise. The point may be anywhere in the syntactic
  7125. ;; whitespace after the last token of the operator identifier.
  7126. ;;
  7127. ;; This function might do hidden buffer changes.
  7128. (save-excursion
  7129. (and c-overloadable-operators-regexp
  7130. (zerop (c-backward-token-2 1 nil lim))
  7131. (looking-at c-overloadable-operators-regexp)
  7132. (or (not c-opt-op-identifier-prefix)
  7133. (and
  7134. (zerop (c-backward-token-2 1 nil lim))
  7135. (looking-at c-opt-op-identifier-prefix)))
  7136. (point))))
  7137. (defsubst c-backward-to-block-anchor (&optional lim)
  7138. ;; Assuming point is at a brace that opens a statement block of some
  7139. ;; kind, move to the proper anchor point for that block. It might
  7140. ;; need to be adjusted further by c-add-stmt-syntax, but the
  7141. ;; position at return is suitable as start position for that
  7142. ;; function.
  7143. ;;
  7144. ;; This function might do hidden buffer changes.
  7145. (unless (= (point) (c-point 'boi))
  7146. (let ((start (c-after-conditional lim)))
  7147. (if start
  7148. (goto-char start)))))
  7149. (defsubst c-backward-to-decl-anchor (&optional lim)
  7150. ;; Assuming point is at a brace that opens the block of a top level
  7151. ;; declaration of some kind, move to the proper anchor point for
  7152. ;; that block.
  7153. ;;
  7154. ;; This function might do hidden buffer changes.
  7155. (unless (= (point) (c-point 'boi))
  7156. (c-beginning-of-statement-1 lim)))
  7157. (defun c-search-decl-header-end ()
  7158. ;; Search forward for the end of the "header" of the current
  7159. ;; declaration. That's the position where the definition body
  7160. ;; starts, or the first variable initializer, or the ending
  7161. ;; semicolon. I.e. search forward for the closest following
  7162. ;; (syntactically relevant) '{', '=' or ';' token. Point is left
  7163. ;; _after_ the first found token, or at point-max if none is found.
  7164. ;;
  7165. ;; This function might do hidden buffer changes.
  7166. (let ((base (point)))
  7167. (if (c-major-mode-is 'c++-mode)
  7168. ;; In C++ we need to take special care to handle operator
  7169. ;; tokens and those pesky template brackets.
  7170. (while (and
  7171. (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
  7172. (or
  7173. (c-end-of-current-token base)
  7174. ;; Handle operator identifiers, i.e. ignore any
  7175. ;; operator token preceded by "operator".
  7176. (save-excursion
  7177. (and (c-safe (c-backward-sexp) t)
  7178. (looking-at c-opt-op-identifier-prefix)))
  7179. (and (eq (char-before) ?<)
  7180. (c-with-syntax-table c++-template-syntax-table
  7181. (if (c-safe (goto-char (c-up-list-forward (point))))
  7182. t
  7183. (goto-char (point-max))
  7184. nil)))))
  7185. (setq base (point)))
  7186. (while (and
  7187. (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
  7188. (c-end-of-current-token base))
  7189. (setq base (point))))))
  7190. (defun c-beginning-of-decl-1 (&optional lim)
  7191. ;; Go to the beginning of the current declaration, or the beginning
  7192. ;; of the previous one if already at the start of it. Point won't
  7193. ;; be moved out of any surrounding paren. Return a cons cell of the
  7194. ;; form (MOVE . KNR-POS). MOVE is like the return value from
  7195. ;; `c-beginning-of-statement-1'. If point skipped over some K&R
  7196. ;; style argument declarations (and they are to be recognized) then
  7197. ;; KNR-POS is set to the start of the first such argument
  7198. ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
  7199. ;; position that bounds the backward search.
  7200. ;;
  7201. ;; NB: Cases where the declaration continues after the block, as in
  7202. ;; "struct foo { ... } bar;", are currently recognized as two
  7203. ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
  7204. ;;
  7205. ;; This function might do hidden buffer changes.
  7206. (catch 'return
  7207. (let* ((start (point))
  7208. (last-stmt-start (point))
  7209. (move (c-beginning-of-statement-1 lim nil t)))
  7210. ;; `c-beginning-of-statement-1' stops at a block start, but we
  7211. ;; want to continue if the block doesn't begin a top level
  7212. ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
  7213. ;; or an open paren.
  7214. (let ((beg (point)) tentative-move)
  7215. ;; Go back one "statement" each time round the loop until we're just
  7216. ;; after a ;, }, or :, or at BOB or the start of a macro or start of
  7217. ;; an ObjC method. This will move over a multiple declaration whose
  7218. ;; components are comma separated.
  7219. (while (and
  7220. ;; Must check with c-opt-method-key in ObjC mode.
  7221. (not (and c-opt-method-key
  7222. (looking-at c-opt-method-key)))
  7223. (/= last-stmt-start (point))
  7224. (progn
  7225. (c-backward-syntactic-ws lim)
  7226. (not (memq (char-before) '(?\; ?} ?: nil))))
  7227. (save-excursion
  7228. (backward-char)
  7229. (not (looking-at "\\s(")))
  7230. ;; Check that we don't move from the first thing in a
  7231. ;; macro to its header.
  7232. (not (eq (setq tentative-move
  7233. (c-beginning-of-statement-1 lim nil t))
  7234. 'macro)))
  7235. (setq last-stmt-start beg
  7236. beg (point)
  7237. move tentative-move))
  7238. (goto-char beg))
  7239. (when c-recognize-knr-p
  7240. (let ((fallback-pos (point)) knr-argdecl-start)
  7241. ;; Handle K&R argdecls. Back up after the "statement" jumped
  7242. ;; over by `c-beginning-of-statement-1', unless it was the
  7243. ;; function body, in which case we're sitting on the opening
  7244. ;; brace now. Then test if we're in a K&R argdecl region and
  7245. ;; that we started at the other side of the first argdecl in
  7246. ;; it.
  7247. (unless (eq (char-after) ?{)
  7248. (goto-char last-stmt-start))
  7249. (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
  7250. (< knr-argdecl-start start)
  7251. (progn
  7252. (goto-char knr-argdecl-start)
  7253. (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
  7254. (throw 'return
  7255. (cons (if (eq (char-after fallback-pos) ?{)
  7256. 'previous
  7257. 'same)
  7258. knr-argdecl-start))
  7259. (goto-char fallback-pos))))
  7260. ;; `c-beginning-of-statement-1' counts each brace block as a separate
  7261. ;; statement, so the result will be 'previous if we've moved over any.
  7262. ;; So change our result back to 'same if necessary.
  7263. ;;
  7264. ;; If they were brace list initializers we might not have moved over a
  7265. ;; declaration boundary though, so change it to 'same if we've moved
  7266. ;; past a '=' before '{', but not ';'. (This ought to be integrated
  7267. ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
  7268. ;; potentially can search over a large amount of text.). Take special
  7269. ;; pains not to get mislead by C++'s "operator=", and the like.
  7270. (if (and (eq move 'previous)
  7271. (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
  7272. c++-template-syntax-table
  7273. (syntax-table))
  7274. (save-excursion
  7275. (and
  7276. (progn
  7277. (while ; keep going back to "[;={"s until we either find
  7278. ; no more, or get to one which isn't an "operator ="
  7279. (and (c-syntactic-re-search-forward "[;={]" start t t t)
  7280. (eq (char-before) ?=)
  7281. c-overloadable-operators-regexp
  7282. c-opt-op-identifier-prefix
  7283. (save-excursion
  7284. (eq (c-backward-token-2) 0)
  7285. (looking-at c-overloadable-operators-regexp)
  7286. (eq (c-backward-token-2) 0)
  7287. (looking-at c-opt-op-identifier-prefix))))
  7288. (eq (char-before) ?=))
  7289. (c-syntactic-re-search-forward "[;{]" start t t)
  7290. (eq (char-before) ?{)
  7291. (c-safe (goto-char (c-up-list-forward (point))) t)
  7292. (not (c-syntactic-re-search-forward ";" start t t))))))
  7293. (cons 'same nil)
  7294. (cons move nil)))))
  7295. (defun c-end-of-decl-1 ()
  7296. ;; Assuming point is at the start of a declaration (as detected by
  7297. ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
  7298. ;; `c-beginning-of-decl-1', this function handles the case when a
  7299. ;; block is followed by identifiers in e.g. struct declarations in C
  7300. ;; or C++. If a proper end was found then t is returned, otherwise
  7301. ;; point is moved as far as possible within the current sexp and nil
  7302. ;; is returned. This function doesn't handle macros; use
  7303. ;; `c-end-of-macro' instead in those cases.
  7304. ;;
  7305. ;; This function might do hidden buffer changes.
  7306. (let ((start (point))
  7307. (decl-syntax-table (if (c-major-mode-is 'c++-mode)
  7308. c++-template-syntax-table
  7309. (syntax-table))))
  7310. (catch 'return
  7311. (c-search-decl-header-end)
  7312. (when (and c-recognize-knr-p
  7313. (eq (char-before) ?\;)
  7314. (c-in-knr-argdecl start))
  7315. ;; Stopped at the ';' in a K&R argdecl section which is
  7316. ;; detected using the same criteria as in
  7317. ;; `c-beginning-of-decl-1'. Move to the following block
  7318. ;; start.
  7319. (c-syntactic-re-search-forward "{" nil 'move t))
  7320. (when (eq (char-before) ?{)
  7321. ;; Encountered a block in the declaration. Jump over it.
  7322. (condition-case nil
  7323. (goto-char (c-up-list-forward (point)))
  7324. (error (goto-char (point-max))
  7325. (throw 'return nil)))
  7326. (if (or (not c-opt-block-decls-with-vars-key)
  7327. (save-excursion
  7328. (c-with-syntax-table decl-syntax-table
  7329. (let ((lim (point)))
  7330. (goto-char start)
  7331. (not (and
  7332. ;; Check for `c-opt-block-decls-with-vars-key'
  7333. ;; before the first paren.
  7334. (c-syntactic-re-search-forward
  7335. (concat "[;=\(\[{]\\|\\("
  7336. c-opt-block-decls-with-vars-key
  7337. "\\)")
  7338. lim t t t)
  7339. (match-beginning 1)
  7340. (not (eq (char-before) ?_))
  7341. ;; Check that the first following paren is
  7342. ;; the block.
  7343. (c-syntactic-re-search-forward "[;=\(\[{]"
  7344. lim t t t)
  7345. (eq (char-before) ?{)))))))
  7346. ;; The declaration doesn't have any of the
  7347. ;; `c-opt-block-decls-with-vars' keywords in the
  7348. ;; beginning, so it ends here at the end of the block.
  7349. (throw 'return t)))
  7350. (c-with-syntax-table decl-syntax-table
  7351. (while (progn
  7352. (if (eq (char-before) ?\;)
  7353. (throw 'return t))
  7354. (c-syntactic-re-search-forward ";" nil 'move t))))
  7355. nil)))
  7356. (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
  7357. ;; Assuming the point is at an open brace, check if it starts a
  7358. ;; block that contains another declaration level, i.e. that isn't a
  7359. ;; statement block or a brace list, and if so return non-nil.
  7360. ;;
  7361. ;; If the check is successful, the return value is the start of the
  7362. ;; keyword that tells what kind of construct it is, i.e. typically
  7363. ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
  7364. ;; the point will be at the start of the construct, before any
  7365. ;; leading specifiers, otherwise it's at the returned position.
  7366. ;;
  7367. ;; The point is clobbered if the check is unsuccessful.
  7368. ;;
  7369. ;; CONTAINING-SEXP is the position of the open of the surrounding
  7370. ;; paren, or nil if none.
  7371. ;;
  7372. ;; The optional LIMIT limits the backward search for the start of
  7373. ;; the construct. It's assumed to be at a syntactically relevant
  7374. ;; position.
  7375. ;;
  7376. ;; If any template arglists are found in the searched region before
  7377. ;; the open brace, they get marked with paren syntax.
  7378. ;;
  7379. ;; This function might do hidden buffer changes.
  7380. (let ((open-brace (point)) kwd-start first-specifier-pos)
  7381. (c-syntactic-skip-backward c-block-prefix-charset limit t)
  7382. (when (and c-recognize-<>-arglists
  7383. (eq (char-before) ?>))
  7384. ;; Could be at the end of a template arglist.
  7385. (let ((c-parse-and-markup-<>-arglists t)
  7386. (c-disallow-comma-in-<>-arglists
  7387. (and containing-sexp
  7388. (not (eq (char-after containing-sexp) ?{)))))
  7389. (while (and
  7390. (c-backward-<>-arglist nil limit)
  7391. (progn
  7392. (c-syntactic-skip-backward c-block-prefix-charset limit t)
  7393. (eq (char-before) ?>))))))
  7394. ;; Note: Can't get bogus hits inside template arglists below since they
  7395. ;; have gotten paren syntax above.
  7396. (when (and
  7397. ;; If `goto-start' is set we begin by searching for the
  7398. ;; first possible position of a leading specifier list.
  7399. ;; The `c-decl-block-key' search continues from there since
  7400. ;; we know it can't match earlier.
  7401. (if goto-start
  7402. (when (c-syntactic-re-search-forward c-symbol-start
  7403. open-brace t t)
  7404. (goto-char (setq first-specifier-pos (match-beginning 0)))
  7405. t)
  7406. t)
  7407. (cond
  7408. ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
  7409. (goto-char (setq kwd-start (match-beginning 0)))
  7410. (or
  7411. ;; Found a keyword that can't be a type?
  7412. (match-beginning 1)
  7413. ;; Can be a type too, in which case it's the return type of a
  7414. ;; function (under the assumption that no declaration level
  7415. ;; block construct starts with a type).
  7416. (not (c-forward-type))
  7417. ;; Jumped over a type, but it could be a declaration keyword
  7418. ;; followed by the declared identifier that we've jumped over
  7419. ;; instead (e.g. in "class Foo {"). If it indeed is a type
  7420. ;; then we should be at the declarator now, so check for a
  7421. ;; valid declarator start.
  7422. ;;
  7423. ;; Note: This doesn't cope with the case when a declared
  7424. ;; identifier is followed by e.g. '(' in a language where '('
  7425. ;; also might be part of a declarator expression. Currently
  7426. ;; there's no such language.
  7427. (not (or (looking-at c-symbol-start)
  7428. (looking-at c-type-decl-prefix-key)))))
  7429. ;; In Pike a list of modifiers may be followed by a brace
  7430. ;; to make them apply to many identifiers. Note that the
  7431. ;; match data will be empty on return in this case.
  7432. ((and (c-major-mode-is 'pike-mode)
  7433. (progn
  7434. (goto-char open-brace)
  7435. (= (c-backward-token-2) 0))
  7436. (looking-at c-specifier-key)
  7437. ;; Use this variant to avoid yet another special regexp.
  7438. (c-keyword-member (c-keyword-sym (match-string 1))
  7439. 'c-modifier-kwds))
  7440. (setq kwd-start (point))
  7441. t)))
  7442. ;; Got a match.
  7443. (if goto-start
  7444. ;; Back up over any preceding specifiers and their clauses
  7445. ;; by going forward from `first-specifier-pos', which is the
  7446. ;; earliest possible position where the specifier list can
  7447. ;; start.
  7448. (progn
  7449. (goto-char first-specifier-pos)
  7450. (while (< (point) kwd-start)
  7451. (if (looking-at c-symbol-key)
  7452. ;; Accept any plain symbol token on the ground that
  7453. ;; it's a specifier masked through a macro (just
  7454. ;; like `c-forward-decl-or-cast-1' skip forward over
  7455. ;; such tokens).
  7456. ;;
  7457. ;; Could be more restrictive wrt invalid keywords,
  7458. ;; but that'd only occur in invalid code so there's
  7459. ;; no use spending effort on it.
  7460. (let ((end (match-end 0)))
  7461. (unless (c-forward-keyword-clause 0)
  7462. (goto-char end)
  7463. (c-forward-syntactic-ws)))
  7464. ;; Can't parse a declaration preamble and is still
  7465. ;; before `kwd-start'. That means `first-specifier-pos'
  7466. ;; was in some earlier construct. Search again.
  7467. (if (c-syntactic-re-search-forward c-symbol-start
  7468. kwd-start 'move t)
  7469. (goto-char (setq first-specifier-pos (match-beginning 0)))
  7470. ;; Got no preamble before the block declaration keyword.
  7471. (setq first-specifier-pos kwd-start))))
  7472. (goto-char first-specifier-pos))
  7473. (goto-char kwd-start))
  7474. kwd-start)))
  7475. (defun c-search-uplist-for-classkey (paren-state)
  7476. ;; Check if the closest containing paren sexp is a declaration
  7477. ;; block, returning a 2 element vector in that case. Aref 0
  7478. ;; contains the bufpos at boi of the class key line, and aref 1
  7479. ;; contains the bufpos of the open brace. This function is an
  7480. ;; obsolete wrapper for `c-looking-at-decl-block'.
  7481. ;;
  7482. ;; This function might do hidden buffer changes.
  7483. (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
  7484. (when open-paren-pos
  7485. (save-excursion
  7486. (goto-char open-paren-pos)
  7487. (when (and (eq (char-after) ?{)
  7488. (c-looking-at-decl-block
  7489. (c-safe-position open-paren-pos paren-state)
  7490. nil))
  7491. (back-to-indentation)
  7492. (vector (point) open-paren-pos))))))
  7493. (defmacro c-pull-open-brace (ps)
  7494. ;; Pull the next open brace from PS (which has the form of paren-state),
  7495. ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
  7496. `(progn
  7497. (while (consp (car ,ps))
  7498. (setq ,ps (cdr ,ps)))
  7499. (prog1 (car ,ps)
  7500. (setq ,ps (cdr ,ps)))))
  7501. (defun c-most-enclosing-decl-block (paren-state)
  7502. ;; Return the buffer position of the most enclosing decl-block brace (in the
  7503. ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
  7504. ;; none was found.
  7505. (let* ((open-brace (c-pull-open-brace paren-state))
  7506. (next-open-brace (c-pull-open-brace paren-state)))
  7507. (while (and open-brace
  7508. (save-excursion
  7509. (goto-char open-brace)
  7510. (not (c-looking-at-decl-block next-open-brace nil))))
  7511. (setq open-brace next-open-brace
  7512. next-open-brace (c-pull-open-brace paren-state)))
  7513. open-brace))
  7514. (defun c-cheap-inside-bracelist-p (paren-state)
  7515. ;; Return the position of the L-brace if point is inside a brace list
  7516. ;; initialization of an array, etc. This is an approximate function,
  7517. ;; designed for speed over accuracy. It will not find every bracelist, but
  7518. ;; a non-nil result is reliable. We simply search for "= {" (naturally with
  7519. ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
  7520. ;; is everywhere else.
  7521. (let (b-pos)
  7522. (save-excursion
  7523. (while
  7524. (and (setq b-pos (c-pull-open-brace paren-state))
  7525. (progn (goto-char b-pos)
  7526. (c-backward-sws)
  7527. (c-backward-token-2)
  7528. (not (looking-at "=")))))
  7529. b-pos)))
  7530. (defun c-inside-bracelist-p (containing-sexp paren-state)
  7531. ;; return the buffer position of the beginning of the brace list
  7532. ;; statement if we're inside a brace list, otherwise return nil.
  7533. ;; CONTAINING-SEXP is the buffer pos of the innermost containing
  7534. ;; paren. PAREN-STATE is the remainder of the state of enclosing
  7535. ;; braces
  7536. ;;
  7537. ;; N.B.: This algorithm can potentially get confused by cpp macros
  7538. ;; placed in inconvenient locations. It's a trade-off we make for
  7539. ;; speed.
  7540. ;;
  7541. ;; This function might do hidden buffer changes.
  7542. (or
  7543. ;; This will pick up brace list declarations.
  7544. (c-safe
  7545. (save-excursion
  7546. (goto-char containing-sexp)
  7547. (c-forward-sexp -1)
  7548. (let (bracepos)
  7549. (if (and (or (looking-at c-brace-list-key)
  7550. (progn (c-forward-sexp -1)
  7551. (looking-at c-brace-list-key)))
  7552. (setq bracepos (c-down-list-forward (point)))
  7553. (not (c-crosses-statement-barrier-p (point)
  7554. (- bracepos 2))))
  7555. (point)))))
  7556. ;; this will pick up array/aggregate init lists, even if they are nested.
  7557. (save-excursion
  7558. (let ((class-key
  7559. ;; Pike can have class definitions anywhere, so we must
  7560. ;; check for the class key here.
  7561. (and (c-major-mode-is 'pike-mode)
  7562. c-decl-block-key))
  7563. bufpos braceassignp lim next-containing)
  7564. (while (and (not bufpos)
  7565. containing-sexp)
  7566. (when paren-state
  7567. (if (consp (car paren-state))
  7568. (setq lim (cdr (car paren-state))
  7569. paren-state (cdr paren-state))
  7570. (setq lim (car paren-state)))
  7571. (when paren-state
  7572. (setq next-containing (car paren-state)
  7573. paren-state (cdr paren-state))))
  7574. (goto-char containing-sexp)
  7575. (if (c-looking-at-inexpr-block next-containing next-containing)
  7576. ;; We're in an in-expression block of some kind. Do not
  7577. ;; check nesting. We deliberately set the limit to the
  7578. ;; containing sexp, so that c-looking-at-inexpr-block
  7579. ;; doesn't check for an identifier before it.
  7580. (setq containing-sexp nil)
  7581. ;; see if the open brace is preceded by = or [...] in
  7582. ;; this statement, but watch out for operator=
  7583. (setq braceassignp 'dontknow)
  7584. (c-backward-token-2 1 t lim)
  7585. ;; Checks to do only on the first sexp before the brace.
  7586. (when (and c-opt-inexpr-brace-list-key
  7587. (eq (char-after) ?\[))
  7588. ;; In Java, an initialization brace list may follow
  7589. ;; directly after "new Foo[]", so check for a "new"
  7590. ;; earlier.
  7591. (while (eq braceassignp 'dontknow)
  7592. (setq braceassignp
  7593. (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
  7594. ((looking-at c-opt-inexpr-brace-list-key) t)
  7595. ((looking-at "\\sw\\|\\s_\\|[.[]")
  7596. ;; Carry on looking if this is an
  7597. ;; identifier (may contain "." in Java)
  7598. ;; or another "[]" sexp.
  7599. 'dontknow)
  7600. (t nil)))))
  7601. ;; Checks to do on all sexps before the brace, up to the
  7602. ;; beginning of the statement.
  7603. (while (eq braceassignp 'dontknow)
  7604. (cond ((eq (char-after) ?\;)
  7605. (setq braceassignp nil))
  7606. ((and class-key
  7607. (looking-at class-key))
  7608. (setq braceassignp nil))
  7609. ((eq (char-after) ?=)
  7610. ;; We've seen a =, but must check earlier tokens so
  7611. ;; that it isn't something that should be ignored.
  7612. (setq braceassignp 'maybe)
  7613. (while (and (eq braceassignp 'maybe)
  7614. (zerop (c-backward-token-2 1 t lim)))
  7615. (setq braceassignp
  7616. (cond
  7617. ;; Check for operator =
  7618. ((and c-opt-op-identifier-prefix
  7619. (looking-at c-opt-op-identifier-prefix))
  7620. nil)
  7621. ;; Check for `<opchar>= in Pike.
  7622. ((and (c-major-mode-is 'pike-mode)
  7623. (or (eq (char-after) ?`)
  7624. ;; Special case for Pikes
  7625. ;; `[]=, since '[' is not in
  7626. ;; the punctuation class.
  7627. (and (eq (char-after) ?\[)
  7628. (eq (char-before) ?`))))
  7629. nil)
  7630. ((looking-at "\\s.") 'maybe)
  7631. ;; make sure we're not in a C++ template
  7632. ;; argument assignment
  7633. ((and
  7634. (c-major-mode-is 'c++-mode)
  7635. (save-excursion
  7636. (let ((here (point))
  7637. (pos< (progn
  7638. (skip-chars-backward "^<>")
  7639. (point))))
  7640. (and (eq (char-before) ?<)
  7641. (not (c-crosses-statement-barrier-p
  7642. pos< here))
  7643. (not (c-in-literal))
  7644. ))))
  7645. nil)
  7646. (t t))))))
  7647. (if (and (eq braceassignp 'dontknow)
  7648. (/= (c-backward-token-2 1 t lim) 0))
  7649. (setq braceassignp nil)))
  7650. (if (not braceassignp)
  7651. (if (eq (char-after) ?\;)
  7652. ;; Brace lists can't contain a semicolon, so we're done.
  7653. (setq containing-sexp nil)
  7654. ;; Go up one level.
  7655. (setq containing-sexp next-containing
  7656. lim nil
  7657. next-containing nil))
  7658. ;; we've hit the beginning of the aggregate list
  7659. (c-beginning-of-statement-1
  7660. (c-most-enclosing-brace paren-state))
  7661. (setq bufpos (point))))
  7662. )
  7663. bufpos))
  7664. ))
  7665. (defun c-looking-at-special-brace-list (&optional lim)
  7666. ;; If we're looking at the start of a pike-style list, ie `({ })',
  7667. ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
  7668. ;; positions and its entry in c-special-brace-lists is returned, nil
  7669. ;; otherwise. The ending position is nil if the list is still open.
  7670. ;; LIM is the limit for forward search. The point may either be at
  7671. ;; the `(' or at the following paren character. Tries to check the
  7672. ;; matching closer, but assumes it's correct if no balanced paren is
  7673. ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
  7674. ;; a special brace list).
  7675. ;;
  7676. ;; This function might do hidden buffer changes.
  7677. (if c-special-brace-lists
  7678. (condition-case ()
  7679. (save-excursion
  7680. (let ((beg (point))
  7681. inner-beg end type)
  7682. (c-forward-syntactic-ws)
  7683. (if (eq (char-after) ?\()
  7684. (progn
  7685. (forward-char 1)
  7686. (c-forward-syntactic-ws)
  7687. (setq inner-beg (point))
  7688. (setq type (assq (char-after) c-special-brace-lists)))
  7689. (if (setq type (assq (char-after) c-special-brace-lists))
  7690. (progn
  7691. (setq inner-beg (point))
  7692. (c-backward-syntactic-ws)
  7693. (forward-char -1)
  7694. (setq beg (if (eq (char-after) ?\()
  7695. (point)
  7696. nil)))))
  7697. (if (and beg type)
  7698. (if (and (c-safe
  7699. (goto-char beg)
  7700. (c-forward-sexp 1)
  7701. (setq end (point))
  7702. (= (char-before) ?\)))
  7703. (c-safe
  7704. (goto-char inner-beg)
  7705. (if (looking-at "\\s(")
  7706. ;; Check balancing of the inner paren
  7707. ;; below.
  7708. (progn
  7709. (c-forward-sexp 1)
  7710. t)
  7711. ;; If the inner char isn't a paren then
  7712. ;; we can't check balancing, so just
  7713. ;; check the char before the outer
  7714. ;; closing paren.
  7715. (goto-char end)
  7716. (backward-char)
  7717. (c-backward-syntactic-ws)
  7718. (= (char-before) (cdr type)))))
  7719. (if (or (/= (char-syntax (char-before)) ?\))
  7720. (= (progn
  7721. (c-forward-syntactic-ws)
  7722. (point))
  7723. (1- end)))
  7724. (cons (cons beg end) type))
  7725. (cons (list beg) type)))))
  7726. (error nil))))
  7727. (defun c-looking-at-bos (&optional lim)
  7728. ;; Return non-nil if between two statements or declarations, assuming
  7729. ;; point is not inside a literal or comment.
  7730. ;;
  7731. ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
  7732. ;; are recommended instead.
  7733. ;;
  7734. ;; This function might do hidden buffer changes.
  7735. (c-at-statement-start-p))
  7736. (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
  7737. (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
  7738. ;; Return non-nil if we're looking at the beginning of a block
  7739. ;; inside an expression. The value returned is actually a cons of
  7740. ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
  7741. ;; position of the beginning of the construct.
  7742. ;;
  7743. ;; LIM limits the backward search. CONTAINING-SEXP is the start
  7744. ;; position of the closest containing list. If it's nil, the
  7745. ;; containing paren isn't used to decide whether we're inside an
  7746. ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
  7747. ;; needs to be farther back.
  7748. ;;
  7749. ;; If CHECK-AT-END is non-nil then extra checks at the end of the
  7750. ;; brace block might be done. It should only be used when the
  7751. ;; construct can be assumed to be complete, i.e. when the original
  7752. ;; starting position was further down than that.
  7753. ;;
  7754. ;; This function might do hidden buffer changes.
  7755. (save-excursion
  7756. (let ((res 'maybe) passed-paren
  7757. (closest-lim (or containing-sexp lim (point-min)))
  7758. ;; Look at the character after point only as a last resort
  7759. ;; when we can't disambiguate.
  7760. (block-follows (and (eq (char-after) ?{) (point))))
  7761. (while (and (eq res 'maybe)
  7762. (progn (c-backward-syntactic-ws)
  7763. (> (point) closest-lim))
  7764. (not (bobp))
  7765. (progn (backward-char)
  7766. (looking-at "[\]\).]\\|\\w\\|\\s_"))
  7767. (c-safe (forward-char)
  7768. (goto-char (scan-sexps (point) -1))))
  7769. (setq res
  7770. (if (looking-at c-keywords-regexp)
  7771. (let ((kw-sym (c-keyword-sym (match-string 1))))
  7772. (cond
  7773. ((and block-follows
  7774. (c-keyword-member kw-sym 'c-inexpr-class-kwds))
  7775. (and (not (eq passed-paren ?\[))
  7776. (or (not (looking-at c-class-key))
  7777. ;; If the class definition is at the start of
  7778. ;; a statement, we don't consider it an
  7779. ;; in-expression class.
  7780. (let ((prev (point)))
  7781. (while (and
  7782. (= (c-backward-token-2 1 nil closest-lim) 0)
  7783. (eq (char-syntax (char-after)) ?w))
  7784. (setq prev (point)))
  7785. (goto-char prev)
  7786. (not (c-at-statement-start-p)))
  7787. ;; Also, in Pike we treat it as an
  7788. ;; in-expression class if it's used in an
  7789. ;; object clone expression.
  7790. (save-excursion
  7791. (and check-at-end
  7792. (c-major-mode-is 'pike-mode)
  7793. (progn (goto-char block-follows)
  7794. (zerop (c-forward-token-2 1 t)))
  7795. (eq (char-after) ?\())))
  7796. (cons 'inexpr-class (point))))
  7797. ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
  7798. (when (not passed-paren)
  7799. (cons 'inexpr-statement (point))))
  7800. ((c-keyword-member kw-sym 'c-lambda-kwds)
  7801. (when (or (not passed-paren)
  7802. (eq passed-paren ?\())
  7803. (cons 'inlambda (point))))
  7804. ((c-keyword-member kw-sym 'c-block-stmt-kwds)
  7805. nil)
  7806. (t
  7807. 'maybe)))
  7808. (if (looking-at "\\s(")
  7809. (if passed-paren
  7810. (if (and (eq passed-paren ?\[)
  7811. (eq (char-after) ?\[))
  7812. ;; Accept several square bracket sexps for
  7813. ;; Java array initializations.
  7814. 'maybe)
  7815. (setq passed-paren (char-after))
  7816. 'maybe)
  7817. 'maybe))))
  7818. (if (eq res 'maybe)
  7819. (when (and c-recognize-paren-inexpr-blocks
  7820. block-follows
  7821. containing-sexp
  7822. (eq (char-after containing-sexp) ?\())
  7823. (goto-char containing-sexp)
  7824. (if (or (save-excursion
  7825. (c-backward-syntactic-ws lim)
  7826. (and (> (point) (or lim (point-min)))
  7827. (c-on-identifier)))
  7828. (and c-special-brace-lists
  7829. (c-looking-at-special-brace-list)))
  7830. nil
  7831. (cons 'inexpr-statement (point))))
  7832. res))))
  7833. (defun c-looking-at-inexpr-block-backward (paren-state)
  7834. ;; Returns non-nil if we're looking at the end of an in-expression
  7835. ;; block, otherwise the same as `c-looking-at-inexpr-block'.
  7836. ;; PAREN-STATE is the paren state relevant at the current position.
  7837. ;;
  7838. ;; This function might do hidden buffer changes.
  7839. (save-excursion
  7840. ;; We currently only recognize a block.
  7841. (let ((here (point))
  7842. (elem (car-safe paren-state))
  7843. containing-sexp)
  7844. (when (and (consp elem)
  7845. (progn (goto-char (cdr elem))
  7846. (c-forward-syntactic-ws here)
  7847. (= (point) here)))
  7848. (goto-char (car elem))
  7849. (if (setq paren-state (cdr paren-state))
  7850. (setq containing-sexp (car-safe paren-state)))
  7851. (c-looking-at-inexpr-block (c-safe-position containing-sexp
  7852. paren-state)
  7853. containing-sexp)))))
  7854. (defun c-at-macro-vsemi-p (&optional pos)
  7855. ;; Is there a "virtual semicolon" at POS or point?
  7856. ;; (See cc-defs.el for full details of "virtual semicolons".)
  7857. ;;
  7858. ;; This is true when point is at the last non syntactic WS position on the
  7859. ;; line, there is a macro call last on the line, and this particular macro's
  7860. ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
  7861. ;; semicolon.
  7862. (save-excursion
  7863. (save-restriction
  7864. (widen)
  7865. (if pos
  7866. (goto-char pos)
  7867. (setq pos (point)))
  7868. (and
  7869. c-macro-with-semi-re
  7870. (eq (skip-chars-backward " \t") 0)
  7871. ;; Check we've got nothing after this except comments and empty lines
  7872. ;; joined by escaped EOLs.
  7873. (skip-chars-forward " \t") ; always returns non-nil.
  7874. (progn
  7875. (while ; go over 1 block comment per iteration.
  7876. (and
  7877. (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
  7878. (goto-char (match-end 0))
  7879. (cond
  7880. ((looking-at c-block-comment-start-regexp)
  7881. (and (forward-comment 1)
  7882. (skip-chars-forward " \t"))) ; always returns non-nil
  7883. ((looking-at c-line-comment-start-regexp)
  7884. (end-of-line)
  7885. nil)
  7886. (t nil))))
  7887. (eolp))
  7888. (goto-char pos)
  7889. (progn (c-backward-syntactic-ws)
  7890. (eq (point) pos))
  7891. ;; Check for one of the listed macros being before point.
  7892. (or (not (eq (char-before) ?\)))
  7893. (when (c-go-list-backward)
  7894. (c-backward-syntactic-ws)
  7895. t))
  7896. (c-simple-skip-symbol-backward)
  7897. (looking-at c-macro-with-semi-re)
  7898. (goto-char pos)
  7899. (not (c-in-literal)))))) ; The most expensive check last.
  7900. (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
  7901. ;; `c-guess-basic-syntax' and the functions that precedes it below
  7902. ;; implements the main decision tree for determining the syntactic
  7903. ;; analysis of the current line of code.
  7904. ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
  7905. ;; auto newline analysis.
  7906. (defvar c-auto-newline-analysis nil)
  7907. (defun c-brace-anchor-point (bracepos)
  7908. ;; BRACEPOS is the position of a brace in a construct like "namespace
  7909. ;; Bar {". Return the anchor point in this construct; this is the
  7910. ;; earliest symbol on the brace's line which isn't earlier than
  7911. ;; "namespace".
  7912. ;;
  7913. ;; Currently (2007-08-17), "like namespace" means "matches
  7914. ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
  7915. ;; or anything like that.
  7916. (save-excursion
  7917. (let ((boi (c-point 'boi bracepos)))
  7918. (goto-char bracepos)
  7919. (while (and (> (point) boi)
  7920. (not (looking-at c-other-decl-block-key)))
  7921. (c-backward-token-2))
  7922. (if (> (point) boi) (point) boi))))
  7923. (defsubst c-add-syntax (symbol &rest args)
  7924. ;; A simple function to prepend a new syntax element to
  7925. ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
  7926. ;; should always be dynamically bound but since we read it first
  7927. ;; we'll fail properly anyway if this function is misused.
  7928. (setq c-syntactic-context (cons (cons symbol args)
  7929. c-syntactic-context)))
  7930. (defsubst c-append-syntax (symbol &rest args)
  7931. ;; Like `c-add-syntax' but appends to the end of the syntax list.
  7932. ;; (Normally not necessary.)
  7933. (setq c-syntactic-context (nconc c-syntactic-context
  7934. (list (cons symbol args)))))
  7935. (defun c-add-stmt-syntax (syntax-symbol
  7936. syntax-extra-args
  7937. stop-at-boi-only
  7938. containing-sexp
  7939. paren-state)
  7940. ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
  7941. ;; needed with further syntax elements of the types `substatement',
  7942. ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
  7943. ;; `defun-block-intro'.
  7944. ;;
  7945. ;; Do the generic processing to anchor the given syntax symbol on
  7946. ;; the preceding statement: Skip over any labels and containing
  7947. ;; statements on the same line, and then search backward until we
  7948. ;; find a statement or block start that begins at boi without a
  7949. ;; label or comment.
  7950. ;;
  7951. ;; Point is assumed to be at the prospective anchor point for the
  7952. ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
  7953. ;; skip past open parens and containing statements. Most of the added
  7954. ;; syntax elements will get the same anchor point - the exception is
  7955. ;; for an anchor in a construct like "namespace"[*] - this is as early
  7956. ;; as possible in the construct but on the same line as the {.
  7957. ;;
  7958. ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
  7959. ;;
  7960. ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
  7961. ;; syntax symbol. They are appended after the anchor point.
  7962. ;;
  7963. ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
  7964. ;; if the current statement starts there.
  7965. ;;
  7966. ;; Note: It's not a problem if PAREN-STATE "overshoots"
  7967. ;; CONTAINING-SEXP, i.e. contains info about parens further down.
  7968. ;;
  7969. ;; This function might do hidden buffer changes.
  7970. (if (= (point) (c-point 'boi))
  7971. ;; This is by far the most common case, so let's give it special
  7972. ;; treatment.
  7973. (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
  7974. (let ((syntax-last c-syntactic-context)
  7975. (boi (c-point 'boi))
  7976. ;; Set when we're on a label, so that we don't stop there.
  7977. ;; FIXME: To be complete we should check if we're on a label
  7978. ;; now at the start.
  7979. on-label)
  7980. ;; Use point as the anchor point for "namespace", "extern", etc.
  7981. (apply 'c-add-syntax syntax-symbol
  7982. (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
  7983. (point) nil)
  7984. syntax-extra-args)
  7985. ;; Loop while we have to back out of containing blocks.
  7986. (while
  7987. (and
  7988. (catch 'back-up-block
  7989. ;; Loop while we have to back up statements.
  7990. (while (or (/= (point) boi)
  7991. on-label
  7992. (looking-at c-comment-start-regexp))
  7993. ;; Skip past any comments that stands between the
  7994. ;; statement start and boi.
  7995. (let ((savepos (point)))
  7996. (while (and (/= savepos boi)
  7997. (c-backward-single-comment))
  7998. (setq savepos (point)
  7999. boi (c-point 'boi)))
  8000. (goto-char savepos))
  8001. ;; Skip to the beginning of this statement or backward
  8002. ;; another one.
  8003. (let ((old-pos (point))
  8004. (old-boi boi)
  8005. (step-type (c-beginning-of-statement-1 containing-sexp)))
  8006. (setq boi (c-point 'boi)
  8007. on-label (eq step-type 'label))
  8008. (cond ((= (point) old-pos)
  8009. ;; If we didn't move we're at the start of a block and
  8010. ;; have to continue outside it.
  8011. (throw 'back-up-block t))
  8012. ((and (eq step-type 'up)
  8013. (>= (point) old-boi)
  8014. (looking-at "else\\>[^_]")
  8015. (save-excursion
  8016. (goto-char old-pos)
  8017. (looking-at "if\\>[^_]")))
  8018. ;; Special case to avoid deeper and deeper indentation
  8019. ;; of "else if" clauses.
  8020. )
  8021. ((and (not stop-at-boi-only)
  8022. (/= old-pos old-boi)
  8023. (memq step-type '(up previous)))
  8024. ;; If stop-at-boi-only is nil, we shouldn't back up
  8025. ;; over previous or containing statements to try to
  8026. ;; reach boi, so go back to the last position and
  8027. ;; exit.
  8028. (goto-char old-pos)
  8029. (throw 'back-up-block nil))
  8030. (t
  8031. (if (and (not stop-at-boi-only)
  8032. (memq step-type '(up previous beginning)))
  8033. ;; If we've moved into another statement then we
  8034. ;; should no longer try to stop in the middle of a
  8035. ;; line.
  8036. (setq stop-at-boi-only t))
  8037. ;; Record this as a substatement if we skipped up one
  8038. ;; level.
  8039. (when (eq step-type 'up)
  8040. (c-add-syntax 'substatement nil))))
  8041. )))
  8042. containing-sexp)
  8043. ;; Now we have to go out of this block.
  8044. (goto-char containing-sexp)
  8045. ;; Don't stop in the middle of a special brace list opener
  8046. ;; like "({".
  8047. (when c-special-brace-lists
  8048. (let ((special-list (c-looking-at-special-brace-list)))
  8049. (when (and special-list
  8050. (< (car (car special-list)) (point)))
  8051. (setq containing-sexp (car (car special-list)))
  8052. (goto-char containing-sexp))))
  8053. (setq paren-state (c-whack-state-after containing-sexp paren-state)
  8054. containing-sexp (c-most-enclosing-brace paren-state)
  8055. boi (c-point 'boi))
  8056. ;; Analyze the construct in front of the block we've stepped out
  8057. ;; from and add the right syntactic element for it.
  8058. (let ((paren-pos (point))
  8059. (paren-char (char-after))
  8060. step-type)
  8061. (if (eq paren-char ?\()
  8062. ;; Stepped out of a parenthesis block, so we're in an
  8063. ;; expression now.
  8064. (progn
  8065. (when (/= paren-pos boi)
  8066. (if (and c-recognize-paren-inexpr-blocks
  8067. (progn
  8068. (c-backward-syntactic-ws containing-sexp)
  8069. (or (not (looking-at "\\>"))
  8070. (not (c-on-identifier))))
  8071. (save-excursion
  8072. (goto-char (1+ paren-pos))
  8073. (c-forward-syntactic-ws)
  8074. (eq (char-after) ?{)))
  8075. ;; Stepped out of an in-expression statement. This
  8076. ;; syntactic element won't get an anchor pos.
  8077. (c-add-syntax 'inexpr-statement)
  8078. ;; A parenthesis normally belongs to an arglist.
  8079. (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
  8080. (goto-char (max boi
  8081. (if containing-sexp
  8082. (1+ containing-sexp)
  8083. (point-min))))
  8084. (setq step-type 'same
  8085. on-label nil))
  8086. ;; Stepped out of a brace block.
  8087. (setq step-type (c-beginning-of-statement-1 containing-sexp)
  8088. on-label (eq step-type 'label))
  8089. (if (and (eq step-type 'same)
  8090. (/= paren-pos (point)))
  8091. (let (inexpr)
  8092. (cond
  8093. ((save-excursion
  8094. (goto-char paren-pos)
  8095. (setq inexpr (c-looking-at-inexpr-block
  8096. (c-safe-position containing-sexp paren-state)
  8097. containing-sexp)))
  8098. (c-add-syntax (if (eq (car inexpr) 'inlambda)
  8099. 'defun-block-intro
  8100. 'statement-block-intro)
  8101. nil))
  8102. ((looking-at c-other-decl-block-key)
  8103. (c-add-syntax
  8104. (cdr (assoc (match-string 1)
  8105. c-other-decl-block-key-in-symbols-alist))
  8106. (max (c-point 'boi paren-pos) (point))))
  8107. (t (c-add-syntax 'defun-block-intro nil))))
  8108. (c-add-syntax 'statement-block-intro nil)))
  8109. (if (= paren-pos boi)
  8110. ;; Always done if the open brace was at boi. The
  8111. ;; c-beginning-of-statement-1 call above is necessary
  8112. ;; anyway, to decide the type of block-intro to add.
  8113. (goto-char paren-pos)
  8114. (setq boi (c-point 'boi)))
  8115. ))
  8116. ;; Fill in the current point as the anchor for all the symbols
  8117. ;; added above.
  8118. (let ((p c-syntactic-context) q)
  8119. (while (not (eq p syntax-last))
  8120. (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
  8121. (while q
  8122. (unless (car q)
  8123. (setcar q (point)))
  8124. (setq q (cdr q)))
  8125. (setq p (cdr p))))
  8126. )))
  8127. (defun c-add-class-syntax (symbol
  8128. containing-decl-open
  8129. containing-decl-start
  8130. containing-decl-kwd
  8131. paren-state)
  8132. ;; The inclass and class-close syntactic symbols are added in
  8133. ;; several places and some work is needed to fix everything.
  8134. ;; Therefore it's collected here.
  8135. ;;
  8136. ;; This function might do hidden buffer changes.
  8137. (goto-char containing-decl-open)
  8138. (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
  8139. (progn
  8140. (c-add-syntax symbol containing-decl-open)
  8141. containing-decl-open)
  8142. (goto-char containing-decl-start)
  8143. ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
  8144. ;; here, but we have to do like this for compatibility.
  8145. (back-to-indentation)
  8146. (c-add-syntax symbol (point))
  8147. (if (and (c-keyword-member containing-decl-kwd
  8148. 'c-inexpr-class-kwds)
  8149. (/= containing-decl-start (c-point 'boi containing-decl-start)))
  8150. (c-add-syntax 'inexpr-class))
  8151. (point)))
  8152. (defun c-guess-continued-construct (indent-point
  8153. char-after-ip
  8154. beg-of-same-or-containing-stmt
  8155. containing-sexp
  8156. paren-state)
  8157. ;; This function contains the decision tree reached through both
  8158. ;; cases 18 and 10. It's a continued statement or top level
  8159. ;; construct of some kind.
  8160. ;;
  8161. ;; This function might do hidden buffer changes.
  8162. (let (special-brace-list placeholder)
  8163. (goto-char indent-point)
  8164. (skip-chars-forward " \t")
  8165. (cond
  8166. ;; (CASE A removed.)
  8167. ;; CASE B: open braces for class or brace-lists
  8168. ((setq special-brace-list
  8169. (or (and c-special-brace-lists
  8170. (c-looking-at-special-brace-list))
  8171. (eq char-after-ip ?{)))
  8172. (cond
  8173. ;; CASE B.1: class-open
  8174. ((save-excursion
  8175. (and (eq (char-after) ?{)
  8176. (c-looking-at-decl-block containing-sexp t)
  8177. (setq beg-of-same-or-containing-stmt (point))))
  8178. (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
  8179. ;; CASE B.2: brace-list-open
  8180. ((or (consp special-brace-list)
  8181. (save-excursion
  8182. (goto-char beg-of-same-or-containing-stmt)
  8183. (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
  8184. indent-point t t t)))
  8185. ;; The most semantically accurate symbol here is
  8186. ;; brace-list-open, but we normally report it simply as a
  8187. ;; statement-cont. The reason is that one normally adjusts
  8188. ;; brace-list-open for brace lists as top-level constructs,
  8189. ;; and brace lists inside statements is a completely different
  8190. ;; context. C.f. case 5A.3.
  8191. (c-beginning-of-statement-1 containing-sexp)
  8192. (c-add-stmt-syntax (if c-auto-newline-analysis
  8193. ;; Turn off the dwim above when we're
  8194. ;; analyzing the nature of the brace
  8195. ;; for the auto newline feature.
  8196. 'brace-list-open
  8197. 'statement-cont)
  8198. nil nil
  8199. containing-sexp paren-state))
  8200. ;; CASE B.3: The body of a function declared inside a normal
  8201. ;; block. Can occur e.g. in Pike and when using gcc
  8202. ;; extensions, but watch out for macros followed by blocks.
  8203. ;; C.f. cases E, 16F and 17G.
  8204. ((and (not (c-at-statement-start-p))
  8205. (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
  8206. 'same)
  8207. (save-excursion
  8208. (let ((c-recognize-typeless-decls nil))
  8209. ;; Turn off recognition of constructs that lacks a
  8210. ;; type in this case, since that's more likely to be
  8211. ;; a macro followed by a block.
  8212. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  8213. (c-add-stmt-syntax 'defun-open nil t
  8214. containing-sexp paren-state))
  8215. ;; CASE B.4: Continued statement with block open. The most
  8216. ;; accurate analysis is perhaps `statement-cont' together with
  8217. ;; `block-open' but we play DWIM and use `substatement-open'
  8218. ;; instead. The rationale is that this typically is a macro
  8219. ;; followed by a block which makes it very similar to a
  8220. ;; statement with a substatement block.
  8221. (t
  8222. (c-add-stmt-syntax 'substatement-open nil nil
  8223. containing-sexp paren-state))
  8224. ))
  8225. ;; CASE C: iostream insertion or extraction operator
  8226. ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
  8227. (save-excursion
  8228. (goto-char beg-of-same-or-containing-stmt)
  8229. ;; If there is no preceding streamop in the statement
  8230. ;; then indent this line as a normal statement-cont.
  8231. (when (c-syntactic-re-search-forward
  8232. "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
  8233. (c-add-syntax 'stream-op (c-point 'boi))
  8234. t))))
  8235. ;; CASE E: In the "K&R region" of a function declared inside a
  8236. ;; normal block. C.f. case B.3.
  8237. ((and (save-excursion
  8238. ;; Check that the next token is a '{'. This works as
  8239. ;; long as no language that allows nested function
  8240. ;; definitions allows stuff like member init lists, K&R
  8241. ;; declarations or throws clauses there.
  8242. ;;
  8243. ;; Note that we do a forward search for something ahead
  8244. ;; of the indentation line here. That's not good since
  8245. ;; the user might not have typed it yet. Unfortunately
  8246. ;; it's exceedingly tricky to recognize a function
  8247. ;; prototype in a code block without resorting to this.
  8248. (c-forward-syntactic-ws)
  8249. (eq (char-after) ?{))
  8250. (not (c-at-statement-start-p))
  8251. (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
  8252. 'same)
  8253. (save-excursion
  8254. (let ((c-recognize-typeless-decls nil))
  8255. ;; Turn off recognition of constructs that lacks a
  8256. ;; type in this case, since that's more likely to be
  8257. ;; a macro followed by a block.
  8258. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  8259. (c-add-stmt-syntax 'func-decl-cont nil t
  8260. containing-sexp paren-state))
  8261. ;;CASE F: continued statement and the only preceding items are
  8262. ;;annotations.
  8263. ((and (c-major-mode-is 'java-mode)
  8264. (setq placeholder (point))
  8265. (c-beginning-of-statement-1)
  8266. (progn
  8267. (while (and (c-forward-annotation)
  8268. (< (point) placeholder))
  8269. (c-forward-syntactic-ws))
  8270. t)
  8271. (prog1
  8272. (>= (point) placeholder)
  8273. (goto-char placeholder)))
  8274. (c-beginning-of-statement-1 containing-sexp)
  8275. (c-add-syntax 'annotation-var-cont (point)))
  8276. ;; CASE G: a template list continuation?
  8277. ;; Mostly a duplication of case 5D.3 to fix templates-19:
  8278. ((and (c-major-mode-is 'c++-mode)
  8279. (save-excursion
  8280. (goto-char indent-point)
  8281. (c-with-syntax-table c++-template-syntax-table
  8282. (setq placeholder (c-up-list-backward)))
  8283. (and placeholder
  8284. (eq (char-after placeholder) ?<)
  8285. (/= (char-before placeholder) ?<)
  8286. (progn
  8287. (goto-char (1+ placeholder))
  8288. (not (looking-at c-<-op-cont-regexp))))))
  8289. (c-with-syntax-table c++-template-syntax-table
  8290. (goto-char placeholder)
  8291. (c-beginning-of-statement-1 containing-sexp t)
  8292. (if (save-excursion
  8293. (c-backward-syntactic-ws containing-sexp)
  8294. (eq (char-before) ?<))
  8295. ;; In a nested template arglist.
  8296. (progn
  8297. (goto-char placeholder)
  8298. (c-syntactic-skip-backward "^,;" containing-sexp t)
  8299. (c-forward-syntactic-ws))
  8300. (back-to-indentation)))
  8301. ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
  8302. ;; template aware.
  8303. (c-add-syntax 'template-args-cont (point) placeholder))
  8304. ;; CASE D: continued statement.
  8305. (t
  8306. (c-beginning-of-statement-1 containing-sexp)
  8307. (c-add-stmt-syntax 'statement-cont nil nil
  8308. containing-sexp paren-state))
  8309. )))
  8310. ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
  8311. ;; 2005/11/29).
  8312. ;;;###autoload
  8313. (defun c-guess-basic-syntax ()
  8314. "Return the syntactic context of the current line."
  8315. (save-excursion
  8316. (beginning-of-line)
  8317. (c-save-buffer-state
  8318. ((indent-point (point))
  8319. (case-fold-search nil)
  8320. ;; A whole ugly bunch of various temporary variables. Have
  8321. ;; to declare them here since it's not possible to declare
  8322. ;; a variable with only the scope of a cond test and the
  8323. ;; following result clauses, and most of this function is a
  8324. ;; single gigantic cond. :P
  8325. literal char-before-ip before-ws-ip char-after-ip macro-start
  8326. in-macro-expr c-syntactic-context placeholder c-in-literal-cache
  8327. step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
  8328. containing-<
  8329. ;; The following record some positions for the containing
  8330. ;; declaration block if we're directly within one:
  8331. ;; `containing-decl-open' is the position of the open
  8332. ;; brace. `containing-decl-start' is the start of the
  8333. ;; declaration. `containing-decl-kwd' is the keyword
  8334. ;; symbol of the keyword that tells what kind of block it
  8335. ;; is.
  8336. containing-decl-open
  8337. containing-decl-start
  8338. containing-decl-kwd
  8339. ;; The open paren of the closest surrounding sexp or nil if
  8340. ;; there is none.
  8341. containing-sexp
  8342. ;; The position after the closest preceding brace sexp
  8343. ;; (nested sexps are ignored), or the position after
  8344. ;; `containing-sexp' if there is none, or (point-min) if
  8345. ;; `containing-sexp' is nil.
  8346. lim
  8347. ;; The paren state outside `containing-sexp', or at
  8348. ;; `indent-point' if `containing-sexp' is nil.
  8349. (paren-state (c-parse-state))
  8350. ;; There's always at most one syntactic element which got
  8351. ;; an anchor pos. It's stored in syntactic-relpos.
  8352. syntactic-relpos
  8353. (c-stmt-delim-chars c-stmt-delim-chars))
  8354. ;; Check if we're directly inside an enclosing declaration
  8355. ;; level block.
  8356. (when (and (setq containing-sexp
  8357. (c-most-enclosing-brace paren-state))
  8358. (progn
  8359. (goto-char containing-sexp)
  8360. (eq (char-after) ?{))
  8361. (setq placeholder
  8362. (c-looking-at-decl-block
  8363. (c-most-enclosing-brace paren-state
  8364. containing-sexp)
  8365. t)))
  8366. (setq containing-decl-open containing-sexp
  8367. containing-decl-start (point)
  8368. containing-sexp nil)
  8369. (goto-char placeholder)
  8370. (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
  8371. (c-keyword-sym (match-string 1)))))
  8372. ;; Init some position variables.
  8373. (if c-state-cache
  8374. (progn
  8375. (setq containing-sexp (car paren-state)
  8376. paren-state (cdr paren-state))
  8377. (if (consp containing-sexp)
  8378. (progn
  8379. (setq lim (cdr containing-sexp))
  8380. (if (cdr c-state-cache)
  8381. ;; Ignore balanced paren. The next entry
  8382. ;; can't be another one.
  8383. (setq containing-sexp (car (cdr c-state-cache))
  8384. paren-state (cdr paren-state))
  8385. ;; If there is no surrounding open paren then
  8386. ;; put the last balanced pair back on paren-state.
  8387. (setq paren-state (cons containing-sexp paren-state)
  8388. containing-sexp nil)))
  8389. (setq lim (1+ containing-sexp))))
  8390. (setq lim (point-min)))
  8391. (when (c-beginning-of-macro)
  8392. (goto-char indent-point)
  8393. (let ((lim1 (c-determine-limit 2000)))
  8394. (setq lim (max lim lim1))))
  8395. ;; If we're in a parenthesis list then ',' delimits the
  8396. ;; "statements" rather than being an operator (with the
  8397. ;; exception of the "for" clause). This difference is
  8398. ;; typically only noticeable when statements are used in macro
  8399. ;; arglists.
  8400. (when (and containing-sexp
  8401. (eq (char-after containing-sexp) ?\())
  8402. (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
  8403. ;; cache char before and after indent point, and move point to
  8404. ;; the most likely position to perform the majority of tests
  8405. (goto-char indent-point)
  8406. (c-backward-syntactic-ws lim)
  8407. (setq before-ws-ip (point)
  8408. char-before-ip (char-before))
  8409. (goto-char indent-point)
  8410. (skip-chars-forward " \t")
  8411. (setq char-after-ip (char-after))
  8412. ;; are we in a literal?
  8413. (setq literal (c-in-literal lim))
  8414. ;; now figure out syntactic qualities of the current line
  8415. (cond
  8416. ;; CASE 1: in a string.
  8417. ((eq literal 'string)
  8418. (c-add-syntax 'string (c-point 'bopl)))
  8419. ;; CASE 2: in a C or C++ style comment.
  8420. ((and (memq literal '(c c++))
  8421. ;; This is a kludge for XEmacs where we use
  8422. ;; `buffer-syntactic-context', which doesn't correctly
  8423. ;; recognize "\*/" to end a block comment.
  8424. ;; `parse-partial-sexp' which is used by
  8425. ;; `c-literal-limits' will however do that in most
  8426. ;; versions, which results in that we get nil from
  8427. ;; `c-literal-limits' even when `c-in-literal' claims
  8428. ;; we're inside a comment.
  8429. (setq placeholder (c-literal-limits lim)))
  8430. (c-add-syntax literal (car placeholder)))
  8431. ;; CASE 3: in a cpp preprocessor macro continuation.
  8432. ((and (save-excursion
  8433. (when (c-beginning-of-macro)
  8434. (setq macro-start (point))))
  8435. (/= macro-start (c-point 'boi))
  8436. (progn
  8437. (setq tmpsymbol 'cpp-macro-cont)
  8438. (or (not c-syntactic-indentation-in-macros)
  8439. (save-excursion
  8440. (goto-char macro-start)
  8441. ;; If at the beginning of the body of a #define
  8442. ;; directive then analyze as cpp-define-intro
  8443. ;; only. Go on with the syntactic analysis
  8444. ;; otherwise. in-macro-expr is set if we're in a
  8445. ;; cpp expression, i.e. before the #define body
  8446. ;; or anywhere in a non-#define directive.
  8447. (if (c-forward-to-cpp-define-body)
  8448. (let ((indent-boi (c-point 'boi indent-point)))
  8449. (setq in-macro-expr (> (point) indent-boi)
  8450. tmpsymbol 'cpp-define-intro)
  8451. (= (point) indent-boi))
  8452. (setq in-macro-expr t)
  8453. nil)))))
  8454. (c-add-syntax tmpsymbol macro-start)
  8455. (setq macro-start nil))
  8456. ;; CASE 11: an else clause?
  8457. ((looking-at "else\\>[^_]")
  8458. (c-beginning-of-statement-1 containing-sexp)
  8459. (c-add-stmt-syntax 'else-clause nil t
  8460. containing-sexp paren-state))
  8461. ;; CASE 12: while closure of a do/while construct?
  8462. ((and (looking-at "while\\>[^_]")
  8463. (save-excursion
  8464. (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
  8465. 'beginning)
  8466. (setq placeholder (point)))))
  8467. (goto-char placeholder)
  8468. (c-add-stmt-syntax 'do-while-closure nil t
  8469. containing-sexp paren-state))
  8470. ;; CASE 13: A catch or finally clause? This case is simpler
  8471. ;; than if-else and do-while, because a block is required
  8472. ;; after every try, catch and finally.
  8473. ((save-excursion
  8474. (and (cond ((c-major-mode-is 'c++-mode)
  8475. (looking-at "catch\\>[^_]"))
  8476. ((c-major-mode-is 'java-mode)
  8477. (looking-at "\\(catch\\|finally\\)\\>[^_]")))
  8478. (and (c-safe (c-backward-syntactic-ws)
  8479. (c-backward-sexp)
  8480. t)
  8481. (eq (char-after) ?{)
  8482. (c-safe (c-backward-syntactic-ws)
  8483. (c-backward-sexp)
  8484. t)
  8485. (if (eq (char-after) ?\()
  8486. (c-safe (c-backward-sexp) t)
  8487. t))
  8488. (looking-at "\\(try\\|catch\\)\\>[^_]")
  8489. (setq placeholder (point))))
  8490. (goto-char placeholder)
  8491. (c-add-stmt-syntax 'catch-clause nil t
  8492. containing-sexp paren-state))
  8493. ;; CASE 18: A substatement we can recognize by keyword.
  8494. ((save-excursion
  8495. (and c-opt-block-stmt-key
  8496. (not (eq char-before-ip ?\;))
  8497. (not (c-at-vsemi-p before-ws-ip))
  8498. (not (memq char-after-ip '(?\) ?\] ?,)))
  8499. (or (not (eq char-before-ip ?}))
  8500. (c-looking-at-inexpr-block-backward c-state-cache))
  8501. (> (point)
  8502. (progn
  8503. ;; Ought to cache the result from the
  8504. ;; c-beginning-of-statement-1 calls here.
  8505. (setq placeholder (point))
  8506. (while (eq (setq step-type
  8507. (c-beginning-of-statement-1 lim))
  8508. 'label))
  8509. (if (eq step-type 'previous)
  8510. (goto-char placeholder)
  8511. (setq placeholder (point))
  8512. (if (and (eq step-type 'same)
  8513. (not (looking-at c-opt-block-stmt-key)))
  8514. ;; Step up to the containing statement if we
  8515. ;; stayed in the same one.
  8516. (let (step)
  8517. (while (eq
  8518. (setq step
  8519. (c-beginning-of-statement-1 lim))
  8520. 'label))
  8521. (if (eq step 'up)
  8522. (setq placeholder (point))
  8523. ;; There was no containing statement after all.
  8524. (goto-char placeholder)))))
  8525. placeholder))
  8526. (if (looking-at c-block-stmt-2-key)
  8527. ;; Require a parenthesis after these keywords.
  8528. ;; Necessary to catch e.g. synchronized in Java,
  8529. ;; which can be used both as statement and
  8530. ;; modifier.
  8531. (and (zerop (c-forward-token-2 1 nil))
  8532. (eq (char-after) ?\())
  8533. (looking-at c-opt-block-stmt-key))))
  8534. (if (eq step-type 'up)
  8535. ;; CASE 18A: Simple substatement.
  8536. (progn
  8537. (goto-char placeholder)
  8538. (cond
  8539. ((eq char-after-ip ?{)
  8540. (c-add-stmt-syntax 'substatement-open nil nil
  8541. containing-sexp paren-state))
  8542. ((save-excursion
  8543. (goto-char indent-point)
  8544. (back-to-indentation)
  8545. (c-forward-label))
  8546. (c-add-stmt-syntax 'substatement-label nil nil
  8547. containing-sexp paren-state))
  8548. (t
  8549. (c-add-stmt-syntax 'substatement nil nil
  8550. containing-sexp paren-state))))
  8551. ;; CASE 18B: Some other substatement. This is shared
  8552. ;; with case 10.
  8553. (c-guess-continued-construct indent-point
  8554. char-after-ip
  8555. placeholder
  8556. lim
  8557. paren-state)))
  8558. ;; CASE 14: A case or default label
  8559. ((looking-at c-label-kwds-regexp)
  8560. (if containing-sexp
  8561. (progn
  8562. (goto-char containing-sexp)
  8563. (setq lim (c-most-enclosing-brace c-state-cache
  8564. containing-sexp))
  8565. (c-backward-to-block-anchor lim)
  8566. (c-add-stmt-syntax 'case-label nil t lim paren-state))
  8567. ;; Got a bogus label at the top level. In lack of better
  8568. ;; alternatives, anchor it on (point-min).
  8569. (c-add-syntax 'case-label (point-min))))
  8570. ;; CASE 15: any other label
  8571. ((save-excursion
  8572. (back-to-indentation)
  8573. (and (not (looking-at c-syntactic-ws-start))
  8574. (c-forward-label)))
  8575. (cond (containing-decl-open
  8576. (setq placeholder (c-add-class-syntax 'inclass
  8577. containing-decl-open
  8578. containing-decl-start
  8579. containing-decl-kwd
  8580. paren-state))
  8581. ;; Append access-label with the same anchor point as
  8582. ;; inclass gets.
  8583. (c-append-syntax 'access-label placeholder))
  8584. (containing-sexp
  8585. (goto-char containing-sexp)
  8586. (setq lim (c-most-enclosing-brace c-state-cache
  8587. containing-sexp))
  8588. (save-excursion
  8589. (setq tmpsymbol
  8590. (if (and (eq (c-beginning-of-statement-1 lim) 'up)
  8591. (looking-at "switch\\>[^_]"))
  8592. ;; If the surrounding statement is a switch then
  8593. ;; let's analyze all labels as switch labels, so
  8594. ;; that they get lined up consistently.
  8595. 'case-label
  8596. 'label)))
  8597. (c-backward-to-block-anchor lim)
  8598. (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
  8599. (t
  8600. ;; A label on the top level. Treat it as a class
  8601. ;; context. (point-min) is the closest we get to the
  8602. ;; class open brace.
  8603. (c-add-syntax 'access-label (point-min)))))
  8604. ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
  8605. ;; 17E.
  8606. ((setq placeholder (c-looking-at-inexpr-block
  8607. (c-safe-position containing-sexp paren-state)
  8608. containing-sexp
  8609. ;; Have to turn on the heuristics after
  8610. ;; the point even though it doesn't work
  8611. ;; very well. C.f. test case class-16.pike.
  8612. t))
  8613. (setq tmpsymbol (assq (car placeholder)
  8614. '((inexpr-class . class-open)
  8615. (inexpr-statement . block-open))))
  8616. (if tmpsymbol
  8617. ;; It's a statement block or an anonymous class.
  8618. (setq tmpsymbol (cdr tmpsymbol))
  8619. ;; It's a Pike lambda. Check whether we are between the
  8620. ;; lambda keyword and the argument list or at the defun
  8621. ;; opener.
  8622. (setq tmpsymbol (if (eq char-after-ip ?{)
  8623. 'inline-open
  8624. 'lambda-intro-cont)))
  8625. (goto-char (cdr placeholder))
  8626. (back-to-indentation)
  8627. (c-add-stmt-syntax tmpsymbol nil t
  8628. (c-most-enclosing-brace c-state-cache (point))
  8629. paren-state)
  8630. (unless (eq (point) (cdr placeholder))
  8631. (c-add-syntax (car placeholder))))
  8632. ;; CASE 5: Line is inside a declaration level block or at top level.
  8633. ((or containing-decl-open (null containing-sexp))
  8634. (cond
  8635. ;; CASE 5A: we are looking at a defun, brace list, class,
  8636. ;; or inline-inclass method opening brace
  8637. ((setq special-brace-list
  8638. (or (and c-special-brace-lists
  8639. (c-looking-at-special-brace-list))
  8640. (eq char-after-ip ?{)))
  8641. (cond
  8642. ;; CASE 5A.1: Non-class declaration block open.
  8643. ((save-excursion
  8644. (let (tmp)
  8645. (and (eq char-after-ip ?{)
  8646. (setq tmp (c-looking-at-decl-block containing-sexp t))
  8647. (progn
  8648. (setq placeholder (point))
  8649. (goto-char tmp)
  8650. (looking-at c-symbol-key))
  8651. (c-keyword-member
  8652. (c-keyword-sym (setq keyword (match-string 0)))
  8653. 'c-other-block-decl-kwds))))
  8654. (goto-char placeholder)
  8655. (c-add-stmt-syntax
  8656. (if (string-equal keyword "extern")
  8657. ;; Special case for extern-lang-open.
  8658. 'extern-lang-open
  8659. (intern (concat keyword "-open")))
  8660. nil t containing-sexp paren-state))
  8661. ;; CASE 5A.2: we are looking at a class opening brace
  8662. ((save-excursion
  8663. (goto-char indent-point)
  8664. (skip-chars-forward " \t")
  8665. (and (eq (char-after) ?{)
  8666. (c-looking-at-decl-block containing-sexp t)
  8667. (setq placeholder (point))))
  8668. (c-add-syntax 'class-open placeholder))
  8669. ;; CASE 5A.3: brace list open
  8670. ((save-excursion
  8671. (c-beginning-of-decl-1 lim)
  8672. (while (looking-at c-specifier-key)
  8673. (goto-char (match-end 1))
  8674. (c-forward-syntactic-ws indent-point))
  8675. (setq placeholder (c-point 'boi))
  8676. (or (consp special-brace-list)
  8677. (and (or (save-excursion
  8678. (goto-char indent-point)
  8679. (setq tmpsymbol nil)
  8680. (while (and (> (point) placeholder)
  8681. (zerop (c-backward-token-2 1 t))
  8682. (/= (char-after) ?=))
  8683. (and c-opt-inexpr-brace-list-key
  8684. (not tmpsymbol)
  8685. (looking-at c-opt-inexpr-brace-list-key)
  8686. (setq tmpsymbol 'topmost-intro-cont)))
  8687. (eq (char-after) ?=))
  8688. (looking-at c-brace-list-key))
  8689. (save-excursion
  8690. (while (and (< (point) indent-point)
  8691. (zerop (c-forward-token-2 1 t))
  8692. (not (memq (char-after) '(?\; ?\()))))
  8693. (not (memq (char-after) '(?\; ?\()))
  8694. ))))
  8695. (if (and (not c-auto-newline-analysis)
  8696. (c-major-mode-is 'java-mode)
  8697. (eq tmpsymbol 'topmost-intro-cont))
  8698. ;; We're in Java and have found that the open brace
  8699. ;; belongs to a "new Foo[]" initialization list,
  8700. ;; which means the brace list is part of an
  8701. ;; expression and not a top level definition. We
  8702. ;; therefore treat it as any topmost continuation
  8703. ;; even though the semantically correct symbol still
  8704. ;; is brace-list-open, on the same grounds as in
  8705. ;; case B.2.
  8706. (progn
  8707. (c-beginning-of-statement-1 lim)
  8708. (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  8709. (c-add-syntax 'brace-list-open placeholder)))
  8710. ;; CASE 5A.4: inline defun open
  8711. ((and containing-decl-open
  8712. (not (c-keyword-member containing-decl-kwd
  8713. 'c-other-block-decl-kwds)))
  8714. (c-add-syntax 'inline-open)
  8715. (c-add-class-syntax 'inclass
  8716. containing-decl-open
  8717. containing-decl-start
  8718. containing-decl-kwd
  8719. paren-state))
  8720. ;; CASE 5A.5: ordinary defun open
  8721. (t
  8722. (save-excursion
  8723. (c-beginning-of-decl-1 lim)
  8724. (while (looking-at c-specifier-key)
  8725. (goto-char (match-end 1))
  8726. (c-forward-syntactic-ws indent-point))
  8727. (c-add-syntax 'defun-open (c-point 'boi))
  8728. ;; Bogus to use bol here, but it's the legacy. (Resolved,
  8729. ;; 2007-11-09)
  8730. ))))
  8731. ;; CASE 5B: After a function header but before the body (or
  8732. ;; the ending semicolon if there's no body).
  8733. ((save-excursion
  8734. (when (setq placeholder (c-just-after-func-arglist-p
  8735. (max lim (c-determine-limit 500))))
  8736. (setq tmp-pos (point))))
  8737. (cond
  8738. ;; CASE 5B.1: Member init list.
  8739. ((eq (char-after tmp-pos) ?:)
  8740. (if (or (>= tmp-pos indent-point)
  8741. (= (c-point 'bosws) (1+ tmp-pos)))
  8742. (progn
  8743. ;; There is no preceding member init clause.
  8744. ;; Indent relative to the beginning of indentation
  8745. ;; for the topmost-intro line that contains the
  8746. ;; prototype's open paren.
  8747. (goto-char placeholder)
  8748. (c-add-syntax 'member-init-intro (c-point 'boi)))
  8749. ;; Indent relative to the first member init clause.
  8750. (goto-char (1+ tmp-pos))
  8751. (c-forward-syntactic-ws)
  8752. (c-add-syntax 'member-init-cont (point))))
  8753. ;; CASE 5B.2: K&R arg decl intro
  8754. ((and c-recognize-knr-p
  8755. (c-in-knr-argdecl lim))
  8756. (c-beginning-of-statement-1 lim)
  8757. (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
  8758. (if containing-decl-open
  8759. (c-add-class-syntax 'inclass
  8760. containing-decl-open
  8761. containing-decl-start
  8762. containing-decl-kwd
  8763. paren-state)))
  8764. ;; CASE 5B.4: Nether region after a C++ or Java func
  8765. ;; decl, which could include a `throws' declaration.
  8766. (t
  8767. (c-beginning-of-statement-1 lim)
  8768. (c-add-syntax 'func-decl-cont (c-point 'boi))
  8769. )))
  8770. ;; CASE 5C: inheritance line. could be first inheritance
  8771. ;; line, or continuation of a multiple inheritance
  8772. ((or (and (c-major-mode-is 'c++-mode)
  8773. (progn
  8774. (when (eq char-after-ip ?,)
  8775. (skip-chars-forward " \t")
  8776. (forward-char))
  8777. (looking-at c-opt-postfix-decl-spec-key)))
  8778. (and (or (eq char-before-ip ?:)
  8779. ;; watch out for scope operator
  8780. (save-excursion
  8781. (and (eq char-after-ip ?:)
  8782. (c-safe (forward-char 1) t)
  8783. (not (eq (char-after) ?:))
  8784. )))
  8785. (save-excursion
  8786. (c-backward-syntactic-ws lim)
  8787. (if (eq char-before-ip ?:)
  8788. (progn
  8789. (forward-char -1)
  8790. (c-backward-syntactic-ws lim)))
  8791. (back-to-indentation)
  8792. (looking-at c-class-key)))
  8793. ;; for Java
  8794. (and (c-major-mode-is 'java-mode)
  8795. (let ((fence (save-excursion
  8796. (c-beginning-of-statement-1 lim)
  8797. (point)))
  8798. cont done)
  8799. (save-excursion
  8800. (while (not done)
  8801. (cond ((looking-at c-opt-postfix-decl-spec-key)
  8802. (setq injava-inher (cons cont (point))
  8803. done t))
  8804. ((or (not (c-safe (c-forward-sexp -1) t))
  8805. (<= (point) fence))
  8806. (setq done t))
  8807. )
  8808. (setq cont t)))
  8809. injava-inher)
  8810. (not (c-crosses-statement-barrier-p (cdr injava-inher)
  8811. (point)))
  8812. ))
  8813. (cond
  8814. ;; CASE 5C.1: non-hanging colon on an inher intro
  8815. ((eq char-after-ip ?:)
  8816. (c-beginning-of-statement-1 lim)
  8817. (c-add-syntax 'inher-intro (c-point 'boi))
  8818. ;; don't add inclass symbol since relative point already
  8819. ;; contains any class offset
  8820. )
  8821. ;; CASE 5C.2: hanging colon on an inher intro
  8822. ((eq char-before-ip ?:)
  8823. (c-beginning-of-statement-1 lim)
  8824. (c-add-syntax 'inher-intro (c-point 'boi))
  8825. (if containing-decl-open
  8826. (c-add-class-syntax 'inclass
  8827. containing-decl-open
  8828. containing-decl-start
  8829. containing-decl-kwd
  8830. paren-state)))
  8831. ;; CASE 5C.3: in a Java implements/extends
  8832. (injava-inher
  8833. (let ((where (cdr injava-inher))
  8834. (cont (car injava-inher)))
  8835. (goto-char where)
  8836. (cond ((looking-at "throws\\>[^_]")
  8837. (c-add-syntax 'func-decl-cont
  8838. (progn (c-beginning-of-statement-1 lim)
  8839. (c-point 'boi))))
  8840. (cont (c-add-syntax 'inher-cont where))
  8841. (t (c-add-syntax 'inher-intro
  8842. (progn (goto-char (cdr injava-inher))
  8843. (c-beginning-of-statement-1 lim)
  8844. (point))))
  8845. )))
  8846. ;; CASE 5C.4: a continued inheritance line
  8847. (t
  8848. (c-beginning-of-inheritance-list lim)
  8849. (c-add-syntax 'inher-cont (point))
  8850. ;; don't add inclass symbol since relative point already
  8851. ;; contains any class offset
  8852. )))
  8853. ;; CASE 5D: this could be a top-level initialization, a
  8854. ;; member init list continuation, or a template argument
  8855. ;; list continuation.
  8856. ((save-excursion
  8857. ;; Note: We use the fact that lim is always after any
  8858. ;; preceding brace sexp.
  8859. (if c-recognize-<>-arglists
  8860. (while (and
  8861. (progn
  8862. (c-syntactic-skip-backward "^;,=<>" lim t)
  8863. (> (point) lim))
  8864. (or
  8865. (when c-overloadable-operators-regexp
  8866. (when (setq placeholder (c-after-special-operator-id lim))
  8867. (goto-char placeholder)
  8868. t))
  8869. (cond
  8870. ((eq (char-before) ?>)
  8871. (or (c-backward-<>-arglist nil lim)
  8872. (backward-char))
  8873. t)
  8874. ((eq (char-before) ?<)
  8875. (backward-char)
  8876. (if (save-excursion
  8877. (c-forward-<>-arglist nil))
  8878. (progn (forward-char)
  8879. nil)
  8880. t))
  8881. (t nil)))))
  8882. ;; NB: No c-after-special-operator-id stuff in this
  8883. ;; clause - we assume only C++ needs it.
  8884. (c-syntactic-skip-backward "^;,=" lim t))
  8885. (memq (char-before) '(?, ?= ?<)))
  8886. (cond
  8887. ;; CASE 5D.3: perhaps a template list continuation?
  8888. ((and (c-major-mode-is 'c++-mode)
  8889. (save-excursion
  8890. (save-restriction
  8891. (c-with-syntax-table c++-template-syntax-table
  8892. (goto-char indent-point)
  8893. (setq placeholder (c-up-list-backward))
  8894. (and placeholder
  8895. (eq (char-after placeholder) ?<))))))
  8896. (c-with-syntax-table c++-template-syntax-table
  8897. (goto-char placeholder)
  8898. (c-beginning-of-statement-1 lim t)
  8899. (if (save-excursion
  8900. (c-backward-syntactic-ws lim)
  8901. (eq (char-before) ?<))
  8902. ;; In a nested template arglist.
  8903. (progn
  8904. (goto-char placeholder)
  8905. (c-syntactic-skip-backward "^,;" lim t)
  8906. (c-forward-syntactic-ws))
  8907. (back-to-indentation)))
  8908. ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
  8909. ;; template aware.
  8910. (c-add-syntax 'template-args-cont (point) placeholder))
  8911. ;; CASE 5D.4: perhaps a multiple inheritance line?
  8912. ((and (c-major-mode-is 'c++-mode)
  8913. (save-excursion
  8914. (c-beginning-of-statement-1 lim)
  8915. (setq placeholder (point))
  8916. (if (looking-at "static\\>[^_]")
  8917. (c-forward-token-2 1 nil indent-point))
  8918. (and (looking-at c-class-key)
  8919. (zerop (c-forward-token-2 2 nil indent-point))
  8920. (if (eq (char-after) ?<)
  8921. (c-with-syntax-table c++-template-syntax-table
  8922. (zerop (c-forward-token-2 1 t indent-point)))
  8923. t)
  8924. (eq (char-after) ?:))))
  8925. (goto-char placeholder)
  8926. (c-add-syntax 'inher-cont (c-point 'boi)))
  8927. ;; CASE 5D.5: Continuation of the "expression part" of a
  8928. ;; top level construct. Or, perhaps, an unrecognized construct.
  8929. (t
  8930. (while (and (setq placeholder (point))
  8931. (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
  8932. 'same)
  8933. (save-excursion
  8934. (c-backward-syntactic-ws)
  8935. (eq (char-before) ?}))
  8936. (< (point) placeholder)))
  8937. (c-add-stmt-syntax
  8938. (cond
  8939. ((eq (point) placeholder) 'statement) ; unrecognized construct
  8940. ;; A preceding comma at the top level means that a
  8941. ;; new variable declaration starts here. Use
  8942. ;; topmost-intro-cont for it, for consistency with
  8943. ;; the first variable declaration. C.f. case 5N.
  8944. ((eq char-before-ip ?,) 'topmost-intro-cont)
  8945. (t 'statement-cont))
  8946. nil nil containing-sexp paren-state))
  8947. ))
  8948. ;; CASE 5F: Close of a non-class declaration level block.
  8949. ((and (eq char-after-ip ?})
  8950. (c-keyword-member containing-decl-kwd
  8951. 'c-other-block-decl-kwds))
  8952. ;; This is inconsistent: Should use `containing-decl-open'
  8953. ;; here if it's at boi, like in case 5J.
  8954. (goto-char containing-decl-start)
  8955. (c-add-stmt-syntax
  8956. (if (string-equal (symbol-name containing-decl-kwd) "extern")
  8957. ;; Special case for compatibility with the
  8958. ;; extern-lang syntactic symbols.
  8959. 'extern-lang-close
  8960. (intern (concat (symbol-name containing-decl-kwd)
  8961. "-close")))
  8962. nil t
  8963. (c-most-enclosing-brace paren-state (point))
  8964. paren-state))
  8965. ;; CASE 5G: we are looking at the brace which closes the
  8966. ;; enclosing nested class decl
  8967. ((and containing-sexp
  8968. (eq char-after-ip ?})
  8969. (eq containing-decl-open containing-sexp))
  8970. (c-add-class-syntax 'class-close
  8971. containing-decl-open
  8972. containing-decl-start
  8973. containing-decl-kwd
  8974. paren-state))
  8975. ;; CASE 5H: we could be looking at subsequent knr-argdecls
  8976. ((and c-recognize-knr-p
  8977. (not containing-sexp) ; can't be knr inside braces.
  8978. (not (eq char-before-ip ?}))
  8979. (save-excursion
  8980. (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
  8981. (and placeholder
  8982. ;; Do an extra check to avoid tripping up on
  8983. ;; statements that occur in invalid contexts
  8984. ;; (e.g. in macro bodies where we don't really
  8985. ;; know the context of what we're looking at).
  8986. (not (and c-opt-block-stmt-key
  8987. (looking-at c-opt-block-stmt-key)))))
  8988. (< placeholder indent-point))
  8989. (goto-char placeholder)
  8990. (c-add-syntax 'knr-argdecl (point)))
  8991. ;; CASE 5I: ObjC method definition.
  8992. ((and c-opt-method-key
  8993. (looking-at c-opt-method-key))
  8994. (c-beginning-of-statement-1 nil t)
  8995. (if (= (point) indent-point)
  8996. ;; Handle the case when it's the first (non-comment)
  8997. ;; thing in the buffer. Can't look for a 'same return
  8998. ;; value from cbos1 since ObjC directives currently
  8999. ;; aren't recognized fully, so that we get 'same
  9000. ;; instead of 'previous if it moved over a preceding
  9001. ;; directive.
  9002. (goto-char (point-min)))
  9003. (c-add-syntax 'objc-method-intro (c-point 'boi)))
  9004. ;; CASE 5P: AWK pattern or function or continuation
  9005. ;; thereof.
  9006. ((c-major-mode-is 'awk-mode)
  9007. (setq placeholder (point))
  9008. (c-add-stmt-syntax
  9009. (if (and (eq (c-beginning-of-statement-1) 'same)
  9010. (/= (point) placeholder))
  9011. 'topmost-intro-cont
  9012. 'topmost-intro)
  9013. nil nil
  9014. containing-sexp paren-state))
  9015. ;; CASE 5N: At a variable declaration that follows a class
  9016. ;; definition or some other block declaration that doesn't
  9017. ;; end at the closing '}'. C.f. case 5D.5.
  9018. ((progn
  9019. (c-backward-syntactic-ws lim)
  9020. (and (eq (char-before) ?})
  9021. (save-excursion
  9022. (let ((start (point)))
  9023. (if (and c-state-cache
  9024. (consp (car c-state-cache))
  9025. (eq (cdar c-state-cache) (point)))
  9026. ;; Speed up the backward search a bit.
  9027. (goto-char (caar c-state-cache)))
  9028. (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
  9029. (setq placeholder (point))
  9030. (if (= start (point))
  9031. ;; The '}' is unbalanced.
  9032. nil
  9033. (c-end-of-decl-1)
  9034. (>= (point) indent-point))))))
  9035. (goto-char placeholder)
  9036. (c-add-stmt-syntax 'topmost-intro-cont nil nil
  9037. containing-sexp paren-state))
  9038. ;; NOTE: The point is at the end of the previous token here.
  9039. ;; CASE 5J: we are at the topmost level, make
  9040. ;; sure we skip back past any access specifiers
  9041. ((and
  9042. ;; A macro continuation line is never at top level.
  9043. (not (and macro-start
  9044. (> indent-point macro-start)))
  9045. (save-excursion
  9046. (setq placeholder (point))
  9047. (or (memq char-before-ip '(?\; ?{ ?} nil))
  9048. (c-at-vsemi-p before-ws-ip)
  9049. (when (and (eq char-before-ip ?:)
  9050. (eq (c-beginning-of-statement-1 lim)
  9051. 'label))
  9052. (c-backward-syntactic-ws lim)
  9053. (setq placeholder (point)))
  9054. (and (c-major-mode-is 'objc-mode)
  9055. (catch 'not-in-directive
  9056. (c-beginning-of-statement-1 lim)
  9057. (setq placeholder (point))
  9058. (while (and (c-forward-objc-directive)
  9059. (< (point) indent-point))
  9060. (c-forward-syntactic-ws)
  9061. (if (>= (point) indent-point)
  9062. (throw 'not-in-directive t))
  9063. (setq placeholder (point)))
  9064. nil)))))
  9065. ;; For historic reasons we anchor at bol of the last
  9066. ;; line of the previous declaration. That's clearly
  9067. ;; highly bogus and useless, and it makes our lives hard
  9068. ;; to remain compatible. :P
  9069. (goto-char placeholder)
  9070. (c-add-syntax 'topmost-intro (c-point 'bol))
  9071. (if containing-decl-open
  9072. (if (c-keyword-member containing-decl-kwd
  9073. 'c-other-block-decl-kwds)
  9074. (progn
  9075. (goto-char (c-brace-anchor-point containing-decl-open))
  9076. (c-add-stmt-syntax
  9077. (if (string-equal (symbol-name containing-decl-kwd)
  9078. "extern")
  9079. ;; Special case for compatibility with the
  9080. ;; extern-lang syntactic symbols.
  9081. 'inextern-lang
  9082. (intern (concat "in"
  9083. (symbol-name containing-decl-kwd))))
  9084. nil t
  9085. (c-most-enclosing-brace paren-state (point))
  9086. paren-state))
  9087. (c-add-class-syntax 'inclass
  9088. containing-decl-open
  9089. containing-decl-start
  9090. containing-decl-kwd
  9091. paren-state)))
  9092. (when (and c-syntactic-indentation-in-macros
  9093. macro-start
  9094. (/= macro-start (c-point 'boi indent-point)))
  9095. (c-add-syntax 'cpp-define-intro)
  9096. (setq macro-start nil)))
  9097. ;; CASE 5K: we are at an ObjC method definition
  9098. ;; continuation line.
  9099. ((and c-opt-method-key
  9100. (save-excursion
  9101. (c-beginning-of-statement-1 lim)
  9102. (beginning-of-line)
  9103. (when (looking-at c-opt-method-key)
  9104. (setq placeholder (point)))))
  9105. (c-add-syntax 'objc-method-args-cont placeholder))
  9106. ;; CASE 5L: we are at the first argument of a template
  9107. ;; arglist that begins on the previous line.
  9108. ((and c-recognize-<>-arglists
  9109. (eq (char-before) ?<)
  9110. (not (and c-overloadable-operators-regexp
  9111. (c-after-special-operator-id lim))))
  9112. (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
  9113. (c-add-syntax 'template-args-cont (c-point 'boi)))
  9114. ;; CASE 5Q: we are at a statement within a macro.
  9115. (macro-start
  9116. (c-beginning-of-statement-1 containing-sexp)
  9117. (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
  9118. ;;CASE 5N: We are at a topmost continuation line and the only
  9119. ;;preceding items are annotations.
  9120. ((and (c-major-mode-is 'java-mode)
  9121. (setq placeholder (point))
  9122. (c-beginning-of-statement-1)
  9123. (progn
  9124. (while (and (c-forward-annotation))
  9125. (c-forward-syntactic-ws))
  9126. t)
  9127. (prog1
  9128. (>= (point) placeholder)
  9129. (goto-char placeholder)))
  9130. (c-add-syntax 'annotation-top-cont (c-point 'boi)))
  9131. ;; CASE 5M: we are at a topmost continuation line
  9132. (t
  9133. (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
  9134. (when (c-major-mode-is 'objc-mode)
  9135. (setq placeholder (point))
  9136. (while (and (c-forward-objc-directive)
  9137. (< (point) indent-point))
  9138. (c-forward-syntactic-ws)
  9139. (setq placeholder (point)))
  9140. (goto-char placeholder))
  9141. (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  9142. ))
  9143. ;; (CASE 6 has been removed.)
  9144. ;; CASE 7: line is an expression, not a statement. Most
  9145. ;; likely we are either in a function prototype or a function
  9146. ;; call argument list
  9147. ((not (or (and c-special-brace-lists
  9148. (save-excursion
  9149. (goto-char containing-sexp)
  9150. (c-looking-at-special-brace-list)))
  9151. (eq (char-after containing-sexp) ?{)))
  9152. (cond
  9153. ;; CASE 7A: we are looking at the arglist closing paren.
  9154. ;; C.f. case 7F.
  9155. ((memq char-after-ip '(?\) ?\]))
  9156. (goto-char containing-sexp)
  9157. (setq placeholder (c-point 'boi))
  9158. (if (and (c-safe (backward-up-list 1) t)
  9159. (>= (point) placeholder))
  9160. (progn
  9161. (forward-char)
  9162. (skip-chars-forward " \t"))
  9163. (goto-char placeholder))
  9164. (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
  9165. (c-most-enclosing-brace paren-state (point))
  9166. paren-state))
  9167. ;; CASE 7B: Looking at the opening brace of an
  9168. ;; in-expression block or brace list. C.f. cases 4, 16A
  9169. ;; and 17E.
  9170. ((and (eq char-after-ip ?{)
  9171. (progn
  9172. (setq placeholder (c-inside-bracelist-p (point)
  9173. paren-state))
  9174. (if placeholder
  9175. (setq tmpsymbol '(brace-list-open . inexpr-class))
  9176. (setq tmpsymbol '(block-open . inexpr-statement)
  9177. placeholder
  9178. (cdr-safe (c-looking-at-inexpr-block
  9179. (c-safe-position containing-sexp
  9180. paren-state)
  9181. containing-sexp)))
  9182. ;; placeholder is nil if it's a block directly in
  9183. ;; a function arglist. That makes us skip out of
  9184. ;; this case.
  9185. )))
  9186. (goto-char placeholder)
  9187. (back-to-indentation)
  9188. (c-add-stmt-syntax (car tmpsymbol) nil t
  9189. (c-most-enclosing-brace paren-state (point))
  9190. paren-state)
  9191. (if (/= (point) placeholder)
  9192. (c-add-syntax (cdr tmpsymbol))))
  9193. ;; CASE 7C: we are looking at the first argument in an empty
  9194. ;; argument list. Use arglist-close if we're actually
  9195. ;; looking at a close paren or bracket.
  9196. ((memq char-before-ip '(?\( ?\[))
  9197. (goto-char containing-sexp)
  9198. (setq placeholder (c-point 'boi))
  9199. (if (and (c-safe (backward-up-list 1) t)
  9200. (>= (point) placeholder))
  9201. (progn
  9202. (forward-char)
  9203. (skip-chars-forward " \t"))
  9204. (goto-char placeholder))
  9205. (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
  9206. (c-most-enclosing-brace paren-state (point))
  9207. paren-state))
  9208. ;; CASE 7D: we are inside a conditional test clause. treat
  9209. ;; these things as statements
  9210. ((progn
  9211. (goto-char containing-sexp)
  9212. (and (c-safe (c-forward-sexp -1) t)
  9213. (looking-at "\\<for\\>[^_]")))
  9214. (goto-char (1+ containing-sexp))
  9215. (c-forward-syntactic-ws indent-point)
  9216. (if (eq char-before-ip ?\;)
  9217. (c-add-syntax 'statement (point))
  9218. (c-add-syntax 'statement-cont (point))
  9219. ))
  9220. ;; CASE 7E: maybe a continued ObjC method call. This is the
  9221. ;; case when we are inside a [] bracketed exp, and what
  9222. ;; precede the opening bracket is not an identifier.
  9223. ((and c-opt-method-key
  9224. (eq (char-after containing-sexp) ?\[)
  9225. (progn
  9226. (goto-char (1- containing-sexp))
  9227. (c-backward-syntactic-ws (c-point 'bod))
  9228. (if (not (looking-at c-symbol-key))
  9229. (c-add-syntax 'objc-method-call-cont containing-sexp))
  9230. )))
  9231. ;; CASE 7F: we are looking at an arglist continuation line,
  9232. ;; but the preceding argument is on the same line as the
  9233. ;; opening paren. This case includes multi-line
  9234. ;; mathematical paren groupings, but we could be on a
  9235. ;; for-list continuation line. C.f. case 7A.
  9236. ((progn
  9237. (goto-char (1+ containing-sexp))
  9238. (< (save-excursion
  9239. (c-forward-syntactic-ws)
  9240. (point))
  9241. (c-point 'bonl)))
  9242. (goto-char containing-sexp) ; paren opening the arglist
  9243. (setq placeholder (c-point 'boi))
  9244. (if (and (c-safe (backward-up-list 1) t)
  9245. (>= (point) placeholder))
  9246. (progn
  9247. (forward-char)
  9248. (skip-chars-forward " \t"))
  9249. (goto-char placeholder))
  9250. (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
  9251. (c-most-enclosing-brace c-state-cache (point))
  9252. paren-state))
  9253. ;; CASE 7G: we are looking at just a normal arglist
  9254. ;; continuation line
  9255. (t (c-forward-syntactic-ws indent-point)
  9256. (c-add-syntax 'arglist-cont (c-point 'boi)))
  9257. ))
  9258. ;; CASE 8: func-local multi-inheritance line
  9259. ((and (c-major-mode-is 'c++-mode)
  9260. (save-excursion
  9261. (goto-char indent-point)
  9262. (skip-chars-forward " \t")
  9263. (looking-at c-opt-postfix-decl-spec-key)))
  9264. (goto-char indent-point)
  9265. (skip-chars-forward " \t")
  9266. (cond
  9267. ;; CASE 8A: non-hanging colon on an inher intro
  9268. ((eq char-after-ip ?:)
  9269. (c-backward-syntactic-ws lim)
  9270. (c-add-syntax 'inher-intro (c-point 'boi)))
  9271. ;; CASE 8B: hanging colon on an inher intro
  9272. ((eq char-before-ip ?:)
  9273. (c-add-syntax 'inher-intro (c-point 'boi)))
  9274. ;; CASE 8C: a continued inheritance line
  9275. (t
  9276. (c-beginning-of-inheritance-list lim)
  9277. (c-add-syntax 'inher-cont (point))
  9278. )))
  9279. ;; CASE 9: we are inside a brace-list
  9280. ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
  9281. (setq special-brace-list
  9282. (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
  9283. (save-excursion
  9284. (goto-char containing-sexp)
  9285. (c-looking-at-special-brace-list)))
  9286. (c-inside-bracelist-p containing-sexp paren-state))))
  9287. (cond
  9288. ;; CASE 9A: In the middle of a special brace list opener.
  9289. ((and (consp special-brace-list)
  9290. (save-excursion
  9291. (goto-char containing-sexp)
  9292. (eq (char-after) ?\())
  9293. (eq char-after-ip (car (cdr special-brace-list))))
  9294. (goto-char (car (car special-brace-list)))
  9295. (skip-chars-backward " \t")
  9296. (if (and (bolp)
  9297. (assoc 'statement-cont
  9298. (setq placeholder (c-guess-basic-syntax))))
  9299. (setq c-syntactic-context placeholder)
  9300. (c-beginning-of-statement-1
  9301. (c-safe-position (1- containing-sexp) paren-state))
  9302. (c-forward-token-2 0)
  9303. (while (looking-at c-specifier-key)
  9304. (goto-char (match-end 1))
  9305. (c-forward-syntactic-ws))
  9306. (c-add-syntax 'brace-list-open (c-point 'boi))))
  9307. ;; CASE 9B: brace-list-close brace
  9308. ((if (consp special-brace-list)
  9309. ;; Check special brace list closer.
  9310. (progn
  9311. (goto-char (car (car special-brace-list)))
  9312. (save-excursion
  9313. (goto-char indent-point)
  9314. (back-to-indentation)
  9315. (or
  9316. ;; We were between the special close char and the `)'.
  9317. (and (eq (char-after) ?\))
  9318. (eq (1+ (point)) (cdr (car special-brace-list))))
  9319. ;; We were before the special close char.
  9320. (and (eq (char-after) (cdr (cdr special-brace-list)))
  9321. (zerop (c-forward-token-2))
  9322. (eq (1+ (point)) (cdr (car special-brace-list)))))))
  9323. ;; Normal brace list check.
  9324. (and (eq char-after-ip ?})
  9325. (c-safe (goto-char (c-up-list-backward (point))) t)
  9326. (= (point) containing-sexp)))
  9327. (if (eq (point) (c-point 'boi))
  9328. (c-add-syntax 'brace-list-close (point))
  9329. (setq lim (c-most-enclosing-brace c-state-cache (point)))
  9330. (c-beginning-of-statement-1 lim)
  9331. (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
  9332. (t
  9333. ;; Prepare for the rest of the cases below by going to the
  9334. ;; token following the opening brace
  9335. (if (consp special-brace-list)
  9336. (progn
  9337. (goto-char (car (car special-brace-list)))
  9338. (c-forward-token-2 1 nil indent-point))
  9339. (goto-char containing-sexp))
  9340. (forward-char)
  9341. (let ((start (point)))
  9342. (c-forward-syntactic-ws indent-point)
  9343. (goto-char (max start (c-point 'bol))))
  9344. (c-skip-ws-forward indent-point)
  9345. (cond
  9346. ;; CASE 9C: we're looking at the first line in a brace-list
  9347. ((= (point) indent-point)
  9348. (if (consp special-brace-list)
  9349. (goto-char (car (car special-brace-list)))
  9350. (goto-char containing-sexp))
  9351. (if (eq (point) (c-point 'boi))
  9352. (c-add-syntax 'brace-list-intro (point))
  9353. (setq lim (c-most-enclosing-brace c-state-cache (point)))
  9354. (c-beginning-of-statement-1 lim)
  9355. (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
  9356. ;; CASE 9D: this is just a later brace-list-entry or
  9357. ;; brace-entry-open
  9358. (t (if (or (eq char-after-ip ?{)
  9359. (and c-special-brace-lists
  9360. (save-excursion
  9361. (goto-char indent-point)
  9362. (c-forward-syntactic-ws (c-point 'eol))
  9363. (c-looking-at-special-brace-list (point)))))
  9364. (c-add-syntax 'brace-entry-open (point))
  9365. (c-add-syntax 'brace-list-entry (point))
  9366. ))
  9367. ))))
  9368. ;; CASE 10: A continued statement or top level construct.
  9369. ((and (not (memq char-before-ip '(?\; ?:)))
  9370. (not (c-at-vsemi-p before-ws-ip))
  9371. (or (not (eq char-before-ip ?}))
  9372. (c-looking-at-inexpr-block-backward c-state-cache))
  9373. (> (point)
  9374. (save-excursion
  9375. (c-beginning-of-statement-1 containing-sexp)
  9376. (setq placeholder (point))))
  9377. (/= placeholder containing-sexp))
  9378. ;; This is shared with case 18.
  9379. (c-guess-continued-construct indent-point
  9380. char-after-ip
  9381. placeholder
  9382. containing-sexp
  9383. paren-state))
  9384. ;; CASE 16: block close brace, possibly closing the defun or
  9385. ;; the class
  9386. ((eq char-after-ip ?})
  9387. ;; From here on we have the next containing sexp in lim.
  9388. (setq lim (c-most-enclosing-brace paren-state))
  9389. (goto-char containing-sexp)
  9390. (cond
  9391. ;; CASE 16E: Closing a statement block? This catches
  9392. ;; cases where it's preceded by a statement keyword,
  9393. ;; which works even when used in an "invalid" context,
  9394. ;; e.g. a macro argument.
  9395. ((c-after-conditional)
  9396. (c-backward-to-block-anchor lim)
  9397. (c-add-stmt-syntax 'block-close nil t lim paren-state))
  9398. ;; CASE 16A: closing a lambda defun or an in-expression
  9399. ;; block? C.f. cases 4, 7B and 17E.
  9400. ((setq placeholder (c-looking-at-inexpr-block
  9401. (c-safe-position containing-sexp paren-state)
  9402. nil))
  9403. (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
  9404. 'inline-close
  9405. 'block-close))
  9406. (goto-char containing-sexp)
  9407. (back-to-indentation)
  9408. (if (= containing-sexp (point))
  9409. (c-add-syntax tmpsymbol (point))
  9410. (goto-char (cdr placeholder))
  9411. (back-to-indentation)
  9412. (c-add-stmt-syntax tmpsymbol nil t
  9413. (c-most-enclosing-brace paren-state (point))
  9414. paren-state)
  9415. (if (/= (point) (cdr placeholder))
  9416. (c-add-syntax (car placeholder)))))
  9417. ;; CASE 16B: does this close an inline or a function in
  9418. ;; a non-class declaration level block?
  9419. ((save-excursion
  9420. (and lim
  9421. (progn
  9422. (goto-char lim)
  9423. (c-looking-at-decl-block
  9424. (c-most-enclosing-brace paren-state lim)
  9425. nil))
  9426. (setq placeholder (point))))
  9427. (c-backward-to-decl-anchor lim)
  9428. (back-to-indentation)
  9429. (if (save-excursion
  9430. (goto-char placeholder)
  9431. (looking-at c-other-decl-block-key))
  9432. (c-add-syntax 'defun-close (point))
  9433. (c-add-syntax 'inline-close (point))))
  9434. ;; CASE 16F: Can be a defun-close of a function declared
  9435. ;; in a statement block, e.g. in Pike or when using gcc
  9436. ;; extensions, but watch out for macros followed by
  9437. ;; blocks. Let it through to be handled below.
  9438. ;; C.f. cases B.3 and 17G.
  9439. ((save-excursion
  9440. (and (not (c-at-statement-start-p))
  9441. (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
  9442. (setq placeholder (point))
  9443. (let ((c-recognize-typeless-decls nil))
  9444. ;; Turn off recognition of constructs that
  9445. ;; lacks a type in this case, since that's more
  9446. ;; likely to be a macro followed by a block.
  9447. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  9448. (back-to-indentation)
  9449. (if (/= (point) containing-sexp)
  9450. (goto-char placeholder))
  9451. (c-add-stmt-syntax 'defun-close nil t lim paren-state))
  9452. ;; CASE 16C: If there is an enclosing brace then this is
  9453. ;; a block close since defun closes inside declaration
  9454. ;; level blocks have been handled above.
  9455. (lim
  9456. ;; If the block is preceded by a case/switch label on
  9457. ;; the same line, we anchor at the first preceding label
  9458. ;; at boi. The default handling in c-add-stmt-syntax
  9459. ;; really fixes it better, but we do like this to keep
  9460. ;; the indentation compatible with version 5.28 and
  9461. ;; earlier. C.f. case 17H.
  9462. (while (and (/= (setq placeholder (point)) (c-point 'boi))
  9463. (eq (c-beginning-of-statement-1 lim) 'label)))
  9464. (goto-char placeholder)
  9465. (if (looking-at c-label-kwds-regexp)
  9466. (c-add-syntax 'block-close (point))
  9467. (goto-char containing-sexp)
  9468. ;; c-backward-to-block-anchor not necessary here; those
  9469. ;; situations are handled in case 16E above.
  9470. (c-add-stmt-syntax 'block-close nil t lim paren-state)))
  9471. ;; CASE 16D: Only top level defun close left.
  9472. (t
  9473. (goto-char containing-sexp)
  9474. (c-backward-to-decl-anchor lim)
  9475. (c-add-stmt-syntax 'defun-close nil nil
  9476. (c-most-enclosing-brace paren-state)
  9477. paren-state))
  9478. ))
  9479. ;; CASE 19: line is an expression, not a statement, and is directly
  9480. ;; contained by a template delimiter. Most likely, we are in a
  9481. ;; template arglist within a statement. This case is based on CASE
  9482. ;; 7. At some point in the future, we may wish to create more
  9483. ;; syntactic symbols such as `template-intro',
  9484. ;; `template-cont-nonempty', etc., and distinguish between them as we
  9485. ;; do for `arglist-intro' etc. (2009-12-07).
  9486. ((and c-recognize-<>-arglists
  9487. (setq containing-< (c-up-list-backward indent-point containing-sexp))
  9488. (eq (char-after containing-<) ?\<))
  9489. (setq placeholder (c-point 'boi containing-<))
  9490. (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
  9491. ; '<') before indent-point.
  9492. (if (>= (point) placeholder)
  9493. (progn
  9494. (forward-char)
  9495. (skip-chars-forward " \t"))
  9496. (goto-char placeholder))
  9497. (c-add-stmt-syntax 'template-args-cont (list containing-<) t
  9498. (c-most-enclosing-brace c-state-cache (point))
  9499. paren-state))
  9500. ;; CASE 17: Statement or defun catchall.
  9501. (t
  9502. (goto-char indent-point)
  9503. ;; Back up statements until we find one that starts at boi.
  9504. (while (let* ((prev-point (point))
  9505. (last-step-type (c-beginning-of-statement-1
  9506. containing-sexp)))
  9507. (if (= (point) prev-point)
  9508. (progn
  9509. (setq step-type (or step-type last-step-type))
  9510. nil)
  9511. (setq step-type last-step-type)
  9512. (/= (point) (c-point 'boi)))))
  9513. (cond
  9514. ;; CASE 17B: continued statement
  9515. ((and (eq step-type 'same)
  9516. (/= (point) indent-point))
  9517. (c-add-stmt-syntax 'statement-cont nil nil
  9518. containing-sexp paren-state))
  9519. ;; CASE 17A: After a case/default label?
  9520. ((progn
  9521. (while (and (eq step-type 'label)
  9522. (not (looking-at c-label-kwds-regexp)))
  9523. (setq step-type
  9524. (c-beginning-of-statement-1 containing-sexp)))
  9525. (eq step-type 'label))
  9526. (c-add-stmt-syntax (if (eq char-after-ip ?{)
  9527. 'statement-case-open
  9528. 'statement-case-intro)
  9529. nil t containing-sexp paren-state))
  9530. ;; CASE 17D: any old statement
  9531. ((progn
  9532. (while (eq step-type 'label)
  9533. (setq step-type
  9534. (c-beginning-of-statement-1 containing-sexp)))
  9535. (eq step-type 'previous))
  9536. (c-add-stmt-syntax 'statement nil t
  9537. containing-sexp paren-state)
  9538. (if (eq char-after-ip ?{)
  9539. (c-add-syntax 'block-open)))
  9540. ;; CASE 17I: Inside a substatement block.
  9541. ((progn
  9542. ;; The following tests are all based on containing-sexp.
  9543. (goto-char containing-sexp)
  9544. ;; From here on we have the next containing sexp in lim.
  9545. (setq lim (c-most-enclosing-brace paren-state containing-sexp))
  9546. (c-after-conditional))
  9547. (c-backward-to-block-anchor lim)
  9548. (c-add-stmt-syntax 'statement-block-intro nil t
  9549. lim paren-state)
  9550. (if (eq char-after-ip ?{)
  9551. (c-add-syntax 'block-open)))
  9552. ;; CASE 17E: first statement in an in-expression block.
  9553. ;; C.f. cases 4, 7B and 16A.
  9554. ((setq placeholder (c-looking-at-inexpr-block
  9555. (c-safe-position containing-sexp paren-state)
  9556. nil))
  9557. (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
  9558. 'defun-block-intro
  9559. 'statement-block-intro))
  9560. (back-to-indentation)
  9561. (if (= containing-sexp (point))
  9562. (c-add-syntax tmpsymbol (point))
  9563. (goto-char (cdr placeholder))
  9564. (back-to-indentation)
  9565. (c-add-stmt-syntax tmpsymbol nil t
  9566. (c-most-enclosing-brace c-state-cache (point))
  9567. paren-state)
  9568. (if (/= (point) (cdr placeholder))
  9569. (c-add-syntax (car placeholder))))
  9570. (if (eq char-after-ip ?{)
  9571. (c-add-syntax 'block-open)))
  9572. ;; CASE 17F: first statement in an inline, or first
  9573. ;; statement in a top-level defun. we can tell this is it
  9574. ;; if there are no enclosing braces that haven't been
  9575. ;; narrowed out by a class (i.e. don't use bod here).
  9576. ((save-excursion
  9577. (or (not (setq placeholder (c-most-enclosing-brace
  9578. paren-state)))
  9579. (and (progn
  9580. (goto-char placeholder)
  9581. (eq (char-after) ?{))
  9582. (c-looking-at-decl-block (c-most-enclosing-brace
  9583. paren-state (point))
  9584. nil))))
  9585. (c-backward-to-decl-anchor lim)
  9586. (back-to-indentation)
  9587. (c-add-syntax 'defun-block-intro (point)))
  9588. ;; CASE 17G: First statement in a function declared inside
  9589. ;; a normal block. This can occur in Pike and with
  9590. ;; e.g. the gcc extensions, but watch out for macros
  9591. ;; followed by blocks. C.f. cases B.3 and 16F.
  9592. ((save-excursion
  9593. (and (not (c-at-statement-start-p))
  9594. (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
  9595. (setq placeholder (point))
  9596. (let ((c-recognize-typeless-decls nil))
  9597. ;; Turn off recognition of constructs that lacks
  9598. ;; a type in this case, since that's more likely
  9599. ;; to be a macro followed by a block.
  9600. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  9601. (back-to-indentation)
  9602. (if (/= (point) containing-sexp)
  9603. (goto-char placeholder))
  9604. (c-add-stmt-syntax 'defun-block-intro nil t
  9605. lim paren-state))
  9606. ;; CASE 17H: First statement in a block.
  9607. (t
  9608. ;; If the block is preceded by a case/switch label on the
  9609. ;; same line, we anchor at the first preceding label at
  9610. ;; boi. The default handling in c-add-stmt-syntax is
  9611. ;; really fixes it better, but we do like this to keep the
  9612. ;; indentation compatible with version 5.28 and earlier.
  9613. ;; C.f. case 16C.
  9614. (while (and (/= (setq placeholder (point)) (c-point 'boi))
  9615. (eq (c-beginning-of-statement-1 lim) 'label)))
  9616. (goto-char placeholder)
  9617. (if (looking-at c-label-kwds-regexp)
  9618. (c-add-syntax 'statement-block-intro (point))
  9619. (goto-char containing-sexp)
  9620. ;; c-backward-to-block-anchor not necessary here; those
  9621. ;; situations are handled in case 17I above.
  9622. (c-add-stmt-syntax 'statement-block-intro nil t
  9623. lim paren-state))
  9624. (if (eq char-after-ip ?{)
  9625. (c-add-syntax 'block-open)))
  9626. ))
  9627. )
  9628. ;; now we need to look at any modifiers
  9629. (goto-char indent-point)
  9630. (skip-chars-forward " \t")
  9631. ;; are we looking at a comment only line?
  9632. (when (and (looking-at c-comment-start-regexp)
  9633. (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
  9634. (c-append-syntax 'comment-intro))
  9635. ;; we might want to give additional offset to friends (in C++).
  9636. (when (and c-opt-friend-key
  9637. (looking-at c-opt-friend-key))
  9638. (c-append-syntax 'friend))
  9639. ;; Set syntactic-relpos.
  9640. (let ((p c-syntactic-context))
  9641. (while (and p
  9642. (if (integerp (c-langelem-pos (car p)))
  9643. (progn
  9644. (setq syntactic-relpos (c-langelem-pos (car p)))
  9645. nil)
  9646. t))
  9647. (setq p (cdr p))))
  9648. ;; Start of or a continuation of a preprocessor directive?
  9649. (if (and macro-start
  9650. (eq macro-start (c-point 'boi))
  9651. (not (and (c-major-mode-is 'pike-mode)
  9652. (eq (char-after (1+ macro-start)) ?\"))))
  9653. (c-append-syntax 'cpp-macro)
  9654. (when (and c-syntactic-indentation-in-macros macro-start)
  9655. (if in-macro-expr
  9656. (when (or
  9657. (< syntactic-relpos macro-start)
  9658. (not (or
  9659. (assq 'arglist-intro c-syntactic-context)
  9660. (assq 'arglist-cont c-syntactic-context)
  9661. (assq 'arglist-cont-nonempty c-syntactic-context)
  9662. (assq 'arglist-close c-syntactic-context))))
  9663. ;; If inside a cpp expression, i.e. anywhere in a
  9664. ;; cpp directive except a #define body, we only let
  9665. ;; through the syntactic analysis that is internal
  9666. ;; in the expression. That means the arglist
  9667. ;; elements, if they are anchored inside the cpp
  9668. ;; expression.
  9669. (setq c-syntactic-context nil)
  9670. (c-add-syntax 'cpp-macro-cont macro-start))
  9671. (when (and (eq macro-start syntactic-relpos)
  9672. (not (assq 'cpp-define-intro c-syntactic-context))
  9673. (save-excursion
  9674. (goto-char macro-start)
  9675. (or (not (c-forward-to-cpp-define-body))
  9676. (<= (point) (c-point 'boi indent-point)))))
  9677. ;; Inside a #define body and the syntactic analysis is
  9678. ;; anchored on the start of the #define. In this case
  9679. ;; we add cpp-define-intro to get the extra
  9680. ;; indentation of the #define body.
  9681. (c-add-syntax 'cpp-define-intro)))))
  9682. ;; return the syntax
  9683. c-syntactic-context)))
  9684. ;; Indentation calculation.
  9685. (defun c-evaluate-offset (offset langelem symbol)
  9686. ;; offset can be a number, a function, a variable, a list, or one of
  9687. ;; the symbols + or -
  9688. ;;
  9689. ;; This function might do hidden buffer changes.
  9690. (let ((res
  9691. (cond
  9692. ((numberp offset) offset)
  9693. ((vectorp offset) offset)
  9694. ((null offset) nil)
  9695. ((eq offset '+) c-basic-offset)
  9696. ((eq offset '-) (- c-basic-offset))
  9697. ((eq offset '++) (* 2 c-basic-offset))
  9698. ((eq offset '--) (* 2 (- c-basic-offset)))
  9699. ((eq offset '*) (/ c-basic-offset 2))
  9700. ((eq offset '/) (/ (- c-basic-offset) 2))
  9701. ((functionp offset)
  9702. (c-evaluate-offset
  9703. (funcall offset
  9704. (cons (c-langelem-sym langelem)
  9705. (c-langelem-pos langelem)))
  9706. langelem symbol))
  9707. ((listp offset)
  9708. (cond
  9709. ((eq (car offset) 'quote)
  9710. (c-benign-error "The offset %S for %s was mistakenly quoted"
  9711. offset symbol)
  9712. nil)
  9713. ((memq (car offset) '(min max))
  9714. (let (res val (method (car offset)))
  9715. (setq offset (cdr offset))
  9716. (while offset
  9717. (setq val (c-evaluate-offset (car offset) langelem symbol))
  9718. (cond
  9719. ((not val))
  9720. ((not res)
  9721. (setq res val))
  9722. ((integerp val)
  9723. (if (vectorp res)
  9724. (c-benign-error "\
  9725. Error evaluating offset %S for %s: \
  9726. Cannot combine absolute offset %S with relative %S in `%s' method"
  9727. (car offset) symbol res val method)
  9728. (setq res (funcall method res val))))
  9729. (t
  9730. (if (integerp res)
  9731. (c-benign-error "\
  9732. Error evaluating offset %S for %s: \
  9733. Cannot combine relative offset %S with absolute %S in `%s' method"
  9734. (car offset) symbol res val method)
  9735. (setq res (vector (funcall method (aref res 0)
  9736. (aref val 0)))))))
  9737. (setq offset (cdr offset)))
  9738. res))
  9739. ((eq (car offset) 'add)
  9740. (let (res val)
  9741. (setq offset (cdr offset))
  9742. (while offset
  9743. (setq val (c-evaluate-offset (car offset) langelem symbol))
  9744. (cond
  9745. ((not val))
  9746. ((not res)
  9747. (setq res val))
  9748. ((integerp val)
  9749. (if (vectorp res)
  9750. (setq res (vector (+ (aref res 0) val)))
  9751. (setq res (+ res val))))
  9752. (t
  9753. (if (vectorp res)
  9754. (c-benign-error "\
  9755. Error evaluating offset %S for %s: \
  9756. Cannot combine absolute offsets %S and %S in `add' method"
  9757. (car offset) symbol res val)
  9758. (setq res val)))) ; Override.
  9759. (setq offset (cdr offset)))
  9760. res))
  9761. (t
  9762. (let (res)
  9763. (when (eq (car offset) 'first)
  9764. (setq offset (cdr offset)))
  9765. (while (and (not res) offset)
  9766. (setq res (c-evaluate-offset (car offset) langelem symbol)
  9767. offset (cdr offset)))
  9768. res))))
  9769. ((and (symbolp offset) (boundp offset))
  9770. (symbol-value offset))
  9771. (t
  9772. (c-benign-error "Unknown offset format %S for %s" offset symbol)
  9773. nil))))
  9774. (if (or (null res) (integerp res)
  9775. (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
  9776. res
  9777. (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
  9778. offset symbol res)
  9779. nil)))
  9780. (defun c-calc-offset (langelem)
  9781. ;; Get offset from LANGELEM which is a list beginning with the
  9782. ;; syntactic symbol and followed by any analysis data it provides.
  9783. ;; That data may be zero or more elements, but if at least one is
  9784. ;; given then the first is the anchor position (or nil). The symbol
  9785. ;; is matched against `c-offsets-alist' and the offset calculated
  9786. ;; from that is returned.
  9787. ;;
  9788. ;; This function might do hidden buffer changes.
  9789. (let* ((symbol (c-langelem-sym langelem))
  9790. (match (assq symbol c-offsets-alist))
  9791. (offset (cdr-safe match)))
  9792. (if match
  9793. (setq offset (c-evaluate-offset offset langelem symbol))
  9794. (if c-strict-syntax-p
  9795. (c-benign-error "No offset found for syntactic symbol %s" symbol))
  9796. (setq offset 0))
  9797. (if (vectorp offset)
  9798. offset
  9799. (or (and (numberp offset) offset)
  9800. (and (symbolp offset) (symbol-value offset))
  9801. 0))
  9802. ))
  9803. (defun c-get-offset (langelem)
  9804. ;; This is a compatibility wrapper for `c-calc-offset' in case
  9805. ;; someone is calling it directly. It takes an old style syntactic
  9806. ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
  9807. ;; new list form.
  9808. ;;
  9809. ;; This function might do hidden buffer changes.
  9810. (if (c-langelem-pos langelem)
  9811. (c-calc-offset (list (c-langelem-sym langelem)
  9812. (c-langelem-pos langelem)))
  9813. (c-calc-offset langelem)))
  9814. (defun c-get-syntactic-indentation (langelems)
  9815. ;; Calculate the syntactic indentation from a syntactic description
  9816. ;; as returned by `c-guess-syntax'.
  9817. ;;
  9818. ;; Note that topmost-intro always has an anchor position at bol, for
  9819. ;; historical reasons. It's often used together with other symbols
  9820. ;; that has more sane positions. Since we always use the first
  9821. ;; found anchor position, we rely on that these other symbols always
  9822. ;; precede topmost-intro in the LANGELEMS list.
  9823. ;;
  9824. ;; This function might do hidden buffer changes.
  9825. (let ((indent 0) anchor)
  9826. (while langelems
  9827. (let* ((c-syntactic-element (car langelems))
  9828. (res (c-calc-offset c-syntactic-element)))
  9829. (if (vectorp res)
  9830. ;; Got an absolute column that overrides any indentation
  9831. ;; we've collected so far, but not the relative
  9832. ;; indentation we might get for the nested structures
  9833. ;; further down the langelems list.
  9834. (setq indent (elt res 0)
  9835. anchor (point-min)) ; A position at column 0.
  9836. ;; Got a relative change of the current calculated
  9837. ;; indentation.
  9838. (setq indent (+ indent res))
  9839. ;; Use the anchor position from the first syntactic
  9840. ;; element with one.
  9841. (unless anchor
  9842. (setq anchor (c-langelem-pos (car langelems)))))
  9843. (setq langelems (cdr langelems))))
  9844. (if anchor
  9845. (+ indent (save-excursion
  9846. (goto-char anchor)
  9847. (current-column)))
  9848. indent)))
  9849. (cc-provide 'cc-engine)
  9850. ;;; cc-engine.el ends here