From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stephen Leake Newsgroups: gmane.emacs.devel Subject: Re: fix for bug#29935 copyright-update inserts year at random places Date: Wed, 03 Jan 2018 16:52:54 -0600 Message-ID: <86shbm4ks9.fsf@stephe-leake.org> References: <868tdh5fag.fsf@stephe-leake.org> <837et0ckou.fsf@gnu.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1515019906 7255 195.159.176.226 (3 Jan 2018 22:51:46 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Wed, 3 Jan 2018 22:51:46 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.90 (windows-nt) To: emacs-devel Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Jan 03 23:51:41 2018 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 1eWrso-0001Ts-FJ for ged-emacs-devel@m.gmane.org; Wed, 03 Jan 2018 23:51:38 +0100 Original-Received: from localhost ([::1]:40276 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eWrun-0001Z4-Ov for ged-emacs-devel@m.gmane.org; Wed, 03 Jan 2018 17:53:41 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37329) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eWruB-0001Xr-9q for emacs-devel@gnu.org; Wed, 03 Jan 2018 17:53:04 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eWru6-0007tv-St for emacs-devel@gnu.org; Wed, 03 Jan 2018 17:53:03 -0500 Original-Received: from smtp137.ord.emailsrvr.com ([173.203.6.137]:46220) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eWru6-0007sE-NM for emacs-devel@gnu.org; Wed, 03 Jan 2018 17:52:58 -0500 Original-Received: from smtp26.relay.ord1a.emailsrvr.com (localhost [127.0.0.1]) by smtp26.relay.ord1a.emailsrvr.com (SMTP Server) with ESMTP id B0365200D1 for ; Wed, 3 Jan 2018 17:52:57 -0500 (EST) X-Auth-ID: board-president@tomahawk-creek-hoa.com Original-Received: by smtp26.relay.ord1a.emailsrvr.com (Authenticated sender: board-president-AT-tomahawk-creek-hoa.com) with ESMTPSA id 6AF40200E5 for ; Wed, 3 Jan 2018 17:52:57 -0500 (EST) X-Sender-Id: board-president@tomahawk-creek-hoa.com Original-Received: from Takver4 (76-218-37-33.lightspeed.kscymo.sbcglobal.net [76.218.37.33]) (using TLSv1.2 with cipher AES256-GCM-SHA384) by 0.0.0.0:587 (trex/5.7.12); Wed, 03 Jan 2018 17:52:57 -0500 In-Reply-To: <837et0ckou.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 02 Jan 2018 18:03:13 +0200") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 173.203.6.137 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:221566 Archived-At: Eli Zaretskii writes: >> From: Stephen Leake >> Date: Mon, 01 Jan 2018 17:29:27 -0600 >> >> Ok to commit to emacs-26? > > How important is it to have this in Emacs 26? > >> + (let ((copyright-end (point))) >> + (setq copyright-current-year (format-time-string "%Y")) >> + (unless (string=3D (buffer-substring (- (match-end 3) 2) (match-end= 3)) >> + (substring copyright-current-year -2)) >> + (if (or noquery >> + (save-window-excursion >> + ;; Fixes some point-moving oddness (bug#2209, bug#29935). >> + (save-excursion >> + (switch-to-buffer (current-buffer)) >> + ;; Ensure the copyright line is displayed. >> + (goto-char copyright-end) >> + (y-or-n-p (if replace >> + (concat "Replace copyright year(s) by " >> + copyright-current-year "? ") >> + (concat "Add " copyright-current-year >> + " to copyright? ")))))) > > Why does this need to use save-window-excursion? 'switch-to-buffer' can pop up a new frame, or use a different window, depending on various settings, so save-window-excursion is needed.=20 '(goto-char copyright-end)' is needed because in general, a buffer does not have a well-defined 'point'; is has a point in each window it is displayed in, and recent Emacsen preserve that point when the buffer is swapped in and out of that window (which is a nice feature). 'copyright-find-end' changes point in the buffer, but that's not used if it's not displayed in the current window. You suggest "using other facilities", but isn't this precisely what save-window-excursion and save-excursion are for? Here's a new patch, with improved comments: --- a/lisp/emacs-lisp/copyright.el +++ b/lisp/emacs-lisp/copyright.el @@ -181,44 +181,54 @@ copyright-update-year ;; This uses the match-data from copyright-find-copyright/end. (goto-char (match-end 1)) (copyright-find-end) - (setq copyright-current-year (format-time-string "%Y")) - (unless (string=3D (buffer-substring (- (match-end 3) 2) (match-end 3)) - (substring copyright-current-year -2)) - (if (or noquery - (save-window-excursion - (switch-to-buffer (current-buffer)) - ;; Fixes some point-moving oddness (bug#2209). - (save-excursion - (y-or-n-p (if replace - (concat "Replace copyright year(s) by " - copyright-current-year "? ") - (concat "Add " copyright-current-year - " to copyright? ")))))) - (if replace - (replace-match copyright-current-year t t nil 3) - (let ((size (save-excursion (skip-chars-backward "0-9")))) - (if (and (eq (% (- (string-to-number copyright-current-year) - (string-to-number (buffer-substring - (+ (point) size) - (point)))) - 100) - 1) - (or (eq (char-after (+ (point) size -1)) ?-) - (eq (char-after (+ (point) size -2)) ?-))) - ;; This is a range so just replace the end part. - (delete-char size) - ;; Insert a comma with the preferred number of spaces. - (insert - (save-excursion - (if (re-search-backward "[0-9]\\( *, *\\)[0-9]" - (line-beginning-position) t) - (match-string 1) - ", "))) - ;; If people use the '91 '92 '93 scheme, do that as well. - (if (eq (char-after (+ (point) size -3)) ?') - (insert ?'))) - ;; Finally insert the new year. - (insert (substring copyright-current-year size))))))) + (let ((copyright-end (point))) + (setq copyright-current-year (format-time-string "%Y")) + (unless (string=3D (buffer-substring (- (match-end 3) 2) (match-end 3)) + (substring copyright-current-year -2)) + (if (or noquery + ;; =E2=80=98switch-to-buffer=E2=80=99 can pop up a new frame= , or use + ;; another window, so preserve the current window + ;; config. + (save-window-excursion + ;; Fixes some point-moving oddness (bug#2209, bug#29935). + (save-excursion + (switch-to-buffer (current-buffer)) + ;; Ensure the copyright line is displayed; + ;; switch-to-buffer has moved point to where it was + ;; when this buffer was last displayed in this + ;; window. + (goto-char copyright-end) + (y-or-n-p (if replace + (concat "Replace copyright year(s) by " + copyright-current-year "? ") + (concat "Add " copyright-current-year + " to copyright? ")))))) + (if replace + (replace-match copyright-current-year t t nil 3) + (let ((size (save-excursion (skip-chars-backward "0-9")))) + (if (and (eq (% (- (string-to-number copyright-current-year) + (string-to-number (buffer-substring + (+ (point) size) + (point)))) + 100) + 1) + (or (eq (char-after (+ (point) size -1)) ?-) + (eq (char-after (+ (point) size -2)) ?-))) + ;; This is a range so just replace the end part. + (delete-char size) + ;; Insert a comma with the preferred number of spaces. + (insert + (save-excursion + (if (re-search-backward "[0-9]\\( *, *\\)[0-9]" + (line-beginning-position) t) + (match-string 1) + ", "))) + ;; If people use the '91 '92 '93 scheme, do that as well. + (if (eq (char-after (+ (point) size -3)) ?') + (insert ?'))) + ;; Finally insert the new year. + (insert (substring copyright-current-year size)))) + )))) =20 ;;;###autoload (defun copyright-update (&optional arg interactivep) Ok to commit? -- -- Stephe