cc-engine.el 407 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240
  1. ;;; cc-engine.el --- core syntax guessing engine for CC mode -*- coding: utf-8 -*-
  2. ;; Copyright (C) 1985, 1987, 1992-2015 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. (eval-when-compile (require 'cl))
  137. ;; Make declarations for all the `c-lang-defvar' variables in cc-langs.
  138. (defmacro c-declare-lang-variables ()
  139. `(progn
  140. ,@(c--mapcan (lambda (init)
  141. `(,(if (elt init 2)
  142. `(defvar ,(car init) nil ,(elt init 2))
  143. `(defvar ,(car init) nil))
  144. (make-variable-buffer-local ',(car init))))
  145. (cdr c-lang-variable-inits))))
  146. (c-declare-lang-variables)
  147. ;;; Internal state variables.
  148. ;; Internal state of hungry delete key feature
  149. (defvar c-hungry-delete-key nil)
  150. (make-variable-buffer-local 'c-hungry-delete-key)
  151. ;; The electric flag (toggled by `c-toggle-electric-state').
  152. ;; If t, electric actions (like automatic reindentation, and (if
  153. ;; c-auto-newline is also set) auto newlining) will happen when an electric
  154. ;; key like `{' is pressed (or an electric keyword like `else').
  155. (defvar c-electric-flag t)
  156. (make-variable-buffer-local 'c-electric-flag)
  157. ;; Internal state of auto newline feature.
  158. (defvar c-auto-newline nil)
  159. (make-variable-buffer-local 'c-auto-newline)
  160. ;; Included in the mode line to indicate the active submodes.
  161. ;; (defvar c-submode-indicators nil)
  162. ;; (make-variable-buffer-local 'c-submode-indicators)
  163. (defun c-calculate-state (arg prevstate)
  164. ;; Calculate the new state of PREVSTATE, t or nil, based on arg. If
  165. ;; arg is nil or zero, toggle the state. If arg is negative, turn
  166. ;; the state off, and if arg is positive, turn the state on
  167. (if (or (not arg)
  168. (zerop (setq arg (prefix-numeric-value arg))))
  169. (not prevstate)
  170. (> arg 0)))
  171. ;; Basic handling of preprocessor directives.
  172. ;; This is a dynamically bound cache used together with
  173. ;; `c-query-macro-start' and `c-query-and-set-macro-start'. It only
  174. ;; works as long as point doesn't cross a macro boundary.
  175. (defvar c-macro-start 'unknown)
  176. (defsubst c-query-and-set-macro-start ()
  177. (if (symbolp c-macro-start)
  178. (setq c-macro-start (save-excursion
  179. (c-save-buffer-state ()
  180. (and (c-beginning-of-macro)
  181. (point)))))
  182. c-macro-start))
  183. (defsubst c-query-macro-start ()
  184. (if (symbolp c-macro-start)
  185. (save-excursion
  186. (c-save-buffer-state ()
  187. (and (c-beginning-of-macro)
  188. (point))))
  189. c-macro-start))
  190. ;; One element macro cache to cope with continual movement within very large
  191. ;; CPP macros.
  192. (defvar c-macro-cache nil)
  193. (make-variable-buffer-local 'c-macro-cache)
  194. ;; Nil or cons of the bounds of the most recent CPP form probed by
  195. ;; `c-beginning-of-macro', `c-end-of-macro' or `c-syntactic-end-of-macro'.
  196. ;; The cdr will be nil if we know only the start of the CPP form.
  197. (defvar c-macro-cache-start-pos nil)
  198. (make-variable-buffer-local 'c-macro-cache-start-pos)
  199. ;; The starting position from where we determined `c-macro-cache'.
  200. (defvar c-macro-cache-syntactic nil)
  201. (make-variable-buffer-local 'c-macro-cache-syntactic)
  202. ;; non-nil iff `c-macro-cache' has both elements set AND the cdr is at a
  203. ;; syntactic end of macro, not merely an apparent one.
  204. (defun c-invalidate-macro-cache (beg end)
  205. ;; Called from a before-change function. If the change region is before or
  206. ;; in the macro characterized by `c-macro-cache' etc., nullify it
  207. ;; appropriately. BEG and END are the standard before-change-functions
  208. ;; parameters. END isn't used.
  209. (cond
  210. ((null c-macro-cache))
  211. ((< beg (car c-macro-cache))
  212. (setq c-macro-cache nil
  213. c-macro-cache-start-pos nil
  214. c-macro-cache-syntactic nil))
  215. ((and (cdr c-macro-cache)
  216. (< beg (cdr c-macro-cache)))
  217. (setcdr c-macro-cache nil)
  218. (setq c-macro-cache-start-pos beg
  219. c-macro-cache-syntactic nil))))
  220. (defun c-macro-is-genuine-p ()
  221. ;; Check that the ostensible CPP construct at point is a real one. In
  222. ;; particular, if point is on the first line of a narrowed buffer, make sure
  223. ;; that the "#" isn't, say, the second character of a "##" operator. Return
  224. ;; t when the macro is real, nil otherwise.
  225. (let ((here (point)))
  226. (beginning-of-line)
  227. (prog1
  228. (if (and (eq (point) (point-min))
  229. (/= (point) 1))
  230. (save-restriction
  231. (widen)
  232. (beginning-of-line)
  233. (and (looking-at c-anchored-cpp-prefix)
  234. (eq (match-beginning 1) here)))
  235. t)
  236. (goto-char here))))
  237. (defun c-beginning-of-macro (&optional lim)
  238. "Go to the beginning of a preprocessor directive.
  239. Leave point at the beginning of the directive and return t if in one,
  240. otherwise return nil and leave point unchanged.
  241. Note that this function might do hidden buffer changes. See the
  242. comment at the start of cc-engine.el for more info."
  243. (let ((here (point)))
  244. (when c-opt-cpp-prefix
  245. (if (and (car c-macro-cache)
  246. (>= (point) (car c-macro-cache))
  247. (or (and (cdr c-macro-cache)
  248. (<= (point) (cdr c-macro-cache)))
  249. (<= (point) c-macro-cache-start-pos)))
  250. (unless (< (car c-macro-cache) (or lim (point-min)))
  251. (progn (goto-char (max (or lim (point-min)) (car c-macro-cache)))
  252. (setq c-macro-cache-start-pos
  253. (max c-macro-cache-start-pos here))
  254. t))
  255. (setq c-macro-cache nil
  256. c-macro-cache-start-pos nil
  257. c-macro-cache-syntactic nil)
  258. (save-restriction
  259. (if lim (narrow-to-region lim (point-max)))
  260. (beginning-of-line)
  261. (while (eq (char-before (1- (point))) ?\\)
  262. (forward-line -1))
  263. (back-to-indentation)
  264. (if (and (<= (point) here)
  265. (looking-at c-opt-cpp-start)
  266. (c-macro-is-genuine-p))
  267. (progn
  268. (setq c-macro-cache (cons (point) nil)
  269. c-macro-cache-start-pos here)
  270. t)
  271. (goto-char here)
  272. nil))))))
  273. (defun c-end-of-macro ()
  274. "Go to the end of a preprocessor directive.
  275. More accurately, move the point to the end of the closest following
  276. line that doesn't end with a line continuation backslash - no check is
  277. done that the point is inside a cpp directive to begin with.
  278. Note that this function might do hidden buffer changes. See the
  279. comment at the start of cc-engine.el for more info."
  280. (if (and (cdr c-macro-cache)
  281. (<= (point) (cdr c-macro-cache))
  282. (>= (point) (car c-macro-cache)))
  283. (goto-char (cdr c-macro-cache))
  284. (unless (and (car c-macro-cache)
  285. (<= (point) c-macro-cache-start-pos)
  286. (>= (point) (car c-macro-cache)))
  287. (setq c-macro-cache nil
  288. c-macro-cache-start-pos nil
  289. c-macro-cache-syntactic nil))
  290. (while (progn
  291. (end-of-line)
  292. (when (and (eq (char-before) ?\\)
  293. (not (eobp)))
  294. (forward-char)
  295. t)))
  296. (when (car c-macro-cache)
  297. (setcdr c-macro-cache (point)))))
  298. (defun c-syntactic-end-of-macro ()
  299. ;; Go to the end of a CPP directive, or a "safe" pos just before.
  300. ;;
  301. ;; This is normally the end of the next non-escaped line. A "safe"
  302. ;; position is one not within a string or comment. (The EOL on a line
  303. ;; comment is NOT "safe").
  304. ;;
  305. ;; This function must only be called from the beginning of a CPP construct.
  306. ;;
  307. ;; Note that this function might do hidden buffer changes. See the comment
  308. ;; at the start of cc-engine.el for more info.
  309. (let* ((here (point))
  310. (there (progn (c-end-of-macro) (point)))
  311. s)
  312. (unless c-macro-cache-syntactic
  313. (setq s (parse-partial-sexp here there))
  314. (while (and (or (nth 3 s) ; in a string
  315. (nth 4 s)) ; in a comment (maybe at end of line comment)
  316. (> there here)) ; No infinite loops, please.
  317. (setq there (1- (nth 8 s)))
  318. (setq s (parse-partial-sexp here there)))
  319. (setq c-macro-cache-syntactic (car c-macro-cache)))
  320. (point)))
  321. (defun c-forward-over-cpp-define-id ()
  322. ;; Assuming point is at the "#" that introduces a preprocessor
  323. ;; directive, it's moved forward to the end of the identifier which is
  324. ;; "#define"d (or whatever c-opt-cpp-macro-define specifies). Non-nil
  325. ;; is returned in this case, in all other cases nil is returned and
  326. ;; point isn't moved.
  327. ;;
  328. ;; This function might do hidden buffer changes.
  329. (when (and c-opt-cpp-macro-define-id
  330. (looking-at c-opt-cpp-macro-define-id))
  331. (goto-char (match-end 0))))
  332. (defun c-forward-to-cpp-define-body ()
  333. ;; Assuming point is at the "#" that introduces a preprocessor
  334. ;; directive, it's moved forward to the start of the definition body
  335. ;; if it's a "#define" (or whatever c-opt-cpp-macro-define
  336. ;; specifies). Non-nil is returned in this case, in all other cases
  337. ;; nil is returned and point isn't moved.
  338. ;;
  339. ;; This function might do hidden buffer changes.
  340. (when (and c-opt-cpp-macro-define-start
  341. (looking-at c-opt-cpp-macro-define-start)
  342. (not (= (match-end 0) (c-point 'eol))))
  343. (goto-char (match-end 0))))
  344. ;;; Basic utility functions.
  345. (defun c-syntactic-content (from to paren-level)
  346. ;; Return the given region as a string where all syntactic
  347. ;; whitespace is removed or, where necessary, replaced with a single
  348. ;; space. If PAREN-LEVEL is given then all parens in the region are
  349. ;; collapsed to "()", "[]" etc.
  350. ;;
  351. ;; This function might do hidden buffer changes.
  352. (save-excursion
  353. (save-restriction
  354. (narrow-to-region from to)
  355. (goto-char from)
  356. (let* ((parts (list nil)) (tail parts) pos in-paren)
  357. (while (re-search-forward c-syntactic-ws-start to t)
  358. (goto-char (setq pos (match-beginning 0)))
  359. (c-forward-syntactic-ws)
  360. (if (= (point) pos)
  361. (forward-char)
  362. (when paren-level
  363. (save-excursion
  364. (setq in-paren (= (car (parse-partial-sexp from pos 1)) 1)
  365. pos (point))))
  366. (if (and (> pos from)
  367. (< (point) to)
  368. (looking-at "\\w\\|\\s_")
  369. (save-excursion
  370. (goto-char (1- pos))
  371. (looking-at "\\w\\|\\s_")))
  372. (progn
  373. (setcdr tail (list (buffer-substring-no-properties from pos)
  374. " "))
  375. (setq tail (cddr tail)))
  376. (setcdr tail (list (buffer-substring-no-properties from pos)))
  377. (setq tail (cdr tail)))
  378. (when in-paren
  379. (when (= (car (parse-partial-sexp pos to -1)) -1)
  380. (setcdr tail (list (buffer-substring-no-properties
  381. (1- (point)) (point))))
  382. (setq tail (cdr tail))))
  383. (setq from (point))))
  384. (setcdr tail (list (buffer-substring-no-properties from to)))
  385. (apply 'concat (cdr parts))))))
  386. (defun c-shift-line-indentation (shift-amt)
  387. ;; Shift the indentation of the current line with the specified
  388. ;; amount (positive inwards). The buffer is modified only if
  389. ;; SHIFT-AMT isn't equal to zero.
  390. (let ((pos (- (point-max) (point)))
  391. (c-macro-start c-macro-start)
  392. tmp-char-inserted)
  393. (if (zerop shift-amt)
  394. nil
  395. ;; If we're on an empty line inside a macro, we take the point
  396. ;; to be at the current indentation and shift it to the
  397. ;; appropriate column. This way we don't treat the extra
  398. ;; whitespace out to the line continuation as indentation.
  399. (when (and (c-query-and-set-macro-start)
  400. (looking-at "[ \t]*\\\\$")
  401. (save-excursion
  402. (skip-chars-backward " \t")
  403. (bolp)))
  404. (insert ?x)
  405. (backward-char)
  406. (setq tmp-char-inserted t))
  407. (unwind-protect
  408. (let ((col (current-indentation)))
  409. (delete-region (c-point 'bol) (c-point 'boi))
  410. (beginning-of-line)
  411. (indent-to (+ col shift-amt)))
  412. (when tmp-char-inserted
  413. (delete-char 1))))
  414. ;; If initial point was within line's indentation and we're not on
  415. ;; a line with a line continuation in a macro, position after the
  416. ;; indentation. Else stay at same point in text.
  417. (if (and (< (point) (c-point 'boi))
  418. (not tmp-char-inserted))
  419. (back-to-indentation)
  420. (if (> (- (point-max) pos) (point))
  421. (goto-char (- (point-max) pos))))))
  422. (defsubst c-keyword-sym (keyword)
  423. ;; Return non-nil if the string KEYWORD is a known keyword. More
  424. ;; precisely, the value is the symbol for the keyword in
  425. ;; `c-keywords-obarray'.
  426. (intern-soft keyword c-keywords-obarray))
  427. (defsubst c-keyword-member (keyword-sym lang-constant)
  428. ;; Return non-nil if the symbol KEYWORD-SYM, as returned by
  429. ;; `c-keyword-sym', is a member of LANG-CONSTANT, which is the name
  430. ;; of a language constant that ends with "-kwds". If KEYWORD-SYM is
  431. ;; nil then the result is nil.
  432. (get keyword-sym lang-constant))
  433. ;; String syntax chars, suitable for skip-syntax-(forward|backward).
  434. (defconst c-string-syntax (if (memq 'gen-string-delim c-emacs-features)
  435. "\"|"
  436. "\""))
  437. ;; Regexp matching string limit syntax.
  438. (defconst c-string-limit-regexp (if (memq 'gen-string-delim c-emacs-features)
  439. "\\s\"\\|\\s|"
  440. "\\s\""))
  441. ;; Regexp matching WS followed by string limit syntax.
  442. (defconst c-ws*-string-limit-regexp
  443. (concat "[ \t]*\\(" c-string-limit-regexp "\\)"))
  444. ;; Holds formatted error strings for the few cases where parse errors
  445. ;; are reported.
  446. (defvar c-parsing-error nil)
  447. (make-variable-buffer-local 'c-parsing-error)
  448. (defun c-echo-parsing-error (&optional quiet)
  449. (when (and c-report-syntactic-errors c-parsing-error (not quiet))
  450. (c-benign-error "%s" c-parsing-error))
  451. c-parsing-error)
  452. ;; Faces given to comments and string literals. This is used in some
  453. ;; situations to speed up recognition; it isn't mandatory that font
  454. ;; locking is in use. This variable is extended with the face in
  455. ;; `c-doc-face-name' when fontification is activated in cc-fonts.el.
  456. (defvar c-literal-faces
  457. (append '(font-lock-comment-face font-lock-string-face)
  458. (when (facep 'font-lock-comment-delimiter-face)
  459. ;; New in Emacs 22.
  460. '(font-lock-comment-delimiter-face))))
  461. (defsubst c-put-c-type-property (pos value)
  462. ;; Put a c-type property with the given value at POS.
  463. (c-put-char-property pos 'c-type value))
  464. (defun c-clear-c-type-property (from to value)
  465. ;; Remove all occurrences of the c-type property that has the given
  466. ;; value in the region between FROM and TO. VALUE is assumed to not
  467. ;; be nil.
  468. ;;
  469. ;; Note: This assumes that c-type is put on single chars only; it's
  470. ;; very inefficient if matching properties cover large regions.
  471. (save-excursion
  472. (goto-char from)
  473. (while (progn
  474. (when (eq (get-text-property (point) 'c-type) value)
  475. (c-clear-char-property (point) 'c-type))
  476. (goto-char (c-next-single-property-change (point) 'c-type nil to))
  477. (< (point) to)))))
  478. ;; Some debug tools to visualize various special positions. This
  479. ;; debug code isn't as portable as the rest of CC Mode.
  480. (cc-bytecomp-defun overlays-in)
  481. (cc-bytecomp-defun overlay-get)
  482. (cc-bytecomp-defun overlay-start)
  483. (cc-bytecomp-defun overlay-end)
  484. (cc-bytecomp-defun delete-overlay)
  485. (cc-bytecomp-defun overlay-put)
  486. (cc-bytecomp-defun make-overlay)
  487. (defun c-debug-add-face (beg end face)
  488. (c-save-buffer-state ((overlays (overlays-in beg end)) overlay)
  489. (while overlays
  490. (setq overlay (car overlays)
  491. overlays (cdr overlays))
  492. (when (eq (overlay-get overlay 'face) face)
  493. (setq beg (min beg (overlay-start overlay))
  494. end (max end (overlay-end overlay)))
  495. (delete-overlay overlay)))
  496. (overlay-put (make-overlay beg end) 'face face)))
  497. (defun c-debug-remove-face (beg end face)
  498. (c-save-buffer-state ((overlays (overlays-in beg end)) overlay
  499. (ol-beg beg) (ol-end end))
  500. (while overlays
  501. (setq overlay (car overlays)
  502. overlays (cdr overlays))
  503. (when (eq (overlay-get overlay 'face) face)
  504. (setq ol-beg (min ol-beg (overlay-start overlay))
  505. ol-end (max ol-end (overlay-end overlay)))
  506. (delete-overlay overlay)))
  507. (when (< ol-beg beg)
  508. (overlay-put (make-overlay ol-beg beg) 'face face))
  509. (when (> ol-end end)
  510. (overlay-put (make-overlay end ol-end) 'face face))))
  511. ;; `c-beginning-of-statement-1' and accompanying stuff.
  512. ;; KLUDGE ALERT: c-maybe-labelp is used to pass information between
  513. ;; c-crosses-statement-barrier-p and c-beginning-of-statement-1. A
  514. ;; better way should be implemented, but this will at least shut up
  515. ;; the byte compiler.
  516. (defvar c-maybe-labelp)
  517. ;; New awk-compatible version of c-beginning-of-statement-1, ACM 2002/6/22
  518. ;; Macros used internally in c-beginning-of-statement-1 for the
  519. ;; automaton actions.
  520. (defmacro c-bos-push-state ()
  521. '(setq stack (cons (cons state saved-pos)
  522. stack)))
  523. (defmacro c-bos-pop-state (&optional do-if-done)
  524. `(if (setq state (car (car stack))
  525. saved-pos (cdr (car stack))
  526. stack (cdr stack))
  527. t
  528. ,do-if-done
  529. (throw 'loop nil)))
  530. (defmacro c-bos-pop-state-and-retry ()
  531. '(throw 'loop (setq state (car (car stack))
  532. saved-pos (cdr (car stack))
  533. ;; Throw nil if stack is empty, else throw non-nil.
  534. stack (cdr stack))))
  535. (defmacro c-bos-save-pos ()
  536. '(setq saved-pos (vector pos tok ptok pptok)))
  537. (defmacro c-bos-restore-pos ()
  538. '(unless (eq (elt saved-pos 0) start)
  539. (setq pos (elt saved-pos 0)
  540. tok (elt saved-pos 1)
  541. ptok (elt saved-pos 2)
  542. pptok (elt saved-pos 3))
  543. (goto-char pos)
  544. (setq sym nil)))
  545. (defmacro c-bos-save-error-info (missing got)
  546. `(setq saved-pos (vector pos ,missing ,got)))
  547. (defmacro c-bos-report-error ()
  548. '(unless noerror
  549. (setq c-parsing-error
  550. (format "No matching `%s' found for `%s' on line %d"
  551. (elt saved-pos 1)
  552. (elt saved-pos 2)
  553. (1+ (count-lines (point-min)
  554. (c-point 'bol (elt saved-pos 0))))))))
  555. (defun c-beginning-of-statement-1 (&optional lim ignore-labels
  556. noerror comma-delim)
  557. "Move to the start of the current statement or declaration, or to
  558. the previous one if already at the beginning of one. Only
  559. statements/declarations on the same level are considered, i.e. don't
  560. move into or out of sexps (not even normal expression parentheses).
  561. If point is already at the earliest statement within braces or parens,
  562. this function doesn't move back into any whitespace preceding it; it
  563. returns 'same in this case.
  564. Stop at statement continuation tokens like \"else\", \"catch\",
  565. \"finally\" and the \"while\" in \"do ... while\" if the start point
  566. is within the continuation. If starting at such a token, move to the
  567. corresponding statement start. If at the beginning of a statement,
  568. move to the closest containing statement if there is any. This might
  569. also stop at a continuation clause.
  570. Labels are treated as part of the following statements if
  571. IGNORE-LABELS is non-nil. (FIXME: Doesn't work if we stop at a known
  572. statement start keyword.) Otherwise, each label is treated as a
  573. separate statement.
  574. Macros are ignored \(i.e. skipped over) unless point is within one, in
  575. which case the content of the macro is treated as normal code. Aside
  576. from any normal statement starts found in it, stop at the first token
  577. of the content in the macro, i.e. the expression of an \"#if\" or the
  578. start of the definition in a \"#define\". Also stop at start of
  579. macros before leaving them.
  580. Return:
  581. 'label if stopped at a label or \"case...:\" or \"default:\";
  582. 'same if stopped at the beginning of the current statement;
  583. 'up if stepped to a containing statement;
  584. 'previous if stepped to a preceding statement;
  585. 'beginning if stepped from a statement continuation clause to
  586. its start clause; or
  587. 'macro if stepped to a macro start.
  588. Note that 'same and not 'label is returned if stopped at the same
  589. label without crossing the colon character.
  590. LIM may be given to limit the search. If the search hits the limit,
  591. point will be left at the closest following token, or at the start
  592. position if that is less ('same is returned in this case).
  593. NOERROR turns off error logging to `c-parsing-error'.
  594. Normally only `;' and virtual semicolons are considered to delimit
  595. statements, but if COMMA-DELIM is non-nil then `,' is treated
  596. as a delimiter too.
  597. Note that this function might do hidden buffer changes. See the
  598. comment at the start of cc-engine.el for more info."
  599. ;; The bulk of this function is a pushdown automaton that looks at statement
  600. ;; boundaries and the tokens (such as "while") in c-opt-block-stmt-key. Its
  601. ;; purpose is to keep track of nested statements, ensuring that such
  602. ;; statements are skipped over in their entirety (somewhat akin to what C-M-p
  603. ;; does with nested braces/brackets/parentheses).
  604. ;;
  605. ;; Note: The position of a boundary is the following token.
  606. ;;
  607. ;; Beginning with the current token (the one following point), move back one
  608. ;; sexp at a time (where a sexp is, more or less, either a token or the
  609. ;; entire contents of a brace/bracket/paren pair). Each time a statement
  610. ;; boundary is crossed or a "while"-like token is found, update the state of
  611. ;; the PDA. Stop at the beginning of a statement when the stack (holding
  612. ;; nested statement info) is empty and the position has been moved.
  613. ;;
  614. ;; The following variables constitute the PDA:
  615. ;;
  616. ;; sym: This is either the "while"-like token (e.g. 'for) we've just
  617. ;; scanned back over, 'boundary if we've just gone back over a
  618. ;; statement boundary, or nil otherwise.
  619. ;; state: takes one of the values (nil else else-boundary while
  620. ;; while-boundary catch catch-boundary).
  621. ;; nil means "no "while"-like token yet scanned".
  622. ;; 'else, for example, means "just gone back over an else".
  623. ;; 'else-boundary means "just gone back over a statement boundary
  624. ;; immediately after having gone back over an else".
  625. ;; saved-pos: A vector of either saved positions (tok ptok pptok, etc.) or
  626. ;; of error reporting information.
  627. ;; stack: The stack onto which the PDA pushes its state. Each entry
  628. ;; consists of a saved value of state and saved-pos. An entry is
  629. ;; pushed when we move back over a "continuation" token (e.g. else)
  630. ;; and popped when we encounter the corresponding opening token
  631. ;; (e.g. if).
  632. ;;
  633. ;;
  634. ;; The following diagram briefly outlines the PDA.
  635. ;;
  636. ;; Common state:
  637. ;; "else": Push state, goto state `else'.
  638. ;; "while": Push state, goto state `while'.
  639. ;; "catch" or "finally": Push state, goto state `catch'.
  640. ;; boundary: Pop state.
  641. ;; other: Do nothing special.
  642. ;;
  643. ;; State `else':
  644. ;; boundary: Goto state `else-boundary'.
  645. ;; other: Error, pop state, retry token.
  646. ;;
  647. ;; State `else-boundary':
  648. ;; "if": Pop state.
  649. ;; boundary: Error, pop state.
  650. ;; other: See common state.
  651. ;;
  652. ;; State `while':
  653. ;; boundary: Save position, goto state `while-boundary'.
  654. ;; other: Pop state, retry token.
  655. ;;
  656. ;; State `while-boundary':
  657. ;; "do": Pop state.
  658. ;; boundary: Restore position if it's not at start, pop state. [*see below]
  659. ;; other: See common state.
  660. ;;
  661. ;; State `catch':
  662. ;; boundary: Goto state `catch-boundary'.
  663. ;; other: Error, pop state, retry token.
  664. ;;
  665. ;; State `catch-boundary':
  666. ;; "try": Pop state.
  667. ;; "catch": Goto state `catch'.
  668. ;; boundary: Error, pop state.
  669. ;; other: See common state.
  670. ;;
  671. ;; [*] In the `while-boundary' state, we had pushed a 'while state, and were
  672. ;; searching for a "do" which would have opened a do-while. If we didn't
  673. ;; find it, we discard the analysis done since the "while", go back to this
  674. ;; token in the buffer and restart the scanning there, this time WITHOUT
  675. ;; pushing the 'while state onto the stack.
  676. ;;
  677. ;; In addition to the above there is some special handling of labels
  678. ;; and macros.
  679. (let ((case-fold-search nil)
  680. (start (point))
  681. macro-start
  682. (delims (if comma-delim '(?\; ?,) '(?\;)))
  683. (c-stmt-delim-chars (if comma-delim
  684. c-stmt-delim-chars-with-comma
  685. c-stmt-delim-chars))
  686. c-in-literal-cache c-maybe-labelp after-case:-pos saved
  687. ;; Current position.
  688. pos
  689. ;; Position of last stmt boundary character (e.g. ;).
  690. boundary-pos
  691. ;; The position of the last sexp or bound that follows the
  692. ;; first found colon, i.e. the start of the nonlabel part of
  693. ;; the statement. It's `start' if a colon is found just after
  694. ;; the start.
  695. after-labels-pos
  696. ;; Like `after-labels-pos', but the first such position inside
  697. ;; a label, i.e. the start of the last label before the start
  698. ;; of the nonlabel part of the statement.
  699. last-label-pos
  700. ;; The last position where a label is possible provided the
  701. ;; statement started there. It's nil as long as no invalid
  702. ;; label content has been found (according to
  703. ;; `c-nonlabel-token-key'). It's `start' if no valid label
  704. ;; content was found in the label. Note that we might still
  705. ;; regard it a label if it starts with `c-label-kwds'.
  706. label-good-pos
  707. ;; Putative positions of the components of a bitfield declaration,
  708. ;; e.g. "int foo : NUM_FOO_BITS ;"
  709. bitfield-type-pos bitfield-id-pos bitfield-size-pos
  710. ;; Symbol just scanned back over (e.g. 'while or 'boundary).
  711. ;; See above.
  712. sym
  713. ;; Current state in the automaton. See above.
  714. state
  715. ;; Current saved positions. See above.
  716. saved-pos
  717. ;; Stack of conses (state . saved-pos).
  718. stack
  719. ;; Regexp which matches "for", "if", etc.
  720. (cond-key (or c-opt-block-stmt-key
  721. "\\<\\>")) ; Matches nothing.
  722. ;; Return value.
  723. (ret 'same)
  724. ;; Positions of the last three sexps or bounds we've stopped at.
  725. tok ptok pptok)
  726. (save-restriction
  727. (if lim (narrow-to-region lim (point-max)))
  728. (if (save-excursion
  729. (and (c-beginning-of-macro)
  730. (/= (point) start)))
  731. (setq macro-start (point)))
  732. ;; Try to skip back over unary operator characters, to register
  733. ;; that we've moved.
  734. (while (progn
  735. (setq pos (point))
  736. (c-backward-syntactic-ws)
  737. ;; Protect post-++/-- operators just before a virtual semicolon.
  738. (and (not (c-at-vsemi-p))
  739. (/= (skip-chars-backward "-+!*&~@`#") 0))))
  740. ;; Skip back over any semicolon here. If it was a bare semicolon, we're
  741. ;; done. Later on we ignore the boundaries for statements that don't
  742. ;; contain any sexp. The only thing that is affected is that the error
  743. ;; checking is a little less strict, and we really don't bother.
  744. (if (and (memq (char-before) delims)
  745. (progn (forward-char -1)
  746. (setq saved (point))
  747. (c-backward-syntactic-ws)
  748. (or (memq (char-before) delims)
  749. (memq (char-before) '(?: nil))
  750. (eq (char-syntax (char-before)) ?\()
  751. (c-at-vsemi-p))))
  752. (setq ret 'previous
  753. pos saved)
  754. ;; Begin at start and not pos to detect macros if we stand
  755. ;; directly after the #.
  756. (goto-char start)
  757. (if (looking-at "\\<\\|\\W")
  758. ;; Record this as the first token if not starting inside it.
  759. (setq tok start))
  760. ;; The following while loop goes back one sexp (balanced parens,
  761. ;; etc. with contents, or symbol or suchlike) each iteration. This
  762. ;; movement is accomplished with a call to c-backward-sexp approx 170
  763. ;; lines below.
  764. ;;
  765. ;; The loop is exited only by throwing nil to the (catch 'loop ...):
  766. ;; 1. On reaching the start of a macro;
  767. ;; 2. On having passed a stmt boundary with the PDA stack empty;
  768. ;; 3. On reaching the start of an Objective C method def;
  769. ;; 4. From macro `c-bos-pop-state'; when the stack is empty;
  770. ;; 5. From macro `c-bos-pop-state-and-retry' when the stack is empty.
  771. (while
  772. (catch 'loop ;; Throw nil to break, non-nil to continue.
  773. (cond
  774. ;; Are we in a macro, just after the opening #?
  775. ((save-excursion
  776. (and macro-start ; Always NIL for AWK.
  777. (progn (skip-chars-backward " \t")
  778. (eq (char-before) ?#))
  779. (progn (setq saved (1- (point)))
  780. (beginning-of-line)
  781. (not (eq (char-before (1- (point))) ?\\)))
  782. (looking-at c-opt-cpp-start)
  783. (progn (skip-chars-forward " \t")
  784. (eq (point) saved))))
  785. (goto-char saved)
  786. (if (and (c-forward-to-cpp-define-body)
  787. (progn (c-forward-syntactic-ws start)
  788. (< (point) start)))
  789. ;; Stop at the first token in the content of the macro.
  790. (setq pos (point)
  791. ignore-labels t) ; Avoid the label check on exit.
  792. (setq pos saved
  793. ret 'macro
  794. ignore-labels t))
  795. (throw 'loop nil)) ; 1. Start of macro.
  796. ;; Do a round through the automaton if we've just passed a
  797. ;; statement boundary or passed a "while"-like token.
  798. ((or sym
  799. (and (looking-at cond-key)
  800. (setq sym (intern (match-string 1)))))
  801. (when (and (< pos start) (null stack))
  802. (throw 'loop nil)) ; 2. Statement boundary.
  803. ;; The PDA state handling.
  804. ;;
  805. ;; Refer to the description of the PDA in the opening
  806. ;; comments. In the following OR form, the first leaf
  807. ;; attempts to handles one of the specific actions detailed
  808. ;; (e.g., finding token "if" whilst in state `else-boundary').
  809. ;; We drop through to the second leaf (which handles common
  810. ;; state) if no specific handler is found in the first cond.
  811. ;; If a parsing error is detected (e.g. an "else" with no
  812. ;; preceding "if"), we throw to the enclosing catch.
  813. ;;
  814. ;; Note that the (eq state 'else) means
  815. ;; "we've just passed an else", NOT "we're looking for an
  816. ;; else".
  817. (or (cond
  818. ((eq state 'else)
  819. (if (eq sym 'boundary)
  820. (setq state 'else-boundary)
  821. (c-bos-report-error)
  822. (c-bos-pop-state-and-retry)))
  823. ((eq state 'else-boundary)
  824. (cond ((eq sym 'if)
  825. (c-bos-pop-state (setq ret 'beginning)))
  826. ((eq sym 'boundary)
  827. (c-bos-report-error)
  828. (c-bos-pop-state))))
  829. ((eq state 'while)
  830. (if (and (eq sym 'boundary)
  831. ;; Since this can cause backtracking we do a
  832. ;; little more careful analysis to avoid it:
  833. ;; If there's a label in front of the while
  834. ;; it can't be part of a do-while.
  835. (not after-labels-pos))
  836. (progn (c-bos-save-pos)
  837. (setq state 'while-boundary))
  838. (c-bos-pop-state-and-retry))) ; Can't be a do-while
  839. ((eq state 'while-boundary)
  840. (cond ((eq sym 'do)
  841. (c-bos-pop-state (setq ret 'beginning)))
  842. ((eq sym 'boundary) ; isn't a do-while
  843. (c-bos-restore-pos) ; the position of the while
  844. (c-bos-pop-state)))) ; no longer searching for do.
  845. ((eq state 'catch)
  846. (if (eq sym 'boundary)
  847. (setq state 'catch-boundary)
  848. (c-bos-report-error)
  849. (c-bos-pop-state-and-retry)))
  850. ((eq state 'catch-boundary)
  851. (cond
  852. ((eq sym 'try)
  853. (c-bos-pop-state (setq ret 'beginning)))
  854. ((eq sym 'catch)
  855. (setq state 'catch))
  856. ((eq sym 'boundary)
  857. (c-bos-report-error)
  858. (c-bos-pop-state)))))
  859. ;; This is state common. We get here when the previous
  860. ;; cond statement found no particular state handler.
  861. (cond ((eq sym 'boundary)
  862. ;; If we have a boundary at the start
  863. ;; position we push a frame to go to the
  864. ;; previous statement.
  865. (if (>= pos start)
  866. (c-bos-push-state)
  867. (c-bos-pop-state)))
  868. ((eq sym 'else)
  869. (c-bos-push-state)
  870. (c-bos-save-error-info 'if 'else)
  871. (setq state 'else))
  872. ((eq sym 'while)
  873. ;; Is this a real while, or a do-while?
  874. ;; The next `when' triggers unless we are SURE that
  875. ;; the `while' is not the tail end of a `do-while'.
  876. (when (or (not pptok)
  877. (memq (char-after pptok) delims)
  878. ;; The following kludge is to prevent
  879. ;; infinite recursion when called from
  880. ;; c-awk-after-if-for-while-condition-p,
  881. ;; or the like.
  882. (and (eq (point) start)
  883. (c-vsemi-status-unknown-p))
  884. (c-at-vsemi-p pptok))
  885. ;; Since this can cause backtracking we do a
  886. ;; little more careful analysis to avoid it: If
  887. ;; the while isn't followed by a (possibly
  888. ;; virtual) semicolon it can't be a do-while.
  889. (c-bos-push-state)
  890. (setq state 'while)))
  891. ((memq sym '(catch finally))
  892. (c-bos-push-state)
  893. (c-bos-save-error-info 'try sym)
  894. (setq state 'catch))))
  895. (when c-maybe-labelp
  896. ;; We're either past a statement boundary or at the
  897. ;; start of a statement, so throw away any label data
  898. ;; for the previous one.
  899. (setq after-labels-pos nil
  900. last-label-pos nil
  901. c-maybe-labelp nil))))
  902. ;; Step to the previous sexp, but not if we crossed a
  903. ;; boundary, since that doesn't consume an sexp.
  904. (if (eq sym 'boundary)
  905. (setq ret 'previous)
  906. ;; HERE IS THE SINGLE PLACE INSIDE THE PDA LOOP WHERE WE MOVE
  907. ;; BACKWARDS THROUGH THE SOURCE.
  908. (c-backward-syntactic-ws)
  909. (let ((before-sws-pos (point))
  910. ;; The end position of the area to search for statement
  911. ;; barriers in this round.
  912. (maybe-after-boundary-pos pos))
  913. ;; Go back over exactly one logical sexp, taking proper
  914. ;; account of macros and escaped EOLs.
  915. (while
  916. (progn
  917. (unless (c-safe (c-backward-sexp) t)
  918. ;; Give up if we hit an unbalanced block. Since the
  919. ;; stack won't be empty the code below will report a
  920. ;; suitable error.
  921. (throw 'loop nil))
  922. (cond
  923. ;; Have we moved into a macro?
  924. ((and (not macro-start)
  925. (c-beginning-of-macro))
  926. ;; Have we crossed a statement boundary? If not,
  927. ;; keep going back until we find one or a "real" sexp.
  928. (and
  929. (save-excursion
  930. (c-end-of-macro)
  931. (not (c-crosses-statement-barrier-p
  932. (point) maybe-after-boundary-pos)))
  933. (setq maybe-after-boundary-pos (point))))
  934. ;; Have we just gone back over an escaped NL? This
  935. ;; doesn't count as a sexp.
  936. ((looking-at "\\\\$")))))
  937. ;; Have we crossed a statement boundary?
  938. (setq boundary-pos
  939. (cond
  940. ;; Are we at a macro beginning?
  941. ((and (not macro-start)
  942. c-opt-cpp-prefix
  943. (looking-at c-opt-cpp-prefix))
  944. (save-excursion
  945. (c-end-of-macro)
  946. (c-crosses-statement-barrier-p
  947. (point) maybe-after-boundary-pos)))
  948. ;; Just gone back over a brace block?
  949. ((and
  950. (eq (char-after) ?{)
  951. (not (c-looking-at-inexpr-block lim nil t))
  952. (save-excursion
  953. (c-backward-token-2 1 t nil)
  954. (not (looking-at "=\\([^=]\\|$\\)"))))
  955. (save-excursion
  956. (c-forward-sexp) (point)))
  957. ;; Just gone back over some paren block?
  958. ((looking-at "\\s\(")
  959. (save-excursion
  960. (goto-char (1+ (c-down-list-backward
  961. before-sws-pos)))
  962. (c-crosses-statement-barrier-p
  963. (point) maybe-after-boundary-pos)))
  964. ;; Just gone back over an ordinary symbol of some sort?
  965. (t (c-crosses-statement-barrier-p
  966. (point) maybe-after-boundary-pos))))
  967. (when boundary-pos
  968. (setq pptok ptok
  969. ptok tok
  970. tok boundary-pos
  971. sym 'boundary)
  972. ;; Like a C "continue". Analyze the next sexp.
  973. (throw 'loop t))))
  974. ;; ObjC method def?
  975. (when (and c-opt-method-key
  976. (setq saved (c-in-method-def-p)))
  977. (setq pos saved
  978. ignore-labels t) ; Avoid the label check on exit.
  979. (throw 'loop nil)) ; 3. ObjC method def.
  980. ;; Might we have a bitfield declaration, "<type> <id> : <size>"?
  981. (if c-has-bitfields
  982. (cond
  983. ;; The : <size> and <id> fields?
  984. ((and (numberp c-maybe-labelp)
  985. (not bitfield-size-pos)
  986. (save-excursion
  987. (goto-char (or tok start))
  988. (not (looking-at c-keywords-regexp)))
  989. (not (looking-at c-keywords-regexp))
  990. (not (c-punctuation-in (point) c-maybe-labelp)))
  991. (setq bitfield-size-pos (or tok start)
  992. bitfield-id-pos (point)))
  993. ;; The <type> field?
  994. ((and bitfield-id-pos
  995. (not bitfield-type-pos))
  996. (if (and (looking-at c-symbol-key) ; Can only be an integer type. :-)
  997. (not (looking-at c-not-primitive-type-keywords-regexp))
  998. (not (c-punctuation-in (point) tok)))
  999. (setq bitfield-type-pos (point))
  1000. (setq bitfield-size-pos nil
  1001. bitfield-id-pos nil)))))
  1002. ;; Handle labels.
  1003. (unless (eq ignore-labels t)
  1004. (when (numberp c-maybe-labelp)
  1005. ;; `c-crosses-statement-barrier-p' has found a colon, so we
  1006. ;; might be in a label now. Have we got a real label
  1007. ;; (including a case label) or something like C++'s "public:"?
  1008. ;; A case label might use an expression rather than a token.
  1009. (setq after-case:-pos (or tok start))
  1010. (if (or (looking-at c-nonlabel-token-key) ; e.g. "while" or "'a'"
  1011. ;; Catch C++'s inheritance construct "class foo : bar".
  1012. (save-excursion
  1013. (and
  1014. (c-safe (c-backward-sexp) t)
  1015. (looking-at c-nonlabel-token-2-key))))
  1016. (setq c-maybe-labelp nil)
  1017. (if after-labels-pos ; Have we already encountered a label?
  1018. (if (not last-label-pos)
  1019. (setq last-label-pos (or tok start)))
  1020. (setq after-labels-pos (or tok start)))
  1021. (setq c-maybe-labelp t
  1022. label-good-pos nil))) ; bogus "label"
  1023. (when (and (not label-good-pos) ; i.e. no invalid "label"'s yet
  1024. ; been found.
  1025. (looking-at c-nonlabel-token-key)) ; e.g. "while :"
  1026. ;; We're in a potential label and it's the first
  1027. ;; time we've found something that isn't allowed in
  1028. ;; one.
  1029. (setq label-good-pos (or tok start))))
  1030. ;; We've moved back by a sexp, so update the token positions.
  1031. (setq sym nil
  1032. pptok ptok
  1033. ptok tok
  1034. tok (point)
  1035. pos tok) ; always non-nil
  1036. ) ; end of (catch loop ....)
  1037. ) ; end of sexp-at-a-time (while ....)
  1038. ;; If the stack isn't empty there might be errors to report.
  1039. (while stack
  1040. (if (and (vectorp saved-pos) (eq (length saved-pos) 3))
  1041. (c-bos-report-error))
  1042. (setq saved-pos (cdr (car stack))
  1043. stack (cdr stack)))
  1044. (when (and (eq ret 'same)
  1045. (not (memq sym '(boundary ignore nil))))
  1046. ;; Need to investigate closer whether we've crossed
  1047. ;; between a substatement and its containing statement.
  1048. (if (setq saved
  1049. (cond ((and (looking-at c-block-stmt-1-2-key)
  1050. (eq (char-after ptok) ?\())
  1051. pptok)
  1052. ((looking-at c-block-stmt-1-key)
  1053. ptok)
  1054. (t pptok)))
  1055. (cond ((> start saved) (setq pos saved))
  1056. ((= start saved) (setq ret 'up)))))
  1057. (when (and (not ignore-labels)
  1058. (eq c-maybe-labelp t)
  1059. (not (eq ret 'beginning))
  1060. after-labels-pos
  1061. (not bitfield-type-pos) ; Bitfields take precedence over labels.
  1062. (or (not label-good-pos)
  1063. (<= label-good-pos pos)
  1064. (progn
  1065. (goto-char (if (and last-label-pos
  1066. (< last-label-pos start))
  1067. last-label-pos
  1068. pos))
  1069. (looking-at c-label-kwds-regexp))))
  1070. ;; We're in a label. Maybe we should step to the statement
  1071. ;; after it.
  1072. (if (< after-labels-pos start)
  1073. (setq pos after-labels-pos)
  1074. (setq ret 'label)
  1075. (if (and last-label-pos (< last-label-pos start))
  1076. ;; Might have jumped over several labels. Go to the last one.
  1077. (setq pos last-label-pos)))))
  1078. ;; Have we got "case <expression>:"?
  1079. (goto-char pos)
  1080. (when (and after-case:-pos
  1081. (not (eq ret 'beginning))
  1082. (looking-at c-case-kwds-regexp))
  1083. (if (< after-case:-pos start)
  1084. (setq pos after-case:-pos))
  1085. (if (eq ret 'same)
  1086. (setq ret 'label)))
  1087. ;; Skip over the unary operators that can start the statement.
  1088. (while (progn
  1089. (c-backward-syntactic-ws)
  1090. ;; protect AWK post-inc/decrement operators, etc.
  1091. (and (not (c-at-vsemi-p (point)))
  1092. (/= (skip-chars-backward "-+!*&~@`#") 0)))
  1093. (setq pos (point)))
  1094. (goto-char pos)
  1095. ret)))
  1096. (defun c-punctuation-in (from to)
  1097. "Return non-nil if there is a non-comment non-macro punctuation character
  1098. between FROM and TO. FROM must not be in a string or comment. The returned
  1099. value is the position of the first such character."
  1100. (save-excursion
  1101. (goto-char from)
  1102. (let ((pos (point)))
  1103. (while (progn (skip-chars-forward c-symbol-chars to)
  1104. (c-forward-syntactic-ws to)
  1105. (> (point) pos))
  1106. (setq pos (point))))
  1107. (and (< (point) to) (point))))
  1108. (defun c-crosses-statement-barrier-p (from to)
  1109. "Return non-nil if buffer positions FROM to TO cross one or more
  1110. statement or declaration boundaries. The returned value is actually
  1111. the position of the earliest boundary char. FROM must not be within
  1112. a string or comment.
  1113. The variable `c-maybe-labelp' is set to the position of the first `:' that
  1114. might start a label (i.e. not part of `::' and not preceded by `?'). If a
  1115. single `?' is found, then `c-maybe-labelp' is cleared.
  1116. For AWK, a statement which is terminated by an EOL (not a \; or a }) is
  1117. regarded as having a \"virtual semicolon\" immediately after the last token on
  1118. the line. If this virtual semicolon is _at_ from, the function recognizes it.
  1119. Note that this function might do hidden buffer changes. See the
  1120. comment at the start of cc-engine.el for more info."
  1121. (let* ((skip-chars
  1122. ;; If the current language has CPP macros, insert # into skip-chars.
  1123. (if c-opt-cpp-symbol
  1124. (concat (substring c-stmt-delim-chars 0 1) ; "^"
  1125. c-opt-cpp-symbol ; usually "#"
  1126. (substring c-stmt-delim-chars 1)) ; e.g. ";{}?:"
  1127. c-stmt-delim-chars))
  1128. (non-skip-list
  1129. (append (substring skip-chars 1) nil)) ; e.g. (?# ?\; ?{ ?} ?? ?:)
  1130. lit-range vsemi-pos)
  1131. (save-restriction
  1132. (widen)
  1133. (save-excursion
  1134. (catch 'done
  1135. (goto-char from)
  1136. (while (progn (skip-chars-forward
  1137. skip-chars
  1138. (min to (c-point 'bonl)))
  1139. (< (point) to))
  1140. (cond
  1141. ;; Virtual semicolon?
  1142. ((and (bolp)
  1143. (save-excursion
  1144. (progn
  1145. (if (setq lit-range (c-literal-limits from)) ; Have we landed in a string/comment?
  1146. (goto-char (car lit-range)))
  1147. (c-backward-syntactic-ws) ; ? put a limit here, maybe?
  1148. (setq vsemi-pos (point))
  1149. (c-at-vsemi-p))))
  1150. (throw 'done vsemi-pos))
  1151. ;; In a string/comment?
  1152. ((setq lit-range (c-literal-limits from))
  1153. (goto-char (cdr lit-range)))
  1154. ((eq (char-after) ?:)
  1155. (forward-char)
  1156. (if (and (eq (char-after) ?:)
  1157. (< (point) to))
  1158. ;; Ignore scope operators.
  1159. (forward-char)
  1160. (setq c-maybe-labelp (1- (point)))))
  1161. ((eq (char-after) ??)
  1162. ;; A question mark. Can't be a label, so stop
  1163. ;; looking for more : and ?.
  1164. (setq c-maybe-labelp nil
  1165. skip-chars (substring c-stmt-delim-chars 0 -2)))
  1166. ;; At a CPP construct or a "#" or "##" operator?
  1167. ((and c-opt-cpp-symbol (looking-at c-opt-cpp-symbol))
  1168. (if (save-excursion
  1169. (skip-chars-backward " \t")
  1170. (and (bolp)
  1171. (or (bobp)
  1172. (not (eq (char-before (1- (point))) ?\\)))))
  1173. (c-end-of-macro)
  1174. (skip-chars-forward c-opt-cpp-symbol)))
  1175. ((memq (char-after) non-skip-list)
  1176. (throw 'done (point)))))
  1177. ;; In trailing space after an as yet undetected virtual semicolon?
  1178. (c-backward-syntactic-ws from)
  1179. (when (and (bolp) (not (bobp))) ; Can happen in AWK Mode with an
  1180. ; unterminated string/regexp.
  1181. (backward-char))
  1182. (if (and (< (point) to)
  1183. (c-at-vsemi-p))
  1184. (point)
  1185. nil))))))
  1186. (defun c-at-statement-start-p ()
  1187. "Return non-nil if the point is at the first token in a statement
  1188. or somewhere in the syntactic whitespace before it.
  1189. A \"statement\" here is not restricted to those inside code blocks.
  1190. Any kind of declaration-like construct that occur outside function
  1191. bodies is also considered a \"statement\".
  1192. Note that this function might do hidden buffer changes. See the
  1193. comment at the start of cc-engine.el for more info."
  1194. (save-excursion
  1195. (let ((end (point))
  1196. c-maybe-labelp)
  1197. (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
  1198. (or (bobp)
  1199. (eq (char-before) ?})
  1200. (and (eq (char-before) ?{)
  1201. (not (and c-special-brace-lists
  1202. (progn (backward-char)
  1203. (c-looking-at-special-brace-list)))))
  1204. (c-crosses-statement-barrier-p (point) end)))))
  1205. (defun c-at-expression-start-p ()
  1206. "Return non-nil if the point is at the first token in an expression or
  1207. statement, or somewhere in the syntactic whitespace before it.
  1208. An \"expression\" here is a bit different from the normal language
  1209. grammar sense: It's any sequence of expression tokens except commas,
  1210. unless they are enclosed inside parentheses of some kind. Also, an
  1211. expression never continues past an enclosing parenthesis, but it might
  1212. contain parenthesis pairs of any sort except braces.
  1213. Since expressions never cross statement boundaries, this function also
  1214. recognizes statement beginnings, just like `c-at-statement-start-p'.
  1215. Note that this function might do hidden buffer changes. See the
  1216. comment at the start of cc-engine.el for more info."
  1217. (save-excursion
  1218. (let ((end (point))
  1219. (c-stmt-delim-chars c-stmt-delim-chars-with-comma)
  1220. c-maybe-labelp)
  1221. (c-syntactic-skip-backward (substring c-stmt-delim-chars 1) nil t)
  1222. (or (bobp)
  1223. (memq (char-before) '(?{ ?}))
  1224. (save-excursion (backward-char)
  1225. (looking-at "\\s("))
  1226. (c-crosses-statement-barrier-p (point) end)))))
  1227. ;; A set of functions that covers various idiosyncrasies in
  1228. ;; implementations of `forward-comment'.
  1229. ;; Note: Some emacsen considers incorrectly that any line comment
  1230. ;; ending with a backslash continues to the next line. I can't think
  1231. ;; of any way to work around that in a reliable way without changing
  1232. ;; the buffer, though. Suggestions welcome. ;) (No, temporarily
  1233. ;; changing the syntax for backslash doesn't work since we must treat
  1234. ;; escapes in string literals correctly.)
  1235. (defun c-forward-single-comment ()
  1236. "Move forward past whitespace and the closest following comment, if any.
  1237. Return t if a comment was found, nil otherwise. In either case, the
  1238. point is moved past the following whitespace. Line continuations,
  1239. i.e. a backslashes followed by line breaks, are treated as whitespace.
  1240. The line breaks that end line comments are considered to be the
  1241. comment enders, so the point will be put on the beginning of the next
  1242. line if it moved past a line comment.
  1243. This function does not do any hidden buffer changes."
  1244. (let ((start (point)))
  1245. (when (looking-at "\\([ \t\n\r\f\v]\\|\\\\[\n\r]\\)+")
  1246. (goto-char (match-end 0)))
  1247. (when (forward-comment 1)
  1248. (if (eobp)
  1249. ;; Some emacsen (e.g. XEmacs 21) return t when moving
  1250. ;; forwards at eob.
  1251. nil
  1252. ;; Emacs includes the ending newline in a b-style (c++)
  1253. ;; comment, but XEmacs doesn't. We depend on the Emacs
  1254. ;; behavior (which also is symmetric).
  1255. (if (and (eolp) (elt (parse-partial-sexp start (point)) 7))
  1256. (condition-case nil (forward-char 1)))
  1257. t))))
  1258. (defsubst c-forward-comments ()
  1259. "Move forward past all following whitespace and comments.
  1260. Line continuations, i.e. a backslashes followed by line breaks, are
  1261. treated as whitespace.
  1262. Note that this function might do hidden buffer changes. See the
  1263. comment at the start of cc-engine.el for more info."
  1264. (while (or
  1265. ;; If forward-comment in at least XEmacs 21 is given a large
  1266. ;; positive value, it'll loop all the way through if it hits
  1267. ;; eob.
  1268. (and (forward-comment 5)
  1269. ;; Some emacsen (e.g. XEmacs 21) return t when moving
  1270. ;; forwards at eob.
  1271. (not (eobp)))
  1272. (when (looking-at "\\\\[\n\r]")
  1273. (forward-char 2)
  1274. t))))
  1275. (defun c-backward-single-comment ()
  1276. "Move backward past whitespace and the closest preceding comment, if any.
  1277. Return t if a comment was found, nil otherwise. In either case, the
  1278. point is moved past the preceding whitespace. Line continuations,
  1279. i.e. a backslashes followed by line breaks, are treated as whitespace.
  1280. The line breaks that end line comments are considered to be the
  1281. comment enders, so the point cannot be at the end of the same line to
  1282. move over a line comment.
  1283. This function does not do any hidden buffer changes."
  1284. (let ((start (point)))
  1285. ;; When we got newline terminated comments, forward-comment in all
  1286. ;; supported emacsen so far will stop at eol of each line not
  1287. ;; ending with a comment when moving backwards. This corrects for
  1288. ;; that, and at the same time handles line continuations.
  1289. (while (progn
  1290. (skip-chars-backward " \t\n\r\f\v")
  1291. (and (looking-at "[\n\r]")
  1292. (eq (char-before) ?\\)))
  1293. (backward-char))
  1294. (if (bobp)
  1295. ;; Some emacsen (e.g. Emacs 19.34) return t when moving
  1296. ;; backwards at bob.
  1297. nil
  1298. ;; Leave point after the closest following newline if we've
  1299. ;; backed up over any above, since forward-comment won't move
  1300. ;; backward over a line comment if point is at the end of the
  1301. ;; same line.
  1302. (re-search-forward "\\=\\s *[\n\r]" start t)
  1303. (if (if (let (open-paren-in-column-0-is-defun-start) (forward-comment -1))
  1304. (if (eolp)
  1305. ;; If forward-comment above succeeded and we're at eol
  1306. ;; then the newline we moved over above didn't end a
  1307. ;; line comment, so we give it another go.
  1308. (let (open-paren-in-column-0-is-defun-start)
  1309. (forward-comment -1))
  1310. t))
  1311. ;; Emacs <= 20 and XEmacs move back over the closer of a
  1312. ;; block comment that lacks an opener.
  1313. (if (looking-at "\\*/")
  1314. (progn (forward-char 2) nil)
  1315. t)))))
  1316. (defsubst c-backward-comments ()
  1317. "Move backward past all preceding whitespace and comments.
  1318. Line continuations, i.e. a backslashes followed by line breaks, are
  1319. treated as whitespace. The line breaks that end line comments are
  1320. considered to be the comment enders, so the point cannot be at the end
  1321. of the same line to move over a line comment. Unlike
  1322. c-backward-syntactic-ws, this function doesn't move back over
  1323. preprocessor directives.
  1324. Note that this function might do hidden buffer changes. See the
  1325. comment at the start of cc-engine.el for more info."
  1326. (let ((start (point)))
  1327. (while (and
  1328. ;; `forward-comment' in some emacsen (e.g. XEmacs 21.4)
  1329. ;; return t when moving backwards at bob.
  1330. (not (bobp))
  1331. (if (let (open-paren-in-column-0-is-defun-start moved-comment)
  1332. (while
  1333. (and (not (setq moved-comment (forward-comment -1)))
  1334. ;; Cope specifically with ^M^J here -
  1335. ;; forward-comment sometimes gets stuck after ^Ms,
  1336. ;; sometimes after ^M^J.
  1337. (or
  1338. (when (eq (char-before) ?\r)
  1339. (backward-char)
  1340. t)
  1341. (when (and (eq (char-before) ?\n)
  1342. (eq (char-before (1- (point))) ?\r))
  1343. (backward-char 2)
  1344. t))))
  1345. moved-comment)
  1346. (if (looking-at "\\*/")
  1347. ;; Emacs <= 20 and XEmacs move back over the
  1348. ;; closer of a block comment that lacks an opener.
  1349. (progn (forward-char 2) nil)
  1350. t)
  1351. ;; XEmacs treats line continuations as whitespace but
  1352. ;; only in the backward direction, which seems a bit
  1353. ;; odd. Anyway, this is necessary for Emacs.
  1354. (when (and (looking-at "[\n\r]")
  1355. (eq (char-before) ?\\)
  1356. (< (point) start))
  1357. (backward-char)
  1358. t))))))
  1359. ;; Tools for skipping over syntactic whitespace.
  1360. ;; The following functions use text properties to cache searches over
  1361. ;; large regions of syntactic whitespace. It works as follows:
  1362. ;;
  1363. ;; o If a syntactic whitespace region contains anything but simple
  1364. ;; whitespace (i.e. space, tab and line breaks), the text property
  1365. ;; `c-in-sws' is put over it. At places where we have stopped
  1366. ;; within that region there's also a `c-is-sws' text property.
  1367. ;; That since there typically are nested whitespace inside that
  1368. ;; must be handled separately, e.g. whitespace inside a comment or
  1369. ;; cpp directive. Thus, from one point with `c-is-sws' it's safe
  1370. ;; to jump to another point with that property within the same
  1371. ;; `c-in-sws' region. It can be likened to a ladder where
  1372. ;; `c-in-sws' marks the bars and `c-is-sws' the rungs.
  1373. ;;
  1374. ;; o The `c-is-sws' property is put on the simple whitespace chars at
  1375. ;; a "rung position" and also maybe on the first following char.
  1376. ;; As many characters as can be conveniently found in this range
  1377. ;; are marked, but no assumption can be made that the whole range
  1378. ;; is marked (it could be clobbered by later changes, for
  1379. ;; instance).
  1380. ;;
  1381. ;; Note that some part of the beginning of a sequence of simple
  1382. ;; whitespace might be part of the end of a preceding line comment
  1383. ;; or cpp directive and must not be considered part of the "rung".
  1384. ;; Such whitespace is some amount of horizontal whitespace followed
  1385. ;; by a newline. In the case of cpp directives it could also be
  1386. ;; two newlines with horizontal whitespace between them.
  1387. ;;
  1388. ;; The reason to include the first following char is to cope with
  1389. ;; "rung positions" that doesn't have any ordinary whitespace. If
  1390. ;; `c-is-sws' is put on a token character it does not have
  1391. ;; `c-in-sws' set simultaneously. That's the only case when that
  1392. ;; can occur, and the reason for not extending the `c-in-sws'
  1393. ;; region to cover it is that the `c-in-sws' region could then be
  1394. ;; accidentally merged with a following one if the token is only
  1395. ;; one character long.
  1396. ;;
  1397. ;; o On buffer changes the `c-in-sws' and `c-is-sws' properties are
  1398. ;; removed in the changed region. If the change was inside
  1399. ;; syntactic whitespace that means that the "ladder" is broken, but
  1400. ;; a later call to `c-forward-sws' or `c-backward-sws' will use the
  1401. ;; parts on either side and use an ordinary search only to "repair"
  1402. ;; the gap.
  1403. ;;
  1404. ;; Special care needs to be taken if a region is removed: If there
  1405. ;; are `c-in-sws' on both sides of it which do not connect inside
  1406. ;; the region then they can't be joined. If e.g. a marked macro is
  1407. ;; broken, syntactic whitespace inside the new text might be
  1408. ;; marked. If those marks would become connected with the old
  1409. ;; `c-in-sws' range around the macro then we could get a ladder
  1410. ;; with one end outside the macro and the other at some whitespace
  1411. ;; within it.
  1412. ;;
  1413. ;; The main motivation for this system is to increase the speed in
  1414. ;; skipping over the large whitespace regions that can occur at the
  1415. ;; top level in e.g. header files that contain a lot of comments and
  1416. ;; cpp directives. For small comments inside code it's probably
  1417. ;; slower than using `forward-comment' straightforwardly, but speed is
  1418. ;; not a significant factor there anyway.
  1419. ; (defface c-debug-is-sws-face
  1420. ; '((t (:background "GreenYellow")))
  1421. ; "Debug face to mark the `c-is-sws' property.")
  1422. ; (defface c-debug-in-sws-face
  1423. ; '((t (:underline t)))
  1424. ; "Debug face to mark the `c-in-sws' property.")
  1425. ; (defun c-debug-put-sws-faces ()
  1426. ; ;; Put the sws debug faces on all the `c-is-sws' and `c-in-sws'
  1427. ; ;; properties in the buffer.
  1428. ; (interactive)
  1429. ; (save-excursion
  1430. ; (c-save-buffer-state (in-face)
  1431. ; (goto-char (point-min))
  1432. ; (setq in-face (if (get-text-property (point) 'c-is-sws)
  1433. ; (point)))
  1434. ; (while (progn
  1435. ; (goto-char (next-single-property-change
  1436. ; (point) 'c-is-sws nil (point-max)))
  1437. ; (if in-face
  1438. ; (progn
  1439. ; (c-debug-add-face in-face (point) 'c-debug-is-sws-face)
  1440. ; (setq in-face nil))
  1441. ; (setq in-face (point)))
  1442. ; (not (eobp))))
  1443. ; (goto-char (point-min))
  1444. ; (setq in-face (if (get-text-property (point) 'c-in-sws)
  1445. ; (point)))
  1446. ; (while (progn
  1447. ; (goto-char (next-single-property-change
  1448. ; (point) 'c-in-sws nil (point-max)))
  1449. ; (if in-face
  1450. ; (progn
  1451. ; (c-debug-add-face in-face (point) 'c-debug-in-sws-face)
  1452. ; (setq in-face nil))
  1453. ; (setq in-face (point)))
  1454. ; (not (eobp)))))))
  1455. (defmacro c-debug-sws-msg (&rest args)
  1456. ;;`(message ,@args)
  1457. )
  1458. (defmacro c-put-is-sws (beg end)
  1459. ;; This macro does a hidden buffer change.
  1460. `(let ((beg ,beg) (end ,end))
  1461. (put-text-property beg end 'c-is-sws t)
  1462. ,@(when (facep 'c-debug-is-sws-face)
  1463. `((c-debug-add-face beg end 'c-debug-is-sws-face)))))
  1464. (defmacro c-put-in-sws (beg end)
  1465. ;; This macro does a hidden buffer change.
  1466. `(let ((beg ,beg) (end ,end))
  1467. (put-text-property beg end 'c-in-sws t)
  1468. ,@(when (facep 'c-debug-is-sws-face)
  1469. `((c-debug-add-face beg end 'c-debug-in-sws-face)))))
  1470. (defmacro c-remove-is-sws (beg end)
  1471. ;; This macro does a hidden buffer change.
  1472. `(let ((beg ,beg) (end ,end))
  1473. (remove-text-properties beg end '(c-is-sws nil))
  1474. ,@(when (facep 'c-debug-is-sws-face)
  1475. `((c-debug-remove-face beg end 'c-debug-is-sws-face)))))
  1476. (defmacro c-remove-in-sws (beg end)
  1477. ;; This macro does a hidden buffer change.
  1478. `(let ((beg ,beg) (end ,end))
  1479. (remove-text-properties beg end '(c-in-sws nil))
  1480. ,@(when (facep 'c-debug-is-sws-face)
  1481. `((c-debug-remove-face beg end 'c-debug-in-sws-face)))))
  1482. (defmacro c-remove-is-and-in-sws (beg end)
  1483. ;; This macro does a hidden buffer change.
  1484. `(let ((beg ,beg) (end ,end))
  1485. (remove-text-properties beg end '(c-is-sws nil c-in-sws nil))
  1486. ,@(when (facep 'c-debug-is-sws-face)
  1487. `((c-debug-remove-face beg end 'c-debug-is-sws-face)
  1488. (c-debug-remove-face beg end 'c-debug-in-sws-face)))))
  1489. (defsubst c-invalidate-sws-region-after (beg end)
  1490. ;; Called from `after-change-functions'. Note that if
  1491. ;; `c-forward-sws' or `c-backward-sws' are used outside
  1492. ;; `c-save-buffer-state' or similar then this will remove the cache
  1493. ;; properties right after they're added.
  1494. ;;
  1495. ;; This function does hidden buffer changes.
  1496. (save-excursion
  1497. ;; Adjust the end to remove the properties in any following simple
  1498. ;; ws up to and including the next line break, if there is any
  1499. ;; after the changed region. This is necessary e.g. when a rung
  1500. ;; marked empty line is converted to a line comment by inserting
  1501. ;; "//" before the line break. In that case the line break would
  1502. ;; keep the rung mark which could make a later `c-backward-sws'
  1503. ;; move into the line comment instead of over it.
  1504. (goto-char end)
  1505. (skip-chars-forward " \t\f\v")
  1506. (when (and (eolp) (not (eobp)))
  1507. (setq end (1+ (point)))))
  1508. (when (and (= beg end)
  1509. (get-text-property beg 'c-in-sws)
  1510. (> beg (point-min))
  1511. (get-text-property (1- beg) 'c-in-sws))
  1512. ;; Ensure that an `c-in-sws' range gets broken. Note that it isn't
  1513. ;; safe to keep a range that was continuous before the change. E.g:
  1514. ;;
  1515. ;; #define foo
  1516. ;; \
  1517. ;; bar
  1518. ;;
  1519. ;; There can be a "ladder" between "#" and "b". Now, if the newline
  1520. ;; after "foo" is removed then "bar" will become part of the cpp
  1521. ;; directive instead of a syntactically relevant token. In that
  1522. ;; case there's no longer syntactic ws from "#" to "b".
  1523. (setq beg (1- beg)))
  1524. (c-debug-sws-msg "c-invalidate-sws-region-after [%s..%s]" beg end)
  1525. (c-remove-is-and-in-sws beg end))
  1526. (defun c-forward-sws ()
  1527. ;; Used by `c-forward-syntactic-ws' to implement the unbounded search.
  1528. ;;
  1529. ;; This function might do hidden buffer changes.
  1530. (let (;; `rung-pos' is set to a position as early as possible in the
  1531. ;; unmarked part of the simple ws region.
  1532. (rung-pos (point)) next-rung-pos rung-end-pos last-put-in-sws-pos
  1533. rung-is-marked next-rung-is-marked simple-ws-end
  1534. ;; `safe-start' is set when it's safe to cache the start position.
  1535. ;; It's not set if we've initially skipped over comments and line
  1536. ;; continuations since we might have gone out through the end of a
  1537. ;; macro then. This provision makes `c-forward-sws' not populate the
  1538. ;; cache in the majority of cases, but otoh is `c-backward-sws' by far
  1539. ;; more common.
  1540. safe-start)
  1541. ;; Skip simple ws and do a quick check on the following character to see
  1542. ;; if it's anything that can't start syntactic ws, so we can bail out
  1543. ;; early in the majority of cases when there just are a few ws chars.
  1544. (skip-chars-forward " \t\n\r\f\v")
  1545. (when (looking-at c-syntactic-ws-start)
  1546. (setq rung-end-pos (min (1+ (point)) (point-max)))
  1547. (if (setq rung-is-marked (text-property-any rung-pos rung-end-pos
  1548. 'c-is-sws t))
  1549. ;; Find the last rung position to avoid setting properties in all
  1550. ;; the cases when the marked rung is complete.
  1551. ;; (`next-single-property-change' is certain to move at least one
  1552. ;; step forward.)
  1553. (setq rung-pos (1- (c-next-single-property-change
  1554. rung-is-marked 'c-is-sws nil rung-end-pos)))
  1555. ;; Got no marked rung here. Since the simple ws might have started
  1556. ;; inside a line comment or cpp directive we must set `rung-pos' as
  1557. ;; high as possible.
  1558. (setq rung-pos (point)))
  1559. (with-silent-modifications
  1560. (while
  1561. (progn
  1562. (while
  1563. (when (and rung-is-marked
  1564. (get-text-property (point) 'c-in-sws))
  1565. ;; The following search is the main reason that `c-in-sws'
  1566. ;; and `c-is-sws' aren't combined to one property.
  1567. (goto-char (c-next-single-property-change
  1568. (point) 'c-in-sws nil (point-max)))
  1569. (unless (get-text-property (point) 'c-is-sws)
  1570. ;; If the `c-in-sws' region extended past the last
  1571. ;; `c-is-sws' char we have to go back a bit.
  1572. (or (get-text-property (1- (point)) 'c-is-sws)
  1573. (goto-char (previous-single-property-change
  1574. (point) 'c-is-sws)))
  1575. (backward-char))
  1576. (c-debug-sws-msg
  1577. "c-forward-sws cached move %s -> %s (max %s)"
  1578. rung-pos (point) (point-max))
  1579. (setq rung-pos (point))
  1580. (and (> (skip-chars-forward " \t\n\r\f\v") 0)
  1581. (not (eobp))))
  1582. ;; We'll loop here if there is simple ws after the last rung.
  1583. ;; That means that there's been some change in it and it's
  1584. ;; possible that we've stepped into another ladder, so extend
  1585. ;; the previous one to join with it if there is one, and try to
  1586. ;; use the cache again.
  1587. (c-debug-sws-msg
  1588. "c-forward-sws extending rung with [%s..%s] (max %s)"
  1589. (1+ rung-pos) (1+ (point)) (point-max))
  1590. (unless (get-text-property (point) 'c-is-sws)
  1591. ;; Remove any `c-in-sws' property from the last char of
  1592. ;; the rung before we mark it with `c-is-sws', so that we
  1593. ;; won't connect with the remains of a broken "ladder".
  1594. (c-remove-in-sws (point) (1+ (point))))
  1595. (c-put-is-sws (1+ rung-pos)
  1596. (1+ (point)))
  1597. (c-put-in-sws rung-pos
  1598. (setq rung-pos (point)
  1599. last-put-in-sws-pos rung-pos)))
  1600. (setq simple-ws-end (point))
  1601. (c-forward-comments)
  1602. (cond
  1603. ((/= (point) simple-ws-end)
  1604. ;; Skipped over comments. Don't cache at eob in case the buffer
  1605. ;; is narrowed.
  1606. (not (eobp)))
  1607. ((save-excursion
  1608. (and c-opt-cpp-prefix
  1609. (looking-at c-opt-cpp-start)
  1610. (progn (skip-chars-backward " \t")
  1611. (bolp))
  1612. (or (bobp)
  1613. (progn (backward-char)
  1614. (not (eq (char-before) ?\\))))))
  1615. ;; Skip a preprocessor directive.
  1616. (end-of-line)
  1617. (while (and (eq (char-before) ?\\)
  1618. (= (forward-line 1) 0))
  1619. (end-of-line))
  1620. (forward-line 1)
  1621. (setq safe-start t)
  1622. ;; Don't cache at eob in case the buffer is narrowed.
  1623. (not (eobp)))))
  1624. ;; We've searched over a piece of non-white syntactic ws. See if this
  1625. ;; can be cached.
  1626. (setq next-rung-pos (point))
  1627. (skip-chars-forward " \t\n\r\f\v")
  1628. (setq rung-end-pos (min (1+ (point)) (point-max)))
  1629. (if (or
  1630. ;; Cache if we haven't skipped comments only, and if we started
  1631. ;; either from a marked rung or from a completely uncached
  1632. ;; position.
  1633. (and safe-start
  1634. (or rung-is-marked
  1635. (not (get-text-property simple-ws-end 'c-in-sws))))
  1636. ;; See if there's a marked rung in the encountered simple ws. If
  1637. ;; so then we can cache, unless `safe-start' is nil. Even then
  1638. ;; we need to do this to check if the cache can be used for the
  1639. ;; next step.
  1640. (and (setq next-rung-is-marked
  1641. (text-property-any next-rung-pos rung-end-pos
  1642. 'c-is-sws t))
  1643. safe-start))
  1644. (progn
  1645. (c-debug-sws-msg
  1646. "c-forward-sws caching [%s..%s] - [%s..%s] (max %s)"
  1647. rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
  1648. (point-max))
  1649. ;; Remove the properties for any nested ws that might be cached.
  1650. ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
  1651. ;; anyway.
  1652. (c-remove-is-sws (1+ simple-ws-end) next-rung-pos)
  1653. (unless (and rung-is-marked (= rung-pos simple-ws-end))
  1654. (c-put-is-sws rung-pos
  1655. (1+ simple-ws-end))
  1656. (setq rung-is-marked t))
  1657. (c-put-in-sws rung-pos
  1658. (setq rung-pos (point)
  1659. last-put-in-sws-pos rung-pos))
  1660. (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
  1661. ;; Remove any `c-in-sws' property from the last char of
  1662. ;; the rung before we mark it with `c-is-sws', so that we
  1663. ;; won't connect with the remains of a broken "ladder".
  1664. (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
  1665. (c-put-is-sws next-rung-pos
  1666. rung-end-pos))
  1667. (c-debug-sws-msg
  1668. "c-forward-sws not caching [%s..%s] - [%s..%s] (max %s)"
  1669. rung-pos (1+ simple-ws-end) next-rung-pos rung-end-pos
  1670. (point-max))
  1671. ;; Set `rung-pos' for the next rung. It's the same thing here as
  1672. ;; initially, except that the rung position is set as early as
  1673. ;; possible since we can't be in the ending ws of a line comment or
  1674. ;; cpp directive now.
  1675. (if (setq rung-is-marked next-rung-is-marked)
  1676. (setq rung-pos (1- (c-next-single-property-change
  1677. rung-is-marked 'c-is-sws nil rung-end-pos)))
  1678. (setq rung-pos next-rung-pos))
  1679. (setq safe-start t)))
  1680. ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
  1681. ;; another one after the point (which might occur when editing inside a
  1682. ;; comment or macro).
  1683. (when (eq last-put-in-sws-pos (point))
  1684. (cond ((< last-put-in-sws-pos (point-max))
  1685. (c-debug-sws-msg
  1686. "c-forward-sws clearing at %s for cache separation"
  1687. last-put-in-sws-pos)
  1688. (c-remove-in-sws last-put-in-sws-pos
  1689. (1+ last-put-in-sws-pos)))
  1690. (t
  1691. ;; If at eob we have to clear the last character before the end
  1692. ;; instead since the buffer might be narrowed and there might
  1693. ;; be a `c-in-sws' after (point-max). In this case it's
  1694. ;; necessary to clear both properties.
  1695. (c-debug-sws-msg
  1696. "c-forward-sws clearing thoroughly at %s for cache separation"
  1697. (1- last-put-in-sws-pos))
  1698. (c-remove-is-and-in-sws (1- last-put-in-sws-pos)
  1699. last-put-in-sws-pos))))
  1700. ))))
  1701. (defun c-backward-sws ()
  1702. ;; Used by `c-backward-syntactic-ws' to implement the unbounded search.
  1703. ;;
  1704. ;; This function might do hidden buffer changes.
  1705. (let (;; `rung-pos' is set to a position as late as possible in the unmarked
  1706. ;; part of the simple ws region.
  1707. (rung-pos (point)) next-rung-pos last-put-in-sws-pos
  1708. rung-is-marked simple-ws-beg cmt-skip-pos)
  1709. ;; Skip simple horizontal ws and do a quick check on the preceding
  1710. ;; character to see if it's anything that can't end syntactic ws, so we can
  1711. ;; bail out early in the majority of cases when there just are a few ws
  1712. ;; chars. Newlines are complicated in the backward direction, so we can't
  1713. ;; skip over them.
  1714. (skip-chars-backward " \t\f")
  1715. (when (and (not (bobp))
  1716. (save-excursion
  1717. (backward-char)
  1718. (looking-at c-syntactic-ws-end)))
  1719. ;; Try to find a rung position in the simple ws preceding point, so that
  1720. ;; we can get a cache hit even if the last bit of the simple ws has
  1721. ;; changed recently.
  1722. (setq simple-ws-beg (point))
  1723. (skip-chars-backward " \t\n\r\f\v")
  1724. (if (setq rung-is-marked (text-property-any
  1725. (point) (min (1+ rung-pos) (point-max))
  1726. 'c-is-sws t))
  1727. ;; `rung-pos' will be the earliest marked position, which means that
  1728. ;; there might be later unmarked parts in the simple ws region.
  1729. ;; It's not worth the effort to fix that; the last part of the
  1730. ;; simple ws is also typically edited often, so it could be wasted.
  1731. (goto-char (setq rung-pos rung-is-marked))
  1732. (goto-char simple-ws-beg))
  1733. (with-silent-modifications
  1734. (while
  1735. (progn
  1736. (while
  1737. (when (and rung-is-marked
  1738. (not (bobp))
  1739. (get-text-property (1- (point)) 'c-in-sws))
  1740. ;; The following search is the main reason that `c-in-sws'
  1741. ;; and `c-is-sws' aren't combined to one property.
  1742. (goto-char (previous-single-property-change
  1743. (point) 'c-in-sws nil (point-min)))
  1744. (unless (get-text-property (point) 'c-is-sws)
  1745. ;; If the `c-in-sws' region extended past the first
  1746. ;; `c-is-sws' char we have to go forward a bit.
  1747. (goto-char (c-next-single-property-change
  1748. (point) 'c-is-sws)))
  1749. (c-debug-sws-msg
  1750. "c-backward-sws cached move %s <- %s (min %s)"
  1751. (point) rung-pos (point-min))
  1752. (setq rung-pos (point))
  1753. (if (and (< (min (skip-chars-backward " \t\f\v")
  1754. (progn
  1755. (setq simple-ws-beg (point))
  1756. (skip-chars-backward " \t\n\r\f\v")))
  1757. 0)
  1758. (setq rung-is-marked
  1759. (text-property-any (point) rung-pos
  1760. 'c-is-sws t)))
  1761. t
  1762. (goto-char simple-ws-beg)
  1763. nil))
  1764. ;; We'll loop here if there is simple ws before the first rung.
  1765. ;; That means that there's been some change in it and it's
  1766. ;; possible that we've stepped into another ladder, so extend
  1767. ;; the previous one to join with it if there is one, and try to
  1768. ;; use the cache again.
  1769. (c-debug-sws-msg
  1770. "c-backward-sws extending rung with [%s..%s] (min %s)"
  1771. rung-is-marked rung-pos (point-min))
  1772. (unless (get-text-property (1- rung-pos) 'c-is-sws)
  1773. ;; Remove any `c-in-sws' property from the last char of
  1774. ;; the rung before we mark it with `c-is-sws', so that we
  1775. ;; won't connect with the remains of a broken "ladder".
  1776. (c-remove-in-sws (1- rung-pos) rung-pos))
  1777. (c-put-is-sws rung-is-marked
  1778. rung-pos)
  1779. (c-put-in-sws rung-is-marked
  1780. (1- rung-pos))
  1781. (setq rung-pos rung-is-marked
  1782. last-put-in-sws-pos rung-pos))
  1783. (c-backward-comments)
  1784. (setq cmt-skip-pos (point))
  1785. (cond
  1786. ((and c-opt-cpp-prefix
  1787. (/= cmt-skip-pos simple-ws-beg)
  1788. (c-beginning-of-macro))
  1789. ;; Inside a cpp directive. See if it should be skipped over.
  1790. (let ((cpp-beg (point)))
  1791. ;; Move back over all line continuations in the region skipped
  1792. ;; over by `c-backward-comments'. If we go past it then we
  1793. ;; started inside the cpp directive.
  1794. (goto-char simple-ws-beg)
  1795. (beginning-of-line)
  1796. (while (and (> (point) cmt-skip-pos)
  1797. (progn (backward-char)
  1798. (eq (char-before) ?\\)))
  1799. (beginning-of-line))
  1800. (if (< (point) cmt-skip-pos)
  1801. ;; Don't move past the cpp directive if we began inside
  1802. ;; it. Note that the position at the end of the last line
  1803. ;; of the macro is also considered to be within it.
  1804. (progn (goto-char cmt-skip-pos)
  1805. nil)
  1806. ;; It's worthwhile to spend a little bit of effort on finding
  1807. ;; the end of the macro, to get a good `simple-ws-beg'
  1808. ;; position for the cache. Note that `c-backward-comments'
  1809. ;; could have stepped over some comments before going into
  1810. ;; the macro, and then `simple-ws-beg' must be kept on the
  1811. ;; same side of those comments.
  1812. (goto-char simple-ws-beg)
  1813. (skip-chars-backward " \t\n\r\f\v")
  1814. (if (eq (char-before) ?\\)
  1815. (forward-char))
  1816. (forward-line 1)
  1817. (if (< (point) simple-ws-beg)
  1818. ;; Might happen if comments after the macro were skipped
  1819. ;; over.
  1820. (setq simple-ws-beg (point)))
  1821. (goto-char cpp-beg)
  1822. t)))
  1823. ((/= (save-excursion
  1824. (skip-chars-forward " \t\n\r\f\v" simple-ws-beg)
  1825. (setq next-rung-pos (point)))
  1826. simple-ws-beg)
  1827. ;; Skipped over comments. Must put point at the end of
  1828. ;; the simple ws at point since we might be after a line
  1829. ;; comment or cpp directive that's been partially
  1830. ;; narrowed out, and we can't risk marking the simple ws
  1831. ;; at the end of it.
  1832. (goto-char next-rung-pos)
  1833. t)))
  1834. ;; We've searched over a piece of non-white syntactic ws. See if this
  1835. ;; can be cached.
  1836. (setq next-rung-pos (point))
  1837. (skip-chars-backward " \t\f\v")
  1838. (if (or
  1839. ;; Cache if we started either from a marked rung or from a
  1840. ;; completely uncached position.
  1841. rung-is-marked
  1842. (not (get-text-property (1- simple-ws-beg) 'c-in-sws))
  1843. ;; Cache if there's a marked rung in the encountered simple ws.
  1844. (save-excursion
  1845. (skip-chars-backward " \t\n\r\f\v")
  1846. (text-property-any (point) (min (1+ next-rung-pos) (point-max))
  1847. 'c-is-sws t)))
  1848. (progn
  1849. (c-debug-sws-msg
  1850. "c-backward-sws caching [%s..%s] - [%s..%s] (min %s)"
  1851. (point) (1+ next-rung-pos)
  1852. simple-ws-beg (min (1+ rung-pos) (point-max))
  1853. (point-min))
  1854. ;; Remove the properties for any nested ws that might be cached.
  1855. ;; Only necessary for `c-is-sws' since `c-in-sws' will be set
  1856. ;; anyway.
  1857. (c-remove-is-sws (1+ next-rung-pos) simple-ws-beg)
  1858. (unless (and rung-is-marked (= simple-ws-beg rung-pos))
  1859. (let ((rung-end-pos (min (1+ rung-pos) (point-max))))
  1860. (unless (get-text-property (1- rung-end-pos) 'c-is-sws)
  1861. ;; Remove any `c-in-sws' property from the last char of
  1862. ;; the rung before we mark it with `c-is-sws', so that we
  1863. ;; won't connect with the remains of a broken "ladder".
  1864. (c-remove-in-sws (1- rung-end-pos) rung-end-pos))
  1865. (c-put-is-sws simple-ws-beg
  1866. rung-end-pos)
  1867. (setq rung-is-marked t)))
  1868. (c-put-in-sws (setq simple-ws-beg (point)
  1869. last-put-in-sws-pos simple-ws-beg)
  1870. rung-pos)
  1871. (c-put-is-sws (setq rung-pos simple-ws-beg)
  1872. (1+ next-rung-pos)))
  1873. (c-debug-sws-msg
  1874. "c-backward-sws not caching [%s..%s] - [%s..%s] (min %s)"
  1875. (point) (1+ next-rung-pos)
  1876. simple-ws-beg (min (1+ rung-pos) (point-max))
  1877. (point-min))
  1878. (setq rung-pos next-rung-pos
  1879. simple-ws-beg (point))
  1880. ))
  1881. ;; Make sure that the newly marked `c-in-sws' region doesn't connect to
  1882. ;; another one before the point (which might occur when editing inside a
  1883. ;; comment or macro).
  1884. (when (eq last-put-in-sws-pos (point))
  1885. (cond ((< (point-min) last-put-in-sws-pos)
  1886. (c-debug-sws-msg
  1887. "c-backward-sws clearing at %s for cache separation"
  1888. (1- last-put-in-sws-pos))
  1889. (c-remove-in-sws (1- last-put-in-sws-pos)
  1890. last-put-in-sws-pos))
  1891. ((> (point-min) 1)
  1892. ;; If at bob and the buffer is narrowed, we have to clear the
  1893. ;; character we're standing on instead since there might be a
  1894. ;; `c-in-sws' before (point-min). In this case it's necessary
  1895. ;; to clear both properties.
  1896. (c-debug-sws-msg
  1897. "c-backward-sws clearing thoroughly at %s for cache separation"
  1898. last-put-in-sws-pos)
  1899. (c-remove-is-and-in-sws last-put-in-sws-pos
  1900. (1+ last-put-in-sws-pos)))))
  1901. ))))
  1902. ;; Other whitespace tools
  1903. (defun c-partial-ws-p (beg end)
  1904. ;; Is the region (beg end) WS, and is there WS (or BOB/EOB) next to the
  1905. ;; region? This is a "heuristic" function. .....
  1906. ;;
  1907. ;; The motivation for the second bit is to check whether removing this
  1908. ;; region would coalesce two symbols.
  1909. ;;
  1910. ;; FIXME!!! This function doesn't check virtual semicolons in any way. Be
  1911. ;; careful about using this function for, e.g. AWK. (2007/3/7)
  1912. (save-excursion
  1913. (let ((end+1 (min (1+ end) (point-max))))
  1914. (or (progn (goto-char (max (point-min) (1- beg)))
  1915. (c-skip-ws-forward end)
  1916. (eq (point) end))
  1917. (progn (goto-char beg)
  1918. (c-skip-ws-forward end+1)
  1919. (eq (point) end+1))))))
  1920. ;; A system for finding noteworthy parens before the point.
  1921. (defconst c-state-cache-too-far 5000)
  1922. ;; A maximum comfortable scanning distance, e.g. between
  1923. ;; `c-state-cache-good-pos' and "HERE" (where we call c-parse-state). When
  1924. ;; this distance is exceeded, we take "emergency measures", e.g. by clearing
  1925. ;; the cache and starting again from point-min or a beginning of defun. This
  1926. ;; value can be tuned for efficiency or set to a lower value for testing.
  1927. (defvar c-state-cache nil)
  1928. (make-variable-buffer-local 'c-state-cache)
  1929. ;; The state cache used by `c-parse-state' to cut down the amount of
  1930. ;; searching. It's the result from some earlier `c-parse-state' call. See
  1931. ;; `c-parse-state''s doc string for details of its structure.
  1932. ;;
  1933. ;; The use of the cached info is more effective if the next
  1934. ;; `c-parse-state' call is on a line close by the one the cached state
  1935. ;; was made at; the cache can actually slow down a little if the
  1936. ;; cached state was made very far back in the buffer. The cache is
  1937. ;; most effective if `c-parse-state' is used on each line while moving
  1938. ;; forward.
  1939. (defvar c-state-cache-good-pos 1)
  1940. (make-variable-buffer-local 'c-state-cache-good-pos)
  1941. ;; This is a position where `c-state-cache' is known to be correct, or
  1942. ;; nil (see below). It's a position inside one of the recorded unclosed
  1943. ;; parens or the top level, but not further nested inside any literal or
  1944. ;; subparen that is closed before the last recorded position.
  1945. ;;
  1946. ;; The exact position is chosen to try to be close to yet earlier than
  1947. ;; the position where `c-state-cache' will be called next. Right now
  1948. ;; the heuristic is to set it to the position after the last found
  1949. ;; closing paren (of any type) before the line on which
  1950. ;; `c-parse-state' was called. That is chosen primarily to work well
  1951. ;; with refontification of the current line.
  1952. ;;
  1953. ;; 2009-07-28: When `c-state-point-min' and the last position where
  1954. ;; `c-parse-state' or for which `c-invalidate-state-cache' was called, are
  1955. ;; both in the same literal, there is no such "good position", and
  1956. ;; c-state-cache-good-pos is then nil. This is the ONLY circumstance in which
  1957. ;; it can be nil. In this case, `c-state-point-min-literal' will be non-nil.
  1958. ;;
  1959. ;; 2009-06-12: In a brace desert, c-state-cache-good-pos may also be in
  1960. ;; the middle of the desert, as long as it is not within a brace pair
  1961. ;; recorded in `c-state-cache' or a paren/bracket pair.
  1962. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1963. ;; We maintain a simple cache of positions which aren't in a literal, so as to
  1964. ;; speed up testing for non-literality.
  1965. (defconst c-state-nonlit-pos-interval 3000)
  1966. ;; The approximate interval between entries in `c-state-nonlit-pos-cache'.
  1967. (defvar c-state-nonlit-pos-cache nil)
  1968. (make-variable-buffer-local 'c-state-nonlit-pos-cache)
  1969. ;; A list of buffer positions which are known not to be in a literal or a cpp
  1970. ;; construct. This is ordered with higher positions at the front of the list.
  1971. ;; Only those which are less than `c-state-nonlit-pos-cache-limit' are valid.
  1972. (defvar c-state-nonlit-pos-cache-limit 1)
  1973. (make-variable-buffer-local 'c-state-nonlit-pos-cache-limit)
  1974. ;; An upper limit on valid entries in `c-state-nonlit-pos-cache'. This is
  1975. ;; reduced by buffer changes, and increased by invocations of
  1976. ;; `c-state-literal-at'.
  1977. (defvar c-state-semi-nonlit-pos-cache nil)
  1978. (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache)
  1979. ;; A list of buffer positions which are known not to be in a literal. This is
  1980. ;; ordered with higher positions at the front of the list. Only those which
  1981. ;; are less than `c-state-semi-nonlit-pos-cache-limit' are valid.
  1982. (defvar c-state-semi-nonlit-pos-cache-limit 1)
  1983. (make-variable-buffer-local 'c-state-semi-nonlit-pos-cache-limit)
  1984. ;; An upper limit on valid entries in `c-state-semi-nonlit-pos-cache'. This is
  1985. ;; reduced by buffer changes, and increased by invocations of
  1986. ;; `c-state-literal-at'. FIXME!!!
  1987. (defsubst c-state-pp-to-literal (from to &optional not-in-delimiter)
  1988. ;; Do a parse-partial-sexp from FROM to TO, returning either
  1989. ;; (STATE TYPE (BEG . END)) if TO is in a literal; or
  1990. ;; (STATE) otherwise,
  1991. ;; where STATE is the parsing state at TO, TYPE is the type of the literal
  1992. ;; (one of 'c, 'c++, 'string) and (BEG . END) is the boundaries of the literal.
  1993. ;;
  1994. ;; Unless NOT-IN-DELIMITER is non-nil, when TO is inside a two-character
  1995. ;; comment opener, this is recognized as being in a comment literal.
  1996. ;;
  1997. ;; Only elements 3 (in a string), 4 (in a comment), 5 (following a quote),
  1998. ;; 7 (comment type) and 8 (start of comment/string) (and possibly 9) of
  1999. ;; STATE are valid.
  2000. (save-excursion
  2001. (let ((s (parse-partial-sexp from to))
  2002. ty co-st)
  2003. (cond
  2004. ((or (nth 3 s) (nth 4 s)) ; in a string or comment
  2005. (setq ty (cond
  2006. ((nth 3 s) 'string)
  2007. ((nth 7 s) 'c++)
  2008. (t 'c)))
  2009. (parse-partial-sexp (point) (point-max)
  2010. nil ; TARGETDEPTH
  2011. nil ; STOPBEFORE
  2012. s ; OLDSTATE
  2013. 'syntax-table) ; stop at end of literal
  2014. `(,s ,ty (,(nth 8 s) . ,(point))))
  2015. ((and (not not-in-delimiter) ; inside a comment starter
  2016. (not (bobp))
  2017. (progn (backward-char)
  2018. (and (not (looking-at "\\s!"))
  2019. (looking-at c-comment-start-regexp))))
  2020. (setq ty (if (looking-at c-block-comment-start-regexp) 'c 'c++)
  2021. co-st (point))
  2022. (forward-comment 1)
  2023. `(,s ,ty (,co-st . ,(point))))
  2024. (t `(,s))))))
  2025. (defun c-state-safe-place (here)
  2026. ;; Return a buffer position before HERE which is "safe", i.e. outside any
  2027. ;; string, comment, or macro.
  2028. ;;
  2029. ;; NOTE: This function manipulates `c-state-nonlit-pos-cache'. This cache
  2030. ;; MAY NOT contain any positions within macros, since macros are frequently
  2031. ;; turned into comments by use of the `c-cpp-delimiter' category properties.
  2032. ;; We cannot rely on this mechanism whilst determining a cache pos since
  2033. ;; this function is also called from outwith `c-parse-state'.
  2034. (save-restriction
  2035. (widen)
  2036. (save-excursion
  2037. (let ((c c-state-nonlit-pos-cache)
  2038. pos npos high-pos lit macro-beg macro-end)
  2039. ;; Trim the cache to take account of buffer changes.
  2040. (while (and c (> (car c) c-state-nonlit-pos-cache-limit))
  2041. (setq c (cdr c)))
  2042. (setq c-state-nonlit-pos-cache c)
  2043. (while (and c (> (car c) here))
  2044. (setq high-pos (car c))
  2045. (setq c (cdr c)))
  2046. (setq pos (or (car c) (point-min)))
  2047. (unless high-pos
  2048. (while
  2049. ;; Add an element to `c-state-nonlit-pos-cache' each iteration.
  2050. (and
  2051. (setq npos
  2052. (when (<= (+ pos c-state-nonlit-pos-interval) here)
  2053. (+ pos c-state-nonlit-pos-interval)))
  2054. ;; Test for being in a literal. If so, go to after it.
  2055. (progn
  2056. (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
  2057. (or (null lit)
  2058. (prog1 (<= (cdr lit) here)
  2059. (setq npos (cdr lit)))))
  2060. ;; Test for being in a macro. If so, go to after it.
  2061. (progn
  2062. (goto-char npos)
  2063. (setq macro-beg
  2064. (and (c-beginning-of-macro) (/= (point) npos) (point)))
  2065. (when macro-beg
  2066. (c-syntactic-end-of-macro)
  2067. (or (eobp) (forward-char))
  2068. (setq macro-end (point)))
  2069. (or (null macro-beg)
  2070. (prog1 (<= macro-end here)
  2071. (setq npos macro-end)))))
  2072. (setq pos npos)
  2073. (setq c-state-nonlit-pos-cache (cons pos c-state-nonlit-pos-cache)))
  2074. ;; Add one extra element above HERE so as to to avoid the previous
  2075. ;; expensive calculation when the next call is close to the current
  2076. ;; one. This is especially useful when inside a large macro.
  2077. (when npos
  2078. (setq c-state-nonlit-pos-cache
  2079. (cons npos c-state-nonlit-pos-cache))))
  2080. (if (> pos c-state-nonlit-pos-cache-limit)
  2081. (setq c-state-nonlit-pos-cache-limit pos))
  2082. pos))))
  2083. (defun c-state-semi-safe-place (here)
  2084. ;; Return a buffer position before HERE which is "safe", i.e. outside any
  2085. ;; string or comment. It may be in a macro.
  2086. (save-restriction
  2087. (widen)
  2088. (save-excursion
  2089. (let ((c c-state-semi-nonlit-pos-cache)
  2090. pos npos high-pos lit macro-beg macro-end)
  2091. ;; Trim the cache to take account of buffer changes.
  2092. (while (and c (> (car c) c-state-semi-nonlit-pos-cache-limit))
  2093. (setq c (cdr c)))
  2094. (setq c-state-semi-nonlit-pos-cache c)
  2095. (while (and c (> (car c) here))
  2096. (setq high-pos (car c))
  2097. (setq c (cdr c)))
  2098. (setq pos (or (car c) (point-min)))
  2099. (unless high-pos
  2100. (while
  2101. ;; Add an element to `c-state-semi-nonlit-pos-cache' each iteration.
  2102. (and
  2103. (<= (setq npos (+ pos c-state-nonlit-pos-interval)) here)
  2104. ;; Test for being in a literal. If so, go to after it.
  2105. (progn
  2106. (setq lit (car (cddr (c-state-pp-to-literal pos npos))))
  2107. (or (null lit)
  2108. (prog1 (<= (cdr lit) here)
  2109. (setq npos (cdr lit))))))
  2110. (setq pos npos)
  2111. (setq c-state-semi-nonlit-pos-cache
  2112. (cons pos c-state-semi-nonlit-pos-cache))))
  2113. (if (> pos c-state-semi-nonlit-pos-cache-limit)
  2114. (setq c-state-semi-nonlit-pos-cache-limit pos))
  2115. pos))))
  2116. (defun c-state-literal-at (here)
  2117. ;; If position HERE is inside a literal, return (START . END), the
  2118. ;; boundaries of the literal (which may be outside the accessible bit of the
  2119. ;; buffer). Otherwise, return nil.
  2120. ;;
  2121. ;; This function is almost the same as `c-literal-limits'. Previously, it
  2122. ;; differed in that it was a lower level function, and that it rigorously
  2123. ;; followed the syntax from BOB. `c-literal-limits' is now (2011-12)
  2124. ;; virtually identical to this function.
  2125. (save-restriction
  2126. (widen)
  2127. (save-excursion
  2128. (let ((pos (c-state-safe-place here)))
  2129. (car (cddr (c-state-pp-to-literal pos here)))))))
  2130. (defsubst c-state-lit-beg (pos)
  2131. ;; Return the start of the literal containing POS, or POS itself.
  2132. (or (car (c-state-literal-at pos))
  2133. pos))
  2134. (defsubst c-state-cache-non-literal-place (pos state)
  2135. ;; Return a position outside of a string/comment/macro at or before POS.
  2136. ;; STATE is the parse-partial-sexp state at POS.
  2137. (let ((res (if (or (nth 3 state) ; in a string?
  2138. (nth 4 state)) ; in a comment?
  2139. (nth 8 state)
  2140. pos)))
  2141. (save-excursion
  2142. (goto-char res)
  2143. (if (c-beginning-of-macro)
  2144. (point)
  2145. res))))
  2146. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2147. ;; Stuff to do with point-min, and coping with any literal there.
  2148. (defvar c-state-point-min 1)
  2149. (make-variable-buffer-local 'c-state-point-min)
  2150. ;; This is (point-min) when `c-state-cache' was last calculated. A change of
  2151. ;; narrowing is likely to affect the parens that are visible before the point.
  2152. (defvar c-state-point-min-lit-type nil)
  2153. (make-variable-buffer-local 'c-state-point-min-lit-type)
  2154. (defvar c-state-point-min-lit-start nil)
  2155. (make-variable-buffer-local 'c-state-point-min-lit-start)
  2156. ;; These two variables define the literal, if any, containing point-min.
  2157. ;; Their values are, respectively, 'string, c, or c++, and the start of the
  2158. ;; literal. If there's no literal there, they're both nil.
  2159. (defvar c-state-min-scan-pos 1)
  2160. (make-variable-buffer-local 'c-state-min-scan-pos)
  2161. ;; This is the earliest buffer-pos from which scanning can be done. It is
  2162. ;; either the end of the literal containing point-min, or point-min itself.
  2163. ;; It becomes nil if the buffer is changed earlier than this point.
  2164. (defun c-state-get-min-scan-pos ()
  2165. ;; Return the lowest valid scanning pos. This will be the end of the
  2166. ;; literal enclosing point-min, or point-min itself.
  2167. (or c-state-min-scan-pos
  2168. (save-restriction
  2169. (save-excursion
  2170. (widen)
  2171. (goto-char c-state-point-min-lit-start)
  2172. (if (eq c-state-point-min-lit-type 'string)
  2173. (forward-sexp)
  2174. (forward-comment 1))
  2175. (setq c-state-min-scan-pos (point))))))
  2176. (defun c-state-mark-point-min-literal ()
  2177. ;; Determine the properties of any literal containing POINT-MIN, setting the
  2178. ;; variables `c-state-point-min-lit-type', `c-state-point-min-lit-start',
  2179. ;; and `c-state-min-scan-pos' accordingly. The return value is meaningless.
  2180. (let ((p-min (point-min))
  2181. lit)
  2182. (save-restriction
  2183. (widen)
  2184. (setq lit (c-state-literal-at p-min))
  2185. (if lit
  2186. (setq c-state-point-min-lit-type
  2187. (save-excursion
  2188. (goto-char (car lit))
  2189. (cond
  2190. ((looking-at c-block-comment-start-regexp) 'c)
  2191. ((looking-at c-line-comment-starter) 'c++)
  2192. (t 'string)))
  2193. c-state-point-min-lit-start (car lit)
  2194. c-state-min-scan-pos (cdr lit))
  2195. (setq c-state-point-min-lit-type nil
  2196. c-state-point-min-lit-start nil
  2197. c-state-min-scan-pos p-min)))))
  2198. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2199. ;; A variable which signals a brace dessert - helpful for reducing the number
  2200. ;; of fruitless backward scans.
  2201. (defvar c-state-brace-pair-desert nil)
  2202. (make-variable-buffer-local 'c-state-brace-pair-desert)
  2203. ;; Used only in `c-append-lower-brace-pair-to-state-cache'. It is set when
  2204. ;; that defun has searched backwards for a brace pair and not found one. Its
  2205. ;; value is either nil or a cons (PA . FROM), where PA is the position of the
  2206. ;; enclosing opening paren/brace/bracket which bounds the backwards search (or
  2207. ;; nil when at top level) and FROM is where the backward search started. It
  2208. ;; is reset to nil in `c-invalidate-state-cache'.
  2209. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2210. ;; Lowish level functions/macros which work directly on `c-state-cache', or a
  2211. ;; list of like structure.
  2212. (defmacro c-state-cache-top-lparen (&optional cache)
  2213. ;; Return the address of the top left brace/bracket/paren recorded in CACHE
  2214. ;; (default `c-state-cache') (or nil).
  2215. (let ((cash (or cache 'c-state-cache)))
  2216. `(if (consp (car ,cash))
  2217. (caar ,cash)
  2218. (car ,cash))))
  2219. (defmacro c-state-cache-top-paren (&optional cache)
  2220. ;; Return the address of the latest brace/bracket/paren (whether left or
  2221. ;; right) recorded in CACHE (default `c-state-cache') or nil.
  2222. (let ((cash (or cache 'c-state-cache)))
  2223. `(if (consp (car ,cash))
  2224. (cdar ,cash)
  2225. (car ,cash))))
  2226. (defmacro c-state-cache-after-top-paren (&optional cache)
  2227. ;; Return the position just after the latest brace/bracket/paren (whether
  2228. ;; left or right) recorded in CACHE (default `c-state-cache') or nil.
  2229. (let ((cash (or cache 'c-state-cache)))
  2230. `(if (consp (car ,cash))
  2231. (cdar ,cash)
  2232. (and (car ,cash)
  2233. (1+ (car ,cash))))))
  2234. (defun c-get-cache-scan-pos (here)
  2235. ;; From the state-cache, determine the buffer position from which we might
  2236. ;; scan forward to HERE to update this cache. This position will be just
  2237. ;; after a paren/brace/bracket recorded in the cache, if possible, otherwise
  2238. ;; return the earliest position in the accessible region which isn't within
  2239. ;; a literal. If the visible portion of the buffer is entirely within a
  2240. ;; literal, return NIL.
  2241. (let ((c c-state-cache) elt)
  2242. ;(while (>= (or (c-state-cache-top-lparen c) 1) here)
  2243. (while (and c
  2244. (>= (c-state-cache-top-lparen c) here))
  2245. (setq c (cdr c)))
  2246. (setq elt (car c))
  2247. (cond
  2248. ((consp elt)
  2249. (if (> (cdr elt) here)
  2250. (1+ (car elt))
  2251. (cdr elt)))
  2252. (elt (1+ elt))
  2253. ((<= (c-state-get-min-scan-pos) here)
  2254. (c-state-get-min-scan-pos))
  2255. (t nil))))
  2256. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2257. ;; Variables which keep track of preprocessor constructs.
  2258. (defvar c-state-old-cpp-beg-marker nil)
  2259. (make-variable-buffer-local 'c-state-old-cpp-beg-marker)
  2260. (defvar c-state-old-cpp-beg nil)
  2261. (make-variable-buffer-local 'c-state-old-cpp-beg)
  2262. (defvar c-state-old-cpp-end-marker nil)
  2263. (make-variable-buffer-local 'c-state-old-cpp-end-marker)
  2264. (defvar c-state-old-cpp-end nil)
  2265. (make-variable-buffer-local 'c-state-old-cpp-end)
  2266. ;; These are the limits of the macro containing point at the previous call of
  2267. ;; `c-parse-state', or nil.
  2268. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2269. ;; Defuns which analyze the buffer, yet don't change `c-state-cache'.
  2270. (defun c-state-balance-parens-backwards (here- here+ top)
  2271. ;; Return the position of the opening paren/brace/bracket before HERE- which
  2272. ;; matches the outermost close p/b/b between HERE+ and TOP. Except when
  2273. ;; there's a macro, HERE- and HERE+ are the same. Like this:
  2274. ;;
  2275. ;; ............................................
  2276. ;; | |
  2277. ;; ( [ ( .........#macro.. ) ( ) ] )
  2278. ;; ^ ^ ^ ^
  2279. ;; | | | |
  2280. ;; return HERE- HERE+ TOP
  2281. ;;
  2282. ;; If there aren't enough opening paren/brace/brackets, return the position
  2283. ;; of the outermost one found, or HERE- if there are none. If there are no
  2284. ;; closing p/b/bs between HERE+ and TOP, return HERE-. HERE-/+ and TOP
  2285. ;; must not be inside literals. Only the accessible portion of the buffer
  2286. ;; will be scanned.
  2287. ;; PART 1: scan from `here+' up to `top', accumulating ")"s which enclose
  2288. ;; `here'. Go round the next loop each time we pass over such a ")". These
  2289. ;; probably match "("s before `here-'.
  2290. (let (pos pa ren+1 lonely-rens)
  2291. (save-excursion
  2292. (save-restriction
  2293. (narrow-to-region (point-min) top) ; This can move point, sometimes.
  2294. (setq pos here+)
  2295. (c-safe
  2296. (while
  2297. (setq ren+1 (c-sc-scan-lists pos 1 1)) ; might signal
  2298. (setq lonely-rens (cons ren+1 lonely-rens)
  2299. pos ren+1)))))
  2300. ;; PART 2: Scan back before `here-' searching for the "("s
  2301. ;; matching/mismatching the ")"s found above. We only need to direct the
  2302. ;; caller to scan when we've encountered unmatched right parens.
  2303. (setq pos here-)
  2304. (when lonely-rens
  2305. (c-safe
  2306. (while
  2307. (and lonely-rens ; actual values aren't used.
  2308. (setq pa (c-sc-scan-lists pos -1 1)))
  2309. (setq pos pa)
  2310. (setq lonely-rens (cdr lonely-rens)))))
  2311. pos))
  2312. (defun c-parse-state-get-strategy (here good-pos)
  2313. ;; Determine the scanning strategy for adjusting `c-parse-state', attempting
  2314. ;; to minimize the amount of scanning. HERE is the pertinent position in
  2315. ;; the buffer, GOOD-POS is a position where `c-state-cache' (possibly with
  2316. ;; its head trimmed) is known to be good, or nil if there is no such
  2317. ;; position.
  2318. ;;
  2319. ;; The return value is a list, one of the following:
  2320. ;;
  2321. ;; o - ('forward START-POINT) - scan forward from START-POINT,
  2322. ;; which is not less than the highest position in `c-state-cache' below HERE,
  2323. ;; which is after GOOD-POS.
  2324. ;; o - ('backward nil) - scan backwards (from HERE).
  2325. ;; o - ('back-and-forward START-POINT) - like 'forward, but when HERE is earlier
  2326. ;; than GOOD-POS.
  2327. ;; o - ('IN-LIT nil) - point is inside the literal containing point-min.
  2328. (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
  2329. strategy ; 'forward, 'backward, or 'IN-LIT.
  2330. start-point)
  2331. (setq good-pos (or good-pos (c-state-get-min-scan-pos)))
  2332. (cond
  2333. ((< here (c-state-get-min-scan-pos))
  2334. (setq strategy 'IN-LIT))
  2335. ((<= good-pos here)
  2336. (setq strategy 'forward
  2337. start-point (max good-pos cache-pos)))
  2338. ((< (- good-pos here) (- here cache-pos)) ; FIXME!!! ; apply some sort of weighting.
  2339. (setq strategy 'backward))
  2340. (t
  2341. (setq strategy 'back-and-forward
  2342. start-point cache-pos)))
  2343. (list strategy start-point)))
  2344. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2345. ;; Routines which change `c-state-cache' and associated values.
  2346. (defun c-renarrow-state-cache ()
  2347. ;; The region (more precisely, point-min) has changed since we
  2348. ;; calculated `c-state-cache'. Amend `c-state-cache' accordingly.
  2349. (if (< (point-min) c-state-point-min)
  2350. ;; If point-min has MOVED BACKWARDS then we drop the state completely.
  2351. ;; It would be possible to do a better job here and recalculate the top
  2352. ;; only.
  2353. (progn
  2354. (c-state-mark-point-min-literal)
  2355. (setq c-state-cache nil
  2356. c-state-cache-good-pos c-state-min-scan-pos
  2357. c-state-brace-pair-desert nil))
  2358. ;; point-min has MOVED FORWARD.
  2359. ;; Is the new point-min inside a (different) literal?
  2360. (unless (and c-state-point-min-lit-start ; at prev. point-min
  2361. (< (point-min) (c-state-get-min-scan-pos)))
  2362. (c-state-mark-point-min-literal))
  2363. ;; Cut off a bit of the tail from `c-state-cache'.
  2364. (let ((ptr (cons nil c-state-cache))
  2365. pa)
  2366. (while (and (setq pa (c-state-cache-top-lparen (cdr ptr)))
  2367. (>= pa (point-min)))
  2368. (setq ptr (cdr ptr)))
  2369. (when (consp ptr)
  2370. (if (eq (cdr ptr) c-state-cache)
  2371. (setq c-state-cache nil
  2372. c-state-cache-good-pos c-state-min-scan-pos)
  2373. (setcdr ptr nil)
  2374. (setq c-state-cache-good-pos (1+ (c-state-cache-top-lparen))))
  2375. )))
  2376. (setq c-state-point-min (point-min)))
  2377. (defun c-append-lower-brace-pair-to-state-cache (from here &optional upper-lim)
  2378. ;; If there is a brace pair preceding FROM in the buffer, at the same level
  2379. ;; of nesting (not necessarily immediately preceding), push a cons onto
  2380. ;; `c-state-cache' to represent it. FROM must not be inside a literal. If
  2381. ;; UPPER-LIM is non-nil, we append the highest brace pair whose "}" is below
  2382. ;; UPPER-LIM.
  2383. ;;
  2384. ;; Return non-nil when this has been done.
  2385. ;;
  2386. ;; The situation it copes with is this transformation:
  2387. ;;
  2388. ;; OLD: { (.) {...........}
  2389. ;; ^ ^
  2390. ;; FROM HERE
  2391. ;;
  2392. ;; NEW: { {....} (.) {.........
  2393. ;; ^ ^ ^
  2394. ;; LOWER BRACE PAIR HERE or HERE
  2395. ;;
  2396. ;; This routine should be fast. Since it can get called a LOT, we maintain
  2397. ;; `c-state-brace-pair-desert', a small cache of "failures", such that we
  2398. ;; reduce the time wasted in repeated fruitless searches in brace deserts.
  2399. (save-excursion
  2400. (save-restriction
  2401. (let* (new-cons
  2402. (cache-pos (c-state-cache-top-lparen)) ; might be nil.
  2403. (macro-start-or-from
  2404. (progn (goto-char from)
  2405. (c-beginning-of-macro)
  2406. (point)))
  2407. (bra ; Position of "{".
  2408. ;; Don't start scanning in the middle of a CPP construct unless
  2409. ;; it contains HERE - these constructs, in Emacs, are "commented
  2410. ;; out" with category properties.
  2411. (if (eq (c-get-char-property macro-start-or-from 'category)
  2412. 'c-cpp-delimiter)
  2413. macro-start-or-from
  2414. from))
  2415. ce) ; Position of "}"
  2416. (or upper-lim (setq upper-lim from))
  2417. ;; If we're essentially repeating a fruitless search, just give up.
  2418. (unless (and c-state-brace-pair-desert
  2419. (eq cache-pos (car c-state-brace-pair-desert))
  2420. (or (null (car c-state-brace-pair-desert))
  2421. (> from (car c-state-brace-pair-desert)))
  2422. (<= from (cdr c-state-brace-pair-desert)))
  2423. ;; DESERT-LIM. Avoid repeated searching through the cached desert.
  2424. (let ((desert-lim
  2425. (and c-state-brace-pair-desert
  2426. (eq cache-pos (car c-state-brace-pair-desert))
  2427. (>= from (cdr c-state-brace-pair-desert))
  2428. (cdr c-state-brace-pair-desert)))
  2429. ;; CACHE-LIM. This limit will be necessary when an opening
  2430. ;; paren at `cache-pos' has just had its matching close paren
  2431. ;; inserted into the buffer. `cache-pos' continues to be a
  2432. ;; search bound, even though the algorithm below would skip
  2433. ;; over the new paren pair.
  2434. (cache-lim (and cache-pos (< cache-pos from) cache-pos)))
  2435. (narrow-to-region
  2436. (cond
  2437. ((and desert-lim cache-lim)
  2438. (max desert-lim cache-lim))
  2439. (desert-lim)
  2440. (cache-lim)
  2441. ((point-min)))
  2442. ;; The top limit is EOB to ensure that `bra' is inside the
  2443. ;; accessible part of the buffer at the next scan operation.
  2444. (1+ (buffer-size))))
  2445. ;; In the next pair of nested loops, the inner one moves back past a
  2446. ;; pair of (mis-)matching parens or brackets; the outer one moves
  2447. ;; back over a sequence of unmatched close brace/paren/bracket each
  2448. ;; time round.
  2449. (while
  2450. (progn
  2451. (c-safe
  2452. (while
  2453. (and (setq ce (c-sc-scan-lists bra -1 -1)) ; back past )/]/}; might signal
  2454. (setq bra (c-sc-scan-lists ce -1 1)) ; back past (/[/{; might signal
  2455. (or (> bra here) ;(> ce here)
  2456. (and
  2457. (< ce here)
  2458. (or (not (eq (char-after bra) ?\{))
  2459. (and (goto-char bra)
  2460. (c-beginning-of-macro)
  2461. (< (point) macro-start-or-from))))))))
  2462. (and ce (< ce bra)))
  2463. (setq bra ce)) ; If we just backed over an unbalanced closing
  2464. ; brace, ignore it.
  2465. (if (and ce (< ce here) (< bra ce) (eq (char-after bra) ?\{))
  2466. ;; We've found the desired brace-pair.
  2467. (progn
  2468. (setq new-cons (cons bra (1+ ce)))
  2469. (cond
  2470. ((consp (car c-state-cache))
  2471. (setcar c-state-cache new-cons))
  2472. ((and (numberp (car c-state-cache)) ; probably never happens
  2473. (< ce (car c-state-cache)))
  2474. (setcdr c-state-cache
  2475. (cons new-cons (cdr c-state-cache))))
  2476. (t (setq c-state-cache (cons new-cons c-state-cache)))))
  2477. ;; We haven't found a brace pair. Record this in the cache.
  2478. (setq c-state-brace-pair-desert
  2479. (cons (if (and ce (< bra ce) (> ce here)) ; {..} straddling HERE?
  2480. bra
  2481. (point-min))
  2482. (min here from)))))))))
  2483. (defsubst c-state-push-any-brace-pair (bra+1 macro-start-or-here)
  2484. ;; If BRA+1 is nil, do nothing. Otherwise, BRA+1 is the buffer position
  2485. ;; following a {, and that brace has a (mis-)matching } (or ]), and we
  2486. ;; "push" "a" brace pair onto `c-state-cache'.
  2487. ;;
  2488. ;; Here "push" means overwrite the top element if it's itself a brace-pair,
  2489. ;; otherwise push it normally.
  2490. ;;
  2491. ;; The brace pair we push is normally the one surrounding BRA+1, but if the
  2492. ;; latter is inside a macro, not being a macro containing
  2493. ;; MACRO-START-OR-HERE, we scan backwards through the buffer for a non-macro
  2494. ;; base pair. This latter case is assumed to be rare.
  2495. ;;
  2496. ;; Note: POINT is not preserved in this routine.
  2497. (if bra+1
  2498. (if (or (> bra+1 macro-start-or-here)
  2499. (progn (goto-char bra+1)
  2500. (not (c-beginning-of-macro))))
  2501. (setq c-state-cache
  2502. (cons (cons (1- bra+1)
  2503. (c-sc-scan-lists bra+1 1 1))
  2504. (if (consp (car c-state-cache))
  2505. (cdr c-state-cache)
  2506. c-state-cache)))
  2507. ;; N.B. This defsubst codes one method for the simple, normal case,
  2508. ;; and a more sophisticated, slower way for the general case. Don't
  2509. ;; eliminate this defsubst - it's a speed optimization.
  2510. (c-append-lower-brace-pair-to-state-cache (1- bra+1) (point-max)))))
  2511. (defun c-append-to-state-cache (from here)
  2512. ;; Scan the buffer from FROM to HERE, adding elements into `c-state-cache'
  2513. ;; for braces etc. Return a candidate for `c-state-cache-good-pos'.
  2514. ;;
  2515. ;; FROM must be after the latest brace/paren/bracket in `c-state-cache', if
  2516. ;; any. Typically, it is immediately after it. It must not be inside a
  2517. ;; literal.
  2518. (let ((here-bol (c-point 'bol here))
  2519. (macro-start-or-here
  2520. (save-excursion (goto-char here)
  2521. (if (c-beginning-of-macro)
  2522. (point)
  2523. here)))
  2524. pa+1 ; pos just after an opening PAren (or brace).
  2525. (ren+1 from) ; usually a pos just after an closing paREN etc.
  2526. ; Is actually the pos. to scan for a (/{/[ from,
  2527. ; which sometimes is after a silly )/}/].
  2528. paren+1 ; Pos after some opening or closing paren.
  2529. paren+1s ; A list of `paren+1's; used to determine a
  2530. ; good-pos.
  2531. bra+1 ; just after L bra-ce.
  2532. bra+1s ; list of OLD values of bra+1.
  2533. mstart) ; start of a macro.
  2534. (save-excursion
  2535. (save-restriction
  2536. (narrow-to-region (point-min) here)
  2537. ;; Each time round the following loop, we enter a successively deeper
  2538. ;; level of brace/paren nesting. (Except sometimes we "continue at
  2539. ;; the existing level".) `pa+1' is a pos inside an opening
  2540. ;; brace/paren/bracket, usually just after it.
  2541. (while
  2542. (progn
  2543. ;; Each time round the next loop moves forward over an opening then
  2544. ;; a closing brace/bracket/paren. This loop is white hot, so it
  2545. ;; plays ugly tricks to go fast. DON'T PUT ANYTHING INTO THIS
  2546. ;; LOOP WHICH ISN'T ABSOLUTELY NECESSARY!!! It terminates when a
  2547. ;; call of `scan-lists' signals an error, which happens when there
  2548. ;; are no more b/b/p's to scan.
  2549. (c-safe
  2550. (while t
  2551. (setq pa+1 (c-sc-scan-lists ren+1 1 -1) ; Into (/{/[; might signal
  2552. paren+1s (cons pa+1 paren+1s))
  2553. (setq ren+1 (c-sc-scan-lists pa+1 1 1)) ; Out of )/}/]; might signal
  2554. (if (and (eq (char-before pa+1) ?{)) ; Check for a macro later.
  2555. (setq bra+1 pa+1))
  2556. (setcar paren+1s ren+1)))
  2557. (if (and pa+1 (> pa+1 ren+1))
  2558. ;; We've just entered a deeper nesting level.
  2559. (progn
  2560. ;; Insert the brace pair (if present) and the single open
  2561. ;; paren/brace/bracket into `c-state-cache' It cannot be
  2562. ;; inside a macro, except one around point, because of what
  2563. ;; `c-neutralize-syntax-in-CPP' has done.
  2564. (c-state-push-any-brace-pair bra+1 macro-start-or-here)
  2565. ;; Insert the opening brace/bracket/paren position.
  2566. (setq c-state-cache (cons (1- pa+1) c-state-cache))
  2567. ;; Clear admin stuff for the next more nested part of the scan.
  2568. (setq ren+1 pa+1 pa+1 nil bra+1 nil bra+1s nil)
  2569. t) ; Carry on the loop
  2570. ;; All open p/b/b's at this nesting level, if any, have probably
  2571. ;; been closed by matching/mismatching ones. We're probably
  2572. ;; finished - we just need to check for having found an
  2573. ;; unmatched )/}/], which we ignore. Such a )/}/] can't be in a
  2574. ;; macro, due the action of `c-neutralize-syntax-in-CPP'.
  2575. (c-safe (setq ren+1 (c-sc-scan-lists ren+1 1 1)))))) ; acts as loop control.
  2576. ;; Record the final, innermost, brace-pair if there is one.
  2577. (c-state-push-any-brace-pair bra+1 macro-start-or-here)
  2578. ;; Determine a good pos
  2579. (while (and (setq paren+1 (car paren+1s))
  2580. (> (if (> paren+1 macro-start-or-here)
  2581. paren+1
  2582. (goto-char paren+1)
  2583. (setq mstart (and (c-beginning-of-macro)
  2584. (point)))
  2585. (or mstart paren+1))
  2586. here-bol))
  2587. (setq paren+1s (cdr paren+1s)))
  2588. (cond
  2589. ((and paren+1 mstart)
  2590. (min paren+1 mstart))
  2591. (paren+1)
  2592. (t from))))))
  2593. (defun c-remove-stale-state-cache (start-point here pps-point)
  2594. ;; Remove stale entries from the `c-cache-state', i.e. those which will
  2595. ;; not be in it when it is amended for position HERE. This may involve
  2596. ;; replacing a CONS element for a brace pair containing HERE with its car.
  2597. ;; Additionally, the "outermost" open-brace entry before HERE will be
  2598. ;; converted to a cons if the matching close-brace is below HERE.
  2599. ;;
  2600. ;; START-POINT is a "maximal" "safe position" - there must be no open
  2601. ;; parens/braces/brackets between START-POINT and HERE.
  2602. ;;
  2603. ;; As a second thing, calculate the result of parse-partial-sexp at
  2604. ;; PPS-POINT, w.r.t. START-POINT. The motivation here is that
  2605. ;; `c-state-cache-good-pos' may become PPS-POINT, but the caller may need to
  2606. ;; adjust it to get outside a string/comment. (Sorry about this! The code
  2607. ;; needs to be FAST).
  2608. ;;
  2609. ;; Return a list (GOOD-POS SCAN-BACK-POS CONS-SEPARATED PPS-STATE), where
  2610. ;; o - GOOD-POS is a position where the new value `c-state-cache' is known
  2611. ;; to be good (we aim for this to be as high as possible);
  2612. ;; o - SCAN-BACK-POS, if not nil, indicates there may be a brace pair
  2613. ;; preceding POS which needs to be recorded in `c-state-cache'. It is a
  2614. ;; position to scan backwards from. It is the position of the "{" of the
  2615. ;; last element to be removed from `c-state-cache', when that elt is a
  2616. ;; cons, otherwise nil.
  2617. ;; o - CONS-SEPARATED is t when a cons element in `c-state-cache' has been
  2618. ;; replaced by its car because HERE lies inside the brace pair represented
  2619. ;; by the cons.
  2620. ;; o - PPS-STATE is the parse-partial-sexp state at PPS-POINT.
  2621. (save-excursion
  2622. (save-restriction
  2623. (narrow-to-region 1 (point-max))
  2624. (let* ((in-macro-start ; start of macro containing HERE or nil.
  2625. (save-excursion
  2626. (goto-char here)
  2627. (and (c-beginning-of-macro)
  2628. (point))))
  2629. (start-point-actual-macro-start ; Start of macro containing
  2630. ; start-point or nil
  2631. (and (< start-point here)
  2632. (save-excursion
  2633. (goto-char start-point)
  2634. (and (c-beginning-of-macro)
  2635. (point)))))
  2636. (start-point-actual-macro-end ; End of this macro, (maybe
  2637. ; HERE), or nil.
  2638. (and start-point-actual-macro-start
  2639. (save-excursion
  2640. (goto-char start-point-actual-macro-start)
  2641. (c-end-of-macro)
  2642. (point))))
  2643. pps-state ; Will be 9 or 10 elements long.
  2644. pos
  2645. upper-lim ; ,beyond which `c-state-cache' entries are removed
  2646. scan-back-pos
  2647. cons-separated
  2648. pair-beg pps-point-state target-depth)
  2649. ;; Remove entries beyond HERE. Also remove any entries inside
  2650. ;; a macro, unless HERE is in the same macro.
  2651. (setq upper-lim
  2652. (if (or (null c-state-old-cpp-beg)
  2653. (and (> here c-state-old-cpp-beg)
  2654. (< here c-state-old-cpp-end)))
  2655. here
  2656. (min here c-state-old-cpp-beg)))
  2657. (while (and c-state-cache (>= (c-state-cache-top-lparen) upper-lim))
  2658. (setq scan-back-pos (car-safe (car c-state-cache)))
  2659. (setq c-state-cache (cdr c-state-cache)))
  2660. ;; If `upper-lim' is inside the last recorded brace pair, remove its
  2661. ;; RBrace and indicate we'll need to search backwards for a previous
  2662. ;; brace pair.
  2663. (when (and c-state-cache
  2664. (consp (car c-state-cache))
  2665. (> (cdar c-state-cache) upper-lim))
  2666. (setcar c-state-cache (caar c-state-cache))
  2667. (setq scan-back-pos (car c-state-cache)
  2668. cons-separated t))
  2669. ;; The next loop jumps forward out of a nested level of parens each
  2670. ;; time round; the corresponding elements in `c-state-cache' are
  2671. ;; removed. `pos' is just after the brace-pair or the open paren at
  2672. ;; (car c-state-cache). There can be no open parens/braces/brackets
  2673. ;; between `start-point'/`start-point-actual-macro-start' and HERE,
  2674. ;; due to the interface spec to this function.
  2675. (setq pos (if (and start-point-actual-macro-end
  2676. (not (eq start-point-actual-macro-start
  2677. in-macro-start)))
  2678. (1+ start-point-actual-macro-end) ; get outside the macro as
  2679. ; marked by a `category' text property.
  2680. start-point))
  2681. (goto-char pos)
  2682. (while (and c-state-cache
  2683. (or (numberp (car c-state-cache)) ; Have we a { at all?
  2684. (cdr c-state-cache))
  2685. (< (point) here))
  2686. (cond
  2687. ((null pps-state) ; first time through
  2688. (setq target-depth -1))
  2689. ((eq (car pps-state) target-depth) ; found closing ),},]
  2690. (setq target-depth (1- (car pps-state))))
  2691. ;; Do nothing when we've merely reached pps-point.
  2692. )
  2693. ;; Scan!
  2694. (setq pps-state
  2695. (c-sc-parse-partial-sexp
  2696. (point) (if (< (point) pps-point) pps-point here)
  2697. target-depth
  2698. nil pps-state))
  2699. (if (= (point) pps-point)
  2700. (setq pps-point-state pps-state))
  2701. (when (eq (car pps-state) target-depth)
  2702. (setq pos (point)) ; POS is now just after an R-paren/brace.
  2703. (cond
  2704. ((and (consp (car c-state-cache))
  2705. (eq (point) (cdar c-state-cache)))
  2706. ;; We've just moved out of the paren pair containing the brace-pair
  2707. ;; at (car c-state-cache). `pair-beg' is where the open paren is,
  2708. ;; and is potentially where the open brace of a cons in
  2709. ;; c-state-cache will be.
  2710. (setq pair-beg (car-safe (cdr c-state-cache))
  2711. c-state-cache (cdr-safe (cdr c-state-cache)))) ; remove {}pair + containing Lparen.
  2712. ((numberp (car c-state-cache))
  2713. (setq pair-beg (car c-state-cache)
  2714. c-state-cache (cdr c-state-cache))) ; remove this
  2715. ; containing Lparen
  2716. ((numberp (cadr c-state-cache))
  2717. (setq pair-beg (cadr c-state-cache)
  2718. c-state-cache (cddr c-state-cache))) ; Remove a paren pair
  2719. ; together with enclosed brace pair.
  2720. ;; (t nil) ; Ignore an unmated Rparen.
  2721. )))
  2722. (if (< (point) pps-point)
  2723. (setq pps-state (c-sc-parse-partial-sexp
  2724. (point) pps-point
  2725. nil nil ; TARGETDEPTH, STOPBEFORE
  2726. pps-state)))
  2727. ;; If the last paren pair we moved out of was actually a brace pair,
  2728. ;; insert it into `c-state-cache'.
  2729. (when (and pair-beg (eq (char-after pair-beg) ?{))
  2730. (if (consp (car-safe c-state-cache))
  2731. (setq c-state-cache (cdr c-state-cache)))
  2732. (setq c-state-cache (cons (cons pair-beg pos)
  2733. c-state-cache)))
  2734. (list pos scan-back-pos cons-separated pps-state)))))
  2735. (defun c-remove-stale-state-cache-backwards (here)
  2736. ;; Strip stale elements of `c-state-cache' by moving backwards through the
  2737. ;; buffer, and inform the caller of the scenario detected.
  2738. ;;
  2739. ;; HERE is the position we're setting `c-state-cache' for.
  2740. ;; CACHE-POS (a locally bound variable) is just after the latest recorded
  2741. ;; position in `c-state-cache' before HERE, or a position at or near
  2742. ;; point-min which isn't in a literal.
  2743. ;;
  2744. ;; This function must only be called only when (> `c-state-cache-good-pos'
  2745. ;; HERE). Usually the gap between CACHE-POS and HERE is large. It is thus
  2746. ;; optimized to eliminate (or minimize) scanning between these two
  2747. ;; positions.
  2748. ;;
  2749. ;; Return a three element list (GOOD-POS SCAN-BACK-POS FWD-FLAG), where:
  2750. ;; o - GOOD-POS is a "good position", where `c-state-cache' is valid, or
  2751. ;; could become so after missing elements are inserted into
  2752. ;; `c-state-cache'. This is JUST AFTER an opening or closing
  2753. ;; brace/paren/bracket which is already in `c-state-cache' or just before
  2754. ;; one otherwise. exceptionally (when there's no such b/p/b handy) the BOL
  2755. ;; before `here''s line, or the start of the literal containing it.
  2756. ;; o - SCAN-BACK-POS, if non-nil, indicates there may be a brace pair
  2757. ;; preceding POS which isn't recorded in `c-state-cache'. It is a position
  2758. ;; to scan backwards from.
  2759. ;; o - FWD-FLAG, if non-nil, indicates there may be parens/braces between
  2760. ;; POS and HERE which aren't recorded in `c-state-cache'.
  2761. ;;
  2762. ;; The comments in this defun use "paren" to mean parenthesis or square
  2763. ;; bracket (as contrasted with a brace), and "(" and ")" likewise.
  2764. ;;
  2765. ;; . {..} (..) (..) ( .. { } ) (...) ( .... . ..)
  2766. ;; | | | | | |
  2767. ;; CP E here D C good
  2768. (let ((cache-pos (c-get-cache-scan-pos here)) ; highest position below HERE in cache (or 1)
  2769. (pos c-state-cache-good-pos)
  2770. pa ren ; positions of "(" and ")"
  2771. dropped-cons ; whether the last element dropped from `c-state-cache'
  2772. ; was a cons (representing a brace-pair)
  2773. good-pos ; see above.
  2774. lit ; (START . END) of a literal containing some point.
  2775. here-lit-start here-lit-end ; bounds of literal containing `here'
  2776. ; or `here' itself.
  2777. here- here+ ; start/end of macro around HERE, or HERE
  2778. (here-bol (c-point 'bol here))
  2779. (too-far-back (max (- here c-state-cache-too-far) (point-min))))
  2780. ;; Remove completely irrelevant entries from `c-state-cache'.
  2781. (while (and c-state-cache
  2782. (>= (setq pa (c-state-cache-top-lparen)) here))
  2783. (setq dropped-cons (consp (car c-state-cache)))
  2784. (setq c-state-cache (cdr c-state-cache))
  2785. (setq pos pa))
  2786. ;; At this stage, (>= pos here);
  2787. ;; (< (c-state-cache-top-lparen) here) (or is nil).
  2788. (cond
  2789. ((and (consp (car c-state-cache))
  2790. (> (cdar c-state-cache) here))
  2791. ;; CASE 1: The top of the cache is a brace pair which now encloses
  2792. ;; `here'. As good-pos, return the address. of the "{". Since we've no
  2793. ;; knowledge of what's inside these braces, we have no alternative but
  2794. ;; to direct the caller to scan the buffer from the opening brace.
  2795. (setq pos (caar c-state-cache))
  2796. (setcar c-state-cache pos)
  2797. (list (1+ pos) pos t)) ; return value. We've just converted a brace pair
  2798. ; entry into a { entry, so the caller needs to
  2799. ; search for a brace pair before the {.
  2800. ;; `here' might be inside a literal. Check for this.
  2801. ((progn
  2802. (setq lit (c-state-literal-at here)
  2803. here-lit-start (or (car lit) here)
  2804. here-lit-end (or (cdr lit) here))
  2805. ;; Has `here' just "newly entered" a macro?
  2806. (save-excursion
  2807. (goto-char here-lit-start)
  2808. (if (and (c-beginning-of-macro)
  2809. (or (null c-state-old-cpp-beg)
  2810. (not (= (point) c-state-old-cpp-beg))))
  2811. (progn
  2812. (setq here- (point))
  2813. (c-end-of-macro)
  2814. (setq here+ (point)))
  2815. (setq here- here-lit-start
  2816. here+ here-lit-end)))
  2817. ;; `here' might be nested inside any depth of parens (or brackets but
  2818. ;; not braces). Scan backwards to find the outermost such opening
  2819. ;; paren, if there is one. This will be the scan position to return.
  2820. (save-restriction
  2821. (narrow-to-region cache-pos (point-max))
  2822. (setq pos (c-state-balance-parens-backwards here- here+ pos)))
  2823. nil)) ; for the cond
  2824. ((< pos here-lit-start)
  2825. ;; CASE 2: Address of outermost ( or [ which now encloses `here', but
  2826. ;; didn't enclose the (previous) `c-state-cache-good-pos'. If there is
  2827. ;; a brace pair preceding this, it will already be in `c-state-cache',
  2828. ;; unless there was a brace pair after it, i.e. there'll only be one to
  2829. ;; scan for if we've just deleted one.
  2830. (list pos (and dropped-cons pos) t)) ; Return value.
  2831. ;; `here' isn't enclosed in a (previously unrecorded) bracket/paren.
  2832. ;; Further forward scanning isn't needed, but we still need to find a
  2833. ;; GOOD-POS. Step out of all enclosing "("s on HERE's line.
  2834. ((progn
  2835. (save-restriction
  2836. (narrow-to-region here-bol (point-max))
  2837. (setq pos here-lit-start)
  2838. (c-safe (while (setq pa (c-sc-scan-lists pos -1 1))
  2839. (setq pos pa)))) ; might signal
  2840. nil)) ; for the cond
  2841. ((save-restriction
  2842. (narrow-to-region too-far-back (point-max))
  2843. (setq ren (c-safe (c-sc-scan-lists pos -1 -1))))
  2844. ;; CASE 3: After a }/)/] before `here''s BOL.
  2845. (list (1+ ren) (and dropped-cons pos) nil)) ; Return value
  2846. ((progn (setq good-pos (c-state-lit-beg (c-point 'bopl here-bol)))
  2847. (>= cache-pos good-pos))
  2848. ;; CASE 3.5: Just after an existing entry in `c-state-cache' on `here''s
  2849. ;; line or the previous line.
  2850. (list cache-pos nil nil))
  2851. (t
  2852. ;; CASE 4; Best of a bad job: BOL before `here-bol', or beginning of
  2853. ;; literal containing it.
  2854. (list good-pos (and dropped-cons good-pos) nil)))))
  2855. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2856. ;; Externally visible routines.
  2857. (defun c-state-cache-init ()
  2858. (setq c-state-cache nil
  2859. c-state-cache-good-pos 1
  2860. c-state-nonlit-pos-cache nil
  2861. c-state-nonlit-pos-cache-limit 1
  2862. c-state-semi-nonlit-pos-cache nil
  2863. c-state-semi-nonlit-pos-cache-limit 1
  2864. c-state-brace-pair-desert nil
  2865. c-state-point-min 1
  2866. c-state-point-min-lit-type nil
  2867. c-state-point-min-lit-start nil
  2868. c-state-min-scan-pos 1
  2869. c-state-old-cpp-beg nil
  2870. c-state-old-cpp-end nil)
  2871. (c-state-mark-point-min-literal))
  2872. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2873. ;; Debugging routines to dump `c-state-cache' in a "replayable" form.
  2874. ;; (defmacro c-sc-de (elt) ; "c-state-cache-dump-element"
  2875. ;; `(format ,(concat "(setq " (symbol-name elt) " %s) ") ,elt))
  2876. ;; (defmacro c-sc-qde (elt) ; "c-state-cache-quote-dump-element"
  2877. ;; `(format ,(concat "(setq " (symbol-name elt) " '%s) ") ,elt))
  2878. ;; (defun c-state-dump ()
  2879. ;; ;; For debugging.
  2880. ;; ;(message
  2881. ;; (concat
  2882. ;; (c-sc-qde c-state-cache)
  2883. ;; (c-sc-de c-state-cache-good-pos)
  2884. ;; (c-sc-qde c-state-nonlit-pos-cache)
  2885. ;; (c-sc-de c-state-nonlit-pos-cache-limit)
  2886. ;; (c-sc-qde c-state-brace-pair-desert)
  2887. ;; (c-sc-de c-state-point-min)
  2888. ;; (c-sc-de c-state-point-min-lit-type)
  2889. ;; (c-sc-de c-state-point-min-lit-start)
  2890. ;; (c-sc-de c-state-min-scan-pos)
  2891. ;; (c-sc-de c-state-old-cpp-beg)
  2892. ;; (c-sc-de c-state-old-cpp-end)))
  2893. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2894. (defun c-invalidate-state-cache-1 (here)
  2895. ;; Invalidate all info on `c-state-cache' that applies to the buffer at HERE
  2896. ;; or higher and set `c-state-cache-good-pos' accordingly. The cache is
  2897. ;; left in a consistent state.
  2898. ;;
  2899. ;; This is much like `c-whack-state-after', but it never changes a paren
  2900. ;; pair element into an open paren element. Doing that would mean that the
  2901. ;; new open paren wouldn't have the required preceding paren pair element.
  2902. ;;
  2903. ;; This function is called from c-after-change.
  2904. ;; The caches of non-literals:
  2905. ;; Note that we use "<=" for the possibility of the second char of a two-char
  2906. ;; comment opener being typed; this would invalidate any cache position at
  2907. ;; HERE.
  2908. (if (<= here c-state-nonlit-pos-cache-limit)
  2909. (setq c-state-nonlit-pos-cache-limit (1- here)))
  2910. (if (<= here c-state-semi-nonlit-pos-cache-limit)
  2911. (setq c-state-semi-nonlit-pos-cache-limit (1- here)))
  2912. ;; `c-state-cache':
  2913. ;; Case 1: if `here' is in a literal containing point-min, everything
  2914. ;; becomes (or is already) nil.
  2915. (if (or (null c-state-cache-good-pos)
  2916. (< here (c-state-get-min-scan-pos)))
  2917. (setq c-state-cache nil
  2918. c-state-cache-good-pos nil
  2919. c-state-min-scan-pos nil)
  2920. ;; Truncate `c-state-cache' and set `c-state-cache-good-pos' to a value
  2921. ;; below `here'. To maintain its consistency, we may need to insert a new
  2922. ;; brace pair.
  2923. (let (open-paren-in-column-0-is-defun-start
  2924. (here-bol (c-point 'bol here))
  2925. too-high-pa ; recorded {/(/[ next above here, or nil.
  2926. dropped-cons ; was the last removed element a brace pair?
  2927. pa)
  2928. ;; The easy bit - knock over-the-top bits off `c-state-cache'.
  2929. (while (and c-state-cache
  2930. (>= (setq pa (c-state-cache-top-paren)) here))
  2931. (setq dropped-cons (consp (car c-state-cache))
  2932. too-high-pa (c-state-cache-top-lparen)
  2933. c-state-cache (cdr c-state-cache)))
  2934. ;; Do we need to add in an earlier brace pair, having lopped one off?
  2935. (if (and dropped-cons
  2936. (< too-high-pa (+ here c-state-cache-too-far)))
  2937. (c-append-lower-brace-pair-to-state-cache too-high-pa here here-bol))
  2938. (setq c-state-cache-good-pos (or (c-state-cache-after-top-paren)
  2939. (c-state-get-min-scan-pos)))))
  2940. ;; The brace-pair desert marker:
  2941. (when (car c-state-brace-pair-desert)
  2942. (if (< here (car c-state-brace-pair-desert))
  2943. (setq c-state-brace-pair-desert nil)
  2944. (if (< here (cdr c-state-brace-pair-desert))
  2945. (setcdr c-state-brace-pair-desert here)))))
  2946. (defun c-parse-state-1 ()
  2947. ;; Find and record all noteworthy parens between some good point earlier in
  2948. ;; the file and point. That good point is at least the beginning of the
  2949. ;; top-level construct we are in, or the beginning of the preceding
  2950. ;; top-level construct if we aren't in one.
  2951. ;;
  2952. ;; The returned value is a list of the noteworthy parens with the last one
  2953. ;; first. If an element in the list is an integer, it's the position of an
  2954. ;; open paren (of any type) which has not been closed before the point. If
  2955. ;; an element is a cons, it gives the position of a closed BRACE paren
  2956. ;; pair[*]; the car is the start brace position and the cdr is the position
  2957. ;; following the closing brace. Only the last closed brace paren pair
  2958. ;; before each open paren and before the point is recorded, and thus the
  2959. ;; state never contains two cons elements in succession. When a close brace
  2960. ;; has no matching open brace (e.g., the matching brace is outside the
  2961. ;; visible region), it is not represented in the returned value.
  2962. ;;
  2963. ;; [*] N.B. The close "brace" might be a mismatching close bracket or paren.
  2964. ;; This defun explicitly treats mismatching parens/braces/brackets as
  2965. ;; matching. It is the open brace which makes it a "brace" pair.
  2966. ;;
  2967. ;; If POINT is within a macro, open parens and brace pairs within
  2968. ;; THIS macro MIGHT be recorded. This depends on whether their
  2969. ;; syntactic properties have been suppressed by
  2970. ;; `c-neutralize-syntax-in-CPP'. This might need fixing (2008-12-11).
  2971. ;;
  2972. ;; Currently no characters which are given paren syntax with the
  2973. ;; syntax-table property are recorded, i.e. angle bracket arglist
  2974. ;; parens are never present here. Note that this might change.
  2975. ;;
  2976. ;; BUG: This function doesn't cope entirely well with unbalanced
  2977. ;; parens in macros. (2008-12-11: this has probably been resolved
  2978. ;; by the function `c-neutralize-syntax-in-CPP'.) E.g. in the
  2979. ;; following case the brace before the macro isn't balanced with the
  2980. ;; one after it:
  2981. ;;
  2982. ;; {
  2983. ;; #define X {
  2984. ;; }
  2985. ;;
  2986. ;; Note to maintainers: this function DOES get called with point
  2987. ;; within comments and strings, so don't assume it doesn't!
  2988. ;;
  2989. ;; This function might do hidden buffer changes.
  2990. (let* ((here (point))
  2991. (here-bopl (c-point 'bopl))
  2992. open-paren-in-column-0-is-defun-start
  2993. strategy ; 'forward, 'backward etc..
  2994. ;; Candidate positions to start scanning from:
  2995. cache-pos ; highest position below HERE already existing in
  2996. ; cache (or 1).
  2997. good-pos
  2998. start-point ; (when scanning forward) a place below HERE where there
  2999. ; are no open parens/braces between it and HERE.
  3000. bopl-state
  3001. res
  3002. cons-separated
  3003. scan-backward-pos scan-forward-p) ; used for 'backward.
  3004. ;; If POINT-MIN has changed, adjust the cache
  3005. (unless (= (point-min) c-state-point-min)
  3006. (c-renarrow-state-cache))
  3007. ;; Strategy?
  3008. (setq res (c-parse-state-get-strategy here c-state-cache-good-pos)
  3009. strategy (car res)
  3010. start-point (cadr res))
  3011. ;; SCAN!
  3012. (cond
  3013. ((memq strategy '(forward back-and-forward))
  3014. (setq res (c-remove-stale-state-cache start-point here here-bopl))
  3015. (setq cache-pos (car res)
  3016. scan-backward-pos (cadr res)
  3017. cons-separated (car (cddr res))
  3018. bopl-state (cadr (cddr res))) ; will be nil if (< here-bopl
  3019. ; start-point)
  3020. (if (and scan-backward-pos
  3021. (or cons-separated (eq strategy 'forward))) ;scan-backward-pos
  3022. (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
  3023. (setq good-pos
  3024. (c-append-to-state-cache cache-pos here))
  3025. (setq c-state-cache-good-pos
  3026. (if (and bopl-state
  3027. (< good-pos (- here c-state-cache-too-far)))
  3028. (c-state-cache-non-literal-place here-bopl bopl-state)
  3029. good-pos)))
  3030. ((eq strategy 'backward)
  3031. (setq res (c-remove-stale-state-cache-backwards here)
  3032. good-pos (car res)
  3033. scan-backward-pos (cadr res)
  3034. scan-forward-p (car (cddr res)))
  3035. (if scan-backward-pos
  3036. (c-append-lower-brace-pair-to-state-cache scan-backward-pos here))
  3037. (setq c-state-cache-good-pos
  3038. (if scan-forward-p
  3039. (c-append-to-state-cache good-pos here)
  3040. good-pos)))
  3041. (t ; (eq strategy 'IN-LIT)
  3042. (setq c-state-cache nil
  3043. c-state-cache-good-pos nil))))
  3044. c-state-cache)
  3045. (defun c-invalidate-state-cache (here)
  3046. ;; This is a wrapper over `c-invalidate-state-cache-1'.
  3047. ;;
  3048. ;; It suppresses the syntactic effect of the < and > (template) brackets and
  3049. ;; of all parens in preprocessor constructs, except for any such construct
  3050. ;; containing point. We can then call `c-invalidate-state-cache-1' without
  3051. ;; worrying further about macros and template delimiters.
  3052. (if (eval-when-compile (memq 'category-properties c-emacs-features))
  3053. ;; Emacs
  3054. (c-with-<->-as-parens-suppressed
  3055. (if (and c-state-old-cpp-beg
  3056. (< c-state-old-cpp-beg here))
  3057. (c-with-all-but-one-cpps-commented-out
  3058. c-state-old-cpp-beg
  3059. (min c-state-old-cpp-end here)
  3060. (c-invalidate-state-cache-1 here))
  3061. (c-with-cpps-commented-out
  3062. (c-invalidate-state-cache-1 here))))
  3063. ;; XEmacs
  3064. (c-invalidate-state-cache-1 here)))
  3065. (defmacro c-state-maybe-marker (place marker)
  3066. ;; If PLACE is non-nil, return a marker marking it, otherwise nil.
  3067. ;; We (re)use MARKER.
  3068. `(and ,place
  3069. (or ,marker (setq ,marker (make-marker)))
  3070. (set-marker ,marker ,place)))
  3071. (defun c-parse-state ()
  3072. ;; This is a wrapper over `c-parse-state-1'. See that function for a
  3073. ;; description of the functionality and return value.
  3074. ;;
  3075. ;; It suppresses the syntactic effect of the < and > (template) brackets and
  3076. ;; of all parens in preprocessor constructs, except for any such construct
  3077. ;; containing point. We can then call `c-parse-state-1' without worrying
  3078. ;; further about macros and template delimiters.
  3079. (let (here-cpp-beg here-cpp-end)
  3080. (save-excursion
  3081. (when (c-beginning-of-macro)
  3082. (setq here-cpp-beg (point))
  3083. (unless
  3084. (> (setq here-cpp-end (c-syntactic-end-of-macro))
  3085. here-cpp-beg)
  3086. (setq here-cpp-beg nil here-cpp-end nil))))
  3087. ;; FIXME!!! Put in a `condition-case' here to protect the integrity of the
  3088. ;; subsystem.
  3089. (prog1
  3090. (if (eval-when-compile (memq 'category-properties c-emacs-features))
  3091. ;; Emacs
  3092. (c-with-<->-as-parens-suppressed
  3093. (if (and here-cpp-beg (> here-cpp-end here-cpp-beg))
  3094. (c-with-all-but-one-cpps-commented-out
  3095. here-cpp-beg here-cpp-end
  3096. (c-parse-state-1))
  3097. (c-with-cpps-commented-out
  3098. (c-parse-state-1))))
  3099. ;; XEmacs
  3100. (c-parse-state-1))
  3101. (setq c-state-old-cpp-beg
  3102. (c-state-maybe-marker here-cpp-beg c-state-old-cpp-beg-marker)
  3103. c-state-old-cpp-end
  3104. (c-state-maybe-marker here-cpp-end c-state-old-cpp-end-marker)))))
  3105. ;; Debug tool to catch cache inconsistencies. This is called from
  3106. ;; 000tests.el.
  3107. (defvar c-debug-parse-state nil)
  3108. (unless (fboundp 'c-real-parse-state)
  3109. (fset 'c-real-parse-state (symbol-function 'c-parse-state)))
  3110. (cc-bytecomp-defun c-real-parse-state)
  3111. (defvar c-parse-state-point nil)
  3112. (defvar c-parse-state-state nil)
  3113. (make-variable-buffer-local 'c-parse-state-state)
  3114. (defun c-record-parse-state-state ()
  3115. (setq c-parse-state-point (point))
  3116. (setq c-parse-state-state
  3117. (mapcar
  3118. (lambda (arg)
  3119. (let ((val (symbol-value arg)))
  3120. (cons arg
  3121. (cond ((consp val) (copy-tree val))
  3122. ((markerp val) (copy-marker val))
  3123. (t val)))))
  3124. '(c-state-cache
  3125. c-state-cache-good-pos
  3126. c-state-nonlit-pos-cache
  3127. c-state-nonlit-pos-cache-limit
  3128. c-state-semi-nonlit-pos-cache
  3129. c-state-semi-nonlit-pos-cache-limit
  3130. c-state-brace-pair-desert
  3131. c-state-point-min
  3132. c-state-point-min-lit-type
  3133. c-state-point-min-lit-start
  3134. c-state-min-scan-pos
  3135. c-state-old-cpp-beg
  3136. c-state-old-cpp-end
  3137. c-parse-state-point))))
  3138. (defun c-replay-parse-state-state ()
  3139. (message
  3140. (concat "(setq "
  3141. (mapconcat
  3142. (lambda (arg)
  3143. (format "%s %s%s" (car arg)
  3144. (if (atom (cdr arg)) "" "'")
  3145. (if (markerp (cdr arg))
  3146. (format "(copy-marker %s)" (marker-position (cdr arg)))
  3147. (cdr arg))))
  3148. c-parse-state-state " ")
  3149. ")")))
  3150. (defun c-debug-parse-state-double-cons (state)
  3151. (let (state-car conses-not-ok)
  3152. (while state
  3153. (setq state-car (car state)
  3154. state (cdr state))
  3155. (if (and (consp state-car)
  3156. (consp (car state)))
  3157. (setq conses-not-ok t)))
  3158. conses-not-ok))
  3159. (defun c-debug-parse-state ()
  3160. (let ((here (point)) (res1 (c-real-parse-state)) res2)
  3161. (let ((c-state-cache nil)
  3162. (c-state-cache-good-pos 1)
  3163. (c-state-nonlit-pos-cache nil)
  3164. (c-state-nonlit-pos-cache-limit 1)
  3165. (c-state-brace-pair-desert nil)
  3166. (c-state-point-min 1)
  3167. (c-state-point-min-lit-type nil)
  3168. (c-state-point-min-lit-start nil)
  3169. (c-state-min-scan-pos 1)
  3170. (c-state-old-cpp-beg nil)
  3171. (c-state-old-cpp-end nil))
  3172. (setq res2 (c-real-parse-state)))
  3173. (unless (equal res1 res2)
  3174. ;; The cache can actually go further back due to the ad-hoc way
  3175. ;; the first paren is found, so try to whack off a bit of its
  3176. ;; start before complaining.
  3177. ;; (save-excursion
  3178. ;; (goto-char (or (c-least-enclosing-brace res2) (point)))
  3179. ;; (c-beginning-of-defun-1)
  3180. ;; (while (not (or (bobp) (eq (char-after) ?{)))
  3181. ;; (c-beginning-of-defun-1))
  3182. ;; (unless (equal (c-whack-state-before (point) res1) res2)
  3183. ;; (message (concat "c-parse-state inconsistency at %s: "
  3184. ;; "using cache: %s, from scratch: %s")
  3185. ;; here res1 res2)))
  3186. (message (concat "c-parse-state inconsistency at %s: "
  3187. "using cache: %s, from scratch: %s")
  3188. here res1 res2)
  3189. (message "Old state:")
  3190. (c-replay-parse-state-state))
  3191. (when (c-debug-parse-state-double-cons res1)
  3192. (message "c-parse-state INVALIDITY at %s: %s"
  3193. here res1)
  3194. (message "Old state:")
  3195. (c-replay-parse-state-state))
  3196. (c-record-parse-state-state)
  3197. res2 ; res1 correct a cascading series of errors ASAP
  3198. ))
  3199. (defun c-toggle-parse-state-debug (&optional arg)
  3200. (interactive "P")
  3201. (setq c-debug-parse-state (c-calculate-state arg c-debug-parse-state))
  3202. (fset 'c-parse-state (symbol-function (if c-debug-parse-state
  3203. 'c-debug-parse-state
  3204. 'c-real-parse-state)))
  3205. (c-keep-region-active)
  3206. (message "c-debug-parse-state %sabled"
  3207. (if c-debug-parse-state "en" "dis")))
  3208. (when c-debug-parse-state
  3209. (c-toggle-parse-state-debug 1))
  3210. (defun c-whack-state-before (bufpos paren-state)
  3211. ;; Whack off any state information from PAREN-STATE which lies
  3212. ;; before BUFPOS. Not destructive on PAREN-STATE.
  3213. (let* ((newstate (list nil))
  3214. (ptr newstate)
  3215. car)
  3216. (while paren-state
  3217. (setq car (car paren-state)
  3218. paren-state (cdr paren-state))
  3219. (if (< (if (consp car) (car car) car) bufpos)
  3220. (setq paren-state nil)
  3221. (setcdr ptr (list car))
  3222. (setq ptr (cdr ptr))))
  3223. (cdr newstate)))
  3224. (defun c-whack-state-after (bufpos paren-state)
  3225. ;; Whack off any state information from PAREN-STATE which lies at or
  3226. ;; after BUFPOS. Not destructive on PAREN-STATE.
  3227. (catch 'done
  3228. (while paren-state
  3229. (let ((car (car paren-state)))
  3230. (if (consp car)
  3231. ;; just check the car, because in a balanced brace
  3232. ;; expression, it must be impossible for the corresponding
  3233. ;; close brace to be before point, but the open brace to
  3234. ;; be after.
  3235. (if (<= bufpos (car car))
  3236. nil ; whack it off
  3237. (if (< bufpos (cdr car))
  3238. ;; its possible that the open brace is before
  3239. ;; bufpos, but the close brace is after. In that
  3240. ;; case, convert this to a non-cons element. The
  3241. ;; rest of the state is before bufpos, so we're
  3242. ;; done.
  3243. (throw 'done (cons (car car) (cdr paren-state)))
  3244. ;; we know that both the open and close braces are
  3245. ;; before bufpos, so we also know that everything else
  3246. ;; on state is before bufpos.
  3247. (throw 'done paren-state)))
  3248. (if (<= bufpos car)
  3249. nil ; whack it off
  3250. ;; it's before bufpos, so everything else should too.
  3251. (throw 'done paren-state)))
  3252. (setq paren-state (cdr paren-state)))
  3253. nil)))
  3254. (defun c-most-enclosing-brace (paren-state &optional bufpos)
  3255. ;; Return the bufpos of the innermost enclosing open paren before
  3256. ;; bufpos, or nil if none was found.
  3257. (let (enclosingp)
  3258. (or bufpos (setq bufpos 134217727))
  3259. (while paren-state
  3260. (setq enclosingp (car paren-state)
  3261. paren-state (cdr paren-state))
  3262. (if (or (consp enclosingp)
  3263. (>= enclosingp bufpos))
  3264. (setq enclosingp nil)
  3265. (setq paren-state nil)))
  3266. enclosingp))
  3267. (defun c-least-enclosing-brace (paren-state)
  3268. ;; Return the bufpos of the outermost enclosing open paren, or nil
  3269. ;; if none was found.
  3270. (let (pos elem)
  3271. (while paren-state
  3272. (setq elem (car paren-state)
  3273. paren-state (cdr paren-state))
  3274. (if (integerp elem)
  3275. (setq pos elem)))
  3276. pos))
  3277. (defun c-safe-position (bufpos paren-state)
  3278. ;; Return the closest "safe" position recorded on PAREN-STATE that
  3279. ;; is higher up than BUFPOS. Return nil if PAREN-STATE doesn't
  3280. ;; contain any. Return nil if BUFPOS is nil, which is useful to
  3281. ;; find the closest limit before a given limit that might be nil.
  3282. ;;
  3283. ;; A "safe" position is a position at or after a recorded open
  3284. ;; paren, or after a recorded close paren. The returned position is
  3285. ;; thus either the first position after a close brace, or the first
  3286. ;; position after an enclosing paren, or at the enclosing paren in
  3287. ;; case BUFPOS is immediately after it.
  3288. (when bufpos
  3289. (let (elem)
  3290. (catch 'done
  3291. (while paren-state
  3292. (setq elem (car paren-state))
  3293. (if (consp elem)
  3294. (cond ((< (cdr elem) bufpos)
  3295. (throw 'done (cdr elem)))
  3296. ((< (car elem) bufpos)
  3297. ;; See below.
  3298. (throw 'done (min (1+ (car elem)) bufpos))))
  3299. (if (< elem bufpos)
  3300. ;; elem is the position at and not after the opening paren, so
  3301. ;; we can go forward one more step unless it's equal to
  3302. ;; bufpos. This is useful in some cases avoid an extra paren
  3303. ;; level between the safe position and bufpos.
  3304. (throw 'done (min (1+ elem) bufpos))))
  3305. (setq paren-state (cdr paren-state)))))))
  3306. (defun c-beginning-of-syntax ()
  3307. ;; This is used for `font-lock-beginning-of-syntax-function'. It
  3308. ;; goes to the closest previous point that is known to be outside
  3309. ;; any string literal or comment. `c-state-cache' is used if it has
  3310. ;; a position in the vicinity.
  3311. (let* ((paren-state c-state-cache)
  3312. elem
  3313. (pos (catch 'done
  3314. ;; Note: Similar code in `c-safe-position'. The
  3315. ;; difference is that we accept a safe position at
  3316. ;; the point and don't bother to go forward past open
  3317. ;; parens.
  3318. (while paren-state
  3319. (setq elem (car paren-state))
  3320. (if (consp elem)
  3321. (cond ((<= (cdr elem) (point))
  3322. (throw 'done (cdr elem)))
  3323. ((<= (car elem) (point))
  3324. (throw 'done (car elem))))
  3325. (if (<= elem (point))
  3326. (throw 'done elem)))
  3327. (setq paren-state (cdr paren-state)))
  3328. (point-min))))
  3329. (if (> pos (- (point) 4000))
  3330. (goto-char pos)
  3331. ;; The position is far back. Try `c-beginning-of-defun-1'
  3332. ;; (although we can't be entirely sure it will go to a position
  3333. ;; outside a comment or string in current emacsen). FIXME:
  3334. ;; Consult `syntax-ppss' here.
  3335. (c-beginning-of-defun-1)
  3336. (if (< (point) pos)
  3337. (goto-char pos)))))
  3338. ;; Tools for scanning identifiers and other tokens.
  3339. (defun c-on-identifier ()
  3340. "Return non-nil if the point is on or directly after an identifier.
  3341. Keywords are recognized and not considered identifiers. If an
  3342. identifier is detected, the returned value is its starting position.
  3343. If an identifier ends at the point and another begins at it \(can only
  3344. happen in Pike) then the point for the preceding one is returned.
  3345. Note that this function might do hidden buffer changes. See the
  3346. comment at the start of cc-engine.el for more info."
  3347. ;; FIXME: Shouldn't this function handle "operator" in C++?
  3348. (save-excursion
  3349. (skip-syntax-backward "w_")
  3350. (or
  3351. ;; Check for a normal (non-keyword) identifier.
  3352. (and (looking-at c-symbol-start)
  3353. (not (looking-at c-keywords-regexp))
  3354. (point))
  3355. (when (c-major-mode-is 'pike-mode)
  3356. ;; Handle the `<operator> syntax in Pike.
  3357. (let ((pos (point)))
  3358. (skip-chars-backward "-!%&*+/<=>^|~[]()")
  3359. (and (if (< (skip-chars-backward "`") 0)
  3360. t
  3361. (goto-char pos)
  3362. (eq (char-after) ?\`))
  3363. (looking-at c-symbol-key)
  3364. (>= (match-end 0) pos)
  3365. (point))))
  3366. ;; Handle the "operator +" syntax in C++.
  3367. (when (and c-overloadable-operators-regexp
  3368. (= (c-backward-token-2 0) 0))
  3369. (cond ((and (looking-at c-overloadable-operators-regexp)
  3370. (or (not c-opt-op-identifier-prefix)
  3371. (and (= (c-backward-token-2 1) 0)
  3372. (looking-at c-opt-op-identifier-prefix))))
  3373. (point))
  3374. ((save-excursion
  3375. (and c-opt-op-identifier-prefix
  3376. (looking-at c-opt-op-identifier-prefix)
  3377. (= (c-forward-token-2 1) 0)
  3378. (looking-at c-overloadable-operators-regexp)))
  3379. (point))))
  3380. )))
  3381. (defsubst c-simple-skip-symbol-backward ()
  3382. ;; If the point is at the end of a symbol then skip backward to the
  3383. ;; beginning of it. Don't move otherwise. Return non-nil if point
  3384. ;; moved.
  3385. ;;
  3386. ;; This function might do hidden buffer changes.
  3387. (or (< (skip-syntax-backward "w_") 0)
  3388. (and (c-major-mode-is 'pike-mode)
  3389. ;; Handle the `<operator> syntax in Pike.
  3390. (let ((pos (point)))
  3391. (if (and (< (skip-chars-backward "-!%&*+/<=>^|~[]()") 0)
  3392. (< (skip-chars-backward "`") 0)
  3393. (looking-at c-symbol-key)
  3394. (>= (match-end 0) pos))
  3395. t
  3396. (goto-char pos)
  3397. nil)))))
  3398. (defun c-beginning-of-current-token (&optional back-limit)
  3399. ;; Move to the beginning of the current token. Do not move if not
  3400. ;; in the middle of one. BACK-LIMIT may be used to bound the
  3401. ;; backward search; if given it's assumed to be at the boundary
  3402. ;; between two tokens. Return non-nil if the point is moved, nil
  3403. ;; otherwise.
  3404. ;;
  3405. ;; This function might do hidden buffer changes.
  3406. (let ((start (point)))
  3407. (if (looking-at "\\w\\|\\s_")
  3408. (skip-syntax-backward "w_" back-limit)
  3409. (when (< (skip-syntax-backward ".()" back-limit) 0)
  3410. (while (let ((pos (or (and (looking-at c-nonsymbol-token-regexp)
  3411. (match-end 0))
  3412. ;; `c-nonsymbol-token-regexp' should always match
  3413. ;; since we've skipped backward over punctuation
  3414. ;; or paren syntax, but consume one char in case
  3415. ;; it doesn't so that we don't leave point before
  3416. ;; some earlier incorrect token.
  3417. (1+ (point)))))
  3418. (if (<= pos start)
  3419. (goto-char pos))))))
  3420. (< (point) start)))
  3421. (defun c-end-of-current-token (&optional back-limit)
  3422. ;; Move to the end of the current token. Do not move if not in the
  3423. ;; middle of one. BACK-LIMIT may be used to bound the backward
  3424. ;; search; if given it's assumed to be at the boundary between two
  3425. ;; tokens. Return non-nil if the point is moved, nil otherwise.
  3426. ;;
  3427. ;; This function might do hidden buffer changes.
  3428. (let ((start (point)))
  3429. (cond ((< (skip-syntax-backward "w_" (1- start)) 0)
  3430. (skip-syntax-forward "w_"))
  3431. ((< (skip-syntax-backward ".()" back-limit) 0)
  3432. (while (progn
  3433. (if (looking-at c-nonsymbol-token-regexp)
  3434. (goto-char (match-end 0))
  3435. ;; `c-nonsymbol-token-regexp' should always match since
  3436. ;; we've skipped backward over punctuation or paren
  3437. ;; syntax, but move forward in case it doesn't so that
  3438. ;; we don't leave point earlier than we started with.
  3439. (forward-char))
  3440. (< (point) start)))))
  3441. (> (point) start)))
  3442. (defconst c-jump-syntax-balanced
  3443. (if (memq 'gen-string-delim c-emacs-features)
  3444. "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\"\\|\\s|"
  3445. "\\w\\|\\s_\\|\\s\(\\|\\s\)\\|\\s\""))
  3446. (defconst c-jump-syntax-unbalanced
  3447. (if (memq 'gen-string-delim c-emacs-features)
  3448. "\\w\\|\\s_\\|\\s\"\\|\\s|"
  3449. "\\w\\|\\s_\\|\\s\""))
  3450. (defun c-forward-token-2 (&optional count balanced limit)
  3451. "Move forward by tokens.
  3452. A token is defined as all symbols and identifiers which aren't
  3453. syntactic whitespace \(note that multicharacter tokens like \"==\" are
  3454. treated properly). Point is always either left at the beginning of a
  3455. token or not moved at all. COUNT specifies the number of tokens to
  3456. move; a negative COUNT moves in the opposite direction. A COUNT of 0
  3457. moves to the next token beginning only if not already at one. If
  3458. BALANCED is true, move over balanced parens, otherwise move into them.
  3459. Also, if BALANCED is true, never move out of an enclosing paren.
  3460. LIMIT sets the limit for the movement and defaults to the point limit.
  3461. The case when LIMIT is set in the middle of a token, comment or macro
  3462. is handled correctly, i.e. the point won't be left there.
  3463. Return the number of tokens left to move \(positive or negative). If
  3464. BALANCED is true, a move over a balanced paren counts as one. Note
  3465. that if COUNT is 0 and no appropriate token beginning is found, 1 will
  3466. be returned. Thus, a return value of 0 guarantees that point is at
  3467. the requested position and a return value less \(without signs) than
  3468. COUNT guarantees that point is at the beginning of some token.
  3469. Note that this function might do hidden buffer changes. See the
  3470. comment at the start of cc-engine.el for more info."
  3471. (or count (setq count 1))
  3472. (if (< count 0)
  3473. (- (c-backward-token-2 (- count) balanced limit))
  3474. (let ((jump-syntax (if balanced
  3475. c-jump-syntax-balanced
  3476. c-jump-syntax-unbalanced))
  3477. (last (point))
  3478. (prev (point)))
  3479. (if (zerop count)
  3480. ;; If count is zero we should jump if in the middle of a token.
  3481. (c-end-of-current-token))
  3482. (save-restriction
  3483. (if limit (narrow-to-region (point-min) limit))
  3484. (if (/= (point)
  3485. (progn (c-forward-syntactic-ws) (point)))
  3486. ;; Skip whitespace. Count this as a move if we did in
  3487. ;; fact move.
  3488. (setq count (max (1- count) 0)))
  3489. (if (eobp)
  3490. ;; Moved out of bounds. Make sure the returned count isn't zero.
  3491. (progn
  3492. (if (zerop count) (setq count 1))
  3493. (goto-char last))
  3494. ;; Use `condition-case' to avoid having the limit tests
  3495. ;; inside the loop.
  3496. (condition-case nil
  3497. (while (and
  3498. (> count 0)
  3499. (progn
  3500. (setq last (point))
  3501. (cond ((looking-at jump-syntax)
  3502. (goto-char (scan-sexps (point) 1))
  3503. t)
  3504. ((looking-at c-nonsymbol-token-regexp)
  3505. (goto-char (match-end 0))
  3506. t)
  3507. ;; `c-nonsymbol-token-regexp' above should always
  3508. ;; match if there are correct tokens. Try to
  3509. ;; widen to see if the limit was set in the
  3510. ;; middle of one, else fall back to treating
  3511. ;; the offending thing as a one character token.
  3512. ((and limit
  3513. (save-restriction
  3514. (widen)
  3515. (looking-at c-nonsymbol-token-regexp)))
  3516. nil)
  3517. (t
  3518. (forward-char)
  3519. t))))
  3520. (c-forward-syntactic-ws)
  3521. (setq prev last
  3522. count (1- count)))
  3523. (error (goto-char last)))
  3524. (when (eobp)
  3525. (goto-char prev)
  3526. (setq count (1+ count)))))
  3527. count)))
  3528. (defun c-backward-token-2 (&optional count balanced limit)
  3529. "Move backward by tokens.
  3530. See `c-forward-token-2' for details."
  3531. (or count (setq count 1))
  3532. (if (< count 0)
  3533. (- (c-forward-token-2 (- count) balanced limit))
  3534. (or limit (setq limit (point-min)))
  3535. (let ((jump-syntax (if balanced
  3536. c-jump-syntax-balanced
  3537. c-jump-syntax-unbalanced))
  3538. (last (point)))
  3539. (if (zerop count)
  3540. ;; The count is zero so try to skip to the beginning of the
  3541. ;; current token.
  3542. (if (> (point)
  3543. (progn (c-beginning-of-current-token) (point)))
  3544. (if (< (point) limit)
  3545. ;; The limit is inside the same token, so return 1.
  3546. (setq count 1))
  3547. ;; We're not in the middle of a token. If there's
  3548. ;; whitespace after the point then we must move backward,
  3549. ;; so set count to 1 in that case.
  3550. (and (looking-at c-syntactic-ws-start)
  3551. ;; If we're looking at a '#' that might start a cpp
  3552. ;; directive then we have to do a more elaborate check.
  3553. (or (/= (char-after) ?#)
  3554. (not c-opt-cpp-prefix)
  3555. (save-excursion
  3556. (and (= (point)
  3557. (progn (beginning-of-line)
  3558. (looking-at "[ \t]*")
  3559. (match-end 0)))
  3560. (or (bobp)
  3561. (progn (backward-char)
  3562. (not (eq (char-before) ?\\)))))))
  3563. (setq count 1))))
  3564. ;; Use `condition-case' to avoid having to check for buffer
  3565. ;; limits in `backward-char', `scan-sexps' and `goto-char' below.
  3566. (condition-case nil
  3567. (while (and
  3568. (> count 0)
  3569. (progn
  3570. (c-backward-syntactic-ws)
  3571. (backward-char)
  3572. (if (looking-at jump-syntax)
  3573. (goto-char (scan-sexps (1+ (point)) -1))
  3574. ;; This can be very inefficient if there's a long
  3575. ;; sequence of operator tokens without any separation.
  3576. ;; That doesn't happen in practice, anyway.
  3577. (c-beginning-of-current-token))
  3578. (>= (point) limit)))
  3579. (setq last (point)
  3580. count (1- count)))
  3581. (error (goto-char last)))
  3582. (if (< (point) limit)
  3583. (goto-char last))
  3584. count)))
  3585. (defun c-forward-token-1 (&optional count balanced limit)
  3586. "Like `c-forward-token-2' but doesn't treat multicharacter operator
  3587. tokens like \"==\" as single tokens, i.e. all sequences of symbol
  3588. characters are jumped over character by character. This function is
  3589. for compatibility only; it's only a wrapper over `c-forward-token-2'."
  3590. (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
  3591. (c-forward-token-2 count balanced limit)))
  3592. (defun c-backward-token-1 (&optional count balanced limit)
  3593. "Like `c-backward-token-2' but doesn't treat multicharacter operator
  3594. tokens like \"==\" as single tokens, i.e. all sequences of symbol
  3595. characters are jumped over character by character. This function is
  3596. for compatibility only; it's only a wrapper over `c-backward-token-2'."
  3597. (let ((c-nonsymbol-token-regexp "\\s.\\|\\s\(\\|\\s\)"))
  3598. (c-backward-token-2 count balanced limit)))
  3599. ;; Tools for doing searches restricted to syntactically relevant text.
  3600. (defun c-syntactic-re-search-forward (regexp &optional bound noerror
  3601. paren-level not-inside-token
  3602. lookbehind-submatch)
  3603. "Like `re-search-forward', but only report matches that are found
  3604. in syntactically significant text. I.e. matches in comments, macros
  3605. or string literals are ignored. The start point is assumed to be
  3606. outside any comment, macro or string literal, or else the content of
  3607. that region is taken as syntactically significant text.
  3608. If PAREN-LEVEL is non-nil, an additional restriction is added to
  3609. ignore matches in nested paren sexps. The search will also not go
  3610. outside the current list sexp, which has the effect that if the point
  3611. should be moved to BOUND when no match is found \(i.e. NOERROR is
  3612. neither nil nor t), then it will be at the closing paren if the end of
  3613. the current list sexp is encountered first.
  3614. If NOT-INSIDE-TOKEN is non-nil, matches in the middle of tokens are
  3615. ignored. Things like multicharacter operators and special symbols
  3616. \(e.g. \"`()\" in Pike) are handled but currently not floating point
  3617. constants.
  3618. If LOOKBEHIND-SUBMATCH is non-nil, it's taken as a number of a
  3619. subexpression in REGEXP. The end of that submatch is used as the
  3620. position to check for syntactic significance. If LOOKBEHIND-SUBMATCH
  3621. isn't used or if that subexpression didn't match then the start
  3622. position of the whole match is used instead. The \"look behind\"
  3623. subexpression is never tested before the starting position, so it
  3624. might be a good idea to include \\=\\= as a match alternative in it.
  3625. Optimization note: Matches might be missed if the \"look behind\"
  3626. subexpression can match the end of nonwhite syntactic whitespace,
  3627. i.e. the end of comments or cpp directives. This since the function
  3628. skips over such things before resuming the search. It's on the other
  3629. hand not safe to assume that the \"look behind\" subexpression never
  3630. matches syntactic whitespace.
  3631. Bug: Unbalanced parens inside cpp directives are currently not handled
  3632. correctly \(i.e. they don't get ignored as they should) when
  3633. PAREN-LEVEL is set.
  3634. Note that this function might do hidden buffer changes. See the
  3635. comment at the start of cc-engine.el for more info."
  3636. (or bound (setq bound (point-max)))
  3637. (if paren-level (setq paren-level -1))
  3638. ;;(message "c-syntactic-re-search-forward %s %s %S" (point) bound regexp)
  3639. (let ((start (point))
  3640. tmp
  3641. ;; Start position for the last search.
  3642. search-pos
  3643. ;; The `parse-partial-sexp' state between the start position
  3644. ;; and the point.
  3645. state
  3646. ;; The current position after the last state update. The next
  3647. ;; `parse-partial-sexp' continues from here.
  3648. (state-pos (point))
  3649. ;; The position at which to check the state and the state
  3650. ;; there. This is separate from `state-pos' since we might
  3651. ;; need to back up before doing the next search round.
  3652. check-pos check-state
  3653. ;; Last position known to end a token.
  3654. (last-token-end-pos (point-min))
  3655. ;; Set when a valid match is found.
  3656. found)
  3657. (condition-case err
  3658. (while
  3659. (and
  3660. (progn
  3661. (setq search-pos (point))
  3662. (re-search-forward regexp bound noerror))
  3663. (progn
  3664. (setq state (parse-partial-sexp
  3665. state-pos (match-beginning 0) paren-level nil state)
  3666. state-pos (point))
  3667. (if (setq check-pos (and lookbehind-submatch
  3668. (or (not paren-level)
  3669. (>= (car state) 0))
  3670. (match-end lookbehind-submatch)))
  3671. (setq check-state (parse-partial-sexp
  3672. state-pos check-pos paren-level nil state))
  3673. (setq check-pos state-pos
  3674. check-state state))
  3675. ;; NOTE: If we got a look behind subexpression and get
  3676. ;; an insignificant match in something that isn't
  3677. ;; syntactic whitespace (i.e. strings or in nested
  3678. ;; parentheses), then we can never skip more than a
  3679. ;; single character from the match start position
  3680. ;; (i.e. `state-pos' here) before continuing the
  3681. ;; search. That since the look behind subexpression
  3682. ;; might match the end of the insignificant region in
  3683. ;; the next search.
  3684. (cond
  3685. ((elt check-state 7)
  3686. ;; Match inside a line comment. Skip to eol. Use
  3687. ;; `re-search-forward' instead of `skip-chars-forward' to get
  3688. ;; the right bound behavior.
  3689. (re-search-forward "[\n\r]" bound noerror))
  3690. ((elt check-state 4)
  3691. ;; Match inside a block comment. Skip to the '*/'.
  3692. (search-forward "*/" bound noerror))
  3693. ((and (not (elt check-state 5))
  3694. (eq (char-before check-pos) ?/)
  3695. (not (c-get-char-property (1- check-pos) 'syntax-table))
  3696. (memq (char-after check-pos) '(?/ ?*)))
  3697. ;; Match in the middle of the opener of a block or line
  3698. ;; comment.
  3699. (if (= (char-after check-pos) ?/)
  3700. (re-search-forward "[\n\r]" bound noerror)
  3701. (search-forward "*/" bound noerror)))
  3702. ;; The last `parse-partial-sexp' above might have
  3703. ;; stopped short of the real check position if the end
  3704. ;; of the current sexp was encountered in paren-level
  3705. ;; mode. The checks above are always false in that
  3706. ;; case, and since they can do better skipping in
  3707. ;; lookbehind-submatch mode, we do them before
  3708. ;; checking the paren level.
  3709. ((and paren-level
  3710. (/= (setq tmp (car check-state)) 0))
  3711. ;; Check the paren level first since we're short of the
  3712. ;; syntactic checking position if the end of the
  3713. ;; current sexp was encountered by `parse-partial-sexp'.
  3714. (if (> tmp 0)
  3715. ;; Inside a nested paren sexp.
  3716. (if lookbehind-submatch
  3717. ;; See the NOTE above.
  3718. (progn (goto-char state-pos) t)
  3719. ;; Skip out of the paren quickly.
  3720. (setq state (parse-partial-sexp state-pos bound 0 nil state)
  3721. state-pos (point)))
  3722. ;; Have exited the current paren sexp.
  3723. (if noerror
  3724. (progn
  3725. ;; The last `parse-partial-sexp' call above
  3726. ;; has left us just after the closing paren
  3727. ;; in this case, so we can modify the bound
  3728. ;; to leave the point at the right position
  3729. ;; upon return.
  3730. (setq bound (1- (point)))
  3731. nil)
  3732. (signal 'search-failed (list regexp)))))
  3733. ((setq tmp (elt check-state 3))
  3734. ;; Match inside a string.
  3735. (if (or lookbehind-submatch
  3736. (not (integerp tmp)))
  3737. ;; See the NOTE above.
  3738. (progn (goto-char state-pos) t)
  3739. ;; Skip to the end of the string before continuing.
  3740. (let ((ender (make-string 1 tmp)) (continue t))
  3741. (while (if (search-forward ender bound noerror)
  3742. (progn
  3743. (setq state (parse-partial-sexp
  3744. state-pos (point) nil nil state)
  3745. state-pos (point))
  3746. (elt state 3))
  3747. (setq continue nil)))
  3748. continue)))
  3749. ((save-excursion
  3750. (save-match-data
  3751. (c-beginning-of-macro start)))
  3752. ;; Match inside a macro. Skip to the end of it.
  3753. (c-end-of-macro)
  3754. (cond ((<= (point) bound) t)
  3755. (noerror nil)
  3756. (t (signal 'search-failed (list regexp)))))
  3757. ((and not-inside-token
  3758. (or (< check-pos last-token-end-pos)
  3759. (< check-pos
  3760. (save-excursion
  3761. (goto-char check-pos)
  3762. (save-match-data
  3763. (c-end-of-current-token last-token-end-pos))
  3764. (setq last-token-end-pos (point))))))
  3765. ;; Inside a token.
  3766. (if lookbehind-submatch
  3767. ;; See the NOTE above.
  3768. (goto-char state-pos)
  3769. (goto-char (min last-token-end-pos bound))))
  3770. (t
  3771. ;; A real match.
  3772. (setq found t)
  3773. nil)))
  3774. ;; Should loop to search again, but take care to avoid
  3775. ;; looping on the same spot.
  3776. (or (/= search-pos (point))
  3777. (if (= (point) bound)
  3778. (if noerror
  3779. nil
  3780. (signal 'search-failed (list regexp)))
  3781. (forward-char)
  3782. t))))
  3783. (error
  3784. (goto-char start)
  3785. (signal (car err) (cdr err))))
  3786. ;;(message "c-syntactic-re-search-forward done %s" (or (match-end 0) (point)))
  3787. (if found
  3788. (progn
  3789. (goto-char (match-end 0))
  3790. (match-end 0))
  3791. ;; Search failed. Set point as appropriate.
  3792. (if (eq noerror t)
  3793. (goto-char start)
  3794. (goto-char bound))
  3795. nil)))
  3796. (defvar safe-pos-list) ; bound in c-syntactic-skip-backward
  3797. (defsubst c-ssb-lit-begin ()
  3798. ;; Return the start of the literal point is in, or nil.
  3799. ;; We read and write the variables `safe-pos', `safe-pos-list', `state'
  3800. ;; bound in the caller.
  3801. ;; Use `parse-partial-sexp' from a safe position down to the point to check
  3802. ;; if it's outside comments and strings.
  3803. (save-excursion
  3804. (let ((pos (point)) safe-pos state)
  3805. ;; Pick a safe position as close to the point as possible.
  3806. ;;
  3807. ;; FIXME: Consult `syntax-ppss' here if our cache doesn't give a good
  3808. ;; position.
  3809. (while (and safe-pos-list
  3810. (> (car safe-pos-list) (point)))
  3811. (setq safe-pos-list (cdr safe-pos-list)))
  3812. (unless (setq safe-pos (car-safe safe-pos-list))
  3813. (setq safe-pos (max (or (c-safe-position
  3814. (point) (or c-state-cache
  3815. (c-parse-state)))
  3816. 0)
  3817. (point-min))
  3818. safe-pos-list (list safe-pos)))
  3819. ;; Cache positions along the way to use if we have to back up more. We
  3820. ;; cache every closing paren on the same level. If the paren cache is
  3821. ;; relevant in this region then we're typically already on the same
  3822. ;; level as the target position. Note that we might cache positions
  3823. ;; after opening parens in case safe-pos is in a nested list. That's
  3824. ;; both uncommon and harmless.
  3825. (while (progn
  3826. (setq state (parse-partial-sexp
  3827. safe-pos pos 0))
  3828. (< (point) pos))
  3829. (setq safe-pos (point)
  3830. safe-pos-list (cons safe-pos safe-pos-list)))
  3831. ;; If the state contains the start of the containing sexp we cache that
  3832. ;; position too, so that parse-partial-sexp in the next run has a bigger
  3833. ;; chance of starting at the same level as the target position and thus
  3834. ;; will get more good safe positions into the list.
  3835. (if (elt state 1)
  3836. (setq safe-pos (1+ (elt state 1))
  3837. safe-pos-list (cons safe-pos safe-pos-list)))
  3838. (if (or (elt state 3) (elt state 4))
  3839. ;; Inside string or comment. Continue search at the
  3840. ;; beginning of it.
  3841. (elt state 8)))))
  3842. (defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
  3843. "Like `skip-chars-backward' but only look at syntactically relevant chars,
  3844. i.e. don't stop at positions inside syntactic whitespace or string
  3845. literals. Preprocessor directives are also ignored, with the exception
  3846. of the one that the point starts within, if any. If LIMIT is given,
  3847. it's assumed to be at a syntactically relevant position.
  3848. If PAREN-LEVEL is non-nil, the function won't stop in nested paren
  3849. sexps, and the search will also not go outside the current paren sexp.
  3850. However, if LIMIT or the buffer limit is reached inside a nested paren
  3851. then the point will be left at the limit.
  3852. Non-nil is returned if the point moved, nil otherwise.
  3853. Note that this function might do hidden buffer changes. See the
  3854. comment at the start of cc-engine.el for more info."
  3855. (let ((start (point))
  3856. state-2
  3857. ;; A list of syntactically relevant positions in descending
  3858. ;; order. It's used to avoid scanning repeatedly over
  3859. ;; potentially large regions with `parse-partial-sexp' to verify
  3860. ;; each position. Used in `c-ssb-lit-begin'
  3861. safe-pos-list
  3862. ;; The result from `c-beginning-of-macro' at the start position or the
  3863. ;; start position itself if it isn't within a macro. Evaluated on
  3864. ;; demand.
  3865. start-macro-beg
  3866. ;; The earliest position after the current one with the same paren
  3867. ;; level. Used only when `paren-level' is set.
  3868. lit-beg
  3869. (paren-level-pos (point)))
  3870. (while
  3871. (progn
  3872. ;; The next loop "tries" to find the end point each time round,
  3873. ;; loops when it hasn't succeeded.
  3874. (while
  3875. (and
  3876. (let ((pos (point)))
  3877. (while (and
  3878. (< (skip-chars-backward skip-chars limit) 0)
  3879. ;; Don't stop inside a literal.
  3880. (when (setq lit-beg (c-ssb-lit-begin))
  3881. (goto-char lit-beg)
  3882. t)))
  3883. (< (point) pos))
  3884. (let ((pos (point)) state-2 pps-end-pos)
  3885. (cond
  3886. ((and paren-level
  3887. (save-excursion
  3888. (setq state-2 (parse-partial-sexp
  3889. pos paren-level-pos -1)
  3890. pps-end-pos (point))
  3891. (/= (car state-2) 0)))
  3892. ;; Not at the right level.
  3893. (if (and (< (car state-2) 0)
  3894. ;; We stop above if we go out of a paren.
  3895. ;; Now check whether it precedes or is
  3896. ;; nested in the starting sexp.
  3897. (save-excursion
  3898. (setq state-2
  3899. (parse-partial-sexp
  3900. pps-end-pos paren-level-pos
  3901. nil nil state-2))
  3902. (< (car state-2) 0)))
  3903. ;; We've stopped short of the starting position
  3904. ;; so the hit was inside a nested list. Go up
  3905. ;; until we are at the right level.
  3906. (condition-case nil
  3907. (progn
  3908. (goto-char (scan-lists pos -1
  3909. (- (car state-2))))
  3910. (setq paren-level-pos (point))
  3911. (if (and limit (>= limit paren-level-pos))
  3912. (progn
  3913. (goto-char limit)
  3914. nil)
  3915. t))
  3916. (error
  3917. (goto-char (or limit (point-min)))
  3918. nil))
  3919. ;; The hit was outside the list at the start
  3920. ;; position. Go to the start of the list and exit.
  3921. (goto-char (1+ (elt state-2 1)))
  3922. nil))
  3923. ((c-beginning-of-macro limit)
  3924. ;; Inside a macro.
  3925. (if (< (point)
  3926. (or start-macro-beg
  3927. (setq start-macro-beg
  3928. (save-excursion
  3929. (goto-char start)
  3930. (c-beginning-of-macro limit)
  3931. (point)))))
  3932. t
  3933. ;; It's inside the same macro we started in so it's
  3934. ;; a relevant match.
  3935. (goto-char pos)
  3936. nil))))))
  3937. (> (point)
  3938. (progn
  3939. ;; Skip syntactic ws afterwards so that we don't stop at the
  3940. ;; end of a comment if `skip-chars' is something like "^/".
  3941. (c-backward-syntactic-ws)
  3942. (point)))))
  3943. ;; We might want to extend this with more useful return values in
  3944. ;; the future.
  3945. (/= (point) start)))
  3946. ;; The following is an alternative implementation of
  3947. ;; `c-syntactic-skip-backward' that uses backward movement to keep
  3948. ;; track of the syntactic context. It turned out to be generally
  3949. ;; slower than the one above which uses forward checks from earlier
  3950. ;; safe positions.
  3951. ;;
  3952. ;;(defconst c-ssb-stop-re
  3953. ;; ;; The regexp matching chars `c-syntactic-skip-backward' needs to
  3954. ;; ;; stop at to avoid going into comments and literals.
  3955. ;; (concat
  3956. ;; ;; Match comment end syntax and string literal syntax. Also match
  3957. ;; ;; '/' for block comment endings (not covered by comment end
  3958. ;; ;; syntax).
  3959. ;; "\\s>\\|/\\|\\s\""
  3960. ;; (if (memq 'gen-string-delim c-emacs-features)
  3961. ;; "\\|\\s|"
  3962. ;; "")
  3963. ;; (if (memq 'gen-comment-delim c-emacs-features)
  3964. ;; "\\|\\s!"
  3965. ;; "")))
  3966. ;;
  3967. ;;(defconst c-ssb-stop-paren-re
  3968. ;; ;; Like `c-ssb-stop-re' but also stops at paren chars.
  3969. ;; (concat c-ssb-stop-re "\\|\\s(\\|\\s)"))
  3970. ;;
  3971. ;;(defconst c-ssb-sexp-end-re
  3972. ;; ;; Regexp matching the ending syntax of a complex sexp.
  3973. ;; (concat c-string-limit-regexp "\\|\\s)"))
  3974. ;;
  3975. ;;(defun c-syntactic-skip-backward (skip-chars &optional limit paren-level)
  3976. ;; "Like `skip-chars-backward' but only look at syntactically relevant chars,
  3977. ;;i.e. don't stop at positions inside syntactic whitespace or string
  3978. ;;literals. Preprocessor directives are also ignored. However, if the
  3979. ;;point is within a comment, string literal or preprocessor directory to
  3980. ;;begin with, its contents is treated as syntactically relevant chars.
  3981. ;;If LIMIT is given, it limits the backward search and the point will be
  3982. ;;left there if no earlier position is found.
  3983. ;;
  3984. ;;If PAREN-LEVEL is non-nil, the function won't stop in nested paren
  3985. ;;sexps, and the search will also not go outside the current paren sexp.
  3986. ;;However, if LIMIT or the buffer limit is reached inside a nested paren
  3987. ;;then the point will be left at the limit.
  3988. ;;
  3989. ;;Non-nil is returned if the point moved, nil otherwise.
  3990. ;;
  3991. ;;Note that this function might do hidden buffer changes. See the
  3992. ;;comment at the start of cc-engine.el for more info."
  3993. ;;
  3994. ;; (save-restriction
  3995. ;; (when limit
  3996. ;; (narrow-to-region limit (point-max)))
  3997. ;;
  3998. ;; (let ((start (point)))
  3999. ;; (catch 'done
  4000. ;; (while (let ((last-pos (point))
  4001. ;; (stop-pos (progn
  4002. ;; (skip-chars-backward skip-chars)
  4003. ;; (point))))
  4004. ;;
  4005. ;; ;; Skip back over the same region as
  4006. ;; ;; `skip-chars-backward' above, but keep to
  4007. ;; ;; syntactically relevant positions.
  4008. ;; (goto-char last-pos)
  4009. ;; (while (and
  4010. ;; ;; `re-search-backward' with a single char regexp
  4011. ;; ;; should be fast.
  4012. ;; (re-search-backward
  4013. ;; (if paren-level c-ssb-stop-paren-re c-ssb-stop-re)
  4014. ;; stop-pos 'move)
  4015. ;;
  4016. ;; (progn
  4017. ;; (cond
  4018. ;; ((looking-at "\\s(")
  4019. ;; ;; `paren-level' is set and we've found the
  4020. ;; ;; start of the containing paren.
  4021. ;; (forward-char)
  4022. ;; (throw 'done t))
  4023. ;;
  4024. ;; ((looking-at c-ssb-sexp-end-re)
  4025. ;; ;; We're at the end of a string literal or paren
  4026. ;; ;; sexp (if `paren-level' is set).
  4027. ;; (forward-char)
  4028. ;; (condition-case nil
  4029. ;; (c-backward-sexp)
  4030. ;; (error
  4031. ;; (goto-char limit)
  4032. ;; (throw 'done t))))
  4033. ;;
  4034. ;; (t
  4035. ;; (forward-char)
  4036. ;; ;; At the end of some syntactic ws or possibly
  4037. ;; ;; after a plain '/' operator.
  4038. ;; (let ((pos (point)))
  4039. ;; (c-backward-syntactic-ws)
  4040. ;; (if (= pos (point))
  4041. ;; ;; Was a plain '/' operator. Go past it.
  4042. ;; (backward-char)))))
  4043. ;;
  4044. ;; (> (point) stop-pos))))
  4045. ;;
  4046. ;; ;; Now the point is either at `stop-pos' or at some
  4047. ;; ;; position further back if `stop-pos' was at a
  4048. ;; ;; syntactically irrelevant place.
  4049. ;;
  4050. ;; ;; Skip additional syntactic ws so that we don't stop
  4051. ;; ;; at the end of a comment if `skip-chars' is
  4052. ;; ;; something like "^/".
  4053. ;; (c-backward-syntactic-ws)
  4054. ;;
  4055. ;; (< (point) stop-pos))))
  4056. ;;
  4057. ;; ;; We might want to extend this with more useful return values
  4058. ;; ;; in the future.
  4059. ;; (/= (point) start))))
  4060. ;; Tools for handling comments and string literals.
  4061. (defun c-in-literal (&optional lim detect-cpp)
  4062. "Return the type of literal point is in, if any.
  4063. The return value is `c' if in a C-style comment, `c++' if in a C++
  4064. style comment, `string' if in a string literal, `pound' if DETECT-CPP
  4065. is non-nil and in a preprocessor line, or nil if somewhere else.
  4066. Optional LIM is used as the backward limit of the search. If omitted,
  4067. or nil, `c-beginning-of-defun' is used.
  4068. The last point calculated is cached if the cache is enabled, i.e. if
  4069. `c-in-literal-cache' is bound to a two element vector.
  4070. Note that this function might do hidden buffer changes. See the
  4071. comment at the start of cc-engine.el for more info."
  4072. (save-restriction
  4073. (widen)
  4074. (let* ((safe-place (c-state-semi-safe-place (point)))
  4075. (lit (c-state-pp-to-literal safe-place (point))))
  4076. (or (cadr lit)
  4077. (and detect-cpp
  4078. (save-excursion (c-beginning-of-macro))
  4079. 'pound)))))
  4080. (defun c-literal-limits (&optional lim near not-in-delimiter)
  4081. "Return a cons of the beginning and end positions of the comment or
  4082. string surrounding point (including both delimiters), or nil if point
  4083. isn't in one. If LIM is non-nil, it's used as the \"safe\" position
  4084. to start parsing from. If NEAR is non-nil, then the limits of any
  4085. literal next to point is returned. \"Next to\" means there's only
  4086. spaces and tabs between point and the literal. The search for such a
  4087. literal is done first in forward direction. If NOT-IN-DELIMITER is
  4088. non-nil, the case when point is inside a starting delimiter won't be
  4089. recognized. This only has effect for comments which have starting
  4090. delimiters with more than one character.
  4091. Note that this function might do hidden buffer changes. See the
  4092. comment at the start of cc-engine.el for more info."
  4093. (save-excursion
  4094. (let* ((pos (point))
  4095. (lim (or lim (c-state-semi-safe-place pos)))
  4096. (pp-to-lit (save-restriction
  4097. (widen)
  4098. (c-state-pp-to-literal lim pos not-in-delimiter)))
  4099. (state (car pp-to-lit))
  4100. (lit-limits (car (cddr pp-to-lit))))
  4101. (cond
  4102. (lit-limits)
  4103. (near
  4104. (goto-char pos)
  4105. ;; Search forward for a literal.
  4106. (skip-chars-forward " \t")
  4107. (cond
  4108. ((looking-at c-string-limit-regexp) ; String.
  4109. (cons (point) (or (c-safe (c-forward-sexp 1) (point))
  4110. (point-max))))
  4111. ((looking-at c-comment-start-regexp) ; Line or block comment.
  4112. (cons (point) (progn (c-forward-single-comment) (point))))
  4113. (t
  4114. ;; Search backward.
  4115. (skip-chars-backward " \t")
  4116. (let ((end (point)) beg)
  4117. (cond
  4118. ((save-excursion
  4119. (< (skip-syntax-backward c-string-syntax) 0)) ; String.
  4120. (setq beg (c-safe (c-backward-sexp 1) (point))))
  4121. ((and (c-safe (forward-char -2) t)
  4122. (looking-at "*/"))
  4123. ;; Block comment. Due to the nature of line
  4124. ;; comments, they will always be covered by the
  4125. ;; normal case above.
  4126. (goto-char end)
  4127. (c-backward-single-comment)
  4128. ;; If LIM is bogus, beg will be bogus.
  4129. (setq beg (point))))
  4130. (if beg (cons beg end))))))
  4131. ))))
  4132. ;; In case external callers use this; it did have a docstring.
  4133. (defalias 'c-literal-limits-fast 'c-literal-limits)
  4134. (defun c-collect-line-comments (range)
  4135. "If the argument is a cons of two buffer positions (such as returned by
  4136. `c-literal-limits'), and that range contains a C++ style line comment,
  4137. then an extended range is returned that contains all adjacent line
  4138. comments (i.e. all comments that starts in the same column with no
  4139. empty lines or non-whitespace characters between them). Otherwise the
  4140. argument is returned.
  4141. Note that this function might do hidden buffer changes. See the
  4142. comment at the start of cc-engine.el for more info."
  4143. (save-excursion
  4144. (condition-case nil
  4145. (if (and (consp range) (progn
  4146. (goto-char (car range))
  4147. (looking-at c-line-comment-starter)))
  4148. (let ((col (current-column))
  4149. (beg (point))
  4150. (bopl (c-point 'bopl))
  4151. (end (cdr range)))
  4152. ;; Got to take care in the backward direction to handle
  4153. ;; comments which are preceded by code.
  4154. (while (and (c-backward-single-comment)
  4155. (>= (point) bopl)
  4156. (looking-at c-line-comment-starter)
  4157. (= col (current-column)))
  4158. (setq beg (point)
  4159. bopl (c-point 'bopl)))
  4160. (goto-char end)
  4161. (while (and (progn (skip-chars-forward " \t")
  4162. (looking-at c-line-comment-starter))
  4163. (= col (current-column))
  4164. (prog1 (zerop (forward-line 1))
  4165. (setq end (point)))))
  4166. (cons beg end))
  4167. range)
  4168. (error range))))
  4169. (defun c-literal-type (range)
  4170. "Convenience function that given the result of `c-literal-limits',
  4171. returns nil or the type of literal that the range surrounds, one
  4172. of the symbols `c', `c++' or `string'. It's much faster than using
  4173. `c-in-literal' and is intended to be used when you need both the
  4174. type of a literal and its limits.
  4175. Note that this function might do hidden buffer changes. See the
  4176. comment at the start of cc-engine.el for more info."
  4177. (if (consp range)
  4178. (save-excursion
  4179. (goto-char (car range))
  4180. (cond ((looking-at c-string-limit-regexp) 'string)
  4181. ((or (looking-at "//") ; c++ line comment
  4182. (and (looking-at "\\s<") ; comment starter
  4183. (looking-at "#"))) ; awk comment.
  4184. 'c++)
  4185. (t 'c))) ; Assuming the range is valid.
  4186. range))
  4187. (defsubst c-determine-limit-get-base (start try-size)
  4188. ;; Get a "safe place" approximately TRY-SIZE characters before START.
  4189. ;; This doesn't preserve point.
  4190. (let* ((pos (max (- start try-size) (point-min)))
  4191. (base (c-state-semi-safe-place pos))
  4192. (s (parse-partial-sexp base pos)))
  4193. (if (or (nth 4 s) (nth 3 s)) ; comment or string
  4194. (nth 8 s)
  4195. (point))))
  4196. (defun c-determine-limit (how-far-back &optional start try-size)
  4197. ;; Return a buffer position HOW-FAR-BACK non-literal characters from START
  4198. ;; (default point). This is done by going back further in the buffer then
  4199. ;; searching forward for literals. The position found won't be in a
  4200. ;; literal. We start searching for the sought position TRY-SIZE (default
  4201. ;; twice HOW-FAR-BACK) bytes back from START. This function must be fast.
  4202. ;; :-)
  4203. (save-excursion
  4204. (let* ((start (or start (point)))
  4205. (try-size (or try-size (* 2 how-far-back)))
  4206. (base (c-determine-limit-get-base start try-size))
  4207. (pos base)
  4208. (s (parse-partial-sexp pos pos)) ; null state.
  4209. stack elt size
  4210. (count 0))
  4211. (while (< pos start)
  4212. ;; Move forward one literal each time round this loop.
  4213. ;; Move forward to the start of a comment or string.
  4214. (setq s (parse-partial-sexp
  4215. pos
  4216. start
  4217. nil ; target-depth
  4218. nil ; stop-before
  4219. s ; state
  4220. 'syntax-table)) ; stop-comment
  4221. ;; Gather details of the non-literal-bit - starting pos and size.
  4222. (setq size (- (if (or (nth 4 s) (nth 3 s))
  4223. (nth 8 s)
  4224. (point))
  4225. pos))
  4226. (if (> size 0)
  4227. (setq stack (cons (cons pos size) stack)))
  4228. ;; Move forward to the end of the comment/string.
  4229. (if (or (nth 4 s) (nth 3 s))
  4230. (setq s (parse-partial-sexp
  4231. (point)
  4232. start
  4233. nil ; target-depth
  4234. nil ; stop-before
  4235. s ; state
  4236. 'syntax-table))) ; stop-comment
  4237. (setq pos (point)))
  4238. ;; Now try and find enough non-literal characters recorded on the stack.
  4239. ;; Go back one recorded literal each time round this loop.
  4240. (while (and (< count how-far-back)
  4241. stack)
  4242. (setq elt (car stack)
  4243. stack (cdr stack))
  4244. (setq count (+ count (cdr elt))))
  4245. ;; Have we found enough yet?
  4246. (cond
  4247. ((>= count how-far-back)
  4248. (+ (car elt) (- count how-far-back)))
  4249. ((eq base (point-min))
  4250. (point-min))
  4251. (t
  4252. (c-determine-limit (- how-far-back count) base try-size))))))
  4253. (defun c-determine-+ve-limit (how-far &optional start-pos)
  4254. ;; Return a buffer position about HOW-FAR non-literal characters forward
  4255. ;; from START-POS (default point), which must not be inside a literal.
  4256. (save-excursion
  4257. (let ((pos (or start-pos (point)))
  4258. (count how-far)
  4259. (s (parse-partial-sexp (point) (point)))) ; null state
  4260. (while (and (not (eobp))
  4261. (> count 0))
  4262. ;; Scan over counted characters.
  4263. (setq s (parse-partial-sexp
  4264. pos
  4265. (min (+ pos count) (point-max))
  4266. nil ; target-depth
  4267. nil ; stop-before
  4268. s ; state
  4269. 'syntax-table)) ; stop-comment
  4270. (setq count (- count (- (point) pos) 1)
  4271. pos (point))
  4272. ;; Scan over literal characters.
  4273. (if (nth 8 s)
  4274. (setq s (parse-partial-sexp
  4275. pos
  4276. (point-max)
  4277. nil ; target-depth
  4278. nil ; stop-before
  4279. s ; state
  4280. 'syntax-table) ; stop-comment
  4281. pos (point))))
  4282. (point))))
  4283. ;; `c-find-decl-spots' and accompanying stuff.
  4284. ;; Variables used in `c-find-decl-spots' to cache the search done for
  4285. ;; the first declaration in the last call. When that function starts,
  4286. ;; it needs to back up over syntactic whitespace to look at the last
  4287. ;; token before the region being searched. That can sometimes cause
  4288. ;; moves back and forth over a quite large region of comments and
  4289. ;; macros, which would be repeated for each changed character when
  4290. ;; we're called during fontification, since font-lock refontifies the
  4291. ;; current line for each change. Thus it's worthwhile to cache the
  4292. ;; first match.
  4293. ;;
  4294. ;; `c-find-decl-syntactic-pos' is a syntactically relevant position in
  4295. ;; the syntactic whitespace less or equal to some start position.
  4296. ;; There's no cached value if it's nil.
  4297. ;;
  4298. ;; `c-find-decl-match-pos' is the match position if
  4299. ;; `c-find-decl-prefix-search' matched before the syntactic whitespace
  4300. ;; at `c-find-decl-syntactic-pos', or nil if there's no such match.
  4301. (defvar c-find-decl-syntactic-pos nil)
  4302. (make-variable-buffer-local 'c-find-decl-syntactic-pos)
  4303. (defvar c-find-decl-match-pos nil)
  4304. (make-variable-buffer-local 'c-find-decl-match-pos)
  4305. (defsubst c-invalidate-find-decl-cache (change-min-pos)
  4306. (and c-find-decl-syntactic-pos
  4307. (< change-min-pos c-find-decl-syntactic-pos)
  4308. (setq c-find-decl-syntactic-pos nil)))
  4309. ; (defface c-debug-decl-spot-face
  4310. ; '((t (:background "Turquoise")))
  4311. ; "Debug face to mark the spots where `c-find-decl-spots' stopped.")
  4312. ; (defface c-debug-decl-sws-face
  4313. ; '((t (:background "Khaki")))
  4314. ; "Debug face to mark the syntactic whitespace between the declaration
  4315. ; spots and the preceding token end.")
  4316. (defmacro c-debug-put-decl-spot-faces (match-pos decl-pos)
  4317. (when (facep 'c-debug-decl-spot-face)
  4318. `(c-save-buffer-state ((match-pos ,match-pos) (decl-pos ,decl-pos))
  4319. (c-debug-add-face (max match-pos (point-min)) decl-pos
  4320. 'c-debug-decl-sws-face)
  4321. (c-debug-add-face decl-pos (min (1+ decl-pos) (point-max))
  4322. 'c-debug-decl-spot-face))))
  4323. (defmacro c-debug-remove-decl-spot-faces (beg end)
  4324. (when (facep 'c-debug-decl-spot-face)
  4325. `(c-save-buffer-state ()
  4326. (c-debug-remove-face ,beg ,end 'c-debug-decl-spot-face)
  4327. (c-debug-remove-face ,beg ,end 'c-debug-decl-sws-face))))
  4328. (defmacro c-find-decl-prefix-search ()
  4329. ;; Macro used inside `c-find-decl-spots'. It ought to be a defun,
  4330. ;; but it contains lots of free variables that refer to things
  4331. ;; inside `c-find-decl-spots'. The point is left at `cfd-match-pos'
  4332. ;; if there is a match, otherwise at `cfd-limit'.
  4333. ;;
  4334. ;; The macro moves point forward to the next putative start of a declaration
  4335. ;; or cfd-limit. This decl start is the next token after a "declaration
  4336. ;; prefix". The declaration prefix is the earlier of `cfd-prop-match' and
  4337. ;; `cfd-re-match'. `cfd-match-pos' is set to the decl prefix.
  4338. ;;
  4339. ;; This macro might do hidden buffer changes.
  4340. '(progn
  4341. ;; Find the next property match position if we haven't got one already.
  4342. (unless cfd-prop-match
  4343. (save-excursion
  4344. (while (progn
  4345. (goto-char (c-next-single-property-change
  4346. (point) 'c-type nil cfd-limit))
  4347. (and (< (point) cfd-limit)
  4348. (not (eq (c-get-char-property (1- (point)) 'c-type)
  4349. 'c-decl-end)))))
  4350. (setq cfd-prop-match (point))))
  4351. ;; Find the next `c-decl-prefix-or-start-re' match if we haven't
  4352. ;; got one already.
  4353. (unless cfd-re-match
  4354. (if (> cfd-re-match-end (point))
  4355. (goto-char cfd-re-match-end))
  4356. ;; Each time round, the next `while' moves forward over a pseudo match
  4357. ;; of `c-decl-prefix-or-start-re' which is either inside a literal, or
  4358. ;; is a ":" not preceded by "public", etc.. `cfd-re-match' and
  4359. ;; `cfd-re-match-end' get set.
  4360. (while
  4361. (progn
  4362. (setq cfd-re-match-end (re-search-forward c-decl-prefix-or-start-re
  4363. cfd-limit 'move))
  4364. (cond
  4365. ((null cfd-re-match-end)
  4366. ;; No match. Finish up and exit the loop.
  4367. (setq cfd-re-match cfd-limit)
  4368. nil)
  4369. ((c-got-face-at
  4370. (if (setq cfd-re-match (match-end 1))
  4371. ;; Matched the end of a token preceding a decl spot.
  4372. (progn
  4373. (goto-char cfd-re-match)
  4374. (1- cfd-re-match))
  4375. ;; Matched a token that start a decl spot.
  4376. (goto-char (match-beginning 0))
  4377. (point))
  4378. c-literal-faces)
  4379. ;; Pseudo match inside a comment or string literal. Skip out
  4380. ;; of comments and string literals.
  4381. (while (progn
  4382. (goto-char (c-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. t) ; Continue the loop over pseudo matches.
  4387. ((and (match-string 1)
  4388. (string= (match-string 1) ":")
  4389. (save-excursion
  4390. (or (/= (c-backward-token-2 2) 0) ; no search limit. :-(
  4391. (not (looking-at c-decl-start-colon-kwd-re)))))
  4392. ;; Found a ":" which isn't part of "public:", etc.
  4393. t)
  4394. (t nil)))) ;; Found a real match. Exit the pseudo-match loop.
  4395. ;; If our match was at the decl start, we have to back up over the
  4396. ;; preceding syntactic ws to set `cfd-match-pos' and to catch
  4397. ;; any decl spots in the syntactic ws.
  4398. (unless cfd-re-match
  4399. (c-backward-syntactic-ws)
  4400. (setq cfd-re-match (point))))
  4401. ;; Choose whichever match is closer to the start.
  4402. (if (< cfd-re-match cfd-prop-match)
  4403. (setq cfd-match-pos cfd-re-match
  4404. cfd-re-match nil)
  4405. (setq cfd-match-pos cfd-prop-match
  4406. cfd-prop-match nil))
  4407. (goto-char cfd-match-pos)
  4408. (when (< cfd-match-pos cfd-limit)
  4409. ;; Skip forward past comments only so we don't skip macros.
  4410. (c-forward-comments)
  4411. ;; Set the position to continue at. We can avoid going over
  4412. ;; the comments skipped above a second time, but it's possible
  4413. ;; that the comment skipping has taken us past `cfd-prop-match'
  4414. ;; since the property might be used inside comments.
  4415. (setq cfd-continue-pos (if cfd-prop-match
  4416. (min cfd-prop-match (point))
  4417. (point))))))
  4418. (defun c-find-decl-spots (cfd-limit cfd-decl-re cfd-face-checklist cfd-fun)
  4419. ;; Call CFD-FUN for each possible spot for a declaration, cast or
  4420. ;; label from the point to CFD-LIMIT.
  4421. ;;
  4422. ;; CFD-FUN is called with point at the start of the spot. It's passed two
  4423. ;; arguments: The first is the end position of the token preceding the spot,
  4424. ;; or 0 for the implicit match at bob. The second is a flag that is t when
  4425. ;; the match is inside a macro. Point should be moved forward by at least
  4426. ;; one token.
  4427. ;;
  4428. ;; If CFD-FUN adds `c-decl-end' properties somewhere below the current spot,
  4429. ;; it should return non-nil to ensure that the next search will find them.
  4430. ;;
  4431. ;; Such a spot is:
  4432. ;; o The first token after bob.
  4433. ;; o The first token after the end of submatch 1 in
  4434. ;; `c-decl-prefix-or-start-re' when that submatch matches. This
  4435. ;; submatch is typically a (L or R) brace or paren, a ;, or a ,.
  4436. ;; o The start of each `c-decl-prefix-or-start-re' match when
  4437. ;; submatch 1 doesn't match. This is, for example, the keyword
  4438. ;; "class" in Pike.
  4439. ;; o The start of a previously recognized declaration; "recognized"
  4440. ;; means that the last char of the previous token has a `c-type'
  4441. ;; text property with the value `c-decl-end'; this only holds
  4442. ;; when `c-type-decl-end-used' is set.
  4443. ;;
  4444. ;; Only a spot that match CFD-DECL-RE and whose face is in the
  4445. ;; CFD-FACE-CHECKLIST list causes CFD-FUN to be called. The face
  4446. ;; check is disabled if CFD-FACE-CHECKLIST is nil.
  4447. ;;
  4448. ;; If the match is inside a macro then the buffer is narrowed to the
  4449. ;; end of it, so that CFD-FUN can investigate the following tokens
  4450. ;; without matching something that begins inside a macro and ends
  4451. ;; outside it. It's to avoid this work that the CFD-DECL-RE and
  4452. ;; CFD-FACE-CHECKLIST checks exist.
  4453. ;;
  4454. ;; The spots are visited approximately in order from top to bottom.
  4455. ;; It's however the positions where `c-decl-prefix-or-start-re'
  4456. ;; matches and where `c-decl-end' properties are found that are in
  4457. ;; order. Since the spots often are at the following token, they
  4458. ;; might be visited out of order insofar as more spots are reported
  4459. ;; later on within the syntactic whitespace between the match
  4460. ;; positions and their spots.
  4461. ;;
  4462. ;; It's assumed that comments and strings are fontified in the
  4463. ;; searched range.
  4464. ;;
  4465. ;; This is mainly used in fontification, and so has an elaborate
  4466. ;; cache to handle repeated calls from the same start position; see
  4467. ;; the variables above.
  4468. ;;
  4469. ;; All variables in this function begin with `cfd-' to avoid name
  4470. ;; collision with the (dynamically bound) variables used in CFD-FUN.
  4471. ;;
  4472. ;; This function might do hidden buffer changes.
  4473. (let ((cfd-start-pos (point)) ; never changed
  4474. (cfd-buffer-end (point-max))
  4475. ;; The end of the token preceding the decl spot last found
  4476. ;; with `c-decl-prefix-or-start-re'. `cfd-limit' if there's
  4477. ;; no match.
  4478. cfd-re-match
  4479. ;; The end position of the last `c-decl-prefix-or-start-re'
  4480. ;; match. If this is greater than `cfd-continue-pos', the
  4481. ;; next regexp search is started here instead.
  4482. (cfd-re-match-end (point-min))
  4483. ;; The end of the last `c-decl-end' found by
  4484. ;; `c-find-decl-prefix-search'. `cfd-limit' if there's no
  4485. ;; match. If searching for the property isn't needed then we
  4486. ;; disable it by setting it to `cfd-limit' directly.
  4487. (cfd-prop-match (unless c-type-decl-end-used cfd-limit))
  4488. ;; The end of the token preceding the decl spot last found by
  4489. ;; `c-find-decl-prefix-search'. 0 for the implicit match at
  4490. ;; bob. `cfd-limit' if there's no match. In other words,
  4491. ;; this is the minimum of `cfd-re-match' and `cfd-prop-match'.
  4492. (cfd-match-pos cfd-limit)
  4493. ;; The position to continue searching at.
  4494. cfd-continue-pos
  4495. ;; The position of the last "real" token we've stopped at.
  4496. ;; This can be greater than `cfd-continue-pos' when we get
  4497. ;; hits inside macros or at `c-decl-end' positions inside
  4498. ;; comments.
  4499. (cfd-token-pos 0)
  4500. ;; The end position of the last entered macro.
  4501. (cfd-macro-end 0))
  4502. ;; Initialize by finding a syntactically relevant start position
  4503. ;; before the point, and do the first `c-decl-prefix-or-start-re'
  4504. ;; search unless we're at bob.
  4505. (let (start-in-literal start-in-macro syntactic-pos)
  4506. ;; Must back up a bit since we look for the end of the previous
  4507. ;; statement or declaration, which is earlier than the first
  4508. ;; returned match.
  4509. ;; This `cond' moves back over any literals or macros. It has special
  4510. ;; handling for when the region being searched is entirely within a
  4511. ;; macro. It sets `cfd-continue-pos' (unless we've reached
  4512. ;; `cfd-limit').
  4513. (cond
  4514. ;; First we need to move to a syntactically relevant position.
  4515. ;; Begin by backing out of comment or string literals.
  4516. ;;
  4517. ;; This arm of the cond actually triggers if we're in a literal,
  4518. ;; and cfd-limit is at most at BONL.
  4519. ((and
  4520. ;; This arm of the `and' moves backwards out of a literal when
  4521. ;; the face at point is a literal face. In this case, its value
  4522. ;; is always non-nil.
  4523. (when (c-got-face-at (point) c-literal-faces)
  4524. ;; Try to use the faces to back up to the start of the
  4525. ;; literal. FIXME: What if the point is on a declaration
  4526. ;; inside a comment?
  4527. (while (and (not (bobp))
  4528. (c-got-face-at (1- (point)) c-literal-faces))
  4529. (goto-char (previous-single-property-change
  4530. (point) 'face nil (point-min))))
  4531. ;; XEmacs doesn't fontify the quotes surrounding string
  4532. ;; literals.
  4533. (and (featurep 'xemacs)
  4534. (eq (get-text-property (point) 'face)
  4535. 'font-lock-string-face)
  4536. (not (bobp))
  4537. (progn (backward-char)
  4538. (not (looking-at c-string-limit-regexp)))
  4539. (forward-char))
  4540. ;; Don't trust the literal to contain only literal faces
  4541. ;; (the font lock package might not have fontified the
  4542. ;; start of it at all, for instance) so check that we have
  4543. ;; arrived at something that looks like a start or else
  4544. ;; resort to `c-literal-limits'.
  4545. (unless (looking-at c-literal-start-regexp)
  4546. (let ((range (c-literal-limits)))
  4547. (if range (goto-char (car range)))))
  4548. (setq start-in-literal (point))) ; end of `and' arm.
  4549. ;; The start is in a literal. If the limit is in the same
  4550. ;; one we don't have to find a syntactic position etc. We
  4551. ;; only check that if the limit is at or before bonl to save
  4552. ;; time; it covers the by far most common case when font-lock
  4553. ;; refontifies the current line only.
  4554. (<= cfd-limit (c-point 'bonl cfd-start-pos))
  4555. (save-excursion
  4556. (goto-char cfd-start-pos)
  4557. (while (progn
  4558. (goto-char (c-next-single-property-change
  4559. (point) 'face nil cfd-limit))
  4560. (and (< (point) cfd-limit)
  4561. (c-got-face-at (point) c-literal-faces))))
  4562. (= (point) cfd-limit))) ; end of `cond' arm condition
  4563. ;; Completely inside a literal. Set up variables to trig the
  4564. ;; (< cfd-continue-pos cfd-start-pos) case below and it'll
  4565. ;; find a suitable start position.
  4566. (setq cfd-continue-pos start-in-literal)) ; end of `cond' arm
  4567. ;; Check if the region might be completely inside a macro, to
  4568. ;; optimize that like the completely-inside-literal above.
  4569. ((save-excursion
  4570. (and (= (forward-line 1) 0)
  4571. (bolp) ; forward-line has funny behavior at eob.
  4572. (>= (point) cfd-limit)
  4573. (progn (backward-char)
  4574. (eq (char-before) ?\\))))
  4575. ;; (Maybe) completely inside a macro. Only need to trig the
  4576. ;; (< cfd-continue-pos cfd-start-pos) case below to make it
  4577. ;; set things up.
  4578. (setq cfd-continue-pos (1- cfd-start-pos)
  4579. start-in-macro t))
  4580. ;; The default arm of the `cond' moves back over any macro we're in
  4581. ;; and over any syntactic WS. It sets `c-find-decl-syntactic-pos'.
  4582. (t
  4583. ;; Back out of any macro so we don't miss any declaration
  4584. ;; that could follow after it.
  4585. (when (c-beginning-of-macro)
  4586. (setq start-in-macro t))
  4587. ;; Now we're at a proper syntactically relevant position so we
  4588. ;; can use the cache. But first clear it if it applied
  4589. ;; further down.
  4590. (c-invalidate-find-decl-cache cfd-start-pos)
  4591. (setq syntactic-pos (point))
  4592. (unless (eq syntactic-pos c-find-decl-syntactic-pos)
  4593. ;; Don't have to do this if the cache is relevant here,
  4594. ;; typically if the same line is refontified again. If
  4595. ;; we're just some syntactic whitespace further down we can
  4596. ;; still use the cache to limit the skipping.
  4597. (c-backward-syntactic-ws c-find-decl-syntactic-pos))
  4598. ;; If we hit `c-find-decl-syntactic-pos' and
  4599. ;; `c-find-decl-match-pos' is set then we install the cached
  4600. ;; values. If we hit `c-find-decl-syntactic-pos' and
  4601. ;; `c-find-decl-match-pos' is nil then we know there's no decl
  4602. ;; prefix in the whitespace before `c-find-decl-syntactic-pos'
  4603. ;; and so we can continue the search from this point. If we
  4604. ;; didn't hit `c-find-decl-syntactic-pos' then we're now in
  4605. ;; the right spot to begin searching anyway.
  4606. (if (and (eq (point) c-find-decl-syntactic-pos)
  4607. c-find-decl-match-pos)
  4608. (setq cfd-match-pos c-find-decl-match-pos
  4609. cfd-continue-pos syntactic-pos)
  4610. (setq c-find-decl-syntactic-pos syntactic-pos)
  4611. (when (if (bobp)
  4612. ;; Always consider bob a match to get the first
  4613. ;; declaration in the file. Do this separately instead of
  4614. ;; letting `c-decl-prefix-or-start-re' match bob, so that
  4615. ;; regexp always can consume at least one character to
  4616. ;; ensure that we won't get stuck in an infinite loop.
  4617. (setq cfd-re-match 0)
  4618. (backward-char)
  4619. (c-beginning-of-current-token)
  4620. (< (point) cfd-limit))
  4621. ;; Do an initial search now. In the bob case above it's
  4622. ;; only done to search for a `c-decl-end' spot.
  4623. (c-find-decl-prefix-search)) ; sets cfd-continue-pos
  4624. (setq c-find-decl-match-pos (and (< cfd-match-pos cfd-start-pos)
  4625. cfd-match-pos))))) ; end of `cond'
  4626. ;; Advance `cfd-continue-pos' if it's before the start position.
  4627. ;; The closest continue position that might have effect at or
  4628. ;; after the start depends on what we started in. This also
  4629. ;; finds a suitable start position in the special cases when the
  4630. ;; region is completely within a literal or macro.
  4631. (when (and cfd-continue-pos (< cfd-continue-pos cfd-start-pos))
  4632. (cond
  4633. (start-in-macro
  4634. ;; If we're in a macro then it's the closest preceding token
  4635. ;; in the macro. Check this before `start-in-literal',
  4636. ;; since if we're inside a literal in a macro, the preceding
  4637. ;; token is earlier than any `c-decl-end' spot inside the
  4638. ;; literal (comment).
  4639. (goto-char (or start-in-literal cfd-start-pos))
  4640. ;; The only syntactic ws in macros are comments.
  4641. (c-backward-comments)
  4642. (backward-char)
  4643. (c-beginning-of-current-token))
  4644. (start-in-literal
  4645. ;; If we're in a comment it can only be the closest
  4646. ;; preceding `c-decl-end' position within that comment, if
  4647. ;; any. Go back to the beginning of such a property so that
  4648. ;; `c-find-decl-prefix-search' will find the end of it.
  4649. ;; (Can't stop at the end and install it directly on
  4650. ;; `cfd-prop-match' since that variable might be cleared
  4651. ;; after `cfd-fun' below.)
  4652. ;;
  4653. ;; Note that if the literal is a string then the property
  4654. ;; search will simply skip to the beginning of it right
  4655. ;; away.
  4656. (if (not c-type-decl-end-used)
  4657. (goto-char start-in-literal)
  4658. (goto-char cfd-start-pos)
  4659. (while (progn
  4660. (goto-char (previous-single-property-change
  4661. (point) 'c-type nil start-in-literal))
  4662. (and (> (point) start-in-literal)
  4663. (not (eq (c-get-char-property (point) 'c-type)
  4664. 'c-decl-end))))))
  4665. (when (= (point) start-in-literal)
  4666. ;; Didn't find any property inside the comment, so we can
  4667. ;; skip it entirely. (This won't skip past a string, but
  4668. ;; that'll be handled quickly by the next
  4669. ;; `c-find-decl-prefix-search' anyway.)
  4670. (c-forward-single-comment)
  4671. (if (> (point) cfd-limit)
  4672. (goto-char cfd-limit))))
  4673. (t
  4674. ;; If we started in normal code, the only match that might
  4675. ;; apply before the start is what we already got in
  4676. ;; `cfd-match-pos' so we can continue at the start position.
  4677. ;; (Note that we don't get here if the first match is below
  4678. ;; it.)
  4679. (goto-char cfd-start-pos))) ; end of `cond'
  4680. ;; Delete found matches if they are before our new continue
  4681. ;; position, so that `c-find-decl-prefix-search' won't back up
  4682. ;; to them later on.
  4683. (setq cfd-continue-pos (point))
  4684. (when (and cfd-re-match (< cfd-re-match cfd-continue-pos))
  4685. (setq cfd-re-match nil))
  4686. (when (and cfd-prop-match (< cfd-prop-match cfd-continue-pos))
  4687. (setq cfd-prop-match nil))) ; end of `when'
  4688. (if syntactic-pos
  4689. ;; This is the normal case and we got a proper syntactic
  4690. ;; position. If there's a match then it's always outside
  4691. ;; macros and comments, so advance to the next token and set
  4692. ;; `cfd-token-pos'. The loop below will later go back using
  4693. ;; `cfd-continue-pos' to fix declarations inside the
  4694. ;; syntactic ws.
  4695. (when (and cfd-match-pos (< cfd-match-pos syntactic-pos))
  4696. (goto-char syntactic-pos)
  4697. (c-forward-syntactic-ws)
  4698. (and cfd-continue-pos
  4699. (< cfd-continue-pos (point))
  4700. (setq cfd-token-pos (point))))
  4701. ;; Have one of the special cases when the region is completely
  4702. ;; within a literal or macro. `cfd-continue-pos' is set to a
  4703. ;; good start position for the search, so do it.
  4704. (c-find-decl-prefix-search)))
  4705. ;; Now loop, one decl spot per iteration. We already have the first
  4706. ;; match in `cfd-match-pos'.
  4707. (while (progn
  4708. ;; Go forward over "false matches", one per iteration.
  4709. (while (and
  4710. (< cfd-match-pos cfd-limit)
  4711. (or
  4712. ;; Kludge to filter out matches on the "<" that
  4713. ;; aren't open parens, for the sake of languages
  4714. ;; that got `c-recognize-<>-arglists' set.
  4715. (and (eq (char-before cfd-match-pos) ?<)
  4716. (not (c-get-char-property (1- cfd-match-pos)
  4717. 'syntax-table)))
  4718. ;; If `cfd-continue-pos' is less or equal to
  4719. ;; `cfd-token-pos', we've got a hit inside a macro
  4720. ;; that's in the syntactic whitespace before the last
  4721. ;; "real" declaration we've checked. If they're equal
  4722. ;; we've arrived at the declaration a second time, so
  4723. ;; there's nothing to do.
  4724. (= cfd-continue-pos cfd-token-pos)
  4725. (progn
  4726. ;; If `cfd-continue-pos' is less than `cfd-token-pos'
  4727. ;; we're still searching for declarations embedded in
  4728. ;; the syntactic whitespace. In that case we need
  4729. ;; only to skip comments and not macros, since they
  4730. ;; can't be nested, and that's already been done in
  4731. ;; `c-find-decl-prefix-search'.
  4732. (when (> cfd-continue-pos cfd-token-pos)
  4733. (c-forward-syntactic-ws)
  4734. (setq cfd-token-pos (point)))
  4735. ;; Continue if the following token fails the
  4736. ;; CFD-DECL-RE and CFD-FACE-CHECKLIST checks.
  4737. (when (or (>= (point) cfd-limit)
  4738. (not (looking-at cfd-decl-re))
  4739. (and cfd-face-checklist
  4740. (not (c-got-face-at
  4741. (point) cfd-face-checklist))))
  4742. (goto-char cfd-continue-pos)
  4743. t)))
  4744. (< (point) cfd-limit)) ; end of "false matches" condition
  4745. (c-find-decl-prefix-search)) ; end of "false matches" loop
  4746. (< (point) cfd-limit)) ; end of condition for "decl-spot" while
  4747. (when (and
  4748. (>= (point) cfd-start-pos)
  4749. (progn
  4750. ;; Narrow to the end of the macro if we got a hit inside
  4751. ;; one, to avoid recognizing things that start inside the
  4752. ;; macro and end outside it.
  4753. (when (> cfd-match-pos cfd-macro-end)
  4754. ;; Not in the same macro as in the previous round.
  4755. (save-excursion
  4756. (goto-char cfd-match-pos)
  4757. (setq cfd-macro-end
  4758. (if (save-excursion (and (c-beginning-of-macro)
  4759. (< (point) cfd-match-pos)))
  4760. (progn (c-end-of-macro)
  4761. (point))
  4762. 0))))
  4763. (if (zerop cfd-macro-end)
  4764. t
  4765. (if (> cfd-macro-end (point))
  4766. (progn (narrow-to-region (point-min) cfd-macro-end)
  4767. t)
  4768. ;; The matched token was the last thing in the macro,
  4769. ;; so the whole match is bogus.
  4770. (setq cfd-macro-end 0)
  4771. nil)))) ; end of when condition
  4772. (c-debug-put-decl-spot-faces cfd-match-pos (point))
  4773. (if (funcall cfd-fun cfd-match-pos (/= cfd-macro-end 0))
  4774. (setq cfd-prop-match nil))
  4775. (when (/= cfd-macro-end 0)
  4776. ;; Restore limits if we did macro narrowing above.
  4777. (narrow-to-region (point-min) cfd-buffer-end)))
  4778. (goto-char cfd-continue-pos)
  4779. (if (= cfd-continue-pos cfd-limit)
  4780. (setq cfd-match-pos cfd-limit)
  4781. (c-find-decl-prefix-search))))) ; Moves point, sets cfd-continue-pos,
  4782. ; cfd-match-pos, etc.
  4783. ;; A cache for found types.
  4784. ;; Buffer local variable that contains an obarray with the types we've
  4785. ;; found. If a declaration is recognized somewhere we record the
  4786. ;; fully qualified identifier in it to recognize it as a type
  4787. ;; elsewhere in the file too. This is not accurate since we do not
  4788. ;; bother with the scoping rules of the languages, but in practice the
  4789. ;; same name is seldom used as both a type and something else in a
  4790. ;; file, and we only use this as a last resort in ambiguous cases (see
  4791. ;; `c-forward-decl-or-cast-1').
  4792. ;;
  4793. ;; Not every type need be in this cache. However, things which have
  4794. ;; ceased to be types must be removed from it.
  4795. ;;
  4796. ;; Template types in C++ are added here too but with the template
  4797. ;; arglist replaced with "<>" in references or "<" for the one in the
  4798. ;; primary type. E.g. the type "Foo<A,B>::Bar<C>" is stored as
  4799. ;; "Foo<>::Bar<". This avoids storing very long strings (since C++
  4800. ;; template specs can be fairly sized programs in themselves) and
  4801. ;; improves the hit ratio (it's a type regardless of the template
  4802. ;; args; it's just not the same type, but we're only interested in
  4803. ;; recognizing types, not telling distinct types apart). Note that
  4804. ;; template types in references are added here too; from the example
  4805. ;; above there will also be an entry "Foo<".
  4806. (defvar c-found-types nil)
  4807. (make-variable-buffer-local 'c-found-types)
  4808. (defsubst c-clear-found-types ()
  4809. ;; Clears `c-found-types'.
  4810. (setq c-found-types (make-vector 53 0)))
  4811. (defun c-add-type (from to)
  4812. ;; Add the given region as a type in `c-found-types'. If the region
  4813. ;; doesn't match an existing type but there is a type which is equal
  4814. ;; to the given one except that the last character is missing, then
  4815. ;; the shorter type is removed. That's done to avoid adding all
  4816. ;; prefixes of a type as it's being entered and font locked. This
  4817. ;; doesn't cover cases like when characters are removed from a type
  4818. ;; or added in the middle. We'd need the position of point when the
  4819. ;; font locking is invoked to solve this well.
  4820. ;;
  4821. ;; This function might do hidden buffer changes.
  4822. (let ((type (c-syntactic-content from to c-recognize-<>-arglists)))
  4823. (unless (intern-soft type c-found-types)
  4824. (unintern (substring type 0 -1) c-found-types)
  4825. (intern type c-found-types))))
  4826. (defun c-unfind-type (name)
  4827. ;; Remove the "NAME" from c-found-types, if present.
  4828. (unintern name c-found-types))
  4829. (defsubst c-check-type (from to)
  4830. ;; Return non-nil if the given region contains a type in
  4831. ;; `c-found-types'.
  4832. ;;
  4833. ;; This function might do hidden buffer changes.
  4834. (intern-soft (c-syntactic-content from to c-recognize-<>-arglists)
  4835. c-found-types))
  4836. (defun c-list-found-types ()
  4837. ;; Return all the types in `c-found-types' as a sorted list of
  4838. ;; strings.
  4839. (let (type-list)
  4840. (mapatoms (lambda (type)
  4841. (setq type-list (cons (symbol-name type)
  4842. type-list)))
  4843. c-found-types)
  4844. (sort type-list 'string-lessp)))
  4845. ;; Shut up the byte compiler.
  4846. (defvar c-maybe-stale-found-type)
  4847. (defun c-trim-found-types (beg end old-len)
  4848. ;; An after change function which, in conjunction with the info in
  4849. ;; c-maybe-stale-found-type (set in c-before-change), removes a type
  4850. ;; from `c-found-types', should this type have become stale. For
  4851. ;; example, this happens to "foo" when "foo \n bar();" becomes
  4852. ;; "foo(); \n bar();". Such stale types, if not removed, foul up
  4853. ;; the fontification.
  4854. ;;
  4855. ;; Have we, perhaps, added non-ws characters to the front/back of a found
  4856. ;; type?
  4857. (when (> end beg)
  4858. (save-excursion
  4859. (when (< end (point-max))
  4860. (goto-char end)
  4861. (if (and (c-beginning-of-current-token) ; only moves when we started in the middle
  4862. (progn (goto-char end)
  4863. (c-end-of-current-token)))
  4864. (c-unfind-type (buffer-substring-no-properties
  4865. end (point)))))
  4866. (when (> beg (point-min))
  4867. (goto-char beg)
  4868. (if (and (c-end-of-current-token) ; only moves when we started in the middle
  4869. (progn (goto-char beg)
  4870. (c-beginning-of-current-token)))
  4871. (c-unfind-type (buffer-substring-no-properties
  4872. (point) beg))))))
  4873. (if c-maybe-stale-found-type ; e.g. (c-decl-id-start "foo" 97 107 " (* ooka) " "o")
  4874. (cond
  4875. ;; Changing the amount of (already existing) whitespace - don't do anything.
  4876. ((and (c-partial-ws-p beg end)
  4877. (or (= beg end) ; removal of WS
  4878. (string-match "^[ \t\n\r\f\v]*$" (nth 5 c-maybe-stale-found-type)))))
  4879. ;; The syntactic relationship which defined a "found type" has been
  4880. ;; destroyed.
  4881. ((eq (car c-maybe-stale-found-type) 'c-decl-id-start)
  4882. (c-unfind-type (cadr c-maybe-stale-found-type)))
  4883. ;; ((eq (car c-maybe-stale-found-type) 'c-decl-type-start) FIXME!!!
  4884. )))
  4885. ;; Setting and removing syntax properties on < and > in languages (C++
  4886. ;; and Java) where they can be template/generic delimiters as well as
  4887. ;; their normal meaning of "less/greater than".
  4888. ;; Normally, < and > have syntax 'punctuation'. When they are found to
  4889. ;; be delimiters, they are marked as such with the category properties
  4890. ;; c-<-as-paren-syntax, c->-as-paren-syntax respectively.
  4891. ;; STRATEGY:
  4892. ;;
  4893. ;; It is impossible to determine with certainty whether a <..> pair in
  4894. ;; C++ is two comparison operators or is template delimiters, unless
  4895. ;; one duplicates a lot of a C++ compiler. For example, the following
  4896. ;; code fragment:
  4897. ;;
  4898. ;; foo (a < b, c > d) ;
  4899. ;;
  4900. ;; could be a function call with two integer parameters (each a
  4901. ;; relational expression), or it could be a constructor for class foo
  4902. ;; taking one parameter d of templated type "a < b, c >". They are
  4903. ;; somewhat easier to distinguish in Java.
  4904. ;;
  4905. ;; The strategy now (2010-01) adopted is to mark and unmark < and
  4906. ;; > IN MATCHING PAIRS ONLY. [Previously, they were marked
  4907. ;; individually when their context so indicated. This gave rise to
  4908. ;; intractable problems when one of a matching pair was deleted, or
  4909. ;; pulled into a literal.]
  4910. ;;
  4911. ;; At each buffer change, the syntax-table properties are removed in a
  4912. ;; before-change function and reapplied, when needed, in an
  4913. ;; after-change function. It is far more important that the
  4914. ;; properties get removed when they they are spurious than that they
  4915. ;; be present when wanted.
  4916. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4917. (defun c-clear-<-pair-props (&optional pos)
  4918. ;; POS (default point) is at a < character. If it is marked with
  4919. ;; open paren syntax-table text property, remove the property,
  4920. ;; together with the close paren property on the matching > (if
  4921. ;; any).
  4922. (save-excursion
  4923. (if pos
  4924. (goto-char pos)
  4925. (setq pos (point)))
  4926. (when (equal (c-get-char-property (point) 'syntax-table)
  4927. c-<-as-paren-syntax)
  4928. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4929. (c-go-list-forward))
  4930. (when (equal (c-get-char-property (1- (point)) 'syntax-table)
  4931. c->-as-paren-syntax) ; should always be true.
  4932. (c-unmark-<->-as-paren (1- (point))))
  4933. (c-unmark-<->-as-paren pos))))
  4934. (defun c-clear->-pair-props (&optional pos)
  4935. ;; POS (default point) is at a > character. If it is marked with
  4936. ;; close paren syntax-table property, remove the property, together
  4937. ;; with the open paren property on the matching < (if any).
  4938. (save-excursion
  4939. (if pos
  4940. (goto-char pos)
  4941. (setq pos (point)))
  4942. (when (equal (c-get-char-property (point) 'syntax-table)
  4943. c->-as-paren-syntax)
  4944. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4945. (c-go-up-list-backward))
  4946. (when (equal (c-get-char-property (point) 'syntax-table)
  4947. c-<-as-paren-syntax) ; should always be true.
  4948. (c-unmark-<->-as-paren (point)))
  4949. (c-unmark-<->-as-paren pos))))
  4950. (defun c-clear-<>-pair-props (&optional pos)
  4951. ;; POS (default point) is at a < or > character. If it has an
  4952. ;; open/close paren syntax-table property, remove this property both
  4953. ;; from the current character and its partner (which will also be
  4954. ;; thusly marked).
  4955. (cond
  4956. ((eq (char-after) ?\<)
  4957. (c-clear-<-pair-props pos))
  4958. ((eq (char-after) ?\>)
  4959. (c-clear->-pair-props pos))
  4960. (t (c-benign-error
  4961. "c-clear-<>-pair-props called from wrong position"))))
  4962. (defun c-clear-<-pair-props-if-match-after (lim &optional pos)
  4963. ;; POS (default point) is at a < character. If it is both marked
  4964. ;; with open/close paren syntax-table property, and has a matching >
  4965. ;; (also marked) which is after LIM, remove the property both from
  4966. ;; the current > and its partner. Return t when this happens, nil
  4967. ;; when it doesn't.
  4968. (save-excursion
  4969. (if pos
  4970. (goto-char pos)
  4971. (setq pos (point)))
  4972. (when (equal (c-get-char-property (point) 'syntax-table)
  4973. c-<-as-paren-syntax)
  4974. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4975. (c-go-list-forward))
  4976. (when (and (>= (point) lim)
  4977. (equal (c-get-char-property (1- (point)) 'syntax-table)
  4978. c->-as-paren-syntax)) ; should always be true.
  4979. (c-unmark-<->-as-paren (1- (point)))
  4980. (c-unmark-<->-as-paren pos))
  4981. t)))
  4982. (defun c-clear->-pair-props-if-match-before (lim &optional pos)
  4983. ;; POS (default point) is at a > character. If it is both marked
  4984. ;; with open/close paren syntax-table property, and has a matching <
  4985. ;; (also marked) which is before LIM, remove the property both from
  4986. ;; the current < and its partner. Return t when this happens, nil
  4987. ;; when it doesn't.
  4988. (save-excursion
  4989. (if pos
  4990. (goto-char pos)
  4991. (setq pos (point)))
  4992. (when (equal (c-get-char-property (point) 'syntax-table)
  4993. c->-as-paren-syntax)
  4994. (with-syntax-table c-no-parens-syntax-table ; ignore unbalanced [,{,(,..
  4995. (c-go-up-list-backward))
  4996. (when (and (<= (point) lim)
  4997. (equal (c-get-char-property (point) 'syntax-table)
  4998. c-<-as-paren-syntax)) ; should always be true.
  4999. (c-unmark-<->-as-paren (point))
  5000. (c-unmark-<->-as-paren pos))
  5001. t)))
  5002. ;; Set by c-common-init in cc-mode.el.
  5003. (defvar c-new-BEG)
  5004. (defvar c-new-END)
  5005. (defun c-before-change-check-<>-operators (beg end)
  5006. ;; Unmark certain pairs of "< .... >" which are currently marked as
  5007. ;; template/generic delimiters. (This marking is via syntax-table
  5008. ;; text properties).
  5009. ;;
  5010. ;; These pairs are those which are in the current "statement" (i.e.,
  5011. ;; the region between the {, }, or ; before BEG and the one after
  5012. ;; END), and which enclose any part of the interval (BEG END).
  5013. ;;
  5014. ;; Note that in C++ (?and Java), template/generic parens cannot
  5015. ;; enclose a brace or semicolon, so we use these as bounds on the
  5016. ;; region we must work on.
  5017. ;;
  5018. ;; This function is called from before-change-functions (via
  5019. ;; c-get-state-before-change-functions). Thus the buffer is widened,
  5020. ;; and point is undefined, both at entry and exit.
  5021. ;;
  5022. ;; FIXME!!! This routine ignores the possibility of macros entirely.
  5023. ;; 2010-01-29.
  5024. (save-excursion
  5025. (let ((beg-lit-limits (progn (goto-char beg) (c-literal-limits)))
  5026. (end-lit-limits (progn (goto-char end) (c-literal-limits)))
  5027. new-beg new-end need-new-beg need-new-end)
  5028. ;; Locate the barrier before the changed region
  5029. (goto-char (if beg-lit-limits (car beg-lit-limits) beg))
  5030. (c-syntactic-skip-backward "^;{}" (c-determine-limit 512))
  5031. (setq new-beg (point))
  5032. ;; Remove the syntax-table/category properties from each pertinent <...>
  5033. ;; pair. Firsly, the ones with the < before beg and > after beg.
  5034. (while
  5035. (c-search-forward-char-property 'syntax-table c-<-as-paren-syntax beg)
  5036. (if (c-clear-<-pair-props-if-match-after beg (1- (point)))
  5037. (setq need-new-beg t)))
  5038. ;; Locate the barrier after END.
  5039. (goto-char (if end-lit-limits (cdr end-lit-limits) end))
  5040. (c-syntactic-re-search-forward "[;{}]" (c-determine-+ve-limit 512) 'end)
  5041. (setq new-end (point))
  5042. ;; Remove syntax-table properties from the remaining pertinent <...>
  5043. ;; pairs, those with a > after end and < before end.
  5044. (while (c-search-backward-char-property 'syntax-table c->-as-paren-syntax end)
  5045. (if (c-clear->-pair-props-if-match-before end)
  5046. (setq need-new-end t)))
  5047. ;; Extend the fontification region, if needed.
  5048. (when need-new-beg
  5049. (goto-char new-beg)
  5050. (c-forward-syntactic-ws)
  5051. (and (< (point) c-new-BEG) (setq c-new-BEG (point))))
  5052. (when need-new-end
  5053. (and (> new-end c-new-END) (setq c-new-END new-end))))))
  5054. (defun c-after-change-check-<>-operators (beg end)
  5055. ;; This is called from `after-change-functions' when
  5056. ;; c-recognize-<>-arglists' is set. It ensures that no "<" or ">"
  5057. ;; chars with paren syntax become part of another operator like "<<"
  5058. ;; or ">=".
  5059. ;;
  5060. ;; This function might do hidden buffer changes.
  5061. (save-excursion
  5062. (goto-char beg)
  5063. (when (or (looking-at "[<>]")
  5064. (< (skip-chars-backward "<>") 0))
  5065. (goto-char beg)
  5066. (c-beginning-of-current-token)
  5067. (when (and (< (point) beg)
  5068. (looking-at c-<>-multichar-token-regexp)
  5069. (< beg (setq beg (match-end 0))))
  5070. (while (progn (skip-chars-forward "^<>" beg)
  5071. (< (point) beg))
  5072. (c-clear-<>-pair-props)
  5073. (forward-char))))
  5074. (when (< beg end)
  5075. (goto-char end)
  5076. (when (or (looking-at "[<>]")
  5077. (< (skip-chars-backward "<>") 0))
  5078. (goto-char end)
  5079. (c-beginning-of-current-token)
  5080. (when (and (< (point) end)
  5081. (looking-at c-<>-multichar-token-regexp)
  5082. (< end (setq end (match-end 0))))
  5083. (while (progn (skip-chars-forward "^<>" end)
  5084. (< (point) end))
  5085. (c-clear-<>-pair-props)
  5086. (forward-char)))))))
  5087. ;; Handling of small scale constructs like types and names.
  5088. ;; Dynamically bound variable that instructs `c-forward-type' to also
  5089. ;; treat possible types (i.e. those that it normally returns 'maybe or
  5090. ;; 'found for) as actual types (and always return 'found for them).
  5091. ;; This means that it records them in `c-record-type-identifiers' if
  5092. ;; that is set, and that it adds them to `c-found-types'.
  5093. (defvar c-promote-possible-types nil)
  5094. ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
  5095. ;; mark up successfully parsed arglists with paren syntax properties on
  5096. ;; the surrounding angle brackets and with `c-<>-arg-sep' in the
  5097. ;; `c-type' property of each argument separating comma.
  5098. ;;
  5099. ;; Setting this variable also makes `c-forward-<>-arglist' recurse into
  5100. ;; all arglists for side effects (i.e. recording types), otherwise it
  5101. ;; exploits any existing paren syntax properties to quickly jump to the
  5102. ;; end of already parsed arglists.
  5103. ;;
  5104. ;; Marking up the arglists is not the default since doing that correctly
  5105. ;; depends on a proper value for `c-restricted-<>-arglists'.
  5106. (defvar c-parse-and-markup-<>-arglists nil)
  5107. ;; Dynamically bound variable that instructs `c-forward-<>-arglist' to
  5108. ;; not accept arglists that contain binary operators.
  5109. ;;
  5110. ;; This is primarily used to handle C++ template arglists. C++
  5111. ;; disambiguates them by checking whether the preceding name is a
  5112. ;; template or not. We can't do that, so we assume it is a template
  5113. ;; if it can be parsed as one. That usually works well since
  5114. ;; comparison expressions on the forms "a < b > c" or "a < b, c > d"
  5115. ;; in almost all cases would be pointless.
  5116. ;;
  5117. ;; However, in function arglists, e.g. in "foo (a < b, c > d)", we
  5118. ;; should let the comma separate the function arguments instead. And
  5119. ;; in a context where the value of the expression is taken, e.g. in
  5120. ;; "if (a < b || c > d)", it's probably not a template.
  5121. (defvar c-restricted-<>-arglists nil)
  5122. ;; Dynamically bound variables that instructs
  5123. ;; `c-forward-keyword-clause', `c-forward-<>-arglist',
  5124. ;; `c-forward-name', `c-forward-type', `c-forward-decl-or-cast-1', and
  5125. ;; `c-forward-label' to record the ranges of all the type and
  5126. ;; reference identifiers they encounter. They will build lists on
  5127. ;; these variables where each element is a cons of the buffer
  5128. ;; positions surrounding each identifier. This recording is only
  5129. ;; activated when `c-record-type-identifiers' is non-nil.
  5130. ;;
  5131. ;; All known types that can't be identifiers are recorded, and also
  5132. ;; other possible types if `c-promote-possible-types' is set.
  5133. ;; Recording is however disabled inside angle bracket arglists that
  5134. ;; are encountered inside names and other angle bracket arglists.
  5135. ;; Such occurrences are taken care of by `c-font-lock-<>-arglists'
  5136. ;; instead.
  5137. ;;
  5138. ;; Only the names in C++ template style references (e.g. "tmpl" in
  5139. ;; "tmpl<a,b>::foo") are recorded as references, other references
  5140. ;; aren't handled here.
  5141. ;;
  5142. ;; `c-forward-label' records the label identifier(s) on
  5143. ;; `c-record-ref-identifiers'.
  5144. (defvar c-record-type-identifiers nil)
  5145. (defvar c-record-ref-identifiers nil)
  5146. ;; This variable will receive a cons cell of the range of the last
  5147. ;; single identifier symbol stepped over by `c-forward-name' if it's
  5148. ;; successful. This is the range that should be put on one of the
  5149. ;; record lists above by the caller. It's assigned nil if there's no
  5150. ;; such symbol in the name.
  5151. (defvar c-last-identifier-range nil)
  5152. (defmacro c-record-type-id (range)
  5153. (if (eq (car-safe range) 'cons)
  5154. ;; Always true.
  5155. `(setq c-record-type-identifiers
  5156. (cons ,range c-record-type-identifiers))
  5157. `(let ((range ,range))
  5158. (if range
  5159. (setq c-record-type-identifiers
  5160. (cons range c-record-type-identifiers))))))
  5161. (defmacro c-record-ref-id (range)
  5162. (if (eq (car-safe range) 'cons)
  5163. ;; Always true.
  5164. `(setq c-record-ref-identifiers
  5165. (cons ,range c-record-ref-identifiers))
  5166. `(let ((range ,range))
  5167. (if range
  5168. (setq c-record-ref-identifiers
  5169. (cons range c-record-ref-identifiers))))))
  5170. ;; Dynamically bound variable that instructs `c-forward-type' to
  5171. ;; record the ranges of types that only are found. Behaves otherwise
  5172. ;; like `c-record-type-identifiers'.
  5173. (defvar c-record-found-types nil)
  5174. (defmacro c-forward-keyword-prefixed-id (type)
  5175. ;; Used internally in `c-forward-keyword-clause' to move forward
  5176. ;; over a type (if TYPE is 'type) or a name (otherwise) which
  5177. ;; possibly is prefixed by keywords and their associated clauses.
  5178. ;; Try with a type/name first to not trip up on those that begin
  5179. ;; with a keyword. Return t if a known or found type is moved
  5180. ;; over. The point is clobbered if nil is returned. If range
  5181. ;; recording is enabled, the identifier is recorded on as a type
  5182. ;; if TYPE is 'type or as a reference if TYPE is 'ref.
  5183. ;;
  5184. ;; This macro might do hidden buffer changes.
  5185. `(let (res)
  5186. (while (if (setq res ,(if (eq type 'type)
  5187. `(c-forward-type)
  5188. `(c-forward-name)))
  5189. nil
  5190. (and (looking-at c-keywords-regexp)
  5191. (c-forward-keyword-clause 1))))
  5192. (when (memq res '(t known found prefix))
  5193. ,(when (eq type 'ref)
  5194. `(when c-record-type-identifiers
  5195. (c-record-ref-id c-last-identifier-range)))
  5196. t)))
  5197. (defmacro c-forward-id-comma-list (type update-safe-pos)
  5198. ;; Used internally in `c-forward-keyword-clause' to move forward
  5199. ;; over a comma separated list of types or names using
  5200. ;; `c-forward-keyword-prefixed-id'.
  5201. ;;
  5202. ;; This macro might do hidden buffer changes.
  5203. `(while (and (progn
  5204. ,(when update-safe-pos
  5205. `(setq safe-pos (point)))
  5206. (eq (char-after) ?,))
  5207. (progn
  5208. (forward-char)
  5209. (c-forward-syntactic-ws)
  5210. (c-forward-keyword-prefixed-id ,type)))))
  5211. (defun c-forward-keyword-clause (match)
  5212. ;; Submatch MATCH in the current match data is assumed to surround a
  5213. ;; token. If it's a keyword, move over it and any immediately
  5214. ;; following clauses associated with it, stopping at the start of
  5215. ;; the next token. t is returned in that case, otherwise the point
  5216. ;; stays and nil is returned. The kind of clauses that are
  5217. ;; recognized are those specified by `c-type-list-kwds',
  5218. ;; `c-ref-list-kwds', `c-colon-type-list-kwds',
  5219. ;; `c-paren-nontype-kwds', `c-paren-type-kwds', `c-<>-type-kwds',
  5220. ;; and `c-<>-arglist-kwds'.
  5221. ;;
  5222. ;; This function records identifier ranges on
  5223. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5224. ;; `c-record-type-identifiers' is non-nil.
  5225. ;;
  5226. ;; Note that for `c-colon-type-list-kwds', which doesn't necessary
  5227. ;; apply directly after the keyword, the type list is moved over
  5228. ;; only when there is no unaccounted token before it (i.e. a token
  5229. ;; that isn't moved over due to some other keyword list). The
  5230. ;; identifier ranges in the list are still recorded if that should
  5231. ;; be done, though.
  5232. ;;
  5233. ;; This function might do hidden buffer changes.
  5234. (let ((kwd-sym (c-keyword-sym (match-string match))) safe-pos pos
  5235. ;; The call to `c-forward-<>-arglist' below is made after
  5236. ;; `c-<>-sexp-kwds' keywords, so we're certain they actually
  5237. ;; are angle bracket arglists and `c-restricted-<>-arglists'
  5238. ;; should therefore be nil.
  5239. (c-parse-and-markup-<>-arglists t)
  5240. c-restricted-<>-arglists)
  5241. (when kwd-sym
  5242. (goto-char (match-end match))
  5243. (c-forward-syntactic-ws)
  5244. (setq safe-pos (point))
  5245. (cond
  5246. ((and (c-keyword-member kwd-sym 'c-type-list-kwds)
  5247. (c-forward-keyword-prefixed-id type))
  5248. ;; There's a type directly after a keyword in `c-type-list-kwds'.
  5249. (c-forward-id-comma-list type t))
  5250. ((and (c-keyword-member kwd-sym 'c-ref-list-kwds)
  5251. (c-forward-keyword-prefixed-id ref))
  5252. ;; There's a name directly after a keyword in `c-ref-list-kwds'.
  5253. (c-forward-id-comma-list ref t))
  5254. ((and (c-keyword-member kwd-sym 'c-paren-any-kwds)
  5255. (eq (char-after) ?\())
  5256. ;; There's an open paren after a keyword in `c-paren-any-kwds'.
  5257. (forward-char)
  5258. (when (and (setq pos (c-up-list-forward))
  5259. (eq (char-before pos) ?\)))
  5260. (when (and c-record-type-identifiers
  5261. (c-keyword-member kwd-sym 'c-paren-type-kwds))
  5262. ;; Use `c-forward-type' on every identifier we can find
  5263. ;; inside the paren, to record the types.
  5264. (while (c-syntactic-re-search-forward c-symbol-start pos t)
  5265. (goto-char (match-beginning 0))
  5266. (unless (c-forward-type)
  5267. (looking-at c-symbol-key) ; Always matches.
  5268. (goto-char (match-end 0)))))
  5269. (goto-char pos)
  5270. (c-forward-syntactic-ws)
  5271. (setq safe-pos (point))))
  5272. ((and (c-keyword-member kwd-sym 'c-<>-sexp-kwds)
  5273. (eq (char-after) ?<)
  5274. (c-forward-<>-arglist (c-keyword-member kwd-sym 'c-<>-type-kwds)))
  5275. (c-forward-syntactic-ws)
  5276. (setq safe-pos (point)))
  5277. ((and (c-keyword-member kwd-sym 'c-nonsymbol-sexp-kwds)
  5278. (not (looking-at c-symbol-start))
  5279. (c-safe (c-forward-sexp) t))
  5280. (c-forward-syntactic-ws)
  5281. (setq safe-pos (point))))
  5282. (when (c-keyword-member kwd-sym 'c-colon-type-list-kwds)
  5283. (if (eq (char-after) ?:)
  5284. ;; If we are at the colon already, we move over the type
  5285. ;; list after it.
  5286. (progn
  5287. (forward-char)
  5288. (c-forward-syntactic-ws)
  5289. (when (c-forward-keyword-prefixed-id type)
  5290. (c-forward-id-comma-list type t)))
  5291. ;; Not at the colon, so stop here. But the identifier
  5292. ;; ranges in the type list later on should still be
  5293. ;; recorded.
  5294. (and c-record-type-identifiers
  5295. (progn
  5296. ;; If a keyword matched both one of the types above and
  5297. ;; this one, we match `c-colon-type-list-re' after the
  5298. ;; clause matched above.
  5299. (goto-char safe-pos)
  5300. (looking-at c-colon-type-list-re))
  5301. (progn
  5302. (goto-char (match-end 0))
  5303. (c-forward-syntactic-ws)
  5304. (c-forward-keyword-prefixed-id type))
  5305. ;; There's a type after the `c-colon-type-list-re' match
  5306. ;; after a keyword in `c-colon-type-list-kwds'.
  5307. (c-forward-id-comma-list type nil))))
  5308. (goto-char safe-pos)
  5309. t)))
  5310. ;; cc-mode requires cc-fonts.
  5311. (declare-function c-fontify-recorded-types-and-refs "cc-fonts" ())
  5312. (defun c-forward-<>-arglist (all-types)
  5313. ;; The point is assumed to be at a "<". Try to treat it as the open
  5314. ;; paren of an angle bracket arglist and move forward to the
  5315. ;; corresponding ">". If successful, the point is left after the
  5316. ;; ">" and t is returned, otherwise the point isn't moved and nil is
  5317. ;; returned. If ALL-TYPES is t then all encountered arguments in
  5318. ;; the arglist that might be types are treated as found types.
  5319. ;;
  5320. ;; The variable `c-parse-and-markup-<>-arglists' controls how this
  5321. ;; function handles text properties on the angle brackets and argument
  5322. ;; separating commas.
  5323. ;;
  5324. ;; `c-restricted-<>-arglists' controls how lenient the template
  5325. ;; arglist recognition should be.
  5326. ;;
  5327. ;; This function records identifier ranges on
  5328. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5329. ;; `c-record-type-identifiers' is non-nil.
  5330. ;;
  5331. ;; This function might do hidden buffer changes.
  5332. (let ((start (point))
  5333. ;; If `c-record-type-identifiers' is set then activate
  5334. ;; recording of any found types that constitute an argument in
  5335. ;; the arglist.
  5336. (c-record-found-types (if c-record-type-identifiers t)))
  5337. (if (catch 'angle-bracket-arglist-escape
  5338. (setq c-record-found-types
  5339. (c-forward-<>-arglist-recur all-types)))
  5340. (progn
  5341. (when (consp c-record-found-types)
  5342. (setq c-record-type-identifiers
  5343. ;; `nconc' doesn't mind that the tail of
  5344. ;; `c-record-found-types' is t.
  5345. (nconc c-record-found-types c-record-type-identifiers)))
  5346. (if (c-major-mode-is 'java-mode) (c-fontify-recorded-types-and-refs))
  5347. t)
  5348. (goto-char start)
  5349. nil)))
  5350. (defun c-forward-<>-arglist-recur (all-types)
  5351. ;; Recursive part of `c-forward-<>-arglist'.
  5352. ;;
  5353. ;; This function might do hidden buffer changes.
  5354. (let ((start (point)) res pos tmp
  5355. ;; Cover this so that any recorded found type ranges are
  5356. ;; automatically lost if it turns out to not be an angle
  5357. ;; bracket arglist. It's propagated through the return value
  5358. ;; on successful completion.
  5359. (c-record-found-types c-record-found-types)
  5360. ;; List that collects the positions after the argument
  5361. ;; separating ',' in the arglist.
  5362. arg-start-pos)
  5363. ;; If the '<' has paren open syntax then we've marked it as an angle
  5364. ;; bracket arglist before, so skip to the end.
  5365. (if (and (not c-parse-and-markup-<>-arglists)
  5366. (c-get-char-property (point) 'syntax-table))
  5367. (progn
  5368. (forward-char)
  5369. (if (and (c-go-up-list-forward)
  5370. (eq (char-before) ?>))
  5371. t
  5372. ;; Got unmatched paren angle brackets. We don't clear the paren
  5373. ;; syntax properties and retry, on the basis that it's very
  5374. ;; unlikely that paren angle brackets become operators by code
  5375. ;; manipulation. It's far more likely that it doesn't match due
  5376. ;; to narrowing or some temporary change.
  5377. (goto-char start)
  5378. nil))
  5379. (forward-char) ; Forward over the opening '<'.
  5380. (unless (looking-at c-<-op-cont-regexp)
  5381. ;; go forward one non-alphanumeric character (group) per iteration of
  5382. ;; this loop.
  5383. (while (and
  5384. (progn
  5385. (c-forward-syntactic-ws)
  5386. (when (or (and c-record-type-identifiers all-types)
  5387. (c-major-mode-is 'java-mode))
  5388. ;; All encountered identifiers are types, so set the
  5389. ;; promote flag and parse the type.
  5390. (progn
  5391. (c-forward-syntactic-ws)
  5392. (if (looking-at "\\?")
  5393. (forward-char)
  5394. (when (looking-at c-identifier-start)
  5395. (let ((c-promote-possible-types t)
  5396. (c-record-found-types t))
  5397. (c-forward-type))))
  5398. (c-forward-syntactic-ws)
  5399. (when (or (looking-at "extends")
  5400. (looking-at "super"))
  5401. (forward-word)
  5402. (c-forward-syntactic-ws)
  5403. (let ((c-promote-possible-types t)
  5404. (c-record-found-types t))
  5405. (c-forward-type)
  5406. (c-forward-syntactic-ws)))))
  5407. (setq pos (point)) ; e.g. first token inside the '<'
  5408. ;; Note: These regexps exploit the match order in \| so
  5409. ;; that "<>" is matched by "<" rather than "[^>:-]>".
  5410. (c-syntactic-re-search-forward
  5411. ;; Stop on ',', '|', '&', '+' and '-' to catch
  5412. ;; common binary operators that could be between
  5413. ;; two comparison expressions "a<b" and "c>d".
  5414. "[<;{},|+&-]\\|[>)]"
  5415. nil t t))
  5416. (cond
  5417. ((eq (char-before) ?>)
  5418. ;; Either an operator starting with '>' or the end of
  5419. ;; the angle bracket arglist.
  5420. (if (looking-at c->-op-without->-cont-regexp)
  5421. (progn
  5422. (goto-char (match-end 0))
  5423. t) ; Continue the loop.
  5424. ;; The angle bracket arglist is finished.
  5425. (when c-parse-and-markup-<>-arglists
  5426. (while arg-start-pos
  5427. (c-put-c-type-property (1- (car arg-start-pos))
  5428. 'c-<>-arg-sep)
  5429. (setq arg-start-pos (cdr arg-start-pos)))
  5430. (c-mark-<-as-paren start)
  5431. (c-mark->-as-paren (1- (point))))
  5432. (setq res t)
  5433. nil)) ; Exit the loop.
  5434. ((eq (char-before) ?<)
  5435. ;; Either an operator starting with '<' or a nested arglist.
  5436. (setq pos (point))
  5437. (let (id-start id-end subres keyword-match)
  5438. (cond
  5439. ;; The '<' begins a multi-char operator.
  5440. ((looking-at c-<-op-cont-regexp)
  5441. (setq tmp (match-end 0))
  5442. (goto-char (match-end 0)))
  5443. ;; We're at a nested <.....>
  5444. ((progn
  5445. (setq tmp pos)
  5446. (backward-char) ; to the '<'
  5447. (and
  5448. (save-excursion
  5449. ;; There's always an identifier before an angle
  5450. ;; bracket arglist, or a keyword in `c-<>-type-kwds'
  5451. ;; or `c-<>-arglist-kwds'.
  5452. (c-backward-syntactic-ws)
  5453. (setq id-end (point))
  5454. (c-simple-skip-symbol-backward)
  5455. (when (or (setq keyword-match
  5456. (looking-at c-opt-<>-sexp-key))
  5457. (not (looking-at c-keywords-regexp)))
  5458. (setq id-start (point))))
  5459. (setq subres
  5460. (let ((c-promote-possible-types t)
  5461. (c-record-found-types t))
  5462. (c-forward-<>-arglist-recur
  5463. (and keyword-match
  5464. (c-keyword-member
  5465. (c-keyword-sym (match-string 1))
  5466. 'c-<>-type-kwds)))))))
  5467. ;; It was an angle bracket arglist.
  5468. (setq c-record-found-types subres)
  5469. ;; Record the identifier before the template as a type
  5470. ;; or reference depending on whether the arglist is last
  5471. ;; in a qualified identifier.
  5472. (when (and c-record-type-identifiers
  5473. (not keyword-match))
  5474. (if (and c-opt-identifier-concat-key
  5475. (progn
  5476. (c-forward-syntactic-ws)
  5477. (looking-at c-opt-identifier-concat-key)))
  5478. (c-record-ref-id (cons id-start id-end))
  5479. (c-record-type-id (cons id-start id-end)))))
  5480. ;; At a "less than" operator.
  5481. (t
  5482. (forward-char)
  5483. )))
  5484. t) ; carry on looping.
  5485. ((and (not c-restricted-<>-arglists)
  5486. (or (and (eq (char-before) ?&)
  5487. (not (eq (char-after) ?&)))
  5488. (eq (char-before) ?,)))
  5489. ;; Just another argument. Record the position. The
  5490. ;; type check stuff that made us stop at it is at
  5491. ;; the top of the loop.
  5492. (setq arg-start-pos (cons (point) arg-start-pos)))
  5493. (t
  5494. ;; Got a character that can't be in an angle bracket
  5495. ;; arglist argument. Abort using `throw', since
  5496. ;; it's useless to try to find a surrounding arglist
  5497. ;; if we're nested.
  5498. (throw 'angle-bracket-arglist-escape nil))))))
  5499. (if res
  5500. (or c-record-found-types t)))))
  5501. (defun c-backward-<>-arglist (all-types &optional limit)
  5502. ;; The point is assumed to be directly after a ">". Try to treat it
  5503. ;; as the close paren of an angle bracket arglist and move back to
  5504. ;; the corresponding "<". If successful, the point is left at
  5505. ;; the "<" and t is returned, otherwise the point isn't moved and
  5506. ;; nil is returned. ALL-TYPES is passed on to
  5507. ;; `c-forward-<>-arglist'.
  5508. ;;
  5509. ;; If the optional LIMIT is given, it bounds the backward search.
  5510. ;; It's then assumed to be at a syntactically relevant position.
  5511. ;;
  5512. ;; This is a wrapper around `c-forward-<>-arglist'. See that
  5513. ;; function for more details.
  5514. (let ((start (point)))
  5515. (backward-char)
  5516. (if (and (not c-parse-and-markup-<>-arglists)
  5517. (c-get-char-property (point) 'syntax-table))
  5518. (if (and (c-go-up-list-backward)
  5519. (eq (char-after) ?<))
  5520. t
  5521. ;; See corresponding note in `c-forward-<>-arglist'.
  5522. (goto-char start)
  5523. nil)
  5524. (while (progn
  5525. (c-syntactic-skip-backward "^<;{}" limit t)
  5526. (and
  5527. (if (eq (char-before) ?<)
  5528. t
  5529. ;; Stopped at bob or a char that isn't allowed in an
  5530. ;; arglist, so we've failed.
  5531. (goto-char start)
  5532. nil)
  5533. (if (> (point)
  5534. (progn (c-beginning-of-current-token)
  5535. (point)))
  5536. ;; If we moved then the "<" was part of some
  5537. ;; multicharacter token.
  5538. t
  5539. (backward-char)
  5540. (let ((beg-pos (point)))
  5541. (if (c-forward-<>-arglist all-types)
  5542. (cond ((= (point) start)
  5543. ;; Matched the arglist. Break the while.
  5544. (goto-char beg-pos)
  5545. nil)
  5546. ((> (point) start)
  5547. ;; We started from a non-paren ">" inside an
  5548. ;; arglist.
  5549. (goto-char start)
  5550. nil)
  5551. (t
  5552. ;; Matched a shorter arglist. Can be a nested
  5553. ;; one so continue looking.
  5554. (goto-char beg-pos)
  5555. t))
  5556. t))))))
  5557. (/= (point) start))))
  5558. (defun c-forward-name ()
  5559. ;; Move forward over a complete name if at the beginning of one,
  5560. ;; stopping at the next following token. A keyword, as such,
  5561. ;; doesn't count as a name. If the point is not at something that
  5562. ;; is recognized as a name then it stays put.
  5563. ;;
  5564. ;; A name could be something as simple as "foo" in C or something as
  5565. ;; complex as "X<Y<class A<int>::B, BIT_MAX >> b>, ::operator<> ::
  5566. ;; Z<(a>b)> :: operator const X<&foo>::T Q::G<unsigned short
  5567. ;; int>::*volatile const" in C++ (this function is actually little
  5568. ;; more than a `looking-at' call in all modes except those that,
  5569. ;; like C++, have `c-recognize-<>-arglists' set).
  5570. ;;
  5571. ;; Return
  5572. ;; o - nil if no name is found;
  5573. ;; o - 'template if it's an identifier ending with an angle bracket
  5574. ;; arglist;
  5575. ;; o - 'operator of it's an operator identifier;
  5576. ;; o - t if it's some other kind of name.
  5577. ;;
  5578. ;; This function records identifier ranges on
  5579. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5580. ;; `c-record-type-identifiers' is non-nil.
  5581. ;;
  5582. ;; This function might do hidden buffer changes.
  5583. (let ((pos (point)) (start (point)) res id-start id-end
  5584. ;; Turn off `c-promote-possible-types' here since we might
  5585. ;; call `c-forward-<>-arglist' and we don't want it to promote
  5586. ;; every suspect thing in the arglist to a type. We're
  5587. ;; typically called from `c-forward-type' in this case, and
  5588. ;; the caller only wants the top level type that it finds to
  5589. ;; be promoted.
  5590. c-promote-possible-types)
  5591. (while
  5592. (and
  5593. (looking-at c-identifier-key)
  5594. (progn
  5595. ;; Check for keyword. We go to the last symbol in
  5596. ;; `c-identifier-key' first.
  5597. (goto-char (setq id-end (match-end 0)))
  5598. (c-simple-skip-symbol-backward)
  5599. (setq id-start (point))
  5600. (if (looking-at c-keywords-regexp)
  5601. (when (and (c-major-mode-is 'c++-mode)
  5602. (looking-at
  5603. (cc-eval-when-compile
  5604. (concat "\\(operator\\|\\(template\\)\\)"
  5605. "\\(" (c-lang-const c-nonsymbol-key c++)
  5606. "\\|$\\)")))
  5607. (if (match-beginning 2)
  5608. ;; "template" is only valid inside an
  5609. ;; identifier if preceded by "::".
  5610. (save-excursion
  5611. (c-backward-syntactic-ws)
  5612. (and (c-safe (backward-char 2) t)
  5613. (looking-at "::")))
  5614. t))
  5615. ;; Handle a C++ operator or template identifier.
  5616. (goto-char id-end)
  5617. (c-forward-syntactic-ws)
  5618. (cond ((eq (char-before id-end) ?e)
  5619. ;; Got "... ::template".
  5620. (let ((subres (c-forward-name)))
  5621. (when subres
  5622. (setq pos (point)
  5623. res subres))))
  5624. ((looking-at c-identifier-start)
  5625. ;; Got a cast operator.
  5626. (when (c-forward-type)
  5627. (setq pos (point)
  5628. res 'operator)
  5629. ;; Now we should match a sequence of either
  5630. ;; '*', '&' or a name followed by ":: *",
  5631. ;; where each can be followed by a sequence
  5632. ;; of `c-opt-type-modifier-key'.
  5633. (while (cond ((looking-at "[*&]")
  5634. (goto-char (match-end 0))
  5635. t)
  5636. ((looking-at c-identifier-start)
  5637. (and (c-forward-name)
  5638. (looking-at "::")
  5639. (progn
  5640. (goto-char (match-end 0))
  5641. (c-forward-syntactic-ws)
  5642. (eq (char-after) ?*))
  5643. (progn
  5644. (forward-char)
  5645. t))))
  5646. (while (progn
  5647. (c-forward-syntactic-ws)
  5648. (setq pos (point))
  5649. (looking-at c-opt-type-modifier-key))
  5650. (goto-char (match-end 1))))))
  5651. ((looking-at c-overloadable-operators-regexp)
  5652. ;; Got some other operator.
  5653. (setq c-last-identifier-range
  5654. (cons (point) (match-end 0)))
  5655. (goto-char (match-end 0))
  5656. (c-forward-syntactic-ws)
  5657. (setq pos (point)
  5658. res 'operator)))
  5659. nil)
  5660. ;; `id-start' is equal to `id-end' if we've jumped over
  5661. ;; an identifier that doesn't end with a symbol token.
  5662. ;; That can occur e.g. for Java import directives on the
  5663. ;; form "foo.bar.*".
  5664. (when (and id-start (/= id-start id-end))
  5665. (setq c-last-identifier-range
  5666. (cons id-start id-end)))
  5667. (goto-char id-end)
  5668. (c-forward-syntactic-ws)
  5669. (setq pos (point)
  5670. res t)))
  5671. (progn
  5672. (goto-char pos)
  5673. (when (or c-opt-identifier-concat-key
  5674. c-recognize-<>-arglists)
  5675. (cond
  5676. ((and c-opt-identifier-concat-key
  5677. (looking-at c-opt-identifier-concat-key))
  5678. ;; Got a concatenated identifier. This handles the
  5679. ;; cases with tricky syntactic whitespace that aren't
  5680. ;; covered in `c-identifier-key'.
  5681. (goto-char (match-end 0))
  5682. (c-forward-syntactic-ws)
  5683. t)
  5684. ((and c-recognize-<>-arglists
  5685. (eq (char-after) ?<))
  5686. ;; Maybe an angle bracket arglist.
  5687. (when (let ((c-record-type-identifiers t)
  5688. (c-record-found-types t))
  5689. (c-forward-<>-arglist nil))
  5690. (c-add-type start (1+ pos))
  5691. (c-forward-syntactic-ws)
  5692. (setq pos (point)
  5693. c-last-identifier-range nil)
  5694. (if (and c-opt-identifier-concat-key
  5695. (looking-at c-opt-identifier-concat-key))
  5696. ;; Continue if there's an identifier concatenation
  5697. ;; operator after the template argument.
  5698. (progn
  5699. (when (and c-record-type-identifiers id-start)
  5700. (c-record-ref-id (cons id-start id-end)))
  5701. (forward-char 2)
  5702. (c-forward-syntactic-ws)
  5703. t)
  5704. (when (and c-record-type-identifiers id-start)
  5705. (c-record-type-id (cons id-start id-end)))
  5706. (setq res 'template)
  5707. nil)))
  5708. )))))
  5709. (goto-char pos)
  5710. res))
  5711. (defun c-forward-type (&optional brace-block-too)
  5712. ;; Move forward over a type spec if at the beginning of one,
  5713. ;; stopping at the next following token. The keyword "typedef"
  5714. ;; isn't part of a type spec here.
  5715. ;;
  5716. ;; BRACE-BLOCK-TOO, when non-nil, means move over the brace block in
  5717. ;; constructs like "struct foo {...} bar ;" or "struct {...} bar;".
  5718. ;; The current (2009-03-10) intention is to convert all uses of
  5719. ;; `c-forward-type' to call with this parameter set, then to
  5720. ;; eliminate it.
  5721. ;;
  5722. ;; Return
  5723. ;; o - t if it's a known type that can't be a name or other
  5724. ;; expression;
  5725. ;; o - 'known if it's an otherwise known type (according to
  5726. ;; `*-font-lock-extra-types');
  5727. ;; o - 'prefix if it's a known prefix of a type;
  5728. ;; o - 'found if it's a type that matches one in `c-found-types';
  5729. ;; o - 'maybe if it's an identifier that might be a type;
  5730. ;; o - 'decltype if it's a decltype(variable) declaration; - or
  5731. ;; o - nil if it can't be a type (the point isn't moved then).
  5732. ;;
  5733. ;; The point is assumed to be at the beginning of a token.
  5734. ;;
  5735. ;; Note that this function doesn't skip past the brace definition
  5736. ;; that might be considered part of the type, e.g.
  5737. ;; "enum {a, b, c} foo".
  5738. ;;
  5739. ;; This function records identifier ranges on
  5740. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  5741. ;; `c-record-type-identifiers' is non-nil.
  5742. ;;
  5743. ;; This function might do hidden buffer changes.
  5744. (when (and c-recognize-<>-arglists
  5745. (looking-at "<"))
  5746. (c-forward-<>-arglist t)
  5747. (c-forward-syntactic-ws))
  5748. (let ((start (point)) pos res name-res id-start id-end id-range)
  5749. ;; Skip leading type modifiers. If any are found we know it's a
  5750. ;; prefix of a type.
  5751. (when c-opt-type-modifier-key ; e.g. "const" "volatile", but NOT "typedef"
  5752. (while (looking-at c-opt-type-modifier-key)
  5753. (goto-char (match-end 1))
  5754. (c-forward-syntactic-ws)
  5755. (setq res 'prefix)))
  5756. (cond
  5757. ((looking-at c-typeof-key) ; e.g. C++'s "decltype".
  5758. (goto-char (match-end 1))
  5759. (c-forward-syntactic-ws)
  5760. (setq res (and (eq (char-after) ?\()
  5761. (c-safe (c-forward-sexp))
  5762. 'decltype))
  5763. (if res
  5764. (c-forward-syntactic-ws)
  5765. (goto-char start)))
  5766. ((looking-at c-type-prefix-key) ; e.g. "struct", "class", but NOT
  5767. ; "typedef".
  5768. (goto-char (match-end 1))
  5769. (c-forward-syntactic-ws)
  5770. (setq pos (point))
  5771. (setq name-res (c-forward-name))
  5772. (setq res (not (null name-res)))
  5773. (when (eq name-res t)
  5774. ;; In many languages the name can be used without the
  5775. ;; prefix, so we add it to `c-found-types'.
  5776. (c-add-type pos (point))
  5777. (when (and c-record-type-identifiers
  5778. c-last-identifier-range)
  5779. (c-record-type-id c-last-identifier-range)))
  5780. (when (and brace-block-too
  5781. (memq res '(t nil))
  5782. (eq (char-after) ?\{)
  5783. (save-excursion
  5784. (c-safe
  5785. (progn (c-forward-sexp)
  5786. (c-forward-syntactic-ws)
  5787. (setq pos (point))))))
  5788. (goto-char pos)
  5789. (setq res t))
  5790. (unless res (goto-char start))) ; invalid syntax
  5791. ((progn
  5792. (setq pos nil)
  5793. (if (looking-at c-identifier-start)
  5794. (save-excursion
  5795. (setq id-start (point)
  5796. name-res (c-forward-name))
  5797. (when name-res
  5798. (setq id-end (point)
  5799. id-range c-last-identifier-range))))
  5800. (and (cond ((looking-at c-primitive-type-key)
  5801. (setq res t))
  5802. ((c-with-syntax-table c-identifier-syntax-table
  5803. (looking-at c-known-type-key))
  5804. (setq res 'known)))
  5805. (or (not id-end)
  5806. (>= (save-excursion
  5807. (save-match-data
  5808. (goto-char (match-end 1))
  5809. (c-forward-syntactic-ws)
  5810. (setq pos (point))))
  5811. id-end)
  5812. (setq res nil))))
  5813. ;; Looking at a primitive or known type identifier. We've
  5814. ;; checked for a name first so that we don't go here if the
  5815. ;; known type match only is a prefix of another name.
  5816. (setq id-end (match-end 1))
  5817. (when (and c-record-type-identifiers
  5818. (or c-promote-possible-types (eq res t)))
  5819. (c-record-type-id (cons (match-beginning 1) (match-end 1))))
  5820. (if (and c-opt-type-component-key
  5821. (save-match-data
  5822. (looking-at c-opt-type-component-key)))
  5823. ;; There might be more keywords for the type.
  5824. (let (safe-pos)
  5825. (c-forward-keyword-clause 1)
  5826. (while (progn
  5827. (setq safe-pos (point))
  5828. (looking-at c-opt-type-component-key))
  5829. (when (and c-record-type-identifiers
  5830. (looking-at c-primitive-type-key))
  5831. (c-record-type-id (cons (match-beginning 1)
  5832. (match-end 1))))
  5833. (c-forward-keyword-clause 1))
  5834. (if (looking-at c-primitive-type-key)
  5835. (progn
  5836. (when c-record-type-identifiers
  5837. (c-record-type-id (cons (match-beginning 1)
  5838. (match-end 1))))
  5839. (c-forward-keyword-clause 1)
  5840. (setq res t))
  5841. (goto-char safe-pos)
  5842. (setq res 'prefix)))
  5843. (unless (save-match-data (c-forward-keyword-clause 1))
  5844. (if pos
  5845. (goto-char pos)
  5846. (goto-char (match-end 1))
  5847. (c-forward-syntactic-ws)))))
  5848. (name-res
  5849. (cond ((eq name-res t)
  5850. ;; A normal identifier.
  5851. (goto-char id-end)
  5852. (if (or res c-promote-possible-types)
  5853. (progn
  5854. (c-add-type id-start id-end)
  5855. (when (and c-record-type-identifiers id-range)
  5856. (c-record-type-id id-range))
  5857. (unless res
  5858. (setq res 'found)))
  5859. (setq res (if (c-check-type id-start id-end)
  5860. ;; It's an identifier that has been used as
  5861. ;; a type somewhere else.
  5862. 'found
  5863. ;; It's an identifier that might be a type.
  5864. 'maybe))))
  5865. ((eq name-res 'template)
  5866. ;; A template is a type.
  5867. (goto-char id-end)
  5868. (setq res t))
  5869. (t
  5870. ;; Otherwise it's an operator identifier, which is not a type.
  5871. (goto-char start)
  5872. (setq res nil)))))
  5873. (when res
  5874. ;; Skip trailing type modifiers. If any are found we know it's
  5875. ;; a type.
  5876. (when c-opt-type-modifier-key
  5877. (while (looking-at c-opt-type-modifier-key) ; e.g. "const", "volatile"
  5878. (goto-char (match-end 1))
  5879. (c-forward-syntactic-ws)
  5880. (setq res t)))
  5881. ;; Step over any type suffix operator. Do not let the existence
  5882. ;; of these alter the classification of the found type, since
  5883. ;; these operators typically are allowed in normal expressions
  5884. ;; too.
  5885. (when c-opt-type-suffix-key ; e.g. "..."
  5886. (while (looking-at c-opt-type-suffix-key)
  5887. (goto-char (match-end 1))
  5888. (c-forward-syntactic-ws)))
  5889. (when c-opt-type-concat-key ; Only/mainly for pike.
  5890. ;; Look for a trailing operator that concatenates the type
  5891. ;; with a following one, and if so step past that one through
  5892. ;; a recursive call. Note that we don't record concatenated
  5893. ;; types in `c-found-types' - it's the component types that
  5894. ;; are recorded when appropriate.
  5895. (setq pos (point))
  5896. (let* ((c-promote-possible-types (or (memq res '(t known))
  5897. c-promote-possible-types))
  5898. ;; If we can't promote then set `c-record-found-types' so that
  5899. ;; we can merge in the types from the second part afterwards if
  5900. ;; it turns out to be a known type there.
  5901. (c-record-found-types (and c-record-type-identifiers
  5902. (not c-promote-possible-types)))
  5903. subres)
  5904. (if (and (looking-at c-opt-type-concat-key)
  5905. (progn
  5906. (goto-char (match-end 1))
  5907. (c-forward-syntactic-ws)
  5908. (setq subres (c-forward-type))))
  5909. (progn
  5910. ;; If either operand certainly is a type then both are, but we
  5911. ;; don't let the existence of the operator itself promote two
  5912. ;; uncertain types to a certain one.
  5913. (cond ((eq res t))
  5914. ((eq subres t)
  5915. (unless (eq name-res 'template)
  5916. (c-add-type id-start id-end))
  5917. (when (and c-record-type-identifiers id-range)
  5918. (c-record-type-id id-range))
  5919. (setq res t))
  5920. ((eq res 'known))
  5921. ((eq subres 'known)
  5922. (setq res 'known))
  5923. ((eq res 'found))
  5924. ((eq subres 'found)
  5925. (setq res 'found))
  5926. (t
  5927. (setq res 'maybe)))
  5928. (when (and (eq res t)
  5929. (consp c-record-found-types))
  5930. ;; Merge in the ranges of any types found by the second
  5931. ;; `c-forward-type'.
  5932. (setq c-record-type-identifiers
  5933. ;; `nconc' doesn't mind that the tail of
  5934. ;; `c-record-found-types' is t.
  5935. (nconc c-record-found-types
  5936. c-record-type-identifiers))))
  5937. (goto-char pos))))
  5938. (when (and c-record-found-types (memq res '(known found)) id-range)
  5939. (setq c-record-found-types
  5940. (cons id-range c-record-found-types))))
  5941. ;;(message "c-forward-type %s -> %s: %s" start (point) res)
  5942. res))
  5943. (defun c-forward-annotation ()
  5944. ;; Used for Java code only at the moment. Assumes point is on the
  5945. ;; @, moves forward an annotation. returns nil if there is no
  5946. ;; annotation at point.
  5947. (and (looking-at "@")
  5948. (progn (forward-char) t)
  5949. (c-forward-type)
  5950. (progn (c-forward-syntactic-ws) t)
  5951. (if (looking-at "(")
  5952. (c-go-list-forward)
  5953. t)))
  5954. (defmacro c-pull-open-brace (ps)
  5955. ;; Pull the next open brace from PS (which has the form of paren-state),
  5956. ;; skipping over any brace pairs. Returns NIL when PS is exhausted.
  5957. `(progn
  5958. (while (consp (car ,ps))
  5959. (setq ,ps (cdr ,ps)))
  5960. (prog1 (car ,ps)
  5961. (setq ,ps (cdr ,ps)))))
  5962. (defun c-back-over-member-initializer-braces ()
  5963. ;; Point is just after a closing brace/parenthesis. Try to parse this as a
  5964. ;; C++ member initializer list, going back to just after the introducing ":"
  5965. ;; and returning t. Otherwise return nil, leaving point unchanged.
  5966. (let ((here (point)) res)
  5967. (setq res
  5968. (catch 'done
  5969. (when (not (c-go-list-backward))
  5970. (throw 'done nil))
  5971. (c-backward-syntactic-ws)
  5972. (when (not (c-simple-skip-symbol-backward))
  5973. (throw 'done nil))
  5974. (c-backward-syntactic-ws)
  5975. (while (eq (char-before) ?,)
  5976. (backward-char)
  5977. (c-backward-syntactic-ws)
  5978. (when (not (memq (char-before) '(?\) ?})))
  5979. (throw 'done nil))
  5980. (when (not (c-go-list-backward))
  5981. (throw 'done nil))
  5982. (c-backward-syntactic-ws)
  5983. (when (not (c-simple-skip-symbol-backward))
  5984. (throw 'done nil))
  5985. (c-backward-syntactic-ws))
  5986. (eq (char-before) ?:)))
  5987. (or res (goto-char here))
  5988. res))
  5989. (defun c-back-over-member-initializers ()
  5990. ;; Test whether we are in a C++ member initializer list, and if so, go back
  5991. ;; to the introducing ":", returning the position of the opening paren of
  5992. ;; the function's arglist. Otherwise return nil, leaving point unchanged.
  5993. (let ((here (point))
  5994. (paren-state (c-parse-state))
  5995. res)
  5996. (setq res
  5997. (catch 'done
  5998. (if (not (c-at-toplevel-p))
  5999. (progn
  6000. (while (not (c-at-toplevel-p))
  6001. (goto-char (c-pull-open-brace paren-state)))
  6002. (c-backward-syntactic-ws)
  6003. (when (not (c-simple-skip-symbol-backward))
  6004. (throw 'done nil))
  6005. (c-backward-syntactic-ws))
  6006. (c-backward-syntactic-ws)
  6007. (when (memq (char-before) '(?\) ?}))
  6008. (when (not (c-go-list-backward))
  6009. (throw 'done nil))
  6010. (c-backward-syntactic-ws))
  6011. (when (c-simple-skip-symbol-backward)
  6012. (c-backward-syntactic-ws)))
  6013. (while (eq (char-before) ?,)
  6014. (backward-char)
  6015. (c-backward-syntactic-ws)
  6016. (when (not (memq (char-before) '(?\) ?})))
  6017. (throw 'done nil))
  6018. (when (not (c-go-list-backward))
  6019. (throw 'done nil))
  6020. (c-backward-syntactic-ws)
  6021. (when (not (c-simple-skip-symbol-backward))
  6022. (throw 'done nil))
  6023. (c-backward-syntactic-ws))
  6024. (and
  6025. (eq (char-before) ?:)
  6026. (c-just-after-func-arglist-p))))
  6027. (or res (goto-char here))
  6028. res))
  6029. ;; Handling of large scale constructs like statements and declarations.
  6030. ;; Macro used inside `c-forward-decl-or-cast-1'. It ought to be a
  6031. ;; defsubst or perhaps even a defun, but it contains lots of free
  6032. ;; variables that refer to things inside `c-forward-decl-or-cast-1'.
  6033. (defmacro c-fdoc-shift-type-backward (&optional short)
  6034. ;; `c-forward-decl-or-cast-1' can consume an arbitrary length list
  6035. ;; of types when parsing a declaration, which means that it
  6036. ;; sometimes consumes the identifier in the declaration as a type.
  6037. ;; This is used to "backtrack" and make the last type be treated as
  6038. ;; an identifier instead.
  6039. `(progn
  6040. ,(unless short
  6041. ;; These identifiers are bound only in the inner let.
  6042. '(setq identifier-type at-type
  6043. identifier-start type-start
  6044. got-parens nil
  6045. got-identifier t
  6046. got-suffix t
  6047. got-suffix-after-parens id-start
  6048. paren-depth 0))
  6049. (if (setq at-type (if (eq backup-at-type 'prefix)
  6050. t
  6051. backup-at-type))
  6052. (setq type-start backup-type-start
  6053. id-start backup-id-start)
  6054. (setq type-start start-pos
  6055. id-start start-pos))
  6056. ;; When these flags already are set we've found specifiers that
  6057. ;; unconditionally signal these attributes - backtracking doesn't
  6058. ;; change that. So keep them set in that case.
  6059. (or at-type-decl
  6060. (setq at-type-decl backup-at-type-decl))
  6061. (or maybe-typeless
  6062. (setq maybe-typeless backup-maybe-typeless))
  6063. ,(unless short
  6064. ;; This identifier is bound only in the inner let.
  6065. '(setq start id-start))))
  6066. (defun c-forward-decl-or-cast-1 (preceding-token-end context last-cast-end)
  6067. ;; Move forward over a declaration or a cast if at the start of one.
  6068. ;; The point is assumed to be at the start of some token. Nil is
  6069. ;; returned if no declaration or cast is recognized, and the point
  6070. ;; is clobbered in that case.
  6071. ;;
  6072. ;; If a declaration is parsed:
  6073. ;;
  6074. ;; The point is left at the first token after the first complete
  6075. ;; declarator, if there is one. The return value is a cons where
  6076. ;; the car is the position of the first token in the declarator. (See
  6077. ;; below for the cdr.)
  6078. ;; Some examples:
  6079. ;;
  6080. ;; void foo (int a, char *b) stuff ...
  6081. ;; car ^ ^ point
  6082. ;; float (*a)[], b;
  6083. ;; car ^ ^ point
  6084. ;; unsigned int a = c_style_initializer, b;
  6085. ;; car ^ ^ point
  6086. ;; unsigned int a (cplusplus_style_initializer), b;
  6087. ;; car ^ ^ point (might change)
  6088. ;; class Foo : public Bar {}
  6089. ;; car ^ ^ point
  6090. ;; class PikeClass (int a, string b) stuff ...
  6091. ;; car ^ ^ point
  6092. ;; enum bool;
  6093. ;; car ^ ^ point
  6094. ;; enum bool flag;
  6095. ;; car ^ ^ point
  6096. ;; void cplusplus_function (int x) throw (Bad);
  6097. ;; car ^ ^ point
  6098. ;; Foo::Foo (int b) : Base (b) {}
  6099. ;; car ^ ^ point
  6100. ;;
  6101. ;; auto foo = 5;
  6102. ;; car ^ ^ point
  6103. ;; auto cplusplus_11 (int a, char *b) -> decltype (bar):
  6104. ;; car ^ ^ point
  6105. ;;
  6106. ;;
  6107. ;;
  6108. ;; The cdr of the return value is non-nil when a
  6109. ;; `c-typedef-decl-kwds' specifier is found in the declaration.
  6110. ;; Specifically it is a dotted pair (A . B) where B is t when a
  6111. ;; `c-typedef-kwds' ("typedef") is present, and A is t when some
  6112. ;; other `c-typedef-decl-kwds' (e.g. class, struct, enum)
  6113. ;; specifier is present. I.e., (some of) the declared
  6114. ;; identifier(s) are types.
  6115. ;;
  6116. ;; If a cast is parsed:
  6117. ;;
  6118. ;; The point is left at the first token after the closing paren of
  6119. ;; the cast. The return value is `cast'. Note that the start
  6120. ;; position must be at the first token inside the cast parenthesis
  6121. ;; to recognize it.
  6122. ;;
  6123. ;; PRECEDING-TOKEN-END is the first position after the preceding
  6124. ;; token, i.e. on the other side of the syntactic ws from the point.
  6125. ;; Use a value less than or equal to (point-min) if the point is at
  6126. ;; the first token in (the visible part of) the buffer.
  6127. ;;
  6128. ;; CONTEXT is a symbol that describes the context at the point:
  6129. ;; 'decl In a comma-separated declaration context (typically
  6130. ;; inside a function declaration arglist).
  6131. ;; '<> In an angle bracket arglist.
  6132. ;; 'arglist Some other type of arglist.
  6133. ;; nil Some other context or unknown context. Includes
  6134. ;; within the parens of an if, for, ... construct.
  6135. ;;
  6136. ;; LAST-CAST-END is the first token after the closing paren of a
  6137. ;; preceding cast, or nil if none is known. If
  6138. ;; `c-forward-decl-or-cast-1' is used in succession, it should be
  6139. ;; the position after the closest preceding call where a cast was
  6140. ;; matched. In that case it's used to discover chains of casts like
  6141. ;; "(a) (b) c".
  6142. ;;
  6143. ;; This function records identifier ranges on
  6144. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  6145. ;; `c-record-type-identifiers' is non-nil.
  6146. ;;
  6147. ;; This function might do hidden buffer changes.
  6148. (let (;; `start-pos' is used below to point to the start of the
  6149. ;; first type, i.e. after any leading specifiers. It might
  6150. ;; also point at the beginning of the preceding syntactic
  6151. ;; whitespace.
  6152. (start-pos (point))
  6153. ;; Set to the result of `c-forward-type'.
  6154. at-type
  6155. ;; The position of the first token in what we currently
  6156. ;; believe is the type in the declaration or cast, after any
  6157. ;; specifiers and their associated clauses.
  6158. type-start
  6159. ;; The position of the first token in what we currently
  6160. ;; believe is the declarator for the first identifier. Set
  6161. ;; when the type is found, and moved forward over any
  6162. ;; `c-decl-hangon-kwds' and their associated clauses that
  6163. ;; occurs after the type.
  6164. id-start
  6165. ;; These store `at-type', `type-start' and `id-start' of the
  6166. ;; identifier before the one in those variables. The previous
  6167. ;; identifier might turn out to be the real type in a
  6168. ;; declaration if the last one has to be the declarator in it.
  6169. ;; If `backup-at-type' is nil then the other variables have
  6170. ;; undefined values.
  6171. backup-at-type backup-type-start backup-id-start
  6172. ;; This stores `kwd-sym' of the symbol before the current one.
  6173. ;; This is needed to distinguish the C++11 version of "auto" from
  6174. ;; the pre C++11 meaning.
  6175. backup-kwd-sym
  6176. ;; Set if we've found a specifier (apart from "typedef") that makes
  6177. ;; the defined identifier(s) types.
  6178. at-type-decl
  6179. ;; Set if we've a "typedef" keyword.
  6180. at-typedef
  6181. ;; Set if we've found a specifier that can start a declaration
  6182. ;; where there's no type.
  6183. maybe-typeless
  6184. ;; Save the value of kwd-sym between loops of the "Check for a
  6185. ;; type" loop. Needed to distinguish a C++11 "auto" from a pre
  6186. ;; C++11 one.
  6187. prev-kwd-sym
  6188. ;; If a specifier is found that also can be a type prefix,
  6189. ;; these flags are set instead of those above. If we need to
  6190. ;; back up an identifier, they are copied to the real flag
  6191. ;; variables. Thus they only take effect if we fail to
  6192. ;; interpret it as a type.
  6193. backup-at-type-decl backup-maybe-typeless
  6194. ;; Whether we've found a declaration or a cast. We might know
  6195. ;; this before we've found the type in it. It's 'ids if we've
  6196. ;; found two consecutive identifiers (usually a sure sign, but
  6197. ;; we should allow that in labels too), and t if we've found a
  6198. ;; specifier keyword (a 100% sure sign).
  6199. at-decl-or-cast
  6200. ;; Set when we need to back up to parse this as a declaration
  6201. ;; but not as a cast.
  6202. backup-if-not-cast
  6203. ;; For casts, the return position.
  6204. cast-end
  6205. ;; Have we got a new-style C++11 "auto"?
  6206. new-style-auto
  6207. ;; Save `c-record-type-identifiers' and
  6208. ;; `c-record-ref-identifiers' since ranges are recorded
  6209. ;; speculatively and should be thrown away if it turns out
  6210. ;; that it isn't a declaration or cast.
  6211. (save-rec-type-ids c-record-type-identifiers)
  6212. (save-rec-ref-ids c-record-ref-identifiers))
  6213. (while (c-forward-annotation)
  6214. (c-forward-syntactic-ws))
  6215. ;; Check for a type. Unknown symbols are treated as possible
  6216. ;; types, but they could also be specifiers disguised through
  6217. ;; macros like __INLINE__, so we recognize both types and known
  6218. ;; specifiers after them too.
  6219. (while
  6220. (let* ((start (point)) kwd-sym kwd-clause-end found-type)
  6221. ;; Look for a specifier keyword clause.
  6222. (when (or (looking-at c-prefix-spec-kwds-re) ;FIXME!!! includes auto
  6223. (and (c-major-mode-is 'java-mode)
  6224. (looking-at "@[A-Za-z0-9]+")))
  6225. (save-match-data
  6226. (if (looking-at c-typedef-key)
  6227. (setq at-typedef t)))
  6228. (setq kwd-sym (c-keyword-sym (match-string 1)))
  6229. (save-excursion
  6230. (c-forward-keyword-clause 1)
  6231. (setq kwd-clause-end (point))))
  6232. (when (setq found-type (c-forward-type t)) ; brace-block-too
  6233. ;; Found a known or possible type or a prefix of a known type.
  6234. (when (and (c-major-mode-is 'c++-mode) ; C++11 style "auto"?
  6235. (eq prev-kwd-sym (c-keyword-sym "auto"))
  6236. (looking-at "[=(]")) ; FIXME!!! proper regexp.
  6237. (setq new-style-auto t)
  6238. (setq found-type nil)
  6239. (goto-char start)) ; position of foo in "auto foo"
  6240. (when at-type
  6241. ;; Got two identifiers with nothing but whitespace
  6242. ;; between them. That can only happen in declarations.
  6243. (setq at-decl-or-cast 'ids)
  6244. (when (eq at-type 'found)
  6245. ;; If the previous identifier is a found type we
  6246. ;; record it as a real one; it might be some sort of
  6247. ;; alias for a prefix like "unsigned".
  6248. (save-excursion
  6249. (goto-char type-start)
  6250. (let ((c-promote-possible-types t))
  6251. (c-forward-type)))))
  6252. (setq backup-at-type at-type
  6253. backup-type-start type-start
  6254. backup-id-start id-start
  6255. backup-kwd-sym kwd-sym
  6256. at-type found-type
  6257. type-start start
  6258. id-start (point)
  6259. ;; The previous ambiguous specifier/type turned out
  6260. ;; to be a type since we've parsed another one after
  6261. ;; it, so clear these backup flags.
  6262. backup-at-type-decl nil
  6263. backup-maybe-typeless nil))
  6264. (if kwd-sym
  6265. (progn
  6266. ;; Handle known specifier keywords and
  6267. ;; `c-decl-hangon-kwds' which can occur after known
  6268. ;; types.
  6269. (if (c-keyword-member kwd-sym 'c-decl-hangon-kwds)
  6270. ;; It's a hang-on keyword that can occur anywhere.
  6271. (progn
  6272. (setq at-decl-or-cast t)
  6273. (if at-type
  6274. ;; Move the identifier start position if
  6275. ;; we've passed a type.
  6276. (setq id-start kwd-clause-end)
  6277. ;; Otherwise treat this as a specifier and
  6278. ;; move the fallback position.
  6279. (setq start-pos kwd-clause-end))
  6280. (goto-char kwd-clause-end))
  6281. ;; It's an ordinary specifier so we know that
  6282. ;; anything before this can't be the type.
  6283. (setq backup-at-type nil
  6284. start-pos kwd-clause-end)
  6285. (if found-type
  6286. ;; It's ambiguous whether this keyword is a
  6287. ;; specifier or a type prefix, so set the backup
  6288. ;; flags. (It's assumed that `c-forward-type'
  6289. ;; moved further than `c-forward-keyword-clause'.)
  6290. (progn
  6291. (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
  6292. (setq backup-at-type-decl t))
  6293. (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
  6294. (setq backup-maybe-typeless t)))
  6295. (when (c-keyword-member kwd-sym 'c-typedef-decl-kwds)
  6296. ;; This test only happens after we've scanned a type.
  6297. ;; So, with valid syntax, kwd-sym can't be 'typedef.
  6298. (setq at-type-decl t))
  6299. (when (c-keyword-member kwd-sym 'c-typeless-decl-kwds)
  6300. (setq maybe-typeless t))
  6301. ;; Haven't matched a type so it's an unambiguous
  6302. ;; specifier keyword and we know we're in a
  6303. ;; declaration.
  6304. (setq at-decl-or-cast t)
  6305. (setq prev-kwd-sym kwd-sym)
  6306. (goto-char kwd-clause-end))))
  6307. ;; If the type isn't known we continue so that we'll jump
  6308. ;; over all specifiers and type identifiers. The reason
  6309. ;; to do this for a known type prefix is to make things
  6310. ;; like "unsigned INT16" work.
  6311. (and found-type (not (eq found-type t))))))
  6312. (cond
  6313. ((eq at-type t)
  6314. ;; If a known type was found, we still need to skip over any
  6315. ;; hangon keyword clauses after it. Otherwise it has already
  6316. ;; been done in the loop above.
  6317. (while (looking-at c-decl-hangon-key)
  6318. (c-forward-keyword-clause 1))
  6319. (setq id-start (point)))
  6320. ((eq at-type 'prefix)
  6321. ;; A prefix type is itself a primitive type when it's not
  6322. ;; followed by another type.
  6323. (setq at-type t))
  6324. ((not at-type)
  6325. ;; Got no type but set things up to continue anyway to handle
  6326. ;; the various cases when a declaration doesn't start with a
  6327. ;; type.
  6328. (setq id-start start-pos))
  6329. ((and (eq at-type 'maybe)
  6330. (c-major-mode-is 'c++-mode))
  6331. ;; If it's C++ then check if the last "type" ends on the form
  6332. ;; "foo::foo" or "foo::~foo", i.e. if it's the name of a
  6333. ;; (con|de)structor.
  6334. (save-excursion
  6335. (let (name end-2 end-1)
  6336. (goto-char id-start)
  6337. (c-backward-syntactic-ws)
  6338. (setq end-2 (point))
  6339. (when (and
  6340. (c-simple-skip-symbol-backward)
  6341. (progn
  6342. (setq name
  6343. (buffer-substring-no-properties (point) end-2))
  6344. ;; Cheating in the handling of syntactic ws below.
  6345. (< (skip-chars-backward ":~ \t\n\r\v\f") 0))
  6346. (progn
  6347. (setq end-1 (point))
  6348. (c-simple-skip-symbol-backward))
  6349. (>= (point) type-start)
  6350. (equal (buffer-substring-no-properties (point) end-1)
  6351. name))
  6352. ;; It is a (con|de)structor name. In that case the
  6353. ;; declaration is typeless so zap out any preceding
  6354. ;; identifier(s) that we might have taken as types.
  6355. (goto-char type-start)
  6356. (setq at-type nil
  6357. backup-at-type nil
  6358. id-start type-start))))))
  6359. ;; Check for and step over a type decl expression after the thing
  6360. ;; that is or might be a type. This can't be skipped since we
  6361. ;; need the correct end position of the declarator for
  6362. ;; `max-type-decl-end-*'.
  6363. (let ((start (point)) (paren-depth 0) pos
  6364. ;; True if there's a non-open-paren match of
  6365. ;; `c-type-decl-prefix-key'.
  6366. got-prefix
  6367. ;; True if the declarator is surrounded by a parenthesis pair.
  6368. got-parens
  6369. ;; True if there is an identifier in the declarator.
  6370. got-identifier
  6371. ;; True if there's a non-close-paren match of
  6372. ;; `c-type-decl-suffix-key'.
  6373. got-suffix
  6374. ;; True if there's a prefix match outside the outermost
  6375. ;; paren pair that surrounds the declarator.
  6376. got-prefix-before-parens
  6377. ;; True if there's a suffix match outside the outermost
  6378. ;; paren pair that surrounds the declarator. The value is
  6379. ;; the position of the first suffix match.
  6380. got-suffix-after-parens
  6381. ;; True if we've parsed the type decl to a token that is
  6382. ;; known to end declarations in this context.
  6383. at-decl-end
  6384. ;; The earlier values of `at-type' and `type-start' if we've
  6385. ;; shifted the type backwards.
  6386. identifier-type identifier-start
  6387. ;; If `c-parse-and-markup-<>-arglists' is set we need to
  6388. ;; turn it off during the name skipping below to avoid
  6389. ;; getting `c-type' properties that might be bogus. That
  6390. ;; can happen since we don't know if
  6391. ;; `c-restricted-<>-arglists' will be correct inside the
  6392. ;; arglist paren that gets entered.
  6393. c-parse-and-markup-<>-arglists
  6394. ;; Start of the identifier for which `got-identifier' was set.
  6395. name-start)
  6396. (goto-char id-start)
  6397. ;; Skip over type decl prefix operators. (Note similar code in
  6398. ;; `c-font-lock-declarators'.)
  6399. (if (and c-recognize-typeless-decls
  6400. (equal c-type-decl-prefix-key "\\<\\>"))
  6401. (when (eq (char-after) ?\()
  6402. (progn
  6403. (setq paren-depth (1+ paren-depth))
  6404. (forward-char)))
  6405. (while (and (looking-at c-type-decl-prefix-key)
  6406. (if (and (c-major-mode-is 'c++-mode)
  6407. (match-beginning 3))
  6408. ;; If the third submatch matches in C++ then
  6409. ;; we're looking at an identifier that's a
  6410. ;; prefix only if it specifies a member pointer.
  6411. (when (progn (setq pos (point))
  6412. (setq got-identifier (c-forward-name)))
  6413. (setq name-start pos)
  6414. (if (looking-at "\\(::\\)")
  6415. ;; We only check for a trailing "::" and
  6416. ;; let the "*" that should follow be
  6417. ;; matched in the next round.
  6418. (progn (setq got-identifier nil) t)
  6419. ;; It turned out to be the real identifier,
  6420. ;; so stop.
  6421. nil))
  6422. t))
  6423. (if (eq (char-after) ?\()
  6424. (progn
  6425. (setq paren-depth (1+ paren-depth))
  6426. (forward-char))
  6427. (unless got-prefix-before-parens
  6428. (setq got-prefix-before-parens (= paren-depth 0)))
  6429. (setq got-prefix t)
  6430. (goto-char (match-end 1)))
  6431. (c-forward-syntactic-ws)))
  6432. (setq got-parens (> paren-depth 0))
  6433. ;; Skip over an identifier.
  6434. (or got-identifier
  6435. (and (looking-at c-identifier-start)
  6436. (setq pos (point))
  6437. (setq got-identifier (c-forward-name))
  6438. (setq name-start pos)))
  6439. ;; Skip over type decl suffix operators.
  6440. (while (if (looking-at c-type-decl-suffix-key)
  6441. (if (eq (char-after) ?\))
  6442. (when (> paren-depth 0)
  6443. (setq paren-depth (1- paren-depth))
  6444. (forward-char)
  6445. t)
  6446. (when (if (save-match-data (looking-at "\\s\("))
  6447. (c-safe (c-forward-sexp 1) t)
  6448. (goto-char (match-end 1))
  6449. t)
  6450. (when (and (not got-suffix-after-parens)
  6451. (= paren-depth 0))
  6452. (setq got-suffix-after-parens (match-beginning 0)))
  6453. (setq got-suffix t)))
  6454. ;; No suffix matched. We might have matched the
  6455. ;; identifier as a type and the open paren of a
  6456. ;; function arglist as a type decl prefix. In that
  6457. ;; case we should "backtrack": Reinterpret the last
  6458. ;; type as the identifier, move out of the arglist and
  6459. ;; continue searching for suffix operators.
  6460. ;;
  6461. ;; Do this even if there's no preceding type, to cope
  6462. ;; with old style function declarations in K&R C,
  6463. ;; (con|de)structors in C++ and `c-typeless-decl-kwds'
  6464. ;; style declarations. That isn't applicable in an
  6465. ;; arglist context, though.
  6466. (when (and (= paren-depth 1)
  6467. (not got-prefix-before-parens)
  6468. (not (eq at-type t))
  6469. (or backup-at-type
  6470. maybe-typeless
  6471. backup-maybe-typeless
  6472. (when c-recognize-typeless-decls
  6473. (not context)))
  6474. (setq pos (c-up-list-forward (point)))
  6475. (eq (char-before pos) ?\)))
  6476. (c-fdoc-shift-type-backward)
  6477. (goto-char pos)
  6478. t))
  6479. (c-forward-syntactic-ws))
  6480. (when (or (and new-style-auto
  6481. (looking-at c-auto-ops-re))
  6482. (and (or maybe-typeless backup-maybe-typeless)
  6483. (not got-identifier)
  6484. (not got-prefix)
  6485. at-type))
  6486. ;; Have found no identifier but `c-typeless-decl-kwds' has
  6487. ;; matched so we know we're inside a declaration. The
  6488. ;; preceding type must be the identifier instead.
  6489. (c-fdoc-shift-type-backward))
  6490. ;; Prepare the "-> type;" for fontification later on.
  6491. (when (and new-style-auto
  6492. (looking-at c-haskell-op-re))
  6493. (save-excursion
  6494. (goto-char (match-end 0))
  6495. (c-forward-syntactic-ws)
  6496. (setq type-start (point))
  6497. (setq at-type (c-forward-type))))
  6498. (setq
  6499. at-decl-or-cast
  6500. (catch 'at-decl-or-cast
  6501. ;; CASE 1
  6502. (when (> paren-depth 0)
  6503. ;; Encountered something inside parens that isn't matched by
  6504. ;; the `c-type-decl-*' regexps, so it's not a type decl
  6505. ;; expression. Try to skip out to the same paren depth to
  6506. ;; not confuse the cast check below.
  6507. (c-safe (goto-char (scan-lists (point) 1 paren-depth)))
  6508. ;; If we've found a specifier keyword then it's a
  6509. ;; declaration regardless.
  6510. (throw 'at-decl-or-cast (eq at-decl-or-cast t)))
  6511. (setq at-decl-end
  6512. (looking-at (cond ((eq context '<>) "[,>]")
  6513. (context "[,\)]")
  6514. (t "[,;]"))))
  6515. ;; Now we've collected info about various characteristics of
  6516. ;; the construct we're looking at. Below follows a decision
  6517. ;; tree based on that. It's ordered to check more certain
  6518. ;; signs before less certain ones.
  6519. (if got-identifier
  6520. (progn
  6521. ;; CASE 2
  6522. (when (and (or at-type maybe-typeless)
  6523. (not (or got-prefix got-parens)))
  6524. ;; Got another identifier directly after the type, so it's a
  6525. ;; declaration.
  6526. (throw 'at-decl-or-cast t))
  6527. (when (and got-parens
  6528. (not got-prefix)
  6529. ;; (not got-suffix-after-parens)
  6530. (or backup-at-type
  6531. maybe-typeless
  6532. backup-maybe-typeless
  6533. (eq at-decl-or-cast t)
  6534. (save-excursion
  6535. (goto-char name-start)
  6536. (not (memq (c-forward-type) '(nil maybe))))))
  6537. ;; Got a declaration of the form "foo bar (gnu);" or "bar
  6538. ;; (gnu);" where we've recognized "bar" as the type and "gnu"
  6539. ;; as the declarator. In this case it's however more likely
  6540. ;; that "bar" is the declarator and "gnu" a function argument
  6541. ;; or initializer (if `c-recognize-paren-inits' is set),
  6542. ;; since the parens around "gnu" would be superfluous if it's
  6543. ;; a declarator. Shift the type one step backward.
  6544. (c-fdoc-shift-type-backward)))
  6545. ;; Found no identifier.
  6546. (if backup-at-type
  6547. (progn
  6548. ;; CASE 3
  6549. (when (= (point) start)
  6550. ;; Got a plain list of identifiers. If a colon follows it's
  6551. ;; a valid label, or maybe a bitfield. Otherwise the last
  6552. ;; one probably is the declared identifier and we should
  6553. ;; back up to the previous type, providing it isn't a cast.
  6554. (if (and (eq (char-after) ?:)
  6555. (not (c-major-mode-is 'java-mode)))
  6556. (cond
  6557. ;; If we've found a specifier keyword then it's a
  6558. ;; declaration regardless.
  6559. ((eq at-decl-or-cast t)
  6560. (throw 'at-decl-or-cast t))
  6561. ((and c-has-bitfields
  6562. (eq at-decl-or-cast 'ids)) ; bitfield.
  6563. (setq backup-if-not-cast t)
  6564. (throw 'at-decl-or-cast t)))
  6565. (setq backup-if-not-cast t)
  6566. (throw 'at-decl-or-cast t)))
  6567. ;; CASE 4
  6568. (when (and got-suffix
  6569. (not got-prefix)
  6570. (not got-parens))
  6571. ;; Got a plain list of identifiers followed by some suffix.
  6572. ;; If this isn't a cast then the last identifier probably is
  6573. ;; the declared one and we should back up to the previous
  6574. ;; type.
  6575. (setq backup-if-not-cast t)
  6576. (throw 'at-decl-or-cast t)))
  6577. ;; CASE 5
  6578. (when (eq at-type t)
  6579. ;; If the type is known we know that there can't be any
  6580. ;; identifier somewhere else, and it's only in declarations in
  6581. ;; e.g. function prototypes and in casts that the identifier may
  6582. ;; be left out.
  6583. (throw 'at-decl-or-cast t))
  6584. (when (= (point) start)
  6585. ;; Only got a single identifier (parsed as a type so far).
  6586. ;; CASE 6
  6587. (if (and
  6588. ;; Check that the identifier isn't at the start of an
  6589. ;; expression.
  6590. at-decl-end
  6591. (cond
  6592. ((eq context 'decl)
  6593. ;; Inside an arglist that contains declarations. If K&R
  6594. ;; style declarations and parenthesis style initializers
  6595. ;; aren't allowed then the single identifier must be a
  6596. ;; type, else we require that it's known or found
  6597. ;; (primitive types are handled above).
  6598. (or (and (not c-recognize-knr-p)
  6599. (not c-recognize-paren-inits))
  6600. (memq at-type '(known found))))
  6601. ((eq context '<>)
  6602. ;; Inside a template arglist. Accept known and found
  6603. ;; types; other identifiers could just as well be
  6604. ;; constants in C++.
  6605. (memq at-type '(known found)))))
  6606. (throw 'at-decl-or-cast t)
  6607. ;; CASE 7
  6608. ;; Can't be a valid declaration or cast, but if we've found a
  6609. ;; specifier it can't be anything else either, so treat it as
  6610. ;; an invalid/unfinished declaration or cast.
  6611. (throw 'at-decl-or-cast at-decl-or-cast))))
  6612. (if (and got-parens
  6613. (not got-prefix)
  6614. (not context)
  6615. (not (eq at-type t))
  6616. (or backup-at-type
  6617. maybe-typeless
  6618. backup-maybe-typeless
  6619. (when c-recognize-typeless-decls
  6620. (or (not got-suffix)
  6621. (not (looking-at
  6622. c-after-suffixed-type-maybe-decl-key))))))
  6623. ;; Got an empty paren pair and a preceding type that probably
  6624. ;; really is the identifier. Shift the type backwards to make
  6625. ;; the last one the identifier. This is analogous to the
  6626. ;; "backtracking" done inside the `c-type-decl-suffix-key' loop
  6627. ;; above.
  6628. ;;
  6629. ;; Exception: In addition to the conditions in that
  6630. ;; "backtracking" code, do not shift backward if we're not
  6631. ;; looking at either `c-after-suffixed-type-decl-key' or "[;,]".
  6632. ;; Since there's no preceding type, the shift would mean that
  6633. ;; the declaration is typeless. But if the regexp doesn't match
  6634. ;; then we will simply fall through in the tests below and not
  6635. ;; recognize it at all, so it's better to try it as an abstract
  6636. ;; declarator instead.
  6637. (c-fdoc-shift-type-backward)
  6638. ;; Still no identifier.
  6639. ;; CASE 8
  6640. (when (and got-prefix (or got-parens got-suffix))
  6641. ;; Require `got-prefix' together with either `got-parens' or
  6642. ;; `got-suffix' to recognize it as an abstract declarator:
  6643. ;; `got-parens' only is probably an empty function call.
  6644. ;; `got-suffix' only can build an ordinary expression together
  6645. ;; with the preceding identifier which we've taken as a type.
  6646. ;; We could actually accept on `got-prefix' only, but that can
  6647. ;; easily occur temporarily while writing an expression so we
  6648. ;; avoid that case anyway. We could do a better job if we knew
  6649. ;; the point when the fontification was invoked.
  6650. (throw 'at-decl-or-cast t))
  6651. ;; CASE 9
  6652. (when (and at-type
  6653. (not got-prefix)
  6654. (not got-parens)
  6655. got-suffix-after-parens
  6656. (eq (char-after got-suffix-after-parens) ?\())
  6657. ;; Got a type, no declarator but a paren suffix. I.e. it's a
  6658. ;; normal function call after all (or perhaps a C++ style object
  6659. ;; instantiation expression).
  6660. (throw 'at-decl-or-cast nil))))
  6661. ;; CASE 10
  6662. (when at-decl-or-cast
  6663. ;; By now we've located the type in the declaration that we know
  6664. ;; we're in.
  6665. (throw 'at-decl-or-cast t))
  6666. ;; CASE 11
  6667. (when (and got-identifier
  6668. (not context)
  6669. (looking-at c-after-suffixed-type-decl-key)
  6670. (if (and got-parens
  6671. (not got-prefix)
  6672. (not got-suffix)
  6673. (not (eq at-type t)))
  6674. ;; Shift the type backward in the case that there's a
  6675. ;; single identifier inside parens. That can only
  6676. ;; occur in K&R style function declarations so it's
  6677. ;; more likely that it really is a function call.
  6678. ;; Therefore we only do this after
  6679. ;; `c-after-suffixed-type-decl-key' has matched.
  6680. (progn (c-fdoc-shift-type-backward) t)
  6681. got-suffix-after-parens))
  6682. ;; A declaration according to `c-after-suffixed-type-decl-key'.
  6683. (throw 'at-decl-or-cast t))
  6684. ;; CASE 12
  6685. (when (and (or got-prefix (not got-parens))
  6686. (memq at-type '(t known)))
  6687. ;; It's a declaration if a known type precedes it and it can't be a
  6688. ;; function call.
  6689. (throw 'at-decl-or-cast t))
  6690. ;; If we get here we can't tell if this is a type decl or a normal
  6691. ;; expression by looking at it alone. (That's under the assumption
  6692. ;; that normal expressions always can look like type decl expressions,
  6693. ;; which isn't really true but the cases where it doesn't hold are so
  6694. ;; uncommon (e.g. some placements of "const" in C++) it's not worth
  6695. ;; the effort to look for them.)
  6696. ;;; 2008-04-16: commented out the next form, to allow the function to recognize
  6697. ;;; "foo (int bar)" in CC (an implicit type (in class foo) without a semicolon)
  6698. ;;; as a(n almost complete) declaration, enabling it to be fontified.
  6699. ;; CASE 13
  6700. ;; (unless (or at-decl-end (looking-at "=[^=]"))
  6701. ;; If this is a declaration it should end here or its initializer(*)
  6702. ;; should start here, so check for allowed separation tokens. Note
  6703. ;; that this rule doesn't work e.g. with a K&R arglist after a
  6704. ;; function header.
  6705. ;;
  6706. ;; *) Don't check for C++ style initializers using parens
  6707. ;; since those already have been matched as suffixes.
  6708. ;;
  6709. ;; If `at-decl-or-cast' is then we've found some other sign that
  6710. ;; it's a declaration or cast, so then it's probably an
  6711. ;; invalid/unfinished one.
  6712. ;; (throw 'at-decl-or-cast at-decl-or-cast))
  6713. ;; Below are tests that only should be applied when we're certain to
  6714. ;; not have parsed halfway through an expression.
  6715. ;; CASE 14
  6716. (when (memq at-type '(t known))
  6717. ;; The expression starts with a known type so treat it as a
  6718. ;; declaration.
  6719. (throw 'at-decl-or-cast t))
  6720. ;; CASE 15
  6721. (when (and (c-major-mode-is 'c++-mode)
  6722. ;; In C++ we check if the identifier is a known type, since
  6723. ;; (con|de)structors use the class name as identifier.
  6724. ;; We've always shifted over the identifier as a type and
  6725. ;; then backed up again in this case.
  6726. identifier-type
  6727. (or (memq identifier-type '(found known))
  6728. (and (eq (char-after identifier-start) ?~)
  6729. ;; `at-type' probably won't be 'found for
  6730. ;; destructors since the "~" is then part of the
  6731. ;; type name being checked against the list of
  6732. ;; known types, so do a check without that
  6733. ;; operator.
  6734. (or (save-excursion
  6735. (goto-char (1+ identifier-start))
  6736. (c-forward-syntactic-ws)
  6737. (c-with-syntax-table
  6738. c-identifier-syntax-table
  6739. (looking-at c-known-type-key)))
  6740. (save-excursion
  6741. (goto-char (1+ identifier-start))
  6742. ;; We have already parsed the type earlier,
  6743. ;; so it'd be possible to cache the end
  6744. ;; position instead of redoing it here, but
  6745. ;; then we'd need to keep track of another
  6746. ;; position everywhere.
  6747. (c-check-type (point)
  6748. (progn (c-forward-type)
  6749. (point))))))))
  6750. (throw 'at-decl-or-cast t))
  6751. (if got-identifier
  6752. (progn
  6753. ;; CASE 16
  6754. (when (and got-prefix-before-parens
  6755. at-type
  6756. (or at-decl-end (looking-at "=[^=]"))
  6757. (not context)
  6758. (not got-suffix))
  6759. ;; Got something like "foo * bar;". Since we're not inside an
  6760. ;; arglist it would be a meaningless expression because the
  6761. ;; result isn't used. We therefore choose to recognize it as
  6762. ;; a declaration. Do not allow a suffix since it could then
  6763. ;; be a function call.
  6764. (throw 'at-decl-or-cast t))
  6765. ;; CASE 17
  6766. (when (and (or got-suffix-after-parens
  6767. (looking-at "=[^=]"))
  6768. (eq at-type 'found)
  6769. (not (eq context 'arglist)))
  6770. ;; Got something like "a (*b) (c);" or "a (b) = c;". It could
  6771. ;; be an odd expression or it could be a declaration. Treat
  6772. ;; it as a declaration if "a" has been used as a type
  6773. ;; somewhere else (if it's a known type we won't get here).
  6774. (throw 'at-decl-or-cast t)))
  6775. ;; CASE 18
  6776. (when (and context
  6777. (or got-prefix
  6778. (and (eq context 'decl)
  6779. (not c-recognize-paren-inits)
  6780. (or got-parens got-suffix))))
  6781. ;; Got a type followed by an abstract declarator. If `got-prefix'
  6782. ;; is set it's something like "a *" without anything after it. If
  6783. ;; `got-parens' or `got-suffix' is set it's "a()", "a[]", "a()[]",
  6784. ;; or similar, which we accept only if the context rules out
  6785. ;; expressions.
  6786. (throw 'at-decl-or-cast t)))
  6787. ;; If we had a complete symbol table here (which rules out
  6788. ;; `c-found-types') we should return t due to the disambiguation rule
  6789. ;; (in at least C++) that anything that can be parsed as a declaration
  6790. ;; is a declaration. Now we're being more defensive and prefer to
  6791. ;; highlight things like "foo (bar);" as a declaration only if we're
  6792. ;; inside an arglist that contains declarations.
  6793. ;; CASE 19
  6794. (eq context 'decl))))
  6795. ;; The point is now after the type decl expression.
  6796. (cond
  6797. ;; Check for a cast.
  6798. ((save-excursion
  6799. (and
  6800. c-cast-parens
  6801. ;; Should be the first type/identifier in a cast paren.
  6802. (> preceding-token-end (point-min))
  6803. (memq (char-before preceding-token-end) c-cast-parens)
  6804. ;; The closing paren should follow.
  6805. (progn
  6806. (c-forward-syntactic-ws)
  6807. (looking-at "\\s\)"))
  6808. ;; There should be a primary expression after it.
  6809. (let (pos)
  6810. (forward-char)
  6811. (c-forward-syntactic-ws)
  6812. (setq cast-end (point))
  6813. (and (looking-at c-primary-expr-regexp)
  6814. (progn
  6815. (setq pos (match-end 0))
  6816. (or
  6817. ;; Check if the expression begins with a prefix keyword.
  6818. (match-beginning 2)
  6819. (if (match-beginning 1)
  6820. ;; Expression begins with an ambiguous operator. Treat
  6821. ;; it as a cast if it's a type decl or if we've
  6822. ;; recognized the type somewhere else.
  6823. (or at-decl-or-cast
  6824. (memq at-type '(t known found)))
  6825. ;; Unless it's a keyword, it's the beginning of a primary
  6826. ;; expression.
  6827. (not (looking-at c-keywords-regexp)))))
  6828. ;; If `c-primary-expr-regexp' matched a nonsymbol token, check
  6829. ;; that it matched a whole one so that we don't e.g. confuse
  6830. ;; the operator '-' with '->'. It's ok if it matches further,
  6831. ;; though, since it e.g. can match the float '.5' while the
  6832. ;; operator regexp only matches '.'.
  6833. (or (not (looking-at c-nonsymbol-token-regexp))
  6834. (<= (match-end 0) pos))))
  6835. ;; There should either be a cast before it or something that isn't an
  6836. ;; identifier or close paren.
  6837. (> preceding-token-end (point-min))
  6838. (progn
  6839. (goto-char (1- preceding-token-end))
  6840. (or (eq (point) last-cast-end)
  6841. (progn
  6842. (c-backward-syntactic-ws)
  6843. (if (< (skip-syntax-backward "w_") 0)
  6844. ;; It's a symbol. Accept it only if it's one of the
  6845. ;; keywords that can precede an expression (without
  6846. ;; surrounding parens).
  6847. (looking-at c-simple-stmt-key)
  6848. (and
  6849. ;; Check that it isn't a close paren (block close is ok,
  6850. ;; though).
  6851. (not (memq (char-before) '(?\) ?\])))
  6852. ;; Check that it isn't a nonsymbol identifier.
  6853. (not (c-on-identifier)))))))))
  6854. ;; Handle the cast.
  6855. (when (and c-record-type-identifiers at-type (not (eq at-type t)))
  6856. (let ((c-promote-possible-types t))
  6857. (goto-char type-start)
  6858. (c-forward-type)))
  6859. (goto-char cast-end)
  6860. 'cast)
  6861. (at-decl-or-cast
  6862. ;; We're at a declaration. Highlight the type and the following
  6863. ;; declarators.
  6864. (when backup-if-not-cast
  6865. (c-fdoc-shift-type-backward t))
  6866. (when (and (eq context 'decl) (looking-at ","))
  6867. ;; Make sure to propagate the `c-decl-arg-start' property to
  6868. ;; the next argument if it's set in this one, to cope with
  6869. ;; interactive refontification.
  6870. (c-put-c-type-property (point) 'c-decl-arg-start))
  6871. ;; Record the type's coordinates in `c-record-type-identifiers' for
  6872. ;; later fontification.
  6873. (when (and c-record-type-identifiers at-type ;; (not (eq at-type t))
  6874. ;; There seems no reason to exclude a token from
  6875. ;; fontification just because it's "a known type that can't
  6876. ;; be a name or other expression". 2013-09-18.
  6877. )
  6878. (let ((c-promote-possible-types t))
  6879. (save-excursion
  6880. (goto-char type-start)
  6881. (c-forward-type))))
  6882. (cons id-start
  6883. (and (or at-type-decl at-typedef)
  6884. (cons at-type-decl at-typedef))))
  6885. (t
  6886. ;; False alarm. Restore the recorded ranges.
  6887. (setq c-record-type-identifiers save-rec-type-ids
  6888. c-record-ref-identifiers save-rec-ref-ids)
  6889. nil))))
  6890. (defun c-forward-label (&optional assume-markup preceding-token-end limit)
  6891. ;; Assuming that point is at the beginning of a token, check if it starts a
  6892. ;; label and if so move over it and return non-nil (t in default situations,
  6893. ;; specific symbols (see below) for interesting situations), otherwise don't
  6894. ;; move and return nil. "Label" here means "most things with a colon".
  6895. ;;
  6896. ;; More precisely, a "label" is regarded as one of:
  6897. ;; (i) a goto target like "foo:" - returns the symbol `goto-target';
  6898. ;; (ii) A case label - either the entire construct "case FOO:", or just the
  6899. ;; bare "case", should the colon be missing. We return t;
  6900. ;; (iii) a keyword which needs a colon, like "default:" or "private:"; We
  6901. ;; return t;
  6902. ;; (iv) One of QT's "extended" C++ variants of
  6903. ;; "private:"/"protected:"/"public:"/"more:" looking like "public slots:".
  6904. ;; Returns the symbol `qt-2kwds-colon'.
  6905. ;; (v) QT's construct "signals:". Returns the symbol `qt-1kwd-colon'.
  6906. ;; (vi) One of the keywords matched by `c-opt-extra-label-key' (without any
  6907. ;; colon). Currently (2006-03), this applies only to Objective C's
  6908. ;; keywords "@private", "@protected", and "@public". Returns t.
  6909. ;;
  6910. ;; One of the things which will NOT be recognized as a label is a bit-field
  6911. ;; element of a struct, something like "int foo:5".
  6912. ;;
  6913. ;; The end of the label is taken to be just after the colon, or the end of
  6914. ;; the first submatch in `c-opt-extra-label-key'. The point is directly
  6915. ;; after the end on return. The terminating char gets marked with
  6916. ;; `c-decl-end' to improve recognition of the following declaration or
  6917. ;; statement.
  6918. ;;
  6919. ;; If ASSUME-MARKUP is non-nil, it's assumed that the preceding
  6920. ;; label, if any, has already been marked up like that.
  6921. ;;
  6922. ;; If PRECEDING-TOKEN-END is given, it should be the first position
  6923. ;; after the preceding token, i.e. on the other side of the
  6924. ;; syntactic ws from the point. Use a value less than or equal to
  6925. ;; (point-min) if the point is at the first token in (the visible
  6926. ;; part of) the buffer.
  6927. ;;
  6928. ;; The optional LIMIT limits the forward scan for the colon.
  6929. ;;
  6930. ;; This function records the ranges of the label symbols on
  6931. ;; `c-record-ref-identifiers' if `c-record-type-identifiers' (!) is
  6932. ;; non-nil.
  6933. ;;
  6934. ;; This function might do hidden buffer changes.
  6935. (let ((start (point))
  6936. label-end
  6937. qt-symbol-idx
  6938. macro-start ; if we're in one.
  6939. label-type
  6940. kwd)
  6941. (cond
  6942. ;; "case" or "default" (Doesn't apply to AWK).
  6943. ((looking-at c-label-kwds-regexp)
  6944. (let ((kwd-end (match-end 1)))
  6945. ;; Record only the keyword itself for fontification, since in
  6946. ;; case labels the following is a constant expression and not
  6947. ;; a label.
  6948. (when c-record-type-identifiers
  6949. (c-record-ref-id (cons (match-beginning 1) kwd-end)))
  6950. ;; Find the label end.
  6951. (goto-char kwd-end)
  6952. (setq label-type
  6953. (if (and (c-syntactic-re-search-forward
  6954. ;; Stop on chars that aren't allowed in expressions,
  6955. ;; and on operator chars that would be meaningless
  6956. ;; there. FIXME: This doesn't cope with ?: operators.
  6957. "[;{=,@]\\|\\(\\=\\|[^:]\\):\\([^:]\\|\\'\\)"
  6958. limit t t nil 1)
  6959. (match-beginning 2))
  6960. (progn ; there's a proper :
  6961. (goto-char (match-beginning 2)) ; just after the :
  6962. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6963. t)
  6964. ;; It's an unfinished label. We consider the keyword enough
  6965. ;; to recognize it as a label, so that it gets fontified.
  6966. ;; Leave the point at the end of it, but don't put any
  6967. ;; `c-decl-end' marker.
  6968. (goto-char kwd-end)
  6969. t))))
  6970. ;; @private, @protected, @public, in Objective C, or similar.
  6971. ((and c-opt-extra-label-key
  6972. (looking-at c-opt-extra-label-key))
  6973. ;; For a `c-opt-extra-label-key' match, we record the whole
  6974. ;; thing for fontification. That's to get the leading '@' in
  6975. ;; Objective-C protection labels fontified.
  6976. (goto-char (match-end 1))
  6977. (when c-record-type-identifiers
  6978. (c-record-ref-id (cons (match-beginning 1) (point))))
  6979. (c-put-c-type-property (1- (point)) 'c-decl-end)
  6980. (setq label-type t))
  6981. ;; All other cases of labels.
  6982. ((and c-recognize-colon-labels ; nil for AWK and IDL, otherwise t.
  6983. ;; A colon label must have something before the colon.
  6984. (not (eq (char-after) ?:))
  6985. ;; Check that we're not after a token that can't precede a label.
  6986. (or
  6987. ;; Trivially succeeds when there's no preceding token.
  6988. ;; Succeeds when we're at a virtual semicolon.
  6989. (if preceding-token-end
  6990. (<= preceding-token-end (point-min))
  6991. (save-excursion
  6992. (c-backward-syntactic-ws)
  6993. (setq preceding-token-end (point))
  6994. (or (bobp)
  6995. (c-at-vsemi-p))))
  6996. ;; Check if we're after a label, if we're after a closing
  6997. ;; paren that belong to statement, and with
  6998. ;; `c-label-prefix-re'. It's done in different order
  6999. ;; depending on `assume-markup' since the checks have
  7000. ;; different expensiveness.
  7001. (if assume-markup
  7002. (or
  7003. (eq (c-get-char-property (1- preceding-token-end) 'c-type)
  7004. 'c-decl-end)
  7005. (save-excursion
  7006. (goto-char (1- preceding-token-end))
  7007. (c-beginning-of-current-token)
  7008. (or (looking-at c-label-prefix-re)
  7009. (looking-at c-block-stmt-1-key)))
  7010. (and (eq (char-before preceding-token-end) ?\))
  7011. (c-after-conditional)))
  7012. (or
  7013. (save-excursion
  7014. (goto-char (1- preceding-token-end))
  7015. (c-beginning-of-current-token)
  7016. (or (looking-at c-label-prefix-re)
  7017. (looking-at c-block-stmt-1-key)))
  7018. (cond
  7019. ((eq (char-before preceding-token-end) ?\))
  7020. (c-after-conditional))
  7021. ((eq (char-before preceding-token-end) ?:)
  7022. ;; Might be after another label, so check it recursively.
  7023. (save-restriction
  7024. (save-excursion
  7025. (goto-char (1- preceding-token-end))
  7026. ;; Essentially the same as the
  7027. ;; `c-syntactic-re-search-forward' regexp below.
  7028. (setq macro-start
  7029. (save-excursion (and (c-beginning-of-macro)
  7030. (point))))
  7031. (if macro-start (narrow-to-region macro-start (point-max)))
  7032. (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+" nil t)
  7033. ;; Note: the following should work instead of the
  7034. ;; narrow-to-region above. Investigate why not,
  7035. ;; sometime. ACM, 2006-03-31.
  7036. ;; (c-syntactic-skip-backward "^-]:?;}=*/%&|,<>!@+"
  7037. ;; macro-start t)
  7038. (let ((pte (point))
  7039. ;; If the caller turned on recording for us,
  7040. ;; it shouldn't apply when we check the
  7041. ;; preceding label.
  7042. c-record-type-identifiers)
  7043. ;; A label can't start at a cpp directive. Check for
  7044. ;; this, since c-forward-syntactic-ws would foul up on it.
  7045. (unless (and c-opt-cpp-prefix (looking-at c-opt-cpp-prefix))
  7046. (c-forward-syntactic-ws)
  7047. (c-forward-label nil pte start))))))))))
  7048. ;; Point is still at the beginning of the possible label construct.
  7049. ;;
  7050. ;; Check that the next nonsymbol token is ":", or that we're in one
  7051. ;; of QT's "slots" declarations. Allow '(' for the sake of macro
  7052. ;; arguments. FIXME: Should build this regexp from the language
  7053. ;; constants.
  7054. (cond
  7055. ;; public: protected: private:
  7056. ((and
  7057. (c-major-mode-is 'c++-mode)
  7058. (search-forward-regexp
  7059. "\\=p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\>[^_]" nil t)
  7060. (progn (backward-char)
  7061. (c-forward-syntactic-ws limit)
  7062. (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon.
  7063. (forward-char)
  7064. (setq label-type t))
  7065. ;; QT double keyword like "protected slots:" or goto target.
  7066. ((progn (goto-char start) nil))
  7067. ((when (c-syntactic-re-search-forward
  7068. "[ \t\n[:?;{=*/%&|,<>!@+-]" limit t t) ; not at EOB
  7069. (backward-char)
  7070. (setq label-end (point))
  7071. (setq qt-symbol-idx
  7072. (and (c-major-mode-is 'c++-mode)
  7073. (string-match
  7074. "\\(p\\(r\\(ivate\\|otected\\)\\|ublic\\)\\|more\\)\\>"
  7075. (buffer-substring start (point)))))
  7076. (c-forward-syntactic-ws limit)
  7077. (cond
  7078. ((looking-at ":\\([^:]\\|\\'\\)") ; A single colon.
  7079. (forward-char)
  7080. (setq label-type
  7081. (if (or (string= "signals" ; Special QT macro
  7082. (setq kwd (buffer-substring-no-properties start label-end)))
  7083. (string= "Q_SIGNALS" kwd))
  7084. 'qt-1kwd-colon
  7085. 'goto-target)))
  7086. ((and qt-symbol-idx
  7087. (search-forward-regexp "\\=\\(slots\\|Q_SLOTS\\)\\>" limit t)
  7088. (progn (c-forward-syntactic-ws limit)
  7089. (looking-at ":\\([^:]\\|\\'\\)"))) ; A single colon
  7090. (forward-char)
  7091. (setq label-type 'qt-2kwds-colon)))))))
  7092. (save-restriction
  7093. (narrow-to-region start (point))
  7094. ;; Check that `c-nonlabel-token-key' doesn't match anywhere.
  7095. (catch 'check-label
  7096. (goto-char start)
  7097. (while (progn
  7098. (when (looking-at c-nonlabel-token-key)
  7099. (goto-char start)
  7100. (setq label-type nil)
  7101. (throw 'check-label nil))
  7102. (and (c-safe (c-forward-sexp)
  7103. (c-forward-syntactic-ws)
  7104. t)
  7105. (not (eobp)))))
  7106. ;; Record the identifiers in the label for fontification, unless
  7107. ;; it begins with `c-label-kwds' in which case the following
  7108. ;; identifiers are part of a (constant) expression that
  7109. ;; shouldn't be fontified.
  7110. (when (and c-record-type-identifiers
  7111. (progn (goto-char start)
  7112. (not (looking-at c-label-kwds-regexp))))
  7113. (while (c-syntactic-re-search-forward c-symbol-key nil t)
  7114. (c-record-ref-id (cons (match-beginning 0)
  7115. (match-end 0)))))
  7116. (c-put-c-type-property (1- (point-max)) 'c-decl-end)
  7117. (goto-char (point-max)))))
  7118. (t
  7119. ;; Not a label.
  7120. (goto-char start)))
  7121. label-type))
  7122. (defun c-forward-objc-directive ()
  7123. ;; Assuming the point is at the beginning of a token, try to move
  7124. ;; forward to the end of the Objective-C directive that starts
  7125. ;; there. Return t if a directive was fully recognized, otherwise
  7126. ;; the point is moved as far as one could be successfully parsed and
  7127. ;; nil is returned.
  7128. ;;
  7129. ;; This function records identifier ranges on
  7130. ;; `c-record-type-identifiers' and `c-record-ref-identifiers' if
  7131. ;; `c-record-type-identifiers' is non-nil.
  7132. ;;
  7133. ;; This function might do hidden buffer changes.
  7134. (let ((start (point))
  7135. start-char
  7136. (c-promote-possible-types t)
  7137. lim
  7138. ;; Turn off recognition of angle bracket arglists while parsing
  7139. ;; types here since the protocol reference list might then be
  7140. ;; considered part of the preceding name or superclass-name.
  7141. c-recognize-<>-arglists)
  7142. (if (or
  7143. (when (looking-at
  7144. (eval-when-compile
  7145. (c-make-keywords-re t
  7146. (append (c-lang-const c-protection-kwds objc)
  7147. '("@end"))
  7148. 'objc-mode)))
  7149. (goto-char (match-end 1))
  7150. t)
  7151. (and
  7152. (looking-at
  7153. (eval-when-compile
  7154. (c-make-keywords-re t
  7155. '("@interface" "@implementation" "@protocol")
  7156. 'objc-mode)))
  7157. ;; Handle the name of the class itself.
  7158. (progn
  7159. ;; (c-forward-token-2) ; 2006/1/13 This doesn't move if the token's
  7160. ;; at EOB.
  7161. (goto-char (match-end 0))
  7162. (setq lim (point))
  7163. (c-skip-ws-forward)
  7164. (c-forward-type))
  7165. (catch 'break
  7166. ;; Look for ": superclass-name" or "( category-name )".
  7167. (when (looking-at "[:\(]")
  7168. (setq start-char (char-after))
  7169. (forward-char)
  7170. (c-forward-syntactic-ws)
  7171. (unless (c-forward-type) (throw 'break nil))
  7172. (when (eq start-char ?\()
  7173. (unless (eq (char-after) ?\)) (throw 'break nil))
  7174. (forward-char)
  7175. (c-forward-syntactic-ws)))
  7176. ;; Look for a protocol reference list.
  7177. (if (eq (char-after) ?<)
  7178. (let ((c-recognize-<>-arglists t)
  7179. (c-parse-and-markup-<>-arglists t)
  7180. c-restricted-<>-arglists)
  7181. (c-forward-<>-arglist t))
  7182. t))))
  7183. (progn
  7184. (c-backward-syntactic-ws lim)
  7185. (c-clear-c-type-property start (1- (point)) 'c-decl-end)
  7186. (c-put-c-type-property (1- (point)) 'c-decl-end)
  7187. t)
  7188. (c-clear-c-type-property start (point) 'c-decl-end)
  7189. nil)))
  7190. (defun c-beginning-of-inheritance-list (&optional lim)
  7191. ;; Go to the first non-whitespace after the colon that starts a
  7192. ;; multiple inheritance introduction. Optional LIM is the farthest
  7193. ;; back we should search.
  7194. ;;
  7195. ;; This function might do hidden buffer changes.
  7196. (c-with-syntax-table c++-template-syntax-table
  7197. (c-backward-token-2 0 t lim)
  7198. (while (and (or (looking-at c-symbol-start)
  7199. (looking-at "[<,]\\|::"))
  7200. (zerop (c-backward-token-2 1 t lim))))))
  7201. (defun c-in-method-def-p ()
  7202. ;; Return nil if we aren't in a method definition, otherwise the
  7203. ;; position of the initial [+-].
  7204. ;;
  7205. ;; This function might do hidden buffer changes.
  7206. (save-excursion
  7207. (beginning-of-line)
  7208. (and c-opt-method-key
  7209. (looking-at c-opt-method-key)
  7210. (point))
  7211. ))
  7212. ;; Contributed by Kevin Ryde <user42@zip.com.au>.
  7213. (defun c-in-gcc-asm-p ()
  7214. ;; Return non-nil if point is within a gcc \"asm\" block.
  7215. ;;
  7216. ;; This should be called with point inside an argument list.
  7217. ;;
  7218. ;; Only one level of enclosing parentheses is considered, so for
  7219. ;; instance nil is returned when in a function call within an asm
  7220. ;; operand.
  7221. ;;
  7222. ;; This function might do hidden buffer changes.
  7223. (and c-opt-asm-stmt-key
  7224. (save-excursion
  7225. (beginning-of-line)
  7226. (backward-up-list 1)
  7227. (c-beginning-of-statement-1 (point-min) nil t)
  7228. (looking-at c-opt-asm-stmt-key))))
  7229. (defun c-at-toplevel-p ()
  7230. "Return a determination as to whether point is \"at the top level\".
  7231. Informally, \"at the top level\" is anywhere where you can write
  7232. a function.
  7233. More precisely, being at the top-level means that point is either
  7234. outside any enclosing block (such as a function definition), or
  7235. directly inside a class, namespace or other block that contains
  7236. another declaration level.
  7237. If point is not at the top-level (e.g. it is inside a method
  7238. definition), then nil is returned. Otherwise, if point is at a
  7239. top-level not enclosed within a class definition, t is returned.
  7240. Otherwise, a 2-vector is returned where the zeroth element is the
  7241. buffer position of the start of the class declaration, and the first
  7242. element is the buffer position of the enclosing class's opening
  7243. brace.
  7244. Note that this function might do hidden buffer changes. See the
  7245. comment at the start of cc-engine.el for more info."
  7246. (let ((paren-state (c-parse-state)))
  7247. (or (not (c-most-enclosing-brace paren-state))
  7248. (c-search-uplist-for-classkey paren-state))))
  7249. (defun c-just-after-func-arglist-p (&optional lim)
  7250. ;; Return non-nil if the point is in the region after the argument
  7251. ;; list of a function and its opening brace (or semicolon in case it
  7252. ;; got no body). If there are K&R style argument declarations in
  7253. ;; that region, the point has to be inside the first one for this
  7254. ;; function to recognize it.
  7255. ;;
  7256. ;; If successful, the point is moved to the first token after the
  7257. ;; function header (see `c-forward-decl-or-cast-1' for details) and
  7258. ;; the position of the opening paren of the function arglist is
  7259. ;; returned.
  7260. ;;
  7261. ;; The point is clobbered if not successful.
  7262. ;;
  7263. ;; LIM is used as bound for backward buffer searches.
  7264. ;;
  7265. ;; This function might do hidden buffer changes.
  7266. (let ((beg (point)) id-start)
  7267. (and
  7268. (eq (c-beginning-of-statement-1 lim) 'same)
  7269. (not (and (c-major-mode-is 'objc-mode)
  7270. (c-forward-objc-directive)))
  7271. (setq id-start
  7272. (car-safe (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil)))
  7273. (< id-start beg)
  7274. ;; There should not be a '=' or ',' between beg and the
  7275. ;; start of the declaration since that means we were in the
  7276. ;; "expression part" of the declaration.
  7277. (or (> (point) beg)
  7278. (not (looking-at "[=,]")))
  7279. (save-excursion
  7280. ;; Check that there's an arglist paren in the
  7281. ;; declaration.
  7282. (goto-char id-start)
  7283. (cond ((eq (char-after) ?\()
  7284. ;; The declarator is a paren expression, so skip past it
  7285. ;; so that we don't get stuck on that instead of the
  7286. ;; function arglist.
  7287. (c-forward-sexp))
  7288. ((and c-opt-op-identifier-prefix
  7289. (looking-at c-opt-op-identifier-prefix))
  7290. ;; Don't trip up on "operator ()".
  7291. (c-forward-token-2 2 t)))
  7292. (and (< (point) beg)
  7293. (c-syntactic-re-search-forward "(" beg t t)
  7294. (1- (point)))))))
  7295. (defun c-in-knr-argdecl (&optional lim)
  7296. ;; Return the position of the first argument declaration if point is
  7297. ;; inside a K&R style argument declaration list, nil otherwise.
  7298. ;; `c-recognize-knr-p' is not checked. If LIM is non-nil, it's a
  7299. ;; position that bounds the backward search for the argument list.
  7300. ;;
  7301. ;; Point must be within a possible K&R region, e.g. just before a top-level
  7302. ;; "{". It must be outside of parens and brackets. The test can return
  7303. ;; false positives otherwise.
  7304. ;;
  7305. ;; This function might do hidden buffer changes.
  7306. (save-excursion
  7307. (save-restriction
  7308. ;; If we're in a macro, our search range is restricted to it. Narrow to
  7309. ;; the searchable range.
  7310. (let* ((macro-start (save-excursion (and (c-beginning-of-macro) (point))))
  7311. (macro-end (save-excursion (and macro-start (c-end-of-macro) (point))))
  7312. (low-lim (max (or lim (point-min)) (or macro-start (point-min))))
  7313. before-lparen after-rparen
  7314. (pp-count-out 20)) ; Max number of paren/brace constructs before
  7315. ; we give up
  7316. (narrow-to-region low-lim (or macro-end (point-max)))
  7317. ;; Search backwards for the defun's argument list. We give up if we
  7318. ;; encounter a "}" (end of a previous defun) an "=" (which can't be in
  7319. ;; a knr region) or BOB.
  7320. ;;
  7321. ;; The criterion for a paren structure being the arg list is:
  7322. ;; o - there is non-WS stuff after it but before any "{"; AND
  7323. ;; o - the token after it isn't a ";" AND
  7324. ;; o - it is preceded by either an identifier (the function name) or
  7325. ;; a macro expansion like "DEFUN (...)"; AND
  7326. ;; o - its content is a non-empty comma-separated list of identifiers
  7327. ;; (an empty arg list won't have a knr region).
  7328. ;;
  7329. ;; The following snippet illustrates these rules:
  7330. ;; int foo (bar, baz, yuk)
  7331. ;; int bar [] ;
  7332. ;; int (*baz) (my_type) ;
  7333. ;; int (*) (void) (*yuk) (void) ;
  7334. ;; {
  7335. (catch 'knr
  7336. (while (> pp-count-out 0) ; go back one paren/bracket pair each time.
  7337. (setq pp-count-out (1- pp-count-out))
  7338. (c-syntactic-skip-backward "^)]}=")
  7339. (cond ((eq (char-before) ?\))
  7340. (setq after-rparen (point)))
  7341. ((eq (char-before) ?\])
  7342. (setq after-rparen nil))
  7343. (t ; either } (hit previous defun) or = or no more
  7344. ; parens/brackets.
  7345. (throw 'knr nil)))
  7346. (if after-rparen
  7347. ;; We're inside a paren. Could it be our argument list....?
  7348. (if
  7349. (and
  7350. (progn
  7351. (goto-char after-rparen)
  7352. (unless (c-go-list-backward) (throw 'knr nil)) ;
  7353. ;; FIXME!!! What about macros between the parens? 2007/01/20
  7354. (setq before-lparen (point)))
  7355. ;; It can't be the arg list if next token is ; or {
  7356. (progn (goto-char after-rparen)
  7357. (c-forward-syntactic-ws)
  7358. (not (memq (char-after) '(?\; ?\{ ?\=))))
  7359. ;; Is the thing preceding the list an identifier (the
  7360. ;; function name), or a macro expansion?
  7361. (progn
  7362. (goto-char before-lparen)
  7363. (eq (c-backward-token-2) 0)
  7364. (or (eq (c-on-identifier) (point))
  7365. (and (eq (char-after) ?\))
  7366. (c-go-up-list-backward)
  7367. (eq (c-backward-token-2) 0)
  7368. (eq (c-on-identifier) (point)))))
  7369. ;; Have we got a non-empty list of comma-separated
  7370. ;; identifiers?
  7371. (progn
  7372. (goto-char before-lparen)
  7373. (c-forward-token-2) ; to first token inside parens
  7374. (and
  7375. (c-on-identifier)
  7376. (c-forward-token-2)
  7377. (catch 'id-list
  7378. (while (eq (char-after) ?\,)
  7379. (c-forward-token-2)
  7380. (unless (c-on-identifier) (throw 'id-list nil))
  7381. (c-forward-token-2))
  7382. (eq (char-after) ?\))))))
  7383. ;; ...Yes. We've identified the function's argument list.
  7384. (throw 'knr
  7385. (progn (goto-char after-rparen)
  7386. (c-forward-syntactic-ws)
  7387. (point)))
  7388. ;; ...No. The current parens aren't the function's arg list.
  7389. (goto-char before-lparen))
  7390. (or (c-go-list-backward) ; backwards over [ .... ]
  7391. (throw 'knr nil)))))))))
  7392. (defun c-skip-conditional ()
  7393. ;; skip forward over conditional at point, including any predicate
  7394. ;; statements in parentheses. No error checking is performed.
  7395. ;;
  7396. ;; This function might do hidden buffer changes.
  7397. (c-forward-sexp (cond
  7398. ;; else if()
  7399. ((looking-at (concat "\\<else"
  7400. "\\([ \t\n]\\|\\\\\n\\)+"
  7401. "if\\>\\([^_]\\|$\\)"))
  7402. 3)
  7403. ;; do, else, try, finally
  7404. ((looking-at (concat "\\<\\("
  7405. "do\\|else\\|try\\|finally"
  7406. "\\)\\>\\([^_]\\|$\\)"))
  7407. 1)
  7408. ;; for, if, while, switch, catch, synchronized, foreach
  7409. (t 2))))
  7410. (defun c-after-conditional (&optional lim)
  7411. ;; If looking at the token after a conditional then return the
  7412. ;; position of its start, otherwise return nil.
  7413. ;;
  7414. ;; This function might do hidden buffer changes.
  7415. (save-excursion
  7416. (and (zerop (c-backward-token-2 1 t lim))
  7417. (or (looking-at c-block-stmt-1-key)
  7418. (and (eq (char-after) ?\()
  7419. (zerop (c-backward-token-2 1 t lim))
  7420. (or (looking-at c-block-stmt-2-key)
  7421. (looking-at c-block-stmt-1-2-key))))
  7422. (point))))
  7423. (defun c-after-special-operator-id (&optional lim)
  7424. ;; If the point is after an operator identifier that isn't handled
  7425. ;; like an ordinary symbol (i.e. like "operator =" in C++) then the
  7426. ;; position of the start of that identifier is returned. nil is
  7427. ;; returned otherwise. The point may be anywhere in the syntactic
  7428. ;; whitespace after the last token of the operator identifier.
  7429. ;;
  7430. ;; This function might do hidden buffer changes.
  7431. (save-excursion
  7432. (and c-overloadable-operators-regexp
  7433. (zerop (c-backward-token-2 1 nil lim))
  7434. (looking-at c-overloadable-operators-regexp)
  7435. (or (not c-opt-op-identifier-prefix)
  7436. (and
  7437. (zerop (c-backward-token-2 1 nil lim))
  7438. (looking-at c-opt-op-identifier-prefix)))
  7439. (point))))
  7440. (defsubst c-backward-to-block-anchor (&optional lim)
  7441. ;; Assuming point is at a brace that opens a statement block of some
  7442. ;; kind, move to the proper anchor point for that block. It might
  7443. ;; need to be adjusted further by c-add-stmt-syntax, but the
  7444. ;; position at return is suitable as start position for that
  7445. ;; function.
  7446. ;;
  7447. ;; This function might do hidden buffer changes.
  7448. (unless (= (point) (c-point 'boi))
  7449. (let ((start (c-after-conditional lim)))
  7450. (if start
  7451. (goto-char start)))))
  7452. (defsubst c-backward-to-decl-anchor (&optional lim)
  7453. ;; Assuming point is at a brace that opens the block of a top level
  7454. ;; declaration of some kind, move to the proper anchor point for
  7455. ;; that block.
  7456. ;;
  7457. ;; This function might do hidden buffer changes.
  7458. (unless (= (point) (c-point 'boi))
  7459. (c-beginning-of-statement-1 lim)))
  7460. (defun c-search-decl-header-end ()
  7461. ;; Search forward for the end of the "header" of the current
  7462. ;; declaration. That's the position where the definition body
  7463. ;; starts, or the first variable initializer, or the ending
  7464. ;; semicolon. I.e. search forward for the closest following
  7465. ;; (syntactically relevant) '{', '=' or ';' token. Point is left
  7466. ;; _after_ the first found token, or at point-max if none is found.
  7467. ;;
  7468. ;; This function might do hidden buffer changes.
  7469. (let ((base (point)))
  7470. (if (c-major-mode-is 'c++-mode)
  7471. ;; In C++ we need to take special care to handle operator
  7472. ;; tokens and those pesky template brackets.
  7473. (while (and
  7474. (c-syntactic-re-search-forward "[;{<=]" nil 'move t t)
  7475. (or
  7476. (c-end-of-current-token base)
  7477. ;; Handle operator identifiers, i.e. ignore any
  7478. ;; operator token preceded by "operator".
  7479. (save-excursion
  7480. (and (c-safe (c-backward-sexp) t)
  7481. (looking-at c-opt-op-identifier-prefix)))
  7482. (and (eq (char-before) ?<)
  7483. (c-with-syntax-table c++-template-syntax-table
  7484. (if (c-safe (goto-char (c-up-list-forward (point))))
  7485. t
  7486. (goto-char (point-max))
  7487. nil)))))
  7488. (setq base (point)))
  7489. (while (and
  7490. (c-syntactic-re-search-forward "[;{=]" nil 'move t t)
  7491. (c-end-of-current-token base))
  7492. (setq base (point))))))
  7493. (defun c-beginning-of-decl-1 (&optional lim)
  7494. ;; Go to the beginning of the current declaration, or the beginning
  7495. ;; of the previous one if already at the start of it. Point won't
  7496. ;; be moved out of any surrounding paren. Return a cons cell of the
  7497. ;; form (MOVE . KNR-POS). MOVE is like the return value from
  7498. ;; `c-beginning-of-statement-1'. If point skipped over some K&R
  7499. ;; style argument declarations (and they are to be recognized) then
  7500. ;; KNR-POS is set to the start of the first such argument
  7501. ;; declaration, otherwise KNR-POS is nil. If LIM is non-nil, it's a
  7502. ;; position that bounds the backward search.
  7503. ;;
  7504. ;; NB: Cases where the declaration continues after the block, as in
  7505. ;; "struct foo { ... } bar;", are currently recognized as two
  7506. ;; declarations, e.g. "struct foo { ... }" and "bar;" in this case.
  7507. ;;
  7508. ;; This function might do hidden buffer changes.
  7509. (catch 'return
  7510. (let* ((start (point))
  7511. (last-stmt-start (point))
  7512. (move (c-beginning-of-statement-1 lim nil t)))
  7513. ;; `c-beginning-of-statement-1' stops at a block start, but we
  7514. ;; want to continue if the block doesn't begin a top level
  7515. ;; construct, i.e. if it isn't preceded by ';', '}', ':', bob,
  7516. ;; or an open paren.
  7517. (let ((beg (point)) tentative-move)
  7518. ;; Go back one "statement" each time round the loop until we're just
  7519. ;; after a ;, }, or :, or at BOB or the start of a macro or start of
  7520. ;; an ObjC method. This will move over a multiple declaration whose
  7521. ;; components are comma separated.
  7522. (while (and
  7523. ;; Must check with c-opt-method-key in ObjC mode.
  7524. (not (and c-opt-method-key
  7525. (looking-at c-opt-method-key)))
  7526. (/= last-stmt-start (point))
  7527. (progn
  7528. (c-backward-syntactic-ws lim)
  7529. (not (memq (char-before) '(?\; ?} ?: nil))))
  7530. (save-excursion
  7531. (backward-char)
  7532. (not (looking-at "\\s(")))
  7533. ;; Check that we don't move from the first thing in a
  7534. ;; macro to its header.
  7535. (not (eq (setq tentative-move
  7536. (c-beginning-of-statement-1 lim nil t))
  7537. 'macro)))
  7538. (setq last-stmt-start beg
  7539. beg (point)
  7540. move tentative-move))
  7541. (goto-char beg))
  7542. (when c-recognize-knr-p
  7543. (let ((fallback-pos (point)) knr-argdecl-start)
  7544. ;; Handle K&R argdecls. Back up after the "statement" jumped
  7545. ;; over by `c-beginning-of-statement-1', unless it was the
  7546. ;; function body, in which case we're sitting on the opening
  7547. ;; brace now. Then test if we're in a K&R argdecl region and
  7548. ;; that we started at the other side of the first argdecl in
  7549. ;; it.
  7550. (unless (eq (char-after) ?{)
  7551. (goto-char last-stmt-start))
  7552. (if (and (setq knr-argdecl-start (c-in-knr-argdecl lim))
  7553. (< knr-argdecl-start start)
  7554. (progn
  7555. (goto-char knr-argdecl-start)
  7556. (not (eq (c-beginning-of-statement-1 lim nil t) 'macro))))
  7557. (throw 'return
  7558. (cons (if (eq (char-after fallback-pos) ?{)
  7559. 'previous
  7560. 'same)
  7561. knr-argdecl-start))
  7562. (goto-char fallback-pos))))
  7563. ;; `c-beginning-of-statement-1' counts each brace block as a separate
  7564. ;; statement, so the result will be 'previous if we've moved over any.
  7565. ;; So change our result back to 'same if necessary.
  7566. ;;
  7567. ;; If they were brace list initializers we might not have moved over a
  7568. ;; declaration boundary though, so change it to 'same if we've moved
  7569. ;; past a '=' before '{', but not ';'. (This ought to be integrated
  7570. ;; into `c-beginning-of-statement-1', so we avoid this extra pass which
  7571. ;; potentially can search over a large amount of text.). Take special
  7572. ;; pains not to get mislead by C++'s "operator=", and the like.
  7573. (if (and (eq move 'previous)
  7574. (c-with-syntax-table (if (c-major-mode-is 'c++-mode)
  7575. c++-template-syntax-table
  7576. (syntax-table))
  7577. (save-excursion
  7578. (and
  7579. (progn
  7580. (while ; keep going back to "[;={"s until we either find
  7581. ; no more, or get to one which isn't an "operator ="
  7582. (and (c-syntactic-re-search-forward "[;={]" start t t t)
  7583. (eq (char-before) ?=)
  7584. c-overloadable-operators-regexp
  7585. c-opt-op-identifier-prefix
  7586. (save-excursion
  7587. (eq (c-backward-token-2) 0)
  7588. (looking-at c-overloadable-operators-regexp)
  7589. (eq (c-backward-token-2) 0)
  7590. (looking-at c-opt-op-identifier-prefix))))
  7591. (eq (char-before) ?=))
  7592. (c-syntactic-re-search-forward "[;{]" start t t)
  7593. (eq (char-before) ?{)
  7594. (c-safe (goto-char (c-up-list-forward (point))) t)
  7595. (not (c-syntactic-re-search-forward ";" start t t))))))
  7596. (cons 'same nil)
  7597. (cons move nil)))))
  7598. (defun c-end-of-decl-1 ()
  7599. ;; Assuming point is at the start of a declaration (as detected by
  7600. ;; e.g. `c-beginning-of-decl-1'), go to the end of it. Unlike
  7601. ;; `c-beginning-of-decl-1', this function handles the case when a
  7602. ;; block is followed by identifiers in e.g. struct declarations in C
  7603. ;; or C++. If a proper end was found then t is returned, otherwise
  7604. ;; point is moved as far as possible within the current sexp and nil
  7605. ;; is returned. This function doesn't handle macros; use
  7606. ;; `c-end-of-macro' instead in those cases.
  7607. ;;
  7608. ;; This function might do hidden buffer changes.
  7609. (let ((start (point))
  7610. (decl-syntax-table (if (c-major-mode-is 'c++-mode)
  7611. c++-template-syntax-table
  7612. (syntax-table))))
  7613. (catch 'return
  7614. (c-search-decl-header-end)
  7615. (when (and c-recognize-knr-p
  7616. (eq (char-before) ?\;)
  7617. (c-in-knr-argdecl start))
  7618. ;; Stopped at the ';' in a K&R argdecl section which is
  7619. ;; detected using the same criteria as in
  7620. ;; `c-beginning-of-decl-1'. Move to the following block
  7621. ;; start.
  7622. (c-syntactic-re-search-forward "{" nil 'move t))
  7623. (when (eq (char-before) ?{)
  7624. ;; Encountered a block in the declaration. Jump over it.
  7625. (condition-case nil
  7626. (goto-char (c-up-list-forward (point)))
  7627. (error (goto-char (point-max))
  7628. (throw 'return nil)))
  7629. (if (or (not c-opt-block-decls-with-vars-key)
  7630. (save-excursion
  7631. (c-with-syntax-table decl-syntax-table
  7632. (let ((lim (point)))
  7633. (goto-char start)
  7634. (not (and
  7635. ;; Check for `c-opt-block-decls-with-vars-key'
  7636. ;; before the first paren.
  7637. (c-syntactic-re-search-forward
  7638. (concat "[;=\(\[{]\\|\\("
  7639. c-opt-block-decls-with-vars-key
  7640. "\\)")
  7641. lim t t t)
  7642. (match-beginning 1)
  7643. (not (eq (char-before) ?_))
  7644. ;; Check that the first following paren is
  7645. ;; the block.
  7646. (c-syntactic-re-search-forward "[;=\(\[{]"
  7647. lim t t t)
  7648. (eq (char-before) ?{)))))))
  7649. ;; The declaration doesn't have any of the
  7650. ;; `c-opt-block-decls-with-vars' keywords in the
  7651. ;; beginning, so it ends here at the end of the block.
  7652. (throw 'return t)))
  7653. (c-with-syntax-table decl-syntax-table
  7654. (while (progn
  7655. (if (eq (char-before) ?\;)
  7656. (throw 'return t))
  7657. (c-syntactic-re-search-forward ";" nil 'move t))))
  7658. nil)))
  7659. (defun c-looking-at-decl-block (containing-sexp goto-start &optional limit)
  7660. ;; Assuming the point is at an open brace, check if it starts a
  7661. ;; block that contains another declaration level, i.e. that isn't a
  7662. ;; statement block or a brace list, and if so return non-nil.
  7663. ;;
  7664. ;; If the check is successful, the return value is the start of the
  7665. ;; keyword that tells what kind of construct it is, i.e. typically
  7666. ;; what `c-decl-block-key' matched. Also, if GOTO-START is set then
  7667. ;; the point will be at the start of the construct, before any
  7668. ;; leading specifiers, otherwise it's at the returned position.
  7669. ;;
  7670. ;; The point is clobbered if the check is unsuccessful.
  7671. ;;
  7672. ;; CONTAINING-SEXP is the position of the open of the surrounding
  7673. ;; paren, or nil if none.
  7674. ;;
  7675. ;; The optional LIMIT limits the backward search for the start of
  7676. ;; the construct. It's assumed to be at a syntactically relevant
  7677. ;; position.
  7678. ;;
  7679. ;; If any template arglists are found in the searched region before
  7680. ;; the open brace, they get marked with paren syntax.
  7681. ;;
  7682. ;; This function might do hidden buffer changes.
  7683. (let ((open-brace (point)) kwd-start first-specifier-pos)
  7684. (c-syntactic-skip-backward c-block-prefix-charset limit t)
  7685. (when (and c-recognize-<>-arglists
  7686. (eq (char-before) ?>))
  7687. ;; Could be at the end of a template arglist.
  7688. (let ((c-parse-and-markup-<>-arglists t))
  7689. (while (and
  7690. (c-backward-<>-arglist nil limit)
  7691. (progn
  7692. (c-syntactic-skip-backward c-block-prefix-charset limit t)
  7693. (eq (char-before) ?>))))))
  7694. ;; Note: Can't get bogus hits inside template arglists below since they
  7695. ;; have gotten paren syntax above.
  7696. (when (and
  7697. ;; If `goto-start' is set we begin by searching for the
  7698. ;; first possible position of a leading specifier list.
  7699. ;; The `c-decl-block-key' search continues from there since
  7700. ;; we know it can't match earlier.
  7701. (if goto-start
  7702. (when (c-syntactic-re-search-forward c-symbol-start
  7703. open-brace t t)
  7704. (goto-char (setq first-specifier-pos (match-beginning 0)))
  7705. t)
  7706. t)
  7707. (cond
  7708. ((c-syntactic-re-search-forward c-decl-block-key open-brace t t t)
  7709. (goto-char (setq kwd-start (match-beginning 0)))
  7710. (and
  7711. ;; Exclude cases where we matched what would ordinarily
  7712. ;; be a block declaration keyword, except where it's not
  7713. ;; legal because it's part of a "compound keyword" like
  7714. ;; "enum class". Of course, if c-after-brace-list-key
  7715. ;; is nil, we can skip the test.
  7716. (or (equal c-after-brace-list-key "\\<\\>")
  7717. (save-match-data
  7718. (save-excursion
  7719. (not
  7720. (and
  7721. (looking-at c-after-brace-list-key)
  7722. (= (c-backward-token-2 1 t) 0)
  7723. (looking-at c-brace-list-key))))))
  7724. (or
  7725. ;; Found a keyword that can't be a type?
  7726. (match-beginning 1)
  7727. ;; Can be a type too, in which case it's the return type of a
  7728. ;; function (under the assumption that no declaration level
  7729. ;; block construct starts with a type).
  7730. (not (c-forward-type))
  7731. ;; Jumped over a type, but it could be a declaration keyword
  7732. ;; followed by the declared identifier that we've jumped over
  7733. ;; instead (e.g. in "class Foo {"). If it indeed is a type
  7734. ;; then we should be at the declarator now, so check for a
  7735. ;; valid declarator start.
  7736. ;;
  7737. ;; Note: This doesn't cope with the case when a declared
  7738. ;; identifier is followed by e.g. '(' in a language where '('
  7739. ;; also might be part of a declarator expression. Currently
  7740. ;; there's no such language.
  7741. (not (or (looking-at c-symbol-start)
  7742. (looking-at c-type-decl-prefix-key))))))
  7743. ;; In Pike a list of modifiers may be followed by a brace
  7744. ;; to make them apply to many identifiers. Note that the
  7745. ;; match data will be empty on return in this case.
  7746. ((and (c-major-mode-is 'pike-mode)
  7747. (progn
  7748. (goto-char open-brace)
  7749. (= (c-backward-token-2) 0))
  7750. (looking-at c-specifier-key)
  7751. ;; Use this variant to avoid yet another special regexp.
  7752. (c-keyword-member (c-keyword-sym (match-string 1))
  7753. 'c-modifier-kwds))
  7754. (setq kwd-start (point))
  7755. t)))
  7756. ;; Got a match.
  7757. (if goto-start
  7758. ;; Back up over any preceding specifiers and their clauses
  7759. ;; by going forward from `first-specifier-pos', which is the
  7760. ;; earliest possible position where the specifier list can
  7761. ;; start.
  7762. (progn
  7763. (goto-char first-specifier-pos)
  7764. (while (< (point) kwd-start)
  7765. (if (looking-at c-symbol-key)
  7766. ;; Accept any plain symbol token on the ground that
  7767. ;; it's a specifier masked through a macro (just
  7768. ;; like `c-forward-decl-or-cast-1' skip forward over
  7769. ;; such tokens).
  7770. ;;
  7771. ;; Could be more restrictive wrt invalid keywords,
  7772. ;; but that'd only occur in invalid code so there's
  7773. ;; no use spending effort on it.
  7774. (let ((end (match-end 0)))
  7775. (unless (c-forward-keyword-clause 0)
  7776. (goto-char end)
  7777. (c-forward-syntactic-ws)))
  7778. ;; Can't parse a declaration preamble and is still
  7779. ;; before `kwd-start'. That means `first-specifier-pos'
  7780. ;; was in some earlier construct. Search again.
  7781. (if (c-syntactic-re-search-forward c-symbol-start
  7782. kwd-start 'move t)
  7783. (goto-char (setq first-specifier-pos (match-beginning 0)))
  7784. ;; Got no preamble before the block declaration keyword.
  7785. (setq first-specifier-pos kwd-start))))
  7786. (goto-char first-specifier-pos))
  7787. (goto-char kwd-start))
  7788. kwd-start)))
  7789. (defun c-search-uplist-for-classkey (paren-state)
  7790. ;; Check if the closest containing paren sexp is a declaration
  7791. ;; block, returning a 2 element vector in that case. Aref 0
  7792. ;; contains the bufpos at boi of the class key line, and aref 1
  7793. ;; contains the bufpos of the open brace. This function is an
  7794. ;; obsolete wrapper for `c-looking-at-decl-block'.
  7795. ;;
  7796. ;; This function might do hidden buffer changes.
  7797. (let ((open-paren-pos (c-most-enclosing-brace paren-state)))
  7798. (when open-paren-pos
  7799. (save-excursion
  7800. (goto-char open-paren-pos)
  7801. (when (and (eq (char-after) ?{)
  7802. (c-looking-at-decl-block
  7803. (c-safe-position open-paren-pos paren-state)
  7804. nil))
  7805. (back-to-indentation)
  7806. (vector (point) open-paren-pos))))))
  7807. (defun c-most-enclosing-decl-block (paren-state)
  7808. ;; Return the buffer position of the most enclosing decl-block brace (in the
  7809. ;; sense of c-looking-at-decl-block) in the PAREN-STATE structure, or nil if
  7810. ;; none was found.
  7811. (let* ((open-brace (c-pull-open-brace paren-state))
  7812. (next-open-brace (c-pull-open-brace paren-state)))
  7813. (while (and open-brace
  7814. (save-excursion
  7815. (goto-char open-brace)
  7816. (not (c-looking-at-decl-block next-open-brace nil))))
  7817. (setq open-brace next-open-brace
  7818. next-open-brace (c-pull-open-brace paren-state)))
  7819. open-brace))
  7820. (defun c-cheap-inside-bracelist-p (paren-state)
  7821. ;; Return the position of the L-brace if point is inside a brace list
  7822. ;; initialization of an array, etc. This is an approximate function,
  7823. ;; designed for speed over accuracy. It will not find every bracelist, but
  7824. ;; a non-nil result is reliable. We simply search for "= {" (naturally with
  7825. ;; syntactic whitespace allowed). PAREN-STATE is the normal thing that it
  7826. ;; is everywhere else.
  7827. (let (b-pos)
  7828. (save-excursion
  7829. (while
  7830. (and (setq b-pos (c-pull-open-brace paren-state))
  7831. (progn (goto-char b-pos)
  7832. (c-backward-sws)
  7833. (c-backward-token-2)
  7834. (not (looking-at "=")))))
  7835. b-pos)))
  7836. (defun c-backward-colon-prefixed-type ()
  7837. ;; We're at the token after what might be a type prefixed with a colon. Try
  7838. ;; moving backward over this type and the colon. On success, return t and
  7839. ;; leave point before colon; on failure, leave point unchanged. Will clobber
  7840. ;; match data.
  7841. (let ((here (point))
  7842. (colon-pos nil))
  7843. (save-excursion
  7844. (while
  7845. (and (eql (c-backward-token-2) 0)
  7846. (or (not (looking-at "\\s)"))
  7847. (c-go-up-list-backward))
  7848. (cond
  7849. ((eql (char-after) ?:)
  7850. (setq colon-pos (point))
  7851. (forward-char)
  7852. (c-forward-syntactic-ws)
  7853. (or (and (c-forward-type)
  7854. (progn (c-forward-syntactic-ws)
  7855. (eq (point) here)))
  7856. (setq colon-pos nil))
  7857. nil)
  7858. ((eql (char-after) ?\()
  7859. t)
  7860. ((looking-at c-symbol-key)
  7861. t)
  7862. (t nil)))))
  7863. (when colon-pos
  7864. (goto-char colon-pos)
  7865. t)))
  7866. (defun c-backward-over-enum-header ()
  7867. ;; We're at a "{". Move back to the enum-like keyword that starts this
  7868. ;; declaration and return t, otherwise don't move and return nil.
  7869. (let ((here (point))
  7870. up-sexp-pos before-identifier)
  7871. (when c-recognize-post-brace-list-type-p
  7872. (c-backward-colon-prefixed-type))
  7873. (while
  7874. (and
  7875. (eq (c-backward-token-2) 0)
  7876. (or (not (looking-at "\\s)"))
  7877. (c-go-up-list-backward))
  7878. (cond
  7879. ((and (looking-at c-symbol-key) (c-on-identifier)
  7880. (not before-identifier))
  7881. (setq before-identifier t))
  7882. ((and before-identifier
  7883. (or (eql (char-after) ?,)
  7884. (looking-at c-postfix-decl-spec-key)))
  7885. (setq before-identifier nil)
  7886. t)
  7887. ((looking-at c-after-brace-list-key) t)
  7888. ((looking-at c-brace-list-key) nil)
  7889. ((and c-recognize-<>-arglists
  7890. (eq (char-after) ?<)
  7891. (looking-at "\\s("))
  7892. t)
  7893. (t nil))))
  7894. (or (looking-at c-brace-list-key)
  7895. (progn (goto-char here) nil))))
  7896. (defun c-inside-bracelist-p (containing-sexp paren-state)
  7897. ;; return the buffer position of the beginning of the brace list
  7898. ;; statement if we're inside a brace list, otherwise return nil.
  7899. ;; CONTAINING-SEXP is the buffer pos of the innermost containing
  7900. ;; paren. PAREN-STATE is the remainder of the state of enclosing
  7901. ;; braces
  7902. ;;
  7903. ;; N.B.: This algorithm can potentially get confused by cpp macros
  7904. ;; placed in inconvenient locations. It's a trade-off we make for
  7905. ;; speed.
  7906. ;;
  7907. ;; This function might do hidden buffer changes.
  7908. (or
  7909. ;; This will pick up brace list declarations.
  7910. (save-excursion
  7911. (goto-char containing-sexp)
  7912. (c-backward-over-enum-header))
  7913. ;; this will pick up array/aggregate init lists, even if they are nested.
  7914. (save-excursion
  7915. (let ((class-key
  7916. ;; Pike can have class definitions anywhere, so we must
  7917. ;; check for the class key here.
  7918. (and (c-major-mode-is 'pike-mode)
  7919. c-decl-block-key))
  7920. bufpos braceassignp lim next-containing macro-start)
  7921. (while (and (not bufpos)
  7922. containing-sexp)
  7923. (when paren-state
  7924. (if (consp (car paren-state))
  7925. (setq lim (cdr (car paren-state))
  7926. paren-state (cdr paren-state))
  7927. (setq lim (car paren-state)))
  7928. (when paren-state
  7929. (setq next-containing (car paren-state)
  7930. paren-state (cdr paren-state))))
  7931. (goto-char containing-sexp)
  7932. (if (c-looking-at-inexpr-block next-containing next-containing)
  7933. ;; We're in an in-expression block of some kind. Do not
  7934. ;; check nesting. We deliberately set the limit to the
  7935. ;; containing sexp, so that c-looking-at-inexpr-block
  7936. ;; doesn't check for an identifier before it.
  7937. (setq containing-sexp nil)
  7938. ;; see if the open brace is preceded by = or [...] in
  7939. ;; this statement, but watch out for operator=
  7940. (setq braceassignp 'dontknow)
  7941. (c-backward-token-2 1 t lim)
  7942. ;; Checks to do only on the first sexp before the brace.
  7943. (when (and c-opt-inexpr-brace-list-key
  7944. (eq (char-after) ?\[))
  7945. ;; In Java, an initialization brace list may follow
  7946. ;; directly after "new Foo[]", so check for a "new"
  7947. ;; earlier.
  7948. (while (eq braceassignp 'dontknow)
  7949. (setq braceassignp
  7950. (cond ((/= (c-backward-token-2 1 t lim) 0) nil)
  7951. ((looking-at c-opt-inexpr-brace-list-key) t)
  7952. ((looking-at "\\sw\\|\\s_\\|[.[]")
  7953. ;; Carry on looking if this is an
  7954. ;; identifier (may contain "." in Java)
  7955. ;; or another "[]" sexp.
  7956. 'dontknow)
  7957. (t nil)))))
  7958. ;; Checks to do on all sexps before the brace, up to the
  7959. ;; beginning of the statement.
  7960. (while (eq braceassignp 'dontknow)
  7961. (cond ((eq (char-after) ?\;)
  7962. (setq braceassignp nil))
  7963. ((and class-key
  7964. (looking-at class-key))
  7965. (setq braceassignp nil))
  7966. ((eq (char-after) ?=)
  7967. ;; We've seen a =, but must check earlier tokens so
  7968. ;; that it isn't something that should be ignored.
  7969. (setq braceassignp 'maybe)
  7970. (while (and (eq braceassignp 'maybe)
  7971. (zerop (c-backward-token-2 1 t lim)))
  7972. (setq braceassignp
  7973. (cond
  7974. ;; Check for operator =
  7975. ((and c-opt-op-identifier-prefix
  7976. (looking-at c-opt-op-identifier-prefix))
  7977. nil)
  7978. ;; Check for `<opchar>= in Pike.
  7979. ((and (c-major-mode-is 'pike-mode)
  7980. (or (eq (char-after) ?`)
  7981. ;; Special case for Pikes
  7982. ;; `[]=, since '[' is not in
  7983. ;; the punctuation class.
  7984. (and (eq (char-after) ?\[)
  7985. (eq (char-before) ?`))))
  7986. nil)
  7987. ((looking-at "\\s.") 'maybe)
  7988. ;; make sure we're not in a C++ template
  7989. ;; argument assignment
  7990. ((and
  7991. (c-major-mode-is 'c++-mode)
  7992. (save-excursion
  7993. (let ((here (point))
  7994. (pos< (progn
  7995. (skip-chars-backward "^<>")
  7996. (point))))
  7997. (and (eq (char-before) ?<)
  7998. (not (c-crosses-statement-barrier-p
  7999. pos< here))
  8000. (not (c-in-literal))
  8001. ))))
  8002. nil)
  8003. (t t))))))
  8004. (if (and (eq braceassignp 'dontknow)
  8005. (/= (c-backward-token-2 1 t lim) 0))
  8006. (setq braceassignp nil)))
  8007. (cond
  8008. (braceassignp
  8009. ;; We've hit the beginning of the aggregate list.
  8010. (c-beginning-of-statement-1
  8011. (c-most-enclosing-brace paren-state))
  8012. (setq bufpos (point)))
  8013. ((eq (char-after) ?\;)
  8014. ;; Brace lists can't contain a semicolon, so we're done.
  8015. (setq containing-sexp nil))
  8016. ((and (setq macro-start (point))
  8017. (c-forward-to-cpp-define-body)
  8018. (eq (point) containing-sexp))
  8019. ;; We've a macro whose expansion starts with the '{'.
  8020. ;; Heuristically, if we have a ';' in it we've not got a
  8021. ;; brace list, otherwise we have.
  8022. (let ((macro-end (progn (c-end-of-macro) (point))))
  8023. (goto-char containing-sexp)
  8024. (forward-char)
  8025. (if (and (c-syntactic-re-search-forward "[;,]" macro-end t t)
  8026. (eq (char-before) ?\;))
  8027. (setq bufpos nil
  8028. containing-sexp nil)
  8029. (setq bufpos macro-start))))
  8030. (t
  8031. ;; Go up one level
  8032. (setq containing-sexp next-containing
  8033. lim nil
  8034. next-containing nil)))))
  8035. bufpos))
  8036. ))
  8037. (defun c-looking-at-special-brace-list (&optional lim)
  8038. ;; If we're looking at the start of a pike-style list, i.e., `({ })',
  8039. ;; `([ ])', `(< >)', etc., a cons of a cons of its starting and ending
  8040. ;; positions and its entry in c-special-brace-lists is returned, nil
  8041. ;; otherwise. The ending position is nil if the list is still open.
  8042. ;; LIM is the limit for forward search. The point may either be at
  8043. ;; the `(' or at the following paren character. Tries to check the
  8044. ;; matching closer, but assumes it's correct if no balanced paren is
  8045. ;; found (i.e. the case `({ ... } ... )' is detected as _not_ being
  8046. ;; a special brace list).
  8047. ;;
  8048. ;; This function might do hidden buffer changes.
  8049. (if c-special-brace-lists
  8050. (condition-case ()
  8051. (save-excursion
  8052. (let ((beg (point))
  8053. inner-beg end type)
  8054. (c-forward-syntactic-ws)
  8055. (if (eq (char-after) ?\()
  8056. (progn
  8057. (forward-char 1)
  8058. (c-forward-syntactic-ws)
  8059. (setq inner-beg (point))
  8060. (setq type (assq (char-after) c-special-brace-lists)))
  8061. (if (setq type (assq (char-after) c-special-brace-lists))
  8062. (progn
  8063. (setq inner-beg (point))
  8064. (c-backward-syntactic-ws)
  8065. (forward-char -1)
  8066. (setq beg (if (eq (char-after) ?\()
  8067. (point)
  8068. nil)))))
  8069. (if (and beg type)
  8070. (if (and (c-safe
  8071. (goto-char beg)
  8072. (c-forward-sexp 1)
  8073. (setq end (point))
  8074. (= (char-before) ?\)))
  8075. (c-safe
  8076. (goto-char inner-beg)
  8077. (if (looking-at "\\s(")
  8078. ;; Check balancing of the inner paren
  8079. ;; below.
  8080. (progn
  8081. (c-forward-sexp 1)
  8082. t)
  8083. ;; If the inner char isn't a paren then
  8084. ;; we can't check balancing, so just
  8085. ;; check the char before the outer
  8086. ;; closing paren.
  8087. (goto-char end)
  8088. (backward-char)
  8089. (c-backward-syntactic-ws)
  8090. (= (char-before) (cdr type)))))
  8091. (if (or (/= (char-syntax (char-before)) ?\))
  8092. (= (progn
  8093. (c-forward-syntactic-ws)
  8094. (point))
  8095. (1- end)))
  8096. (cons (cons beg end) type))
  8097. (cons (list beg) type)))))
  8098. (error nil))))
  8099. (defun c-looking-at-bos (&optional lim)
  8100. ;; Return non-nil if between two statements or declarations, assuming
  8101. ;; point is not inside a literal or comment.
  8102. ;;
  8103. ;; Obsolete - `c-at-statement-start-p' or `c-at-expression-start-p'
  8104. ;; are recommended instead.
  8105. ;;
  8106. ;; This function might do hidden buffer changes.
  8107. (c-at-statement-start-p))
  8108. (make-obsolete 'c-looking-at-bos 'c-at-statement-start-p "22.1")
  8109. (defun c-looking-at-inexpr-block (lim containing-sexp &optional check-at-end)
  8110. ;; Return non-nil if we're looking at the beginning of a block
  8111. ;; inside an expression. The value returned is actually a cons of
  8112. ;; either 'inlambda, 'inexpr-statement or 'inexpr-class and the
  8113. ;; position of the beginning of the construct.
  8114. ;;
  8115. ;; LIM limits the backward search. CONTAINING-SEXP is the start
  8116. ;; position of the closest containing list. If it's nil, the
  8117. ;; containing paren isn't used to decide whether we're inside an
  8118. ;; expression or not. If both LIM and CONTAINING-SEXP are used, LIM
  8119. ;; needs to be farther back.
  8120. ;;
  8121. ;; If CHECK-AT-END is non-nil then extra checks at the end of the
  8122. ;; brace block might be done. It should only be used when the
  8123. ;; construct can be assumed to be complete, i.e. when the original
  8124. ;; starting position was further down than that.
  8125. ;;
  8126. ;; This function might do hidden buffer changes.
  8127. (save-excursion
  8128. (let ((res 'maybe) passed-paren
  8129. (closest-lim (or containing-sexp lim (point-min)))
  8130. ;; Look at the character after point only as a last resort
  8131. ;; when we can't disambiguate.
  8132. (block-follows (and (eq (char-after) ?{) (point))))
  8133. (while (and (eq res 'maybe)
  8134. (progn (c-backward-syntactic-ws)
  8135. (> (point) closest-lim))
  8136. (not (bobp))
  8137. (progn (backward-char)
  8138. (looking-at "[\]\).]\\|\\w\\|\\s_"))
  8139. (c-safe (forward-char)
  8140. (goto-char (scan-sexps (point) -1))))
  8141. (setq res
  8142. (if (looking-at c-keywords-regexp)
  8143. (let ((kw-sym (c-keyword-sym (match-string 1))))
  8144. (cond
  8145. ((and block-follows
  8146. (c-keyword-member kw-sym 'c-inexpr-class-kwds))
  8147. (and (not (eq passed-paren ?\[))
  8148. (or (not (looking-at c-class-key))
  8149. ;; If the class definition is at the start of
  8150. ;; a statement, we don't consider it an
  8151. ;; in-expression class.
  8152. (let ((prev (point)))
  8153. (while (and
  8154. (= (c-backward-token-2 1 nil closest-lim) 0)
  8155. (eq (char-syntax (char-after)) ?w))
  8156. (setq prev (point)))
  8157. (goto-char prev)
  8158. (not (c-at-statement-start-p)))
  8159. ;; Also, in Pike we treat it as an
  8160. ;; in-expression class if it's used in an
  8161. ;; object clone expression.
  8162. (save-excursion
  8163. (and check-at-end
  8164. (c-major-mode-is 'pike-mode)
  8165. (progn (goto-char block-follows)
  8166. (zerop (c-forward-token-2 1 t)))
  8167. (eq (char-after) ?\())))
  8168. (cons 'inexpr-class (point))))
  8169. ((c-keyword-member kw-sym 'c-inexpr-block-kwds)
  8170. (when (not passed-paren)
  8171. (cons 'inexpr-statement (point))))
  8172. ((c-keyword-member kw-sym 'c-lambda-kwds)
  8173. (when (or (not passed-paren)
  8174. (eq passed-paren ?\())
  8175. (cons 'inlambda (point))))
  8176. ((c-keyword-member kw-sym 'c-block-stmt-kwds)
  8177. nil)
  8178. (t
  8179. 'maybe)))
  8180. (if (looking-at "\\s(")
  8181. (if passed-paren
  8182. (if (and (eq passed-paren ?\[)
  8183. (eq (char-after) ?\[))
  8184. ;; Accept several square bracket sexps for
  8185. ;; Java array initializations.
  8186. 'maybe)
  8187. (setq passed-paren (char-after))
  8188. 'maybe)
  8189. 'maybe))))
  8190. (if (eq res 'maybe)
  8191. (when (and c-recognize-paren-inexpr-blocks
  8192. block-follows
  8193. containing-sexp
  8194. (eq (char-after containing-sexp) ?\())
  8195. (goto-char containing-sexp)
  8196. (if (or (save-excursion
  8197. (c-backward-syntactic-ws lim)
  8198. (and (> (point) (or lim (point-min)))
  8199. (c-on-identifier)))
  8200. (and c-special-brace-lists
  8201. (c-looking-at-special-brace-list)))
  8202. nil
  8203. (cons 'inexpr-statement (point))))
  8204. res))))
  8205. (defun c-looking-at-inexpr-block-backward (paren-state)
  8206. ;; Returns non-nil if we're looking at the end of an in-expression
  8207. ;; block, otherwise the same as `c-looking-at-inexpr-block'.
  8208. ;; PAREN-STATE is the paren state relevant at the current position.
  8209. ;;
  8210. ;; This function might do hidden buffer changes.
  8211. (save-excursion
  8212. ;; We currently only recognize a block.
  8213. (let ((here (point))
  8214. (elem (car-safe paren-state))
  8215. containing-sexp)
  8216. (when (and (consp elem)
  8217. (progn (goto-char (cdr elem))
  8218. (c-forward-syntactic-ws here)
  8219. (= (point) here)))
  8220. (goto-char (car elem))
  8221. (if (setq paren-state (cdr paren-state))
  8222. (setq containing-sexp (car-safe paren-state)))
  8223. (c-looking-at-inexpr-block (c-safe-position containing-sexp
  8224. paren-state)
  8225. containing-sexp)))))
  8226. (defun c-at-macro-vsemi-p (&optional pos)
  8227. ;; Is there a "virtual semicolon" at POS or point?
  8228. ;; (See cc-defs.el for full details of "virtual semicolons".)
  8229. ;;
  8230. ;; This is true when point is at the last non syntactic WS position on the
  8231. ;; line, there is a macro call last on the line, and this particular macro's
  8232. ;; name is defined by the regexp `c-vs-macro-regexp' as not needing a
  8233. ;; semicolon.
  8234. (save-excursion
  8235. (save-restriction
  8236. (widen)
  8237. (if pos
  8238. (goto-char pos)
  8239. (setq pos (point)))
  8240. (and
  8241. c-macro-with-semi-re
  8242. (eq (skip-chars-backward " \t") 0)
  8243. ;; Check we've got nothing after this except comments and empty lines
  8244. ;; joined by escaped EOLs.
  8245. (skip-chars-forward " \t") ; always returns non-nil.
  8246. (progn
  8247. (while ; go over 1 block comment per iteration.
  8248. (and
  8249. (looking-at "\\(\\\\[\n\r][ \t]*\\)*")
  8250. (goto-char (match-end 0))
  8251. (cond
  8252. ((looking-at c-block-comment-start-regexp)
  8253. (and (forward-comment 1)
  8254. (skip-chars-forward " \t"))) ; always returns non-nil
  8255. ((looking-at c-line-comment-start-regexp)
  8256. (end-of-line)
  8257. nil)
  8258. (t nil))))
  8259. (eolp))
  8260. (goto-char pos)
  8261. (progn (c-backward-syntactic-ws)
  8262. (eq (point) pos))
  8263. ;; Check for one of the listed macros being before point.
  8264. (or (not (eq (char-before) ?\)))
  8265. (when (c-go-list-backward)
  8266. (c-backward-syntactic-ws)
  8267. t))
  8268. (c-simple-skip-symbol-backward)
  8269. (looking-at c-macro-with-semi-re)
  8270. (goto-char pos)
  8271. (not (c-in-literal)))))) ; The most expensive check last.
  8272. (defun c-macro-vsemi-status-unknown-p () t) ; See cc-defs.el.
  8273. ;; `c-guess-basic-syntax' and the functions that precedes it below
  8274. ;; implements the main decision tree for determining the syntactic
  8275. ;; analysis of the current line of code.
  8276. ;; Dynamically bound to t when `c-guess-basic-syntax' is called during
  8277. ;; auto newline analysis.
  8278. (defvar c-auto-newline-analysis nil)
  8279. (defun c-brace-anchor-point (bracepos)
  8280. ;; BRACEPOS is the position of a brace in a construct like "namespace
  8281. ;; Bar {". Return the anchor point in this construct; this is the
  8282. ;; earliest symbol on the brace's line which isn't earlier than
  8283. ;; "namespace".
  8284. ;;
  8285. ;; Currently (2007-08-17), "like namespace" means "matches
  8286. ;; c-other-block-decl-kwds". It doesn't work with "class" or "struct"
  8287. ;; or anything like that.
  8288. (save-excursion
  8289. (let ((boi (c-point 'boi bracepos)))
  8290. (goto-char bracepos)
  8291. (while (and (> (point) boi)
  8292. (not (looking-at c-other-decl-block-key)))
  8293. (c-backward-token-2))
  8294. (if (> (point) boi) (point) boi))))
  8295. (defsubst c-add-syntax (symbol &rest args)
  8296. ;; A simple function to prepend a new syntax element to
  8297. ;; `c-syntactic-context'. Using `setq' on it is unsafe since it
  8298. ;; should always be dynamically bound but since we read it first
  8299. ;; we'll fail properly anyway if this function is misused.
  8300. (setq c-syntactic-context (cons (cons symbol args)
  8301. c-syntactic-context)))
  8302. (defsubst c-append-syntax (symbol &rest args)
  8303. ;; Like `c-add-syntax' but appends to the end of the syntax list.
  8304. ;; (Normally not necessary.)
  8305. (setq c-syntactic-context (nconc c-syntactic-context
  8306. (list (cons symbol args)))))
  8307. (defun c-add-stmt-syntax (syntax-symbol
  8308. syntax-extra-args
  8309. stop-at-boi-only
  8310. containing-sexp
  8311. paren-state)
  8312. ;; Add the indicated SYNTAX-SYMBOL to `c-syntactic-context', extending it as
  8313. ;; needed with further syntax elements of the types `substatement',
  8314. ;; `inexpr-statement', `arglist-cont-nonempty', `statement-block-intro', and
  8315. ;; `defun-block-intro'.
  8316. ;;
  8317. ;; Do the generic processing to anchor the given syntax symbol on
  8318. ;; the preceding statement: Skip over any labels and containing
  8319. ;; statements on the same line, and then search backward until we
  8320. ;; find a statement or block start that begins at boi without a
  8321. ;; label or comment.
  8322. ;;
  8323. ;; Point is assumed to be at the prospective anchor point for the
  8324. ;; given SYNTAX-SYMBOL. More syntax entries are added if we need to
  8325. ;; skip past open parens and containing statements. Most of the added
  8326. ;; syntax elements will get the same anchor point - the exception is
  8327. ;; for an anchor in a construct like "namespace"[*] - this is as early
  8328. ;; as possible in the construct but on the same line as the {.
  8329. ;;
  8330. ;; [*] i.e. with a keyword matching c-other-block-decl-kwds.
  8331. ;;
  8332. ;; SYNTAX-EXTRA-ARGS are a list of the extra arguments for the
  8333. ;; syntax symbol. They are appended after the anchor point.
  8334. ;;
  8335. ;; If STOP-AT-BOI-ONLY is nil, we can stop in the middle of the line
  8336. ;; if the current statement starts there.
  8337. ;;
  8338. ;; Note: It's not a problem if PAREN-STATE "overshoots"
  8339. ;; CONTAINING-SEXP, i.e. contains info about parens further down.
  8340. ;;
  8341. ;; This function might do hidden buffer changes.
  8342. (if (= (point) (c-point 'boi))
  8343. ;; This is by far the most common case, so let's give it special
  8344. ;; treatment.
  8345. (apply 'c-add-syntax syntax-symbol (point) syntax-extra-args)
  8346. (let ((syntax-last c-syntactic-context)
  8347. (boi (c-point 'boi))
  8348. ;; Set when we're on a label, so that we don't stop there.
  8349. ;; FIXME: To be complete we should check if we're on a label
  8350. ;; now at the start.
  8351. on-label)
  8352. ;; Use point as the anchor point for "namespace", "extern", etc.
  8353. (apply 'c-add-syntax syntax-symbol
  8354. (if (rassq syntax-symbol c-other-decl-block-key-in-symbols-alist)
  8355. (point) nil)
  8356. syntax-extra-args)
  8357. ;; Loop while we have to back out of containing blocks.
  8358. (while
  8359. (and
  8360. (catch 'back-up-block
  8361. ;; Loop while we have to back up statements.
  8362. (while (or (/= (point) boi)
  8363. on-label
  8364. (looking-at c-comment-start-regexp))
  8365. ;; Skip past any comments that stands between the
  8366. ;; statement start and boi.
  8367. (let ((savepos (point)))
  8368. (while (and (/= savepos boi)
  8369. (c-backward-single-comment))
  8370. (setq savepos (point)
  8371. boi (c-point 'boi)))
  8372. (goto-char savepos))
  8373. ;; Skip to the beginning of this statement or backward
  8374. ;; another one.
  8375. (let ((old-pos (point))
  8376. (old-boi boi)
  8377. (step-type (c-beginning-of-statement-1 containing-sexp)))
  8378. (setq boi (c-point 'boi)
  8379. on-label (eq step-type 'label))
  8380. (cond ((= (point) old-pos)
  8381. ;; If we didn't move we're at the start of a block and
  8382. ;; have to continue outside it.
  8383. (throw 'back-up-block t))
  8384. ((and (eq step-type 'up)
  8385. (>= (point) old-boi)
  8386. (looking-at "else\\>[^_]")
  8387. (save-excursion
  8388. (goto-char old-pos)
  8389. (looking-at "if\\>[^_]")))
  8390. ;; Special case to avoid deeper and deeper indentation
  8391. ;; of "else if" clauses.
  8392. )
  8393. ((and (not stop-at-boi-only)
  8394. (/= old-pos old-boi)
  8395. (memq step-type '(up previous)))
  8396. ;; If stop-at-boi-only is nil, we shouldn't back up
  8397. ;; over previous or containing statements to try to
  8398. ;; reach boi, so go back to the last position and
  8399. ;; exit.
  8400. (goto-char old-pos)
  8401. (throw 'back-up-block nil))
  8402. (t
  8403. (if (and (not stop-at-boi-only)
  8404. (memq step-type '(up previous beginning)))
  8405. ;; If we've moved into another statement then we
  8406. ;; should no longer try to stop in the middle of a
  8407. ;; line.
  8408. (setq stop-at-boi-only t))
  8409. ;; Record this as a substatement if we skipped up one
  8410. ;; level.
  8411. (when (eq step-type 'up)
  8412. (c-add-syntax 'substatement nil))))
  8413. )))
  8414. containing-sexp)
  8415. ;; Now we have to go out of this block.
  8416. (goto-char containing-sexp)
  8417. ;; Don't stop in the middle of a special brace list opener
  8418. ;; like "({".
  8419. (when c-special-brace-lists
  8420. (let ((special-list (c-looking-at-special-brace-list)))
  8421. (when (and special-list
  8422. (< (car (car special-list)) (point)))
  8423. (setq containing-sexp (car (car special-list)))
  8424. (goto-char containing-sexp))))
  8425. (setq paren-state (c-whack-state-after containing-sexp paren-state)
  8426. containing-sexp (c-most-enclosing-brace paren-state)
  8427. boi (c-point 'boi))
  8428. ;; Analyze the construct in front of the block we've stepped out
  8429. ;; from and add the right syntactic element for it.
  8430. (let ((paren-pos (point))
  8431. (paren-char (char-after))
  8432. step-type)
  8433. (if (eq paren-char ?\()
  8434. ;; Stepped out of a parenthesis block, so we're in an
  8435. ;; expression now.
  8436. (progn
  8437. (when (/= paren-pos boi)
  8438. (if (and c-recognize-paren-inexpr-blocks
  8439. (progn
  8440. (c-backward-syntactic-ws containing-sexp)
  8441. (or (not (looking-at "\\>"))
  8442. (not (c-on-identifier))))
  8443. (save-excursion
  8444. (goto-char (1+ paren-pos))
  8445. (c-forward-syntactic-ws)
  8446. (eq (char-after) ?{)))
  8447. ;; Stepped out of an in-expression statement. This
  8448. ;; syntactic element won't get an anchor pos.
  8449. (c-add-syntax 'inexpr-statement)
  8450. ;; A parenthesis normally belongs to an arglist.
  8451. (c-add-syntax 'arglist-cont-nonempty nil paren-pos)))
  8452. (goto-char (max boi
  8453. (if containing-sexp
  8454. (1+ containing-sexp)
  8455. (point-min))))
  8456. (setq step-type 'same
  8457. on-label nil))
  8458. ;; Stepped out of a brace block.
  8459. (setq step-type (c-beginning-of-statement-1 containing-sexp)
  8460. on-label (eq step-type 'label))
  8461. (if (and (eq step-type 'same)
  8462. (/= paren-pos (point)))
  8463. (let (inexpr)
  8464. (cond
  8465. ((save-excursion
  8466. (goto-char paren-pos)
  8467. (setq inexpr (c-looking-at-inexpr-block
  8468. (c-safe-position containing-sexp paren-state)
  8469. containing-sexp)))
  8470. (c-add-syntax (if (eq (car inexpr) 'inlambda)
  8471. 'defun-block-intro
  8472. 'statement-block-intro)
  8473. nil))
  8474. ((looking-at c-other-decl-block-key)
  8475. (c-add-syntax
  8476. (cdr (assoc (match-string 1)
  8477. c-other-decl-block-key-in-symbols-alist))
  8478. (max (c-point 'boi paren-pos) (point))))
  8479. (t (c-add-syntax 'defun-block-intro nil))))
  8480. (c-add-syntax 'statement-block-intro nil)))
  8481. (if (= paren-pos boi)
  8482. ;; Always done if the open brace was at boi. The
  8483. ;; c-beginning-of-statement-1 call above is necessary
  8484. ;; anyway, to decide the type of block-intro to add.
  8485. (goto-char paren-pos)
  8486. (setq boi (c-point 'boi)))
  8487. ))
  8488. ;; Fill in the current point as the anchor for all the symbols
  8489. ;; added above.
  8490. (let ((p c-syntactic-context) q)
  8491. (while (not (eq p syntax-last))
  8492. (setq q (cdr (car p))) ; e.g. (nil 28) [from (arglist-cont-nonempty nil 28)]
  8493. (while q
  8494. (unless (car q)
  8495. (setcar q (point)))
  8496. (setq q (cdr q)))
  8497. (setq p (cdr p))))
  8498. )))
  8499. (defun c-add-class-syntax (symbol
  8500. containing-decl-open
  8501. containing-decl-start
  8502. containing-decl-kwd
  8503. paren-state)
  8504. ;; The inclass and class-close syntactic symbols are added in
  8505. ;; several places and some work is needed to fix everything.
  8506. ;; Therefore it's collected here.
  8507. ;;
  8508. ;; This function might do hidden buffer changes.
  8509. (goto-char containing-decl-open)
  8510. (if (and (eq symbol 'inclass) (= (point) (c-point 'boi)))
  8511. (progn
  8512. (c-add-syntax symbol containing-decl-open)
  8513. containing-decl-open)
  8514. (goto-char containing-decl-start)
  8515. ;; Ought to use `c-add-stmt-syntax' instead of backing up to boi
  8516. ;; here, but we have to do like this for compatibility.
  8517. (back-to-indentation)
  8518. (c-add-syntax symbol (point))
  8519. (if (and (c-keyword-member containing-decl-kwd
  8520. 'c-inexpr-class-kwds)
  8521. (/= containing-decl-start (c-point 'boi containing-decl-start)))
  8522. (c-add-syntax 'inexpr-class))
  8523. (point)))
  8524. (defun c-guess-continued-construct (indent-point
  8525. char-after-ip
  8526. beg-of-same-or-containing-stmt
  8527. containing-sexp
  8528. paren-state)
  8529. ;; This function contains the decision tree reached through both
  8530. ;; cases 18 and 10. It's a continued statement or top level
  8531. ;; construct of some kind.
  8532. ;;
  8533. ;; This function might do hidden buffer changes.
  8534. (let (special-brace-list placeholder)
  8535. (goto-char indent-point)
  8536. (skip-chars-forward " \t")
  8537. (cond
  8538. ;; (CASE A removed.)
  8539. ;; CASE B: open braces for class or brace-lists
  8540. ((setq special-brace-list
  8541. (or (and c-special-brace-lists
  8542. (c-looking-at-special-brace-list))
  8543. (eq char-after-ip ?{)))
  8544. (cond
  8545. ;; CASE B.1: class-open
  8546. ((save-excursion
  8547. (and (eq (char-after) ?{)
  8548. (c-looking-at-decl-block containing-sexp t)
  8549. (setq beg-of-same-or-containing-stmt (point))))
  8550. (c-add-syntax 'class-open beg-of-same-or-containing-stmt))
  8551. ;; CASE B.2: brace-list-open
  8552. ((or (consp special-brace-list)
  8553. (save-excursion
  8554. (goto-char beg-of-same-or-containing-stmt)
  8555. (c-syntactic-re-search-forward "=\\([^=]\\|$\\)"
  8556. indent-point t t t)))
  8557. ;; The most semantically accurate symbol here is
  8558. ;; brace-list-open, but we normally report it simply as a
  8559. ;; statement-cont. The reason is that one normally adjusts
  8560. ;; brace-list-open for brace lists as top-level constructs,
  8561. ;; and brace lists inside statements is a completely different
  8562. ;; context. C.f. case 5A.3.
  8563. (c-beginning-of-statement-1 containing-sexp)
  8564. (c-add-stmt-syntax (if c-auto-newline-analysis
  8565. ;; Turn off the dwim above when we're
  8566. ;; analyzing the nature of the brace
  8567. ;; for the auto newline feature.
  8568. 'brace-list-open
  8569. 'statement-cont)
  8570. nil nil
  8571. containing-sexp paren-state))
  8572. ;; CASE B.3: The body of a function declared inside a normal
  8573. ;; block. Can occur e.g. in Pike and when using gcc
  8574. ;; extensions, but watch out for macros followed by blocks.
  8575. ;; C.f. cases E, 16F and 17G.
  8576. ((and (not (c-at-statement-start-p))
  8577. (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
  8578. 'same)
  8579. (save-excursion
  8580. (let ((c-recognize-typeless-decls nil))
  8581. ;; Turn off recognition of constructs that lacks a
  8582. ;; type in this case, since that's more likely to be
  8583. ;; a macro followed by a block.
  8584. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  8585. (c-add-stmt-syntax 'defun-open nil t
  8586. containing-sexp paren-state))
  8587. ;; CASE B.4: Continued statement with block open. The most
  8588. ;; accurate analysis is perhaps `statement-cont' together with
  8589. ;; `block-open' but we play DWIM and use `substatement-open'
  8590. ;; instead. The rationale is that this typically is a macro
  8591. ;; followed by a block which makes it very similar to a
  8592. ;; statement with a substatement block.
  8593. (t
  8594. (c-add-stmt-syntax 'substatement-open nil nil
  8595. containing-sexp paren-state))
  8596. ))
  8597. ;; CASE C: iostream insertion or extraction operator
  8598. ((and (looking-at "\\(<<\\|>>\\)\\([^=]\\|$\\)")
  8599. (save-excursion
  8600. (goto-char beg-of-same-or-containing-stmt)
  8601. ;; If there is no preceding streamop in the statement
  8602. ;; then indent this line as a normal statement-cont.
  8603. (when (c-syntactic-re-search-forward
  8604. "\\(<<\\|>>\\)\\([^=]\\|$\\)" indent-point 'move t t)
  8605. (c-add-syntax 'stream-op (c-point 'boi))
  8606. t))))
  8607. ;; CASE E: In the "K&R region" of a function declared inside a
  8608. ;; normal block. C.f. case B.3.
  8609. ((and (save-excursion
  8610. ;; Check that the next token is a '{'. This works as
  8611. ;; long as no language that allows nested function
  8612. ;; definitions allows stuff like member init lists, K&R
  8613. ;; declarations or throws clauses there.
  8614. ;;
  8615. ;; Note that we do a forward search for something ahead
  8616. ;; of the indentation line here. That's not good since
  8617. ;; the user might not have typed it yet. Unfortunately
  8618. ;; it's exceedingly tricky to recognize a function
  8619. ;; prototype in a code block without resorting to this.
  8620. (c-forward-syntactic-ws)
  8621. (eq (char-after) ?{))
  8622. (not (c-at-statement-start-p))
  8623. (eq (c-beginning-of-statement-1 containing-sexp nil nil t)
  8624. 'same)
  8625. (save-excursion
  8626. (let ((c-recognize-typeless-decls nil))
  8627. ;; Turn off recognition of constructs that lacks a
  8628. ;; type in this case, since that's more likely to be
  8629. ;; a macro followed by a block.
  8630. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  8631. (c-add-stmt-syntax 'func-decl-cont nil t
  8632. containing-sexp paren-state))
  8633. ;;CASE F: continued statement and the only preceding items are
  8634. ;;annotations.
  8635. ((and (c-major-mode-is 'java-mode)
  8636. (setq placeholder (point))
  8637. (c-beginning-of-statement-1)
  8638. (progn
  8639. (while (and (c-forward-annotation)
  8640. (< (point) placeholder))
  8641. (c-forward-syntactic-ws))
  8642. t)
  8643. (prog1
  8644. (>= (point) placeholder)
  8645. (goto-char placeholder)))
  8646. (c-beginning-of-statement-1 containing-sexp)
  8647. (c-add-syntax 'annotation-var-cont (point)))
  8648. ;; CASE G: a template list continuation?
  8649. ;; Mostly a duplication of case 5D.3 to fix templates-19:
  8650. ((and (c-major-mode-is 'c++-mode)
  8651. (save-excursion
  8652. (goto-char indent-point)
  8653. (c-with-syntax-table c++-template-syntax-table
  8654. (setq placeholder (c-up-list-backward)))
  8655. (and placeholder
  8656. (eq (char-after placeholder) ?<)
  8657. (/= (char-before placeholder) ?<)
  8658. (progn
  8659. (goto-char (1+ placeholder))
  8660. (not (looking-at c-<-op-cont-regexp))))))
  8661. (c-with-syntax-table c++-template-syntax-table
  8662. (goto-char placeholder)
  8663. (c-beginning-of-statement-1 containing-sexp t))
  8664. (if (save-excursion
  8665. (c-backward-syntactic-ws containing-sexp)
  8666. (eq (char-before) ?<))
  8667. ;; In a nested template arglist.
  8668. (progn
  8669. (goto-char placeholder)
  8670. (c-syntactic-skip-backward "^,;" containing-sexp t)
  8671. (c-forward-syntactic-ws))
  8672. (back-to-indentation))
  8673. ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
  8674. ;; template aware.
  8675. (c-add-syntax 'template-args-cont (point) placeholder))
  8676. ;; CASE D: continued statement.
  8677. (t
  8678. (c-beginning-of-statement-1 containing-sexp)
  8679. (c-add-stmt-syntax 'statement-cont nil nil
  8680. containing-sexp paren-state))
  8681. )))
  8682. ;; The next autoload was added by RMS on 2005/8/9 - don't know why (ACM,
  8683. ;; 2005/11/29).
  8684. ;;;###autoload
  8685. (defun c-guess-basic-syntax ()
  8686. "Return the syntactic context of the current line."
  8687. (save-excursion
  8688. (beginning-of-line)
  8689. (c-save-buffer-state
  8690. ((indent-point (point))
  8691. (case-fold-search nil)
  8692. open-paren-in-column-0-is-defun-start
  8693. ;; A whole ugly bunch of various temporary variables. Have
  8694. ;; to declare them here since it's not possible to declare
  8695. ;; a variable with only the scope of a cond test and the
  8696. ;; following result clauses, and most of this function is a
  8697. ;; single gigantic cond. :P
  8698. literal char-before-ip before-ws-ip char-after-ip macro-start
  8699. in-macro-expr c-syntactic-context placeholder c-in-literal-cache
  8700. step-type tmpsymbol keyword injava-inher special-brace-list tmp-pos
  8701. containing-<
  8702. ;; The following record some positions for the containing
  8703. ;; declaration block if we're directly within one:
  8704. ;; `containing-decl-open' is the position of the open
  8705. ;; brace. `containing-decl-start' is the start of the
  8706. ;; declaration. `containing-decl-kwd' is the keyword
  8707. ;; symbol of the keyword that tells what kind of block it
  8708. ;; is.
  8709. containing-decl-open
  8710. containing-decl-start
  8711. containing-decl-kwd
  8712. ;; The open paren of the closest surrounding sexp or nil if
  8713. ;; there is none.
  8714. containing-sexp
  8715. ;; The position after the closest preceding brace sexp
  8716. ;; (nested sexps are ignored), or the position after
  8717. ;; `containing-sexp' if there is none, or (point-min) if
  8718. ;; `containing-sexp' is nil.
  8719. lim
  8720. ;; The paren state outside `containing-sexp', or at
  8721. ;; `indent-point' if `containing-sexp' is nil.
  8722. (paren-state (c-parse-state))
  8723. ;; There's always at most one syntactic element which got
  8724. ;; an anchor pos. It's stored in syntactic-relpos.
  8725. syntactic-relpos
  8726. (c-stmt-delim-chars c-stmt-delim-chars))
  8727. ;; Check if we're directly inside an enclosing declaration
  8728. ;; level block.
  8729. (when (and (setq containing-sexp
  8730. (c-most-enclosing-brace paren-state))
  8731. (progn
  8732. (goto-char containing-sexp)
  8733. (eq (char-after) ?{))
  8734. (setq placeholder
  8735. (c-looking-at-decl-block
  8736. (c-most-enclosing-brace paren-state
  8737. containing-sexp)
  8738. t)))
  8739. (setq containing-decl-open containing-sexp
  8740. containing-decl-start (point)
  8741. containing-sexp nil)
  8742. (goto-char placeholder)
  8743. (setq containing-decl-kwd (and (looking-at c-keywords-regexp)
  8744. (c-keyword-sym (match-string 1)))))
  8745. ;; Init some position variables.
  8746. (if paren-state
  8747. (progn
  8748. (setq containing-sexp (car paren-state)
  8749. paren-state (cdr paren-state))
  8750. (if (consp containing-sexp)
  8751. (save-excursion
  8752. (goto-char (cdr containing-sexp))
  8753. (if (and (c-major-mode-is 'c++-mode)
  8754. (c-back-over-member-initializer-braces))
  8755. (c-syntactic-skip-backward "^}" nil t))
  8756. (setq lim (point))
  8757. (if paren-state
  8758. ;; Ignore balanced paren. The next entry
  8759. ;; can't be another one.
  8760. (setq containing-sexp (car paren-state)
  8761. paren-state (cdr paren-state))
  8762. ;; If there is no surrounding open paren then
  8763. ;; put the last balanced pair back on paren-state.
  8764. (setq paren-state (cons containing-sexp paren-state)
  8765. containing-sexp nil)))
  8766. (setq lim (1+ containing-sexp))))
  8767. (setq lim (point-min)))
  8768. ;; If we're in a parenthesis list then ',' delimits the
  8769. ;; "statements" rather than being an operator (with the
  8770. ;; exception of the "for" clause). This difference is
  8771. ;; typically only noticeable when statements are used in macro
  8772. ;; arglists.
  8773. (when (and containing-sexp
  8774. (eq (char-after containing-sexp) ?\())
  8775. (setq c-stmt-delim-chars c-stmt-delim-chars-with-comma))
  8776. ;; cache char before and after indent point, and move point to
  8777. ;; the most likely position to perform the majority of tests
  8778. (goto-char indent-point)
  8779. (c-backward-syntactic-ws lim)
  8780. (setq before-ws-ip (point)
  8781. char-before-ip (char-before))
  8782. (goto-char indent-point)
  8783. (skip-chars-forward " \t")
  8784. (setq char-after-ip (char-after))
  8785. ;; are we in a literal?
  8786. (setq literal (c-in-literal lim))
  8787. ;; now figure out syntactic qualities of the current line
  8788. (cond
  8789. ;; CASE 1: in a string.
  8790. ((eq literal 'string)
  8791. (c-add-syntax 'string (c-point 'bopl)))
  8792. ;; CASE 2: in a C or C++ style comment.
  8793. ((and (memq literal '(c c++))
  8794. ;; This is a kludge for XEmacs where we use
  8795. ;; `buffer-syntactic-context', which doesn't correctly
  8796. ;; recognize "\*/" to end a block comment.
  8797. ;; `parse-partial-sexp' which is used by
  8798. ;; `c-literal-limits' will however do that in most
  8799. ;; versions, which results in that we get nil from
  8800. ;; `c-literal-limits' even when `c-in-literal' claims
  8801. ;; we're inside a comment.
  8802. (setq placeholder (c-literal-limits lim)))
  8803. (c-add-syntax literal (car placeholder)))
  8804. ;; CASE 3: in a cpp preprocessor macro continuation.
  8805. ((and (save-excursion
  8806. (when (c-beginning-of-macro)
  8807. (setq macro-start (point))))
  8808. (/= macro-start (c-point 'boi))
  8809. (progn
  8810. (setq tmpsymbol 'cpp-macro-cont)
  8811. (or (not c-syntactic-indentation-in-macros)
  8812. (save-excursion
  8813. (goto-char macro-start)
  8814. ;; If at the beginning of the body of a #define
  8815. ;; directive then analyze as cpp-define-intro
  8816. ;; only. Go on with the syntactic analysis
  8817. ;; otherwise. in-macro-expr is set if we're in a
  8818. ;; cpp expression, i.e. before the #define body
  8819. ;; or anywhere in a non-#define directive.
  8820. (if (c-forward-to-cpp-define-body)
  8821. (let ((indent-boi (c-point 'boi indent-point)))
  8822. (setq in-macro-expr (> (point) indent-boi)
  8823. tmpsymbol 'cpp-define-intro)
  8824. (= (point) indent-boi))
  8825. (setq in-macro-expr t)
  8826. nil)))))
  8827. (c-add-syntax tmpsymbol macro-start)
  8828. (setq macro-start nil))
  8829. ;; CASE 11: an else clause?
  8830. ((looking-at "else\\>[^_]")
  8831. (c-beginning-of-statement-1 containing-sexp)
  8832. (c-add-stmt-syntax 'else-clause nil t
  8833. containing-sexp paren-state))
  8834. ;; CASE 12: while closure of a do/while construct?
  8835. ((and (looking-at "while\\>[^_]")
  8836. (save-excursion
  8837. (prog1 (eq (c-beginning-of-statement-1 containing-sexp)
  8838. 'beginning)
  8839. (setq placeholder (point)))))
  8840. (goto-char placeholder)
  8841. (c-add-stmt-syntax 'do-while-closure nil t
  8842. containing-sexp paren-state))
  8843. ;; CASE 13: A catch or finally clause? This case is simpler
  8844. ;; than if-else and do-while, because a block is required
  8845. ;; after every try, catch and finally.
  8846. ((save-excursion
  8847. (and (cond ((c-major-mode-is 'c++-mode)
  8848. (looking-at "catch\\>[^_]"))
  8849. ((c-major-mode-is 'java-mode)
  8850. (looking-at "\\(catch\\|finally\\)\\>[^_]")))
  8851. (and (c-safe (c-backward-syntactic-ws)
  8852. (c-backward-sexp)
  8853. t)
  8854. (eq (char-after) ?{)
  8855. (c-safe (c-backward-syntactic-ws)
  8856. (c-backward-sexp)
  8857. t)
  8858. (if (eq (char-after) ?\()
  8859. (c-safe (c-backward-sexp) t)
  8860. t))
  8861. (looking-at "\\(try\\|catch\\)\\>[^_]")
  8862. (setq placeholder (point))))
  8863. (goto-char placeholder)
  8864. (c-add-stmt-syntax 'catch-clause nil t
  8865. containing-sexp paren-state))
  8866. ;; CASE 18: A substatement we can recognize by keyword.
  8867. ((save-excursion
  8868. (and c-opt-block-stmt-key
  8869. (not (eq char-before-ip ?\;))
  8870. (not (c-at-vsemi-p before-ws-ip))
  8871. (not (memq char-after-ip '(?\) ?\] ?,)))
  8872. (or (not (eq char-before-ip ?}))
  8873. (c-looking-at-inexpr-block-backward c-state-cache))
  8874. (> (point)
  8875. (progn
  8876. ;; Ought to cache the result from the
  8877. ;; c-beginning-of-statement-1 calls here.
  8878. (setq placeholder (point))
  8879. (while (eq (setq step-type
  8880. (c-beginning-of-statement-1 lim))
  8881. 'label))
  8882. (if (eq step-type 'previous)
  8883. (goto-char placeholder)
  8884. (setq placeholder (point))
  8885. (if (and (eq step-type 'same)
  8886. (not (looking-at c-opt-block-stmt-key)))
  8887. ;; Step up to the containing statement if we
  8888. ;; stayed in the same one.
  8889. (let (step)
  8890. (while (eq
  8891. (setq step
  8892. (c-beginning-of-statement-1 lim))
  8893. 'label))
  8894. (if (eq step 'up)
  8895. (setq placeholder (point))
  8896. ;; There was no containing statement after all.
  8897. (goto-char placeholder)))))
  8898. placeholder))
  8899. (if (looking-at c-block-stmt-2-key)
  8900. ;; Require a parenthesis after these keywords.
  8901. ;; Necessary to catch e.g. synchronized in Java,
  8902. ;; which can be used both as statement and
  8903. ;; modifier.
  8904. (and (zerop (c-forward-token-2 1 nil))
  8905. (eq (char-after) ?\())
  8906. (looking-at c-opt-block-stmt-key))))
  8907. (if (eq step-type 'up)
  8908. ;; CASE 18A: Simple substatement.
  8909. (progn
  8910. (goto-char placeholder)
  8911. (cond
  8912. ((eq char-after-ip ?{)
  8913. (c-add-stmt-syntax 'substatement-open nil nil
  8914. containing-sexp paren-state))
  8915. ((save-excursion
  8916. (goto-char indent-point)
  8917. (back-to-indentation)
  8918. (c-forward-label))
  8919. (c-add-stmt-syntax 'substatement-label nil nil
  8920. containing-sexp paren-state))
  8921. (t
  8922. (c-add-stmt-syntax 'substatement nil nil
  8923. containing-sexp paren-state))))
  8924. ;; CASE 18B: Some other substatement. This is shared
  8925. ;; with case 10.
  8926. (c-guess-continued-construct indent-point
  8927. char-after-ip
  8928. placeholder
  8929. lim
  8930. paren-state)))
  8931. ;; CASE 14: A case or default label
  8932. ((looking-at c-label-kwds-regexp)
  8933. (if containing-sexp
  8934. (progn
  8935. (goto-char containing-sexp)
  8936. (setq lim (c-most-enclosing-brace c-state-cache
  8937. containing-sexp))
  8938. (c-backward-to-block-anchor lim)
  8939. (c-add-stmt-syntax 'case-label nil t lim paren-state))
  8940. ;; Got a bogus label at the top level. In lack of better
  8941. ;; alternatives, anchor it on (point-min).
  8942. (c-add-syntax 'case-label (point-min))))
  8943. ;; CASE 15: any other label
  8944. ((save-excursion
  8945. (back-to-indentation)
  8946. (and (not (looking-at c-syntactic-ws-start))
  8947. (c-forward-label)))
  8948. (cond (containing-decl-open
  8949. (setq placeholder (c-add-class-syntax 'inclass
  8950. containing-decl-open
  8951. containing-decl-start
  8952. containing-decl-kwd
  8953. paren-state))
  8954. ;; Append access-label with the same anchor point as
  8955. ;; inclass gets.
  8956. (c-append-syntax 'access-label placeholder))
  8957. (containing-sexp
  8958. (goto-char containing-sexp)
  8959. (setq lim (c-most-enclosing-brace c-state-cache
  8960. containing-sexp))
  8961. (save-excursion
  8962. (setq tmpsymbol
  8963. (if (and (eq (c-beginning-of-statement-1 lim) 'up)
  8964. (looking-at "switch\\>[^_]"))
  8965. ;; If the surrounding statement is a switch then
  8966. ;; let's analyze all labels as switch labels, so
  8967. ;; that they get lined up consistently.
  8968. 'case-label
  8969. 'label)))
  8970. (c-backward-to-block-anchor lim)
  8971. (c-add-stmt-syntax tmpsymbol nil t lim paren-state))
  8972. (t
  8973. ;; A label on the top level. Treat it as a class
  8974. ;; context. (point-min) is the closest we get to the
  8975. ;; class open brace.
  8976. (c-add-syntax 'access-label (point-min)))))
  8977. ;; CASE 4: In-expression statement. C.f. cases 7B, 16A and
  8978. ;; 17E.
  8979. ((setq placeholder (c-looking-at-inexpr-block
  8980. (c-safe-position containing-sexp paren-state)
  8981. containing-sexp
  8982. ;; Have to turn on the heuristics after
  8983. ;; the point even though it doesn't work
  8984. ;; very well. C.f. test case class-16.pike.
  8985. t))
  8986. (setq tmpsymbol (assq (car placeholder)
  8987. '((inexpr-class . class-open)
  8988. (inexpr-statement . block-open))))
  8989. (if tmpsymbol
  8990. ;; It's a statement block or an anonymous class.
  8991. (setq tmpsymbol (cdr tmpsymbol))
  8992. ;; It's a Pike lambda. Check whether we are between the
  8993. ;; lambda keyword and the argument list or at the defun
  8994. ;; opener.
  8995. (setq tmpsymbol (if (eq char-after-ip ?{)
  8996. 'inline-open
  8997. 'lambda-intro-cont)))
  8998. (goto-char (cdr placeholder))
  8999. (back-to-indentation)
  9000. (c-add-stmt-syntax tmpsymbol nil t
  9001. (c-most-enclosing-brace c-state-cache (point))
  9002. paren-state)
  9003. (unless (eq (point) (cdr placeholder))
  9004. (c-add-syntax (car placeholder))))
  9005. ;; CASE 5: Line is inside a declaration level block or at top level.
  9006. ((or containing-decl-open (null containing-sexp))
  9007. (cond
  9008. ;; CASE 5A: we are looking at a defun, brace list, class,
  9009. ;; or inline-inclass method opening brace
  9010. ((setq special-brace-list
  9011. (or (and c-special-brace-lists
  9012. (c-looking-at-special-brace-list))
  9013. (eq char-after-ip ?{)))
  9014. (cond
  9015. ;; CASE 5A.1: Non-class declaration block open.
  9016. ((save-excursion
  9017. (let (tmp)
  9018. (and (eq char-after-ip ?{)
  9019. (setq tmp (c-looking-at-decl-block containing-sexp t))
  9020. (progn
  9021. (setq placeholder (point))
  9022. (goto-char tmp)
  9023. (looking-at c-symbol-key))
  9024. (c-keyword-member
  9025. (c-keyword-sym (setq keyword (match-string 0)))
  9026. 'c-other-block-decl-kwds))))
  9027. (goto-char placeholder)
  9028. (c-add-stmt-syntax
  9029. (if (string-equal keyword "extern")
  9030. ;; Special case for extern-lang-open.
  9031. 'extern-lang-open
  9032. (intern (concat keyword "-open")))
  9033. nil t containing-sexp paren-state))
  9034. ;; CASE 5A.2: we are looking at a class opening brace
  9035. ((save-excursion
  9036. (goto-char indent-point)
  9037. (skip-chars-forward " \t")
  9038. (and (eq (char-after) ?{)
  9039. (c-looking-at-decl-block containing-sexp t)
  9040. (setq placeholder (point))))
  9041. (c-add-syntax 'class-open placeholder))
  9042. ;; CASE 5A.3: brace list open
  9043. ((save-excursion
  9044. (c-beginning-of-decl-1 lim)
  9045. (while (looking-at c-specifier-key)
  9046. (goto-char (match-end 1))
  9047. (c-forward-syntactic-ws indent-point))
  9048. (setq placeholder (c-point 'boi))
  9049. (or (consp special-brace-list)
  9050. (and (or (save-excursion
  9051. (goto-char indent-point)
  9052. (setq tmpsymbol nil)
  9053. (while (and (> (point) placeholder)
  9054. (zerop (c-backward-token-2 1 t))
  9055. (not (looking-at "=\\([^=]\\|$\\)")))
  9056. (and c-opt-inexpr-brace-list-key
  9057. (not tmpsymbol)
  9058. (looking-at c-opt-inexpr-brace-list-key)
  9059. (setq tmpsymbol 'topmost-intro-cont)))
  9060. (looking-at "=\\([^=]\\|$\\)"))
  9061. (looking-at c-brace-list-key))
  9062. (save-excursion
  9063. (while (and (< (point) indent-point)
  9064. (zerop (c-forward-token-2 1 t))
  9065. (not (memq (char-after) '(?\; ?\()))))
  9066. (not (memq (char-after) '(?\; ?\()))
  9067. ))))
  9068. (if (and (not c-auto-newline-analysis)
  9069. (c-major-mode-is 'java-mode)
  9070. (eq tmpsymbol 'topmost-intro-cont))
  9071. ;; We're in Java and have found that the open brace
  9072. ;; belongs to a "new Foo[]" initialization list,
  9073. ;; which means the brace list is part of an
  9074. ;; expression and not a top level definition. We
  9075. ;; therefore treat it as any topmost continuation
  9076. ;; even though the semantically correct symbol still
  9077. ;; is brace-list-open, on the same grounds as in
  9078. ;; case B.2.
  9079. (progn
  9080. (c-beginning-of-statement-1 lim)
  9081. (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  9082. (c-add-syntax 'brace-list-open placeholder)))
  9083. ;; CASE 5A.4: inline defun open
  9084. ((and containing-decl-open
  9085. (not (c-keyword-member containing-decl-kwd
  9086. 'c-other-block-decl-kwds)))
  9087. (c-add-syntax 'inline-open)
  9088. (c-add-class-syntax 'inclass
  9089. containing-decl-open
  9090. containing-decl-start
  9091. containing-decl-kwd
  9092. paren-state))
  9093. ;; CASE 5A.5: ordinary defun open
  9094. (t
  9095. (save-excursion
  9096. (c-beginning-of-decl-1 lim)
  9097. (while (looking-at c-specifier-key)
  9098. (goto-char (match-end 1))
  9099. (c-forward-syntactic-ws indent-point))
  9100. (c-add-syntax 'defun-open (c-point 'boi))
  9101. ;; Bogus to use bol here, but it's the legacy. (Resolved,
  9102. ;; 2007-11-09)
  9103. ))))
  9104. ;; CASE 5R: Member init list. (Used to be part of CASE 5B.1)
  9105. ;; Note there is no limit on the backward search here, since member
  9106. ;; init lists can, in practice, be very large.
  9107. ((save-excursion
  9108. (when (setq placeholder (c-back-over-member-initializers))
  9109. (setq tmp-pos (point))))
  9110. (if (= (c-point 'bosws) (1+ tmp-pos))
  9111. (progn
  9112. ;; There is no preceding member init clause.
  9113. ;; Indent relative to the beginning of indentation
  9114. ;; for the topmost-intro line that contains the
  9115. ;; prototype's open paren.
  9116. (goto-char placeholder)
  9117. (c-add-syntax 'member-init-intro (c-point 'boi)))
  9118. ;; Indent relative to the first member init clause.
  9119. (goto-char (1+ tmp-pos))
  9120. (c-forward-syntactic-ws)
  9121. (c-add-syntax 'member-init-cont (point))))
  9122. ;; CASE 5B: After a function header but before the body (or
  9123. ;; the ending semicolon if there's no body).
  9124. ((save-excursion
  9125. (when (setq placeholder (c-just-after-func-arglist-p
  9126. (max lim (c-determine-limit 500))))
  9127. (setq tmp-pos (point))))
  9128. (cond
  9129. ;; CASE 5B.1: Member init list.
  9130. ((eq (char-after tmp-pos) ?:)
  9131. ;; There is no preceding member init clause.
  9132. ;; Indent relative to the beginning of indentation
  9133. ;; for the topmost-intro line that contains the
  9134. ;; prototype's open paren.
  9135. (goto-char placeholder)
  9136. (c-add-syntax 'member-init-intro (c-point 'boi)))
  9137. ;; CASE 5B.2: K&R arg decl intro
  9138. ((and c-recognize-knr-p
  9139. (c-in-knr-argdecl lim))
  9140. (c-beginning-of-statement-1 lim)
  9141. (c-add-syntax 'knr-argdecl-intro (c-point 'boi))
  9142. (if containing-decl-open
  9143. (c-add-class-syntax 'inclass
  9144. containing-decl-open
  9145. containing-decl-start
  9146. containing-decl-kwd
  9147. paren-state)))
  9148. ;; CASE 5B.4: Nether region after a C++ or Java func
  9149. ;; decl, which could include a `throws' declaration.
  9150. (t
  9151. (c-beginning-of-statement-1 lim)
  9152. (c-add-syntax 'func-decl-cont (c-point 'boi))
  9153. )))
  9154. ;; CASE 5C: inheritance line. could be first inheritance
  9155. ;; line, or continuation of a multiple inheritance
  9156. ((or (and (c-major-mode-is 'c++-mode)
  9157. (progn
  9158. (when (eq char-after-ip ?,)
  9159. (skip-chars-forward " \t")
  9160. (forward-char))
  9161. (looking-at c-opt-postfix-decl-spec-key)))
  9162. (and (or (eq char-before-ip ?:)
  9163. ;; watch out for scope operator
  9164. (save-excursion
  9165. (and (eq char-after-ip ?:)
  9166. (c-safe (forward-char 1) t)
  9167. (not (eq (char-after) ?:))
  9168. )))
  9169. (save-excursion
  9170. (c-beginning-of-statement-1 lim)
  9171. (when (looking-at c-opt-<>-sexp-key)
  9172. (goto-char (match-end 1))
  9173. (c-forward-syntactic-ws)
  9174. (c-forward-<>-arglist nil)
  9175. (c-forward-syntactic-ws))
  9176. (looking-at c-class-key)))
  9177. ;; for Java
  9178. (and (c-major-mode-is 'java-mode)
  9179. (let ((fence (save-excursion
  9180. (c-beginning-of-statement-1 lim)
  9181. (point)))
  9182. cont done)
  9183. (save-excursion
  9184. (while (not done)
  9185. (cond ((looking-at c-opt-postfix-decl-spec-key)
  9186. (setq injava-inher (cons cont (point))
  9187. done t))
  9188. ((or (not (c-safe (c-forward-sexp -1) t))
  9189. (<= (point) fence))
  9190. (setq done t))
  9191. )
  9192. (setq cont t)))
  9193. injava-inher)
  9194. (not (c-crosses-statement-barrier-p (cdr injava-inher)
  9195. (point)))
  9196. ))
  9197. (cond
  9198. ;; CASE 5C.1: non-hanging colon on an inher intro
  9199. ((eq char-after-ip ?:)
  9200. (c-beginning-of-statement-1 lim)
  9201. (c-add-syntax 'inher-intro (c-point 'boi))
  9202. ;; don't add inclass symbol since relative point already
  9203. ;; contains any class offset
  9204. )
  9205. ;; CASE 5C.2: hanging colon on an inher intro
  9206. ((eq char-before-ip ?:)
  9207. (c-beginning-of-statement-1 lim)
  9208. (c-add-syntax 'inher-intro (c-point 'boi))
  9209. (if containing-decl-open
  9210. (c-add-class-syntax 'inclass
  9211. containing-decl-open
  9212. containing-decl-start
  9213. containing-decl-kwd
  9214. paren-state)))
  9215. ;; CASE 5C.3: in a Java implements/extends
  9216. (injava-inher
  9217. (let ((where (cdr injava-inher))
  9218. (cont (car injava-inher)))
  9219. (goto-char where)
  9220. (cond ((looking-at "throws\\>[^_]")
  9221. (c-add-syntax 'func-decl-cont
  9222. (progn (c-beginning-of-statement-1 lim)
  9223. (c-point 'boi))))
  9224. (cont (c-add-syntax 'inher-cont where))
  9225. (t (c-add-syntax 'inher-intro
  9226. (progn (goto-char (cdr injava-inher))
  9227. (c-beginning-of-statement-1 lim)
  9228. (point))))
  9229. )))
  9230. ;; CASE 5C.4: a continued inheritance line
  9231. (t
  9232. (c-beginning-of-inheritance-list lim)
  9233. (c-add-syntax 'inher-cont (point))
  9234. ;; don't add inclass symbol since relative point already
  9235. ;; contains any class offset
  9236. )))
  9237. ;; CASE 5P: AWK pattern or function or continuation
  9238. ;; thereof.
  9239. ((c-major-mode-is 'awk-mode)
  9240. (setq placeholder (point))
  9241. (c-add-stmt-syntax
  9242. (if (and (eq (c-beginning-of-statement-1) 'same)
  9243. (/= (point) placeholder))
  9244. 'topmost-intro-cont
  9245. 'topmost-intro)
  9246. nil nil
  9247. containing-sexp paren-state))
  9248. ;; CASE 5D: this could be a top-level initialization, a
  9249. ;; member init list continuation, or a template argument
  9250. ;; list continuation.
  9251. ((save-excursion
  9252. ;; Note: We use the fact that lim is always after any
  9253. ;; preceding brace sexp.
  9254. (if c-recognize-<>-arglists
  9255. (while (and
  9256. (progn
  9257. (c-syntactic-skip-backward "^;,=<>" lim t)
  9258. (> (point) lim))
  9259. (or
  9260. (when c-overloadable-operators-regexp
  9261. (when (setq placeholder (c-after-special-operator-id lim))
  9262. (goto-char placeholder)
  9263. t))
  9264. (cond
  9265. ((eq (char-before) ?>)
  9266. (or (c-backward-<>-arglist nil lim)
  9267. (backward-char))
  9268. t)
  9269. ((eq (char-before) ?<)
  9270. (backward-char)
  9271. (if (save-excursion
  9272. (c-forward-<>-arglist nil))
  9273. (progn (forward-char)
  9274. nil)
  9275. t))
  9276. (t nil)))))
  9277. ;; NB: No c-after-special-operator-id stuff in this
  9278. ;; clause - we assume only C++ needs it.
  9279. (c-syntactic-skip-backward "^;,=" lim t))
  9280. (memq (char-before) '(?, ?= ?<)))
  9281. (cond
  9282. ;; CASE 5D.3: perhaps a template list continuation?
  9283. ((and (c-major-mode-is 'c++-mode)
  9284. (save-excursion
  9285. (save-restriction
  9286. (c-with-syntax-table c++-template-syntax-table
  9287. (goto-char indent-point)
  9288. (setq placeholder (c-up-list-backward))
  9289. (and placeholder
  9290. (eq (char-after placeholder) ?<))))))
  9291. (c-with-syntax-table c++-template-syntax-table
  9292. (goto-char placeholder)
  9293. (c-beginning-of-statement-1 lim t))
  9294. (if (save-excursion
  9295. (c-backward-syntactic-ws lim)
  9296. (eq (char-before) ?<))
  9297. ;; In a nested template arglist.
  9298. (progn
  9299. (goto-char placeholder)
  9300. (c-syntactic-skip-backward "^,;" lim t)
  9301. (c-forward-syntactic-ws))
  9302. (back-to-indentation))
  9303. ;; FIXME: Should use c-add-stmt-syntax, but it's not yet
  9304. ;; template aware.
  9305. (c-add-syntax 'template-args-cont (point) placeholder))
  9306. ;; CASE 5D.4: perhaps a multiple inheritance line?
  9307. ((and (c-major-mode-is 'c++-mode)
  9308. (save-excursion
  9309. (c-beginning-of-statement-1 lim)
  9310. (setq placeholder (point))
  9311. (if (looking-at "static\\>[^_]")
  9312. (c-forward-token-2 1 nil indent-point))
  9313. (and (looking-at c-class-key)
  9314. (zerop (c-forward-token-2 2 nil indent-point))
  9315. (if (eq (char-after) ?<)
  9316. (c-with-syntax-table c++-template-syntax-table
  9317. (zerop (c-forward-token-2 1 t indent-point)))
  9318. t)
  9319. (eq (char-after) ?:))))
  9320. (goto-char placeholder)
  9321. (c-add-syntax 'inher-cont (c-point 'boi)))
  9322. ;; CASE 5D.5: Continuation of the "expression part" of a
  9323. ;; top level construct. Or, perhaps, an unrecognized construct.
  9324. (t
  9325. (while (and (setq placeholder (point))
  9326. (eq (car (c-beginning-of-decl-1 containing-sexp)) ; Can't use `lim' here.
  9327. 'same)
  9328. (save-excursion
  9329. (c-backward-syntactic-ws)
  9330. (eq (char-before) ?}))
  9331. (< (point) placeholder)))
  9332. (c-add-stmt-syntax
  9333. (cond
  9334. ((eq (point) placeholder) 'statement) ; unrecognized construct
  9335. ;; A preceding comma at the top level means that a
  9336. ;; new variable declaration starts here. Use
  9337. ;; topmost-intro-cont for it, for consistency with
  9338. ;; the first variable declaration. C.f. case 5N.
  9339. ((eq char-before-ip ?,) 'topmost-intro-cont)
  9340. (t 'statement-cont))
  9341. nil nil containing-sexp paren-state))
  9342. ))
  9343. ;; CASE 5F: Close of a non-class declaration level block.
  9344. ((and (eq char-after-ip ?})
  9345. (c-keyword-member containing-decl-kwd
  9346. 'c-other-block-decl-kwds))
  9347. ;; This is inconsistent: Should use `containing-decl-open'
  9348. ;; here if it's at boi, like in case 5J.
  9349. (goto-char containing-decl-start)
  9350. (c-add-stmt-syntax
  9351. (if (string-equal (symbol-name containing-decl-kwd) "extern")
  9352. ;; Special case for compatibility with the
  9353. ;; extern-lang syntactic symbols.
  9354. 'extern-lang-close
  9355. (intern (concat (symbol-name containing-decl-kwd)
  9356. "-close")))
  9357. nil t
  9358. (c-most-enclosing-brace paren-state (point))
  9359. paren-state))
  9360. ;; CASE 5G: we are looking at the brace which closes the
  9361. ;; enclosing nested class decl
  9362. ((and containing-sexp
  9363. (eq char-after-ip ?})
  9364. (eq containing-decl-open containing-sexp))
  9365. (c-add-class-syntax 'class-close
  9366. containing-decl-open
  9367. containing-decl-start
  9368. containing-decl-kwd
  9369. paren-state))
  9370. ;; CASE 5H: we could be looking at subsequent knr-argdecls
  9371. ((and c-recognize-knr-p
  9372. (not containing-sexp) ; can't be knr inside braces.
  9373. (not (eq char-before-ip ?}))
  9374. (save-excursion
  9375. (setq placeholder (cdr (c-beginning-of-decl-1 lim)))
  9376. (and placeholder
  9377. ;; Do an extra check to avoid tripping up on
  9378. ;; statements that occur in invalid contexts
  9379. ;; (e.g. in macro bodies where we don't really
  9380. ;; know the context of what we're looking at).
  9381. (not (and c-opt-block-stmt-key
  9382. (looking-at c-opt-block-stmt-key)))))
  9383. (< placeholder indent-point))
  9384. (goto-char placeholder)
  9385. (c-add-syntax 'knr-argdecl (point)))
  9386. ;; CASE 5I: ObjC method definition.
  9387. ((and c-opt-method-key
  9388. (looking-at c-opt-method-key))
  9389. (c-beginning-of-statement-1 nil t)
  9390. (if (= (point) indent-point)
  9391. ;; Handle the case when it's the first (non-comment)
  9392. ;; thing in the buffer. Can't look for a 'same return
  9393. ;; value from cbos1 since ObjC directives currently
  9394. ;; aren't recognized fully, so that we get 'same
  9395. ;; instead of 'previous if it moved over a preceding
  9396. ;; directive.
  9397. (goto-char (point-min)))
  9398. (c-add-syntax 'objc-method-intro (c-point 'boi)))
  9399. ;; CASE 5N: At a variable declaration that follows a class
  9400. ;; definition or some other block declaration that doesn't
  9401. ;; end at the closing '}'. C.f. case 5D.5.
  9402. ((progn
  9403. (c-backward-syntactic-ws lim)
  9404. (and (eq (char-before) ?})
  9405. (save-excursion
  9406. (let ((start (point)))
  9407. (if (and c-state-cache
  9408. (consp (car c-state-cache))
  9409. (eq (cdar c-state-cache) (point)))
  9410. ;; Speed up the backward search a bit.
  9411. (goto-char (caar c-state-cache)))
  9412. (c-beginning-of-decl-1 containing-sexp) ; Can't use `lim' here.
  9413. (setq placeholder (point))
  9414. (if (= start (point))
  9415. ;; The '}' is unbalanced.
  9416. nil
  9417. (c-end-of-decl-1)
  9418. (>= (point) indent-point))))))
  9419. (goto-char placeholder)
  9420. (c-add-stmt-syntax 'topmost-intro-cont nil nil
  9421. containing-sexp paren-state))
  9422. ;; NOTE: The point is at the end of the previous token here.
  9423. ;; CASE 5J: we are at the topmost level, make
  9424. ;; sure we skip back past any access specifiers
  9425. ((and
  9426. ;; A macro continuation line is never at top level.
  9427. (not (and macro-start
  9428. (> indent-point macro-start)))
  9429. (save-excursion
  9430. (setq placeholder (point))
  9431. (or (memq char-before-ip '(?\; ?{ ?} nil))
  9432. (c-at-vsemi-p before-ws-ip)
  9433. (when (and (eq char-before-ip ?:)
  9434. (eq (c-beginning-of-statement-1 lim)
  9435. 'label))
  9436. (c-backward-syntactic-ws lim)
  9437. (setq placeholder (point)))
  9438. (and (c-major-mode-is 'objc-mode)
  9439. (catch 'not-in-directive
  9440. (c-beginning-of-statement-1 lim)
  9441. (setq placeholder (point))
  9442. (while (and (c-forward-objc-directive)
  9443. (< (point) indent-point))
  9444. (c-forward-syntactic-ws)
  9445. (if (>= (point) indent-point)
  9446. (throw 'not-in-directive t))
  9447. (setq placeholder (point)))
  9448. nil)))))
  9449. ;; For historic reasons we anchor at bol of the last
  9450. ;; line of the previous declaration. That's clearly
  9451. ;; highly bogus and useless, and it makes our lives hard
  9452. ;; to remain compatible. :P
  9453. (goto-char placeholder)
  9454. (c-add-syntax 'topmost-intro (c-point 'bol))
  9455. (if containing-decl-open
  9456. (if (c-keyword-member containing-decl-kwd
  9457. 'c-other-block-decl-kwds)
  9458. (progn
  9459. (goto-char (c-brace-anchor-point containing-decl-open))
  9460. (c-add-stmt-syntax
  9461. (if (string-equal (symbol-name containing-decl-kwd)
  9462. "extern")
  9463. ;; Special case for compatibility with the
  9464. ;; extern-lang syntactic symbols.
  9465. 'inextern-lang
  9466. (intern (concat "in"
  9467. (symbol-name containing-decl-kwd))))
  9468. nil t
  9469. (c-most-enclosing-brace paren-state (point))
  9470. paren-state))
  9471. (c-add-class-syntax 'inclass
  9472. containing-decl-open
  9473. containing-decl-start
  9474. containing-decl-kwd
  9475. paren-state)))
  9476. (when (and c-syntactic-indentation-in-macros
  9477. macro-start
  9478. (/= macro-start (c-point 'boi indent-point)))
  9479. (c-add-syntax 'cpp-define-intro)
  9480. (setq macro-start nil)))
  9481. ;; CASE 5K: we are at an ObjC method definition
  9482. ;; continuation line.
  9483. ((and c-opt-method-key
  9484. (save-excursion
  9485. (c-beginning-of-statement-1 lim)
  9486. (beginning-of-line)
  9487. (when (looking-at c-opt-method-key)
  9488. (setq placeholder (point)))))
  9489. (c-add-syntax 'objc-method-args-cont placeholder))
  9490. ;; CASE 5L: we are at the first argument of a template
  9491. ;; arglist that begins on the previous line.
  9492. ((and c-recognize-<>-arglists
  9493. (eq (char-before) ?<)
  9494. (not (and c-overloadable-operators-regexp
  9495. (c-after-special-operator-id lim))))
  9496. (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
  9497. (c-add-syntax 'template-args-cont (c-point 'boi)))
  9498. ;; CASE 5Q: we are at a statement within a macro.
  9499. (macro-start
  9500. (c-beginning-of-statement-1 containing-sexp)
  9501. (c-add-stmt-syntax 'statement nil t containing-sexp paren-state))
  9502. ;;CASE 5N: We are at a topmost continuation line and the only
  9503. ;;preceding items are annotations.
  9504. ((and (c-major-mode-is 'java-mode)
  9505. (setq placeholder (point))
  9506. (c-beginning-of-statement-1)
  9507. (progn
  9508. (while (and (c-forward-annotation))
  9509. (c-forward-syntactic-ws))
  9510. t)
  9511. (prog1
  9512. (>= (point) placeholder)
  9513. (goto-char placeholder)))
  9514. (c-add-syntax 'annotation-top-cont (c-point 'boi)))
  9515. ;; CASE 5M: we are at a topmost continuation line
  9516. (t
  9517. (c-beginning-of-statement-1 (c-safe-position (point) paren-state))
  9518. (when (c-major-mode-is 'objc-mode)
  9519. (setq placeholder (point))
  9520. (while (and (c-forward-objc-directive)
  9521. (< (point) indent-point))
  9522. (c-forward-syntactic-ws)
  9523. (setq placeholder (point)))
  9524. (goto-char placeholder))
  9525. (c-add-syntax 'topmost-intro-cont (c-point 'boi)))
  9526. ))
  9527. ;; (CASE 6 has been removed.)
  9528. ;; CASE 7: line is an expression, not a statement. Most
  9529. ;; likely we are either in a function prototype or a function
  9530. ;; call argument list
  9531. ((not (or (and c-special-brace-lists
  9532. (save-excursion
  9533. (goto-char containing-sexp)
  9534. (c-looking-at-special-brace-list)))
  9535. (eq (char-after containing-sexp) ?{)))
  9536. (cond
  9537. ;; CASE 7A: we are looking at the arglist closing paren.
  9538. ;; C.f. case 7F.
  9539. ((memq char-after-ip '(?\) ?\]))
  9540. (goto-char containing-sexp)
  9541. (setq placeholder (c-point 'boi))
  9542. (if (and (c-safe (backward-up-list 1) t)
  9543. (>= (point) placeholder))
  9544. (progn
  9545. (forward-char)
  9546. (skip-chars-forward " \t"))
  9547. (goto-char placeholder))
  9548. (c-add-stmt-syntax 'arglist-close (list containing-sexp) t
  9549. (c-most-enclosing-brace paren-state (point))
  9550. paren-state))
  9551. ;; CASE 7B: Looking at the opening brace of an
  9552. ;; in-expression block or brace list. C.f. cases 4, 16A
  9553. ;; and 17E.
  9554. ((and (eq char-after-ip ?{)
  9555. (progn
  9556. (setq placeholder (c-inside-bracelist-p (point)
  9557. paren-state))
  9558. (if placeholder
  9559. (setq tmpsymbol '(brace-list-open . inexpr-class))
  9560. (setq tmpsymbol '(block-open . inexpr-statement)
  9561. placeholder
  9562. (cdr-safe (c-looking-at-inexpr-block
  9563. (c-safe-position containing-sexp
  9564. paren-state)
  9565. containing-sexp)))
  9566. ;; placeholder is nil if it's a block directly in
  9567. ;; a function arglist. That makes us skip out of
  9568. ;; this case.
  9569. )))
  9570. (goto-char placeholder)
  9571. (back-to-indentation)
  9572. (c-add-stmt-syntax (car tmpsymbol) nil t
  9573. (c-most-enclosing-brace paren-state (point))
  9574. paren-state)
  9575. (if (/= (point) placeholder)
  9576. (c-add-syntax (cdr tmpsymbol))))
  9577. ;; CASE 7C: we are looking at the first argument in an empty
  9578. ;; argument list. Use arglist-close if we're actually
  9579. ;; looking at a close paren or bracket.
  9580. ((memq char-before-ip '(?\( ?\[))
  9581. (goto-char containing-sexp)
  9582. (setq placeholder (c-point 'boi))
  9583. (if (and (c-safe (backward-up-list 1) t)
  9584. (>= (point) placeholder))
  9585. (progn
  9586. (forward-char)
  9587. (skip-chars-forward " \t"))
  9588. (goto-char placeholder))
  9589. (c-add-stmt-syntax 'arglist-intro (list containing-sexp) t
  9590. (c-most-enclosing-brace paren-state (point))
  9591. paren-state))
  9592. ;; CASE 7D: we are inside a conditional test clause. treat
  9593. ;; these things as statements
  9594. ((progn
  9595. (goto-char containing-sexp)
  9596. (and (c-safe (c-forward-sexp -1) t)
  9597. (looking-at "\\<for\\>[^_]")))
  9598. (goto-char (1+ containing-sexp))
  9599. (c-forward-syntactic-ws indent-point)
  9600. (if (eq char-before-ip ?\;)
  9601. (c-add-syntax 'statement (point))
  9602. (c-add-syntax 'statement-cont (point))
  9603. ))
  9604. ;; CASE 7E: maybe a continued ObjC method call. This is the
  9605. ;; case when we are inside a [] bracketed exp, and what
  9606. ;; precede the opening bracket is not an identifier.
  9607. ((and c-opt-method-key
  9608. (eq (char-after containing-sexp) ?\[)
  9609. (progn
  9610. (goto-char (1- containing-sexp))
  9611. (c-backward-syntactic-ws (c-point 'bod))
  9612. (if (not (looking-at c-symbol-key))
  9613. (c-add-syntax 'objc-method-call-cont containing-sexp))
  9614. )))
  9615. ;; CASE 7F: we are looking at an arglist continuation line,
  9616. ;; but the preceding argument is on the same line as the
  9617. ;; opening paren. This case includes multi-line
  9618. ;; mathematical paren groupings, but we could be on a
  9619. ;; for-list continuation line. C.f. case 7A.
  9620. ((progn
  9621. (goto-char (1+ containing-sexp))
  9622. (< (save-excursion
  9623. (c-forward-syntactic-ws)
  9624. (point))
  9625. (c-point 'bonl)))
  9626. (goto-char containing-sexp) ; paren opening the arglist
  9627. (setq placeholder (c-point 'boi))
  9628. (if (and (c-safe (backward-up-list 1) t)
  9629. (>= (point) placeholder))
  9630. (progn
  9631. (forward-char)
  9632. (skip-chars-forward " \t"))
  9633. (goto-char placeholder))
  9634. (c-add-stmt-syntax 'arglist-cont-nonempty (list containing-sexp) t
  9635. (c-most-enclosing-brace c-state-cache (point))
  9636. paren-state))
  9637. ;; CASE 7G: we are looking at just a normal arglist
  9638. ;; continuation line
  9639. (t (c-forward-syntactic-ws indent-point)
  9640. (c-add-syntax 'arglist-cont (c-point 'boi)))
  9641. ))
  9642. ;; CASE 8: func-local multi-inheritance line
  9643. ((and (c-major-mode-is 'c++-mode)
  9644. (save-excursion
  9645. (goto-char indent-point)
  9646. (skip-chars-forward " \t")
  9647. (looking-at c-opt-postfix-decl-spec-key)))
  9648. (goto-char indent-point)
  9649. (skip-chars-forward " \t")
  9650. (cond
  9651. ;; CASE 8A: non-hanging colon on an inher intro
  9652. ((eq char-after-ip ?:)
  9653. (c-backward-syntactic-ws lim)
  9654. (c-add-syntax 'inher-intro (c-point 'boi)))
  9655. ;; CASE 8B: hanging colon on an inher intro
  9656. ((eq char-before-ip ?:)
  9657. (c-add-syntax 'inher-intro (c-point 'boi)))
  9658. ;; CASE 8C: a continued inheritance line
  9659. (t
  9660. (c-beginning-of-inheritance-list lim)
  9661. (c-add-syntax 'inher-cont (point))
  9662. )))
  9663. ;; CASE 9: we are inside a brace-list
  9664. ((and (not (c-major-mode-is 'awk-mode)) ; Maybe this isn't needed (ACM, 2002/3/29)
  9665. (setq special-brace-list
  9666. (or (and c-special-brace-lists ;;;; ALWAYS NIL FOR AWK!!
  9667. (save-excursion
  9668. (goto-char containing-sexp)
  9669. (c-looking-at-special-brace-list)))
  9670. (c-inside-bracelist-p containing-sexp paren-state))))
  9671. (cond
  9672. ;; CASE 9A: In the middle of a special brace list opener.
  9673. ((and (consp special-brace-list)
  9674. (save-excursion
  9675. (goto-char containing-sexp)
  9676. (eq (char-after) ?\())
  9677. (eq char-after-ip (car (cdr special-brace-list))))
  9678. (goto-char (car (car special-brace-list)))
  9679. (skip-chars-backward " \t")
  9680. (if (and (bolp)
  9681. (assoc 'statement-cont
  9682. (setq placeholder (c-guess-basic-syntax))))
  9683. (setq c-syntactic-context placeholder)
  9684. (c-beginning-of-statement-1
  9685. (c-safe-position (1- containing-sexp) paren-state))
  9686. (c-forward-token-2 0)
  9687. (while (looking-at c-specifier-key)
  9688. (goto-char (match-end 1))
  9689. (c-forward-syntactic-ws))
  9690. (c-add-syntax 'brace-list-open (c-point 'boi))))
  9691. ;; CASE 9B: brace-list-close brace
  9692. ((if (consp special-brace-list)
  9693. ;; Check special brace list closer.
  9694. (progn
  9695. (goto-char (car (car special-brace-list)))
  9696. (save-excursion
  9697. (goto-char indent-point)
  9698. (back-to-indentation)
  9699. (or
  9700. ;; We were between the special close char and the `)'.
  9701. (and (eq (char-after) ?\))
  9702. (eq (1+ (point)) (cdr (car special-brace-list))))
  9703. ;; We were before the special close char.
  9704. (and (eq (char-after) (cdr (cdr special-brace-list)))
  9705. (zerop (c-forward-token-2))
  9706. (eq (1+ (point)) (cdr (car special-brace-list)))))))
  9707. ;; Normal brace list check.
  9708. (and (eq char-after-ip ?})
  9709. (c-safe (goto-char (c-up-list-backward (point))) t)
  9710. (= (point) containing-sexp)))
  9711. (if (eq (point) (c-point 'boi))
  9712. (c-add-syntax 'brace-list-close (point))
  9713. (setq lim (c-most-enclosing-brace c-state-cache (point)))
  9714. (c-beginning-of-statement-1 lim nil nil t)
  9715. (c-add-stmt-syntax 'brace-list-close nil t lim paren-state)))
  9716. (t
  9717. ;; Prepare for the rest of the cases below by going to the
  9718. ;; token following the opening brace
  9719. (if (consp special-brace-list)
  9720. (progn
  9721. (goto-char (car (car special-brace-list)))
  9722. (c-forward-token-2 1 nil indent-point))
  9723. (goto-char containing-sexp))
  9724. (forward-char)
  9725. (let ((start (point)))
  9726. (c-forward-syntactic-ws indent-point)
  9727. (goto-char (max start (c-point 'bol))))
  9728. (c-skip-ws-forward indent-point)
  9729. (cond
  9730. ;; CASE 9C: we're looking at the first line in a brace-list
  9731. ((= (point) indent-point)
  9732. (if (consp special-brace-list)
  9733. (goto-char (car (car special-brace-list)))
  9734. (goto-char containing-sexp))
  9735. (if (eq (point) (c-point 'boi))
  9736. (c-add-syntax 'brace-list-intro (point))
  9737. (setq lim (c-most-enclosing-brace c-state-cache (point)))
  9738. (c-beginning-of-statement-1 lim)
  9739. (c-add-stmt-syntax 'brace-list-intro nil t lim paren-state)))
  9740. ;; CASE 9D: this is just a later brace-list-entry or
  9741. ;; brace-entry-open
  9742. (t (if (or (eq char-after-ip ?{)
  9743. (and c-special-brace-lists
  9744. (save-excursion
  9745. (goto-char indent-point)
  9746. (c-forward-syntactic-ws (c-point 'eol))
  9747. (c-looking-at-special-brace-list (point)))))
  9748. (c-add-syntax 'brace-entry-open (point))
  9749. (c-add-syntax 'brace-list-entry (point))
  9750. ))
  9751. ))))
  9752. ;; CASE 10: A continued statement or top level construct.
  9753. ((and (not (memq char-before-ip '(?\; ?:)))
  9754. (not (c-at-vsemi-p before-ws-ip))
  9755. (or (not (eq char-before-ip ?}))
  9756. (c-looking-at-inexpr-block-backward c-state-cache))
  9757. (> (point)
  9758. (save-excursion
  9759. (c-beginning-of-statement-1 containing-sexp)
  9760. (setq placeholder (point))))
  9761. (/= placeholder containing-sexp))
  9762. ;; This is shared with case 18.
  9763. (c-guess-continued-construct indent-point
  9764. char-after-ip
  9765. placeholder
  9766. containing-sexp
  9767. paren-state))
  9768. ;; CASE 16: block close brace, possibly closing the defun or
  9769. ;; the class
  9770. ((eq char-after-ip ?})
  9771. ;; From here on we have the next containing sexp in lim.
  9772. (setq lim (c-most-enclosing-brace paren-state))
  9773. (goto-char containing-sexp)
  9774. (cond
  9775. ;; CASE 16E: Closing a statement block? This catches
  9776. ;; cases where it's preceded by a statement keyword,
  9777. ;; which works even when used in an "invalid" context,
  9778. ;; e.g. a macro argument.
  9779. ((c-after-conditional)
  9780. (c-backward-to-block-anchor lim)
  9781. (c-add-stmt-syntax 'block-close nil t lim paren-state))
  9782. ;; CASE 16A: closing a lambda defun or an in-expression
  9783. ;; block? C.f. cases 4, 7B and 17E.
  9784. ((setq placeholder (c-looking-at-inexpr-block
  9785. (c-safe-position containing-sexp paren-state)
  9786. nil))
  9787. (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
  9788. 'inline-close
  9789. 'block-close))
  9790. (goto-char containing-sexp)
  9791. (back-to-indentation)
  9792. (if (= containing-sexp (point))
  9793. (c-add-syntax tmpsymbol (point))
  9794. (goto-char (cdr placeholder))
  9795. (back-to-indentation)
  9796. (c-add-stmt-syntax tmpsymbol nil t
  9797. (c-most-enclosing-brace paren-state (point))
  9798. paren-state)
  9799. (if (/= (point) (cdr placeholder))
  9800. (c-add-syntax (car placeholder)))))
  9801. ;; CASE 16B: does this close an inline or a function in
  9802. ;; a non-class declaration level block?
  9803. ((save-excursion
  9804. (and lim
  9805. (progn
  9806. (goto-char lim)
  9807. (c-looking-at-decl-block
  9808. (c-most-enclosing-brace paren-state lim)
  9809. nil))
  9810. (setq placeholder (point))))
  9811. (c-backward-to-decl-anchor lim)
  9812. (back-to-indentation)
  9813. (if (save-excursion
  9814. (goto-char placeholder)
  9815. (looking-at c-other-decl-block-key))
  9816. (c-add-syntax 'defun-close (point))
  9817. (c-add-syntax 'inline-close (point))))
  9818. ;; CASE 16F: Can be a defun-close of a function declared
  9819. ;; in a statement block, e.g. in Pike or when using gcc
  9820. ;; extensions, but watch out for macros followed by
  9821. ;; blocks. Let it through to be handled below.
  9822. ;; C.f. cases B.3 and 17G.
  9823. ((save-excursion
  9824. (and (not (c-at-statement-start-p))
  9825. (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
  9826. (setq placeholder (point))
  9827. (let ((c-recognize-typeless-decls nil))
  9828. ;; Turn off recognition of constructs that
  9829. ;; lacks a type in this case, since that's more
  9830. ;; likely to be a macro followed by a block.
  9831. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  9832. (back-to-indentation)
  9833. (if (/= (point) containing-sexp)
  9834. (goto-char placeholder))
  9835. (c-add-stmt-syntax 'defun-close nil t lim paren-state))
  9836. ;; CASE 16C: If there is an enclosing brace then this is
  9837. ;; a block close since defun closes inside declaration
  9838. ;; level blocks have been handled above.
  9839. (lim
  9840. ;; If the block is preceded by a case/switch label on
  9841. ;; the same line, we anchor at the first preceding label
  9842. ;; at boi. The default handling in c-add-stmt-syntax
  9843. ;; really fixes it better, but we do like this to keep
  9844. ;; the indentation compatible with version 5.28 and
  9845. ;; earlier. C.f. case 17H.
  9846. (while (and (/= (setq placeholder (point)) (c-point 'boi))
  9847. (eq (c-beginning-of-statement-1 lim) 'label)))
  9848. (goto-char placeholder)
  9849. (if (looking-at c-label-kwds-regexp)
  9850. (c-add-syntax 'block-close (point))
  9851. (goto-char containing-sexp)
  9852. ;; c-backward-to-block-anchor not necessary here; those
  9853. ;; situations are handled in case 16E above.
  9854. (c-add-stmt-syntax 'block-close nil t lim paren-state)))
  9855. ;; CASE 16D: Only top level defun close left.
  9856. (t
  9857. (goto-char containing-sexp)
  9858. (c-backward-to-decl-anchor lim)
  9859. (c-add-stmt-syntax 'defun-close nil nil
  9860. (c-most-enclosing-brace paren-state)
  9861. paren-state))
  9862. ))
  9863. ;; CASE 19: line is an expression, not a statement, and is directly
  9864. ;; contained by a template delimiter. Most likely, we are in a
  9865. ;; template arglist within a statement. This case is based on CASE
  9866. ;; 7. At some point in the future, we may wish to create more
  9867. ;; syntactic symbols such as `template-intro',
  9868. ;; `template-cont-nonempty', etc., and distinguish between them as we
  9869. ;; do for `arglist-intro' etc. (2009-12-07).
  9870. ((and c-recognize-<>-arglists
  9871. (setq containing-< (c-up-list-backward indent-point containing-sexp))
  9872. (eq (char-after containing-<) ?\<))
  9873. (setq placeholder (c-point 'boi containing-<))
  9874. (goto-char containing-sexp) ; Most nested Lbrace/Lparen (but not
  9875. ; '<') before indent-point.
  9876. (if (>= (point) placeholder)
  9877. (progn
  9878. (forward-char)
  9879. (skip-chars-forward " \t"))
  9880. (goto-char placeholder))
  9881. (c-add-stmt-syntax 'template-args-cont (list containing-<) t
  9882. (c-most-enclosing-brace c-state-cache (point))
  9883. paren-state))
  9884. ;; CASE 17: Statement or defun catchall.
  9885. (t
  9886. (goto-char indent-point)
  9887. ;; Back up statements until we find one that starts at boi.
  9888. (while (let* ((prev-point (point))
  9889. (last-step-type (c-beginning-of-statement-1
  9890. containing-sexp)))
  9891. (if (= (point) prev-point)
  9892. (progn
  9893. (setq step-type (or step-type last-step-type))
  9894. nil)
  9895. (setq step-type last-step-type)
  9896. (/= (point) (c-point 'boi)))))
  9897. (cond
  9898. ;; CASE 17B: continued statement
  9899. ((and (eq step-type 'same)
  9900. (/= (point) indent-point))
  9901. (c-add-stmt-syntax 'statement-cont nil nil
  9902. containing-sexp paren-state))
  9903. ;; CASE 17A: After a case/default label?
  9904. ((progn
  9905. (while (and (eq step-type 'label)
  9906. (not (looking-at c-label-kwds-regexp)))
  9907. (setq step-type
  9908. (c-beginning-of-statement-1 containing-sexp)))
  9909. (eq step-type 'label))
  9910. (c-add-stmt-syntax (if (eq char-after-ip ?{)
  9911. 'statement-case-open
  9912. 'statement-case-intro)
  9913. nil t containing-sexp paren-state))
  9914. ;; CASE 17D: any old statement
  9915. ((progn
  9916. (while (eq step-type 'label)
  9917. (setq step-type
  9918. (c-beginning-of-statement-1 containing-sexp)))
  9919. (eq step-type 'previous))
  9920. (c-add-stmt-syntax 'statement nil t
  9921. containing-sexp paren-state)
  9922. (if (eq char-after-ip ?{)
  9923. (c-add-syntax 'block-open)))
  9924. ;; CASE 17I: Inside a substatement block.
  9925. ((progn
  9926. ;; The following tests are all based on containing-sexp.
  9927. (goto-char containing-sexp)
  9928. ;; From here on we have the next containing sexp in lim.
  9929. (setq lim (c-most-enclosing-brace paren-state containing-sexp))
  9930. (c-after-conditional))
  9931. (c-backward-to-block-anchor lim)
  9932. (c-add-stmt-syntax 'statement-block-intro nil t
  9933. lim paren-state)
  9934. (if (eq char-after-ip ?{)
  9935. (c-add-syntax 'block-open)))
  9936. ;; CASE 17E: first statement in an in-expression block.
  9937. ;; C.f. cases 4, 7B and 16A.
  9938. ((setq placeholder (c-looking-at-inexpr-block
  9939. (c-safe-position containing-sexp paren-state)
  9940. nil))
  9941. (setq tmpsymbol (if (eq (car placeholder) 'inlambda)
  9942. 'defun-block-intro
  9943. 'statement-block-intro))
  9944. (back-to-indentation)
  9945. (if (= containing-sexp (point))
  9946. (c-add-syntax tmpsymbol (point))
  9947. (goto-char (cdr placeholder))
  9948. (back-to-indentation)
  9949. (c-add-stmt-syntax tmpsymbol nil t
  9950. (c-most-enclosing-brace c-state-cache (point))
  9951. paren-state)
  9952. (if (/= (point) (cdr placeholder))
  9953. (c-add-syntax (car placeholder))))
  9954. (if (eq char-after-ip ?{)
  9955. (c-add-syntax 'block-open)))
  9956. ;; CASE 17F: first statement in an inline, or first
  9957. ;; statement in a top-level defun. we can tell this is it
  9958. ;; if there are no enclosing braces that haven't been
  9959. ;; narrowed out by a class (i.e. don't use bod here).
  9960. ((save-excursion
  9961. (or (not (setq placeholder (c-most-enclosing-brace
  9962. paren-state)))
  9963. (and (progn
  9964. (goto-char placeholder)
  9965. (eq (char-after) ?{))
  9966. (c-looking-at-decl-block (c-most-enclosing-brace
  9967. paren-state (point))
  9968. nil))))
  9969. (c-backward-to-decl-anchor lim)
  9970. (back-to-indentation)
  9971. (c-add-syntax 'defun-block-intro (point)))
  9972. ;; CASE 17G: First statement in a function declared inside
  9973. ;; a normal block. This can occur in Pike and with
  9974. ;; e.g. the gcc extensions, but watch out for macros
  9975. ;; followed by blocks. C.f. cases B.3 and 16F.
  9976. ((save-excursion
  9977. (and (not (c-at-statement-start-p))
  9978. (eq (c-beginning-of-statement-1 lim nil nil t) 'same)
  9979. (setq placeholder (point))
  9980. (let ((c-recognize-typeless-decls nil))
  9981. ;; Turn off recognition of constructs that lacks
  9982. ;; a type in this case, since that's more likely
  9983. ;; to be a macro followed by a block.
  9984. (c-forward-decl-or-cast-1 (c-point 'bosws) nil nil))))
  9985. (back-to-indentation)
  9986. (if (/= (point) containing-sexp)
  9987. (goto-char placeholder))
  9988. (c-add-stmt-syntax 'defun-block-intro nil t
  9989. lim paren-state))
  9990. ;; CASE 17H: First statement in a block.
  9991. (t
  9992. ;; If the block is preceded by a case/switch label on the
  9993. ;; same line, we anchor at the first preceding label at
  9994. ;; boi. The default handling in c-add-stmt-syntax is
  9995. ;; really fixes it better, but we do like this to keep the
  9996. ;; indentation compatible with version 5.28 and earlier.
  9997. ;; C.f. case 16C.
  9998. (while (and (/= (setq placeholder (point)) (c-point 'boi))
  9999. (eq (c-beginning-of-statement-1 lim) 'label)))
  10000. (goto-char placeholder)
  10001. (if (looking-at c-label-kwds-regexp)
  10002. (c-add-syntax 'statement-block-intro (point))
  10003. (goto-char containing-sexp)
  10004. ;; c-backward-to-block-anchor not necessary here; those
  10005. ;; situations are handled in case 17I above.
  10006. (c-add-stmt-syntax 'statement-block-intro nil t
  10007. lim paren-state))
  10008. (if (eq char-after-ip ?{)
  10009. (c-add-syntax 'block-open)))
  10010. ))
  10011. )
  10012. ;; now we need to look at any modifiers
  10013. (goto-char indent-point)
  10014. (skip-chars-forward " \t")
  10015. ;; are we looking at a comment only line?
  10016. (when (and (looking-at c-comment-start-regexp)
  10017. (/= (c-forward-token-2 0 nil (c-point 'eol)) 0))
  10018. (c-append-syntax 'comment-intro))
  10019. ;; we might want to give additional offset to friends (in C++).
  10020. (when (and c-opt-friend-key
  10021. (looking-at c-opt-friend-key))
  10022. (c-append-syntax 'friend))
  10023. ;; Set syntactic-relpos.
  10024. (let ((p c-syntactic-context))
  10025. (while (and p
  10026. (if (integerp (c-langelem-pos (car p)))
  10027. (progn
  10028. (setq syntactic-relpos (c-langelem-pos (car p)))
  10029. nil)
  10030. t))
  10031. (setq p (cdr p))))
  10032. ;; Start of or a continuation of a preprocessor directive?
  10033. (if (and macro-start
  10034. (eq macro-start (c-point 'boi))
  10035. (not (and (c-major-mode-is 'pike-mode)
  10036. (eq (char-after (1+ macro-start)) ?\"))))
  10037. (c-append-syntax 'cpp-macro)
  10038. (when (and c-syntactic-indentation-in-macros macro-start)
  10039. (if in-macro-expr
  10040. (when (or
  10041. (< syntactic-relpos macro-start)
  10042. (not (or
  10043. (assq 'arglist-intro c-syntactic-context)
  10044. (assq 'arglist-cont c-syntactic-context)
  10045. (assq 'arglist-cont-nonempty c-syntactic-context)
  10046. (assq 'arglist-close c-syntactic-context))))
  10047. ;; If inside a cpp expression, i.e. anywhere in a
  10048. ;; cpp directive except a #define body, we only let
  10049. ;; through the syntactic analysis that is internal
  10050. ;; in the expression. That means the arglist
  10051. ;; elements, if they are anchored inside the cpp
  10052. ;; expression.
  10053. (setq c-syntactic-context nil)
  10054. (c-add-syntax 'cpp-macro-cont macro-start))
  10055. (when (and (eq macro-start syntactic-relpos)
  10056. (not (assq 'cpp-define-intro c-syntactic-context))
  10057. (save-excursion
  10058. (goto-char macro-start)
  10059. (or (not (c-forward-to-cpp-define-body))
  10060. (<= (point) (c-point 'boi indent-point)))))
  10061. ;; Inside a #define body and the syntactic analysis is
  10062. ;; anchored on the start of the #define. In this case
  10063. ;; we add cpp-define-intro to get the extra
  10064. ;; indentation of the #define body.
  10065. (c-add-syntax 'cpp-define-intro)))))
  10066. ;; return the syntax
  10067. c-syntactic-context)))
  10068. ;; Indentation calculation.
  10069. (defun c-evaluate-offset (offset langelem symbol)
  10070. ;; offset can be a number, a function, a variable, a list, or one of
  10071. ;; the symbols + or -
  10072. ;;
  10073. ;; This function might do hidden buffer changes.
  10074. (let ((res
  10075. (cond
  10076. ((numberp offset) offset)
  10077. ((vectorp offset) offset)
  10078. ((null offset) nil)
  10079. ((eq offset '+) c-basic-offset)
  10080. ((eq offset '-) (- c-basic-offset))
  10081. ((eq offset '++) (* 2 c-basic-offset))
  10082. ((eq offset '--) (* 2 (- c-basic-offset)))
  10083. ((eq offset '*) (/ c-basic-offset 2))
  10084. ((eq offset '/) (/ (- c-basic-offset) 2))
  10085. ((functionp offset)
  10086. (c-evaluate-offset
  10087. (funcall offset
  10088. (cons (c-langelem-sym langelem)
  10089. (c-langelem-pos langelem)))
  10090. langelem symbol))
  10091. ((listp offset)
  10092. (cond
  10093. ((eq (car offset) 'quote)
  10094. (c-benign-error "The offset %S for %s was mistakenly quoted"
  10095. offset symbol)
  10096. nil)
  10097. ((memq (car offset) '(min max))
  10098. (let (res val (method (car offset)))
  10099. (setq offset (cdr offset))
  10100. (while offset
  10101. (setq val (c-evaluate-offset (car offset) langelem symbol))
  10102. (cond
  10103. ((not val))
  10104. ((not res)
  10105. (setq res val))
  10106. ((integerp val)
  10107. (if (vectorp res)
  10108. (c-benign-error "\
  10109. Error evaluating offset %S for %s: \
  10110. Cannot combine absolute offset %S with relative %S in `%s' method"
  10111. (car offset) symbol res val method)
  10112. (setq res (funcall method res val))))
  10113. (t
  10114. (if (integerp res)
  10115. (c-benign-error "\
  10116. Error evaluating offset %S for %s: \
  10117. Cannot combine relative offset %S with absolute %S in `%s' method"
  10118. (car offset) symbol res val method)
  10119. (setq res (vector (funcall method (aref res 0)
  10120. (aref val 0)))))))
  10121. (setq offset (cdr offset)))
  10122. res))
  10123. ((eq (car offset) 'add)
  10124. (let (res val)
  10125. (setq offset (cdr offset))
  10126. (while offset
  10127. (setq val (c-evaluate-offset (car offset) langelem symbol))
  10128. (cond
  10129. ((not val))
  10130. ((not res)
  10131. (setq res val))
  10132. ((integerp val)
  10133. (if (vectorp res)
  10134. (setq res (vector (+ (aref res 0) val)))
  10135. (setq res (+ res val))))
  10136. (t
  10137. (if (vectorp res)
  10138. (c-benign-error "\
  10139. Error evaluating offset %S for %s: \
  10140. Cannot combine absolute offsets %S and %S in `add' method"
  10141. (car offset) symbol res val)
  10142. (setq res val)))) ; Override.
  10143. (setq offset (cdr offset)))
  10144. res))
  10145. (t
  10146. (let (res)
  10147. (when (eq (car offset) 'first)
  10148. (setq offset (cdr offset)))
  10149. (while (and (not res) offset)
  10150. (setq res (c-evaluate-offset (car offset) langelem symbol)
  10151. offset (cdr offset)))
  10152. res))))
  10153. ((and (symbolp offset) (boundp offset))
  10154. (symbol-value offset))
  10155. (t
  10156. (c-benign-error "Unknown offset format %S for %s" offset symbol)
  10157. nil))))
  10158. (if (or (null res) (integerp res)
  10159. (and (vectorp res) (= (length res) 1) (integerp (aref res 0))))
  10160. res
  10161. (c-benign-error "Error evaluating offset %S for %s: Got invalid value %S"
  10162. offset symbol res)
  10163. nil)))
  10164. (defun c-calc-offset (langelem)
  10165. ;; Get offset from LANGELEM which is a list beginning with the
  10166. ;; syntactic symbol and followed by any analysis data it provides.
  10167. ;; That data may be zero or more elements, but if at least one is
  10168. ;; given then the first is the anchor position (or nil). The symbol
  10169. ;; is matched against `c-offsets-alist' and the offset calculated
  10170. ;; from that is returned.
  10171. ;;
  10172. ;; This function might do hidden buffer changes.
  10173. (let* ((symbol (c-langelem-sym langelem))
  10174. (match (assq symbol c-offsets-alist))
  10175. (offset (cdr-safe match)))
  10176. (if match
  10177. (setq offset (c-evaluate-offset offset langelem symbol))
  10178. (if c-strict-syntax-p
  10179. (c-benign-error "No offset found for syntactic symbol %s" symbol))
  10180. (setq offset 0))
  10181. (if (vectorp offset)
  10182. offset
  10183. (or (and (numberp offset) offset)
  10184. (and (symbolp offset) (symbol-value offset))
  10185. 0))
  10186. ))
  10187. (defun c-get-offset (langelem)
  10188. ;; This is a compatibility wrapper for `c-calc-offset' in case
  10189. ;; someone is calling it directly. It takes an old style syntactic
  10190. ;; element on the form (SYMBOL . ANCHOR-POS) and converts it to the
  10191. ;; new list form.
  10192. ;;
  10193. ;; This function might do hidden buffer changes.
  10194. (if (c-langelem-pos langelem)
  10195. (c-calc-offset (list (c-langelem-sym langelem)
  10196. (c-langelem-pos langelem)))
  10197. (c-calc-offset langelem)))
  10198. (defun c-get-syntactic-indentation (langelems)
  10199. ;; Calculate the syntactic indentation from a syntactic description
  10200. ;; as returned by `c-guess-syntax'.
  10201. ;;
  10202. ;; Note that topmost-intro always has an anchor position at bol, for
  10203. ;; historical reasons. It's often used together with other symbols
  10204. ;; that has more sane positions. Since we always use the first
  10205. ;; found anchor position, we rely on that these other symbols always
  10206. ;; precede topmost-intro in the LANGELEMS list.
  10207. ;;
  10208. ;; This function might do hidden buffer changes.
  10209. (let ((indent 0) anchor)
  10210. (while langelems
  10211. (let* ((c-syntactic-element (car langelems))
  10212. (res (c-calc-offset c-syntactic-element)))
  10213. (if (vectorp res)
  10214. ;; Got an absolute column that overrides any indentation
  10215. ;; we've collected so far, but not the relative
  10216. ;; indentation we might get for the nested structures
  10217. ;; further down the langelems list.
  10218. (setq indent (elt res 0)
  10219. anchor (point-min)) ; A position at column 0.
  10220. ;; Got a relative change of the current calculated
  10221. ;; indentation.
  10222. (setq indent (+ indent res))
  10223. ;; Use the anchor position from the first syntactic
  10224. ;; element with one.
  10225. (unless anchor
  10226. (setq anchor (c-langelem-pos (car langelems)))))
  10227. (setq langelems (cdr langelems))))
  10228. (if anchor
  10229. (+ indent (save-excursion
  10230. (goto-char anchor)
  10231. (current-column)))
  10232. indent)))
  10233. (cc-provide 'cc-engine)
  10234. ;; Local Variables:
  10235. ;; indent-tabs-mode: t
  10236. ;; tab-width: 8
  10237. ;; End:
  10238. ;;; cc-engine.el ends here