Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

特定のメジャーモードでセミコロン入力後の改行挿入を常に無効化したい #24

Closed
kyanny opened this issue Mar 4, 2015 · 1 comment

Comments

@kyanny
Copy link

kyanny commented Mar 4, 2015

Javascript-mode (js.el) のときにセミコロンを入力後、改行が自動挿入される挙動を無効化したく、以下の設定を追加しました。

(defun my-js-mode-hook ()
  ;; Disable auto newline insertion after input semi colon (;) at javascript-mode
  ;; http://insnvlovn.blogspot.jp/2010/04/emacs-php-mode.html
  (c-toggle-auto-hungry-state -1))

(add-hook 'js-mode-hook 'my-js-mode-hook)

この状態で拡張子 .js のファイルを開くと意図通りセミコロン入力後に改行が自動挿入されないのですが、後述する Ruby 用の設定も別途定義してある状態で拡張子 .rb のファイルを開き、その後すでに .js のファイルを編集中のバッファに切り替えると、セミコロン入力後に改行が自動挿入されてしまいます。

Ruby 用の設定ではいくつかのファイルを require しているので、おそらく何かが悪さをして Javascript-mode のほうの設定に悪影響を及ぼしているのだと思うのですが、そのような不意の設定上書きを避け、 Javascript-mode のバッファでは常に改行の自動挿入を無効化したいのですが、方法はありますか?

(Ruby 用の設定を読み込まない状態では上記の問題は発生しませんでした)

;;; Ruby
;; ruby-mode.el
(autoload 'ruby-mode "ruby-mode" "Major mode for ruby files" t)
(add-to-list 'auto-mode-alist '("\\.rb$" . ruby-mode))
(add-to-list 'interpreter-mode-alist '("ruby" . ruby-mode))

(add-to-list 'auto-mode-alist '("[Rr]akefile" . ruby-mode))
(add-to-list 'auto-mode-alist '("Gemfile" . ruby-mode))
(add-to-list 'auto-mode-alist '("Procfile" . ruby-mode))
(add-to-list 'auto-mode-alist '("Guardfile" . ruby-mode))
(add-to-list 'auto-mode-alist '("Capfile" . ruby-mode))
(add-to-list 'auto-mode-alist '("config.ru" . ruby-mode))
(add-to-list 'auto-mode-alist '(".gemspec" . ruby-mode))
(add-to-list 'auto-mode-alist '(".irbrc" . ruby-mode))
(add-to-list 'auto-mode-alist '(".pryrc" . ruby-mode))
(add-to-list 'auto-mode-alist '(".rake" . ruby-mode))

;; http://blog.livedoor.jp/ooboofo3/archives/53748087.html
(require 'ruby-end)
(add-hook 'ruby-mode-hook
          '(lambda ()
             (abbrev-mode 1)
             (electric-pair-mode t)
             (electric-indent-mode t)
             (electric-layout-mode t)))

(require 'ruby-block)
(ruby-block-mode t)
(setq ruby-block-highlight-toggle t)

(require 'ruby-additional)

(setq ruby-indent-level 2)
(setq ruby-indent-tabs-mode nil)
(setq ruby-deep-indent-paren-style nil)

;; disable insert magic comment
(defun ruby-mode-set-encoding () ())

;; http://willnet.in/13
(defadvice ruby-indent-line (after unindent-closing-paren activate)
  (let ((column (current-column))
        indent offset)
    (save-excursion
      (back-to-indentation)
      (let ((state (syntax-ppss)))
        (setq offset (- column (current-column)))
        (when (and (eq (char-after) ?\))
                   (not (zerop (car state))))
          (goto-char (cadr state))
          (setq indent (current-indentation)))))
    (when indent
      (indent-line-to indent)
      (when (> offset 0) (forward-char offset)))))

(flycheck-define-checker ruby
  "A Ruby syntax checker using the standard (MRI) Ruby interpreter.

See URL `http://www.ruby-lang.org/'."
  :command ("rbenv" "exec" "ruby" "-w" "-c" source)
  :error-patterns
  ;; These patterns support output from JRuby, too, to deal with RVM or Rbenv
  ((error line-start
          "SyntaxError in " (file-name) ":" line ": " (message)
          line-end)
   (warning line-start
            (file-name) ":" line ":" (optional column ":")
            " warning: " (message) line-end)
   (error line-start (file-name) ":" line ": " (message) line-end))
  :modes 'ruby-mode)
(add-hook 'ruby-mode-hook 'flycheck-mode)
@kyanny
Copy link
Author

kyanny commented Mar 4, 2015

すいません、自己解決しました。

Ruby 用の設定で ruby-mode-hook のなかで (electric-layout-mode t) しているが、 js.el のなかで electric-layout-rules が定義されており、そのルールの中に「セミコロンの後」というのがあるため、ruby-mode でファイルを開くと electric-layout-mode が有効になり、もともと Javascript-mode 用に定義されていたルールが発動する、という流れでした。
https://github.com/emacs-mirror/emacs/blob/master/lisp/progmodes/js.el#L3469-L3470

electric-layout-mode は electric-indent-mode とちがいバッファローカルに有効にする亜種はないようなので、 Javascript-mode のフックで electric-layout-rules 変数を上書きし、「セミコロンの後」というルールを消すことで対応しました。

(defun my-js-mode-hook ()
  ;; Disable auto newline insertion after input semi colon (;) at javascript-mode
  ;; http://insnvlovn.blogspot.jp/2010/04/emacs-php-mode.html
  (setq-local electric-layout-rules
              '((?\{ . after) (?\} . before))))

@kyanny kyanny closed this as completed Mar 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant