【问题标题】:suppress additional braces in emacs electric mode在 emacs 电动模式下抑制额外的大括号
【发布时间】:2009-11-19 04:25:13
【问题描述】:

我开始使用 ruby​​-electric-mode。我喜欢它,只是我习惯于自己关闭左括号(另一对对我仍然有用)。当我自己键入右括号时,如何使 emacs 抑制其他括号?我现在每次都手动删除自动插入的括号。

提前致谢, 拉古。

【问题讨论】:

    标签: ruby emacs


    【解决方案1】:

    听起来你想要的是让 } 跳转到(已经插入的)},或者只是插入一个 } 并删除之前电动模式插入的}

    这段代码应该做你想做的事,在 } 上做什么的选择由变量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)
    

    【讨论】:

    • 哇!这太棒了。我在您的代码中将 '}' 更改为 ')' (对于我的情况),它开始工作,开箱即用。非常感谢!
    • @Raghu FWIW,我使用了},因为这就是人们说“大括号”时通常的意思——见en.wikipedia.org/wiki/Bracket
    【解决方案2】:

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

    取消选中“所有内容”并仅选中要自动插入的内容。一定要“保存以备将来使用”。

    如果您决定您最喜欢自动插入,但在某些地方您想在一次按键时将其关闭,请使用 C-q (Control-q ) 在左括号/括号/大括号/引号之前禁止自动插入结束标记。

    【讨论】:

    • 谢谢克里斯,这对我有帮助。但是,就没有两全其美的方法吗?即,让emacs对我的括号进行配对,但是当我自己输入结束对时删除重复项?这将帮助我更好地在我的代码中导航,因为我可以通过键入它们来跳过 emacs 插入的字符(有点像覆盖模式,但仅适用于自动插入的字符)
    • 啊,我不清楚你想要发生什么。看起来 Trey 的回答 (stackoverflow.com/questions/1760859/…) 可能会如您所愿。
    【解决方案3】:

    遇到了同样的问题。我找到的解决方案是:

    • 使用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 包才能让这个 sn-p 工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-05
      • 2010-11-04
      • 1970-01-01
      • 2011-04-03
      • 1970-01-01
      • 1970-01-01
      • 2012-02-20
      相关资源
      最近更新 更多