ada-xref.el 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383
  1. ;; ada-xref.el --- for lookup and completion in Ada mode
  2. ;; Copyright (C) 1994-2012 Free Software Foundation, Inc.
  3. ;; Author: Markus Heritsch <Markus.Heritsch@studbox.uni-stuttgart.de>
  4. ;; Rolf Ebert <ebert@inf.enst.fr>
  5. ;; Emmanuel Briot <briot@gnat.com>
  6. ;; Maintainer: Stephen Leake <stephen_leake@stephe-leake.org>
  7. ;; Keywords: languages ada xref
  8. ;; Package: ada-mode
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;;; This Package provides a set of functions to use the output of the
  22. ;;; cross reference capabilities of the GNAT Ada compiler
  23. ;;; for lookup and completion in Ada mode.
  24. ;;;
  25. ;;; If a file *.`adp' exists in the ada-file directory, then it is
  26. ;;; read for configuration information. It is read only the first
  27. ;;; time a cross-reference is asked for, and is not read later.
  28. ;;; You need Emacs >= 20.2 to run this package
  29. ;;; History:
  30. ;;
  31. ;;; Code:
  32. ;; ----- Requirements -----------------------------------------------------
  33. (require 'compile)
  34. (require 'comint)
  35. (require 'find-file)
  36. (require 'ada-mode)
  37. ;; ------ User variables
  38. (defcustom ada-xref-other-buffer t
  39. "*If nil, always display the cross-references in the same buffer.
  40. Otherwise create either a new buffer or a new frame."
  41. :type 'boolean :group 'ada)
  42. (defcustom ada-xref-create-ali nil
  43. "*If non-nil, run gcc whenever the cross-references are not up-to-date.
  44. If nil, the cross-reference mode never runs gcc."
  45. :type 'boolean :group 'ada)
  46. (defcustom ada-xref-confirm-compile nil
  47. "*If non-nil, ask for confirmation before compiling or running the application."
  48. :type 'boolean :group 'ada)
  49. (defcustom ada-krunch-args "0"
  50. "*Maximum number of characters for filenames created by `gnatkr'.
  51. Set to 0, if you don't use crunched filenames. This should be a string."
  52. :type 'string :group 'ada)
  53. (defcustom ada-gnat-cmd "gnat"
  54. "Default GNAT project file parser.
  55. Will be run with args \"list -v -Pfile.gpr\".
  56. Default is standard GNAT distribution; alternate \"gnatpath\"
  57. is faster, available from Ada mode web site."
  58. :type 'string :group 'ada)
  59. (defcustom ada-gnatls-args '("-v")
  60. "*Arguments to pass to `gnatls' to find location of the runtime.
  61. Typical use is to pass `--RTS=soft-floats' on some systems that support it.
  62. You can also add `-I-' if you do not want the current directory to be included.
  63. Otherwise, going from specs to bodies and back will first look for files in the
  64. current directory. This only has an impact if you are not using project files,
  65. but only ADA_INCLUDE_PATH."
  66. :type '(repeat string) :group 'ada)
  67. (defcustom ada-prj-default-comp-opt "-gnatq -gnatQ"
  68. "Default compilation options."
  69. :type 'string :group 'ada)
  70. (defcustom ada-prj-default-bind-opt ""
  71. "Default binder options."
  72. :type 'string :group 'ada)
  73. (defcustom ada-prj-default-link-opt ""
  74. "Default linker options."
  75. :type 'string :group 'ada)
  76. (defcustom ada-prj-default-gnatmake-opt "-g"
  77. "Default options for `gnatmake'."
  78. :type 'string :group 'ada)
  79. (defcustom ada-prj-default-gpr-file ""
  80. "Default GNAT project file.
  81. If non-empty, this file is parsed to set the source and object directories for
  82. the Ada mode project."
  83. :type 'string :group 'ada)
  84. (defcustom ada-prj-ada-project-path-sep
  85. (cond ((boundp 'path-separator) path-separator) ; 20.3+
  86. ((memq system-type '(windows-nt ms-dos)) ";")
  87. (t ":"))
  88. "Default separator for ada_project_path project variable."
  89. :type 'string :group 'ada)
  90. (defcustom ada-prj-gnatfind-switches "-rf"
  91. "Default switches to use for `gnatfind'.
  92. You should modify this variable, for instance to add `-a', if you are working
  93. in an environment where most ALI files are write-protected.
  94. The command `gnatfind' is used every time you choose the menu
  95. \"Show all references\"."
  96. :type 'string :group 'ada)
  97. (defcustom ada-prj-default-check-cmd
  98. (concat "${cross_prefix}gnatmake -u -c -gnatc ${gnatmake_opt} ${full_current}"
  99. " -cargs ${comp_opt}")
  100. "*Default command to be used to compile a single file.
  101. Emacs will substitute the current filename for ${full_current}, or add
  102. the filename at the end. This is the same syntax as in the project file."
  103. :type 'string :group 'ada)
  104. (defcustom ada-prj-default-comp-cmd
  105. (concat "${cross_prefix}gnatmake -u -c ${gnatmake_opt} ${full_current} -cargs"
  106. " ${comp_opt}")
  107. "*Default command to be used to compile a single file.
  108. Emacs will substitute the current filename for ${full_current}, or add
  109. the filename at the end. This is the same syntax as in the project file."
  110. :type 'string :group 'ada)
  111. (defcustom ada-prj-default-debugger "${cross_prefix}gdb"
  112. "*Default name of the debugger."
  113. :type 'string :group 'ada)
  114. (defcustom ada-prj-default-make-cmd
  115. (concat "${cross_prefix}gnatmake -o ${main} ${main} ${gnatmake_opt} "
  116. "-cargs ${comp_opt} -bargs ${bind_opt} -largs ${link_opt}")
  117. "*Default command to be used to compile the application.
  118. This is the same syntax as in the project file."
  119. :type 'string :group 'ada)
  120. (defcustom ada-prj-default-project-file ""
  121. "*Name of the current project file.
  122. Emacs will not try to use the search algorithm to find the project file if
  123. this string is not empty. It is set whenever a project file is found."
  124. :type '(file :must-match t) :group 'ada)
  125. (defcustom ada-gnatstub-opts "-q -I${src_dir}"
  126. "*Options to pass to `gnatsub' to generate the body of a package.
  127. This has the same syntax as in the project file (with variable substitution)."
  128. :type 'string :group 'ada)
  129. (defcustom ada-always-ask-project nil
  130. "*If nil, use default values when no project file was found.
  131. Otherwise, ask the user for the name of the project file to use."
  132. :type 'boolean :group 'ada)
  133. (defconst ada-on-ms-windows (memq system-type '(windows-nt))
  134. "True if we are running on Windows.")
  135. (defcustom ada-tight-gvd-integration nil
  136. "*If non-nil, a new Emacs frame will be swallowed in GVD when debugging.
  137. If GVD is not the debugger used, nothing happens."
  138. :type 'boolean :group 'ada)
  139. (defcustom ada-xref-search-with-egrep t
  140. "*If non-nil, use egrep to find the possible declarations for an entity.
  141. This alternate method is used when the exact location was not found in the
  142. information provided by GNAT. However, it might be expensive if you have a lot
  143. of sources, since it will search in all the files in your project."
  144. :type 'boolean :group 'ada)
  145. (defvar ada-load-project-hook nil
  146. "Hook that is run when loading a project file.
  147. Each function in this hook takes one argument FILENAME, that is the name of
  148. the project file to load.
  149. This hook should be used to support new formats for the project files.
  150. If the function can load the file with the given filename, it should create a
  151. buffer that contains a conversion of the file to the standard format of the
  152. project files, and return that buffer. (The usual \"src_dir=\" or \"obj_dir=\"
  153. lines.) It should return nil if it doesn't know how to convert that project
  154. file.")
  155. ;; ------- Nothing to be modified by the user below this
  156. (defvar ada-last-prj-file ""
  157. "Name of the last project file entered by the user.")
  158. (defconst ada-prj-file-extension ".adp"
  159. "The extension used for project files.")
  160. (defvar ada-xref-runtime-library-specs-path '()
  161. "Directories where the specs for the standard library is found.
  162. This is used for cross-references.")
  163. (defvar ada-xref-runtime-library-ali-path '()
  164. "Directories where the ali for the standard library is found.
  165. This is used for cross-references.")
  166. (defvar ada-xref-pos-ring '()
  167. "List of positions selected by the cross-references functions.
  168. Used to go back to these positions.")
  169. (defvar ada-cd-command
  170. (if (string-match "cmdproxy.exe" shell-file-name)
  171. "cd /d"
  172. "cd")
  173. "Command to use to change to a specific directory.
  174. On Windows systems using `cmdproxy.exe' as the shell,
  175. we need to use `/d' or the drive is never changed.")
  176. (defvar ada-command-separator (if ada-on-ms-windows " && " "\n")
  177. "Separator to use between multiple commands to `compile' or `start-process'.
  178. `cmdproxy.exe' doesn't recognize multiple-line commands, so we have to use
  179. \"&&\" for now.")
  180. (defconst ada-xref-pos-ring-max 16
  181. "Number of positions kept in the list `ada-xref-pos-ring'.")
  182. (defvar ada-operator-re
  183. "\\+\\|-\\|/\\|\\*\\*\\|\\*\\|=\\|&\\|abs\\|mod\\|rem\\|and\\|not\\|or\\|xor\\|<=\\|<\\|>=\\|>"
  184. "Regexp to match for operators.")
  185. (defvar ada-xref-project-files '()
  186. "Associative list of project files with properties.
  187. It has the format: (project project ...)
  188. A project has the format: (project-file . project-plist)
  189. \(See 'apropos plist' for operations on property lists).
  190. See `ada-default-prj-properties' for the list of valid properties.
  191. The current project is retrieved with `ada-xref-current-project'.
  192. Properties are retrieved with `ada-xref-get-project-field', set with
  193. `ada-xref-set-project-field'. If project properties are accessed with no
  194. project file, a (nil . default-properties) entry is created.")
  195. ;; ----- Identlist manipulation -------------------------------------------
  196. ;; An identlist is a vector that is used internally to reference an identifier
  197. ;; To facilitate its use, we provide the following macros
  198. (defmacro ada-make-identlist () (make-vector 8 nil))
  199. (defmacro ada-name-of (identlist) (list 'aref identlist 0))
  200. (defmacro ada-line-of (identlist) (list 'aref identlist 1))
  201. (defmacro ada-column-of (identlist) (list 'aref identlist 2))
  202. (defmacro ada-file-of (identlist) (list 'aref identlist 3))
  203. (defmacro ada-ali-index-of (identlist) (list 'aref identlist 4))
  204. (defmacro ada-declare-file-of (identlist) (list 'aref identlist 5))
  205. (defmacro ada-references-of (identlist) (list 'aref identlist 6))
  206. (defmacro ada-on-declaration (identlist) (list 'aref identlist 7))
  207. (defmacro ada-set-name (identlist name) (list 'aset identlist 0 name))
  208. (defmacro ada-set-line (identlist line) (list 'aset identlist 1 line))
  209. (defmacro ada-set-column (identlist col) (list 'aset identlist 2 col))
  210. (defmacro ada-set-file (identlist file) (list 'aset identlist 3 file))
  211. (defmacro ada-set-ali-index (identlist index) (list 'aset identlist 4 index))
  212. (defmacro ada-set-declare-file (identlist file) (list 'aset identlist 5 file))
  213. (defmacro ada-set-references (identlist ref) (list 'aset identlist 6 ref))
  214. (defmacro ada-set-on-declaration (ident value) (list 'aset ident 7 value))
  215. (defsubst ada-get-ali-buffer (file)
  216. "Read the ali file FILE into a new buffer, and return the buffer's name."
  217. (find-file-noselect (ada-get-ali-file-name file)))
  218. ;; -----------------------------------------------------------------------
  219. (defun ada-quote-cmd (cmd)
  220. "Duplicate all `\\' characters in CMD so that it can be passed to `compile'."
  221. (mapconcat 'identity (split-string cmd "\\\\") "\\\\"))
  222. (defun ada-find-executable (exec-name)
  223. "Find the full path to the executable file EXEC-NAME.
  224. If not found, throw an error.
  225. On Windows systems, this will properly handle .exe extension as well."
  226. (let ((result (or (ada-find-file-in-dir exec-name exec-path)
  227. (ada-find-file-in-dir (concat exec-name ".exe") exec-path))))
  228. (if result
  229. result
  230. (error "'%s' not found in path" exec-name))))
  231. (defun ada-initialize-runtime-library (cross-prefix)
  232. "Initialize the variables for the runtime library location.
  233. CROSS-PREFIX is the prefix to use for the `gnatls' command."
  234. (let ((gnatls
  235. (condition-case nil
  236. ;; if gnatls not found, just give up (may not be using GNAT)
  237. (ada-find-executable (concat cross-prefix "gnatls"))
  238. (error nil))))
  239. (if gnatls
  240. (save-excursion
  241. (setq ada-xref-runtime-library-specs-path '()
  242. ada-xref-runtime-library-ali-path '())
  243. (set-buffer (get-buffer-create "*gnatls*"))
  244. (widen)
  245. (erase-buffer)
  246. ;; Even if we get an error, delete the *gnatls* buffer
  247. (unwind-protect
  248. (let ((status (apply 'call-process gnatls (append '(nil t nil) ada-gnatls-args))))
  249. (goto-char (point-min))
  250. ;; Since we didn't provide all the inputs gnatls expects, it returns status 4
  251. (if (/= 4 status)
  252. (error (buffer-substring (point) (line-end-position))))
  253. ;; Source path
  254. (search-forward "Source Search Path:")
  255. (forward-line 1)
  256. (while (not (looking-at "^$"))
  257. (back-to-indentation)
  258. (if (looking-at "<Current_Directory>")
  259. (add-to-list 'ada-xref-runtime-library-specs-path ".")
  260. (add-to-list 'ada-xref-runtime-library-specs-path
  261. (buffer-substring-no-properties
  262. (point)
  263. (point-at-eol))))
  264. (forward-line 1))
  265. ;; Object path
  266. (search-forward "Object Search Path:")
  267. (forward-line 1)
  268. (while (not (looking-at "^$"))
  269. (back-to-indentation)
  270. (if (looking-at "<Current_Directory>")
  271. (add-to-list 'ada-xref-runtime-library-ali-path ".")
  272. (add-to-list 'ada-xref-runtime-library-ali-path
  273. (buffer-substring-no-properties
  274. (point)
  275. (point-at-eol))))
  276. (forward-line 1))
  277. )
  278. (kill-buffer nil))))
  279. (set 'ada-xref-runtime-library-specs-path
  280. (reverse ada-xref-runtime-library-specs-path))
  281. (set 'ada-xref-runtime-library-ali-path
  282. (reverse ada-xref-runtime-library-ali-path))
  283. ))
  284. (defun ada-gnat-parse-gpr (plist gpr-file)
  285. "Set gpr_file, src_dir and obj_dir properties in PLIST by parsing GPR-FILE.
  286. Return new value of PLIST.
  287. GPR_FILE must be full path to file, normalized.
  288. src_dir, obj_dir will include compiler runtime.
  289. Assumes environment variable ADA_PROJECT_PATH is set properly."
  290. (with-current-buffer (get-buffer-create "*gnatls*")
  291. (erase-buffer)
  292. ;; this can take a long time; let the user know what's up
  293. (message "Parsing %s ..." gpr-file)
  294. ;; Even if we get an error, delete the *gnatls* buffer
  295. (unwind-protect
  296. (let* ((cross-prefix (plist-get plist 'cross_prefix))
  297. (gnat (concat cross-prefix ada-gnat-cmd))
  298. ;; Putting quotes around gpr-file confuses gnatpath on Lynx; not clear why
  299. (gpr-opt (concat "-P" gpr-file))
  300. (src-dir '())
  301. (obj-dir '())
  302. (status (call-process gnat nil t nil "list" "-v" gpr-opt)))
  303. (goto-char (point-min))
  304. (if (/= 0 status)
  305. (error (buffer-substring (point) (line-end-position))))
  306. ;; Source path
  307. (search-forward "Source Search Path:")
  308. (forward-line 1) ; first directory in list
  309. (while (not (looking-at "^$")) ; terminate on blank line
  310. (back-to-indentation) ; skip whitespace
  311. (add-to-list 'src-dir
  312. (if (looking-at "<Current_Directory>")
  313. default-directory
  314. (expand-file-name
  315. (buffer-substring-no-properties
  316. (point) (line-end-position)))))
  317. (forward-line 1))
  318. ;; Object path
  319. (search-forward "Object Search Path:")
  320. (forward-line 1)
  321. (while (not (looking-at "^$"))
  322. (back-to-indentation)
  323. (add-to-list 'obj-dir
  324. (if (looking-at "<Current_Directory>")
  325. default-directory
  326. (expand-file-name
  327. (buffer-substring-no-properties
  328. (point) (line-end-position)))))
  329. (forward-line 1))
  330. ;; Set properties
  331. (setq plist (plist-put plist 'gpr_file gpr-file))
  332. (setq plist (plist-put plist 'src_dir src-dir))
  333. (plist-put plist 'obj_dir obj-dir)
  334. )
  335. (kill-buffer nil)
  336. (message "Parsing %s ... done" gpr-file)
  337. )
  338. ))
  339. (defun ada-treat-cmd-string (cmd-string)
  340. "Replace variable references ${var} in CMD-STRING with the appropriate value.
  341. Also replace standard environment variables $var.
  342. Assumes project exists.
  343. As a special case, ${current} is replaced with the name of the current
  344. file, minus extension but with directory, and ${full_current} is
  345. replaced by the name including the extension."
  346. (while (string-match "\\(-[^-\$IO]*[IO]\\)?\${\\([^}]+\\)}" cmd-string)
  347. (let (value
  348. (name (match-string 2 cmd-string)))
  349. (cond
  350. ((string= name "current")
  351. (setq value (file-name-sans-extension (buffer-file-name))))
  352. ((string= name "full_current")
  353. (setq value (buffer-file-name)))
  354. (t
  355. (save-match-data
  356. (setq value (ada-xref-get-project-field (intern name))))))
  357. ;; Check if there is an environment variable with the same name
  358. (if (null value)
  359. (if (not (setq value (getenv name)))
  360. (message "%s" (concat "No project or environment variable " name " found"))))
  361. (cond
  362. ((null value)
  363. (setq cmd-string (replace-match "" t t cmd-string)))
  364. ((stringp value)
  365. (setq cmd-string (replace-match value t t cmd-string)))
  366. ((listp value)
  367. (let ((prefix (match-string 1 cmd-string)))
  368. (setq cmd-string (replace-match
  369. (mapconcat (lambda(x) (concat prefix x)) value " ")
  370. t t cmd-string)))))
  371. ))
  372. (substitute-in-file-name cmd-string))
  373. (defun ada-xref-get-project-field (field)
  374. "Extract the value of FIELD from the current project file.
  375. Project variables are substituted.
  376. Note that for src_dir and obj_dir, you should rather use
  377. `ada-xref-get-src-dir-field' or `ada-xref-get-obj-dir-field'
  378. which will in addition return the default paths."
  379. (let* ((project-plist (cdr (ada-xref-current-project)))
  380. (value (plist-get project-plist field)))
  381. (cond
  382. ((eq field 'gnatmake_opt)
  383. (let ((gpr-file (plist-get project-plist 'gpr_file)))
  384. (if (not (string= gpr-file ""))
  385. (setq value (concat "-P\"" gpr-file "\" " value)))))
  386. ;; FIXME: check for src_dir, obj_dir here, rather than requiring user to do it
  387. (t
  388. nil))
  389. ;; Substitute the ${...} constructs in all the strings, including
  390. ;; inside lists
  391. (cond
  392. ((stringp value)
  393. (ada-treat-cmd-string value))
  394. ((null value)
  395. nil)
  396. ((listp value)
  397. (mapcar (lambda(x) (if x (ada-treat-cmd-string x) x)) value))
  398. (t
  399. value)
  400. )
  401. ))
  402. (defun ada-xref-get-src-dir-field ()
  403. "Return the full value for src_dir, including the default directories.
  404. All the directories are returned as absolute directories."
  405. (let ((build-dir (ada-xref-get-project-field 'build_dir)))
  406. (append
  407. ;; Add ${build_dir} in front of the path
  408. (list build-dir)
  409. (ada-get-absolute-dir-list (ada-xref-get-project-field 'src_dir)
  410. build-dir)
  411. ;; Add the standard runtime at the end
  412. ada-xref-runtime-library-specs-path)))
  413. (defun ada-xref-get-obj-dir-field ()
  414. "Return the full value for obj_dir, including the default directories.
  415. All the directories are returned as absolute directories."
  416. (let ((build-dir (ada-xref-get-project-field 'build_dir)))
  417. (append
  418. ;; Add ${build_dir} in front of the path
  419. (list build-dir)
  420. (ada-get-absolute-dir-list (ada-xref-get-project-field 'obj_dir)
  421. build-dir)
  422. ;; Add the standard runtime at the end
  423. ada-xref-runtime-library-ali-path)))
  424. (defun ada-xref-set-project-field (field value)
  425. "Set FIELD to VALUE in current project. Assumes project exists."
  426. ;; same algorithm to find project-plist as ada-xref-current-project
  427. (let* ((file-name (ada-xref-current-project-file))
  428. (project-plist (cdr (assoc file-name ada-xref-project-files))))
  429. (setq project-plist (plist-put project-plist field value))
  430. (setcdr (assoc file-name ada-xref-project-files) project-plist)))
  431. (defun ada-xref-update-project-menu ()
  432. "Update the menu Ada->Project, with the list of available project files."
  433. ;; Create the standard items.
  434. (let ((submenu
  435. `("Project"
  436. ["Load..." ada-set-default-project-file t]
  437. ["New..." ada-prj-new t]
  438. ["Edit..." ada-prj-edit t]
  439. "---"
  440. ;; Add the project files
  441. ,@(mapcar
  442. (lambda (x)
  443. (let* ((name (or (car x) "<default>"))
  444. (command `(lambda ()
  445. "Select the current project file."
  446. (interactive)
  447. (ada-select-prj-file ,name))))
  448. (vector
  449. (file-name-nondirectory name)
  450. command
  451. :button (cons
  452. :toggle
  453. (equal ada-prj-default-project-file
  454. (car x))
  455. ))))
  456. (or ada-xref-project-files '(nil))))))
  457. (easy-menu-add-item ada-mode-menu '() submenu)))
  458. ;;-------------------------------------------------------------
  459. ;;-- Searching a file anywhere on the source path.
  460. ;;--
  461. ;;-- The following functions provide support for finding a file anywhere
  462. ;;-- on the source path, without providing an explicit directory.
  463. ;;-- They also provide file name completion in the minibuffer.
  464. ;;--
  465. ;;-- Public subprograms: ada-find-file
  466. ;;--
  467. ;;-------------------------------------------------------------
  468. (defun ada-do-file-completion (string predicate flag)
  469. "Completion function when reading a file from the minibuffer.
  470. Completion is attempted in all the directories in the source path,
  471. as defined in the project file."
  472. ;; FIXME: doc arguments
  473. ;; This function is not itself interactive, but it is called as part
  474. ;; of the prompt of interactive functions, so we require a project
  475. ;; file.
  476. (ada-require-project-file)
  477. (let (list
  478. (dirs (ada-xref-get-src-dir-field)))
  479. (while dirs
  480. (if (file-directory-p (car dirs))
  481. (set 'list (append list (file-name-all-completions string (car dirs)))))
  482. (set 'dirs (cdr dirs)))
  483. (cond ((equal flag 'lambda)
  484. (assoc string list))
  485. (flag
  486. list)
  487. (t
  488. (try-completion string
  489. (mapcar (lambda (x) (cons x 1)) list)
  490. predicate)))))
  491. ;;;###autoload
  492. (defun ada-find-file (filename)
  493. "Open FILENAME, from anywhere in the source path.
  494. Completion is available."
  495. (interactive
  496. (list (completing-read "File: " 'ada-do-file-completion)))
  497. (let ((file (ada-find-src-file-in-dir filename)))
  498. (if file
  499. (find-file file)
  500. (error "%s not found in src_dir" filename))))
  501. ;; ----- Utilities -------------------------------------------------
  502. (defun ada-require-project-file ()
  503. "If the current project does not exist, load or create a default one.
  504. Should only be called from interactive functions."
  505. (if (string= "" ada-prj-default-project-file)
  506. (ada-reread-prj-file (ada-prj-find-prj-file t))))
  507. (defun ada-xref-current-project-file ()
  508. "Return the current project file name; never nil.
  509. Call `ada-require-project-file' first if a project must exist."
  510. (if (not (string= "" ada-prj-default-project-file))
  511. ada-prj-default-project-file
  512. (ada-prj-find-prj-file t)))
  513. (defun ada-xref-current-project ()
  514. "Return the current project.
  515. Call `ada-require-project-file' first to ensure a project exists."
  516. (let ((file-name (ada-xref-current-project-file)))
  517. (assoc file-name ada-xref-project-files)))
  518. (defun ada-show-current-project ()
  519. "Display current project file name in message buffer."
  520. (interactive)
  521. (message (ada-xref-current-project-file)))
  522. (defun ada-show-current-main ()
  523. "Display current main file name in message buffer."
  524. (interactive)
  525. (message "ada-mode main: %s" (ada-xref-get-project-field 'main)))
  526. (defun ada-xref-push-pos (filename position)
  527. "Push (FILENAME, POSITION) on the position ring for cross-references."
  528. (setq ada-xref-pos-ring (cons (list position filename) ada-xref-pos-ring))
  529. (if (> (length ada-xref-pos-ring) ada-xref-pos-ring-max)
  530. (setcdr (nthcdr (1- ada-xref-pos-ring-max) ada-xref-pos-ring) nil)))
  531. (defun ada-xref-goto-previous-reference ()
  532. "Go to the previous cross-reference we were on."
  533. (interactive)
  534. (if ada-xref-pos-ring
  535. (let ((pos (car ada-xref-pos-ring)))
  536. (setq ada-xref-pos-ring (cdr ada-xref-pos-ring))
  537. (find-file (car (cdr pos)))
  538. (goto-char (car pos)))))
  539. (defun ada-convert-file-name (name)
  540. "Convert from NAME to a name that can be used by the compilation commands.
  541. This is overridden on VMS to convert from VMS filenames to Unix filenames."
  542. name)
  543. ;; FIXME: use convert-standard-filename instead
  544. (defun ada-set-default-project-file (file)
  545. "Set FILE as the current project file."
  546. (interactive "fProject file:")
  547. (ada-parse-prj-file file)
  548. (ada-select-prj-file file))
  549. ;; ------ Handling the project file -----------------------------
  550. (defun ada-prj-find-prj-file (&optional no-user-question)
  551. "Find the project file associated with the current buffer.
  552. If the buffer is not in Ada mode, or not associated with a file,
  553. return `ada-prj-default-project-file'. Otherwise, search for a file with
  554. the same base name as the Ada file, but extension given by
  555. `ada-prj-file-extension' (default .adp). If not found, search for *.adp
  556. in the current directory; if several are found, and NO-USER-QUESTION
  557. is non-nil, prompt the user to select one. If none are found, return
  558. 'default.adp'."
  559. (let (selected)
  560. (if (not (and (derived-mode-p 'ada-mode)
  561. buffer-file-name))
  562. ;; Not in an Ada buffer, or current buffer not associated
  563. ;; with a file (for instance an emerge buffer)
  564. (setq selected nil)
  565. ;; other cases: use a more complex algorithm
  566. (let* ((current-file (buffer-file-name))
  567. (first-choice (concat
  568. (file-name-sans-extension current-file)
  569. ada-prj-file-extension))
  570. (dir (file-name-directory current-file))
  571. (prj-files (directory-files
  572. dir t
  573. (concat ".*" (regexp-quote
  574. ada-prj-file-extension) "$")))
  575. (choice nil))
  576. (cond
  577. ((file-exists-p first-choice)
  578. ;; filename.adp
  579. (set 'selected first-choice))
  580. ((= (length prj-files) 1)
  581. ;; Exactly one project file was found in the current directory
  582. (set 'selected (car prj-files)))
  583. ((and (> (length prj-files) 1) (not no-user-question))
  584. ;; multiple project files in current directory, ask the user
  585. (save-window-excursion
  586. (with-output-to-temp-buffer "*choice list*"
  587. (princ "There are more than one possible project file.\n")
  588. (princ "Which one should we use ?\n\n")
  589. (princ " no. file name \n")
  590. (princ " --- ------------------------\n")
  591. (let ((counter 1))
  592. (while (<= counter (length prj-files))
  593. (princ (format " %2d) %s\n"
  594. counter
  595. (nth (1- counter) prj-files)))
  596. (setq counter (1+ counter))
  597. ))) ; end of with-output-to ...
  598. (setq choice nil)
  599. (while (or
  600. (not choice)
  601. (not (integerp choice))
  602. (< choice 1)
  603. (> choice (length prj-files)))
  604. (setq choice (string-to-number
  605. (read-from-minibuffer "Enter No. of your choice: "))))
  606. (set 'selected (nth (1- choice) prj-files))))
  607. ((= (length prj-files) 0)
  608. ;; No project file in the current directory; ask user
  609. (unless (or no-user-question (not ada-always-ask-project))
  610. (setq ada-last-prj-file
  611. (read-file-name
  612. (concat "project file [" ada-last-prj-file "]:")
  613. nil ada-last-prj-file))
  614. (unless (string= ada-last-prj-file "")
  615. (set 'selected ada-last-prj-file))))
  616. )))
  617. (or selected "default.adp")
  618. ))
  619. (defun ada-default-prj-properties ()
  620. "Return the default project properties list with the current buffer as main."
  621. (let ((file (buffer-file-name nil)))
  622. (list
  623. ;; variable name alphabetical order
  624. 'ada_project_path (or (getenv "ADA_PROJECT_PATH") "")
  625. 'ada_project_path_sep ada-prj-ada-project-path-sep
  626. 'bind_opt ada-prj-default-bind-opt
  627. 'build_dir default-directory
  628. 'casing (if (listp ada-case-exception-file)
  629. ada-case-exception-file
  630. (list ada-case-exception-file))
  631. 'check_cmd (list ada-prj-default-check-cmd) ;; FIXME: should not a list
  632. 'comp_cmd (list ada-prj-default-comp-cmd) ;; FIXME: should not a list
  633. 'comp_opt ada-prj-default-comp-opt
  634. 'cross_prefix ""
  635. 'debug_cmd (concat ada-prj-default-debugger
  636. " ${main}" (if ada-on-ms-windows ".exe")) ;; FIXME: don't need .exe?
  637. 'debug_post_cmd (list nil)
  638. 'debug_pre_cmd (list (concat ada-cd-command " ${build_dir}"))
  639. 'gnatmake_opt ada-prj-default-gnatmake-opt
  640. 'gnatfind_opt ada-prj-gnatfind-switches
  641. 'gpr_file ada-prj-default-gpr-file
  642. 'link_opt ada-prj-default-link-opt
  643. 'main (if file
  644. (file-name-nondirectory
  645. (file-name-sans-extension file))
  646. "")
  647. 'make_cmd (list ada-prj-default-make-cmd) ;; FIXME: should not a list
  648. 'obj_dir (list ".")
  649. 'remote_machine ""
  650. 'run_cmd (list (concat "./${main}" (if ada-on-ms-windows ".exe")))
  651. ;; FIXME: should not a list
  652. ;; FIXME: don't need .exe?
  653. 'src_dir (list ".")
  654. )))
  655. (defun ada-parse-prj-file (prj-file)
  656. "Read PRJ-FILE, set project properties in `ada-xref-project-files'."
  657. (let ((project (ada-default-prj-properties)))
  658. (setq prj-file (expand-file-name prj-file))
  659. (if (string= (file-name-extension prj-file) "gpr")
  660. (set 'project (ada-gnat-parse-gpr project prj-file))
  661. (set 'project (ada-parse-prj-file-1 prj-file project))
  662. )
  663. ;; Store the project properties
  664. (if (assoc prj-file ada-xref-project-files)
  665. (setcdr (assoc prj-file ada-xref-project-files) project)
  666. (add-to-list 'ada-xref-project-files (cons prj-file project)))
  667. (ada-xref-update-project-menu)
  668. ))
  669. (defun ada-parse-prj-file-1 (prj-file project)
  670. "Parse the Ada mode project file PRJ-FILE, set project properties in PROJECT.
  671. Return new value of PROJECT."
  672. (let ((ada-buffer (current-buffer))
  673. ;; fields that are lists or otherwise require special processing
  674. ada_project_path casing comp_cmd check_cmd
  675. debug_pre_cmd debug_post_cmd gpr_file make_cmd obj_dir src_dir run_cmd)
  676. ;; Give users a chance to use compiler-specific project file formats
  677. (let ((buffer (run-hook-with-args-until-success
  678. 'ada-load-project-hook prj-file)))
  679. (unless buffer
  680. ;; we load the project file with no warnings; if it does not
  681. ;; exist, we stay in the Ada buffer; no project variable
  682. ;; settings will be found. That works for the default
  683. ;; "default.adp", which does not exist as a file.
  684. (setq buffer (find-file-noselect prj-file nil)))
  685. (set-buffer buffer))
  686. (widen)
  687. (goto-char (point-min))
  688. ;; process each line
  689. (while (not (eobp))
  690. ;; ignore lines that don't have the format "name=value", put
  691. ;; 'name', 'value' in match-string.
  692. (if (looking-at "^\\([^=\n]+\\)=\\(.*\\)")
  693. (cond
  694. ;; FIXME: strip trailing spaces
  695. ;; variable name alphabetical order
  696. ((string= (match-string 1) "ada_project_path")
  697. (add-to-list 'ada_project_path
  698. (expand-file-name
  699. (substitute-in-file-name (match-string 2)))))
  700. ((string= (match-string 1) "build_dir")
  701. (set 'project
  702. (plist-put project 'build_dir
  703. (file-name-as-directory (match-string 2)))))
  704. ((string= (match-string 1) "casing")
  705. (add-to-list 'casing
  706. (expand-file-name (substitute-in-file-name (match-string 2)))))
  707. ((string= (match-string 1) "check_cmd")
  708. (add-to-list 'check_cmd (match-string 2)))
  709. ((string= (match-string 1) "comp_cmd")
  710. (add-to-list 'comp_cmd (match-string 2)))
  711. ((string= (match-string 1) "debug_post_cmd")
  712. (add-to-list 'debug_post_cmd (match-string 2)))
  713. ((string= (match-string 1) "debug_pre_cmd")
  714. (add-to-list 'debug_pre_cmd (match-string 2)))
  715. ((string= (match-string 1) "gpr_file")
  716. ;; expand now; path is relative to Emacs project file
  717. (setq gpr_file (expand-file-name (match-string 2))))
  718. ((string= (match-string 1) "make_cmd")
  719. (add-to-list 'make_cmd (match-string 2)))
  720. ((string= (match-string 1) "obj_dir")
  721. (add-to-list 'obj_dir
  722. (file-name-as-directory
  723. (expand-file-name (match-string 2)))))
  724. ((string= (match-string 1) "run_cmd")
  725. (add-to-list 'run_cmd (match-string 2)))
  726. ((string= (match-string 1) "src_dir")
  727. (add-to-list 'src_dir
  728. (file-name-as-directory
  729. (expand-file-name (match-string 2)))))
  730. (t
  731. ;; any other field in the file is just copied
  732. (set 'project (plist-put project
  733. (intern (match-string 1))
  734. (match-string 2))))))
  735. (forward-line 1))
  736. ;; done reading file
  737. ;; back to the user buffer
  738. (set-buffer ada-buffer)
  739. ;; process accumulated lists
  740. (if ada_project_path
  741. (let ((sep (plist-get project 'ada_project_path_sep)))
  742. (setq ada_project_path (reverse ada_project_path))
  743. (setq ada_project_path (mapconcat 'identity ada_project_path sep))
  744. (set 'project (plist-put project 'ada_project_path ada_project_path))
  745. ;; env var needed now for ada-gnat-parse-gpr
  746. (setenv "ADA_PROJECT_PATH" ada_project_path)))
  747. (if debug_post_cmd (set 'project (plist-put project 'debug_post_cmd (reverse debug_post_cmd))))
  748. (if debug_pre_cmd (set 'project (plist-put project 'debug_pre_cmd (reverse debug_pre_cmd))))
  749. (if casing (set 'project (plist-put project 'casing (reverse casing))))
  750. (if check_cmd (set 'project (plist-put project 'check_cmd (reverse check_cmd))))
  751. (if comp_cmd (set 'project (plist-put project 'comp_cmd (reverse comp_cmd))))
  752. (if make_cmd (set 'project (plist-put project 'make_cmd (reverse make_cmd))))
  753. (if run_cmd (set 'project (plist-put project 'run_cmd (reverse run_cmd))))
  754. (if gpr_file
  755. (progn
  756. (set 'project (ada-gnat-parse-gpr project gpr_file))
  757. ;; append Ada source and object directories to others from Emacs project file
  758. (setq src_dir (append (plist-get project 'src_dir) src_dir))
  759. (setq obj_dir (append (plist-get project 'obj_dir) obj_dir))
  760. (setq ada-xref-runtime-library-specs-path '()
  761. ada-xref-runtime-library-ali-path '()))
  762. )
  763. ;; FIXME: gnatpath.exe doesn't output the runtime libraries, so always call ada-initialize-runtime-library
  764. ;; if using a gpr_file, the runtime library directories are
  765. ;; included in src_dir and obj_dir; otherwise they are in the
  766. ;; 'runtime-library' variables.
  767. ;; FIXME: always append to src_dir, obj_dir
  768. (ada-initialize-runtime-library (or (ada-xref-get-project-field 'cross_prefix) ""))
  769. ;;)
  770. (if obj_dir (set 'project (plist-put project 'obj_dir (reverse obj_dir))))
  771. (if src_dir (set 'project (plist-put project 'src_dir (reverse src_dir))))
  772. project
  773. ))
  774. (defun ada-select-prj-file (file)
  775. "Select FILE as the current project file."
  776. (interactive)
  777. (setq ada-prj-default-project-file (expand-file-name file))
  778. (let ((casing (ada-xref-get-project-field 'casing)))
  779. (if casing
  780. (progn
  781. ;; FIXME: use ada-get-absolute-dir here
  782. (setq ada-case-exception-file casing)
  783. (ada-case-read-exceptions))))
  784. (let ((ada_project_path (ada-xref-get-project-field 'ada_project_path)))
  785. (if ada_project_path
  786. ;; FIXME: use ada-get-absolute-dir, mapconcat here
  787. (setenv "ADA_PROJECT_PATH" ada_project_path)))
  788. (setq compilation-search-path (ada-xref-get-src-dir-field))
  789. (setq ada-search-directories-internal
  790. ;; FIXME: why do we need directory-file-name here?
  791. (append (mapcar 'directory-file-name compilation-search-path)
  792. ada-search-directories))
  793. ;; return 't', for decent display in message buffer when called interactively
  794. t)
  795. (defun ada-find-references (&optional pos arg local-only)
  796. "Find all references to the entity under POS.
  797. Calls gnatfind to find the references.
  798. If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved.
  799. If LOCAL-ONLY is non-nil, only declarations in the current file are returned."
  800. (interactive "d\nP")
  801. (ada-require-project-file)
  802. (let* ((identlist (ada-read-identifier pos))
  803. (alifile (ada-get-ali-file-name (ada-file-of identlist)))
  804. (process-environment (ada-set-environment)))
  805. (set-buffer (get-file-buffer (ada-file-of identlist)))
  806. ;; if the file is more recent than the executable
  807. (if (or (buffer-modified-p (current-buffer))
  808. (file-newer-than-file-p (ada-file-of identlist) alifile))
  809. (ada-find-any-references (ada-name-of identlist)
  810. (ada-file-of identlist)
  811. nil nil local-only arg)
  812. (ada-find-any-references (ada-name-of identlist)
  813. (ada-file-of identlist)
  814. (ada-line-of identlist)
  815. (ada-column-of identlist) local-only arg)))
  816. )
  817. (defun ada-find-local-references (&optional pos arg)
  818. "Find all references to the entity under POS.
  819. Calls `gnatfind' to find the references.
  820. If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved."
  821. (interactive "d\nP")
  822. (ada-find-references pos arg t))
  823. (defconst ada-gnatfind-buffer-name "*gnatfind*")
  824. (defun ada-find-any-references
  825. (entity &optional file line column local-only append)
  826. "Search for references to any entity whose name is ENTITY.
  827. ENTITY was first found the location given by FILE, LINE and COLUMN.
  828. If LOCAL-ONLY is non-nil, then list only the references in FILE,
  829. which is much faster.
  830. If APPEND is non-nil, then append the output of the command to the
  831. existing buffer `*gnatfind*', if there is one."
  832. (interactive "sEntity name: ")
  833. (ada-require-project-file)
  834. ;; Prepare the gnatfind command. Note that we must protect the quotes
  835. ;; around operators, so that they are correctly handled and can be
  836. ;; processed (gnatfind \"+\":...).
  837. (let* ((quote-entity
  838. (if (= (aref entity 0) ?\")
  839. (if ada-on-ms-windows
  840. (concat "\\\"" (substring entity 1 -1) "\\\"")
  841. (concat "'\"" (substring entity 1 -1) "\"'"))
  842. entity))
  843. (switches (ada-xref-get-project-field 'gnatfind_opt))
  844. ;; FIXME: use gpr_file
  845. (cross-prefix (ada-xref-get-project-field 'cross_prefix))
  846. (command (concat cross-prefix "gnat find " switches " "
  847. quote-entity
  848. (if file (concat ":" (file-name-nondirectory file)))
  849. (if line (concat ":" line))
  850. (if column (concat ":" column))
  851. (if local-only (concat " " (file-name-nondirectory file)))
  852. ))
  853. old-contents)
  854. ;; If a project file is defined, use it
  855. (if (and ada-prj-default-project-file
  856. (not (string= ada-prj-default-project-file "")))
  857. (if (string-equal (file-name-extension ada-prj-default-project-file)
  858. "gpr")
  859. (setq command (concat command " -P\"" ada-prj-default-project-file "\""))
  860. (setq command (concat command " -p\"" ada-prj-default-project-file "\""))))
  861. (if (and append (get-buffer ada-gnatfind-buffer-name))
  862. (with-current-buffer "*gnatfind*"
  863. (setq old-contents (buffer-string))))
  864. (let ((compilation-error "reference"))
  865. (compilation-start command 'compilation-mode (lambda (_mode) ada-gnatfind-buffer-name)))
  866. ;; Hide the "Compilation" menu
  867. (with-current-buffer ada-gnatfind-buffer-name
  868. (local-unset-key [menu-bar compilation-menu])
  869. (if old-contents
  870. (progn
  871. (goto-char 1)
  872. (set 'buffer-read-only nil)
  873. (insert old-contents)
  874. (set 'buffer-read-only t)
  875. (goto-char (point-max)))))
  876. )
  877. )
  878. (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file))
  879. ;; ----- Identifier Completion --------------------------------------------
  880. (defun ada-complete-identifier (pos)
  881. "Try to complete the identifier around POS, using compiler cross-reference information."
  882. (interactive "d")
  883. (ada-require-project-file)
  884. ;; Initialize function-local variables and jump to the .ali buffer
  885. ;; Note that for regexp search is case insensitive too
  886. (let* ((curbuf (current-buffer))
  887. (identlist (ada-read-identifier pos))
  888. (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
  889. (regexp-quote (ada-name-of identlist))
  890. "[a-zA-Z0-9_]*\\)"))
  891. (completed nil)
  892. (symalist nil))
  893. ;; Open the .ali file
  894. (set-buffer (ada-get-ali-buffer (buffer-file-name)))
  895. (goto-char (point-max))
  896. ;; build an alist of possible completions
  897. (while (re-search-backward sofar nil t)
  898. (setq symalist (cons (cons (match-string 1) nil) symalist)))
  899. (setq completed (try-completion "" symalist))
  900. ;; kills .ali buffer
  901. (kill-buffer nil)
  902. ;; deletes the incomplete identifier in the buffer
  903. (set-buffer curbuf)
  904. (looking-at "[a-zA-Z0-9_]+")
  905. (replace-match "")
  906. ;; inserts the completed symbol
  907. (insert completed)
  908. ))
  909. ;; ----- Cross-referencing ----------------------------------------
  910. (defun ada-point-and-xref ()
  911. "Jump to the declaration of the entity below the cursor."
  912. (interactive)
  913. (mouse-set-point last-input-event)
  914. (ada-goto-declaration (point)))
  915. (defun ada-point-and-xref-body ()
  916. "Jump to the body of the entity under the cursor."
  917. (interactive)
  918. (mouse-set-point last-input-event)
  919. (ada-goto-body (point)))
  920. (defun ada-goto-body (pos &optional other-frame)
  921. "Display the body of the entity around POS.
  922. OTHER-FRAME non-nil means display in another frame.
  923. If the entity doesn't have a body, display its declaration.
  924. As a side effect, the buffer for the declaration is also open."
  925. (interactive "d")
  926. (ada-goto-declaration pos other-frame)
  927. ;; Temporarily force the display in the same buffer, since we
  928. ;; already changed previously
  929. (let ((ada-xref-other-buffer nil))
  930. (ada-goto-declaration (point) nil)))
  931. (defun ada-goto-declaration (pos &optional other-frame)
  932. "Display the declaration of the identifier around POS.
  933. The declaration is shown in another buffer if `ada-xref-other-buffer' is
  934. non-nil.
  935. If OTHER-FRAME is non-nil, display the cross-reference in another frame."
  936. (interactive "d")
  937. (ada-require-project-file)
  938. (push-mark pos)
  939. (ada-xref-push-pos (buffer-file-name) pos)
  940. ;; First try the standard algorithm by looking into the .ali file, but if
  941. ;; that file was too old or even did not exist, try to look in the whole
  942. ;; object path for a possible location.
  943. (let ((identlist (ada-read-identifier pos)))
  944. (condition-case err
  945. (ada-find-in-ali identlist other-frame)
  946. ;; File not found: print explicit error message
  947. (error-file-not-found
  948. (message (concat (error-message-string err)
  949. (nthcdr 1 err))))
  950. (error
  951. (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist))))
  952. ;; If the ALI file was up-to-date, then we probably have a predefined
  953. ;; entity, whose references are not given by GNAT
  954. (if (and (file-exists-p ali-file)
  955. (file-newer-than-file-p ali-file (ada-file-of identlist)))
  956. (message "No cross-reference found -- may be a predefined entity.")
  957. ;; Else, look in every ALI file, except if the user doesn't want that
  958. (if ada-xref-search-with-egrep
  959. (ada-find-in-src-path identlist other-frame)
  960. (message "Cross-referencing information is not up-to-date; please recompile.")
  961. )))))))
  962. (defun ada-goto-declaration-other-frame (pos)
  963. "Display the declaration of the identifier around POS.
  964. The declaration is shown in another frame if `ada-xref-other-buffer' is
  965. non-nil."
  966. (interactive "d")
  967. (ada-goto-declaration pos t))
  968. (defun ada-remote (command)
  969. "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
  970. (let ((machine (ada-xref-get-project-field 'remote_machine)))
  971. (if (or (not machine) (string= machine ""))
  972. command
  973. (format "%s %s '(%s)'"
  974. remote-shell-program
  975. machine
  976. command))))
  977. (defun ada-get-absolute-dir-list (dir-list root-dir)
  978. "Return the list of absolute directories found in DIR-LIST.
  979. If a directory is a relative directory, ROOT-DIR is prepended.
  980. Project and environment variables are substituted."
  981. (mapcar (lambda (x) (expand-file-name x (ada-treat-cmd-string root-dir))) dir-list))
  982. (defun ada-set-environment ()
  983. "Prepare an environment for Ada compilation.
  984. This returns a new value to use for `process-environment',
  985. but does not actually put it into use.
  986. It modifies the source path and object path with the values found in the
  987. project file."
  988. (let ((include (getenv "ADA_INCLUDE_PATH"))
  989. (objects (getenv "ADA_OBJECTS_PATH"))
  990. (build-dir (ada-xref-get-project-field 'build_dir)))
  991. (if include
  992. (set 'include (concat path-separator include)))
  993. (if objects
  994. (set 'objects (concat path-separator objects)))
  995. (cons
  996. (concat "ADA_INCLUDE_PATH="
  997. (mapconcat (lambda(x) (expand-file-name x build-dir))
  998. (ada-xref-get-project-field 'src_dir)
  999. path-separator)
  1000. include)
  1001. (cons
  1002. (concat "ADA_OBJECTS_PATH="
  1003. (mapconcat (lambda(x) (expand-file-name x build-dir))
  1004. (ada-xref-get-project-field 'obj_dir)
  1005. path-separator)
  1006. objects)
  1007. process-environment))))
  1008. (defun ada-compile-application (&optional arg)
  1009. "Compile the application, using the command found in the project file.
  1010. If ARG is not nil, ask for user confirmation."
  1011. (interactive "P")
  1012. (ada-require-project-file)
  1013. (let ((cmd (ada-xref-get-project-field 'make_cmd))
  1014. (process-environment (ada-set-environment))
  1015. (compilation-scroll-output t))
  1016. (setq compilation-search-path (ada-xref-get-src-dir-field))
  1017. ;; If no project file was found, ask the user
  1018. (unless cmd
  1019. (setq cmd '("") arg t))
  1020. ;; Make a single command from the list of commands, including the
  1021. ;; commands to run it on a remote machine.
  1022. (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
  1023. (if (or ada-xref-confirm-compile arg)
  1024. (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
  1025. ;; Insert newlines so as to separate the name of the commands to run
  1026. ;; and the output of the commands. This doesn't work with cmdproxy.exe,
  1027. ;; which gets confused by newline characters.
  1028. (if (not (string-match ".exe" shell-file-name))
  1029. (setq cmd (concat cmd "\n\n")))
  1030. (compile (ada-quote-cmd cmd))))
  1031. (defun ada-set-main-compile-application ()
  1032. "Set main project variable to current buffer, build main."
  1033. (interactive)
  1034. (ada-require-project-file)
  1035. (let* ((file (buffer-file-name (current-buffer)))
  1036. main)
  1037. (if (not file)
  1038. (error "No file for current buffer")
  1039. (setq main
  1040. (if file
  1041. (file-name-nondirectory
  1042. (file-name-sans-extension file))
  1043. ""))
  1044. (ada-xref-set-project-field 'main main)
  1045. (ada-compile-application))))
  1046. (defun ada-compile-current (&optional arg prj-field)
  1047. "Recompile the current file.
  1048. If ARG is non-nil, ask for user confirmation of the command.
  1049. PRJ-FIELD is the name of the field to use in the project file to get the
  1050. command, and should be either `comp_cmd' (default) or `check_cmd'."
  1051. (interactive "P")
  1052. (ada-require-project-file)
  1053. (let* ((field (if prj-field prj-field 'comp_cmd))
  1054. (cmd (ada-xref-get-project-field field))
  1055. (process-environment (ada-set-environment))
  1056. (compilation-scroll-output t))
  1057. (unless cmd
  1058. (setq cmd '("") arg t))
  1059. ;; Make a single command from the list of commands, including the
  1060. ;; commands to run it on a remote machine.
  1061. (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
  1062. ;; If no project file was found, ask the user
  1063. (if (or ada-xref-confirm-compile arg)
  1064. (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
  1065. (compile (ada-quote-cmd cmd))))
  1066. (defun ada-check-current (&optional arg)
  1067. "Check the current file for syntax errors.
  1068. If ARG is non-nil, ask for user confirmation of the command."
  1069. (interactive "P")
  1070. (ada-compile-current arg 'check_cmd))
  1071. (defun ada-run-application (&optional arg)
  1072. "Run the application.
  1073. If ARG is non-nil, ask for user confirmation."
  1074. (interactive)
  1075. (ada-require-project-file)
  1076. (let ((machine (ada-xref-get-project-field 'cross_prefix)))
  1077. (if (and machine (not (string= machine "")))
  1078. (error "This feature is not supported yet for cross environments")))
  1079. (let ((command (ada-xref-get-project-field 'run_cmd)))
  1080. ;; Guess the command if it wasn't specified
  1081. (if (not command)
  1082. (set 'command (list (file-name-sans-extension (buffer-name)))))
  1083. ;; Modify the command to run remotely
  1084. (setq command (ada-remote (mapconcat 'identity command
  1085. ada-command-separator)))
  1086. ;; Ask for the arguments to the command if required
  1087. (if (or ada-xref-confirm-compile arg)
  1088. (setq command (read-from-minibuffer "Enter command to execute: "
  1089. command)))
  1090. ;; Run the command
  1091. (with-current-buffer (get-buffer-create "*run*")
  1092. (set 'buffer-read-only nil)
  1093. (erase-buffer)
  1094. (start-process "run" (current-buffer) shell-file-name
  1095. "-c" command)
  1096. (comint-mode)
  1097. ;; Set these two variables to their default values, since otherwise
  1098. ;; the output buffer is scrolled so that only the last output line
  1099. ;; is visible at the top of the buffer.
  1100. (set (make-local-variable 'scroll-step) 0)
  1101. (set (make-local-variable 'scroll-conservatively) 0)
  1102. )
  1103. (display-buffer "*run*")
  1104. ;; change to buffer *run* for interactive programs
  1105. (other-window 1)
  1106. (switch-to-buffer "*run*")
  1107. ))
  1108. (defun ada-gdb-application (&optional arg executable-name)
  1109. "Start the debugger on the application.
  1110. If ARG is non-nil, ask the user to confirm the command.
  1111. EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
  1112. project file."
  1113. (interactive "P")
  1114. (ada-require-project-file)
  1115. (let ((buffer (current-buffer))
  1116. cmd pre-cmd post-cmd)
  1117. (setq cmd (if executable-name
  1118. (concat ada-prj-default-debugger " " executable-name)
  1119. (ada-xref-get-project-field 'debug_cmd))
  1120. pre-cmd (ada-xref-get-project-field 'debug_pre_cmd)
  1121. post-cmd (ada-xref-get-project-field 'debug_post_cmd))
  1122. ;; If the command was not given in the project file, start a bare gdb
  1123. (if (not cmd)
  1124. (set 'cmd (concat ada-prj-default-debugger
  1125. " "
  1126. (or executable-name
  1127. (file-name-sans-extension (buffer-file-name))))))
  1128. ;; For gvd, add an extra switch so that the Emacs window is completely
  1129. ;; swallowed inside the Gvd one
  1130. (if (and ada-tight-gvd-integration
  1131. (string-match "^[^ \t]*gvd" cmd))
  1132. ;; Start a new frame, so that when gvd exists we do not kill Emacs
  1133. ;; We make sure that gvd swallows the new frame, not the one the
  1134. ;; user has been using until now
  1135. ;; The frame is made invisible initially, so that GtkPlug gets a
  1136. ;; chance to fully manage it. Then it works fine with Enlightenment
  1137. ;; as well
  1138. (let ((frame (make-frame '((visibility . nil)))))
  1139. (set 'cmd (concat
  1140. cmd " --editor-window="
  1141. (cdr (assoc 'outer-window-id (frame-parameters frame)))))
  1142. (select-frame frame)))
  1143. ;; Add a -fullname switch
  1144. ;; Use the remote machine
  1145. (set 'cmd (ada-remote (concat cmd " -fullname ")))
  1146. ;; Ask for confirmation if required
  1147. (if (or arg ada-xref-confirm-compile)
  1148. (set 'cmd (read-from-minibuffer "enter command to debug: " cmd)))
  1149. (let ((old-comint-exec (symbol-function 'comint-exec)))
  1150. ;; Do not add -fullname, since we can have a 'rsh' command in front.
  1151. ;; FIXME: This is evil but luckily a nop under Emacs-21.3.50 ! -stef
  1152. (fset 'gud-gdb-massage-args (lambda (_file args) args))
  1153. (set 'pre-cmd (mapconcat 'identity pre-cmd ada-command-separator))
  1154. (if (not (equal pre-cmd ""))
  1155. (setq pre-cmd (concat pre-cmd ada-command-separator)))
  1156. (set 'post-cmd (mapconcat 'identity post-cmd "\n"))
  1157. (if post-cmd
  1158. (set 'post-cmd (concat post-cmd "\n")))
  1159. ;; Temporarily replaces the definition of `comint-exec' so that we
  1160. ;; can execute commands before running gdb.
  1161. ;; FIXME: This is evil and not temporary !!! -stef
  1162. (fset 'comint-exec
  1163. `(lambda (buffer name command startfile switches)
  1164. (let (compilation-buffer-name-function)
  1165. (save-excursion
  1166. (set 'compilation-buffer-name-function
  1167. (lambda(x) (buffer-name buffer)))
  1168. (compile (ada-quote-cmd
  1169. (concat ,pre-cmd
  1170. command " "
  1171. (mapconcat 'identity switches " "))))))
  1172. ))
  1173. ;; Tight integration should force the tty mode
  1174. (if (and (string-match "gvd" (comint-arguments cmd 0 0))
  1175. ada-tight-gvd-integration
  1176. (not (string-match "--tty" cmd)))
  1177. (setq cmd (concat cmd "--tty")))
  1178. (if (and (string-match "jdb" (comint-arguments cmd 0 0))
  1179. (boundp 'jdb))
  1180. (funcall (symbol-function 'jdb) cmd)
  1181. (gdb cmd))
  1182. ;; Restore the standard fset command (or for instance C-U M-x shell
  1183. ;; wouldn't work anymore
  1184. (fset 'comint-exec old-comint-exec)
  1185. ;; Send post-commands to the debugger
  1186. (process-send-string (get-buffer-process (current-buffer)) post-cmd)
  1187. ;; Move to the end of the debugger buffer, so that it is automatically
  1188. ;; scrolled from then on.
  1189. (goto-char (point-max))
  1190. ;; Display both the source window and the debugger window (the former
  1191. ;; above the latter). No need to show the debugger window unless it
  1192. ;; is going to have some relevant information.
  1193. (if (or (not (string-match "gvd" (comint-arguments cmd 0 0)))
  1194. (string-match "--tty" cmd))
  1195. (split-window-below))
  1196. (switch-to-buffer buffer)
  1197. )))
  1198. (defun ada-reread-prj-file (&optional filename)
  1199. "Reread either the current project, or FILENAME if non-nil.
  1200. If FILENAME is non-nil, set it as current project."
  1201. (interactive "P")
  1202. (if (not filename)
  1203. (setq filename ada-prj-default-project-file))
  1204. (ada-parse-prj-file filename)
  1205. (ada-select-prj-file filename))
  1206. ;; ------ Private routines
  1207. (defun ada-xref-current (file &optional ali-file-name)
  1208. "Update the cross-references for FILE.
  1209. This in fact recompiles FILE to create ALI-FILE-NAME.
  1210. This function returns the name of the file that was recompiled to generate
  1211. the cross-reference information. Note that the ali file can then be deduced
  1212. by replacing the file extension with `.ali'."
  1213. ;; kill old buffer
  1214. (if (and ali-file-name
  1215. (get-file-buffer ali-file-name))
  1216. (kill-buffer (get-file-buffer ali-file-name)))
  1217. (let* ((name (ada-convert-file-name file))
  1218. (body-name (or (ada-get-body-name name) name)))
  1219. ;; Always recompile the body when we can. We thus temporarily switch to a
  1220. ;; buffer than contains the body of the unit
  1221. (save-excursion
  1222. (let ((body-visible (find-buffer-visiting body-name))
  1223. process)
  1224. (if body-visible
  1225. (set-buffer body-visible)
  1226. (find-file body-name))
  1227. ;; Execute the compilation. Note that we must wait for the end of the
  1228. ;; process, or the ALI file would still not be available.
  1229. ;; Unfortunately, the underlying `compile' command that we use is
  1230. ;; asynchronous.
  1231. (ada-compile-current)
  1232. (setq process (get-buffer-process "*compilation*"))
  1233. (while (and process
  1234. (not (equal (process-status process) 'exit)))
  1235. (sit-for 1))
  1236. ;; remove the buffer for the body if it wasn't there before
  1237. (unless body-visible
  1238. (kill-buffer (find-buffer-visiting body-name)))
  1239. ))
  1240. body-name))
  1241. (defun ada-find-file-in-dir (file dir-list)
  1242. "Search for FILE in DIR-LIST."
  1243. (let (found)
  1244. (while (and (not found) dir-list)
  1245. (set 'found (concat (file-name-as-directory (car dir-list))
  1246. (file-name-nondirectory file)))
  1247. (unless (file-exists-p found)
  1248. (set 'found nil))
  1249. (set 'dir-list (cdr dir-list)))
  1250. found))
  1251. (defun ada-find-ali-file-in-dir (file)
  1252. "Find the ali file FILE, searching obj_dir for the current project.
  1253. Adds build_dir in front of the search path to conform to gnatmake's behavior,
  1254. and the standard runtime location at the end."
  1255. (ada-find-file-in-dir file (ada-xref-get-obj-dir-field)))
  1256. (defun ada-find-src-file-in-dir (file)
  1257. "Find the source file FILE, searching src_dir for the current project.
  1258. Adds the standard runtime location at the end of the search path to conform
  1259. to gnatmake's behavior."
  1260. (ada-find-file-in-dir file (ada-xref-get-src-dir-field)))
  1261. (defun ada-get-ali-file-name (file)
  1262. "Create the ali file name for the Ada file FILE.
  1263. The file is searched for in every directory shown in the obj_dir lines of
  1264. the project file."
  1265. ;; This function has to handle the special case of non-standard
  1266. ;; file names (i.e. not .adb or .ads)
  1267. ;; The trick is the following:
  1268. ;; 1- replace the extension of the current file with .ali,
  1269. ;; and look for this file
  1270. ;; 2- If this file is found:
  1271. ;; grep the "^U" lines, and make sure we are not reading the
  1272. ;; .ali file for a spec file. If we are, go to step 3.
  1273. ;; 3- If the file is not found or step 2 failed:
  1274. ;; find the name of the "other file", ie the body, and look
  1275. ;; for its associated .ali file by substituting the extension
  1276. ;;
  1277. ;; We must also handle the case of separate packages and subprograms:
  1278. ;; 4- If no ali file was found, we try to modify the file name by removing
  1279. ;; everything after the last '-' or '.' character, so as to get the
  1280. ;; ali file for the parent unit. If we found an ali file, we check that
  1281. ;; it indeed contains the definition for the separate entity by checking
  1282. ;; the 'D' lines. This is done repeatedly, in case the direct parent is
  1283. ;; also a separate.
  1284. (with-current-buffer (get-file-buffer file)
  1285. (let ((short-ali-file-name
  1286. (concat (file-name-sans-extension (file-name-nondirectory file))
  1287. ".ali"))
  1288. ali-file-name
  1289. is-spec)
  1290. ;; If we have a non-standard file name, and this is a spec, we first
  1291. ;; look for the .ali file of the body, since this is the one that
  1292. ;; contains the most complete information. If not found, we will do what
  1293. ;; we can with the .ali file for the spec...
  1294. (if (not (string= (file-name-extension file) "ads"))
  1295. (let ((specs ada-spec-suffixes))
  1296. (while specs
  1297. (if (string-match (concat (regexp-quote (car specs)) "$")
  1298. file)
  1299. (set 'is-spec t))
  1300. (set 'specs (cdr specs)))))
  1301. (if is-spec
  1302. (set 'ali-file-name
  1303. (ada-find-ali-file-in-dir
  1304. (concat (file-name-sans-extension
  1305. (file-name-nondirectory
  1306. (ada-other-file-name)))
  1307. ".ali"))))
  1308. (setq ali-file-name
  1309. (or ali-file-name
  1310. ;; Else we take the .ali file associated with the unit
  1311. (ada-find-ali-file-in-dir short-ali-file-name)
  1312. ;; else we did not find the .ali file Second chance: in case
  1313. ;; the files do not have standard names (such as for instance
  1314. ;; file_s.ada and file_b.ada), try to go to the other file
  1315. ;; and look for its ali file
  1316. (ada-find-ali-file-in-dir
  1317. (concat (file-name-sans-extension
  1318. (file-name-nondirectory (ada-other-file-name)))
  1319. ".ali"))
  1320. ;; If we still don't have an ali file, try to get the one
  1321. ;; from the parent unit, in case we have a separate entity.
  1322. (let ((parent-name (file-name-sans-extension
  1323. (file-name-nondirectory file))))
  1324. (while (and (not ali-file-name)
  1325. (string-match "^\\(.*\\)[.-][^.-]*" parent-name))
  1326. (set 'parent-name (match-string 1 parent-name))
  1327. (set 'ali-file-name (ada-find-ali-file-in-dir
  1328. (concat parent-name ".ali")))
  1329. )
  1330. ali-file-name)))
  1331. ;; If still not found, try to recompile the file
  1332. (if (not ali-file-name)
  1333. ;; Recompile only if the user asked for this, and search the ali
  1334. ;; filename again. We avoid a possible infinite recursion by
  1335. ;; temporarily disabling the automatic compilation.
  1336. (if ada-xref-create-ali
  1337. (setq ali-file-name
  1338. (concat (file-name-sans-extension (ada-xref-current file))
  1339. ".ali"))
  1340. (error "`.ali' file not found; recompile your source file"))
  1341. ;; same if the .ali file is too old and we must recompile it
  1342. (if (and (file-newer-than-file-p file ali-file-name)
  1343. ada-xref-create-ali)
  1344. (ada-xref-current file ali-file-name)))
  1345. ;; Always return the correct absolute file name
  1346. (expand-file-name ali-file-name))
  1347. ))
  1348. (defun ada-get-ada-file-name (file original-file)
  1349. "Create the complete file name (+directory) for FILE.
  1350. The original file (where the user was) is ORIGINAL-FILE.
  1351. Search in project file for possible paths."
  1352. (save-excursion
  1353. ;; If the buffer for original-file, use it to get the values from the
  1354. ;; project file, otherwise load the file and its project file
  1355. (let ((buffer (get-file-buffer original-file)))
  1356. (if buffer
  1357. (set-buffer buffer)
  1358. (find-file original-file)))
  1359. ;; we choose the first possible completion and we
  1360. ;; return the absolute file name
  1361. (let ((filename (ada-find-src-file-in-dir file)))
  1362. (if filename
  1363. (expand-file-name filename)
  1364. (signal 'error-file-not-found (file-name-nondirectory file)))
  1365. )))
  1366. (defun ada-find-file-number-in-ali (file)
  1367. "Return the file number for FILE in the associated ali file."
  1368. (set-buffer (ada-get-ali-buffer file))
  1369. (goto-char (point-min))
  1370. (let ((begin (re-search-forward "^D")))
  1371. (beginning-of-line)
  1372. (re-search-forward (concat "^D " (file-name-nondirectory file)))
  1373. (count-lines begin (point))))
  1374. (defun ada-read-identifier (pos)
  1375. "Return the identlist around POS and switch to the .ali buffer.
  1376. The returned list represents the entity, and can be manipulated through the
  1377. macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
  1378. ;; If at end of buffer (e.g the buffer is empty), error
  1379. (if (>= (point) (point-max))
  1380. (error "No identifier on point"))
  1381. ;; goto first character of the identifier/operator (skip backward < and >
  1382. ;; since they are part of multiple character operators
  1383. (goto-char pos)
  1384. (skip-chars-backward "a-zA-Z0-9_<>")
  1385. ;; check if it really is an identifier
  1386. (if (ada-in-comment-p)
  1387. (error "Inside comment"))
  1388. (let (identifier identlist)
  1389. ;; Just in front of a string => we could have an operator declaration,
  1390. ;; as in "+", "-", ..
  1391. (if (= (char-after) ?\")
  1392. (forward-char 1))
  1393. ;; if looking at an operator
  1394. ;; This is only true if:
  1395. ;; - the symbol is +, -, ...
  1396. ;; - the symbol is made of letters, and not followed by _ or a letter
  1397. (if (and (looking-at ada-operator-re)
  1398. (or (not (= (char-syntax (char-after)) ?w))
  1399. (not (or (= (char-syntax (char-after (match-end 0))) ?w)
  1400. (= (char-after (match-end 0)) ?_)))))
  1401. (progn
  1402. (if (and (= (char-before) ?\")
  1403. (= (char-after (+ (length (match-string 0)) (point))) ?\"))
  1404. (forward-char -1))
  1405. (set 'identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
  1406. (if (ada-in-string-p)
  1407. (error "Inside string or character constant"))
  1408. (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
  1409. (error "No cross-reference available for reserved keyword"))
  1410. (if (looking-at "[a-zA-Z0-9_]+")
  1411. (set 'identifier (match-string 0))
  1412. (error "No identifier around")))
  1413. ;; Build the identlist
  1414. (set 'identlist (ada-make-identlist))
  1415. (ada-set-name identlist (downcase identifier))
  1416. (ada-set-line identlist
  1417. (number-to-string (count-lines 1 (point))))
  1418. (ada-set-column identlist
  1419. (number-to-string (1+ (current-column))))
  1420. (ada-set-file identlist (buffer-file-name))
  1421. identlist
  1422. ))
  1423. (defun ada-get-all-references (identlist)
  1424. "Complete IDENTLIST with definition file and places where it is referenced.
  1425. Information is extracted from the ali file."
  1426. (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
  1427. declaration-found)
  1428. (set-buffer ali-buffer)
  1429. (goto-char (point-min))
  1430. (ada-set-on-declaration identlist nil)
  1431. ;; First attempt: we might already be on the declaration of the identifier
  1432. ;; We want to look for the declaration only in a definite interval (after
  1433. ;; the "^X ..." line for the current file, and before the next "^X" line
  1434. (if (re-search-forward
  1435. (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
  1436. nil t)
  1437. (let ((bound (save-excursion (re-search-forward "^X " nil t))))
  1438. (set 'declaration-found
  1439. (re-search-forward
  1440. (concat "^" (ada-line-of identlist)
  1441. "." (ada-column-of identlist)
  1442. "[ *]" (ada-name-of identlist)
  1443. "[{\[\(<= ]?\\(.*\\)$") bound t))
  1444. (if declaration-found
  1445. (ada-set-on-declaration identlist t))
  1446. ))
  1447. ;; If declaration is still nil, then we were not on a declaration, and
  1448. ;; have to fall back on other algorithms
  1449. (unless declaration-found
  1450. ;; Since we already know the number of the file, search for a direct
  1451. ;; reference to it
  1452. (goto-char (point-min))
  1453. (set 'declaration-found t)
  1454. (ada-set-ali-index
  1455. identlist
  1456. (number-to-string (ada-find-file-number-in-ali
  1457. (ada-file-of identlist))))
  1458. (unless (re-search-forward (concat (ada-ali-index-of identlist)
  1459. "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
  1460. (ada-line-of identlist)
  1461. "[^etpzkd<>=^]"
  1462. (ada-column-of identlist) "\\>")
  1463. nil t)
  1464. ;; if we did not find it, it may be because the first reference
  1465. ;; is not required to have a 'unit_number|' item included.
  1466. ;; Or maybe we are already on the declaration...
  1467. (unless (re-search-forward
  1468. (concat
  1469. "^[0-9]+.[0-9]+[ *]"
  1470. (ada-name-of identlist)
  1471. "[ <{=\(\[]\\(.\\|\n\\.\\)*\\<"
  1472. (ada-line-of identlist)
  1473. "[^0-9]"
  1474. (ada-column-of identlist) "\\>")
  1475. nil t)
  1476. ;; If still not found, then either the declaration is unknown
  1477. ;; or the source file has been modified since the ali file was
  1478. ;; created
  1479. (set 'declaration-found nil)
  1480. )
  1481. )
  1482. ;; Last check to be completely sure we have found the correct line (the
  1483. ;; ali might not be up to date for instance)
  1484. (if declaration-found
  1485. (progn
  1486. (beginning-of-line)
  1487. ;; while we have a continuation line, go up one line
  1488. (while (looking-at "^\\.")
  1489. (forward-line -1)
  1490. (beginning-of-line))
  1491. (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
  1492. (ada-name-of identlist) "[ <{=\(\[]"))
  1493. (set 'declaration-found nil))))
  1494. ;; Still no success ! The ali file must be too old, and we need to
  1495. ;; use a basic algorithm based on guesses. Note that this only happens
  1496. ;; if the user does not want us to automatically recompile files
  1497. ;; automatically
  1498. (unless declaration-found
  1499. (if (ada-xref-find-in-modified-ali identlist)
  1500. (set 'declaration-found t)
  1501. ;; No more idea to find the declaration. Give up
  1502. (progn
  1503. (kill-buffer ali-buffer)
  1504. (error "No declaration of %s found" (ada-name-of identlist))
  1505. )))
  1506. )
  1507. ;; Now that we have found a suitable line in the .ali file, get the
  1508. ;; information available
  1509. (beginning-of-line)
  1510. (if declaration-found
  1511. (let ((current-line (buffer-substring
  1512. (point) (point-at-eol))))
  1513. (save-excursion
  1514. (forward-line 1)
  1515. (beginning-of-line)
  1516. (while (looking-at "^\\.\\(.*\\)")
  1517. (set 'current-line (concat current-line (match-string 1)))
  1518. (forward-line 1))
  1519. )
  1520. (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
  1521. ;; If we can find the file
  1522. (condition-case err
  1523. (ada-set-declare-file
  1524. identlist
  1525. (ada-get-ada-file-name (match-string 1)
  1526. (ada-file-of identlist)))
  1527. ;; Else clean up the ali file
  1528. (error-file-not-found
  1529. (signal (car err) (cdr err)))
  1530. (error
  1531. (kill-buffer ali-buffer)
  1532. (error (error-message-string err)))
  1533. ))
  1534. (ada-set-references identlist current-line)
  1535. ))
  1536. ))
  1537. (defun ada-xref-find-in-modified-ali (identlist)
  1538. "Find the matching position for IDENTLIST in the current ali buffer.
  1539. This function is only called when the file was not up-to-date, so we need
  1540. to make some guesses.
  1541. This function is disabled for operators, and only works for identifiers."
  1542. (unless (= (string-to-char (ada-name-of identlist)) ?\")
  1543. (progn
  1544. (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
  1545. (my-regexp (concat "[ *]"
  1546. (regexp-quote (ada-name-of identlist)) " "))
  1547. (line-ada "--")
  1548. (col-ada "--")
  1549. (line-ali 0)
  1550. (len 0)
  1551. (choice 0)
  1552. (ali-buffer (current-buffer)))
  1553. (goto-char (point-max))
  1554. (while (re-search-backward my-regexp nil t)
  1555. (save-excursion
  1556. (set 'line-ali (count-lines 1 (point)))
  1557. (beginning-of-line)
  1558. ;; have a look at the line and column numbers
  1559. (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
  1560. (progn
  1561. (setq line-ada (match-string 1))
  1562. (setq col-ada (match-string 2)))
  1563. (setq line-ada "--")
  1564. (setq col-ada "--")
  1565. )
  1566. ;; construct a list with the file names and the positions within
  1567. (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
  1568. (add-to-list
  1569. 'declist (list line-ali (match-string 1) line-ada col-ada))
  1570. )
  1571. )
  1572. )
  1573. ;; how many possible declarations have we found ?
  1574. (setq len (length declist))
  1575. (cond
  1576. ;; none => error
  1577. ((= len 0)
  1578. (kill-buffer (current-buffer))
  1579. (error "No declaration of %s recorded in .ali file"
  1580. (ada-name-of identlist)))
  1581. ;; one => should be the right one
  1582. ((= len 1)
  1583. (goto-char (point-min))
  1584. (forward-line (1- (caar declist))))
  1585. ;; more than one => display choice list
  1586. (t
  1587. (save-window-excursion
  1588. (with-output-to-temp-buffer "*choice list*"
  1589. (princ "Identifier is overloaded and Xref information is not up to date.\n")
  1590. (princ "Possible declarations are:\n\n")
  1591. (princ " no. in file at line col\n")
  1592. (princ " --- --------------------- ---- ----\n")
  1593. (let ((counter 0))
  1594. (while (< counter len)
  1595. (princ (format " %2d) %-21s %4s %4s\n"
  1596. (1+ counter)
  1597. (ada-get-ada-file-name
  1598. (nth 1 (nth counter declist))
  1599. (ada-file-of identlist))
  1600. (nth 2 (nth counter declist))
  1601. (nth 3 (nth counter declist))
  1602. ))
  1603. (setq counter (1+ counter))
  1604. ) ; end of while
  1605. ) ; end of let
  1606. ) ; end of with-output-to ...
  1607. (setq choice nil)
  1608. (while (or
  1609. (not choice)
  1610. (not (integerp choice))
  1611. (< choice 1)
  1612. (> choice len))
  1613. (setq choice
  1614. (string-to-number
  1615. (read-from-minibuffer "Enter No. of your choice: "))))
  1616. )
  1617. (set-buffer ali-buffer)
  1618. (goto-char (point-min))
  1619. (forward-line (1- (car (nth (1- choice) declist))))
  1620. ))))))
  1621. (defun ada-find-in-ali (identlist &optional other-frame)
  1622. "Look in the .ali file for the definition of the identifier in IDENTLIST.
  1623. If OTHER-FRAME is non-nil, and `ada-xref-other-buffer' is non-nil,
  1624. opens a new window to show the declaration."
  1625. (ada-get-all-references identlist)
  1626. (let ((ali-line (ada-references-of identlist))
  1627. (locations nil)
  1628. (start 0)
  1629. file line col)
  1630. ;; Note: in some cases, an entity can have multiple references to the
  1631. ;; bodies (this is for instance the case for a separate subprogram, that
  1632. ;; has a reference both to the stub and to the real body).
  1633. ;; In that case, we simply go to each one in turn.
  1634. ;; Get all the possible locations
  1635. (string-match "^\\([0-9]+\\)[a-zA-Z+*]\\([0-9]+\\)[ *]" ali-line)
  1636. (set 'locations (list (list (match-string 1 ali-line) ;; line
  1637. (match-string 2 ali-line) ;; column
  1638. (ada-declare-file-of identlist))))
  1639. (while (string-match "\\([0-9]+\\)[bc]\\(<[^>]+>\\)?\\([0-9]+\\)"
  1640. ali-line start)
  1641. (setq line (match-string 1 ali-line)
  1642. col (match-string 3 ali-line)
  1643. start (match-end 3))
  1644. ;; it there was a file number in the same line
  1645. ;; Make sure we correctly handle the case where the first file reference
  1646. ;; on the line is the type reference.
  1647. ;; 1U2 T(2|2r3) 34r23
  1648. (if (string-match (concat "[^{(<0-9]\\([0-9]+\\)|\\([^|bc]+\\)?"
  1649. (match-string 0 ali-line))
  1650. ali-line)
  1651. (let ((file-number (match-string 1 ali-line)))
  1652. (goto-char (point-min))
  1653. (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
  1654. (string-to-number file-number))
  1655. (set 'file (match-string 1))
  1656. )
  1657. ;; Else get the nearest file
  1658. (set 'file (ada-declare-file-of identlist)))
  1659. (set 'locations (append locations (list (list line col file)))))
  1660. ;; Add the specs at the end again, so that from the last body we go to
  1661. ;; the specs
  1662. (set 'locations (append locations (list (car locations))))
  1663. ;; Find the new location we want to go to.
  1664. ;; If we are on none of the locations listed, we simply go to the specs.
  1665. (setq line (caar locations)
  1666. col (nth 1 (car locations))
  1667. file (nth 2 (car locations)))
  1668. (while locations
  1669. (if (and (string= (caar locations) (ada-line-of identlist))
  1670. (string= (nth 1 (car locations)) (ada-column-of identlist))
  1671. (string= (file-name-nondirectory (nth 2 (car locations)))
  1672. (file-name-nondirectory (ada-file-of identlist))))
  1673. (setq locations (cadr locations)
  1674. line (car locations)
  1675. col (nth 1 locations)
  1676. file (nth 2 locations)
  1677. locations nil)
  1678. (set 'locations (cdr locations))))
  1679. ;; Find the file in the source path
  1680. (set 'file (ada-get-ada-file-name file (ada-file-of identlist)))
  1681. ;; Kill the .ali buffer
  1682. (kill-buffer (current-buffer))
  1683. ;; Now go to the buffer
  1684. (ada-xref-change-buffer file
  1685. (string-to-number line)
  1686. (1- (string-to-number col))
  1687. identlist
  1688. other-frame)
  1689. ))
  1690. (defun ada-find-in-src-path (identlist &optional other-frame)
  1691. "More general function for cross-references.
  1692. This function should be used when the standard algorithm that parses the
  1693. .ali file has failed, either because that file was too old or even did not
  1694. exist.
  1695. This function attempts to find the possible declarations for the identifier
  1696. anywhere in the object path.
  1697. This command requires the external `egrep' program to be available.
  1698. This works well when one is using an external library and wants to find
  1699. the declaration and documentation of the subprograms one is using."
  1700. ;; FIXME: what does this function do?
  1701. (let (list
  1702. (dirs (ada-xref-get-obj-dir-field))
  1703. (regexp (concat "[ *]" (ada-name-of identlist)))
  1704. line column
  1705. choice
  1706. file)
  1707. ;; Do the grep in all the directories. We do multiple shell
  1708. ;; commands instead of one in case there is no .ali file in one
  1709. ;; of the directory and the shell stops because of that.
  1710. (with-current-buffer (get-buffer-create "*grep*")
  1711. (while dirs
  1712. (insert (shell-command-to-string
  1713. (concat
  1714. "grep -E -i -h "
  1715. (shell-quote-argument (concat "^X|" regexp "( |$)"))
  1716. " "
  1717. (shell-quote-argument (file-name-as-directory (car dirs)))
  1718. "*.ali")))
  1719. (set 'dirs (cdr dirs)))
  1720. ;; Now parse the output
  1721. (set 'case-fold-search t)
  1722. (goto-char (point-min))
  1723. (while (re-search-forward regexp nil t)
  1724. (save-excursion
  1725. (beginning-of-line)
  1726. (if (not (= (char-after) ?X))
  1727. (progn
  1728. (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
  1729. (setq line (match-string 1)
  1730. column (match-string 2))
  1731. (re-search-backward "^X [0-9]+ \\(.*\\)$")
  1732. (set 'file (list (match-string 1) line column))
  1733. ;; There could be duplicate choices, because of the structure
  1734. ;; of the .ali files
  1735. (unless (member file list)
  1736. (set 'list (append list (list file))))))))
  1737. ;; Current buffer is still "*grep*"
  1738. (kill-buffer "*grep*")
  1739. )
  1740. ;; Now display the list of possible matches
  1741. (cond
  1742. ;; No choice found => Error
  1743. ((null list)
  1744. (error "No cross-reference found, please recompile your file"))
  1745. ;; Only one choice => Do the cross-reference
  1746. ((= (length list) 1)
  1747. (set 'file (ada-find-src-file-in-dir (caar list)))
  1748. (if file
  1749. (ada-xref-change-buffer file
  1750. (string-to-number (nth 1 (car list)))
  1751. (string-to-number (nth 2 (car list)))
  1752. identlist
  1753. other-frame)
  1754. (error "%s not found in src_dir" (caar list)))
  1755. (message "This is only a (good) guess at the cross-reference.")
  1756. )
  1757. ;; Else, ask the user
  1758. (t
  1759. (save-window-excursion
  1760. (with-output-to-temp-buffer "*choice list*"
  1761. (princ "Identifier is overloaded and Xref information is not up to date.\n")
  1762. (princ "Possible declarations are:\n\n")
  1763. (princ " no. in file at line col\n")
  1764. (princ " --- --------------------- ---- ----\n")
  1765. (let ((counter 0))
  1766. (while (< counter (length list))
  1767. (princ (format " %2d) %-21s %4s %4s\n"
  1768. (1+ counter)
  1769. (nth 0 (nth counter list))
  1770. (nth 1 (nth counter list))
  1771. (nth 2 (nth counter list))
  1772. ))
  1773. (setq counter (1+ counter))
  1774. )))
  1775. (setq choice nil)
  1776. (while (or (not choice)
  1777. (not (integerp choice))
  1778. (< choice 1)
  1779. (> choice (length list)))
  1780. (setq choice
  1781. (string-to-number
  1782. (read-from-minibuffer "Enter No. of your choice: "))))
  1783. )
  1784. (set 'choice (1- choice))
  1785. (kill-buffer "*choice list*")
  1786. (set 'file (ada-find-src-file-in-dir (car (nth choice list))))
  1787. (if file
  1788. (ada-xref-change-buffer file
  1789. (string-to-number (nth 1 (nth choice list)))
  1790. (string-to-number (nth 2 (nth choice list)))
  1791. identlist
  1792. other-frame)
  1793. (signal 'error-file-not-found (car (nth choice list))))
  1794. (message "This is only a (good) guess at the cross-reference.")
  1795. ))))
  1796. (defun ada-xref-change-buffer
  1797. (file line column identlist &optional other-frame)
  1798. "Select and display FILE, at LINE and COLUMN.
  1799. If we do not end on the same identifier as IDENTLIST, find the
  1800. closest match. Kills the .ali buffer at the end.
  1801. If OTHER-FRAME is non-nil, creates a new frame to show the file."
  1802. (let (declaration-buffer)
  1803. ;; Select and display the destination buffer
  1804. (if ada-xref-other-buffer
  1805. (if other-frame
  1806. (find-file-other-frame file)
  1807. (set 'declaration-buffer (find-file-noselect file))
  1808. (set-buffer declaration-buffer)
  1809. (switch-to-buffer-other-window declaration-buffer)
  1810. )
  1811. (find-file file)
  1812. )
  1813. ;; move the cursor to the correct position
  1814. (push-mark)
  1815. (goto-char (point-min))
  1816. (forward-line (1- line))
  1817. (move-to-column column)
  1818. ;; If we are not on the identifier, the ali file was not up-to-date.
  1819. ;; Try to find the nearest position where the identifier is found,
  1820. ;; this is probably the right one.
  1821. (unless (looking-at (ada-name-of identlist))
  1822. (ada-xref-search-nearest (ada-name-of identlist)))
  1823. ))
  1824. (defun ada-xref-search-nearest (name)
  1825. "Search for NAME nearest to the position recorded in the Xref file.
  1826. Return the position of the declaration in the buffer, or nil if not found."
  1827. (let ((orgpos (point))
  1828. (newpos nil)
  1829. (diff nil))
  1830. (goto-char (point-max))
  1831. ;; loop - look for all declarations of name in this file
  1832. (while (search-backward name nil t)
  1833. ;; check if it really is a complete Ada identifier
  1834. (if (and
  1835. (not (save-excursion
  1836. (goto-char (match-end 0))
  1837. (looking-at "_")))
  1838. (not (ada-in-string-or-comment-p))
  1839. (or
  1840. ;; variable declaration ?
  1841. (save-excursion
  1842. (skip-chars-forward "a-zA-Z_0-9" )
  1843. (ada-goto-next-non-ws)
  1844. (looking-at ":[^=]"))
  1845. ;; procedure, function, task or package declaration ?
  1846. (save-excursion
  1847. (ada-goto-previous-word)
  1848. (looking-at "\\<[pP][rR][oO][cC][eE][dD][uU][rR][eE]\\>\\|\\<[fF][uU][nN][cC][tT][iI][oO][nN]\\>\\|\\<[tT][yY][pP][eE]\\>\\|\\<[tT][aA][sS][kK]\\>\\|\\<[pP][aA][cC][kK][aA][gG][eE]\\>\\|\\<[bB][oO][dD][yY]\\>"))))
  1849. ;; check if it is nearer than the ones before if any
  1850. (if (or (not diff)
  1851. (< (abs (- (point) orgpos)) diff))
  1852. (progn
  1853. (setq newpos (point)
  1854. diff (abs (- newpos orgpos))))))
  1855. )
  1856. (if newpos
  1857. (progn
  1858. (message "ATTENTION: this declaration is only a (good) guess ...")
  1859. (goto-char newpos))
  1860. nil)))
  1861. ;; Find the parent library file of the current file
  1862. (defun ada-goto-parent ()
  1863. "Go to the parent library file."
  1864. (interactive)
  1865. (ada-require-project-file)
  1866. (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
  1867. (unit-name nil)
  1868. (body-name nil)
  1869. (ali-name nil))
  1870. (with-current-buffer buffer
  1871. (goto-char (point-min))
  1872. (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
  1873. (setq unit-name (match-string 1))
  1874. (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
  1875. (progn
  1876. (kill-buffer buffer)
  1877. (error "No parent unit !"))
  1878. (setq unit-name (match-string 1 unit-name))
  1879. )
  1880. ;; look for the file name for the parent unit specification
  1881. (goto-char (point-min))
  1882. (re-search-forward (concat "^W " unit-name
  1883. "%s[ \t]+\\([^ \t]+\\)[ \t]+"
  1884. "\\([^ \t\n]+\\)"))
  1885. (setq body-name (match-string 1))
  1886. (setq ali-name (match-string 2))
  1887. (kill-buffer buffer)
  1888. )
  1889. (setq ali-name (ada-find-ali-file-in-dir ali-name))
  1890. (save-excursion
  1891. ;; Tries to open the new ali file to find the spec file
  1892. (if ali-name
  1893. (progn
  1894. (find-file ali-name)
  1895. (goto-char (point-min))
  1896. (re-search-forward (concat "^U " unit-name "%s[ \t]+"
  1897. "\\([^ \t]+\\)"))
  1898. (setq body-name (match-string 1))
  1899. (kill-buffer (current-buffer))
  1900. )
  1901. )
  1902. )
  1903. (find-file body-name)
  1904. ))
  1905. (defun ada-make-filename-from-adaname (adaname)
  1906. "Determine the filename in which ADANAME is found.
  1907. This is a GNAT specific function that uses gnatkrunch."
  1908. (let ((krunch-buf (generate-new-buffer "*gkrunch*"))
  1909. (cross-prefix (plist-get (cdr (ada-xref-current-project)) 'cross_prefix)))
  1910. (with-current-buffer krunch-buf
  1911. ;; send adaname to external process `gnatkr'.
  1912. ;; Add a dummy extension, since gnatkr versions have two different
  1913. ;; behaviors depending on the version:
  1914. ;; Up to 3.15: "AA.BB.CC" => aa-bb-cc
  1915. ;; After: "AA.BB.CC" => aa-bb.cc
  1916. (call-process (concat cross-prefix "gnatkr") nil krunch-buf nil
  1917. (concat adaname ".adb") ada-krunch-args)
  1918. ;; fetch output of that process
  1919. (setq adaname (buffer-substring
  1920. (point-min)
  1921. (progn
  1922. (goto-char (point-min))
  1923. (end-of-line)
  1924. (point))))
  1925. ;; Remove the extra extension we added above
  1926. (setq adaname (substring adaname 0 -4))
  1927. (kill-buffer krunch-buf)))
  1928. adaname
  1929. )
  1930. (defun ada-make-body-gnatstub (&optional interactive)
  1931. "Create an Ada package body in the current buffer.
  1932. This function uses the `gnat stub' program to create the body.
  1933. This function typically is to be hooked into `ff-file-created-hook'.
  1934. If INTERACTIVE is nil, assume this is called from `ff-file-created-hook'."
  1935. (interactive "p")
  1936. (ada-require-project-file)
  1937. ;; If not interactive, assume we are being called from
  1938. ;; ff-file-created-hook. Then the current buffer is for the body
  1939. ;; file, but we will create a new one after gnat stub runs
  1940. (unless interactive
  1941. (set-buffer-modified-p nil)
  1942. (kill-buffer (current-buffer)))
  1943. (save-some-buffers nil nil)
  1944. ;; Make sure the current buffer is the spec, so gnat stub gets the
  1945. ;; right package parameter (this might not be the case if for
  1946. ;; instance the user was asked for a project file)
  1947. (unless (buffer-file-name (car (buffer-list)))
  1948. (set-buffer (cadr (buffer-list))))
  1949. ;; Call the external process
  1950. (let* ((project-plist (cdr (ada-xref-current-project)))
  1951. (gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
  1952. (gpr-file (plist-get project-plist 'gpr_file))
  1953. (filename (buffer-file-name (car (buffer-list))))
  1954. (output (concat (file-name-sans-extension filename) ".adb"))
  1955. (cross-prefix (plist-get project-plist 'cross_prefix))
  1956. (gnatstub-cmd (concat cross-prefix "gnat stub"
  1957. (if (not (string= gpr-file ""))
  1958. (concat " -P\"" gpr-file "\""))
  1959. " " gnatstub-opts " " filename))
  1960. (buffer (get-buffer-create "*gnat stub*")))
  1961. (with-current-buffer buffer
  1962. (compilation-minor-mode 1)
  1963. (erase-buffer)
  1964. (insert gnatstub-cmd)
  1965. (newline)
  1966. )
  1967. (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
  1968. ;; clean up the output
  1969. (if (file-exists-p output)
  1970. (progn
  1971. (find-file output)
  1972. (kill-buffer buffer))
  1973. ;; file not created; display the error message
  1974. (display-buffer buffer))))
  1975. (defun ada-xref-initialize ()
  1976. "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
  1977. For instance, it creates the gnat-specific menus, sets some hooks for
  1978. `find-file'."
  1979. (remove-hook 'ff-file-created-hook 'ada-make-body) ; from global hook
  1980. (remove-hook 'ff-file-created-hook 'ada-make-body t) ; from local hook
  1981. (add-hook 'ff-file-created-hook 'ada-make-body-gnatstub nil t)
  1982. ;; Completion for file names in the mini buffer should ignore .ali files
  1983. (add-to-list 'completion-ignored-extensions ".ali")
  1984. (ada-xref-update-project-menu)
  1985. )
  1986. ;; ----- Add to ada-mode-hook ---------------------------------------------
  1987. ;; This must be done before initializing the Ada menu.
  1988. (add-hook 'ada-mode-hook 'ada-xref-initialize)
  1989. ;; Define a new error type
  1990. (put 'error-file-not-found
  1991. 'error-conditions
  1992. '(error ada-mode-errors error-file-not-found))
  1993. (put 'error-file-not-found
  1994. 'error-message
  1995. "File not found in src-dir (check project file): ")
  1996. (provide 'ada-xref)
  1997. ;;; ada-xref.el ends here