2012-11-03 35 views
0

我正在寫一個函數來取消註釋,不管模式如何。我想在行的開頭刪除所有註釋字符。如何循環while-char等於字符串?

如何使下面的代碼片斷循環,直到下面的字符不等於註釋開始? (所以基本上有這樣的「如果」去和和直到following-char不等於comment-start了)

(if (string= (byte-to-string (following-char)) comment-start) 
    (progn (delete-forward-char 1) 
      (when (string= (byte-to-string (following-char)) " ") 
       (delete-forward-char 1)))) 
+1

爲什麼不直接使用,取消註釋區域'M-;' – kindahero

+0

因爲該取消註釋區域僅適用於區域。這也取消註釋當前行上的註釋字符,而不管註釋點在哪裏。 – PascalVKooten

+0

@ kindahero對我來說很棒! – PascalVKooten

回答

0

while循環更容易比我想象:

(defun uncomment-mode-specific() 
    "Uncomment region OR uncomment beginning of line comment OR uncomment end" 
    (interactive) 
    (if (region-active-p) 
     (uncomment-region (region-beginning) (region-end)) 
    (back-to-indentation)) 
    (setq scvar 0) 
    (setq scskipvar 0) 
    (while (= scvar 0) 
    (if (string= (byte-to-string (following-char)) comment-start) 
     (progn (delete-forward-char 1) 
      (when (string= (byte-to-string (following-char)) " ") 
         (delete-forward-char 1)) 
      (setq scskipvar 1)) 
     (setq scvar 1))) 
    (if (= scskipvar 0) 
     (progn (search-forward comment-start nil t) 
     (left-char 1) 
     (kill-line)) 
    ) 
)