From: Stephen Leake <stephen_leake@stephe-leake.org>
To: emacs-devel <emacs-devel@gnu.org>
Subject: Re: fix for bug#29935 copyright-update inserts year at random places
Date: Wed, 03 Jan 2018 16:52:54 -0600 [thread overview]
Message-ID: <86shbm4ks9.fsf@stephe-leake.org> (raw)
In-Reply-To: <837et0ckou.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 02 Jan 2018 18:03:13 +0200")
Eli Zaretskii <eliz@gnu.org> writes:
>> From: Stephen Leake <stephen_leake@stephe-leake.org>
>> 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= (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.
'(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= (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= (buffer-substring (- (match-end 3) 2) (match-end 3))
+ (substring copyright-current-year -2))
+ (if (or noquery
+ ;; ‘switch-to-buffer’ 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))))
+ ))))
;;;###autoload
(defun copyright-update (&optional arg interactivep)
Ok to commit?
--
-- Stephe
next prev parent reply other threads:[~2018-01-03 22:52 UTC|newest]
Thread overview: 22+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-01 23:29 fix for bug#29935 copyright-update inserts year at random places Stephen Leake
2018-01-02 9:21 ` Lele Gaifax
2018-01-02 16:03 ` Eli Zaretskii
2018-01-03 22:52 ` Stephen Leake [this message]
2018-01-04 1:01 ` Stefan Monnier
2018-01-04 3:21 ` Stephen Leake
2018-01-04 5:34 ` Stefan Monnier
2018-01-04 12:47 ` Stephen Leake
2018-01-04 18:23 ` Stefan Monnier
2018-01-07 16:08 ` martin rudalics
2018-01-07 17:34 ` Stefan Monnier
2018-01-08 9:53 ` martin rudalics
2018-01-08 13:15 ` Stefan Monnier
2018-01-08 16:56 ` Stephen Leake
2018-01-08 18:37 ` martin rudalics
2018-01-08 19:05 ` Eli Zaretskii
2018-01-11 16:48 ` Stephen Leake
2018-01-08 18:18 ` martin rudalics
2018-01-08 22:18 ` Stefan Monnier
2018-01-09 9:42 ` martin rudalics
2018-01-09 13:07 ` Stefan Monnier
2018-01-10 10:19 ` martin rudalics
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=86shbm4ks9.fsf@stephe-leake.org \
--to=stephen_leake@stephe-leake.org \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).