option-edit.el 38.5 KB
Newer Older
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
;;;;;
;;;;; $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. 
;;;;;
;;;; ================================================================
;;;; ================================================================
;;;;
David Byers's avatar
David Byers committed
28
;;;; File: option-edit.el
29
;;;;
David Byers's avatar
David Byers committed
30
;;;; Customization for LysKOM
31
32
33
34
35
36
37
38
39
;;;;


(setq lyskom-clientversion-long 
      (concat lyskom-clientversion-long
	      "$Id$\n"))

;;; ======================================================================
;;; Require Per Abrahamsens widget package, version 0.991 or later.
David Byers's avatar
David Byers committed
40
41
;;;

42
43
44
;;; ======================================================================
;;; Global variables

David Byers's avatar
David Byers committed
45
46
47
(defvar lyskom-widgets nil
  "List of widgets in the customize buffer.")

48
(defvar lyskom-customize-buffer-format
David Byers's avatar
David Byers committed
49
50
  '("\n"
    (lyskom bold centered)
51
52
53
    "\n\n"
    lyskom-doc
    "\n\n"
David Byers's avatar
David Byers committed
54
55
    [kom-customize-format]
    "\n\n"
56
57
58
59
    section
    (look-and-feel-misc bold centered)
    section
    "\n"
David Byers's avatar
David Byers committed
60
61
    [kom-default-language]
    "\n"
62
    [kom-idle-hide]
David Byers's avatar
David Byers committed
63
64
65
66
    [kom-show-where-and-what]
    "\n"
    [kom-friends]
    "\n"
67
68
    [kom-presence-messages]
    [kom-presence-messages-in-buffer]
David Byers's avatar
David Byers committed
69
70
71
72
73
74
75
76
77
78
79
    "\n"
    [kom-page-before-command]
    "\n\n"
    section
    (window-locations bold centered)
    section
    "\n"
    (windows-where bold centered)
    "\n"
    windows-doc
    "\n\n"
80
    [kom-customize-in-window]
David Byers's avatar
David Byers committed
81
82
83
84
85
    [kom-edit-filters-in-window]
    [kom-prioritize-in-window]
    [kom-list-membership-in-window]
    [kom-write-texts-in-window]
    [kom-view-commented-in-window]
86
87
88
    
    "\n\n"
    section
David Byers's avatar
David Byers committed
89
    (audio-cues bold centered)
90
91
    section
    "\n"
David Byers's avatar
David Byers committed
92
93
94
95
96
97
    [kom-audio-player]
    "\n"
    (audio-cues-when bold centered)
    "\n"
    audio-cues-doc
    "\n\n"
98
99
    [kom-ding-on-priority-break]
    [kom-ding-on-new-letter]
David Byers's avatar
David Byers committed
100
101
102
103
104
105
106
107
108
109
110
    [kom-ding-on-wait-done]
    [kom-ding-on-common-messages]
    [kom-ding-on-group-messages]
    [kom-ding-on-personal-messages]
    [kom-ding-on-no-subject]

    "\n\n"
    section
    (reading bold centered)
    section
    "\n"
111
    [kom-created-texts-are-read]
David Byers's avatar
David Byers committed
112
    [kom-higher-priority-breaks]
113
114
    [kom-show-footnotes-immediately]
    [kom-follow-comments-outside-membership]
David Byers's avatar
David Byers committed
115
    "\n"
116
117
    [kom-default-mark]
    [kom-membership-default-priority]
David Byers's avatar
David Byers committed
118
119
120
121
    "\n"
    [kom-print-number-of-unread-on-entrance]
    [kom-dashed-lines]
    [kom-show-author-at-end]
David Byers's avatar
David Byers committed
122

123
124
125
126
127
128
    "\n\n"
    section
    (writing bold centered)
    section
    "\n"
    [kom-cite-string]
129
    [kom-ispell-dictionary]
David Byers's avatar
David Byers committed
130
131
132
    "\n"
    sending-doc
    "\n\n"
133
134
    [kom-confirm-multiple-recipients]
    [kom-check-commented-author-membership]
David Byers's avatar
David Byers committed
135
    [kom-check-for-new-comments]
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158

    "\n\n"
    section
    (urls bold centered)
    section
    "\n"
    [kom-url-viewer-preferences]
    [kom-mosaic-command]
    [kom-netscape-command]

    "\n\n"
    section
    (personal-messages bold centered)
    section
    "\n"
    [kom-show-personal-messages-in-buffer]
    [kom-pop-personal-messages]
    [kom-default-message-recipient]
    "\n"
    (automatic-replies bold centered)
    "\n"
    [kom-ansaphone-record-messages]
    [kom-ansaphone-show-messages]
David Byers's avatar
David Byers committed
159
    "\n"
160
161
162
163
164
165
166
167
168
    [kom-ansaphone-default-reply]

    "\n\n"
    section
    (remote-control bold centered)
    section
    "\n"
    [kom-remote-control]
    [kom-self-control]
David Byers's avatar
David Byers committed
169
170
    "\n"
    [kom-remote-controllers]
171
172
173
174
175
176
177
178
179
180
    "\n\n"
    section
    (hooks bold centered)
    section
    "\n"
    [kom-login-hook]
    [kom-do-when-done]
    ))

;;; ======================================================================
David Byers's avatar
David Byers committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
;;;     User functions
;;;

(defun lyskom-customize-apply ()
  "Set the variables in the LysKOM buffer according to their values in the
customize buffer but do not save them to the server."
  (interactive)
  (let ((tmp lyskom-widgets))
    (save-excursion
      (set-buffer lyskom-buffer)
      (while tmp
        (set (car (car tmp))
             (widget-value (cdr (car tmp))))
        (setq tmp (cdr tmp))))))

