Path: blob/master/elisp/slime/slime-1.2/swank-source-file-cache.lisp
990 views
;;;; Source-file cache1;;;2;;; To robustly find source locations in CMUCL and SBCL it's useful to3;;; have the exact source code that the loaded code was compiled from.4;;; In this source we can accurately find the right location, and from5;;; that location we can extract a "snippet" of code to show what the6;;; definition looks like. Emacs can use this snippet in a best-match7;;; search to locate the right definition, which works well even if8;;; the buffer has been modified.9;;;10;;; The idea is that if a definition previously started with11;;; `(define-foo bar' then it probably still does.12;;;13;;; Whenever we see that the file on disk has the same14;;; `file-write-date' as a location we're looking for we cache the15;;; whole file inside Lisp. That way we will still have the matching16;;; version even if the file is later modified on disk. If the file is17;;; later recompiled and reloaded then we replace our cache entry.18;;;19;;; This code has been placed in the Public Domain. All warranties20;;; are disclaimed.2122(in-package :swank-backend)2324(defvar *cache-sourcecode* t25"When true complete source files are cached.26The cache is used to keep known good copies of the source text which27correspond to the loaded code. Finding definitions is much more28reliable when the exact source is available, so we cache it in case it29gets edited on disk later.")3031(defvar *source-file-cache* (make-hash-table :test 'equal)32"Cache of source file contents.33Maps from truename to source-cache-entry structure.")3435(defstruct (source-cache-entry36(:conc-name source-cache-entry.)37(:constructor make-source-cache-entry (text date)))38text date)3940(defimplementation buffer-first-change (filename)41"Load a file into the cache when the user modifies its buffer.42This is a win if the user then saves the file and tries to M-. into it."43(unless (source-cached-p filename)44(ignore-errors45(source-cache-get filename (file-write-date filename))))46nil)4748(defun get-source-code (filename code-date)49"Return the source code for FILENAME as written on DATE in a string.50If the exact version cannot be found then return the current one from disk."51(or (source-cache-get filename code-date)52(read-file filename)))5354(defun source-cache-get (filename date)55"Return the source code for FILENAME as written on DATE in a string.56Return NIL if the right version cannot be found."57(when *cache-sourcecode*58(let ((entry (gethash filename *source-file-cache*)))59(cond ((and entry (equal date (source-cache-entry.date entry)))60;; Cache hit.61(source-cache-entry.text entry))62((or (null entry)63(not (equal date (source-cache-entry.date entry))))64;; Cache miss.65(if (equal (file-write-date filename) date)66;; File on disk has the correct version.67(let ((source (read-file filename)))68(setf (gethash filename *source-file-cache*)69(make-source-cache-entry source date))70source)71nil))))))7273(defun source-cached-p (filename)74"Is any version of FILENAME in the source cache?"75(if (gethash filename *source-file-cache*) t))7677(defun read-file (filename)78"Return the entire contents of FILENAME as a string."79(with-open-file (s filename :direction :input80:external-format (or (guess-external-format filename)81(find-external-format "latin-1")82:default))83(let* ((string (make-string (file-length s)))84(length (read-sequence string s)))85(subseq string 0 length))))8687;;;; Snippets8889(defvar *source-snippet-size* 25690"Maximum number of characters in a snippet of source code.91Snippets at the beginning of definitions are used to tell Emacs what92the definitions looks like, so that it can accurately find them by93text search.")9495(defun read-snippet (stream &optional position)96"Read a string of upto *SOURCE-SNIPPET-SIZE* characters from STREAM.97If POSITION is given, set the STREAM's file position first."98(when position99(file-position stream position))100#+sbcl (skip-comments-and-whitespace stream)101(read-upto-n-chars stream *source-snippet-size*))102103(defun read-snippet-from-string (string &optional position)104(with-input-from-string (s string)105(read-snippet s position)))106107(defun skip-comments-and-whitespace (stream)108(case (peek-char nil stream)109((#\Space #\Tab #\Newline #\Linefeed #\Page)110(read-char stream)111(skip-comments-and-whitespace stream))112(#\;113(read-line stream)114(skip-comments-and-whitespace stream))))115116(defun read-upto-n-chars (stream n)117"Return a string of upto N chars from STREAM."118(let* ((string (make-string n))119(chars (read-sequence string stream)))120(subseq string 0 chars)))121122123124