view-text.el 50.5 KB
Newer Older
1
;;;;; -*-coding: raw-text;-*-
Linus Tolke's avatar
Linus Tolke committed
2
3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
Linus Tolke's avatar
Linus Tolke committed
5
6
7
8
9
;;;;;
;;;;; 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 
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
35
;;; ================================================================
;;; ================================================================
;;;
;;; File: view-text.el
;;; 
;;; This file contains functions that have to do with putting
;;; a text into the lyskom buffer.
;;;


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


41
42
43
44
(defvar lyskom-view-text-text)
(defvar lyskom-view-text-text-stat)


45
46
(defun lyskom-view-text (text-no &optional mark-as-read
				 follow-comments conf-stat priority
47
48
				 build-review-tree filter-active
				 flat-review)
49
50
  "Display text number TEXT-NO.
Args: TEXT-NO &optional MARK-AS-READ FOLLOW-COMMENTS CONF-STAT 
51
PRIORITY BUILD-REVIEW-TREE FLAT-REVIEW.
Per Cederqvist's avatar
.    
Per Cederqvist committed
52
53
54
55
56
If MARK-AS-READ is non-nil the text will be marked as read.
If FOLLOW-COMMENTS is non-nil all comments and footnotes to this text will be
read before the next text. CONF-STAT must be the conference status of the
current conference, and PRIORITY the priority, if FOLLOW-COMMENTS is non-nil.
If BUILD-REVIEW-TREE is non-nil then it fixes a new entry in the 
57
lyskom-reading-list to read the comments to this.
58
59
If FLAT-REVIEW is non-nil this is a review caused by lyskom-view-commented or
kom-view-text viewing REVIEW or REVIEW-MARK text.
60
61

Note that this function must not be called asynchronously."
62

63
64
  (let ((inhibit-read-only t)
	(filter (and filter-active
David Byers's avatar
David Byers committed
65
66
67
                     (lyskom-filter-text-p text-no)))
        (start nil)
        (end nil)
David Byers's avatar
David Byers committed
68
69
        (todo nil)
        (lyskom-last-text-format-flags nil))
70
    (cond ((eq filter 'skip-text) (lyskom-filter-prompt text-no 'filter-text)
71
	   (setq todo 'next-text)
David Kågedal's avatar
David Kågedal committed
72
	   (lyskom-mark-as-read (blocking-do 'get-text-stat text-no))
73
74
75
	   'next-text)
	  ((eq filter 'skip-tree)
	   (lyskom-filter-prompt text-no 'filter-tree)
David Kågedal's avatar
David Kågedal committed
76
77
78
79
80
81

	   ;;(initiate-get-text-stat 'main 'lyskom-jump text-no t)
	   ;; Let's try something else:
	   (lyskom-jump (blocking-do 'get-text-stat text-no) t)
	   (lyskom-wait-queue 'main)

82
	   (setq todo 'next-text)
83
84
85
86
87
	   'next-text)
	  (t
	   (if (not (or (null filter) 
			(eq filter 'dontshow)))
	       (lyskom-message "%s" (lyskom-get-string 'invalid-filter-list)))
88
	   (if (eq filter 'dontshow)
89
	       (setq todo 'next-text))
90
91
	   (blocking-do-multiple ((text-stat (get-text-stat text-no))
				  (text (get-text text-no)))
92
93
             (setq lyskom-view-text-text text
                   lyskom-view-text-text-stat text-stat)
94
	     (if (and text-stat text)
95
		 (progn
96
                   (run-hooks 'lyskom-view-text-hook)
97
98
99
		   ;; Use a marker, because the buffer may lose data
		   ;; at the top if kom-max-buffer-size is set.
		   (setq start (point-max-marker))
100
101
		   (lyskom-format-insert "%#2@%[%#1n%] " text-stat (list 'lyskom-text-start
                                                                         (text-stat->text-no text-stat)))
102
103
104
105
106
107
108
                   (let ((mx-date (car (lyskom-get-aux-item (text-stat->aux-items text-stat) 21)))
                         (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)))
                         (mx-to (lyskom-get-aux-item (text-stat->aux-items text-stat) 19))
                         (mx-cc (lyskom-get-aux-item (text-stat->aux-items text-stat) 20))
                         (mx-filename (lyskom-get-aux-item (text-stat->aux-items text-stat) 10104))
                         (mx-sender (lyskom-get-aux-item (text-stat->aux-items text-stat) 10103))
David Byers's avatar
David Byers committed
109
			 (mx-headers (lyskom-get-aux-item (text-stat->aux-items text-stat) 24))
David Byers's avatar
David Byers committed
110
                         (mx-reply-to (lyskom-get-aux-item (text-stat->aux-items text-stat) 18)))
111
112
113
114
115
                         

                     ;; Insert date

                     (if mx-date
116
117
118
                         (let ((date (lyskom-mx-date-to-time mx-date)))
                           (if date
                             (progn
119
                               (lyskom-insert (lyskom-format-time 'date-and-time date))
120
121
122
123
124
                               (when (time->tzhr date)
                                 (lyskom-format-insert " %#1s%#2s " 
                                                       (time->tzhr date)
                                                       (time->tzmin date))))
                             (lyskom-format-insert (aux-item->data mx-date))))
125
126
127
128
                       (lyskom-insert
                        (lyskom-format-time
                         'date-and-time
                         (text-stat->creation-time text-stat))))
129

130
                   ;; Insert number of lines
131

132
133
134
135
136
137
138
139
140
		   (lyskom-insert 
		    (if (= 1 (text-stat->no-of-lines text-stat))
			(lyskom-get-string 'line)
		      (lyskom-format 'lines 
				     (let ((n (text-stat->no-of-lines
					       text-stat)))
				       (if (= n 0) ; Added to increase
					   2 ; compatibility with old KOM. /lw
					 n)))))
141
		   
142
143
144
145
146
147
148
149
150
151
152
153
154
                   ;; Insert the author

                   (lyskom-insert (or (lyskom-format-mx-author mx-from mx-author) ""))
                   (unless (or mx-from mx-author)
                     (lyskom-format-insert "%#1P" (text-stat->author text-stat)))

                   ;; Insert filtration prompt

		   (when (eq filter 'dontshow)
                     (lyskom-insert " ")
                     (lyskom-insert (lyskom-get-string 'filtered)))
                   (lyskom-insert "\n")

155
156
157
158
159
160
161
162
163
		   (setq end (point-max))

		   (if (and kom-text-properties
			    (null filter)
			    (not (lyskom-face-default-p 'kom-first-line-face)))
		       (add-text-properties
			start end '(face kom-first-line-face)))
		   (set-marker start nil)

164
165
                   ;; Insert sender

166
                   (when (and mx-sender kom-show-imported-envelope-sender)
167
168
169
170
171
172
173
174
                     (lyskom-format-insert 'envelope-sender 
                                           (aux-item->data (car mx-sender))))
                   (when mx-filename
                     (lyskom-format-insert 'attachment-filename
                                           (aux-item->data (car mx-filename))))

                   ;; Insert imported at

David Byers's avatar
David Byers committed
175
                   (cond ((and (or mx-from mx-author mx-sender mx-headers)
176
177
                               (text-stat->author text-stat)
                               kom-show-imported-importer)
178
                          (lyskom-format-insert 'text-imported-at-by
179
180
181
                                                (lyskom-format-time
                                                 'date-and-time
                                                 (text-stat->creation-time text-stat))
182
183
184
                                                (text-stat->author text-stat)))
                         (mx-from
                          (lyskom-format-insert 'text-imported-at
185
186
187
                                                (lyskom-format-time
                                                 'date-and-time
                                                 (text-stat->creation-time text-stat))))
188
                         (mx-date (lyskom-format-insert 'text-created-at
189
190
191
                                                        (lyskom-format-time
                                                         'date-and-time
                                                         (text-stat->creation-time text-stat)))))
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
                   (when kom-show-imported-external-recipients
                     (mapcar (lambda (el)
                               (lyskom-format-insert "%#1s: %#2s\n"
                                                     (lyskom-get-string 'mx-Recipient)
                                                     (aux-item->data el)))
                             mx-to)
                     (mapcar (lambda (el)
                               (lyskom-format-insert "%#1s: %#2s\n"
                                                     (lyskom-get-string 'mx-Extra-recipient)
                                                     (aux-item->data el)))
                             mx-cc)
                     (mapcar (lambda (el)
                               (lyskom-format-insert "%#1s: %#2s\n"
                                                     (lyskom-get-string 'mx-Extern-reply-to)
                                                     (aux-item->data el)))
                             mx-reply-to)
                     )
209

210
211
212


                   ;; Insert all recipients and other header lines.
213
214

		   (if (eq filter 'dontshow)
David Kågedal's avatar
David Kågedal committed
215
216
		       (lyskom-mark-as-read
			(blocking-do 'get-text-stat text-no))
217
218
219
220
		     (lyskom-traverse misc
			 (text-stat->misc-info-list text-stat)
		       (let ((type (misc-info->type misc)))
			 (cond
221
			  ((memq type '(RECPT BCC-RECPT CC-RECPT))
222
			   (lyskom-print-header-recpt 
David Kågedal's avatar
David Kågedal committed
223
			    (misc-info->recipient-no misc)
224
225
226
227
			    misc))
			  ((eq type 'COMM-IN)
			   (if kom-reading-puts-comments-in-pointers-last
			       nil
David Byers's avatar
David Byers committed
228
229
230
231
                             (lyskom-print-header-comm 
                              (misc-info->comm-in misc)
                              misc
                              text-stat)))
232
233
234
235
236
			  ((eq type 'FOOTN-IN)
			   (if kom-reading-puts-comments-in-pointers-last
			       nil
			     (lyskom-print-header-comm
			      (misc-info->footn-in misc)
237
238
			      misc
                              text-stat)))
239
240
			  ((eq type 'COMM-TO)
			   (lyskom-print-header-comm (misc-info->comm-to misc)
241
242
						     misc
                                                     text-stat))
243
244
			  ((eq type 'FOOTN-TO)
			   (lyskom-print-header-comm (misc-info->footn-to misc)
245
246
						     misc
                                                     text-stat))
247
			  )))
248

249
                     ;; Insert aux-items that go in the header.
250
251
252
253
254
255

                     (let ((text nil))
                       (lyskom-traverse-aux aux
                                            (text-stat->aux-items text-stat)
                         (when (and
                             (or (eq (lyskom-aux-item-definition-field aux
David Byers's avatar
David Byers committed
256
                                                                 'text-print-when)
257
258
                                     'header)
                              (and (eq (lyskom-aux-item-definition-field aux
David Byers's avatar
David Byers committed
259
                                                                  'text-print-when)
260
261
262
263
                                       'comment)
                               (not kom-reading-puts-comments-in-pointers-last)
                               ))
                             (setq text 
264
                                   (lyskom-aux-item-call 
265
                                    aux
David Byers's avatar
David Byers committed
266
                                    'text-print
267
268
269
270
271
272
                                    aux
                                    text-stat)))
                           (lyskom-insert text)
                           (lyskom-insert "\n"))))


273
274
                     ;; If the text is marked, insert line saying so.

275
276
277
		     (let ((num-marks (text-stat->no-of-marks text-stat))
			   (is-marked-by-me (cache-text-is-marked
					     (text-stat->text-no text-stat))))
278
279
280
281
		       (when (> num-marks 0)
			 (if is-marked-by-me
			     (if (= num-marks 1)
				 (lyskom-format-insert 'marked-by-you
282
						       (mark->symbolic-mark-type
283
284
285
							is-marked-by-me))
			       (lyskom-format-insert 'marked-by-you-and-others
						     (1- num-marks)
286
						     (mark->symbolic-mark-type
287
288
289
						      is-marked-by-me)))
			   (lyskom-format-insert 'marked-by-several num-marks))))

290
291
                     ;; Insert the text body.

292
		     (lyskom-print-text text-stat text
293
					mark-as-read text-no flat-review))
294
295
296

                   ;; Insert aux-items that go in the footer.

297
298
299
300
301
                   (let ((text nil))
                       (lyskom-traverse-aux aux
                                            (text-stat->aux-items text-stat)
                         (when (and
                             (or (eq (lyskom-aux-item-definition-field aux
David Byers's avatar
David Byers committed
302
                                                                 'text-print-when)
303
304
                                     'footer)
                              (and (eq (lyskom-aux-item-definition-field aux
David Byers's avatar
David Byers committed
305
                                                                  'text-print-when)
306
307
308
309
                                       'comment)
                                   kom-reading-puts-comments-in-pointers-last
                               ))
                             (setq text 
310
                                   (lyskom-aux-item-call 
311
                                    aux
David Byers's avatar
David Byers committed
312
                                    'text-print
313
314
315
316
317
                                    aux
                                    text-stat)))
                           (lyskom-insert text)
                           (lyskom-insert "\n"))))

318
319
320
321
322
323
                   ;; Insert fast replies.

                   (let ((author-fast-replies nil)
                         (other-fast-replies nil))
                     (lyskom-traverse-aux aux (text-stat->aux-items text-stat)
                      (when (eq (aux-item->tag aux) 2)
324
325
326
327
328
329
330
                        (if (or (eq lyskom-current-command 'kom-review-noconversion)
                                (not (eq (text-stat->author text-stat)
                                         (aux-item->creator aux))))
                            (setq other-fast-replies
                                  (cons aux other-fast-replies))
                          (setq author-fast-replies
                                (cons aux author-fast-replies)))))
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
                     (when author-fast-replies
                       (lyskom-insert
                        (concat
                         (lyskom-format 'author-fast-replies)
                         "\n"
                         (mapconcat (lambda (item)
                                      (concat
                                       (lyskom-format 'author-fast-reply-aux
                                                      (aux-item->data item)
                                                      (aux-item->creator item))
                                       (lyskom-aux-item-terminating-button
                                        item
                                        text-stat)
                                       "\n"))
                                    (nreverse author-fast-replies)
                                    ""))))
                     (when other-fast-replies
                       (lyskom-insert
                        (concat
                         (lyskom-format 'other-fast-replies)
                         "\n"
                         (mapconcat (lambda (item)
                                      (concat
                                       (lyskom-format 'other-fast-reply-aux
                                                      (aux-item->data item)
                                                      (aux-item->creator item))
                                       (lyskom-aux-item-terminating-button
                                        item
                                        text-stat)
                                       "\n"))
                                    (nreverse other-fast-replies)
                                    "")))))

                   ;; Insert footnote/comment references.
365

366
367
		   (if kom-reading-puts-comments-in-pointers-last
		       (lyskom-view-text-handle-saved-comments text-stat))
368
369
370

                   ;; Prefetch commented texts.

371
		   (if (or follow-comments
David Kågedal's avatar
David Kågedal committed
372
373
			   ;; Checking build-review-tree should not be
			   ;; necessary, really /davidk
374
375
			   build-review-tree)
		       ;; This shows footnotes also.
376
377
378
379
380
381
382
		       (progn
			 ;; this is a bit silly, as it prefetches all
			 ;; the texts many times.
			 (lyskom-prefetch-texttree text-no)
			 (lyskom-follow-comments text-stat
						 conf-stat mark-as-read
						 priority build-review-tree)))
383
384
                   (if (lyskom-text-p (cache-get-text text-no))
                       (cache-del-text text-no))
385
		   ))
386
387
388
	       (lyskom-format-insert 'no-such-text-no text-no))
             (let ((aux-items (text-stat->aux-items text-stat)))
               (while aux-items
389
390
391
                 (lyskom-aux-item-call (car aux-items)
                                       'read-action
                                       text-stat)
392
393
                 (setq aux-items (cdr aux-items))))
             )))
394
    todo))
395
396


397
398
399
400
401
402
403
404
405
406
407
408
409
410
(defun lyskom-text-is-mail-p (text-stat)
  "Ad-hoc test to determine whether a text is a mail by peeking at aux-items."
  (lyskom-match-aux-items
   (text-stat->aux-items text-stat)
   (lambda (aux-item)
     (let ((tag (aux-item->tag aux-item)))
       (or (and (> tag 15)	; 16:mx-author,	17:mx-from,	18:mx-reply-to,
		(< tag 22))	; 19:mx-to,	20:mx-cc,	21:mx-date,
	   (= 24 tag))))))	; 24:mx-headers

(defun lyskom-filter-signature-hook ()
  "Filter out the signature of imported mail messages. Most useful
when put in your `lyskom-view-text-hook'."
  (unless (eq 'kom-review-noconversion lyskom-current-command)
411
412
    (when (lyskom-text-is-mail-p lyskom-view-text-text-stat)
      (let* ((body (text->text-mass lyskom-view-text-text))
413
414
	     (sign (string-match "^-- $" body)))
	(when sign
415
	  (set-text->text-mass lyskom-view-text-text (substring body 0 sign))
416
417
418
419
420
	  (lyskom-signal-reformatted-text 'reformat-signature))))))

;(add-hook 'lyskom-view-text-hook 'lyskom-filter-signature-hook)


421
422
423
424


(defun lyskom-follow-comments (text-stat conf-stat 
					 mark-as-read priority review-tree)
Per Cederqvist's avatar
.    
Per Cederqvist committed
425
  "Arrange so that all comments and footnotes to this text will be read.
426
427
428
This will essentially fix the reading list and issue calls for the cache to be
filled.

429
Arguments are: TEXT-STAT CONF-STAT MARK-AS-READ PRIORITY REVIEW-TREE
Per Cederqvist's avatar
.    
Per Cederqvist committed
430
431
TEXT-STAT is the current text.
CONF-STAT the current conference
432
If MARK-AS-READ is non-nil the texts will be marked as read.
Per Cederqvist's avatar
.    
Per Cederqvist committed
433
434
435
PRIORITY the priority of the reading.
If REVIEW-TREE is non-nil then build an entry of type 'REVIEW-TREE in the 
lyskom-reading-list."
436
437
438
439
  ;; . Find footnotes and show them.
  ;; . Fix the reading-list
  ;; . Issue cache-filling initiate-calls for everything left comments.
  
David Byers's avatar
David Byers committed
440
441
442
  (let ((flist nil)
        (clist nil)
        (mx-attachments-in (lyskom-get-text-attachments text-stat)))
443
444
445
446
447
448
    (lyskom-traverse misc (text-stat->misc-info-list text-stat)
      (cond
       ((and (eq (misc-info->type misc) 'FOOTN-IN)
	     (> (misc-info->footn-in misc) (text-stat->text-no text-stat))
	     kom-show-footnotes-immediately)
	;; Show the footnote
David Kågedal's avatar
David Kågedal committed
449
450
	(lyskom-view-text (misc-info->footn-in misc)
			  mark-as-read t conf-stat priority review-tree)
451
452
	(lyskom-is-read (misc-info->footn-in misc)))
       ((eq (misc-info->type misc) 'FOOTN-IN)
David Kågedal's avatar
David Kågedal committed
453
	(setq flist (cons (misc-info->footn-in misc) flist)))
454
       ((eq (misc-info->type misc) 'COMM-IN)
David Byers's avatar
David Byers committed
455
        (setq clist (cons (misc-info->comm-in misc) clist)))))
David Kågedal's avatar
David Kågedal committed
456
457
458
459
460
    
    (let (comments footnotes)
      ;; Find the comments that we should read and enter them into the
      ;; read-list.
      (lyskom-traverse no clist
David Byers's avatar
David Byers committed
461
462
463
464
        (cond ((or review-tree
                   kom-follow-attachments
                   (not (memq no mx-attachments-in)))
               (let ((text-stat (blocking-do 'get-text-stat no)))
465
466
                 (if (and text-stat
                          (or review-tree
David Byers's avatar
David Byers committed
467
468
469
470
                              (not (lyskom-text-read-p text-stat))))
                     (setq comments (cons no comments)))))
              ((memq no mx-attachments-in)
               (lyskom-skip-attachments no mark-as-read))))
471
      (if comments
David Byers's avatar
David Byers committed
472
473
474
475
476
477
478
479
480
481
482
483
484
          (if review-tree
              (lyskom-review-enter-read-info
               (lyskom-create-read-info 'REVIEW-TREE
                                        conf-stat priority
                                        (lyskom-create-text-list comments)
                                        (text-stat->text-no text-stat)) t)
            (read-list-enter-read-info
             (lyskom-create-read-info 'COMM-IN
                                      conf-stat priority
                                      (lyskom-create-text-list comments)
                                      (text-stat->text-no text-stat))
             lyskom-reading-list t)))

David Kågedal's avatar
David Kågedal committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
      ;; Find the footnotes that we should read and enter them into
      ;; the read-list. A slight trick is to increase the priority so
      ;; that they will be read first.
      (lyskom-traverse no flist
	(let ((text-stat (blocking-do 'get-text-stat no)))
	  (if (or review-tree
		  (and text-stat
		       (not (lyskom-text-read-p text-stat))))
	      (setq footnotes (cons no footnotes)))))
      (if footnotes
	  (read-list-enter-read-info
	   (lyskom-create-read-info (if review-tree 'REVIEW-TREE 'FOOTN-IN)
				    conf-stat (1+ priority)
				    (lyskom-create-text-list footnotes)
				    (text-stat->text-no text-stat))
	   lyskom-reading-list t)))))
501

David Byers's avatar
David Byers committed
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
(defun lyskom-get-text-attachments (text-stat)
  "Return a list of all attachments to TEXT-STAT."
  (mapcar
   (lambda (el)
     (string-to-number (aux-item->data el)))
   (lyskom-get-aux-item (text-stat->aux-items text-stat) 10101)))

(defun lyskom-skip-attachments (text-no mark-as-read)
  "Skip the hierarchy of attachments starting at text-no"
  (let ((queue (list text-no))
        (done nil)
        (tmp (make-collector))
        (result nil))
    ;; Note: this does block the client, but explicitly checks that
    ;;       it is not being called from under the parser (where it
    ;;       really doesn't make a whole lot of sense anyway), so
    ;;       we ought to be OK anyway.
    (unless lyskom-is-parsing
      (while queue
        (while queue
          (when (not (memq (car queue) done))
            (initiate-get-text-stat 'background
                                    'collector-push
                                    (car queue) 
                                    tmp)
            (setq queue (cdr queue))
            (setq done (cons (car queue) done))))
        (lyskom-wait-queue 'background)

        (setq result (nconc result (collector->value tmp)))
        (lyskom-traverse text-stat (collector->value tmp)
          (setq queue (nconc queue (lyskom-get-text-attachments text-stat)))))

      (lyskom-traverse text-stat result
        (read-list-delete-text (text-stat->text-no text-stat) lyskom-reading-list)
        (read-list-delete-text (text-stat->text-no text-stat) lyskom-to-do-list)
David Byers's avatar
David Byers committed
538
539
540
        (when mark-as-read
          (lyskom-mark-as-read text-stat)
          (lyskom-is-read-handler text-stat))))))
David Byers's avatar
David Byers committed
541

542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562

(defun lyskom-fetch-text-for-cache (text-stat)
  "Fetches the author and other things of a text and does nothing with it."
  (cond
   (text-stat
    (lyskom-fetch-author-for-cache text-stat)
    (initiate-get-text 'fill-cache nil (text-stat->text-no text-stat))
    (lyskom-traverse misc (text-stat->misc-info-list text-stat)
      (cond
       ((eq (misc-info->type misc) 'FOOTN-IN)
	(initiate-get-text-stat 'fill-cache 'lyskom-fetch-author-for-cache
				(misc-info->footn-in misc)))
       ((eq (misc-info->type misc) 'FOOTN-TO)
	(initiate-get-text-stat 'fill-cache 'lyskom-fetch-author-for-cache
				(misc-info->footn-to misc)))
       ((eq (misc-info->type misc) 'COMM-IN)
	(initiate-get-text-stat 'fill-cache 'lyskom-fetch-author-for-cache
				(misc-info->comm-in misc)))
       ((eq (misc-info->type misc) 'COMM-TO)
	(initiate-get-text-stat 'fill-cache 'lyskom-fetch-author-for-cache
				(misc-info->comm-to misc))))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
563
564


565
566
567
568
569
570

(defun lyskom-fetch-author-for-cache (text-stat)
  "Fetches the author of a text and does nothing with it."
  (if text-stat				;Error check.
      (initiate-get-conf-stat 'fill-cache nil (text-stat->author text-stat))))

Per Cederqvist's avatar
.    
Per Cederqvist committed
571
572
573
574
575
576
577


(defun lyskom-text-read-p (text-stat)
  "Return t if TEXT-STAT has been marked as read in all
recipients to it that the user is a member in."
  (let* ((misc-info-list (text-stat->misc-info-list text-stat))
	 (i (length misc-info-list))
578
	 (res 'not-member))
Per Cederqvist's avatar
.    
Per Cederqvist committed
579
580
581
582
583
    (while (and res (not (zerop i)))
      (setq i (1- i))
      (let* ((misc-info (elt misc-info-list i))
	     (type (misc-info->type misc-info)))
	(cond
584
	 ((memq type '(RECPT BCC-RECPT CC-RECPT))
585
586
587
	  ;; Is this function ever called asynchronously? If not, we
	  ;; can use lyskom-get-membership istead.
	  (let ((membership (lyskom-try-get-membership
David Byers's avatar
David Byers committed
588
			     (misc-info->recipient-no misc-info) t))
Per Cederqvist's avatar
.    
Per Cederqvist committed
589
		(loc-no (misc-info->local-no misc-info)))
590
591
592
593
594

	    ;; Make a note that this text really is in a group we are
	    ;; a member of.
	    (if (and res membership) (setq res t))
	    
Per Cederqvist's avatar
.    
Per Cederqvist committed
595
596
597
	    (if (and membership
		     (> loc-no (membership->last-text-read membership))
		     (not (lyskom-vmemq loc-no
598
					(membership->read-texts membership))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
599
		(setq res nil)))))))
600
601
602
    (if (eq res 'not-member)
	(not kom-follow-comments-outside-membership)
      res)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
603
604


605
(defun lyskom-text-read-at-least-once-p (text-stat &optional bg)
David Byers's avatar
David Byers committed
606
  "Return t if TEXT-STAT has been marked as read in any of the recipients
607
608
609
610
the user is a member of. Uses blocking-do. Returns t if TEXT-STAT is nil.

If BG is non-nil, this function is run in the background and must not use
blocking-do."
David Byers's avatar
David Byers committed
611
612
613
614
615
616
617
618
619
620
621
  (if text-stat
      (let* ((misc-info-list (text-stat->misc-info-list text-stat))
             (misc-item nil)
             (type nil)
             (membership nil)
             (is-member nil)
             (result nil))
        (while misc-info-list
          (setq misc-item (car misc-info-list))
          (setq type (misc-info->type misc-item))
          (setq misc-info-list (cdr misc-info-list))
622
          (cond ((memq type '(RECPT BCC-RECPT CC-RECPT))
623
624
                 (setq membership (if bg
                                      (lyskom-try-get-membership
David Byers's avatar
David Byers committed
625
                                       (misc-info->recipient-no misc-item) t)
626
                                    (lyskom-get-membership
David Byers's avatar
David Byers committed
627
                                     (misc-info->recipient-no misc-item) t)))
David Byers's avatar
David Byers committed
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
                 (when membership
                   (setq is-member t)
                   (when (or (<= (misc-info->local-no misc-item)
                                 (membership->last-text-read membership))
                             (lyskom-vmemq (misc-info->local-no misc-item)
                                           (membership->read-texts
                                            membership)))
                     (setq result t)
                     (setq misc-info-list nil))))))
        (cond (result result)
              ((not is-member) (not kom-follow-comments-outside-membership))
              (t nil)))
    t))


643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
(defun lyskom-subtract-one-day (x)
  (let ((high-x (1- (car x)))
        (low-x (car (cdr x))))
    (if (> 20864 low-x)
        (setq high-x (1- high-x)
              low-x (+ low-x 65536)))
    (setq low-x (- low-x 20864))
    (list high-x low-x nil)))

(defun lyskom-calculate-day-diff (time)
  (let* ((now (current-time))
         (yesterday (lyskom-subtract-one-day now))
         (decnow (decode-time now))
         (decthen (decode-time yesterday)))
    (cond ((and (= (time->mday time) (elt decnow 3))
658
659
                (= (time->mon time)  (elt decnow 4))
                (= (time->year time) (elt decnow 5)))
660
661
           'today)
          ((and (= (time->mday time) (elt decthen 3))
662
663
                (= (time->mon time)  (elt decthen 4))
                (= (time->year time) (elt decthen 5)))
664
665
666
           'yesterday)
          (t nil))))

Per Cederqvist's avatar
.    
Per Cederqvist committed
667

668
669
670
671
672
673
674
(defun lyskom-format-text-footer (text author author-name format format-flags)
  "Format the footer of a text."
  (let* ((result "")
         (start 0)
         (format-letter nil)
         (field-width nil)
         (kom-deferred-printing nil)
675
         (author-name-len nil)
676
677
         (have-author (and format (string-match "%=?[0-9]*P" format))))

David Byers's avatar
David Byers committed
678
    (setq author-name-len (lyskom-string-width (or author-name (conf-stat->name author))))
679

680
681
    ;; No user-specified format, so output dashes and stuff

682
    (if (null format)
683
684
685
686
687
        (let* ((format-flag-string
                (if (and format-flags (listp format-flags))
                    (let ((first-flag t))
                      (concat
                       "("
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
                       (mapconcat 
                        (lambda (str)
                          (let ((face nil))
                            ;; If it's a cons, the cdr is property list
                            (when (consp str)
                              (setq face (cdr str)
                                    str (car str)))
                            ;; Get the string
                            (setq str (lyskom-get-string str))
                            ;; Upcase the first in the list
                            (when first-flag 
                              (setq first-flag nil str (upcase-initials str)))
                            ;; If we have a plist, apply it
                            (when face
                              (add-text-properties 0 (length str) face str))
                            ;; Return the string
                            str))
                        format-flags ", ")
706
707
708
709
                       ")"))
                  ""))
               (end-dash-chars
                (- kom-text-footer-dash-length
David Byers's avatar
David Byers committed
710
                   (lyskom-string-width (int-to-string (text-stat->text-no text))) 
711
712
                   3                             ; Parens and space
                   (if kom-show-author-at-end
713
                       (+ author-name-len 2) 
714
                     0)
David Byers's avatar
David Byers committed
715
                   (string-width format-flag-string)))
716
717
718
719
720
721
722
723
724
725
726
727
728
               (end-dash (if (> end-dash-chars 0)
                             (make-string end-dash-chars ?-)
                           "")))
          (lyskom-format
           (cond ((and kom-dashed-lines kom-show-author-at-end)
                  "(%#1n) /%#2P/%#3s%#4s")
                 (kom-dashed-lines
                  "(%#1n) %#3s%#4s")
                 (kom-show-author-at-end
                  "(%#1n) /%#2P/")
                 (t
                  "(%#1n)"))
           text
729
           (or author-name author)
730
731
           end-dash
           format-flag-string))
732

733
734
      ;; User-specified format. Do all kinds of weird stuff

735
736
737
738
739
740
741
742
743
744
745
746
      (while (string-match "%\\(=?-?[0-9]+\\)?\\([-nPpf% ]\\)" format start)
        (setq result (concat result (substring format start 
                                               (match-beginning 0))))
        (setq format-letter (aref format (match-beginning 2)))
        (setq field-width (match-string 1 format))
        (when (null field-width) (setq field-width ""))
        (setq start (match-end 0))
        (setq result
              (concat 
               result
               (cond
                ((eq format-letter ?p)
David Byers's avatar
David Byers committed
747
748
                 (lyskom-format (format "%%%s#1p" field-width) 
                                (or author (text-stat->author text))))
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772

                ((eq format-letter ?P)
                 (lyskom-format (format "%%%s#1P" field-width) 
                                (or author author-name)))

                ((eq format-letter ?n)
                 (lyskom-format (format "%%%s#1n" field-width) 
                                (text-stat->text-no text)))

                ((eq format-letter ?f)
                 (if format-flags
                     (lyskom-format (format "%%%s#1s" field-width) 
                                    (lyskom-get-string format-flags))
                   ""))

                ((eq format-letter ?-)
                 (let ((width 
                        (cond ((null field-width) 42)
                              ((string= "" field-width) 42)
                              ((eq ?= (aref field-width 0))
                               (string-to-int (substring field-width 1)))
                              (t (string-to-int field-width)))))
                 
                   (lyskom-format 
773
                    "%#1s"
774
                    (if have-author
775
776
                        (if (< author-name-len width)
                            (make-string (- width author-name-len) ?-)
777
778
779
780
781
782
783
784
785
786
787
788
789
790
                          "")
                      (make-string width ?-)))))

                ((eq format-letter ?%)
                 "%")
                (t (concat "%" field-width (make-string 1 format-letter))))))
        )
      (setq result (concat result (substring format start)))
      (if (string-match "[ \t]+\\'" result)
          (substring result 0 (match-beginning 0))
        result))))



David Byers's avatar
David Byers committed
791
792
(defun lyskom-deferred-insert-footer (conf-stat defer-info)
  "Insert the name of a conference at a previously reserved place."
793
794
  (let* ((text-stat (elt (defer-info->data defer-info) 0))
         (format-flags (elt (defer-info->data defer-info) 1))
795
         (mx-from (car (lyskom-get-aux-item (text-stat->aux-items text-stat) 17)))
796
797
798
799
         (mx-author (car (lyskom-get-aux-item (text-stat->aux-items text-stat) 16)))
         (name (cond (conf-stat nil)
                      ((= (defer-info->call-par defer-info) 0)
                       (lyskom-get-string 'person-is-anonymous))
800
801
                      (t (lyskom-format 'person-does-not-exist
                                        (text-stat->author text-stat))))))
802
803
804

    (setq name (or (lyskom-format-mx-author mx-from mx-author) name))

805
806
807
808
809
810
    (lyskom-replace-deferred defer-info 
                             (lyskom-format-text-footer text-stat
                                                        conf-stat
                                                        name
                                                        kom-text-footer-format
                                                        format-flags))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
811

812

813
(defun lyskom-print-text (text-stat text mark-as-read text-no flat-review)
Per Cederqvist's avatar
.    
Per Cederqvist committed
814
815
816
  "Print a text. The header must already be printed.
Print an error message if TEXT-STAT or TEXT is nil.
Mark the text as read if (and only if) MARK-AS-READ is non-nil.
817
Args: TEXT-STAT TEXT MARK-AS-READ TEXT-NO FLAT-REVIEW."
818
819
  (let ((lyskom-current-function 'lyskom-print-text))
    (cond
Per Cederqvist's avatar
.    
Per Cederqvist committed
820
     ((or (null text)
821
          (null text-stat))
Per Cederqvist's avatar
.    
Per Cederqvist committed
822
823
824
      (lyskom-format-insert 'no-such-text-no text-no)
      (setq lyskom-previous-text lyskom-current-text)
      (setq lyskom-current-text text-no))
825
     (t
826
827
      (when (eq (text-stat->author text-stat) lyskom-pers-no)
        (setq lyskom-last-seen-written (text-stat->text-no text-stat)))
828
      (let* ((str (text->decoded-text-mass text text-stat))
829
	     (truncated nil)
830
831
             ;; s1 s2 t1 t2
	     body)
832

833
834
835
836
837
838
839
840
        (cond
         ((string-match "\n" str)
          (setq lyskom-current-subject (substring str 0 (match-beginning 0)))
          (setq body (substring str (match-end 0)))
          (lyskom-insert-string 'head-Subject)
          (let ((lyskom-current-function-phase 'subject))
            (lyskom-format-insert "%#1r\n" 
                                  (copy-sequence lyskom-current-subject)))
841
          (when kom-dashed-lines
842
              (lyskom-insert 
843
844
845
               (make-string kom-text-header-dash-length ?-)))

          (lyskom-insert "\n")
846
          ;; (setq t1 (point-max))
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864

	  ;; Truncate body if flat-review and long text
	  (if (and flat-review kom-truncate-show-lines kom-truncate-threshold)
	      (let ((lines 0)
		    (pos 0)
		    (show-lines (min kom-truncate-show-lines
				     kom-truncate-threshold))
		    last-line)
		(while (and pos (setq pos (string-match "\n" body pos)))
		  (setq pos (1+ pos))
		  (setq lines (1+ lines))
		  (if (= lines show-lines)
		      (setq last-line (1- pos)))
		  (if (>= lines kom-truncate-threshold)
		      (setq body (substring body 0 last-line)
			    pos nil
			    truncated t)))))
		    
865
          (let ((lyskom-current-function-phase 'body))
David Byers's avatar
David Byers committed
866
            (lyskom-format-insert "%#1t\n" (cons text-stat body)))
867
868
869

	  ;; Indicate that the text was truncated
	  (if truncated
870
871
872
              (lyskom-signal-reformatted-text
               '(reformat-truncated . (face kom-warning-face))))

873
	  
874
875
          ;; (setq t2 (point-max))
	  )
876
         (t                             ;No \n found. Don't print header.
877
878
879
880
          (when kom-dashed-lines
            (lyskom-insert 
             (make-string kom-text-header-dash-length ?-)))
          (lyskom-insert "\n")
David Byers's avatar
David Byers committed
881
          (lyskom-format-insert "%#1t\n" (cons text-stat str))
882
883
          (setq lyskom-current-subject "")))
        (sit-for 0)
884
885
886
887
888
889
890
891
        (let* ((lyskom-current-function-phase 'footer)
               (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)))
               (author-name (lyskom-format-mx-author mx-from mx-author)))

          (if (and kom-deferred-printing
                   (or kom-text-footer-format
                       kom-show-author-at-end))
892
893
894
895
896
897
898
899
900
901
902
903
904
905
              (progn
                (lyskom-format-insert "%#1s\n" lyskom-defer-indicator)
                (lyskom-defer-insertion
                 (lyskom-create-defer-info
                  'get-conf-stat
                  (text-stat->author text-stat)
                  'lyskom-deferred-insert-footer
                  (set-marker (make-marker)
                              (- (point-max) 
                                 (length lyskom-defer-indicator)
                                 1))
                  (length lyskom-defer-indicator)
                  "%#1s"
                  (list text-stat lyskom-last-text-format-flags))))
906
907
908
909
910
            (let* ((conf-stat (and (or kom-text-footer-format
                                       kom-show-author-at-end)
                                   (not (eq 0 (text-stat->author text-stat)))
                                   (blocking-do 'get-conf-stat 
                                                (text-stat->author text-stat)))))
911
912
913
              (lyskom-insert (lyskom-format-text-footer 
                              text-stat
                              conf-stat
914
915
916
917
918
919
                              (cond (author-name author-name)
                                    ((eq (text-stat->author text-stat) 0)
                                     (lyskom-get-string 'person-is-anonymous))
                                    (conf-stat (conf-stat->name conf-stat))
                                    (t (lyskom-format 'person-does-not-exist 
                                                      (text-stat->author text-stat))))
920
921
922
                              kom-text-footer-format
                              lyskom-last-text-format-flags)))
            (lyskom-insert "\n")))
923
924
925
926
        (if mark-as-read
            (lyskom-mark-as-read text-stat))
        (setq lyskom-previous-text lyskom-current-text)
        (setq lyskom-current-text (text-stat->text-no text-stat)))))))
Per Cederqvist's avatar
.    
Per Cederqvist committed
927
928
929


(defun lyskom-mark-as-read (text-stat)
Per Cederqvist's avatar
Per Cederqvist committed
930
  "Mark a text as read in all conferences that are recipients.
Per Cederqvist's avatar
Per Cederqvist committed
931
932
933
934
935
936
Tell the server that it is read.

Args: TEXT-STAT

This function does not remove the text from the internal structures of
the client. That is done by lyskom-is-read."
Per Cederqvist's avatar
.    
Per Cederqvist committed
937
938
939
  (let ((misc-info-list (text-stat->misc-info-list text-stat)))
    (lyskom-traverse
     misc-info misc-info-list
940
     (if (and (memq (misc-info->type misc-info) '(RECPT BCC-RECPT CC-RECPT))
941
942
943
944
	      ;; The whole membership list might not be fetched
	      ;; yet. So we better mark it as read in all conferences.
	      ;; (lyskom-member-p (misc-info->recipient-no misc-info))
	      )
945
946
	 (initiate-mark-as-read 'background 
                                'lyskom-mark-as-read-callback
Per Cederqvist's avatar
.    
Per Cederqvist committed
947
				(misc-info->recipient-no misc-info)
948
949
950
951
952
953
954
				(list (misc-info->local-no misc-info))
                                text-stat
                                (misc-info->recipient-no misc-info)
                                (list (misc-info->local-no misc-info)))))))

(defun lyskom-mark-as-read-callback (result text-stat recipient local-nos)
  (lp--maybe-update-unreads recipient))
Per Cederqvist's avatar
.    
Per Cederqvist committed
955
956


David Kågedal's avatar
David Kågedal committed
957
(defun lyskom-print-header-recpt (conf-no misc)
Per Cederqvist's avatar
.    
Per Cederqvist committed
958
  "Print a line of info about a recipient (or cc-recipient) of a text."
959
960
961
  (lyskom-format-insert "%#1s: %#2M <%#3d>\n"
			(cond ((eq (misc-info->type misc) 'RECPT)
			       (lyskom-get-string 'Recipient))
David Byers's avatar
David Byers committed
962
963
964
965
966
                              ((eq (misc-info->type misc) 'BCC-RECPT)
                               (lyskom-get-string 'Hidden-recipient))
                              ((eq (misc-info->type misc) 'CC-RECPT)
                               (lyskom-get-string 'Extra-recipient))
                              (t (lyskom-get-string 'Strange-recipient)))
David Kågedal's avatar
David Kågedal committed
967
			conf-no
968
			(misc-info->local-no misc))
Per Cederqvist's avatar
.    
Per Cederqvist committed
969
970
  (if (misc-info->sent-at misc)
      (lyskom-format-insert 'send-at
971
972
			    (lyskom-format-time
                             'date-and-time
Per Cederqvist's avatar
.    
Per Cederqvist committed
973
974
			     (misc-info->sent-at misc))))
  (if (misc-info->sender misc)
David Kågedal's avatar
David Kågedal committed
975
      (lyskom-format-insert 'sent-by (misc-info->sender misc)))
Per Cederqvist's avatar
.    
Per Cederqvist committed
976
  (if (misc-info->rec-time misc)
David Kågedal's avatar
David Kågedal committed
977
978
      (lyskom-format-insert
       'recieved-at
979
       (lyskom-format-time 'date-and-time (misc-info->rec-time misc)))))
David Kågedal's avatar
David Kågedal committed
980
981


Per Cederqvist's avatar
.    
Per Cederqvist committed
982
983


984
(defun lyskom-view-text-handle-saved-comments (text-stat)
Per Cederqvist's avatar
.    
Per Cederqvist committed
985
986
  "Ask server for what is needed to print the references to commenting texts 
and prints the references. Used at the bottom of the text.
987
988
989
990
991
992
Args: TEXT-STAT of the text being read."
  (lyskom-traverse misc
      (text-stat->misc-info-list text-stat)
    (let ((type (misc-info->type misc)))
      (cond
       ((eq type 'COMM-IN)
David Byers's avatar
David Byers committed
993
        (lyskom-print-header-comm (misc-info->comm-in misc) misc text-stat))
994
       ((eq type 'FOOTN-IN)
995
	(lyskom-print-header-comm (misc-info->footn-in misc) misc text-stat))))))
996
997


998

999
(defun lyskom-print-header-comm (text misc read-text-stat)
1000
  "Get author of TEXT-NO and print a header line."
1001
  (let ((text-stat (if kom-deferred-printing
1002
		       (cache-get-text-stat text)
1003
		     (blocking-do 'get-text-stat text))))
1004
1005

    ;; Print information about the link
1006
    (cond (text-stat
1007
           (lyskom-insert-header-comm text-stat misc read-text-stat))
1008
          ((not kom-deferred-printing) 
1009
           (lyskom-insert-header-comm text-stat misc read-text-stat))
1010
1011
1012
1013
1014
1015
1016
1017
          (t
           (let ((defer-info (lyskom-create-defer-info
                              'get-text-stat
                              text
                              'lyskom-insert-deferred-header-comm
                              (point-max-marker)
                              (length lyskom-defer-indicator)
                              nil       ; Filled in later
1018
                              (list misc read-text-stat))))
1019
1020
             (lyskom-format-insert "%#1s\n" lyskom-defer-indicator)
             (lyskom-defer-insertion defer-info))))
1021
1022
1023

    ;; Print information about who added the link
    (if (misc-info->sent-at misc)
1024
1025
	(lyskom-format-insert 'send-at (lyskom-format-time
                                        'date-and-time
1026
1027
1028
					(misc-info->sent-at misc))))
    (if (misc-info->sender misc)
	(lyskom-format-insert 'sent-by (misc-info->sender misc)))))
1029
1030
1031


(defun lyskom-insert-deferred-header-comm (text-stat defer-info)
1032
  (let* ((author (if text-stat (text-stat->author text-stat) nil))
1033
1034
	 (misc (elt (defer-info->data defer-info) 0))
	 (read-text-stat (elt (defer-info->data defer-info) 1))
1035
	 (type (misc-info->type misc))
David Byers's avatar
David Byers committed
1036
1037
1038
1039
1040
         (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)))
         (mx-attachments-in (lyskom-get-text-attachments read-text-stat))
1041
1042
1043
         (mx-belongs-to (mapcar
                         (lambda (el)
                           (string-to-number (aux-item->data el)))
David Byers's avatar
David Byers committed
1044
1045
                         (lyskom-get-aux-item (text-stat->aux-items 
                                               read-text-stat) 10100)))
1046
         (content-type "")
1047
	 fmt data)
1048
1049

    (setq author (or (lyskom-format-mx-author mx-from mx-author) author))
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
    ;; Extract the content type

    (if (and mx-from
             (setq content-type
                   (car (lyskom-get-aux-item (text-stat->aux-items 
                                              text-stat) 1))))
        (progn (string-match "^\\(\\S-+\\)" (aux-item->data content-type))
               (setq content-type (format "(%s) "
                                          (aux-item->data content-type))))
      (setq content-type ""))
1060
    
1061
1062
    (cond
     ((eq type 'COMM-TO)
1063
1064
      (setq fmt (cond ((memq (misc-info->comm-to misc) mx-belongs-to)
                       'attachment-to-text)
1065
1066
                      (author 'comment-to-text-by)
                      (t 'comment-to-text))
1067
1068
	    data (misc-info->comm-to misc)))
     ((eq type 'FOOTN-TO)
1069
1070
      (setq fmt (cond ((memq (misc-info->footn-to misc) mx-belongs-to)
                       'attachment-to-text)
1071
1072
                      (author 'footnote-to-text-by)
                      (t 'footnote-to-text))
1073
1074
	    data (misc-info->footn-to misc)))
     ((eq type 'COMM-IN)
1075
1076
      (setq fmt (cond ((memq (misc-info->comm-in misc) mx-attachments-in) 
                       'attachment-in-text)
1077
1078
                      (author 'comment-in-text-by)
                      (t 'comment-in-text))
1079
1080
	    data (misc-info->comm-in misc)))
     ((eq type 'FOOTN-IN)
1081
1082
      (setq fmt (cond ((memq (misc-info->footn-in misc) mx-attachments-in)
                       'attachment-in-text)
1083
1084
                      (author 'footnote-in-text-by)
                      (t 'footnote-in-text))
1085
	    data (misc-info->footn-in misc)))) 
1086
1087
    (set-defer-info->format defer-info fmt)
    ; Note: author is ignored if fmt is not *-by
1088
    (lyskom-replace-deferred defer-info data author content-type)))
1089

1090
1091


1092
(defun lyskom-insert-header-comm (text-stat misc read-text-stat)
1093
1094
  "Get author of TEXT-NO and print a header line."
  ;;+++ error kommer att se annorlunda ut.
1095
1096
1097
  (let ((author (if text-stat
		    (text-stat->author text-stat)
		  nil))
1098
1099
        (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)))
David Byers's avatar
David Byers committed
1100
        (mx-attachments-in (lyskom-get-text-attachments read-text-stat))
1101
1102
1103
1104
        (mx-belongs-to (mapcar
                        (lambda (el)
                          (string-to-number (aux-item->data el)))
                        (lyskom-get-aux-item (text-stat->aux-items read-text-stat) 10100)))
1105
1106
        (content-type "")
        (is-attachment nil)
1107
1108
1109
        (type (misc-info->type misc)))

    (setq author (or (lyskom-format-mx-author mx-from mx-author) author))
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121

    ;; Extract the content type

    (if (and mx-from
             (setq content-type
                   (car (lyskom-get-aux-item (text-stat->aux-items 
                                              text-stat) 1))))
        (progn (string-match "^\\(\\S-+\\)" (aux-item->data content-type))
               (setq content-type (format "(%s) "
                                          (aux-item->data content-type))))
      (setq content-type ""))

1122
    
1123
1124
    (cond
     ((eq type 'COMM-TO)
1125
1126
      (lyskom-format-insert (if (and (memq (misc-info->comm-to misc) mx-belongs-to)
                                     (setq is-attachment t))
1127
1128
                                'attachment-to-text
                              'comment-to-text)
1129
1130
1131
                            (misc-info->comm-to misc)
                            nil
                            content-type))
1132
     ((eq type 'FOOTN-TO)
1133
1134
      (lyskom-format-insert (if (and (memq (misc-info->footn-to misc) mx-belongs-to)
                                     (setq is-attachment t))
1135
1136
                                'attachment-to-text
                              'footnote-to-text)
1137
1138
1139
                            (misc-info->footn-to misc)
                            nil
                            content-type))
1140
     ((eq type 'COMM-IN)
1141
1142
      (lyskom-format-insert (if (and (memq (misc-info->comm-in misc) mx-attachments-in)
                                     (setq is-attachment t))
1143
1144
                                'attachment-in-text
                              'comment-in-text)
1145
1146
1147
                            (misc-info->comm-in misc)
                            nil
                            content-type))
1148
     ((eq type 'FOOTN-IN)
1149
1150
      (lyskom-format-insert (if (and (memq (misc-info->footn-in misc) mx-attachments-in)
                                     (setq is-attachment t))
1151
1152
                                'attachment-in-text
                              'footnote-in-text)
1153
1154
1155
1156
                            (misc-info->footn-in misc)
                            nil
                            content-type)))
    (if (and author (not is-attachment))
1157
	(lyskom-format-insert 'written-by author)
1158
      (lyskom-insert "\n"))))
1159

1160
1161
1162
1163
1164
1165
(defun lyskom-format-mx-author (mx-from mx-author)
  (let ((author nil))
    (when (or mx-from mx-author)
      (setq author (lyskom-get-string 'email-name-prefix))
      (when mx-author (setq author (concat author (aux-item->data mx-author))))
      (when (and mx-from mx-author) (setq author (concat author " ")))
1166
1167
1168
1169
1170
1171
      (when mx-from
        (setq author (concat author
                      (lyskom-format 
                       "<%[%#1@%#2s%]>"
                       (lyskom-default-button 'email (aux-item->data mx-from))
                       (aux-item->data mx-from)))))
1172
1173
1174
      (setq author (concat (lyskom-get-string 'email-name-suffix) author))
      )
    author))
1175

1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
(defun lyskom-mx-date-to-time (mx-date)
 "Attempt to convert MX-DATE to a lyskom time structure.
Returns the time structure if successful, otherwise nil."
  (if (and mx-date
           (condition-case nil
               (progn (require 'calendar)
                      (require 'cal-iso)
                      t)
             (error nil))
           (string-match "\\([0-9][0-9][0-9][0-9]\\)-\\([0-9][0-9]\\)-\\([0-9][0-9]\\) \\([0-9][0-9]\\):\\([0-9][0-9]\\):\\([0-9][0-9]\\) \\([-+][0-9][0-9]\\)?\\([0-9][0-9]\\)?"
                         (aux-item->data mx-date)))
      (let* ((secs (string-to-number (match-string 6 (aux-item->data mx-date))))
             (mins (string-to-number (match-string 5 (aux-item->data mx-date))))
             (hour (string-to-number (match-string 4 (aux-item->data mx-date))))
             (mday (string-to-number (match-string 3 (aux-item->data mx-date))))
             (mon  (string-to-number (match-string 2 (aux-item->data mx-date))))
             (year (string-to-number (match-string 1 (aux-item->data mx-date))))
             (tzhr (match-string 7 (aux-item->data mx-date)))
             (tzmin (or (match-string 8 (aux-item->data mx-date)) ""))
             (wday (elt 
                    (calendar-iso-from-absolute
                     (calendar-absolute-from-gregorian
                      (list mon mday year)))
                    1)))
1200
        (lyskom-create-time secs mins hour mday mon year wday 0 nil tzhr tzmin))))
1201
1202
1203
1204

;;; Local Variables: 
;;; eval: (put 'lyskom-traverse 'lisp-indent-hook 2)
;;; end: