GNU bug report logs - #48043
UTF-8 magic comment is unwelcome with recent Ruby versions

Previous Next

Package: emacs;

Reported by: Peter Oliver <p.d.oliver <at> mavit.org.uk>

Date: Mon, 26 Apr 2021 18:29:01 UTC

Severity: normal

Tags: patch

Fixed in version 28.1

Done: Dmitry Gutov <dgutov <at> yandex.ru>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 48043 in the body.
You can then email your comments to 48043 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#48043; Package emacs. (Mon, 26 Apr 2021 18:29:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to Peter Oliver <p.d.oliver <at> mavit.org.uk>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Mon, 26 Apr 2021 18:29:01 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Peter Oliver <p.d.oliver <at> mavit.org.uk>
To: bug-gnu-emacs <at> gnu.org
Subject: UTF-8 magic comment is unwelcome with recent Ruby versions
Date: Mon, 26 Apr 2021 19:28:21 +0100 (BST)
[Message part 1 (text/plain, inline)]
When saving a ruby-mode buffer, if the content is not plain ASCII, then the default behaviour is to add a specially-formatted comment that indicates the encoding to the Ruby interpreter.  E.g.,

# coding: utf-8

However, since Ruby 2.0 released in 2013, the default encoding for Ruby has been UTF-8.  Consequently, users of other editors tend not to include this comment when using UTF-8.  When you edit such a file with Emacs, you end up with a messy diff.

Two patches are attached to address this:

- The first patch adds a new choice to ruby-insert-encoding-magic-comment, unless-utf8, which causes the magic comment not to be inserted if the encoding is UTF-8.

- The second patch, perhaps more controversially, makes this the default.

-- 
Peter Oliver
[0001-New-choice-for-ruby-insert-encoding-magic-comment-un.patch (text/plain, attachment)]
[0002-Default-ruby-insert-encoding-magic-comment-to-unless.patch (text/plain, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48043; Package emacs. (Mon, 26 Apr 2021 21:06:01 GMT) Full text and rfc822 format available.

Message #8 received at 48043 <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Peter Oliver <p.d.oliver <at> mavit.org.uk>, 48043 <at> debbugs.gnu.org
Subject: Re: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby
 versions
Date: Tue, 27 Apr 2021 00:04:58 +0300
Hi!

On 26.04.2021 21:28, Peter Oliver wrote:
> When saving a ruby-mode buffer, if the content is not plain ASCII, then 
> the default behaviour is to add a specially-formatted comment that 
> indicates the encoding to the Ruby interpreter.  E.g.,
> 
> # coding: utf-8
> 
> However, since Ruby 2.0 released in 2013, the default encoding for Ruby 
> has been UTF-8.  Consequently, users of other editors tend not to 
> include this comment when using UTF-8.  When you edit such a file with 
> Emacs, you end up with a messy diff.
> 
> Two patches are attached to address this:
> 
> - The first patch adds a new choice to 
> ruby-insert-encoding-magic-comment, unless-utf8, which causes the magic 
> comment not to be inserted if the encoding is UTF-8.
> 
> - The second patch, perhaps more controversially, makes this the default.

Both changes make sense to me.

However, I've looked at the existing code and found a prior change which 
intended for this to be more customizable already, yet had a minor bug.

Please try out the following patch:

diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 84ac8fdb28..35772827ce 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -291,6 +291,7 @@ ruby-deep-indent-paren-style

 (defcustom ruby-encoding-map
   '((us-ascii       . nil)       ;; Do not put coding: us-ascii
+    (utf-8          . nil)       ;; Default since Ruby 2.0
     (shift-jis      . cp932)     ;; Emacs charset name of Shift_JIS
     (shift_jis      . cp932)     ;; MIME charset name of Shift_JIS
     (japanese-cp932 . cp932))    ;; Emacs charset name of CP932
@@ -760,7 +761,7 @@ ruby--insert-coding-comment

 (defun ruby--detect-encoding ()
   (if (eq ruby-insert-encoding-magic-comment 'always-utf8)
-      "utf-8"
+      'utf-8
     (let ((coding-system
            (or save-buffer-coding-system
                buffer-file-coding-system)))
@@ -769,12 +770,11 @@ ruby--detect-encoding
                 (or (coding-system-get coding-system 'mime-charset)
                     (coding-system-change-eol-conversion coding-system 
nil))))
       (if coding-system
-          (symbol-name
-           (if ruby-use-encoding-map
-               (let ((elt (assq coding-system ruby-encoding-map)))
-                 (if elt (cdr elt) coding-system))
-             coding-system))
-        "ascii-8bit"))))
+          (if ruby-use-encoding-map
+              (let ((elt (assq coding-system ruby-encoding-map)))
+                (if elt (cdr elt) coding-system))
+            coding-system)
+        'ascii-8bit))))

 (defun ruby--encoding-comment-required-p ()
   (or (eq ruby-insert-encoding-magic-comment 'always-utf8)
@@ -796,7 +796,7 @@ ruby-mode-set-encoding
                    (unless (string= (match-string 2) coding-system)
                      (goto-char (match-beginning 2))
                      (delete-region (point) (match-end 2))
-                     (insert coding-system)))
+                     (insert (symbol-name coding-system))))
                   ((looking-at "\\s *#.*coding\\s *[:=]"))
                   (t (when ruby-insert-encoding-magic-comment
                        (ruby--insert-coding-comment coding-system))))






Added tag(s) patch. Request was from Stefan Kangas <stefan <at> marxist.se> to control <at> debbugs.gnu.org. (Tue, 27 Apr 2021 14:13:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48043; Package emacs. (Tue, 27 Apr 2021 15:30:02 GMT) Full text and rfc822 format available.

Message #13 received at 48043 <at> debbugs.gnu.org (full text, mbox):

From: Peter Oliver <p.d.oliver <at> mavit.org.uk>
To: Dmitry Gutov <dgutov <at> yandex.ru>
Cc: 48043 <at> debbugs.gnu.org
Subject: Re: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby
 versions
Date: Tue, 27 Apr 2021 16:29:17 +0100 (BST)
[Message part 1 (text/plain, inline)]
On Tue, 27 Apr 2021, Dmitry Gutov wrote:

> On 26.04.2021 21:28, Peter Oliver wrote:
>
>>  Two patches are attached to address this:
>>
>>  - The first patch adds a new choice to ruby-insert-encoding-magic-comment,
>>  unless-utf8, which causes the magic comment not to be inserted if the
>>  encoding is UTF-8.
>>
>>  - The second patch, perhaps more controversially, makes this the default.
>
> Both changes make sense to me.
>
> However, I've looked at the existing code and found a prior change which 
> intended for this to be more customizable already, yet had a minor bug.
>
> Please try out the following patch:

That works for me, and I think is more straightforward than my approach.  Thanks.

Attached is an additional patch which adapts the tests added in my patch for your patch.

-- 
Peter Oliver
[0001-Test-ruby-mode-set-encoding-with-a-few-different-enc.patch (text/plain, attachment)]

Reply sent to Dmitry Gutov <dgutov <at> yandex.ru>:
You have taken responsibility. (Wed, 28 Apr 2021 02:25:02 GMT) Full text and rfc822 format available.

Notification sent to Peter Oliver <p.d.oliver <at> mavit.org.uk>:
bug acknowledged by developer. (Wed, 28 Apr 2021 02:25:02 GMT) Full text and rfc822 format available.

Message #18 received at 48043-done <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Peter Oliver <p.d.oliver <at> mavit.org.uk>
Cc: 48043-done <at> debbugs.gnu.org
Subject: Re: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby
 versions
Date: Wed, 28 Apr 2021 05:23:56 +0300
Version: 28.1

On 27.04.2021 18:29, Peter Oliver wrote:

> That works for me, and I think is more straightforward than my 
> approach.  Thanks.
> 
> Attached is an additional patch which adapts the tests added in my patch 
> for your patch.

Thanks! I've pushed the change and the tests to master.

Please note that since (AFAICT) you don't have FSF copyright assignment 
on file this exhausts the allowed limit for code contributions to Emacs.

Would you like us to send you the assignment form, so that the next 
patch could be accepted without reservation?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48043; Package emacs. (Wed, 28 Apr 2021 12:00:03 GMT) Full text and rfc822 format available.

Message #21 received at 48043-done <at> debbugs.gnu.org (full text, mbox):

From: Peter Oliver <p.d.oliver <at> mavit.org.uk>
To: Dmitry Gutov <dgutov <at> yandex.ru>
Cc: 48043-done <at> debbugs.gnu.org
Subject: Re: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby
 versions
Date: Wed, 28 Apr 2021 12:59:32 +0100 (BST)
On Wed, 28 Apr 2021, Dmitry Gutov wrote:

> Please note that since (AFAICT) you don't have FSF copyright assignment on 
> file this exhausts the allowed limit for code contributions to Emacs.
>
> Would you like us to send you the assignment form, so that the next patch 
> could be accepted without reservation?

Yes please.

-- 
Peter Oliver




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48043; Package emacs. (Wed, 28 Apr 2021 12:30:02 GMT) Full text and rfc822 format available.

Message #24 received at 48043-done <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Peter Oliver <p.d.oliver <at> mavit.org.uk>
Cc: 48043-done <at> debbugs.gnu.org, dgutov <at> yandex.ru
Subject: Re: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby
 versions
Date: Wed, 28 Apr 2021 15:28:57 +0300
> Date: Wed, 28 Apr 2021 12:59:32 +0100 (BST)
> From: Peter Oliver <p.d.oliver <at> mavit.org.uk>
> Cc: 48043-done <at> debbugs.gnu.org
> 
> > Would you like us to send you the assignment form, so that the next patch 
> > could be accepted without reservation?
> 
> Yes please.

Thanks, form sent off-list.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Thu, 27 May 2021 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 23 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.