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