1 ;;; xref.scm -- cross-referencing utilities
3 ;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Mon Mar 02, 2009 02:37
12 (define-module (geiser xref)
13 #:export (symbol-location
18 #:use-module (geiser utils)
19 #:use-module (geiser modules)
20 #:use-module (geiser doc)
21 #:use-module (oop goops)
22 #:use-module (system xref)
23 #:use-module (system vm program))
25 (define (symbol-location sym)
26 (cond ((symbol-module sym) => module-location)
27 (else (let ((obj (symbol->object sym)))
28 (or (and (program? obj) (program-location obj))
31 (define (generic-methods sym)
32 (let* ((gen (symbol->object sym))
33 (methods (if (is-a? gen <generic>)
34 (generic-function-methods gen)
36 (filter (lambda (x) (not (null? x)))
38 (make-xref (method-procedure m) sym (symbol-module sym)))
41 (define (make-xref proc name module)
43 `(("location" . ,(or (program-location proc) (symbol-location name)))
44 ("signature" . ,(object-signature name proc))
45 ("module" . ,(or module '())))))
47 (define (program-location p)
48 (cond ((not (program? p)) #f)
49 ((program-source p 0) =>
50 (lambda (s) (make-location (program-path p) (source:line s))))
51 ((program-path p) => (lambda (s) (make-location s #f)))
54 (define (program-path p)
55 (let* ((mod (program-module p))
56 (name (and (module? mod) (module-name mod))))
57 (and name (module-path name))))
59 (define (procedure-xref proc . mod-name)
60 (let* ((proc-name (or (procedure-name proc) '<anonymous>))
61 (mod-name (if (null? mod-name)
62 (symbol-module proc-name)
64 (make-xref proc proc-name mod-name)))
67 (let ((mod (symbol-module sym #t)))
69 (apply append (map (lambda (procs)
71 (procedure-xref proc (car procs)))
73 (procedure-callers (cons mod sym)))))))
76 (let ((obj (symbol->object sym)))
78 (map procedure-xref (procedure-callees obj)))))
80 (define (find-file path)
81 (let loop ((dirs %load-path))
83 (let ((candidate (string-append (car dirs) "/" path)))
84 (if (file-exists? candidate) candidate (loop (cdr dirs)))))))