cc-engine.el 391 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819
  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 (not necessarily
  2347. ;; immediately preceding), push a cons onto `c-state-cache' to represent it.
  2348. ;; FROM must not be inside a literal. If UPPER-LIM is non-nil, we append
  2349. ;; the highest brace pair whose "}" is below UPPER-LIM.
  2350. ;;
  2351. ;; Return non-nil when this has been done.
  2352. ;;
  2353. ;; This routine should be fast. Since it can get called a LOT, we maintain
  2354. ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
  2355. ;; reduce the time wasted in repeated fruitless searches in brace deserts.
  2356. (save-excursion
  2357. (save-restriction
  2358. (let ((bra from) ce ; Positions of "{" and "}".
  2359. new-cons
  2360. (cache-pos (c-state-cache-top-lparen)) ; might be nil.
  2361. (macro-start-or-from
  2362. (progn (goto-char from)
  2363. (c-beginning-of-macro)
  2364. (point))))
  2365. (or upper-lim (setq upper-lim from))
  2366. ;; If we're essentially repeating a fruitless search, just give up.
  2367. (unless (and c-state-brace-pair-desert
  2368. (eq cache-pos (car c-state-brace-pair-desert))
  2369. (<= from (cdr c-state-brace-pair-desert)))
  2370. ;; Only search what we absolutely need to:
  2371. (if (and c-state-brace-pair-desert
  2372. (eq cache-pos (car c-state-brace-pair-desert)))
  2373. (narrow-to-region (cdr c-state-brace-pair-desert) (point-max)))
  2374. ;; In the next pair of nested loops, the inner one moves back past a
  2375. ;; pair of (mis-)matching parens or brackets; the outer one moves
  2376. ;; back over a sequence of unmatched close brace/paren/bracket each
  2377. ;; time round.
  2378. (while
  2379. (progn
  2380. (c-safe
  2381. (while
  2382. (and (setq ce (scan-lists bra -1 -1)) ; back past )/]/}; might signal
  2383. (setq bra (scan-lists ce -1 1)) ; back past (/[/{; might signal
  2384. (or (> ce upper-lim)
  2385. (not (eq (char-after bra) ?\{))
  2386. (and (goto-char bra)
  2387. (c-beginning-of-macro)
  2388. (< (point) macro-start-or-from))))))
  2389. (and ce (< ce bra)))
  2390. (setq bra ce)) ; If we just backed over an unbalanced closing
  2391. ; brace, ignore it.
  2392. (if (and ce (< bra ce) (eq (char-after bra) ?\{))
  2393. ;; We've found the desired brace-pair.
  2394. (progn
  2395. (setq new-cons (cons bra (1+ ce)))
  2396. (cond
  2397. ((consp (car c-state-cache))
  2398. (setcar c-state-cache new-cons))
  2399. ((and (numberp (car c-state-cache)) ; probably never happens
  2400. (< ce (car c-state-cache)))
  2401. (setcdr c-state-cache
  2402. (cons new-cons (cdr c-state-cache))))
  2403. (t (setq c-state-cache (cons new-cons c-state-cache)))))
  2404. ;; We haven't found a brace pair. Record this.
  2405. (setq c-state-brace-pair-desert (cons cache-pos from))))))))
  2406. (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
  2407. ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
  2408. ;; following a {, and that brace has a (mis-)matching } (or ]), and we
  2409. ;; "push" "a" brace pair onto `c-state-cache'.
  2410. ;;
  2411. ;; Here "push" means overwrite the top element if it's itself a brace-pair,
  2412. ;; otherwise push it normally.
  2413. ;;
  2414. ;; The brace pair we push is normally the one surrounding BRA+1, but if the
  2415. ;; latter is inside a macro, not being a macro containing
  2416. ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
  2417. ;; base pair. This latter case is assumed to be rare.
  2418. ;;
  2419. ;; Note: POINT is not preserved in this routine.
  2420. (if bra+1
  2421. (if (or (> bra+1 macro-start-or-here)
  2422. (progn (goto-char bra+1)
  2423. (not (c-beginning-of-macro))))
  2424. (setq c-state-cache
  2425. (cons (cons (1- bra+1)
  2426. (scan-lists bra+1 1 1))
  2427. (if (consp (car c-state-cache))
  2428. (cdr c-state-cache)
  2429. c-state-cache)))
  2430. ;; N.B. This defsubst codes one method for the simple, normal case,
  2431. ;; and a more sophisticated, slower way for the general case. Don't
  2432. ;; eliminate this defsubst - it's a speed optimization.
  2433. (c-append-lower-brace-pair-to-state-cache (1- bra+1)))))
  2434. (defun c-append-to-state-cache (from)
  2435. ;; Scan the buffer from FROM to (point-max), adding elements into
  2436. ;; `c-state-cache' for braces etc. Return a candidate for
  2437. ;; `c-state-cache-good-pos'.
  2438. ;;
  2439. ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
  2440. ;; any. Typically, it is immediately after it. It must not be inside a
  2441. ;; literal.
  2442. (let ((here-bol (c-point 'bol (point-max)))
  2443. (macro-start-or-here
  2444. (save-excursion (goto-char (point-max))
  2445. (if (c-beginning-of-macro)
  2446. (point)
  2447. (point-max))))
  2448. pa+1 ; pos just after an opening PAren (or brace).
  2449. (ren+1 from) ; usually a pos just after an closing paREN etc.
  2450. ; Is actually the pos. to scan for a (/{/[ from,
  2451. ; which sometimes is after a silly )/}/].
  2452. paren+1 ; Pos after some opening or closing paren.
  2453. paren+1s ; A list of `paren+1's; used to determine a
  2454. ; good-pos.
  2455. bra+1 ce+1 ; just after L/R bra-ces.
  2456. bra+1s ; list of OLD values of bra+1.
  2457. mstart) ; start of a macro.
  2458. (save-excursion
  2459. ;; Each time round the following loop, we enter a successively deeper
  2460. ;; level of brace/paren nesting. (Except sometimes we "continue at
  2461. ;; the existing level".) `pa+1' is a pos inside an opening
  2462. ;; brace/paren/bracket, usually just after it.
  2463. (while
  2464. (progn
  2465. ;; Each time round the next loop moves forward over an opening then
  2466. ;; a closing brace/bracket/paren. This loop is white hot, so it
  2467. ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
  2468. ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
  2469. ;; call of `scan-lists' signals an error, which happens when there
  2470. ;; are no more b/b/p's to scan.
  2471. (c-safe
  2472. (while t
  2473. (setq pa+1 (scan-lists ren+1 1 -1) ; Into (/{/[; might signal
  2474. paren+1s (cons pa+1 paren+1s))
  2475. (setq ren+1 (scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
  2476. (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
  2477. (setq bra+1 pa+1))
  2478. (setcar paren+1s ren+1)))
  2479. (if (and pa+1 (> pa+1 ren+1))
  2480. ;; We've just entered a deeper nesting level.
  2481. (progn
  2482. ;; Insert the brace pair (if present) and the single open
  2483. ;; paren/brace/bracket into `c-state-cache' It cannot be
  2484. ;; inside a macro, except one around point, because of what
  2485. ;; `c-neutralize-syntax-in-CPP' has done.
  2486. (c-state-push-any-brace-pair bra+1 macro-start-or-here)
  2487. ;; Insert the opening brace/bracket/paren position.
  2488. (setq c-state-cache (cons (1- pa+1) c-state-cache))
  2489. ;; Clear admin stuff for the next more nested part of the scan.
  2490. (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
  2491. t) ; Carry on the loop
  2492. ;; All open p/b/b's at this nesting level, if any, have probably
  2493. ;; been closed by matching/mismatching ones. We're probably
  2494. ;; finished - we just need to check for having found an
  2495. ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
  2496. ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
  2497. (c-safe (setq ren+1 (scan-lists ren+1 1 1)))))) ; acts as loop control.
  2498. ;; Record the final, innermost, brace-pair if there is one.
  2499. (c-state-push-any-brace-pair bra+1 macro-start-or-here)
  2500. ;; Determine a good pos
  2501. (while (and (setq paren+1 (car paren+1s))
  2502. (> (if (> paren+1 macro-start-or-here)
  2503. paren+1
  2504. (goto-char paren+1)
  2505. (setq mstart (and (c-beginning-of-macro)
  2506. (point)))
  2507. (or mstart paren+1))
  2508. here-bol))
  2509. (setq paren+1s (cdr paren+1s)))
  2510. (cond
  2511. ((and paren+1 mstart)
  2512. (min paren+1 mstart))
  2513. (paren+1)
  2514. (t from)))))
  2515. (defun c-remove-stale-state-cache (good-pos pps-point)
  2516. ;; Remove stale entries from the `c-cache-state', i.e. those which will
  2517. ;; not be in it when it is amended for position (point-max).
  2518. ;; Additionally, the "outermost" open-brace entry before (point-max)
  2519. ;; will be converted to a cons if the matching close-brace is scanned.
  2520. ;;
  2521. ;; GOOD-POS is a "maximal" "safe position" - there must be no open
  2522. ;; parens/braces/brackets between GOOD-POS and (point-max).
  2523. ;;
  2524. ;; As a second thing, calculate the result of parse-partial-sexp at
  2525. ;; PPS-POINT, w.r.t. GOOD-POS. The motivation here is that
  2526. ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
  2527. ;; adjust it to get outside a string/comment. (Sorry about this! The code
  2528. ;; needs to be FAST).
  2529. ;;
  2530. ;; Return a list (GOOD-POS SCAN-BACK-POS PPS-STATE), where
  2531. ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
  2532. ;; to be good (we aim for this to be as high as possible);
  2533. ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
  2534. ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
  2535. ;; position to scan backwards from.
  2536. ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
  2537. (save-restriction
  2538. (narrow-to-region 1 (point-max))
  2539. (save-excursion
  2540. (let* ((in-macro-start ; start of macro containing (point-max) or nil.
  2541. (save-excursion
  2542. (goto-char (point-max))
  2543. (and (c-beginning-of-macro)
  2544. (point))))
  2545. (good-pos-actual-macro-start ; Start of macro containing good-pos
  2546. ; or nil
  2547. (and (< good-pos (point-max))
  2548. (save-excursion
  2549. (goto-char good-pos)
  2550. (and (c-beginning-of-macro)
  2551. (point)))))
  2552. (good-pos-actual-macro-end ; End of this macro, (maybe
  2553. ; (point-max)), or nil.
  2554. (and good-pos-actual-macro-start
  2555. (save-excursion
  2556. (goto-char good-pos-actual-macro-start)
  2557. (c-end-of-macro)
  2558. (point))))
  2559. pps-state ; Will be 9 or 10 elements long.
  2560. pos
  2561. upper-lim ; ,beyond which `c-state-cache' entries are removed
  2562. scan-back-pos
  2563. pair-beg pps-point-state target-depth)
  2564. ;; Remove entries beyond (point-max). Also remove any entries inside
  2565. ;; a macro, unless (point-max) is in the same macro.
  2566. (setq upper-lim
  2567. (if (or (null c-state-old-cpp-beg)
  2568. (and (> (point-max) c-state-old-cpp-beg)
  2569. (< (point-max) c-state-old-cpp-end)))
  2570. (point-max)
  2571. (min (point-max) c-state-old-cpp-beg)))
  2572. (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
  2573. (setq c-state-cache (cdr c-state-cache)))
  2574. ;; If `upper-lim' is inside the last recorded brace pair, remove its
  2575. ;; RBrace and indicate we'll need to search backwards for a previous
  2576. ;; brace pair.
  2577. (when (and c-state-cache
  2578. (consp (car c-state-cache))
  2579. (> (cdar c-state-cache) upper-lim))
  2580. (setcar c-state-cache (caar c-state-cache))
  2581. (setq scan-back-pos (car c-state-cache)))
  2582. ;; The next loop jumps forward out of a nested level of parens each
  2583. ;; time round; the corresponding elements in `c-state-cache' are
  2584. ;; removed. `pos' is just after the brace-pair or the open paren at
  2585. ;; (car c-state-cache). There can be no open parens/braces/brackets
  2586. ;; between `good-pos'/`good-pos-actual-macro-start' and (point-max),
  2587. ;; due to the interface spec to this function.
  2588. (setq pos (if (and good-pos-actual-macro-end
  2589. (not (eq good-pos-actual-macro-start
  2590. in-macro-start)))
  2591. (1+ good-pos-actual-macro-end) ; get outside the macro as
  2592. ; marked by a `category' text property.
  2593. good-pos))
  2594. (goto-char pos)
  2595. (while (and c-state-cache
  2596. (< (point) (point-max)))
  2597. (cond
  2598. ((null pps-state) ; first time through
  2599. (setq target-depth -1))
  2600. ((eq (car pps-state) target-depth) ; found closing ),},]
  2601. (setq target-depth (1- (car pps-state))))
  2602. ;; Do nothing when we've merely reached pps-point.
  2603. )
  2604. ;; Scan!
  2605. (setq pps-state
  2606. (parse-partial-sexp
  2607. (point) (if (< (point) pps-point) pps-point (point-max))
  2608. target-depth
  2609. nil pps-state))
  2610. (if (= (point) pps-point)
  2611. (setq pps-point-state pps-state))
  2612. (when (eq (car pps-state) target-depth)
  2613. (setq pos (point)) ; POS is now just after an R-paren/brace.
  2614. (cond
  2615. ((and (consp (car c-state-cache))
  2616. (eq (point) (cdar c-state-cache)))
  2617. ;; We've just moved out of the paren pair containing the brace-pair
  2618. ;; at (car c-state-cache). `pair-beg' is where the open paren is,
  2619. ;; and is potentially where the open brace of a cons in
  2620. ;; c-state-cache will be.
  2621. (setq pair-beg (car-safe (cdr c-state-cache))
  2622. c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
  2623. ((numberp (car c-state-cache))
  2624. (setq pair-beg (car c-state-cache)
  2625. c-state-cache (cdr c-state-cache))) ; remove this
  2626. ; containing Lparen
  2627. ((numberp (cadr c-state-cache))
  2628. (setq pair-beg (cadr c-state-cache)
  2629. c-state-cache (cddr c-state-cache))) ; Remove a paren pair
  2630. ; together with enclosed brace pair.
  2631. ;; (t nil) ; Ignore an unmated Rparen.
  2632. )))
  2633. (if (< (point) pps-point)
  2634. (setq pps-state (parse-partial-sexp (point) pps-point
  2635. nil nil ; TARGETDEPTH, STOPBEFORE
  2636. pps-state)))
  2637. ;; If the last paren pair we moved out of was actually a brace pair,
  2638. ;; insert it into `c-state-cache'.
  2639. (when (and pair-beg (eq (char-after pair-beg) ?{))
  2640. (if (consp (car-safe c-state-cache))
  2641. (setq c-state-cache (cdr c-state-cache)))
  2642. (setq c-state-cache (cons (cons pair-beg pos)
  2643. c-state-cache)))
  2644. (list pos scan-back-pos pps-state)))))
  2645. (defun c-remove-stale-state-cache-backwards (here cache-pos)
  2646. ;; Strip stale elements of `c-state-cache' by moving backwards through the
  2647. ;; buffer, and inform the caller of the scenario detected.
  2648. ;;
  2649. ;; HERE is the position we're setting `c-state-cache' for.
  2650. ;; CACHE-POS is just after the latest recorded position in `c-state-cache'
  2651. ;; before HERE, or a position at or near point-min which isn't in a
  2652. ;; literal.
  2653. ;;
  2654. ;; This function must only be called only when (> `c-state-cache-good-pos'
  2655. ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
  2656. ;; optimized to eliminate (or minimize) scanning between these two
  2657. ;; positions.
  2658. ;;
  2659. ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
  2660. ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
  2661. ;; could become so after missing elements are inserted into
  2662. ;; `c-state-cache'. This is JUST AFTER an opening or closing
  2663. ;; brace/paren/bracket which is already in `c-state-cache' or just before
  2664. ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
  2665. ;; before `here''s line, or the start of the literal containing it.
  2666. ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
  2667. ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
  2668. ;; to scan backwards from.
  2669. ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
  2670. ;; POS and HERE which aren't recorded in `c-state-cache'.
  2671. ;;
  2672. ;; The comments in this defun use "paren" to mean parenthesis or square
  2673. ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
  2674. ;;
  2675. ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
  2676. ;; | | | | | |
  2677. ;; CP E here D C good
  2678. (let ((pos c-state-cache-good-pos)
  2679. pa ren ; positions of "(" and ")"
  2680. dropped-cons ; whether the last element dropped from `c-state-cache'
  2681. ; was a cons (representing a brace-pair)
  2682. good-pos ; see above.
  2683. lit ; (START . END) of a literal containing some point.
  2684. here-lit-start here-lit-end ; bounds of literal containing `here'
  2685. ; or `here' itself.
  2686. here- here+ ; start/end of macro around HERE, or HERE
  2687. (here-bol (c-point 'bol here))
  2688. (too-far-back (max (- here c-state-cache-too-far) (point-min))))
  2689. ;; Remove completely irrelevant entries from `c-state-cache'.
  2690. (while (and c-state-cache
  2691. (>= (setq pa (c-state-cache-top-lparen)) here))
  2692. (setq dropped-cons (consp (car c-state-cache)))
  2693. (setq c-state-cache (cdr c-state-cache))
  2694. (setq pos pa))
  2695. ;; At this stage, (> pos here);
  2696. ;; (< (c-state-cache-top-lparen) here) (or is nil).
  2697. (cond
  2698. ((and (consp (car c-state-cache))
  2699. (> (cdar c-state-cache) here))
  2700. ;; CASE 1: The top of the cache is a brace pair which now encloses
  2701. ;; `here'. As good-pos, return the address. of the "{". Since we've no
  2702. ;; knowledge of what's inside these braces, we have no alternative but
  2703. ;; to direct the caller to scan the buffer from the opening brace.
  2704. (setq pos (caar c-state-cache))
  2705. (setcar c-state-cache pos)
  2706. (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
  2707. ; entry into a { entry, so the caller needs to
  2708. ; search for a brace pair before the {.
  2709. ;; `here' might be inside a literal. Check for this.
  2710. ((progn
  2711. (setq lit (c-state-literal-at here)
  2712. here-lit-start (or (car lit) here)
  2713. here-lit-end (or (cdr lit) here))
  2714. ;; Has `here' just "newly entered" a macro?
  2715. (save-excursion
  2716. (goto-char here-lit-start)
  2717. (if (and (c-beginning-of-macro)
  2718. (or (null c-state-old-cpp-beg)
  2719. (not (= (point) c-state-old-cpp-beg))))
  2720. (progn
  2721. (setq here- (point))
  2722. (c-end-of-macro)
  2723. (setq here+ (point)))
  2724. (setq here- here-lit-start
  2725. here+ here-lit-end)))
  2726. ;; `here' might be nested inside any depth of parens (or brackets but
  2727. ;; not braces). Scan backwards to find the outermost such opening
  2728. ;; paren, if there is one. This will be the scan position to return.
  2729. (save-restriction
  2730. (narrow-to-region cache-pos (point-max))
  2731. (setq pos (c-state-balance-parens-backwards here- here+ pos)))
  2732. nil)) ; for the cond
  2733. ((< pos here-lit-start)
  2734. ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
  2735. ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
  2736. ;; a brace pair preceding this, it will already be in `c-state-cache',
  2737. ;; unless there was a brace pair after it, i.e. there'll only be one to
  2738. ;; scan for if we've just deleted one.
  2739. (list pos (and dropped-cons pos) t)) ; Return value.
  2740. ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
  2741. ;; Further forward scanning isn't needed, but we still need to find a
  2742. ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
  2743. ((progn
  2744. (save-restriction
  2745. (narrow-to-region here-bol (point-max))
  2746. (setq pos here-lit-start)
  2747. (c-safe (while (setq pa (scan-lists pos -1 1))
  2748. (setq pos pa)))) ; might signal
  2749. nil)) ; for the cond
  2750. ((setq ren (c-safe-scan-lists pos -1 -1 too-far-back))
  2751. ;; CASE 3: After a }/)/] before `here''s BOL.
  2752. (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
  2753. (t
  2754. ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
  2755. ;; literal containing it.
  2756. (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
  2757. (list good-pos (and dropped-cons good-pos) nil)))))
  2758. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2759. ;; Externally visible routines.
  2760. (defun c-state-cache-init ()
  2761. (setq c-state-cache nil
  2762. c-state-cache-good-pos 1
  2763. c-state-nonlit-pos-cache nil
  2764. c-state-nonlit-pos-cache-limit 1
  2765. c-state-brace-pair-desert nil
  2766. c-state-point-min 1
  2767. c-state-point-min-lit-type nil
  2768. c-state-point-min-lit-start nil
  2769. c-state-min-scan-pos 1
  2770. c-state-old-cpp-beg nil
  2771. c-state-old-cpp-end nil)
  2772. (c-state-mark-point-min-literal))
  2773. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2774. ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
  2775. ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
  2776. ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
  2777. ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
  2778. ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
  2779. ;; (defun c-state-dump ()
  2780. ;; ;; For debugging.
  2781. ;; ;(message
  2782. ;; (concat
  2783. ;; (c-sc-qde c-state-cache)
  2784. ;; (c-sc-de c-state-cache-good-pos)
  2785. ;; (c-sc-qde c-state-nonlit-pos-cache)
  2786. ;; (c-sc-de c-state-nonlit-pos-cache-limit)
  2787. ;; (c-sc-qde c-state-brace-pair-desert)
  2788. ;; (c-sc-de c-state-point-min)
  2789. ;; (c-sc-de c-state-point-min-lit-type)
  2790. ;; (c-sc-de c-state-point-min-lit-start)
  2791. ;; (c-sc-de c-state-min-scan-pos)
  2792. ;; (c-sc-de c-state-old-cpp-beg)
  2793. ;; (c-sc-de c-state-old-cpp-end)))
  2794. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2795. (defun c-invalidate-state-cache-1 (here)
  2796. ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
  2797. ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
  2798. ;; left in a consistent state.
  2799. ;;
  2800. ;; This is much like `c-whack-state-after', but it never changes a paren
  2801. ;; pair element into an open paren element. Doing that would mean that the
  2802. ;; new open paren wouldn't have the required preceding paren pair element.
  2803. ;;
  2804. ;; This function is called from c-after-change.
  2805. ;; The caches of non-literals:
  2806. (if (< here c-state-nonlit-pos-cache-limit)
  2807. (setq c-state-nonlit-pos-cache-limit here))
  2808. (if (< here c-state-semi-nonlit-pos-cache-limit)
  2809. (setq c-state-semi-nonlit-pos-cache-limit here))
  2810. ;; `c-state-cache':
  2811. ;; Case 1: if `here' is in a literal containing point-min, everything
  2812. ;; becomes (or is already) nil.
  2813. (if (or (null c-state-cache-good-pos)
  2814. (< here (c-state-get-min-scan-pos)))
  2815. (setq c-state-cache nil
  2816. c-state-cache-good-pos nil
  2817. c-state-min-scan-pos nil)
  2818. ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
  2819. ;; below `here'. To maintain its consistency, we may need to insert a new
  2820. ;; brace pair.
  2821. (let ((here-bol (c-point 'bol here))
  2822. too-high-pa ; recorded {/(/[ next above here, or nil.
  2823. dropped-cons ; was the last removed element a brace pair?
  2824. pa)
  2825. ;; The easy bit - knock over-the-top bits off `c-state-cache'.
  2826. (while (and c-state-cache
  2827. (>= (setq pa (c-state-cache-top-paren)) here))
  2828. (setq dropped-cons (consp (car c-state-cache))
  2829. too-high-pa (c-state-cache-top-lparen)
  2830. c-state-cache (cdr c-state-cache)))
  2831. ;; Do we need to add in an earlier brace pair, having lopped one off?
  2832. (if (and dropped-cons
  2833. (< too-high-pa (+ here c-state-cache-too-far)))
  2834. (c-append-lower-brace-pair-to-state-cache too-high-pa here-bol))
  2835. (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
  2836. (c-state-get-min-scan-pos)))))
  2837. ;; The brace-pair desert marker:
  2838. (when (car c-state-brace-pair-desert)
  2839. (if (< here (car c-state-brace-pair-desert))
  2840. (setq c-state-brace-pair-desert nil)
  2841. (if (< here (cdr c-state-brace-pair-desert))
  2842. (setcdr c-state-brace-pair-desert here)))))
  2843. (defun c-parse-state-1 ()
  2844. ;; Find and record all noteworthy parens between some good point earlier in
  2845. ;; the file and point. That good point is at least the beginning of the
  2846. ;; top-level construct we are in, or the beginning of the preceding
  2847. ;; top-level construct if we aren't in one.
  2848. ;;
  2849. ;; The returned value is a list of the noteworthy parens with the last one
  2850. ;; first. If an element in the list is an integer, it's the position of an
  2851. ;; open paren (of any type) which has not been closed before the point. If
  2852. ;; an element is a cons, it gives the position of a closed BRACE paren
  2853. ;; pair[*]; the car is the start brace position and the cdr is the position
  2854. ;; following the closing brace. Only the last closed brace paren pair
  2855. ;; before each open paren and before the point is recorded, and thus the
  2856. ;; state never contains two cons elements in succession. When a close brace
  2857. ;; has no matching open brace (e.g., the matching brace is outside the
  2858. ;; visible region), it is not represented in the returned value.
  2859. ;;
  2860. ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
  2861. ;; This defun explicitly treats mismatching parens/braces/brackets as
  2862. ;; matching. It is the open brace which makes it a "brace" pair.
  2863. ;;
  2864. ;; If POINT is within a macro, open parens and brace pairs within
  2865. ;; THIS macro MIGHT be recorded. This depends on whether their
  2866. ;; syntactic properties have been suppressed by
  2867. ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
  2868. ;;
  2869. ;; Currently no characters which are given paren syntax with the
  2870. ;; syntax-table property are recorded, i.e. angle bracket arglist
  2871. ;; parens are never present here. Note that this might change.
  2872. ;;
  2873. ;; BUG: This function doesn't cope entirely well with unbalanced
  2874. ;; parens in macros. (2008-12-11: this has probably been resolved
  2875. ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
  2876. ;; following case the brace before the macro isn't balanced with the
  2877. ;; one after it:
  2878. ;;
  2879. ;; {
  2880. ;; #define X {
  2881. ;; }
  2882. ;;
  2883. ;; Note to maintainers: this function DOES get called with point
  2884. ;; within comments and strings, so don't assume it doesn't!
  2885. ;;
  2886. ;; This function might do hidden buffer changes.
  2887. (let* ((here (point))
  2888. (here-bopl (c-point 'bopl))
  2889. strategy ; 'forward, 'backward etc..
  2890. ;; Candidate positions to start scanning from:
  2891. cache-pos ; highest position below HERE already existing in
  2892. ; cache (or 1).
  2893. good-pos
  2894. start-point
  2895. bopl-state
  2896. res
  2897. scan-backward-pos scan-forward-p) ; used for 'backward.
  2898. ;; If POINT-MIN has changed, adjust the cache
  2899. (unless (= (point-min) c-state-point-min)
  2900. (c-renarrow-state-cache))
  2901. ;; Strategy?
  2902. (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
  2903. strategy (car res)
  2904. cache-pos (cadr res)
  2905. start-point (nth 2 res))
  2906. (when (eq strategy 'BOD)
  2907. (setq c-state-cache nil
  2908. c-state-cache-good-pos start-point))
  2909. ;; SCAN!
  2910. (save-restriction
  2911. (cond
  2912. ((memq strategy '(forward BOD))
  2913. (narrow-to-region (point-min) here)
  2914. (setq res (c-remove-stale-state-cache start-point here-bopl))
  2915. (setq cache-pos (car res)
  2916. scan-backward-pos (cadr res)
  2917. bopl-state (car (cddr res))) ; will be nil if (< here-bopl
  2918. ; start-point)
  2919. (if scan-backward-pos
  2920. (c-append-lower-brace-pair-to-state-cache scan-backward-pos))
  2921. (setq good-pos
  2922. (c-append-to-state-cache cache-pos))
  2923. (setq c-state-cache-good-pos
  2924. (if (and bopl-state
  2925. (< good-pos (- here c-state-cache-too-far)))
  2926. (c-state-cache-non-literal-place here-bopl bopl-state)
  2927. good-pos)))
  2928. ((eq strategy 'backward)
  2929. (setq res (c-remove-stale-state-cache-backwards here cache-pos)
  2930. good-pos (car res)
  2931. scan-backward-pos (cadr res)
  2932. scan-forward-p (car (cddr res)))
  2933. (if scan-backward-pos
  2934. (c-append-lower-brace-pair-to-state-cache
  2935. scan-backward-pos))
  2936. (setq c-state-cache-good-pos
  2937. (if scan-forward-p
  2938. (progn (narrow-to-region (point-min) here)
  2939. (c-append-to-state-cache good-pos))
  2940. good-pos)))
  2941. (t ; (eq strategy 'IN-LIT)
  2942. (setq c-state-cache nil
  2943. c-state-cache-good-pos nil)))))
  2944. c-state-cache)
  2945. (defun c-invalidate-state-cache (here)
  2946. ;; This is a wrapper over `c-invalidate-state-cache-1'.
  2947. ;;
  2948. ;; It suppresses the syntactic effect of the < and > (template) brackets and
  2949. ;; of all parens in preprocessor constructs, except for any such construct
  2950. ;; containing point. We can then call `c-invalidate-state-cache-1' without
  2951. ;; worrying further about macros and template delimiters.
  2952. (c-with-<->-as-parens-suppressed
  2953. (if (and c-state-old-cpp-beg
  2954. (< c-state-old-cpp-beg here))
  2955. (c-with-all-but-one-cpps-commented-out
  2956. c-state-old-cpp-beg
  2957. (min c-state-old-cpp-end here)
  2958. (c-invalidate-state-cache-1 here))
  2959. (c-with-cpps-commented-out
  2960. (c-invalidate-state-cache-1 here)))))
  2961. (defun c-parse-state ()
  2962. ;; This is a wrapper over `c-parse-state-1'. See that function for a
  2963. ;; description of the functionality and return value.
  2964. ;;
  2965. ;; It suppresses the syntactic effect of the < and > (template) brackets and
  2966. ;; of all parens in preprocessor constructs, except for any such construct
  2967. ;; containing point. We can then call `c-parse-state-1' without worrying
  2968. ;; further about macros and template delimiters.
  2969. (let (here-cpp-beg here-cpp-end)
  2970. (save-excursion
  2971. (when (c-beginning-of-macro)
  2972. (setq here-cpp-beg (point))
  2973. (unless
  2974. (> (setq here-cpp-end (c-syntactic-end-of-macro))
  2975. here-cpp-beg)
  2976. (setq here-cpp-beg nil here-cpp-end nil))))
  2977. ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
  2978. ;; subsystem.
  2979. (prog1
  2980. (c-with-<->-as-parens-suppressed
  2981. (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
  2982. (c-with-all-but-one-cpps-commented-out
  2983. here-cpp-beg here-cpp-end
  2984. (c-parse-state-1))
  2985. (c-with-cpps-commented-out
  2986. (c-parse-state-1))))
  2987. (setq c-state-old-cpp-beg (and here-cpp-beg (copy-marker here-cpp-beg t))
  2988. c-state-old-cpp-end (and here-cpp-end (copy-marker here-cpp-end t)))
  2989. )))
  2990. ;; Debug tool to catch cache inconsistencies. This is called from
  2991. ;; 000tests.el.
  2992. (defvar c-debug-parse-state nil)
  2993. (unless (fboundp 'c-real-parse-state)
  2994. (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
  2995. (cc-bytecomp-defun c-real-parse-state)
  2996. (defvar c-parse-state-state nil)
  2997. (defun c-record-parse-state-state ()
  2998. (setq c-parse-state-state
  2999. (mapcar
  3000. (lambda (arg)
  3001. (cons arg (symbol-value arg)))
  3002. '(c-state-cache
  3003. c-state-cache-good-pos
  3004. c-state-nonlit-pos-cache
  3005. c-state-nonlit-pos-cache-limit
  3006. c-state-brace-pair-desert
  3007. c-state-point-min
  3008. c-state-point-min-lit-type
  3009. c-state-point-min-lit-start
  3010. c-state-min-scan-pos
  3011. c-state-old-cpp-beg
  3012. c-state-old-cpp-end))))
  3013. (defun c-replay-parse-state-state ()
  3014. (message
  3015. (concat "(setq "
  3016. (mapconcat
  3017. (lambda (arg)
  3018. (format "%s %s%s" (car arg) (if (atom (cdr arg)) "" "'") (cdr arg)))
  3019. c-parse-state-state " ")
  3020. ")")))
  3021. (defun c-debug-parse-state ()
  3022. (let ((here (point)) (res1 (c-real-parse-state)) res2)
  3023. (let ((c-state-cache nil)
  3024. (c-state-cache-good-pos 1)
  3025. (c-state-nonlit-pos-cache nil)
  3026. (c-state-nonlit-pos-cache-limit 1)
  3027. (c-state-brace-pair-desert nil)
  3028. (c-state-point-min 1)
  3029. (c-state-point-min-lit-type nil)
  3030. (c-state-point-min-lit-start nil)
  3031. (c-state-min-scan-pos 1)
  3032. (c-state-old-cpp-beg nil)
  3033. (c-state-old-cpp-end nil))
  3034. (setq res2 (c-real-parse-state)))
  3035. (unless (equal res1 res2)
  3036. ;; The cache can actually go further back due to the ad-hoc way
  3037. ;; the first paren is found, so try to whack off a bit of its
  3038. ;; start before complaining.
  3039. ;; (save-excursion
  3040. ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
  3041. ;; (c-beginning-of-defun-1)
  3042. ;; (while (not (or (bobp) (eq (char-after) ?{)))
  3043. ;; (c-beginning-of-defun-1))
  3044. ;; (unless (equal (c-whack-state-before (point) res1) res2)
  3045. ;; (message (concat "c-parse-state inconsistency at %s: "
  3046. ;; "using cache: %s, from scratch: %s")
  3047. ;; here res1 res2)))
  3048. (message (concat "c-parse-state inconsistency at %s: "
  3049. "using cache: %s, from scratch: %s")
  3050. here res1 res2)
  3051. (message "Old state:")
  3052. (c-replay-parse-state-state))
  3053. (c-record-parse-state-state)
  3054. res1))
  3055. (defun c-toggle-parse-state-debug (&optional arg)
  3056. (interactive "P")
  3057. (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
  3058. (fset 'c-parse-state (symbol-function (if c-debug-parse-state
  3059. 'c-debug-parse-state
  3060. 'c-real-parse-state)))
  3061. (c-keep-region-active))
  3062. (when c-debug-parse-state
  3063. (c-toggle-parse-state-debug 1))
  3064. (defun c-whack-state-before (bufpos paren-state)
  3065. ;; Whack off any state information from PAREN-STATE which lies
  3066. ;; before BUFPOS. Not destructive on PAREN-STATE.
  3067. (let* ((newstate (list nil))
  3068. (ptr newstate)
  3069. car)
  3070. (while paren-state
  3071. (setq car (car paren-state)
  3072. paren-state (cdr paren-state))
  3073. (if (< (if (consp car) (car car) car) bufpos)
  3074. (setq paren-state nil)
  3075. (setcdr ptr (list car))
  3076. (setq ptr (cdr ptr))))
  3077. (cdr newstate)))
  3078. (defun c-whack-state-after (bufpos paren-state)
  3079. ;; Whack off any state information from PAREN-STATE which lies at or
  3080. ;; after BUFPOS. Not destructive on PAREN-STATE.
  3081. (catch 'done
  3082. (while paren-state
  3083. (let ((car (car paren-state)))
  3084. (if (consp car)
  3085. ;; just check the car, because in a balanced brace
  3086. ;; expression, it must be impossible for the corresponding
  3087. ;; close brace to be before point, but the open brace to
  3088. ;; be after.
  3089. (if (<= bufpos (car car))
  3090. nil ; whack it off
  3091. (if (< bufpos (cdr car))
  3092. ;; its possible that the open brace is before
  3093. ;; bufpos, but the close brace is after. In that
  3094. ;; case, convert this to a non-cons element. The
  3095. ;; rest of the state is before bufpos, so we're
  3096. ;; done.
  3097. (throw 'done (cons (car car) (cdr paren-state)))
  3098. ;; we know that both the open and close braces are
  3099. ;; before bufpos, so we also know that everything else
  3100. ;; on state is before bufpos.
  3101. (throw 'done paren-state)))
  3102. (if (<= bufpos car)
  3103. nil ; whack it off
  3104. ;; it's before bufpos, so everything else should too.
  3105. (throw 'done paren-state)))
  3106. (setq paren-state (cdr paren-state)))
  3107. nil)))
  3108. (defun c-most-enclosing-brace (paren-state &optional bufpos)
  3109. ;; Return the bufpos of the innermost enclosing open paren before
  3110. ;; bufpos, or nil if none was found.
  3111. (let (enclosingp)
  3112. (or bufpos (setq bufpos 134217727))
  3113. (while paren-state
  3114. (setq enclosingp (car paren-state)
  3115. paren-state (cdr paren-state))
  3116. (if (or (consp enclosingp)
  3117. (>= enclosingp bufpos))
  3118. (setq enclosingp nil)
  3119. (setq paren-state nil)))
  3120. enclosingp))
  3121. (defun c-least-enclosing-brace (paren-state)
  3122. ;; Return the bufpos of the outermost enclosing open paren, or nil
  3123. ;; if none was found.
  3124. (let (pos elem)
  3125. (while paren-state
  3126. (setq elem (car paren-state)
  3127. paren-state (cdr paren-state))
  3128. (if (integerp elem)
  3129. (setq pos elem)))
  3130. pos))
  3131. (defun c-safe-position (bufpos paren-state)
  3132. ;; Return the closest "safe" position recorded on PAREN-STATE that
  3133. ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
  3134. ;; contain any. Return nil if BUFPOS is nil, which is useful to
  3135. ;; find the closest limit before a given limit that might be nil.
  3136. ;;
  3137. ;; A "safe" position is a position at or after a recorded open
  3138. ;; paren, or after a recorded close paren. The returned position is
  3139. ;; thus either the first position after a close brace, or the first
  3140. ;; position after an enclosing paren, or at the enclosing paren in
  3141. ;; case BUFPOS is immediately after it.
  3142. (when bufpos
  3143. (let (elem)
  3144. (catch 'done
  3145. (while paren-state
  3146. (setq elem (car paren-state))
  3147. (if (consp elem)
  3148. (cond ((< (cdr elem) bufpos)
  3149. (throw 'done (cdr elem)))
  3150. ((< (car elem) bufpos)
  3151. ;; See below.
  3152. (throw 'done (min (1+ (car elem)) bufpos))))
  3153. (if (< elem bufpos)
  3154. ;; elem is the position at and not after the opening paren, so
  3155. ;; we can go forward one more step unless it's equal to
  3156. ;; bufpos. This is useful in some cases avoid an extra paren
  3157. ;; level between the safe position and bufpos.
  3158. (throw 'done (min (1+ elem) bufpos))))
  3159. (setq paren-state (cdr paren-state)))))))
  3160. (defun c-beginning-of-syntax ()
  3161. ;; This is used for `font-lock-beginning-of-syntax-function'. It
  3162. ;; goes to the closest previous point that is known to be outside
  3163. ;; any string literal or comment. `c-state-cache' is used if it has
  3164. ;; a position in the vicinity.
  3165. (let* ((paren-state c-state-cache)
  3166. elem
  3167. (pos (catch 'done
  3168. ;; Note: Similar code in `c-safe-position'. The
  3169. ;; difference is that we accept a safe position at
  3170. ;; the point and don't bother to go forward past open
  3171. ;; parens.
  3172. (while paren-state
  3173. (setq elem (car paren-state))
  3174. (if (consp elem)
  3175. (cond ((<= (cdr elem) (point))
  3176. (throw 'done (cdr elem)))
  3177. ((<= (car elem) (point))
  3178. (throw 'done (car elem))))
  3179. (if (<= elem (point))
  3180. (throw 'done elem)))
  3181. (setq paren-state (cdr paren-state)))
  3182. (point-min))))
  3183. (if (> pos (- (point) 4000))
  3184. (goto-char pos)
  3185. ;; The position is far back. Try `c-beginning-of-defun-1'
  3186. ;; (although we can't be entirely sure it will go to a position
  3187. ;; outside a comment or string in current emacsen). FIXME:
  3188. ;; Consult `syntax-ppss' here.
  3189. (c-beginning-of-defun-1)
  3190. (if (< (point) pos)
  3191. (goto-char pos)))))
  3192. ;; Tools for scanning identifiers and other tokens.
  3193. (defun c-on-identifier ()
  3194. "Return non-nil if the point is on or directly after an identifier.
  3195. Keywords are recognized and not considered identifiers. If an
  3196. identifier is detected, the returned value is its starting position.
  3197. If an identifier ends at the point and another begins at it \(can only
  3198. happen in Pike) then the point for the preceding one is returned.
  3199. Note that this function might do hidden buffer changes. See the
  3200. comment at the start of cc-engine.el for more info."
  3201. ;; FIXME: Shouldn't this function handle "operator" in C++?
  3202. (save-excursion
  3203. (skip-syntax-backward "w_")
  3204. (or
  3205. ;; Check for a normal (non-keyword) identifier.
  3206. (and (looking-at c-symbol-start)
  3207. (not (looking-at c-keywords-regexp))
  3208. (point))
  3209. (when (c-major-mode-is 'pike-mode)
  3210. ;; Handle the `<operator> syntax in Pike.
  3211. (let ((pos (point)))
  3212. (skip-chars-backward "-!%&*+/<=>^|~[]()")
  3213. (and (if (< (skip-chars-backward "`") 0)
  3214. t
  3215. (goto-char pos)
  3216. (eq (char-after) ?\`))
  3217. (looking-at c-symbol-key)
  3218. (>= (match-end 0) pos)
  3219. (point))))
  3220. ;; Handle the "operator +" syntax in C++.
  3221. (when (and c-overloadable-operators-regexp
  3222. (= (c-backward-token-2 0) 0))
  3223. (cond ((and (looking-at c-overloadable-operators-regexp)
  3224. (or (not c-opt-op-identifier-prefix)
  3225. (and (= (c-backward-token-2 1) 0)
  3226. (looking-at c-opt-op-identifier-prefix))))
  3227. (point))
  3228. ((save-excursion
  3229. (and c-opt-op-identifier-prefix
  3230. (looking-at c-opt-op-identifier-prefix)
  3231. (= (c-forward-token-2 1) 0)
  3232. (looking-at c-overloadable-operators-regexp)))
  3233. (point))))
  3234. )))
  3235. (defsubst c-simple-skip-symbol-backward ()
  3236. ;; If the point is at the end of a symbol then skip backward to the
  3237. ;; beginning of it. Don't move otherwise. Return non-nil if point
  3238. ;; moved.
  3239. ;;
  3240. ;; This function might do hidden buffer changes.
  3241. (or (< (skip-syntax-backward "w_") 0)
  3242. (and (c-major-mode-is 'pike-mode)
  3243. ;; Handle the `<operator> syntax in Pike.
  3244. (let ((pos (point)))
  3245. (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
  3246. (< (skip-chars-backward "`") 0)
  3247. (looking-at c-symbol-key)
  3248. (>= (match-end 0) pos))
  3249. t
  3250. (goto-char pos)
  3251. nil)))))
  3252. (defun c-beginning-of-current-token (&optional back-limit)
  3253. ;; Move to the beginning of the current token. Do not move if not
  3254. ;; in the middle of one. BACK-LIMIT may be used to bound the
  3255. ;; backward search; if given it's assumed to be at the boundary
  3256. ;; between two tokens. Return non-nil if the point is moved, nil
  3257. ;; otherwise.
  3258. ;;
  3259. ;; This function might do hidden buffer changes.
  3260. (let ((start (point)))
  3261. (if (looking-at "\\w\\|\\s_")
  3262. (skip-syntax-backward "w_" back-limit)
  3263. (when (< (skip-syntax-backward ".()" back-limit) 0)
  3264. (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
  3265. (match-end 0))
  3266. ;; `c-nonsymbol-token-regexp' should always match
  3267. ;; since we've skipped backward over punctuator
  3268. ;; or paren syntax, but consume one char in case
  3269. ;; it doesn't so that we don't leave point before
  3270. ;; some earlier incorrect token.
  3271. (1+ (point)))))
  3272. (if (<= pos start)
  3273. (goto-char pos))))))
  3274. (< (point) start)))
  3275. (defun c-end-of-current-token (&optional back-limit)
  3276. ;; Move to the end of the current token. Do not move if not in the
  3277. ;; middle of one. BACK-LIMIT may be used to bound the backward
  3278. ;; search; if given it's assumed to be at the boundary between two
  3279. ;; tokens. Return non-nil if the point is moved, nil otherwise.
  3280. ;;
  3281. ;; This function might do hidden buffer changes.
  3282. (let ((start (point)))
  3283. (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
  3284. (skip-syntax-forward "w_"))
  3285. ((< (skip-syntax-backward ".()" back-limit) 0)
  3286. (while (progn
  3287. (if (looking-at c-nonsymbol-token-regexp)
  3288. (goto-char (match-end 0))
  3289. ;; `c-nonsymbol-token-regexp' should always match since
  3290. ;; we've skipped backward over punctuator or paren
  3291. ;; syntax, but move forward in case it doesn't so that
  3292. ;; we don't leave point earlier than we started with.
  3293. (forward-char))
  3294. (< (point) start)))))
  3295. (> (point) start)))
  3296. (defconst c-jump-syntax-balanced
  3297. (if (memq 'gen-string-delim c-emacs-features)
  3298. "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
  3299. "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
  3300. (defconst c-jump-syntax-unbalanced
  3301. (if (memq 'gen-string-delim c-emacs-features)
  3302. "\\w\\|\\s_\\|\\s\"\\|\\s|"
  3303. "\\w\\|\\s_\\|\\s\""))
  3304. (defun c-forward-token-2 (&optional count balanced limit)
  3305. "Move forward by tokens.
  3306. A token is defined as all symbols and identifiers which aren't
  3307. syntactic whitespace \(note that multicharacter tokens like \"==\" are
  3308. treated properly). Point is always either left at the beginning of a
  3309. token or not moved at all. COUNT specifies the number of tokens to
  3310. move; a negative COUNT moves in the opposite direction. A COUNT of 0
  3311. moves to the next token beginning only if not already at one. If
  3312. BALANCED is true, move over balanced parens, otherwise move into them.
  3313. Also, if BALANCED is true, never move out of an enclosing paren.
  3314. LIMIT sets the limit for the movement and defaults to the point limit.
  3315. The case when LIMIT is set in the middle of a token, comment or macro
  3316. is handled correctly, i.e. the point won't be left there.
  3317. Return the number of tokens left to move \(positive or negative). If
  3318. BALANCED is true, a move over a balanced paren counts as one. Note
  3319. that if COUNT is 0 and no appropriate token beginning is found, 1 will
  3320. be returned. Thus, a return value of 0 guarantees that point is at
  3321. the requested position and a return value less \(without signs) than
  3322. COUNT guarantees that point is at the beginning of some token.
  3323. Note that this function might do hidden buffer changes. See the
  3324. comment at the start of cc-engine.el for more info."
  3325. (or count (setq count 1))
  3326. (if (< count 0)
  3327. (- (c-backward-token-2 (- count) balanced limit))
  3328. (let ((jump-syntax (if balanced
  3329. c-jump-syntax-balanced
  3330. c-jump-syntax-unbalanced))
  3331. (last (point))
  3332. (prev (point)))
  3333. (if (zerop count)
  3334. ;; If count is zero we should jump if in the middle of a token.
  3335. (c-end-of-current-token))
  3336. (save-restriction
  3337. (if limit (narrow-to-region (point-min) limit))
  3338. (if (/= (point)
  3339. (progn (c-forward-syntactic-ws) (point)))
  3340. ;; Skip whitespace. Count this as a move if we did in
  3341. ;; fact move.
  3342. (setq count (max (1- count) 0)))
  3343. (if (eobp)
  3344. ;; Moved out of bounds. Make sure the returned count isn't zero.
  3345. (progn
  3346. (if (zerop count) (setq count 1))
  3347. (goto-char last))
  3348. ;; Use `condition-case' to avoid having the limit tests
  3349. ;; inside the loop.
  3350. (condition-case nil
  3351. (while (and
  3352. (> count 0)
  3353. (progn
  3354. (setq last (point))
  3355. (cond ((looking-at jump-syntax)
  3356. (goto-char (scan-sexps (point) 1))
  3357. t)
  3358. ((looking-at c-nonsymbol-token-regexp)
  3359. (goto-char (match-end 0))
  3360. t)
  3361. ;; `c-nonsymbol-token-regexp' above should always
  3362. ;; match if there are correct tokens. Try to
  3363. ;; widen to see if the limit was set in the
  3364. ;; middle of one, else fall back to treating
  3365. ;; the offending thing as a one character token.
  3366. ((and limit
  3367. (save-restriction
  3368. (widen)
  3369. (looking-at c-nonsymbol-token-regexp)))
  3370. nil)
  3371. (t
  3372. (forward-char)
  3373. t))))
  3374. (c-forward-syntactic-ws)
  3375. (setq prev last
  3376. count (1- count)))
  3377. (error (goto-char last)))
  3378. (when (eobp)
  3379. (goto-char prev)
  3380. (setq count (1+ count)))))
  3381. count)))
  3382. (defun c-backward-token-2 (&optional count balanced limit)
  3383. "Move backward by tokens.
  3384. See `c-forward-token-2' for details."
  3385. (or count (setq count 1))
  3386. (if (< count 0)
  3387. (- (c-forward-token-2 (- count) balanced limit))
  3388. (or limit (setq limit (point-min)))
  3389. (let ((jump-syntax (if balanced
  3390. c-jump-syntax-balanced
  3391. c-jump-syntax-unbalanced))
  3392. (last (point)))
  3393. (if (zerop count)
  3394. ;; The count is zero so try to skip to the beginning of the
  3395. ;; current token.
  3396. (if (> (point)
  3397. (progn (c-beginning-of-current-token) (point)))
  3398. (if (< (point) limit)
  3399. ;; The limit is inside the same token, so return 1.
  3400. (setq count 1))
  3401. ;; We're not in the middle of a token. If there's
  3402. ;; whitespace after the point then we must move backward,
  3403. ;; so set count to 1 in that case.
  3404. (and (looking-at c-syntactic-ws-start)
  3405. ;; If we're looking at a '#' that might start a cpp
  3406. ;; directive then we have to do a more elaborate check.
  3407. (or (/= (char-after) ?#)
  3408. (not c-opt-cpp-prefix)
  3409. (save-excursion
  3410. (and (= (point)
  3411. (progn (beginning-of-line)
  3412. (looking-at "[ \t]*")
  3413. (match-end 0)))
  3414. (or (bobp)
  3415. (progn (backward-char)
  3416. (not (eq (char-before) ?\\)))))))
  3417. (setq count 1))))
  3418. ;; Use `condition-case' to avoid having to check for buffer
  3419. ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
  3420. (condition-case nil
  3421. (while (and
  3422. (> count 0)
  3423. (progn
  3424. (c-backward-syntactic-ws)
  3425. (backward-char)
  3426. (if (looking-at jump-syntax)
  3427. (goto-char (scan-sexps (1+ (point)) -1))
  3428. ;; This can be very inefficient if there's a long
  3429. ;; sequence of operator tokens without any separation.
  3430. ;; That doesn't happen in practice, anyway.
  3431. (c-beginning-of-current-token))
  3432. (>= (point) limit)))
  3433. (setq last (point)
  3434. count (1- count)))
  3435. (error (goto-char last)))
  3436. (if (< (point) limit)
  3437. (goto-char last))
  3438. count)))
  3439. (defun c-forward-token-1 (&optional count balanced limit)
  3440. "Like `c-forward-token-2' but doesn't treat multicharacter operator
  3441. tokens like \"==\" as single tokens, i.e. all sequences of symbol
  3442. characters are jumped over character by character. This function is
  3443. for compatibility only; it's only a wrapper over `c-forward-token-2'."
  3444. (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
  3445. (c-forward-token-2 count balanced limit)))
  3446. (defun c-backward-token-1 (&optional count balanced limit)
  3447. "Like `c-backward-token-2' but doesn't treat multicharacter operator
  3448. tokens like \"==\" as single tokens, i.e. all sequences of symbol
  3449. characters are jumped over character by character. This function is
  3450. for compatibility only; it's only a wrapper over `c-backward-token-2'."
  3451. (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
  3452. (c-backward-token-2 count balanced limit)))
  3453. ;; Tools for doing searches restricted to syntactically relevant text.
  3454. (defun c-syntactic-re-search-forward (regexp &optional bound noerror
  3455. paren-level not-inside-token
  3456. lookbehind-submatch)
  3457. "Like `re-search-forward', but only report matches that are found
  3458. in syntactically significant text. I.e. matches in comments, macros
  3459. or string literals are ignored. The start point is assumed to be
  3460. outside any comment, macro or string literal, or else the content of
  3461. that region is taken as syntactically significant text.
  3462. If PAREN-LEVEL is non-nil, an additional restriction is added to
  3463. ignore matches in nested paren sexps. The search will also not go
  3464. outside the current list sexp, which has the effect that if the point
  3465. should be moved to BOUND when no match is found \(i.e. NOERROR is
  3466. neither nil nor t), then it will be at the closing paren if the end of
  3467. the current list sexp is encountered first.
  3468. If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
  3469. ignored. Things like multicharacter operators and special symbols
  3470. \(e.g. \"`()\" in Pike) are handled but currently not floating point
  3471. constants.
  3472. If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
  3473. subexpression in REGEXP. The end of that submatch is used as the
  3474. position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
  3475. isn't used or if that subexpression didn't match then the start
  3476. position of the whole match is used instead. The \"look behind\"
  3477. subexpression is never tested before the starting position, so it
  3478. might be a good idea to include \\=\\= as a match alternative in it.
  3479. Optimization note: Matches might be missed if the \"look behind\"
  3480. subexpression can match the end of nonwhite syntactic whitespace,
  3481. i.e. the end of comments or cpp directives. This since the function
  3482. skips over such things before resuming the search. It's on the other
  3483. hand not safe to assume that the \"look behind\" subexpression never
  3484. matches syntactic whitespace.
  3485. Bug: Unbalanced parens inside cpp directives are currently not handled
  3486. correctly \(i.e. they don't get ignored as they should) when
  3487. PAREN-LEVEL is set.
  3488. Note that this function might do hidden buffer changes. See the
  3489. comment at the start of cc-engine.el for more info."
  3490. (or bound (setq bound (point-max)))
  3491. (if paren-level (setq paren-level -1))
  3492. ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
  3493. (let ((start (point))
  3494. tmp
  3495. ;; Start position for the last search.
  3496. search-pos
  3497. ;; The `parse-partial-sexp' state between the start position
  3498. ;; and the point.
  3499. state
  3500. ;; The current position after the last state update. The next
  3501. ;; `parse-partial-sexp' continues from here.
  3502. (state-pos (point))
  3503. ;; The position at which to check the state and the state
  3504. ;; there. This is separate from `state-pos' since we might
  3505. ;; need to back up before doing the next search round.
  3506. check-pos check-state
  3507. ;; Last position known to end a token.
  3508. (last-token-end-pos (point-min))
  3509. ;; Set when a valid match is found.
  3510. found)
  3511. (condition-case err
  3512. (while
  3513. (and
  3514. (progn
  3515. (setq search-pos (point))
  3516. (re-search-forward regexp bound noerror))
  3517. (progn
  3518. (setq state (parse-partial-sexp
  3519. state-pos (match-beginning 0) paren-level nil state)
  3520. state-pos (point))
  3521. (if (setq check-pos (and lookbehind-submatch
  3522. (or (not paren-level)
  3523. (>= (car state) 0))
  3524. (match-end lookbehind-submatch)))
  3525. (setq check-state (parse-partial-sexp
  3526. state-pos check-pos paren-level nil state))
  3527. (setq check-pos state-pos
  3528. check-state state))
  3529. ;; NOTE: If we got a look behind subexpression and get
  3530. ;; an insignificant match in something that isn't
  3531. ;; syntactic whitespace (i.e. strings or in nested
  3532. ;; parentheses), then we can never skip more than a
  3533. ;; single character from the match start position
  3534. ;; (i.e. `state-pos' here) before continuing the
  3535. ;; search. That since the look behind subexpression
  3536. ;; might match the end of the insignificant region in
  3537. ;; the next search.
  3538. (cond
  3539. ((elt check-state 7)
  3540. ;; Match inside a line comment. Skip to eol. Use
  3541. ;; `re-search-forward' instead of `skip-chars-forward' to get
  3542. ;; the right bound behavior.
  3543. (re-search-forward "[\n\r]" bound noerror))
  3544. ((elt check-state 4)
  3545. ;; Match inside a block comment. Skip to the '*/'.
  3546. (search-forward "*/" bound noerror))
  3547. ((and (not (elt check-state 5))
  3548. (eq (char-before check-pos) ?/)
  3549. (not (c-get-char-property (1- check-pos) 'syntax-table))
  3550. (memq (char-after check-pos) '(?/ ?*)))
  3551. ;; Match in the middle of the opener of a block or line
  3552. ;; comment.
  3553. (if (= (char-after check-pos) ?/)
  3554. (re-search-forward "[\n\r]" bound noerror)
  3555. (search-forward "*/" bound noerror)))
  3556. ;; The last `parse-partial-sexp' above might have
  3557. ;; stopped short of the real check position if the end
  3558. ;; of the current sexp was encountered in paren-level
  3559. ;; mode. The checks above are always false in that
  3560. ;; case, and since they can do better skipping in
  3561. ;; lookbehind-submatch mode, we do them before
  3562. ;; checking the paren level.
  3563. ((and paren-level
  3564. (/= (setq tmp (car check-state)) 0))
  3565. ;; Check the paren level first since we're short of the
  3566. ;; syntactic checking position if the end of the
  3567. ;; current sexp was encountered by `parse-partial-sexp'.
  3568. (if (> tmp 0)
  3569. ;; Inside a nested paren sexp.
  3570. (if lookbehind-submatch
  3571. ;; See the NOTE above.
  3572. (progn (goto-char state-pos) t)
  3573. ;; Skip out of the paren quickly.
  3574. (setq state (parse-partial-sexp state-pos bound 0 nil state)
  3575. state-pos (point)))
  3576. ;; Have exited the current paren sexp.
  3577. (if noerror
  3578. (progn
  3579. ;; The last `parse-partial-sexp' call above
  3580. ;; has left us just after the closing paren
  3581. ;; in this case, so we can modify the bound
  3582. ;; to leave the point at the right position
  3583. ;; upon return.
  3584. (setq bound (1- (point)))
  3585. nil)
  3586. (signal 'search-failed (list regexp)))))
  3587. ((setq tmp (elt check-state 3))
  3588. ;; Match inside a string.
  3589. (if (or lookbehind-submatch
  3590. (not (integerp tmp)))
  3591. ;; See the NOTE above.
  3592. (progn (goto-char state-pos) t)
  3593. ;; Skip to the end of the string before continuing.
  3594. (let ((ender (make-string 1 tmp)) (continue t))
  3595. (while (if (search-forward ender bound noerror)
  3596. (progn
  3597. (setq state (parse-partial-sexp
  3598. state-pos (point) nil nil state)
  3599. state-pos (point))
  3600. (elt state 3))
  3601. (setq continue nil)))
  3602. continue)))
  3603. ((save-excursion
  3604. (save-match-data
  3605. (c-beginning-of-macro start)))
  3606. ;; Match inside a macro. Skip to the end of it.
  3607. (c-end-of-macro)
  3608. (cond ((<= (point) bound) t)
  3609. (noerror nil)
  3610. (t (signal 'search-failed (list regexp)))))
  3611. ((and not-inside-token
  3612. (or (< check-pos last-token-end-pos)
  3613. (< check-pos
  3614. (save-excursion
  3615. (goto-char check-pos)
  3616. (save-match-data
  3617. (c-end-of-current-token last-token-end-pos))
  3618. (setq last-token-end-pos (point))))))
  3619. ;; Inside a token.
  3620. (if lookbehind-submatch
  3621. ;; See the NOTE above.
  3622. (goto-char state-pos)
  3623. (goto-char (min last-token-end-pos bound))))
  3624. (t
  3625. ;; A real match.
  3626. (setq found t)
  3627. nil)))
  3628. ;; Should loop to search again, but take care to avoid
  3629. ;; looping on the same spot.
  3630. (or (/= search-pos (point))
  3631. (if (= (point) bound)
  3632. (if noerror
  3633. nil
  3634. (signal 'search-failed (list regexp)))
  3635. (forward-char)
  3636. t))))
  3637. (error
  3638. (goto-char start)
  3639. (signal (car err) (cdr err))))
  3640. ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
  3641. (if found
  3642. (progn
  3643. (goto-char (match-end 0))
  3644. (match-end 0))
  3645. ;; Search failed. Set point as appropriate.
  3646. (if (eq noerror t)
  3647. (goto-char start)
  3648. (goto-char bound))
  3649. nil)))
  3650. (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
  3651. (defsubst c-ssb-lit-begin ()
  3652. ;; Return the start of the literal point is in, or nil.
  3653. ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
  3654. ;; bound in the caller.
  3655. ;; Use `parse-partial-sexp' from a safe position down to the point to check
  3656. ;; if it's outside comments and strings.
  3657. (save-excursion
  3658. (let ((pos (point)) safe-pos state pps-end-pos)
  3659. ;; Pick a safe position as close to the point as possible.
  3660. ;;
  3661. ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
  3662. ;; position.
  3663. (while (and safe-pos-list
  3664. (> (car safe-pos-list) (point)))
  3665. (setq safe-pos-list (cdr safe-pos-list)))
  3666. (unless (setq safe-pos (car-safe safe-pos-list))
  3667. (setq safe-pos (max (or (c-safe-position
  3668. (point) (or c-state-cache
  3669. (c-parse-state)))
  3670. 0)
  3671. (point-min))
  3672. safe-pos-list (list safe-pos)))
  3673. ;; Cache positions along the way to use if we have to back up more. We
  3674. ;; cache every closing paren on the same level. If the paren cache is
  3675. ;; relevant in this region then we're typically already on the same
  3676. ;; level as the target position. Note that we might cache positions
  3677. ;; after opening parens in case safe-pos is in a nested list. That's
  3678. ;; both uncommon and harmless.
  3679. (while (progn
  3680. (setq state (parse-partial-sexp
  3681. safe-pos pos 0))
  3682. (< (point) pos))
  3683. (setq safe-pos (point)
  3684. safe-pos-list (cons safe-pos safe-pos-list)))
  3685. ;; If the state contains the start of the containing sexp we cache that
  3686. ;; position too, so that parse-partial-sexp in the next run has a bigger
  3687. ;; chance of starting at the same level as the target position and thus
  3688. ;; will get more good safe positions into the list.
  3689. (if (elt state 1)
  3690. (setq safe-pos (1+ (elt state 1))
  3691. safe-pos-list (cons safe-pos safe-pos-list)))
  3692. (if (or (elt state 3) (elt state 4))
  3693. ;; Inside string or comment. Continue search at the
  3694. ;; beginning of it.
  3695. (elt state 8)))))
  3696. (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
  3697. "Like `skip-chars-backward' but only look at syntactically relevant chars,
  3698. i.e. don't stop at positions inside syntactic whitespace or string
  3699. literals. Preprocessor directives are also ignored, with the exception
  3700. of the one that the point starts within, if any. If LIMIT is given,
  3701. it's assumed to be at a syntactically relevant position.
  3702. If PAREN-LEVEL is non-nil, the function won't stop in nested paren
  3703. sexps, and the search will also not go outside the current paren sexp.
  3704. However, if LIMIT or the buffer limit is reached inside a nested paren
  3705. then the point will be left at the limit.
  3706. Non-nil is returned if the point moved, nil otherwise.
  3707. Note that this function might do hidden buffer changes. See the
  3708. comment at the start of cc-engine.el for more info."
  3709. (let ((start (point))
  3710. state-2
  3711. ;; A list of syntactically relevant positions in descending
  3712. ;; order. It's used to avoid scanning repeatedly over
  3713. ;; potentially large regions with `parse-partial-sexp' to verify
  3714. ;; each position. Used in `c-ssb-lit-begin'
  3715. safe-pos-list
  3716. ;; The result from `c-beginning-of-macro' at the start position or the
  3717. ;; start position itself if it isn't within a macro. Evaluated on
  3718. ;; demand.
  3719. start-macro-beg
  3720. ;; The earliest position after the current one with the same paren
  3721. ;; level. Used only when `paren-level' is set.
  3722. lit-beg
  3723. (paren-level-pos (point)))
  3724. (while
  3725. (progn
  3726. ;; The next loop "tries" to find the end point each time round,
  3727. ;; loops when it hasn't succeeded.
  3728. (while
  3729. (and
  3730. (< (skip-chars-backward skip-chars limit) 0)
  3731. (let ((pos (point)) state-2 pps-end-pos)
  3732. (cond
  3733. ;; Don't stop inside a literal
  3734. ((setq lit-beg (c-ssb-lit-begin))
  3735. (goto-char lit-beg)
  3736. t)
  3737. ((and paren-level
  3738. (save-excursion
  3739. (setq state-2 (parse-partial-sexp
  3740. pos paren-level-pos -1)
  3741. pps-end-pos (point))
  3742. (/= (car state-2) 0)))
  3743. ;; Not at the right level.
  3744. (if (and (< (car state-2) 0)
  3745. ;; We stop above if we go out of a paren.
  3746. ;; Now check whether it precedes or is
  3747. ;; nested in the starting sexp.
  3748. (save-excursion
  3749. (setq state-2
  3750. (parse-partial-sexp
  3751. pps-end-pos paren-level-pos
  3752. nil nil state-2))
  3753. (< (car state-2) 0)))
  3754. ;; We've stopped short of the starting position
  3755. ;; so the hit was inside a nested list. Go up
  3756. ;; until we are at the right level.
  3757. (condition-case nil
  3758. (progn
  3759. (goto-char (scan-lists pos -1
  3760. (- (car state-2))))
  3761. (setq paren-level-pos (point))
  3762. (if (and limit (>= limit paren-level-pos))
  3763. (progn
  3764. (goto-char limit)
  3765. nil)
  3766. t))
  3767. (error
  3768. (goto-char (or limit (point-min)))
  3769. nil))
  3770. ;; The hit was outside the list at the start
  3771. ;; position. Go to the start of the list and exit.
  3772. (goto-char (1+ (elt state-2 1)))
  3773. nil))
  3774. ((c-beginning-of-macro limit)
  3775. ;; Inside a macro.
  3776. (if (< (point)
  3777. (or start-macro-beg
  3778. (setq start-macro-beg
  3779. (save-excursion
  3780. (goto-char start)
  3781. (c-beginning-of-macro limit)
  3782. (point)))))
  3783. t
  3784. ;; It's inside the same macro we started in so it's
  3785. ;; a relevant match.
  3786. (goto-char pos)
  3787. nil))))))
  3788. (> (point)
  3789. (progn
  3790. ;; Skip syntactic ws afterwards so that we don't stop at the
  3791. ;; end of a comment if `skip-chars' is something like "^/".
  3792. (c-backward-syntactic-ws)
  3793. (point)))))
  3794. ;; We might want to extend this with more useful return values in
  3795. ;; the future.
  3796. (/= (point) start)))
  3797. ;; The following is an alternative implementation of
  3798. ;; `c-syntactic-skip-backward' that uses backward movement to keep
  3799. ;; track of the syntactic context. It turned out to be generally
  3800. ;; slower than the one above which uses forward checks from earlier
  3801. ;; safe positions.
  3802. ;;
  3803. ;;(defconst c-ssb-stop-re
  3804. ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
  3805. ;; ;; stop at to avoid going into comments and literals.
  3806. ;; (concat
  3807. ;; ;; Match comment end syntax and string literal syntax. Also match
  3808. ;; ;; '/' for block comment endings (not covered by comment end
  3809. ;; ;; syntax).
  3810. ;; "\\s>\\|/\\|\\s\""
  3811. ;; (if (memq 'gen-string-delim c-emacs-features)
  3812. ;; "\\|\\s|"
  3813. ;; "")
  3814. ;; (if (memq 'gen-comment-delim c-emacs-features)
  3815. ;; "\\|\\s!"
  3816. ;; "")))
  3817. ;;
  3818. ;;(defconst c-ssb-stop-paren-re
  3819. ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
  3820. ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
  3821. ;;
  3822. ;;(defconst c-ssb-sexp-end-re
  3823. ;; ;; Regexp matching the ending syntax of a complex sexp.
  3824. ;; (concat c-string-limit-regexp "\\|\\s)"))
  3825. ;;
  3826. ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
  3827. ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
  3828. ;;i.e. don't stop at positions inside syntactic whitespace or string
  3829. ;;literals. Preprocessor directives are also ignored. However, if the
  3830. ;;point is within a comment, string literal or preprocessor directory to
  3831. ;;begin with, its contents is treated as syntactically relevant chars.
  3832. ;;If LIMIT is given, it limits the backward search and the point will be
  3833. ;;left there if no earlier position is found.
  3834. ;;
  3835. ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
  3836. ;;sexps, and the search will also not go outside the current paren sexp.
  3837. ;;However, if LIMIT or the buffer limit is reached inside a nested paren
  3838. ;;then the point will be left at the limit.
  3839. ;;
  3840. ;;Non-nil is returned if the point moved, nil otherwise.
  3841. ;;
  3842. ;;Note that this function might do hidden buffer changes. See the
  3843. ;;comment at the start of cc-engine.el for more info."
  3844. ;;
  3845. ;; (save-restriction
  3846. ;; (when limit
  3847. ;; (narrow-to-region limit (point-max)))
  3848. ;;
  3849. ;; (let ((start (point)))
  3850. ;; (catch 'done
  3851. ;; (while (let ((last-pos (point))
  3852. ;; (stop-pos (progn
  3853. ;; (skip-chars-backward skip-chars)
  3854. ;; (point))))
  3855. ;;
  3856. ;; ;; Skip back over the same region as
  3857. ;; ;; `skip-chars-backward' above, but keep to
  3858. ;; ;; syntactically relevant positions.
  3859. ;; (goto-char last-pos)
  3860. ;; (while (and
  3861. ;; ;; `re-search-backward' with a single char regexp
  3862. ;; ;; should be fast.
  3863. ;; (re-search-backward
  3864. ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
  3865. ;; stop-pos 'move)
  3866. ;;
  3867. ;; (progn
  3868. ;; (cond
  3869. ;; ((looking-at "\\s(")
  3870. ;; ;; `paren-level' is set and we've found the
  3871. ;; ;; start of the containing paren.
  3872. ;; (forward-char)
  3873. ;; (throw 'done t))
  3874. ;;
  3875. ;; ((looking-at c-ssb-sexp-end-re)
  3876. ;; ;; We're at the end of a string literal or paren
  3877. ;; ;; sexp (if `paren-level' is set).
  3878. ;; (forward-char)
  3879. ;; (condition-case nil
  3880. ;; (c-backward-sexp)
  3881. ;; (error
  3882. ;; (goto-char limit)
  3883. ;; (throw 'done t))))
  3884. ;;
  3885. ;; (t
  3886. ;; (forward-char)
  3887. ;; ;; At the end of some syntactic ws or possibly
  3888. ;; ;; after a plain '/' operator.
  3889. ;; (let ((pos (point)))
  3890. ;; (c-backward-syntactic-ws)
  3891. ;; (if (= pos (point))
  3892. ;; ;; Was a plain '/' operator. Go past it.
  3893. ;; (backward-char)))))
  3894. ;;
  3895. ;; (> (point) stop-pos))))
  3896. ;;
  3897. ;; ;; Now the point is either at `stop-pos' or at some
  3898. ;; ;; position further back if `stop-pos' was at a
  3899. ;; ;; syntactically irrelevant place.
  3900. ;;
  3901. ;; ;; Skip additional syntactic ws so that we don't stop
  3902. ;; ;; at the end of a comment if `skip-chars' is
  3903. ;; ;; something like "^/".
  3904. ;; (c-backward-syntactic-ws)
  3905. ;;
  3906. ;; (< (point) stop-pos))))
  3907. ;;
  3908. ;; ;; We might want to extend this with more useful return values
  3909. ;; ;; in the future.
  3910. ;; (/= (point) start))))
  3911. ;; Tools for handling comments and string literals.
  3912. (defun c-in-literal (&optional lim detect-cpp)
  3913. "Return the type of literal point is in, if any.
  3914. The return value is `c' if in a C-style comment, `c++' if in a C++
  3915. style comment, `string' if in a string literal, `pound' if DETECT-CPP
  3916. is non-nil and in a preprocessor line, or nil if somewhere else.
  3917. Optional LIM is used as the backward limit of the search. If omitted,
  3918. or nil, `c-beginning-of-defun' is used.
  3919. The last point calculated is cached if the cache is enabled, i.e. if
  3920. `c-in-literal-cache' is bound to a two element vector.
  3921. Note that this function might do hidden buffer changes. See the
  3922. comment at the start of cc-engine.el for more info."
  3923. (save-restriction
  3924. (widen)
  3925. (let* ((safe-place (c-state-semi-safe-place (point)))
  3926. (lit (c-state-pp-to-literal safe-place (point))))
  3927. (or (cadr lit)
  3928. (and detect-cpp
  3929. (save-excursion (c-beginning-of-macro))
  3930. 'pound)))))
  3931. (defun c-literal-limits (&optional lim near not-in-delimiter)
  3932. "Return a cons of the beginning and end positions of the comment or
  3933. string surrounding point (including both delimiters), or nil if point
  3934. isn't in one. If LIM is non-nil, it's used as the \"safe\" position
  3935. to start parsing from. If NEAR is non-nil, then the limits of any
  3936. literal next to point is returned. \"Next to\" means there's only
  3937. spaces and tabs between point and the literal. The search for such a
  3938. literal is done first in forward direction. If NOT-IN-DELIMITER is
  3939. non-nil, the case when point is inside a starting delimiter won't be
  3940. recognized. This only has effect for comments which have starting
  3941. delimiters with more than one character.
  3942. Note that this function might do hidden buffer changes. See the
  3943. comment at the start of cc-engine.el for more info."
  3944. (save-excursion
  3945. (let* ((pos (point))
  3946. (lim (or lim (c-state-semi-safe-place pos)))
  3947. (pp-to-lit (save-restriction
  3948. (widen)
  3949. (c-state-pp-to-literal lim pos)))
  3950. (state (car pp-to-lit))
  3951. (lit-limits (car (cddr pp-to-lit))))
  3952. (cond
  3953. (lit-limits)
  3954. ((and (not not-in-delimiter)
  3955. (not (elt state 5))
  3956. (eq (char-before) ?/)
  3957. (looking-at "[/*]")) ; FIXME!!! use c-line/block-comment-starter. 2008-09-28.
  3958. ;; We're standing in a comment starter.
  3959. (backward-char 1)
  3960. (cons (point) (progn (c-forward-single-comment) (point))))
  3961. (near
  3962. (goto-char pos)
  3963. ;; Search forward for a literal.
  3964. (skip-chars-forward " \t")
  3965. (cond
  3966. ((looking-at c-string-limit-regexp) ; String.
  3967. (cons (point) (or (c-safe (c-forward-sexp 1) (point))
  3968. (point-max))))
  3969. ((looking-at c-comment-start-regexp) ; Line or block comment.
  3970. (cons (point) (progn (c-forward-single-comment) (point))))
  3971. (t
  3972. ;; Search backward.
  3973. (skip-chars-backward " \t")
  3974. (let ((end (point)) beg)
  3975. (cond
  3976. ((save-excursion
  3977. (< (skip-syntax-backward c-string-syntax) 0)) ; String.
  3978. (setq beg (c-safe (c-backward-sexp 1) (point))))
  3979. ((and (c-safe (forward-char -2) t)
  3980. (looking-at "*/"))
  3981. ;; Block comment. Due to the nature of line
  3982. ;; comments, they will always be covered by the
  3983. ;; normal case above.
  3984. (goto-char end)
  3985. (c-backward-single-comment)
  3986. ;; If LIM is bogus, beg will be bogus.
  3987. (setq beg (point))))
  3988. (if beg (cons beg end))))))
  3989. ))))
  3990. ;; In case external callers use this; it did have a docstring.
  3991. (defalias 'c-literal-limits-fast 'c-literal-limits)
  3992. (defun c-collect-line-comments (range)
  3993. "If the argument is a cons of two buffer positions (such as returned by
  3994. `c-literal-limits'), and that range contains a C++ style line comment,
  3995. then an extended range is returned that contains all adjacent line
  3996. comments (i.e. all comments that starts in the same column with no
  3997. empty lines or non-whitespace characters between them). Otherwise the
  3998. argument is returned.
  3999. Note that this function might do hidden buffer changes. See the
  4000. comment at the start of cc-engine.el for more info."
  4001. (save-excursion
  4002. (condition-case nil
  4003. (if (and (consp range) (progn
  4004. (goto-char (car range))
  4005. (looking-at c-line-comment-starter)))
  4006. (let ((col (current-column))
  4007. (beg (point))
  4008. (bopl (c-point 'bopl))
  4009. (end (cdr range)))
  4010. ;; Got to take care in the backward direction to handle
  4011. ;; comments which are preceded by code.
  4012. (while (and (c-backward-single-comment)
  4013. (>= (point) bopl)
  4014. (looking-at c-line-comment-starter)
  4015. (= col (current-column)))
  4016. (setq beg (point)
  4017. bopl (c-point 'bopl)))
  4018. (goto-char end)
  4019. (while (and (progn (skip-chars-forward " \t")
  4020. (looking-at c-line-comment-starter))
  4021. (= col (current-column))
  4022. (prog1 (zerop (forward-line 1))
  4023. (setq end (point)))))
  4024. (cons beg end))
  4025. range)
  4026. (error range))))
  4027. (defun c-literal-type (range)
  4028. "Convenience function that given the result of `c-literal-limits',
  4029. returns nil or the type of literal that the range surrounds, one
  4030. of the symbols 'c, 'c++ or 'string. It's much faster than using
  4031. `c-in-literal' and is intended to be used when you need both the
  4032. type of a literal and its limits.
  4033. Note that this function might do hidden buffer changes. See the
  4034. comment at the start of cc-engine.el for more info."
  4035. (if (consp range)
  4036. (save-excursion
  4037. (goto-char (car range))
  4038. (cond ((looking-at c-string-limit-regexp) 'string)
  4039. ((or (looking-at "//") ; c++ line comment
  4040. (and (looking-at "\\s<") ; comment starter
  4041. (looking-at "#"))) ; awk comment.
  4042. 'c++)
  4043. (t 'c))) ; Assuming the range is valid.
  4044. range))
  4045. (defsubst c-determine-limit-get-base (start try-size)
  4046. ;; Get a "safe place" approximately TRY-SIZE characters before START.
  4047. ;; This doesn't preserve point.
  4048. (let* ((pos (max (- start try-size) (point-min)))
  4049. (base (c-state-semi-safe-place pos))
  4050. (s (parse-partial-sexp base pos)))
  4051. (if (or (nth 4 s) (nth 3 s)) ; comment or string
  4052. (nth 8 s)
  4053. (point))))
  4054. (defun c-determine-limit (how-far-back &optional start try-size)
  4055. ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
  4056. ;; (default point). This is done by going back further in the buffer then
  4057. ;; searching forward for literals. The position found won't be in a
  4058. ;; literal. We start searching for the sought position TRY-SIZE (default
  4059. ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
  4060. ;; :-)
  4061. (save-excursion
  4062. (let* ((start (or start (point)))
  4063. (try-size (or try-size (* 2 how-far-back)))
  4064. (base (c-determine-limit-get-base start try-size))
  4065. (pos base)
  4066. (s (parse-partial-sexp pos pos)) ; null state.
  4067. stack elt size
  4068. (count 0))
  4069. (while (< pos start)
  4070. ;; Move forward one literal each time round this loop.
  4071. ;; Move forward to the start of a comment or string.
  4072. (setq s (parse-partial-sexp
  4073. pos
  4074. start
  4075. nil ; target-depth
  4076. nil ; stop-before
  4077. s ; state
  4078. 'syntax-table)) ; stop-comment
  4079. ;; Gather details of the non-literal-bit - starting pos and size.
  4080. (setq size (- (if (or (nth 4 s) (nth 3 s))
  4081. (nth 8 s)
  4082. (point))
  4083. pos))
  4084. (if (> size 0)
  4085. (setq stack (cons (cons pos size) stack)))
  4086. ;; Move forward to the end of the comment/string.
  4087. (if (or (nth 4 s) (nth 3 s))
  4088. (setq s (parse-partial-sexp
  4089. (point)
  4090. start
  4091. nil ; target-depth
  4092. nil ; stop-before
  4093. s ; state
  4094. 'syntax-table))) ; stop-comment
  4095. (setq pos (point)))
  4096. ;; Now try and find enough non-literal characters recorded on the stack.
  4097. ;; Go back one recorded literal each time round this loop.
  4098. (while (and (< count how-far-back)
  4099. stack)
  4100. (setq elt (car stack)
  4101. stack (cdr stack))
  4102. (setq count (+ count (cdr elt))))
  4103. ;; Have we found enough yet?
  4104. (cond
  4105. ((>= count how-far-back)
  4106. (+ (car elt) (- count how-far-back)))
  4107. ((eq base (point-min))
  4108. (point-min))
  4109. (t
  4110. (c-determine-limit (- how-far-back count) base try-size))))))
  4111. (defun c-determine-+ve-limit (how-far &optional start-pos)
  4112. ;; Return a buffer position about HOW-FAR non-literal characters forward
  4113. ;; from START-POS (default point), which must not be inside a literal.
  4114. (save-excursion
  4115. (let ((pos (or start-pos (point)))
  4116. (count how-far)
  4117. (s (parse-partial-sexp (point) (point)))) ; null state
  4118. (while (and (not (eobp))
  4119. (> count 0))
  4120. ;; Scan over counted characters.
  4121. (setq s (parse-partial-sexp
  4122. pos
  4123. (min (+ pos count) (point-max))
  4124. nil ; target-depth
  4125. nil ; stop-before
  4126. s ; state
  4127. 'syntax-table)) ; stop-comment
  4128. (setq count (- count (- (point) pos) 1)
  4129. pos (point))
  4130. ;; Scan over literal characters.
  4131. (if (nth 8 s)
  4132. (setq s (parse-partial-sexp
  4133. pos
  4134. (point-max)
  4135. nil ; target-depth
  4136. nil ; stop-before
  4137. s ; state
  4138. 'syntax-table) ; stop-comment
  4139. pos (point))))
  4140. (point))))
  4141. ;; `c-find-decl-spots' and accompanying stuff.
  4142. ;; Variables used in `c-find-decl-spots' to cache the search done for
  4143. ;; the first declaration in the last call. When that function starts,
  4144. ;; it needs to back up over syntactic whitespace to look at the last
  4145. ;; token before the region being searched. That can sometimes cause
  4146. ;; moves back and forth over a quite large region of comments and
  4147. ;; macros, which would be repeated for each changed character when
  4148. ;; we're called during fontification, since font-lock refontifies the
  4149. ;; current line for each change. Thus it's worthwhile to cache the
  4150. ;; first match.
  4151. ;;
  4152. ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
  4153. ;; the syntactic whitespace less or equal to some start position.
  4154. ;; There's no cached value if it's nil.
  4155. ;;
  4156. ;; `c-find-decl-match-pos' is the match position if
  4157. ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
  4158. ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
  4159. (defvar c-find-decl-syntactic-pos nil)
  4160. (make-variable-buffer-local 'c-find-decl-syntactic-pos)
  4161. (defvar c-find-decl-match-pos nil)
  4162. (make-variable-buffer-local 'c-find-decl-match-pos)
  4163. (defsubst c-invalidate-find-decl-cache (change-min-pos)
  4164. (and c-find-decl-syntactic-pos
  4165. (< change-min-pos c-find-decl-syntactic-pos)
  4166. (setq c-find-decl-syntactic-pos nil)))
  4167. ; (defface c-debug-decl-spot-face
  4168. ; '((t (:background "Turquoise")))
  4169. ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
  4170. ; (defface c-debug-decl-sws-face
  4171. ; '((t (:background "Khaki")))
  4172. ; "Debug face to mark the syntactic whitespace between the declaration
  4173. ; spots and the preceding token end.")
  4174. (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
  4175. (when (facep 'c-debug-decl-spot-face)
  4176. `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
  4177. (c-debug-add-face (max match-pos (point-min)) decl-pos
  4178. 'c-debug-decl-sws-face)
  4179. (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
  4180. 'c-debug-decl-spot-face))))
  4181. (defmacro c-debug-remove-decl-spot-faces (beg end)
  4182. (when (facep 'c-debug-decl-spot-face)
  4183. `(c-save-buffer-state ()
  4184. (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
  4185. (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
  4186. (defmacro c-find-decl-prefix-search ()
  4187. ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
  4188. ;; but it contains lots of free variables that refer to things
  4189. ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
  4190. ;; if there is a match, otherwise at `cfd-limit'.
  4191. ;;
  4192. ;; This macro might do hidden buffer changes.
  4193. '(progn
  4194. ;; Find the next property match position if we haven't got one already.
  4195. (unless cfd-prop-match
  4196. (save-excursion
  4197. (while (progn
  4198. (goto-char (next-single-property-change
  4199. (point) 'c-type nil cfd-limit))
  4200. (and (< (point) cfd-limit)
  4201. (not (eq (c-get-char-property (1- (point)) 'c-type)
  4202. 'c-decl-end)))))
  4203. (setq cfd-prop-match (point))))
  4204. ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
  4205. ;; got one already.
  4206. (unless cfd-re-match
  4207. (if (> cfd-re-match-end (point))
  4208. (goto-char cfd-re-match-end))
  4209. (while (if (setq cfd-re-match-end
  4210. (re-search-forward c-decl-prefix-or-start-re
  4211. cfd-limit 'move))
  4212. ;; Match. Check if it's inside a comment or string literal.
  4213. (c-got-face-at
  4214. (if (setq cfd-re-match (match-end 1))
  4215. ;; Matched the end of a token preceding a decl spot.
  4216. (progn
  4217. (goto-char cfd-re-match)
  4218. (1- cfd-re-match))
  4219. ;; Matched a token that start a decl spot.
  4220. (goto-char (match-beginning 0))
  4221. (point))
  4222. c-literal-faces)
  4223. ;; No match. Finish up and exit the loop.
  4224. (setq cfd-re-match cfd-limit)
  4225. nil)
  4226. ;; Skip out of comments and string literals.
  4227. (while (progn
  4228. (goto-char (next-single-property-change
  4229. (point) 'face nil cfd-limit))
  4230. (and (< (point) cfd-limit)
  4231. (c-got-face-at (point) c-literal-faces)))))
  4232. ;; If we matched at the decl start, we have to back up over the
  4233. ;; preceding syntactic ws to set `cfd-match-pos' and to catch
  4234. ;; any decl spots in the syntactic ws.
  4235. (unless cfd-re-match
  4236. (c-backward-syntactic-ws)
  4237. (setq cfd-re-match (point))))
  4238. ;; Choose whichever match is closer to the start.
  4239. (if (< cfd-re-match cfd-prop-match)
  4240. (setq cfd-match-pos cfd-re-match
  4241. cfd-re-match nil)
  4242. (setq cfd-match-pos cfd-prop-match
  4243. cfd-prop-match nil))
  4244. (goto-char cfd-match-pos)
  4245. (when (< cfd-match-pos cfd-limit)
  4246. ;; Skip forward past comments only so we don't skip macros.
  4247. (c-forward-comments)
  4248. ;; Set the position to continue at. We can avoid going over
  4249. ;; the comments skipped above a second time, but it's possible
  4250. ;; that the comment skipping has taken us past `cfd-prop-match'
  4251. ;; since the property might be used inside comments.
  4252. (setq cfd-continue-pos (if cfd-prop-match
  4253. (min cfd-prop-match (point))
  4254. (point))))))
  4255. (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
  4256. ;; Call CFD-FUN for each possible spot for a declaration, cast or
  4257. ;; label from the point to CFD-LIMIT.
  4258. ;;
  4259. ;; CFD-FUN is called with point at the start of the spot. It's passed two
  4260. ;; arguments: The first is the end position of the token preceding the spot,
  4261. ;; or 0 for the implicit match at bob. The second is a flag that is t when
  4262. ;; the match is inside a macro. Point should be moved forward by at least
  4263. ;; one token.
  4264. ;;
  4265. ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
  4266. ;; it should return non-nil to ensure that the next search will find them.
  4267. ;;
  4268. ;; Such a spot is:
  4269. ;; o The first token after bob.
  4270. ;; o The first token after the end of submatch 1 in
  4271. ;; `c-decl-prefix-or-start-re' when that submatch matches.
  4272. ;; o The start of each `c-decl-prefix-or-start-re' match when
  4273. ;; submatch 1 doesn't match.
  4274. ;; o The first token after the end of each occurrence of the
  4275. ;; `c-type' text property with the value `c-decl-end', provided
  4276. ;; `c-type-decl-end-used' is set.
  4277. ;;
  4278. ;; Only a spot that match CFD-DECL-RE and whose face is in the
  4279. ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
  4280. ;; check is disabled if CFD-FACE-CHECKLIST is nil.
  4281. ;;
  4282. ;; If the match is inside a macro then the buffer is narrowed to the
  4283. ;; end of it, so that CFD-FUN can investigate the following tokens
  4284. ;; without matching something that begins inside a macro and ends
  4285. ;; outside it. It's to avoid this work that the CFD-DECL-RE and
  4286. ;; CFD-FACE-CHECKLIST checks exist.
  4287. ;;
  4288. ;; The spots are visited approximately in order from top to bottom.
  4289. ;; It's however the positions where `c-decl-prefix-or-start-re'
  4290. ;; matches and where `c-decl-end' properties are found that are in
  4291. ;; order. Since the spots often are at the following token, they
  4292. ;; might be visited out of order insofar as more spots are reported
  4293. ;; later on within the syntactic whitespace between the match
  4294. ;; positions and their spots.
  4295. ;;
  4296. ;; It's assumed that comments and strings are fontified in the
  4297. ;; searched range.
  4298. ;;
  4299. ;; This is mainly used in fontification, and so has an elaborate
  4300. ;; cache to handle repeated calls from the same start position; see
  4301. ;; the variables above.
  4302. ;;
  4303. ;; All variables in this function begin with `cfd-' to avoid name
  4304. ;; collision with the (dynamically bound) variables used in CFD-FUN.
  4305. ;;
  4306. ;; This function might do hidden buffer changes.
  4307. (let ((cfd-start-pos (point))
  4308. (cfd-buffer-end (point-max))
  4309. ;; The end of the token preceding the decl spot last found
  4310. ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
  4311. ;; no match.
  4312. cfd-re-match
  4313. ;; The end position of the last `c-decl-prefix-or-start-re'
  4314. ;; match. If this is greater than `cfd-continue-pos', the
  4315. ;; next regexp search is started here instead.
  4316. (cfd-re-match-end (point-min))
  4317. ;; The end of the last `c-decl-end' found by
  4318. ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
  4319. ;; match. If searching for the property isn't needed then we
  4320. ;; disable it by setting it to `cfd-limit' directly.
  4321. (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
  4322. ;; The end of the token preceding the decl spot last found by
  4323. ;; `c-find-decl-prefix-search'. 0 for the implicit match at
  4324. ;; bob. `cfd-limit' if there's no match. In other words,
  4325. ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
  4326. (cfd-match-pos cfd-limit)
  4327. ;; The position to continue searching at.
  4328. cfd-continue-pos
  4329. ;; The position of the last "real" token we've stopped at.
  4330. ;; This can be greater than `cfd-continue-pos' when we get
  4331. ;; hits inside macros or at `c-decl-end' positions inside
  4332. ;; comments.
  4333. (cfd-token-pos 0)
  4334. ;; The end position of the last entered macro.
  4335. (cfd-macro-end 0))
  4336. ;; Initialize by finding a syntactically relevant start position
  4337. ;; before the point, and do the first `c-decl-prefix-or-start-re'
  4338. ;; search unless we're at bob.
  4339. (let (start-in-literal start-in-macro syntactic-pos)
  4340. ;; Must back up a bit since we look for the end of the previous
  4341. ;; statement or declaration, which is earlier than the first
  4342. ;; returned match.
  4343. (cond
  4344. ;; First we need to move to a syntactically relevant position.
  4345. ;; Begin by backing out of comment or string literals.
  4346. ((and
  4347. (when (c-got-face-at (point) c-literal-faces)
  4348. ;; Try to use the faces to back up to the start of the
  4349. ;; literal. FIXME: What if the point is on a declaration
  4350. ;; inside a comment?
  4351. (while (and (not (bobp))
  4352. (c-got-face-at (1- (point)) c-literal-faces))
  4353. (goto-char (previous-single-property-change
  4354. (point) 'face nil (point-min))))
  4355. ;; XEmacs doesn't fontify the quotes surrounding string
  4356. ;; literals.
  4357. (and (featurep 'xemacs)
  4358. (eq (get-text-property (point) 'face)
  4359. 'font-lock-string-face)
  4360. (not (bobp))
  4361. (progn (backward-char)
  4362. (not (looking-at c-string-limit-regexp)))
  4363. (forward-char))
  4364. ;; Don't trust the literal to contain only literal faces
  4365. ;; (the font lock package might not have fontified the
  4366. ;; start of it at all, for instance) so check that we have
  4367. ;; arrived at something that looks like a start or else
  4368. ;; resort to `c-literal-limits'.
  4369. (unless (looking-at c-literal-start-regexp)
  4370. (let ((range (c-literal-limits)))
  4371. (if range (goto-char (car range)))))
  4372. (setq start-in-literal (point)))
  4373. ;; The start is in a literal. If the limit is in the same
  4374. ;; one we don't have to find a syntactic position etc. We
  4375. ;; only check that if the limit is at or before bonl to save
  4376. ;; time; it covers the by far most common case when font-lock
  4377. ;; refontifies the current line only.
  4378. (<= cfd-limit (c-point 'bonl cfd-start-pos))
  4379. (save-excursion
  4380. (goto-char cfd-start-pos)
  4381. (while (progn
  4382. (goto-char (next-single-property-change
  4383. (point) 'face nil cfd-limit))
  4384. (and (< (point) cfd-limit)
  4385. (c-got-face-at (point) c-literal-faces))))
  4386. (= (point) cfd-limit)))
  4387. ;; Completely inside a literal. Set up variables to trig the
  4388. ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
  4389. ;; find a suitable start position.
  4390. (setq cfd-continue-pos start-in-literal))
  4391. ;; Check if the region might be completely inside a macro, to
  4392. ;; optimize that like the completely-inside-literal above.
  4393. ((save-excursion
  4394. (and (= (forward-line 1) 0)
  4395. (bolp) ; forward-line has funny behavior at eob.
  4396. (>= (point) cfd-limit)
  4397. (progn (backward-char)
  4398. (eq (char-before) ?\\))))
  4399. ;; (Maybe) completely inside a macro. Only need to trig the
  4400. ;; (< cfd-continue-pos cfd-start-pos) case below to make it
  4401. ;; set things up.
  4402. (setq cfd-continue-pos (1- cfd-start-pos)
  4403. start-in-macro t))
  4404. (t
  4405. ;; Back out of any macro so we don't miss any declaration
  4406. ;; that could follow after it.
  4407. (when (c-beginning-of-macro)
  4408. (setq start-in-macro t))
  4409. ;; Now we're at a proper syntactically relevant position so we
  4410. ;; can use the cache. But first clear it if it applied
  4411. ;; further down.
  4412. (c-invalidate-find-decl-cache cfd-start-pos)
  4413. (setq syntactic-pos (point))
  4414. (unless (eq syntactic-pos c-find-decl-syntactic-pos)
  4415. ;; Don't have to do this if the cache is relevant here,
  4416. ;; typically if the same line is refontified again. If
  4417. ;; we're just some syntactic whitespace further down we can
  4418. ;; still use the cache to limit the skipping.
  4419. (c-backward-syntactic-ws c-find-decl-syntactic-pos))
  4420. ;; If we hit `c-find-decl-syntactic-pos' and
  4421. ;; `c-find-decl-match-pos' is set then we install the cached
  4422. ;; values. If we hit `c-find-decl-syntactic-pos' and
  4423. ;; `c-find-decl-match-pos' is nil then we know there's no decl
  4424. ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
  4425. ;; and so we can continue the search from this point. If we
  4426. ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
  4427. ;; the right spot to begin searching anyway.
  4428. (if (and (eq (point) c-find-decl-syntactic-pos)
  4429. c-find-decl-match-pos)
  4430. (setq cfd-match-pos c-find-decl-match-pos
  4431. cfd-continue-pos syntactic-pos)
  4432. (setq c-find-decl-syntactic-pos syntactic-pos)
  4433. (when (if (bobp)
  4434. ;; Always consider bob a match to get the first
  4435. ;; declaration in the file. Do this separately instead of
  4436. ;; letting `c-decl-prefix-or-start-re' match bob, so that
  4437. ;; regexp always can consume at least one character to
  4438. ;; ensure that we won't get stuck in an infinite loop.
  4439. (setq cfd-re-match 0)
  4440. (backward-char)
  4441. (c-beginning-of-current-token)
  4442. (< (point) cfd-limit))
  4443. ;; Do an initial search now. In the bob case above it's
  4444. ;; only done to search for a `c-decl-end' spot.
  4445. (c-find-decl-prefix-search))
  4446. (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
  4447. cfd-match-pos)))))
  4448. ;; Advance `cfd-continue-pos' if it's before the start position.
  4449. ;; The closest continue position that might have effect at or
  4450. ;; after the start depends on what we started in. This also
  4451. ;; finds a suitable start position in the special cases when the
  4452. ;; region is completely within a literal or macro.
  4453. (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
  4454. (cond
  4455. (start-in-macro
  4456. ;; If we're in a macro then it's the closest preceding token
  4457. ;; in the macro. Check this before `start-in-literal',
  4458. ;; since if we're inside a literal in a macro, the preceding
  4459. ;; token is earlier than any `c-decl-end' spot inside the
  4460. ;; literal (comment).
  4461. (goto-char (or start-in-literal cfd-start-pos))
  4462. ;; The only syntactic ws in macros are comments.
  4463. (c-backward-comments)
  4464. (backward-char)
  4465. (c-beginning-of-current-token))
  4466. (start-in-literal
  4467. ;; If we're in a comment it can only be the closest
  4468. ;; preceding `c-decl-end' position within that comment, if
  4469. ;; any. Go back to the beginning of such a property so that
  4470. ;; `c-find-decl-prefix-search' will find the end of it.
  4471. ;; (Can't stop at the end and install it directly on
  4472. ;; `cfd-prop-match' since that variable might be cleared
  4473. ;; after `cfd-fun' below.)
  4474. ;;
  4475. ;; Note that if the literal is a string then the property
  4476. ;; search will simply skip to the beginning of it right
  4477. ;; away.
  4478. (if (not c-type-decl-end-used)
  4479. (goto-char start-in-literal)
  4480. (goto-char cfd-start-pos)
  4481. (while (progn
  4482. (goto-char (previous-single-property-change
  4483. (point) 'c-type nil start-in-literal))
  4484. (and (> (point) start-in-literal)
  4485. (not (eq (c-get-char-property (point) 'c-type)
  4486. 'c-decl-end))))))
  4487. (when (= (point) start-in-literal)
  4488. ;; Didn't find any property inside the comment, so we can
  4489. ;; skip it entirely. (This won't skip past a string, but
  4490. ;; that'll be handled quickly by the next
  4491. ;; `c-find-decl-prefix-search' anyway.)
  4492. (c-forward-single-comment)
  4493. (if (> (point) cfd-limit)
  4494. (goto-char cfd-limit))))
  4495. (t
  4496. ;; If we started in normal code, the only match that might
  4497. ;; apply before the start is what we already got in
  4498. ;; `cfd-match-pos' so we can continue at the start position.
  4499. ;; (Note that we don't get here if the first match is below
  4500. ;; it.)
  4501. (goto-char cfd-start-pos)))
  4502. ;; Delete found matches if they are before our new continue
  4503. ;; position, so that `c-find-decl-prefix-search' won't back up
  4504. ;; to them later on.
  4505. (setq cfd-continue-pos (point))
  4506. (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
  4507. (setq cfd-re-match nil))
  4508. (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
  4509. (setq cfd-prop-match nil)))
  4510. (if syntactic-pos
  4511. ;; This is the normal case and we got a proper syntactic
  4512. ;; position. If there's a match then it's always outside
  4513. ;; macros and comments, so advance to the next token and set
  4514. ;; `cfd-token-pos'. The loop below will later go back using
  4515. ;; `cfd-continue-pos' to fix declarations inside the
  4516. ;; syntactic ws.
  4517. (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
  4518. (goto-char syntactic-pos)
  4519. (c-forward-syntactic-ws)
  4520. (and cfd-continue-pos
  4521. (< cfd-continue-pos (point))
  4522. (setq cfd-token-pos (point))))
  4523. ;; Have one of the special cases when the region is completely
  4524. ;; within a literal or macro. `cfd-continue-pos' is set to a
  4525. ;; good start position for the search, so do it.
  4526. (c-find-decl-prefix-search)))
  4527. ;; Now loop. Round what? (ACM, 2006/7/5). We already got the first match.
  4528. (while (progn
  4529. (while (and
  4530. (< cfd-match-pos cfd-limit)
  4531. (or
  4532. ;; Kludge to filter out matches on the "<" that
  4533. ;; aren't open parens, for the sake of languages
  4534. ;; that got `c-recognize-<>-arglists' set.
  4535. (and (eq (char-before cfd-match-pos) ?<)
  4536. (not (c-get-char-property (1- cfd-match-pos)
  4537. 'syntax-table)))
  4538. ;; If `cfd-continue-pos' is less or equal to
  4539. ;; `cfd-token-pos', we've got a hit inside a macro
  4540. ;; that's in the syntactic whitespace before the last
  4541. ;; "real" declaration we've checked. If they're equal
  4542. ;; we've arrived at the declaration a second time, so
  4543. ;; there's nothing to do.
  4544. (= cfd-continue-pos cfd-token-pos)
  4545. (progn
  4546. ;; If `cfd-continue-pos' is less than `cfd-token-pos'
  4547. ;; we're still searching for declarations embedded in
  4548. ;; the syntactic whitespace. In that case we need
  4549. ;; only to skip comments and not macros, since they
  4550. ;; can't be nested, and that's already been done in
  4551. ;; `c-find-decl-prefix-search'.
  4552. (when (> cfd-continue-pos cfd-token-pos)
  4553. (c-forward-syntactic-ws)
  4554. (setq cfd-token-pos (point)))
  4555. ;; Continue if the following token fails the
  4556. ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
  4557. (when (or (>= (point) cfd-limit)
  4558. (not (looking-at cfd-decl-re))
  4559. (and cfd-face-checklist
  4560. (not (c-got-face-at
  4561. (point) cfd-face-checklist))))
  4562. (goto-char cfd-continue-pos)
  4563. t)))
  4564. (< (point) cfd-limit))
  4565. (c-find-decl-prefix-search))
  4566. (< (point) cfd-limit))
  4567. (when (and
  4568. (>= (point) cfd-start-pos)
  4569. (progn
  4570. ;; Narrow to the end of the macro if we got a hit inside
  4571. ;; one, to avoid recognizing things that start inside the
  4572. ;; macro and end outside it.
  4573. (when (> cfd-match-pos cfd-macro-end)
  4574. ;; Not in the same macro as in the previous round.
  4575. (save-excursion
  4576. (goto-char cfd-match-pos)
  4577. (setq cfd-macro-end
  4578. (if (save-excursion (and (c-beginning-of-macro)
  4579. (< (point) cfd-match-pos)))
  4580. (progn (c-end-of-macro)
  4581. (point))
  4582. 0))))
  4583. (if (zerop cfd-macro-end)
  4584. t
  4585. (if (> cfd-macro-end (point))
  4586. (progn (narrow-to-region (point-min) cfd-macro-end)
  4587. t)
  4588. ;; The matched token was the last thing in the macro,
  4589. ;; so the whole match is bogus.
  4590. (setq cfd-macro-end 0)
  4591. nil))))
  4592. (c-debug-put-decl-spot-faces cfd-match-pos (point))
  4593. (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
  4594. (setq cfd-prop-match nil))
  4595. (when (/= cfd-macro-end 0)
  4596. ;; Restore limits if we did macro narrowing above.
  4597. (narrow-to-region (point-min) cfd-buffer-end)))
  4598. (goto-char cfd-continue-pos)
  4599. (if (= cfd-continue-pos cfd-limit)
  4600. (setq cfd-match-pos cfd-limit)
  4601. (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
  4602. ; cfd-match-pos, etc.
  4603. ;; A cache for found types.
  4604. ;; Buffer local variable that contains an obarray with the types we've
  4605. ;; found. If a declaration is recognized somewhere we record the
  4606. ;; fully qualified identifier in it to recognize it as a type
  4607. ;; elsewhere in the file too. This is not accurate since we do not
  4608. ;; bother with the scoping rules of the languages, but in practice the
  4609. ;; same name is seldom used as both a type and something else in a
  4610. ;; file, and we only use this as a last resort in ambiguous cases (see
  4611. ;; `c-forward-decl-or-cast-1').
  4612. ;;
  4613. ;; Not every type need be in this cache. However, things which have
  4614. ;; ceased to be types must be removed from it.
  4615. ;;
  4616. ;; Template types in C++ are added here too but with the template
  4617. ;; arglist replaced with "<>" in references or "<" for the one in the
  4618. ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
  4619. ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
  4620. ;; template specs can be fairly sized programs in themselves) and
  4621. ;; improves the hit ratio (it's a type regardless of the template
  4622. ;; args; it's just not the same type, but we're only interested in
  4623. ;; recognizing types, not telling distinct types apart). Note that
  4624. ;; template types in references are added here too; from the example
  4625. ;; above there will also be an entry "Foo<".
  4626. (defvar c-found-types nil)
  4627. (make-variable-buffer-local 'c-found-types)
  4628. (defsubst c-clear-found-types ()
  4629. ;; Clears `c-found-types'.
  4630. (setq c-found-types (make-vector 53 0)))
  4631. (defun c-add-type (from to)
  4632. ;; Add the given region as a type in `c-found-types'. If the region
  4633. ;; doesn't match an existing type but there is a type which is equal
  4634. ;; to the given one except that the last character is missing, then
  4635. ;; the shorter type is removed. That's done to avoid adding all
  4636. ;; prefixes of a type as it's being entered and font locked. This
  4637. ;; doesn't cover cases like when characters are removed from a type
  4638. ;; or added in the middle. We'd need the position of point when the
  4639. ;; font locking is invoked to solve this well.
  4640. ;;
  4641. ;; This function might do hidden buffer changes.
  4642. (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
  4643. (unless (intern-soft type c-found-types)
  4644. (unintern (substring type 0 -1) c-found-types)
  4645. (intern type c-found-types))))
  4646. (defun c-unfind-type (name)
  4647. ;; Remove the "NAME" from c-found-types, if present.
  4648. (unintern name c-found-types))
  4649. (defsubst c-check-type (from to)
  4650. ;; Return non-nil if the given region contains a type in
  4651. ;; `c-found-types'.
  4652. ;;
  4653. ;; This function might do hidden buffer changes.
  4654. (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
  4655. c-found-types))
  4656. (defun c-list-found-types ()
  4657. ;; Return all the types in `c-found-types' as a sorted list of
  4658. ;; strings.
  4659. (let (type-list)
  4660. (mapatoms (lambda (type)
  4661. (setq type-list (cons (symbol-name type)
  4662. type-list)))
  4663. c-found-types)
  4664. (sort type-list 'string-lessp)))
  4665. ;; Shut up the byte compiler.
  4666. (defvar c-maybe-stale-found-type)
  4667. (defun c-trim-found-types (beg end old-len)
  4668. ;; An after change function which, in conjunction with the info in
  4669. ;; c-maybe-stale-found-type (set in c-before-change), removes a type
  4670. ;; from `c-found-types', should this type have become stale. For
  4671. ;; example, this happens to "foo" when "foo \n bar();" becomes
  4672. ;; "foo(); \n bar();". Such stale types, if not removed, foul up
  4673. ;; the fontification.
  4674. ;;
  4675. ;; Have we, perhaps, added non-ws characters to the front/back of a found
  4676. ;; type?
  4677. (when (> end beg)
  4678. (save-excursion
  4679. (when (< end (point-max))
  4680. (goto-char end)
  4681. (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
  4682. (progn (goto-char end)
  4683. (c-end-of-current-token)))
  4684. (c-unfind-type (buffer-substring-no-properties
  4685. end (point)))))
  4686. (when (> beg (point-min))
  4687. (goto-char beg)
  4688. (if (and (c-end-of-current-token) ; only moves when we started in the middle
  4689. (progn (goto-char beg)
  4690. (c-beginning-of-current-token)))
  4691. (c-unfind-type (buffer-substring-no-properties
  4692. (point) beg))))))
  4693. (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
  4694. (cond
  4695. ;; Changing the amount of (already existing) whitespace - don't do anything.
  4696. ((and (c-partial-ws-p beg end)
  4697. (or (= beg end) ; removal of WS
  4698. (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
  4699. ;; The syntactic relationship which defined a "found type" has been
  4700. ;; destroyed.
  4701. ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
  4702. (c-unfind-type (cadr c-maybe-stale-found-type)))
  4703. ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
  4704. )))
  4705. ;; Setting and removing syntax properties on < and > in languages (C++
  4706. ;; and Java) where they can be template/generic delimiters as well as
  4707. ;; their normal meaning of "less/greater than".
  4708. ;; Normally, < and > have syntax 'punctuation'. When they are found to
  4709. ;; be delimiters, they are marked as such with the category properties
  4710. ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
  4711. ;; STRATEGY:
  4712. ;;
  4713. ;; It is impossible to determine with certainty whether a <..> pair in
  4714. ;; C++ is two comparison operators or is template delimiters, unless
  4715. ;; one duplicates a lot of a C++ compiler. For example, the following
  4716. ;; code fragment:
  4717. ;;
  4718. ;; foo (a < b, c > d) ;
  4719. ;;
  4720. ;; could be a function call with two integer parameters (each a
  4721. ;; relational expression), or it could be a constructor for class foo
  4722. ;; taking one parameter d of templated type "a < b, c >". They are
  4723. ;; somewhat easier to distinguish in Java.
  4724. ;;
  4725. ;; The strategy now (2010-01) adopted is to mark and unmark < and
  4726. ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
  4727. ;; individually when their context so indicated. This gave rise to
  4728. ;; intractable problems when one of a matching pair was deleted, or
  4729. ;; pulled into a literal.]
  4730. ;;
  4731. ;; At each buffer change, the syntax-table properties are removed in a
  4732. ;; before-change function and reapplied, when needed, in an
  4733. ;; after-change function. It is far more important that the
  4734. ;; properties get removed when they they are spurious than that they
  4735. ;; be present when wanted.
  4736. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4737. (defun c-clear-<-pair-props (&optional pos)
  4738. ;; POS (default point) is at a < character. If it is marked with
  4739. ;; open paren syntax-table text property, remove the property,
  4740. ;; together with the close paren property on the matching > (if
  4741. ;; any).
  4742. (save-excursion
  4743. (if pos
  4744. (goto-char pos)
  4745. (setq pos (point)))
  4746. (when (equal (c-get-char-property (point) 'syntax-table)
  4747. c-<-as-paren-syntax)
  4748. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4749. (c-go-list-forward))
  4750. (when (equal (c-get-char-property (1- (point)) 'syntax-table)
  4751. c->-as-paren-syntax) ; should always be true.
  4752. (c-clear-char-property (1- (point)) 'category))
  4753. (c-clear-char-property pos 'category))))
  4754. (defun c-clear->-pair-props (&optional pos)
  4755. ;; POS (default point) is at a > character. If it is marked with
  4756. ;; close paren syntax-table property, remove the property, together
  4757. ;; with the open paren property on the matching < (if any).
  4758. (save-excursion
  4759. (if pos
  4760. (goto-char pos)
  4761. (setq pos (point)))
  4762. (when (equal (c-get-char-property (point) 'syntax-table)
  4763. c->-as-paren-syntax)
  4764. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4765. (c-go-up-list-backward))
  4766. (when (equal (c-get-char-property (point) 'syntax-table)
  4767. c-<-as-paren-syntax) ; should always be true.
  4768. (c-clear-char-property (point) 'category))
  4769. (c-clear-char-property pos 'category))))
  4770. (defun c-clear-<>-pair-props (&optional pos)
  4771. ;; POS (default point) is at a < or > character. If it has an
  4772. ;; open/close paren syntax-table property, remove this property both
  4773. ;; from the current character and its partner (which will also be
  4774. ;; thusly marked).
  4775. (cond
  4776. ((eq (char-after) ?\<)
  4777. (c-clear-<-pair-props pos))
  4778. ((eq (char-after) ?\>)
  4779. (c-clear->-pair-props pos))
  4780. (t (c-benign-error
  4781. "c-clear-<>-pair-props called from wrong position"))))
  4782. (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
  4783. ;; POS (default point) is at a < character. If it is both marked
  4784. ;; with open/close paren syntax-table property, and has a matching >
  4785. ;; (also marked) which is after LIM, remove the property both from
  4786. ;; the current > and its partner. Return t when this happens, nil
  4787. ;; when it doesn't.
  4788. (save-excursion
  4789. (if pos
  4790. (goto-char pos)
  4791. (setq pos (point)))
  4792. (when (equal (c-get-char-property (point) 'syntax-table)
  4793. c-<-as-paren-syntax)
  4794. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4795. (c-go-list-forward))
  4796. (when (and (>= (point) lim)
  4797. (equal (c-get-char-property (1- (point)) 'syntax-table)
  4798. c->-as-paren-syntax)) ; should always be true.
  4799. (c-unmark-<->-as-paren (1- (point)))
  4800. (c-unmark-<->-as-paren pos))
  4801. t)))
  4802. (defun c-clear->-pair-props-if-match-before (lim &optional pos)
  4803. ;; POS (default point) is at a > character. If it is both marked
  4804. ;; with open/close paren syntax-table property, and has a matching <
  4805. ;; (also marked) which is before LIM, remove the property both from
  4806. ;; the current < and its partner. Return t when this happens, nil
  4807. ;; when it doesn't.
  4808. (save-excursion
  4809. (if pos
  4810. (goto-char pos)
  4811. (setq pos (point)))
  4812. (when (equal (c-get-char-property (point) 'syntax-table)
  4813. c->-as-paren-syntax)
  4814. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4815. (c-go-up-list-backward))
  4816. (when (and (<= (point) lim)
  4817. (equal (c-get-char-property (point) 'syntax-table)
  4818. c-<-as-paren-syntax)) ; should always be true.
  4819. (c-unmark-<->-as-paren (point))
  4820. (c-unmark-<->-as-paren pos))
  4821. t)))
  4822. ;; Set by c-common-init in cc-mode.el.
  4823. (defvar c-new-BEG)
  4824. (defvar c-new-END)
  4825. (defun c-before-change-check-<>-operators (beg end)
  4826. ;; Unmark certain pairs of "< .... >" which are currently marked as
  4827. ;; template/generic delimiters. (This marking is via syntax-table
  4828. ;; text properties).
  4829. ;;
  4830. ;; These pairs are those which are in the current "statement" (i.e.,
  4831. ;; the region between the {, }, or ; before BEG and the one after
  4832. ;; END), and which enclose any part of the interval (BEG END).
  4833. ;;
  4834. ;; Note that in C++ (?and Java), template/generic parens cannot
  4835. ;; enclose a brace or semicolon, so we use these as bounds on the
  4836. ;; region we must work on.
  4837. ;;
  4838. ;; This function is called from before-change-functions (via
  4839. ;; c-get-state-before-change-functions). Thus the buffer is widened,
  4840. ;; and point is undefined, both at entry and exit.
  4841. ;;
  4842. ;; FIXME!!! This routine ignores the possibility of macros entirely.
  4843. ;; 2010-01-29.
  4844. (save-excursion
  4845. (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
  4846. (end-lit-limits (progn (goto-char end) (c-literal-limits)))
  4847. new-beg new-end need-new-beg need-new-end)
  4848. ;; Locate the barrier before the changed region
  4849. (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
  4850. (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
  4851. (setq new-beg (point))
  4852. ;; Remove the syntax-table properties from each pertinent <...> pair.
  4853. ;; Firsly, the ones with the < before beg and > after beg.
  4854. (while (c-search-forward-char-property 'category 'c-<-as-paren-syntax beg)
  4855. (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
  4856. (setq need-new-beg t)))
  4857. ;; Locate the barrier after END.
  4858. (goto-char (if end-lit-limits (cdr end-lit-limits) end))
  4859. (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
  4860. (setq new-end (point))
  4861. ;; Remove syntax-table properties from the remaining pertinent <...>
  4862. ;; pairs, those with a > after end and < before end.
  4863. (while (c-search-backward-char-property 'category 'c->-as-paren-syntax end)
  4864. (if (c-clear->-pair-props-if-match-before end)
  4865. (setq need-new-end t)))
  4866. ;; Extend the fontification region, if needed.
  4867. (when need-new-beg
  4868. (goto-char new-beg)
  4869. (c-forward-syntactic-ws)
  4870. (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
  4871. (when need-new-end
  4872. (and (> new-end c-new-END) (setq c-new-END new-end))))))
  4873. (defun c-after-change-check-<>-operators (beg end)
  4874. ;; This is called from `after-change-functions' when
  4875. ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
  4876. ;; chars with paren syntax become part of another operator like "<<"
  4877. ;; or ">=".
  4878. ;;
  4879. ;; This function might do hidden buffer changes.
  4880. (save-excursion
  4881. (goto-char beg)
  4882. (when (or (looking-at "[<>]")
  4883. (< (skip-chars-backward "<>") 0))
  4884. (goto-char beg)
  4885. (c-beginning-of-current-token)
  4886. (when (and (< (point) beg)
  4887. (looking-at c-<>-multichar-token-regexp)
  4888. (< beg (setq beg (match-end 0))))
  4889. (while (progn (skip-chars-forward "^<>" beg)
  4890. (< (point) beg))
  4891. (c-clear-<>-pair-props)
  4892. (forward-char))))
  4893. (when (< beg end)
  4894. (goto-char end)
  4895. (when (or (looking-at "[<>]")
  4896. (< (skip-chars-backward "<>") 0))
  4897. (goto-char end)
  4898. (c-beginning-of-current-token)
  4899. (when (and (< (point) end)
  4900. (looking-at c-<>-multichar-token-regexp)
  4901. (< end (setq end (match-end 0))))
  4902. (while (progn (skip-chars-forward "^<>" end)
  4903. (< (point) end))
  4904. (c-clear-<>-pair-props)
  4905. (forward-char)))))))
  4906. ;; Handling of small scale constructs like types and names.
  4907. ;; Dynamically bound variable that instructs `c-forward-type' to also
  4908. ;; treat possible types (i.e. those that it normally returns 'maybe or
  4909. ;; 'found for) as actual types (and always return 'found for them).
  4910. ;; This means that it records them in `c-record-type-identifiers' if
  4911. ;; that is set, and that it adds them to `c-found-types'.
  4912. (defvar c-promote-possible-types nil)
  4913. ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
  4914. ;; mark up successfully parsed arglists with paren syntax properties on
  4915. ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
  4916. ;; `c-type' property of each argument separating comma.
  4917. ;;
  4918. ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
  4919. ;; all arglists for side effects (i.e. recording types), otherwise it
  4920. ;; exploits any existing paren syntax properties to quickly jump to the
  4921. ;; end of already parsed arglists.
  4922. ;;
  4923. ;; Marking up the arglists is not the default since doing that correctly
  4924. ;; depends on a proper value for `c-restricted-<>-arglists'.
  4925. (defvar c-parse-and-markup-<>-arglists nil)
  4926. ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
  4927. ;; not accept arglists that contain binary operators.
  4928. ;;
  4929. ;; This is primarily used to handle C++ template arglists. C++
  4930. ;; disambiguates them by checking whether the preceding name is a
  4931. ;; template or not. We can't do that, so we assume it is a template
  4932. ;; if it can be parsed as one. That usually works well since
  4933. ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
  4934. ;; in almost all cases would be pointless.
  4935. ;;
  4936. ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
  4937. ;; should let the comma separate the function arguments instead. And
  4938. ;; in a context where the value of the expression is taken, e.g. in
  4939. ;; "if (a < b || c > d)", it's probably not a template.
  4940. (defvar c-restricted-<>-arglists nil)
  4941. ;; Dynamically bound variables that instructs
  4942. ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
  4943. ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
  4944. ;; `c-forward-label' to record the ranges of all the type and
  4945. ;; reference identifiers they encounter. They will build lists on
  4946. ;; these variables where each element is a cons of the buffer
  4947. ;; positions surrounding each identifier. This recording is only
  4948. ;; activated when `c-record-type-identifiers' is non-nil.
  4949. ;;
  4950. ;; All known types that can't be identifiers are recorded, and also
  4951. ;; other possible types if `c-promote-possible-types' is set.
  4952. ;; Recording is however disabled inside angle bracket arglists that
  4953. ;; are encountered inside names and other angle bracket arglists.
  4954. ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
  4955. ;; instead.
  4956. ;;
  4957. ;; Only the names in C++ template style references (e.g. "tmpl" in
  4958. ;; "tmpl<a,b>::foo") are recorded as references, other references
  4959. ;; aren't handled here.
  4960. ;;
  4961. ;; `c-forward-label' records the label identifier(s) on
  4962. ;; `c-record-ref-identifiers'.
  4963. (defvar c-record-type-identifiers nil)
  4964. (defvar c-record-ref-identifiers nil)
  4965. ;; This variable will receive a cons cell of the range of the last
  4966. ;; single identifier symbol stepped over by `c-forward-name' if it's
  4967. ;; successful. This is the range that should be put on one of the
  4968. ;; record lists above by the caller. It's assigned nil if there's no
  4969. ;; such symbol in the name.
  4970. (defvar c-last-identifier-range nil)
  4971. (defmacro c-record-type-id (range)
  4972. (if (eq (car-safe range) 'cons)
  4973. ;; Always true.
  4974. `(setq c-record-type-identifiers
  4975. (cons ,range c-record-type-identifiers))
  4976. `(let ((range ,range))
  4977. (if range
  4978. (setq c-record-type-identifiers
  4979. (cons range c-record-type-identifiers))))))
  4980. (defmacro c-record-ref-id (range)
  4981. (if (eq (car-safe range) 'cons)
  4982. ;; Always true.
  4983. `(setq c-record-ref-identifiers
  4984. (cons ,range c-record-ref-identifiers))
  4985. `(let ((range ,range))
  4986. (if range
  4987. (setq c-record-ref-identifiers
  4988. (cons range c-record-ref-identifiers))))))
  4989. ;; Dynamically bound variable that instructs `c-forward-type' to
  4990. ;; record the ranges of types that only are found. Behaves otherwise
  4991. ;; like `c-record-type-identifiers'.
  4992. (defvar c-record-found-types nil)
  4993. (defmacro c-forward-keyword-prefixed-id (type)
  4994. ;; Used internally in `c-forward-keyword-clause' to move forward
  4995. ;; over a type (if TYPE is 'type) or a name (otherwise) which
  4996. ;; possibly is prefixed by keywords and their associated clauses.
  4997. ;; Try with a type/name first to not trip up on those that begin
  4998. ;; with a keyword. Return t if a known or found type is moved
  4999. ;; over. The point is clobbered if nil is returned. If range
  5000. ;; recording is enabled, the identifier is recorded on as a type
  5001. ;; if TYPE is 'type or as a reference if TYPE is 'ref.
  5002. ;;
  5003. ;; This macro might do hidden buffer changes.
  5004. `(let (res)
  5005. (while (if (setq res ,(if (eq type 'type)
  5006. `(c-forward-type)
  5007. `(c-forward-name)))
  5008. nil
  5009. (and (looking-at c-keywords-regexp)
  5010. (c-forward-keyword-clause 1))))
  5011. (when (memq res '(t known found prefix))
  5012. ,(when (eq type 'ref)
  5013. `(when c-record-type-identifiers
  5014. (c-record-ref-id c-last-identifier-range)))
  5015. t)))
  5016. (defmacro c-forward-id-comma-list (type update-safe-pos)
  5017. ;; Used internally in `c-forward-keyword-clause' to move forward
  5018. ;; over a comma separated list of types or names using
  5019. ;; `c-forward-keyword-prefixed-id'.
  5020. ;;
  5021. ;; This macro might do hidden buffer changes.
  5022. `(while (and (progn
  5023. ,(when update-safe-pos
  5024. `(setq safe-pos (point)))
  5025. (eq (char-after) ?,))
  5026. (progn
  5027. (forward-char)
  5028. (c-forward-syntactic-ws)
  5029. (c-forward-keyword-prefixed-id ,type)))))
  5030. (defun c-forward-keyword-clause (match)
  5031. ;; Submatch MATCH in the current match data is assumed to surround a
  5032. ;; token. If it's a keyword, move over it and any immediately
  5033. ;; following clauses associated with it, stopping at the start of
  5034. ;; the next token. t is returned in that case, otherwise the point
  5035. ;; stays and nil is returned. The kind of clauses that are
  5036. ;; recognized are those specified by `c-type-list-kwds',
  5037. ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
  5038. ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
  5039. ;; and `c-<>-arglist-kwds'.
  5040. ;;
  5041. ;; This function records identifier ranges on
  5042. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5043. ;; `c-record-type-identifiers' is non-nil.
  5044. ;;
  5045. ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
  5046. ;; apply directly after the keyword, the type list is moved over
  5047. ;; only when there is no unaccounted token before it (i.e. a token
  5048. ;; that isn't moved over due to some other keyword list). The
  5049. ;; identifier ranges in the list are still recorded if that should
  5050. ;; be done, though.
  5051. ;;
  5052. ;; This function might do hidden buffer changes.
  5053. (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
  5054. ;; The call to `c-forward-<>-arglist' below is made after
  5055. ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
  5056. ;; are angle bracket arglists and `c-restricted-<>-arglists'
  5057. ;; should therefore be nil.
  5058. (c-parse-and-markup-<>-arglists t)
  5059. c-restricted-<>-arglists)
  5060. (when kwd-sym
  5061. (goto-char (match-end match))
  5062. (c-forward-syntactic-ws)
  5063. (setq safe-pos (point))
  5064. (cond
  5065. ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
  5066. (c-forward-keyword-prefixed-id type))
  5067. ;; There's a type directly after a keyword in `c-type-list-kwds'.
  5068. (c-forward-id-comma-list type t))
  5069. ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
  5070. (c-forward-keyword-prefixed-id ref))
  5071. ;; There's a name directly after a keyword in `c-ref-list-kwds'.
  5072. (c-forward-id-comma-list ref t))
  5073. ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
  5074. (eq (char-after) ?\())
  5075. ;; There's an open paren after a keyword in `c-paren-any-kwds'.
  5076. (forward-char)
  5077. (when (and (setq pos (c-up-list-forward))
  5078. (eq (char-before pos) ?\)))
  5079. (when (and c-record-type-identifiers
  5080. (c-keyword-member kwd-sym 'c-paren-type-kwds))
  5081. ;; Use `c-forward-type' on every identifier we can find
  5082. ;; inside the paren, to record the types.
  5083. (while (c-syntactic-re-search-forward c-symbol-start pos t)
  5084. (goto-char (match-beginning 0))
  5085. (unless (c-forward-type)
  5086. (looking-at c-symbol-key) ; Always matches.
  5087. (goto-char (match-end 0)))))
  5088. (goto-char pos)
  5089. (c-forward-syntactic-ws)
  5090. (setq safe-pos (point))))
  5091. ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
  5092. (eq (char-after) ?<)
  5093. (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
  5094. (c-forward-syntactic-ws)
  5095. (setq safe-pos (point)))
  5096. ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
  5097. (not (looking-at c-symbol-start))
  5098. (c-safe (c-forward-sexp) t))
  5099. (c-forward-syntactic-ws)
  5100. (setq safe-pos (point))))
  5101. (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
  5102. (if (eq (char-after) ?:)
  5103. ;; If we are at the colon already, we move over the type
  5104. ;; list after it.
  5105. (progn
  5106. (forward-char)
  5107. (c-forward-syntactic-ws)
  5108. (when (c-forward-keyword-prefixed-id type)
  5109. (c-forward-id-comma-list type t)))
  5110. ;; Not at the colon, so stop here. But the identifier
  5111. ;; ranges in the type list later on should still be
  5112. ;; recorded.
  5113. (and c-record-type-identifiers
  5114. (progn
  5115. ;; If a keyword matched both one of the types above and
  5116. ;; this one, we match `c-colon-type-list-re' after the
  5117. ;; clause matched above.
  5118. (goto-char safe-pos)
  5119. (looking-at c-colon-type-list-re))
  5120. (progn
  5121. (goto-char (match-end 0))
  5122. (c-forward-syntactic-ws)
  5123. (c-forward-keyword-prefixed-id type))
  5124. ;; There's a type after the `c-colon-type-list-re' match
  5125. ;; after a keyword in `c-colon-type-list-kwds'.
  5126. (c-forward-id-comma-list type nil))))
  5127. (goto-char safe-pos)
  5128. t)))
  5129. ;; cc-mode requires cc-fonts.
  5130. (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
  5131. (defun c-forward-<>-arglist (all-types)
  5132. ;; The point is assumed to be at a "<". Try to treat it as the open
  5133. ;; paren of an angle bracket arglist and move forward to the
  5134. ;; corresponding ">". If successful, the point is left after the
  5135. ;; ">" and t is returned, otherwise the point isn't moved and nil is
  5136. ;; returned. If ALL-TYPES is t then all encountered arguments in
  5137. ;; the arglist that might be types are treated as found types.
  5138. ;;
  5139. ;; The variable `c-parse-and-markup-<>-arglists' controls how this
  5140. ;; function handles text properties on the angle brackets and argument
  5141. ;; separating commas.
  5142. ;;
  5143. ;; `c-restricted-<>-arglists' controls how lenient the template
  5144. ;; arglist recognition should be.
  5145. ;;
  5146. ;; This function records identifier ranges on
  5147. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5148. ;; `c-record-type-identifiers' is non-nil.
  5149. ;;
  5150. ;; This function might do hidden buffer changes.
  5151. (let ((start (point))
  5152. ;; If `c-record-type-identifiers' is set then activate
  5153. ;; recording of any found types that constitute an argument in
  5154. ;; the arglist.
  5155. (c-record-found-types (if c-record-type-identifiers t)))
  5156. (if (catch 'angle-bracket-arglist-escape
  5157. (setq c-record-found-types
  5158. (c-forward-<>-arglist-recur all-types)))
  5159. (progn
  5160. (when (consp c-record-found-types)
  5161. (setq c-record-type-identifiers
  5162. ;; `nconc' doesn't mind that the tail of
  5163. ;; `c-record-found-types' is t.
  5164. (nconc c-record-found-types c-record-type-identifiers)))
  5165. (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
  5166. t)
  5167. (goto-char start)
  5168. nil)))
  5169. (defun c-forward-<>-arglist-recur (all-types)
  5170. ;; Recursive part of `c-forward-<>-arglist'.
  5171. ;;
  5172. ;; This function might do hidden buffer changes.
  5173. (let ((start (point)) res pos tmp
  5174. ;; Cover this so that any recorded found type ranges are
  5175. ;; automatically lost if it turns out to not be an angle
  5176. ;; bracket arglist. It's propagated through the return value
  5177. ;; on successful completion.
  5178. (c-record-found-types c-record-found-types)
  5179. ;; List that collects the positions after the argument
  5180. ;; separating ',' in the arglist.
  5181. arg-start-pos)
  5182. ;; If the '<' has paren open syntax then we've marked it as an angle
  5183. ;; bracket arglist before, so skip to the end.
  5184. (if (and (not c-parse-and-markup-<>-arglists)
  5185. (c-get-char-property (point) 'syntax-table))
  5186. (progn
  5187. (forward-char)
  5188. (if (and (c-go-up-list-forward)
  5189. (eq (char-before) ?>))
  5190. t
  5191. ;; Got unmatched paren angle brackets. We don't clear the paren
  5192. ;; syntax properties and retry, on the basis that it's very
  5193. ;; unlikely that paren angle brackets become operators by code
  5194. ;; manipulation. It's far more likely that it doesn't match due
  5195. ;; to narrowing or some temporary change.
  5196. (goto-char start)
  5197. nil))
  5198. (forward-char) ; Forward over the opening '<'.
  5199. (unless (looking-at c-<-op-cont-regexp)
  5200. ;; go forward one non-alphanumeric character (group) per iteration of
  5201. ;; this loop.
  5202. (while (and
  5203. (progn
  5204. (c-forward-syntactic-ws)
  5205. (let ((orig-record-found-types c-record-found-types))
  5206. (when (or (and c-record-type-identifiers all-types)
  5207. (c-major-mode-is 'java-mode))
  5208. ;; All encountered identifiers are types, so set the
  5209. ;; promote flag and parse the type.
  5210. (progn
  5211. (c-forward-syntactic-ws)
  5212. (if (looking-at "\\?")
  5213. (forward-char)
  5214. (when (looking-at c-identifier-start)
  5215. (let ((c-promote-possible-types t)
  5216. (c-record-found-types t))
  5217. (c-forward-type))))
  5218. (c-forward-syntactic-ws)
  5219. (when (or (looking-at "extends")
  5220. (looking-at "super"))
  5221. (forward-word)
  5222. (c-forward-syntactic-ws)
  5223. (let ((c-promote-possible-types t)
  5224. (c-record-found-types t))
  5225. (c-forward-type)
  5226. (c-forward-syntactic-ws))))))
  5227. (setq pos (point)) ; e.g. first token inside the '<'
  5228. ;; Note: These regexps exploit the match order in \| so
  5229. ;; that "<>" is matched by "<" rather than "[^>:-]>".
  5230. (c-syntactic-re-search-forward
  5231. ;; Stop on ',', '|', '&', '+' and '-' to catch
  5232. ;; common binary operators that could be between
  5233. ;; two comparison expressions "a<b" and "c>d".
  5234. "[<;{},|+&-]\\|[>)]"
  5235. nil t t))
  5236. (cond
  5237. ((eq (char-before) ?>)
  5238. ;; Either an operator starting with '>' or the end of
  5239. ;; the angle bracket arglist.
  5240. (if (looking-at c->-op-cont-regexp)
  5241. (progn
  5242. (goto-char (match-end 0))
  5243. t) ; Continue the loop.
  5244. ;; The angle bracket arglist is finished.
  5245. (when c-parse-and-markup-<>-arglists
  5246. (while arg-start-pos
  5247. (c-put-c-type-property (1- (car arg-start-pos))
  5248. 'c-<>-arg-sep)
  5249. (setq arg-start-pos (cdr arg-start-pos)))
  5250. (c-mark-<-as-paren start)
  5251. (c-mark->-as-paren (1- (point))))
  5252. (setq res t)
  5253. nil)) ; Exit the loop.
  5254. ((eq (char-before) ?<)
  5255. ;; Either an operator starting with '<' or a nested arglist.
  5256. (setq pos (point))
  5257. (let (id-start id-end subres keyword-match)
  5258. (cond
  5259. ;; The '<' begins a multi-char operator.
  5260. ((looking-at c-<-op-cont-regexp)
  5261. (setq tmp (match-end 0))
  5262. (goto-char (match-end 0)))
  5263. ;; We're at a nested <.....>
  5264. ((progn
  5265. (setq tmp pos)
  5266. (backward-char) ; to the '<'
  5267. (and
  5268. (save-excursion
  5269. ;; There's always an identifier before an angle
  5270. ;; bracket arglist, or a keyword in `c-<>-type-kwds'
  5271. ;; or `c-<>-arglist-kwds'.
  5272. (c-backward-syntactic-ws)
  5273. (setq id-end (point))
  5274. (c-simple-skip-symbol-backward)
  5275. (when (or (setq keyword-match
  5276. (looking-at c-opt-<>-sexp-key))
  5277. (not (looking-at c-keywords-regexp)))
  5278. (setq id-start (point))))
  5279. (setq subres
  5280. (let ((c-promote-possible-types t)
  5281. (c-record-found-types t))
  5282. (c-forward-<>-arglist-recur
  5283. (and keyword-match
  5284. (c-keyword-member
  5285. (c-keyword-sym (match-string 1))
  5286. 'c-<>-type-kwds)))))))
  5287. ;; It was an angle bracket arglist.
  5288. (setq c-record-found-types subres)
  5289. ;; Record the identifier before the template as a type
  5290. ;; or reference depending on whether the arglist is last
  5291. ;; in a qualified identifier.
  5292. (when (and c-record-type-identifiers
  5293. (not keyword-match))
  5294. (if (and c-opt-identifier-concat-key
  5295. (progn
  5296. (c-forward-syntactic-ws)
  5297. (looking-at c-opt-identifier-concat-key)))
  5298. (c-record-ref-id (cons id-start id-end))
  5299. (c-record-type-id (cons id-start id-end)))))
  5300. ;; At a "less than" operator.
  5301. (t
  5302. (forward-char)
  5303. )))
  5304. t) ; carry on looping.
  5305. ((and (not c-restricted-<>-arglists)
  5306. (or (and (eq (char-before) ?&)
  5307. (not (eq (char-after) ?&)))
  5308. (eq (char-before) ?,)))
  5309. ;; Just another argument. Record the position. The
  5310. ;; type check stuff that made us stop at it is at
  5311. ;; the top of the loop.
  5312. (setq arg-start-pos (cons (point) arg-start-pos)))
  5313. (t
  5314. ;; Got a character that can't be in an angle bracket
  5315. ;; arglist argument. Abort using `throw', since
  5316. ;; it's useless to try to find a surrounding arglist
  5317. ;; if we're nested.
  5318. (throw 'angle-bracket-arglist-escape nil))))))
  5319. (if res
  5320. (or c-record-found-types t)))))
  5321. (defun c-backward-<>-arglist (all-types &optional limit)
  5322. ;; The point is assumed to be directly after a ">". Try to treat it
  5323. ;; as the close paren of an angle bracket arglist and move back to
  5324. ;; the corresponding "<". If successful, the point is left at
  5325. ;; the "<" and t is returned, otherwise the point isn't moved and
  5326. ;; nil is returned. ALL-TYPES is passed on to
  5327. ;; `c-forward-<>-arglist'.
  5328. ;;
  5329. ;; If the optional LIMIT is given, it bounds the backward search.
  5330. ;; It's then assumed to be at a syntactically relevant position.
  5331. ;;
  5332. ;; This is a wrapper around `c-forward-<>-arglist'. See that
  5333. ;; function for more details.
  5334. (let ((start (point)))
  5335. (backward-char)
  5336. (if (and (not c-parse-and-markup-<>-arglists)
  5337. (c-get-char-property (point) 'syntax-table))
  5338. (if (and (c-go-up-list-backward)
  5339. (eq (char-after) ?<))
  5340. t
  5341. ;; See corresponding note in `c-forward-<>-arglist'.
  5342. (goto-char start)
  5343. nil)
  5344. (while (progn
  5345. (c-syntactic-skip-backward "^<;{}" limit t)
  5346. (and
  5347. (if (eq (char-before) ?<)
  5348. t
  5349. ;; Stopped at bob or a char that isn't allowed in an
  5350. ;; arglist, so we've failed.
  5351. (goto-char start)
  5352. nil)
  5353. (if (> (point)
  5354. (progn (c-beginning-of-current-token)
  5355. (point)))
  5356. ;; If we moved then the "<" was part of some
  5357. ;; multicharacter token.
  5358. t
  5359. (backward-char)
  5360. (let ((beg-pos (point)))
  5361. (if (c-forward-<>-arglist all-types)
  5362. (cond ((= (point) start)
  5363. ;; Matched the arglist. Break the while.
  5364. (goto-char beg-pos)
  5365. nil)
  5366. ((> (point) start)
  5367. ;; We started from a non-paren ">" inside an
  5368. ;; arglist.
  5369. (goto-char start)
  5370. nil)
  5371. (t
  5372. ;; Matched a shorter arglist. Can be a nested
  5373. ;; one so continue looking.
  5374. (goto-char beg-pos)
  5375. t))
  5376. t))))))
  5377. (/= (point) start))))
  5378. (defun c-forward-name ()
  5379. ;; Move forward over a complete name if at the beginning of one,
  5380. ;; stopping at the next following token. A keyword, as such,
  5381. ;; doesn't count as a name. If the point is not at something that
  5382. ;; is recognized as a name then it stays put.
  5383. ;;
  5384. ;; A name could be something as simple as "foo" in C or something as
  5385. ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
  5386. ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
  5387. ;; int>::*volatile const" in C++ (this function is actually little
  5388. ;; more than a `looking-at' call in all modes except those that,
  5389. ;; like C++, have `c-recognize-<>-arglists' set).
  5390. ;;
  5391. ;; Return
  5392. ;; o - nil if no name is found;
  5393. ;; o - 'template if it's an identifier ending with an angle bracket
  5394. ;; arglist;
  5395. ;; o - 'operator of it's an operator identifier;
  5396. ;; o - t if it's some other kind of name.
  5397. ;;
  5398. ;; This function records identifier ranges on
  5399. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5400. ;; `c-record-type-identifiers' is non-nil.
  5401. ;;
  5402. ;; This function might do hidden buffer changes.
  5403. (let ((pos (point)) (start (point)) res id-start id-end
  5404. ;; Turn off `c-promote-possible-types' here since we might
  5405. ;; call `c-forward-<>-arglist' and we don't want it to promote
  5406. ;; every suspect thing in the arglist to a type. We're
  5407. ;; typically called from `c-forward-type' in this case, and
  5408. ;; the caller only wants the top level type that it finds to
  5409. ;; be promoted.
  5410. c-promote-possible-types)
  5411. (while
  5412. (and
  5413. (looking-at c-identifier-key)
  5414. (progn
  5415. ;; Check for keyword. We go to the last symbol in
  5416. ;; `c-identifier-key' first.
  5417. (goto-char (setq id-end (match-end 0)))
  5418. (c-simple-skip-symbol-backward)
  5419. (setq id-start (point))
  5420. (if (looking-at c-keywords-regexp)
  5421. (when (and (c-major-mode-is 'c++-mode)
  5422. (looking-at
  5423. (cc-eval-when-compile
  5424. (concat "\\(operator\\|\\(template\\)\\)"
  5425. "\\(" (c-lang-const c-nonsymbol-key c++)
  5426. "\\|$\\)")))
  5427. (if (match-beginning 2)
  5428. ;; "template" is only valid inside an
  5429. ;; identifier if preceded by "::".
  5430. (save-excursion
  5431. (c-backward-syntactic-ws)
  5432. (and (c-safe (backward-char 2) t)
  5433. (looking-at "::")))
  5434. t))
  5435. ;; Handle a C++ operator or template identifier.
  5436. (goto-char id-end)
  5437. (c-forward-syntactic-ws)
  5438. (cond ((eq (char-before id-end) ?e)
  5439. ;; Got "... ::template".
  5440. (let ((subres (c-forward-name)))
  5441. (when subres
  5442. (setq pos (point)
  5443. res subres))))
  5444. ((looking-at c-identifier-start)
  5445. ;; Got a cast operator.
  5446. (when (c-forward-type)
  5447. (setq pos (point)
  5448. res 'operator)
  5449. ;; Now we should match a sequence of either
  5450. ;; '*', '&' or a name followed by ":: *",
  5451. ;; where each can be followed by a sequence
  5452. ;; of `c-opt-type-modifier-key'.
  5453. (while (cond ((looking-at "[*&]")
  5454. (goto-char (match-end 0))
  5455. t)
  5456. ((looking-at c-identifier-start)
  5457. (and (c-forward-name)
  5458. (looking-at "::")
  5459. (progn
  5460. (goto-char (match-end 0))
  5461. (c-forward-syntactic-ws)
  5462. (eq (char-after) ?*))
  5463. (progn
  5464. (forward-char)
  5465. t))))
  5466. (while (progn
  5467. (c-forward-syntactic-ws)
  5468. (setq pos (point))
  5469. (looking-at c-opt-type-modifier-key))
  5470. (goto-char (match-end 1))))))
  5471. ((looking-at c-overloadable-operators-regexp)
  5472. ;; Got some other operator.
  5473. (setq c-last-identifier-range
  5474. (cons (point) (match-end 0)))
  5475. (goto-char (match-end 0))
  5476. (c-forward-syntactic-ws)
  5477. (setq pos (point)
  5478. res 'operator)))
  5479. nil)
  5480. ;; `id-start' is equal to `id-end' if we've jumped over
  5481. ;; an identifier that doesn't end with a symbol token.
  5482. ;; That can occur e.g. for Java import directives on the
  5483. ;; form "foo.bar.*".
  5484. (when (and id-start (/= id-start id-end))
  5485. (setq c-last-identifier-range
  5486. (cons id-start id-end)))
  5487. (goto-char id-end)
  5488. (c-forward-syntactic-ws)
  5489. (setq pos (point)
  5490. res t)))
  5491. (progn
  5492. (goto-char pos)
  5493. (when (or c-opt-identifier-concat-key
  5494. c-recognize-<>-arglists)
  5495. (cond
  5496. ((and c-opt-identifier-concat-key
  5497. (looking-at c-opt-identifier-concat-key))
  5498. ;; Got a concatenated identifier. This handles the
  5499. ;; cases with tricky syntactic whitespace that aren't
  5500. ;; covered in `c-identifier-key'.
  5501. (goto-char (match-end 0))
  5502. (c-forward-syntactic-ws)
  5503. t)
  5504. ((and c-recognize-<>-arglists
  5505. (eq (char-after) ?<))
  5506. ;; Maybe an angle bracket arglist.
  5507. (when (let ((c-record-type-identifiers t)
  5508. (c-record-found-types t))
  5509. (c-forward-<>-arglist nil))
  5510. (c-add-type start (1+ pos))
  5511. (c-forward-syntactic-ws)
  5512. (setq pos (point)
  5513. c-last-identifier-range nil)
  5514. (if (and c-opt-identifier-concat-key
  5515. (looking-at c-opt-identifier-concat-key))
  5516. ;; Continue if there's an identifier concatenation
  5517. ;; operator after the template argument.
  5518. (progn
  5519. (when (and c-record-type-identifiers id-start)
  5520. (c-record-ref-id (cons id-start id-end)))
  5521. (forward-char 2)
  5522. (c-forward-syntactic-ws)
  5523. t)
  5524. (when (and c-record-type-identifiers id-start)
  5525. (c-record-type-id (cons id-start id-end)))
  5526. (setq res 'template)
  5527. nil)))
  5528. )))))
  5529. (goto-char pos)
  5530. res))
  5531. (defun c-forward-type (&optional brace-block-too)
  5532. ;; Move forward over a type spec if at the beginning of one,
  5533. ;; stopping at the next following token. The keyword "typedef"
  5534. ;; isn't part of a type spec here.
  5535. ;;
  5536. ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
  5537. ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
  5538. ;; The current (2009-03-10) intention is to convert all uses of
  5539. ;; `c-forward-type' to call with this parameter set, then to
  5540. ;; eliminate it.
  5541. ;;
  5542. ;; Return
  5543. ;; o - t if it's a known type that can't be a name or other
  5544. ;; expression;
  5545. ;; o - 'known if it's an otherwise known type (according to
  5546. ;; `*-font-lock-extra-types');
  5547. ;; o - 'prefix if it's a known prefix of a type;
  5548. ;; o - 'found if it's a type that matches one in `c-found-types';
  5549. ;; o - 'maybe if it's an identifier that might be a type; or
  5550. ;; o - nil if it can't be a type (the point isn't moved then).
  5551. ;;
  5552. ;; The point is assumed to be at the beginning of a token.
  5553. ;;
  5554. ;; Note that this function doesn't skip past the brace definition
  5555. ;; that might be considered part of the type, e.g.
  5556. ;; "enum {a, b, c} foo".
  5557. ;;
  5558. ;; This function records identifier ranges on
  5559. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5560. ;; `c-record-type-identifiers' is non-nil.
  5561. ;;
  5562. ;; This function might do hidden buffer changes.
  5563. (when (and c-recognize-<>-arglists
  5564. (looking-at "<"))
  5565. (c-forward-<>-arglist t)
  5566. (c-forward-syntactic-ws))
  5567. (let ((start (point)) pos res name-res id-start id-end id-range)
  5568. ;; Skip leading type modifiers. If any are found we know it's a
  5569. ;; prefix of a type.
  5570. (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
  5571. (while (looking-at c-opt-type-modifier-key)
  5572. (goto-char (match-end 1))
  5573. (c-forward-syntactic-ws)
  5574. (setq res 'prefix)))
  5575. (cond
  5576. ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
  5577. ; "typedef".
  5578. (goto-char (match-end 1))
  5579. (c-forward-syntactic-ws)
  5580. (setq pos (point))
  5581. (setq name-res (c-forward-name))
  5582. (setq res (not (null name-res)))
  5583. (when (eq name-res t)
  5584. ;; In many languages the name can be used without the
  5585. ;; prefix, so we add it to `c-found-types'.
  5586. (c-add-type pos (point))
  5587. (when (and c-record-type-identifiers
  5588. c-last-identifier-range)
  5589. (c-record-type-id c-last-identifier-range)))
  5590. (when (and brace-block-too
  5591. (memq res '(t nil))
  5592. (eq (char-after) ?\{)
  5593. (save-excursion
  5594. (c-safe
  5595. (progn (c-forward-sexp)
  5596. (c-forward-syntactic-ws)
  5597. (setq pos (point))))))
  5598. (goto-char pos)
  5599. (setq res t))
  5600. (unless res (goto-char start))) ; invalid syntax
  5601. ((progn
  5602. (setq pos nil)
  5603. (if (looking-at c-identifier-start)
  5604. (save-excursion
  5605. (setq id-start (point)
  5606. name-res (c-forward-name))
  5607. (when name-res
  5608. (setq id-end (point)
  5609. id-range c-last-identifier-range))))
  5610. (and (cond ((looking-at c-primitive-type-key)
  5611. (setq res t))
  5612. ((c-with-syntax-table c-identifier-syntax-table
  5613. (looking-at c-known-type-key))
  5614. (setq res 'known)))
  5615. (or (not id-end)
  5616. (>= (save-excursion
  5617. (save-match-data
  5618. (goto-char (match-end 1))
  5619. (c-forward-syntactic-ws)
  5620. (setq pos (point))))
  5621. id-end)
  5622. (setq res nil))))
  5623. ;; Looking at a primitive or known type identifier. We've
  5624. ;; checked for a name first so that we don't go here if the
  5625. ;; known type match only is a prefix of another name.
  5626. (setq id-end (match-end 1))
  5627. (when (and c-record-type-identifiers
  5628. (or c-promote-possible-types (eq res t)))
  5629. (c-record-type-id (cons (match-beginning 1) (match-end 1))))
  5630. (if (and c-opt-type-component-key
  5631. (save-match-data
  5632. (looking-at c-opt-type-component-key)))
  5633. ;; There might be more keywords for the type.
  5634. (let (safe-pos)
  5635. (c-forward-keyword-clause 1)
  5636. (while (progn
  5637. (setq safe-pos (point))
  5638. (looking-at c-opt-type-component-key))
  5639. (when (and c-record-type-identifiers
  5640. (looking-at c-primitive-type-key))
  5641. (c-record-type-id (cons (match-beginning 1)
  5642. (match-end 1))))
  5643. (c-forward-keyword-clause 1))
  5644. (if (looking-at c-primitive-type-key)
  5645. (progn
  5646. (when c-record-type-identifiers
  5647. (c-record-type-id (cons (match-beginning 1)
  5648. (match-end 1))))
  5649. (c-forward-keyword-clause 1)
  5650. (setq res t))
  5651. (goto-char safe-pos)
  5652. (setq res 'prefix)))
  5653. (unless (save-match-data (c-forward-keyword-clause 1))
  5654. (if pos
  5655. (goto-char pos)
  5656. (goto-char (match-end 1))
  5657. (c-forward-syntactic-ws)))))
  5658. (name-res
  5659. (cond ((eq name-res t)
  5660. ;; A normal identifier.
  5661. (goto-char id-end)
  5662. (if (or res c-promote-possible-types)
  5663. (progn
  5664. (c-add-type id-start id-end)
  5665. (when (and c-record-type-identifiers id-range)
  5666. (c-record-type-id id-range))
  5667. (unless res
  5668. (setq res 'found)))
  5669. (setq res (if (c-check-type id-start id-end)
  5670. ;; It's an identifier that has been used as
  5671. ;; a type somewhere else.
  5672. 'found
  5673. ;; It's an identifier that might be a type.
  5674. 'maybe))))
  5675. ((eq name-res 'template)
  5676. ;; A template is a type.
  5677. (goto-char id-end)
  5678. (setq res t))
  5679. (t
  5680. ;; Otherwise it's an operator identifier, which is not a type.
  5681. (goto-char start)
  5682. (setq res nil)))))
  5683. (when res
  5684. ;; Skip trailing type modifiers. If any are found we know it's
  5685. ;; a type.
  5686. (when c-opt-type-modifier-key
  5687. (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
  5688. (goto-char (match-end 1))
  5689. (c-forward-syntactic-ws)
  5690. (setq res t)))
  5691. ;; Step over any type suffix operator. Do not let the existence
  5692. ;; of these alter the classification of the found type, since
  5693. ;; these operators typically are allowed in normal expressions
  5694. ;; too.
  5695. (when c-opt-type-suffix-key
  5696. (while (looking-at c-opt-type-suffix-key)
  5697. (goto-char (match-end 1))
  5698. (c-forward-syntactic-ws)))
  5699. (when c-opt-type-concat-key ; Only/mainly for pike.
  5700. ;; Look for a trailing operator that concatenates the type
  5701. ;; with a following one, and if so step past that one through
  5702. ;; a recursive call. Note that we don't record concatenated
  5703. ;; types in `c-found-types' - it's the component types that
  5704. ;; are recorded when appropriate.
  5705. (setq pos (point))
  5706. (let* ((c-promote-possible-types (or (memq res '(t known))
  5707. c-promote-possible-types))
  5708. ;; If we can't promote then set `c-record-found-types' so that
  5709. ;; we can merge in the types from the second part afterwards if
  5710. ;; it turns out to be a known type there.
  5711. (c-record-found-types (and c-record-type-identifiers
  5712. (not c-promote-possible-types)))
  5713. subres)
  5714. (if (and (looking-at c-opt-type-concat-key)
  5715. (progn
  5716. (goto-char (match-end 1))
  5717. (c-forward-syntactic-ws)
  5718. (setq subres (c-forward-type))))
  5719. (progn
  5720. ;; If either operand certainly is a type then both are, but we
  5721. ;; don't let the existence of the operator itself promote two
  5722. ;; uncertain types to a certain one.
  5723. (cond ((eq res t))
  5724. ((eq subres t)
  5725. (unless (eq name-res 'template)
  5726. (c-add-type id-start id-end))
  5727. (when (and c-record-type-identifiers id-range)
  5728. (c-record-type-id id-range))
  5729. (setq res t))
  5730. ((eq res 'known))
  5731. ((eq subres 'known)
  5732. (setq res 'known))
  5733. ((eq res 'found))
  5734. ((eq subres 'found)
  5735. (setq res 'found))
  5736. (t
  5737. (setq res 'maybe)))
  5738. (when (and (eq res t)
  5739. (consp c-record-found-types))
  5740. ;; Merge in the ranges of any types found by the second
  5741. ;; `c-forward-type'.
  5742. (setq c-record-type-identifiers
  5743. ;; `nconc' doesn't mind that the tail of
  5744. ;; `c-record-found-types' is t.
  5745. (nconc c-record-found-types
  5746. c-record-type-identifiers))))
  5747. (goto-char pos))))
  5748. (when (and c-record-found-types (memq res '(known found)) id-range)
  5749. (setq c-record-found-types
  5750. (cons id-range c-record-found-types))))
  5751. ;;(message "c-forward-type %s -> %s: %s" start (point) res)
  5752. res))
  5753. (defun c-forward-annotation ()
  5754. ;; Used for Java code only at the moment. Assumes point is on the
  5755. ;; @, moves forward an annotation. returns nil if there is no
  5756. ;; annotation at point.
  5757. (and (looking-at "@")
  5758. (progn (forward-char) t)
  5759. (c-forward-type)
  5760. (progn (c-forward-syntactic-ws) t)
  5761. (if (looking-at "(")
  5762. (c-go-list-forward)
  5763. t)))
  5764. ;; Handling of large scale constructs like statements and declarations.
  5765. ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
  5766. ;; defsubst or perhaps even a defun, but it contains lots of free
  5767. ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
  5768. (defmacro c-fdoc-shift-type-backward (&optional short)
  5769. ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
  5770. ;; of types when parsing a declaration, which means that it
  5771. ;; sometimes consumes the identifier in the declaration as a type.
  5772. ;; This is used to "backtrack" and make the last type be treated as
  5773. ;; an identifier instead.
  5774. `(progn
  5775. ,(unless short
  5776. ;; These identifiers are bound only in the inner let.
  5777. '(setq identifier-type at-type
  5778. identifier-start type-start
  5779. got-parens nil
  5780. got-identifier t
  5781. got-suffix t
  5782. got-suffix-after-parens id-start
  5783. paren-depth 0))
  5784. (if (setq at-type (if (eq backup-at-type 'prefix)
  5785. t
  5786. backup-at-type))
  5787. (setq type-start backup-type-start
  5788. id-start backup-id-start)
  5789. (setq type-start start-pos
  5790. id-start start-pos))
  5791. ;; When these flags already are set we've found specifiers that
  5792. ;; unconditionally signal these attributes - backtracking doesn't
  5793. ;; change that. So keep them set in that case.
  5794. (or at-type-decl
  5795. (setq at-type-decl backup-at-type-decl))
  5796. (or maybe-typeless
  5797. (setq maybe-typeless backup-maybe-typeless))
  5798. ,(unless short
  5799. ;; This identifier is bound only in the inner let.
  5800. '(setq start id-start))))
  5801. (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
  5802. ;; Move forward over a declaration or a cast if at the start of one.
  5803. ;; The point is assumed to be at the start of some token. Nil is
  5804. ;; returned if no declaration or cast is recognized, and the point
  5805. ;; is clobbered in that case.
  5806. ;;
  5807. ;; If a declaration is parsed:
  5808. ;;
  5809. ;; The point is left at the first token after the first complete
  5810. ;; declarator, if there is one. The return value is a cons where
  5811. ;; the car is the position of the first token in the declarator. (See
  5812. ;; below for the cdr.)
  5813. ;; Some examples:
  5814. ;;
  5815. ;; void foo (int a, char *b) stuff ...
  5816. ;; car ^ ^ point
  5817. ;; float (*a)[], b;
  5818. ;; car ^ ^ point
  5819. ;; unsigned int a = c_style_initializer, b;
  5820. ;; car ^ ^ point
  5821. ;; unsigned int a (cplusplus_style_initializer), b;
  5822. ;; car ^ ^ point (might change)
  5823. ;; class Foo : public Bar {}
  5824. ;; car ^ ^ point
  5825. ;; class PikeClass (int a, string b) stuff ...
  5826. ;; car ^ ^ point
  5827. ;; enum bool;
  5828. ;; car ^ ^ point
  5829. ;; enum bool flag;
  5830. ;; car ^ ^ point
  5831. ;; void cplusplus_function (int x) throw (Bad);
  5832. ;; car ^ ^ point
  5833. ;; Foo::Foo (int b) : Base (b) {}
  5834. ;; car ^ ^ point
  5835. ;;
  5836. ;; The cdr of the return value is non-nil when a
  5837. ;; `c-typedef-decl-kwds' specifier is found in the declaration.
  5838. ;; Specifically it is a dotted pair (A . B) where B is t when a
  5839. ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
  5840. ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
  5841. ;; specifier is present. I.e., (some of) the declared
  5842. ;; identifier(s) are types.
  5843. ;;
  5844. ;; If a cast is parsed:
  5845. ;;
  5846. ;; The point is left at the first token after the closing paren of
  5847. ;; the cast. The return value is `cast'. Note that the start
  5848. ;; position must be at the first token inside the cast parenthesis
  5849. ;; to recognize it.
  5850. ;;
  5851. ;; PRECEDING-TOKEN-END is the first position after the preceding
  5852. ;; token, i.e. on the other side of the syntactic ws from the point.
  5853. ;; Use a value less than or equal to (point-min) if the point is at
  5854. ;; the first token in (the visible part of) the buffer.
  5855. ;;
  5856. ;; CONTEXT is a symbol that describes the context at the point:
  5857. ;; 'decl In a comma-separated declaration context (typically
  5858. ;; inside a function declaration arglist).
  5859. ;; '<> In an angle bracket arglist.
  5860. ;; 'arglist Some other type of arglist.
  5861. ;; nil Some other context or unknown context. Includes
  5862. ;; within the parens of an if, for, ... construct.
  5863. ;;
  5864. ;; LAST-CAST-END is the first token after the closing paren of a
  5865. ;; preceding cast, or nil if none is known. If
  5866. ;; `c-forward-decl-or-cast-1' is used in succession, it should be
  5867. ;; the position after the closest preceding call where a cast was
  5868. ;; matched. In that case it's used to discover chains of casts like
  5869. ;; "(a) (b) c".
  5870. ;;
  5871. ;; This function records identifier ranges on
  5872. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5873. ;; `c-record-type-identifiers' is non-nil.
  5874. ;;
  5875. ;; This function might do hidden buffer changes.
  5876. (let (;; `start-pos' is used below to point to the start of the
  5877. ;; first type, i.e. after any leading specifiers. It might
  5878. ;; also point at the beginning of the preceding syntactic
  5879. ;; whitespace.
  5880. (start-pos (point))
  5881. ;; Set to the result of `c-forward-type'.
  5882. at-type
  5883. ;; The position of the first token in what we currently
  5884. ;; believe is the type in the declaration or cast, after any
  5885. ;; specifiers and their associated clauses.
  5886. type-start
  5887. ;; The position of the first token in what we currently
  5888. ;; believe is the declarator for the first identifier. Set
  5889. ;; when the type is found, and moved forward over any
  5890. ;; `c-decl-hangon-kwds' and their associated clauses that
  5891. ;; occurs after the type.
  5892. id-start
  5893. ;; These store `at-type', `type-start' and `id-start' of the
  5894. ;; identifier before the one in those variables. The previous
  5895. ;; identifier might turn out to be the real type in a
  5896. ;; declaration if the last one has to be the declarator in it.
  5897. ;; If `backup-at-type' is nil then the other variables have
  5898. ;; undefined values.
  5899. backup-at-type backup-type-start backup-id-start
  5900. ;; Set if we've found a specifier (apart from "typedef") that makes
  5901. ;; the defined identifier(s) types.
  5902. at-type-decl
  5903. ;; Set if we've a "typedef" keyword.
  5904. at-typedef
  5905. ;; Set if we've found a specifier that can start a declaration
  5906. ;; where there's no type.
  5907. maybe-typeless
  5908. ;; If a specifier is found that also can be a type prefix,
  5909. ;; these flags are set instead of those above. If we need to
  5910. ;; back up an identifier, they are copied to the real flag
  5911. ;; variables. Thus they only take effect if we fail to
  5912. ;; interpret it as a type.
  5913. backup-at-type-decl backup-maybe-typeless
  5914. ;; Whether we've found a declaration or a cast. We might know
  5915. ;; this before we've found the type in it. It's 'ids if we've
  5916. ;; found two consecutive identifiers (usually a sure sign, but
  5917. ;; we should allow that in labels too), and t if we've found a
  5918. ;; specifier keyword (a 100% sure sign).
  5919. at-decl-or-cast
  5920. ;; Set when we need to back up to parse this as a declaration
  5921. ;; but not as a cast.
  5922. backup-if-not-cast
  5923. ;; For casts, the return position.
  5924. cast-end
  5925. ;; Save `c-record-type-identifiers' and
  5926. ;; `c-record-ref-identifiers' since ranges are recorded
  5927. ;; speculatively and should be thrown away if it turns out
  5928. ;; that it isn't a declaration or cast.
  5929. (save-rec-type-ids c-record-type-identifiers)
  5930. (save-rec-ref-ids c-record-ref-identifiers))
  5931. (while (c-forward-annotation)
  5932. (c-forward-syntactic-ws))
  5933. ;; Check for a type. Unknown symbols are treated as possible
  5934. ;; types, but they could also be specifiers disguised through
  5935. ;; macros like __INLINE__, so we recognize both types and known
  5936. ;; specifiers after them too.
  5937. (while
  5938. (let* ((start (point)) kwd-sym kwd-clause-end found-type)
  5939. ;; Look for a specifier keyword clause.
  5940. (when (or (looking-at c-prefix-spec-kwds-re)
  5941. (and (c-major-mode-is 'java-mode)
  5942. (looking-at "@[A-Za-z0-9]+")))
  5943. (if (looking-at c-typedef-key)
  5944. (setq at-typedef t))
  5945. (setq kwd-sym (c-keyword-sym (match-string 1)))
  5946. (save-excursion
  5947. (c-forward-keyword-clause 1)
  5948. (setq kwd-clause-end (point))))
  5949. (when (setq found-type (c-forward-type t)) ; brace-block-too
  5950. ;; Found a known or possible type or a prefix of a known type.
  5951. (when at-type
  5952. ;; Got two identifiers with nothing but whitespace
  5953. ;; between them. That can only happen in declarations.
  5954. (setq at-decl-or-cast 'ids)
  5955. (when (eq at-type 'found)
  5956. ;; If the previous identifier is a found type we
  5957. ;; record it as a real one; it might be some sort of
  5958. ;; alias for a prefix like "unsigned".
  5959. (save-excursion
  5960. (goto-char type-start)
  5961. (let ((c-promote-possible-types t))
  5962. (c-forward-type)))))
  5963. (setq backup-at-type at-type
  5964. backup-type-start type-start
  5965. backup-id-start id-start
  5966. at-type found-type
  5967. type-start start
  5968. id-start (point)
  5969. ;; The previous ambiguous specifier/type turned out
  5970. ;; to be a type since we've parsed another one after
  5971. ;; it, so clear these backup flags.
  5972. backup-at-type-decl nil
  5973. backup-maybe-typeless nil))
  5974. (if kwd-sym
  5975. (progn
  5976. ;; Handle known specifier keywords and
  5977. ;; `c-decl-hangon-kwds' which can occur after known
  5978. ;; types.
  5979. (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
  5980. ;; It's a hang-on keyword that can occur anywhere.
  5981. (progn
  5982. (setq at-decl-or-cast t)
  5983. (if at-type
  5984. ;; Move the identifier start position if
  5985. ;; we've passed a type.
  5986. (setq id-start kwd-clause-end)
  5987. ;; Otherwise treat this as a specifier and
  5988. ;; move the fallback position.
  5989. (setq start-pos kwd-clause-end))
  5990. (goto-char kwd-clause-end))
  5991. ;; It's an ordinary specifier so we know that
  5992. ;; anything before this can't be the type.
  5993. (setq backup-at-type nil
  5994. start-pos kwd-clause-end)
  5995. (if found-type
  5996. ;; It's ambiguous whether this keyword is a
  5997. ;; specifier or a type prefix, so set the backup
  5998. ;; flags. (It's assumed that `c-forward-type'
  5999. ;; moved further than `c-forward-keyword-clause'.)
  6000. (progn
  6001. (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
  6002. (setq backup-at-type-decl t))
  6003. (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
  6004. (setq backup-maybe-typeless t)))
  6005. (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
  6006. ;; This test only happens after we've scanned a type.
  6007. ;; So, with valid syntax, kwd-sym can't be 'typedef.
  6008. (setq at-type-decl t))
  6009. (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
  6010. (setq maybe-typeless t))
  6011. ;; Haven't matched a type so it's an unambiguous
  6012. ;; specifier keyword and we know we're in a
  6013. ;; declaration.
  6014. (setq at-decl-or-cast t)
  6015. (goto-char kwd-clause-end))))
  6016. ;; If the type isn't known we continue so that we'll jump
  6017. ;; over all specifiers and type identifiers. The reason
  6018. ;; to do this for a known type prefix is to make things
  6019. ;; like "unsigned INT16" work.
  6020. (and found-type (not (eq found-type t))))))
  6021. (cond
  6022. ((eq at-type t)
  6023. ;; If a known type was found, we still need to skip over any
  6024. ;; hangon keyword clauses after it. Otherwise it has already
  6025. ;; been done in the loop above.
  6026. (while (looking-at c-decl-hangon-key)
  6027. (c-forward-keyword-clause 1))
  6028. (setq id-start (point)))
  6029. ((eq at-type 'prefix)
  6030. ;; A prefix type is itself a primitive type when it's not
  6031. ;; followed by another type.
  6032. (setq at-type t))
  6033. ((not at-type)
  6034. ;; Got no type but set things up to continue anyway to handle
  6035. ;; the various cases when a declaration doesn't start with a
  6036. ;; type.
  6037. (setq id-start start-pos))
  6038. ((and (eq at-type 'maybe)
  6039. (c-major-mode-is 'c++-mode))
  6040. ;; If it's C++ then check if the last "type" ends on the form
  6041. ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
  6042. ;; (con|de)structor.
  6043. (save-excursion
  6044. (let (name end-2 end-1)
  6045. (goto-char id-start)
  6046. (c-backward-syntactic-ws)
  6047. (setq end-2 (point))
  6048. (when (and
  6049. (c-simple-skip-symbol-backward)
  6050. (progn
  6051. (setq name
  6052. (buffer-substring-no-properties (point) end-2))
  6053. ;; Cheating in the handling of syntactic ws below.
  6054. (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
  6055. (progn
  6056. (setq end-1 (point))
  6057. (c-simple-skip-symbol-backward))
  6058. (>= (point) type-start)
  6059. (equal (buffer-substring-no-properties (point) end-1)
  6060. name))
  6061. ;; It is a (con|de)structor name. In that case the
  6062. ;; declaration is typeless so zap out any preceding
  6063. ;; identifier(s) that we might have taken as types.
  6064. (goto-char type-start)
  6065. (setq at-type nil
  6066. backup-at-type nil
  6067. id-start type-start))))))
  6068. ;; Check for and step over a type decl expression after the thing
  6069. ;; that is or might be a type. This can't be skipped since we
  6070. ;; need the correct end position of the declarator for
  6071. ;; `max-type-decl-end-*'.
  6072. (let ((start (point)) (paren-depth 0) pos
  6073. ;; True if there's a non-open-paren match of
  6074. ;; `c-type-decl-prefix-key'.
  6075. got-prefix
  6076. ;; True if the declarator is surrounded by a parenthesis pair.
  6077. got-parens
  6078. ;; True if there is an identifier in the declarator.
  6079. got-identifier
  6080. ;; True if there's a non-close-paren match of
  6081. ;; `c-type-decl-suffix-key'.
  6082. got-suffix
  6083. ;; True if there's a prefix match outside the outermost
  6084. ;; paren pair that surrounds the declarator.
  6085. got-prefix-before-parens
  6086. ;; True if there's a suffix match outside the outermost
  6087. ;; paren pair that surrounds the declarator. The value is
  6088. ;; the position of the first suffix match.
  6089. got-suffix-after-parens
  6090. ;; True if we've parsed the type decl to a token that is
  6091. ;; known to end declarations in this context.
  6092. at-decl-end
  6093. ;; The earlier values of `at-type' and `type-start' if we've
  6094. ;; shifted the type backwards.
  6095. identifier-type identifier-start
  6096. ;; If `c-parse-and-markup-<>-arglists' is set we need to
  6097. ;; turn it off during the name skipping below to avoid
  6098. ;; getting `c-type' properties that might be bogus. That
  6099. ;; can happen since we don't know if
  6100. ;; `c-restricted-<>-arglists' will be correct inside the
  6101. ;; arglist paren that gets entered.
  6102. c-parse-and-markup-<>-arglists)
  6103. (goto-char id-start)
  6104. ;; Skip over type decl prefix operators. (Note similar code in
  6105. ;; `c-font-lock-declarators'.)
  6106. (while (and (looking-at c-type-decl-prefix-key)
  6107. (if (and (c-major-mode-is 'c++-mode)
  6108. (match-beginning 3))
  6109. ;; If the second submatch matches in C++ then
  6110. ;; we're looking at an identifier that's a
  6111. ;; prefix only if it specifies a member pointer.
  6112. (when (setq got-identifier (c-forward-name))
  6113. (if (looking-at "\\(::\\)")
  6114. ;; We only check for a trailing "::" and
  6115. ;; let the "*" that should follow be
  6116. ;; matched in the next round.
  6117. (progn (setq got-identifier nil) t)
  6118. ;; It turned out to be the real identifier,
  6119. ;; so stop.
  6120. nil))
  6121. t))
  6122. (if (eq (char-after) ?\()
  6123. (progn
  6124. (setq paren-depth (1+ paren-depth))
  6125. (forward-char))
  6126. (unless got-prefix-before-parens
  6127. (setq got-prefix-before-parens (= paren-depth 0)))
  6128. (setq got-prefix t)
  6129. (goto-char (match-end 1)))
  6130. (c-forward-syntactic-ws))
  6131. (setq got-parens (> paren-depth 0))
  6132. ;; Skip over an identifier.
  6133. (or got-identifier
  6134. (and (looking-at c-identifier-start)
  6135. (setq got-identifier (c-forward-name))))
  6136. ;; Skip over type decl suffix operators.
  6137. (while (if (looking-at c-type-decl-suffix-key)
  6138. (if (eq (char-after) ?\))
  6139. (when (> paren-depth 0)
  6140. (setq paren-depth (1- paren-depth))
  6141. (forward-char)
  6142. t)
  6143. (when (if (save-match-data (looking-at "\\s\("))
  6144. (c-safe (c-forward-sexp 1) t)
  6145. (goto-char (match-end 1))
  6146. t)
  6147. (when (and (not got-suffix-after-parens)
  6148. (= paren-depth 0))
  6149. (setq got-suffix-after-parens (match-beginning 0)))
  6150. (setq got-suffix t)))
  6151. ;; No suffix matched. We might have matched the
  6152. ;; identifier as a type and the open paren of a
  6153. ;; function arglist as a type decl prefix. In that
  6154. ;; case we should "backtrack": Reinterpret the last
  6155. ;; type as the identifier, move out of the arglist and
  6156. ;; continue searching for suffix operators.
  6157. ;;
  6158. ;; Do this even if there's no preceding type, to cope
  6159. ;; with old style function declarations in K&R C,
  6160. ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
  6161. ;; style declarations. That isn't applicable in an
  6162. ;; arglist context, though.
  6163. (when (and (= paren-depth 1)
  6164. (not got-prefix-before-parens)
  6165. (not (eq at-type t))
  6166. (or backup-at-type
  6167. maybe-typeless
  6168. backup-maybe-typeless
  6169. (when c-recognize-typeless-decls
  6170. (not context)))
  6171. (setq pos (c-up-list-forward (point)))
  6172. (eq (char-before pos) ?\)))
  6173. (c-fdoc-shift-type-backward)
  6174. (goto-char pos)
  6175. t))
  6176. (c-forward-syntactic-ws))
  6177. (when (and (or maybe-typeless backup-maybe-typeless)
  6178. (not got-identifier)
  6179. (not got-prefix)
  6180. at-type)
  6181. ;; Have found no identifier but `c-typeless-decl-kwds' has
  6182. ;; matched so we know we're inside a declaration. The
  6183. ;; preceding type must be the identifier instead.
  6184. (c-fdoc-shift-type-backward))
  6185. (setq
  6186. at-decl-or-cast
  6187. (catch 'at-decl-or-cast
  6188. ;; CASE 1
  6189. (when (> paren-depth 0)
  6190. ;; Encountered something inside parens that isn't matched by
  6191. ;; the `c-type-decl-*' regexps, so it's not a type decl
  6192. ;; expression. Try to skip out to the same paren depth to
  6193. ;; not confuse the cast check below.
  6194. (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
  6195. ;; If we've found a specifier keyword then it's a
  6196. ;; declaration regardless.
  6197. (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
  6198. (setq at-decl-end
  6199. (looking-at (cond ((eq context '<>) "[,>]")
  6200. (context "[,\)]")
  6201. (t "[,;]"))))
  6202. ;; Now we've collected info about various characteristics of
  6203. ;; the construct we're looking at. Below follows a decision
  6204. ;; tree based on that. It's ordered to check more certain
  6205. ;; signs before less certain ones.
  6206. (if got-identifier
  6207. (progn
  6208. ;; CASE 2
  6209. (when (and (or at-type maybe-typeless)
  6210. (not (or got-prefix got-parens)))
  6211. ;; Got another identifier directly after the type, so it's a
  6212. ;; declaration.
  6213. (throw 'at-decl-or-cast t))
  6214. (when (and got-parens
  6215. (not got-prefix)
  6216. (not got-suffix-after-parens)
  6217. (or backup-at-type
  6218. maybe-typeless
  6219. backup-maybe-typeless))
  6220. ;; Got a declaration of the form "foo bar (gnu);" where we've
  6221. ;; recognized "bar" as the type and "gnu" as the declarator.
  6222. ;; In this case it's however more likely that "bar" is the
  6223. ;; declarator and "gnu" a function argument or initializer (if
  6224. ;; `c-recognize-paren-inits' is set), since the parens around
  6225. ;; "gnu" would be superfluous if it's a declarator. Shift the
  6226. ;; type one step backward.
  6227. (c-fdoc-shift-type-backward)))
  6228. ;; Found no identifier.
  6229. (if backup-at-type
  6230. (progn
  6231. ;; CASE 3
  6232. (when (= (point) start)
  6233. ;; Got a plain list of identifiers. If a colon follows it's
  6234. ;; a valid label, or maybe a bitfield. Otherwise the last
  6235. ;; one probably is the declared identifier and we should
  6236. ;; back up to the previous type, providing it isn't a cast.
  6237. (if (and (eq (char-after) ?:)
  6238. (not (c-major-mode-is 'java-mode)))
  6239. (cond
  6240. ;; If we've found a specifier keyword then it's a
  6241. ;; declaration regardless.
  6242. ((eq at-decl-or-cast t)
  6243. (throw 'at-decl-or-cast t))
  6244. ((and c-has-bitfields
  6245. (eq at-decl-or-cast 'ids)) ; bitfield.
  6246. (setq backup-if-not-cast t)
  6247. (throw 'at-decl-or-cast t)))
  6248. (setq backup-if-not-cast t)
  6249. (throw 'at-decl-or-cast t)))
  6250. ;; CASE 4
  6251. (when (and got-suffix
  6252. (not got-prefix)
  6253. (not got-parens))
  6254. ;; Got a plain list of identifiers followed by some suffix.
  6255. ;; If this isn't a cast then the last identifier probably is
  6256. ;; the declared one and we should back up to the previous
  6257. ;; type.
  6258. (setq backup-if-not-cast t)
  6259. (throw 'at-decl-or-cast t)))
  6260. ;; CASE 5
  6261. (when (eq at-type t)
  6262. ;; If the type is known we know that there can't be any
  6263. ;; identifier somewhere else, and it's only in declarations in
  6264. ;; e.g. function prototypes and in casts that the identifier may
  6265. ;; be left out.
  6266. (throw 'at-decl-or-cast t))
  6267. (when (= (point) start)
  6268. ;; Only got a single identifier (parsed as a type so far).
  6269. ;; CASE 6
  6270. (if (and
  6271. ;; Check that the identifier isn't at the start of an
  6272. ;; expression.
  6273. at-decl-end
  6274. (cond
  6275. ((eq context 'decl)
  6276. ;; Inside an arglist that contains declarations. If K&R
  6277. ;; style declarations and parenthesis style initializers
  6278. ;; aren't allowed then the single identifier must be a
  6279. ;; type, else we require that it's known or found
  6280. ;; (primitive types are handled above).
  6281. (or (and (not c-recognize-knr-p)
  6282. (not c-recognize-paren-inits))
  6283. (memq at-type '(known found))))
  6284. ((eq context '<>)
  6285. ;; Inside a template arglist. Accept known and found
  6286. ;; types; other identifiers could just as well be
  6287. ;; constants in C++.
  6288. (memq at-type '(known found)))))
  6289. (throw 'at-decl-or-cast t)
  6290. ;; CASE 7
  6291. ;; Can't be a valid declaration or cast, but if we've found a
  6292. ;; specifier it can't be anything else either, so treat it as
  6293. ;; an invalid/unfinished declaration or cast.
  6294. (throw 'at-decl-or-cast at-decl-or-cast))))
  6295. (if (and got-parens
  6296. (not got-prefix)
  6297. (not context)
  6298. (not (eq at-type t))
  6299. (or backup-at-type
  6300. maybe-typeless
  6301. backup-maybe-typeless
  6302. (when c-recognize-typeless-decls
  6303. (or (not got-suffix)
  6304. (not (looking-at
  6305. c-after-suffixed-type-maybe-decl-key))))))
  6306. ;; Got an empty paren pair and a preceding type that probably
  6307. ;; really is the identifier. Shift the type backwards to make
  6308. ;; the last one the identifier. This is analogous to the
  6309. ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
  6310. ;; above.
  6311. ;;
  6312. ;; Exception: In addition to the conditions in that
  6313. ;; "backtracking" code, do not shift backward if we're not
  6314. ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
  6315. ;; Since there's no preceding type, the shift would mean that
  6316. ;; the declaration is typeless. But if the regexp doesn't match
  6317. ;; then we will simply fall through in the tests below and not
  6318. ;; recognize it at all, so it's better to try it as an abstract
  6319. ;; declarator instead.
  6320. (c-fdoc-shift-type-backward)
  6321. ;; Still no identifier.
  6322. ;; CASE 8
  6323. (when (and got-prefix (or got-parens got-suffix))
  6324. ;; Require `got-prefix' together with either `got-parens' or
  6325. ;; `got-suffix' to recognize it as an abstract declarator:
  6326. ;; `got-parens' only is probably an empty function call.
  6327. ;; `got-suffix' only can build an ordinary expression together
  6328. ;; with the preceding identifier which we've taken as a type.
  6329. ;; We could actually accept on `got-prefix' only, but that can
  6330. ;; easily occur temporarily while writing an expression so we
  6331. ;; avoid that case anyway. We could do a better job if we knew
  6332. ;; the point when the fontification was invoked.
  6333. (throw 'at-decl-or-cast t))
  6334. ;; CASE 9
  6335. (when (and at-type
  6336. (not got-prefix)
  6337. (not got-parens)
  6338. got-suffix-after-parens
  6339. (eq (char-after got-suffix-after-parens) ?\())
  6340. ;; Got a type, no declarator but a paren suffix. I.e. it's a
  6341. ;; normal function call after all (or perhaps a C++ style object
  6342. ;; instantiation expression).
  6343. (throw 'at-decl-or-cast nil))))
  6344. ;; CASE 10
  6345. (when at-decl-or-cast
  6346. ;; By now we've located the type in the declaration that we know
  6347. ;; we're in.
  6348. (throw 'at-decl-or-cast t))
  6349. ;; CASE 11
  6350. (when (and got-identifier
  6351. (not context)
  6352. (looking-at c-after-suffixed-type-decl-key)
  6353. (if (and got-parens
  6354. (not got-prefix)
  6355. (not got-suffix)
  6356. (not (eq at-type t)))
  6357. ;; Shift the type backward in the case that there's a
  6358. ;; single identifier inside parens. That can only
  6359. ;; occur in K&R style function declarations so it's
  6360. ;; more likely that it really is a function call.
  6361. ;; Therefore we only do this after
  6362. ;; `c-after-suffixed-type-decl-key' has matched.
  6363. (progn (c-fdoc-shift-type-backward) t)
  6364. got-suffix-after-parens))
  6365. ;; A declaration according to `c-after-suffixed-type-decl-key'.
  6366. (throw 'at-decl-or-cast t))
  6367. ;; CASE 12
  6368. (when (and (or got-prefix (not got-parens))
  6369. (memq at-type '(t known)))
  6370. ;; It's a declaration if a known type precedes it and it can't be a
  6371. ;; function call.
  6372. (throw 'at-decl-or-cast t))
  6373. ;; If we get here we can't tell if this is a type decl or a normal
  6374. ;; expression by looking at it alone. (That's under the assumption
  6375. ;; that normal expressions always can look like type decl expressions,
  6376. ;; which isn't really true but the cases where it doesn't hold are so
  6377. ;; uncommon (e.g. some placements of "const" in C++) it's not worth
  6378. ;; the effort to look for them.)
  6379. (unless (or at-decl-end (looking-at "=[^=]"))
  6380. ;; If this is a declaration it should end here or its initializer(*)
  6381. ;; should start here, so check for allowed separation tokens. Note
  6382. ;; that this rule doesn't work e.g. with a K&R arglist after a
  6383. ;; function header.
  6384. ;;
  6385. ;; *) Don't check for C++ style initializers using parens
  6386. ;; since those already have been matched as suffixes.
  6387. ;;
  6388. ;; If `at-decl-or-cast' is then we've found some other sign that
  6389. ;; it's a declaration or cast, so then it's probably an
  6390. ;; invalid/unfinished one.
  6391. (throw 'at-decl-or-cast at-decl-or-cast))
  6392. ;; Below are tests that only should be applied when we're certain to
  6393. ;; not have parsed halfway through an expression.
  6394. ;; CASE 14
  6395. (when (memq at-type '(t known))
  6396. ;; The expression starts with a known type so treat it as a
  6397. ;; declaration.
  6398. (throw 'at-decl-or-cast t))
  6399. ;; CASE 15
  6400. (when (and (c-major-mode-is 'c++-mode)
  6401. ;; In C++ we check if the identifier is a known type, since
  6402. ;; (con|de)structors use the class name as identifier.
  6403. ;; We've always shifted over the identifier as a type and
  6404. ;; then backed up again in this case.
  6405. identifier-type
  6406. (or (memq identifier-type '(found known))
  6407. (and (eq (char-after identifier-start) ?~)
  6408. ;; `at-type' probably won't be 'found for
  6409. ;; destructors since the "~" is then part of the
  6410. ;; type name being checked against the list of
  6411. ;; known types, so do a check without that
  6412. ;; operator.
  6413. (or (save-excursion
  6414. (goto-char (1+ identifier-start))
  6415. (c-forward-syntactic-ws)
  6416. (c-with-syntax-table
  6417. c-identifier-syntax-table
  6418. (looking-at c-known-type-key)))
  6419. (save-excursion
  6420. (goto-char (1+ identifier-start))
  6421. ;; We have already parsed the type earlier,
  6422. ;; so it'd be possible to cache the end
  6423. ;; position instead of redoing it here, but
  6424. ;; then we'd need to keep track of another
  6425. ;; position everywhere.
  6426. (c-check-type (point)
  6427. (progn (c-forward-type)
  6428. (point))))))))
  6429. (throw 'at-decl-or-cast t))
  6430. (if got-identifier
  6431. (progn
  6432. ;; CASE 16
  6433. (when (and got-prefix-before-parens
  6434. at-type
  6435. (or at-decl-end (looking-at "=[^=]"))
  6436. (not context)
  6437. (not got-suffix))
  6438. ;; Got something like "foo * bar;". Since we're not inside an
  6439. ;; arglist it would be a meaningless expression because the
  6440. ;; result isn't used. We therefore choose to recognize it as
  6441. ;; a declaration. Do not allow a suffix since it could then
  6442. ;; be a function call.
  6443. (throw 'at-decl-or-cast t))
  6444. ;; CASE 17
  6445. (when (and (or got-suffix-after-parens
  6446. (looking-at "=[^=]"))
  6447. (eq at-type 'found)
  6448. (not (eq context 'arglist)))
  6449. ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
  6450. ;; be an odd expression or it could be a declaration. Treat
  6451. ;; it as a declaration if "a" has been used as a type
  6452. ;; somewhere else (if it's a known type we won't get here).
  6453. (throw 'at-decl-or-cast t)))
  6454. ;; CASE 18
  6455. (when (and context
  6456. (or got-prefix
  6457. (and (eq context 'decl)
  6458. (not c-recognize-paren-inits)
  6459. (or got-parens got-suffix))))
  6460. ;; Got a type followed by an abstract declarator. If `got-prefix'
  6461. ;; is set it's something like "a *" without anything after it. If
  6462. ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
  6463. ;; or similar, which we accept only if the context rules out
  6464. ;; expressions.
  6465. (throw 'at-decl-or-cast t)))
  6466. ;; If we had a complete symbol table here (which rules out
  6467. ;; `c-found-types') we should return t due to the disambiguation rule
  6468. ;; (in at least C++) that anything that can be parsed as a declaration
  6469. ;; is a declaration. Now we're being more defensive and prefer to
  6470. ;; highlight things like "foo (bar);" as a declaration only if we're
  6471. ;; inside an arglist that contains declarations.
  6472. (eq context 'decl))))
  6473. ;; The point is now after the type decl expression.
  6474. (cond
  6475. ;; Check for a cast.
  6476. ((save-excursion
  6477. (and
  6478. c-cast-parens
  6479. ;; Should be the first type/identifier in a cast paren.
  6480. (> preceding-token-end (point-min))
  6481. (memq (char-before preceding-token-end) c-cast-parens)
  6482. ;; The closing paren should follow.
  6483. (progn
  6484. (c-forward-syntactic-ws)
  6485. (looking-at "\\s\)"))
  6486. ;; There should be a primary expression after it.
  6487. (let (pos)
  6488. (forward-char)
  6489. (c-forward-syntactic-ws)
  6490. (setq cast-end (point))
  6491. (and (looking-at c-primary-expr-regexp)
  6492. (progn
  6493. (setq pos (match-end 0))
  6494. (or
  6495. ;; Check if the expression begins with a prefix keyword.
  6496. (match-beginning 2)
  6497. (if (match-beginning 1)
  6498. ;; Expression begins with an ambiguous operator. Treat
  6499. ;; it as a cast if it's a type decl or if we've
  6500. ;; recognized the type somewhere else.
  6501. (or at-decl-or-cast
  6502. (memq at-type '(t known found)))
  6503. ;; Unless it's a keyword, it's the beginning of a primary
  6504. ;; expression.
  6505. (not (looking-at c-keywords-regexp)))))
  6506. ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
  6507. ;; that it matched a whole one so that we don't e.g. confuse
  6508. ;; the operator '-' with '->'. It's ok if it matches further,
  6509. ;; though, since it e.g. can match the float '.5' while the
  6510. ;; operator regexp only matches '.'.
  6511. (or (not (looking-at c-nonsymbol-token-regexp))
  6512. (<= (match-end 0) pos))))
  6513. ;; There should either be a cast before it or something that isn't an
  6514. ;; identifier or close paren.
  6515. (> preceding-token-end (point-min))
  6516. (progn
  6517. (goto-char (1- preceding-token-end))
  6518. (or (eq (point) last-cast-end)
  6519. (progn
  6520. (c-backward-syntactic-ws)
  6521. (if (< (skip-syntax-backward "w_") 0)
  6522. ;; It's a symbol. Accept it only if it's one of the
  6523. ;; keywords that can precede an expression (without
  6524. ;; surrounding parens).
  6525. (looking-at c-simple-stmt-key)
  6526. (and
  6527. ;; Check that it isn't a close paren (block close is ok,
  6528. ;; though).
  6529. (not (memq (char-before) '(?\) ?\])))
  6530. ;; Check that it isn't a nonsymbol identifier.
  6531. (not (c-on-identifier)))))))))
  6532. ;; Handle the cast.
  6533. (when (and c-record-type-identifiers at-type (not (eq at-type t)))
  6534. (let ((c-promote-possible-types t))
  6535. (goto-char type-start)
  6536. (c-forward-type)))
  6537. (goto-char cast-end)
  6538. 'cast)
  6539. (at-decl-or-cast
  6540. ;; We're at a declaration. Highlight the type and the following
  6541. ;; declarators.
  6542. (when backup-if-not-cast
  6543. (c-fdoc-shift-type-backward t))
  6544. (when (and (eq context 'decl) (looking-at ","))
  6545. ;; Make sure to propagate the `c-decl-arg-start' property to
  6546. ;; the next argument if it's set in this one, to cope with
  6547. ;; interactive refontification.
  6548. (c-put-c-type-property (point) 'c-decl-arg-start))
  6549. (when (and c-record-type-identifiers at-type (not (eq at-type t)))
  6550. (let ((c-promote-possible-types t))
  6551. (save-excursion
  6552. (goto-char type-start)
  6553. (c-forward-type))))
  6554. (cons id-start
  6555. (and (or at-type-decl at-typedef)
  6556. (cons at-type-decl at-typedef))))
  6557. (t
  6558. ;; False alarm. Restore the recorded ranges.
  6559. (setq c-record-type-identifiers save-rec-type-ids
  6560. c-record-ref-identifiers save-rec-ref-ids)
  6561. nil))))
  6562. (defun c-forward-label (&optional assume-markup preceding-token-end limit)
  6563. ;; Assuming that point is at the beginning of a token, check if it starts a
  6564. ;; label and if so move over it and return non-nil (t in default situations,
  6565. ;; specific symbols (see below) for interesting situations), otherwise don't
  6566. ;; move and return nil. "Label" here means "most things with a colon".
  6567. ;;
  6568. ;; More precisely, a "label" is regarded as one of:
  6569. ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
  6570. ;; (ii) A case label - either the entire construct "case FOO:", or just the
  6571. ;; bare "case", should the colon be missing. We return t;
  6572. ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
  6573. ;; return t;
  6574. ;; (iv) One of QT's "extended" C++ variants of
  6575. ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
  6576. ;; Returns the symbol `qt-2kwds-colon'.
  6577. ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
  6578. ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
  6579. ;; colon). Currently (2006-03), this applies only to Objective C's
  6580. ;; keywords "@private", "@protected", and "@public". Returns t.
  6581. ;;
  6582. ;; One of the things which will NOT be recognized as a label is a bit-field
  6583. ;; element of a struct, something like "int foo:5".
  6584. ;;
  6585. ;; The end of the label is taken to be just after the colon, or the end of
  6586. ;; the first submatch in `c-opt-extra-label-key'. The point is directly
  6587. ;; after the end on return. The terminating char gets marked with
  6588. ;; `c-decl-end' to improve recognition of the following declaration or
  6589. ;; statement.
  6590. ;;
  6591. ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
  6592. ;; label, if any, has already been marked up like that.
  6593. ;;
  6594. ;; If PRECEDING-TOKEN-END is given, it should be the first position
  6595. ;; after the preceding token, i.e. on the other side of the
  6596. ;; syntactic ws from the point. Use a value less than or equal to
  6597. ;; (point-min) if the point is at the first token in (the visible
  6598. ;; part of) the buffer.
  6599. ;;
  6600. ;; The optional LIMIT limits the forward scan for the colon.
  6601. ;;
  6602. ;; This function records the ranges of the label symbols on
  6603. ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
  6604. ;; non-nil.
  6605. ;;
  6606. ;; This function might do hidden buffer changes.
  6607. (let ((start (point))
  6608. label-end
  6609. qt-symbol-idx
  6610. macro-start ; if we're in one.
  6611. label-type
  6612. kwd)
  6613. (cond
  6614. ;; "case" or "default" (Doesn't apply to AWK).
  6615. ((looking-at c-label-kwds-regexp)
  6616. (let ((kwd-end (match-end 1)))
  6617. ;; Record only the keyword itself for fontification, since in
  6618. ;; case labels the following is a constant expression and not
  6619. ;; a label.
  6620. (when c-record-type-identifiers
  6621. (c-record-ref-id (cons (match-beginning 1) kwd-end)))
  6622. ;; Find the label end.
  6623. (goto-char kwd-end)
  6624. (setq label-type
  6625. (if (and (c-syntactic-re-search-forward
  6626. ;; Stop on chars that aren't allowed in expressions,
  6627. ;; and on operator chars that would be meaningless
  6628. ;; there. FIXME: This doesn't cope with ?: operators.
  6629. "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
  6630. limit t t nil 1)
  6631. (match-beginning 2))
  6632. (progn ; there's a proper :
  6633. (goto-char (match-beginning 2)) ; just after the :
  6634. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6635. t)
  6636. ;; It's an unfinished label. We consider the keyword enough
  6637. ;; to recognize it as a label, so that it gets fontified.
  6638. ;; Leave the point at the end of it, but don't put any
  6639. ;; `c-decl-end' marker.
  6640. (goto-char kwd-end)
  6641. t))))
  6642. ;; @private, @protected, @public, in Objective C, or similar.
  6643. ((and c-opt-extra-label-key
  6644. (looking-at c-opt-extra-label-key))
  6645. ;; For a `c-opt-extra-label-key' match, we record the whole
  6646. ;; thing for fontification. That's to get the leading '@' in
  6647. ;; Objective-C protection labels fontified.
  6648. (goto-char (match-end 1))
  6649. (when c-record-type-identifiers
  6650. (c-record-ref-id (cons (match-beginning 1) (point))))
  6651. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6652. (setq label-type t))
  6653. ;; All other cases of labels.
  6654. ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
  6655. ;; A colon label must have something before the colon.
  6656. (not (eq (char-after) ?:))
  6657. ;; Check that we're not after a token that can't precede a label.
  6658. (or
  6659. ;; Trivially succeeds when there's no preceding token.
  6660. ;; Succeeds when we're at a virtual semicolon.
  6661. (if preceding-token-end
  6662. (<= preceding-token-end (point-min))
  6663. (save-excursion
  6664. (c-backward-syntactic-ws)
  6665. (setq preceding-token-end (point))
  6666. (or (bobp)
  6667. (c-at-vsemi-p))))
  6668. ;; Check if we're after a label, if we're after a closing
  6669. ;; paren that belong to statement, and with
  6670. ;; `c-label-prefix-re'. It's done in different order
  6671. ;; depending on `assume-markup' since the checks have
  6672. ;; different expensiveness.
  6673. (if assume-markup
  6674. (or
  6675. (eq (c-get-char-property (1- preceding-token-end) 'c-type)
  6676. 'c-decl-end)
  6677. (save-excursion
  6678. (goto-char (1- preceding-token-end))
  6679. (c-beginning-of-current-token)
  6680. (or (looking-at c-label-prefix-re)
  6681. (looking-at c-block-stmt-1-key)))
  6682. (and (eq (char-before preceding-token-end) ?\))
  6683. (c-after-conditional)))
  6684. (or
  6685. (save-excursion
  6686. (goto-char (1- preceding-token-end))
  6687. (c-beginning-of-current-token)
  6688. (or (looking-at c-label-prefix-re)
  6689. (looking-at c-block-stmt-1-key)))
  6690. (cond
  6691. ((eq (char-before preceding-token-end) ?\))
  6692. (c-after-conditional))
  6693. ((eq (char-before preceding-token-end) ?:)
  6694. ;; Might be after another label, so check it recursively.
  6695. (save-restriction
  6696. (save-excursion
  6697. (goto-char (1- preceding-token-end))
  6698. ;; Essentially the same as the
  6699. ;; `c-syntactic-re-search-forward' regexp below.
  6700. (setq macro-start
  6701. (save-excursion (and (c-beginning-of-macro)
  6702. (point))))
  6703. (if macro-start (narrow-to-region macro-start (point-max)))
  6704. (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
  6705. ;; Note: the following should work instead of the
  6706. ;; narrow-to-region above. Investigate why not,
  6707. ;; sometime. ACM, 2006-03-31.
  6708. ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
  6709. ;; macro-start t)
  6710. (let ((pte (point))
  6711. ;; If the caller turned on recording for us,
  6712. ;; it shouldn't apply when we check the
  6713. ;; preceding label.
  6714. c-record-type-identifiers)
  6715. ;; A label can't start at a cpp directive. Check for
  6716. ;; this, since c-forward-syntactic-ws would foul up on it.
  6717. (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
  6718. (c-forward-syntactic-ws)
  6719. (c-forward-label nil pte start))))))))))
  6720. ;; Point is still at the beginning of the possible label construct.
  6721. ;;
  6722. ;; Check that the next nonsymbol token is ":", or that we're in one
  6723. ;; of QT's "slots" declarations. Allow '(' for the sake of macro
  6724. ;; arguments. FIXME: Should build this regexp from the language
  6725. ;; constants.
  6726. (cond
  6727. ;; public: protected: private:
  6728. ((and
  6729. (c-major-mode-is 'c++-mode)
  6730. (search-forward-regexp
  6731. "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
  6732. (progn (backward-char)
  6733. (c-forward-syntactic-ws limit)
  6734. (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
  6735. (forward-char)
  6736. (setq label-type t))
  6737. ;; QT double keyword like "protected slots:" or goto target.
  6738. ((progn (goto-char start) nil))
  6739. ((when (c-syntactic-re-search-forward
  6740. "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
  6741. (backward-char)
  6742. (setq label-end (point))
  6743. (setq qt-symbol-idx
  6744. (and (c-major-mode-is 'c++-mode)
  6745. (string-match
  6746. "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
  6747. (buffer-substring start (point)))))
  6748. (c-forward-syntactic-ws limit)
  6749. (cond
  6750. ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
  6751. (forward-char)
  6752. (setq label-type
  6753. (if (or (string= "signals" ; Special QT macro
  6754. (setq kwd (buffer-substring-no-properties start label-end)))
  6755. (string= "Q_SIGNALS" kwd))
  6756. 'qt-1kwd-colon
  6757. 'goto-target)))
  6758. ((and qt-symbol-idx
  6759. (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
  6760. (progn (c-forward-syntactic-ws limit)
  6761. (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
  6762. (forward-char)
  6763. (setq label-type 'qt-2kwds-colon)))))))
  6764. (save-restriction
  6765. (narrow-to-region start (point))
  6766. ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
  6767. (catch 'check-label
  6768. (goto-char start)
  6769. (while (progn
  6770. (when (looking-at c-nonlabel-token-key)
  6771. (goto-char start)
  6772. (setq label-type nil)
  6773. (throw 'check-label nil))
  6774. (and (c-safe (c-forward-sexp)
  6775. (c-forward-syntactic-ws)
  6776. t)
  6777. (not (eobp)))))
  6778. ;; Record the identifiers in the label for fontification, unless
  6779. ;; it begins with `c-label-kwds' in which case the following
  6780. ;; identifiers are part of a (constant) expression that
  6781. ;; shouldn't be fontified.
  6782. (when (and c-record-type-identifiers
  6783. (progn (goto-char start)
  6784. (not (looking-at c-label-kwds-regexp))))
  6785. (while (c-syntactic-re-search-forward c-symbol-key nil t)
  6786. (c-record-ref-id (cons (match-beginning 0)
  6787. (match-end 0)))))
  6788. (c-put-c-type-property (1- (point-max)) 'c-decl-end)
  6789. (goto-char (point-max)))))
  6790. (t
  6791. ;; Not a label.
  6792. (goto-char start)))
  6793. label-type))
  6794. (defun c-forward-objc-directive ()
  6795. ;; Assuming the point is at the beginning of a token, try to move
  6796. ;; forward to the end of the Objective-C directive that starts
  6797. ;; there. Return t if a directive was fully recognized, otherwise
  6798. ;; the point is moved as far as one could be successfully parsed and
  6799. ;; nil is returned.
  6800. ;;
  6801. ;; This function records identifier ranges on
  6802. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  6803. ;; `c-record-type-identifiers' is non-nil.
  6804. ;;
  6805. ;; This function might do hidden buffer changes.
  6806. (let ((start (point))
  6807. start-char
  6808. (c-promote-possible-types t)
  6809. lim
  6810. ;; Turn off recognition of angle bracket arglists while parsing
  6811. ;; types here since the protocol reference list might then be
  6812. ;; considered part of the preceding name or superclass-name.
  6813. c-recognize-<>-arglists)
  6814. (if (or
  6815. (when (looking-at
  6816. (eval-when-compile
  6817. (c-make-keywords-re t
  6818. (append (c-lang-const c-protection-kwds objc)
  6819. '("@end"))
  6820. 'objc-mode)))
  6821. (goto-char (match-end 1))
  6822. t)
  6823. (and
  6824. (looking-at
  6825. (eval-when-compile
  6826. (c-make-keywords-re t
  6827. '("@interface" "@implementation" "@protocol")
  6828. 'objc-mode)))
  6829. ;; Handle the name of the class itself.
  6830. (progn
  6831. ; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
  6832. ; at EOB.
  6833. (goto-char (match-end 0))
  6834. (setq lim (point))
  6835. (c-skip-ws-forward)
  6836. (c-forward-type))
  6837. (catch 'break
  6838. ;; Look for ": superclass-name" or "( category-name )".
  6839. (when (looking-at "[:\(]")
  6840. (setq start-char (char-after))
  6841. (forward-char)
  6842. (c-forward-syntactic-ws)
  6843. (unless (c-forward-type) (throw 'break nil))
  6844. (when (eq start-char ?\()
  6845. (unless (eq (char-after) ?\)) (throw 'break nil))
  6846. (forward-char)
  6847. (c-forward-syntactic-ws)))
  6848. ;; Look for a protocol reference list.
  6849. (if (eq (char-after) ?<)
  6850. (let ((c-recognize-<>-arglists t)
  6851. (c-parse-and-markup-<>-arglists t)
  6852. c-restricted-<>-arglists)
  6853. (c-forward-<>-arglist t))
  6854. t))))
  6855. (progn
  6856. (c-backward-syntactic-ws lim)
  6857. (c-clear-c-type-property start (1- (point)) 'c-decl-end)
  6858. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6859. t)
  6860. (c-clear-c-type-property start (point) 'c-decl-end)
  6861. nil)))
  6862. (defun c-beginning-of-inheritance-list (&optional lim)
  6863. ;; Go to the first non-whitespace after the colon that starts a
  6864. ;; multiple inheritance introduction. Optional LIM is the farthest
  6865. ;; back we should search.
  6866. ;;
  6867. ;; This function might do hidden buffer changes.
  6868. (c-with-syntax-table c++-template-syntax-table
  6869. (c-backward-token-2 0 t lim)
  6870. (while (and (or (looking-at c-symbol-start)
  6871. (looking-at "[<,]\\|::"))
  6872. (zerop (c-backward-token-2 1 t lim))))))
  6873. (defun c-in-method-def-p ()
  6874. ;; Return nil if we aren't in a method definition, otherwise the
  6875. ;; position of the initial [+-].
  6876. ;;
  6877. ;; This function might do hidden buffer changes.
  6878. (save-excursion
  6879. (beginning-of-line)
  6880. (and c-opt-method-key
  6881. (looking-at c-opt-method-key)
  6882. (point))
  6883. ))
  6884. ;; Contributed by Kevin Ryde <user42@zip.com.au>.
  6885. (defun c-in-gcc-asm-p ()
  6886. ;; Return non-nil if point is within a gcc \"asm\" block.
  6887. ;;
  6888. ;; This should be called with point inside an argument list.
  6889. ;;
  6890. ;; Only one level of enclosing parentheses is considered, so for
  6891. ;; instance `nil' is returned when in a function call within an asm
  6892. ;; operand.
  6893. ;;
  6894. ;; This function might do hidden buffer changes.
  6895. (and c-opt-asm-stmt-key
  6896. (save-excursion
  6897. (beginning-of-line)
  6898. (backward-up-list 1)
  6899. (c-beginning-of-statement-1 (point-min) nil t)
  6900. (looking-at c-opt-asm-stmt-key))))
  6901. (defun c-at-toplevel-p ()
  6902. "Return a determination as to whether point is \"at the top level\".
  6903. Informally, \"at the top level\" is anywhere where you can write
  6904. a function.
  6905. More precisely, being at the top-level means that point is either
  6906. outside any enclosing block (such as a function definition), or
  6907. directly inside a class, namespace or other block that contains
  6908. another declaration level.
  6909. If point is not at the top-level (e.g. it is inside a method
  6910. definition), then nil is returned. Otherwise, if point is at a
  6911. top-level not enclosed within a class definition, t is returned.
  6912. Otherwise, a 2-vector is returned where the zeroth element is the
  6913. buffer position of the start of the class declaration, and the first
  6914. element is the buffer position of the enclosing class's opening
  6915. brace.
  6916. Note that this function might do hidden buffer changes. See the
  6917. comment at the start of cc-engine.el for more info."
  6918. (let ((paren-state (c-parse-state)))
  6919. (or (not (c-most-enclosing-brace paren-state))
  6920. (c-search-uplist-for-classkey paren-state))))
  6921. (defun c-just-after-func-arglist-p (&optional lim)
  6922. ;; Return non-nil if the point is in the region after the argument
  6923. ;; list of a function and its opening brace (or semicolon in case it
  6924. ;; got no body). If there are K&R style argument declarations in
  6925. ;; that region, the point has to be inside the first one for this
  6926. ;; function to recognize it.
  6927. ;;
  6928. ;; If successful, the point is moved to the first token after the
  6929. ;; function header (see `c-forward-decl-or-cast-1' for details) and
  6930. ;; the position of the opening paren of the function arglist is
  6931. ;; returned.
  6932. ;;
  6933. ;; The point is clobbered if not successful.
  6934. ;;
  6935. ;; LIM is used as bound for backward buffer searches.
  6936. ;;
  6937. ;; This function might do hidden buffer changes.
  6938. (let ((beg (point)) end id-start)
  6939. (and
  6940. (eq (c-beginning-of-statement-1 lim) 'same)
  6941. (not (and (c-major-mode-is 'objc-mode)
  6942. (c-forward-objc-directive)))
  6943. (setq id-start
  6944. (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
  6945. (< id-start beg)
  6946. ;; There should not be a '=' or ',' between beg and the
  6947. ;; start of the declaration since that means we were in the
  6948. ;; "expression part" of the declaration.
  6949. (or (> (point) beg)
  6950. (not (looking-at "[=,]")))
  6951. (save-excursion
  6952. ;; Check that there's an arglist paren in the
  6953. ;; declaration.
  6954. (goto-char id-start)
  6955. (cond ((eq (char-after) ?\()
  6956. ;; The declarator is a paren expression, so skip past it
  6957. ;; so that we don't get stuck on that instead of the
  6958. ;; function arglist.
  6959. (c-forward-sexp))
  6960. ((and c-opt-op-identifier-prefix
  6961. (looking-at c-opt-op-identifier-prefix))
  6962. ;; Don't trip up on "operator ()".
  6963. (c-forward-token-2 2 t)))
  6964. (and (< (point) beg)
  6965. (c-syntactic-re-search-forward "(" beg t t)
  6966. (1- (point)))))))
  6967. (defun c-in-knr-argdecl (&optional lim)
  6968. ;; Return the position of the first argument declaration if point is
  6969. ;; inside a K&R style argument declaration list, nil otherwise.
  6970. ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
  6971. ;; position that bounds the backward search for the argument list.
  6972. ;;
  6973. ;; Point must be within a possible K&R region, e.g. just before a top-level
  6974. ;; "{". It must be outside of parens and brackets. The test can return
  6975. ;; false positives otherwise.
  6976. ;;
  6977. ;; This function might do hidden buffer changes.
  6978. (save-excursion
  6979. (save-restriction
  6980. ;; If we're in a macro, our search range is restricted to it. Narrow to
  6981. ;; the searchable range.
  6982. (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
  6983. (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
  6984. (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
  6985. before-lparen after-rparen
  6986. (pp-count-out 20)) ; Max number of paren/brace constructs before
  6987. ; we give up
  6988. (narrow-to-region low-lim (or macro-end (point-max)))
  6989. ;; Search backwards for the defun's argument list. We give up if we
  6990. ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
  6991. ;; a knr region) or BOB.
  6992. ;;
  6993. ;; The criterion for a paren structure being the arg list is:
  6994. ;; o - there is non-WS stuff after it but before any "{"; AND
  6995. ;; o - the token after it isn't a ";" AND
  6996. ;; o - it is preceded by either an identifier (the function name) or
  6997. ;; a macro expansion like "DEFUN (...)"; AND
  6998. ;; o - its content is a non-empty comma-separated list of identifiers
  6999. ;; (an empty arg list won't have a knr region).
  7000. ;;
  7001. ;; The following snippet illustrates these rules:
  7002. ;; int foo (bar, baz, yuk)
  7003. ;; int bar [] ;
  7004. ;; int (*baz) (my_type) ;
  7005. ;; int (*) (void) (*yuk) (void) ;
  7006. ;; {
  7007. (catch 'knr
  7008. (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
  7009. (setq pp-count-out (1- pp-count-out))
  7010. (c-syntactic-skip-backward "^)]}=")
  7011. (cond ((eq (char-before) ?\))
  7012. (setq after-rparen (point)))
  7013. ((eq (char-before) ?\])
  7014. (setq after-rparen nil))
  7015. (t ; either } (hit previous defun) or = or no more
  7016. ; parens/brackets.
  7017. (throw 'knr nil)))
  7018. (if after-rparen
  7019. ;; We're inside a paren. Could it be our argument list....?
  7020. (if
  7021. (and
  7022. (progn
  7023. (goto-char after-rparen)
  7024. (unless (c-go-list-backward) (throw 'knr nil)) ;
  7025. ;; FIXME!!! What about macros between the parens? 2007/01/20
  7026. (setq before-lparen (point)))
  7027. ;; It can't be the arg list if next token is ; or {
  7028. (progn (goto-char after-rparen)
  7029. (c-forward-syntactic-ws)
  7030. (not (memq (char-after) '(?\; ?\{ ?\=))))
  7031. ;; Is the thing preceding the list an identifier (the
  7032. ;; function name), or a macro expansion?
  7033. (progn
  7034. (goto-char before-lparen)
  7035. (eq (c-backward-token-2) 0)
  7036. (or (eq (c-on-identifier) (point))
  7037. (and (eq (char-after) ?\))
  7038. (c-go-up-list-backward)
  7039. (eq (c-backward-token-2) 0)
  7040. (eq (c-on-identifier) (point)))))
  7041. ;; Have we got a non-empty list of comma-separated
  7042. ;; identifiers?
  7043. (progn
  7044. (goto-char before-lparen)
  7045. (c-forward-token-2) ; to first token inside parens
  7046. (and
  7047. (c-on-identifier)
  7048. (c-forward-token-2)
  7049. (catch 'id-list
  7050. (while (eq (char-after) ?\,)
  7051. (c-forward-token-2)
  7052. (unless (c-on-identifier) (throw 'id-list nil))
  7053. (c-forward-token-2))
  7054. (eq (char-after) ?\))))))
  7055. ;; ...Yes. We've identified the function's argument list.
  7056. (throw 'knr
  7057. (progn (goto-char after-rparen)
  7058. (c-forward-syntactic-ws)
  7059. (point)))
  7060. ;; ...No. The current parens aren't the function's arg list.
  7061. (goto-char before-lparen))
  7062. (or (c-go-list-backward) ; backwards over [ .... ]
  7063. (throw 'knr nil)))))))))
  7064. (defun c-skip-conditional ()
  7065. ;; skip forward over conditional at point, including any predicate
  7066. ;; statements in parentheses. No error checking is performed.
  7067. ;;
  7068. ;; This function might do hidden buffer changes.
  7069. (c-forward-sexp (cond
  7070. ;; else if()
  7071. ((looking-at (concat "\\<else"
  7072. "\\([ \t\n]\\|\\\\\n\\)+"
  7073. "if\\>\\([^_]\\|$\\)"))
  7074. 3)
  7075. ;; do, else, try, finally
  7076. ((looking-at (concat "\\<\\("
  7077. "do\\|else\\|try\\|finally"
  7078. "\\)\\>\\([^_]\\|$\\)"))
  7079. 1)
  7080. ;; for, if, while, switch, catch, synchronized, foreach
  7081. (t 2))))
  7082. (defun c-after-conditional (&optional lim)
  7083. ;; If looking at the token after a conditional then return the
  7084. ;; position of its start, otherwise return nil.
  7085. ;;
  7086. ;; This function might do hidden buffer changes.
  7087. (save-excursion
  7088. (and (zerop (c-backward-token-2 1 t lim))
  7089. (or (looking-at c-block-stmt-1-key)
  7090. (and (eq (char-after) ?\()
  7091. (zerop (c-backward-token-2 1 t lim))
  7092. (looking-at c-block-stmt-2-key)))
  7093. (point))))
  7094. (defun c-after-special-operator-id (&optional lim)
  7095. ;; If the point is after an operator identifier that isn't handled
  7096. ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
  7097. ;; position of the start of that identifier is returned. nil is
  7098. ;; returned otherwise. The point may be anywhere in the syntactic
  7099. ;; whitespace after the last token of the operator identifier.
  7100. ;;
  7101. ;; This function might do hidden buffer changes.
  7102. (save-excursion
  7103. (and c-overloadable-operators-regexp
  7104. (zerop (c-backward-token-2 1 nil lim))
  7105. (looking-at c-overloadable-operators-regexp)
  7106. (or (not c-opt-op-identifier-prefix)
  7107. (and
  7108. (zerop (c-backward-token-2 1 nil lim))
  7109. (looking-at c-opt-op-identifier-prefix)))
  7110. (point))))
  7111. (defsubst c-backward-to-block-anchor (&optional lim)
  7112. ;; Assuming point is at a brace that opens a statement block of some
  7113. ;; kind, move to the proper anchor point for that block. It might
  7114. ;; need to be adjusted further by c-add-stmt-syntax, but the
  7115. ;; position at return is suitable as start position for that
  7116. ;; function.
  7117. ;;
  7118. ;; This function might do hidden buffer changes.
  7119. (unless (= (point) (c-point 'boi))
  7120. (let ((start (c-after-conditional lim)))
  7121. (if start
  7122. (goto-char start)))))
  7123. (defsubst c-backward-to-decl-anchor (&optional lim)
  7124. ;; Assuming point is at a brace that opens the block of a top level
  7125. ;; declaration of some kind, move to the proper anchor point for
  7126. ;; that block.
  7127. ;;
  7128. ;; This function might do hidden buffer changes.
  7129. (unless (= (point) (c-point 'boi))
  7130. (c-beginning-of-statement-1 lim)))
  7131. (defun c-search-decl-header-end ()
  7132. ;; Search forward for the end of the "header" of the current
  7133. ;; declaration. That's the position where the definition body
  7134. ;; starts, or the first variable initializer, or the ending
  7135. ;; semicolon. I.e. search forward for the closest following
  7136. ;; (syntactically relevant) '{', '=' or ';' token. Point is left
  7137. ;; _after_ the first found token, or at point-max if none is found.
  7138. ;;
  7139. ;; This function might do hidden buffer changes.
  7140. (let ((base (point)))
  7141. (if (c-major-mode-is 'c++-mode)
  7142. ;; In C++ we need to take special care to handle operator
  7143. ;; tokens and those pesky template brackets.
  7144. (while (and
  7145. (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
  7146. (or
  7147. (c-end-of-current-token base)
  7148. ;; Handle operator identifiers, i.e. ignore any
  7149. ;; operator token preceded by "operator".
  7150. (save-excursion
  7151. (and (c-safe (c-backward-sexp) t)
  7152. (looking-at c-opt-op-identifier-prefix)))
  7153. (and (eq (char-before) ?<)
  7154. (c-with-syntax-table c++-template-syntax-table
  7155. (if (c-safe (goto-char (c-up-list-forward (point))))
  7156. t
  7157. (goto-char (point-max))
  7158. nil)))))
  7159. (setq base (point)))
  7160. (while (and
  7161. (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
  7162. (c-end-of-current-token base))
  7163. (setq base (point))))))
  7164. (defun c-beginning-of-decl-1 (&optional lim)
  7165. ;; Go to the beginning of the current declaration, or the beginning
  7166. ;; of the previous one if already at the start of it. Point won't
  7167. ;; be moved out of any surrounding paren. Return a cons cell of the
  7168. ;; form (MOVE . KNR-POS). MOVE is like the return value from
  7169. ;; `c-beginning-of-statement-1'. If point skipped over some K&R
  7170. ;; style argument declarations (and they are to be recognized) then
  7171. ;; KNR-POS is set to the start of the first such argument
  7172. ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
  7173. ;; position that bounds the backward search.
  7174. ;;
  7175. ;; NB: Cases where the declaration continues after the block, as in
  7176. ;; "struct foo { ... } bar;", are currently recognized as two
  7177. ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
  7178. ;;
  7179. ;; This function might do hidden buffer changes.
  7180. (catch 'return
  7181. (let* ((start (point))
  7182. (last-stmt-start (point))
  7183. (move (c-beginning-of-statement-1 lim nil t)))
  7184. ;; `c-beginning-of-statement-1' stops at a block start, but we
  7185. ;; want to continue if the block doesn't begin a top level
  7186. ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
  7187. ;; or an open paren.
  7188. (let ((beg (point)) tentative-move)
  7189. ;; Go back one "statement" each time round the loop until we're just
  7190. ;; after a ;, }, or :, or at BOB or the start of a macro or start of
  7191. ;; an ObjC method. This will move over a multiple declaration whose
  7192. ;; components are comma separated.
  7193. (while (and
  7194. ;; Must check with c-opt-method-key in ObjC mode.
  7195. (not (and c-opt-method-key
  7196. (looking-at c-opt-method-key)))
  7197. (/= last-stmt-start (point))
  7198. (progn
  7199. (c-backward-syntactic-ws lim)
  7200. (not (memq (char-before) '(?\; ?} ?: nil))))
  7201. (save-excursion
  7202. (backward-char)
  7203. (not (looking-at "\\s(")))
  7204. ;; Check that we don't move from the first thing in a
  7205. ;; macro to its header.
  7206. (not (eq (setq tentative-move
  7207. (c-beginning-of-statement-1 lim nil t))
  7208. 'macro)))
  7209. (setq last-stmt-start beg
  7210. beg (point)
  7211. move tentative-move))
  7212. (goto-char beg))
  7213. (when c-recognize-knr-p
  7214. (let ((fallback-pos (point)) knr-argdecl-start)
  7215. ;; Handle K&R argdecls. Back up after the "statement" jumped
  7216. ;; over by `c-beginning-of-statement-1', unless it was the
  7217. ;; function body, in which case we're sitting on the opening
  7218. ;; brace now. Then test if we're in a K&R argdecl region and
  7219. ;; that we started at the other side of the first argdecl in
  7220. ;; it.
  7221. (unless (eq (char-after) ?{)
  7222. (goto-char last-stmt-start))
  7223. (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
  7224. (< knr-argdecl-start start)
  7225. (progn
  7226. (goto-char knr-argdecl-start)
  7227. (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
  7228. (throw 'return
  7229. (cons (if (eq (char-after fallback-pos) ?{)
  7230. 'previous
  7231. 'same)
  7232. knr-argdecl-start))
  7233. (goto-char fallback-pos))))
  7234. ;; `c-beginning-of-statement-1' counts each brace block as a separate
  7235. ;; statement, so the result will be 'previous if we've moved over any.
  7236. ;; So change our result back to 'same if necessary.
  7237. ;;
  7238. ;; If they were brace list initializers we might not have moved over a
  7239. ;; declaration boundary though, so change it to 'same if we've moved
  7240. ;; past a '=' before '{', but not ';'. (This ought to be integrated
  7241. ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
  7242. ;; potentially can search over a large amount of text.). Take special
  7243. ;; pains not to get mislead by C++'s "operator=", and the like.
  7244. (if (and (eq move 'previous)
  7245. (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
  7246. c++-template-syntax-table
  7247. (syntax-table))
  7248. (save-excursion
  7249. (and
  7250. (progn
  7251. (while ; keep going back to "[;={"s until we either find
  7252. ; no more, or get to one which isn't an "operator ="
  7253. (and (c-syntactic-re-search-forward "[;={]" start t t t)
  7254. (eq (char-before) ?=)
  7255. c-overloadable-operators-regexp
  7256. c-opt-op-identifier-prefix
  7257. (save-excursion
  7258. (eq (c-backward-token-2) 0)
  7259. (looking-at c-overloadable-operators-regexp)
  7260. (eq (c-backward-token-2) 0)
  7261. (looking-at c-opt-op-identifier-prefix))))
  7262. (eq (char-before) ?=))
  7263. (c-syntactic-re-search-forward "[;{]" start t t)
  7264. (eq (char-before) ?{)
  7265. (c-safe (goto-char (c-up-list-forward (point))) t)
  7266. (not (c-syntactic-re-search-forward ";" start t t))))))
  7267. (cons 'same nil)
  7268. (cons move nil)))))
  7269. (defun c-end-of-decl-1 ()
  7270. ;; Assuming point is at the start of a declaration (as detected by
  7271. ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
  7272. ;; `c-beginning-of-decl-1', this function handles the case when a
  7273. ;; block is followed by identifiers in e.g. struct declarations in C
  7274. ;; or C++. If a proper end was found then t is returned, otherwise
  7275. ;; point is moved as far as possible within the current sexp and nil
  7276. ;; is returned. This function doesn't handle macros; use
  7277. ;; `c-end-of-macro' instead in those cases.
  7278. ;;
  7279. ;; This function might do hidden buffer changes.
  7280. (let ((start (point))
  7281. (decl-syntax-table (if (c-major-mode-is 'c++-mode)
  7282. c++-template-syntax-table
  7283. (syntax-table))))
  7284. (catch 'return
  7285. (c-search-decl-header-end)
  7286. (when (and c-recognize-knr-p
  7287. (eq (char-before) ?\;)
  7288. (c-in-knr-argdecl start))
  7289. ;; Stopped at the ';' in a K&R argdecl section which is
  7290. ;; detected using the same criteria as in
  7291. ;; `c-beginning-of-decl-1'. Move to the following block
  7292. ;; start.
  7293. (c-syntactic-re-search-forward "{" nil 'move t))
  7294. (when (eq (char-before) ?{)
  7295. ;; Encountered a block in the declaration. Jump over it.
  7296. (condition-case nil
  7297. (goto-char (c-up-list-forward (point)))
  7298. (error (goto-char (point-max))
  7299. (throw 'return nil)))
  7300. (if (or (not c-opt-block-decls-with-vars-key)
  7301. (save-excursion
  7302. (c-with-syntax-table decl-syntax-table
  7303. (let ((lim (point)))
  7304. (goto-char start)
  7305. (not (and
  7306. ;; Check for `c-opt-block-decls-with-vars-key'
  7307. ;; before the first paren.
  7308. (c-syntactic-re-search-forward
  7309. (concat "[;=\(\[{]\\|\\("
  7310. c-opt-block-decls-with-vars-key
  7311. "\\)")
  7312. lim t t t)
  7313. (match-beginning 1)
  7314. (not (eq (char-before) ?_))
  7315. ;; Check that the first following paren is
  7316. ;; the block.
  7317. (c-syntactic-re-search-forward "[;=\(\[{]"
  7318. lim t t t)
  7319. (eq (char-before) ?{)))))))
  7320. ;; The declaration doesn't have any of the
  7321. ;; `c-opt-block-decls-with-vars' keywords in the
  7322. ;; beginning, so it ends here at the end of the block.
  7323. (throw 'return t)))
  7324. (c-with-syntax-table decl-syntax-table
  7325. (while (progn
  7326. (if (eq (char-before) ?\;)
  7327. (throw 'return t))
  7328. (c-syntactic-re-search-forward ";" nil 'move t))))
  7329. nil)))
  7330. (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
  7331. ;; Assuming the point is at an open brace, check if it starts a
  7332. ;; block that contains another declaration level, i.e. that isn't a
  7333. ;; statement block or a brace list, and if so return non-nil.
  7334. ;;
  7335. ;; If the check is successful, the return value is the start of the
  7336. ;; keyword that tells what kind of construct it is, i.e. typically
  7337. ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
  7338. ;; the point will be at the start of the construct, before any
  7339. ;; leading specifiers, otherwise it's at the returned position.
  7340. ;;
  7341. ;; The point is clobbered if the check is unsuccessful.
  7342. ;;
  7343. ;; CONTAINING-SEXP is the position of the open of the surrounding
  7344. ;; paren, or nil if none.
  7345. ;;
  7346. ;; The optional LIMIT limits the backward search for the start of
  7347. ;; the construct. It's assumed to be at a syntactically relevant
  7348. ;; position.
  7349. ;;
  7350. ;; If any template arglists are found in the searched region before
  7351. ;; the open brace, they get marked with paren syntax.
  7352. ;;
  7353. ;; This function might do hidden buffer changes.
  7354. (let ((open-brace (point)) kwd-start first-specifier-pos)
  7355. (c-syntactic-skip-backward c-block-prefix-charset limit t)
  7356. (when (and c-recognize-<>-arglists
  7357. (eq (char-before) ?>))
  7358. ;; Could be at the end of a template arglist.
  7359. (let ((c-parse-and-markup-<>-arglists t)
  7360. (c-disallow-comma-in-<>-arglists
  7361. (and containing-sexp
  7362. (not (eq (char-after containing-sexp) ?{)))))
  7363. (while (and
  7364. (c-backward-<>-arglist nil limit)
  7365. (progn
  7366. (c-syntactic-skip-backward c-block-prefix-charset limit t)
  7367. (eq (char-before) ?>))))))
  7368. ;; Note: Can't get bogus hits inside template arglists below since they
  7369. ;; have gotten paren syntax above.
  7370. (when (and
  7371. ;; If `goto-start' is set we begin by searching for the
  7372. ;; first possible position of a leading specifier list.
  7373. ;; The `c-decl-block-key' search continues from there since
  7374. ;; we know it can't match earlier.
  7375. (if goto-start
  7376. (when (c-syntactic-re-search-forward c-symbol-start
  7377. open-brace t t)
  7378. (goto-char (setq first-specifier-pos (match-beginning 0)))
  7379. t)
  7380. t)
  7381. (cond
  7382. ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
  7383. (goto-char (setq kwd-start (match-beginning 0)))
  7384. (or
  7385. ;; Found a keyword that can't be a type?
  7386. (match-beginning 1)
  7387. ;; Can be a type too, in which case it's the return type of a
  7388. ;; function (under the assumption that no declaration level
  7389. ;; block construct starts with a type).
  7390. (not (c-forward-type))
  7391. ;; Jumped over a type, but it could be a declaration keyword
  7392. ;; followed by the declared identifier that we've jumped over
  7393. ;; instead (e.g. in "class Foo {"). If it indeed is a type
  7394. ;; then we should be at the declarator now, so check for a
  7395. ;; valid declarator start.
  7396. ;;
  7397. ;; Note: This doesn't cope with the case when a declared
  7398. ;; identifier is followed by e.g. '(' in a language where '('
  7399. ;; also might be part of a declarator expression. Currently
  7400. ;; there's no such language.
  7401. (not (or (looking-at c-symbol-start)
  7402. (looking-at c-type-decl-prefix-key)))))
  7403. ;; In Pike a list of modifiers may be followed by a brace
  7404. ;; to make them apply to many identifiers. Note that the
  7405. ;; match data will be empty on return in this case.
  7406. ((and (c-major-mode-is 'pike-mode)
  7407. (progn
  7408. (goto-char open-brace)
  7409. (= (c-backward-token-2) 0))
  7410. (looking-at c-specifier-key)
  7411. ;; Use this variant to avoid yet another special regexp.
  7412. (c-keyword-member (c-keyword-sym (match-string 1))
  7413. 'c-modifier-kwds))
  7414. (setq kwd-start (point))
  7415. t)))
  7416. ;; Got a match.
  7417. (if goto-start
  7418. ;; Back up over any preceding specifiers and their clauses
  7419. ;; by going forward from `first-specifier-pos', which is the
  7420. ;; earliest possible position where the specifier list can
  7421. ;; start.
  7422. (progn
  7423. (goto-char first-specifier-pos)
  7424. (while (< (point) kwd-start)
  7425. (if (looking-at c-symbol-key)
  7426. ;; Accept any plain symbol token on the ground that
  7427. ;; it's a specifier masked through a macro (just
  7428. ;; like `c-forward-decl-or-cast-1' skip forward over
  7429. ;; such tokens).
  7430. ;;
  7431. ;; Could be more restrictive wrt invalid keywords,
  7432. ;; but that'd only occur in invalid code so there's
  7433. ;; no use spending effort on it.
  7434. (let ((end (match-end 0)))
  7435. (unless (c-forward-keyword-clause 0)
  7436. (goto-char end)
  7437. (c-forward-syntactic-ws)))
  7438. ;; Can't parse a declaration preamble and is still
  7439. ;; before `kwd-start'. That means `first-specifier-pos'
  7440. ;; was in some earlier construct. Search again.
  7441. (if (c-syntactic-re-search-forward c-symbol-start
  7442. kwd-start 'move t)
  7443. (goto-char (setq first-specifier-pos (match-beginning 0)))
  7444. ;; Got no preamble before the block declaration keyword.
  7445. (setq first-specifier-pos kwd-start))))
  7446. (goto-char first-specifier-pos))
  7447. (goto-char kwd-start))
  7448. kwd-start)))
  7449. (defun c-search-uplist-for-classkey (paren-state)
  7450. ;; Check if the closest containing paren sexp is a declaration
  7451. ;; block, returning a 2 element vector in that case. Aref 0
  7452. ;; contains the bufpos at boi of the class key line, and aref 1
  7453. ;; contains the bufpos of the open brace. This function is an
  7454. ;; obsolete wrapper for `c-looking-at-decl-block'.
  7455. ;;
  7456. ;; This function might do hidden buffer changes.
  7457. (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
  7458. (when open-paren-pos
  7459. (save-excursion
  7460. (goto-char open-paren-pos)
  7461. (when (and (eq (char-after) ?{)
  7462. (c-looking-at-decl-block
  7463. (c-safe-position open-paren-pos paren-state)
  7464. nil))
  7465. (back-to-indentation)
  7466. (vector (point) open-paren-pos))))))
  7467. (defmacro c-pull-open-brace (ps)
  7468. ;; Pull the next open brace from PS (which has the form of paren-state),
  7469. ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
  7470. `(progn
  7471. (while (consp (car ,ps))
  7472. (setq ,ps (cdr ,ps)))
  7473. (prog1 (car ,ps)
  7474. (setq ,ps (cdr ,ps)))))
  7475. (defun c-most-enclosing-decl-block (paren-state)
  7476. ;; Return the buffer position of the most enclosing decl-block brace (in the
  7477. ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
  7478. ;; none was found.
  7479. (let* ((open-brace (c-pull-open-brace paren-state))
  7480. (next-open-brace (c-pull-open-brace paren-state)))
  7481. (while (and open-brace
  7482. (save-excursion
  7483. (goto-char open-brace)
  7484. (not (c-looking-at-decl-block next-open-brace nil))))
  7485. (setq open-brace next-open-brace
  7486. next-open-brace (c-pull-open-brace paren-state)))
  7487. open-brace))
  7488. (defun c-cheap-inside-bracelist-p (paren-state)
  7489. ;; Return the position of the L-brace if point is inside a brace list
  7490. ;; initialization of an array, etc. This is an approximate function,
  7491. ;; designed for speed over accuracy. It will not find every bracelist, but
  7492. ;; a non-nil result is reliable. We simply search for "= {" (naturally with
  7493. ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
  7494. ;; is everywhere else.
  7495. (let (b-pos)
  7496. (save-excursion
  7497. (while
  7498. (and (setq b-pos (c-pull-open-brace paren-state))
  7499. (progn (goto-char b-pos)
  7500. (c-backward-sws)
  7501. (c-backward-token-2)
  7502. (not (looking-at "=")))))
  7503. b-pos)))
  7504. (defun c-inside-bracelist-p (containing-sexp paren-state)
  7505. ;; return the buffer position of the beginning of the brace list
  7506. ;; statement if we're inside a brace list, otherwise return nil.
  7507. ;; CONTAINING-SEXP is the buffer pos of the innermost containing
  7508. ;; paren. PAREN-STATE is the remainder of the state of enclosing
  7509. ;; braces
  7510. ;;
  7511. ;; N.B.: This algorithm can potentially get confused by cpp macros
  7512. ;; placed in inconvenient locations. It's a trade-off we make for
  7513. ;; speed.
  7514. ;;
  7515. ;; This function might do hidden buffer changes.
  7516. (or
  7517. ;; This will pick up brace list declarations.
  7518. (c-safe
  7519. (save-excursion
  7520. (goto-char containing-sexp)
  7521. (c-forward-sexp -1)
  7522. (let (bracepos)
  7523. (if (and (or (looking-at c-brace-list-key)
  7524. (progn (c-forward-sexp -1)
  7525. (looking-at c-brace-list-key)))
  7526. (setq bracepos (c-down-list-forward (point)))
  7527. (not (c-crosses-statement-barrier-p (point)
  7528. (- bracepos 2))))
  7529. (point)))))
  7530. ;; this will pick up array/aggregate init lists, even if they are nested.
  7531. (save-excursion
  7532. (let ((class-key
  7533. ;; Pike can have class definitions anywhere, so we must
  7534. ;; check for the class key here.
  7535. (and (c-major-mode-is 'pike-mode)
  7536. c-decl-block-key))
  7537. bufpos braceassignp lim next-containing)
  7538. (while (and (not bufpos)
  7539. containing-sexp)
  7540. (when paren-state
  7541. (if (consp (car paren-state))
  7542. (setq lim (cdr (car paren-state))
  7543. paren-state (cdr paren-state))
  7544. (setq lim (car paren-state)))
  7545. (when paren-state
  7546. (setq next-containing (car paren-state)
  7547. paren-state (cdr paren-state))))
  7548. (goto-char containing-sexp)
  7549. (if (c-looking-at-inexpr-block next-containing next-containing)
  7550. ;; We're in an in-expression block of some kind. Do not
  7551. ;; check nesting. We deliberately set the limit to the
  7552. ;; containing sexp, so that c-looking-at-inexpr-block
  7553. ;; doesn't check for an identifier before it.
  7554. (setq containing-sexp nil)
  7555. ;; see if the open brace is preceded by = or [...] in
  7556. ;; this statement, but watch out for operator=
  7557. (setq braceassignp 'dontknow)
  7558. (c-backward-token-2 1 t lim)
  7559. ;; Checks to do only on the first sexp before the brace.
  7560. (when (and c-opt-inexpr-brace-list-key
  7561. (eq (char-after) ?\[))
  7562. ;; In Java, an initialization brace list may follow
  7563. ;; directly after "new Foo[]", so check for a "new"
  7564. ;; earlier.
  7565. (while (eq braceassignp 'dontknow)
  7566. (setq braceassignp
  7567. (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
  7568. ((looking-at c-opt-inexpr-brace-list-key) t)
  7569. ((looking-at "\\sw\\|\\s_\\|[.[]")
  7570. ;; Carry on looking if this is an
  7571. ;; identifier (may contain "." in Java)
  7572. ;; or another "[]" sexp.
  7573. 'dontknow)
  7574. (t nil)))))
  7575. ;; Checks to do on all sexps before the brace, up to the
  7576. ;; beginning of the statement.
  7577. (while (eq braceassignp 'dontknow)
  7578. (cond ((eq (char-after) ?\;)
  7579. (setq braceassignp nil))
  7580. ((and class-key
  7581. (looking-at class-key))
  7582. (setq braceassignp nil))
  7583. ((eq (char-after) ?=)
  7584. ;; We've seen a =, but must check earlier tokens so
  7585. ;; that it isn't something that should be ignored.
  7586. (setq braceassignp 'maybe)
  7587. (while (and (eq braceassignp 'maybe)
  7588. (zerop (c-backward-token-2 1 t lim)))
  7589. (setq braceassignp
  7590. (cond
  7591. ;; Check for operator =
  7592. ((and c-opt-op-identifier-prefix
  7593. (looking-at c-opt-op-identifier-prefix))
  7594. nil)
  7595. ;; Check for `<opchar>= in Pike.
  7596. ((and (c-major-mode-is 'pike-mode)
  7597. (or (eq (char-after) ?`)
  7598. ;; Special case for Pikes
  7599. ;; `[]=, since '[' is not in
  7600. ;; the punctuation class.
  7601. (and (eq (char-after) ?\[)
  7602. (eq (char-before) ?`))))
  7603. nil)
  7604. ((looking-at "\\s.") 'maybe)
  7605. ;; make sure we're not in a C++ template
  7606. ;; argument assignment
  7607. ((and
  7608. (c-major-mode-is 'c++-mode)
  7609. (save-excursion
  7610. (let ((here (point))
  7611. (pos< (progn
  7612. (skip-chars-backward "^<>")
  7613. (point))))
  7614. (and (eq (char-before) ?<)
  7615. (not (c-crosses-statement-barrier-p
  7616. pos< here))
  7617. (not (c-in-literal))
  7618. ))))
  7619. nil)
  7620. (t t))))))
  7621. (if (and (eq braceassignp 'dontknow)
  7622. (/= (c-backward-token-2 1 t lim) 0))
  7623. (setq braceassignp nil)))
  7624. (if (not braceassignp)
  7625. (if (eq (char-after) ?\;)
  7626. ;; Brace lists can't contain a semicolon, so we're done.
  7627. (setq containing-sexp nil)
  7628. ;; Go up one level.
  7629. (setq containing-sexp next-containing
  7630. lim nil
  7631. next-containing nil))
  7632. ;; we've hit the beginning of the aggregate list
  7633. (c-beginning-of-statement-1
  7634. (c-most-enclosing-brace paren-state))
  7635. (setq bufpos (point))))
  7636. )
  7637. bufpos))
  7638. ))
  7639. (defun c-looking-at-special-brace-list (&optional lim)
  7640. ;; If we're looking at the start of a pike-style list, ie `({ })',
  7641. ;; `([ ])', `(< >)' etc, a cons of a cons of its starting and ending
  7642. ;; positions and its entry in c-special-brace-lists is returned, nil
  7643. ;; otherwise. The ending position is nil if the list is still open.
  7644. ;; LIM is the limit for forward search. The point may either be at
  7645. ;; the `(' or at the following paren character. Tries to check the
  7646. ;; matching closer, but assumes it's correct if no balanced paren is
  7647. ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
  7648. ;; a special brace list).
  7649. ;;
  7650. ;; This function might do hidden buffer changes.
  7651. (if c-special-brace-lists
  7652. (condition-case ()
  7653. (save-excursion
  7654. (let ((beg (point))
  7655. inner-beg end type)
  7656. (c-forward-syntactic-ws)
  7657. (if (eq (char-after) ?\()
  7658. (progn
  7659. (forward-char 1)
  7660. (c-forward-syntactic-ws)
  7661. (setq inner-beg (point))
  7662. (setq type (assq (char-after) c-special-brace-lists)))
  7663. (if (setq type (assq (char-after) c-special-brace-lists))
  7664. (progn
  7665. (setq inner-beg (point))
  7666. (c-backward-syntactic-ws)
  7667. (forward-char -1)
  7668. (setq beg (if (eq (char-after) ?\()
  7669. (point)
  7670. nil)))))
  7671. (if (and beg type)
  7672. (if (and (c-safe
  7673. (goto-char beg)
  7674. (c-forward-sexp 1)
  7675. (setq end (point))
  7676. (= (char-before) ?\)))
  7677. (c-safe
  7678. (goto-char inner-beg)
  7679. (if (looking-at "\\s(")
  7680. ;; Check balancing of the inner paren
  7681. ;; below.
  7682. (progn
  7683. (c-forward-sexp 1)
  7684. t)
  7685. ;; If the inner char isn't a paren then
  7686. ;; we can't check balancing, so just
  7687. ;; check the char before the outer
  7688. ;; closing paren.
  7689. (goto-char end)
  7690. (backward-char)
  7691. (c-backward-syntactic-ws)
  7692. (= (char-before) (cdr type)))))
  7693. (if (or (/= (char-syntax (char-before)) ?\))
  7694. (= (progn
  7695. (c-forward-syntactic-ws)
  7696. (point))
  7697. (1- end)))
  7698. (cons (cons beg end) type))
  7699. (cons (list beg) type)))))
  7700. (error nil))))
  7701. (defun c-looking-at-bos (&optional lim)
  7702. ;; Return non-nil if between two statements or declarations, assuming
  7703. ;; point is not inside a literal or comment.
  7704. ;;
  7705. ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
  7706. ;; are recommended instead.
  7707. ;;
  7708. ;; This function might do hidden buffer changes.
  7709. (c-at-statement-start-p))
  7710. (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
  7711. (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
  7712. ;; Return non-nil if we're looking at the beginning of a block
  7713. ;; inside an expression. The value returned is actually a cons of
  7714. ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
  7715. ;; position of the beginning of the construct.
  7716. ;;
  7717. ;; LIM limits the backward search. CONTAINING-SEXP is the start
  7718. ;; position of the closest containing list. If it's nil, the
  7719. ;; containing paren isn't used to decide whether we're inside an
  7720. ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
  7721. ;; needs to be farther back.
  7722. ;;
  7723. ;; If CHECK-AT-END is non-nil then extra checks at the end of the
  7724. ;; brace block might be done. It should only be used when the
  7725. ;; construct can be assumed to be complete, i.e. when the original
  7726. ;; starting position was further down than that.
  7727. ;;
  7728. ;; This function might do hidden buffer changes.
  7729. (save-excursion
  7730. (let ((res 'maybe) passed-paren
  7731. (closest-lim (or containing-sexp lim (point-min)))
  7732. ;; Look at the character after point only as a last resort
  7733. ;; when we can't disambiguate.
  7734. (block-follows (and (eq (char-after) ?{) (point))))
  7735. (while (and (eq res 'maybe)
  7736. (progn (c-backward-syntactic-ws)
  7737. (> (point) closest-lim))
  7738. (not (bobp))
  7739. (progn (backward-char)
  7740. (looking-at "[\]\).]\\|\\w\\|\\s_"))
  7741. (c-safe (forward-char)
  7742. (goto-char (scan-sexps (point) -1))))
  7743. (setq res
  7744. (if (looking-at c-keywords-regexp)
  7745. (let ((kw-sym (c-keyword-sym (match-string 1))))
  7746. (cond
  7747. ((and block-follows
  7748. (c-keyword-member kw-sym 'c-inexpr-class-kwds))
  7749. (and (not (eq passed-paren ?\[))
  7750. (or (not (looking-at c-class-key))
  7751. ;; If the class definition is at the start of
  7752. ;; a statement, we don't consider it an
  7753. ;; in-expression class.
  7754. (let ((prev (point)))
  7755. (while (and
  7756. (= (c-backward-token-2 1 nil closest-lim) 0)
  7757. (eq (char-syntax (char-after)) ?w))
  7758. (setq prev (point)))
  7759. (goto-char prev)
  7760. (not (c-at-statement-start-p)))
  7761. ;; Also, in Pike we treat it as an
  7762. ;; in-expression class if it's used in an
  7763. ;; object clone expression.
  7764. (save-excursion
  7765. (and check-at-end
  7766. (c-major-mode-is 'pike-mode)
  7767. (progn (goto-char block-follows)
  7768. (zerop (c-forward-token-2 1 t)))
  7769. (eq (char-after) ?\())))
  7770. (cons 'inexpr-class (point))))
  7771. ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
  7772. (when (not passed-paren)
  7773. (cons 'inexpr-statement (point))))
  7774. ((c-keyword-member kw-sym 'c-lambda-kwds)
  7775. (when (or (not passed-paren)
  7776. (eq passed-paren ?\())
  7777. (cons 'inlambda (point))))
  7778. ((c-keyword-member kw-sym 'c-block-stmt-kwds)
  7779. nil)
  7780. (t
  7781. 'maybe)))
  7782. (if (looking-at "\\s(")
  7783. (if passed-paren
  7784. (if (and (eq passed-paren ?\[)
  7785. (eq (char-after) ?\[))
  7786. ;; Accept several square bracket sexps for
  7787. ;; Java array initializations.
  7788. 'maybe)
  7789. (setq passed-paren (char-after))
  7790. 'maybe)
  7791. 'maybe))))
  7792. (if (eq res 'maybe)
  7793. (when (and c-recognize-paren-inexpr-blocks
  7794. block-follows
  7795. containing-sexp
  7796. (eq (char-after containing-sexp) ?\())
  7797. (goto-char containing-sexp)
  7798. (if (or (save-excursion
  7799. (c-backward-syntactic-ws lim)
  7800. (and (> (point) (or lim (point-min)))
  7801. (c-on-identifier)))
  7802. (and c-special-brace-lists
  7803. (c-looking-at-special-brace-list)))
  7804. nil
  7805. (cons 'inexpr-statement (point))))
  7806. res))))
  7807. (defun c-looking-at-inexpr-block-backward (paren-state)
  7808. ;; Returns non-nil if we're looking at the end of an in-expression
  7809. ;; block, otherwise the same as `c-looking-at-inexpr-block'.
  7810. ;; PAREN-STATE is the paren state relevant at the current position.
  7811. ;;
  7812. ;; This function might do hidden buffer changes.
  7813. (save-excursion
  7814. ;; We currently only recognize a block.
  7815. (let ((here (point))
  7816. (elem (car-safe paren-state))
  7817. containing-sexp)
  7818. (when (and (consp elem)
  7819. (progn (goto-char (cdr elem))
  7820. (c-forward-syntactic-ws here)
  7821. (= (point) here)))
  7822. (goto-char (car elem))
  7823. (if (setq paren-state (cdr paren-state))
  7824. (setq containing-sexp (car-safe paren-state)))
  7825. (c-looking-at-inexpr-block (c-safe-position containing-sexp
  7826. paren-state)
  7827. containing-sexp)))))
  7828. (defun c-at-macro-vsemi-p (&optional pos)
  7829. ;; Is there a "virtual semicolon" at POS or point?
  7830. ;; (See cc-defs.el for full details of "virtual semicolons".)
  7831. ;;
  7832. ;; This is true when point is at the last non syntactic WS position on the
  7833. ;; line, there is a macro call last on the line, and this particular macro's
  7834. ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
  7835. ;; semicolon.
  7836. (save-excursion
  7837. (save-restriction
  7838. (widen)
  7839. (if pos
  7840. (goto-char pos)
  7841. (setq pos (point)))
  7842. (and
  7843. c-macro-with-semi-re
  7844. (eq (skip-chars-backward " \t") 0)
  7845. ;; Check we've got nothing after this except comments and empty lines
  7846. ;; joined by escaped EOLs.
  7847. (skip-chars-forward " \t") ; always returns non-nil.
  7848. (progn
  7849. (while ; go over 1 block comment per iteration.
  7850. (and
  7851. (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
  7852. (goto-char (match-end 0))
  7853. (cond
  7854. ((looking-at c-block-comment-start-regexp)
  7855. (and (forward-comment 1)
  7856. (skip-chars-forward " \t"))) ; always returns non-nil
  7857. ((looking-at c-line-comment-start-regexp)
  7858. (end-of-line)
  7859. nil)
  7860. (t nil))))
  7861. (eolp))
  7862. (goto-char pos)
  7863. (progn (c-backward-syntactic-ws)
  7864. (eq (point) pos))
  7865. ;; Check for one of the listed macros being before point.
  7866. (or (not (eq (char-before) ?\)))
  7867. (when (c-go-list-backward)
  7868. (c-backward-syntactic-ws)
  7869. t))
  7870. (c-simple-skip-symbol-backward)
  7871. (looking-at c-macro-with-semi-re)
  7872. (goto-char pos)
  7873. (not (c-in-literal)))))) ; The most expensive check last.
  7874. (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
  7875. ;; `c-guess-basic-syntax' and the functions that precedes it below
  7876. ;; implements the main decision tree for determining the syntactic
  7877. ;; analysis of the current line of code.
  7878. ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
  7879. ;; auto newline analysis.
  7880. (defvar c-auto-newline-analysis nil)
  7881. (defun c-brace-anchor-point (bracepos)
  7882. ;; BRACEPOS is the position of a brace in a construct like "namespace
  7883. ;; Bar {". Return the anchor point in this construct; this is the
  7884. ;; earliest symbol on the brace's line which isn't earlier than
  7885. ;; "namespace".
  7886. ;;
  7887. ;; Currently (2007-08-17), "like namespace" means "matches
  7888. ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
  7889. ;; or anything like that.
  7890. (save-excursion
  7891. (let ((boi (c-point 'boi bracepos)))
  7892. (goto-char bracepos)
  7893. (while (and (> (point) boi)
  7894. (not (looking-at c-other-decl-block-key)))
  7895. (c-backward-token-2))
  7896. (if (> (point) boi) (point) boi))))
  7897. (defsubst c-add-syntax (symbol &rest args)
  7898. ;; A simple function to prepend a new syntax element to
  7899. ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
  7900. ;; should always be dynamically bound but since we read it first
  7901. ;; we'll fail properly anyway if this function is misused.
  7902. (setq c-syntactic-context (cons (cons symbol args)
  7903. c-syntactic-context)))
  7904. (defsubst c-append-syntax (symbol &rest args)
  7905. ;; Like `c-add-syntax' but appends to the end of the syntax list.
  7906. ;; (Normally not necessary.)
  7907. (setq c-syntactic-context (nconc c-syntactic-context
  7908. (list (cons symbol args)))))
  7909. (defun c-add-stmt-syntax (syntax-symbol
  7910. syntax-extra-args
  7911. stop-at-boi-only
  7912. containing-sexp
  7913. paren-state)
  7914. ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
  7915. ;; needed with further syntax elements of the types `substatement',
  7916. ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
  7917. ;; `defun-block-intro'.
  7918. ;;
  7919. ;; Do the generic processing to anchor the given syntax symbol on
  7920. ;; the preceding statement: Skip over any labels and containing
  7921. ;; statements on the same line, and then search backward until we
  7922. ;; find a statement or block start that begins at boi without a
  7923. ;; label or comment.
  7924. ;;
  7925. ;; Point is assumed to be at the prospective anchor point for the
  7926. ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
  7927. ;; skip past open parens and containing statements. Most of the added
  7928. ;; syntax elements will get the same anchor point - the exception is
  7929. ;; for an anchor in a construct like "namespace"[*] - this is as early
  7930. ;; as possible in the construct but on the same line as the {.
  7931. ;;
  7932. ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
  7933. ;;
  7934. ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
  7935. ;; syntax symbol. They are appended after the anchor point.
  7936. ;;
  7937. ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
  7938. ;; if the current statement starts there.
  7939. ;;
  7940. ;; Note: It's not a problem if PAREN-STATE "overshoots"
  7941. ;; CONTAINING-SEXP, i.e. contains info about parens further down.
  7942. ;;
  7943. ;; This function might do hidden buffer changes.
  7944. (if (= (point) (c-point 'boi))
  7945. ;; This is by far the most common case, so let's give it special
  7946. ;; treatment.
  7947. (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
  7948. (let ((syntax-last c-syntactic-context)
  7949. (boi (c-point 'boi))
  7950. ;; Set when we're on a label, so that we don't stop there.
  7951. ;; FIXME: To be complete we should check if we're on a label
  7952. ;; now at the start.
  7953. on-label)
  7954. ;; Use point as the anchor point for "namespace", "extern", etc.
  7955. (apply 'c-add-syntax syntax-symbol
  7956. (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
  7957. (point) nil)
  7958. syntax-extra-args)
  7959. ;; Loop while we have to back out of containing blocks.
  7960. (while
  7961. (and
  7962. (catch 'back-up-block
  7963. ;; Loop while we have to back up statements.
  7964. (while (or (/= (point) boi)
  7965. on-label
  7966. (looking-at c-comment-start-regexp))
  7967. ;; Skip past any comments that stands between the
  7968. ;; statement start and boi.
  7969. (let ((savepos (point)))
  7970. (while (and (/= savepos boi)
  7971. (c-backward-single-comment))
  7972. (setq savepos (point)
  7973. boi (c-point 'boi)))
  7974. (goto-char savepos))
  7975. ;; Skip to the beginning of this statement or backward
  7976. ;; another one.
  7977. (let ((old-pos (point))
  7978. (old-boi boi)
  7979. (step-type (c-beginning-of-statement-1 containing-sexp)))
  7980. (setq boi (c-point 'boi)
  7981. on-label (eq step-type 'label))
  7982. (cond ((= (point) old-pos)
  7983. ;; If we didn't move we're at the start of a block and
  7984. ;; have to continue outside it.
  7985. (throw 'back-up-block t))
  7986. ((and (eq step-type 'up)
  7987. (>= (point) old-boi)
  7988. (looking-at "else\\>[^_]")
  7989. (save-excursion
  7990. (goto-char old-pos)
  7991. (looking-at "if\\>[^_]")))
  7992. ;; Special case to avoid deeper and deeper indentation
  7993. ;; of "else if" clauses.
  7994. )
  7995. ((and (not stop-at-boi-only)
  7996. (/= old-pos old-boi)
  7997. (memq step-type '(up previous)))
  7998. ;; If stop-at-boi-only is nil, we shouldn't back up
  7999. ;; over previous or containing statements to try to
  8000. ;; reach boi, so go back to the last position and
  8001. ;; exit.
  8002. (goto-char old-pos)
  8003. (throw 'back-up-block nil))
  8004. (t
  8005. (if (and (not stop-at-boi-only)
  8006. (memq step-type '(up previous beginning)))
  8007. ;; If we've moved into another statement then we
  8008. ;; should no longer try to stop in the middle of a
  8009. ;; line.
  8010. (setq stop-at-boi-only t))
  8011. ;; Record this as a substatement if we skipped up one
  8012. ;; level.
  8013. (when (eq step-type 'up)
  8014. (c-add-syntax 'substatement nil))))
  8015. )))
  8016. containing-sexp)
  8017. ;; Now we have to go out of this block.
  8018. (goto-char containing-sexp)
  8019. ;; Don't stop in the middle of a special brace list opener
  8020. ;; like "({".
  8021. (when c-special-brace-lists
  8022. (let ((special-list (c-looking-at-special-brace-list)))
  8023. (when (and special-list
  8024. (< (car (car special-list)) (point)))
  8025. (setq containing-sexp (car (car special-list)))
  8026. (goto-char containing-sexp))))
  8027. (setq paren-state (c-whack-state-after containing-sexp paren-state)
  8028. containing-sexp (c-most-enclosing-brace paren-state)
  8029. boi (c-point 'boi))
  8030. ;; Analyze the construct in front of the block we've stepped out
  8031. ;; from and add the right syntactic element for it.
  8032. (let ((paren-pos (point))
  8033. (paren-char (char-after))
  8034. step-type)
  8035. (if (eq paren-char ?\()
  8036. ;; Stepped out of a parenthesis block, so we're in an
  8037. ;; expression now.
  8038. (progn
  8039. (when (/= paren-pos boi)
  8040. (if (and c-recognize-paren-inexpr-blocks
  8041. (progn
  8042. (c-backward-syntactic-ws containing-sexp)
  8043. (or (not (looking-at "\\>"))
  8044. (not (c-on-identifier))))
  8045. (save-excursion
  8046. (goto-char (1+ paren-pos))
  8047. (c-forward-syntactic-ws)
  8048. (eq (char-after) ?{)))
  8049. ;; Stepped out of an in-expression statement. This
  8050. ;; syntactic element won't get an anchor pos.
  8051. (c-add-syntax 'inexpr-statement)
  8052. ;; A parenthesis normally belongs to an arglist.
  8053. (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
  8054. (goto-char (max boi
  8055. (if containing-sexp
  8056. (1+ containing-sexp)
  8057. (point-min))))
  8058. (setq step-type 'same
  8059. on-label nil))
  8060. ;; Stepped out of a brace block.
  8061. (setq step-type (c-beginning-of-statement-1 containing-sexp)
  8062. on-label (eq step-type 'label))
  8063. (if (and (eq step-type 'same)
  8064. (/= paren-pos (point)))
  8065. (let (inexpr)
  8066. (cond
  8067. ((save-excursion
  8068. (goto-char paren-pos)
  8069. (setq inexpr (c-looking-at-inexpr-block
  8070. (c-safe-position containing-sexp paren-state)
  8071. containing-sexp)))
  8072. (c-add-syntax (if (eq (car inexpr) 'inlambda)
  8073. 'defun-block-intro
  8074. 'statement-block-intro)
  8075. nil))
  8076. ((looking-at c-other-decl-block-key)
  8077. (c-add-syntax
  8078. (cdr (assoc (match-string 1)
  8079. c-other-decl-block-key-in-symbols-alist))
  8080. (max (c-point 'boi paren-pos) (point))))
  8081. (t (c-add-syntax 'defun-block-intro nil))))
  8082. (c-add-syntax 'statement-block-intro nil)))
  8083. (if (= paren-pos boi)
  8084. ;; Always done if the open brace was at boi. The
  8085. ;; c-beginning-of-statement-1 call above is necessary
  8086. ;; anyway, to decide the type of block-intro to add.
  8087. (goto-char paren-pos)
  8088. (setq boi (c-point 'boi)))
  8089. ))
  8090. ;; Fill in the current point as the anchor for all the symbols
  8091. ;; added above.
  8092. (let ((p c-syntactic-context) q)
  8093. (while (not (eq p syntax-last))
  8094. (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
  8095. (while q
  8096. (unless (car q)
  8097. (setcar q (point)))
  8098. (setq q (cdr q)))
  8099. (setq p (cdr p))))
  8100. )))
  8101. (defun c-add-class-syntax (symbol
  8102. containing-decl-open
  8103. containing-decl-start
  8104. containing-decl-kwd
  8105. paren-state)
  8106. ;; The inclass and class-close syntactic symbols are added in
  8107. ;; several places and some work is needed to fix everything.
  8108. ;; Therefore it's collected here.
  8109. ;;
  8110. ;; This function might do hidden buffer changes.
  8111. (goto-char containing-decl-open)
  8112. (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
  8113. (progn
  8114. (c-add-syntax symbol containing-decl-open)
  8115. containing-decl-open)
  8116. (goto-char containing-decl-start)
  8117. ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
  8118. ;; here, but we have to do like this for compatibility.
  8119. (back-to-indentation)
  8120. (c-add-syntax symbol (point))
  8121. (if (and (c-keyword-member containing-decl-kwd
  8122. 'c-inexpr-class-kwds)
  8123. (/= containing-decl-start (c-point 'boi containing-decl-start)))
  8124. (c-add-syntax 'inexpr-class))
  8125. (point)))
  8126. (defun c-guess-continued-construct (indent-point
  8127. char-after-ip
  8128. beg-of-same-or-containing-stmt
  8129. containing-sexp
  8130. paren-state)
  8131. ;; This function contains the decision tree reached through both
  8132. ;; cases 18 and 10. It's a continued statement or top level
  8133. ;; construct of some kind.
  8134. ;;
  8135. ;; This function might do hidden buffer changes.
  8136. (let (special-brace-list placeholder)
  8137. (goto-char indent-point)
  8138. (skip-chars-forward " \t")
  8139. (cond
  8140. ;; (CASE A removed.)
  8141. ;; CASE B: open braces for class or brace-lists
  8142. ((setq special-brace-list
  8143. (or (and c-special-brace-lists
  8144. (c-looking-at-special-brace-list))
  8145. (eq char-after-ip ?{)))
  8146. (cond
  8147. ;; CASE B.1: class-open
  8148. ((save-excursion
  8149. (and (eq (char-after) ?{)
  8150. (c-looking-at-decl-block containing-sexp t)
  8151. (setq beg-of-same-or-containing-stmt (point))))
  8152. (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
  8153. ;; CASE B.2: brace-list-open
  8154. ((or (consp special-brace-list)
  8155. (save-excursion
  8156. (goto-char beg-of-same-or-containing-stmt)
  8157. (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
  8158. indent-point t t t)))
  8159. ;; The most semantically accurate symbol here is
  8160. ;; brace-list-open, but we normally report it simply as a
  8161. ;; statement-cont. The reason is that one normally adjusts
  8162. ;; brace-list-open for brace lists as top-level constructs,
  8163. ;; and brace lists inside statements is a completely different
  8164. ;; context. C.f. case 5A.3.
  8165. (c-beginning-of-statement-1 containing-sexp)
  8166. (c-add-stmt-syntax (if c-auto-newline-analysis
  8167. ;; Turn off the dwim above when we're
  8168. ;; analyzing the nature of the brace
  8169. ;; for the auto newline feature.
  8170. 'brace-list-open
  8171. 'statement-cont)
  8172. nil nil
  8173. containing-sexp paren-state))
  8174. ;; CASE B.3: The body of a function declared inside a normal
  8175. ;; block. Can occur e.g. in Pike and when using gcc
  8176. ;; extensions, but watch out for macros followed by blocks.
  8177. ;; C.f. cases E, 16F and 17G.
  8178. ((and (not (c-at-statement-start-p))
  8179. (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
  8180. 'same)
  8181. (save-excursion
  8182. (let ((c-recognize-typeless-decls nil))
  8183. ;; Turn off recognition of constructs that lacks a
  8184. ;; type in this case, since that's more likely to be
  8185. ;; a macro followed by a block.
  8186. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  8187. (c-add-stmt-syntax 'defun-open nil t
  8188. containing-sexp paren-state))
  8189. ;; CASE B.4: Continued statement with block open. The most
  8190. ;; accurate analysis is perhaps `statement-cont' together with
  8191. ;; `block-open' but we play DWIM and use `substatement-open'
  8192. ;; instead. The rationale is that this typically is a macro
  8193. ;; followed by a block which makes it very similar to a
  8194. ;; statement with a substatement block.
  8195. (t
  8196. (c-add-stmt-syntax 'substatement-open nil nil
  8197. containing-sexp paren-state))
  8198. ))
  8199. ;; CASE C: iostream insertion or extraction operator
  8200. ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
  8201. (save-excursion
  8202. (goto-char beg-of-same-or-containing-stmt)
  8203. ;; If there is no preceding streamop in the statement
  8204. ;; then indent this line as a normal statement-cont.
  8205. (when (c-syntactic-re-search-forward
  8206. "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
  8207. (c-add-syntax 'stream-op (c-point 'boi))
  8208. t))))
  8209. ;; CASE E: In the "K&R region" of a function declared inside a
  8210. ;; normal block. C.f. case B.3.
  8211. ((and (save-excursion
  8212. ;; Check that the next token is a '{'. This works as
  8213. ;; long as no language that allows nested function
  8214. ;; definitions allows stuff like member init lists, K&R
  8215. ;; declarations or throws clauses there.
  8216. ;;
  8217. ;; Note that we do a forward search for something ahead
  8218. ;; of the indentation line here. That's not good since
  8219. ;; the user might not have typed it yet. Unfortunately
  8220. ;; it's exceedingly tricky to recognize a function
  8221. ;; prototype in a code block without resorting to this.
  8222. (c-forward-syntactic-ws)
  8223. (eq (char-after) ?{))
  8224. (not (c-at-statement-start-p))
  8225. (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
  8226. 'same)
  8227. (save-excursion
  8228. (let ((c-recognize-typeless-decls nil))
  8229. ;; Turn off recognition of constructs that lacks a
  8230. ;; type in this case, since that's more likely to be
  8231. ;; a macro followed by a block.
  8232. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  8233. (c-add-stmt-syntax 'func-decl-cont nil t
  8234. containing-sexp paren-state))
  8235. ;;CASE F: continued statement and the only preceding items are
  8236. ;;annotations.
  8237. ((and (c-major-mode-is 'java-mode)
  8238. (setq placeholder (point))
  8239. (c-beginning-of-statement-1)
  8240. (progn
  8241. (while (and (c-forward-annotation)
  8242. (< (point) placeholder))
  8243. (c-forward-syntactic-ws))
  8244. t)
  8245. (prog1
  8246. (>= (point) placeholder)
  8247. (goto-char placeholder)))
  8248. (c-beginning-of-statement-1 containing-sexp)
  8249. (c-add-syntax 'annotation-var-cont (point)))
  8250. ;; CASE G: a template list continuation?
  8251. ;; Mostly a duplication of case 5D.3 to fix templates-19:
  8252. ((and (c-major-mode-is 'c++-mode)
  8253. (save-excursion
  8254. (goto-char indent-point)
  8255. (c-with-syntax-table c++-template-syntax-table
  8256. (setq placeholder (c-up-list-backward)))
  8257. (and placeholder
  8258. (eq (char-after placeholder) ?<)
  8259. (/= (char-before placeholder) ?<)
  8260. (progn
  8261. (goto-char (1+ placeholder))
  8262. (not (looking-at c-<-op-cont-regexp))))))
  8263. (c-with-syntax-table c++-template-syntax-table
  8264. (goto-char placeholder)
  8265. (c-beginning-of-statement-1 containing-sexp t)
  8266. (if (save-excursion
  8267. (c-backward-syntactic-ws containing-sexp)
  8268. (eq (char-before) ?<))
  8269. ;; In a nested template arglist.
  8270. (progn
  8271. (goto-char placeholder)
  8272. (c-syntactic-skip-backward "^,;" containing-sexp t)
  8273. (c-forward-syntactic-ws))
  8274. (back-to-indentation)))
  8275. ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
  8276. ;; template aware.
  8277. (c-add-syntax 'template-args-cont (point) placeholder))
  8278. ;; CASE D: continued statement.
  8279. (t
  8280. (c-beginning-of-statement-1 containing-sexp)
  8281. (c-add-stmt-syntax 'statement-cont nil nil
  8282. containing-sexp paren-state))
  8283. )))
  8284. ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
  8285. ;; 2005/11/29).
  8286. ;;;###autoload
  8287. (defun c-guess-basic-syntax ()
  8288. "Return the syntactic context of the current line."
  8289. (save-excursion
  8290. (beginning-of-line)
  8291. (c-save-buffer-state
  8292. ((indent-point (point))
  8293. (case-fold-search nil)
  8294. ;; A whole ugly bunch of various temporary variables. Have
  8295. ;; to declare them here since it's not possible to declare
  8296. ;; a variable with only the scope of a cond test and the
  8297. ;; following result clauses, and most of this function is a
  8298. ;; single gigantic cond. :P
  8299. literal char-before-ip before-ws-ip char-after-ip macro-start
  8300. in-macro-expr c-syntactic-context placeholder c-in-literal-cache
  8301. step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
  8302. containing-<
  8303. ;; The following record some positions for the containing
  8304. ;; declaration block if we're directly within one:
  8305. ;; `containing-decl-open' is the position of the open
  8306. ;; brace. `containing-decl-start' is the start of the
  8307. ;; declaration. `containing-decl-kwd' is the keyword
  8308. ;; symbol of the keyword that tells what kind of block it
  8309. ;; is.
  8310. containing-decl-open
  8311. containing-decl-start
  8312. containing-decl-kwd
  8313. ;; The open paren of the closest surrounding sexp or nil if
  8314. ;; there is none.
  8315. containing-sexp
  8316. ;; The position after the closest preceding brace sexp
  8317. ;; (nested sexps are ignored), or the position after
  8318. ;; `containing-sexp' if there is none, or (point-min) if
  8319. ;; `containing-sexp' is nil.
  8320. lim
  8321. ;; The paren state outside `containing-sexp', or at
  8322. ;; `indent-point' if `containing-sexp' is nil.
  8323. (paren-state (c-parse-state))
  8324. ;; There's always at most one syntactic element which got
  8325. ;; an anchor pos. It's stored in syntactic-relpos.
  8326. syntactic-relpos
  8327. (c-stmt-delim-chars c-stmt-delim-chars))
  8328. ;; Check if we're directly inside an enclosing declaration
  8329. ;; level block.
  8330. (when (and (setq containing-sexp
  8331. (c-most-enclosing-brace paren-state))
  8332. (progn
  8333. (goto-char containing-sexp)
  8334. (eq (char-after) ?{))
  8335. (setq placeholder
  8336. (c-looking-at-decl-block
  8337. (c-most-enclosing-brace paren-state
  8338. containing-sexp)
  8339. t)))
  8340. (setq containing-decl-open containing-sexp
  8341. containing-decl-start (point)
  8342. containing-sexp nil)
  8343. (goto-char placeholder)
  8344. (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
  8345. (c-keyword-sym (match-string 1)))))
  8346. ;; Init some position variables.
  8347. (if c-state-cache
  8348. (progn
  8349. (setq containing-sexp (car paren-state)
  8350. paren-state (cdr paren-state))
  8351. (if (consp containing-sexp)
  8352. (progn
  8353. (setq lim (cdr containing-sexp))
  8354. (if (cdr c-state-cache)
  8355. ;; Ignore balanced paren. The next entry
  8356. ;; can't be another one.
  8357. (setq containing-sexp (car (cdr c-state-cache))
  8358. paren-state (cdr paren-state))
  8359. ;; If there is no surrounding open paren then
  8360. ;; put the last balanced pair back on paren-state.
  8361. (setq paren-state (cons containing-sexp paren-state)
  8362. containing-sexp nil)))
  8363. (setq lim (1+ containing-sexp))))
  8364. (setq lim (point-min)))
  8365. (when (c-beginning-of-macro)
  8366. (goto-char indent-point)
  8367. (let ((lim1 (c-determine-limit 2000)))
  8368. (setq lim (max lim lim1))))
  8369. ;; If we're in a parenthesis list then ',' delimits the
  8370. ;; "statements" rather than being an operator (with the
  8371. ;; exception of the "for" clause). This difference is
  8372. ;; typically only noticeable when statements are used in macro
  8373. ;; arglists.
  8374. (when (and containing-sexp
  8375. (eq (char-after containing-sexp) ?\())
  8376. (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
  8377. ;; cache char before and after indent point, and move point to
  8378. ;; the most likely position to perform the majority of tests
  8379. (goto-char indent-point)
  8380. (c-backward-syntactic-ws lim)
  8381. (setq before-ws-ip (point)
  8382. char-before-ip (char-before))
  8383. (goto-char indent-point)
  8384. (skip-chars-forward " \t")
  8385. (setq char-after-ip (char-after))
  8386. ;; are we in a literal?
  8387. (setq literal (c-in-literal lim))
  8388. ;; now figure out syntactic qualities of the current line
  8389. (cond
  8390. ;; CASE 1: in a string.
  8391. ((eq literal 'string)
  8392. (c-add-syntax 'string (c-point 'bopl)))
  8393. ;; CASE 2: in a C or C++ style comment.
  8394. ((and (memq literal '(c c++))
  8395. ;; This is a kludge for XEmacs where we use
  8396. ;; `buffer-syntactic-context', which doesn't correctly
  8397. ;; recognize "\*/" to end a block comment.
  8398. ;; `parse-partial-sexp' which is used by
  8399. ;; `c-literal-limits' will however do that in most
  8400. ;; versions, which results in that we get nil from
  8401. ;; `c-literal-limits' even when `c-in-literal' claims
  8402. ;; we're inside a comment.
  8403. (setq placeholder (c-literal-limits lim)))
  8404. (c-add-syntax literal (car placeholder)))
  8405. ;; CASE 3: in a cpp preprocessor macro continuation.
  8406. ((and (save-excursion
  8407. (when (c-beginning-of-macro)
  8408. (setq macro-start (point))))
  8409. (/= macro-start (c-point 'boi))
  8410. (progn
  8411. (setq tmpsymbol 'cpp-macro-cont)
  8412. (or (not c-syntactic-indentation-in-macros)
  8413. (save-excursion
  8414. (goto-char macro-start)
  8415. ;; If at the beginning of the body of a #define
  8416. ;; directive then analyze as cpp-define-intro
  8417. ;; only. Go on with the syntactic analysis
  8418. ;; otherwise. in-macro-expr is set if we're in a
  8419. ;; cpp expression, i.e. before the #define body
  8420. ;; or anywhere in a non-#define directive.
  8421. (if (c-forward-to-cpp-define-body)
  8422. (let ((indent-boi (c-point 'boi indent-point)))
  8423. (setq in-macro-expr (> (point) indent-boi)
  8424. tmpsymbol 'cpp-define-intro)
  8425. (= (point) indent-boi))
  8426. (setq in-macro-expr t)
  8427. nil)))))
  8428. (c-add-syntax tmpsymbol macro-start)
  8429. (setq macro-start nil))
  8430. ;; CASE 11: an else clause?
  8431. ((looking-at "else\\>[^_]")
  8432. (c-beginning-of-statement-1 containing-sexp)
  8433. (c-add-stmt-syntax 'else-clause nil t
  8434. containing-sexp paren-state))
  8435. ;; CASE 12: while closure of a do/while construct?
  8436. ((and (looking-at "while\\>[^_]")
  8437. (save-excursion
  8438. (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
  8439. 'beginning)
  8440. (setq placeholder (point)))))
  8441. (goto-char placeholder)
  8442. (c-add-stmt-syntax 'do-while-closure nil t
  8443. containing-sexp paren-state))
  8444. ;; CASE 13: A catch or finally clause? This case is simpler
  8445. ;; than if-else and do-while, because a block is required
  8446. ;; after every try, catch and finally.
  8447. ((save-excursion
  8448. (and (cond ((c-major-mode-is 'c++-mode)
  8449. (looking-at "catch\\>[^_]"))
  8450. ((c-major-mode-is 'java-mode)
  8451. (looking-at "\\(catch\\|finally\\)\\>[^_]")))
  8452. (and (c-safe (c-backward-syntactic-ws)
  8453. (c-backward-sexp)
  8454. t)
  8455. (eq (char-after) ?{)
  8456. (c-safe (c-backward-syntactic-ws)
  8457. (c-backward-sexp)
  8458. t)
  8459. (if (eq (char-after) ?\()
  8460. (c-safe (c-backward-sexp) t)
  8461. t))
  8462. (looking-at "\\(try\\|catch\\)\\>[^_]")
  8463. (setq placeholder (point))))
  8464. (goto-char placeholder)
  8465. (c-add-stmt-syntax 'catch-clause nil t
  8466. containing-sexp paren-state))
  8467. ;; CASE 18: A substatement we can recognize by keyword.
  8468. ((save-excursion
  8469. (and c-opt-block-stmt-key
  8470. (not (eq char-before-ip ?\;))
  8471. (not (c-at-vsemi-p before-ws-ip))
  8472. (not (memq char-after-ip '(?\) ?\] ?,)))
  8473. (or (not (eq char-before-ip ?}))
  8474. (c-looking-at-inexpr-block-backward c-state-cache))
  8475. (> (point)
  8476. (progn
  8477. ;; Ought to cache the result from the
  8478. ;; c-beginning-of-statement-1 calls here.
  8479. (setq placeholder (point))
  8480. (while (eq (setq step-type
  8481. (c-beginning-of-statement-1 lim))
  8482. 'label))
  8483. (if (eq step-type 'previous)
  8484. (goto-char placeholder)
  8485. (setq placeholder (point))
  8486. (if (and (eq step-type 'same)
  8487. (not (looking-at c-opt-block-stmt-key)))
  8488. ;; Step up to the containing statement if we
  8489. ;; stayed in the same one.
  8490. (let (step)
  8491. (while (eq
  8492. (setq step
  8493. (c-beginning-of-statement-1 lim))
  8494. 'label))
  8495. (if (eq step 'up)
  8496. (setq placeholder (point))
  8497. ;; There was no containing statement after all.
  8498. (goto-char placeholder)))))
  8499. placeholder))
  8500. (if (looking-at c-block-stmt-2-key)
  8501. ;; Require a parenthesis after these keywords.
  8502. ;; Necessary to catch e.g. synchronized in Java,
  8503. ;; which can be used both as statement and
  8504. ;; modifier.
  8505. (and (zerop (c-forward-token-2 1 nil))
  8506. (eq (char-after) ?\())
  8507. (looking-at c-opt-block-stmt-key))))
  8508. (if (eq step-type 'up)
  8509. ;; CASE 18A: Simple substatement.
  8510. (progn
  8511. (goto-char placeholder)
  8512. (cond
  8513. ((eq char-after-ip ?{)
  8514. (c-add-stmt-syntax 'substatement-open nil nil
  8515. containing-sexp paren-state))
  8516. ((save-excursion
  8517. (goto-char indent-point)
  8518. (back-to-indentation)
  8519. (c-forward-label))
  8520. (c-add-stmt-syntax 'substatement-label nil nil
  8521. containing-sexp paren-state))
  8522. (t
  8523. (c-add-stmt-syntax 'substatement nil nil
  8524. containing-sexp paren-state))))
  8525. ;; CASE 18B: Some other substatement. This is shared
  8526. ;; with case 10.
  8527. (c-guess-continued-construct indent-point
  8528. char-after-ip
  8529. placeholder
  8530. lim
  8531. paren-state)))
  8532. ;; CASE 14: A case or default label
  8533. ((looking-at c-label-kwds-regexp)
  8534. (if containing-sexp
  8535. (progn
  8536. (goto-char containing-sexp)
  8537. (setq lim (c-most-enclosing-brace c-state-cache
  8538. containing-sexp))
  8539. (c-backward-to-block-anchor lim)
  8540. (c-add-stmt-syntax 'case-label nil t lim paren-state))
  8541. ;; Got a bogus label at the top level. In lack of better
  8542. ;; alternatives, anchor it on (point-min).
  8543. (c-add-syntax 'case-label (point-min))))
  8544. ;; CASE 15: any other label
  8545. ((save-excursion
  8546. (back-to-indentation)
  8547. (and (not (looking-at c-syntactic-ws-start))
  8548. (c-forward-label)))
  8549. (cond (containing-decl-open
  8550. (setq placeholder (c-add-class-syntax 'inclass
  8551. containing-decl-open
  8552. containing-decl-start
  8553. containing-decl-kwd
  8554. paren-state))
  8555. ;; Append access-label with the same anchor point as
  8556. ;; inclass gets.
  8557. (c-append-syntax 'access-label placeholder))
  8558. (containing-sexp
  8559. (goto-char containing-sexp)
  8560. (setq lim (c-most-enclosing-brace c-state-cache
  8561. containing-sexp))
  8562. (save-excursion
  8563. (setq tmpsymbol
  8564. (if (and (eq (c-beginning-of-statement-1 lim) 'up)
  8565. (looking-at "switch\\>[^_]"))
  8566. ;; If the surrounding statement is a switch then
  8567. ;; let's analyze all labels as switch labels, so
  8568. ;; that they get lined up consistently.
  8569. 'case-label
  8570. 'label)))
  8571. (c-backward-to-block-anchor lim)
  8572. (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
  8573. (t
  8574. ;; A label on the top level. Treat it as a class
  8575. ;; context. (point-min) is the closest we get to the
  8576. ;; class open brace.
  8577. (c-add-syntax 'access-label (point-min)))))
  8578. ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
  8579. ;; 17E.
  8580. ((setq placeholder (c-looking-at-inexpr-block
  8581. (c-safe-position containing-sexp paren-state)
  8582. containing-sexp
  8583. ;; Have to turn on the heuristics after
  8584. ;; the point even though it doesn't work
  8585. ;; very well. C.f. test case class-16.pike.
  8586. t))
  8587. (setq tmpsymbol (assq (car placeholder)
  8588. '((inexpr-class . class-open)
  8589. (inexpr-statement . block-open))))
  8590. (if tmpsymbol
  8591. ;; It's a statement block or an anonymous class.
  8592. (setq tmpsymbol (cdr tmpsymbol))
  8593. ;; It's a Pike lambda. Check whether we are between the
  8594. ;; lambda keyword and the argument list or at the defun
  8595. ;; opener.
  8596. (setq tmpsymbol (if (eq char-after-ip ?{)
  8597. 'inline-open
  8598. 'lambda-intro-cont)))
  8599. (goto-char (cdr placeholder))
  8600. (back-to-indentation)
  8601. (c-add-stmt-syntax tmpsymbol nil t
  8602. (c-most-enclosing-brace c-state-cache (point))
  8603. paren-state)
  8604. (unless (eq (point) (cdr placeholder))
  8605. (c-add-syntax (car placeholder))))
  8606. ;; CASE 5: Line is inside a declaration level block or at top level.
  8607. ((or containing-decl-open (null containing-sexp))
  8608. (cond
  8609. ;; CASE 5A: we are looking at a defun, brace list, class,
  8610. ;; or inline-inclass method opening brace
  8611. ((setq special-brace-list
  8612. (or (and c-special-brace-lists
  8613. (c-looking-at-special-brace-list))
  8614. (eq char-after-ip ?{)))
  8615. (cond
  8616. ;; CASE 5A.1: Non-class declaration block open.
  8617. ((save-excursion
  8618. (let (tmp)
  8619. (and (eq char-after-ip ?{)
  8620. (setq tmp (c-looking-at-decl-block containing-sexp t))
  8621. (progn
  8622. (setq placeholder (point))
  8623. (goto-char tmp)
  8624. (looking-at c-symbol-key))
  8625. (c-keyword-member
  8626. (c-keyword-sym (setq keyword (match-string 0)))
  8627. 'c-other-block-decl-kwds))))
  8628. (goto-char placeholder)
  8629. (c-add-stmt-syntax
  8630. (if (string-equal keyword "extern")
  8631. ;; Special case for extern-lang-open.
  8632. 'extern-lang-open
  8633. (intern (concat keyword "-open")))
  8634. nil t containing-sexp paren-state))
  8635. ;; CASE 5A.2: we are looking at a class opening brace
  8636. ((save-excursion
  8637. (goto-char indent-point)
  8638. (skip-chars-forward " \t")
  8639. (and (eq (char-after) ?{)
  8640. (c-looking-at-decl-block containing-sexp t)
  8641. (setq placeholder (point))))
  8642. (c-add-syntax 'class-open placeholder))
  8643. ;; CASE 5A.3: brace list open
  8644. ((save-excursion
  8645. (c-beginning-of-decl-1 lim)
  8646. (while (looking-at c-specifier-key)
  8647. (goto-char (match-end 1))
  8648. (c-forward-syntactic-ws indent-point))
  8649. (setq placeholder (c-point 'boi))
  8650. (or (consp special-brace-list)
  8651. (and (or (save-excursion
  8652. (goto-char indent-point)
  8653. (setq tmpsymbol nil)
  8654. (while (and (> (point) placeholder)
  8655. (zerop (c-backward-token-2 1 t))
  8656. (/= (char-after) ?=))
  8657. (and c-opt-inexpr-brace-list-key
  8658. (not tmpsymbol)
  8659. (looking-at c-opt-inexpr-brace-list-key)
  8660. (setq tmpsymbol 'topmost-intro-cont)))
  8661. (eq (char-after) ?=))
  8662. (looking-at c-brace-list-key))
  8663. (save-excursion
  8664. (while (and (< (point) indent-point)
  8665. (zerop (c-forward-token-2 1 t))
  8666. (not (memq (char-after) '(?\; ?\()))))
  8667. (not (memq (char-after) '(?\; ?\()))
  8668. ))))
  8669. (if (and (not c-auto-newline-analysis)
  8670. (c-major-mode-is 'java-mode)
  8671. (eq tmpsymbol 'topmost-intro-cont))
  8672. ;; We're in Java and have found that the open brace
  8673. ;; belongs to a "new Foo[]" initialization list,
  8674. ;; which means the brace list is part of an
  8675. ;; expression and not a top level definition. We
  8676. ;; therefore treat it as any topmost continuation
  8677. ;; even though the semantically correct symbol still
  8678. ;; is brace-list-open, on the same grounds as in
  8679. ;; case B.2.
  8680. (progn
  8681. (c-beginning-of-statement-1 lim)
  8682. (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  8683. (c-add-syntax 'brace-list-open placeholder)))
  8684. ;; CASE 5A.4: inline defun open
  8685. ((and containing-decl-open
  8686. (not (c-keyword-member containing-decl-kwd
  8687. 'c-other-block-decl-kwds)))
  8688. (c-add-syntax 'inline-open)
  8689. (c-add-class-syntax 'inclass
  8690. containing-decl-open
  8691. containing-decl-start
  8692. containing-decl-kwd
  8693. paren-state))
  8694. ;; CASE 5A.5: ordinary defun open
  8695. (t
  8696. (save-excursion
  8697. (c-beginning-of-decl-1 lim)
  8698. (while (looking-at c-specifier-key)
  8699. (goto-char (match-end 1))
  8700. (c-forward-syntactic-ws indent-point))
  8701. (c-add-syntax 'defun-open (c-point 'boi))
  8702. ;; Bogus to use bol here, but it's the legacy. (Resolved,
  8703. ;; 2007-11-09)
  8704. ))))
  8705. ;; CASE 5B: After a function header but before the body (or
  8706. ;; the ending semicolon if there's no body).
  8707. ((save-excursion
  8708. (when (setq placeholder (c-just-after-func-arglist-p
  8709. (max lim (c-determine-limit 500))))
  8710. (setq tmp-pos (point))))
  8711. (cond
  8712. ;; CASE 5B.1: Member init list.
  8713. ((eq (char-after tmp-pos) ?:)
  8714. (if (or (>= tmp-pos indent-point)
  8715. (= (c-point 'bosws) (1+ tmp-pos)))
  8716. (progn
  8717. ;; There is no preceding member init clause.
  8718. ;; Indent relative to the beginning of indentation
  8719. ;; for the topmost-intro line that contains the
  8720. ;; prototype's open paren.
  8721. (goto-char placeholder)
  8722. (c-add-syntax 'member-init-intro (c-point 'boi)))
  8723. ;; Indent relative to the first member init clause.
  8724. (goto-char (1+ tmp-pos))
  8725. (c-forward-syntactic-ws)
  8726. (c-add-syntax 'member-init-cont (point))))
  8727. ;; CASE 5B.2: K&R arg decl intro
  8728. ((and c-recognize-knr-p
  8729. (c-in-knr-argdecl lim))
  8730. (c-beginning-of-statement-1 lim)
  8731. (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
  8732. (if containing-decl-open
  8733. (c-add-class-syntax 'inclass
  8734. containing-decl-open
  8735. containing-decl-start
  8736. containing-decl-kwd
  8737. paren-state)))
  8738. ;; CASE 5B.4: Nether region after a C++ or Java func
  8739. ;; decl, which could include a `throws' declaration.
  8740. (t
  8741. (c-beginning-of-statement-1 lim)
  8742. (c-add-syntax 'func-decl-cont (c-point 'boi))
  8743. )))
  8744. ;; CASE 5C: inheritance line. could be first inheritance
  8745. ;; line, or continuation of a multiple inheritance
  8746. ((or (and (c-major-mode-is 'c++-mode)
  8747. (progn
  8748. (when (eq char-after-ip ?,)
  8749. (skip-chars-forward " \t")
  8750. (forward-char))
  8751. (looking-at c-opt-postfix-decl-spec-key)))
  8752. (and (or (eq char-before-ip ?:)
  8753. ;; watch out for scope operator
  8754. (save-excursion
  8755. (and (eq char-after-ip ?:)
  8756. (c-safe (forward-char 1) t)
  8757. (not (eq (char-after) ?:))
  8758. )))
  8759. (save-excursion
  8760. (c-backward-syntactic-ws lim)
  8761. (if (eq char-before-ip ?:)
  8762. (progn
  8763. (forward-char -1)
  8764. (c-backward-syntactic-ws lim)))
  8765. (back-to-indentation)
  8766. (looking-at c-class-key)))
  8767. ;; for Java
  8768. (and (c-major-mode-is 'java-mode)
  8769. (let ((fence (save-excursion
  8770. (c-beginning-of-statement-1 lim)
  8771. (point)))
  8772. cont done)
  8773. (save-excursion
  8774. (while (not done)
  8775. (cond ((looking-at c-opt-postfix-decl-spec-key)
  8776. (setq injava-inher (cons cont (point))
  8777. done t))
  8778. ((or (not (c-safe (c-forward-sexp -1) t))
  8779. (<= (point) fence))
  8780. (setq done t))
  8781. )
  8782. (setq cont t)))
  8783. injava-inher)
  8784. (not (c-crosses-statement-barrier-p (cdr injava-inher)
  8785. (point)))
  8786. ))
  8787. (cond
  8788. ;; CASE 5C.1: non-hanging colon on an inher intro
  8789. ((eq char-after-ip ?:)
  8790. (c-beginning-of-statement-1 lim)
  8791. (c-add-syntax 'inher-intro (c-point 'boi))
  8792. ;; don't add inclass symbol since relative point already
  8793. ;; contains any class offset
  8794. )
  8795. ;; CASE 5C.2: hanging colon on an inher intro
  8796. ((eq char-before-ip ?:)
  8797. (c-beginning-of-statement-1 lim)
  8798. (c-add-syntax 'inher-intro (c-point 'boi))
  8799. (if containing-decl-open
  8800. (c-add-class-syntax 'inclass
  8801. containing-decl-open
  8802. containing-decl-start
  8803. containing-decl-kwd
  8804. paren-state)))
  8805. ;; CASE 5C.3: in a Java implements/extends
  8806. (injava-inher
  8807. (let ((where (cdr injava-inher))
  8808. (cont (car injava-inher)))
  8809. (goto-char where)
  8810. (cond ((looking-at "throws\\>[^_]")
  8811. (c-add-syntax 'func-decl-cont
  8812. (progn (c-beginning-of-statement-1 lim)
  8813. (c-point 'boi))))
  8814. (cont (c-add-syntax 'inher-cont where))
  8815. (t (c-add-syntax 'inher-intro
  8816. (progn (goto-char (cdr injava-inher))
  8817. (c-beginning-of-statement-1 lim)
  8818. (point))))
  8819. )))
  8820. ;; CASE 5C.4: a continued inheritance line
  8821. (t
  8822. (c-beginning-of-inheritance-list lim)
  8823. (c-add-syntax 'inher-cont (point))
  8824. ;; don't add inclass symbol since relative point already
  8825. ;; contains any class offset
  8826. )))
  8827. ;; CASE 5D: this could be a top-level initialization, a
  8828. ;; member init list continuation, or a template argument
  8829. ;; list continuation.
  8830. ((save-excursion
  8831. ;; Note: We use the fact that lim is always after any
  8832. ;; preceding brace sexp.
  8833. (if c-recognize-<>-arglists
  8834. (while (and
  8835. (progn
  8836. (c-syntactic-skip-backward "^;,=<>" lim t)
  8837. (> (point) lim))
  8838. (or
  8839. (when c-overloadable-operators-regexp
  8840. (when (setq placeholder (c-after-special-operator-id lim))
  8841. (goto-char placeholder)
  8842. t))
  8843. (cond
  8844. ((eq (char-before) ?>)
  8845. (or (c-backward-<>-arglist nil lim)
  8846. (backward-char))
  8847. t)
  8848. ((eq (char-before) ?<)
  8849. (backward-char)
  8850. (if (save-excursion
  8851. (c-forward-<>-arglist nil))
  8852. (progn (forward-char)
  8853. nil)
  8854. t))
  8855. (t nil)))))
  8856. ;; NB: No c-after-special-operator-id stuff in this
  8857. ;; clause - we assume only C++ needs it.
  8858. (c-syntactic-skip-backward "^;,=" lim t))
  8859. (memq (char-before) '(?, ?= ?<)))
  8860. (cond
  8861. ;; CASE 5D.3: perhaps a template list continuation?
  8862. ((and (c-major-mode-is 'c++-mode)
  8863. (save-excursion
  8864. (save-restriction
  8865. (c-with-syntax-table c++-template-syntax-table
  8866. (goto-char indent-point)
  8867. (setq placeholder (c-up-list-backward))
  8868. (and placeholder
  8869. (eq (char-after placeholder) ?<))))))
  8870. (c-with-syntax-table c++-template-syntax-table
  8871. (goto-char placeholder)
  8872. (c-beginning-of-statement-1 lim t)
  8873. (if (save-excursion
  8874. (c-backward-syntactic-ws lim)
  8875. (eq (char-before) ?<))
  8876. ;; In a nested template arglist.
  8877. (progn
  8878. (goto-char placeholder)
  8879. (c-syntactic-skip-backward "^,;" lim t)
  8880. (c-forward-syntactic-ws))
  8881. (back-to-indentation)))
  8882. ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
  8883. ;; template aware.
  8884. (c-add-syntax 'template-args-cont (point) placeholder))
  8885. ;; CASE 5D.4: perhaps a multiple inheritance line?
  8886. ((and (c-major-mode-is 'c++-mode)
  8887. (save-excursion
  8888. (c-beginning-of-statement-1 lim)
  8889. (setq placeholder (point))
  8890. (if (looking-at "static\\>[^_]")
  8891. (c-forward-token-2 1 nil indent-point))
  8892. (and (looking-at c-class-key)
  8893. (zerop (c-forward-token-2 2 nil indent-point))
  8894. (if (eq (char-after) ?<)
  8895. (c-with-syntax-table c++-template-syntax-table
  8896. (zerop (c-forward-token-2 1 t indent-point)))
  8897. t)
  8898. (eq (char-after) ?:))))
  8899. (goto-char placeholder)
  8900. (c-add-syntax 'inher-cont (c-point 'boi)))
  8901. ;; CASE 5D.5: Continuation of the "expression part" of a
  8902. ;; top level construct. Or, perhaps, an unrecognized construct.
  8903. (t
  8904. (while (and (setq placeholder (point))
  8905. (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
  8906. 'same)
  8907. (save-excursion
  8908. (c-backward-syntactic-ws)
  8909. (eq (char-before) ?}))
  8910. (< (point) placeholder)))
  8911. (c-add-stmt-syntax
  8912. (cond
  8913. ((eq (point) placeholder) 'statement) ; unrecognized construct
  8914. ;; A preceding comma at the top level means that a
  8915. ;; new variable declaration starts here. Use
  8916. ;; topmost-intro-cont for it, for consistency with
  8917. ;; the first variable declaration. C.f. case 5N.
  8918. ((eq char-before-ip ?,) 'topmost-intro-cont)
  8919. (t 'statement-cont))
  8920. nil nil containing-sexp paren-state))
  8921. ))
  8922. ;; CASE 5F: Close of a non-class declaration level block.
  8923. ((and (eq char-after-ip ?})
  8924. (c-keyword-member containing-decl-kwd
  8925. 'c-other-block-decl-kwds))
  8926. ;; This is inconsistent: Should use `containing-decl-open'
  8927. ;; here if it's at boi, like in case 5J.
  8928. (goto-char containing-decl-start)
  8929. (c-add-stmt-syntax
  8930. (if (string-equal (symbol-name containing-decl-kwd) "extern")
  8931. ;; Special case for compatibility with the
  8932. ;; extern-lang syntactic symbols.
  8933. 'extern-lang-close
  8934. (intern (concat (symbol-name containing-decl-kwd)
  8935. "-close")))
  8936. nil t
  8937. (c-most-enclosing-brace paren-state (point))
  8938. paren-state))
  8939. ;; CASE 5G: we are looking at the brace which closes the
  8940. ;; enclosing nested class decl
  8941. ((and containing-sexp
  8942. (eq char-after-ip ?})
  8943. (eq containing-decl-open containing-sexp))
  8944. (c-add-class-syntax 'class-close
  8945. containing-decl-open
  8946. containing-decl-start
  8947. containing-decl-kwd
  8948. paren-state))
  8949. ;; CASE 5H: we could be looking at subsequent knr-argdecls
  8950. ((and c-recognize-knr-p
  8951. (not containing-sexp) ; can't be knr inside braces.
  8952. (not (eq char-before-ip ?}))
  8953. (save-excursion
  8954. (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
  8955. (and placeholder
  8956. ;; Do an extra check to avoid tripping up on
  8957. ;; statements that occur in invalid contexts
  8958. ;; (e.g. in macro bodies where we don't really
  8959. ;; know the context of what we're looking at).
  8960. (not (and c-opt-block-stmt-key
  8961. (looking-at c-opt-block-stmt-key)))))
  8962. (< placeholder indent-point))
  8963. (goto-char placeholder)
  8964. (c-add-syntax 'knr-argdecl (point)))
  8965. ;; CASE 5I: ObjC method definition.
  8966. ((and c-opt-method-key
  8967. (looking-at c-opt-method-key))
  8968. (c-beginning-of-statement-1 nil t)
  8969. (if (= (point) indent-point)
  8970. ;; Handle the case when it's the first (non-comment)
  8971. ;; thing in the buffer. Can't look for a 'same return
  8972. ;; value from cbos1 since ObjC directives currently
  8973. ;; aren't recognized fully, so that we get 'same
  8974. ;; instead of 'previous if it moved over a preceding
  8975. ;; directive.
  8976. (goto-char (point-min)))
  8977. (c-add-syntax 'objc-method-intro (c-point 'boi)))
  8978. ;; CASE 5P: AWK pattern or function or continuation
  8979. ;; thereof.
  8980. ((c-major-mode-is 'awk-mode)
  8981. (setq placeholder (point))
  8982. (c-add-stmt-syntax
  8983. (if (and (eq (c-beginning-of-statement-1) 'same)
  8984. (/= (point) placeholder))
  8985. 'topmost-intro-cont
  8986. 'topmost-intro)
  8987. nil nil
  8988. containing-sexp paren-state))
  8989. ;; CASE 5N: At a variable declaration that follows a class
  8990. ;; definition or some other block declaration that doesn't
  8991. ;; end at the closing '}'. C.f. case 5D.5.
  8992. ((progn
  8993. (c-backward-syntactic-ws lim)
  8994. (and (eq (char-before) ?})
  8995. (save-excursion
  8996. (let ((start (point)))
  8997. (if (and c-state-cache
  8998. (consp (car c-state-cache))
  8999. (eq (cdar c-state-cache) (point)))
  9000. ;; Speed up the backward search a bit.
  9001. (goto-char (caar c-state-cache)))
  9002. (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
  9003. (setq placeholder (point))
  9004. (if (= start (point))
  9005. ;; The '}' is unbalanced.
  9006. nil
  9007. (c-end-of-decl-1)
  9008. (>= (point) indent-point))))))
  9009. (goto-char placeholder)
  9010. (c-add-stmt-syntax 'topmost-intro-cont nil nil
  9011. containing-sexp paren-state))
  9012. ;; NOTE: The point is at the end of the previous token here.
  9013. ;; CASE 5J: we are at the topmost level, make
  9014. ;; sure we skip back past any access specifiers
  9015. ((and
  9016. ;; A macro continuation line is never at top level.
  9017. (not (and macro-start
  9018. (> indent-point macro-start)))
  9019. (save-excursion
  9020. (setq placeholder (point))
  9021. (or (memq char-before-ip '(?\; ?{ ?} nil))
  9022. (c-at-vsemi-p before-ws-ip)
  9023. (when (and (eq char-before-ip ?:)
  9024. (eq (c-beginning-of-statement-1 lim)
  9025. 'label))
  9026. (c-backward-syntactic-ws lim)
  9027. (setq placeholder (point)))
  9028. (and (c-major-mode-is 'objc-mode)
  9029. (catch 'not-in-directive
  9030. (c-beginning-of-statement-1 lim)
  9031. (setq placeholder (point))
  9032. (while (and (c-forward-objc-directive)
  9033. (< (point) indent-point))
  9034. (c-forward-syntactic-ws)
  9035. (if (>= (point) indent-point)
  9036. (throw 'not-in-directive t))
  9037. (setq placeholder (point)))
  9038. nil)))))
  9039. ;; For historic reasons we anchor at bol of the last
  9040. ;; line of the previous declaration. That's clearly
  9041. ;; highly bogus and useless, and it makes our lives hard
  9042. ;; to remain compatible. :P
  9043. (goto-char placeholder)
  9044. (c-add-syntax 'topmost-intro (c-point 'bol))
  9045. (if containing-decl-open
  9046. (if (c-keyword-member containing-decl-kwd
  9047. 'c-other-block-decl-kwds)
  9048. (progn
  9049. (goto-char (c-brace-anchor-point containing-decl-open))
  9050. (c-add-stmt-syntax
  9051. (if (string-equal (symbol-name containing-decl-kwd)
  9052. "extern")
  9053. ;; Special case for compatibility with the
  9054. ;; extern-lang syntactic symbols.
  9055. 'inextern-lang
  9056. (intern (concat "in"
  9057. (symbol-name containing-decl-kwd))))
  9058. nil t
  9059. (c-most-enclosing-brace paren-state (point))
  9060. paren-state))
  9061. (c-add-class-syntax 'inclass
  9062. containing-decl-open
  9063. containing-decl-start
  9064. containing-decl-kwd
  9065. paren-state)))
  9066. (when (and c-syntactic-indentation-in-macros
  9067. macro-start
  9068. (/= macro-start (c-point 'boi indent-point)))
  9069. (c-add-syntax 'cpp-define-intro)
  9070. (setq macro-start nil)))
  9071. ;; CASE 5K: we are at an ObjC method definition
  9072. ;; continuation line.
  9073. ((and c-opt-method-key
  9074. (save-excursion
  9075. (c-beginning-of-statement-1 lim)
  9076. (beginning-of-line)
  9077. (when (looking-at c-opt-method-key)
  9078. (setq placeholder (point)))))
  9079. (c-add-syntax 'objc-method-args-cont placeholder))
  9080. ;; CASE 5L: we are at the first argument of a template
  9081. ;; arglist that begins on the previous line.
  9082. ((and c-recognize-<>-arglists
  9083. (eq (char-before) ?<)
  9084. (not (and c-overloadable-operators-regexp
  9085. (c-after-special-operator-id lim))))
  9086. (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
  9087. (c-add-syntax 'template-args-cont (c-point 'boi)))
  9088. ;; CASE 5Q: we are at a statement within a macro.
  9089. (macro-start
  9090. (c-beginning-of-statement-1 containing-sexp)
  9091. (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
  9092. ;;CASE 5N: We are at a topmost continuation line and the only
  9093. ;;preceding items are annotations.
  9094. ((and (c-major-mode-is 'java-mode)
  9095. (setq placeholder (point))
  9096. (c-beginning-of-statement-1)
  9097. (progn
  9098. (while (and (c-forward-annotation))
  9099. (c-forward-syntactic-ws))
  9100. t)
  9101. (prog1
  9102. (>= (point) placeholder)
  9103. (goto-char placeholder)))
  9104. (c-add-syntax 'annotation-top-cont (c-point 'boi)))
  9105. ;; CASE 5M: we are at a topmost continuation line
  9106. (t
  9107. (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
  9108. (when (c-major-mode-is 'objc-mode)
  9109. (setq placeholder (point))
  9110. (while (and (c-forward-objc-directive)
  9111. (< (point) indent-point))
  9112. (c-forward-syntactic-ws)
  9113. (setq placeholder (point)))
  9114. (goto-char placeholder))
  9115. (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  9116. ))
  9117. ;; (CASE 6 has been removed.)
  9118. ;; CASE 7: line is an expression, not a statement. Most
  9119. ;; likely we are either in a function prototype or a function
  9120. ;; call argument list
  9121. ((not (or (and c-special-brace-lists
  9122. (save-excursion
  9123. (goto-char containing-sexp)
  9124. (c-looking-at-special-brace-list)))
  9125. (eq (char-after containing-sexp) ?{)))
  9126. (cond
  9127. ;; CASE 7A: we are looking at the arglist closing paren.
  9128. ;; C.f. case 7F.
  9129. ((memq char-after-ip '(?\) ?\]))
  9130. (goto-char containing-sexp)
  9131. (setq placeholder (c-point 'boi))
  9132. (if (and (c-safe (backward-up-list 1) t)
  9133. (>= (point) placeholder))
  9134. (progn
  9135. (forward-char)
  9136. (skip-chars-forward " \t"))
  9137. (goto-char placeholder))
  9138. (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
  9139. (c-most-enclosing-brace paren-state (point))
  9140. paren-state))
  9141. ;; CASE 7B: Looking at the opening brace of an
  9142. ;; in-expression block or brace list. C.f. cases 4, 16A
  9143. ;; and 17E.
  9144. ((and (eq char-after-ip ?{)
  9145. (progn
  9146. (setq placeholder (c-inside-bracelist-p (point)
  9147. paren-state))
  9148. (if placeholder
  9149. (setq tmpsymbol '(brace-list-open . inexpr-class))
  9150. (setq tmpsymbol '(block-open . inexpr-statement)
  9151. placeholder
  9152. (cdr-safe (c-looking-at-inexpr-block
  9153. (c-safe-position containing-sexp
  9154. paren-state)
  9155. containing-sexp)))
  9156. ;; placeholder is nil if it's a block directly in
  9157. ;; a function arglist. That makes us skip out of
  9158. ;; this case.
  9159. )))
  9160. (goto-char placeholder)
  9161. (back-to-indentation)
  9162. (c-add-stmt-syntax (car tmpsymbol) nil t
  9163. (c-most-enclosing-brace paren-state (point))
  9164. paren-state)
  9165. (if (/= (point) placeholder)
  9166. (c-add-syntax (cdr tmpsymbol))))
  9167. ;; CASE 7C: we are looking at the first argument in an empty
  9168. ;; argument list. Use arglist-close if we're actually
  9169. ;; looking at a close paren or bracket.
  9170. ((memq char-before-ip '(?\( ?\[))
  9171. (goto-char containing-sexp)
  9172. (setq placeholder (c-point 'boi))
  9173. (if (and (c-safe (backward-up-list 1) t)
  9174. (>= (point) placeholder))
  9175. (progn
  9176. (forward-char)
  9177. (skip-chars-forward " \t"))
  9178. (goto-char placeholder))
  9179. (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
  9180. (c-most-enclosing-brace paren-state (point))
  9181. paren-state))
  9182. ;; CASE 7D: we are inside a conditional test clause. treat
  9183. ;; these things as statements
  9184. ((progn
  9185. (goto-char containing-sexp)
  9186. (and (c-safe (c-forward-sexp -1) t)
  9187. (looking-at "\\<for\\>[^_]")))
  9188. (goto-char (1+ containing-sexp))
  9189. (c-forward-syntactic-ws indent-point)
  9190. (if (eq char-before-ip ?\;)
  9191. (c-add-syntax 'statement (point))
  9192. (c-add-syntax 'statement-cont (point))
  9193. ))
  9194. ;; CASE 7E: maybe a continued ObjC method call. This is the
  9195. ;; case when we are inside a [] bracketed exp, and what
  9196. ;; precede the opening bracket is not an identifier.
  9197. ((and c-opt-method-key
  9198. (eq (char-after containing-sexp) ?\[)
  9199. (progn
  9200. (goto-char (1- containing-sexp))
  9201. (c-backward-syntactic-ws (c-point 'bod))
  9202. (if (not (looking-at c-symbol-key))
  9203. (c-add-syntax 'objc-method-call-cont containing-sexp))
  9204. )))
  9205. ;; CASE 7F: we are looking at an arglist continuation line,
  9206. ;; but the preceding argument is on the same line as the
  9207. ;; opening paren. This case includes multi-line
  9208. ;; mathematical paren groupings, but we could be on a
  9209. ;; for-list continuation line. C.f. case 7A.
  9210. ((progn
  9211. (goto-char (1+ containing-sexp))
  9212. (< (save-excursion
  9213. (c-forward-syntactic-ws)
  9214. (point))
  9215. (c-point 'bonl)))
  9216. (goto-char containing-sexp) ; paren opening the arglist
  9217. (setq placeholder (c-point 'boi))
  9218. (if (and (c-safe (backward-up-list 1) t)
  9219. (>= (point) placeholder))
  9220. (progn
  9221. (forward-char)
  9222. (skip-chars-forward " \t"))
  9223. (goto-char placeholder))
  9224. (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
  9225. (c-most-enclosing-brace c-state-cache (point))
  9226. paren-state))
  9227. ;; CASE 7G: we are looking at just a normal arglist
  9228. ;; continuation line
  9229. (t (c-forward-syntactic-ws indent-point)
  9230. (c-add-syntax 'arglist-cont (c-point 'boi)))
  9231. ))
  9232. ;; CASE 8: func-local multi-inheritance line
  9233. ((and (c-major-mode-is 'c++-mode)
  9234. (save-excursion
  9235. (goto-char indent-point)
  9236. (skip-chars-forward " \t")
  9237. (looking-at c-opt-postfix-decl-spec-key)))
  9238. (goto-char indent-point)
  9239. (skip-chars-forward " \t")
  9240. (cond
  9241. ;; CASE 8A: non-hanging colon on an inher intro
  9242. ((eq char-after-ip ?:)
  9243. (c-backward-syntactic-ws lim)
  9244. (c-add-syntax 'inher-intro (c-point 'boi)))
  9245. ;; CASE 8B: hanging colon on an inher intro
  9246. ((eq char-before-ip ?:)
  9247. (c-add-syntax 'inher-intro (c-point 'boi)))
  9248. ;; CASE 8C: a continued inheritance line
  9249. (t
  9250. (c-beginning-of-inheritance-list lim)
  9251. (c-add-syntax 'inher-cont (point))
  9252. )))
  9253. ;; CASE 9: we are inside a brace-list
  9254. ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
  9255. (setq special-brace-list
  9256. (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
  9257. (save-excursion
  9258. (goto-char containing-sexp)
  9259. (c-looking-at-special-brace-list)))
  9260. (c-inside-bracelist-p containing-sexp paren-state))))
  9261. (cond
  9262. ;; CASE 9A: In the middle of a special brace list opener.
  9263. ((and (consp special-brace-list)
  9264. (save-excursion
  9265. (goto-char containing-sexp)
  9266. (eq (char-after) ?\())
  9267. (eq char-after-ip (car (cdr special-brace-list))))
  9268. (goto-char (car (car special-brace-list)))
  9269. (skip-chars-backward " \t")
  9270. (if (and (bolp)
  9271. (assoc 'statement-cont
  9272. (setq placeholder (c-guess-basic-syntax))))
  9273. (setq c-syntactic-context placeholder)
  9274. (c-beginning-of-statement-1
  9275. (c-safe-position (1- containing-sexp) paren-state))
  9276. (c-forward-token-2 0)
  9277. (while (looking-at c-specifier-key)
  9278. (goto-char (match-end 1))
  9279. (c-forward-syntactic-ws))
  9280. (c-add-syntax 'brace-list-open (c-point 'boi))))
  9281. ;; CASE 9B: brace-list-close brace
  9282. ((if (consp special-brace-list)
  9283. ;; Check special brace list closer.
  9284. (progn
  9285. (goto-char (car (car special-brace-list)))
  9286. (save-excursion
  9287. (goto-char indent-point)
  9288. (back-to-indentation)
  9289. (or
  9290. ;; We were between the special close char and the `)'.
  9291. (and (eq (char-after) ?\))
  9292. (eq (1+ (point)) (cdr (car special-brace-list))))
  9293. ;; We were before the special close char.
  9294. (and (eq (char-after) (cdr (cdr special-brace-list)))
  9295. (zerop (c-forward-token-2))
  9296. (eq (1+ (point)) (cdr (car special-brace-list)))))))
  9297. ;; Normal brace list check.
  9298. (and (eq char-after-ip ?})
  9299. (c-safe (goto-char (c-up-list-backward (point))) t)
  9300. (= (point) containing-sexp)))
  9301. (if (eq (point) (c-point 'boi))
  9302. (c-add-syntax 'brace-list-close (point))
  9303. (setq lim (c-most-enclosing-brace c-state-cache (point)))
  9304. (c-beginning-of-statement-1 lim)
  9305. (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
  9306. (t
  9307. ;; Prepare for the rest of the cases below by going to the
  9308. ;; token following the opening brace
  9309. (if (consp special-brace-list)
  9310. (progn
  9311. (goto-char (car (car special-brace-list)))
  9312. (c-forward-token-2 1 nil indent-point))
  9313. (goto-char containing-sexp))
  9314. (forward-char)
  9315. (let ((start (point)))
  9316. (c-forward-syntactic-ws indent-point)
  9317. (goto-char (max start (c-point 'bol))))
  9318. (c-skip-ws-forward indent-point)
  9319. (cond
  9320. ;; CASE 9C: we're looking at the first line in a brace-list
  9321. ((= (point) indent-point)
  9322. (if (consp special-brace-list)
  9323. (goto-char (car (car special-brace-list)))
  9324. (goto-char containing-sexp))
  9325. (if (eq (point) (c-point 'boi))
  9326. (c-add-syntax 'brace-list-intro (point))
  9327. (setq lim (c-most-enclosing-brace c-state-cache (point)))
  9328. (c-beginning-of-statement-1 lim)
  9329. (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
  9330. ;; CASE 9D: this is just a later brace-list-entry or
  9331. ;; brace-entry-open
  9332. (t (if (or (eq char-after-ip ?{)
  9333. (and c-special-brace-lists
  9334. (save-excursion
  9335. (goto-char indent-point)
  9336. (c-forward-syntactic-ws (c-point 'eol))
  9337. (c-looking-at-special-brace-list (point)))))
  9338. (c-add-syntax 'brace-entry-open (point))
  9339. (c-add-syntax 'brace-list-entry (point))
  9340. ))
  9341. ))))
  9342. ;; CASE 10: A continued statement or top level construct.
  9343. ((and (not (memq char-before-ip '(?\; ?:)))
  9344. (not (c-at-vsemi-p before-ws-ip))
  9345. (or (not (eq char-before-ip ?}))
  9346. (c-looking-at-inexpr-block-backward c-state-cache))
  9347. (> (point)
  9348. (save-excursion
  9349. (c-beginning-of-statement-1 containing-sexp)
  9350. (setq placeholder (point))))
  9351. (/= placeholder containing-sexp))
  9352. ;; This is shared with case 18.
  9353. (c-guess-continued-construct indent-point
  9354. char-after-ip
  9355. placeholder
  9356. containing-sexp
  9357. paren-state))
  9358. ;; CASE 16: block close brace, possibly closing the defun or
  9359. ;; the class
  9360. ((eq char-after-ip ?})
  9361. ;; From here on we have the next containing sexp in lim.
  9362. (setq lim (c-most-enclosing-brace paren-state))
  9363. (goto-char containing-sexp)
  9364. (cond
  9365. ;; CASE 16E: Closing a statement block? This catches
  9366. ;; cases where it's preceded by a statement keyword,
  9367. ;; which works even when used in an "invalid" context,
  9368. ;; e.g. a macro argument.
  9369. ((c-after-conditional)
  9370. (c-backward-to-block-anchor lim)
  9371. (c-add-stmt-syntax 'block-close nil t lim paren-state))
  9372. ;; CASE 16A: closing a lambda defun or an in-expression
  9373. ;; block? C.f. cases 4, 7B and 17E.
  9374. ((setq placeholder (c-looking-at-inexpr-block
  9375. (c-safe-position containing-sexp paren-state)
  9376. nil))
  9377. (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
  9378. 'inline-close
  9379. 'block-close))
  9380. (goto-char containing-sexp)
  9381. (back-to-indentation)
  9382. (if (= containing-sexp (point))
  9383. (c-add-syntax tmpsymbol (point))
  9384. (goto-char (cdr placeholder))
  9385. (back-to-indentation)
  9386. (c-add-stmt-syntax tmpsymbol nil t
  9387. (c-most-enclosing-brace paren-state (point))
  9388. paren-state)
  9389. (if (/= (point) (cdr placeholder))
  9390. (c-add-syntax (car placeholder)))))
  9391. ;; CASE 16B: does this close an inline or a function in
  9392. ;; a non-class declaration level block?
  9393. ((save-excursion
  9394. (and lim
  9395. (progn
  9396. (goto-char lim)
  9397. (c-looking-at-decl-block
  9398. (c-most-enclosing-brace paren-state lim)
  9399. nil))
  9400. (setq placeholder (point))))
  9401. (c-backward-to-decl-anchor lim)
  9402. (back-to-indentation)
  9403. (if (save-excursion
  9404. (goto-char placeholder)
  9405. (looking-at c-other-decl-block-key))
  9406. (c-add-syntax 'defun-close (point))
  9407. (c-add-syntax 'inline-close (point))))
  9408. ;; CASE 16F: Can be a defun-close of a function declared
  9409. ;; in a statement block, e.g. in Pike or when using gcc
  9410. ;; extensions, but watch out for macros followed by
  9411. ;; blocks. Let it through to be handled below.
  9412. ;; C.f. cases B.3 and 17G.
  9413. ((save-excursion
  9414. (and (not (c-at-statement-start-p))
  9415. (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
  9416. (setq placeholder (point))
  9417. (let ((c-recognize-typeless-decls nil))
  9418. ;; Turn off recognition of constructs that
  9419. ;; lacks a type in this case, since that's more
  9420. ;; likely to be a macro followed by a block.
  9421. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  9422. (back-to-indentation)
  9423. (if (/= (point) containing-sexp)
  9424. (goto-char placeholder))
  9425. (c-add-stmt-syntax 'defun-close nil t lim paren-state))
  9426. ;; CASE 16C: If there is an enclosing brace then this is
  9427. ;; a block close since defun closes inside declaration
  9428. ;; level blocks have been handled above.
  9429. (lim
  9430. ;; If the block is preceded by a case/switch label on
  9431. ;; the same line, we anchor at the first preceding label
  9432. ;; at boi. The default handling in c-add-stmt-syntax
  9433. ;; really fixes it better, but we do like this to keep
  9434. ;; the indentation compatible with version 5.28 and
  9435. ;; earlier. C.f. case 17H.
  9436. (while (and (/= (setq placeholder (point)) (c-point 'boi))
  9437. (eq (c-beginning-of-statement-1 lim) 'label)))
  9438. (goto-char placeholder)
  9439. (if (looking-at c-label-kwds-regexp)
  9440. (c-add-syntax 'block-close (point))
  9441. (goto-char containing-sexp)
  9442. ;; c-backward-to-block-anchor not necessary here; those
  9443. ;; situations are handled in case 16E above.
  9444. (c-add-stmt-syntax 'block-close nil t lim paren-state)))
  9445. ;; CASE 16D: Only top level defun close left.
  9446. (t
  9447. (goto-char containing-sexp)
  9448. (c-backward-to-decl-anchor lim)
  9449. (c-add-stmt-syntax 'defun-close nil nil
  9450. (c-most-enclosing-brace paren-state)
  9451. paren-state))
  9452. ))
  9453. ;; CASE 19: line is an expression, not a statement, and is directly
  9454. ;; contained by a template delimiter. Most likely, we are in a
  9455. ;; template arglist within a statement. This case is based on CASE
  9456. ;; 7. At some point in the future, we may wish to create more
  9457. ;; syntactic symbols such as `template-intro',
  9458. ;; `template-cont-nonempty', etc., and distinguish between them as we
  9459. ;; do for `arglist-intro' etc. (2009-12-07).
  9460. ((and c-recognize-<>-arglists
  9461. (setq containing-< (c-up-list-backward indent-point containing-sexp))
  9462. (eq (char-after containing-<) ?\<))
  9463. (setq placeholder (c-point 'boi containing-<))
  9464. (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
  9465. ; '<') before indent-point.
  9466. (if (>= (point) placeholder)
  9467. (progn
  9468. (forward-char)
  9469. (skip-chars-forward " \t"))
  9470. (goto-char placeholder))
  9471. (c-add-stmt-syntax 'template-args-cont (list containing-<) t
  9472. (c-most-enclosing-brace c-state-cache (point))
  9473. paren-state))
  9474. ;; CASE 17: Statement or defun catchall.
  9475. (t
  9476. (goto-char indent-point)
  9477. ;; Back up statements until we find one that starts at boi.
  9478. (while (let* ((prev-point (point))
  9479. (last-step-type (c-beginning-of-statement-1
  9480. containing-sexp)))
  9481. (if (= (point) prev-point)
  9482. (progn
  9483. (setq step-type (or step-type last-step-type))
  9484. nil)
  9485. (setq step-type last-step-type)
  9486. (/= (point) (c-point 'boi)))))
  9487. (cond
  9488. ;; CASE 17B: continued statement
  9489. ((and (eq step-type 'same)
  9490. (/= (point) indent-point))
  9491. (c-add-stmt-syntax 'statement-cont nil nil
  9492. containing-sexp paren-state))
  9493. ;; CASE 17A: After a case/default label?
  9494. ((progn
  9495. (while (and (eq step-type 'label)
  9496. (not (looking-at c-label-kwds-regexp)))
  9497. (setq step-type
  9498. (c-beginning-of-statement-1 containing-sexp)))
  9499. (eq step-type 'label))
  9500. (c-add-stmt-syntax (if (eq char-after-ip ?{)
  9501. 'statement-case-open
  9502. 'statement-case-intro)
  9503. nil t containing-sexp paren-state))
  9504. ;; CASE 17D: any old statement
  9505. ((progn
  9506. (while (eq step-type 'label)
  9507. (setq step-type
  9508. (c-beginning-of-statement-1 containing-sexp)))
  9509. (eq step-type 'previous))
  9510. (c-add-stmt-syntax 'statement nil t
  9511. containing-sexp paren-state)
  9512. (if (eq char-after-ip ?{)
  9513. (c-add-syntax 'block-open)))
  9514. ;; CASE 17I: Inside a substatement block.
  9515. ((progn
  9516. ;; The following tests are all based on containing-sexp.
  9517. (goto-char containing-sexp)
  9518. ;; From here on we have the next containing sexp in lim.
  9519. (setq lim (c-most-enclosing-brace paren-state containing-sexp))
  9520. (c-after-conditional))
  9521. (c-backward-to-block-anchor lim)
  9522. (c-add-stmt-syntax 'statement-block-intro nil t
  9523. lim paren-state)
  9524. (if (eq char-after-ip ?{)
  9525. (c-add-syntax 'block-open)))
  9526. ;; CASE 17E: first statement in an in-expression block.
  9527. ;; C.f. cases 4, 7B and 16A.
  9528. ((setq placeholder (c-looking-at-inexpr-block
  9529. (c-safe-position containing-sexp paren-state)
  9530. nil))
  9531. (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
  9532. 'defun-block-intro
  9533. 'statement-block-intro))
  9534. (back-to-indentation)
  9535. (if (= containing-sexp (point))
  9536. (c-add-syntax tmpsymbol (point))
  9537. (goto-char (cdr placeholder))
  9538. (back-to-indentation)
  9539. (c-add-stmt-syntax tmpsymbol nil t
  9540. (c-most-enclosing-brace c-state-cache (point))
  9541. paren-state)
  9542. (if (/= (point) (cdr placeholder))
  9543. (c-add-syntax (car placeholder))))
  9544. (if (eq char-after-ip ?{)
  9545. (c-add-syntax 'block-open)))
  9546. ;; CASE 17F: first statement in an inline, or first
  9547. ;; statement in a top-level defun. we can tell this is it
  9548. ;; if there are no enclosing braces that haven't been
  9549. ;; narrowed out by a class (i.e. don't use bod here).
  9550. ((save-excursion
  9551. (or (not (setq placeholder (c-most-enclosing-brace
  9552. paren-state)))
  9553. (and (progn
  9554. (goto-char placeholder)
  9555. (eq (char-after) ?{))
  9556. (c-looking-at-decl-block (c-most-enclosing-brace
  9557. paren-state (point))
  9558. nil))))
  9559. (c-backward-to-decl-anchor lim)
  9560. (back-to-indentation)
  9561. (c-add-syntax 'defun-block-intro (point)))
  9562. ;; CASE 17G: First statement in a function declared inside
  9563. ;; a normal block. This can occur in Pike and with
  9564. ;; e.g. the gcc extensions, but watch out for macros
  9565. ;; followed by blocks. C.f. cases B.3 and 16F.
  9566. ((save-excursion
  9567. (and (not (c-at-statement-start-p))
  9568. (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
  9569. (setq placeholder (point))
  9570. (let ((c-recognize-typeless-decls nil))
  9571. ;; Turn off recognition of constructs that lacks
  9572. ;; a type in this case, since that's more likely
  9573. ;; to be a macro followed by a block.
  9574. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  9575. (back-to-indentation)
  9576. (if (/= (point) containing-sexp)
  9577. (goto-char placeholder))
  9578. (c-add-stmt-syntax 'defun-block-intro nil t
  9579. lim paren-state))
  9580. ;; CASE 17H: First statement in a block.
  9581. (t
  9582. ;; If the block is preceded by a case/switch label on the
  9583. ;; same line, we anchor at the first preceding label at
  9584. ;; boi. The default handling in c-add-stmt-syntax is
  9585. ;; really fixes it better, but we do like this to keep the
  9586. ;; indentation compatible with version 5.28 and earlier.
  9587. ;; C.f. case 16C.
  9588. (while (and (/= (setq placeholder (point)) (c-point 'boi))
  9589. (eq (c-beginning-of-statement-1 lim) 'label)))
  9590. (goto-char placeholder)
  9591. (if (looking-at c-label-kwds-regexp)
  9592. (c-add-syntax 'statement-block-intro (point))
  9593. (goto-char containing-sexp)
  9594. ;; c-backward-to-block-anchor not necessary here; those
  9595. ;; situations are handled in case 17I above.
  9596. (c-add-stmt-syntax 'statement-block-intro nil t
  9597. lim paren-state))
  9598. (if (eq char-after-ip ?{)
  9599. (c-add-syntax 'block-open)))
  9600. ))
  9601. )
  9602. ;; now we need to look at any modifiers
  9603. (goto-char indent-point)
  9604. (skip-chars-forward " \t")
  9605. ;; are we looking at a comment only line?
  9606. (when (and (looking-at c-comment-start-regexp)
  9607. (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
  9608. (c-append-syntax 'comment-intro))
  9609. ;; we might want to give additional offset to friends (in C++).
  9610. (when (and c-opt-friend-key
  9611. (looking-at c-opt-friend-key))
  9612. (c-append-syntax 'friend))
  9613. ;; Set syntactic-relpos.
  9614. (let ((p c-syntactic-context))
  9615. (while (and p
  9616. (if (integerp (c-langelem-pos (car p)))
  9617. (progn
  9618. (setq syntactic-relpos (c-langelem-pos (car p)))
  9619. nil)
  9620. t))
  9621. (setq p (cdr p))))
  9622. ;; Start of or a continuation of a preprocessor directive?
  9623. (if (and macro-start
  9624. (eq macro-start (c-point 'boi))
  9625. (not (and (c-major-mode-is 'pike-mode)
  9626. (eq (char-after (1+ macro-start)) ?\"))))
  9627. (c-append-syntax 'cpp-macro)
  9628. (when (and c-syntactic-indentation-in-macros macro-start)
  9629. (if in-macro-expr
  9630. (when (or
  9631. (< syntactic-relpos macro-start)
  9632. (not (or
  9633. (assq 'arglist-intro c-syntactic-context)
  9634. (assq 'arglist-cont c-syntactic-context)
  9635. (assq 'arglist-cont-nonempty c-syntactic-context)
  9636. (assq 'arglist-close c-syntactic-context))))
  9637. ;; If inside a cpp expression, i.e. anywhere in a
  9638. ;; cpp directive except a #define body, we only let
  9639. ;; through the syntactic analysis that is internal
  9640. ;; in the expression. That means the arglist
  9641. ;; elements, if they are anchored inside the cpp
  9642. ;; expression.
  9643. (setq c-syntactic-context nil)
  9644. (c-add-syntax 'cpp-macro-cont macro-start))
  9645. (when (and (eq macro-start syntactic-relpos)
  9646. (not (assq 'cpp-define-intro c-syntactic-context))
  9647. (save-excursion
  9648. (goto-char macro-start)
  9649. (or (not (c-forward-to-cpp-define-body))
  9650. (<= (point) (c-point 'boi indent-point)))))
  9651. ;; Inside a #define body and the syntactic analysis is
  9652. ;; anchored on the start of the #define. In this case
  9653. ;; we add cpp-define-intro to get the extra
  9654. ;; indentation of the #define body.
  9655. (c-add-syntax 'cpp-define-intro)))))
  9656. ;; return the syntax
  9657. c-syntactic-context)))
  9658. ;; Indentation calculation.
  9659. (defun c-evaluate-offset (offset langelem symbol)
  9660. ;; offset can be a number, a function, a variable, a list, or one of
  9661. ;; the symbols + or -
  9662. ;;
  9663. ;; This function might do hidden buffer changes.
  9664. (let ((res
  9665. (cond
  9666. ((numberp offset) offset)
  9667. ((vectorp offset) offset)
  9668. ((null offset) nil)
  9669. ((eq offset '+) c-basic-offset)
  9670. ((eq offset '-) (- c-basic-offset))
  9671. ((eq offset '++) (* 2 c-basic-offset))
  9672. ((eq offset '--) (* 2 (- c-basic-offset)))
  9673. ((eq offset '*) (/ c-basic-offset 2))
  9674. ((eq offset '/) (/ (- c-basic-offset) 2))
  9675. ((functionp offset)
  9676. (c-evaluate-offset
  9677. (funcall offset
  9678. (cons (c-langelem-sym langelem)
  9679. (c-langelem-pos langelem)))
  9680. langelem symbol))
  9681. ((listp offset)
  9682. (cond
  9683. ((eq (car offset) 'quote)
  9684. (c-benign-error "The offset %S for %s was mistakenly quoted"
  9685. offset symbol)
  9686. nil)
  9687. ((memq (car offset) '(min max))
  9688. (let (res val (method (car offset)))
  9689. (setq offset (cdr offset))
  9690. (while offset
  9691. (setq val (c-evaluate-offset (car offset) langelem symbol))
  9692. (cond
  9693. ((not val))
  9694. ((not res)
  9695. (setq res val))
  9696. ((integerp val)
  9697. (if (vectorp res)
  9698. (c-benign-error "\
  9699. Error evaluating offset %S for %s: \
  9700. Cannot combine absolute offset %S with relative %S in `%s' method"
  9701. (car offset) symbol res val method)
  9702. (setq res (funcall method res val))))
  9703. (t
  9704. (if (integerp res)
  9705. (c-benign-error "\
  9706. Error evaluating offset %S for %s: \
  9707. Cannot combine relative offset %S with absolute %S in `%s' method"
  9708. (car offset) symbol res val method)
  9709. (setq res (vector (funcall method (aref res 0)
  9710. (aref val 0)))))))
  9711. (setq offset (cdr offset)))
  9712. res))
  9713. ((eq (car offset) 'add)
  9714. (let (res val)
  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. (setq res (vector (+ (aref res 0) val)))
  9725. (setq res (+ res val))))
  9726. (t
  9727. (if (vectorp res)
  9728. (c-benign-error "\
  9729. Error evaluating offset %S for %s: \
  9730. Cannot combine absolute offsets %S and %S in `add' method"
  9731. (car offset) symbol res val)
  9732. (setq res val)))) ; Override.
  9733. (setq offset (cdr offset)))
  9734. res))
  9735. (t
  9736. (let (res)
  9737. (when (eq (car offset) 'first)
  9738. (setq offset (cdr offset)))
  9739. (while (and (not res) offset)
  9740. (setq res (c-evaluate-offset (car offset) langelem symbol)
  9741. offset (cdr offset)))
  9742. res))))
  9743. ((and (symbolp offset) (boundp offset))
  9744. (symbol-value offset))
  9745. (t
  9746. (c-benign-error "Unknown offset format %S for %s" offset symbol)
  9747. nil))))
  9748. (if (or (null res) (integerp res)
  9749. (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
  9750. res
  9751. (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
  9752. offset symbol res)
  9753. nil)))
  9754. (defun c-calc-offset (langelem)
  9755. ;; Get offset from LANGELEM which is a list beginning with the
  9756. ;; syntactic symbol and followed by any analysis data it provides.
  9757. ;; That data may be zero or more elements, but if at least one is
  9758. ;; given then the first is the anchor position (or nil). The symbol
  9759. ;; is matched against `c-offsets-alist' and the offset calculated
  9760. ;; from that is returned.
  9761. ;;
  9762. ;; This function might do hidden buffer changes.
  9763. (let* ((symbol (c-langelem-sym langelem))
  9764. (match (assq symbol c-offsets-alist))
  9765. (offset (cdr-safe match)))
  9766. (if match
  9767. (setq offset (c-evaluate-offset offset langelem symbol))
  9768. (if c-strict-syntax-p
  9769. (c-benign-error "No offset found for syntactic symbol %s" symbol))
  9770. (setq offset 0))
  9771. (if (vectorp offset)
  9772. offset
  9773. (or (and (numberp offset) offset)
  9774. (and (symbolp offset) (symbol-value offset))
  9775. 0))
  9776. ))
  9777. (defun c-get-offset (langelem)
  9778. ;; This is a compatibility wrapper for `c-calc-offset' in case
  9779. ;; someone is calling it directly. It takes an old style syntactic
  9780. ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
  9781. ;; new list form.
  9782. ;;
  9783. ;; This function might do hidden buffer changes.
  9784. (if (c-langelem-pos langelem)
  9785. (c-calc-offset (list (c-langelem-sym langelem)
  9786. (c-langelem-pos langelem)))
  9787. (c-calc-offset langelem)))
  9788. (defun c-get-syntactic-indentation (langelems)
  9789. ;; Calculate the syntactic indentation from a syntactic description
  9790. ;; as returned by `c-guess-syntax'.
  9791. ;;
  9792. ;; Note that topmost-intro always has an anchor position at bol, for
  9793. ;; historical reasons. It's often used together with other symbols
  9794. ;; that has more sane positions. Since we always use the first
  9795. ;; found anchor position, we rely on that these other symbols always
  9796. ;; precede topmost-intro in the LANGELEMS list.
  9797. ;;
  9798. ;; This function might do hidden buffer changes.
  9799. (let ((indent 0) anchor)
  9800. (while langelems
  9801. (let* ((c-syntactic-element (car langelems))
  9802. (res (c-calc-offset c-syntactic-element)))
  9803. (if (vectorp res)
  9804. ;; Got an absolute column that overrides any indentation
  9805. ;; we've collected so far, but not the relative
  9806. ;; indentation we might get for the nested structures
  9807. ;; further down the langelems list.
  9808. (setq indent (elt res 0)
  9809. anchor (point-min)) ; A position at column 0.
  9810. ;; Got a relative change of the current calculated
  9811. ;; indentation.
  9812. (setq indent (+ indent res))
  9813. ;; Use the anchor position from the first syntactic
  9814. ;; element with one.
  9815. (unless anchor
  9816. (setq anchor (c-langelem-pos (car langelems)))))
  9817. (setq langelems (cdr langelems))))
  9818. (if anchor
  9819. (+ indent (save-excursion
  9820. (goto-char anchor)
  9821. (current-column)))
  9822. indent)))
  9823. (cc-provide 'cc-engine)
  9824. ;;; cc-engine.el ends here