Skip to content


Subversion checkout URL

You can clone with
Download ZIP
100644 123 lines (106 sloc) 4.832 kB
c7d0bf4 Swank clojure and SLIME setup
Brian Carper authored
1 ;;;; Source-file cache
2 ;;;
3 ;;; To robustly find source locations in CMUCL and SBCL it's useful to
4 ;;; have the exact source code that the loaded code was compiled from.
5 ;;; In this source we can accurately find the right location, and from
6 ;;; that location we can extract a "snippet" of code to show what the
7 ;;; definition looks like. Emacs can use this snippet in a best-match
8 ;;; search to locate the right definition, which works well even if
9 ;;; the buffer has been modified.
10 ;;;
11 ;;; The idea is that if a definition previously started with
12 ;;; `(define-foo bar' then it probably still does.
13 ;;;
14 ;;; Whenever we see that the file on disk has the same
15 ;;; `file-write-date' as a location we're looking for we cache the
16 ;;; whole file inside Lisp. That way we will still have the matching
17 ;;; version even if the file is later modified on disk. If the file is
18 ;;; later recompiled and reloaded then we replace our cache entry.
19 ;;;
20 ;;; This code has been placed in the Public Domain. All warranties
21 ;;; are disclaimed.
23 (in-package :swank-backend)
25 (defvar *cache-sourcecode* t
26 "When true complete source files are cached.
27 The cache is used to keep known good copies of the source text which
28 correspond to the loaded code. Finding definitions is much more
29 reliable when the exact source is available, so we cache it in case it
30 gets edited on disk later.")
32 (defvar *source-file-cache* (make-hash-table :test 'equal)
33 "Cache of source file contents.
34 Maps from truename to source-cache-entry structure.")
36 (defstruct (source-cache-entry
37 (:conc-name source-cache-entry.)
38 (:constructor make-source-cache-entry (text date)))
39 text date)
41 (defimplementation buffer-first-change (filename)
42 "Load a file into the cache when the user modifies its buffer.
43 This is a win if the user then saves the file and tries to M-. into it."
44 (unless (source-cached-p filename)
45 (ignore-errors
46 (source-cache-get filename (file-write-date filename))))
47 nil)
49 (defun get-source-code (filename code-date)
50 "Return the source code for FILENAME as written on DATE in a string.
51 If the exact version cannot be found then return the current one from disk."
52 (or (source-cache-get filename code-date)
53 (read-file filename)))
55 (defun source-cache-get (filename date)
56 "Return the source code for FILENAME as written on DATE in a string.
57 Return NIL if the right version cannot be found."
58 (when *cache-sourcecode*
59 (let ((entry (gethash filename *source-file-cache*)))
60 (cond ((and entry (equal date ( entry)))
61 ;; Cache hit.
62 (source-cache-entry.text entry))
63 ((or (null entry)
64 (not (equal date ( entry))))
65 ;; Cache miss.
66 (if (equal (file-write-date filename) date)
67 ;; File on disk has the correct version.
68 (let ((source (read-file filename)))
69 (setf (gethash filename *source-file-cache*)
70 (make-source-cache-entry source date))
71 source)
72 nil))))))
74 (defun source-cached-p (filename)
75 "Is any version of FILENAME in the source cache?"
76 (if (gethash filename *source-file-cache*) t))
78 (defun read-file (filename)
79 "Return the entire contents of FILENAME as a string."
80 (with-open-file (s filename :direction :input
81 :external-format (or (guess-external-format filename)
82 (find-external-format "latin-1")
83 :default))
61a0cc6 @cdaddr better smart-tab; swank/slime version bump
84 (let* ((string (make-string (file-length s)))
85 (length (read-sequence string s)))
86 (subseq string 0 length))))
c7d0bf4 Swank clojure and SLIME setup
Brian Carper authored
88 ;;;; Snippets
90 (defvar *source-snippet-size* 256
91 "Maximum number of characters in a snippet of source code.
92 Snippets at the beginning of definitions are used to tell Emacs what
93 the definitions looks like, so that it can accurately find them by
94 text search.")
96 (defun read-snippet (stream &optional position)
97 "Read a string of upto *SOURCE-SNIPPET-SIZE* characters from STREAM.
98 If POSITION is given, set the STREAM's file position first."
99 (when position
100 (file-position stream position))
101 #+sbcl (skip-comments-and-whitespace stream)
102 (read-upto-n-chars stream *source-snippet-size*))
104 (defun read-snippet-from-string (string &optional position)
105 (with-input-from-string (s string)
106 (read-snippet s position)))
108 (defun skip-comments-and-whitespace (stream)
109 (case (peek-char nil stream)
110 ((#\Space #\Tab #\Newline #\Linefeed #\Page)
111 (read-char stream)
112 (skip-comments-and-whitespace stream))
113 (#\;
114 (read-line stream)
115 (skip-comments-and-whitespace stream))))
117 (defun read-upto-n-chars (stream n)
118 "Return a string of upto N chars from STREAM."
119 (let* ((string (make-string n))
120 (chars (read-sequence string stream)))
121 (subseq string 0 chars)))
Something went wrong with that request. Please try again.