gnus-kill.el 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. ;;; gnus-kill.el --- kill commands for Gnus
  2. ;; Copyright (C) 1995-2015 Free Software Foundation, Inc.
  3. ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  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-art)
  22. (require 'gnus-range)
  23. (defcustom gnus-kill-file-mode-hook nil
  24. "Hook for Gnus kill file mode."
  25. :group 'gnus-score-kill
  26. :type 'hook)
  27. (defcustom gnus-kill-expiry-days 7
  28. "*Number of days before expiring unused kill file entries."
  29. :group 'gnus-score-kill
  30. :group 'gnus-score-expire
  31. :type 'integer)
  32. (defcustom gnus-kill-save-kill-file nil
  33. "*If non-nil, will save kill files after processing them."
  34. :group 'gnus-score-kill
  35. :type 'boolean)
  36. (defcustom gnus-winconf-kill-file nil
  37. "What does this do, Lars?
  38. I don't know, Per."
  39. :group 'gnus-score-kill
  40. :type 'sexp)
  41. (defcustom gnus-kill-killed t
  42. "*If non-nil, Gnus will apply kill files to already killed articles.
  43. If it is nil, Gnus will never apply kill files to articles that have
  44. already been through the scoring process, which might very well save lots
  45. of time."
  46. :group 'gnus-score-kill
  47. :type 'boolean)
  48. (defmacro gnus-raise (field expression level)
  49. `(gnus-kill ,field ,expression
  50. (function (gnus-summary-raise-score ,level)) t))
  51. (defmacro gnus-lower (field expression level)
  52. `(gnus-kill ,field ,expression
  53. (function (gnus-summary-raise-score (- ,level))) t))
  54. ;;;
  55. ;;; Gnus Kill File Mode
  56. ;;;
  57. (defvar gnus-kill-file-mode-map
  58. (let ((map (make-sparse-keymap)))
  59. (set-keymap-parent map emacs-lisp-mode-map)
  60. (gnus-define-keymap map
  61. "\C-c\C-k\C-s" gnus-kill-file-kill-by-subject
  62. "\C-c\C-k\C-a" gnus-kill-file-kill-by-author
  63. "\C-c\C-k\C-t" gnus-kill-file-kill-by-thread
  64. "\C-c\C-k\C-x" gnus-kill-file-kill-by-xref
  65. "\C-c\C-a" gnus-kill-file-apply-buffer
  66. "\C-c\C-e" gnus-kill-file-apply-last-sexp
  67. "\C-c\C-c" gnus-kill-file-exit)
  68. map))
  69. (define-derived-mode gnus-kill-file-mode emacs-lisp-mode "Kill"
  70. "Major mode for editing kill files.
  71. If you are using this mode - you probably shouldn't. Kill files
  72. perform badly and paint with a pretty broad brush. Score files, on
  73. the other hand, are vastly faster (40x speedup) and give you more
  74. control over what to do.
  75. In addition to Emacs-Lisp Mode, the following commands are available:
  76. \\{gnus-kill-file-mode-map}
  77. A kill file contains Lisp expressions to be applied to a selected
  78. newsgroup. The purpose is to mark articles as read on the basis of
  79. some set of regexps. A global kill file is applied to every newsgroup,
  80. and a local kill file is applied to a specified newsgroup. Since a
  81. global kill file is applied to every newsgroup, for better performance
  82. use a local one.
  83. A kill file can contain any kind of Emacs Lisp expressions expected
  84. to be evaluated in the Summary buffer. Writing Lisp programs for this
  85. purpose is not so easy because the internal working of Gnus must be
  86. well-known. For this reason, Gnus provides a general function which
  87. does this easily for non-Lisp programmers.
  88. The `gnus-kill' function executes commands available in Summary Mode
  89. by their key sequences. `gnus-kill' should be called with FIELD,
  90. REGEXP and optional COMMAND and ALL. FIELD is a string representing
  91. the header field or an empty string. If FIELD is an empty string, the
  92. entire article body is searched for. REGEXP is a string which is
  93. compared with FIELD value. COMMAND is a string representing a valid
  94. key sequence in Summary mode or Lisp expression. COMMAND defaults to
  95. '(gnus-summary-mark-as-read nil \"X\"). Make sure that COMMAND is
  96. executed in the Summary buffer. If the second optional argument ALL
  97. is non-nil, the COMMAND is applied to articles which are already
  98. marked as read or unread. Articles which are marked are skipped over
  99. by default.
  100. For example, if you want to mark articles of which subjects contain
  101. the string `AI' as read, a possible kill file may look like:
  102. (gnus-kill \"Subject\" \"AI\")
  103. If you want to mark articles with `D' instead of `X', you can use
  104. the following expression:
  105. (gnus-kill \"Subject\" \"AI\" \"d\")
  106. In this example it is assumed that the command
  107. `gnus-summary-mark-as-read-forward' is assigned to `d' in Summary Mode.
  108. It is possible to delete unnecessary headers which are marked with
  109. `X' in a kill file as follows:
  110. (gnus-expunge \"X\")
  111. If the Summary buffer is empty after applying kill files, Gnus will
  112. exit the selected newsgroup normally. If headers which are marked
  113. with `D' are deleted in a kill file, it is impossible to read articles
  114. which are marked as read in the previous Gnus sessions. Marks other
  115. than `D' should be used for articles which should really be deleted.
  116. Entry to this mode calls emacs-lisp-mode-hook and
  117. gnus-kill-file-mode-hook with no arguments, if that value is non-nil.")
  118. (defun gnus-kill-file-edit-file (newsgroup)
  119. "Begin editing a kill file for NEWSGROUP.
  120. If NEWSGROUP is nil, the global kill file is selected."
  121. (interactive "sNewsgroup: ")
  122. (let ((file (gnus-newsgroup-kill-file newsgroup)))
  123. (gnus-make-directory (file-name-directory file))
  124. ;; Save current window configuration if this is first invocation.
  125. (or (and (get-file-buffer file)
  126. (get-buffer-window (get-file-buffer file)))
  127. (setq gnus-winconf-kill-file (current-window-configuration)))
  128. ;; Hack windows.
  129. (let ((buffer (find-file-noselect file)))
  130. (cond ((get-buffer-window buffer)
  131. (pop-to-buffer buffer))
  132. ((derived-mode-p 'gnus-group-mode)
  133. (gnus-configure-windows 'group) ;Take all windows.
  134. (pop-to-buffer buffer))
  135. ((derived-mode-p 'gnus-summary-mode)
  136. (gnus-configure-windows 'article)
  137. (pop-to-buffer gnus-article-buffer)
  138. (bury-buffer gnus-article-buffer)
  139. (switch-to-buffer buffer))
  140. (t ;No good rules.
  141. (find-file-other-window file))))
  142. (gnus-kill-file-mode)))
  143. ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
  144. (defun gnus-kill-set-kill-buffer ()
  145. (let* ((file (gnus-newsgroup-kill-file gnus-newsgroup-name))
  146. (buffer (find-file-noselect file)))
  147. (set-buffer buffer)
  148. (gnus-kill-file-mode)
  149. (bury-buffer buffer)))
  150. (defun gnus-kill-file-enter-kill (field regexp &optional dont-move)
  151. ;; Enter kill file entry.
  152. ;; FIELD: String containing the name of the header field to kill.
  153. ;; REGEXP: The string to kill.
  154. (save-excursion
  155. (let (string)
  156. (unless (derived-mode-p 'gnus-kill-file-mode)
  157. (gnus-kill-set-kill-buffer))
  158. (unless dont-move
  159. (goto-char (point-max)))
  160. (insert (setq string (format "(gnus-kill %S %S)\n" field regexp)))
  161. (gnus-kill-file-apply-string string))))
  162. (defun gnus-kill-file-kill-by-subject ()
  163. "Kill by subject."
  164. (interactive)
  165. (gnus-kill-file-enter-kill
  166. "Subject"
  167. (if (vectorp gnus-current-headers)
  168. (regexp-quote
  169. (gnus-simplify-subject (mail-header-subject gnus-current-headers)))
  170. "")
  171. t))
  172. (defun gnus-kill-file-kill-by-author ()
  173. "Kill by author."
  174. (interactive)
  175. (gnus-kill-file-enter-kill
  176. "From"
  177. (if (vectorp gnus-current-headers)
  178. (regexp-quote (mail-header-from gnus-current-headers))
  179. "") t))
  180. (defun gnus-kill-file-kill-by-thread ()
  181. "Kill by author."
  182. (interactive)
  183. (gnus-kill-file-enter-kill
  184. "References"
  185. (if (vectorp gnus-current-headers)
  186. (regexp-quote (mail-header-id gnus-current-headers))
  187. "")))
  188. (defun gnus-kill-file-kill-by-xref ()
  189. "Kill by Xref."
  190. (interactive)
  191. (let ((xref (and (vectorp gnus-current-headers)
  192. (mail-header-xref gnus-current-headers)))
  193. (start 0)
  194. group)
  195. (if xref
  196. (while (string-match " \\([^ \t]+\\):" xref start)
  197. (setq start (match-end 0))
  198. (when (not (string=
  199. (setq group
  200. (substring xref (match-beginning 1) (match-end 1)))
  201. gnus-newsgroup-name))
  202. (gnus-kill-file-enter-kill
  203. "Xref" (concat " " (regexp-quote group) ":") t)))
  204. (gnus-kill-file-enter-kill "Xref" "" t))))
  205. (defun gnus-kill-file-raise-followups-to-author (level)
  206. "Raise score for all followups to the current author."
  207. (interactive "p")
  208. (let ((name (mail-header-from gnus-current-headers))
  209. string)
  210. (save-excursion
  211. (gnus-kill-set-kill-buffer)
  212. (goto-char (point-min))
  213. (setq name (read-string (concat "Add " level
  214. " to followup articles to: ")
  215. (regexp-quote name)))
  216. (setq
  217. string
  218. (format
  219. "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
  220. "From" name level))
  221. (insert string)
  222. (gnus-kill-file-apply-string string))
  223. (gnus-message
  224. 6 "Added temporary score file entry for followups to %s." name)))
  225. (defun gnus-kill-file-apply-buffer ()
  226. "Apply current buffer to current newsgroup."
  227. (interactive)
  228. (if (and gnus-current-kill-article
  229. (get-buffer gnus-summary-buffer))
  230. ;; Assume newsgroup is selected.
  231. (gnus-kill-file-apply-string (buffer-string))
  232. (ding) (gnus-message 2 "No newsgroup is selected.")))
  233. (defun gnus-kill-file-apply-string (string)
  234. "Apply STRING to current newsgroup."
  235. (interactive)
  236. (let ((string (concat "(progn \n" string "\n)")))
  237. (save-excursion
  238. (save-window-excursion
  239. (pop-to-buffer gnus-summary-buffer)
  240. (eval (car (read-from-string string)))))))
  241. (defun gnus-kill-file-apply-last-sexp ()
  242. "Apply sexp before point in current buffer to current newsgroup."
  243. (interactive)
  244. (if (and gnus-current-kill-article
  245. (get-buffer gnus-summary-buffer))
  246. ;; Assume newsgroup is selected.
  247. (let ((string
  248. (buffer-substring
  249. (save-excursion (forward-sexp -1) (point)) (point))))
  250. (save-excursion
  251. (save-window-excursion
  252. (pop-to-buffer gnus-summary-buffer)
  253. (eval (car (read-from-string string))))))
  254. (ding) (gnus-message 2 "No newsgroup is selected.")))
  255. (defun gnus-kill-file-exit ()
  256. "Save a kill file, then return to the previous buffer."
  257. (interactive)
  258. (save-buffer)
  259. (let ((killbuf (current-buffer)))
  260. ;; We don't want to return to article buffer.
  261. (when (get-buffer gnus-article-buffer)
  262. (bury-buffer gnus-article-buffer))
  263. ;; Delete the KILL file windows.
  264. (delete-windows-on killbuf)
  265. ;; Restore last window configuration if available.
  266. (when gnus-winconf-kill-file
  267. (set-window-configuration gnus-winconf-kill-file))
  268. (setq gnus-winconf-kill-file nil)
  269. ;; Kill the KILL file buffer. Suggested by tale@pawl.rpi.edu.
  270. (kill-buffer killbuf)))
  271. ;; For kill files
  272. (defun gnus-expunge (marks)
  273. "Remove lines marked with MARKS."
  274. (with-current-buffer gnus-summary-buffer
  275. (gnus-summary-limit-to-marks marks 'reverse)))
  276. (defun gnus-apply-kill-file-unless-scored ()
  277. "Apply .KILL file, unless a .SCORE file for the same newsgroup exists."
  278. (cond ((file-exists-p (gnus-score-file-name gnus-newsgroup-name))
  279. ;; Ignores global KILL.
  280. (when (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name))
  281. (gnus-message 3 "Note: Ignoring %s.KILL; preferring .SCORE"
  282. gnus-newsgroup-name))
  283. 0)
  284. ((or (file-exists-p (gnus-newsgroup-kill-file nil))
  285. (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name)))
  286. (gnus-apply-kill-file-internal))
  287. (t
  288. 0)))
  289. (defun gnus-apply-kill-file-internal ()
  290. "Apply a kill file to the current newsgroup.
  291. Returns the number of articles marked as read."
  292. (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
  293. (gnus-newsgroup-kill-file gnus-newsgroup-name)))
  294. (unreads (length gnus-newsgroup-unreads))
  295. (gnus-summary-inhibit-highlight t)
  296. beg)
  297. (setq gnus-newsgroup-kill-headers nil)
  298. ;; If there are any previously scored articles, we remove these
  299. ;; from the `gnus-newsgroup-headers' list that the score functions
  300. ;; will see. This is probably pretty wasteful when it comes to
  301. ;; conses, but is, I think, faster than having to assq in every
  302. ;; single score function.
  303. (let ((files kill-files))
  304. (while files
  305. (if (file-exists-p (car files))
  306. (let ((headers gnus-newsgroup-headers))
  307. (if gnus-kill-killed
  308. (setq gnus-newsgroup-kill-headers
  309. (mapcar (lambda (header) (mail-header-number header))
  310. headers))
  311. (while headers
  312. (unless (gnus-member-of-range
  313. (mail-header-number (car headers))
  314. gnus-newsgroup-killed)
  315. (push (mail-header-number (car headers))
  316. gnus-newsgroup-kill-headers))
  317. (setq headers (cdr headers))))
  318. (setq files nil))
  319. (setq files (cdr files)))))
  320. (if (not gnus-newsgroup-kill-headers)
  321. ()
  322. (save-window-excursion
  323. (save-excursion
  324. (while kill-files
  325. (if (not (file-exists-p (car kill-files)))
  326. ()
  327. (gnus-message 6 "Processing kill file %s..." (car kill-files))
  328. (find-file (car kill-files))
  329. (goto-char (point-min))
  330. (if (consp (ignore-errors (read (current-buffer))))
  331. (gnus-kill-parse-gnus-kill-file)
  332. (gnus-kill-parse-rn-kill-file))
  333. (gnus-message
  334. 6 "Processing kill file %s...done" (car kill-files)))
  335. (setq kill-files (cdr kill-files)))))
  336. (gnus-set-mode-line 'summary)
  337. (if beg
  338. (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
  339. (or (eq nunreads 0)
  340. (gnus-message 6 "Marked %d articles as read" nunreads))
  341. nunreads)
  342. 0))))
  343. ;; Parse a Gnus killfile.
  344. (defun gnus-kill-parse-gnus-kill-file ()
  345. (goto-char (point-min))
  346. (gnus-kill-file-mode)
  347. (let (beg form)
  348. (while (progn
  349. (setq beg (point))
  350. (setq form (ignore-errors (read (current-buffer)))))
  351. (unless (listp form)
  352. (error "Invalid kill entry (possibly rn kill file?): %s" form))
  353. (if (or (eq (car form) 'gnus-kill)
  354. (eq (car form) 'gnus-raise)
  355. (eq (car form) 'gnus-lower))
  356. (progn
  357. (delete-region beg (point))
  358. (insert (or (eval form) "")))
  359. (with-current-buffer gnus-summary-buffer
  360. (ignore-errors (eval form)))))
  361. (and (buffer-modified-p)
  362. gnus-kill-save-kill-file
  363. (save-buffer))
  364. (set-buffer-modified-p nil)))
  365. ;; Parse an rn killfile.
  366. (defun gnus-kill-parse-rn-kill-file ()
  367. (goto-char (point-min))
  368. (gnus-kill-file-mode)
  369. (let ((mod-to-header
  370. '((?a . "")
  371. (?h . "")
  372. (?f . "from")
  373. (?: . "subject")))
  374. ;;(com-to-com
  375. ;; '((?m . " ")
  376. ;; (?j . "X")))
  377. pattern modifier commands)
  378. (while (not (eobp))
  379. (if (not (looking-at "[ \t]*/\\([^/]*\\)/\\([ahfcH]\\)?:\\([a-z=:]*\\)"))
  380. ()
  381. (setq pattern (buffer-substring (match-beginning 1) (match-end 1)))
  382. (setq modifier (if (match-beginning 2) (char-after (match-beginning 2))
  383. ?s))
  384. (setq commands (buffer-substring (match-beginning 3) (match-end 3)))
  385. ;; The "f:+" command marks everything *but* the matches as read,
  386. ;; so we simply first match everything as read, and then unmark
  387. ;; PATTERN later.
  388. (when (string-match "\\+" commands)
  389. (gnus-kill "from" ".")
  390. (setq commands "m"))
  391. (gnus-kill
  392. (or (cdr (assq modifier mod-to-header)) "subject")
  393. pattern
  394. (if (string-match "m" commands)
  395. '(gnus-summary-tick-article nil " ")
  396. '(gnus-summary-mark-as-read nil "X"))
  397. nil t))
  398. (forward-line 1))))
  399. ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
  400. ;; <joseph@cis.ohio-state.edu>.
  401. (defun gnus-kill (field regexp &optional exe-command all silent)
  402. "If FIELD of an article matches REGEXP, execute COMMAND.
  403. Optional 1st argument COMMAND is default to
  404. (gnus-summary-mark-as-read nil \"X\").
  405. If optional 2nd argument ALL is non-nil, articles marked are also applied to.
  406. If FIELD is an empty string (or nil), entire article body is searched for.
  407. COMMAND must be a Lisp expression or a string representing a key sequence."
  408. ;; We don't want to change current point nor window configuration.
  409. (let ((old-buffer (current-buffer)))
  410. (save-excursion
  411. (save-window-excursion
  412. ;; Selected window must be summary buffer to execute keyboard
  413. ;; macros correctly. See command_loop_1.
  414. (switch-to-buffer gnus-summary-buffer 'norecord)
  415. (goto-char (point-min)) ;From the beginning.
  416. (let ((kill-list regexp)
  417. (date (current-time-string))
  418. (command (or exe-command '(gnus-summary-mark-as-read
  419. nil gnus-kill-file-mark)))
  420. kill kdate prev)
  421. (if (listp kill-list)
  422. ;; It is a list.
  423. (if (not (consp (cdr kill-list)))
  424. ;; It's of the form (regexp . date).
  425. (if (zerop (gnus-execute field (car kill-list)
  426. command nil (not all)))
  427. (when (> (days-between date (cdr kill-list))
  428. gnus-kill-expiry-days)
  429. (setq regexp nil))
  430. (setcdr kill-list date))
  431. (while (setq kill (car kill-list))
  432. (if (consp kill)
  433. ;; It's a temporary kill.
  434. (progn
  435. (setq kdate (cdr kill))
  436. (if (zerop (gnus-execute
  437. field (car kill) command nil (not all)))
  438. (when (> (days-between date kdate)
  439. gnus-kill-expiry-days)
  440. ;; Time limit has been exceeded, so we
  441. ;; remove the match.
  442. (if prev
  443. (setcdr prev (cdr kill-list))
  444. (setq regexp (cdr regexp))))
  445. ;; Successful kill. Set the date to today.
  446. (setcdr kill date)))
  447. ;; It's a permanent kill.
  448. (gnus-execute field kill command nil (not all)))
  449. (setq prev kill-list)
  450. (setq kill-list (cdr kill-list))))
  451. (gnus-execute field kill-list command nil (not all))))))
  452. (switch-to-buffer old-buffer)
  453. (when (and (derived-mode-p 'gnus-kill-file-mode) regexp (not silent))
  454. (gnus-pp-gnus-kill
  455. (nconc (list 'gnus-kill field
  456. (if (consp regexp) (list 'quote regexp) regexp))
  457. (when (or exe-command all)
  458. (list (list 'quote exe-command)))
  459. (if all (list t) nil))))))
  460. (defun gnus-pp-gnus-kill (object)
  461. (if (or (not (consp (nth 2 object)))
  462. (not (consp (cdr (nth 2 object))))
  463. (and (eq 'quote (car (nth 2 object)))
  464. (not (consp (cdadr (nth 2 object))))))
  465. (concat "\n" (gnus-prin1-to-string object))
  466. (with-current-buffer (gnus-get-buffer-create "*Gnus PP*")
  467. (buffer-disable-undo)
  468. (erase-buffer)
  469. (insert (format "\n(%S %S\n '(" (nth 0 object) (nth 1 object)))
  470. (let ((klist (cadr (nth 2 object)))
  471. (first t))
  472. (while klist
  473. (insert (if first (progn (setq first nil) "") "\n ")
  474. (gnus-prin1-to-string (car klist)))
  475. (setq klist (cdr klist))))
  476. (insert ")")
  477. (and (nth 3 object)
  478. (insert "\n "
  479. (if (and (consp (nth 3 object))
  480. (not (eq 'quote (car (nth 3 object)))))
  481. "'" "")
  482. (gnus-prin1-to-string (nth 3 object))))
  483. (when (nth 4 object)
  484. (insert "\n t"))
  485. (insert ")")
  486. (prog1
  487. (buffer-string)
  488. (kill-buffer (current-buffer))))))
  489. (defun gnus-execute-1 (function regexp form header)
  490. (save-excursion
  491. (let (did-kill)
  492. (if (null header)
  493. nil ;Nothing to do.
  494. (if function
  495. ;; Compare with header field.
  496. (let (value)
  497. (and header
  498. (progn
  499. (setq value (funcall function header))
  500. ;; Number (Lines:) or symbol must be converted to string.
  501. (unless (stringp value)
  502. (setq value (gnus-prin1-to-string value)))
  503. (setq did-kill (string-match regexp value)))
  504. (cond ((stringp form) ;Keyboard macro.
  505. (execute-kbd-macro form))
  506. ((functionp form)
  507. (funcall form))
  508. (t
  509. (eval form)))))
  510. ;; Search article body.
  511. (let ((gnus-current-article nil) ;Save article pointer.
  512. (gnus-last-article nil)
  513. (gnus-break-pages nil) ;No need to break pages.
  514. (gnus-mark-article-hook nil)) ;Inhibit marking as read.
  515. (gnus-message
  516. 6 "Searching for article: %d..." (mail-header-number header))
  517. (gnus-article-setup-buffer)
  518. (gnus-article-prepare (mail-header-number header) t)
  519. (when (with-current-buffer gnus-article-buffer
  520. (goto-char (point-min))
  521. (setq did-kill (re-search-forward regexp nil t)))
  522. (cond ((stringp form) ;Keyboard macro.
  523. (execute-kbd-macro form))
  524. ((functionp form)
  525. (funcall form))
  526. (t
  527. (eval form)))))))
  528. did-kill)))
  529. (defun gnus-execute (field regexp form &optional backward unread)
  530. "If FIELD of article header matches REGEXP, execute Lisp FORM (or a string).
  531. If FIELD is an empty string (or nil), entire article body is searched for.
  532. If optional 1st argument BACKWARD is non-nil, do backward instead.
  533. If optional 2nd argument UNREAD is non-nil, articles which are
  534. marked as read or ticked are ignored."
  535. (save-excursion
  536. (let ((killed-no 0)
  537. function article header extras)
  538. (cond
  539. ;; Search body.
  540. ((or (null field)
  541. (string-equal field ""))
  542. (setq function nil))
  543. ;; Get access function of header field.
  544. ((cond ((fboundp
  545. (setq function
  546. (intern-soft
  547. (concat "mail-header-" (downcase field)))))
  548. (setq function `(lambda (h) (,function h))))
  549. ((when (setq extras
  550. (member (downcase field)
  551. (mapcar (lambda (header)
  552. (downcase (symbol-name header)))
  553. gnus-extra-headers)))
  554. (setq function
  555. `(lambda (h)
  556. (gnus-extra-header
  557. (quote ,(nth (- (length gnus-extra-headers)
  558. (length extras))
  559. gnus-extra-headers))
  560. h)))))))
  561. ;; Signal error.
  562. (t
  563. (error "Unknown header field: \"%s\"" field)))
  564. ;; Starting from the current article.
  565. (while (or
  566. ;; First article.
  567. (and (not article)
  568. (setq article (gnus-summary-article-number)))
  569. ;; Find later articles.
  570. (setq article
  571. (gnus-summary-search-forward unread nil backward)))
  572. (and (or (null gnus-newsgroup-kill-headers)
  573. (memq article gnus-newsgroup-kill-headers))
  574. (vectorp (setq header (gnus-summary-article-header article)))
  575. (gnus-execute-1 function regexp form header)
  576. (setq killed-no (1+ killed-no))))
  577. ;; Return the number of killed articles.
  578. killed-no)))
  579. ;;;###autoload
  580. (defalias 'gnus-batch-kill 'gnus-batch-score)
  581. ;;;###autoload
  582. (defun gnus-batch-score ()
  583. "Run batched scoring.
  584. Usage: emacs -batch -l ~/.emacs -l gnus -f gnus-batch-score"
  585. (interactive)
  586. (let* ((gnus-newsrc-options-n
  587. (gnus-newsrc-parse-options
  588. (concat "options -n "
  589. (mapconcat 'identity command-line-args-left " "))))
  590. (gnus-expert-user t)
  591. (mail-sources nil)
  592. (gnus-use-dribble-file nil)
  593. (gnus-batch-mode t)
  594. info group newsrc unread
  595. ;; Disable verbose message.
  596. gnus-novice-user gnus-large-newsgroup
  597. gnus-options-subscribe gnus-auto-subscribed-groups
  598. gnus-options-not-subscribe)
  599. ;; Eat all arguments.
  600. (setq command-line-args-left nil)
  601. (gnus-slave)
  602. ;; Apply kills to specified newsgroups in command line arguments.
  603. (setq newsrc (cdr gnus-newsrc-alist))
  604. (while (setq info (pop newsrc))
  605. (setq group (gnus-info-group info)
  606. unread (gnus-group-unread group))
  607. (when (and (<= (gnus-info-level info) gnus-level-subscribed)
  608. (and unread
  609. (or (eq unread t)
  610. (not (zerop unread)))))
  611. (ignore-errors
  612. (gnus-summary-read-group group nil t nil t))
  613. (when (eq (current-buffer) (get-buffer gnus-summary-buffer))
  614. (gnus-summary-exit))))
  615. ;; Exit Emacs.
  616. (switch-to-buffer gnus-group-buffer)
  617. (gnus-group-save-newsrc)))
  618. (provide 'gnus-kill)
  619. ;;; gnus-kill.el ends here