Define it using defun-q then print it out using my _PrinH function. :P
although still will have to unindent some of the lines, and correct the uppercases.you may need to improve _PrinH function a bit, otherwise you may loose some significant digits in real numbers (in case there are some)
:g/^;/d
I'm a notepad++ user, but for certain tasks I don't mind opening up VLIDE.
So does anyone know what Find/Replace expression(s) to use to get rid of all the comments? :thinking:
;.*
An editor has little to do with it; what you need to "effectively remove comments from X language" is something called a lexical analyzer. ...Vim allows you to use regex (any fairly good editor should) which will get you fairly good results. Vim extends the regex stuff a bit more (the above is just plain regex though) so you could do more with Vim compared to any other editor which just allows for regex but, your just splitting hairs at that point really.
If you don't know Vim, and aren't willing to learn it, then don't bother installing it.
A simple solution would be to set the 'Search Mode' to Regular Expression, then use a Find string of:Code: [Select];.*
But note that this would erroneously match semi-colons within strings...
...A little history: VI is one of two of the first editors (I mean, as you think of editors) so in essence you can thank VI (and emacs) for all of your current editors features. Vim is "VI improved" so it brings a *few* more features to the mix. Now, here's where you need to start taking what I am saying with some salt because many people will have differing opinions or take offense but I will dispense with all political correctness from here on out because they have their opinions and I have mine. VIM is the best there is; many people will tell you their editor is the best because it has X feature(s) but you can either do that out of the box with Vim (since 1982) or find a plug in to do it. You can make Vim a point and click editor or use it as is, you can do virtually anything with it because it's configuration is open (I've played Tetris in Vim). Vim's features count in the hundreds and you configure it for what you want it for. However, Vim has a very steep learning curve. Installing Vim to just edit a text file would be akin to learning AutoCAD to sketch a bird house you want to build.If you don't know Vim, and aren't willing to learn it, then don't bother installing it.
First rule for something that should be learn is to grab attention, without knowing what advantages this editor has and how much of them I'm going to use
theres no point in installing it, unless I'm about to execute a simple task thats not figured out in my editor (like this one).
...
;| This is a comment |;
(defun c:CleanLisp ( / _CleanLisp _FileToList _FindControls _FindQuotes _Pairs _Remove _Replace _ReplaceAll _String-Positions _String-Searches _StripBlockComments _StripEolComments _Main )
;; CleanLisp 1.0. Copyright (c) 2017 Michael Puckett.
;;
;; SUMMARY:
;;
;; Remove all end of line and block comments as well as
;; superfluous carriage returns from a lisp file.
;;
;; KNOWN LIMITATIONS:
;;
;; Will not properly process files that host strings that
;; spill over lines. That is, the opening quote is on one
;; line and the closing quote is on another.
;;
;; e.g.
;;
;; (princ "\nThis is really bad practice
;; ; don't do it.")
;;
;; Will likely not process similar horribly formed code.
;;
;; No intention to address these limitations at this time.
;;
;; As this is the first iteration of this program there
;; are numerous unknown bugs and limitations. Help me find
;; them.
;;
;; TERMS OF USE:
;;
;; Provided complete with mistakes, errors and omissions
;; and without warranty for any particular use. You may
;; not use this code in whole or in part unless you assume
;; full responsibility for all consequences resulting from
;; its direct or indirect use.
;;
;; All that said, attribution would be awesome; lol. :D
(defun _String-Positions ( code text / result )
(if (/= "" text)
( (lambda ( i )
(while (setq i (vl-string-position code text (1+ i)))
(setq result (cons i result))
)
(reverse result)
)
-1
)
)
)
(defun _String-Searches ( x text / result )
(if (/= "" text)
( (lambda ( i len )
(while (setq i (vl-string-search x text (+ i len)))
(setq result (cons i result))
)
(reverse result)
)
-1
(strlen x)
)
)
)
(defun _Replace ( oldtext newtext text / i idx )
(if (null (_String-Searches oldtext newtext))
(while (setq idx (_String-Searches oldtext text))
(foreach i (reverse idx)
(setq text (vl-string-subst newtext oldtext text i))
)
)
)
text
)
(defun _ReplaceAll ( oldlist newtext text )
(foreach oldtext oldlist
(setq text (_Replace oldtext newtext text))
)
text
)
(defun _Pairs ( lst / rst )
(while lst
(setq
rst (cons (list (car lst) (cadr lst)) rst)
lst (cddr lst)
)
)
(reverse (if (cadar rst) rst (cons (list (caar rst) (caar rst)) (cdr rst))))
)
(defun _FindControls ( text / result )
( (lambda ( i )
(while (setq i (vl-string-position 92 text (1+ i)))
(setq result (cons i result))
)
(reverse result)
)
-1
)
)
(defun _FindQuotes ( text / quotes controls )
(if (setq quotes (_String-Positions 34 text))
(_Pairs
(if (setq controls (_String-Positions 92 text))
(mapcar '1+
(vl-remove-if
(function (lambda (x) (member x controls)))
(mapcar '1- quotes)
)
)
quotes
)
)
)
)
(defun _Remove ( lst quotes )
(if quotes
(vl-remove-if
(function (lambda (x) (vl-some (function (lambda (p) (< (car p) x (cadr p)))) quotes)))
lst
)
lst
)
)
(defun _StripEolComments ( ln / quotes eol_comments beg_comments end_comments )
(if
(setq
eol_comments (_Remove (_String-Searches ";" ln) (setq quotes (_FindQuotes ln)))
beg_comments (_Remove (_String-Searches ";|" ln) quotes)
end_comments (_Remove (_String-Searches "|;" ln) quotes)
eol_comments (vl-remove-if (function (lambda (x) (member x beg_comments))) eol_comments)
eol_comments (vl-remove-if (function (lambda (x) (member (1- x) end_comments))) eol_comments)
)
(vl-string-right-trim " \t" (substr ln 1 (car eol_comments)))
ln
)
)
(defun _StripBlockComments ( body / quotes beg_comments end_comments flag )
(if
(setq
quotes (_FindQuotes body)
beg_comments (_Remove (_String-Searches ";|" body) quotes)
end_comments (_Remove (_String-Searches "|;" body) quotes)
flag (and beg_comments (eq (length beg_comments) (length end_comments)))
)
(foreach p (reverse (mapcar 'list beg_comments end_comments))
(setq body (strcat (substr body 1 (car p)) (substr body (+ 3 (cadr p)))))
)
)
body
)
(defun _FileToList ( fn / handle stream result )
(if (setq handle (open fn "r"))
(progn
(while (setq stream (read-line handle))
(setq result (cons (vl-string-right-trim " \t" stream) result))
)
(close handle)
(reverse result)
)
)
)
(defun _CleanLisp ( lispfile / lst )
(if (setq lst (_FileToList lispfile))
(_Replace "\n\n\n" "\n\n"
(_ReplaceAll '(" \n" "\t\n") "\n"
(_StripBlockComments
(apply 'strcat
(mapcar
(function (lambda (x) (strcat x "\n")))
(mapcar '_StripEolComments
(vl-remove-if-not
(function (lambda (x) (wcmatch (vl-string-left-trim " \t" x) "~;[~|]*")))
lst
)
)
)
)
)
)
)
)
)
(defun _Main ( / fn temp handle )
(if (setq fn (getfiled "Select lisp file:" "" "lsp" 0))
(progn
(setq
temp (vl-filename-mktemp "lean-cuisine.lsp")
handle (open temp "w")
)
(princ (vl-string-trim " \n" (_CleanLisp fn)) handle)
(close handle)
(startapp "notepad.exe" temp)
)
)
(princ)
)
(_Main)
)
You can write a simple lisp routine to clear out comments. You will need to decide whether to
you want to deal with inline/multiline notes as well ieCode: [Select];| This is a comment |;
-David
(cond ( (not row) ) ( (setq row (car L)) (setq L (cdr L)) ) )
(cond
( (not row) row)
(cmt (setq row nil) ) ; comment mode is on
( (wcmatch row "*;|*") (setq cmt T) (setq row nil) ) ; comment mode on
( (wcmatch row "*|;*") (setq cmt nil) (setq row (TrimStingUpTo row "|;")) ) ; comment mode off
( (wcmatch row "*;*") ; theres a code'n'comment
(setq row (TrimStingUpTo row ";"))
)
( (wcmatch row "*;") ; code and a single ';'
(setq row (TrimStingUpTo row ";"))
)
( (wcmatch row ";*") ; comment
(setq row nil)
)
(T row) ; no comment, only code
); cond
Don't have time for more than quick & dirty ...
Question, why do you want to remove comments?
It's a Trump directive.
(and (or (and lisp (findfile lisp)) (setq lisp (getfiled "Specify LISP file" (strcat (getenv "userprofile") "\\Desktop\\") "lsp" 16))) (setq handle (open lisp "r")))
so then:(_CleanLisp "sample.lsp") or (_CleanLisp nil)
;)
Seems to work - Thanks for sharing your skills, Michael! :)
That would be "How do I remove twitter comments". :laugh:
My pleasure. PS: Removes ;| This is a comment |; type comments too.
Yeah, if you pass it a nil it will crash.
Yeah, I've expected this after a quick glance, still not tested if would work for multiline ;| ... \n ... \n ... |; comments.
Nonono, the point is that now you could either pass a filename or use nil to prompt for a file.
My bad, please accept my apologies, read it was too fast with bleary eyes. I actually do that in a utility I have to harvest cvs/tsv data.
Disagree. What I wrote does not step outside your skill set. I was surprised to beat you to the punch to be honest.
And critique away! I may not agree but any code posted should be equal candidate for examination, improvement, yada.
(alert "\"Test1;Test2\"")
Code: [Select](alert "\"Test1;Test2\"")
I'd take this as a compliment
problem is that I'm so confused recently that I cannot estimate what I'm able to write and what I can't (so usually then im invoking the 'HelpFromTheSwamp method).
I think its like "I'm unable to find the right words to describe it" for coding.
I agree about criticism on codes, just trying to avoid the obvious stuff
And revised. :P
(+ 1 ;|2|;)
My pleasure. PS: Removes ;| This is a comment |; type comments too.
Yeah, I've expected this after a quick glance, still not tested if would work for multiline ;| ... \n ... \n ... |; comments.
Yeah, if you pass it a nil it will crash.
Nonono, the point is that now you could either pass a filename or use nil to prompt for a file.
Not talking about error-proofing the argument(s).
Code: [Select](+ 1 ;|2|;)
And revised. :P
(alert "1
;2")
(defun stripcomments ( lsp / des rgx rtn str tmp )
(if (setq des (open lsp "r"))
(progn
(while (setq str (read-line des)) (setq tmp (vl-list* "\n" str tmp)))
(close des)
(cond
( (null (setq rgx (vlax-create-object "vbscript.regexp")))
(prompt "\nUnable to interface with RegEx object.")
)
( (vl-catch-all-error-p
(setq rtn
(vl-catch-all-apply
'(lambda ( )
(vlax-put-property rgx 'global actrue)
(vlax-put-property rgx 'multiline actrue)
(vlax-put-property rgx 'pattern "(\"(?:[^\\\\\"]|\\\\.)*\")|(?:;\\|[\\S\\s]+?\\|;)|(?:;[^\\n]*)")
(vlax-invoke rgx 'replace (apply 'strcat (reverse tmp)) "$1")
)
)
)
)
(prompt (strcat "\nError: " (vl-catch-all-error-message rtn)))
)
( (princ rtn) )
)
)
)
(princ)
)
(vl-load-com) (princ)
Here's another way:
...
If its possible with regular expressions, then by knowing the correct expression one could do it in a lisp routine through the regexp object and process that certain .lsp file. (as a regex dummy, just asking: anyone about to try?)
A regular expression is a string that describes a match pattern. The match pattern provides a template that can be used to test another string, the search string, for a matching sub-string. In its simplest form, the match pattern string is just a sequence of characters that must be matched. For example, the pattern "fred" matches this exact sequence of characters and only this sequence. More sophisticated regular expressions can match against items such as file names, path names, and Internet URLs. Thus, the RegExp object is frequently used to validate data for correct form and syntax.
This (https://www.theswamp.org/index.php?topic=37030) is also amazing. :-)
This (https://www.theswamp.org/index.php?topic=37030) is also amazing. :-)
At a first glance this book looks really enjoyable to read!
Thank you, Lee! :)
You're most welcome - the 'book' is more a PDF of a slideshow, and is therefore best viewed in an appropriate PDF viewer by scrolling through with the pages fitting the full screen, so that you can see the 'animations'.