comparison lisp/gnus/shr.el @ 110804:30fed27e97bc

Merge changes made in Gnus trunk. nnimap.el (nnimap-request-rename-group): Add this method. shr.el: Keep track of the natural width of TD elements, so we know which ones to expand. shr.el: Expand TD elements to fill available space.
author Katsumi Yamaoka <yamaoka@jpl.org>
date Thu, 07 Oct 2010 11:46:01 +0000
parents 647b7ac5007c
children 07053df95af6
comparison
equal deleted inserted replaced
110803:e5dfbfe93896 110804:30fed27e97bc
182 (when (> column 0) 182 (when (> column 0)
183 (cond 183 (cond
184 ((and (or (not first) 184 ((and (or (not first)
185 (eq shr-state 'space)) 185 (eq shr-state 'space))
186 (> (+ column (length elem) 1) shr-width)) 186 (> (+ column (length elem) 1) shr-width))
187 (insert "\n")) 187 (insert "\n")
188 (put-text-property (1- (point)) (point) 'shr-break t))
188 ((not first) 189 ((not first)
189 (insert " ")))) 190 (insert " "))))
190 (setq first nil) 191 (setq first nil)
191 (when (and (bolp) 192 (when (and (bolp)
192 (> shr-indentation 0)) 193 (> shr-indentation 0))
457 (suggested-widths (shr-pro-rate-columns columns)) 458 (suggested-widths (shr-pro-rate-columns columns))
458 ;; Do a "test rendering" to see how big each TD is (this can 459 ;; Do a "test rendering" to see how big each TD is (this can
459 ;; be smaller (if there's little text) or bigger (if there's 460 ;; be smaller (if there's little text) or bigger (if there's
460 ;; unbreakable text). 461 ;; unbreakable text).
461 (sketch (shr-make-table cont suggested-widths)) 462 (sketch (shr-make-table cont suggested-widths))
462 (sketch-widths (shr-table-widths sketch (length suggested-widths)))) 463 (sketch-widths (shr-table-widths sketch suggested-widths)))
463 ;; Then render the table again with these new "hard" widths. 464 ;; Then render the table again with these new "hard" widths.
464 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths)) 465 (shr-insert-table (shr-make-table cont sketch-widths t) sketch-widths))
465 ;; Finally, insert all the images after the table. The Emacs buffer 466 ;; Finally, insert all the images after the table. The Emacs buffer
466 ;; model isn't strong enough to allow us to put the images actually 467 ;; model isn't strong enough to allow us to put the images actually
467 ;; into the tables. 468 ;; into the tables.
488 (dotimes (i height) 489 (dotimes (i height)
489 (shr-indent) 490 (shr-indent)
490 (insert "|\n")) 491 (insert "|\n"))
491 (dolist (column row) 492 (dolist (column row)
492 (goto-char start) 493 (goto-char start)
493 (let ((lines (split-string (nth 2 column) "\n")) 494 (let ((lines (nth 2 column))
494 (overlay-lines (nth 3 column)) 495 (overlay-lines (nth 3 column))
495 overlay overlay-line) 496 overlay overlay-line)
496 (dolist (line lines) 497 (dolist (line lines)
497 (setq overlay-line (pop overlay-lines)) 498 (setq overlay-line (pop overlay-lines))
498 (when (> (length line) 0) 499 (when (> (length line) 0)
518 (insert "+") 519 (insert "+")
519 (dotimes (i (length widths)) 520 (dotimes (i (length widths))
520 (insert (make-string (aref widths i) ?-) ?+)) 521 (insert (make-string (aref widths i) ?-) ?+))
521 (insert "\n")) 522 (insert "\n"))
522 523
523 (defun shr-table-widths (table length) 524 (defun shr-table-widths (table suggested-widths)
524 (let ((widths (make-vector length 0))) 525 (let* ((length (length suggested-widths))
526 (widths (make-vector length 0))
527 (natural-widths (make-vector length 0)))
525 (dolist (row table) 528 (dolist (row table)
526 (let ((i 0)) 529 (let ((i 0))
527 (dolist (column row) 530 (dolist (column row)
528 (aset widths i (max (aref widths i) 531 (aset widths i (max (aref widths i)
529 (car column))) 532 (car column)))
530 (incf i)))) 533 (aset natural-widths i (max (aref natural-widths i)
534 (cadr column)))
535 (setq i (1+ i)))))
536 (let ((extra (- (reduce '+ suggested-widths)
537 (reduce '+ widths)))
538 (expanded-columns 0))
539 (when (> extra 0)
540 (dotimes (i length)
541 ;; If the natural width is wider than the rendered width, we
542 ;; want to allow the column to expand.
543 (when (> (aref natural-widths i) (aref widths i))
544 (setq expanded-columns (1+ expanded-columns))))
545 (dotimes (i length)
546 (when (> (aref natural-widths i) (aref widths i))
547 (aset widths i (min
548 (1+ (aref natural-widths i))
549 (+ (/ extra expanded-columns)
550 (aref widths i))))))))
531 widths)) 551 widths))
532 552
533 (defun shr-make-table (cont widths &optional fill) 553 (defun shr-make-table (cont widths &optional fill)
534 (let ((trs nil)) 554 (let ((trs nil))
535 (dolist (row cont) 555 (dolist (row cont)
573 (while (not (eobp)) 593 (while (not (eobp))
574 (end-of-line) 594 (end-of-line)
575 (when (> (- width (current-column)) 0) 595 (when (> (- width (current-column)) 0)
576 (insert (make-string (- width (current-column)) ? ))) 596 (insert (make-string (- width (current-column)) ? )))
577 (forward-line 1)))) 597 (forward-line 1))))
578 (list max 598 (if fill
579 (count-lines (point-min) (point-max)) 599 (list max
580 (buffer-string) 600 (count-lines (point-min) (point-max))
581 (and fill 601 (split-string (buffer-string) "\n")
582 (shr-collect-overlays)))))) 602 (shr-collect-overlays))
603 (list max
604 (shr-natural-width))))))
605
606 (defun shr-natural-width ()
607 (goto-char (point-min))
608 (let ((current 0)
609 (max 0))
610 (while (not (eobp))
611 (end-of-line)
612 (setq current (+ current (current-column)))
613 (unless (get-text-property (point) 'shr-break)
614 (setq max (max max current)
615 current 0))
616 (forward-line 1))
617 max))
583 618
584 (defun shr-collect-overlays () 619 (defun shr-collect-overlays ()
585 (save-excursion 620 (save-excursion
586 (goto-char (point-min)) 621 (goto-char (point-min))
587 (let ((overlays nil)) 622 (let ((overlays nil))
606 641
607 (defun shr-pro-rate-columns (columns) 642 (defun shr-pro-rate-columns (columns)
608 (let ((total-percentage 0) 643 (let ((total-percentage 0)
609 (widths (make-vector (length columns) 0))) 644 (widths (make-vector (length columns) 0)))
610 (dotimes (i (length columns)) 645 (dotimes (i (length columns))
611 (incf total-percentage (aref columns i))) 646 (setq total-percentage (+ total-percentage (aref columns i))))
612 (setq total-percentage (/ 1.0 total-percentage)) 647 (setq total-percentage (/ 1.0 total-percentage))
613 (dotimes (i (length columns)) 648 (dotimes (i (length columns))
614 (aset widths i (max (truncate (* (aref columns i) 649 (aset widths i (max (truncate (* (aref columns i)
615 total-percentage 650 total-percentage
616 shr-width)) 651 (- shr-width (1+ (length columns)))))
617 10))) 652 10)))
618 widths)) 653 widths))
619 654
620 ;; Return a summary of the number and shape of the TDs in the table. 655 ;; Return a summary of the number and shape of the TDs in the table.
621 (defun shr-column-specs (cont) 656 (defun shr-column-specs (cont)