flags.el 15.3 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, 1996  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
5
6
7
8
9
;;;;;
;;;;; 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 
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

Per Cederqvist's avatar
.    
Per Cederqvist committed
43
44
;;; Author: Linus Tolke

45
46
47
;;; Dummy defun of original-user-variable-p to eliminate compiler warning.

(defun original-user-variable-p (x) nil)
Per Cederqvist's avatar
.    
Per Cederqvist committed
48
49
50
51
52
53
54
55
56
57
58
59

(fset 'original-user-variable-p
      (symbol-function 'user-variable-p))


(defun lyskom-user-variable-p (symbol)
  (and (original-user-variable-p symbol)
       (or (string-match "^kom-"
			 (symbol-name symbol))
	   (string-match "^lyskom-"
			 (symbol-name symbol)))))

60
61
(defun lyskom-Edit-options-modify (modfun)
  (save-excursion
62
63
   (let ((inhibit-read-only t)
	 var pos tmp)
64
65
66
67
68
69
70
71
72
73
74
     (re-search-backward "^;; \\|\\`")
     (forward-char 3)
     (setq pos (point))
     (save-restriction
       (narrow-to-region pos (progn (end-of-line) (1- (point))))
       (goto-char pos)
       (setq var (read (current-buffer))))
     (goto-char pos)
     (forward-line 1)
     (forward-char 1)
     (save-excursion
75
       (set var (setq tmp (funcall modfun var)))
76
77
       (if (boundp 'lyskom-buffer)
           (set-buffer lyskom-buffer))
78
       (set var tmp))
79
80
     (kill-sexp 1)
     (prin1 (symbol-value var) (current-buffer)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
81

82
83
84
(defvar lyskom-options-text nil
  "Text mass when reading options.")

85
86
87
88
89
90
91
92
93
(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)))

94

95
(lyskom-external-function edit-options)
Per Cederqvist's avatar
.    
Per Cederqvist committed
96
97
98
99
(defun kom-edit-options ()
  "Edit options for the lyskom client."
  (interactive)
  (fset 'user-variable-p
100
	(symbol-function 'lyskom-user-variable-p))  
Per Cederqvist's avatar
.    
Per Cederqvist committed
101
102
103
  (let ((buf (current-buffer))
	(curwin (current-window-configuration)))
    (edit-options)
104
105
    (fset 'Edit-options-modify
          (symbol-function 'lyskom-Edit-options-modify))
Per Cederqvist's avatar
.    
Per Cederqvist committed
106
107
108
109
110
111
112
113
114
115
    (make-local-variable 'lyskom-buffer)
    (make-local-variable 'lyskom-edit-return-to-configuration)
    (setq lyskom-buffer buf)
    (setq lyskom-edit-return-to-configuration curwin)
    (local-set-key "\C-c\C-c" 'kom-edit-options-send)
    (local-set-key "\C-c\C-k" 'kom-edit-quit)
    )
  (fset 'user-variable-p
	(symbol-function 'original-user-variable-p)))

116
117
118
119
120
121
122
;;;============================================================
;;;  kom-edit-options-send
;;;
;;;  Finish an edit options session.
;;;  This function must be kept in sync with lyskom-save-options
;;;  below.
;;;
Per Cederqvist's avatar
.    
Per Cederqvist committed
123
124
125
126
127
128
129
130
131
132
133
134

(defun kom-edit-options-send ()
  "Finishes the edit options and sends the new settings to the server."
  (interactive)
  ; The check for changes is not a very good one.
  (cond
   ((not (eq major-mode 'Edit-options-mode))
    (error "You are not in the correct buffer. (Couldn't fool me this time."))
   ((buffer-modified-p (current-buffer))
    ;lets do it.
    ;lyskom-global-variables is a list of variables in the common block.
    ;lyskom-elisp-variables is a list of varibles in the elisp block.
David Byers's avatar
David Byers committed
135
    (let* ((optbuf (current-buffer))
David Byers's avatar
David Byers committed
136
           (print-readably t)
David Byers's avatar
David Byers committed
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
           (common-block nil)
           (elisp-block nil))
      (save-excursion
        (set-buffer lyskom-buffer)
        (setq
         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
                          (substring (symbol-name var) 4) 
                          (prin1-to-string (symbol-value var)))))
                      lyskom-global-non-boolean-variables
                      "\n")
           )
         elisp-block
          (mapconcat (function
                      (lambda (var)
                        (lyskom-format-objects (symbol-name var) 
                                               (prin1-to-string
                                                (symbol-value var)))))
                     lyskom-elisp-variables
                     "\n"))
        (lyskom-start-of-command (lyskom-get-string 'saving-settings) t)
        (lyskom-insert-string 'hang-on)
        (initiate-create-text 'options 'lyskom-edit-options-send
Per Cederqvist's avatar
.    
Per Cederqvist committed
171
			    ;;; This is a cludge awaiting prot-B
172
173
174
175
176
177
178
179
180
181
182
183
184
185
			    (apply 'lyskom-format-objects 
				   (apply 'lyskom-format-objects 
					  "common"
					  "elisp"
					  (mapcar 
					   (function car)
					   lyskom-other-clients-user-areas))
				   common-block
				   elisp-block
				   (mapcar (function cdr) 
					   lyskom-other-clients-user-areas))
;			    (concat common-block "----------\n" elisp-block)
			    (lyskom-create-misc-list) 
                            nil
186
                            optbuf))))
David Kågedal's avatar
David Kågedal committed
187
   (t
188
189
190
191
    (let ((optbuf (current-buffer)))
      (set-buffer lyskom-buffer)
      (lyskom-start-of-command (lyskom-get-string 'saving-settings) t)
      (lyskom-insert-string 'no-changes)
192
      (lyskom-edit-options-done t optbuf)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
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


(defun lyskom-edit-options-send (text-no optbuf)
  "Handles the call after the options text has been sent to the buffer."
  (if text-no
      (initiate-set-user-area 'options 'lyskom-edit-options-done
			      lyskom-pers-no text-no optbuf)
    (lyskom-insert-string 'could-not-create-area)
    (lyskom-end-of-command)))


(defun lyskom-edit-options-done (success optbuf)
  "Handles the return from the set user area call.
If successful then set the buffer not-modified. Else print a warning."
  (if success
      (save-excursion
	;;;+++ This should be done with the asynchronous call instead.
	(cache-del-pers-stat lyskom-pers-no)
	(set-buffer optbuf)
	(not-modified)
	(set-window-configuration lyskom-edit-return-to-configuration))
    (lyskom-format-insert 'could-not-set-user-area lyskom-errno))
  (lyskom-end-of-command))


David Byers's avatar
X    
David Byers committed
218
219
220
(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
221

222
223
224
225
226
227
228
229
230
;;;============================================================
;;;  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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
  (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
                          (substring (symbol-name var) 4) 
                          (prin1-to-string (symbol-value var)))))
                      lyskom-global-non-boolean-variables
                      "\n")
           ))
         (elisp-block
251
252
          (mapconcat (function
                      (lambda (var)
David Byers's avatar
David Byers committed
253
254
255
256
257
                        (lyskom-format-objects (symbol-name var) 
                                               (prin1-to-string
                                                (symbol-value var)))))
                     lyskom-elisp-variables
                     "\n")))
258
259
    (save-excursion
      (set-buffer kombuf)
260
      (lyskom-message "%s" start-message)
261
262
263
264
265
266
267
268
269
270
271
272
273
      (initiate-create-text 'options 'lyskom-save-options-2
                            (apply 'lyskom-format-objects
                                   (apply 'lyskom-format-objects
                                          "common"
                                          "elisp"
                                          (mapcar 
                                           (function car)
                                           lyskom-other-clients-user-areas))
                                   common-block
                                   elisp-block
                                   (mapcar (function cdr)
                                           lyskom-other-clients-user-areas))
                            (lyskom-create-misc-list) 
274
                            nil
275
276
277
278
279
280
281
282
283
                            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
284
    (save-excursion
285
286
     (set-buffer kombuf)
     (lyskom-insert-string 'could-not-save-options)
287
     (lyskom-message "%s" (lyskom-get-string 'could-not-save-options)))))
288
289
290
291
292
293
294

(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)
295
          (lyskom-message "%s" done-message))
296
      (lyskom-format-insert 'could-not-set-user-area lyskom-errno)
297
      (lyskom-message "%s" error-message))))
298
299


Per Cederqvist's avatar
.    
Per Cederqvist committed
300
(defun lyskom-read-options ()
David Kågedal's avatar
David Kågedal committed
301
  "Reads the user-area and sets the variables according to the choises."
Per Cederqvist's avatar
.    
Per Cederqvist committed
302
303
  (if (and lyskom-pers-no
	   (not (zerop lyskom-pers-no)))
304
305
306
      (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
307
	  (setq lyskom-other-clients-user-areas nil)
Per Cederqvist's avatar
.    
Per Cederqvist committed
308
	  (if (zerop (pers-stat->user-area pers-stat))
David Kågedal's avatar
David Kågedal committed
309
	      (progn
David Kågedal's avatar
David Kågedal committed
310
		;; (lyskom-tell-phrases-validate)
Linus Tolke's avatar
Linus Tolke committed
311
		(setq lyskom-options-done t))
312
313
314
	    (lyskom-read-options-eval 
	     (blocking-do 'get-text
			  (pers-stat->user-area pers-stat))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
315
316
317
318
319


(defun lyskom-read-options-eval (text)
  "Handles the call from where we have the text."
  (if text				;+++ Other error handler
320
      (let* ((lyskom-options-text (text->text-mass text))
Per Cederqvist's avatar
.    
Per Cederqvist committed
321
322
	     (pointers (lyskom-read-options-eval-get-holerith))
	     common-no elisp-no
323
	     (rest lyskom-options-text)
Per Cederqvist's avatar
.    
Per Cederqvist committed
324
325
	     working
	     (r 1))
326
	(let* ((lyskom-options-text pointers)
327
	       word
Per Cederqvist's avatar
.    
Per Cederqvist committed
328
	       (r 1))
329
	  (while (> (length lyskom-options-text) 2)
Per Cederqvist's avatar
.    
Per Cederqvist committed
330
331
332
333
334
335
336
	    (setq word (lyskom-read-options-eval-get-holerith))
	    (cond
	     ((string= word "common")
	      (setq common-no r))
	     ((string= word "elisp")
	      (setq elisp-no r))
	     (t
337
338
	      ;; Build up lyskom-other-clients-user-areas so that it
	      ;; contains a list of pairs: (name . number). (string, int).
Per Cederqvist's avatar
.    
Per Cederqvist committed
339
340
341
342
343
	      (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))
344
345
	(setq lyskom-options-text rest)
	(while (> (length lyskom-options-text) 2)
Per Cederqvist's avatar
.    
Per Cederqvist committed
346
347
	  (setq working (lyskom-read-options-eval-get-holerith))
	  (cond
348
349
350
	   ;; Note that common-no may be nil here, so the comparison
	   ;; cannot be performed with '=.
	   ((equal r common-no)
351
	    (let ((lyskom-options-text working)
352
		  name gname value)
353
	      (while (> (length lyskom-options-text) 2)
354
		(setq gname (lyskom-read-options-eval-get-holerith))
Per Cederqvist's avatar
.    
Per Cederqvist committed
355
		(setq value (lyskom-read-options-eval-get-holerith))
356
357
358
359
360
361
362
363
364
365
366
		(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))))
David Byers's avatar
David Byers committed
367
		(lyskom-maybe-set-var-from-string name value))))
368
369
370
	   ;; Note that elisp-no may be nil here, so the comparison
	   ;; cannot be performed with '=.
	   ((equal r elisp-no)
371
	    (let ((lyskom-options-text working)
Per Cederqvist's avatar
.    
Per Cederqvist committed
372
		  name value)
373
	      (while (> (length lyskom-options-text) 2)
Per Cederqvist's avatar
.    
Per Cederqvist committed
374
375
		(setq name (lyskom-read-options-eval-get-holerith))
		(setq value (lyskom-read-options-eval-get-holerith))
David Byers's avatar
David Byers committed
376
		(lyskom-maybe-set-var-from-string name value))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
377
378
379
	   (t
	    (let ((pos lyskom-other-clients-user-areas))
	      (while (and pos
380
381
382
			  (not (equal
				(cdr (car pos))	;The position or the string.
				r)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
383
384
		(setq pos (cdr pos)))
	      (if pos
385
386
387
		  (setcdr (car pos) working))))) ;Insert the string
						 ;where the position
						 ;was stored.
Per Cederqvist's avatar
.    
Per Cederqvist committed
388
389
	  (++ r))

390
391
	(setq lyskom-filter-list (append kom-permanent-filter-list
					 kom-session-filter-list))
Linus Tolke's avatar
Linus Tolke committed
392
	(setq lyskom-do-when-done (cons kom-do-when-done kom-do-when-done))
Per Cederqvist's avatar
.    
Per Cederqvist committed
393
394
395
396
397
398
	;; 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)))
Linus Tolke's avatar
Linus Tolke committed
399
400
		(setcdr pos nil))))))
  (setq lyskom-options-done t))
Per Cederqvist's avatar
.    
Per Cederqvist committed
401
402
403


(defun lyskom-read-options-eval-get-holerith ()
404
405
406
407
  (while (string-match "\\s-" (substring lyskom-options-text 0 1))
    (setq lyskom-options-text (substring lyskom-options-text 1)))
  (let ((len (string-to-int lyskom-options-text))
	(start (progn (string-match "[0-9]+H" lyskom-options-text)
Per Cederqvist's avatar
.    
Per Cederqvist committed
408
		      (match-end 0))))
David Byers's avatar
X    
David Byers committed
409
    (let ((name (substring lyskom-options-text start (+ start len))))
410
      (setq lyskom-options-text (substring lyskom-options-text
David Byers's avatar
X    
David Byers committed
411
412
                                           (+ start len)))
      name)))
413

David Byers's avatar
David Byers committed
414
415
416
417
418
419
420
(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)
        (t (lyskom-set-var-from-string var string))))

421
422
423
424
425
(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
426
	(condition-case nil
427
428
	    (read-from-string string)
	  (invalid-read-syntax
David Kågedal's avatar
David Kågedal committed
429
430
	   (lyskom-format-insert (lyskom-get-string 'error-in-options)
				 var string)
431
	   nil)))))