1 ;;; gnus-kill.el --- kill commands for Gnus
3 ;; Copyright (C) 1995-2018 Free Software Foundation, Inc.
5 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
6 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
32 (defcustom gnus-kill-file-mode-hook nil
33 "Hook for Gnus kill file mode."
34 :group
'gnus-score-kill
37 (defcustom gnus-kill-expiry-days
7
38 "Number of days before expiring unused kill file entries."
39 :group
'gnus-score-kill
40 :group
'gnus-score-expire
43 (defcustom gnus-kill-save-kill-file nil
44 "If non-nil, will save kill files after processing them."
45 :group
'gnus-score-kill
48 (defvar gnus-winconf-kill-file nil
49 "What does this do, Lars?
52 (defcustom gnus-kill-killed t
53 "If non-nil, Gnus will apply kill files to already killed articles.
54 If it is nil, Gnus will never apply kill files to articles that have
55 already been through the scoring process, which might very well save lots
57 :group
'gnus-score-kill
62 (defmacro gnus-raise
(field expression level
)
63 `(gnus-kill ,field
,expression
64 (function (gnus-summary-raise-score ,level
)) t
))
66 (defmacro gnus-lower
(field expression level
)
67 `(gnus-kill ,field
,expression
68 (function (gnus-summary-raise-score (- ,level
))) t
))
71 ;;; Gnus Kill File Mode
74 (defvar gnus-kill-file-mode-map
75 (let ((map (make-sparse-keymap)))
76 (set-keymap-parent map emacs-lisp-mode-map
)
77 (gnus-define-keymap map
78 "\C-c\C-k\C-s" gnus-kill-file-kill-by-subject
79 "\C-c\C-k\C-a" gnus-kill-file-kill-by-author
80 "\C-c\C-k\C-t" gnus-kill-file-kill-by-thread
81 "\C-c\C-k\C-x" gnus-kill-file-kill-by-xref
82 "\C-c\C-a" gnus-kill-file-apply-buffer
83 "\C-c\C-e" gnus-kill-file-apply-last-sexp
84 "\C-c\C-c" gnus-kill-file-exit
)
87 (define-derived-mode gnus-kill-file-mode emacs-lisp-mode
"Kill"
88 "Major mode for editing kill files.
90 If you are using this mode - you probably shouldn't. Kill files
91 perform badly and paint with a pretty broad brush. Score files, on
92 the other hand, are vastly faster (40x speedup) and give you more
93 control over what to do.
95 In addition to Emacs-Lisp Mode, the following commands are available:
97 \\{gnus-kill-file-mode-map}
99 A kill file contains Lisp expressions to be applied to a selected
100 newsgroup. The purpose is to mark articles as read on the basis of
101 some set of regexps. A global kill file is applied to every newsgroup,
102 and a local kill file is applied to a specified newsgroup. Since a
103 global kill file is applied to every newsgroup, for better performance
106 A kill file can contain any kind of Emacs Lisp expressions expected
107 to be evaluated in the Summary buffer. Writing Lisp programs for this
108 purpose is not so easy because the internal working of Gnus must be
109 well-known. For this reason, Gnus provides a general function which
110 does this easily for non-Lisp programmers.
112 The `gnus-kill' function executes commands available in Summary Mode
113 by their key sequences. `gnus-kill' should be called with FIELD,
114 REGEXP and optional COMMAND and ALL. FIELD is a string representing
115 the header field or an empty string. If FIELD is an empty string, the
116 entire article body is searched for. REGEXP is a string which is
117 compared with FIELD value. COMMAND is a string representing a valid
118 key sequence in Summary mode or Lisp expression. COMMAND defaults to
119 \(gnus-summary-mark-as-read nil \"X\"). Make sure that COMMAND is
120 executed in the Summary buffer. If the second optional argument ALL
121 is non-nil, the COMMAND is applied to articles which are already
122 marked as read or unread. Articles which are marked are skipped over
125 For example, if you want to mark articles of which subjects contain
126 the string `AI' as read, a possible kill file may look like:
128 (gnus-kill \"Subject\" \"AI\")
130 If you want to mark articles with `D' instead of `X', you can use
131 the following expression:
133 (gnus-kill \"Subject\" \"AI\" \"d\")
135 In this example it is assumed that the command
136 `gnus-summary-mark-as-read-forward' is assigned to `d' in Summary Mode.
138 It is possible to delete unnecessary headers which are marked with
139 `X' in a kill file as follows:
143 If the Summary buffer is empty after applying kill files, Gnus will
144 exit the selected newsgroup normally. If headers which are marked
145 with `D' are deleted in a kill file, it is impossible to read articles
146 which are marked as read in the previous Gnus sessions. Marks other
147 than `D' should be used for articles which should really be deleted.
149 Entry to this mode calls emacs-lisp-mode-hook and
150 gnus-kill-file-mode-hook with no arguments, if that value is non-nil.")
152 (defun gnus-kill-file-edit-file (newsgroup)
153 "Begin editing a kill file for NEWSGROUP.
154 If NEWSGROUP is nil, the global kill file is selected."
155 (interactive "sNewsgroup: ")
156 (let ((file (gnus-newsgroup-kill-file newsgroup
)))
157 (gnus-make-directory (file-name-directory file
))
158 ;; Save current window configuration if this is first invocation.
159 (or (and (get-file-buffer file
)
160 (get-buffer-window (get-file-buffer file
)))
161 (setq gnus-winconf-kill-file
(current-window-configuration)))
163 (let ((buffer (find-file-noselect file
)))
164 (cond ((get-buffer-window buffer
)
165 (pop-to-buffer buffer
))
166 ((derived-mode-p 'gnus-group-mode
)
167 (gnus-configure-windows 'group
) ;Take all windows.
168 (pop-to-buffer buffer
))
169 ((derived-mode-p 'gnus-summary-mode
)
170 (gnus-configure-windows 'article
)
171 (pop-to-buffer gnus-article-buffer
)
172 (bury-buffer gnus-article-buffer
)
173 (switch-to-buffer buffer
))
175 (find-file-other-window file
))))
176 (gnus-kill-file-mode)))
178 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
179 (defun gnus-kill-set-kill-buffer ()
180 (let* ((file (gnus-newsgroup-kill-file gnus-newsgroup-name
))
181 (buffer (find-file-noselect file
)))
183 (gnus-kill-file-mode)
184 (bury-buffer buffer
)))
186 (defun gnus-kill-file-enter-kill (field regexp
&optional dont-move
)
187 ;; Enter kill file entry.
188 ;; FIELD: String containing the name of the header field to kill.
189 ;; REGEXP: The string to kill.
192 (unless (derived-mode-p 'gnus-kill-file-mode
)
193 (gnus-kill-set-kill-buffer))
195 (goto-char (point-max)))
196 (insert (setq string
(format "(gnus-kill %S %S)\n" field regexp
)))
197 (gnus-kill-file-apply-string string
))))
199 (defun gnus-kill-file-kill-by-subject ()
202 (gnus-kill-file-enter-kill
204 (if (vectorp gnus-current-headers
)
206 (gnus-simplify-subject (mail-header-subject gnus-current-headers
)))
210 (defun gnus-kill-file-kill-by-author ()
213 (gnus-kill-file-enter-kill
215 (if (vectorp gnus-current-headers
)
216 (regexp-quote (mail-header-from gnus-current-headers
))
219 (defun gnus-kill-file-kill-by-thread ()
222 (gnus-kill-file-enter-kill
224 (if (vectorp gnus-current-headers
)
225 (regexp-quote (mail-header-id gnus-current-headers
))
228 (defun gnus-kill-file-kill-by-xref ()
231 (let ((xref (and (vectorp gnus-current-headers
)
232 (mail-header-xref gnus-current-headers
)))
236 (while (string-match " \\([^ \t]+\\):" xref start
)
237 (setq start
(match-end 0))
240 (substring xref
(match-beginning 1) (match-end 1)))
241 gnus-newsgroup-name
))
242 (gnus-kill-file-enter-kill
243 "Xref" (concat " " (regexp-quote group
) ":") t
)))
244 (gnus-kill-file-enter-kill "Xref" "" t
))))
246 (defun gnus-kill-file-raise-followups-to-author (level)
247 "Raise score for all followups to the current author."
249 (let ((name (mail-header-from gnus-current-headers
))
252 (gnus-kill-set-kill-buffer)
253 (goto-char (point-min))
254 (setq name
(read-string (concat "Add " level
255 " to followup articles to: ")
256 (regexp-quote name
)))
260 "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
263 (gnus-kill-file-apply-string string
))
265 6 "Added temporary score file entry for followups to %s." name
)))
267 (defun gnus-kill-file-apply-buffer ()
268 "Apply current buffer to current newsgroup."
270 (if (and gnus-current-kill-article
271 (get-buffer gnus-summary-buffer
))
272 ;; Assume newsgroup is selected.
273 (gnus-kill-file-apply-string (buffer-string))
274 (ding) (gnus-message 2 "No newsgroup is selected.")))
276 (defun gnus-kill-file-apply-string (string)
277 "Apply STRING to current newsgroup."
279 (let ((string (concat "(progn \n" string
"\n)")))
281 (save-window-excursion
282 (pop-to-buffer gnus-summary-buffer
)
283 (eval (car (read-from-string string
)))))))
285 (defun gnus-kill-file-apply-last-sexp ()
286 "Apply sexp before point in current buffer to current newsgroup."
288 (if (and gnus-current-kill-article
289 (get-buffer gnus-summary-buffer
))
290 ;; Assume newsgroup is selected.
293 (save-excursion (forward-sexp -
1) (point)) (point))))
295 (save-window-excursion
296 (pop-to-buffer gnus-summary-buffer
)
297 (eval (car (read-from-string string
))))))
298 (ding) (gnus-message 2 "No newsgroup is selected.")))
300 (defun gnus-kill-file-exit ()
301 "Save a kill file, then return to the previous buffer."
304 (let ((killbuf (current-buffer)))
305 ;; We don't want to return to article buffer.
306 (when (get-buffer gnus-article-buffer
)
307 (bury-buffer gnus-article-buffer
))
308 ;; Delete the KILL file windows.
309 (delete-windows-on killbuf
)
310 ;; Restore last window configuration if available.
311 (when gnus-winconf-kill-file
312 (set-window-configuration gnus-winconf-kill-file
))
313 (setq gnus-winconf-kill-file nil
)
314 ;; Kill the KILL file buffer. Suggested by tale@pawl.rpi.edu.
315 (kill-buffer killbuf
)))
319 (defun gnus-expunge (marks)
320 "Remove lines marked with MARKS."
321 (with-current-buffer gnus-summary-buffer
322 (gnus-summary-limit-to-marks marks
'reverse
)))
324 (defun gnus-apply-kill-file-unless-scored ()
325 "Apply .KILL file, unless a .SCORE file for the same newsgroup exists."
326 (cond ((file-exists-p (gnus-score-file-name gnus-newsgroup-name
))
327 ;; Ignores global KILL.
328 (when (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name
))
329 (gnus-message 3 "Note: Ignoring %s.KILL; preferring .SCORE"
330 gnus-newsgroup-name
))
332 ((or (file-exists-p (gnus-newsgroup-kill-file nil
))
333 (file-exists-p (gnus-newsgroup-kill-file gnus-newsgroup-name
)))
334 (gnus-apply-kill-file-internal))
338 (defun gnus-apply-kill-file-internal ()
339 "Apply a kill file to the current newsgroup.
340 Returns the number of articles marked as read."
341 (let* ((kill-files (list (gnus-newsgroup-kill-file nil
)
342 (gnus-newsgroup-kill-file gnus-newsgroup-name
)))
343 (unreads (length gnus-newsgroup-unreads
))
344 (gnus-summary-inhibit-highlight t
)
346 (setq gnus-newsgroup-kill-headers nil
)
347 ;; If there are any previously scored articles, we remove these
348 ;; from the `gnus-newsgroup-headers' list that the score functions
349 ;; will see. This is probably pretty wasteful when it comes to
350 ;; conses, but is, I think, faster than having to assq in every
351 ;; single score function.
352 (let ((files kill-files
))
354 (if (file-exists-p (car files
))
355 (let ((headers gnus-newsgroup-headers
))
357 (setq gnus-newsgroup-kill-headers
358 (mapcar (lambda (header) (mail-header-number header
))
361 (unless (gnus-member-of-range
362 (mail-header-number (car headers
))
363 gnus-newsgroup-killed
)
364 (push (mail-header-number (car headers
))
365 gnus-newsgroup-kill-headers
))
366 (setq headers
(cdr headers
))))
368 (setq files
(cdr files
)))))
369 (if (not gnus-newsgroup-kill-headers
)
371 (save-window-excursion
374 (if (not (file-exists-p (car kill-files
)))
376 (gnus-message 6 "Processing kill file %s..." (car kill-files
))
377 (find-file (car kill-files
))
378 (goto-char (point-min))
380 (if (consp (ignore-errors (read (current-buffer))))
381 (gnus-kill-parse-gnus-kill-file)
382 (gnus-kill-parse-rn-kill-file))
385 6 "Processing kill file %s...done" (car kill-files
)))
386 (setq kill-files
(cdr kill-files
)))))
388 (gnus-set-mode-line 'summary
)
391 (let ((nunreads (- unreads
(length gnus-newsgroup-unreads
))))
393 (gnus-message 6 "Marked %d articles as read" nunreads
))
397 ;; Parse a Gnus killfile.
398 (defun gnus-kill-parse-gnus-kill-file ()
399 (goto-char (point-min))
400 (gnus-kill-file-mode)
404 (setq form
(ignore-errors (read (current-buffer)))))
406 (error "Invalid kill entry (possibly rn kill file?): %s" form
))
407 (if (or (eq (car form
) 'gnus-kill
)
408 (eq (car form
) 'gnus-raise
)
409 (eq (car form
) 'gnus-lower
))
411 (delete-region beg
(point))
412 (insert (or (eval form
) "")))
413 (with-current-buffer gnus-summary-buffer
414 (ignore-errors (eval form
)))))
415 (and (buffer-modified-p)
416 gnus-kill-save-kill-file
418 (set-buffer-modified-p nil
)))
420 ;; Parse an rn killfile.
421 (defun gnus-kill-parse-rn-kill-file ()
422 (goto-char (point-min))
423 (gnus-kill-file-mode)
432 pattern modifier commands
)
434 (if (not (looking-at "[ \t]*/\\([^/]*\\)/\\([ahfcH]\\)?:\\([a-z=:]*\\)"))
436 (setq pattern
(buffer-substring (match-beginning 1) (match-end 1)))
437 (setq modifier
(if (match-beginning 2) (char-after (match-beginning 2))
439 (setq commands
(buffer-substring (match-beginning 3) (match-end 3)))
441 ;; The "f:+" command marks everything *but* the matches as read,
442 ;; so we simply first match everything as read, and then unmark
444 (when (string-match "\\+" commands
)
445 (gnus-kill "from" ".")
449 (or (cdr (assq modifier mod-to-header
)) "subject")
451 (if (string-match "m" commands
)
452 '(gnus-summary-tick-article nil
" ")
453 '(gnus-summary-mark-as-read nil
"X"))
457 ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
458 ;; <joseph@cis.ohio-state.edu>.
459 (defun gnus-kill (field regexp
&optional exe-command all silent
)
460 "If FIELD of an article matches REGEXP, execute COMMAND.
461 Optional 1st argument COMMAND is default to
462 (gnus-summary-mark-as-read nil \"X\").
463 If optional 2nd argument ALL is non-nil, articles marked are also applied to.
464 If FIELD is an empty string (or nil), entire article body is searched for.
465 COMMAND must be a Lisp expression or a string representing a key sequence."
466 ;; We don't want to change current point nor window configuration.
467 (let ((old-buffer (current-buffer)))
469 (save-window-excursion
470 ;; Selected window must be summary buffer to execute keyboard
471 ;; macros correctly. See command_loop_1.
472 (switch-to-buffer gnus-summary-buffer
'norecord
)
473 (goto-char (point-min)) ;From the beginning.
474 (let ((kill-list regexp
)
475 (date (current-time-string))
476 (command (or exe-command
'(gnus-summary-mark-as-read
477 nil gnus-kill-file-mark
)))
479 (if (listp kill-list
)
481 (if (not (consp (cdr kill-list
)))
482 ;; It's of the form (regexp . date).
483 (if (zerop (gnus-execute field
(car kill-list
)
484 command nil
(not all
)))
485 (when (> (days-between date
(cdr kill-list
))
486 gnus-kill-expiry-days
)
488 (setcdr kill-list date
))
489 (while (setq kill
(car kill-list
))
491 ;; It's a temporary kill.
493 (setq kdate
(cdr kill
))
494 (if (zerop (gnus-execute
495 field
(car kill
) command nil
(not all
)))
496 (when (> (days-between date kdate
)
497 gnus-kill-expiry-days
)
498 ;; Time limit has been exceeded, so we
501 (setcdr prev
(cdr kill-list
))
502 (setq regexp
(cdr regexp
))))
503 ;; Successful kill. Set the date to today.
505 ;; It's a permanent kill.
506 (gnus-execute field kill command nil
(not all
)))
507 (setq prev kill-list
)
508 (setq kill-list
(cdr kill-list
))))
509 (gnus-execute field kill-list command nil
(not all
))))))
510 (switch-to-buffer old-buffer
)
511 (when (and (derived-mode-p 'gnus-kill-file-mode
) regexp
(not silent
))
513 (nconc (list 'gnus-kill field
514 (if (consp regexp
) (list 'quote regexp
) regexp
))
515 (when (or exe-command all
)
516 (list (list 'quote exe-command
)))
517 (if all
(list t
) nil
))))))
519 (defun gnus-pp-gnus-kill (object)
520 (if (or (not (consp (nth 2 object
)))
521 (not (consp (cdr (nth 2 object
))))
522 (and (eq 'quote
(car (nth 2 object
)))
523 (not (consp (cdadr (nth 2 object
))))))
524 (concat "\n" (gnus-prin1-to-string object
))
525 (with-current-buffer (gnus-get-buffer-create "*Gnus PP*")
526 (buffer-disable-undo)
528 (insert (format "\n(%S %S\n '(" (nth 0 object
) (nth 1 object
)))
529 (let ((klist (cadr (nth 2 object
)))
532 (insert (if first
(progn (setq first nil
) "") "\n ")
533 (gnus-prin1-to-string (car klist
)))
534 (setq klist
(cdr klist
))))
538 (if (and (consp (nth 3 object
))
539 (not (eq 'quote
(car (nth 3 object
)))))
541 (gnus-prin1-to-string (nth 3 object
))))
547 (kill-buffer (current-buffer))))))
549 (defun gnus-execute-1 (function regexp form header
)
555 ;; Compare with header field.
559 (setq value
(funcall function header
))
560 ;; Number (Lines:) or symbol must be converted to string.
561 (unless (stringp value
)
562 (setq value
(gnus-prin1-to-string value
)))
563 (setq did-kill
(string-match regexp value
)))
564 (cond ((stringp form
) ;Keyboard macro.
565 (execute-kbd-macro form
))
570 ;; Search article body.
571 (let ((gnus-current-article nil
) ;Save article pointer.
572 (gnus-last-article nil
)
573 (gnus-break-pages nil
) ;No need to break pages.
574 (gnus-mark-article-hook nil
)) ;Inhibit marking as read.
576 6 "Searching for article: %d..." (mail-header-number header
))
577 (gnus-article-setup-buffer)
578 (gnus-article-prepare (mail-header-number header
) t
)
579 (when (with-current-buffer gnus-article-buffer
580 (goto-char (point-min))
581 (setq did-kill
(re-search-forward regexp nil t
)))
582 (cond ((stringp form
) ;Keyboard macro.
583 (execute-kbd-macro form
))
590 (defun gnus-execute (field regexp form
&optional backward unread
)
591 "If FIELD of article header matches REGEXP, execute Lisp FORM (or a string).
592 If FIELD is an empty string (or nil), entire article body is searched for.
593 If optional 1st argument BACKWARD is non-nil, do backward instead.
594 If optional 2nd argument UNREAD is non-nil, articles which are
595 marked as read or ticked are ignored."
598 function article header extras
)
602 (string-equal field
""))
604 ;; Get access function of header field.
608 (concat "mail-header-" (downcase field
)))))
609 (setq function
`(lambda (h) (,function h
))))
611 (member (downcase field
)
612 (mapcar (lambda (header)
613 (downcase (symbol-name header
)))
614 gnus-extra-headers
)))
618 (quote ,(nth (- (length gnus-extra-headers
)
624 (error "Unknown header field: \"%s\"" field
)))
625 ;; Starting from the current article.
629 (setq article
(gnus-summary-article-number)))
630 ;; Find later articles.
632 (gnus-summary-search-forward unread nil backward
)))
633 (and (or (null gnus-newsgroup-kill-headers
)
634 (memq article gnus-newsgroup-kill-headers
))
635 (vectorp (setq header
(gnus-summary-article-header article
)))
636 (gnus-execute-1 function regexp form header
)
637 (setq killed-no
(1+ killed-no
))))
638 ;; Return the number of killed articles.
642 (defalias 'gnus-batch-kill
'gnus-batch-score
)
644 (defun gnus-batch-score ()
645 "Run batched scoring.
646 Usage: emacs -batch -l ~/.emacs -l gnus -f gnus-batch-score"
648 (let* ((gnus-newsrc-options-n
649 (gnus-newsrc-parse-options
650 (concat "options -n "
651 (mapconcat 'identity command-line-args-left
" "))))
654 (gnus-use-dribble-file nil
)
656 info group newsrc unread
657 ;; Disable verbose message.
658 gnus-novice-user gnus-large-newsgroup
659 gnus-options-subscribe gnus-auto-subscribed-groups
660 gnus-options-not-subscribe
)
661 ;; Eat all arguments.
662 (setq command-line-args-left nil
)
664 ;; Apply kills to specified newsgroups in command line arguments.
665 (setq newsrc
(cdr gnus-newsrc-alist
))
666 (while (setq info
(pop newsrc
))
667 (setq group
(gnus-info-group info
)
668 unread
(gnus-group-unread group
))
669 (when (and (<= (gnus-info-level info
) gnus-level-subscribed
)
672 (not (zerop unread
)))))
674 (gnus-summary-read-group group nil t nil t
))
675 (when (eq (current-buffer) (get-buffer gnus-summary-buffer
))
676 (gnus-summary-exit))))
678 (switch-to-buffer gnus-group-buffer
)
679 (gnus-group-save-newsrc)))
683 ;;; gnus-kill.el ends here