Klimi's new dotfiles with stow.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

136 satır
5.1 KiB

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