Last active
August 29, 2015 14:05
-
-
Save Eskatrem/dd27a4d8cd77ab469c0b to your computer and use it in GitHub Desktop.
comment lines matching a regexp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun s-trim-left (s) | |
"Remove whitespace at the beginning of S." | |
(if (string-match "\\`[ \t\n\r]+" s) | |
(replace-match "" t t s) | |
s)) | |
(defun first-word (s) | |
(let ((s-trim (s-trim-left s))) | |
(car (split-string s-trim " ")))) | |
(defun get-lines-of-buffer (buff what-to-do) | |
(with-current-buffer buff | |
;;(save-excursion) | |
(setq start-char 1) | |
(while (< start-char (point-max)) | |
(goto-char start-char) | |
(let* ((end-line (line-end-position)) | |
(tmp-str (buffer-substring-no-properties (line-beginning-position) end-line))) | |
(funcall what-to-do tmp-str) | |
(setq start-char (+ 1 end-line)))))) | |
(defun get-nth-char (s n) | |
(substring s n (+ 1 n))) | |
(defun get-count-spaces (str) | |
(progn | |
(setq n 0) | |
(while (and (string= " " (get-nth-char str n)) (< n (string-width str))) | |
(setq n (+ 1 n))) | |
n)) | |
(defun comment-prints () | |
(interactive) | |
(save-excursion | |
(get-lines-of-buffer | |
(current-buffer) | |
(lambda (str) | |
(when | |
(string= "print" (first-word str)) | |
(goto-char (+ (line-beginning-position) (get-count-spaces str))) | |
(insert "#" )) )))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment