edit-text.el 36.7 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
2
;;;;;
;;;;; $Id$
3
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4
5
6
7
8
;;;;;
;;;;; 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 
9
;;;;; the Free Software Foundation; either version 2, or (at your option) 
Linus Tolke Y's avatar
Linus Tolke Y committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
;;;;; 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
25
26
27
28
29
30
31
32
33
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: edit-text.el
;;;;
;;;; This file contains functions which lets the LysKOM user edit
;;;; a text in a window. It also defines a new mode - lyskom-edit-mode.
;;;;

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


Per Cederqvist's avatar
.    
Per Cederqvist committed
39
40
41
42
43
44
;;;; ================================================================


(defvar lyskom-edit-mode-name "LysKOM edit"
  "Name of the mode.")

Linus Tolke Y's avatar
Linus Tolke Y committed
45
46
47
48
49
50
51
52
53
54
55
56
57
58
(defvar lyskom-is-dedicated-edit-window nil
  "Status variable for an edit-window.")

(defvar lyskom-edit-handler nil
  "Status variable for an edit-buffer.
See lyskom-edit-handler-data.")

(defvar lyskom-edit-handler-data nil
  "Status variable for an edit-buffer.
See lyskom-edit-handler.")

(defvar lyskom-edit-return-to-configuration nil
  "Status variable for an edit-buffer.")

59
;;; Error signaled by lyskom-edit-parse-headers
60
61
62
(put 'lyskom-edit-text-abort 'error-conditions
     '(error lyskom-error lyskom-edit-error lyskom-abort-edit))

63
(put 'lyskom-unknown-header 'error-conditions
64
     '(error lyskom-error lyskom-edit-error lyskom-unknown-header))
65
66

(put 'lyskom-no-subject 'error-conditions
67
     '(error lyskom-error lyskom-edit-error lyskom-no-subject))
David Byers's avatar
David Byers committed
68

Per Cederqvist's avatar
.    
Per Cederqvist committed
69
70
71
72
73
74
75
76
77
78
79
80
81
(defun lyskom-edit-text (proc misc-list subject body
			      &optional handler &rest data)
  "Edit a text in a new buffer.
PROC is the associated process.
MISC-LIST is the default misc-list.
SUBJECT is subject (a string).
BODY is the default text-body (a string, normally empty.)
HANDLER is a function to call when the text has been created.
DATA is extra data to send to the function. HANDLER is called with
	(apply HANDLER text-no DATA)
where text-no is the number of the text.
Does lyskom-end-of-command."
  (setq lyskom-is-writing t)
82
  (lyskom-dispatch-edit-text proc misc-list subject body
83
                             handler data))
84
85
86
87


(defun lyskom-dispatch-edit-text (proc misc-list subject body
				       &optional handler &rest data)
88
  "Same as lyskom-edit-text except that it doesn't set lyskom-is-writing."
David Byers's avatar
X    
David Byers committed
89
90
91
92
  (let ((buffer (lyskom-get-buffer-create 'write-texts
                                          (concat 
                                           (buffer-name (process-buffer proc))
                                           "-edit")))
Per Cederqvist's avatar
.    
Per Cederqvist committed
93
	(config (current-window-configuration)))
David Byers's avatar
X    
David Byers committed
94
95
96

    (lyskom-display-buffer buffer)
    (lyskom-edit-mode)
Per Cederqvist's avatar
.    
Per Cederqvist committed
97
98
99
100
101
102
103
    (make-local-variable 'lyskom-edit-handler)
    (make-local-variable 'lyskom-edit-handler-data)
    (make-local-variable 'lyskom-edit-return-to-configuration)
    (setq lyskom-edit-handler handler)
    (setq lyskom-edit-handler-data data)
    (setq lyskom-edit-return-to-configuration config)
    (lyskom-edit-insert-miscs misc-list subject body)
104
    (goto-char (point-min))
105
106
    (re-search-forward (regexp-quote (lyskom-get-string
				      'header-subject))
107
108
		       (point-max)
		       'end)
109
110
    (if (not (looking-at "\\s-*$"))
        (goto-char (point-max)))
111
    (lyskom-message "%s" (lyskom-get-string 'press-C-c-C-c)))
David Byers's avatar
David Byers committed
112
  (set-buffer lyskom-buffer)
113
  )
Per Cederqvist's avatar
.    
Per Cederqvist committed
114
115


David Byers's avatar
David Byers committed
116

Per Cederqvist's avatar
.    
Per Cederqvist committed
117
118
119
120
121
122
123
124
(defun lyskom-edit-insert-miscs (misc-list subject body)
  "Insert MISC-LIST into header of text.
recpt		-> Mottagare: <%d> %s.
cc-recpt	-> Extra kopia: <%d> %s.
comm-to		-> Kommentar till text %d.
footn-to	-> Fotnot till text %d."
  (let ((edit-buffer (current-buffer))
	(where-put-misc (point-min-marker))
David Byers's avatar
David Byers committed
125
	(main-buffer lyskom-buffer))
Per Cederqvist's avatar
.    
Per Cederqvist committed
126
127
128
129
130
131
132
    (set-buffer main-buffer)
    (setq misc-list (cdr misc-list))
    (while (not (null misc-list))
      (let ((key (car (car misc-list)))
	    (data (cdr (car misc-list))))
	(cond
	 ((eq key 'recpt)
133
134
135
	  (lyskom-edit-insert-misc-conf (blocking-do 'get-conf-stat data)
					(lyskom-get-string 'recipient)
					where-put-misc data))
Per Cederqvist's avatar
.    
Per Cederqvist committed
136
	 ((eq key 'cc-recpt)
137
138
139
	  (lyskom-edit-insert-misc-conf (blocking-do 'get-conf-stat data)
					(lyskom-get-string 'carbon-copy)
					where-put-misc data))
Per Cederqvist's avatar
.    
Per Cederqvist committed
140
	 ((eq key 'comm-to)
141
142
143
	  (lyskom-edit-get-commented-author (blocking-do 'get-text-stat data)
					    (lyskom-get-string 'comment)
					    where-put-misc data))
Per Cederqvist's avatar
.    
Per Cederqvist committed
144
	 ((eq key 'footn-to)
145
146
147
148
149
	  (lyskom-edit-get-commented-author (blocking-do 'get-text-stat data)
					    (lyskom-get-string 'footnote)
					    where-put-misc data)))
	(setq misc-list (cdr misc-list))))
    (princ (lyskom-format 'text-mass subject 
150
151
			  (substitute-command-keys
			   (lyskom-get-string 'header-separator))
152
			  body 
153
			  (lyskom-get-string 'header-subject))
154
155
156
157
	   where-put-misc)
    (set-buffer edit-buffer)
    (goto-char where-put-misc)
    ))
Per Cederqvist's avatar
.    
Per Cederqvist committed
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173


(defun lyskom-edit-goto-char (marker)
  "Positions the editing at MARKER."
  (let ((curbuf (current-buffer)))
    (set-buffer (marker-buffer marker))
    (save-window-excursion
      (goto-char marker))
    (set-buffer curbuf)))


(defun lyskom-edit-insert-misc-conf (conf-stat string stream number)
  "Insert Mottagare: or Extra kopia: in edit-buffer.
Args: CONF-STAT STRING STREAM NUMBER
CONF-STAT is the conf-stat of the conference that is about to be put in,
STRING is the string that is inserted.
174
STREAM is the buffer or a marker telling the position.
Per Cederqvist's avatar
.    
Per Cederqvist committed
175
NUMBER is the number of the person. Used if the conf-stat is nil."
David Byers's avatar
X    
David Byers committed
176
177
178
179
180
  (lyskom-princ  (lyskom-format "%#1s <%#2m> %#3M\n" 
                                string
                                (or conf-stat number)
                                (or conf-stat ""))
                 stream))
Per Cederqvist's avatar
.    
Per Cederqvist committed
181
182
183
184


(defun lyskom-edit-get-commented-author (text-stat string stream number)
  (if text-stat
185
186
187
      (lyskom-edit-insert-commented-author 
       (blocking-do 'get-conf-stat (text-stat->author text-stat))
       string stream number)
Per Cederqvist's avatar
.    
Per Cederqvist committed
188
189
190
191
    (lyskom-edit-insert-commented-author nil string stream number)))


(defun lyskom-edit-insert-commented-author (conf-stat string stream number)
David Byers's avatar
X    
David Byers committed
192
193
194
195
196
197
198
199
  (lyskom-princ (lyskom-format 'comment-to-by
                               string
                               number
                               (if conf-stat
                                   (lyskom-format 'by conf-stat)
                                 ""))
                stream))

David Kågedal's avatar
David Kågedal committed
200

Per Cederqvist's avatar
.    
Per Cederqvist committed
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220


(defun lyskom-create-misc-list (&rest misc-pairs)
  "Create a misc-list.
Note that a misc-list is very different from a misc-info-list.
A misc-list is used when creating a new text, and sent to the server.
A misc-info-list is what is received from the server. Although the server
has the same format for the two things, this client uses two quite
different formats.

The arguments to this function is any number of pairs of data. The first
item in each pair should be one of recpt, cc-recpt, comm-to or footn-to.
The second item should be the corresponding conf- or text-no.

The result is a list of dotted pairs:
	('recpt . conf-no)
	('cc-recpt . conf-no)
	('comm-to . text-no)
	('footn-to . text-no).
First element is a type-tag."
David Byers's avatar
David Byers committed
221
   (let ((result (cons 'MISC-LIST nil)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
222
223
224
225
226
227
228
229
230
231
232
    (while (not (null misc-pairs))
      (nconc result (cons (cons (car misc-pairs)
				(car (cdr misc-pairs)))
			  nil))
      (setq misc-pairs (cdr (cdr misc-pairs))))
    result))


;;; ================================================================
;;;                   lyskom-edit-mode

Linus Tolke Y's avatar
Linus Tolke Y committed
233
234
235
236
237
238
(defvar lyskom-edit-mode-hook nil
  "*List of functions to be called when entering lyskom-edit-mode.
Watch out! None of these functions are allowed to do kill-all-local-variables
because kom-edit-send and other functions depend on some variables to be able
to enter the text in the correct lyskom-process.")

Per Cederqvist's avatar
.    
Per Cederqvist committed
239
240
241
(defun lyskom-edit-mode ()
  "\\<lyskom-edit-mode-map>Mode for editing texts for LysKOM.
Commands:
Linus Tolke Y's avatar
Linus Tolke Y committed
242
243
244
245
246
247
248
249
250
251
\\[kom-edit-send]   sends the text when you are ready. The buffer will be
	  deleted if (and only if) the server accepts the text.
\\[kom-edit-quit]   aborts the editing. You will get back to the LysKOM buffer.

\\[kom-edit-show-commented]   shows the commented text in a temporary buffer.

\\[kom-edit-add-recipient]   asks for another recipient and adds him to the header.
\\[kom-edit-add-copy]   as \\[kom-edit-add-recipient] but adds him as copy-recipient.

\\[kom-edit-insert-commented]   inserts the commented of footnoted text.
252
\\[kom-edit-insert-text]   inserts the shown text, you tell the number."
Per Cederqvist's avatar
.    
Per Cederqvist committed
253
  (interactive)
David Byers's avatar
X    
David Byers committed
254
  (kill-all-local-variables)
255
  (text-mode)
David Byers's avatar
X    
David Byers committed
256
  (lyskom-set-menus 'lyskom-edit-mode lyskom-edit-mode-map)
257
  (setq mode-line-buffer-identification '("LysKOM (server: %b)"))
Per Cederqvist's avatar
.    
Per Cederqvist committed
258
259
  (setq major-mode 'lyskom-edit-mode)
  (setq mode-name lyskom-edit-mode-name)
David Byers's avatar
X    
David Byers committed
260
  (lyskom-use-local-map lyskom-edit-mode-map)
Per Cederqvist's avatar
.    
Per Cederqvist committed
261
262
263
264
  (auto-save-mode 1)
  (auto-fill-mode 1)
  (make-local-variable 'paragraph-start)
  (make-local-variable 'paragraph-separate)
265
266
  (setq paragraph-start (concat "^" 
				(regexp-quote 
267
268
				 (substitute-command-keys
				  (lyskom-get-string 'header-separator)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
269
				"$\\|" paragraph-start))
270
271
  (setq paragraph-separate (concat "^" 
				   (regexp-quote 
272
273
				    (substitute-command-keys
				     (lyskom-get-string 'header-separator)))
274
				   "$\\|" paragraph-separate))
Per Cederqvist's avatar
.    
Per Cederqvist committed
275
276
277
278
279
280
281
  (run-hooks 'lyskom-edit-mode-hook))


;;; ================================================================
;;;   Functions bound to keyboard seqences in lyskom-edit-mode
;;;

282
283
284
285
(defun kom-edit-send-anonymous ()
  "Send the text anonymously to the server."
  (interactive)
  (lyskom-edit-send 'initiate-create-anonymous-text))
Per Cederqvist's avatar
.    
Per Cederqvist committed
286
287
288
289

(defun kom-edit-send ()
  "Send the text to the server."
  (interactive)
290
291
292
293
  (lyskom-edit-send 'initiate-create-text))

(defun lyskom-edit-send (send-function)
  "Send the text to the server by calling SEND-FUNCTION."
294
  (condition-case err
David Byers's avatar
David Byers committed
295
296
297
298
299
300
301
302
303
304
      (if (or (string= mode-name lyskom-edit-mode-name)
	      (j-or-n-p (lyskom-get-string 'already-sent)))
	  (progn 
	    (let ((buffer (current-buffer))
		  headers misc-list subject message)

	      (save-excursion
		(setq headers (lyskom-edit-parse-headers)
		      misc-list (apply 'lyskom-create-misc-list (cdr headers))
		      subject (car headers)))
305
              ;;
David Byers's avatar
David Byers committed
306
307
308
309
310
              ;; Run user hooks
              ;; ####: ++++: FIXME: We should quit more graciously.

              (if (not (run-hook-with-args-until-failure 
                        'lyskom-send-text-hook))
311
                  (signal 'lyskom-edit-text-abort nil))
David Byers's avatar
David Byers committed
312
313
                                                
              ;;
314
315
316
              ;; Check that there is a subject
              ;;

David Byers's avatar
David Byers committed
317
318
319
	      (if (string= subject "")
		  (let ((old (point)))
		    (goto-char (point-min))
320
		    (re-search-forward (lyskom-get-string 'header-subject)
David Byers's avatar
David Byers committed
321
322
323
				       nil t)
		    (end-of-line)
		    (if (/= (point) old)
324
			(signal 'lyskom-no-subject '(enter-subject-idi)))))
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

              ;;
              ;; Check the recipients
              ;;

              (let ((extra-headers
                     (lyskom-edit-send-check-recipients misc-list
                                                        subject)))
                (if extra-headers
                    (setq misc-list (apply 'lyskom-create-misc-list
                                           (cdr (nconc headers
                                                       extra-headers))))))

              ;;
              ;; Transform the message text
              ;;

342
	      (setq message
David Byers's avatar
David Byers committed
343
344
345
346
                    (if (fboundp lyskom-send-text-transform-function)
                        (funcall lyskom-send-text-transform-function
                                 (lyskom-edit-extract-text))
                      (lyskom-edit-extract-text)))
347

David Byers's avatar
David Byers committed
348
349
	      (setq mode-name "LysKOM sending")
	      (save-excursion
David Byers's avatar
David Byers committed
350
		(set-buffer lyskom-buffer)
David Byers's avatar
David Byers committed
351
352
353
354
355
		;; Don't change the prompt if we won't see our own text
		(if kom-created-texts-are-read
		    (setq lyskom-dont-change-prompt t))
		(setq lyskom-is-writing nil)
		(lyskom-tell-internat 'kom-tell-send)
David Byers's avatar
David Byers committed
356
		(funcall send-function
357
358
359
360
361
                         'sending
                         'lyskom-create-text-handler
                         (concat subject "\n" message)
                         misc-list
                         buffer)))
David Byers's avatar
David Byers committed
362
            (lyskom-undisplay-buffer)
David Byers's avatar
David Byers committed
363
	    (goto-char (point-max))))
David Byers's avatar
David Byers committed
364
    ;;
365
    ;; Catch no-subject and other things
David Byers's avatar
David Byers committed
366
367
    ;;

368
369
370
    (lyskom-abort-edit
     (apply 'lyskom-message (cdr-safe err)))
    (lyskom-no-subject
David Byers's avatar
David Byers committed
371
     (lyskom-beep kom-ding-on-no-subject)
David Byers's avatar
David Byers committed
372
373
     (if (cdr-safe (cdr-safe err))
	 (goto-char (car-safe (cdr-safe (cdr-safe err)))))
374
     (lyskom-message "%s" (lyskom-get-string (car (cdr err))))
375
     (condition-case nil
David Byers's avatar
David Byers committed
376
377
         (let ((text ""))
           (save-excursion
David Byers's avatar
David Byers committed
378
             (set-buffer lyskom-buffer)
David Byers's avatar
David Byers committed
379
380
381
382
             (if (and (string= "kom.lysator.liu.se" lyskom-server-name)
                      (eq lyskom-pers-no 698))
                 (setq text "Ärende, IDI!")))
           (save-excursion (insert text)))
383
384
       (error nil)))
    (lyskom-unknown-header
385
     (lyskom-message "%s" (lyskom-get-string (car (cdr err)))))))
David Byers's avatar
David Byers committed
386

David Byers's avatar
David Byers committed
387
(eval-when-compile (defvar ispell-dictionary nil))
David Byers's avatar
David Byers committed
388
389
390
(eval-when-compile (defvar ispell-message-text-end nil))
(eval-when-compile (defvar ispell-message-start-skip nil))
(eval-when-compile (defvar ispell-message-end-skip nil))
David Byers's avatar
David Byers committed
391
392


393
394
395
396
397
398
399
400
401
402
403
404
(defun lyskom-ispell-text ()
  "Check spelling of the text body.
Put this in lyskom-send-text-hook"
  (kom-ispell-message)
  t)


(defun kom-ispell-message ()
  "Check spelling of the text.
kom-ispell-dictionary is the dictionary to use to check spelling.
Based on ispell-message."
  (interactive)
David Byers's avatar
David Byers committed
405
  (require 'ispell)
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
  (let ((ispell-dictionary (or kom-ispell-dictionary ispell-dictionary))
        (kill-ispell (or (not (boundp 'ispell-dictionary))
                         (not (string= kom-ispell-dictionary
                                       ispell-dictionary))))
        (result nil))
    (when kill-ispell (ispell-kill-ispell t))

    ;; Checking code

    (save-excursion
      (goto-char (point-min))
      (let* ((internal-messagep 
              (save-excursion
                (re-search-forward
                 (concat "^"
                         (regexp-quote
                          (substitute-command-keys
                           (lyskom-get-string 'header-separator)))
                         "$")
                 nil t)))
             (limit 
              (copy-marker
               (cond ((not ispell-message-text-end) (point-max))
                     ((char-or-string-p ispell-message-text-end)
                      (if (re-search-forward ispell-message-text-end nil t)
                          (match-beginning 0)
                        (point-max)))
                     (t (min (point-max) (funcall ispell-message-text-end))))))
             (cite-regexp (regexp-quote kom-cite-string))
             (cite-regexp-start (concat "^[ \t]*$\\|" cite-regexp))
             (cite-regexp-end (concat "^\\(" cite-regexp "\\)"))
             (old-case-fold-search case-fold-search)
             (case-fold-search t)
             (ispell-checking-message t)
             (subject-string 
              (concat "^" (regexp-quote (lyskom-get-string 'subject)))))
        (goto-char (point-min))
        (while (if internal-messagep
                   (< (point) internal-messagep)
                 (not (eobp)))
          (if (looking-at subject-string)
              (progn (goto-char (match-end 0))
                     (let ((case-fold-search old-case-fold-search))
                       (ispell-region (point)
                                      (progn
                                        (end-of-line)
                                        (point)))))
            (forward-line 1)))

        (while (< (point) limit)
          (while (and (looking-at cite-regexp-start)
                      (< (point) limit)
                      (zerop (forward-line 1))))

          (if (< (point) limit)
              (let* ((start (point))
                     (end-c (and (re-search-forward cite-regexp-end limit 'end)
                                 (match-beginning 0)))
                     (end-fwd (and (goto-char start)
David Byers's avatar
David Byers committed
465
                                   (boundp 'ispell-message-start-skip)
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
                                   (re-search-forward ispell-message-start-skip
                                                      limit 'end)))
                     (end (or (and end-c end-fwd (min end-c end-fwd))
                              end-c 
                              end-fwd
                              (marker-position limit))))
                (goto-char start)
                (setq result (ispell-region start end))
                  (if (and end-fwd (= end end-fwd))
                      (progn (goto-char end)
                             (re-search-forward ispell-message-end-skip 
                                                limit 'end))
                    (goto-char end)))))
          (set-marker limit nil)
          result))

    (when kill-ispell (ispell-kill-ispell t))
    result))

David Byers's avatar
David Byers committed
485

486
487
488
489
490
491
492
(defun lyskom-edit-send-check-recipients (misc-list subject) 
  "Check that the recipients of this text are OK. Ask the user to
confirm multiple recipients; check that the author of the commented
text is a member of some recipient of this text."
  (let* ((comm-to-list nil)
         (recipient-list nil)
         (author-list nil)
493
         (author-is-member nil)
David Byers's avatar
David Byers committed
494
         (text-stat nil)
495
         (collector (make-collector))
496
         (extra-headers nil)
David Byers's avatar
David Byers committed
497
         (buffer (current-buffer))
David Byers's avatar
David Byers committed
498
         (me (save-excursion (set-buffer lyskom-buffer)
499
500
                             lyskom-pers-no))
         (num-me 0))
David Byers's avatar
David Byers committed
501
    (ignore text-stat)                  ; Have no idea if its ever used...
502
503
504

    ;;
    ;; List all texts this text is a comment to
David Byers's avatar
David Byers committed
505
    ;; List all recipients of the text
506
507
508
509
510
511
512
513
514
515
516
    ;;

    (lyskom-traverse misc (cdr misc-list)
      (cond ((eq (car misc) 'comm-to)
             (setq comm-to-list (cons (cdr misc)
                                      comm-to-list)))
            ((or (eq (car misc) 'recpt)
                 (eq (car misc) 'cc-recpt))
             (if (eq (cdr misc) me) (setq num-me (1+ num-me)))
             (setq recipient-list (cons (cdr misc) recipient-list)))))

David Byers's avatar
David Byers committed
517
518
519
520
521
522
523
524
525
526
527
528
529
530
    ;;
    ;; Check for new comments
    ;;

    (when (save-excursion (set-buffer lyskom-buffer)
                           (cond ((null kom-check-for-new-comments) nil)
                                 ((functionp kom-check-for-new-comments)
                                  (funcall kom-check-for-new-comments
                                           buffer misc-list subject))
                                 (t t)))
      (lyskom-message (lyskom-format 'checking-comments))
      (save-excursion
        (set-buffer lyskom-buffer)
        (set-collector->value collector nil)
531
	
David Byers's avatar
David Byers committed
532
533
534
535
536
537
538
539
540
        (mapcar (function (lambda (text-stat)
                            (cache-del-text-stat text-stat)
                            (initiate-get-text-stat 'sending 
                                                    'collector-push
                                                    text-stat
                                                    collector)))
                comm-to-list)
        (lyskom-wait-queue 'sending)
                                             
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
	(lyskom-traverse
	 text-stat (collector->value collector)
	 (when text-stat
	   (when (catch 'unread
		   (lyskom-traverse
		    misc-item (text-stat->misc-info-list text-stat)
		    (when (and (eq (misc-info->type misc-item) 'COMM-IN)
			       (not (lyskom-text-read-at-least-once-p 
				     (blocking-do
				      'get-text-stat
				      (misc-info->comm-in misc-item)))))
		      (throw 'unread t))))
	     (unless (lyskom-j-or-n-p
		      (lyskom-format 'have-unread-comment text-stat))
	       (signal 'lyskom-edit-text-abort
		       (list "%s"
			     (lyskom-get-string 
			      'please-check-commented-texts))))))))
David Byers's avatar
David Byers committed
559
      (lyskom-message (lyskom-format 'checking-comments-done)))
560
561
    
    
562
563
564
565
    ;;
    ;; Confirm multiple recipients
    ;;
    
David Byers's avatar
David Byers committed
566
567

    (set-collector->value collector nil)
David Byers's avatar
David Byers committed
568

David Byers's avatar
David Byers committed
569
570
571
    (if (and (lyskom-default-value 'kom-confirm-multiple-recipients)
             (not (eq (lyskom-default-value 'kom-confirm-multiple-recipients)
                      'before))
572
             (> (- (length recipient-list) num-me) 1))
573
574
575
576
577
578
579
580
581
        (save-excursion
          (goto-char (point-min))
          (if (not 
               (lyskom-j-or-n-p
                (lyskom-format 'comment-all-relevant-p) t))
              (signal 'lyskom-edit-text-abort 
                      (list "%s" 
                            (lyskom-get-string 
                             'please-edit-recipients))))))
582

David Byers's avatar
David Byers committed
583
    (if (and (lyskom-default-value 'kom-check-commented-author-membership)
584
             (assq 'comm-to (cdr misc-list)))
585
        (progn
586
          (lyskom-message (lyskom-get-string 'checking-rcpt))
587
588
589
590
591
592
593
594
595
596
597

          ;;
          ;; For each commented text, get the author
          ;;
        
          (setq author-list (mapcar 
                             (function (lambda (x)
                                         (text-stat->author
                                          (blocking-do 'get-text-stat x))))
                             comm-to-list))

598
          ;;
599
          ;; For each author, see if the author is a direct recipient
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
          ;; of the text. If so, there is no point in continuing.
          ;; (People can unsubscribe from their mailboxes, but if they
          ;; do, this code won't help anyway.)
          ;;

          (lyskom-traverse misc (cdr misc-list)
            (cond ((eq (car misc) 'comm-to)
                   (setq comm-to-list (cons (cdr misc)
                                            comm-to-list)))
                  ((or (eq (car misc) 'recpt)
                       (eq (car misc) 'cc-recpt))
                   (if (or (memq (cdr misc) author-list)
                           (eq (cdr misc) me))
                       (setq author-list (delq (cdr misc) author-list))))))

615
          ;;
616
617
          ;; For each author, get his or her memberships in all
          ;; recipient conferences.
618
619
620
          ;;

          (save-excursion
David Byers's avatar
David Byers committed
621
            (set-buffer lyskom-buffer)
622
623
624
625
626
627
628
            (mapcar (function
                     (lambda (author-number)
                       (mapcar
                        (function
                         (lambda (conference-number)
                           (initiate-query-read-texts 
                            'sending
David Byers's avatar
X    
David Byers committed
629
630
631
                            'collector-push
                            author-number conference-number
                            collector)))
632
                        recipient-list)
633

634
                       (lyskom-wait-queue 'sending)
635
636
                       (setq author-is-member (collector->value collector))

David Byers's avatar
David Byers committed
637
                       (if (and (null (delq nil author-is-member))
David Kågedal's avatar
David Kågedal committed
638
				(not (zerop author-number))
639
640
641
642
643
644
645
646
647
648
                                (lyskom-j-or-n-p
                                 (let ((kom-deferred-printing nil))
                                   (lyskom-format
                                    'add-recipient-p
                                    author-number)) t))
                           (setq extra-headers
                                 (nconc (list 'recpt 
                                              author-number)
                                        extra-headers)))))
                    author-list))))
649

650
    extra-headers))
651
    
David Byers's avatar
David Byers committed
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
(defun lyskom-send-enriched (message)
  (condition-case err
      (let ((buf (lyskom-get-buffer-create 'lyskom-enriched 
                                           "lyskom-enriched" 
                                           t)))
        (unwind-protect
            (save-excursion
              (set-buffer buf)
              (insert message)
              (goto-char (point-min))
              (format-encode-buffer 'text/enriched)
              (goto-char (point-min))
              (search-forward "\n\n")
              (if (and (not (string= (buffer-substring (point)
                                                       (point-max)) message))
                       (save-excursion
                         (set-buffer lyskom-buffer)
                         (lyskom-j-or-n-p 
                          (lyskom-get-string 'send-formatted) t)))
                  (concat "enriched:\n" (buffer-string))
                message))
          (kill-buffer buf)))
    (error (if (lyskom-j-or-n-p
                (lyskom-format (lyskom-get-string 'transform-error)
                               (error-message-string err)))
               message
             (signal 'lyskom-edit-text-abort nil)))))

Per Cederqvist's avatar
.    
Per Cederqvist committed
680
681
682
683
684
685
686
687
688


(defun kom-edit-quit ()
  "Kill the text (if any) written so far and continue reading."
  (interactive)
  (let ((edit-buffer (current-buffer)))
    (goto-char (point-max))
    (setq lyskom-is-writing nil)
    (lyskom-tell-internat 'kom-tell-regret)
David Byers's avatar
X    
David Byers committed
689
690
691
692
    (lyskom-save-excursion (set-buffer edit-buffer)
                           (delete-auto-save-file-if-necessary))
    (lyskom-undisplay-buffer edit-buffer)
    (kill-buffer edit-buffer))
Per Cederqvist's avatar
.    
Per Cederqvist committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
  (garbage-collect))			;Take care of the garbage.


(defun kom-edit-show-commented ()
  "Show the commented text in another window."
  (interactive)
  (lyskom-edit-get-commented 'lyskom-edit-show-commented))


(defun kom-edit-insert-commented ()
  "Insert the commented text with '>' first on each line"
  (interactive)
  (lyskom-edit-get-commented 'lyskom-edit-insert-commented))


(defun kom-edit-insert-digit-text ()
  (interactive)
710
  (setq unread-command-events (cons last-command-event unread-command-events))
Per Cederqvist's avatar
.    
Per Cederqvist committed
711
712
713
714
715
  (call-interactively 'kom-edit-insert-text nil))


(defun kom-edit-insert-text (no)
  "Insert the text number NO with '>' first on each line"
Linus Tolke Y's avatar
Linus Tolke Y committed
716
717
718
719
720
721
722
  (interactive (list
		(cond
		 ((null current-prefix-arg)
		  (string-to-int 
		   (read-from-minibuffer 
		    (format "%s" (lyskom-get-string 'which-text-include)))))
		 ((prefix-numeric-value current-prefix-arg)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
723
  (let ((buffer (current-buffer)))
David Byers's avatar
David Byers committed
724
    (set-buffer lyskom-buffer)
Per Cederqvist's avatar
.    
Per Cederqvist committed
725
726
727
728
729
730
731
732
733
734
    (initiate-get-text 'edit 'lyskom-edit-insert-commented no buffer)
    (set-buffer buffer)
    (sit-for 0)))
    

(defun lyskom-edit-get-commented (thendo)
  "Get the commented text and then do THENDO with it."
  (let ((p (point)))
    (save-excursion
      (let* ((buffer (current-buffer))
735
             (headers (condition-case nil
736
737
738
739
740
741
742
743
744
745
746
747
                          (cdr (lyskom-edit-parse-headers))
                        (lyskom-edit-error nil))) ; Ignore these errors
             (no nil))
        (while headers
          (if (or (eq (car headers) 'comm-to)
                  (eq (car headers) 'footn-to))
              (setq no (car (cdr headers))
                    headers nil)
            (setq headers (cdr (cdr headers)))))
        (cond
         (no
          (goto-char p)
David Byers's avatar
David Byers committed
748
          (set-buffer lyskom-buffer)
749
750
751
752
753
          (initiate-get-text 'edit thendo no buffer)
          (set-buffer buffer))
         (t
          (lyskom-message "%s" (lyskom-get-string 'no-such-text-m))))))
    (sit-for 0)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778


;;; ================================================================
;;;	  Add recipient, copy-recipient - Addera mottagare 
;;;
;;;  Author: Anders Gertz
;;;  Changed by: Linus Tolke


(defun kom-edit-add-recipient ()
  "Adds a conference as recipient to the text being edited."
  (interactive)
  (lyskom-edit-add-recipient/copy (lyskom-get-string 'added-recipient)
				  (lyskom-get-string 'recipient)))


(defun kom-edit-add-copy ()
  "Adds a conference to which a copy of the edited text will be sent."
  (interactive)
  (lyskom-edit-add-recipient/copy (lyskom-get-string 'added-carbon-copy)
				  (lyskom-get-string 'carbon-copy)))


(defun lyskom-edit-add-recipient/copy (prompt string)
  "Adds a new recipient or a cc-recipient to the text which is being edited."
779
  (let ((edit-buffer (current-buffer))
780
	(insert-at (point-min-marker))
781
	(conf-stat (lyskom-read-conf-stat prompt '(all) nil "" t)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
782
    (lyskom-save-excursion
783
     ;;(save-excursion
David Byers's avatar
David Byers committed
784
     (set-buffer lyskom-buffer)
785
786
     ;; +++ The information about msg-of-day might be old. We should
     ;; make sure it is up-to-date.
787
788
789
790
791
792
793
794
795
796
797
798
     (let ((text-no (conf-stat->msg-of-day conf-stat)))
       (if (zerop text-no)
	   (lyskom-edit-insert-misc-conf conf-stat string insert-at nil)
	 (let ((text (blocking-do 'get-text text-no)))
	   (if (and text (get-buffer-window edit-buffer))
	       (let ((win-config (current-window-configuration)))
		 ;;(set-buffer buffer)
		 (with-output-to-temp-buffer "*Motd*"
		   (princ (lyskom-format 'conf-has-motd-no
					 (text->text-no text)
					 (text->text-mass text))))
		 (and (j-or-n-p (lyskom-get-string 'still-want-to-add))
799
800
		      (lyskom-edit-insert-misc-conf conf-stat string
						    insert-at nil))
801
		 (set-window-configuration win-config))
802
803
	     (lyskom-edit-insert-misc-conf conf-stat string
					   insert-at nil))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
804
805
806
807
808
809
810
811



;;; ================================================================
;;;   Help functions for the functions bound to keyboard sequences 
;;;                       in lyskom-edit-mode.


812
(defun lyskom-looking-at-header (header match-number)
813
  "Check if point is at the beginning of a header of type HEADER.
David Kågedal's avatar
David Kågedal committed
814
815
Return the corresponding number (conf no etc.) if MATCH-NUMBER is
non-nil. If MATCH-NUMBER is 'angled, only match a number inside <>."
816
  (if (looking-at
817
       (concat (lyskom-get-string header)
818
819
820
821
822
823
824
825
826
827
	       (cond ((eq match-number 'angled)
		      "[^0-9]*<\\([0-9]+\\)>")
		     (match-number
		      "[^0-9]*\\([0-9]+\\)")
		     (nil
		      ""))))
      (if match-number
	  (string-to-int (buffer-substring (match-beginning 1)
					   (match-end 1)))
	t)
828
829
830
831
832
833
834
835
    nil))

(defun lyskom-edit-parse-headers ()
  "Parse the headers of an article.
They are returned as a list where the first element is the subject,
and the rest is a list (HEADER DATA HEADER DATA ...), where HEADER is
either 'recpt, 'cc-recpt, 'comm-to or 'footn-to. This is to make it
easy to use the result in a call to `lyskom-create-misc-list'."
Per Cederqvist's avatar
.    
Per Cederqvist committed
836
  (goto-char (point-min))
837
838
839
840
  (let ((result (cons "" nil)))		; The car will be replaced by
					; the real subject
    (save-restriction
      ;; Narrow to headers
841
842
      (search-forward (substitute-command-keys
		       (lyskom-get-string 'header-separator)))
843
844
845
846
847
848
      (beginning-of-line)
      (narrow-to-region (point-min) (point))
      (goto-char (point-min))
      (while (< (point) (point-max))
	(let ((case-fold-search t)
	      n)
Per Cederqvist's avatar
.    
Per Cederqvist committed
849
	  (cond
850
	   ((setq n (lyskom-looking-at-header 'recipient-prefix 'angled))
851
	    (nconc result (list 'recpt n)))
852
	   ((setq n (lyskom-looking-at-header 'carbon-copy-prefix 'angled))
853
	    (nconc result (list 'cc-recpt n)))
854
	   ((setq n (lyskom-looking-at-header 'comment-prefix t))
855
	    (nconc result (list 'comm-to n)))
856
	   ((setq n (lyskom-looking-at-header 'footnote-prefix t))
857
	    (nconc result (list 'footn-to n)))
858
	   ((lyskom-looking-at-header 'header-subject nil)
859
860
	    (setcar result (lyskom-edit-extract-subject)))
	   (t
861
	    (signal 'lyskom-unknown-header (list 'unknown-header (point))))))
862
863
864
	(forward-line 1)))
    result))

Per Cederqvist's avatar
.    
Per Cederqvist committed
865
866
867
868
869
870
871
872
873


(defun lyskom-edit-extract-subject ()
  "Find the subject.
Point must be located on the line where the subject is."
  (re-search-forward ": \\(.*\\)")
  (buffer-substring (match-beginning 1) (match-end 1)))


874
875
876
877
878
(defun lyskom-edit-extract-text ()
  "Get text as a string."
  (save-excursion
    (goto-char (point-min))
    (if (not (or (re-search-forward 
879
		  (substitute-command-keys
880
		   (lyskom-get-string 'header-separator))
881
		  nil (point-max))
882
883
		 (search-forward 
		  (substitute-command-keys
884
		   (lyskom-get-string 'header-separator)) 
885
		  nil (point-max))))
886
887
888
889
890
891
892
893
894
	(signal 'lyskom-internal-error
		"Altered lyskom-header-separator line.")
      (buffer-substring (1+ (point))
			(progn
			  (goto-char (1- (point-max)))
			  (while (looking-at "\\s-")	; remove trailing
			    (backward-char 1))		; whitespace
			  (forward-char 1)
			  (point))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
895
896
897
898

  
(defun lyskom-create-text-handler (text-no edit-buffer)
  "Handle an attempt to write a text."
David Kågedal's avatar
David Kågedal committed
899
  (lyskom-tell-internat 'kom-tell-silence)
Per Cederqvist's avatar
.    
Per Cederqvist committed
900
901
902
903
  (message "")
  (cond
   ((null text-no)
    (lyskom-insert-before-prompt
Linus Tolke Y's avatar
Linus Tolke Y committed
904
905
     (lyskom-format 'could-not-create-text lyskom-errno
		    (lyskom-get-error-text lyskom-errno)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
906
    (beep)
907
    (lyskom-message "%s" (lyskom-format 'could-not-create-text lyskom-errno
Linus Tolke Y's avatar
Linus Tolke Y committed
908
				   (lyskom-get-error-text lyskom-errno)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
    (set-buffer edit-buffer)
    (setq mode-name lyskom-edit-mode-name)
    (sit-for 0))
   (t
    (lyskom-insert-before-prompt
     (lyskom-format 'text-created  text-no))

    ;; Immediately mark the text as read if kom-created-texts-are-read is set.
    
    (cond
     (kom-created-texts-are-read
      (lyskom-is-read text-no)
      (initiate-get-text-stat 'background 'lyskom-mark-as-read
			      text-no)
      (lyskom-run 'background 'set 'lyskom-dont-change-prompt nil)
David Kågedal's avatar
David Kågedal committed
924
      (lyskom-run 'background 'lyskom-set-mode-line))
925
     (t					; Probably not necessary
David Kågedal's avatar
David Kågedal committed
926
927
      (setq lyskom-dont-change-prompt nil)))
    
Per Cederqvist's avatar
.    
Per Cederqvist committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
    (set-buffer edit-buffer)		;Need local variables.

    ;; Select the old configuration.

    (let ((hnd lyskom-edit-handler)
	  (dta lyskom-edit-handler-data))
      (cond
       ((get-buffer-window edit-buffer)
	(set-window-configuration lyskom-edit-return-to-configuration)
	(set-buffer (window-buffer (selected-window)))
	(goto-char (point-max))))
	

      ;; Apply handler.

David Byers's avatar
David Byers committed
943
      (set-buffer lyskom-buffer)
Per Cederqvist's avatar
.    
Per Cederqvist committed
944
945
946
947
948
949
950
951
      (if hnd
	  (apply hnd text-no dta)))
    
    ;; Kill the edit-buffer.

    (lyskom-save-excursion
     (set-buffer edit-buffer)
     (delete-auto-save-file-if-necessary))
David Byers's avatar
X    
David Byers committed
952
    (kill-buffer edit-buffer))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
953
954
955
956
957
958
959
960
961
962
963


(defun lyskom-edit-show-commented (text editing-buffer)
  "Handles the TEXT from the return of the call of the text.
The EDITING-BUFFER is the buffer the editing is done in. If this buffer is 
not displayed nothing is done. If displayed then this buffer is chosen then 
the with-output-to-temp-buffer command is issued to make them both apear."
  (and text
       (get-buffer-window editing-buffer)
       (progn
	 (set-buffer editing-buffer)
David Byers's avatar
X    
David Byers committed
964
965
966
967
968
969
970
971
         (let ((buf (lyskom-get-buffer-create 'view-commented "*Commented*"))
               (kom-deferred-printing nil))
           (lyskom-display-buffer buf)
           (save-excursion (set-buffer buf)
                           (erase-buffer)
                           (lyskom-view-text (text->text-no text))
                           (set-buffer-modified-p nil)
                           (lyskom-view-mode))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
972
973
974
975
976
977
978


(defun lyskom-edit-insert-commented (text editing-buffer)
  "Handles the TEXT from the return of the call of the text.
The text is inserted in the buffer with '>' first on each line."
  (if text
      (progn
979
980
981
982
983
984
985
986
987
988
989
990
991
992
        (set-buffer editing-buffer)
        (and (not (bolp))
             (insert "\n"))
        (and (not (eolp))
             (open-line 1))
        (let* ((pb (point))
               (as (string-match "\n" (text->text-mass text)))
               (te (substring (text->text-mass text) (1+ as))))
          (insert te)
          (while (<= pb (point))
            (beginning-of-line)
            (insert (or kom-cite-string 62))
            (forward-line -1)
            )))
993
    (lyskom-message "%s" (lyskom-get-string 'no-get-text))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
994
995


996

Per Cederqvist's avatar
.    
Per Cederqvist committed
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
;;; ================================================================
;;;        Maphanteringsfunktion - keymap handling.
;;;
;;; Author: Linus Tolke
;;;

(defun overlay-map (oldmap newmap)
  "Returns a map that is the union of OLDMAP and NEWMAP. NEW-MAP has priority.
This function chooses whether the returned map is a list or an array.
Currently always same type as oldmap.
BUG: does not descend in the maps."
  (cond
   ((not (keymapp oldmap))
    newmap)
   ((not (keymapp newmap))
    oldmap)
   (t
    (let ((map (copy-keymap oldmap))
	  (r 0))
1016
1017
1018
      (cond
       ((fboundp 'map-keymap)		;Special for lucid-emacs
	(map-keymap
Per Cederqvist's avatar
.    
Per Cederqvist committed
1019
	 (function
1020
	  (lambda (event function)
1021
	    (define-key map (vector event) function)))
1022
	 newmap))
Linus Tolke Y's avatar
Linus Tolke Y committed
1023
1024
1025
1026
1027
       ((and (string-match "^19" emacs-version)
	     (arrayp (car (cdr newmap))))
	(while (< r (length (car (cdr newmap))))
	  (if (aref (car (cdr newmap)) r)
	      (define-key map (char-to-string r) (aref (car (cdr newmap)) r)))
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
	  (setq r (1+ r)))
	(mapcar
	 (function
	  (lambda (ele)
	    (define-key map 
	      (cond
	       ((integerp (car ele))
		(char-to-string (car ele)))
	       ((vector (car ele))))
	      (cdr ele))))
	 (cdr (cdr newmap))))
Linus Tolke Y's avatar
Linus Tolke Y committed
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
       ((arrayp newmap)
	(while (< r (length newmap))
	  (if (aref newmap r)
	      (define-key map (char-to-string r) (aref newmap r)))
	  (setq r (1+ r))))
       (t
	(mapcar
	 (function
	  (lambda (ele)
	    (define-key map 
	      (cond
	       ((integerp (car ele))
		(char-to-string (car ele)))
	       ((vector (car ele))))
	      (cdr ele))))
	 (cdr newmap))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
1055
      map))))