check-strings.el 14.7 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
  (lcs-message t "Checking face schemes")
  (lcs-check-face-schemes)

41
  (lcs-message t "Checking variables")
David Byers's avatar
David Byers committed
42
  (lcs-check-language-vars)
43
44
45
46
47
48
49
50
  (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)
David Byers's avatar
David Byers committed
51
  (mapcar (lambda (x) (lcs-check-category (car x))) lyskom-language-categories)
52

53
54
  (lcs-message t "Checking customizeable variables")
  (lcs-check-customize-variables)
55
  
56
57
;  (lcs-message t "Checking help strings")
;  (lcs-check-help-strings)
58

59
60
61
  (or noninteractive
      (display-buffer lcs-message-buffer)))

62
63
64
(defun lcs-check-face-schemes ()
  "Check that all face schemes seem to be OK."
  (lyskom-traverse scheme lyskom-face-schemes
David Byers's avatar
David Byers committed
65
    (let ((faces (delq 'property (mapcar 'car (cdr scheme))))
66
67
68
69
70
71
72
73
74
75
76
77
78
          (tmp nil))
      (lyskom-traverse expected-face lyskom-faces
        (if (setq tmp (memq expected-face faces))
            (progn (when (memq expected-face (cdr tmp))
                     (lcs-message nil "(%s) Duplicate face in scheme: %s"
                                  (car scheme) expected-face))
                   (setq faces (delq expected-face faces)))
          (lcs-message nil "(%s) Face scheme missing face: %s"
                       (car scheme) expected-face)))
      (lyskom-traverse extra-face faces
        (lcs-message nil "(%s) Face scheme contains unknown face: %s"
                     (car scheme) extra-face)))))

David Byers's avatar
David Byers committed
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
(defun lcs-check-language-vars ()
  "Check that all language-specific variables exist in all languages"
  (mapcar (lambda (var)
            (setq var (car var))
            (let ((missing 
                   (delq nil 
                         (mapcar
                          (lambda (lang)
                            (setq lang (car lang))
                            (unless (assq lang (get var 'lyskom-language-var))
                              lang))
                          lyskom-languages))))
              (when missing
                (lcs-message nil "(%s) Missing languages: %s" var missing))))
          lyskom-language-vars))

95
96
97
98
99
100
101
102
103
104
105
106
(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)))

107
108
(defun lcs-check-category (category)
  "Check the strings in CATEGORY."
109
110
111
112
113
114
115
  (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))))))
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
  
(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)
131
	(first-lang nil)
132
133
134
135
136
137
	(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
138
139
140
141
142
143
144
	    (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)
145
                   (lcs-ignore-ending-mismatch category name first-lang lang)
David Byers's avatar
David Byers committed
146
147
                   (lcs-message nil "(%s:%s) Ending mismatch\n    %S\n    %S"
                               category name first-str str))))
148
	  (setq format-list flist
149
150
		first-str str
                first-lang lang))
151
152
153
154

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

155
156
157
158
    (mapcar (lambda (el)
              (setq langs (delq el langs)))
            (lcs-expected-missing-langs category name))

159
160
161
162
    (if langs
	(lcs-message nil "(%s:%s) Missing languages %s"
		     category name langs))))

163
(defun lcs-check-key-bindings (commands keymap &optional show-all-missing)
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  "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))))
182
183
184
185
    (when show-all-missing
      (mapcar (lambda (el)
                (lcs-message nil "(%s:%s) Missing in %s" keymap el langs))
              all-missing))))
186
187


188
189
190
191
192
193
194
195
196
(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."
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
  (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(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))))))
    
233
234
235
236
237
238
(defun lcs-delete-format-arg (el flist)
  (let ((result nil))
    (while (and flist (not (equal el (car flist))))
      (setq result (cons (car flist) result)
            flist (cdr flist)))
    (setq result (nconc (nreverse result) (cdr flist)))))
David Byers's avatar
David Byers committed
239

240
241
242
243
244
245
246
(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)
247
248
	(setq template (lcs-delete-format-arg (car flist) template))
	(setq flist (lcs-delete-format-arg (car flist) flist))))
249
250
    (and result (null template))))

251
252
253
254
255
256
257
258
259
260
261
(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)))
262
263
264
265
266

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

267
268
    (mapcar 
     (lambda (var)
269
270
271
272
273
274
275
       (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))
276
              (if (lyskom-try-get-string (intern (format "%s-tag" var))
277
                                              'Lyskom-custom-strings)
278
279
                  (lcs-message nil "(%s:%s) Tag string for variable declared as missing."
                               'lyskom-custom-strings var))
280
              (if (lyskom-try-get-string (intern (format "%s-doc" var))
281
282
283
284
                                              '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))
285
286
287
288
289
              (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))
290
             (t (unless (lyskom-try-get-string 
291
292
293
294
                         (intern (format "%s-tag" var))
                         'lyskom-custom-strings)
                  (lcs-message nil "(%s:%s) No tag string"
                               'lyskom-custom-strings var))
295
                (unless (lyskom-try-get-string 
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
                         (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)))
313
314
315

(defun lcs-check-help-strings ()
  "Check help strings"
316
  (lcs-check-category 'lyskom-help-strings)
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
  (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))))
                   
368
369
             
             
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
(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
392