当前位置: 动力学知识库 > 问答 > 编程问答 >

function - How to loop while following-char equals string?

问题描述:

I am writing a function to uncomment regardless of mode. I want to delete all comment characters at the beginning of a line.

How do I make the snippet below loop until the following character is not equal to comment-start? (so basically have this "if" go on and on and on until following-char is not equal to comment-start anymore)

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

网友答案:

A while loop was easier than I thought:

(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))
    )
)
分享给朋友:
您可能感兴趣的文章:
随机阅读: