prolog.el 159 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200
  1. ;;; prolog.el --- major mode for editing and running Prolog (and Mercury) code
  2. ;; Copyright (C) 1986-1987, 1997-1999, 2002-2003, 2011-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Authors: Emil Åström <emil_astrom(at)hotmail(dot)com>
  5. ;; Milan Zamazal <pdm(at)freesoft(dot)cz>
  6. ;; Stefan Bruda <stefan(at)bruda(dot)ca>
  7. ;; * See below for more details
  8. ;; Maintainer: Stefan Bruda <stefan(at)bruda(dot)ca>
  9. ;; Keywords: prolog major mode sicstus swi mercury
  10. (defvar prolog-mode-version "1.22"
  11. "Prolog mode version number.")
  12. ;; This file is part of GNU Emacs.
  13. ;; GNU Emacs is free software: you can redistribute it and/or modify
  14. ;; it under the terms of the GNU General Public License as published by
  15. ;; the Free Software Foundation, either version 3 of the License, or
  16. ;; (at your option) any later version.
  17. ;; GNU Emacs is distributed in the hope that it will be useful,
  18. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. ;; GNU General Public License for more details.
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  23. ;; Original author: Masanobu UMEDA <umerin(at)mse(dot)kyutech(dot)ac(dot)jp>
  24. ;; Parts of this file was taken from a modified version of the original
  25. ;; by Johan Andersson, Peter Olin, Mats Carlsson, Johan Bevemyr, Stefan
  26. ;; Andersson, and Per Danielsson (all SICS people), and Henrik Båkman
  27. ;; at Uppsala University, Sweden.
  28. ;;
  29. ;; Some ideas and also a few lines of code have been borrowed (not stolen ;-)
  30. ;; from Oz.el, the Emacs major mode for the Oz programming language,
  31. ;; Copyright (C) 1993 DFKI GmbH, Germany, with permission.
  32. ;; Authored by Ralf Scheidhauer and Michael Mehl
  33. ;; ([scheidhr|mehl](at)dfki(dot)uni-sb(dot)de)
  34. ;;
  35. ;; More ideas and code have been taken from the SICStus debugger mode
  36. ;; (http://www.csd.uu.se/~perm/source_debug/index.shtml -- broken link
  37. ;; as of Mon May 5 08:23:48 EDT 2003) by Per Mildner.
  38. ;;
  39. ;; Additions for ECLiPSe and other helpful suggestions: Stephan Heuel
  40. ;; <heuel(at)ipb(dot)uni-bonn(dot)de>
  41. ;;; Commentary:
  42. ;;
  43. ;; This package provides a major mode for editing Prolog code, with
  44. ;; all the bells and whistles one would expect, including syntax
  45. ;; highlighting and auto indentation. It can also send regions to an
  46. ;; inferior Prolog process.
  47. ;;
  48. ;; The code requires the comint, easymenu, info, imenu, and font-lock
  49. ;; libraries. These are normally distributed with GNU Emacs and
  50. ;; XEmacs.
  51. ;;; Installation:
  52. ;;
  53. ;; Insert the following lines in your init file--typically ~/.emacs
  54. ;; (GNU Emacs and XEmacs <21.4), or ~/.xemacs/init.el (XEmacs
  55. ;; 21.4)--to use this mode when editing Prolog files under Emacs:
  56. ;;
  57. ;; (setq load-path (cons "/usr/lib/xemacs/site-lisp" load-path))
  58. ;; (autoload 'run-prolog "prolog" "Start a Prolog sub-process." t)
  59. ;; (autoload 'prolog-mode "prolog" "Major mode for editing Prolog programs." t)
  60. ;; (autoload 'mercury-mode "prolog" "Major mode for editing Mercury programs." t)
  61. ;; (setq prolog-system 'swi) ; optional, the system you are using;
  62. ;; ; see `prolog-system' below for possible values
  63. ;; (setq auto-mode-alist (append '(("\\.pl$" . prolog-mode)
  64. ;; ("\\.m$" . mercury-mode))
  65. ;; auto-mode-alist))
  66. ;;
  67. ;; where the path in the first line is the file system path to this file.
  68. ;; MSDOS paths can be written like "d:/programs/emacs-19.34/site-lisp".
  69. ;; Note: In XEmacs, either `/usr/lib/xemacs/site-lisp' (RPM default in
  70. ;; Red Hat-based distributions) or `/usr/local/lib/xemacs/site-lisp'
  71. ;; (default when compiling from sources) are automatically added to
  72. ;; `load-path', so the first line is not necessary provided that you
  73. ;; put this file in the appropriate place.
  74. ;;
  75. ;; The last s-expression above makes sure that files ending with .pl
  76. ;; are assumed to be Prolog files and not Perl, which is the default
  77. ;; Emacs setting. If this is not wanted, remove this line. It is then
  78. ;; necessary to either
  79. ;;
  80. ;; o insert in your Prolog files the following comment as the first line:
  81. ;;
  82. ;; % -*- Mode: Prolog -*-
  83. ;;
  84. ;; and then the file will be open in Prolog mode no matter its
  85. ;; extension, or
  86. ;;
  87. ;; o manually switch to prolog mode after opening a Prolog file, by typing
  88. ;; M-x prolog-mode.
  89. ;;
  90. ;; If the command to start the prolog process ('sicstus', 'pl' or
  91. ;; 'swipl' for SWI prolog, etc.) is not available in the default path,
  92. ;; then it is necessary to set the value of the environment variable
  93. ;; EPROLOG to a shell command to invoke the prolog process. In XEmacs
  94. ;; and Emacs 20+ you can also customize the variable
  95. ;; `prolog-program-name' (in the group `prolog-inferior') and provide
  96. ;; a full path for your Prolog system (swi, scitus, etc.).
  97. ;;
  98. ;; Note: I (Stefan, the current maintainer) work under XEmacs. Future
  99. ;; developments will thus be biased towards XEmacs (OK, I admit it,
  100. ;; I am biased towards XEmacs in general), though I will do my best
  101. ;; to keep the GNU Emacs compatibility. So if you work under Emacs
  102. ;; and see something that does not work do drop me a line, as I have
  103. ;; a smaller chance to notice this kind of bugs otherwise.
  104. ;; Changelog:
  105. ;; Version 1.22:
  106. ;; o Allowed both 'swipl' and 'pl' as names for the SWI Prolog
  107. ;; interpreter.
  108. ;; o Atoms that start a line are not blindly colored as
  109. ;; predicates. Instead we check that they are followed by ( or
  110. ;; :- first. Patch suggested by Guy Wiener.
  111. ;; Version 1.21:
  112. ;; o Cleaned up the code that defines faces. The missing face
  113. ;; warnings on some Emacsen should disappear.
  114. ;; Version 1.20:
  115. ;; o Improved the handling of clause start detection and multi-line
  116. ;; comments: `prolog-clause-start' no longer finds non-predicate
  117. ;; (e.g., capitalized strings) beginning of clauses.
  118. ;; `prolog-tokenize' recognizes when the end point is within a
  119. ;; multi-line comment.
  120. ;; Version 1.19:
  121. ;; o Minimal changes for Aquamacs inclusion and in general for
  122. ;; better coping with finding the Prolog executable. Patch
  123. ;; provided by David Reitter
  124. ;; Version 1.18:
  125. ;; o Fixed syntax highlighting for clause heads that do not begin at
  126. ;; the beginning of the line.
  127. ;; o Fixed compilation warnings under Emacs.
  128. ;; o Updated the email address of the current maintainer.
  129. ;; Version 1.17:
  130. ;; o Minor indentation fix (patch by Markus Triska)
  131. ;; o `prolog-underscore-wordchar-flag' defaults now to nil (more
  132. ;; consistent to other Emacs modes)
  133. ;; Version 1.16:
  134. ;; o Eliminated a possible compilation warning.
  135. ;; Version 1.15:
  136. ;; o Introduced three new customizable variables: electric colon
  137. ;; (`prolog-electric-colon-flag', default nil), electric dash
  138. ;; (`prolog-electric-dash-flag', default nil), and a possibility
  139. ;; to prevent the predicate template insertion from adding commas
  140. ;; (`prolog-electric-dot-full-predicate-template', defaults to t
  141. ;; since it seems quicker to me to just type those commas). A
  142. ;; trivial adaptation of a patch by Markus Triska.
  143. ;; o Improved the behavior of electric if-then-else to only skip
  144. ;; forward if the parenthesis/semicolon is preceded by
  145. ;; whitespace. Once more a trivial adaptation of a patch by
  146. ;; Markus Triska.
  147. ;; Version 1.14:
  148. ;; o Cleaned up align code. `prolog-align-flag' is eliminated (since
  149. ;; on a second thought it does not do anything useful). Added key
  150. ;; binding (C-c C-a) and menu entry for alignment.
  151. ;; o Condensed regular expressions for lower and upper case
  152. ;; characters (GNU Emacs seems to go over the regexp length limit
  153. ;; with the original form). My code on the matter was improved
  154. ;; considerably by Markus Triska.
  155. ;; o Fixed `prolog-insert-spaces-after-paren' (which used an
  156. ;; uninitialized variable).
  157. ;; o Minor changes to clean up the code and avoid some implicit
  158. ;; package requirements.
  159. ;; Version 1.13:
  160. ;; o Removed the use of `map-char-table' in `prolog-build-case-strings'
  161. ;; which appears to cause problems in (at least) Emacs 23.0.0.1.
  162. ;; o Added if-then-else indentation + corresponding electric
  163. ;; characters. New customization: `prolog-electric-if-then-else-flag'
  164. ;; o Align support (requires `align'). New customization:
  165. ;; `prolog-align-flag'.
  166. ;; o Temporary consult files have now the same name throughout the
  167. ;; session. This prevents issues with reconsulting a buffer
  168. ;; (this event is no longer passed to Prolog as a request to
  169. ;; consult a new file).
  170. ;; o Adaptive fill mode is now turned on. Comment indentation is
  171. ;; still worse than it could be though, I am working on it.
  172. ;; o Improved filling and auto-filling capabilities. Now block
  173. ;; comments should be [auto-]filled correctly most of the time;
  174. ;; the following pattern in particular is worth noting as being
  175. ;; filled correctly:
  176. ;; <some code here> % some comment here that goes beyond the
  177. ;; % rightmost column, possibly combined with
  178. ;; % subsequent comment lines
  179. ;; o `prolog-char-quote-workaround' now defaults to nil.
  180. ;; o Note: Many of the above improvements have been suggested by
  181. ;; Markus Triska, who also provided useful patches on the matter
  182. ;; when he realized that I was slow in responding. Many thanks.
  183. ;; Version 1.11 / 1.12
  184. ;; o GNU Emacs compatibility fix for paragraph filling (fixed
  185. ;; incorrectly in 1.11, fix fixed in 1.12).
  186. ;; Version 1.10
  187. ;; o Added paragraph filling in comment blocks and also correct auto
  188. ;; filling for comments.
  189. ;; o Fixed the possible "Regular expression too big" error in
  190. ;; `prolog-electric-dot'.
  191. ;; Version 1.9
  192. ;; o Parenthesis expressions are now indented by default so that
  193. ;; components go one underneath the other, just as for compound
  194. ;; terms. You can use the old style (the second and subsequent
  195. ;; lines being indented to the right in a parenthesis expression)
  196. ;; by setting the customizable variable `prolog-paren-indent-p'
  197. ;; (group "Prolog Indentation") to t.
  198. ;; o (Somehow awkward) handling of the 0' character escape
  199. ;; sequence. I am looking into a better way of doing it but
  200. ;; prospects look bleak. If this breaks things for you please let
  201. ;; me know and also set the `prolog-char-quote-workaround' (group
  202. ;; "Prolog Other") to nil.
  203. ;; Version 1.8
  204. ;; o Key binding fix.
  205. ;; Version 1.7
  206. ;; o Fixed a number of issues with the syntax of single quotes,
  207. ;; including Debian bug #324520.
  208. ;; Version 1.6
  209. ;; o Fixed mercury mode menu initialization (Debian bug #226121).
  210. ;; o Fixed (i.e., eliminated) Delete remapping (Debian bug #229636).
  211. ;; o Corrected indentation for clauses defining quoted atoms.
  212. ;; Version 1.5:
  213. ;; o Keywords fontifying should work in console mode so this is
  214. ;; enabled everywhere.
  215. ;; Version 1.4:
  216. ;; o Now supports GNU Prolog--minor adaptation of a patch by Stefan
  217. ;; Moeding.
  218. ;; Version 1.3:
  219. ;; o Info-follow-nearest-node now called correctly under Emacs too
  220. ;; (thanks to Nicolas Pelletier). Should be implemented more
  221. ;; elegantly (i.e., without compilation warnings) in the future.
  222. ;; Version 1.2:
  223. ;; o Another prompt fix, still in SWI mode (people seem to have
  224. ;; changed the prompt of SWI Prolog).
  225. ;; Version 1.1:
  226. ;; o Fixed dots in the end of line comments causing indentation
  227. ;; problems. The following code is now correctly indented (note
  228. ;; the dot terminating the comment):
  229. ;; a(X) :- b(X),
  230. ;; c(X). % comment here.
  231. ;; a(X).
  232. ;; and so is this (and variants):
  233. ;; a(X) :- b(X),
  234. ;; c(X). /* comment here. */
  235. ;; a(X).
  236. ;; Version 1.0:
  237. ;; o Revamped the menu system.
  238. ;; o Yet another prompt recognition fix (SWI mode).
  239. ;; o This is more of a renumbering than a new edition. I promoted
  240. ;; the mode to version 1.0 to emphasize the fact that it is now
  241. ;; mature and stable enough to be considered production (in my
  242. ;; opinion anyway).
  243. ;; Version 0.1.41:
  244. ;; o GNU Emacs compatibility fixes.
  245. ;; Version 0.1.40:
  246. ;; o prolog-get-predspec is now suitable to be called as
  247. ;; imenu-extract-index-name-function. The predicate index works.
  248. ;; o Since imenu works now as advertised, prolog-imenu-flag is t
  249. ;; by default.
  250. ;; o Eliminated prolog-create-predicate-index since the imenu
  251. ;; utilities now work well. Actually, this function is also
  252. ;; buggy, and I see no reason to fix it since we do not need it
  253. ;; anyway.
  254. ;; o Fixed prolog-pred-start, prolog-clause-start, prolog-clause-info.
  255. ;; o Fix for prolog-build-case-strings; now prolog-upper-case-string
  256. ;; and prolog-lower-case-string are correctly initialized,
  257. ;; o Various font-lock changes; most importantly, block comments (/*
  258. ;; ... */) are now correctly fontified in XEmacs even when they
  259. ;; extend on multiple lines.
  260. ;; Version 0.1.36:
  261. ;; o The debug prompt of SWI Prolog is now correctly recognized.
  262. ;; Version 0.1.35:
  263. ;; o Minor font-lock bug fixes.
  264. ;;; TODO:
  265. ;; Replace ":type 'sexp" with more precise Custom types.
  266. ;;; Code:
  267. (eval-when-compile
  268. (require 'font-lock)
  269. ;; We need imenu everywhere because of the predicate index!
  270. (require 'imenu)
  271. ;)
  272. (require 'info)
  273. (require 'shell)
  274. )
  275. (require 'comint)
  276. (require 'easymenu)
  277. (require 'align)
  278. (defgroup prolog nil
  279. "Major modes for editing and running Prolog and Mercury files."
  280. :group 'languages)
  281. (defgroup prolog-faces nil
  282. "Prolog mode specific faces."
  283. :group 'font-lock)
  284. (defgroup prolog-indentation nil
  285. "Prolog mode indentation configuration."
  286. :group 'prolog)
  287. (defgroup prolog-font-lock nil
  288. "Prolog mode font locking patterns."
  289. :group 'prolog)
  290. (defgroup prolog-keyboard nil
  291. "Prolog mode keyboard flags."
  292. :group 'prolog)
  293. (defgroup prolog-inferior nil
  294. "Inferior Prolog mode options."
  295. :group 'prolog)
  296. (defgroup prolog-other nil
  297. "Other Prolog mode options."
  298. :group 'prolog)
  299. ;;-------------------------------------------------------------------
  300. ;; User configurable variables
  301. ;;-------------------------------------------------------------------
  302. ;; General configuration
  303. (defcustom prolog-system nil
  304. "Prolog interpreter/compiler used.
  305. The value of this variable is nil or a symbol.
  306. If it is a symbol, it determines default values of other configuration
  307. variables with respect to properties of the specified Prolog
  308. interpreter/compiler.
  309. Currently recognized symbol values are:
  310. eclipse - Eclipse Prolog
  311. mercury - Mercury
  312. sicstus - SICStus Prolog
  313. swi - SWI Prolog
  314. gnu - GNU Prolog"
  315. :version "24.1"
  316. :group 'prolog
  317. :type '(choice (const :tag "SICStus" :value sicstus)
  318. (const :tag "SWI Prolog" :value swi)
  319. (const :tag "GNU Prolog" :value gnu)
  320. (const :tag "ECLiPSe Prolog" :value eclipse)
  321. ;; Mercury shouldn't be needed since we have a separate
  322. ;; major mode for it.
  323. (const :tag "Default" :value nil)))
  324. (make-variable-buffer-local 'prolog-system)
  325. ;; NB: This alist can not be processed in prolog-mode-variables to
  326. ;; create a prolog-system-version-i variable since it is needed
  327. ;; prior to the call to prolog-mode-variables.
  328. (defcustom prolog-system-version
  329. '((sicstus (3 . 6))
  330. (swi (0 . 0))
  331. (mercury (0 . 0))
  332. (eclipse (3 . 7))
  333. (gnu (0 . 0)))
  334. ;; FIXME: This should be auto-detected instead of user-provided.
  335. "Alist of Prolog system versions.
  336. The version numbers are of the format (Major . Minor)."
  337. :version "24.1"
  338. :type '(repeat (list (symbol :tag "System")
  339. (cons :tag "Version numbers" (integer :tag "Major")
  340. (integer :tag "Minor"))))
  341. :group 'prolog)
  342. ;; Indentation
  343. (defcustom prolog-indent-width 4
  344. "The indentation width used by the editing buffer."
  345. :group 'prolog-indentation
  346. :type 'integer)
  347. (defcustom prolog-align-comments-flag t
  348. "Non-nil means automatically align comments when indenting."
  349. :version "24.1"
  350. :group 'prolog-indentation
  351. :type 'boolean)
  352. (defcustom prolog-indent-mline-comments-flag t
  353. "Non-nil means indent contents of /* */ comments.
  354. Otherwise leave such lines as they are."
  355. :version "24.1"
  356. :group 'prolog-indentation
  357. :type 'boolean)
  358. (defcustom prolog-object-end-to-0-flag t
  359. "Non-nil means indent closing '}' in SICStus object definitions to level 0.
  360. Otherwise indent to `prolog-indent-width'."
  361. :version "24.1"
  362. :group 'prolog-indentation
  363. :type 'boolean)
  364. (defcustom prolog-left-indent-regexp "\\(;\\|\\*?->\\)"
  365. "Regexp for character sequences after which next line is indented.
  366. Next line after such a regexp is indented to the opening parenthesis level."
  367. :version "24.1"
  368. :group 'prolog-indentation
  369. :type 'regexp)
  370. (defcustom prolog-paren-indent-p nil
  371. "If non-nil, increase indentation for parenthesis expressions.
  372. The second and subsequent line in a parenthesis expression other than
  373. a compound term can either be indented `prolog-paren-indent' to the
  374. right (if this variable is non-nil) or in the same way as for compound
  375. terms (if this variable is nil, default)."
  376. :version "24.1"
  377. :group 'prolog-indentation
  378. :type 'boolean)
  379. (defcustom prolog-paren-indent 4
  380. "The indentation increase for parenthesis expressions.
  381. Only used in ( If -> Then ; Else) and ( Disj1 ; Disj2 ) style expressions."
  382. :version "24.1"
  383. :group 'prolog-indentation
  384. :type 'integer)
  385. (defcustom prolog-parse-mode 'beg-of-clause
  386. "The parse mode used (decides from which point parsing is done).
  387. Legal values:
  388. 'beg-of-line - starts parsing at the beginning of a line, unless the
  389. previous line ends with a backslash. Fast, but has
  390. problems detecting multiline /* */ comments.
  391. 'beg-of-clause - starts parsing at the beginning of the current clause.
  392. Slow, but copes better with /* */ comments."
  393. :version "24.1"
  394. :group 'prolog-indentation
  395. :type '(choice (const :value beg-of-line)
  396. (const :value beg-of-clause)))
  397. ;; Font locking
  398. (defcustom prolog-keywords
  399. '((eclipse
  400. ("use_module" "begin_module" "module_interface" "dynamic"
  401. "external" "export" "dbgcomp" "nodbgcomp" "compile"))
  402. (mercury
  403. ("all" "else" "end_module" "equality" "external" "fail" "func" "if"
  404. "implementation" "import_module" "include_module" "inst" "instance"
  405. "interface" "mode" "module" "not" "pragma" "pred" "some" "then" "true"
  406. "type" "typeclass" "use_module" "where"))
  407. (sicstus
  408. ("block" "dynamic" "mode" "module" "multifile" "meta_predicate"
  409. "parallel" "public" "sequential" "volatile"))
  410. (swi
  411. ("discontiguous" "dynamic" "ensure_loaded" "export" "export_list" "import"
  412. "meta_predicate" "module" "module_transparent" "multifile" "require"
  413. "use_module" "volatile"))
  414. (gnu
  415. ("built_in" "char_conversion" "discontiguous" "dynamic" "ensure_linked"
  416. "ensure_loaded" "foreign" "include" "initialization" "multifile" "op"
  417. "public" "set_prolog_flag"))
  418. (t
  419. ;; FIXME: Shouldn't we just use the union of all the above here?
  420. ("dynamic" "module")))
  421. "Alist of Prolog keywords which is used for font locking of directives."
  422. :version "24.1"
  423. :group 'prolog-font-lock
  424. :type 'sexp)
  425. (defcustom prolog-types
  426. '((mercury
  427. ("char" "float" "int" "io__state" "string" "univ"))
  428. (t nil))
  429. "Alist of Prolog types used by font locking."
  430. :version "24.1"
  431. :group 'prolog-font-lock
  432. :type 'sexp)
  433. (defcustom prolog-mode-specificators
  434. '((mercury
  435. ("bound" "di" "free" "ground" "in" "mdi" "mui" "muo" "out" "ui" "uo"))
  436. (t nil))
  437. "Alist of Prolog mode specificators used by font locking."
  438. :version "24.1"
  439. :group 'prolog-font-lock
  440. :type 'sexp)
  441. (defcustom prolog-determinism-specificators
  442. '((mercury
  443. ("cc_multi" "cc_nondet" "det" "erroneous" "failure" "multi" "nondet"
  444. "semidet"))
  445. (t nil))
  446. "Alist of Prolog determinism specificators used by font locking."
  447. :version "24.1"
  448. :group 'prolog-font-lock
  449. :type 'sexp)
  450. (defcustom prolog-directives
  451. '((mercury
  452. ("^#[0-9]+"))
  453. (t nil))
  454. "Alist of Prolog source code directives used by font locking."
  455. :version "24.1"
  456. :group 'prolog-font-lock
  457. :type 'sexp)
  458. ;; Keyboard
  459. (defcustom prolog-electric-newline-flag (not (fboundp 'electric-indent-mode))
  460. "Non-nil means automatically indent the next line when the user types RET."
  461. :version "24.1"
  462. :group 'prolog-keyboard
  463. :type 'boolean)
  464. (defcustom prolog-hungry-delete-key-flag nil
  465. "Non-nil means delete key consumes all preceding spaces."
  466. :version "24.1"
  467. :group 'prolog-keyboard
  468. :type 'boolean)
  469. (defcustom prolog-electric-dot-flag nil
  470. "Non-nil means make dot key electric.
  471. Electric dot appends newline or inserts head of a new clause.
  472. If dot is pressed at the end of a line where at least one white space
  473. precedes the point, it inserts a recursive call to the current predicate.
  474. If dot is pressed at the beginning of an empty line, it inserts the head
  475. of a new clause for the current predicate. It does not apply in strings
  476. and comments.
  477. It does not apply in strings and comments."
  478. :version "24.1"
  479. :group 'prolog-keyboard
  480. :type 'boolean)
  481. (defcustom prolog-electric-dot-full-predicate-template nil
  482. "If nil, electric dot inserts only the current predicate's name and `('
  483. for recursive calls or new clause heads. Non-nil means to also
  484. insert enough commas to cover the predicate's arity and `)',
  485. and dot and newline for recursive calls."
  486. :version "24.1"
  487. :group 'prolog-keyboard
  488. :type 'boolean)
  489. (defcustom prolog-electric-underscore-flag nil
  490. "Non-nil means make underscore key electric.
  491. Electric underscore replaces the current variable with underscore.
  492. If underscore is pressed not on a variable then it behaves as usual."
  493. :version "24.1"
  494. :group 'prolog-keyboard
  495. :type 'boolean)
  496. (defcustom prolog-electric-tab-flag nil
  497. "Non-nil means make TAB key electric.
  498. Electric TAB inserts spaces after parentheses, ->, and ;
  499. in ( If -> Then ; Else) and ( Disj1 ; Disj2 ) style expressions."
  500. :version "24.1"
  501. :group 'prolog-keyboard
  502. :type 'boolean)
  503. (defcustom prolog-electric-if-then-else-flag nil
  504. "Non-nil makes `(', `>' and `;' electric
  505. to automatically indent if-then-else constructs."
  506. :version "24.1"
  507. :group 'prolog-keyboard
  508. :type 'boolean)
  509. (defcustom prolog-electric-colon-flag nil
  510. "Makes `:' electric (inserts `:-' on a new line).
  511. If non-nil, pressing `:' at the end of a line that starts in
  512. the first column (i.e., clause heads) inserts ` :-' and newline."
  513. :version "24.1"
  514. :group 'prolog-keyboard
  515. :type 'boolean)
  516. (defcustom prolog-electric-dash-flag nil
  517. "Makes `-' electric (inserts a `-->' on a new line).
  518. If non-nil, pressing `-' at the end of a line that starts in
  519. the first column (i.e., DCG heads) inserts ` -->' and newline."
  520. :version "24.1"
  521. :group 'prolog-keyboard
  522. :type 'boolean)
  523. (defcustom prolog-old-sicstus-keys-flag nil
  524. "Non-nil means old SICStus Prolog mode keybindings are used."
  525. :version "24.1"
  526. :group 'prolog-keyboard
  527. :type 'boolean)
  528. ;; Inferior mode
  529. (defcustom prolog-program-name
  530. `(((getenv "EPROLOG") (eval (getenv "EPROLOG")))
  531. (eclipse "eclipse")
  532. (mercury nil)
  533. (sicstus "sicstus")
  534. (swi ,(if (not (executable-find "swipl")) "pl" "swipl"))
  535. (gnu "gprolog")
  536. (t ,(let ((names '("prolog" "gprolog" "swipl" "pl")))
  537. (while (and names
  538. (not (executable-find (car names))))
  539. (setq names (cdr names)))
  540. (or (car names) "prolog"))))
  541. "Alist of program names for invoking an inferior Prolog with `run-prolog'."
  542. :group 'prolog-inferior
  543. :type 'sexp)
  544. (defun prolog-program-name ()
  545. (prolog-find-value-by-system prolog-program-name))
  546. (defcustom prolog-program-switches
  547. '((sicstus ("-i"))
  548. (t nil))
  549. "Alist of switches given to inferior Prolog run with `run-prolog'."
  550. :version "24.1"
  551. :group 'prolog-inferior
  552. :type 'sexp)
  553. (defun prolog-program-switches ()
  554. (prolog-find-value-by-system prolog-program-switches))
  555. (defcustom prolog-consult-string
  556. '((eclipse "[%f].")
  557. (mercury nil)
  558. (sicstus (eval (if (prolog-atleast-version '(3 . 7))
  559. "prolog:zap_file(%m,%b,consult,%l)."
  560. "prolog:zap_file(%m,%b,consult).")))
  561. (swi "[%f].")
  562. (gnu "[%f].")
  563. (t "reconsult(%f)."))
  564. "Alist of strings defining predicate for reconsulting.
  565. Some parts of the string are replaced:
  566. `%f' by the name of the consulted file (can be a temporary file)
  567. `%b' by the file name of the buffer to consult
  568. `%m' by the module name and name of the consulted file separated by colon
  569. `%l' by the line offset into the file. This is 0 unless consulting a
  570. region of a buffer, in which case it is the number of lines before
  571. the region."
  572. :group 'prolog-inferior
  573. :type 'sexp)
  574. (defun prolog-consult-string ()
  575. (prolog-find-value-by-system prolog-consult-string))
  576. (defcustom prolog-compile-string
  577. '((eclipse "[%f].")
  578. (mercury "mmake ")
  579. (sicstus (eval (if (prolog-atleast-version '(3 . 7))
  580. "prolog:zap_file(%m,%b,compile,%l)."
  581. "prolog:zap_file(%m,%b,compile).")))
  582. (swi "[%f].")
  583. (t "compile(%f)."))
  584. "Alist of strings and lists defining predicate for recompilation.
  585. Some parts of the string are replaced:
  586. `%f' by the name of the compiled file (can be a temporary file)
  587. `%b' by the file name of the buffer to compile
  588. `%m' by the module name and name of the compiled file separated by colon
  589. `%l' by the line offset into the file. This is 0 unless compiling a
  590. region of a buffer, in which case it is the number of lines before
  591. the region.
  592. If `prolog-program-name' is non-nil, it is a string sent to a Prolog process.
  593. If `prolog-program-name' is nil, it is an argument to the `compile' function."
  594. :group 'prolog-inferior
  595. :type 'sexp)
  596. (defun prolog-compile-string ()
  597. (prolog-find-value-by-system prolog-compile-string))
  598. (defcustom prolog-eof-string "end_of_file.\n"
  599. "Alist of strings that represent end of file for prolog.
  600. nil means send actual operating system end of file."
  601. :group 'prolog-inferior
  602. :type 'sexp)
  603. (defcustom prolog-prompt-regexp
  604. '((eclipse "^[a-zA-Z0-9()]* *\\?- \\|^\\[[a-zA-Z]* [0-9]*\\]:")
  605. (sicstus "| [ ?][- ] *")
  606. (swi "^\\(\\[[a-zA-Z]*\\] \\)?[1-9]?[0-9]*[ ]?\\?- \\|^| +")
  607. (gnu "^| \\?-")
  608. (t "^|? *\\?-"))
  609. "Alist of prompts of the prolog system command line."
  610. :version "24.1"
  611. :group 'prolog-inferior
  612. :type 'sexp)
  613. (defun prolog-prompt-regexp ()
  614. (prolog-find-value-by-system prolog-prompt-regexp))
  615. ;; (defcustom prolog-continued-prompt-regexp
  616. ;; '((sicstus "^\\(| +\\| +\\)")
  617. ;; (t "^|: +"))
  618. ;; "Alist of regexps matching the prompt when consulting `user'."
  619. ;; :group 'prolog-inferior
  620. ;; :type 'sexp)
  621. (defcustom prolog-debug-on-string "debug.\n"
  622. "Predicate for enabling debug mode."
  623. :version "24.1"
  624. :group 'prolog-inferior
  625. :type 'string)
  626. (defcustom prolog-debug-off-string "nodebug.\n"
  627. "Predicate for disabling debug mode."
  628. :version "24.1"
  629. :group 'prolog-inferior
  630. :type 'string)
  631. (defcustom prolog-trace-on-string "trace.\n"
  632. "Predicate for enabling tracing."
  633. :version "24.1"
  634. :group 'prolog-inferior
  635. :type 'string)
  636. (defcustom prolog-trace-off-string "notrace.\n"
  637. "Predicate for disabling tracing."
  638. :version "24.1"
  639. :group 'prolog-inferior
  640. :type 'string)
  641. (defcustom prolog-zip-on-string "zip.\n"
  642. "Predicate for enabling zip mode for SICStus."
  643. :version "24.1"
  644. :group 'prolog-inferior
  645. :type 'string)
  646. (defcustom prolog-zip-off-string "nozip.\n"
  647. "Predicate for disabling zip mode for SICStus."
  648. :version "24.1"
  649. :group 'prolog-inferior
  650. :type 'string)
  651. (defcustom prolog-use-standard-consult-compile-method-flag t
  652. "Non-nil means use the standard compilation method.
  653. Otherwise the new compilation method will be used. This
  654. utilizes a special compilation buffer with the associated
  655. features such as parsing of error messages and automatically
  656. jumping to the source code responsible for the error.
  657. Warning: the new method is so far only experimental and
  658. does contain bugs. The recommended setting for the novice user
  659. is non-nil for this variable."
  660. :version "24.1"
  661. :group 'prolog-inferior
  662. :type 'boolean)
  663. ;; Miscellaneous
  664. (defcustom prolog-use-prolog-tokenizer-flag
  665. (not (fboundp 'syntax-propertize-rules))
  666. "Non-nil means use the internal prolog tokenizer for indentation etc.
  667. Otherwise use `parse-partial-sexp' which is faster but sometimes incorrect."
  668. :version "24.1"
  669. :group 'prolog-other
  670. :type 'boolean)
  671. (defcustom prolog-imenu-flag t
  672. "Non-nil means add a clause index menu for all prolog files."
  673. :version "24.1"
  674. :group 'prolog-other
  675. :type 'boolean)
  676. (defcustom prolog-imenu-max-lines 3000
  677. "The maximum number of lines of the file for imenu to be enabled.
  678. Relevant only when `prolog-imenu-flag' is non-nil."
  679. :version "24.1"
  680. :group 'prolog-other
  681. :type 'integer)
  682. (defcustom prolog-info-predicate-index
  683. "(sicstus)Predicate Index"
  684. "The info node for the SICStus predicate index."
  685. :version "24.1"
  686. :group 'prolog-other
  687. :type 'string)
  688. (defcustom prolog-underscore-wordchar-flag nil
  689. "Non-nil means underscore (_) is a word-constituent character."
  690. :version "24.1"
  691. :group 'prolog-other
  692. :type 'boolean)
  693. (defcustom prolog-use-sicstus-sd nil
  694. "If non-nil, use the source level debugger of SICStus 3#7 and later."
  695. :version "24.1"
  696. :group 'prolog-other
  697. :type 'boolean)
  698. (defcustom prolog-char-quote-workaround nil
  699. "If non-nil, declare 0 as a quote character to handle 0'<char>.
  700. This is really kludgy, and unneeded (i.e. obsolete) in Emacs>=24."
  701. :version "24.1"
  702. :group 'prolog-other
  703. :type 'boolean)
  704. ;;-------------------------------------------------------------------
  705. ;; Internal variables
  706. ;;-------------------------------------------------------------------
  707. ;;(defvar prolog-temp-filename "") ; Later set by `prolog-temporary-file'
  708. (defvar prolog-mode-syntax-table
  709. ;; The syntax accepted varies depending on the implementation used.
  710. ;; Here are some of the differences:
  711. ;; - SWI-Prolog accepts nested /*..*/ comments.
  712. ;; - Edinburgh-style Prologs take <radix>'<number> for non-decimal number,
  713. ;; whereas ISO-style Prologs use 0[obx]<number> instead.
  714. ;; - In atoms \x<hex> sometimes needs a terminating \ (ISO-style)
  715. ;; and sometimes not.
  716. (let ((table (make-syntax-table)))
  717. (if prolog-underscore-wordchar-flag
  718. (modify-syntax-entry ?_ "w" table)
  719. (modify-syntax-entry ?_ "_" table))
  720. (modify-syntax-entry ?+ "." table)
  721. (modify-syntax-entry ?- "." table)
  722. (modify-syntax-entry ?= "." table)
  723. (modify-syntax-entry ?< "." table)
  724. (modify-syntax-entry ?> "." table)
  725. (modify-syntax-entry ?| "." table)
  726. (modify-syntax-entry ?\' "\"" table)
  727. ;; Any better way to handle the 0'<char> construct?!?
  728. (when prolog-char-quote-workaround
  729. (modify-syntax-entry ?0 "\\" table))
  730. (modify-syntax-entry ?% "<" table)
  731. (modify-syntax-entry ?\n ">" table)
  732. (if (featurep 'xemacs)
  733. (progn
  734. (modify-syntax-entry ?* ". 67" table)
  735. (modify-syntax-entry ?/ ". 58" table)
  736. )
  737. ;; Emacs wants to see this it seems:
  738. (modify-syntax-entry ?* ". 23b" table)
  739. (modify-syntax-entry ?/ ". 14" table)
  740. )
  741. table))
  742. (defvar prolog-mode-abbrev-table nil)
  743. (defvar prolog-upper-case-string ""
  744. "A string containing all upper case characters.
  745. Set by prolog-build-case-strings.")
  746. (defvar prolog-lower-case-string ""
  747. "A string containing all lower case characters.
  748. Set by prolog-build-case-strings.")
  749. (defvar prolog-atom-char-regexp ""
  750. "Set by prolog-set-atom-regexps.")
  751. ;; "Regexp specifying characters which constitute atoms without quoting.")
  752. (defvar prolog-atom-regexp ""
  753. "Set by prolog-set-atom-regexps.")
  754. (defconst prolog-left-paren "[[({]" ;FIXME: Why not \\s(?
  755. "The characters used as left parentheses for the indentation code.")
  756. (defconst prolog-right-paren "[])}]" ;FIXME: Why not \\s)?
  757. "The characters used as right parentheses for the indentation code.")
  758. (defconst prolog-quoted-atom-regexp
  759. "\\(^\\|[^0-9]\\)\\('\\([^\n']\\|\\\\'\\)*'\\)"
  760. "Regexp matching a quoted atom.")
  761. (defconst prolog-string-regexp
  762. "\\(\"\\([^\n\"]\\|\\\\\"\\)*\"\\)"
  763. "Regexp matching a string.")
  764. (defconst prolog-head-delimiter "\\(:-\\|\\+:\\|-:\\|\\+\\?\\|-\\?\\|-->\\)"
  765. "A regexp for matching on the end delimiter of a head (e.g. \":-\").")
  766. (defvar prolog-compilation-buffer "*prolog-compilation*"
  767. "Name of the output buffer for Prolog compilation/consulting.")
  768. (defvar prolog-temporary-file-name nil)
  769. (defvar prolog-keywords-i nil)
  770. (defvar prolog-types-i nil)
  771. (defvar prolog-mode-specificators-i nil)
  772. (defvar prolog-determinism-specificators-i nil)
  773. (defvar prolog-directives-i nil)
  774. (defvar prolog-eof-string-i nil)
  775. ;; (defvar prolog-continued-prompt-regexp-i nil)
  776. (defvar prolog-help-function-i nil)
  777. (defvar prolog-align-rules
  778. (eval-when-compile
  779. (mapcar
  780. (lambda (x)
  781. (let ((name (car x))
  782. (sym (cdr x)))
  783. `(,(intern (format "prolog-%s" name))
  784. (regexp . ,(format "\\(\\s-*\\)%s\\(\\s-*\\)" sym))
  785. (tab-stop . nil)
  786. (modes . '(prolog-mode))
  787. (group . (1 2)))))
  788. '(("dcg" . "-->") ("rule" . ":-") ("simplification" . "<=>")
  789. ("propagation" . "==>")))))
  790. ;;-------------------------------------------------------------------
  791. ;; Prolog mode
  792. ;;-------------------------------------------------------------------
  793. ;; Example: (prolog-atleast-version '(3 . 6))
  794. (defun prolog-atleast-version (version)
  795. "Return t if the version of the current prolog system is VERSION or later.
  796. VERSION is of the format (Major . Minor)"
  797. ;; Version.major < major or
  798. ;; Version.major = major and Version.minor <= minor
  799. (let* ((thisversion (prolog-find-value-by-system prolog-system-version))
  800. (thismajor (car thisversion))
  801. (thisminor (cdr thisversion)))
  802. (or (< (car version) thismajor)
  803. (and (= (car version) thismajor)
  804. (<= (cdr version) thisminor)))
  805. ))
  806. (define-abbrev-table 'prolog-mode-abbrev-table ())
  807. (defun prolog-find-value-by-system (alist)
  808. "Get value from ALIST according to `prolog-system'."
  809. (let ((system (or prolog-system
  810. (let ((infbuf (prolog-inferior-buffer 'dont-run)))
  811. (when infbuf
  812. (buffer-local-value 'prolog-system infbuf))))))
  813. (if (listp alist)
  814. (let (result
  815. id)
  816. (while alist
  817. (setq id (car (car alist)))
  818. (if (or (eq id system)
  819. (eq id t)
  820. (and (listp id)
  821. (eval id)))
  822. (progn
  823. (setq result (car (cdr (car alist))))
  824. (if (and (listp result)
  825. (eq (car result) 'eval))
  826. (setq result (eval (car (cdr result)))))
  827. (setq alist nil))
  828. (setq alist (cdr alist))))
  829. result)
  830. alist)))
  831. (defconst prolog-syntax-propertize-function
  832. (when (fboundp 'syntax-propertize-rules)
  833. (syntax-propertize-rules
  834. ;; GNU Prolog only accepts 0'\' rather than 0'', but the only
  835. ;; possible meaning of 0'' is rather clear.
  836. ("\\<0\\(''?\\)"
  837. (1 (unless (save-excursion (nth 8 (syntax-ppss (match-beginning 0))))
  838. (string-to-syntax "_"))))
  839. ;; We could check that we're not inside an atom, but I don't think
  840. ;; that 'foo 8'z could be a valid syntax anyway, so why bother?
  841. ("\\<[1-9][0-9]*\\('\\)[0-9a-zA-Z]" (1 "_"))
  842. ;; Supposedly, ISO-Prolog wants \NNN\ for octal and \xNNN\ for hexadecimal
  843. ;; escape sequences in atoms, so be careful not to let the terminating \
  844. ;; escape a subsequent quote.
  845. ("\\\\[x0-7][0-9a-fA-F]*\\(\\\\\\)" (1 "_"))
  846. )))
  847. (defun prolog-mode-variables ()
  848. "Set some common variables to Prolog code specific values."
  849. (setq local-abbrev-table prolog-mode-abbrev-table)
  850. (set (make-local-variable 'paragraph-start)
  851. (concat "[ \t]*$\\|" page-delimiter)) ;'%%..'
  852. (set (make-local-variable 'paragraph-separate) paragraph-start)
  853. (set (make-local-variable 'paragraph-ignore-fill-prefix) t)
  854. (set (make-local-variable 'normal-auto-fill-function) 'prolog-do-auto-fill)
  855. (set (make-local-variable 'indent-line-function) 'prolog-indent-line)
  856. (set (make-local-variable 'comment-start) "%")
  857. (set (make-local-variable 'comment-end) "")
  858. (set (make-local-variable 'comment-add) 1)
  859. (set (make-local-variable 'comment-start-skip)
  860. ;; This complex regexp makes sure that comments cannot start
  861. ;; inside quoted atoms or strings
  862. (format "^\\(\\(%s\\|%s\\|[^\n\'\"%%]\\)*\\)\\(/\\*+ *\\|%%+ *\\)"
  863. prolog-quoted-atom-regexp prolog-string-regexp))
  864. (set (make-local-variable 'comment-indent-function) 'prolog-comment-indent)
  865. (set (make-local-variable 'parens-require-spaces) nil)
  866. ;; Initialize Prolog system specific variables
  867. (dolist (var '(prolog-keywords prolog-types prolog-mode-specificators
  868. prolog-determinism-specificators prolog-directives
  869. prolog-eof-string
  870. ;; prolog-continued-prompt-regexp
  871. prolog-help-function))
  872. (set (intern (concat (symbol-name var) "-i"))
  873. (prolog-find-value-by-system (symbol-value var))))
  874. (when (null (prolog-program-name))
  875. (set (make-local-variable 'compile-command) (prolog-compile-string)))
  876. (set (make-local-variable 'font-lock-defaults)
  877. '(prolog-font-lock-keywords nil nil ((?_ . "w"))))
  878. (set (make-local-variable 'syntax-propertize-function)
  879. prolog-syntax-propertize-function)
  880. )
  881. (defun prolog-mode-keybindings-common (map)
  882. "Define keybindings common to both Prolog modes in MAP."
  883. (define-key map "\C-c?" 'prolog-help-on-predicate)
  884. (define-key map "\C-c/" 'prolog-help-apropos)
  885. (define-key map "\C-c\C-d" 'prolog-debug-on)
  886. (define-key map "\C-c\C-t" 'prolog-trace-on)
  887. (define-key map "\C-c\C-z" 'prolog-zip-on)
  888. (define-key map "\C-c\r" 'run-prolog))
  889. (defun prolog-mode-keybindings-edit (map)
  890. "Define keybindings for Prolog mode in MAP."
  891. (define-key map "\M-a" 'prolog-beginning-of-clause)
  892. (define-key map "\M-e" 'prolog-end-of-clause)
  893. (define-key map "\M-q" 'prolog-fill-paragraph)
  894. (define-key map "\C-c\C-a" 'align)
  895. (define-key map "\C-\M-a" 'prolog-beginning-of-predicate)
  896. (define-key map "\C-\M-e" 'prolog-end-of-predicate)
  897. (define-key map "\M-\C-c" 'prolog-mark-clause)
  898. (define-key map "\M-\C-h" 'prolog-mark-predicate)
  899. (define-key map "\M-\C-n" 'prolog-forward-list)
  900. (define-key map "\M-\C-p" 'prolog-backward-list)
  901. (define-key map "\C-c\C-n" 'prolog-insert-predicate-template)
  902. (define-key map "\C-c\C-s" 'prolog-insert-predspec)
  903. (define-key map "\M-\r" 'prolog-insert-next-clause)
  904. (define-key map "\C-c\C-va" 'prolog-variables-to-anonymous)
  905. (define-key map "\C-c\C-v\C-s" 'prolog-view-predspec)
  906. (define-key map [Backspace] 'prolog-electric-delete)
  907. (define-key map "." 'prolog-electric-dot)
  908. (define-key map "_" 'prolog-electric-underscore)
  909. (define-key map "(" 'prolog-electric-if-then-else)
  910. (define-key map ";" 'prolog-electric-if-then-else)
  911. (define-key map ">" 'prolog-electric-if-then-else)
  912. (define-key map ":" 'prolog-electric-colon)
  913. (define-key map "-" 'prolog-electric-dash)
  914. (if prolog-electric-newline-flag
  915. (define-key map "\r" 'newline-and-indent))
  916. ;; If we're running SICStus, then map C-c C-c e/d to enabling
  917. ;; and disabling of the source-level debugging facilities.
  918. ;(if (and (eq prolog-system 'sicstus)
  919. ; (prolog-atleast-version '(3 . 7)))
  920. ; (progn
  921. ; (define-key map "\C-c\C-ce" 'prolog-enable-sicstus-sd)
  922. ; (define-key map "\C-c\C-cd" 'prolog-disable-sicstus-sd)
  923. ; ))
  924. (if prolog-old-sicstus-keys-flag
  925. (progn
  926. (define-key map "\C-c\C-c" 'prolog-consult-predicate)
  927. (define-key map "\C-cc" 'prolog-consult-region)
  928. (define-key map "\C-cC" 'prolog-consult-buffer)
  929. (define-key map "\C-c\C-k" 'prolog-compile-predicate)
  930. (define-key map "\C-ck" 'prolog-compile-region)
  931. (define-key map "\C-cK" 'prolog-compile-buffer))
  932. (define-key map "\C-c\C-p" 'prolog-consult-predicate)
  933. (define-key map "\C-c\C-r" 'prolog-consult-region)
  934. (define-key map "\C-c\C-b" 'prolog-consult-buffer)
  935. (define-key map "\C-c\C-f" 'prolog-consult-file)
  936. (define-key map "\C-c\C-cp" 'prolog-compile-predicate)
  937. (define-key map "\C-c\C-cr" 'prolog-compile-region)
  938. (define-key map "\C-c\C-cb" 'prolog-compile-buffer)
  939. (define-key map "\C-c\C-cf" 'prolog-compile-file))
  940. ;; Inherited from the old prolog.el.
  941. (define-key map "\e\C-x" 'prolog-consult-region)
  942. (define-key map "\C-c\C-l" 'prolog-consult-file)
  943. (define-key map "\C-c\C-z" 'switch-to-prolog))
  944. (defun prolog-mode-keybindings-inferior (_map)
  945. "Define keybindings for inferior Prolog mode in MAP."
  946. ;; No inferior mode specific keybindings now.
  947. )
  948. (defvar prolog-mode-map
  949. (let ((map (make-sparse-keymap)))
  950. (prolog-mode-keybindings-common map)
  951. (prolog-mode-keybindings-edit map)
  952. map))
  953. (defvar prolog-mode-hook nil
  954. "List of functions to call after the prolog mode has initialized.")
  955. (unless (fboundp 'prog-mode)
  956. (defalias 'prog-mode 'fundamental-mode))
  957. ;;;###autoload
  958. (define-derived-mode prolog-mode prog-mode "Prolog"
  959. "Major mode for editing Prolog code.
  960. Blank lines and `%%...' separate paragraphs. `%'s starts a comment
  961. line and comments can also be enclosed in /* ... */.
  962. If an optional argument SYSTEM is non-nil, set up mode for the given system.
  963. To find out what version of Prolog mode you are running, enter
  964. `\\[prolog-mode-version]'.
  965. Commands:
  966. \\{prolog-mode-map}
  967. Entry to this mode calls the value of `prolog-mode-hook'
  968. if that value is non-nil."
  969. (setq mode-name (concat "Prolog"
  970. (cond
  971. ((eq prolog-system 'eclipse) "[ECLiPSe]")
  972. ((eq prolog-system 'sicstus) "[SICStus]")
  973. ((eq prolog-system 'swi) "[SWI]")
  974. ((eq prolog-system 'gnu) "[GNU]")
  975. (t ""))))
  976. (prolog-mode-variables)
  977. (prolog-build-case-strings)
  978. (prolog-set-atom-regexps)
  979. (dolist (ar prolog-align-rules) (add-to-list 'align-rules-list ar))
  980. ;; imenu entry moved to the appropriate hook for consistency
  981. ;; Load SICStus debugger if suitable
  982. (if (and (eq prolog-system 'sicstus)
  983. (prolog-atleast-version '(3 . 7))
  984. prolog-use-sicstus-sd)
  985. (prolog-enable-sicstus-sd))
  986. (prolog-menu))
  987. (defvar mercury-mode-map
  988. (let ((map (make-sparse-keymap)))
  989. (set-keymap-parent map prolog-mode-map)
  990. map))
  991. ;;;###autoload
  992. (define-derived-mode mercury-mode prolog-mode "Prolog[Mercury]"
  993. "Major mode for editing Mercury programs.
  994. Actually this is just customized `prolog-mode'."
  995. (set (make-local-variable 'prolog-system) 'mercury))
  996. ;;-------------------------------------------------------------------
  997. ;; Inferior prolog mode
  998. ;;-------------------------------------------------------------------
  999. (defvar prolog-inferior-mode-map
  1000. (let ((map (make-sparse-keymap)))
  1001. (prolog-mode-keybindings-common map)
  1002. (prolog-mode-keybindings-inferior map)
  1003. (define-key map [remap self-insert-command]
  1004. 'prolog-inferior-self-insert-command)
  1005. map))
  1006. (defvar prolog-inferior-mode-hook nil
  1007. "List of functions to call after the inferior prolog mode has initialized.")
  1008. (defvar prolog-inferior-error-regexp-alist
  1009. '(;; GNU Prolog used to not follow the GNU standard format.
  1010. ;; ("^\\(.*?\\):\\([0-9]+\\) error: .*(char:\\([0-9]+\\)" 1 2 3)
  1011. ;; SWI-Prolog.
  1012. ("^\\(?:\\?- *\\)?\\(\\(?:ERROR\\|\\(W\\)arning\\): *\\(.*?\\):\\([1-9][0-9]*\\):\\(?:\\([0-9]*\\):\\)?\\)\\(?:$\\| \\)"
  1013. 3 4 5 (2 . nil) 1)
  1014. ;; GNU-Prolog now uses the GNU standard format.
  1015. gnu))
  1016. (defun prolog-inferior-self-insert-command ()
  1017. "Insert the char in the buffer or pass it directly to the process."
  1018. (interactive)
  1019. (let* ((proc (get-buffer-process (current-buffer)))
  1020. (pmark (and proc (marker-position (process-mark proc)))))
  1021. ;; FIXME: the same treatment would be needed for SWI-Prolog, but I can't
  1022. ;; seem to find any way for Emacs to figure out when to use it because
  1023. ;; SWI doesn't include a " ? " or some such recognizable marker.
  1024. (if (and (eq prolog-system 'gnu)
  1025. pmark
  1026. (null current-prefix-arg)
  1027. (eobp)
  1028. (eq (point) pmark)
  1029. (save-excursion
  1030. (goto-char (- pmark 3))
  1031. ;; FIXME: check this comes from the process's output, maybe?
  1032. (looking-at " \\? ")))
  1033. ;; This is GNU prolog waiting to know whether you want more answers
  1034. ;; or not (or abort, etc...). The answer is a single char, not
  1035. ;; a line, so pass this char directly rather than wait for RET to
  1036. ;; send a whole line.
  1037. (comint-send-string proc (string last-command-event))
  1038. (call-interactively 'self-insert-command))))
  1039. (declare-function 'compilation-shell-minor-mode "compile" (&optional arg))
  1040. (defvar compilation-error-regexp-alist)
  1041. (define-derived-mode prolog-inferior-mode comint-mode "Inferior Prolog"
  1042. "Major mode for interacting with an inferior Prolog process.
  1043. The following commands are available:
  1044. \\{prolog-inferior-mode-map}
  1045. Entry to this mode calls the value of `prolog-mode-hook' with no arguments,
  1046. if that value is non-nil. Likewise with the value of `comint-mode-hook'.
  1047. `prolog-mode-hook' is called after `comint-mode-hook'.
  1048. You can send text to the inferior Prolog from other buffers
  1049. using the commands `send-region', `send-string' and \\[prolog-consult-region].
  1050. Commands:
  1051. Tab indents for Prolog; with argument, shifts rest
  1052. of expression rigidly with the current line.
  1053. Paragraphs are separated only by blank lines and '%%'. '%'s start comments.
  1054. Return at end of buffer sends line as input.
  1055. Return not at end copies rest of line to end and sends it.
  1056. \\[comint-delchar-or-maybe-eof] sends end-of-file as input.
  1057. \\[comint-kill-input] and \\[backward-kill-word] are kill commands,
  1058. imitating normal Unix input editing.
  1059. \\[comint-interrupt-subjob] interrupts the shell or its current subjob if any.
  1060. \\[comint-stop-subjob] stops, likewise.
  1061. \\[comint-quit-subjob] sends quit signal, likewise.
  1062. To find out what version of Prolog mode you are running, enter
  1063. `\\[prolog-mode-version]'."
  1064. (require 'compile)
  1065. (setq comint-input-filter 'prolog-input-filter)
  1066. (setq mode-line-process '(": %s"))
  1067. (prolog-mode-variables)
  1068. (setq comint-prompt-regexp (prolog-prompt-regexp))
  1069. (set (make-local-variable 'shell-dirstack-query) "pwd.")
  1070. (set (make-local-variable 'compilation-error-regexp-alist)
  1071. prolog-inferior-error-regexp-alist)
  1072. (compilation-shell-minor-mode)
  1073. (prolog-inferior-menu))
  1074. (defun prolog-input-filter (str)
  1075. (cond ((string-match "\\`\\s *\\'" str) nil) ;whitespace
  1076. ((not (derived-mode-p 'prolog-inferior-mode)) t)
  1077. ((= (length str) 1) nil) ;one character
  1078. ((string-match "\\`[rf] *[0-9]*\\'" str) nil) ;r(edo) or f(ail)
  1079. (t t)))
  1080. ;;;###autoload
  1081. (defun run-prolog (arg)
  1082. "Run an inferior Prolog process, input and output via buffer *prolog*.
  1083. With prefix argument ARG, restart the Prolog process if running before."
  1084. (interactive "P")
  1085. ;; FIXME: It should be possible to interactively specify the command to use
  1086. ;; to run prolog.
  1087. (if (and arg (get-process "prolog"))
  1088. (progn
  1089. (process-send-string "prolog" "halt.\n")
  1090. (while (get-process "prolog") (sit-for 0.1))))
  1091. (let ((buff (buffer-name)))
  1092. (if (not (string= buff "*prolog*"))
  1093. (prolog-goto-prolog-process-buffer))
  1094. ;; Load SICStus debugger if suitable
  1095. (if (and (eq prolog-system 'sicstus)
  1096. (prolog-atleast-version '(3 . 7))
  1097. prolog-use-sicstus-sd)
  1098. (prolog-enable-sicstus-sd))
  1099. (prolog-mode-variables)
  1100. (prolog-ensure-process)
  1101. ))
  1102. (defun prolog-inferior-guess-flavor (&optional ignored)
  1103. (setq prolog-system
  1104. (when (or (numberp prolog-system) (markerp prolog-system))
  1105. (save-excursion
  1106. (goto-char (1+ prolog-system))
  1107. (cond
  1108. ((looking-at "GNU Prolog") 'gnu)
  1109. ((looking-at "Welcome to SWI-Prolog\\|%.*\\<swi_") 'swi)
  1110. ((looking-at ".*\n") nil) ;There's at least one line.
  1111. (t prolog-system)))))
  1112. (when (symbolp prolog-system)
  1113. (remove-hook 'comint-output-filter-functions
  1114. 'prolog-inferior-guess-flavor t)
  1115. (when prolog-system
  1116. (setq comint-prompt-regexp (prolog-prompt-regexp))
  1117. (if (eq prolog-system 'gnu)
  1118. (set (make-local-variable 'comint-process-echoes) t)))))
  1119. (defun prolog-ensure-process (&optional wait)
  1120. "If Prolog process is not running, run it.
  1121. If the optional argument WAIT is non-nil, wait for Prolog prompt specified by
  1122. the variable `prolog-prompt-regexp'."
  1123. (if (null (prolog-program-name))
  1124. (error "This Prolog system has defined no interpreter."))
  1125. (if (comint-check-proc "*prolog*")
  1126. ()
  1127. (with-current-buffer (get-buffer-create "*prolog*")
  1128. (prolog-inferior-mode)
  1129. (apply 'make-comint-in-buffer "prolog" (current-buffer)
  1130. (prolog-program-name) nil (prolog-program-switches))
  1131. (unless prolog-system
  1132. ;; Setup auto-detection.
  1133. (set (make-local-variable 'prolog-system)
  1134. ;; Force re-detection.
  1135. (let* ((proc (get-buffer-process (current-buffer)))
  1136. (pmark (and proc (marker-position (process-mark proc)))))
  1137. (cond
  1138. ((null pmark) (1- (point-min)))
  1139. ;; The use of insert-before-markers in comint.el together with
  1140. ;; the potential use of comint-truncate-buffer in the output
  1141. ;; filter, means that it's difficult to reliably keep track of
  1142. ;; the buffer position where the process's output started.
  1143. ;; If possible we use a marker at "start - 1", so that
  1144. ;; insert-before-marker at `start' won't shift it. And if not,
  1145. ;; we fall back on using a plain integer.
  1146. ((> pmark (point-min)) (copy-marker (1- pmark)))
  1147. (t (1- pmark)))))
  1148. (add-hook 'comint-output-filter-functions
  1149. 'prolog-inferior-guess-flavor nil t))
  1150. (if wait
  1151. (progn
  1152. (goto-char (point-max))
  1153. (while
  1154. (save-excursion
  1155. (not
  1156. (re-search-backward
  1157. (concat "\\(" (prolog-prompt-regexp) "\\)" "\\=")
  1158. nil t)))
  1159. (sit-for 0.1)))))))
  1160. (defun prolog-inferior-buffer (&optional dont-run)
  1161. (or (get-buffer "*prolog*")
  1162. (unless dont-run
  1163. (prolog-ensure-process)
  1164. (get-buffer "*prolog*"))))
  1165. (defun prolog-process-insert-string (process string)
  1166. "Insert STRING into inferior Prolog buffer running PROCESS."
  1167. ;; Copied from elisp manual, greek to me
  1168. (with-current-buffer (process-buffer process)
  1169. ;; FIXME: Use window-point-insertion-type instead.
  1170. (let ((moving (= (point) (process-mark process))))
  1171. (save-excursion
  1172. ;; Insert the text, moving the process-marker.
  1173. (goto-char (process-mark process))
  1174. (insert string)
  1175. (set-marker (process-mark process) (point)))
  1176. (if moving (goto-char (process-mark process))))))
  1177. ;;------------------------------------------------------------
  1178. ;; Old consulting and compiling functions
  1179. ;;------------------------------------------------------------
  1180. (declare-function compilation-forget-errors "compile" ())
  1181. (declare-function compilation-fake-loc "compile"
  1182. (marker file &optional line col))
  1183. (defun prolog-old-process-region (compilep start end)
  1184. "Process the region limited by START and END positions.
  1185. If COMPILEP is non-nil then use compilation, otherwise consulting."
  1186. (prolog-ensure-process)
  1187. ;(let ((tmpfile prolog-temp-filename)
  1188. (let ((tmpfile (prolog-temporary-file))
  1189. ;(process (get-process "prolog"))
  1190. (first-line (1+ (count-lines
  1191. (point-min)
  1192. (save-excursion
  1193. (goto-char start)
  1194. (point))))))
  1195. (write-region start end tmpfile)
  1196. (setq start (copy-marker start))
  1197. (with-current-buffer (prolog-inferior-buffer)
  1198. (compilation-forget-errors)
  1199. (compilation-fake-loc start tmpfile))
  1200. (process-send-string
  1201. "prolog" (prolog-build-prolog-command
  1202. compilep tmpfile (prolog-bsts buffer-file-name)
  1203. first-line))
  1204. (prolog-goto-prolog-process-buffer)))
  1205. (defun prolog-old-process-predicate (compilep)
  1206. "Process the predicate around point.
  1207. If COMPILEP is non-nil then use compilation, otherwise consulting."
  1208. (prolog-old-process-region
  1209. compilep (prolog-pred-start) (prolog-pred-end)))
  1210. (defun prolog-old-process-buffer (compilep)
  1211. "Process the entire buffer.
  1212. If COMPILEP is non-nil then use compilation, otherwise consulting."
  1213. (prolog-old-process-region compilep (point-min) (point-max)))
  1214. (defun prolog-old-process-file (compilep)
  1215. "Process the file of the current buffer.
  1216. If COMPILEP is non-nil then use compilation, otherwise consulting."
  1217. (save-some-buffers)
  1218. (prolog-ensure-process)
  1219. (with-current-buffer (prolog-inferior-buffer)
  1220. (compilation-forget-errors))
  1221. (process-send-string
  1222. "prolog" (prolog-build-prolog-command
  1223. compilep buffer-file-name
  1224. (prolog-bsts buffer-file-name)))
  1225. (prolog-goto-prolog-process-buffer))
  1226. ;;------------------------------------------------------------
  1227. ;; Consulting and compiling
  1228. ;;------------------------------------------------------------
  1229. ;; Interactive interface functions, used by both the standard
  1230. ;; and the experimental consultation and compilation functions
  1231. (defun prolog-consult-file ()
  1232. "Consult file of current buffer."
  1233. (interactive)
  1234. (if prolog-use-standard-consult-compile-method-flag
  1235. (prolog-old-process-file nil)
  1236. (prolog-consult-compile-file nil)))
  1237. (defun prolog-consult-buffer ()
  1238. "Consult buffer."
  1239. (interactive)
  1240. (if prolog-use-standard-consult-compile-method-flag
  1241. (prolog-old-process-buffer nil)
  1242. (prolog-consult-compile-buffer nil)))
  1243. (defun prolog-consult-region (beg end)
  1244. "Consult region between BEG and END."
  1245. (interactive "r")
  1246. (if prolog-use-standard-consult-compile-method-flag
  1247. (prolog-old-process-region nil beg end)
  1248. (prolog-consult-compile-region nil beg end)))
  1249. (defun prolog-consult-predicate ()
  1250. "Consult the predicate around current point."
  1251. (interactive)
  1252. (if prolog-use-standard-consult-compile-method-flag
  1253. (prolog-old-process-predicate nil)
  1254. (prolog-consult-compile-predicate nil)))
  1255. (defun prolog-compile-file ()
  1256. "Compile file of current buffer."
  1257. (interactive)
  1258. (if prolog-use-standard-consult-compile-method-flag
  1259. (prolog-old-process-file t)
  1260. (prolog-consult-compile-file t)))
  1261. (defun prolog-compile-buffer ()
  1262. "Compile buffer."
  1263. (interactive)
  1264. (if prolog-use-standard-consult-compile-method-flag
  1265. (prolog-old-process-buffer t)
  1266. (prolog-consult-compile-buffer t)))
  1267. (defun prolog-compile-region (beg end)
  1268. "Compile region between BEG and END."
  1269. (interactive "r")
  1270. (if prolog-use-standard-consult-compile-method-flag
  1271. (prolog-old-process-region t beg end)
  1272. (prolog-consult-compile-region t beg end)))
  1273. (defun prolog-compile-predicate ()
  1274. "Compile the predicate around current point."
  1275. (interactive)
  1276. (if prolog-use-standard-consult-compile-method-flag
  1277. (prolog-old-process-predicate t)
  1278. (prolog-consult-compile-predicate t)))
  1279. (defun prolog-buffer-module ()
  1280. "Select Prolog module name appropriate for current buffer.
  1281. Bases decision on buffer contents (-*- line)."
  1282. ;; Look for -*- ... module: MODULENAME; ... -*-
  1283. (let (beg end)
  1284. (save-excursion
  1285. (goto-char (point-min))
  1286. (skip-chars-forward " \t")
  1287. (and (search-forward "-*-" (line-end-position) t)
  1288. (progn
  1289. (skip-chars-forward " \t")
  1290. (setq beg (point))
  1291. (search-forward "-*-" (line-end-position) t))
  1292. (progn
  1293. (forward-char -3)
  1294. (skip-chars-backward " \t")
  1295. (setq end (point))
  1296. (goto-char beg)
  1297. (and (let ((case-fold-search t))
  1298. (search-forward "module:" end t))
  1299. (progn
  1300. (skip-chars-forward " \t")
  1301. (setq beg (point))
  1302. (if (search-forward ";" end t)
  1303. (forward-char -1)
  1304. (goto-char end))
  1305. (skip-chars-backward " \t")
  1306. (buffer-substring beg (point)))))))))
  1307. (defun prolog-build-prolog-command (compilep file buffername
  1308. &optional first-line)
  1309. "Make Prolog command for FILE compilation/consulting.
  1310. If COMPILEP is non-nil, consider compilation, otherwise consulting."
  1311. (let* ((compile-string
  1312. ;; FIXME: If the process is not running yet, the auto-detection of
  1313. ;; prolog-system won't help here, so we should make sure
  1314. ;; we first run Prolog and then build the command.
  1315. (if compilep (prolog-compile-string) (prolog-consult-string)))
  1316. (module (prolog-buffer-module))
  1317. (file-name (concat "'" (prolog-bsts file) "'"))
  1318. (module-name (if module (concat "'" module "'")))
  1319. (module-file (if module
  1320. (concat module-name ":" file-name)
  1321. file-name))
  1322. strbeg strend
  1323. (lineoffset (if first-line
  1324. (- first-line 1)
  1325. 0)))
  1326. ;; Assure that there is a buffer name
  1327. (if (not buffername)
  1328. (error "The buffer is not saved"))
  1329. (if (not (string-match "\\`'.*'\\'" buffername)) ; Add quotes
  1330. (setq buffername (concat "'" buffername "'")))
  1331. (while (string-match "%m" compile-string)
  1332. (setq strbeg (substring compile-string 0 (match-beginning 0)))
  1333. (setq strend (substring compile-string (match-end 0)))
  1334. (setq compile-string (concat strbeg module-file strend)))
  1335. ;; FIXME: The code below will %-expand any %[fbl] that appears in
  1336. ;; module-file.
  1337. (while (string-match "%f" compile-string)
  1338. (setq strbeg (substring compile-string 0 (match-beginning 0)))
  1339. (setq strend (substring compile-string (match-end 0)))
  1340. (setq compile-string (concat strbeg file-name strend)))
  1341. (while (string-match "%b" compile-string)
  1342. (setq strbeg (substring compile-string 0 (match-beginning 0)))
  1343. (setq strend (substring compile-string (match-end 0)))
  1344. (setq compile-string (concat strbeg buffername strend)))
  1345. (while (string-match "%l" compile-string)
  1346. (setq strbeg (substring compile-string 0 (match-beginning 0)))
  1347. (setq strend (substring compile-string (match-end 0)))
  1348. (setq compile-string (concat strbeg (format "%d" lineoffset) strend)))
  1349. (concat compile-string "\n")))
  1350. ;; The rest of this page is experimental code!
  1351. ;; Global variables for process filter function
  1352. (defvar prolog-process-flag nil
  1353. "Non-nil means that a prolog task (i.e. a consultation or compilation job)
  1354. is running.")
  1355. (defvar prolog-consult-compile-output ""
  1356. "Hold the unprocessed output from the current prolog task.")
  1357. (defvar prolog-consult-compile-first-line 1
  1358. "The number of the first line of the file to consult/compile.
  1359. Used for temporary files.")
  1360. (defvar prolog-consult-compile-file nil
  1361. "The file to compile/consult (can be a temporary file).")
  1362. (defvar prolog-consult-compile-real-file nil
  1363. "The file name of the buffer to compile/consult.")
  1364. (defvar compilation-parse-errors-function)
  1365. (defun prolog-consult-compile (compilep file &optional first-line)
  1366. "Consult/compile FILE.
  1367. If COMPILEP is non-nil, perform compilation, otherwise perform CONSULTING.
  1368. COMMAND is a string described by the variables `prolog-consult-string'
  1369. and `prolog-compile-string'.
  1370. Optional argument FIRST-LINE is the number of the first line in the compiled
  1371. region.
  1372. This function must be called from the source code buffer."
  1373. (if prolog-process-flag
  1374. (error "Another Prolog task is running."))
  1375. (prolog-ensure-process t)
  1376. (let* ((buffer (get-buffer-create prolog-compilation-buffer))
  1377. (real-file buffer-file-name)
  1378. (command-string (prolog-build-prolog-command compilep file
  1379. real-file first-line))
  1380. (process (get-process "prolog"))
  1381. (old-filter (process-filter process)))
  1382. (with-current-buffer buffer
  1383. (delete-region (point-min) (point-max))
  1384. ;; FIXME: Wasn't this supposed to use prolog-inferior-mode?
  1385. (compilation-mode)
  1386. ;; FIXME: This doesn't seem to cooperate well with new(ish) compile.el.
  1387. ;; Setting up font-locking for this buffer
  1388. (set (make-local-variable 'font-lock-defaults)
  1389. '(prolog-font-lock-keywords nil nil ((?_ . "w"))))
  1390. (if (eq prolog-system 'sicstus)
  1391. ;; FIXME: This looks really problematic: not only is this using
  1392. ;; the old compilation-parse-errors-function, but
  1393. ;; prolog-parse-sicstus-compilation-errors only accepts one argument
  1394. ;; whereas compile.el calls it with 2 (and did so at least since
  1395. ;; Emacs-20).
  1396. (set (make-local-variable 'compilation-parse-errors-function)
  1397. 'prolog-parse-sicstus-compilation-errors))
  1398. (setq buffer-read-only nil)
  1399. (insert command-string "\n"))
  1400. (save-selected-window
  1401. (pop-to-buffer buffer))
  1402. (setq prolog-process-flag t
  1403. prolog-consult-compile-output ""
  1404. prolog-consult-compile-first-line (if first-line (1- first-line) 0)
  1405. prolog-consult-compile-file file
  1406. prolog-consult-compile-real-file (if (string=
  1407. file buffer-file-name)
  1408. nil
  1409. real-file))
  1410. (with-current-buffer buffer
  1411. (goto-char (point-max))
  1412. (set-process-filter process 'prolog-consult-compile-filter)
  1413. (process-send-string "prolog" command-string)
  1414. ;; (prolog-build-prolog-command compilep file real-file first-line))
  1415. (while (and prolog-process-flag
  1416. (accept-process-output process 10)) ; 10 secs is ok?
  1417. (sit-for 0.1)
  1418. (unless (get-process "prolog")
  1419. (setq prolog-process-flag nil)))
  1420. (insert (if compilep
  1421. "\nCompilation finished.\n"
  1422. "\nConsulted.\n"))
  1423. (set-process-filter process old-filter))))
  1424. (defvar compilation-error-list)
  1425. (defun prolog-parse-sicstus-compilation-errors (limit)
  1426. "Parse the prolog compilation buffer for errors.
  1427. Argument LIMIT is a buffer position limiting searching.
  1428. For use with the `compilation-parse-errors-function' variable."
  1429. (setq compilation-error-list nil)
  1430. (message "Parsing SICStus error messages...")
  1431. (let (filepath dir file errorline)
  1432. (while
  1433. (re-search-backward
  1434. "{\\([a-zA-Z ]* ERROR\\|Warning\\):.* in line[s ]*\\([0-9]+\\)"
  1435. limit t)
  1436. (setq errorline (string-to-number (match-string 2)))
  1437. (save-excursion
  1438. (re-search-backward
  1439. "{\\(consulting\\|compiling\\|processing\\) \\(.*\\)\\.\\.\\.}"
  1440. limit t)
  1441. (setq filepath (match-string 2)))
  1442. ;; ###### Does this work with SICStus under Windows (i.e. backslashes and stuff?)
  1443. (if (string-match "\\(.*/\\)\\([^/]*\\)$" filepath)
  1444. (progn
  1445. (setq dir (match-string 1 filepath))
  1446. (setq file (match-string 2 filepath))))
  1447. (setq compilation-error-list
  1448. (cons
  1449. (cons (save-excursion
  1450. (beginning-of-line)
  1451. (point-marker))
  1452. (list (list file dir) errorline))
  1453. compilation-error-list)
  1454. ))
  1455. ))
  1456. (defun prolog-consult-compile-filter (process output)
  1457. "Filter function for Prolog compilation PROCESS.
  1458. Argument OUTPUT is a name of the output file."
  1459. ;;(message "start")
  1460. (setq prolog-consult-compile-output
  1461. (concat prolog-consult-compile-output output))
  1462. ;;(message "pccf1: %s" prolog-consult-compile-output)
  1463. ;; Iterate through the lines of prolog-consult-compile-output
  1464. (let (outputtype)
  1465. (while (and prolog-process-flag
  1466. (or
  1467. ;; Trace question
  1468. (progn
  1469. (setq outputtype 'trace)
  1470. (and (eq prolog-system 'sicstus)
  1471. (string-match
  1472. "^[ \t]*[0-9]+[ \t]*[0-9]+[ \t]*Call:.*? "
  1473. prolog-consult-compile-output)))
  1474. ;; Match anything
  1475. (progn
  1476. (setq outputtype 'normal)
  1477. (string-match "^.*\n" prolog-consult-compile-output))
  1478. ))
  1479. ;;(message "outputtype: %s" outputtype)
  1480. (setq output (match-string 0 prolog-consult-compile-output))
  1481. ;; remove the text in output from prolog-consult-compile-output
  1482. (setq prolog-consult-compile-output
  1483. (substring prolog-consult-compile-output (length output)))
  1484. ;;(message "pccf2: %s" prolog-consult-compile-output)
  1485. ;; If temporary files were used, then we change the error
  1486. ;; messages to point to the original source file.
  1487. ;; FIXME: Use compilation-fake-loc instead.
  1488. (cond
  1489. ;; If the prolog process was in trace mode then it requires
  1490. ;; user input
  1491. ((and (eq prolog-system 'sicstus)
  1492. (eq outputtype 'trace))
  1493. (let ((input (concat (read-string output) "\n")))
  1494. (process-send-string process input)
  1495. (setq output (concat output input))))
  1496. ((eq prolog-system 'sicstus)
  1497. (if (and prolog-consult-compile-real-file
  1498. (string-match
  1499. "\\({.*:.* in line[s ]*\\)\\([0-9]+\\)-\\([0-9]+\\)" output))
  1500. (setq output (replace-match
  1501. ;; Adds a {processing ...} line so that
  1502. ;; `prolog-parse-sicstus-compilation-errors'
  1503. ;; finds the real file instead of the temporary one.
  1504. ;; Also fixes the line numbers.
  1505. (format "Added by Emacs: {processing %s...}\n%s%d-%d"
  1506. prolog-consult-compile-real-file
  1507. (match-string 1 output)
  1508. (+ prolog-consult-compile-first-line
  1509. (string-to-number
  1510. (match-string 2 output)))
  1511. (+ prolog-consult-compile-first-line
  1512. (string-to-number
  1513. (match-string 3 output))))
  1514. t t output)))
  1515. )
  1516. ((eq prolog-system 'swi)
  1517. (if (and prolog-consult-compile-real-file
  1518. (string-match (format
  1519. "%s\\([ \t]*:[ \t]*\\)\\([0-9]+\\)"
  1520. prolog-consult-compile-file)
  1521. output))
  1522. (setq output (replace-match
  1523. ;; Real filename + text + fixed linenum
  1524. (format "%s%s%d"
  1525. prolog-consult-compile-real-file
  1526. (match-string 1 output)
  1527. (+ prolog-consult-compile-first-line
  1528. (string-to-number
  1529. (match-string 2 output))))
  1530. t t output)))
  1531. )
  1532. (t ())
  1533. )
  1534. ;; Write the output in the *prolog-compilation* buffer
  1535. (insert output)))
  1536. ;; If the prompt is visible, then the task is finished
  1537. (if (string-match (prolog-prompt-regexp) prolog-consult-compile-output)
  1538. (setq prolog-process-flag nil)))
  1539. (defun prolog-consult-compile-file (compilep)
  1540. "Consult/compile file of current buffer.
  1541. If COMPILEP is non-nil, compile, otherwise consult."
  1542. (let ((file buffer-file-name))
  1543. (if file
  1544. (progn
  1545. (save-some-buffers)
  1546. (prolog-consult-compile compilep file))
  1547. (prolog-consult-compile-region compilep (point-min) (point-max)))))
  1548. (defun prolog-consult-compile-buffer (compilep)
  1549. "Consult/compile current buffer.
  1550. If COMPILEP is non-nil, compile, otherwise consult."
  1551. (prolog-consult-compile-region compilep (point-min) (point-max)))
  1552. (defun prolog-consult-compile-region (compilep beg end)
  1553. "Consult/compile region between BEG and END.
  1554. If COMPILEP is non-nil, compile, otherwise consult."
  1555. ;(let ((file prolog-temp-filename)
  1556. (let ((file (prolog-bsts (prolog-temporary-file)))
  1557. (lines (count-lines 1 beg)))
  1558. (write-region beg end file nil 'no-message)
  1559. (write-region "\n" nil file t 'no-message)
  1560. (prolog-consult-compile compilep file
  1561. (if (bolp) (1+ lines) lines))
  1562. (delete-file file)))
  1563. (defun prolog-consult-compile-predicate (compilep)
  1564. "Consult/compile the predicate around current point.
  1565. If COMPILEP is non-nil, compile, otherwise consult."
  1566. (prolog-consult-compile-region
  1567. compilep (prolog-pred-start) (prolog-pred-end)))
  1568. ;;-------------------------------------------------------------------
  1569. ;; Font-lock stuff
  1570. ;;-------------------------------------------------------------------
  1571. ;; Auxiliary functions
  1572. (defun prolog-make-keywords-regexp (keywords &optional protect)
  1573. "Create regexp from the list of strings KEYWORDS.
  1574. If PROTECT is non-nil, surround the result regexp by word breaks."
  1575. (let ((regexp
  1576. (if (fboundp 'regexp-opt)
  1577. ;; Emacs 20
  1578. ;; Avoid compile warnings under earlier versions by using eval
  1579. (eval '(regexp-opt keywords))
  1580. ;; Older Emacsen
  1581. (concat (mapconcat 'regexp-quote keywords "\\|")))
  1582. ))
  1583. (if protect
  1584. (concat "\\<\\(" regexp "\\)\\>")
  1585. regexp)))
  1586. (defun prolog-font-lock-object-matcher (bound)
  1587. "Find SICStus objects method name for font lock.
  1588. Argument BOUND is a buffer position limiting searching."
  1589. (let (point
  1590. (case-fold-search nil))
  1591. (while (and (not point)
  1592. (re-search-forward "\\(::[ \t\n]*{\\|&\\)[ \t]*"
  1593. bound t))
  1594. (while (or (re-search-forward "\\=\n[ \t]*" bound t)
  1595. (re-search-forward "\\=%.*" bound t)
  1596. (and (re-search-forward "\\=/\\*" bound t)
  1597. (re-search-forward "\\*/[ \t]*" bound t))))
  1598. (setq point (re-search-forward
  1599. (format "\\=\\(%s\\)" prolog-atom-regexp)
  1600. bound t)))
  1601. point))
  1602. (defsubst prolog-face-name-p (facename)
  1603. ;; Return t if FACENAME is the name of a face. This method is
  1604. ;; necessary since facep in XEmacs only returns t for the actual
  1605. ;; face objects (while it's only their names that are used just
  1606. ;; about anywhere else) without providing a predicate that tests
  1607. ;; face names. This function (including the above commentary) is
  1608. ;; borrowed from cc-mode.
  1609. (memq facename (face-list)))
  1610. ;; Set everything up
  1611. (defun prolog-font-lock-keywords ()
  1612. "Set up font lock keywords for the current Prolog system."
  1613. ;(when window-system
  1614. (require 'font-lock)
  1615. ;; Define Prolog faces
  1616. (defface prolog-redo-face
  1617. '((((class grayscale)) (:italic t))
  1618. (((class color)) (:foreground "darkorchid"))
  1619. (t (:italic t)))
  1620. "Prolog mode face for highlighting redo trace lines."
  1621. :group 'prolog-faces)
  1622. (defface prolog-exit-face
  1623. '((((class grayscale)) (:underline t))
  1624. (((class color) (background dark)) (:foreground "green"))
  1625. (((class color) (background light)) (:foreground "ForestGreen"))
  1626. (t (:underline t)))
  1627. "Prolog mode face for highlighting exit trace lines."
  1628. :group 'prolog-faces)
  1629. (defface prolog-exception-face
  1630. '((((class grayscale)) (:bold t :italic t :underline t))
  1631. (((class color)) (:bold t :foreground "black" :background "Khaki"))
  1632. (t (:bold t :italic t :underline t)))
  1633. "Prolog mode face for highlighting exception trace lines."
  1634. :group 'prolog-faces)
  1635. (defface prolog-warning-face
  1636. '((((class grayscale)) (:underline t))
  1637. (((class color) (background dark)) (:foreground "blue"))
  1638. (((class color) (background light)) (:foreground "MidnightBlue"))
  1639. (t (:underline t)))
  1640. "Face name to use for compiler warnings."
  1641. :group 'prolog-faces)
  1642. (defface prolog-builtin-face
  1643. '((((class color) (background light)) (:foreground "Purple"))
  1644. (((class color) (background dark)) (:foreground "Cyan"))
  1645. (((class grayscale) (background light)) (:foreground "LightGray" :bold t))
  1646. (((class grayscale) (background dark)) (:foreground "DimGray" :bold t))
  1647. (t (:bold t)))
  1648. "Face name to use for compiler warnings."
  1649. :group 'prolog-faces)
  1650. (defvar prolog-warning-face
  1651. (if (prolog-face-name-p 'font-lock-warning-face)
  1652. 'font-lock-warning-face
  1653. 'prolog-warning-face)
  1654. "Face name to use for built in predicates.")
  1655. (defvar prolog-builtin-face
  1656. (if (prolog-face-name-p 'font-lock-builtin-face)
  1657. 'font-lock-builtin-face
  1658. 'prolog-builtin-face)
  1659. "Face name to use for built in predicates.")
  1660. (defvar prolog-redo-face 'prolog-redo-face
  1661. "Face name to use for redo trace lines.")
  1662. (defvar prolog-exit-face 'prolog-exit-face
  1663. "Face name to use for exit trace lines.")
  1664. (defvar prolog-exception-face 'prolog-exception-face
  1665. "Face name to use for exception trace lines.")
  1666. ;; Font Lock Patterns
  1667. (let (
  1668. ;; "Native" Prolog patterns
  1669. (head-predicates
  1670. (list (format "^\\(%s\\)\\((\\|[ \t]*:-\\)" prolog-atom-regexp)
  1671. 1 font-lock-function-name-face))
  1672. ;(list (format "^%s" prolog-atom-regexp)
  1673. ; 0 font-lock-function-name-face))
  1674. (head-predicates-1
  1675. (list (format "\\.[ \t]*\\(%s\\)" prolog-atom-regexp)
  1676. 1 font-lock-function-name-face) )
  1677. (variables
  1678. '("\\<\\([_A-Z][a-zA-Z0-9_]*\\)"
  1679. 1 font-lock-variable-name-face))
  1680. (important-elements
  1681. (list (if (eq prolog-system 'mercury)
  1682. "[][}{;|]\\|\\\\[+=]\\|<?=>?"
  1683. "[][}{!;|]\\|\\*->")
  1684. 0 'font-lock-keyword-face))
  1685. (important-elements-1
  1686. '("[^-*]\\(->\\)" 1 font-lock-keyword-face))
  1687. (predspecs ; module:predicate/cardinality
  1688. (list (format "\\<\\(%s:\\|\\)%s/[0-9]+"
  1689. prolog-atom-regexp prolog-atom-regexp)
  1690. 0 font-lock-function-name-face 'prepend))
  1691. (keywords ; directives (queries)
  1692. (list
  1693. (if (eq prolog-system 'mercury)
  1694. (concat
  1695. "\\<\\("
  1696. (prolog-make-keywords-regexp prolog-keywords-i)
  1697. "\\|"
  1698. (prolog-make-keywords-regexp
  1699. prolog-determinism-specificators-i)
  1700. "\\)\\>")
  1701. (concat
  1702. "^[?:]- *\\("
  1703. (prolog-make-keywords-regexp prolog-keywords-i)
  1704. "\\)\\>"))
  1705. 1 prolog-builtin-face))
  1706. (quoted_atom (list prolog-quoted-atom-regexp
  1707. 2 'font-lock-string-face 'append))
  1708. (string (list prolog-string-regexp
  1709. 1 'font-lock-string-face 'append))
  1710. ;; SICStus specific patterns
  1711. (sicstus-object-methods
  1712. (if (eq prolog-system 'sicstus)
  1713. '(prolog-font-lock-object-matcher
  1714. 1 font-lock-function-name-face)))
  1715. ;; Mercury specific patterns
  1716. (types
  1717. (if (eq prolog-system 'mercury)
  1718. (list
  1719. (prolog-make-keywords-regexp prolog-types-i t)
  1720. 0 'font-lock-type-face)))
  1721. (modes
  1722. (if (eq prolog-system 'mercury)
  1723. (list
  1724. (prolog-make-keywords-regexp prolog-mode-specificators-i t)
  1725. 0 'font-lock-reference-face)))
  1726. (directives
  1727. (if (eq prolog-system 'mercury)
  1728. (list
  1729. (prolog-make-keywords-regexp prolog-directives-i t)
  1730. 0 'prolog-warning-face)))
  1731. ;; Inferior mode specific patterns
  1732. (prompt
  1733. ;; FIXME: Should be handled by comint already.
  1734. (list (prolog-prompt-regexp) 0 'font-lock-keyword-face))
  1735. (trace-exit
  1736. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1737. (cond
  1738. ((eq prolog-system 'sicstus)
  1739. '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Exit\\):"
  1740. 1 prolog-exit-face))
  1741. ((eq prolog-system 'swi)
  1742. '("[ \t]*\\(Exit\\):[ \t]*([ \t0-9]*)" 1 prolog-exit-face))
  1743. (t nil)))
  1744. (trace-fail
  1745. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1746. (cond
  1747. ((eq prolog-system 'sicstus)
  1748. '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Fail\\):"
  1749. 1 prolog-warning-face))
  1750. ((eq prolog-system 'swi)
  1751. '("[ \t]*\\(Fail\\):[ \t]*([ \t0-9]*)" 1 prolog-warning-face))
  1752. (t nil)))
  1753. (trace-redo
  1754. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1755. (cond
  1756. ((eq prolog-system 'sicstus)
  1757. '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Redo\\):"
  1758. 1 prolog-redo-face))
  1759. ((eq prolog-system 'swi)
  1760. '("[ \t]*\\(Redo\\):[ \t]*([ \t0-9]*)" 1 prolog-redo-face))
  1761. (t nil)))
  1762. (trace-call
  1763. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1764. (cond
  1765. ((eq prolog-system 'sicstus)
  1766. '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Call\\):"
  1767. 1 font-lock-function-name-face))
  1768. ((eq prolog-system 'swi)
  1769. '("[ \t]*\\(Call\\):[ \t]*([ \t0-9]*)"
  1770. 1 font-lock-function-name-face))
  1771. (t nil)))
  1772. (trace-exception
  1773. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1774. (cond
  1775. ((eq prolog-system 'sicstus)
  1776. '("[ \t]*[0-9]+[ \t]+[0-9]+[ \t]*\\(Exception\\):"
  1777. 1 prolog-exception-face))
  1778. ((eq prolog-system 'swi)
  1779. '("[ \t]*\\(Exception\\):[ \t]*([ \t0-9]*)"
  1780. 1 prolog-exception-face))
  1781. (t nil)))
  1782. (error-message-identifier
  1783. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1784. (cond
  1785. ((eq prolog-system 'sicstus)
  1786. '("{\\([A-Z]* ?ERROR:\\)" 1 prolog-exception-face prepend))
  1787. ((eq prolog-system 'swi)
  1788. '("^[[]\\(WARNING:\\)" 1 prolog-builtin-face prepend))
  1789. (t nil)))
  1790. (error-whole-messages
  1791. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1792. (cond
  1793. ((eq prolog-system 'sicstus)
  1794. '("{\\([A-Z]* ?ERROR:.*\\)}[ \t]*$"
  1795. 1 font-lock-comment-face append))
  1796. ((eq prolog-system 'swi)
  1797. '("^[[]WARNING:[^]]*[]]$" 0 font-lock-comment-face append))
  1798. (t nil)))
  1799. (error-warning-messages
  1800. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1801. ;; Mostly errors that SICStus asks the user about how to solve,
  1802. ;; such as "NAME CLASH:" for example.
  1803. (cond
  1804. ((eq prolog-system 'sicstus)
  1805. '("^[A-Z ]*[A-Z]+:" 0 prolog-warning-face))
  1806. (t nil)))
  1807. (warning-messages
  1808. ;; FIXME: Add to compilation-error-regexp-alist instead.
  1809. (cond
  1810. ((eq prolog-system 'sicstus)
  1811. '("\\({ ?\\(Warning\\|WARNING\\) ?:.*}\\)[ \t]*$"
  1812. 2 prolog-warning-face prepend))
  1813. (t nil))))
  1814. ;; Make font lock list
  1815. (delq
  1816. nil
  1817. (cond
  1818. ((eq major-mode 'prolog-mode)
  1819. (list
  1820. head-predicates
  1821. head-predicates-1
  1822. quoted_atom
  1823. string
  1824. variables
  1825. important-elements
  1826. important-elements-1
  1827. predspecs
  1828. keywords
  1829. sicstus-object-methods
  1830. types
  1831. modes
  1832. directives))
  1833. ((eq major-mode 'prolog-inferior-mode)
  1834. (list
  1835. prompt
  1836. error-message-identifier
  1837. error-whole-messages
  1838. error-warning-messages
  1839. warning-messages
  1840. predspecs
  1841. trace-exit
  1842. trace-fail
  1843. trace-redo
  1844. trace-call
  1845. trace-exception))
  1846. ((eq major-mode 'compilation-mode)
  1847. (list
  1848. error-message-identifier
  1849. error-whole-messages
  1850. error-warning-messages
  1851. warning-messages
  1852. predspecs))))
  1853. ))
  1854. ;;-------------------------------------------------------------------
  1855. ;; Indentation stuff
  1856. ;;-------------------------------------------------------------------
  1857. ;; NB: This function *MUST* have this optional argument since XEmacs
  1858. ;; assumes it. This does not mean we have to use it...
  1859. (defun prolog-indent-line (&optional _whole-exp)
  1860. "Indent current line as Prolog code.
  1861. With argument, indent any additional lines of the same clause
  1862. rigidly along with this one (not yet)."
  1863. (interactive "p")
  1864. (let ((indent (prolog-indent-level))
  1865. (pos (- (point-max) (point))))
  1866. (beginning-of-line)
  1867. (skip-chars-forward " \t")
  1868. (indent-line-to indent)
  1869. (if (> (- (point-max) pos) (point))
  1870. (goto-char (- (point-max) pos)))
  1871. ;; Align comments
  1872. (if (and prolog-align-comments-flag
  1873. (save-excursion
  1874. (line-beginning-position)
  1875. ;; (let ((start (comment-search-forward (line-end-position) t)))
  1876. ;; (and start ;There's a comment to indent.
  1877. ;; ;; If it's first on the line, we've indented it already
  1878. ;; ;; and prolog-goto-comment-column would inf-loop.
  1879. ;; (progn (goto-char start) (skip-chars-backward " \t")
  1880. ;; (not (bolp)))))))
  1881. (and (looking-at comment-start-skip)
  1882. ;; The definition of comment-start-skip used in this
  1883. ;; mode is unusual in that it only matches at BOL.
  1884. (progn (skip-chars-forward " \t")
  1885. (not (eq (point) (match-end 1)))))))
  1886. (save-excursion
  1887. (prolog-goto-comment-column t)))
  1888. ;; Insert spaces if needed
  1889. (if (or prolog-electric-tab-flag prolog-electric-if-then-else-flag)
  1890. (prolog-insert-spaces-after-paren))
  1891. ))
  1892. (defun prolog-comment-indent ()
  1893. "Compute prolog comment indentation."
  1894. ;; FIXME: Only difference with default behavior is that %%% is not
  1895. ;; flushed to column 0 but just left where the user put it.
  1896. (cond ((looking-at "%%%") (prolog-indentation-level-of-line))
  1897. ((looking-at "%%") (prolog-indent-level))
  1898. (t
  1899. (save-excursion
  1900. (skip-chars-backward " \t")
  1901. ;; Insert one space at least, except at left margin.
  1902. (max (+ (current-column) (if (bolp) 0 1))
  1903. comment-column)))
  1904. ))
  1905. (defun prolog-indent-level ()
  1906. "Compute prolog indentation level."
  1907. (save-excursion
  1908. (beginning-of-line)
  1909. (let ((totbal (prolog-region-paren-balance
  1910. (prolog-clause-start t) (point)))
  1911. (oldpoint (point)))
  1912. (skip-chars-forward " \t")
  1913. (cond
  1914. ((looking-at "%%%") (prolog-indentation-level-of-line))
  1915. ;Large comment starts
  1916. ((looking-at "%[^%]") comment-column) ;Small comment starts
  1917. ((bobp) 0) ;Beginning of buffer
  1918. ;; If we found '}' then we must check if it's the
  1919. ;; end of an object declaration or something else.
  1920. ((and (looking-at "}")
  1921. (save-excursion
  1922. (forward-char 1)
  1923. ;; Goto to matching {
  1924. (if prolog-use-prolog-tokenizer-flag
  1925. (prolog-backward-list)
  1926. (backward-list))
  1927. (skip-chars-backward " \t")
  1928. (backward-char 2)
  1929. (looking-at "::")))
  1930. ;; It was an object
  1931. (if prolog-object-end-to-0-flag
  1932. 0
  1933. prolog-indent-width))
  1934. ;;End of /* */ comment
  1935. ((looking-at "\\*/")
  1936. (save-excursion
  1937. (prolog-find-start-of-mline-comment)
  1938. (skip-chars-backward " \t")
  1939. (- (current-column) 2)))
  1940. ;; Here we check if the current line is within a /* */ pair
  1941. ((and (looking-at "[^%/]")
  1942. (eq (prolog-in-string-or-comment) 'cmt))
  1943. (if prolog-indent-mline-comments-flag
  1944. (prolog-find-start-of-mline-comment)
  1945. ;; Same as before
  1946. (prolog-indentation-level-of-line)))
  1947. (t
  1948. (let ((empty t) ind linebal)
  1949. ;; See previous indentation
  1950. (while empty
  1951. (forward-line -1)
  1952. (beginning-of-line)
  1953. (if (bobp)
  1954. (setq empty nil)
  1955. (skip-chars-forward " \t")
  1956. (if (not (or (not (member (prolog-in-string-or-comment)
  1957. '(nil txt)))
  1958. (looking-at "%")
  1959. (looking-at "\n")))
  1960. (setq empty nil))))
  1961. ;; Store this line's indentation
  1962. (setq ind (if (bobp)
  1963. 0 ;Beginning of buffer.
  1964. (current-column))) ;Beginning of clause.
  1965. ;; Compute the balance of the line
  1966. (setq linebal (prolog-paren-balance))
  1967. ;;(message "bal of previous line %d totbal %d" linebal totbal)
  1968. (if (< linebal 0)
  1969. (progn
  1970. ;; Add 'indent-level' mode to find-unmatched-paren instead?
  1971. (end-of-line)
  1972. (setq ind (prolog-find-indent-of-matching-paren))))
  1973. ;;(message "ind %d" ind)
  1974. (beginning-of-line)
  1975. ;; Check if the line ends with ":-", ".", ":: {", "}" (might be
  1976. ;; unnecessary), "&" or ")" (The last four concerns SICStus objects)
  1977. (cond
  1978. ;; If the last char of the line is a '&' then set the indent level
  1979. ;; to prolog-indent-width (used in SICStus objects)
  1980. ((and (eq prolog-system 'sicstus)
  1981. (looking-at ".+&[ \t]*\\(%.*\\|\\)$"))
  1982. (setq ind prolog-indent-width))
  1983. ;; Increase indentation if the previous line was the head of a rule
  1984. ;; and does not contain a '.'
  1985. ((and (looking-at (format ".*%s[^\\.]*[ \t]*\\(%%.*\\|\\)$"
  1986. prolog-head-delimiter))
  1987. ;; We must check that the match is at a paren balance of 0.
  1988. (save-excursion
  1989. (let ((p (point)))
  1990. (re-search-forward prolog-head-delimiter)
  1991. (>= 0 (prolog-region-paren-balance p (point))))))
  1992. (let ((headindent
  1993. (if (< (prolog-paren-balance) 0)
  1994. (save-excursion
  1995. (end-of-line)
  1996. (prolog-find-indent-of-matching-paren))
  1997. (prolog-indentation-level-of-line))))
  1998. (setq ind (+ headindent prolog-indent-width))))
  1999. ;; The previous line was the head of an object
  2000. ((looking-at ".+ *::.*{[ \t]*$")
  2001. (setq ind prolog-indent-width))
  2002. ;; If a '.' is found at the end of the previous line, then
  2003. ;; decrease the indentation. (The \\(%.*\\|\\) part of the
  2004. ;; regexp is for comments at the end of the line)
  2005. ((and (looking-at "^.+\\.[ \t]*\\(%.*\\|\\)$")
  2006. ;; Make sure that the '.' found is not in a comment or string
  2007. (save-excursion
  2008. (end-of-line)
  2009. (re-search-backward "\\.[ \t]*\\(%.*\\|\\)$" (point-min))
  2010. ;; Guard against the real '.' being followed by a
  2011. ;; commented '.'.
  2012. (if (eq (prolog-in-string-or-comment) 'cmt)
  2013. ;; commented out '.'
  2014. (let ((here (line-beginning-position)))
  2015. (end-of-line)
  2016. (re-search-backward "\\.[ \t]*%.*$" here t))
  2017. (not (prolog-in-string-or-comment))
  2018. )
  2019. ))
  2020. (setq ind 0))
  2021. ;; If a '.' is found at the end of the previous line, then
  2022. ;; decrease the indentation. (The /\\*.*\\*/ part of the
  2023. ;; regexp is for C-like comments at the end of the
  2024. ;; line--can we merge with the case above?).
  2025. ((and (looking-at "^.+\\.[ \t]*\\(/\\*.*\\|\\)$")
  2026. ;; Make sure that the '.' found is not in a comment or string
  2027. (save-excursion
  2028. (end-of-line)
  2029. (re-search-backward "\\.[ \t]*\\(/\\*.*\\|\\)$" (point-min))
  2030. ;; Guard against the real '.' being followed by a
  2031. ;; commented '.'.
  2032. (if (eq (prolog-in-string-or-comment) 'cmt)
  2033. ;; commented out '.'
  2034. (let ((here (line-beginning-position)))
  2035. (end-of-line)
  2036. (re-search-backward "\\.[ \t]*/\\*.*$" here t))
  2037. (not (prolog-in-string-or-comment))
  2038. )
  2039. ))
  2040. (setq ind 0))
  2041. )
  2042. ;; If the last non comment char is a ',' or left paren or a left-
  2043. ;; indent-regexp then indent to open parenthesis level
  2044. (if (and
  2045. (> totbal 0)
  2046. ;; SICStus objects have special syntax rules if point is
  2047. ;; not inside additional parens (objects are defined
  2048. ;; within {...})
  2049. (not (and (eq prolog-system 'sicstus)
  2050. (= totbal 1)
  2051. (prolog-in-object))))
  2052. (if (looking-at
  2053. (format "\\(%s\\|%s\\|0'.\\|[0-9]+'[0-9a-zA-Z]+\\|[^\n\'\"%%]\\)*\\(,\\|%s\\|%s\\)\[ \t]*\\(%%.*\\|\\)$"
  2054. prolog-quoted-atom-regexp prolog-string-regexp
  2055. prolog-left-paren prolog-left-indent-regexp))
  2056. (progn
  2057. (goto-char oldpoint)
  2058. (setq ind (prolog-find-unmatched-paren
  2059. (if prolog-paren-indent-p
  2060. 'termdependent
  2061. 'skipwhite)))
  2062. ;;(setq ind (prolog-find-unmatched-paren 'termdependent))
  2063. )
  2064. (goto-char oldpoint)
  2065. (setq ind (prolog-find-unmatched-paren nil))
  2066. ))
  2067. ;; Return the indentation level
  2068. ind
  2069. ))))))
  2070. (defun prolog-find-indent-of-matching-paren ()
  2071. "Find the indentation level based on the matching parenthesis.
  2072. Indentation level is set to the one the point is after when the function is
  2073. called."
  2074. (save-excursion
  2075. ;; Go to the matching paren
  2076. (if prolog-use-prolog-tokenizer-flag
  2077. (prolog-backward-list)
  2078. (backward-list))
  2079. ;; If this was the first paren on the line then return this line's
  2080. ;; indentation level
  2081. (if (prolog-paren-is-the-first-on-line-p)
  2082. (prolog-indentation-level-of-line)
  2083. ;; It was not the first one
  2084. (progn
  2085. ;; Find the next paren
  2086. (prolog-goto-next-paren 0)
  2087. ;; If this paren is a left one then use its column as indent level,
  2088. ;; if not then recurse this function
  2089. (if (looking-at prolog-left-paren)
  2090. (+ (current-column) 1)
  2091. (progn
  2092. (forward-char 1)
  2093. (prolog-find-indent-of-matching-paren)))
  2094. ))
  2095. ))
  2096. (defun prolog-indentation-level-of-line ()
  2097. "Return the indentation level of the current line."
  2098. (save-excursion
  2099. (beginning-of-line)
  2100. (skip-chars-forward " \t")
  2101. (current-column)))
  2102. (defun prolog-paren-is-the-first-on-line-p ()
  2103. "Return t if the parenthesis under the point is the first one on the line.
  2104. Return nil otherwise.
  2105. Note: does not check if the point is actually at a parenthesis!"
  2106. (save-excursion
  2107. (let ((begofline (line-beginning-position)))
  2108. (if (= begofline (point))
  2109. t
  2110. (if (prolog-goto-next-paren begofline)
  2111. nil
  2112. t)))))
  2113. (defun prolog-find-unmatched-paren (&optional mode)
  2114. "Return the column of the last unmatched left parenthesis.
  2115. If MODE is `skipwhite' then any white space after the parenthesis is added to
  2116. the answer.
  2117. If MODE is `plusone' then the parenthesis' column +1 is returned.
  2118. If MODE is `termdependent' then if the unmatched parenthesis is part of
  2119. a compound term the function will work as `skipwhite', otherwise
  2120. it will return the column paren plus the value of `prolog-paren-indent'.
  2121. If MODE is nil or not set then the parenthesis' exact column is returned."
  2122. (save-excursion
  2123. ;; If the next paren we find is a left one we're finished, if it's
  2124. ;; a right one then we go back one step and recurse
  2125. (prolog-goto-next-paren 0)
  2126. (let ((roundparen (looking-at "(")))
  2127. (if (looking-at prolog-left-paren)
  2128. (let ((not-part-of-term
  2129. (save-excursion
  2130. (backward-char 1)
  2131. (looking-at "[ \t]"))))
  2132. (if (eq mode nil)
  2133. (current-column)
  2134. (if (and roundparen
  2135. (eq mode 'termdependent)
  2136. not-part-of-term)
  2137. (+ (current-column)
  2138. (if prolog-electric-tab-flag
  2139. ;; Electric TAB
  2140. prolog-paren-indent
  2141. ;; Not electric TAB
  2142. (if (looking-at ".[ \t]*$")
  2143. 2
  2144. prolog-paren-indent))
  2145. )
  2146. (forward-char 1)
  2147. (if (or (eq mode 'skipwhite) (eq mode 'termdependent) )
  2148. (skip-chars-forward " \t"))
  2149. (current-column))))
  2150. ;; Not looking at left paren
  2151. (progn
  2152. (forward-char 1)
  2153. ;; Go to the matching paren. When we get there we have a total
  2154. ;; balance of 0.
  2155. (if prolog-use-prolog-tokenizer-flag
  2156. (prolog-backward-list)
  2157. (backward-list))
  2158. (prolog-find-unmatched-paren mode)))
  2159. )))
  2160. (defun prolog-paren-balance ()
  2161. "Return the parenthesis balance of the current line.
  2162. A return value of n means n more left parentheses than right ones."
  2163. (save-excursion
  2164. (end-of-line)
  2165. (prolog-region-paren-balance (line-beginning-position) (point))))
  2166. (defun prolog-region-paren-balance (beg end)
  2167. "Return the summed parenthesis balance in the region.
  2168. The region is limited by BEG and END positions."
  2169. (save-excursion
  2170. (let ((state (if prolog-use-prolog-tokenizer-flag
  2171. (prolog-tokenize beg end)
  2172. (parse-partial-sexp beg end))))
  2173. (nth 0 state))))
  2174. (defun prolog-goto-next-paren (limit-pos)
  2175. "Move the point to the next parenthesis earlier in the buffer.
  2176. Return t if a match was found before LIMIT-POS. Return nil otherwise."
  2177. (let ((retval (re-search-backward
  2178. (concat prolog-left-paren "\\|" prolog-right-paren)
  2179. limit-pos t)))
  2180. ;; If a match was found but it was in a string or comment, then recurse
  2181. (if (and retval (prolog-in-string-or-comment))
  2182. (prolog-goto-next-paren limit-pos)
  2183. retval)
  2184. ))
  2185. (defun prolog-in-string-or-comment ()
  2186. "Check whether string, atom, or comment is under current point.
  2187. Return:
  2188. `txt' if the point is in a string, atom, or character code expression
  2189. `cmt' if the point is in a comment
  2190. nil otherwise."
  2191. (save-excursion
  2192. (let* ((start
  2193. (if (eq prolog-parse-mode 'beg-of-line)
  2194. ;; 'beg-of-line
  2195. (save-excursion
  2196. (let (safepoint)
  2197. (beginning-of-line)
  2198. (setq safepoint (point))
  2199. (while (and (> (point) (point-min))
  2200. (progn
  2201. (forward-line -1)
  2202. (end-of-line)
  2203. (if (not (bobp))
  2204. (backward-char 1))
  2205. (looking-at "\\\\"))
  2206. )
  2207. (beginning-of-line)
  2208. (setq safepoint (point)))
  2209. safepoint))
  2210. ;; 'beg-of-clause
  2211. (prolog-clause-start)))
  2212. (end (point))
  2213. (state (if prolog-use-prolog-tokenizer-flag
  2214. (prolog-tokenize start end)
  2215. (if (fboundp 'syntax-ppss)
  2216. (syntax-ppss)
  2217. (parse-partial-sexp start end)))))
  2218. (cond
  2219. ((nth 3 state) 'txt) ; String
  2220. ((nth 4 state) 'cmt) ; Comment
  2221. (t
  2222. (cond
  2223. ((looking-at "%") 'cmt) ; Start of a comment
  2224. ((looking-at "/\\*") 'cmt) ; Start of a comment
  2225. ((looking-at "\'") 'txt) ; Start of an atom
  2226. ((looking-at "\"") 'txt) ; Start of a string
  2227. (t nil)
  2228. ))))
  2229. ))
  2230. (defun prolog-find-start-of-mline-comment ()
  2231. "Return the start column of a /* */ comment.
  2232. This assumes that the point is inside a comment."
  2233. (re-search-backward "/\\*" (point-min) t)
  2234. (forward-char 2)
  2235. (skip-chars-forward " \t")
  2236. (current-column))
  2237. (defun prolog-insert-spaces-after-paren ()
  2238. "Insert spaces after the opening parenthesis, \"then\" (->) and \"else\" (;) branches.
  2239. Spaces are inserted if all preceding objects on the line are
  2240. whitespace characters, parentheses, or then/else branches."
  2241. (save-excursion
  2242. (let ((regexp (concat "(\\|" prolog-left-indent-regexp))
  2243. level)
  2244. (beginning-of-line)
  2245. (skip-chars-forward " \t")
  2246. (when (looking-at regexp)
  2247. ;; Treat "( If -> " lines specially.
  2248. ;;(setq incr (if (looking-at "(.*->")
  2249. ;; 2
  2250. ;; prolog-paren-indent))
  2251. ;; work on all subsequent "->", "(", ";"
  2252. (while (looking-at regexp)
  2253. (goto-char (match-end 0))
  2254. (setq level (+ (prolog-find-unmatched-paren) prolog-paren-indent))
  2255. ;; Remove old white space
  2256. (let ((start (point)))
  2257. (skip-chars-forward " \t")
  2258. (delete-region start (point)))
  2259. (indent-to level)
  2260. (skip-chars-forward " \t"))
  2261. )))
  2262. (when (save-excursion
  2263. (backward-char 2)
  2264. (looking-at "\\s ;\\|\\s (\\|->")) ; (looking-at "\\s \\((\\|;\\)"))
  2265. (skip-chars-forward " \t"))
  2266. )
  2267. ;;;; Comment filling
  2268. (defun prolog-comment-limits ()
  2269. "Return the current comment limits plus the comment type (block or line).
  2270. The comment limits are the range of a block comment or the range that
  2271. contains all adjacent line comments (i.e. all comments that starts in
  2272. the same column with no empty lines or non-whitespace characters
  2273. between them)."
  2274. (let ((here (point))
  2275. lit-limits-b lit-limits-e lit-type beg end
  2276. )
  2277. (save-restriction
  2278. ;; Widen to catch comment limits correctly.
  2279. (widen)
  2280. (setq end (line-end-position)
  2281. beg (line-beginning-position))
  2282. (save-excursion
  2283. (beginning-of-line)
  2284. (setq lit-type (if (search-forward-regexp "%" end t) 'line 'block))
  2285. ; (setq lit-type 'line)
  2286. ;(if (search-forward-regexp "^[ \t]*%" end t)
  2287. ; (setq lit-type 'line)
  2288. ; (if (not (search-forward-regexp "%" end t))
  2289. ; (setq lit-type 'block)
  2290. ; (if (not (= (forward-line 1) 0))
  2291. ; (setq lit-type 'block)
  2292. ; (setq done t
  2293. ; ret (prolog-comment-limits)))
  2294. ; ))
  2295. (if (eq lit-type 'block)
  2296. (progn
  2297. (goto-char here)
  2298. (when (looking-at "/\\*") (forward-char 2))
  2299. (when (and (looking-at "\\*") (> (point) (point-min))
  2300. (forward-char -1) (looking-at "/"))
  2301. (forward-char 1))
  2302. (when (save-excursion (search-backward "/*" nil t))
  2303. (list (save-excursion (search-backward "/*") (point))
  2304. (or (search-forward "*/" nil t) (point-max)) lit-type)))
  2305. ;; line comment
  2306. (setq lit-limits-b (- (point) 1)
  2307. lit-limits-e end)
  2308. (condition-case nil
  2309. (if (progn (goto-char lit-limits-b)
  2310. (looking-at "%"))
  2311. (let ((col (current-column)) done)
  2312. (setq beg (point)
  2313. end lit-limits-e)
  2314. ;; Always at the beginning of the comment
  2315. ;; Go backward now
  2316. (beginning-of-line)
  2317. (while (and (zerop (setq done (forward-line -1)))
  2318. (search-forward-regexp "^[ \t]*%"
  2319. (line-end-position) t)
  2320. (= (+ 1 col) (current-column)))
  2321. (setq beg (- (point) 1)))
  2322. (when (= done 0)
  2323. (forward-line 1))
  2324. ;; We may have a line with code above...
  2325. (when (and (zerop (setq done (forward-line -1)))
  2326. (search-forward "%" (line-end-position) t)
  2327. (= (+ 1 col) (current-column)))
  2328. (setq beg (- (point) 1)))
  2329. (when (= done 0)
  2330. (forward-line 1))
  2331. ;; Go forward
  2332. (goto-char lit-limits-b)
  2333. (beginning-of-line)
  2334. (while (and (zerop (forward-line 1))
  2335. (search-forward-regexp "^[ \t]*%"
  2336. (line-end-position) t)
  2337. (= (+ 1 col) (current-column)))
  2338. (setq end (line-end-position)))
  2339. (list beg end lit-type))
  2340. (list lit-limits-b lit-limits-e lit-type)
  2341. )
  2342. (error (list lit-limits-b lit-limits-e lit-type))))
  2343. ))))
  2344. (defun prolog-guess-fill-prefix ()
  2345. ;; fill 'txt entities?
  2346. (when (save-excursion
  2347. (end-of-line)
  2348. (equal (prolog-in-string-or-comment) 'cmt))
  2349. (let* ((bounds (prolog-comment-limits))
  2350. (cbeg (car bounds))
  2351. (type (nth 2 bounds))
  2352. beg end)
  2353. (save-excursion
  2354. (end-of-line)
  2355. (setq end (point))
  2356. (beginning-of-line)
  2357. (setq beg (point))
  2358. (if (and (eq type 'line)
  2359. (> cbeg beg)
  2360. (save-excursion (not (search-forward-regexp "^[ \t]*%"
  2361. cbeg t))))
  2362. (progn
  2363. (goto-char cbeg)
  2364. (search-forward-regexp "%+[ \t]*" end t)
  2365. (prolog-replace-in-string (buffer-substring beg (point))
  2366. "[^ \t%]" " "))
  2367. ;(goto-char beg)
  2368. (if (search-forward-regexp "^[ \t]*\\(%+\\|\\*+\\|/\\*+\\)[ \t]*"
  2369. end t)
  2370. (prolog-replace-in-string (buffer-substring beg (point)) "/" " ")
  2371. (beginning-of-line)
  2372. (when (search-forward-regexp "^[ \t]+" end t)
  2373. (buffer-substring beg (point)))))))))
  2374. (defun prolog-fill-paragraph ()
  2375. "Fill paragraph comment at or after point."
  2376. (interactive)
  2377. (let* ((bounds (prolog-comment-limits))
  2378. (type (nth 2 bounds)))
  2379. (if (eq type 'line)
  2380. (let ((fill-prefix (prolog-guess-fill-prefix)))
  2381. (fill-paragraph nil))
  2382. (save-excursion
  2383. (save-restriction
  2384. ;; exclude surrounding lines that delimit a multiline comment
  2385. ;; and don't contain alphabetic characters, like "/*******",
  2386. ;; "- - - */" etc.
  2387. (save-excursion
  2388. (backward-paragraph)
  2389. (unless (bobp) (forward-line))
  2390. (if (string-match "^/\\*[^a-zA-Z]*$" (thing-at-point 'line))
  2391. (narrow-to-region (point-at-eol) (point-max))))
  2392. (save-excursion
  2393. (forward-paragraph)
  2394. (forward-line -1)
  2395. (if (string-match "^[^a-zA-Z]*\\*/$" (thing-at-point 'line))
  2396. (narrow-to-region (point-min) (point-at-bol))))
  2397. (let ((fill-prefix (prolog-guess-fill-prefix)))
  2398. (fill-paragraph nil))))
  2399. )))
  2400. (defun prolog-do-auto-fill ()
  2401. "Carry out Auto Fill for Prolog mode.
  2402. In effect it sets the `fill-prefix' when inside comments and then calls
  2403. `do-auto-fill'."
  2404. (let ((fill-prefix (prolog-guess-fill-prefix)))
  2405. (do-auto-fill)
  2406. ))
  2407. (defalias 'prolog-replace-in-string
  2408. (if (fboundp 'replace-in-string)
  2409. #'replace-in-string
  2410. (lambda (str regexp newtext &optional literal)
  2411. (replace-regexp-in-string regexp newtext str nil literal))))
  2412. ;;-------------------------------------------------------------------
  2413. ;; The tokenizer
  2414. ;;-------------------------------------------------------------------
  2415. (defconst prolog-tokenize-searchkey
  2416. (concat "[0-9]+'"
  2417. "\\|"
  2418. "['\"]"
  2419. "\\|"
  2420. prolog-left-paren
  2421. "\\|"
  2422. prolog-right-paren
  2423. "\\|"
  2424. "%"
  2425. "\\|"
  2426. "/\\*"
  2427. ))
  2428. (defun prolog-tokenize (beg end &optional stopcond)
  2429. "Tokenize a region of prolog code between BEG and END.
  2430. STOPCOND decides the stop condition of the parsing. Valid values
  2431. are 'zerodepth which stops the parsing at the first right parenthesis
  2432. where the parenthesis depth is zero, 'skipover which skips over
  2433. the current entity (e.g. a list, a string, etc.) and nil.
  2434. The function returns a list with the following information:
  2435. 0. parenthesis depth
  2436. 3. 'atm if END is inside an atom
  2437. 'str if END is inside a string
  2438. 'chr if END is in a character code expression (0'x)
  2439. nil otherwise
  2440. 4. non-nil if END is inside a comment
  2441. 5. end position (always equal to END if STOPCOND is nil)
  2442. The rest of the elements are undefined."
  2443. (save-excursion
  2444. (let* ((end2 (1+ end))
  2445. oldp
  2446. (depth 0)
  2447. (quoted nil)
  2448. inside_cmt
  2449. (endpos end2)
  2450. skiptype ; The type of entity we'll skip over
  2451. )
  2452. (goto-char beg)
  2453. (if (and (eq stopcond 'skipover)
  2454. (looking-at "[^[({'\"]"))
  2455. (setq endpos (point)) ; Stay where we are
  2456. (while (and
  2457. (re-search-forward prolog-tokenize-searchkey end2 t)
  2458. (< (point) end2))
  2459. (progn
  2460. (setq oldp (point))
  2461. (goto-char (match-beginning 0))
  2462. (cond
  2463. ;; Atoms and strings
  2464. ((looking-at "'")
  2465. ;; Find end of atom
  2466. (if (re-search-forward "[^\\]'" end2 'limit)
  2467. ;; Found end of atom
  2468. (progn
  2469. (setq oldp end2)
  2470. (if (and (eq stopcond 'skipover)
  2471. (not skiptype))
  2472. (setq endpos (point))
  2473. (setq oldp (point)))) ; Continue tokenizing
  2474. (setq quoted 'atm)))
  2475. ((looking-at "\"")
  2476. ;; Find end of string
  2477. (if (re-search-forward "[^\\]\"" end2 'limit)
  2478. ;; Found end of string
  2479. (progn
  2480. (setq oldp end2)
  2481. (if (and (eq stopcond 'skipover)
  2482. (not skiptype))
  2483. (setq endpos (point))
  2484. (setq oldp (point)))) ; Continue tokenizing
  2485. (setq quoted 'str)))
  2486. ;; Paren stuff
  2487. ((looking-at prolog-left-paren)
  2488. (setq depth (1+ depth))
  2489. (setq skiptype 'paren))
  2490. ((looking-at prolog-right-paren)
  2491. (setq depth (1- depth))
  2492. (if (and
  2493. (or (eq stopcond 'zerodepth)
  2494. (and (eq stopcond 'skipover)
  2495. (eq skiptype 'paren)))
  2496. (= depth 0))
  2497. (progn
  2498. (setq endpos (1+ (point)))
  2499. (setq oldp end2))))
  2500. ;; Comment stuff
  2501. ((looking-at comment-start)
  2502. (end-of-line)
  2503. ;; (if (>= (point) end2)
  2504. (if (>= (point) end)
  2505. (progn
  2506. (setq inside_cmt t)
  2507. (setq oldp end2))
  2508. (setq oldp (point))))
  2509. ((looking-at "/\\*")
  2510. (if (re-search-forward "\\*/" end2 'limit)
  2511. (setq oldp (point))
  2512. (setq inside_cmt t)
  2513. (setq oldp end2)))
  2514. ;; 0'char
  2515. ((looking-at "0'")
  2516. (setq oldp (1+ (match-end 0)))
  2517. (if (> oldp end)
  2518. (setq quoted 'chr)))
  2519. ;; base'number
  2520. ((looking-at "[0-9]+'")
  2521. (goto-char (match-end 0))
  2522. (skip-chars-forward "0-9a-zA-Z")
  2523. (setq oldp (point)))
  2524. )
  2525. (goto-char oldp)
  2526. )) ; End of while
  2527. )
  2528. ;; Deal with multi-line comments
  2529. (and (prolog-inside-mline-comment end)
  2530. (setq inside_cmt t))
  2531. ;; Create return list
  2532. (list depth nil nil quoted inside_cmt endpos)
  2533. )))
  2534. (defun prolog-inside-mline-comment (here)
  2535. (save-excursion
  2536. (goto-char here)
  2537. (let* ((next-close (save-excursion (search-forward "*/" nil t)))
  2538. (next-open (save-excursion (search-forward "/*" nil t)))
  2539. (prev-open (save-excursion (search-backward "/*" nil t)))
  2540. (prev-close (save-excursion (search-backward "*/" nil t)))
  2541. (unmatched-next-close (and next-close
  2542. (or (not next-open)
  2543. (> next-open next-close))))
  2544. (unmatched-prev-open (and prev-open
  2545. (or (not prev-close)
  2546. (> prev-open prev-close))))
  2547. )
  2548. (or unmatched-next-close unmatched-prev-open)
  2549. )))
  2550. ;;-------------------------------------------------------------------
  2551. ;; Online help
  2552. ;;-------------------------------------------------------------------
  2553. (defvar prolog-help-function
  2554. '((mercury nil)
  2555. (eclipse prolog-help-online)
  2556. ;; (sicstus prolog-help-info)
  2557. (sicstus prolog-find-documentation)
  2558. (swi prolog-help-online)
  2559. (t prolog-help-online))
  2560. "Alist for the name of the function for finding help on a predicate.")
  2561. (defun prolog-help-on-predicate ()
  2562. "Invoke online help on the atom under cursor."
  2563. (interactive)
  2564. (cond
  2565. ;; Redirect help for SICStus to `prolog-find-documentation'.
  2566. ((eq prolog-help-function-i 'prolog-find-documentation)
  2567. (prolog-find-documentation))
  2568. ;; Otherwise, ask for the predicate name and then call the function
  2569. ;; in prolog-help-function-i
  2570. (t
  2571. (let* ((word (prolog-atom-under-point))
  2572. (predicate (read-string
  2573. (format "Help on predicate%s: "
  2574. (if word
  2575. (concat " (default " word ")")
  2576. ""))
  2577. nil nil word))
  2578. ;;point
  2579. )
  2580. (if prolog-help-function-i
  2581. (funcall prolog-help-function-i predicate)
  2582. (error "Sorry, no help method defined for this Prolog system."))))
  2583. ))
  2584. (defun prolog-help-info (predicate)
  2585. (let ((buffer (current-buffer))
  2586. oldp
  2587. (str (concat "^\\* " (regexp-quote predicate) " */")))
  2588. (require 'info)
  2589. (pop-to-buffer nil)
  2590. (Info-goto-node prolog-info-predicate-index)
  2591. (if (not (re-search-forward str nil t))
  2592. (error (format "Help on predicate `%s' not found." predicate)))
  2593. (setq oldp (point))
  2594. (if (re-search-forward str nil t)
  2595. ;; Multiple matches, ask user
  2596. (let ((max 2)
  2597. n)
  2598. ;; Count matches
  2599. (while (re-search-forward str nil t)
  2600. (setq max (1+ max)))
  2601. (goto-char oldp)
  2602. (re-search-backward "[^ /]" nil t)
  2603. (recenter 0)
  2604. (setq n (read-string ;; was read-input, which is obsolete
  2605. (format "Several matches, choose (1-%d): " max) "1"))
  2606. (forward-line (- (string-to-number n) 1)))
  2607. ;; Single match
  2608. (re-search-backward "[^ /]" nil t))
  2609. ;; (Info-follow-nearest-node (point))
  2610. (prolog-Info-follow-nearest-node)
  2611. (re-search-forward (concat "^`" (regexp-quote predicate)) nil t)
  2612. (beginning-of-line)
  2613. (recenter 0)
  2614. (pop-to-buffer buffer)))
  2615. (defun prolog-Info-follow-nearest-node ()
  2616. (if (featurep 'xemacs)
  2617. (Info-follow-nearest-node (point))
  2618. (Info-follow-nearest-node)))
  2619. (defun prolog-help-online (predicate)
  2620. (prolog-ensure-process)
  2621. (process-send-string "prolog" (concat "help(" predicate ").\n"))
  2622. (display-buffer "*prolog*"))
  2623. (defun prolog-help-apropos (string)
  2624. "Find Prolog apropos on given STRING.
  2625. This function is only available when `prolog-system' is set to `swi'."
  2626. (interactive "sApropos: ")
  2627. (cond
  2628. ((eq prolog-system 'swi)
  2629. (prolog-ensure-process)
  2630. (process-send-string "prolog" (concat "apropos(" string ").\n"))
  2631. (display-buffer "*prolog*"))
  2632. (t
  2633. (error "Sorry, no Prolog apropos available for this Prolog system."))))
  2634. (defun prolog-atom-under-point ()
  2635. "Return the atom under or left to the point."
  2636. (save-excursion
  2637. (let ((nonatom_chars "[](){},\. \t\n")
  2638. start)
  2639. (skip-chars-forward (concat "^" nonatom_chars))
  2640. (skip-chars-backward nonatom_chars)
  2641. (skip-chars-backward (concat "^" nonatom_chars))
  2642. (setq start (point))
  2643. (skip-chars-forward (concat "^" nonatom_chars))
  2644. (buffer-substring-no-properties start (point))
  2645. )))
  2646. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2647. ;; Help function with completion
  2648. ;; Stolen from Per Mildner's SICStus debugger mode and modified
  2649. (defun prolog-find-documentation ()
  2650. "Go to the Info node for a predicate in the SICStus Info manual."
  2651. (interactive)
  2652. (let ((pred (prolog-read-predicate)))
  2653. (prolog-goto-predicate-info pred)))
  2654. (defvar prolog-info-alist nil
  2655. "Alist with all builtin predicates.
  2656. Only for internal use by `prolog-find-documentation'")
  2657. ;; Very similar to prolog-help-info except that that function cannot
  2658. ;; cope with arity and that it asks the user if there are several
  2659. ;; functors with different arity. This function also uses
  2660. ;; prolog-info-alist for finding the info node, rather than parsing
  2661. ;; the predicate index.
  2662. (defun prolog-goto-predicate-info (predicate)
  2663. "Go to the info page for PREDICATE, which is a PredSpec."
  2664. (interactive)
  2665. (require 'info)
  2666. (string-match "\\(.*\\)/\\([0-9]+\\).*$" predicate)
  2667. (let ((buffer (current-buffer))
  2668. (name (match-string 1 predicate))
  2669. (arity (string-to-number (match-string 2 predicate)))
  2670. ;oldp
  2671. ;(str (regexp-quote predicate))
  2672. )
  2673. (pop-to-buffer nil)
  2674. (Info-goto-node
  2675. prolog-info-predicate-index) ;; We must be in the SICStus pages
  2676. (Info-goto-node (car (cdr (assoc predicate prolog-info-alist))))
  2677. (prolog-find-term (regexp-quote name) arity "^`")
  2678. (recenter 0)
  2679. (pop-to-buffer buffer))
  2680. )
  2681. (defun prolog-read-predicate ()
  2682. "Read a PredSpec from the user.
  2683. Returned value is a string \"FUNCTOR/ARITY\".
  2684. Interaction supports completion."
  2685. (let ((default (prolog-atom-under-point)))
  2686. ;; If the predicate index is not yet built, do it now
  2687. (if (not prolog-info-alist)
  2688. (prolog-build-info-alist))
  2689. ;; Test if the default string could be the base for completion.
  2690. ;; Discard it if not.
  2691. (if (eq (try-completion default prolog-info-alist) nil)
  2692. (setq default nil))
  2693. ;; Read the PredSpec from the user
  2694. (completing-read
  2695. (if (zerop (length default))
  2696. "Help on predicate: "
  2697. (concat "Help on predicate (default " default "): "))
  2698. prolog-info-alist nil t nil nil default)))
  2699. (defun prolog-build-info-alist (&optional verbose)
  2700. "Build an alist of all builtins and library predicates.
  2701. Each element is of the form (\"NAME/ARITY\" . (INFO-NODE1 INFO-NODE2 ...)).
  2702. Typically there is just one Info node associated with each name
  2703. If an optional argument VERBOSE is non-nil, print messages at the beginning
  2704. and end of list building."
  2705. (if verbose
  2706. (message "Building info alist..."))
  2707. (setq prolog-info-alist
  2708. (let ((l ())
  2709. (last-entry (cons "" ())))
  2710. (save-excursion
  2711. (save-window-excursion
  2712. ;; select any window but the minibuffer (as we cannot switch
  2713. ;; buffers in minibuffer window.
  2714. ;; I am not sure this is the right/best way
  2715. (if (active-minibuffer-window) ; nil if none active
  2716. (select-window (next-window)))
  2717. ;; Do this after going away from minibuffer window
  2718. (save-window-excursion
  2719. (info))
  2720. (Info-goto-node prolog-info-predicate-index)
  2721. (goto-char (point-min))
  2722. (while (re-search-forward
  2723. "^\\* \\(.+\\)/\\([0-9]+\\)\\([^\n:*]*\\):" nil t)
  2724. (let* ((name (match-string 1))
  2725. (arity (string-to-number (match-string 2)))
  2726. (comment (match-string 3))
  2727. (fa (format "%s/%d%s" name arity comment))
  2728. info-node)
  2729. (beginning-of-line)
  2730. ;; Extract the info node name
  2731. (setq info-node (progn
  2732. (re-search-forward ":[ \t]*\\([^:]+\\).$")
  2733. (match-string 1)
  2734. ))
  2735. ;; ###### Easier? (from Milan version 0.1.28)
  2736. ;; (setq info-node (Info-extract-menu-node-name))
  2737. (if (equal fa (car last-entry))
  2738. (setcdr last-entry (cons info-node (cdr last-entry)))
  2739. (setq last-entry (cons fa (list info-node))
  2740. l (cons last-entry l)))))
  2741. (nreverse l)
  2742. ))))
  2743. (if verbose
  2744. (message "Building info alist... done.")))
  2745. ;;-------------------------------------------------------------------
  2746. ;; Miscellaneous functions
  2747. ;;-------------------------------------------------------------------
  2748. ;; For Windows. Change backslash to slash. SICStus handles either
  2749. ;; path separator but backslash must be doubled, therefore use slash.
  2750. (defun prolog-bsts (string)
  2751. "Change backslashes to slashes in STRING."
  2752. (let ((str1 (copy-sequence string))
  2753. (len (length string))
  2754. (i 0))
  2755. (while (< i len)
  2756. (if (char-equal (aref str1 i) ?\\)
  2757. (aset str1 i ?/))
  2758. (setq i (1+ i)))
  2759. str1))
  2760. ;;(defun prolog-temporary-file ()
  2761. ;; "Make temporary file name for compilation."
  2762. ;; (make-temp-name
  2763. ;; (concat
  2764. ;; (or
  2765. ;; (getenv "TMPDIR")
  2766. ;; (getenv "TEMP")
  2767. ;; (getenv "TMP")
  2768. ;; (getenv "SYSTEMP")
  2769. ;; "/tmp")
  2770. ;; "/prolcomp")))
  2771. ;;(setq prolog-temp-filename (prolog-bsts (prolog-temporary-file)))
  2772. (defun prolog-temporary-file ()
  2773. "Make temporary file name for compilation."
  2774. (if prolog-temporary-file-name
  2775. ;; We already have a file, erase content and continue
  2776. (progn
  2777. (write-region "" nil prolog-temporary-file-name nil 'silent)
  2778. prolog-temporary-file-name)
  2779. ;; Actually create the file and set `prolog-temporary-file-name'
  2780. ;; accordingly.
  2781. (setq prolog-temporary-file-name
  2782. (make-temp-file "prolcomp" nil ".pl"))))
  2783. (defun prolog-goto-prolog-process-buffer ()
  2784. "Switch to the prolog process buffer and go to its end."
  2785. (switch-to-buffer-other-window "*prolog*")
  2786. (goto-char (point-max))
  2787. )
  2788. (defun prolog-enable-sicstus-sd ()
  2789. "Enable the source level debugging facilities of SICStus 3.7 and later."
  2790. (interactive)
  2791. (require 'pltrace) ; Load the SICStus debugger code
  2792. ;; Turn on the source level debugging by default
  2793. (add-hook 'prolog-inferior-mode-hook 'pltrace-on)
  2794. (if (not prolog-use-sicstus-sd)
  2795. (progn
  2796. ;; If there is a *prolog* buffer, then call pltrace-on
  2797. (if (get-buffer "*prolog*")
  2798. ;; Avoid compilation warnings by using eval
  2799. (eval '(pltrace-on)))
  2800. (setq prolog-use-sicstus-sd t)
  2801. )))
  2802. (defun prolog-disable-sicstus-sd ()
  2803. "Disable the source level debugging facilities of SICStus 3.7 and later."
  2804. (interactive)
  2805. (setq prolog-use-sicstus-sd nil)
  2806. ;; Remove the hook
  2807. (remove-hook 'prolog-inferior-mode-hook 'pltrace-on)
  2808. ;; If there is a *prolog* buffer, then call pltrace-off
  2809. (if (get-buffer "*prolog*")
  2810. ;; Avoid compile warnings by using eval
  2811. (eval '(pltrace-off))))
  2812. (defun prolog-toggle-sicstus-sd ()
  2813. ;; FIXME: Use define-minor-mode.
  2814. "Toggle the source level debugging facilities of SICStus 3.7 and later."
  2815. (interactive)
  2816. (if prolog-use-sicstus-sd
  2817. (prolog-disable-sicstus-sd)
  2818. (prolog-enable-sicstus-sd)))
  2819. (defun prolog-debug-on (&optional arg)
  2820. "Enable debugging.
  2821. When called with prefix argument ARG, disable debugging instead."
  2822. (interactive "P")
  2823. (if arg
  2824. (prolog-debug-off)
  2825. (prolog-process-insert-string (get-process "prolog")
  2826. prolog-debug-on-string)
  2827. (process-send-string "prolog" prolog-debug-on-string)))
  2828. (defun prolog-debug-off ()
  2829. "Disable debugging."
  2830. (interactive)
  2831. (prolog-process-insert-string (get-process "prolog")
  2832. prolog-debug-off-string)
  2833. (process-send-string "prolog" prolog-debug-off-string))
  2834. (defun prolog-trace-on (&optional arg)
  2835. "Enable tracing.
  2836. When called with prefix argument ARG, disable tracing instead."
  2837. (interactive "P")
  2838. (if arg
  2839. (prolog-trace-off)
  2840. (prolog-process-insert-string (get-process "prolog")
  2841. prolog-trace-on-string)
  2842. (process-send-string "prolog" prolog-trace-on-string)))
  2843. (defun prolog-trace-off ()
  2844. "Disable tracing."
  2845. (interactive)
  2846. (prolog-process-insert-string (get-process "prolog")
  2847. prolog-trace-off-string)
  2848. (process-send-string "prolog" prolog-trace-off-string))
  2849. (defun prolog-zip-on (&optional arg)
  2850. "Enable zipping (for SICStus 3.7 and later).
  2851. When called with prefix argument ARG, disable zipping instead."
  2852. (interactive "P")
  2853. (if (not (and (eq prolog-system 'sicstus)
  2854. (prolog-atleast-version '(3 . 7))))
  2855. (error "Only works for SICStus 3.7 and later"))
  2856. (if arg
  2857. (prolog-zip-off)
  2858. (prolog-process-insert-string (get-process "prolog")
  2859. prolog-zip-on-string)
  2860. (process-send-string "prolog" prolog-zip-on-string)))
  2861. (defun prolog-zip-off ()
  2862. "Disable zipping (for SICStus 3.7 and later)."
  2863. (interactive)
  2864. (prolog-process-insert-string (get-process "prolog")
  2865. prolog-zip-off-string)
  2866. (process-send-string "prolog" prolog-zip-off-string))
  2867. ;; (defun prolog-create-predicate-index ()
  2868. ;; "Create an index for all predicates in the buffer."
  2869. ;; (let ((predlist '())
  2870. ;; clauseinfo
  2871. ;; object
  2872. ;; pos
  2873. ;; )
  2874. ;; (goto-char (point-min))
  2875. ;; ;; Replace with prolog-clause-start!
  2876. ;; (while (re-search-forward "^.+:-" nil t)
  2877. ;; (setq pos (match-beginning 0))
  2878. ;; (setq clauseinfo (prolog-clause-info))
  2879. ;; (setq object (prolog-in-object))
  2880. ;; (setq predlist (append
  2881. ;; predlist
  2882. ;; (list (cons
  2883. ;; (if (and (eq prolog-system 'sicstus)
  2884. ;; (prolog-in-object))
  2885. ;; (format "%s::%s/%d"
  2886. ;; object
  2887. ;; (nth 0 clauseinfo)
  2888. ;; (nth 1 clauseinfo))
  2889. ;; (format "%s/%d"
  2890. ;; (nth 0 clauseinfo)
  2891. ;; (nth 1 clauseinfo)))
  2892. ;; pos
  2893. ;; ))))
  2894. ;; (prolog-end-of-predicate))
  2895. ;; predlist))
  2896. (defun prolog-get-predspec ()
  2897. (save-excursion
  2898. (let ((state (prolog-clause-info))
  2899. (object (prolog-in-object)))
  2900. (if (or (equal (nth 0 state) "") (equal (prolog-in-string-or-comment) 'cmt))
  2901. nil
  2902. (if (and (eq prolog-system 'sicstus)
  2903. object)
  2904. (format "%s::%s/%d"
  2905. object
  2906. (nth 0 state)
  2907. (nth 1 state))
  2908. (format "%s/%d"
  2909. (nth 0 state)
  2910. (nth 1 state)))
  2911. ))))
  2912. ;; For backward compatibility. Stolen from custom.el.
  2913. (or (fboundp 'match-string)
  2914. ;; Introduced in Emacs 19.29.
  2915. (defun match-string (num &optional string)
  2916. "Return string of text matched by last search.
  2917. NUM specifies which parenthesized expression in the last regexp.
  2918. Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
  2919. Zero means the entire text matched by the whole regexp or whole string.
  2920. STRING should be given if the last search was by `string-match' on STRING."
  2921. (if (match-beginning num)
  2922. (if string
  2923. (substring string (match-beginning num) (match-end num))
  2924. (buffer-substring (match-beginning num) (match-end num))))))
  2925. (defun prolog-pred-start ()
  2926. "Return the starting point of the first clause of the current predicate."
  2927. (save-excursion
  2928. (goto-char (prolog-clause-start))
  2929. ;; Find first clause, unless it was a directive
  2930. (if (and (not (looking-at "[:?]-"))
  2931. (not (looking-at "[ \t]*[%/]")) ; Comment
  2932. )
  2933. (let* ((pinfo (prolog-clause-info))
  2934. (predname (nth 0 pinfo))
  2935. (arity (nth 1 pinfo))
  2936. (op (point)))
  2937. (while (and (re-search-backward
  2938. (format "^%s\\([(\\.]\\| *%s\\)"
  2939. predname prolog-head-delimiter) nil t)
  2940. (= arity (nth 1 (prolog-clause-info)))
  2941. )
  2942. (setq op (point)))
  2943. (if (eq prolog-system 'mercury)
  2944. ;; Skip to the beginning of declarations of the predicate
  2945. (progn
  2946. (goto-char (prolog-beginning-of-clause))
  2947. (while (and (not (eq (point) op))
  2948. (looking-at
  2949. (format ":-[ \t]*\\(pred\\|mode\\)[ \t]+%s"
  2950. predname)))
  2951. (setq op (point))
  2952. (goto-char (prolog-beginning-of-clause)))))
  2953. op)
  2954. (point))))
  2955. (defun prolog-pred-end ()
  2956. "Return the position at the end of the last clause of the current predicate."
  2957. (save-excursion
  2958. (goto-char (prolog-clause-end)) ; if we are before the first predicate
  2959. (goto-char (prolog-clause-start))
  2960. (let* ((pinfo (prolog-clause-info))
  2961. (predname (nth 0 pinfo))
  2962. (arity (nth 1 pinfo))
  2963. oldp
  2964. (notdone t)
  2965. (op (point)))
  2966. (if (looking-at "[:?]-")
  2967. ;; This was a directive
  2968. (progn
  2969. (if (and (eq prolog-system 'mercury)
  2970. (looking-at
  2971. (format ":-[ \t]*\\(pred\\|mode\\)[ \t]+\\(%s+\\)"
  2972. prolog-atom-regexp)))
  2973. ;; Skip predicate declarations
  2974. (progn
  2975. (setq predname (buffer-substring-no-properties
  2976. (match-beginning 2) (match-end 2)))
  2977. (while (re-search-forward
  2978. (format
  2979. "\n*\\(:-[ \t]*\\(pred\\|mode\\)[ \t]+\\)?%s[( \t]"
  2980. predname)
  2981. nil t))))
  2982. (goto-char (prolog-clause-end))
  2983. (setq op (point)))
  2984. ;; It was not a directive, find the last clause
  2985. (while (and notdone
  2986. (re-search-forward
  2987. (format "^%s\\([(\\.]\\| *%s\\)"
  2988. predname prolog-head-delimiter) nil t)
  2989. (= arity (nth 1 (prolog-clause-info))))
  2990. (setq oldp (point))
  2991. (setq op (prolog-clause-end))
  2992. (if (>= oldp op)
  2993. ;; End of clause not found.
  2994. (setq notdone nil)
  2995. ;; Continue while loop
  2996. (goto-char op))))
  2997. op)))
  2998. (defun prolog-clause-start (&optional not-allow-methods)
  2999. "Return the position at the start of the head of the current clause.
  3000. If NOTALLOWMETHODS is non-nil then do not match on methods in
  3001. objects (relevant only if 'prolog-system' is set to 'sicstus)."
  3002. (save-excursion
  3003. (let ((notdone t)
  3004. (retval (point-min)))
  3005. (end-of-line)
  3006. ;; SICStus object?
  3007. (if (and (not not-allow-methods)
  3008. (eq prolog-system 'sicstus)
  3009. (prolog-in-object))
  3010. (while (and
  3011. notdone
  3012. ;; Search for a head or a fact
  3013. (re-search-backward
  3014. ;; If in object, then find method start.
  3015. ;; "^[ \t]+[a-z$].*\\(:-\\|&\\|:: {\\|,\\)"
  3016. "^[ \t]+[a-z$].*\\(:-\\|&\\|:: {\\)" ; The comma causes
  3017. ; problems since we cannot assume
  3018. ; that the line starts at column 0,
  3019. ; thus we don't know if the line
  3020. ; is a head or a subgoal
  3021. (point-min) t))
  3022. (if (>= (prolog-paren-balance) 0) ; To no match on " a) :-"
  3023. ;; Start of method found
  3024. (progn
  3025. (setq retval (point))
  3026. (setq notdone nil)))
  3027. ) ; End of while
  3028. ;; Not in object
  3029. (while (and
  3030. notdone
  3031. ;; Search for a text at beginning of a line
  3032. ;; ######
  3033. ;; (re-search-backward "^[a-z$']" nil t))
  3034. (let ((case-fold-search nil))
  3035. (re-search-backward
  3036. ;; (format "^[%s$']" prolog-lower-case-string)
  3037. ;; FIXME: Use [:lower:]
  3038. (format "^\\([%s$']\\|[:?]-\\)" prolog-lower-case-string)
  3039. nil t)))
  3040. (let ((bal (prolog-paren-balance)))
  3041. (cond
  3042. ((> bal 0)
  3043. ;; Start of clause found
  3044. (progn
  3045. (setq retval (point))
  3046. (setq notdone nil)))
  3047. ((and (= bal 0)
  3048. (looking-at
  3049. (format ".*\\(\\.\\|%s\\|!,\\)[ \t]*\\(%%.*\\|\\)$"
  3050. prolog-head-delimiter)))
  3051. ;; Start of clause found if the line ends with a '.' or
  3052. ;; a prolog-head-delimiter
  3053. (progn
  3054. (setq retval (point))
  3055. (setq notdone nil))
  3056. )
  3057. (t nil) ; Do nothing
  3058. ))))
  3059. retval)))
  3060. (defun prolog-clause-end (&optional not-allow-methods)
  3061. "Return the position at the end of the current clause.
  3062. If NOTALLOWMETHODS is non-nil then do not match on methods in
  3063. objects (relevant only if 'prolog-system' is set to 'sicstus)."
  3064. (save-excursion
  3065. (beginning-of-line) ; Necessary since we use "^...." for the search.
  3066. (if (re-search-forward
  3067. (if (and (not not-allow-methods)
  3068. (eq prolog-system 'sicstus)
  3069. (prolog-in-object))
  3070. (format
  3071. "^\\(%s\\|%s\\|[^\n\'\"%%]\\)*&[ \t]*\\(\\|%%.*\\)$\\|[ \t]*}"
  3072. prolog-quoted-atom-regexp prolog-string-regexp)
  3073. (format
  3074. "^\\(%s\\|%s\\|[^\n\'\"%%]\\)*\\.[ \t]*\\(\\|%%.*\\)$"
  3075. prolog-quoted-atom-regexp prolog-string-regexp))
  3076. nil t)
  3077. (if (and (prolog-in-string-or-comment)
  3078. (not (eobp)))
  3079. (progn
  3080. (forward-char)
  3081. (prolog-clause-end))
  3082. (point))
  3083. (point))))
  3084. (defun prolog-clause-info ()
  3085. "Return a (name arity) list for the current clause."
  3086. (save-excursion
  3087. (goto-char (prolog-clause-start))
  3088. (let* ((op (point))
  3089. (predname
  3090. (if (looking-at prolog-atom-char-regexp)
  3091. (progn
  3092. (skip-chars-forward "^ (\\.")
  3093. (buffer-substring op (point)))
  3094. ""))
  3095. (arity 0))
  3096. ;; Retrieve the arity.
  3097. (if (looking-at prolog-left-paren)
  3098. (let ((endp (save-excursion
  3099. (prolog-forward-list) (point))))
  3100. (setq arity 1)
  3101. (forward-char 1) ; Skip the opening paren.
  3102. (while (progn
  3103. (skip-chars-forward "^[({,'\"")
  3104. (< (point) endp))
  3105. (if (looking-at ",")
  3106. (progn
  3107. (setq arity (1+ arity))
  3108. (forward-char 1) ; Skip the comma.
  3109. )
  3110. ;; We found a string, list or something else we want
  3111. ;; to skip over. Always use prolog-tokenize,
  3112. ;; parse-partial-sexp does not have a 'skipover mode.
  3113. (goto-char (nth 5 (prolog-tokenize (point) endp 'skipover))))
  3114. )))
  3115. (list predname arity))))
  3116. (defun prolog-in-object ()
  3117. "Return object name if the point is inside a SICStus object definition."
  3118. ;; Return object name if the last line that starts with a character
  3119. ;; that is neither white space nor a comment start
  3120. (save-excursion
  3121. (if (save-excursion
  3122. (beginning-of-line)
  3123. (looking-at "\\([^\n ]+\\)[ \t]*::[ \t]*{"))
  3124. ;; We were in the head of the object
  3125. (match-string 1)
  3126. ;; We were not in the head
  3127. (if (and (re-search-backward "^[a-z$'}]" nil t)
  3128. (looking-at "\\([^\n ]+\\)[ \t]*::[ \t]*{"))
  3129. (match-string 1)
  3130. nil))))
  3131. (defun prolog-forward-list ()
  3132. "Move the point to the matching right parenthesis."
  3133. (interactive)
  3134. (if prolog-use-prolog-tokenizer-flag
  3135. (let ((state (prolog-tokenize (point) (point-max) 'zerodepth)))
  3136. (goto-char (nth 5 state)))
  3137. (forward-list)))
  3138. ;; NB: This could be done more efficiently!
  3139. (defun prolog-backward-list ()
  3140. "Move the point to the matching left parenthesis."
  3141. (interactive)
  3142. (if prolog-use-prolog-tokenizer-flag
  3143. (let ((bal 0)
  3144. (paren-regexp (concat prolog-left-paren "\\|" prolog-right-paren))
  3145. (notdone t))
  3146. ;; FIXME: Doesn't this incorrectly count 0'( and 0') ?
  3147. (while (and notdone (re-search-backward paren-regexp nil t))
  3148. (cond
  3149. ((looking-at prolog-left-paren)
  3150. (if (not (prolog-in-string-or-comment))
  3151. (setq bal (1+ bal)))
  3152. (if (= bal 0)
  3153. (setq notdone nil)))
  3154. ((looking-at prolog-right-paren)
  3155. (if (not (prolog-in-string-or-comment))
  3156. (setq bal (1- bal))))
  3157. )))
  3158. (backward-list)))
  3159. (defun prolog-beginning-of-clause ()
  3160. "Move to the beginning of current clause.
  3161. If already at the beginning of clause, move to previous clause."
  3162. (interactive)
  3163. (let ((point (point))
  3164. (new-point (prolog-clause-start)))
  3165. (if (and (>= new-point point)
  3166. (> point 1))
  3167. (progn
  3168. (goto-char (1- point))
  3169. (goto-char (prolog-clause-start)))
  3170. (goto-char new-point)
  3171. (skip-chars-forward " \t"))))
  3172. ;; (defun prolog-previous-clause ()
  3173. ;; "Move to the beginning of the previous clause."
  3174. ;; (interactive)
  3175. ;; (forward-char -1)
  3176. ;; (prolog-beginning-of-clause))
  3177. (defun prolog-end-of-clause ()
  3178. "Move to the end of clause.
  3179. If already at the end of clause, move to next clause."
  3180. (interactive)
  3181. (let ((point (point))
  3182. (new-point (prolog-clause-end)))
  3183. (if (and (<= new-point point)
  3184. (not (eq new-point (point-max))))
  3185. (progn
  3186. (goto-char (1+ point))
  3187. (goto-char (prolog-clause-end)))
  3188. (goto-char new-point))))
  3189. ;; (defun prolog-next-clause ()
  3190. ;; "Move to the beginning of the next clause."
  3191. ;; (interactive)
  3192. ;; (prolog-end-of-clause)
  3193. ;; (forward-char)
  3194. ;; (prolog-end-of-clause)
  3195. ;; (prolog-beginning-of-clause))
  3196. (defun prolog-beginning-of-predicate ()
  3197. "Go to the nearest beginning of predicate before current point.
  3198. Return the final point or nil if no such a beginning was found."
  3199. (interactive)
  3200. (let ((op (point))
  3201. (pos (prolog-pred-start)))
  3202. (if pos
  3203. (if (= op pos)
  3204. (if (not (bobp))
  3205. (progn
  3206. (goto-char pos)
  3207. (backward-char 1)
  3208. (setq pos (prolog-pred-start))
  3209. (if pos
  3210. (progn
  3211. (goto-char pos)
  3212. (point)))))
  3213. (goto-char pos)
  3214. (point)))))
  3215. (defun prolog-end-of-predicate ()
  3216. "Go to the end of the current predicate."
  3217. (interactive)
  3218. (let ((op (point)))
  3219. (goto-char (prolog-pred-end))
  3220. (if (= op (point))
  3221. (progn
  3222. (forward-line 1)
  3223. (prolog-end-of-predicate)))))
  3224. (defun prolog-insert-predspec ()
  3225. "Insert the predspec for the current predicate."
  3226. (interactive)
  3227. (let* ((pinfo (prolog-clause-info))
  3228. (predname (nth 0 pinfo))
  3229. (arity (nth 1 pinfo)))
  3230. (insert (format "%s/%d" predname arity))))
  3231. (defun prolog-view-predspec ()
  3232. "Insert the predspec for the current predicate."
  3233. (interactive)
  3234. (let* ((pinfo (prolog-clause-info))
  3235. (predname (nth 0 pinfo))
  3236. (arity (nth 1 pinfo)))
  3237. (message (format "%s/%d" predname arity))))
  3238. (defun prolog-insert-predicate-template ()
  3239. "Insert the template for the current clause."
  3240. (interactive)
  3241. (let* ((n 1)
  3242. oldp
  3243. (pinfo (prolog-clause-info))
  3244. (predname (nth 0 pinfo))
  3245. (arity (nth 1 pinfo)))
  3246. (insert predname)
  3247. (if (> arity 0)
  3248. (progn
  3249. (insert "(")
  3250. (when prolog-electric-dot-full-predicate-template
  3251. (setq oldp (point))
  3252. (while (< n arity)
  3253. (insert ",")
  3254. (setq n (1+ n)))
  3255. (insert ")")
  3256. (goto-char oldp))
  3257. ))
  3258. ))
  3259. (defun prolog-insert-next-clause ()
  3260. "Insert newline and the name of the current clause."
  3261. (interactive)
  3262. (insert "\n")
  3263. (prolog-insert-predicate-template))
  3264. (defun prolog-insert-module-modeline ()
  3265. "Insert a modeline for module specification.
  3266. This line should be first in the buffer.
  3267. The module name should be written manually just before the semi-colon."
  3268. (interactive)
  3269. (insert "%%% -*- Module: ; -*-\n")
  3270. (backward-char 6))
  3271. (defalias 'prolog-uncomment-region
  3272. (if (fboundp 'uncomment-region) #'uncomment-region
  3273. (lambda (beg end)
  3274. "Uncomment the region between BEG and END."
  3275. (interactive "r")
  3276. (comment-region beg end -1))))
  3277. (defun prolog-goto-comment-column (&optional nocreate)
  3278. "Move comments on the current line to the correct position.
  3279. If NOCREATE is nil (or omitted) and there is no comment on the line, then
  3280. a new comment is created."
  3281. (interactive)
  3282. (beginning-of-line)
  3283. (if (or (not nocreate)
  3284. (and
  3285. (re-search-forward
  3286. (format "^\\(\\(%s\\|%s\\|[^\n\'\"%%]\\)*\\)%% *"
  3287. prolog-quoted-atom-regexp prolog-string-regexp)
  3288. (line-end-position) 'limit)
  3289. (progn
  3290. (goto-char (match-beginning 0))
  3291. (not (eq (prolog-in-string-or-comment) 'txt)))))
  3292. (indent-for-comment)))
  3293. (defun prolog-indent-predicate ()
  3294. "*Indent the current predicate."
  3295. (interactive)
  3296. (indent-region (prolog-pred-start) (prolog-pred-end) nil))
  3297. (defun prolog-indent-buffer ()
  3298. "*Indent the entire buffer."
  3299. (interactive)
  3300. (indent-region (point-min) (point-max) nil))
  3301. (defun prolog-mark-clause ()
  3302. "Put mark at the end of this clause and move point to the beginning."
  3303. (interactive)
  3304. (let ((pos (point)))
  3305. (goto-char (prolog-clause-end))
  3306. (forward-line 1)
  3307. (beginning-of-line)
  3308. (set-mark (point))
  3309. (goto-char pos)
  3310. (goto-char (prolog-clause-start))))
  3311. (defun prolog-mark-predicate ()
  3312. "Put mark at the end of this predicate and move point to the beginning."
  3313. (interactive)
  3314. (goto-char (prolog-pred-end))
  3315. (let ((pos (point)))
  3316. (forward-line 1)
  3317. (beginning-of-line)
  3318. (set-mark (point))
  3319. (goto-char pos)
  3320. (goto-char (prolog-pred-start))))
  3321. ;; Stolen from `cc-mode.el':
  3322. (defun prolog-electric-delete (arg)
  3323. "Delete preceding character or whitespace.
  3324. If `prolog-hungry-delete-key-flag' is non-nil, then all preceding whitespace is
  3325. consumed. If however an ARG is supplied, or `prolog-hungry-delete-key-flag' is
  3326. nil, or point is inside a literal then the function in the variable
  3327. `backward-delete-char' is called."
  3328. (interactive "P")
  3329. (if (or (not prolog-hungry-delete-key-flag)
  3330. arg
  3331. (prolog-in-string-or-comment))
  3332. (funcall 'backward-delete-char (prefix-numeric-value arg))
  3333. (let ((here (point)))
  3334. (skip-chars-backward " \t\n")
  3335. (if (/= (point) here)
  3336. (delete-region (point) here)
  3337. (funcall 'backward-delete-char 1)
  3338. ))))
  3339. ;; For XEmacs compatibility (suggested by Per Mildner)
  3340. (put 'prolog-electric-delete 'pending-delete 'supersede)
  3341. (defun prolog-electric-if-then-else (arg)
  3342. "If `prolog-electric-if-then-else-flag' is non-nil, indent if-then-else constructs.
  3343. Bound to the >, ; and ( keys."
  3344. (interactive "P")
  3345. (self-insert-command (prefix-numeric-value arg))
  3346. (if prolog-electric-if-then-else-flag (prolog-insert-spaces-after-paren)))
  3347. (defun prolog-electric-colon (arg)
  3348. "If `prolog-electric-colon-flag' is non-nil, insert the electric `:' construct.
  3349. That is, insert space (if appropriate), `:-' and newline if colon is pressed
  3350. at the end of a line that starts in the first column (i.e., clause
  3351. heads)."
  3352. (interactive "P")
  3353. (if (and prolog-electric-colon-flag
  3354. (null arg)
  3355. (eolp)
  3356. ;(not (string-match "^\\s " (thing-at-point 'line))))
  3357. (not (string-match "^\\(\\s \\|%\\)" (thing-at-point 'line))))
  3358. (progn
  3359. (unless (save-excursion (backward-char 1) (looking-at "\\s "))
  3360. (insert " "))
  3361. (insert ":-\n")
  3362. (prolog-indent-line))
  3363. (self-insert-command (prefix-numeric-value arg))))
  3364. (defun prolog-electric-dash (arg)
  3365. "If `prolog-electric-dash-flag' is non-nil, insert the electric `-' construct.
  3366. that is, insert space (if appropriate), `-->' and newline if dash is pressed
  3367. at the end of a line that starts in the first column (i.e., DCG
  3368. heads)."
  3369. (interactive "P")
  3370. (if (and prolog-electric-dash-flag
  3371. (null arg)
  3372. (eolp)
  3373. ;(not (string-match "^\\s " (thing-at-point 'line))))
  3374. (not (string-match "^\\(\\s \\|%\\)" (thing-at-point 'line))))
  3375. (progn
  3376. (unless (save-excursion (backward-char 1) (looking-at "\\s "))
  3377. (insert " "))
  3378. (insert "-->\n")
  3379. (prolog-indent-line))
  3380. (self-insert-command (prefix-numeric-value arg))))
  3381. (defun prolog-electric-dot (arg)
  3382. "Insert dot and newline or a head of a new clause.
  3383. If `prolog-electric-dot-flag' is nil, then simply insert dot.
  3384. Otherwise::
  3385. When invoked at the end of nonempty line, insert dot and newline.
  3386. When invoked at the end of an empty line, insert a recursive call to
  3387. the current predicate.
  3388. When invoked at the beginning of line, insert a head of a new clause
  3389. of the current predicate.
  3390. When called with prefix argument ARG, insert just dot."
  3391. (interactive "P")
  3392. ;; Check for situations when the electricity should not be active
  3393. (if (or (not prolog-electric-dot-flag)
  3394. arg
  3395. (prolog-in-string-or-comment)
  3396. ;; Do not be electric in a floating point number or an operator
  3397. (not
  3398. (or
  3399. ;; (re-search-backward
  3400. ;; ######
  3401. ;; "\\(^\\|[])}a-zA-Z_!'0-9]+\\)[ \t]*\\=" nil t)))
  3402. (save-excursion
  3403. (re-search-backward
  3404. ;; "\\(^\\|[])}_!'0-9]+\\)[ \t]*\\=" nil t)))
  3405. "\\(^\\|[])}_!'0-9]+\\)[ \t]*\\="
  3406. nil t))
  3407. (save-excursion
  3408. (re-search-backward
  3409. ;; "\\(^\\|[])}a-zA-Z]+\\)[ \t]*\\=" nil t)))
  3410. (format "\\(^\\|[])}%s]+\\)[ \t]*\\="
  3411. prolog-lower-case-string) ;FIXME: [:lower:]
  3412. nil t))
  3413. (save-excursion
  3414. (re-search-backward
  3415. ;; "\\(^\\|[])}a-zA-Z]+\\)[ \t]*\\=" nil t)))
  3416. (format "\\(^\\|[])}%s]+\\)[ \t]*\\="
  3417. prolog-upper-case-string) ;FIXME: [:upper:]
  3418. nil t))
  3419. )
  3420. )
  3421. ;; Do not be electric if inside a parenthesis pair.
  3422. (not (= (prolog-region-paren-balance (prolog-clause-start) (point))
  3423. 0))
  3424. )
  3425. (funcall 'self-insert-command (prefix-numeric-value arg))
  3426. (cond
  3427. ;; Beginning of line
  3428. ((bolp)
  3429. (prolog-insert-predicate-template))
  3430. ;; At an empty line with at least one whitespace
  3431. ((save-excursion
  3432. (beginning-of-line)
  3433. (looking-at "[ \t]+$"))
  3434. (prolog-insert-predicate-template)
  3435. (when prolog-electric-dot-full-predicate-template
  3436. (save-excursion
  3437. (end-of-line)
  3438. (insert ".\n"))))
  3439. ;; Default
  3440. (t
  3441. (insert ".\n"))
  3442. )))
  3443. (defun prolog-electric-underscore ()
  3444. "Replace variable with an underscore.
  3445. If `prolog-electric-underscore-flag' is non-nil and the point is
  3446. on a variable then replace the variable with underscore and skip
  3447. the following comma and whitespace, if any.
  3448. If the point is not on a variable then insert underscore."
  3449. (interactive)
  3450. (if prolog-electric-underscore-flag
  3451. (let (;start
  3452. (case-fold-search nil)
  3453. (oldp (point)))
  3454. ;; ######
  3455. ;;(skip-chars-backward "a-zA-Z_")
  3456. (skip-chars-backward
  3457. (format "%s%s_"
  3458. ;; FIXME: Why not "a-zA-Z"?
  3459. prolog-lower-case-string
  3460. prolog-upper-case-string))
  3461. ;(setq start (point))
  3462. (if (and (not (prolog-in-string-or-comment))
  3463. ;; ######
  3464. ;; (looking-at "\\<[_A-Z][a-zA-Z_0-9]*\\>"))
  3465. (looking-at (format "\\<[_%s][%s%s_0-9]*\\>"
  3466. ;; FIXME: Use [:upper:] and friends.
  3467. prolog-upper-case-string
  3468. prolog-lower-case-string
  3469. prolog-upper-case-string)))
  3470. (progn
  3471. (replace-match "_")
  3472. (skip-chars-forward ", \t\n"))
  3473. (goto-char oldp)
  3474. (self-insert-command 1))
  3475. )
  3476. (self-insert-command 1))
  3477. )
  3478. (defun prolog-find-term (functor arity &optional prefix)
  3479. "Go to the position at the start of the next occurrence of a term.
  3480. The term is specified with FUNCTOR and ARITY. The optional argument
  3481. PREFIX is the prefix of the search regexp."
  3482. (let* (;; If prefix is not set then use the default "\\<"
  3483. (prefix (if (not prefix)
  3484. "\\<"
  3485. prefix))
  3486. (regexp (concat prefix functor))
  3487. (i 1))
  3488. ;; Build regexp for the search if the arity is > 0
  3489. (if (= arity 0)
  3490. ;; Add that the functor must be at the end of a word. This
  3491. ;; does not work if the arity is > 0 since the closing )
  3492. ;; is not a word constituent.
  3493. (setq regexp (concat regexp "\\>"))
  3494. ;; Arity is > 0, add parens and commas
  3495. (setq regexp (concat regexp "("))
  3496. (while (< i arity)
  3497. (setq regexp (concat regexp ".+,"))
  3498. (setq i (1+ i)))
  3499. (setq regexp (concat regexp ".+)")))
  3500. ;; Search, and return position
  3501. (if (re-search-forward regexp nil t)
  3502. (goto-char (match-beginning 0))
  3503. (error "Term not found"))
  3504. ))
  3505. (defun prolog-variables-to-anonymous (beg end)
  3506. "Replace all variables within a region BEG to END by anonymous variables."
  3507. (interactive "r")
  3508. (save-excursion
  3509. (let ((case-fold-search nil))
  3510. (goto-char end)
  3511. (while (re-search-backward "\\<[A-Z_][a-zA-Z_0-9]*\\>" beg t)
  3512. (progn
  3513. (replace-match "_")
  3514. (backward-char)))
  3515. )))
  3516. (defun prolog-set-atom-regexps ()
  3517. "Set the `prolog-atom-char-regexp' and `prolog-atom-regexp' variables.
  3518. Must be called after `prolog-build-case-strings'."
  3519. (setq prolog-atom-char-regexp
  3520. (format "[%s%s0-9_$]"
  3521. ;; FIXME: why not a-zA-Z?
  3522. prolog-lower-case-string
  3523. prolog-upper-case-string))
  3524. (setq prolog-atom-regexp
  3525. (format "[%s$]%s*"
  3526. prolog-lower-case-string
  3527. prolog-atom-char-regexp))
  3528. )
  3529. (defun prolog-build-case-strings ()
  3530. "Set `prolog-upper-case-string' and `prolog-lower-case-string'.
  3531. Uses the current case-table for extracting the relevant information."
  3532. (let ((up_string "")
  3533. (low_string ""))
  3534. ;; Use `map-char-table' if it is defined. Otherwise enumerate all
  3535. ;; numbers between 0 and 255. `map-char-table' is probably safer.
  3536. ;;
  3537. ;; `map-char-table' causes problems under Emacs 23.0.0.1, the
  3538. ;; while loop seems to do its job well (Ryszard Szopa)
  3539. ;;
  3540. ;;(if (and (not (featurep 'xemacs))
  3541. ;; (fboundp 'map-char-table))
  3542. ;; (map-char-table
  3543. ;; (lambda (key value)
  3544. ;; (cond
  3545. ;; ((and
  3546. ;; (eq (prolog-int-to-char key) (downcase key))
  3547. ;; (eq (prolog-int-to-char key) (upcase key)))
  3548. ;; ;; Do nothing if upper and lower case are the same
  3549. ;; )
  3550. ;; ((eq (prolog-int-to-char key) (downcase key))
  3551. ;; ;; The char is lower case
  3552. ;; (setq low_string (format "%s%c" low_string key)))
  3553. ;; ((eq (prolog-int-to-char key) (upcase key))
  3554. ;; ;; The char is upper case
  3555. ;; (setq up_string (format "%s%c" up_string key)))
  3556. ;; ))
  3557. ;; (current-case-table))
  3558. ;; `map-char-table' was undefined.
  3559. (let ((key 0))
  3560. (while (< key 256)
  3561. (cond
  3562. ((and
  3563. (eq (prolog-int-to-char key) (downcase key))
  3564. (eq (prolog-int-to-char key) (upcase key)))
  3565. ;; Do nothing if upper and lower case are the same
  3566. )
  3567. ((eq (prolog-int-to-char key) (downcase key))
  3568. ;; The char is lower case
  3569. (setq low_string (format "%s%c" low_string key)))
  3570. ((eq (prolog-int-to-char key) (upcase key))
  3571. ;; The char is upper case
  3572. (setq up_string (format "%s%c" up_string key)))
  3573. )
  3574. (setq key (1+ key))))
  3575. ;; )
  3576. ;; The strings are single-byte strings
  3577. (setq prolog-upper-case-string (prolog-dash-letters up_string))
  3578. (setq prolog-lower-case-string (prolog-dash-letters low_string))
  3579. ))
  3580. ;(defun prolog-regexp-dash-continuous-chars (chars)
  3581. ; (let ((ints (mapcar #'prolog-char-to-int (string-to-list chars)))
  3582. ; (beg 0)
  3583. ; (end 0))
  3584. ; (if (null ints)
  3585. ; chars
  3586. ; (while (and (< (+ beg 1) (length chars))
  3587. ; (not (or (= (+ (nth beg ints) 1) (nth (+ beg 1) ints))
  3588. ; (= (nth beg ints) (nth (+ beg 1) ints)))))
  3589. ; (setq beg (+ beg 1)))
  3590. ; (setq beg (+ beg 1)
  3591. ; end beg)
  3592. ; (while (and (< (+ end 1) (length chars))
  3593. ; (or (= (+ (nth end ints) 1) (nth (+ end 1) ints))
  3594. ; (= (nth end ints) (nth (+ end 1) ints))))
  3595. ; (setq end (+ end 1)))
  3596. ; (if (equal (substring chars end) "")
  3597. ; (substring chars 0 beg)
  3598. ; (concat (substring chars 0 beg) "-"
  3599. ; (prolog-regexp-dash-continuous-chars (substring chars end))))
  3600. ; )))
  3601. (defun prolog-ints-intervals (ints)
  3602. "Return a list of intervals (from . to) covering INTS."
  3603. (when ints
  3604. (setq ints (sort ints '<))
  3605. (let ((prev (car ints))
  3606. (interval-start (car ints))
  3607. intervals)
  3608. (while ints
  3609. (let ((next (car ints)))
  3610. (when (> next (1+ prev)) ; start of new interval
  3611. (setq intervals (cons (cons interval-start prev) intervals))
  3612. (setq interval-start next))
  3613. (setq prev next)
  3614. (setq ints (cdr ints))))
  3615. (setq intervals (cons (cons interval-start prev) intervals))
  3616. (reverse intervals))))
  3617. (defun prolog-dash-letters (string)
  3618. "Return a condensed regexp covering all letters in STRING."
  3619. (let ((intervals (prolog-ints-intervals (mapcar #'prolog-char-to-int
  3620. (string-to-list string))))
  3621. codes)
  3622. (while intervals
  3623. (let* ((i (car intervals))
  3624. (from (car i))
  3625. (to (cdr i))
  3626. (c (cond ((= from to) `(,from))
  3627. ((= (1+ from) to) `(,from ,to))
  3628. (t `(,from ?- ,to)))))
  3629. (setq codes (cons c codes)))
  3630. (setq intervals (cdr intervals)))
  3631. (apply 'concat (reverse codes))))
  3632. ;(defun prolog-condense-character-sets (regexp)
  3633. ; "Condense adjacent characters in character sets of REGEXP."
  3634. ; (let ((next -1))
  3635. ; (while (setq next (string-match "\\[\\(.*?\\)\\]" regexp (1+ next)))
  3636. ; (setq regexp (replace-match (prolog-dash-letters (match-string 1 regexp))
  3637. ; t t regexp 1))))
  3638. ; regexp)
  3639. ;; GNU Emacs compatibility: GNU Emacs does not differentiate between
  3640. ;; ints and chars, or at least these two are interchangeable.
  3641. (defalias 'prolog-int-to-char
  3642. (if (fboundp 'int-to-char) #'int-to-char #'identity))
  3643. (defalias 'prolog-char-to-int
  3644. (if (fboundp 'char-to-int) #'char-to-int #'identity))
  3645. ;;-------------------------------------------------------------------
  3646. ;; Menu stuff (both for the editing buffer and for the inferior
  3647. ;; prolog buffer)
  3648. ;;-------------------------------------------------------------------
  3649. (unless (fboundp 'region-exists-p)
  3650. (defun region-exists-p ()
  3651. "Non-nil iff the mark is set. Lobotomized version for Emacsen that do not provide their own."
  3652. (mark)))
  3653. ;; GNU Emacs ignores `easy-menu-add' so the order in which the menus
  3654. ;; are defined _is_ important!
  3655. (easy-menu-define
  3656. prolog-menu-help (list prolog-mode-map prolog-inferior-mode-map)
  3657. "Help menu for the Prolog mode."
  3658. ;; FIXME: Does it really deserve a whole menu to itself?
  3659. `(,(if (featurep 'xemacs) "Help"
  3660. ;; Not sure it's worth the trouble. --Stef
  3661. ;; (add-to-list 'menu-bar-final-items
  3662. ;; (easy-menu-intern "Prolog-Help"))
  3663. "Prolog-help")
  3664. ["On predicate" prolog-help-on-predicate prolog-help-function-i]
  3665. ["Apropos" prolog-help-apropos (eq prolog-system 'swi)]
  3666. "---"
  3667. ["Describe mode" describe-mode t]))
  3668. (easy-menu-define
  3669. prolog-edit-menu-runtime prolog-mode-map
  3670. "Runtime Prolog commands available from the editing buffer"
  3671. ;; FIXME: Don't use a whole menu for just "Run Mercury". --Stef
  3672. `("System"
  3673. ;; Runtime menu name.
  3674. ,@(unless (featurep 'xemacs)
  3675. '(:label (cond ((eq prolog-system 'eclipse) "ECLiPSe")
  3676. ((eq prolog-system 'mercury) "Mercury")
  3677. (t "System"))))
  3678. ;; Consult items, NIL for mercury.
  3679. ["Consult file" prolog-consult-file
  3680. :included (not (eq prolog-system 'mercury))]
  3681. ["Consult buffer" prolog-consult-buffer
  3682. :included (not (eq prolog-system 'mercury))]
  3683. ["Consult region" prolog-consult-region :active (region-exists-p)
  3684. :included (not (eq prolog-system 'mercury))]
  3685. ["Consult predicate" prolog-consult-predicate
  3686. :included (not (eq prolog-system 'mercury))]
  3687. ;; Compile items, NIL for everything but SICSTUS.
  3688. ,(if (featurep 'xemacs) "---"
  3689. ["---" nil :included (eq prolog-system 'sicstus)])
  3690. ["Compile file" prolog-compile-file
  3691. :included (eq prolog-system 'sicstus)]
  3692. ["Compile buffer" prolog-compile-buffer
  3693. :included (eq prolog-system 'sicstus)]
  3694. ["Compile region" prolog-compile-region :active (region-exists-p)
  3695. :included (eq prolog-system 'sicstus)]
  3696. ["Compile predicate" prolog-compile-predicate
  3697. :included (eq prolog-system 'sicstus)]
  3698. ;; Debug items, NIL for Mercury.
  3699. ,(if (featurep 'xemacs) "---"
  3700. ["---" nil :included (not (eq prolog-system 'mercury))])
  3701. ;; FIXME: Could we use toggle or radio buttons? --Stef
  3702. ["Debug" prolog-debug-on :included (not (eq prolog-system 'mercury))]
  3703. ["Debug off" prolog-debug-off
  3704. ;; In SICStus, these are pairwise disjunctive,
  3705. ;; so it's enough with a single "off"-command
  3706. :included (not (memq prolog-system '(mercury sicstus)))]
  3707. ["Trace" prolog-trace-on :included (not (eq prolog-system 'mercury))]
  3708. ["Trace off" prolog-trace-off
  3709. :included (not (memq prolog-system '(mercury sicstus)))]
  3710. ["Zip" prolog-zip-on :included (and (eq prolog-system 'sicstus)
  3711. (prolog-atleast-version '(3 . 7)))]
  3712. ["All debug off" prolog-debug-off
  3713. :included (eq prolog-system 'sicstus)]
  3714. ["Source level debugging"
  3715. prolog-toggle-sicstus-sd
  3716. :included (and (eq prolog-system 'sicstus)
  3717. (prolog-atleast-version '(3 . 7)))
  3718. :style toggle
  3719. :selected prolog-use-sicstus-sd]
  3720. "---"
  3721. ["Run" run-prolog
  3722. :suffix (cond ((eq prolog-system 'eclipse) "ECLiPSe")
  3723. ((eq prolog-system 'mercury) "Mercury")
  3724. (t "Prolog"))]))
  3725. (easy-menu-define
  3726. prolog-edit-menu-insert-move prolog-mode-map
  3727. "Commands for Prolog code manipulation."
  3728. '("Prolog"
  3729. ["Comment region" comment-region (region-exists-p)]
  3730. ["Uncomment region" prolog-uncomment-region (region-exists-p)]
  3731. ["Add comment/move to comment" indent-for-comment t]
  3732. ["Convert variables in region to '_'" prolog-variables-to-anonymous
  3733. :active (region-exists-p) :included (not (eq prolog-system 'mercury))]
  3734. "---"
  3735. ["Insert predicate template" prolog-insert-predicate-template t]
  3736. ["Insert next clause head" prolog-insert-next-clause t]
  3737. ["Insert predicate spec" prolog-insert-predspec t]
  3738. ["Insert module modeline" prolog-insert-module-modeline t]
  3739. "---"
  3740. ["Beginning of clause" prolog-beginning-of-clause t]
  3741. ["End of clause" prolog-end-of-clause t]
  3742. ["Beginning of predicate" prolog-beginning-of-predicate t]
  3743. ["End of predicate" prolog-end-of-predicate t]
  3744. "---"
  3745. ["Indent line" prolog-indent-line t]
  3746. ["Indent region" indent-region (region-exists-p)]
  3747. ["Indent predicate" prolog-indent-predicate t]
  3748. ["Indent buffer" prolog-indent-buffer t]
  3749. ["Align region" align (region-exists-p)]
  3750. "---"
  3751. ["Mark clause" prolog-mark-clause t]
  3752. ["Mark predicate" prolog-mark-predicate t]
  3753. ["Mark paragraph" mark-paragraph t]
  3754. ;;"---"
  3755. ;;["Fontify buffer" font-lock-fontify-buffer t]
  3756. ))
  3757. (defun prolog-menu ()
  3758. "Add the menus for the Prolog editing buffers."
  3759. (easy-menu-add prolog-edit-menu-insert-move)
  3760. (easy-menu-add prolog-edit-menu-runtime)
  3761. ;; Add predicate index menu
  3762. (set (make-local-variable 'imenu-create-index-function)
  3763. 'imenu-default-create-index-function)
  3764. ;;Milan (this has problems with object methods...) ###### Does it? (Stefan)
  3765. (setq imenu-prev-index-position-function 'prolog-beginning-of-predicate)
  3766. (setq imenu-extract-index-name-function 'prolog-get-predspec)
  3767. (if (and prolog-imenu-flag
  3768. (< (count-lines (point-min) (point-max)) prolog-imenu-max-lines))
  3769. (imenu-add-to-menubar "Predicates"))
  3770. (easy-menu-add prolog-menu-help))
  3771. (easy-menu-define
  3772. prolog-inferior-menu-all prolog-inferior-mode-map
  3773. "Menu for the inferior Prolog buffer."
  3774. `("Prolog"
  3775. ;; Runtime menu name.
  3776. ,@(unless (featurep 'xemacs)
  3777. '(:label (cond ((eq prolog-system 'eclipse) "ECLiPSe")
  3778. ((eq prolog-system 'mercury) "Mercury")
  3779. (t "Prolog"))))
  3780. ;; Debug items, NIL for Mercury.
  3781. ,(if (featurep 'xemacs) "---"
  3782. ["---" nil :included (not (eq prolog-system 'mercury))])
  3783. ;; FIXME: Could we use toggle or radio buttons? --Stef
  3784. ["Debug" prolog-debug-on :included (not (eq prolog-system 'mercury))]
  3785. ["Debug off" prolog-debug-off
  3786. ;; In SICStus, these are pairwise disjunctive,
  3787. ;; so it's enough with a single "off"-command
  3788. :included (not (memq prolog-system '(mercury sicstus)))]
  3789. ["Trace" prolog-trace-on :included (not (eq prolog-system 'mercury))]
  3790. ["Trace off" prolog-trace-off
  3791. :included (not (memq prolog-system '(mercury sicstus)))]
  3792. ["Zip" prolog-zip-on :included (and (eq prolog-system 'sicstus)
  3793. (prolog-atleast-version '(3 . 7)))]
  3794. ["All debug off" prolog-debug-off
  3795. :included (eq prolog-system 'sicstus)]
  3796. ["Source level debugging"
  3797. prolog-toggle-sicstus-sd
  3798. :included (and (eq prolog-system 'sicstus)
  3799. (prolog-atleast-version '(3 . 7)))
  3800. :style toggle
  3801. :selected prolog-use-sicstus-sd]
  3802. ;; Runtime.
  3803. "---"
  3804. ["Interrupt Prolog" comint-interrupt-subjob t]
  3805. ["Quit Prolog" comint-quit-subjob t]
  3806. ["Kill Prolog" comint-kill-subjob t]))
  3807. (defun prolog-inferior-menu ()
  3808. "Create the menus for the Prolog inferior buffer.
  3809. This menu is dynamically created because one may change systems during
  3810. the life of an Emacs session."
  3811. (easy-menu-add prolog-inferior-menu-all)
  3812. (easy-menu-add prolog-menu-help))
  3813. (defun prolog-mode-version ()
  3814. "Echo the current version of Prolog mode in the minibuffer."
  3815. (interactive)
  3816. (message "Using Prolog mode version %s" prolog-mode-version))
  3817. (provide 'prolog)
  3818. ;;; prolog.el ends here