all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Bozhidar Batsov <bozhidar@batsov.com>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: Re: Subject: [PATCH] * lisp/progmodes/ruby-mode.el: Set `ruby-insert-encoding-magic-comment' to nil by default.
Date: Wed, 13 Nov 2013 16:36:43 +0200	[thread overview]
Message-ID: <CAM9Zgm1kvx=4q6tSygaBu9ZB+0zeuw1rcjkxiSvoLiFb+XKnOg@mail.gmail.com> (raw)
In-Reply-To: <527B7433.1070101@yandex.ru>


[-- Attachment #1.1: Type: text/plain, Size: 909 bytes --]

I've attached here a second patch that adds support for always inserting a
utf-8 encoding comment. I've also simplified a bit the code of
`ruby-mode-set-encoding' (by breaking it into several functions) as it was
quite convoluted.


On 7 November 2013 13:06, Dmitry Gutov <dgutov@yandex.ru> wrote:

> On 07.11.2013 12:23, Bozhidar Batsov wrote:
>
>> Btw, Dimitry - please add some info the ruby-mode.el about running the
>> tests. I tried evaluating and running them manually, but this crashed my
>> Emacs (the tests hogged my CPU completely and I had to kill Emacs).
>> Maybe there is something wrong with them right now, or perhaps they
>> should invoked in some manner I'm not aware of.
>>
>
> I'm not aware of any problems running the tests, looks like a bug. Weren't
> you able to stop the freezing with C-g, at least?
>
> I just open ruby-mode-tests.el, M-x eval-buffer, M-x ert RET.
> They run fine.
>

[-- Attachment #1.2: Type: text/html, Size: 1395 bytes --]

[-- Attachment #2: 0001-lisp-progmodes-ruby-mode.el-ruby-mode-set-encoding.patch --]
[-- Type: application/octet-stream, Size: 4700 bytes --]

From 1768305926bf60084a42ffeca913be32f6ee288e Mon Sep 17 00:00:00 2001
From: Bozhidar Batsov <bozhidar@batsov.com>
Date: Wed, 13 Nov 2013 16:31:59 +0200
Subject: [PATCH] * lisp/progmodes/ruby-mode.el (ruby-mode-set-encoding):

Add support for always inserting an utf-8 encoding comment.
---
 lisp/progmodes/ruby-mode.el | 68 ++++++++++++++++++++++++++++-----------------
 1 file changed, 42 insertions(+), 26 deletions(-)

diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el
index 87454cd..1e8faf0 100644
--- a/lisp/progmodes/ruby-mode.el
+++ b/lisp/progmodes/ruby-mode.el
@@ -256,7 +256,12 @@ explicitly declared in magic comment."
   :group 'ruby)
 
 (defcustom ruby-insert-encoding-magic-comment t
-  "Insert a magic Emacs 'coding' comment upon save if this is non-nil."
+  "Insert a magic Emacs 'coding' comment upon save if this is non-nil.
+The encoding will be auto-detected.  The format of the encoding comment
+is customizable via `ruby-encoding-magic-comment-style'.
+
+When set to `always-utf8' an utf-8 comment will always be added, even if it's not
+required."
   :type 'boolean :group 'ruby)
 
 (defcustom ruby-encoding-magic-comment-style 'ruby
@@ -633,28 +638,47 @@ explicitly declared in magic comment."
   (setq-local paragraph-separate paragraph-start)
   (setq-local paragraph-ignore-fill-prefix t))
 
+(defun ruby--insert-coding-comment (encoding)
+  (let ((encoding-magic-comment-template
+         (pcase ruby-encoding-magic-comment-style
+           (`ruby "# coding: %s")
+           (`emacs "# -*- coding: %s -*-")
+           (`custom
+            ruby-custom-encoding-magic-comment-template))))
+    (insert
+     (format encoding-magic-comment-template encoding)
+     "\n")))
+
+(defun ruby--detect-encoding ()
+  (if (eq ruby-insert-encoding-magic-comment 'always-utf8)
+      "utf-8"
+    (let ((coding-system
+           (or save-buffer-coding-system
+               buffer-file-coding-system)))
+      (if coding-system
+          (setq coding-system
+                (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"))))
+
+(defun ruby--encoding-comment-required-p ()
+  (or (re-search-forward "[^\0-\177]" nil t)
+      (eq ruby-insert-encoding-magic-comment 'always-utf8)))
+
 (defun ruby-mode-set-encoding ()
   "Insert a magic comment header with the proper encoding if necessary."
   (save-excursion
     (widen)
     (goto-char (point-min))
-    (when (re-search-forward "[^\0-\177]" nil t)
+    (when (ruby--encoding-comment-required-p)
       (goto-char (point-min))
-      (let ((coding-system
-             (or save-buffer-coding-system
-                 buffer-file-coding-system)))
-        (if coding-system
-            (setq coding-system
-                  (or (coding-system-get coding-system 'mime-charset)
-                      (coding-system-change-eol-conversion coding-system nil))))
-        (setq coding-system
-              (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"))
+      (let ((coding-system (ruby--detect-encoding)))
         (when coding-system
           (if (looking-at "^#!") (beginning-of-line 2))
           (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
@@ -669,15 +693,7 @@ explicitly declared in magic comment."
                    (insert coding-system)))
                 ((looking-at "\\s *#.*coding\\s *[:=]"))
                 (t (when ruby-insert-encoding-magic-comment
-                     (let ((encoding-magic-comment-template
-                            (pcase ruby-encoding-magic-comment-style
-                              (`ruby "# coding: %s")
-                              (`emacs "# -*- coding: %s -*-")
-                              (`custom
-                               ruby-custom-encoding-magic-comment-template))))
-                       (insert
-                        (format encoding-magic-comment-template coding-system)
-                        "\n")))))
+                     (ruby--insert-coding-comment coding-system))))
           (when (buffer-modified-p)
             (basic-save-buffer-1)))))))
 
-- 
1.8.4


  reply	other threads:[~2013-11-13 14:36 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-11-01 12:59 Subject: [PATCH] * lisp/progmodes/ruby-mode.el: Set `ruby-insert-encoding-magic-comment' to nil by default Bozhidar Batsov
2013-11-01 17:46 ` Stefan Monnier
2013-11-01 18:51   ` Bozhidar Batsov
2013-11-02  0:00 ` Dmitry Gutov
2013-11-02 10:38   ` Bozhidar Batsov
2013-11-02 20:15     ` Dmitry Gutov
2013-11-03  7:35       ` Bozhidar Batsov
2013-11-03  9:22         ` Dmitry Gutov
2013-11-04 13:40           ` Bozhidar Batsov
2013-11-06 17:10             ` Bozhidar Batsov
2013-11-06 21:45               ` Dmitry Gutov
2013-11-07 10:23                 ` Bozhidar Batsov
2013-11-07 11:06                   ` Dmitry Gutov
2013-11-13 14:36                     ` Bozhidar Batsov [this message]
2013-11-13 19:19                       ` Dmitry Gutov
2013-11-14  9:53                         ` Bozhidar Batsov
2013-11-14 13:23                           ` Dmitry Gutov

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

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAM9Zgm1kvx=4q6tSygaBu9ZB+0zeuw1rcjkxiSvoLiFb+XKnOg@mail.gmail.com' \
    --to=bozhidar@batsov.com \
    --cc=dgutov@yandex.ru \
    --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 external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.