From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: How can I undo a change from a modification-hook? Date: Wed, 26 Apr 2017 08:26:13 -0400 Message-ID: References: <2448005f-9fc4-78b2-3203-9fc8204f6f5b@mit.edu> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1493209640 29919 195.159.176.226 (26 Apr 2017 12:27:20 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 26 Apr 2017 12:27:20 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Apr 26 14:27:17 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3M2N-0007fx-Rd for ged-emacs-devel@m.gmane.org; Wed, 26 Apr 2017 14:27:15 +0200 Original-Received: from localhost ([::1]:54994 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3M2T-00050d-Nf for ged-emacs-devel@m.gmane.org; Wed, 26 Apr 2017 08:27:21 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:57788) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1d3M1i-0004uO-WC for emacs-devel@gnu.org; Wed, 26 Apr 2017 08:26:35 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1d3M1f-0001Im-1X for emacs-devel@gnu.org; Wed, 26 Apr 2017 08:26:34 -0400 Original-Received: from [195.159.176.226] (port=58543 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1d3M1e-0001Hr-RE for emacs-devel@gnu.org; Wed, 26 Apr 2017 08:26:30 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1d3M1R-0006Pt-6b for emacs-devel@gnu.org; Wed, 26 Apr 2017 14:26:17 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 22 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:0m7oErut3UfjyNy4CXF7RTSTpRg= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:214305 Archived-At: > (by raising an error), and to allow changes when in a comment. I can't > find a good way to allow whitespace edits at the end of a line, though. > I can detect such changes from the second call to the modification hook > (during which it's easy to know whether the change is only adding or > removing space at the end of a line), but I can't easily undo the changes > and error out if they involve more than whitespace, can I? Technically you can, but it's a bit messy and will likely not be 100% reliable. Personally, in your case, I wouldn't bother doing it, but here's how I'd do it otherwise: - in the before-hook stash the value of buffer-undo-list somewhere. [ While you're at it, stash the beg/end there as well. ] - in the after-hook check the change (using beg/end/len) [ and while you're there, check that the beg/end/len is within the announces beg/end stashed earlier, and if it's not just don't do anything since it means the stash buffer-undo-list is likely unrelated. ] and if it doesn't fit your criteria, call undo until you reach the previously stashed value of buffer-undo-list, and then signal the error. -- Stefan