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