all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* with-temp-buffer, insert-file-content and errors
@ 2005-07-15 22:54 drkm
  2005-07-17  6:45 ` Richard M. Stallman
  0 siblings, 1 reply; 5+ messages in thread
From: drkm @ 2005-07-15 22:54 UTC (permalink / raw)


  Hi

  If you eval the following:

    (defun drkm:test ()
      (interactive)
      (with-temp-buffer
        (insert-file-contents "/tmp/an-existing-file" t)
        (insert "To modify the buffer...")
        (error "Some error")))

and then 'M-x drkm:test <RET>', you'll be prompt for saving or
not the " *temp*" buffer, *before* seeing the error (making a
function is only to not have the debugger started because using
'C-x C-e').

  But because you don't see the error when you are prompted, in
some case it may be more difficult to know what you want to
response.

  I guess 'with-temp-buffer' have to be modified to show the
error before (or while) 'kill-buffer' prompts the user, isn't it?

--drkm

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: with-temp-buffer, insert-file-content and errors
  2005-07-15 22:54 with-temp-buffer, insert-file-content and errors drkm
@ 2005-07-17  6:45 ` Richard M. Stallman
  2005-07-17 12:11   ` drkm
  2005-07-18 23:11   ` Michael Olson
  0 siblings, 2 replies; 5+ messages in thread
From: Richard M. Stallman @ 2005-07-17  6:45 UTC (permalink / raw)
  Cc: emacs-devel

	(defun drkm:test ()
	  (interactive)
	  (with-temp-buffer
	    (insert-file-contents "/tmp/an-existing-file" t)
	    (insert "To modify the buffer...")
	    (error "Some error")))

I think it is incorrect to use insert-file-contents with VISIT non-nil
on a temp buffer, so I think we don't need to try to fix this.
(It would not be easy, since the unwind-protect cleanup has to
execute before the error message gets displayed.)

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: with-temp-buffer, insert-file-content and errors
  2005-07-17  6:45 ` Richard M. Stallman
@ 2005-07-17 12:11   ` drkm
  2005-07-18 23:11   ` Michael Olson
  1 sibling, 0 replies; 5+ messages in thread
From: drkm @ 2005-07-17 12:11 UTC (permalink / raw)


"Richard M. Stallman" writes:

> I think it is incorrect to use insert-file-contents with VISIT non-nil
> on a temp buffer, so I think we don't need to try to fix this.

  Ok.  Where did you find this info?

> (It would not be easy, since the unwind-protect cleanup has to
> execute before the error message gets displayed.)

  Yep.

--drkm

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: with-temp-buffer, insert-file-content and errors
  2005-07-17  6:45 ` Richard M. Stallman
  2005-07-17 12:11   ` drkm
@ 2005-07-18 23:11   ` Michael Olson
  2005-07-19 14:34     ` Stefan Monnier
  1 sibling, 1 reply; 5+ messages in thread
From: Michael Olson @ 2005-07-18 23:11 UTC (permalink / raw)



[-- Attachment #1.1: Type: text/plain, Size: 2569 bytes --]

"Richard M. Stallman" <rms@gnu.org> writes:

> I think it is incorrect to use insert-file-contents with VISIT
> non-nil on a temp buffer, so I think we don't need to try to fix
> this.  (It would not be easy, since the unwind-protect cleanup has
> to execute before the error message gets displayed.)

drkm was using code from my emacs-wiki program as an example for this.
You're right in that using a non-nil VISIT argument was a bad idea.
I've also worked around the "prompted to save a temp buffer" problem
by making a macro called `emacs-wiki-with-temp-buffer' as follows.
The condition-case call is mainly used for displaying diagnostic
information, so it might not be generally useful.  A part of the
cleanup code,

         (when (buffer-live-p ,temp-buffer)
           (with-current-buffer ,temp-buffer
             (set-buffer-modified-p nil))
           (... (kill-buffer ,temp-buffer)))

might perhaps be generally useful, but this is mostly an FYI rather
than a feature request.

(defmacro emacs-wiki-with-temp-buffer (&rest body)
  "Create a temporary buffer, and evaluate BODY there like `progn'.
See also `with-temp-file' and `with-output-to-string'.
Unlike `with-temp-buffer', this will never attempt to save the temp buffer."
  (let ((temp-buffer (make-symbol "temp-buffer")))
    `(let ((,temp-buffer (generate-new-buffer " *emacs-wiki-temp*")))
       (unwind-protect
           (condition-case err
               (with-current-buffer ,temp-buffer
                 ,@body)
             (error
              (if (fboundp 'display-warning)
                  (display-warning 'emacs-wiki
                                   (format "%s: Error occurred: %s"
                                           (emacs-wiki-page-name)
                                           err)
                                   :warning)
                (message "%s: Error occurred: %s"
                         (emacs-wiki-page-name)
                         err))))
         (when (buffer-live-p ,temp-buffer)
           (with-current-buffer ,temp-buffer
             (set-buffer-modified-p nil))
           (unless debug-on-error (kill-buffer ,temp-buffer)))))))
(put 'emacs-wiki-with-temp-buffer 'lisp-indent-function 0)
(put 'emacs-wiki-with-temp-buffer 'edebug-form-spec '(body))

-- 
Michael Olson -- FSF Associate Member #652 -- http://www.mwolson.org/
Interests: anime, Debian, XHTML, wiki, Emacs Lisp
  /` |\ | | | IRC: mwolson on freenode.net: #hcoop, #muse, #pulug
 |_] | \| |_| Jabber: mwolson_at_hcoop.net

[-- Attachment #1.2: Type: application/pgp-signature, Size: 188 bytes --]

[-- Attachment #2: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: with-temp-buffer, insert-file-content and errors
  2005-07-18 23:11   ` Michael Olson
@ 2005-07-19 14:34     ` Stefan Monnier
  0 siblings, 0 replies; 5+ messages in thread
From: Stefan Monnier @ 2005-07-19 14:34 UTC (permalink / raw)
  Cc: emacs-devel

> drkm was using code from my emacs-wiki program as an example for this.
> You're right in that using a non-nil VISIT argument was a bad idea.
> I've also worked around the "prompted to save a temp buffer" problem
> by making a macro called `emacs-wiki-with-temp-buffer' as follows.
> The condition-case call is mainly used for displaying diagnostic
> information, so it might not be generally useful.  A part of the
> cleanup code,

>          (when (buffer-live-p ,temp-buffer)
>            (with-current-buffer ,temp-buffer
>              (set-buffer-modified-p nil))
>            (... (kill-buffer ,temp-buffer)))

A temp buffer's buffer-file-name should be nil.  Otherwise, it doesn't
deserve the name "temporary".  So the above is a bad idea since it will hide
bugs like the one where you've set the VISIT arg to non-nil.


        Stefan

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2005-07-19 14:34 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-07-15 22:54 with-temp-buffer, insert-file-content and errors drkm
2005-07-17  6:45 ` Richard M. Stallman
2005-07-17 12:11   ` drkm
2005-07-18 23:11   ` Michael Olson
2005-07-19 14:34     ` Stefan Monnier

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.