【发布时间】:2009-11-19 04:25:13
【问题描述】:
我开始使用 ruby-electric-mode。我喜欢它,只是我习惯于自己关闭左括号(另一对对我仍然有用)。当我自己键入右括号时,如何使 emacs 抑制其他括号?我现在每次都手动删除自动插入的括号。
提前致谢, 拉古。
【问题讨论】:
我开始使用 ruby-electric-mode。我喜欢它,只是我习惯于自己关闭左括号(另一对对我仍然有用)。当我自己键入右括号时,如何使 emacs 抑制其他括号?我现在每次都手动删除自动插入的括号。
提前致谢, 拉古。
【问题讨论】:
听起来你想要的是让 } 跳转到(已经插入的)},或者只是插入一个 } 并删除之前电动模式插入的}。
这段代码应该做你想做的事,在 } 上做什么的选择由变量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)
【讨论】:
},因为这就是人们说“大括号”时通常的意思——见en.wikipedia.org/wiki/Bracket。
这是一个“可自定义”的设置。运行M-x customize-variable(ESCx,如果您没有 Meta 键)并自定义 ruby-electric-expand-delimiters-list。
取消选中“所有内容”并仅选中要自动插入的内容。一定要“保存以备将来使用”。
如果您决定您最喜欢自动插入,但在某些地方您想在一次按键时将其关闭,请使用 C-q (Control-q ) 在左括号/括号/大括号/引号之前禁止自动插入结束标记。
【讨论】:
遇到了同样的问题。我找到的解决方案是:
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 包才能让这个 sn-p 工作。
【讨论】: