all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Automatically checking parentheses before a global save not working.
@ 2010-07-01 13:49 Elena
       [not found] ` <87k4pffa8w.fsf@fh-trier.de>
  0 siblings, 1 reply; 4+ messages in thread
From: Elena @ 2010-07-01 13:49 UTC (permalink / raw)
  To: help-gnu-emacs

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))
			 (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.


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

* Re: Automatically checking parentheses before a global save not  working.
       [not found]   ` <e4d6ef68-2e04-44d8-9765-eda0bb8c338d@c10g2000yqi.googlegroups.com>
@ 2010-07-01 19:11     ` Uday S Reddy
  2010-08-04 15:37       ` Johan Bockgård
       [not found]     ` <87eifndnck.fsf@fh-trier.de>
  1 sibling, 1 reply; 4+ messages in thread
From: Uday S Reddy @ 2010-07-01 19:11 UTC (permalink / raw)
  To: help-gnu-emacs

Elena wrote:
> On 1 Lug, 18:16, Andreas Politz <poli...@fh-trier.de> wrote:
>>>                     (switch-to-buffer (buffer-name))
>> There is no user interaction at this point.
> 
> What do you mean, please? Can't I call `switch-to-buffer' there?
> However, `yes-or-no-p' (which interacts with the user) works, and the
> buffer to fix is being correctly identified by name.

All that is fine.  But you can't expect to be able to edit the buffer in the 
midst of a save-buffer action.  The best thing to would be to signal an error 
again, so that the save-buffer is aborted.

> 
> If I can't switch buffer right away, maybe an alternative and safe
> approach would be to set a delayed switch, which would run after
> termination of `condition-case'.
> 
>> Also, I suppose running the hook is wrapped in some
>> current-buffer-saving-macro.

Andreas is wondering if save-buffer calls the before-save-hook inside a 
save-current-buffer macro, so that save-buffer finishes in the same buffer it 
started.  If so, running switch-to-buffer in the middle of the hook will have 
no effect.

You might try printing an error message, e.g.,

   (error "Unbalanced parentheses in %s" (current-buffer))

Cheers,
Uday


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

* Re: Automatically checking parentheses before a global save not  working.
       [not found]     ` <87eifndnck.fsf@fh-trier.de>
@ 2010-07-02  8:54       ` Elena
  0 siblings, 0 replies; 4+ messages in thread
From: Elena @ 2010-07-02  8:54 UTC (permalink / raw)
  To: help-gnu-emacs

On 1 Lug, 19:16, Andreas Politz <poli...@fh-trier.de> wrote:
> (condition-case nil
>     (check-parens)
>   (error
>    (when (yes-or-no-p (format "Fix parens in buffer %s ?" (buffer-name)))
>      (switch-to-buffer (current-buffer))
>      (recursive-edit) ;;Edit and press C-M-c
>      (check-parens))))

Thanks Andreas. This works even better than what I was trying to
achieve (I would have just aborted saving before leaving the user into
the offending buffer).


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

* Re: Automatically checking parentheses before a global save not working.
  2010-07-01 19:11     ` Uday S Reddy
@ 2010-08-04 15:37       ` Johan Bockgård
  0 siblings, 0 replies; 4+ messages in thread
From: Johan Bockgård @ 2010-08-04 15:37 UTC (permalink / raw)
  To: help-gnu-emacs

Uday S Reddy <uDOTsDOTreddy@cs.bham.ac.uk> writes:

> Andreas is wondering if save-buffer calls the before-save-hook inside
> a save-current-buffer macro, so that save-buffer finishes in the same
> buffer it started.  If so, running switch-to-buffer in the middle of
> the hook will have no effect.

Yes it will.


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

end of thread, other threads:[~2010-08-04 15:37 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-07-01 13:49 Automatically checking parentheses before a global save not working Elena
     [not found] ` <87k4pffa8w.fsf@fh-trier.de>
     [not found]   ` <e4d6ef68-2e04-44d8-9765-eda0bb8c338d@c10g2000yqi.googlegroups.com>
2010-07-01 19:11     ` Uday S Reddy
2010-08-04 15:37       ` Johan Bockgård
     [not found]     ` <87eifndnck.fsf@fh-trier.de>
2010-07-02  8:54       ` Elena

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.