Retrieving (load)ed source code from CCL?

2019-03-31 02:35发布

问题:

I called (load "code.lisp") with CCL, then accidentally deleted code.lisp. Is there any way for me to retrieve the source code? Does CCL have it in memory anywhere?

回答1:

That's a very special functionality. Here only for Clozure CL. The code won't work anywhere else. This works for me in the CCL IDE. It retrieves the source code of symbols who are :internal or :external in a certain package. It doesn't do that for symbols which are inherited from other packages (you would then often have the source code from the package CL or CCL, which is a bit too much).

(defun retrieve-source-code (&optional (package *package*))
  (do-symbols (s package)
    (multiple-value-bind (symbol where)
                         (find-symbol (symbol-name s)
                                      package)
      (declare (ignore symbol))
      (when (member where '(:internal :external))
        (let ((ds (find-definition-sources s)))
          (when (and ds (listp ds))
            (loop for (nil sn) in ds
              for snt = (source-note-text sn)
              when snt do (progn
                            (terpri)
                            (princ snt)
                            (terpri)))))))))

As you can see, it can retrieve itself (and more):

? (retrieve-source-code)

(defun retrieve-source-code (&optional (package *package*))
    (do-symbols (s package)
      (multiple-value-bind (symbol where)
                           (find-symbol (symbol-name s)
                                        package)
        (declare (ignore symbol))
        (when (member where '(:internal :external))
          (let ((ds (find-definition-sources s)))
            (when (and ds (listp ds))
              (loop for (nil sn) in ds
                for snt = (source-note-text sn)
                when snt do (progn
                              (terpri)
                              (princ snt)
                              (terpri)))))))))
NIL