view-text.el 44.7 KB
Newer Older
1
;;;;; -*-coding: raw-text;-*-
Linus Tolke Y's avatar
Linus Tolke Y committed
2 3
;;;;;
;;;;; $Id$
4
;;;;; Copyright (C) 1991, 1996  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y 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 Y's avatar
Linus Tolke Y 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
(defun lyskom-view-text (text-no &optional mark-as-read
				 follow-comments conf-stat priority
43 44
				 build-review-tree filter-active
				 flat-review)
45 46
  "Display text number TEXT-NO.
Args: TEXT-NO &optional MARK-AS-READ FOLLOW-COMMENTS CONF-STAT 
47
PRIORITY BUILD-REVIEW-TREE FLAT-REVIEW.
Per Cederqvist's avatar
.  
Per Cederqvist committed
48 49 50 51 52
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 
53
lyskom-reading-list to read the comments to this.
54 55
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.
56 57

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

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

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

78
	   (setq todo 'next-text)
79 80 81 82 83
	   'next-text)
	  (t
	   (if (not (or (null filter) 
			(eq filter 'dontshow)))
	       (lyskom-message "%s" (lyskom-get-string 'invalid-filter-list)))
84
	   (if (eq filter 'dontshow)
85
	       (setq todo 'next-text))
86 87
	   (blocking-do-multiple ((text-stat (get-text-stat text-no))
				  (text (get-text text-no)))
88
	     (if (and text-stat text)
89
		 (progn
90
                   (run-hooks 'lyskom-view-text-hook)
91 92 93
		   ;; Use a marker, because the buffer may lose data
		   ;; at the top if kom-max-buffer-size is set.
		   (setq start (point-max-marker))
94 95
		   (lyskom-format-insert "%#2@%[%#1n%] " text-stat (list 'lyskom-text-start
                                                                         (text-stat->text-no text-stat)))
96 97 98 99 100 101 102
                   (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
103
                         (mx-reply-to (lyskom-get-aux-item (text-stat->aux-items text-stat) 18)))
104 105 106 107 108
                         

                     ;; Insert date

                     (if mx-date
109 110 111 112 113 114 115 116 117
                         (let ((date (lyskom-mx-date-to-time mx-date)))
                           (if date
                             (progn
                               (lyskom-print-date-and-time date)
                               (when (time->tzhr date)
                                 (lyskom-format-insert " %#1s%#2s " 
                                                       (time->tzhr date)
                                                       (time->tzmin date))))
                             (lyskom-format-insert (aux-item->data mx-date))))
118 119 120 121 122 123
                       (lyskom-print-date-and-time (text-stat->creation-time
                                                    text-stat)
                                                   'time-y-m-d-h-m))

                     ;; Insert number of lines

124 125 126 127 128 129 130 131 132
		   (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)))))
133
		   
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
                   ;; 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")

                   ;; Insert sender

149
                   (when (and mx-sender kom-show-imported-envelope-sender)
150 151 152 153 154 155 156 157
                     (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

158 159 160
                   (cond ((and mx-from 
                               (text-stat->author text-stat)
                               kom-show-imported-importer)
161 162 163 164 165 166 167 168 169 170 171 172 173 174
                          (lyskom-format-insert 'text-imported-at-by
                                                (lyskom-return-date-and-time (text-stat->creation-time
                                                                              text-stat)
                                                                             'time-y-m-d-h-m)
                                                (text-stat->author text-stat)))
                         (mx-from
                          (lyskom-format-insert 'text-imported-at
                                                (lyskom-return-date-and-time (text-stat->creation-time
                                                                              text-stat)
                                                                             'time-y-m-d-h-m)))
                         (mx-date (lyskom-format-insert 'text-created-at
                                                        (lyskom-return-date-and-time (text-stat->creation-time
                                                                                      text-stat)
                                                                                     'time-y-m-d-h-m))))
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
                   (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)
                     )
192

193
		   
194
		   (setq end (point-max))
David Byers's avatar
David Byers committed
195

David Kågedal's avatar
David Kågedal committed
196 197
		   (if (and kom-text-properties
			    (null filter)
198
			    (not (lyskom-face-default-p 'kom-first-line-face)))
David Kågedal's avatar
David Kågedal committed
199 200
		       (add-text-properties
			start end '(face kom-first-line-face)))
201
		   (set-marker start nil)
David Byers's avatar
David Byers committed
202

203
		   ;; All recipients and other header lines.
204 205

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

                     ;;
                     ;; Print aux-items that go in the header
                     ;;

                     (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
252
                                                                 'text-print-when)
253 254
                                     'header)
                              (and (eq (lyskom-aux-item-definition-field aux
David Byers's avatar
David Byers committed
255
                                                                  'text-print-when)
256 257 258 259 260 261
                                       'comment)
                               (not kom-reading-puts-comments-in-pointers-last)
                               ))
                             (setq text 
                                   (lyskom-aux-item-definition-call 
                                    aux
David Byers's avatar
David Byers committed
262
                                    'text-print
263 264 265 266 267 268
                                    aux
                                    text-stat)))
                           (lyskom-insert text)
                           (lyskom-insert "\n"))))


269 270 271 272 273 274 275 276
		     (let ((num-marks (text-stat->no-of-marks text-stat))
			   (is-marked-by-me (cache-text-is-marked
					     (text-stat->text-no text-stat))))
		       (if (> num-marks 0)
			   (lyskom-insert 
			    (if is-marked-by-me
				(if (= num-marks 1)
				    (lyskom-get-string 'marked-by-you)
David Byers's avatar
David Byers committed
277 278 279
                                  (lyskom-format 'marked-by-you-and-others
                                                 (1- num-marks)))
                              (lyskom-format 'marked-by-several num-marks)))))
280
		   
281
		     (lyskom-print-text text-stat text
282
					mark-as-read text-no flat-review))
283 284
		   
		   
285 286 287 288 289
                   (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
290
                                                                 'text-print-when)
291 292
                                     'footer)
                              (and (eq (lyskom-aux-item-definition-field aux
David Byers's avatar
David Byers committed
293
                                                                  'text-print-when)
294 295 296 297 298 299
                                       'comment)
                                   kom-reading-puts-comments-in-pointers-last
                               ))
                             (setq text 
                                   (lyskom-aux-item-definition-call 
                                    aux
David Byers's avatar
David Byers committed
300
                                    'text-print
301 302 303 304 305 306
                                    aux
                                    text-stat)))
                           (lyskom-insert text)
                           (lyskom-insert "\n"))))


307 308 309 310
		   (if kom-reading-puts-comments-in-pointers-last
		       (lyskom-view-text-handle-saved-comments text-stat))
		   
		   (if (or follow-comments
David Kågedal's avatar
David Kågedal committed
311 312
			   ;; Checking build-review-tree should not be
			   ;; necessary, really /davidk
313 314
			   build-review-tree)
		       ;; This shows footnotes also.
315 316 317 318 319 320 321
		       (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)))
322
		   ))
323 324 325 326 327 328 329 330
	       (lyskom-format-insert 'no-such-text-no text-no))
             (let ((aux-items (text-stat->aux-items text-stat)))
               (while aux-items
                 (lyskom-aux-item-definition-call (car aux-items)
                                                  'read-action
                                                  text-stat)
                 (setq aux-items (cdr aux-items))))
             )))
331
    todo))
332
	  
333 334 335 336 337
	  


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

Arguments are: TEXT-STAT CONF-STAT PRIORITY REVIEW-TREE
Per Cederqvist's avatar
.  
Per Cederqvist committed
343 344 345 346 347
TEXT-STAT is the current text.
CONF-STAT the current conference
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."
348 349 350 351
  ;; . Find footnotes and show them.
  ;; . Fix the reading-list
  ;; . Issue cache-filling initiate-calls for everything left comments.
  
David Kågedal's avatar
David Kågedal committed
352
  (let (flist clist)
353 354 355 356 357 358
    (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
359 360
	(lyskom-view-text (misc-info->footn-in misc)
			  mark-as-read t conf-stat priority review-tree)
361 362
	(lyskom-is-read (misc-info->footn-in misc)))
       ((eq (misc-info->type misc) 'FOOTN-IN)
David Kågedal's avatar
David Kågedal committed
363
	(setq flist (cons (misc-info->footn-in misc) flist)))
364
       ((eq (misc-info->type misc) 'COMM-IN)
365
        (if lyskom-show-comments        ; +++SOJGE
David Kågedal's avatar
David Kågedal committed
366 367 368 369 370 371 372 373 374 375 376
            (setq clist (cons (misc-info->comm-in misc) clist))))))
    
    (let (comments footnotes)
      ;; Find the comments that we should read and enter them into the
      ;; read-list.
      (lyskom-traverse no clist
	(let ((text-stat (blocking-do 'get-text-stat no)))
	  (if (or review-tree
		  (and text-stat
		       (not (lyskom-text-read-p text-stat))))
	      (setq comments (cons no comments)))))
377
      (if comments
David Byers's avatar
David Byers committed
378 379 380 381 382 383 384 385 386 387 388 389 390
          (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
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
      ;; 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)))))
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428


(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
429 430


431 432 433 434 435 436

(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
437 438 439 440 441 442 443


(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))
444
	 (res 'not-member))
Per Cederqvist's avatar
.  
Per Cederqvist committed
445 446 447 448 449
    (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
David Byers's avatar
David Byers committed
450 451 452
	 ((or (eq type 'RECPT) 
              (eq type 'BCC-RECPT)
              (eq type 'CC-RECPT))
453 454 455
	  ;; 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
456
			     (misc-info->recipient-no misc-info) t))
Per Cederqvist's avatar
.  
Per Cederqvist committed
457
		(loc-no (misc-info->local-no misc-info)))
458 459 460 461 462

	    ;; 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
463 464 465
	    (if (and membership
		     (> loc-no (membership->last-text-read membership))
		     (not (lyskom-vmemq loc-no
466
					(membership->read-texts membership))))
Per Cederqvist's avatar
.  
Per Cederqvist committed
467
		(setq res nil)))))))
468 469 470
    (if (eq res 'not-member)
	(not kom-follow-comments-outside-membership)
      res)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
471 472


473
(defun lyskom-text-read-at-least-once-p (text-stat &optional bg)
David Byers's avatar
David Byers committed
474
  "Return t if TEXT-STAT has been marked as read in any of the recipients
475 476 477 478
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
479 480 481 482 483 484 485 486 487 488 489
  (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))
David Byers's avatar
David Byers committed
490 491 492
          (cond ((or (eq type 'RECPT) 
                     (eq type 'BCC-RECPT)
                     (eq type 'CC-RECPT))
493 494
                 (setq membership (if bg
                                      (lyskom-try-get-membership
David Byers's avatar
David Byers committed
495
                                       (misc-info->recipient-no misc-item) t)
496
                                    (lyskom-get-membership
David Byers's avatar
David Byers committed
497
                                     (misc-info->recipient-no misc-item) t)))
David Byers's avatar
David Byers committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
                 (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))


513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
(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))
                (= (1+ (time->mon time)) (elt decnow 4))
                (= (+ (time->year time) 1900) (elt decnow 5)))
           'today)
          ((and (= (time->mday time) (elt decthen 3))
                (= (1+ (time->mon time)) (elt decthen 4))
                (= (+ (time->year time) 1900) (elt decthen 5)))
           'yesterday)
          (t nil))))

David Byers's avatar
David Byers committed
537 538
(defun lyskom-return-date-and-time (time &optional fmt)
  "Return date and time as a string. Arg: TIME."
David Byers's avatar
David Byers committed
539 540
  (let* ((diff (and lyskom-print-complex-dates
		    (lyskom-calculate-day-diff time))))
David Byers's avatar
David Byers committed
541
    (lyskom-format
David Byers's avatar
David Byers committed
542
     (if diff
543
         (intern (concat (symbol-name diff) "-time-format-string"))
David Byers's avatar
David Byers committed
544
       (or fmt 'time-yyyy-mm-dd-hh-mm))
545 546 547 548 549 550 551
     (+ (time->year time) 1900)
     (1+ (time->mon  time))
     (time->mday time)
     (time->hour time)
     (time->min  time)
     (and diff (lyskom-get-string diff)))))
                  
David Byers's avatar
David Byers committed
552 553 554
(defun lyskom-print-date-and-time (time &optional fmt)
  "Print date and time. Arg: TIME"
  (lyskom-insert (lyskom-return-date-and-time time fmt)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
555

556 557 558 559 560 561 562
(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)
563
         (author-name-len nil)
564 565
         (have-author (and format (string-match "%=?[0-9]*P" format))))

566 567
    (setq author-name-len (length (or author-name (conf-stat->name author))))

568 569
    ;; No user-specified format, so output dashes and stuff

570
    (if (null format)
571 572 573 574 575
        (let* ((format-flag-string
                (if (and format-flags (listp format-flags))
                    (let ((first-flag t))
                      (concat
                       "("
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
                       (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 ", ")
594 595 596 597 598 599 600
                       ")"))
                  ""))
               (end-dash-chars
                (- kom-text-footer-dash-length
                   (length (int-to-string (text-stat->text-no text))) 
                   3                             ; Parens and space
                   (if kom-show-author-at-end
601
                       (+ author-name-len 2) 
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616
                     0)
                   (length format-flag-string)))
               (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
617
           (or author-name author)
618 619
           end-dash
           format-flag-string))
620

621 622
      ;; User-specified format. Do all kinds of weird stuff

623 624 625 626 627 628 629 630 631 632 633 634
      (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
635 636
                 (lyskom-format (format "%%%s#1p" field-width) 
                                (or author (text-stat->author text))))
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660

                ((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 
661
                    "%#1s"
662
                    (if have-author
663 664
                        (if (< author-name-len width)
                            (make-string (- width author-name-len) ?-)
665 666 667 668 669 670 671 672 673 674 675 676 677 678
                          "")
                      (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
679 680
(defun lyskom-deferred-insert-footer (conf-stat defer-info)
  "Insert the name of a conference at a previously reserved place."
681 682
  (let* ((text-stat (elt (defer-info->data defer-info) 0))
         (format-flags (elt (defer-info->data defer-info) 1))
683
         (mx-from (car (lyskom-get-aux-item (text-stat->aux-items text-stat) 17)))
684 685 686 687 688
         (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))
                      (t (lyskom-format 'person-does-not-exist)))))
689 690 691

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

692 693 694 695 696 697
    (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
698

699

700
(defun lyskom-print-text (text-stat text mark-as-read text-no flat-review)
Per Cederqvist's avatar
.  
Per Cederqvist committed
701 702 703
  "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.
704
Args: TEXT-STAT TEXT MARK-AS-READ TEXT-NO FLAT-REVIEW."
705 706
  (let ((lyskom-current-function 'lyskom-print-text))
    (cond
Per Cederqvist's avatar
.  
Per Cederqvist committed
707
     ((or (null text)
708
          (null text-stat))
Per Cederqvist's avatar
.  
Per Cederqvist committed
709 710 711
      (lyskom-format-insert 'no-such-text-no text-no)
      (setq lyskom-previous-text lyskom-current-text)
      (setq lyskom-current-text text-no))
712
     (t
Per Cederqvist's avatar
.  
Per Cederqvist committed
713
      (let* ((str (text->text-mass text))
714
	     (truncated nil)
715 716
             ;; s1 s2 t1 t2
	     body)
717 718 719 720 721 722 723 724
        (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)))
725
          (when kom-dashed-lines
726
              (lyskom-insert 
727 728 729
               (make-string kom-text-header-dash-length ?-)))

          (lyskom-insert "\n")
730
          ;; (setq t1 (point-max))
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748

	  ;; 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)))))
		    
749
          (let ((lyskom-current-function-phase 'body))
David Byers's avatar
David Byers committed
750
            (lyskom-format-insert "%#1t\n" (cons text-stat body)))
751 752 753

	  ;; Indicate that the text was truncated
	  (if truncated
754 755 756
              (lyskom-signal-reformatted-text
               '(reformat-truncated . (face kom-warning-face))))

757
	  
758 759
          ;; (setq t2 (point-max))
	  )
760
         (t                             ;No \n found. Don't print header.
761 762 763 764
          (when kom-dashed-lines
            (lyskom-insert 
             (make-string kom-text-header-dash-length ?-)))
          (lyskom-insert "\n")
David Byers's avatar
David Byers committed
765
          (lyskom-format-insert "%#1t\n" (cons text-stat str))
766 767 768 769 770
          (setq lyskom-current-subject "")))
        (if (lyskom-text-p (cache-get-text (text->text-no text)))
            (cache-del-text (text->text-no text)))
        (sit-for 0)
        (let ((lyskom-current-function-phase 'footer))
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
          (if kom-deferred-printing
              (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))))
            (let* ((conf-stat (blocking-do 'get-conf-stat 
                                           (text-stat->author text-stat)))
                   (author-name
                    (or (conf-stat->name conf-stat)
                        (and (eq (text-stat->author text-stat) 0)
                             (lyskom-get-string 'person-is-anonymous))
                        (lyskom-format 'person-does-not-exist
                                       (text-stat->author text-stat)))))
              (lyskom-insert (lyskom-format-text-footer 
                              text-stat
                              conf-stat
                              author-name
                              kom-text-footer-format
                              lyskom-last-text-format-flags)))
            (lyskom-insert "\n")))
801 802 803 804
        (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
805 806 807


(defun lyskom-mark-as-read (text-stat)
Per Cederqvist's avatar
Per Cederqvist committed
808
  "Mark a text as read in all conferences that are recipients.
Per Cederqvist's avatar
Per Cederqvist committed
809 810 811 812 813 814
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
815 816 817 818
  (let ((misc-info-list (text-stat->misc-info-list text-stat)))
    (lyskom-traverse
     misc-info misc-info-list
     (if (and (or (eq (misc-info->type misc-info) 'RECPT)
David Byers's avatar
David Byers committed
819
                  (eq (misc-info->type misc-info) 'BCC-RECPT)
Per Cederqvist's avatar
.  
Per Cederqvist committed
820
		  (eq (misc-info->type misc-info) 'CC-RECPT))
821 822 823 824
	      ;; 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))
	      )
Per Cederqvist's avatar
.  
Per Cederqvist committed
825 826 827 828 829
	 (initiate-mark-as-read 'background nil
				(misc-info->recipient-no misc-info)
				(list (misc-info->local-no misc-info)))))))


David Kågedal's avatar
David Kågedal committed
830
(defun lyskom-print-header-recpt (conf-no misc)
Per Cederqvist's avatar
.  
Per Cederqvist committed
831
  "Print a line of info about a recipient (or cc-recipient) of a text."
832 833 834
  (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
835 836 837 838 839
                              ((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
840
			conf-no
841
			(misc-info->local-no misc))
Per Cederqvist's avatar
.  
Per Cederqvist committed
842 843
  (if (misc-info->sent-at misc)
      (lyskom-format-insert 'send-at
David Byers's avatar
David Byers committed
844
			    (lyskom-return-date-and-time 
Per Cederqvist's avatar
.  
Per Cederqvist committed
845 846
			     (misc-info->sent-at misc))))
  (if (misc-info->sender misc)
David Kågedal's avatar
David Kågedal committed
847
      (lyskom-format-insert 'sent-by (misc-info->sender misc)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
848
  (if (misc-info->rec-time misc)
David Kågedal's avatar
David Kågedal committed
849 850 851 852 853
      (lyskom-format-insert
       'recieved-at
       (lyskom-return-date-and-time (misc-info->rec-time misc)))))


Per Cederqvist's avatar
.  
Per Cederqvist committed
854 855


856
(defun lyskom-view-text-handle-saved-comments (text-stat)
Per Cederqvist's avatar
.  
Per Cederqvist committed
857 858
  "Ask server for what is needed to print the references to commenting texts 
and prints the references. Used at the bottom of the text.
859 860 861 862 863 864
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)
865
        (if lyskom-show-comments        ;+++SOJGE
866
            (lyskom-print-header-comm (misc-info->comm-in misc) misc text-stat)))
867
       ((eq type 'FOOTN-IN)
868
	(lyskom-print-header-comm (misc-info->footn-in misc) misc text-stat))))))
869 870


871

872
(defun lyskom-print-header-comm (text misc read-text-stat)
873
  "Get author of TEXT-NO and print a header line."
874
  (let ((text-stat (if kom-deferred-printing
875
		       (cache-get-text-stat text)
876
		     (blocking-do 'get-text-stat text))))
877 878

    ;; Print information about the link
879
    (cond (text-stat
880
           (lyskom-insert-header-comm text-stat misc read-text-stat))
881
          ((not kom-deferred-printing) 
882
           (lyskom-insert-header-comm text-stat misc read-text-stat))
883 884 885 886 887 888 889 890
          (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
891
                              (list misc read-text-stat))))
892 893
             (lyskom-format-insert "%#1s\n" lyskom-defer-indicator)
             (lyskom-defer-insertion defer-info))))
894 895 896 897 898 899 900

    ;; Print information about who added the link
    (if (misc-info->sent-at misc)
	(lyskom-format-insert 'send-at (lyskom-return-date-and-time 
					(misc-info->sent-at misc))))
    (if (misc-info->sender misc)
	(lyskom-format-insert 'sent-by (misc-info->sender misc)))))
901 902 903


(defun lyskom-insert-deferred-header-comm (text-stat defer-info)
904
  (let* ((author (if text-stat (text-stat->author text-stat) nil))
905 906
	 (misc (elt (defer-info->data defer-info) 0))
	 (read-text-stat (elt (defer-info->data defer-info) 1))
907
	 (type (misc-info->type misc))
908 909 910 911 912 913 914 915 916 917
         (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 (mapcar
                             (lambda (el)
                               (string-to-number (aux-item->data el)))
                             (lyskom-get-aux-item (text-stat->aux-items read-text-stat) 10101)))
         (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)))
918
         (content-type "")
919
	 fmt data)
920 921

    (setq author (or (lyskom-format-mx-author mx-from mx-author) author))
922 923 924 925 926 927 928 929 930 931
    ;; 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 ""))
932
    
933 934
    (cond
     ((eq type 'COMM-TO)
935 936
      (setq fmt (cond ((memq (misc-info->comm-to misc) mx-belongs-to)
                       'attachment-to-text)
937 938
                      (author 'comment-to-text-by)
                      (t 'comment-to-text))
939 940
	    data (misc-info->comm-to misc)))
     ((eq type 'FOOTN-TO)
941 942
      (setq fmt (cond ((memq (misc-info->footn-to misc) mx-belongs-to)
                       'attachment-to-text)
943 944
                      (author 'footnote-to-text-by)
                      (t 'footnote-to-text))
945 946
	    data (misc-info->footn-to misc)))
     ((eq type 'COMM-IN)
947 948
      (setq fmt (cond ((memq (misc-info->comm-in misc) mx-attachments-in) 
                       'attachment-in-text)
949 950
                      (author 'comment-in-text-by)
                      (t 'comment-in-text))
951 952
	    data (misc-info->comm-in misc)))
     ((eq type 'FOOTN-IN)
953 954
      (setq fmt (cond ((memq (misc-info->footn-in misc) mx-attachments-in)
                       'attachment-in-text)
955 956
                      (author 'footnote-in-text-by)
                      (t 'footnote-in-text))
957
	    data (misc-info->footn-in misc)))) 
958 959
    (set-defer-info->format defer-info fmt)
    ; Note: author is ignored if fmt is not *-by
960
    (lyskom-replace-deferred defer-info data author content-type)))
961

962 963


964
(defun lyskom-insert-header-comm (text-stat misc read-text-stat)
965 966
  "Get author of TEXT-NO and print a header line."
  ;;+++ error kommer att se annorlunda ut.
967 968 969
  (let ((author (if text-stat
		    (text-stat->author text-stat)
		  nil))
970 971 972 973 974 975 976 977 978 979
        (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 (mapcar
                            (lambda (el)
                              (string-to-number (aux-item->data el)))
                            (lyskom-get-aux-item (text-stat->aux-items read-text-stat) 10101)))
        (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)))
980 981
        (content-type "")
        (is-attachment nil)
982 983 984
        (type (misc-info->type misc)))

    (setq author (or (lyskom-format-mx-author mx-from mx-author) author))
985 986 987 988 989 990 991 992 993 994 995 996

    ;; 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 ""))

997
    
998 999
    (cond
     ((eq type 'COMM-TO)
1000 1001
      (lyskom-format-insert (if (and (memq (misc-info->comm-to misc) mx-belongs-to)
                                     (setq is-attachment t))
1002 1003
                                'attachment-to-text
                              'comment-to-text)
1004 1005 1006
                            (misc-info->comm-to misc)
                            nil
                            content-type))
1007
     ((eq type 'FOOTN-TO)
1008 1009
      (lyskom-format-insert (if (and (memq (misc-info->footn-to misc) mx-belongs-to)
                                     (setq is-attachment t))
1010 1011
                                'attachment-to-text
                              'footnote-to-text)
1012 1013 1014
                            (misc-info->footn-to misc)
                            nil
                            content-type))
1015
     ((eq type 'COMM-IN)
1016 1017
      (lyskom-format-insert (if (and (memq (misc-info->comm-in misc) mx-attachments-in)
                                     (setq is-attachment t))
1018 1019
                                'attachment-in-text
                              'comment-in-text)
1020 1021 1022
                            (misc-info->comm-in misc)
                            nil
                            content-type))
1023
     ((eq type 'FOOTN-IN)
1024 1025
      (lyskom-format-insert (if (and (memq (misc-info->footn-in misc) mx-attachments-in)
                                     (setq is-attachment t))
1026 1027
                                'attachment-in-text
                              'footnote-in-text)
1028 1029 1030 1031
                            (misc-info->footn-in misc)
                            nil
                            content-type)))
    (if (and author (not is-attachment))
1032
	(lyskom-format-insert 'written-by author)
1033
      (lyskom-insert "\n"))))
1034

1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045
(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 " ")))
      (when mx-from (setq author (concat author "<" (aux-item->data mx-from) ">")))
      (setq author (concat (lyskom-get-string 'email-name-suffix) author))
      )
    author))
    
1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
(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