unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Gutov <dgutov@yandex.ru>
To: Peter Oliver <p.d.oliver@mavit.org.uk>, 48043@debbugs.gnu.org
Subject: bug#48043: UTF-8 magic comment is unwelcome with recent Ruby versions
Date: Tue, 27 Apr 2021 00:04:58 +0300	[thread overview]
Message-ID: <5a8e33ff-2ca9-7125-2fac-1874a9f69900@yandex.ru> (raw)
In-Reply-To: <dc8a3f5b-83c9-3759-f63c-1bd37a83a788@froglet.home.mavit.org.uk>

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))))







  reply	other threads:[~2021-04-26 21:04 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-26 18:28 bug#48043: UTF-8 magic comment is unwelcome with recent Ruby versions Peter Oliver
2021-04-26 21:04 ` Dmitry Gutov [this message]
2021-04-27 15:29   ` Peter Oliver
2021-04-28  2:23     ` Dmitry Gutov
2021-04-28 11:59       ` Peter Oliver
2021-04-28 12:28         ` Eli Zaretskii

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=5a8e33ff-2ca9-7125-2fac-1874a9f69900@yandex.ru \
    --to=dgutov@yandex.ru \
    --cc=48043@debbugs.gnu.org \
    --cc=p.d.oliver@mavit.org.uk \
    /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).