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