[gnu.emacs] save-context-predicate in saveconf.el

ghh@cognito.princeton.edu (Gilbert Harman) (07/25/89)

I have been using the recently posted version of saveconf.el
in gnu emacs v 18.54.  I would like to use this to have
emacs start up by visiting all the files I was working on
when I last stopped.  Except that I would like not to have
my RMAIL file load as a text file.  It would seem that I ought to be able
to do this using the variable: save-context-predicate.  But
I can't get it to work.  In fact, I notice that the default
for the variable is supposed to prevent /tmp and /usr/tmp
files from being restored, and that doesn't work for me
either.

Here is the defvar in saveconf.el

(defvar save-context-predicate
  (function (lambda (w)
	      (and (buffer-file-name (window-buffer w))
		   (not (string-match "^\\(/usr\\)?/tmp/"
				      (buffer-file-name (window-buffer w)))))))
  "*Value is a predicate function which determines which windows' contexts
are saved.  When the `save-context' command is invoked, this function will
be called once for each existing Emacs window.  The function should accept
one argument which will be a window object, and should return non-nil if
the window's context should be saved.")

This is what I tried in my .emacs file so as to prevent my
RMAIL file from being remembered.

(setq save-context-predicate
  (function (lambda (w)
	      (and (buffer-file-name (window-buffer w))
		   (not (string-match "\\(^\\(/usr\\)?/tmp/\\)\\|RMAIL"
				      (buffer-file-name (window-buffer w)))))))
)

--
		       Gilbert Harman
                       Princeton University Cognitive Science Laboratory
	               221 Nassau Street, Princeton, NJ 08542
			      
		       ghh@princeton.edu
		       HARMAN@PUCC.BITNET

kjones@talos.uucp (Kyle Jones) (07/25/89)

Gilbert Harman writes:
 > This is what I tried in my .emacs file so as to prevent my
 > RMAIL file from being remembered.
 > 
 > (setq save-context-predicate
 >   (function (lambda (w)
 > 	      (and (buffer-file-name (window-buffer w))
 > 		   (not (string-match "\\(^\\(/usr\\)?/tmp/\\)\\|RMAIL"
 > 				      (buffer-file-name (window-buffer w)))))))
 > )

This won't work unless the RMAIL buffer is in a window.  The value of
save-context-predicate is called for each *window*, not each buffer.
Perhaps save-context-predicate's semantics should be changed to the
latter.

ghh@cognito.princeton.edu (Gilbert Harman) (07/25/89)

In article <1989Jul25.145204.3015@talos.uucp> kjones@talos.uucp (Kyle Jones) writes:

   Gilbert Harman writes:
    > This is what I tried in my .emacs file so as to prevent my
    > RMAIL file from being remembered.
    > 
    > (setq save-context-predicate
    >   (function (lambda (w)
    > 	      (and (buffer-file-name (window-buffer w))
    > 		   (not (string-match "\\(^\\(/usr\\)?/tmp/\\)\\|RMAIL"
    > 				      (buffer-file-name (window-buffer w)))))))
    > )

   This won't work unless the RMAIL buffer is in a window.  The value of
   save-context-predicate is called for each *window*, not each buffer.
   Perhaps save-context-predicate's semantics should be changed to the
   latter.

Thanks for the pointer.  I have a quick and dirty solution
that introduces a new variable, save-buffer-predicate,
initially defined as follows:

(defvar save-buffer-predicate
  (function (lambda (w)
	      (and (not (string-match "\\(^\\(/usr\\)?/tmp/\\)\\|RMAIL"
				      w)))))

  "*Value is a predicate function which determines which buffers' contexts
are saved.  When the `save-context' command is invoked and
save-buffer-context is not nil, this function will be called once for each
existing Emacs buffer.  The function should accept one argument which will
be a buffer name, and should return non-nil if the buffer's context
should be saved.")

Then I modified the save-context function by inserting an
application of this function:

...
	(if save-buffer-context
	    (mapcar
	     (function
	      (lambda (b)
		(set-buffer b)
		(cond ((and buffer-file-name
			    (funcall save-buffer-predicate buffer-file-name))
		       (prin1 buffer-file-name context-buffer)
		       (princ " " context-buffer)
		       (prin1 (point) context-buffer)
		       (princ "\n" context-buffer)))))
	     (buffer-list)))
...

The antecedent in the cond originally checked to make sure
that buffer-file-name is not nil.  This now also checks to
make sure the name isn't the name of a file you don't want
to save the context for.

This seems to work.

--
		       Gilbert Harman
                       Princeton University Cognitive Science Laboratory
	               221 Nassau Street, Princeton, NJ 08542
			      
		       ghh@princeton.edu
		       HARMAN@PUCC.BITNET

grunwald@flute.cs.uiuc.edu (Dirk Grunwald) (07/26/89)

I have been using the following with great success:

(setq save-context-predicate
      (function
       (lambda (w)
	 (and
	  ; nil?
	  (buffer-file-name (window-buffer w))
	  ; /usr/tmp or /tmp?
	  (not (string-match "^\\(/usr\\)?/tmp/"
			       (buffer-file-name (window-buffer w))))
	  ; rmail mode?
	  (let
	      ((isok t))
	    (save-excursion
	      (set-buffer (window-buffer w))
	      (setq isok
		    (not (or (eq major-mode 'rmail-mode)
			     (eq major-mode 'mail-mode)))))
	    isok))
	  )))

Since I've switched to VM, I've adopted another approach:

(setq auto-mode-alist
      (append
       (list (cons "\\.mail$" 'run-vm-mode) (cons "INBOX$" 'run-vm-mode))))
(defun run-vm-mode ()
  (vm (buffer-file-name)))


which works very well. You could also change the pattern to match
things in your vm-folder-directory, but I define that later, so
didn't do that here.
--
Dirk Grunwald -- Univ. of Illinois 		  (grunwald@flute.cs.uiuc.edu)

wjc@ho5cad.ATT.COM (Bill Carpenter) (07/26/89)

In article <GHH.89Jul24201245@cognito.princeton.edu> ghh@cognito.princeton.edu (Gilbert Harman) writes:

> I have been using the recently posted version of saveconf.el ....
> Except that I would like not to have my RMAIL file load as a text
> file.

I don't use saveconf.el any more, but when I did, I had the same
problem.  Here is how I got around it (see below).  I found this
non-intuitive enough that I saved it as a comment in my ~/.emacs even
after I stopped using it.  In addition to protecting RMAIL, it also
protected my directory of mail folders.
--
   Bill Carpenter         att!ho5cad!wjc  or  attmail!bill


;; save context stuff
;; The original "saveconf.el" didn't apply the predicate to buffers
;; that weren't currently displayed in a window (they were saved
;; unconditionally.  That was trouble if you had an RMAIL file because
;; it got visited on startup and so was not configured correctly when
;; visited via "rmail" later.
;(setq save-context-predicate
;  (function (lambda (w)
;	      (and 
;		   (buffer-file-name (if (bufferp w) w (window-buffer w)))
;		   (not (string-match "RMAIL"
;		      (buffer-file-name (if (bufferp w) w (window-buffer w)))))
;		   (not (string-match "/mail/"
;		      (buffer-file-name (if (bufferp w) w (window-buffer w)))))
;		   (not (string-match "^\\(/usr\\)?/tmp/"
;		      (buffer-file-name (if (bufferp w) w (window-buffer w)))))
;))))
;