gnus-topic.el 60 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792
  1. ;;; gnus-topic.el --- a folding minor mode for Gnus group buffers
  2. ;; Copyright (C) 1995-2012 Free Software Foundation, Inc.
  3. ;; Author: Ilja Weis <kult@uni-paderborn.de>
  4. ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
  5. ;; Keywords: news
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile (require 'cl))
  20. (require 'gnus)
  21. (require 'gnus-group)
  22. (require 'gnus-start)
  23. (require 'gnus-util)
  24. (defgroup gnus-topic nil
  25. "Group topics."
  26. :group 'gnus-group)
  27. (defvar gnus-topic-mode nil
  28. "Minor mode for Gnus group buffers.")
  29. (defcustom gnus-topic-mode-hook nil
  30. "Hook run in topic mode buffers."
  31. :type 'hook
  32. :group 'gnus-topic)
  33. (when (featurep 'xemacs)
  34. (add-hook 'gnus-topic-mode-hook 'gnus-xmas-topic-menu-add))
  35. (defcustom gnus-topic-line-format "%i[ %(%{%n%}%) -- %A ]%v\n"
  36. "Format of topic lines.
  37. It works along the same lines as a normal formatting string,
  38. with some simple extensions.
  39. %i Indentation based on topic level.
  40. %n Topic name.
  41. %v Nothing if the topic is visible, \"...\" otherwise.
  42. %g Number of groups in the topic.
  43. %a Number of unread articles in the groups in the topic.
  44. %A Number of unread articles in the groups in the topic and its subtopics.
  45. General format specifiers can also be used.
  46. See Info node `(gnus)Formatting Variables'."
  47. :link '(custom-manual "(gnus)Formatting Variables")
  48. :type 'string
  49. :group 'gnus-topic)
  50. (defcustom gnus-topic-indent-level 2
  51. "*How much each subtopic should be indented."
  52. :type 'integer
  53. :group 'gnus-topic)
  54. (defcustom gnus-topic-display-empty-topics t
  55. "*If non-nil, display the topic lines even of topics that have no unread articles."
  56. :type 'boolean
  57. :group 'gnus-topic)
  58. ;; Internal variables.
  59. (defvar gnus-topic-active-topology nil)
  60. (defvar gnus-topic-active-alist nil)
  61. (defvar gnus-topic-unreads nil)
  62. (defvar gnus-topology-checked-p nil
  63. "Whether the topology has been checked in this session.")
  64. (defvar gnus-topic-killed-topics nil)
  65. (defvar gnus-topic-inhibit-change-level nil)
  66. (defconst gnus-topic-line-format-alist
  67. `((?n name ?s)
  68. (?v visible ?s)
  69. (?i indentation ?s)
  70. (?g number-of-groups ?d)
  71. (?a (gnus-topic-articles-in-topic entries) ?d)
  72. (?A total-number-of-articles ?d)
  73. (?l level ?d)))
  74. (defvar gnus-topic-line-format-spec nil)
  75. ;;; Utility functions
  76. (defun gnus-group-topic-name ()
  77. "The name of the topic on the current line."
  78. (let ((topic (get-text-property (point-at-bol) 'gnus-topic)))
  79. (and topic (symbol-name topic))))
  80. (defun gnus-group-topic-level ()
  81. "The level of the topic on the current line."
  82. (get-text-property (point-at-bol) 'gnus-topic-level))
  83. (defun gnus-group-topic-unread ()
  84. "The number of unread articles in topic on the current line."
  85. (get-text-property (point-at-bol) 'gnus-topic-unread))
  86. (defun gnus-topic-unread (topic)
  87. "Return the number of unread articles in TOPIC."
  88. (or (cdr (assoc topic gnus-topic-unreads))
  89. 0))
  90. (defun gnus-group-topic-p ()
  91. "Return non-nil if the current line is a topic."
  92. (gnus-group-topic-name))
  93. (defun gnus-topic-visible-p ()
  94. "Return non-nil if the current topic is visible."
  95. (get-text-property (point-at-bol) 'gnus-topic-visible))
  96. (defun gnus-topic-articles-in-topic (entries)
  97. (let ((total 0)
  98. number)
  99. (while entries
  100. (when (numberp (setq number (car (pop entries))))
  101. (incf total number)))
  102. total))
  103. (defun gnus-group-topic (group)
  104. "Return the topic GROUP is a member of."
  105. (let ((alist gnus-topic-alist)
  106. out)
  107. (while alist
  108. (when (member group (cdar alist))
  109. (setq out (caar alist)
  110. alist nil))
  111. (setq alist (cdr alist)))
  112. out))
  113. (defun gnus-group-parent-topic (group)
  114. "Return the topic GROUP is member of by looking at the group buffer."
  115. (with-current-buffer gnus-group-buffer
  116. (if (gnus-group-goto-group group)
  117. (gnus-current-topic)
  118. (gnus-group-topic group))))
  119. (defun gnus-topic-goto-topic (topic)
  120. (when topic
  121. (gnus-goto-char (text-property-any (point-min) (point-max)
  122. 'gnus-topic (intern topic)))))
  123. (defun gnus-topic-jump-to-topic (topic)
  124. "Go to TOPIC."
  125. (interactive
  126. (list (gnus-completing-read "Go to topic" (gnus-topic-list) t)))
  127. (let ((buffer-read-only nil))
  128. (dolist (topic (gnus-current-topics topic))
  129. (unless (gnus-topic-goto-topic topic)
  130. (gnus-topic-goto-missing-topic topic)
  131. (gnus-topic-display-missing-topic topic))))
  132. (gnus-topic-goto-topic topic))
  133. (defun gnus-current-topic ()
  134. "Return the name of the current topic."
  135. (let ((result
  136. (or (get-text-property (point) 'gnus-topic)
  137. (save-excursion
  138. (and (gnus-goto-char (previous-single-property-change
  139. (point) 'gnus-topic))
  140. (get-text-property (max (1- (point)) (point-min))
  141. 'gnus-topic))))))
  142. (when result
  143. (symbol-name result))))
  144. (defun gnus-current-topics (&optional topic)
  145. "Return a list of all current topics, lowest in hierarchy first.
  146. If TOPIC, start with that topic."
  147. (let ((topic (or topic (gnus-current-topic)))
  148. topics)
  149. (while topic
  150. (push topic topics)
  151. (setq topic (gnus-topic-parent-topic topic)))
  152. (nreverse topics)))
  153. (defun gnus-group-active-topic-p ()
  154. "Say whether the current topic comes from the active topics."
  155. (get-text-property (point-at-bol) 'gnus-active))
  156. (defun gnus-topic-find-groups (topic &optional level all lowest recursive)
  157. "Return entries for all visible groups in TOPIC.
  158. If RECURSIVE is t, return groups in its subtopics too."
  159. (let ((groups (cdr (assoc topic gnus-topic-alist)))
  160. info clevel unread group params visible-groups entry active)
  161. (setq lowest (or lowest 1))
  162. (setq level (or level gnus-level-unsubscribed))
  163. ;; We go through the newsrc to look for matches.
  164. (while groups
  165. (when (setq group (pop groups))
  166. (setq entry (gnus-group-entry group)
  167. info (nth 2 entry)
  168. params (gnus-info-params info)
  169. active (gnus-active group)
  170. unread (or (car entry)
  171. (and (not (equal group "dummy.group"))
  172. active
  173. (- (1+ (cdr active)) (car active))))
  174. clevel (or (gnus-info-level info)
  175. (if (member group gnus-zombie-list)
  176. gnus-level-zombie gnus-level-killed))))
  177. (and
  178. info ; nil means that the group is dead.
  179. (<= clevel level)
  180. (>= clevel lowest) ; Is inside the level we want.
  181. (or all
  182. (if (or (eq unread t)
  183. (eq unread nil))
  184. gnus-group-list-inactive-groups
  185. (> unread 0))
  186. (and gnus-list-groups-with-ticked-articles
  187. (cdr (assq 'tick (gnus-info-marks info))))
  188. ;; Has right readedness.
  189. ;; Check for permanent visibility.
  190. (and gnus-permanently-visible-groups
  191. (string-match gnus-permanently-visible-groups group))
  192. (memq 'visible params)
  193. (cdr (assq 'visible params)))
  194. ;; Add this group to the list of visible groups.
  195. (push (or entry group) visible-groups)))
  196. (setq visible-groups (nreverse visible-groups))
  197. (when recursive
  198. (if (eq recursive t)
  199. (setq recursive (cdr (gnus-topic-find-topology topic))))
  200. (dolist (topic-topology (cdr recursive))
  201. (setq visible-groups
  202. (nconc visible-groups
  203. (gnus-topic-find-groups
  204. (caar topic-topology)
  205. level all lowest topic-topology)))))
  206. visible-groups))
  207. (defun gnus-topic-goto-previous-topic (n)
  208. "Go to the N'th previous topic."
  209. (interactive "p")
  210. (gnus-topic-goto-next-topic (- n)))
  211. (defun gnus-topic-goto-next-topic (n)
  212. "Go to the N'th next topic."
  213. (interactive "p")
  214. (let ((backward (< n 0))
  215. (n (abs n))
  216. (topic (gnus-current-topic)))
  217. (while (and (> n 0)
  218. (setq topic
  219. (if backward
  220. (gnus-topic-previous-topic topic)
  221. (gnus-topic-next-topic topic))))
  222. (gnus-topic-goto-topic topic)
  223. (setq n (1- n)))
  224. (when (/= 0 n)
  225. (gnus-message 7 "No more topics"))
  226. n))
  227. (defun gnus-topic-previous-topic (topic)
  228. "Return the previous topic on the same level as TOPIC."
  229. (let ((top (cddr (gnus-topic-find-topology
  230. (gnus-topic-parent-topic topic)))))
  231. (unless (equal topic (caaar top))
  232. (while (and top (not (equal (caaadr top) topic)))
  233. (setq top (cdr top)))
  234. (caaar top))))
  235. (defun gnus-topic-parent-topic (topic &optional topology)
  236. "Return the parent of TOPIC."
  237. (unless topology
  238. (setq topology gnus-topic-topology))
  239. (let ((parent (car (pop topology)))
  240. result found)
  241. (while (and topology
  242. (not (setq found (equal (caaar topology) topic)))
  243. (not (setq result (gnus-topic-parent-topic
  244. topic (car topology)))))
  245. (setq topology (cdr topology)))
  246. (or result (and found parent))))
  247. (defun gnus-topic-next-topic (topic &optional previous)
  248. "Return the next sibling of TOPIC."
  249. (let ((parentt (cddr (gnus-topic-find-topology
  250. (gnus-topic-parent-topic topic))))
  251. prev)
  252. (while (and parentt
  253. (not (equal (caaar parentt) topic)))
  254. (setq prev (caaar parentt)
  255. parentt (cdr parentt)))
  256. (if previous
  257. prev
  258. (caaadr parentt))))
  259. (defun gnus-topic-forward-topic (num)
  260. "Go to the next topic on the same level as the current one."
  261. (let* ((topic (gnus-current-topic))
  262. (way (if (< num 0) 'gnus-topic-previous-topic
  263. 'gnus-topic-next-topic))
  264. (num (abs num)))
  265. (while (and (not (zerop num))
  266. (setq topic (funcall way topic)))
  267. (when (gnus-topic-goto-topic topic)
  268. (decf num)))
  269. (unless (zerop num)
  270. (goto-char (point-max)))
  271. num))
  272. (defun gnus-topic-find-topology (topic &optional topology level remove)
  273. "Return the topology of TOPIC."
  274. (unless topology
  275. (setq topology gnus-topic-topology)
  276. (setq level 0))
  277. (let ((top topology)
  278. result)
  279. (if (equal (caar topology) topic)
  280. (progn
  281. (when remove
  282. (delq topology remove))
  283. (cons level topology))
  284. (setq topology (cdr topology))
  285. (while (and topology
  286. (not (setq result (gnus-topic-find-topology
  287. topic (car topology) (1+ level)
  288. (and remove top)))))
  289. (setq topology (cdr topology)))
  290. result)))
  291. (defvar gnus-tmp-topics nil)
  292. (defun gnus-topic-list (&optional topology)
  293. "Return a list of all topics in the topology."
  294. (unless topology
  295. (setq topology gnus-topic-topology
  296. gnus-tmp-topics nil))
  297. (push (caar topology) gnus-tmp-topics)
  298. (mapc 'gnus-topic-list (cdr topology))
  299. gnus-tmp-topics)
  300. ;;; Topic parameter jazz
  301. (defun gnus-topic-parameters (topic)
  302. "Return the parameters for TOPIC."
  303. (let ((top (gnus-topic-find-topology topic)))
  304. (when top
  305. (nth 3 (cadr top)))))
  306. (defun gnus-topic-set-parameters (topic parameters)
  307. "Set the topic parameters of TOPIC to PARAMETERS."
  308. (let ((top (gnus-topic-find-topology topic)))
  309. (unless top
  310. (error "No such topic: %s" topic))
  311. ;; We may have to extend if there is no parameters here
  312. ;; to begin with.
  313. (unless (nthcdr 2 (cadr top))
  314. (nconc (cadr top) (list nil)))
  315. (unless (nthcdr 3 (cadr top))
  316. (nconc (cadr top) (list nil)))
  317. (setcar (nthcdr 3 (cadr top)) parameters)
  318. (gnus-dribble-enter
  319. (format "(gnus-topic-set-parameters %S '%S)" topic parameters))))
  320. (defun gnus-group-topic-parameters (group)
  321. "Compute the group parameters for GROUP in topic mode.
  322. Possibly inherit parameters from topics above GROUP."
  323. (let ((params-list (copy-sequence (gnus-group-get-parameter group))))
  324. (save-excursion
  325. (gnus-topic-hierarchical-parameters
  326. ;; First we try to go to the group within the group buffer and find the
  327. ;; topic for the group that way. This hopefully copes well with groups
  328. ;; that are in more than one topic. Failing that (i.e. when the group
  329. ;; isn't visible in the group buffer) we find a topic for the group via
  330. ;; gnus-group-topic.
  331. (or (and (gnus-group-goto-group group)
  332. (gnus-current-topic))
  333. (gnus-group-topic group))
  334. params-list))))
  335. (defun gnus-topic-hierarchical-parameters (topic &optional group-params-list)
  336. "Compute the topic parameters for TOPIC.
  337. Possibly inherit parameters from topics above TOPIC.
  338. If optional argument GROUP-PARAMS-LIST is non-nil, use it as the basis for
  339. inheritance."
  340. (let ((params-list
  341. ;; We probably have lots of nil elements here, so we remove them.
  342. ;; Probably faster than doing this "properly".
  343. (delq nil (cons group-params-list
  344. (mapcar 'gnus-topic-parameters
  345. (gnus-current-topics topic)))))
  346. param out params)
  347. ;; Now we have all the parameters, so we go through them
  348. ;; and do inheritance in the obvious way.
  349. (let (posting-style)
  350. (while (setq params (pop params-list))
  351. (while (setq param (pop params))
  352. (when (atom param)
  353. (setq param (cons param t)))
  354. (cond ((eq (car param) 'posting-style)
  355. (let ((param (cdr param))
  356. elt)
  357. (while (setq elt (pop param))
  358. (unless (assoc (car elt) posting-style)
  359. (push elt posting-style)))))
  360. (t
  361. (unless (assq (car param) out)
  362. (push param out))))))
  363. (and posting-style (push (cons 'posting-style posting-style) out)))
  364. ;; Return the resulting parameter list.
  365. out))
  366. ;;; General utility functions
  367. (defun gnus-topic-enter-dribble ()
  368. (gnus-dribble-enter
  369. (format "(setq gnus-topic-topology '%S)" gnus-topic-topology)))
  370. ;;; Generating group buffers
  371. (defun gnus-group-prepare-topics (level &optional predicate lowest
  372. regexp list-topic topic-level)
  373. "List all newsgroups with unread articles of level LEVEL or lower.
  374. Use the `gnus-group-topics' to sort the groups.
  375. If PREDICATE is a function, list groups that the function returns non-nil;
  376. if it is t, list groups that have no unread articles.
  377. If LOWEST is non-nil, list all newsgroups of level LOWEST or higher."
  378. (set-buffer gnus-group-buffer)
  379. (let ((buffer-read-only nil)
  380. (lowest (or lowest 1))
  381. (not-in-list
  382. (and gnus-group-listed-groups
  383. (copy-sequence gnus-group-listed-groups))))
  384. (gnus-update-format-specifications nil 'topic)
  385. (when (or (not gnus-topic-alist)
  386. (not gnus-topology-checked-p))
  387. (gnus-topic-check-topology))
  388. (unless list-topic
  389. (erase-buffer))
  390. ;; List dead groups?
  391. (when (or gnus-group-listed-groups
  392. (and (>= level gnus-level-zombie)
  393. (<= lowest gnus-level-zombie)))
  394. (gnus-group-prepare-flat-list-dead
  395. (setq gnus-zombie-list (sort gnus-zombie-list 'string<))
  396. gnus-level-zombie ?Z
  397. regexp))
  398. (when (or gnus-group-listed-groups
  399. (and (>= level gnus-level-killed)
  400. (<= lowest gnus-level-killed)))
  401. (gnus-group-prepare-flat-list-dead
  402. (setq gnus-killed-list (sort gnus-killed-list 'string<))
  403. gnus-level-killed ?K regexp)
  404. (when not-in-list
  405. (unless gnus-killed-hashtb
  406. (gnus-make-hashtable-from-killed))
  407. (gnus-group-prepare-flat-list-dead
  408. (gnus-remove-if (lambda (group)
  409. (or (gnus-group-entry group)
  410. (gnus-gethash group gnus-killed-hashtb)))
  411. not-in-list)
  412. gnus-level-killed ?K regexp)))
  413. ;; Use topics.
  414. (prog1
  415. (when (or (< lowest gnus-level-zombie)
  416. gnus-group-listed-groups)
  417. (if list-topic
  418. (let ((top (gnus-topic-find-topology list-topic)))
  419. (gnus-topic-prepare-topic (cdr top) (car top)
  420. (or topic-level level) predicate
  421. nil lowest regexp))
  422. (gnus-topic-prepare-topic gnus-topic-topology 0
  423. (or topic-level level) predicate
  424. nil lowest regexp)))
  425. (gnus-group-set-mode-line)
  426. (setq gnus-group-list-mode (cons level predicate))
  427. (gnus-run-hooks 'gnus-group-prepare-hook))))
  428. (defun gnus-topic-prepare-topic (topicl level &optional list-level
  429. predicate silent
  430. lowest regexp)
  431. "Insert TOPIC into the group buffer.
  432. If SILENT, don't insert anything. Return the number of unread
  433. articles in the topic and its subtopics."
  434. (let* ((type (pop topicl))
  435. (entries (gnus-topic-find-groups
  436. (car type)
  437. (if gnus-group-listed-groups
  438. gnus-level-killed
  439. list-level)
  440. (or predicate gnus-group-listed-groups
  441. (cdr (assq 'visible
  442. (gnus-topic-hierarchical-parameters
  443. (car type)))))
  444. (if gnus-group-listed-groups 0 lowest)))
  445. (visiblep (and (eq (nth 1 type) 'visible) (not silent)))
  446. (gnus-group-indentation
  447. (make-string (* gnus-topic-indent-level level) ? ))
  448. (beg (progn (beginning-of-line) (point)))
  449. (topicl (reverse topicl))
  450. (all-entries entries)
  451. (point-max (point-max))
  452. (unread 0)
  453. (topic (car type))
  454. info entry end active tick)
  455. ;; Insert any sub-topics.
  456. (while topicl
  457. (incf unread
  458. (gnus-topic-prepare-topic
  459. (pop topicl) (1+ level) list-level predicate
  460. (not visiblep) lowest regexp)))
  461. (setq end (point))
  462. (goto-char beg)
  463. ;; Insert all the groups that belong in this topic.
  464. (while (setq entry (pop entries))
  465. (when (if (stringp entry)
  466. (gnus-group-prepare-logic
  467. entry
  468. (and
  469. (or (not gnus-group-listed-groups)
  470. (if (< list-level gnus-level-zombie) nil
  471. (let ((entry-level
  472. (if (member entry gnus-zombie-list)
  473. gnus-level-zombie gnus-level-killed)))
  474. (and (<= entry-level list-level)
  475. (>= entry-level lowest)))))
  476. (cond
  477. ((stringp regexp)
  478. (string-match regexp entry))
  479. ((functionp regexp)
  480. (funcall regexp entry))
  481. ((null regexp) t)
  482. (t nil))))
  483. (setq info (nth 2 entry))
  484. (gnus-group-prepare-logic
  485. (gnus-info-group info)
  486. (and (or (not gnus-group-listed-groups)
  487. (let ((entry-level (gnus-info-level info)))
  488. (and (<= entry-level list-level)
  489. (>= entry-level lowest))))
  490. (or (not (functionp predicate))
  491. (funcall predicate info))
  492. (or (not (stringp regexp))
  493. (string-match regexp (gnus-info-group info))))))
  494. (when visiblep
  495. (if (stringp entry)
  496. ;; Dead groups.
  497. (gnus-group-insert-group-line
  498. entry (if (member entry gnus-zombie-list)
  499. gnus-level-zombie gnus-level-killed)
  500. nil (- (1+ (cdr (setq active (gnus-active entry))))
  501. (car active))
  502. nil)
  503. ;; Living groups.
  504. (when (setq info (nth 2 entry))
  505. (gnus-group-insert-group-line
  506. (gnus-info-group info)
  507. (gnus-info-level info) (gnus-info-marks info)
  508. (car entry) (gnus-info-method info)))))
  509. (when (and (listp entry)
  510. (numberp (car entry)))
  511. (incf unread (car entry)))
  512. (when (listp entry)
  513. (setq tick t))))
  514. (goto-char beg)
  515. ;; Insert the topic line.
  516. (when (and (not silent)
  517. (or gnus-topic-display-empty-topics ;We want empty topics
  518. (not (zerop unread)) ;Non-empty
  519. tick ;Ticked articles
  520. (/= point-max (point-max)))) ;Inactive groups
  521. (gnus-extent-start-open (point))
  522. (gnus-topic-insert-topic-line
  523. (car type) visiblep
  524. (not (eq (nth 2 type) 'hidden))
  525. level all-entries unread))
  526. (gnus-topic-update-unreads (car type) unread)
  527. (when gnus-group-update-tool-bar
  528. (gnus-put-text-property beg end 'point-entered
  529. 'gnus-tool-bar-update)
  530. (gnus-put-text-property beg end 'point-left
  531. 'gnus-tool-bar-update))
  532. (goto-char end)
  533. unread))
  534. (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
  535. "Remove the current topic."
  536. (let ((topic (gnus-group-topic-name))
  537. (level (gnus-group-topic-level))
  538. (beg (progn (beginning-of-line) (point)))
  539. buffer-read-only)
  540. (when topic
  541. (while (and (zerop (forward-line 1))
  542. (> (or (gnus-group-topic-level) (1+ level)) level)))
  543. (delete-region beg (point))
  544. ;; Do the change in this rather odd manner because it has been
  545. ;; reported that some topics share parts of some lists, for some
  546. ;; reason. I have been unable to determine why this is the
  547. ;; case, but this hack seems to take care of things.
  548. (let ((data (cadr (gnus-topic-find-topology topic))))
  549. (setcdr data
  550. (list (if insert 'visible 'invisible)
  551. (caddr data)
  552. (cadddr data))))
  553. (if total-remove
  554. (setq gnus-topic-alist
  555. (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
  556. (gnus-topic-insert-topic topic in-level)))))
  557. (defun gnus-topic-insert-topic (topic &optional level)
  558. "Insert TOPIC."
  559. (gnus-group-prepare-topics
  560. (car gnus-group-list-mode) (cdr gnus-group-list-mode)
  561. nil nil topic level))
  562. (defun gnus-topic-fold (&optional insert topic)
  563. "Remove/insert the current topic."
  564. (let ((topic (or topic (gnus-group-topic-name))))
  565. (when topic
  566. (save-excursion
  567. (if (not (gnus-group-active-topic-p))
  568. (gnus-topic-remove-topic
  569. (or insert (not (gnus-topic-visible-p))))
  570. (let ((gnus-topic-topology gnus-topic-active-topology)
  571. (gnus-topic-alist gnus-topic-active-alist)
  572. (gnus-group-list-mode (cons 5 t)))
  573. (gnus-topic-remove-topic
  574. (or insert (not (gnus-topic-visible-p))) nil nil 9)
  575. (gnus-topic-enter-dribble)))))))
  576. (defun gnus-topic-insert-topic-line (name visiblep shownp level entries
  577. &optional unread)
  578. (let* ((visible (if visiblep "" "..."))
  579. (indentation (make-string (* gnus-topic-indent-level level) ? ))
  580. (total-number-of-articles unread)
  581. (number-of-groups (length entries))
  582. (active-topic (eq gnus-topic-alist gnus-topic-active-alist))
  583. gnus-tmp-header)
  584. (gnus-topic-update-unreads name unread)
  585. (beginning-of-line)
  586. ;; Insert the text.
  587. (if shownp
  588. (gnus-add-text-properties
  589. (point)
  590. (prog1 (1+ (point))
  591. (eval gnus-topic-line-format-spec))
  592. (list 'gnus-topic (intern name)
  593. 'gnus-topic-level level
  594. 'gnus-topic-unread unread
  595. 'gnus-active active-topic
  596. 'gnus-topic-visible visiblep)))))
  597. (defun gnus-topic-update-unreads (topic unreads)
  598. (setq gnus-topic-unreads (delq (assoc topic gnus-topic-unreads)
  599. gnus-topic-unreads))
  600. (push (cons topic unreads) gnus-topic-unreads))
  601. (defun gnus-topic-update-topics-containing-group (group)
  602. "Update all topics that have GROUP as a member."
  603. (when (and (eq major-mode 'gnus-group-mode)
  604. gnus-topic-mode)
  605. (save-excursion
  606. (let ((alist gnus-topic-alist))
  607. ;; This is probably not entirely correct. If a topic
  608. ;; isn't shown, then it's not updated. But the updating
  609. ;; should be performed in any case, since the topic's
  610. ;; parent should be updated. Pfft.
  611. (while alist
  612. (when (and (member group (cdar alist))
  613. (gnus-topic-goto-topic (caar alist)))
  614. (gnus-topic-update-topic-line (caar alist)))
  615. (pop alist))))))
  616. (defun gnus-topic-update-topic ()
  617. "Update all parent topics to the current group."
  618. (when (and (eq major-mode 'gnus-group-mode)
  619. gnus-topic-mode)
  620. (let ((group (gnus-group-group-name))
  621. (m (point-marker))
  622. (buffer-read-only nil))
  623. (when (and group
  624. (gnus-get-info group)
  625. (gnus-topic-goto-topic (gnus-current-topic)))
  626. (gnus-topic-update-topic-line (gnus-group-topic-name))
  627. (goto-char m)
  628. (set-marker m nil)
  629. (gnus-group-position-point)))))
  630. (defun gnus-topic-goto-missing-group (group)
  631. "Place point where GROUP is supposed to be inserted."
  632. (let* ((topic (gnus-group-topic group))
  633. (groups (cdr (assoc topic gnus-topic-alist)))
  634. (g (cdr (member group groups)))
  635. (unfound t)
  636. entry)
  637. ;; Try to jump to a visible group.
  638. (while (and g
  639. (not (gnus-group-goto-group (car g) t)))
  640. (pop g))
  641. ;; It wasn't visible, so we try to see where to insert it.
  642. (when (not g)
  643. (setq g (cdr (member group (reverse groups))))
  644. (while (and g unfound)
  645. (when (gnus-group-goto-group (pop g) t)
  646. (forward-line 1)
  647. (setq unfound nil)))
  648. (when (and unfound
  649. topic
  650. (not (gnus-topic-goto-missing-topic topic)))
  651. (gnus-topic-display-missing-topic topic)))))
  652. (defun gnus-topic-display-missing-topic (topic)
  653. "Insert topic lines recursively for missing topics."
  654. (let ((parent (gnus-topic-find-topology
  655. (gnus-topic-parent-topic topic))))
  656. (when (and parent
  657. (not (gnus-topic-goto-missing-topic (caadr parent))))
  658. (gnus-topic-display-missing-topic (caadr parent))))
  659. (gnus-topic-goto-missing-topic topic)
  660. ;; Skip past all groups in the topic we're in.
  661. (while (gnus-group-group-name)
  662. (forward-line 1))
  663. (let* ((top (gnus-topic-find-topology topic))
  664. (children (cddr top))
  665. (type (cadr top))
  666. (unread 0)
  667. (entries (gnus-topic-find-groups
  668. (car type) (car gnus-group-list-mode)
  669. (cdr gnus-group-list-mode)))
  670. entry)
  671. (while children
  672. (incf unread (gnus-topic-unread (caar (pop children)))))
  673. (while (setq entry (pop entries))
  674. (when (numberp (car entry))
  675. (incf unread (car entry))))
  676. (gnus-topic-insert-topic-line
  677. topic t t (car (gnus-topic-find-topology topic)) nil unread)))
  678. (defun gnus-topic-goto-missing-topic (topic)
  679. (if (gnus-topic-goto-topic topic)
  680. (forward-line 1)
  681. ;; Topic not displayed.
  682. (let* ((top (gnus-topic-find-topology
  683. (gnus-topic-parent-topic topic)))
  684. (tp (reverse (cddr top))))
  685. (if (not top)
  686. (gnus-topic-insert-topic-line
  687. topic t t (car (gnus-topic-find-topology topic)) nil 0)
  688. (while (not (equal (caaar tp) topic))
  689. (setq tp (cdr tp)))
  690. (pop tp)
  691. (while (and tp
  692. (not (gnus-topic-goto-topic (caaar tp))))
  693. (pop tp))
  694. (if tp
  695. (gnus-topic-forward-topic 1)
  696. (gnus-topic-goto-missing-topic (caadr top)))))
  697. nil))
  698. (defun gnus-topic-update-topic-line (topic-name &optional reads)
  699. (let* ((top (gnus-topic-find-topology topic-name))
  700. (type (cadr top))
  701. (children (cddr top))
  702. (entries (gnus-topic-find-groups
  703. (car type) (car gnus-group-list-mode)
  704. (cdr gnus-group-list-mode)))
  705. (parent (gnus-topic-parent-topic topic-name))
  706. (all-entries entries)
  707. (unread 0)
  708. old-unread entry new-unread)
  709. (when (gnus-topic-goto-topic (car type))
  710. ;; Tally all the groups that belong in this topic.
  711. (if reads
  712. (setq unread (- (gnus-group-topic-unread) reads))
  713. (while children
  714. (incf unread (gnus-topic-unread (caar (pop children)))))
  715. (while (setq entry (pop entries))
  716. (when (numberp (car entry))
  717. (incf unread (car entry)))))
  718. (setq old-unread (gnus-group-topic-unread))
  719. ;; Insert the topic line.
  720. (gnus-topic-insert-topic-line
  721. (car type) (gnus-topic-visible-p)
  722. (not (eq (nth 2 type) 'hidden))
  723. (gnus-group-topic-level) all-entries unread)
  724. (gnus-delete-line)
  725. (forward-line -1)
  726. (setq new-unread (gnus-group-topic-unread)))
  727. (when parent
  728. (forward-line -1)
  729. (gnus-topic-update-topic-line
  730. parent
  731. (- (or old-unread 0) (or new-unread 0))))
  732. unread))
  733. (defun gnus-topic-group-indentation ()
  734. (make-string
  735. (* gnus-topic-indent-level
  736. (or (save-excursion
  737. (forward-line -1)
  738. (gnus-topic-goto-topic (gnus-current-topic))
  739. (gnus-group-topic-level))
  740. 0))
  741. ? ))
  742. ;;; Initialization
  743. (gnus-add-shutdown 'gnus-topic-close 'gnus)
  744. (defun gnus-topic-close ()
  745. (setq gnus-topic-active-topology nil
  746. gnus-topic-active-alist nil
  747. gnus-topic-killed-topics nil
  748. gnus-topology-checked-p nil))
  749. (defun gnus-topic-check-topology ()
  750. ;; The first time we set the topology to whatever we have
  751. ;; gotten here, which can be rather random.
  752. (unless gnus-topic-alist
  753. (gnus-topic-init-alist))
  754. (setq gnus-topology-checked-p t)
  755. ;; Go through the topic alist and make sure that all topics
  756. ;; are in the topic topology.
  757. (let ((topics (gnus-topic-list))
  758. (alist gnus-topic-alist)
  759. changed)
  760. (while alist
  761. (unless (member (caar alist) topics)
  762. (nconc gnus-topic-topology
  763. (list (list (list (caar alist) 'visible))))
  764. (setq changed t))
  765. (setq alist (cdr alist)))
  766. (when changed
  767. (gnus-topic-enter-dribble))
  768. ;; Conversely, go through the topology and make sure that all
  769. ;; topologies have alists.
  770. (while topics
  771. (unless (assoc (car topics) gnus-topic-alist)
  772. (push (list (car topics)) gnus-topic-alist))
  773. (pop topics)))
  774. ;; Go through all living groups and make sure that
  775. ;; they belong to some topic.
  776. (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
  777. (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
  778. (newsrc (cdr gnus-newsrc-alist))
  779. group)
  780. (while newsrc
  781. (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
  782. (setcdr entry (list group))
  783. (setq entry (cdr entry)))))
  784. ;; Go through all topics and make sure they contain only living groups.
  785. (let ((alist gnus-topic-alist)
  786. topic)
  787. (while (setq topic (pop alist))
  788. (while (cdr topic)
  789. (if (and (cadr topic)
  790. (gnus-group-entry (cadr topic)))
  791. (setq topic (cdr topic))
  792. (setcdr topic (cddr topic)))))))
  793. (defun gnus-topic-init-alist ()
  794. "Initialize the topic structures."
  795. (setq gnus-topic-topology
  796. (cons (list "Gnus" 'visible)
  797. (mapcar (lambda (topic)
  798. (list (list (car topic) 'visible)))
  799. '(("misc")))))
  800. (setq gnus-topic-alist
  801. (list (cons "misc"
  802. (mapcar (lambda (info) (gnus-info-group info))
  803. (cdr gnus-newsrc-alist)))
  804. (list "Gnus")))
  805. (gnus-topic-enter-dribble))
  806. ;;; Maintenance
  807. (defun gnus-topic-clean-alist ()
  808. "Remove bogus groups from the topic alist."
  809. (let ((topic-alist gnus-topic-alist)
  810. result topic)
  811. (unless gnus-killed-hashtb
  812. (gnus-make-hashtable-from-killed))
  813. (while (setq topic (pop topic-alist))
  814. (let ((topic-name (pop topic))
  815. group filtered-topic)
  816. (while (setq group (pop topic))
  817. (when (and (or (gnus-active group)
  818. (gnus-info-method (gnus-get-info group)))
  819. (not (gnus-gethash group gnus-killed-hashtb)))
  820. (push group filtered-topic)))
  821. (push (cons topic-name (nreverse filtered-topic)) result)))
  822. (setq gnus-topic-alist (nreverse result))))
  823. (defun gnus-topic-change-level (group level oldlevel &optional previous)
  824. "Run when changing levels to enter/remove groups from topics."
  825. (with-current-buffer gnus-group-buffer
  826. (let ((buffer-read-only nil))
  827. (unless gnus-topic-inhibit-change-level
  828. (gnus-group-goto-group (or (car (nth 2 previous)) group))
  829. (when (and gnus-topic-mode
  830. gnus-topic-alist
  831. (not gnus-topic-inhibit-change-level))
  832. ;; Remove the group from the topics.
  833. (if (and (< oldlevel gnus-level-zombie)
  834. (>= level gnus-level-zombie))
  835. (let ((alist gnus-topic-alist))
  836. (while (gnus-group-goto-group group)
  837. (gnus-delete-line))
  838. (while alist
  839. (when (member group (car alist))
  840. (setcdr (car alist) (delete group (cdar alist))))
  841. (pop alist)))
  842. ;; If the group is subscribed we enter it into the topics.
  843. (when (and (< level gnus-level-zombie)
  844. (>= oldlevel gnus-level-zombie))
  845. (let* ((prev (gnus-group-group-name))
  846. (gnus-topic-inhibit-change-level t)
  847. (gnus-group-indentation
  848. (make-string
  849. (* gnus-topic-indent-level
  850. (or (save-excursion
  851. (gnus-topic-goto-topic (gnus-current-topic))
  852. (gnus-group-topic-level))
  853. 0))
  854. ? ))
  855. (yanked (list group))
  856. alist talist end)
  857. ;; Then we enter the yanked groups into the topics
  858. ;; they belong to.
  859. (when (setq alist (assoc (save-excursion
  860. (forward-line -1)
  861. (or
  862. (gnus-current-topic)
  863. (caar gnus-topic-topology)))
  864. gnus-topic-alist))
  865. (setq talist alist)
  866. (when (stringp yanked)
  867. (setq yanked (list yanked)))
  868. (if (not prev)
  869. (nconc alist yanked)
  870. (if (not (cdr alist))
  871. (setcdr alist (nconc yanked (cdr alist)))
  872. (while (and (not end) (cdr alist))
  873. (when (equal (cadr alist) prev)
  874. (setcdr alist (nconc yanked (cdr alist)))
  875. (setq end t))
  876. (setq alist (cdr alist)))
  877. (unless end
  878. (nconc talist yanked))))))
  879. (gnus-topic-update-topic))))))))
  880. (defun gnus-topic-goto-next-group (group props)
  881. "Go to group or the next group after group."
  882. (if (not group)
  883. (if (not (memq 'gnus-topic props))
  884. (goto-char (point-max))
  885. (let ((topic (symbol-name (cadr (memq 'gnus-topic props)))))
  886. (or (gnus-topic-goto-topic topic)
  887. (gnus-topic-goto-topic (gnus-topic-next-topic topic)))))
  888. (if (gnus-group-goto-group group)
  889. t
  890. ;; The group is no longer visible.
  891. (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
  892. (topic-visible (save-excursion (gnus-topic-goto-topic (car list))))
  893. (after (and topic-visible (cdr (member group (cdr list))))))
  894. ;; First try to put point on a group after the current one.
  895. (while (and after
  896. (not (gnus-group-goto-group (car after))))
  897. (setq after (cdr after)))
  898. ;; Then try to put point on a group before point.
  899. (unless after
  900. (setq after (cdr (member group (reverse (cdr list)))))
  901. (while (and after
  902. (not (gnus-group-goto-group (car after))))
  903. (setq after (cdr after))))
  904. ;; Finally, just put point on the topic.
  905. (if (not (car list))
  906. (goto-char (point-min))
  907. (unless after
  908. (if topic-visible
  909. (gnus-goto-char topic-visible)
  910. (gnus-topic-goto-topic (gnus-topic-next-topic (car list))))
  911. (setq after nil)))
  912. t))))
  913. ;;; Topic-active functions
  914. (defun gnus-topic-grok-active (&optional force)
  915. "Parse all active groups and create topic structures for them."
  916. ;; First we make sure that we have really read the active file.
  917. (when (or force
  918. (not gnus-topic-active-alist))
  919. (let (groups)
  920. ;; Get a list of all groups available.
  921. (mapatoms (lambda (g) (when (symbol-value g)
  922. (push (symbol-name g) groups)))
  923. gnus-active-hashtb)
  924. (setq groups (sort groups 'string<))
  925. ;; Init the variables.
  926. (setq gnus-topic-active-topology (list (list "" 'visible)))
  927. (setq gnus-topic-active-alist nil)
  928. ;; Descend the top-level hierarchy.
  929. (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
  930. ;; Set the top-level topic names to something nice.
  931. (setcar (car gnus-topic-active-topology) "Gnus active")
  932. (setcar (car gnus-topic-active-alist) "Gnus active"))))
  933. (defun gnus-topic-grok-active-1 (topology groups)
  934. (let* ((name (caar topology))
  935. (prefix (concat "^" (regexp-quote name)))
  936. tgroups ntopology group)
  937. (while (and groups
  938. (string-match prefix (setq group (car groups))))
  939. (if (not (string-match "\\." group (match-end 0)))
  940. ;; There are no further hierarchies here, so we just
  941. ;; enter this group into the list belonging to this
  942. ;; topic.
  943. (push (pop groups) tgroups)
  944. ;; New sub-hierarchy, so we add it to the topology.
  945. (nconc topology (list (setq ntopology
  946. (list (list (substring
  947. group 0 (match-end 0))
  948. 'invisible)))))
  949. ;; Descend the hierarchy.
  950. (setq groups (gnus-topic-grok-active-1 ntopology groups))))
  951. ;; We remove the trailing "." from the topic name.
  952. (setq name
  953. (if (string-match "\\.$" name)
  954. (substring name 0 (match-beginning 0))
  955. name))
  956. ;; Add this topic and its groups to the topic alist.
  957. (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
  958. (setcar (car topology) name)
  959. ;; We return the rest of the groups that didn't belong
  960. ;; to this topic.
  961. groups))
  962. ;;; Topic mode, commands and keymap.
  963. (defvar gnus-topic-mode-map nil)
  964. (defvar gnus-group-topic-map nil)
  965. (unless gnus-topic-mode-map
  966. (setq gnus-topic-mode-map (make-sparse-keymap))
  967. ;; Override certain group mode keys.
  968. (gnus-define-keys gnus-topic-mode-map
  969. "=" gnus-topic-select-group
  970. "\r" gnus-topic-select-group
  971. " " gnus-topic-read-group
  972. "\C-c\C-x" gnus-topic-expire-articles
  973. "c" gnus-topic-catchup-articles
  974. "\C-k" gnus-topic-kill-group
  975. "\C-y" gnus-topic-yank-group
  976. "\M-g" gnus-topic-get-new-news-this-topic
  977. "AT" gnus-topic-list-active
  978. "Gp" gnus-topic-edit-parameters
  979. "#" gnus-topic-mark-topic
  980. "\M-#" gnus-topic-unmark-topic
  981. [tab] gnus-topic-indent
  982. [(meta tab)] gnus-topic-unindent
  983. "\C-i" gnus-topic-indent
  984. "\M-\C-i" gnus-topic-unindent
  985. gnus-mouse-2 gnus-mouse-pick-topic)
  986. ;; Define a new submap.
  987. (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
  988. "#" gnus-topic-mark-topic
  989. "\M-#" gnus-topic-unmark-topic
  990. "n" gnus-topic-create-topic
  991. "m" gnus-topic-move-group
  992. "D" gnus-topic-remove-group
  993. "c" gnus-topic-copy-group
  994. "h" gnus-topic-hide-topic
  995. "s" gnus-topic-show-topic
  996. "j" gnus-topic-jump-to-topic
  997. "M" gnus-topic-move-matching
  998. "C" gnus-topic-copy-matching
  999. "\M-p" gnus-topic-goto-previous-topic
  1000. "\M-n" gnus-topic-goto-next-topic
  1001. "\C-i" gnus-topic-indent
  1002. [tab] gnus-topic-indent
  1003. "r" gnus-topic-rename
  1004. "\177" gnus-topic-delete
  1005. [delete] gnus-topic-delete
  1006. "H" gnus-topic-toggle-display-empty-topics)
  1007. (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
  1008. "s" gnus-topic-sort-groups
  1009. "a" gnus-topic-sort-groups-by-alphabet
  1010. "u" gnus-topic-sort-groups-by-unread
  1011. "l" gnus-topic-sort-groups-by-level
  1012. "e" gnus-topic-sort-groups-by-server
  1013. "v" gnus-topic-sort-groups-by-score
  1014. "r" gnus-topic-sort-groups-by-rank
  1015. "m" gnus-topic-sort-groups-by-method))
  1016. (defun gnus-topic-make-menu-bar ()
  1017. (unless (boundp 'gnus-topic-menu)
  1018. (easy-menu-define
  1019. gnus-topic-menu gnus-topic-mode-map ""
  1020. '("Topics"
  1021. ["Toggle topics" gnus-topic-mode t]
  1022. ("Groups"
  1023. ["Copy..." gnus-topic-copy-group t]
  1024. ["Move..." gnus-topic-move-group t]
  1025. ["Remove" gnus-topic-remove-group t]
  1026. ["Copy matching..." gnus-topic-copy-matching t]
  1027. ["Move matching..." gnus-topic-move-matching t])
  1028. ("Topics"
  1029. ["Goto..." gnus-topic-jump-to-topic t]
  1030. ["Show" gnus-topic-show-topic t]
  1031. ["Hide" gnus-topic-hide-topic t]
  1032. ["Delete" gnus-topic-delete t]
  1033. ["Rename..." gnus-topic-rename t]
  1034. ["Create..." gnus-topic-create-topic t]
  1035. ["Mark" gnus-topic-mark-topic t]
  1036. ["Indent" gnus-topic-indent t]
  1037. ["Sort" gnus-topic-sort-topics t]
  1038. ["Previous topic" gnus-topic-goto-previous-topic t]
  1039. ["Next topic" gnus-topic-goto-next-topic t]
  1040. ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
  1041. ["Edit parameters" gnus-topic-edit-parameters t])
  1042. ["List active" gnus-topic-list-active t]))))
  1043. (defun gnus-topic-mode (&optional arg redisplay)
  1044. "Minor mode for topicsifying Gnus group buffers."
  1045. ;; FIXME: Use define-minor-mode.
  1046. (interactive (list current-prefix-arg t))
  1047. (when (eq major-mode 'gnus-group-mode)
  1048. (make-local-variable 'gnus-topic-mode)
  1049. (setq gnus-topic-mode
  1050. (if (null arg) (not gnus-topic-mode)
  1051. (> (prefix-numeric-value arg) 0)))
  1052. ;; Infest Gnus with topics.
  1053. (if (not gnus-topic-mode)
  1054. (setq gnus-goto-missing-group-function nil)
  1055. (when (gnus-visual-p 'topic-menu 'menu)
  1056. (gnus-topic-make-menu-bar))
  1057. (gnus-set-format 'topic t)
  1058. (add-minor-mode 'gnus-topic-mode " Topic" gnus-topic-mode-map)
  1059. (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
  1060. (set (make-local-variable 'gnus-group-prepare-function)
  1061. 'gnus-group-prepare-topics)
  1062. (set (make-local-variable 'gnus-group-get-parameter-function)
  1063. 'gnus-group-topic-parameters)
  1064. (set (make-local-variable 'gnus-group-goto-next-group-function)
  1065. 'gnus-topic-goto-next-group)
  1066. (set (make-local-variable 'gnus-group-indentation-function)
  1067. 'gnus-topic-group-indentation)
  1068. (set (make-local-variable 'gnus-group-update-group-function)
  1069. 'gnus-topic-update-topics-containing-group)
  1070. (set (make-local-variable 'gnus-group-sort-alist-function)
  1071. 'gnus-group-sort-topic)
  1072. (setq gnus-group-change-level-function 'gnus-topic-change-level)
  1073. (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
  1074. (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
  1075. (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
  1076. nil 'local)
  1077. (setq gnus-topology-checked-p nil)
  1078. ;; We check the topology.
  1079. (when gnus-newsrc-alist
  1080. (gnus-topic-check-topology))
  1081. (gnus-run-hooks 'gnus-topic-mode-hook))
  1082. ;; Remove topic infestation.
  1083. (unless gnus-topic-mode
  1084. (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
  1085. (setq gnus-group-change-level-function nil)
  1086. (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
  1087. (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
  1088. (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
  1089. (when redisplay
  1090. (gnus-group-list-groups))))
  1091. (defun gnus-topic-select-group (&optional all)
  1092. "Select this newsgroup.
  1093. No article is selected automatically.
  1094. If the group is opened, just switch the summary buffer.
  1095. If ALL is non-nil, already read articles become readable.
  1096. If ALL is a positive number, fetch this number of the latest
  1097. articles in the group. If ALL is a negative number, fetch this
  1098. number of the earliest articles in the group.
  1099. If performed over a topic line, toggle folding the topic."
  1100. (interactive "P")
  1101. (when (and (eobp) (not (gnus-group-group-name)))
  1102. (forward-line -1))
  1103. (if (gnus-group-topic-p)
  1104. (let ((gnus-group-list-mode
  1105. (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
  1106. (gnus-topic-fold all)
  1107. (gnus-dribble-touch))
  1108. (gnus-group-select-group all)))
  1109. (defun gnus-mouse-pick-topic (e)
  1110. "Select the group or topic under the mouse pointer."
  1111. (interactive "e")
  1112. (mouse-set-point e)
  1113. (gnus-topic-read-group nil))
  1114. (defun gnus-topic-expire-articles (topic)
  1115. "Expire articles in this topic or group."
  1116. (interactive (list (gnus-group-topic-name)))
  1117. (if (not topic)
  1118. (call-interactively 'gnus-group-expire-articles)
  1119. (save-excursion
  1120. (gnus-message 5 "Expiring groups in %s..." topic)
  1121. (let ((gnus-group-marked
  1122. (mapcar (lambda (entry) (car (nth 2 entry)))
  1123. (gnus-topic-find-groups topic gnus-level-killed t
  1124. nil t))))
  1125. (gnus-group-expire-articles nil))
  1126. (gnus-message 5 "Expiring groups in %s...done" topic))))
  1127. (defun gnus-topic-catchup-articles (topic)
  1128. "Catchup this topic or group.
  1129. Also see `gnus-group-catchup'."
  1130. (interactive (list (gnus-group-topic-name)))
  1131. (if (not topic)
  1132. (call-interactively 'gnus-group-catchup-current)
  1133. (save-excursion
  1134. (let* ((groups
  1135. (mapcar (lambda (entry) (car (nth 2 entry)))
  1136. (gnus-topic-find-groups topic gnus-level-killed t
  1137. nil t)))
  1138. (buffer-read-only nil)
  1139. (gnus-group-marked groups))
  1140. (gnus-group-catchup-current)
  1141. (mapcar 'gnus-topic-update-topics-containing-group groups)))))
  1142. (defun gnus-topic-read-group (&optional all no-article group)
  1143. "Read news in this newsgroup.
  1144. If the prefix argument ALL is non-nil, already read articles become
  1145. readable.
  1146. If ALL is a positive number, fetch this number of the latest
  1147. articles in the group. If ALL is a negative number, fetch this
  1148. number of the earliest articles in the group.
  1149. If the optional argument NO-ARTICLE is non-nil, no article will
  1150. be auto-selected upon group entry. If GROUP is non-nil, fetch
  1151. that group.
  1152. If performed over a topic line, toggle folding the topic."
  1153. (interactive "P")
  1154. (when (and (eobp) (not (gnus-group-group-name)))
  1155. (forward-line -1))
  1156. (if (gnus-group-topic-p)
  1157. (let ((gnus-group-list-mode
  1158. (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
  1159. (gnus-topic-fold all))
  1160. (gnus-group-read-group all no-article group)))
  1161. (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
  1162. "Create a new TOPIC under PARENT.
  1163. When used interactively, PARENT will be the topic under point."
  1164. (interactive
  1165. (list
  1166. (read-string "New topic: ")
  1167. (gnus-current-topic)))
  1168. ;; Check whether this topic already exists.
  1169. (when (gnus-topic-find-topology topic)
  1170. (error "Topic already exists"))
  1171. (unless parent
  1172. (setq parent (caar gnus-topic-topology)))
  1173. (let ((top (cdr (gnus-topic-find-topology parent)))
  1174. (full-topic (or full-topic (list (list topic 'visible nil nil)))))
  1175. (unless top
  1176. (error "No such parent topic: %s" parent))
  1177. (if previous
  1178. (progn
  1179. (while (and (cdr top)
  1180. (not (equal (caaadr top) previous)))
  1181. (setq top (cdr top)))
  1182. (setcdr top (cons full-topic (cdr top))))
  1183. (nconc top (list full-topic)))
  1184. (unless (assoc topic gnus-topic-alist)
  1185. (push (list topic) gnus-topic-alist)))
  1186. (gnus-topic-enter-dribble)
  1187. (gnus-group-list-groups)
  1188. (gnus-topic-goto-topic topic))
  1189. ;; FIXME:
  1190. ;; 1. When the marked groups are overlapped with the process
  1191. ;; region, the behavior of move or remove is not right.
  1192. ;; 2. Can't process on several marked groups with a same name,
  1193. ;; because gnus-group-marked only keeps one copy.
  1194. (defvar gnus-topic-history nil)
  1195. (defun gnus-topic-move-group (n topic &optional copyp)
  1196. "Move the next N groups to TOPIC.
  1197. If COPYP, copy the groups instead."
  1198. (interactive
  1199. (list current-prefix-arg
  1200. (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t
  1201. nil 'gnus-topic-history)))
  1202. (let ((use-marked (and (not n) (not (gnus-region-active-p))
  1203. gnus-group-marked t))
  1204. (groups (gnus-group-process-prefix n))
  1205. (topicl (assoc topic gnus-topic-alist))
  1206. (start-topic (gnus-group-topic-name))
  1207. (start-group (progn (forward-line 1) (gnus-group-group-name)))
  1208. entry)
  1209. (if (and (not groups) (not copyp) start-topic)
  1210. (gnus-topic-move start-topic topic)
  1211. (dolist (g groups)
  1212. (gnus-group-remove-mark g use-marked)
  1213. (when (and
  1214. (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
  1215. (not copyp))
  1216. (setcdr entry (gnus-delete-first g (cdr entry))))
  1217. (nconc topicl (list g)))
  1218. (gnus-topic-enter-dribble)
  1219. (if start-group
  1220. (gnus-group-goto-group start-group)
  1221. (gnus-topic-goto-topic start-topic))
  1222. (gnus-group-list-groups))))
  1223. (defun gnus-topic-remove-group (&optional n)
  1224. "Remove the current group from the topic."
  1225. (interactive "P")
  1226. (let ((use-marked (and (not n) (not (gnus-region-active-p))
  1227. gnus-group-marked t))
  1228. (groups (gnus-group-process-prefix n)))
  1229. (mapc
  1230. (lambda (group)
  1231. (gnus-group-remove-mark group use-marked)
  1232. (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
  1233. (buffer-read-only nil))
  1234. (when (and topicl group)
  1235. (gnus-delete-line)
  1236. (gnus-delete-first group topicl))
  1237. (gnus-topic-update-topic)))
  1238. groups)
  1239. (gnus-topic-enter-dribble)
  1240. (gnus-group-position-point)))
  1241. (defun gnus-topic-copy-group (n topic)
  1242. "Copy the current group to a topic."
  1243. (interactive
  1244. (list current-prefix-arg
  1245. (gnus-completing-read
  1246. "Copy to topic" (mapcar 'car gnus-topic-alist) t)))
  1247. (gnus-topic-move-group n topic t))
  1248. (defun gnus-topic-kill-group (&optional n discard)
  1249. "Kill the next N groups."
  1250. (interactive "P")
  1251. (if (gnus-group-topic-p)
  1252. (let ((topic (gnus-group-topic-name)))
  1253. (push (cons
  1254. (gnus-topic-find-topology topic)
  1255. (assoc topic gnus-topic-alist))
  1256. gnus-topic-killed-topics)
  1257. (gnus-topic-remove-topic nil t)
  1258. (gnus-topic-find-topology topic nil nil gnus-topic-topology)
  1259. (gnus-topic-enter-dribble))
  1260. (gnus-group-kill-group n discard)
  1261. (if (not (gnus-group-topic-p))
  1262. (gnus-topic-update-topic)
  1263. ;; Move up one line so that we update the right topic.
  1264. (forward-line -1)
  1265. (gnus-topic-update-topic)
  1266. (forward-line 1))))
  1267. (defun gnus-topic-yank-group (&optional arg)
  1268. "Yank the last topic."
  1269. (interactive "p")
  1270. (if gnus-topic-killed-topics
  1271. (let* ((previous
  1272. (or (gnus-group-topic-name)
  1273. (gnus-topic-next-topic (gnus-current-topic))))
  1274. (data (pop gnus-topic-killed-topics))
  1275. (alist (cdr data))
  1276. (item (cdar data)))
  1277. (push alist gnus-topic-alist)
  1278. (gnus-topic-create-topic
  1279. (caar item) (gnus-topic-parent-topic previous) previous
  1280. item)
  1281. (gnus-topic-enter-dribble)
  1282. (gnus-topic-goto-topic (caar item)))
  1283. (let* ((prev (gnus-group-group-name))
  1284. (gnus-topic-inhibit-change-level t)
  1285. (gnus-group-indentation
  1286. (make-string
  1287. (* gnus-topic-indent-level
  1288. (or (save-excursion
  1289. (gnus-topic-goto-topic (gnus-current-topic))
  1290. (gnus-group-topic-level))
  1291. 0))
  1292. ? ))
  1293. yanked alist)
  1294. ;; We first yank the groups the normal way...
  1295. (setq yanked (gnus-group-yank-group arg))
  1296. ;; Then we enter the yanked groups into the topics they belong
  1297. ;; to.
  1298. (setq alist (assoc (save-excursion
  1299. (forward-line -1)
  1300. (gnus-current-topic))
  1301. gnus-topic-alist))
  1302. (when (stringp yanked)
  1303. (setq yanked (list yanked)))
  1304. (if (not prev)
  1305. (nconc alist yanked)
  1306. (if (not (cdr alist))
  1307. (setcdr alist (nconc yanked (cdr alist)))
  1308. (while (cdr alist)
  1309. (when (equal (cadr alist) prev)
  1310. (setcdr alist (nconc yanked (cdr alist)))
  1311. (setq alist nil))
  1312. (setq alist (cdr alist))))))
  1313. (gnus-topic-update-topic)))
  1314. (defun gnus-topic-hide-topic (&optional permanent)
  1315. "Hide the current topic.
  1316. If PERMANENT, make it stay hidden in subsequent sessions as well."
  1317. (interactive "P")
  1318. (when (gnus-current-topic)
  1319. (gnus-topic-goto-topic (gnus-current-topic))
  1320. (if permanent
  1321. (setcar (cddr
  1322. (cadr
  1323. (gnus-topic-find-topology (gnus-current-topic))))
  1324. 'hidden))
  1325. (gnus-topic-remove-topic nil nil)))
  1326. (defun gnus-topic-show-topic (&optional permanent)
  1327. "Show the hidden topic.
  1328. If PERMANENT, make it stay shown in subsequent sessions as well."
  1329. (interactive "P")
  1330. (when (gnus-group-topic-p)
  1331. (if (not permanent)
  1332. (gnus-topic-remove-topic t nil)
  1333. (let ((topic
  1334. (gnus-topic-find-topology
  1335. (gnus-completing-read "Show topic"
  1336. (mapcar 'car gnus-topic-alist) t))))
  1337. (setcar (cddr (cadr topic)) nil)
  1338. (setcar (cdr (cadr topic)) 'visible)
  1339. (gnus-group-list-groups)))))
  1340. (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
  1341. "Mark all groups in the TOPIC with the process mark.
  1342. If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
  1343. (interactive (list (gnus-group-topic-name)
  1344. nil
  1345. (and current-prefix-arg t)))
  1346. (if (not topic)
  1347. (call-interactively 'gnus-group-mark-group)
  1348. (save-excursion
  1349. (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
  1350. (not non-recursive))))
  1351. (while groups
  1352. (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
  1353. (gnus-info-group (nth 2 (pop groups)))))))))
  1354. (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
  1355. "Remove the process mark from all groups in the TOPIC.
  1356. If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
  1357. (interactive (list (gnus-group-topic-name)
  1358. nil
  1359. (and current-prefix-arg t)))
  1360. (if (not topic)
  1361. (call-interactively 'gnus-group-unmark-group)
  1362. (gnus-topic-mark-topic topic t non-recursive)))
  1363. (defun gnus-topic-get-new-news-this-topic (&optional n)
  1364. "Check for new news in the current topic."
  1365. (interactive "P")
  1366. (if (not (gnus-group-topic-p))
  1367. (gnus-group-get-new-news-this-group n)
  1368. (let* ((topic (gnus-group-topic-name))
  1369. (data (cadr (gnus-topic-find-topology topic))))
  1370. (save-excursion
  1371. (gnus-topic-mark-topic topic nil (and n t))
  1372. (gnus-group-get-new-news-this-group))
  1373. (gnus-topic-remove-topic (eq 'visible (cadr data))))))
  1374. (defun gnus-topic-move-matching (regexp topic &optional copyp)
  1375. "Move all groups that match REGEXP to some topic."
  1376. (interactive
  1377. (let (topic)
  1378. (nreverse
  1379. (list
  1380. (setq topic (gnus-completing-read "Move to topic"
  1381. (mapcar 'car gnus-topic-alist) t))
  1382. (read-string (format "Move to %s (regexp): " topic))))))
  1383. (gnus-group-mark-regexp regexp)
  1384. (gnus-topic-move-group nil topic copyp))
  1385. (defun gnus-topic-copy-matching (regexp topic &optional copyp)
  1386. "Copy all groups that match REGEXP to some topic."
  1387. (interactive
  1388. (let (topic)
  1389. (nreverse
  1390. (list
  1391. (setq topic (gnus-completing-read "Copy to topic"
  1392. (mapcar 'car gnus-topic-alist) t))
  1393. (read-string (format "Copy to %s (regexp): " topic))))))
  1394. (gnus-topic-move-matching regexp topic t))
  1395. (defun gnus-topic-delete (topic)
  1396. "Delete a topic."
  1397. (interactive (list (gnus-group-topic-name)))
  1398. (unless topic
  1399. (error "No topic to be deleted"))
  1400. (let ((entry (assoc topic gnus-topic-alist))
  1401. (buffer-read-only nil))
  1402. (when (cdr entry)
  1403. (error "Topic not empty"))
  1404. ;; Delete if visible.
  1405. (when (gnus-topic-goto-topic topic)
  1406. (gnus-delete-line))
  1407. ;; Remove from alist.
  1408. (setq gnus-topic-alist (delq entry gnus-topic-alist))
  1409. ;; Remove from topology.
  1410. (gnus-topic-find-topology topic nil nil 'delete)
  1411. (gnus-dribble-touch)))
  1412. (defun gnus-topic-rename (old-name new-name)
  1413. "Rename a topic."
  1414. (interactive
  1415. (let ((topic (gnus-current-topic)))
  1416. (list topic
  1417. (read-string (format "Rename %s to: " topic) topic))))
  1418. ;; Check whether the new name exists.
  1419. (when (gnus-topic-find-topology new-name)
  1420. (error "Topic '%s' already exists" new-name))
  1421. ;; "nil" is an invalid name, for reasons I'd rather not go
  1422. ;; into here. Trust me.
  1423. (when (equal new-name "nil")
  1424. (error "Invalid name: %s" nil))
  1425. ;; Do the renaming.
  1426. (let ((top (gnus-topic-find-topology old-name))
  1427. (entry (assoc old-name gnus-topic-alist)))
  1428. (when top
  1429. (setcar (cadr top) new-name))
  1430. (when entry
  1431. (setcar entry new-name))
  1432. (forward-line -1)
  1433. (gnus-dribble-touch)
  1434. (gnus-group-list-groups)
  1435. (forward-line 1)))
  1436. (defun gnus-topic-indent (&optional unindent)
  1437. "Indent a topic -- make it a sub-topic of the previous topic.
  1438. If UNINDENT, remove an indentation."
  1439. (interactive "P")
  1440. (if unindent
  1441. (gnus-topic-unindent)
  1442. (let* ((topic (gnus-current-topic))
  1443. (parent (gnus-topic-previous-topic topic))
  1444. (buffer-read-only nil))
  1445. (unless parent
  1446. (error "Nothing to indent %s into" topic))
  1447. (when topic
  1448. (gnus-topic-goto-topic topic)
  1449. (gnus-topic-kill-group)
  1450. (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
  1451. (gnus-topic-create-topic
  1452. topic parent nil (cdar (car gnus-topic-killed-topics)))
  1453. (pop gnus-topic-killed-topics)
  1454. (or (gnus-topic-goto-topic topic)
  1455. (gnus-topic-goto-topic parent))))))
  1456. (defun gnus-topic-unindent ()
  1457. "Unindent a topic."
  1458. (interactive)
  1459. (let* ((topic (gnus-current-topic))
  1460. (parent (gnus-topic-parent-topic topic))
  1461. (grandparent (gnus-topic-parent-topic parent)))
  1462. (unless grandparent
  1463. (error "Nothing to indent %s into" topic))
  1464. (when topic
  1465. (gnus-topic-goto-topic topic)
  1466. (gnus-topic-kill-group)
  1467. (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
  1468. (gnus-topic-create-topic
  1469. topic grandparent (gnus-topic-next-topic parent)
  1470. (cdar (car gnus-topic-killed-topics)))
  1471. (pop gnus-topic-killed-topics)
  1472. (gnus-topic-goto-topic topic))))
  1473. (defun gnus-topic-list-active (&optional force)
  1474. "List all groups that Gnus knows about in a topicsified fashion.
  1475. If FORCE, always re-read the active file."
  1476. (interactive "P")
  1477. (when force
  1478. (gnus-get-killed-groups))
  1479. (gnus-topic-grok-active force)
  1480. (let ((gnus-topic-topology gnus-topic-active-topology)
  1481. (gnus-topic-alist gnus-topic-active-alist)
  1482. gnus-killed-list gnus-zombie-list)
  1483. (gnus-group-list-groups gnus-level-killed nil 1)))
  1484. (defun gnus-topic-toggle-display-empty-topics ()
  1485. "Show/hide topics that have no unread articles."
  1486. (interactive)
  1487. (setq gnus-topic-display-empty-topics
  1488. (not gnus-topic-display-empty-topics))
  1489. (gnus-group-list-groups)
  1490. (message "%s empty topics"
  1491. (if gnus-topic-display-empty-topics
  1492. "Showing" "Hiding")))
  1493. ;;; Topic sorting functions
  1494. (defun gnus-topic-edit-parameters (group)
  1495. "Edit the group parameters of GROUP.
  1496. If performed on a topic, edit the topic parameters instead."
  1497. (interactive (list (gnus-group-group-name)))
  1498. (if group
  1499. (gnus-group-edit-group-parameters group)
  1500. (if (not (gnus-group-topic-p))
  1501. (error "Nothing to edit on the current line")
  1502. (let ((topic (gnus-group-topic-name)))
  1503. (gnus-edit-form
  1504. (gnus-topic-parameters topic)
  1505. (format "Editing the topic parameters for `%s'."
  1506. (or group topic))
  1507. `(lambda (form)
  1508. (gnus-topic-set-parameters ,topic form)))))))
  1509. (defun gnus-group-sort-topic (func reverse)
  1510. "Sort groups in the topics according to FUNC and REVERSE."
  1511. (let ((alist gnus-topic-alist))
  1512. (while alist
  1513. ;; !!!Sometimes nil elements sneak into the alist,
  1514. ;; for some reason or other.
  1515. (setcar alist (delq nil (car alist)))
  1516. (setcar alist (delete "dummy.group" (car alist)))
  1517. (gnus-topic-sort-topic (pop alist) func reverse))))
  1518. (defun gnus-topic-sort-topic (topic func reverse)
  1519. ;; Each topic only lists the name of the group, while
  1520. ;; the sort predicates expect group infos as inputs.
  1521. ;; So we first transform the group names into infos,
  1522. ;; then sort, and then transform back into group names.
  1523. (setcdr
  1524. topic
  1525. (mapcar
  1526. (lambda (info) (gnus-info-group info))
  1527. (sort
  1528. (mapcar
  1529. (lambda (group) (gnus-get-info group))
  1530. (cdr topic))
  1531. func)))
  1532. ;; Do the reversal, if necessary.
  1533. (when reverse
  1534. (setcdr topic (nreverse (cdr topic)))))
  1535. (defun gnus-topic-sort-groups (func &optional reverse)
  1536. "Sort the current topic according to FUNC.
  1537. If REVERSE, reverse the sorting order."
  1538. (interactive (list gnus-group-sort-function current-prefix-arg))
  1539. (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
  1540. (gnus-topic-sort-topic
  1541. topic (gnus-make-sort-function func) reverse)
  1542. (gnus-group-list-groups)))
  1543. (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
  1544. "Sort the current topic alphabetically by group name.
  1545. If REVERSE, sort in reverse order."
  1546. (interactive "P")
  1547. (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
  1548. (defun gnus-topic-sort-groups-by-unread (&optional reverse)
  1549. "Sort the current topic by number of unread articles.
  1550. If REVERSE, sort in reverse order."
  1551. (interactive "P")
  1552. (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
  1553. (defun gnus-topic-sort-groups-by-level (&optional reverse)
  1554. "Sort the current topic by group level.
  1555. If REVERSE, sort in reverse order."
  1556. (interactive "P")
  1557. (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
  1558. (defun gnus-topic-sort-groups-by-score (&optional reverse)
  1559. "Sort the current topic by group score.
  1560. If REVERSE, sort in reverse order."
  1561. (interactive "P")
  1562. (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
  1563. (defun gnus-topic-sort-groups-by-rank (&optional reverse)
  1564. "Sort the current topic by group rank.
  1565. If REVERSE, sort in reverse order."
  1566. (interactive "P")
  1567. (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
  1568. (defun gnus-topic-sort-groups-by-method (&optional reverse)
  1569. "Sort the current topic alphabetically by backend name.
  1570. If REVERSE, sort in reverse order."
  1571. (interactive "P")
  1572. (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
  1573. (defun gnus-topic-sort-groups-by-server (&optional reverse)
  1574. "Sort the current topic alphabetically by server name.
  1575. If REVERSE, sort in reverse order."
  1576. (interactive "P")
  1577. (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
  1578. (defun gnus-topic-sort-topics-1 (top reverse)
  1579. (if (cdr top)
  1580. (let ((subtop
  1581. (mapcar (gnus-byte-compile
  1582. `(lambda (top)
  1583. (gnus-topic-sort-topics-1 top ,reverse)))
  1584. (sort (cdr top)
  1585. (lambda (t1 t2)
  1586. (string-lessp (caar t1) (caar t2)))))))
  1587. (setcdr top (if reverse (reverse subtop) subtop))))
  1588. top)
  1589. (defun gnus-topic-sort-topics (&optional topic reverse)
  1590. "Sort topics in TOPIC alphabetically by topic name.
  1591. If REVERSE, reverse the sorting order."
  1592. (interactive
  1593. (list (gnus-completing-read "Sort topics in"
  1594. (mapcar 'car gnus-topic-alist) t
  1595. (gnus-current-topic))
  1596. current-prefix-arg))
  1597. (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
  1598. gnus-topic-topology)))
  1599. (gnus-topic-sort-topics-1 topic-topology reverse)
  1600. (gnus-topic-enter-dribble)
  1601. (gnus-group-list-groups)
  1602. (gnus-topic-goto-topic topic)))
  1603. (defun gnus-topic-move (current to)
  1604. "Move the CURRENT topic to TO."
  1605. (interactive
  1606. (list
  1607. (gnus-group-topic-name)
  1608. (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t)))
  1609. (unless (and current to)
  1610. (error "Can't find topic"))
  1611. (let ((current-top (cdr (gnus-topic-find-topology current)))
  1612. (to-top (cdr (gnus-topic-find-topology to))))
  1613. (unless current-top
  1614. (error "Can't find topic `%s'" current))
  1615. (unless to-top
  1616. (error "Can't find topic `%s'" to))
  1617. (if (gnus-topic-find-topology to current-top 0);; Don't care the level
  1618. (error "Can't move `%s' to its sub-level" current))
  1619. (gnus-topic-find-topology current nil nil 'delete)
  1620. (setcdr (last to-top) (list current-top))
  1621. (gnus-topic-enter-dribble)
  1622. (gnus-group-list-groups)
  1623. (gnus-topic-goto-topic current)))
  1624. (defun gnus-subscribe-topics (newsgroup)
  1625. (catch 'end
  1626. (let (match gnus-group-change-level-function)
  1627. (dolist (topic (gnus-topic-list))
  1628. (when (and (setq match (cdr (assq 'subscribe
  1629. (gnus-topic-parameters topic))))
  1630. (string-match match newsgroup))
  1631. ;; Just subscribe the group.
  1632. (gnus-subscribe-alphabetically newsgroup)
  1633. ;; Add the group to the topic.
  1634. (nconc (assoc topic gnus-topic-alist) (list newsgroup))
  1635. ;; if this topic specifies a default level, use it
  1636. (let ((subscribe-level (cdr (assq 'subscribe-level
  1637. (gnus-topic-parameters topic)))))
  1638. (when subscribe-level
  1639. (gnus-group-change-level newsgroup subscribe-level
  1640. gnus-level-default-subscribed)))
  1641. (throw 'end t)))
  1642. nil)))
  1643. (provide 'gnus-topic)
  1644. ;;; gnus-topic.el ends here