check-strings.el 12.9 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
David Byers's avatar
Ny fil    
David Byers committed
2
3
4
5
6
7
8
9
;;;;;
;;;;; $Id$
;;;;; Copyright (C) 1996  Lysator Academic Computer Association.
;;;;;
;;;;
;;;;  This file is used for checking swedish-strings.el and
;;;;  english-strings.el. Run using
;;;;
10
11
12
;;;;  emacs -batch -l check-strings.el [-l <lyskomfiles>.el] -f lyskom-check-strings
;;;;
;;;;  or by M-x lyskom-check-strings
David Byers's avatar
Ny fil    
David Byers committed
13
14
15
;;;;
;;;;

16
17
(require 'lyskom)

David Byers's avatar
David Byers committed
18
19
20
21

(defvar lcs-dont-check-ending-categories '(lyskom-command)
  "String categories where ending mismatches are OK.")

22
23
(defvar lcs-message-buffer "*LysKOM string check*")

24
25
26
27
28
29
30
31
(defun lcs-expected-missing-langs (category name)
  (cdr (assq 'lyskom-missing-languages (get name category))))

(defun lcs-ignore-ending-mismatch (category name l1 l2)
  (and name category l1 l2
       (member (cons l1 l2)
               (cdr (assq 'lyskom-ending-mismatch (get name category))))))

32
33
34
35
36
37
(defun lyskom-check-strings ()
  "Check the strings in LysKOM for sanity."
  (interactive)
  (or noninteractive
      (lcs-setup-message-buffer))

38
39
40
41
42
43
44
45
46
  (lcs-message t "Checking variables")
  (lcs-check-lyskom-commands (mapcar 'car
                                     (lcs-all-category-string
                                      'lyskom-command)))

  (lcs-message t "Checking languages: %s" (mapcar 'car lyskom-languages))
  (lcs-check-key-bindings (mapcar 'car (lcs-all-category-string
                                        'lyskom-command))
                          'lyskom-mode-map)
47
48
  (mapcar 'lcs-check-category lyskom-language-categories)

49
50
  (lcs-message t "Checking customizeable variables")
  (lcs-check-customize-variables)
51
  
52
53
;  (lcs-message t "Checking help strings")
;  (lcs-check-help-strings)
54

55
56
57
  (or noninteractive
      (display-buffer lcs-message-buffer)))

58
59
60
61
62
63
64
65
66
67
68
69
(defun lcs-check-lyskom-commands (commands)
  "Check the lyskom-commands variable"
  (let ((tmp (copy-sequence lyskom-commands)))
    (mapcar (lambda (el)
              (if (memq el lyskom-commands)
                  (setq tmp (delq el tmp))
                (lcs-message nil "(%s) Missing in lyskom-commands" el)))
            commands)
    (mapcar (lambda (el)
              (lcs-message nil "(%s) Unknown command in lyskom-commands" el))
            tmp)))

70
71
(defun lcs-check-category (category)
  "Check the strings in CATEGORY."
72
73
74
75
76
77
78
  (unless (eq category 'lyskom-help-strings)
    (lcs-message t "Checking category %s" category)
    (let ((strings (lcs-all-category-string category)))
      (while strings
        (lcs-check-strings category (car (car strings))
                           (cdr (car strings)))
        (setq strings (cdr strings))))))
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  
(defun lcs-all-category-string (category)
  "Returns list of names for CATEGORY, and their strings."
  (mapcar (function
	   (lambda (symbol)
	     (let ((info (get symbol category)))
	       (if info (cons symbol info)))))
	  (get category 'lyskom-language-symbols)))

(defun lcs-check-strings (category name strings)
  "Check the strings in CATEGORY named NAME.
STRINGS is a list of (language . string)."
;;  (lcs-message t "Checking %s:%s" category name)
  (let ((format-list 'uninitialized)
	(first-str nil)
94
	(first-lang nil)
95
96
97
98
99
100
	(langs (mapcar 'car lyskom-languages)))
    (while strings
      (let* ((lang (car (car strings)))
	     (str (cdr (car strings)))
	     (flist (lcs-check-string category name lang str)))
	(if (listp format-list)
David Byers's avatar
David Byers committed
101
102
103
104
105
106
107
	    (progn 
              (or (lcs-check-format-string format-list flist)
                  (lcs-message nil "(%s:%s) Format mismatch\n    %S\n    %S"
                               category name first-str str))
              (and 
               (not (memq category lcs-dont-check-ending-categories))
               (or (lcs-check-string-ending first-str str)
108
                   (lcs-ignore-ending-mismatch category name first-lang lang)
David Byers's avatar
David Byers committed
109
110
                   (lcs-message nil "(%s:%s) Ending mismatch\n    %S\n    %S"
                               category name first-str str))))
111
	  (setq format-list flist
112
113
		first-str str
                first-lang lang))
114
115
116
117

	(setq strings (cdr strings)
	      langs (delq lang langs))))

118
119
120
121
    (mapcar (lambda (el)
              (setq langs (delq el langs)))
            (lcs-expected-missing-langs category name))

122
123
124
125
    (if langs
	(lcs-message nil "(%s:%s) Missing languages %s"
		     category name langs))))

126
(defun lcs-check-key-bindings (commands keymap &optional show-all-missing)
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  "Check the commands in CATEGORY named NAME for bindings 
in lyskom-X-mode-map"
  (let* ((langs (mapcar 'car lyskom-languages))
         (keymaps (get keymap 'lyskom-language-keymap))
         (all-missing nil))
    (while commands
      (let ((lang-c (copy-sequence langs)))
        (mapcar (lambda (lang)
                  (when (where-is-internal (car commands)
                                           (symbol-value (cdr (assq lang keymaps))))
                    (setq lang-c (delq lang lang-c))))
                langs)
        (cond ((equal lang-c langs) (setq all-missing
                                          (cons (car commands) all-missing)))
              (lang-c 
               (lcs-message nil "(%s:%s) Missing binding in %s" keymap 
                            (car commands) lang-c)))
        (setq commands (cdr commands))))
145
146
147
148
    (when show-all-missing
      (mapcar (lambda (el)
                (lcs-message nil "(%s:%s) Missing in %s" keymap el langs))
              all-missing))))
149
150


151
152
153
154
155
156
157
158
159
(defun lcs-check-string (category name lang string)
  "Check the string in CATEGORY named NAME in language LANG.
STRING is the string."
  (if (and string (stringp string))
      (lcs-split-format-string string)
    nil))

(defun lcs-split-format-string (string)
  "Extract the formatters from STRING."
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  (let ((result nil)
	(start 0))
    (while (and (< start (length string))
		(string-match lyskom-format-format
			      string
			      start)
		(setq start (match-end 0))
		(setq result (cons 
			      (concat
			       (match-string 1 string)
			       (match-string 3 string)
			       (match-string 5 string)
			       (match-string 6 string))
			      result))))
    result))


David Byers's avatar
David Byers committed
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
(defconst lcs-match-endings 
  '("\\." "\\?" ":" "!" "\\.\n" "\\?\n" ":\n" "!\n" "\n" ")" ")\n"
    "\\? +" ": +" "\\? +\n" ": +\n")
  "String endings that should be identical in various languages.")

(defun lcs-check-string-ending (template str)
  (cond ((or (not (stringp template))
             (not (stringp str))) t)
        (t (let ((result
                  (mapcar
                   (function
                    (lambda (x)
                      (let ((pat (concat "\\`\\(.\\|\n\\)*" x "\\'")))
                        (eq (string-match pat template)
                            (string-match pat str)))))
                   lcs-match-endings)))
             (cond ((memq nil result) nil)
                   (t t))))))
    

197
198
199
200
201
202
203
204
205
206
207
(defun lcs-check-format-string (template flist)
  "Match the formatters in TEMPLATE to those in FLIST."
  (let* ((result t))
    (setq template (copy-sequence template))
    (while flist
      (if (not (member (car flist) template))
	  (setq result nil flist nil)
	(setq template (delete (car flist) template))
	(setq flist (delete (car flist) flist))))
    (and result (null template))))

208
209
210
211
212
213
214
215
216
217
218
(defun lcs-check-customize-variables ()
  "Check customize variables.
Check that all customizeable variables are server-stored.
Check that all server-stored variables are customizeable."
  (let ((cust-vars-in-buffer
         (delq nil (mapcar (lambda (el) (when (vectorp el) (elt el 0))) 
                           lyskom-customize-buffer-format)))
        (cust-vars-widgets (mapcar 'car lyskom-custom-variables))
        (cust-vars-all (append lyskom-elisp-variables 
                               lyskom-global-boolean-variables 
                               lyskom-global-non-boolean-variables)))
219
220
221
222
223

    ;; Check that variables have widget definitions and are in the
    ;; customize buffer, or are declared missing and really *are*
    ;; missing.

224
225
    (mapcar 
     (lambda (var)
226
227
228
229
230
231
232
233
       (cond ((memq var lyskom-custom-variables-missing)
              (if (memq var cust-vars-widgets)
                  (lcs-message nil "(%s:%s) Widget definition for variable declared as missing."
                               'lyskom-custom-variables var))
              (if (memq var cust-vars-in-buffer)
                  (lcs-message nil "(%s:%s) Variable declared as missing in custom buffer."
                               'lyskom-custom-variables var))
              (if (lyskom-get-string-internal (intern (format "%s-tag" var))
234
                                              'Lyskom-custom-strings)
235
236
237
238
239
240
241
                  (lcs-message nil "(%s:%s) Tag string for variable declared as missing."
                               'lyskom-custom-strings var))
              (if (lyskom-get-string-internal (intern (format "%s-doc" var))
                                              'lyskom-custom-strings)
                  (lcs-message nil "(%s:%s) Doc string for variable declared as missing."
                               'lyskom-custom-strings var)))
             ((not (memq var cust-vars-widgets))
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
              (lcs-message nil "(%s:%s) No widget definition for variable."
                           'lyskom-custom-variables var))
             ((not (memq var cust-vars-in-buffer))
              (lcs-message nil "(%s:%s) Variable not in customize buffer."
                           'lyskom-customize-buffer-format var))
             (t (unless (lyskom-get-string-internal 
                         (intern (format "%s-tag" var))
                         'lyskom-custom-strings)
                  (lcs-message nil "(%s:%s) No tag string"
                               'lyskom-custom-strings var))
                (unless (lyskom-get-string-internal 
                         (intern (format "%s-doc" var))
                         'lyskom-custom-strings)
                  (lcs-message nil "(%s:%s) No doc string"
                               'lyskom-custom-strings var))))
       (setq cust-vars-in-buffer (delq var cust-vars-in-buffer))
       (setq cust-vars-widgets (delq var cust-vars-widgets))
       nil)
     cust-vars-all)

    (mapcar (lambda (var)
              (lcs-message nil "(%s:%s) Not a server-stored variable."
                           'lyskom-custom-variables var))
            cust-vars-widgets)
    (mapcar (lambda (var)
              (lcs-message nil "(%s:%s) Not a server-stored variable."
                           'lyskom-custom-variables var))
            cust-vars-in-buffer)))
270
271
272

(defun lcs-check-help-strings ()
  "Check help strings"
273
  (lcs-check-category 'lyskom-help-strings)
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
  (let ((commands (mapcar 'car
                          (lcs-all-category-string
                           'lyskom-command)))
        (help-strings (mapcar 'car
                              (lcs-all-category-string 'lyskom-help-strings)))
        (help-symbols nil)
        (tmp (apply 'append (mapcar 'cdr lyskom-help-categories))))

    ;; Extract information from the help categories into a plist
    ;; where keys are item types and values are list of symbols
    ;; that appear in the items.

    (while tmp
      (setq help-symbols
            (plist-put help-symbols
                       (elt (car tmp) 0)
                       (cons (elt (car tmp) 1)
                             (plist-get help-symbols (elt (car tmp) 0)))))
      (setq tmp (cdr tmp)))

   ;; Ensure that all commands are listed in the help strings variable
    (lcs-check-help-strings-2 "command" commands)
    (lcs-check-help-strings-2 "help category" 
                              (mapcar 'car lyskom-help-categories))

    ;; Ensure that all commands are in some command category
    (setq tmp commands)
    (while tmp
      (unless (memq (car tmp) (plist-get help-symbols 'command))
        (lcs-message nil "(%s:%s) Command not categorized."
                     'lyskom-help-categories (car tmp)))
      (setq tmp (cdr tmp)))

    ;; Ensure that all referenced categories exist 
    (setq tmp (plist-get help-symbols 'category))
    (while tmp
      (unless (assq (car tmp) lyskom-help-categories)
        (lcs-message nil "(%s:%s) Reference to help missing category."
                     'lyskom-help-categories (car tmp)))
      (setq tmp (cdr tmp)))
    )) 

(defun lcs-check-help-strings-2 (wot syms)
  (while syms
    (unless (memq (car syms) help-strings)
      (lcs-message nil "(%s:%s) Help string missing for %s."
                   'lyskom-help-strings
                   (car syms)
                   wot))
      (setq syms (cdr syms))))
                   
325
326
             
             
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
(defun lcs-setup-message-buffer ()
  "Inititalize the message buffer for string checking."
  (save-excursion
    (set-buffer (get-buffer-create lcs-message-buffer))
    (erase-buffer)))

(defun lcs-message (echo format &rest args)
  "Display a message during string checking.
If ECHO is non-nil display the message in the echo area, otherwise
only append it to the log buffer.
FORMAT and ARGS are as for `format'."
  (let ((msg (apply 'format format args)))
    (if noninteractive
	(princ (concat msg "\n") t)
      (save-excursion
	(set-buffer lcs-message-buffer)
	(goto-char (point-max))
	(insert msg "\n"))
      (if echo
	  (message msg)))))
	
    
David Byers's avatar
David Byers committed
349