help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Automatically checking parentheses before a global save not working.


From: Andreas Politz
Subject: Re: Automatically checking parentheses before a global save not working.
Date: Wed, 08 Dec 2010 15:16:49 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Elena <egarrulo@gmail.com> writes:

> Hello,
>
> I've written a function to check parentheses of Lisp buffers during a
> global save - after being added to `before-save-hook' - so that
> whenever it spots an unbalanced parentheses, it offers the user a
> chance to fix it. However, it seems I can't switch to the offending
> buffer. Maybe I just don't understand the way `condition-case' works.
>
> Here is my code:
>
> (defun fix-parens-or-save-anyway ()
>       "Check for unbalanced parentheses before saving. To be used with
> `before-save-hook'.
>
> If parentheses are balanced, do nothing. Otherwise let the user know
> and ask for confirmation before saving. If user confirms, do nothing;
> otherwise switch to such buffer and let the user fix the error.
> "
>       (condition-case nil
>               (check-parens)
>               (error
>                (unless (yes-or-no-p (format "%s: Unbalanced parentheses. Save
> anyway?" (buffer-name)))
>                        (switch-to-buffer (buffer-name))

There is no user interaction at this point.  You could try something
with `recursive-edit', but I don't know if it is a good idea to change
the buffer state at this point (in the `before-save-hook').

Also, I suppose running the hook is wrapped in some
current-buffer-saving-macro.

>                        (check-parens)))))
>
> (defun my-emacs-lisp-save-hook()
>       (add-hook 'before-save-hook
>                         'fix-parens-or-save-anyway
>                         nil ; Prepend this hook (default).
>                         t ; This is a local hook.
>                         ))
>
> (add-hook 'emacs-lisp-mode-hook 'my-emacs-lisp-save-hook)
>
> Thanks for your help.

-ap


reply via email to

[Prev in Thread] Current Thread [Next in Thread]