home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
bbs
/
may94
/
util
/
edit
/
jade.lha
/
Jade
/
lisp
/
buffers.jl
< prev
next >
Wrap
Lisp/Scheme
|
1994-04-17
|
16KB
|
454 lines
;;;; buffers.jl -- High-level buffer/file handling
;;; Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
;;; This file is part of Jade.
;;; Jade is free software; you can redistribute it and/or modify it
;;; under the terms of the GNU General Public License as published by
;;; the Free Software Foundation; either version 2, or (at your option)
;;; any later version.
;;; Jade is distributed in the hope that it will be useful, but
;;; WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;;; GNU General Public License for more details.
;;; You should have received a copy of the GNU General Public License
;;; along with Jade; see the file COPYING. If not, write to
;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
(defvar auto-save-p t
"When t files are auto-save'd regularly.")
(defvar default-auto-save-interval 120
"The number of seconds between each auto-save.")
(defvar make-backup-files t
"When non-nil backups of files are made when they are saved.")
(defvar backup-by-copying nil
"When non-nil all file backups are made by copying the file, not by
renaming it.")
(defvar amiga-use-file-req-p t
"*AMIGA ONLY*
When non-nil the normal ASL file requester is used when file names are
prompted for.")
(setq default-buffer (current-buffer))
(defvar buffer-list (cons default-buffer nil)
"List of buffers in most-recently-used order. Each window has it's own.")
(set-window-variable 'buffer-list)
(defvar standard-output default-buffer
"Stream that `prin?' writes its output to by default")
(defvar standard-input default-buffer
"Stream that `read' takes it's input from by default")
(defvar buffer-file-modtime 0
"Holds the modification time of the file this buffer was loaded from")
(set-buffer-variable 'buffer-file-modtime)
(defun goto-buffer (buf)
"(goto-buffer BUFFER)
Switch the current buffer to BUFFER which can either be a buffer-object
or a string naming an existing buffer. The selected buffer is moved to
the head of the buffer list."
(cond
((stringp buf)
(setq buf (get-buffer buf)))
((bufferp buf))
(t
(title "error: bad arg to goto-buffer")
(return)))
(setq buffer-list (cons buf (delq buf buffer-list)))
(set-current-buffer buf))
(defun open-file (name &aux buf)
"(open-file FILE-NAME)
If no buffer containing file FILE-NAME exits try to create one.
After creating a new buffer (named after the file's (not path) name)
it first call the hook `read-file-hook' with arguments `(file-name buffer)'
If this hook returns nil (ie, no members of the hook decided to read the
file into memory) the file is read into the buffer verbatim.
Once the file is in memory, through the hook or otherwise, this function
then tries to initialise the correct editing mode for the file.
`open-file' always returns the buffer holding the file, or nil if it
doesn't exist."
(unless (setq buf (get-file-buffer name))
(when (setq buf (make-buffer (base-name name)))
(add-buffer buf buffer-list)
(with-buffer buf
(unless (eval-hook 'read-file-hook name buf)
(set-file-name buf name)
(if (file-exists-p name)
(read-buffer name)
(title "New file")))
(set-buffer-modified buf nil)
(when auto-save-p
(setq auto-save-interval default-auto-save-interval))
(setq
buffer-file-modtime (file-modtime name)
last-save-time (current-time))
(when (auto-save-file-newer-p name)
(title "warning: auto-saved file is newer")
(beep))
(when (and (file-exists-p name) (not (file-writeable-p name)))
(set-buffer-read-only buf t))
(setq keymap-path (cons global-keymap nil))
(eval-hook 'open-file-hook buf)
(init-mode buf))))
buf)
(defun find-file (&optional name)
"(find-file [FILE-NAME])
Sets the current buffer to that containing the file FILE-NAME, if FILE-NAME
is unspecified it will be prompted for. If the file is not already in memory
`open-file' will be used to load it."
(unless name
(setq name (funcall (if (and (amiga-p) amiga-use-file-req-p) 'file-req 'prompt-for-file)
"Find file: "
(path-name (file-name)))))
(when name
(goto-buffer (open-file name))))
(defun find-file-read-only (&optional name)
"(find-file-read-only [FILE-NAME])
Similar to `find-file' except that the buffer is edited in read-only mode."
(unless name
(setq name (funcall (if (and (amiga-p) amiga-use-file-req-p) 'file-req 'prompt-for-file)
"Find file read-only:"
(path-name (file-name)))))
(when name
(let
((buf (open-file name)))
(when buf
(set-buffer-read-only buf t)
(goto-buffer buf)))))
(defun open-alternate-file (name &optional buf)
"(open-alternate-file FILE-NAME [BUFFER] )
Kills BUFFER and returns a buffer containing FILE-NAME (through the
`kill-buffer' and `open-file' functions)."
(when (stringp name)
(when (null buf)
(setq buf (current-buffer)))
(kill-buffer buf)
(open-file name)))
(defun find-alternate-file (&optional name)
"(find-alternate-file [FILE-NAME])
If FILE-NAME is unspecified one will be prompted for. The current buffer
is killed and one editing FILE-NAME is found."
(unless name
(setq name (funcall (if (and (amiga-p) amiga-use-file-req-p) 'file-req 'prompt-for-file)
"Find alternate file: "
(path-name (file-name)))))
(when name
(goto-buffer (open-alternate-file name))))
(defun write-file (buf &optional name)
"(write-file BUFFER [FILE-NAME] )
Writes the contents of BUFFER to the file FILE-NAME, or to the one
that it is associated with."
(unless (stringp name)
(setq name (file-name buf)))
(unless (eval-hook 'write-file-hook buf name)
(when (and make-backup-files (file-regular-p name))
(let
((backup-name (concat name ?~)))
(if backup-by-copying
(copy-file name backup-name)
(when (and (file-owner-p name)
(= (file-nlinks name) 1))
(when (file-exists-p backup-name)
(delete-file backup-name))
(rename-file name backup-name)))))
(write-buffer name buf)))
(defun save-file (&optional buf &aux name)
"(save-file [BUFFER])
Saves the buffer BUFFER, or the current buffer, to the file that it is
associated with, then sets the number of modifications made to this file
to zero.
Note: if no changes have been made to this buffer, it won't be saved."
(unless (bufferp buf)
(setq buf (current-buffer)))
(with-buffer buf
(if (not (buffer-modified-p))
(title "no changes need to be saved!")
(setq name (file-name))
(when (and
(> (file-modtime name) buffer-file-modtime)
(not (ask-yes-or-no "File on disk has changed since it was loaded, save anyway")))
(return nil))
(when (write-file buf)
(set-buffer-modified buf nil)
(setq
last-save-time (current-time)
last-save-changes (buffer-changes)
last-user-save-changes (buffer-changes))
(setq buffer-file-modtime (file-modtime name))
(delete-auto-save-file)
(format t "Saved file %S." name)))))
(defun save-file-as (&optional name buf &aux old-name)
"(save-file-as [FILE-NAME] [BUFFER])
Saves the buffer BUFFER, or the current one, to the file FILE-NAME,
resetting the name of the buffer and the file that it is associated with
to reflect FILE-NAME. Also sets the modification count to zero."
(unless name
(setq name
(if (and (amiga-p) amiga-use-file-req-p)
(file-req "Write file:" (path-name (file-name)) t)
(prompt-for-file "Write file: " (path-name (file-name))))))
(unless (bufferp buf)
(setq buf (current-buffer)))
(with-buffer buf
(setq old-name (file-name))
(set-file-name buf name)
(set-buffer-name buf (base-name name))
(when (write-file buf)
(set-buffer-modified buf nil)
(setq
last-save-time (current-time)
last-save-changes (buffer-changes)
last-user-save-changes (buffer-changes))
(setq buffer-file-modtime (file-modtime name))
(delete-file (make-auto-save-name old-name))
(format t "Saved file %S." name))))
(defun insert-file (&optional name buf)
"(insert-file [FILE-NAME] [BUFFER])
Inserts the file FILE-NAME (may be prompted for) into the buffer BUFFER (or
the current one) before the cursor position."
(u