2013-07-25 120 views
1

我是新来的Emacs和使用emacs 24并试图与C-C C-C绑定功能注释掉一行。我在我的init.el文件中有以下内容,但似乎在C++中不起作用。为什么我的C-c C-c键盘图工作在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++的按键组合,但我不知道是什么或如何解决呢?有没有人有任何想法?

+0

你尝试内置的命令'评论-dwim',可在'M-;'在所有模式? – lunaryorn

回答

3

是的,C++模式有它自己的键盘布局,其覆盖全球的地图。使用以下代替:

(define-key c++-mode-map (kbd "C-c C-c") 'toggle-comment-on-line) 
+0

谢谢你看起来不错。我从这一行中得到错误:'错误的类型参数:keymapp,C++ - mode-map'。 – aw4lly

+2

(define-key C++ - mode-map(kbd“C-c C-c”)'toggle-comment-on-line) 为我工作(丢失'之前的C++ ...) – aw4lly

+1

@ aw4lly修复,对此感到遗憾。 – Malabarba

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))) 

作为一个方面说明,我强烈建议对结合CC CC, 因为这是一个非常受欢迎模式的特异性结合,在每一个模式的不同, 而是指通常确认

  • org-mode它评估的代码为Babel块
  • message-mode它发送电子邮件
  • python-mode它发送缓冲器在过程中wdired
  • 它确认您编辑的文件名

所以你真的有头痛,如果你绑定,除非你使用Emacs 只是c++-mode是 。

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

+0

什么阻碍对默认绑定'M-;''为带注释dwim'? – lunaryorn

+0

'C-。'是最好的地产,'M-;'是郊区。 –

+0

嗯,它是Flyspell的主要地产,因为Flyspell已经声称''flyspell-auto-correct-word'的C-。。 – lunaryorn

0

如果你愿意使用不同的密钥绑定,可以使用下面的代码:

;; Nothing to see here. 

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