emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] trunk r115101: * lisp/progmodes/ruby-mode.el (ruby-mode-se


From: Bozhidar Batsov
Subject: [Emacs-diffs] trunk r115101: * lisp/progmodes/ruby-mode.el (ruby-mode-set-encoding):
Date: Thu, 14 Nov 2013 10:37:02 +0000
User-agent: Bazaar (2.6b2)

------------------------------------------------------------
revno: 115101
revision-id: address@hidden
parent: address@hidden
committer: Bozhidar Batsov <address@hidden>
branch nick: master
timestamp: Thu 2013-11-14 12:35:49 +0200
message:
  * lisp/progmodes/ruby-mode.el (ruby-mode-set-encoding):
  Add support for always inserting an utf-8 encoding comment.
modified:
  lisp/ChangeLog                 changelog-20091113204419-o5vbwnq5f7feedwu-1432
  lisp/progmodes/ruby-mode.el    
rubymode.el-20091113204419-o5vbwnq5f7feedwu-8804
=== modified file 'lisp/ChangeLog'
--- a/lisp/ChangeLog    2013-11-14 07:36:39 +0000
+++ b/lisp/ChangeLog    2013-11-14 10:35:49 +0000
@@ -1,3 +1,8 @@
+2013-11-14  Bozhidar Batsov  <address@hidden>
+
+       * progmodes/ruby-mode.el (ruby-mode-set-encoding):
+       Add the ability to always insert an utf-8 encoding comment.
+
 2013-11-14  Michael Albinus  <address@hidden>
 
        * net/tramp-gvfs.el (top): Run init code only when

=== modified file 'lisp/progmodes/ruby-mode.el'
--- a/lisp/progmodes/ruby-mode.el       2013-11-12 14:15:14 +0000
+++ b/lisp/progmodes/ruby-mode.el       2013-11-14 10:35:49 +0000
@@ -256,7 +256,12 @@
   :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 Ruby encoding 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,49 @@
   (setq-local paragraph-separate paragraph-start)
   (setq-local paragraph-ignore-fill-prefix t))
 
+(defun ruby--insert-coding-comment (encoding)
+  "Insert a magic coding comment for ENCODING.
+The style of the comment is controlled by `ruby-encoding-magic-comment-style'."
+  (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 (eq ruby-insert-encoding-magic-comment 'always-utf8)
+      (re-search-forward "[^\0-\177]" nil t)))
+
 (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 +695,7 @@
                    (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)))))))
 


reply via email to

[Prev in Thread] Current Thread [Next in Thread]