1 ;;; nnheader.el --- header access macros for Gnus and its backends
3 ;; Copyright (C) 1987, 1988, 1989, 1990, 1993, 1994, 1995, 1996,
5 ;; Free Software Foundation, Inc.
7 ;; Author: Masanobu UMEDA <umerin@flab.flab.fujitsu.junet>
8 ;; Lars Magne Ingebrigtsen <larsi@gnus.org>
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
32 (eval-when-compile (require 'cl))
37 (defvar nnheader-max-head-length 4096
38 "*Max length of the head of articles.")
40 (defvar nnheader-head-chop-length 2048
41 "*Length of each read operation when trying to fetch HEAD headers.")
43 (defvar nnheader-file-name-translation-alist nil
44 "*Alist that says how to translate characters in file names.
45 For instance, if \":\" is invalid as a file character in file names
46 on your system, you could say something like:
48 \(setq nnheader-file-name-translation-alist '((?: . ?_)))")
51 (autoload 'nnmail-message-id "nnmail")
52 (autoload 'mail-position-on-field "sendmail")
53 (autoload 'message-remove-header "message")
54 (autoload 'gnus-point-at-eol "gnus-util")
55 (autoload 'gnus-delete-line "gnus-util")
56 (autoload 'gnus-buffer-live-p "gnus-util"))
58 ;;; Header access macros.
62 ;; These macros may look very much like the ones in GNUS 4.1. They
63 ;; are, in a way, but you should note that the indices they use have
64 ;; been changed from the internal GNUS format to the NOV format. The
65 ;; makes it possible to read headers from XOVER much faster.
67 ;; The format of a header is now:
68 ;; [number subject from date id references chars lines xref extra]
70 ;; (That next-to-last entry is defined as "misc" in the NOV format,
71 ;; but Gnus uses it for xrefs.)
73 (defalias 'mail-header-number 'mime-entity-location-internal)
74 (defalias 'mail-header-set-number 'mime-entity-set-location-internal)
76 (defalias 'mail-header-subject 'mime-gnus-entity-subject-internal)
77 (defalias 'mail-header-set-subject 'mime-gnus-entity-set-subject-internal)
79 (defalias 'mail-header-from 'mime-gnus-entity-from-internal)
80 (defalias 'mail-header-set-from 'mime-gnus-entity-set-from-internal)
82 (defalias 'mail-header-date 'mime-gnus-entity-date-internal)
83 (defalias 'mail-header-set-date 'mime-gnus-entity-set-date-internal)
85 (defalias 'mail-header-message-id 'mime-gnus-entity-id-internal)
86 (defalias 'mail-header-id 'mime-gnus-entity-id-internal)
87 (defalias 'mail-header-set-message-id 'mime-gnus-entity-set-id-internal)
88 (defalias 'mail-header-set-id 'mime-gnus-entity-set-id-internal)
90 (defalias 'mail-header-references 'mime-gnus-entity-references-internal)
91 (defalias 'mail-header-set-references
92 'mime-gnus-entity-set-references-internal)
94 (defalias 'mail-header-chars 'mime-gnus-entity-chars-internal)
95 (defalias 'mail-header-set-chars 'mime-gnus-entity-set-chars-internal)
97 (defalias 'mail-header-lines 'mime-gnus-entity-lines-internal)
98 (defalias 'mail-header-set-lines 'mime-gnus-entity-set-lines-internal)
100 (defalias 'mail-header-xref 'mime-gnus-entity-xref-internal)
101 (defalias 'mail-header-set-xref 'mime-gnus-entity-set-xref-internal)
103 (defalias 'mail-header-extra 'mime-gnus-entity-extra-internal)
104 (defalias 'mail-header-set-extra 'mime-gnus-entity-set-extra-internal)
106 (defalias 'nnheader-decode-subject (mime-find-field-decoder 'Subject 'nov))
107 (defalias 'nnheader-decode-from (mime-find-field-decoder 'From 'nov))
109 (defsubst nnheader-decode-field-body (field-body field-name
110 &optional mode max-column)
111 (mime-decode-field-body field-body
112 (if (stringp field-name)
113 (intern (capitalize field-name))
117 (defsubst make-full-mail-header (&optional number subject from date id
118 references chars lines xref
120 "Create a new mail header structure initialized with the parameters given."
121 (luna-make-entity (mm-expand-class-name 'gnus)
124 (nnheader-decode-subject subject))
126 (nnheader-decode-from from))
129 :references references
136 (let ((func (mime-find-field-decoder
139 (funcall func (cdr field))
144 (cons 'Subject subject)
148 (defsubst make-full-mail-header-from-decoded-header
149 (&optional number subject from date id references chars lines xref extra)
150 "Create a new mail header structure initialized with the parameters given."
151 (luna-make-entity (mm-expand-class-name 'gnus)
157 :references references
163 (defsubst make-mail-header (&optional init)
164 "Create a new mail header structure initialized with INIT."
165 (make-full-mail-header init init init init init
166 init init init init init))
168 ;; fake message-ids: generation and detection
170 (defvar nnheader-fake-message-id 1)
172 (defsubst nnheader-generate-fake-message-id ()
173 (concat "fake+none+" (int-to-string (incf nnheader-fake-message-id))))
175 (defsubst nnheader-fake-message-id-p (id)
176 (save-match-data ; regular message-id's are <.*>
177 (string-match "\\`fake\\+none\\+[0-9]+\\'" id)))
179 ;; Parsing headers and NOV lines.
181 (defsubst nnheader-header-value ()
182 (buffer-substring (match-end 0) (gnus-point-at-eol)))
184 (defun nnheader-parse-head (&optional naked)
185 (let ((case-fold-search t)
186 (cur (current-buffer))
187 (buffer-read-only nil)
188 in-reply-to lines p ref)
189 (goto-char (point-min))
192 ;; Search to the beginning of the next header. Error messages
193 ;; do not begin with 2 or 3.
195 (when (or naked (re-search-forward "^[23][0-9]+ " nil t))
196 ;; This implementation of this function, with nine
197 ;; search-forwards instead of the one re-search-forward and
198 ;; a case (which basically was the old function) is actually
199 ;; about twice as fast, even though it looks messier. You
200 ;; can't have everything, I guess. Speed and elegance
201 ;; don't always go hand in hand.
202 (make-full-mail-header
212 (narrow-to-region (point)
213 (or (and (search-forward "\n.\n" nil t)
219 (if (search-forward "\nsubject: " nil t)
220 (nnheader-header-value) "(none)"))
224 (if (search-forward "\nfrom: " nil t)
225 (nnheader-header-value) "(nobody)"))
229 (if (search-forward "\ndate: " nil t)
230 (nnheader-header-value) ""))
234 (if (search-forward "\nmessage-id:" nil t)
236 (1- (or (search-forward "<" (gnus-point-at-eol) t)
238 (or (search-forward ">" (gnus-point-at-eol) t) (point)))
239 ;; If there was no message-id, we just fake one to make
240 ;; subsequent routines simpler.
241 (nnheader-generate-fake-message-id)))
245 (if (search-forward "\nreferences: " nil t)
246 (nnheader-header-value)
247 ;; Get the references from the in-reply-to header if there
248 ;; were no references and the in-reply-to header looks
250 (if (and (search-forward "\nin-reply-to: " nil t)
251 (setq in-reply-to (nnheader-header-value))
252 (string-match "<[^\n>]+>" in-reply-to))
254 (setq ref (substring in-reply-to (match-beginning 0)
256 (while (string-match "<[^\n>]+>"
257 in-reply-to (match-end 0))
258 (setq ref2 (substring in-reply-to (match-beginning 0)
260 (when (> (length ref2) (length ref))
269 (if (search-forward "\nlines: " nil t)
270 (if (numberp (setq lines (read cur)))
276 (and (search-forward "\nxref: " nil t)
277 (nnheader-header-value)))
280 (when nnmail-extra-headers
281 (let ((extra nnmail-extra-headers)
285 (when (search-forward
286 (concat "\n" (symbol-name (car extra)) ": ") nil t)
287 (push (cons (car extra) (nnheader-header-value))
292 (goto-char (point-min))
295 (defmacro nnheader-nov-skip-field ()
296 '(search-forward "\t" eol 'move))
298 (defmacro nnheader-nov-field ()
299 '(buffer-substring (point) (if (nnheader-nov-skip-field) (1- (point)) eol)))
301 (defmacro nnheader-nov-read-integer ()
303 (if (eq (char-after) ?\t)
305 (let ((num (condition-case nil
306 (read (current-buffer))
308 (if (numberp num) num 0)))
310 (search-forward "\t" eol 'move))))
312 (defmacro nnheader-nov-parse-extra ()
314 (while (not (memq (char-after) '(?\n nil)))
315 (setq string (nnheader-nov-field))
316 (when (string-match "^\\([^ :]+\\): " string)
317 (push (cons (intern (match-string 1 string))
318 (substring string (match-end 0)))
322 (defmacro nnheader-nov-read-message-id ()
323 '(let ((id (nnheader-nov-field)))
324 (if (string-match "^<[^>]+>$" id)
326 (nnheader-generate-fake-message-id))))
328 (defun nnheader-parse-nov ()
329 (let ((eol (gnus-point-at-eol)))
330 (make-full-mail-header
331 (nnheader-nov-read-integer) ; number
332 (nnheader-nov-field) ; subject
333 (nnheader-nov-field) ; from
334 (nnheader-nov-field) ; date
335 (nnheader-nov-read-message-id) ; id
336 (nnheader-nov-field) ; refs
337 (nnheader-nov-read-integer) ; chars
338 (nnheader-nov-read-integer) ; lines
339 (if (eq (char-after) ?\n)
341 (nnheader-nov-field)) ; misc
342 (nnheader-nov-parse-extra)))) ; extra
344 (defun nnheader-insert-nov (header)
345 (princ (mail-header-number header) (current-buffer))
348 (or (mime-entity-fetch-field header 'Subject) "(none)") "\t"
349 (or (mime-entity-fetch-field header 'From) "(nobody)") "\t"
350 (or (mail-header-date header) "") "\t"
351 (or (mail-header-id header)
354 (or (mail-header-references header) "") "\t")
355 (princ (or (mail-header-chars header) 0) (current-buffer))
357 (princ (or (mail-header-lines header) 0) (current-buffer))
359 (when (mail-header-xref header)
360 (insert "Xref: " (mail-header-xref header)))
361 (when (or (mail-header-xref header)
362 (mail-header-extra header))
364 (when (mail-header-extra header)
365 (let ((extra (mail-header-extra header))
368 (setq field (pop extra)
369 value (mime-entity-fetch-field header (car field)))
370 (insert (symbol-name (car field)) ": " (or value (cdr field)) "\t"))))
373 (defun nnheader-insert-header (header)
375 "Subject: " (or (mail-header-subject header) "(none)") "\n"
376 "From: " (or (mail-header-from header) "(nobody)") "\n"
377 "Date: " (or (mail-header-date header) "") "\n"
378 "Message-ID: " (or (mail-header-id header) (nnmail-message-id)) "\n"
379 "References: " (or (mail-header-references header) "") "\n"
381 (princ (or (mail-header-lines header) 0) (current-buffer))
384 (defun nnheader-insert-article-line (article)
385 (goto-char (point-min))
387 (princ article (current-buffer))
388 (insert " Article retrieved.\n")
389 (search-forward "\n\n" nil 'move)
390 (delete-region (point) (point-max))
394 (defun nnheader-nov-delete-outside-range (beg end)
395 "Delete all NOV lines that lie outside the BEG to END range."
396 ;; First we find the first wanted line.
397 (nnheader-find-nov-line beg)
398 (delete-region (point-min) (point))
399 ;; Then we find the last wanted line.
400 (when (nnheader-find-nov-line end)
402 (delete-region (point) (point-max)))
404 (defun nnheader-find-nov-line (article)
405 "Put point at the NOV line that start with ARTICLE.
406 If ARTICLE doesn't exist, put point where that line
407 would have been. The function will return non-nil if
408 the line could be found."
409 ;; This function basically does a binary search.
410 (let ((max (point-max))
411 (min (goto-char (point-min)))
412 (cur (current-buffer))
416 (goto-char (/ (+ max min) 2))
418 (if (or (= (point) prev)
422 (while (and (not (numberp (setq num (read cur))))
425 (cond ((> num article)
430 (setq found 'yes)))))
431 ;; We may be at the first line.
434 (setq num (read cur)))
435 ;; Now we may have found the article we're looking for, or we
436 ;; may be somewhere near it.
437 (when (and (not (eq found 'yes))
438 (not (eq num article)))
440 (while (and (< (point) max)
441 (or (not (numberp num))
446 (= (setq num (read cur)) article)))
447 (unless (eq num article)
452 ;; Various cruft the backends and Gnus need to communicate.
454 (defvar nntp-server-buffer nil)
455 (defvar gnus-verbose-backends 7
456 "*A number that says how talkative the Gnus backends should be.")
457 (defvar gnus-nov-is-evil nil
458 "If non-nil, Gnus backends will never output headers in the NOV format.")
459 (defvar news-reply-yank-from nil)
460 (defvar news-reply-yank-message-id nil)
462 (defvar nnheader-callback-function nil)
464 (defun nnheader-init-server-buffer ()
465 "Initialize the Gnus-backend communication buffer."
467 (unless (gnus-buffer-live-p nntp-server-buffer)
468 (setq nntp-server-buffer (get-buffer-create " *nntpd*")))
469 (mm-enable-multibyte)
470 (set-buffer nntp-server-buffer)
472 (kill-all-local-variables)
473 (setq case-fold-search t) ;Should ignore case.
476 ;;; Various functions the backends use.
478 (defun nnheader-file-error (file)
479 "Return a string that says what is wrong with FILE."
482 ((not (file-exists-p file))
484 ((file-directory-p file)
486 ((not (file-readable-p file))
487 "%s is not readable"))
490 (defun nnheader-insert-head (file)
491 "Insert the head of the article."
492 (when (file-exists-p file)
493 (if (eq nnheader-max-head-length t)
494 ;; Just read the entire file.
495 (nnheader-insert-file-contents file)
496 ;; Read 1K blocks until we find a separator.
499 (while (and (eq nnheader-head-chop-length
500 (nth 1 (nnheader-insert-file-contents
502 (incf beg nnheader-head-chop-length))))
503 (prog1 (not (search-forward "\n\n" nil t))
504 (goto-char (point-max)))
505 (or (null nnheader-max-head-length)
506 (< beg nnheader-max-head-length))))))
509 (defun nnheader-article-p ()
510 "Say whether the current buffer looks like an article."
511 (goto-char (point-min))
512 (if (not (search-forward "\n\n" nil t))
514 (narrow-to-region (point-min) (1- (point)))
515 (goto-char (point-min))
516 (while (looking-at "[a-zA-Z][^ \t]+:.*\n\\([ \t].*\n\\)*\\|From .*\n")
517 (goto-char (match-end 0)))
522 (defun nnheader-insert-references (references message-id)
523 "Insert a References header based on REFERENCES and MESSAGE-ID."
524 (if (and (not references) (not message-id))
525 ;; This is invalid, but not all articles have Message-IDs.
527 (mail-position-on-field "References")
528 (let ((begin (save-excursion (beginning-of-line) (point)))
533 (when (and references message-id)
537 ;; Fold long References lines to conform to RFC1036 (sort of).
538 ;; The region must end with a newline to fill the region
539 ;; without inserting extra newline.
540 (fill-region-as-paragraph begin (1+ (point))))))
542 (defun nnheader-replace-header (header new-value)
543 "Remove HEADER and insert the NEW-VALUE."
546 (nnheader-narrow-to-headers)
548 (message-remove-header header)
549 (goto-char (point-max))
550 (insert header ": " new-value "\n")))))
552 (defun nnheader-narrow-to-headers ()
553 "Narrow to the head of an article."
556 (goto-char (point-min))
557 (if (search-forward "\n\n" nil t)
560 (goto-char (point-min)))
562 (defun nnheader-set-temp-buffer (name &optional noerase)
563 "Set-buffer to an empty (possibly new) buffer called NAME with undo disabled."
564 (set-buffer (get-buffer-create name))
565 (buffer-disable-undo)
570 (defvar jka-compr-compression-info-list)
571 (defvar nnheader-numerical-files
572 (if (boundp 'jka-compr-compression-info-list)
573 (concat "\\([0-9]+\\)\\("
574 (mapconcat (lambda (i) (aref i 0))
575 jka-compr-compression-info-list "\\|")
578 "Regexp that match numerical files.")
580 (defvar nnheader-numerical-short-files (concat "^" nnheader-numerical-files)
581 "Regexp that matches numerical file names.")
583 (defvar nnheader-numerical-full-files (concat "/" nnheader-numerical-files)
584 "Regexp that matches numerical full file paths.")
586 (defsubst nnheader-file-to-number (file)
587 "Take a file name and return the article number."
588 (if (string= nnheader-numerical-short-files "^[0-9]+$")
590 (string-match nnheader-numerical-short-files file)
591 (string-to-int (match-string 0 file))))
593 (defun nnheader-directory-files-safe (&rest args)
594 ;; It has been reported numerous times that `directory-files'
595 ;; fails with an alarming frequency on NFS mounted file systems.
596 ;; This function executes that function twice and returns
597 ;; the longest result.
598 (let ((first (apply 'directory-files args))
599 (second (apply 'directory-files args)))
600 (if (> (length first) (length second))
604 (defun nnheader-directory-articles (dir)
605 "Return a list of all article files in a directory."
606 (mapcar 'nnheader-file-to-number
607 (nnheader-directory-files-safe
608 dir nil nnheader-numerical-short-files t)))
610 (defun nnheader-article-to-file-alist (dir)
611 "Return an alist of article/file pairs in DIR."
612 (mapcar (lambda (file) (cons (nnheader-file-to-number file) file))
613 (nnheader-directory-files-safe
614 dir nil nnheader-numerical-short-files t)))
616 (defun nnheader-fold-continuation-lines ()
617 "Fold continuation lines in the current buffer."
618 (nnheader-replace-regexp "\\(\r?\n[ \t]+\\)+" " "))
620 (defun nnheader-translate-file-chars (file &optional full)
621 "Translate FILE into something that can be a file name.
622 If FULL, translate everything."
623 (if (null nnheader-file-name-translation-alist)
624 ;; No translation is necessary.
629 ;; Do complete translation.
630 (setq leaf (copy-sequence file)
632 i (if (and (< 1 (length leaf)) (eq ?: (aref leaf 1)))
634 ;; We translate -- but only the file name. We leave the directory
636 (if (string-match "/[^/]+\\'" file)
637 ;; This is needed on NT's and stuff.
638 (setq leaf (substring file (1+ (match-beginning 0)))
639 path (substring file 0 (1+ (match-beginning 0))))
640 ;; Fall back on this.
641 (setq leaf (file-name-nondirectory file)
642 path (file-name-directory file))))
643 (setq len (length leaf))
645 (when (setq trans (cdr (assq (aref leaf i)
646 nnheader-file-name-translation-alist)))
649 (concat path leaf))))
651 (defun nnheader-report (backend &rest args)
652 "Report an error from the BACKEND.
653 The first string in ARGS can be a format string."
654 (set (intern (format "%s-status-string" backend))
655 (if (< (length args) 2)
657 (apply 'format args)))
660 (defun nnheader-get-report (backend)
661 "Get the most recent report from BACKEND."
663 (nnheader-message 5 "%s" (symbol-value (intern (format "%s-status-string"
665 (error (nnheader-message 5 ""))))
667 (defun nnheader-insert (format &rest args)
668 "Clear the communication buffer and insert FORMAT and ARGS into the buffer.
669 If FORMAT isn't a format string, it and all ARGS will be inserted
672 (set-buffer nntp-server-buffer)
674 (if (string-match "%" format)
675 (insert (apply 'format format args))
676 (apply 'insert format args))
679 (defun nnheader-replace-chars-in-string (string from to)
680 "Replace characters in STRING from FROM to TO."
681 (let ((string (substring string 0)) ;Copy string.
682 (len (length string))
684 ;; Replace all occurrences of FROM with TO.
686 (when (= (aref string idx) from)
687 (aset string idx to))
691 (defun nnheader-replace-duplicate-chars-in-string (string from to)
692 "Replace characters in STRING from FROM to TO."
693 (let ((string (substring string 0)) ;Copy string.
694 (len (length string))
696 ;; Replace all occurrences of FROM with TO.
698 (setq i (aref string idx))
699 (when (and (eq prev from) (= i from))
700 (aset string (1- idx) to)
701 (aset string idx to))
706 (defun nnheader-file-to-group (file &optional top)
707 "Return a group name based on FILE and TOP."
708 (nnheader-replace-chars-in-string
712 (substring (expand-file-name file)
715 (file-name-as-directory top))))
719 (defun nnheader-message (level &rest args)
720 "Message if the Gnus backends are talkative."
721 (if (or (not (numberp gnus-verbose-backends))
722 (<= level gnus-verbose-backends))
723 (apply 'message args)
724 (apply 'format args)))
726 (defun nnheader-be-verbose (level)
727 "Return whether the backends should be verbose on LEVEL."
728 (or (not (numberp gnus-verbose-backends))
729 (<= level gnus-verbose-backends)))
731 (defvar nnheader-pathname-coding-system 'binary
732 "*Coding system for pathname.")
734 (defun nnheader-group-pathname (group dir &optional file)
735 "Make pathname for GROUP."
737 (let ((dir (file-name-as-directory (expand-file-name dir))))
738 ;; If this directory exists, we use it directly.
739 (file-name-as-directory
740 (if (file-directory-p (concat dir group))
741 (expand-file-name group dir)
742 ;; If not, we translate dots into slashes.
743 (expand-file-name (mm-encode-coding-string
744 (nnheader-replace-chars-in-string group ?. ?/)
745 nnheader-pathname-coding-system)
747 (cond ((null file) "")
748 ((numberp file) (int-to-string file))
751 (defun nnheader-functionp (form)
752 "Return non-nil if FORM is funcallable."
753 (or (and (symbolp form) (fboundp form))
754 (and (listp form) (eq (car form) 'lambda))))
756 (defun nnheader-concat (dir &rest files)
757 "Concat DIR as directory to FILE."
758 (apply 'concat (file-name-as-directory dir) files))
760 (defun nnheader-ms-strip-cr ()
761 "Strip ^M from the end of all lines."
763 (goto-char (point-min))
764 (while (re-search-forward "\r$" nil t)
765 (delete-backward-char 1))))
767 (defun nnheader-file-size (file)
768 "Return the file size of FILE or 0."
769 (or (nth 7 (file-attributes file)) 0))
771 (defun nnheader-find-etc-directory (package &optional file)
772 "Go through the path and find the \".../etc/PACKAGE\" directory.
773 If FILE, find the \".../etc/PACKAGE\" file instead."
774 (let ((path load-path)
776 ;; We try to find the dir by looking at the load path,
777 ;; stripping away the last component and adding "etc/".
783 (directory-file-name (car path)))
786 (or file (file-directory-p dir)))
789 (setq path (cdr path))))
792 (defvar ange-ftp-path-format)
793 (defvar efs-path-regexp)
794 (defun nnheader-re-read-dir (path)
795 "Re-read directory PATH if PATH is on a remote system."
796 (if (and (fboundp 'efs-re-read-dir) (boundp 'efs-path-regexp))
797 (when (string-match efs-path-regexp path)
798 (efs-re-read-dir path))
799 (when (and (fboundp 'ange-ftp-re-read-dir) (boundp 'ange-ftp-path-format))
800 (when (string-match (car ange-ftp-path-format) path)
801 (ange-ftp-re-read-dir path)))))
803 (defvar nnheader-file-coding-system 'raw-text
804 "Coding system used in file backends of Gnus.")
806 (defun nnheader-insert-file-contents (filename &optional visit beg end replace)
807 "Like `insert-file-contents', q.v., but only reads in the file.
808 A buffer may be modified in several ways after reading into the buffer due
809 to advanced Emacs features, such as file-name-handlers, format decoding,
810 find-file-hooks, etc.
811 This function ensures that none of these modifications will take place."
812 (let ((coding-system-for-read nnheader-file-coding-system))
813 (mm-insert-file-contents filename visit beg end replace)))
815 (defun nnheader-find-file-noselect (&rest args)
816 (let ((format-alist nil)
817 (auto-mode-alist (mm-auto-mode-alist))
818 (default-major-mode 'fundamental-mode)
819 (enable-local-variables nil)
820 (after-insert-file-functions nil)
821 (enable-local-eval nil)
822 (find-file-hooks nil)
823 (coding-system-for-read nnheader-file-coding-system))
824 (apply 'find-file-noselect args)))
826 (defun nnheader-directory-regular-files (dir)
827 "Return a list of all regular files in DIR."
828 (let ((files (directory-files dir t))
831 (when (file-regular-p (car files))
832 (push (car files) out))
836 (defun nnheader-directory-files (&rest args)
837 "Same as `directory-files', but prune \".\" and \"..\"."
838 (let ((files (apply 'directory-files args))
841 (unless (member (file-name-nondirectory (car files)) '("." ".."))
842 (push (car files) out))
846 (defmacro nnheader-skeleton-replace (from &optional to regexp)
847 `(let ((new (generate-new-buffer " *nnheader replace*"))
848 (cur (current-buffer))
851 (goto-char (point-min))
852 (while (,(if regexp 're-search-forward 'search-forward)
854 (insert-buffer-substring
855 cur start (prog1 (match-beginning 0) (set-buffer new)))
856 (goto-char (point-max))
857 ,(when to `(insert ,to))
859 (setq start (point)))
860 (insert-buffer-substring
861 cur start (prog1 (point-max) (set-buffer new)))
862 (copy-to-buffer cur (point-min) (point-max))
863 (kill-buffer (current-buffer))
866 (defun nnheader-replace-string (from to)
867 "Do a fast replacement of FROM to TO from point to point-max."
868 (nnheader-skeleton-replace from to))
870 (defun nnheader-replace-regexp (from to)
871 "Do a fast regexp replacement of FROM to TO from point to point-max."
872 (nnheader-skeleton-replace from to t))
874 (defun nnheader-strip-cr ()
875 "Strip all \r's from the current buffer."
876 (nnheader-skeleton-replace "\r"))
878 (fset 'nnheader-run-at-time 'run-at-time)
879 (fset 'nnheader-cancel-timer 'cancel-timer)
880 (fset 'nnheader-cancel-function-timers 'cancel-function-timers)
882 (when (string-match "XEmacs\\|Lucid" emacs-version)
883 (require 'nnheaderxm))
885 (run-hooks 'nnheader-load-hook)
889 ;;; nnheader.el ends here