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