diff options
| author | Bozhidar Batsov | 2013-11-14 12:35:49 +0200 |
|---|---|---|
| committer | Bozhidar Batsov | 2013-11-14 12:35:49 +0200 |
| commit | 6c1bf08604c4820137bf6e5948b8c00b0d7d5aeb (patch) | |
| tree | 590df21e0228bb713ad882a686b9bdecb3e2477d | |
| parent | 90794abbf92f3cb206e69f054928847044427220 (diff) | |
| download | emacs-6c1bf08604c4820137bf6e5948b8c00b0d7d5aeb.tar.gz emacs-6c1bf08604c4820137bf6e5948b8c00b0d7d5aeb.zip | |
* lisp/progmodes/ruby-mode.el (ruby-mode-set-encoding):
Add support for always inserting an utf-8 encoding comment.
| -rw-r--r-- | lisp/ChangeLog | 5 | ||||
| -rw-r--r-- | lisp/progmodes/ruby-mode.el | 70 |
2 files changed, 49 insertions, 26 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 55d12e69fc0..e0b700a14ba 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,8 @@ | |||
| 1 | 2013-11-14 Bozhidar Batsov <bozhidar@batsov.com> | ||
| 2 | |||
| 3 | * progmodes/ruby-mode.el (ruby-mode-set-encoding): | ||
| 4 | Add the ability to always insert an utf-8 encoding comment. | ||
| 5 | |||
| 1 | 2013-11-14 Michael Albinus <michael.albinus@gmx.de> | 6 | 2013-11-14 Michael Albinus <michael.albinus@gmx.de> |
| 2 | 7 | ||
| 3 | * net/tramp-gvfs.el (top): Run init code only when | 8 | * net/tramp-gvfs.el (top): Run init code only when |
diff --git a/lisp/progmodes/ruby-mode.el b/lisp/progmodes/ruby-mode.el index 87454cdbbb5..8051a759410 100644 --- a/lisp/progmodes/ruby-mode.el +++ b/lisp/progmodes/ruby-mode.el | |||
| @@ -256,7 +256,12 @@ explicitly declared in magic comment." | |||
| 256 | :group 'ruby) | 256 | :group 'ruby) |
| 257 | 257 | ||
| 258 | (defcustom ruby-insert-encoding-magic-comment t | 258 | (defcustom ruby-insert-encoding-magic-comment t |
| 259 | "Insert a magic Emacs 'coding' comment upon save if this is non-nil." | 259 | "Insert a magic Ruby encoding comment upon save if this is non-nil. |
| 260 | The encoding will be auto-detected. The format of the encoding comment | ||
| 261 | is customizable via `ruby-encoding-magic-comment-style'. | ||
| 262 | |||
| 263 | When set to `always-utf8' an utf-8 comment will always be added, | ||
| 264 | even if it's not required." | ||
| 260 | :type 'boolean :group 'ruby) | 265 | :type 'boolean :group 'ruby) |
| 261 | 266 | ||
| 262 | (defcustom ruby-encoding-magic-comment-style 'ruby | 267 | (defcustom ruby-encoding-magic-comment-style 'ruby |
| @@ -633,28 +638,49 @@ explicitly declared in magic comment." | |||
| 633 | (setq-local paragraph-separate paragraph-start) | 638 | (setq-local paragraph-separate paragraph-start) |
| 634 | (setq-local paragraph-ignore-fill-prefix t)) | 639 | (setq-local paragraph-ignore-fill-prefix t)) |
| 635 | 640 | ||
| 641 | (defun ruby--insert-coding-comment (encoding) | ||
| 642 | "Insert a magic coding comment for ENCODING. | ||
| 643 | The style of the comment is controlled by `ruby-encoding-magic-comment-style'." | ||
| 644 | (let ((encoding-magic-comment-template | ||
| 645 | (pcase ruby-encoding-magic-comment-style | ||
| 646 | (`ruby "# coding: %s") | ||
| 647 | (`emacs "# -*- coding: %s -*-") | ||
| 648 | (`custom | ||
| 649 | ruby-custom-encoding-magic-comment-template)))) | ||
| 650 | (insert | ||
| 651 | (format encoding-magic-comment-template encoding) | ||
| 652 | "\n"))) | ||
| 653 | |||
| 654 | (defun ruby--detect-encoding () | ||
| 655 | (if (eq ruby-insert-encoding-magic-comment 'always-utf8) | ||
| 656 | "utf-8" | ||
| 657 | (let ((coding-system | ||
| 658 | (or save-buffer-coding-system | ||
| 659 | buffer-file-coding-system))) | ||
| 660 | (if coding-system | ||
| 661 | (setq coding-system | ||
| 662 | (or (coding-system-get coding-system 'mime-charset) | ||
| 663 | (coding-system-change-eol-conversion coding-system nil)))) | ||
| 664 | (if coding-system | ||
| 665 | (symbol-name | ||
| 666 | (if ruby-use-encoding-map | ||
| 667 | (let ((elt (assq coding-system ruby-encoding-map))) | ||
| 668 | (if elt (cdr elt) coding-system)) | ||
| 669 | coding-system)) | ||
| 670 | "ascii-8bit")))) | ||
| 671 | |||
| 672 | (defun ruby--encoding-comment-required-p () | ||
| 673 | (or (eq ruby-insert-encoding-magic-comment 'always-utf8) | ||
| 674 | (re-search-forward "[^\0-\177]" nil t))) | ||
| 675 | |||
| 636 | (defun ruby-mode-set-encoding () | 676 | (defun ruby-mode-set-encoding () |
| 637 | "Insert a magic comment header with the proper encoding if necessary." | 677 | "Insert a magic comment header with the proper encoding if necessary." |
| 638 | (save-excursion | 678 | (save-excursion |
| 639 | (widen) | 679 | (widen) |
| 640 | (goto-char (point-min)) | 680 | (goto-char (point-min)) |
| 641 | (when (re-search-forward "[^\0-\177]" nil t) | 681 | (when (ruby--encoding-comment-required-p) |
| 642 | (goto-char (point-min)) | 682 | (goto-char (point-min)) |
| 643 | (let ((coding-system | 683 | (let ((coding-system (ruby--detect-encoding))) |
| 644 | (or save-buffer-coding-system | ||
| 645 | buffer-file-coding-system))) | ||
| 646 | (if coding-system | ||
| 647 | (setq coding-system | ||
| 648 | (or (coding-system-get coding-system 'mime-charset) | ||
| 649 | (coding-system-change-eol-conversion coding-system nil)))) | ||
| 650 | (setq coding-system | ||
| 651 | (if coding-system | ||
| 652 | (symbol-name | ||
| 653 | (if ruby-use-encoding-map | ||
| 654 | (let ((elt (assq coding-system ruby-encoding-map))) | ||
| 655 | (if elt (cdr elt) coding-system)) | ||
| 656 | coding-system)) | ||
| 657 | "ascii-8bit")) | ||
| 658 | (when coding-system | 684 | (when coding-system |
| 659 | (if (looking-at "^#!") (beginning-of-line 2)) | 685 | (if (looking-at "^#!") (beginning-of-line 2)) |
| 660 | (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)") | 686 | (cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)") |
| @@ -669,15 +695,7 @@ explicitly declared in magic comment." | |||
| 669 | (insert coding-system))) | 695 | (insert coding-system))) |
| 670 | ((looking-at "\\s *#.*coding\\s *[:=]")) | 696 | ((looking-at "\\s *#.*coding\\s *[:=]")) |
| 671 | (t (when ruby-insert-encoding-magic-comment | 697 | (t (when ruby-insert-encoding-magic-comment |
| 672 | (let ((encoding-magic-comment-template | 698 | (ruby--insert-coding-comment coding-system)))) |
| 673 | (pcase ruby-encoding-magic-comment-style | ||
| 674 | (`ruby "# coding: %s") | ||
| 675 | (`emacs "# -*- coding: %s -*-") | ||
| 676 | (`custom | ||
| 677 | ruby-custom-encoding-magic-comment-template)))) | ||
| 678 | (insert | ||
| 679 | (format encoding-magic-comment-template coding-system) | ||
| 680 | "\n"))))) | ||
| 681 | (when (buffer-modified-p) | 699 | (when (buffer-modified-p) |
| 682 | (basic-save-buffer-1))))))) | 700 | (basic-save-buffer-1))))))) |
| 683 | 701 | ||