gnus-topic.el 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  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. (topic (car type))
  448. info entry end active tick)
  449. ;; Insert any sub-topics.
  450. (while topicl
  451. (incf unread
  452. (gnus-topic-prepare-topic
  453. (pop topicl) (1+ level) list-level predicate
  454. (not visiblep) lowest regexp)))
  455. (setq end (point))
  456. (goto-char beg)
  457. ;; Insert all the groups that belong in this topic.
  458. (while (setq entry (pop entries))
  459. (when (if (stringp entry)
  460. (gnus-group-prepare-logic
  461. entry
  462. (and
  463. (or (not gnus-group-listed-groups)
  464. (if (< list-level gnus-level-zombie) nil
  465. (let ((entry-level
  466. (if (member entry gnus-zombie-list)
  467. gnus-level-zombie gnus-level-killed)))
  468. (and (<= entry-level list-level)
  469. (>= entry-level lowest)))))
  470. (cond
  471. ((stringp regexp)
  472. (string-match regexp entry))
  473. ((functionp regexp)
  474. (funcall regexp entry))
  475. ((null regexp) t)
  476. (t nil))))
  477. (setq info (nth 2 entry))
  478. (gnus-group-prepare-logic
  479. (gnus-info-group info)
  480. (and (or (not gnus-group-listed-groups)
  481. (let ((entry-level (gnus-info-level info)))
  482. (and (<= entry-level list-level)
  483. (>= entry-level lowest))))
  484. (or (not (functionp predicate))
  485. (funcall predicate info))
  486. (or (not (stringp regexp))
  487. (string-match regexp (gnus-info-group info))))))
  488. (when visiblep
  489. (if (stringp entry)
  490. ;; Dead groups.
  491. (gnus-group-insert-group-line
  492. entry (if (member entry gnus-zombie-list)
  493. gnus-level-zombie gnus-level-killed)
  494. nil (- (1+ (cdr (setq active (gnus-active entry))))
  495. (car active))
  496. nil)
  497. ;; Living groups.
  498. (when (setq info (nth 2 entry))
  499. (gnus-group-insert-group-line
  500. (gnus-info-group info)
  501. (gnus-info-level info) (gnus-info-marks info)
  502. (car entry) (gnus-info-method info)))))
  503. (when (and (listp entry)
  504. (numberp (car entry)))
  505. (incf unread (car entry)))
  506. (when (listp entry)
  507. (setq tick t))))
  508. (goto-char beg)
  509. ;; Insert the topic line.
  510. (when (and (not silent)
  511. (or gnus-topic-display-empty-topics ;We want empty topics
  512. (not (zerop unread)) ;Non-empty
  513. tick ;Ticked articles
  514. (/= point-max (point-max)))) ;Inactive groups
  515. (gnus-extent-start-open (point))
  516. (gnus-topic-insert-topic-line
  517. (car type) visiblep
  518. (not (eq (nth 2 type) 'hidden))
  519. level all-entries unread))
  520. (gnus-topic-update-unreads (car type) unread)
  521. (gnus-group--setup-tool-bar-update beg end)
  522. (goto-char end)
  523. unread))
  524. (defun gnus-topic-remove-topic (&optional insert total-remove hide in-level)
  525. "Remove the current topic."
  526. (let ((topic (gnus-group-topic-name))
  527. (level (gnus-group-topic-level))
  528. (beg (progn (beginning-of-line) (point)))
  529. buffer-read-only)
  530. (when topic
  531. (while (and (zerop (forward-line 1))
  532. (> (or (gnus-group-topic-level) (1+ level)) level)))
  533. (delete-region beg (point))
  534. ;; Do the change in this rather odd manner because it has been
  535. ;; reported that some topics share parts of some lists, for some
  536. ;; reason. I have been unable to determine why this is the
  537. ;; case, but this hack seems to take care of things.
  538. (let ((data (cadr (gnus-topic-find-topology topic))))
  539. (setcdr data
  540. (list (if insert 'visible 'invisible)
  541. (caddr data)
  542. (cadddr data))))
  543. (if total-remove
  544. (setq gnus-topic-alist
  545. (delq (assoc topic gnus-topic-alist) gnus-topic-alist))
  546. (gnus-topic-insert-topic topic in-level)))))
  547. (defun gnus-topic-insert-topic (topic &optional level)
  548. "Insert TOPIC."
  549. (gnus-group-prepare-topics
  550. (car gnus-group-list-mode) (cdr gnus-group-list-mode)
  551. nil nil topic level))
  552. (defun gnus-topic-fold (&optional insert topic)
  553. "Remove/insert the current topic."
  554. (let ((topic (or topic (gnus-group-topic-name))))
  555. (when topic
  556. (save-excursion
  557. (if (not (gnus-group-active-topic-p))
  558. (gnus-topic-remove-topic
  559. (or insert (not (gnus-topic-visible-p))))
  560. (let ((gnus-topic-topology gnus-topic-active-topology)
  561. (gnus-topic-alist gnus-topic-active-alist)
  562. (gnus-group-list-mode (cons 5 t)))
  563. (gnus-topic-remove-topic
  564. (or insert (not (gnus-topic-visible-p))) nil nil 9)
  565. (gnus-topic-enter-dribble)))))))
  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. entry)
  627. ;; Try to jump to a visible group.
  628. (while (and g
  629. (not (gnus-group-goto-group (car g) t)))
  630. (pop g))
  631. ;; It wasn't visible, so we try to see where to insert it.
  632. (when (not g)
  633. (setq g (cdr (member group (reverse groups))))
  634. (while (and g unfound)
  635. (when (gnus-group-goto-group (pop g) t)
  636. (forward-line 1)
  637. (setq unfound nil)))
  638. (when (and unfound
  639. topic
  640. (not (gnus-topic-goto-missing-topic topic)))
  641. (gnus-topic-display-missing-topic topic)))))
  642. (defun gnus-topic-display-missing-topic (topic)
  643. "Insert topic lines recursively for missing topics."
  644. (let ((parent (gnus-topic-find-topology
  645. (gnus-topic-parent-topic topic))))
  646. (when (and parent
  647. (not (gnus-topic-goto-missing-topic (caadr parent))))
  648. (gnus-topic-display-missing-topic (caadr parent))))
  649. (gnus-topic-goto-missing-topic topic)
  650. ;; Skip past all groups in the topic we're in.
  651. (while (gnus-group-group-name)
  652. (forward-line 1))
  653. (let* ((top (gnus-topic-find-topology topic))
  654. (children (cddr top))
  655. (type (cadr top))
  656. (unread 0)
  657. (entries (gnus-topic-find-groups
  658. (car type) (car gnus-group-list-mode)
  659. (cdr gnus-group-list-mode)))
  660. entry)
  661. (while children
  662. (incf unread (gnus-topic-unread (caar (pop children)))))
  663. (while (setq entry (pop entries))
  664. (when (numberp (car entry))
  665. (incf unread (car entry))))
  666. (gnus-topic-insert-topic-line
  667. topic t t (car (gnus-topic-find-topology topic)) nil unread)))
  668. (defun gnus-topic-goto-missing-topic (topic)
  669. (if (gnus-topic-goto-topic topic)
  670. (forward-line 1)
  671. ;; Topic not displayed.
  672. (let* ((top (gnus-topic-find-topology
  673. (gnus-topic-parent-topic topic)))
  674. (tp (reverse (cddr top))))
  675. (if (not top)
  676. (gnus-topic-insert-topic-line
  677. topic t t (car (gnus-topic-find-topology topic)) nil 0)
  678. (while (not (equal (caaar tp) topic))
  679. (setq tp (cdr tp)))
  680. (pop tp)
  681. (while (and tp
  682. (not (gnus-topic-goto-topic (caaar tp))))
  683. (pop tp))
  684. (if tp
  685. (gnus-topic-forward-topic 1)
  686. (gnus-topic-goto-missing-topic (caadr top)))))
  687. nil))
  688. (defun gnus-topic-update-topic-line (topic-name &optional reads)
  689. (let* ((top (gnus-topic-find-topology topic-name))
  690. (type (cadr top))
  691. (children (cddr top))
  692. (entries (gnus-topic-find-groups
  693. (car type) (car gnus-group-list-mode)
  694. (cdr gnus-group-list-mode)))
  695. (parent (gnus-topic-parent-topic topic-name))
  696. (all-entries entries)
  697. (unread 0)
  698. old-unread entry new-unread)
  699. (when (gnus-topic-goto-topic (car type))
  700. ;; Tally all the groups that belong in this topic.
  701. (if reads
  702. (setq unread (- (gnus-group-topic-unread) reads))
  703. (while children
  704. (incf unread (gnus-topic-unread (caar (pop children)))))
  705. (while (setq entry (pop entries))
  706. (when (numberp (car entry))
  707. (incf unread (car entry)))))
  708. (setq old-unread (gnus-group-topic-unread))
  709. ;; Insert the topic line.
  710. (gnus-topic-insert-topic-line
  711. (car type) (gnus-topic-visible-p)
  712. (not (eq (nth 2 type) 'hidden))
  713. (gnus-group-topic-level) all-entries unread)
  714. (gnus-delete-line)
  715. (forward-line -1)
  716. (setq new-unread (gnus-group-topic-unread)))
  717. (when parent
  718. (forward-line -1)
  719. (gnus-topic-update-topic-line
  720. parent
  721. (- (or old-unread 0) (or new-unread 0))))
  722. unread))
  723. (defun gnus-topic-group-indentation ()
  724. (make-string
  725. (* gnus-topic-indent-level
  726. (or (save-excursion
  727. (forward-line -1)
  728. (gnus-topic-goto-topic (gnus-current-topic))
  729. (gnus-group-topic-level))
  730. 0))
  731. ? ))
  732. ;;; Initialization
  733. (gnus-add-shutdown 'gnus-topic-close 'gnus)
  734. (defun gnus-topic-close ()
  735. (setq gnus-topic-active-topology nil
  736. gnus-topic-active-alist nil
  737. gnus-topic-killed-topics nil
  738. gnus-topology-checked-p nil))
  739. (defun gnus-topic-check-topology ()
  740. ;; The first time we set the topology to whatever we have
  741. ;; gotten here, which can be rather random.
  742. (unless gnus-topic-alist
  743. (gnus-topic-init-alist))
  744. (setq gnus-topology-checked-p t)
  745. ;; Go through the topic alist and make sure that all topics
  746. ;; are in the topic topology.
  747. (let ((topics (gnus-topic-list))
  748. (alist gnus-topic-alist)
  749. changed)
  750. (while alist
  751. (unless (member (caar alist) topics)
  752. (nconc gnus-topic-topology
  753. (list (list (list (caar alist) 'visible))))
  754. (setq changed t))
  755. (setq alist (cdr alist)))
  756. (when changed
  757. (gnus-topic-enter-dribble))
  758. ;; Conversely, go through the topology and make sure that all
  759. ;; topologies have alists.
  760. (while topics
  761. (unless (assoc (car topics) gnus-topic-alist)
  762. (push (list (car topics)) gnus-topic-alist))
  763. (pop topics)))
  764. ;; Go through all living groups and make sure that
  765. ;; they belong to some topic.
  766. (let* ((tgroups (apply 'append (mapcar 'cdr gnus-topic-alist)))
  767. (entry (last (assoc (caar gnus-topic-topology) gnus-topic-alist)))
  768. (newsrc (cdr gnus-newsrc-alist))
  769. group)
  770. (while newsrc
  771. (unless (member (setq group (gnus-info-group (pop newsrc))) tgroups)
  772. (setcdr entry (list group))
  773. (setq entry (cdr entry)))))
  774. ;; Go through all topics and make sure they contain only living groups.
  775. (let ((alist gnus-topic-alist)
  776. topic)
  777. (while (setq topic (pop alist))
  778. (while (cdr topic)
  779. (if (and (cadr topic)
  780. (gnus-group-entry (cadr topic)))
  781. (setq topic (cdr topic))
  782. (setcdr topic (cddr topic)))))))
  783. (defun gnus-topic-init-alist ()
  784. "Initialize the topic structures."
  785. (setq gnus-topic-topology
  786. (cons (list "Gnus" 'visible)
  787. (mapcar (lambda (topic)
  788. (list (list (car topic) 'visible)))
  789. '(("misc")))))
  790. (setq gnus-topic-alist
  791. (list (cons "misc"
  792. (mapcar (lambda (info) (gnus-info-group info))
  793. (cdr gnus-newsrc-alist)))
  794. (list "Gnus")))
  795. (gnus-topic-enter-dribble))
  796. ;;; Maintenance
  797. (defun gnus-topic-clean-alist ()
  798. "Remove bogus groups from the topic alist."
  799. (let ((topic-alist gnus-topic-alist)
  800. result topic)
  801. (unless gnus-killed-hashtb
  802. (gnus-make-hashtable-from-killed))
  803. (while (setq topic (pop topic-alist))
  804. (let ((topic-name (pop topic))
  805. group filtered-topic)
  806. (while (setq group (pop topic))
  807. (when (and (or (gnus-active group)
  808. (gnus-info-method (gnus-get-info group)))
  809. (not (gnus-gethash group gnus-killed-hashtb)))
  810. (push group filtered-topic)))
  811. (push (cons topic-name (nreverse filtered-topic)) result)))
  812. (setq gnus-topic-alist (nreverse result))))
  813. (defun gnus-topic-change-level (group level oldlevel &optional previous)
  814. "Run when changing levels to enter/remove groups from topics."
  815. (with-current-buffer gnus-group-buffer
  816. (let ((inhibit-read-only t))
  817. (unless gnus-topic-inhibit-change-level
  818. (gnus-group-goto-group (or (car (nth 2 previous)) group))
  819. (when (and gnus-topic-mode
  820. gnus-topic-alist
  821. (not gnus-topic-inhibit-change-level))
  822. ;; Remove the group from the topics.
  823. (if (and (< oldlevel gnus-level-zombie)
  824. (>= level gnus-level-zombie))
  825. (let ((alist gnus-topic-alist))
  826. (while (gnus-group-goto-group group)
  827. (gnus-delete-line))
  828. (while alist
  829. (when (member group (car alist))
  830. (setcdr (car alist) (delete group (cdar alist))))
  831. (pop alist)))
  832. ;; If the group is subscribed we enter it into the topics.
  833. (when (and (< level gnus-level-zombie)
  834. (>= oldlevel gnus-level-zombie))
  835. (let* ((prev (gnus-group-group-name))
  836. (gnus-topic-inhibit-change-level t)
  837. (gnus-group-indentation
  838. (make-string
  839. (* gnus-topic-indent-level
  840. (or (save-excursion
  841. (gnus-topic-goto-topic (gnus-current-topic))
  842. (gnus-group-topic-level))
  843. 0))
  844. ? ))
  845. (yanked (list group))
  846. alist talist end)
  847. ;; Then we enter the yanked groups into the topics
  848. ;; they belong to.
  849. (when (setq alist (assoc (save-excursion
  850. (forward-line -1)
  851. (or
  852. (gnus-current-topic)
  853. (caar gnus-topic-topology)))
  854. gnus-topic-alist))
  855. (setq talist alist)
  856. (when (stringp yanked)
  857. (setq yanked (list yanked)))
  858. (if (not prev)
  859. (nconc alist yanked)
  860. (if (not (cdr alist))
  861. (setcdr alist (nconc yanked (cdr alist)))
  862. (while (and (not end) (cdr alist))
  863. (when (equal (cadr alist) prev)
  864. (setcdr alist (nconc yanked (cdr alist)))
  865. (setq end t))
  866. (setq alist (cdr alist)))
  867. (unless end
  868. (nconc talist yanked))))))
  869. (gnus-topic-update-topic))))))))
  870. (defun gnus-topic-goto-next-group (group props)
  871. "Go to group or the next group after group."
  872. (if (not group)
  873. (if (not (memq 'gnus-topic props))
  874. (goto-char (point-max))
  875. (let ((topic (symbol-name (cadr (memq 'gnus-topic props)))))
  876. (or (gnus-topic-goto-topic topic)
  877. (gnus-topic-goto-topic (gnus-topic-next-topic topic)))))
  878. (if (gnus-group-goto-group group)
  879. t
  880. ;; The group is no longer visible.
  881. (let* ((list (assoc (gnus-group-topic group) gnus-topic-alist))
  882. (topic-visible (save-excursion (gnus-topic-goto-topic (car list))))
  883. (after (and topic-visible (cdr (member group (cdr list))))))
  884. ;; First try to put point on a group after the current one.
  885. (while (and after
  886. (not (gnus-group-goto-group (car after))))
  887. (setq after (cdr after)))
  888. ;; Then try to put point on a group before point.
  889. (unless after
  890. (setq after (cdr (member group (reverse (cdr list)))))
  891. (while (and after
  892. (not (gnus-group-goto-group (car after))))
  893. (setq after (cdr after))))
  894. ;; Finally, just put point on the topic.
  895. (if (not (car list))
  896. (goto-char (point-min))
  897. (unless after
  898. (if topic-visible
  899. (gnus-goto-char topic-visible)
  900. (gnus-topic-goto-topic (gnus-topic-next-topic (car list))))
  901. (setq after nil)))
  902. t))))
  903. ;;; Topic-active functions
  904. (defun gnus-topic-grok-active (&optional force)
  905. "Parse all active groups and create topic structures for them."
  906. ;; First we make sure that we have really read the active file.
  907. (when (or force
  908. (not gnus-topic-active-alist))
  909. (let (groups)
  910. ;; Get a list of all groups available.
  911. (mapatoms (lambda (g) (when (symbol-value g)
  912. (push (symbol-name g) groups)))
  913. gnus-active-hashtb)
  914. (setq groups (sort groups 'string<))
  915. ;; Init the variables.
  916. (setq gnus-topic-active-topology (list (list "" 'visible)))
  917. (setq gnus-topic-active-alist nil)
  918. ;; Descend the top-level hierarchy.
  919. (gnus-topic-grok-active-1 gnus-topic-active-topology groups)
  920. ;; Set the top-level topic names to something nice.
  921. (setcar (car gnus-topic-active-topology) "Gnus active")
  922. (setcar (car gnus-topic-active-alist) "Gnus active"))))
  923. (defun gnus-topic-grok-active-1 (topology groups)
  924. (let* ((name (caar topology))
  925. (prefix (concat "^" (regexp-quote name)))
  926. tgroups ntopology group)
  927. (while (and groups
  928. (string-match prefix (setq group (car groups))))
  929. (if (not (string-match "\\." group (match-end 0)))
  930. ;; There are no further hierarchies here, so we just
  931. ;; enter this group into the list belonging to this
  932. ;; topic.
  933. (push (pop groups) tgroups)
  934. ;; New sub-hierarchy, so we add it to the topology.
  935. (nconc topology (list (setq ntopology
  936. (list (list (substring
  937. group 0 (match-end 0))
  938. 'invisible)))))
  939. ;; Descend the hierarchy.
  940. (setq groups (gnus-topic-grok-active-1 ntopology groups))))
  941. ;; We remove the trailing "." from the topic name.
  942. (setq name
  943. (if (string-match "\\.$" name)
  944. (substring name 0 (match-beginning 0))
  945. name))
  946. ;; Add this topic and its groups to the topic alist.
  947. (push (cons name (nreverse tgroups)) gnus-topic-active-alist)
  948. (setcar (car topology) name)
  949. ;; We return the rest of the groups that didn't belong
  950. ;; to this topic.
  951. groups))
  952. ;;; Topic mode, commands and keymap.
  953. (defvar gnus-topic-mode-map nil)
  954. (defvar gnus-group-topic-map nil)
  955. (unless gnus-topic-mode-map
  956. (setq gnus-topic-mode-map (make-sparse-keymap))
  957. ;; Override certain group mode keys.
  958. (gnus-define-keys gnus-topic-mode-map
  959. "=" gnus-topic-select-group
  960. "\r" gnus-topic-select-group
  961. " " gnus-topic-read-group
  962. "\C-c\C-x" gnus-topic-expire-articles
  963. "c" gnus-topic-catchup-articles
  964. "\C-k" gnus-topic-kill-group
  965. "\C-y" gnus-topic-yank-group
  966. "\M-g" gnus-topic-get-new-news-this-topic
  967. "AT" gnus-topic-list-active
  968. "Gp" gnus-topic-edit-parameters
  969. "#" gnus-topic-mark-topic
  970. "\M-#" gnus-topic-unmark-topic
  971. [tab] gnus-topic-indent
  972. [(meta tab)] gnus-topic-unindent
  973. "\C-i" gnus-topic-indent
  974. "\M-\C-i" gnus-topic-unindent
  975. gnus-mouse-2 gnus-mouse-pick-topic)
  976. ;; Define a new submap.
  977. (gnus-define-keys (gnus-group-topic-map "T" gnus-group-mode-map)
  978. "#" gnus-topic-mark-topic
  979. "\M-#" gnus-topic-unmark-topic
  980. "n" gnus-topic-create-topic
  981. "m" gnus-topic-move-group
  982. "D" gnus-topic-remove-group
  983. "c" gnus-topic-copy-group
  984. "h" gnus-topic-hide-topic
  985. "s" gnus-topic-show-topic
  986. "j" gnus-topic-jump-to-topic
  987. "M" gnus-topic-move-matching
  988. "C" gnus-topic-copy-matching
  989. "\M-p" gnus-topic-goto-previous-topic
  990. "\M-n" gnus-topic-goto-next-topic
  991. "\C-i" gnus-topic-indent
  992. [tab] gnus-topic-indent
  993. "r" gnus-topic-rename
  994. "\177" gnus-topic-delete
  995. [delete] gnus-topic-delete
  996. "H" gnus-topic-toggle-display-empty-topics)
  997. (gnus-define-keys (gnus-topic-sort-map "S" gnus-group-topic-map)
  998. "s" gnus-topic-sort-groups
  999. "a" gnus-topic-sort-groups-by-alphabet
  1000. "u" gnus-topic-sort-groups-by-unread
  1001. "l" gnus-topic-sort-groups-by-level
  1002. "e" gnus-topic-sort-groups-by-server
  1003. "v" gnus-topic-sort-groups-by-score
  1004. "r" gnus-topic-sort-groups-by-rank
  1005. "m" gnus-topic-sort-groups-by-method))
  1006. (defun gnus-topic-make-menu-bar ()
  1007. (unless (boundp 'gnus-topic-menu)
  1008. (easy-menu-define
  1009. gnus-topic-menu gnus-topic-mode-map ""
  1010. '("Topics"
  1011. ["Toggle topics" gnus-topic-mode t]
  1012. ("Groups"
  1013. ["Copy..." gnus-topic-copy-group t]
  1014. ["Move..." gnus-topic-move-group t]
  1015. ["Remove" gnus-topic-remove-group t]
  1016. ["Copy matching..." gnus-topic-copy-matching t]
  1017. ["Move matching..." gnus-topic-move-matching t])
  1018. ("Topics"
  1019. ["Goto..." gnus-topic-jump-to-topic t]
  1020. ["Show" gnus-topic-show-topic t]
  1021. ["Hide" gnus-topic-hide-topic t]
  1022. ["Delete" gnus-topic-delete t]
  1023. ["Rename..." gnus-topic-rename t]
  1024. ["Create..." gnus-topic-create-topic t]
  1025. ["Mark" gnus-topic-mark-topic t]
  1026. ["Indent" gnus-topic-indent t]
  1027. ["Sort" gnus-topic-sort-topics t]
  1028. ["Previous topic" gnus-topic-goto-previous-topic t]
  1029. ["Next topic" gnus-topic-goto-next-topic t]
  1030. ["Toggle hide empty" gnus-topic-toggle-display-empty-topics t]
  1031. ["Edit parameters" gnus-topic-edit-parameters t])
  1032. ["List active" gnus-topic-list-active t]))))
  1033. (define-minor-mode gnus-topic-mode
  1034. "Minor mode for topicsifying Gnus group buffers."
  1035. :lighter " Topic" :keymap gnus-topic-mode-map
  1036. (if (not (derived-mode-p 'gnus-group-mode))
  1037. (setq gnus-topic-mode nil)
  1038. ;; Infest Gnus with topics.
  1039. (if (not gnus-topic-mode)
  1040. (setq gnus-goto-missing-group-function nil)
  1041. (when (gnus-visual-p 'topic-menu 'menu)
  1042. (gnus-topic-make-menu-bar))
  1043. (gnus-set-format 'topic t)
  1044. (add-hook 'gnus-group-catchup-group-hook 'gnus-topic-update-topic)
  1045. (set (make-local-variable 'gnus-group-prepare-function)
  1046. 'gnus-group-prepare-topics)
  1047. (set (make-local-variable 'gnus-group-get-parameter-function)
  1048. 'gnus-group-topic-parameters)
  1049. (set (make-local-variable 'gnus-group-goto-next-group-function)
  1050. 'gnus-topic-goto-next-group)
  1051. (set (make-local-variable 'gnus-group-indentation-function)
  1052. 'gnus-topic-group-indentation)
  1053. (set (make-local-variable 'gnus-group-update-group-function)
  1054. 'gnus-topic-update-topics-containing-group)
  1055. (set (make-local-variable 'gnus-group-sort-alist-function)
  1056. 'gnus-group-sort-topic)
  1057. (setq gnus-group-change-level-function 'gnus-topic-change-level)
  1058. (setq gnus-goto-missing-group-function 'gnus-topic-goto-missing-group)
  1059. (gnus-make-local-hook 'gnus-check-bogus-groups-hook)
  1060. (add-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist
  1061. nil 'local)
  1062. (setq gnus-topology-checked-p nil)
  1063. ;; We check the topology.
  1064. (when gnus-newsrc-alist
  1065. (gnus-topic-check-topology)))
  1066. ;; Remove topic infestation.
  1067. (unless gnus-topic-mode
  1068. (remove-hook 'gnus-summary-exit-hook 'gnus-topic-update-topic)
  1069. (setq gnus-group-change-level-function nil)
  1070. (remove-hook 'gnus-check-bogus-groups-hook 'gnus-topic-clean-alist)
  1071. (setq gnus-group-prepare-function 'gnus-group-prepare-flat)
  1072. (setq gnus-group-sort-alist-function 'gnus-group-sort-flat))
  1073. (when (gmm-called-interactively-p 'any)
  1074. (gnus-group-list-groups))))
  1075. (defun gnus-topic-select-group (&optional all)
  1076. "Select this newsgroup.
  1077. No article is selected automatically.
  1078. If the group is opened, just switch the summary buffer.
  1079. If ALL is non-nil, already read articles become readable.
  1080. If ALL is a positive number, fetch this number of the latest
  1081. articles in the group. If ALL is a negative number, fetch this
  1082. number of the earliest articles in the group.
  1083. If performed over a topic line, toggle folding the topic."
  1084. (interactive "P")
  1085. (when (and (eobp) (not (gnus-group-group-name)))
  1086. (forward-line -1))
  1087. (if (gnus-group-topic-p)
  1088. (let ((gnus-group-list-mode
  1089. (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
  1090. (gnus-topic-fold all)
  1091. (gnus-dribble-touch))
  1092. (gnus-group-select-group all)))
  1093. (defun gnus-mouse-pick-topic (e)
  1094. "Select the group or topic under the mouse pointer."
  1095. (interactive "e")
  1096. (mouse-set-point e)
  1097. (gnus-topic-read-group nil))
  1098. (defun gnus-topic-expire-articles (topic)
  1099. "Expire articles in this topic or group."
  1100. (interactive (list (gnus-group-topic-name)))
  1101. (if (not topic)
  1102. (call-interactively 'gnus-group-expire-articles)
  1103. (save-excursion
  1104. (gnus-message 5 "Expiring groups in %s..." topic)
  1105. (let ((gnus-group-marked
  1106. (mapcar (lambda (entry) (car (nth 2 entry)))
  1107. (gnus-topic-find-groups topic gnus-level-killed t
  1108. nil t))))
  1109. (gnus-group-expire-articles nil))
  1110. (gnus-message 5 "Expiring groups in %s...done" topic))))
  1111. (defun gnus-topic-catchup-articles (topic)
  1112. "Catchup this topic or group.
  1113. Also see `gnus-group-catchup'."
  1114. (interactive (list (gnus-group-topic-name)))
  1115. (if (not topic)
  1116. (call-interactively 'gnus-group-catchup-current)
  1117. (save-excursion
  1118. (let* ((groups
  1119. (mapcar (lambda (entry) (car (nth 2 entry)))
  1120. (gnus-topic-find-groups topic gnus-level-killed t
  1121. nil t)))
  1122. (inhibit-read-only t)
  1123. (gnus-group-marked groups))
  1124. (gnus-group-catchup-current)
  1125. (mapcar 'gnus-topic-update-topics-containing-group groups)))))
  1126. (defun gnus-topic-read-group (&optional all no-article group)
  1127. "Read news in this newsgroup.
  1128. If the prefix argument ALL is non-nil, already read articles become
  1129. readable.
  1130. If ALL is a positive number, fetch this number of the latest
  1131. articles in the group. If ALL is a negative number, fetch this
  1132. number of the earliest articles in the group.
  1133. If the optional argument NO-ARTICLE is non-nil, no article will
  1134. be auto-selected upon group entry. If GROUP is non-nil, fetch
  1135. that group.
  1136. If performed over a topic line, toggle folding the topic."
  1137. (interactive "P")
  1138. (when (and (eobp) (not (gnus-group-group-name)))
  1139. (forward-line -1))
  1140. (if (gnus-group-topic-p)
  1141. (let ((gnus-group-list-mode
  1142. (if all (cons (if (numberp all) all 7) t) gnus-group-list-mode)))
  1143. (gnus-topic-fold all))
  1144. (gnus-group-read-group all no-article group)))
  1145. (defun gnus-topic-create-topic (topic parent &optional previous full-topic)
  1146. "Create a new TOPIC under PARENT.
  1147. When used interactively, PARENT will be the topic under point."
  1148. (interactive
  1149. (list
  1150. (read-string "New topic: ")
  1151. (gnus-current-topic)))
  1152. ;; Check whether this topic already exists.
  1153. (when (gnus-topic-find-topology topic)
  1154. (error "Topic already exists"))
  1155. (unless parent
  1156. (setq parent (caar gnus-topic-topology)))
  1157. (let ((top (cdr (gnus-topic-find-topology parent)))
  1158. (full-topic (or full-topic (list (list topic 'visible nil nil)))))
  1159. (unless top
  1160. (error "No such parent topic: %s" parent))
  1161. (if previous
  1162. (progn
  1163. (while (and (cdr top)
  1164. (not (equal (caaadr top) previous)))
  1165. (setq top (cdr top)))
  1166. (setcdr top (cons full-topic (cdr top))))
  1167. (nconc top (list full-topic)))
  1168. (unless (assoc topic gnus-topic-alist)
  1169. (push (list topic) gnus-topic-alist)))
  1170. (gnus-topic-enter-dribble)
  1171. (gnus-group-list-groups)
  1172. (gnus-topic-goto-topic topic))
  1173. ;; FIXME:
  1174. ;; 1. When the marked groups are overlapped with the process
  1175. ;; region, the behavior of move or remove is not right.
  1176. ;; 2. Can't process on several marked groups with a same name,
  1177. ;; because gnus-group-marked only keeps one copy.
  1178. (defvar gnus-topic-history nil)
  1179. (defun gnus-topic-move-group (n topic &optional copyp)
  1180. "Move the next N groups to TOPIC.
  1181. If COPYP, copy the groups instead."
  1182. (interactive
  1183. (list current-prefix-arg
  1184. (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t
  1185. nil 'gnus-topic-history)))
  1186. (let ((use-marked (and (not n) (not (gnus-region-active-p))
  1187. gnus-group-marked t))
  1188. (groups (gnus-group-process-prefix n))
  1189. (topicl (assoc topic gnus-topic-alist))
  1190. (start-topic (gnus-group-topic-name))
  1191. (start-group (progn (forward-line 1) (gnus-group-group-name)))
  1192. entry)
  1193. (if (and (not groups) (not copyp) start-topic)
  1194. (gnus-topic-move start-topic topic)
  1195. (dolist (g groups)
  1196. (gnus-group-remove-mark g use-marked)
  1197. (when (and
  1198. (setq entry (assoc (gnus-current-topic) gnus-topic-alist))
  1199. (not copyp))
  1200. (setcdr entry (gnus-delete-first g (cdr entry))))
  1201. (nconc topicl (list g)))
  1202. (gnus-topic-enter-dribble)
  1203. (if start-group
  1204. (gnus-group-goto-group start-group)
  1205. (gnus-topic-goto-topic start-topic))
  1206. (gnus-group-list-groups))))
  1207. (defun gnus-topic-remove-group (&optional n)
  1208. "Remove the current group from the topic."
  1209. (interactive "P")
  1210. (let ((use-marked (and (not n) (not (gnus-region-active-p))
  1211. gnus-group-marked t))
  1212. (groups (gnus-group-process-prefix n)))
  1213. (mapc
  1214. (lambda (group)
  1215. (gnus-group-remove-mark group use-marked)
  1216. (let ((topicl (assoc (gnus-current-topic) gnus-topic-alist))
  1217. (inhibit-read-only t))
  1218. (when (and topicl group)
  1219. (gnus-delete-line)
  1220. (gnus-delete-first group topicl))
  1221. (gnus-topic-update-topic)))
  1222. groups)
  1223. (gnus-topic-enter-dribble)
  1224. (gnus-group-position-point)))
  1225. (defun gnus-topic-copy-group (n topic)
  1226. "Copy the current group to a topic."
  1227. (interactive
  1228. (list current-prefix-arg
  1229. (gnus-completing-read
  1230. "Copy to topic" (mapcar 'car gnus-topic-alist) t)))
  1231. (gnus-topic-move-group n topic t))
  1232. (defun gnus-topic-kill-group (&optional n discard)
  1233. "Kill the next N groups."
  1234. (interactive "P")
  1235. (if (gnus-group-topic-p)
  1236. (let ((topic (gnus-group-topic-name)))
  1237. (push (cons
  1238. (gnus-topic-find-topology topic)
  1239. (assoc topic gnus-topic-alist))
  1240. gnus-topic-killed-topics)
  1241. (gnus-topic-remove-topic nil t)
  1242. (gnus-topic-find-topology topic nil nil gnus-topic-topology)
  1243. (gnus-topic-enter-dribble))
  1244. (gnus-group-kill-group n discard)
  1245. (if (not (gnus-group-topic-p))
  1246. (gnus-topic-update-topic)
  1247. ;; Move up one line so that we update the right topic.
  1248. (forward-line -1)
  1249. (gnus-topic-update-topic)
  1250. (forward-line 1))))
  1251. (defun gnus-topic-yank-group (&optional arg)
  1252. "Yank the last topic."
  1253. (interactive "p")
  1254. (if gnus-topic-killed-topics
  1255. (let* ((previous
  1256. (or (gnus-group-topic-name)
  1257. (gnus-topic-next-topic (gnus-current-topic))))
  1258. (data (pop gnus-topic-killed-topics))
  1259. (alist (cdr data))
  1260. (item (cdar data)))
  1261. (push alist gnus-topic-alist)
  1262. (gnus-topic-create-topic
  1263. (caar item) (gnus-topic-parent-topic previous) previous
  1264. item)
  1265. (gnus-topic-enter-dribble)
  1266. (gnus-topic-goto-topic (caar item)))
  1267. (let* ((prev (gnus-group-group-name))
  1268. (gnus-topic-inhibit-change-level t)
  1269. (gnus-group-indentation
  1270. (make-string
  1271. (* gnus-topic-indent-level
  1272. (or (save-excursion
  1273. (gnus-topic-goto-topic (gnus-current-topic))
  1274. (gnus-group-topic-level))
  1275. 0))
  1276. ? ))
  1277. yanked alist)
  1278. ;; We first yank the groups the normal way...
  1279. (setq yanked (gnus-group-yank-group arg))
  1280. ;; Then we enter the yanked groups into the topics they belong
  1281. ;; to.
  1282. (setq alist (assoc (save-excursion
  1283. (forward-line -1)
  1284. (gnus-current-topic))
  1285. gnus-topic-alist))
  1286. (when (stringp yanked)
  1287. (setq yanked (list yanked)))
  1288. (if (not prev)
  1289. (nconc alist yanked)
  1290. (if (not (cdr alist))
  1291. (setcdr alist (nconc yanked (cdr alist)))
  1292. (while (cdr alist)
  1293. (when (equal (cadr alist) prev)
  1294. (setcdr alist (nconc yanked (cdr alist)))
  1295. (setq alist nil))
  1296. (setq alist (cdr alist))))))
  1297. (gnus-topic-update-topic)))
  1298. (defun gnus-topic-hide-topic (&optional permanent)
  1299. "Hide the current topic.
  1300. If PERMANENT, make it stay hidden in subsequent sessions as well."
  1301. (interactive "P")
  1302. (when (gnus-current-topic)
  1303. (gnus-topic-goto-topic (gnus-current-topic))
  1304. (if permanent
  1305. (setcar (cddr
  1306. (cadr
  1307. (gnus-topic-find-topology (gnus-current-topic))))
  1308. 'hidden))
  1309. (gnus-topic-remove-topic nil nil)))
  1310. (defun gnus-topic-show-topic (&optional permanent)
  1311. "Show the hidden topic.
  1312. If PERMANENT, make it stay shown in subsequent sessions as well."
  1313. (interactive "P")
  1314. (when (gnus-group-topic-p)
  1315. (if (not permanent)
  1316. (gnus-topic-remove-topic t nil)
  1317. (let ((topic
  1318. (gnus-topic-find-topology
  1319. (gnus-completing-read "Show topic"
  1320. (mapcar 'car gnus-topic-alist) t))))
  1321. (setcar (cddr (cadr topic)) nil)
  1322. (setcar (cdr (cadr topic)) 'visible)
  1323. (gnus-group-list-groups)))))
  1324. (defun gnus-topic-mark-topic (topic &optional unmark non-recursive)
  1325. "Mark all groups in the TOPIC with the process mark.
  1326. If NON-RECURSIVE (which is the prefix) is t, don't mark its subtopics."
  1327. (interactive (list (gnus-group-topic-name)
  1328. nil
  1329. (and current-prefix-arg t)))
  1330. (if (not topic)
  1331. (call-interactively 'gnus-group-mark-group)
  1332. (save-excursion
  1333. (let ((groups (gnus-topic-find-groups topic gnus-level-killed t nil
  1334. (not non-recursive))))
  1335. (while groups
  1336. (funcall (if unmark 'gnus-group-remove-mark 'gnus-group-set-mark)
  1337. (gnus-info-group (nth 2 (pop groups)))))))))
  1338. (defun gnus-topic-unmark-topic (topic &optional dummy non-recursive)
  1339. "Remove the process mark from all groups in the TOPIC.
  1340. If NON-RECURSIVE (which is the prefix) is t, don't unmark its subtopics."
  1341. (interactive (list (gnus-group-topic-name)
  1342. nil
  1343. (and current-prefix-arg t)))
  1344. (if (not topic)
  1345. (call-interactively 'gnus-group-unmark-group)
  1346. (gnus-topic-mark-topic topic t non-recursive)))
  1347. (defun gnus-topic-get-new-news-this-topic (&optional n)
  1348. "Check for new news in the current topic."
  1349. (interactive "P")
  1350. (if (not (gnus-group-topic-p))
  1351. (gnus-group-get-new-news-this-group n)
  1352. (let* ((topic (gnus-group-topic-name))
  1353. (data (cadr (gnus-topic-find-topology topic))))
  1354. (save-excursion
  1355. (gnus-topic-mark-topic topic nil (and n t))
  1356. (gnus-group-get-new-news-this-group))
  1357. (gnus-topic-remove-topic (eq 'visible (cadr data))))))
  1358. (defun gnus-topic-move-matching (regexp topic &optional copyp)
  1359. "Move all groups that match REGEXP to some topic."
  1360. (interactive
  1361. (let (topic)
  1362. (nreverse
  1363. (list
  1364. (setq topic (gnus-completing-read "Move to topic"
  1365. (mapcar 'car gnus-topic-alist) t))
  1366. (read-string (format "Move to %s (regexp): " topic))))))
  1367. (gnus-group-mark-regexp regexp)
  1368. (gnus-topic-move-group nil topic copyp))
  1369. (defun gnus-topic-copy-matching (regexp topic &optional copyp)
  1370. "Copy all groups that match REGEXP to some topic."
  1371. (interactive
  1372. (let (topic)
  1373. (nreverse
  1374. (list
  1375. (setq topic (gnus-completing-read "Copy to topic"
  1376. (mapcar 'car gnus-topic-alist) t))
  1377. (read-string (format "Copy to %s (regexp): " topic))))))
  1378. (gnus-topic-move-matching regexp topic t))
  1379. (defun gnus-topic-delete (topic)
  1380. "Delete a topic."
  1381. (interactive (list (gnus-group-topic-name)))
  1382. (unless topic
  1383. (error "No topic to be deleted"))
  1384. (let ((entry (assoc topic gnus-topic-alist))
  1385. (inhibit-read-only t))
  1386. (when (cdr entry)
  1387. (error "Topic not empty"))
  1388. ;; Delete if visible.
  1389. (when (gnus-topic-goto-topic topic)
  1390. (gnus-delete-line))
  1391. ;; Remove from alist.
  1392. (setq gnus-topic-alist (delq entry gnus-topic-alist))
  1393. ;; Remove from topology.
  1394. (gnus-topic-find-topology topic nil nil 'delete)
  1395. (gnus-dribble-touch)))
  1396. (defun gnus-topic-rename (old-name new-name)
  1397. "Rename a topic."
  1398. (interactive
  1399. (let ((topic (gnus-current-topic)))
  1400. (list topic
  1401. (read-string (format "Rename %s to: " topic) topic))))
  1402. ;; Check whether the new name exists.
  1403. (when (gnus-topic-find-topology new-name)
  1404. (error "Topic `%s' already exists" new-name))
  1405. ;; "nil" is an invalid name, for reasons I'd rather not go
  1406. ;; into here. Trust me.
  1407. (when (equal new-name "nil")
  1408. (error "Invalid name: %s" nil))
  1409. ;; Do the renaming.
  1410. (let ((top (gnus-topic-find-topology old-name))
  1411. (entry (assoc old-name gnus-topic-alist)))
  1412. (when top
  1413. (setcar (cadr top) new-name))
  1414. (when entry
  1415. (setcar entry new-name))
  1416. (forward-line -1)
  1417. (gnus-dribble-touch)
  1418. (gnus-group-list-groups)
  1419. (forward-line 1)))
  1420. (defun gnus-topic-indent (&optional unindent)
  1421. "Indent a topic -- make it a sub-topic of the previous topic.
  1422. If UNINDENT, remove an indentation."
  1423. (interactive "P")
  1424. (if unindent
  1425. (gnus-topic-unindent)
  1426. (let* ((topic (gnus-current-topic))
  1427. (parent (gnus-topic-previous-topic topic))
  1428. (inhibit-read-only t))
  1429. (unless parent
  1430. (error "Nothing to indent %s into" topic))
  1431. (when topic
  1432. (gnus-topic-goto-topic topic)
  1433. (gnus-topic-kill-group)
  1434. (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
  1435. (gnus-topic-create-topic
  1436. topic parent nil (cdar (car gnus-topic-killed-topics)))
  1437. (pop gnus-topic-killed-topics)
  1438. (or (gnus-topic-goto-topic topic)
  1439. (gnus-topic-goto-topic parent))))))
  1440. (defun gnus-topic-unindent ()
  1441. "Unindent a topic."
  1442. (interactive)
  1443. (let* ((topic (gnus-current-topic))
  1444. (parent (gnus-topic-parent-topic topic))
  1445. (grandparent (gnus-topic-parent-topic parent)))
  1446. (unless grandparent
  1447. (error "Nothing to indent %s into" topic))
  1448. (when topic
  1449. (gnus-topic-goto-topic topic)
  1450. (gnus-topic-kill-group)
  1451. (push (cdar gnus-topic-killed-topics) gnus-topic-alist)
  1452. (gnus-topic-create-topic
  1453. topic grandparent (gnus-topic-next-topic parent)
  1454. (cdar (car gnus-topic-killed-topics)))
  1455. (pop gnus-topic-killed-topics)
  1456. (gnus-topic-goto-topic topic))))
  1457. (defun gnus-topic-list-active (&optional force)
  1458. "List all groups that Gnus knows about in a topicsified fashion.
  1459. If FORCE, always re-read the active file."
  1460. (interactive "P")
  1461. (when force
  1462. (gnus-get-killed-groups))
  1463. (gnus-topic-grok-active force)
  1464. (let ((gnus-topic-topology gnus-topic-active-topology)
  1465. (gnus-topic-alist gnus-topic-active-alist)
  1466. gnus-killed-list gnus-zombie-list)
  1467. (gnus-group-list-groups gnus-level-killed nil 1)))
  1468. (defun gnus-topic-toggle-display-empty-topics ()
  1469. "Show/hide topics that have no unread articles."
  1470. (interactive)
  1471. (setq gnus-topic-display-empty-topics
  1472. (not gnus-topic-display-empty-topics))
  1473. (gnus-group-list-groups)
  1474. (message "%s empty topics"
  1475. (if gnus-topic-display-empty-topics
  1476. "Showing" "Hiding")))
  1477. ;;; Topic sorting functions
  1478. (defun gnus-topic-edit-parameters (group)
  1479. "Edit the group parameters of GROUP.
  1480. If performed on a topic, edit the topic parameters instead."
  1481. (interactive (list (gnus-group-group-name)))
  1482. (if group
  1483. (gnus-group-edit-group-parameters group)
  1484. (if (not (gnus-group-topic-p))
  1485. (error "Nothing to edit on the current line")
  1486. (let ((topic (gnus-group-topic-name)))
  1487. (gnus-edit-form
  1488. (gnus-topic-parameters topic)
  1489. (format "Editing the topic parameters for `%s'."
  1490. (or group topic))
  1491. `(lambda (form)
  1492. (gnus-topic-set-parameters ,topic form)))))))
  1493. (defun gnus-group-sort-topic (func reverse)
  1494. "Sort groups in the topics according to FUNC and REVERSE."
  1495. (let ((alist gnus-topic-alist))
  1496. (while alist
  1497. ;; !!!Sometimes nil elements sneak into the alist,
  1498. ;; for some reason or other.
  1499. (setcar alist (delq nil (car alist)))
  1500. (setcar alist (delete "dummy.group" (car alist)))
  1501. (gnus-topic-sort-topic (pop alist) func reverse))))
  1502. (defun gnus-topic-sort-topic (topic func reverse)
  1503. ;; Each topic only lists the name of the group, while
  1504. ;; the sort predicates expect group infos as inputs.
  1505. ;; So we first transform the group names into infos,
  1506. ;; then sort, and then transform back into group names.
  1507. (setcdr
  1508. topic
  1509. (mapcar
  1510. (lambda (info) (gnus-info-group info))
  1511. (sort
  1512. (mapcar
  1513. (lambda (group) (gnus-get-info group))
  1514. (cdr topic))
  1515. func)))
  1516. ;; Do the reversal, if necessary.
  1517. (when reverse
  1518. (setcdr topic (nreverse (cdr topic)))))
  1519. (defun gnus-topic-sort-groups (func &optional reverse)
  1520. "Sort the current topic according to FUNC.
  1521. If REVERSE, reverse the sorting order."
  1522. (interactive (list gnus-group-sort-function current-prefix-arg))
  1523. (let ((topic (assoc (gnus-current-topic) gnus-topic-alist)))
  1524. (gnus-topic-sort-topic
  1525. topic (gnus-make-sort-function func) reverse)
  1526. (gnus-group-list-groups)))
  1527. (defun gnus-topic-sort-groups-by-alphabet (&optional reverse)
  1528. "Sort the current topic alphabetically by group name.
  1529. If REVERSE, sort in reverse order."
  1530. (interactive "P")
  1531. (gnus-topic-sort-groups 'gnus-group-sort-by-alphabet reverse))
  1532. (defun gnus-topic-sort-groups-by-unread (&optional reverse)
  1533. "Sort the current topic by number of unread articles.
  1534. If REVERSE, sort in reverse order."
  1535. (interactive "P")
  1536. (gnus-topic-sort-groups 'gnus-group-sort-by-unread reverse))
  1537. (defun gnus-topic-sort-groups-by-level (&optional reverse)
  1538. "Sort the current topic by group level.
  1539. If REVERSE, sort in reverse order."
  1540. (interactive "P")
  1541. (gnus-topic-sort-groups 'gnus-group-sort-by-level reverse))
  1542. (defun gnus-topic-sort-groups-by-score (&optional reverse)
  1543. "Sort the current topic by group score.
  1544. If REVERSE, sort in reverse order."
  1545. (interactive "P")
  1546. (gnus-topic-sort-groups 'gnus-group-sort-by-score reverse))
  1547. (defun gnus-topic-sort-groups-by-rank (&optional reverse)
  1548. "Sort the current topic by group rank.
  1549. If REVERSE, sort in reverse order."
  1550. (interactive "P")
  1551. (gnus-topic-sort-groups 'gnus-group-sort-by-rank reverse))
  1552. (defun gnus-topic-sort-groups-by-method (&optional reverse)
  1553. "Sort the current topic alphabetically by backend name.
  1554. If REVERSE, sort in reverse order."
  1555. (interactive "P")
  1556. (gnus-topic-sort-groups 'gnus-group-sort-by-method reverse))
  1557. (defun gnus-topic-sort-groups-by-server (&optional reverse)
  1558. "Sort the current topic alphabetically by server name.
  1559. If REVERSE, sort in reverse order."
  1560. (interactive "P")
  1561. (gnus-topic-sort-groups 'gnus-group-sort-by-server reverse))
  1562. (defun gnus-topic-sort-topics-1 (top reverse)
  1563. (if (cdr top)
  1564. (let ((subtop
  1565. (mapcar (gnus-byte-compile
  1566. `(lambda (top)
  1567. (gnus-topic-sort-topics-1 top ,reverse)))
  1568. (sort (cdr top)
  1569. (lambda (t1 t2)
  1570. (string-lessp (caar t1) (caar t2)))))))
  1571. (setcdr top (if reverse (reverse subtop) subtop))))
  1572. top)
  1573. (defun gnus-topic-sort-topics (&optional topic reverse)
  1574. "Sort topics in TOPIC alphabetically by topic name.
  1575. If REVERSE, reverse the sorting order."
  1576. (interactive
  1577. (list (gnus-completing-read "Sort topics in"
  1578. (mapcar 'car gnus-topic-alist) t
  1579. (gnus-current-topic))
  1580. current-prefix-arg))
  1581. (let ((topic-topology (or (and topic (cdr (gnus-topic-find-topology topic)))
  1582. gnus-topic-topology)))
  1583. (gnus-topic-sort-topics-1 topic-topology reverse)
  1584. (gnus-topic-enter-dribble)
  1585. (gnus-group-list-groups)
  1586. (gnus-topic-goto-topic topic)))
  1587. (defun gnus-topic-move (current to)
  1588. "Move the CURRENT topic to TO."
  1589. (interactive
  1590. (list
  1591. (gnus-group-topic-name)
  1592. (gnus-completing-read "Move to topic" (mapcar 'car gnus-topic-alist) t)))
  1593. (unless (and current to)
  1594. (error "Can't find topic"))
  1595. (let ((current-top (cdr (gnus-topic-find-topology current)))
  1596. (to-top (cdr (gnus-topic-find-topology to))))
  1597. (unless current-top
  1598. (error "Can't find topic `%s'" current))
  1599. (unless to-top
  1600. (error "Can't find topic `%s'" to))
  1601. (if (gnus-topic-find-topology to current-top 0);; Don't care the level
  1602. (error "Can't move `%s' to its sub-level" current))
  1603. (gnus-topic-find-topology current nil nil 'delete)
  1604. (setcdr (last to-top) (list current-top))
  1605. (gnus-topic-enter-dribble)
  1606. (gnus-group-list-groups)
  1607. (gnus-topic-goto-topic current)))
  1608. (defun gnus-subscribe-topics (newsgroup)
  1609. (catch 'end
  1610. (let (match gnus-group-change-level-function)
  1611. (dolist (topic (gnus-topic-list))
  1612. (when (and (setq match (cdr (assq 'subscribe
  1613. (gnus-topic-parameters topic))))
  1614. (string-match match newsgroup))
  1615. ;; Just subscribe the group.
  1616. (gnus-subscribe-alphabetically newsgroup)
  1617. ;; Add the group to the topic.
  1618. (nconc (assoc topic gnus-topic-alist) (list newsgroup))
  1619. ;; if this topic specifies a default level, use it
  1620. (let ((subscribe-level (cdr (assq 'subscribe-level
  1621. (gnus-topic-parameters topic)))))
  1622. (when subscribe-level
  1623. (gnus-group-change-level newsgroup subscribe-level
  1624. gnus-level-default-subscribed)))
  1625. (throw 'end t)))
  1626. nil)))
  1627. (provide 'gnus-topic)
  1628. ;;; gnus-topic.el ends here