| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: SYSTEM -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 2001, Juan Jose Garcia-Ripoll. |
| 6 |
;;;; |
| 7 |
;;;; See file 'LICENSE' for the copyright details. |
| 8 |
;;;; |
| 9 |
|
| 10 |
(in-package "SYSTEM") |
| 11 |
|
| 12 |
;;;;---------------------------------------------------------------------- |
| 13 |
;;;; Documentation system |
| 14 |
;;;; |
| 15 |
|
| 16 |
#+ecl-min |
| 17 |
(progn |
| 18 |
(*make-special '*documentation-pool*) |
| 19 |
(setq *documentation-pool* nil) |
| 20 |
(*make-special '*keep-documentation*) |
| 21 |
(setq *keep-documentation* t)) |
| 22 |
#-ecl-min |
| 23 |
(progn |
| 24 |
(setq *documentation-pool* |
| 25 |
(list (make-hash-table :test #'equal :size 128) |
| 26 |
"SYS:help.doc")) |
| 27 |
(defparameter *keep-documentation* t)) |
| 28 |
|
| 29 |
(defun new-documentation-pool (&optional (size 1024)) |
| 30 |
"Args: (&optional hash-size) |
| 31 |
Sets up a new hash table for storing documentation strings." |
| 32 |
(push (make-hash-table :test #'eql :size size) |
| 33 |
*documentation-pool*)) |
| 34 |
|
| 35 |
(defun record-cons (record key sub-key) |
| 36 |
(let ((cons (cons key sub-key))) |
| 37 |
(dolist (i record i) |
| 38 |
(when (equalp (car i) cons) |
| 39 |
(return i))))) |
| 40 |
|
| 41 |
(defun record-field (record key sub-key) |
| 42 |
(cdr (record-cons record key sub-key))) |
| 43 |
|
| 44 |
(defun set-record-field (record key sub-key value) |
| 45 |
(let ((field (record-cons record key sub-key))) |
| 46 |
(if field |
| 47 |
(rplacd field value) |
| 48 |
(setq record (list* (cons (cons key sub-key) value) record))) |
| 49 |
record)) |
| 50 |
|
| 51 |
(defun rem-record-field (record key sub-key) |
| 52 |
(let ((x (record-cons record key sub-key))) |
| 53 |
(if x |
| 54 |
(let ((output '())) |
| 55 |
(dolist (i record output) |
| 56 |
(when (not (eq i x)) |
| 57 |
(setq output (cons i output))))) |
| 58 |
record))) |
| 59 |
|
| 60 |
(defun annotate (object key sub-key value) |
| 61 |
(let ((dict (first *documentation-pool*))) |
| 62 |
(when (hash-table-p dict) |
| 63 |
(let ((record (set-record-field (gethash object dict) |
| 64 |
key sub-key value))) |
| 65 |
(si::hash-set object dict record))))) |
| 66 |
|
| 67 |
(defun remove-annotation (object key sub-key) |
| 68 |
(let ((dict (first *documentation-pool*))) |
| 69 |
(when (hash-table-p dict) |
| 70 |
(let ((record (rem-record-field (gethash object dict) |
| 71 |
key sub-key))) |
| 72 |
(if record |
| 73 |
(si::hash-set object dict record) |
| 74 |
(remhash object dict)))))) |
| 75 |
|
| 76 |
(defun get-annotation (object key &optional (sub-key :all)) |
| 77 |
(let ((output '())) |
| 78 |
(dolist (dict *documentation-pool* output) |
| 79 |
(let ((record (if (hash-table-p dict) |
| 80 |
(gethash object dict) |
| 81 |
(if (stringp dict) |
| 82 |
(search-help-file object dict) |
| 83 |
nil)))) |
| 84 |
(when record |
| 85 |
(if (eq sub-key :all) |
| 86 |
(dolist (i record) |
| 87 |
(let ((key-sub-key (car i))) |
| 88 |
(when (equal (car key-sub-key) key) |
| 89 |
(push (cons (cdr key-sub-key) (cdr i)) output)))) |
| 90 |
(if (setq output (record-field record key sub-key)) |
| 91 |
(return output)))))))) |
| 92 |
|
| 93 |
(defun dump-documentation (file &optional (merge nil)) |
| 94 |
"Args: (filespec &optional (merge nil)) |
| 95 |
Saves the current hash table for documentation strings to the specificed file. |
| 96 |
If MERGE is true, merges the contents of this table with the original values in |
| 97 |
the help file." |
| 98 |
(let ((dict (first *documentation-pool*))) |
| 99 |
(when (hash-table-p dict) |
| 100 |
(dump-help-file dict file merge) |
| 101 |
(rplaca *documentation-pool* file)))) |
| 102 |
|
| 103 |
(defun get-documentation (object doc-type) |
| 104 |
(when (functionp object) |
| 105 |
(when (null (setq object (compiled-function-name object))) |
| 106 |
(return-from get-documentation nil))) |
| 107 |
(if (and object (listp object) (si::valid-function-name-p object)) |
| 108 |
(get-annotation (second object) 'setf-documentation doc-type) |
| 109 |
(get-annotation object 'documentation doc-type))) |
| 110 |
|
| 111 |
(defun set-documentation (object doc-type string) |
| 112 |
(when (not (or (stringp string) (null string))) |
| 113 |
(error "~S is not a valid documentation string" string)) |
| 114 |
(let ((key 'documentation)) |
| 115 |
(when (and object (listp object) (si::valid-function-name-p object)) |
| 116 |
(setq object (second object) key 'setf-documentation)) |
| 117 |
(if string |
| 118 |
(annotate object key doc-type string) |
| 119 |
(remove-annotation object key doc-type))) |
| 120 |
string) |
| 121 |
|
| 122 |
(defun expand-set-documentation (symbol doc-type string) |
| 123 |
(when (and *keep-documentation* string) |
| 124 |
(when (not (stringp string)) |
| 125 |
(error "~S is not a valid documentation string" string)) |
| 126 |
`((set-documentation ',symbol ',doc-type ,string)))) |
| 127 |
|
| 128 |
#-clos |
| 129 |
(defun documentation (object type) |
| 130 |
"Args: (symbol doc-type) |
| 131 |
Returns the DOC-TYPE doc-string of SYMBOL; NIL if none exists. Possible doc- |
| 132 |
types are: |
| 133 |
FUNCTION (special forms, macros, and functions) |
| 134 |
VARIABLE (global variables) |
| 135 |
TYPE (type specifiers) |
| 136 |
STRUCTURE (structures) |
| 137 |
SETF (SETF methods) |
| 138 |
All built-in special forms, macros, functions, and variables have their doc- |
| 139 |
strings." |
| 140 |
(cond ((member type '(function type variable setf structure)) |
| 141 |
(when (not (symbolp object)) |
| 142 |
(error "~S is not a symbol." object)) |
| 143 |
(si::get-documentation object type)) |
| 144 |
(t |
| 145 |
(error "~S is an unknown documentation type" type)))) |
| 146 |
|
| 147 |
(defun make-dspec (definition) |
| 148 |
(when (consp definition) |
| 149 |
(let* ((kind (first definition)) |
| 150 |
(name (second definition)) |
| 151 |
(extra '())) |
| 152 |
(when (eq kind 'defmethod) |
| 153 |
(let ((list (third definition))) |
| 154 |
(setq extra (if (symbolp list) |
| 155 |
(cons list (fourth definition)) |
| 156 |
list)))) |
| 157 |
(list* kind name extra)))) |
| 158 |
|
| 159 |
;; (EXT:OPTIONAL-ANNOTATION arguments for EXT:ANNOTATE) |
| 160 |
(si::fset 'ext:optional-annotation |
| 161 |
#'(ext:lambda-block ext:optional-annotation (whole env) |
| 162 |
(declare (ignore env)) |
| 163 |
`(ext:annotate ,@(rest whole))) |
| 164 |
t) |
| 165 |
|
| 166 |
(defun default-annotation-logic (source-location definition output-form |
| 167 |
&optional (dspec (make-dspec definition))) |
| 168 |
(let* ((kind (first definition)) |
| 169 |
(name (second definition))) |
| 170 |
`(progn |
| 171 |
(ext:optional-annotation ',name 'location ',dspec ',source-location) |
| 172 |
,(when (member kind '(defun defmacro defgeneric)) |
| 173 |
`(ext:optional-annotation ',name :lambda-list nil ',(third definition))) |
| 174 |
,output-form))) |
| 175 |
|
| 176 |
#+ecl-min |
| 177 |
(when (null *documentation-pool*) (new-documentation-pool 1024)) |
| 178 |
(setq ext::*register-with-pde-hook* 'default-annotation-logic) |