Add 2012 to FSF copyright years for Emacs files.
[gnus] / lisp / gnus-salt.el
1 ;;; gnus-salt.el --- alternate summary mode interfaces for Gnus
2
3 ;; Copyright (C) 1996-1999, 2001-2012  Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28 (eval-when-compile
29   (when (featurep 'xemacs)
30     (require 'easy-mmode))) ; for `define-minor-mode'
31
32 (require 'gnus)
33 (require 'gnus-sum)
34 (require 'gnus-win)
35
36 ;;;
37 ;;; gnus-pick-mode
38 ;;;
39
40 (defcustom gnus-pick-display-summary nil
41   "*Display summary while reading."
42   :type 'boolean
43   :group 'gnus-summary-pick)
44
45 (defcustom gnus-pick-mode-hook nil
46   "Hook run in summary pick mode buffers."
47   :type 'hook
48   :group 'gnus-summary-pick)
49
50 (when (featurep 'xemacs)
51   (add-hook 'gnus-pick-mode-hook 'gnus-xmas-pick-menu-add))
52
53 (defcustom gnus-mark-unpicked-articles-as-read nil
54   "*If non-nil, mark all unpicked articles as read."
55   :type 'boolean
56   :group 'gnus-summary-pick)
57
58 (defcustom gnus-pick-elegant-flow t
59   "If non-nil, `gnus-pick-start-reading' runs
60  `gnus-summary-next-group' when no articles have been picked."
61   :type 'boolean
62   :group 'gnus-summary-pick)
63
64 (defcustom gnus-summary-pick-line-format
65   "%-5P %U\%R\%z\%I\%(%[%4L: %-23,23n%]%) %s\n"
66   "*The format specification of the lines in pick buffers.
67 It accepts the same format specs that `gnus-summary-line-format' does."
68   :type 'string
69   :group 'gnus-summary-pick)
70
71 ;;; Internal variables.
72
73 (defvar gnus-pick-mode-map
74   (let ((map (make-sparse-keymap)))
75     (gnus-define-keys map
76       " " gnus-pick-next-page
77       "u" gnus-pick-unmark-article-or-thread
78       "." gnus-pick-article-or-thread
79       gnus-down-mouse-2 gnus-pick-mouse-pick-region
80       "\r" gnus-pick-start-reading)
81     map))
82
83 (defun gnus-pick-make-menu-bar ()
84   (unless (boundp 'gnus-pick-menu)
85     (easy-menu-define
86       gnus-pick-menu gnus-pick-mode-map ""
87       '("Pick"
88         ("Pick"
89          ["Article" gnus-summary-mark-as-processable t]
90          ["Thread" gnus-uu-mark-thread t]
91          ["Region" gnus-uu-mark-region t]
92          ["Regexp" gnus-uu-mark-by-regexp t]
93          ["Buffer" gnus-uu-mark-buffer t])
94         ("Unpick"
95          ["Article" gnus-summary-unmark-as-processable t]
96          ["Thread" gnus-uu-unmark-thread t]
97          ["Region" gnus-uu-unmark-region t]
98          ["Regexp" gnus-uu-unmark-by-regexp t]
99          ["Buffer" gnus-summary-unmark-all-processable t])
100         ["Start reading" gnus-pick-start-reading t]
101         ["Switch pick mode off" gnus-pick-mode gnus-pick-mode]))))
102
103 (eval-when-compile
104   (when (featurep 'xemacs)
105     (defvar gnus-pick-mode-on-hook)
106     (defvar gnus-pick-mode-off-hook)))
107
108 (define-minor-mode gnus-pick-mode
109   "Minor mode for providing a pick-and-read interface in Gnus summary buffers.
110
111 \\{gnus-pick-mode-map}"
112   :lighter " Pick" :keymap gnus-pick-mode-map
113   (cond
114    ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-pick-mode nil))
115    ((not gnus-pick-mode)
116     ;; FIXME: a buffer-local minor mode removing globally from a hook??
117     (remove-hook 'gnus-message-setup-hook 'gnus-pick-setup-message))
118    (t
119     ;; Make sure that we don't select any articles upon group entry.
120     (set (make-local-variable 'gnus-auto-select-first) nil)
121     ;; Change line format.
122     (setq gnus-summary-line-format gnus-summary-pick-line-format)
123     (setq gnus-summary-line-format-spec nil)
124     (gnus-update-format-specifications nil 'summary)
125     (gnus-update-summary-mark-positions)
126     ;; FIXME: a buffer-local minor mode adding globally to a hook??
127     (add-hook 'gnus-message-setup-hook 'gnus-pick-setup-message)
128     (set (make-local-variable 'gnus-summary-goto-unread) 'never)
129     ;; Set up the menu.
130     (when (gnus-visual-p 'pick-menu 'menu)
131       (gnus-pick-make-menu-bar)))))
132
133 (defun gnus-pick-setup-message ()
134   "Make Message do the right thing on exit."
135   (when (and (gnus-buffer-live-p gnus-summary-buffer)
136              (with-current-buffer gnus-summary-buffer
137                gnus-pick-mode))
138     (message-add-action
139      `(gnus-configure-windows ,gnus-current-window-configuration t)
140      'send 'exit 'postpone 'kill)))
141
142 (defvar gnus-pick-line-number 1)
143 (defun gnus-pick-line-number ()
144   "Return the current line number."
145   (if (bobp)
146       (setq gnus-pick-line-number 1)
147     (incf gnus-pick-line-number)))
148
149 (defun gnus-pick-start-reading (&optional catch-up)
150   "Start reading the picked articles.
151 If given a prefix, mark all unpicked articles as read."
152   (interactive "P")
153   (if gnus-newsgroup-processable
154       (progn
155         (gnus-summary-limit-to-articles nil)
156         (when (or catch-up gnus-mark-unpicked-articles-as-read)
157           (gnus-summary-limit-mark-excluded-as-read))
158         (gnus-summary-first-article)
159         (gnus-configure-windows
160          (if gnus-pick-display-summary 'article 'pick) t))
161     (if gnus-pick-elegant-flow
162         (progn
163           (when (or catch-up gnus-mark-unpicked-articles-as-read)
164             (gnus-summary-catchup nil t))
165           (if (gnus-group-quit-config gnus-newsgroup-name)
166               (gnus-summary-exit)
167             (gnus-summary-next-group)))
168       (error "No articles have been picked"))))
169
170 (defun gnus-pick-goto-article (arg)
171   "Go to the article number indicated by ARG.
172 If ARG is an invalid article number, then stay on current line."
173   (let (pos)
174     (save-excursion
175       (goto-char (point-min))
176       (when (zerop (forward-line (1- (prefix-numeric-value arg))))
177         (setq pos (point))))
178     (if (not pos)
179         (gnus-error 2 "No such line: %s" arg)
180       (goto-char pos))))
181
182 (defun gnus-pick-article (&optional arg)
183   "Pick the article on the current line.
184 If ARG, pick the article on that line instead."
185   (interactive "P")
186   (when arg
187     (gnus-pick-goto-article arg))
188   (gnus-summary-mark-as-processable 1))
189
190 (defun gnus-pick-article-or-thread (&optional arg)
191   "If `gnus-thread-hide-subtree' is t, then pick the thread on the current line.
192 Otherwise pick the article on the current line.
193 If ARG, pick the article/thread on that line instead."
194   (interactive "P")
195   (when arg
196     (gnus-pick-goto-article arg))
197   (if gnus-thread-hide-subtree
198       (progn
199         (save-excursion
200           (gnus-uu-mark-thread))
201         (forward-line 1))
202     (gnus-summary-mark-as-processable 1)))
203
204 (defun gnus-pick-unmark-article-or-thread (&optional arg)
205   "If `gnus-thread-hide-subtree' is t, then unmark the thread on current line.
206 Otherwise unmark the article on current line.
207 If ARG, unmark thread/article on that line instead."
208   (interactive "P")
209   (when arg
210     (gnus-pick-goto-article arg))
211   (if gnus-thread-hide-subtree
212       (save-excursion
213         (gnus-uu-unmark-thread))
214     (gnus-summary-unmark-as-processable 1)))
215
216 (defun gnus-pick-mouse-pick (e)
217   (interactive "e")
218   (mouse-set-point e)
219   (save-excursion
220     (gnus-summary-mark-as-processable 1)))
221
222 (defun gnus-pick-mouse-pick-region (start-event)
223   "Pick articles that the mouse is dragged over.
224 This must be bound to a button-down mouse event."
225   (interactive "e")
226   (mouse-minibuffer-check start-event)
227   (let* ((echo-keystrokes 0)
228          (start-posn (event-start start-event))
229          (start-point (posn-point start-posn))
230          (start-line (1+ (count-lines (point-min) start-point)))
231          (start-window (posn-window start-posn))
232          (bounds (gnus-window-edges start-window))
233          (top (nth 1 bounds))
234          (bottom (if (window-minibuffer-p start-window)
235                      (nth 3 bounds)
236                    ;; Don't count the mode line.
237                    (1- (nth 3 bounds))))
238          (click-count (1- (event-click-count start-event))))
239     (setq mouse-selection-click-count click-count)
240     (setq mouse-selection-click-count-buffer (current-buffer))
241     (mouse-set-point start-event)
242    ;; In case the down click is in the middle of some intangible text,
243     ;; use the end of that text, and put it in START-POINT.
244     (when (< (point) start-point)
245       (goto-char start-point))
246     (gnus-pick-article)
247     (setq start-point (point))
248     ;; end-of-range is used only in the single-click case.
249     ;; It is the place where the drag has reached so far
250     ;; (but not outside the window where the drag started).
251     (let (event end end-point (end-of-range (point)))
252       (track-mouse
253         (while (progn
254                  (setq event (cdr (gnus-read-event-char)))
255                  (or (mouse-movement-p event)
256                      (eq (car-safe event) 'switch-frame)))
257           (if (eq (car-safe event) 'switch-frame)
258               nil
259             (setq end (event-end event)
260                   end-point (posn-point end))
261
262             (cond
263              ;; Are we moving within the original window?
264              ((and (eq (posn-window end) start-window)
265                    (integer-or-marker-p end-point))
266               ;; Go to START-POINT first, so that when we move to END-POINT,
267               ;; if it's in the middle of intangible text,
268               ;; point jumps in the direction away from START-POINT.
269               (goto-char start-point)
270               (goto-char end-point)
271               (gnus-pick-article)
272               ;; In case the user moved his mouse really fast, pick
273               ;; articles on the line between this one and the last one.
274               (let* ((this-line (1+ (count-lines (point-min) end-point)))
275                      (min-line (min this-line start-line))
276                      (max-line (max this-line start-line)))
277                 (while (< min-line max-line)
278                   (goto-char (point-min))
279                   (forward-line (1- min-line))
280                   (gnus-pick-article)
281                   (setq min-line (1+ min-line)))
282                 (setq start-line this-line))
283               (when (zerop (% click-count 3))
284                 (setq end-of-range (point))))
285              (t
286               (let ((mouse-row (cdr (cdr (mouse-position)))))
287                 (cond
288                  ((null mouse-row))
289                  ((< mouse-row top)
290                   (mouse-scroll-subr start-window (- mouse-row top)))
291                  ((>= mouse-row bottom)
292                   (mouse-scroll-subr start-window
293                                      (1+ (- mouse-row bottom)))))))))))
294       (when (consp event)
295         (let ((fun (key-binding (vector (car event)))))
296           ;; Run the binding of the terminating up-event, if possible.
297        ;; In the case of a multiple click, it gives the wrong results,
298           ;; because it would fail to set up a region.
299           (when nil
300       ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
301        ;; In this case, we can just let the up-event execute normally.
302             (let ((end (event-end event)))
303               ;; Set the position in the event before we replay it,
304               ;; because otherwise it may have a position in the wrong
305               ;; buffer.
306               (setcar (cdr end) end-of-range)
307               ;; Delete the overlay before calling the function,
308              ;; because delete-overlay increases buffer-modified-tick.
309               (push event unread-command-events))))))))
310
311 (defun gnus-pick-next-page ()
312   "Go to the next page.  If at the end of the buffer, start reading articles."
313   (interactive)
314   (let ((scroll-in-place nil))
315     (condition-case nil
316         (scroll-up)
317       (end-of-buffer (gnus-pick-start-reading)))))
318
319 ;;;
320 ;;; gnus-binary-mode
321 ;;;
322
323 (defvar gnus-binary-mode-hook nil
324   "Hook run in summary binary mode buffers.")
325
326 (defvar gnus-binary-mode-map
327   (let ((map (make-sparse-keymap)))
328     (gnus-define-keys map
329       "g" gnus-binary-show-article)
330     map))
331
332 (defun gnus-binary-make-menu-bar ()
333   (unless (boundp 'gnus-binary-menu)
334     (easy-menu-define
335       gnus-binary-menu gnus-binary-mode-map ""
336       '("Pick"
337         ["Switch binary mode off" gnus-binary-mode t]))))
338
339 (eval-when-compile
340   (when (featurep 'xemacs)
341     (defvar gnus-binary-mode-on-hook)
342     (defvar gnus-binary-mode-off-hook)))
343
344 (define-minor-mode gnus-binary-mode
345   "Minor mode for providing a binary group interface in Gnus summary buffers."
346   :lighter " Binary" :keymap gnus-binary-mode-map
347   (cond
348    ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-binary-mode nil))
349    (gnus-binary-mode
350     ;; Make sure that we don't select any articles upon group entry.
351     (make-local-variable 'gnus-auto-select-first)
352     (setq gnus-auto-select-first nil)
353     (make-local-variable 'gnus-summary-display-article-function)
354     (setq gnus-summary-display-article-function 'gnus-binary-display-article)
355     ;; Set up the menu.
356     (when (gnus-visual-p 'binary-menu 'menu)
357       (gnus-binary-make-menu-bar)))))
358
359 (defun gnus-binary-display-article (article &optional all-header)
360   "Run ARTICLE through the binary decode functions."
361   (when (gnus-summary-goto-subject article)
362     (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
363       (gnus-uu-decode-uu))))
364
365 (defun gnus-binary-show-article (&optional arg)
366   "Bypass the binary functions and show the article."
367   (interactive "P")
368   (let (gnus-summary-display-article-function)
369     (gnus-summary-show-article arg)))
370
371 ;;;
372 ;;; gnus-tree-mode
373 ;;;
374
375 (defcustom gnus-tree-line-format "%(%[%3,3n%]%)"
376   "Format of tree elements."
377   :type 'string
378   :group 'gnus-summary-tree)
379
380 (defcustom gnus-tree-minimize-window t
381   "If non-nil, minimize the tree buffer window.
382 If a number, never let the tree buffer grow taller than that number of
383 lines."
384   :type '(choice boolean
385                  integer)
386   :group 'gnus-summary-tree)
387
388 (defcustom gnus-selected-tree-face 'modeline
389   "*Face used for highlighting selected articles in the thread tree."
390   :type 'face
391   :group 'gnus-summary-tree)
392
393 (defvar gnus-tree-brackets '((?\[ . ?\]) (?\( . ?\))
394                              (?\{ . ?\}) (?< . ?>))
395   "Brackets used in tree nodes.")
396
397 (defvar gnus-tree-parent-child-edges '(?- ?\\ ?|)
398   "Characters used to connect parents with children.")
399
400 (defcustom gnus-tree-mode-line-format "Gnus: %%b %S %Z"
401   "*The format specification for the tree mode line."
402   :type 'string
403   :group 'gnus-summary-tree)
404
405 (defcustom gnus-generate-tree-function 'gnus-generate-vertical-tree
406   "*Function for generating a thread tree.
407 Two predefined functions are available:
408 `gnus-generate-horizontal-tree' and `gnus-generate-vertical-tree'."
409   :type '(radio (function-item gnus-generate-vertical-tree)
410                 (function-item gnus-generate-horizontal-tree)
411                 (function :tag "Other" nil))
412   :group 'gnus-summary-tree)
413
414 (defcustom gnus-tree-mode-hook nil
415   "*Hook run in tree mode buffers."
416   :type 'hook
417   :group 'gnus-summary-tree)
418
419 (when (featurep 'xemacs)
420   (add-hook 'gnus-tree-mode-hook 'gnus-xmas-tree-menu-add)
421   (add-hook 'gnus-tree-mode-hook 'gnus-xmas-switch-horizontal-scrollbar-off))
422
423
424 ;;; Internal variables.
425
426 (defvar gnus-tree-line-format-alist
427   `((?n gnus-tmp-name ?s)
428     (?f gnus-tmp-from ?s)
429     (?N gnus-tmp-number ?d)
430     (?\[ gnus-tmp-open-bracket ?c)
431     (?\] gnus-tmp-close-bracket ?c)
432     (?s gnus-tmp-subject ?s)))
433
434 (defvar gnus-tree-mode-line-format-alist gnus-summary-mode-line-format-alist)
435
436 (defvar gnus-tree-mode-line-format-spec nil)
437 (defvar gnus-tree-line-format-spec nil)
438
439 (defvar gnus-tree-node-length nil)
440 (defvar gnus-selected-tree-overlay nil)
441
442 (defvar gnus-tree-displayed-thread nil)
443 (defvar gnus-tree-inhibit nil)
444
445 (defvar gnus-tree-mode-map nil)
446 (put 'gnus-tree-mode 'mode-class 'special)
447
448 (unless gnus-tree-mode-map
449   (setq gnus-tree-mode-map (make-keymap))
450   (suppress-keymap gnus-tree-mode-map)
451   (gnus-define-keys
452       gnus-tree-mode-map
453     "\r" gnus-tree-select-article
454     gnus-mouse-2 gnus-tree-pick-article
455     "\C-?" gnus-tree-read-summary-keys
456     "h" gnus-tree-show-summary
457
458     "\C-c\C-i" gnus-info-find-node)
459
460   (substitute-key-definition
461    'undefined 'gnus-tree-read-summary-keys gnus-tree-mode-map))
462
463 (defun gnus-tree-make-menu-bar ()
464   (unless (boundp 'gnus-tree-menu)
465     (easy-menu-define
466       gnus-tree-menu gnus-tree-mode-map ""
467       '("Tree"
468         ["Select article" gnus-tree-select-article t]))))
469
470 (defun gnus-tree-mode ()
471   "Major mode for displaying thread trees."
472   (interactive)
473   (gnus-set-format 'tree-mode)
474   (gnus-set-format 'tree t)
475   (when (gnus-visual-p 'tree-menu 'menu)
476     (gnus-tree-make-menu-bar))
477   (kill-all-local-variables)
478   (gnus-simplify-mode-line)
479   (setq mode-name "Tree")
480   (setq major-mode 'gnus-tree-mode)
481   (use-local-map gnus-tree-mode-map)
482   (buffer-disable-undo)
483   (setq buffer-read-only t)
484   (setq truncate-lines t)
485   (save-excursion
486     (gnus-set-work-buffer)
487     (gnus-tree-node-insert (make-mail-header "") nil)
488     (setq gnus-tree-node-length (1- (point))))
489   (gnus-run-mode-hooks 'gnus-tree-mode-hook))
490
491 (defun gnus-tree-read-summary-keys (&optional arg)
492   "Read a summary buffer key sequence and execute it."
493   (interactive "P")
494   (unless gnus-tree-inhibit
495     (let ((buf (current-buffer))
496           (gnus-tree-inhibit t)
497           win)
498       (set-buffer gnus-article-buffer)
499       (gnus-article-read-summary-keys arg nil t)
500       (when (setq win (get-buffer-window buf))
501         (select-window win)
502         (when gnus-selected-tree-overlay
503           (goto-char (or (gnus-overlay-end gnus-selected-tree-overlay) 1)))
504         (gnus-tree-minimize)))))
505
506 (defun gnus-tree-show-summary ()
507   "Reconfigure windows to show summary buffer."
508   (interactive)
509   (if (not (gnus-buffer-live-p gnus-summary-buffer))
510       (error "There is no summary buffer for this tree buffer")
511     (gnus-configure-windows 'article)
512     (gnus-summary-goto-subject gnus-current-article)))
513
514 (defun gnus-tree-select-article (article)
515   "Select the article under point, if any."
516   (interactive (list (gnus-tree-article-number)))
517   (let ((buf (current-buffer)))
518     (when article
519       (with-current-buffer gnus-summary-buffer
520         (gnus-summary-goto-article article))
521       (select-window (get-buffer-window buf)))))
522
523 (defun gnus-tree-pick-article (e)
524   "Select the article under the mouse pointer."
525   (interactive "e")
526   (mouse-set-point e)
527   (gnus-tree-select-article (gnus-tree-article-number)))
528
529 (defun gnus-tree-article-number ()
530   (get-text-property (point) 'gnus-number))
531
532 (defun gnus-tree-article-region (article)
533   "Return a cons with BEG and END of the article region."
534   (let ((pos (text-property-any
535               (point-min) (point-max) 'gnus-number article)))
536     (when pos
537       (cons pos (next-single-property-change pos 'gnus-number)))))
538
539 (defun gnus-tree-goto-article (article)
540   (let ((pos (text-property-any
541               (point-min) (point-max) 'gnus-number article)))
542     (when pos
543       (goto-char pos))))
544
545 (defun gnus-tree-recenter ()
546   "Center point in the tree window."
547   (let ((selected (selected-window))
548         (tree-window (gnus-get-buffer-window gnus-tree-buffer t)))
549     (when tree-window
550       (select-window tree-window)
551       (when gnus-selected-tree-overlay
552         (goto-char (or (gnus-overlay-end gnus-selected-tree-overlay) 1)))
553       (let* ((top (cond ((< (window-height) 4) 0)
554                         ((< (window-height) 7) 1)
555                         (t 2)))
556              (height (1- (window-height)))
557              (bottom (save-excursion (goto-char (point-max))
558                                      (forward-line (- height))
559                                      (point))))
560       ;; Set the window start to either `bottom', which is the biggest
561         ;; possible valid number, or the second line from the top,
562         ;; whichever is the least.
563         (set-window-start
564          tree-window (min bottom (save-excursion
565                                    (forward-line (- top)) (point)))))
566       (select-window selected))))
567
568 (defun gnus-get-tree-buffer ()
569   "Return the tree buffer properly initialized."
570   (with-current-buffer (gnus-get-buffer-create gnus-tree-buffer)
571     (unless (eq major-mode 'gnus-tree-mode)
572       (gnus-tree-mode))
573     (current-buffer)))
574
575 (defun gnus-tree-minimize ()
576   (when (and gnus-tree-minimize-window
577              (not (one-window-p)))
578     (let ((windows 0)
579           tot-win-height)
580       (walk-windows (lambda (window) (incf windows)))
581       (setq tot-win-height
582             (- (frame-height)
583                (* window-min-height (1- windows))
584                2))
585       (let* ((window-min-height 2)
586              (height (count-lines (point-min) (point-max)))
587              (min (max (1- window-min-height) height))
588              (tot (if (numberp gnus-tree-minimize-window)
589                       (min gnus-tree-minimize-window min)
590                     min))
591              (win (get-buffer-window (current-buffer)))
592              (wh (and win (1- (window-height win)))))
593         (setq tot (min tot tot-win-height))
594         (when (and win
595                    (not (eq tot wh)))
596           (let ((selected (selected-window)))
597             (when (ignore-errors (select-window win))
598               (enlarge-window (- tot wh))
599               (select-window selected))))))))
600
601 ;;; Generating the tree.
602
603 (defun gnus-tree-node-insert (header sparse &optional adopted)
604   (let* ((dummy (stringp header))
605          (header (if (vectorp header) header
606                    (progn
607                      (setq header (make-mail-header "*****"))
608                      (mail-header-set-number header 0)
609                      (mail-header-set-lines header 0)
610                      (mail-header-set-chars header 0)
611                      header)))
612          (gnus-tmp-from (mail-header-from header))
613          (gnus-tmp-subject (mail-header-subject header))
614          (gnus-tmp-number (mail-header-number header))
615          (gnus-tmp-name
616           (cond
617            ((string-match "(.+)" gnus-tmp-from)
618             (substring gnus-tmp-from
619                        (1+ (match-beginning 0)) (1- (match-end 0))))
620            ((string-match "<[^>]+> *$" gnus-tmp-from)
621             (let ((beg (match-beginning 0)))
622               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
623                        (substring gnus-tmp-from (1+ (match-beginning 0))
624                                   (1- (match-end 0))))
625                   (substring gnus-tmp-from 0 beg))))
626            ((memq gnus-tmp-number sparse)
627             "***")
628            (t gnus-tmp-from)))
629          (gnus-tmp-open-bracket
630           (cond ((memq gnus-tmp-number sparse)
631                  (caadr gnus-tree-brackets))
632                 (dummy (caaddr gnus-tree-brackets))
633                 (adopted (car (nth 3 gnus-tree-brackets)))
634                 (t (caar gnus-tree-brackets))))
635          (gnus-tmp-close-bracket
636           (cond ((memq gnus-tmp-number sparse)
637                  (cdadr gnus-tree-brackets))
638                 (adopted (cdr (nth 3 gnus-tree-brackets)))
639                 (dummy
640                  (cdaddr gnus-tree-brackets))
641                 (t (cdar gnus-tree-brackets))))
642          (buffer-read-only nil)
643          beg end)
644     (gnus-add-text-properties
645      (setq beg (point))
646      (setq end (progn (eval gnus-tree-line-format-spec) (point)))
647      (list 'gnus-number gnus-tmp-number))
648     (when (or t (gnus-visual-p 'tree-highlight 'highlight))
649       (gnus-tree-highlight-node gnus-tmp-number beg end))))
650
651 (defun gnus-tree-highlight-node (article beg end)
652   "Highlight current line according to `gnus-summary-highlight'."
653   (let ((list gnus-summary-highlight)
654         face)
655     (with-current-buffer gnus-summary-buffer
656       (let* ((score (or (cdr (assq article gnus-newsgroup-scored))
657                         gnus-summary-default-score 0))
658              (default gnus-summary-default-score)
659              (default-high gnus-summary-default-high-score)
660              (default-low gnus-summary-default-low-score)
661              (uncached (memq article gnus-newsgroup-undownloaded))
662              (downloaded (not uncached))
663              (mark (or (gnus-summary-article-mark article) gnus-unread-mark)))
664         ;; Eval the cars of the lists until we find a match.
665         (while (and list
666                     (not (eval (caar list))))
667           (setq list (cdr list)))))
668     (unless (eq (setq face (cdar list)) (get-text-property beg 'face))
669       (gnus-put-text-property-excluding-characters-with-faces
670        beg end 'face
671        (if (boundp face) (symbol-value face) face)))))
672
673 (defun gnus-tree-indent (level)
674   (insert (make-string (1- (* (1+ gnus-tree-node-length) level)) ? )))
675
676 (defvar gnus-tmp-limit)
677 (defvar gnus-tmp-sparse)
678 (defvar gnus-tmp-indent)
679
680 (defun gnus-generate-tree (thread)
681   "Generate a thread tree for THREAD."
682   (with-current-buffer (gnus-get-tree-buffer)
683     (let ((buffer-read-only nil)
684           (gnus-tmp-indent 0))
685       (erase-buffer)
686       (funcall gnus-generate-tree-function thread 0)
687       (gnus-set-mode-line 'tree)
688       (goto-char (point-min))
689       (gnus-tree-minimize)
690       (gnus-tree-recenter)
691       (let ((selected (selected-window)))
692         (when (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t)
693           (select-window (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t))
694           (gnus-horizontal-recenter)
695           (select-window selected))))))
696
697 (defun gnus-generate-horizontal-tree (thread level &optional dummyp adopted)
698   "Generate a horizontal tree."
699   (let* ((dummy (stringp (car thread)))
700          (do (or dummy
701                  (and (car thread)
702                       (memq (mail-header-number (car thread))
703                             gnus-tmp-limit))))
704          col beg)
705     (if (not do)
706         ;; We don't want this article.
707         (setq thread (cdr thread))
708       (if (not (bolp))
709           ;; Not the first article on the line, so we insert a "-".
710           (insert (car gnus-tree-parent-child-edges))
711         ;; If the level isn't zero, then we insert some indentation.
712         (unless (zerop level)
713           (gnus-tree-indent level)
714           (insert (cadr gnus-tree-parent-child-edges))
715           (setq col (- (setq beg (point)) (point-at-bol) 1))
716           ;; Draw "|" lines upwards.
717           (while (progn
718                    (forward-line -1)
719                    (forward-char col)
720                    (eq (char-after) ? ))
721             (delete-char 1)
722             (insert (caddr gnus-tree-parent-child-edges)))
723           (goto-char beg)))
724       (setq dummyp nil)
725       ;; Insert the article node.
726       (gnus-tree-node-insert (pop thread) gnus-tmp-sparse adopted))
727     (if (null thread)
728         ;; End of the thread, so we go to the next line.
729         (unless (bolp)
730           (insert "\n"))
731       ;; Recurse downwards in all children of this article.
732       (while thread
733         (gnus-generate-horizontal-tree
734          (pop thread) (if do (1+ level) level)
735          (or dummyp dummy) dummy)))))
736
737 (defsubst gnus-tree-indent-vertical ()
738   (let ((len (- (* (1+ gnus-tree-node-length) gnus-tmp-indent)
739                 (- (point) (point-at-bol)))))
740     (when (> len 0)
741       (insert (make-string len ? )))))
742
743 (defsubst gnus-tree-forward-line (n)
744   (while (>= (decf n) 0)
745     (unless (zerop (forward-line 1))
746       (end-of-line)
747       (insert "\n")))
748   (end-of-line))
749
750 (defun gnus-generate-vertical-tree (thread level &optional dummyp adopted)
751   "Generate a vertical tree."
752   (let* ((dummy (stringp (car thread)))
753          (do (or dummy
754                  (and (car thread)
755                       (memq (mail-header-number (car thread))
756                             gnus-tmp-limit))))
757          beg)
758     (if (not do)
759         ;; We don't want this article.
760         (setq thread (cdr thread))
761       (if (not (save-excursion (beginning-of-line) (bobp)))
762           ;; Not the first article on the line, so we insert a "-".
763           (progn
764             (gnus-tree-indent-vertical)
765             (insert (make-string (/ gnus-tree-node-length 2) ? ))
766             (insert (caddr gnus-tree-parent-child-edges))
767             (gnus-tree-forward-line 1))
768         ;; If the level isn't zero, then we insert some indentation.
769         (unless (zerop gnus-tmp-indent)
770           (gnus-tree-forward-line (1- (* 2 level)))
771           (gnus-tree-indent-vertical)
772           (delete-char -1)
773           (insert (cadr gnus-tree-parent-child-edges))
774           (setq beg (point))
775           (forward-char -1)
776           ;; Draw "-" lines leftwards.
777           (while (and (not (bobp))
778                       (eq (char-after (1- (point))) ? ))
779             (delete-char -1)
780             (insert (car gnus-tree-parent-child-edges))
781             (forward-char -1))
782           (goto-char beg)
783           (gnus-tree-forward-line 1)))
784       (setq dummyp nil)
785       ;; Insert the article node.
786       (gnus-tree-indent-vertical)
787       (gnus-tree-node-insert (pop thread) gnus-tmp-sparse adopted)
788       (gnus-tree-forward-line 1))
789     (if (null thread)
790         ;; End of the thread, so we go to the next line.
791         (progn
792           (goto-char (point-min))
793           (end-of-line)
794           (incf gnus-tmp-indent))
795       ;; Recurse downwards in all children of this article.
796       (while thread
797         (gnus-generate-vertical-tree
798          (pop thread) (if do (1+ level) level)
799          (or dummyp dummy) dummy)))))
800
801 ;;; Interface functions.
802
803 (defun gnus-possibly-generate-tree (article &optional force)
804   "Generate the thread tree for ARTICLE if it isn't displayed already."
805   (when (with-current-buffer gnus-summary-buffer
806           (and gnus-use-trees
807                gnus-show-threads
808                (vectorp (gnus-summary-article-header article))))
809     (save-excursion
810       (let ((top (with-current-buffer gnus-summary-buffer
811                    (gnus-cut-thread
812                     (gnus-remove-thread
813                      (mail-header-id
814                       (gnus-summary-article-header article))
815                      t))))
816             (gnus-tmp-limit gnus-newsgroup-limit)
817             (gnus-tmp-sparse gnus-newsgroup-sparse))
818         (when (or force
819                   (not (eq top gnus-tree-displayed-thread)))
820           (gnus-generate-tree top)
821           (setq gnus-tree-displayed-thread top))))))
822
823 (defun gnus-tree-open (group)
824   (gnus-get-tree-buffer))
825
826 (defun gnus-tree-close (group)
827   (gnus-kill-buffer gnus-tree-buffer))
828
829 (defun gnus-tree-perhaps-minimize ()
830   (when (and gnus-tree-minimize-window
831              (get-buffer gnus-tree-buffer))
832     (with-current-buffer gnus-tree-buffer
833       (gnus-tree-minimize))))
834
835 (defun gnus-highlight-selected-tree (article)
836   "Highlight the selected article in the tree."
837   (let ((buf (current-buffer))
838         region)
839     (set-buffer gnus-tree-buffer)
840     (when (setq region (gnus-tree-article-region article))
841       (when (or (not gnus-selected-tree-overlay)
842                 (gnus-extent-detached-p gnus-selected-tree-overlay))
843         ;; Create a new overlay.
844         (gnus-overlay-put
845          (setq gnus-selected-tree-overlay
846                (gnus-make-overlay (point-min) (1+ (point-min))))
847          'face gnus-selected-tree-face))
848       ;; Move the overlay to the article.
849       (gnus-move-overlay
850        gnus-selected-tree-overlay (goto-char (car region)) (cdr region))
851       (gnus-tree-minimize)
852       (gnus-tree-recenter)
853       (let ((selected (selected-window)))
854         (when (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t)
855           (select-window (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t))
856           (gnus-horizontal-recenter)
857           (select-window selected))))
858 ;; If we remove this save-excursion, it updates the wrong mode lines?!?
859     (with-current-buffer gnus-tree-buffer
860       (gnus-set-mode-line 'tree))
861     (set-buffer buf)))
862
863 (defun gnus-tree-highlight-article (article face)
864   (with-current-buffer (gnus-get-tree-buffer)
865     (let (region)
866       (when (setq region (gnus-tree-article-region article))
867         (gnus-put-text-property (car region) (cdr region) 'face face)
868         (set-window-point
869          (gnus-get-buffer-window (current-buffer) t) (cdr region))))))
870
871 ;;; Allow redefinition of functions.
872 (gnus-ems-redefine)
873
874 (provide 'gnus-salt)
875
876 ;;; gnus-salt.el ends here