ada-mode.el 183 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491
  1. ;;; ada-mode.el --- major-mode for editing Ada sources
  2. ;; Copyright (C) 1994-1995, 1997-2017 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-strictly 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-strictly -1)
  1132. (point))
  1133. (goto-char aa-end)))))
  1134. (defun ada-region-selected ()
  1135. "Should we operate on an active region?"
  1136. (if (fboundp 'use-region-p)
  1137. (use-region-p)
  1138. (region-active-p)))
  1139. ;;-----------------------------------------------------------------
  1140. ;; auto-casing
  1141. ;; Since Ada is case-insensitive, the Ada mode provides an extensive set of
  1142. ;; functions to auto-case identifiers, keywords, ...
  1143. ;; The basic rules for autocasing are defined through the variables
  1144. ;; `ada-case-attribute', `ada-case-keyword' and `ada-case-identifier'. These
  1145. ;; are references to the functions that will do the actual casing.
  1146. ;;
  1147. ;; However, in most cases, the user will want to define some exceptions to
  1148. ;; these casing rules. This is done through a list of files, that contain
  1149. ;; one word per line. These files are stored in `ada-case-exception-file'.
  1150. ;; For backward compatibility, this variable can also be a string.
  1151. ;;-----------------------------------------------------------------
  1152. (defun ada-save-exceptions-to-file (file-name)
  1153. "Save the casing exception lists to the file FILE-NAME.
  1154. Casing exception lists are `ada-case-exception' and `ada-case-exception-substring'."
  1155. (find-file (expand-file-name file-name))
  1156. (erase-buffer)
  1157. (mapc (lambda (x) (insert (car x) "\n"))
  1158. (sort (copy-sequence ada-case-exception)
  1159. (lambda(a b) (string< (car a) (car b)))))
  1160. (mapc (lambda (x) (insert "*" (car x) "\n"))
  1161. (sort (copy-sequence ada-case-exception-substring)
  1162. (lambda(a b) (string< (car a) (car b)))))
  1163. (save-buffer)
  1164. (kill-buffer nil)
  1165. )
  1166. (defun ada-create-case-exception (&optional word)
  1167. "Define WORD as an exception for the casing system.
  1168. If WORD is not given, then the current word in the buffer is used instead.
  1169. The new word is added to the first file in `ada-case-exception-file'.
  1170. The standard casing rules will no longer apply to this word."
  1171. (interactive)
  1172. (let ((file-name
  1173. (cond ((stringp ada-case-exception-file)
  1174. ada-case-exception-file)
  1175. ((listp ada-case-exception-file)
  1176. (car ada-case-exception-file))
  1177. (t
  1178. (error (concat "No exception file specified. "
  1179. "See variable ada-case-exception-file"))))))
  1180. (unless word
  1181. (with-syntax-table ada-mode-symbol-syntax-table
  1182. (save-excursion
  1183. (skip-syntax-backward "w")
  1184. (setq word (buffer-substring-no-properties
  1185. (point) (save-excursion (forward-word-strictly 1)
  1186. (point)))))))
  1187. ;; Reread the exceptions file, in case it was modified by some other,
  1188. (ada-case-read-exceptions-from-file file-name)
  1189. ;; If the word is already in the list, even with a different casing
  1190. ;; we simply want to replace it.
  1191. (if (and (not (equal ada-case-exception '()))
  1192. (assoc-string word ada-case-exception t))
  1193. (setcar (assoc-string word ada-case-exception t) word)
  1194. (add-to-list 'ada-case-exception (cons word t)))
  1195. (ada-save-exceptions-to-file file-name)))
  1196. (defun ada-create-case-exception-substring (&optional word)
  1197. "Define the substring WORD as an exception for the casing system.
  1198. If WORD is not given, then the current word in the buffer is used instead,
  1199. or the selected region if any is active.
  1200. The new word is added to the first file in `ada-case-exception-file'.
  1201. When auto-casing a word, this substring will be special-cased, unless the
  1202. word itself has a special casing."
  1203. (interactive)
  1204. (let ((file-name
  1205. (cond ((stringp ada-case-exception-file)
  1206. ada-case-exception-file)
  1207. ((listp ada-case-exception-file)
  1208. (car ada-case-exception-file))
  1209. (t
  1210. (error (concat "No exception file specified. "
  1211. "See variable ada-case-exception-file"))))))
  1212. ;; Find the substring to define as an exception. Order is: the parameter,
  1213. ;; if any, or the selected region, or the word under the cursor
  1214. (cond
  1215. (word nil)
  1216. ((ada-region-selected)
  1217. (setq word (buffer-substring-no-properties
  1218. (region-beginning) (region-end))))
  1219. (t
  1220. (let ((underscore-syntax (char-syntax ?_)))
  1221. (unwind-protect
  1222. (progn
  1223. (modify-syntax-entry ?_ "." (syntax-table))
  1224. (save-excursion
  1225. (skip-syntax-backward "w")
  1226. (setq word (buffer-substring-no-properties
  1227. (point)
  1228. (save-excursion (forward-word-strictly 1)
  1229. (point))))))
  1230. (modify-syntax-entry ?_ (make-string 1 underscore-syntax)
  1231. (syntax-table))))))
  1232. ;; Reread the exceptions file, in case it was modified by some other,
  1233. (ada-case-read-exceptions-from-file file-name)
  1234. ;; If the word is already in the list, even with a different casing
  1235. ;; we simply want to replace it.
  1236. (if (and (not (equal ada-case-exception-substring '()))
  1237. (assoc-string word ada-case-exception-substring t))
  1238. (setcar (assoc-string word ada-case-exception-substring t) word)
  1239. (add-to-list 'ada-case-exception-substring (cons word t))
  1240. )
  1241. (ada-save-exceptions-to-file file-name)
  1242. (message "%s" (concat "Defining " word " as a casing exception"))))
  1243. (defun ada-case-read-exceptions-from-file (file-name)
  1244. "Read the content of the casing exception file FILE-NAME."
  1245. (if (file-readable-p (expand-file-name file-name))
  1246. (let ((buffer (current-buffer)))
  1247. (find-file (expand-file-name file-name))
  1248. (set-syntax-table ada-mode-symbol-syntax-table)
  1249. (widen)
  1250. (goto-char (point-min))
  1251. (while (not (eobp))
  1252. ;; If the item is already in the list, even with an other casing,
  1253. ;; do not add it again. This way, the user can easily decide which
  1254. ;; priority should be applied to each casing exception
  1255. (let ((word (buffer-substring-no-properties
  1256. (point) (save-excursion (forward-word-strictly 1)
  1257. (point)))))
  1258. ;; Handling a substring ?
  1259. (if (char-equal (string-to-char word) ?*)
  1260. (progn
  1261. (setq word (substring word 1))
  1262. (unless (assoc-string word ada-case-exception-substring t)
  1263. (add-to-list 'ada-case-exception-substring (cons word t))))
  1264. (unless (assoc-string word ada-case-exception t)
  1265. (add-to-list 'ada-case-exception (cons word t)))))
  1266. (forward-line 1))
  1267. (kill-buffer nil)
  1268. (set-buffer buffer)))
  1269. )
  1270. (defun ada-case-read-exceptions ()
  1271. "Read all the casing exception files from `ada-case-exception-file'."
  1272. (interactive)
  1273. ;; Reinitialize the casing exception list
  1274. (setq ada-case-exception '()
  1275. ada-case-exception-substring '())
  1276. (cond ((stringp ada-case-exception-file)
  1277. (ada-case-read-exceptions-from-file ada-case-exception-file))
  1278. ((listp ada-case-exception-file)
  1279. (mapcar 'ada-case-read-exceptions-from-file
  1280. ada-case-exception-file))))
  1281. (defun ada-adjust-case-substring ()
  1282. "Adjust case of substrings in the previous word."
  1283. (interactive)
  1284. (let ((substrings ada-case-exception-substring)
  1285. (max (point))
  1286. (case-fold-search t)
  1287. (underscore-syntax (char-syntax ?_))
  1288. re)
  1289. (save-excursion
  1290. (forward-word -1)
  1291. (unwind-protect
  1292. (progn
  1293. (modify-syntax-entry ?_ "." (syntax-table))
  1294. (while substrings
  1295. (setq re (concat "\\b" (regexp-quote (caar substrings)) "\\b"))
  1296. (save-excursion
  1297. (while (re-search-forward re max t)
  1298. (replace-match (caar substrings) t)))
  1299. (setq substrings (cdr substrings))
  1300. )
  1301. )
  1302. (modify-syntax-entry ?_ (make-string 1 underscore-syntax) (syntax-table)))
  1303. )))
  1304. (defun ada-adjust-case-identifier ()
  1305. "Adjust case of the previous identifier.
  1306. The auto-casing is done according to the value of `ada-case-identifier'
  1307. and the exceptions defined in `ada-case-exception-file'."
  1308. (interactive)
  1309. (if (or (equal ada-case-exception '())
  1310. (equal (char-after) ?_))
  1311. (progn
  1312. (funcall ada-case-identifier -1)
  1313. (ada-adjust-case-substring))
  1314. (progn
  1315. (let ((end (point))
  1316. (start (save-excursion (skip-syntax-backward "w")
  1317. (point)))
  1318. match)
  1319. ;; If we have an exception, replace the word by the correct casing
  1320. (if (setq match (assoc-string (buffer-substring start end)
  1321. ada-case-exception t))
  1322. (progn
  1323. (delete-region start end)
  1324. (insert (car match)))
  1325. ;; Else simply re-case the word
  1326. (funcall ada-case-identifier -1)
  1327. (ada-adjust-case-substring))))))
  1328. (defun ada-after-keyword-p ()
  1329. "Return t if cursor is after a keyword that is not an attribute."
  1330. (save-excursion
  1331. (forward-word-strictly -1)
  1332. (and (not (and (char-before)
  1333. (or (= (char-before) ?_)
  1334. (= (char-before) ?'))));; unless we have a _ or '
  1335. (looking-at (concat ada-keywords "[^_]")))))
  1336. (defun ada-adjust-case (&optional force-identifier)
  1337. "Adjust the case of the word before the character just typed.
  1338. If FORCE-IDENTIFIER is non-nil then also adjust keyword as identifier."
  1339. (if (not (bobp))
  1340. (progn
  1341. (forward-char -1)
  1342. (if (and (not (bobp))
  1343. ;; or if at the end of a character constant
  1344. (not (and (eq (following-char) ?')
  1345. (eq (char-before (1- (point))) ?')))
  1346. ;; or if the previous character was not part of a word
  1347. (eq (char-syntax (char-before)) ?w)
  1348. ;; if in a string or a comment
  1349. (not (ada-in-string-or-comment-p))
  1350. ;; if in a numeric literal
  1351. (not (ada-in-numeric-literal-p))
  1352. )
  1353. (if (save-excursion
  1354. (forward-word -1)
  1355. (or (= (point) (point-min))
  1356. (backward-char 1))
  1357. (= (following-char) ?'))
  1358. (funcall ada-case-attribute -1)
  1359. (if (and
  1360. (not force-identifier) ; (MH)
  1361. (ada-after-keyword-p))
  1362. (funcall ada-case-keyword -1)
  1363. (ada-adjust-case-identifier))))
  1364. (forward-char 1)
  1365. ))
  1366. )
  1367. (defun ada-adjust-case-interactive (arg)
  1368. "Adjust the case of the previous word, and process the character just typed.
  1369. ARG is the prefix the user entered with \\[universal-argument]."
  1370. (interactive "P")
  1371. (if ada-auto-case
  1372. (let ((lastk last-command-event))
  1373. (with-syntax-table ada-mode-symbol-syntax-table
  1374. (cond ((memq lastk '(?\n ?\r))
  1375. ;; Horrible kludge.
  1376. (insert " ")
  1377. (ada-adjust-case)
  1378. ;; horrible dekludge
  1379. (delete-char -1)
  1380. ;; some special keys and their bindings
  1381. (cond
  1382. ((eq lastk ?\n)
  1383. (funcall ada-lfd-binding))
  1384. ((eq lastk ?\r)
  1385. (funcall ada-ret-binding))))
  1386. ((eq lastk ?\C-i) (ada-tab))
  1387. ;; Else just insert the character
  1388. ((self-insert-command (prefix-numeric-value arg))))
  1389. ;; if there is a keyword in front of the underscore
  1390. ;; then it should be part of an identifier (MH)
  1391. (if (eq lastk ?_)
  1392. (ada-adjust-case t)
  1393. (ada-adjust-case))))
  1394. ;; Else, no auto-casing
  1395. (cond
  1396. ((eq last-command-event ?\n)
  1397. (funcall ada-lfd-binding))
  1398. ((eq last-command-event ?\r)
  1399. (funcall ada-ret-binding))
  1400. (t
  1401. (self-insert-command (prefix-numeric-value arg))))))
  1402. (defun ada-activate-keys-for-case ()
  1403. ;; FIXME: Use post-self-insert-hook instead of changing key bindings.
  1404. "Modify the key bindings for all the keys that should readjust the casing."
  1405. (interactive)
  1406. ;; Save original key-bindings to allow swapping ret/lfd
  1407. ;; when casing is activated.
  1408. ;; The 'or ...' is there to be sure that the value will not
  1409. ;; be changed again when Ada mode is called more than once
  1410. (or ada-ret-binding (setq ada-ret-binding (key-binding "\C-M")))
  1411. (or ada-lfd-binding (setq ada-lfd-binding (key-binding "\C-j")))
  1412. ;; Call case modifying function after certain keys.
  1413. (mapcar (function (lambda(key) (define-key
  1414. ada-mode-map
  1415. (char-to-string key)
  1416. 'ada-adjust-case-interactive)))
  1417. '( ?` ?_ ?# ?% ?& ?* ?( ?) ?- ?= ?+
  1418. ?| ?\; ?: ?' ?\" ?< ?, ?. ?> ?/ ?\n 32 ?\r )))
  1419. (defun ada-loose-case-word (&optional _arg)
  1420. "Upcase first letter and letters following `_' in the following word.
  1421. No other letter is modified.
  1422. ARG is ignored, and is there for compatibility with `capitalize-word' only."
  1423. (interactive)
  1424. (save-excursion
  1425. (let ((end (save-excursion (skip-syntax-forward "w") (point)))
  1426. (first t))
  1427. (skip-syntax-backward "w")
  1428. (while (and (or first (search-forward "_" end t))
  1429. (< (point) end))
  1430. (and first
  1431. (setq first nil))
  1432. (insert-char (upcase (following-char)) 1)
  1433. (delete-char 1)))))
  1434. (defun ada-no-auto-case (&optional _arg)
  1435. "Do nothing. ARG is ignored.
  1436. This function can be used for the auto-casing variables in Ada mode, to
  1437. adapt to unusual auto-casing schemes. Since it does nothing, you can for
  1438. instance use it for `ada-case-identifier' if you don't want any special
  1439. auto-casing for identifiers, whereas keywords have to be lower-cased.
  1440. See also `ada-auto-case' to disable auto casing altogether."
  1441. nil)
  1442. (defun ada-capitalize-word (&optional _arg)
  1443. "Upcase first letter and letters following `_', lower case other letters.
  1444. ARG is ignored, and is there for compatibility with `capitalize-word' only."
  1445. (interactive)
  1446. (let ((end (save-excursion (skip-syntax-forward "w") (point)))
  1447. (begin (save-excursion (skip-syntax-backward "w") (point))))
  1448. (capitalize-region begin end)))
  1449. (defun ada-adjust-case-region (from to)
  1450. "Adjust the case of all words in the region between FROM and TO.
  1451. Attention: This function might take very long for big regions!"
  1452. (interactive "*r")
  1453. (let ((begin nil)
  1454. (end nil)
  1455. (keywordp nil)
  1456. (attribp nil))
  1457. (message "Adjusting case ...")
  1458. (with-syntax-table ada-mode-symbol-syntax-table
  1459. (save-excursion
  1460. (goto-char to)
  1461. ;;
  1462. ;; loop: look for all identifiers, keywords, and attributes
  1463. ;;
  1464. (while (re-search-backward "\\<\\(\\sw+\\)\\>" from t)
  1465. (setq end (match-end 1))
  1466. (setq attribp
  1467. (and (> (point) from)
  1468. (save-excursion
  1469. (forward-char -1)
  1470. (setq attribp (looking-at "'.[^']")))))
  1471. (or
  1472. ;; do nothing if it is a string or comment
  1473. (ada-in-string-or-comment-p)
  1474. (progn
  1475. ;;
  1476. ;; get the identifier or keyword or attribute
  1477. ;;
  1478. (setq begin (point))
  1479. (setq keywordp (looking-at ada-keywords))
  1480. (goto-char end)
  1481. ;;
  1482. ;; casing according to user-option
  1483. ;;
  1484. (if attribp
  1485. (funcall ada-case-attribute -1)
  1486. (if keywordp
  1487. (funcall ada-case-keyword -1)
  1488. (ada-adjust-case-identifier)))
  1489. (goto-char begin))))
  1490. (message "Adjusting case ... Done")))))
  1491. (defun ada-adjust-case-buffer ()
  1492. "Adjust the case of all words in the whole buffer.
  1493. ATTENTION: This function might take very long for big buffers!"
  1494. (interactive "*")
  1495. (ada-adjust-case-region (point-min) (point-max)))
  1496. ;;--------------------------------------------------------------
  1497. ;; Format Parameter Lists
  1498. ;; Some special algorithms are provided to indent the parameter lists in
  1499. ;; subprogram declarations. This is done in two steps:
  1500. ;; - First parses the parameter list. The returned list has the following
  1501. ;; format:
  1502. ;; ( (<Param_Name> in? out? access? <Type_Name> <Default_Expression>)
  1503. ;; ... )
  1504. ;; This is done in `ada-scan-paramlist'.
  1505. ;; - Delete and recreate the parameter list in function
  1506. ;; `ada-insert-paramlist'.
  1507. ;; Both steps are called from `ada-format-paramlist'.
  1508. ;; Note: Comments inside the parameter list are lost.
  1509. ;; The syntax has to be correct, or the reformatting will fail.
  1510. ;;--------------------------------------------------------------
  1511. (defun ada-format-paramlist ()
  1512. "Reformat the parameter list point is in."
  1513. (interactive)
  1514. (let ((begin nil)
  1515. (end nil)
  1516. (delend nil)
  1517. (paramlist nil))
  1518. (with-syntax-table ada-mode-symbol-syntax-table
  1519. ;; check if really inside parameter list
  1520. (or (ada-in-paramlist-p)
  1521. (error "Not in parameter list"))
  1522. ;; find start of current parameter-list
  1523. (ada-search-ignore-string-comment
  1524. (concat ada-subprog-start-re "\\|\\<body\\>" ) t nil)
  1525. (down-list 1)
  1526. (backward-char 1)
  1527. (setq begin (point))
  1528. ;; find end of parameter-list
  1529. (forward-sexp 1)
  1530. (setq delend (point))
  1531. (delete-char -1)
  1532. (insert "\n")
  1533. ;; find end of last parameter-declaration
  1534. (forward-comment -1000)
  1535. (setq end (point))
  1536. ;; build a list of all elements of the parameter-list
  1537. (setq paramlist (ada-scan-paramlist (1+ begin) end))
  1538. ;; delete the original parameter-list
  1539. (delete-region begin delend)
  1540. ;; insert the new parameter-list
  1541. (goto-char begin)
  1542. (ada-insert-paramlist paramlist))))
  1543. (defun ada-scan-paramlist (begin end)
  1544. "Scan the parameter list found in between BEGIN and END.
  1545. Return the equivalent internal parameter list."
  1546. (let ((paramlist (list))
  1547. (param (list))
  1548. (notend t)
  1549. (apos nil)
  1550. (epos nil)
  1551. (semipos nil)
  1552. (match-cons nil))
  1553. (goto-char begin)
  1554. ;; loop until end of last parameter
  1555. (while notend
  1556. ;; find first character of parameter-declaration
  1557. (ada-goto-next-non-ws)
  1558. (setq apos (point))
  1559. ;; find last character of parameter-declaration
  1560. (if (setq match-cons
  1561. (ada-search-ignore-string-comment "[ \t\n]*;" nil end t))
  1562. (progn
  1563. (setq epos (car match-cons))
  1564. (setq semipos (cdr match-cons)))
  1565. (setq epos end))
  1566. ;; read name(s) of parameter(s)
  1567. (goto-char apos)
  1568. (looking-at "\\(\\(\\sw\\|[_, \t\n]\\)*\\(\\sw\\|_\\)\\)[ \t\n]*:[^=]")
  1569. (setq param (list (match-string 1)))
  1570. (ada-search-ignore-string-comment ":" nil epos t 'search-forward)
  1571. ;; look for 'in'
  1572. (setq apos (point))
  1573. (setq param
  1574. (append param
  1575. (list
  1576. (consp
  1577. (ada-search-ignore-string-comment
  1578. "in" nil epos t 'word-search-forward)))))
  1579. ;; look for 'out'
  1580. (goto-char apos)
  1581. (setq param
  1582. (append param
  1583. (list
  1584. (consp
  1585. (ada-search-ignore-string-comment
  1586. "out" nil epos t 'word-search-forward)))))
  1587. ;; look for 'access'
  1588. (goto-char apos)
  1589. (setq param
  1590. (append param
  1591. (list
  1592. (consp
  1593. (ada-search-ignore-string-comment
  1594. "access" nil epos t 'word-search-forward)))))
  1595. ;; skip 'in'/'out'/'access'
  1596. (goto-char apos)
  1597. (ada-goto-next-non-ws)
  1598. (while (looking-at "\\<\\(in\\|out\\|access\\)\\>")
  1599. (forward-word-strictly 1)
  1600. (ada-goto-next-non-ws))
  1601. ;; read type of parameter
  1602. ;; We accept spaces in the name, since some software like Rose
  1603. ;; generates something like: "A : B 'Class"
  1604. (looking-at "\\<\\(\\sw\\|[_.' \t]\\)+\\>")
  1605. (setq param
  1606. (append param
  1607. (list (match-string 0))))
  1608. ;; read default-expression, if there is one
  1609. (goto-char (setq apos (match-end 0)))
  1610. (setq param
  1611. (append param
  1612. (list
  1613. (if (setq match-cons
  1614. (ada-search-ignore-string-comment
  1615. ":=" nil epos t 'search-forward))
  1616. (buffer-substring (car match-cons) epos)
  1617. nil))))
  1618. ;; add this parameter-declaration to the list
  1619. (setq paramlist (append paramlist (list param)))
  1620. ;; check if it was the last parameter
  1621. (if (eq epos end)
  1622. (setq notend nil)
  1623. (goto-char semipos))
  1624. )
  1625. (reverse paramlist)))
  1626. (defun ada-insert-paramlist (paramlist)
  1627. "Insert a formatted PARAMLIST in the buffer."
  1628. (let ((i (length paramlist))
  1629. (parlen 0)
  1630. (typlen 0)
  1631. (inp nil)
  1632. (outp nil)
  1633. (accessp nil)
  1634. (column nil)
  1635. (firstcol nil))
  1636. ;; loop until last parameter
  1637. (while (not (zerop i))
  1638. (setq i (1- i))
  1639. ;; get max length of parameter-name
  1640. (setq parlen (max parlen (length (nth 0 (nth i paramlist)))))
  1641. ;; get max length of type-name
  1642. (setq typlen (max typlen (length (nth 4 (nth i paramlist)))))
  1643. ;; is there any 'in' ?
  1644. (setq inp (or inp (nth 1 (nth i paramlist))))
  1645. ;; is there any 'out' ?
  1646. (setq outp (or outp (nth 2 (nth i paramlist))))
  1647. ;; is there any 'access' ?
  1648. (setq accessp (or accessp (nth 3 (nth i paramlist))))
  1649. )
  1650. ;; does paramlist already start on a separate line ?
  1651. (if (save-excursion
  1652. (re-search-backward "^.\\|[^ \t]" nil t)
  1653. (looking-at "^."))
  1654. ;; yes => re-indent it
  1655. (progn
  1656. (ada-indent-current)
  1657. (save-excursion
  1658. (if (looking-at "\\(is\\|return\\)")
  1659. (replace-match " \\1"))))
  1660. ;; no => insert it where we are after removing any whitespace
  1661. (fixup-whitespace)
  1662. (save-excursion
  1663. (cond
  1664. ((looking-at "[ \t]*\\(\n\\|;\\)")
  1665. (replace-match "\\1"))
  1666. ((looking-at "[ \t]*\\(is\\|return\\)")
  1667. (replace-match " \\1"))))
  1668. (insert " "))
  1669. (insert "(")
  1670. (ada-indent-current)
  1671. (setq firstcol (current-column))
  1672. (setq i (length paramlist))
  1673. ;; loop until last parameter
  1674. (while (not (zerop i))
  1675. (setq i (1- i))
  1676. (setq column firstcol)
  1677. ;; insert parameter-name, space and colon
  1678. (insert (nth 0 (nth i paramlist)))
  1679. (indent-to (+ column parlen 1))
  1680. (insert ": ")
  1681. (setq column (current-column))
  1682. ;; insert 'in' or space
  1683. (if (nth 1 (nth i paramlist))
  1684. (insert "in ")
  1685. (if (and
  1686. (or inp
  1687. accessp)
  1688. (not (nth 3 (nth i paramlist))))
  1689. (insert " ")))
  1690. ;; insert 'out' or space
  1691. (if (nth 2 (nth i paramlist))
  1692. (insert "out ")
  1693. (if (and
  1694. (or outp
  1695. accessp)
  1696. (not (nth 3 (nth i paramlist))))
  1697. (insert " ")))
  1698. ;; insert 'access'
  1699. (if (nth 3 (nth i paramlist))
  1700. (insert "access "))
  1701. (setq column (current-column))
  1702. ;; insert type-name and, if necessary, space and default-expression
  1703. (insert (nth 4 (nth i paramlist)))
  1704. (if (nth 5 (nth i paramlist))
  1705. (progn
  1706. (indent-to (+ column typlen 1))
  1707. (insert (nth 5 (nth i paramlist)))))
  1708. ;; check if it was the last parameter
  1709. (if (zerop i)
  1710. (insert ")")
  1711. ;; no => insert ';' and newline and indent
  1712. (insert ";")
  1713. (newline)
  1714. (indent-to firstcol))
  1715. )
  1716. ;; if anything follows, except semicolon, newline, is or return
  1717. ;; put it in a new line and indent it
  1718. (unless (looking-at "[ \t]*\\(;\\|\n\\|is\\|return\\)")
  1719. (ada-indent-newline-indent))
  1720. ))
  1721. ;;;----------------------------------------------------------------
  1722. ;; Indentation Engine
  1723. ;; All indentations are indicated as a two-element string:
  1724. ;; - position of reference in the buffer
  1725. ;; - offset to indent from this position (can also be a symbol or a list
  1726. ;; that are evaluated)
  1727. ;; Thus the total indentation for a line is the column number of the reference
  1728. ;; position plus whatever value the evaluation of the second element provides.
  1729. ;; This mechanism is used so that the Ada mode can "explain" how the
  1730. ;; indentation was calculated, by showing which variables were used.
  1731. ;;
  1732. ;; The indentation itself is done in only one pass: first we try to guess in
  1733. ;; what context we are by looking at the following keyword or punctuation
  1734. ;; sign. If nothing remarkable is found, just try to guess the indentation
  1735. ;; based on previous lines.
  1736. ;;
  1737. ;; The relevant functions for indentation are:
  1738. ;; - `ada-indent-region': Re-indent a region of text
  1739. ;; - `ada-justified-indent-current': Re-indent the current line and shows the
  1740. ;; calculation that were done
  1741. ;; - `ada-indent-current': Re-indent the current line
  1742. ;; - `ada-get-current-indent': Calculate the indentation for the current line,
  1743. ;; based on the context (see above).
  1744. ;; - `ada-get-indent-*': Calculate the indentation in a specific context.
  1745. ;; For efficiency, these functions do not check they are in the correct
  1746. ;; context.
  1747. ;;;----------------------------------------------------------------
  1748. (defun ada-indent-region (beg end)
  1749. "Indent the region between BEG end END."
  1750. (interactive "*r")
  1751. (goto-char beg)
  1752. (let ((block-done 0)
  1753. (lines-remaining (count-lines beg end))
  1754. (msg (format "%%4d out of %4d lines remaining ..."
  1755. (count-lines beg end)))
  1756. (endmark (copy-marker end)))
  1757. ;; catch errors while indenting
  1758. (while (< (point) endmark)
  1759. (if (> block-done 39)
  1760. (progn
  1761. (setq lines-remaining (- lines-remaining block-done)
  1762. block-done 0)
  1763. (message msg lines-remaining)))
  1764. (if (= (char-after) ?\n) nil
  1765. (ada-indent-current))
  1766. (forward-line 1)
  1767. (setq block-done (1+ block-done)))
  1768. (message "Indenting ... done")))
  1769. (defun ada-indent-newline-indent ()
  1770. "Indent the current line, insert a newline and then indent the new line."
  1771. (interactive "*")
  1772. (ada-indent-current)
  1773. (newline)
  1774. (ada-indent-current))
  1775. (defun ada-indent-newline-indent-conditional ()
  1776. "Insert a newline and indent it.
  1777. The original line is re-indented if `ada-indent-after-return' is non-nil."
  1778. (interactive "*")
  1779. ;; If at end of buffer (entering brand new code), some indentation
  1780. ;; fails. For example, a block label requires whitespace following
  1781. ;; the : to be recognized. So we do the newline first, then
  1782. ;; go back and indent the original line.
  1783. (newline)
  1784. (if ada-indent-after-return
  1785. (progn
  1786. (forward-char -1)
  1787. (ada-indent-current)
  1788. (forward-char 1)))
  1789. (ada-indent-current))
  1790. (defun ada-justified-indent-current ()
  1791. "Indent the current line and explain how the calculation was done."
  1792. (interactive)
  1793. (let ((cur-indent (ada-indent-current)))
  1794. (let ((line (save-excursion
  1795. (goto-char (car cur-indent))
  1796. (count-lines 1 (point)))))
  1797. (if (equal (cdr cur-indent) '(0))
  1798. (message (concat "same indentation as line " (number-to-string line)))
  1799. (message "%s" (mapconcat (lambda(x)
  1800. (cond
  1801. ((symbolp x)
  1802. (symbol-name x))
  1803. ((numberp x)
  1804. (number-to-string x))
  1805. ((listp x)
  1806. (concat "- " (symbol-name (cadr x))))
  1807. ))
  1808. (cdr cur-indent)
  1809. " + "))))
  1810. (save-excursion
  1811. (goto-char (car cur-indent))
  1812. (sit-for 1))))
  1813. (defun ada-batch-reformat ()
  1814. "Re-indent and re-case all the files found on the command line.
  1815. This function should be used from the command line, with a
  1816. command like:
  1817. emacs -batch -l ada-mode -f ada-batch-reformat file1 file2 ..."
  1818. (while command-line-args-left
  1819. (let ((source (car command-line-args-left)))
  1820. (message "Formatting %s" source)
  1821. (find-file source)
  1822. (ada-indent-region (point-min) (point-max))
  1823. (ada-adjust-case-buffer)
  1824. (write-file source))
  1825. (setq command-line-args-left (cdr command-line-args-left)))
  1826. (message "Done")
  1827. (kill-emacs 0))
  1828. (defsubst ada-goto-previous-word ()
  1829. "Move point to the beginning of the previous word of Ada code.
  1830. Return the new position of point or nil if not found."
  1831. (ada-goto-next-word t))
  1832. (defun ada-indent-current ()
  1833. "Indent current line as Ada code.
  1834. Return the calculation that was done, including the reference point
  1835. and the offset."
  1836. (interactive)
  1837. (let ((orgpoint (point-marker))
  1838. cur-indent tmp-indent
  1839. prev-indent)
  1840. (unwind-protect
  1841. (with-syntax-table ada-mode-symbol-syntax-table
  1842. ;; This needs to be done here so that the advice is not always
  1843. ;; activated (this might interact badly with other modes)
  1844. (if (featurep 'xemacs)
  1845. (ad-activate 'parse-partial-sexp t))
  1846. (save-excursion
  1847. (setq cur-indent
  1848. ;; Not First line in the buffer ?
  1849. (if (save-excursion (zerop (forward-line -1)))
  1850. (progn
  1851. (back-to-indentation)
  1852. (ada-get-current-indent))
  1853. ;; first line in the buffer
  1854. (list (point-min) 0))))
  1855. ;; Evaluate the list to get the column to indent to
  1856. ;; prev-indent contains the column to indent to
  1857. (if cur-indent
  1858. (setq prev-indent (save-excursion (goto-char (car cur-indent))
  1859. (current-column))
  1860. tmp-indent (cdr cur-indent))
  1861. (setq prev-indent 0 tmp-indent '()))
  1862. (while (not (null tmp-indent))
  1863. (cond
  1864. ((numberp (car tmp-indent))
  1865. (setq prev-indent (+ prev-indent (car tmp-indent))))
  1866. (t
  1867. (setq prev-indent (+ prev-indent (eval (car tmp-indent)))))
  1868. )
  1869. (setq tmp-indent (cdr tmp-indent)))
  1870. ;; only re-indent if indentation is different then the current
  1871. (if (= (save-excursion (back-to-indentation) (current-column)) prev-indent)
  1872. nil
  1873. (beginning-of-line)
  1874. (delete-horizontal-space)
  1875. (indent-to prev-indent))
  1876. ;;
  1877. ;; restore position of point
  1878. ;;
  1879. (goto-char orgpoint)
  1880. (if (< (current-column) (current-indentation))
  1881. (back-to-indentation)))
  1882. (if (featurep 'xemacs)
  1883. (ad-deactivate 'parse-partial-sexp)))
  1884. cur-indent))
  1885. (defun ada-get-current-indent ()
  1886. "Return the indentation to use for the current line."
  1887. (let (column
  1888. pos
  1889. match-cons
  1890. result
  1891. (orgpoint (save-excursion
  1892. (beginning-of-line)
  1893. (forward-comment -10000)
  1894. (forward-line 1)
  1895. (point))))
  1896. (setq result
  1897. (cond
  1898. ;;-----------------------------
  1899. ;; in open parenthesis, but not in parameter-list
  1900. ;;-----------------------------
  1901. ((and ada-indent-to-open-paren
  1902. (not (ada-in-paramlist-p))
  1903. (setq column (ada-in-open-paren-p)))
  1904. ;; check if we have something like this (Table_Component_Type =>
  1905. ;; Source_File_Record)
  1906. (save-excursion
  1907. ;; Align the closing parenthesis on the opening one
  1908. (if (= (following-char) ?\))
  1909. (save-excursion
  1910. (goto-char column)
  1911. (skip-chars-backward " \t")
  1912. (list (1- (point)) 0))
  1913. (if (and (skip-chars-backward " \t")
  1914. (= (char-before) ?\n)
  1915. (not (forward-comment -10000))
  1916. (= (char-before) ?>))
  1917. ;; ??? Could use a different variable
  1918. (list column 'ada-broken-indent)
  1919. ;; We want all continuation lines to be indented the same
  1920. ;; (ada-broken-line from the opening parenthesis. However, in
  1921. ;; parameter list, each new parameter should be indented at the
  1922. ;; column as the opening parenthesis.
  1923. ;; A special case to handle nested boolean expressions, as in
  1924. ;; ((B
  1925. ;; and then C) -- indented by ada-broken-indent
  1926. ;; or else D) -- indenting this line.
  1927. ;; ??? This is really a hack, we should have a proper way to go to
  1928. ;; ??? the beginning of the statement
  1929. (if (= (char-before) ?\))
  1930. (backward-sexp))
  1931. (if (memq (char-before) '(?, ?\; ?\( ?\)))
  1932. (list column 0)
  1933. (list column 'ada-continuation-indent)
  1934. )))))
  1935. ;;---------------------------
  1936. ;; at end of buffer
  1937. ;;---------------------------
  1938. ((not (char-after))
  1939. (ada-indent-on-previous-lines nil orgpoint orgpoint))
  1940. ;;---------------------------
  1941. ;; starting with e
  1942. ;;---------------------------
  1943. ((= (downcase (char-after)) ?e)
  1944. (cond
  1945. ;; ------- end ------
  1946. ((looking-at "end\\>")
  1947. (let ((label 0)
  1948. limit)
  1949. (save-excursion
  1950. (ada-goto-matching-start 1)
  1951. ;;
  1952. ;; found 'loop' => skip back to 'while' or 'for'
  1953. ;; if 'loop' is not on a separate line
  1954. ;; Stop the search for 'while' and 'for' when a ';' is encountered.
  1955. ;;
  1956. (if (save-excursion
  1957. (beginning-of-line)
  1958. (looking-at ".+\\<loop\\>"))
  1959. (progn
  1960. (save-excursion
  1961. (setq limit (car (ada-search-ignore-string-comment ";" t))))
  1962. (if (save-excursion
  1963. (and
  1964. (setq match-cons
  1965. (ada-search-ignore-string-comment ada-loop-start-re t limit))
  1966. (not (looking-at "\\<loop\\>"))))
  1967. (progn
  1968. (goto-char (car match-cons))
  1969. (save-excursion
  1970. (back-to-indentation)
  1971. (if (looking-at ada-block-label-re)
  1972. (setq label (- ada-label-indent))))))))
  1973. ;; found 'record' =>
  1974. ;; if the keyword is found at the beginning of a line (or just
  1975. ;; after limited, we indent on it, otherwise we indent on the
  1976. ;; beginning of the type declaration)
  1977. ;; type A is (B : Integer;
  1978. ;; C : Integer) is record
  1979. ;; end record; -- This is badly indented otherwise
  1980. (if (looking-at "record")
  1981. (if (save-excursion
  1982. (beginning-of-line)
  1983. (looking-at "^[ \t]*\\(record\\|limited record\\)"))
  1984. (list (save-excursion (back-to-indentation) (point)) 0)
  1985. (list (save-excursion
  1986. (car (ada-search-ignore-string-comment "\\<type\\>" t)))
  1987. 0))
  1988. ;; Else keep the same indentation as the beginning statement
  1989. (list (+ (save-excursion (back-to-indentation) (point)) label) 0)))))
  1990. ;; ------ exception ----
  1991. ((looking-at "exception\\>")
  1992. (save-excursion
  1993. (ada-goto-matching-start 1)
  1994. (list (save-excursion (back-to-indentation) (point)) 0)))
  1995. ;; else
  1996. ((looking-at "else\\>")
  1997. (if (save-excursion (ada-goto-previous-word)
  1998. (looking-at "\\<or\\>"))
  1999. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2000. (save-excursion
  2001. (ada-goto-matching-start 1 nil t)
  2002. (list (progn (back-to-indentation) (point)) 0))))
  2003. ;; elsif
  2004. ((looking-at "elsif\\>")
  2005. (save-excursion
  2006. (ada-goto-matching-start 1 nil t)
  2007. (list (progn (back-to-indentation) (point)) 0)))
  2008. ))
  2009. ;;---------------------------
  2010. ;; starting with w (when)
  2011. ;;---------------------------
  2012. ((and (= (downcase (char-after)) ?w)
  2013. (looking-at "when\\>"))
  2014. (save-excursion
  2015. (ada-goto-matching-start 1)
  2016. (list (save-excursion (back-to-indentation) (point))
  2017. 'ada-when-indent)))
  2018. ;;---------------------------
  2019. ;; starting with t (then)
  2020. ;;---------------------------
  2021. ((and (= (downcase (char-after)) ?t)
  2022. (looking-at "then\\>"))
  2023. (if (save-excursion (ada-goto-previous-word)
  2024. (looking-at "and\\>"))
  2025. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2026. (save-excursion
  2027. ;; Select has been added for the statement: "select ... then abort"
  2028. (ada-search-ignore-string-comment
  2029. "\\<\\(elsif\\|if\\|select\\)\\>" t nil)
  2030. (list (progn (back-to-indentation) (point))
  2031. 'ada-stmt-end-indent))))
  2032. ;;---------------------------
  2033. ;; starting with l (loop)
  2034. ;;---------------------------
  2035. ((and (= (downcase (char-after)) ?l)
  2036. (looking-at "loop\\>"))
  2037. (setq pos (point))
  2038. (save-excursion
  2039. (goto-char (match-end 0))
  2040. (ada-goto-stmt-start)
  2041. (if (looking-at "\\<\\(loop\\|if\\)\\>")
  2042. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2043. (unless (looking-at ada-loop-start-re)
  2044. (ada-search-ignore-string-comment ada-loop-start-re
  2045. nil pos))
  2046. (if (looking-at "\\<loop\\>")
  2047. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2048. (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))))
  2049. ;;----------------------------
  2050. ;; starting with l (limited) or r (record)
  2051. ;;----------------------------
  2052. ((or (and (= (downcase (char-after)) ?l)
  2053. (looking-at "limited\\>"))
  2054. (and (= (downcase (char-after)) ?r)
  2055. (looking-at "record\\>")))
  2056. (save-excursion
  2057. (ada-search-ignore-string-comment
  2058. "\\<\\(type\\|use\\)\\>" t nil)
  2059. (if (looking-at "\\<use\\>")
  2060. (ada-search-ignore-string-comment "for" t nil nil
  2061. 'word-search-backward))
  2062. (list (progn (back-to-indentation) (point))
  2063. 'ada-indent-record-rel-type)))
  2064. ;;---------------------------
  2065. ;; starting with b (begin)
  2066. ;;---------------------------
  2067. ((and (= (downcase (char-after)) ?b)
  2068. (looking-at "begin\\>"))
  2069. (save-excursion
  2070. (if (ada-goto-decl-start t)
  2071. (list (progn (back-to-indentation) (point)) 0)
  2072. (ada-indent-on-previous-lines nil orgpoint orgpoint))))
  2073. ;;---------------------------
  2074. ;; starting with i (is)
  2075. ;;---------------------------
  2076. ((and (= (downcase (char-after)) ?i)
  2077. (looking-at "is\\>"))
  2078. (if (and ada-indent-is-separate
  2079. (save-excursion
  2080. (goto-char (match-end 0))
  2081. (ada-goto-next-non-ws (point-at-eol))
  2082. (looking-at "\\<abstract\\>\\|\\<separate\\>")))
  2083. (save-excursion
  2084. (ada-goto-stmt-start)
  2085. (list (progn (back-to-indentation) (point)) 'ada-indent))
  2086. (save-excursion
  2087. (ada-goto-stmt-start)
  2088. (if (looking-at "\\<overriding\\|package\\|procedure\\|function\\>")
  2089. (list (progn (back-to-indentation) (point)) 0)
  2090. (list (progn (back-to-indentation) (point)) 'ada-indent)))))
  2091. ;;---------------------------
  2092. ;; starting with r (return, renames)
  2093. ;;---------------------------
  2094. ((and (= (downcase (char-after)) ?r)
  2095. (looking-at "re\\(turn\\|names\\)\\>"))
  2096. (save-excursion
  2097. (let ((var 'ada-indent-return))
  2098. ;; If looking at a renames, skip the 'return' statement too
  2099. (if (looking-at "renames")
  2100. (let (pos)
  2101. (save-excursion
  2102. (setq pos (ada-search-ignore-string-comment ";\\|return\\>" t)))
  2103. (if (and pos
  2104. (= (downcase (char-after (car pos))) ?r))
  2105. (goto-char (car pos)))
  2106. (setq var 'ada-indent-renames)))
  2107. (forward-comment -1000)
  2108. (if (= (char-before) ?\))
  2109. (forward-sexp -1)
  2110. (forward-word-strictly -1))
  2111. ;; If there is a parameter list, and we have a function declaration
  2112. ;; or a access to subprogram declaration
  2113. (let ((num-back 1))
  2114. (if (and (= (following-char) ?\()
  2115. (save-excursion
  2116. (or (progn
  2117. (backward-word-strictly 1)
  2118. (looking-at "\\(function\\|procedure\\)\\>"))
  2119. (progn
  2120. (backward-word-strictly 1)
  2121. (setq num-back 2)
  2122. (looking-at "\\(function\\|procedure\\)\\>")))))
  2123. ;; The indentation depends of the value of ada-indent-return
  2124. (if (<= (eval var) 0)
  2125. (list (point) (list '- var))
  2126. (list (progn (backward-word-strictly num-back) (point))
  2127. var))
  2128. ;; Else there is no parameter list, but we have a function
  2129. ;; Only do something special if the user want to indent
  2130. ;; relative to the "function" keyword
  2131. (if (and (> (eval var) 0)
  2132. (save-excursion (forward-word-strictly -1)
  2133. (looking-at "function\\>")))
  2134. (list (progn (forward-word-strictly -1) (point)) var)
  2135. ;; Else...
  2136. (ada-indent-on-previous-lines nil orgpoint orgpoint)))))))
  2137. ;;--------------------------------
  2138. ;; starting with 'o' or 'p'
  2139. ;; 'or' as statement-start
  2140. ;; 'private' as statement-start
  2141. ;;--------------------------------
  2142. ((and (or (= (downcase (char-after)) ?o)
  2143. (= (downcase (char-after)) ?p))
  2144. (or (ada-looking-at-semi-or)
  2145. (ada-looking-at-semi-private)))
  2146. (save-excursion
  2147. ;; ??? Wasn't this done already in ada-looking-at-semi-or ?
  2148. (ada-goto-matching-start 1)
  2149. (list (progn (back-to-indentation) (point)) 0)))
  2150. ;;--------------------------------
  2151. ;; starting with 'd' (do)
  2152. ;;--------------------------------
  2153. ((and (= (downcase (char-after)) ?d)
  2154. (looking-at "do\\>"))
  2155. (save-excursion
  2156. (ada-goto-stmt-start)
  2157. (list (progn (back-to-indentation) (point)) 'ada-stmt-end-indent)))
  2158. ;;--------------------------------
  2159. ;; starting with '-' (comment)
  2160. ;;--------------------------------
  2161. ((= (char-after) ?-)
  2162. (if ada-indent-comment-as-code
  2163. ;; Indent comments on previous line comments if required
  2164. ;; We must use a search-forward (even if the code is more complex),
  2165. ;; since we want to find the beginning of the comment.
  2166. (let (pos)
  2167. (if (and ada-indent-align-comments
  2168. (save-excursion
  2169. (forward-line -1)
  2170. (beginning-of-line)
  2171. (while (and (not pos)
  2172. (search-forward "--" (point-at-eol) t))
  2173. (unless (ada-in-string-p)
  2174. (setq pos (point))))
  2175. pos))
  2176. (list (- pos 2) 0)
  2177. ;; Else always on previous line
  2178. (ada-indent-on-previous-lines nil orgpoint orgpoint)))
  2179. ;; Else same indentation as the previous line
  2180. (list (save-excursion (back-to-indentation) (point)) 0)))
  2181. ;;--------------------------------
  2182. ;; starting with '#' (preprocessor line)
  2183. ;;--------------------------------
  2184. ((and (= (char-after) ?#)
  2185. (equal ada-which-compiler 'gnat)
  2186. (looking-at "#[ \t]*\\(if\\|els\\(e\\|if\\)\\|end[ \t]*if\\)"))
  2187. (list (point-at-bol) 0))
  2188. ;;--------------------------------
  2189. ;; starting with ')' (end of a parameter list)
  2190. ;;--------------------------------
  2191. ((and (not (eobp)) (= (char-after) ?\)))
  2192. (save-excursion
  2193. (forward-char 1)
  2194. (backward-sexp 1)
  2195. (list (point) 0)))
  2196. ;;---------------------------------
  2197. ;; new/abstract/separate
  2198. ;;---------------------------------
  2199. ((looking-at "\\(new\\|abstract\\|separate\\)\\>")
  2200. (ada-indent-on-previous-lines nil orgpoint orgpoint))
  2201. ;;---------------------------------
  2202. ;; package/function/procedure
  2203. ;;---------------------------------
  2204. ((and (or (= (downcase (char-after)) ?p) (= (downcase (char-after)) ?f))
  2205. (looking-at "\\<\\(package\\|function\\|procedure\\)\\>"))
  2206. (save-excursion
  2207. ;; Go up until we find either a generic section, or the end of the
  2208. ;; previous subprogram/package, or 'overriding' for this function/procedure
  2209. (let (found)
  2210. (while (and (not found)
  2211. (ada-search-ignore-string-comment
  2212. "\\<\\(generic\\|end\\|begin\\|overriding\\|package\\|procedure\\|function\\)\\>" t))
  2213. ;; avoid "with procedure"... in generic parts
  2214. (save-excursion
  2215. (forward-word-strictly -1)
  2216. (setq found (not (looking-at "with"))))))
  2217. (cond
  2218. ((looking-at "\\<generic\\|overriding\\>")
  2219. (list (progn (back-to-indentation) (point)) 0))
  2220. (t
  2221. (ada-indent-on-previous-lines nil orgpoint orgpoint)))))
  2222. ;;---------------------------------
  2223. ;; label
  2224. ;;---------------------------------
  2225. ((looking-at ada-label-re)
  2226. (if (ada-in-decl-p)
  2227. ;; ada-block-label-re matches variable declarations
  2228. (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2229. (append (ada-indent-on-previous-lines nil orgpoint orgpoint)
  2230. '(ada-label-indent))))
  2231. ))
  2232. ;;---------------------------------
  2233. ;; Other syntaxes
  2234. ;;---------------------------------
  2235. (or result (ada-indent-on-previous-lines nil orgpoint orgpoint))))
  2236. (defun ada-indent-on-previous-lines (&optional nomove orgpoint initial-pos)
  2237. "Calculate the indentation for the new line after ORGPOINT.
  2238. The result list is based on the previous lines in the buffer.
  2239. If NOMOVE is nil, moves point to the beginning of the current statement.
  2240. if INITIAL-POS is non-nil, moves point to INITIAL-POS before calculation."
  2241. (if initial-pos
  2242. (goto-char initial-pos))
  2243. (let ((oldpoint (point)))
  2244. ;; Is inside a parameter-list ?
  2245. (if (ada-in-paramlist-p)
  2246. (ada-get-indent-paramlist)
  2247. ;; Move to beginning of current statement. If already at a
  2248. ;; statement start, move to beginning of enclosing statement.
  2249. (unless nomove
  2250. (ada-goto-stmt-start t))
  2251. ;; no beginning found => don't change indentation
  2252. (if (and (eq oldpoint (point))
  2253. (not nomove))
  2254. (ada-get-indent-nochange)
  2255. (cond
  2256. ;;
  2257. ((and
  2258. ada-indent-to-open-paren
  2259. (ada-in-open-paren-p))
  2260. (ada-get-indent-open-paren))
  2261. ;;
  2262. ((looking-at "end\\>")
  2263. (ada-get-indent-end orgpoint))
  2264. ;;
  2265. ((looking-at ada-loop-start-re)
  2266. (ada-get-indent-loop orgpoint))
  2267. ;;
  2268. ((looking-at ada-subprog-start-re)
  2269. (ada-get-indent-subprog orgpoint))
  2270. ;;
  2271. ((looking-at ada-block-start-re)
  2272. (ada-get-indent-block-start orgpoint))
  2273. ;;
  2274. ((looking-at ada-block-label-re) ; also variable declaration
  2275. (ada-get-indent-block-label orgpoint))
  2276. ;;
  2277. ((looking-at ada-goto-label-re)
  2278. (ada-get-indent-goto-label orgpoint))
  2279. ;;
  2280. ((looking-at "\\(sub\\)?type\\>")
  2281. (ada-get-indent-type orgpoint))
  2282. ;;
  2283. ;; "then" has to be included in the case of "select...then abort"
  2284. ;; statements, since (goto-stmt-start) at the beginning of
  2285. ;; the current function would leave the cursor on that position
  2286. ((looking-at "\\(\\(els\\)?if\\>\\)\\|then abort\\>")
  2287. (ada-get-indent-if orgpoint))
  2288. ;;
  2289. ((looking-at "case\\>")
  2290. (ada-get-indent-case orgpoint))
  2291. ;;
  2292. ((looking-at "when\\>")
  2293. (ada-get-indent-when orgpoint))
  2294. ;;
  2295. ((looking-at "separate\\>")
  2296. (ada-get-indent-nochange))
  2297. ;;
  2298. ((looking-at "with\\>\\|use\\>")
  2299. ;; Are we still in that statement, or are we in fact looking at
  2300. ;; the previous one ?
  2301. (if (save-excursion (search-forward ";" oldpoint t))
  2302. (list (progn (back-to-indentation) (point)) 0)
  2303. (list (point) (if (looking-at "with")
  2304. 'ada-with-indent
  2305. 'ada-use-indent))))
  2306. ;;
  2307. (t
  2308. (ada-get-indent-noindent orgpoint)))))
  2309. ))
  2310. (defun ada-get-indent-open-paren ()
  2311. "Calculate the indentation when point is behind an unclosed parenthesis."
  2312. (list (ada-in-open-paren-p) 0))
  2313. (defun ada-get-indent-nochange ()
  2314. "Return the current indentation of the previous line."
  2315. (save-excursion
  2316. (forward-line -1)
  2317. (back-to-indentation)
  2318. (list (point) 0)))
  2319. (defun ada-get-indent-paramlist ()
  2320. "Calculate the indentation when point is inside a parameter list."
  2321. (save-excursion
  2322. (ada-search-ignore-string-comment "[^ \t\n]" t nil t)
  2323. (cond
  2324. ;; in front of the first parameter
  2325. ((= (char-after) ?\()
  2326. (goto-char (match-end 0))
  2327. (list (point) 0))
  2328. ;; in front of another parameter
  2329. ((= (char-after) ?\;)
  2330. (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
  2331. (ada-goto-next-non-ws)
  2332. (list (point) 0))
  2333. ;; After an affectation (default parameter value in subprogram
  2334. ;; declaration)
  2335. ((and (= (following-char) ?=) (= (preceding-char) ?:))
  2336. (back-to-indentation)
  2337. (list (point) 'ada-broken-indent))
  2338. ;; inside a parameter declaration
  2339. (t
  2340. (goto-char (cdr (ada-search-ignore-string-comment "(\\|;" t nil t)))
  2341. (ada-goto-next-non-ws)
  2342. (list (point) 'ada-broken-indent)))))
  2343. (defun ada-get-indent-end (orgpoint)
  2344. "Calculate the indentation when point is just before an end statement.
  2345. ORGPOINT is the limit position used in the calculation."
  2346. (let ((defun-name nil)
  2347. (indent nil))
  2348. ;; is the line already terminated by ';' ?
  2349. (if (save-excursion
  2350. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2351. 'search-forward))
  2352. ;; yes, look what's following 'end'
  2353. (progn
  2354. (forward-word-strictly 1)
  2355. (ada-goto-next-non-ws)
  2356. (cond
  2357. ;;
  2358. ;; loop/select/if/case/return
  2359. ;;
  2360. ((looking-at "\\<\\(loop\\|select\\|if\\|case\\|return\\)\\>")
  2361. (save-excursion (ada-check-matching-start (match-string 0)))
  2362. (list (save-excursion (back-to-indentation) (point)) 0))
  2363. ;;
  2364. ;; record
  2365. ;;
  2366. ((looking-at "\\<record\\>")
  2367. (save-excursion
  2368. (ada-check-matching-start (match-string 0))
  2369. ;; we are now looking at the matching "record" statement
  2370. (forward-word-strictly 1)
  2371. (ada-goto-stmt-start)
  2372. ;; now on the matching type declaration, or use clause
  2373. (unless (looking-at "\\(for\\|type\\)\\>")
  2374. (ada-search-ignore-string-comment "\\<type\\>" t))
  2375. (list (progn (back-to-indentation) (point)) 0)))
  2376. ;;
  2377. ;; a named block end
  2378. ;;
  2379. ((looking-at ada-ident-re)
  2380. (setq defun-name (match-string 0))
  2381. (save-excursion
  2382. (ada-goto-matching-start 0)
  2383. (ada-check-defun-name defun-name))
  2384. (list (progn (back-to-indentation) (point)) 0))
  2385. ;;
  2386. ;; a block-end without name
  2387. ;;
  2388. ((= (char-after) ?\;)
  2389. (save-excursion
  2390. (ada-goto-matching-start 0)
  2391. (if (looking-at "\\<begin\\>")
  2392. (progn
  2393. (setq indent (list (point) 0))
  2394. (if (ada-goto-decl-start t)
  2395. (list (progn (back-to-indentation) (point)) 0)
  2396. indent))
  2397. (list (progn (back-to-indentation) (point)) 0)
  2398. )))
  2399. ;;
  2400. ;; anything else - should maybe signal an error ?
  2401. ;;
  2402. (t
  2403. (list (save-excursion (back-to-indentation) (point))
  2404. 'ada-broken-indent))))
  2405. (list (save-excursion (back-to-indentation) (point))
  2406. 'ada-broken-indent))))
  2407. (defun ada-get-indent-case (orgpoint)
  2408. "Calculate the indentation when point is just before a case statement.
  2409. ORGPOINT is the limit position used in the calculation."
  2410. (let ((match-cons nil)
  2411. (opos (point)))
  2412. (cond
  2413. ;;
  2414. ;; case..is..when..=>
  2415. ;;
  2416. ((save-excursion
  2417. (setq match-cons (and
  2418. ;; the `=>' must be after the keyword `is'.
  2419. (ada-search-ignore-string-comment
  2420. "is" nil orgpoint nil 'word-search-forward)
  2421. (ada-search-ignore-string-comment
  2422. "[ \t\n]+=>" nil orgpoint))))
  2423. (save-excursion
  2424. (goto-char (car match-cons))
  2425. (unless (ada-search-ignore-string-comment "when" t opos)
  2426. (error "Missing `when' between `case' and `=>'"))
  2427. (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))
  2428. ;;
  2429. ;; case..is..when
  2430. ;;
  2431. ((save-excursion
  2432. (setq match-cons (ada-search-ignore-string-comment
  2433. "when" nil orgpoint nil 'word-search-forward)))
  2434. (goto-char (cdr match-cons))
  2435. (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
  2436. ;;
  2437. ;; case..is
  2438. ;;
  2439. ((save-excursion
  2440. (setq match-cons (ada-search-ignore-string-comment
  2441. "is" nil orgpoint nil 'word-search-forward)))
  2442. (list (save-excursion (back-to-indentation) (point)) 'ada-when-indent))
  2443. ;;
  2444. ;; incomplete case
  2445. ;;
  2446. (t
  2447. (list (save-excursion (back-to-indentation) (point))
  2448. 'ada-broken-indent)))))
  2449. (defun ada-get-indent-when (orgpoint)
  2450. "Calculate the indentation when point is just before a when statement.
  2451. ORGPOINT is the limit position used in the calculation."
  2452. (let ((cur-indent (save-excursion (back-to-indentation) (point))))
  2453. (if (ada-search-ignore-string-comment "[ \t\n]*=>" nil orgpoint)
  2454. (list cur-indent 'ada-indent)
  2455. (list cur-indent 'ada-broken-indent))))
  2456. (defun ada-get-indent-if (orgpoint)
  2457. "Calculate the indentation when point is just before an if statement.
  2458. ORGPOINT is the limit position used in the calculation."
  2459. (let ((cur-indent (save-excursion (back-to-indentation) (point)))
  2460. (match-cons nil))
  2461. ;;
  2462. ;; Move to the correct then (ignore all "and then")
  2463. ;;
  2464. (while (and (setq match-cons (ada-search-ignore-string-comment
  2465. "\\<\\(then\\|and[ \t]*then\\)\\>"
  2466. nil orgpoint))
  2467. (= (downcase (char-after (car match-cons))) ?a)))
  2468. ;; If "then" was found (we are looking at it)
  2469. (if match-cons
  2470. (progn
  2471. ;;
  2472. ;; 'then' first in separate line ?
  2473. ;; => indent according to 'then',
  2474. ;; => else indent according to 'if'
  2475. ;;
  2476. (if (save-excursion
  2477. (back-to-indentation)
  2478. (looking-at "\\<then\\>"))
  2479. (setq cur-indent (save-excursion (back-to-indentation) (point))))
  2480. ;; skip 'then'
  2481. (forward-word-strictly 1)
  2482. (list cur-indent 'ada-indent))
  2483. (list cur-indent 'ada-broken-indent))))
  2484. (defun ada-get-indent-block-start (orgpoint)
  2485. "Calculate the indentation when point is at the start of a block.
  2486. ORGPOINT is the limit position used in the calculation."
  2487. (let ((pos nil))
  2488. (cond
  2489. ((save-excursion
  2490. (forward-word-strictly 1)
  2491. (setq pos (ada-goto-next-non-ws orgpoint)))
  2492. (goto-char pos)
  2493. (save-excursion
  2494. (ada-indent-on-previous-lines t orgpoint)))
  2495. ;; Special case for record types, for instance for:
  2496. ;; type A is (B : Integer;
  2497. ;; C : Integer) is record
  2498. ;; null; -- This is badly indented otherwise
  2499. ((looking-at "record")
  2500. ;; If record is at the beginning of the line, indent from there
  2501. (if (save-excursion
  2502. (beginning-of-line)
  2503. (looking-at "^[ \t]*\\(record\\|limited record\\)"))
  2504. (list (save-excursion (back-to-indentation) (point)) 'ada-indent)
  2505. ;; else indent relative to the type command
  2506. (list (save-excursion
  2507. (car (ada-search-ignore-string-comment "\\<type\\>" t)))
  2508. 'ada-indent)))
  2509. ;; Special case for label:
  2510. ((looking-at ada-block-label-re)
  2511. (list (- (save-excursion (back-to-indentation) (point)) ada-label-indent) 'ada-indent))
  2512. ;; nothing follows the block-start
  2513. (t
  2514. (list (save-excursion (back-to-indentation) (point)) 'ada-indent)))))
  2515. (defun ada-get-indent-subprog (orgpoint)
  2516. "Calculate the indentation when point is just before a subprogram.
  2517. ORGPOINT is the limit position used in the calculation."
  2518. (let ((match-cons nil)
  2519. (cur-indent (save-excursion (back-to-indentation) (point)))
  2520. (foundis nil))
  2521. ;;
  2522. ;; is there an 'is' in front of point ?
  2523. ;;
  2524. (if (save-excursion
  2525. (setq match-cons
  2526. (ada-search-ignore-string-comment
  2527. "\\<\\(is\\|do\\)\\>" nil orgpoint)))
  2528. ;;
  2529. ;; yes, then skip to its end
  2530. ;;
  2531. (progn
  2532. (setq foundis t)
  2533. (goto-char (cdr match-cons)))
  2534. ;;
  2535. ;; no, then goto next non-ws, if there is one in front of point
  2536. ;;
  2537. (progn
  2538. (unless (ada-goto-next-non-ws orgpoint)
  2539. (goto-char orgpoint))))
  2540. (cond
  2541. ;;
  2542. ;; nothing follows 'is'
  2543. ;;
  2544. ((and
  2545. foundis
  2546. (save-excursion
  2547. (not (ada-search-ignore-string-comment
  2548. "[^ \t\n]" nil orgpoint t))))
  2549. (list cur-indent 'ada-indent))
  2550. ;;
  2551. ;; is abstract/separate/new ...
  2552. ;;
  2553. ((and
  2554. foundis
  2555. (save-excursion
  2556. (setq match-cons
  2557. (ada-search-ignore-string-comment
  2558. "\\<\\(separate\\|new\\|abstract\\)\\>"
  2559. nil orgpoint))))
  2560. (goto-char (car match-cons))
  2561. (ada-search-ignore-string-comment ada-subprog-start-re t)
  2562. (ada-get-indent-noindent orgpoint))
  2563. ;;
  2564. ;; something follows 'is'
  2565. ;;
  2566. ((and
  2567. foundis
  2568. (save-excursion (setq match-cons (ada-goto-next-non-ws orgpoint)))
  2569. (goto-char match-cons)
  2570. (ada-indent-on-previous-lines t orgpoint)))
  2571. ;;
  2572. ;; no 'is' but ';'
  2573. ;;
  2574. ((save-excursion
  2575. (ada-search-ignore-string-comment ";" nil orgpoint nil 'search-forward))
  2576. (list cur-indent 0))
  2577. ;;
  2578. ;; no 'is' or ';'
  2579. ;;
  2580. (t
  2581. (list cur-indent 'ada-broken-indent)))))
  2582. (defun ada-get-indent-noindent (orgpoint)
  2583. "Calculate the indentation when point is just before a `noindent stmt'.
  2584. ORGPOINT is the limit position used in the calculation."
  2585. (let ((label 0))
  2586. (save-excursion
  2587. (beginning-of-line)
  2588. (cond
  2589. ;; This one is called when indenting a line preceded by a multi-line
  2590. ;; subprogram declaration (in that case, we are at this point inside
  2591. ;; the parameter declaration list)
  2592. ((ada-in-paramlist-p)
  2593. (ada-previous-procedure)
  2594. (list (save-excursion (back-to-indentation) (point)) 0))
  2595. ;; This one is called when indenting the second line of a multi-line
  2596. ;; declaration section, in a declare block or a record declaration
  2597. ((looking-at "[ \t]*\\(\\sw\\|_\\)*[ \t]*,[ \t]*$")
  2598. (list (save-excursion (back-to-indentation) (point))
  2599. 'ada-broken-decl-indent))
  2600. ;; This one is called in every other case when indenting a line at the
  2601. ;; top level
  2602. (t
  2603. (if (looking-at (concat "[ \t]*" ada-block-label-re))
  2604. (setq label (- ada-label-indent))
  2605. (let (p)
  2606. ;; "with private" or "null record" cases
  2607. (if (or (save-excursion
  2608. (and (ada-search-ignore-string-comment "\\<private\\>" nil orgpoint)
  2609. (setq p (point))
  2610. (save-excursion (forward-char -7);; skip back "private"
  2611. (ada-goto-previous-word)
  2612. (looking-at "with"))))
  2613. (save-excursion
  2614. (and (ada-search-ignore-string-comment "\\<record\\>" nil orgpoint)
  2615. (setq p (point))
  2616. (save-excursion (forward-char -6);; skip back "record"
  2617. (ada-goto-previous-word)
  2618. (looking-at "null")))))
  2619. (progn
  2620. (goto-char p)
  2621. (re-search-backward "\\<\\(type\\|subtype\\)\\>" nil t)
  2622. (list (save-excursion (back-to-indentation) (point)) 0)))))
  2623. (if (save-excursion
  2624. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2625. 'search-forward))
  2626. (list (+ (save-excursion (back-to-indentation) (point)) label) 0)
  2627. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2628. 'ada-broken-indent)))))))
  2629. (defun ada-get-indent-block-label (orgpoint)
  2630. "Calculate the indentation when before a label or variable declaration.
  2631. ORGPOINT is the limit position used in the calculation."
  2632. (let ((match-cons nil)
  2633. (cur-indent (save-excursion (back-to-indentation) (point))))
  2634. (ada-search-ignore-string-comment ":" nil)
  2635. (cond
  2636. ;; loop label
  2637. ((save-excursion
  2638. (setq match-cons (ada-search-ignore-string-comment
  2639. ada-loop-start-re nil orgpoint)))
  2640. (goto-char (car match-cons))
  2641. (ada-get-indent-loop orgpoint))
  2642. ;; declare label
  2643. ((save-excursion
  2644. (setq match-cons (ada-search-ignore-string-comment
  2645. "\\<declare\\|begin\\>" nil orgpoint)))
  2646. (goto-char (car match-cons))
  2647. (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
  2648. ;; variable declaration
  2649. ((ada-in-decl-p)
  2650. (if (save-excursion
  2651. (ada-search-ignore-string-comment ";" nil orgpoint))
  2652. (list cur-indent 0)
  2653. (list cur-indent 'ada-broken-indent)))
  2654. ;; nothing follows colon
  2655. (t
  2656. (list cur-indent '(- ada-label-indent))))))
  2657. (defun ada-get-indent-goto-label (orgpoint)
  2658. "Calculate the indentation when at a goto label."
  2659. (search-forward ">>")
  2660. (ada-goto-next-non-ws)
  2661. (if (>= (point) orgpoint)
  2662. ;; labeled statement is the one we need to indent
  2663. (list (- (point) ada-label-indent))
  2664. ;; else indentation is indent for labeled statement
  2665. (ada-indent-on-previous-lines t orgpoint)))
  2666. (defun ada-get-indent-loop (orgpoint)
  2667. "Calculate the indentation when just before a loop or a for ... use.
  2668. ORGPOINT is the limit position used in the calculation."
  2669. (let ((match-cons nil)
  2670. (pos (point))
  2671. ;; If looking at a named block, skip the label
  2672. (label (save-excursion
  2673. (back-to-indentation)
  2674. (if (looking-at ada-block-label-re)
  2675. (- ada-label-indent)
  2676. 0))))
  2677. (cond
  2678. ;;
  2679. ;; statement complete
  2680. ;;
  2681. ((save-excursion
  2682. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2683. 'search-forward))
  2684. (list (+ (save-excursion (back-to-indentation) (point)) label) 0))
  2685. ;;
  2686. ;; simple loop
  2687. ;;
  2688. ((looking-at "loop\\>")
  2689. (setq pos (ada-get-indent-block-start orgpoint))
  2690. (if (equal label 0)
  2691. pos
  2692. (list (+ (car pos) label) (cadr pos))))
  2693. ;;
  2694. ;; 'for'- loop (or also a for ... use statement)
  2695. ;;
  2696. ((looking-at "for\\>")
  2697. (cond
  2698. ;;
  2699. ;; for ... use
  2700. ;;
  2701. ((save-excursion
  2702. (and
  2703. (goto-char (match-end 0))
  2704. (ada-goto-next-non-ws orgpoint)
  2705. (forward-word-strictly 1)
  2706. (if (= (char-after) ?') (forward-word-strictly 1) t)
  2707. (ada-goto-next-non-ws orgpoint)
  2708. (looking-at "\\<use\\>")
  2709. ;;
  2710. ;; check if there is a 'record' before point
  2711. ;;
  2712. (progn
  2713. (setq match-cons (ada-search-ignore-string-comment
  2714. "record" nil orgpoint nil 'word-search-forward))
  2715. t)))
  2716. (if match-cons
  2717. (progn
  2718. (goto-char (car match-cons))
  2719. (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
  2720. (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
  2721. )
  2722. ;;
  2723. ;; for..loop
  2724. ;;
  2725. ((save-excursion
  2726. (setq match-cons (ada-search-ignore-string-comment
  2727. "loop" nil orgpoint nil 'word-search-forward)))
  2728. (goto-char (car match-cons))
  2729. ;;
  2730. ;; indent according to 'loop', if it's first in the line;
  2731. ;; otherwise to 'for'
  2732. ;;
  2733. (unless (save-excursion
  2734. (back-to-indentation)
  2735. (looking-at "\\<loop\\>"))
  2736. (goto-char pos))
  2737. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2738. 'ada-indent))
  2739. ;;
  2740. ;; for-statement is broken
  2741. ;;
  2742. (t
  2743. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2744. 'ada-broken-indent))))
  2745. ;;
  2746. ;; 'while'-loop
  2747. ;;
  2748. ((looking-at "while\\>")
  2749. ;;
  2750. ;; while..loop ?
  2751. ;;
  2752. (if (save-excursion
  2753. (setq match-cons (ada-search-ignore-string-comment
  2754. "loop" nil orgpoint nil 'word-search-forward)))
  2755. (progn
  2756. (goto-char (car match-cons))
  2757. ;;
  2758. ;; indent according to 'loop', if it's first in the line;
  2759. ;; otherwise to 'while'.
  2760. ;;
  2761. (unless (save-excursion
  2762. (back-to-indentation)
  2763. (looking-at "\\<loop\\>"))
  2764. (goto-char pos))
  2765. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2766. 'ada-indent))
  2767. (list (+ (save-excursion (back-to-indentation) (point)) label)
  2768. 'ada-broken-indent))))))
  2769. (defun ada-get-indent-type (orgpoint)
  2770. "Calculate the indentation when before a type statement.
  2771. ORGPOINT is the limit position used in the calculation."
  2772. (let ((match-dat nil))
  2773. (cond
  2774. ;;
  2775. ;; complete record declaration
  2776. ;;
  2777. ((save-excursion
  2778. (and
  2779. (setq match-dat (ada-search-ignore-string-comment
  2780. "end" nil orgpoint nil 'word-search-forward))
  2781. (ada-goto-next-non-ws)
  2782. (looking-at "\\<record\\>")
  2783. (forward-word-strictly 1)
  2784. (ada-goto-next-non-ws)
  2785. (= (char-after) ?\;)))
  2786. (goto-char (car match-dat))
  2787. (list (save-excursion (back-to-indentation) (point)) 0))
  2788. ;;
  2789. ;; record type
  2790. ;;
  2791. ((save-excursion
  2792. (setq match-dat (ada-search-ignore-string-comment
  2793. "record" nil orgpoint nil 'word-search-forward)))
  2794. (goto-char (car match-dat))
  2795. (list (save-excursion (back-to-indentation) (point)) 'ada-indent))
  2796. ;;
  2797. ;; complete type declaration
  2798. ;;
  2799. ((save-excursion
  2800. (ada-search-ignore-string-comment ";" nil orgpoint nil
  2801. 'search-forward))
  2802. (list (save-excursion (back-to-indentation) (point)) 0))
  2803. ;;
  2804. ;; "type ... is", but not "type ... is ...", which is broken
  2805. ;;
  2806. ((save-excursion
  2807. (and
  2808. (ada-search-ignore-string-comment "is" nil orgpoint nil
  2809. 'word-search-forward)
  2810. (not (ada-goto-next-non-ws orgpoint))))
  2811. (list (save-excursion (back-to-indentation) (point)) 'ada-broken-indent))
  2812. ;;
  2813. ;; broken statement
  2814. ;;
  2815. (t
  2816. (list (save-excursion (back-to-indentation) (point))
  2817. 'ada-broken-indent)))))
  2818. ;; -----------------------------------------------------------
  2819. ;; -- searching and matching
  2820. ;; -----------------------------------------------------------
  2821. (defun ada-goto-stmt-start (&optional ignore-goto-label)
  2822. "Move point to the beginning of the statement that point is in or after.
  2823. Return the new position of point.
  2824. As a special case, if we are looking at a closing parenthesis, skip to the
  2825. open parenthesis."
  2826. (let ((match-dat nil)
  2827. (orgpoint (point)))
  2828. (setq match-dat (ada-search-prev-end-stmt))
  2829. (if match-dat
  2830. ;;
  2831. ;; found a previous end-statement => check if anything follows
  2832. ;;
  2833. (unless (looking-at "declare")
  2834. (progn
  2835. (unless (save-excursion
  2836. (goto-char (cdr match-dat))
  2837. (ada-goto-next-non-ws orgpoint ignore-goto-label))
  2838. ;;
  2839. ;; nothing follows => it's the end-statement directly in
  2840. ;; front of point => search again
  2841. ;;
  2842. (setq match-dat (ada-search-prev-end-stmt)))
  2843. ;;
  2844. ;; if found the correct end-statement => goto next non-ws
  2845. ;;
  2846. (if match-dat
  2847. (goto-char (cdr match-dat)))
  2848. (ada-goto-next-non-ws)
  2849. ))
  2850. ;;
  2851. ;; no previous end-statement => we are at the beginning of the
  2852. ;; accessible part of the buffer
  2853. ;;
  2854. (progn
  2855. (goto-char (point-min))
  2856. ;;
  2857. ;; skip to the very first statement, if there is one
  2858. ;;
  2859. (unless (ada-goto-next-non-ws orgpoint)
  2860. (goto-char orgpoint))))
  2861. (point)))
  2862. (defun ada-search-prev-end-stmt ()
  2863. "Move point to previous end statement.
  2864. Return a cons cell whose car is the beginning and whose cdr
  2865. is the end of the match."
  2866. (let ((match-dat nil)
  2867. (found nil))
  2868. ;; search until found or beginning-of-buffer
  2869. (while
  2870. (and
  2871. (not found)
  2872. (setq match-dat (ada-search-ignore-string-comment
  2873. ada-end-stmt-re t)))
  2874. (goto-char (car match-dat))
  2875. (unless (ada-in-open-paren-p)
  2876. (cond
  2877. ((and (looking-at
  2878. "\\<\\(record\\|loop\\|select\\|else\\|then\\)\\>")
  2879. (save-excursion
  2880. (ada-goto-previous-word)
  2881. (looking-at "\\<\\(end\\|or\\|and\\)\\>[ \t]*[^;]")))
  2882. (forward-word-strictly -1))
  2883. ((looking-at "is")
  2884. (setq found
  2885. (and (save-excursion (ada-goto-previous-word)
  2886. (ada-goto-previous-word)
  2887. (not (looking-at "subtype")))
  2888. (save-excursion (goto-char (cdr match-dat))
  2889. (ada-goto-next-non-ws)
  2890. ;; words that can go after an 'is'
  2891. (not (looking-at
  2892. (eval-when-compile
  2893. (concat "\\<"
  2894. (regexp-opt
  2895. '("separate" "access" "array"
  2896. "private" "abstract" "new") t)
  2897. "\\>\\|("))))))))
  2898. ((looking-at "private")
  2899. (save-excursion
  2900. (backward-word-strictly 1)
  2901. (setq found (not (looking-at "is")))))
  2902. (t
  2903. (setq found t))
  2904. )))
  2905. (if found
  2906. match-dat
  2907. nil)))
  2908. (defun ada-goto-next-non-ws (&optional limit skip-goto-label)
  2909. "Skip to next non-whitespace character.
  2910. Skips spaces, newlines and comments, and possibly goto labels.
  2911. Return `point' if moved, nil if not.
  2912. Stop the search at LIMIT.
  2913. Do not call this function from within a string."
  2914. (unless limit
  2915. (setq limit (point-max)))
  2916. (while (and (<= (point) limit)
  2917. (or (progn (forward-comment 10000)
  2918. (if (and (not (eobp))
  2919. (save-excursion (forward-char 1)
  2920. (ada-in-string-p)))
  2921. (progn (forward-sexp 1) t)))
  2922. (and skip-goto-label
  2923. (looking-at ada-goto-label-re)
  2924. (progn
  2925. (goto-char (match-end 0))
  2926. t)))))
  2927. (if (< (point) limit)
  2928. (point)
  2929. nil)
  2930. )
  2931. (defun ada-goto-stmt-end (&optional limit)
  2932. "Move point to the end of the statement that point is in or before.
  2933. Return the new position of point or nil if not found.
  2934. Stop the search at LIMIT."
  2935. (if (ada-search-ignore-string-comment ada-end-stmt-re nil limit)
  2936. (point)
  2937. nil))
  2938. (defun ada-goto-next-word (&optional backward)
  2939. "Move point to the beginning of the next word of Ada code.
  2940. If BACKWARD is non-nil, jump to the beginning of the previous word.
  2941. Return the new position of point or nil if not found."
  2942. (let ((match-cons nil)
  2943. (orgpoint (point)))
  2944. (unless backward
  2945. (skip-syntax-forward "w_"))
  2946. (if (setq match-cons
  2947. (ada-search-ignore-string-comment "\\sw\\|\\s_" backward nil t))
  2948. ;;
  2949. ;; move to the beginning of the word found
  2950. ;;
  2951. (progn
  2952. (goto-char (car match-cons))
  2953. (skip-syntax-backward "w_")
  2954. (point))
  2955. ;;
  2956. ;; if not found, restore old position of point
  2957. ;;
  2958. (goto-char orgpoint)
  2959. 'nil)))
  2960. (defun ada-check-matching-start (keyword)
  2961. "Signal an error if matching block start is not KEYWORD.
  2962. Moves point to the matching block start."
  2963. (ada-goto-matching-start 0)
  2964. (unless (looking-at (concat "\\<" keyword "\\>"))
  2965. (error "Matching start is not `%s'" keyword)))
  2966. (defun ada-check-defun-name (defun-name)
  2967. "Check if the name of the matching defun really is DEFUN-NAME.
  2968. Assumes point to be already positioned by `ada-goto-matching-start'.
  2969. Moves point to the beginning of the declaration."
  2970. ;; named block without a `declare'; ada-goto-matching-start leaves
  2971. ;; point at start of 'begin' for a block.
  2972. (if (save-excursion
  2973. (ada-goto-previous-word)
  2974. (looking-at (concat "\\<" defun-name "\\> *:")))
  2975. t ; name matches
  2976. ;; else
  2977. ;;
  2978. ;; 'accept' or 'package' ?
  2979. ;;
  2980. (unless (looking-at ada-subprog-start-re)
  2981. (ada-goto-decl-start))
  2982. ;;
  2983. ;; 'begin' of 'procedure'/'function'/'task' or 'declare'
  2984. ;;
  2985. (save-excursion
  2986. ;;
  2987. ;; a named 'declare'-block ? => jump to the label
  2988. ;;
  2989. (if (looking-at "\\<declare\\>")
  2990. (progn
  2991. (forward-comment -1)
  2992. (backward-word-strictly 1))
  2993. ;;
  2994. ;; no, => 'procedure'/'function'/'task'/'protected'
  2995. ;;
  2996. (progn
  2997. (forward-word-strictly 2)
  2998. (backward-word-strictly 1)
  2999. ;;
  3000. ;; skip 'body' 'type'
  3001. ;;
  3002. (if (looking-at "\\<\\(body\\|type\\)\\>")
  3003. (forward-word-strictly 1))
  3004. (forward-sexp 1)
  3005. (backward-sexp 1)))
  3006. ;;
  3007. ;; should be looking-at the correct name
  3008. ;;
  3009. (unless (looking-at (concat "\\<" defun-name "\\>"))
  3010. (error "Matching defun has different name: %s"
  3011. (buffer-substring (point)
  3012. (progn (forward-sexp 1) (point))))))))
  3013. (defun ada-goto-decl-start (&optional noerror)
  3014. "Move point to the declaration start of the current construct.
  3015. If NOERROR is non-nil, return nil if no match was found;
  3016. otherwise throw error."
  3017. (let ((nest-count 1)
  3018. (regexp (eval-when-compile
  3019. (concat "\\<"
  3020. (regexp-opt
  3021. '("is" "separate" "end" "declare" "if" "new" "begin" "generic" "when") t)
  3022. "\\>")))
  3023. ;; first should be set to t if we should stop at the first
  3024. ;; "begin" we encounter.
  3025. (first t)
  3026. (count-generic nil)
  3027. (stop-at-when nil)
  3028. )
  3029. ;; Ignore "when" most of the time, except if we are looking at the
  3030. ;; beginning of a block (structure: case .. is
  3031. ;; when ... =>
  3032. ;; begin ...
  3033. ;; exception ... )
  3034. (if (looking-at "begin")
  3035. (setq stop-at-when t))
  3036. (if (or
  3037. (looking-at "\\<\\(package\\|procedure\\|function\\)\\>")
  3038. (save-excursion
  3039. (ada-search-ignore-string-comment
  3040. "\\<\\(package\\|procedure\\|function\\|generic\\)\\>" t)
  3041. (looking-at "generic")))
  3042. (setq count-generic t))
  3043. ;; search backward for interesting keywords
  3044. (while (and
  3045. (not (zerop nest-count))
  3046. (ada-search-ignore-string-comment regexp t))
  3047. ;;
  3048. ;; calculate nest-depth
  3049. ;;
  3050. (cond
  3051. ;;
  3052. ((looking-at "end")
  3053. (ada-goto-matching-start 1 noerror)
  3054. ;; In some case, two begin..end block can follow each other closely,
  3055. ;; which we have to detect, as in
  3056. ;; procedure P is
  3057. ;; procedure Q is
  3058. ;; begin
  3059. ;; end;
  3060. ;; begin -- here we should go to procedure, not begin
  3061. ;; end
  3062. (if (looking-at "begin")
  3063. (let ((loop-again t))
  3064. (save-excursion
  3065. (while loop-again
  3066. ;; If begin was just there as the beginning of a block
  3067. ;; (with no declare) then do nothing, otherwise just
  3068. ;; register that we have to find the statement that
  3069. ;; required the begin
  3070. (ada-search-ignore-string-comment
  3071. "\\<\\(declare\\|begin\\|end\\|procedure\\|function\\|task\\|package\\)\\>"
  3072. t)
  3073. (if (looking-at "end")
  3074. (ada-goto-matching-start 1 noerror t)
  3075. (setq loop-again nil)
  3076. (unless (looking-at "begin")
  3077. (setq nest-count (1+ nest-count))))
  3078. ))
  3079. )))
  3080. ;;
  3081. ((looking-at "generic")
  3082. (if count-generic
  3083. (progn
  3084. (setq first nil)
  3085. (setq nest-count (1- nest-count)))))
  3086. ;;
  3087. ((looking-at "if")
  3088. (save-excursion
  3089. (forward-word-strictly -1)
  3090. (unless (looking-at "\\<end[ \t\n]*if\\>")
  3091. (progn
  3092. (setq nest-count (1- nest-count))
  3093. (setq first nil)))))
  3094. ;;
  3095. ((looking-at "declare\\|generic")
  3096. (setq nest-count (1- nest-count))
  3097. (setq first t))
  3098. ;;
  3099. ((looking-at "is")
  3100. ;; look for things to ignore
  3101. (if
  3102. (or
  3103. ;; generic formal parameter
  3104. (looking-at "is[ t]+<>")
  3105. ;; A type definition, or a case statement. Note that the
  3106. ;; goto-matching-start above on 'end record' leaves us at
  3107. ;; 'record', not at 'type'.
  3108. ;;
  3109. ;; We get to a case statement here by calling
  3110. ;; 'ada-move-to-end' from inside a case statement; then
  3111. ;; we are not ignoring 'when'.
  3112. (save-excursion
  3113. ;; Skip type discriminants or case argument function call param list
  3114. (forward-comment -10000)
  3115. (forward-char -1)
  3116. (if (= (char-after) ?\))
  3117. (progn
  3118. (forward-char 1)
  3119. (backward-sexp 1)
  3120. (forward-comment -10000)
  3121. ))
  3122. ;; skip type or case argument name
  3123. (skip-chars-backward "a-zA-Z0-9_.'")
  3124. (ada-goto-previous-word)
  3125. (and
  3126. ;; if it's a protected type, it's the decl start we
  3127. ;; are looking for; since we didn't see the 'end'
  3128. ;; above, we are inside it.
  3129. (looking-at "\\<\\(sub\\)?type\\|case\\>")
  3130. (save-match-data
  3131. (ada-goto-previous-word)
  3132. (not (looking-at "\\<protected\\>"))))
  3133. ) ; end of type definition p
  3134. ;; null procedure declaration
  3135. (save-excursion (ada-goto-next-word) (looking-at "\\<null\\>"))
  3136. );; end or
  3137. ;; skip this construct
  3138. nil
  3139. ;; this is the right "is"
  3140. (setq nest-count (1- nest-count))
  3141. (setq first nil)))
  3142. ;;
  3143. ((looking-at "new")
  3144. (if (save-excursion
  3145. (ada-goto-previous-word)
  3146. (looking-at "is"))
  3147. (goto-char (match-beginning 0))))
  3148. ;;
  3149. ((and first
  3150. (looking-at "begin"))
  3151. (setq nest-count 0))
  3152. ;;
  3153. ((looking-at "when")
  3154. (save-excursion
  3155. (forward-word-strictly -1)
  3156. (unless (looking-at "\\<exit[ \t\n]*when\\>")
  3157. (progn
  3158. (if stop-at-when
  3159. (setq nest-count (1- nest-count)))
  3160. ))))
  3161. ;;
  3162. ((looking-at "begin")
  3163. (setq first nil))
  3164. ;;
  3165. (t
  3166. (setq nest-count (1+ nest-count))
  3167. (setq first nil)))
  3168. );; end of loop
  3169. ;; check if declaration-start is really found
  3170. (if (and
  3171. (zerop nest-count)
  3172. (if (looking-at "is")
  3173. (ada-search-ignore-string-comment ada-subprog-start-re t)
  3174. (looking-at "declare\\|generic")))
  3175. t
  3176. (if noerror nil
  3177. (error "No matching proc/func/task/declare/package/protected")))
  3178. ))
  3179. (defun ada-goto-matching-start (&optional nest-level noerror gotothen)
  3180. "Move point to the beginning of a block-start.
  3181. Which block depends on the value of NEST-LEVEL, which defaults to zero.
  3182. If NOERROR is non-nil, it only returns nil if no matching start was found.
  3183. If GOTOTHEN is non-nil, point moves to the `then' following `if'."
  3184. (let ((nest-count (if nest-level nest-level 0))
  3185. (found nil)
  3186. (last-was-begin '())
  3187. ;; List all keywords encountered while traversing
  3188. ;; something like '("end" "end" "begin")
  3189. ;; This is removed from the list when "package", "procedure",...
  3190. ;; are seen. The goal is to find whether a package has an elaboration
  3191. ;; part
  3192. (pos nil))
  3193. ;; search backward for interesting keywords
  3194. (while (and
  3195. (not found)
  3196. (ada-search-ignore-string-comment ada-matching-start-re t))
  3197. (unless (and (looking-at "\\<record\\>")
  3198. (save-excursion
  3199. (forward-word-strictly -1)
  3200. (looking-at "\\<null\\>")))
  3201. (progn
  3202. ;; calculate nest-depth
  3203. (cond
  3204. ;; found block end => increase nest depth
  3205. ((looking-at "end")
  3206. (push nil last-was-begin)
  3207. (setq nest-count (1+ nest-count)))
  3208. ;; found loop/select/record/case/if => check if it starts or
  3209. ;; ends a block
  3210. ((looking-at "loop\\|select\\|record\\|case\\|if")
  3211. (setq pos (point))
  3212. (save-excursion
  3213. ;; check if keyword follows 'end'
  3214. (ada-goto-previous-word)
  3215. (if (looking-at "\\<end\\>[ \t]*[^;]")
  3216. (progn
  3217. ;; it ends a block => increase nest depth
  3218. (setq nest-count (1+ nest-count)
  3219. pos (point))
  3220. (push nil last-was-begin))
  3221. ;; it starts a block => decrease nest depth
  3222. (setq nest-count (1- nest-count))
  3223. ;; Some nested "begin .. end" blocks with no "declare"?
  3224. ;; => remove those entries
  3225. (while (car last-was-begin)
  3226. (setq last-was-begin (cdr (cdr last-was-begin))))
  3227. (setq last-was-begin (cdr last-was-begin))
  3228. ))
  3229. (goto-char pos)
  3230. )
  3231. ;; found package start => check if it really is a block
  3232. ((looking-at "package")
  3233. (save-excursion
  3234. ;; ignore if this is just a renames statement
  3235. (let ((current (point))
  3236. (pos (ada-search-ignore-string-comment
  3237. "\\<\\(is\\|renames\\|;\\)\\>" nil)))
  3238. (if pos
  3239. (goto-char (car pos))
  3240. (error (concat
  3241. "No matching `is' or `renames' for `package' at"
  3242. " line "
  3243. (number-to-string (count-lines 1 (1+ current)))))))
  3244. (unless (looking-at "renames")
  3245. (progn
  3246. (forward-word-strictly 1)
  3247. (ada-goto-next-non-ws)
  3248. ;; ignore it if it is only a declaration with 'new'
  3249. ;; We could have package Foo is new ....
  3250. ;; or package Foo is separate;
  3251. ;; or package Foo is begin null; end Foo
  3252. ;; for elaboration code (elaboration)
  3253. (if (and (not (looking-at "\\<\\(new\\|separate\\|begin\\)\\>"))
  3254. (not (car last-was-begin)))
  3255. (setq nest-count (1- nest-count))))))
  3256. (setq last-was-begin (cdr last-was-begin))
  3257. )
  3258. ;; found task start => check if it has a body
  3259. ((looking-at "task")
  3260. (save-excursion
  3261. (forward-word-strictly 1)
  3262. (ada-goto-next-non-ws)
  3263. (cond
  3264. ((looking-at "\\<body\\>"))
  3265. ((looking-at "\\<type\\>")
  3266. ;; In that case, do nothing if there is a "is"
  3267. (forward-word-strictly 2);; skip "type"
  3268. (ada-goto-next-non-ws);; skip type name
  3269. ;; Do nothing if we are simply looking at a simple
  3270. ;; "task type name;" statement with no block
  3271. (unless (looking-at ";")
  3272. (progn
  3273. ;; Skip the parameters
  3274. (if (looking-at "(")
  3275. (ada-search-ignore-string-comment ")" nil))
  3276. (let ((tmp (ada-search-ignore-string-comment
  3277. "\\<\\(is\\|;\\)\\>" nil)))
  3278. (if tmp
  3279. (progn
  3280. (goto-char (car tmp))
  3281. (if (looking-at "is")
  3282. (setq nest-count (1- nest-count)))))))))
  3283. (t
  3284. ;; Check if that task declaration had a block attached to
  3285. ;; it (i.e do nothing if we have just "task name;")
  3286. (unless (progn (forward-word-strictly 1)
  3287. (looking-at "[ \t]*;"))
  3288. (setq nest-count (1- nest-count))))))
  3289. (setq last-was-begin (cdr last-was-begin))
  3290. )
  3291. ((looking-at "declare")
  3292. ;; remove entry for begin and end (include nested begin..end
  3293. ;; groups)
  3294. (setq last-was-begin (cdr last-was-begin))
  3295. (let ((count 1))
  3296. (while (and (> count 0))
  3297. (if (equal (car last-was-begin) t)
  3298. (setq count (1+ count))
  3299. (setq count (1- count)))
  3300. (setq last-was-begin (cdr last-was-begin))
  3301. )))
  3302. ((looking-at "protected")
  3303. ;; Ignore if this is just a declaration
  3304. (save-excursion
  3305. (let ((pos (ada-search-ignore-string-comment
  3306. "\\(\\<is\\>\\|\\<renames\\>\\|;\\)" nil)))
  3307. (if pos
  3308. (goto-char (car pos)))
  3309. (if (looking-at "is")
  3310. ;; remove entry for end
  3311. (setq last-was-begin (cdr last-was-begin)))))
  3312. (setq nest-count (1- nest-count)))
  3313. ((or (looking-at "procedure")
  3314. (looking-at "function"))
  3315. ;; Ignore if this is just a declaration
  3316. (save-excursion
  3317. (let ((pos (ada-search-ignore-string-comment
  3318. "\\(\\<is\\>\\|\\<renames\\>\\|)[ \t]*;\\)" nil)))
  3319. (if pos
  3320. (goto-char (car pos)))
  3321. (if (looking-at "is")
  3322. ;; remove entry for begin and end
  3323. (setq last-was-begin (cdr (cdr last-was-begin))))))
  3324. )
  3325. ;; all the other block starts
  3326. (t
  3327. (push (looking-at "begin") last-was-begin)
  3328. (setq nest-count (1- nest-count)))
  3329. )
  3330. ;; match is found, if nest-depth is zero
  3331. (setq found (zerop nest-count))))) ; end of loop
  3332. (if (bobp)
  3333. (point)
  3334. (if found
  3335. ;;
  3336. ;; match found => is there anything else to do ?
  3337. ;;
  3338. (progn
  3339. (cond
  3340. ;;
  3341. ;; found 'if' => skip to 'then', if it's on a separate line
  3342. ;; and GOTOTHEN is non-nil
  3343. ;;
  3344. ((and
  3345. gotothen
  3346. (looking-at "if")
  3347. (save-excursion
  3348. (ada-search-ignore-string-comment "then" nil nil nil
  3349. 'word-search-forward)
  3350. (back-to-indentation)
  3351. (looking-at "\\<then\\>")))
  3352. (goto-char (match-beginning 0)))
  3353. ;;
  3354. ;; found 'do' => skip back to 'accept' or 'return'
  3355. ;;
  3356. ((looking-at "do")
  3357. (unless (ada-search-ignore-string-comment
  3358. "\\<accept\\|return\\>" t)
  3359. (error "Missing `accept' or `return' in front of `do'"))))
  3360. (point))
  3361. (if noerror
  3362. nil
  3363. (error "No matching start"))))))
  3364. (defun ada-goto-matching-end (&optional nest-level noerror)
  3365. "Move point to the end of a block.
  3366. Which block depends on the value of NEST-LEVEL, which defaults to zero.
  3367. If NOERROR is non-nil, it only returns nil if no matching start found."
  3368. (let ((nest-count (or nest-level 0))
  3369. (regex (eval-when-compile
  3370. (concat "\\<"
  3371. (regexp-opt '("end" "loop" "select" "begin" "case"
  3372. "if" "task" "package" "record" "do"
  3373. "procedure" "function") t)
  3374. "\\>")))
  3375. found
  3376. pos
  3377. ;; First is used for subprograms: they are generally handled
  3378. ;; recursively, but of course we do not want to do that the
  3379. ;; first time (see comment below about subprograms)
  3380. (first (not (looking-at "declare"))))
  3381. ;; If we are already looking at one of the keywords, this shouldn't count
  3382. ;; in the nesting loop below, so we just make sure we don't count it.
  3383. ;; "declare" is a special case because we need to look after the "begin"
  3384. ;; keyword
  3385. (if (looking-at "\\<if\\|loop\\|case\\|begin\\>")
  3386. (forward-char 1))
  3387. ;;
  3388. ;; search forward for interesting keywords
  3389. ;;
  3390. (while (and
  3391. (not found)
  3392. (ada-search-ignore-string-comment regex nil))
  3393. ;;
  3394. ;; calculate nest-depth
  3395. ;;
  3396. (backward-word-strictly 1)
  3397. (cond
  3398. ;; procedures and functions need to be processed recursively, in
  3399. ;; case they are defined in a declare/begin block, as in:
  3400. ;; declare -- NL 0 (nested level)
  3401. ;; A : Boolean;
  3402. ;; procedure B (C : D) is
  3403. ;; begin -- NL 1
  3404. ;; null;
  3405. ;; end B; -- NL 0, and we would exit
  3406. ;; begin
  3407. ;; end; -- we should exit here
  3408. ;; processing them recursively avoids the need for any special
  3409. ;; handling.
  3410. ;; Nothing should be done if we have only the specs or a
  3411. ;; generic instantiation.
  3412. ((and (looking-at "\\<procedure\\|function\\>"))
  3413. (if first
  3414. (forward-word-strictly 1)
  3415. (setq pos (point))
  3416. (ada-search-ignore-string-comment "is\\|;")
  3417. (if (= (char-before) ?s)
  3418. (progn
  3419. (ada-goto-next-non-ws)
  3420. (unless (looking-at "\\<new\\>")
  3421. (progn
  3422. (goto-char pos)
  3423. (ada-goto-matching-end 0 t)))))))
  3424. ;; found block end => decrease nest depth
  3425. ((looking-at "\\<end\\>")
  3426. (setq nest-count (1- nest-count)
  3427. found (<= nest-count 0))
  3428. ;; skip the following keyword
  3429. (if (progn
  3430. (skip-chars-forward "end")
  3431. (ada-goto-next-non-ws)
  3432. (looking-at "\\<\\(loop\\|select\\|record\\|case\\|if\\)\\>"))
  3433. (forward-word-strictly 1)))
  3434. ;; found package start => check if it really starts a block, and is not
  3435. ;; in fact a generic instantiation for instance
  3436. ((looking-at "\\<package\\>")
  3437. (ada-search-ignore-string-comment "is" nil nil nil
  3438. 'word-search-forward)
  3439. (ada-goto-next-non-ws)
  3440. ;; ignore and skip it if it is only a 'new' package
  3441. (if (looking-at "\\<new\\>")
  3442. (goto-char (match-end 0))
  3443. (setq nest-count (1+ nest-count)
  3444. found (<= nest-count 0))))
  3445. ;; all the other block starts
  3446. (t
  3447. (if (not first)
  3448. (setq nest-count (1+ nest-count)))
  3449. (setq found (<= nest-count 0))
  3450. (forward-word-strictly 1))) ; end of 'cond'
  3451. (setq first nil))
  3452. (if found
  3453. t
  3454. (if noerror
  3455. nil
  3456. (error "No matching end")))
  3457. ))
  3458. (defun ada-search-ignore-string-comment
  3459. (search-re &optional backward limit paramlists search-func)
  3460. "Regexp-search for SEARCH-RE, ignoring comments, strings.
  3461. Returns a cons cell of begin and end of match data or nil, if not found.
  3462. If BACKWARD is non-nil, search backward; search forward otherwise.
  3463. The search stops at pos LIMIT.
  3464. If PARAMLISTS is nil, ignore parameter lists.
  3465. The search is done using SEARCH-FUNC. SEARCH-FUNC can be optimized
  3466. in case we are searching for a constant string.
  3467. Point is moved at the beginning of the SEARCH-RE."
  3468. (let (found
  3469. begin
  3470. end
  3471. parse-result)
  3472. ;; FIXME: need to pass BACKWARD to search-func!
  3473. (unless search-func
  3474. (setq search-func (if backward 're-search-backward 're-search-forward)))
  3475. ;;
  3476. ;; search until found or end-of-buffer
  3477. ;; We have to test that we do not look further than limit
  3478. ;;
  3479. (with-syntax-table ada-mode-symbol-syntax-table
  3480. (while (and (not found)
  3481. (or (not limit)
  3482. (or (and backward (<= limit (point)))
  3483. (>= limit (point))))
  3484. (funcall search-func search-re limit 1))
  3485. (setq begin (match-beginning 0))
  3486. (setq end (match-end 0))
  3487. (setq parse-result (parse-partial-sexp (point-at-bol) (point)))
  3488. (cond
  3489. ;;
  3490. ;; If inside a string, skip it (and the following comments)
  3491. ;;
  3492. ((ada-in-string-p parse-result)
  3493. (if (featurep 'xemacs)
  3494. (search-backward "\"" nil t)
  3495. (goto-char (nth 8 parse-result)))
  3496. (unless backward (forward-sexp 1)))
  3497. ;;
  3498. ;; If inside a comment, skip it (and the following comments)
  3499. ;; There is a special code for comments at the end of the file
  3500. ;;
  3501. ((ada-in-comment-p parse-result)
  3502. (if (featurep 'xemacs)
  3503. (progn
  3504. (forward-line 1)
  3505. (beginning-of-line)
  3506. (forward-comment -1))
  3507. (goto-char (nth 8 parse-result)))
  3508. (unless backward
  3509. ;; at the end of the file, it is not possible to skip a comment
  3510. ;; so we just go at the end of the line
  3511. (if (forward-comment 1)
  3512. (progn
  3513. (forward-comment 1000)
  3514. (beginning-of-line))
  3515. (end-of-line))))
  3516. ;;
  3517. ;; directly in front of a comment => skip it, if searching forward
  3518. ;;
  3519. ((and (= (char-after begin) ?-) (= (char-after (1+ begin)) ?-))
  3520. (unless backward (progn (forward-char -1) (forward-comment 1000))))
  3521. ;;
  3522. ;; found a parameter-list but should ignore it => skip it
  3523. ;;
  3524. ((and (not paramlists) (ada-in-paramlist-p))
  3525. (if backward
  3526. (search-backward "(" nil t)
  3527. (search-forward ")" nil t)))
  3528. ;;
  3529. ;; found what we were looking for
  3530. ;;
  3531. (t
  3532. (setq found t))))) ; end of loop
  3533. (if found
  3534. (cons begin end)
  3535. nil)))
  3536. ;; -------------------------------------------------------
  3537. ;; -- Testing the position of the cursor
  3538. ;; -------------------------------------------------------
  3539. (defun ada-in-decl-p ()
  3540. "Return t if point is inside a declarative part.
  3541. Assumes point to be at the end of a statement."
  3542. (or (ada-in-paramlist-p)
  3543. (save-excursion
  3544. (ada-goto-decl-start t))))
  3545. (defun ada-looking-at-semi-or ()
  3546. "Return t if looking at an `or' following a semicolon."
  3547. (save-excursion
  3548. (and (looking-at "\\<or\\>")
  3549. (progn
  3550. (forward-word-strictly 1)
  3551. (ada-goto-stmt-start)
  3552. (looking-at "\\<or\\>")))))
  3553. (defun ada-looking-at-semi-private ()
  3554. "Return t if looking at the start of a private section in a package.
  3555. Return nil if the private is part of the package name, as in
  3556. 'private package A is...' (this can only happen at top level)."
  3557. (save-excursion
  3558. (and (looking-at "\\<private\\>")
  3559. (not (looking-at "\\<private[ \t]*\\(package\\|generic\\)"))
  3560. ;; Make sure this is the start of a private section (ie after
  3561. ;; a semicolon or just after the package declaration, but not
  3562. ;; after a 'type ... is private' or 'is new ... with private'
  3563. ;;
  3564. ;; Note that a 'private' statement at the beginning of the buffer
  3565. ;; does not indicate a private section, since this is instead a
  3566. ;; 'private procedure ...'
  3567. (progn (forward-comment -1000)
  3568. (and (not (bobp))
  3569. (or (= (char-before) ?\;)
  3570. (and (forward-word-strictly -3)
  3571. (looking-at "\\<package\\>"))))))))
  3572. (defun ada-in-paramlist-p ()
  3573. "Return t if point is inside the parameter-list of a declaration, but not a subprogram call or aggregate."
  3574. (save-excursion
  3575. (and
  3576. (ada-search-ignore-string-comment "(\\|)" t nil t)
  3577. ;; inside parentheses ?
  3578. (= (char-after) ?\()
  3579. ;; We could be looking at two things here:
  3580. ;; operator definition: function "." (
  3581. ;; subprogram definition: procedure .... (
  3582. ;; Let's skip back over the first one
  3583. (progn
  3584. (skip-chars-backward " \t\n")
  3585. (if (= (char-before) ?\")
  3586. (backward-char 3)
  3587. (backward-word-strictly 1))
  3588. t)
  3589. ;; and now over the second one
  3590. (backward-word-strictly 1)
  3591. ;; We should ignore the case when the reserved keyword is in a
  3592. ;; comment (for instance, when we have:
  3593. ;; -- .... package
  3594. ;; Test (A)
  3595. ;; we should return nil
  3596. (not (ada-in-string-or-comment-p))
  3597. ;; right keyword two words before parenthesis ?
  3598. ;; Type is in this list because of discriminants
  3599. ;; pragma is not, because the syntax is that of a subprogram call.
  3600. (looking-at (eval-when-compile
  3601. (concat "\\<\\("
  3602. "procedure\\|function\\|body\\|"
  3603. "task\\|entry\\|accept\\|"
  3604. "access[ \t]+procedure\\|"
  3605. "access[ \t]+function\\|"
  3606. "type\\)\\>"))))))
  3607. (defun ada-search-ignore-complex-boolean (regexp backwardp)
  3608. "Search for REGEXP, ignoring comments, strings, `and then', `or else'.
  3609. If BACKWARDP is non-nil, search backward; search forward otherwise."
  3610. (let (result)
  3611. (while (and (setq result (ada-search-ignore-string-comment regexp backwardp))
  3612. (save-excursion (forward-word-strictly -1)
  3613. (looking-at "and then\\|or else"))))
  3614. result))
  3615. (defun ada-in-open-paren-p ()
  3616. "Non-nil if in an open parenthesis.
  3617. Return value is the position of the first non-ws behind the last unclosed
  3618. parenthesis, or nil."
  3619. (save-excursion
  3620. (let ((parse (parse-partial-sexp
  3621. (point)
  3622. (or (car (ada-search-ignore-complex-boolean
  3623. "\\<\\(;\\|is\\|then\\|loop\\|begin\\|else\\)\\>"
  3624. t))
  3625. (point-min)))))
  3626. (if (nth 1 parse)
  3627. (progn
  3628. (goto-char (1+ (nth 1 parse)))
  3629. ;; Skip blanks, if they are not followed by a comment
  3630. ;; See:
  3631. ;; type A is ( Value_0,
  3632. ;; Value_1);
  3633. ;; type B is ( -- comment
  3634. ;; Value_2);
  3635. (if (or (not ada-indent-handle-comment-special)
  3636. (not (looking-at "[ \t]+--")))
  3637. (skip-chars-forward " \t"))
  3638. (point))))))
  3639. ;; -----------------------------------------------------------
  3640. ;; -- Behavior Of TAB Key
  3641. ;; -----------------------------------------------------------
  3642. (defun ada-tab ()
  3643. "Do indenting or tabbing according to `ada-tab-policy'.
  3644. In Transient Mark mode, if the mark is active, operate on the contents
  3645. of the region. Otherwise, operate only on the current line."
  3646. (interactive)
  3647. (cond ((eq ada-tab-policy 'indent-rigidly) (ada-tab-hard))
  3648. ((eq ada-tab-policy 'indent-auto)
  3649. (if (ada-region-selected)
  3650. (ada-indent-region (region-beginning) (region-end))
  3651. (ada-indent-current)))
  3652. ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
  3653. ))
  3654. (defun ada-untab (_arg)
  3655. "Delete leading indenting according to `ada-tab-policy'."
  3656. ;; FIXME: ARG is ignored
  3657. (interactive "P")
  3658. (cond ((eq ada-tab-policy 'indent-rigidly) (ada-untab-hard))
  3659. ((eq ada-tab-policy 'indent-auto) (error "Not implemented"))
  3660. ((eq ada-tab-policy 'always-tab) (error "Not implemented"))
  3661. ))
  3662. (defun ada-indent-current-function ()
  3663. "Ada mode version of the `indent-line-function'."
  3664. (interactive "*")
  3665. (let ((starting-point (point-marker)))
  3666. (beginning-of-line)
  3667. (ada-tab)
  3668. (if (< (point) starting-point)
  3669. (goto-char starting-point))
  3670. (set-marker starting-point nil)
  3671. ))
  3672. (defun ada-tab-hard ()
  3673. "Indent current line to next tab stop."
  3674. (interactive)
  3675. (save-excursion
  3676. (beginning-of-line)
  3677. (insert-char ? ada-indent))
  3678. (if (bolp) (forward-char ada-indent)))
  3679. (defun ada-untab-hard ()
  3680. "Indent current line to previous tab stop."
  3681. (interactive)
  3682. (indent-rigidly (point-at-bol) (point-at-eol) (- 0 ada-indent)))
  3683. ;; ------------------------------------------------------------
  3684. ;; -- Miscellaneous
  3685. ;; ------------------------------------------------------------
  3686. ;; Not needed any more for Emacs 21.2, but still needed for backward
  3687. ;; compatibility
  3688. (defun ada-remove-trailing-spaces ()
  3689. "Remove trailing spaces in the whole buffer."
  3690. (interactive)
  3691. (save-match-data
  3692. (save-excursion
  3693. (save-restriction
  3694. (widen)
  3695. (goto-char (point-min))
  3696. (while (re-search-forward "[ \t]+$" (point-max) t)
  3697. (replace-match "" nil nil))))))
  3698. (defun ada-gnat-style ()
  3699. "Clean up comments, `(' and `,' for GNAT style checking switch."
  3700. (interactive)
  3701. (save-excursion
  3702. ;; The \n is required, or the line after an empty comment line is
  3703. ;; simply ignored.
  3704. (goto-char (point-min))
  3705. (while (re-search-forward "--[ \t]*\\([^-\n]\\)" nil t)
  3706. (replace-match "-- \\1")
  3707. (forward-line 1)
  3708. (beginning-of-line))
  3709. (goto-char (point-min))
  3710. (while (re-search-forward "\\>(" nil t)
  3711. (if (not (ada-in-string-or-comment-p))
  3712. (replace-match " (")))
  3713. (goto-char (point-min))
  3714. (while (re-search-forward ";--" nil t)
  3715. (forward-char -1)
  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 ")[ \t]+)" nil t)
  3724. (if (not (ada-in-string-or-comment-p))
  3725. (replace-match "))")))
  3726. (goto-char (point-min))
  3727. (while (re-search-forward "\\>:" nil t)
  3728. (if (not (ada-in-string-or-comment-p))
  3729. (replace-match " :")))
  3730. ;; Make sure there is a space after a ','.
  3731. ;; Always go back to the beginning of the match, since otherwise
  3732. ;; a statement like ('F','D','E') is incorrectly modified.
  3733. (goto-char (point-min))
  3734. (while (re-search-forward ",[ \t]*\\(.\\)" nil t)
  3735. (if (not (save-excursion
  3736. (goto-char (match-beginning 0))
  3737. (ada-in-string-or-comment-p)))
  3738. (replace-match ", \\1")))
  3739. ;; Operators should be surrounded by spaces.
  3740. (goto-char (point-min))
  3741. (while (re-search-forward
  3742. "[ \t]*\\(/=\\|\\*\\*\\|:=\\|\\.\\.\\|[-:+*/]\\)[ \t]*"
  3743. nil t)
  3744. (goto-char (match-beginning 1))
  3745. (if (or (looking-at "--")
  3746. (ada-in-string-or-comment-p))
  3747. (progn
  3748. (forward-line 1)
  3749. (beginning-of-line))
  3750. (cond
  3751. ((string= (match-string 1) "/=")
  3752. (replace-match " /= "))
  3753. ((string= (match-string 1) "..")
  3754. (replace-match " .. "))
  3755. ((string= (match-string 1) "**")
  3756. (replace-match " ** "))
  3757. ((string= (match-string 1) ":=")
  3758. (replace-match " := "))
  3759. (t
  3760. (replace-match " \\1 ")))
  3761. (forward-char 1)))
  3762. ))
  3763. ;; -------------------------------------------------------------
  3764. ;; -- Moving To Procedures/Packages/Statements
  3765. ;; -------------------------------------------------------------
  3766. (defun ada-move-to-start ()
  3767. "Move point to the matching start of the current Ada structure."
  3768. (interactive)
  3769. (let ((pos (point)))
  3770. (with-syntax-table ada-mode-symbol-syntax-table
  3771. (save-excursion
  3772. ;;
  3773. ;; do nothing if in string or comment or not on 'end ...;'
  3774. ;; or if an error occurs during processing
  3775. ;;
  3776. (or
  3777. (ada-in-string-or-comment-p)
  3778. (and (progn
  3779. (or (looking-at "[ \t]*\\<end\\>")
  3780. (backward-word-strictly 1))
  3781. (or (looking-at "[ \t]*\\<end\\>")
  3782. (backward-word-strictly 1))
  3783. (or (looking-at "[ \t]*\\<end\\>")
  3784. (error "Not on end ...;")))
  3785. (ada-goto-matching-start 1)
  3786. (setq pos (point))
  3787. ;;
  3788. ;; on 'begin' => go on, according to user option
  3789. ;;
  3790. ada-move-to-declaration
  3791. (looking-at "\\<begin\\>")
  3792. (ada-goto-decl-start)
  3793. (setq pos (point))))
  3794. ) ; end of save-excursion
  3795. ;; now really move to the found position
  3796. (goto-char pos))))
  3797. (defun ada-move-to-end ()
  3798. "Move point to the end of the block around point.
  3799. Moves to `begin' if in a declarative part."
  3800. (interactive)
  3801. (let ((pos (point))
  3802. decl-start)
  3803. (with-syntax-table ada-mode-symbol-syntax-table
  3804. (save-excursion
  3805. (cond
  3806. ;; Go to the beginning of the current word, and check if we are
  3807. ;; directly on 'begin'
  3808. ((save-excursion
  3809. (skip-syntax-backward "w")
  3810. (looking-at "\\<begin\\>"))
  3811. (ada-goto-matching-end 1))
  3812. ;; on first line of subprogram body
  3813. ;; Do nothing for specs or generic instantiation, since these are
  3814. ;; handled as the general case (find the enclosing block)
  3815. ;; We also need to make sure that we ignore nested subprograms
  3816. ((save-excursion
  3817. (and (skip-syntax-backward "w")
  3818. (looking-at "\\<function\\>\\|\\<procedure\\>" )
  3819. (ada-search-ignore-string-comment "is\\|;")
  3820. (not (= (char-before) ?\;))
  3821. ))
  3822. (skip-syntax-backward "w")
  3823. (ada-goto-matching-end 0 t))
  3824. ;; on first line of task declaration
  3825. ((save-excursion
  3826. (and (ada-goto-stmt-start)
  3827. (looking-at "\\<task\\>" )
  3828. (forward-word-strictly 1)
  3829. (ada-goto-next-non-ws)
  3830. (looking-at "\\<body\\>")))
  3831. (ada-search-ignore-string-comment "begin" nil nil nil
  3832. 'word-search-forward))
  3833. ;; accept block start
  3834. ((save-excursion
  3835. (and (ada-goto-stmt-start)
  3836. (looking-at "\\<accept\\>" )))
  3837. (ada-goto-matching-end 0))
  3838. ;; package start
  3839. ((save-excursion
  3840. (setq decl-start (and (ada-goto-decl-start t) (point)))
  3841. (and decl-start (looking-at "\\<package\\>")))
  3842. (ada-goto-matching-end 1))
  3843. ;; On a "declare" keyword
  3844. ((save-excursion
  3845. (skip-syntax-backward "w")
  3846. (looking-at "\\<declare\\>"))
  3847. (ada-goto-matching-end 0 t))
  3848. ;; inside a 'begin' ... 'end' block
  3849. (decl-start
  3850. (goto-char decl-start)
  3851. (ada-goto-matching-end 0 t))
  3852. ;; (hopefully ;-) everything else
  3853. (t
  3854. (ada-goto-matching-end 1)))
  3855. (setq pos (point))
  3856. )
  3857. ;; now really move to the position found
  3858. (goto-char pos))))
  3859. (defun ada-next-procedure ()
  3860. "Move point to next procedure."
  3861. (interactive)
  3862. (end-of-line)
  3863. (if (re-search-forward ada-procedure-start-regexp nil t)
  3864. (goto-char (match-beginning 4))
  3865. (error "No more functions/procedures/tasks")))
  3866. (defun ada-previous-procedure ()
  3867. "Move point to previous procedure."
  3868. (interactive)
  3869. (beginning-of-line)
  3870. (if (re-search-backward ada-procedure-start-regexp nil t)
  3871. (goto-char (match-beginning 4))
  3872. (error "No more functions/procedures/tasks")))
  3873. (defun ada-next-package ()
  3874. "Move point to next package."
  3875. (interactive)
  3876. (end-of-line)
  3877. (if (re-search-forward ada-package-start-regexp nil t)
  3878. (goto-char (match-beginning 1))
  3879. (error "No more packages")))
  3880. (defun ada-previous-package ()
  3881. "Move point to previous package."
  3882. (interactive)
  3883. (beginning-of-line)
  3884. (if (re-search-backward ada-package-start-regexp nil t)
  3885. (goto-char (match-beginning 1))
  3886. (error "No more packages")))
  3887. ;; ------------------------------------------------------------
  3888. ;; -- Define keymap and menus for Ada
  3889. ;; -------------------------------------------------------------
  3890. (defun ada-create-keymap ()
  3891. "Create the keymap associated with the Ada mode."
  3892. ;; All non-standard keys go into ada-mode-extra-map
  3893. (define-key ada-mode-map ada-mode-extra-prefix ada-mode-extra-map)
  3894. ;; Indentation and Formatting
  3895. (define-key ada-mode-map "\C-j" 'ada-indent-newline-indent-conditional)
  3896. (define-key ada-mode-map "\C-m" 'ada-indent-newline-indent-conditional)
  3897. (define-key ada-mode-map "\t" 'ada-tab)
  3898. (define-key ada-mode-map "\C-c\t" 'ada-justified-indent-current)
  3899. (define-key ada-mode-map "\C-c\C-l" 'ada-indent-region)
  3900. (define-key ada-mode-map [(shift tab)] 'ada-untab)
  3901. (define-key ada-mode-map "\C-c\C-f" 'ada-format-paramlist)
  3902. ;; We don't want to make meta-characters case-specific.
  3903. ;; Movement
  3904. (define-key ada-mode-map "\M-\C-e" 'ada-next-procedure)
  3905. (define-key ada-mode-map "\M-\C-a" 'ada-previous-procedure)
  3906. (define-key ada-mode-map "\C-c\C-a" 'ada-move-to-start)
  3907. (define-key ada-mode-map "\C-c\C-e" 'ada-move-to-end)
  3908. ;; Compilation
  3909. (unless (lookup-key ada-mode-map "\C-c\C-c")
  3910. (define-key ada-mode-map "\C-c\C-c" 'compile))
  3911. ;; Casing
  3912. (define-key ada-mode-map "\C-c\C-b" 'ada-adjust-case-buffer)
  3913. (define-key ada-mode-map "\C-c\C-t" 'ada-case-read-exceptions)
  3914. (define-key ada-mode-map "\C-c\C-y" 'ada-create-case-exception)
  3915. (define-key ada-mode-map "\C-c\C-\M-y" 'ada-create-case-exception-substring)
  3916. ;; On XEmacs, you can easily specify whether DEL should deletes
  3917. ;; one character forward or one character backward. Take this into
  3918. ;; account
  3919. (define-key ada-mode-map
  3920. (if (boundp 'delete-key-deletes-forward) [backspace] "\177")
  3921. 'backward-delete-char-untabify)
  3922. ;; Make body
  3923. (define-key ada-mode-map "\C-c\C-n" 'ada-make-subprogram-body)
  3924. ;; Use predefined function of Emacs19 for comments (RE)
  3925. (define-key ada-mode-map "\C-c;" 'comment-region)
  3926. (define-key ada-mode-map "\C-c:" 'ada-uncomment-region)
  3927. ;; The following keys are bound to functions defined in ada-xref.el or
  3928. ;; ada-prj,el., However, RMS rightly thinks that the code should be shared,
  3929. ;; and activated only if the right compiler is used
  3930. (define-key ada-mode-map (if (featurep 'xemacs) '(shift button3) [S-mouse-3])
  3931. 'ada-point-and-xref)
  3932. (define-key ada-mode-map [(control tab)] 'ada-complete-identifier)
  3933. (define-key ada-mode-extra-map "o" 'ff-find-other-file)
  3934. (define-key ada-mode-map "\C-c5\C-d" 'ada-goto-declaration-other-frame)
  3935. (define-key ada-mode-map "\C-c\C-d" 'ada-goto-declaration)
  3936. (define-key ada-mode-map "\C-c\C-s" 'ada-xref-goto-previous-reference)
  3937. (define-key ada-mode-map "\C-c\C-c" 'ada-compile-application)
  3938. (define-key ada-mode-extra-map "c" 'ada-change-prj)
  3939. (define-key ada-mode-extra-map "d" 'ada-set-default-project-file)
  3940. (define-key ada-mode-extra-map "g" 'ada-gdb-application)
  3941. (define-key ada-mode-map "\C-c\C-m" 'ada-set-main-compile-application)
  3942. (define-key ada-mode-extra-map "r" 'ada-run-application)
  3943. (define-key ada-mode-map "\C-c\C-o" 'ada-goto-parent)
  3944. (define-key ada-mode-map "\C-c\C-r" 'ada-find-references)
  3945. (define-key ada-mode-extra-map "l" 'ada-find-local-references)
  3946. (define-key ada-mode-map "\C-c\C-v" 'ada-check-current)
  3947. (define-key ada-mode-extra-map "f" 'ada-find-file)
  3948. (define-key ada-mode-extra-map "u" 'ada-prj-edit)
  3949. (define-key ada-mode-map "\C-xnd" 'ada-narrow-to-defun); override narrow-to-defun
  3950. ;; The templates, defined in ada-stmt.el
  3951. (let ((map (make-sparse-keymap)))
  3952. (define-key map "h" 'ada-header)
  3953. (define-key map "\C-a" 'ada-array)
  3954. (define-key map "b" 'ada-exception-block)
  3955. (define-key map "d" 'ada-declare-block)
  3956. (define-key map "c" 'ada-case)
  3957. (define-key map "\C-e" 'ada-elsif)
  3958. (define-key map "e" 'ada-else)
  3959. (define-key map "\C-k" 'ada-package-spec)
  3960. (define-key map "k" 'ada-package-body)
  3961. (define-key map "\C-p" 'ada-procedure-spec)
  3962. (define-key map "p" 'ada-subprogram-body)
  3963. (define-key map "\C-f" 'ada-function-spec)
  3964. (define-key map "f" 'ada-for-loop)
  3965. (define-key map "i" 'ada-if)
  3966. (define-key map "l" 'ada-loop)
  3967. (define-key map "\C-r" 'ada-record)
  3968. (define-key map "\C-s" 'ada-subtype)
  3969. (define-key map "S" 'ada-tabsize)
  3970. (define-key map "\C-t" 'ada-task-spec)
  3971. (define-key map "t" 'ada-task-body)
  3972. (define-key map "\C-y" 'ada-type)
  3973. (define-key map "\C-v" 'ada-private)
  3974. (define-key map "u" 'ada-use)
  3975. (define-key map "\C-u" 'ada-with)
  3976. (define-key map "\C-w" 'ada-when)
  3977. (define-key map "w" 'ada-while-loop)
  3978. (define-key map "\C-x" 'ada-exception)
  3979. (define-key map "x" 'ada-exit)
  3980. (define-key ada-mode-extra-map "t" map))
  3981. )
  3982. (defun ada-create-menu ()
  3983. "Create the Ada menu as shown in the menu bar."
  3984. (let ((m '("Ada"
  3985. ("Help"
  3986. ["Ada Mode" (info "ada-mode") t]
  3987. ["GNAT User's Guide" (info "gnat_ugn")
  3988. (eq ada-which-compiler 'gnat)]
  3989. ["GNAT Reference Manual" (info "gnat_rm")
  3990. (eq ada-which-compiler 'gnat)]
  3991. ["Gcc Documentation" (info "gcc")
  3992. (eq ada-which-compiler 'gnat)]
  3993. ["Gdb Documentation" (info "gdb")
  3994. (eq ada-which-compiler 'gnat)]
  3995. ["Ada95 Reference Manual" (info "arm95") t])
  3996. ("Options" :included (derived-mode-p 'ada-mode)
  3997. ["Auto Casing" (setq ada-auto-case (not ada-auto-case))
  3998. :style toggle :selected ada-auto-case]
  3999. ["Auto Indent After Return"
  4000. (setq ada-indent-after-return (not ada-indent-after-return))
  4001. :style toggle :selected ada-indent-after-return]
  4002. ["Automatically Recompile For Cross-references"
  4003. (setq ada-xref-create-ali (not ada-xref-create-ali))
  4004. :style toggle :selected ada-xref-create-ali
  4005. :included (eq ada-which-compiler 'gnat)]
  4006. ["Confirm Commands"
  4007. (setq ada-xref-confirm-compile (not ada-xref-confirm-compile))
  4008. :style toggle :selected ada-xref-confirm-compile
  4009. :included (eq ada-which-compiler 'gnat)]
  4010. ["Show Cross-references In Other Buffer"
  4011. (setq ada-xref-other-buffer (not ada-xref-other-buffer))
  4012. :style toggle :selected ada-xref-other-buffer
  4013. :included (eq ada-which-compiler 'gnat)]
  4014. ["Tight Integration With GNU Visual Debugger"
  4015. (setq ada-tight-gvd-integration (not ada-tight-gvd-integration))
  4016. :style toggle :selected ada-tight-gvd-integration
  4017. :included (string-match "gvd" ada-prj-default-debugger)])
  4018. ["Customize" (customize-group 'ada)
  4019. :included (fboundp 'customize-group)]
  4020. ["Check file" ada-check-current t]
  4021. ["Compile file" ada-compile-current t]
  4022. ["Set main and Build" ada-set-main-compile-application t]
  4023. ["Show main" ada-show-current-main t]
  4024. ["Build" ada-compile-application t]
  4025. ["Run" ada-run-application t]
  4026. ["Debug" ada-gdb-application (eq ada-which-compiler 'gnat)]
  4027. ["------" nil nil]
  4028. ("Project"
  4029. ["Show project" ada-show-current-project t]
  4030. ["Load..." ada-set-default-project-file t]
  4031. ["New..." ada-prj-new t]
  4032. ["Edit..." ada-prj-edit t])
  4033. ("Goto" :included (derived-mode-p 'ada-mode)
  4034. ["Goto Declaration/Body" ada-goto-declaration
  4035. (eq ada-which-compiler 'gnat)]
  4036. ["Goto Body" ada-goto-body
  4037. (eq ada-which-compiler 'gnat)]
  4038. ["Goto Declaration Other Frame"
  4039. ada-goto-declaration-other-frame
  4040. (eq ada-which-compiler 'gnat)]
  4041. ["Goto Previous Reference" ada-xref-goto-previous-reference
  4042. (eq ada-which-compiler 'gnat)]
  4043. ["List Local References" ada-find-local-references
  4044. (eq ada-which-compiler 'gnat)]
  4045. ["List References" ada-find-references
  4046. (eq ada-which-compiler 'gnat)]
  4047. ["Goto Reference To Any Entity" ada-find-any-references
  4048. (eq ada-which-compiler 'gnat)]
  4049. ["Goto Parent Unit" ada-goto-parent
  4050. (eq ada-which-compiler 'gnat)]
  4051. ["--" nil nil]
  4052. ["Next compilation error" next-error t]
  4053. ["Previous Package" ada-previous-package t]
  4054. ["Next Package" ada-next-package t]
  4055. ["Previous Procedure" ada-previous-procedure t]
  4056. ["Next Procedure" ada-next-procedure t]
  4057. ["Goto Start Of Statement" ada-move-to-start t]
  4058. ["Goto End Of Statement" ada-move-to-end t]
  4059. ["-" nil nil]
  4060. ["Other File" ff-find-other-file t]
  4061. ["Other File Other Window" ada-ff-other-window t])
  4062. ("Edit" :included (derived-mode-p 'ada-mode)
  4063. ["Search File On Source Path" ada-find-file t]
  4064. ["------" nil nil]
  4065. ["Complete Identifier" ada-complete-identifier t]
  4066. ["-----" nil nil]
  4067. ["Indent Line" ada-indent-current-function t]
  4068. ["Justify Current Indentation" ada-justified-indent-current t]
  4069. ["Indent Lines in Selection" ada-indent-region t]
  4070. ["Indent Lines in File"
  4071. (ada-indent-region (point-min) (point-max)) t]
  4072. ["Format Parameter List" ada-format-paramlist t]
  4073. ["-" nil nil]
  4074. ["Comment Selection" comment-region t]
  4075. ["Uncomment Selection" ada-uncomment-region t]
  4076. ["--" nil nil]
  4077. ["Fill Comment Paragraph" fill-paragraph t]
  4078. ["Fill Comment Paragraph Justify"
  4079. ada-fill-comment-paragraph-justify t]
  4080. ["Fill Comment Paragraph Postfix"
  4081. ada-fill-comment-paragraph-postfix t]
  4082. ["---" nil nil]
  4083. ["Adjust Case Selection" ada-adjust-case-region t]
  4084. ["Adjust Case in File" ada-adjust-case-buffer t]
  4085. ["Create Case Exception" ada-create-case-exception t]
  4086. ["Create Case Exception Substring"
  4087. ada-create-case-exception-substring t]
  4088. ["Reload Case Exceptions" ada-case-read-exceptions t]
  4089. ["----" nil nil]
  4090. ["Make body for subprogram" ada-make-subprogram-body t]
  4091. ["-----" nil nil]
  4092. ["Narrow to subprogram" ada-narrow-to-defun t])
  4093. ("Templates"
  4094. :included (derived-mode-p 'ada-mode)
  4095. ["Header" ada-header t]
  4096. ["-" nil nil]
  4097. ["Package Body" ada-package-body t]
  4098. ["Package Spec" ada-package-spec t]
  4099. ["Function Spec" ada-function-spec t]
  4100. ["Procedure Spec" ada-procedure-spec t]
  4101. ["Proc/func Body" ada-subprogram-body t]
  4102. ["Task Body" ada-task-body t]
  4103. ["Task Spec" ada-task-spec t]
  4104. ["Declare Block" ada-declare-block t]
  4105. ["Exception Block" ada-exception-block t]
  4106. ["--" nil nil]
  4107. ["Entry" ada-entry t]
  4108. ["Entry family" ada-entry-family t]
  4109. ["Select" ada-select t]
  4110. ["Accept" ada-accept t]
  4111. ["Or accept" ada-or-accept t]
  4112. ["Or delay" ada-or-delay t]
  4113. ["Or terminate" ada-or-terminate t]
  4114. ["---" nil nil]
  4115. ["Type" ada-type t]
  4116. ["Private" ada-private t]
  4117. ["Subtype" ada-subtype t]
  4118. ["Record" ada-record t]
  4119. ["Array" ada-array t]
  4120. ["----" nil nil]
  4121. ["If" ada-if t]
  4122. ["Else" ada-else t]
  4123. ["Elsif" ada-elsif t]
  4124. ["Case" ada-case t]
  4125. ["-----" nil nil]
  4126. ["While Loop" ada-while-loop t]
  4127. ["For Loop" ada-for-loop t]
  4128. ["Loop" ada-loop t]
  4129. ["------" nil nil]
  4130. ["Exception" ada-exception t]
  4131. ["Exit" ada-exit t]
  4132. ["When" ada-when t])
  4133. )))
  4134. (easy-menu-define ada-mode-menu ada-mode-map "Menu keymap for Ada mode" m)
  4135. (if (featurep 'xemacs)
  4136. (progn
  4137. (define-key ada-mode-map [menu-bar] ada-mode-menu)
  4138. (setq mode-popup-menu (cons "Ada mode" ada-mode-menu))))))
  4139. ;; -------------------------------------------------------
  4140. ;; Commenting/Uncommenting code
  4141. ;; The following two calls are provided to enhance the standard
  4142. ;; comment-region function, which only allows uncommenting if the
  4143. ;; comment is at the beginning of a line. If the line have been re-indented,
  4144. ;; we are unable to use comment-region, which makes no sense.
  4145. ;;
  4146. ;; In addition, we provide an interface to the standard comment handling
  4147. ;; function for justifying the comments.
  4148. ;; -------------------------------------------------------
  4149. (defadvice comment-region (before ada-uncomment-anywhere disable)
  4150. (if (and (consp arg) ;; a prefix with \C-u is of the form '(4), whereas
  4151. ;; \C-u 2 sets arg to '2' (fixed by S.Leake)
  4152. (derived-mode-p 'ada-mode))
  4153. (save-excursion
  4154. (let ((cs (concat "^[ \t]*" (regexp-quote comment-start))))
  4155. (goto-char beg)
  4156. (while (re-search-forward cs end t)
  4157. (replace-match comment-start))
  4158. ))))
  4159. (defun ada-uncomment-region (beg end &optional arg)
  4160. "Uncomment region BEG .. END.
  4161. ARG gives number of comment characters."
  4162. (interactive "r\nP")
  4163. ;; This advice is not needed anymore with Emacs21. However, for older
  4164. ;; versions, as well as for XEmacs, we still need to enable it.
  4165. (if (or (<= emacs-major-version 20) (featurep 'xemacs))
  4166. (progn
  4167. (ad-activate 'comment-region)
  4168. (comment-region beg end (- (or arg 2)))
  4169. (ad-deactivate 'comment-region))
  4170. (comment-region beg end (list (- (or arg 2))))
  4171. (ada-indent-region beg end)))
  4172. (defun ada-fill-comment-paragraph-justify ()
  4173. "Fill current comment paragraph and justify each line as well."
  4174. (interactive)
  4175. (ada-fill-comment-paragraph 'full))
  4176. (defun ada-fill-comment-paragraph-postfix ()
  4177. "Fill current comment paragraph and justify each line as well.
  4178. Adds `ada-fill-comment-postfix' at the end of each line."
  4179. (interactive)
  4180. (ada-fill-comment-paragraph 'full t))
  4181. (defun ada-fill-comment-paragraph (&optional justify postfix)
  4182. "Fill the current comment paragraph.
  4183. If JUSTIFY is non-nil, each line is justified as well.
  4184. If POSTFIX and JUSTIFY are non-nil, `ada-fill-comment-postfix' is appended
  4185. to each line filled and justified.
  4186. The paragraph is indented on the first line."
  4187. (interactive "P")
  4188. ;; check if inside comment or just in front a comment
  4189. (if (and (not (ada-in-comment-p))
  4190. (not (looking-at "[ \t]*--")))
  4191. (error "Not inside comment"))
  4192. (let* (indent from to
  4193. (opos (point-marker))
  4194. ;; Sets this variable to nil, otherwise it prevents
  4195. ;; fill-region-as-paragraph to work on Emacs <= 20.2
  4196. (parse-sexp-lookup-properties nil)
  4197. fill-prefix
  4198. (fill-column (current-fill-column)))
  4199. ;; Find end of paragraph
  4200. (back-to-indentation)
  4201. (while (and (not (eobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
  4202. (forward-line 1)
  4203. ;; If we were at the last line in the buffer, create a dummy empty
  4204. ;; line at the end of the buffer.
  4205. (if (eobp)
  4206. (insert "\n")
  4207. (back-to-indentation)))
  4208. (beginning-of-line)
  4209. (setq to (point-marker))
  4210. (goto-char opos)
  4211. ;; Find beginning of paragraph
  4212. (back-to-indentation)
  4213. (while (and (not (bobp)) (looking-at ".*--[ \t]*[^ \t\n]"))
  4214. (forward-line -1)
  4215. (back-to-indentation))
  4216. ;; We want one line above the first one, unless we are at the beginning
  4217. ;; of the buffer
  4218. (unless (bobp)
  4219. (forward-line 1))
  4220. (beginning-of-line)
  4221. (setq from (point-marker))
  4222. ;; Calculate the indentation we will need for the paragraph
  4223. (back-to-indentation)
  4224. (setq indent (current-column))
  4225. ;; unindent the first line of the paragraph
  4226. (delete-region from (point))
  4227. ;; Remove the old postfixes
  4228. (goto-char from)
  4229. (while (re-search-forward "--\n" to t)
  4230. (replace-match "\n"))
  4231. (goto-char (1- to))
  4232. (setq to (point-marker))
  4233. ;; Indent and justify the paragraph
  4234. (setq fill-prefix ada-fill-comment-prefix)
  4235. (set-left-margin from to indent)
  4236. (if postfix
  4237. (setq fill-column (- fill-column (length ada-fill-comment-postfix))))
  4238. (fill-region-as-paragraph from to justify)
  4239. ;; Add the postfixes if required
  4240. (if postfix
  4241. (save-restriction
  4242. (goto-char from)
  4243. (narrow-to-region from to)
  4244. (while (not (eobp))
  4245. (end-of-line)
  4246. (insert-char ? (- fill-column (current-column)))
  4247. (insert ada-fill-comment-postfix)
  4248. (forward-line))
  4249. ))
  4250. ;; In Emacs <= 20.2 and XEmacs <=20.4, there is a bug, and a newline is
  4251. ;; inserted at the end. Delete it
  4252. (if (or (featurep 'xemacs)
  4253. (<= emacs-major-version 19)
  4254. (and (= emacs-major-version 20)
  4255. (<= emacs-minor-version 2)))
  4256. (progn
  4257. (goto-char to)
  4258. (end-of-line)
  4259. (delete-char 1)))
  4260. (goto-char opos)))
  4261. ;; ---------------------------------------------------
  4262. ;; support for find-file.el
  4263. ;; These functions are used by find-file to guess the file names from
  4264. ;; unit names, and to find the other file (spec or body) from the current
  4265. ;; file (body or spec).
  4266. ;; It is also used to find in which function we are, so as to put the
  4267. ;; cursor at the correct position.
  4268. ;; Standard Ada does not force any relation between unit names and file names,
  4269. ;; so some of these functions can only be a good approximation. However, they
  4270. ;; are also overridden in `ada-xref'.el when we know that the user is using
  4271. ;; GNAT.
  4272. ;; ---------------------------------------------------
  4273. ;; Overridden when we work with GNAT, to use gnatkrunch
  4274. (defun ada-make-filename-from-adaname (adaname)
  4275. "Determine the filename in which ADANAME is found.
  4276. This matches the GNAT default naming convention, except for
  4277. pre-defined units."
  4278. (while (string-match "\\." adaname)
  4279. (setq adaname (replace-match "-" t t adaname)))
  4280. (downcase adaname)
  4281. )
  4282. (defun ada-other-file-name ()
  4283. "Return the name of the other file.
  4284. The name returned is the body if `current-buffer' is the spec,
  4285. or the spec otherwise."
  4286. (let ((is-spec nil)
  4287. (is-body nil)
  4288. (suffixes ada-spec-suffixes)
  4289. (name (buffer-file-name)))
  4290. ;; Guess whether we have a spec or a body, and get the basename of the
  4291. ;; file. Since the extension may not start with '.', we can not use
  4292. ;; file-name-extension
  4293. (while (and (not is-spec)
  4294. suffixes)
  4295. (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
  4296. (setq is-spec t
  4297. name (match-string 1 name)))
  4298. (setq suffixes (cdr suffixes)))
  4299. (if (not is-spec)
  4300. (progn
  4301. (setq suffixes ada-body-suffixes)
  4302. (while (and (not is-body)
  4303. suffixes)
  4304. (if (string-match (concat "\\(.*\\)" (car suffixes) "$") name)
  4305. (setq is-body t
  4306. name (match-string 1 name)))
  4307. (setq suffixes (cdr suffixes)))))
  4308. ;; If this wasn't in either list, return name itself
  4309. (if (not (or is-spec is-body))
  4310. name
  4311. ;; Else find the other possible names
  4312. (if is-spec
  4313. (setq suffixes ada-body-suffixes)
  4314. (setq suffixes ada-spec-suffixes))
  4315. (setq is-spec name)
  4316. (while suffixes
  4317. ;; If we are using project file, search for the other file in all
  4318. ;; the possible src directories.
  4319. (if (fboundp 'ada-find-src-file-in-dir)
  4320. (let ((other
  4321. (ada-find-src-file-in-dir
  4322. (file-name-nondirectory (concat name (car suffixes))))))
  4323. (if other
  4324. (setq is-spec other)))
  4325. ;; Else search in the current directory
  4326. (if (file-exists-p (concat name (car suffixes)))
  4327. (setq is-spec (concat name (car suffixes)))))
  4328. (setq suffixes (cdr suffixes)))
  4329. is-spec)))
  4330. (defun ada-which-function-are-we-in ()
  4331. "Return the name of the function whose definition/declaration point is in.
  4332. Used in `ff-pre-load-hook'."
  4333. (setq ff-function-name nil)
  4334. (save-excursion
  4335. (end-of-line);; make sure we get the complete name
  4336. (or (if (re-search-backward ada-procedure-start-regexp nil t)
  4337. (setq ff-function-name (match-string 5)))
  4338. (if (re-search-backward ada-package-start-regexp nil t)
  4339. (setq ff-function-name (match-string 4))))
  4340. ))
  4341. (defvar ada-last-which-function-line -1
  4342. "Last line on which `ada-which-function' was called.")
  4343. (defvar ada-last-which-function-subprog 0
  4344. "Last subprogram name returned by `ada-which-function'.")
  4345. (make-variable-buffer-local 'ada-last-which-function-subprog)
  4346. (make-variable-buffer-local 'ada-last-which-function-line)
  4347. (defun ada-which-function ()
  4348. "Return the name of the function whose body the point is in.
  4349. This function works even in the case of nested subprograms, whereas the
  4350. standard Emacs function `which-function' does not.
  4351. Since the search can be long, the results are cached."
  4352. (let ((line (count-lines 1 (point)))
  4353. (pos (point))
  4354. end-pos
  4355. func-name indent
  4356. found)
  4357. ;; If this is the same line as before, simply return the same result
  4358. (if (= line ada-last-which-function-line)
  4359. ada-last-which-function-subprog
  4360. (save-excursion
  4361. ;; In case the current line is also the beginning of the body
  4362. (end-of-line)
  4363. ;; Are we looking at "function Foo\n (paramlist)"
  4364. (skip-chars-forward " \t\n(")
  4365. (condition-case nil
  4366. (up-list 1)
  4367. (error nil))
  4368. (skip-chars-forward " \t\n")
  4369. (if (looking-at "return")
  4370. (progn
  4371. (forward-word-strictly 1)
  4372. (skip-chars-forward " \t\n")
  4373. (skip-chars-forward "a-zA-Z0-9_'")))
  4374. ;; Can't simply do forward-word, in case the "is" is not on the
  4375. ;; same line as the closing parenthesis
  4376. (skip-chars-forward "is \t\n")
  4377. ;; No look for the closest subprogram body that has not ended yet.
  4378. ;; Not that we expect all the bodies to be finished by "end <name>",
  4379. ;; or a simple "end;" indented in the same column as the start of
  4380. ;; the subprogram. The goal is to be as efficient as possible.
  4381. (while (and (not found)
  4382. (re-search-backward ada-imenu-subprogram-menu-re nil t))
  4383. ;; Get the function name, but not the properties, or this changes
  4384. ;; the face in the mode line on Emacs 21
  4385. (setq func-name (match-string-no-properties 3))
  4386. (if (and (not (ada-in-comment-p))
  4387. (not (save-excursion
  4388. (goto-char (match-end 0))
  4389. (looking-at "[ \t\n]*new"))))
  4390. (save-excursion
  4391. (back-to-indentation)
  4392. (setq indent (current-column))
  4393. (if (ada-search-ignore-string-comment
  4394. (concat "end[ \t]+" func-name "[ \t]*;\\|^"
  4395. (make-string indent ? ) "end;"))
  4396. (setq end-pos (point))
  4397. (setq end-pos (point-max)))
  4398. (if (>= end-pos pos)
  4399. (setq found func-name))))
  4400. )
  4401. (setq ada-last-which-function-line line
  4402. ada-last-which-function-subprog found)
  4403. found))))
  4404. (defun ada-ff-other-window ()
  4405. "Find other file in other window using `ff-find-other-file'."
  4406. (interactive)
  4407. (and (fboundp 'ff-find-other-file)
  4408. (ff-find-other-file t)))
  4409. (defun ada-set-point-accordingly ()
  4410. "Move to the function declaration that was set by `ff-which-function-are-we-in'."
  4411. (if ff-function-name
  4412. (progn
  4413. (goto-char (point-min))
  4414. (unless (ada-search-ignore-string-comment
  4415. (concat ff-function-name "\\b") nil)
  4416. (goto-char (point-min))))))
  4417. (defun ada-get-body-name (&optional spec-name)
  4418. "Return the file name for the body of SPEC-NAME.
  4419. If SPEC-NAME is nil, return the body for the current package.
  4420. Return nil if no body was found."
  4421. (interactive)
  4422. (unless spec-name (setq spec-name (buffer-file-name)))
  4423. ;; Remove the spec extension. We can not simply remove the file extension,
  4424. ;; but we need to take into account the specific non-GNAT extensions that the
  4425. ;; user might have specified.
  4426. (let ((suffixes ada-spec-suffixes)
  4427. end)
  4428. (while suffixes
  4429. (setq end (- (length spec-name) (length (car suffixes))))
  4430. (if (string-equal (car suffixes) (substring spec-name end))
  4431. (setq spec-name (substring spec-name 0 end)))
  4432. (setq suffixes (cdr suffixes))))
  4433. ;; If find-file.el was available, use its functions
  4434. (if (fboundp 'ff-get-file-name)
  4435. (ff-get-file-name ada-search-directories-internal
  4436. (ada-make-filename-from-adaname
  4437. (file-name-nondirectory
  4438. (file-name-sans-extension spec-name)))
  4439. ada-body-suffixes)
  4440. ;; Else emulate it very simply
  4441. (concat (ada-make-filename-from-adaname
  4442. (file-name-nondirectory
  4443. (file-name-sans-extension spec-name)))
  4444. ".adb")))
  4445. ;; ---------------------------------------------------
  4446. ;; support for font-lock.el
  4447. ;; Strings are a real pain in Ada because a single quote character is
  4448. ;; overloaded as a string quote and type/instance delimiter. By default, a
  4449. ;; single quote is given punctuation syntax in `ada-mode-syntax-table'.
  4450. ;; So, for Font Lock mode purposes, we mark single quotes as having string
  4451. ;; syntax when the gods that created Ada determine them to be.
  4452. ;;
  4453. ;; This only works in Emacs. See the comments before the grammar functions
  4454. ;; at the beginning of this file for how this is done with XEmacs.
  4455. ;; ----------------------------------------------------
  4456. (defconst ada-font-lock-syntactic-keywords
  4457. ;; Mark single quotes as having string quote syntax in 'c' instances.
  4458. ;; We used to explicitly avoid ''' as a special case for fear the buffer
  4459. ;; be highlighted as a string, but it seems this fear is unfounded.
  4460. ;;
  4461. ;; This sets the properties of the characters, so that ada-in-string-p
  4462. ;; correctly handles '"' too...
  4463. '(("[^a-zA-Z0-9)]\\('\\)[^\n]\\('\\)" (1 (7 . ?')) (2 (7 . ?')))
  4464. ("^[ \t]*\\(#\\(if\\|else\\|elsif\\|end\\)\\)" (1 (11 . ?\n)))))
  4465. (defvar ada-font-lock-keywords
  4466. (eval-when-compile
  4467. (list
  4468. ;;
  4469. ;; handle "type T is access function return S;"
  4470. (list "\\<\\(function[ \t]+return\\)\\>" '(1 font-lock-keyword-face) )
  4471. ;; preprocessor line
  4472. (list "^[ \t]*\\(#.*\n\\)" '(1 font-lock-type-face t))
  4473. ;;
  4474. ;; accept, entry, function, package (body), protected (body|type),
  4475. ;; pragma, procedure, task (body) plus name.
  4476. (list (concat
  4477. "\\<\\("
  4478. "accept\\|"
  4479. "entry\\|"
  4480. "function\\|"
  4481. "package[ \t]+body\\|"
  4482. "package\\|"
  4483. "pragma\\|"
  4484. "procedure\\|"
  4485. "protected[ \t]+body\\|"
  4486. "protected[ \t]+type\\|"
  4487. "protected\\|"
  4488. "task[ \t]+body\\|"
  4489. "task[ \t]+type\\|"
  4490. "task"
  4491. "\\)\\>[ \t]*"
  4492. "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
  4493. '(1 font-lock-keyword-face) '(2 font-lock-function-name-face nil t))
  4494. ;;
  4495. ;; Optional keywords followed by a type name.
  4496. (list (concat ; ":[ \t]*"
  4497. "\\<\\(access[ \t]+all\\|access[ \t]+constant\\|access\\|constant\\|in[ \t]+reverse\\|\\|in[ \t]+out\\|in\\|out\\)\\>"
  4498. "[ \t]*"
  4499. "\\(\\sw+\\(\\.\\sw*\\)*\\)?")
  4500. '(1 font-lock-keyword-face nil t) '(2 font-lock-type-face nil t))
  4501. ;;
  4502. ;; Main keywords, except those treated specially below.
  4503. (concat "\\<"
  4504. (regexp-opt
  4505. '("abort" "abs" "abstract" "accept" "access" "aliased" "all"
  4506. "and" "array" "at" "begin" "case" "declare" "delay" "delta"
  4507. "digits" "do" "else" "elsif" "entry" "exception" "exit" "for"
  4508. "generic" "if" "in" "interface" "is" "limited" "loop" "mod" "not"
  4509. "null" "or" "others" "overriding" "private" "protected" "raise"
  4510. "range" "record" "rem" "renames" "requeue" "return" "reverse"
  4511. "select" "separate" "synchronized" "tagged" "task" "terminate"
  4512. "then" "until" "when" "while" "with" "xor") t)
  4513. "\\>")
  4514. ;;
  4515. ;; Anything following end and not already fontified is a body name.
  4516. '("\\<\\(end\\)\\>\\([ \t]+\\)?\\(\\(\\sw\\|[_.]\\)+\\)?"
  4517. (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
  4518. ;;
  4519. ;; Keywords followed by a type or function name.
  4520. (list (concat "\\<\\("
  4521. "new\\|of\\|subtype\\|type"
  4522. "\\)\\>[ \t]*\\(\\sw+\\(\\.\\sw*\\)*\\)?[ \t]*\\((\\)?")
  4523. '(1 font-lock-keyword-face)
  4524. '(2 (if (match-beginning 4)
  4525. font-lock-function-name-face
  4526. font-lock-type-face) nil t))
  4527. ;;
  4528. ;; Keywords followed by a (comma separated list of) reference.
  4529. ;; Note that font-lock only works on single lines, thus we can not
  4530. ;; correctly highlight a with_clause that spans multiple lines.
  4531. (list (concat "\\<\\(goto\\|raise\\|use\\|with\\)"
  4532. "[ \t]+\\([a-zA-Z0-9_., \t]+\\)\\W")
  4533. '(1 font-lock-keyword-face) '(2 font-lock-constant-face nil t))
  4534. ;;
  4535. ;; Goto tags.
  4536. '("<<\\(\\sw+\\)>>" 1 font-lock-constant-face)
  4537. ;; Highlight based-numbers (R. Reagan <robin-reply@reagans.org>)
  4538. (list "\\([0-9]+#[0-9a-fA-F_]+#\\)" '(1 font-lock-constant-face t))
  4539. ;; Ada unnamed numerical constants
  4540. (list "\\W\\([-+]?[0-9._]+\\)\\>" '(1 font-lock-constant-face))
  4541. ))
  4542. "Default expressions to highlight in Ada mode.")
  4543. ;; ---------------------------------------------------------
  4544. ;; Support for outline.el
  4545. ;; ---------------------------------------------------------
  4546. (defun ada-outline-level ()
  4547. "This is so that `current-column' DTRT in otherwise-hidden text."
  4548. ;; patch from Dave Love <fx@gnu.org>
  4549. (let (buffer-invisibility-spec)
  4550. (save-excursion
  4551. (back-to-indentation)
  4552. (current-column))))
  4553. ;; ---------------------------------------------------------
  4554. ;; Support for narrow-to-region
  4555. ;; ---------------------------------------------------------
  4556. (defun ada-narrow-to-defun (&optional _arg)
  4557. "Make text outside current subprogram invisible.
  4558. The subprogram visible is the one that contains or follow point.
  4559. Optional ARG is ignored.
  4560. Use \\[widen] to go back to the full visibility for the buffer."
  4561. (interactive)
  4562. (save-excursion
  4563. (let (end)
  4564. (widen)
  4565. (forward-line 1)
  4566. (ada-previous-procedure)
  4567. (setq end (point-at-bol))
  4568. (ada-move-to-end)
  4569. (end-of-line)
  4570. (narrow-to-region end (point))
  4571. (message
  4572. "Use M-x widen to get back to full visibility in the buffer"))))
  4573. ;; ---------------------------------------------------------
  4574. ;; Automatic generation of code
  4575. ;; The Ada mode has a set of function to automatically generate a subprogram
  4576. ;; or package body from its spec.
  4577. ;; These function only use a primary and basic algorithm, this could use a
  4578. ;; lot of improvement.
  4579. ;; When the user is using GNAT, we rather use gnatstub to generate an accurate
  4580. ;; body.
  4581. ;; ----------------------------------------------------------
  4582. (defun ada-gen-treat-proc (match)
  4583. "Make dummy body of a procedure/function specification.
  4584. MATCH is a cons cell containing the start and end locations of the last search
  4585. for `ada-procedure-start-regexp'."
  4586. (goto-char (car match))
  4587. (let (func-found procname functype)
  4588. (cond
  4589. ((or (looking-at "^[ \t]*procedure")
  4590. (setq func-found (looking-at "^[ \t]*function")))
  4591. ;; treat it as a proc/func
  4592. (forward-word-strictly 2)
  4593. (forward-word-strictly -1)
  4594. (setq procname (buffer-substring (point) (cdr match))) ; store proc name
  4595. ;; goto end of procname
  4596. (goto-char (cdr match))
  4597. ;; skip over parameterlist
  4598. (unless (looking-at "[ \t\n]*\\(;\\|return\\)")
  4599. (forward-sexp))
  4600. ;; if function, skip over 'return' and result type.
  4601. (if func-found
  4602. (progn
  4603. (forward-word-strictly 1)
  4604. (skip-chars-forward " \t\n")
  4605. (setq functype (buffer-substring (point)
  4606. (progn
  4607. (skip-chars-forward
  4608. "a-zA-Z0-9_.")
  4609. (point))))))
  4610. ;; look for next non WS
  4611. (cond
  4612. ((looking-at "[ \t]*;")
  4613. (delete-region (match-beginning 0) (match-end 0));; delete the ';'
  4614. (ada-indent-newline-indent)
  4615. (insert "is")
  4616. (ada-indent-newline-indent)
  4617. (if func-found
  4618. (progn
  4619. (insert "Result : " functype ";")
  4620. (ada-indent-newline-indent)))
  4621. (insert "begin")
  4622. (ada-indent-newline-indent)
  4623. (if func-found
  4624. (insert "return Result;")
  4625. (insert "null;"))
  4626. (ada-indent-newline-indent)
  4627. (insert "end " procname ";")
  4628. (ada-indent-newline-indent)
  4629. )
  4630. ((looking-at "[ \t\n]*is")
  4631. ;; do nothing
  4632. )
  4633. ((looking-at "[ \t\n]*rename")
  4634. ;; do nothing
  4635. )
  4636. (t
  4637. (message "unknown syntax"))))
  4638. (t
  4639. (if (looking-at "^[ \t]*task")
  4640. (progn
  4641. (message "Task conversion is not yet implemented")
  4642. (forward-word-strictly 2)
  4643. (if (looking-at "[ \t]*;")
  4644. (forward-line)
  4645. (ada-move-to-end))
  4646. ))))))
  4647. (defun ada-make-body ()
  4648. "Create an Ada package body in the current buffer.
  4649. The spec must be the previously visited buffer.
  4650. This function typically is to be hooked into `ff-file-created-hook'."
  4651. (delete-region (point-min) (point-max))
  4652. (insert-buffer-substring (car (cdr (buffer-list))))
  4653. (goto-char (point-min))
  4654. (ada-mode)
  4655. (let (found ada-procedure-or-package-start-regexp)
  4656. (if (setq found
  4657. (ada-search-ignore-string-comment ada-package-start-regexp nil))
  4658. (progn (goto-char (cdr found))
  4659. (insert " body")
  4660. )
  4661. (error "No package"))
  4662. (setq ada-procedure-or-package-start-regexp
  4663. (concat ada-procedure-start-regexp
  4664. "\\|"
  4665. ada-package-start-regexp))
  4666. (while (setq found
  4667. (ada-search-ignore-string-comment
  4668. ada-procedure-or-package-start-regexp nil))
  4669. (progn
  4670. (goto-char (car found))
  4671. (if (looking-at ada-package-start-regexp)
  4672. (progn (goto-char (cdr found))
  4673. (insert " body"))
  4674. (ada-gen-treat-proc found))))))
  4675. (defun ada-make-subprogram-body ()
  4676. "Create a dummy subprogram body in package body file from spec surrounding point."
  4677. (interactive)
  4678. (let* ((found (re-search-backward ada-procedure-start-regexp nil t))
  4679. (spec (match-beginning 0))
  4680. body-file)
  4681. (if found
  4682. (progn
  4683. (goto-char spec)
  4684. (if (and (re-search-forward "(\\|;" nil t)
  4685. (= (char-before) ?\())
  4686. (progn
  4687. (ada-search-ignore-string-comment ")" nil)
  4688. (ada-search-ignore-string-comment ";" nil)))
  4689. (setq spec (buffer-substring spec (point)))
  4690. ;; If find-file.el was available, use its functions
  4691. (setq body-file (ada-get-body-name))
  4692. (if body-file
  4693. (find-file body-file)
  4694. (error "No body found for the package. Create it first"))
  4695. (save-restriction
  4696. (widen)
  4697. (goto-char (point-max))
  4698. (forward-comment -10000)
  4699. (re-search-backward "\\<end\\>" nil t)
  4700. ;; Move to the beginning of the elaboration part, if any
  4701. (re-search-backward "^begin" nil t)
  4702. (newline)
  4703. (forward-char -1)
  4704. (insert spec)
  4705. (re-search-backward ada-procedure-start-regexp nil t)
  4706. (ada-gen-treat-proc (cons (match-beginning 0) (match-end 0)))
  4707. ))
  4708. (error "Not in subprogram spec"))))
  4709. ;; --------------------------------------------------------
  4710. ;; Global initializations
  4711. ;; --------------------------------------------------------
  4712. ;; Create the keymap once and for all. If we do that in ada-mode,
  4713. ;; the keys changed in the user's .emacs have to be modified
  4714. ;; every time
  4715. (ada-create-keymap)
  4716. (ada-create-menu)
  4717. ;; Add the default extensions (and set up speedbar)
  4718. (ada-add-extensions ".ads" ".adb")
  4719. ;; This two files are generated by GNAT when running with -gnatD
  4720. (if (equal ada-which-compiler 'gnat)
  4721. (ada-add-extensions ".ads.dg" ".adb.dg"))
  4722. ;; Read the special cases for exceptions
  4723. (ada-case-read-exceptions)
  4724. ;; Setup auto-loading of the other Ada mode files.
  4725. (autoload 'ada-change-prj "ada-xref" nil t)
  4726. (autoload 'ada-check-current "ada-xref" nil t)
  4727. (autoload 'ada-compile-application "ada-xref" nil t)
  4728. (autoload 'ada-compile-current "ada-xref" nil t)
  4729. (autoload 'ada-complete-identifier "ada-xref" nil t)
  4730. (autoload 'ada-find-file "ada-xref" nil t)
  4731. (autoload 'ada-find-any-references "ada-xref" nil t)
  4732. (autoload 'ada-find-src-file-in-dir "ada-xref" nil t)
  4733. (autoload 'ada-find-local-references "ada-xref" nil t)
  4734. (autoload 'ada-find-references "ada-xref" nil t)
  4735. (autoload 'ada-gdb-application "ada-xref" nil t)
  4736. (autoload 'ada-goto-declaration "ada-xref" nil t)
  4737. (autoload 'ada-goto-declaration-other-frame "ada-xref" nil t)
  4738. (autoload 'ada-goto-parent "ada-xref" nil t)
  4739. (autoload 'ada-make-body-gnatstub "ada-xref" nil t)
  4740. (autoload 'ada-point-and-xref "ada-xref" nil t)
  4741. (autoload 'ada-reread-prj-file "ada-xref" nil t)
  4742. (autoload 'ada-run-application "ada-xref" nil t)
  4743. (autoload 'ada-set-default-project-file "ada-xref" nil t)
  4744. (autoload 'ada-xref-goto-previous-reference "ada-xref" nil t)
  4745. (autoload 'ada-set-main-compile-application "ada-xref" nil t)
  4746. (autoload 'ada-show-current-main "ada-xref" nil t)
  4747. (autoload 'ada-customize "ada-prj" nil t)
  4748. (autoload 'ada-prj-edit "ada-prj" nil t)
  4749. (autoload 'ada-prj-new "ada-prj" nil t)
  4750. (autoload 'ada-prj-save "ada-prj" nil t)
  4751. (autoload 'ada-array "ada-stmt" nil t)
  4752. (autoload 'ada-case "ada-stmt" nil t)
  4753. (autoload 'ada-declare-block "ada-stmt" nil t)
  4754. (autoload 'ada-else "ada-stmt" nil t)
  4755. (autoload 'ada-elsif "ada-stmt" nil t)
  4756. (autoload 'ada-exception "ada-stmt" nil t)
  4757. (autoload 'ada-exception-block "ada-stmt" nil t)
  4758. (autoload 'ada-exit "ada-stmt" nil t)
  4759. (autoload 'ada-for-loop "ada-stmt" nil t)
  4760. (autoload 'ada-function-spec "ada-stmt" nil t)
  4761. (autoload 'ada-header "ada-stmt" nil t)
  4762. (autoload 'ada-if "ada-stmt" nil t)
  4763. (autoload 'ada-loop "ada-stmt" nil t)
  4764. (autoload 'ada-package-body "ada-stmt" nil t)
  4765. (autoload 'ada-package-spec "ada-stmt" nil t)
  4766. (autoload 'ada-private "ada-stmt" nil t)
  4767. (autoload 'ada-procedure-spec "ada-stmt" nil t)
  4768. (autoload 'ada-record "ada-stmt" nil t)
  4769. (autoload 'ada-subprogram-body "ada-stmt" nil t)
  4770. (autoload 'ada-subtype "ada-stmt" nil t)
  4771. (autoload 'ada-tabsize "ada-stmt" nil t)
  4772. (autoload 'ada-task-body "ada-stmt" nil t)
  4773. (autoload 'ada-task-spec "ada-stmt" nil t)
  4774. (autoload 'ada-type "ada-stmt" nil t)
  4775. (autoload 'ada-use "ada-stmt" nil t)
  4776. (autoload 'ada-when "ada-stmt" nil t)
  4777. (autoload 'ada-while-loop "ada-stmt" nil t)
  4778. (autoload 'ada-with "ada-stmt" nil t)
  4779. ;;; provide ourselves
  4780. (provide 'ada-mode)
  4781. ;;; ada-mode.el ends here