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