mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
misc/ruby-mode.el: Improve ruby-mode-set-encoding
.
* misc/ruby-additional.el (ruby-mode-set-encoding): Add support for `prefer-utf-8` which was introduced in Emacs trunk. * misc/ruby-additional.el (ruby-encoding-map): Add a mapping from `japanese-cp932` to `cp932` to fix the problem where saving a source file written in Shift_JIS twice would end up having `coding: japanese-cp932` which Ruby could not recognize. * misc/ruby-additional.el (ruby-mode-set-encoding): Add support for encodings mapped to nil in `ruby-encoding-map`. * misc/ruby-additional.el (ruby-encoding-map): Map `us-ascii` and `utf-8` to nil by default, meaning they need not be explicitly declared in magic comment. * misc/ruby-additional.el (ruby-encoding-map): Add type declaration for better customize UI. * misc/ruby-mode.el: Ditto for the above. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@43186 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
381784be7e
commit
1ecf20cda2
3 changed files with 147 additions and 72 deletions
22
ChangeLog
22
ChangeLog
|
@ -1,3 +1,25 @@
|
||||||
|
Tue Oct 8 03:19:19 2013 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
|
* misc/ruby-additional.el (ruby-mode-set-encoding): Add support
|
||||||
|
for `prefer-utf-8` which was introduced in Emacs trunk.
|
||||||
|
|
||||||
|
* misc/ruby-additional.el (ruby-encoding-map): Add a mapping from
|
||||||
|
`japanese-cp932` to `cp932` to fix the problem where saving a
|
||||||
|
source file written in Shift_JIS twice would end up having
|
||||||
|
`coding: japanese-cp932` which Ruby could not recognize.
|
||||||
|
|
||||||
|
* misc/ruby-additional.el (ruby-mode-set-encoding): Add support
|
||||||
|
for encodings mapped to nil in `ruby-encoding-map`.
|
||||||
|
|
||||||
|
* misc/ruby-additional.el (ruby-encoding-map): Map `us-ascii` and
|
||||||
|
`utf-8` to nil by default, meaning they need not be explicitly
|
||||||
|
declared in magic comment.
|
||||||
|
|
||||||
|
* misc/ruby-additional.el (ruby-encoding-map): Add type
|
||||||
|
declaration for better customize UI.
|
||||||
|
|
||||||
|
* misc/ruby-mode.el: Ditto for the above.
|
||||||
|
|
||||||
Tue Oct 8 00:14:53 2013 Akinori MUSHA <knu@iDaemons.org>
|
Tue Oct 8 00:14:53 2013 Akinori MUSHA <knu@iDaemons.org>
|
||||||
|
|
||||||
* misc/ruby-additional.el: Add a standard header and footer,
|
* misc/ruby-additional.el: Add a standard header and footer,
|
||||||
|
|
|
@ -73,31 +73,57 @@
|
||||||
(or (ruby-brace-to-do-end)
|
(or (ruby-brace-to-do-end)
|
||||||
(ruby-do-end-to-brace)))
|
(ruby-do-end-to-brace)))
|
||||||
|
|
||||||
|
(defcustom ruby-encoding-map
|
||||||
|
'((us-ascii . nil) ;; Do not put coding: us-ascii
|
||||||
|
(utf-8 . nil) ;; Do not put coding: utf-8
|
||||||
|
(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
|
||||||
|
"Alist to map encoding name from Emacs to Ruby.
|
||||||
|
Associating an encoding name with nil means it needs not be
|
||||||
|
explicitly declared in magic comment."
|
||||||
|
:type '(repeat (cons (symbol :tag "From") (symbol :tag "To")))
|
||||||
|
:group 'ruby)
|
||||||
|
|
||||||
(defun ruby-mode-set-encoding ()
|
(defun ruby-mode-set-encoding ()
|
||||||
"Insert a magic comment header with the proper encoding always.
|
"Insert or update a magic comment header with the proper encoding.
|
||||||
Now encoding needs to be set always explicitly actually."
|
`ruby-encoding-map' is looked up to convert an encoding name from
|
||||||
|
Emacs to Ruby."
|
||||||
|
(let* ((nonascii
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(let ((coding-system))
|
|
||||||
(widen)
|
(widen)
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(if (re-search-forward "[^\0-\177]" nil t)
|
(re-search-forward "[^\0-\177]" nil t)))
|
||||||
(progn
|
(coding-system
|
||||||
(goto-char (point-min))
|
|
||||||
(setq coding-system
|
|
||||||
(or coding-system-for-write
|
(or coding-system-for-write
|
||||||
buffer-file-coding-system))
|
buffer-file-coding-system))
|
||||||
|
(coding-system
|
||||||
|
(and coding-system
|
||||||
|
(coding-system-change-eol-conversion coding-system nil)))
|
||||||
|
(coding-system
|
||||||
|
(and coding-system
|
||||||
|
(or
|
||||||
|
(coding-system-get coding-system :mime-charset)
|
||||||
|
(let ((coding-type (coding-system-get coding-system :coding-type)))
|
||||||
|
(cond ((eq coding-type 'undecided)
|
||||||
|
(if nonascii
|
||||||
|
(if (coding-system-get coding-system :prefer-utf-8)
|
||||||
|
'utf-8 'ascii-8bit)))
|
||||||
|
((memq coding-type '(utf-8 shift-jis))
|
||||||
|
coding-type))))))
|
||||||
|
(coding-system
|
||||||
|
(or coding-system
|
||||||
|
'us-ascii))
|
||||||
|
(coding-system
|
||||||
|
(let ((cons (assq coding-system ruby-encoding-map)))
|
||||||
|
(if cons (cdr cons) coding-system)))
|
||||||
|
(coding-system
|
||||||
|
(and coding-system
|
||||||
|
(symbol-name coding-system))))
|
||||||
(if coding-system
|
(if coding-system
|
||||||
(setq coding-system
|
(save-excursion
|
||||||
(or (coding-system-get coding-system 'mime-charset)
|
(widen)
|
||||||
(coding-system-change-eol-conversion coding-system nil))))
|
(goto-char (point-min))
|
||||||
(setq coding-system
|
|
||||||
(if coding-system
|
|
||||||
(symbol-name
|
|
||||||
(or (and ruby-use-encoding-map
|
|
||||||
(cdr (assq coding-system ruby-encoding-map)))
|
|
||||||
coding-system))
|
|
||||||
"ascii-8bit")))
|
|
||||||
(setq coding-system "us-ascii"))
|
|
||||||
(if (looking-at "^#!") (beginning-of-line 2))
|
(if (looking-at "^#!") (beginning-of-line 2))
|
||||||
(cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
|
(cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
|
||||||
(unless (string= (match-string 2) coding-system)
|
(unless (string= (match-string 2) coding-system)
|
||||||
|
@ -111,7 +137,7 @@ Now encoding needs to be set always explicitly actually."
|
||||||
(insert coding-system)))
|
(insert coding-system)))
|
||||||
((looking-at "\\s *#.*coding\\s *[:=]"))
|
((looking-at "\\s *#.*coding\\s *[:=]"))
|
||||||
(t (when ruby-insert-encoding-magic-comment
|
(t (when ruby-insert-encoding-magic-comment
|
||||||
(insert "# -*- coding: " coding-system " -*-\n")))))))
|
(insert "# -*- coding: " coding-system " -*-\n"))))))))
|
||||||
|
|
||||||
))
|
))
|
||||||
|
|
||||||
|
|
|
@ -240,8 +240,16 @@ Also ignores spaces after parenthesis when 'space."
|
||||||
"Default deep indent style."
|
"Default deep indent style."
|
||||||
:options '(t nil space) :group 'ruby)
|
:options '(t nil space) :group 'ruby)
|
||||||
|
|
||||||
(defcustom ruby-encoding-map '((shift_jis . cp932) (shift-jis . cp932))
|
(defcustom ruby-encoding-map
|
||||||
"Alist to map encoding name from emacs to ruby."
|
'((us-ascii . nil) ;; Do not put coding: us-ascii
|
||||||
|
(utf-8 . nil) ;; Do not put coding: utf-8
|
||||||
|
(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
|
||||||
|
"Alist to map encoding name from Emacs to Ruby.
|
||||||
|
Associating an encoding name with nil means it needs not be
|
||||||
|
explicitly declared in magic comment."
|
||||||
|
:type '(repeat (cons (symbol :tag "From") (symbol :tag "To")))
|
||||||
:group 'ruby)
|
:group 'ruby)
|
||||||
|
|
||||||
(defcustom ruby-use-encoding-map t
|
(defcustom ruby-use-encoding-map t
|
||||||
|
@ -326,25 +334,44 @@ Also ignores spaces after parenthesis when 'space."
|
||||||
(setq paragraph-ignore-fill-prefix t))
|
(setq paragraph-ignore-fill-prefix t))
|
||||||
|
|
||||||
(defun ruby-mode-set-encoding ()
|
(defun ruby-mode-set-encoding ()
|
||||||
|
"Insert or update a magic comment header with the proper encoding.
|
||||||
|
`ruby-encoding-map' is looked up to convert an encoding name from
|
||||||
|
Emacs to Ruby."
|
||||||
|
(let* ((nonascii
|
||||||
(save-excursion
|
(save-excursion
|
||||||
(widen)
|
(widen)
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(when (re-search-forward "[^\0-\177]" nil t)
|
(re-search-forward "[^\0-\177]" nil t)))
|
||||||
(goto-char (point-min))
|
(coding-system
|
||||||
(let ((coding-system
|
|
||||||
(or coding-system-for-write
|
(or coding-system-for-write
|
||||||
buffer-file-coding-system)))
|
buffer-file-coding-system))
|
||||||
|
(coding-system
|
||||||
|
(and coding-system
|
||||||
|
(coding-system-change-eol-conversion coding-system nil)))
|
||||||
|
(coding-system
|
||||||
|
(and coding-system
|
||||||
|
(or
|
||||||
|
(coding-system-get coding-system :mime-charset)
|
||||||
|
(let ((coding-type (coding-system-get coding-system :coding-type)))
|
||||||
|
(cond ((eq coding-type 'undecided)
|
||||||
|
(if nonascii
|
||||||
|
(if (coding-system-get coding-system :prefer-utf-8)
|
||||||
|
'utf-8 'ascii-8bit)))
|
||||||
|
((memq coding-type '(utf-8 shift-jis))
|
||||||
|
coding-type))))))
|
||||||
|
(coding-system
|
||||||
|
(or coding-system
|
||||||
|
'us-ascii))
|
||||||
|
(coding-system
|
||||||
|
(let ((cons (assq coding-system ruby-encoding-map)))
|
||||||
|
(if cons (cdr cons) coding-system)))
|
||||||
|
(coding-system
|
||||||
|
(and coding-system
|
||||||
|
(symbol-name coding-system))))
|
||||||
(if coding-system
|
(if coding-system
|
||||||
(setq coding-system
|
(save-excursion
|
||||||
(or (coding-system-get coding-system 'mime-charset)
|
(widen)
|
||||||
(coding-system-change-eol-conversion coding-system nil))))
|
(goto-char (point-min))
|
||||||
(setq coding-system
|
|
||||||
(if coding-system
|
|
||||||
(symbol-name
|
|
||||||
(or (and ruby-use-encoding-map
|
|
||||||
(cdr (assq coding-system ruby-encoding-map)))
|
|
||||||
coding-system))
|
|
||||||
"ascii-8bit"))
|
|
||||||
(if (looking-at "^#!") (beginning-of-line 2))
|
(if (looking-at "^#!") (beginning-of-line 2))
|
||||||
(cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
|
(cond ((looking-at "\\s *#.*-\*-\\s *\\(en\\)?coding\\s *:\\s *\\([-a-z0-9_]*\\)\\s *\\(;\\|-\*-\\)")
|
||||||
(unless (string= (match-string 2) coding-system)
|
(unless (string= (match-string 2) coding-system)
|
||||||
|
@ -357,8 +384,8 @@ Also ignores spaces after parenthesis when 'space."
|
||||||
((forward-char)))))
|
((forward-char)))))
|
||||||
(insert coding-system)))
|
(insert coding-system)))
|
||||||
((looking-at "\\s *#.*coding\\s *[:=]"))
|
((looking-at "\\s *#.*coding\\s *[:=]"))
|
||||||
(t (insert "# -*- coding: " coding-system " -*-\n"))
|
(t (when ruby-insert-encoding-magic-comment
|
||||||
)))))
|
(insert "# -*- coding: " coding-system " -*-\n"))))))))
|
||||||
|
|
||||||
(defun ruby-current-indentation ()
|
(defun ruby-current-indentation ()
|
||||||
(save-excursion
|
(save-excursion
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue