Mercurial > emacs
annotate lisp/gnus/gnus-logic.el @ 57974:be8ba6e58a49
(hide-body): Don't hide lines at the top of the file
that precede the first header line.
author | Eli Zaretskii <eliz@gnu.org> |
---|---|
date | Sat, 06 Nov 2004 12:10:38 +0000 |
parents | 55fd4f77387a |
children | 18a818a2ee7c cce1c0ee76ee |
rev | line source |
---|---|
17493 | 1 ;;; gnus-logic.el --- advanced scoring code for Gnus |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002 |
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
24357
diff
changeset
|
3 ;; Free Software Foundation, Inc. |
17493 | 4 |
24357
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19521
diff
changeset
|
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org> |
17493 | 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 the | |
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
23 ;; Boston, MA 02111-1307, USA. | |
24 | |
25 ;;; Commentary: | |
26 | |
27 ;;; Code: | |
28 | |
19521
6f6cf9184e93
Require cl at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
17493
diff
changeset
|
29 (eval-when-compile (require 'cl)) |
6f6cf9184e93
Require cl at compile time.
Richard M. Stallman <rms@gnu.org>
parents:
17493
diff
changeset
|
30 |
17493 | 31 (require 'gnus) |
32 (require 'gnus-score) | |
33 (require 'gnus-util) | |
34 | |
35 ;;; Internal variables. | |
36 | |
37 (defvar gnus-advanced-headers nil) | |
38 | |
39 ;; To avoid having 8-bit characters in the source file. | |
40 (defvar gnus-advanced-not (intern (format "%c" 172))) | |
41 | |
42 (defconst gnus-advanced-index | |
43 ;; Name to index alist. | |
44 '(("number" 0 gnus-advanced-integer) | |
45 ("subject" 1 gnus-advanced-string) | |
46 ("from" 2 gnus-advanced-string) | |
47 ("date" 3 gnus-advanced-date) | |
48 ("message-id" 4 gnus-advanced-string) | |
49 ("references" 5 gnus-advanced-string) | |
50 ("chars" 6 gnus-advanced-integer) | |
51 ("lines" 7 gnus-advanced-integer) | |
52 ("xref" 8 gnus-advanced-string) | |
53 ("head" nil gnus-advanced-body) | |
54 ("body" nil gnus-advanced-body) | |
55 ("all" nil gnus-advanced-body))) | |
56 | |
57 (eval-and-compile | |
58 (autoload 'parse-time-string "parse-time")) | |
59 | |
60 (defun gnus-score-advanced (rule &optional trace) | |
61 "Apply advanced scoring RULE to all the articles in the current group." | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
62 (let (new-score score multiple) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
63 (dolist (gnus-advanced-headers gnus-newsgroup-headers) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
64 (when (setq multiple (gnus-advanced-score-rule (car rule))) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
65 (setq new-score (or (nth 1 rule) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
66 gnus-score-interactive-default-score)) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
67 (when (numberp multiple) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
68 (setq new-score (* multiple new-score))) |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
69 ;; This rule was successful, so we add the score to this |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
70 ;; article. |
17493 | 71 (if (setq score (assq (mail-header-number gnus-advanced-headers) |
72 gnus-newsgroup-scored)) | |
73 (setcdr score | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
74 (+ (cdr score) new-score)) |
17493 | 75 (push (cons (mail-header-number gnus-advanced-headers) |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
76 new-score) |
17493 | 77 gnus-newsgroup-scored) |
78 (when trace | |
79 (push (cons "A file" rule) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
80 ;; Must be synced with `gnus-score-edit-file-at-point'. |
17493 | 81 gnus-score-trace))))))) |
82 | |
83 (defun gnus-advanced-score-rule (rule) | |
84 "Apply RULE to `gnus-advanced-headers'." | |
85 (let ((type (car rule))) | |
86 (cond | |
87 ;; "And" rule. | |
88 ((or (eq type '&) (eq type 'and)) | |
89 (pop rule) | |
90 (if (not rule) | |
91 t ; Empty rule is true. | |
92 (while (and rule | |
93 (gnus-advanced-score-rule (car rule))) | |
94 (pop rule)) | |
95 ;; If all the rules were true, then `rule' should be nil. | |
96 (not rule))) | |
97 ;; "Or" rule. | |
98 ((or (eq type '|) (eq type 'or)) | |
99 (pop rule) | |
100 (if (not rule) | |
101 nil | |
102 (while (and rule | |
103 (not (gnus-advanced-score-rule (car rule)))) | |
104 (pop rule)) | |
105 ;; If one of the rules returned true, then `rule' should be non-nil. | |
106 rule)) | |
107 ;; "Not" rule. | |
108 ((or (eq type '!) (eq type 'not) (eq type gnus-advanced-not)) | |
109 (not (gnus-advanced-score-rule (nth 1 rule)))) | |
110 ;; This is a `1-'-type redirection rule. | |
111 ((and (symbolp type) | |
112 (string-match "^[0-9]+-$\\|^\\^+$" (symbol-name type))) | |
113 (let ((gnus-advanced-headers | |
114 (gnus-parent-headers | |
115 gnus-advanced-headers | |
116 (if (string-match "^\\([0-9]+\\)-$" (symbol-name type)) | |
117 ;; 1- type redirection. | |
118 (string-to-number | |
119 (substring (symbol-name type) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
120 (match-beginning 1) (match-end 1))) |
17493 | 121 ;; ^^^ type redirection. |
122 (length (symbol-name type)))))) | |
123 (when gnus-advanced-headers | |
124 (gnus-advanced-score-rule (nth 1 rule))))) | |
125 ;; Plain scoring rule. | |
126 ((stringp type) | |
127 (gnus-advanced-score-article rule)) | |
128 ;; Bug-out time! | |
129 (t | |
130 (error "Unknown advanced score type: %s" rule))))) | |
131 | |
132 (defun gnus-advanced-score-article (rule) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
133 ;; `rule' is a semi-normal score rule, so we find out what function |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
134 ;; that's supposed to do the actual processing. |
17493 | 135 (let* ((header (car rule)) |
136 (func (assoc (downcase header) gnus-advanced-index))) | |
137 (if (not func) | |
138 (error "No such header: %s" rule) | |
139 ;; Call the score function. | |
140 (funcall (caddr func) (or (cadr func) header) | |
141 (cadr rule) (caddr rule))))) | |
142 | |
143 (defun gnus-advanced-string (index match type) | |
144 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX." | |
145 (let* ((type (or type 's)) | |
146 (case-fold-search (not (eq (downcase (symbol-name type)) | |
147 (symbol-name type)))) | |
33268
88bece18cf0d
2000-10-07 15:42:59 ShengHuo ZHU <zsh@cs.rochester.edu>
Dave Love <fx@gnu.org>
parents:
31716
diff
changeset
|
148 (header (or (aref gnus-advanced-headers index) ""))) |
17493 | 149 (cond |
150 ((memq type '(r R regexp Regexp)) | |
151 (string-match match header)) | |
152 ((memq type '(s S string String)) | |
153 (string-match (regexp-quote match) header)) | |
154 ((memq type '(e E exact Exact)) | |
155 (string= match header)) | |
156 ((memq type '(f F fuzzy Fuzzy)) | |
157 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match)) | |
158 header)) | |
159 (t | |
160 (error "No such string match type: %s" type))))) | |
161 | |
162 (defun gnus-advanced-integer (index match type) | |
163 (if (not (memq type '(< > <= >= =))) | |
164 (error "No such integer score type: %s" type) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
165 (funcall type (or (aref gnus-advanced-headers index) 0) match))) |
17493 | 166 |
167 (defun gnus-advanced-date (index match type) | |
24357
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19521
diff
changeset
|
168 (let ((date (apply 'encode-time (parse-time-string |
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19521
diff
changeset
|
169 (aref gnus-advanced-headers index)))) |
15fc6acbae7a
Upgrading to Gnus 5.7; see ChangeLog
Lars Magne Ingebrigtsen <larsi@gnus.org>
parents:
19521
diff
changeset
|
170 (match (apply 'encode-time (parse-time-string match)))) |
17493 | 171 (cond |
172 ((eq type 'at) | |
173 (equal date match)) | |
174 ((eq type 'before) | |
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
24357
diff
changeset
|
175 (time-less-p match date)) |
17493 | 176 ((eq type 'after) |
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
24357
diff
changeset
|
177 (time-less-p date match)) |
17493 | 178 (t |
179 (error "No such date score type: %s" type))))) | |
180 | |
181 (defun gnus-advanced-body (header match type) | |
182 (when (string= header "all") | |
183 (setq header "article")) | |
184 (save-excursion | |
185 (set-buffer nntp-server-buffer) | |
186 (let* ((request-func (cond ((string= "head" header) | |
187 'gnus-request-head) | |
188 ((string= "body" header) | |
189 'gnus-request-body) | |
190 (t 'gnus-request-article))) | |
191 ofunc article) | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
192 ;; Not all backends support partial fetching. In that case, we |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
193 ;; just fetch the entire article. |
17493 | 194 (unless (gnus-check-backend-function |
195 (intern (concat "request-" header)) | |
196 gnus-newsgroup-name) | |
197 (setq ofunc request-func) | |
198 (setq request-func 'gnus-request-article)) | |
199 (setq article (mail-header-number gnus-advanced-headers)) | |
200 (gnus-message 7 "Scoring article %s..." article) | |
201 (when (funcall request-func article gnus-newsgroup-name) | |
202 (goto-char (point-min)) | |
203 ;; If just parts of the article is to be searched and the | |
56927
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
204 ;; backend didn't support partial fetching, we just narrow to |
55fd4f77387a
Revision: miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-523
Miles Bader <miles@gnu.org>
parents:
52401
diff
changeset
|
205 ;; the relevant parts. |
17493 | 206 (when ofunc |
207 (if (eq ofunc 'gnus-request-head) | |
208 (narrow-to-region | |
209 (point) | |
210 (or (search-forward "\n\n" nil t) (point-max))) | |
211 (narrow-to-region | |
212 (or (search-forward "\n\n" nil t) (point)) | |
213 (point-max)))) | |
214 (let* ((case-fold-search (not (eq (downcase (symbol-name type)) | |
215 (symbol-name type)))) | |
216 (search-func | |
217 (cond ((memq type '(r R regexp Regexp)) | |
218 're-search-forward) | |
219 ((memq type '(s S string String)) | |
220 'search-forward) | |
221 (t | |
31716
9968f55ad26e
Update to emacs-21-branch of the Gnus CVS repository.
Gerd Moellmann <gerd@gnu.org>
parents:
24357
diff
changeset
|
222 (error "Invalid match type: %s" type))))) |
17493 | 223 (goto-char (point-min)) |
224 (prog1 | |
225 (funcall search-func match nil t) | |
226 (widen))))))) | |
227 | |
228 (provide 'gnus-logic) | |
229 | |
52401 | 230 ;;; arch-tag: 9651a100-4a59-4b69-a55b-e511e67c0f8d |
38413
a26d9b55abb6
Some fixes to follow coding conventions in files from Gnus.
Pavel Janík <Pavel@Janik.cz>
parents:
33342
diff
changeset
|
231 ;;; gnus-logic.el ends here |