在emacs电动模式中抑制额外的括号

时间:2009-11-19 04:25:13

标签: ruby emacs

我开始使用ruby-electric-mode。我喜欢它,除了我习惯于自己关闭开括号(其他配对对我来说仍然有用)。当我自己键入结束括号时,如何使emacs抑制其他括号?我现在每次都手动删除自动插入的支架。

提前致谢, Raghu。

3 个答案:

答案 0 :(得分:3)

听起来你想要的是} 要么跳转到(已插入)} ,要么简单地插入} 并删除之前由电子模式插入的}

此代码应该按照您的意愿执行,对} 的操作选择由变量my-ruby-close-brace-goto-close切换。

;; assuming
;; (require 'ruby)
;; (require 'ruby-electric)
(defvar my-ruby-close-brace-goto-close t
  "Non-nill indicates to move point to the next }, otherwise insert } 
and delete the following }.")

(defun my-ruby-close-brace ()
  "replacement for ruby-electric-brace for the close brace"
  (interactive)
  (let ((p (point)))
    (if my-ruby-close-brace-goto-close
        (unless (search-forward "}" nil t)
          (message "No close brace found")
          (insert "}"))
      (insert "}")
      (save-excursion (if (search-forward "}" nil t)
                           (delete-char -1))))))
(define-key ruby-mode-map "}" 'my-ruby-close-brace)

答案 1 :(得分:2)

这是一个“可自定义”的设置。如果您没有Meta键,请运行M-x customize-variable ESC x )并自定义ruby-electric-expand-delimiters-list

取消选中“Everything”并仅检查您要自动插入的内容。一定要“为将来的会议保存”。

如果您认为自己最喜欢自动插入,但有些地方想要将其关闭以进行单次击键,那么请使用C-q Control - q )在打开paren / bracket / brace / quote之前抑制自动插入结束标记。

答案 2 :(得分:0)

陷入同样的​​问题。我找到的解决方案是:

  • 使用autopair,它完全符合您的要求。确保安装它。
  • 启用ruby-electric-mode但仅适用于|,因为其余部分已经处理完毕。

这导致.emacs文件中的以下代码:

(use-package autopair
  :config (autopair-global-mode)
  )
(use-package ruby-electric-mode
  :init
  (setq ruby-electric-expand-delimiters-list (quote (124)))
  )
(add-hook 'ruby-mode-hook 'ruby-electric-mode)

此代码使用use-package包,请确保您安装了它(M-X list-packages,然后在该行上找到use-package,然后找到i,然后x和重启emacs)。

此外,访问此帖子的人可能会感兴趣。我添加了此代码以跳过使用TAB的结束分隔符,它有助于跳过它们。注释掉while行(并调整)),使TAB跳过所有结束分隔符(取自emacs board discussion):

(use-package bind-key)

(defun special-tab ()
 "Wrapper for tab key invocation. 

  If point is just before a close delimiter, skip forward until 
  there is no closed delimiter in front of point.  Otherwise, invoke
  normal tab command for current mode.

  Must be bound to <tab> using bind-key* macro from bind-key package.

  Note, this function will not be called if `override-global-mode' is
  turned off."
  (interactive)
  (defun next-char-delims (delims)
    (let ((char (and (not (equal (point) (point-max)))
                     (string (char-after (point))))))
      (when char (member char delims))))
  (let ((open '("'" "\"" ")" "]" "}" "|")))
    (if (next-char-delims open)
        (progn (forward-char 1))
               ;;(while (next-char-delims open)
               ;;  (forward-char 1)))
      (call-interactively (key-binding (kbd "TAB"))))))

(if (macrop 'bind-key*)        
    (bind-key* (kbd "<tab>") 'special-tab)
  (user-error "Must have bind-key from use-package.el to use special-tab function"))

这一次,您需要此代码段的bind-key包才能生效。

相关问题