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