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


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


40 41
(defun lyskom-view-text (text-no &optional mark-as-read
				 follow-comments conf-stat priority
42
				 build-review-tree filter-active)
43 44
  "Display text number TEXT-NO.
Args: TEXT-NO &optional MARK-AS-READ FOLLOW-COMMENTS CONF-STAT 
Per Cederqvist's avatar
.  
Per Cederqvist committed
45 46 47 48 49 50 51
PRIORITY BUILD-REVIEW-TREE.
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 
lyskom-reading-list to read the comments to this."
52

53
  (let ((filter (and filter-active
54 55
		    (lyskom-filter-text-p text-no)))
	(todo nil))
56
    (cond ((eq filter 'skip-text) (lyskom-filter-prompt text-no 'filter-text)
57
	   (setq todo 'next-text)
58 59 60
	   'next-text)
	  ((eq filter 'skip-tree)
	   (lyskom-filter-prompt text-no 'filter-tree)
David Kågedal's avatar
David Kågedal committed
61 62 63 64 65 66

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

67
	   (setq todo 'next-text)
68 69 70 71 72
	   'next-text)
	  (t
	   (if (not (or (null filter) 
			(eq filter 'dontshow)))
	       (lyskom-message "%s" (lyskom-get-string 'invalid-filter-list)))
73
	   (if (eq filter 'dontshow)
74
	       (setq todo 'next-text))
75 76 77 78 79
	   (blocking-do-multiple ((text-stat (get-text-stat text-no))
				  (text (get-text text-no)))
	     (if (and text-stat
		      text)
		 (progn
80 81
		   (lyskom-format-insert "%#1n " 
					 text-stat)
82
		   (lyskom-print-date-and-time (text-stat->creation-time
David Byers's avatar
David Byers committed
83 84
                                        text-stat)
                                       'time-y-m-d-h-m)
85 86 87 88 89 90 91 92 93
		   (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)))))
94 95 96 97 98 99 100
		   
		   (if (eq filter 'dontshow)
		       (lyskom-format-insert "%#1P %#2s\n"
					     (text-stat->author text-stat)
					     (lyskom-get-string 'filtered))
		     (lyskom-format-insert "%#1P\n"
					   (text-stat->author text-stat)))
101 102
		   
		   ;; All recipients and other header lines.
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

		   (if (eq filter 'dontshow)
		       nil
		     (lyskom-traverse misc
			 (text-stat->misc-info-list text-stat)
		       (let ((type (misc-info->type misc)))
			 (cond
			  ((or (eq type 'RECPT)
			       (eq type 'CC-RECPT))
			   (lyskom-print-header-recpt 
			    (blocking-do 'get-conf-stat
					 (misc-info->recipient-no
					  misc))
			    misc))
			  ((eq type 'COMM-IN)
			   (if kom-reading-puts-comments-in-pointers-last
			       nil
			     (lyskom-print-header-comm
			      (misc-info->comm-in misc)
			      misc)))
			  ((eq type 'FOOTN-IN)
			   (if kom-reading-puts-comments-in-pointers-last
			       nil
			     (lyskom-print-header-comm
			      (misc-info->footn-in misc)
			      misc)))
			  ((eq type 'COMM-TO)
			   (lyskom-print-header-comm (misc-info->comm-to misc)
						     misc))
			  ((eq type 'FOOTN-TO)
			   (lyskom-print-header-comm (misc-info->footn-to misc)
						     misc))
			  )))
		     (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)
				  (if (= num-marks 2)
				      (lyskom-get-string 
				       'marked-by-you-and-one)
				    (lyskom-format 'marked-by-you-and-several
						   (1- num-marks))))
149
			      (if (= num-marks 1)
150 151 152
				  (lyskom-get-string 'marked-by-one)
				(lyskom-format 'marked-by-several
					       num-marks))))))
153
		   
154 155 156
		     (lyskom-print-text text-stat (blocking-do
						   'get-text text-no)
					mark-as-read text-no))
157 158 159 160 161 162 163 164 165 166 167
		   
		   
		   (if kom-reading-puts-comments-in-pointers-last
		       (lyskom-view-text-handle-saved-comments text-stat))
		   
		   (if (or follow-comments
			   build-review-tree)
		       ;; This shows footnotes also.
		       (lyskom-follow-comments text-stat conf-stat mark-as-read
					       priority build-review-tree))
		   )
168
	       (lyskom-format-insert 'no-such-text text-no)))))
169
    todo))
170
	  
171 172
	  
(defun lyskom-insert-person-name (conf-no)
173
	    "Inserts the name the conf CONF-NO. If CONF-NO is 0 this person does not exist."
174 175 176 177 178 179
  (let ((cs nil))
    (if (zerop conf-no)
	nil
      (setq cs (blocking-do 'get-conf-stat conf-no)))
    (if cs
	(lyskom-insert (conf-stat->name cs))
180 181 182
      (if (eq conf-no 0)
	  (lyskom-format-insert 'person-is-anonymous)
	(lyskom-format-insert 'person-does-not-exist conf-no)))))
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198


(defun lyskom-insert-conf-name (conf-no)
  "Inserts the name the conf CONF-NO. If CONF-NO is 0 this person does not exist."
  (let ((cs nil))
    (if (zerop conf-no)
	nil
      (setq cs (blocking-do 'get-conf-stat conf-no)))
    (if cs
	(lyskom-insert (conf-stat->name cs))
      (lyskom-format-insert 'conf-does-not-exist conf-no))))



(defun lyskom-follow-comments (text-stat conf-stat 
					 mark-as-read priority review-tree)
Per Cederqvist's avatar
.  
Per Cederqvist committed
199
  "Arrange so that all comments and footnotes to this text will be read.
200 201 202 203
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
204 205 206 207 208
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."
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
  ;; . Find footnotes and show them.
  ;; . Fix the reading-list
  ;; . Issue cache-filling initiate-calls for everything left comments.
  
  (let ((list))
    (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
	(lyskom-view-text (misc-info->footn-in misc) mark-as-read)
	(lyskom-is-read (misc-info->footn-in misc)))
       ((eq (misc-info->type misc) 'FOOTN-IN)
	(setq list (cons (misc-info->footn-in misc) list)))
       ((eq (misc-info->type misc) 'COMM-IN)
	(setq list (cons (misc-info->comm-in misc) list)))))

    (let ((comments nil))
      (lyskom-traverse no list
	(cond
	 ((or review-tree
	      (not (lyskom-text-read-p (blocking-do 'get-text-stat no))))
	  (setq comments (cons no comments)))))
      (if comments
	  (read-list-enter-read-info
	   (lyskom-create-read-info (if review-tree 'REVIEW-TREE 'COMM-IN)
				    conf-stat priority
				    (lyskom-create-text-list comments)
				    (text-stat->text-no text-stat))
	   lyskom-reading-list t))
      ;; We have most of the text stats in the cache...
      ;; Just a few stray things to fill the cache...
      (lyskom-traverse no comments
	(initiate-get-text-stat 'fill-cache 'lyskom-fetch-text-for-cache no)))))


(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
266 267


268 269 270 271 272 273

(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
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298


(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))
	 (res t))
    (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
	 ((or (eq type 'RECPT) (eq type 'CC-RECPT))
	  (let ((membership (lyskom-member-p
			     (misc-info->recipient-no misc-info)))
		(loc-no (misc-info->local-no misc-info)))
	    (if (and membership
		     (> loc-no (membership->last-text-read membership))
		     (not (lyskom-vmemq loc-no
				       (membership->read-texts membership))))
		(setq res nil)))))))
    res))


299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
(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
323 324
(defun lyskom-return-date-and-time (time &optional fmt)
  "Return date and time as a string. Arg: TIME."
325
  (let* ((diff (lyskom-calculate-day-diff time)))
David Byers's avatar
David Byers committed
326
    (lyskom-format
327 328
     (if (and diff lyskom-print-complex-dates)
         (intern (concat (symbol-name diff) "-time-format-string"))
David Byers's avatar
David Byers committed
329
       (or fmt 'time-yyyy-mm-dd-hh-mm))
330 331 332 333 334 335 336
     (+ (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
337 338 339
(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
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354


(defun lyskom-print-text (text-stat text mark-as-read text-no)
  "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.
Args: TEXT-STAT TEXT MARK-AS-READ TEXT-NO."
  (cond
     ((or (null text)
	  (null text-stat))
      (lyskom-format-insert 'no-such-text-no text-no)
      (setq lyskom-previous-text lyskom-current-text)
      (setq lyskom-current-text text-no))
   (t
      (let* ((str (text->text-mass text))
355
	     s1 s2 t1 t2 body)
Per Cederqvist's avatar
.  
Per Cederqvist committed
356 357 358
	(cond
	 ((string-match "\n" str)
	    (setq lyskom-current-subject (substring str 0 (match-beginning 0)))
359
	    (setq body (substring str (match-end 0)))
Per Cederqvist's avatar
.  
Per Cederqvist committed
360 361
	    (lyskom-insert-string 'head-Subject)
	    (setq s1 (point-max))
362
	    (lyskom-format-insert "%#1r\n" lyskom-current-subject)
Per Cederqvist's avatar
.  
Per Cederqvist committed
363 364 365 366 367 368
	    (setq s2 (point-max))
	    (if kom-dashed-lines
		(lyskom-insert 
		 "------------------------------------------------------------\n")
	      (lyskom-insert "\n"))
	    (setq t1 (point-max))
369
	    (lyskom-format-insert "%#1t" body)
Per Cederqvist's avatar
.  
Per Cederqvist committed
370 371 372
	    (setq t2 (point-max)))
	 (t				;No \n found. Don't print header.
	  (setq s1 (point-max))
David Kågedal's avatar
David Kågedal committed
373
	  (lyskom-format-insert "%#1t" str)
Per Cederqvist's avatar
.  
Per Cederqvist committed
374 375 376 377 378
	  (setq s2 (point-max))
	  (setq t1 (point-max)
		t2 (point-max))
	  (setq lyskom-current-subject "")))
	(if (lyskom-text-p (cache-get-text (text->text-no text)))
379
	    (cache-del-text (text->text-no text)))
380
	(sit-for 0)
381
	(lyskom-format-insert 
Per Cederqvist's avatar
.  
Per Cederqvist committed
382
			(if kom-dashed-lines
383 384 385
			    "\n(%#1n) -----------------------------------\n"
			  "\n(%#1n)\n")
			(text->text-no text))
Per Cederqvist's avatar
.  
Per Cederqvist committed
386 387 388 389 390 391 392
	(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))))))


(defun lyskom-mark-as-read (text-stat)
Per Cederqvist's avatar
Per Cederqvist committed
393
  "Mark a text as read in all conferences that are recipients.
Per Cederqvist's avatar
Per Cederqvist committed
394 395 396 397 398 399
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
400 401 402 403 404 405 406 407 408 409 410 411 412
  (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)
		  (eq (misc-info->type misc-info) 'CC-RECPT))
	      (lyskom-member-p (misc-info->recipient-no misc-info)))
	 (initiate-mark-as-read 'background nil
				(misc-info->recipient-no misc-info)
				(list (misc-info->local-no misc-info)))))))


(defun lyskom-print-header-recpt (conf-stat misc)
  "Print a line of info about a recipient (or cc-recipient) of a text."
413 414 415 416 417 418
  (lyskom-format-insert "%#1s: %#2M <%#3d>\n"
			(cond ((eq (misc-info->type misc) 'RECPT)
			       (lyskom-get-string 'Recipient))
			      (t (lyskom-get-string 'Extra-recipient)))
			conf-stat
			(misc-info->local-no misc))
Per Cederqvist's avatar
.  
Per Cederqvist committed
419 420
  (if (misc-info->sent-at misc)
      (lyskom-format-insert 'send-at
David Byers's avatar
David Byers committed
421
			    (lyskom-return-date-and-time 
Per Cederqvist's avatar
.  
Per Cederqvist committed
422 423
			     (misc-info->sent-at misc))))
  (if (misc-info->sender misc)
424
	(lyskom-insert (lyskom-format 'sent-by (misc-info->sender misc))))
Per Cederqvist's avatar
.  
Per Cederqvist committed
425 426
  (if (misc-info->rec-time misc)
      (lyskom-format-insert 'recieved-at
David Byers's avatar
David Byers committed
427
			    (lyskom-return-date-and-time (misc-info->rec-time misc)))))
Per Cederqvist's avatar
.  
Per Cederqvist committed
428 429


430
(defun lyskom-view-text-handle-saved-comments (text-stat)
Per Cederqvist's avatar
.  
Per Cederqvist committed
431 432
  "Ask server for what is needed to print the references to commenting texts 
and prints the references. Used at the bottom of the text.
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
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)
	(lyskom-print-header-comm (misc-info->comm-in misc) misc))
       ((eq type 'FOOTN-IN)
	(lyskom-print-header-comm (misc-info->footn-in misc) misc))))))


(defun lyskom-print-header-comm (text misc)
  "Get author of TEXT-NO and print a header line."
  (let ((text-stat (blocking-do 'get-text-stat text)))
    ;;+++ error kommer att se annorlunda ut.
    (if text-stat
449
	(let* ((author (blocking-do 'get-conf-stat
450 451
				    (text-stat->author text-stat)))
	       (type (misc-info->type misc))
452 453 454 455 456 457
	       (tyname (cond ((eq 0 (text-stat->author text-stat))
                          (lyskom-get-string 'person-is-anonymous))
                         ((null author)
                          (lyskom-format 'person-does-not-exist
                                         (text-stat->author text-stat)))
                         (t author))))
458

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
	  (cond
	   ((eq type 'COMM-TO)
	    (lyskom-format-insert 'comment-to-text-by 
				  (misc-info->comm-to misc)
				  tyname))
	   ((eq type 'FOOTN-TO)
	    (lyskom-format-insert 'footnote-to-text-by
				  (misc-info->footn-to misc)
				  tyname))
	   ((eq type 'COMM-IN)
	    (lyskom-format-insert 'comment-in-text-by
				  (misc-info->comm-in misc)
				  tyname))
	   ((eq type 'FOOTN-IN)
	    (lyskom-format-insert 'footnote-in-text-by
				  (misc-info->footn-in misc)
				  tyname))))
      ;; Client tolerans agains buggy servers...
      ;; We are writing the line about what comments exists and
      ;; the reference text does not exist anymore. Strange.
      nil)))



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