1

我是 emacs 的新手,使用 emacs 24 并尝试将 Cc Cc 绑定到一个函数以注释掉一行。我的 init.el 文件中有以下内容,但它似乎不适用于 c++。

(defun toggle-comment-on-line ()
  "comment or uncomment current line"
  (interactive)
  (comment-or-uncomment-region (line-beginning-position) (line-end-position))
  (next-line))

 (global-set-key (kbd "C-c C-c") 'toggle-comment-on-line)

当我在草稿页中玩耍时,它工作正常,当我检查C-h k C-c C-c它时会显示正确的功能,但是当我在 C++ 中时,相同的命令会显示文本:

C-c C-c runs the command comment-region, which is an interactive
compiled Lisp function in `newcomment.el'.

It is bound to C-c C-c, <menu-bar> <C++> <Comment Out Region>.

(comment-region BEG END &optional ARG)

Comment or uncomment each line in the region.
With just C-u prefix arg, uncomment each line in region BEG .. END.
Numeric prefix ARG means use ARG comment characters.
If ARG is negative, delete that many comment characters instead.

The strings used as comment starts are built from `comment-start'
and `comment-padding'; the strings used as comment ends are built
from `comment-end' and `comment-padding'.

By default, the `comment-start' markers are inserted at the
current indentation of the region, and comments are terminated on
each line (even for syntaxes in which newline does not end the
comment and blank lines do not get comments).  This can be
changed with `comment-style'.

我假设其他东西正在覆盖 C++ 键绑定,但我不知道什么或如何修复它?有没有人有任何想法?

4

3 回答 3

3

是的,c++ 模式有它自己的键盘映射,它会覆盖全局映射。请改用以下内容:

 (define-key c++-mode-map (kbd "C-c C-c") 'toggle-comment-on-line)
于 2013-07-25T23:48:41.107 回答
1

我对您的代码进行了一些改进,下面还有用于绑定键而不会出现错误的代码(发生这种情况是因为您试图在定义键 c++-mode-map之前对其进行定义)

(defun toggle-comment-on-line ()
  "comment or uncomment current line"
  (interactive)
  (let ((beg (if (region-active-p)
                (region-beginning)
              (line-beginning-position)))
        (end (if (region-active-p)
                (region-end)
              (line-end-position))))
    (comment-or-uncomment-region beg end)
    (next-line)))

(add-hook 'c++-mode-hook
      (lambda()
            (define-key c++-mode-map (kbd "C-c C-c") 'moo-complete)))

作为旁注,我强烈建议不要使用 binding C-c C-c,因为这是一种非常流行的特定于模式的绑定,在每种模式中都不同,但通常意味着确认

  • org-mode其中评估 babel 代码块
  • message-mode其中发送电子邮件
  • python-mode它将缓冲区发送到进程
  • 其中wdired确认您对文件名的编辑

所以如果你绑定它真的会很头疼,除非你使用 Emacs只是为了c++-mode.

我已经使用 Emacs 3 年了,而且我已经comment-dwimC-.. 到目前为止,我对此感到非常满意。

于 2013-07-26T06:13:39.740 回答
0

如果您愿意使用不同的键绑定,可以使用以下代码:

;; Nothing to see here.

之后你可以做C-a C-SPC C-n M-;

于 2013-07-26T13:56:18.203 回答