(PC-do-complete-and-exit): use minibuffer-prompt-end to
[emacs.git] / lisp / gnus / gnus-logic.el
bloba6028352bf5282599be0ed43206cce3291ee91a4
1 ;;; gnus-logic.el --- advanced scoring code for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
24 ;;; Commentary:
26 ;;; Code:
28 (eval-when-compile (require 'cl))
30 (eval-when-compile (require 'cl))
32 (require 'gnus)
33 (require 'gnus-score)
34 (require 'gnus-util)
36 ;;; Internal variables.
38 (defvar gnus-advanced-headers nil)
40 ;; To avoid having 8-bit characters in the source file.
41 (defvar gnus-advanced-not (intern (format "%c" 172)))
43 (defconst gnus-advanced-index
44 ;; Name to index alist.
45 '(("number" 0 gnus-advanced-integer)
46 ("subject" 1 gnus-advanced-string)
47 ("from" 2 gnus-advanced-string)
48 ("date" 3 gnus-advanced-date)
49 ("message-id" 4 gnus-advanced-string)
50 ("references" 5 gnus-advanced-string)
51 ("chars" 6 gnus-advanced-integer)
52 ("lines" 7 gnus-advanced-integer)
53 ("xref" 8 gnus-advanced-string)
54 ("head" nil gnus-advanced-body)
55 ("body" nil gnus-advanced-body)
56 ("all" nil gnus-advanced-body)))
58 (eval-and-compile
59 (autoload 'parse-time-string "parse-time"))
61 (defun gnus-score-advanced (rule &optional trace)
62 "Apply advanced scoring RULE to all the articles in the current group."
63 (let ((headers gnus-newsgroup-headers)
64 gnus-advanced-headers score)
65 (while (setq gnus-advanced-headers (pop headers))
66 (when (gnus-advanced-score-rule (car rule))
67 ;; This rule was successful, so we add the score to
68 ;; this article.
69 (if (setq score (assq (mail-header-number gnus-advanced-headers)
70 gnus-newsgroup-scored))
71 (setcdr score
72 (+ (cdr score)
73 (or (nth 1 rule)
74 gnus-score-interactive-default-score)))
75 (push (cons (mail-header-number gnus-advanced-headers)
76 (or (nth 1 rule)
77 gnus-score-interactive-default-score))
78 gnus-newsgroup-scored)
79 (when trace
80 (push (cons "A file" rule)
81 gnus-score-trace)))))))
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)
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)
120 (match-beginning 0) (match-end 0)))
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!
130 (error "Unknown advanced score type: %s" rule)))))
132 (defun gnus-advanced-score-article (rule)
133 ;; `rule' is a semi-normal score rule, so we find out
134 ;; what function that's supposed to do the actual
135 ;; processing.
136 (let* ((header (car rule))
137 (func (assoc (downcase header) gnus-advanced-index)))
138 (if (not func)
139 (error "No such header: %s" rule)
140 ;; Call the score function.
141 (funcall (caddr func) (or (cadr func) header)
142 (cadr rule) (caddr rule)))))
144 (defun gnus-advanced-string (index match type)
145 "See whether string MATCH of TYPE matches `gnus-advanced-headers' in INDEX."
146 (let* ((type (or type 's))
147 (case-fold-search (not (eq (downcase (symbol-name type))
148 (symbol-name type))))
149 (header (aref gnus-advanced-headers index)))
150 (cond
151 ((memq type '(r R regexp Regexp))
152 (string-match match header))
153 ((memq type '(s S string String))
154 (string-match (regexp-quote match) header))
155 ((memq type '(e E exact Exact))
156 (string= match header))
157 ((memq type '(f F fuzzy Fuzzy))
158 (string-match (regexp-quote (gnus-simplify-subject-fuzzy match))
159 header))
161 (error "No such string match type: %s" type)))))
163 (defun gnus-advanced-integer (index match type)
164 (if (not (memq type '(< > <= >= =)))
165 (error "No such integer score type: %s" type)
166 (funcall type match (or (aref gnus-advanced-headers index) 0))))
168 (defun gnus-advanced-date (index match type)
169 (let ((date (apply 'encode-time (parse-time-string
170 (aref gnus-advanced-headers index))))
171 (match (apply 'encode-time (parse-time-string match))))
172 (cond
173 ((eq type 'at)
174 (equal date match))
175 ((eq type 'before)
176 (gnus-time-less match date))
177 ((eq type 'after)
178 (gnus-time-less date match))
180 (error "No such date score type: %s" type)))))
182 (defun gnus-advanced-body (header match type)
183 (when (string= header "all")
184 (setq header "article"))
185 (save-excursion
186 (set-buffer nntp-server-buffer)
187 (let* ((request-func (cond ((string= "head" header)
188 'gnus-request-head)
189 ((string= "body" header)
190 'gnus-request-body)
191 (t 'gnus-request-article)))
192 ofunc article)
193 ;; Not all backends support partial fetching. In that case,
194 ;; we just fetch the entire article.
195 (unless (gnus-check-backend-function
196 (intern (concat "request-" header))
197 gnus-newsgroup-name)
198 (setq ofunc request-func)
199 (setq request-func 'gnus-request-article))
200 (setq article (mail-header-number gnus-advanced-headers))
201 (gnus-message 7 "Scoring article %s..." article)
202 (when (funcall request-func article gnus-newsgroup-name)
203 (goto-char (point-min))
204 ;; If just parts of the article is to be searched and the
205 ;; backend didn't support partial fetching, we just narrow
206 ;; to the relevant parts.
207 (when ofunc
208 (if (eq ofunc 'gnus-request-head)
209 (narrow-to-region
210 (point)
211 (or (search-forward "\n\n" nil t) (point-max)))
212 (narrow-to-region
213 (or (search-forward "\n\n" nil t) (point))
214 (point-max))))
215 (let* ((case-fold-search (not (eq (downcase (symbol-name type))
216 (symbol-name type))))
217 (search-func
218 (cond ((memq type '(r R regexp Regexp))
219 're-search-forward)
220 ((memq type '(s S string String))
221 'search-forward)
223 (error "Illegal match type: %s" type)))))
224 (goto-char (point-min))
225 (prog1
226 (funcall search-func match nil t)
227 (widen)))))))
229 (provide 'gnus-logic)
231 ;;; gnus-logic.el ends here.