(mm-url-insert-file-contents-external): Autoload
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2
3 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;;   2005, 2006 Free Software Foundation, Inc.
5
6 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
7 ;; Keywords: news
8
9 ;; This file is part of GNU Emacs.
10
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 2, or (at your option)
14 ;; any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile
31   (require 'cl)
32   (defvar tool-bar-map)
33   (defvar w3m-minor-mode-map))
34
35 (require 'gnus)
36 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
37 (eval-and-compile
38   (let ((recursive-load-depth-limit 100))
39     (require 'gnus-sum)))
40 (require 'gnus-spec)
41 (require 'gnus-int)
42 (require 'gnus-win)
43 (require 'mm-bodies)
44 (require 'mail-parse)
45 (require 'mm-decode)
46 (require 'mm-view)
47 (require 'wid-edit)
48 (require 'mm-uu)
49 (require 'message)
50
51 (autoload 'gnus-msg-mail "gnus-msg" nil t)
52 (autoload 'gnus-button-mailto "gnus-msg")
53 (autoload 'gnus-button-reply "gnus-msg" nil t)
54 (autoload 'parse-time-string "parse-time" nil nil)
55 (autoload 'ansi-color-apply-on-region "ansi-color")
56 (autoload 'mm-url-insert-file-contents-external "mm-url")
57
58 (defgroup gnus-article nil
59   "Article display."
60   :link '(custom-manual "(gnus)Article Buffer")
61   :group 'gnus)
62
63 (defgroup gnus-article-treat nil
64   "Treating article parts."
65   :link '(custom-manual "(gnus)Article Hiding")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-hiding nil
69   "Hiding article parts."
70   :link '(custom-manual "(gnus)Article Hiding")
71   :group 'gnus-article)
72
73 (defgroup gnus-article-highlight nil
74   "Article highlighting."
75   :link '(custom-manual "(gnus)Article Highlighting")
76   :group 'gnus-article
77   :group 'gnus-visual)
78
79 (defgroup gnus-article-signature nil
80   "Article signatures."
81   :link '(custom-manual "(gnus)Article Signature")
82   :group 'gnus-article)
83
84 (defgroup gnus-article-headers nil
85   "Article headers."
86   :link '(custom-manual "(gnus)Hiding Headers")
87   :group 'gnus-article)
88
89 (defgroup gnus-article-washing nil
90   "Special commands on articles."
91   :link '(custom-manual "(gnus)Article Washing")
92   :group 'gnus-article)
93
94 (defgroup gnus-article-emphasis nil
95   "Fontisizing articles."
96   :link '(custom-manual "(gnus)Article Fontisizing")
97   :group 'gnus-article)
98
99 (defgroup gnus-article-saving nil
100   "Saving articles."
101   :link '(custom-manual "(gnus)Saving Articles")
102   :group 'gnus-article)
103
104 (defgroup gnus-article-mime nil
105   "Worshiping the MIME wonder."
106   :link '(custom-manual "(gnus)Using MIME")
107   :group 'gnus-article)
108
109 (defgroup gnus-article-buttons nil
110   "Pushable buttons in the article buffer."
111   :link '(custom-manual "(gnus)Article Buttons")
112   :group 'gnus-article)
113
114 (defgroup gnus-article-various nil
115   "Other article options."
116   :link '(custom-manual "(gnus)Misc Article")
117   :group 'gnus-article)
118
119 (defcustom gnus-ignored-headers
120   (mapcar
121    (lambda (header)
122      (concat "^" header ":"))
123    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
124      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
125      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
126      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
127      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
128      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
129      "X-Attribution" "X-Originating-IP" "Delivered-To"
130      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
131      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
132      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
133      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
134      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
135      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
136      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
137      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
138      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
139      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
140      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
141      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
142      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
143      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
144      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
145      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
146      "List-[A-Za-z]+" "X-Listprocessor-Version"
147      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
148      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
149      "X-Received" "Content-length" "X-precedence"
150      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
151      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
152      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
153      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
154      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
155      "X-Content-length" "X-Posting-Agent" "Original-Received"
156      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
157      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
158      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
159      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
160      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"))
161   "*All headers that start with this regexp will be hidden.
162 This variable can also be a list of regexps of headers to be ignored.
163 If `gnus-visible-headers' is non-nil, this variable will be ignored."
164   :type '(choice :custom-show nil
165                  regexp
166                  (repeat regexp))
167   :group 'gnus-article-hiding)
168
169 (defcustom gnus-visible-headers
170   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
171   "*All headers that do not match this regexp will be hidden.
172 This variable can also be a list of regexp of headers to remain visible.
173 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
174   :type '(repeat :value-to-internal (lambda (widget value)
175                                       (custom-split-regexp-maybe value))
176                  :match (lambda (widget value)
177                           (or (stringp value)
178                               (widget-editable-list-match widget value)))
179                  regexp)
180   :group 'gnus-article-hiding)
181
182 (defcustom gnus-sorted-header-list
183   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
184     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
185   "*This variable is a list of regular expressions.
186 If it is non-nil, headers that match the regular expressions will
187 be placed first in the article buffer in the sequence specified by
188 this list."
189   :type '(repeat regexp)
190   :group 'gnus-article-hiding)
191
192 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
193   "Headers that are only to be displayed if they have interesting data.
194 Possible values in this list are:
195
196   'empty       Headers with no content.
197   'newsgroups  Newsgroup identical to Gnus group.
198   'to-address  To identical to To-address.
199   'to-list     To identical to To-list.
200   'cc-list     CC identical to To-list.
201   'followup-to Followup-to identical to Newsgroups.
202   'reply-to    Reply-to identical to From.
203   'date        Date less than four days old.
204   'long-to     To and/or Cc longer than 1024 characters.
205   'many-to     Multiple To and/or Cc."
206   :type '(set (const :tag "Headers with no content." empty)
207               (const :tag "Newsgroups identical to Gnus group." newsgroups)
208               (const :tag "To identical to To-address." to-address)
209               (const :tag "To identical to To-list." to-list)
210               (const :tag "CC identical to To-list." cc-list)
211               (const :tag "Followup-to identical to Newsgroups." followup-to)
212               (const :tag "Reply-to identical to From." reply-to)
213               (const :tag "Date less than four days old." date)
214               (const :tag "To and/or Cc longer than 1024 characters." long-to)
215               (const :tag "Multiple To and/or Cc headers." many-to))
216   :group 'gnus-article-hiding)
217
218 (defcustom gnus-article-skip-boring nil
219   "Skip over text that is not worth reading.
220 By default, if you set this t, then Gnus will display citations and
221 signatures, but will never scroll down to show you a page consisting
222 only of boring text.  Boring text is controlled by
223 `gnus-article-boring-faces'."
224   :version "22.1"
225   :type 'boolean
226   :group 'gnus-article-hiding)
227
228 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
229   "Regexp matching signature separator.
230 This can also be a list of regexps.  In that case, it will be checked
231 from head to tail looking for a separator.  Searches will be done from
232 the end of the buffer."
233   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
234                  (regexp)
235                  (repeat :tag "List of regexp" regexp))
236   :group 'gnus-article-signature)
237
238 (defcustom gnus-signature-limit nil
239   "Provide a limit to what is considered a signature.
240 If it is a number, no signature may not be longer (in characters) than
241 that number.  If it is a floating point number, no signature may be
242 longer (in lines) than that number.  If it is a function, the function
243 will be called without any parameters, and if it returns nil, there is
244 no signature in the buffer.  If it is a string, it will be used as a
245 regexp.  If it matches, the text in question is not a signature.
246
247 This can also be a list of the above values."
248   :type '(choice (const nil)
249                  (integer :value 200)
250                  (number :value 4.0)
251                  (function :value fun)
252                  (regexp :value ".*"))
253   :group 'gnus-article-signature)
254
255 (defcustom gnus-hidden-properties '(invisible t intangible t)
256   "Property list to use for hiding text."
257   :type 'sexp
258   :group 'gnus-article-hiding)
259
260 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
261 ;; frames in a session.
262 (defcustom gnus-article-x-face-command
263   (if (featurep 'xemacs)
264       (if (or (gnus-image-type-available-p 'xface)
265               (gnus-image-type-available-p 'pbm))
266           'gnus-display-x-face-in-from
267         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
268     (if (gnus-image-type-available-p 'pbm)
269         'gnus-display-x-face-in-from
270       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
271 display -"))
272   "*String or function to be executed to display an X-Face header.
273 If it is a string, the command will be executed in a sub-shell
274 asynchronously.  The compressed face will be piped to this command."
275   :type `(choice string
276                  (function-item gnus-display-x-face-in-from)
277                  function)
278   :version "21.1"
279   :group 'gnus-picon
280   :group 'gnus-article-washing)
281
282 (defcustom gnus-article-x-face-too-ugly nil
283   "Regexp matching posters whose face shouldn't be shown automatically."
284   :type '(choice regexp (const nil))
285   :group 'gnus-article-washing)
286
287 (defcustom gnus-article-banner-alist nil
288   "Banner alist for stripping.
289 For example,
290      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
291   :version "21.1"
292   :type '(repeat (cons symbol regexp))
293   :group 'gnus-article-washing)
294
295 (gnus-define-group-parameter
296  banner
297  :variable-document
298  "Alist of regexps (to match group names) and banner."
299  :variable-group gnus-article-washing
300  :parameter-type
301  '(choice :tag "Banner"
302           :value nil
303           (const :tag "Remove signature" signature)
304           (symbol :tag "Item in `gnus-article-banner-alist'" none)
305           regexp
306           (const :tag "None" nil))
307  :parameter-document
308  "If non-nil, specify how to remove `banners' from articles.
309
310 Symbol `signature' means to remove signatures delimited by
311 `gnus-signature-separator'.  Any other symbol is used to look up a
312 regular expression to match the banner in `gnus-article-banner-alist'.
313 A string is used as a regular expression to match the banner
314 directly.")
315
316 (defcustom gnus-article-address-banner-alist nil
317   "Alist of mail addresses and banners.
318 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
319 to match a mail address in the From: header, BANNER is one of a symbol
320 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
321 If ADDRESS matches author's mail address, it will remove things like
322 advertisements.  For example:
323
324 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
325 "
326   :type '(repeat
327           (cons
328            (regexp :tag "Address")
329            (choice :tag "Banner" :value nil
330                    (const :tag "Remove signature" signature)
331                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
332                    regexp
333                    (const :tag "None" nil))))
334   :version "22.1"
335   :group 'gnus-article-washing)
336
337 (defmacro gnus-emphasis-custom-with-format (&rest body)
338   `(let ((format "\
339 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
340 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
341      ,@body))
342
343 (defun gnus-emphasis-custom-value-to-external (value)
344   (gnus-emphasis-custom-with-format
345    (if (consp (car value))
346        (list (format format (car (car value)) (cdr (car value)))
347              2
348              (if (nth 1 value) 2 3)
349              (nth 2 value))
350      value)))
351
352 (defun gnus-emphasis-custom-value-to-internal (value)
353   (gnus-emphasis-custom-with-format
354    (let ((regexp (concat "\\`"
355                          (format (regexp-quote format)
356                                  "\\([^()]+\\)" "\\([^()]+\\)")
357                          "\\'"))
358          pattern)
359      (if (string-match regexp (setq pattern (car value)))
360          (list (cons (match-string 1 pattern) (match-string 2 pattern))
361                (= (nth 2 value) 2)
362                (nth 3 value))
363        value))))
364
365 (defcustom gnus-emphasis-alist
366   (let ((types
367          '(("\\*" "\\*" bold nil 2)
368            ("_" "_" underline)
369            ("/" "/" italic)
370            ("_/" "/_" underline-italic)
371            ("_\\*" "\\*_" underline-bold)
372            ("\\*/" "/\\*" bold-italic)
373            ("_\\*/" "/\\*_" underline-bold-italic))))
374     (nconc
375      (gnus-emphasis-custom-with-format
376       (mapcar (lambda (spec)
377                 (list (format format (car spec) (cadr spec))
378                       (or (nth 3 spec) 2)
379                       (or (nth 4 spec) 3)
380                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
381               types))
382      '(;; I've never seen anyone use this strikethru convention whereas I've
383        ;; several times seen it triggered by normal text.  --Stef
384        ;; Miles suggests that this form is sometimes used but for italics,
385        ;; so maybe we should map it to `italic'.
386        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
387        ;; 2 3 gnus-emphasis-strikethru)
388        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
389         2 3 gnus-emphasis-underline))))
390   "*Alist that says how to fontify certain phrases.
391 Each item looks like this:
392
393   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
394
395 The first element is a regular expression to be matched.  The second
396 is a number that says what regular expression grouping used to find
397 the entire emphasized word.  The third is a number that says what
398 regexp grouping should be displayed and highlighted.  The fourth
399 is the face used for highlighting."
400   :type
401   '(repeat
402     (menu-choice
403      :format "%[Customizing Style%]\n%v"
404      :indent 2
405      (group :tag "Default"
406             :value ("" 0 0 default)
407             :value-create
408             (lambda (widget)
409               (let ((value (widget-get
410                             (cadr (widget-get (widget-get widget :parent)
411                                               :args))
412                             :value)))
413                 (if (not (eq (nth 2 value) 'default))
414                     (widget-put
415                      widget
416                      :value
417                      (gnus-emphasis-custom-value-to-external value))))
418               (widget-group-value-create widget))
419             regexp
420             (integer :format "Match group: %v")
421             (integer :format "Emphasize group: %v")
422             face)
423      (group :tag "Simple"
424             :value (("_" . "_") nil default)
425             (cons :format "%v"
426                   (regexp :format "Start regexp: %v")
427                   (regexp :format "End regexp: %v"))
428             (boolean :format "Show start and end patterns: %[%v%]\n"
429                      :on " On " :off " Off ")
430             face)))
431   :get (lambda (symbol)
432          (mapcar 'gnus-emphasis-custom-value-to-internal
433                  (default-value symbol)))
434   :set (lambda (symbol value)
435          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
436                                      value)))
437   :group 'gnus-article-emphasis)
438
439 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
440   "A regexp to describe whitespace which should not be emphasized.
441 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
442 The former avoids underlining of leading and trailing whitespace,
443 and the latter avoids underlining any whitespace at all."
444   :version "21.1"
445   :group 'gnus-article-emphasis
446   :type 'regexp)
447
448 (defface gnus-emphasis-bold '((t (:bold t)))
449   "Face used for displaying strong emphasized text (*word*)."
450   :group 'gnus-article-emphasis)
451
452 (defface gnus-emphasis-italic '((t (:italic t)))
453   "Face used for displaying italic emphasized text (/word/)."
454   :group 'gnus-article-emphasis)
455
456 (defface gnus-emphasis-underline '((t (:underline t)))
457   "Face used for displaying underlined emphasized text (_word_)."
458   :group 'gnus-article-emphasis)
459
460 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
461   "Face used for displaying underlined bold emphasized text (_*word*_)."
462   :group 'gnus-article-emphasis)
463
464 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
465   "Face used for displaying underlined italic emphasized text (_/word/_)."
466   :group 'gnus-article-emphasis)
467
468 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
469   "Face used for displaying bold italic emphasized text (/*word*/)."
470   :group 'gnus-article-emphasis)
471
472 (defface gnus-emphasis-underline-bold-italic
473   '((t (:bold t :italic t :underline t)))
474   "Face used for displaying underlined bold italic emphasized text.
475 Example: (_/*word*/_)."
476   :group 'gnus-article-emphasis)
477
478 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
479                                       '((t (:strikethru t)))
480                                     '((t (:strike-through t))))
481   "Face used for displaying strike-through text (-word-)."
482   :group 'gnus-article-emphasis)
483
484 (defface gnus-emphasis-highlight-words
485   '((t (:background "black" :foreground "yellow")))
486   "Face used for displaying highlighted words."
487   :group 'gnus-article-emphasis)
488
489 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
490   "Format for display of Date headers in article bodies.
491 See `format-time-string' for the possible values.
492
493 The variable can also be function, which should return a complete Date
494 header.  The function is called with one argument, the time, which can
495 be fed to `format-time-string'."
496   :type '(choice string function)
497   :link '(custom-manual "(gnus)Article Date")
498   :group 'gnus-article-washing)
499
500 (defcustom gnus-save-all-headers t
501   "*If non-nil, don't remove any headers before saving."
502   :group 'gnus-article-saving
503   :type 'boolean)
504
505 (defcustom gnus-prompt-before-saving 'always
506   "*This variable says how much prompting is to be done when saving articles.
507 If it is nil, no prompting will be done, and the articles will be
508 saved to the default files.  If this variable is `always', each and
509 every article that is saved will be preceded by a prompt, even when
510 saving large batches of articles.  If this variable is neither nil not
511 `always', there the user will be prompted once for a file name for
512 each invocation of the saving commands."
513   :group 'gnus-article-saving
514   :type '(choice (item always)
515                  (item :tag "never" nil)
516                  (sexp :tag "once" :format "%t\n" :value t)))
517
518 (defcustom gnus-saved-headers gnus-visible-headers
519   "Headers to keep if `gnus-save-all-headers' is nil.
520 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
521 If that variable is nil, however, all headers that match this regexp
522 will be kept while the rest will be deleted before saving."
523   :group 'gnus-article-saving
524   :type 'regexp)
525
526 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
527   "A function to save articles in your favourite format.
528 The function must be interactively callable (in other words, it must
529 be an Emacs command).
530
531 Gnus provides the following functions:
532
533 * gnus-summary-save-in-rmail (Rmail format)
534 * gnus-summary-save-in-mail (Unix mail format)
535 * gnus-summary-save-in-folder (MH folder)
536 * gnus-summary-save-in-file (article format)
537 * gnus-summary-save-body-in-file (article body)
538 * gnus-summary-save-in-vm (use VM's folder format)
539 * gnus-summary-write-to-file (article format -- overwrite)."
540   :group 'gnus-article-saving
541   :type '(radio (function-item gnus-summary-save-in-rmail)
542                 (function-item gnus-summary-save-in-mail)
543                 (function-item gnus-summary-save-in-folder)
544                 (function-item gnus-summary-save-in-file)
545                 (function-item gnus-summary-save-body-in-file)
546                 (function-item gnus-summary-save-in-vm)
547                 (function-item gnus-summary-write-to-file)
548                 (function)))
549
550 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
551   "A function generating a file name to save articles in Rmail format.
552 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
553   :group 'gnus-article-saving
554   :type 'function)
555
556 (defcustom gnus-mail-save-name 'gnus-plain-save-name
557   "A function generating a file name to save articles in Unix mail format.
558 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
559   :group 'gnus-article-saving
560   :type 'function)
561
562 (defcustom gnus-folder-save-name 'gnus-folder-save-name
563   "A function generating a file name to save articles in MH folder.
564 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
565   :group 'gnus-article-saving
566   :type 'function)
567
568 (defcustom gnus-file-save-name 'gnus-numeric-save-name
569   "A function generating a file name to save articles in article format.
570 The function is called with NEWSGROUP, HEADERS, and optional
571 LAST-FILE."
572   :group 'gnus-article-saving
573   :type 'function)
574
575 (defcustom gnus-split-methods
576   '((gnus-article-archive-name)
577     (gnus-article-nndoc-name))
578   "*Variable used to suggest where articles are to be saved.
579 For instance, if you would like to save articles related to Gnus in
580 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
581 you could set this variable to something like:
582
583  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
584    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
585
586 This variable is an alist where the key is the match and the
587 value is a list of possible files to save in if the match is
588 non-nil.
589
590 If the match is a string, it is used as a regexp match on the
591 article.  If the match is a symbol, that symbol will be funcalled
592 from the buffer of the article to be saved with the newsgroup as
593 the parameter.  If it is a list, it will be evaled in the same
594 buffer.
595
596 If this form or function returns a string, this string will be
597 used as a possible file name; and if it returns a non-nil list,
598 that list will be used as possible file names."
599   :group 'gnus-article-saving
600   :type '(repeat (choice (list :value (fun) function)
601                          (cons :value ("" "") regexp (repeat string))
602                          (sexp :value nil))))
603
604 (defcustom gnus-page-delimiter "^\^L"
605   "*Regexp describing what to use as article page delimiters.
606 The default value is \"^\^L\", which is a form linefeed at the
607 beginning of a line."
608   :type 'regexp
609   :group 'gnus-article-various)
610
611 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
612   "*The format specification for the article mode line.
613 See `gnus-summary-mode-line-format' for a closer description.
614
615 The following additional specs are available:
616
617 %w  The article washing status.
618 %m  The number of MIME parts in the article."
619   :type 'string
620   :group 'gnus-article-various)
621
622 (defcustom gnus-article-mode-hook nil
623   "*A hook for Gnus article mode."
624   :type 'hook
625   :group 'gnus-article-various)
626
627 (when (featurep 'xemacs)
628   ;; Extracted from gnus-xmas-define in order to preserve user settings
629   (when (fboundp 'turn-off-scroll-in-place)
630     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
631   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
632   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
633
634 (defcustom gnus-article-menu-hook nil
635   "*Hook run after the creation of the article mode menu."
636   :type 'hook
637   :group 'gnus-article-various)
638
639 (defcustom gnus-article-prepare-hook nil
640   "*A hook called after an article has been prepared in the article buffer."
641   :type 'hook
642   :group 'gnus-article-various)
643
644 (defcustom gnus-copy-article-ignored-headers nil
645   "List of headers to be removed when copying an article.
646 Each element is a regular expression."
647   :version "23.0" ;; No Gnus
648   :type '(repeat regexp)
649   :group 'gnus-article-various)
650
651 (make-obsolete-variable 'gnus-article-hide-pgp-hook
652                         "This variable is obsolete in Gnus 5.10.")
653
654 (defcustom gnus-article-button-face 'bold
655   "Face used for highlighting buttons in the article buffer.
656
657 An article button is a piece of text that you can activate by pressing
658 `RET' or `mouse-2' above it."
659   :type 'face
660   :group 'gnus-article-buttons)
661
662 (defcustom gnus-article-mouse-face 'highlight
663   "Face used for mouse highlighting in the article buffer.
664
665 Article buttons will be displayed in this face when the cursor is
666 above them."
667   :type 'face
668   :group 'gnus-article-buttons)
669
670 (defcustom gnus-signature-face 'gnus-signature
671   "Face used for highlighting a signature in the article buffer.
672 Obsolete; use the face `gnus-signature' for customizations instead."
673   :type 'face
674   :group 'gnus-article-highlight
675   :group 'gnus-article-signature)
676
677 (defface gnus-signature
678   '((t
679      (:italic t)))
680   "Face used for highlighting a signature in the article buffer."
681   :group 'gnus-article-highlight
682   :group 'gnus-article-signature)
683 ;; backward-compatibility alias
684 (put 'gnus-signature-face 'face-alias 'gnus-signature)
685
686 (defface gnus-header-from
687   '((((class color)
688       (background dark))
689      (:foreground "spring green"))
690     (((class color)
691       (background light))
692      (:foreground "red3"))
693     (t
694      (:italic t)))
695   "Face used for displaying from headers."
696   :group 'gnus-article-headers
697   :group 'gnus-article-highlight)
698 ;; backward-compatibility alias
699 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
700
701 (defface gnus-header-subject
702   '((((class color)
703       (background dark))
704      (:foreground "SeaGreen3"))
705     (((class color)
706       (background light))
707      (:foreground "red4"))
708     (t
709      (:bold t :italic t)))
710   "Face used for displaying subject headers."
711   :group 'gnus-article-headers
712   :group 'gnus-article-highlight)
713 ;; backward-compatibility alias
714 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
715
716 (defface gnus-header-newsgroups
717   '((((class color)
718       (background dark))
719      (:foreground "yellow" :italic t))
720     (((class color)
721       (background light))
722      (:foreground "MidnightBlue" :italic t))
723     (t
724      (:italic t)))
725   "Face used for displaying newsgroups headers.
726 In the default setup this face is only used for crossposted
727 articles."
728   :group 'gnus-article-headers
729   :group 'gnus-article-highlight)
730 ;; backward-compatibility alias
731 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
732
733 (defface gnus-header-name
734   '((((class color)
735       (background dark))
736      (:foreground "SeaGreen"))
737     (((class color)
738       (background light))
739      (:foreground "maroon"))
740     (t
741      (:bold t)))
742   "Face used for displaying header names."
743   :group 'gnus-article-headers
744   :group 'gnus-article-highlight)
745 ;; backward-compatibility alias
746 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
747
748 (defface gnus-header-content
749   '((((class color)
750       (background dark))
751      (:foreground "forest green" :italic t))
752     (((class color)
753       (background light))
754      (:foreground "indianred4" :italic t))
755     (t
756      (:italic t)))  "Face used for displaying header content."
757   :group 'gnus-article-headers
758   :group 'gnus-article-highlight)
759 ;; backward-compatibility alias
760 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
761
762 (defcustom gnus-header-face-alist
763   '(("From" nil gnus-header-from)
764     ("Subject" nil gnus-header-subject)
765     ("Newsgroups:.*," nil gnus-header-newsgroups)
766     ("" gnus-header-name gnus-header-content))
767   "*Controls highlighting of article headers.
768
769 An alist of the form (HEADER NAME CONTENT).
770
771 HEADER is a regular expression which should match the name of a
772 header and NAME and CONTENT are either face names or nil.
773
774 The name of each header field will be displayed using the face
775 specified by the first element in the list where HEADER matches
776 the header name and NAME is non-nil.  Similarly, the content will
777 be displayed by the first non-nil matching CONTENT face."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight
780   :type '(repeat (list (regexp :tag "Header")
781                        (choice :tag "Name"
782                                (item :tag "skip" nil)
783                                (face :value default))
784                        (choice :tag "Content"
785                                (item :tag "skip" nil)
786                                (face :value default)))))
787
788 (defcustom gnus-article-decode-hook
789   '(article-decode-charset article-decode-encoded-words
790                            article-decode-group-name article-decode-idna-rhs)
791   "*Hook run to decode charsets in articles."
792   :group 'gnus-article-headers
793   :type 'hook)
794
795 (defcustom gnus-display-mime-function 'gnus-display-mime
796   "Function to display MIME articles."
797   :group 'gnus-article-mime
798   :type 'function)
799
800 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
801   "Function used to decode headers.")
802
803 (defvar gnus-article-dumbquotes-map
804   '(("\200" "EUR")
805     ("\202" ",")
806     ("\203" "f")
807     ("\204" ",,")
808     ("\205" "...")
809     ("\213" "<")
810     ("\214" "OE")
811     ("\221" "`")
812     ("\222" "'")
813     ("\223" "``")
814     ("\224" "\"")
815     ("\225" "*")
816     ("\226" "-")
817     ("\227" "--")
818     ("\230" "~")
819     ("\231" "(TM)")
820     ("\233" ">")
821     ("\234" "oe")
822     ("\264" "'"))
823   "Table for MS-to-Latin1 translation.")
824
825 (defcustom gnus-ignored-mime-types nil
826   "List of MIME types that should be ignored by Gnus."
827   :version "21.1"
828   :group 'gnus-article-mime
829   :type '(repeat regexp))
830
831 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
832   "List of MIME types that should not be given buttons when rendered inline.
833 See also `gnus-buttonized-mime-types' which may override this variable.
834 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
835   :version "21.1"
836   :group 'gnus-article-mime
837   :type '(repeat regexp))
838
839 (defcustom gnus-buttonized-mime-types nil
840   "List of MIME types that should be given buttons when rendered inline.
841 If set, this variable overrides `gnus-unbuttonized-mime-types'.
842 To see e.g. security buttons you could set this to
843 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
844 this list to display radio buttons that allow you to choose one of two
845 media types those mails include.  See also `mm-discouraged-alternatives'.
846 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
847   :version "22.1"
848   :group 'gnus-article-mime
849   :type '(repeat regexp))
850
851 (defcustom gnus-inhibit-mime-unbuttonizing nil
852   "If non-nil, all MIME parts get buttons.
853 When nil (the default value), then some MIME parts do not get buttons,
854 as described by the variables `gnus-buttonized-mime-types' and
855 `gnus-unbuttonized-mime-types'."
856   :version "22.1"
857   :group 'gnus-article-mime
858   :type 'boolean)
859
860 (defcustom gnus-body-boundary-delimiter "_"
861   "String used to delimit header and body.
862 This variable is used by `gnus-article-treat-body-boundary' which can
863 be controlled by `gnus-treat-body-boundary'."
864   :version "22.1"
865   :group 'gnus-article-various
866   :type '(choice (item :tag "None" :value nil)
867                  string))
868
869 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
870                                   "/usr/share/picons")
871   "Defines the location of the faces database.
872 For information on obtaining this database of pretty pictures, please
873 see http://www.cs.indiana.edu/picons/ftp/index.html"
874   :version "22.1"
875   :type '(repeat directory)
876   :link '(url-link :tag "download"
877                    "http://www.cs.indiana.edu/picons/ftp/index.html")
878   :link '(custom-manual "(gnus)Picons")
879   :group 'gnus-picon)
880
881 (defun gnus-picons-installed-p ()
882   "Say whether picons are installed on your machine."
883   (let ((installed nil))
884     (dolist (database gnus-picon-databases)
885       (when (file-exists-p database)
886         (setq installed t)))
887     installed))
888
889 (defcustom gnus-article-mime-part-function nil
890   "Function called with a MIME handle as the argument.
891 This is meant for people who want to do something automatic based
892 on parts -- for instance, adding Vcard info to a database."
893   :group 'gnus-article-mime
894   :type '(choice (const nil)
895                  function))
896
897 (defcustom gnus-mime-multipart-functions nil
898   "An alist of MIME types to functions to display them."
899   :version "21.1"
900   :group 'gnus-article-mime
901   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
902
903 (defcustom gnus-article-date-lapsed-new-header nil
904   "Whether the X-Sent and Date headers can coexist.
905 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
906 either replace the old \"Date:\" header (if this variable is nil), or
907 be added below it (otherwise)."
908   :version "21.1"
909   :group 'gnus-article-headers
910   :type 'boolean)
911
912 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
913   "Function called with a MIME handle as the argument.
914 This is meant for people who want to view first matched part.
915 For `undisplayed-alternative' (default), the first undisplayed
916 part or alternative part is used.  For `undisplayed', the first
917 undisplayed part is used.  For a function, the first part which
918 the function return t is used.  For nil, the first part is
919 used."
920   :version "21.1"
921   :group 'gnus-article-mime
922   :type '(choice
923           (item :tag "first" :value nil)
924           (item :tag "undisplayed" :value undisplayed)
925           (item :tag "undisplayed or alternative"
926                 :value undisplayed-alternative)
927           (function)))
928
929 (defcustom gnus-mime-action-alist
930   '(("save to file" . gnus-mime-save-part)
931     ("save and strip" . gnus-mime-save-part-and-strip)
932     ("replace with file" . gnus-mime-replace-part)
933     ("delete part" . gnus-mime-delete-part)
934     ("display as text" . gnus-mime-inline-part)
935     ("view the part" . gnus-mime-view-part)
936     ("pipe to command" . gnus-mime-pipe-part)
937     ("toggle display" . gnus-article-press-button)
938     ("toggle display" . gnus-article-view-part-as-charset)
939     ("view as type" . gnus-mime-view-part-as-type)
940     ("view internally" . gnus-mime-view-part-internally)
941     ("view externally" . gnus-mime-view-part-externally))
942   "An alist of actions that run on the MIME attachment."
943   :group 'gnus-article-mime
944   :type '(repeat (cons (string :tag "name")
945                        (function))))
946
947 (defcustom gnus-auto-select-part 1
948   "Advance to next MIME part when deleting or stripping parts.
949
950 When 0, point will be placed on the same part as before.  When
951 positive (negative), move point forward (backwards) this many
952 parts.  When nil, redisplay article."
953   :version "23.0" ;; No Gnus
954   :group 'gnus-article-mime
955   :type '(choice (const nil :tag "Redisplay article.")
956                  (const 1 :tag "Next part.")
957                  (const 0 :tag "Current part.")
958                  integer))
959
960 ;;;
961 ;;; The treatment variables
962 ;;;
963
964 (defvar gnus-part-display-hook nil
965   "Hook called on parts that are to receive treatment.")
966
967 (defvar gnus-article-treat-custom
968   '(choice (const :tag "Off" nil)
969            (const :tag "On" t)
970            (const :tag "Header" head)
971            (const :tag "First" first)
972            (const :tag "Last" last)
973            (integer :tag "Less")
974            (repeat :tag "Groups" regexp)
975            (sexp :tag "Predicate")))
976
977 (defvar gnus-article-treat-head-custom
978   '(choice (const :tag "Off" nil)
979            (const :tag "Header" head)))
980
981 (defvar gnus-article-treat-types '("text/plain")
982   "Parts to treat.")
983
984 (defvar gnus-inhibit-treatment nil
985   "Whether to inhibit treatment.")
986
987 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
988   "Highlight the signature.
989 Valid values are nil, t, `head', `first', `last', an integer or a
990 predicate.  See Info node `(gnus)Customizing Articles'."
991   :group 'gnus-article-treat
992   :link '(custom-manual "(gnus)Customizing Articles")
993   :type gnus-article-treat-custom)
994 (put 'gnus-treat-highlight-signature 'highlight t)
995
996 (defcustom gnus-treat-buttonize 100000
997   "Add buttons.
998 Valid values are nil, t, `head', `first', `last', an integer or a
999 predicate.  See Info node `(gnus)Customizing Articles'."
1000   :group 'gnus-article-treat
1001   :link '(custom-manual "(gnus)Customizing Articles")
1002   :type gnus-article-treat-custom)
1003 (put 'gnus-treat-buttonize 'highlight t)
1004
1005 (defcustom gnus-treat-buttonize-head 'head
1006   "Add buttons to the head.
1007 Valid values are nil, t, `head', `first', `last', an integer or a
1008 predicate.  See Info node `(gnus)Customizing Articles'."
1009   :group 'gnus-article-treat
1010   :link '(custom-manual "(gnus)Customizing Articles")
1011   :type gnus-article-treat-head-custom)
1012 (put 'gnus-treat-buttonize-head 'highlight t)
1013
1014 (defcustom gnus-treat-emphasize
1015   (and (or window-system
1016            (featurep 'xemacs))
1017        50000)
1018   "Emphasize text.
1019 Valid values are nil, t, `head', `first', `last', an integer or a
1020 predicate.  See Info node `(gnus)Customizing Articles'."
1021   :group 'gnus-article-treat
1022   :link '(custom-manual "(gnus)Customizing Articles")
1023   :type gnus-article-treat-custom)
1024 (put 'gnus-treat-emphasize 'highlight t)
1025
1026 (defcustom gnus-treat-strip-cr nil
1027   "Remove carriage returns.
1028 Valid values are nil, t, `head', `first', `last', an integer or a
1029 predicate.  See Info node `(gnus)Customizing Articles'."
1030   :version "22.1"
1031   :group 'gnus-article-treat
1032   :link '(custom-manual "(gnus)Customizing Articles")
1033   :type gnus-article-treat-custom)
1034
1035 (defcustom gnus-treat-unsplit-urls nil
1036   "Remove newlines from within URLs.
1037 Valid values are nil, t, `head', `first', `last', an integer or a
1038 predicate.  See Info node `(gnus)Customizing Articles'."
1039   :version "22.1"
1040   :group 'gnus-article-treat
1041   :link '(custom-manual "(gnus)Customizing Articles")
1042   :type gnus-article-treat-custom)
1043
1044 (defcustom gnus-treat-leading-whitespace nil
1045   "Remove leading whitespace in headers.
1046 Valid values are nil, t, `head', `first', `last', an integer or a
1047 predicate.  See Info node `(gnus)Customizing Articles'."
1048   :version "22.1"
1049   :group 'gnus-article-treat
1050   :link '(custom-manual "(gnus)Customizing Articles")
1051   :type gnus-article-treat-custom)
1052
1053 (defcustom gnus-treat-hide-headers 'head
1054   "Hide headers.
1055 Valid values are nil, t, `head', `first', `last', an integer or a
1056 predicate.  See Info node `(gnus)Customizing Articles'."
1057   :group 'gnus-article-treat
1058   :link '(custom-manual "(gnus)Customizing Articles")
1059   :type gnus-article-treat-head-custom)
1060
1061 (defcustom gnus-treat-hide-boring-headers nil
1062   "Hide boring headers.
1063 Valid values are nil, t, `head', `first', `last', an integer or a
1064 predicate.  See Info node `(gnus)Customizing Articles'."
1065   :group 'gnus-article-treat
1066   :link '(custom-manual "(gnus)Customizing Articles")
1067   :type gnus-article-treat-head-custom)
1068
1069 (defcustom gnus-treat-hide-signature nil
1070   "Hide the signature.
1071 Valid values are nil, t, `head', `first', `last', an integer or a
1072 predicate.  See Info node `(gnus)Customizing Articles'."
1073   :group 'gnus-article-treat
1074   :link '(custom-manual "(gnus)Customizing Articles")
1075   :type gnus-article-treat-custom)
1076
1077 (defcustom gnus-treat-fill-article nil
1078   "Fill the article.
1079 Valid values are nil, t, `head', `first', `last', an integer or a
1080 predicate.  See Info node `(gnus)Customizing Articles'."
1081   :group 'gnus-article-treat
1082   :link '(custom-manual "(gnus)Customizing Articles")
1083   :type gnus-article-treat-custom)
1084
1085 (defcustom gnus-treat-hide-citation nil
1086   "Hide cited text.
1087 Valid values are nil, t, `head', `first', `last', an integer or a
1088 predicate.  See Info node `(gnus)Customizing Articles'."
1089   :group 'gnus-article-treat
1090   :link '(custom-manual "(gnus)Customizing Articles")
1091   :type gnus-article-treat-custom)
1092
1093 (defcustom gnus-treat-hide-citation-maybe nil
1094   "Hide cited text.
1095 Valid values are nil, t, `head', `first', `last', an integer or a
1096 predicate.  See Info node `(gnus)Customizing Articles'."
1097   :group 'gnus-article-treat
1098   :link '(custom-manual "(gnus)Customizing Articles")
1099   :type gnus-article-treat-custom)
1100
1101 (defcustom gnus-treat-strip-list-identifiers 'head
1102   "Strip list identifiers from `gnus-list-identifiers`.
1103 Valid values are nil, t, `head', `first', `last', an integer or a
1104 predicate.  See Info node `(gnus)Customizing Articles'."
1105   :version "21.1"
1106   :group 'gnus-article-treat
1107   :link '(custom-manual "(gnus)Customizing Articles")
1108   :type gnus-article-treat-custom)
1109
1110 (make-obsolete-variable 'gnus-treat-strip-pgp
1111                         "This option is obsolete in Gnus 5.10.")
1112
1113 (defcustom gnus-treat-strip-pem nil
1114   "Strip PEM signatures.
1115 Valid values are nil, t, `head', `first', `last', an integer or a
1116 predicate.  See Info node `(gnus)Customizing Articles'."
1117   :group 'gnus-article-treat
1118   :link '(custom-manual "(gnus)Customizing Articles")
1119   :type gnus-article-treat-custom)
1120
1121 (defcustom gnus-treat-strip-banner t
1122   "Strip banners from articles.
1123 The banner to be stripped is specified in the `banner' group parameter.
1124 Valid values are nil, t, `head', `first', `last', an integer or a
1125 predicate.  See Info node `(gnus)Customizing Articles'."
1126   :group 'gnus-article-treat
1127   :link '(custom-manual "(gnus)Customizing Articles")
1128   :type gnus-article-treat-custom)
1129
1130 (defcustom gnus-treat-highlight-headers 'head
1131   "Highlight the headers.
1132 Valid values are nil, t, `head', `first', `last', an integer or a
1133 predicate.  See Info node `(gnus)Customizing Articles'."
1134   :group 'gnus-article-treat
1135   :link '(custom-manual "(gnus)Customizing Articles")
1136   :type gnus-article-treat-head-custom)
1137 (put 'gnus-treat-highlight-headers 'highlight t)
1138
1139 (defcustom gnus-treat-highlight-citation t
1140   "Highlight cited text.
1141 Valid values are nil, t, `head', `first', `last', an integer or a
1142 predicate.  See Info node `(gnus)Customizing Articles'."
1143   :group 'gnus-article-treat
1144   :link '(custom-manual "(gnus)Customizing Articles")
1145   :type gnus-article-treat-custom)
1146 (put 'gnus-treat-highlight-citation 'highlight t)
1147
1148 (defcustom gnus-treat-date-ut nil
1149   "Display the Date in UT (GMT).
1150 Valid values are nil, t, `head', `first', `last', an integer or a
1151 predicate.  See Info node `(gnus)Customizing Articles'."
1152   :group 'gnus-article-treat
1153   :link '(custom-manual "(gnus)Customizing Articles")
1154   :type gnus-article-treat-head-custom)
1155
1156 (defcustom gnus-treat-date-local nil
1157   "Display the Date in the local timezone.
1158 Valid values are nil, t, `head', `first', `last', an integer or a
1159 predicate.  See Info node `(gnus)Customizing Articles'."
1160   :group 'gnus-article-treat
1161   :link '(custom-manual "(gnus)Customizing Articles")
1162   :type gnus-article-treat-head-custom)
1163
1164 (defcustom gnus-treat-date-english nil
1165   "Display the Date in a format that can be read aloud in English.
1166 Valid values are nil, t, `head', `first', `last', an integer or a
1167 predicate.  See Info node `(gnus)Customizing Articles'."
1168   :version "22.1"
1169   :group 'gnus-article-treat
1170   :link '(custom-manual "(gnus)Customizing Articles")
1171   :type gnus-article-treat-head-custom)
1172
1173 (defcustom gnus-treat-date-lapsed nil
1174   "Display the Date header in a way that says how much time has elapsed.
1175 Valid values are nil, t, `head', `first', `last', an integer or a
1176 predicate.  See Info node `(gnus)Customizing Articles'."
1177   :group 'gnus-article-treat
1178   :link '(custom-manual "(gnus)Customizing Articles")
1179   :type gnus-article-treat-head-custom)
1180
1181 (defcustom gnus-treat-date-original nil
1182   "Display the date in the original timezone.
1183 Valid values are nil, t, `head', `first', `last', an integer or a
1184 predicate.  See Info node `(gnus)Customizing Articles'."
1185   :group 'gnus-article-treat
1186   :link '(custom-manual "(gnus)Customizing Articles")
1187   :type gnus-article-treat-head-custom)
1188
1189 (defcustom gnus-treat-date-iso8601 nil
1190   "Display the date in the ISO8601 format.
1191 Valid values are nil, t, `head', `first', `last', an integer or a
1192 predicate.  See Info node `(gnus)Customizing Articles'."
1193   :version "21.1"
1194   :group 'gnus-article-treat
1195   :link '(custom-manual "(gnus)Customizing Articles")
1196   :type gnus-article-treat-head-custom)
1197
1198 (defcustom gnus-treat-date-user-defined nil
1199   "Display the date in a user-defined format.
1200 The format is defined by the `gnus-article-time-format' variable.
1201 Valid values are nil, t, `head', `first', `last', an integer or a
1202 predicate.  See Info node `(gnus)Customizing Articles'."
1203   :group 'gnus-article-treat
1204   :link '(custom-manual "(gnus)Customizing Articles")
1205   :type gnus-article-treat-head-custom)
1206
1207 (defcustom gnus-treat-strip-headers-in-body t
1208   "Strip the X-No-Archive header line from the beginning of the body.
1209 Valid values are nil, t, `head', `first', `last', an integer or a
1210 predicate.  See Info node `(gnus)Customizing Articles'."
1211   :version "21.1"
1212   :group 'gnus-article-treat
1213   :link '(custom-manual "(gnus)Customizing Articles")
1214   :type gnus-article-treat-custom)
1215
1216 (defcustom gnus-treat-strip-trailing-blank-lines nil
1217   "Strip trailing blank lines.
1218 Valid values are nil, t, `head', `first', `last', an integer or a
1219 predicate.  See Info node `(gnus)Customizing Articles'.
1220
1221 When set to t, it also strips trailing blanks in all MIME parts.
1222 Consider to use `last' instead."
1223   :group 'gnus-article-treat
1224   :link '(custom-manual "(gnus)Customizing Articles")
1225   :type gnus-article-treat-custom)
1226
1227 (defcustom gnus-treat-strip-leading-blank-lines nil
1228   "Strip leading blank lines.
1229 Valid values are nil, t, `head', `first', `last', an integer or a
1230 predicate.  See Info node `(gnus)Customizing Articles'.
1231
1232 When set to t, it also strips trailing blanks in all MIME parts."
1233   :group 'gnus-article-treat
1234   :link '(custom-manual "(gnus)Customizing Articles")
1235   :type gnus-article-treat-custom)
1236
1237 (defcustom gnus-treat-strip-multiple-blank-lines nil
1238   "Strip multiple blank lines.
1239 Valid values are nil, t, `head', `first', `last', an integer or a
1240 predicate.  See Info node `(gnus)Customizing Articles'."
1241   :group 'gnus-article-treat
1242   :link '(custom-manual "(gnus)Customizing Articles")
1243   :type gnus-article-treat-custom)
1244
1245 (defcustom gnus-treat-unfold-headers 'head
1246   "Unfold folded header lines.
1247 Valid values are nil, t, `head', `first', `last', an integer or a
1248 predicate.  See Info node `(gnus)Customizing Articles'."
1249   :version "22.1"
1250   :group 'gnus-article-treat
1251   :link '(custom-manual "(gnus)Customizing Articles")
1252   :type gnus-article-treat-custom)
1253
1254 (defcustom gnus-treat-fold-headers nil
1255   "Fold headers.
1256 Valid values are nil, t, `head', `first', `last', an integer or a
1257 predicate.  See Info node `(gnus)Customizing Articles'."
1258   :version "22.1"
1259   :group 'gnus-article-treat
1260   :link '(custom-manual "(gnus)Customizing Articles")
1261   :type gnus-article-treat-custom)
1262
1263 (defcustom gnus-treat-fold-newsgroups 'head
1264   "Fold the Newsgroups and Followup-To headers.
1265 Valid values are nil, t, `head', `first', `last', an integer or a
1266 predicate.  See Info node `(gnus)Customizing Articles'."
1267   :version "22.1"
1268   :group 'gnus-article-treat
1269   :link '(custom-manual "(gnus)Customizing Articles")
1270   :type gnus-article-treat-custom)
1271
1272 (defcustom gnus-treat-overstrike t
1273   "Treat overstrike highlighting.
1274 Valid values are nil, t, `head', `first', `last', an integer or a
1275 predicate.  See Info node `(gnus)Customizing Articles'."
1276   :group 'gnus-article-treat
1277   :link '(custom-manual "(gnus)Customizing Articles")
1278   :type gnus-article-treat-custom)
1279 (put 'gnus-treat-overstrike 'highlight t)
1280
1281 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1282   "Treat ANSI SGR control sequences.
1283 Valid values are nil, t, `head', `first', `last', an integer or a
1284 predicate.  See Info node `(gnus)Customizing Articles'."
1285   :group 'gnus-article-treat
1286   :link '(custom-manual "(gnus)Customizing Articles")
1287   :type gnus-article-treat-custom)
1288
1289 (make-obsolete-variable 'gnus-treat-display-xface
1290                         'gnus-treat-display-x-face)
1291
1292 (defcustom gnus-treat-display-x-face
1293   (and (not noninteractive)
1294        (or (and (fboundp 'image-type-available-p)
1295                 (image-type-available-p 'xbm)
1296                 (string-match "^0x" (shell-command-to-string "uncompface"))
1297                 (executable-find "icontopbm"))
1298            (and (featurep 'xemacs)
1299                 (featurep 'xface)))
1300        'head)
1301   "Display X-Face headers.
1302 Valid values are nil, t, `head', `first', `last', an integer or a
1303 predicate.  See Info node `(gnus)Customizing Articles' and Info
1304 node `(gnus)X-Face' for details."
1305   :group 'gnus-article-treat
1306   :version "21.1"
1307   :link '(custom-manual "(gnus)Customizing Articles")
1308   :link '(custom-manual "(gnus)X-Face")
1309   :type gnus-article-treat-head-custom
1310   :set (lambda (symbol value)
1311          (set-default
1312           symbol
1313           (cond ((or (boundp symbol) (get symbol 'saved-value))
1314                  value)
1315                 ((boundp 'gnus-treat-display-xface)
1316                  (message "\
1317 ** gnus-treat-display-xface is an obsolete variable;\
1318  use gnus-treat-display-x-face instead")
1319                  (default-value 'gnus-treat-display-xface))
1320                 ((get 'gnus-treat-display-xface 'saved-value)
1321                  (message "\
1322 ** gnus-treat-display-xface is an obsolete variable;\
1323  use gnus-treat-display-x-face instead")
1324                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1325                 (t
1326                  value)))))
1327 (put 'gnus-treat-display-x-face 'highlight t)
1328
1329 (defcustom gnus-treat-display-face
1330   (and (not noninteractive)
1331        (or (and (fboundp 'image-type-available-p)
1332                 (image-type-available-p 'png))
1333            (and (featurep 'xemacs)
1334                 (featurep 'png)))
1335        'head)
1336   "Display Face headers.
1337 Valid values are nil, t, `head', `first', `last', an integer or a
1338 predicate.  See Info node `(gnus)Customizing Articles' and Info
1339 node `(gnus)X-Face' for details."
1340   :group 'gnus-article-treat
1341   :version "22.1"
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :link '(custom-manual "(gnus)X-Face")
1344   :type gnus-article-treat-head-custom)
1345 (put 'gnus-treat-display-face 'highlight t)
1346
1347 (defcustom gnus-treat-display-smileys
1348   (if (or (and (featurep 'xemacs)
1349                (featurep 'xpm))
1350           (and (fboundp 'image-type-available-p)
1351                (image-type-available-p 'pbm)))
1352       t nil)
1353   "Display smileys.
1354 Valid values are nil, t, `head', `first', `last', an integer or a
1355 predicate.  See Info node `(gnus)Customizing Articles' and Info
1356 node `(gnus)Smileys' for details."
1357   :group 'gnus-article-treat
1358   :version "21.1"
1359   :link '(custom-manual "(gnus)Customizing Articles")
1360   :link '(custom-manual "(gnus)Smileys")
1361   :type gnus-article-treat-custom)
1362 (put 'gnus-treat-display-smileys 'highlight t)
1363
1364 (defcustom gnus-treat-from-picon
1365   (if (and (gnus-image-type-available-p 'xpm)
1366            (gnus-picons-installed-p))
1367       'head nil)
1368   "Display picons in the From header.
1369 Valid values are nil, t, `head', `first', `last', an integer or a
1370 predicate.  See Info node `(gnus)Customizing Articles' and Info
1371 node `(gnus)Picons' for details."
1372   :version "22.1"
1373   :group 'gnus-article-treat
1374   :group 'gnus-picon
1375   :link '(custom-manual "(gnus)Customizing Articles")
1376   :link '(custom-manual "(gnus)Picons")
1377   :type gnus-article-treat-head-custom)
1378 (put 'gnus-treat-from-picon 'highlight t)
1379
1380 (defcustom gnus-treat-mail-picon
1381   (if (and (gnus-image-type-available-p 'xpm)
1382            (gnus-picons-installed-p))
1383       'head nil)
1384   "Display picons in To and Cc headers.
1385 Valid values are nil, t, `head', `first', `last', an integer or a
1386 predicate.  See Info node `(gnus)Customizing Articles' and Info
1387 node `(gnus)Picons' for details."
1388   :version "22.1"
1389   :group 'gnus-article-treat
1390   :group 'gnus-picon
1391   :link '(custom-manual "(gnus)Customizing Articles")
1392   :link '(custom-manual "(gnus)Picons")
1393   :type gnus-article-treat-head-custom)
1394 (put 'gnus-treat-mail-picon 'highlight t)
1395
1396 (defcustom gnus-treat-newsgroups-picon
1397   (if (and (gnus-image-type-available-p 'xpm)
1398            (gnus-picons-installed-p))
1399       'head nil)
1400   "Display picons in the Newsgroups and Followup-To headers.
1401 Valid values are nil, t, `head', `first', `last', an integer or a
1402 predicate.  See Info node `(gnus)Customizing Articles' and Info
1403 node `(gnus)Picons' for details."
1404   :version "22.1"
1405   :group 'gnus-article-treat
1406   :group 'gnus-picon
1407   :link '(custom-manual "(gnus)Customizing Articles")
1408   :link '(custom-manual "(gnus)Picons")
1409   :type gnus-article-treat-head-custom)
1410 (put 'gnus-treat-newsgroups-picon 'highlight t)
1411
1412 (defcustom gnus-treat-body-boundary
1413   (if (and (eq window-system 'x)
1414            (or gnus-treat-newsgroups-picon
1415                gnus-treat-mail-picon
1416                gnus-treat-from-picon))
1417       'head nil)
1418   "Draw a boundary at the end of the headers.
1419 Valid values are nil and `head'.
1420 See Info node `(gnus)Customizing Articles' for details."
1421   :version "22.1"
1422   :group 'gnus-article-treat
1423   :link '(custom-manual "(gnus)Customizing Articles")
1424   :type gnus-article-treat-head-custom)
1425
1426 (defcustom gnus-treat-capitalize-sentences nil
1427   "Capitalize sentence-starting words.
1428 Valid values are nil, t, `head', `first', `last', an integer or a
1429 predicate.  See Info node `(gnus)Customizing Articles'."
1430   :version "21.1"
1431   :group 'gnus-article-treat
1432   :link '(custom-manual "(gnus)Customizing Articles")
1433   :type gnus-article-treat-custom)
1434
1435 (defcustom gnus-treat-wash-html nil
1436   "Format as HTML.
1437 Valid values are nil, t, `head', `first', `last', an integer or a
1438 predicate.  See Info node `(gnus)Customizing Articles'."
1439   :version "22.1"
1440   :group 'gnus-article-treat
1441   :link '(custom-manual "(gnus)Customizing Articles")
1442   :type gnus-article-treat-custom)
1443
1444 (defcustom gnus-treat-fill-long-lines nil
1445   "Fill long lines.
1446 Valid values are nil, t, `head', `first', `last', an integer or a
1447 predicate.  See Info node `(gnus)Customizing Articles'."
1448   :group 'gnus-article-treat
1449   :link '(custom-manual "(gnus)Customizing Articles")
1450   :type gnus-article-treat-custom)
1451
1452 (defcustom gnus-treat-play-sounds nil
1453   "Play sounds.
1454 Valid values are nil, t, `head', `first', `last', an integer or a
1455 predicate.  See Info node `(gnus)Customizing Articles'."
1456   :version "21.1"
1457   :group 'gnus-article-treat
1458   :link '(custom-manual "(gnus)Customizing Articles")
1459   :type gnus-article-treat-custom)
1460
1461 (defcustom gnus-treat-translate nil
1462   "Translate articles from one language to another.
1463 Valid values are nil, t, `head', `first', `last', an integer or a
1464 predicate.  See Info node `(gnus)Customizing Articles'."
1465   :version "21.1"
1466   :group 'gnus-article-treat
1467   :link '(custom-manual "(gnus)Customizing Articles")
1468   :type gnus-article-treat-custom)
1469
1470 (defcustom gnus-treat-x-pgp-sig nil
1471   "Verify X-PGP-Sig.
1472 To automatically treat X-PGP-Sig, set it to head.
1473 Valid values are nil, t, `head', `first', `last', an integer or a
1474 predicate.  See Info node `(gnus)Customizing Articles'."
1475   :version "22.1"
1476   :group 'gnus-article-treat
1477   :group 'mime-security
1478   :link '(custom-manual "(gnus)Customizing Articles")
1479   :type gnus-article-treat-custom)
1480
1481 (defvar gnus-article-encrypt-protocol-alist
1482   '(("PGP" . mml2015-self-encrypt)))
1483
1484 ;; Set to nil if more than one protocol added to
1485 ;; gnus-article-encrypt-protocol-alist.
1486 (defcustom gnus-article-encrypt-protocol "PGP"
1487   "The protocol used for encrypt articles.
1488 It is a string, such as \"PGP\". If nil, ask user."
1489   :version "22.1"
1490   :type 'string
1491   :group 'mime-security)
1492
1493 (defvar gnus-article-wash-function nil
1494   "Function used for converting HTML into text.")
1495
1496 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1497                               (mm-coding-system-p 'utf-8)
1498                               (executable-find idna-program))
1499   "Whether IDNA decoding of headers is used when viewing messages.
1500 This requires GNU Libidn, and by default only enabled if it is found."
1501   :version "22.1"
1502   :group 'gnus-article-headers
1503   :type 'boolean)
1504
1505 (defcustom gnus-article-over-scroll nil
1506   "If non-nil, allow scrolling the article buffer even when there no more text."
1507   :version "22.1"
1508   :group 'gnus-article
1509   :type 'boolean)
1510
1511 ;;; Internal variables
1512
1513 (defvar gnus-english-month-names
1514   '("January" "February" "March" "April" "May" "June" "July" "August"
1515     "September" "October" "November" "December"))
1516
1517 (defvar gnus-button-regexp nil)
1518 (defvar gnus-button-marker-list nil)
1519 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1520
1521 (defvar gnus-button-last nil)
1522 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1523
1524 (defvar article-goto-body-goes-to-point-min-p nil)
1525 (defvar gnus-article-wash-types nil)
1526 (defvar gnus-article-emphasis-alist nil)
1527 (defvar gnus-article-image-alist nil)
1528
1529 (defvar gnus-article-mime-handle-alist-1 nil)
1530 (defvar gnus-treatment-function-alist
1531   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1532     (gnus-treat-strip-banner gnus-article-strip-banner)
1533     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1534     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1535     (gnus-treat-buttonize gnus-article-add-buttons)
1536     (gnus-treat-fill-article gnus-article-fill-cited-article)
1537     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1538     (gnus-treat-strip-cr gnus-article-remove-cr)
1539     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1540     (gnus-treat-date-ut gnus-article-date-ut)
1541     (gnus-treat-date-local gnus-article-date-local)
1542     (gnus-treat-date-english gnus-article-date-english)
1543     (gnus-treat-date-original gnus-article-date-original)
1544     (gnus-treat-date-user-defined gnus-article-date-user)
1545     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1546     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1547     (gnus-treat-display-x-face gnus-article-display-x-face)
1548     (gnus-treat-display-face gnus-article-display-face)
1549     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1550     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1551     (gnus-treat-hide-signature gnus-article-hide-signature)
1552     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1553     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1554     (gnus-treat-strip-pem gnus-article-hide-pem)
1555     (gnus-treat-from-picon gnus-treat-from-picon)
1556     (gnus-treat-mail-picon gnus-treat-mail-picon)
1557     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1558     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1559     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1560     (gnus-treat-strip-trailing-blank-lines
1561      gnus-article-remove-trailing-blank-lines)
1562     (gnus-treat-strip-leading-blank-lines
1563      gnus-article-strip-leading-blank-lines)
1564     (gnus-treat-strip-multiple-blank-lines
1565      gnus-article-strip-multiple-blank-lines)
1566     (gnus-treat-overstrike gnus-article-treat-overstrike)
1567     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1568     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1569     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1570     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1571     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1572     (gnus-treat-display-smileys gnus-treat-smiley)
1573     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1574     (gnus-treat-wash-html gnus-article-wash-html)
1575     (gnus-treat-emphasize gnus-article-emphasize)
1576     (gnus-treat-hide-citation gnus-article-hide-citation)
1577     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1578     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1579     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1580     (gnus-treat-play-sounds gnus-earcon-display)))
1581
1582 (defvar gnus-article-mime-handle-alist nil)
1583 (defvar article-lapsed-timer nil)
1584 (defvar gnus-article-current-summary nil)
1585
1586 (defvar gnus-article-mode-syntax-table
1587   (let ((table (copy-syntax-table text-mode-syntax-table)))
1588     ;; This causes the citation match run O(2^n).
1589     ;; (modify-syntax-entry ?- "w" table)
1590     (modify-syntax-entry ?> ")<" table)
1591     (modify-syntax-entry ?< "(>" table)
1592     ;; make M-. in article buffers work for `foo' strings
1593     (modify-syntax-entry ?' " " table)
1594     (modify-syntax-entry ?` " " table)
1595     table)
1596   "Syntax table used in article mode buffers.
1597 Initialized from `text-mode-syntax-table.")
1598
1599 (defvar gnus-save-article-buffer nil)
1600
1601 (defvar gnus-article-mode-line-format-alist
1602   (nconc '((?w (gnus-article-wash-status) ?s)
1603            (?m (gnus-article-mime-part-status) ?s))
1604          gnus-summary-mode-line-format-alist))
1605
1606 (defvar gnus-number-of-articles-to-be-saved nil)
1607
1608 (defvar gnus-inhibit-hiding nil)
1609
1610 (defvar gnus-article-edit-mode nil)
1611
1612 ;;; Macros for dealing with the article buffer.
1613
1614 (defmacro gnus-with-article-headers (&rest forms)
1615   `(save-excursion
1616      (set-buffer gnus-article-buffer)
1617      (save-restriction
1618        (let ((inhibit-read-only t)
1619              (inhibit-point-motion-hooks t)
1620              (case-fold-search t))
1621          (article-narrow-to-head)
1622          ,@forms))))
1623
1624 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1625 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1626
1627 (defmacro gnus-with-article-buffer (&rest forms)
1628   `(save-excursion
1629      (set-buffer gnus-article-buffer)
1630      (let ((inhibit-read-only t))
1631        ,@forms)))
1632
1633 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1634 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1635
1636 (defun gnus-article-goto-header (header)
1637   "Go to HEADER, which is a regular expression."
1638   (re-search-forward (concat "^\\(" header "\\):") nil t))
1639
1640 (defsubst gnus-article-hide-text (b e props)
1641   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1642   (gnus-add-text-properties-when 'article-type nil b e props)
1643   (when (memq 'intangible props)
1644     (put-text-property
1645      (max (1- b) (point-min))
1646      b 'intangible (cddr (memq 'intangible props)))))
1647
1648 (defsubst gnus-article-unhide-text (b e)
1649   "Remove hidden text properties from region between B and E."
1650   (remove-text-properties b e gnus-hidden-properties)
1651   (when (memq 'intangible gnus-hidden-properties)
1652     (put-text-property (max (1- b) (point-min))
1653                        b 'intangible nil)))
1654
1655 (defun gnus-article-hide-text-type (b e type)
1656   "Hide text of TYPE between B and E."
1657   (gnus-add-wash-type type)
1658   (gnus-article-hide-text
1659    b e (cons 'article-type (cons type gnus-hidden-properties))))
1660
1661 (defun gnus-article-unhide-text-type (b e type)
1662   "Unhide text of TYPE between B and E."
1663   (gnus-delete-wash-type type)
1664   (remove-text-properties
1665    b e (cons 'article-type (cons type gnus-hidden-properties)))
1666   (when (memq 'intangible gnus-hidden-properties)
1667     (put-text-property (max (1- b) (point-min))
1668                        b 'intangible nil)))
1669
1670 (defun gnus-article-hide-text-of-type (type)
1671   "Hide text of TYPE in the current buffer."
1672   (save-excursion
1673     (let ((b (point-min))
1674           (e (point-max)))
1675       (while (setq b (text-property-any b e 'article-type type))
1676         (add-text-properties b (incf b) gnus-hidden-properties)))))
1677
1678 (defun gnus-article-delete-text-of-type (type)
1679   "Delete text of TYPE in the current buffer."
1680   (save-excursion
1681     (let ((b (point-min)))
1682       (if (eq type 'multipart)
1683           ;; Remove MIME buttons associated with multipart/alternative parts.
1684           (progn
1685             (goto-char b)
1686             (while (if (get-text-property (point) 'gnus-part)
1687                        (setq b (point))
1688                      (when (setq b (next-single-property-change (point)
1689                                                                 'gnus-part))
1690                        (goto-char b)
1691                        t))
1692               (end-of-line)
1693               (skip-chars-forward "\n")
1694               (when (eq (get-text-property b 'article-type) 'multipart)
1695                 (delete-region b (point)))))
1696         (while (setq b (text-property-any b (point-max) 'article-type type))
1697           (delete-region
1698            b (or (text-property-not-all b (point-max) 'article-type type)
1699                  (point-max))))))))
1700
1701 (defun gnus-article-delete-invisible-text ()
1702   "Delete all invisible text in the current buffer."
1703   (save-excursion
1704     (let ((b (point-min)))
1705       (while (setq b (text-property-any b (point-max) 'invisible t))
1706         (delete-region
1707          b (or (text-property-not-all b (point-max) 'invisible t)
1708                (point-max)))))))
1709
1710 (defun gnus-article-text-type-exists-p (type)
1711   "Say whether any text of type TYPE exists in the buffer."
1712   (text-property-any (point-min) (point-max) 'article-type type))
1713
1714 (defsubst gnus-article-header-rank ()
1715   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1716   (let ((list gnus-sorted-header-list)
1717         (i 1))
1718     (while list
1719       (if (looking-at (car list))
1720           (setq list nil)
1721         (setq list (cdr list))
1722         (incf i)))
1723       i))
1724
1725 (defun article-hide-headers (&optional arg delete)
1726   "Hide unwanted headers and possibly sort them as well."
1727   (interactive)
1728   ;; This function might be inhibited.
1729   (unless gnus-inhibit-hiding
1730     (let ((inhibit-read-only nil)
1731           (case-fold-search t)
1732           (max (1+ (length gnus-sorted-header-list)))
1733           (inhibit-point-motion-hooks t)
1734           (cur (current-buffer))
1735           ignored visible beg)
1736       (save-excursion
1737         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1738         ;; group parameters, so we should go to the summary buffer.
1739         (when (prog1
1740                   (condition-case nil
1741                       (progn (set-buffer gnus-summary-buffer) t)
1742                     (error nil))
1743                 (setq ignored (when (not gnus-visible-headers)
1744                                 (cond ((stringp gnus-ignored-headers)
1745                                        gnus-ignored-headers)
1746                                       ((listp gnus-ignored-headers)
1747                                        (mapconcat 'identity
1748                                                   gnus-ignored-headers
1749                                                   "\\|"))))
1750                       visible (cond ((stringp gnus-visible-headers)
1751                                      gnus-visible-headers)
1752                                     ((and gnus-visible-headers
1753                                           (listp gnus-visible-headers))
1754                                      (mapconcat 'identity
1755                                                 gnus-visible-headers
1756                                                 "\\|")))))
1757           (set-buffer cur))
1758         (save-restriction
1759           ;; First we narrow to just the headers.
1760           (article-narrow-to-head)
1761           ;; Hide any "From " lines at the beginning of (mail) articles.
1762           (while (looking-at "From ")
1763             (forward-line 1))
1764           (unless (bobp)
1765             (delete-region (point-min) (point)))
1766           ;; Then treat the rest of the header lines.
1767           ;; Then we use the two regular expressions
1768           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1769           ;; select which header lines is to remain visible in the
1770           ;; article buffer.
1771           (while (re-search-forward "^[^ \t:]*:" nil t)
1772             (beginning-of-line)
1773             ;; Mark the rank of the header.
1774             (put-text-property
1775              (point) (1+ (point)) 'message-rank
1776              (if (or (and visible (looking-at visible))
1777                      (and ignored
1778                           (not (looking-at ignored))))
1779                  (gnus-article-header-rank)
1780                (+ 2 max)))
1781             (forward-line 1))
1782           (message-sort-headers-1)
1783           (when (setq beg (text-property-any
1784                            (point-min) (point-max) 'message-rank (+ 2 max)))
1785             ;; We delete the unwanted headers.
1786             (gnus-add-wash-type 'headers)
1787             (add-text-properties (point-min) (+ 5 (point-min))
1788                                  '(article-type headers dummy-invisible t))
1789             (delete-region beg (point-max))))))))
1790
1791 (defun article-hide-boring-headers (&optional arg)
1792   "Toggle hiding of headers that aren't very interesting.
1793 If given a negative prefix, always show; if given a positive prefix,
1794 always hide."
1795   (interactive (gnus-article-hidden-arg))
1796   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1797              (not gnus-show-all-headers))
1798     (save-excursion
1799       (save-restriction
1800         (let ((inhibit-read-only t)
1801               (inhibit-point-motion-hooks t))
1802           (article-narrow-to-head)
1803           (dolist (elem gnus-boring-article-headers)
1804             (goto-char (point-min))
1805             (cond
1806              ;; Hide empty headers.
1807              ((eq elem 'empty)
1808               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1809                 (forward-line -1)
1810                 (gnus-article-hide-text-type
1811                  (point-at-bol)
1812                  (progn
1813                    (end-of-line)
1814                    (if (re-search-forward "^[^ \t]" nil t)
1815                        (match-beginning 0)
1816                      (point-max)))
1817                  'boring-headers)))
1818              ;; Hide boring Newsgroups header.
1819              ((eq elem 'newsgroups)
1820               (when (gnus-string-equal
1821                      (gnus-fetch-field "newsgroups")
1822                      (gnus-group-real-name
1823                       (if (boundp 'gnus-newsgroup-name)
1824                           gnus-newsgroup-name
1825                         "")))
1826                 (gnus-article-hide-header "newsgroups")))
1827              ((eq elem 'to-address)
1828               (let ((to (message-fetch-field "to"))
1829                     (to-address
1830                      (gnus-parameter-to-address
1831                       (if (boundp 'gnus-newsgroup-name)
1832                           gnus-newsgroup-name ""))))
1833                 (when (and to to-address
1834                            (ignore-errors
1835                              (gnus-string-equal
1836                               ;; only one address in To
1837                               (nth 1 (mail-extract-address-components to))
1838                               to-address)))
1839                   (gnus-article-hide-header "to"))))
1840              ((eq elem 'to-list)
1841               (let ((to (message-fetch-field "to"))
1842                     (to-list
1843                      (gnus-parameter-to-list
1844                       (if (boundp 'gnus-newsgroup-name)
1845                           gnus-newsgroup-name ""))))
1846                 (when (and to to-list
1847                            (ignore-errors
1848                              (gnus-string-equal
1849                               ;; only one address in To
1850                               (nth 1 (mail-extract-address-components to))
1851                               to-list)))
1852                   (gnus-article-hide-header "to"))))
1853              ((eq elem 'cc-list)
1854               (let ((cc (message-fetch-field "cc"))
1855                     (to-list
1856                      (gnus-parameter-to-list
1857                       (if (boundp 'gnus-newsgroup-name)
1858                           gnus-newsgroup-name ""))))
1859                 (when (and cc to-list
1860                            (ignore-errors
1861                              (gnus-string-equal
1862                               ;; only one address in CC
1863                               (nth 1 (mail-extract-address-components cc))
1864                               to-list)))
1865                   (gnus-article-hide-header "cc"))))
1866              ((eq elem 'followup-to)
1867               (when (gnus-string-equal
1868                      (message-fetch-field "followup-to")
1869                      (message-fetch-field "newsgroups"))
1870                 (gnus-article-hide-header "followup-to")))
1871              ((eq elem 'reply-to)
1872               (if (gnus-group-find-parameter
1873                    gnus-newsgroup-name 'broken-reply-to)
1874                   (gnus-article-hide-header "reply-to")
1875                 (let ((from (message-fetch-field "from"))
1876                       (reply-to (message-fetch-field "reply-to")))
1877                   (when
1878                       (and
1879                        from reply-to
1880                        (ignore-errors
1881                          (equal
1882                           (sort (mapcar
1883                                  (lambda (x) (downcase (cadr x)))
1884                                  (mail-extract-address-components from t))
1885                                 'string<)
1886                           (sort (mapcar
1887                                  (lambda (x) (downcase (cadr x)))
1888                                  (mail-extract-address-components reply-to t))
1889                                 'string<))))
1890                     (gnus-article-hide-header "reply-to")))))
1891              ((eq elem 'date)
1892               (let ((date (message-fetch-field "date")))
1893                 (when (and date
1894                            (< (days-between (current-time-string) date)
1895                               4))
1896                   (gnus-article-hide-header "date"))))
1897              ((eq elem 'long-to)
1898               (let ((to (message-fetch-field "to"))
1899                     (cc (message-fetch-field "cc")))
1900                 (when (> (length to) 1024)
1901                   (gnus-article-hide-header "to"))
1902                 (when (> (length cc) 1024)
1903                   (gnus-article-hide-header "cc"))))
1904              ((eq elem 'many-to)
1905               (let ((to-count 0)
1906                     (cc-count 0))
1907                 (goto-char (point-min))
1908                 (while (re-search-forward "^to:" nil t)
1909                   (setq to-count (1+ to-count)))
1910                 (when (> to-count 1)
1911                   (while (> to-count 0)
1912                     (goto-char (point-min))
1913                     (save-restriction
1914                       (re-search-forward "^to:" nil nil to-count)
1915                       (forward-line -1)
1916                       (narrow-to-region (point) (point-max))
1917                       (gnus-article-hide-header "to"))
1918                     (setq to-count (1- to-count))))
1919                 (goto-char (point-min))
1920                 (while (re-search-forward "^cc:" nil t)
1921                   (setq cc-count (1+ cc-count)))
1922                 (when (> cc-count 1)
1923                   (while (> cc-count 0)
1924                     (goto-char (point-min))
1925                     (save-restriction
1926                       (re-search-forward "^cc:" nil nil cc-count)
1927                       (forward-line -1)
1928                       (narrow-to-region (point) (point-max))
1929                       (gnus-article-hide-header "cc"))
1930                     (setq cc-count (1- cc-count)))))))))))))
1931
1932 (defun gnus-article-hide-header (header)
1933   (save-excursion
1934     (goto-char (point-min))
1935     (when (re-search-forward (concat "^" header ":") nil t)
1936       (gnus-article-hide-text-type
1937        (point-at-bol)
1938        (progn
1939          (end-of-line)
1940          (if (re-search-forward "^[^ \t]" nil t)
1941              (match-beginning 0)
1942            (point-max)))
1943        'boring-headers))))
1944
1945 (defvar gnus-article-normalized-header-length 40
1946   "Length of normalized headers.")
1947
1948 (defun article-normalize-headers ()
1949   "Make all header lines 40 characters long."
1950   (interactive)
1951   (let ((inhibit-read-only t)
1952         column)
1953     (save-excursion
1954       (save-restriction
1955         (article-narrow-to-head)
1956         (while (not (eobp))
1957           (cond
1958            ((< (setq column (- (point-at-eol) (point)))
1959                gnus-article-normalized-header-length)
1960             (end-of-line)
1961             (insert (make-string
1962                      (- gnus-article-normalized-header-length column)
1963                      ? )))
1964            ((> column gnus-article-normalized-header-length)
1965             (gnus-put-text-property
1966              (progn
1967                (forward-char gnus-article-normalized-header-length)
1968                (point))
1969              (point-at-eol)
1970              'invisible t))
1971            (t
1972             ;; Do nothing.
1973             ))
1974           (forward-line 1))))))
1975
1976 (defun article-treat-dumbquotes ()
1977   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
1978 Note that this function guesses whether a character is a sm*rtq**t* or
1979 not, so it should only be used interactively.
1980
1981 Sm*rtq**t*s are M****s***'s unilateral extension to the
1982 iso-8859-1 character map in an attempt to provide more quoting
1983 characters.  If you see something like \\222 or \\264 where
1984 you're expecting some kind of apostrophe or quotation mark, then
1985 try this wash."
1986   (interactive)
1987   (article-translate-strings gnus-article-dumbquotes-map))
1988
1989 (defun article-translate-characters (from to)
1990   "Translate all characters in the body of the article according to FROM and TO.
1991 FROM is a string of characters to translate from; to is a string of
1992 characters to translate to."
1993   (save-excursion
1994     (when (article-goto-body)
1995       (let ((inhibit-read-only t)
1996             (x (make-string 225 ?x))
1997             (i -1))
1998         (while (< (incf i) (length x))
1999           (aset x i i))
2000         (setq i 0)
2001         (while (< i (length from))
2002           (aset x (aref from i) (aref to i))
2003           (incf i))
2004         (translate-region (point) (point-max) x)))))
2005
2006 (defun article-translate-strings (map)
2007   "Translate all string in the body of the article according to MAP.
2008 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2009   (save-excursion
2010     (when (article-goto-body)
2011       (let ((inhibit-read-only t))
2012         (dolist (elem map)
2013           (save-excursion
2014             (while (search-forward (car elem) nil t)
2015               (replace-match (cadr elem)))))))))
2016
2017 (defun article-treat-overstrike ()
2018   "Translate overstrikes into bold text."
2019   (interactive)
2020   (save-excursion
2021     (when (article-goto-body)
2022       (let ((inhibit-read-only t))
2023         (while (search-forward "\b" nil t)
2024           (let ((next (char-after))
2025                 (previous (char-after (- (point) 2))))
2026             ;; We do the boldification/underlining by hiding the
2027             ;; overstrikes and putting the proper text property
2028             ;; on the letters.
2029             (cond
2030              ((eq next previous)
2031               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2032               (put-text-property (point) (1+ (point)) 'face 'bold))
2033              ((eq next ?_)
2034               (gnus-article-hide-text-type
2035                (1- (point)) (1+ (point)) 'overstrike)
2036               (put-text-property
2037                (- (point) 2) (1- (point)) 'face 'underline))
2038              ((eq previous ?_)
2039               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2040               (put-text-property
2041                (point) (1+ (point)) 'face 'underline)))))))))
2042
2043 (defun article-treat-ansi-sequences ()
2044   "Translate ANSI SGR control sequences into overlays or extents."
2045   (interactive)
2046   (save-excursion
2047     (when (article-goto-body)
2048       (let ((inhibit-read-only t))
2049         (ansi-color-apply-on-region (point) (point-max))))))
2050
2051 (defun gnus-article-treat-unfold-headers ()
2052   "Unfold folded message headers.
2053 Only the headers that fit into the current window width will be
2054 unfolded."
2055   (interactive)
2056   (gnus-with-article-headers
2057     (let (length)
2058       (while (not (eobp))
2059         (save-restriction
2060           (mail-header-narrow-to-field)
2061           (let ((header (buffer-string)))
2062             (with-temp-buffer
2063               (insert header)
2064               (goto-char (point-min))
2065               (while (re-search-forward "\n[\t ]" nil t)
2066                 (replace-match " " t t)))
2067             (setq length (- (point-max) (point-min) 1)))
2068           (when (< length (window-width))
2069             (while (re-search-forward "\n[\t ]" nil t)
2070               (replace-match " " t t)))
2071           (goto-char (point-max)))))))
2072
2073 (defun gnus-article-treat-fold-headers ()
2074   "Fold message headers."
2075   (interactive)
2076   (gnus-with-article-headers
2077     (while (not (eobp))
2078       (save-restriction
2079         (mail-header-narrow-to-field)
2080         (mail-header-fold-field)
2081         (goto-char (point-max))))))
2082
2083 (defun gnus-treat-smiley ()
2084   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2085   (interactive)
2086   (gnus-with-article-buffer
2087     (if (memq 'smiley gnus-article-wash-types)
2088         (gnus-delete-images 'smiley)
2089       (article-goto-body)
2090       (let ((images (smiley-region (point) (point-max))))
2091         (when images
2092           (gnus-add-wash-type 'smiley)
2093           (dolist (image images)
2094             (gnus-add-image 'smiley image)))))))
2095
2096 (defun gnus-article-remove-images ()
2097   "Remove all images from the article buffer."
2098   (interactive)
2099   (gnus-with-article-buffer
2100     (dolist (elem gnus-article-image-alist)
2101       (gnus-delete-images (car elem)))))
2102
2103 (defun gnus-article-treat-fold-newsgroups ()
2104   "Unfold folded message headers.
2105 Only the headers that fit into the current window width will be
2106 unfolded."
2107   (interactive)
2108   (gnus-with-article-headers
2109     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2110       (save-restriction
2111         (mail-header-narrow-to-field)
2112         (while (re-search-forward ", *" nil t)
2113           (replace-match ", " t t))
2114         (mail-header-fold-field)
2115         (goto-char (point-max))))))
2116
2117 (defun gnus-article-treat-body-boundary ()
2118   "Place a boundary line at the end of the headers."
2119   (interactive)
2120   (when (and gnus-body-boundary-delimiter
2121              (> (length gnus-body-boundary-delimiter) 0))
2122     (gnus-with-article-headers
2123       (goto-char (point-max))
2124       (let ((start (point)))
2125         (insert "X-Boundary: ")
2126         (gnus-add-text-properties start (point) '(invisible t intangible t))
2127         (insert (let (str)
2128                   (while (>= (1- (window-width)) (length str))
2129                     (setq str (concat str gnus-body-boundary-delimiter)))
2130                   (substring str 0 (1- (window-width))))
2131                 "\n")
2132         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2133
2134 (defun article-fill-long-lines ()
2135   "Fill lines that are wider than the window width."
2136   (interactive)
2137   (save-excursion
2138     (let ((inhibit-read-only t)
2139           (width (window-width (get-buffer-window (current-buffer)))))
2140       (save-restriction
2141         (article-goto-body)
2142         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2143           (while (not (eobp))
2144             (end-of-line)
2145             (when (>= (current-column) (min fill-column width))
2146               (narrow-to-region (min (1+ (point)) (point-max))
2147                                 (point-at-bol))
2148               (let ((goback (point-marker)))
2149                 (fill-paragraph nil)
2150                 (goto-char (marker-position goback)))
2151               (widen))
2152             (forward-line 1)))))))
2153
2154 (defun article-capitalize-sentences ()
2155   "Capitalize the first word in each sentence."
2156   (interactive)
2157   (save-excursion
2158     (let ((inhibit-read-only t)
2159           (paragraph-start "^[\n\^L]"))
2160       (article-goto-body)
2161       (while (not (eobp))
2162         (capitalize-word 1)
2163         (forward-sentence)))))
2164
2165 (defun article-remove-cr ()
2166   "Remove trailing CRs and then translate remaining CRs into LFs."
2167   (interactive)
2168   (save-excursion
2169     (let ((inhibit-read-only t))
2170       (goto-char (point-min))
2171       (while (re-search-forward "\r+$" nil t)
2172         (replace-match "" t t))
2173       (goto-char (point-min))
2174       (while (search-forward "\r" nil t)
2175         (replace-match "\n" t t)))))
2176
2177 (defun article-remove-trailing-blank-lines ()
2178   "Remove all trailing blank lines from the article."
2179   (interactive)
2180   (save-excursion
2181     (let ((inhibit-read-only t))
2182       (goto-char (point-max))
2183       (delete-region
2184        (point)
2185        (progn
2186          (while (and (not (bobp))
2187                      (looking-at "^[ \t]*$")
2188                      (not (gnus-annotation-in-region-p
2189                            (point) (point-at-eol))))
2190            (forward-line -1))
2191          (forward-line 1)
2192          (point))))))
2193
2194 (eval-when-compile
2195   (defvar gnus-face-properties-alist))
2196
2197 (defun article-display-face ()
2198   "Display any Face headers in the header."
2199   (interactive)
2200   (let ((wash-face-p buffer-read-only))
2201     (gnus-with-article-headers
2202       ;; When displaying parts, this function can be called several times on
2203       ;; the same article, without any intended toggle semantic (as typing `W
2204       ;; D d' would have). So face deletion must occur only when we come from
2205       ;; an interactive command, that is when the *Article* buffer is
2206       ;; read-only.
2207       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2208           (gnus-delete-images 'face)
2209         (let (face faces from)
2210           (save-current-buffer
2211             (when (and wash-face-p
2212                        (gnus-buffer-live-p gnus-original-article-buffer)
2213                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2214               (set-buffer gnus-original-article-buffer))
2215             (save-restriction
2216               (mail-narrow-to-head)
2217               (while (gnus-article-goto-header "Face")
2218                 (push (mail-header-field-value) faces))))
2219           (when faces
2220             (goto-char (point-min))
2221             (let ((from (gnus-article-goto-header "from"))
2222                   png image)
2223               (unless from
2224                 (insert "From:")
2225                 (setq from (point))
2226                 (insert "[no `from' set]\n"))
2227               (while faces
2228                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2229                   (setq image
2230                         (apply 'gnus-create-image png 'png t
2231                                (cdr (assq 'png gnus-face-properties-alist))))
2232                   (goto-char from)
2233                   (gnus-add-wash-type 'face)
2234                   (gnus-add-image 'face image)
2235                   (gnus-put-image image nil 'face))))))))))
2236
2237 (defun article-display-x-face (&optional force)
2238   "Look for an X-Face header and display it if present."
2239   (interactive (list 'force))
2240   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2241     (gnus-with-article-headers
2242       ;; Delete the old process, if any.
2243       (when (process-status "article-x-face")
2244         (delete-process "article-x-face"))
2245       ;; See the comment in `article-display-face'.
2246       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2247           ;; We have already displayed X-Faces, so we remove them
2248           ;; instead.
2249           (gnus-delete-images 'xface)
2250         ;; Display X-Faces.
2251         (let (x-faces from face)
2252           (save-current-buffer
2253             (when (and wash-face-p
2254                        (gnus-buffer-live-p gnus-original-article-buffer)
2255                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2256               ;; If type `W f', use gnus-original-article-buffer,
2257               ;; otherwise use the current buffer because displaying
2258               ;; RFC822 parts calls this function too.
2259               (set-buffer gnus-original-article-buffer))
2260             (save-restriction
2261               (mail-narrow-to-head)
2262               (while (gnus-article-goto-header "X-Face")
2263                 (push (mail-header-field-value) x-faces))
2264               (setq from (message-fetch-field "from"))))
2265           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2266           ;; single external face.
2267           (when (stringp gnus-article-x-face-command)
2268             (setq x-faces (list (car x-faces))))
2269           (when (and x-faces
2270                      gnus-article-x-face-command
2271                      (or force
2272                          ;; Check whether this face is censored.
2273                          (not gnus-article-x-face-too-ugly)
2274                          (and from
2275                               (not (string-match gnus-article-x-face-too-ugly
2276                                                  from)))))
2277             (while (setq face (pop x-faces))
2278               ;; We display the face.
2279               (cond ((stringp gnus-article-x-face-command)
2280                      ;; The command is a string, so we interpret the command
2281                      ;; as a, well, command, and fork it off.
2282                      (let ((process-connection-type nil))
2283                        (gnus-set-process-query-on-exit-flag
2284                         (start-process
2285                          "article-x-face" nil shell-file-name
2286                          shell-command-switch gnus-article-x-face-command)
2287                         nil)
2288                        (with-temp-buffer
2289                          (insert face)
2290                          (process-send-region "article-x-face"
2291                                               (point-min) (point-max)))
2292                        (process-send-eof "article-x-face")))
2293                     ((functionp gnus-article-x-face-command)
2294                      ;; The command is a lisp function, so we call it.
2295                      (funcall gnus-article-x-face-command face))
2296                     (t
2297                      (error "%s is not a function"
2298                             gnus-article-x-face-command))))))))))
2299
2300 (defun article-decode-mime-words ()
2301   "Decode all MIME-encoded words in the article."
2302   (interactive)
2303   (gnus-with-article-buffer
2304     (let ((inhibit-point-motion-hooks t)
2305           (mail-parse-charset gnus-newsgroup-charset)
2306           (mail-parse-ignored-charsets
2307            (with-current-buffer gnus-summary-buffer
2308              gnus-newsgroup-ignored-charsets)))
2309       (mail-decode-encoded-word-region (point-min) (point-max)))))
2310
2311 (defun article-decode-charset (&optional prompt)
2312   "Decode charset-encoded text in the article.
2313 If PROMPT (the prefix), prompt for a coding system to use."
2314   (interactive "P")
2315   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2316         (inhibit-read-only t)
2317         (mail-parse-charset gnus-newsgroup-charset)
2318         (mail-parse-ignored-charsets
2319          (save-excursion (condition-case nil
2320                              (set-buffer gnus-summary-buffer)
2321                            (error))
2322                          gnus-newsgroup-ignored-charsets))
2323         ct cte ctl charset format)
2324     (save-excursion
2325       (save-restriction
2326         (article-narrow-to-head)
2327         (setq ct (message-fetch-field "Content-Type" t)
2328               cte (message-fetch-field "Content-Transfer-Encoding" t)
2329               ctl (and ct (mail-header-parse-content-type ct))
2330               charset (cond
2331                        (prompt
2332                         (mm-read-coding-system "Charset to decode: "))
2333                        (ctl
2334                         (mail-content-type-get ctl 'charset)))
2335               format (and ctl (mail-content-type-get ctl 'format)))
2336         (when cte
2337           (setq cte (mail-header-strip cte)))
2338         (if (and ctl (not (string-match "/" (car ctl))))
2339             (setq ctl nil))
2340         (goto-char (point-max)))
2341       (forward-line 1)
2342       (save-restriction
2343         (narrow-to-region (point) (point-max))
2344         (when (and (eq mail-parse-charset 'gnus-decoded)
2345                    (eq (mm-body-7-or-8) '8bit))
2346           ;; The text code could have been decoded.
2347           (setq charset mail-parse-charset))
2348         (when (and (or (not ctl)
2349                        (equal (car ctl) "text/plain"))
2350                    (not format)) ;; article with format will decode later.
2351           (mm-decode-body
2352            charset (and cte (intern (downcase
2353                                      (gnus-strip-whitespace cte))))
2354            (car ctl)))))))
2355
2356 (defun article-decode-encoded-words ()
2357   "Remove encoded-word encoding from headers."
2358   (let ((inhibit-point-motion-hooks t)
2359         (mail-parse-charset gnus-newsgroup-charset)
2360         (mail-parse-ignored-charsets
2361          (save-excursion (condition-case nil
2362                              (set-buffer gnus-summary-buffer)
2363                            (error))
2364                          gnus-newsgroup-ignored-charsets))
2365         (inhibit-read-only t))
2366     (save-restriction
2367       (article-narrow-to-head)
2368       (funcall gnus-decode-header-function (point-min) (point-max)))))
2369
2370 (defun article-decode-group-name ()
2371   "Decode group names in `Newsgroups:'."
2372   (let ((inhibit-point-motion-hooks t)
2373         (inhibit-read-only t)
2374         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2375     (when (and (or gnus-group-name-charset-method-alist
2376                    gnus-group-name-charset-group-alist)
2377                (gnus-buffer-live-p gnus-original-article-buffer))
2378       (save-restriction
2379         (article-narrow-to-head)
2380         (with-current-buffer gnus-original-article-buffer
2381           (goto-char (point-min)))
2382         (while (re-search-forward
2383                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2384           (replace-match (save-match-data
2385                            (gnus-decode-newsgroups
2386                             ;; XXX how to use data in article buffer?
2387                             (with-current-buffer gnus-original-article-buffer
2388                               (re-search-forward
2389                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2390                                nil t)
2391                               (match-string 1))
2392                             gnus-newsgroup-name method))
2393                          t t nil 1))
2394         (goto-char (point-min))
2395         (with-current-buffer gnus-original-article-buffer
2396           (goto-char (point-min)))
2397         (while (re-search-forward
2398                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2399           (replace-match (save-match-data
2400                            (gnus-decode-newsgroups
2401                             ;; XXX how to use data in article buffer?
2402                             (with-current-buffer gnus-original-article-buffer
2403                               (re-search-forward
2404                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2405                                nil t)
2406                               (match-string 1))
2407                             gnus-newsgroup-name method))
2408                          t t nil 1))))))
2409
2410 (autoload 'idna-to-unicode "idna")
2411
2412 (defun article-decode-idna-rhs ()
2413   "Decode IDNA strings in RHS in various headers in current buffer.
2414 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2415 Mail-Reply-To: and Mail-Followup-To:."
2416   (when gnus-use-idna
2417     (save-restriction
2418       (let ((inhibit-point-motion-hooks t)
2419             (inhibit-read-only t))
2420         (article-narrow-to-head)
2421         (goto-char (point-min))
2422         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2423           (let (ace unicode)
2424             (when (save-match-data
2425                     (and (setq ace (match-string 1))
2426                          (save-excursion
2427                            (and (re-search-backward "^[^ \t]" nil t)
2428                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2429                          (setq unicode (idna-to-unicode ace))))
2430               (unless (string= ace unicode)
2431                 (replace-match unicode nil nil nil 1)))))))))
2432
2433 (defun article-de-quoted-unreadable (&optional force read-charset)
2434   "Translate a quoted-printable-encoded article.
2435 If FORCE, decode the article whether it is marked as quoted-printable
2436 or not.
2437 If READ-CHARSET, ask for a coding system."
2438   (interactive (list 'force current-prefix-arg))
2439   (save-excursion
2440     (let ((inhibit-read-only t) type charset)
2441       (if (gnus-buffer-live-p gnus-original-article-buffer)
2442           (with-current-buffer gnus-original-article-buffer
2443             (setq type
2444                   (gnus-fetch-field "content-transfer-encoding"))
2445             (let* ((ct (gnus-fetch-field "content-type"))
2446                    (ctl (and ct (mail-header-parse-content-type ct))))
2447               (setq charset (and ctl
2448                                  (mail-content-type-get ctl 'charset)))
2449               (if (stringp charset)
2450                   (setq charset (intern (downcase charset)))))))
2451       (if read-charset
2452           (setq charset (mm-read-coding-system "Charset: " charset)))
2453       (unless charset
2454         (setq charset gnus-newsgroup-charset))
2455       (when (or force
2456                 (and type (let ((case-fold-search t))
2457                             (string-match "quoted-printable" type))))
2458         (article-goto-body)
2459         (quoted-printable-decode-region
2460          (point) (point-max) (mm-charset-to-coding-system charset))))))
2461
2462 (defun article-de-base64-unreadable (&optional force read-charset)
2463   "Translate a base64 article.
2464 If FORCE, decode the article whether it is marked as base64 not.
2465 If READ-CHARSET, ask for a coding system."
2466   (interactive (list 'force current-prefix-arg))
2467   (save-excursion
2468     (let ((inhibit-read-only t) type charset)
2469       (if (gnus-buffer-live-p gnus-original-article-buffer)
2470           (with-current-buffer gnus-original-article-buffer
2471             (setq type
2472                   (gnus-fetch-field "content-transfer-encoding"))
2473             (let* ((ct (gnus-fetch-field "content-type"))
2474                    (ctl (and ct (mail-header-parse-content-type ct))))
2475               (setq charset (and ctl
2476                                  (mail-content-type-get ctl 'charset)))
2477               (if (stringp charset)
2478                   (setq charset (intern (downcase charset)))))))
2479       (if read-charset
2480           (setq charset (mm-read-coding-system "Charset: " charset)))
2481       (unless charset
2482         (setq charset gnus-newsgroup-charset))
2483       (when (or force
2484                 (and type (let ((case-fold-search t))
2485                             (string-match "base64" type))))
2486         (article-goto-body)
2487         (save-restriction
2488           (narrow-to-region (point) (point-max))
2489           (base64-decode-region (point-min) (point-max))
2490           (mm-decode-coding-region
2491            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2492
2493 (eval-when-compile
2494   (require 'rfc1843))
2495
2496 (defun article-decode-HZ ()
2497   "Translate a HZ-encoded article."
2498   (interactive)
2499   (require 'rfc1843)
2500   (save-excursion
2501     (let ((inhibit-read-only t))
2502       (rfc1843-decode-region (point-min) (point-max)))))
2503
2504 (defun article-unsplit-urls ()
2505   "Remove the newlines that some other mailers insert into URLs."
2506   (interactive)
2507   (save-excursion
2508     (let ((inhibit-read-only t))
2509       (goto-char (point-min))
2510       (while (re-search-forward
2511               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2512         (replace-match "\\1\\3" t)))
2513     (when (interactive-p)
2514       (gnus-treat-article nil))))
2515
2516
2517 (defun article-wash-html (&optional read-charset)
2518   "Format an HTML article.
2519 If READ-CHARSET, ask for a coding system.  If it is a number, the
2520 charset defined in `gnus-summary-show-article-charset-alist' is used."
2521   (interactive "P")
2522   (save-excursion
2523     (let ((inhibit-read-only t)
2524           charset)
2525       (if read-charset
2526           (if (or (and (numberp read-charset)
2527                        (setq charset
2528                              (cdr
2529                               (assq read-charset
2530                                     gnus-summary-show-article-charset-alist))))
2531                   (setq charset (mm-read-coding-system "Charset: ")))
2532               (let ((gnus-summary-show-article-charset-alist
2533                      (list (cons 1 charset))))
2534                 (with-current-buffer gnus-summary-buffer
2535                   (gnus-summary-show-article 1)))
2536             (error "No charset is given"))
2537         (when (gnus-buffer-live-p gnus-original-article-buffer)
2538           (with-current-buffer gnus-original-article-buffer
2539             (let* ((ct (gnus-fetch-field "content-type"))
2540                    (ctl (and ct (mail-header-parse-content-type ct))))
2541               (setq charset (and ctl
2542                                  (mail-content-type-get ctl 'charset)))
2543               (when (stringp charset)
2544                 (setq charset (intern (downcase charset)))))))
2545         (unless charset
2546           (setq charset gnus-newsgroup-charset)))
2547       (article-goto-body)
2548       (save-window-excursion
2549         (save-restriction
2550           (narrow-to-region (point) (point-max))
2551           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2552                  (entry (assq func mm-text-html-washer-alist)))
2553             (when entry
2554               (setq func (cdr entry)))
2555             (cond
2556              ((functionp func)
2557               (funcall func))
2558              (t
2559               (apply (car func) (cdr func))))))))))
2560
2561 (defun gnus-article-wash-html-with-w3 ()
2562   "Wash the current buffer with w3."
2563   (mm-setup-w3)
2564   (let ((w3-strict-width (window-width))
2565         (url-standalone-mode t)
2566         (url-gateway-unplugged t)
2567         (w3-honor-stylesheets nil))
2568     (condition-case ()
2569         (w3-region (point-min) (point-max))
2570       (error))))
2571
2572 (defun gnus-article-wash-html-with-w3m ()
2573   "Wash the current buffer with emacs-w3m."
2574   (mm-setup-w3m)
2575   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2576         w3m-force-redisplay)
2577     (w3m-region (point-min) (point-max)))
2578   (when (and mm-inline-text-html-with-w3m-keymap
2579              (boundp 'w3m-minor-mode-map)
2580              w3m-minor-mode-map)
2581     (add-text-properties
2582      (point-min) (point-max)
2583      (list 'keymap w3m-minor-mode-map
2584            ;; Put the mark meaning this part was rendered by emacs-w3m.
2585            'mm-inline-text-html-with-w3m t))))
2586
2587 (eval-when-compile (defvar charset)) ;; Bound by `article-wash-html'.
2588
2589 (defun gnus-article-wash-html-with-w3m-standalone ()
2590   "Wash the current buffer with w3m."
2591   (if (mm-w3m-standalone-supports-m17n-p)
2592       (progn
2593         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2594           ;; The default.
2595           (setq charset 'iso-8859-1))
2596         (let ((coding-system-for-write charset)
2597               (coding-system-for-read charset))
2598           (call-process-region
2599            (point-min) (point-max)
2600            "w3m" t t nil "-dump" "-T" "text/html"
2601            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2602     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2603
2604 (defun gnus-article-browse-html-parts (list)
2605   "View all \"text/html\" parts from LIST.
2606 Recurse into multiparts."
2607   ;; Internal function used by `gnus-article-browse-html-article'.
2608   (let ((showed))
2609     ;; Find and show the html-parts.
2610     (dolist (handle list)
2611       ;; If HTML, show it:
2612       (when (listp handle)
2613         (cond ((and (bufferp (car handle))
2614                     (string-match "text/html" (car (mm-handle-type handle))))
2615                (let ((tmp-file (mm-make-temp-file
2616                                 ;; Do we need to care for 8.3 filenames?
2617                                 "mm-" nil ".html")))
2618                  (mm-save-part-to-file handle tmp-file)
2619                  (browse-url tmp-file)
2620                  (setq showed t)))
2621               ;; If multipart, recurse
2622               ((and (stringp (car handle))
2623                     (string-match "^multipart/" (car handle))
2624                     (setq showed
2625                           (or showed
2626                               (gnus-article-browse-html-parts handle))))))))
2627     showed))
2628
2629 ;; TODO: Key binding; Remove temp files.
2630 (defun gnus-article-browse-html-article ()
2631   "View \"text/html\" parts of the current article with a WWW browser."
2632   (interactive)
2633   (save-window-excursion
2634     ;; Open raw article and select the buffer
2635     (gnus-summary-show-article t)
2636     (gnus-summary-select-article-buffer)
2637     (let ((parts (mm-dissect-buffer t t)))
2638       ;; If singlepart, enforce a list.
2639       (when (and (bufferp (car parts))
2640                  (stringp (car (mm-handle-type parts))))
2641         (setq parts (list parts)))
2642       ;; Process the list
2643       (unless (gnus-article-browse-html-parts parts)
2644         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
2645       (gnus-summary-show-article))))
2646
2647 (defun article-hide-list-identifiers ()
2648   "Remove list identifies from the Subject header.
2649 The `gnus-list-identifiers' variable specifies what to do."
2650   (interactive)
2651   (let ((inhibit-point-motion-hooks t)
2652         (regexp (if (consp gnus-list-identifiers)
2653                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2654                   gnus-list-identifiers))
2655         (inhibit-read-only t))
2656     (when regexp
2657       (save-excursion
2658         (save-restriction
2659           (article-narrow-to-head)
2660           (goto-char (point-min))
2661           (while (re-search-forward
2662                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2663                   nil t)
2664             (delete-region (match-beginning 2) (match-end 0))
2665             (beginning-of-line))
2666           (when (re-search-forward
2667                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2668             (delete-region (match-beginning 1) (match-end 1))))))))
2669
2670 (defun article-hide-pem (&optional arg)
2671   "Toggle hiding of any PEM headers and signatures in the current article.
2672 If given a negative prefix, always show; if given a positive prefix,
2673 always hide."
2674   (interactive (gnus-article-hidden-arg))
2675   (unless (gnus-article-check-hidden-text 'pem arg)
2676     (save-excursion
2677       (let ((inhibit-read-only t) end)
2678         (goto-char (point-min))
2679         ;; Hide the horrendously ugly "header".
2680         (when (and (search-forward
2681                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2682                     nil t)
2683                    (setq end (1+ (match-beginning 0))))
2684           (gnus-add-wash-type 'pem)
2685           (gnus-article-hide-text-type
2686            end
2687            (if (search-forward "\n\n" nil t)
2688                (match-end 0)
2689              (point-max))
2690            'pem)
2691           ;; Hide the trailer as well
2692           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2693                                 nil t)
2694             (gnus-article-hide-text-type
2695              (match-beginning 0) (match-end 0) 'pem)))))))
2696
2697 (defun article-strip-banner ()
2698   "Strip the banners specified by the `banner' group parameter and by
2699 `gnus-article-address-banner-alist'."
2700   (interactive)
2701   (save-excursion
2702     (save-restriction
2703       (let ((inhibit-point-motion-hooks t))
2704         (when (gnus-parameter-banner gnus-newsgroup-name)
2705           (article-really-strip-banner
2706            (gnus-parameter-banner gnus-newsgroup-name)))
2707         (when gnus-article-address-banner-alist
2708           ;; Note that the From header is decoded here, so it is
2709           ;; required that the *-extract-address-components function
2710           ;; supports non-ASCII text.
2711           (let ((from (save-restriction
2712                         (widen)
2713                         (article-narrow-to-head)
2714                         (mail-fetch-field "from"))))
2715             (when (and from
2716                        (setq from
2717                              (cadr (funcall gnus-extract-address-components
2718                                             from))))
2719               (catch 'found
2720                 (dolist (pair gnus-article-address-banner-alist)
2721                   (when (string-match (car pair) from)
2722                     (throw 'found
2723                            (article-really-strip-banner (cdr pair)))))))))))))
2724
2725 (defun article-really-strip-banner (banner)
2726   "Strip the banner specified by the argument."
2727   (save-excursion
2728     (save-restriction
2729       (let ((inhibit-point-motion-hooks t)
2730             (gnus-signature-limit nil)
2731             (inhibit-read-only t))
2732         (article-goto-body)
2733         (cond
2734          ((eq banner 'signature)
2735           (when (gnus-article-narrow-to-signature)
2736             (widen)
2737             (forward-line -1)
2738             (delete-region (point) (point-max))))
2739          ((symbolp banner)
2740           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2741               (while (re-search-forward banner nil t)
2742                 (delete-region (match-beginning 0) (match-end 0)))))
2743          ((stringp banner)
2744           (while (re-search-forward banner nil t)
2745             (delete-region (match-beginning 0) (match-end 0)))))))))
2746
2747 (defun article-babel ()
2748   "Translate article using an online translation service."
2749   (interactive)
2750   (require 'babel)
2751   (gnus-with-article-buffer
2752     (when (article-goto-body)
2753       (let* ((start (point))
2754              (end (point-max))
2755              (orig (buffer-substring start end))
2756              (trans (babel-as-string orig)))
2757         (save-restriction
2758           (narrow-to-region start end)
2759           (delete-region start end)
2760           (insert trans))))))
2761
2762 (defun article-hide-signature (&optional arg)
2763   "Hide the signature in the current article.
2764 If given a negative prefix, always show; if given a positive prefix,
2765 always hide."
2766   (interactive (gnus-article-hidden-arg))
2767   (unless (gnus-article-check-hidden-text 'signature arg)
2768     (save-excursion
2769       (save-restriction
2770         (let ((inhibit-read-only t))
2771           (when (gnus-article-narrow-to-signature)
2772             (gnus-article-hide-text-type
2773              (point-min) (point-max) 'signature))))))
2774   (gnus-set-mode-line 'article))
2775
2776 (defun article-strip-headers-in-body ()
2777   "Strip offensive headers from bodies."
2778   (interactive)
2779   (save-excursion
2780     (article-goto-body)
2781     (let ((case-fold-search t))
2782       (when (looking-at "x-no-archive:")
2783         (gnus-delete-line)))))
2784
2785 (defun article-strip-leading-blank-lines ()
2786   "Remove all blank lines from the beginning of the article."
2787   (interactive)
2788   (save-excursion
2789     (let ((inhibit-point-motion-hooks t)
2790           (inhibit-read-only t))
2791       (when (article-goto-body)
2792         (while (and (not (eobp))
2793                     (looking-at "[ \t]*$"))
2794           (gnus-delete-line))))))
2795
2796 (defun article-narrow-to-head ()
2797   "Narrow the buffer to the head of the message.
2798 Point is left at the beginning of the narrowed-to region."
2799   (narrow-to-region
2800    (goto-char (point-min))
2801    (if (search-forward "\n\n" nil 1)
2802        (1- (point))
2803      (point-max)))
2804   (goto-char (point-min)))
2805
2806 (defun article-goto-body ()
2807   "Place point at the start of the body."
2808   (goto-char (point-min))
2809   (cond
2810    ;; This variable is only bound when dealing with separate
2811    ;; MIME body parts.
2812    (article-goto-body-goes-to-point-min-p
2813     t)
2814    ((search-forward "\n\n" nil t)
2815     t)
2816    (t
2817     (goto-char (point-max))
2818     nil)))
2819
2820 (defun article-strip-multiple-blank-lines ()
2821   "Replace consecutive blank lines with one empty line."
2822   (interactive)
2823   (save-excursion
2824     (let ((inhibit-point-motion-hooks t)
2825           (inhibit-read-only t))
2826       ;; First make all blank lines empty.
2827       (article-goto-body)
2828       (while (re-search-forward "^[ \t]+$" nil t)
2829         (unless (gnus-annotation-in-region-p
2830                  (match-beginning 0) (match-end 0))
2831           (replace-match "" nil t)))
2832       ;; Then replace multiple empty lines with a single empty line.
2833       (article-goto-body)
2834       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2835         (unless (gnus-annotation-in-region-p
2836                  (match-beginning 0) (match-end 0))
2837           (delete-region (match-beginning 1) (match-end 1)))))))
2838
2839 (defun article-strip-leading-space ()
2840   "Remove all white space from the beginning of the lines in the article."
2841   (interactive)
2842   (save-excursion
2843     (let ((inhibit-point-motion-hooks t)
2844           (inhibit-read-only t))
2845       (article-goto-body)
2846       (while (re-search-forward "^[ \t]+" nil t)
2847         (replace-match "" t t)))))
2848
2849 (defun article-strip-trailing-space ()
2850   "Remove all white space from the end of the lines in the article."
2851   (interactive)
2852   (save-excursion
2853     (let ((inhibit-point-motion-hooks t)
2854           (inhibit-read-only t))
2855       (article-goto-body)
2856       (while (re-search-forward "[ \t]+$" nil t)
2857         (replace-match "" t t)))))
2858
2859 (defun article-strip-blank-lines ()
2860   "Strip leading, trailing and multiple blank lines."
2861   (interactive)
2862   (article-strip-leading-blank-lines)
2863   (article-remove-trailing-blank-lines)
2864   (article-strip-multiple-blank-lines))
2865
2866 (defun article-strip-all-blank-lines ()
2867   "Strip all blank lines."
2868   (interactive)
2869   (save-excursion
2870     (let ((inhibit-point-motion-hooks t)
2871           (inhibit-read-only t))
2872       (article-goto-body)
2873       (while (re-search-forward "^[ \t]*\n" nil t)
2874         (replace-match "" t t)))))
2875
2876 (defun gnus-article-narrow-to-signature ()
2877   "Narrow to the signature; return t if a signature is found, else nil."
2878   (let ((inhibit-point-motion-hooks t))
2879     (when (gnus-article-search-signature)
2880       (forward-line 1)
2881       ;; Check whether we have some limits to what we consider
2882       ;; to be a signature.
2883       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2884                       (list gnus-signature-limit)))
2885             limit limited)
2886         (while (setq limit (pop limits))
2887           (if (or (and (integerp limit)
2888                        (< (- (point-max) (point)) limit))
2889                   (and (floatp limit)
2890                        (< (count-lines (point) (point-max)) limit))
2891                   (and (functionp limit)
2892                        (funcall limit))
2893                   (and (stringp limit)
2894                        (not (re-search-forward limit nil t))))
2895               ()                        ; This limit did not succeed.
2896             (setq limited t
2897                   limits nil)))
2898         (unless limited
2899           (narrow-to-region (point) (point-max))
2900           t)))))
2901
2902 (defun gnus-article-search-signature ()
2903   "Search the current buffer for the signature separator.
2904 Put point at the beginning of the signature separator."
2905   (let ((cur (point)))
2906     (goto-char (point-max))
2907     (if (if (stringp gnus-signature-separator)
2908             (re-search-backward gnus-signature-separator nil t)
2909           (let ((seps gnus-signature-separator))
2910             (while (and seps
2911                         (not (re-search-backward (car seps) nil t)))
2912               (pop seps))
2913             seps))
2914         t
2915       (goto-char cur)
2916       nil)))
2917
2918 (defun gnus-article-hidden-arg ()
2919   "Return the current prefix arg as a number, or 0 if no prefix."
2920   (list (if current-prefix-arg
2921             (prefix-numeric-value current-prefix-arg)
2922           0)))
2923
2924 (defun gnus-article-check-hidden-text (type arg)
2925   "Return nil if hiding is necessary.
2926 Arg can be nil or a number.  nil and positive means hide, negative
2927 means show, 0 means toggle."
2928   (save-excursion
2929     (save-restriction
2930       (let ((hide (gnus-article-hidden-text-p type)))
2931         (cond
2932          ((or (null arg)
2933               (> arg 0))
2934           nil)
2935          ((< arg 0)
2936           (gnus-article-show-hidden-text type)
2937           t)
2938          (t
2939           (if (eq hide 'hidden)
2940               (progn
2941                 (gnus-article-show-hidden-text type)
2942                 t)
2943             nil)))))))
2944
2945 (defun gnus-article-hidden-text-p (type)
2946   "Say whether the current buffer contains hidden text of type TYPE."
2947   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2948     (while (and pos
2949                 (not (get-text-property pos 'invisible))
2950                 (not (get-text-property pos 'dummy-invisible)))
2951       (setq pos
2952             (text-property-any (1+ pos) (point-max) 'article-type type)))
2953     (if pos
2954         'hidden
2955       nil)))
2956
2957 (defun gnus-article-show-hidden-text (type &optional dummy)
2958   "Show all hidden text of type TYPE.
2959 Originally it is hide instead of DUMMY."
2960   (let ((inhibit-read-only t)
2961         (inhibit-point-motion-hooks t))
2962     (gnus-remove-text-properties-when
2963      'article-type type
2964      (point-min) (point-max)
2965      (cons 'article-type (cons type
2966                                gnus-hidden-properties)))
2967     (gnus-delete-wash-type type)))
2968
2969 (defconst article-time-units
2970   `((year . ,(* 365.25 24 60 60))
2971     (week . ,(* 7 24 60 60))
2972     (day . ,(* 24 60 60))
2973     (hour . ,(* 60 60))
2974     (minute . 60)
2975     (second . 1))
2976   "Mapping from time units to seconds.")
2977
2978 (defun gnus-article-forward-header ()
2979   "Move point to the start of the next header.
2980 If the current header is a continuation header, this can be several
2981 lines forward."
2982   (let ((ended nil))
2983     (while (not ended)
2984       (forward-line 1)
2985       (if (looking-at "[ \t]+[^ \t]")
2986           (forward-line 1)
2987         (setq ended t)))))
2988
2989 (defun article-date-ut (&optional type highlight)
2990   "Convert DATE date to universal time in the current article.
2991 If TYPE is `local', convert to local time; if it is `lapsed', output
2992 how much time has lapsed since DATE.  For `lapsed', the value of
2993 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2994 should replace the \"Date:\" one, or should be added below it."
2995   (interactive (list 'ut t))
2996   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
2997          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
2998                              tdate-regexp)
2999                             ((eq type 'lapsed)
3000                              "^X-Sent:[ \t]")
3001                             (article-lapsed-timer
3002                              "^Date:[ \t]")
3003                             (t
3004                              tdate-regexp)))
3005          (case-fold-search t)
3006          (inhibit-read-only t)
3007          (inhibit-point-motion-hooks t)
3008          pos date bface eface)
3009     (save-excursion
3010       (save-restriction
3011         (widen)
3012         (goto-char (point-min))
3013         (while (or (setq date (get-text-property (setq pos (point))
3014                                                  'original-date))
3015                    (when (setq pos (next-single-property-change
3016                                     (point) 'original-date))
3017                      (setq date (get-text-property pos 'original-date))
3018                      t))
3019           (narrow-to-region pos (or (text-property-any pos (point-max)
3020                                                        'original-date nil)
3021                                     (point-max)))
3022           (goto-char (point-min))
3023           (when (re-search-forward tdate-regexp nil t)
3024             (setq bface (get-text-property (point-at-bol) 'face)
3025                   eface (get-text-property (1- (point-at-eol)) 'face)))
3026           (goto-char (point-min))
3027           (setq pos nil)
3028           ;; Delete any old Date headers.
3029           (while (re-search-forward date-regexp nil t)
3030             (if pos
3031                 (delete-region (point-at-bol) (progn
3032                                                 (gnus-article-forward-header)
3033                                                 (point)))
3034               (delete-region (point-at-bol) (progn
3035                                               (gnus-article-forward-header)
3036                                               (forward-char -1)
3037                                               (point)))
3038               (setq pos (point))))
3039           (when (and (not pos)
3040                      (re-search-forward tdate-regexp nil t))
3041             (forward-line 1))
3042           (gnus-goto-char pos)
3043           (insert (article-make-date-line date (or type 'ut)))
3044           (unless pos
3045             (insert "\n")
3046             (forward-line -1))
3047           ;; Do highlighting.
3048           (beginning-of-line)
3049           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3050             (put-text-property (match-beginning 1) (1+ (match-end 1))
3051                                'face bface)
3052             (put-text-property (match-beginning 2) (match-end 2)
3053                                'face eface))
3054           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3055           (goto-char (point-max))
3056           (widen))))))
3057
3058 (defun article-make-date-line (date type)
3059   "Return a DATE line of TYPE."
3060   (unless (memq type '(local ut original user iso8601 lapsed english))
3061     (error "Unknown conversion type: %s" type))
3062   (condition-case ()
3063       (let ((time (date-to-time date)))
3064         (cond
3065          ;; Convert to the local timezone.
3066          ((eq type 'local)
3067           (concat "Date: " (message-make-date time)))
3068          ;; Convert to Universal Time.
3069          ((eq type 'ut)
3070           (concat "Date: "
3071                   (substring
3072                    (message-make-date
3073                     (let* ((e (parse-time-string date))
3074                            (tm (apply 'encode-time e))
3075                            (ms (car tm))
3076                            (ls (- (cadr tm) (car (current-time-zone time)))))
3077                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3078                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3079                             (t (list ms ls)))))
3080                    0 -5)
3081                   "UT"))
3082          ;; Get the original date from the article.
3083          ((eq type 'original)
3084           (concat "Date: " (if (string-match "\n+$" date)
3085                                (substring date 0 (match-beginning 0))
3086                              date)))
3087          ;; Let the user define the format.
3088          ((eq type 'user)
3089           (let ((format (or (condition-case nil
3090                                 (with-current-buffer gnus-summary-buffer
3091                                   gnus-article-time-format)
3092                               (error nil))
3093                             gnus-article-time-format)))
3094             (if (functionp format)
3095                 (funcall format time)
3096               (concat "Date: " (format-time-string format time)))))
3097          ;; ISO 8601.
3098          ((eq type 'iso8601)
3099           (let ((tz (car (current-time-zone time))))
3100             (concat
3101              "Date: "
3102              (format-time-string "%Y%m%dT%H%M%S" time)
3103              (format "%s%02d%02d"
3104                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3105                      (/ (% (abs tz) 3600) 60)))))
3106          ;; Do an X-Sent lapsed format.
3107          ((eq type 'lapsed)
3108           ;; If the date is seriously mangled, the timezone functions are
3109           ;; liable to bug out, so we ignore all errors.
3110           (let* ((now (current-time))
3111                  (real-time (subtract-time now time))
3112                  (real-sec (and real-time
3113                                 (+ (* (float (car real-time)) 65536)
3114                                    (cadr real-time))))
3115                  (sec (and real-time (abs real-sec)))
3116                  num prev)
3117             (cond
3118              ((null real-time)
3119               "X-Sent: Unknown")
3120              ((zerop sec)
3121               "X-Sent: Now")
3122              (t
3123               (concat
3124                "X-Sent: "
3125                ;; This is a bit convoluted, but basically we go
3126                ;; through the time units for years, weeks, etc,
3127                ;; and divide things to see whether that results
3128                ;; in positive answers.
3129                (mapconcat
3130                 (lambda (unit)
3131                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3132                       ;; The (remaining) seconds are too few to
3133                       ;; be divided into this time unit.
3134                       ""
3135                     ;; It's big enough, so we output it.
3136                     (setq sec (- sec (* num (cdr unit))))
3137                     (prog1
3138                         (concat (if prev ", " "") (int-to-string
3139                                                    (floor num))
3140                                 " " (symbol-name (car unit))
3141                                 (if (> num 1) "s" ""))
3142                       (setq prev t))))
3143                 article-time-units "")
3144                ;; If dates are odd, then it might appear like the
3145                ;; article was sent in the future.
3146                (if (> real-sec 0)
3147                    " ago"
3148                  " in the future"))))))
3149          ;; Display the date in proper English
3150          ((eq type 'english)
3151           (let ((dtime (decode-time time)))
3152             (concat
3153              "Date: the "
3154              (number-to-string (nth 3 dtime))
3155              (let ((digit (% (nth 3 dtime) 10)))
3156                (cond
3157                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3158                 ((= digit 1) "st")
3159                 ((= digit 2) "nd")
3160                 ((= digit 3) "rd")
3161                 (t "th")))
3162              " of "
3163              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3164              " "
3165              (number-to-string (nth 5 dtime))
3166              " at "
3167              (format "%02d" (nth 2 dtime))
3168              ":"
3169              (format "%02d" (nth 1 dtime)))))))
3170     (error
3171      (format "Date: %s (from Gnus)" date))))
3172
3173 (defun article-date-local (&optional highlight)
3174   "Convert the current article date to the local timezone."
3175   (interactive (list t))
3176   (article-date-ut 'local highlight))
3177
3178 (defun article-date-english (&optional highlight)
3179   "Convert the current article date to something that is proper English."
3180   (interactive (list t))
3181   (article-date-ut 'english highlight))
3182
3183 (defun article-date-original (&optional highlight)
3184   "Convert the current article date to what it was originally.
3185 This is only useful if you have used some other date conversion
3186 function and want to see what the date was before converting."
3187   (interactive (list t))
3188   (article-date-ut 'original highlight))
3189
3190 (defun article-date-lapsed (&optional highlight)
3191   "Convert the current article date to time lapsed since it was sent."
3192   (interactive (list t))
3193   (article-date-ut 'lapsed highlight))
3194
3195 (defun article-update-date-lapsed ()
3196   "Function to be run from a timer to update the lapsed time line."
3197   (save-match-data
3198     (let (deactivate-mark)
3199       (save-excursion
3200         (ignore-errors
3201          (walk-windows
3202           (lambda (w)
3203             (set-buffer (window-buffer w))
3204             (when (eq major-mode 'gnus-article-mode)
3205               (let ((mark (point-marker)))
3206                 (goto-char (point-min))
3207                 (when (re-search-forward "^X-Sent:" nil t)
3208                   (article-date-lapsed t))
3209                 (goto-char (marker-position mark))
3210                 (move-marker mark nil))))
3211           nil 'visible))))))
3212
3213 (defun gnus-start-date-timer (&optional n)
3214   "Start a timer to update the X-Sent header in the article buffers.
3215 The numerical prefix says how frequently (in seconds) the function
3216 is to run."
3217   (interactive "p")
3218   (unless n
3219     (setq n 1))
3220   (gnus-stop-date-timer)
3221   (setq article-lapsed-timer
3222         (run-at-time 1 n 'article-update-date-lapsed)))
3223
3224 (defun gnus-stop-date-timer ()
3225   "Stop the X-Sent timer."
3226   (interactive)
3227   (when article-lapsed-timer
3228     (nnheader-cancel-timer article-lapsed-timer)
3229     (setq article-lapsed-timer nil)))
3230
3231 (defun article-date-user (&optional highlight)
3232   "Convert the current article date to the user-defined format.
3233 This format is defined by the `gnus-article-time-format' variable."
3234   (interactive (list t))
3235   (article-date-ut 'user highlight))
3236
3237 (defun article-date-iso8601 (&optional highlight)
3238   "Convert the current article date to ISO8601."
3239   (interactive (list t))
3240   (article-date-ut 'iso8601 highlight))
3241
3242 (defmacro gnus-article-save-original-date (&rest forms)
3243   "Save the original date as a text property and evaluate FORMS."
3244   `(let* ((case-fold-search t)
3245           (start (progn
3246                    (goto-char (point-min))
3247                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3248                               (not (bolp)))
3249                      (match-end 0))))
3250           (date (when (and start
3251                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3252                                               nil t))
3253                   (buffer-substring-no-properties start
3254                                                   (match-beginning 0)))))
3255      (goto-char (point-max))
3256      (skip-chars-backward "\n")
3257      (put-text-property (point-min) (point) 'original-date date)
3258      ,@forms
3259      (goto-char (point-max))
3260      (skip-chars-backward "\n")
3261      (put-text-property (point-min) (point) 'original-date date)))
3262
3263 ;; (defun article-show-all ()
3264 ;;   "Show all hidden text in the article buffer."
3265 ;;   (interactive)
3266 ;;   (save-excursion
3267 ;;     (let ((inhibit-read-only t))
3268 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3269
3270 (defun article-remove-leading-whitespace ()
3271   "Remove excessive whitespace from all headers."
3272   (interactive)
3273   (save-excursion
3274     (save-restriction
3275       (let ((inhibit-read-only t))
3276         (article-narrow-to-head)
3277         (goto-char (point-min))
3278         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3279           (delete-region (match-beginning 1) (match-end 1)))))))
3280
3281 (defun article-emphasize (&optional arg)
3282   "Emphasize text according to `gnus-emphasis-alist'."
3283   (interactive (gnus-article-hidden-arg))
3284   (unless (gnus-article-check-hidden-text 'emphasis arg)
3285     (save-excursion
3286       (let ((alist (or
3287                     (condition-case nil
3288                         (with-current-buffer gnus-summary-buffer
3289                           gnus-article-emphasis-alist)
3290                       (error))
3291                     gnus-emphasis-alist))
3292             (inhibit-read-only t)
3293             (props (append '(article-type emphasis)
3294                            gnus-hidden-properties))
3295             regexp elem beg invisible visible face)
3296         (article-goto-body)
3297         (setq beg (point))
3298         (while (setq elem (pop alist))
3299           (goto-char beg)
3300           (setq regexp (car elem)
3301                 invisible (nth 1 elem)
3302                 visible (nth 2 elem)
3303                 face (nth 3 elem))
3304           (while (re-search-forward regexp nil t)
3305             (when (and (match-beginning visible) (match-beginning invisible))
3306               (gnus-article-hide-text
3307                (match-beginning invisible) (match-end invisible) props)
3308               (gnus-article-unhide-text-type
3309                (match-beginning visible) (match-end visible) 'emphasis)
3310               (gnus-put-overlay-excluding-newlines
3311                (match-beginning visible) (match-end visible) 'face face)
3312               (gnus-add-wash-type 'emphasis)
3313               (goto-char (match-end invisible)))))))))
3314
3315 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3316   "Setup newsgroup emphasis alist."
3317   (unless gnus-article-emphasis-alist
3318     (let ((name (and gnus-newsgroup-name
3319                      (gnus-group-real-name gnus-newsgroup-name))))
3320       (make-local-variable 'gnus-article-emphasis-alist)
3321       (setq gnus-article-emphasis-alist
3322             (nconc
3323              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3324                (while (setq elem (pop alist))
3325                  (when (and name (string-match (car elem) name))
3326                    (setq alist nil
3327                          highlight (copy-sequence (cdr elem)))))
3328                highlight)
3329              (copy-sequence highlight-words)
3330              (if gnus-newsgroup-name
3331                  (copy-sequence (gnus-group-find-parameter
3332                                  gnus-newsgroup-name 'highlight-words t)))
3333              gnus-emphasis-alist)))))
3334
3335 (eval-when-compile
3336   (defvar gnus-summary-article-menu)
3337   (defvar gnus-summary-post-menu))
3338
3339 ;;; Saving functions.
3340
3341 (defun gnus-article-save (save-buffer file &optional num)
3342   "Save the currently selected article."
3343   (unless gnus-save-all-headers
3344     ;; Remove headers according to `gnus-saved-headers'.
3345     (let ((gnus-visible-headers
3346            (or gnus-saved-headers gnus-visible-headers))
3347           (gnus-article-buffer save-buffer))
3348       (save-excursion
3349         (set-buffer save-buffer)
3350         (article-hide-headers 1 t))))
3351   (save-window-excursion
3352     (if (not gnus-default-article-saver)
3353         (error "No default saver is defined")
3354       ;; !!! Magic!  The saving functions all save
3355       ;; `gnus-save-article-buffer' (or so they think), but we
3356       ;; bind that variable to our save-buffer.
3357       (set-buffer gnus-article-buffer)
3358       (let* ((gnus-save-article-buffer save-buffer)
3359              (filename
3360               (cond
3361                ((not gnus-prompt-before-saving) 'default)
3362                ((eq gnus-prompt-before-saving 'always) nil)
3363                (t file)))
3364              (gnus-number-of-articles-to-be-saved
3365               (when (eq gnus-prompt-before-saving t)
3366                 num)))                  ; Magic
3367         (set-buffer gnus-article-current-summary)
3368         (funcall gnus-default-article-saver filename)))))
3369
3370 (defun gnus-read-save-file-name (prompt &optional filename
3371                                         function group headers variable)
3372   (let ((default-name
3373           (funcall function group headers (symbol-value variable)))
3374         result)
3375     (setq result
3376           (expand-file-name
3377            (cond
3378             ((eq filename 'default)
3379              default-name)
3380             ((eq filename t)
3381              default-name)
3382             (filename filename)
3383             (t
3384              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3385                     (prompt
3386                      (format prompt
3387                              (if (and gnus-number-of-articles-to-be-saved
3388                                       (> gnus-number-of-articles-to-be-saved 1))
3389                                  (format "these %d articles"
3390                                          gnus-number-of-articles-to-be-saved)
3391                                "this article")))
3392                     (file
3393                      ;; Let the split methods have their say.
3394                      (cond
3395                       ;; No split name was found.
3396                       ((null split-name)
3397                        (read-file-name
3398                         (concat prompt " (default "
3399                                 (file-name-nondirectory default-name) "): ")
3400                         (file-name-directory default-name)
3401                         default-name))
3402                       ;; A single group name is returned.
3403                       ((stringp split-name)
3404                        (setq default-name
3405                              (funcall function split-name headers
3406                                       (symbol-value variable)))
3407                        (read-file-name
3408                         (concat prompt " (default "
3409                                 (file-name-nondirectory default-name) "): ")
3410                         (file-name-directory default-name)
3411                         default-name))
3412                       ;; A single split name was found
3413                       ((= 1 (length split-name))
3414                        (let* ((name (expand-file-name
3415                                      (car split-name)
3416                                      gnus-article-save-directory))
3417                               (dir (cond ((file-directory-p name)
3418                                           (file-name-as-directory name))
3419                                          ((file-exists-p name) name)
3420                                          (t gnus-article-save-directory))))
3421                          (read-file-name
3422                           (concat prompt " (default " name "): ")
3423                           dir name)))
3424                       ;; A list of splits was found.
3425                       (t
3426                        (setq split-name (nreverse split-name))
3427                        (let (result)
3428                          (let ((file-name-history
3429                                 (nconc split-name file-name-history)))
3430                            (setq result
3431                                  (expand-file-name
3432                                   (read-file-name
3433                                    (concat prompt " (`M-p' for defaults): ")
3434                                    gnus-article-save-directory
3435                                    (car split-name))
3436                                   gnus-article-save-directory)))
3437                          (car (push result file-name-history)))))))
3438                ;; Create the directory.
3439                (gnus-make-directory (file-name-directory file))
3440                ;; If we have read a directory, we append the default file name.
3441                (when (file-directory-p file)
3442                  (setq file (expand-file-name (file-name-nondirectory
3443                                                default-name)
3444                                               (file-name-as-directory file))))
3445                ;; Possibly translate some characters.
3446                (nnheader-translate-file-chars file))))))
3447     (gnus-make-directory (file-name-directory result))
3448     (set variable result)))
3449
3450 (defun gnus-article-archive-name (group)
3451   "Return the first instance of an \"Archive-name\" in the current buffer."
3452   (let ((case-fold-search t))
3453     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3454       (nnheader-concat gnus-article-save-directory
3455                        (match-string 1)))))
3456
3457 (defun gnus-article-nndoc-name (group)
3458   "If GROUP is an nndoc group, return the name of the parent group."
3459   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3460     (gnus-group-get-parameter group 'save-article-group)))
3461
3462 (defun gnus-summary-save-in-rmail (&optional filename)
3463   "Append this article to Rmail file.
3464 Optional argument FILENAME specifies file name.
3465 Directory to save to is default to `gnus-article-save-directory'."
3466   (setq filename (gnus-read-save-file-name
3467                   "Save %s in rmail file" filename
3468                   gnus-rmail-save-name gnus-newsgroup-name
3469                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3470   (gnus-eval-in-buffer-window gnus-save-article-buffer
3471     (save-excursion
3472       (save-restriction
3473         (widen)
3474         (gnus-output-to-rmail filename))))
3475   filename)
3476
3477 (defun gnus-summary-save-in-mail (&optional filename)
3478   "Append this article to Unix mail file.
3479 Optional argument FILENAME specifies file name.
3480 Directory to save to is default to `gnus-article-save-directory'."
3481   (setq filename (gnus-read-save-file-name
3482                   "Save %s in Unix mail file" filename
3483                   gnus-mail-save-name gnus-newsgroup-name
3484                   gnus-current-headers 'gnus-newsgroup-last-mail))
3485   (gnus-eval-in-buffer-window gnus-save-article-buffer
3486     (save-excursion
3487       (save-restriction
3488         (widen)
3489         (if (and (file-readable-p filename)
3490                  (file-regular-p filename)
3491                  (mail-file-babyl-p filename))
3492             (rmail-output-to-rmail-file filename t)
3493           (gnus-output-to-mail filename)))))
3494   filename)
3495
3496 (defun gnus-summary-save-in-file (&optional filename overwrite)
3497   "Append this article to file.
3498 Optional argument FILENAME specifies file name.
3499 Directory to save to is default to `gnus-article-save-directory'."
3500   (setq filename (gnus-read-save-file-name
3501                   "Save %s in file" filename
3502                   gnus-file-save-name gnus-newsgroup-name
3503                   gnus-current-headers 'gnus-newsgroup-last-file))
3504   (gnus-eval-in-buffer-window gnus-save-article-buffer
3505     (save-excursion
3506       (save-restriction
3507         (widen)
3508         (when (and overwrite
3509                    (file-exists-p filename))
3510           (delete-file filename))
3511         (gnus-output-to-file filename))))
3512   filename)
3513
3514 (defun gnus-summary-write-to-file (&optional filename)
3515   "Write this article to a file, overwriting it if the file exists.
3516 Optional argument FILENAME specifies file name.
3517 The directory to save in defaults to `gnus-article-save-directory'."
3518   (gnus-summary-save-in-file nil t))
3519
3520 (defun gnus-summary-save-body-in-file (&optional filename)
3521   "Append this article body to a file.
3522 Optional argument FILENAME specifies file name.
3523 The directory to save in defaults to `gnus-article-save-directory'."
3524   (setq filename (gnus-read-save-file-name
3525                   "Save %s body in file" filename
3526                   gnus-file-save-name gnus-newsgroup-name
3527                   gnus-current-headers 'gnus-newsgroup-last-file))
3528   (gnus-eval-in-buffer-window gnus-save-article-buffer
3529     (save-excursion
3530       (save-restriction
3531         (widen)
3532         (when (article-goto-body)
3533           (narrow-to-region (point) (point-max)))
3534         (gnus-output-to-file filename))))
3535   filename)
3536
3537 (defun gnus-summary-save-in-pipe (&optional command)
3538   "Pipe this article to subprocess."
3539   (setq command
3540         (cond ((and (eq command 'default)
3541                     gnus-last-shell-command)
3542                gnus-last-shell-command)
3543               ((stringp command)
3544                command)
3545               (t (read-string
3546                   (format
3547                    "Shell command on %s: "
3548                    (if (and gnus-number-of-articles-to-be-saved
3549                             (> gnus-number-of-articles-to-be-saved 1))
3550                        (format "these %d articles"
3551                                gnus-number-of-articles-to-be-saved)
3552                      "this article"))
3553                   gnus-last-shell-command))))
3554   (when (string-equal command "")
3555     (if gnus-last-shell-command
3556         (setq command gnus-last-shell-command)
3557       (error "A command is required")))
3558   (gnus-eval-in-buffer-window gnus-article-buffer
3559     (save-restriction
3560       (widen)
3561       (shell-command-on-region (point-min) (point-max) command nil)))
3562   (setq gnus-last-shell-command command))
3563
3564 (defun gnus-summary-pipe-to-muttprint (&optional command)
3565   "Pipe this article to muttprint."
3566   (setq command (read-string
3567                  "Print using command: " gnus-summary-muttprint-program
3568                  nil gnus-summary-muttprint-program))
3569   (gnus-summary-save-in-pipe command))
3570
3571 ;;; Article file names when saving.
3572
3573 (defun gnus-capitalize-newsgroup (newsgroup)
3574   "Capitalize NEWSGROUP name."
3575   (when (not (zerop (length newsgroup)))
3576     (concat (char-to-string (upcase (aref newsgroup 0)))
3577             (substring newsgroup 1))))
3578
3579 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3580   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3581 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3582 Otherwise, it is like ~/News/news/group/num."
3583   (let ((default
3584           (expand-file-name
3585            (concat (if (gnus-use-long-file-name 'not-save)
3586                        (gnus-capitalize-newsgroup newsgroup)
3587                      (gnus-newsgroup-directory-form newsgroup))
3588                    "/" (int-to-string (mail-header-number headers)))
3589            gnus-article-save-directory)))
3590     (if (and last-file
3591              (string-equal (file-name-directory default)
3592                            (file-name-directory last-file))
3593              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3594         default
3595       (or last-file default))))
3596
3597 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3598   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3599 If variable `gnus-use-long-file-name' is non-nil, it is
3600 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3601   (let ((default
3602           (expand-file-name
3603            (concat (if (gnus-use-long-file-name 'not-save)
3604                        newsgroup
3605                      (gnus-newsgroup-directory-form newsgroup))
3606                    "/" (int-to-string (mail-header-number headers)))
3607            gnus-article-save-directory)))
3608     (if (and last-file
3609              (string-equal (file-name-directory default)
3610                            (file-name-directory last-file))
3611              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3612         default
3613       (or last-file default))))
3614
3615 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3616   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3617 If variable `gnus-use-long-file-name' is non-nil, it is
3618 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3619   (or last-file
3620       (expand-file-name
3621        (if (gnus-use-long-file-name 'not-save)
3622            newsgroup
3623          (file-relative-name
3624           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3625           default-directory))
3626        gnus-article-save-directory)))
3627
3628 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3629   "Generate file name from sender."
3630   (let ((from (mail-header-from headers)))
3631     (expand-file-name
3632      (if (and from (string-match "\\([^ <]+\\)@" from))
3633          (match-string 1 from)
3634        "nobody")
3635      gnus-article-save-directory)))
3636
3637 (defun article-verify-x-pgp-sig ()
3638   "Verify X-PGP-Sig."
3639   (interactive)
3640   (if (gnus-buffer-live-p gnus-original-article-buffer)
3641       (let ((sig (with-current-buffer gnus-original-article-buffer
3642                    (gnus-fetch-field "X-PGP-Sig")))
3643             items info headers)
3644         (when (and sig
3645                    mml2015-use
3646                    (mml2015-clear-verify-function))
3647           (with-temp-buffer
3648             (insert-buffer-substring gnus-original-article-buffer)
3649             (setq items (split-string sig))
3650             (message-narrow-to-head)
3651             (let ((inhibit-point-motion-hooks t)
3652                   (case-fold-search t))
3653               ;; Don't verify multiple headers.
3654               (setq headers (mapconcat (lambda (header)
3655                                          (concat header ": "
3656                                                  (mail-fetch-field header)
3657                                                  "\n"))
3658                                        (split-string (nth 1 items) ",") "")))
3659             (delete-region (point-min) (point-max))
3660             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3661             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3662             (insert headers)
3663             (widen)
3664             (forward-line)
3665             (while (not (eobp))
3666               (if (looking-at "^-")
3667                   (insert "- "))
3668               (forward-line))
3669             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3670             (insert "Version: " (car items) "\n\n")
3671             (insert (mapconcat 'identity (cddr items) "\n"))
3672             (insert "\n-----END PGP SIGNATURE-----\n")
3673             (let ((mm-security-handle (list (format "multipart/signed"))))
3674               (mml2015-clean-buffer)
3675               (let ((coding-system-for-write (or gnus-newsgroup-charset
3676                                                  'iso-8859-1)))
3677                 (funcall (mml2015-clear-verify-function)))
3678               (setq info
3679                     (or (mm-handle-multipart-ctl-parameter
3680                          mm-security-handle 'gnus-details)
3681                         (mm-handle-multipart-ctl-parameter
3682                          mm-security-handle 'gnus-info)))))
3683           (when info
3684             (let ((inhibit-read-only t) bface eface)
3685               (save-restriction
3686                 (message-narrow-to-head)
3687                 (goto-char (point-max))
3688                 (forward-line -1)
3689                 (setq bface (get-text-property (point-at-bol) 'face)
3690                       eface (get-text-property (1- (point-at-eol)) 'face))
3691                 (message-remove-header "X-Gnus-PGP-Verify")
3692                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3693                     (forward-line)
3694                   (goto-char (point-max)))
3695                 (narrow-to-region (point) (point))
3696                 (insert "X-Gnus-PGP-Verify: " info "\n")
3697                 (goto-char (point-min))
3698                 (forward-line)
3699                 (while (not (eobp))
3700                   (if (not (looking-at "^[ \t]"))
3701                       (insert " "))
3702                   (forward-line))
3703                 ;; Do highlighting.
3704                 (goto-char (point-min))
3705                 (when (looking-at "\\([^:]+\\): *")
3706                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3707                                      'face bface)
3708                   (put-text-property (match-end 0) (point-max)
3709                                      'face eface)))))))))
3710
3711 (defun article-verify-cancel-lock ()
3712   "Verify Cancel-Lock header."
3713   (interactive)
3714   (if (gnus-buffer-live-p gnus-original-article-buffer)
3715       (canlock-verify gnus-original-article-buffer)))
3716
3717 (eval-and-compile
3718   (mapcar
3719    (lambda (func)
3720      (let (afunc gfunc)
3721        (if (consp func)
3722            (setq afunc (car func)
3723                  gfunc (cdr func))
3724          (setq afunc func
3725                gfunc (intern (format "gnus-%s" func))))
3726        (defalias gfunc
3727          (when (fboundp afunc)
3728            `(lambda (&optional interactive &rest args)
3729               ,(documentation afunc t)
3730               (interactive (list t))
3731               (save-excursion
3732                 (set-buffer gnus-article-buffer)
3733                 (if interactive
3734                     (call-interactively ',afunc)
3735                   (apply ',afunc args))))))))
3736    '(article-hide-headers
3737      article-verify-x-pgp-sig
3738      article-verify-cancel-lock
3739      article-hide-boring-headers
3740      article-treat-overstrike
3741      article-treat-ansi-sequences
3742      article-fill-long-lines
3743      article-capitalize-sentences
3744      article-remove-cr
3745      article-remove-leading-whitespace
3746      article-display-x-face
3747      article-display-face
3748      article-de-quoted-unreadable
3749      article-de-base64-unreadable
3750      article-decode-HZ
3751      article-wash-html
3752      article-unsplit-urls
3753      article-hide-list-identifiers
3754      article-strip-banner
3755      article-babel
3756      article-hide-pem
3757      article-hide-signature
3758      article-strip-headers-in-body
3759      article-remove-trailing-blank-lines
3760      article-strip-leading-blank-lines
3761      article-strip-multiple-blank-lines
3762      article-strip-leading-space
3763      article-strip-trailing-space
3764      article-strip-blank-lines
3765      article-strip-all-blank-lines
3766      article-date-local
3767      article-date-english
3768      article-date-iso8601
3769      article-date-original
3770      article-date-ut
3771      article-decode-mime-words
3772      article-decode-charset
3773      article-decode-encoded-words
3774      article-date-user
3775      article-date-lapsed
3776      article-emphasize
3777      article-treat-dumbquotes
3778      article-normalize-headers
3779 ;;     (article-show-all . gnus-article-show-all-headers)
3780      )))
3781 \f
3782 ;;;
3783 ;;; Gnus article mode
3784 ;;;
3785
3786 (put 'gnus-article-mode 'mode-class 'special)
3787
3788 (set-keymap-parent gnus-article-mode-map widget-keymap)
3789
3790 (gnus-define-keys gnus-article-mode-map
3791   " " gnus-article-goto-next-page
3792   "\177" gnus-article-goto-prev-page
3793   [delete] gnus-article-goto-prev-page
3794   [backspace] gnus-article-goto-prev-page
3795   "\C-c^" gnus-article-refer-article
3796   "h" gnus-article-show-summary
3797   "s" gnus-article-show-summary
3798   "\C-c\C-m" gnus-article-mail
3799   "?" gnus-article-describe-briefly
3800   "e" gnus-summary-edit-article
3801   "<" beginning-of-buffer
3802   ">" end-of-buffer
3803   "\C-c\C-i" gnus-info-find-node
3804   "\C-c\C-b" gnus-bug
3805   "R" gnus-article-reply-with-original
3806   "F" gnus-article-followup-with-original
3807   "\C-hk" gnus-article-describe-key
3808   "\C-hc" gnus-article-describe-key-briefly
3809
3810   "\C-d" gnus-article-read-summary-keys
3811   "\M-*" gnus-article-read-summary-keys
3812   "\M-#" gnus-article-read-summary-keys
3813   "\M-^" gnus-article-read-summary-keys
3814   "\M-g" gnus-article-read-summary-keys)
3815
3816 (substitute-key-definition
3817  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
3818
3819 (defun gnus-article-make-menu-bar ()
3820   (unless (boundp 'gnus-article-commands-menu)
3821     (gnus-summary-make-menu-bar))
3822   (gnus-turn-off-edit-menu 'article)
3823   (unless (boundp 'gnus-article-article-menu)
3824     (easy-menu-define
3825      gnus-article-article-menu gnus-article-mode-map ""
3826      '("Article"
3827        ["Scroll forwards" gnus-article-goto-next-page t]
3828        ["Scroll backwards" gnus-article-goto-prev-page t]
3829        ["Show summary" gnus-article-show-summary t]
3830        ["Fetch Message-ID at point" gnus-article-refer-article t]
3831        ["Mail to address at point" gnus-article-mail t]
3832        ["Send a bug report" gnus-bug t]))
3833
3834     (easy-menu-define
3835      gnus-article-treatment-menu gnus-article-mode-map ""
3836      ;; Fixme: this should use :active (and maybe :visible).
3837      '("Treatment"
3838        ["Hide headers" gnus-article-hide-headers t]
3839        ["Hide signature" gnus-article-hide-signature t]
3840        ["Hide citation" gnus-article-hide-citation t]
3841        ["Treat overstrike" gnus-article-treat-overstrike t]
3842        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3843        ["Remove carriage return" gnus-article-remove-cr t]
3844        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3845        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
3846        ["Remove base64" gnus-article-de-base64-unreadable t]
3847        ["Treat html" gnus-article-wash-html t]
3848        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3849        ["Decode HZ" gnus-article-decode-HZ t]))
3850
3851     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3852
3853     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3854
3855     (gnus-run-hooks 'gnus-article-menu-hook)))
3856
3857 (defun gnus-article-mode ()
3858   "Major mode for displaying an article.
3859
3860 All normal editing commands are switched off.
3861
3862 The following commands are available in addition to all summary mode
3863 commands:
3864 \\<gnus-article-mode-map>
3865 \\[gnus-article-next-page]\t Scroll the article one page forwards
3866 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3867 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3868 \\[gnus-article-show-summary]\t Display the summary buffer
3869 \\[gnus-article-mail]\t Send a reply to the address near point
3870 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3871 \\[gnus-info-find-node]\t Go to the Gnus info node"
3872   (interactive)
3873   (kill-all-local-variables)
3874   (gnus-simplify-mode-line)
3875   (setq mode-name "Article")
3876   (setq major-mode 'gnus-article-mode)
3877   (make-local-variable 'minor-mode-alist)
3878   (use-local-map gnus-article-mode-map)
3879   (when (gnus-visual-p 'article-menu 'menu)
3880     (gnus-article-make-menu-bar)
3881     (when gnus-summary-tool-bar-map
3882       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3883   (gnus-update-format-specifications nil 'article-mode)
3884   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3885   (set (make-local-variable 'gnus-page-broken) nil)
3886   (make-local-variable 'gnus-button-marker-list)
3887   (make-local-variable 'gnus-article-current-summary)
3888   (make-local-variable 'gnus-article-mime-handles)
3889   (make-local-variable 'gnus-article-decoded-p)
3890   (make-local-variable 'gnus-article-mime-handle-alist)
3891   (make-local-variable 'gnus-article-wash-types)
3892   (make-local-variable 'gnus-article-image-alist)
3893   (make-local-variable 'gnus-article-charset)
3894   (make-local-variable 'gnus-article-ignored-charsets)
3895   ;; Prevent recent Emacsen from displaying non-break space as "\ ".
3896   (set (make-local-variable 'nobreak-char-display) nil)
3897   (gnus-set-default-directory)
3898   (buffer-disable-undo)
3899   (setq buffer-read-only t
3900         show-trailing-whitespace nil)
3901   (set-syntax-table gnus-article-mode-syntax-table)
3902   (mm-enable-multibyte)
3903   (gnus-run-mode-hooks 'gnus-article-mode-hook))
3904
3905 (defun gnus-article-setup-buffer ()
3906   "Initialize the article buffer."
3907   (let* ((name (if gnus-single-article-buffer "*Article*"
3908                  (concat "*Article " gnus-newsgroup-name "*")))
3909          (original
3910           (progn (string-match "\\*Article" name)
3911                  (concat " *Original Article"
3912                          (substring name (match-end 0))))))
3913     (setq gnus-article-buffer name)
3914     (setq gnus-original-article-buffer original)
3915     (setq gnus-article-mime-handle-alist nil)
3916     ;; This might be a variable local to the summary buffer.
3917     (unless gnus-single-article-buffer
3918       (save-excursion
3919         (set-buffer gnus-summary-buffer)
3920         (setq gnus-article-buffer name)
3921         (setq gnus-original-article-buffer original)
3922         (gnus-set-global-variables)))
3923     (gnus-article-setup-highlight-words)
3924     ;; Init original article buffer.
3925     (save-excursion
3926       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3927       (mm-enable-multibyte)
3928       (setq major-mode 'gnus-original-article-mode)
3929       (make-local-variable 'gnus-original-article))
3930     (if (and (get-buffer name)
3931              (with-current-buffer name
3932                (if gnus-article-edit-mode
3933                    (if (y-or-n-p "Article mode edit in progress; discard? ")
3934                        (progn
3935                          (set-buffer-modified-p nil)
3936                          (gnus-kill-buffer name)
3937                          (message "")
3938                          nil)
3939                      (error "Action aborted"))
3940                  t)))
3941         (save-excursion
3942           (set-buffer name)
3943           (set (make-local-variable 'gnus-article-edit-mode) nil)
3944           (when gnus-article-mime-handles
3945             (mm-destroy-parts gnus-article-mime-handles)
3946             (setq gnus-article-mime-handles nil))
3947           ;; Set it to nil in article-buffer!
3948           (setq gnus-article-mime-handle-alist nil)
3949           (buffer-disable-undo)
3950           (setq buffer-read-only t)
3951           ;; This list just keeps growing if we don't reset it.
3952           (setq gnus-button-marker-list nil)
3953           (unless (eq major-mode 'gnus-article-mode)
3954             (gnus-article-mode))
3955           (current-buffer))
3956       (save-excursion
3957         (set-buffer (gnus-get-buffer-create name))
3958         (gnus-article-mode)
3959         (make-local-variable 'gnus-summary-buffer)
3960         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3961         (current-buffer)))))
3962
3963 ;; Set article window start at LINE, where LINE is the number of lines
3964 ;; from the head of the article.
3965 (defun gnus-article-set-window-start (&optional line)
3966   (set-window-start
3967    (gnus-get-buffer-window gnus-article-buffer t)
3968    (save-excursion
3969      (set-buffer gnus-article-buffer)
3970      (goto-char (point-min))
3971      (if (not line)
3972          (point-min)
3973        (gnus-message 6 "Moved to bookmark")
3974        (search-forward "\n\n" nil t)
3975        (forward-line line)
3976        (point)))))
3977
3978 (defun gnus-article-prepare (article &optional all-headers header)
3979   "Prepare ARTICLE in article mode buffer.
3980 ARTICLE should either be an article number or a Message-ID.
3981 If ARTICLE is an id, HEADER should be the article headers.
3982 If ALL-HEADERS is non-nil, no headers are hidden."
3983   (save-excursion
3984     ;; Make sure we start in a summary buffer.
3985     (unless (eq major-mode 'gnus-summary-mode)
3986       (set-buffer gnus-summary-buffer))
3987     (setq gnus-summary-buffer (current-buffer))
3988     (let* ((gnus-article (if header (mail-header-number header) article))
3989            (summary-buffer (current-buffer))
3990            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3991            (group gnus-newsgroup-name)
3992            result)
3993       (save-excursion
3994         (gnus-article-setup-buffer)
3995         (set-buffer gnus-article-buffer)
3996         ;; Deactivate active regions.
3997         (when (and (boundp 'transient-mark-mode)
3998                    transient-mark-mode)
3999           (setq mark-active nil))
4000         (if (not (setq result (let ((inhibit-read-only t))
4001                                 (gnus-request-article-this-buffer
4002                                  article group))))
4003             ;; There is no such article.
4004             (save-excursion
4005               (when (and (numberp article)
4006                          (not (memq article gnus-newsgroup-sparse)))
4007                 (setq gnus-article-current
4008                       (cons gnus-newsgroup-name article))
4009                 (set-buffer gnus-summary-buffer)
4010                 (setq gnus-current-article article)
4011                 (if (and (memq article gnus-newsgroup-undownloaded)
4012                          (not (gnus-online (gnus-find-method-for-group
4013                                             gnus-newsgroup-name))))
4014                     (progn
4015                       (gnus-summary-set-agent-mark article)
4016                       (message "Message marked for downloading"))
4017                   (gnus-summary-mark-article article gnus-canceled-mark)
4018                   (unless (memq article gnus-newsgroup-sparse)
4019                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4020           (if (or (eq result 'pseudo)
4021                   (eq result 'nneething))
4022               (progn
4023                 (save-excursion
4024                   (set-buffer summary-buffer)
4025                   (push article gnus-newsgroup-history)
4026                   (setq gnus-last-article gnus-current-article
4027                         gnus-current-article 0
4028                         gnus-current-headers nil
4029                         gnus-article-current nil)
4030                   (if (eq result 'nneething)
4031                       (gnus-configure-windows 'summary)
4032                     (gnus-configure-windows 'article))
4033                   (gnus-set-global-variables))
4034                 (let ((gnus-article-mime-handle-alist-1
4035                        gnus-article-mime-handle-alist))
4036                   (gnus-set-mode-line 'article)))
4037             ;; The result from the `request' was an actual article -
4038             ;; or at least some text that is now displayed in the
4039             ;; article buffer.
4040             (when (and (numberp article)
4041                        (not (eq article gnus-current-article)))
4042               ;; Seems like a new article has been selected.
4043               ;; `gnus-current-article' must be an article number.
4044               (save-excursion
4045                 (set-buffer summary-buffer)
4046                 (push article gnus-newsgroup-history)
4047                 (setq gnus-last-article gnus-current-article
4048                       gnus-current-article article
4049                       gnus-current-headers
4050                       (gnus-summary-article-header gnus-current-article)
4051                       gnus-article-current
4052                       (cons gnus-newsgroup-name gnus-current-article))
4053                 (unless (vectorp gnus-current-headers)
4054                   (setq gnus-current-headers nil))
4055                 (gnus-summary-goto-subject gnus-current-article)
4056                 (when (gnus-summary-show-thread)
4057                   ;; If the summary buffer really was folded, the
4058                   ;; previous goto may not actually have gone to
4059                   ;; the right article, but the thread root instead.
4060                   ;; So we go again.
4061                   (gnus-summary-goto-subject gnus-current-article))
4062                 (gnus-run-hooks 'gnus-mark-article-hook)
4063                 (gnus-set-mode-line 'summary)
4064                 (when (gnus-visual-p 'article-highlight 'highlight)
4065                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4066                 ;; Set the global newsgroup variables here.
4067                 (gnus-set-global-variables)
4068                 (setq gnus-have-all-headers
4069                       (or all-headers gnus-show-all-headers))))
4070             (save-excursion
4071               (gnus-configure-windows 'article))
4072             (when (or (numberp article)
4073                       (stringp article))
4074               (gnus-article-prepare-display)
4075               ;; Do page break.
4076               (goto-char (point-min))
4077               (when gnus-break-pages
4078                 (gnus-narrow-to-page)))
4079             (let ((gnus-article-mime-handle-alist-1
4080                    gnus-article-mime-handle-alist))
4081               (gnus-set-mode-line 'article))
4082             (article-goto-body)
4083             (unless (bobp)
4084               (forward-line -1))
4085             (set-window-point (get-buffer-window (current-buffer)) (point))
4086             (gnus-configure-windows 'article)
4087             t))))))
4088
4089 ;;;###autoload
4090 (defun gnus-article-prepare-display ()
4091   "Make the current buffer look like a nice article."
4092   ;; Hooks for getting information from the article.
4093   ;; This hook must be called before being narrowed.
4094   (let ((gnus-article-buffer (current-buffer))
4095         buffer-read-only
4096         (inhibit-read-only t))
4097     (unless (eq major-mode 'gnus-article-mode)
4098       (gnus-article-mode))
4099     (setq buffer-read-only nil
4100           gnus-article-wash-types nil
4101           gnus-article-image-alist nil)
4102     (gnus-run-hooks 'gnus-tmp-internal-hook)
4103     (when gnus-display-mime-function
4104       (funcall gnus-display-mime-function))
4105     (gnus-run-hooks 'gnus-article-prepare-hook)))
4106
4107 ;;;
4108 ;;; Gnus MIME viewing functions
4109 ;;;
4110
4111 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4112   "Format of the MIME buttons.
4113
4114 Valid specifiers include:
4115 %t  The MIME type
4116 %T  MIME type, along with additional info
4117 %n  The `name' parameter
4118 %d  The description, if any
4119 %l  The length of the encoded part
4120 %p  The part identifier number
4121 %e  Dots if the part isn't displayed
4122
4123 General format specifiers can also be used.  See Info node
4124 `(gnus)Formatting Variables'.")
4125
4126 (defvar gnus-mime-button-line-format-alist
4127   '((?t gnus-tmp-type ?s)
4128     (?T gnus-tmp-type-long ?s)
4129     (?n gnus-tmp-name ?s)
4130     (?d gnus-tmp-description ?s)
4131     (?p gnus-tmp-id ?s)
4132     (?l gnus-tmp-length ?d)
4133     (?e gnus-tmp-dots ?s)))
4134
4135 (defvar gnus-mime-button-commands
4136   '((gnus-article-press-button "\r" "Toggle Display")
4137     (gnus-mime-view-part "v" "View Interactively...")
4138     (gnus-mime-view-part-as-type "t" "View As Type...")
4139     (gnus-mime-view-part-as-charset "C" "View As charset...")
4140     (gnus-mime-save-part "o" "Save...")
4141     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4142     (gnus-mime-replace-part "r" "Replace part")
4143     (gnus-mime-delete-part "d" "Delete part")
4144     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4145     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4146     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4147     (gnus-mime-view-part-externally "e" "View Externally")
4148     (gnus-mime-print-part "p" "Print")
4149     (gnus-mime-pipe-part "|" "Pipe To Command...")
4150     (gnus-mime-action-on-part "." "Take action on the part...")))
4151
4152 (defun gnus-article-mime-part-status ()
4153   (if gnus-article-mime-handle-alist-1
4154       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4155           " (1 part)"
4156         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4157     ""))
4158
4159 (defvar gnus-mime-button-map
4160   (let ((map (make-sparse-keymap)))
4161     (define-key map gnus-mouse-2 'gnus-article-push-button)
4162     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4163     (dolist (c gnus-mime-button-commands)
4164       (define-key map (cadr c) (car c)))
4165     map))
4166
4167 (easy-menu-define
4168   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4169   `("MIME Part"
4170     ,@(mapcar (lambda (c)
4171                 (vector (caddr c) (car c) :enable t))
4172               gnus-mime-button-commands)))
4173
4174 (defun gnus-mime-button-menu (event prefix)
4175  "Construct a context-sensitive menu of MIME commands."
4176  (interactive "e\nP")
4177  (save-window-excursion
4178    (let ((pos (event-start event)))
4179      (select-window (posn-window pos))
4180      (goto-char (posn-point pos))
4181      (gnus-article-check-buffer)
4182      (popup-menu gnus-mime-button-menu nil prefix))))
4183
4184 (defun gnus-mime-view-all-parts (&optional handles)
4185   "View all the MIME parts."
4186   (interactive)
4187   (with-current-buffer gnus-article-buffer
4188     (let ((handles (or handles gnus-article-mime-handles))
4189           (mail-parse-charset gnus-newsgroup-charset)
4190           (mail-parse-ignored-charsets
4191            (with-current-buffer gnus-summary-buffer
4192              gnus-newsgroup-ignored-charsets)))
4193       (when handles
4194         (mm-remove-parts handles)
4195         (goto-char (point-min))
4196         (or (search-forward "\n\n") (goto-char (point-max)))
4197         (let ((inhibit-read-only t))
4198           (delete-region (point) (point-max))
4199           (mm-display-parts handles))))))
4200
4201 (defun gnus-article-jump-to-part (n)
4202   "Jump to MIME part N."
4203   (interactive "P")
4204   (pop-to-buffer gnus-article-buffer)
4205   ;; FIXME: why is it necessary?
4206   (sit-for 0)
4207   (let ((parts (length gnus-article-mime-handle-alist)))
4208     (or n (setq n
4209                 (string-to-number
4210                  (read-string ;; Emacs 21 doesn't have `read-number'.
4211                   (format "Jump to part (2..%s): " parts)))))
4212     (unless (and (integerp n) (<= n parts) (>= n 1))
4213       (setq n
4214             (progn
4215               (gnus-message 7 "Invalid part `%s', using %s instead."
4216                             n parts)
4217               parts)))
4218     (gnus-message 9 "Jumping to part %s." n)
4219     (cond ((>= gnus-auto-select-part 1)
4220            (while (and (<= n parts)
4221                        (not (gnus-article-goto-part n)))
4222              (setq n (1+ n))))
4223           ((< gnus-auto-select-part 0)
4224            (while (and (>= n 1)
4225                        (not (gnus-article-goto-part n)))
4226              (setq n (1- n))))
4227           (t
4228            (gnus-article-goto-part n)))))
4229
4230 (eval-when-compile
4231   (defsubst gnus-article-edit-part (handles &optional current-id)
4232     "Edit an article in order to delete a mime part.
4233 This function is exclusively used by `gnus-mime-save-part-and-strip'
4234 and `gnus-mime-delete-part', and not provided at run-time normally."
4235     (gnus-article-edit-article
4236      `(lambda ()
4237         (buffer-disable-undo)
4238         (erase-buffer)
4239         (let ((mail-parse-charset (or gnus-article-charset
4240                                       ',gnus-newsgroup-charset))
4241               (mail-parse-ignored-charsets
4242                (or gnus-article-ignored-charsets
4243                    ',gnus-newsgroup-ignored-charsets))
4244               (mbl mml-buffer-list))
4245           (setq mml-buffer-list nil)
4246           (insert-buffer-substring gnus-original-article-buffer)
4247           (mime-to-mml ',handles)
4248           (setq gnus-article-mime-handles nil)
4249           (let ((mbl1 mml-buffer-list))
4250             (setq mml-buffer-list mbl)
4251             (set (make-local-variable 'mml-buffer-list) mbl1))
4252           (gnus-make-local-hook 'kill-buffer-hook)
4253           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4254      `(lambda (no-highlight)
4255         (let ((mail-parse-charset (or gnus-article-charset
4256                                       ',gnus-newsgroup-charset))
4257               (message-options message-options)
4258               (message-options-set-recipient)
4259               (mail-parse-ignored-charsets
4260                (or gnus-article-ignored-charsets
4261                    ',gnus-newsgroup-ignored-charsets)))
4262           (mml-to-mime)
4263           (mml-destroy-buffers)
4264           (remove-hook 'kill-buffer-hook
4265                        'mml-destroy-buffers t)
4266           (kill-local-variable 'mml-buffer-list))
4267         (gnus-summary-edit-article-done
4268          ,(or (mail-header-references gnus-current-headers) "")
4269          ,(gnus-group-read-only-p)
4270          ,gnus-summary-buffer no-highlight))
4271      t)
4272     (gnus-article-edit-done)
4273     (gnus-summary-expand-window)
4274     (gnus-summary-show-article)
4275     (when (and current-id (integerp gnus-auto-select-part))
4276       (gnus-article-jump-to-part
4277        (+ current-id gnus-auto-select-part)))))
4278
4279 (defun gnus-mime-replace-part (file)
4280   "Replace MIME part under point with an external body."
4281   ;; Useful if file has already been saved to disk
4282   (interactive
4283    (list
4284     (mm-with-multibyte
4285       (read-file-name "Replace MIME part with file: "
4286                       (or mm-default-directory default-directory)
4287                       nil nil))))
4288   (gnus-mime-save-part-and-strip file))
4289
4290 (defun gnus-mime-save-part-and-strip (&optional file)
4291   "Save the MIME part under point then replace it with an external body.
4292 If FILE is given, use it for the external part."
4293   (interactive)
4294   (gnus-article-check-buffer)
4295   (when (gnus-group-read-only-p)
4296     (error "The current group does not support deleting of parts"))
4297   (when (mm-complicated-handles gnus-article-mime-handles)
4298     (error "\
4299 The current article has a complicated MIME structure, giving up..."))
4300   (let* ((data (get-text-property (point) 'gnus-data))
4301          (id (get-text-property (point) 'gnus-part))
4302          param
4303          (handles gnus-article-mime-handles))
4304     (unless file
4305       (setq file
4306             (and data (mm-save-part data "Delete MIME part and save to: "))))
4307     (when file
4308       (with-current-buffer (mm-handle-buffer data)
4309         (erase-buffer)
4310         (insert "Content-Type: " (mm-handle-media-type data))
4311         (mml-insert-parameter-string (cdr (mm-handle-type data))
4312                                      '(charset))
4313         (insert "\n")
4314         (insert "Content-ID: " (message-make-message-id) "\n")
4315         (insert "Content-Transfer-Encoding: binary\n")
4316         (insert "\n"))
4317       (setcdr data
4318               (cdr (mm-make-handle nil
4319                                    `("message/external-body"
4320                                      (access-type . "LOCAL-FILE")
4321                                      (name . ,file)))))
4322       ;; (set-buffer gnus-summary-buffer)
4323       (gnus-article-edit-part handles id))))
4324
4325 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4326 ;; parts...>') but with stripping would be nice.
4327
4328 (defun gnus-mime-delete-part ()
4329   "Delete the MIME part under point.
4330 Replace it with some information about the removed part."
4331   (interactive)
4332   (gnus-article-check-buffer)
4333   (when (gnus-group-read-only-p)
4334     (error "The current group does not support deleting of parts"))
4335   (when (mm-complicated-handles gnus-article-mime-handles)
4336     (error "\
4337 The current article has a complicated MIME structure, giving up..."))
4338   (when (or gnus-expert-user
4339             (gnus-yes-or-no-p "\
4340 Deleting parts may malfunction or destroy the article; continue? "))
4341     (let* ((data (get-text-property (point) 'gnus-data))
4342            (id (get-text-property (point) 'gnus-part))
4343            (handles gnus-article-mime-handles)
4344            (none "(none)")
4345            (description
4346             (or
4347              (mail-decode-encoded-word-string (or (mm-handle-description data)
4348                                                   none))))
4349            (filename
4350             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4351                 none))
4352            (type (mm-handle-media-type data)))
4353       (unless data
4354         (error "No MIME part under point"))
4355       (with-current-buffer (mm-handle-buffer data)
4356         (let ((bsize (format "%s" (buffer-size))))
4357           (erase-buffer)
4358           (insert
4359            (concat
4360             ",----\n"
4361             "| The following attachment has been deleted:\n"
4362             "|\n"
4363             "| Type:           " type "\n"
4364             "| Filename:       " filename "\n"
4365             "| Size (encoded): " bsize " Byte\n"
4366             "| Description:    " description "\n"
4367             "`----\n"))
4368           (setcdr data
4369                   (cdr (mm-make-handle
4370                         nil `("text/plain") nil nil
4371                         (list "attachment")
4372                         (format "Deleted attachment (%s bytes)" bsize))))))
4373       ;; (set-buffer gnus-summary-buffer)
4374       (gnus-article-edit-part handles id))))
4375
4376 (defun gnus-mime-save-part ()
4377   "Save the MIME part under point."
4378   (interactive)
4379   (gnus-article-check-buffer)
4380   (let ((data (get-text-property (point) 'gnus-data)))
4381     (when data
4382       (mm-save-part data))))
4383
4384 (defun gnus-mime-pipe-part ()
4385   "Pipe the MIME part under point to a process."
4386   (interactive)
4387   (gnus-article-check-buffer)
4388   (let ((data (get-text-property (point) 'gnus-data)))
4389     (when data
4390       (mm-pipe-part data))))
4391
4392 (defun gnus-mime-view-part ()
4393   "Interactively choose a viewing method for the MIME part under point."
4394   (interactive)
4395   (gnus-article-check-buffer)
4396   (let ((data (get-text-property (point) 'gnus-data)))
4397     (when data
4398       (setq gnus-article-mime-handles
4399             (mm-merge-handles
4400              gnus-article-mime-handles (setq data (copy-sequence data))))
4401       (mm-interactively-view-part data))))
4402
4403 (defun gnus-mime-view-part-as-type-internal ()
4404   (gnus-article-check-buffer)
4405   (let* ((handle (get-text-property (point) 'gnus-data))
4406          (name (or
4407                 ;; Content-Type: foo/bar; name=...
4408                 (mail-content-type-get (mm-handle-type handle) 'name)
4409                 ;; Content-Disposition: attachment; filename=...
4410                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4411          (def-type (and name (mm-default-file-encoding name))))
4412     (and def-type (cons def-type 0))))
4413
4414 (defun gnus-mime-view-part-as-type (&optional mime-type)
4415   "Choose a MIME media type, and view the part as such."
4416   (interactive)
4417   (unless mime-type
4418     (setq mime-type
4419           (let ((default (gnus-mime-view-part-as-type-internal)))
4420             (completing-read
4421              (format "View as MIME type (default %s): "
4422                      (car default))
4423              (mapcar #'list (mailcap-mime-types))
4424              nil nil nil nil
4425              (car default)))))
4426   (gnus-article-check-buffer)
4427   (let ((handle (get-text-property (point) 'gnus-data)))
4428     (when handle
4429       (setq handle
4430             (mm-make-handle (mm-handle-buffer handle)
4431                             (cons mime-type (cdr (mm-handle-type handle)))
4432                             (mm-handle-encoding handle)
4433                             (mm-handle-undisplayer handle)
4434                             (mm-handle-disposition handle)
4435                             (mm-handle-description handle)
4436                             nil
4437                             (mm-handle-id handle)))
4438       (setq gnus-article-mime-handles
4439             (mm-merge-handles gnus-article-mime-handles handle))
4440       (gnus-mm-display-part handle))))
4441
4442 (defun gnus-mime-copy-part (&optional handle arg)
4443   "Put the MIME part under point into a new buffer.
4444 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4445 are decompressed."
4446   (interactive (list nil current-prefix-arg))
4447   (gnus-article-check-buffer)
4448   (unless handle
4449     (setq handle (get-text-property (point) 'gnus-data)))
4450   (when handle
4451     (let ((filename (or (mail-content-type-get (mm-handle-disposition handle)
4452                                                'name)
4453                         (mail-content-type-get (mm-handle-disposition handle)
4454                                                'filename)))
4455           contents dont-decode charset coding-system)
4456       (mm-with-unibyte-buffer
4457         (mm-insert-part handle)
4458         (setq contents (or (condition-case nil
4459                                (mm-decompress-buffer filename nil 'sig)
4460                              (error
4461                               (setq dont-decode t)
4462                               nil))
4463                            (buffer-string))))
4464       (setq filename (cond (filename (file-name-nondirectory filename))
4465                            (dont-decode "*raw data*")
4466                            (t "*decoded*")))
4467       (cond
4468        (dont-decode)
4469        ((not arg)
4470         (unless (setq charset (mail-content-type-get
4471                                (mm-handle-type handle) 'charset))
4472           (unless (setq coding-system (mm-with-unibyte-buffer
4473                                         (insert contents)
4474                                         (mm-find-buffer-file-coding-system)))
4475             (setq charset gnus-newsgroup-charset))))
4476        ((numberp arg)
4477         (setq charset (or (cdr (assq arg
4478                                      gnus-summary-show-article-charset-alist))
4479                           (mm-read-coding-system "Charset: ")))))
4480       (switch-to-buffer (generate-new-buffer filename))
4481       (if (or coding-system
4482               (and charset
4483                    (setq coding-system (mm-charset-to-coding-system charset))
4484                    (not (eq charset 'ascii))))
4485           (progn
4486             (mm-enable-multibyte)
4487             (insert (mm-decode-coding-string contents coding-system))
4488             (setq buffer-file-coding-system
4489                   (if (boundp 'last-coding-system-used)
4490                       (symbol-value 'last-coding-system-used)
4491                     coding-system)))
4492         (mm-disable-multibyte)
4493         (insert contents)
4494         (setq buffer-file-coding-system mm-binary-coding-system))
4495       ;; We do it this way to make `normal-mode' set the appropriate mode.
4496       (unwind-protect
4497           (progn
4498             (setq buffer-file-name (expand-file-name filename))
4499             (normal-mode))
4500         (setq buffer-file-name nil))
4501       (goto-char (point-min)))))
4502
4503 (defun gnus-mime-print-part (&optional handle filename)
4504   "Print the MIME part under point."
4505   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4506   (gnus-article-check-buffer)
4507   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4508          (contents (and handle (mm-get-part handle)))
4509          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4510          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4511     (when contents
4512         (if printer
4513             (unwind-protect
4514                 (progn
4515                   (mm-save-part-to-file handle file)
4516                   (call-process shell-file-name nil
4517                                 (generate-new-buffer " *mm*")
4518                                 nil
4519                                 shell-command-switch
4520                                 (mm-mailcap-command
4521                                  printer file (mm-handle-type handle))))
4522               (delete-file file))
4523           (with-temp-buffer
4524             (insert contents)
4525             (gnus-print-buffer))
4526           (ps-despool filename)))))
4527
4528 (defun gnus-mime-inline-part (&optional handle arg)
4529   "Insert the MIME part under point into the current buffer.
4530 Compressed files like .gz and .bz2 are decompressed."
4531   (interactive (list nil current-prefix-arg))
4532   (gnus-article-check-buffer)
4533   (unless handle
4534     (setq handle (get-text-property (point) 'gnus-data)))
4535   (when handle
4536     (let ((b (point))
4537           (inhibit-read-only t)
4538           contents charset coding-system)
4539       (if (and (not arg) (mm-handle-undisplayer handle))
4540           (mm-remove-part handle)
4541         (mm-with-unibyte-buffer
4542           (mm-insert-part handle)
4543           (setq contents
4544                 (or (mm-decompress-buffer
4545                      (or (mail-content-type-get (mm-handle-disposition handle)
4546                                                 'name)
4547                          (mail-content-type-get (mm-handle-disposition handle)
4548                                                 'filename))
4549                      nil t)
4550                     (buffer-string))))
4551         (cond
4552          ((not arg)
4553           (unless (setq charset (mail-content-type-get
4554                                  (mm-handle-type handle) 'charset))
4555             (unless (setq coding-system
4556                           (mm-with-unibyte-buffer
4557                             (insert contents)
4558                             (mm-find-buffer-file-coding-system)))
4559               (setq charset gnus-newsgroup-charset))))
4560          ((numberp arg)
4561           (if (mm-handle-undisplayer handle)
4562               (mm-remove-part handle))
4563           (setq charset
4564                 (or (cdr (assq arg
4565                                gnus-summary-show-article-charset-alist))
4566                     (mm-read-coding-system "Charset: "))))
4567          (t
4568           (if (mm-handle-undisplayer handle)
4569               (mm-remove-part handle))))
4570         (forward-line 2)
4571         (mm-insert-inline
4572          handle
4573          (if (or coding-system
4574                  (and charset
4575                       (setq coding-system
4576                             (mm-charset-to-coding-system charset))
4577                       (not (eq coding-system 'ascii))))
4578              (mm-decode-coding-string contents coding-system)
4579            (mm-string-to-multibyte contents)))
4580         (goto-char b)))))
4581
4582 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4583   "Insert the MIME part under point into the current buffer using the
4584 specified charset."
4585   (interactive (list nil current-prefix-arg))
4586   (gnus-article-check-buffer)
4587   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4588          contents charset
4589          (b (point))
4590          (inhibit-read-only t))
4591     (when handle
4592       (if (mm-handle-undisplayer handle)
4593           (mm-remove-part handle))
4594       (let ((gnus-newsgroup-charset
4595              (or (cdr (assq arg
4596                             gnus-summary-show-article-charset-alist))
4597                  (mm-read-coding-system "Charset: ")))
4598           (gnus-newsgroup-ignored-charsets 'gnus-all))
4599         (gnus-article-press-button)))))
4600
4601 (defun gnus-mime-view-part-externally (&optional handle)
4602   "View the MIME part under point with an external viewer."
4603   (interactive)
4604   (gnus-article-check-buffer)
4605   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4606          (mm-user-display-methods nil)
4607          (mm-inlined-types nil)
4608          (mail-parse-charset gnus-newsgroup-charset)
4609          (mail-parse-ignored-charsets
4610           (with-current-buffer gnus-summary-buffer
4611             gnus-newsgroup-ignored-charsets)))
4612     (when handle
4613       (if (mm-handle-undisplayer handle)
4614           (mm-remove-part handle)
4615         (mm-display-part handle)))))
4616
4617 (defun gnus-mime-view-part-internally (&optional handle)
4618   "View the MIME part under point with an internal viewer.
4619 If no internal viewer is available, use an external viewer."
4620   (interactive)
4621   (gnus-article-check-buffer)
4622   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4623          (mm-inlined-types '(".*"))
4624          (mm-inline-large-images t)
4625          (mail-parse-charset gnus-newsgroup-charset)
4626          (mail-parse-ignored-charsets
4627           (with-current-buffer gnus-summary-buffer
4628             gnus-newsgroup-ignored-charsets))
4629          (inhibit-read-only t))
4630     (when handle
4631       (if (mm-handle-undisplayer handle)
4632           (mm-remove-part handle)
4633         (mm-display-part handle)))))
4634
4635 (defun gnus-mime-action-on-part (&optional action)
4636   "Do something with the MIME attachment at \(point\)."
4637   (interactive
4638    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4639   (gnus-article-check-buffer)
4640   (let ((action-pair (assoc action gnus-mime-action-alist)))
4641     (if action-pair
4642         (funcall (cdr action-pair)))))
4643
4644 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
4645   "Call FUNCTION on MIME part N.
4646 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
4647 If INTERACTIVE, call FUNCTION interactivly."
4648   (let (window frame)
4649     ;; Check whether the article is displayed.
4650     (unless (and (gnus-buffer-live-p gnus-article-buffer)
4651                  (setq window (get-buffer-window gnus-article-buffer t))
4652                  (frame-visible-p (setq frame (window-frame window))))
4653       (error "No article is displayed"))
4654     (with-current-buffer gnus-article-buffer
4655       ;; Check whether the article displays the right contents.
4656       (unless (with-current-buffer gnus-summary-buffer
4657                 (eq gnus-current-article (gnus-summary-article-number)))
4658         (error "You should select the right article first"))
4659       ;; Check whether the specified part exists.
4660       (when (> n (length gnus-article-mime-handle-alist))
4661         (error "No such part")))
4662     (unless
4663         (progn
4664           ;; To select the window is needed so that the cursor
4665           ;; might be visible on the MIME button.
4666           (select-window (prog1
4667                              window
4668                            (setq window (selected-window))
4669                            ;; Article may be displayed in the other frame.
4670                            (gnus-select-frame-set-input-focus
4671                             (prog1
4672                                 frame
4673                               (setq frame (selected-frame))))))
4674           (when (gnus-article-goto-part n)
4675             ;; We point the cursor and the arrow at the MIME button
4676             ;; when the `function' prompt the user for something.
4677             (let ((cursor-in-non-selected-windows t)
4678                   (overlay-arrow-string "=>")
4679                   (overlay-arrow-position (point-marker)))
4680               (unwind-protect
4681                   (cond
4682                    ((and no-handle interactive)
4683                     (call-interactively function))
4684                    (no-handle
4685                     (funcall function))
4686                    (interactive
4687                     (call-interactively
4688                      function
4689                      (cdr (assq n gnus-article-mime-handle-alist))))
4690                    (t
4691                     (funcall function
4692                              (cdr (assq n gnus-article-mime-handle-alist)))))
4693                 (set-marker overlay-arrow-position nil)
4694                 (unless gnus-auto-select-part
4695                   (gnus-select-frame-set-input-focus frame)
4696                   (select-window window))))
4697             t))
4698       (if gnus-inhibit-mime-unbuttonizing
4699           ;; This is the default though the program shouldn't reach here.
4700           (error "No such part")
4701         ;; The part which doesn't have the MIME button is selected.
4702         ;; So, we display all the buttons and redo it.
4703         (let ((gnus-inhibit-mime-unbuttonizing t))
4704           (gnus-summary-show-article)
4705           (gnus-article-part-wrapper n function no-handle))))))
4706
4707 (defun gnus-article-pipe-part (n)
4708   "Pipe MIME part N, which is the numerical prefix."
4709   (interactive "p")
4710   (gnus-article-part-wrapper n 'mm-pipe-part))
4711
4712 (defun gnus-article-save-part (n)
4713   "Save MIME part N, which is the numerical prefix."
4714   (interactive "p")
4715   (gnus-article-part-wrapper n 'mm-save-part))
4716
4717 (defun gnus-article-interactively-view-part (n)
4718   "View MIME part N interactively, which is the numerical prefix."
4719   (interactive "p")
4720   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4721
4722 (defun gnus-article-copy-part (n)
4723   "Copy MIME part N, which is the numerical prefix."
4724   (interactive "p")
4725   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4726
4727 (defun gnus-article-view-part-as-charset (n)
4728   "View MIME part N using a specified charset.
4729 N is the numerical prefix."
4730   (interactive "p")
4731   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4732
4733 (defun gnus-article-view-part-externally (n)
4734   "View MIME part N externally, which is the numerical prefix."
4735   (interactive "p")
4736   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4737
4738 (defun gnus-article-inline-part (n)
4739   "Inline MIME part N, which is the numerical prefix."
4740   (interactive "p")
4741   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4742
4743 (defun gnus-article-save-part-and-strip (n)
4744   "Save MIME part N and replace it with an external body.
4745 N is the numerical prefix."
4746   (interactive "p")
4747   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
4748
4749 (defun gnus-article-replace-part (n)
4750   "Replace MIME part N with an external body.
4751 N is the numerical prefix."
4752   (interactive "p")
4753   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
4754
4755 (defun gnus-article-delete-part (n)
4756   "Delete MIME part N and add some information about the removed part.
4757 N is the numerical prefix."
4758   (interactive "p")
4759   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
4760
4761 (defun gnus-article-mime-match-handle-first (condition)
4762   (if condition
4763       (let (n)
4764         (dolist (ihandle gnus-article-mime-handle-alist)
4765           (if (and (cond
4766                     ((functionp condition)
4767                      (funcall condition (cdr ihandle)))
4768                     ((eq condition 'undisplayed)
4769                      (not (or (mm-handle-undisplayer (cdr ihandle))
4770                               (equal (mm-handle-media-type (cdr ihandle))
4771                                      "multipart/alternative"))))
4772                     ((eq condition 'undisplayed-alternative)
4773                      (not (mm-handle-undisplayer (cdr ihandle))))
4774                     (t t))
4775                    (gnus-article-goto-part (car ihandle))
4776                    (or (not n) (< (car ihandle) n)))
4777               (setq n (car ihandle))))
4778         (or n 1))
4779     1))
4780
4781 (defun gnus-article-view-part (&optional n)
4782   "View MIME part N, which is the numerical prefix."
4783   (interactive "P")
4784   (with-current-buffer gnus-article-buffer
4785     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4786                              gnus-article-mime-match-handle-function)))
4787     (when (> n (length gnus-article-mime-handle-alist))
4788       (error "No such part"))
4789     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4790       (when (gnus-article-goto-part n)
4791         (if (equal (car handle) "multipart/alternative")
4792             (gnus-article-press-button)
4793           (when (eq (gnus-mm-display-part handle) 'internal)
4794             (gnus-set-window-start)))))))
4795
4796 (defsubst gnus-article-mime-total-parts ()
4797   (if (bufferp (car gnus-article-mime-handles))
4798       1 ;; single part
4799     (1- (length gnus-article-mime-handles))))
4800
4801 (defun gnus-mm-display-part (handle)
4802   "Display HANDLE and fix MIME button."
4803   (let ((id (get-text-property (point) 'gnus-part))
4804         (point (point))
4805         (inhibit-read-only t))
4806     (forward-line 1)
4807     (prog1
4808         (let ((window (selected-window))
4809               (mail-parse-charset gnus-newsgroup-charset)
4810               (mail-parse-ignored-charsets
4811                (if (gnus-buffer-live-p gnus-summary-buffer)
4812                    (with-current-buffer gnus-summary-buffer
4813                      gnus-newsgroup-ignored-charsets)
4814                  nil)))
4815           (save-excursion
4816             (unwind-protect
4817                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4818                       (beg (point)))
4819                   (when win
4820                     (select-window win))
4821                   (goto-char point)
4822                   (forward-line)
4823                   (if (mm-handle-displayed-p handle)
4824                       ;; This will remove the part.
4825                       (mm-display-part handle)
4826                     (save-restriction
4827                       (narrow-to-region (point)
4828                                         (if (eobp) (point) (1+ (point))))
4829                       (mm-display-part handle)
4830                       ;; We narrow to the part itself and
4831                       ;; then call the treatment functions.
4832                       (goto-char (point-min))
4833                       (forward-line 1)
4834                       (narrow-to-region (point) (point-max))
4835                       (gnus-treat-article
4836                        nil id
4837                        (gnus-article-mime-total-parts)
4838                        (mm-handle-media-type handle)))))
4839               (if (window-live-p window)
4840                   (select-window window)))))
4841       (goto-char point)
4842       (gnus-delete-line)
4843       (gnus-insert-mime-button
4844        handle id (list (mm-handle-displayed-p handle)))
4845       (goto-char point))))
4846
4847 (defun gnus-article-goto-part (n)
4848   "Go to MIME part N."
4849   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4850
4851 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4852   (let ((gnus-tmp-name
4853          (or (mail-content-type-get (mm-handle-type handle) 'name)
4854              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4855              (mail-content-type-get (mm-handle-type handle) 'url)
4856              ""))
4857         (gnus-tmp-type (mm-handle-media-type handle))
4858         (gnus-tmp-description
4859          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4860                                               "")))
4861         (gnus-tmp-dots
4862          (if (if displayed (car displayed)
4863                (mm-handle-displayed-p handle))
4864              "" "..."))
4865         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4866                            (buffer-size)))
4867         gnus-tmp-type-long b e)
4868     (when (string-match ".*/" gnus-tmp-name)
4869       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4870     (setq gnus-tmp-type-long (concat gnus-tmp-type
4871                                      (and (not (equal gnus-tmp-name ""))
4872                                           (concat "; " gnus-tmp-name))))
4873     (unless (equal gnus-tmp-description "")
4874       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4875     (unless (bolp)
4876       (insert "\n"))
4877     (setq b (point))
4878     (gnus-eval-format
4879      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4880      `(keymap ,gnus-mime-button-map
4881               gnus-callback gnus-mm-display-part
4882               gnus-part ,gnus-tmp-id
4883               article-type annotation
4884               gnus-data ,handle))
4885     (setq e (if (bolp)
4886                 ;; Exclude a newline.
4887                 (1- (point))
4888               (point)))
4889     (widget-convert-button
4890      'link b e
4891      :mime-handle handle
4892      :action 'gnus-widget-press-button
4893      :button-keymap gnus-mime-button-map
4894      :help-echo
4895      (lambda (widget/window &optional overlay pos)
4896        ;; Needed to properly clear the message due to a bug in
4897        ;; wid-edit (XEmacs only).
4898        (if (boundp 'help-echo-owns-message)
4899            (setq help-echo-owns-message t))
4900        (format
4901         "%S: %s the MIME part; %S: more options"
4902         (aref gnus-mouse-2 0)
4903         ;; XEmacs will get a single widget arg; Emacs 21 will get
4904         ;; window, overlay, position.
4905         (if (mm-handle-displayed-p
4906              (if overlay
4907                  (with-current-buffer (gnus-overlay-buffer overlay)
4908                    (widget-get (widget-at (gnus-overlay-start overlay))
4909                                :mime-handle))
4910                (widget-get widget/window :mime-handle)))
4911             "hide" "show")
4912         (aref gnus-down-mouse-3 0))))))
4913
4914 (defun gnus-widget-press-button (elems el)
4915   (goto-char (widget-get elems :from))
4916   (gnus-article-press-button))
4917
4918 (defvar gnus-displaying-mime nil)
4919
4920 (defun gnus-display-mime (&optional ihandles)
4921   "Display the MIME parts."
4922   (save-excursion
4923     (save-selected-window
4924       (let ((window (get-buffer-window gnus-article-buffer))
4925             (point (point)))
4926         (when window
4927           (select-window window)
4928           ;; We have to do this since selecting the window
4929           ;; may change the point.  So we set the window point.
4930           (set-window-point window point)))
4931       (let ((handles ihandles)
4932             (inhibit-read-only t)
4933             handle)
4934         (cond (handles)
4935               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
4936                (when gnus-article-emulate-mime
4937                  (mm-uu-dissect-text-parts handles)))
4938               (gnus-article-emulate-mime
4939                (setq handles (mm-uu-dissect))))
4940         (when (and (not ihandles)
4941                    (not gnus-displaying-mime))
4942           ;; Top-level call; we clean up.
4943           (when gnus-article-mime-handles
4944             (mm-destroy-parts gnus-article-mime-handles)
4945             (setq gnus-article-mime-handle-alist nil));; A trick.
4946           (setq gnus-article-mime-handles handles)
4947           ;; We allow users to glean info from the handles.
4948           (when gnus-article-mime-part-function
4949             (gnus-mime-part-function handles)))
4950         (if (and handles
4951                  (or (not (stringp (car handles)))
4952                      (cdr handles)))
4953             (progn
4954               (when (and (not ihandles)
4955                          (not gnus-displaying-mime))
4956                 ;; Clean up for mime parts.
4957                 (article-goto-body)
4958                 (delete-region (point) (point-max)))
4959               (let ((gnus-displaying-mime t))
4960                 (gnus-mime-display-part handles)))
4961           (save-restriction
4962             (article-goto-body)
4963             (narrow-to-region (point) (point-max))
4964             (gnus-treat-article nil 1 1)
4965             (widen)))
4966         (unless ihandles
4967           ;; Highlight the headers.
4968           (save-excursion
4969             (save-restriction
4970               (article-goto-body)
4971               (narrow-to-region (point-min) (point))
4972               (gnus-article-save-original-date
4973                (gnus-treat-article 'head)))))))))
4974
4975 (defcustom gnus-mime-display-multipart-as-mixed nil
4976   "Display \"multipart\" parts as  \"multipart/mixed\".
4977
4978 If t, it overrides nil values of
4979 `gnus-mime-display-multipart-alternative-as-mixed' and
4980 `gnus-mime-display-multipart-related-as-mixed'."
4981   :group 'gnus-article-mime
4982   :type 'boolean)
4983
4984 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
4985   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
4986   :version "22.1"
4987   :group 'gnus-article-mime
4988   :type 'boolean)
4989
4990 (defcustom gnus-mime-display-multipart-related-as-mixed nil
4991   "Display \"multipart/related\" parts as  \"multipart/mixed\".
4992
4993 If displaying \"text/html\" is discouraged \(see
4994 `mm-discouraged-alternatives'\) images or other material inside a
4995 \"multipart/related\" part might be overlooked when this variable is nil."
4996   :version "22.1"
4997   :group 'gnus-article-mime
4998   :type 'boolean)
4999
5000 (defun gnus-mime-display-part (handle)
5001   (cond
5002    ;; Maybe a broken MIME message.
5003    ((null handle))
5004    ;; Single part.
5005    ((not (stringp (car handle)))
5006     (gnus-mime-display-single handle))
5007    ;; User-defined multipart
5008    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5009     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5010              handle))
5011    ;; multipart/alternative
5012    ((and (equal (car handle) "multipart/alternative")
5013          (not (or gnus-mime-display-multipart-as-mixed
5014                   gnus-mime-display-multipart-alternative-as-mixed)))
5015     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5016       (push (cons id handle) gnus-article-mime-handle-alist)
5017       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5018    ;; multipart/related
5019    ((and (equal (car handle) "multipart/related")
5020          (not (or gnus-mime-display-multipart-as-mixed
5021                   gnus-mime-display-multipart-related-as-mixed)))
5022     ;;;!!!We should find the start part, but we just default
5023     ;;;!!!to the first part.
5024     ;;(gnus-mime-display-part (cadr handle))
5025     ;;;!!! Most multipart/related is an HTML message plus images.
5026     ;;;!!! Unfortunately we are unable to let W3 display those
5027     ;;;!!! included images, so we just display it as a mixed multipart.
5028     ;;(gnus-mime-display-mixed (cdr handle))
5029     ;;;!!! No, w3 can display everything just fine.
5030     (gnus-mime-display-part (cadr handle)))
5031    ((equal (car handle) "multipart/signed")
5032     (gnus-add-wash-type 'signed)
5033     (gnus-mime-display-security handle))
5034    ((equal (car handle) "multipart/encrypted")
5035     (gnus-add-wash-type 'encrypted)
5036     (gnus-mime-display-security handle))
5037    ;; Other multiparts are handled like multipart/mixed.
5038    (t
5039     (gnus-mime-display-mixed (cdr handle)))))
5040
5041 (defun gnus-mime-part-function (handles)
5042   (if (stringp (car handles))
5043       (mapcar 'gnus-mime-part-function (cdr handles))
5044     (funcall gnus-article-mime-part-function handles)))
5045
5046 (defun gnus-mime-display-mixed (handles)
5047   (mapcar 'gnus-mime-display-part handles))
5048
5049 (defun gnus-mime-display-single (handle)
5050   (let ((type (mm-handle-media-type handle))
5051         (ignored gnus-ignored-mime-types)
5052         (not-attachment t)
5053         (move nil)
5054         display text)
5055     (catch 'ignored
5056       (progn
5057         (while ignored
5058           (when (string-match (pop ignored) type)
5059             (throw 'ignored nil)))
5060         (if (and (setq not-attachment
5061                        (and (not (mm-inline-override-p handle))
5062                             (or (not (mm-handle-disposition handle))
5063                                 (equal (car (mm-handle-disposition handle))
5064                                        "inline")
5065                                 (mm-attachment-override-p handle))))
5066                  (mm-automatic-display-p handle)
5067                  (or (and
5068                       (mm-inlinable-p handle)
5069                       (mm-inlined-p handle))
5070                      (mm-automatic-external-display-p type)))
5071             (setq display t)
5072           (when (equal (mm-handle-media-supertype handle) "text")
5073             (setq text t)))
5074         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5075               beg)
5076           (push (cons id handle) gnus-article-mime-handle-alist)
5077           (when (or (not display)
5078                     (not (gnus-unbuttonized-mime-type-p type)))
5079             (gnus-insert-mime-button
5080              handle id (list (or display (and not-attachment text))))
5081             (gnus-article-insert-newline)
5082             ;; Remember modify the number of forward lines.
5083             (setq move t))
5084           (setq beg (point))
5085           (cond
5086            (display
5087             (when move
5088               (forward-line -1)
5089               (setq beg (point)))
5090             (let ((mail-parse-charset gnus-newsgroup-charset)
5091                   (mail-parse-ignored-charsets
5092                    (save-excursion (condition-case ()
5093                                        (set-buffer gnus-summary-buffer)
5094                                      (error))
5095                                    gnus-newsgroup-ignored-charsets)))
5096               (mm-display-part handle t))
5097             (goto-char (point-max)))
5098            ((and text not-attachment)
5099             (when move
5100               (forward-line -1)
5101               (setq beg (point)))
5102             (gnus-article-insert-newline)
5103             (mm-insert-inline
5104              handle
5105              (let ((charset (mail-content-type-get (mm-handle-type handle)
5106                                                    'charset)))
5107                (cond ((not charset)
5108                       (mm-string-as-multibyte (mm-get-part handle)))
5109                      ((eq charset 'gnus-decoded)
5110                       (with-current-buffer (mm-handle-buffer handle)
5111                         (buffer-string)))
5112                      (t
5113                       (mm-decode-string (mm-get-part handle) charset)))))
5114             (goto-char (point-max))))
5115           ;; Do highlighting.
5116           (save-excursion
5117             (save-restriction
5118               (narrow-to-region beg (point))
5119               (gnus-treat-article
5120                nil id
5121                (gnus-article-mime-total-parts)
5122                (mm-handle-media-type handle)))))))))
5123
5124 (defun gnus-unbuttonized-mime-type-p (type)
5125   "Say whether TYPE is to be unbuttonized."
5126   (unless gnus-inhibit-mime-unbuttonizing
5127     (when (catch 'found
5128             (let ((types gnus-unbuttonized-mime-types))
5129               (while types
5130                 (when (string-match (pop types) type)
5131                   (throw 'found t)))))
5132       (not (catch 'found
5133              (let ((types gnus-buttonized-mime-types))
5134                (while types
5135                  (when (string-match (pop types) type)
5136                    (throw 'found t)))))))))
5137
5138 (defun gnus-article-insert-newline ()
5139   "Insert a newline, but mark it as undeletable."
5140   (gnus-put-text-property
5141    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5142
5143 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5144   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5145          (ihandles handles)
5146          (point (point))
5147          handle (inhibit-read-only t) from props begend not-pref)
5148     (save-window-excursion
5149       (save-restriction
5150         (when ibegend
5151           (narrow-to-region (car ibegend)
5152                             (or (cdr ibegend)
5153                                 (progn
5154                                   (goto-char (car ibegend))
5155                                   (forward-line 2)
5156                                   (point))))
5157           (delete-region (point-min) (point-max))
5158           (mm-remove-parts handles))
5159         (setq begend (list (point-marker)))
5160         ;; Do the toggle.
5161         (unless (setq not-pref (cadr (member preferred ihandles)))
5162           (setq not-pref (car ihandles)))
5163         (when (or ibegend
5164                   (not preferred)
5165                   (not (gnus-unbuttonized-mime-type-p
5166                         "multipart/alternative")))
5167           (gnus-add-text-properties
5168            (setq from (point))
5169            (progn
5170              (insert (format "%d.  " id))
5171              (point))
5172            `(gnus-callback
5173              (lambda (handles)
5174                (unless ,(not ibegend)
5175                  (setq gnus-article-mime-handle-alist
5176                        ',gnus-article-mime-handle-alist))
5177                (gnus-mime-display-alternative
5178                 ',ihandles ',not-pref ',begend ,id))
5179              keymap ,gnus-mime-button-map
5180              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5181              face ,gnus-article-button-face
5182              gnus-part ,id
5183              article-type multipart))
5184           (widget-convert-button 'link from (point)
5185                                  :action 'gnus-widget-press-button
5186                                  :button-keymap gnus-widget-button-keymap)
5187           ;; Do the handles
5188           (while (setq handle (pop handles))
5189             (gnus-add-text-properties
5190              (setq from (point))
5191              (progn
5192                (insert (format "(%c) %-18s"
5193                                (if (equal handle preferred) ?* ? )
5194                                (mm-handle-media-type handle)))
5195                (point))
5196              `(gnus-callback
5197                (lambda (handles)
5198                  (unless ,(not ibegend)
5199                    (setq gnus-article-mime-handle-alist
5200                          ',gnus-article-mime-handle-alist))
5201                  (gnus-mime-display-alternative
5202                   ',ihandles ',handle ',begend ,id))
5203                keymap ,gnus-mime-button-map
5204                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5205                face ,gnus-article-button-face
5206                gnus-part ,id
5207                gnus-data ,handle))
5208             (widget-convert-button 'link from (point)
5209                                    :action 'gnus-widget-press-button
5210                                    :button-keymap gnus-widget-button-keymap)
5211             (insert "  "))
5212           (insert "\n\n"))
5213         (when preferred
5214           (if (stringp (car preferred))
5215               (gnus-display-mime preferred)
5216             (let ((mail-parse-charset gnus-newsgroup-charset)
5217                   (mail-parse-ignored-charsets
5218                    (with-current-buffer gnus-summary-buffer
5219                      gnus-newsgroup-ignored-charsets)))
5220               (mm-display-part preferred)
5221               ;; Do highlighting.
5222               (save-excursion
5223                 (save-restriction
5224                   (narrow-to-region (car begend) (point-max))
5225                   (gnus-treat-article
5226                    nil (length gnus-article-mime-handle-alist)
5227                    (gnus-article-mime-total-parts)
5228                    (mm-handle-media-type handle))))))
5229           (goto-char (point-max))
5230           (setcdr begend (point-marker)))))
5231     (when ibegend
5232       (goto-char point))))
5233
5234 (defconst gnus-article-wash-status-strings
5235   (let ((alist '((cite "c" "Possible hidden citation text"
5236                        " " "All citation text visible")
5237                  (headers "h" "Hidden headers"
5238                           " " "All headers visible.")
5239                  (pgp "p" "Encrypted or signed message status hidden"
5240                       " " "No hidden encryption nor digital signature status")
5241                  (signature "s" "Signature has been hidden"
5242                             " " "Signature is visible")
5243                  (overstrike "o" "Overstrike (^H) characters applied"
5244                              " " "No overstrike characters applied")
5245                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5246                            " " "No /*_emphasis_*/ characters applied")))
5247         result)
5248     (dolist (entry alist result)
5249       (let ((key (nth 0 entry))
5250             (on (copy-sequence (nth 1 entry)))
5251             (on-help (nth 2 entry))
5252             (off (copy-sequence (nth 3 entry)))
5253             (off-help (nth 4 entry)))
5254         (put-text-property 0 1 'help-echo on-help on)
5255         (put-text-property 0 1 'help-echo off-help off)
5256         (push (list key on off) result))))
5257   "Alist of strings describing wash status in the mode line.
5258 Each entry has the form (KEY ON OF), where the KEY is a symbol
5259 representing the particular washing function, ON is the string to use
5260 in the article mode line when the washing function is active, and OFF
5261 is the string to use when it is inactive.")
5262
5263 (defun gnus-article-wash-status-entry (key value)
5264   (let ((entry (assoc key gnus-article-wash-status-strings)))
5265     (if value (nth 1 entry) (nth 2 entry))))
5266
5267 (defun gnus-article-wash-status ()
5268   "Return a string which display status of article washing."
5269   (with-current-buffer gnus-article-buffer
5270     (let ((cite (memq 'cite gnus-article-wash-types))
5271           (headers (memq 'headers gnus-article-wash-types))
5272           (boring (memq 'boring-headers gnus-article-wash-types))
5273           (pgp (memq 'pgp gnus-article-wash-types))
5274           (pem (memq 'pem gnus-article-wash-types))
5275           (signed (memq 'signed gnus-article-wash-types))
5276           (encrypted (memq 'encrypted gnus-article-wash-types))
5277           (signature (memq 'signature gnus-article-wash-types))
5278           (overstrike (memq 'overstrike gnus-article-wash-types))
5279           (emphasis (memq 'emphasis gnus-article-wash-types)))
5280       (concat
5281        (gnus-article-wash-status-entry 'cite cite)
5282        (gnus-article-wash-status-entry 'headers (or headers boring))
5283        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5284        (gnus-article-wash-status-entry 'signature signature)
5285        (gnus-article-wash-status-entry 'overstrike overstrike)
5286        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5287
5288 (defun gnus-add-wash-type (type)
5289   "Add a washing of TYPE to the current status."
5290   (add-to-list 'gnus-article-wash-types type))
5291
5292 (defun gnus-delete-wash-type (type)
5293   "Add a washing of TYPE to the current status."
5294   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5295
5296 (defun gnus-add-image (category image)
5297   "Add IMAGE of CATEGORY to the list of displayed images."
5298   (let ((entry (assq category gnus-article-image-alist)))
5299     (unless entry
5300       (setq entry (list category))
5301       (push entry gnus-article-image-alist))
5302     (nconc entry (list image))))
5303
5304 (defun gnus-delete-images (category)
5305   "Delete all images in CATEGORY."
5306   (let ((entry (assq category gnus-article-image-alist)))
5307     (dolist (image (cdr entry))
5308       (gnus-remove-image image category))
5309     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5310     (gnus-delete-wash-type category)))
5311
5312 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5313
5314 (defun gnus-article-maybe-hide-headers ()
5315   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5316 Provided for backwards compatibility."
5317   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5318                  (not (with-current-buffer gnus-summary-buffer
5319                         gnus-have-all-headers)))
5320              (not gnus-inhibit-hiding))
5321     (gnus-article-hide-headers)))
5322
5323 ;;; Article savers.
5324
5325 (defun gnus-output-to-file (file-name)
5326   "Append the current article to a file named FILE-NAME."
5327   (let ((artbuf (current-buffer)))
5328     (with-temp-buffer
5329       (insert-buffer-substring artbuf)
5330       ;; Append newline at end of the buffer as separator, and then
5331       ;; save it to file.
5332       (goto-char (point-max))
5333       (insert "\n")
5334       (let ((file-name-coding-system nnmail-pathname-coding-system))
5335         (mm-append-to-file (point-min) (point-max) file-name))
5336       t)))
5337
5338 (defun gnus-narrow-to-page (&optional arg)
5339   "Narrow the article buffer to a page.
5340 If given a numerical ARG, move forward ARG pages."
5341   (interactive "P")
5342   (setq arg (if arg (prefix-numeric-value arg) 0))
5343   (save-excursion
5344     (set-buffer gnus-article-buffer)
5345     (goto-char (point-min))
5346     (widen)
5347     ;; Remove any old next/prev buttons.
5348     (when (gnus-visual-p 'page-marker)
5349       (let ((inhibit-read-only t))
5350         (gnus-remove-text-with-property 'gnus-prev)
5351         (gnus-remove-text-with-property 'gnus-next)))
5352     (if
5353         (cond ((< arg 0)
5354                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5355               ((> arg 0)
5356                (re-search-forward page-delimiter nil 'move arg)))
5357         (goto-char (match-end 0))
5358       (save-excursion
5359         (goto-char (point-min))
5360         (setq gnus-page-broken
5361               (and (re-search-forward page-delimiter nil t) t))))
5362     (when gnus-page-broken
5363       (narrow-to-region
5364        (point)
5365        (if (re-search-forward page-delimiter nil 'move)
5366            (match-beginning 0)
5367          (point)))
5368       (when (and (gnus-visual-p 'page-marker)
5369                  (> (point-min) (save-restriction (widen) (point-min))))
5370         (save-excursion
5371           (goto-char (point-min))
5372           (gnus-insert-prev-page-button)))
5373       (when (and (gnus-visual-p 'page-marker)
5374                  (< (point-max) (save-restriction (widen) (point-max))))
5375         (save-excursion
5376           (goto-char (point-max))
5377           (gnus-insert-next-page-button))))))
5378
5379 ;; Article mode commands
5380
5381 (defun gnus-article-goto-next-page ()
5382   "Show the next page of the article."
5383   (interactive)
5384   (when (gnus-article-next-page)
5385     (goto-char (point-min))
5386     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5387
5388
5389 (defun gnus-article-goto-prev-page ()
5390   "Show the previous page of the article."
5391   (interactive)
5392   (if (bobp)
5393       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5394     (gnus-article-prev-page nil)))
5395
5396 ;; This is cleaner but currently breaks `gnus-pick-mode':
5397 ;;
5398 ;; (defun gnus-article-goto-next-page ()
5399 ;;   "Show the next page of the article."
5400 ;;   (interactive)
5401 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5402 ;;     (gnus-summary-next-page)))
5403 ;;
5404 ;; (defun gnus-article-goto-prev-page ()
5405 ;;   "Show the next page of the article."
5406 ;;   (interactive)
5407 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5408 ;;     (gnus-summary-prev-page)))
5409
5410 (defun gnus-article-next-page (&optional lines)
5411   "Show the next page of the current article.
5412 If end of article, return non-nil.  Otherwise return nil.
5413 Argument LINES specifies lines to be scrolled up."
5414   (interactive "p")
5415   (move-to-window-line -1)
5416   (if (save-excursion
5417         (end-of-line)
5418         (and (pos-visible-in-window-p)  ;Not continuation line.
5419              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5420       ;; Nothing in this page.
5421       (if (or (not gnus-page-broken)
5422               (save-excursion
5423                 (save-restriction
5424                   (widen)
5425                   (forward-line)
5426                   (eobp)))) ;Real end-of-buffer?
5427           (progn
5428             (when gnus-article-over-scroll
5429               (gnus-article-next-page-1 lines))
5430             t)                  ;Nothing more.
5431         (gnus-narrow-to-page 1)         ;Go to next page.
5432         nil)
5433     ;; More in this page.
5434     (gnus-article-next-page-1 lines)
5435     nil))
5436
5437 (defmacro gnus-article-beginning-of-window ()
5438   "Move point to the beginning of the window.
5439 In Emacs, the point is placed at the line number which `scroll-margin'
5440 specifies."
5441   (if (featurep 'xemacs)
5442       '(move-to-window-line 0)
5443     '(move-to-window-line
5444       (min (max 0 scroll-margin)
5445            (max 1 (- (window-height)
5446                      (if mode-line-format 1 0)
5447                      (if header-line-format 1 0)))))))
5448
5449 (defun gnus-article-next-page-1 (lines)
5450   (when (and (not (featurep 'xemacs))
5451              (numberp lines)
5452              (> lines 0)
5453              (numberp (symbol-value 'scroll-margin))
5454              (> (symbol-value 'scroll-margin) 0))
5455     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
5456     ;; too many number of lines if `scroll-margin' is set as two or greater.
5457     (setq lines (min lines
5458                      (max 0 (- (count-lines (window-start) (point-max))
5459                                (symbol-value 'scroll-margin))))))
5460   (condition-case ()
5461       (let ((scroll-in-place nil))
5462         (scroll-up lines))
5463     (end-of-buffer
5464      ;; Long lines may cause an end-of-buffer error.
5465      (goto-char (point-max))))
5466   (gnus-article-beginning-of-window))
5467
5468 (defun gnus-article-prev-page (&optional lines)
5469   "Show previous page of current article.
5470 Argument LINES specifies lines to be scrolled down."
5471   (interactive "p")
5472   (move-to-window-line 0)
5473   (if (and gnus-page-broken
5474            (bobp)
5475            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5476       (progn
5477         (gnus-narrow-to-page -1)        ;Go to previous page.
5478         (goto-char (point-max))
5479         (recenter -1))
5480     (prog1
5481         (condition-case ()
5482             (let ((scroll-in-place nil))
5483               (scroll-down lines))
5484           (beginning-of-buffer
5485            (goto-char (point-min))))
5486       (gnus-article-beginning-of-window))))
5487
5488 (defun gnus-article-only-boring-p ()
5489   "Decide whether there is only boring text remaining in the article.
5490 Something \"interesting\" is a word of at least two letters that does
5491 not have a face in `gnus-article-boring-faces'."
5492   (when (and gnus-article-skip-boring
5493              (boundp 'gnus-article-boring-faces)
5494              (symbol-value 'gnus-article-boring-faces))
5495     (save-excursion
5496       (catch 'only-boring
5497         (while (re-search-forward "\\b\\w\\w" nil t)
5498           (forward-char -1)
5499           (when (not (gnus-intersection
5500                       (gnus-faces-at (point))
5501                       (symbol-value 'gnus-article-boring-faces)))
5502             (throw 'only-boring nil)))
5503         (throw 'only-boring t)))))
5504
5505 (defun gnus-article-refer-article ()
5506   "Read article specified by message-id around point."
5507   (interactive)
5508   (save-excursion
5509     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
5510     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
5511     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
5512         (let ((msg-id (concat "<" (match-string 0) ">")))
5513           (set-buffer gnus-summary-buffer)
5514           (gnus-summary-refer-article msg-id))
5515       (error "No references around point"))))
5516
5517 (defun gnus-article-show-summary ()
5518   "Reconfigure windows to show summary buffer."
5519   (interactive)
5520   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5521       (error "There is no summary buffer for this article buffer")
5522     (gnus-article-set-globals)
5523     (gnus-configure-windows 'article)
5524     (gnus-summary-goto-subject gnus-current-article)
5525     (gnus-summary-position-point)))
5526
5527 (defun gnus-article-describe-briefly ()
5528   "Describe article mode commands briefly."
5529   (interactive)
5530   (gnus-message 6 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page   \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
5531
5532 (defun gnus-article-summary-command ()
5533   "Execute the last keystroke in the summary buffer."
5534   (interactive)
5535   (let ((obuf (current-buffer))
5536         (owin (current-window-configuration))
5537         func)
5538     (switch-to-buffer gnus-article-current-summary 'norecord)
5539     (setq func (lookup-key (current-local-map) (this-command-keys)))
5540     (call-interactively func)
5541     (set-buffer obuf)
5542     (set-window-configuration owin)
5543     (set-window-point (get-buffer-window (current-buffer)) (point))))
5544
5545 (defun gnus-article-summary-command-nosave ()
5546   "Execute the last keystroke in the summary buffer."
5547   (interactive)
5548   (let (func)
5549     (pop-to-buffer gnus-article-current-summary 'norecord)
5550     (setq func (lookup-key (current-local-map) (this-command-keys)))
5551     (call-interactively func)))
5552
5553 (defun gnus-article-check-buffer ()
5554   "Beep if not in an article buffer."
5555   (unless (equal major-mode 'gnus-article-mode)
5556     (error "Command invoked outside of a Gnus article buffer")))
5557
5558 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5559   "Read a summary buffer key sequence and execute it from the article buffer."
5560   (interactive "P")
5561   (gnus-article-check-buffer)
5562   (let ((nosaves
5563          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5564            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5565            "=" "^" "\M-^" "|"))
5566         (nosave-but-article
5567          '("A\r"))
5568         (nosave-in-article
5569          '("\C-d"))
5570         (up-to-top
5571          '("n" "Gn" "p" "Gp"))
5572         keys new-sum-point)
5573     (save-excursion
5574       (set-buffer gnus-article-current-summary)
5575       (let (gnus-pick-mode)
5576         (push (or key last-command-event) unread-command-events)
5577         (setq keys (if (featurep 'xemacs)
5578                        (events-to-keys (read-key-sequence nil))
5579                      (read-key-sequence nil)))))
5580
5581     (message "")
5582
5583     (if (or (member keys nosaves)
5584             (member keys nosave-but-article)
5585             (member keys nosave-in-article))
5586         (let (func)
5587           (save-window-excursion
5588             (pop-to-buffer gnus-article-current-summary 'norecord)
5589             ;; We disable the pick minor mode commands.
5590             (let (gnus-pick-mode)
5591               (setq func (lookup-key (current-local-map) keys))))
5592           (if (or (not func)
5593                   (numberp func))
5594               (ding)
5595             (unless (member keys nosave-in-article)
5596               (set-buffer gnus-article-current-summary))
5597             (call-interactively func)
5598             (setq new-sum-point (point)))
5599           (when (member keys nosave-but-article)
5600             (pop-to-buffer gnus-article-buffer 'norecord)))
5601       ;; These commands should restore window configuration.
5602       (let ((obuf (current-buffer))
5603             (owin (current-window-configuration))
5604             (opoint (point))
5605             win func in-buffer selected new-sum-start new-sum-hscroll)
5606         (cond (not-restore-window
5607                (pop-to-buffer gnus-article-current-summary 'norecord))
5608               ((setq win (get-buffer-window gnus-article-current-summary))
5609                (select-window win))
5610               (t
5611                (switch-to-buffer gnus-article-current-summary 'norecord)))
5612         (setq in-buffer (current-buffer))
5613         ;; We disable the pick minor mode commands.
5614         (if (and (setq func (let (gnus-pick-mode)
5615                               (lookup-key (current-local-map) keys)))
5616                  (functionp func))
5617             (progn
5618               (call-interactively func)
5619               (when (eq win (selected-window))
5620                 (setq new-sum-point (point)
5621                       new-sum-start (window-start win)
5622                       new-sum-hscroll (window-hscroll win)))
5623               (when (eq in-buffer (current-buffer))
5624                 (setq selected (gnus-summary-select-article))
5625                 (set-buffer obuf)
5626                 (unless not-restore-window
5627                   (set-window-configuration owin))
5628                 (when (eq selected 'old)
5629                   (article-goto-body)
5630                   (set-window-start (get-buffer-window (current-buffer))
5631                                     1)
5632                   (set-window-point (get-buffer-window (current-buffer))
5633                                     (point)))
5634                 (when (and (not not-restore-window)
5635                            new-sum-point)
5636                   (set-window-point win new-sum-point)
5637                   (set-window-start win new-sum-start)
5638                   (set-window-hscroll win new-sum-hscroll))))
5639           (set-window-configuration owin)
5640           (ding))))))
5641
5642 (defun gnus-article-describe-key (key)
5643   "Display documentation of the function invoked by KEY.  KEY is a string."
5644   (interactive "kDescribe key: ")
5645   (gnus-article-check-buffer)
5646   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5647       (save-excursion
5648         (set-buffer gnus-article-current-summary)
5649         (let (gnus-pick-mode)
5650           (if (featurep 'xemacs)
5651               (progn
5652                 (push (elt key 0) unread-command-events)
5653                 (setq key (events-to-keys
5654                            (read-key-sequence "Describe key: "))))
5655             (setq unread-command-events
5656                   (mapcar
5657                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5658                    (string-to-list key)))
5659             (setq key (read-key-sequence "Describe key: "))))
5660         (describe-key key))
5661     (describe-key key)))
5662
5663 (defun gnus-article-describe-key-briefly (key &optional insert)
5664   "Display documentation of the function invoked by KEY.  KEY is a string."
5665   (interactive "kDescribe key: \nP")
5666   (gnus-article-check-buffer)
5667   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5668       (save-excursion
5669         (set-buffer gnus-article-current-summary)
5670         (let (gnus-pick-mode)
5671           (if (featurep 'xemacs)
5672               (progn
5673                 (push (elt key 0) unread-command-events)
5674                 (setq key (events-to-keys
5675                            (read-key-sequence "Describe key: "))))
5676             (setq unread-command-events
5677                   (mapcar
5678                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5679                    (string-to-list key)))
5680             (setq key (read-key-sequence "Describe key: "))))
5681         (describe-key-briefly key insert))
5682     (describe-key-briefly key insert)))
5683
5684 (defun gnus-article-reply-with-original (&optional wide)
5685   "Start composing a reply mail to the current message.
5686 The text in the region will be yanked.  If the region isn't active,
5687 the entire article will be yanked."
5688   (interactive "P")
5689   (let ((article (cdr gnus-article-current))
5690         contents)
5691     (if (not (gnus-region-active-p))
5692         (with-current-buffer gnus-summary-buffer
5693           (gnus-summary-reply (list (list article)) wide))
5694       (setq contents (buffer-substring (point) (mark t)))
5695       ;; Deactivate active regions.
5696       (when (and (boundp 'transient-mark-mode)
5697                  transient-mark-mode)
5698         (setq mark-active nil))
5699       (with-current-buffer gnus-summary-buffer
5700         (gnus-summary-reply
5701          (list (list article contents)) wide)))))
5702
5703 (defun gnus-article-followup-with-original ()
5704   "Compose a followup to the current article.
5705 The text in the region will be yanked.  If the region isn't active,
5706 the entire article will be yanked."
5707   (interactive)
5708   (let ((article (cdr gnus-article-current))
5709         contents)
5710       (if (not (gnus-region-active-p))
5711           (with-current-buffer gnus-summary-buffer
5712             (gnus-summary-followup (list (list article))))
5713         (setq contents (buffer-substring (point) (mark t)))
5714         ;; Deactivate active regions.
5715         (when (and (boundp 'transient-mark-mode)
5716                    transient-mark-mode)
5717           (setq mark-active nil))
5718         (with-current-buffer gnus-summary-buffer
5719           (gnus-summary-followup
5720            (list (list article contents)))))))
5721
5722 (defun gnus-article-hide (&optional arg force)
5723   "Hide all the gruft in the current article.
5724 This means that signatures, cited text and (some) headers will be
5725 hidden.
5726 If given a prefix, show the hidden text instead."
5727   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5728   (gnus-article-hide-headers arg)
5729   (gnus-article-hide-list-identifiers arg)
5730   (gnus-article-hide-citation-maybe arg force)
5731   (gnus-article-hide-signature arg))
5732
5733 (defun gnus-article-maybe-highlight ()
5734   "Do some article highlighting if article highlighting is requested."
5735   (when (gnus-visual-p 'article-highlight 'highlight)
5736     (gnus-article-highlight-some)))
5737
5738 (defun gnus-check-group-server ()
5739   ;; Make sure the connection to the server is alive.
5740   (unless (gnus-server-opened
5741            (gnus-find-method-for-group gnus-newsgroup-name))
5742     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5743     (gnus-request-group gnus-newsgroup-name t)))
5744
5745 (eval-when-compile
5746   (autoload 'nneething-get-file-name "nneething"))
5747
5748 (defun gnus-request-article-this-buffer (article group)
5749   "Get an article and insert it into this buffer."
5750   (let (do-update-line sparse-header)
5751     (prog1
5752         (save-excursion
5753           (erase-buffer)
5754           (gnus-kill-all-overlays)
5755           (setq group (or group gnus-newsgroup-name))
5756
5757           ;; Using `gnus-request-article' directly will insert the article into
5758           ;; `nntp-server-buffer' - so we'll save some time by not having to
5759           ;; copy it from the server buffer into the article buffer.
5760
5761           ;; We only request an article by message-id when we do not have the
5762           ;; headers for it, so we'll have to get those.
5763           (when (stringp article)
5764             (gnus-read-header article))
5765
5766           ;; If the article number is negative, that means that this article
5767           ;; doesn't belong in this newsgroup (possibly), so we find its
5768           ;; message-id and request it by id instead of number.
5769           (when (and (numberp article)
5770                      gnus-summary-buffer
5771                      (get-buffer gnus-summary-buffer)
5772                      (gnus-buffer-exists-p gnus-summary-buffer))
5773             (save-excursion
5774               (set-buffer gnus-summary-buffer)
5775               (let ((header (gnus-summary-article-header article)))
5776                 (when (< article 0)
5777                   (cond
5778                    ((memq article gnus-newsgroup-sparse)
5779                     ;; This is a sparse gap article.
5780                     (setq do-update-line article)
5781                     (setq article (mail-header-id header))
5782                     (setq sparse-header (gnus-read-header article))
5783                     (setq gnus-newsgroup-sparse
5784                           (delq article gnus-newsgroup-sparse)))
5785                    ((vectorp header)
5786                     ;; It's a real article.
5787                     (setq article (mail-header-id header)))
5788                    (t
5789                     ;; It is an extracted pseudo-article.
5790                     (setq article 'pseudo)
5791                     (gnus-request-pseudo-article header))))
5792
5793                 (let ((method (gnus-find-method-for-group
5794                                gnus-newsgroup-name)))
5795                   (when (and (eq (car method) 'nneething)
5796                              (vectorp header))
5797                     (let ((dir (nneething-get-file-name
5798                                 (mail-header-id header))))
5799                       (when (and (stringp dir)
5800                                  (file-directory-p dir))
5801                         (setq article 'nneething)
5802                         (gnus-group-enter-directory dir))))))))
5803
5804           (cond
5805            ;; Refuse to select canceled articles.
5806            ((and (numberp article)
5807                  gnus-summary-buffer
5808                  (get-buffer gnus-summary-buffer)
5809                  (gnus-buffer-exists-p gnus-summary-buffer)
5810                  (eq (cdr (with-current-buffer gnus-summary-buffer
5811                             (assq article gnus-newsgroup-reads)))
5812                      gnus-canceled-mark))
5813             nil)
5814            ;; We first check `gnus-original-article-buffer'.
5815            ((and (get-buffer gnus-original-article-buffer)
5816                  (numberp article)
5817                  (with-current-buffer gnus-original-article-buffer
5818                    (and (equal (car gnus-original-article) group)
5819                         (eq (cdr gnus-original-article) article))))
5820             (insert-buffer-substring gnus-original-article-buffer)
5821             'article)
5822            ;; Check the backlog.
5823            ((and gnus-keep-backlog
5824                  (gnus-backlog-request-article group article (current-buffer)))
5825             'article)
5826            ;; Check asynchronous pre-fetch.
5827            ((gnus-async-request-fetched-article group article (current-buffer))
5828             (gnus-async-prefetch-next group article gnus-summary-buffer)
5829             (when (and (numberp article) gnus-keep-backlog)
5830               (gnus-backlog-enter-article group article (current-buffer)))
5831             'article)
5832            ;; Check the cache.
5833            ((and gnus-use-cache
5834                  (numberp article)
5835                  (gnus-cache-request-article article group))
5836             'article)
5837            ;; Check the agent cache.
5838            ((gnus-agent-request-article article group)
5839             'article)
5840            ;; Get the article and put into the article buffer.
5841            ((or (stringp article)
5842                 (numberp article))
5843             (let ((gnus-override-method gnus-override-method)
5844                   (methods (and (stringp article)
5845                                 gnus-refer-article-method))
5846                   (backend (car (gnus-find-method-for-group
5847                                  gnus-newsgroup-name)))
5848                   result
5849                   (inhibit-read-only t))
5850               (if (or (not (listp methods))
5851                       (and (symbolp (car methods))
5852                            (assq (car methods) nnoo-definition-alist)))
5853                   (setq methods (list methods)))
5854               (when (and (null gnus-override-method)
5855                          methods)
5856                 (setq gnus-override-method (pop methods)))
5857               (while (not result)
5858                 (when (eq gnus-override-method 'current)
5859                   (setq gnus-override-method
5860                         (with-current-buffer gnus-summary-buffer
5861                           gnus-current-select-method)))
5862                 (erase-buffer)
5863                 (gnus-kill-all-overlays)
5864                 (let ((gnus-newsgroup-name group))
5865                   (gnus-check-group-server))
5866                 (cond
5867                  ((gnus-request-article article group (current-buffer))
5868                   (when (numberp article)
5869                     (gnus-async-prefetch-next group article
5870                                               gnus-summary-buffer)
5871                     (when gnus-keep-backlog
5872                       (gnus-backlog-enter-article
5873                        group article (current-buffer))))
5874                   (setq result 'article))
5875                  (methods
5876                   (setq gnus-override-method (pop methods)))
5877                  ((not (string-match "^400 "
5878                                      (nnheader-get-report backend)))
5879                   ;; If we get 400 server disconnect, reconnect and
5880                   ;; retry; otherwise, assume the article has expired.
5881                   (setq result 'done))))
5882               (and (eq result 'article) 'article)))
5883            ;; It was a pseudo.
5884            (t article)))
5885
5886       ;; Associate this article with the current summary buffer.
5887       (setq gnus-article-current-summary gnus-summary-buffer)
5888
5889       ;; Take the article from the original article buffer
5890       ;; and place it in the buffer it's supposed to be in.
5891       (when (and (get-buffer gnus-article-buffer)
5892                  (equal (buffer-name (current-buffer))
5893                         (buffer-name (get-buffer gnus-article-buffer))))
5894         (save-excursion
5895           (if (get-buffer gnus-original-article-buffer)
5896               (set-buffer gnus-original-article-buffer)
5897             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5898             (buffer-disable-undo)
5899             (setq major-mode 'gnus-original-article-mode)
5900             (setq buffer-read-only t))
5901           (let ((inhibit-read-only t))
5902             (erase-buffer)
5903             (insert-buffer-substring gnus-article-buffer))
5904           (setq gnus-original-article (cons group article)))
5905
5906         ;; Decode charsets.
5907         (run-hooks 'gnus-article-decode-hook)
5908         ;; Mark article as decoded or not.
5909         (setq gnus-article-decoded-p gnus-article-decode-hook))
5910
5911       ;; Update sparse articles.
5912       (when (and do-update-line
5913                  (or (numberp article)
5914                      (stringp article)))
5915         (let ((buf (current-buffer)))
5916           (set-buffer gnus-summary-buffer)
5917           (gnus-summary-update-article do-update-line sparse-header)
5918           (gnus-summary-goto-subject do-update-line nil t)
5919           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5920                             (point))
5921           (set-buffer buf))))))
5922
5923 ;;;
5924 ;;; Article editing
5925 ;;;
5926
5927 (defcustom gnus-article-edit-mode-hook nil
5928   "Hook run in article edit mode buffers."
5929   :group 'gnus-article-various
5930   :type 'hook)
5931
5932 (defvar gnus-article-edit-done-function nil)
5933
5934 (defvar gnus-article-edit-mode-map nil)
5935
5936 ;; Should we be using derived.el for this?
5937 (unless gnus-article-edit-mode-map
5938   (setq gnus-article-edit-mode-map (make-keymap))
5939   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5940
5941   (gnus-define-keys gnus-article-edit-mode-map
5942     "\C-c?"    describe-mode
5943     "\C-c\C-c" gnus-article-edit-done
5944     "\C-c\C-k" gnus-article-edit-exit
5945     "\C-c\C-f\C-t" message-goto-to
5946     "\C-c\C-f\C-o" message-goto-from
5947     "\C-c\C-f\C-b" message-goto-bcc
5948     ;;"\C-c\C-f\C-w" message-goto-fcc
5949     "\C-c\C-f\C-c" message-goto-cc
5950     "\C-c\C-f\C-s" message-goto-subject
5951     "\C-c\C-f\C-r" message-goto-reply-to
5952     "\C-c\C-f\C-n" message-goto-newsgroups
5953     "\C-c\C-f\C-d" message-goto-distribution
5954     "\C-c\C-f\C-f" message-goto-followup-to
5955     "\C-c\C-f\C-m" message-goto-mail-followup-to
5956     "\C-c\C-f\C-k" message-goto-keywords
5957     "\C-c\C-f\C-u" message-goto-summary
5958     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5959     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
5960     "\C-c\C-b" message-goto-body
5961     "\C-c\C-i" message-goto-signature
5962
5963     "\C-c\C-t" message-insert-to
5964     "\C-c\C-n" message-insert-newsgroups
5965     "\C-c\C-o" message-sort-headers
5966     "\C-c\C-e" message-elide-region
5967     "\C-c\C-v" message-delete-not-region
5968     "\C-c\C-z" message-kill-to-signature
5969     "\M-\r" message-newline-and-reformat
5970     "\C-c\C-a" mml-attach-file
5971     "\C-a" message-beginning-of-line
5972     "\t" message-tab
5973     "\M-;" comment-region)
5974
5975   (gnus-define-keys (gnus-article-edit-wash-map
5976                      "\C-c\C-w" gnus-article-edit-mode-map)
5977     "f" gnus-article-edit-full-stops))
5978
5979 (easy-menu-define
5980   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5981   '("Field"
5982     ["Fetch To" message-insert-to t]
5983     ["Fetch Newsgroups" message-insert-newsgroups t]
5984     "----"
5985     ["To" message-goto-to t]
5986     ["From" message-goto-from t]
5987     ["Subject" message-goto-subject t]
5988     ["Cc" message-goto-cc t]
5989     ["Reply-To" message-goto-reply-to t]
5990     ["Summary" message-goto-summary t]
5991     ["Keywords" message-goto-keywords t]
5992     ["Newsgroups" message-goto-newsgroups t]
5993     ["Followup-To" message-goto-followup-to t]
5994     ["Mail-Followup-To" message-goto-mail-followup-to t]
5995     ["Distribution" message-goto-distribution t]
5996     ["Body" message-goto-body t]
5997     ["Signature" message-goto-signature t]))
5998
5999 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6000   "Major mode for editing articles.
6001 This is an extended text-mode.
6002
6003 \\{gnus-article-edit-mode-map}"
6004   (make-local-variable 'gnus-article-edit-done-function)
6005   (make-local-variable 'gnus-prev-winconf)
6006   (set (make-local-variable 'font-lock-defaults)
6007        '(message-font-lock-keywords t))
6008   (set (make-local-variable 'mail-header-separator) "")
6009   (set (make-local-variable 'gnus-article-edit-mode) t)
6010   (easy-menu-add message-mode-field-menu message-mode-map)
6011   (mml-mode)
6012   (setq buffer-read-only nil)
6013   (buffer-enable-undo)
6014   (widen))
6015
6016 (defun gnus-article-edit (&optional force)
6017   "Edit the current article.
6018 This will have permanent effect only in mail groups.
6019 If FORCE is non-nil, allow editing of articles even in read-only
6020 groups."
6021   (interactive "P")
6022   (when (and (not force)
6023              (gnus-group-read-only-p))
6024     (error "The current newsgroup does not support article editing"))
6025   (gnus-article-date-original)
6026   (gnus-article-edit-article
6027    'ignore
6028    `(lambda (no-highlight)
6029       'ignore
6030       (gnus-summary-edit-article-done
6031        ,(or (mail-header-references gnus-current-headers) "")
6032        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6033
6034 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6035   "Start editing the contents of the current article buffer."
6036   (let ((winconf (current-window-configuration)))
6037     (set-buffer gnus-article-buffer)
6038     (let ((message-auto-save-directory
6039            ;; Don't associate the article buffer with a draft file.
6040            nil))
6041       (gnus-article-edit-mode))
6042     (funcall start-func)
6043     (set-buffer-modified-p nil)
6044     (gnus-configure-windows 'edit-article)
6045     (setq gnus-article-edit-done-function exit-func)
6046     (setq gnus-prev-winconf winconf)
6047     (unless quiet
6048       (gnus-message 6 "C-c C-c to end edits"))))
6049
6050 (defun gnus-article-edit-done (&optional arg)
6051   "Update the article edits and exit."
6052   (interactive "P")
6053   (let ((func gnus-article-edit-done-function)
6054         (buf (current-buffer))
6055         (start (window-start))
6056         (p (point))
6057         (winconf gnus-prev-winconf))
6058     (widen) ;; Widen it in case that users narrowed the buffer.
6059     (funcall func arg)
6060     (set-buffer buf)
6061     ;; The cache and backlog have to be flushed somewhat.
6062     (when gnus-keep-backlog
6063       (gnus-backlog-remove-article
6064        (car gnus-article-current) (cdr gnus-article-current)))
6065     ;; Flush original article as well.
6066     (save-excursion
6067       (when (get-buffer gnus-original-article-buffer)
6068         (set-buffer gnus-original-article-buffer)
6069         (setq gnus-original-article nil)))
6070     (when gnus-use-cache
6071       (gnus-cache-update-article
6072        (car gnus-article-current) (cdr gnus-article-current)))
6073     ;; We remove all text props from the article buffer.
6074     (kill-all-local-variables)
6075     (set-text-properties (point-min) (point-max) nil)
6076     (gnus-article-mode)
6077     (set-window-configuration winconf)
6078     (set-buffer buf)
6079     (set-window-start (get-buffer-window buf) start)
6080     (set-window-point (get-buffer-window buf) (point)))
6081   (gnus-summary-show-article))
6082
6083 (defun gnus-article-edit-exit ()
6084   "Exit the article editing without updating."
6085   (interactive)
6086   (when (or (not (buffer-modified-p))
6087             (yes-or-no-p "Article modified; kill anyway? "))
6088     (let ((curbuf (current-buffer))
6089           (p (point))
6090           (window-start (window-start)))
6091       (erase-buffer)
6092       (if (gnus-buffer-live-p gnus-original-article-buffer)
6093           (insert-buffer-substring gnus-original-article-buffer))
6094       (let ((winconf gnus-prev-winconf))
6095         (kill-all-local-variables)
6096         (gnus-article-mode)
6097         (set-window-configuration winconf)
6098         ;; Tippy-toe some to make sure that point remains where it was.
6099         (save-current-buffer
6100           (set-buffer curbuf)
6101           (set-window-start (get-buffer-window (current-buffer)) window-start)
6102           (goto-char p))))
6103     (gnus-summary-show-article)))
6104
6105 (defun gnus-article-edit-full-stops ()
6106   "Interactively repair spacing at end of sentences."
6107   (interactive)
6108   (save-excursion
6109     (goto-char (point-min))
6110     (search-forward-regexp "^$" nil t)
6111     (let ((case-fold-search nil))
6112       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6113
6114 ;;;
6115 ;;; Article highlights
6116 ;;;
6117
6118 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6119
6120 ;;; Internal Variables:
6121
6122 (defcustom gnus-button-url-regexp
6123   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6124       "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?[-a-z0-9_=!?#$@~%&*+\\/:;.,[:word:]]+[-a-z0-9_=#$@~%&*+\\/[:word:]]\\)"
6125     "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)\\)")
6126   "Regular expression that matches URLs."
6127   :group 'gnus-article-buttons
6128   :type 'regexp)
6129
6130 (defcustom gnus-button-valid-fqdn-regexp
6131   message-valid-fqdn-regexp
6132   "Regular expression that matches a valid FQDN."
6133   :version "22.1"
6134   :group 'gnus-article-buttons
6135   :type 'regexp)
6136
6137 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6138 (defcustom gnus-button-valid-localpart-regexp
6139   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t ]*"
6140   "Regular expression that matches a localpart of mail addresses or MIDs."
6141   :version "22.1"
6142   :group 'gnus-article-buttons
6143   :type 'regexp)
6144
6145 (defcustom gnus-button-man-handler 'manual-entry
6146   "Function to use for displaying man pages.
6147 The function must take at least one argument with a string naming the
6148 man page."
6149   :version "22.1"
6150   :type '(choice (function-item :tag "Man" manual-entry)
6151                  (function-item :tag "Woman" woman)
6152                  (function :tag "Other"))
6153   :group 'gnus-article-buttons)
6154
6155 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6156   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6157 If the default site is too slow, try to find a CTAN mirror, see
6158 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6159 the variable `gnus-button-handle-ctan'."
6160   :version "22.1"
6161   :group 'gnus-article-buttons
6162   :link '(custom-manual "(gnus)Group Parameters")
6163   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6164                  (const "http://tug.ctan.org/tex-archive/")
6165                  (const "http://www.dante.de/CTAN/")
6166                  (string :tag "Other")))
6167
6168 (defcustom gnus-button-ctan-handler 'browse-url
6169   "Function to use for displaying CTAN links.
6170 The function must take one argument, the string naming the URL."
6171   :version "22.1"
6172   :type '(choice (function-item :tag "Browse Url" browse-url)
6173                  (function :tag "Other"))
6174   :group 'gnus-article-buttons)
6175
6176 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6177   "Bogus strings removed from CTAN URLs."
6178   :version "22.1"
6179   :group 'gnus-article-buttons
6180   :type '(choice (const "^/?tex-archive/\\|/")
6181                  (regexp :tag "Other")))
6182
6183 (defcustom gnus-button-ctan-directory-regexp
6184   (regexp-opt
6185    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6186          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6187          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6188          "tds" "tools" "usergrps" "web") t)
6189   "Regular expression for ctan directories.
6190 It should match all directories in the top level of `gnus-ctan-url'."
6191   :version "22.1"
6192   :group 'gnus-article-buttons
6193   :type 'regexp)
6194
6195 (defcustom gnus-button-mid-or-mail-regexp
6196   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6197           gnus-button-valid-fqdn-regexp
6198           ">?\\)\\b")
6199   "Regular expression that matches a message ID or a mail address."
6200   :version "22.1"
6201   :group 'gnus-article-buttons
6202   :type 'regexp)
6203
6204 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6205   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6206 Strings like this can be either a message ID or a mail address.  If it is one
6207 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6208 message ID or a mail address, respectively.  If this variable is set to the
6209 symbol `ask', always query the user what do do.  If it is a function, this
6210 function will be called with the string as it's only argument.  The function
6211 must return `mid', `mail', `invalid' or `ask'."
6212   :version "22.1"
6213   :group 'gnus-article-buttons
6214   :type '(choice (function-item :tag "Heuristic function"
6215                                 gnus-button-mid-or-mail-heuristic)
6216                  (const ask)
6217                  (const mid)
6218                  (const mail)))
6219
6220 (defcustom gnus-button-mid-or-mail-heuristic-alist
6221   '((-10.0 . ".+\\$.+@")
6222     (-10.0 . "#")
6223     (-10.0 . "\\*")
6224     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6225     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6226     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6227     (-1.0  . "^[^a-z]+@")
6228     ;;
6229     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6230     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6231     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6232     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6233     ;;
6234     (-2.0  . "^[0-9]")
6235     (-1.0  . "^[0-9][0-9]")
6236     ;;
6237     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6238     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6239     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6240     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6241     ;;
6242     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6243     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6244     ;;       "[0-9]{8,}.*\@"
6245     (-3.0
6246      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6247     ;; "[0-9]{12,}.*\@"
6248     ;; compensation for TDMA dated mail addresses:
6249     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6250     ;;
6251     (-20.0 . "\\.fsf@") ;; Gnus
6252     (-20.0 . "^slrn")
6253     (-20.0 . "^Pine")
6254     (-20.0 . "_-_") ;; Subject change in thread
6255     ;;
6256     (-20.0 . "\\.ln@") ;; leafnode
6257     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6258     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6259     ;;
6260     ;; (5.0 . "") ;; $local_part_len <= 7
6261     (10.0  . "^[^0-9]+@")
6262     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6263     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6264     (3.0   . "\@stud")
6265     ;;
6266     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6267     ;;
6268     (0.5   . "^[A-Z][a-z]")
6269     (0.5   . "^[A-Z][a-z][a-z]")
6270     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6271     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6272   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6273
6274 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6275 address.  The REGEXP is processed with `case-fold-search' set to nil."
6276   :version "22.1"
6277   :group 'gnus-article-buttons
6278   :type '(repeat (cons (number :tag "Rate")
6279                        (regexp :tag "Regexp"))))
6280
6281 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6282   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6283 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6284 address, `ask' if unsure and `invalid' if the string is invalid."
6285   (let ((case-fold-search nil)
6286         (list gnus-button-mid-or-mail-heuristic-alist)
6287         (result 0) rate regexp lpartlen elem)
6288     (setq lpartlen
6289           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6290     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6291     ;; Certain special cases...
6292     (when (string-match
6293            (concat
6294             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
6295             "^[0-9]+\\.[0-9]+@compuserve\\|"
6296             "@public\\.gmane\\.org")
6297            mid-or-mail)
6298       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
6299       (setq result 'mail))
6300     (when (string-match "@.*@\\| " mid-or-mail)
6301       (gnus-message 8 "`%s' is invalid." mid-or-mail)
6302       (setq result 'invalid))
6303     ;; Nothing more to do, if result is not a number here...
6304     (when (numberp result)
6305       (while list
6306         (setq elem (car list)
6307               rate (car elem)
6308               regexp (cdr elem)
6309               list (cdr list))
6310         (when (string-match regexp mid-or-mail)
6311           (setq result (+ result rate))
6312           (gnus-message
6313            9 "`%s' matched `%s', rate `%s', result `%s'."
6314            mid-or-mail regexp rate result)))
6315       (when (<= lpartlen 7)
6316         (setq result (+ result 5.0))
6317         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6318                       mid-or-mail result))
6319       (when (>= lpartlen 12)
6320         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6321         (cond
6322          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6323           ;; Long local part should contain realname if e-mail address,
6324           ;; too many digits: message-id.
6325           ;; $score -= 5.0 + 0.1 * $local_part_len;
6326           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6327           (setq result (+ result rate))
6328           (gnus-message
6329            9 "Many digits in `%s', rate `%s', result `%s'."
6330            mid-or-mail rate result))
6331          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6332                         mid-or-mail)
6333           ;; Too few vowels [^aeiouy]{4,}.*\@
6334           (setq result (+ result -5.0))
6335           (gnus-message
6336            9 "Few vowels in `%s', rate `%s', result `%s'."
6337            mid-or-mail -5.0 result))
6338          (t
6339           (setq result (+ result 5.0))
6340           (gnus-message
6341            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6342     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6343     ;; Maybe we should make this a customizable alist: (condition . 'result)
6344     (cond
6345      ((symbolp result) result)
6346      ;; Now convert number into proper results:
6347      ((< result -10.0) 'mid)
6348      ((> result  10.0) 'mail)
6349      (t 'ask))))
6350
6351 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6352   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6353          (url-mid (concat "news" ":" mid-or-mail))
6354          (url-mailto (concat "mailto" ":" mid-or-mail)))
6355     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6356     (when (fboundp pref)
6357       (setq guessed
6358             ;; get rid of surrounding angles...
6359             (funcall pref
6360                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6361       (if (or (eq 'mid guessed) (eq 'mail guessed))
6362           (setq pref guessed)
6363         (setq pref 'ask)))
6364     (if (eq pref 'ask)
6365         (save-window-excursion
6366           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6367               (setq pref 'mail)
6368             (setq pref 'mid))))
6369     (cond ((eq pref 'mid)
6370            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6371            (gnus-button-handle-news url-mid))
6372           ((eq pref 'mail)
6373            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6374            (gnus-url-mailto url-mailto))
6375           (t (gnus-message 3 "Invalid string.")))))
6376
6377 (defun gnus-button-handle-custom (fun arg)
6378   "Call function FUN on argument ARG.
6379 Both FUN and ARG are supposed to be strings.  ARG will be passed
6380 as a symbol to FUN."
6381   (funcall (intern fun) (intern arg)))
6382
6383 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6384
6385 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6386 ;; stuff?
6387
6388 (defun gnus-button-handle-describe-function (url)
6389   "Call `describe-function' when pushing the corresponding URL button."
6390   (describe-function
6391    (intern
6392     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6393
6394 (defun gnus-button-handle-describe-variable (url)
6395   "Call `describe-variable' when pushing the corresponding URL button."
6396   (describe-variable
6397    (intern
6398     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6399
6400 (defun gnus-button-handle-symbol (url)
6401 "Display help on variable or function.
6402 Calls `describe-variable' or `describe-function'."
6403   (let ((sym (intern url)))
6404     (cond
6405      ((fboundp sym) (describe-function sym))
6406      ((boundp sym) (describe-variable sym))
6407      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6408
6409 (defun gnus-button-handle-describe-key (url)
6410   "Call `describe-key' when pushing the corresponding URL button."
6411   (let* ((key-string
6412           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6413          (keys (ignore-errors (eval `(kbd ,key-string)))))
6414     (if keys
6415         (describe-key keys)
6416       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6417
6418 (defun gnus-button-handle-apropos (url)
6419   "Call `apropos' when pushing the corresponding URL button."
6420   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6421
6422 (defun gnus-button-handle-apropos-command (url)
6423   "Call `apropos' when pushing the corresponding URL button."
6424   (apropos-command
6425    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6426
6427 (defun gnus-button-handle-apropos-variable (url)
6428   "Call `apropos' when pushing the corresponding URL button."
6429   (funcall
6430    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6431    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6432
6433 (defun gnus-button-handle-apropos-documentation (url)
6434   "Call `apropos' when pushing the corresponding URL button."
6435   (funcall
6436    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6437    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6438
6439 (defun gnus-button-handle-library (url)
6440   "Call `locate-library' when pushing the corresponding URL button."
6441   (gnus-message 9 "url=`%s'" url)
6442   (let* ((lib (locate-library url))
6443          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6444     (if (not lib)
6445         (gnus-message 1 "Cannot locale library `%s'." url)
6446       (find-file-read-only file))))
6447
6448 (defun gnus-button-handle-ctan (url)
6449   "Call `browse-url' when pushing a CTAN URL button."
6450   (funcall
6451    gnus-button-ctan-handler
6452    (concat
6453     gnus-ctan-url
6454     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6455
6456 (defcustom gnus-button-tex-level 5
6457   "*Integer that says how many TeX-related buttons Gnus will show.
6458 The higher the number, the more buttons will appear and the more false
6459 positives are possible.  Note that you can set this variable local to
6460 specific groups.  Setting it higher in TeX groups is probably a good idea.
6461 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6462 how to set variables in specific groups."
6463   :version "22.1"
6464   :group 'gnus-article-buttons
6465   :link '(custom-manual "(gnus)Group Parameters")
6466   :type 'integer)
6467
6468 (defcustom gnus-button-man-level 5
6469   "*Integer that says how many man-related buttons Gnus will show.
6470 The higher the number, the more buttons will appear and the more false
6471 positives are possible.  Note that you can set this variable local to
6472 specific groups.  Setting it higher in Unix groups is probably a good idea.
6473 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6474 how to set variables in specific groups."
6475   :version "22.1"
6476   :group 'gnus-article-buttons
6477   :link '(custom-manual "(gnus)Group Parameters")
6478   :type 'integer)
6479
6480 (defcustom gnus-button-emacs-level 5
6481   "*Integer that says how many emacs-related buttons Gnus will show.
6482 The higher the number, the more buttons will appear and the more false
6483 positives are possible.  Note that you can set this variable local to
6484 specific groups.  Setting it higher in Emacs or Gnus related groups is
6485 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6486 `gnus-parameters' on how to set variables in specific groups."
6487   :version "22.1"
6488   :group 'gnus-article-buttons
6489   :link '(custom-manual "(gnus)Group Parameters")
6490   :type 'integer)
6491
6492 (defcustom gnus-button-message-level 5
6493   "*Integer that says how many buttons for news or mail messages will appear.
6494 The higher the number, the more buttons will appear and the more false
6495 positives are possible."
6496   ;; mail addresses, MIDs, URLs for news, ...
6497   :version "22.1"
6498   :group 'gnus-article-buttons
6499   :type 'integer)
6500
6501 (defcustom gnus-button-browse-level 5
6502   "*Integer that says how many buttons for browsing will appear.
6503 The higher the number, the more buttons will appear and the more false
6504 positives are possible."
6505   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6506   :version "22.1"
6507   :group 'gnus-article-buttons
6508   :type 'integer)
6509
6510 (defcustom gnus-button-alist
6511   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6512      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6513     ((concat "\\b\\(nntp\\|news\\):\\("
6514              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
6515      0 t gnus-button-handle-news 2)
6516     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6517      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6518     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6519      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6520     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
6521     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
6522      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
6523     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6524      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6525     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6526      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6527     ;; RFC 2368 (The mailto URL scheme)
6528     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6529      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6530     ("\\bmailto:\\([^ \n\t]+\\)"
6531      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6532     ;; CTAN
6533     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6534              gnus-button-ctan-directory-regexp
6535              "[^][>)!;:,'\n\t ]+\\)")
6536      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6537     ((concat "\\btex-archive/\\("
6538              gnus-button-ctan-directory-regexp
6539              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6540      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6541     ((concat
6542       "\\b\\("
6543       gnus-button-ctan-directory-regexp
6544       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6545      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6546     ;; This is info (home-grown style) <info://foo/bar+baz>
6547     ("\\binfo://\\([^'\">\n\t ]+\\)"
6548      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
6549     ;; Info GNOME style <info:foo#bar_baz>
6550     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
6551      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
6552     ;; Info KDE style <info:(foo)bar baz>
6553     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
6554      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
6555     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6556      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6557     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6558      ;; Info links like `C-h i d m CC Mode RET'
6559      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6560     ;; This is custom
6561     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6562      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
6563     ;; Emacs help commands
6564     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6565      ;; regexp doesn't match arguments containing ` '.
6566      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6567     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6568      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6569     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6570      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6571     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6572      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6573     ;; The following entries may lead to many false positives so don't enable
6574     ;; them by default (use a high button level).
6575     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
6576      ;; Exclude [.?] for URLs in gmane.emacs.cvs
6577      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6578     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6579      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6580     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6581      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6582     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6583      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6584     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6585      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6586     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6587      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
6588     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6589      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6590     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6591      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6592     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6593      ;; Unlike the other regexps we really have to require quoting
6594      ;; here to determine where it ends.
6595      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6596     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
6597     ("<URL: *\\([^<>]*\\)>"
6598      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6599     ;; RFC 2396 (2.4.3., delims) ...
6600     ("\"URL: *\\([^\"]*\\)\""
6601      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6602     ;; RFC 2396 (2.4.3., delims) ...
6603     ("\"URL: *\\([^\"]*\\)\""
6604      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6605     ;; Raw URLs.
6606     (gnus-button-url-regexp
6607      0 (>= gnus-button-browse-level 0) browse-url 0)
6608     ;; man pages
6609     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
6610      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6611      gnus-button-handle-man 1)
6612     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6613     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
6614      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6615      gnus-button-handle-man 1)
6616     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6617     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6618     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
6619      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6620     ;; MID or mail: To avoid too many false positives we don't try to catch
6621     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6622     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6623     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6624     ;; so that non-ambiguous entries (see above) match first.
6625     (gnus-button-mid-or-mail-regexp
6626      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6627   "*Alist of regexps matching buttons in article bodies.
6628
6629 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6630 REGEXP: is the string (case insensitive) matching text around the button (can
6631 also be Lisp expression evaluating to a string),
6632 BUTTON: is the number of the regexp grouping actually matching the button,
6633 FORM: is a Lisp expression which must eval to true for the button to
6634 be added,
6635 CALLBACK: is the function to call when the user push this button, and each
6636 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6637
6638 CALLBACK can also be a variable, in that case the value of that
6639 variable it the real callback function."
6640   :group 'gnus-article-buttons
6641   :type '(repeat (list (choice regexp variable sexp)
6642                        (integer :tag "Button")
6643                        (sexp :tag "Form")
6644                        (function :tag "Callback")
6645                        (repeat :tag "Par"
6646                                :inline t
6647                                (integer :tag "Regexp group")))))
6648
6649 (defcustom gnus-header-button-alist
6650   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
6651      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6652     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6653      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6654     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6655      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
6656     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6657      0 (>= gnus-button-browse-level 0) browse-url 0)
6658     ("^Subject:" gnus-button-url-regexp
6659      0 (>= gnus-button-browse-level 0) browse-url 0)
6660     ("^[^:]+:" gnus-button-url-regexp
6661      0 (>= gnus-button-browse-level 0) browse-url 0)
6662     ("^OpenPGP:.*url=" gnus-button-url-regexp
6663      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
6664     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6665      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6666     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6667      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6668   "*Alist of headers and regexps to match buttons in article heads.
6669
6670 This alist is very similar to `gnus-button-alist', except that each
6671 alist has an additional HEADER element first in each entry:
6672
6673 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6674
6675 HEADER is a regexp to match a header.  For a fuller explanation, see
6676 `gnus-button-alist'."
6677   :group 'gnus-article-buttons
6678   :group 'gnus-article-headers
6679   :type '(repeat (list (regexp :tag "Header")
6680                        (choice regexp variable)
6681                        (integer :tag "Button")
6682                        (sexp :tag "Form")
6683                        (function :tag "Callback")
6684                        (repeat :tag "Par"
6685                                :inline t
6686                                (integer :tag "Regexp group")))))
6687
6688 ;;; Commands:
6689
6690 (defun gnus-article-push-button (event)
6691   "Check text under the mouse pointer for a callback function.
6692 If the text under the mouse pointer has a `gnus-callback' property,
6693 call it with the value of the `gnus-data' text property."
6694   (interactive "e")
6695   (set-buffer (window-buffer (posn-window (event-start event))))
6696   (let* ((pos (posn-point (event-start event)))
6697          (data (get-text-property pos 'gnus-data))
6698          (fun (get-text-property pos 'gnus-callback)))
6699     (goto-char pos)
6700     (when fun
6701       (funcall fun data))))
6702
6703 (defun gnus-article-press-button ()
6704   "Check text at point for a callback function.
6705 If the text at point has a `gnus-callback' property,
6706 call it with the value of the `gnus-data' text property."
6707   (interactive)
6708   (let ((data (get-text-property (point) 'gnus-data))
6709         (fun (get-text-property (point) 'gnus-callback)))
6710     (when fun
6711       (funcall fun data))))
6712
6713 (defun gnus-article-highlight (&optional force)
6714   "Highlight current article.
6715 This function calls `gnus-article-highlight-headers',
6716 `gnus-article-highlight-citation',
6717 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6718 do the highlighting.  See the documentation for those functions."
6719   (interactive (list 'force))
6720   (gnus-article-highlight-headers)
6721   (gnus-article-highlight-citation force)
6722   (gnus-article-highlight-signature)
6723   (gnus-article-add-buttons force)
6724   (gnus-article-add-buttons-to-head))
6725
6726 (defun gnus-article-highlight-some (&optional force)
6727   "Highlight current article.
6728 This function calls `gnus-article-highlight-headers',
6729 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6730 do the highlighting.  See the documentation for those functions."
6731   (interactive (list 'force))
6732   (gnus-article-highlight-headers)
6733   (gnus-article-highlight-signature)
6734   (gnus-article-add-buttons))
6735
6736 (defun gnus-article-highlight-headers ()
6737   "Highlight article headers as specified by `gnus-header-face-alist'."
6738   (interactive)
6739   (gnus-with-article-headers
6740     (let (regexp header-face field-face from hpoints fpoints)
6741       (dolist (entry gnus-header-face-alist)
6742         (goto-char (point-min))
6743         (setq regexp (concat "^\\("
6744                              (if (string-equal "" (nth 0 entry))
6745                                  "[^\t ]"
6746                                (nth 0 entry))
6747                              "\\)")
6748               header-face (nth 1 entry)
6749               field-face (nth 2 entry))
6750         (while (and (re-search-forward regexp nil t)
6751                     (not (eobp)))
6752           (beginning-of-line)
6753           (setq from (point))
6754           (unless (search-forward ":" nil t)
6755             (forward-char 1))
6756           (when (and header-face
6757                      (not (memq (point) hpoints)))
6758             (push (point) hpoints)
6759             (gnus-put-text-property from (point) 'face header-face))
6760           (when (and field-face
6761                      (not (memq (setq from (point)) fpoints)))
6762             (push from fpoints)
6763             (if (re-search-forward "^[^ \t]" nil t)
6764                 (forward-char -2)
6765               (goto-char (point-max)))
6766             (gnus-put-text-property from (point) 'face field-face)))))))
6767
6768 (defun gnus-article-highlight-signature ()
6769   "Highlight the signature in an article.
6770 It does this by highlighting everything after
6771 `gnus-signature-separator' using the face `gnus-signature'."
6772   (interactive)
6773   (gnus-with-article-buffer
6774     (let ((inhibit-point-motion-hooks t))
6775       (save-restriction
6776         (when (and gnus-signature-face
6777                    (gnus-article-narrow-to-signature))
6778           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6779                             'face gnus-signature-face)
6780           (widen)
6781           (gnus-article-search-signature)
6782           (let ((start (match-beginning 0))
6783                 (end (set-marker (make-marker) (1+ (match-end 0)))))
6784             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
6785                                      end)))))))
6786
6787 (defun gnus-button-in-region-p (b e prop)
6788   "Say whether PROP exists in the region."
6789   (text-property-not-all b e prop nil))
6790
6791 (defun gnus-article-add-buttons (&optional force)
6792   "Find external references in the article and make buttons of them.
6793 \"External references\" are things like Message-IDs and URLs, as
6794 specified by `gnus-button-alist'."
6795   (interactive (list 'force))
6796   (gnus-with-article-buffer
6797     (let ((inhibit-point-motion-hooks t)
6798           (case-fold-search t)
6799           (alist gnus-button-alist)
6800           beg entry regexp)
6801       ;; Remove all old markers.
6802       (let (marker entry new-list)
6803         (while (setq marker (pop gnus-button-marker-list))
6804           (if (or (< marker (point-min)) (>= marker (point-max)))
6805               (push marker new-list)
6806             (goto-char marker)
6807             (when (setq entry (gnus-button-entry))
6808               (put-text-property (match-beginning (nth 1 entry))
6809                                  (match-end (nth 1 entry))
6810                                  'gnus-callback nil))
6811             (set-marker marker nil)))
6812         (setq gnus-button-marker-list new-list))
6813       ;; We skip the headers.
6814       (article-goto-body)
6815       (setq beg (point))
6816       (while (setq entry (pop alist))
6817         (setq regexp (eval (car entry)))
6818         (goto-char beg)
6819         (while (re-search-forward regexp nil t)
6820           (let* ((start (and entry (match-beginning (nth 1 entry))))
6821                  (end (and entry (match-end (nth 1 entry))))
6822                  (from (match-beginning 0)))
6823             (when (and (or (eq t (nth 2 entry))
6824                            (eval (nth 2 entry)))
6825                        (not (gnus-button-in-region-p
6826                              start end 'gnus-callback)))
6827               ;; That optional form returned non-nil, so we add the
6828               ;; button.
6829               (gnus-article-add-button
6830                start end 'gnus-button-push
6831                (car (push (set-marker (make-marker) from)
6832                           gnus-button-marker-list))))))))))
6833
6834 ;; Add buttons to the head of an article.
6835 (defun gnus-article-add-buttons-to-head ()
6836   "Add buttons to the head of the article."
6837   (interactive)
6838   (gnus-with-article-headers
6839     (let (beg end)
6840       (dolist (entry gnus-header-button-alist)
6841         ;; Each alist entry.
6842         (goto-char (point-min))
6843         (while (re-search-forward (car entry) nil t)
6844           ;; Each header matching the entry.
6845           (setq beg (match-beginning 0))
6846           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6847                              (match-beginning 0))
6848                         (point-max)))
6849           (goto-char beg)
6850           (while (re-search-forward (eval (nth 1 entry)) end t)
6851             ;; Each match within a header.
6852             (let* ((entry (cdr entry))
6853                    (start (match-beginning (nth 1 entry)))
6854                    (end (match-end (nth 1 entry)))
6855                    (form (nth 2 entry)))
6856               (goto-char (match-end 0))
6857               (when (eval form)
6858                 (gnus-article-add-button
6859                  start end (nth 3 entry)
6860                  (buffer-substring (match-beginning (nth 4 entry))
6861                                    (match-end (nth 4 entry)))))))
6862           (goto-char end))))))
6863
6864 ;;; External functions:
6865
6866 (defun gnus-article-add-button (from to fun &optional data)
6867   "Create a button between FROM and TO with callback FUN and data DATA."
6868   (when gnus-article-button-face
6869     (gnus-overlay-put (gnus-make-overlay from to)
6870                       'face gnus-article-button-face))
6871   (gnus-add-text-properties
6872    from to
6873    (nconc (and gnus-article-mouse-face
6874                (list gnus-mouse-face-prop gnus-article-mouse-face))
6875           (list 'gnus-callback fun)
6876           (and data (list 'gnus-data data))))
6877   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6878                          :button-keymap gnus-widget-button-keymap))
6879
6880 ;;; Internal functions:
6881
6882 (defun gnus-article-set-globals ()
6883   (with-current-buffer gnus-summary-buffer
6884     (gnus-set-global-variables)))
6885
6886 (defun gnus-signature-toggle (end)
6887   (gnus-with-article-buffer
6888     (let ((inhibit-point-motion-hooks t))
6889       (if (text-property-any end (point-max) 'article-type 'signature)
6890           (progn
6891             (gnus-delete-wash-type 'signature)
6892             (gnus-remove-text-properties-when
6893              'article-type 'signature end (point-max)
6894              (cons 'article-type (cons 'signature
6895                                        gnus-hidden-properties))))
6896         (gnus-add-wash-type 'signature)
6897         (gnus-add-text-properties-when
6898          'article-type nil end (point-max)
6899          (cons 'article-type (cons 'signature
6900                                    gnus-hidden-properties)))))
6901     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
6902       (gnus-set-mode-line 'article))))
6903
6904 (defun gnus-button-entry ()
6905   ;; Return the first entry in `gnus-button-alist' matching this place.
6906   (let ((alist gnus-button-alist)
6907         (entry nil))
6908     (while alist
6909       (setq entry (pop alist))
6910       (if (looking-at (eval (car entry)))
6911           (setq alist nil)
6912         (setq entry nil)))
6913     entry))
6914
6915 (defun gnus-button-push (marker)
6916   ;; Push button starting at MARKER.
6917   (save-excursion
6918     (goto-char marker)
6919     (let* ((entry (gnus-button-entry))
6920            (inhibit-point-motion-hooks t)
6921            (fun (nth 3 entry))
6922            (args (mapcar (lambda (group)
6923                            (let ((string (match-string group)))
6924                              (set-text-properties
6925                               0 (length string) nil string)
6926                              string))
6927                          (nthcdr 4 entry))))
6928       (cond
6929        ((fboundp fun)
6930         (apply fun args))
6931        ((and (boundp fun)
6932              (fboundp (symbol-value fun)))
6933         (apply (symbol-value fun) args))
6934        (t
6935         (gnus-message 1 "You must define `%S' to use this button"
6936                       (cons fun args)))))))
6937
6938 (defun gnus-parse-news-url (url)
6939   (let (scheme server port group message-id articles)
6940     (with-temp-buffer
6941       (insert url)
6942       (goto-char (point-min))
6943       (when (looking-at "\\([A-Za-z]+\\):")
6944         (setq scheme (match-string 1))
6945         (goto-char (match-end 0)))
6946       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
6947         (setq server (match-string 1))
6948         (setq port (if (stringp (match-string 3))
6949                        (string-to-number (match-string 3))
6950                      (match-string 3)))
6951         (goto-char (match-end 0)))
6952
6953       (cond
6954        ((looking-at "\\(.*@.*\\)")
6955         (setq message-id (match-string 1)))
6956        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
6957         (setq group (match-string 1)
6958               articles (split-string (match-string 2) "-")))
6959        ((looking-at "\\([^/]+\\)/?")
6960         (setq group (match-string 1)))
6961        (t
6962         (error "Unknown news URL syntax"))))
6963     (list scheme server port group message-id articles)))
6964
6965 (defun gnus-button-handle-news (url)
6966   "Fetch a news URL."
6967   (destructuring-bind (scheme server port group message-id articles)
6968       (gnus-parse-news-url url)
6969     (cond
6970      (message-id
6971       (save-excursion
6972         (set-buffer gnus-summary-buffer)
6973         (if server
6974             (let ((gnus-refer-article-method
6975                    (nconc (list (list 'nntp server))
6976                           gnus-refer-article-method))
6977                   (nntp-port-number (or port "nntp")))
6978               (gnus-message 7 "Fetching %s with %s"
6979                             message-id gnus-refer-article-method)
6980               (gnus-summary-refer-article message-id))
6981           (gnus-summary-refer-article message-id))))
6982      (group
6983       (gnus-button-fetch-group url)))))
6984
6985 (defun gnus-button-handle-man (url)
6986   "Fetch a man page."
6987   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
6988   (when (eq gnus-button-man-handler 'woman)
6989     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
6990   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
6991   (funcall gnus-button-man-handler url))
6992
6993 (defun gnus-button-handle-info-url (url)
6994   "Fetch an info URL."
6995   (setq url (mm-subst-char-in-string ?+ ?\  url))
6996   (cond
6997    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
6998     (gnus-info-find-node
6999      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7000                      "Gnus")
7001              ")" (gnus-url-unhex-string (match-string 2 url)))))
7002    ((string-match "([^)\"]+)[^\"]+" url)
7003     (setq url
7004           (gnus-replace-in-string
7005            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7006     (gnus-info-find-node url))
7007    (t (error "Can't parse %s" url))))
7008
7009 (defun gnus-button-handle-info-url-gnome (url)
7010   "Fetch GNOME style info URL."
7011   (setq url (mm-subst-char-in-string ?_ ?\  url))
7012   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7013       (gnus-info-find-node
7014        (concat "("
7015                (gnus-url-unhex-string
7016                  (match-string 1 url))
7017                ")"
7018                (or (gnus-url-unhex-string
7019                     (match-string 2 url))
7020                    "Top")))
7021     (error "Can't parse %s" url)))
7022
7023 (defun gnus-button-handle-info-url-kde (url)
7024   "Fetch KDE style info URL."
7025   (gnus-info-find-node (gnus-url-unhex-string url)))
7026
7027 (defun gnus-button-handle-info-keystrokes (url)
7028   "Call `info' when pushing the corresponding URL button."
7029   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7030   (info)
7031   (Info-directory)
7032   (Info-menu url))
7033
7034 (defun gnus-button-openpgp (url)
7035   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7036   (with-temp-buffer
7037     (mm-url-insert-file-contents-external url)
7038     (pgg-snarf-keys-region (point-min) (point-max))
7039     (pgg-display-output-buffer nil nil nil)))
7040
7041 (defun gnus-button-message-id (message-id)
7042   "Fetch MESSAGE-ID."
7043   (with-current-buffer gnus-summary-buffer
7044     (gnus-summary-refer-article message-id)))
7045
7046 (defun gnus-button-fetch-group (address)
7047   "Fetch GROUP specified by ADDRESS."
7048   (if (not (string-match "[:/]" address))
7049       ;; This is just a simple group url.
7050       (gnus-group-read-ephemeral-group address gnus-select-method)
7051     (if (not
7052          (string-match
7053           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7054           address))
7055         (error "Can't parse %s" address)
7056       (gnus-group-read-ephemeral-group
7057        (match-string 4 address)
7058        `(nntp ,(match-string 1 address)
7059               (nntp-address ,(match-string 1 address))
7060               (nntp-port-number ,(if (match-end 3)
7061                                      (match-string 3 address)
7062                                    "nntp")))
7063        nil nil nil
7064        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7065
7066 (defun gnus-url-parse-query-string (query &optional downcase)
7067   (let (retval pairs cur key val)
7068     (setq pairs (split-string query "&"))
7069     (while pairs
7070       (setq cur (car pairs)
7071             pairs (cdr pairs))
7072       (if (not (string-match "=" cur))
7073           nil                           ; Grace
7074         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7075               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7076         (if downcase
7077             (setq key (downcase key)))
7078         (setq cur (assoc key retval))
7079         (if cur
7080             (setcdr cur (cons val (cdr cur)))
7081           (setq retval (cons (list key val) retval)))))
7082     retval))
7083
7084 (defun gnus-url-mailto (url)
7085   ;; Send mail to someone
7086   (when (string-match "mailto:/*\\(.*\\)" url)
7087     (setq url (substring url (match-beginning 1) nil)))
7088   (let (to args subject func)
7089     (setq args (gnus-url-parse-query-string
7090                 (if (string-match "^\\?" url)
7091                     (substring url 1)
7092                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7093                       (concat "to=" (match-string 1 url) "&"
7094                               (match-string 2 url))
7095                     (concat "to=" url)))
7096                 t)
7097           subject (cdr-safe (assoc "subject" args)))
7098     (gnus-msg-mail)
7099     (while args
7100       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7101       (if (fboundp func)
7102           (funcall func)
7103         (message-position-on-field (caar args)))
7104       (insert (gnus-replace-in-string
7105                (mapconcat 'identity (reverse (cdar args)) ", ")
7106                "\r\n" "\n" t))
7107       (setq args (cdr args)))
7108     (if subject
7109         (message-goto-body)
7110       (message-goto-subject))))
7111
7112 (defun gnus-button-embedded-url (address)
7113   "Activate ADDRESS with `browse-url'."
7114   (browse-url (gnus-strip-whitespace address)))
7115
7116 ;;; Next/prev buttons in the article buffer.
7117
7118 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7119 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7120
7121 (defvar gnus-prev-page-map
7122   (let ((map (make-sparse-keymap)))
7123     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7124     (define-key map "\r" 'gnus-button-prev-page)
7125     map))
7126
7127 (defvar gnus-next-page-map
7128   (let ((map (make-sparse-keymap)))
7129     (unless (>= emacs-major-version 21)
7130       ;; XEmacs doesn't care.
7131       (set-keymap-parent map gnus-article-mode-map))
7132     (define-key map gnus-mouse-2 'gnus-button-next-page)
7133     (define-key map "\r" 'gnus-button-next-page)
7134     map))
7135
7136 (defun gnus-insert-prev-page-button ()
7137   (let ((b (point))
7138         (inhibit-read-only t))
7139     (gnus-eval-format
7140      gnus-prev-page-line-format nil
7141      `(keymap ,gnus-prev-page-map
7142          gnus-prev t
7143          gnus-callback gnus-article-button-prev-page
7144          article-type annotation))
7145     (widget-convert-button
7146      'link b (if (bolp)
7147                  ;; Exclude a newline.
7148                  (1- (point))
7149                (point))
7150      :action 'gnus-button-prev-page
7151      :button-keymap gnus-prev-page-map)))
7152
7153 (defun gnus-button-next-page (&optional args more-args)
7154   "Go to the next page."
7155   (interactive)
7156   (let ((win (selected-window)))
7157     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7158     (gnus-article-next-page)
7159     (select-window win)))
7160
7161 (defun gnus-button-prev-page (&optional args more-args)
7162   "Go to the prev page."
7163   (interactive)
7164   (let ((win (selected-window)))
7165     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7166     (gnus-article-prev-page)
7167     (select-window win)))
7168
7169 (defun gnus-insert-next-page-button ()
7170   (let ((b (point))
7171         (inhibit-read-only t))
7172     (gnus-eval-format gnus-next-page-line-format nil
7173                       `(keymap ,gnus-next-page-map
7174                           gnus-next t
7175                           gnus-callback gnus-article-button-next-page
7176                           article-type annotation))
7177     (widget-convert-button
7178      'link b (if (bolp)
7179                  ;; Exclude a newline.
7180                  (1- (point))
7181                (point))
7182      :action 'gnus-button-next-page
7183      :button-keymap gnus-next-page-map)))
7184
7185 (defun gnus-article-button-next-page (arg)
7186   "Go to the next page."
7187   (interactive "P")
7188   (let ((win (selected-window)))
7189     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7190     (gnus-article-next-page)
7191     (select-window win)))
7192
7193 (defun gnus-article-button-prev-page (arg)
7194   "Go to the prev page."
7195   (interactive "P")
7196   (let ((win (selected-window)))
7197     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7198     (gnus-article-prev-page)
7199     (select-window win)))
7200
7201 (defvar gnus-decode-header-methods
7202   '(mail-decode-encoded-word-region)
7203   "List of methods used to decode headers.
7204
7205 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7206 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
7207 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
7208 whose names match REGEXP.
7209
7210 For example:
7211 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7212  mail-decode-encoded-word-region
7213  (\"chinese\" . rfc1843-decode-region))
7214 ")
7215
7216 (defvar gnus-decode-header-methods-cache nil)
7217
7218 (defun gnus-multi-decode-header (start end)
7219   "Apply the functions from `gnus-encoded-word-methods' that match."
7220   (unless (and gnus-decode-header-methods-cache
7221                (eq gnus-newsgroup-name
7222                    (car gnus-decode-header-methods-cache)))
7223     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7224     (mapcar (lambda (x)
7225               (if (symbolp x)
7226                   (nconc gnus-decode-header-methods-cache (list x))
7227                 (if (and gnus-newsgroup-name
7228                          (string-match (car x) gnus-newsgroup-name))
7229                     (nconc gnus-decode-header-methods-cache
7230                            (list (cdr x))))))
7231           gnus-decode-header-methods))
7232   (let ((xlist gnus-decode-header-methods-cache))
7233     (pop xlist)
7234     (save-restriction
7235       (narrow-to-region start end)
7236       (while xlist
7237         (funcall (pop xlist) (point-min) (point-max))))))
7238
7239 ;;;
7240 ;;; Treatment top-level handling.
7241 ;;;
7242
7243 (defun gnus-treat-article (condition &optional part-number total-parts type)
7244   (let ((length (- (point-max) (point-min)))
7245         (alist gnus-treatment-function-alist)
7246         (article-goto-body-goes-to-point-min-p t)
7247         (treated-type
7248          (or (not type)
7249              (catch 'found
7250                (let ((list gnus-article-treat-types))
7251                  (while list
7252                    (when (string-match (pop list) type)
7253                      (throw 'found t)))))))
7254         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7255         val elem)
7256     (gnus-run-hooks 'gnus-part-display-hook)
7257     (dolist (elem alist)
7258       (setq val
7259             (save-excursion
7260               (when (gnus-buffer-live-p gnus-summary-buffer)
7261                 (set-buffer gnus-summary-buffer))
7262               (symbol-value (car elem))))
7263       (when (and (or (consp val)
7264                      treated-type)
7265                  (gnus-treat-predicate val)
7266                  (or (not (get (car elem) 'highlight))
7267                      highlightp))
7268         (save-restriction
7269           (funcall (cadr elem)))))))
7270
7271 ;; Dynamic variables.
7272 (eval-when-compile
7273   (defvar part-number)
7274   (defvar total-parts)
7275   (defvar type)
7276   (defvar condition)
7277   (defvar length))
7278
7279 (defun gnus-treat-predicate (val)
7280   (cond
7281    ((null val)
7282     nil)
7283    (condition
7284     (eq condition val))
7285    ((and (listp val)
7286          (stringp (car val)))
7287     (apply 'gnus-or (mapcar `(lambda (s)
7288                                (string-match s ,(or gnus-newsgroup-name "")))
7289                             val)))
7290    ((listp val)
7291     (let ((pred (pop val)))
7292       (cond
7293        ((eq pred 'or)
7294         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7295        ((eq pred 'and)
7296         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7297        ((eq pred 'not)
7298         (not (gnus-treat-predicate (car val))))
7299        ((eq pred 'typep)
7300         (equal (car val) type))
7301        (t
7302         (error "%S is not a valid predicate" pred)))))
7303    ((eq val t)
7304     t)
7305    ((eq val 'head)
7306     nil)
7307    ((eq val 'first)
7308     (eq part-number 1))
7309    ((eq val 'last)
7310     (eq part-number total-parts))
7311    ((numberp val)
7312     (< length val))
7313    (t
7314     (error "%S is not a valid value" val))))
7315
7316 (defun gnus-article-encrypt-body (protocol &optional n)
7317   "Encrypt the article body."
7318   (interactive
7319    (list
7320     (or gnus-article-encrypt-protocol
7321         (completing-read "Encrypt protocol: "
7322                          gnus-article-encrypt-protocol-alist
7323                          nil t))
7324     current-prefix-arg))
7325   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7326     (unless func
7327       (error "Can't find the encrypt protocol %s" protocol))
7328     (if (member gnus-newsgroup-name '("nndraft:delayed"
7329                                       "nndraft:drafts"
7330                                       "nndraft:queue"))
7331         (error "Can't encrypt the article in group %s"
7332                gnus-newsgroup-name))
7333     (gnus-summary-iterate n
7334       (save-excursion
7335         (set-buffer gnus-summary-buffer)
7336         (let ((mail-parse-charset gnus-newsgroup-charset)
7337               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7338               (summary-buffer gnus-summary-buffer)
7339               references point)
7340           (gnus-set-global-variables)
7341           (when (gnus-group-read-only-p)
7342             (error "The current newsgroup does not support article encrypt"))
7343           (gnus-summary-show-article t)
7344           (setq references
7345               (or (mail-header-references gnus-current-headers) ""))
7346           (set-buffer gnus-article-buffer)
7347           (let* ((inhibit-read-only t)
7348                  (headers
7349                   (mapcar (lambda (field)
7350                             (and (save-restriction
7351                                    (message-narrow-to-head)
7352                                    (goto-char (point-min))
7353                                    (search-forward field nil t))
7354                                  (prog2
7355                                      (message-narrow-to-field)
7356                                      (buffer-string)
7357                                    (delete-region (point-min) (point-max))
7358                                    (widen))))
7359                           '("Content-Type:" "Content-Transfer-Encoding:"
7360                             "Content-Disposition:"))))
7361             (message-narrow-to-head)
7362             (message-remove-header "MIME-Version")
7363             (goto-char (point-max))
7364             (setq point (point))
7365             (insert (apply 'concat headers))
7366             (widen)
7367             (narrow-to-region point (point-max))
7368             (let ((message-options message-options))
7369               (message-options-set 'message-sender user-mail-address)
7370               (message-options-set 'message-recipients user-mail-address)
7371               (message-options-set 'message-sign-encrypt 'not)
7372               (funcall func))
7373             (goto-char (point-min))
7374             (insert "MIME-Version: 1.0\n")
7375             (widen)
7376             (gnus-summary-edit-article-done
7377              references nil summary-buffer t))
7378           (when gnus-keep-backlog
7379             (gnus-backlog-remove-article
7380              (car gnus-article-current) (cdr gnus-article-current)))
7381           (save-excursion
7382             (when (get-buffer gnus-original-article-buffer)
7383               (set-buffer gnus-original-article-buffer)
7384               (setq gnus-original-article nil)))
7385           (when gnus-use-cache
7386             (gnus-cache-update-article
7387              (car gnus-article-current) (cdr gnus-article-current))))))))
7388
7389 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7390   "The following specs can be used:
7391 %t  The security MIME type
7392 %i  Additional info
7393 %d  Details
7394 %D  Details if button is pressed")
7395
7396 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7397   "The following specs can be used:
7398 %t  The security MIME type
7399 %i  Additional info
7400 %d  Details
7401 %D  Details if button is pressed")
7402
7403 (defvar gnus-mime-security-button-line-format-alist
7404   '((?t gnus-tmp-type ?s)
7405     (?i gnus-tmp-info ?s)
7406     (?d gnus-tmp-details ?s)
7407     (?D gnus-tmp-pressed-details ?s)))
7408
7409 (defvar gnus-mime-security-button-commands
7410   '((gnus-article-press-button "\r" "Show Detail")
7411     (undefined "v")
7412     (undefined "t")
7413     (undefined "C")
7414     (gnus-mime-security-save-part "o" "Save...")
7415     (undefined "\C-o")
7416     (undefined "r")
7417     (undefined "d")
7418     (undefined "c")
7419     (undefined "i")
7420     (undefined "E")
7421     (undefined "e")
7422     (undefined "p")
7423     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
7424     (undefined ".")))
7425
7426 (defvar gnus-mime-security-button-map
7427   (let ((map (make-sparse-keymap)))
7428     (define-key map gnus-mouse-2 'gnus-article-push-button)
7429     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
7430     (dolist (c gnus-mime-security-button-commands)
7431       (define-key map (cadr c) (car c)))
7432     map))
7433
7434 (easy-menu-define
7435   gnus-mime-security-button-menu gnus-mime-security-button-map
7436   "Security button menu."
7437   `("Security Part"
7438     ,@(delq nil
7439             (mapcar (lambda (c)
7440                       (unless (eq (car c) 'undefined)
7441                         (vector (caddr c) (car c) :enable t)))
7442                     gnus-mime-security-button-commands))))
7443
7444 (defun gnus-mime-security-button-menu (event prefix)
7445   "Construct a context-sensitive menu of security commands."
7446   (interactive "e\nP")
7447   (save-window-excursion
7448     (let ((pos (event-start event)))
7449       (select-window (posn-window pos))
7450       (goto-char (posn-point pos))
7451       (gnus-article-check-buffer)
7452       (popup-menu gnus-mime-security-button-menu nil prefix))))
7453
7454 (defvar gnus-mime-security-details-buffer nil)
7455
7456 (defvar gnus-mime-security-button-pressed nil)
7457
7458 (defvar gnus-mime-security-show-details-inline t
7459   "If non-nil, show details in the article buffer.")
7460
7461 (defun gnus-mime-security-verify-or-decrypt (handle)
7462   (mm-remove-parts (cdr handle))
7463   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7464         point (inhibit-read-only t))
7465     (if region
7466         (goto-char (car region)))
7467     (save-restriction
7468       (narrow-to-region (point) (point))
7469       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7470         (let* ((mm-verify-option 'known)
7471                (mm-decrypt-option 'known)
7472                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7473           (unless (eq nparts (cdr handle))
7474             (mm-destroy-parts (cdr handle))
7475             (setcdr handle nparts))))
7476       (setq point (point))
7477       (gnus-mime-display-security handle)
7478       (goto-char (point-max)))
7479     (when region
7480       (delete-region (point) (cdr region))
7481       (set-marker (car region) nil)
7482       (set-marker (cdr region) nil))
7483     (goto-char point)))
7484
7485 (defun gnus-mime-security-show-details (handle)
7486   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7487     (if (not details)
7488         (gnus-message 5 "No details.")
7489       (if gnus-mime-security-show-details-inline
7490           (let ((gnus-mime-security-button-pressed
7491                  (not (get-text-property (point) 'gnus-mime-details)))
7492                 (gnus-mime-security-button-line-format
7493                  (get-text-property (point) 'gnus-line-format))
7494                 (inhibit-read-only t))
7495             (forward-char -1)
7496             (while (eq (get-text-property (point) 'gnus-line-format)
7497                        gnus-mime-security-button-line-format)
7498               (forward-char -1))
7499             (forward-char)
7500             (save-restriction
7501               (narrow-to-region (point) (point))
7502               (gnus-insert-mime-security-button handle))
7503             (delete-region (point)
7504                            (or (text-property-not-all
7505                                 (point) (point-max)
7506                                 'gnus-line-format
7507                                 gnus-mime-security-button-line-format)
7508                                (point-max))))
7509         ;; Not inlined.
7510         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7511             (with-current-buffer gnus-mime-security-details-buffer
7512               (erase-buffer)
7513               t)
7514           (setq gnus-mime-security-details-buffer
7515                 (gnus-get-buffer-create "*MIME Security Details*")))
7516         (with-current-buffer gnus-mime-security-details-buffer
7517           (insert details)
7518           (goto-char (point-min)))
7519         (pop-to-buffer gnus-mime-security-details-buffer)))))
7520
7521 (defun gnus-mime-security-press-button (handle)
7522   (save-excursion
7523     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7524         (gnus-mime-security-show-details handle)
7525       (gnus-mime-security-verify-or-decrypt handle))))
7526
7527 (defun gnus-insert-mime-security-button (handle &optional displayed)
7528   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7529          (gnus-tmp-type
7530           (concat
7531            (or (nth 2 (assoc protocol mm-verify-function-alist))
7532                (nth 2 (assoc protocol mm-decrypt-function-alist))
7533                "Unknown")
7534            (if (equal (car handle) "multipart/signed")
7535                " Signed" " Encrypted")
7536            " Part"))
7537          (gnus-tmp-info
7538           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7539               "Undecided"))
7540          (gnus-tmp-details
7541           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7542          gnus-tmp-pressed-details
7543          b e)
7544     (setq gnus-tmp-details
7545           (if gnus-tmp-details
7546               (concat "\n" gnus-tmp-details)
7547             ""))
7548     (setq gnus-tmp-pressed-details
7549           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7550     (unless (bolp)
7551       (insert "\n"))
7552     (setq b (point))
7553     (gnus-eval-format
7554      gnus-mime-security-button-line-format
7555      gnus-mime-security-button-line-format-alist
7556      `(keymap ,gnus-mime-security-button-map
7557          gnus-callback gnus-mime-security-press-button
7558          gnus-line-format ,gnus-mime-security-button-line-format
7559          gnus-mime-details ,gnus-mime-security-button-pressed
7560          article-type annotation
7561          gnus-data ,handle))
7562     (setq e (if (bolp)
7563                 ;; Exclude a newline.
7564                 (1- (point))
7565               (point)))
7566     (widget-convert-button
7567      'link b e
7568      :mime-handle handle
7569      :action 'gnus-widget-press-button
7570      :button-keymap gnus-mime-security-button-map
7571      :help-echo
7572      (lambda (widget/window &optional overlay pos)
7573        ;; Needed to properly clear the message due to a bug in
7574        ;; wid-edit (XEmacs only).
7575        (when (boundp 'help-echo-owns-message)
7576          (setq help-echo-owns-message t))
7577        (format
7578         "%S: show detail; %S: more options"
7579         (aref gnus-mouse-2 0)
7580         (aref gnus-down-mouse-3 0))))))
7581
7582 (defun gnus-mime-display-security (handle)
7583   (save-restriction
7584     (narrow-to-region (point) (point))
7585     (unless (gnus-unbuttonized-mime-type-p (car handle))
7586       (gnus-insert-mime-security-button handle))
7587     (gnus-mime-display-part (cadr handle))
7588     (unless (bolp)
7589       (insert "\n"))
7590     (unless (gnus-unbuttonized-mime-type-p (car handle))
7591       (let ((gnus-mime-security-button-line-format
7592              gnus-mime-security-button-end-line-format))
7593         (gnus-insert-mime-security-button handle)))
7594     (mm-set-handle-multipart-parameter
7595      handle 'gnus-region
7596      (cons (set-marker (make-marker) (point-min))
7597            (set-marker (make-marker) (point-max))))))
7598
7599 (defun gnus-mime-security-run-function (function)
7600   "Run FUNCTION with the security part under point."
7601   (gnus-article-check-buffer)
7602   (let ((data (get-text-property (point) 'gnus-data))
7603         buffer handle)
7604     (when (and (stringp (car-safe data))
7605                (setq buffer (mm-handle-multipart-original-buffer data))
7606                (setq handle (cadr data)))
7607       (if (bufferp (mm-handle-buffer handle))
7608           (progn
7609             (setq handle (cons buffer (copy-sequence (cdr handle))))
7610             (mm-handle-set-undisplayer handle nil))
7611         (setq handle (mm-make-handle
7612                       buffer
7613                       (mm-handle-multipart-ctl-parameter handle 'protocol)
7614                       nil nil nil nil nil nil)))
7615       (funcall function handle))))
7616
7617 (defun gnus-mime-security-save-part ()
7618   "Save the security part under point."
7619   (interactive)
7620   (gnus-mime-security-run-function 'mm-save-part))
7621
7622 (defun gnus-mime-security-pipe-part ()
7623   "Pipe the security part under point to a process."
7624   (interactive)
7625   (gnus-mime-security-run-function 'mm-pipe-part))
7626
7627 (gnus-ems-redefine)
7628
7629 (provide 'gnus-art)
7630
7631 (run-hooks 'gnus-art-load-hook)
7632
7633 ;;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
7634 ;;; gnus-art.el ends here