You may be interested in this OLD lisp.
;;;==================[ Strip_comments.lsp ]===========================
;;; Author: Copyright© 2006 Charles Alan Butler
;;; Version: 1.2 Oct. 20, 2006
;;; Purpose: To strip trailing comments from the end of lines
;;; in a lisp file.
;;; Requirements: User makes file selection and input of comment
;;; string starting characters
;;; Enter H for help at the 'String to Remove' propmt
;;; User has the option to remove empty lined created
;;; when they are stripped
;;; Returns: nil
;;;==================================================================
(defun c:strip_comments (/ file2read file2write filename
filepath filespec honorblocks ignoreon
lncnt outfile pos pos2
stcnt str strpat strippat
DelLines skip stflag strip_help
sphraser
)
;;
;;----------------------------
;; H E L P
;;----------------------------
;;
(defun strip_help ()
(alert
(strcat
"Strip_Comment.lsp (c) 2006 Charles Alan Butler\n"
"\nEntering ;_&;- will strip the following lines"
"\n (if var1 ;- test var1 \n" " ) ;_ endif "
"\nNote a special case where \"; \" is entered, but this \";; \" will be ignored."
"\nThe user may enter multiple strings to strip at the same time by"
"\nseparating them with '&' Example: ;_&;-&; F"
"\nAny characters starting with ';_' or ';-' or '; F' will be stripped."
"\nA warning though, it does not skip over quoted text. "
"\nSo this would be stripped (setq str \"This is a test ;_\")"
"\nto this (setq str \"This is a test "
"\nTherefore use caution in choosing the match characters."
"\nAnother prompt will allow for the removal of any line made empty when"
"\nthe comment is strpped."
"\n\nPlease report any problems you may have.")
)
) ; end defun help
;;-------------------------
;; string phraser by CAB
;;-------------------------
;; return a list of strings split from str using delim
(defun sphraser (str delim / ptr lst stp)
(setq stp 1)
(while (setq ptr (vl-string-search delim str (1- stp)))
(setq lst (cons (substr str stp (- (1+ ptr) stp)) lst))
(setq stp (+ ptr 2))
)
(reverse (cons (substr str stp) lst))
)
;;
;; o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
;; M A I N R O U T I N E S T A R T S H E R E
;; o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o-o
;;
(if
(and
(setq filespec (getfiled "Select a File in the Folder you want to use"
"" "lsp" 4))
(setq filepath (strcat (vl-filename-directory filespec) "\\"))
(setq filename (strcat (vl-filename-base filespec)
(vl-filename-extension filespec)))
)
(progn ; --------- Begin File Operations -------------------
(while
(progn
;; get search string, allow mutiple strings
(setq strpat
(getstring t
(strcat
"\nEnter string to remove, Seperate strings with & "
"\nExample \";_&; \" Enter H for help "
"\nEnter only for default <;_> "))
)
(cond
((= (strcase strpat) "H")
(strip_help)
t
)
((= (vl-string-trim " \t\n" strpat) "")
(setq strippat '(";_")) ; default
nil ; exit loop
)
((and (setq strippat (vl-remove "" (sphraser strpat "&")))
(> (length strippat) 0) ; this is the only error check
)
nil ; exit loop
)
)
)
)
;; Yes/No to ignore Block Comments
(initget "Yes No") ; Yes = nil = skip over Block Comments
(setq HonorBlocks (/= (getkword "\nSkip Block Comments [Yes/No] <Yes>: ") "No"))
;; Yes/No to remove emptied lines
(initget "Yes No") ; Yes = t = delete lines if empty after striping
(setq DelLines (/= (getkword
"\nRemove lines that are made empty [Yes/No] <No>: ") "Yes"))
;; Output File Name
(setq outfile (strcat (vl-filename-base filespec) "-NEW"
(vl-filename-extension filespec))
)
(if ; Open both Read & Write files are open
(cond
((not (setq file2read (open filespec "r")))
(alert (strcat "Error - Can not open file - " filename))
)
((not (setq file2write (open (strcat filepath outfile) "w")))
(alert (strcat "Error - Can not open file - " outfile))
)
(t ; all is OK if you are here
)
)
;;
;; Process the File
;;
(progn
(setq lncnt 0
stcnt 0
ignoreon nil ; deal with Block Comments
)
;; Block Comments - does not support more than one pair
;; in a single line
(while (setq str (read-line file2read))
(setq lncnt (1+ lncnt))
(cond
;; Check for block comments first
((and honorblocks ignoreon (setq pos (vl-string-search "|;" str)))
(setq ignoreon nil)
(if (and (setq pos2 (vl-string-search ";|" str))
(> pos2 pos)
)
(setq ignoreon t)
)
)
((and honorblocks (setq pos (vl-string-search ";|" str)))
(setq ignoreon t)
(if (and (setq pos2 (vl-string-search "|;" str))
(< pos2 pos)
)
(setq ignoreon nil)
)
)
(ignoreon ; t = skip comment block
)
;; Check for target string
((vl-remove nil
(mapcar '(lambda (x) (vl-string-search x str)) strippat)
)
(setq stflag nil)
(foreach sp strippat
;; ignore special case where target is "; " & this is found ";; "
(if (and (setq pos (vl-string-search sp str))
(or (/= sp "; ")
(and (= sp "; ")
(or (zerop pos)
(/= (substr str pos 1) ";")))))
(progn
(if (and (setq pos (vl-string-search sp str 0))
(setq pos2 (vl-string-search "\"" str 0))
)
(setq pos (StrSearch sp str))
)
(if pos ; nil if ; is within a quote
(progn
(setq str (substr str 1 pos)
stflag t
)
(if (and DelLine (= (vl-string-trim "" str) ""))
(setq skip t)
)
)
)
)
)
)
(if stflag (setq stcnt (1+ stcnt)))
)
;; fall through if nothing to change
)
(if skip
(setq skip nil)
(write-line str file2write)
)
)
(close file2read)
(if (close file2write)
(prompt (strcat "\n*** Error creating file " outfile " ***"))
(progn
(prompt (strcat "\n*** New created. " outfile " ***"
"\nLines examined " (itoa lncnt)
". Lines stripped " (itoa stcnt) "."))
;; debug - view new file in NotePad
;; remove the ;; in the next line to start NotePad
(startapp "notepad.exe" (strcat filepath outfile))
)
)
(setq file2read nil
file2write
nil
)
)
;; Else close file if they were opened
(progn
(and file2read (close file2read))
(and file2write (close file2write))
)
)
) ; progn ----------------------------------
)
(princ)
)
(prompt "\nStrip_Comments.lsp loaded, Enter strip_comments to run.")
(princ)
;; EOF
;;;=======================[ StrSearch.lsp ]=======================
;;; Author: Copyright© 2006 Charles Alan Butler
;;; Version: 1.0 Oct. 20, 2006
;;; Purpose:
;;; This is a vl-string-search that will ignore characters
;;; found within quoted text, i.e. search unquoted text only
;;; return the position (base 0) of the target string
;;; within the search string, honor quoted text
;;;
;;; Requirements: -tar = String to search for
;;; -str = String to search in
;;; Returns: -the position number, base 0, or nil
;;;==============================================================
;;;
;;; Example: (setq str "\"yy;_\" ;_ real comment"
;;; (setq pos (chk ";_" str)) ; returns 7
;;; (substr str (1+ pos)) ; returns ";_ real comment"
;;;
(defun StrSearch (tar str / tlen q1 q2 qx idx idx2 sc loop rtn)
(setq tlen (strlen tar)
idx 0
idx2 0
)
;; NOTE loop is a dummy var so the value is returned from the cond
;;******************************************
;; no quotes found, this should not happen
;; added here as a fail safe
;;******************************************
(if (null (vl-string-search "\"" str 0))
(setq rtn (vl-string-search tar str 0)) ; return the position if it exist
(while
(cond
;;======================================
;; find the opening quote, ignore \\\"
((and (null q1) (setq q1 (vl-string-search "\"" str idx)))
(if (and (setq qx (vl-string-search "\\\"" str idx))
(= (1- q1) qx)
)
(setq idx (1+ q1)
q1 nil
qx nil
)
(setq idx (1+ q1))
)
t
)
;;======================================
;; find the closing quote for the open
;; quote we have, ignore \\\"
((and q1 (null q2)(setq q2 (vl-string-search "\"" str idx)))
(if (and (setq qx (vl-string-search "\\\"" str idx))
(= (1- q2) qx)
)
(setq idx (1+ q2)
q2 nil
)
)
t
)
;;=====================================
((setq sc (vl-string-search tar str idx2))
(cond
;; simicolon is within quotes
((< q1 sc q2)
(setq idx2 (+ sc tlen) ; ignore it
sc nil
)
t ; stay in loop
)
;; quote is in a comment
((or (and (null q1) (null q2))
(and (null q2) (< sc q1))
)
(setq rtn sc) ; value to return
nil ; exit the loop
)
;; simicolon is out side of this quote pair
((> sc q2)
(if (> sc (vl-string-search "\"" str (1+ q2)))
;; set up to look for another quote
(setq idx (1+ q2)
q1 nil
q2 nil
loop t
)
(setq rtn sc ; value to return
loop nil ; exit the loop
)
)
)
;; quote is in the comment
((< sc q1)
(setq rtn sc) ; value to return
nil ; exit the loop
)
) ; end cond stmt
)
;;====================================
) ; end cond stmt
) ; end while
)
rtn
)