gnus-salt.el 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  1. ;;; gnus-salt.el --- alternate summary mode interfaces for Gnus
  2. ;; Copyright (C) 1996-1999, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
  4. ;; Keywords: news
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (eval-when-compile (require 'cl))
  19. (eval-when-compile
  20. (when (featurep 'xemacs)
  21. (require 'easy-mmode))) ; for `define-minor-mode'
  22. (require 'gnus)
  23. (require 'gnus-sum)
  24. (require 'gnus-win)
  25. ;;;
  26. ;;; gnus-pick-mode
  27. ;;;
  28. (defcustom gnus-pick-display-summary nil
  29. "*Display summary while reading."
  30. :type 'boolean
  31. :group 'gnus-summary-pick)
  32. (defcustom gnus-pick-mode-hook nil
  33. "Hook run in summary pick mode buffers."
  34. :type 'hook
  35. :group 'gnus-summary-pick)
  36. (when (featurep 'xemacs)
  37. (add-hook 'gnus-pick-mode-hook 'gnus-xmas-pick-menu-add))
  38. (defcustom gnus-mark-unpicked-articles-as-read nil
  39. "*If non-nil, mark all unpicked articles as read."
  40. :type 'boolean
  41. :group 'gnus-summary-pick)
  42. (defcustom gnus-pick-elegant-flow t
  43. "If non-nil, `gnus-pick-start-reading' runs
  44. `gnus-summary-next-group' when no articles have been picked."
  45. :type 'boolean
  46. :group 'gnus-summary-pick)
  47. (defcustom gnus-summary-pick-line-format
  48. "%-5P %U\%R\%z\%I\%(%[%4L: %-23,23n%]%) %s\n"
  49. "*The format specification of the lines in pick buffers.
  50. It accepts the same format specs that `gnus-summary-line-format' does."
  51. :type 'string
  52. :group 'gnus-summary-pick)
  53. ;;; Internal variables.
  54. (defvar gnus-pick-mode-map
  55. (let ((map (make-sparse-keymap)))
  56. (gnus-define-keys map
  57. " " gnus-pick-next-page
  58. "u" gnus-pick-unmark-article-or-thread
  59. "." gnus-pick-article-or-thread
  60. gnus-down-mouse-2 gnus-pick-mouse-pick-region
  61. "\r" gnus-pick-start-reading)
  62. map))
  63. (defun gnus-pick-make-menu-bar ()
  64. (unless (boundp 'gnus-pick-menu)
  65. (easy-menu-define
  66. gnus-pick-menu gnus-pick-mode-map ""
  67. '("Pick"
  68. ("Pick"
  69. ["Article" gnus-summary-mark-as-processable t]
  70. ["Thread" gnus-uu-mark-thread t]
  71. ["Region" gnus-uu-mark-region t]
  72. ["Regexp" gnus-uu-mark-by-regexp t]
  73. ["Buffer" gnus-uu-mark-buffer t])
  74. ("Unpick"
  75. ["Article" gnus-summary-unmark-as-processable t]
  76. ["Thread" gnus-uu-unmark-thread t]
  77. ["Region" gnus-uu-unmark-region t]
  78. ["Regexp" gnus-uu-unmark-by-regexp t]
  79. ["Buffer" gnus-summary-unmark-all-processable t])
  80. ["Start reading" gnus-pick-start-reading t]
  81. ["Switch pick mode off" gnus-pick-mode gnus-pick-mode]))))
  82. (eval-when-compile
  83. (when (featurep 'xemacs)
  84. (defvar gnus-pick-mode-on-hook)
  85. (defvar gnus-pick-mode-off-hook)))
  86. (define-minor-mode gnus-pick-mode
  87. "Minor mode for providing a pick-and-read interface in Gnus summary buffers.
  88. \\{gnus-pick-mode-map}"
  89. :lighter " Pick" :keymap gnus-pick-mode-map
  90. (cond
  91. ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-pick-mode nil))
  92. ((not gnus-pick-mode)
  93. ;; FIXME: a buffer-local minor mode removing globally from a hook??
  94. (remove-hook 'gnus-message-setup-hook 'gnus-pick-setup-message))
  95. (t
  96. ;; Make sure that we don't select any articles upon group entry.
  97. (set (make-local-variable 'gnus-auto-select-first) nil)
  98. ;; Change line format.
  99. (setq gnus-summary-line-format gnus-summary-pick-line-format)
  100. (setq gnus-summary-line-format-spec nil)
  101. (gnus-update-format-specifications nil 'summary)
  102. (gnus-update-summary-mark-positions)
  103. ;; FIXME: a buffer-local minor mode adding globally to a hook??
  104. (add-hook 'gnus-message-setup-hook 'gnus-pick-setup-message)
  105. (set (make-local-variable 'gnus-summary-goto-unread) 'never)
  106. ;; Set up the menu.
  107. (when (gnus-visual-p 'pick-menu 'menu)
  108. (gnus-pick-make-menu-bar)))))
  109. (defun gnus-pick-setup-message ()
  110. "Make Message do the right thing on exit."
  111. (when (and (gnus-buffer-live-p gnus-summary-buffer)
  112. (with-current-buffer gnus-summary-buffer
  113. gnus-pick-mode))
  114. (message-add-action
  115. `(gnus-configure-windows ,gnus-current-window-configuration t)
  116. 'send 'exit 'postpone 'kill)))
  117. (defvar gnus-pick-line-number 1)
  118. (defun gnus-pick-line-number ()
  119. "Return the current line number."
  120. (if (bobp)
  121. (setq gnus-pick-line-number 1)
  122. (incf gnus-pick-line-number)))
  123. (defun gnus-pick-start-reading (&optional catch-up)
  124. "Start reading the picked articles.
  125. If given a prefix, mark all unpicked articles as read."
  126. (interactive "P")
  127. (if gnus-newsgroup-processable
  128. (progn
  129. (gnus-summary-limit-to-articles nil)
  130. (when (or catch-up gnus-mark-unpicked-articles-as-read)
  131. (gnus-summary-limit-mark-excluded-as-read))
  132. (gnus-summary-first-article)
  133. (gnus-configure-windows
  134. (if gnus-pick-display-summary 'article 'pick) t))
  135. (if gnus-pick-elegant-flow
  136. (progn
  137. (when (or catch-up gnus-mark-unpicked-articles-as-read)
  138. (gnus-summary-catchup nil t))
  139. (if (gnus-group-quit-config gnus-newsgroup-name)
  140. (gnus-summary-exit)
  141. (gnus-summary-next-group)))
  142. (error "No articles have been picked"))))
  143. (defun gnus-pick-goto-article (arg)
  144. "Go to the article number indicated by ARG.
  145. If ARG is an invalid article number, then stay on current line."
  146. (let (pos)
  147. (save-excursion
  148. (goto-char (point-min))
  149. (when (zerop (forward-line (1- (prefix-numeric-value arg))))
  150. (setq pos (point))))
  151. (if (not pos)
  152. (gnus-error 2 "No such line: %s" arg)
  153. (goto-char pos))))
  154. (defun gnus-pick-article (&optional arg)
  155. "Pick the article on the current line.
  156. If ARG, pick the article on that line instead."
  157. (interactive "P")
  158. (when arg
  159. (gnus-pick-goto-article arg))
  160. (gnus-summary-mark-as-processable 1))
  161. (defun gnus-pick-article-or-thread (&optional arg)
  162. "If `gnus-thread-hide-subtree' is t, then pick the thread on the current line.
  163. Otherwise pick the article on the current line.
  164. If ARG, pick the article/thread on that line instead."
  165. (interactive "P")
  166. (when arg
  167. (gnus-pick-goto-article arg))
  168. (if gnus-thread-hide-subtree
  169. (progn
  170. (save-excursion
  171. (gnus-uu-mark-thread))
  172. (forward-line 1))
  173. (gnus-summary-mark-as-processable 1)))
  174. (defun gnus-pick-unmark-article-or-thread (&optional arg)
  175. "If `gnus-thread-hide-subtree' is t, then unmark the thread on current line.
  176. Otherwise unmark the article on current line.
  177. If ARG, unmark thread/article on that line instead."
  178. (interactive "P")
  179. (when arg
  180. (gnus-pick-goto-article arg))
  181. (if gnus-thread-hide-subtree
  182. (save-excursion
  183. (gnus-uu-unmark-thread))
  184. (gnus-summary-unmark-as-processable 1)))
  185. (defun gnus-pick-mouse-pick (e)
  186. (interactive "e")
  187. (mouse-set-point e)
  188. (save-excursion
  189. (gnus-summary-mark-as-processable 1)))
  190. (defun gnus-pick-mouse-pick-region (start-event)
  191. "Pick articles that the mouse is dragged over.
  192. This must be bound to a button-down mouse event."
  193. (interactive "e")
  194. (mouse-minibuffer-check start-event)
  195. (let* ((echo-keystrokes 0)
  196. (start-posn (event-start start-event))
  197. (start-point (posn-point start-posn))
  198. (start-line (1+ (count-lines (point-min) start-point)))
  199. (start-window (posn-window start-posn))
  200. (bounds (gnus-window-edges start-window))
  201. (top (nth 1 bounds))
  202. (bottom (if (window-minibuffer-p start-window)
  203. (nth 3 bounds)
  204. ;; Don't count the mode line.
  205. (1- (nth 3 bounds))))
  206. (click-count (1- (event-click-count start-event))))
  207. (setq mouse-selection-click-count click-count)
  208. (setq mouse-selection-click-count-buffer (current-buffer))
  209. (mouse-set-point start-event)
  210. ;; In case the down click is in the middle of some intangible text,
  211. ;; use the end of that text, and put it in START-POINT.
  212. (when (< (point) start-point)
  213. (goto-char start-point))
  214. (gnus-pick-article)
  215. (setq start-point (point))
  216. ;; end-of-range is used only in the single-click case.
  217. ;; It is the place where the drag has reached so far
  218. ;; (but not outside the window where the drag started).
  219. (let (event end end-point (end-of-range (point)))
  220. (track-mouse
  221. (while (progn
  222. (setq event (cdr (gnus-read-event-char)))
  223. (or (mouse-movement-p event)
  224. (eq (car-safe event) 'switch-frame)))
  225. (if (eq (car-safe event) 'switch-frame)
  226. nil
  227. (setq end (event-end event)
  228. end-point (posn-point end))
  229. (cond
  230. ;; Are we moving within the original window?
  231. ((and (eq (posn-window end) start-window)
  232. (integer-or-marker-p end-point))
  233. ;; Go to START-POINT first, so that when we move to END-POINT,
  234. ;; if it's in the middle of intangible text,
  235. ;; point jumps in the direction away from START-POINT.
  236. (goto-char start-point)
  237. (goto-char end-point)
  238. (gnus-pick-article)
  239. ;; In case the user moved his mouse really fast, pick
  240. ;; articles on the line between this one and the last one.
  241. (let* ((this-line (1+ (count-lines (point-min) end-point)))
  242. (min-line (min this-line start-line))
  243. (max-line (max this-line start-line)))
  244. (while (< min-line max-line)
  245. (goto-char (point-min))
  246. (forward-line (1- min-line))
  247. (gnus-pick-article)
  248. (setq min-line (1+ min-line)))
  249. (setq start-line this-line))
  250. (when (zerop (% click-count 3))
  251. (setq end-of-range (point))))
  252. (t
  253. (let ((mouse-row (cdr (cdr (mouse-position)))))
  254. (cond
  255. ((null mouse-row))
  256. ((< mouse-row top)
  257. (mouse-scroll-subr start-window (- mouse-row top)))
  258. ((>= mouse-row bottom)
  259. (mouse-scroll-subr start-window
  260. (1+ (- mouse-row bottom)))))))))))
  261. (when (consp event)
  262. (let ((fun (key-binding (vector (car event)))))
  263. ;; Run the binding of the terminating up-event, if possible.
  264. ;; In the case of a multiple click, it gives the wrong results,
  265. ;; because it would fail to set up a region.
  266. (when nil
  267. ;; (and (= (mod mouse-selection-click-count 3) 0) (fboundp fun))
  268. ;; In this case, we can just let the up-event execute normally.
  269. (let ((end (event-end event)))
  270. ;; Set the position in the event before we replay it,
  271. ;; because otherwise it may have a position in the wrong
  272. ;; buffer.
  273. (setcar (cdr end) end-of-range)
  274. ;; Delete the overlay before calling the function,
  275. ;; because delete-overlay increases buffer-modified-tick.
  276. (push event unread-command-events))))))))
  277. (defun gnus-pick-next-page ()
  278. "Go to the next page. If at the end of the buffer, start reading articles."
  279. (interactive)
  280. (let ((scroll-in-place nil))
  281. (condition-case nil
  282. (scroll-up)
  283. (end-of-buffer (gnus-pick-start-reading)))))
  284. ;;;
  285. ;;; gnus-binary-mode
  286. ;;;
  287. (defvar gnus-binary-mode-hook nil
  288. "Hook run in summary binary mode buffers.")
  289. (defvar gnus-binary-mode-map
  290. (let ((map (make-sparse-keymap)))
  291. (gnus-define-keys map
  292. "g" gnus-binary-show-article)
  293. map))
  294. (defun gnus-binary-make-menu-bar ()
  295. (unless (boundp 'gnus-binary-menu)
  296. (easy-menu-define
  297. gnus-binary-menu gnus-binary-mode-map ""
  298. '("Pick"
  299. ["Switch binary mode off" gnus-binary-mode t]))))
  300. (eval-when-compile
  301. (when (featurep 'xemacs)
  302. (defvar gnus-binary-mode-on-hook)
  303. (defvar gnus-binary-mode-off-hook)))
  304. (define-minor-mode gnus-binary-mode
  305. "Minor mode for providing a binary group interface in Gnus summary buffers."
  306. :lighter " Binary" :keymap gnus-binary-mode-map
  307. (cond
  308. ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-binary-mode nil))
  309. (gnus-binary-mode
  310. ;; Make sure that we don't select any articles upon group entry.
  311. (make-local-variable 'gnus-auto-select-first)
  312. (setq gnus-auto-select-first nil)
  313. (make-local-variable 'gnus-summary-display-article-function)
  314. (setq gnus-summary-display-article-function 'gnus-binary-display-article)
  315. ;; Set up the menu.
  316. (when (gnus-visual-p 'binary-menu 'menu)
  317. (gnus-binary-make-menu-bar)))))
  318. (defun gnus-binary-display-article (article &optional all-header)
  319. "Run ARTICLE through the binary decode functions."
  320. (when (gnus-summary-goto-subject article)
  321. (let ((gnus-view-pseudos (or gnus-view-pseudos 'automatic)))
  322. (gnus-uu-decode-uu))))
  323. (defun gnus-binary-show-article (&optional arg)
  324. "Bypass the binary functions and show the article."
  325. (interactive "P")
  326. (let (gnus-summary-display-article-function)
  327. (gnus-summary-show-article arg)))
  328. ;;;
  329. ;;; gnus-tree-mode
  330. ;;;
  331. (defcustom gnus-tree-line-format "%(%[%3,3n%]%)"
  332. "Format of tree elements."
  333. :type 'string
  334. :group 'gnus-summary-tree)
  335. (defcustom gnus-tree-minimize-window t
  336. "If non-nil, minimize the tree buffer window.
  337. If a number, never let the tree buffer grow taller than that number of
  338. lines."
  339. :type '(choice boolean
  340. integer)
  341. :group 'gnus-summary-tree)
  342. (defcustom gnus-selected-tree-face 'modeline
  343. "*Face used for highlighting selected articles in the thread tree."
  344. :type 'face
  345. :group 'gnus-summary-tree)
  346. (defvar gnus-tree-brackets '((?\[ . ?\]) (?\( . ?\))
  347. (?\{ . ?\}) (?< . ?>))
  348. "Brackets used in tree nodes.")
  349. (defvar gnus-tree-parent-child-edges '(?- ?\\ ?|)
  350. "Characters used to connect parents with children.")
  351. (defcustom gnus-tree-mode-line-format "Gnus: %%b %S %Z"
  352. "*The format specification for the tree mode line."
  353. :type 'string
  354. :group 'gnus-summary-tree)
  355. (defcustom gnus-generate-tree-function 'gnus-generate-vertical-tree
  356. "*Function for generating a thread tree.
  357. Two predefined functions are available:
  358. `gnus-generate-horizontal-tree' and `gnus-generate-vertical-tree'."
  359. :type '(radio (function-item gnus-generate-vertical-tree)
  360. (function-item gnus-generate-horizontal-tree)
  361. (function :tag "Other" nil))
  362. :group 'gnus-summary-tree)
  363. (defcustom gnus-tree-mode-hook nil
  364. "*Hook run in tree mode buffers."
  365. :type 'hook
  366. :group 'gnus-summary-tree)
  367. (when (featurep 'xemacs)
  368. (add-hook 'gnus-tree-mode-hook 'gnus-xmas-tree-menu-add)
  369. (add-hook 'gnus-tree-mode-hook 'gnus-xmas-switch-horizontal-scrollbar-off))
  370. ;;; Internal variables.
  371. (defvar gnus-tree-line-format-alist
  372. `((?n gnus-tmp-name ?s)
  373. (?f gnus-tmp-from ?s)
  374. (?N gnus-tmp-number ?d)
  375. (?\[ gnus-tmp-open-bracket ?c)
  376. (?\] gnus-tmp-close-bracket ?c)
  377. (?s gnus-tmp-subject ?s)))
  378. (defvar gnus-tree-mode-line-format-alist gnus-summary-mode-line-format-alist)
  379. (defvar gnus-tree-mode-line-format-spec nil)
  380. (defvar gnus-tree-line-format-spec nil)
  381. (defvar gnus-tree-node-length nil)
  382. (defvar gnus-selected-tree-overlay nil)
  383. (defvar gnus-tree-displayed-thread nil)
  384. (defvar gnus-tree-inhibit nil)
  385. (defvar gnus-tree-mode-map nil)
  386. (put 'gnus-tree-mode 'mode-class 'special)
  387. (unless gnus-tree-mode-map
  388. (setq gnus-tree-mode-map (make-keymap))
  389. (suppress-keymap gnus-tree-mode-map)
  390. (gnus-define-keys
  391. gnus-tree-mode-map
  392. "\r" gnus-tree-select-article
  393. gnus-mouse-2 gnus-tree-pick-article
  394. "\C-?" gnus-tree-read-summary-keys
  395. "h" gnus-tree-show-summary
  396. "\C-c\C-i" gnus-info-find-node)
  397. (substitute-key-definition
  398. 'undefined 'gnus-tree-read-summary-keys gnus-tree-mode-map))
  399. (defun gnus-tree-make-menu-bar ()
  400. (unless (boundp 'gnus-tree-menu)
  401. (easy-menu-define
  402. gnus-tree-menu gnus-tree-mode-map ""
  403. '("Tree"
  404. ["Select article" gnus-tree-select-article t]))))
  405. (defun gnus-tree-mode ()
  406. "Major mode for displaying thread trees."
  407. (interactive)
  408. (gnus-set-format 'tree-mode)
  409. (gnus-set-format 'tree t)
  410. (when (gnus-visual-p 'tree-menu 'menu)
  411. (gnus-tree-make-menu-bar))
  412. (kill-all-local-variables)
  413. (gnus-simplify-mode-line)
  414. (setq mode-name "Tree")
  415. (setq major-mode 'gnus-tree-mode)
  416. (use-local-map gnus-tree-mode-map)
  417. (buffer-disable-undo)
  418. (setq buffer-read-only t)
  419. (setq truncate-lines t)
  420. (save-excursion
  421. (gnus-set-work-buffer)
  422. (gnus-tree-node-insert (make-mail-header "") nil)
  423. (setq gnus-tree-node-length (1- (point))))
  424. (gnus-run-mode-hooks 'gnus-tree-mode-hook))
  425. (defun gnus-tree-read-summary-keys (&optional arg)
  426. "Read a summary buffer key sequence and execute it."
  427. (interactive "P")
  428. (unless gnus-tree-inhibit
  429. (let ((buf (current-buffer))
  430. (gnus-tree-inhibit t)
  431. win)
  432. (set-buffer gnus-article-buffer)
  433. (gnus-article-read-summary-keys arg nil t)
  434. (when (setq win (get-buffer-window buf))
  435. (select-window win)
  436. (when gnus-selected-tree-overlay
  437. (goto-char (or (gnus-overlay-end gnus-selected-tree-overlay) 1)))
  438. (gnus-tree-minimize)))))
  439. (defun gnus-tree-show-summary ()
  440. "Reconfigure windows to show summary buffer."
  441. (interactive)
  442. (if (not (gnus-buffer-live-p gnus-summary-buffer))
  443. (error "There is no summary buffer for this tree buffer")
  444. (gnus-configure-windows 'article)
  445. (gnus-summary-goto-subject gnus-current-article)))
  446. (defun gnus-tree-select-article (article)
  447. "Select the article under point, if any."
  448. (interactive (list (gnus-tree-article-number)))
  449. (let ((buf (current-buffer)))
  450. (when article
  451. (with-current-buffer gnus-summary-buffer
  452. (gnus-summary-goto-article article))
  453. (select-window (get-buffer-window buf)))))
  454. (defun gnus-tree-pick-article (e)
  455. "Select the article under the mouse pointer."
  456. (interactive "e")
  457. (mouse-set-point e)
  458. (gnus-tree-select-article (gnus-tree-article-number)))
  459. (defun gnus-tree-article-number ()
  460. (get-text-property (point) 'gnus-number))
  461. (defun gnus-tree-article-region (article)
  462. "Return a cons with BEG and END of the article region."
  463. (let ((pos (text-property-any
  464. (point-min) (point-max) 'gnus-number article)))
  465. (when pos
  466. (cons pos (next-single-property-change pos 'gnus-number)))))
  467. (defun gnus-tree-goto-article (article)
  468. (let ((pos (text-property-any
  469. (point-min) (point-max) 'gnus-number article)))
  470. (when pos
  471. (goto-char pos))))
  472. (defun gnus-tree-recenter ()
  473. "Center point in the tree window."
  474. (let ((selected (selected-window))
  475. (tree-window (gnus-get-buffer-window gnus-tree-buffer t)))
  476. (when tree-window
  477. (select-window tree-window)
  478. (when gnus-selected-tree-overlay
  479. (goto-char (or (gnus-overlay-end gnus-selected-tree-overlay) 1)))
  480. (let* ((top (cond ((< (window-height) 4) 0)
  481. ((< (window-height) 7) 1)
  482. (t 2)))
  483. (height (1- (window-height)))
  484. (bottom (save-excursion (goto-char (point-max))
  485. (forward-line (- height))
  486. (point))))
  487. ;; Set the window start to either `bottom', which is the biggest
  488. ;; possible valid number, or the second line from the top,
  489. ;; whichever is the least.
  490. (set-window-start
  491. tree-window (min bottom (save-excursion
  492. (forward-line (- top)) (point)))))
  493. (select-window selected))))
  494. (defun gnus-get-tree-buffer ()
  495. "Return the tree buffer properly initialized."
  496. (with-current-buffer (gnus-get-buffer-create gnus-tree-buffer)
  497. (unless (eq major-mode 'gnus-tree-mode)
  498. (gnus-tree-mode))
  499. (current-buffer)))
  500. (defun gnus-tree-minimize ()
  501. (when (and gnus-tree-minimize-window
  502. (not (one-window-p)))
  503. (let ((windows 0)
  504. tot-win-height)
  505. (walk-windows (lambda (window) (incf windows)))
  506. (setq tot-win-height
  507. (- (frame-height)
  508. (* window-min-height (1- windows))
  509. 2))
  510. (let* ((window-min-height 2)
  511. (height (count-lines (point-min) (point-max)))
  512. (min (max (1- window-min-height) height))
  513. (tot (if (numberp gnus-tree-minimize-window)
  514. (min gnus-tree-minimize-window min)
  515. min))
  516. (win (get-buffer-window (current-buffer)))
  517. (wh (and win (1- (window-height win)))))
  518. (setq tot (min tot tot-win-height))
  519. (when (and win
  520. (not (eq tot wh)))
  521. (let ((selected (selected-window)))
  522. (when (ignore-errors (select-window win))
  523. (enlarge-window (- tot wh))
  524. (select-window selected))))))))
  525. ;;; Generating the tree.
  526. (defun gnus-tree-node-insert (header sparse &optional adopted)
  527. (let* ((dummy (stringp header))
  528. (header (if (vectorp header) header
  529. (progn
  530. (setq header (make-mail-header "*****"))
  531. (mail-header-set-number header 0)
  532. (mail-header-set-lines header 0)
  533. (mail-header-set-chars header 0)
  534. header)))
  535. (gnus-tmp-from (mail-header-from header))
  536. (gnus-tmp-subject (mail-header-subject header))
  537. (gnus-tmp-number (mail-header-number header))
  538. (gnus-tmp-name
  539. (cond
  540. ((string-match "(.+)" gnus-tmp-from)
  541. (substring gnus-tmp-from
  542. (1+ (match-beginning 0)) (1- (match-end 0))))
  543. ((string-match "<[^>]+> *$" gnus-tmp-from)
  544. (let ((beg (match-beginning 0)))
  545. (or (and (string-match "^\"[^\"]*\"" gnus-tmp-from)
  546. (substring gnus-tmp-from (1+ (match-beginning 0))
  547. (1- (match-end 0))))
  548. (substring gnus-tmp-from 0 beg))))
  549. ((memq gnus-tmp-number sparse)
  550. "***")
  551. (t gnus-tmp-from)))
  552. (gnus-tmp-open-bracket
  553. (cond ((memq gnus-tmp-number sparse)
  554. (caadr gnus-tree-brackets))
  555. (dummy (caaddr gnus-tree-brackets))
  556. (adopted (car (nth 3 gnus-tree-brackets)))
  557. (t (caar gnus-tree-brackets))))
  558. (gnus-tmp-close-bracket
  559. (cond ((memq gnus-tmp-number sparse)
  560. (cdadr gnus-tree-brackets))
  561. (adopted (cdr (nth 3 gnus-tree-brackets)))
  562. (dummy
  563. (cdaddr gnus-tree-brackets))
  564. (t (cdar gnus-tree-brackets))))
  565. (buffer-read-only nil)
  566. beg end)
  567. (gnus-add-text-properties
  568. (setq beg (point))
  569. (setq end (progn (eval gnus-tree-line-format-spec) (point)))
  570. (list 'gnus-number gnus-tmp-number))
  571. (when (or t (gnus-visual-p 'tree-highlight 'highlight))
  572. (gnus-tree-highlight-node gnus-tmp-number beg end))))
  573. (defun gnus-tree-highlight-node (article beg end)
  574. "Highlight current line according to `gnus-summary-highlight'."
  575. (let ((list gnus-summary-highlight)
  576. face)
  577. (with-current-buffer gnus-summary-buffer
  578. (let* ((score (or (cdr (assq article gnus-newsgroup-scored))
  579. gnus-summary-default-score 0))
  580. (default gnus-summary-default-score)
  581. (default-high gnus-summary-default-high-score)
  582. (default-low gnus-summary-default-low-score)
  583. (uncached (memq article gnus-newsgroup-undownloaded))
  584. (downloaded (not uncached))
  585. (mark (or (gnus-summary-article-mark article) gnus-unread-mark)))
  586. ;; Eval the cars of the lists until we find a match.
  587. (while (and list
  588. (not (eval (caar list))))
  589. (setq list (cdr list)))))
  590. (unless (eq (setq face (cdar list)) (get-text-property beg 'face))
  591. (gnus-put-text-property-excluding-characters-with-faces
  592. beg end 'face
  593. (if (boundp face) (symbol-value face) face)))))
  594. (defun gnus-tree-indent (level)
  595. (insert (make-string (1- (* (1+ gnus-tree-node-length) level)) ? )))
  596. (defvar gnus-tmp-limit)
  597. (defvar gnus-tmp-sparse)
  598. (defvar gnus-tmp-indent)
  599. (defun gnus-generate-tree (thread)
  600. "Generate a thread tree for THREAD."
  601. (with-current-buffer (gnus-get-tree-buffer)
  602. (let ((buffer-read-only nil)
  603. (gnus-tmp-indent 0))
  604. (erase-buffer)
  605. (funcall gnus-generate-tree-function thread 0)
  606. (gnus-set-mode-line 'tree)
  607. (goto-char (point-min))
  608. (gnus-tree-minimize)
  609. (gnus-tree-recenter)
  610. (let ((selected (selected-window)))
  611. (when (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t)
  612. (select-window (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t))
  613. (gnus-horizontal-recenter)
  614. (select-window selected))))))
  615. (defun gnus-generate-horizontal-tree (thread level &optional dummyp adopted)
  616. "Generate a horizontal tree."
  617. (let* ((dummy (stringp (car thread)))
  618. (do (or dummy
  619. (and (car thread)
  620. (memq (mail-header-number (car thread))
  621. gnus-tmp-limit))))
  622. col beg)
  623. (if (not do)
  624. ;; We don't want this article.
  625. (setq thread (cdr thread))
  626. (if (not (bolp))
  627. ;; Not the first article on the line, so we insert a "-".
  628. (insert (car gnus-tree-parent-child-edges))
  629. ;; If the level isn't zero, then we insert some indentation.
  630. (unless (zerop level)
  631. (gnus-tree-indent level)
  632. (insert (cadr gnus-tree-parent-child-edges))
  633. (setq col (- (setq beg (point)) (point-at-bol) 1))
  634. ;; Draw "|" lines upwards.
  635. (while (progn
  636. (forward-line -1)
  637. (forward-char col)
  638. (eq (char-after) ? ))
  639. (delete-char 1)
  640. (insert (caddr gnus-tree-parent-child-edges)))
  641. (goto-char beg)))
  642. (setq dummyp nil)
  643. ;; Insert the article node.
  644. (gnus-tree-node-insert (pop thread) gnus-tmp-sparse adopted))
  645. (if (null thread)
  646. ;; End of the thread, so we go to the next line.
  647. (unless (bolp)
  648. (insert "\n"))
  649. ;; Recurse downwards in all children of this article.
  650. (while thread
  651. (gnus-generate-horizontal-tree
  652. (pop thread) (if do (1+ level) level)
  653. (or dummyp dummy) dummy)))))
  654. (defsubst gnus-tree-indent-vertical ()
  655. (let ((len (- (* (1+ gnus-tree-node-length) gnus-tmp-indent)
  656. (- (point) (point-at-bol)))))
  657. (when (> len 0)
  658. (insert (make-string len ? )))))
  659. (defsubst gnus-tree-forward-line (n)
  660. (while (>= (decf n) 0)
  661. (unless (zerop (forward-line 1))
  662. (end-of-line)
  663. (insert "\n")))
  664. (end-of-line))
  665. (defun gnus-generate-vertical-tree (thread level &optional dummyp adopted)
  666. "Generate a vertical tree."
  667. (let* ((dummy (stringp (car thread)))
  668. (do (or dummy
  669. (and (car thread)
  670. (memq (mail-header-number (car thread))
  671. gnus-tmp-limit))))
  672. beg)
  673. (if (not do)
  674. ;; We don't want this article.
  675. (setq thread (cdr thread))
  676. (if (not (save-excursion (beginning-of-line) (bobp)))
  677. ;; Not the first article on the line, so we insert a "-".
  678. (progn
  679. (gnus-tree-indent-vertical)
  680. (insert (make-string (/ gnus-tree-node-length 2) ? ))
  681. (insert (caddr gnus-tree-parent-child-edges))
  682. (gnus-tree-forward-line 1))
  683. ;; If the level isn't zero, then we insert some indentation.
  684. (unless (zerop gnus-tmp-indent)
  685. (gnus-tree-forward-line (1- (* 2 level)))
  686. (gnus-tree-indent-vertical)
  687. (delete-char -1)
  688. (insert (cadr gnus-tree-parent-child-edges))
  689. (setq beg (point))
  690. (forward-char -1)
  691. ;; Draw "-" lines leftwards.
  692. (while (and (not (bobp))
  693. (eq (char-after (1- (point))) ? ))
  694. (delete-char -1)
  695. (insert (car gnus-tree-parent-child-edges))
  696. (forward-char -1))
  697. (goto-char beg)
  698. (gnus-tree-forward-line 1)))
  699. (setq dummyp nil)
  700. ;; Insert the article node.
  701. (gnus-tree-indent-vertical)
  702. (gnus-tree-node-insert (pop thread) gnus-tmp-sparse adopted)
  703. (gnus-tree-forward-line 1))
  704. (if (null thread)
  705. ;; End of the thread, so we go to the next line.
  706. (progn
  707. (goto-char (point-min))
  708. (end-of-line)
  709. (incf gnus-tmp-indent))
  710. ;; Recurse downwards in all children of this article.
  711. (while thread
  712. (gnus-generate-vertical-tree
  713. (pop thread) (if do (1+ level) level)
  714. (or dummyp dummy) dummy)))))
  715. ;;; Interface functions.
  716. (defun gnus-possibly-generate-tree (article &optional force)
  717. "Generate the thread tree for ARTICLE if it isn't displayed already."
  718. (when (with-current-buffer gnus-summary-buffer
  719. (and gnus-use-trees
  720. gnus-show-threads
  721. (vectorp (gnus-summary-article-header article))))
  722. (save-excursion
  723. (let ((top (with-current-buffer gnus-summary-buffer
  724. (gnus-cut-thread
  725. (gnus-remove-thread
  726. (mail-header-id
  727. (gnus-summary-article-header article))
  728. t))))
  729. (gnus-tmp-limit gnus-newsgroup-limit)
  730. (gnus-tmp-sparse gnus-newsgroup-sparse))
  731. (when (or force
  732. (not (eq top gnus-tree-displayed-thread)))
  733. (gnus-generate-tree top)
  734. (setq gnus-tree-displayed-thread top))))))
  735. (defun gnus-tree-open (group)
  736. (gnus-get-tree-buffer))
  737. (defun gnus-tree-close (group)
  738. (gnus-kill-buffer gnus-tree-buffer))
  739. (defun gnus-tree-perhaps-minimize ()
  740. (when (and gnus-tree-minimize-window
  741. (get-buffer gnus-tree-buffer))
  742. (with-current-buffer gnus-tree-buffer
  743. (gnus-tree-minimize))))
  744. (defun gnus-highlight-selected-tree (article)
  745. "Highlight the selected article in the tree."
  746. (let ((buf (current-buffer))
  747. region)
  748. (set-buffer gnus-tree-buffer)
  749. (when (setq region (gnus-tree-article-region article))
  750. (when (or (not gnus-selected-tree-overlay)
  751. (gnus-extent-detached-p gnus-selected-tree-overlay))
  752. ;; Create a new overlay.
  753. (gnus-overlay-put
  754. (setq gnus-selected-tree-overlay
  755. (gnus-make-overlay (point-min) (1+ (point-min))))
  756. 'face gnus-selected-tree-face))
  757. ;; Move the overlay to the article.
  758. (gnus-move-overlay
  759. gnus-selected-tree-overlay (goto-char (car region)) (cdr region))
  760. (gnus-tree-minimize)
  761. (gnus-tree-recenter)
  762. (let ((selected (selected-window)))
  763. (when (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t)
  764. (select-window (gnus-get-buffer-window (set-buffer gnus-tree-buffer) t))
  765. (gnus-horizontal-recenter)
  766. (select-window selected))))
  767. ;; If we remove this save-excursion, it updates the wrong mode lines?!?
  768. (with-current-buffer gnus-tree-buffer
  769. (gnus-set-mode-line 'tree))
  770. (set-buffer buf)))
  771. (defun gnus-tree-highlight-article (article face)
  772. (with-current-buffer (gnus-get-tree-buffer)
  773. (let (region)
  774. (when (setq region (gnus-tree-article-region article))
  775. (gnus-put-text-property (car region) (cdr region) 'face face)
  776. (set-window-point
  777. (gnus-get-buffer-window (current-buffer) t) (cdr region))))))
  778. ;;; Allow redefinition of functions.
  779. (gnus-ems-redefine)
  780. (provide 'gnus-salt)
  781. ;;; gnus-salt.el ends here