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