ada-mode.el 183 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508
  1. ;;; ada-mode.el --- major-mode for editing Ada sources
  2. ;; Copyright (C) 1994-1995, 1997-2012 Free Software Foundation, Inc.
  3. ;; Author: Rolf Ebert <ebert@inf.enst.fr>
  4. ;; Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
  5. ;; Emmanuel Briot <briot@gnat.com>
  6. ;; Maintainer: Stephen Leake <stephen_leake@member.fsf.org>
  7. ;; Keywords: languages ada
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; This mode is a major mode for editing Ada code. This is a major
  21. ;; rewrite of the file packaged with Emacs-20. The Ada mode is
  22. ;; composed of four Lisp files: ada-mode.el, ada-xref.el, ada-prj.el
  23. ;; and ada-stmt.el. Only this file (ada-mode.el) is completely
  24. ;; independent from the GNU Ada compiler GNAT, distributed by Ada
  25. ;; Core Technologies. All the other files rely heavily on features
  26. ;; provided only by GNAT.
  27. ;;
  28. ;; Note: this mode will not work with Emacs 19. If you are on a VMS
  29. ;; system, where the latest version of Emacs is 19.28, you will need
  30. ;; another file, called ada-vms.el, that provides some required
  31. ;; functions.
  32. ;;; Usage:
  33. ;; Emacs should enter Ada mode automatically when you load an Ada file.
  34. ;; By default, the valid extensions for Ada files are .ads, .adb or .ada
  35. ;; If the ada-mode does not start automatically, then simply type the
  36. ;; following command :
  37. ;; M-x ada-mode
  38. ;;
  39. ;; By default, ada-mode is configured to take full advantage of the GNAT
  40. ;; compiler (the menus will include the cross-referencing features,...).
  41. ;; If you are using another compiler, you might want to set the following
  42. ;; variable in your .emacs (Note: do not set this in the ada-mode-hook, it
  43. ;; won't work) :
  44. ;; (setq ada-which-compiler 'generic)
  45. ;;
  46. ;; This mode requires find-file.el to be present on your system.
  47. ;;; History:
  48. ;; The first Ada mode for GNU Emacs was written by V. Broman in
  49. ;; 1985. He based his work on the already existing Modula-2 mode.
  50. ;; This was distributed as ada.el in versions of Emacs prior to 19.29.
  51. ;;
  52. ;; Lynn Slater wrote an extensive Ada mode in 1989. It consisted of
  53. ;; several files with support for dired commands and other nice
  54. ;; things. It is currently available from the PAL
  55. ;; (wuarchive.wustl.edu:/languages/ada) as ada-mode-1.06a.tar.Z.
  56. ;;
  57. ;; The probably very first Ada mode (called electric-ada.el) was
  58. ;; written by Steven D. Litvintchouk and Steven M. Rosen for the
  59. ;; Gosling Emacs. L. Slater based his development on ada.el and
  60. ;; electric-ada.el.
  61. ;;
  62. ;; A complete rewrite by M. Heritsch and R. Ebert has been done.
  63. ;; Some ideas from the Ada mode mailing list have been
  64. ;; added. Some of the functionality of L. Slater's mode has not
  65. ;; (yet) been recoded in this new mode. Perhaps you prefer sticking
  66. ;; to his version.
  67. ;;
  68. ;; A complete rewrite for Emacs-20 / GNAT-3.11 has been done by Ada Core
  69. ;; Technologies.
  70. ;;; Credits:
  71. ;; Many thanks to John McCabe <john@assen.demon.co.uk> for sending so
  72. ;; many patches included in this package.
  73. ;; Christian Egli <Christian.Egli@hcsd.hac.com>:
  74. ;; ada-imenu-generic-expression
  75. ;; Many thanks also to the following persons that have contributed
  76. ;; to the ada-mode
  77. ;; Philippe Waroquiers (PW) <philippe@cfmu.eurocontrol.be> in particular,
  78. ;; woodruff@stc.llnl.gov (John Woodruff)
  79. ;; jj@ddci.dk (Jesper Joergensen)
  80. ;; gse@ocsystems.com (Scott Evans)
  81. ;; comar@gnat.com (Cyrille Comar)
  82. ;; stephen.leake@gsfc.nasa.gov (Stephen Leake)
  83. ;; robin-reply@reagans.org
  84. ;; and others for their valuable hints.
  85. ;;; Code:
  86. ;; Note: Every function in this package is compiler-independent.
  87. ;; The names start with ada-
  88. ;; The variables that the user can edit can all be modified through
  89. ;; the customize mode. They are sorted in alphabetical order in this
  90. ;; file.
  91. ;; Supported packages.
  92. ;; This package supports a number of other Emacs modes. These other modes
  93. ;; should be loaded before the ada-mode, which will then setup some variables
  94. ;; to improve the support for Ada code.
  95. ;; Here is the list of these modes:
  96. ;; `which-function-mode': Display in the modeline the name of the subprogram
  97. ;; the cursor is in.
  98. ;; `outline-mode': Provides the capability to collapse or expand the code
  99. ;; for specific language constructs, for instance if you want to hide the
  100. ;; code corresponding to a subprogram
  101. ;; `align': This mode is now provided with Emacs 21, but can also be
  102. ;; installed manually for older versions of Emacs. It provides the
  103. ;; capability to automatically realign the selected region (for instance
  104. ;; all ':=', ':' and '--' will be aligned on top of each other.
  105. ;; `imenu': Provides a menu with the list of entities defined in the current
  106. ;; buffer, and an easy way to jump to any of them
  107. ;; `speedbar': Provides a separate file browser, and the capability for each
  108. ;; file to see the list of entities defined in it and to jump to them
  109. ;; easily
  110. ;; `abbrev-mode': Provides the capability to define abbreviations, which
  111. ;; are automatically expanded when you type them. See the Emacs manual.
  112. (require 'find-file nil t)
  113. (require 'align nil t)
  114. (require 'which-func nil t)
  115. (require 'compile nil t)
  116. (defvar ispell-check-comments)
  117. (defvar skeleton-further-elements)
  118. (defun ada-mode-version ()
  119. "Return Ada mode version."
  120. (interactive)
  121. (let ((version-string "4.00"))
  122. (if (called-interactively-p 'interactive)
  123. (message version-string)
  124. version-string)))
  125. (defvar ada-mode-hook nil
  126. "*List of functions to call when Ada mode is invoked.
  127. This hook is automatically executed after the `ada-mode' is
  128. fully loaded.
  129. This is a good place to add Ada environment specific bindings.")
  130. (defgroup ada nil
  131. "Major mode for editing and compiling Ada source in Emacs."
  132. :link '(custom-group-link :tag "Font Lock Faces group" font-lock-faces)
  133. :group 'languages)
  134. (defcustom ada-auto-case t
  135. "*Non-nil means automatically change case of preceding word while typing.
  136. Casing is done according to `ada-case-keyword', `ada-case-identifier'
  137. and `ada-case-attribute'."
  138. :type 'boolean :group 'ada)
  139. (defcustom ada-broken-decl-indent 0
  140. "*Number of columns to indent a broken declaration.
  141. An example is :
  142. declare
  143. A,
  144. >>>>>B : Integer;"
  145. :type 'integer :group 'ada)
  146. (defcustom ada-broken-indent 2
  147. "*Number of columns to indent the continuation of a broken line.
  148. An example is :
  149. My_Var : My_Type := (Field1 =>
  150. >>>>>>>>>Value);"
  151. :type 'integer :group 'ada)
  152. (defcustom ada-continuation-indent ada-broken-indent
  153. "*Number of columns to indent the continuation of broken lines in parenthesis.
  154. An example is :
  155. Func (Param1,
  156. >>>>>Param2);"
  157. :type 'integer :group 'ada)
  158. (defcustom ada-case-attribute 'ada-capitalize-word
  159. "*Function to call to adjust the case of Ada attributes.
  160. It may be `downcase-word', `upcase-word', `ada-loose-case-word',
  161. `ada-capitalize-word' or `ada-no-auto-case'."
  162. :type '(choice (const downcase-word)
  163. (const upcase-word)
  164. (const ada-capitalize-word)
  165. (const ada-loose-case-word)
  166. (const ada-no-auto-case))
  167. :group 'ada)
  168. (defcustom ada-case-exception-file
  169. (list (convert-standard-filename' "~/.emacs_case_exceptions"))
  170. "*List of special casing exceptions dictionaries for identifiers.
  171. The first file is the one where new exceptions will be saved by Emacs
  172. when you call `ada-create-case-exception'.
  173. These files should contain one word per line, that gives the casing
  174. to be used for that word in Ada files. If the line starts with the
  175. character *, then the exception will be used for substrings that either
  176. start at the beginning of a word or after a _ character, and end either
  177. at the end of the word or at a _ character. Each line can be terminated
  178. by a comment."
  179. :type '(repeat (file))
  180. :group 'ada)
  181. (defcustom ada-case-keyword 'downcase-word
  182. "*Function to call to adjust the case of an Ada keywords.
  183. It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
  184. `ada-capitalize-word'."
  185. :type '(choice (const downcase-word)
  186. (const upcase-word)
  187. (const ada-capitalize-word)
  188. (const ada-loose-case-word)
  189. (const ada-no-auto-case))
  190. :group 'ada)
  191. (defcustom ada-case-identifier 'ada-loose-case-word
  192. "*Function to call to adjust the case of an Ada identifier.
  193. It may be `downcase-word', `upcase-word', `ada-loose-case-word' or
  194. `ada-capitalize-word'."
  195. :type '(choice (const downcase-word)
  196. (const upcase-word)
  197. (const ada-capitalize-word)
  198. (const ada-loose-case-word)
  199. (const ada-no-auto-case))
  200. :group 'ada)
  201. (defcustom ada-clean-buffer-before-saving t
  202. "*Non-nil means remove trailing spaces and untabify the buffer before saving."
  203. :type 'boolean :group 'ada)
  204. (make-obsolete-variable 'ada-clean-buffer-before-saving
  205. "use the `write-file-functions' hook."
  206. "23.2")
  207. (defcustom ada-indent 3
  208. "*Size of Ada indentation.
  209. An example is :
  210. procedure Foo is
  211. begin
  212. >>>>>>>>>>null;"
  213. :type 'integer :group 'ada)
  214. (defcustom ada-indent-after-return t
  215. "*Non-nil means automatically indent after RET or LFD."
  216. :type 'boolean :group 'ada)
  217. (defcustom ada-indent-align-comments t
  218. "*Non-nil means align comments on previous line comments, if any.
  219. If nil, indentation is calculated as usual.
  220. Note that indentation is calculated only if `ada-indent-comment-as-code' is t.
  221. For instance:
  222. A := 1; -- A multi-line comment
  223. -- aligned if `ada-indent-align-comments' is t"
  224. :type 'boolean :group 'ada)
  225. (defcustom ada-indent-comment-as-code t
  226. "*Non-nil means indent comment lines as code.
  227. A nil value means do not auto-indent comments."
  228. :type 'boolean :group 'ada)
  229. (defcustom ada-indent-handle-comment-special nil
  230. "*Non-nil if comment lines should be handled specially inside parenthesis.
  231. By default, if the line that contains the open parenthesis has some
  232. text following it, then the following lines will be indented in the
  233. same column as this text. This will not be true if the first line is
  234. a comment and `ada-indent-handle-comment-special' is t.
  235. type A is
  236. ( Value_1, -- common behavior, when not a comment
  237. Value_2);
  238. type A is
  239. ( -- `ada-indent-handle-comment-special' is nil
  240. Value_1,
  241. Value_2);
  242. type A is
  243. ( -- `ada-indent-handle-comment-special' is non-nil
  244. Value_1,
  245. Value_2);"
  246. :type 'boolean :group 'ada)
  247. (defcustom ada-indent-is-separate t
  248. "*Non-nil means indent 'is separate' or 'is abstract' if on a single line."
  249. :type 'boolean :group 'ada)
  250. (defcustom ada-indent-record-rel-type 3
  251. "*Indentation for 'record' relative to 'type' or 'use'.
  252. An example is:
  253. type A is
  254. >>>>>>>>>>>record"
  255. :type 'integer :group 'ada)
  256. (defcustom ada-indent-renames ada-broken-indent
  257. "*Indentation for renames relative to the matching function statement.
  258. If `ada-indent-return' is null or negative, the indentation is done relative to
  259. the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
  260. An example is:
  261. function A (B : Integer)
  262. return C;
  263. >>>renames Foo;"
  264. :type 'integer :group 'ada)
  265. (defcustom ada-indent-return 0
  266. "*Indentation for 'return' relative to the matching 'function' statement.
  267. If `ada-indent-return' is null or negative, the indentation is done relative to
  268. the open parenthesis (if there is no parenthesis, `ada-broken-indent' is used).
  269. An example is:
  270. function A (B : Integer)
  271. >>>>>return C;"
  272. :type 'integer :group 'ada)
  273. (defcustom ada-indent-to-open-paren t
  274. "*Non-nil means indent according to the innermost open parenthesis."
  275. :type 'boolean :group 'ada)
  276. (defcustom ada-fill-comment-prefix "-- "
  277. "*Text inserted in the first columns when filling a comment paragraph.
  278. Note: if you modify this variable, you will have to invoke `ada-mode'
  279. again to take account of the new value."
  280. :type 'string :group 'ada)
  281. (defcustom ada-fill-comment-postfix " --"
  282. "*Text inserted at the end of each line when filling a comment paragraph.
  283. Used by `ada-fill-comment-paragraph-postfix'."
  284. :type 'string :group 'ada)
  285. (defcustom ada-label-indent -4
  286. "*Number of columns to indent a label.
  287. An example is:
  288. procedure Foo is
  289. begin
  290. >>>>Label:
  291. This is also used for <<..>> labels"
  292. :type 'integer :group 'ada)
  293. (defcustom ada-language-version 'ada95
  294. "*Ada language version; one of `ada83', `ada95', `ada2005'."
  295. :type '(choice (const ada83) (const ada95) (const ada2005)) :group 'ada)
  296. (defcustom ada-move-to-declaration nil
  297. "*Non-nil means `ada-move-to-start' moves to the subprogram declaration, not to 'begin'."
  298. :type 'boolean :group 'ada)
  299. (defcustom ada-popup-key '[down-mouse-3]
  300. "*Key used for binding the contextual menu.
  301. If nil, no contextual menu is available."
  302. :type '(restricted-sexp :match-alternatives (stringp vectorp))
  303. :group 'ada)
  304. (defcustom ada-search-directories
  305. (append '(".")
  306. (split-string (or (getenv "ADA_INCLUDE_PATH") "") ":")
  307. '("/usr/adainclude" "/usr/local/adainclude"
  308. "/opt/gnu/adainclude"))
  309. "*Default list of directories to search for Ada files.
  310. See the description for the `ff-search-directories' variable. This variable
  311. is the initial value of `ada-search-directories-internal'."
  312. :type '(repeat (choice :tag "Directory"
  313. (const :tag "default" nil)
  314. (directory :format "%v")))
  315. :group 'ada)
  316. (defvar ada-search-directories-internal ada-search-directories
  317. "Internal version of `ada-search-directories'.
  318. Its value is the concatenation of the search path as read in the project file
  319. and the standard runtime location, and the value of the user-defined
  320. `ada-search-directories'.")
  321. (defcustom ada-stmt-end-indent 0
  322. "*Number of columns to indent the end of a statement on a separate line.
  323. An example is:
  324. if A = B
  325. >>>>then"
  326. :type 'integer :group 'ada)
  327. (defcustom ada-tab-policy 'indent-auto
  328. "*Control the behavior of the TAB key.
  329. Must be one of :
  330. `indent-rigidly' : always adds `ada-indent' blanks at the beginning of the line.
  331. `indent-auto' : use indentation functions in this file.
  332. `always-tab' : do `indent-relative'."
  333. :type '(choice (const indent-auto)
  334. (const indent-rigidly)
  335. (const always-tab))
  336. :group 'ada)
  337. (defcustom ada-use-indent ada-broken-indent
  338. "*Indentation for the lines in a 'use' statement.
  339. An example is:
  340. use Ada.Text_IO,
  341. >>>>Ada.Numerics;"
  342. :type 'integer :group 'ada)
  343. (defcustom ada-when-indent 3
  344. "*Indentation for 'when' relative to 'exception' or 'case'.
  345. An example is:
  346. case A is
  347. >>>>when B =>"
  348. :type 'integer :group 'ada)
  349. (defcustom ada-with-indent ada-broken-indent
  350. "*Indentation for the lines in a 'with' statement.
  351. An example is:
  352. with Ada.Text_IO,
  353. >>>>Ada.Numerics;"
  354. :type 'integer :group 'ada)
  355. (defcustom ada-which-compiler 'gnat
  356. "*Name of the compiler to use.
  357. This will determine what features are made available through the Ada mode.
  358. The possible choices are:
  359. `gnat': Use Ada Core Technologies' GNAT compiler. Add some cross-referencing
  360. features.
  361. `generic': Use a generic compiler."
  362. :type '(choice (const gnat)
  363. (const generic))
  364. :group 'ada)
  365. ;;; ---- end of user configurable variables
  366. (defvar ada-body-suffixes '(".adb")
  367. "List of possible suffixes for Ada body files.
  368. The extensions should include a `.' if needed.")
  369. (defvar ada-spec-suffixes '(".ads")
  370. "List of possible suffixes for Ada spec files.
  371. The extensions should include a `.' if needed.")
  372. (defvar ada-mode-menu (make-sparse-keymap "Ada")
  373. "Menu for Ada mode.")
  374. (defvar ada-mode-map (make-sparse-keymap)
  375. "Local keymap used for Ada mode.")
  376. (defvar ada-mode-extra-map (make-sparse-keymap)
  377. "Keymap used for non-standard keybindings.")
  378. ;; default is C-c C-q because it's free in ada-mode-map
  379. (defvar ada-mode-extra-prefix "\C-c\C-q"
  380. "Prefix key to access `ada-mode-extra-map' functions.")
  381. (defvar ada-mode-abbrev-table nil
  382. "Local abbrev table for Ada mode.")
  383. (define-abbrev-table 'ada-mode-abbrev-table ())
  384. (defvar ada-mode-syntax-table nil
  385. "Syntax table to be used for editing Ada source code.")
  386. (defvar ada-mode-symbol-syntax-table nil
  387. "Syntax table for Ada, where `_' is a word constituent.")
  388. (eval-when-compile
  389. ;; These values are used in eval-when-compile expressions.
  390. (defconst ada-83-string-keywords
  391. '("abort" "abs" "accept" "access" "all" "and" "array" "at" "begin"
  392. "body" "case" "constant" "declare" "delay" "delta" "digits" "do"
  393. "else" "elsif" "end" "entry" "exception" "exit" "for" "function"
  394. "generic" "goto" "if" "in" "is" "limited" "loop" "mod" "new"
  395. "not" "null" "of" "or" "others" "out" "package" "pragma" "private"
  396. "procedure" "raise" "range" "record" "rem" "renames" "return"
  397. "reverse" "select" "separate" "subtype" "task" "terminate" "then"
  398. "type" "use" "when" "while" "with" "xor")
  399. "List of Ada 83 keywords.
  400. Used to define `ada-*-keywords'.")
  401. (defconst ada-95-string-keywords
  402. '("abstract" "aliased" "protected" "requeue" "tagged" "until")
  403. "List of keywords new in Ada 95.
  404. Used to define `ada-*-keywords'.")
  405. (defconst ada-2005-string-keywords
  406. '("interface" "overriding" "synchronized")
  407. "List of keywords new in Ada 2005.
  408. Used to define `ada-*-keywords.'"))
  409. (defvar ada-ret-binding nil
  410. "Variable to save key binding of RET when casing is activated.")
  411. (defvar ada-case-exception '()
  412. "Alist of words (entities) that have special casing.")
  413. (defvar ada-case-exception-substring '()
  414. "Alist of substrings (entities) that have special casing.
  415. The substrings are detected for word constituent when the word
  416. is not itself in `ada-case-exception', and only for substrings that
  417. either are at the beginning or end of the word, or start after '_'.")
  418. (defvar ada-lfd-binding nil
  419. "Variable to save key binding of LFD when casing is activated.")
  420. (defvar ada-other-file-alist nil
  421. "Variable used by `find-file' to find the name of the other package.
  422. See `ff-other-file-alist'.")
  423. (defvar ada-align-list
  424. '(("[^:]\\(\\s-*\\):[^:]" 1 t)
  425. ("[^=]\\(\\s-+\\)=[^=]" 1 t)
  426. ("\\(\\s-*\\)use\\s-" 1)
  427. ("\\(\\s-*\\)--" 1))
  428. "Ada support for align.el <= 2.2.
  429. This variable provides regular expressions on which to align different lines.
  430. See `align-mode-alist' for more information.")
  431. (defvar ada-align-modes
  432. '((ada-declaration
  433. (regexp . "[^:]\\(\\s-*\\):[^:]")
  434. (valid . (lambda() (not (ada-in-comment-p))))
  435. (modes . '(ada-mode)))
  436. (ada-assignment
  437. (regexp . "[^=]\\(\\s-+\\)=[^=]")
  438. (valid . (lambda() (not (ada-in-comment-p))))
  439. (modes . '(ada-mode)))
  440. (ada-comment
  441. (regexp . "\\(\\s-*\\)--")
  442. (modes . '(ada-mode)))
  443. (ada-use
  444. (regexp . "\\(\\s-*\\)use\\s-")
  445. (valid . (lambda() (not (ada-in-comment-p))))
  446. (modes . '(ada-mode)))
  447. )
  448. "Ada support for align.el >= 2.8.
  449. This variable defines several rules to use to align different lines.")
  450. (defconst ada-align-region-separate
  451. (eval-when-compile
  452. (concat
  453. "^\\s-*\\($\\|\\("
  454. "begin\\|"
  455. "declare\\|"
  456. "else\\|"
  457. "end\\|"
  458. "exception\\|"
  459. "for\\|"
  460. "function\\|"
  461. "generic\\|"
  462. "if\\|"
  463. "is\\|"
  464. "procedure\\|"
  465. "record\\|"
  466. "return\\|"
  467. "type\\|"
  468. "when"
  469. "\\)\\>\\)"))
  470. "See the variable `align-region-separate' for more information.")
  471. ;;; ---- Below are the regexp used in this package for parsing
  472. (defconst ada-83-keywords
  473. (eval-when-compile
  474. (concat "\\<" (regexp-opt ada-83-string-keywords t) "\\>"))
  475. "Regular expression matching Ada83 keywords.")
  476. (defconst ada-95-keywords
  477. (eval-when-compile
  478. (concat "\\<" (regexp-opt
  479. (append
  480. ada-95-string-keywords
  481. ada-83-string-keywords) t) "\\>"))
  482. "Regular expression matching Ada95 keywords.")
  483. (defconst ada-2005-keywords
  484. (eval-when-compile
  485. (concat "\\<" (regexp-opt
  486. (append
  487. ada-2005-string-keywords
  488. ada-83-string-keywords
  489. ada-95-string-keywords) t) "\\>"))
  490. "Regular expression matching Ada2005 keywords.")
  491. (defvar ada-keywords ada-2005-keywords
  492. "Regular expression matching Ada keywords.")
  493. ;; FIXME: make this customizable
  494. (defconst ada-ident-re
  495. "[[:alpha:]]\\(?:[_[:alnum:]]\\)*"
  496. ;; [:alnum:] matches any multibyte word constituent, as well as
  497. ;; Latin-1 letters and numbers. This allows __ and trailing _;
  498. ;; someone (emacs bug#1919) proposed [^\W_] to fix that, but \W does
  499. ;; _not_ mean "not word constituent" inside a character alternative.
  500. "Regexp matching an Ada identifier.")
  501. (defconst ada-goto-label-re
  502. (concat "<<" ada-ident-re ">>")
  503. "Regexp matching a goto label.")
  504. (defconst ada-block-label-re
  505. (concat ada-ident-re "[ \t\n]*:[^=]")
  506. "Regexp matching a block label.
  507. Note that this also matches a variable declaration.")
  508. (defconst ada-label-re
  509. (concat "\\(?:" ada-block-label-re "\\)\\|\\(?:" ada-goto-label-re "\\)")
  510. "Regexp matching a goto or block label.")
  511. ;; "with" needs to be included in the regexp, to match generic subprogram parameters
  512. ;; Similarly, we put '[not] overriding' on the same line with 'procedure' etc.
  513. (defvar ada-procedure-start-regexp
  514. (concat
  515. "^[ \t]*\\(with[ \t]+\\)?\\(\\(not[ \t]+\\)?overriding[ \t]+\\)?\\(procedure\\|function\\|task\\)[ \t\n]+"
  516. ;; subprogram name: operator ("[+/=*]")
  517. "\\("
  518. "\\(\"[^\"]+\"\\)"
  519. ;; subprogram name: name
  520. "\\|"
  521. "\\(\\(\\sw\\|[_.]\\)+\\)"
  522. "\\)")
  523. "Regexp matching Ada subprogram start.
  524. The actual start is at (match-beginning 4). The name is in (match-string 5).")
  525. (defconst ada-name-regexp
  526. "\\([a-zA-Z][a-zA-Z0-9_.']*[a-zA-Z0-9]\\)"
  527. "Regexp matching a fully qualified name (including attribute).")
  528. (defconst ada-package-start-regexp
  529. (concat "^[ \t]*\\(private[ \t]+\\)?\\(package\\)[ \t\n]+\\(body[ \t]*\\)?" ada-name-regexp)
  530. "Regexp matching start of package.
  531. The package name is in (match-string 4).")
  532. (defconst ada-compile-goto-error-file-linenr-re
  533. "\\([-_.a-zA-Z0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?"
  534. "Regexp matching filename:linenr[:column].")
  535. ;;; ---- regexps for indentation functions
  536. (defvar ada-block-start-re
  537. (eval-when-compile
  538. (concat "\\<\\(" (regexp-opt '("begin" "declare" "else"
  539. "exception" "generic" "loop" "or"
  540. "private" "select" ))
  541. "\\|\\(\\(limited\\|abstract\\|tagged\\)[ \t\n]+\\)*record\\)\\>"))
  542. "Regexp for keywords starting Ada blocks.")
  543. (defvar ada-end-stmt-re
  544. (eval-when-compile
  545. (concat "\\("
  546. ";" "\\|"
  547. "=>[ \t]*$" "\\|"
  548. "=>[ \t]*--.*$" "\\|"
  549. "^[ \t]*separate[ \t]*(\\(\\sw\\|[_.]\\)+)" "\\|"
  550. "\\<" (regexp-opt '("begin" "declare" "is" "do" "else" "generic"
  551. "loop" "private" "record" "select"
  552. "then abort" "then") t) "\\>" "\\|"
  553. "^[ \t]*" (regexp-opt '("function" "package" "procedure")
  554. t) "\\>\\(\\sw\\|[ \t_.]\\)+\\<is\\>" "\\|"
  555. "^[ \t]*exception\\>"
  556. "\\)") )
  557. "Regexp of possible ends for a non-broken statement.
  558. A new statement starts after these.")
  559. (defvar ada-matching-start-re
  560. (eval-when-compile
  561. (concat "\\<"
  562. (regexp-opt
  563. '("end" "loop" "select" "begin" "case" "do" "declare"
  564. "if" "task" "package" "procedure" "function" "record" "protected") t)
  565. "\\>"))
  566. "Regexp used in `ada-goto-matching-start'.")
  567. (defvar ada-loop-start-re
  568. "\\<\\(for\\|while\\|loop\\)\\>"
  569. "Regexp for the start of a loop.")
  570. (defvar ada-subprog-start-re
  571. (eval-when-compile
  572. (concat "\\<" (regexp-opt '("accept" "entry" "function" "overriding" "package" "procedure"
  573. "protected" "task") t) "\\>"))
  574. "Regexp for the start of a subprogram.")
  575. (defvar ada-contextual-menu-on-identifier nil
  576. "Set to true when the right mouse button was clicked on an identifier.")
  577. (defvar ada-contextual-menu-last-point nil
  578. "Position of point just before displaying the menu.
  579. This is a list (point buffer).
  580. Since `ada-popup-menu' moves the point where the user clicked, the region
  581. is modified. Therefore no command from the menu knows what the user selected
  582. before displaying the contextual menu.
  583. To get the original region, restore the point to this position before
  584. calling `region-end' and `region-beginning'.
  585. Modify this variable if you want to restore the point to another position.")
  586. (easy-menu-define ada-contextual-menu nil
  587. "Menu to use when the user presses the right mouse button.
  588. The variable `ada-contextual-menu-on-identifier' will be set to t before
  589. displaying the menu if point was on an identifier."
  590. '("Ada"
  591. ["Goto Declaration/Body" ada-point-and-xref
  592. :included ada-contextual-menu-on-identifier]
  593. ["Goto Body" ada-point-and-xref-body
  594. :included ada-contextual-menu-on-identifier]
  595. ["Goto Previous Reference" ada-xref-goto-previous-reference]
  596. ["List References" ada-find-references
  597. :included ada-contextual-menu-on-identifier]
  598. ["List Local References" ada-find-local-references
  599. :included ada-contextual-menu-on-identifier]
  600. ["-" nil nil]
  601. ["Other File" ff-find-other-file]
  602. ["Goto Parent Unit" ada-goto-parent]))
  603. ;;------------------------------------------------------------------
  604. ;; Support for imenu (see imenu.el)
  605. ;;------------------------------------------------------------------
  606. (defconst ada-imenu-comment-re "\\([ \t]*--.*\\)?")
  607. (defconst ada-imenu-subprogram-menu-re
  608. (concat "^[ \t]*\\(overriding[ \t]*\\)?\\(procedure\\|function\\)[ \t\n]+"
  609. "\\(\\(\\sw\\|_\\)+\\)[ \t\n]*\\([ \t\n]\\|([^)]+)"
  610. ada-imenu-comment-re
  611. "\\)[ \t\n]*"
  612. "\\(return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?is[ \t\n]"))
  613. (defvar ada-imenu-generic-expression
  614. (list
  615. (list nil ada-imenu-subprogram-menu-re 3)
  616. (list "*Specs*"
  617. (concat
  618. "^[ \t]*\\(procedure\\|function\\)[ \t\n]+\\(\\(\\sw\\|_\\)+\\)"
  619. "\\("
  620. "\\(" ada-imenu-comment-re "[ \t\n]+\\|[ \t\n]*([^)]+)"
  621. ada-imenu-comment-re "\\)";; parameter list or simple space
  622. "\\([ \t\n]*return[ \t\n]+\\(\\sw\\|[_.]\\)+[ \t\n]*\\)?"
  623. "\\)?;") 2)
  624. '("*Tasks*" "^[ \t]*task[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
  625. '("*Type Defs*" "^[ \t]*\\(sub\\)?type[ \t]+\\(\\(\\sw\\|_\\)+\\)" 2)
  626. '("*Protected*"
  627. "^[ \t]*protected[ \t]+\\(type[ \t]+\\)?\\(\\(body[ \t]+\\)?\\(\\sw\\|_\\)+\\)" 2)
  628. '("*Packages*" "^[ \t]*package[ \t]+\\(\\(body[ \t]+\\)?\\(\\sw\\|[_.]\\)+\\)" 1))
  629. "Imenu generic expression for Ada mode.
  630. See `imenu-generic-expression'. This variable will create several submenus for
  631. each type of entity that can be found in an Ada file.")
  632. ;;------------------------------------------------------------
  633. ;; Support for compile.el
  634. ;;------------------------------------------------------------
  635. (defun ada-compile-mouse-goto-error ()
  636. "Mouse interface for `ada-compile-goto-error'."
  637. (interactive)
  638. (mouse-set-point last-input-event)
  639. (ada-compile-goto-error (point))
  640. )
  641. (defun ada-compile-goto-error (pos)
  642. "Replace `compile-goto-error' from compile.el.
  643. If POS is on a file and line location, go to this position. It adds
  644. to compile.el the capacity to go to a reference in an error message.
  645. For instance, on these lines:
  646. foo.adb:61:11: [...] in call to size declared at foo.ads:11
  647. foo.adb:61:11: [...] in call to local declared at line 20
  648. the 4 file locations can be clicked on and jumped to."
  649. (interactive "d")
  650. (goto-char pos)
  651. (skip-chars-backward "-a-zA-Z0-9_:./\\")
  652. (cond
  653. ;; special case: looking at a filename:line not at the beginning of a line
  654. ;; or a simple line reference "at line ..."
  655. ((and (not (bolp))
  656. (or (looking-at ada-compile-goto-error-file-linenr-re)
  657. (and
  658. (save-excursion
  659. (beginning-of-line)
  660. (looking-at ada-compile-goto-error-file-linenr-re))
  661. (save-excursion
  662. (if (looking-at "\\([0-9]+\\)") (backward-word 1))
  663. (looking-at "line \\([0-9]+\\)"))))
  664. )
  665. (let ((line (if (match-beginning 2) (match-string 2) (match-string 1)))
  666. (file (if (match-beginning 2) (match-string 1)
  667. (save-excursion (beginning-of-line)
  668. (looking-at ada-compile-goto-error-file-linenr-re)
  669. (match-string 1))))
  670. (error-pos (point-marker))
  671. source)
  672. ;; set source marker
  673. (save-excursion
  674. (compilation-find-file (point-marker) (match-string 1) "./")
  675. (set-buffer file)
  676. (when (stringp line)
  677. (goto-char (point-min))
  678. (forward-line (1- (string-to-number line))))
  679. (setq source (point-marker)))
  680. (compilation-goto-locus error-pos source nil)
  681. ))
  682. ;; otherwise, default behavior
  683. (t
  684. (compile-goto-error))
  685. )
  686. (recenter))
  687. ;;-------------------------------------------------------------------------
  688. ;; Grammar related function
  689. ;; The functions below work with the syntax class of the characters in an Ada
  690. ;; buffer. Two syntax tables are created, depending on whether we want '_'
  691. ;; to be considered as part of a word or not.
  692. ;; Some characters may have multiple meanings depending on the context:
  693. ;; - ' is either the beginning of a constant character or an attribute
  694. ;; - # is either part of a based literal or a gnatprep statement.
  695. ;; - " starts a string, but not if inside a constant character.
  696. ;; - ( and ) should be ignored if inside a constant character.
  697. ;; Thus their syntax property is changed automatically, and we can still use
  698. ;; the standard Emacs functions for sexp (see `ada-in-string-p')
  699. ;;
  700. ;; On Emacs, this is done through the `syntax-table' text property. The
  701. ;; corresponding action is applied automatically each time the buffer
  702. ;; changes via syntax-propertize-function.
  703. ;;
  704. ;; on XEmacs, the `syntax-table' property does not exist and we have to use a
  705. ;; slow advice to `parse-partial-sexp' to do the same thing.
  706. ;; When executing parse-partial-sexp, we simply modify the strings before and
  707. ;; after, so that the special constants '"', '(' and ')' do not interact
  708. ;; with parse-partial-sexp.
  709. ;; Note: this code is slow and needs to be rewritten as soon as something
  710. ;; better is available on XEmacs.
  711. ;;-------------------------------------------------------------------------
  712. (defun ada-create-syntax-table ()
  713. "Create the two syntax tables use in the Ada mode.
  714. The standard table declares `_' as a symbol constituent, the second one
  715. declares it as a word constituent."
  716. (interactive)
  717. (setq ada-mode-syntax-table (make-syntax-table))
  718. ;; define string brackets (`%' is alternative string bracket, but
  719. ;; almost never used as such and throws font-lock and indentation
  720. ;; off the track.)
  721. (modify-syntax-entry ?% "$" ada-mode-syntax-table)
  722. (modify-syntax-entry ?\" "\"" ada-mode-syntax-table)
  723. (modify-syntax-entry ?: "." ada-mode-syntax-table)
  724. (modify-syntax-entry ?\; "." ada-mode-syntax-table)
  725. (modify-syntax-entry ?& "." ada-mode-syntax-table)
  726. (modify-syntax-entry ?\| "." ada-mode-syntax-table)
  727. (modify-syntax-entry ?+ "." ada-mode-syntax-table)
  728. (modify-syntax-entry ?* "." ada-mode-syntax-table)
  729. (modify-syntax-entry ?/ "." ada-mode-syntax-table)
  730. (modify-syntax-entry ?= "." ada-mode-syntax-table)
  731. (modify-syntax-entry ?< "." ada-mode-syntax-table)
  732. (modify-syntax-entry ?> "." ada-mode-syntax-table)
  733. (modify-syntax-entry ?$ "." ada-mode-syntax-table)
  734. (modify-syntax-entry ?\[ "." ada-mode-syntax-table)
  735. (modify-syntax-entry ?\] "." ada-mode-syntax-table)
  736. (modify-syntax-entry ?\{ "." ada-mode-syntax-table)
  737. (modify-syntax-entry ?\} "." ada-mode-syntax-table)
  738. (modify-syntax-entry ?. "." ada-mode-syntax-table)
  739. (modify-syntax-entry ?\\ "." ada-mode-syntax-table)
  740. (modify-syntax-entry ?\' "." ada-mode-syntax-table)
  741. ;; a single hyphen is punctuation, but a double hyphen starts a comment
  742. (modify-syntax-entry ?- ". 12" ada-mode-syntax-table)
  743. ;; See the comment above on grammar related function for the special
  744. ;; setup for '#'.
  745. (if (featurep 'xemacs)
  746. (modify-syntax-entry ?# "<" ada-mode-syntax-table)
  747. (modify-syntax-entry ?# "$" ada-mode-syntax-table))
  748. ;; and \f and \n end a comment
  749. (modify-syntax-entry ?\f "> " ada-mode-syntax-table)
  750. (modify-syntax-entry ?\n "> " ada-mode-syntax-table)
  751. ;; define what belongs in Ada symbols
  752. (modify-syntax-entry ?_ "_" ada-mode-syntax-table)
  753. ;; define parentheses to match
  754. (modify-syntax-entry ?\( "()" ada-mode-syntax-table)
  755. (modify-syntax-entry ?\) ")(" ada-mode-syntax-table)
  756. (setq ada-mode-symbol-syntax-table (copy-syntax-table ada-mode-syntax-table))
  757. (modify-syntax-entry ?_ "w" ada-mode-symbol-syntax-table)
  758. )
  759. ;; Support of special characters in XEmacs (see the comments at the beginning
  760. ;; of the section on Grammar related functions).
  761. (if (featurep 'xemacs)
  762. (defadvice parse-partial-sexp (around parse-partial-sexp-protect-constants)
  763. "Handles special character constants and gnatprep statements."
  764. (let (change)
  765. (if (< to from)
  766. (let ((tmp from))
  767. (setq from to to tmp)))
  768. (save-excursion
  769. (goto-char from)
  770. (while (re-search-forward "'\\([(\")#]\\)'" to t)
  771. (setq change (cons (list (match-beginning 1)
  772. 1
  773. (match-string 1))
  774. change))
  775. (replace-match "'A'"))
  776. (goto-char from)
  777. (while (re-search-forward "\\(#[0-9a-fA-F]*#\\)" to t)
  778. (setq change (cons (list (match-beginning 1)
  779. (length (match-string 1))
  780. (match-string 1))
  781. change))
  782. (replace-match (make-string (length (match-string 1)) ?@))))
  783. ad-do-it
  784. (save-excursion
  785. (while change
  786. (goto-char (caar change))
  787. (delete-char (cadar change))
  788. (insert (caddar change))
  789. (setq change (cdr change)))))))
  790. (unless (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
  791. ;; Before `syntax-propertize', we had to use font-lock to apply syntax-table
  792. ;; properties, and in some cases we even had to do it manually (in
  793. ;; `ada-after-change-function'). `ada-handle-syntax-table-properties'
  794. ;; decides which method to use.
  795. (defun ada-set-syntax-table-properties ()
  796. "Assign `syntax-table' properties in accessible part of buffer.
  797. In particular, character constants are said to be strings, #...#
  798. are treated as numbers instead of gnatprep comments."
  799. (let ((modified (buffer-modified-p))
  800. (buffer-undo-list t)
  801. (inhibit-read-only t)
  802. (inhibit-point-motion-hooks t)
  803. (inhibit-modification-hooks t))
  804. (remove-text-properties (point-min) (point-max) '(syntax-table nil))
  805. (goto-char (point-min))
  806. (while (re-search-forward
  807. ;; The following regexp was adapted from
  808. ;; `ada-font-lock-syntactic-keywords'.
  809. "^[ \t]*\\(#\\(?:if\\|else\\|elsif\\|end\\)\\)\\|[^a-zA-Z0-9)]\\('\\)[^'\n]\\('\\)"
  810. nil t)
  811. (if (match-beginning 1)
  812. (put-text-property
  813. (match-beginning 1) (match-end 1) 'syntax-table '(11 . ?\n))
  814. (put-text-property
  815. (match-beginning 2) (match-end 2) 'syntax-table '(7 . ?'))
  816. (put-text-property
  817. (match-beginning 3) (match-end 3) 'syntax-table '(7 . ?'))))
  818. (unless modified
  819. (restore-buffer-modified-p nil))))
  820. (defun ada-after-change-function (beg end _old-len)
  821. "Called when the region between BEG and END was changed in the buffer.
  822. OLD-LEN indicates what the length of the replaced text was."
  823. (save-excursion
  824. (save-restriction
  825. (let ((from (progn (goto-char beg) (line-beginning-position)))
  826. (to (progn (goto-char end) (line-end-position))))
  827. (narrow-to-region from to)
  828. (save-match-data
  829. (ada-set-syntax-table-properties))))))
  830. (defun ada-initialize-syntax-table-properties ()
  831. "Assign `syntax-table' properties in current buffer."
  832. (save-excursion
  833. (save-restriction
  834. (widen)
  835. (save-match-data
  836. (ada-set-syntax-table-properties))))
  837. (add-hook 'after-change-functions 'ada-after-change-function nil t))
  838. (defun ada-handle-syntax-table-properties ()
  839. "Handle `syntax-table' properties."
  840. (if font-lock-mode
  841. ;; `font-lock-mode' will take care of `syntax-table' properties.
  842. (remove-hook 'after-change-functions 'ada-after-change-function t)
  843. ;; Take care of `syntax-table' properties manually.
  844. (ada-initialize-syntax-table-properties)))
  845. ) ;;(not (fboundp 'syntax-propertize))
  846. ;;------------------------------------------------------------------
  847. ;; Testing the grammatical context
  848. ;;------------------------------------------------------------------
  849. (defsubst ada-in-comment-p (&optional parse-result)
  850. "Return t if inside a comment.
  851. If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
  852. (nth 4 (or parse-result
  853. (parse-partial-sexp
  854. (line-beginning-position) (point)))))
  855. (defsubst ada-in-string-p (&optional parse-result)
  856. "Return t if point is inside a string.
  857. If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
  858. (nth 3 (or parse-result
  859. (parse-partial-sexp
  860. (line-beginning-position) (point)))))
  861. (defsubst ada-in-string-or-comment-p (&optional parse-result)
  862. "Return t if inside a comment or string.
  863. If PARSE-RESULT is non-nil, use it instead of calling `parse-partial-sexp'."
  864. (setq parse-result (or parse-result
  865. (parse-partial-sexp
  866. (line-beginning-position) (point))))
  867. (or (ada-in-string-p parse-result) (ada-in-comment-p parse-result)))
  868. (defsubst ada-in-numeric-literal-p ()
  869. "Return t if point is after a prefix of a numeric literal."
  870. (looking-back "\\([0-9]+#[0-9a-fA-F_]+\\)"))
  871. ;;------------------------------------------------------------------
  872. ;; Contextual menus
  873. ;; The Ada mode comes with contextual menus, bound by default to the right
  874. ;; mouse button.
  875. ;; Add items to this menu by modifying `ada-contextual-menu'. Note that the
  876. ;; variable `ada-contextual-menu-on-identifier' is set automatically to t
  877. ;; if the mouse button was pressed on an identifier.
  878. ;;------------------------------------------------------------------
  879. (defun ada-call-from-contextual-menu (function)
  880. "Execute FUNCTION when called from the contextual menu.
  881. It forces Emacs to change the cursor position."
  882. (interactive)
  883. (funcall function)
  884. (setq ada-contextual-menu-last-point
  885. (list (point) (current-buffer))))
  886. (defun ada-popup-menu (position)
  887. "Pops up a contextual menu, depending on where the user clicked.
  888. POSITION is the location the mouse was clicked on.
  889. Sets `ada-contextual-menu-last-point' to the current position before
  890. displaying the menu. When a function from the menu is called, the
  891. point is where the mouse button was clicked."
  892. (interactive "e")
  893. ;; declare this as a local variable, so that the function called
  894. ;; in the contextual menu does not hide the region in
  895. ;; transient-mark-mode.
  896. (let ((deactivate-mark nil))
  897. (setq ada-contextual-menu-last-point
  898. (list (point) (current-buffer)))
  899. (mouse-set-point last-input-event)
  900. (setq ada-contextual-menu-on-identifier
  901. (and (char-after)
  902. (or (= (char-syntax (char-after)) ?w)
  903. (= (char-after) ?_))
  904. (not (ada-in-string-or-comment-p))
  905. (save-excursion (skip-syntax-forward "w")
  906. (not (ada-after-keyword-p)))
  907. ))
  908. (if (fboundp 'popup-menu)
  909. (funcall (symbol-function 'popup-menu) ada-contextual-menu)
  910. (let (choice)
  911. (setq choice (x-popup-menu position ada-contextual-menu))
  912. (if choice
  913. (funcall (lookup-key ada-contextual-menu (vector (car choice)))))))
  914. (set-buffer (cadr ada-contextual-menu-last-point))
  915. (goto-char (car ada-contextual-menu-last-point))
  916. ))
  917. ;;------------------------------------------------------------------
  918. ;; Misc functions
  919. ;;------------------------------------------------------------------
  920. ;;;###autoload
  921. (defun ada-add-extensions (spec body)
  922. "Define SPEC and BODY as being valid extensions for Ada files.
  923. Going from body to spec with `ff-find-other-file' used these
  924. extensions.
  925. SPEC and BODY are two regular expressions that must match against
  926. the file name."
  927. (let* ((reg (concat (regexp-quote body) "$"))
  928. (tmp (assoc reg ada-other-file-alist)))
  929. (if tmp
  930. (setcdr tmp (list (cons spec (cadr tmp))))
  931. (add-to-list 'ada-other-file-alist (list reg (list spec)))))
  932. (let* ((reg (concat (regexp-quote spec) "$"))
  933. (tmp (assoc reg ada-other-file-alist)))
  934. (if tmp
  935. (setcdr tmp (list (cons body (cadr tmp))))
  936. (add-to-list 'ada-other-file-alist (list reg (list body)))))
  937. (add-to-list 'auto-mode-alist
  938. (cons (concat (regexp-quote spec) "\\'") 'ada-mode))
  939. (add-to-list 'auto-mode-alist
  940. (cons (concat (regexp-quote body) "\\'") 'ada-mode))
  941. (add-to-list 'ada-spec-suffixes spec)
  942. (add-to-list 'ada-body-suffixes body)
  943. ;; Support for speedbar (Specifies that we want to see these files in
  944. ;; speedbar)
  945. (if (fboundp 'speedbar-add-supported-extension)
  946. (progn
  947. (funcall (symbol-function 'speedbar-add-supported-extension)
  948. spec)
  949. (funcall (symbol-function 'speedbar-add-supported-extension)
  950. body))))
  951. (defvar ada-font-lock-syntactic-keywords) ; defined below
  952. ;;;###autoload
  953. (define-derived-mode ada-mode prog-mode "Ada"
  954. "Ada mode is the major mode for editing Ada code."
  955. ;; Set the paragraph delimiters so that one can select a whole block
  956. ;; simply with M-h
  957. (set (make-local-variable 'paragraph-start) "[ \t\n\f]*$")
  958. (set (make-local-variable 'paragraph-separate) "[ \t\n\f]*$")
  959. ;; comment end must be set because it may hold a wrong value if
  960. ;; this buffer had been in another mode before. RE
  961. (set (make-local-variable 'comment-end) "")
  962. ;; used by autofill and indent-new-comment-line
  963. (set (make-local-variable 'comment-start-skip) "---*[ \t]*")
  964. ;; used by autofill to break a comment line and continue it on another line.
  965. ;; The reason we need this one is that the default behavior does not work
  966. ;; correctly with the definition of paragraph-start above when the comment
  967. ;; is right after a multi-line subprogram declaration (the comments are
  968. ;; aligned under the latest parameter, not under the declaration start).
  969. (set (make-local-variable 'comment-line-break-function)
  970. (lambda (&optional soft) (let ((fill-prefix nil))
  971. (indent-new-comment-line soft))))
  972. (set (make-local-variable 'indent-line-function)
  973. 'ada-indent-current-function)
  974. (set (make-local-variable 'comment-column) 40)
  975. ;; Emacs 20.3 defines a comment-padding to insert spaces between
  976. ;; the comment and the text. We do not want any, this is already
  977. ;; included in comment-start
  978. (unless (featurep 'xemacs)
  979. (set (make-local-variable 'parse-sexp-ignore-comments) t)
  980. (set (make-local-variable 'comment-padding) 0)
  981. (set (make-local-variable 'parse-sexp-lookup-properties) t))
  982. (setq case-fold-search t)
  983. (if (boundp 'imenu-case-fold-search)
  984. (setq imenu-case-fold-search t))
  985. (set (make-local-variable 'fill-paragraph-function)
  986. 'ada-fill-comment-paragraph)
  987. ;; Support for compile.el
  988. ;; We just substitute our own functions to go to the error.
  989. (add-hook 'compilation-mode-hook
  990. (lambda()
  991. ;; FIXME: This has global impact! -stef
  992. (define-key compilation-minor-mode-map [mouse-2]
  993. 'ada-compile-mouse-goto-error)
  994. (define-key compilation-minor-mode-map "\C-c\C-c"
  995. 'ada-compile-goto-error)
  996. (define-key compilation-minor-mode-map "\C-m"
  997. 'ada-compile-goto-error)))
  998. ;; font-lock support :
  999. (set (make-local-variable 'font-lock-defaults)
  1000. '(ada-font-lock-keywords
  1001. nil t
  1002. ((?\_ . "w") (?# . "."))
  1003. beginning-of-line))
  1004. (if (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
  1005. (set (make-local-variable 'syntax-propertize-function)
  1006. (syntax-propertize-via-font-lock ada-font-lock-syntactic-keywords))
  1007. (set (make-local-variable 'font-lock-syntactic-keywords)
  1008. ada-font-lock-syntactic-keywords))
  1009. ;; Set up support for find-file.el.
  1010. (set (make-local-variable 'ff-other-file-alist)
  1011. 'ada-other-file-alist)
  1012. (set (make-local-variable 'ff-search-directories)
  1013. 'ada-search-directories-internal)
  1014. (setq ff-post-load-hook 'ada-set-point-accordingly
  1015. ff-file-created-hook 'ada-make-body)
  1016. (add-hook 'ff-pre-load-hook 'ada-which-function-are-we-in)
  1017. (make-local-variable 'ff-special-constructs)
  1018. (mapc (lambda (pair) (add-to-list 'ff-special-constructs pair))
  1019. (list
  1020. ;; Top level child package declaration; go to the parent package.
  1021. (cons (eval-when-compile
  1022. (concat "^\\(private[ \t]\\)?[ \t]*package[ \t]+"
  1023. "\\(body[ \t]+\\)?"
  1024. "\\(\\(\\sw\\|[_.]\\)+\\)\\.\\(\\sw\\|_\\)+[ \t\n]+is"))
  1025. (lambda ()
  1026. (ff-get-file
  1027. ada-search-directories-internal
  1028. (ada-make-filename-from-adaname (match-string 3))
  1029. ada-spec-suffixes)))
  1030. ;; A "separate" clause.
  1031. (cons "^separate[ \t\n]*(\\(\\(\\sw\\|[_.]\\)+\\))"
  1032. (lambda ()
  1033. (ff-get-file
  1034. ada-search-directories-internal
  1035. (ada-make-filename-from-adaname (match-string 1))
  1036. ada-spec-suffixes)))
  1037. ;; A "with" clause.
  1038. (cons "^with[ \t]+\\([a-zA-Z0-9_\\.]+\\)"
  1039. (lambda ()
  1040. (ff-get-file
  1041. ada-search-directories-internal
  1042. (ada-make-filename-from-adaname (match-string 1))
  1043. ada-spec-suffixes)))
  1044. ))
  1045. ;; Support for outline-minor-mode
  1046. (set (make-local-variable 'outline-regexp)
  1047. "\\([ \t]*\\(procedure\\|function\\|package\\|if\\|while\\|for\\|declare\\|case\\|end\\|begin\\|loop\\)\\|--\\)")
  1048. (set (make-local-variable 'outline-level) 'ada-outline-level)
  1049. ;; Support for imenu : We want a sorted index
  1050. (setq imenu-generic-expression ada-imenu-generic-expression)
  1051. (setq imenu-sort-function 'imenu--sort-by-name)
  1052. ;; Support for ispell : Check only comments
  1053. (set (make-local-variable 'ispell-check-comments) 'exclusive)
  1054. ;; Support for align
  1055. (add-to-list 'align-dq-string-modes 'ada-mode)
  1056. (add-to-list 'align-open-comment-modes 'ada-mode)
  1057. (set (make-local-variable 'align-region-separate) ada-align-region-separate)
  1058. ;; Exclude comments alone on line from alignment.
  1059. (add-to-list 'align-exclude-rules-list
  1060. '(ada-solo-comment
  1061. (regexp . "^\\(\\s-*\\)--")
  1062. (modes . '(ada-mode))))
  1063. (add-to-list 'align-exclude-rules-list
  1064. '(ada-solo-use
  1065. (regexp . "^\\(\\s-*\\)\\<use\\>")
  1066. (modes . '(ada-mode))))
  1067. (setq ada-align-modes nil)
  1068. (add-to-list 'ada-align-modes
  1069. '(ada-declaration-assign
  1070. (regexp . "[^:]\\(\\s-*\\):[^:]")
  1071. (valid . (lambda() (not (ada-in-comment-p))))
  1072. (repeat . t)
  1073. (modes . '(ada-mode))))
  1074. (add-to-list 'ada-align-modes
  1075. '(ada-associate
  1076. (regexp . "[^=]\\(\\s-*\\)=>")
  1077. (valid . (lambda() (not (ada-in-comment-p))))
  1078. (modes . '(ada-mode))))
  1079. (add-to-list 'ada-align-modes
  1080. '(ada-comment
  1081. (regexp . "\\(\\s-*\\)--")
  1082. (modes . '(ada-mode))))
  1083. (add-to-list 'ada-align-modes
  1084. '(ada-use
  1085. (regexp . "\\(\\s-*\\)\\<use\\s-")
  1086. (valid . (lambda() (not (ada-in-comment-p))))
  1087. (modes . '(ada-mode))))
  1088. (add-to-list 'ada-align-modes
  1089. '(ada-at
  1090. (regexp . "\\(\\s-+\\)at\\>")
  1091. (modes . '(ada-mode))))
  1092. (setq align-mode-rules-list ada-align-modes)
  1093. ;; Set up the contextual menu
  1094. (if ada-popup-key
  1095. (define-key ada-mode-map ada-popup-key 'ada-popup-menu))
  1096. ;; Support for Abbreviations (the user still need to "M-x abbrev-mode"
  1097. (setq local-abbrev-table ada-mode-abbrev-table)
  1098. ;; Support for which-function mode
  1099. (set (make-local-variable 'which-func-functions) '(ada-which-function))
  1100. ;; Support for indent-new-comment-line (Especially for XEmacs)
  1101. (set (make-local-variable 'comment-multi-line) nil)
  1102. ;; Support for add-log
  1103. (set (make-local-variable 'add-log-current-defun-function)
  1104. 'ada-which-function)
  1105. (easy-menu-add ada-mode-menu ada-mode-map)
  1106. (set (make-local-variable 'skeleton-further-elements)
  1107. '((< '(backward-delete-char-untabify
  1108. (min ada-indent (current-column))))))
  1109. (add-hook 'skeleton-end-hook 'ada-adjust-case-skeleton nil t)
  1110. ;; To be run after the hook, in case the user modified
  1111. ;; ada-fill-comment-prefix
  1112. (add-hook 'hack-local-variables-hook
  1113. (lambda ()
  1114. (set (make-local-variable 'comment-start)
  1115. (or ada-fill-comment-prefix "-- "))
  1116. ;; Run this after the hook to give the users a chance
  1117. ;; to activate font-lock-mode.
  1118. (unless (or (eval-when-compile (fboundp 'syntax-propertize-via-font-lock))
  1119. (featurep 'xemacs))
  1120. (ada-initialize-syntax-table-properties)
  1121. (add-hook 'font-lock-mode-hook
  1122. 'ada-handle-syntax-table-properties nil t))
  1123. ;; FIXME: ada-language-version might be set in the mode
  1124. ;; hook or it might even be set later on via file-local
  1125. ;; vars, so ada-keywords should be set lazily.
  1126. (cond ((eq ada-language-version 'ada83)
  1127. (setq ada-keywords ada-83-keywords))
  1128. ((eq ada-language-version 'ada95)
  1129. (setq ada-keywords ada-95-keywords))
  1130. ((eq ada-language-version 'ada2005)
  1131. (setq ada-keywords ada-2005-keywords)))
  1132. (if ada-auto-case
  1133. (ada-activate-keys-for-case)))
  1134. nil 'local))
  1135. (defun ada-adjust-case-skeleton ()
  1136. "Adjust the case of the text inserted by a skeleton."
  1137. (save-excursion
  1138. (let ((aa-end (point)))
  1139. (ada-adjust-case-region
  1140. (progn (goto-char (symbol-value 'beg)) (forward-word -1) (point))
  1141. (goto-char aa-end)))))
  1142. (defun ada-region-selected ()
  1143. "Should we operate on an active region?"
  1144. (if (fboundp 'use-region-p)
  1145. (use-region-p)
  1146. (region-active-p)))
  1147. ;;-----------------------------------------------------------------
  1148. ;; auto-casing
  1149. ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
  1150. ;; functions to auto-case identifiers, keywords, ...
  1151. ;; The basic rules for autocasing are defined through the variables
  1152. ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
  1153. ;; are references to the functions that will do the actual casing.
  1154. ;;
  1155. ;; However, in most cases, the user will want to define some exceptions to
  1156. ;; these casing rules. This is done through a list of files, that contain
  1157. ;; one word per line. These files are stored in `ada-case-exception-file'.
  1158. ;; For backward compatibility, this variable can also be a string.
  1159. ;;-----------------------------------------------------------------
  1160. (defun ada-save-exceptions-to-file (file-name)
  1161. "Save the casing exception lists to the file FILE-NAME.
  1162. Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
  1163. (find-file (expand-file-name file-name))
  1164. (erase-buffer)
  1165. (mapc (lambda (x) (insert (car x) "\n"))
  1166. (sort (copy-sequence ada-case-exception)
  1167. (lambda(a b) (string< (car a) (car b)))))
  1168. (mapc (lambda (x) (insert "*" (car x) "\n"))
  1169. (sort (copy-sequence ada-case-exception-substring)
  1170. (lambda(a b) (string< (car a) (car b)))))
  1171. (save-buffer)
  1172. (kill-buffer nil)
  1173. )
  1174. (defun ada-create-case-exception (&optional word)
  1175. "Define WORD as an exception for the casing system.
  1176. If WORD is not given, then the current word in the buffer is used instead.
  1177. The new word is added to the first file in `ada-case-exception-file'.
  1178. The standard casing rules will no longer apply to this word."
  1179. (interactive)
  1180. (let ((file-name
  1181. (cond ((stringp ada-case-exception-file)
  1182. ada-case-exception-file)
  1183. ((listp ada-case-exception-file)
  1184. (car ada-case-exception-file))
  1185. (t
  1186. (error (concat "No exception file specified. "
  1187. "See variable ada-case-exception-file"))))))
  1188. (unless word
  1189. (with-syntax-table ada-mode-symbol-syntax-table
  1190. (save-excursion
  1191. (skip-syntax-backward "w")
  1192. (setq word (buffer-substring-no-properties
  1193. (point) (save-excursion (forward-word 1) (point)))))))
  1194. ;; Reread the exceptions file, in case it was modified by some other,
  1195. (ada-case-read-exceptions-from-file file-name)
  1196. ;; If the word is already in the list, even with a different casing
  1197. ;; we simply want to replace it.
  1198. (if (and (not (equal ada-case-exception '()))
  1199. (assoc-string word ada-case-exception t))
  1200. (setcar (assoc-string word ada-case-exception t) word)
  1201. (add-to-list 'ada-case-exception (cons word t)))
  1202. (ada-save-exceptions-to-file file-name)))
  1203. (defun ada-create-case-exception-substring (&optional word)
  1204. "Define the substring WORD as an exception for the casing system.
  1205. If WORD is not given, then the current word in the buffer is used instead,
  1206. or the selected region if any is active.
  1207. The new word is added to the first file in `ada-case-exception-file'.
  1208. When auto-casing a word, this substring will be special-cased, unless the
  1209. word itself has a special casing."
  1210. (interactive)
  1211. (let ((file-name
  1212. (cond ((stringp ada-case-exception-file)
  1213. ada-case-exception-file)
  1214. ((listp ada-case-exception-file)
  1215. (car ada-case-exception-file))
  1216. (t
  1217. (error (concat "No exception file specified. "
  1218. "See variable ada-case-exception-file"))))))
  1219. ;; Find the substring to define as an exception. Order is: the parameter,
  1220. ;; if any, or the selected region, or the word under the cursor
  1221. (cond
  1222. (word nil)
  1223. ((ada-region-selected)
  1224. (setq word (buffer-substring-no-properties
  1225. (region-beginning) (region-end))))
  1226. (t
  1227. (let ((underscore-syntax (char-syntax ?_)))
  1228. (unwind-protect
  1229. (progn
  1230. (modify-syntax-entry ?_ "." (syntax-table))
  1231. (save-excursion
  1232. (skip-syntax-backward "w")
  1233. (setq word (buffer-substring-no-properties
  1234. (point)
  1235. (save-excursion (forward-word 1) (point))))))
  1236. (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
  1237. (syntax-table))))))
  1238. ;; Reread the exceptions file, in case it was modified by some other,
  1239. (ada-case-read-exceptions-from-file file-name)
  1240. ;; If the word is already in the list, even with a different casing
  1241. ;; we simply want to replace it.
  1242. (if (and (not (equal ada-case-exception-substring '()))
  1243. (assoc-string word ada-case-exception-substring t))
  1244. (setcar (assoc-string word ada-case-exception-substring t) word)
  1245. (add-to-list 'ada-case-exception-substring (cons word t))
  1246. )
  1247. (ada-save-exceptions-to-file file-name)
  1248. (message "%s" (concat "Defining " word " as a casing exception"))))
  1249. (defun ada-case-read-exceptions-from-file (file-name)
  1250. "Read the content of the casing exception file FILE-NAME."
  1251. (if (file-readable-p (expand-file-name file-name))
  1252. (let ((buffer (current-buffer)))
  1253. (find-file (expand-file-name file-name))
  1254. (set-syntax-table ada-mode-symbol-syntax-table)
  1255. (widen)
  1256. (goto-char (point-min))
  1257. (while (not (eobp))
  1258. ;; If the item is already in the list, even with an other casing,
  1259. ;; do not add it again. This way, the user can easily decide which
  1260. ;; priority should be applied to each casing exception
  1261. (let ((word (buffer-substring-no-properties
  1262. (point) (save-excursion (forward-word 1) (point)))))
  1263. ;; Handling a substring ?
  1264. (if (char-equal (string-to-char word) ?*)
  1265. (progn
  1266. (setq word (substring word 1))
  1267. (unless (assoc-string word ada-case-exception-substring t)
  1268. (add-to-list 'ada-case-exception-substring (cons word t))))
  1269. (unless (assoc-string word ada-case-exception t)
  1270. (add-to-list 'ada-case-exception (cons word t)))))
  1271. (forward-line 1))
  1272. (kill-buffer nil)
  1273. (set-buffer buffer)))
  1274. )
  1275. (defun ada-case-read-exceptions ()
  1276. "Read all the casing exception files from `ada-case-exception-file'."
  1277. (interactive)
  1278. ;; Reinitialize the casing exception list
  1279. (setq ada-case-exception '()
  1280. ada-case-exception-substring '())
  1281. (cond ((stringp ada-case-exception-file)
  1282. (ada-case-read-exceptions-from-file ada-case-exception-file))
  1283. ((listp ada-case-exception-file)
  1284. (mapcar 'ada-case-read-exceptions-from-file
  1285. ada-case-exception-file))))
  1286. (defun ada-adjust-case-substring ()
  1287. "Adjust case of substrings in the previous word."
  1288. (interactive)
  1289. (let ((substrings ada-case-exception-substring)
  1290. (max (point))
  1291. (case-fold-search t)
  1292. (underscore-syntax (char-syntax ?_))
  1293. re)
  1294. (save-excursion
  1295. (forward-word -1)
  1296. (unwind-protect
  1297. (progn
  1298. (modify-syntax-entry ?_ "." (syntax-table))
  1299. (while substrings
  1300. (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
  1301. (save-excursion
  1302. (while (re-search-forward re max t)
  1303. (replace-match (caar substrings) t)))
  1304. (setq substrings (cdr substrings))
  1305. )
  1306. )
  1307. (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
  1308. )))
  1309. (defun ada-adjust-case-identifier ()
  1310. "Adjust case of the previous identifier.
  1311. The auto-casing is done according to the value of `ada-case-identifier'
  1312. and the exceptions defined in `ada-case-exception-file'."
  1313. (interactive)
  1314. (if (or (equal ada-case-exception '())
  1315. (equal (char-after) ?_))
  1316. (progn
  1317. (funcall ada-case-identifier -1)
  1318. (ada-adjust-case-substring))
  1319. (progn
  1320. (let ((end (point))
  1321. (start (save-excursion (skip-syntax-backward "w")
  1322. (point)))
  1323. match)
  1324. ;; If we have an exception, replace the word by the correct casing
  1325. (if (setq match (assoc-string (buffer-substring start end)
  1326. ada-case-exception t))
  1327. (progn
  1328. (delete-region start end)
  1329. (insert (car match)))
  1330. ;; Else simply re-case the word
  1331. (funcall ada-case-identifier -1)
  1332. (ada-adjust-case-substring))))))
  1333. (defun ada-after-keyword-p ()
  1334. "Return t if cursor is after a keyword that is not an attribute."
  1335. (save-excursion
  1336. (forward-word -1)
  1337. (and (not (and (char-before)
  1338. (or (= (char-before) ?_)
  1339. (= (char-before) ?'))));; unless we have a _ or '
  1340. (looking-at (concat ada-keywords "[^_]")))))
  1341. (defun ada-adjust-case (&optional force-identifier)
  1342. "Adjust the case of the word before the character just typed.
  1343. If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
  1344. (if (not (bobp))
  1345. (progn
  1346. (forward-char -1)
  1347. (if (and (not (bobp))
  1348. ;; or if at the end of a character constant
  1349. (not (and (eq (following-char) ?')
  1350. (eq (char-before (1- (point))) ?')))
  1351. ;; or if the previous character was not part of a word
  1352. (eq (char-syntax (char-before)) ?w)
  1353. ;; if in a string or a comment
  1354. (not (ada-in-string-or-comment-p))
  1355. ;; if in a numeric literal
  1356. (not (ada-in-numeric-literal-p))
  1357. )
  1358. (if (save-excursion
  1359. (forward-word -1)
  1360. (or (= (point) (point-min))
  1361. (backward-char 1))
  1362. (= (following-char) ?'))
  1363. (funcall ada-case-attribute -1)
  1364. (if (and
  1365. (not force-identifier) ; (MH)
  1366. (ada-after-keyword-p))
  1367. (funcall ada-case-keyword -1)
  1368. (ada-adjust-case-identifier))))
  1369. (forward-char 1)
  1370. ))
  1371. )
  1372. (defun ada-adjust-case-interactive (arg)
  1373. "Adjust the case of the previous word, and process the character just typed.
  1374. ARG is the prefix the user entered with \\[universal-argument]."
  1375. (interactive "P")
  1376. (if ada-auto-case
  1377. (let ((lastk last-command-event))
  1378. (with-syntax-table ada-mode-symbol-syntax-table
  1379. (cond ((or (eq lastk ?\n)
  1380. (eq lastk ?\r))
  1381. ;; horrible kludge
  1382. (insert " ")
  1383. (ada-adjust-case)
  1384. ;; horrible dekludge
  1385. (delete-char -1)
  1386. ;; some special keys and their bindings
  1387. (cond
  1388. ((eq lastk ?\n)
  1389. (funcall ada-lfd-binding))
  1390. ((eq lastk ?\r)
  1391. (funcall ada-ret-binding))))
  1392. ((eq lastk ?\C-i) (ada-tab))
  1393. ;; Else just insert the character
  1394. ((self-insert-command (prefix-numeric-value arg))))
  1395. ;; if there is a keyword in front of the underscore
  1396. ;; then it should be part of an identifier (MH)
  1397. (if (eq lastk ?_)
  1398. (ada-adjust-case t)
  1399. (ada-adjust-case))))
  1400. ;; Else, no auto-casing
  1401. (cond
  1402. ((eq last-command-event ?\n)
  1403. (funcall ada-lfd-binding))
  1404. ((eq last-command-event ?\r)
  1405. (funcall ada-ret-binding))
  1406. (t
  1407. (self-insert-command (prefix-numeric-value arg))))))
  1408. (defun ada-activate-keys-for-case ()
  1409. ;; FIXME: Use post-self-insert-hook instead of changing key bindings.
  1410. "Modify the key bindings for all the keys that should readjust the casing."
  1411. (interactive)
  1412. ;; Save original key-bindings to allow swapping ret/lfd
  1413. ;; when casing is activated.
  1414. ;; The 'or ...' is there to be sure that the value will not
  1415. ;; be changed again when Ada mode is called more than once
  1416. (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
  1417. (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
  1418. ;; Call case modifying function after certain keys.
  1419. (mapcar (function (lambda(key) (define-key
  1420. ada-mode-map
  1421. (char-to-string key)
  1422. 'ada-adjust-case-interactive)))
  1423. '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
  1424. ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
  1425. (defun ada-loose-case-word (&optional _arg)
  1426. "Upcase first letter and letters following `_' in the following word.
  1427. No other letter is modified.
  1428. ARG is ignored, and is there for compatibility with `capitalize-word' only."
  1429. (interactive)
  1430. (save-excursion
  1431. (let ((end (save-excursion (skip-syntax-forward "w") (point)))
  1432. (first t))
  1433. (skip-syntax-backward "w")
  1434. (while (and (or first (search-forward "_" end t))
  1435. (< (point) end))
  1436. (and first
  1437. (setq first nil))
  1438. (insert-char (upcase (following-char)) 1)
  1439. (delete-char 1)))))
  1440. (defun ada-no-auto-case (&optional _arg)
  1441. "Do nothing. ARG is ignored.
  1442. This function can be used for the auto-casing variables in Ada mode, to
  1443. adapt to unusual auto-casing schemes. Since it does nothing, you can for
  1444. instance use it for `ada-case-identifier' if you don't want any special
  1445. auto-casing for identifiers, whereas keywords have to be lower-cased.
  1446. See also `ada-auto-case' to disable auto casing altogether."
  1447. nil)
  1448. (defun ada-capitalize-word (&optional _arg)
  1449. "Upcase first letter and letters following '_', lower case other letters.
  1450. ARG is ignored, and is there for compatibility with `capitalize-word' only."
  1451. (interactive)
  1452. (let ((end (save-excursion (skip-syntax-forward "w") (point)))
  1453. (begin (save-excursion (skip-syntax-backward "w") (point))))
  1454. (modify-syntax-entry ?_ "_")
  1455. (capitalize-region begin end)
  1456. (modify-syntax-entry ?_ "w")))
  1457. (defun ada-adjust-case-region (from to)
  1458. "Adjust the case of all words in the region between FROM and TO.
  1459. Attention: This function might take very long for big regions!"
  1460. (interactive "*r")
  1461. (let ((begin nil)
  1462. (end nil)
  1463. (keywordp nil)
  1464. (attribp nil))
  1465. (message "Adjusting case ...")
  1466. (with-syntax-table ada-mode-symbol-syntax-table
  1467. (save-excursion
  1468. (goto-char to)
  1469. ;;
  1470. ;; loop: look for all identifiers, keywords, and attributes
  1471. ;;
  1472. (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
  1473. (setq end (match-end 1))
  1474. (setq attribp
  1475. (and (> (point) from)
  1476. (save-excursion
  1477. (forward-char -1)
  1478. (setq attribp (looking-at "'.[^']")))))
  1479. (or
  1480. ;; do nothing if it is a string or comment
  1481. (ada-in-string-or-comment-p)
  1482. (progn
  1483. ;;
  1484. ;; get the identifier or keyword or attribute
  1485. ;;
  1486. (setq begin (point))
  1487. (setq keywordp (looking-at ada-keywords))
  1488. (goto-char end)
  1489. ;;
  1490. ;; casing according to user-option
  1491. ;;
  1492. (if attribp
  1493. (funcall ada-case-attribute -1)
  1494. (if keywordp
  1495. (funcall ada-case-keyword -1)
  1496. (ada-adjust-case-identifier)))
  1497. (goto-char begin))))
  1498. (message "Adjusting case ... Done")))))
  1499. (defun ada-adjust-case-buffer ()
  1500. "Adjust the case of all words in the whole buffer.
  1501. ATTENTION: This function might take very long for big buffers!"
  1502. (interactive "*")
  1503. (ada-adjust-case-region (point-min) (point-max)))
  1504. ;;--------------------------------------------------------------
  1505. ;; Format Parameter Lists
  1506. ;; Some special algorithms are provided to indent the parameter lists in
  1507. ;; subprogram declarations. This is done in two steps:
  1508. ;; - First parses the parameter list. The returned list has the following
  1509. ;; format:
  1510. ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
  1511. ;; ... )
  1512. ;; This is done in `ada-scan-paramlist'.
  1513. ;; - Delete and recreate the parameter list in function
  1514. ;; `ada-insert-paramlist'.
  1515. ;; Both steps are called from `ada-format-paramlist'.
  1516. ;; Note: Comments inside the parameter list are lost.
  1517. ;; The syntax has to be correct, or the reformatting will fail.
  1518. ;;--------------------------------------------------------------
  1519. (defun ada-format-paramlist ()
  1520. "Reformat the parameter list point is in."
  1521. (interactive)
  1522. (let ((begin nil)
  1523. (end nil)
  1524. (delend nil)
  1525. (paramlist nil))
  1526. (with-syntax-table ada-mode-symbol-syntax-table
  1527. ;; check if really inside parameter list
  1528. (or (ada-in-paramlist-p)
  1529. (error "Not in parameter list"))
  1530. ;; find start of current parameter-list
  1531. (ada-search-ignore-string-comment
  1532. (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
  1533. (down-list 1)
  1534. (backward-char 1)
  1535. (setq begin (point))
  1536. ;; find end of parameter-list
  1537. (forward-sexp 1)
  1538. (setq delend (point))
  1539. (delete-char -1)
  1540. (insert "\n")
  1541. ;; find end of last parameter-declaration
  1542. (forward-comment -1000)
  1543. (setq end (point))
  1544. ;; build a list of all elements of the parameter-list
  1545. (setq paramlist (ada-scan-paramlist (1+ begin) end))
  1546. ;; delete the original parameter-list
  1547. (delete-region begin delend)
  1548. ;; insert the new parameter-list
  1549. (goto-char begin)
  1550. (ada-insert-paramlist paramlist))))
  1551. (defun ada-scan-paramlist (begin end)
  1552. "Scan the parameter list found in between BEGIN and END.
  1553. Return the equivalent internal parameter list."
  1554. (let ((paramlist (list))
  1555. (param (list))
  1556. (notend t)
  1557. (apos nil)
  1558. (epos nil)
  1559. (semipos nil)
  1560. (match-cons nil))
  1561. (goto-char begin)
  1562. ;; loop until end of last parameter
  1563. (while notend
  1564. ;; find first character of parameter-declaration
  1565. (ada-goto-next-non-ws)
  1566. (setq apos (point))
  1567. ;; find last character of parameter-declaration
  1568. (if (setq match-cons
  1569. (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
  1570. (progn
  1571. (setq epos (car match-cons))
  1572. (setq semipos (cdr match-cons)))
  1573. (setq epos end))
  1574. ;; read name(s) of parameter(s)
  1575. (goto-char apos)
  1576. (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
  1577. (setq param (list (match-string 1)))
  1578. (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
  1579. ;; look for 'in'
  1580. (setq apos (point))
  1581. (setq param
  1582. (append param
  1583. (list
  1584. (consp
  1585. (ada-search-ignore-string-comment
  1586. "in" nil epos t 'word-search-forward)))))
  1587. ;; look for 'out'
  1588. (goto-char apos)
  1589. (setq param
  1590. (append param
  1591. (list
  1592. (consp
  1593. (ada-search-ignore-string-comment
  1594. "out" nil epos t 'word-search-forward)))))
  1595. ;; look for 'access'
  1596. (goto-char apos)
  1597. (setq param
  1598. (append param
  1599. (list
  1600. (consp
  1601. (ada-search-ignore-string-comment
  1602. "access" nil epos t 'word-search-forward)))))
  1603. ;; skip 'in'/'out'/'access'
  1604. (goto-char apos)
  1605. (ada-goto-next-non-ws)
  1606. (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
  1607. (forward-word 1)
  1608. (ada-goto-next-non-ws))
  1609. ;; read type of parameter
  1610. ;; We accept spaces in the name, since some software like Rose
  1611. ;; generates something like: "A : B 'Class"
  1612. (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
  1613. (setq param
  1614. (append param
  1615. (list (match-string 0))))
  1616. ;; read default-expression, if there is one
  1617. (goto-char (setq apos (match-end 0)))
  1618. (setq param
  1619. (append param
  1620. (list
  1621. (if (setq match-cons
  1622. (ada-search-ignore-string-comment
  1623. ":=" nil epos t 'search-forward))
  1624. (buffer-substring (car match-cons) epos)
  1625. nil))))
  1626. ;; add this parameter-declaration to the list
  1627. (setq paramlist (append paramlist (list param)))
  1628. ;; check if it was the last parameter
  1629. (if (eq epos end)
  1630. (setq notend nil)
  1631. (goto-char semipos))
  1632. )
  1633. (reverse paramlist)))
  1634. (defun ada-insert-paramlist (paramlist)
  1635. "Insert a formatted PARAMLIST in the buffer."
  1636. (let ((i (length paramlist))
  1637. (parlen 0)
  1638. (typlen 0)
  1639. (inp nil)
  1640. (outp nil)
  1641. (accessp nil)
  1642. (column nil)
  1643. (firstcol nil))
  1644. ;; loop until last parameter
  1645. (while (not (zerop i))
  1646. (setq i (1- i))
  1647. ;; get max length of parameter-name
  1648. (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
  1649. ;; get max length of type-name
  1650. (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
  1651. ;; is there any 'in' ?
  1652. (setq inp (or inp (nth 1 (nth i paramlist))))
  1653. ;; is there any 'out' ?
  1654. (setq outp (or outp (nth 2 (nth i paramlist))))
  1655. ;; is there any 'access' ?
  1656. (setq accessp (or accessp (nth 3 (nth i paramlist))))
  1657. )
  1658. ;; does paramlist already start on a separate line ?
  1659. (if (save-excursion
  1660. (re-search-backward "^.\\|[^ \t]" nil t)
  1661. (looking-at "^."))
  1662. ;; yes => re-indent it
  1663. (progn
  1664. (ada-indent-current)
  1665. (save-excursion
  1666. (if (looking-at "\\(is\\|return\\)")
  1667. (replace-match " \\1"))))
  1668. ;; no => insert it where we are after removing any whitespace
  1669. (fixup-whitespace)
  1670. (save-excursion
  1671. (cond
  1672. ((looking-at "[ \t]*\\(\n\\|;\\)")
  1673. (replace-match "\\1"))
  1674. ((looking-at "[ \t]*\\(is\\|return\\)")
  1675. (replace-match " \\1"))))
  1676. (insert " "))
  1677. (insert "(")
  1678. (ada-indent-current)
  1679. (setq firstcol (current-column))
  1680. (setq i (length paramlist))
  1681. ;; loop until last parameter
  1682. (while (not (zerop i))
  1683. (setq i (1- i))
  1684. (setq column firstcol)
  1685. ;; insert parameter-name, space and colon
  1686. (insert (nth 0 (nth i paramlist)))
  1687. (indent-to (+ column parlen 1))
  1688. (insert ": ")
  1689. (setq column (current-column))
  1690. ;; insert 'in' or space
  1691. (if (nth 1 (nth i paramlist))
  1692. (insert "in ")
  1693. (if (and
  1694. (or inp
  1695. accessp)
  1696. (not (nth 3 (nth i paramlist))))
  1697. (insert " ")))
  1698. ;; insert 'out' or space
  1699. (if (nth 2 (nth i paramlist))
  1700. (insert "out ")
  1701. (if (and
  1702. (or outp
  1703. accessp)
  1704. (not (nth 3 (nth i paramlist))))
  1705. (insert " ")))
  1706. ;; insert 'access'
  1707. (if (nth 3 (nth i paramlist))
  1708. (insert "access "))
  1709. (setq column (current-column))
  1710. ;; insert type-name and, if necessary, space and default-expression
  1711. (insert (nth 4 (nth i paramlist)))
  1712. (if (nth 5 (nth i paramlist))
  1713. (progn
  1714. (indent-to (+ column typlen 1))
  1715. (insert (nth 5 (nth i paramlist)))))
  1716. ;; check if it was the last parameter
  1717. (if (zerop i)
  1718. (insert ")")
  1719. ;; no => insert ';' and newline and indent
  1720. (insert ";")
  1721. (newline)
  1722. (indent-to firstcol))
  1723. )
  1724. ;; if anything follows, except semicolon, newline, is or return
  1725. ;; put it in a new line and indent it
  1726. (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
  1727. (ada-indent-newline-indent))
  1728. ))
  1729. ;;;----------------------------------------------------------------
  1730. ;; Indentation Engine
  1731. ;; All indentations are indicated as a two-element string:
  1732. ;; - position of reference in the buffer
  1733. ;; - offset to indent from this position (can also be a symbol or a list
  1734. ;; that are evaluated)
  1735. ;; Thus the total indentation for a line is the column number of the reference
  1736. ;; position plus whatever value the evaluation of the second element provides.
  1737. ;; This mechanism is used so that the Ada mode can "explain" how the
  1738. ;; indentation was calculated, by showing which variables were used.
  1739. ;;
  1740. ;; The indentation itself is done in only one pass: first we try to guess in
  1741. ;; what context we are by looking at the following keyword or punctuation
  1742. ;; sign. If nothing remarkable is found, just try to guess the indentation
  1743. ;; based on previous lines.
  1744. ;;
  1745. ;; The relevant functions for indentation are:
  1746. ;; - `ada-indent-region': Re-indent a region of text
  1747. ;; - `ada-justified-indent-current': Re-indent the current line and shows the
  1748. ;; calculation that were done
  1749. ;; - `ada-indent-current': Re-indent the current line
  1750. ;; - `ada-get-current-indent': Calculate the indentation for the current line,
  1751. ;; based on the context (see above).
  1752. ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
  1753. ;; For efficiency, these functions do not check they are in the correct
  1754. ;; context.
  1755. ;;;----------------------------------------------------------------
  1756. (defun ada-indent-region (beg end)
  1757. "Indent the region between BEG end END."
  1758. (interactive "*r")
  1759. (goto-char beg)
  1760. (let ((block-done 0)
  1761. (lines-remaining (count-lines beg end))
  1762. (msg (format "%%4d out of %4d lines remaining ..."
  1763. (count-lines beg end)))
  1764. (endmark (copy-marker end)))
  1765. ;; catch errors while indenting
  1766. (while (< (point) endmark)
  1767. (if (> block-done 39)
  1768. (progn
  1769. (setq lines-remaining (- lines-remaining block-done)
  1770. block-done 0)
  1771. (message msg lines-remaining)))
  1772. (if (= (char-after) ?\n) nil
  1773. (ada-indent-current))
  1774. (forward-line 1)
  1775. (setq block-done (1+ block-done)))
  1776. (message "Indenting ... done")))
  1777. (defun ada-indent-newline-indent ()
  1778. "Indent the current line, insert a newline and then indent the new line."
  1779. (interactive "*")
  1780. (ada-indent-current)
  1781. (newline)
  1782. (ada-indent-current))
  1783. (defun ada-indent-newline-indent-conditional ()
  1784. "Insert a newline and indent it.
  1785. The original line is re-indented if `ada-indent-after-return' is non-nil."
  1786. (interactive "*")
  1787. ;; If at end of buffer (entering brand new code), some indentation
  1788. ;; fails. For example, a block label requires whitespace following
  1789. ;; the : to be recognized. So we do the newline first, then
  1790. ;; go back and indent the original line.
  1791. (newline)
  1792. (if ada-indent-after-return
  1793. (progn
  1794. (forward-char -1)
  1795. (ada-indent-current)
  1796. (forward-char 1)))
  1797. (ada-indent-current))
  1798. (defun ada-justified-indent-current ()
  1799. "Indent the current line and explain how the calculation was done."
  1800. (interactive)
  1801. (let ((cur-indent (ada-indent-current)))
  1802. (let ((line (save-excursion
  1803. (goto-char (car cur-indent))
  1804. (count-lines 1 (point)))))
  1805. (if (equal (cdr cur-indent) '(0))
  1806. (message (concat "same indentation as line " (number-to-string line)))
  1807. (message "%s" (mapconcat (lambda(x)
  1808. (cond
  1809. ((symbolp x)
  1810. (symbol-name x))
  1811. ((numberp x)
  1812. (number-to-string x))
  1813. ((listp x)
  1814. (concat "- " (symbol-name (cadr x))))
  1815. ))
  1816. (cdr cur-indent)
  1817. " + "))))
  1818. (save-excursion
  1819. (goto-char (car cur-indent))
  1820. (sit-for 1))))
  1821. (defun ada-batch-reformat ()
  1822. "Re-indent and re-case all the files found on the command line.
  1823. This function should be used from the command line, with a
  1824. command like:
  1825. emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
  1826. (while command-line-args-left
  1827. (let ((source (car command-line-args-left)))
  1828. (message "Formatting %s" source)
  1829. (find-file source)
  1830. (ada-indent-region (point-min) (point-max))
  1831. (ada-adjust-case-buffer)
  1832. (write-file source))
  1833. (setq command-line-args-left (cdr command-line-args-left)))
  1834. (message "Done")
  1835. (kill-emacs 0))
  1836. (defsubst ada-goto-previous-word ()
  1837. "Move point to the beginning of the previous word of Ada code.
  1838. Return the new position of point or nil if not found."
  1839. (ada-goto-next-word t))
  1840. (defun ada-indent-current ()
  1841. "Indent current line as Ada code.
  1842. Return the calculation that was done, including the reference point
  1843. and the offset."
  1844. (interactive)
  1845. (let ((orgpoint (point-marker))
  1846. cur-indent tmp-indent
  1847. prev-indent)
  1848. (unwind-protect
  1849. (with-syntax-table ada-mode-symbol-syntax-table
  1850. ;; This need to be done here so that the advice is not always
  1851. ;; activated (this might interact badly with other modes)
  1852. (if (featurep 'xemacs)
  1853. (ad-activate 'parse-partial-sexp t))
  1854. (save-excursion
  1855. (setq cur-indent
  1856. ;; Not First line in the buffer ?
  1857. (if (save-excursion (zerop (forward-line -1)))
  1858. (progn
  1859. (back-to-indentation)
  1860. (ada-get-current-indent))
  1861. ;; first line in the buffer
  1862. (list (point-min) 0))))
  1863. ;; Evaluate the list to get the column to indent to
  1864. ;; prev-indent contains the column to indent to
  1865. (if cur-indent
  1866. (setq prev-indent (save-excursion (goto-char (car cur-indent))
  1867. (current-column))
  1868. tmp-indent (cdr cur-indent))
  1869. (setq prev-indent 0 tmp-indent '()))
  1870. (while (not (null tmp-indent))
  1871. (cond
  1872. ((numberp (car tmp-indent))
  1873. (setq prev-indent (+ prev-indent (car tmp-indent))))
  1874. (t
  1875. (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
  1876. )
  1877. (setq tmp-indent (cdr tmp-indent)))
  1878. ;; only re-indent if indentation is different then the current
  1879. (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
  1880. nil
  1881. (beginning-of-line)
  1882. (delete-horizontal-space)
  1883. (indent-to prev-indent))
  1884. ;;
  1885. ;; restore position of point
  1886. ;;
  1887. (goto-char orgpoint)
  1888. (if (< (current-column) (current-indentation))
  1889. (back-to-indentation)))
  1890. (if (featurep 'xemacs)
  1891. (ad-deactivate 'parse-partial-sexp)))
  1892. cur-indent))
  1893. (defun ada-get-current-indent ()
  1894. "Return the indentation to use for the current line."
  1895. (let (column
  1896. pos
  1897. match-cons
  1898. result
  1899. (orgpoint (save-excursion
  1900. (beginning-of-line)
  1901. (forward-comment -10000)
  1902. (forward-line 1)
  1903. (point))))
  1904. (setq result
  1905. (cond
  1906. ;;-----------------------------
  1907. ;; in open parenthesis, but not in parameter-list
  1908. ;;-----------------------------
  1909. ((and ada-indent-to-open-paren
  1910. (not (ada-in-paramlist-p))
  1911. (setq column (ada-in-open-paren-p)))
  1912. ;; check if we have something like this (Table_Component_Type =>
  1913. ;; Source_File_Record)
  1914. (save-excursion
  1915. ;; Align the closing parenthesis on the opening one
  1916. (if (= (following-char) ?\))
  1917. (save-excursion
  1918. (goto-char column)
  1919. (skip-chars-backward " \t")
  1920. (list (1- (point)) 0))
  1921. (if (and (skip-chars-backward " \t")
  1922. (= (char-before) ?\n)
  1923. (not (forward-comment -10000))
  1924. (= (char-before) ?>))
  1925. ;; ??? Could use a different variable
  1926. (list column 'ada-broken-indent)
  1927. ;; We want all continuation lines to be indented the same
  1928. ;; (ada-broken-line from the opening parenthesis. However, in
  1929. ;; parameter list, each new parameter should be indented at the
  1930. ;; column as the opening parenthesis.
  1931. ;; A special case to handle nested boolean expressions, as in
  1932. ;; ((B
  1933. ;; and then C) -- indented by ada-broken-indent
  1934. ;; or else D) -- indenting this line.
  1935. ;; ??? This is really a hack, we should have a proper way to go to
  1936. ;; ??? the beginning of the statement
  1937. (if (= (char-before) ?\))
  1938. (backward-sexp))
  1939. (if (memq (char-before) '(?, ?\; ?\( ?\)))
  1940. (list column 0)
  1941. (list column 'ada-continuation-indent)
  1942. )))))
  1943. ;;---------------------------
  1944. ;; at end of buffer
  1945. ;;---------------------------
  1946. ((not (char-after))
  1947. (ada-indent-on-previous-lines nil orgpoint orgpoint))
  1948. ;;---------------------------
  1949. ;; starting with e
  1950. ;;---------------------------
  1951. ((= (downcase (char-after)) ?e)
  1952. (cond
  1953. ;; ------- end ------
  1954. ((looking-at "end\\>")
  1955. (let ((label 0)
  1956. limit)
  1957. (save-excursion
  1958. (ada-goto-matching-start 1)
  1959. ;;
  1960. ;; found 'loop' => skip back to 'while' or 'for'
  1961. ;; if 'loop' is not on a separate line
  1962. ;; Stop the search for 'while' and 'for' when a ';' is encountered.
  1963. ;;
  1964. (if (save-excursion
  1965. (beginning-of-line)
  1966. (looking-at ".+\\<loop\\>"))
  1967. (progn
  1968. (save-excursion
  1969. (setq limit (car (ada-search-ignore-string-comment ";" t))))
  1970. (if (save-excursion
  1971. (and
  1972. (setq match-cons
  1973. (ada-search-ignore-string-comment ada-loop-start-re t limit))
  1974. (not (looking-at "\\<loop\\>"))))
  1975. (progn
  1976. (goto-char (car match-cons))
  1977. (save-excursion
  1978. (back-to-indentation)
  1979. (if (looking-at ada-block-label-re)
  1980. (setq label (- ada-label-indent))))))))
  1981. ;; found 'record' =>
  1982. ;; if the keyword is found at the beginning of a line (or just
  1983. ;; after limited, we indent on it, otherwise we indent on the
  1984. ;; beginning of the type declaration)
  1985. ;; type A is (B : Integer;
  1986. ;; C : Integer) is record
  1987. ;; end record; -- This is badly indented otherwise
  1988. (if (looking-at "record")
  1989. (if (save-excursion
  1990. (beginning-of-line)
  1991. (looking-at "^[ \t]*\\(record\\|limited record\\)"))
  1992. (list (save-excursion (back-to-indentation) (point)) 0)
  1993. (list (save-excursion
  1994. (car (ada-search-ignore-string-comment "\\<type\\>" t)))
  1995. 0))
  1996. ;; Else keep the same indentation as the beginning statement
  1997. (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
  1998. ;; ------ exception ----
  1999. ((looking-at "exception\\>")
  2000. (save-excursion
  2001. (ada-goto-matching-start 1)
  2002. (list (save-excursion (back-to-indentation) (point)) 0)))
  2003. ;; else
  2004. ((looking-at "else\\>")
  2005. (if (save-excursion (ada-goto-previous-word)
  2006. (looking-at "\\<or\\>"))
  2007. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2008. (save-excursion
  2009. (ada-goto-matching-start 1 nil t)
  2010. (list (progn (back-to-indentation) (point)) 0))))
  2011. ;; elsif
  2012. ((looking-at "elsif\\>")
  2013. (save-excursion
  2014. (ada-goto-matching-start 1 nil t)
  2015. (list (progn (back-to-indentation) (point)) 0)))
  2016. ))
  2017. ;;---------------------------
  2018. ;; starting with w (when)
  2019. ;;---------------------------
  2020. ((and (= (downcase (char-after)) ?w)
  2021. (looking-at "when\\>"))
  2022. (save-excursion
  2023. (ada-goto-matching-start 1)
  2024. (list (save-excursion (back-to-indentation) (point))
  2025. 'ada-when-indent)))
  2026. ;;---------------------------
  2027. ;; starting with t (then)
  2028. ;;---------------------------
  2029. ((and (= (downcase (char-after)) ?t)
  2030. (looking-at "then\\>"))
  2031. (if (save-excursion (ada-goto-previous-word)
  2032. (looking-at "and\\>"))
  2033. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2034. (save-excursion
  2035. ;; Select has been added for the statement: "select ... then abort"
  2036. (ada-search-ignore-string-comment
  2037. "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
  2038. (list (progn (back-to-indentation) (point))
  2039. 'ada-stmt-end-indent))))
  2040. ;;---------------------------
  2041. ;; starting with l (loop)
  2042. ;;---------------------------
  2043. ((and (= (downcase (char-after)) ?l)
  2044. (looking-at "loop\\>"))
  2045. (setq pos (point))
  2046. (save-excursion
  2047. (goto-char (match-end 0))
  2048. (ada-goto-stmt-start)
  2049. (if (looking-at "\\<\\(loop\\|if\\)\\>")
  2050. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2051. (unless (looking-at ada-loop-start-re)
  2052. (ada-search-ignore-string-comment ada-loop-start-re
  2053. nil pos))
  2054. (if (looking-at "\\<loop\\>")
  2055. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2056. (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
  2057. ;;----------------------------
  2058. ;; starting with l (limited) or r (record)
  2059. ;;----------------------------
  2060. ((or (and (= (downcase (char-after)) ?l)
  2061. (looking-at "limited\\>"))
  2062. (and (= (downcase (char-after)) ?r)
  2063. (looking-at "record\\>")))
  2064. (save-excursion
  2065. (ada-search-ignore-string-comment
  2066. "\\<\\(type\\|use\\)\\>" t nil)
  2067. (if (looking-at "\\<use\\>")
  2068. (ada-search-ignore-string-comment "for" t nil nil
  2069. 'word-search-backward))
  2070. (list (progn (back-to-indentation) (point))
  2071. 'ada-indent-record-rel-type)))
  2072. ;;---------------------------
  2073. ;; starting with b (begin)
  2074. ;;---------------------------
  2075. ((and (= (downcase (char-after)) ?b)
  2076. (looking-at "begin\\>"))
  2077. (save-excursion
  2078. (if (ada-goto-decl-start t)
  2079. (list (progn (back-to-indentation) (point)) 0)
  2080. (ada-indent-on-previous-lines nil orgpoint orgpoint))))
  2081. ;;---------------------------
  2082. ;; starting with i (is)
  2083. ;;---------------------------
  2084. ((and (= (downcase (char-after)) ?i)
  2085. (looking-at "is\\>"))
  2086. (if (and ada-indent-is-separate
  2087. (save-excursion
  2088. (goto-char (match-end 0))
  2089. (ada-goto-next-non-ws (point-at-eol))
  2090. (looking-at "\\<abstract\\>\\|\\<separate\\>")))
  2091. (save-excursion
  2092. (ada-goto-stmt-start)
  2093. (list (progn (back-to-indentation) (point)) 'ada-indent))
  2094. (save-excursion
  2095. (ada-goto-stmt-start)
  2096. (if (looking-at "\\<overriding\\|package\\|procedure\\|function\\>")
  2097. (list (progn (back-to-indentation) (point)) 0)
  2098. (list (progn (back-to-indentation) (point)) 'ada-indent)))))
  2099. ;;---------------------------
  2100. ;; starting with r (return, renames)
  2101. ;;---------------------------
  2102. ((and (= (downcase (char-after)) ?r)
  2103. (looking-at "re\\(turn\\|names\\)\\>"))
  2104. (save-excursion
  2105. (let ((var 'ada-indent-return))
  2106. ;; If looking at a renames, skip the 'return' statement too
  2107. (if (looking-at "renames")
  2108. (let (pos)
  2109. (save-excursion
  2110. (setq pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
  2111. (if (and pos
  2112. (= (downcase (char-after (car pos))) ?r))
  2113. (goto-char (car pos)))
  2114. (setq var 'ada-indent-renames)))
  2115. (forward-comment -1000)
  2116. (if (= (char-before) ?\))
  2117. (forward-sexp -1)
  2118. (forward-word -1))
  2119. ;; If there is a parameter list, and we have a function declaration
  2120. ;; or a access to subprogram declaration
  2121. (let ((num-back 1))
  2122. (if (and (= (following-char) ?\()
  2123. (save-excursion
  2124. (or (progn
  2125. (backward-word 1)
  2126. (looking-at "\\(function\\|procedure\\)\\>"))
  2127. (progn
  2128. (backward-word 1)
  2129. (setq num-back 2)
  2130. (looking-at "\\(function\\|procedure\\)\\>")))))
  2131. ;; The indentation depends of the value of ada-indent-return
  2132. (if (<= (eval var) 0)
  2133. (list (point) (list '- var))
  2134. (list (progn (backward-word num-back) (point))
  2135. var))
  2136. ;; Else there is no parameter list, but we have a function
  2137. ;; Only do something special if the user want to indent
  2138. ;; relative to the "function" keyword
  2139. (if (and (> (eval var) 0)
  2140. (save-excursion (forward-word -1)
  2141. (looking-at "function\\>")))
  2142. (list (progn (forward-word -1) (point)) var)
  2143. ;; Else...
  2144. (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
  2145. ;;--------------------------------
  2146. ;; starting with 'o' or 'p'
  2147. ;; 'or' as statement-start
  2148. ;; 'private' as statement-start
  2149. ;;--------------------------------
  2150. ((and (or (= (downcase (char-after)) ?o)
  2151. (= (downcase (char-after)) ?p))
  2152. (or (ada-looking-at-semi-or)
  2153. (ada-looking-at-semi-private)))
  2154. (save-excursion
  2155. ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
  2156. (ada-goto-matching-start 1)
  2157. (list (progn (back-to-indentation) (point)) 0)))
  2158. ;;--------------------------------
  2159. ;; starting with 'd' (do)
  2160. ;;--------------------------------
  2161. ((and (= (downcase (char-after)) ?d)
  2162. (looking-at "do\\>"))
  2163. (save-excursion
  2164. (ada-goto-stmt-start)
  2165. (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
  2166. ;;--------------------------------
  2167. ;; starting with '-' (comment)
  2168. ;;--------------------------------
  2169. ((= (char-after) ?-)
  2170. (if ada-indent-comment-as-code
  2171. ;; Indent comments on previous line comments if required
  2172. ;; We must use a search-forward (even if the code is more complex),
  2173. ;; since we want to find the beginning of the comment.
  2174. (let (pos)
  2175. (if (and ada-indent-align-comments
  2176. (save-excursion
  2177. (forward-line -1)
  2178. (beginning-of-line)
  2179. (while (and (not pos)
  2180. (search-forward "--" (point-at-eol) t))
  2181. (unless (ada-in-string-p)
  2182. (setq pos (point))))
  2183. pos))
  2184. (list (- pos 2) 0)
  2185. ;; Else always on previous line
  2186. (ada-indent-on-previous-lines nil orgpoint orgpoint)))
  2187. ;; Else same indentation as the previous line
  2188. (list (save-excursion (back-to-indentation) (point)) 0)))
  2189. ;;--------------------------------
  2190. ;; starting with '#' (preprocessor line)
  2191. ;;--------------------------------
  2192. ((and (= (char-after) ?#)
  2193. (equal ada-which-compiler 'gnat)
  2194. (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
  2195. (list (point-at-bol) 0))
  2196. ;;--------------------------------
  2197. ;; starting with ')' (end of a parameter list)
  2198. ;;--------------------------------
  2199. ((and (not (eobp)) (= (char-after) ?\)))
  2200. (save-excursion
  2201. (forward-char 1)
  2202. (backward-sexp 1)
  2203. (list (point) 0)))
  2204. ;;---------------------------------
  2205. ;; new/abstract/separate
  2206. ;;---------------------------------
  2207. ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
  2208. (ada-indent-on-previous-lines nil orgpoint orgpoint))
  2209. ;;---------------------------------
  2210. ;; package/function/procedure
  2211. ;;---------------------------------
  2212. ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
  2213. (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
  2214. (save-excursion
  2215. ;; Go up until we find either a generic section, or the end of the
  2216. ;; previous subprogram/package, or 'overriding' for this function/procedure
  2217. (let (found)
  2218. (while (and (not found)
  2219. (ada-search-ignore-string-comment
  2220. "\\<\\(generic\\|end\\|begin\\|overriding\\|package\\|procedure\\|function\\)\\>" t))
  2221. ;; avoid "with procedure"... in generic parts
  2222. (save-excursion
  2223. (forward-word -1)
  2224. (setq found (not (looking-at "with"))))))
  2225. (cond
  2226. ((looking-at "\\<generic\\|overriding\\>")
  2227. (list (progn (back-to-indentation) (point)) 0))
  2228. (t
  2229. (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
  2230. ;;---------------------------------
  2231. ;; label
  2232. ;;---------------------------------
  2233. ((looking-at ada-label-re)
  2234. (if (ada-in-decl-p)
  2235. ;; ada-block-label-re matches variable declarations
  2236. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2237. (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2238. '(ada-label-indent))))
  2239. ))
  2240. ;;---------------------------------
  2241. ;; Other syntaxes
  2242. ;;---------------------------------
  2243. (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
  2244. (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
  2245. "Calculate the indentation for the new line after ORGPOINT.
  2246. The result list is based on the previous lines in the buffer.
  2247. If NOMOVE is nil, moves point to the beginning of the current statement.
  2248. if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
  2249. (if initial-pos
  2250. (goto-char initial-pos))
  2251. (let ((oldpoint (point)))
  2252. ;; Is inside a parameter-list ?
  2253. (if (ada-in-paramlist-p)
  2254. (ada-get-indent-paramlist)
  2255. ;; Move to beginning of current statement. If already at a
  2256. ;; statement start, move to beginning of enclosing statement.
  2257. (unless nomove
  2258. (ada-goto-stmt-start t))
  2259. ;; no beginning found => don't change indentation
  2260. (if (and (eq oldpoint (point))
  2261. (not nomove))
  2262. (ada-get-indent-nochange)
  2263. (cond
  2264. ;;
  2265. ((and
  2266. ada-indent-to-open-paren
  2267. (ada-in-open-paren-p))
  2268. (ada-get-indent-open-paren))
  2269. ;;
  2270. ((looking-at "end\\>")
  2271. (ada-get-indent-end orgpoint))
  2272. ;;
  2273. ((looking-at ada-loop-start-re)
  2274. (ada-get-indent-loop orgpoint))
  2275. ;;
  2276. ((looking-at ada-subprog-start-re)
  2277. (ada-get-indent-subprog orgpoint))
  2278. ;;
  2279. ((looking-at ada-block-start-re)
  2280. (ada-get-indent-block-start orgpoint))
  2281. ;;
  2282. ((looking-at ada-block-label-re) ; also variable declaration
  2283. (ada-get-indent-block-label orgpoint))
  2284. ;;
  2285. ((looking-at ada-goto-label-re)
  2286. (ada-get-indent-goto-label orgpoint))
  2287. ;;
  2288. ((looking-at "\\(sub\\)?type\\>")
  2289. (ada-get-indent-type orgpoint))
  2290. ;;
  2291. ;; "then" has to be included in the case of "select...then abort"
  2292. ;; statements, since (goto-stmt-start) at the beginning of
  2293. ;; the current function would leave the cursor on that position
  2294. ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\\>")
  2295. (ada-get-indent-if orgpoint))
  2296. ;;
  2297. ((looking-at "case\\>")
  2298. (ada-get-indent-case orgpoint))
  2299. ;;
  2300. ((looking-at "when\\>")
  2301. (ada-get-indent-when orgpoint))
  2302. ;;
  2303. ((looking-at "separate\\>")
  2304. (ada-get-indent-nochange))
  2305. ;;
  2306. ((looking-at "with\\>\\|use\\>")
  2307. ;; Are we still in that statement, or are we in fact looking at
  2308. ;; the previous one ?
  2309. (if (save-excursion (search-forward ";" oldpoint t))
  2310. (list (progn (back-to-indentation) (point)) 0)
  2311. (list (point) (if (looking-at "with")
  2312. 'ada-with-indent
  2313. 'ada-use-indent))))
  2314. ;;
  2315. (t
  2316. (ada-get-indent-noindent orgpoint)))))
  2317. ))
  2318. (defun ada-get-indent-open-paren ()
  2319. "Calculate the indentation when point is behind an unclosed parenthesis."
  2320. (list (ada-in-open-paren-p) 0))
  2321. (defun ada-get-indent-nochange ()
  2322. "Return the current indentation of the previous line."
  2323. (save-excursion
  2324. (forward-line -1)
  2325. (back-to-indentation)
  2326. (list (point) 0)))
  2327. (defun ada-get-indent-paramlist ()
  2328. "Calculate the indentation when point is inside a parameter list."
  2329. (save-excursion
  2330. (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
  2331. (cond
  2332. ;; in front of the first parameter
  2333. ((= (char-after) ?\()
  2334. (goto-char (match-end 0))
  2335. (list (point) 0))
  2336. ;; in front of another parameter
  2337. ((= (char-after) ?\;)
  2338. (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
  2339. (ada-goto-next-non-ws)
  2340. (list (point) 0))
  2341. ;; After an affectation (default parameter value in subprogram
  2342. ;; declaration)
  2343. ((and (= (following-char) ?=) (= (preceding-char) ?:))
  2344. (back-to-indentation)
  2345. (list (point) 'ada-broken-indent))
  2346. ;; inside a parameter declaration
  2347. (t
  2348. (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
  2349. (ada-goto-next-non-ws)
  2350. (list (point) 'ada-broken-indent)))))
  2351. (defun ada-get-indent-end (orgpoint)
  2352. "Calculate the indentation when point is just before an end statement.
  2353. ORGPOINT is the limit position used in the calculation."
  2354. (let ((defun-name nil)
  2355. (indent nil))
  2356. ;; is the line already terminated by ';' ?
  2357. (if (save-excursion
  2358. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2359. 'search-forward))
  2360. ;; yes, look what's following 'end'
  2361. (progn
  2362. (forward-word 1)
  2363. (ada-goto-next-non-ws)
  2364. (cond
  2365. ;;
  2366. ;; loop/select/if/case/return
  2367. ;;
  2368. ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
  2369. (save-excursion (ada-check-matching-start (match-string 0)))
  2370. (list (save-excursion (back-to-indentation) (point)) 0))
  2371. ;;
  2372. ;; record
  2373. ;;
  2374. ((looking-at "\\<record\\>")
  2375. (save-excursion
  2376. (ada-check-matching-start (match-string 0))
  2377. ;; we are now looking at the matching "record" statement
  2378. (forward-word 1)
  2379. (ada-goto-stmt-start)
  2380. ;; now on the matching type declaration, or use clause
  2381. (unless (looking-at "\\(for\\|type\\)\\>")
  2382. (ada-search-ignore-string-comment "\\<type\\>" t))
  2383. (list (progn (back-to-indentation) (point)) 0)))
  2384. ;;
  2385. ;; a named block end
  2386. ;;
  2387. ((looking-at ada-ident-re)
  2388. (setq defun-name (match-string 0))
  2389. (save-excursion
  2390. (ada-goto-matching-start 0)
  2391. (ada-check-defun-name defun-name))
  2392. (list (progn (back-to-indentation) (point)) 0))
  2393. ;;
  2394. ;; a block-end without name
  2395. ;;
  2396. ((= (char-after) ?\;)
  2397. (save-excursion
  2398. (ada-goto-matching-start 0)
  2399. (if (looking-at "\\<begin\\>")
  2400. (progn
  2401. (setq indent (list (point) 0))
  2402. (if (ada-goto-decl-start t)
  2403. (list (progn (back-to-indentation) (point)) 0)
  2404. indent))
  2405. (list (progn (back-to-indentation) (point)) 0)
  2406. )))
  2407. ;;
  2408. ;; anything else - should maybe signal an error ?
  2409. ;;
  2410. (t
  2411. (list (save-excursion (back-to-indentation) (point))
  2412. 'ada-broken-indent))))
  2413. (list (save-excursion (back-to-indentation) (point))
  2414. 'ada-broken-indent))))
  2415. (defun ada-get-indent-case (orgpoint)
  2416. "Calculate the indentation when point is just before a case statement.
  2417. ORGPOINT is the limit position used in the calculation."
  2418. (let ((match-cons nil)
  2419. (opos (point)))
  2420. (cond
  2421. ;;
  2422. ;; case..is..when..=>
  2423. ;;
  2424. ((save-excursion
  2425. (setq match-cons (and
  2426. ;; the `=>' must be after the keyword `is'.
  2427. (ada-search-ignore-string-comment
  2428. "is" nil orgpoint nil 'word-search-forward)
  2429. (ada-search-ignore-string-comment
  2430. "[ \t\n]+=>" nil orgpoint))))
  2431. (save-excursion
  2432. (goto-char (car match-cons))
  2433. (unless (ada-search-ignore-string-comment "when" t opos)
  2434. (error "Missing 'when' between 'case' and '=>'"))
  2435. (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
  2436. ;;
  2437. ;; case..is..when
  2438. ;;
  2439. ((save-excursion
  2440. (setq match-cons (ada-search-ignore-string-comment
  2441. "when" nil orgpoint nil 'word-search-forward)))
  2442. (goto-char (cdr match-cons))
  2443. (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
  2444. ;;
  2445. ;; case..is
  2446. ;;
  2447. ((save-excursion
  2448. (setq match-cons (ada-search-ignore-string-comment
  2449. "is" nil orgpoint nil 'word-search-forward)))
  2450. (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
  2451. ;;
  2452. ;; incomplete case
  2453. ;;
  2454. (t
  2455. (list (save-excursion (back-to-indentation) (point))
  2456. 'ada-broken-indent)))))
  2457. (defun ada-get-indent-when (orgpoint)
  2458. "Calculate the indentation when point is just before a when statement.
  2459. ORGPOINT is the limit position used in the calculation."
  2460. (let ((cur-indent (save-excursion (back-to-indentation) (point))))
  2461. (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
  2462. (list cur-indent 'ada-indent)
  2463. (list cur-indent 'ada-broken-indent))))
  2464. (defun ada-get-indent-if (orgpoint)
  2465. "Calculate the indentation when point is just before an if statement.
  2466. ORGPOINT is the limit position used in the calculation."
  2467. (let ((cur-indent (save-excursion (back-to-indentation) (point)))
  2468. (match-cons nil))
  2469. ;;
  2470. ;; Move to the correct then (ignore all "and then")
  2471. ;;
  2472. (while (and (setq match-cons (ada-search-ignore-string-comment
  2473. "\\<\\(then\\|and[ \t]*then\\)\\>"
  2474. nil orgpoint))
  2475. (= (downcase (char-after (car match-cons))) ?a)))
  2476. ;; If "then" was found (we are looking at it)
  2477. (if match-cons
  2478. (progn
  2479. ;;
  2480. ;; 'then' first in separate line ?
  2481. ;; => indent according to 'then',
  2482. ;; => else indent according to 'if'
  2483. ;;
  2484. (if (save-excursion
  2485. (back-to-indentation)
  2486. (looking-at "\\<then\\>"))
  2487. (setq cur-indent (save-excursion (back-to-indentation) (point))))
  2488. ;; skip 'then'
  2489. (forward-word 1)
  2490. (list cur-indent 'ada-indent))
  2491. (list cur-indent 'ada-broken-indent))))
  2492. (defun ada-get-indent-block-start (orgpoint)
  2493. "Calculate the indentation when point is at the start of a block.
  2494. ORGPOINT is the limit position used in the calculation."
  2495. (let ((pos nil))
  2496. (cond
  2497. ((save-excursion
  2498. (forward-word 1)
  2499. (setq pos (ada-goto-next-non-ws orgpoint)))
  2500. (goto-char pos)
  2501. (save-excursion
  2502. (ada-indent-on-previous-lines t orgpoint)))
  2503. ;; Special case for record types, for instance for:
  2504. ;; type A is (B : Integer;
  2505. ;; C : Integer) is record
  2506. ;; null; -- This is badly indented otherwise
  2507. ((looking-at "record")
  2508. ;; If record is at the beginning of the line, indent from there
  2509. (if (save-excursion
  2510. (beginning-of-line)
  2511. (looking-at "^[ \t]*\\(record\\|limited record\\)"))
  2512. (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
  2513. ;; else indent relative to the type command
  2514. (list (save-excursion
  2515. (car (ada-search-ignore-string-comment "\\<type\\>" t)))
  2516. 'ada-indent)))
  2517. ;; Special case for label:
  2518. ((looking-at ada-block-label-re)
  2519. (list (- (save-excursion (back-to-indentation) (point)) ada-label-indent) 'ada-indent))
  2520. ;; nothing follows the block-start
  2521. (t
  2522. (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
  2523. (defun ada-get-indent-subprog (orgpoint)
  2524. "Calculate the indentation when point is just before a subprogram.
  2525. ORGPOINT is the limit position used in the calculation."
  2526. (let ((match-cons nil)
  2527. (cur-indent (save-excursion (back-to-indentation) (point)))
  2528. (foundis nil))
  2529. ;;
  2530. ;; is there an 'is' in front of point ?
  2531. ;;
  2532. (if (save-excursion
  2533. (setq match-cons
  2534. (ada-search-ignore-string-comment
  2535. "\\<\\(is\\|do\\)\\>" nil orgpoint)))
  2536. ;;
  2537. ;; yes, then skip to its end
  2538. ;;
  2539. (progn
  2540. (setq foundis t)
  2541. (goto-char (cdr match-cons)))
  2542. ;;
  2543. ;; no, then goto next non-ws, if there is one in front of point
  2544. ;;
  2545. (progn
  2546. (unless (ada-goto-next-non-ws orgpoint)
  2547. (goto-char orgpoint))))
  2548. (cond
  2549. ;;
  2550. ;; nothing follows 'is'
  2551. ;;
  2552. ((and
  2553. foundis
  2554. (save-excursion
  2555. (not (ada-search-ignore-string-comment
  2556. "[^ \t\n]" nil orgpoint t))))
  2557. (list cur-indent 'ada-indent))
  2558. ;;
  2559. ;; is abstract/separate/new ...
  2560. ;;
  2561. ((and
  2562. foundis
  2563. (save-excursion
  2564. (setq match-cons
  2565. (ada-search-ignore-string-comment
  2566. "\\<\\(separate\\|new\\|abstract\\)\\>"
  2567. nil orgpoint))))
  2568. (goto-char (car match-cons))
  2569. (ada-search-ignore-string-comment ada-subprog-start-re t)
  2570. (ada-get-indent-noindent orgpoint))
  2571. ;;
  2572. ;; something follows 'is'
  2573. ;;
  2574. ((and
  2575. foundis
  2576. (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
  2577. (goto-char match-cons)
  2578. (ada-indent-on-previous-lines t orgpoint)))
  2579. ;;
  2580. ;; no 'is' but ';'
  2581. ;;
  2582. ((save-excursion
  2583. (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
  2584. (list cur-indent 0))
  2585. ;;
  2586. ;; no 'is' or ';'
  2587. ;;
  2588. (t
  2589. (list cur-indent 'ada-broken-indent)))))
  2590. (defun ada-get-indent-noindent (orgpoint)
  2591. "Calculate the indentation when point is just before a 'noindent stmt'.
  2592. ORGPOINT is the limit position used in the calculation."
  2593. (let ((label 0))
  2594. (save-excursion
  2595. (beginning-of-line)
  2596. (cond
  2597. ;; This one is called when indenting a line preceded by a multi-line
  2598. ;; subprogram declaration (in that case, we are at this point inside
  2599. ;; the parameter declaration list)
  2600. ((ada-in-paramlist-p)
  2601. (ada-previous-procedure)
  2602. (list (save-excursion (back-to-indentation) (point)) 0))
  2603. ;; This one is called when indenting the second line of a multi-line
  2604. ;; declaration section, in a declare block or a record declaration
  2605. ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
  2606. (list (save-excursion (back-to-indentation) (point))
  2607. 'ada-broken-decl-indent))
  2608. ;; This one is called in every other case when indenting a line at the
  2609. ;; top level
  2610. (t
  2611. (if (looking-at (concat "[ \t]*" ada-block-label-re))
  2612. (setq label (- ada-label-indent))
  2613. (let (p)
  2614. ;; "with private" or "null record" cases
  2615. (if (or (save-excursion
  2616. (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
  2617. (setq p (point))
  2618. (save-excursion (forward-char -7);; skip back "private"
  2619. (ada-goto-previous-word)
  2620. (looking-at "with"))))
  2621. (save-excursion
  2622. (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
  2623. (setq p (point))
  2624. (save-excursion (forward-char -6);; skip back "record"
  2625. (ada-goto-previous-word)
  2626. (looking-at "null")))))
  2627. (progn
  2628. (goto-char p)
  2629. (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
  2630. (list (save-excursion (back-to-indentation) (point)) 0)))))
  2631. (if (save-excursion
  2632. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2633. 'search-forward))
  2634. (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
  2635. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2636. 'ada-broken-indent)))))))
  2637. (defun ada-get-indent-block-label (orgpoint)
  2638. "Calculate the indentation when before a label or variable declaration.
  2639. ORGPOINT is the limit position used in the calculation."
  2640. (let ((match-cons nil)
  2641. (cur-indent (save-excursion (back-to-indentation) (point))))
  2642. (ada-search-ignore-string-comment ":" nil)
  2643. (cond
  2644. ;; loop label
  2645. ((save-excursion
  2646. (setq match-cons (ada-search-ignore-string-comment
  2647. ada-loop-start-re nil orgpoint)))
  2648. (goto-char (car match-cons))
  2649. (ada-get-indent-loop orgpoint))
  2650. ;; declare label
  2651. ((save-excursion
  2652. (setq match-cons (ada-search-ignore-string-comment
  2653. "\\<declare\\|begin\\>" nil orgpoint)))
  2654. (goto-char (car match-cons))
  2655. (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
  2656. ;; variable declaration
  2657. ((ada-in-decl-p)
  2658. (if (save-excursion
  2659. (ada-search-ignore-string-comment ";" nil orgpoint))
  2660. (list cur-indent 0)
  2661. (list cur-indent 'ada-broken-indent)))
  2662. ;; nothing follows colon
  2663. (t
  2664. (list cur-indent '(- ada-label-indent))))))
  2665. (defun ada-get-indent-goto-label (orgpoint)
  2666. "Calculate the indentation when at a goto label."
  2667. (search-forward ">>")
  2668. (ada-goto-next-non-ws)
  2669. (if (>= (point) orgpoint)
  2670. ;; labeled statement is the one we need to indent
  2671. (list (- (point) ada-label-indent))
  2672. ;; else indentation is indent for labeled statement
  2673. (ada-indent-on-previous-lines t orgpoint)))
  2674. (defun ada-get-indent-loop (orgpoint)
  2675. "Calculate the indentation when just before a loop or a for ... use.
  2676. ORGPOINT is the limit position used in the calculation."
  2677. (let ((match-cons nil)
  2678. (pos (point))
  2679. ;; If looking at a named block, skip the label
  2680. (label (save-excursion
  2681. (back-to-indentation)
  2682. (if (looking-at ada-block-label-re)
  2683. (- ada-label-indent)
  2684. 0))))
  2685. (cond
  2686. ;;
  2687. ;; statement complete
  2688. ;;
  2689. ((save-excursion
  2690. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2691. 'search-forward))
  2692. (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
  2693. ;;
  2694. ;; simple loop
  2695. ;;
  2696. ((looking-at "loop\\>")
  2697. (setq pos (ada-get-indent-block-start orgpoint))
  2698. (if (equal label 0)
  2699. pos
  2700. (list (+ (car pos) label) (cadr pos))))
  2701. ;;
  2702. ;; 'for'- loop (or also a for ... use statement)
  2703. ;;
  2704. ((looking-at "for\\>")
  2705. (cond
  2706. ;;
  2707. ;; for ... use
  2708. ;;
  2709. ((save-excursion
  2710. (and
  2711. (goto-char (match-end 0))
  2712. (ada-goto-next-non-ws orgpoint)
  2713. (forward-word 1)
  2714. (if (= (char-after) ?') (forward-word 1) t)
  2715. (ada-goto-next-non-ws orgpoint)
  2716. (looking-at "\\<use\\>")
  2717. ;;
  2718. ;; check if there is a 'record' before point
  2719. ;;
  2720. (progn
  2721. (setq match-cons (ada-search-ignore-string-comment
  2722. "record" nil orgpoint nil 'word-search-forward))
  2723. t)))
  2724. (if match-cons
  2725. (progn
  2726. (goto-char (car match-cons))
  2727. (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
  2728. (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
  2729. )
  2730. ;;
  2731. ;; for..loop
  2732. ;;
  2733. ((save-excursion
  2734. (setq match-cons (ada-search-ignore-string-comment
  2735. "loop" nil orgpoint nil 'word-search-forward)))
  2736. (goto-char (car match-cons))
  2737. ;;
  2738. ;; indent according to 'loop', if it's first in the line;
  2739. ;; otherwise to 'for'
  2740. ;;
  2741. (unless (save-excursion
  2742. (back-to-indentation)
  2743. (looking-at "\\<loop\\>"))
  2744. (goto-char pos))
  2745. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2746. 'ada-indent))
  2747. ;;
  2748. ;; for-statement is broken
  2749. ;;
  2750. (t
  2751. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2752. 'ada-broken-indent))))
  2753. ;;
  2754. ;; 'while'-loop
  2755. ;;
  2756. ((looking-at "while\\>")
  2757. ;;
  2758. ;; while..loop ?
  2759. ;;
  2760. (if (save-excursion
  2761. (setq match-cons (ada-search-ignore-string-comment
  2762. "loop" nil orgpoint nil 'word-search-forward)))
  2763. (progn
  2764. (goto-char (car match-cons))
  2765. ;;
  2766. ;; indent according to 'loop', if it's first in the line;
  2767. ;; otherwise to 'while'.
  2768. ;;
  2769. (unless (save-excursion
  2770. (back-to-indentation)
  2771. (looking-at "\\<loop\\>"))
  2772. (goto-char pos))
  2773. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2774. 'ada-indent))
  2775. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2776. 'ada-broken-indent))))))
  2777. (defun ada-get-indent-type (orgpoint)
  2778. "Calculate the indentation when before a type statement.
  2779. ORGPOINT is the limit position used in the calculation."
  2780. (let ((match-dat nil))
  2781. (cond
  2782. ;;
  2783. ;; complete record declaration
  2784. ;;
  2785. ((save-excursion
  2786. (and
  2787. (setq match-dat (ada-search-ignore-string-comment
  2788. "end" nil orgpoint nil 'word-search-forward))
  2789. (ada-goto-next-non-ws)
  2790. (looking-at "\\<record\\>")
  2791. (forward-word 1)
  2792. (ada-goto-next-non-ws)
  2793. (= (char-after) ?\;)))
  2794. (goto-char (car match-dat))
  2795. (list (save-excursion (back-to-indentation) (point)) 0))
  2796. ;;
  2797. ;; record type
  2798. ;;
  2799. ((save-excursion
  2800. (setq match-dat (ada-search-ignore-string-comment
  2801. "record" nil orgpoint nil 'word-search-forward)))
  2802. (goto-char (car match-dat))
  2803. (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
  2804. ;;
  2805. ;; complete type declaration
  2806. ;;
  2807. ((save-excursion
  2808. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2809. 'search-forward))
  2810. (list (save-excursion (back-to-indentation) (point)) 0))
  2811. ;;
  2812. ;; "type ... is", but not "type ... is ...", which is broken
  2813. ;;
  2814. ((save-excursion
  2815. (and
  2816. (ada-search-ignore-string-comment "is" nil orgpoint nil
  2817. 'word-search-forward)
  2818. (not (ada-goto-next-non-ws orgpoint))))
  2819. (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
  2820. ;;
  2821. ;; broken statement
  2822. ;;
  2823. (t
  2824. (list (save-excursion (back-to-indentation) (point))
  2825. 'ada-broken-indent)))))
  2826. ;; -----------------------------------------------------------
  2827. ;; -- searching and matching
  2828. ;; -----------------------------------------------------------
  2829. (defun ada-goto-stmt-start (&optional ignore-goto-label)
  2830. "Move point to the beginning of the statement that point is in or after.
  2831. Return the new position of point.
  2832. As a special case, if we are looking at a closing parenthesis, skip to the
  2833. open parenthesis."
  2834. (let ((match-dat nil)
  2835. (orgpoint (point)))
  2836. (setq match-dat (ada-search-prev-end-stmt))
  2837. (if match-dat
  2838. ;;
  2839. ;; found a previous end-statement => check if anything follows
  2840. ;;
  2841. (unless (looking-at "declare")
  2842. (progn
  2843. (unless (save-excursion
  2844. (goto-char (cdr match-dat))
  2845. (ada-goto-next-non-ws orgpoint ignore-goto-label))
  2846. ;;
  2847. ;; nothing follows => it's the end-statement directly in
  2848. ;; front of point => search again
  2849. ;;
  2850. (setq match-dat (ada-search-prev-end-stmt)))
  2851. ;;
  2852. ;; if found the correct end-statement => goto next non-ws
  2853. ;;
  2854. (if match-dat
  2855. (goto-char (cdr match-dat)))
  2856. (ada-goto-next-non-ws)
  2857. ))
  2858. ;;
  2859. ;; no previous end-statement => we are at the beginning of the
  2860. ;; accessible part of the buffer
  2861. ;;
  2862. (progn
  2863. (goto-char (point-min))
  2864. ;;
  2865. ;; skip to the very first statement, if there is one
  2866. ;;
  2867. (unless (ada-goto-next-non-ws orgpoint)
  2868. (goto-char orgpoint))))
  2869. (point)))
  2870. (defun ada-search-prev-end-stmt ()
  2871. "Move point to previous end statement.
  2872. Return a cons cell whose car is the beginning and whose cdr
  2873. is the end of the match."
  2874. (let ((match-dat nil)
  2875. (found nil))
  2876. ;; search until found or beginning-of-buffer
  2877. (while
  2878. (and
  2879. (not found)
  2880. (setq match-dat (ada-search-ignore-string-comment
  2881. ada-end-stmt-re t)))
  2882. (goto-char (car match-dat))
  2883. (unless (ada-in-open-paren-p)
  2884. (cond
  2885. ((and (looking-at
  2886. "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
  2887. (save-excursion
  2888. (ada-goto-previous-word)
  2889. (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
  2890. (forward-word -1))
  2891. ((looking-at "is")
  2892. (setq found
  2893. (and (save-excursion (ada-goto-previous-word)
  2894. (ada-goto-previous-word)
  2895. (not (looking-at "subtype")))
  2896. (save-excursion (goto-char (cdr match-dat))
  2897. (ada-goto-next-non-ws)
  2898. ;; words that can go after an 'is'
  2899. (not (looking-at
  2900. (eval-when-compile
  2901. (concat "\\<"
  2902. (regexp-opt
  2903. '("separate" "access" "array"
  2904. "private" "abstract" "new") t)
  2905. "\\>\\|("))))))))
  2906. ((looking-at "private")
  2907. (save-excursion
  2908. (backward-word 1)
  2909. (setq found (not (looking-at "is")))))
  2910. (t
  2911. (setq found t))
  2912. )))
  2913. (if found
  2914. match-dat
  2915. nil)))
  2916. (defun ada-goto-next-non-ws (&optional limit skip-goto-label)
  2917. "Skip to next non-whitespace character.
  2918. Skips spaces, newlines and comments, and possibly goto labels.
  2919. Return `point' if moved, nil if not.
  2920. Stop the search at LIMIT.
  2921. Do not call this function from within a string."
  2922. (unless limit
  2923. (setq limit (point-max)))
  2924. (while (and (<= (point) limit)
  2925. (or (progn (forward-comment 10000)
  2926. (if (and (not (eobp))
  2927. (save-excursion (forward-char 1)
  2928. (ada-in-string-p)))
  2929. (progn (forward-sexp 1) t)))
  2930. (and skip-goto-label
  2931. (looking-at ada-goto-label-re)
  2932. (progn
  2933. (goto-char (match-end 0))
  2934. t)))))
  2935. (if (< (point) limit)
  2936. (point)
  2937. nil)
  2938. )
  2939. (defun ada-goto-stmt-end (&optional limit)
  2940. "Move point to the end of the statement that point is in or before.
  2941. Return the new position of point or nil if not found.
  2942. Stop the search at LIMIT."
  2943. (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
  2944. (point)
  2945. nil))
  2946. (defun ada-goto-next-word (&optional backward)
  2947. "Move point to the beginning of the next word of Ada code.
  2948. If BACKWARD is non-nil, jump to the beginning of the previous word.
  2949. Return the new position of point or nil if not found."
  2950. (let ((match-cons nil)
  2951. (orgpoint (point))
  2952. (old-syntax (char-to-string (char-syntax ?_))))
  2953. (modify-syntax-entry ?_ "w")
  2954. (unless backward
  2955. (skip-syntax-forward "w"))
  2956. (if (setq match-cons
  2957. (ada-search-ignore-string-comment "\\w" backward nil t))
  2958. ;;
  2959. ;; move to the beginning of the word found
  2960. ;;
  2961. (progn
  2962. (goto-char (car match-cons))
  2963. (skip-syntax-backward "w")
  2964. (point))
  2965. ;;
  2966. ;; if not found, restore old position of point
  2967. ;;
  2968. (goto-char orgpoint)
  2969. 'nil)
  2970. (modify-syntax-entry ?_ old-syntax))
  2971. )
  2972. (defun ada-check-matching-start (keyword)
  2973. "Signal an error if matching block start is not KEYWORD.
  2974. Moves point to the matching block start."
  2975. (ada-goto-matching-start 0)
  2976. (unless (looking-at (concat "\\<" keyword "\\>"))
  2977. (error "Matching start is not '%s'" keyword)))
  2978. (defun ada-check-defun-name (defun-name)
  2979. "Check if the name of the matching defun really is DEFUN-NAME.
  2980. Assumes point to be already positioned by `ada-goto-matching-start'.
  2981. Moves point to the beginning of the declaration."
  2982. ;; named block without a `declare'; ada-goto-matching-start leaves
  2983. ;; point at start of 'begin' for a block.
  2984. (if (save-excursion
  2985. (ada-goto-previous-word)
  2986. (looking-at (concat "\\<" defun-name "\\> *:")))
  2987. t ; name matches
  2988. ;; else
  2989. ;;
  2990. ;; 'accept' or 'package' ?
  2991. ;;
  2992. (unless (looking-at ada-subprog-start-re)
  2993. (ada-goto-decl-start))
  2994. ;;
  2995. ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
  2996. ;;
  2997. (save-excursion
  2998. ;;
  2999. ;; a named 'declare'-block ? => jump to the label
  3000. ;;
  3001. (if (looking-at "\\<declare\\>")
  3002. (progn
  3003. (forward-comment -1)
  3004. (backward-word 1))
  3005. ;;
  3006. ;; no, => 'procedure'/'function'/'task'/'protected'
  3007. ;;
  3008. (progn
  3009. (forward-word 2)
  3010. (backward-word 1)
  3011. ;;
  3012. ;; skip 'body' 'type'
  3013. ;;
  3014. (if (looking-at "\\<\\(body\\|type\\)\\>")
  3015. (forward-word 1))
  3016. (forward-sexp 1)
  3017. (backward-sexp 1)))
  3018. ;;
  3019. ;; should be looking-at the correct name
  3020. ;;
  3021. (unless (looking-at (concat "\\<" defun-name "\\>"))
  3022. (error "Matching defun has different name: %s"
  3023. (buffer-substring (point)
  3024. (progn (forward-sexp 1) (point))))))))
  3025. (defun ada-goto-decl-start (&optional noerror)
  3026. "Move point to the declaration start of the current construct.
  3027. If NOERROR is non-nil, return nil if no match was found;
  3028. otherwise throw error."
  3029. (let ((nest-count 1)
  3030. (regexp (eval-when-compile
  3031. (concat "\\<"
  3032. (regexp-opt
  3033. '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
  3034. "\\>")))
  3035. ;; first should be set to t if we should stop at the first
  3036. ;; "begin" we encounter.
  3037. (first t)
  3038. (count-generic nil)
  3039. (stop-at-when nil)
  3040. )
  3041. ;; Ignore "when" most of the time, except if we are looking at the
  3042. ;; beginning of a block (structure: case .. is
  3043. ;; when ... =>
  3044. ;; begin ...
  3045. ;; exception ... )
  3046. (if (looking-at "begin")
  3047. (setq stop-at-when t))
  3048. (if (or
  3049. (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
  3050. (save-excursion
  3051. (ada-search-ignore-string-comment
  3052. "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
  3053. (looking-at "generic")))
  3054. (setq count-generic t))
  3055. ;; search backward for interesting keywords
  3056. (while (and
  3057. (not (zerop nest-count))
  3058. (ada-search-ignore-string-comment regexp t))
  3059. ;;
  3060. ;; calculate nest-depth
  3061. ;;
  3062. (cond
  3063. ;;
  3064. ((looking-at "end")
  3065. (ada-goto-matching-start 1 noerror)
  3066. ;; In some case, two begin..end block can follow each other closely,
  3067. ;; which we have to detect, as in
  3068. ;; procedure P is
  3069. ;; procedure Q is
  3070. ;; begin
  3071. ;; end;
  3072. ;; begin -- here we should go to procedure, not begin
  3073. ;; end
  3074. (if (looking-at "begin")
  3075. (let ((loop-again t))
  3076. (save-excursion
  3077. (while loop-again
  3078. ;; If begin was just there as the beginning of a block
  3079. ;; (with no declare) then do nothing, otherwise just
  3080. ;; register that we have to find the statement that
  3081. ;; required the begin
  3082. (ada-search-ignore-string-comment
  3083. "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
  3084. t)
  3085. (if (looking-at "end")
  3086. (ada-goto-matching-start 1 noerror t)
  3087. (setq loop-again nil)
  3088. (unless (looking-at "begin")
  3089. (setq nest-count (1+ nest-count))))
  3090. ))
  3091. )))
  3092. ;;
  3093. ((looking-at "generic")
  3094. (if count-generic
  3095. (progn
  3096. (setq first nil)
  3097. (setq nest-count (1- nest-count)))))
  3098. ;;
  3099. ((looking-at "if")
  3100. (save-excursion
  3101. (forward-word -1)
  3102. (unless (looking-at "\\<end[ \t\n]*if\\>")
  3103. (progn
  3104. (setq nest-count (1- nest-count))
  3105. (setq first nil)))))
  3106. ;;
  3107. ((looking-at "declare\\|generic")
  3108. (setq nest-count (1- nest-count))
  3109. (setq first t))
  3110. ;;
  3111. ((looking-at "is")
  3112. ;; look for things to ignore
  3113. (if
  3114. (or
  3115. ;; generic formal parameter
  3116. (looking-at "is[ t]+<>")
  3117. ;; A type definition, or a case statement. Note that the
  3118. ;; goto-matching-start above on 'end record' leaves us at
  3119. ;; 'record', not at 'type'.
  3120. ;;
  3121. ;; We get to a case statement here by calling
  3122. ;; 'ada-move-to-end' from inside a case statement; then
  3123. ;; we are not ignoring 'when'.
  3124. (save-excursion
  3125. ;; Skip type discriminants or case argument function call param list
  3126. (forward-comment -10000)
  3127. (forward-char -1)
  3128. (if (= (char-after) ?\))
  3129. (progn
  3130. (forward-char 1)
  3131. (backward-sexp 1)
  3132. (forward-comment -10000)
  3133. ))
  3134. ;; skip type or case argument name
  3135. (skip-chars-backward "a-zA-Z0-9_.'")
  3136. (ada-goto-previous-word)
  3137. (and
  3138. ;; if it's a protected type, it's the decl start we
  3139. ;; are looking for; since we didn't see the 'end'
  3140. ;; above, we are inside it.
  3141. (looking-at "\\<\\(sub\\)?type\\|case\\>")
  3142. (save-match-data
  3143. (ada-goto-previous-word)
  3144. (not (looking-at "\\<protected\\>"))))
  3145. ) ; end of type definition p
  3146. ;; null procedure declaration
  3147. (save-excursion (ada-goto-next-word) (looking-at "\\<null\\>"))
  3148. );; end or
  3149. ;; skip this construct
  3150. nil
  3151. ;; this is the right "is"
  3152. (setq nest-count (1- nest-count))
  3153. (setq first nil)))
  3154. ;;
  3155. ((looking-at "new")
  3156. (if (save-excursion
  3157. (ada-goto-previous-word)
  3158. (looking-at "is"))
  3159. (goto-char (match-beginning 0))))
  3160. ;;
  3161. ((and first
  3162. (looking-at "begin"))
  3163. (setq nest-count 0))
  3164. ;;
  3165. ((looking-at "when")
  3166. (save-excursion
  3167. (forward-word -1)
  3168. (unless (looking-at "\\<exit[ \t\n]*when\\>")
  3169. (progn
  3170. (if stop-at-when
  3171. (setq nest-count (1- nest-count)))
  3172. ))))
  3173. ;;
  3174. ((looking-at "begin")
  3175. (setq first nil))
  3176. ;;
  3177. (t
  3178. (setq nest-count (1+ nest-count))
  3179. (setq first nil)))
  3180. );; end of loop
  3181. ;; check if declaration-start is really found
  3182. (if (and
  3183. (zerop nest-count)
  3184. (if (looking-at "is")
  3185. (ada-search-ignore-string-comment ada-subprog-start-re t)
  3186. (looking-at "declare\\|generic")))
  3187. t
  3188. (if noerror nil
  3189. (error "No matching proc/func/task/declare/package/protected")))
  3190. ))
  3191. (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
  3192. "Move point to the beginning of a block-start.
  3193. Which block depends on the value of NEST-LEVEL, which defaults to zero.
  3194. If NOERROR is non-nil, it only returns nil if no matching start was found.
  3195. If GOTOTHEN is non-nil, point moves to the 'then' following 'if'."
  3196. (let ((nest-count (if nest-level nest-level 0))
  3197. (found nil)
  3198. (last-was-begin '())
  3199. ;; List all keywords encountered while traversing
  3200. ;; something like '("end" "end" "begin")
  3201. ;; This is removed from the list when "package", "procedure",...
  3202. ;; are seen. The goal is to find whether a package has an elaboration
  3203. ;; part
  3204. (pos nil))
  3205. ;; search backward for interesting keywords
  3206. (while (and
  3207. (not found)
  3208. (ada-search-ignore-string-comment ada-matching-start-re t))
  3209. (unless (and (looking-at "\\<record\\>")
  3210. (save-excursion
  3211. (forward-word -1)
  3212. (looking-at "\\<null\\>")))
  3213. (progn
  3214. ;; calculate nest-depth
  3215. (cond
  3216. ;; found block end => increase nest depth
  3217. ((looking-at "end")
  3218. (push nil last-was-begin)
  3219. (setq nest-count (1+ nest-count)))
  3220. ;; found loop/select/record/case/if => check if it starts or
  3221. ;; ends a block
  3222. ((looking-at "loop\\|select\\|record\\|case\\|if")
  3223. (setq pos (point))
  3224. (save-excursion
  3225. ;; check if keyword follows 'end'
  3226. (ada-goto-previous-word)
  3227. (if (looking-at "\\<end\\>[ \t]*[^;]")
  3228. (progn
  3229. ;; it ends a block => increase nest depth
  3230. (setq nest-count (1+ nest-count)
  3231. pos (point))
  3232. (push nil last-was-begin))
  3233. ;; it starts a block => decrease nest depth
  3234. (setq nest-count (1- nest-count))
  3235. ;; Some nested "begin .. end" blocks with no "declare"?
  3236. ;; => remove those entries
  3237. (while (car last-was-begin)
  3238. (setq last-was-begin (cdr (cdr last-was-begin))))
  3239. (setq last-was-begin (cdr last-was-begin))
  3240. ))
  3241. (goto-char pos)
  3242. )
  3243. ;; found package start => check if it really is a block
  3244. ((looking-at "package")
  3245. (save-excursion
  3246. ;; ignore if this is just a renames statement
  3247. (let ((current (point))
  3248. (pos (ada-search-ignore-string-comment
  3249. "\\<\\(is\\|renames\\|;\\)\\>" nil)))
  3250. (if pos
  3251. (goto-char (car pos))
  3252. (error (concat
  3253. "No matching 'is' or 'renames' for 'package' at"
  3254. " line "
  3255. (number-to-string (count-lines 1 (1+ current)))))))
  3256. (unless (looking-at "renames")
  3257. (progn
  3258. (forward-word 1)
  3259. (ada-goto-next-non-ws)
  3260. ;; ignore it if it is only a declaration with 'new'
  3261. ;; We could have package Foo is new ....
  3262. ;; or package Foo is separate;
  3263. ;; or package Foo is begin null; end Foo
  3264. ;; for elaboration code (elaboration)
  3265. (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
  3266. (not (car last-was-begin)))
  3267. (setq nest-count (1- nest-count))))))
  3268. (setq last-was-begin (cdr last-was-begin))
  3269. )
  3270. ;; found task start => check if it has a body
  3271. ((looking-at "task")
  3272. (save-excursion
  3273. (forward-word 1)
  3274. (ada-goto-next-non-ws)
  3275. (cond
  3276. ((looking-at "\\<body\\>"))
  3277. ((looking-at "\\<type\\>")
  3278. ;; In that case, do nothing if there is a "is"
  3279. (forward-word 2);; skip "type"
  3280. (ada-goto-next-non-ws);; skip type name
  3281. ;; Do nothing if we are simply looking at a simple
  3282. ;; "task type name;" statement with no block
  3283. (unless (looking-at ";")
  3284. (progn
  3285. ;; Skip the parameters
  3286. (if (looking-at "(")
  3287. (ada-search-ignore-string-comment ")" nil))
  3288. (let ((tmp (ada-search-ignore-string-comment
  3289. "\\<\\(is\\|;\\)\\>" nil)))
  3290. (if tmp
  3291. (progn
  3292. (goto-char (car tmp))
  3293. (if (looking-at "is")
  3294. (setq nest-count (1- nest-count)))))))))
  3295. (t
  3296. ;; Check if that task declaration had a block attached to
  3297. ;; it (i.e do nothing if we have just "task name;")
  3298. (unless (progn (forward-word 1)
  3299. (looking-at "[ \t]*;"))
  3300. (setq nest-count (1- nest-count))))))
  3301. (setq last-was-begin (cdr last-was-begin))
  3302. )
  3303. ((looking-at "declare")
  3304. ;; remove entry for begin and end (include nested begin..end
  3305. ;; groups)
  3306. (setq last-was-begin (cdr last-was-begin))
  3307. (let ((count 1))
  3308. (while (and (> count 0))
  3309. (if (equal (car last-was-begin) t)
  3310. (setq count (1+ count))
  3311. (setq count (1- count)))
  3312. (setq last-was-begin (cdr last-was-begin))
  3313. )))
  3314. ((looking-at "protected")
  3315. ;; Ignore if this is just a declaration
  3316. (save-excursion
  3317. (let ((pos (ada-search-ignore-string-comment
  3318. "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil)))
  3319. (if pos
  3320. (goto-char (car pos)))
  3321. (if (looking-at "is")
  3322. ;; remove entry for end
  3323. (setq last-was-begin (cdr last-was-begin)))))
  3324. (setq nest-count (1- nest-count)))
  3325. ((or (looking-at "procedure")
  3326. (looking-at "function"))
  3327. ;; Ignore if this is just a declaration
  3328. (save-excursion
  3329. (let ((pos (ada-search-ignore-string-comment
  3330. "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil)))
  3331. (if pos
  3332. (goto-char (car pos)))
  3333. (if (looking-at "is")
  3334. ;; remove entry for begin and end
  3335. (setq last-was-begin (cdr (cdr last-was-begin))))))
  3336. )
  3337. ;; all the other block starts
  3338. (t
  3339. (push (looking-at "begin") last-was-begin)
  3340. (setq nest-count (1- nest-count)))
  3341. )
  3342. ;; match is found, if nest-depth is zero
  3343. (setq found (zerop nest-count))))) ; end of loop
  3344. (if (bobp)
  3345. (point)
  3346. (if found
  3347. ;;
  3348. ;; match found => is there anything else to do ?
  3349. ;;
  3350. (progn
  3351. (cond
  3352. ;;
  3353. ;; found 'if' => skip to 'then', if it's on a separate line
  3354. ;; and GOTOTHEN is non-nil
  3355. ;;
  3356. ((and
  3357. gotothen
  3358. (looking-at "if")
  3359. (save-excursion
  3360. (ada-search-ignore-string-comment "then" nil nil nil
  3361. 'word-search-forward)
  3362. (back-to-indentation)
  3363. (looking-at "\\<then\\>")))
  3364. (goto-char (match-beginning 0)))
  3365. ;;
  3366. ;; found 'do' => skip back to 'accept' or 'return'
  3367. ;;
  3368. ((looking-at "do")
  3369. (unless (ada-search-ignore-string-comment
  3370. "\\<accept\\|return\\>" t)
  3371. (error "Missing 'accept' or 'return' in front of 'do'"))))
  3372. (point))
  3373. (if noerror
  3374. nil
  3375. (error "No matching start"))))))
  3376. (defun ada-goto-matching-end (&optional nest-level noerror)
  3377. "Move point to the end of a block.
  3378. Which block depends on the value of NEST-LEVEL, which defaults to zero.
  3379. If NOERROR is non-nil, it only returns nil if no matching start found."
  3380. (let ((nest-count (or nest-level 0))
  3381. (regex (eval-when-compile
  3382. (concat "\\<"
  3383. (regexp-opt '("end" "loop" "select" "begin" "case"
  3384. "if" "task" "package" "record" "do"
  3385. "procedure" "function") t)
  3386. "\\>")))
  3387. found
  3388. pos
  3389. ;; First is used for subprograms: they are generally handled
  3390. ;; recursively, but of course we do not want to do that the
  3391. ;; first time (see comment below about subprograms)
  3392. (first (not (looking-at "declare"))))
  3393. ;; If we are already looking at one of the keywords, this shouldn't count
  3394. ;; in the nesting loop below, so we just make sure we don't count it.
  3395. ;; "declare" is a special case because we need to look after the "begin"
  3396. ;; keyword
  3397. (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
  3398. (forward-char 1))
  3399. ;;
  3400. ;; search forward for interesting keywords
  3401. ;;
  3402. (while (and
  3403. (not found)
  3404. (ada-search-ignore-string-comment regex nil))
  3405. ;;
  3406. ;; calculate nest-depth
  3407. ;;
  3408. (backward-word 1)
  3409. (cond
  3410. ;; procedures and functions need to be processed recursively, in
  3411. ;; case they are defined in a declare/begin block, as in:
  3412. ;; declare -- NL 0 (nested level)
  3413. ;; A : Boolean;
  3414. ;; procedure B (C : D) is
  3415. ;; begin -- NL 1
  3416. ;; null;
  3417. ;; end B; -- NL 0, and we would exit
  3418. ;; begin
  3419. ;; end; -- we should exit here
  3420. ;; processing them recursively avoids the need for any special
  3421. ;; handling.
  3422. ;; Nothing should be done if we have only the specs or a
  3423. ;; generic instantiation.
  3424. ((and (looking-at "\\<procedure\\|function\\>"))
  3425. (if first
  3426. (forward-word 1)
  3427. (setq pos (point))
  3428. (ada-search-ignore-string-comment "is\\|;")
  3429. (if (= (char-before) ?s)
  3430. (progn
  3431. (ada-goto-next-non-ws)
  3432. (unless (looking-at "\\<new\\>")
  3433. (progn
  3434. (goto-char pos)
  3435. (ada-goto-matching-end 0 t)))))))
  3436. ;; found block end => decrease nest depth
  3437. ((looking-at "\\<end\\>")
  3438. (setq nest-count (1- nest-count)
  3439. found (<= nest-count 0))
  3440. ;; skip the following keyword
  3441. (if (progn
  3442. (skip-chars-forward "end")
  3443. (ada-goto-next-non-ws)
  3444. (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
  3445. (forward-word 1)))
  3446. ;; found package start => check if it really starts a block, and is not
  3447. ;; in fact a generic instantiation for instance
  3448. ((looking-at "\\<package\\>")
  3449. (ada-search-ignore-string-comment "is" nil nil nil
  3450. 'word-search-forward)
  3451. (ada-goto-next-non-ws)
  3452. ;; ignore and skip it if it is only a 'new' package
  3453. (if (looking-at "\\<new\\>")
  3454. (goto-char (match-end 0))
  3455. (setq nest-count (1+ nest-count)
  3456. found (<= nest-count 0))))
  3457. ;; all the other block starts
  3458. (t
  3459. (if (not first)
  3460. (setq nest-count (1+ nest-count)))
  3461. (setq found (<= nest-count 0))
  3462. (forward-word 1))) ; end of 'cond'
  3463. (setq first nil))
  3464. (if found
  3465. t
  3466. (if noerror
  3467. nil
  3468. (error "No matching end")))
  3469. ))
  3470. (defun ada-search-ignore-string-comment
  3471. (search-re &optional backward limit paramlists search-func)
  3472. "Regexp-search for SEARCH-RE, ignoring comments, strings.
  3473. Returns a cons cell of begin and end of match data or nil, if not found.
  3474. If BACKWARD is non-nil, search backward; search forward otherwise.
  3475. The search stops at pos LIMIT.
  3476. If PARAMLISTS is nil, ignore parameter lists.
  3477. The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
  3478. in case we are searching for a constant string.
  3479. Point is moved at the beginning of the SEARCH-RE."
  3480. (let (found
  3481. begin
  3482. end
  3483. parse-result)
  3484. ;; FIXME: need to pass BACKWARD to search-func!
  3485. (unless search-func
  3486. (setq search-func (if backward 're-search-backward 're-search-forward)))
  3487. ;;
  3488. ;; search until found or end-of-buffer
  3489. ;; We have to test that we do not look further than limit
  3490. ;;
  3491. (with-syntax-table ada-mode-symbol-syntax-table
  3492. (while (and (not found)
  3493. (or (not limit)
  3494. (or (and backward (<= limit (point)))
  3495. (>= limit (point))))
  3496. (funcall search-func search-re limit 1))
  3497. (setq begin (match-beginning 0))
  3498. (setq end (match-end 0))
  3499. (setq parse-result (parse-partial-sexp (point-at-bol) (point)))
  3500. (cond
  3501. ;;
  3502. ;; If inside a string, skip it (and the following comments)
  3503. ;;
  3504. ((ada-in-string-p parse-result)
  3505. (if (featurep 'xemacs)
  3506. (search-backward "\"" nil t)
  3507. (goto-char (nth 8 parse-result)))
  3508. (unless backward (forward-sexp 1)))
  3509. ;;
  3510. ;; If inside a comment, skip it (and the following comments)
  3511. ;; There is a special code for comments at the end of the file
  3512. ;;
  3513. ((ada-in-comment-p parse-result)
  3514. (if (featurep 'xemacs)
  3515. (progn
  3516. (forward-line 1)
  3517. (beginning-of-line)
  3518. (forward-comment -1))
  3519. (goto-char (nth 8 parse-result)))
  3520. (unless backward
  3521. ;; at the end of the file, it is not possible to skip a comment
  3522. ;; so we just go at the end of the line
  3523. (if (forward-comment 1)
  3524. (progn
  3525. (forward-comment 1000)
  3526. (beginning-of-line))
  3527. (end-of-line))))
  3528. ;;
  3529. ;; directly in front of a comment => skip it, if searching forward
  3530. ;;
  3531. ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
  3532. (unless backward (progn (forward-char -1) (forward-comment 1000))))
  3533. ;;
  3534. ;; found a parameter-list but should ignore it => skip it
  3535. ;;
  3536. ((and (not paramlists) (ada-in-paramlist-p))
  3537. (if backward
  3538. (search-backward "(" nil t)
  3539. (search-forward ")" nil t)))
  3540. ;;
  3541. ;; found what we were looking for
  3542. ;;
  3543. (t
  3544. (setq found t))))) ; end of loop
  3545. (if found
  3546. (cons begin end)
  3547. nil)))
  3548. ;; -------------------------------------------------------
  3549. ;; -- Testing the position of the cursor
  3550. ;; -------------------------------------------------------
  3551. (defun ada-in-decl-p ()
  3552. "Return t if point is inside a declarative part.
  3553. Assumes point to be at the end of a statement."
  3554. (or (ada-in-paramlist-p)
  3555. (save-excursion
  3556. (ada-goto-decl-start t))))
  3557. (defun ada-looking-at-semi-or ()
  3558. "Return t if looking at an 'or' following a semicolon."
  3559. (save-excursion
  3560. (and (looking-at "\\<or\\>")
  3561. (progn
  3562. (forward-word 1)
  3563. (ada-goto-stmt-start)
  3564. (looking-at "\\<or\\>")))))
  3565. (defun ada-looking-at-semi-private ()
  3566. "Return t if looking at the start of a private section in a package.
  3567. Return nil if the private is part of the package name, as in
  3568. 'private package A is...' (this can only happen at top level)."
  3569. (save-excursion
  3570. (and (looking-at "\\<private\\>")
  3571. (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
  3572. ;; Make sure this is the start of a private section (ie after
  3573. ;; a semicolon or just after the package declaration, but not
  3574. ;; after a 'type ... is private' or 'is new ... with private'
  3575. ;;
  3576. ;; Note that a 'private' statement at the beginning of the buffer
  3577. ;; does not indicate a private section, since this is instead a
  3578. ;; 'private procedure ...'
  3579. (progn (forward-comment -1000)
  3580. (and (not (bobp))
  3581. (or (= (char-before) ?\;)
  3582. (and (forward-word -3)
  3583. (looking-at "\\<package\\>"))))))))
  3584. (defun ada-in-paramlist-p ()
  3585. "Return t if point is inside the parameter-list of a declaration, but not a subprogram call or aggregate."
  3586. (save-excursion
  3587. (and
  3588. (ada-search-ignore-string-comment "(\\|)" t nil t)
  3589. ;; inside parentheses ?
  3590. (= (char-after) ?\()
  3591. ;; We could be looking at two things here:
  3592. ;; operator definition: function "." (
  3593. ;; subprogram definition: procedure .... (
  3594. ;; Let's skip back over the first one
  3595. (progn
  3596. (skip-chars-backward " \t\n")
  3597. (if (= (char-before) ?\")
  3598. (backward-char 3)
  3599. (backward-word 1))
  3600. t)
  3601. ;; and now over the second one
  3602. (backward-word 1)
  3603. ;; We should ignore the case when the reserved keyword is in a
  3604. ;; comment (for instance, when we have:
  3605. ;; -- .... package
  3606. ;; Test (A)
  3607. ;; we should return nil
  3608. (not (ada-in-string-or-comment-p))
  3609. ;; right keyword two words before parenthesis ?
  3610. ;; Type is in this list because of discriminants
  3611. ;; pragma is not, because the syntax is that of a subprogram call.
  3612. (looking-at (eval-when-compile
  3613. (concat "\\<\\("
  3614. "procedure\\|function\\|body\\|"
  3615. "task\\|entry\\|accept\\|"
  3616. "access[ \t]+procedure\\|"
  3617. "access[ \t]+function\\|"
  3618. "type\\)\\>"))))))
  3619. (defun ada-search-ignore-complex-boolean (regexp backwardp)
  3620. "Search for REGEXP, ignoring comments, strings, 'and then', 'or else'.
  3621. If BACKWARDP is non-nil, search backward; search forward otherwise."
  3622. (let (result)
  3623. (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
  3624. (save-excursion (forward-word -1)
  3625. (looking-at "and then\\|or else"))))
  3626. result))
  3627. (defun ada-in-open-paren-p ()
  3628. "Non-nil if in an open parenthesis.
  3629. Return value is the position of the first non-ws behind the last unclosed
  3630. parenthesis, or nil."
  3631. (save-excursion
  3632. (let ((parse (parse-partial-sexp
  3633. (point)
  3634. (or (car (ada-search-ignore-complex-boolean
  3635. "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
  3636. t))
  3637. (point-min)))))
  3638. (if (nth 1 parse)
  3639. (progn
  3640. (goto-char (1+ (nth 1 parse)))
  3641. ;; Skip blanks, if they are not followed by a comment
  3642. ;; See:
  3643. ;; type A is ( Value_0,
  3644. ;; Value_1);
  3645. ;; type B is ( -- comment
  3646. ;; Value_2);
  3647. (if (or (not ada-indent-handle-comment-special)
  3648. (not (looking-at "[ \t]+--")))
  3649. (skip-chars-forward " \t"))
  3650. (point))))))
  3651. ;; -----------------------------------------------------------
  3652. ;; -- Behavior Of TAB Key
  3653. ;; -----------------------------------------------------------
  3654. (defun ada-tab ()
  3655. "Do indenting or tabbing according to `ada-tab-policy'.
  3656. In Transient Mark mode, if the mark is active, operate on the contents
  3657. of the region. Otherwise, operate only on the current line."
  3658. (interactive)
  3659. (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
  3660. ((eq ada-tab-policy 'indent-auto)
  3661. (if (ada-region-selected)
  3662. (ada-indent-region (region-beginning) (region-end))
  3663. (ada-indent-current)))
  3664. ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
  3665. ))
  3666. (defun ada-untab (_arg)
  3667. "Delete leading indenting according to `ada-tab-policy'."
  3668. ;; FIXME: ARG is ignored
  3669. (interactive "P")
  3670. (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
  3671. ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
  3672. ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
  3673. ))
  3674. (defun ada-indent-current-function ()
  3675. "Ada mode version of the `indent-line-function'."
  3676. (interactive "*")
  3677. (let ((starting-point (point-marker)))
  3678. (beginning-of-line)
  3679. (ada-tab)
  3680. (if (< (point) starting-point)
  3681. (goto-char starting-point))
  3682. (set-marker starting-point nil)
  3683. ))
  3684. (defun ada-tab-hard ()
  3685. "Indent current line to next tab stop."
  3686. (interactive)
  3687. (save-excursion
  3688. (beginning-of-line)
  3689. (insert-char ? ada-indent))
  3690. (if (bolp) (forward-char ada-indent)))
  3691. (defun ada-untab-hard ()
  3692. "Indent current line to previous tab stop."
  3693. (interactive)
  3694. (indent-rigidly (point-at-bol) (point-at-eol) (- 0 ada-indent)))
  3695. ;; ------------------------------------------------------------
  3696. ;; -- Miscellaneous
  3697. ;; ------------------------------------------------------------
  3698. ;; Not needed any more for Emacs 21.2, but still needed for backward
  3699. ;; compatibility
  3700. (defun ada-remove-trailing-spaces ()
  3701. "Remove trailing spaces in the whole buffer."
  3702. (interactive)
  3703. (save-match-data
  3704. (save-excursion
  3705. (save-restriction
  3706. (widen)
  3707. (goto-char (point-min))
  3708. (while (re-search-forward "[ \t]+$" (point-max) t)
  3709. (replace-match "" nil nil))))))
  3710. (defun ada-gnat-style ()
  3711. "Clean up comments, `(' and `,' for GNAT style checking switch."
  3712. (interactive)
  3713. (save-excursion
  3714. ;; The \n is required, or the line after an empty comment line is
  3715. ;; simply ignored.
  3716. (goto-char (point-min))
  3717. (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
  3718. (replace-match "-- \\1")
  3719. (forward-line 1)
  3720. (beginning-of-line))
  3721. (goto-char (point-min))
  3722. (while (re-search-forward "\\>(" nil t)
  3723. (if (not (ada-in-string-or-comment-p))
  3724. (replace-match " (")))
  3725. (goto-char (point-min))
  3726. (while (re-search-forward ";--" nil t)
  3727. (forward-char -1)
  3728. (if (not (ada-in-string-or-comment-p))
  3729. (replace-match "; --")))
  3730. (goto-char (point-min))
  3731. (while (re-search-forward "([ \t]+" nil t)
  3732. (if (not (ada-in-string-or-comment-p))
  3733. (replace-match "(")))
  3734. (goto-char (point-min))
  3735. (while (re-search-forward ")[ \t]+)" nil t)
  3736. (if (not (ada-in-string-or-comment-p))
  3737. (replace-match "))")))
  3738. (goto-char (point-min))
  3739. (while (re-search-forward "\\>:" nil t)
  3740. (if (not (ada-in-string-or-comment-p))
  3741. (replace-match " :")))
  3742. ;; Make sure there is a space after a ','.
  3743. ;; Always go back to the beginning of the match, since otherwise
  3744. ;; a statement like ('F','D','E') is incorrectly modified.
  3745. (goto-char (point-min))
  3746. (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
  3747. (if (not (save-excursion
  3748. (goto-char (match-beginning 0))
  3749. (ada-in-string-or-comment-p)))
  3750. (replace-match ", \\1")))
  3751. ;; Operators should be surrounded by spaces.
  3752. (goto-char (point-min))
  3753. (while (re-search-forward
  3754. "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
  3755. nil t)
  3756. (goto-char (match-beginning 1))
  3757. (if (or (looking-at "--")
  3758. (ada-in-string-or-comment-p))
  3759. (progn
  3760. (forward-line 1)
  3761. (beginning-of-line))
  3762. (cond
  3763. ((string= (match-string 1) "/=")
  3764. (replace-match " /= "))
  3765. ((string= (match-string 1) "..")
  3766. (replace-match " .. "))
  3767. ((string= (match-string 1) "**")
  3768. (replace-match " ** "))
  3769. ((string= (match-string 1) ":=")
  3770. (replace-match " := "))
  3771. (t
  3772. (replace-match " \\1 ")))
  3773. (forward-char 1)))
  3774. ))
  3775. ;; -------------------------------------------------------------
  3776. ;; -- Moving To Procedures/Packages/Statements
  3777. ;; -------------------------------------------------------------
  3778. (defun ada-move-to-start ()
  3779. "Move point to the matching start of the current Ada structure."
  3780. (interactive)
  3781. (let ((pos (point)))
  3782. (with-syntax-table ada-mode-symbol-syntax-table
  3783. (save-excursion
  3784. ;;
  3785. ;; do nothing if in string or comment or not on 'end ...;'
  3786. ;; or if an error occurs during processing
  3787. ;;
  3788. (or
  3789. (ada-in-string-or-comment-p)
  3790. (and (progn
  3791. (or (looking-at "[ \t]*\\<end\\>")
  3792. (backward-word 1))
  3793. (or (looking-at "[ \t]*\\<end\\>")
  3794. (backward-word 1))
  3795. (or (looking-at "[ \t]*\\<end\\>")
  3796. (error "Not on end ...;")))
  3797. (ada-goto-matching-start 1)
  3798. (setq pos (point))
  3799. ;;
  3800. ;; on 'begin' => go on, according to user option
  3801. ;;
  3802. ada-move-to-declaration
  3803. (looking-at "\\<begin\\>")
  3804. (ada-goto-decl-start)
  3805. (setq pos (point))))
  3806. ) ; end of save-excursion
  3807. ;; now really move to the found position
  3808. (goto-char pos))))
  3809. (defun ada-move-to-end ()
  3810. "Move point to the end of the block around point.
  3811. Moves to 'begin' if in a declarative part."
  3812. (interactive)
  3813. (let ((pos (point))
  3814. decl-start)
  3815. (with-syntax-table ada-mode-symbol-syntax-table
  3816. (save-excursion
  3817. (cond
  3818. ;; Go to the beginning of the current word, and check if we are
  3819. ;; directly on 'begin'
  3820. ((save-excursion
  3821. (skip-syntax-backward "w")
  3822. (looking-at "\\<begin\\>"))
  3823. (ada-goto-matching-end 1))
  3824. ;; on first line of subprogram body
  3825. ;; Do nothing for specs or generic instantiation, since these are
  3826. ;; handled as the general case (find the enclosing block)
  3827. ;; We also need to make sure that we ignore nested subprograms
  3828. ((save-excursion
  3829. (and (skip-syntax-backward "w")
  3830. (looking-at "\\<function\\>\\|\\<procedure\\>" )
  3831. (ada-search-ignore-string-comment "is\\|;")
  3832. (not (= (char-before) ?\;))
  3833. ))
  3834. (skip-syntax-backward "w")
  3835. (ada-goto-matching-end 0 t))
  3836. ;; on first line of task declaration
  3837. ((save-excursion
  3838. (and (ada-goto-stmt-start)
  3839. (looking-at "\\<task\\>" )
  3840. (forward-word 1)
  3841. (ada-goto-next-non-ws)
  3842. (looking-at "\\<body\\>")))
  3843. (ada-search-ignore-string-comment "begin" nil nil nil
  3844. 'word-search-forward))
  3845. ;; accept block start
  3846. ((save-excursion
  3847. (and (ada-goto-stmt-start)
  3848. (looking-at "\\<accept\\>" )))
  3849. (ada-goto-matching-end 0))
  3850. ;; package start
  3851. ((save-excursion
  3852. (setq decl-start (and (ada-goto-decl-start t) (point)))
  3853. (and decl-start (looking-at "\\<package\\>")))
  3854. (ada-goto-matching-end 1))
  3855. ;; On a "declare" keyword
  3856. ((save-excursion
  3857. (skip-syntax-backward "w")
  3858. (looking-at "\\<declare\\>"))
  3859. (ada-goto-matching-end 0 t))
  3860. ;; inside a 'begin' ... 'end' block
  3861. (decl-start
  3862. (goto-char decl-start)
  3863. (ada-goto-matching-end 0 t))
  3864. ;; (hopefully ;-) everything else
  3865. (t
  3866. (ada-goto-matching-end 1)))
  3867. (setq pos (point))
  3868. )
  3869. ;; now really move to the position found
  3870. (goto-char pos))))
  3871. (defun ada-next-procedure ()
  3872. "Move point to next procedure."
  3873. (interactive)
  3874. (end-of-line)
  3875. (if (re-search-forward ada-procedure-start-regexp nil t)
  3876. (goto-char (match-beginning 4))
  3877. (error "No more functions/procedures/tasks")))
  3878. (defun ada-previous-procedure ()
  3879. "Move point to previous procedure."
  3880. (interactive)
  3881. (beginning-of-line)
  3882. (if (re-search-backward ada-procedure-start-regexp nil t)
  3883. (goto-char (match-beginning 4))
  3884. (error "No more functions/procedures/tasks")))
  3885. (defun ada-next-package ()
  3886. "Move point to next package."
  3887. (interactive)
  3888. (end-of-line)
  3889. (if (re-search-forward ada-package-start-regexp nil t)
  3890. (goto-char (match-beginning 1))
  3891. (error "No more packages")))
  3892. (defun ada-previous-package ()
  3893. "Move point to previous package."
  3894. (interactive)
  3895. (beginning-of-line)
  3896. (if (re-search-backward ada-package-start-regexp nil t)
  3897. (goto-char (match-beginning 1))
  3898. (error "No more packages")))
  3899. ;; ------------------------------------------------------------
  3900. ;; -- Define keymap and menus for Ada
  3901. ;; -------------------------------------------------------------
  3902. (defun ada-create-keymap ()
  3903. "Create the keymap associated with the Ada mode."
  3904. ;; All non-standard keys go into ada-mode-extra-map
  3905. (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map)
  3906. ;; Indentation and Formatting
  3907. (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
  3908. (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
  3909. (define-key ada-mode-map "\t" 'ada-tab)
  3910. (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
  3911. (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
  3912. (define-key ada-mode-map [(shift tab)] 'ada-untab)
  3913. (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
  3914. ;; We don't want to make meta-characters case-specific.
  3915. ;; Movement
  3916. (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
  3917. (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
  3918. (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
  3919. (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
  3920. ;; Compilation
  3921. (unless (lookup-key ada-mode-map "\C-c\C-c")
  3922. (define-key ada-mode-map "\C-c\C-c" 'compile))
  3923. ;; Casing
  3924. (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
  3925. (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
  3926. (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
  3927. (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
  3928. ;; On XEmacs, you can easily specify whether DEL should deletes
  3929. ;; one character forward or one character backward. Take this into
  3930. ;; account
  3931. (define-key ada-mode-map
  3932. (if (boundp 'delete-key-deletes-forward) [backspace] "\177")
  3933. 'backward-delete-char-untabify)
  3934. ;; Make body
  3935. (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
  3936. ;; Use predefined function of Emacs19 for comments (RE)
  3937. (define-key ada-mode-map "\C-c;" 'comment-region)
  3938. (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
  3939. ;; The following keys are bound to functions defined in ada-xref.el or
  3940. ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
  3941. ;; and activated only if the right compiler is used
  3942. (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
  3943. 'ada-point-and-xref)
  3944. (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
  3945. (define-key ada-mode-extra-map "o" 'ff-find-other-file)
  3946. (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
  3947. (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
  3948. (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
  3949. (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
  3950. (define-key ada-mode-extra-map "c" 'ada-change-prj)
  3951. (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
  3952. (define-key ada-mode-extra-map "g" 'ada-gdb-application)
  3953. (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
  3954. (define-key ada-mode-extra-map "r" 'ada-run-application)
  3955. (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
  3956. (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
  3957. (define-key ada-mode-extra-map "l" 'ada-find-local-references)
  3958. (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
  3959. (define-key ada-mode-extra-map "f" 'ada-find-file)
  3960. (define-key ada-mode-extra-map "u" 'ada-prj-edit)
  3961. (define-key ada-mode-map "\C-xnd" 'ada-narrow-to-defun); override narrow-to-defun
  3962. ;; The templates, defined in ada-stmt.el
  3963. (let ((map (make-sparse-keymap)))
  3964. (define-key map "h" 'ada-header)
  3965. (define-key map "\C-a" 'ada-array)
  3966. (define-key map "b" 'ada-exception-block)
  3967. (define-key map "d" 'ada-declare-block)
  3968. (define-key map "c" 'ada-case)
  3969. (define-key map "\C-e" 'ada-elsif)
  3970. (define-key map "e" 'ada-else)
  3971. (define-key map "\C-k" 'ada-package-spec)
  3972. (define-key map "k" 'ada-package-body)
  3973. (define-key map "\C-p" 'ada-procedure-spec)
  3974. (define-key map "p" 'ada-subprogram-body)
  3975. (define-key map "\C-f" 'ada-function-spec)
  3976. (define-key map "f" 'ada-for-loop)
  3977. (define-key map "i" 'ada-if)
  3978. (define-key map "l" 'ada-loop)
  3979. (define-key map "\C-r" 'ada-record)
  3980. (define-key map "\C-s" 'ada-subtype)
  3981. (define-key map "S" 'ada-tabsize)
  3982. (define-key map "\C-t" 'ada-task-spec)
  3983. (define-key map "t" 'ada-task-body)
  3984. (define-key map "\C-y" 'ada-type)
  3985. (define-key map "\C-v" 'ada-private)
  3986. (define-key map "u" 'ada-use)
  3987. (define-key map "\C-u" 'ada-with)
  3988. (define-key map "\C-w" 'ada-when)
  3989. (define-key map "w" 'ada-while-loop)
  3990. (define-key map "\C-x" 'ada-exception)
  3991. (define-key map "x" 'ada-exit)
  3992. (define-key ada-mode-extra-map "t" map))
  3993. )
  3994. (defun ada-create-menu ()
  3995. "Create the Ada menu as shown in the menu bar."
  3996. (let ((m '("Ada"
  3997. ("Help"
  3998. ["Ada Mode" (info "ada-mode") t]
  3999. ["GNAT User's Guide" (info "gnat_ugn")
  4000. (eq ada-which-compiler 'gnat)]
  4001. ["GNAT Reference Manual" (info "gnat_rm")
  4002. (eq ada-which-compiler 'gnat)]
  4003. ["Gcc Documentation" (info "gcc")
  4004. (eq ada-which-compiler 'gnat)]
  4005. ["Gdb Documentation" (info "gdb")
  4006. (eq ada-which-compiler 'gnat)]
  4007. ["Ada95 Reference Manual" (info "arm95") t])
  4008. ("Options" :included (derived-mode-p 'ada-mode)
  4009. ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
  4010. :style toggle :selected ada-auto-case]
  4011. ["Auto Indent After Return"
  4012. (setq ada-indent-after-return (not ada-indent-after-return))
  4013. :style toggle :selected ada-indent-after-return]
  4014. ["Automatically Recompile For Cross-references"
  4015. (setq ada-xref-create-ali (not ada-xref-create-ali))
  4016. :style toggle :selected ada-xref-create-ali
  4017. :included (eq ada-which-compiler 'gnat)]
  4018. ["Confirm Commands"
  4019. (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
  4020. :style toggle :selected ada-xref-confirm-compile
  4021. :included (eq ada-which-compiler 'gnat)]
  4022. ["Show Cross-references In Other Buffer"
  4023. (setq ada-xref-other-buffer (not ada-xref-other-buffer))
  4024. :style toggle :selected ada-xref-other-buffer
  4025. :included (eq ada-which-compiler 'gnat)]
  4026. ["Tight Integration With GNU Visual Debugger"
  4027. (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
  4028. :style toggle :selected ada-tight-gvd-integration
  4029. :included (string-match "gvd" ada-prj-default-debugger)])
  4030. ["Customize" (customize-group 'ada)
  4031. :included (fboundp 'customize-group)]
  4032. ["Check file" ada-check-current t]
  4033. ["Compile file" ada-compile-current t]
  4034. ["Set main and Build" ada-set-main-compile-application t]
  4035. ["Show main" ada-show-current-main t]
  4036. ["Build" ada-compile-application t]
  4037. ["Run" ada-run-application t]
  4038. ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
  4039. ["------" nil nil]
  4040. ("Project"
  4041. ["Show project" ada-show-current-project t]
  4042. ["Load..." ada-set-default-project-file t]
  4043. ["New..." ada-prj-new t]
  4044. ["Edit..." ada-prj-edit t])
  4045. ("Goto" :included (derived-mode-p 'ada-mode)
  4046. ["Goto Declaration/Body" ada-goto-declaration
  4047. (eq ada-which-compiler 'gnat)]
  4048. ["Goto Body" ada-goto-body
  4049. (eq ada-which-compiler 'gnat)]
  4050. ["Goto Declaration Other Frame"
  4051. ada-goto-declaration-other-frame
  4052. (eq ada-which-compiler 'gnat)]
  4053. ["Goto Previous Reference" ada-xref-goto-previous-reference
  4054. (eq ada-which-compiler 'gnat)]
  4055. ["List Local References" ada-find-local-references
  4056. (eq ada-which-compiler 'gnat)]
  4057. ["List References" ada-find-references
  4058. (eq ada-which-compiler 'gnat)]
  4059. ["Goto Reference To Any Entity" ada-find-any-references
  4060. (eq ada-which-compiler 'gnat)]
  4061. ["Goto Parent Unit" ada-goto-parent
  4062. (eq ada-which-compiler 'gnat)]
  4063. ["--" nil nil]
  4064. ["Next compilation error" next-error t]
  4065. ["Previous Package" ada-previous-package t]
  4066. ["Next Package" ada-next-package t]
  4067. ["Previous Procedure" ada-previous-procedure t]
  4068. ["Next Procedure" ada-next-procedure t]
  4069. ["Goto Start Of Statement" ada-move-to-start t]
  4070. ["Goto End Of Statement" ada-move-to-end t]
  4071. ["-" nil nil]
  4072. ["Other File" ff-find-other-file t]
  4073. ["Other File Other Window" ada-ff-other-window t])
  4074. ("Edit" :included (derived-mode-p 'ada-mode)
  4075. ["Search File On Source Path" ada-find-file t]
  4076. ["------" nil nil]
  4077. ["Complete Identifier" ada-complete-identifier t]
  4078. ["-----" nil nil]
  4079. ["Indent Line" ada-indent-current-function t]
  4080. ["Justify Current Indentation" ada-justified-indent-current t]
  4081. ["Indent Lines in Selection" ada-indent-region t]
  4082. ["Indent Lines in File"
  4083. (ada-indent-region (point-min) (point-max)) t]
  4084. ["Format Parameter List" ada-format-paramlist t]
  4085. ["-" nil nil]
  4086. ["Comment Selection" comment-region t]
  4087. ["Uncomment Selection" ada-uncomment-region t]
  4088. ["--" nil nil]
  4089. ["Fill Comment Paragraph" fill-paragraph t]
  4090. ["Fill Comment Paragraph Justify"
  4091. ada-fill-comment-paragraph-justify t]
  4092. ["Fill Comment Paragraph Postfix"
  4093. ada-fill-comment-paragraph-postfix t]
  4094. ["---" nil nil]
  4095. ["Adjust Case Selection" ada-adjust-case-region t]
  4096. ["Adjust Case in File" ada-adjust-case-buffer t]
  4097. ["Create Case Exception" ada-create-case-exception t]
  4098. ["Create Case Exception Substring"
  4099. ada-create-case-exception-substring t]
  4100. ["Reload Case Exceptions" ada-case-read-exceptions t]
  4101. ["----" nil nil]
  4102. ["Make body for subprogram" ada-make-subprogram-body t]
  4103. ["-----" nil nil]
  4104. ["Narrow to subprogram" ada-narrow-to-defun t])
  4105. ("Templates"
  4106. :included (derived-mode-p 'ada-mode)
  4107. ["Header" ada-header t]
  4108. ["-" nil nil]
  4109. ["Package Body" ada-package-body t]
  4110. ["Package Spec" ada-package-spec t]
  4111. ["Function Spec" ada-function-spec t]
  4112. ["Procedure Spec" ada-procedure-spec t]
  4113. ["Proc/func Body" ada-subprogram-body t]
  4114. ["Task Body" ada-task-body t]
  4115. ["Task Spec" ada-task-spec t]
  4116. ["Declare Block" ada-declare-block t]
  4117. ["Exception Block" ada-exception-block t]
  4118. ["--" nil nil]
  4119. ["Entry" ada-entry t]
  4120. ["Entry family" ada-entry-family t]
  4121. ["Select" ada-select t]
  4122. ["Accept" ada-accept t]
  4123. ["Or accept" ada-or-accept t]
  4124. ["Or delay" ada-or-delay t]
  4125. ["Or terminate" ada-or-terminate t]
  4126. ["---" nil nil]
  4127. ["Type" ada-type t]
  4128. ["Private" ada-private t]
  4129. ["Subtype" ada-subtype t]
  4130. ["Record" ada-record t]
  4131. ["Array" ada-array t]
  4132. ["----" nil nil]
  4133. ["If" ada-if t]
  4134. ["Else" ada-else t]
  4135. ["Elsif" ada-elsif t]
  4136. ["Case" ada-case t]
  4137. ["-----" nil nil]
  4138. ["While Loop" ada-while-loop t]
  4139. ["For Loop" ada-for-loop t]
  4140. ["Loop" ada-loop t]
  4141. ["------" nil nil]
  4142. ["Exception" ada-exception t]
  4143. ["Exit" ada-exit t]
  4144. ["When" ada-when t])
  4145. )))
  4146. (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
  4147. (if (featurep 'xemacs)
  4148. (progn
  4149. (define-key ada-mode-map [menu-bar] ada-mode-menu)
  4150. (setq mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
  4151. ;; -------------------------------------------------------
  4152. ;; Commenting/Uncommenting code
  4153. ;; The following two calls are provided to enhance the standard
  4154. ;; comment-region function, which only allows uncommenting if the
  4155. ;; comment is at the beginning of a line. If the line have been re-indented,
  4156. ;; we are unable to use comment-region, which makes no sense.
  4157. ;;
  4158. ;; In addition, we provide an interface to the standard comment handling
  4159. ;; function for justifying the comments.
  4160. ;; -------------------------------------------------------
  4161. (defadvice comment-region (before ada-uncomment-anywhere disable)
  4162. (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
  4163. ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
  4164. (derived-mode-p 'ada-mode))
  4165. (save-excursion
  4166. (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
  4167. (goto-char beg)
  4168. (while (re-search-forward cs end t)
  4169. (replace-match comment-start))
  4170. ))))
  4171. (defun ada-uncomment-region (beg end &optional arg)
  4172. "Uncomment region BEG .. END.
  4173. ARG gives number of comment characters."
  4174. (interactive "r\nP")
  4175. ;; This advice is not needed anymore with Emacs21. However, for older
  4176. ;; versions, as well as for XEmacs, we still need to enable it.
  4177. (if (or (<= emacs-major-version 20) (featurep 'xemacs))
  4178. (progn
  4179. (ad-activate 'comment-region)
  4180. (comment-region beg end (- (or arg 2)))
  4181. (ad-deactivate 'comment-region))
  4182. (comment-region beg end (list (- (or arg 2))))
  4183. (ada-indent-region beg end)))
  4184. (defun ada-fill-comment-paragraph-justify ()
  4185. "Fill current comment paragraph and justify each line as well."
  4186. (interactive)
  4187. (ada-fill-comment-paragraph 'full))
  4188. (defun ada-fill-comment-paragraph-postfix ()
  4189. "Fill current comment paragraph and justify each line as well.
  4190. Adds `ada-fill-comment-postfix' at the end of each line."
  4191. (interactive)
  4192. (ada-fill-comment-paragraph 'full t))
  4193. (defun ada-fill-comment-paragraph (&optional justify postfix)
  4194. "Fill the current comment paragraph.
  4195. If JUSTIFY is non-nil, each line is justified as well.
  4196. If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
  4197. to each line filled and justified.
  4198. The paragraph is indented on the first line."
  4199. (interactive "P")
  4200. ;; check if inside comment or just in front a comment
  4201. (if (and (not (ada-in-comment-p))
  4202. (not (looking-at "[ \t]*--")))
  4203. (error "Not inside comment"))
  4204. (let* (indent from to
  4205. (opos (point-marker))
  4206. ;; Sets this variable to nil, otherwise it prevents
  4207. ;; fill-region-as-paragraph to work on Emacs <= 20.2
  4208. (parse-sexp-lookup-properties nil)
  4209. fill-prefix
  4210. (fill-column (current-fill-column)))
  4211. ;; Find end of paragraph
  4212. (back-to-indentation)
  4213. (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
  4214. (forward-line 1)
  4215. ;; If we were at the last line in the buffer, create a dummy empty
  4216. ;; line at the end of the buffer.
  4217. (if (eobp)
  4218. (insert "\n")
  4219. (back-to-indentation)))
  4220. (beginning-of-line)
  4221. (setq to (point-marker))
  4222. (goto-char opos)
  4223. ;; Find beginning of paragraph
  4224. (back-to-indentation)
  4225. (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
  4226. (forward-line -1)
  4227. (back-to-indentation))
  4228. ;; We want one line above the first one, unless we are at the beginning
  4229. ;; of the buffer
  4230. (unless (bobp)
  4231. (forward-line 1))
  4232. (beginning-of-line)
  4233. (setq from (point-marker))
  4234. ;; Calculate the indentation we will need for the paragraph
  4235. (back-to-indentation)
  4236. (setq indent (current-column))
  4237. ;; unindent the first line of the paragraph
  4238. (delete-region from (point))
  4239. ;; Remove the old postfixes
  4240. (goto-char from)
  4241. (while (re-search-forward "--\n" to t)
  4242. (replace-match "\n"))
  4243. (goto-char (1- to))
  4244. (setq to (point-marker))
  4245. ;; Indent and justify the paragraph
  4246. (setq fill-prefix ada-fill-comment-prefix)
  4247. (set-left-margin from to indent)
  4248. (if postfix
  4249. (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
  4250. (fill-region-as-paragraph from to justify)
  4251. ;; Add the postfixes if required
  4252. (if postfix
  4253. (save-restriction
  4254. (goto-char from)
  4255. (narrow-to-region from to)
  4256. (while (not (eobp))
  4257. (end-of-line)
  4258. (insert-char ? (- fill-column (current-column)))
  4259. (insert ada-fill-comment-postfix)
  4260. (forward-line))
  4261. ))
  4262. ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
  4263. ;; inserted at the end. Delete it
  4264. (if (or (featurep 'xemacs)
  4265. (<= emacs-major-version 19)
  4266. (and (= emacs-major-version 20)
  4267. (<= emacs-minor-version 2)))
  4268. (progn
  4269. (goto-char to)
  4270. (end-of-line)
  4271. (delete-char 1)))
  4272. (goto-char opos)))
  4273. ;; ---------------------------------------------------
  4274. ;; support for find-file.el
  4275. ;; These functions are used by find-file to guess the file names from
  4276. ;; unit names, and to find the other file (spec or body) from the current
  4277. ;; file (body or spec).
  4278. ;; It is also used to find in which function we are, so as to put the
  4279. ;; cursor at the correct position.
  4280. ;; Standard Ada does not force any relation between unit names and file names,
  4281. ;; so some of these functions can only be a good approximation. However, they
  4282. ;; are also overridden in `ada-xref'.el when we know that the user is using
  4283. ;; GNAT.
  4284. ;; ---------------------------------------------------
  4285. ;; Overridden when we work with GNAT, to use gnatkrunch
  4286. (defun ada-make-filename-from-adaname (adaname)
  4287. "Determine the filename in which ADANAME is found.
  4288. This matches the GNAT default naming convention, except for
  4289. pre-defined units."
  4290. (while (string-match "\\." adaname)
  4291. (setq adaname (replace-match "-" t t adaname)))
  4292. (downcase adaname)
  4293. )
  4294. (defun ada-other-file-name ()
  4295. "Return the name of the other file.
  4296. The name returned is the body if `current-buffer' is the spec,
  4297. or the spec otherwise."
  4298. (let ((is-spec nil)
  4299. (is-body nil)
  4300. (suffixes ada-spec-suffixes)
  4301. (name (buffer-file-name)))
  4302. ;; Guess whether we have a spec or a body, and get the basename of the
  4303. ;; file. Since the extension may not start with '.', we can not use
  4304. ;; file-name-extension
  4305. (while (and (not is-spec)
  4306. suffixes)
  4307. (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
  4308. (setq is-spec t
  4309. name (match-string 1 name)))
  4310. (setq suffixes (cdr suffixes)))
  4311. (if (not is-spec)
  4312. (progn
  4313. (setq suffixes ada-body-suffixes)
  4314. (while (and (not is-body)
  4315. suffixes)
  4316. (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
  4317. (setq is-body t
  4318. name (match-string 1 name)))
  4319. (setq suffixes (cdr suffixes)))))
  4320. ;; If this wasn't in either list, return name itself
  4321. (if (not (or is-spec is-body))
  4322. name
  4323. ;; Else find the other possible names
  4324. (if is-spec
  4325. (setq suffixes ada-body-suffixes)
  4326. (setq suffixes ada-spec-suffixes))
  4327. (setq is-spec name)
  4328. (while suffixes
  4329. ;; If we are using project file, search for the other file in all
  4330. ;; the possible src directories.
  4331. (if (fboundp 'ada-find-src-file-in-dir)
  4332. (let ((other
  4333. (ada-find-src-file-in-dir
  4334. (file-name-nondirectory (concat name (car suffixes))))))
  4335. (if other
  4336. (setq is-spec other)))
  4337. ;; Else search in the current directory
  4338. (if (file-exists-p (concat name (car suffixes)))
  4339. (setq is-spec (concat name (car suffixes)))))
  4340. (setq suffixes (cdr suffixes)))
  4341. is-spec)))
  4342. (defun ada-which-function-are-we-in ()
  4343. "Return the name of the function whose definition/declaration point is in.
  4344. Used in `ff-pre-load-hook'."
  4345. (setq ff-function-name nil)
  4346. (save-excursion
  4347. (end-of-line);; make sure we get the complete name
  4348. (or (if (re-search-backward ada-procedure-start-regexp nil t)
  4349. (setq ff-function-name (match-string 5)))
  4350. (if (re-search-backward ada-package-start-regexp nil t)
  4351. (setq ff-function-name (match-string 4))))
  4352. ))
  4353. (defvar ada-last-which-function-line -1
  4354. "Last line on which `ada-which-function' was called.")
  4355. (defvar ada-last-which-function-subprog 0
  4356. "Last subprogram name returned by `ada-which-function'.")
  4357. (make-variable-buffer-local 'ada-last-which-function-subprog)
  4358. (make-variable-buffer-local 'ada-last-which-function-line)
  4359. (defun ada-which-function ()
  4360. "Return the name of the function whose body the point is in.
  4361. This function works even in the case of nested subprograms, whereas the
  4362. standard Emacs function `which-function' does not.
  4363. Since the search can be long, the results are cached."
  4364. (let ((line (count-lines 1 (point)))
  4365. (pos (point))
  4366. end-pos
  4367. func-name indent
  4368. found)
  4369. ;; If this is the same line as before, simply return the same result
  4370. (if (= line ada-last-which-function-line)
  4371. ada-last-which-function-subprog
  4372. (save-excursion
  4373. ;; In case the current line is also the beginning of the body
  4374. (end-of-line)
  4375. ;; Are we looking at "function Foo\n (paramlist)"
  4376. (skip-chars-forward " \t\n(")
  4377. (condition-case nil
  4378. (up-list 1)
  4379. (error nil))
  4380. (skip-chars-forward " \t\n")
  4381. (if (looking-at "return")
  4382. (progn
  4383. (forward-word 1)
  4384. (skip-chars-forward " \t\n")
  4385. (skip-chars-forward "a-zA-Z0-9_'")))
  4386. ;; Can't simply do forward-word, in case the "is" is not on the
  4387. ;; same line as the closing parenthesis
  4388. (skip-chars-forward "is \t\n")
  4389. ;; No look for the closest subprogram body that has not ended yet.
  4390. ;; Not that we expect all the bodies to be finished by "end <name>",
  4391. ;; or a simple "end;" indented in the same column as the start of
  4392. ;; the subprogram. The goal is to be as efficient as possible.
  4393. (while (and (not found)
  4394. (re-search-backward ada-imenu-subprogram-menu-re nil t))
  4395. ;; Get the function name, but not the properties, or this changes
  4396. ;; the face in the modeline on Emacs 21
  4397. (setq func-name (match-string-no-properties 3))
  4398. (if (and (not (ada-in-comment-p))
  4399. (not (save-excursion
  4400. (goto-char (match-end 0))
  4401. (looking-at "[ \t\n]*new"))))
  4402. (save-excursion
  4403. (back-to-indentation)
  4404. (setq indent (current-column))
  4405. (if (ada-search-ignore-string-comment
  4406. (concat "end[ \t]+" func-name "[ \t]*;\\|^"
  4407. (make-string indent ? ) "end;"))
  4408. (setq end-pos (point))
  4409. (setq end-pos (point-max)))
  4410. (if (>= end-pos pos)
  4411. (setq found func-name))))
  4412. )
  4413. (setq ada-last-which-function-line line
  4414. ada-last-which-function-subprog found)
  4415. found))))
  4416. (defun ada-ff-other-window ()
  4417. "Find other file in other window using `ff-find-other-file'."
  4418. (interactive)
  4419. (and (fboundp 'ff-find-other-file)
  4420. (ff-find-other-file t)))
  4421. (defun ada-set-point-accordingly ()
  4422. "Move to the function declaration that was set by `ff-which-function-are-we-in'."
  4423. (if ff-function-name
  4424. (progn
  4425. (goto-char (point-min))
  4426. (unless (ada-search-ignore-string-comment
  4427. (concat ff-function-name "\\b") nil)
  4428. (goto-char (point-min))))))
  4429. (defun ada-get-body-name (&optional spec-name)
  4430. "Return the file name for the body of SPEC-NAME.
  4431. If SPEC-NAME is nil, return the body for the current package.
  4432. Return nil if no body was found."
  4433. (interactive)
  4434. (unless spec-name (setq spec-name (buffer-file-name)))
  4435. ;; Remove the spec extension. We can not simply remove the file extension,
  4436. ;; but we need to take into account the specific non-GNAT extensions that the
  4437. ;; user might have specified.
  4438. (let ((suffixes ada-spec-suffixes)
  4439. end)
  4440. (while suffixes
  4441. (setq end (- (length spec-name) (length (car suffixes))))
  4442. (if (string-equal (car suffixes) (substring spec-name end))
  4443. (setq spec-name (substring spec-name 0 end)))
  4444. (setq suffixes (cdr suffixes))))
  4445. ;; If find-file.el was available, use its functions
  4446. (if (fboundp 'ff-get-file-name)
  4447. (ff-get-file-name ada-search-directories-internal
  4448. (ada-make-filename-from-adaname
  4449. (file-name-nondirectory
  4450. (file-name-sans-extension spec-name)))
  4451. ada-body-suffixes)
  4452. ;; Else emulate it very simply
  4453. (concat (ada-make-filename-from-adaname
  4454. (file-name-nondirectory
  4455. (file-name-sans-extension spec-name)))
  4456. ".adb")))
  4457. ;; ---------------------------------------------------
  4458. ;; support for font-lock.el
  4459. ;; Strings are a real pain in Ada because a single quote character is
  4460. ;; overloaded as a string quote and type/instance delimiter. By default, a
  4461. ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
  4462. ;; So, for Font Lock mode purposes, we mark single quotes as having string
  4463. ;; syntax when the gods that created Ada determine them to be.
  4464. ;;
  4465. ;; This only works in Emacs. See the comments before the grammar functions
  4466. ;; at the beginning of this file for how this is done with XEmacs.
  4467. ;; ----------------------------------------------------
  4468. (defconst ada-font-lock-syntactic-keywords
  4469. ;; Mark single quotes as having string quote syntax in 'c' instances.
  4470. ;; We used to explicitly avoid ''' as a special case for fear the buffer
  4471. ;; be highlighted as a string, but it seems this fear is unfounded.
  4472. ;;
  4473. ;; This sets the properties of the characters, so that ada-in-string-p
  4474. ;; correctly handles '"' too...
  4475. '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
  4476. ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
  4477. (defvar ada-font-lock-keywords
  4478. (eval-when-compile
  4479. (list
  4480. ;;
  4481. ;; handle "type T is access function return S;"
  4482. (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
  4483. ;; preprocessor line
  4484. (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
  4485. ;;
  4486. ;; accept, entry, function, package (body), protected (body|type),
  4487. ;; pragma, procedure, task (body) plus name.
  4488. (list (concat
  4489. "\\<\\("
  4490. "accept\\|"
  4491. "entry\\|"
  4492. "function\\|"
  4493. "package[ \t]+body\\|"
  4494. "package\\|"
  4495. "pragma\\|"
  4496. "procedure\\|"
  4497. "protected[ \t]+body\\|"
  4498. "protected[ \t]+type\\|"
  4499. "protected\\|"
  4500. "task[ \t]+body\\|"
  4501. "task[ \t]+type\\|"
  4502. "task"
  4503. "\\)\\>[ \t]*"
  4504. "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
  4505. '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
  4506. ;;
  4507. ;; Optional keywords followed by a type name.
  4508. (list (concat ; ":[ \t]*"
  4509. "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
  4510. "[ \t]*"
  4511. "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
  4512. '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
  4513. ;;
  4514. ;; Main keywords, except those treated specially below.
  4515. (concat "\\<"
  4516. (regexp-opt
  4517. '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
  4518. "and" "array" "at" "begin" "case" "declare" "delay" "delta"
  4519. "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
  4520. "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
  4521. "null" "or" "others" "overriding" "private" "protected" "raise"
  4522. "range" "record" "rem" "renames" "requeue" "return" "reverse"
  4523. "select" "separate" "synchronized" "tagged" "task" "terminate"
  4524. "then" "until" "when" "while" "with" "xor") t)
  4525. "\\>")
  4526. ;;
  4527. ;; Anything following end and not already fontified is a body name.
  4528. '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
  4529. (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
  4530. ;;
  4531. ;; Keywords followed by a type or function name.
  4532. (list (concat "\\<\\("
  4533. "new\\|of\\|subtype\\|type"
  4534. "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
  4535. '(1 font-lock-keyword-face)
  4536. '(2 (if (match-beginning 4)
  4537. font-lock-function-name-face
  4538. font-lock-type-face) nil t))
  4539. ;;
  4540. ;; Keywords followed by a (comma separated list of) reference.
  4541. ;; Note that font-lock only works on single lines, thus we can not
  4542. ;; correctly highlight a with_clause that spans multiple lines.
  4543. (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
  4544. "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
  4545. '(1 font-lock-keyword-face) '(2 font-lock-reference-face nil t))
  4546. ;;
  4547. ;; Goto tags.
  4548. '("<<\\(\\sw+\\)>>" 1 font-lock-reference-face)
  4549. ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
  4550. (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
  4551. ;; Ada unnamed numerical constants
  4552. (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
  4553. ))
  4554. "Default expressions to highlight in Ada mode.")
  4555. ;; ---------------------------------------------------------
  4556. ;; Support for outline.el
  4557. ;; ---------------------------------------------------------
  4558. (defun ada-outline-level ()
  4559. "This is so that `current-column' DTRT in otherwise-hidden text."
  4560. ;; patch from Dave Love <fx@gnu.org>
  4561. (let (buffer-invisibility-spec)
  4562. (save-excursion
  4563. (back-to-indentation)
  4564. (current-column))))
  4565. ;; ---------------------------------------------------------
  4566. ;; Support for narrow-to-region
  4567. ;; ---------------------------------------------------------
  4568. (defun ada-narrow-to-defun (&optional _arg)
  4569. "Make text outside current subprogram invisible.
  4570. The subprogram visible is the one that contains or follow point.
  4571. Optional ARG is ignored.
  4572. Use \\[widen] to go back to the full visibility for the buffer."
  4573. (interactive)
  4574. (save-excursion
  4575. (let (end)
  4576. (widen)
  4577. (forward-line 1)
  4578. (ada-previous-procedure)
  4579. (setq end (point-at-bol))
  4580. (ada-move-to-end)
  4581. (end-of-line)
  4582. (narrow-to-region end (point))
  4583. (message
  4584. "Use M-x widen to get back to full visibility in the buffer"))))
  4585. ;; ---------------------------------------------------------
  4586. ;; Automatic generation of code
  4587. ;; The Ada mode has a set of function to automatically generate a subprogram
  4588. ;; or package body from its spec.
  4589. ;; These function only use a primary and basic algorithm, this could use a
  4590. ;; lot of improvement.
  4591. ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
  4592. ;; body.
  4593. ;; ----------------------------------------------------------
  4594. (defun ada-gen-treat-proc (match)
  4595. "Make dummy body of a procedure/function specification.
  4596. MATCH is a cons cell containing the start and end locations of the last search
  4597. for `ada-procedure-start-regexp'."
  4598. (goto-char (car match))
  4599. (let (func-found procname functype)
  4600. (cond
  4601. ((or (looking-at "^[ \t]*procedure")
  4602. (setq func-found (looking-at "^[ \t]*function")))
  4603. ;; treat it as a proc/func
  4604. (forward-word 2)
  4605. (forward-word -1)
  4606. (setq procname (buffer-substring (point) (cdr match))) ; store proc name
  4607. ;; goto end of procname
  4608. (goto-char (cdr match))
  4609. ;; skip over parameterlist
  4610. (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
  4611. (forward-sexp))
  4612. ;; if function, skip over 'return' and result type.
  4613. (if func-found
  4614. (progn
  4615. (forward-word 1)
  4616. (skip-chars-forward " \t\n")
  4617. (setq functype (buffer-substring (point)
  4618. (progn
  4619. (skip-chars-forward
  4620. "a-zA-Z0-9_\.")
  4621. (point))))))
  4622. ;; look for next non WS
  4623. (cond
  4624. ((looking-at "[ \t]*;")
  4625. (delete-region (match-beginning 0) (match-end 0));; delete the ';'
  4626. (ada-indent-newline-indent)
  4627. (insert "is")
  4628. (ada-indent-newline-indent)
  4629. (if func-found
  4630. (progn
  4631. (insert "Result : " functype ";")
  4632. (ada-indent-newline-indent)))
  4633. (insert "begin")
  4634. (ada-indent-newline-indent)
  4635. (if func-found
  4636. (insert "return Result;")
  4637. (insert "null;"))
  4638. (ada-indent-newline-indent)
  4639. (insert "end " procname ";")
  4640. (ada-indent-newline-indent)
  4641. )
  4642. ((looking-at "[ \t\n]*is")
  4643. ;; do nothing
  4644. )
  4645. ((looking-at "[ \t\n]*rename")
  4646. ;; do nothing
  4647. )
  4648. (t
  4649. (message "unknown syntax"))))
  4650. (t
  4651. (if (looking-at "^[ \t]*task")
  4652. (progn
  4653. (message "Task conversion is not yet implemented")
  4654. (forward-word 2)
  4655. (if (looking-at "[ \t]*;")
  4656. (forward-line)
  4657. (ada-move-to-end))
  4658. ))))))
  4659. (defun ada-make-body ()
  4660. "Create an Ada package body in the current buffer.
  4661. The spec must be the previously visited buffer.
  4662. This function typically is to be hooked into `ff-file-created-hook'."
  4663. (delete-region (point-min) (point-max))
  4664. (insert-buffer-substring (car (cdr (buffer-list))))
  4665. (goto-char (point-min))
  4666. (ada-mode)
  4667. (let (found ada-procedure-or-package-start-regexp)
  4668. (if (setq found
  4669. (ada-search-ignore-string-comment ada-package-start-regexp nil))
  4670. (progn (goto-char (cdr found))
  4671. (insert " body")
  4672. )
  4673. (error "No package"))
  4674. (setq ada-procedure-or-package-start-regexp
  4675. (concat ada-procedure-start-regexp
  4676. "\\|"
  4677. ada-package-start-regexp))
  4678. (while (setq found
  4679. (ada-search-ignore-string-comment
  4680. ada-procedure-or-package-start-regexp nil))
  4681. (progn
  4682. (goto-char (car found))
  4683. (if (looking-at ada-package-start-regexp)
  4684. (progn (goto-char (cdr found))
  4685. (insert " body"))
  4686. (ada-gen-treat-proc found))))))
  4687. (defun ada-make-subprogram-body ()
  4688. "Create a dummy subprogram body in package body file from spec surrounding point."
  4689. (interactive)
  4690. (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
  4691. (spec (match-beginning 0))
  4692. body-file)
  4693. (if found
  4694. (progn
  4695. (goto-char spec)
  4696. (if (and (re-search-forward "(\\|;" nil t)
  4697. (= (char-before) ?\())
  4698. (progn
  4699. (ada-search-ignore-string-comment ")" nil)
  4700. (ada-search-ignore-string-comment ";" nil)))
  4701. (setq spec (buffer-substring spec (point)))
  4702. ;; If find-file.el was available, use its functions
  4703. (setq body-file (ada-get-body-name))
  4704. (if body-file
  4705. (find-file body-file)
  4706. (error "No body found for the package. Create it first"))
  4707. (save-restriction
  4708. (widen)
  4709. (goto-char (point-max))
  4710. (forward-comment -10000)
  4711. (re-search-backward "\\<end\\>" nil t)
  4712. ;; Move to the beginning of the elaboration part, if any
  4713. (re-search-backward "^begin" nil t)
  4714. (newline)
  4715. (forward-char -1)
  4716. (insert spec)
  4717. (re-search-backward ada-procedure-start-regexp nil t)
  4718. (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
  4719. ))
  4720. (error "Not in subprogram spec"))))
  4721. ;; --------------------------------------------------------
  4722. ;; Global initializations
  4723. ;; --------------------------------------------------------
  4724. ;; Create the keymap once and for all. If we do that in ada-mode,
  4725. ;; the keys changed in the user's .emacs have to be modified
  4726. ;; every time
  4727. (ada-create-keymap)
  4728. (ada-create-menu)
  4729. ;; Create the syntax tables, but do not activate them
  4730. (ada-create-syntax-table)
  4731. ;; Add the default extensions (and set up speedbar)
  4732. (ada-add-extensions ".ads" ".adb")
  4733. ;; This two files are generated by GNAT when running with -gnatD
  4734. (if (equal ada-which-compiler 'gnat)
  4735. (ada-add-extensions ".ads.dg" ".adb.dg"))
  4736. ;; Read the special cases for exceptions
  4737. (ada-case-read-exceptions)
  4738. ;; Setup auto-loading of the other Ada mode files.
  4739. (autoload 'ada-change-prj "ada-xref" nil t)
  4740. (autoload 'ada-check-current "ada-xref" nil t)
  4741. (autoload 'ada-compile-application "ada-xref" nil t)
  4742. (autoload 'ada-compile-current "ada-xref" nil t)
  4743. (autoload 'ada-complete-identifier "ada-xref" nil t)
  4744. (autoload 'ada-find-file "ada-xref" nil t)
  4745. (autoload 'ada-find-any-references "ada-xref" nil t)
  4746. (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
  4747. (autoload 'ada-find-local-references "ada-xref" nil t)
  4748. (autoload 'ada-find-references "ada-xref" nil t)
  4749. (autoload 'ada-gdb-application "ada-xref" nil t)
  4750. (autoload 'ada-goto-declaration "ada-xref" nil t)
  4751. (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
  4752. (autoload 'ada-goto-parent "ada-xref" nil t)
  4753. (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
  4754. (autoload 'ada-point-and-xref "ada-xref" nil t)
  4755. (autoload 'ada-reread-prj-file "ada-xref" nil t)
  4756. (autoload 'ada-run-application "ada-xref" nil t)
  4757. (autoload 'ada-set-default-project-file "ada-xref" nil t)
  4758. (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
  4759. (autoload 'ada-set-main-compile-application "ada-xref" nil t)
  4760. (autoload 'ada-show-current-main "ada-xref" nil t)
  4761. (autoload 'ada-customize "ada-prj" nil t)
  4762. (autoload 'ada-prj-edit "ada-prj" nil t)
  4763. (autoload 'ada-prj-new "ada-prj" nil t)
  4764. (autoload 'ada-prj-save "ada-prj" nil t)
  4765. (autoload 'ada-array "ada-stmt" nil t)
  4766. (autoload 'ada-case "ada-stmt" nil t)
  4767. (autoload 'ada-declare-block "ada-stmt" nil t)
  4768. (autoload 'ada-else "ada-stmt" nil t)
  4769. (autoload 'ada-elsif "ada-stmt" nil t)
  4770. (autoload 'ada-exception "ada-stmt" nil t)
  4771. (autoload 'ada-exception-block "ada-stmt" nil t)
  4772. (autoload 'ada-exit "ada-stmt" nil t)
  4773. (autoload 'ada-for-loop "ada-stmt" nil t)
  4774. (autoload 'ada-function-spec "ada-stmt" nil t)
  4775. (autoload 'ada-header "ada-stmt" nil t)
  4776. (autoload 'ada-if "ada-stmt" nil t)
  4777. (autoload 'ada-loop "ada-stmt" nil t)
  4778. (autoload 'ada-package-body "ada-stmt" nil t)
  4779. (autoload 'ada-package-spec "ada-stmt" nil t)
  4780. (autoload 'ada-private "ada-stmt" nil t)
  4781. (autoload 'ada-procedure-spec "ada-stmt" nil t)
  4782. (autoload 'ada-record "ada-stmt" nil t)
  4783. (autoload 'ada-subprogram-body "ada-stmt" nil t)
  4784. (autoload 'ada-subtype "ada-stmt" nil t)
  4785. (autoload 'ada-tabsize "ada-stmt" nil t)
  4786. (autoload 'ada-task-body "ada-stmt" nil t)
  4787. (autoload 'ada-task-spec "ada-stmt" nil t)
  4788. (autoload 'ada-type "ada-stmt" nil t)
  4789. (autoload 'ada-use "ada-stmt" nil t)
  4790. (autoload 'ada-when "ada-stmt" nil t)
  4791. (autoload 'ada-while-loop "ada-stmt" nil t)
  4792. (autoload 'ada-with "ada-stmt" nil t)
  4793. ;;; provide ourselves
  4794. (provide 'ada-mode)
  4795. ;;; ada-mode.el ends here