flags.el 7.15 KB
Newer Older
Per Cederqvist's avatar
.  
Per Cederqvist committed
1
2
3
4
5
6
7
8
9
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: flags.el
;;;;
;;;; This file contains code for editing the user variables and saving
;;;; them in the user area.
;;;;

10
11
12
13
14
(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))


Per Cederqvist's avatar
.  
Per Cederqvist committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
;;; Author: Linus Tolke


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


(defun kom-edit-options ()
  "Edit options for the lyskom client."
  (interactive)
  (fset 'user-variable-p
	(symbol-function 'lyskom-user-variable-p))
  (let ((buf (current-buffer))
	(curwin (current-window-configuration)))
    (edit-options)
    (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)))


(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.
    (let ((buf lyskom-buffer)
	  (optbuf (current-buffer))
	  (common-block 
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
	   (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")
	    ))
Per Cederqvist's avatar
.  
Per Cederqvist committed
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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
171
172
173
174
175
176
177
178
179
180
	  (elisp-block
	   (mapconcat (function
		       (lambda (var)
			 (lyskom-format-objects (symbol-name var) 
						(prin1-to-string
						 (symbol-value var)))))
		      lyskom-elisp-variables
		      "\n")))
      (set-buffer buf)
      (lyskom-start-of-command (lyskom-get-string 'saving-settings) t)
      (lyskom-insert-string 'hang-on)
      (initiate-create-text 'options 'lyskom-edit-options-send
			    ;;; This is a cludge awaiting prot-B
			    (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) optbuf)))))


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


(defun lyskom-read-options ()
  "Reads the user-area and sets the variables according to the choises."
  (if (and lyskom-pers-no
	   (not (zerop lyskom-pers-no)))
      (initiate-get-pers-stat 'options 'lyskom-read-options-2 lyskom-pers-no)))


(defun lyskom-read-options-2 (pers-stat)
  "Handles the call from when we have the pers-stat."
  (if pers-stat				;+++ Other error handler.
      (progn 
	  (setq lyskom-other-clients-user-areas)
	  (if (zerop (pers-stat->user-area pers-stat))
	      (setq lyskom-do-when-starting kom-do-when-starting)
	    (initiate-get-text 'options 'lyskom-read-options-eval
			       (pers-stat->user-area pers-stat))))
    (lyskom-insert-string 'you-dont-exist)))


(defun lyskom-read-options-eval (text)
  "Handles the call from where we have the text."
  (if text				;+++ Other error handler
      (let* ((txt (text->text-mass text))
	     (pointers (lyskom-read-options-eval-get-holerith))
	     common-no elisp-no
	     (rest txt)
	     working
	     (r 1))
	(let* ((txt pointers)
	       word no
	       (r 1))
	  (while (> (length txt) 2)
	    (setq word (lyskom-read-options-eval-get-holerith))
	    (cond
	     ((string= word "common")
	      (setq common-no r))
	     ((string= word "elisp")
	      (setq elisp-no r))
	     (t
	      (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 txt rest)
	(while (> (length txt) 2)
	  (setq working (lyskom-read-options-eval-get-holerith))
	  (cond
	   ((= r common-no)
	    (let ((txt working)
181
		  name gname value)
Per Cederqvist's avatar
.  
Per Cederqvist committed
182
	      (while (> (length txt) 2)
183
		(setq gname (lyskom-read-options-eval-get-holerith))
Per Cederqvist's avatar
.  
Per Cederqvist committed
184
		(setq value (lyskom-read-options-eval-get-holerith))
185
186
187
188
189
190
191
192
193
194
195
		(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))))
Per Cederqvist's avatar
.  
Per Cederqvist committed
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
		(set (intern name) (car (read-from-string value))))))
	   ((= r elisp-no)
	    (let ((txt working)
		  name value)
	      (while (> (length txt) 2)
		(setq name (lyskom-read-options-eval-get-holerith))
		(setq value (lyskom-read-options-eval-get-holerith))
		(set (intern name) (car (read-from-string value))))))
	   (t
	    (let ((pos lyskom-other-clients-user-areas))
	      (while (and pos
			  (not (= (cdr (car pos)) r)))
		(setq pos (cdr pos)))
	      (if pos
		  (setcdr (car pos) working)))))
	  (++ r))

	(setq lyskom-do-when-starting kom-do-when-starting)
Linus Tolke's avatar
Linus Tolke committed
214
	(setq lyskom-do-when-done (cons kom-do-when-done kom-do-when-done))
Linus Tolke's avatar
Linus Tolke committed
215
	(run-hooks 'lyskom-login-hook)
Per Cederqvist's avatar
.  
Per Cederqvist committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
	;; 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)))))))


(defun lyskom-read-options-eval-get-holerith ()
  (let ((len (string-to-int txt))
	(start (progn (string-match "[0-9]+H" txt)
		      (match-end 0))))
    (prog1
	(substring txt start (+ start len))
      (setq txt (substring txt (+ start len))))))