5d1df4763a9a53401210af4d2616f10f72436a52
[gnus] / lisp / nnheaderxm.el
1 ;;; nnheaderxm.el --- making Gnus backends work under XEmacs
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (defun nnheader-xmas-run-at-time (time repeat function &rest args)
29   (start-itimer
30    "nnheader-run-at-time"
31    `(lambda ()
32       (,function ,@args))
33    time repeat))
34
35 (defun nnheader-xmas-cancel-timer (timer)
36   (delete-itimer timer))
37
38 ;; Written by Erik Naggum <erik@naggum.no>.
39 ;; Saved by Steve Baur <steve@miranova.com>.
40 (defun nnheader-xmas-insert-file-contents-literally (filename &optional visit beg end replace)
41   "Like `insert-file-contents', q.v., but only reads in the file.
42 A buffer may be modified in several ways after reading into the buffer due
43 to advanced Emacs features, such as file-name-handlers, format decoding,
44 find-file-hooks, etc.
45   This function ensures that none of these modifications will take place."
46   (let (                                ; (file-name-handler-alist nil)
47         (format-alist nil)
48         (after-insert-file-functions nil)
49         (find-buffer-file-type-function 
50          (if (fboundp 'find-buffer-file-type)
51              (symbol-function 'find-buffer-file-type)
52            nil)))
53     (unwind-protect
54         (progn
55           (fset 'find-buffer-file-type (lambda (filename) t))
56           (insert-file-contents filename visit beg end replace))
57       (if find-buffer-file-type-function
58           (fset 'find-buffer-file-type find-buffer-file-type-function)
59         (fmakunbound 'find-buffer-file-type)))))
60
61 (defun nnheader-xmas-find-file-noselect (filename &optional nowarn rawfile)
62   "Read file FILENAME into a buffer and return the buffer.
63 If a buffer exists visiting FILENAME, return that one, but
64 verify that the file has not changed since visited or saved.
65 The buffer is not selected, just returned to the caller."
66   (setq filename
67         (abbreviate-file-name
68          (expand-file-name filename)))
69   (if (file-directory-p filename)
70       (if find-file-run-dired
71           (dired-noselect filename)
72         (error "%s is a directory." filename))
73     (let* ((buf (get-file-buffer filename))
74            (truename (abbreviate-file-name (file-truename filename)))
75            (number (nthcdr 10 (file-attributes truename)))
76            ;; Find any buffer for a file which has same truename.
77            (other (and (not buf) 
78                        (if (fboundp 'find-buffer-visiting)
79                            (find-buffer-visiting filename)
80                          (get-file-buffer filename))))
81            error)
82       ;; Let user know if there is a buffer with the same truename.
83       (if other
84           (progn
85             (or nowarn
86                 (string-equal filename (buffer-file-name other))
87                 (message "%s and %s are the same file"
88                          filename (buffer-file-name other)))
89             ;; Optionally also find that buffer.
90             (if (or (and (boundp 'find-file-existing-other-name)
91                          find-file-existing-other-name)
92                     find-file-visit-truename)
93                 (setq buf other))))
94       (if buf
95           (or nowarn
96               (verify-visited-file-modtime buf)
97               (cond ((not (file-exists-p filename))
98                      (error "File %s no longer exists!" filename))
99                     ((yes-or-no-p
100                       (if (string= (file-name-nondirectory filename)
101                                    (buffer-name buf))
102                           (format
103                            (if (buffer-modified-p buf)
104                                "File %s changed on disk.  Discard your edits? "
105                              "File %s changed on disk.  Reread from disk? ")
106                            (file-name-nondirectory filename))
107                         (format
108                          (if (buffer-modified-p buf)
109                              "File %s changed on disk.  Discard your edits in %s? "
110                            "File %s changed on disk.  Reread from disk into %s? ")
111                          (file-name-nondirectory filename)
112                          (buffer-name buf))))
113                      (save-excursion
114                        (set-buffer buf)
115                        (revert-buffer t t)))))
116         (save-excursion
117 ;;; The truename stuff makes this obsolete.
118 ;;;       (let* ((link-name (car (file-attributes filename)))
119 ;;;              (linked-buf (and (stringp link-name)
120 ;;;                               (get-file-buffer link-name))))
121 ;;;         (if (bufferp linked-buf)
122 ;;;             (message "Symbolic link to file in buffer %s"
123 ;;;                      (buffer-name linked-buf))))
124           (setq buf (create-file-buffer filename))
125           ;;      (set-buffer-major-mode buf)
126           (set-buffer buf)
127           (erase-buffer)
128           (if rawfile
129               (condition-case ()
130                   (nnheader-insert-file-contents-literally filename t)
131                 (file-error
132                  ;; Unconditionally set error
133                  (setq error t)))
134             (condition-case ()
135                 (insert-file-contents filename t)
136               (file-error
137                ;; Run find-file-not-found-hooks until one returns non-nil.
138                (or t                    ; (run-hook-with-args-until-success 'find-file-not-found-hooks)
139                    ;; If they fail too, set error.
140                    (setq error t)))))
141           ;; Find the file's truename, and maybe use that as visited name.
142           (setq buffer-file-truename truename)
143           (setq buffer-file-number number)
144           ;; On VMS, we may want to remember which directory in a search list
145           ;; the file was found in.
146           (and (eq system-type 'vax-vms)
147                (let (logical)
148                  (if (string-match ":" (file-name-directory filename))
149                      (setq logical (substring (file-name-directory filename)
150                                               0 (match-beginning 0))))
151                  (not (member logical find-file-not-true-dirname-list)))
152                (setq buffer-file-name buffer-file-truename))
153           (if find-file-visit-truename
154               (setq buffer-file-name
155                     (setq filename
156                           (expand-file-name buffer-file-truename))))
157           ;; Set buffer's default directory to that of the file.
158           (setq default-directory (file-name-directory filename))
159           ;; Turn off backup files for certain file names.  Since
160           ;; this is a permanent local, the major mode won't eliminate it.
161           (and (not (funcall backup-enable-predicate buffer-file-name))
162                (progn
163                  (make-local-variable 'backup-inhibited)
164                  (setq backup-inhibited t)))
165           (if rawfile
166               nil
167             (after-find-file error (not nowarn)))))
168       buf)))
169
170 (fset 'nnheader-run-at-time 'nnheader-xmas-run-at-time)
171 (fset 'nnheader-cancel-timer 'nnheader-xmas-cancel-timer)
172 (fset 'nnheader-find-file-noselect 'nnheader-xmas-find-file-noselect)
173 (fset 'nnheader-insert-file-contents-literally
174       (if (fboundp 'insert-file-contents-literally)
175           'insert-file-contents-literally
176         'nnheader-xmas-insert-file-contents-literally))
177
178 (provide 'nnheaderxm)
179
180 ;;; nnheaderxm.el ends here.