4e3bc91a92926b9863c6d6bdd2ccb0e8ec52a414
[gnus] / lisp / gnus-salt.el
1 ;;; gnus-salt.el --- alternate summary mode interfaces for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (require 'gnus)
33 (require 'gnus-sum)
34 (require 'gnus-win)
35
36 ;;;
37 ;;; gnus-pick-mode
38 ;;;
39
40 (defvar gnus-pick-mode nil
41   "Minor mode for providing a pick-and-read interface in Gnus
42 summary buffers.")
43
44 (defcustom gnus-pick-display-summary nil
45   "*Display summary while reading."
46   :type 'boolean
47   :group 'gnus-summary-pick)
48
49 (defcustom gnus-pick-mode-hook nil
50   "Hook run in summary pick mode buffers."
51   :type 'hook
52   :group 'gnus-summary-pick)
53
54 (when (featurep 'xemacs)
55   (add-hook 'gnus-pick-mode-hook 'gnus-xmas-pick-menu-add))
56
57 (defcustom gnus-mark-unpicked-articles-as-read nil
58   "*If non-nil, mark all unpicked articles as read."
59   :type 'boolean
60   :group 'gnus-summary-pick)
61
62 (defcustom gnus-pick-elegant-flow t
63   "If non-nil, `gnus-pick-start-reading' runs
64  `gnus-summary-next-group' when no articles have been picked."
65   :type 'boolean
66   :group 'gnus-summary-pick)
67
68 (defcustom gnus-summary-pick-line-format
69   "%-5P %U\%R\%z\%I\%(%[%4L: %-23,23n%]%) %s\n"
70   "*The format specification of the lines in pick buffers.
71 It accepts the same format specs that `gnus-summary-line-format' does."
72   :type 'string
73   :group 'gnus-summary-pick)
74
75 ;;; Internal variables.
76
77 (defvar gnus-pick-mode-map nil)
78
79 (unless gnus-pick-mode-map
80   (setq gnus-pick-mode-map (make-sparse-keymap))
81
82   (gnus-define-keys gnus-pick-mode-map
83     " " gnus-pick-next-page
84     "u" gnus-pick-unmark-article-or-thread
85     "." gnus-pick-article-or-thread
86     gnus-down-mouse-2 gnus-pick-mouse-pick-region
87     "\r" gnus-pick-start-reading))
88
89 (defun gnus-pick-make-menu-bar ()
90   (unless (boundp 'gnus-pick-menu)
91     (easy-menu-define
92       gnus-pick-menu gnus-pick-mode-map ""
93       '("Pick"
94         ("Pick"
95          ["Article" gnus-summary-mark-as-processable t]
96          ["Thread" gnus-uu-mark-thread t]
97          ["Region" gnus-uu-mark-region t]
98          ["Regexp" gnus-uu-mark-by-regexp t]
99          ["Buffer" gnus-uu-mark-buffer t])
100         ("Unpick"
101          ["Article" gnus-summary-unmark-as-processable t]
102          ["Thread" gnus-uu-unmark-thread t]
103          ["Region" gnus-uu-unmark-region t]
104          ["Regexp" gnus-uu-unmark-by-regexp t]
105          ["Buffer" gnus-summary-unmark-all-processable t])
106         ["Start reading" gnus-pick-start-reading t]
107         ["Switch pick mode off" gnus-pick-mode gnus-pick-mode]))))
108
109 (defun gnus-pick-mode (&optional arg)
110   "Minor mode for providing a pick-and-read interface in Gnus summary buffers.
111
112 \\{gnus-pick-mode-map}"
113   (interactive "P")
114   (when (eq major-mode 'gnus-summary-mode)
115     (if (not (set (make-local-variable 'gnus-pick-mode)
116                   (if (null arg) (not gnus-pick-mode)
117                     (> (prefix-numeric-value arg) 0))))
118         (remove-hook 'gnus-message-setup-hook 'gnus-pick-setup-message)
119       ;; Make sure that we don't select any articles upon group entry.
120       (set (make-local-variable 'gnus-auto-select-first) nil)
121       ;; Change line format.
122       (setq gnus-summary-line-format gnus-summary-pick-line-format)
123       (setq gnus-summary-line-format-spec nil)
124       (gnus-update-format-specifications nil 'summary)
125       (gnus-update-summary-mark-positions)
126       (add-hook 'gnus-message-setup-hook 'gnus-pick-setup-message)
127       (set (make-local-variable 'gnus-summary-goto-unread) 'never)
128       ;; Set up the menu.
129       (when (gnus-visual-p 'pick-menu 'menu)
130         (gnus-pick-make-menu-bar))
131       (add-minor-mode 'gnus-pick-mode " Pick" gnus-pick-mode-map)
132       (gnus-run-hooks 'gnus-pick-mode-hook))))
133
134 (defun gnus-pick-setup-message ()
135   "Make Message do the right thing on exit."
136   (when (and (gnus-buffer-live-p gnus-summary-buffer)
137              (with-current-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 (point-min) 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 (point-min) end-point)))
276                      (min-line (min this-line start-line))
277                      (max-line (max this-line start-line)))
278                 ;; Why not use `forward-line'?  --Stef
279                 (while (< min-line max-line)
280                   (goto-line min-line)
281                   (gnus-pick-article)
282                   (setq min-line (1+ min-line)))
283                 (setq start-line this-line))
284               (when (zerop (% click-count 3))
285                 (setq end-of-range (point))))
286              (t
287               (let ((mouse-row (cdr (cdr (mouse-position)))))
288                 (cond
289                  ((null mouse-row))
290                  ((< mouse-row top)
291                   (mouse-scroll-subr start-window (- mouse-row top)))
292                  ((>= mouse-row bottom)
293                   (mouse-scroll-subr start-window
294                                      (1+ (- mouse-row bottom)))))))))))
295       (when (consp event)
296         (let ((fun (key-binding (vector (car event)))))
297           ;; Run the binding of the terminating up-event, if possible.
298        ;; In the case of a multiple click, it gives the wrong results,
299           ;; because it would fail to set up a region.
300           (when nil
301       ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
302        ;; In this case, we can just let the up-event execute normally.
303             (let ((end (event-end event)))
304               ;; Set the position in the event before we replay it,
305               ;; because otherwise it may have a position in the wrong
306               ;; buffer.
307               (setcar (cdr end) end-of-range)
308               ;; Delete the overlay before calling the function,
309              ;; because delete-overlay increases buffer-modified-tick.
310               (push event unread-command-events))))))))
311
312 (defun gnus-pick-next-page ()
313   "Go to the next page.  If at the end of the buffer, start reading articles."
314   (interactive)
315   (let ((scroll-in-place nil))
316     (condition-case nil
317         (scroll-up)
318       (end-of-buffer (gnus-pick-start-reading)))))
319
320 ;;;
321 ;;; gnus-binary-mode
322 ;;;
323
324 (defvar gnus-binary-mode nil
325   "Minor mode for providing a binary group interface in Gnus summary buffers.")
326
327 (defvar gnus-binary-mode-hook nil
328   "Hook run in summary binary mode buffers.")
329
330 (defvar gnus-binary-mode-map nil)
331
332 (unless gnus-binary-mode-map
333   (setq gnus-binary-mode-map (make-sparse-keymap))
334
335   (gnus-define-keys
336       gnus-binary-mode-map
337     "g" gnus-binary-show-article))
338
339 (defun gnus-binary-make-menu-bar ()
340   (unless (boundp 'gnus-binary-menu)
341     (easy-menu-define
342       gnus-binary-menu gnus-binary-mode-map ""
343       '("Pick"
344         ["Switch binary mode off" gnus-binary-mode t]))))
345
346 (defun gnus-binary-mode (&optional arg)
347   "Minor mode for providing a binary group interface in Gnus summary buffers."
348   (interactive "P")
349   (when (eq major-mode 'gnus-summary-mode)
350     (make-local-variable 'gnus-binary-mode)
351     (setq gnus-binary-mode
352           (if (null arg) (not gnus-binary-mode)
353             (> (prefix-numeric-value arg) 0)))
354     (when gnus-binary-mode
355       ;; Make sure that we don't select any articles upon group entry.
356       (make-local-variable 'gnus-auto-select-first)
357       (setq gnus-auto-select-first nil)
358       (make-local-variable 'gnus-summary-display-article-function)
359       (setq gnus-summary-display-article-function 'gnus-binary-display-article)
360       ;; Set up the menu.
361       (when (gnus-visual-p 'binary-menu 'menu)
362         (gnus-binary-make-menu-bar))
363       (add-minor-mode 'gnus-binary-mode " Binary" gnus-binary-mode-map)
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 (or 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-mode-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       (with-current-buffer gnus-summary-buffer
527         (gnus-summary-goto-article article))
528       (select-window (get-buffer-window buf)))))
529
530 (defun gnus-tree-pick-article (e)
531   "Select the article under the mouse pointer."
532   (interactive "e")
533   (mouse-set-point e)
534   (gnus-tree-select-article (gnus-tree-article-number)))
535
536 (defun gnus-tree-article-number ()
537   (get-text-property (point) 'gnus-number))
538
539 (defun gnus-tree-article-region (article)
540   "Return a cons with BEG and END of the article region."
541   (let ((pos (text-property-any
542               (point-min) (point-max) 'gnus-number article)))
543     (when pos
544       (cons pos (next-single-property-change pos 'gnus-number)))))
545
546 (defun gnus-tree-goto-article (article)
547   (let ((pos (text-property-any
548               (point-min) (point-max) 'gnus-number article)))
549     (when pos
550       (goto-char pos))))
551
552 (defun gnus-tree-recenter ()
553   "Center point in the tree window."
554   (let ((selected (selected-window))
555         (tree-window (gnus-get-buffer-window gnus-tree-buffer t)))
556     (when tree-window
557       (select-window tree-window)
558       (when gnus-selected-tree-overlay
559         (goto-char (or (gnus-overlay-end gnus-selected-tree-overlay) 1)))
560       (let* ((top (cond ((< (window-height) 4) 0)
561                         ((< (window-height) 7) 1)
562                         (t 2)))
563              (height (1- (window-height)))
564              (bottom (save-excursion (goto-char (point-max))
565                                      (forward-line (- height))
566                                      (point))))
567       ;; Set the window start to either `bottom', which is the biggest
568         ;; possible valid number, or the second line from the top,
569         ;; whichever is the least.
570         (set-window-start
571          tree-window (min bottom (save-excursion
572                                    (forward-line (- top)) (point)))))
573       (select-window selected))))
574
575 (defun gnus-get-tree-buffer ()
576   "Return the tree buffer properly initialized."
577   (with-current-buffer (gnus-get-buffer-create gnus-tree-buffer)
578     (unless (eq major-mode 'gnus-tree-mode)
579       (gnus-tree-mode))
580     (current-buffer)))
581
582 (defun gnus-tree-minimize ()
583   (when (and gnus-tree-minimize-window
584              (not (one-window-p)))
585     (let ((windows 0)
586           tot-win-height)
587       (walk-windows (lambda (window) (incf windows)))
588       (setq tot-win-height
589             (- (frame-height)
590                (* window-min-height (1- windows))
591                2))
592       (let* ((window-min-height 2)
593              (height (count-lines (point-min) (point-max)))
594              (min (max (1- window-min-height) height))
595              (tot (if (numberp gnus-tree-minimize-window)
596                       (min gnus-tree-minimize-window min)
597                     min))
598              (win (get-buffer-window (current-buffer)))
599              (wh (and win (1- (window-height win)))))
600         (setq tot (min tot tot-win-height))
601         (when (and win
602                    (not (eq tot wh)))
603           (let ((selected (selected-window)))
604             (when (ignore-errors (select-window win))
605               (enlarge-window (- tot wh))
606               (select-window selected))))))))
607
608 ;;; Generating the tree.
609
610 (defun gnus-tree-node-insert (header sparse &optional adopted)
611   (let* ((dummy (stringp header))
612          (header (if (vectorp header) header
613                    (progn
614                      (setq header (make-mail-header "*****"))
615                      (mail-header-set-number header 0)
616                      (mail-header-set-lines header 0)
617                      (mail-header-set-chars header 0)
618                      header)))
619          (gnus-tmp-from (mail-header-from header))
620          (gnus-tmp-subject (mail-header-subject header))
621          (gnus-tmp-number (mail-header-number header))
622          (gnus-tmp-name
623           (cond
624            ((string-match "(.+)" gnus-tmp-from)
625             (substring gnus-tmp-from
626                        (1+ (match-beginning 0)) (1- (match-end 0))))
627            ((string-match "<[^>]+> *$" gnus-tmp-from)
628             (let ((beg (match-beginning 0)))
629               (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
630                        (substring gnus-tmp-from (1+ (match-beginning 0))
631                                   (1- (match-end 0))))
632                   (substring gnus-tmp-from 0 beg))))
633            ((memq gnus-tmp-number sparse)
634             "***")
635            (t gnus-tmp-from)))
636          (gnus-tmp-open-bracket
637           (cond ((memq gnus-tmp-number sparse)
638                  (caadr gnus-tree-brackets))
639                 (dummy (caaddr gnus-tree-brackets))
640                 (adopted (car (nth 3 gnus-tree-brackets)))
641                 (t (caar gnus-tree-brackets))))
642          (gnus-tmp-close-bracket
643           (cond ((memq gnus-tmp-number sparse)
644                  (cdadr gnus-tree-brackets))
645                 (adopted (cdr (nth 3 gnus-tree-brackets)))
646                 (dummy
647                  (cdaddr gnus-tree-brackets))
648                 (t (cdar gnus-tree-brackets))))
649          (buffer-read-only nil)
650          beg end)
651     (gnus-add-text-properties
652      (setq beg (point))
653      (setq end (progn (eval gnus-tree-line-format-spec) (point)))
654      (list 'gnus-number gnus-tmp-number))
655     (when (or t (gnus-visual-p 'tree-highlight 'highlight))
656       (gnus-tree-highlight-node gnus-tmp-number beg end))))
657
658 (defun gnus-tree-highlight-node (article beg end)
659   "Highlight current line according to `gnus-summary-highlight'."
660   (let ((list gnus-summary-highlight)
661         face)
662     (with-current-buffer gnus-summary-buffer
663       (let* ((score (or (cdr (assq article gnus-newsgroup-scored))
664                         gnus-summary-default-score 0))
665              (default gnus-summary-default-score)
666              (default-high gnus-summary-default-high-score)
667              (default-low gnus-summary-default-low-score)
668              (uncached (memq article gnus-newsgroup-undownloaded))
669              (downloaded (not uncached))
670              (mark (or (gnus-summary-article-mark article) gnus-unread-mark)))
671         ;; Eval the cars of the lists until we find a match.
672         (while (and list
673                     (not (eval (caar list))))
674           (setq list (cdr list)))))
675     (unless (eq (setq face (cdar list)) (get-text-property beg 'face))
676       (gnus-put-text-property-excluding-characters-with-faces
677        beg end 'face
678        (if (boundp face) (symbol-value face) face)))))
679
680 (defun gnus-tree-indent (level)
681   (insert (make-string (1- (* (1+ gnus-tree-node-length) level)) ? )))
682
683 (defvar gnus-tmp-limit)
684 (defvar gnus-tmp-sparse)
685 (defvar gnus-tmp-indent)
686
687 (defun gnus-generate-tree (thread)
688   "Generate a thread tree for THREAD."
689   (with-current-buffer (gnus-get-tree-buffer)
690     (let ((buffer-read-only nil)
691           (gnus-tmp-indent 0))
692       (erase-buffer)
693       (funcall gnus-generate-tree-function thread 0)
694       (gnus-set-mode-line 'tree)
695       (goto-char (point-min))
696       (gnus-tree-minimize)
697       (gnus-tree-recenter)
698       (let ((selected (selected-window)))
699         (when (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t)
700           (select-window (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t))
701           (gnus-horizontal-recenter)
702           (select-window selected))))))
703
704 (defun gnus-generate-horizontal-tree (thread level &optional dummyp adopted)
705   "Generate a horizontal tree."
706   (let* ((dummy (stringp (car thread)))
707          (do (or dummy
708                  (and (car thread)
709                       (memq (mail-header-number (car thread))
710                             gnus-tmp-limit))))
711          col beg)
712     (if (not do)
713         ;; We don't want this article.
714         (setq thread (cdr thread))
715       (if (not (bolp))
716           ;; Not the first article on the line, so we insert a "-".
717           (insert (car gnus-tree-parent-child-edges))
718         ;; If the level isn't zero, then we insert some indentation.
719         (unless (zerop level)
720           (gnus-tree-indent level)
721           (insert (cadr gnus-tree-parent-child-edges))
722           (setq col (- (setq beg (point)) (point-at-bol) 1))
723           ;; Draw "|" lines upwards.
724           (while (progn
725                    (forward-line -1)
726                    (forward-char col)
727                    (eq (char-after) ? ))
728             (delete-char 1)
729             (insert (caddr gnus-tree-parent-child-edges)))
730           (goto-char beg)))
731       (setq dummyp nil)
732       ;; Insert the article node.
733       (gnus-tree-node-insert (pop thread) gnus-tmp-sparse adopted))
734     (if (null thread)
735         ;; End of the thread, so we go to the next line.
736         (unless (bolp)
737           (insert "\n"))
738       ;; Recurse downwards in all children of this article.
739       (while thread
740         (gnus-generate-horizontal-tree
741          (pop thread) (if do (1+ level) level)
742          (or dummyp dummy) dummy)))))
743
744 (defsubst gnus-tree-indent-vertical ()
745   (let ((len (- (* (1+ gnus-tree-node-length) gnus-tmp-indent)
746                 (- (point) (point-at-bol)))))
747     (when (> len 0)
748       (insert (make-string len ? )))))
749
750 (defsubst gnus-tree-forward-line (n)
751   (while (>= (decf n) 0)
752     (unless (zerop (forward-line 1))
753       (end-of-line)
754       (insert "\n")))
755   (end-of-line))
756
757 (defun gnus-generate-vertical-tree (thread level &optional dummyp adopted)
758   "Generate a vertical tree."
759   (let* ((dummy (stringp (car thread)))
760          (do (or dummy
761                  (and (car thread)
762                       (memq (mail-header-number (car thread))
763                             gnus-tmp-limit))))
764          beg)
765     (if (not do)
766         ;; We don't want this article.
767         (setq thread (cdr thread))
768       (if (not (save-excursion (beginning-of-line) (bobp)))
769           ;; Not the first article on the line, so we insert a "-".
770           (progn
771             (gnus-tree-indent-vertical)
772             (insert (make-string (/ gnus-tree-node-length 2) ? ))
773             (insert (caddr gnus-tree-parent-child-edges))
774             (gnus-tree-forward-line 1))
775         ;; If the level isn't zero, then we insert some indentation.
776         (unless (zerop gnus-tmp-indent)
777           (gnus-tree-forward-line (1- (* 2 level)))
778           (gnus-tree-indent-vertical)
779           (delete-char -1)
780           (insert (cadr gnus-tree-parent-child-edges))
781           (setq beg (point))
782           (forward-char -1)
783           ;; Draw "-" lines leftwards.
784           (while (and (not (bobp))
785                       (eq (char-after (1- (point))) ? ))
786             (delete-char -1)
787             (insert (car gnus-tree-parent-child-edges))
788             (forward-char -1))
789           (goto-char beg)
790           (gnus-tree-forward-line 1)))
791       (setq dummyp nil)
792       ;; Insert the article node.
793       (gnus-tree-indent-vertical)
794       (gnus-tree-node-insert (pop thread) gnus-tmp-sparse adopted)
795       (gnus-tree-forward-line 1))
796     (if (null thread)
797         ;; End of the thread, so we go to the next line.
798         (progn
799           (goto-char (point-min))
800           (end-of-line)
801           (incf gnus-tmp-indent))
802       ;; Recurse downwards in all children of this article.
803       (while thread
804         (gnus-generate-vertical-tree
805          (pop thread) (if do (1+ level) level)
806          (or dummyp dummy) dummy)))))
807
808 ;;; Interface functions.
809
810 (defun gnus-possibly-generate-tree (article &optional force)
811   "Generate the thread tree for ARTICLE if it isn't displayed already."
812   (when (with-current-buffer gnus-summary-buffer
813           (and gnus-use-trees
814                gnus-show-threads
815                (vectorp (gnus-summary-article-header article))))
816     (save-excursion
817       (let ((top (with-current-buffer gnus-summary-buffer
818                    (gnus-cut-thread
819                     (gnus-remove-thread
820                      (mail-header-id
821                       (gnus-summary-article-header article))
822                      t))))
823             (gnus-tmp-limit gnus-newsgroup-limit)
824             (gnus-tmp-sparse gnus-newsgroup-sparse))
825         (when (or force
826                   (not (eq top gnus-tree-displayed-thread)))
827           (gnus-generate-tree top)
828           (setq gnus-tree-displayed-thread top))))))
829
830 (defun gnus-tree-open (group)
831   (gnus-get-tree-buffer))
832
833 (defun gnus-tree-close (group)
834   (gnus-kill-buffer gnus-tree-buffer))
835
836 (defun gnus-tree-perhaps-minimize ()
837   (when (and gnus-tree-minimize-window
838              (get-buffer gnus-tree-buffer))
839     (with-current-buffer gnus-tree-buffer
840       (gnus-tree-minimize))))
841
842 (defun gnus-highlight-selected-tree (article)
843   "Highlight the selected article in the tree."
844   (let ((buf (current-buffer))
845         region)
846     (set-buffer gnus-tree-buffer)
847     (when (setq region (gnus-tree-article-region article))
848       (when (or (not gnus-selected-tree-overlay)
849                 (gnus-extent-detached-p gnus-selected-tree-overlay))
850         ;; Create a new overlay.
851         (gnus-overlay-put
852          (setq gnus-selected-tree-overlay
853                (gnus-make-overlay (point-min) (1+ (point-min))))
854          'face gnus-selected-tree-face))
855       ;; Move the overlay to the article.
856       (gnus-move-overlay
857        gnus-selected-tree-overlay (goto-char (car region)) (cdr region))
858       (gnus-tree-minimize)
859       (gnus-tree-recenter)
860       (let ((selected (selected-window)))
861         (when (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t)
862           (select-window (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t))
863           (gnus-horizontal-recenter)
864           (select-window selected))))
865 ;; If we remove this save-excursion, it updates the wrong mode lines?!?
866     (with-current-buffer gnus-tree-buffer
867       (gnus-set-mode-line 'tree))
868     (set-buffer buf)))
869
870 (defun gnus-tree-highlight-article (article face)
871   (with-current-buffer (gnus-get-tree-buffer)
872     (let (region)
873       (when (setq region (gnus-tree-article-region article))
874         (gnus-put-text-property (car region) (cdr region) 'face face)
875         (set-window-point
876          (gnus-get-buffer-window (current-buffer) t) (cdr region))))))
877
878 ;;;
879 ;;; gnus-carpal
880 ;;;
881
882 (defvar gnus-carpal-group-buffer-buttons
883   '(("next" . gnus-group-next-unread-group)
884     ("prev" . gnus-group-prev-unread-group)
885     ("read" . gnus-group-read-group)
886     ("select" . gnus-group-select-group)
887     ("catch-up" . gnus-group-catchup-current)
888     ("new-news" . gnus-group-get-new-news-this-group)
889     ("toggle-sub" . gnus-group-unsubscribe-current-group)
890     ("subscribe" . gnus-group-unsubscribe-group)
891     ("kill" . gnus-group-kill-group)
892     ("yank" . gnus-group-yank-group)
893     ("describe" . gnus-group-describe-group)
894     "list"
895     ("subscribed" . gnus-group-list-groups)
896     ("all" . gnus-group-list-all-groups)
897     ("killed" . gnus-group-list-killed)
898     ("zombies" . gnus-group-list-zombies)
899     ("matching" . gnus-group-list-matching)
900     ("post" . gnus-group-post-news)
901     ("mail" . gnus-group-mail)
902     ("local" . (lambda () (interactive) (gnus-group-news 0)))
903     ("rescan" . gnus-group-get-new-news)
904     ("browse-foreign" . gnus-group-browse-foreign)
905     ("exit" . gnus-group-exit)))
906
907 (defvar gnus-carpal-summary-buffer-buttons
908   '("mark"
909     ("read" . gnus-summary-mark-as-read-forward)
910     ("tick" . gnus-summary-tick-article-forward)
911     ("clear" . gnus-summary-clear-mark-forward)
912     ("expirable" . gnus-summary-mark-as-expirable)
913     "move"
914     ("scroll" . gnus-summary-next-page)
915     ("next-unread" . gnus-summary-next-unread-article)
916     ("prev-unread" . gnus-summary-prev-unread-article)
917     ("first" . gnus-summary-first-unread-article)
918     ("best" . gnus-summary-best-unread-article)
919     "article"
920     ("headers" . gnus-summary-toggle-header)
921     ("uudecode" . gnus-uu-decode-uu)
922     ("enter-digest" . gnus-summary-enter-digest-group)
923     ("fetch-parent" . gnus-summary-refer-parent-article)
924     "mail"
925     ("move" . gnus-summary-move-article)
926     ("copy" . gnus-summary-copy-article)
927     ("respool" . gnus-summary-respool-article)
928     "threads"
929     ("lower" . gnus-summary-lower-thread)
930     ("kill" . gnus-summary-kill-thread)
931     "post"
932     ("post" . gnus-summary-post-news)
933     ("local" . gnus-summary-news-other-window)
934     ("mail" . gnus-summary-mail-other-window)
935     ("followup" . gnus-summary-followup-with-original)
936     ("reply" . gnus-summary-reply-with-original)
937     ("cancel" . gnus-summary-cancel-article)
938     "misc"
939     ("exit" . gnus-summary-exit)
940     ("fed-up" . gnus-summary-catchup-and-goto-next-group)))
941
942 (defvar gnus-carpal-server-buffer-buttons
943   '(("add" . gnus-server-add-server)
944     ("browse" . gnus-server-browse-server)
945     ("list" . gnus-server-list-servers)
946     ("kill" . gnus-server-kill-server)
947     ("yank" . gnus-server-yank-server)
948     ("copy" . gnus-server-copy-server)
949     ("exit" . gnus-server-exit)))
950
951 (defvar gnus-carpal-browse-buffer-buttons
952   '(("subscribe" . gnus-browse-unsubscribe-current-group)
953     ("exit" . gnus-browse-exit)))
954
955 (defvar gnus-carpal-group-buffer "*Carpal Group*")
956 (defvar gnus-carpal-summary-buffer "*Carpal Summary*")
957 (defvar gnus-carpal-server-buffer "*Carpal Server*")
958 (defvar gnus-carpal-browse-buffer "*Carpal Browse*")
959
960 (defvar gnus-carpal-attached-buffer nil)
961
962 (defvar gnus-carpal-mode-hook nil
963   "*Hook run in carpal mode buffers.")
964
965 (defvar gnus-carpal-button-face 'bold
966   "*Face used on carpal buttons.")
967
968 (defvar gnus-carpal-header-face 'bold-italic
969   "*Face used on carpal buffer headers.")
970
971 (defvar gnus-carpal-mode-map nil)
972 (put 'gnus-carpal-mode 'mode-class 'special)
973
974 (if gnus-carpal-mode-map
975     nil
976   (setq gnus-carpal-mode-map (make-keymap))
977   (suppress-keymap gnus-carpal-mode-map)
978   (define-key gnus-carpal-mode-map " " 'gnus-carpal-select)
979   (define-key gnus-carpal-mode-map "\r" 'gnus-carpal-select)
980   (define-key gnus-carpal-mode-map gnus-mouse-2 'gnus-carpal-mouse-select))
981
982 (defun gnus-carpal-mode ()
983   "Major mode for clicking buttons.
984
985 All normal editing commands are switched off.
986 \\<gnus-carpal-mode-map>
987 The following commands are available:
988
989 \\{gnus-carpal-mode-map}"
990   (interactive)
991   (kill-all-local-variables)
992   (setq mode-line-modified (cdr gnus-mode-line-modified))
993   (setq major-mode 'gnus-carpal-mode)
994   (setq mode-name "Gnus Carpal")
995   (setq mode-line-process nil)
996   (use-local-map gnus-carpal-mode-map)
997   (buffer-disable-undo)
998   (setq buffer-read-only t)
999   (make-local-variable 'gnus-carpal-attached-buffer)
1000   (gnus-run-mode-hooks 'gnus-carpal-mode-hook))
1001
1002 (defun gnus-carpal-setup-buffer (type)
1003   (let ((buffer (symbol-value (intern (format "gnus-carpal-%s-buffer" type)))))
1004     (if (get-buffer buffer)
1005         ()
1006       (with-current-buffer (gnus-get-buffer-create buffer)
1007         (gnus-carpal-mode)
1008         (setq gnus-carpal-attached-buffer
1009               (intern (format "gnus-%s-buffer" type)))
1010         (let ((buttons (symbol-value
1011                         (intern (format "gnus-carpal-%s-buffer-buttons"
1012                                         type))))
1013               (buffer-read-only nil)
1014               button)
1015           (while buttons
1016             (setq button (car buttons)
1017                   buttons (cdr buttons))
1018             (if (stringp button)
1019                 (set-text-properties
1020                  (point)
1021                  (prog2 (insert button) (point) (insert " "))
1022                  (list 'face gnus-carpal-header-face))
1023               (set-text-properties
1024                (point)
1025                (prog2 (insert (car button)) (point) (insert " "))
1026                (list 'gnus-callback (cdr button)
1027                      'face gnus-carpal-button-face
1028                      gnus-mouse-face-prop 'highlight))))
1029           (let ((fill-column (- (window-width) 2)))
1030             (fill-region (point-min) (point-max)))
1031           (set-window-point (get-buffer-window (current-buffer))
1032                             (point-min)))))))
1033
1034 (defun gnus-carpal-select ()
1035   "Select the button under point."
1036   (interactive)
1037   (let ((func (get-text-property (point) 'gnus-callback)))
1038     (if (null func)
1039         ()
1040       (pop-to-buffer (symbol-value gnus-carpal-attached-buffer))
1041       (call-interactively func))))
1042
1043 (defun gnus-carpal-mouse-select (event)
1044   "Select the button under the mouse pointer."
1045   (interactive "e")
1046   (mouse-set-point event)
1047   (gnus-carpal-select))
1048
1049 ;;; Allow redefinition of functions.
1050 (gnus-ems-redefine)
1051
1052 (provide 'gnus-salt)
1053
1054 ;; arch-tag: 35449164-77b3-4398-bcbd-a2e3e998f810
1055 ;;; gnus-salt.el ends here