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