*** empty log message ***
[gnus] / lisp / gnus-xmas.el
1 ;;; gnus-xmas.el --- Gnus functions for XEmacs
2 ;; Copyright (C) 1995,96 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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (require 'text-props)
28 (defvar menu-bar-mode t)
29
30 (defvar gnus-xmas-glyph-directory nil
31   "*Directory where Gnus logos and icons are located.
32 If this variable is nil, Gnus will try to locate the directory
33 automatically.")
34
35 ;;; Internal variables.
36
37 (defvar gnus-xmas-logo (make-glyph (make-specifier 'image)))
38
39 ;; Don't warn about these undefined variables.
40
41 (defvar gnus-group-mode-hook)
42 (defvar gnus-summary-mode-hook)
43 (defvar gnus-article-mode-hook)
44
45 ;;defined in gnus.el
46 (defvar gnus-active-hashtb)
47 (defvar gnus-article-buffer)
48 (defvar gnus-auto-center-summary)
49 (defvar gnus-buffer-list)
50 (defvar gnus-current-headers)
51 (defvar gnus-level-killed)
52 (defvar gnus-level-zombie)
53 (defvar gnus-newsgroup-bookmarks)
54 (defvar gnus-newsgroup-dependencies)
55 (defvar gnus-newsgroup-selected-overlay)
56 (defvar gnus-newsrc-hashtb)
57 (defvar gnus-read-mark)
58 (defvar gnus-refer-article-method)
59 (defvar gnus-reffed-article-number)
60 (defvar gnus-unread-mark)
61 (defvar gnus-version)
62 (defvar gnus-view-pseudos)
63 (defvar gnus-view-pseudos-separately)
64 (defvar gnus-visual)
65 (defvar gnus-zombie-list)
66 ;;defined in gnus-msg.el
67 (defvar gnus-article-copy)
68 (defvar gnus-check-before-posting)
69 ;;defined in gnus-vis.el
70 (defvar gnus-article-button-face)
71 (defvar gnus-article-mouse-face)
72 (defvar gnus-summary-selected-face)
73 (defvar gnus-group-reading-menu)
74 (defvar gnus-group-group-menu)
75 (defvar gnus-group-misc-menu)
76 (defvar gnus-summary-article-menu)
77 (defvar gnus-summary-thread-menu)
78 (defvar gnus-summary-misc-menu)
79 (defvar gnus-summary-post-menu)
80 (defvar gnus-summary-kill-menu)
81 (defvar gnus-article-article-menu)
82 (defvar gnus-article-treatment-menu)
83 (defvar gnus-mouse-2)
84 (defvar standard-display-table)
85
86 (defun gnus-xmas-set-text-properties (start end props &optional buffer)
87   "You should NEVER use this function.  It is ideologically blasphemous.
88 It is provided only to ease porting of broken FSF Emacs programs."
89   (if (and (stringp buffer) (not (setq buffer (get-buffer buffer))))
90       nil
91     (map-extents (lambda (extent ignored)
92                    (remove-text-properties 
93                     start end
94                     (list (extent-property extent 'text-prop) nil)
95                     buffer))
96                  buffer start end nil nil 'text-prop)
97     (add-text-properties start end props buffer)))
98
99 (defun gnus-xmas-highlight-selected-summary ()
100   ;; Highlight selected article in summary buffer
101   (if gnus-summary-selected-face
102       (progn
103         (if gnus-newsgroup-selected-overlay
104             (delete-extent gnus-newsgroup-selected-overlay))
105         (setq gnus-newsgroup-selected-overlay 
106               (make-extent (gnus-point-at-bol) (gnus-point-at-eol)))
107         (set-extent-face gnus-newsgroup-selected-overlay
108                          gnus-summary-selected-face))))
109
110 (defun gnus-xmas-summary-recenter ()
111   (let* ((top (cond ((< (window-height) 4) 0)
112                     ((< (window-height) 7) 1)
113                     (t 2)))
114          (height (- (window-height) 2))
115          (bottom (save-excursion (goto-char (point-max))
116                                  (forward-line (- height))
117                                  (point)))
118          (window (get-buffer-window (current-buffer))))
119     (and 
120      ;; The user has to want it,
121      gnus-auto-center-summary 
122      ;; the article buffer must be displayed,
123      (get-buffer-window gnus-article-buffer)
124      ;; Set the window start to either `bottom', which is the biggest
125      ;; possible valid number, or the second line from the top,
126      ;; whichever is the least.
127      (set-window-start
128       window (min bottom (save-excursion
129                            (forward-line (- top)) (point)))))))
130
131 (defun gnus-xmas-group-remove-excess-properties ()
132   (let ((end (point))
133         (beg (progn (forward-line -1) (point))))
134     (remove-text-properties (1+ beg) end '(gnus-group nil))
135     (remove-text-properties 
136      beg end 
137      '(gnus-topic nil gnus-topic-level nil gnus-topic-visible nil))
138     (goto-char end)))
139                   
140 (defun gnus-xmas-copy-article-buffer (&optional article-buffer)
141   (setq gnus-article-copy (get-buffer-create " *gnus article copy*"))
142   (buffer-disable-undo gnus-article-copy)
143   (or (memq gnus-article-copy gnus-buffer-list)
144       (setq gnus-buffer-list (cons gnus-article-copy gnus-buffer-list)))
145   (let ((article-buffer (or article-buffer gnus-article-buffer))
146         buf)
147     (if (and (get-buffer article-buffer)
148              (buffer-name (get-buffer article-buffer)))
149         (save-excursion
150           (set-buffer article-buffer)
151           (widen)
152           (setq buf (buffer-substring (point-min) (point-max)))
153           (set-buffer gnus-article-copy)
154           (erase-buffer)
155           (insert (format "%s" buf))))))
156
157 (defun gnus-xmas-article-push-button (event)
158   "Check text under the mouse pointer for a callback function.
159 If the text under the mouse pointer has a `gnus-callback' property,
160 call it with the value of the `gnus-data' text property."
161   (interactive "e")
162   (set-buffer (window-buffer (event-window event)))
163   (let* ((pos (event-closest-point event))
164          (data (get-text-property pos 'gnus-data))
165          (fun (get-text-property pos 'gnus-callback)))
166     (if fun (funcall fun data))))
167
168 (defun gnus-xmas-move-overlay (extent start end &optional buffer)
169   (set-extent-endpoints extent start end))
170
171 ;; Fixed by Christopher Davis <ckd@loiosh.kei.com>.
172 (defun gnus-xmas-article-add-button (from to fun &optional data)
173   "Create a button between FROM and TO with callback FUN and data DATA."
174   (and gnus-article-button-face
175        (gnus-overlay-put (gnus-make-overlay from to) 
176                          'face gnus-article-button-face))
177   (add-text-properties 
178    from to
179    (nconc
180     (and gnus-article-mouse-face
181          (list 'mouse-face gnus-article-mouse-face))
182     (list 'gnus-callback fun)
183     (and data (list 'gnus-data data))
184     (list 'highlight t))))
185
186 (defun gnus-xmas-window-top-edge (&optional window)
187   (nth 1 (window-pixel-edges window)))
188
189 ;; Select the lowest window on the frame.
190 (defun gnus-xmas-appt-select-lowest-window ()
191   (let* ((lowest-window (selected-window))
192          (bottom-edge (car (cdr (cdr (cdr (window-pixel-edges))))))
193          (last-window (previous-window))
194          (window-search t))
195     (while window-search
196       (let* ((this-window (next-window))
197              (next-bottom-edge (car (cdr (cdr (cdr 
198                                                (window-pixel-edges 
199                                                 this-window)))))))
200         (if (< bottom-edge next-bottom-edge)
201             (progn
202               (setq bottom-edge next-bottom-edge)
203               (setq lowest-window this-window)))
204
205         (select-window this-window)
206         (if (eq last-window this-window)
207             (progn
208               (select-window lowest-window)
209               (setq window-search nil)))))))
210
211 (defun gnus-xmas-group-menu-add ()
212   (easy-menu-add gnus-group-reading-menu)
213   (easy-menu-add gnus-group-group-menu)
214   (easy-menu-add gnus-group-misc-menu))
215
216 (defun gnus-xmas-summary-menu-add ()
217   (easy-menu-add gnus-summary-article-menu)
218   (easy-menu-add gnus-summary-thread-menu)
219   (easy-menu-add gnus-summary-misc-menu)
220   (easy-menu-add gnus-summary-post-menu)
221   (easy-menu-add gnus-summary-kill-menu)) 
222
223 (defun gnus-xmas-article-menu-add ()
224   (easy-menu-add gnus-article-article-menu)
225   (easy-menu-add gnus-article-treatment-menu))
226
227 (defun gnus-xmas-read-event-char ()
228   "Get the next event."
229   (let ((event (next-event)))
230     ;; We junk all non-key events.  Is this naughty?
231     (while (not (key-press-event-p event))
232       (setq event (next-event)))
233     (cons (and (key-press-event-p event) 
234                (numberp (event-key event))
235                (event-to-character event)) 
236           event)))
237
238 (defun gnus-xmas-seconds-since-epoch (date)
239   "Return a floating point number that says how many seconds have lapsed between Jan 1 12:00:00 1970 and DATE."
240   (let* ((tdate (mapcar (lambda (ti) (and ti (string-to-int ti)))
241                         (timezone-parse-date date)))
242          (ttime (mapcar (lambda (ti) (and ti (string-to-int ti)))
243                         (timezone-parse-time
244                          (aref (timezone-parse-date date) 3))))
245          (edate (mapcar (lambda (ti) (and ti (string-to-int ti)))
246                         (timezone-parse-date "Jan 1 12:00:00 1970")))
247          (tday (- (timezone-absolute-from-gregorian 
248                    (nth 1 tdate) (nth 2 tdate) (nth 0 tdate))
249                   (timezone-absolute-from-gregorian 
250                    (nth 1 edate) (nth 2 edate) (nth 0 edate)))))
251     (+ (nth 2 ttime)
252        (* (nth 1 ttime) 60)
253        (* (float (nth 0 ttime)) 60 60)
254        (* (float tday) 60 60 24))))
255
256 (defun gnus-xmas-define ()
257   (setq gnus-mouse-2 [button2])
258
259   (or (memq 'underline (list-faces))
260       (and (fboundp 'make-face)
261            (funcall (intern "make-face") 'underline)))
262   ;; Must avoid calling set-face-underline-p directly, because it
263   ;; is a defsubst in emacs19, and will make the .elc files non
264   ;; portable!
265   (or (face-differs-from-default-p 'underline)
266       (funcall (intern "set-face-underline-p") 'underline t))
267
268   (fset 'gnus-make-overlay 'make-extent)
269   (fset 'gnus-overlay-put 'set-extent-property)
270   (fset 'gnus-move-overlay 'gnus-xmas-move-overlay)
271       
272   (fset 'set-text-properties 'gnus-xmas-set-text-properties)
273
274   (fset 'nnheader-find-file-noselect 'gnus-xmas-find-file-noselect)
275
276   (or (boundp 'standard-display-table) (setq standard-display-table nil))
277
278   (defvar gnus-mouse-face-prop 'highlight)
279
280   (unless (fboundp 'encode-time)
281     (defun encode-time (sec minute hour day month year &optional zone)
282       (let ((seconds
283              (gnus-xmas-seconds-since-epoch
284               (timezone-make-arpa-date 
285                year month day (timezone-make-time-string hour minute sec)
286                zone))))
287         (list (floor (/ seconds (expt 2 16)))
288               (round (mod seconds (expt 2 16)))))))
289       
290   (defun gnus-byte-code (func)
291     "Return a form that can be `eval'ed based on FUNC."
292     (let ((fval (symbol-function func)))
293       (if (byte-code-function-p fval)
294           (list 'funcall fval)
295         (cons 'progn (cdr (cdr fval))))))
296       
297   ;; Fix by "jeff (j.d.) sparkes" <jsparkes@bnr.ca>.
298   (defvar gnus-display-type (device-class)
299     "A symbol indicating the display Emacs is running under.
300 The symbol should be one of `color', `grayscale' or `mono'. If Emacs
301 guesses this display attribute wrongly, either set this variable in
302 your `~/.emacs' or set the resource `Emacs.displayType' in your
303 `~/.Xdefaults'. See also `gnus-background-mode'.
304
305 This is a meta-variable that will affect what default values other
306 variables get.  You would normally not change this variable, but
307 pounce directly on the real variables themselves.")
308
309
310   (or (fboundp 'x-color-values)
311       (fset 'x-color-values 
312             (lambda (color)
313               (color-instance-rgb-components
314                (make-color-instance color)))))
315     
316   (defvar gnus-background-mode 
317     (let ((bg-resource 
318            (condition-case ()
319                (x-get-resource ".backgroundMode" "BackgroundMode" 'string)
320              (error nil)))
321           (params (frame-parameters)))
322       (cond (bg-resource (intern (downcase bg-resource)))
323             ((and (assq 'background-color params)
324                   (< (apply '+ (x-color-values
325                                 (cdr (assq 'background-color params))))
326                      (/ (apply '+ (x-color-values "white")) 3)))
327              'dark)
328             (t 'light)))
329     "A symbol indicating the Emacs background brightness.
330 The symbol should be one of `light' or `dark'.
331 If Emacs guesses this frame attribute wrongly, either set this variable in
332 your `~/.emacs' or set the resource `Emacs.backgroundMode' in your
333 `~/.Xdefaults'.
334 See also `gnus-display-type'.
335
336 This is a meta-variable that will affect what default values other
337 variables get.  You would normally not change this variable, but
338 pounce directly on the real variables themselves.")
339   )
340
341
342
343 (defun gnus-xmas-redefine ()
344   "Redefine lots of Gnus functions for XEmacs."
345   (fset 'gnus-summary-make-display-table (lambda () nil))
346   (fset 'gnus-visual-turn-off-edit-menu 'identity)
347   (fset 'gnus-highlight-selected-summary
348         'gnus-xmas-highlight-selected-summary)
349   (fset 'gnus-summary-recenter 'gnus-xmas-summary-recenter)
350   (fset 'gnus-group-remove-excess-properties
351         'gnus-xmas-group-remove-excess-properties)
352   (fset 'gnus-copy-article-buffer 'gnus-xmas-copy-article-buffer)
353   (fset 'gnus-article-push-button 'gnus-xmas-article-push-button)
354   (fset 'gnus-article-add-button 'gnus-xmas-article-add-button)
355   (fset 'gnus-window-top-edge 'gnus-xmas-window-top-edge)
356   (fset 'set-text-properties 'gnus-xmas-set-text-properties)
357   (fset 'gnus-read-event-char 'gnus-xmas-read-event-char)
358   (fset 'gnus-group-startup-message 'gnus-xmas-group-startup-message)
359
360   (or (fboundp 'appt-select-lowest-window)
361       (fset 'appt-select-lowest-window 
362             'gnus-xmas-appt-select-lowest-window))
363
364   (add-hook 'gnus-group-mode-hook 'gnus-xmas-group-menu-add)
365   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-summary-menu-add)
366   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add)
367
368   (add-hook 'gnus-group-mode-hook 'gnus-xmas-setup-group-toolbar)
369   (add-hook 'gnus-summary-mode-hook 'gnus-xmas-setup-summary-toolbar))
370
371
372 ;;; XEmacs logo and toolbar.
373
374 (defun gnus-xmas-find-glyph-directory ()
375   (or gnus-xmas-glyph-directory ; We have a dir already...
376       (let ((path load-path)
377             dir)
378         ;; We try to find the dir by looking at the load path,
379         ;; stripping away the last component and adding "etc/".
380         (while path
381           (setq dir (concat
382                      (file-name-directory (directory-file-name (car path)))
383                      "etc/"))
384           (if (and (file-exists-p dir)
385                    (file-directory-p dir)
386                    (file-exists-p (concat dir "gnus-group-exit-icon-up.xpm")))
387               (setq gnus-xmas-glyph-directory dir
388                     path nil)
389             (setq path (cdr path))))
390         gnus-xmas-glyph-directory)))
391
392 (defun gnus-xmas-group-startup-message (&optional x y)
393   "Insert startup message in current buffer."
394   ;; Insert the message.
395   (gnus-xmas-find-glyph-directory)
396   (erase-buffer)
397   (let ((file (and gnus-xmas-glyph-directory
398                    (concat 
399                     (file-name-as-directory gnus-xmas-glyph-directory)
400                     "gnus.xpm"))))
401     (if (and (featurep 'xpm)
402              (not (equal (device-type) 'tty))
403              file (file-exists-p file))
404         (progn
405           (set-glyph-property gnus-xmas-logo 'image file)
406           (set-glyph-image gnus-xmas-logo file 'global 'x)
407
408           (insert " ")
409           (set-extent-begin-glyph (make-extent (point) (point)) gnus-xmas-logo)
410           (goto-char (point-min))
411           (while (not (eobp))
412             (insert (make-string (/ (max (- (window-width) (or x 35)) 0) 2)
413                                  ? ))
414             (forward-line 1))
415           (goto-char (point-min))
416           (let* ((pheight (+ 20 (count-lines (point-min) (point-max))))
417                  (wheight (window-height))
418                  (rest (- wheight pheight)))
419             (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n))))
420
421       (insert
422        (format "              %s
423           _    ___ _             _      
424           _ ___ __ ___  __    _ ___     
425           __   _     ___    __  ___     
426               _           ___     _     
427              _  _ __             _      
428              ___   __            _      
429                    __           _       
430                     _      _   _        
431                    _      _    _        
432                       _  _    _         
433                   __  ___               
434                  _   _ _     _          
435                 _   _                   
436               _    _                    
437              _    _                     
438             _                         
439           __                             
440
441
442                ""))
443       ;; And then hack it.
444       (gnus-indent-rigidly (point-min) (point-max) 
445                            (/ (max (- (window-width) (or x 46)) 0) 2))
446       (goto-char (point-min))
447       (forward-line 1)
448       (let* ((pheight (count-lines (point-min) (point-max)))
449              (wheight (window-height))
450              (rest (- wheight pheight)))
451         (insert (make-string (max 0 (* 2 (/ rest 3))) ?\n))))
452     ;; Fontify some.
453     (goto-char (point-min))
454     (and (search-forward "Praxis" nil t)
455          (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
456     (goto-char (point-min))
457     (let* ((mode-string (gnus-group-set-mode-line)))
458       (setq mode-line-buffer-identification 
459             (list (concat gnus-version (substring (car mode-string) 4))))
460       (set-buffer-modified-p t))))
461
462
463 ;;; The toolbar.
464
465 (defvar gnus-use-toolbar 'default-toolbar
466   "*If nil, do not use a toolbar.
467 If it is non-nil, it must be a toolbar.  The five legal values are
468 `default-toolbar', `top-toolbar', `bottom-toolbar',
469 `right-toolbar', and `left-toolbar'.")
470
471 (defvar gnus-group-toolbar 
472   '([gnus-group-exit-icon gnus-group-exit t "Exit Gnus"]
473     [gnus-group-kill-group-icon gnus-group-kill-group t "Kill group"]
474     [gnus-group-get-new-news-icon gnus-group-get-new-news t "Get new news"]
475     [gnus-group-get-new-news-this-group-icon 
476      gnus-group-get-new-news-this-group t "Get new news in this group"]
477     [gnus-group-catchup-current-icon 
478      gnus-group-catchup-current t "Catchup group"]
479     [gnus-group-describe-group-icon 
480      gnus-group-describe-group t "Describe group"])
481   "The group buffer toolbar.")
482
483 (defvar gnus-summary-toolbar 
484   '([gnus-summary-post-news-icon 
485      gnus-summary-post-news t "Post an article"]
486     [gnus-summary-save-article-file-icon
487      gnus-summary-save-article-file t "Save article in file"]
488     [gnus-summary-save-article-icon
489      gnus-summary-save-article t "Save article"]
490     [gnus-summary-reply-icon 
491      gnus-summary-reply t "Mail a reply"]
492     [gnus-summary-reply-with-original-icon
493      gnus-summary-reply-with-original t "Mail a reply and yank the original"]
494     [gnus-summary-followup-icon 
495      gnus-summary-followup t "Post a followup"]
496     [gnus-summary-followup-with-original-icon
497      gnus-summary-followup-with-original t 
498      "Post a followup and yank the original"]
499     [gnus-uu-decode-uu-icon
500      gnus-uu-decode-uu t "Decode uuencoded articles"]
501     [gnus-uu-post-news-icon 
502      gnus-uu-post-news t "Post an uuencoded article"]
503     [gnus-summary-caesar-message-icon
504      gnus-summary-caesar-message t "Rot 13"]
505     [gnus-summary-cancel-article-icon
506      gnus-summary-cancel-article t "Cancel article"])
507   "The summary buffer toolbar.")
508
509 (defun gnus-xmas-setup-toolbar (bar &optional force)
510   (let ((dir (gnus-xmas-find-glyph-directory))
511         icon up down disabled name)
512     (if (not dir)
513         ()
514       (if (and (not force)
515                (boundp (aref (car bar) 0)))
516           dir
517         (while bar
518           (setq icon (aref (car bar) 0)
519                 name (symbol-name icon)
520                 bar (cdr bar))
521           (setq up (concat dir name "-up.xpm"))
522           (setq down (concat dir name "-down.xpm"))
523           (setq disabled (concat dir name "-disabled.xpm"))
524           (if (not (file-exists-p up))
525               (set icon nil)
526             (set icon (toolbar-make-button-list
527                        up (and (file-exists-p down) down)
528                        (and (file-exists-p disabled) disabled)))))
529         dir))))
530
531 (defun gnus-xmas-setup-group-toolbar ()
532   (and gnus-use-toolbar
533        (gnus-xmas-setup-toolbar gnus-group-toolbar)
534        (set-specifier (symbol-value gnus-use-toolbar)
535                       (cons (current-buffer) gnus-group-toolbar))))
536
537 (defun gnus-xmas-setup-summary-toolbar ()
538   (and gnus-use-toolbar
539        (gnus-xmas-setup-toolbar gnus-summary-toolbar)
540        (set-specifier (symbol-value gnus-use-toolbar)
541                       (cons (current-buffer) gnus-summary-toolbar))))
542
543 ;; Written by Erik Naggum <erik@naggum.no>.
544 ;; Saved by Steve Baur <steve@miranova.com>.
545 (defun insert-file-contents-literally (filename &optional visit beg end replace)
546   "Like `insert-file-contents', q.v., but only reads in the file.
547 A buffer may be modified in several ways after reading into the buffer due
548 to advanced Emacs features, such as file-name-handlers, format decoding,
549 find-file-hooks, etc.
550   This function ensures that none of these modifications will take place."
551   (let (                                ; (file-name-handler-alist nil)
552         (format-alist nil)
553         (after-insert-file-functions nil)
554         (find-buffer-file-type-function 
555          (if (fboundp 'find-buffer-file-type)
556              (symbol-function 'find-buffer-file-type)
557            nil)))
558     (unwind-protect
559         (progn
560           (fset 'find-buffer-file-type (lambda (filename) t))
561           (insert-file-contents filename visit beg end replace))
562       (if find-buffer-file-type-function
563           (fset 'find-buffer-file-type find-buffer-file-type-function)
564         (fmakunbound 'find-buffer-file-type)))))
565
566 (defun gnus-xmas-find-file-noselect (filename &optional nowarn rawfile)
567   "Read file FILENAME into a buffer and return the buffer.
568 If a buffer exists visiting FILENAME, return that one, but
569 verify that the file has not changed since visited or saved.
570 The buffer is not selected, just returned to the caller."
571   (setq filename
572         (abbreviate-file-name
573          (expand-file-name filename)))
574   (if (file-directory-p filename)
575       (if find-file-run-dired
576           (dired-noselect filename)
577         (error "%s is a directory." filename))
578     (let* ((buf (get-file-buffer filename))
579            (truename (abbreviate-file-name (file-truename filename)))
580            (number (nthcdr 10 (file-attributes truename)))
581            ;; Find any buffer for a file which has same truename.
582            (other (and (not buf) 
583                        (if (fboundp 'find-buffer-visiting)
584                            (find-buffer-visiting filename)
585                          (get-file-buffer filename))))
586            error)
587       ;; Let user know if there is a buffer with the same truename.
588       (if other
589           (progn
590             (or nowarn
591                 (string-equal filename (buffer-file-name other))
592                 (message "%s and %s are the same file"
593                          filename (buffer-file-name other)))
594             ;; Optionally also find that buffer.
595             (if (or (and (boundp 'find-file-existing-other-name)
596                          find-file-existing-other-name)
597                     find-file-visit-truename)
598                 (setq buf other))))
599       (if buf
600           (or nowarn
601               (verify-visited-file-modtime buf)
602               (cond ((not (file-exists-p filename))
603                      (error "File %s no longer exists!" filename))
604                     ((yes-or-no-p
605                       (if (string= (file-name-nondirectory filename)
606                                    (buffer-name buf))
607                           (format
608                            (if (buffer-modified-p buf)
609                                "File %s changed on disk.  Discard your edits? "
610                              "File %s changed on disk.  Reread from disk? ")
611                            (file-name-nondirectory filename))
612                         (format
613                          (if (buffer-modified-p buf)
614                              "File %s changed on disk.  Discard your edits in %s? "
615                            "File %s changed on disk.  Reread from disk into %s? ")
616                          (file-name-nondirectory filename)
617                          (buffer-name buf))))
618                      (save-excursion
619                        (set-buffer buf)
620                        (revert-buffer t t)))))
621         (save-excursion
622 ;;; The truename stuff makes this obsolete.
623 ;;;       (let* ((link-name (car (file-attributes filename)))
624 ;;;              (linked-buf (and (stringp link-name)
625 ;;;                               (get-file-buffer link-name))))
626 ;;;         (if (bufferp linked-buf)
627 ;;;             (message "Symbolic link to file in buffer %s"
628 ;;;                      (buffer-name linked-buf))))
629           (setq buf (create-file-buffer filename))
630           ;;      (set-buffer-major-mode buf)
631           (set-buffer buf)
632           (erase-buffer)
633           (if rawfile
634               (condition-case ()
635                   (insert-file-contents-literally filename t)
636                 (file-error
637                  ;; Unconditionally set error
638                  (setq error t)))
639             (condition-case ()
640                 (insert-file-contents filename t)
641               (file-error
642                ;; Run find-file-not-found-hooks until one returns non-nil.
643                (or t                    ; (run-hook-with-args-until-success 'find-file-not-found-hooks)
644                    ;; If they fail too, set error.
645                    (setq error t)))))
646           ;; Find the file's truename, and maybe use that as visited name.
647           (setq buffer-file-truename truename)
648           (setq buffer-file-number number)
649           ;; On VMS, we may want to remember which directory in a search list
650           ;; the file was found in.
651           (and (eq system-type 'vax-vms)
652                (let (logical)
653                  (if (string-match ":" (file-name-directory filename))
654                      (setq logical (substring (file-name-directory filename)
655                                               0 (match-beginning 0))))
656                  (not (member logical find-file-not-true-dirname-list)))
657                (setq buffer-file-name buffer-file-truename))
658           (if find-file-visit-truename
659               (setq buffer-file-name
660                     (setq filename
661                           (expand-file-name buffer-file-truename))))
662           ;; Set buffer's default directory to that of the file.
663           (setq default-directory (file-name-directory filename))
664           ;; Turn off backup files for certain file names.  Since
665           ;; this is a permanent local, the major mode won't eliminate it.
666           (and (not (funcall backup-enable-predicate buffer-file-name))
667                (progn
668                  (make-local-variable 'backup-inhibited)
669                  (setq backup-inhibited t)))
670           (if rawfile
671               nil
672             (after-find-file error (not nowarn)))))
673       buf)))
674
675
676 ;;; gnus-xmas.el ends here