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

Per Cederqvist's avatar
.    
Per Cederqvist committed
46
47
48
(defvar lyskom-edit-mode-name "LysKOM edit"
  "Name of the mode.")

Linus Tolke Y's avatar
Linus Tolke Y committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
(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.")

Per Cederqvist's avatar
.    
Per Cederqvist committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(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)
  (lyskom-end-of-command)
77
78
79
80
81
82
83
  (lyskom-dispatch-edit-text proc misc-list subject body
			     handler data))


(defun lyskom-dispatch-edit-text (proc misc-list subject body
				       &optional handler &rest data)
  "Same as lyskom-edit-text except that it doesn't call lyskom-end-of-command."
84
85
  (let ((buf (process-buffer proc))
	(buffer (generate-new-buffer
Per Cederqvist's avatar
.    
Per Cederqvist committed
86
87
		 (concat (buffer-name (process-buffer proc)) "-edit")))
	(config (current-window-configuration)))
88
89
    (setq lyskom-list-of-edit-buffers (cons buffer 
					    lyskom-list-of-edit-buffers))
Linus Tolke Y's avatar
Linus Tolke Y committed
90
    (condition-case emacs-18\.55
91
92
	(process-kill-without-query (get-buffer-process (current-buffer)) t)
      (error
93
       ;; You lose some...
94
95
       (message "Old emacs! Upgrade!")
       (process-kill-without-query (get-buffer-process (current-buffer)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
96
97
98
99
100
101
102
103
    (cond
     ((and (or (bufferp kom-write-texts-in-window)
	       (stringp kom-write-texts-in-window))
	   (get-buffer-window kom-write-texts-in-window))
      (select-window (get-buffer-window kom-write-texts-in-window))
      (switch-to-buffer buffer))
     ((eq kom-write-texts-in-window 'other)
      (switch-to-buffer-other-window buffer))
Linus Tolke Y's avatar
Linus Tolke Y committed
104
105
106
107
108
109
110
     ((and (eq kom-write-texts-in-window 'other-frame)
	   (not (eq (selected-frame) (next-frame))))
      (select-frame (next-frame)))
     ((eq kom-write-texts-in-window 'new-frame)
      (make-local-variable 'lyskom-is-dedicated-edit-window)
      (setq lyskom-is-dedicated-edit-window t)
      (switch-to-buffer-other-frame buffer))
Per Cederqvist's avatar
.    
Per Cederqvist committed
111
112
113
114
115
116
117
     (t
      (switch-to-buffer buffer)))
    (lyskom-edit-mode)
    (setq lyskom-proc proc)
    (make-local-variable 'lyskom-edit-handler)
    (make-local-variable 'lyskom-edit-handler-data)
    (make-local-variable 'lyskom-edit-return-to-configuration)
118
    (make-local-variable 'kom-buffer)
Per Cederqvist's avatar
.    
Per Cederqvist committed
119
120
121
    (setq lyskom-edit-handler handler)
    (setq lyskom-edit-handler-data data)
    (setq lyskom-edit-return-to-configuration config)
122
    (setq kom-buffer buf)
Per Cederqvist's avatar
.    
Per Cederqvist committed
123
    (lyskom-edit-insert-miscs misc-list subject body)
124
125
126
127
128
129
    (goto-char (point-min))
    (re-search-forward (concat "\\(" (regexp-quote lyskom-header-subject)
			       "\\|" (regexp-quote lyskom-swascii-header-subject)
			       "\\)")
		       (point-max)
		       'end)
130
131
    (if (not (looking-at "\\s-*$"))
        (goto-char (point-max)))
132
    (lyskom-message "%s" (lyskom-get-string 'press-C-c-C-c)))
133
134
  (set-buffer (process-buffer lyskom-proc))
  )
Per Cederqvist's avatar
.    
Per Cederqvist committed
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152


(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))
	(main-buffer (process-buffer lyskom-proc)))
    (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)
153
154
155
	  (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
156
	 ((eq key 'cc-recpt)
157
158
159
	  (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
160
	 ((eq key 'comm-to)
161
162
163
	  (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
164
	 ((eq key 'footn-to)
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
	  (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 
			  (if kom-emacs-knows-iso-8859-1
			      lyskom-header-separator
			    lyskom-swascii-header-separator)
			  body 
			  (if kom-emacs-knows-iso-8859-1
			      lyskom-header-subject
			    lyskom-swascii-header-subject))
	   where-put-misc)
    (set-buffer edit-buffer)
    (goto-char where-put-misc)
    ))
Per Cederqvist's avatar
.    
Per Cederqvist committed
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196


(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.
197
STREAM is the buffer or a marker telling the position.
Per Cederqvist's avatar
.    
Per Cederqvist committed
198
199
200
201
202
NUMBER is the number of the person. Used if the conf-stat is nil."
  (save-excursion
    (let ((buf (cond
		((bufferp stream) stream)
		((markerp stream) (marker-buffer stream)))))
203
204
205
206
207
208
209
210
211
212
213
214
      (set-buffer buf)
      (save-excursion
	(if (markerp stream) (goto-char stream))
	(princ (concat string
		       (format " <%d> " (if conf-stat
					    (conf-stat->conf-no conf-stat)
					  number))
		       (if conf-stat
			   (conf-stat->name conf-stat)
			 "")
		       "\n")
	       stream)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
215
216
217
218


(defun lyskom-edit-get-commented-author (text-stat string stream number)
  (if text-stat
219
220
221
      (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
222
223
224
225
226
227
228
229
230
231
    (lyskom-edit-insert-commented-author nil string stream number)))


(defun lyskom-edit-insert-commented-author (conf-stat string stream number)
  (princ (lyskom-format 'comment-to-by
			string number 
			(if conf-stat
			    (lyskom-format 'by (conf-stat->name conf-stat))
			  ""))
	 stream)
232
  )
Per Cederqvist's avatar
.    
Per Cederqvist committed
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
			  


(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."
  (let ((result (cons 'MISC-LIST nil)))
    (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
(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.")

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


Per Cederqvist's avatar
.    
Per Cederqvist committed
282
283
284
(defun lyskom-edit-mode ()
  "\\<lyskom-edit-mode-map>Mode for editing texts for LysKOM.
Commands:
Linus Tolke Y's avatar
Linus Tolke Y committed
285
286
287
288
289
290
291
292
293
294
295
296
\\[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.
\\[kom-edit-insert-text]   inserts the shown text, you tell the number.

Per Cederqvist's avatar
.    
Per Cederqvist committed
297
298
299
300
301
The mode is based on the mode in the lyskom-edit-mode-mode-hook or fundamental.
Entry to this mode runs lyskom-edit-mode-hook."
  (interactive)
  (lyskom-clear-vars)
  (setq mode-line-buffer-identification '("LysKOM (server: %b)"))
302
  (text-mode)
Per Cederqvist's avatar
.    
Per Cederqvist committed
303
  (run-hooks 'lyskom-edit-mode-mode-hook)
Linus Tolke Y's avatar
Linus Tolke Y committed
304
305
  (setq lyskom-edit-mode-mode-map (and (current-local-map)
				       (copy-keymap (current-local-map))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
306
307
  (setq major-mode 'lyskom-edit-mode)
  (setq mode-name lyskom-edit-mode-name)
308
  ;; (setq buffer-offer-save t)
309
  (use-local-map (overlay-map lyskom-edit-mode-mode-map
Per Cederqvist's avatar
.    
Per Cederqvist committed
310
311
312
313
314
			      lyskom-edit-mode-map))
  (auto-save-mode 1)
  (auto-fill-mode 1)
  (make-local-variable 'paragraph-start)
  (make-local-variable 'paragraph-separate)
315
  (make-local-variable 'lyskom-proc)
316
317
318
319
320
321
  (setq paragraph-start (concat "^" 
				(regexp-quote 
				 lyskom-header-separator)
				"$\\|^"
				(regexp-quote
				 lyskom-swascii-header-separator)
Per Cederqvist's avatar
.    
Per Cederqvist committed
322
				"$\\|" paragraph-start))
323
324
325
326
327
328
  (setq paragraph-separate (concat "^" 
				   (regexp-quote 
				    lyskom-header-separator)
				   "$\\|^"
				   (regexp-quote
				    lyskom-swascii-header-separator)
329
				   "$\\|" paragraph-separate))
Per Cederqvist's avatar
.    
Per Cederqvist committed
330
331
332
333
334
335
336
  (run-hooks 'lyskom-edit-mode-hook))


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

337
338
339
340
(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
341
342
343
344

(defun kom-edit-send ()
  "Send the text to the server."
  (interactive)
345
346
347
348
349
  (lyskom-edit-send 'initiate-create-text))

(defun lyskom-edit-send (send-function)
  "Send the text to the server by calling SEND-FUNCTION."
  (interactive)
Per Cederqvist's avatar
.    
Per Cederqvist committed
350
351
352
353
  (if (or (string= mode-name lyskom-edit-mode-name)
	  (j-or-n-p (lyskom-get-string 'already-sent)))
      (progn 
	(let ((buffer (current-buffer))
354
	      headers misc-list subject message)
Per Cederqvist's avatar
.    
Per Cederqvist committed
355
356

	  (save-excursion
357
358
359
	    (setq headers (lyskom-edit-parse-headers)
		  misc-list (apply 'lyskom-create-misc-list (cdr headers))
		  subject (car headers)))
360
	  (setq message (lyskom-edit-extract-text))
Per Cederqvist's avatar
.    
Per Cederqvist committed
361
362
363
	  (setq mode-name "LysKOM sending")
	  (save-excursion
	    (set-buffer (process-buffer lyskom-proc))
David Kågedal's avatar
David Kågedal committed
364
365
366
	    ;; 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))
Per Cederqvist's avatar
.    
Per Cederqvist committed
367
368
	    (setq lyskom-is-writing nil)
	    (lyskom-tell-internat 'kom-tell-send)
369
	    (funcall send-function 'sending 'lyskom-create-text-handler
Per Cederqvist's avatar
.    
Per Cederqvist committed
370
371
				  (concat subject "\n" message) misc-list
				  buffer)))
372
373
	(if kom-dont-restore-window-after-editing
	    (bury-buffer)
Linus Tolke Y's avatar
Linus Tolke Y committed
374
	  (save-excursion
375
376
	    (if (and (boundp 'lyskom-is-dedicated-edit-window)
		     lyskom-is-dedicated-edit-window)
Linus Tolke Y's avatar
Linus Tolke Y committed
377
378
379
		(condition-case error
		    (delete-frame)
		  (error))))
380
381
	  (set-window-configuration lyskom-edit-return-to-configuration)
	  (set-buffer (window-buffer (selected-window))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
382
383
384
385
386
387
388
	(goto-char (point-max)))))


(defun kom-edit-quit ()
  "Kill the text (if any) written so far and continue reading."
  (interactive)
  (let ((edit-buffer (current-buffer)))
Linus Tolke Y's avatar
Linus Tolke Y committed
389
390
391
392
    (if kom-dont-restore-window-after-editing
	(bury-buffer)
      ;; Select the old configuration.
      (save-excursion
393
394
	(if (and (boundp 'lyskom-is-dedicated-edit-window)
		 lyskom-is-dedicated-edit-window)
Linus Tolke Y's avatar
Linus Tolke Y committed
395
396
397
398
399
	    (condition-case error
		(delete-frame)
	      (error))))
      (set-window-configuration lyskom-edit-return-to-configuration)
      (set-buffer (window-buffer (selected-window))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
400
401
402
403
404
405
    (goto-char (point-max))
    (setq lyskom-is-writing nil)
    (lyskom-tell-internat 'kom-tell-regret)
    (lyskom-save-excursion
     (set-buffer edit-buffer)
     (delete-auto-save-file-if-necessary))
406
407
    (kill-buffer edit-buffer)
    (lyskom-count-down-edits))
Per Cederqvist's avatar
.    
Per Cederqvist committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  (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)
425
  (if (boundp 'unread-command-event)	;Special for lucid-emacs
426
427
      (setq unread-command-event last-command-event)
    (setq unread-command-char last-command-char))
Per Cederqvist's avatar
.    
Per Cederqvist committed
428
429
430
431
432
  (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
433
434
435
436
437
438
439
  (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
440
441
442
443
444
445
446
447
448
449
450
451
  (let ((buffer (current-buffer)))
    (set-buffer (process-buffer lyskom-proc))
    (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))
David Kågedal's avatar
David Kågedal committed
452
453
454
455
456
457
	     (headers (cdr (lyskom-edit-parse-headers)))
	     (no nil))
	(while headers
	  (if (or (eq (car headers) 'comm-to)
		  (eq (car headers) 'footn-to))
	      (setq no (car (cdr headers))
David Kågedal's avatar
David Kågedal committed
458
459
		    headers nil)
	    (setq headers (cdr (cdr headers)))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
460
461
462
463
464
465
466
467
	(cond
	 (no
	  (goto-char p)
	  (set-buffer (process-buffer lyskom-proc))
	  (initiate-get-text 'edit thendo no buffer)
	  (set-buffer buffer)
	  (sit-for 0))
	 (t
468
	  (lyskom-message "%s" (lyskom-get-string 'no-such-text-m))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494


;;; ================================================================
;;;	  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."
  (let ((marker (point-min-marker))
495
496
497
	(edit-buffer (current-buffer))
	(insert-at (point-min-marker))
	(conf-stat (lyskom-read-conf-stat prompt 'all nil "")))
Per Cederqvist's avatar
.    
Per Cederqvist committed
498
    (lyskom-save-excursion
499
     ;;(save-excursion
Per Cederqvist's avatar
.    
Per Cederqvist committed
500
     (set-buffer (process-buffer lyskom-proc))
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
     (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))
		      (lyskom-edit-insert-misc-conf conf-stat string insert-at nil))
		 (set-window-configuration win-config))
	     (lyskom-edit-insert-misc-conf conf-stat string insert-at nil))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
516
517
518
519
520
521
522
523



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


524
525
526
527
528
529
530
531
(defun lyskom-looking-at-header (header angled)
  "Check if point is at the beginning of a header of type HEADER.
Return the corresponding number (conf no etc.). If ANGLED is non-nil,
only match a number inside <>."
  (if (looking-at
       (concat (downcase (substring (lyskom-get-string header)
				    0 4))
	       (if angled
532
533
		   "[^0-9]*<\\([0-9]+\\)>"
		 "[^0-9]*\\([0-9]+\\)")))
534
535
536
537
538
539
540
541
542
543
      (string-to-int (buffer-substring (match-beginning 1)
				       (match-end 1)))
    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
544
  (goto-char (point-min))
545
546
547
548
549
550
551
552
553
554
555
556
557
  (let ((result (cons "" nil)))		; The car will be replaced by
					; the real subject
    (save-restriction
      ;; Narrow to headers
      (search-forward (if kom-emacs-knows-iso-8859-1
			  lyskom-header-separator
			lyskom-swascii-header-separator))
      (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
558
	  (cond
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
	   ((setq n (lyskom-looking-at-header 'recipient t))
	    (nconc result (list 'recpt n)))
	   ((setq n (lyskom-looking-at-header 'carbon-copy t))
	    (nconc result (list 'cc-recpt n)))
	   ((setq n (lyskom-looking-at-header 'comment nil))
	    (nconc result (list 'comm-to n)))
	   ((setq n (lyskom-looking-at-header 'footnote nil))
	    (nconc result (list 'footn-to n)))
	   ((looking-at (if kom-emacs-knows-iso-8859-1
			    lyskom-header-subject
			  lyskom-swascii-header-subject))
	    (setcar result (lyskom-edit-extract-subject)))))
	(forward-line 1)))
    result))

	    
		 

;;; OBSOLETE!!!
;;(defun lyskom-edit-read-misc-list ()
;;  "Read misc-list from buffer."
;;  ; +++ Should use lyskom-create-misc-list.
;;  (goto-char (point-min))
;;  (let ((result (cons 'MISC-LIST nil)))
;;    (while (and (< (point) (point-max))
;;		(not (or (equal (char-to-string 
;;				 (elt 
;;				  (if kom-emacs-knows-iso-8859-1
;;				      lyskom-header-subject
;;				    lyskom-swascii-header-subject)
;;				  0))
;;				(buffer-substring (point) (1+ (point))))
;;			 (equal (char-to-string 
;;				 (elt 
;;				  (if kom-emacs-knows-iso-8859-1
;;				      lyskom-swascii-header-subject
;;				    lyskom-header-subject)
;;				  0))
;;				(buffer-substring (point) (1+ (point)))))))
;;      (let ((char (string-to-char
;;		   (upcase (buffer-substring (point) (1+ (point)))))))
;;	(nconc 
;;	 result
;;	 (cons
;;	  (cond
;;	   ((eq char (elt (lyskom-get-string 'recipient) 0)) ;recpt
;;	    (re-search-forward "<\\([0-9]+\\)>")
;;	    (cons 'recpt (string-to-int (buffer-substring
;;					 (match-beginning 1)
;;					 (match-end 1)))))
;;	   ((eq char (elt (lyskom-get-string 'carbon-copy) 0)) ;cc-recpt
;;	    (re-search-forward "<\\([0-9]+\\)>")
;;	    (cons 'cc-recpt (string-to-int (buffer-substring
;;					    (match-beginning 1)
;;					    (match-end 1)))))
;;	   ((eq char (elt (lyskom-get-string 'comment) 0)) ;comm-to
;;	    (re-search-forward "\\([0-9]+\\)")
;;	    (cons 'comm-to (string-to-int (buffer-substring
;;					   (match-beginning 1)
;;					   (match-end 1)))))
;;	   ((eq char (elt (lyskom-get-string 'footnote) 0)) ;footn-to
;;	    (re-search-forward "\\([0-9]+\\)")
;;	    (cons 'footn-to (string-to-int (buffer-substring
;;					    (match-beginning 1)
;;					    (match-end 1)))))
;;	   (t 
;;	    (signal 'lyskom-internal-error 
;;		    (list "Unknown header line: "
;;			  (buffer-substring (point)
;;					    (progn 
;;					      (end-of-line)
;;					      (point)))))))
;;	  nil)))
;;      (beginning-of-line 2))
;;    result))	   
Per Cederqvist's avatar
.    
Per Cederqvist committed
634
635
636
637
638
639
640
641
642


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


643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
(defun lyskom-edit-extract-text ()
  "Get text as a string."
  (save-excursion
    (goto-char (point-min))
    (if (not (or (re-search-forward 
		  (regexp-quote 
		   (if kom-emacs-knows-iso-8859-1
		       lyskom-header-separator
		     lyskom-swascii-header-separator)) 
		  nil (point-max))
		 (re-search-forward 
		  (regexp-quote 
		   (if kom-emacs-knows-iso-8859-1
		       lyskom-swascii-header-separator
		     lyskom-header-separator)) 
		  nil (point-max))))
659
660
661
662
663
664
665
666
667
	(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
668
669
670
671
672
673
674
675
676

  
(defun lyskom-create-text-handler (text-no edit-buffer)
  "Handle an attempt to write a text."
  (lyskom-tell-server "")
  (message "")
  (cond
   ((null text-no)
    (lyskom-insert-before-prompt
Linus Tolke Y's avatar
Linus Tolke Y committed
677
678
     (lyskom-format 'could-not-create-text lyskom-errno
		    (lyskom-get-error-text lyskom-errno)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
679
    (beep)
680
    (lyskom-message "%s" (lyskom-format 'could-not-create-text lyskom-errno
Linus Tolke Y's avatar
Linus Tolke Y committed
681
				   (lyskom-get-error-text lyskom-errno)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
    (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
697
698
699
700
      (lyskom-run 'background 'lyskom-set-mode-line))
     (t
      (setq lyskom-dont-change-prompt nil)))
    
Per Cederqvist's avatar
.    
Per Cederqvist committed
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    (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.

      (set-buffer (process-buffer lyskom-proc))
      (if hnd
	  (apply hnd text-no dta)))
    
    ;; Kill the edit-buffer.

    (lyskom-save-excursion
     (set-buffer edit-buffer)
     (delete-auto-save-file-if-necessary))
725
726
    (kill-buffer edit-buffer)
    (lyskom-count-down-edits))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
727
728
729
730
731
732
733
734
735
736
737
738


(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)
	 (with-output-to-temp-buffer "*Commented*"
Linus Tolke Y's avatar
Linus Tolke Y committed
739
740
741
742
	   (princ
	    (if kom-emacs-knows-iso-8859-1
		(text->text-mass text)
	      (iso-8859-1-to-swascii (text->text-mass text))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762


(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
	(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 62)
	    (backward-char 2))))
763
    (lyskom-message "%s" (lyskom-get-string 'no-get-text))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
764
765


766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
(defun lyskom-count-down-edits ()
  "Counts down the number of edit sessions.
Returns non-nil if there are sessions left.

Can be called from any of the lyskom-associated buffers. At least the main
buffer and edit buffers."
  (save-excursion
    (let ((proc (or (get-buffer-process (current-buffer))
		    (and (boundp 'lyskom-proc)
			 (processp lyskom-proc)
			 lyskom-proc)
		    (signal 'lyskom-internal-error
			    "lyskom-count-down-edits called from "
			    (current-buffer)))))
      (set-buffer (process-buffer proc))
      (while (and lyskom-list-of-edit-buffers
		  (not (memq (car lyskom-list-of-edit-buffers) (buffer-list))))
	(setq lyskom-list-of-edit-buffers (cdr lyskom-list-of-edit-buffers)))
Linus Tolke Y's avatar
Linus Tolke Y committed
784
      (condition-case emacs-18\.55
785
786
787
788
789
790
791
	  (if lyskom-list-of-edit-buffers
	      (process-kill-without-query proc t)
	    (process-kill-without-query proc nil))
	(error
	 ;; You loose some if you only have emacs-18.55...
	 (message "Running emacs 18.55 or earlier? Please upgrade!")
	 (process-kill-without-query proc))))
792
793
794
    lyskom-list-of-edit-buffers))
      

Per Cederqvist's avatar
.    
Per Cederqvist committed
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
;;; ================================================================
;;;        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))
814
815
816
      (cond
       ((fboundp 'map-keymap)		;Special for lucid-emacs
	(map-keymap
Per Cederqvist's avatar
.    
Per Cederqvist committed
817
	 (function
818
	  (lambda (event function)
819
	    (define-key map (vector event) function)))
820
	 newmap))
Linus Tolke Y's avatar
Linus Tolke Y committed
821
822
823
824
825
       ((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)))
826
827
828
829
830
831
832
833
834
835
836
	  (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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
       ((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
853
      map))))