check-strings.el 9.03 KB
Newer Older
1
;;;;; -*-coding: raw-text;-*-
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
24
25
26
27
28
29
(defvar lcs-message-buffer "*LysKOM string check*")

(defun lyskom-check-strings ()
  "Check the strings in LysKOM for sanity."
  (interactive)
  (or noninteractive
      (lcs-setup-message-buffer))

30
31
32
33
34
35
36
37
38
  (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)
39
40
  (mapcar 'lcs-check-category lyskom-language-categories)

41
42
43
  (lcs-message t "Checking customizeable variables")
  (lcs-check-customize-variables)

44
45
46
  (or noninteractive
      (display-buffer lcs-message-buffer)))

47
48
49
50
51
52
53
54
55
56
57
58
(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)))

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
85
86
87
(defun lcs-check-category (category)
  "Check the strings in CATEGORY."
  (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)))))
  
(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)
	(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
88
89
90
91
92
93
94
95
96
	    (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)
                   (lcs-message nil "(%s:%s) Ending mismatch\n    %S\n    %S"
                               category name first-str str))))
97
98
99
100
101
102
103
104
105
106
	  (setq format-list flist
		first-str str))

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

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

107
(defun lcs-check-key-bindings (commands keymap &optional show-all-missing)
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
  "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))))
126
127
128
129
    (when show-all-missing
      (mapcar (lambda (el)
                (lcs-message nil "(%s:%s) Missing in %s" keymap el langs))
              all-missing))))
130
131


132
133
134
135
136
137
138
139
140
(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."
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
  (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
(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))))))
    

178
179
180
181
182
183
184
185
186
187
188
(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))))

189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
(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)))
    (mapcar 
     (lambda (var)
       (cond ((not (memq var cust-vars-widgets))
              (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)))
             
             
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
(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
255