Commits

Arne Babenhauserheide committed f273d8b

add hook function to make this similar to a mode.

Comments (0)

Files changed (1)

libs/rainbow-text.el

 ))
 
 
-(defun rainbow-text-colorize ()
+(defun rainbow-text-colorize (&optional begin end length)
   "colorize a buffer by its chars."
   (interactive)
-  (loop for i from 1 to (1- (point-max))
-        do 
-        (save-excursion
-          (let 
-              (
-               (color (concat "#" 
-                              (number-to-string (+ 4 (mod i 5)))
-                              (number-to-string (+ 2 (mod i 7)))
-                              (number-to-string (mod i 9))))
-               (charcolor (nth 1 (assoc (char-to-string (char-after i)) rainbow-text-char-colors)))
-               )
-            (if charcolor
-                (put-text-property i (1+ i) 'font-lock-face `(:foreground ,charcolor))
-              (put-text-property i (1+ i) 'font-lock-face `(:foreground ,color)))))))
+  (let (
+        (begin (if (not begin)
+                   1
+                 begin))
+        (end (if (not end)
+                 (point-max)
+               end)))
+    (loop for i from begin to (1- end)
+          do 
+          (save-excursion
+            (let 
+                (
+                 (color (concat "#" 
+                                (number-to-string (+ 4 (mod i 5)))
+                                (number-to-string (+ 2 (mod i 7)))
+                                (number-to-string (mod i 9))))
+                 (charcolor (nth 1 (assoc (char-to-string (char-after i)) rainbow-text-char-colors)))
+                 )
+              (if charcolor
+                  (put-text-property i (1+ i) 'font-lock-face `(:foreground ,charcolor))
+                (put-text-property i (1+ i) 'font-lock-face `(:foreground ,color))))))))
+  
+(defun rainbow-text-add-hook () 
+  "Initialize rainbow text for this buffer."
+  (interactive)
+  (add-hook 'after-change-functions 'rainbow-text-colorize))
 
 (provide 'rainbow-text)