language.el 7.97 KB
Newer Older
David Kågedal's avatar
David Kågedal committed
1
2
3
4
5
6
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
;;;;;
;;;;; $Id$
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
;;;;;
;;;;; This file is part of the LysKOM server.
;;;;; 
;;;;; 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
34

35
36
;;; Variables

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

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

45
46
(defvar lyskom-language-categories nil
  "Categories used")
47

David Kågedal's avatar
David Kågedal committed
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
(defvar lyskom-language-vars nil
  "A list of all language-dependent variables.")

(defun lyskom-language-var-internal (var language val)
  "Defines a language-local variable value."
  (or (memq var lyskom-language-vars)
      (setq lyskom-language-vars
	    (cons var lyskom-language-vars)))
  (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)))))

(defmacro lyskom-language-var (var language val)
  (list 'lyskom-language-var-internal
	(list 'quote var)
	(list 'quote language)
	(list 'quote val)))

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

70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
(defun lyskom-set-language-vars (language)
  (mapcar
   (function
    (lambda (var)
      (if (or (not (symbol-value var))
	      (get var 'lyskom-language-force))
	  (set var (eval (cdr (assq language
				    (get var 'lyskom-language-var))))))))
   lyskom-language-vars))

;;; Keymaps

(defvar lyskom-language-keymaps nil
  "A list of all language-dependent variables.")

(defun lyskom-language-keymap-internal (keymap language langmap)
  "Defines a language-local variable value."
  ;; 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)
  (list 'lyskom-language-keymap-internal
	(list 'quote keymap)
	(list 'quote language)
	(list 'quote langmap)))

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

(defun lyskom-set-language-keymaps (language)
  (mapcar
   (function
    (lambda (map)
David Byers's avatar
X    
David Byers committed
115
116
117
118
      (set-keymap-parent (symbol-value map)
                         (eval (cdr (assq language
                                          (get map
                                               'lyskom-language-keymap)))))))
119
120
   lyskom-language-keymaps))

David Byers's avatar
X    
David Byers committed
121
122
123
124
125
126
127
128
129
;(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))

130
131
132
;;; String catalogs

(defun lyskom-language-strings-internal (category language alist)
Niels Möller's avatar
Niels Möller committed
133
134
135
136
  "Associates names to symbols.

CATEGORY and LANGUAGE determines what kind of association to
create. ALIST is a mapping from symbols to strings."
137
138
139
140
  ;; Record category
  (or (memq category lyskom-language-categories)
      (setq lyskom-language-categories
	    (cons category lyskom-language-categories)))
David Byers's avatar
David Byers committed
141
  (let ((record (get category 'lyskom-language-symbols)))
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
    (mapcar (function (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))))))
	    alist)
    (put category 'lyskom-language-symbols record)))
Niels Möller's avatar
Niels Möller committed
157

158
159
160
161
162
163
(defmacro lyskom-language-strings (category language alist)
  (list 'lyskom-language-strings-internal
	(list 'quote category)
	(list 'quote language)
	alist)) 

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

166
167
168
169
170
171
172
(defsubst lyskom-get-string-internal (symbol category)
    (cdr (assq lyskom-language (get symbol category))))

(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
173
174
(defun lyskom-get-string (symbol &optional category)
  "Returns string assiciated with SYMBOL"
175
176
177
178
179
    (or (lyskom-get-string-internal symbol (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
180
181
182
183

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

184
according to CATEGORY and lyskom-language. Kind of inverse to
Niels Möller's avatar
Niels Möller committed
185
186
187
188
189
lyskom-define-language."
  (mapcar (function (lambda (symbol)
		      (cons symbol (lyskom-get-string symbol category))))
	  symbols))

190
191
192
193
194
195
196
197
198
199
(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."
    (or (lyskom-get-string-internal symbol 'lyskom-menu)
        (lyskom-get-string-internal symbol 'lyskom-command)
        (lyskom-get-string-error 'lyskom-get-menu-string symbol 'lyskom-menu)))

(defun lyskom-string-check-category (category)
200
201
202
203
204
205
  "Returns list of names for the category, and their supported languages"
  (mapcar (function
	   (lambda (symbol)
	     (let ((info (get symbol category)))
	       (if info (cons symbol (mapcar 'car info))))))
	  (get category 'lyskom-language-symbols)))
206

207

208
209
210
211
(defun lyskom-define-language (language &rest names)
  (let ((match (assq language lyskom-languages)))
    (if match
	(setcdr match names)
David Byers's avatar
David Byers committed
212
213
214
215
216
217
218
219
220
221
222
223
224
      (setq lyskom-languages (cons (cons language names) lyskom-languages))))
  (unless (and lyskom-language
               kom-default-language)
    (setq lyskom-language language)
    (setq kom-default-language language)))

(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)))))
225
226
227

(defun lyskom-set-language (language)
  "Set the current language to LANGUAGE."
David Byers's avatar
David Byers committed
228
229
230
231
232
233
234
235
236
237
  (cond ((not (assq language lyskom-languages))
         (lyskom-format-insert-before-prompt 'language-not-loaded
                                             (lyskom-language-name language))
         nil)
        (t (setq lyskom-language language)
           (lyskom-set-language-vars language)
           (lyskom-set-language-keymaps language)
           (lyskom-update-menus)
           (lyskom-update-prompt t)
           t)))
238

239
			      
240
(eval-and-compile (provide 'lyskom-language))
David Kågedal's avatar
David Kågedal committed
241
242

;;; language.el ends here