*** empty log message ***
[gnus] / lisp / gnus-kill.el
1 ;;; gnus-kill.el --- kill commands for Gnus
2 ;; Copyright (C) 1995 Free Software Foundation, Inc.
3
4 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
5 ;;      Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
6 ;; Keywords: news
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'gnus)
29
30 (defvar gnus-kill-file-mode-hook nil
31   "*A hook for Gnus kill file mode.")
32
33 (defvar gnus-kill-expiry-days 7
34   "*Number of days before expiring unused kill file entries.")
35
36 (defvar gnus-winconf-kill-file nil)
37
38 \f
39
40 (defmacro gnus-raise (field expression level)
41   (` (gnus-kill (, field) (, expression)
42                 (function (gnus-summary-raise-score (, level))) t)))
43
44 (defmacro gnus-lower (field expression level)
45   (` (gnus-kill (, field) (, expression)
46                 (function (gnus-summary-raise-score (- (, level)))) t)))
47
48 ;;;
49 ;;; Gnus Kill File Mode
50 ;;;
51
52 (defvar gnus-kill-file-mode-map nil)
53
54 (if gnus-kill-file-mode-map
55     nil
56   (setq gnus-kill-file-mode-map (copy-keymap emacs-lisp-mode-map))
57   (define-key gnus-kill-file-mode-map 
58     "\C-c\C-k\C-s" 'gnus-kill-file-kill-by-subject)
59   (define-key gnus-kill-file-mode-map
60     "\C-c\C-k\C-a" 'gnus-kill-file-kill-by-author)
61   (define-key gnus-kill-file-mode-map
62     "\C-c\C-k\C-t" 'gnus-kill-file-kill-by-thread)
63   (define-key gnus-kill-file-mode-map 
64     "\C-c\C-k\C-x" 'gnus-kill-file-kill-by-xref)
65   (define-key gnus-kill-file-mode-map
66     "\C-c\C-a" 'gnus-kill-file-apply-buffer)
67   (define-key gnus-kill-file-mode-map
68     "\C-c\C-e" 'gnus-kill-file-apply-last-sexp)
69   (define-key gnus-kill-file-mode-map 
70     "\C-c\C-c" 'gnus-kill-file-exit))
71
72 (defun gnus-kill-file-mode ()
73   "Major mode for editing kill files.
74
75 If you are using this mode - you probably shouldn't.  Kill files
76 perform badly and paint with a pretty broad brush.  Score files, on
77 the other hand, are vastly faster (40x speedup) and give you more
78 control over what to do.
79
80 In addition to Emacs-Lisp Mode, the following commands are available:
81
82 \\{gnus-kill-file-mode-map}
83
84   A kill file contains Lisp expressions to be applied to a selected
85 newsgroup.  The purpose is to mark articles as read on the basis of
86 some set of regexps.  A global kill file is applied to every newsgroup,
87 and a local kill file is applied to a specified newsgroup.  Since a
88 global kill file is applied to every newsgroup, for better performance
89 use a local one.
90
91   A kill file can contain any kind of Emacs Lisp expressions expected
92 to be evaluated in the Summary buffer.  Writing Lisp programs for this
93 purpose is not so easy because the internal working of Gnus must be
94 well-known.  For this reason, Gnus provides a general function which
95 does this easily for non-Lisp programmers.
96
97   The `gnus-kill' function executes commands available in Summary Mode
98 by their key sequences. `gnus-kill' should be called with FIELD,
99 REGEXP and optional COMMAND and ALL.  FIELD is a string representing
100 the header field or an empty string.  If FIELD is an empty string, the
101 entire article body is searched for.  REGEXP is a string which is
102 compared with FIELD value. COMMAND is a string representing a valid
103 key sequence in Summary mode or Lisp expression. COMMAND defaults to
104 '(gnus-summary-mark-as-read nil \"X\").  Make sure that COMMAND is
105 executed in the Summary buffer.  If the second optional argument ALL
106 is non-nil, the COMMAND is applied to articles which are already
107 marked as read or unread.  Articles which are marked are skipped over
108 by default.
109
110   For example, if you want to mark articles of which subjects contain
111 the string `AI' as read, a possible kill file may look like:
112
113         (gnus-kill \"Subject\" \"AI\")
114
115   If you want to mark articles with `D' instead of `X', you can use
116 the following expression:
117
118         (gnus-kill \"Subject\" \"AI\" \"d\")
119
120 In this example it is assumed that the command
121 `gnus-summary-mark-as-read-forward' is assigned to `d' in Summary Mode.
122
123   It is possible to delete unnecessary headers which are marked with
124 `X' in a kill file as follows:
125
126         (gnus-expunge \"X\")
127
128   If the Summary buffer is empty after applying kill files, Gnus will
129 exit the selected newsgroup normally.  If headers which are marked
130 with `D' are deleted in a kill file, it is impossible to read articles
131 which are marked as read in the previous Gnus sessions.  Marks other
132 than `D' should be used for articles which should really be deleted.
133
134 Entry to this mode calls emacs-lisp-mode-hook and
135 gnus-kill-file-mode-hook with no arguments, if that value is non-nil."
136   (interactive)
137   (kill-all-local-variables)
138   (use-local-map gnus-kill-file-mode-map)
139   (set-syntax-table emacs-lisp-mode-syntax-table)
140   (setq major-mode 'gnus-kill-file-mode)
141   (setq mode-name "Kill")
142   (lisp-mode-variables nil)
143   (run-hooks 'emacs-lisp-mode-hook 'gnus-kill-file-mode-hook))
144
145 (defun gnus-kill-file-edit-file (newsgroup)
146   "Begin editing a kill file for NEWSGROUP.
147 If NEWSGROUP is nil, the global kill file is selected."
148   (interactive "sNewsgroup: ")
149   (let ((file (gnus-newsgroup-kill-file newsgroup)))
150     (gnus-make-directory (file-name-directory file))
151     ;; Save current window configuration if this is first invocation.
152     (or (and (get-file-buffer file)
153              (get-buffer-window (get-file-buffer file)))
154         (setq gnus-winconf-kill-file (current-window-configuration)))
155     ;; Hack windows.
156     (let ((buffer (find-file-noselect file)))
157       (cond ((get-buffer-window buffer)
158              (pop-to-buffer buffer))
159             ((eq major-mode 'gnus-group-mode)
160              (gnus-configure-windows '(1 0 0)) ;Take all windows.
161              (pop-to-buffer gnus-group-buffer)
162              ;; Fix by sachs@SLINKY.CS.NYU.EDU (Jay Sachs).
163              (let ((gnus-summary-buffer buffer))
164                (gnus-configure-windows '(1 1 0))) ;Split into two.
165              (pop-to-buffer buffer))
166             ((eq major-mode 'gnus-summary-mode)
167              (gnus-configure-windows 'article)
168              (pop-to-buffer gnus-article-buffer)
169              (bury-buffer gnus-article-buffer)
170              (switch-to-buffer buffer))
171             (t                          ;No good rules.
172              (find-file-other-window file))))
173     (gnus-kill-file-mode)))
174
175 ;; Fix by Sudish Joseph <joseph@cis.ohio-state.edu>.
176 (defun gnus-kill-set-kill-buffer ()
177   (let* ((file (gnus-newsgroup-kill-file gnus-newsgroup-name))
178          (buffer (find-file-noselect file)))
179     (set-buffer buffer)
180     (gnus-kill-file-mode)
181     (bury-buffer buffer)))
182
183 (defun gnus-kill-file-enter-kill (field regexp)
184   ;; Enter kill file entry.
185   ;; FIELD: String containing the name of the header field to kill.
186   ;; REGEXP: The string to kill.
187   (save-excursion
188     (let (string)
189       (gnus-kill-set-kill-buffer)
190       (goto-char (point-max))
191       (insert (setq string (format "(gnus-kill %S %S)\n" field regexp)))
192       (gnus-kill-file-apply-string string))))
193     
194 (defun gnus-kill-file-kill-by-subject ()
195   "Kill by subject."
196   (interactive)
197   (gnus-kill-file-enter-kill
198    "Subject" 
199    (regexp-quote 
200     (gnus-simplify-subject (header-subject gnus-current-headers)))))
201   
202 (defun gnus-kill-file-kill-by-author ()
203   "Kill by author."
204   (interactive)
205   (gnus-kill-file-enter-kill
206    "From" (regexp-quote (header-from gnus-current-headers))))
207  
208 (defun gnus-kill-file-kill-by-thread ()
209   "Kill by author."
210   (interactive "p")
211   (gnus-kill-file-enter-kill
212    "References" (regexp-quote (header-id gnus-current-headers))))
213  
214 (defun gnus-kill-file-kill-by-xref ()
215   "Kill by Xref."
216   (interactive)
217   (let ((xref (header-xref gnus-current-headers))
218         (start 0)
219         group)
220     (if xref
221         (while (string-match " \\([^ \t]+\\):" xref start)
222           (setq start (match-end 0))
223           (if (not (string= 
224                     (setq group 
225                           (substring xref (match-beginning 1) (match-end 1)))
226                     gnus-newsgroup-name))
227               (gnus-kill-file-enter-kill 
228                "Xref" (concat " " (regexp-quote group) ":"))))
229       (gnus-kill-file-enter-kill "Xref" ""))))
230
231 (defun gnus-kill-file-raise-followups-to-author (level)
232   "Raise score for all followups to the current author."
233   (interactive "p")
234   (let ((name (header-from gnus-current-headers))
235         string)
236     (save-excursion
237       (gnus-kill-set-kill-buffer)
238       (goto-char (point-min))
239       (setq name (read-string (concat "Add " level
240                                       " to followup articles to: ")
241                               (regexp-quote name)))
242       (setq 
243        string
244        (format
245         "(gnus-kill %S %S '(gnus-summary-temporarily-raise-by-thread %S))\n"
246         "From" name level))
247       (insert string)
248       (gnus-kill-file-apply-string string))
249     (message "Added temporary score file entry for followups to %s." name)))
250
251 (defun gnus-kill-file-apply-buffer ()
252   "Apply current buffer to current newsgroup."
253   (interactive)
254   (if (and gnus-current-kill-article
255            (get-buffer gnus-summary-buffer))
256       ;; Assume newsgroup is selected.
257       (gnus-kill-file-apply-string (buffer-string))
258     (ding) (message "No newsgroup is selected.")))
259
260 (defun gnus-kill-file-apply-string (string)
261   "Apply STRING to current newsgroup."
262   (interactive)
263   (let ((string (concat "(progn \n" string "\n)")))
264     (save-excursion
265       (save-window-excursion
266         (pop-to-buffer gnus-summary-buffer)
267         (eval (car (read-from-string string)))))))
268
269 (defun gnus-kill-file-apply-last-sexp ()
270   "Apply sexp before point in current buffer to current newsgroup."
271   (interactive)
272   (if (and gnus-current-kill-article
273            (get-buffer gnus-summary-buffer))
274       ;; Assume newsgroup is selected.
275       (let ((string
276              (buffer-substring
277               (save-excursion (forward-sexp -1) (point)) (point))))
278         (save-excursion
279           (save-window-excursion
280             (pop-to-buffer gnus-summary-buffer)
281             (eval (car (read-from-string string))))))
282     (ding) (message "No newsgroup is selected.")))
283
284 (defun gnus-kill-file-exit ()
285   "Save a kill file, then return to the previous buffer."
286   (interactive)
287   (save-buffer)
288   (let ((killbuf (current-buffer)))
289     ;; We don't want to return to article buffer.
290     (and (get-buffer gnus-article-buffer)
291          (bury-buffer gnus-article-buffer))
292     ;; Delete the KILL file windows.
293     (delete-windows-on killbuf)
294     ;; Restore last window configuration if available.
295     (and gnus-winconf-kill-file
296          (set-window-configuration gnus-winconf-kill-file))
297     (setq gnus-winconf-kill-file nil)
298     ;; Kill the KILL file buffer.  Suggested by tale@pawl.rpi.edu.
299     (kill-buffer killbuf)))
300
301 ;; For kill files
302
303 (defun gnus-Newsgroup-kill-file (newsgroup)
304   "Return the name of a kill file for NEWSGROUP.
305 If NEWSGROUP is nil, return the global kill file instead."
306   (cond ((or (null newsgroup)
307              (string-equal newsgroup ""))
308          ;; The global kill file is placed at top of the directory.
309          (expand-file-name gnus-kill-file-name
310                            (or gnus-kill-files-directory "~/News")))
311         (gnus-use-long-file-name
312          ;; Append ".KILL" to capitalized newsgroup name.
313          (expand-file-name (concat (gnus-capitalize-newsgroup newsgroup)
314                                    "." gnus-kill-file-name)
315                            (or gnus-kill-files-directory "~/News")))
316         (t
317          ;; Place "KILL" under the hierarchical directory.
318          (expand-file-name (concat (gnus-newsgroup-directory-form newsgroup)
319                                    "/" gnus-kill-file-name)
320                            (or gnus-kill-files-directory "~/News")))))
321
322 (defun gnus-expunge (marks)
323   "Remove lines marked with MARKS."
324   (save-excursion
325     (set-buffer gnus-summary-buffer)
326     (gnus-summary-remove-lines-marked-with marks)))
327
328 (defun gnus-apply-kill-file-internal ()
329   "Apply a kill file to the current newsgroup.
330 Returns the number of articles marked as read."
331   (let* ((kill-files (list (gnus-newsgroup-kill-file nil)
332                            (gnus-newsgroup-kill-file gnus-newsgroup-name)))
333          (unreads (length gnus-newsgroup-unreads))
334          (gnus-summary-inhibit-highlight t)
335          (mark-below (or gnus-summary-mark-below gnus-summary-default-score 0))
336          (expunge-below gnus-summary-expunge-below)
337          form beg)
338     (setq gnus-newsgroup-kill-headers nil)
339     (or gnus-newsgroup-headers-hashtb-by-number
340         (gnus-make-headers-hashtable-by-number))
341     ;; If there are any previously scored articles, we remove these
342     ;; from the `gnus-newsgroup-headers' list that the score functions
343     ;; will see. This is probably pretty wasteful when it comes to
344     ;; conses, but is, I think, faster than having to assq in every
345     ;; single score funtion.
346     (let ((files kill-files))
347       (while files
348         (if (file-exists-p (car files))
349             (let ((headers gnus-newsgroup-headers))
350               (if gnus-kill-killed
351                   (setq gnus-newsgroup-kill-headers
352                         (mapcar (lambda (header) (header-number header))
353                                 headers))
354                 (while headers
355                   (or (gnus-member-of-range 
356                        (header-number (car headers)) 
357                        gnus-newsgroup-killed)
358                       (setq gnus-newsgroup-kill-headers 
359                             (cons (header-number (car headers))
360                                   gnus-newsgroup-kill-headers)))
361                   (setq headers (cdr headers))))
362               (setq files nil))
363           (setq files (cdr files)))))
364     (if gnus-newsgroup-kill-headers
365         (save-excursion
366           (while kill-files
367             (if (file-exists-p (car kill-files))
368                 (progn
369                   (message "Processing kill file %s..." (car kill-files))
370                   (find-file (car kill-files))
371                   (gnus-kill-file-mode)
372                   (gnus-add-current-to-buffer-list)
373                   (goto-char (point-min))
374                   (while (progn
375                            (setq beg (point))
376                            (setq form (condition-case nil 
377                                           (read (current-buffer)) 
378                                         (error nil))))
379                     (or (listp form)
380                         (error 
381                          "Illegal kill entry (possibly rn kill file?): %s"
382                          form))
383                     (if (or (eq (car form) 'gnus-kill)
384                             (eq (car form) 'gnus-raise)
385                             (eq (car form) 'gnus-lower))
386                         (progn
387                           (delete-region beg (point))
388                           (insert (or (eval form) "")))
389                       (condition-case ()
390                           (eval form)
391                         (error nil))))
392                   (and (buffer-modified-p) (save-buffer))
393                   (message "Processing kill file %s...done" (car kill-files))))
394             (setq kill-files (cdr kill-files)))))
395     (if beg
396         (let ((nunreads (- unreads (length gnus-newsgroup-unreads))))
397           (or (eq nunreads 0)
398               (message "Marked %d articles as read" nunreads))
399           nunreads)
400       0)))
401
402 ;; Kill changes and new format by suggested by JWZ and Sudish Joseph
403 ;; <joseph@cis.ohio-state.edu>.  
404 (defun gnus-kill (field regexp &optional exe-command all)
405   "If FIELD of an article matches REGEXP, execute COMMAND.
406 Optional 1st argument COMMAND is default to
407         (gnus-summary-mark-as-read nil \"X\").
408 If optional 2nd argument ALL is non-nil, articles marked are also applied to.
409 If FIELD is an empty string (or nil), entire article body is searched for.
410 COMMAND must be a lisp expression or a string representing a key sequence."
411   ;; We don't want to change current point nor window configuration.
412   (save-excursion
413     (save-window-excursion
414       ;; Selected window must be summary buffer to execute keyboard
415       ;; macros correctly. See command_loop_1.
416       (switch-to-buffer gnus-summary-buffer 'norecord)
417       (goto-char (point-min))           ;From the beginning.
418       (let ((kill-list regexp)
419             (date (current-time-string))
420             (command (or exe-command '(gnus-summary-mark-as-read 
421                                        nil gnus-kill-file-mark)))
422             kill kdate prev)
423         (if (listp kill-list)
424             ;; It is a list.
425             (if (not (consp (cdr kill-list)))
426                 ;; It's on the form (regexp . date).
427                 (if (zerop (gnus-execute field (car kill-list) 
428                                          command nil (not all)))
429                     (if (> (gnus-days-between date (cdr kill-list))
430                            gnus-kill-expiry-days)
431                         (setq regexp nil))
432                   (setcdr kill-list date))
433               (while (setq kill (car kill-list))
434                 (if (consp kill)
435                     ;; It's a temporary kill.
436                     (progn
437                       (setq kdate (cdr kill))
438                       (if (zerop (gnus-execute 
439                                   field (car kill) command nil (not all)))
440                           (if (> (gnus-days-between date kdate)
441                                  gnus-kill-expiry-days)
442                               ;; Time limit has been exceeded, so we
443                               ;; remove the match.
444                               (if prev
445                                   (setcdr prev (cdr kill-list))
446                                 (setq regexp (cdr regexp))))
447                         ;; Successful kill. Set the date to today.
448                         (setcdr kill date)))
449                   ;; It's a permanent kill.
450                   (gnus-execute field kill command nil (not all)))
451                 (setq prev kill-list)
452                 (setq kill-list (cdr kill-list))))
453           (gnus-execute field kill-list command nil (not all))))))
454   (if (and (eq major-mode 'gnus-kill-file-mode) regexp)
455       (gnus-pp-gnus-kill
456        (nconc (list 'gnus-kill field 
457                     (if (consp regexp) (list 'quote regexp) regexp))
458               (if (or exe-command all) (list (list 'quote exe-command)))
459               (if all (list t) nil)))))
460
461 (defun gnus-pp-gnus-kill (object)
462   (if (or (not (consp (nth 2 object)))
463           (not (consp (cdr (nth 2 object))))
464           (and (eq 'quote (car (nth 2 object)))
465                (not (consp (cdr (car (cdr (nth 2 object))))))))
466       (concat "\n" (prin1-to-string object))
467     (save-excursion
468       (set-buffer (get-buffer-create "*Gnus PP*"))
469       (buffer-disable-undo (current-buffer))
470       (erase-buffer)
471       (insert (format "\n(%S %S\n  '(" (nth 0 object) (nth 1 object)))
472       (let ((klist (car (cdr (nth 2 object))))
473             (first t))
474         (while klist
475           (insert (if first (progn (setq first nil) "")  "\n    ")
476                   (prin1-to-string (car klist)))
477           (setq klist (cdr klist))))
478       (insert ")")
479       (and (nth 3 object)
480            (insert "\n  " 
481                    (if (and (consp (nth 3 object))
482                             (not (eq 'quote (car (nth 3 object))))) 
483                        "'" "")
484                    (prin1-to-string (nth 3 object))))
485       (and (nth 4 object)
486            (insert "\n  t"))
487       (insert ")")
488       (prog1
489           (buffer-substring (point-min) (point-max))
490         (kill-buffer (current-buffer))))))
491
492 (defun gnus-execute-1 (function regexp form header)
493   (save-excursion
494     (let (did-kill)
495       (if (null header)
496           nil                           ;Nothing to do.
497         (if function
498             ;; Compare with header field.
499             (let (value)
500               (and header
501                    (progn
502                      (setq value (funcall function header))
503                      ;; Number (Lines:) or symbol must be converted to string.
504                      (or (stringp value)
505                          (setq value (prin1-to-string value)))
506                      (setq did-kill (string-match regexp value)))
507                    (if (stringp form)   ;Keyboard macro.
508                        (execute-kbd-macro form)
509                      (funcall 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             (message "Searching for article: %d..." (header-number header))
516             (gnus-article-setup-buffer)
517             (gnus-article-prepare (header-number header) t)
518             (if (save-excursion
519                   (set-buffer gnus-article-buffer)
520                   (goto-char (point-min))
521                   (setq did-kill (re-search-forward regexp nil t)))
522                 (if (stringp form)      ;Keyboard macro.
523                     (execute-kbd-macro form)
524                   (eval form))))))
525       did-kill)))
526
527 (defun gnus-execute (field regexp form &optional backward ignore-marked)
528   "If FIELD of article header matches REGEXP, execute lisp FORM (or a string).
529 If FIELD is an empty string (or nil), entire article body is searched for.
530 If optional 1st argument BACKWARD is non-nil, do backward instead.
531 If optional 2nd argument IGNORE-MARKED is non-nil, articles which are
532 marked as read or ticked are ignored."
533   (save-excursion
534     (let ((killed-no 0)
535           function header article)
536       (if (or (null field) (string-equal field ""))
537           (setq function nil)
538         ;; Get access function of header filed.
539         (setq function (intern-soft (concat "gnus-header-" (downcase field))))
540         (if (and function (fboundp function))
541             (setq function (symbol-function function))
542           (error "Unknown header field: \"%s\"" field))
543         ;; Make FORM funcallable.
544         (if (and (listp form) (not (eq (car form) 'lambda)))
545             (setq form (list 'lambda nil form))))
546       ;; Starting from the current article.
547       (while (or (and (not article)
548                       (setq article (gnus-summary-article-number))
549                       t)
550                  (setq article 
551                        (gnus-summary-search-subject 
552                         backward (not ignore-marked))))
553         (and (or (null gnus-newsgroup-kill-headers)
554                  (memq article gnus-newsgroup-kill-headers))
555              (gnus-execute-1 function regexp form 
556                              (gnus-get-header-by-number article))
557              (setq killed-no (1+ killed-no))))
558       killed-no)))
559