language.el 11.8 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
David Kågedal's avatar
David Kågedal committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
David Kågedal's avatar
David Kågedal committed
5
;;;;;
6
;;;;; This file is part of the LysKOM Emacs LISP client.
David Kågedal's avatar
David Kågedal committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
;;;;; the Free Software Foundation; either version 2, or (at your option) 
;;;;; any later version.
;;;;; 
;;;;; LysKOM is distributed in the hope that it will be useful, but WITHOUT
;;;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;;;; for more details.
;;;;; 
;;;;; You should have received a copy of the GNU General Public License
;;;;; along with LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: language.el
;;;; Author: Niels Mller
;;;;
;;;;

(require 'lyskom-vars "vars")
Niels Möller's avatar
Niels Möller committed
35

36
37
;;; Variables

38
39
;;(defvar lyskom-language-symbols nil
;;  "Symbols with language data bound to them")
40

41
(defvar lyskom-languages nil
42
  "An alist of defined languages.
43
44
45
Each entry is a pair (SYMBOL . (NAME NAME ...)) where symbol is the symbol
used for identification, and the NAMEs are names of the language.")

46
(defvar lyskom-language-categories nil
David Byers's avatar
David Byers committed
47
48
49
  "Categories of language-specific variables.
Each element is a cons cell (NAME . SCOPE), where NAME is the name of
the category and SCOPE is its scope (global or local).")
50

David Kågedal's avatar
David Kågedal committed
51
(defvar lyskom-language-vars nil
David Byers's avatar
David Byers committed
52
53
54
  "A list of all language-dependent variables.
Each element is a cons cell (NAME . SCOPE), where NAME is the name of
the category and SCOPE is its scope (global or local).")
David Kågedal's avatar
David Kågedal committed
55

David Byers's avatar
David Byers committed
56
57

(defun lyskom-language-var-internal (scope var language val)
David Kågedal's avatar
David Kågedal committed
58
  "Defines a language-local variable value."
David Byers's avatar
David Byers committed
59
  (or (assq var lyskom-language-vars)
David Kågedal's avatar
David Kågedal committed
60
      (setq lyskom-language-vars
David Byers's avatar
David Byers committed
61
	    (cons (cons var scope) lyskom-language-vars)))
David Kågedal's avatar
David Kågedal committed
62
63
64
65
66
67
  (let* ((alist (get var 'lyskom-language-var))
	 (entry (assq language alist)))
    (if entry
	(setcdr entry val)
      (put var 'lyskom-language-var (cons (cons language val) alist)))))

David Byers's avatar
David Byers committed
68
69
70
71
72
(defmacro lyskom-language-var (scope var language val)
  `(lyskom-language-var-internal ',scope
                                 ',var
                                 ',language
                                 ',val))
David Kågedal's avatar
David Kågedal committed
73
74
75

(put 'lyskom-language-var 'lisp-indent-function 2)

David Byers's avatar
David Byers committed
76
77
78
79
80
(defun lyskom-set-language-vars (language scope)
  "Set language-specific variables to values for LANGUAGE.
SCOPE must be one of global or local, and specifies the scope of the change.
If SCOPE is global, change all variables, even those that affect multiple
sessions."
81
  (mapcar
David Byers's avatar
David Byers committed
82
83
84
85
86
87
88
89
   (lambda (spec)
     (let ((var (car spec))
           (var-scope (cdr spec)))
       (when (or (eq scope 'global) (eq var-scope 'local))
         (when (or (not (symbol-value var))
                   (get var 'lyskom-language-force))
           (set var (eval (cdr (assq language
                                     (get var 'lyskom-language-var)))))))))
90
91
92
93
94
   lyskom-language-vars))

;;; Keymaps

(defvar lyskom-language-keymaps nil
95
  "A list of all language-dependent keymaps.")
96
97

(defun lyskom-language-keymap-internal (keymap language langmap)
98
  "Defines a language-local keymap."
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
  ;; If the "real" keymap has no value, set it to an empty keymap
  (if (eval keymap)
      nil
    (set keymap (make-sparse-keymap)))
  ;; Add it to the list of keymaps
  (or (memq keymap lyskom-language-keymaps)
      (setq lyskom-language-keymaps
	    (cons keymap lyskom-language-keymaps)))
  ;; Modify the property list
  (let* ((alist (get keymap 'lyskom-language-keymap))
	 (entry (assq language alist)))
    (if entry
	(setcdr entry langmap)
      (put keymap 'lyskom-language-keymap
	   (cons (cons language langmap) alist)))))

(defmacro lyskom-language-keymap (keymap language langmap)
David Byers's avatar
David Byers committed
116
117
118
  `(lyskom-language-keymap-internal ',keymap
                                    ',language
                                    ',langmap))
119
120
121
122
123

(put 'lyskom-language-keymap 'lisp-indent-function 2)

(defun lyskom-set-language-keymaps (language)
  (mapcar
David Byers's avatar
David Byers committed
124
125
126
127
128
   (lambda (map)
     (set-keymap-parent (symbol-value map)
                        (eval (cdr (assq language
                                         (get map
                                              'lyskom-language-keymap))))))
129
130
   lyskom-language-keymaps))

David Byers's avatar
X    
David Byers committed
131
132
133
134
135
136
137
138
139
;(defun lyskom-set-language-keymaps (language)
;  (mapcar
;   (function
;    (lambda (map)
;      (setcdr (symbol-value map)
;	      (eval (cdr (assq language
;			       (get map 'lyskom-language-keymap)))))))
;   lyskom-language-keymaps))

140
141
;;; String catalogs

David Byers's avatar
David Byers committed
142
(defun lyskom-language-strings-internal (scope category language alist)
Niels Möller's avatar
Niels Möller committed
143
  "Associates names to symbols.
David Byers's avatar
David Byers committed
144
145
See documentation of lyskom-language-strings for information on the 
parameters to this function."
146
  ;; Record category
David Byers's avatar
David Byers committed
147
  (or (assq category lyskom-language-categories)
148
      (setq lyskom-language-categories
David Byers's avatar
David Byers committed
149
	    (cons (cons category scope) lyskom-language-categories)))
David Byers's avatar
David Byers committed
150
  (let ((record (get category 'lyskom-language-symbols)))
David Byers's avatar
David Byers committed
151
152
153
154
155
156
157
158
159
160
161
    (mapcar (lambda (pair)
              (let* ((symbol (car pair))
                     (string (cdr pair))
                     (llist (get symbol category))
                     (entry (assq language llist)))
                ;; Record symbol
                (or (memq symbol record)
                    (setq record (cons symbol record)))
                (if entry
                    (setcdr entry string)
                  (put symbol category (cons (cons language string) llist)))))
162
163
	    alist)
    (put category 'lyskom-language-symbols record)))
Niels Möller's avatar
Niels Möller committed
164

David Byers's avatar
David Byers committed
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
(defmacro lyskom-language-strings (scope category language alist)
  "Define a category of strings.
SCOPE is the scope of language-specificity. If it is global, then these
strings apply globally and will not be altered by changing the session
language.

CATEGORY is the name of the category.

LANGUAGE is the language, a symbol denoting the ISO639 language code.

ALIST is the list of strings."
  `(lyskom-language-strings-internal ',scope
                                     ',category
                                     ',language
                                     ,alist))
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201

(defun lyskom-language-missing-string-internal (category string languages)
  (let ((old-missing (assq 'lyskom-missing-languages (get string category))))
    (if old-missing
        (setcdr old-missing (append languages (cdr old-missing)))
      (put string category (cons (cons 'lyskom-missing-languages languages)
                                 (get string category))))))

(defun lyskom-language-ending-mismatch-internal (category string l1 l2)
  (let ((old-mismatch (assq 'lyskom-ending-mismatch (get string category))))
    (if old-mismatch
        (setcdr old-mismatch (append (list (cons l1 l2) (cons l2 l1))
                                     (cdr old-mismatch)))
      (put string category (cons (cons 'lyskom-ending-mismatch 
                                       (list (cons l1 l2) (cons l2 l1)))
                                 (get string category))))))

(defmacro lyskom-language-missing-string (category string &rest languages)
  `(lyskom-language-missing-string-internal ',category ',string ',languages))

(defmacro lyskom-language-ending-mismatch (category string l1 l2)
  `(lyskom-language-ending-mismatch-internal ',category ',string ',l1 ',l2))
202

David Kågedal's avatar
David Kågedal committed
203
(put 'lyskom-language-strings 'lisp-indent-function 2)
Niels Möller's avatar
Niels Möller committed
204

David Byers's avatar
David Byers committed
205
206
207
(defsubst lyskom-tell-string (key)
  "Retrieve the phrase indexed by the key from the kom-tell-phrases
assoc list."
David Byers's avatar
David Byers committed
208
209
  (condition-case nil
      (lyskom-get-string key 'kom-tell-phrases)
210
211
    (lyskom-internal-error (message "Bad kom-tell-phrases: missing %s" key)
                           "")))
David Byers's avatar
David Byers committed
212

213
(defsubst lyskom-try-get-string (symbol category)
David Byers's avatar
David Byers committed
214
215
216
217
    (cdr (assq (if (eq (cdr (assq category lyskom-language-categories)) 'local)
                   lyskom-language
                 lyskom-global-language)
               (get symbol category))))
218
219
220
221
222

(defsubst lyskom-get-string-error (function symbol category)
  (signal 'lyskom-internal-error
	  (list function (list symbol category ": string not found"))))

Niels Möller's avatar
Niels Möller committed
223
(defun lyskom-get-string (symbol &optional category)
224
  "Returns string associated with SYMBOL"
225
    (or (lyskom-try-get-string symbol (or category 'lyskom-message))
226
227
228
229
        (lyskom-get-string-error 'lyskom-get-string
                                 symbol
                                 (or category 'lyskom-message))))

230
231
232
233
(defun lyskom-get-string-sol (symbol &optional category)
  "Returns string associated with SYMBOL
If kom-long-lines is set, return the long form of the string, if it exists."
  (or  (and kom-long-lines
234
            (lyskom-try-get-string (intern (concat (symbol-name symbol)
235
236
                                                        "-long"))
                                        (or category 'lyskom-message)))
237
       (lyskom-try-get-string symbol
238
239
240
241
242
                                   (or category 'lyskom-message))
       (lyskom-get-string-error 'lyskom-get-string
                                symbol
                                (or category 'lyskom-message))))

Niels Möller's avatar
Niels Möller committed
243
244
245
246

(defun lyskom-get-strings (symbols &optional category)
  "Returns an alist of (symbol . string) pairs

247
according to CATEGORY and lyskom-language. Kind of inverse to
Niels Möller's avatar
Niels Möller committed
248
lyskom-define-language."
David Byers's avatar
David Byers committed
249
250
  (mapcar (lambda (symbol)
            (cons symbol (lyskom-get-string symbol category)))
Niels Möller's avatar
Niels Möller committed
251
252
	  symbols))

253
254
255
256
257
(defun lyskom-get-menu-string (symbol)
  "Returns the name of a menu(item)

Looks for the 'lyskom-menu category, or 'lyskom-command
if 'lyskom-menu is not found."
David Byers's avatar
David Byers committed
258
  (encode-coding-string 
259
260
    (or (lyskom-try-get-string symbol 'lyskom-menu)
        (lyskom-try-get-string symbol 'lyskom-command)
David Byers's avatar
David Byers committed
261
262
        (lyskom-get-string-error 'lyskom-get-menu-string symbol 'lyskom-menu))
    'iso-8859-1))
263
264

(defun lyskom-string-check-category (category)
265
  "Returns list of names for the category, and their supported languages"
David Byers's avatar
David Byers committed
266
267
268
  (mapcar (lambda (symbol)
            (let ((info (get symbol category)))
              (if info (cons symbol (mapcar 'car info)))))
269
	  (get category 'lyskom-language-symbols)))
270

271

272
(defun lyskom-define-language (language coding &rest names)
273
274
275
  (let ((match (assq language lyskom-languages)))
    (if match
	(setcdr match names)
David Byers's avatar
David Byers committed
276
      (setq lyskom-languages (cons (cons language names) lyskom-languages))))
277
278
279
280
281
  (put language 'lyskom-language-coding coding))

(defun lyskom-language-coding (language)
  (or (get language 'lyskom-language-coding)
      'raw-text))
David Byers's avatar
David Byers committed
282
283
284
285
286
287
288
289

(defun lyskom-language-name (language)
  "Return the name of language code LANGUAGE in the current language."
  (save-excursion
    (when lyskom-buffer (set-buffer lyskom-buffer))
    (or (cdr (assq language lyskom-language-codes))
        (lyskom-format (cdr (assq '-- lyskom-language-codes))
                       (symbol-name language)))))
290

David Byers's avatar
David Byers committed
291
(defun lyskom-set-language (language scope)
292
293
  "Set the current language to LANGUAGE.
Returns non-nil on success and nil on failure."
David Byers's avatar
David Byers committed
294
295
296
297
  (cond ((not (assq language lyskom-languages))
         (lyskom-format-insert-before-prompt 'language-not-loaded
                                             (lyskom-language-name language))
         nil)
David Byers's avatar
David Byers committed
298
299
300
301
302
303
304
305
306
307
308
309
        (t 
         (cond ((eq scope 'local) 
                (setq lyskom-language language))
               ((eq scope 'global) 
                (setq lyskom-global-language language)
                (lyskom-set-language-keymaps language)))
         (lyskom-set-language-vars language scope)
         (when (eq scope 'global) (lyskom-update-menus))
         (lyskom-update-prompt t)
         (lyskom-update-command-completion)
         t)))

310
(eval-and-compile (provide 'lyskom-language))
David Kågedal's avatar
David Kågedal committed
311
312

;;; language.el ends here