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