gnus-topic.el 59 KB

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