edit-text.el 77.1 KB
Newer Older
David Byers's avatar
David Byers committed
1
;;;;; -*-coding: iso-8859-1;-*-
Linus Tolke's avatar
Linus Tolke committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991-2002  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
5
;;;;;
6
;;;;; This file is part of the LysKOM Emacs LISP client.
Linus Tolke's avatar
Linus Tolke committed
7
8
9
;;;;; 
;;;;; LysKOM is free software; you can redistribute it and/or modify it
;;;;; under the terms of the GNU General Public License as published by 
10
;;;;; the Free Software Foundation; either version 2, or (at your option) 
Linus Tolke's avatar
Linus Tolke committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
;;;;; any later version.
;;;;; 
;;;;; LysKOM is distributed in the hope that it will be useful, but WITHOUT
;;;;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
;;;;; FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
;;;;; for more details.
;;;;; 
;;;;; You should have received a copy of the GNU General Public License
;;;;; along with LysKOM; see the file COPYING.  If not, write to
;;;;; Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
;;;;; or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
;;;;; MA 02139, USA.
;;;;;
;;;;; Please mail bug reports to bug-lyskom@lysator.liu.se. 
;;;;;
Per Cederqvist's avatar
.  
Per Cederqvist committed
26
27
28
29
30
31
32
33
34
;;;; ================================================================
;;;; ================================================================
;;;;
;;;; File: 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.
;;;;

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


Per Cederqvist's avatar
.  
Per Cederqvist committed
40
41
;;;; ================================================================

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
;;; Set variables to make lyskom-edit-mode a minor mode. This
;;; simplifies some stuff a lot

(defvar lyskom-edit-mode nil
  "Mode variable for lyskom-edit-mode")
(make-variable-buffer-local 'lyskom-edit-mode)

(defvar lyskom-edit-sending-mode nil
  "Mode variable for lyskom-edit-sending-mode")
(make-variable-buffer-local 'lyskom-edit-sending-mode)

(defvar lyskom-edit-sent-mode nil
  "Mode variable for lyskom-edit-sent-mode")
(make-variable-buffer-local 'lyskom-edit-sent-mode)

(put 'lyskom-edit-mode 'permanent-local t)

(or (assq 'lyskom-edit-mode minor-mode-alist)
    (setq minor-mode-alist (cons '(lyskom-edit-mode " LysKOM Edit")
                                 minor-mode-alist)))

(or (assq 'lyskom-edit-sending-mode minor-mode-alist)
    (setq minor-mode-alist (cons '(lyskom-edit-sending-mode " LysKOM Sending")
                                 minor-mode-alist)))

(or (assq 'lyskom-edit-sent-mode minor-mode-alist)
    (setq minor-mode-alist (cons '(lyskom-edit-sent-mode " LysKOM Sent")
                                 minor-mode-alist)))

(or (assq 'lyskom-edit-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
	  (cons (cons 'lyskom-edit-mode lyskom-edit-mode-map)
		minor-mode-map-alist)))

(or (assq 'lyskom-edit-sending-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
	  (cons (cons 'lyskom-edit-sending-mode lyskom-edit-mode-map)
		minor-mode-map-alist)))

(or (assq 'lyskom-edit-sent-mode minor-mode-map-alist)
    (setq minor-mode-map-alist
	  (cons (cons 'lyskom-edit-sending-mode lyskom-edit-mode-map)
		minor-mode-map-alist)))


Per Cederqvist's avatar
.  
Per Cederqvist committed
87
88
89
90

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

91
92
93
(defvar lyskom-edit-text-sent nil
  "Non-nil when a text has been sent")

Linus Tolke's avatar
Linus Tolke committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
(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.")

108
;;; Error signaled by lyskom-edit-parse-headers
109
(put 'lyskom-edit-text-abort 'error-conditions
110
     '(error lyskom-error lyskom-edit-error lyskom-edit-text-abort))
111

112
(put 'lyskom-unknown-header 'error-conditions
113
     '(error lyskom-error lyskom-edit-error lyskom-unknown-header))
114
115

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

118
119
120
(put 'lyskom-edit-error 'error-conditions
     '(error lyskom-error lyskom-edit-error))

Per Cederqvist's avatar
.  
Per Cederqvist committed
121
122
123
124
125
126
127
128
129
130
131
132
133
(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)
134
  (lyskom-dispatch-edit-text proc misc-list subject body
135
                             handler data))
136
137
138
139


(defun lyskom-dispatch-edit-text (proc misc-list subject body
				       &optional handler &rest data)
140
  "Same as lyskom-edit-text except that it doesn't set lyskom-is-writing."
David Byers's avatar
X    
David Byers committed
141
142
143
144
  (let ((buffer (lyskom-get-buffer-create 'write-texts
                                          (concat 
                                           (buffer-name (process-buffer proc))
                                           "-edit")))
Per Cederqvist's avatar
.  
Per Cederqvist committed
145
	(config (current-window-configuration)))
David Byers's avatar
X    
David Byers committed
146
147

    (lyskom-display-buffer buffer)
148
149
150
    (text-mode)
    (lyskom-ignore-errors
      (run-hooks 'lyskom-edit-mode-mode-hook))
David Byers's avatar
X    
David Byers committed
151
    (lyskom-edit-mode)
Per Cederqvist's avatar
.  
Per Cederqvist committed
152
153
154
155
156
157
    (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)
David Byers's avatar
David Byers committed
158
    (buffer-disable-undo)
Per Cederqvist's avatar
.  
Per Cederqvist committed
159
    (lyskom-edit-insert-miscs misc-list subject body)
David Byers's avatar
David Byers committed
160
    (buffer-enable-undo)
161
    (goto-char (point-min))
162
163
    (re-search-forward (regexp-quote (lyskom-get-string
				      'header-subject))
164
165
		       (point-max)
		       'end)
166
167
    (if (not (looking-at "\\s-*$"))
        (goto-char (point-max)))
168
    (lyskom-message "%s" (lyskom-get-string 'press-C-c-C-c)))
David Byers's avatar
David Byers committed
169
  (set-buffer lyskom-buffer)
Linus Tolke's avatar
Linus Tolke committed
170
  )
Per Cederqvist's avatar
.  
Per Cederqvist committed
171
172


173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
(defun lyskom-edit-find-separator (&optional move-point)
  "Return the end position of the separator line.
If MOVE-POINT is non-nil, move point to the start of the separator."
  (prog1
      (save-excursion
        (goto-char (point-min))
        (and (re-search-forward
              (concat "^"
                      (regexp-quote
                       (substitute-command-keys
                        (lyskom-get-string 'header-separator)))
                      "$")
              nil t)))
    (when (and move-point (match-beginning 0))
      (goto-char (match-beginning 0)))))

David Byers's avatar
David Byers committed
189

190
(defun lyskom-edit-insert-miscs (misc-list subject body &optional aux-list)
Per Cederqvist's avatar
.  
Per Cederqvist committed
191
  "Insert MISC-LIST into header of text.
David Byers's avatar
David Byers committed
192
193
194
195
196
RECPT		-> Mottagare: <%d> %s.
CC-RECPT	-> Extra kopia: <%d> %s.
BCC-RECPT       -> Blind kopia: <%d> %s.
COMM-TO		-> Kommentar till text %d.
FOOTN-TO	-> Fotnot till text %d.
197
nil             -> Ingenting."
Per Cederqvist's avatar
.  
Per Cederqvist committed
198
199
  (let ((edit-buffer (current-buffer))
	(where-put-misc (point-min-marker))
David Byers's avatar
David Byers committed
200
	(main-buffer lyskom-buffer))
Per Cederqvist's avatar
.  
Per Cederqvist committed
201
202
203
204
205
206
    (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
David Byers's avatar
David Byers committed
207
	 ((eq key 'RECPT)
208
209
210
	  (lyskom-edit-insert-misc-conf (blocking-do 'get-conf-stat data)
					(lyskom-get-string 'recipient)
					where-put-misc data))
David Byers's avatar
David Byers committed
211
	 ((eq key 'CC-RECPT)
212
213
214
	  (lyskom-edit-insert-misc-conf (blocking-do 'get-conf-stat data)
					(lyskom-get-string 'carbon-copy)
					where-put-misc data))
David Byers's avatar
David Byers committed
215
	 ((eq key 'BCC-RECPT)
David Byers's avatar
David Byers committed
216
217
218
	  (lyskom-edit-insert-misc-conf (blocking-do 'get-conf-stat data)
					(lyskom-get-string 'blank-carbon-copy)
					where-put-misc data))
David Byers's avatar
David Byers committed
219
	 ((eq key 'COMM-TO)
220
221
222
	  (lyskom-edit-get-commented-author (blocking-do 'get-text-stat data)
					    (lyskom-get-string 'comment)
					    where-put-misc data))
David Byers's avatar
David Byers committed
223
	 ((eq key 'FOOTN-TO)
224
225
226
227
	  (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))))
228
229
    (mapcar (function
             (lambda (item)
230
               (let ((data (lyskom-aux-item-call
231
                            item '(edit-insert print)
David Byers's avatar
David Byers committed
232
                            item lyskom-pers-no)))
233
234
                 (when data
                   (lyskom-princ
235
236
237
238
239
240
                    (lyskom-format "%#1@%[%#3s%] %#2s\n" 
                            (lyskom-default-button 'aux-edit-menu 
                                                   (cons edit-buffer
                                                         (copy-marker where-put-misc)))
                            data
                            (lyskom-get-string 'aux-item-prefix))
241
242
                    where-put-misc)))))
            aux-list)
243
244
245
246
247
248
    (unless kom-edit-hide-add-button
      (lyskom-princ (lyskom-format "%[%#1@%#2s%]\n"
                                   (lyskom-default-button 'add-recipient-or-xref
                                                          edit-buffer)
                                   (lyskom-get-string 'add-recpt-button-text))
                    where-put-misc))
249
250
251
252
253
254
    (lyskom-princ (lyskom-format 'text-mass subject 
				 (substitute-command-keys
				  (lyskom-get-string 'header-separator))
				 body 
				 (lyskom-get-string 'header-subject))
		  where-put-misc)
255
256
257
    (set-buffer edit-buffer)
    (goto-char where-put-misc)
    ))
Per Cederqvist's avatar
.  
Per Cederqvist committed
258
259
260
261
262
263
264
265
266
267
268
269


(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)
270
271
  "Insert Recipient:, Carbon copy: or Blind Carbon copy: in
edit-buffer.
Per Cederqvist's avatar
.  
Per Cederqvist committed
272
273
CONF-STAT is the conf-stat of the conference that is about to be put in,
STRING is the string that is inserted.
274
STREAM is the buffer or a marker telling the position.
Per Cederqvist's avatar
.  
Per Cederqvist committed
275
NUMBER is the number of the person. Used if the conf-stat is nil."
276
277
278
279
280
281
282
283
284
285
286
287
  (lyskom-princ
   (lyskom-format "%[%#1@%#2s%]: <%#3m> %#4M\n" 
                  (lyskom-default-button
                   'recpt-type
                   (list (or (conf-stat->conf-no conf-stat) number)
                         (marker-buffer stream))
                   (list (lyskom-get-string 'recpt-type-popup-title)
                         string))
                  string
                  (or conf-stat number)
                  (or conf-stat ""))
   stream))
Per Cederqvist's avatar
.  
Per Cederqvist committed
288
289
290

(defun lyskom-edit-get-commented-author (text-stat string stream number)
  (if text-stat
David Byers's avatar
David Byers committed
291
292
293
294
295
296
      (let ((mx-from (car (lyskom-get-aux-item (text-stat->aux-items text-stat) 17)))
            (mx-author (car (lyskom-get-aux-item (text-stat->aux-items text-stat) 16))))
        
       

        (lyskom-edit-insert-commented-author 
297
         (if (or mx-from mx-author)
David Byers's avatar
David Byers committed
298
299
300
             (lyskom-format-mx-author mx-from mx-author)
           (blocking-do 'get-conf-stat (text-stat->author text-stat)))
         string stream number))
Per Cederqvist's avatar
.  
Per Cederqvist committed
301
302
303
304
    (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
305
306
307
308
309
310
311
312
  (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
313

Per Cederqvist's avatar
.  
Per Cederqvist committed
314
315
316
317
318
319
320
321
322
323
324


(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
David Byers's avatar
David Byers committed
325
item in each pair should be one of RECPT, CC-RECPT, COMM-TO or FOOTN-TO.
Per Cederqvist's avatar
.  
Per Cederqvist committed
326
327
328
The second item should be the corresponding conf- or text-no.

The result is a list of dotted pairs:
David Byers's avatar
David Byers committed
329
330
331
332
333
	('RECPT . conf-no)
	('CC-RECPT . conf-no)
	('BCC-RECPT . conf-no)
	('COMM-TO . text-no)
	('FOOTN-TO . text-no).
Per Cederqvist's avatar
.  
Per Cederqvist committed
334
First element is a type-tag."
David Byers's avatar
David Byers committed
335
   (let ((result (cons 'MISC-LIST nil)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
336
337
338
339
340
341
342
343
344
345
346
    (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's avatar
Linus Tolke committed
347
348
349
350
351
352
(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.")

353
354
355
356
357
358
359
360
361
362
363
(defvar lyskom-edit-mode-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.

This one differs from lyskom-edit-mode-hook in that it is called before
the lyskom-special key bindings are added.")


(defun lyskom-edit-mode (&optional arg)
Per Cederqvist's avatar
.  
Per Cederqvist committed
364
365
  "\\<lyskom-edit-mode-map>Mode for editing texts for LysKOM.
Commands:
Linus Tolke's avatar
Linus Tolke committed
366
367
368
369
370
371
372
373
\\[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.
374
\\[kom-edit-add-cross-reference]   asks for what to refer to and adds a cross reference to it.
Linus Tolke's avatar
Linus Tolke committed
375

Joel Rosdahl's avatar
Joel Rosdahl committed
376
\\[kom-edit-insert-commented]   inserts the commented or footnoted text.
377
\\[kom-edit-insert-text]   inserts the shown text, you tell the number.
378
\\[kom-edit-insert-link]   asks for what to link to and inserts a link to it.
379
380
381
382
383
384
385
386
387

Even though this is a minor mode, it's not intended to be turned on and off,
so it's not as clean as it ought to be."
  (interactive "P")
  (setq lyskom-edit-mode
        (if (null arg)
            (not lyskom-edit-mode)
          (> (prefix-numeric-value arg) 0)))

388
389
  (lyskom-set-menus 'lyskom-edit-mode lyskom-edit-mode-map)

390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
  (when lyskom-edit-mode
    (lyskom-edit-sending-mode 0)
    (lyskom-edit-sent-mode 0)
    (auto-fill-mode 1)
    (auto-save-mode 1)
    (when (not (local-variable-p 'lyskom-edit-text-sent (current-buffer)))
      (make-local-variable 'lyskom-edit-text-sent)
      (setq lyskom-edit-text-sent nil))
    (make-local-variable 'paragraph-start)
    (make-local-variable 'paragraph-separate)
    (setq paragraph-start (concat "^" 
                                  (regexp-quote 
                                   (substitute-command-keys
                                    (lyskom-get-string 'header-separator)))
                                  "$\\|" paragraph-start))
    (setq paragraph-separate (concat "^" 
                                     (regexp-quote 
                                      (substitute-command-keys
                                       (lyskom-get-string 'header-separator)))
                                     "$\\|" paragraph-separate))
410
411
412
413
    (make-local-variable 'comment-start)
    (make-local-variable 'comment-end)
    (setq comment-start kom-cite-string
	  comment-end "")
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
    (run-hooks 'lyskom-edit-mode-hook)))

(defun lyskom-edit-sending-mode (arg)
  (interactive "P")
  (setq lyskom-edit-sending-mode 
        (if (null arg)
            (not lyskom-edit-sending-mode)
          (> (prefix-numeric-value arg) 0)))

  (when lyskom-edit-sending-mode
    (lyskom-edit-mode 0)
    (lyskom-edit-sent-mode 0)))

(defun lyskom-edit-sent-mode (arg)
  (interactive "P")
  (setq lyskom-edit-sent-mode 
        (if (null arg)
            (not lyskom-edit-sent-mode)
          (> (prefix-numeric-value arg) 0)))

  (when lyskom-edit-sent-mode
    (lyskom-edit-sending-mode 0)
    (lyskom-edit-mode 0)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
437
438
439
440
441
442


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

443
444
445
(defun kom-edit-send-anonymous ()
  "Send the text anonymously to the server."
  (interactive)
David Byers's avatar
David Byers committed
446
  (lyskom-edit-send 'initiate-create-anonymous-text t))
Per Cederqvist's avatar
.  
Per Cederqvist committed
447
448
449
450

(defun kom-edit-send ()
  "Send the text to the server."
  (interactive)
David Byers's avatar
David Byers committed
451
   (if (and (lyskom-default-value 'lyskom-is-anonymous)
452
            (lyskom-j-or-n-p 'do-send-anonymous))
David Byers's avatar
David Byers committed
453
454
       (lyskom-edit-send 'initiate-create-anonymous-text t)
     (lyskom-edit-send 'initiate-create-text nil)))
455

456
457
458
459
(defun lyskom-edit-send (send-function &optional is-anonymous)
  "Send the text to the server by calling SEND-FUNCTION.
If optional IS-ANONYMOUS is non-nil, assume that the text is being submitted
anonymously and take actions to avoid revealing the sender."
460
  (condition-case err
David Byers's avatar
David Byers committed
461
      (if (or (not lyskom-edit-text-sent)
David Byers's avatar
David Byers committed
462
463
464
	      (j-or-n-p (lyskom-get-string 'already-sent)))
	  (progn 
	    (let ((buffer (current-buffer))
465
466
467
468
469
		  (headers nil)
                  (misc-list nil)
                  (subject nil)
                  (message nil)
                  (aux-list nil))
David Byers's avatar
David Byers committed
470

471
472
473
474
475
476
477
478
479
              ;;
              ;; Run user hooks
              ;; ####: ++++: FIXME: We should quit more graciously.

              (if (and (not (run-hook-with-args-until-failure 'kom-send-text-hook))
                       (not (run-hook-with-args-until-failure 'lyskom-send-text-hook)))
                  (signal 'lyskom-edit-text-abort nil))


David Byers's avatar
David Byers committed
480
481
	      (save-excursion
		(setq headers (lyskom-edit-parse-headers)
482
483
484
		      misc-list (apply 'lyskom-create-misc-list 
                                       (elt headers 1))
                      aux-list (elt headers 2)
David Byers's avatar
David Byers committed
485
		      subject (car headers)))
David Byers's avatar
David Byers committed
486
487

              ;;
488
489
490
              ;; Check that there is a subject
              ;;

David Byers's avatar
David Byers committed
491
492
	      (if (or (null subject)
                      (string= subject ""))
David Byers's avatar
David Byers committed
493
494
		  (let ((old (point)))
		    (goto-char (point-min))
495
		    (re-search-forward (lyskom-get-string 'header-subject)
David Byers's avatar
David Byers committed
496
497
498
				       nil t)
		    (end-of-line)
		    (if (/= (point) old)
499
			(signal 'lyskom-no-subject '(enter-subject-idi)))))
500
501
502
503
504
505
506
507
508
509

              ;;
              ;; 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
510
511
                                           (nconc (elt headers 1)
						  extra-headers)))))
512

David Byers's avatar
David Byers committed
513
514
515
516
517
518
519
520
521
522
523
              ;;
              ;; Check that we don't add ourselves to an anon text
              ;;

              (when (and is-anonymous
                         (rassq lyskom-pers-no (cdr misc-list))
                         (lyskom-j-or-n-p 
                          (lyskom-get-string 'remove-self-sending-anonymous)))
                (rplacd misc-list (delq (rassq lyskom-pers-no (cdr misc-list))
                                        (cdr misc-list))))

David Byers's avatar
David Byers committed
524

525
526
527
528
              ;;
              ;; Transform the message text
              ;;

529
	      (setq message
David Byers's avatar
David Byers committed
530
531
532
533
                    (if (fboundp lyskom-send-text-transform-function)
                        (funcall lyskom-send-text-transform-function
                                 (lyskom-edit-extract-text))
                      (lyskom-edit-extract-text)))
534

David Byers's avatar
David Byers committed
535
	      (save-excursion
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
                (let* ((full-message
                        (cond ((and lyskom-allow-missing-subject
                                    (or (null subject)
                                        (string= subject ""))
                                    (not (string-match ".*\n" message)))
                               message)
                              (t (concat (or subject "") "\n" message))))
                       (content-type (lyskom-get-aux-item aux-list 1))
                       (charset (and content-type
                                     (cdr (lyskom-mime-decode-content-type
                                           (aux-item->data (car content-type))))))
                       (mime-charset (lyskom-mime-string-charset full-message)))

                  ;; If the charset isn't already set, encode the string

                  (if (and mime-charset (null charset))
                      (setq full-message
                            (lyskom-mime-encode-string full-message))
                    (when (lyskom-j-or-n-p 'too-many-languages)
                      (keyboard-quit)))

                  ;; Add the charset data to the content type

                  (cond ((> (length content-type) 1)
                         (lyskom-error (lyskom-get-string 'too-many-content-types)))
                        ((null content-type)
                         (setq aux-list
                               (cons (lyskom-create-aux-item 0 1 nil nil
                                                             (lyskom-create-aux-item-flags nil nil nil nil nil nil nil nil)
                                                             0
566
                                                             (format "text/x-kom-basic;charset=%S" mime-charset))
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
                                     aux-list)))
                        ((null charset)
                         (set-aux-item->data (car content-type)
                                             (format "%s;charset=%S"
                                                     (aux-item->data (car content-type))
                                                     mime-charset))))

                  ;; Send the text

                  (lyskom-edit-sending-mode 1)
                  (set-buffer lyskom-buffer)
                  ;; Don't change the prompt if we won't see our own text
                  (if (and kom-created-texts-are-read
                           (not is-anonymous))
                      (setq lyskom-dont-change-prompt t))
                  (setq lyskom-is-writing nil)
                  (lyskom-tell-internat 'kom-tell-send)
584
585
586
587
588
589
590
591
592
593
594
595
596
597
                  (run-hook-with-args 'lyskom-create-text-hook
                              full-message
                              misc-list
                              (if (not is-anonymous)
                                  (cons (lyskom-create-aux-item
                                         0 15 0 0
                                         (lyskom-create-aux-item-flags
                                          nil nil nil nil nil nil nil nil)
                                         0 (concat "lyskom.el "
                                                   lyskom-clientversion))
                                        aux-list)
                                aux-list)
                              buffer
                              is-anonymous)
David Byers's avatar
David Byers committed
598
599
600
601
602
603
604
605
606
607
608
609
610
611
                  (run-hook-with-args 'kom-create-text-hook
                              full-message
                              misc-list
                              (if (not is-anonymous)
                                  (cons (lyskom-create-aux-item
                                         0 15 0 0
                                         (lyskom-create-aux-item-flags
                                          nil nil nil nil nil nil nil nil)
                                         0 (concat "lyskom.el "
                                                   lyskom-clientversion))
                                        aux-list)
                                aux-list)
                              buffer
                              is-anonymous)
612

613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
                  (funcall send-function
                           'sending
                           'lyskom-create-text-handler
                           full-message
                           misc-list
                           (if (not is-anonymous)
                               (cons (lyskom-create-aux-item
                                      0 15 0 0
                                      (lyskom-create-aux-item-flags
                                       nil nil nil nil nil nil nil nil)
                                      0 (concat "lyskom.el "
                                                lyskom-clientversion))
                                     aux-list)
                             aux-list)
                           buffer
                           is-anonymous))))
David Byers's avatar
David Byers committed
629
            (lyskom-undisplay-buffer)
David Byers's avatar
David Byers committed
630
	    (goto-char (point-max))))
David Byers's avatar
David Byers committed
631
    ;;
632
    ;; Catch no-subject and other things
David Byers's avatar
David Byers committed
633
634
    ;;

635
    (lyskom-edit-text-abort
636
637
     (apply 'lyskom-message (cdr-safe err)))
    (lyskom-no-subject
David Byers's avatar
David Byers committed
638
     (lyskom-beep kom-ding-on-no-subject)
David Byers's avatar
David Byers committed
639
640
     (if (cdr-safe (cdr-safe err))
	 (goto-char (car-safe (cdr-safe (cdr-safe err)))))
641
     (lyskom-message "%s" (lyskom-get-string (car (cdr err))))
642
     (condition-case nil
David Byers's avatar
David Byers committed
643
644
         (let ((text ""))
           (save-excursion
David Byers's avatar
David Byers committed
645
             (set-buffer lyskom-buffer)
David Byers's avatar
David Byers committed
646
647
648
649
             (if (and (string= "kom.lysator.liu.se" lyskom-server-name)
                      (eq lyskom-pers-no 698))
                 (setq text "rende, IDI!")))
           (save-excursion (insert text)))
650
651
       (error nil)))
    (lyskom-unknown-header
652
     (lyskom-message "%s" (lyskom-get-string (car (cdr err)))))))
David Byers's avatar
David Byers committed
653

David Byers's avatar
David Byers committed
654
(eval-when-compile (defvar ispell-dictionary nil))
David Byers's avatar
David Byers committed
655
656
657
(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
658
659


660
661
(defun lyskom-ispell-text ()
  "Check spelling of the text body.
David Byers's avatar
David Byers committed
662
Put this in kom-send-text-hook"
663
664
665
666
  (kom-ispell-message)
  t)


David Byers's avatar
David Byers committed
667
668
669
670
671
672
(eval-when-compile
  (defvar ispell-dictionary nil)
  (defvar ispell-message-text-end nil)
  (defvar ispell-message-start-skip nil)
  (defvar ispell-message-end-skip nil))

673
674
675
676
677
(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
678
  (require 'ispell)
679
680
  (let ((ispell-dictionary (or kom-ispell-dictionary ispell-dictionary))
        (kill-ispell (or (not (boundp 'ispell-dictionary))
681
                         (not (lyskom-string= kom-ispell-dictionary
682
683
684
685
686
687
688
689
                                       ispell-dictionary))))
        (result nil))
    (when kill-ispell (ispell-kill-ispell t))

    ;; Checking code

    (save-excursion
      (goto-char (point-min))
690
      (let* ((internal-messagep (lyskom-edit-find-separator t))
691
692
693
694
695
696
697
698
             (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))))))
David Byers's avatar
David Byers committed
699
700
701
             (cite-regexp 
              (regexp-quote
               (lyskom-default-value 'kom-cite-string)))
702
703
704
705
706
707
708
             (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)))))
709
        (lyskom-ignore ispell-checking-message)
710
711
712
713
        (goto-char (point-min))
        (while (if internal-messagep
                   (< (point) internal-messagep)
                 (not (eobp)))
David Byers's avatar
David Byers committed
714
          (if (lyskom-looking-at subject-string)
715
716
717
718
719
720
721
722
723
              (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)
David Byers's avatar
David Byers committed
724
          (while (and (lyskom-looking-at cite-regexp-start)
725
726
727
728
729
730
731
732
                      (< (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
733
                                   (boundp 'ispell-message-start-skip)
734
                                   (stringp ispell-message-start-skip)
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
                                   (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))

754
755
756
(defun lyskom-is-permitted-author (conf-stat)
  (and conf-stat
       (or (eq 0 (conf-stat->permitted-submitters conf-stat))
David Byers's avatar
David Byers committed
757
758
           (lyskom-is-supervisor (conf-stat->conf-no conf-stat)
                                 lyskom-pers-no))))
759

David Byers's avatar
David Byers committed
760

761
762
763
(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
David Byers's avatar
David Byers committed
764
765
766
text is a member of some recipient of this text.

Cannot be called from a callback."
767
768
  (let* ((comm-to-list nil)
         (recipient-list nil)
David Byers's avatar
David Byers committed
769
         (text-stat nil)
770
         (collector (make-collector))
771
         (extra-headers nil)
David Byers's avatar
David Byers committed
772
         (buffer (current-buffer))
David Byers's avatar
David Byers committed
773
         (me (save-excursion (set-buffer lyskom-buffer)
774
                             lyskom-pers-no))
David Byers's avatar
David Byers committed
775
776
         (num-me 0)
         (num-real-recpt 0))
David Byers's avatar
David Byers committed
777
    (lyskom-ignore text-stat)       ; Have no idea if its ever used...
778
779
780

    ;;
    ;; List all texts this text is a comment to
David Byers's avatar
David Byers committed
781
    ;; List all recipients of the text
782
783
784
    ;;

    (lyskom-traverse misc (cdr misc-list)
David Byers's avatar
David Byers committed
785
      (cond ((eq (car misc) 'COMM-TO)
786
787
             (setq comm-to-list (cons (cdr misc)
                                      comm-to-list)))
David Byers's avatar
David Byers committed
788
789
            ((memq (car misc) lyskom-recpt-types-list)
             (when (eq (car misc) 'RECPT)
David Byers's avatar
David Byers committed
790
791
               (setq num-real-recpt (1+ num-real-recpt))
               (when (eq (cdr misc) me) (setq num-me (1+ num-me))))
792
793
             (setq recipient-list (cons (cdr misc) recipient-list)))))

David Byers's avatar
David Byers committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
    ;;
    ;; Check that there are recipients
    ;;

    (when (null recipient-list)
      (lyskom-error "%s" (lyskom-format 'no-recipients)))

    ;;
    ;; Check for duplicate recipients
    ;;

    (let ((tmp recipient-list))
      (while tmp
        (when (memq (car tmp) (cdr tmp))
          (lyskom-error "%s" (lyskom-format 'duplicate-recipients (car tmp))))
        (setq tmp (cdr tmp))))

David Byers's avatar
David Byers committed
811
812
813
814
815
    ;;
    ;; Check for new comments
    ;;

    (when (save-excursion (set-buffer lyskom-buffer)
David Byers's avatar
David Byers committed
816
817
818
819
820
                          (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)))
821
      (lyskom-message "%s" (lyskom-format 'checking-comments))
David Byers's avatar
David Byers committed
822
823
824
      (save-excursion
        (set-buffer lyskom-buffer)
        (set-collector->value collector nil)
David Byers's avatar
David Byers committed
825

David Byers's avatar
David Byers committed
826
827
828
829
830
831
832
833
        (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)
David Byers's avatar
David Byers committed
834

835
	(lyskom-traverse
David Byers's avatar
David Byers committed
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
            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))))))))
853
      (lyskom-message "%s" (lyskom-format 'checking-comments-done)))
854
855
    
    
856
857
858
859
    ;;
    ;; Confirm multiple recipients
    ;;
    
David Byers's avatar
David Byers committed
860
861
862
863

    (if (and (lyskom-default-value 'kom-confirm-multiple-recipients)
             (not (eq (lyskom-default-value 'kom-confirm-multiple-recipients)
                      'before))
David Byers's avatar
David Byers committed
864
865
             (> (- num-real-recpt num-me) 1))

866
867
868
869
        (save-excursion
          (goto-char (point-min))
          (if (not 
               (lyskom-j-or-n-p
870
                (lyskom-format 'comment-all-relevant-p)))
871
872
873
874
              (signal 'lyskom-edit-text-abort 
                      (list "%s" 
                            (lyskom-get-string 
                             'please-edit-recipients))))))
875

876
877
878
879
    ;;
    ;; Check that the authors of all commented texts get to see the new text
    ;;

David Byers's avatar
David Byers committed
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
    (when (and (lyskom-default-value 'kom-check-commented-author-membership)
               (assq 'COMM-TO (cdr misc-list)))
      (lyskom-message "%s" (lyskom-get-string 'checking-rcpt))

      (let ((raw-author-list (make-collector))
            (author-list nil)
            (authors-to-ask-about nil)
            (recipient-list (let ((result nil))
                              (lyskom-traverse misc (cdr misc-list)
                                (when (memq (car misc) 
                                            lyskom-recpt-types-list)
                                  (setq result (cons (cdr misc) result))))
                              (nreverse result))))
        ;;
        ;; Collect conf-stats of all authors to check. Since there
        ;; could be several, do it in a non-blocking way.
        ;;

        (lyskom-traverse text-no comm-to-list
          (initiate-get-text-stat 
           'sending
           (lambda (text-stat collector)
             (when text-stat
               (initiate-get-conf-stat 'sending
                                       'collector-push
                                       (text-stat->author text-stat)
                                       collector)))
           text-no
           raw-author-list))
        (lyskom-wait-queue 'sending)
David Byers's avatar
David Byers committed
910
911
912
        (setq raw-author-list (lyskom-delete-duplicates
                               (collector->value raw-author-list)
                               'conf-stat->conf-no))
David Byers's avatar
David Byers committed
913
914

        ;;
David Byers's avatar
David Byers committed
915
        ;; Filter the list. Remove all authors that are direct recipients
David Byers's avatar
David Byers committed
916
917
918
919
920
921
922
923
        ;; or whose send-comments-to is a direct recipient or who are
        ;; listed in kom-dont-check-commented-authors
        ;;

        (lyskom-traverse author raw-author-list
          (let ((send-comments-to
                 (car (lyskom-get-aux-item (conf-stat->aux-items author) 33))))

David Byers's avatar
David Byers committed
924
925
926
927
928
929
930
931
932
933
934
935
936
937
            (when send-comments-to
              (cond ((string-match "^\\([0-9]+\\)\\s-+\\([0-9]+\\)" (aux-item->data send-comments-to))
                     (setq send-comments-to 
                           (cons (string-to-number
                                  (match-string 1 (aux-item->data send-comments-to)))
                                 (string-to-number
                                  (match-string 2 (aux-item->data send-comments-to))))))
                    ((string-match "^\\([0-9]+\\)"
                                   (aux-item->data send-comments-to))
                     (setq send-comments-to
                           (cons (string-to-number
                                  (match-string 1 (aux-item->data send-comments-to)))
                                 0)))
                    (t (setq send-comments-to nil))))
David Byers's avatar
David Byers committed
938
939
940
941
942
943
944
945
946
947
948
949
950
951

            (cond

             ;; Author is in kom-dont-check-commented-authors

             ((memq (conf-stat->conf-no author) 
                    kom-dont-check-commented-authors))

             ;; Author is direct recipient to text

             ((memq (conf-stat->conf-no author) recipient-list))

             ;; Author has a zero send-comments-to

David Byers's avatar
David Byers committed
952
             ((and send-comments-to (zerop (car send-comments-to))))
David Byers's avatar
David Byers committed
953
954
955
956
957
958
959

             ;; We don't have permission to send stuff to the author's
             ;; send-comments-to or to the author if there is no
             ;; send-comments-to

             ((or (and send-comments-to
                       (not (lyskom-is-permitted-author
David Byers's avatar
David Byers committed
960
                             (blocking-do 'get-conf-stat (car send-comments-to)))))
David Byers's avatar
David Byers committed
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
                  (and (not send-comments-to)
                       (not (lyskom-is-permitted-author author)))))

             ;; We can't filter the author straight off, so we add it
             ;; to the list that we want to check completely.

             (t (setq author-list (cons author author-list))))))

        ;;
        ;; Now author-list contains a list of authors that we need to
        ;; check memberships of. Traverse all authors and get their
        ;; memberships in all recipients.

        (lyskom-traverse author author-list
          (let ((memberships (make-collector)))
            (lyskom-traverse recipient recipient-list
              (initiate-query-read-texts 'sending
                                         (lambda (x y)
                                           (collector-push x y))
                                         (conf-stat->conf-no author)
                                         recipient
                                         memberships))
            (lyskom-wait-queue 'sending)
            (setq memberships (collector->value memberships))

            ;;
            ;; Now memberships contains the memberships of author
            ;; in all recipients of the text. Traverse all memberships
            ;; in the collector and see if there is one that is not
            ;; an active membership. If there is not, then we need to
            ;; ask the user whether they want to add the author.
            ;;

            (unless (lyskom-traverse membership memberships
                      (when (and membership
                                 (not (membership-type->passive
                                       (membership->type membership))))
                        (lyskom-traverse-break t)))
David Byers's avatar
David Byers committed
999
              (setq authors-to-ask-about (cons author authors-to-ask-about)))))
David Byers's avatar
David Byers committed
1000

David Byers's avatar
David Byers committed
1001
1002
1003
1004
1005
1006
1007
1008
        ;;
        ;; Now authors-to-ask-about contains all authors that we
        ;; want to ask about. So do that.
        ;;

        (lyskom-traverse author authors-to-ask-about
          (let ((send-comments-to
                 (car (lyskom-get-aux-item (conf-stat->aux-items author) 33))))
David Byers's avatar
David Byers committed
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
            (when send-comments-to
              (cond ((string-match "^\\([0-9]+\\)\\s-+\\([0-9]+\\)" (aux-item->data send-comments-to))
                     (setq send-comments-to 
                           (cons (string-to-number
                                  (match-string 1 (aux-item->data send-comments-to)))
                                 (string-to-number
                                  (match-string 2 (aux-item->data send-comments-to))))))
                    ((string-match "^\\([0-9]+\\)"
                                   (aux-item->data send-comments-to))
                     (setq send-comments-to
                           (cons (string-to-number
                                  (match-string 1 (aux-item->data send-comments-to)))
                                 0)))
                    (t (setq send-comments-to nil))))
David Byers's avatar
David Byers committed
1023

David Byers's avatar
David Byers committed
1024
1025
1026
            (when (lyskom-j-or-n-p 
                   (lyskom-format 'add-recipient-p 
                                  author
David Byers's avatar
David Byers committed
1027
                                  (car send-comments-to)))
David Byers's avatar
David Byers committed
1028
              (setq extra-headers
David Byers's avatar
David Byers committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
                    (nconc
                     (list
                      (cond ((and send-comments-to
                                  (eq (cdr send-comments-to) 0)) 'RECPT)
                            ((and send-comments-to
                                  (eq (cdr send-comments-to) 1)) 'CC-RECPT)
                            ((and send-comments-to
                                  (eq (cdr send-comments-to) 15)) 'BCC-RECPT)
                            (t (if (lyskom-j-or-n-p
                                    (lyskom-format 'really-add-as-recpt-q author))
                                   'RECPT
                                 'CC-RECPT)))
                            (conf-stat->conf-no author))
                     extra-headers)))))))
1043
    extra-headers))
1044
    
David Byers's avatar
David Byers committed
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
(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")
1058
1059
              (if (and (not (lyskom-string= (buffer-substring (point)
                                                              (point-max)) message))
David Byers's avatar
David Byers committed
1060
1061
1062
                       (save-excursion
                         (set-buffer lyskom-buffer)
                         (lyskom-j-or-n-p 
1063
                          (lyskom-get-string 'send-formatted))))
David Byers's avatar
David Byers committed
1064
1065
1066
1067
1068
1069
1070
1071
1072
                  (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
1073
1074
1075
1076
1077
1078
1079
1080
1081


(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
1082
1083
1084
1085
    (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
1086
1087
1088
1089
1090
1091
1092
1093
1094
  (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))


1095
1096
1097
1098
1099
1100
1101
(defun kom-edit-insert-commented (arg)
  "Insert the commented text. Unless an empty prefix argument is
given, prepend each line with your commenting prefix (or '>')."
  (interactive "P")
  (lyskom-edit-get-commented
   'lyskom-edit-insert-commented
   (list (cond ((and arg (listp arg)) "")
1102
	       (t nil)))))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1103
1104


1105
1106
1107
1108
1109
1110
(defun kom-edit-insert-buglist ()
  "Insert the commented buglist, Roxen Internet Software style."
  (interactive)
  (lyskom-edit-get-commented 'lyskom-edit-insert-buglist))


Per Cederqvist's avatar
.  
Per Cederqvist committed
1111
1112
(defun kom-edit-insert-digit-text ()
  (interactive)
1113
  (setq unread-command-events (cons last-command-event unread-command-events))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1114
1115
1116
1117
  (call-interactively 'kom-edit-insert-text nil))


(defun kom-edit-insert-text (no)
1118
  "Insert the text number NO with '>' first on each line."
Linus Tolke's avatar
Linus Tolke committed
1119
1120
1121
1122
  (interactive (list
		(cond
		 ((null current-prefix-arg)
		  (string-to-int 
1123
		   (lyskom-read-from-minibuffer 
Linus Tolke's avatar
Linus Tolke committed
1124
1125
		    (format "%s" (lyskom-get-string 'which-text-include)))))
		 ((prefix-numeric-value current-prefix-arg)))))
David Byers's avatar
David Byers committed
1126
1127
  (let ((buffer (current-buffer))
        (window (selected-window)))
David Byers's avatar
David Byers committed
1128
    (set-buffer lyskom-buffer)
1129
    (lyskom-collect 'edit)
1130
1131
    (initiate-get-text 'edit nil no)
    (initiate-get-text-stat 'edit nil no)
1132
    (lyskom-use 'edit 'lyskom-edit-insert-commented buffer window)
Per Cederqvist's avatar
.  
Per Cederqvist committed
1133
1134
1135
    (set-buffer buffer)
    (sit-for 0)))

1136
1137
1138
1139
1140

(defun lyskom-edit-get-commented (thendo &optional arg-list)
  "Get the commented text and text stat and then do THENDO with it.
This function is called with an argument list TEXT TEXT-STAT BUFFER
WINDOW plus any optional arguments given in ARG-LIST."
Per Cederqvist's avatar
.  
Per Cederqvist committed
1141
1142
1143
  (let ((p (point)))
    (save-excursion
      (let* ((buffer (current-buffer))
David Byers's avatar
David Byers committed
1144
             (window (selected-window))
1145
             (headers (condition-case nil
1146
                          (elt (lyskom-edit-parse-headers) 1)
1147
1148
1149
                        (lyskom-edit-error nil))) ; Ignore these errors
             (no nil))
        (while headers
David Byers's avatar
David Byers committed
1150
          (if (memq (car headers) '(COMM-TO FOOTN-TO))
1151
1152
1153
1154
1155
1156
              (setq no (car (cdr headers))
                    headers nil)
            (setq headers (cdr (cdr headers)))))
        (cond
         (no
          (goto-char p)
David Byers's avatar
David Byers committed
1157
          (set-buffer lyskom-buffer)
1158
1159
1160
          (blocking-do-multiple ((text (get-text no))
                                 (text-stat (get-text-stat no)))
            (set-buffer buffer)
1161
            (apply thendo text text-stat buffer window arg-list)))
1162
1163
1164
         (t
          (lyskom-message "%s" (lyskom-get-string 'no-such-text-m))))))
    (sit-for 0)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1165
1166


1167
1168
1169
;;; ============================================================
;;; Info node stuff

1170
1171
1172
(defvar Info-current-node)
(defvar Info-current-file)

David Byers's avatar
David Byers committed
1173
;; NOTUSED: kom-yank-info-nodename
1174
1175
1176
1177
1178
1179
1180
1181
(defun kom-yank-info-nodename ()
  "Put the current Info-node on the kill-ring."
  (interactive)
  (kill-new (format "*Note %s: (%s)%s,"
		    Info-current-node
		    (file-name-nondirectory Info-current-file)
		    Info-current-node)))

David Byers's avatar
David Byers committed
1182
1183

;; NOTUSED: kom-insert-last-info-nodename
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
(defun kom-insert-last-info-nodename ()
  "Insert a reference to the most recently visited info node."
  (interactive)
  (condition-case nil
      (let ((link nil))
        (save-excursion
          (set-buffer (get-buffer "*info*"))
          (setq link (format "*Note %s: (%s)%s,"
                             Info-current-node
                             (file-name-nondirectory Info-current-file)
                             Info-current-node)))
        (insert link))
1196
    (error (lyskom-message "%s" (lyskom-get-string 'cant-find-info-node)))))
1197

Per Cederqvist's avatar
.  
Per Cederqvist committed
1198
1199
1200
1201
1202
1203
;;; ================================================================
;;;	  Add recipient, copy-recipient - Addera mottagare 
;;;
;;;  Author: Anders Gertz
;;;  Changed by: Linus Tolke

David Byers's avatar
David Byers committed
1204
1205
1206
1207
1208
1209
1210
(defun kom-edit-add-comment ()
  "Adds a text as commented to the text being edited."
  (interactive)
  (let* ((edit-buffer (current-buffer))
         (insert-at (point-min-marker))
         (text-no (lyskom-read-number (lyskom-get-string 'text-to-comment-q)))
         (text-stat (blocking-do 'get-text-stat text-no)))
1211
    (lyskom-ignore edit-buffer)
David Byers's avatar
David Byers committed
1212
1213
1214
1215
1216
1217
1218
1219
1220
    (lyskom-save-excursion
     (if text-stat
         (lyskom-edit-get-commented-author 
          (blocking-do 'get-text-stat text-no)
          (lyskom-get-string 'comment)
          insert-at text-no)
       (lyskom-error "%s" (lyskom-get-string 'no-such-text-m))))))


Per Cederqvist's avatar
.  
Per Cederqvist committed
1221
1222
1223
1224
(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)
David Byers's avatar
David Byers committed
1225
                                 nil
David Byers's avatar
David Byers committed
1226
                                  'RECPT))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1227
1228


David Byers's avatar
David Byers committed
1229
1230
1231
1232
(defun kom-edit-add-bcc ()
  "Adds a conference as bcc recipient to the text being edited."
  (interactive)
  (lyskom-edit-add-recipient/copy (lyskom-get-string 'added-blank-carbon-copy)
David Byers's avatar
David Byers committed
1233
                                  nil
David Byers's avatar
David Byers committed
1234
                                  'BCC-RECPT))
David Byers's avatar
David Byers committed
1235
1236


Per Cederqvist's avatar
.  
Per Cederqvist committed
1237
1238
1239
1240
(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)
David Byers's avatar
David Byers committed
1241
                                  nil
David Byers's avatar
David Byers committed
1242
                                  'CC-RECPT))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1243

David Byers's avatar
David Byers committed
1244
1245
1246
1247
1248
1249
(defun kom-edit-move-text ()
  "Adds a conference as a recipient, and changes all other recipients to
CC recipients."
  (interactive)
  (lyskom-edit-add-recipient/copy (lyskom-get-string 'who-to-move-to-q)
                                  'lyskom-edit-move-recipients))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1250

David Byers's avatar
David Byers committed
1251

David Byers's avatar
David Byers committed
1252
1253
1254
1255
1256
(defun lyskom-edit-move-recipients (conf-stat insert-at edit-buffer)
  (save-excursion
    (set-buffer edit-buffer)
    (let* ((tmp (lyskom-edit-parse-headers))
           (subject (car tmp))
David Byers's avatar
David Byers committed
1257
1258
           (miscs (mapcar (lambda (x) (if (eq (car x) 'RECPT) 
                                          (cons 'CC-RECPT (cdr x)) x))
David Byers's avatar
David Byers committed
1259
1260
1261
1262
1263
1264
1265
1266
                  (cdr (lyskom-edit-translate-headers (elt tmp 1)))))
           (aux-list (elt tmp 2))
           (elem nil))


      ;; If the new target is already a recipient, convert it to the right 
      ;; kind. Otherwise insert the new target after the last comm-to

David Byers's avatar
David Byers committed
1267
      (setq elem (lyskom-edit-find-misc miscs lyskom-recpt-types-list 
David Byers's avatar
David Byers committed
1268
1269
                                        (conf-stat->conf-no conf-stat)))
      (if elem
David Byers's avatar
David Byers committed
1270
          (setcar elem 'RECPT)
David Byers's avatar
David Byers committed
1271
        (lyskom-insert-in-list
David Byers's avatar
David Byers committed
1272
         (cons 'RECPT (conf-stat->conf-no conf-stat))
David Byers's avatar
David Byers committed
1273
         miscs
David Byers's avatar
David Byers committed
1274
         (car (cdr (memq (lyskom-edit-find-misc miscs '(FOOTN-TO COMM-TO) 
David Byers's avatar
David Byers committed
1275
1276
1277
1278
                                                nil t)
                         miscs)))))

      (lyskom-edit-replace-headers subject (cons 'MISC-LIST miscs) aux-list))))
David Byers's avatar
David Byers committed
1279
1280
1281
                                  


David Byers's avatar
David Byers committed
1282
(defun lyskom-edit-do-add-recipient/copy (recpt-type recpt-no edit-buffer)
1283
  (save-excursion
David Byers's avatar
David Byers committed
1284
1285
1286
    (set-buffer edit-buffer)
    (let* ((headers (lyskom-edit-parse-headers))
           (miscs (lyskom-edit-translate-headers (elt headers 1)))
David Byers's avatar
David Byers committed
1287
           (elem (lyskom-edit-find-misc miscs lyskom-recpt-types-list
David Byers's avatar
David Byers committed
1288
1289
1290
1291
1292
1293
1294
1295
                                        recpt-no)))

      (cond (elem (setcar elem recpt-type))
            (t (setq miscs
                     (append miscs (list (cons recpt-type recpt-no))))))
      (lyskom-edit-replace-headers (elt headers 0)
                                   miscs
                                   (elt headers 2)))))
David Byers's avatar
David Byers committed
1296
1297


David Byers's avatar
David Byers committed
1298
1299
1300
1301
1302
1303
(defun lyskom-edit-add-recipient/copy (prompt 
                                       &optional what-to-do recpt-type)
  "Adds a new recipient or a cc-recipient to the text which is being edited.
PROMPT is the prompt to use to ask the user for a recipient.
WHAT-TO-DO is a function to call to do the insertion.
RECPT-TYPE is the type of recipient to add."
1304
  (let ((edit-buffer (current-buffer))
1305
	(insert-at (point-min-marker))
1306
	(conf-stat (lyskom-read-conf-stat prompt '(all) nil "" t)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1307
    (lyskom-save-excursion
David Byers's avatar
David Byers committed
1308
     (save-excursion
David Byers's avatar
David Byers committed
1309
     (set-buffer lyskom-buffer)
1310
1311
     ;; +++ The information about msg-of-day might be old. We should
     ;; make sure it is up-to-date.
David Byers's avatar
David Byers committed
1312

1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
     (let ((text-no (conf-stat->msg-of-day conf-stat))
           (win-config nil)
           (text nil)
           (text-stat nil)
           (collector (make-collector)))

       (unless (zerop text-no)
         (initiate-get-text 'edit 'collector-push text-no collector)
         (initiate-get-text-stat 'edit 'collector-push text-no collector)
         (lyskom-wait-queue 'edit)
         (setq text-stat (elt (collector->value collector) 0))
         (setq text (elt (collector->value collector) 1)))

       (when (or (null text)
                 (null text-stat)
                 (null (get-buffer-window edit-buffer))
                 (progn (setq win-config (current-window-configuration))
                        (with-output-to-temp-buffer "*Motd*"
                          (lyskom-princ
                           (lyskom-format 'conf-has-motd-no
                                          (text->text-no text)
                                          (text->decoded-text-mass text 
                                                                   text-stat))))
                        (j-or-n-p (lyskom-get-string 'still-want-to-add))))

1338
         (when (and kom-confirm-add-recipients
David Byers's avatar
David Byers committed
1339
                    (eq recpt-type 'RECPT)
1340
1341
                    (not (lyskom-j-or-n-p (lyskom-format
                                           'really-add-as-recpt-q
1342
                                           conf-stat))))
David Byers's avatar
David Byers committed
1343
           (setq recpt-type 'CC-RECPT))
1344

David Byers's avatar
David Byers committed
1345
1346
1347
1348
1349
         (if what-to-do
           (funcall what-to-do conf-stat insert-at edit-buffer)
           (lyskom-edit-do-add-recipient/copy recpt-type
                                              (conf-stat->conf-no conf-stat)
                                              edit-buffer)))
1350
       (when win-config (set-window-configuration win-config)))))))
Per Cederqvist's avatar
.  
Per Cederqvist committed
1351

1352
1353
1354
1355
1356
1357
(defun lyskom-edit-sub-recipient/copy (recpt-no edit-buffer)
  "Remove the recipient having RECPT-NO from EDIT-BUFFER"
  (save-excursion
    (set-buffer edit-buffer)
    (let* ((headers (lyskom-edit-parse-headers))
           (miscs (lyskom-edit-translate-headers (elt headers 1)))
David Byers's avatar
David Byers committed
1358
           (elem (lyskom-edit-find-misc miscs lyskom-recpt-types-list