;;AUTOLOAD: grep
;;AUTODOC: grep "a grep as a klone function"
;; (grep [:i][:f] pattern files...)
;;
;; A "grep" implemented in klone (demo function)
;; options:
;; :i   ignore case
;; :f   fgrep, quotes regexp
;; returns a string which is a grep-like output

(defun grep (&rest args &aux 
    (res (Stream ""))
    re
    ignore-case
    fgrep
    line
  )
  (while (typep (0 args) Keyword)
    (if (= :i (0 args)) (setq ignore-case t)
      (= :f (0 args)) (setq fgrep t)
    )
    (delete args 0)
  )
  (setq re (0 args))
  (if fgrep (setq re (quote-string-for-regexp re)))
  (if ignore-case (setq re (re-nocase re)))
  (setq re (regcomp re))
  (delete args 0)
  (dolist (file args)
    (with (fd (open file))
      (while (setq line (read-line fd ()))
	(if (regexec re line)
	  (print-format res "%n0:%n1\n" file line)
  ))))
  (String res)
)
