makedoc.el 5.13 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(defvar lyskom-doc-vardefs nil)

(defvar lyskom-doc-keymaps
  '(lyskom-mode-map lyskom-edit-mode-map))

(fset 'tmpfn-1 (symbol-function 'defvar))

(defun lyskom-doc-defvar (var val doc)
  (setq lyskom-doc-vardefs (cons (list var val
                                       (if (and doc
                                                (string-match "^*" doc))
                                           t
                                         nil))
                                 lyskom-doc-vardefs))
  (set var val))


David Byers's avatar
David Byers committed
18
(defmacro lyskom-doc-defvar-macro (var &optional val doc &rest junk)
19
20
21
22
23
24
  (list 'lyskom-doc-defvar (list 'quote var) val doc))


(defun lyskom-update-documentation ()
  (unwind-protect
      (progn
David Byers's avatar
David Byers committed
25
        (fset 'def-kom-var 'lyskom-doc-defvar-macro)
26
        (load-file "../src/lyskom.el"))
David Byers's avatar
David Byers committed
27
    (fset 'def-kom-var (symbol-function 'tmpfn-1)))
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  (find-file "lyskom-commands.data")
  (lyskom-check-cmd-doc)
  (find-file "lyskom-variables.data")
  (lyskom-check-vbl-doc))


(defun lyskom-check-vbl-doc ()
  (let ((doc-list nil)
        (new-list nil))
    (setq doc-list (lyskom-check-doc-find-keys 0 'intern))
    (mapcar (function
             (lambda (x)
               (if (not (memq (car x) doc-list))
                   (setq new-list (cons (car x) new-list)))))
            lyskom-doc-vardefs)
    (if new-list
        (progn
          (mapcar 'lyskom-make-var-doc-outline new-list)
          (save-buffer))
      (message "No new variables to document"))
  ))

(defun lyskom-make-var-doc-outline (var)
  (if (elt (assq var lyskom-doc-vardefs) 2)
      (progn
        (message "New variable: %s" var)
        (insert
         (concat
          (symbol-name var)
          "\t"
          ;; Allowed values
          "\t"
           (replace-in-string 
            (format "%S" (elt (assq var lyskom-doc-vardefs) 1))
            "\n" "")
          "\t"
          (if (or (memq var lyskom-elisp-variables)
                  (memq var lyskom-global-boolean-variables))
              "t"
            "nil")
          "\t"
          (if (elt (assq var lyskom-doc-vardefs) 2)
              "t"
            "nil")
          "\t"
          ;; Xref
          "\t"
          ;; Doc
          "\n")))))
    

(defun lyskom-check-cmd-doc ()
  (let ((doc-list nil)
        (new-list nil))
    (setq doc-list (lyskom-check-doc-find-keys 0 'intern))
    (mapcar (function
             (lambda (x)
David Byers's avatar
David Byers committed
85
86
               (if (not (memq x doc-list))
                   (setq new-list (cons x new-list)))))
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
            lyskom-commands)

    (goto-char (point-min))
;    (message "Documented functions: %S" doc-list)
    (if new-list
        (progn
          (mapcar 'lyskom-make-fn-doc-outline new-list)
          (save-buffer))
      (message "No new commands to document"))
    ))

(defun lyskom-make-fn-doc-outline (fn)
  (let ((cmdname (lyskom-command-name fn)))
    (if cmdname
        (progn
          (message "New command: %S (%s)" fn cmdname)
          (insert
           (concat
            (symbol-name fn)
            "\t"
            cmdname
            "\t"
            (lyskom-make-fn-keybindings fn)
            "\t"
            ;; Variable xref
            "\t"
            ;; Command xref
            "\t"
            ;; Documentation
            "\n")))
      (message "Command %S has no name\n"))))

(defun lyskom-make-fn-keybindings (fn)
  (mapconcat (function
              (lambda (x)
                (replace-in-string 
                 (replace-in-string
                  (replace-in-string
                   (replace-in-string
                    (replace-in-string
                     (replace-in-string
                      (key-description x)
                      "}" "")
                     "{" "")
                    "|" "")
                   "]" "")
                  "[" "")
                 "\\" "")))
             (apply 'append
                    (mapcar 
                     (function
                      (lambda (keymap)
                        (where-is-internal fn (symbol-value keymap))))
                     lyskom-doc-keymaps))
             ", "))

(defun lyskom-check-doc-find-keys (fieldno modfn)
  (let ((numlines (count-lines (point-min) (point-max)))
        (lineno 0)
        (string nil)
        (result nil)
        (regexp (concat "^"
         (let ((tmp "\\([^\t]*\\)"))
           (while (> fieldno 0)
             (setq tmp (concat "[^\t]*\t" tmp))
             (setq fieldno (1- fieldno)))
           tmp))))
             
    (goto-char (point-min))
    (while (< lineno numlines)
      (setq lineno (1+ lineno))
      (goto-line lineno)
      (setq string (buffer-substring (lyskom-bol) (lyskom-eol)))
      (string-match regexp string)
      (setq result (cons 
                    (funcall modfn (match-string 1 string)) result)))
    (nreverse result)))


  

(defun lyskom-bol ()
  (save-excursion (beginning-of-line) (point)))

(defun lyskom-eol ()
  (save-excursion (end-of-line) (point)))


(defun replace-in-string (string from to)
  (let ((i 0))
    (if (stringp from)
	(setq from (aref from 0)))
    (if (stringp to)
	(setq to (aref to 0)))
    (while (< i (length string))
      (if (= (aref string i) from)
	  (aset string i to))
      (setq i (1+ i)))
    string))