flags.el 13.2 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
44
(defvar lyskom-options-text nil
  "Text mass when reading options.")
45

David Byers's avatar
X    
David Byers committed
46
47
48
(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
49

50
51
52
53
54
55
56
57
58
59
(def-kom-command kom-save-options ()
  "Save options that have been set somewhere."
  (interactive)
  (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)))


60
61
62
63
64
65
66
67
68
;;;============================================================
;;;  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
69
70
71
72
73
74
75
76
77
78
79
80
  ;; `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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  (let* ((print-readably t)
         (common-block 
          (concat
           (mapconcat (function
                       (lambda (var)
                         (lyskom-format-objects
                          (substring (symbol-name var) 4) 
                          (if (symbol-value var) "1" "0"))))
                      lyskom-global-boolean-variables
                      "\n")
           "\n"
           (mapconcat (function
                       (lambda (var)
                         (lyskom-format-objects
95
                          (substring (symbol-name var) 4)
David Byers's avatar
David Byers committed
96
                          (lyskom-flag-value-to-string var))))
David Byers's avatar
David Byers committed
97
98
99
100
                      lyskom-global-non-boolean-variables
                      "\n")
           ))
         (elisp-block
101
102
103
104
105
106
          (mapconcat
	   (lambda (var)
	     (concat (format "%dH%s"
			     (length (symbol-name var))
			     (symbol-name var))
		     " "
David Byers's avatar
David Byers committed
107
		     (let* ((data (lyskom-flag-value-to-string var))
108
109
110
111
112
113
			    (coding 
			     (lyskom-mime-charset-coding-system
			      (lyskom-mime-string-charset data)))
			    (val (condition-case nil
				     (encode-coding-string data coding)
				   (error nil))))
114
                       (lyskom-ignore val)
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
		       (if (and val nil)
			   (format "%dC%s%dH%s"
				   (string-bytes (symbol-name coding))
				   (symbol-name coding)
				   (string-bytes val)
				   val)
			 (setq data
			       (encode-coding-string 
				data
				(or lyskom-server-coding-system 'raw-text)))
			 (format "%dH%s"
				 (string-bytes data)
				 data)))))
	   lyskom-elisp-variables
	   "\n")))
130
131
    (save-excursion
      (set-buffer kombuf)
132
      (lyskom-message "%s" start-message)
133
      (initiate-create-text
David Byers's avatar
David Byers committed
134
135
       'options 
       'lyskom-save-options-2
136
137
138
139
140
141
142
143
144
145
146
147
148
149
       (cons 'raw-text
	     (apply 'lyskom-format-objects
		    (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)
			     (cons 'STRING (cons 'raw-text (cdr el))))
			    lyskom-other-clients-user-areas)))
150
                            (lyskom-create-misc-list) 
David Byers's avatar
David Byers committed
151
152
153
154
155
156
                            (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"))
157
158
159
160
161
162
163
164
165
                            kombuf
                            done-message
                            error-message))))

(defun lyskom-save-options-2 (text-no kombuf done-message error-message)
  (if text-no
      (initiate-set-user-area 'options 'lyskom-save-options-3
                              lyskom-pers-no text-no kombuf 
                              done-message error-message)
David Kågedal's avatar
typo    
David Kågedal committed
166
    (save-excursion
167
168
     (set-buffer kombuf)
     (lyskom-insert-string 'could-not-save-options)
169
     (lyskom-message "%s" (lyskom-get-string 'could-not-save-options)))))
170
171
172
173
174
175
176

(defun lyskom-save-options-3 (success kombuf done-message error-message)
  (save-excursion
    (set-buffer kombuf)
    (if success
        (progn
          (cache-del-pers-stat lyskom-pers-no)
177
          (lyskom-message "%s" done-message))
178
      (lyskom-format-insert 'could-not-set-user-area lyskom-errno)
179
      (lyskom-message "%s" error-message))))
180
181


Per Cederqvist's avatar
.    
Per Cederqvist committed
182
(defun lyskom-read-options ()
David Kågedal's avatar
David Kågedal committed
183
  "Reads the user-area and sets the variables according to the choises."
Per Cederqvist's avatar
.    
Per Cederqvist committed
184
185
  (if (and lyskom-pers-no
	   (not (zerop lyskom-pers-no)))
186
187
188
      (let ((pers-stat (blocking-do 'get-pers-stat lyskom-pers-no)))
	(if (not pers-stat)  ;+++ Other error handler.
	    (lyskom-insert-string 'you-dont-exist)
David Byers's avatar
X    
David Byers committed
189
	  (setq lyskom-other-clients-user-areas nil)
Per Cederqvist's avatar
.    
Per Cederqvist committed
190
	  (if (zerop (pers-stat->user-area pers-stat))
David Kågedal's avatar
David Kågedal committed
191
	      (progn
David Kågedal's avatar
David Kågedal committed
192
		;; (lyskom-tell-phrases-validate)
Linus Tolke's avatar
Linus Tolke committed
193
		(setq lyskom-options-done t))
194
195
196
	    (lyskom-read-options-eval 
	     (blocking-do 'get-text
			  (pers-stat->user-area pers-stat))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
197

David Byers's avatar
David Byers committed
198
(defvar lyskom-ignored-user-area-vars)
Per Cederqvist's avatar
.    
Per Cederqvist committed
199
200
(defun lyskom-read-options-eval (text)
  "Handles the call from where we have the text."
201
202
203
204
  (condition-case nil
      (if text				;+++ Other error handler
	  (let* ((lyskom-options-text (text->text-mass text))
		 (pointers (lyskom-read-options-eval-get-holerith t))
David Byers's avatar
David Byers committed
205
                 (lyskom-ignored-user-area-vars nil)
206
207
208
209
210
211
212
		 common-no elisp-no
		 (rest lyskom-options-text)
		 working
		 (r 1))
	    (let* ((lyskom-options-text pointers)
		   word
		   (r 1))
213
	      (while (> (length lyskom-options-text) 2)
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
		(setq word (lyskom-read-options-eval-get-holerith t))
		(cond
		 ((lyskom-string= word "common")
		  (setq common-no r))
		 ((lyskom-string= word "elisp")
		  (setq elisp-no r))
		 (t
		  ;; 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)
		      name gname value)
		  (while (> (length lyskom-options-text) 2)
		    (setq gname (lyskom-read-options-eval-get-holerith))
		    (setq value (lyskom-read-options-eval-get-holerith))
		    (setq name (concat "kom-" gname))
		    (if (memq (intern-soft name) lyskom-global-boolean-variables)
			(if (string= value "1")
			    (setq value "t")
			  (setq value "nil"))
		      (if (memq (intern-soft name) 
				lyskom-global-non-boolean-variables)
			  nil
			(setq name (concat "UNK-" gname))
			(setq lyskom-global-non-boolean-variables
			      (cons name lyskom-global-non-boolean-variables))))
		    (lyskom-maybe-set-var-from-string name value))))
	       ;; 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 (lyskom-read-options-eval-get-holerith))
		    (setq value (lyskom-read-options-eval-get-holerith))
		    (lyskom-maybe-set-var-from-string name value))))
	       (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
					;where the position
					;was stored.
	      (++ r))

David Byers's avatar
David Byers committed
274
275
276
277
278
279
280
            (when lyskom-ignored-user-area-vars
              (lyskom-format-insert-before-prompt
               'ignored-user-area-var
               (mapconcat 'symbol-name 
                          lyskom-ignored-user-area-vars
                          "\n    ")))

281
282
283
284
285
286
287
	    (mapcar 'lyskom-recompile-filter kom-permanent-filter-list)
	    (mapcar 'lyskom-recompile-filter kom-session-filter-list)

	    (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
Per Cederqvist's avatar
.    
Per Cederqvist committed
288
289
	    (let ((pos lyskom-other-clients-user-areas))
	      (if pos
290
291
292
293
		  (progn
		    (while (stringp (cdr (car (cdr pos))))
		      (setq pos (cdr pos)))
		    (setcdr pos nil))))))
294
    (error (lyskom-message "%s" (lyskom-get-string 'error-in-options-short))))
Linus Tolke's avatar
Linus Tolke committed
295
  (setq lyskom-options-done t))
Per Cederqvist's avatar
.    
Per Cederqvist committed
296
297


David Byers's avatar
David Byers committed
298
299
(defun lyskom-read-options-eval-get-holerith (&optional no-coding)
  (let ((coding (if no-coding 'raw-text lyskom-server-coding-system)))
300
    (while (string-match "\\(\\s-\\|[\n\r]\\)" (substring lyskom-options-text 0 1))
David Byers's avatar
David Byers committed
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
      (setq lyskom-options-text (substring lyskom-options-text 1)))

    ;; Read the explicit coding, if any

    (when (string-match "^[0-9]+C" lyskom-options-text)
      (let ((len (string-to-int lyskom-options-text)))
        (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

    (let ((len (string-to-int lyskom-options-text))
          (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
            (decode-coding-string name coding)
          (error name))))))
325

David Byers's avatar
David Byers committed
326
327
328
329
330
(defun lyskom-maybe-set-var-from-string (var string)
  "This is a wrapper around lyskom-set-var-from-string that does nothing
if the variable is in kom-dont-read-saved-variables."
  (cond ((eq kom-dont-read-saved-variables t) nil)
        ((memq (intern var) kom-dont-read-saved-variables) nil)
331
        ((not (or (memq (intern var) lyskom-elisp-variables)
David Byers's avatar
David Byers committed
332
                  (memq (intern var) lyskom-global-non-boolean-variables)
333
                  (memq (intern var) lyskom-global-boolean-variables)))
David Byers's avatar
David Byers committed
334
335
336
         (setq lyskom-ignored-user-area-vars 
               (cons (intern var)
                     lyskom-ignored-user-area-vars)))
David Byers's avatar
David Byers committed
337
338
        (t (lyskom-set-var-from-string var string))))

339
340
341
342
343
(defun lyskom-set-var-from-string (var string)
  "This is a wrapper aroud read-from-string.
It returns nil, and writes a message when an error occurs."
  (set (intern var)
       (car
344
	(condition-case nil
345
346
	    (read-from-string string)
	  (invalid-read-syntax
David Kågedal's avatar
David Kågedal committed
347
348
	   (lyskom-format-insert (lyskom-get-string 'error-in-options)
				 var string)
349
	   nil)))))
David Byers's avatar
David Byers committed
350
351
352
353
354
355
356
357
358
359
360
361


(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)))))