From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Lars Ingebrigtsen Newsgroups: gmane.emacs.bugs Subject: bug#34720: 26.1; Reverting a GPG buffer moves all markers to the end of the file Date: Mon, 26 Aug 2019 09:20:29 +0200 Message-ID: <875zmk5r5u.fsf@gnus.org> References: <87a7ic9due.fsf@gnu.org> <87muhnrwvs.fsf@mouse.gnus.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="93528"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux) Cc: 34720@debbugs.gnu.org To: Ian Dunn Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Aug 26 09:21:17 2019 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1i29JP-000O3x-Qh for geb-bug-gnu-emacs@m.gmane.org; Mon, 26 Aug 2019 09:21:11 +0200 Original-Received: from localhost ([::1]:50358 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i29JO-0007Fm-8d for geb-bug-gnu-emacs@m.gmane.org; Mon, 26 Aug 2019 03:21:10 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37944) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i29JI-0007Ff-EO for bug-gnu-emacs@gnu.org; Mon, 26 Aug 2019 03:21:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i29JG-0004UO-Ug for bug-gnu-emacs@gnu.org; Mon, 26 Aug 2019 03:21:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:36682) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i29JG-0004UE-QT for bug-gnu-emacs@gnu.org; Mon, 26 Aug 2019 03:21:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1i29JG-0002Uc-IL for bug-gnu-emacs@gnu.org; Mon, 26 Aug 2019 03:21:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Lars Ingebrigtsen Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 26 Aug 2019 07:21:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 34720 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: confirmed Original-Received: via spool by 34720-submit@debbugs.gnu.org id=B34720.15668040389510 (code B ref 34720); Mon, 26 Aug 2019 07:21:02 +0000 Original-Received: (at 34720) by debbugs.gnu.org; 26 Aug 2019 07:20:38 +0000 Original-Received: from localhost ([127.0.0.1]:45503 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1i29Ir-0002TI-MR for submit@debbugs.gnu.org; Mon, 26 Aug 2019 03:20:38 -0400 Original-Received: from quimby.gnus.org ([80.91.231.51]:49272) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1i29Ip-0002T8-Ln for 34720@debbugs.gnu.org; Mon, 26 Aug 2019 03:20:36 -0400 Original-Received: from cm-84.212.202.86.getinternet.no ([84.212.202.86] helo=marnie) by quimby.gnus.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1i29Ij-0004iC-Uk; Mon, 26 Aug 2019 09:20:33 +0200 In-Reply-To: <87muhnrwvs.fsf@mouse.gnus.org> (Lars Ingebrigtsen's message of "Tue, 09 Jul 2019 18:33:11 +0200") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 209.51.188.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.org gmane.emacs.bugs:165884 Archived-At: Lars Ingebrigtsen writes: >> 1. Open a GnuPG encrypted file >> 2. Run the following: >> (setq test-marker (make-marker)) >> (move-marker test-marker (point-marker)) >> ;; test-marker points to `point' >> 3. Revert the buffer >> ;; test-marker now points to the end of the buffer I took a stab at this with the patch below (which is the wrong solution because it doesn't clear the undo list etc). But I don't quite understand why this is so much work. :-) The problem is twofold: The first is in epa.el itself, which deletes and then inserts, which destroys the markers. The second is the call to `decode-coding-region' in `decode-coding-inserted-region', which also destroys the markers. The third is that the interface to the new replace-buffer-contents function is really awkward -- it only takes a buffer as its SOURCE, which means that if you want to feed it something, you have to skip around to temporary buffers instead of feeding it a string, which would be natural. You have to be a with-temp-buffer/with-current-buffer/let contortionist to get it to be safe. And replace-buffer-contents does so much more than just restoring markers, anyway, so I'm not sure it's the right solution here, anyway. Would anybody mind if I just write a `with-saved-markers' macro in subr-x, which would make all these problems to away and make the solution a two-liner in epa itself? (Unless such a macro exists somewhere already.) diff --git a/lisp/epa-file.el b/lisp/epa-file.el index d9886d3d67..03f53c40a9 100644 --- a/lisp/epa-file.el +++ b/lisp/epa-file.el @@ -24,6 +24,7 @@ (require 'epa) (require 'epa-hook) +(require 'mule) (defcustom epa-file-cache-passphrase-for-symmetric-encryption nil "If non-nil, cache passphrase for symmetric encryption. @@ -102,16 +103,21 @@ epa-file-run-real-handler (apply operation args))) (defun epa-file-decode-and-insert (string file visit beg end replace) - (if (fboundp 'decode-coding-inserted-region) - (save-restriction - (narrow-to-region (point) (point)) - (insert string) - (decode-coding-inserted-region - (point-min) (point-max) - (substring file 0 (string-match epa-file-name-regexp file)) - visit beg end replace)) - (insert (epa-file--decode-coding-string string (or coding-system-for-read - 'undecided))))) + (let ((buffer (current-buffer))) + (with-temp-buffer + (insert string) + (let ((coding (decode-coding-inserted-region-coding-system + (substring + file 0 (string-match epa-file-name-regexp file)) + visit beg end replace))) + (if coding + (decode-coding-region (point-min) (point-max) coding) + (setq last-coding-system-used coding)) + (let ((temp (current-buffer))) + (with-current-buffer buffer + (if replace + (replace-buffer-contents temp) + (insert-buffer-substring temp)))))))) (defvar epa-file-error nil) (defun epa-file--find-file-not-found-function () @@ -147,8 +153,6 @@ epa-file-insert-file-contents (format "Decrypting %s" file))) (unwind-protect (progn - (if replace - (goto-char (point-min))) (condition-case error (setq string (epg-decrypt-file context local-file nil)) (error @@ -187,12 +191,9 @@ epa-file-insert-file-contents ;; really edit the buffer. (let ((buffer-file-name (if visit nil buffer-file-name))) - (save-restriction - (narrow-to-region (point) (point)) - (epa-file-decode-and-insert string file visit beg end replace) - (setq length (- (point-max) (point-min)))) - (if replace - (delete-region (point) (point-max)))) + (setq length + (epa-file-decode-and-insert + string file visit beg end replace))) (if visit (set-visited-file-modtime)))) (if (and local-copy diff --git a/lisp/international/mule.el b/lisp/international/mule.el index ec6f647688..5338e54cf6 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el @@ -2244,6 +2244,25 @@ modify-coding-system-alist (cons (cons regexp coding-system) network-coding-system-alist))))))) +(defun decode-coding-inserted-region-coding-system (filename + visit beg end replace) + "Return a coding system for the current buffer as if it is read from FILENAME." + (let ((coding coding-system-for-read)) + (unless coding + (setq coding (funcall set-auto-coding-function + filename (- (point-max) (point-min))))) + (unless coding + (setq coding (car (find-operation-coding-system + 'insert-file-contents + (cons filename (current-buffer)) + visit beg end replace)))) + (if (coding-system-p coding) + (or enable-multibyte-characters + (setq coding + (coding-system-change-text-conversion coding 'raw-text))) + (setq coding nil)) + coding)) + (defun decode-coding-inserted-region (from to filename &optional visit beg end replace) "Decode the region between FROM and TO as if it is read from file FILENAME. @@ -2253,8 +2272,7 @@ decode-coding-inserted-region Part of the job of this function is setting `buffer-undo-list' appropriately." (save-excursion (save-restriction - (let ((coding coding-system-for-read) - undo-list-saved) + (let (coding undo-list-saved) (if visit ;; Temporarily turn off undo recording, if we're decoding the ;; text of a visited file. @@ -2268,19 +2286,8 @@ decode-coding-inserted-region buffer-undo-list t)))) (narrow-to-region from to) (goto-char (point-min)) - (or coding - (setq coding (funcall set-auto-coding-function - filename (- (point-max) (point-min))))) - (or coding - (setq coding (car (find-operation-coding-system - 'insert-file-contents - (cons filename (current-buffer)) - visit beg end replace)))) - (if (coding-system-p coding) - (or enable-multibyte-characters - (setq coding - (coding-system-change-text-conversion coding 'raw-text))) - (setq coding nil)) + (setq coding (decode-coding-inserted-region-coding-system + filename visit beg end replace)) (if coding (decode-coding-region (point-min) (point-max) coding) (setq last-coding-system-used coding)) -- (domestic pets only, the antidote for overdose, milk.) bloggy blog: http://lars.ingebrigtsen.no