home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 31 / CDASC_31_1996_juillet_aout.iso / vrac_os2 / e31el3.zip / EMACS / 19.31 / LISP / NNHEADER.EL < prev    next >
Text File  |  1996-02-27  |  14KB  |  370 lines

  1. ;;; nnheader.el --- header access macros for Gnus and its backends
  2.  
  3. ;; Copyright (C) 1987,88,89,90,93,94,95 Free Software Foundation, Inc.
  4.  
  5. ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
  6. ;;     Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
  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., 59 Temple Place - Suite 330,
  24. ;; Boston, MA 02111-1307, USA.
  25.  
  26. ;;; Commentary:
  27.  
  28. ;; These macros may look very much like the ones in GNUS 4.1. They
  29. ;; are, in a way, but you should note that the indices they use have
  30. ;; been changed from the internal GNUS format to the NOV format. Makes
  31. ;; it possible to read headers from XOVER much faster.
  32. ;;
  33. ;; The format of a header is now:
  34. ;; [number subject from date id references chars lines xref]
  35. ;;
  36. ;; (That last entry is defined as "misc" in the NOV format, but Gnus
  37. ;; uses it for xrefs.)
  38.  
  39. ;;; Code:
  40.  
  41. (defalias 'nntp-header-number 'mail-header-number)
  42. (defmacro mail-header-number (header)
  43.   "Return article number in HEADER."
  44.   (` (aref (, header) 0)))
  45.  
  46. (defalias 'nntp-set-header-number 'mail-header-set-number)
  47. (defmacro mail-header-set-number (header number)
  48.   "Set article number of HEADER to NUMBER."
  49.   (` (aset (, header) 0 (, number))))
  50.  
  51. (defalias 'nntp-header-subject 'mail-header-subject)
  52. (defmacro mail-header-subject (header)
  53.   "Return subject string in HEADER."
  54.   (` (aref (, header) 1)))
  55.  
  56. (defalias 'nntp-set-header-subject 'mail-header-set-subject)
  57. (defmacro mail-header-set-subject (header subject)
  58.   "Set article subject of HEADER to SUBJECT."
  59.   (` (aset (, header) 1 (, subject))))
  60.  
  61. (defalias 'nntp-header-from 'mail-header-from)
  62. (defmacro mail-header-from (header)
  63.   "Return author string in HEADER."
  64.   (` (aref (, header) 2)))
  65.  
  66. (defalias 'nntp-set-header-from 'mail-header-set-from)
  67. (defmacro mail-header-set-from (header from)
  68.   "Set article author of HEADER to FROM."
  69.   (` (aset (, header) 2 (, from))))
  70.  
  71. (defalias 'nntp-header-date 'mail-header-date)
  72. (defmacro mail-header-date (header)
  73.   "Return date in HEADER."
  74.   (` (aref (, header) 3)))
  75.  
  76. (defalias 'nntp-set-header-date 'mail-header-set-date)
  77. (defmacro mail-header-set-date (header date)
  78.   "Set article date of HEADER to DATE."
  79.   (` (aset (, header) 3 (, date))))
  80.  
  81. (defalias 'nntp-header-id 'mail-header-id)
  82. (defmacro mail-header-id (header)
  83.   "Return Id in HEADER."
  84.   (` (aref (, header) 4)))
  85.  
  86. (defalias 'nntp-set-header-id 'mail-header-set-id)
  87. (defmacro mail-header-set-id (header id)
  88.   "Set article Id of HEADER to ID."
  89.   (` (aset (, header) 4 (, id))))
  90.  
  91. (defalias 'nntp-header-references 'mail-header-references)
  92. (defmacro mail-header-references (header)
  93.   "Return references in HEADER."
  94.   (` (aref (, header) 5)))
  95.  
  96. (defalias 'nntp-set-header-references 'mail-header-set-references)
  97. (defmacro mail-header-set-references (header ref)
  98.   "Set article references of HEADER to REF."
  99.   (` (aset (, header) 5 (, ref))))
  100.  
  101. (defalias 'nntp-header-chars 'mail-header-chars)
  102. (defmacro mail-header-chars (header)
  103.   "Return number of chars of article in HEADER."
  104.   (` (aref (, header) 6)))
  105.  
  106. (defalias 'nntp-set-header-chars 'mail-header-set-chars)
  107. (defmacro mail-header-set-chars (header chars)
  108.   "Set number of chars in article of HEADER to CHARS."
  109.   (` (aset (, header) 6 (, chars))))
  110.  
  111. (defalias 'nntp-header-lines 'mail-header-lines)
  112. (defmacro mail-header-lines (header)
  113.   "Return lines in HEADER."
  114.   (` (aref (, header) 7)))
  115.  
  116. (defalias 'nntp-set-header-lines 'mail-header-set-lines)
  117. (defmacro mail-header-set-lines (header lines)
  118.   "Set article lines of HEADER to LINES."
  119.   (` (aset (, header) 7 (, lines))))
  120.  
  121. (defalias 'nntp-header-xref 'mail-header-xref)
  122. (defmacro mail-header-xref (header)
  123.   "Return xref string in HEADER."
  124.   (` (aref (, header) 8)))
  125.  
  126. (defalias 'nntp-set-header-xref 'mail-header-set-xref)
  127. (defmacro mail-header-set-xref (header xref)
  128.   "Set article xref of HEADER to xref."
  129.   (` (aset (, header) 8 (, xref))))
  130.  
  131.  
  132. ;; Various cruft the backends and Gnus need to communicate.
  133.  
  134. (defvar nntp-server-buffer nil)
  135. (defvar gnus-verbose-backends t
  136.   "*If non-nil, Gnus backends will generate lots of comments.")
  137. (defvar gnus-nov-is-evil nil
  138.   "If non-nil, Gnus backends will never output headers in the NOV format.")
  139. (defvar news-reply-yank-from nil)
  140. (defvar news-reply-yank-message-id nil)
  141.  
  142. ;; All backends use this function, so I moved it to this file.
  143.  
  144. (defun nnheader-init-server-buffer ()
  145.   (save-excursion
  146.     (setq nntp-server-buffer (get-buffer-create " *nntpd*"))
  147.     (set-buffer nntp-server-buffer)
  148.     (buffer-disable-undo (current-buffer))
  149.     (erase-buffer)
  150.     (kill-all-local-variables)
  151.     (setq case-fold-search t)        ;Should ignore case.
  152.     t))
  153.  
  154. (defun nnheader-set-init-variables (server defs)
  155.   (let ((s server)
  156.     val)
  157.     ;; First we set the server variables in the sequence required.  We
  158.     ;; use the definitions from the `defs' list where that is
  159.     ;; possible. 
  160.     (while s
  161.       (set (car (car s)) 
  162.        (if (setq val (assq (car (car s)) defs))
  163.            (nth 1 val)
  164.          (nth 1 (car s))))
  165.       (setq s (cdr s)))
  166.     ;; The we go through the defs list and set any variables that were
  167.     ;; not set in the first sweep.
  168.     (while defs
  169.       (if (not (assq (car (car defs)) server))
  170.       (set (car (car defs)) 
  171.            (if (and (symbolp (nth 1 (car defs)))
  172.             (not (boundp (nth 1 (car defs)))))
  173.            (nth 1 (car defs))
  174.          (eval (nth 1 (car defs))))))
  175.       (setq defs (cdr defs)))))
  176.  
  177. (defun nnheader-save-variables (server)
  178.   (let (out)
  179.     (while server
  180.       (setq out (cons (list (car (car server)) 
  181.                 (symbol-value (car (car server))))
  182.               out))
  183.       (setq server (cdr server)))
  184.     (nreverse out)))
  185.  
  186. (defun nnheader-restore-variables (state)
  187.   (while state
  188.     (set (car (car state)) (nth 1 (car state)))
  189.     (setq state (cdr state))))
  190.  
  191. (defvar nnheader-max-head-length 4096
  192.   "The maximum length of a HEAD.")
  193.  
  194. (defun nnheader-insert-head (file)
  195.   "Insert the head of the article."
  196.   (if (eq nnheader-max-head-length t)
  197.       ;; Just read the entire file.
  198.       (insert-file-contents-literally file)
  199.     ;; Read 1K blocks until we find a separator.
  200.     (let ((beg 0)
  201.       (chop 1024))
  202.       (while (and (eq chop (nth 1 (insert-file-contents-literally
  203.                    file nil beg (setq beg (+ beg chop)))))
  204.           (prog1 (not (search-forward "\n\n" nil t)) 
  205.             (goto-char (point-max)))
  206.           (or (null nnheader-max-head-length)
  207.               (< beg nnheader-max-head-length)))))))
  208.  
  209. (defun nnheader-article-p ()
  210.   (goto-char (point-min))
  211.   (if (not (search-forward "\n\n" nil t))
  212.       nil
  213.     (narrow-to-region (point-min) (1- (point)))
  214.     (goto-char (point-min))
  215.     (while (looking-at "[A-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
  216.       (goto-char (match-end 0)))
  217.     (prog1
  218.     (eobp)
  219.       (widen))))    
  220.  
  221. ;; Written by Erik Naggum <erik@naggum.no>.
  222. (defun nnheader-insert-file-contents-literally (filename &optional visit beg end replace)
  223.   "Like `insert-file-contents', q.v., but only reads in the file.
  224. A buffer may be modified in several ways after reading into the buffer due
  225. to advanced Emacs features, such as file-name-handlers, format decoding,
  226. find-file-hooks, etc.
  227.   This function ensures that none of these modifications will take place."
  228.   (let (                ; (file-name-handler-alist nil)
  229.     (format-alist nil)
  230.     (after-insert-file-functions nil)
  231.     (find-buffer-file-type-function 
  232.      (if (fboundp 'find-buffer-file-type)
  233.          (symbol-function 'find-buffer-file-type)
  234.        nil)))
  235.     (unwind-protect
  236.     (progn
  237.       (fset 'find-buffer-file-type (lambda (filename) t))
  238.       (insert-file-contents filename visit beg end replace))
  239.       (if find-buffer-file-type-function
  240.       (fset 'find-buffer-file-type find-buffer-file-type-function)
  241.     (fmakunbound 'find-buffer-file-type)))))
  242.  
  243. (defun nnheader-find-file-noselect (filename &optional nowarn rawfile)
  244.   "Read file FILENAME into a buffer and return the buffer.
  245. If a buffer exists visiting FILENAME, return that one, but
  246. verify that the file has not changed since visited or saved.
  247. The buffer is not selected, just returned to the caller."
  248.   (setq filename
  249.     (abbreviate-file-name
  250.      (expand-file-name filename)))
  251.   (if (file-directory-p filename)
  252.       (if find-file-run-dired
  253.       (dired-noselect filename)
  254.     (error "%s is a directory." filename))
  255.     (let* ((buf (get-file-buffer filename))
  256.        (truename (abbreviate-file-name (file-truename filename)))
  257.        (number (nthcdr 10 (file-attributes truename)))
  258.        ;; Find any buffer for a file which has same truename.
  259.        (other (and (not buf) 
  260.                (if (fboundp 'find-buffer-visiting)
  261.                (find-buffer-visiting filename)
  262.              (get-file-buffer filename))))
  263.        error)
  264.       ;; Let user know if there is a buffer with the same truename.
  265.       (if other
  266.       (progn
  267.         (or nowarn
  268.         (string-equal filename (buffer-file-name other))
  269.         (message "%s and %s are the same file"
  270.              filename (buffer-file-name other)))
  271.         ;; Optionally also find that buffer.
  272.         (if (or (and (boundp 'find-file-existing-other-name)
  273.              find-file-existing-other-name)
  274.             find-file-visit-truename)
  275.         (setq buf other))))
  276.       (if buf
  277.       (or nowarn
  278.           (verify-visited-file-modtime buf)
  279.           (cond ((not (file-exists-p filename))
  280.              (error "File %s no longer exists!" filename))
  281.             ((yes-or-no-p
  282.               (if (string= (file-name-nondirectory filename)
  283.                    (buffer-name buf))
  284.               (format
  285.                (if (buffer-modified-p buf)
  286.                    "File %s changed on disk.  Discard your edits? "
  287.                  "File %s changed on disk.  Reread from disk? ")
  288.                (file-name-nondirectory filename))
  289.             (format
  290.              (if (buffer-modified-p buf)
  291.                  "File %s changed on disk.  Discard your edits in %s? "
  292.                "File %s changed on disk.  Reread from disk into %s? ")
  293.              (file-name-nondirectory filename)
  294.              (buffer-name buf))))
  295.              (save-excursion
  296.                (set-buffer buf)
  297.                (revert-buffer t t)))))
  298.     (save-excursion
  299. ;;; The truename stuff makes this obsolete.
  300. ;;;      (let* ((link-name (car (file-attributes filename)))
  301. ;;;         (linked-buf (and (stringp link-name)
  302. ;;;                  (get-file-buffer link-name))))
  303. ;;;        (if (bufferp linked-buf)
  304. ;;;        (message "Symbolic link to file in buffer %s"
  305. ;;;             (buffer-name linked-buf))))
  306.       (setq buf (create-file-buffer filename))
  307.       ;;      (set-buffer-major-mode buf)
  308.       (set-buffer buf)
  309.       (erase-buffer)
  310.       (if rawfile
  311.           (condition-case ()
  312.           (nnheader-insert-file-contents-literally filename t)
  313.         (file-error
  314.          ;; Unconditionally set error
  315.          (setq error t)))
  316.         (condition-case ()
  317.         (insert-file-contents filename t)
  318.           (file-error
  319.            ;; Run find-file-not-found-hooks until one returns non-nil.
  320.            (or t            ; (run-hook-with-args-until-success 'find-file-not-found-hooks)
  321.            ;; If they fail too, set error.
  322.            (setq error t)))))
  323.       ;; Find the file's truename, and maybe use that as visited name.
  324.       (setq buffer-file-truename truename)
  325.       (setq buffer-file-number number)
  326.       ;; On VMS, we may want to remember which directory in a search list
  327.       ;; the file was found in.
  328.       (and (eq system-type 'vax-vms)
  329.            (let (logical)
  330.          (if (string-match ":" (file-name-directory filename))
  331.              (setq logical (substring (file-name-directory filename)
  332.                           0 (match-beginning 0))))
  333.          (not (member logical find-file-not-true-dirname-list)))
  334.            (setq buffer-file-name buffer-file-truename))
  335.       (if find-file-visit-truename
  336.           (setq buffer-file-name
  337.             (setq filename
  338.               (expand-file-name buffer-file-truename))))
  339.       ;; Set buffer's default directory to that of the file.
  340.       (setq default-directory (file-name-directory filename))
  341.       ;; Turn off backup files for certain file names.  Since
  342.       ;; this is a permanent local, the major mode won't eliminate it.
  343.       (and (not (funcall backup-enable-predicate buffer-file-name))
  344.            (progn
  345.          (make-local-variable 'backup-inhibited)
  346.          (setq backup-inhibited t)))
  347.       (if rawfile
  348.           nil
  349.         (after-find-file error (not nowarn)))))
  350.       buf)))
  351.  
  352. (defun nnheader-insert-references (references message-id)
  353.   (if (and (not references) (not message-id)) 
  354.       () ; This is illegal, but not all articles have Message-IDs.
  355.     (mail-position-on-field "References")
  356.     ;; Fold long references line to follow RFC1036.
  357.     (let ((begin (gnus-point-at-bol))
  358.       (fill-column 78)
  359.       (fill-prefix "\t"))
  360.       (if references (insert references))
  361.       (if (and references message-id) (insert " "))
  362.       (if message-id (insert message-id))
  363.       ;; The region must end with a newline to fill the region
  364.       ;; without inserting extra newline.
  365.       (fill-region-as-paragraph begin (1+ (point))))))
  366.  
  367. (provide 'nnheader)
  368.  
  369. ;;; nnheader.el ends here
  370.