(defun lyskom-customize-send ()
  "Save variables to the server"
  (save-excursion
    (set-buffer lyskom-buffer)
    (lyskom-save-options (current-buffer)
                         (lyskom-get-string 'saving-settings)
                         (lyskom-get-string 'saving-settings-done)
David Byers's avatar
David Byers committed
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
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
                         (lyskom-get-string 'could-not-save-options))
    (let ((var-list nil))
      (mapcar 
       (function 
        (lambda (e)
          (when (and (vectorp e)
                     (symbolp (elt e 0))
                     (not (memq (elt e 0) lyskom-elisp-variables))
                     (not (memq (elt e 0) lyskom-global-boolean-variables))
                     (not (memq (elt e 0) lyskom-global-non-boolean-variables))
                     (boundp (elt e 0)))
            (setq var-list (cons (cons (elt e 0)
                                       (symbol-value (elt e 0)))
                                 var-list)))))
       lyskom-customize-buffer-format)
      
      (let* ((actual-save-options-init-file
              (or (and (boundp 'save-options-init-file)
                       save-options-init-file)
                  (and (not (equal user-init-file ""))
                       user-init-file)
                  (and (eq system-type 'ms-dos)
                       (concat "~" (user-login-name) "/_emacs"))
                  (concat "~" (user-login-name) "/.emacs")))
             (init-output-buffer (find-file-noselect
                                  actual-save-options-init-file))
             (init-output-marker nil))

        (save-excursion
          (set-buffer init-output-buffer)
          ;;
          ;; Find and delete the previously saved data, and position to write.
          ;;
          (goto-char (point-min))
          (if (re-search-forward "^;;; LysKOM Settings *\n" nil 'move)
              (let ((p (match-beginning 0)))
                (goto-char p)
                (or (re-search-forward
                     "^;;; End of LysKOM Settings *\\(\n\\|\\'\\)"
                     nil t)
                    (error "can't find END of saved state in .emacs"))
                (delete-region p (match-end 0)))
            (goto-char (point-max))
            (insert "\n"))
          (setq init-output-marker (point-marker)))

    (let ((standard-output init-output-marker))
      (princ ";;; LysKOM Settings\n")
      (princ ";;; =====================\n")
      (mapcar
       (function 
        (lambda (x)
          (princ (format "(setq %S %S)\n" (car x) (cdr x)))))
       var-list)
      (princ ";;; ============================\n")
      (princ ";;; End of LysKOM Settings\n"))

    (set-marker init-output-marker nil)
    (save-excursion
      (set-buffer init-output-buffer)
      (save-buffer))
    ))))
David Byers's avatar
David Byers committed
265
266
267
268
269
270
271
272
273
274

(defun lyskom-customize-save ()
  "Apply changes and save them to the server."
  (interactive)
  (lyskom-customize-apply)
  (lyskom-customize-send))

(defun lyskom-customize-quit ()
  "Quit the customize buffer without saving"
  (interactive)
275
276
277
  (let ((buf (current-buffer)))
    (lyskom-undisplay-buffer buf)
    (kill-buffer buf)))
David Byers's avatar
David Byers committed
278
279
280
281
282
283
284
285

(defun lyskom-customize-save-and-quit ()
  "Save and quit the customize buffer"
  (interactive)
  (lyskom-customize-apply)
  (lyskom-customize-send)
  (lyskom-customize-quit))

David Byers's avatar
David Byers committed
286
287
288
289
(defun lyskom-customize-help ()
  (interactive)
  (message (lyskom-get-string 'customize-help)))

David Byers's avatar
David Byers committed
290
291
292
293
294
295
296
(defun lyskom-widget-click (event)
  (interactive "e")
  (let ((pos (event-point event)))
    (if (and pos (widget-at pos))
        (widget-button-click event)
      (popup-mode-menu))))

David Byers's avatar
David Byers committed
297
298
299
(defun kom-customize ()
  "Open the customize buffer"
  (interactive)
David Byers's avatar
X    
David Byers committed
300
301
302
303
304
  (let ((buf (lyskom-get-buffer-create 'customize
                                       (lyskom-format
                                        (lyskom-custom-string 'buffer-name)
                                        lyskom-server-name)
                                       t)))
David Byers's avatar
David Byers committed
305
306
307
308
309
    (unwind-protect
        (progn
          (lyskom-start-of-command 'kom-customize)
           (save-excursion
             (set-buffer buf)
David Byers's avatar
X    
David Byers committed
310
             (kill-all-local-variables)
David Byers's avatar
David Byers committed
311
312
313
314
             (make-local-variable 'lyskom-widgets)
             (setq lyskom-widgets nil)
             (let ((inhibit-read-only t))
               (erase-buffer))
David Byers's avatar
David Byers committed
315
             (use-local-map lyskom-customize-map)
David Byers's avatar
David Byers committed
316
             (condition-case nil
317
                 (copy-face 'kom-active-face 'widget-button-face))
David Byers's avatar
David Byers committed
318
319
320
321
322
323
324
325
326
327
             (mapcar 'lyskom-custom-insert lyskom-customize-buffer-format)
             (widget-setup)
             (mapcar (function
                      (lambda (variable)
                        (widget-value-set (cdr variable)
                                          (save-excursion
                                            (set-buffer lyskom-buffer)
                                            (symbol-value (car variable))))))
                     lyskom-widgets)
             (widget-setup))
David Byers's avatar
X    
David Byers committed
328
           (lyskom-display-buffer buf))
David Byers's avatar
David Byers committed
329
330
331
332
333
      (save-excursion
        (set-buffer lyskom-buffer)
        (lyskom-end-of-command)))
    (goto-char (point-min))))

334

David Byers's avatar
David Byers committed
335
336
337
338
339
340
;;; ============================================================
;;; Non-user functions
;;;

(defun lyskom-custom-insert (w)
  (cond ((symbolp w)
341
         (widget-insert (substitute-command-keys (lyskom-custom-string w))))
David Byers's avatar
David Byers committed
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
        ((listp w)
         (let ((start (point))
               (end nil)
               (inhibit-read-only t))
           (widget-insert (lyskom-custom-string (car w)))
           (setq end (point))
           (mapcar (function
                    (lambda (fn)
                      (funcall (intern (concat "lyskom-custom-insert-"
                                               (symbol-name fn)))
                               start end)))
                   (cdr w))))
        ((stringp w)
         (widget-insert w))
        ((vectorp w)
         (setq lyskom-widgets (cons (cons (aref w 0)
                                          (lyskom-create-widget (aref w 0)))
                                    lyskom-widgets)))))

(defun lyskom-custom-insert-bold (s e)
David Byers's avatar
David Byers committed
362
  (add-text-properties s e (list 'face 'bold 'end-closed nil)))
David Byers's avatar
David Byers committed
363
364
365
366
367
368
369
370
371
372
373
374

(defun lyskom-custom-insert-centered (s e)
  (save-excursion (goto-char s)
                  (center-line 1)))


(defun lyskom-custom-get-value (var)
  (save-excursion
    (set-buffer lyskom-buffer)
    (symbol-value var)))

(defun lyskom-custom-string (s)
375
  (lyskom-get-string s 'lyskom-custom-strings))
David Byers's avatar
David Byers committed
376
377
378
379
380


(defvar lyskom-custom-variables
  '((kom-emacs-knows-iso-8859-1 (toggle (yes no)))
    (kom-write-texts-in-window (open-window))
David Byers's avatar
X    
David Byers committed
381
382
    (kom-list-membership-in-window (open-window))
    (kom-edit-filters-in-window (open-window))
David Byers's avatar
David Byers committed
383
384
    (kom-prioritize-in-window (open-window))
    (kom-customize-in-window (open-window))
David Byers's avatar
David Byers committed
385
386
387
    (kom-view-commented-in-window (open-window))
    (kom-edit-filters-in-window (open-window))
    (kom-list-membership-in-window (open-window))
388
389
    (kom-customize-format (choice ((const (long-format long))
                                   (const (short-format short)))))
David Byers's avatar
David Byers committed
390
    (kom-default-language (language-choice))
David Byers's avatar
X    
David Byers committed
391
392
    (kom-user-prompt-format (string))
    (kom-user-prompt-format-executing (string))
David Byers's avatar
David Byers committed
393
394
395
396
    (kom-cite-string (string))
    (kom-created-texts-are-read (toggle (yes no)))
    (kom-default-mark (choice ((number (0 255) 
                                       :tag selected-mark
397
398
                                       :format "%[%t%] (%v)"
                                       :size 0)
David Byers's avatar
David Byers committed
399
400
401
                               (const (ask nil)))))
    (kom-reading-puts-comments-in-pointers-last (toggle (before after)))
    (kom-dashed-lines (toggle (on off)))
David Byers's avatar
David Byers committed
402
    (kom-show-author-at-end (toggle (on off)))
David Byers's avatar
David Byers committed
403
    (kom-print-number-of-unread-on-entrance (toggle (yes no)))
404
405
406
407
408
409
410
411
412
    (kom-presence-messages      (choice ((const (on t))
              (repeat (person nil :tag name)
                      :tag some-persons
                      :menu-tag some-persons))))
    (kom-presence-messages-in-buffer
     (choice ((const (on t))
              (repeat (person nil :tag name)
                      :tag some-persons
                      :menu-tag some-persons))))
David Byers's avatar
David Byers committed
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
    (kom-show-where-and-what (toggle (yes no)))
    (kom-idle-hide (number))
    (kom-show-footnotes-immediately (toggle (yes no)))
    (kom-follow-comments-outside-membership (toggle (yes no)))
    (kom-read-depth-first (toggle (depth-first time-order)))
    (kom-continuous-scrolling  (toggle (on off)))
    (kom-deferred-printing (toggle (on off)))
    (kom-higher-priority-breaks (choice ((const (express-break express))
                                         (const (break t))
                                         (const (no-break nil)))))
    (kom-login-hook (repeat (command nil :tag command)))
    (kom-do-when-done (repeat (choice ((command nil :tag command)
                                       (kbd-macro nil :tag kbd-macro))
                                      :tag execute
                                      :format "%[%t%] %v")))
    (kom-page-before-command (choice ((const (page-none nil))
                                      (const (page-all t))
                                      (repeat (command nil
                                                       :tag command)
                                              :tag page-some
433
                                              :menu-tag page-some
David Byers's avatar
David Byers committed
434
                                              :format "%[%t%] %v"
435
                                              :value (kom-view-next-text)))
David Byers's avatar
David Byers committed
436
                                     :format "%[%t%] %v"))
David Byers's avatar
David Byers committed
437
438
439
440
441
    (kom-permissive-completion (noggle (on off)))
    (kom-membership-default-priority
     (choice ((const (ask-every-time nil))
              (number (0 255)
                      :tag fixed-priority 
442
443
                      :format "%[%t%] (%v)"
                      :size 0))))
David Byers's avatar
David Byers committed
444
445
446
447
448
    (kom-show-personal-messages-in-buffer
     (choice ((const (messages-in-lyskom-buffer t))
              (const (discard-messages nil))
              (string nil :tag in-named-buffer))))
    (kom-pop-personal-messages (toggle (yes no)))
449
    (kom-ding-on-new-letter (ding))
David Byers's avatar
David Byers committed
450
451
452
453
454
    (kom-ding-on-priority-break (ding))
    (kom-ding-on-wait-done (ding))
    (kom-ding-on-common-messages (ding))
    (kom-ding-on-group-messages (ding))
    (kom-ding-on-personal-messages (ding))
David Byers's avatar
David Byers committed
455
    (kom-ding-on-no-subject (ding))
David Byers's avatar
David Byers committed
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
    (kom-audio-player (file))
    (kom-default-message-recipient 
     (choice ((const (everybody-rcpt everybody))
              (const (group-rcpt group))
              (const (sender-rcpt sender)))))
    (lyskom-filter-outgoing-messages (noggle (yes no)))
    (kom-friends (repeat (person nil :tag name)))
    (kom-url-viewer-preferences (repeat (url-viewer nil :tag viewer-program)))
    (kom-mosaic-command (file))
    (kom-netscape-command (file))
    (kom-confirm-multiple-recipients
     (choice ((const (dont-check nil))
              (const (check-before-open before))
              (const (check-before-send after)
                     :match (lambda (w v) (and v (not (eq v 'before))))))))
    (kom-check-commented-author-membership (toggle (yes no)))
David Byers's avatar
David Byers committed
472
    (kom-check-for-new-comments (toggle (yes no)))
David Byers's avatar
David Byers committed
473
474
475
476
477
478
    (kom-inhibit-typeahead (noggle (yes no)))
    (kom-max-buffer-size (choice ((const (no-size-limit nil))
                                  (number nil :tag max-size-in-bytes))))

    (kom-ansaphone-record-messages (toggle (yes no)))
    (kom-ansaphone-show-messages (toggle (yes no)))
David Byers's avatar
David Byers committed
479
    (kom-ansaphone-default-reply (string nil :format "%[%t%]\n%v"))
David Byers's avatar
David Byers committed
480
481
482
    (kom-remote-control (toggle (on off)))
    (kom-remote-controllers (repeat (person nil :tag name)))
    (kom-self-control (toggle (yes no)))
483
    (kom-ispell-dictionary (ispell-dictionary))
David Byers's avatar
David Byers committed
484
485
486
487
488
489
490
491
492
493
494
495
496
))

(defvar lyskom-widget-functions 
  '((toggle . lyskom-toggle-widget)
    (noggle . lyskom-toggle-widget-inverse)
    (ding   . lyskom-ding-widget)
    (choice . lyskom-choice-widget)
    (string . lyskom-string-widget)
    (number . lyskom-number-widget)
    (const .  lyskom-item-widget)
    (repeat . lyskom-repeat-widget)
    (kbd-macro . lyskom-kbd-macro-widget)
    (url-viewer . lyskom-url-viewer-widget)
497
    (ispell-dictionary . lyskom-ispell-dictionary-widget)
David Byers's avatar
David Byers committed
498
499
500
    (open-window . lyskom-open-window-widget)
    (command . lyskom-command-widget)
    (person . lyskom-person-widget)
David Byers's avatar
David Byers committed
501
    (language-choice . lyskom-language-widget)
David Byers's avatar
David Byers committed
502
503
504
505
506
507
508
509
510
    (file . lyskom-file-widget)
))

(defun lyskom-create-widget (variable)
  (let* ((el (assq variable lyskom-custom-variables))
         (dummy (or el (error "Unknown variable: %S" variable)))
         (spec (lyskom-widget-convert-specification (car (cdr el))))
         (tag-sym (intern (concat (symbol-name variable) "-tag")))
         (doc-sym (intern (concat (symbol-name variable) "-doc")))
511
         (help-sym (intern (concat (symbol-name variable) "-help")))
David Byers's avatar
David Byers committed
512
513
         (value (save-excursion (set-buffer lyskom-buffer)
                                (symbol-value variable))))
David Byers's avatar
David Byers committed
514
    (ignore value help-sym dummy)       ; Are they ever used?
David Byers's avatar
David Byers committed
515
516
    (setq spec 
          (cons (car spec)
517
518
519
520
521
522
523
524
525
                (append
                 (list ':tag
                       (lyskom-custom-string tag-sym)
                       ':value
                       (lyskom-custom-get-value variable)
                       ;; ':doc
                       ;; (lyskom-custom-string doc-sym)
                       )
                 (cdr spec))))
David Byers's avatar
David Byers committed
526
    (let ((widget (apply 'widget-create spec)))
David Byers's avatar
David Byers committed
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
      (condition-case nil
          (progn
            (lyskom-custom-string doc-sym)
            (widget-insert "  ")
            (widget-create 'lyskom-widget-help
                           ':value (lyskom-default-value 'kom-customize-format)
                           ':args
                           (list (list 'long
                                       "\n%s\n\n"
                                       (lyskom-custom-string doc-sym)
                                       "!")
                                 (list 'short 
                                       "%s"
                                       ""
                                       "?"))
David Byers's avatar
David Byers committed
542
                           ':format "%[[%T]%]\n%D"))
David Byers's avatar
David Byers committed
543
544
        (error (widget-insert "\n")))
        widget)))
David Byers's avatar
David Byers committed
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581


(defun lyskom-widget-convert-specification (spec)
  "Convert a LysKOM widget specification to something widget-create accepts"
  (let ((convertfn (assq (car spec) lyskom-widget-functions)))
    (if (null convertfn)
        (error "Unknown widget type: %S" spec))
    (nconc (funcall (cdr convertfn)
                    (car spec)
                    (car (cdr spec))
                    (car (cdr (cdr spec))))
           (lyskom-widget-convert-props spec))))

(defun lyskom-widget-convert-props (spec)
  "Convert widget properties to a format that widget-create likes"
  (let ((propl (nthcdr 2 spec))
        (result))
    (if (/= 0 (% (length propl) 2))
        (error "Widget property list has odd length: %S" spec))
    (while propl
      (setq result (cons (car propl) result))
      (setq propl (cdr propl))
      (setq result
            (cons (cond ((symbolp (car propl))
                         (cond ((eq (car propl) t) t)
                               ((eq (car propl) nil) nil)
                               (t (lyskom-custom-string (car propl)))))
                        ((and (consp (car propl))
                              (eq 'quote (car (car propl))))
                         (car (cdr (car propl))))
                        (t (car propl)))
                  result))
      (setq propl (cdr propl)))
    (nreverse result)))


(defun lyskom-file-widget (type &optional args propl)
David Byers's avatar
David Byers committed
582
  (list 'file ':format "%[%t%] %v" ':size 0))
David Byers's avatar
David Byers committed
583
584
585
586
587
588
589
590

(defun lyskom-person-widget (type &optional args propl)
  (list 'lyskom-name))

(defun lyskom-command-widget (type &optional args propl)
  (list 'lyskom-command))

(defun lyskom-kbd-macro-widget (type &optional args propl)
591
  (list 'lyskom-kbd-macro ':macro-buffer lyskom-buffer))
David Byers's avatar
David Byers committed
592
593
594

(defun lyskom-item-widget (type &optional args propl)
  (list 'item
595
        ':format "%t"
David Byers's avatar
David Byers committed
596
597
598
        ':tag (lyskom-custom-string (elt args 0))
        ':value (elt args 1)))

David Byers's avatar
David Byers committed
599
600
601
602
603
604
605
606
607
(defun lyskom-language-widget (type &optional args propl)
  (list 'menu-choice
        ':format "%[%t%] %v"
        ':case-fold t
        ':args
        (mapcar
         (function
          (lambda (x)
            (list 'item
David Byers's avatar
David Byers committed
608
                  ':tag (lyskom-language-name (car x))
David Byers's avatar
David Byers committed
609
610
611
612
                  ':format "%t"
                  ':value (elt x 0))))
         lyskom-languages)))

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
(defun lyskom-ispell-dictionary-widget (type &optional args propl)
  (require 'ispell)
  (list 'menu-choice
        ':format "%[%t%] %v"
        ':case-fold nil
        ':args
        (cons (list 'item
                    ':tag "ispell-dictionary"
                    ':format "%t"
                    ':value nil)
              (delq nil
                    (mapcar 
                     (function
                      (lambda (x)
                        (and (car x)
                             (list 'item
                                   ':tag (car x)
                                   ':format "%t"
                                   ':value (car x)))))
                     ispell-dictionary-alist)))))

David Byers's avatar
David Byers committed
634
(defun lyskom-url-viewer-widget (type &optional args propl)
635
  (list 'menu-choice 
David Byers's avatar
David Byers committed
636
        ':format "%[%v%]\n"
637
638
        ':case-fold t
        ':args
David Byers's avatar
David Byers committed
639
        (list (list 'item
David Byers's avatar
David Byers committed
640
641
642
                    ':tag (lyskom-custom-string 'no-viewer)
                    ':format "%t"
                    ':value nil)
David Byers's avatar
David Byers committed
643
              (list 'item 
David Byers's avatar
X    
David Byers committed
644
645
646
647
                    ':tag (lyskom-custom-string 'default-viewer)
                    ':format "%t"
                    ':value "default")
              (list 'item
David Byers's avatar
David Byers committed
648
                    ':tag (lyskom-custom-string 'netscape-viewer)
649
                    ':format "%t"
David Byers's avatar
David Byers committed
650
651
652
                    ':value "netscape")
              (list 'item
                    ':tag (lyskom-custom-string 'emacs-w3-viewer)
653
                    ':format "%t"
David Byers's avatar
David Byers committed
654
655
656
                    ':value "w3")
              (list 'item 
                    ':tag (lyskom-custom-string 'emacs-general-viewer)
657
                    ':format "%t"
David Byers's avatar
David Byers committed
658
659
660
                    ':value "emacs")
              (list 'item
                    ':tag (lyskom-custom-string 'emacs-dired-viewer)
661
                    ':format "%t"
David Byers's avatar
David Byers committed
662
663
664
                    ':value "dired")
              (list 'item 
                    ':tag (lyskom-custom-string 'emacs-mail-viewer)
665
                    ':format "%t"
David Byers's avatar
David Byers committed
666
667
668
                    ':value "mail-mode")
              (list 'item
                    ':tag (lyskom-custom-string 'emacs-telnet-viewer)
669
                    ':format "%t"
David Byers's avatar
David Byers committed
670
671
672
                    ':value "telnet-mode")
              (list 'item
                    ':tag (lyskom-custom-string 'mosaic-viewer)
673
                    ':format "%t"
David Byers's avatar
David Byers committed
674
675
676
677
678
                    ':value "mosaic")
              (list 'item
                    ':tag (lyskom-custom-string 'lynx-viewer)
                    ':format "%t"
                    ':value "lynx"))))
David Byers's avatar
David Byers committed
679
680
681

(defun lyskom-open-window-widget (type &optional args propl)
  (list 'menu-choice
682
        ':case-fold t
David Byers's avatar
David Byers committed
683
        ':format "%[%t%] %v"
David Byers's avatar
David Byers committed
684
685
686
        ':args
        (list (list 'item
                    ':tag (lyskom-custom-string 'other-window)
687
                    ':format "%t"
David Byers's avatar
David Byers committed
688
                    ':value 'other)
David Byers's avatar
David Byers committed
689
690
691
692
              (list 'item 
                    ':tag (lyskom-custom-string 'other-frame)
                    ':format "%t"
                    ':value 'other-frame)
David Byers's avatar
David Byers committed
693
694
              (list 'item 
                    ':tag (lyskom-custom-string 'new-frame)
695
                    ':format "%t"
David Byers's avatar
David Byers committed
696
697
698
                    ':value 'new-frame)
              (list 'item
                    ':tag (lyskom-custom-string 'lyskom-window)
699
                    ':format "%t"
David Byers's avatar
David Byers committed
700
701
702
                    ':value nil)
              (list 'editable-field
                    ':tag (lyskom-custom-string 'window-on-buffer)
703
704
705
                    ':format "%[%t%]: `%v'"
                    ':value ""
                    ':size 0))))
David Byers's avatar
David Byers committed
706
707
708

(defun lyskom-ding-widget (type &optional args propl)
  (list 'menu-choice
709
        ':case-fold t
David Byers's avatar
David Byers committed
710
        ':format "%[%t%] %v"
David Byers's avatar
David Byers committed
711
712
713
714
        ':args
        (list (list 'item 
                    ':tag (lyskom-custom-string 'turned-off)
                    ':value nil
715
                    ':format "%t"
David Byers's avatar
David Byers committed
716
717
718
719
                    ':match '(lambda (w v) (or (null v) (eq v 0))))
              (list 'lyskom-number
                    ':tag (lyskom-custom-string 'number-of-times)
                    ':value "1"
720
721
                    ':format "%[%t%]: (%v)"
                    ':size 0
David Byers's avatar
David Byers committed
722
723
724
                    ':min-value 1
                    ':max-value 255)
              (list 'lyskom-string
725
726
                    ':tag (lyskom-custom-string 'sound-file)
                    ':size 0))))
David Byers's avatar
David Byers committed
727
728

(defun lyskom-toggle-widget-inverse (type &optional args propl)
729
730
  (list 'menu-choice
        ':case-fold t
David Byers's avatar
David Byers committed
731
        ':format "%[%t%] %v"
732
733
734
735
736
737
738
739
740
        ':args
        (list (list 'item ':tag (lyskom-custom-string (elt args 0))
                    ':value nil
                    ':format "%t")
              (list 'item 
                    ':tag (lyskom-custom-string (elt args 1))
                    ':value t
                    ':match '(lambda (w v) v)
                    ':format "%t"))))
David Byers's avatar
David Byers committed
741
742

(defun lyskom-toggle-widget (type &optional args propl)
743
744
  (list 'menu-choice
        ':case-fold t
David Byers's avatar
David Byers committed
745
        ':format "%[%t%] %v"
746
747
748
749
750
751
752
753
754
        ':args
        (list (list 'item
                    ':tag (lyskom-custom-string (elt args 0))
                    ':value t
                    ':format "%t"
                    ':match '(lambda (w v) v))
              (list 'item ':tag (lyskom-custom-string (elt args 1))
                    ':value nil
                    ':format "%t"))))
David Byers's avatar
David Byers committed
755
756
757
758


(defun lyskom-repeat-widget (type &optional args propl)
  (list 'editable-list 
David Byers's avatar
David Byers committed
759
        ':format "%[%t%]\n%v%i"
David Byers's avatar
David Byers committed
760
761
762
763
        ':args
        (list (lyskom-widget-convert-specification args))))

(defun lyskom-choice-widget (type &optional args propl)
764
765
  (list 'menu-choice
        ':case-fold t
David Byers's avatar
David Byers committed
766
        ':format "%[%t%] %v"
767
        ':args
David Byers's avatar
David Byers committed
768
769
770
771
        (mapcar 'lyskom-widget-convert-specification args)))


(defun lyskom-string-widget (type &optional args propl)
David Byers's avatar
David Byers committed
772
  (list 'lyskom-string ':size 0 ':format "%[%t%] `%v'"))
David Byers's avatar
David Byers committed
773
774
775
776
777
778


(defun lyskom-number-widget (type &optional args propl)
  (if args
      (list 'lyskom-number
            ':min-value (elt args 0)
779
780
781
782
            ':max-value (elt args 1)
            ':size 0)
    (list 'lyskom-number
          ':size 0)))
David Byers's avatar
David Byers committed
783
784


785
786

;;; ======================================================================
David Byers's avatar
David Byers committed
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
;;; Generic LysKOM widget functions
;;;

(defun lyskom-widget-value-get (widget)
  (widget-get widget ':value))

(defun lyskom-widget-value-delete (widget)
  (set-marker (widget-get widget ':value-from) nil)
  (set-marker (widget-get widget ':value-to) nil))
  

(defun lyskom-widget-value-to-external (widget value)
  value)

(defun lyskom-widget-value-to-internal (widget value)
  value)

804
805
806
807
808
809
(defun lyskom-widget-invalid-value (widget)
  (widget-put widget ':error 
              (lyskom-format (lyskom-custom-string 'invalid-value)
                             (widget-value widget)))
  widget)

David Byers's avatar
David Byers committed
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835

;;;
;;; The Person Widget
;;;

(defun lyskom-widget-name-action (widget &optional event)
  (widget-value-set widget
                    (save-excursion
                      (set-buffer lyskom-buffer)
                      (lyskom-read-conf-no
                       (lyskom-custom-string
                        (cond ((widget-get widget ':lyskom-prompt)
                               (widget-get widget ':lyskom-prompt))
                              ((and 
                                (memq 'pers (widget-get widget
                                                        ':lyskom-predicate))
                                (memq 'conf (widget-get widget
                                                        ':lyskom-predicate)))
                               'which-conf-or-person)
                              ((memq 'pers (widget-get widget
                                                       ':lyskom-predicate))
                               'which-person)
                              ((memq 'conf (widget-get widget
                                                       ':lyskom-predicate))
                               'which-conf)
                              (t 'which-name)))
836
837
838
839
                       (widget-get widget ':lyskom-predicate)
                       nil
                       nil
                       t)))
David Byers's avatar
David Byers committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
  (widget-setup))
               
(defun lyskom-widget-name-value-create (widget)
  (let* ((size (widget-get widget ':size))
         (value (widget-get widget ':value))
         (from (point))
         (string (save-excursion
                   (set-buffer lyskom-buffer)
                   (cond ((or (null value) (eq 0 value)) "")
                         (t (or (conf-stat->name
                                 (blocking-do 'get-conf-stat value))
                                (lyskom-format
                                 (lyskom-custom-string 'some-person)
                                 value)))))))
    (if (null size)
        (insert string)
      (insert string)
      (if (< (length value) size)
          (insert-char ?\  (- size (length value)))))

    (widget-put widget ':value-from (copy-marker from))
    (widget-put widget ':value-to (copy-marker (point)))
    (set-marker-insertion-type (widget-get widget ':value-to) nil)
    (if (null size)
        (insert ?\n)
      (insert ?\ ))))

(defun lyskom-widget-name-match (widget value)
  (and (numberp value)
       (>= value 0)))

(defun lyskom-widget-name-validate (widget)
  (let ((value (widget-value widget)))
873
874
875
876
    (if (and (numberp value)
             (>= value 0))
        nil
      (lyskom-widget-invalid-value widget))))
David Byers's avatar
David Byers committed
877
878
879
880
881

(define-widget 'lyskom-name 'default
  "A LysKOM person"
  ':format "%[[*]%] %v"
  ':value 0
882
  ':lyskom-predicate '(pers)
David Byers's avatar
David Byers committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
  ':action 'lyskom-widget-name-action
  ':value-create 'lyskom-widget-name-value-create
  ':value-delete 'lyskom-widget-value-delete
  ':value-get 'lyskom-widget-value-get
  ':value-to-external 'lyskom-widget-value-to-external
  ':value-to-internal 'lyskom-widget-value-to-internal
  ':match 'lyskom-widget-name-match
  ':validate 'lyskom-widget-name-validate
)

(define-widget 'lyskom-name-list 'editable-list
  "A list of LysKOM commands"
  ':entry-format "%i %d %v"
  ':args '(lyskom-name))



;;;
;;; The Command Widget
;;;

(defun lyskom-widget-command-action (widget &optional event)
  (widget-value-set widget
906
907
908
		    (save-excursion
		      (set-buffer lyskom-buffer)
		      (lyskom-read-extended-command)))
David Byers's avatar
David Byers committed
909
910
911
912
913
914
915
916
917
  (widget-setup))
               
(defun lyskom-widget-command-value-create (widget)
  (let* ((size (widget-get widget ':size))
         (value (widget-get widget ':value))
         (from (point))
         (string (save-excursion
                   (set-buffer lyskom-buffer)
                   (cond ((null value) "")
918
                         (t (or (lyskom-get-string value 'lyskom-command)
David Byers's avatar
David Byers committed
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
                                (lyskom-format
                                 (lyskom-custom-string 'unknown-command)
                                 (symbol-name value))))))))
    (if (null size)
        (insert string)
      (insert string)
      (if (< (length value) size)
          (insert-char ?\  (- size (length value)))))

    (widget-put widget ':value-from (copy-marker from))
    (widget-put widget ':value-to (copy-marker (point)))
    (set-marker-insertion-type (widget-get widget ':value-to) nil)
    (if (null size)
        (insert ?\n)
      (insert ?\ ))))

(defun lyskom-widget-command-match (widget value)
  (symbolp value))

(defun lyskom-widget-command-validate (widget)
939
940
941
942
  (if (symbolp (widget-value widget))
      nil
    (lyskom-widget-invalid-value widget)))
  
David Byers's avatar
David Byers committed
943
944
945

(define-widget 'lyskom-command 'default
  "A LysKOM command"
David Byers's avatar
David Byers committed
946
  ':format "%[%t%] %v"
David Byers's avatar
David Byers committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
  ':value 'kom-display-time
  ':action 'lyskom-widget-command-action
  ':value-create 'lyskom-widget-command-value-create
  ':value-delete 'lyskom-widget-value-delete
  ':value-get 'lyskom-widget-value-get
  ':value-to-external 'lyskom-widget-value-to-external
  ':value-to-internal 'lyskom-widget-value-to-internal
  ':match 'lyskom-widget-command-match
  ':validate 'lyskom-widget-command-validate
)

(define-widget 'lyskom-command-list 'editable-list
  "A list of LysKOM commands"
  ':entry-format "%i %d %v"
  ':args '(lyskom-command))


;;;
;;; A new String widget
;;;

(defun lyskom-widget-string-action (widget &optional event)
  (let ((tmp (read-from-minibuffer 
              (format "%s: " (widget-get widget ':tag))
David Byers's avatar
David Byers committed
971
              (widget-value widget))))
David Byers's avatar
David Byers committed
972
973
974
975
976
977
    (widget-value-set widget tmp)
    (widget-setup)))


(define-widget 'lyskom-string 'editable-field
  "A string"
David Byers's avatar
David Byers committed
978
  ':format "%[%t%] %v"
David Byers's avatar
David Byers committed
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
  ':action 'lyskom-widget-string-action)


;;;
;;; A Number widget
;;;

(defun lyskom-widget-number-action (widget &optional event)
  (let ((min (widget-get widget ':min-value))
        (max (widget-get widget ':max-value)))
    (widget-value-set
     widget
     (if (and min max)
         (lyskom-read-num-range min max
                                (concat (widget-get widget ':tag) ": ")
                                t
                                (widget-value widget))
       (lyskom-read-number (concat (widget-get widget ':tag) ": ")
                           (widget-value widget))))
    (widget-setup)))

(defun lyskom-widget-number-value-to-external (widget value)
  (string-to-int value))

(defun lyskom-widget-number-value-to-internal (widget value)
David Byers's avatar
David Byers committed
1004
  (format "% 4d " value))
David Byers's avatar
David Byers committed
1005
1006

(defun lyskom-widget-number-validate (widget)
1007
1008
1009
  (if (numberp (widget-value widget))
      nil
    (lyskom-widget-invalid-value widget)))
David Byers's avatar
David Byers committed
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021

(defun lyskom-widget-number-match (widget value)
  (if (and (widget-get widget ':max-value)
           (widget-get widget ':min-value))
      (and (numberp value)
           (>= value (widget-get widget ':min-value))
           (<= value (widget-get widget ':max-value)))
    (numberp value)))


(define-widget 'lyskom-number 'editable-field
  "A number"
David Byers's avatar
David Byers committed
1022
  ':format "%[%t%] %v"
David Byers's avatar
David Byers committed
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
  ':action 'lyskom-widget-number-action
  ':value-to-external 'lyskom-widget-number-value-to-external
  ':value-to-internal 'lyskom-widget-number-value-to-internal
  ':match 'lyskom-widget-number-match
  ':validate 'lyskom-widget-number-validate)


;;;
;;; Keyboard macro widget
;;;

(defun lyskom-widget-kbd-macro-match (widget value)
  (or (stringp value)
      (vectorp value)))

(defun lyskom-widget-kbd-macro-validate (widget)
1039
1040
1041
  (if (lyskom-widget-kbd-macro-match widget (widget-value widget))
      nil
    (lyskom-widget-invalid-value widget)))
David Byers's avatar
David Byers committed
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084

(defun lyskom-widget-kbd-macro-value-create (widget)
  (let* ((size (widget-get widget ':size))
         (value (widget-get widget ':value))
         (from (point))
         (string (mapconcat 'single-key-description (append value nil) " ")))
    (if (null size)
        (insert string)
      (insert string)
      (if (< (length string) size)
          (insert-char ?\  (- size (length string)))))

    (widget-put widget ':value-from (copy-marker from))
    (widget-put widget ':value-to (copy-marker (point)))
    (set-marker-insertion-type (widget-get widget ':value-to) nil)
    (if (null size)
        (insert ?\n)
      (insert ?\ ))))

;;;
;;; This is a truly disgusting piece of work. In Gnu Emacs it's not
;;; possible to simply read a keyboard macro in a recursive command
;;; loop and have the command loop end when macro definition time is
;;; up, so we start a macro definition, make the normal end-kbd-macro
;;; keys just exit the recursive command loop, go recursive and when
;;; the recursive loop is over, any which way, we see if a macro was
;;; defined and reset the keyboard bindings.
;;;

(defun lyskom-widget-end-kbd-macro ()
  (interactive)
  (exit-recursive-edit))

(defun lyskom-widget-kbd-macro-action (widget &optional event)
  (let ((tmp last-kbd-macro)
        (value nil))
    (save-window-excursion
      (unwind-protect
          (progn
            (mapcar (function
                     (lambda (k)
                       (define-key global-map k 'lyskom-widget-end-kbd-macro)))
                    (where-is-internal 'end-kbd-macro))
1085
1086
            (if (widget-get widget ':macro-buffer)
                (pop-to-buffer (widget-get widget ':macro-buffer)))
David Byers's avatar
David Byers committed
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
            (start-kbd-macro nil)
            (recursive-edit))
        (mapcar (function
                 (lambda (k)
                   (define-key global-map k 'end-kbd-macro)))
                (where-is-internal 'lyskom-widget-end-kbd-macro))
        (condition-case e
            (end-kbd-macro)
          (error (setq last-kbd-macro nil)))))
    (setq value
          (prog1 last-kbd-macro
            (setq last-kbd-macro tmp)))
    (if value
        (progn
          (widget-value-set widget value)
          (widget-setup)))))


(define-widget 'lyskom-kbd-macro 'default
  "A Keyboard Macro"
  ':action 'lyskom-widget-kbd-macro-action
  ':value ""
David Byers's avatar
David Byers committed
1109
  ':format "%[%t%] %v"
David Byers's avatar
David Byers committed
1110
1111
1112
1113
1114
1115
1116
  ':validate 'lyskom-widget-kbd-macro-validate
  ':value-create 'lyskom-widget-kbd-macro-value-create
  ':value-delete 'lyskom-widget-value-delete
  ':value-get 'lyskom-widget-value-get
  ':value-to-external 'lyskom-widget-value-to-external
  ':value-to-internal 'lyskom-widget-value-to-internal
  ':match 'lyskom-widget-kbd-macro-match)
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132


(defun lyskom-widget-help-action (widget &optional event)
  (let* ((value (widget-get widget ':value))
         (syms (car (cdr (memq (assq value (widget-get widget ':args))
                               (widget-get widget ':args))))))
    (if (null syms)
        (setq syms (car (widget-get widget ':args))))
    (widget-value-set widget (car syms))
    (widget-setup)))


(defun lyskom-widget-help-format-handler (widget escape)
  (let* ((value (widget-get widget ':value))
         (spec  (assq value (widget-get widget ':args))))
    (cond ((eq escape ?D)
David Byers's avatar
David Byers committed
1133
1134
           (if (widget-get widget ':indent)
               (insert-char ?  (widget-get widget ':indent)))
1135
1136
           (insert (format (elt spec 1) (elt spec 2))))
          ((eq escape ?T)
David Byers's avatar
David Byers committed
1137
1138
           (if (widget-get widget ':indent)
               (insert-char ?  (widget-get widget ':indent)))
1139
1140
1141
1142
1143
1144
1145
1146
1147
           (insert (elt spec 3)))
          (t (widget-default-format-handler widget escape)))))


(define-widget 'lyskom-widget-help 'push-button
  "A help widget"
  ':format "%[[%t]%] %D"
  ':action 'lyskom-widget-help-action
  ':format-handler 'lyskom-widget-help-format-handler)