gnus-kill.el 24 KB

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