ada-xref.el 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365
  1. ;; ada-xref.el --- for lookup and completion in Ada mode
  2. ;; Copyright (C) 1994-2015 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. (setq ada-xref-runtime-library-specs-path
  280. (reverse ada-xref-runtime-library-specs-path))
  281. (setq 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. (setq list (append list (file-name-all-completions string (car dirs)))))
  482. (setq 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-set-default-project-file (file)
  540. "Set FILE as the current project file."
  541. (interactive "fProject file:")
  542. (ada-parse-prj-file file)
  543. (ada-select-prj-file file))
  544. ;; ------ Handling the project file -----------------------------
  545. (defun ada-prj-find-prj-file (&optional no-user-question)
  546. "Find the project file associated with the current buffer.
  547. If the buffer is not in Ada mode, or not associated with a file,
  548. return `ada-prj-default-project-file'. Otherwise, search for a file with
  549. the same base name as the Ada file, but extension given by
  550. `ada-prj-file-extension' (default .adp). If not found, search for *.adp
  551. in the current directory; if several are found, and NO-USER-QUESTION
  552. is non-nil, prompt the user to select one. If none are found, return
  553. \"default.adp\"."
  554. (let (selected)
  555. (if (not (and (derived-mode-p 'ada-mode)
  556. buffer-file-name))
  557. ;; Not in an Ada buffer, or current buffer not associated
  558. ;; with a file (for instance an emerge buffer)
  559. (setq selected nil)
  560. ;; other cases: use a more complex algorithm
  561. (let* ((current-file (buffer-file-name))
  562. (first-choice (concat
  563. (file-name-sans-extension current-file)
  564. ada-prj-file-extension))
  565. (dir (file-name-directory current-file))
  566. (prj-files (directory-files
  567. dir t
  568. (concat ".*" (regexp-quote
  569. ada-prj-file-extension) "$")))
  570. (choice nil))
  571. (cond
  572. ((file-exists-p first-choice)
  573. ;; filename.adp
  574. (setq selected first-choice))
  575. ((= (length prj-files) 1)
  576. ;; Exactly one project file was found in the current directory
  577. (setq selected (car prj-files)))
  578. ((and (> (length prj-files) 1) (not no-user-question))
  579. ;; multiple project files in current directory, ask the user
  580. (save-window-excursion
  581. (with-output-to-temp-buffer "*choice list*"
  582. (princ "There are more than one possible project file.\n")
  583. (princ "Which one should we use ?\n\n")
  584. (princ " no. file name \n")
  585. (princ " --- ------------------------\n")
  586. (let ((counter 1))
  587. (while (<= counter (length prj-files))
  588. (princ (format " %2d) %s\n"
  589. counter
  590. (nth (1- counter) prj-files)))
  591. (setq counter (1+ counter))
  592. ))) ; end of with-output-to ...
  593. (setq choice nil)
  594. (while (or
  595. (not choice)
  596. (not (integerp choice))
  597. (< choice 1)
  598. (> choice (length prj-files)))
  599. (setq choice (string-to-number
  600. (read-from-minibuffer "Enter No. of your choice: "))))
  601. (setq selected (nth (1- choice) prj-files))))
  602. ((= (length prj-files) 0)
  603. ;; No project file in the current directory; ask user
  604. (unless (or no-user-question (not ada-always-ask-project))
  605. (setq ada-last-prj-file
  606. (read-file-name
  607. (concat "project file [" ada-last-prj-file "]:")
  608. nil ada-last-prj-file))
  609. (unless (string= ada-last-prj-file "")
  610. (setq selected ada-last-prj-file))))
  611. )))
  612. (or selected "default.adp")
  613. ))
  614. (defun ada-default-prj-properties ()
  615. "Return the default project properties list with the current buffer as main."
  616. (let ((file (buffer-file-name nil)))
  617. (list
  618. ;; variable name alphabetical order
  619. 'ada_project_path (or (getenv "ADA_PROJECT_PATH") "")
  620. 'ada_project_path_sep ada-prj-ada-project-path-sep
  621. 'bind_opt ada-prj-default-bind-opt
  622. 'build_dir default-directory
  623. 'casing (if (listp ada-case-exception-file)
  624. ada-case-exception-file
  625. (list ada-case-exception-file))
  626. 'check_cmd (list ada-prj-default-check-cmd) ;; FIXME: should not a list
  627. 'comp_cmd (list ada-prj-default-comp-cmd) ;; FIXME: should not a list
  628. 'comp_opt ada-prj-default-comp-opt
  629. 'cross_prefix ""
  630. 'debug_cmd (concat ada-prj-default-debugger
  631. " ${main}" (if ada-on-ms-windows ".exe")) ;; FIXME: don't need .exe?
  632. 'debug_post_cmd (list nil)
  633. 'debug_pre_cmd (list (concat ada-cd-command " ${build_dir}"))
  634. 'gnatmake_opt ada-prj-default-gnatmake-opt
  635. 'gnatfind_opt ada-prj-gnatfind-switches
  636. 'gpr_file ada-prj-default-gpr-file
  637. 'link_opt ada-prj-default-link-opt
  638. 'main (if file
  639. (file-name-nondirectory
  640. (file-name-sans-extension file))
  641. "")
  642. 'make_cmd (list ada-prj-default-make-cmd) ;; FIXME: should not a list
  643. 'obj_dir (list ".")
  644. 'remote_machine ""
  645. 'run_cmd (list (concat "./${main}" (if ada-on-ms-windows ".exe")))
  646. ;; FIXME: should not a list
  647. ;; FIXME: don't need .exe?
  648. 'src_dir (list ".")
  649. )))
  650. (defun ada-parse-prj-file (prj-file)
  651. "Read PRJ-FILE, set project properties in `ada-xref-project-files'."
  652. (let ((project (ada-default-prj-properties)))
  653. (setq prj-file (expand-file-name prj-file))
  654. (if (string= (file-name-extension prj-file) "gpr")
  655. (setq project (ada-gnat-parse-gpr project prj-file))
  656. (setq project (ada-parse-prj-file-1 prj-file project))
  657. )
  658. ;; Store the project properties
  659. (if (assoc prj-file ada-xref-project-files)
  660. (setcdr (assoc prj-file ada-xref-project-files) project)
  661. (add-to-list 'ada-xref-project-files (cons prj-file project)))
  662. (ada-xref-update-project-menu)
  663. ))
  664. (defun ada-parse-prj-file-1 (prj-file project)
  665. "Parse the Ada mode project file PRJ-FILE, set project properties in PROJECT.
  666. Return new value of PROJECT."
  667. (let ((ada-buffer (current-buffer))
  668. ;; fields that are lists or otherwise require special processing
  669. ada_project_path casing comp_cmd check_cmd
  670. debug_pre_cmd debug_post_cmd gpr_file make_cmd obj_dir src_dir run_cmd)
  671. ;; Give users a chance to use compiler-specific project file formats
  672. (let ((buffer (run-hook-with-args-until-success
  673. 'ada-load-project-hook prj-file)))
  674. (unless buffer
  675. ;; we load the project file with no warnings; if it does not
  676. ;; exist, we stay in the Ada buffer; no project variable
  677. ;; settings will be found. That works for the default
  678. ;; "default.adp", which does not exist as a file.
  679. (setq buffer (find-file-noselect prj-file nil)))
  680. (set-buffer buffer))
  681. (widen)
  682. (goto-char (point-min))
  683. ;; process each line
  684. (while (not (eobp))
  685. ;; ignore lines that don't have the format "name=value", put
  686. ;; 'name', 'value' in match-string.
  687. (if (looking-at "^\\([^=\n]+\\)=\\(.*\\)")
  688. (cond
  689. ;; FIXME: strip trailing spaces
  690. ;; variable name alphabetical order
  691. ((string= (match-string 1) "ada_project_path")
  692. (add-to-list 'ada_project_path
  693. (expand-file-name
  694. (substitute-in-file-name (match-string 2)))))
  695. ((string= (match-string 1) "build_dir")
  696. (setq project
  697. (plist-put project 'build_dir
  698. (file-name-as-directory (match-string 2)))))
  699. ((string= (match-string 1) "casing")
  700. (add-to-list 'casing
  701. (expand-file-name (substitute-in-file-name (match-string 2)))))
  702. ((string= (match-string 1) "check_cmd")
  703. (add-to-list 'check_cmd (match-string 2)))
  704. ((string= (match-string 1) "comp_cmd")
  705. (add-to-list 'comp_cmd (match-string 2)))
  706. ((string= (match-string 1) "debug_post_cmd")
  707. (add-to-list 'debug_post_cmd (match-string 2)))
  708. ((string= (match-string 1) "debug_pre_cmd")
  709. (add-to-list 'debug_pre_cmd (match-string 2)))
  710. ((string= (match-string 1) "gpr_file")
  711. ;; expand now; path is relative to Emacs project file
  712. (setq gpr_file (expand-file-name (match-string 2))))
  713. ((string= (match-string 1) "make_cmd")
  714. (add-to-list 'make_cmd (match-string 2)))
  715. ((string= (match-string 1) "obj_dir")
  716. (add-to-list 'obj_dir
  717. (file-name-as-directory
  718. (expand-file-name (match-string 2)))))
  719. ((string= (match-string 1) "run_cmd")
  720. (add-to-list 'run_cmd (match-string 2)))
  721. ((string= (match-string 1) "src_dir")
  722. (add-to-list 'src_dir
  723. (file-name-as-directory
  724. (expand-file-name (match-string 2)))))
  725. (t
  726. ;; any other field in the file is just copied
  727. (setq project (plist-put project
  728. (intern (match-string 1))
  729. (match-string 2))))))
  730. (forward-line 1))
  731. ;; done reading file
  732. ;; back to the user buffer
  733. (set-buffer ada-buffer)
  734. ;; process accumulated lists
  735. (if ada_project_path
  736. (let ((sep (plist-get project 'ada_project_path_sep)))
  737. (setq ada_project_path (reverse ada_project_path))
  738. (setq ada_project_path (mapconcat 'identity ada_project_path sep))
  739. (setq project (plist-put project 'ada_project_path ada_project_path))
  740. ;; env var needed now for ada-gnat-parse-gpr
  741. (setenv "ADA_PROJECT_PATH" ada_project_path)))
  742. (if debug_post_cmd (setq project (plist-put project 'debug_post_cmd (reverse debug_post_cmd))))
  743. (if debug_pre_cmd (setq project (plist-put project 'debug_pre_cmd (reverse debug_pre_cmd))))
  744. (if casing (setq project (plist-put project 'casing (reverse casing))))
  745. (if check_cmd (setq project (plist-put project 'check_cmd (reverse check_cmd))))
  746. (if comp_cmd (setq project (plist-put project 'comp_cmd (reverse comp_cmd))))
  747. (if make_cmd (setq project (plist-put project 'make_cmd (reverse make_cmd))))
  748. (if run_cmd (setq project (plist-put project 'run_cmd (reverse run_cmd))))
  749. (if gpr_file
  750. (progn
  751. (setq project (ada-gnat-parse-gpr project gpr_file))
  752. ;; append Ada source and object directories to others from Emacs project file
  753. (setq src_dir (append (plist-get project 'src_dir) src_dir))
  754. (setq obj_dir (append (plist-get project 'obj_dir) obj_dir))
  755. (setq ada-xref-runtime-library-specs-path '()
  756. ada-xref-runtime-library-ali-path '()))
  757. )
  758. ;; FIXME: gnatpath.exe doesn't output the runtime libraries, so always call ada-initialize-runtime-library
  759. ;; if using a gpr_file, the runtime library directories are
  760. ;; included in src_dir and obj_dir; otherwise they are in the
  761. ;; 'runtime-library' variables.
  762. ;; FIXME: always append to src_dir, obj_dir
  763. (ada-initialize-runtime-library (or (ada-xref-get-project-field 'cross_prefix) ""))
  764. ;;)
  765. (if obj_dir (setq project (plist-put project 'obj_dir (reverse obj_dir))))
  766. (if src_dir (setq project (plist-put project 'src_dir (reverse src_dir))))
  767. project
  768. ))
  769. (defun ada-select-prj-file (file)
  770. "Select FILE as the current project file."
  771. (interactive)
  772. (setq ada-prj-default-project-file (expand-file-name file))
  773. (let ((casing (ada-xref-get-project-field 'casing)))
  774. (if casing
  775. (progn
  776. ;; FIXME: use ada-get-absolute-dir here
  777. (setq ada-case-exception-file casing)
  778. (ada-case-read-exceptions))))
  779. (let ((ada_project_path (ada-xref-get-project-field 'ada_project_path)))
  780. (if ada_project_path
  781. ;; FIXME: use ada-get-absolute-dir, mapconcat here
  782. (setenv "ADA_PROJECT_PATH" ada_project_path)))
  783. (setq compilation-search-path (ada-xref-get-src-dir-field))
  784. (setq ada-search-directories-internal
  785. ;; FIXME: why do we need directory-file-name here?
  786. (append (mapcar 'directory-file-name compilation-search-path)
  787. ada-search-directories))
  788. ;; return t, for decent display in message buffer when called interactively
  789. t)
  790. (defun ada-find-references (&optional pos arg local-only)
  791. "Find all references to the entity under POS.
  792. Calls gnatfind to find the references.
  793. If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved.
  794. If LOCAL-ONLY is non-nil, only declarations in the current file are returned."
  795. (interactive "d\nP")
  796. (ada-require-project-file)
  797. (let* ((identlist (ada-read-identifier pos))
  798. (alifile (ada-get-ali-file-name (ada-file-of identlist)))
  799. (process-environment (ada-set-environment)))
  800. (set-buffer (get-file-buffer (ada-file-of identlist)))
  801. ;; if the file is more recent than the executable
  802. (if (or (buffer-modified-p (current-buffer))
  803. (file-newer-than-file-p (ada-file-of identlist) alifile))
  804. (ada-find-any-references (ada-name-of identlist)
  805. (ada-file-of identlist)
  806. nil nil local-only arg)
  807. (ada-find-any-references (ada-name-of identlist)
  808. (ada-file-of identlist)
  809. (ada-line-of identlist)
  810. (ada-column-of identlist) local-only arg)))
  811. )
  812. (defun ada-find-local-references (&optional pos arg)
  813. "Find all references to the entity under POS.
  814. Calls `gnatfind' to find the references.
  815. If ARG is non-nil, the contents of the old *gnatfind* buffer is preserved."
  816. (interactive "d\nP")
  817. (ada-find-references pos arg t))
  818. (defconst ada-gnatfind-buffer-name "*gnatfind*")
  819. (defun ada-find-any-references
  820. (entity &optional file line column local-only append)
  821. "Search for references to any entity whose name is ENTITY.
  822. ENTITY was first found the location given by FILE, LINE and COLUMN.
  823. If LOCAL-ONLY is non-nil, then list only the references in FILE,
  824. which is much faster.
  825. If APPEND is non-nil, then append the output of the command to the
  826. existing buffer `*gnatfind*', if there is one."
  827. (interactive "sEntity name: ")
  828. (ada-require-project-file)
  829. ;; Prepare the gnatfind command. Note that we must protect the quotes
  830. ;; around operators, so that they are correctly handled and can be
  831. ;; processed (gnatfind \"+\":...).
  832. (let* ((quote-entity
  833. (if (= (aref entity 0) ?\")
  834. (if ada-on-ms-windows
  835. (concat "\\\"" (substring entity 1 -1) "\\\"")
  836. (concat "'\"" (substring entity 1 -1) "\"'"))
  837. entity))
  838. (switches (ada-xref-get-project-field 'gnatfind_opt))
  839. ;; FIXME: use gpr_file
  840. (cross-prefix (ada-xref-get-project-field 'cross_prefix))
  841. (command (concat cross-prefix "gnat find " switches " "
  842. quote-entity
  843. (if file (concat ":" (file-name-nondirectory file)))
  844. (if line (concat ":" line))
  845. (if column (concat ":" column))
  846. (if local-only (concat " " (file-name-nondirectory file)))
  847. ))
  848. old-contents)
  849. ;; If a project file is defined, use it
  850. (if (and ada-prj-default-project-file
  851. (not (string= ada-prj-default-project-file "")))
  852. (if (string-equal (file-name-extension ada-prj-default-project-file)
  853. "gpr")
  854. (setq command (concat command " -P\"" ada-prj-default-project-file "\""))
  855. (setq command (concat command " -p\"" ada-prj-default-project-file "\""))))
  856. (if (and append (get-buffer ada-gnatfind-buffer-name))
  857. (with-current-buffer "*gnatfind*"
  858. (setq old-contents (buffer-string))))
  859. (let ((compilation-error "reference"))
  860. (compilation-start command 'compilation-mode (lambda (_mode) ada-gnatfind-buffer-name)))
  861. ;; Hide the "Compilation" menu
  862. (with-current-buffer ada-gnatfind-buffer-name
  863. (local-unset-key [menu-bar compilation-menu])
  864. (if old-contents
  865. (progn
  866. (goto-char 1)
  867. (setq buffer-read-only nil)
  868. (insert old-contents)
  869. (setq buffer-read-only t)
  870. (goto-char (point-max)))))
  871. )
  872. )
  873. (defalias 'ada-change-prj (symbol-function 'ada-set-default-project-file))
  874. ;; ----- Identifier Completion --------------------------------------------
  875. (defun ada-complete-identifier (pos)
  876. "Try to complete the identifier around POS, using compiler cross-reference information."
  877. (interactive "d")
  878. (ada-require-project-file)
  879. ;; Initialize function-local variables and jump to the .ali buffer
  880. ;; Note that for regexp search is case insensitive too
  881. (let* ((curbuf (current-buffer))
  882. (identlist (ada-read-identifier pos))
  883. (sofar (concat "^[0-9]+[a-zA-Z][0-9]+[ *]\\("
  884. (regexp-quote (ada-name-of identlist))
  885. "[a-zA-Z0-9_]*\\)"))
  886. (completed nil)
  887. (symalist nil))
  888. ;; Open the .ali file
  889. (set-buffer (ada-get-ali-buffer (buffer-file-name)))
  890. (goto-char (point-max))
  891. ;; build an alist of possible completions
  892. (while (re-search-backward sofar nil t)
  893. (setq symalist (cons (cons (match-string 1) nil) symalist)))
  894. (setq completed (try-completion "" symalist))
  895. ;; kills .ali buffer
  896. (kill-buffer nil)
  897. ;; deletes the incomplete identifier in the buffer
  898. (set-buffer curbuf)
  899. (looking-at "[a-zA-Z0-9_]+")
  900. (replace-match "")
  901. ;; inserts the completed symbol
  902. (insert completed)
  903. ))
  904. ;; ----- Cross-referencing ----------------------------------------
  905. (defun ada-point-and-xref ()
  906. "Jump to the declaration of the entity below the cursor."
  907. (interactive)
  908. (mouse-set-point last-input-event)
  909. (ada-goto-declaration (point)))
  910. (defun ada-point-and-xref-body ()
  911. "Jump to the body of the entity under the cursor."
  912. (interactive)
  913. (mouse-set-point last-input-event)
  914. (ada-goto-body (point)))
  915. (defun ada-goto-body (pos &optional other-frame)
  916. "Display the body of the entity around POS.
  917. OTHER-FRAME non-nil means display in another frame.
  918. If the entity doesn't have a body, display its declaration.
  919. As a side effect, the buffer for the declaration is also open."
  920. (interactive "d")
  921. (ada-goto-declaration pos other-frame)
  922. ;; Temporarily force the display in the same buffer, since we
  923. ;; already changed previously
  924. (let ((ada-xref-other-buffer nil))
  925. (ada-goto-declaration (point) nil)))
  926. (defun ada-goto-declaration (pos &optional other-frame)
  927. "Display the declaration of the identifier around POS.
  928. The declaration is shown in another buffer if `ada-xref-other-buffer' is
  929. non-nil.
  930. If OTHER-FRAME is non-nil, display the cross-reference in another frame."
  931. (interactive "d")
  932. (ada-require-project-file)
  933. (push-mark pos)
  934. (ada-xref-push-pos (buffer-file-name) pos)
  935. ;; First try the standard algorithm by looking into the .ali file, but if
  936. ;; that file was too old or even did not exist, try to look in the whole
  937. ;; object path for a possible location.
  938. (let ((identlist (ada-read-identifier pos)))
  939. (condition-case err
  940. (ada-find-in-ali identlist other-frame)
  941. ;; File not found: print explicit error message
  942. (ada-error-file-not-found
  943. (message (concat (error-message-string err)
  944. (nthcdr 1 err))))
  945. (error
  946. (let ((ali-file (ada-get-ali-file-name (ada-file-of identlist))))
  947. ;; If the ALI file was up-to-date, then we probably have a predefined
  948. ;; entity, whose references are not given by GNAT
  949. (if (and (file-exists-p ali-file)
  950. (file-newer-than-file-p ali-file (ada-file-of identlist)))
  951. (message "No cross-reference found -- may be a predefined entity.")
  952. ;; Else, look in every ALI file, except if the user doesn't want that
  953. (if ada-xref-search-with-egrep
  954. (ada-find-in-src-path identlist other-frame)
  955. (message "Cross-referencing information is not up-to-date; please recompile.")
  956. )))))))
  957. (defun ada-goto-declaration-other-frame (pos)
  958. "Display the declaration of the identifier around POS.
  959. The declaration is shown in another frame if `ada-xref-other-buffer' is
  960. non-nil."
  961. (interactive "d")
  962. (ada-goto-declaration pos t))
  963. (defun ada-remote (command)
  964. "Return the remote version of COMMAND, or COMMAND if remote_machine is nil."
  965. (let ((machine (ada-xref-get-project-field 'remote_machine)))
  966. (if (or (not machine) (string= machine ""))
  967. command
  968. (format "%s %s '(%s)'"
  969. remote-shell-program
  970. machine
  971. command))))
  972. (defun ada-get-absolute-dir-list (dir-list root-dir)
  973. "Return the list of absolute directories found in DIR-LIST.
  974. If a directory is a relative directory, ROOT-DIR is prepended.
  975. Project and environment variables are substituted."
  976. (mapcar (lambda (x) (expand-file-name x (ada-treat-cmd-string root-dir))) dir-list))
  977. (defun ada-set-environment ()
  978. "Prepare an environment for Ada compilation.
  979. This returns a new value to use for `process-environment',
  980. but does not actually put it into use.
  981. It modifies the source path and object path with the values found in the
  982. project file."
  983. (let ((include (getenv "ADA_INCLUDE_PATH"))
  984. (objects (getenv "ADA_OBJECTS_PATH"))
  985. (build-dir (ada-xref-get-project-field 'build_dir)))
  986. (if include
  987. (setq include (concat path-separator include)))
  988. (if objects
  989. (setq objects (concat path-separator objects)))
  990. (cons
  991. (concat "ADA_INCLUDE_PATH="
  992. (mapconcat (lambda(x) (expand-file-name x build-dir))
  993. (ada-xref-get-project-field 'src_dir)
  994. path-separator)
  995. include)
  996. (cons
  997. (concat "ADA_OBJECTS_PATH="
  998. (mapconcat (lambda(x) (expand-file-name x build-dir))
  999. (ada-xref-get-project-field 'obj_dir)
  1000. path-separator)
  1001. objects)
  1002. process-environment))))
  1003. (defun ada-compile-application (&optional arg)
  1004. "Compile the application, using the command found in the project file.
  1005. If ARG is not nil, ask for user confirmation."
  1006. (interactive "P")
  1007. (ada-require-project-file)
  1008. (let ((cmd (ada-xref-get-project-field 'make_cmd))
  1009. (process-environment (ada-set-environment))
  1010. (compilation-scroll-output t))
  1011. (setq compilation-search-path (ada-xref-get-src-dir-field))
  1012. ;; If no project file was found, ask the user
  1013. (unless cmd
  1014. (setq cmd '("") arg t))
  1015. ;; Make a single command from the list of commands, including the
  1016. ;; commands to run it on a remote machine.
  1017. (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
  1018. (if (or ada-xref-confirm-compile arg)
  1019. (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
  1020. ;; Insert newlines so as to separate the name of the commands to run
  1021. ;; and the output of the commands. This doesn't work with cmdproxy.exe,
  1022. ;; which gets confused by newline characters.
  1023. (if (not (string-match ".exe" shell-file-name))
  1024. (setq cmd (concat cmd "\n\n")))
  1025. (compile (ada-quote-cmd cmd))))
  1026. (defun ada-set-main-compile-application ()
  1027. "Set main project variable to current buffer, build main."
  1028. (interactive)
  1029. (ada-require-project-file)
  1030. (let* ((file (buffer-file-name (current-buffer)))
  1031. main)
  1032. (if (not file)
  1033. (error "No file for current buffer")
  1034. (setq main
  1035. (if file
  1036. (file-name-nondirectory
  1037. (file-name-sans-extension file))
  1038. ""))
  1039. (ada-xref-set-project-field 'main main)
  1040. (ada-compile-application))))
  1041. (defun ada-compile-current (&optional arg prj-field)
  1042. "Recompile the current file.
  1043. If ARG is non-nil, ask for user confirmation of the command.
  1044. PRJ-FIELD is the name of the field to use in the project file to get the
  1045. command, and should be either `comp_cmd' (default) or `check_cmd'."
  1046. (interactive "P")
  1047. (ada-require-project-file)
  1048. (let* ((field (if prj-field prj-field 'comp_cmd))
  1049. (cmd (ada-xref-get-project-field field))
  1050. (process-environment (ada-set-environment))
  1051. (compilation-scroll-output t))
  1052. (unless cmd
  1053. (setq cmd '("") arg t))
  1054. ;; Make a single command from the list of commands, including the
  1055. ;; commands to run it on a remote machine.
  1056. (setq cmd (ada-remote (mapconcat 'identity cmd ada-command-separator)))
  1057. ;; If no project file was found, ask the user
  1058. (if (or ada-xref-confirm-compile arg)
  1059. (setq cmd (read-from-minibuffer "enter command to compile: " cmd)))
  1060. (compile (ada-quote-cmd cmd))))
  1061. (defun ada-check-current (&optional arg)
  1062. "Check the current file for syntax errors.
  1063. If ARG is non-nil, ask for user confirmation of the command."
  1064. (interactive "P")
  1065. (ada-compile-current arg 'check_cmd))
  1066. (defun ada-run-application (&optional arg)
  1067. "Run the application.
  1068. If ARG is non-nil, ask for user confirmation."
  1069. (interactive)
  1070. (ada-require-project-file)
  1071. (let ((machine (ada-xref-get-project-field 'cross_prefix)))
  1072. (if (and machine (not (string= machine "")))
  1073. (error "This feature is not supported yet for cross environments")))
  1074. (let ((command (ada-xref-get-project-field 'run_cmd)))
  1075. ;; Guess the command if it wasn't specified
  1076. (if (not command)
  1077. (setq command (list (file-name-sans-extension (buffer-name)))))
  1078. ;; Modify the command to run remotely
  1079. (setq command (ada-remote (mapconcat 'identity command
  1080. ada-command-separator)))
  1081. ;; Ask for the arguments to the command if required
  1082. (if (or ada-xref-confirm-compile arg)
  1083. (setq command (read-from-minibuffer "Enter command to execute: "
  1084. command)))
  1085. ;; Run the command
  1086. (with-current-buffer (get-buffer-create "*run*")
  1087. (setq buffer-read-only nil)
  1088. (erase-buffer)
  1089. (start-process "run" (current-buffer) shell-file-name
  1090. "-c" command)
  1091. (comint-mode)
  1092. ;; Set these two variables to their default values, since otherwise
  1093. ;; the output buffer is scrolled so that only the last output line
  1094. ;; is visible at the top of the buffer.
  1095. (set (make-local-variable 'scroll-step) 0)
  1096. (set (make-local-variable 'scroll-conservatively) 0)
  1097. )
  1098. (display-buffer "*run*")
  1099. ;; change to buffer *run* for interactive programs
  1100. (other-window 1)
  1101. (switch-to-buffer "*run*")
  1102. ))
  1103. (defun ada-gdb-application (&optional arg executable-name)
  1104. "Start the debugger on the application.
  1105. If ARG is non-nil, ask the user to confirm the command.
  1106. EXECUTABLE-NAME, if non-nil, is debugged instead of the file specified in the
  1107. project file."
  1108. (interactive "P")
  1109. (ada-require-project-file)
  1110. (let ((buffer (current-buffer))
  1111. cmd pre-cmd post-cmd)
  1112. (setq cmd (if executable-name
  1113. (concat ada-prj-default-debugger " " executable-name)
  1114. (ada-xref-get-project-field 'debug_cmd))
  1115. pre-cmd (ada-xref-get-project-field 'debug_pre_cmd)
  1116. post-cmd (ada-xref-get-project-field 'debug_post_cmd))
  1117. ;; If the command was not given in the project file, start a bare gdb
  1118. (if (not cmd)
  1119. (setq cmd (concat ada-prj-default-debugger
  1120. " "
  1121. (or executable-name
  1122. (file-name-sans-extension (buffer-file-name))))))
  1123. ;; For gvd, add an extra switch so that the Emacs window is completely
  1124. ;; swallowed inside the Gvd one
  1125. (if (and ada-tight-gvd-integration
  1126. (string-match "^[^ \t]*gvd" cmd))
  1127. ;; Start a new frame, so that when gvd exists we do not kill Emacs
  1128. ;; We make sure that gvd swallows the new frame, not the one the
  1129. ;; user has been using until now
  1130. ;; The frame is made invisible initially, so that GtkPlug gets a
  1131. ;; chance to fully manage it. Then it works fine with Enlightenment
  1132. ;; as well
  1133. (let ((frame (make-frame '((visibility . nil)))))
  1134. (setq cmd (concat
  1135. cmd " --editor-window="
  1136. (cdr (assoc 'outer-window-id (frame-parameters frame)))))
  1137. (select-frame frame)))
  1138. ;; Add a -fullname switch
  1139. ;; Use the remote machine
  1140. (setq cmd (ada-remote (concat cmd " -fullname ")))
  1141. ;; Ask for confirmation if required
  1142. (if (or arg ada-xref-confirm-compile)
  1143. (setq cmd (read-from-minibuffer "enter command to debug: " cmd)))
  1144. (let ((old-comint-exec (symbol-function 'comint-exec)))
  1145. ;; Do not add -fullname, since we can have a 'rsh' command in front.
  1146. ;; FIXME: This is evil but luckily a nop under Emacs-21.3.50 ! -stef
  1147. (fset 'gud-gdb-massage-args (lambda (_file args) args))
  1148. (setq pre-cmd (mapconcat 'identity pre-cmd ada-command-separator))
  1149. (if (not (equal pre-cmd ""))
  1150. (setq pre-cmd (concat pre-cmd ada-command-separator)))
  1151. (setq post-cmd (mapconcat 'identity post-cmd "\n"))
  1152. (if post-cmd
  1153. (setq post-cmd (concat post-cmd "\n")))
  1154. ;; Temporarily replaces the definition of `comint-exec' so that we
  1155. ;; can execute commands before running gdb.
  1156. ;; FIXME: This is evil and not temporary !!! -stef
  1157. (fset 'comint-exec
  1158. `(lambda (buffer name command startfile switches)
  1159. (let (compilation-buffer-name-function)
  1160. (save-excursion
  1161. (setq compilation-buffer-name-function
  1162. (lambda(x) (buffer-name buffer)))
  1163. (compile (ada-quote-cmd
  1164. (concat ,pre-cmd
  1165. command " "
  1166. (mapconcat 'identity switches " "))))))
  1167. ))
  1168. ;; Tight integration should force the tty mode
  1169. (if (and (string-match "gvd" (comint-arguments cmd 0 0))
  1170. ada-tight-gvd-integration
  1171. (not (string-match "--tty" cmd)))
  1172. (setq cmd (concat cmd "--tty")))
  1173. (if (and (string-match "jdb" (comint-arguments cmd 0 0))
  1174. (boundp 'jdb))
  1175. (funcall (symbol-function 'jdb) cmd)
  1176. (gdb cmd))
  1177. ;; Restore the standard fset command (or for instance C-U M-x shell
  1178. ;; wouldn't work anymore
  1179. (fset 'comint-exec old-comint-exec)
  1180. ;; Send post-commands to the debugger
  1181. (process-send-string (get-buffer-process (current-buffer)) post-cmd)
  1182. ;; Move to the end of the debugger buffer, so that it is automatically
  1183. ;; scrolled from then on.
  1184. (goto-char (point-max))
  1185. ;; Display both the source window and the debugger window (the former
  1186. ;; above the latter). No need to show the debugger window unless it
  1187. ;; is going to have some relevant information.
  1188. (if (or (not (string-match "gvd" (comint-arguments cmd 0 0)))
  1189. (string-match "--tty" cmd))
  1190. (split-window-below))
  1191. (switch-to-buffer buffer)
  1192. )))
  1193. (defun ada-reread-prj-file (&optional filename)
  1194. "Reread either the current project, or FILENAME if non-nil.
  1195. If FILENAME is non-nil, set it as current project."
  1196. (interactive "P")
  1197. (if (not filename)
  1198. (setq filename ada-prj-default-project-file))
  1199. (ada-parse-prj-file filename)
  1200. (ada-select-prj-file filename))
  1201. ;; ------ Private routines
  1202. (defun ada-xref-current (file &optional ali-file-name)
  1203. "Update the cross-references for FILE.
  1204. This in fact recompiles FILE to create ALI-FILE-NAME.
  1205. This function returns the name of the file that was recompiled to generate
  1206. the cross-reference information. Note that the ali file can then be deduced
  1207. by replacing the file extension with `.ali'."
  1208. ;; kill old buffer
  1209. (if (and ali-file-name
  1210. (get-file-buffer ali-file-name))
  1211. (kill-buffer (get-file-buffer ali-file-name)))
  1212. (let* ((name (convert-standard-filename file))
  1213. (body-name (or (ada-get-body-name name) name)))
  1214. ;; Always recompile the body when we can. We thus temporarily switch to a
  1215. ;; buffer than contains the body of the unit
  1216. (save-excursion
  1217. (let ((body-visible (find-buffer-visiting body-name))
  1218. process)
  1219. (if body-visible
  1220. (set-buffer body-visible)
  1221. (find-file body-name))
  1222. ;; Execute the compilation. Note that we must wait for the end of the
  1223. ;; process, or the ALI file would still not be available.
  1224. ;; Unfortunately, the underlying `compile' command that we use is
  1225. ;; asynchronous.
  1226. (ada-compile-current)
  1227. (setq process (get-buffer-process "*compilation*"))
  1228. (while (and process
  1229. (not (equal (process-status process) 'exit)))
  1230. (sit-for 1))
  1231. ;; remove the buffer for the body if it wasn't there before
  1232. (unless body-visible
  1233. (kill-buffer (find-buffer-visiting body-name)))
  1234. ))
  1235. body-name))
  1236. (defun ada-find-file-in-dir (file dir-list)
  1237. "Search for FILE in DIR-LIST."
  1238. (let (found)
  1239. (while (and (not found) dir-list)
  1240. (setq found (concat (file-name-as-directory (car dir-list))
  1241. (file-name-nondirectory file)))
  1242. (unless (file-exists-p found)
  1243. (setq found nil))
  1244. (setq dir-list (cdr dir-list)))
  1245. found))
  1246. (defun ada-find-ali-file-in-dir (file)
  1247. "Find the ali file FILE, searching obj_dir for the current project.
  1248. Adds build_dir in front of the search path to conform to gnatmake's behavior,
  1249. and the standard runtime location at the end."
  1250. (ada-find-file-in-dir file (ada-xref-get-obj-dir-field)))
  1251. (defun ada-find-src-file-in-dir (file)
  1252. "Find the source file FILE, searching src_dir for the current project.
  1253. Adds the standard runtime location at the end of the search path to conform
  1254. to gnatmake's behavior."
  1255. (ada-find-file-in-dir file (ada-xref-get-src-dir-field)))
  1256. (defun ada-get-ali-file-name (file)
  1257. "Create the ali file name for the Ada file FILE.
  1258. The file is searched for in every directory shown in the obj_dir lines of
  1259. the project file."
  1260. ;; This function has to handle the special case of non-standard
  1261. ;; file names (i.e. not .adb or .ads)
  1262. ;; The trick is the following:
  1263. ;; 1- replace the extension of the current file with .ali,
  1264. ;; and look for this file
  1265. ;; 2- If this file is found:
  1266. ;; grep the "^U" lines, and make sure we are not reading the
  1267. ;; .ali file for a spec file. If we are, go to step 3.
  1268. ;; 3- If the file is not found or step 2 failed:
  1269. ;; find the name of the "other file", ie the body, and look
  1270. ;; for its associated .ali file by substituting the extension
  1271. ;;
  1272. ;; We must also handle the case of separate packages and subprograms:
  1273. ;; 4- If no ali file was found, we try to modify the file name by removing
  1274. ;; everything after the last '-' or '.' character, so as to get the
  1275. ;; ali file for the parent unit. If we found an ali file, we check that
  1276. ;; it indeed contains the definition for the separate entity by checking
  1277. ;; the 'D' lines. This is done repeatedly, in case the direct parent is
  1278. ;; also a separate.
  1279. (with-current-buffer (get-file-buffer file)
  1280. (let ((short-ali-file-name (concat (file-name-base file) ".ali"))
  1281. ali-file-name
  1282. is-spec)
  1283. ;; If we have a non-standard file name, and this is a spec, we first
  1284. ;; look for the .ali file of the body, since this is the one that
  1285. ;; contains the most complete information. If not found, we will do what
  1286. ;; we can with the .ali file for the spec...
  1287. (if (not (string= (file-name-extension file) "ads"))
  1288. (let ((specs ada-spec-suffixes))
  1289. (while specs
  1290. (if (string-match (concat (regexp-quote (car specs)) "$")
  1291. file)
  1292. (setq is-spec t))
  1293. (setq specs (cdr specs)))))
  1294. (if is-spec
  1295. (setq ali-file-name
  1296. (ada-find-ali-file-in-dir
  1297. (concat (file-name-base (ada-other-file-name)) ".ali"))))
  1298. (setq ali-file-name
  1299. (or ali-file-name
  1300. ;; Else we take the .ali file associated with the unit
  1301. (ada-find-ali-file-in-dir short-ali-file-name)
  1302. ;; else we did not find the .ali file Second chance: in case
  1303. ;; the files do not have standard names (such as for instance
  1304. ;; file_s.ada and file_b.ada), try to go to the other file
  1305. ;; and look for its ali file
  1306. (ada-find-ali-file-in-dir
  1307. (concat (file-name-base (ada-other-file-name)) ".ali"))
  1308. ;; If we still don't have an ali file, try to get the one
  1309. ;; from the parent unit, in case we have a separate entity.
  1310. (let ((parent-name (file-name-base file)))
  1311. (while (and (not ali-file-name)
  1312. (string-match "^\\(.*\\)[.-][^.-]*" parent-name))
  1313. (setq parent-name (match-string 1 parent-name))
  1314. (setq ali-file-name (ada-find-ali-file-in-dir
  1315. (concat parent-name ".ali")))
  1316. )
  1317. ali-file-name)))
  1318. ;; If still not found, try to recompile the file
  1319. (if (not ali-file-name)
  1320. ;; Recompile only if the user asked for this, and search the ali
  1321. ;; filename again. We avoid a possible infinite recursion by
  1322. ;; temporarily disabling the automatic compilation.
  1323. (if ada-xref-create-ali
  1324. (setq ali-file-name
  1325. (concat (file-name-sans-extension (ada-xref-current file))
  1326. ".ali"))
  1327. (error "`.ali' file not found; recompile your source file"))
  1328. ;; same if the .ali file is too old and we must recompile it
  1329. (if (and (file-newer-than-file-p file ali-file-name)
  1330. ada-xref-create-ali)
  1331. (ada-xref-current file ali-file-name)))
  1332. ;; Always return the correct absolute file name
  1333. (expand-file-name ali-file-name))
  1334. ))
  1335. (defun ada-get-ada-file-name (file original-file)
  1336. "Create the complete file name (+directory) for FILE.
  1337. The original file (where the user was) is ORIGINAL-FILE.
  1338. Search in project file for possible paths."
  1339. (save-excursion
  1340. ;; If the buffer for original-file, use it to get the values from the
  1341. ;; project file, otherwise load the file and its project file
  1342. (let ((buffer (get-file-buffer original-file)))
  1343. (if buffer
  1344. (set-buffer buffer)
  1345. (find-file original-file)))
  1346. ;; we choose the first possible completion and we
  1347. ;; return the absolute file name
  1348. (let ((filename (ada-find-src-file-in-dir file)))
  1349. (if filename
  1350. (expand-file-name filename)
  1351. (signal 'ada-error-file-not-found (file-name-nondirectory file)))
  1352. )))
  1353. (defun ada-find-file-number-in-ali (file)
  1354. "Return the file number for FILE in the associated ali file."
  1355. (set-buffer (ada-get-ali-buffer file))
  1356. (goto-char (point-min))
  1357. (let ((begin (re-search-forward "^D")))
  1358. (beginning-of-line)
  1359. (re-search-forward (concat "^D " (file-name-nondirectory file)))
  1360. (count-lines begin (point))))
  1361. (defun ada-read-identifier (pos)
  1362. "Return the identlist around POS and switch to the .ali buffer.
  1363. The returned list represents the entity, and can be manipulated through the
  1364. macros `ada-name-of', `ada-line-of', `ada-column-of', `ada-file-of',..."
  1365. ;; If at end of buffer (e.g the buffer is empty), error
  1366. (if (>= (point) (point-max))
  1367. (error "No identifier on point"))
  1368. ;; goto first character of the identifier/operator (skip backward < and >
  1369. ;; since they are part of multiple character operators
  1370. (goto-char pos)
  1371. (skip-chars-backward "a-zA-Z0-9_<>")
  1372. ;; check if it really is an identifier
  1373. (if (ada-in-comment-p)
  1374. (error "Inside comment"))
  1375. (let (identifier identlist)
  1376. ;; Just in front of a string => we could have an operator declaration,
  1377. ;; as in "+", "-", ..
  1378. (if (= (char-after) ?\")
  1379. (forward-char 1))
  1380. ;; if looking at an operator
  1381. ;; This is only true if:
  1382. ;; - the symbol is +, -, ...
  1383. ;; - the symbol is made of letters, and not followed by _ or a letter
  1384. (if (and (looking-at ada-operator-re)
  1385. (or (not (= (char-syntax (char-after)) ?w))
  1386. (not (or (= (char-syntax (char-after (match-end 0))) ?w)
  1387. (= (char-after (match-end 0)) ?_)))))
  1388. (progn
  1389. (if (and (= (char-before) ?\")
  1390. (= (char-after (+ (length (match-string 0)) (point))) ?\"))
  1391. (forward-char -1))
  1392. (setq identifier (regexp-quote (concat "\"" (match-string 0) "\""))))
  1393. (if (ada-in-string-p)
  1394. (error "Inside string or character constant"))
  1395. (if (looking-at (concat ada-keywords "[^a-zA-Z_]"))
  1396. (error "No cross-reference available for reserved keyword"))
  1397. (if (looking-at "[a-zA-Z0-9_]+")
  1398. (setq identifier (match-string 0))
  1399. (error "No identifier around")))
  1400. ;; Build the identlist
  1401. (setq identlist (ada-make-identlist))
  1402. (ada-set-name identlist (downcase identifier))
  1403. (ada-set-line identlist
  1404. (number-to-string (count-lines 1 (point))))
  1405. (ada-set-column identlist
  1406. (number-to-string (1+ (current-column))))
  1407. (ada-set-file identlist (buffer-file-name))
  1408. identlist
  1409. ))
  1410. (defun ada-get-all-references (identlist)
  1411. "Complete IDENTLIST with definition file and places where it is referenced.
  1412. Information is extracted from the ali file."
  1413. (let ((ali-buffer (ada-get-ali-buffer (ada-file-of identlist)))
  1414. declaration-found)
  1415. (set-buffer ali-buffer)
  1416. (goto-char (point-min))
  1417. (ada-set-on-declaration identlist nil)
  1418. ;; First attempt: we might already be on the declaration of the identifier
  1419. ;; We want to look for the declaration only in a definite interval (after
  1420. ;; the "^X ..." line for the current file, and before the next "^X" line
  1421. (if (re-search-forward
  1422. (concat "^X [0-9]+ " (file-name-nondirectory (ada-file-of identlist)))
  1423. nil t)
  1424. (let ((bound (save-excursion (re-search-forward "^X " nil t))))
  1425. (setq declaration-found
  1426. (re-search-forward
  1427. (concat "^" (ada-line-of identlist)
  1428. "." (ada-column-of identlist)
  1429. "[ *]" (ada-name-of identlist)
  1430. "[{[(<= ]?\\(.*\\)$") bound t))
  1431. (if declaration-found
  1432. (ada-set-on-declaration identlist t))
  1433. ))
  1434. ;; If declaration is still nil, then we were not on a declaration, and
  1435. ;; have to fall back on other algorithms
  1436. (unless declaration-found
  1437. ;; Since we already know the number of the file, search for a direct
  1438. ;; reference to it
  1439. (goto-char (point-min))
  1440. (setq declaration-found t)
  1441. (ada-set-ali-index
  1442. identlist
  1443. (number-to-string (ada-find-file-number-in-ali
  1444. (ada-file-of identlist))))
  1445. (unless (re-search-forward (concat (ada-ali-index-of identlist)
  1446. "|\\([0-9]+[^0-9][0-9]+\\(\n\\.\\)? \\)*"
  1447. (ada-line-of identlist)
  1448. "[^etpzkd<>=^]"
  1449. (ada-column-of identlist) "\\>")
  1450. nil t)
  1451. ;; if we did not find it, it may be because the first reference
  1452. ;; is not required to have a 'unit_number|' item included.
  1453. ;; Or maybe we are already on the declaration...
  1454. (unless (re-search-forward
  1455. (concat
  1456. "^[0-9]+.[0-9]+[ *]"
  1457. (ada-name-of identlist)
  1458. "[ <{=([]\\(.\\|\n\\.\\)*\\<"
  1459. (ada-line-of identlist)
  1460. "[^0-9]"
  1461. (ada-column-of identlist) "\\>")
  1462. nil t)
  1463. ;; If still not found, then either the declaration is unknown
  1464. ;; or the source file has been modified since the ali file was
  1465. ;; created
  1466. (setq declaration-found nil)
  1467. )
  1468. )
  1469. ;; Last check to be completely sure we have found the correct line (the
  1470. ;; ali might not be up to date for instance)
  1471. (if declaration-found
  1472. (progn
  1473. (beginning-of-line)
  1474. ;; while we have a continuation line, go up one line
  1475. (while (looking-at "^\\.")
  1476. (forward-line -1)
  1477. (beginning-of-line))
  1478. (unless (looking-at (concat "[0-9]+.[0-9]+[ *]"
  1479. (ada-name-of identlist) "[ <{=([]"))
  1480. (setq declaration-found nil))))
  1481. ;; Still no success ! The ali file must be too old, and we need to
  1482. ;; use a basic algorithm based on guesses. Note that this only happens
  1483. ;; if the user does not want us to automatically recompile files
  1484. ;; automatically
  1485. (unless declaration-found
  1486. (if (ada-xref-find-in-modified-ali identlist)
  1487. (setq declaration-found t)
  1488. ;; No more idea to find the declaration. Give up
  1489. (progn
  1490. (kill-buffer ali-buffer)
  1491. (error "No declaration of %s found" (ada-name-of identlist))
  1492. )))
  1493. )
  1494. ;; Now that we have found a suitable line in the .ali file, get the
  1495. ;; information available
  1496. (beginning-of-line)
  1497. (if declaration-found
  1498. (let ((current-line (buffer-substring
  1499. (point) (point-at-eol))))
  1500. (save-excursion
  1501. (forward-line 1)
  1502. (beginning-of-line)
  1503. (while (looking-at "^\\.\\(.*\\)")
  1504. (setq current-line (concat current-line (match-string 1)))
  1505. (forward-line 1))
  1506. )
  1507. (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9_.-]+\\)" nil t)
  1508. ;; If we can find the file
  1509. (condition-case err
  1510. (ada-set-declare-file
  1511. identlist
  1512. (ada-get-ada-file-name (match-string 1)
  1513. (ada-file-of identlist)))
  1514. ;; Else clean up the ali file
  1515. (ada-error-file-not-found
  1516. (signal (car err) (cdr err)))
  1517. (error
  1518. (kill-buffer ali-buffer)
  1519. (error (error-message-string err)))
  1520. ))
  1521. (ada-set-references identlist current-line)
  1522. ))
  1523. ))
  1524. (defun ada-xref-find-in-modified-ali (identlist)
  1525. "Find the matching position for IDENTLIST in the current ali buffer.
  1526. This function is only called when the file was not up-to-date, so we need
  1527. to make some guesses.
  1528. This function is disabled for operators, and only works for identifiers."
  1529. (unless (= (string-to-char (ada-name-of identlist)) ?\")
  1530. (progn
  1531. (let ((declist '()) ;;; ( (line_in_ali_file line_in_ada) ( ... ))
  1532. (my-regexp (concat "[ *]"
  1533. (regexp-quote (ada-name-of identlist)) " "))
  1534. (line-ada "--")
  1535. (col-ada "--")
  1536. (line-ali 0)
  1537. (len 0)
  1538. (choice 0)
  1539. (ali-buffer (current-buffer)))
  1540. (goto-char (point-max))
  1541. (while (re-search-backward my-regexp nil t)
  1542. (save-excursion
  1543. (setq line-ali (count-lines 1 (point)))
  1544. (beginning-of-line)
  1545. ;; have a look at the line and column numbers
  1546. (if (looking-at "^\\([0-9]+\\).\\([0-9]+\\)[ *]")
  1547. (progn
  1548. (setq line-ada (match-string 1))
  1549. (setq col-ada (match-string 2)))
  1550. (setq line-ada "--")
  1551. (setq col-ada "--")
  1552. )
  1553. ;; construct a list with the file names and the positions within
  1554. (if (re-search-backward "^X [0-9]+ \\([a-zA-Z0-9._-]+\\)" nil t)
  1555. (add-to-list
  1556. 'declist (list line-ali (match-string 1) line-ada col-ada))
  1557. )
  1558. )
  1559. )
  1560. ;; how many possible declarations have we found ?
  1561. (setq len (length declist))
  1562. (cond
  1563. ;; none => error
  1564. ((= len 0)
  1565. (kill-buffer (current-buffer))
  1566. (error "No declaration of %s recorded in .ali file"
  1567. (ada-name-of identlist)))
  1568. ;; one => should be the right one
  1569. ((= len 1)
  1570. (goto-char (point-min))
  1571. (forward-line (1- (caar declist))))
  1572. ;; more than one => display choice list
  1573. (t
  1574. (save-window-excursion
  1575. (with-output-to-temp-buffer "*choice list*"
  1576. (princ "Identifier is overloaded and Xref information is not up to date.\n")
  1577. (princ "Possible declarations are:\n\n")
  1578. (princ " no. in file at line col\n")
  1579. (princ " --- --------------------- ---- ----\n")
  1580. (let ((counter 0))
  1581. (while (< counter len)
  1582. (princ (format " %2d) %-21s %4s %4s\n"
  1583. (1+ counter)
  1584. (ada-get-ada-file-name
  1585. (nth 1 (nth counter declist))
  1586. (ada-file-of identlist))
  1587. (nth 2 (nth counter declist))
  1588. (nth 3 (nth counter declist))
  1589. ))
  1590. (setq counter (1+ counter))
  1591. ) ; end of while
  1592. ) ; end of let
  1593. ) ; end of with-output-to ...
  1594. (setq choice nil)
  1595. (while (or
  1596. (not choice)
  1597. (not (integerp choice))
  1598. (< choice 1)
  1599. (> choice len))
  1600. (setq choice
  1601. (string-to-number
  1602. (read-from-minibuffer "Enter No. of your choice: "))))
  1603. )
  1604. (set-buffer ali-buffer)
  1605. (goto-char (point-min))
  1606. (forward-line (1- (car (nth (1- choice) declist))))
  1607. ))))))
  1608. (defun ada-find-in-ali (identlist &optional other-frame)
  1609. "Look in the .ali file for the definition of the identifier in IDENTLIST.
  1610. If OTHER-FRAME is non-nil, and `ada-xref-other-buffer' is non-nil,
  1611. opens a new window to show the declaration."
  1612. (ada-get-all-references identlist)
  1613. (let ((ali-line (ada-references-of identlist))
  1614. (locations nil)
  1615. (start 0)
  1616. file line col)
  1617. ;; Note: in some cases, an entity can have multiple references to the
  1618. ;; bodies (this is for instance the case for a separate subprogram, that
  1619. ;; has a reference both to the stub and to the real body).
  1620. ;; In that case, we simply go to each one in turn.
  1621. ;; Get all the possible locations
  1622. (string-match "^\\([0-9]+\\)[a-zA-Z+*]\\([0-9]+\\)[ *]" ali-line)
  1623. (setq locations (list (list (match-string 1 ali-line) ;; line
  1624. (match-string 2 ali-line) ;; column
  1625. (ada-declare-file-of identlist))))
  1626. (while (string-match "\\([0-9]+\\)[bc]\\(<[^>]+>\\)?\\([0-9]+\\)"
  1627. ali-line start)
  1628. (setq line (match-string 1 ali-line)
  1629. col (match-string 3 ali-line)
  1630. start (match-end 3))
  1631. ;; it there was a file number in the same line
  1632. ;; Make sure we correctly handle the case where the first file reference
  1633. ;; on the line is the type reference.
  1634. ;; 1U2 T(2|2r3) 34r23
  1635. (if (string-match (concat "[^{(<0-9]\\([0-9]+\\)|\\([^|bc]+\\)?"
  1636. (match-string 0 ali-line))
  1637. ali-line)
  1638. (let ((file-number (match-string 1 ali-line)))
  1639. (goto-char (point-min))
  1640. (re-search-forward "^D \\([a-zA-Z0-9_.-]+\\)" nil t
  1641. (string-to-number file-number))
  1642. (setq file (match-string 1))
  1643. )
  1644. ;; Else get the nearest file
  1645. (setq file (ada-declare-file-of identlist)))
  1646. (setq locations (append locations (list (list line col file)))))
  1647. ;; Add the specs at the end again, so that from the last body we go to
  1648. ;; the specs
  1649. (setq locations (append locations (list (car locations))))
  1650. ;; Find the new location we want to go to.
  1651. ;; If we are on none of the locations listed, we simply go to the specs.
  1652. (setq line (caar locations)
  1653. col (nth 1 (car locations))
  1654. file (nth 2 (car locations)))
  1655. (while locations
  1656. (if (and (string= (caar locations) (ada-line-of identlist))
  1657. (string= (nth 1 (car locations)) (ada-column-of identlist))
  1658. (string= (file-name-nondirectory (nth 2 (car locations)))
  1659. (file-name-nondirectory (ada-file-of identlist))))
  1660. (setq locations (cadr locations)
  1661. line (car locations)
  1662. col (nth 1 locations)
  1663. file (nth 2 locations)
  1664. locations nil)
  1665. (setq locations (cdr locations))))
  1666. ;; Find the file in the source path
  1667. (setq file (ada-get-ada-file-name file (ada-file-of identlist)))
  1668. ;; Kill the .ali buffer
  1669. (kill-buffer (current-buffer))
  1670. ;; Now go to the buffer
  1671. (ada-xref-change-buffer file
  1672. (string-to-number line)
  1673. (1- (string-to-number col))
  1674. identlist
  1675. other-frame)
  1676. ))
  1677. (defun ada-find-in-src-path (identlist &optional other-frame)
  1678. "More general function for cross-references.
  1679. This function should be used when the standard algorithm that parses the
  1680. .ali file has failed, either because that file was too old or even did not
  1681. exist.
  1682. This function attempts to find the possible declarations for the identifier
  1683. anywhere in the object path.
  1684. This command requires the external `egrep' program to be available.
  1685. This works well when one is using an external library and wants to find
  1686. the declaration and documentation of the subprograms one is using."
  1687. ;; FIXME: what does this function do?
  1688. (let (list
  1689. (dirs (ada-xref-get-obj-dir-field))
  1690. (regexp (concat "[ *]" (ada-name-of identlist)))
  1691. line column
  1692. choice
  1693. file)
  1694. ;; Do the grep in all the directories. We do multiple shell
  1695. ;; commands instead of one in case there is no .ali file in one
  1696. ;; of the directory and the shell stops because of that.
  1697. (with-current-buffer (get-buffer-create "*grep*")
  1698. (while dirs
  1699. (insert (shell-command-to-string
  1700. (concat
  1701. "grep -E -i -h "
  1702. (shell-quote-argument (concat "^X|" regexp "( |$)"))
  1703. " "
  1704. (shell-quote-argument (file-name-as-directory (car dirs)))
  1705. "*.ali")))
  1706. (setq dirs (cdr dirs)))
  1707. ;; Now parse the output
  1708. (setq case-fold-search t)
  1709. (goto-char (point-min))
  1710. (while (re-search-forward regexp nil t)
  1711. (save-excursion
  1712. (beginning-of-line)
  1713. (if (not (= (char-after) ?X))
  1714. (progn
  1715. (looking-at "\\([0-9]+\\).\\([0-9]+\\)")
  1716. (setq line (match-string 1)
  1717. column (match-string 2))
  1718. (re-search-backward "^X [0-9]+ \\(.*\\)$")
  1719. (setq file (list (match-string 1) line column))
  1720. ;; There could be duplicate choices, because of the structure
  1721. ;; of the .ali files
  1722. (unless (member file list)
  1723. (setq list (append list (list file))))))))
  1724. ;; Current buffer is still "*grep*"
  1725. (kill-buffer "*grep*")
  1726. )
  1727. ;; Now display the list of possible matches
  1728. (cond
  1729. ;; No choice found => Error
  1730. ((null list)
  1731. (error "No cross-reference found, please recompile your file"))
  1732. ;; Only one choice => Do the cross-reference
  1733. ((= (length list) 1)
  1734. (setq file (ada-find-src-file-in-dir (caar list)))
  1735. (if file
  1736. (ada-xref-change-buffer file
  1737. (string-to-number (nth 1 (car list)))
  1738. (string-to-number (nth 2 (car list)))
  1739. identlist
  1740. other-frame)
  1741. (error "%s not found in src_dir" (caar list)))
  1742. (message "This is only a (good) guess at the cross-reference.")
  1743. )
  1744. ;; Else, ask the user
  1745. (t
  1746. (save-window-excursion
  1747. (with-output-to-temp-buffer "*choice list*"
  1748. (princ "Identifier is overloaded and Xref information is not up to date.\n")
  1749. (princ "Possible declarations are:\n\n")
  1750. (princ " no. in file at line col\n")
  1751. (princ " --- --------------------- ---- ----\n")
  1752. (let ((counter 0))
  1753. (while (< counter (length list))
  1754. (princ (format " %2d) %-21s %4s %4s\n"
  1755. (1+ counter)
  1756. (nth 0 (nth counter list))
  1757. (nth 1 (nth counter list))
  1758. (nth 2 (nth counter list))
  1759. ))
  1760. (setq counter (1+ counter))
  1761. )))
  1762. (setq choice nil)
  1763. (while (or (not choice)
  1764. (not (integerp choice))
  1765. (< choice 1)
  1766. (> choice (length list)))
  1767. (setq choice
  1768. (string-to-number
  1769. (read-from-minibuffer "Enter No. of your choice: "))))
  1770. )
  1771. (setq choice (1- choice))
  1772. (kill-buffer "*choice list*")
  1773. (setq file (ada-find-src-file-in-dir (car (nth choice list))))
  1774. (if file
  1775. (ada-xref-change-buffer file
  1776. (string-to-number (nth 1 (nth choice list)))
  1777. (string-to-number (nth 2 (nth choice list)))
  1778. identlist
  1779. other-frame)
  1780. (signal 'ada-error-file-not-found (car (nth choice list))))
  1781. (message "This is only a (good) guess at the cross-reference.")
  1782. ))))
  1783. (defun ada-xref-change-buffer
  1784. (file line column identlist &optional other-frame)
  1785. "Select and display FILE, at LINE and COLUMN.
  1786. If we do not end on the same identifier as IDENTLIST, find the
  1787. closest match. Kills the .ali buffer at the end.
  1788. If OTHER-FRAME is non-nil, creates a new frame to show the file."
  1789. (let (declaration-buffer)
  1790. ;; Select and display the destination buffer
  1791. (if ada-xref-other-buffer
  1792. (if other-frame
  1793. (find-file-other-frame file)
  1794. (setq declaration-buffer (find-file-noselect file))
  1795. (set-buffer declaration-buffer)
  1796. (switch-to-buffer-other-window declaration-buffer)
  1797. )
  1798. (find-file file)
  1799. )
  1800. ;; move the cursor to the correct position
  1801. (push-mark)
  1802. (goto-char (point-min))
  1803. (forward-line (1- line))
  1804. (move-to-column column)
  1805. ;; If we are not on the identifier, the ali file was not up-to-date.
  1806. ;; Try to find the nearest position where the identifier is found,
  1807. ;; this is probably the right one.
  1808. (unless (looking-at (ada-name-of identlist))
  1809. (ada-xref-search-nearest (ada-name-of identlist)))
  1810. ))
  1811. (defun ada-xref-search-nearest (name)
  1812. "Search for NAME nearest to the position recorded in the Xref file.
  1813. Return the position of the declaration in the buffer, or nil if not found."
  1814. (let ((orgpos (point))
  1815. (newpos nil)
  1816. (diff nil))
  1817. (goto-char (point-max))
  1818. ;; loop - look for all declarations of name in this file
  1819. (while (search-backward name nil t)
  1820. ;; check if it really is a complete Ada identifier
  1821. (if (and
  1822. (not (save-excursion
  1823. (goto-char (match-end 0))
  1824. (looking-at "_")))
  1825. (not (ada-in-string-or-comment-p))
  1826. (or
  1827. ;; variable declaration ?
  1828. (save-excursion
  1829. (skip-chars-forward "a-zA-Z_0-9" )
  1830. (ada-goto-next-non-ws)
  1831. (looking-at ":[^=]"))
  1832. ;; procedure, function, task or package declaration ?
  1833. (save-excursion
  1834. (ada-goto-previous-word)
  1835. (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]\\>"))))
  1836. ;; check if it is nearer than the ones before if any
  1837. (if (or (not diff)
  1838. (< (abs (- (point) orgpos)) diff))
  1839. (progn
  1840. (setq newpos (point)
  1841. diff (abs (- newpos orgpos))))))
  1842. )
  1843. (if newpos
  1844. (progn
  1845. (message "ATTENTION: this declaration is only a (good) guess ...")
  1846. (goto-char newpos))
  1847. nil)))
  1848. ;; Find the parent library file of the current file
  1849. (defun ada-goto-parent ()
  1850. "Go to the parent library file."
  1851. (interactive)
  1852. (ada-require-project-file)
  1853. (let ((buffer (ada-get-ali-buffer (buffer-file-name)))
  1854. (unit-name nil)
  1855. (body-name nil)
  1856. (ali-name nil))
  1857. (with-current-buffer buffer
  1858. (goto-char (point-min))
  1859. (re-search-forward "^U \\([^ \t%]+\\)%[bs][ \t]+\\([^ \t]+\\)")
  1860. (setq unit-name (match-string 1))
  1861. (if (not (string-match "\\(.*\\)\\.[^.]+" unit-name))
  1862. (progn
  1863. (kill-buffer buffer)
  1864. (error "No parent unit !"))
  1865. (setq unit-name (match-string 1 unit-name))
  1866. )
  1867. ;; look for the file name for the parent unit specification
  1868. (goto-char (point-min))
  1869. (re-search-forward (concat "^W " unit-name
  1870. "%s[ \t]+\\([^ \t]+\\)[ \t]+"
  1871. "\\([^ \t\n]+\\)"))
  1872. (setq body-name (match-string 1))
  1873. (setq ali-name (match-string 2))
  1874. (kill-buffer buffer)
  1875. )
  1876. (setq ali-name (ada-find-ali-file-in-dir ali-name))
  1877. (save-excursion
  1878. ;; Tries to open the new ali file to find the spec file
  1879. (if ali-name
  1880. (progn
  1881. (find-file ali-name)
  1882. (goto-char (point-min))
  1883. (re-search-forward (concat "^U " unit-name "%s[ \t]+"
  1884. "\\([^ \t]+\\)"))
  1885. (setq body-name (match-string 1))
  1886. (kill-buffer (current-buffer))
  1887. )
  1888. )
  1889. )
  1890. (find-file body-name)
  1891. ))
  1892. (defun ada-make-filename-from-adaname (adaname)
  1893. "Determine the filename in which ADANAME is found.
  1894. This is a GNAT specific function that uses gnatkrunch."
  1895. (let ((krunch-buf (generate-new-buffer "*gkrunch*"))
  1896. (cross-prefix (plist-get (cdr (ada-xref-current-project)) 'cross_prefix)))
  1897. (with-current-buffer krunch-buf
  1898. ;; send adaname to external process `gnatkr'.
  1899. ;; Add a dummy extension, since gnatkr versions have two different
  1900. ;; behaviors depending on the version:
  1901. ;; Up to 3.15: "AA.BB.CC" => aa-bb-cc
  1902. ;; After: "AA.BB.CC" => aa-bb.cc
  1903. (call-process (concat cross-prefix "gnatkr") nil krunch-buf nil
  1904. (concat adaname ".adb") ada-krunch-args)
  1905. ;; fetch output of that process
  1906. (setq adaname (buffer-substring
  1907. (point-min)
  1908. (progn
  1909. (goto-char (point-min))
  1910. (end-of-line)
  1911. (point))))
  1912. ;; Remove the extra extension we added above
  1913. (setq adaname (substring adaname 0 -4))
  1914. (kill-buffer krunch-buf)))
  1915. adaname
  1916. )
  1917. (defun ada-make-body-gnatstub (&optional interactive)
  1918. "Create an Ada package body in the current buffer.
  1919. This function uses the `gnat stub' program to create the body.
  1920. This function typically is to be hooked into `ff-file-created-hook'.
  1921. If INTERACTIVE is nil, assume this is called from `ff-file-created-hook'."
  1922. (interactive "p")
  1923. (ada-require-project-file)
  1924. ;; If not interactive, assume we are being called from
  1925. ;; ff-file-created-hook. Then the current buffer is for the body
  1926. ;; file, but we will create a new one after gnat stub runs
  1927. (unless interactive
  1928. (set-buffer-modified-p nil)
  1929. (kill-buffer (current-buffer)))
  1930. (save-some-buffers nil nil)
  1931. ;; Make sure the current buffer is the spec, so gnat stub gets the
  1932. ;; right package parameter (this might not be the case if for
  1933. ;; instance the user was asked for a project file)
  1934. (unless (buffer-file-name (car (buffer-list)))
  1935. (set-buffer (cadr (buffer-list))))
  1936. ;; Call the external process
  1937. (let* ((project-plist (cdr (ada-xref-current-project)))
  1938. (gnatstub-opts (ada-treat-cmd-string ada-gnatstub-opts))
  1939. (gpr-file (plist-get project-plist 'gpr_file))
  1940. (filename (buffer-file-name (car (buffer-list))))
  1941. (output (concat (file-name-sans-extension filename) ".adb"))
  1942. (cross-prefix (plist-get project-plist 'cross_prefix))
  1943. (gnatstub-cmd (concat cross-prefix "gnat stub"
  1944. (if (not (string= gpr-file ""))
  1945. (concat " -P\"" gpr-file "\""))
  1946. " " gnatstub-opts " " filename))
  1947. (buffer (get-buffer-create "*gnat stub*")))
  1948. (with-current-buffer buffer
  1949. (compilation-minor-mode 1)
  1950. (erase-buffer)
  1951. (insert gnatstub-cmd)
  1952. (newline)
  1953. )
  1954. (call-process shell-file-name nil buffer nil "-c" gnatstub-cmd)
  1955. ;; clean up the output
  1956. (if (file-exists-p output)
  1957. (progn
  1958. (find-file output)
  1959. (kill-buffer buffer))
  1960. ;; file not created; display the error message
  1961. (display-buffer buffer))))
  1962. (defun ada-xref-initialize ()
  1963. "Function called by `ada-mode-hook' to initialize the ada-xref.el package.
  1964. For instance, it creates the gnat-specific menus, sets some hooks for
  1965. `find-file'."
  1966. (remove-hook 'ff-file-created-hook 'ada-make-body) ; from global hook
  1967. (remove-hook 'ff-file-created-hook 'ada-make-body t) ; from local hook
  1968. (add-hook 'ff-file-created-hook 'ada-make-body-gnatstub nil t)
  1969. ;; Completion for file names in the mini buffer should ignore .ali files
  1970. (add-to-list 'completion-ignored-extensions ".ali")
  1971. (ada-xref-update-project-menu)
  1972. )
  1973. ;; ----- Add to ada-mode-hook ---------------------------------------------
  1974. ;; This must be done before initializing the Ada menu.
  1975. (add-hook 'ada-mode-hook 'ada-xref-initialize)
  1976. ;; Define a new error type
  1977. (define-error 'ada-error-file-not-found
  1978. "File not found in src-dir (check project file): " 'ada-mode-errors)
  1979. (provide 'ada-xref)
  1980. ;;; ada-xref.el ends here