ada-mode.el 182 KB

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