dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

slynk-source-file-cache.lisp (5153B)


      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.
     22 
     23 (defpackage slynk-source-file-cache
     24   (:use cl slynk-backend)
     25   (:import-from slynk-backend
     26 		defimplementation buffer-first-change)
     27   (:export
     28    get-source-code
     29    source-cache-get ;FIXME: isn't it odd that both are exported?
     30 
     31    *source-snippet-size*
     32    read-snippet
     33    read-snippet-from-string
     34    ))
     35 
     36 (in-package slynk-source-file-cache)
     37 
     38 (defvar *cache-sourcecode* t
     39   "When true complete source files are cached.
     40 The cache is used to keep known good copies of the source text which
     41 correspond to the loaded code. Finding definitions is much more
     42 reliable when the exact source is available, so we cache it in case it
     43 gets edited on disk later.")
     44 
     45 (defvar *source-file-cache* (make-hash-table :test 'equal)
     46   "Cache of source file contents.
     47 Maps from truename to source-cache-entry structure.")
     48 
     49 (defstruct (source-cache-entry
     50              (:conc-name source-cache-entry.)
     51              (:constructor make-source-cache-entry (text date)))
     52   text date)
     53 
     54 (defimplementation buffer-first-change (filename)
     55   "Load a file into the cache when the user modifies its buffer.
     56 This is a win if the user then saves the file and tries to M-. into it."
     57   (unless (source-cached-p filename)
     58     (ignore-errors
     59       (source-cache-get filename (file-write-date filename))))
     60   nil)
     61 
     62 (defun get-source-code (filename code-date)
     63   "Return the source code for FILENAME as written on DATE in a string.
     64 If the exact version cannot be found then return the current one from disk."
     65   (or (source-cache-get filename code-date)
     66       (read-file filename)))
     67 
     68 (defun source-cache-get (filename date)
     69   "Return the source code for FILENAME as written on DATE in a string.
     70 Return NIL if the right version cannot be found."
     71   (when *cache-sourcecode*
     72     (let ((entry (gethash filename *source-file-cache*)))
     73       (cond ((and entry (equal date (source-cache-entry.date entry)))
     74              ;; Cache hit.
     75              (source-cache-entry.text entry))
     76             ((or (null entry)
     77                  (not (equal date (source-cache-entry.date entry))))
     78              ;; Cache miss.
     79              (if (equal (file-write-date filename) date)
     80                  ;; File on disk has the correct version.
     81                  (let ((source (read-file filename)))
     82                    (setf (gethash filename *source-file-cache*)
     83                          (make-source-cache-entry source date))
     84                    source)
     85                  nil))))))
     86 
     87 (defun source-cached-p (filename)
     88   "Is any version of FILENAME in the source cache?"
     89   (if (gethash filename *source-file-cache*) t))
     90 
     91 (defun read-file (filename)
     92   "Return the entire contents of FILENAME as a string."
     93   (with-open-file (s filename :direction :input
     94 		     :external-format (or (guess-external-format filename)
     95 					  (find-external-format "latin-1")
     96 					  :default))
     97     (let* ((string (make-string (file-length s)))
     98            (length (read-sequence string s)))
     99       (subseq string 0 length))))
    100 
    101 ;;;; Snippets
    102 
    103 (defvar *source-snippet-size* 256
    104   "Maximum number of characters in a snippet of source code.
    105 Snippets at the beginning of definitions are used to tell Emacs what
    106 the definitions looks like, so that it can accurately find them by
    107 text search.")
    108 
    109 (defun read-snippet (stream &optional position)
    110   "Read a string of upto *SOURCE-SNIPPET-SIZE* characters from STREAM.
    111 If POSITION is given, set the STREAM's file position first."
    112   (when position
    113     (file-position stream position))
    114   #+sbcl (skip-comments-and-whitespace stream)
    115   (read-upto-n-chars stream *source-snippet-size*))
    116 
    117 (defun read-snippet-from-string (string &optional position)
    118   (with-input-from-string (s string)
    119     (read-snippet s position)))
    120 
    121 (defun skip-comments-and-whitespace (stream)
    122   (case (peek-char nil stream nil nil)
    123     ((#\Space #\Tab #\Newline #\Linefeed #\Page)
    124      (read-char stream)
    125      (skip-comments-and-whitespace stream))
    126     (#\;
    127      (read-line stream)
    128      (skip-comments-and-whitespace stream))))
    129 
    130 (defun read-upto-n-chars (stream n)
    131   "Return a string of upto N chars from STREAM."
    132   (let* ((string (make-string n))
    133          (chars  (read-sequence string stream)))
    134     (subseq string 0 chars)))