2013-08-17 26 views
3

emacs中是否有命令可以取消註釋整個註釋塊,而無需先標記它?取消註釋多行而不選擇它們的命令

例如,讓我們說點是在下面的代碼中的註釋裏:

(setq doing-this t) 
    ;; (progn |<--This is the point 
    ;; (er/expand-region 1) 
    ;; (uncomment-region (region-beginning) (region-end))) 

我想它可將命令這個:

(setq doing-this t) 
    (progn 
    (er/expand-region 1) 
    (uncomment-region (region-beginning) (region-end))) 

這是相當容易寫(un)評論單行的命令,但我還沒有找到一個儘可能多地取消註釋行的命令。有沒有可用的?

+0

多個遊標是一個極好的包:https://github.com/magnars/multiple-cursors.el你可以去每行或該地區的每個線的右側的左側,同時鍵入或刪除多行上的字符。 – lawlist

回答

3

快速回復---代碼可以改進,使其更有用。例如,除了​​之外,您可能還想將其擴展到其他類型的評論。

(defun uncomment-these-lines() 
    (interactive) 
    (let ((opoint (point)) 
     beg end) 
    (save-excursion 
     (forward-line 0) 
     (while (looking-at "^;;; ") (forward-line -1)) 
     (unless (= opoint (point)) 
     (forward-line 1) 
     (setq beg (point))) 
     (goto-char opoint) 
     (forward-line 0) 
     (while (looking-at "^;;; ") (forward-line 1)) 
     (unless (= opoint (point)) 
     (setq end (point))) 
     (when (and beg end) 
     (comment-region beg end '(4)))))) 

關鍵是comment-region。 FWIW,我將comment-region綁定到C-x C-;。只需與C-u一起使用即可取消註釋。

+0

作品!非常感謝。 – Malabarba

+0

@Drew如果你喜歡它,看一下變量'comment-start'和'comment-end'來使代碼更一般。 – Thomas

+0

是的,它可以通過幾種方式進行改進,包括除了';;;;;;;;;;;;;;;;;;;;;;;;;;;;;;是,to, '在大膽。我自己並沒有太多的使用這樣的命令,但@BruceConnor可能會擴展它。 – Drew

3

您可以使用Emacs的註釋處理函數來製作Drew命令的通用版本。

(defun uncomment-current() 
    (interactive) 
    (save-excursion 
    (goto-char (point-at-eol)) 
    (goto-char (nth 8 (syntax-ppss))) 
    (uncomment-region 
    (progn 
     (forward-comment -10000) 
     (point)) 
    (progn 
     (forward-comment 10000) 
     (point))))) 
+1

'forward-comment'「放棄」太容易了;如果點不直接在評論後面,它似乎沒有捕捉任何東西。假設註釋是行中的第一個非空白字符,可以通過在行程開始處添加'(行首)'來解決。 – scarlet

+1

我會使用'uncomment-region'。我首先用'(goto-char(nth 8(syntax-ppss)))'這樣的東西移動到當前註釋之外。 – Stefan

+0

已更新,來自scarlet和stefan的建議 –

相關問題