flags.el 21.9 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
Linus Tolke's avatar
Linus Tolke committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
5
;;;;;
6
;;;;; This file is part of the LysKOM Emacs LISP client.
Linus Tolke's avatar
Linus Tolke committed
7
8
9
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
10
;;;;; the Free Software Foundation; either version 2, or (at your option) 
Linus Tolke's avatar
Linus Tolke committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;;;;; 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. 
;;;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
26
27
28
29
30
31
32
33
34
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: flags.el
;;;;
;;;; This file contains code for editing the user variables and saving
;;;; them in the user area.
;;;;

35
36
37
38
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))

39
40
41
(eval-when-compile
  (require 'lyskom-command "command"))

42
43
(put 'lyskom-invalid-flag-type 'error-conditions
     '(error lyskom-error lyskom-invalid-flag-type))
44

45
46
(defvar lyskom-options-text nil
  "Text mass when reading options.")
47

David Byers's avatar
X    
David Byers committed
48
49
50
(def-kom-var lyskom-options-done nil
  "When we have read all options this is turned non-nil."
  local)
Linus Tolke's avatar
Linus Tolke committed
51

52
(def-kom-command kom-save-options (arg)
53
  "Save all LysKOM settings. This will save settings in the server but
54
55
not save anything to your Emacs init file (usually .emacs). With a 
prefix argument, remove all unknown settings.
56
57
58

To edit settings, use `kom-customize'. This command can also save
settings to your emacs init file."
59
60
  (interactive "P")
  (when arg (setq lyskom-saved-unknown-variables nil))
61
62
63
64
65
66
  (lyskom-save-options (or lyskom-buffer 
                           (current-buffer))
                       (lyskom-get-string 'saving-settings)
                       (lyskom-get-string 'saving-settings-done)
                       (lyskom-get-string 'could-not-save-options)))

67
(def-kom-command kom-copy-options ()
68
69
70
71
72
73
74
  "Copy settings from one LysKOM session to another. This is useful if
you have configured on session to your liking and want ths settings to
be used on other servers as well.

This command will save settings to the LysKOM server, but will not
save anything to your Emacs init file. Use `kom-customize' to edit
settings and save them to your emacs init file."
75
76
77
78
79
80
81
82
83
84
85
86
87
88
  (interactive)
  (let* ((completions
          (mapcar (lambda (x)
                    (cons (save-excursion
                            (set-buffer x)
                            (buffer-name))
                          x))
                  (delq (or lyskom-buffer (current-buffer))
                        (copy-sequence lyskom-buffer-list))))
         (from-session (completing-read 
                        (lyskom-get-string 'session-to-copy-options-from)
                        completions
                        nil t)))
    (lyskom-message (lyskom-get-string 'reading-settings-from) from-session)
89
    (setq lyskom-saved-unknown-variables (lyskom-read-options from-session))
90
91
92
93
94
95
96
97
98
    (lyskom-message (lyskom-get-string 'reading-settings-from-done) from-session)

    ;; Inline kom-save-options
    (lyskom-save-options (or lyskom-buffer (current-buffer))
                         (lyskom-get-string 'saving-settings)
                         (lyskom-get-string 'saving-settings-done)
                         (lyskom-get-string 'could-not-save-options))
))

99

100
101
102
103
104
105
106
107
108
;;;============================================================
;;;  lyskom-save-options
;;;
;;;  Save user-area without feedback in the KOM buffer. This
;;;  is for use by all functions but edit-options.
;;;
;;;  Messages are given in the minibuffer

(defun lyskom-save-options (kombuf start-message done-message error-message)
David Byers's avatar
David Byers committed
109
110
111
112
113
114
115
116
117
118
119
120
  ;; `kom-presence-messages' isn't used directly by the emacs lisp client.
  ;; Since there is much finer control of what presence messages to give
  ;; it's hard to encode this is a single boolean, so usually don't try.
  ;; Ony if the user sees all or no presence messages is it rather clear
  ;; what the common block value should be:
  (cond ((or (eq kom-presence-messages-in-buffer t)
 	     (eq kom-presence-messages-in-echo-area t))
 	 (setq kom-presence-messages t))
 	((not (or kom-presence-messages-in-buffer
 		  kom-presence-messages-in-echo-area))
 	 (setq kom-presence-messages nil)))

David Byers's avatar
David Byers committed
121
122
123
  (let* ((print-readably t)
         (common-block 
          (concat
124
           (mapconcat (lambda (var)
125
126
127
128
129
130
131
132
133
134
135
136
                        (condition-case nil
                            (let ((common-name (elt var 0))
                                  (elisp-name (elt var 1))
                                  (type (elt var 2)))
                              (lyskom-format-objects
                               (symbol-name common-name)
                               (funcall 
                                (cdr (assq 'write
                                           (cdr (assq (or type t)
                                                      lyskom-global-variable-types))))
                                (symbol-value elisp-name))))
                          (lyskom-invalid-flag-type "")))
137
                      lyskom-global-variables
David Byers's avatar
David Byers committed
138
139
140
                      "\n")
           ))
         (elisp-block
141
142
143
144
145
146
147
148
          (concat (mapconcat
                   (lambda (var)
                     (let ((name (lyskom-encode-coding-string 
                                  (symbol-name var)
                                  (or lyskom-server-coding-system 'raw-text)))
                           (data (lyskom-encode-coding-string 
                                  (lyskom-flag-value-to-string var)
                                  (or lyskom-server-coding-system 'raw-text))))
David Byers's avatar
   
David Byers committed
149
150
                       (when (lyskom-multibyte-string-p data)
                         (lyskom-format-insert-before-prompt "Errors encoding %#1s\n" name))
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
                       (format "%dH%s %dH%s" 
                               (lyskom-string-bytes name) name
                               (lyskom-string-bytes data) data)))
                   lyskom-elisp-variables
                   "\n")
                  (mapconcat
                   (lambda (el)
                     (let ((var (lyskom-encode-coding-string
                                 (symbol-name (car el))
                                 (or lyskom-server-coding-system 'raw-text)))
                           (data (lyskom-encode-coding-string 
                                  (cdr el) 
                                  (or lyskom-server-coding-system 'raw-text))))
                       (format "\n%dH%s %dH%s" 
                               (lyskom-string-bytes var)
                               var
                               (lyskom-string-bytes data)
                               data)))
                   lyskom-saved-unknown-variables
                   "")))
         (user-area (apply 'lyskom-format-objects
172
173
174
175
176
177
178
179
180
181
		    (apply 'lyskom-format-objects
			   "common"
			   "elisp"
			   (mapcar 
			    (lambda (el)
			      (cons 'STRING (cons 'raw-text (car el))))
			    lyskom-other-clients-user-areas))
		    (cons 'STRING (cons 'raw-text common-block))
		    (cons 'STRING (cons 'raw-text elisp-block))
		    (mapcar (lambda (el)
182
183
184
185
186
187
188
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
                              (cons 'STRING (cons 'raw-text (cdr el))))
			    lyskom-other-clients-user-areas))))

    (if (or (lyskom-multibyte-string-p elisp-block)
            (lyskom-multibyte-string-p common-block)
            (memq t (mapcar 'lyskom-multibyte-string-p lyskom-other-clients-user-areas)))
        (lyskom-save-excursion
          (set-buffer kombuf)
          (lyskom-format-insert 'save-options-failed-internal
                                (lyskom-multibyte-string-p common-block)
                                (lyskom-multibyte-string-p elisp-block)
                                (memq t (mapcar 'lyskom-multibyte-string-p 
                                                lyskom-other-clients-user-areas)))
          (when error-message (lyskom-message "%s" error-message)))
      (save-excursion
        (set-buffer kombuf)
        (when start-message (lyskom-message "%s" start-message))
        (initiate-create-text
         'options 
         'lyskom-save-options-2
         (cons 'raw-text user-area)
         (lyskom-create-misc-list) 
         (list
          (lyskom-create-aux-item 
           0 1 nil nil
           (lyskom-create-aux-item-flags nil nil nil nil nil nil nil nil)
           0
           "x-kom/user-area"))
         kombuf
         done-message
         error-message)))))

;; This was in lyskom-save-options, to encode each string usgin
;; its own coding system
;;
;; (coding 
;; (lyskom-mime-charset-coding-system
;; (lyskom-mime-string-charset data)))
;; (val (condition-case nil
;; (lyskom-encode-coding-string data coding)
;; (error nil)))
;; (lyskom-ignore val)
;; (format "%dC%s%dH%s"
;;             (lyskom-string-bytes (symbol-name coding))
;;             (symbol-name coding)
;;             (lyskom-string-bytes val)
;;             val)

230
231
232

(defun lyskom-save-options-2 (text-no kombuf done-message error-message)
  (if text-no
233
234
235
236
      (progn (setq lyskom-current-user-area text-no)
             (initiate-set-user-area 'options 'lyskom-save-options-3
                                     lyskom-pers-no text-no kombuf 
                                     done-message error-message text-no))
David Kågedal's avatar
typo    
David Kågedal committed
237
    (save-excursion
238
239
     (set-buffer kombuf)
     (lyskom-insert-string 'could-not-save-options)
240
     (lyskom-message "%s" (lyskom-get-string 'could-not-save-options)))))
241

242
243
(defun lyskom-save-options-3 (success kombuf done-message
                                      error-message text-no)
244
245
246
  (save-excursion
    (set-buffer kombuf)
    (if success
247
248
249
250
        (let ((pers-stat (cache-get-pers-stat lyskom-pers-no)))
          (when pers-stat
            (set-pers-stat->user-area pers-stat text-no))
          (setq lyskom-current-user-area text-no)
251
          (when done-message (lyskom-message "%s" done-message)))
252
      (lyskom-format-insert 'could-not-set-user-area lyskom-errno)
253
      (when error-message (lyskom-message "%s" error-message)))))
254

255
(defun lyskom-read-options (&optional buffer)
David Byers's avatar
David Byers committed
256
  "Reads the user-area and sets the variables according to the choises.
257
258
259
260
261
Returns a association list of variables that were ignored. Each list
element is a cons (NAME . VALUE), where NAME is the name of the variable
and VALUE is the unparsed value (i.e. it is always a string).

If optional BUFFER is non-nil, read settings in that buffer."
Per Cederqvist's avatar
.    
Per Cederqvist committed
262
263
  (if (and lyskom-pers-no
	   (not (zerop lyskom-pers-no)))
264
265
266
267
      (let ((pers-stat 
             (save-excursion
               (when buffer (set-buffer buffer))
               (blocking-do 'get-pers-stat lyskom-pers-no))))
268
	(if (not pers-stat)             ;+++ Other error handler.
David Byers's avatar
David Byers committed
269
270
	    (progn (lyskom-insert-string 'you-dont-exist)
                   nil)
David Byers's avatar
X    
David Byers committed
271
	  (setq lyskom-other-clients-user-areas nil)
Per Cederqvist's avatar
.    
Per Cederqvist committed
272
	  (if (zerop (pers-stat->user-area pers-stat))
David Kågedal's avatar
David Kågedal committed
273
	      (progn
David Kågedal's avatar
David Kågedal committed
274
		;; (lyskom-tell-phrases-validate)
David Byers's avatar
David Byers committed
275
276
		(setq lyskom-options-done t)
                nil)
277
	    (lyskom-read-options-eval 
278
279
280
281
             (save-excursion
               (when buffer (set-buffer buffer))
               (blocking-do 'get-text
                            (pers-stat->user-area pers-stat)))))))
David Byers's avatar
David Byers committed
282
    nil))
Per Cederqvist's avatar
.    
Per Cederqvist committed
283
284

(defun lyskom-read-options-eval (text)
David Byers's avatar
David Byers committed
285
  "Handles the call from where we have the text.
286
287
Returns a alist of variables that were ignored. See lyskom-read-options
for more information."
David Byers's avatar
David Byers committed
288
289
  (let ((ignored-user-area-vars nil))
    (condition-case nil
290
291
292
293
294
295
296
297
298
        (when text                        ;+++ Other error handler
          (let* ((lyskom-options-text (text->text-mass text))
                 (pointers (lyskom-read-options-eval-get-holerith t))
                 common-no elisp-no
                 (rest lyskom-options-text)
                 working
                 (r 1))
            (let* ((lyskom-options-text pointers)
                   word
David Byers's avatar
David Byers committed
299
300
                   (r 1))
              (while (> (length lyskom-options-text) 2)
301
                (setq word (lyskom-read-options-eval-get-holerith t))
David Byers's avatar
David Byers committed
302
                (cond
303
304
305
306
                 ((lyskom-string= word "common")
                  (setq common-no r))
                 ((lyskom-string= word "elisp")
                  (setq elisp-no r))
David Byers's avatar
David Byers committed
307
                 (t
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
                  ;; Build up lyskom-other-clients-user-areas so that it
                  ;; contains a list of pairs: (name . number). (string, int).
                  (setq lyskom-other-clients-user-areas
                        (cons (cons word r) lyskom-other-clients-user-areas))))
                (++ r)))
            (setq lyskom-other-clients-user-areas 
                  (nreverse lyskom-other-clients-user-areas))
            (setq lyskom-options-text rest)
            (while (> (length lyskom-options-text) 2)
              (setq working (lyskom-read-options-eval-get-holerith t))
              (cond
               ;; Note that common-no may be nil here, so the comparison
               ;; cannot be performed with '=.
               ((equal r common-no)
                (let ((lyskom-options-text working)
                      spec name value)
                  (while (> (length lyskom-options-text) 2)
                    (setq name (intern (lyskom-read-options-eval-get-holerith)))
                    (setq value (lyskom-read-options-eval-get-holerith))
                    (setq spec
                          (cond ((lyskom-flag-global-variable-from-common name))
                                (t (let ((tmp (vector name
                                                      (intern (format "lyskom-UNK-%S" name))
                                                      nil)))
                                     (setq lyskom-global-variables
                                           (cons tmp lyskom-global-variables))
                                     tmp))))

                    (unless (lyskom-maybe-set-var-from-string (elt spec 1) value (elt spec 2))
                      (setq ignored-user-area-vars
338
                            (cons (cons (elt spec 1) value) ignored-user-area-vars))))))
339
340
341
342
343
344
345
346
347
348
349
350
351
               ;; Note that elisp-no may be nil here, so the comparison
               ;; cannot be performed with '=.
               ((equal r elisp-no)
                (let ((lyskom-options-text working)
                      name value)
                  (while (> (length lyskom-options-text) 2)
                    (setq name (intern (lyskom-read-options-eval-get-holerith)))
                    (setq value (lyskom-read-options-eval-get-holerith))
                    (if (lyskom-maybe-set-var-from-string name value)
                        (when (functionp (cdr (assq name lyskom-transition-variables)))
                          (set name (funcall (cdr (assq name lyskom-transition-variables))
                                             (symbol-value name))))
                      (setq ignored-user-area-vars
352
                            (cons (cons name value) ignored-user-area-vars))))))
353
354
355
356
357
358
359
360
361
               (t
                (let ((pos lyskom-other-clients-user-areas))
                  (while (and pos
                              (not (equal
                                    (cdr (car pos)) ;The position or the string.
                                    r)))
                    (setq pos (cdr pos)))
                  (if pos
                      (setcdr (car pos) working))))) ;Insert the string
362
363
					;where the position
					;was stored.
364
              (++ r))
365

366
367
            (mapcar 'lyskom-recompile-filter kom-permanent-filter-list)
            (mapcar 'lyskom-recompile-filter kom-session-filter-list)
David Byers's avatar
David Byers committed
368

369
370
371
372
373
374
375
376
377
378
379
            (setq lyskom-filter-list (append kom-permanent-filter-list
                                             kom-session-filter-list))
            (setq lyskom-do-when-done (cons kom-do-when-done kom-do-when-done))
            ;; Remove not found user-areas
            (let ((pos lyskom-other-clients-user-areas))
              (if pos
                  (progn
                    (while (stringp (cdr (car (cdr pos))))
                      (setq pos (cdr pos)))
                    (setcdr pos nil)))))
          (setq lyskom-current-user-area (text->text-no text)))
David Byers's avatar
David Byers committed
380
381
      (error (lyskom-message "%s" (lyskom-get-string 'error-in-options-short))))
    (setq lyskom-options-done t)
382
    (setq lyskom-current-user-area (if text (text->text-no text) 0))
David Byers's avatar
David Byers committed
383
    ignored-user-area-vars))
Per Cederqvist's avatar
.    
Per Cederqvist committed
384
385


David Byers's avatar
David Byers committed
386
387
(defun lyskom-read-options-eval-get-holerith (&optional no-coding)
  (let ((coding (if no-coding 'raw-text lyskom-server-coding-system)))
388
    (while (string-match "\\(\\s-\\|[\n\r]\\)" (substring lyskom-options-text 0 1))
David Byers's avatar
David Byers committed
389
390
391
392
393
      (setq lyskom-options-text (substring lyskom-options-text 1)))

    ;; Read the explicit coding, if any

    (when (string-match "^[0-9]+C" lyskom-options-text)
394
      (let ((len (lyskom-string-to-number lyskom-options-text)))
David Byers's avatar
David Byers committed
395
396
397
398
399
400
401
402
403
        (setq coding (intern
                      (substring lyskom-options-text 
                                 (match-end 0)
                                 (+ (match-end 0) len))))
        (setq lyskom-options-text (substring lyskom-options-text
                                             (+ (match-end 0) len))))) 

    ;; Read the string

404
    (let ((len (lyskom-string-to-number lyskom-options-text))
David Byers's avatar
David Byers committed
405
406
407
408
409
410
          (start (progn (string-match "[0-9]+H" lyskom-options-text)
                        (match-end 0))))
      (let ((name (substring lyskom-options-text start (+ start len))))
        (setq lyskom-options-text (substring lyskom-options-text
                                             (+ start len)))
        (condition-case nil
411
            (lyskom-decode-coding-string name coding)
David Byers's avatar
David Byers committed
412
          (error name))))))
413

David Byers's avatar
David Byers committed
414
415
416
417
418
419
420
421
422
423
424
425
426
(defun lyskom-get-holerith (string &optional no-coding)
  "Get the first holerith string in STRING.
If optional NO-CODING is set, assume the string has internal coding."
  (let ((coding (if no-coding 'raw-text lyskom-server-coding-system)))

    ;; Strip initial whitespace

    (when (string-match "^\\(\\s-\\|[\n\r]\\)+" string)
      (setq string (substring string (match-end 0))))

    ;; Read the explicit coding, if any

    (when (string-match "^[0-9]+C" string)
427
      (let ((len (lyskom-string-to-number string)))
David Byers's avatar
David Byers committed
428
429
430
431
432
433
434
435
        (setq coding (intern
                      (substring string 
                                 (match-end 0)
                                 (+ (match-end 0) len))))
        (setq string (substring string (+ (match-end 0) len)))))

    ;; Read the string

436
    (let ((len (lyskom-string-to-number string))
David Byers's avatar
David Byers committed
437
438
439
440
441
          (start (progn (string-match "[0-9]+H" string)
                        (match-end 0))))
      (let ((name (substring string start (+ start len))))
        (setq string (substring string (+ start len)))
        (condition-case nil
442
            (cons (lyskom-decode-coding-string name coding) string)
David Byers's avatar
David Byers committed
443
444
          (error (cons name string)))))))

445
(defun lyskom-maybe-set-var-from-string (var string &optional type)
David Byers's avatar
David Byers committed
446
  "This is a wrapper around lyskom-set-var-from-string that does nothing
David Byers's avatar
David Byers committed
447
448
449
450
if the variable is in kom-dont-read-saved-variables.

Return non-nil if the variable shouldn't have been set in the first place."
  (cond ((eq kom-dont-read-saved-variables t) t)
451
452
453
454
455
        ((memq var kom-dont-read-saved-variables) t)
        ((not (or (memq var lyskom-elisp-variables)
                  (assq var lyskom-transition-variables)
                  (lyskom-flag-global-variable-from-elisp var))) nil)
        (t (lyskom-set-var-from-string var string type) t)))
David Byers's avatar
David Byers committed
456

457
(defun lyskom-set-var-from-string (var string &optional type)
458
459
  "This is a wrapper aroud read-from-string.
It returns nil, and writes a message when an error occurs."
460
461
462
463
464
465
466
467
468
469
  (set var (condition-case nil
               (funcall 
                (cdr (assq 'read
                           (cdr (assq (or type t)
                                      lyskom-global-variable-types))))
                string)
             (error
              (lyskom-format-insert (lyskom-get-string 'error-in-options)
                                    var string)
              nil))))
David Byers's avatar
David Byers committed
470
471
472
473
474
475
476
477
478
479
480
481


(defun lyskom-flag-value-to-string (symbol)
  "Convert value of SYMBOL to a string."
  (cond ((eq symbol 'kom-permanent-filter-list)
         (prin1-to-string 
          (mapcar (lambda (filter)
                    (lyskom-create-filter (filter->pattern filter)
                                          (filter->attribute-list filter)
                                          nil))
                  kom-permanent-filter-list)))
        (t (prin1-to-string (symbol-value symbol)))))
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505


(defun lyskom-flag-global-variable-from-common (var)
  "Return the element from lyskom-global-variables corresponding to VAR."
  (lyskom-traverse el lyskom-global-variables
    (when (eq (aref el 0) var)
      (lyskom-traverse-break el))))

(defun lyskom-flag-global-variable-from-elisp (var)
  "Return the element from lyskom-global-variables corresponding to
elisp variable VAR."
  (lyskom-traverse el lyskom-global-variables
    (when (eq (aref el 1) var)
      (lyskom-traverse-break el))))

(defun lyskom-flag-write-boolean (val)
  (if val "1" "0"))

(defun lyskom-flag-read-boolean (str)
  (string= "1" str))

(defun lyskom-flag-write-symbol-list (val)
  (cond ((symbolp val)
         (lyskom-format-objects (symbol-name val)))
506
507
        (t (mapconcat (lambda (x) (lyskom-format-objects (symbol-name x)))
                      val " "))))
508
509
510
511
512
513

(defun lyskom-flag-read-symbol-list (str)
  (mapcar 'intern (lyskom-get-holerith-list str)))

(defun lyskom-flag-read-from-string (str)
  (car (read-from-string str)))
514
515
516

(defun lyskom-flag-read-integer (str)
  (cond ((equal str "nil") nil)
517
        (t (lyskom-string-to-number str))))
518
519
520
521

(defun lyskom-flag-write-integer (val)
  (cond ((integerp val) (prin1-to-string val))
        (t (signal 'lyskom-invalid-flag-type nil))))