Author Topic: Get rid of comments (Using NP++ or VLIDE)  (Read 12609 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Swamp Rat
  • Posts: 812
Get rid of comments (Using NP++ or VLIDE)
« on: December 16, 2017, 04:55:21 PM »
Hey everyone,
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:

For example to get rid of all comments from a code like this:

Code - Auto/Visual Lisp: [Select]
  1. (defun C:test ( / pt n )
  2.   ; this code does bla
  3.   ; and also does bla-ba
  4.   ; its very big
  5.   ; and also has a ton of comments
  6.   (if
  7.     ; so much comments
  8.     ; its like you are reading a book
  9.     (and ; (and) function does..
  10.       (setq pt (getpoint "\nPick a point: ")) ; this prompts the user for a point
  11.       ; the comments appear out ot of nowhere
  12.       ; that you might get lost
  13.       (progn (initget 7) (setq n (getreal "Number: ")))
  14.     ); and
  15.     (progn ; the (progn) function does bla..
  16.       (entmakex ; entmakex creates that object
  17.         (list ; this is a list of some dxf group codes
  18.           '(0 . "LINE")
  19.           '(8 . "Layer3") ; thats a group code for layer
  20.           (cons 10 (cons (+ (car pt) n) (cdr pt)))
  21.           (cons 11 (cons (- (car pt) n) (cdr pt)))
  22.         ); list
  23.       ); entmakex
  24.     ); progn
  25.   ); if
  26.   (princ)
  27. ); defun C:test

Usually I don't use ;| ... |; comments but won't mind if one knows how to get rid of these aswell.
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #1 on: December 16, 2017, 06:25:22 PM »
Define it using defun-q then print it out using my _PrinH function. :P
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #2 on: December 16, 2017, 06:41:20 PM »
Define it using defun-q then print it out using my _PrinH function. :P

The defun-q sounds like a nice idea, however along with _PrinH ended up with funny result:

Code - Auto/Visual Lisp: [Select]
  1. _$ (prinh test)
  2.  
  3. (
  4.   (
  5.     /
  6.     PT
  7.     N
  8.   )
  9.   (
  10.     IF
  11.     (
  12.       AND
  13.       (
  14.         SETQ
  15.         PT
  16.         (
  17.           GETPOINT
  18.           "\nPick a point: "
  19.         )
  20.       )
  21.       (
  22.         PROGN
  23.         (
  24.           INITGET
  25.           7
  26.         )
  27.         (
  28.           SETQ
  29.           N
  30.           (
  31.             GETREAL
  32.             "Number: "
  33.           )
  34.         )
  35.       )
  36.     )
  37.     (
  38.       PROGN
  39.       (
  40.         ENTMAKEX
  41.         (
  42.           LIST
  43.           (
  44.             QUOTE
  45.             (0 . "LINE")
  46.           )
  47.           (
  48.             QUOTE
  49.             (8 . "Layer3")
  50.           )
  51.           (
  52.             CONS
  53.             10
  54.             (
  55.               CONS
  56.               (
  57.                 +
  58.                 (
  59.                   CAR
  60.                   PT
  61.                 )
  62.                 N
  63.               )
  64.               (
  65.                 CDR
  66.                 PT
  67.               )
  68.             )
  69.           )
  70.           (
  71.             CONS
  72.             11
  73.             (
  74.               CONS
  75.               (
  76.                 -
  77.                 (
  78.                   CAR
  79.                   PT
  80.                 )
  81.                 N
  82.               )
  83.               (
  84.                 CDR
  85.                 PT
  86.               )
  87.             )
  88.           )
  89.         )
  90.       )
  91.     )
  92.   )
  93.   (
  94.     PRINC
  95.   )
  96. )

The hirearchy went a bit further.  :laugh:

EDIT: I've forgot about the "pretty-print" option, makes the formatting issue way better:
Code - Auto/Visual Lisp: [Select]
  1. ((/ PT N) (IF (AND (SETQ PT (GETPOINT "\nPick a point: "))
  2.                    (PROGN (INITGET 7) (SETQ N (GETREAL "Number: ")))
  3.               )
  4.             (PROGN
  5.               (ENTMAKEX (LIST '(0 . "LINE")
  6.                               '(8 . "Layer3")
  7.                               (CONS 10 (CONS (+ (CAR PT) N) (CDR PT)))
  8.                               (CONS 11 (CONS (- (CAR PT) N) (CDR PT)))
  9.                         )
  10.               )
  11.             )
  12.           )
  13.           (PRINC)
  14. )

This one can be easily refixed using the indent by fold plugin in NP++, although still will have to unindent some of the lines, and correct the uppercases.
« Last Edit: December 16, 2017, 06:52:15 PM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #3 on: December 16, 2017, 07:09:46 PM »
Looks good to me. :P

Laughs aside, I'm not familiar with NP++ but in textapad it's easy to highlight, and then delete all lines that start with a semicolon.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #4 on: December 16, 2017, 07:18:05 PM »
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)

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #5 on: December 16, 2017, 07:21:20 PM »
In Vim:
Code: [Select]
:g/^;/d
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #6 on: December 16, 2017, 07:24:09 PM »
MP, I really appreciate your help,
Obviously we could process the rows of the .lsp file and erase the ones that are fully-commented:
Code - Auto/Visual Lisp: [Select]
  1.   ; this code does bla
  2.   ; and also does bla-ba
  3.   ;...
Trouble would be on the rows that have both - code and comment:
Code - Auto/Visual Lisp: [Select]
  1.  (and ; (and) function does..

I just thought someone already figured out a technique for such task already, seems not.  :rolleyes2:
Its just really irritating that your editor/IDE recognizes the comments and highlights them in different colour, but you can't actually do anything.

Vovka, thanks for pointing that out, but I think _PrinH must be put aside.

John, thanks I'll try that if I there are no other good options (cause it would require me to install Vim).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #7 on: December 16, 2017, 07:33:52 PM »
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.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #8 on: December 16, 2017, 07:40:14 PM »
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:

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...

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #9 on: December 17, 2017, 08:55:15 AM »

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.


Thanks, learned something new today.
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?)
NP++ has a regular expression search mode, but unfortunately doesn't work with your example.


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).





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...

Thanks Lee, that worked!
I'll keep an eye for the strings - this shouldn't be hard. :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

JohnK

  • Administrator
  • Seagull
  • Posts: 10625
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #10 on: December 17, 2017, 12:31:31 PM »
...
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).

...
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.

You'd install/configure Vim if you want to program in many languages; you can certainly use it for just AutoLisp but you may be taking on too much for just simple AutoLisp editing (Vim is a Castle whereas the VLIDE is an outhouse).

Now, you are certainly fine with editors like NP++ or Crimson or whatever but you are talking a whole different class of editors when you think of Vim or Emacs (they are the two biggest/best). What I mean is, you may find "the limits of" far more often with other editors then with the two "big boy" editors.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

David Bethel

  • Swamp Rat
  • Posts: 656
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #11 on: December 17, 2017, 12:54:37 PM »
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  ie

Code: [Select]
  ;|   This is a comment |;

-David
R12 Dos - A2K

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #12 on: December 18, 2017, 01:19:19 PM »
Don't have time for more than quick & dirty ...

Edit: Revised to address VovKa's challenges as well as wrap in command def.

Code: [Select]
(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)

)

Sample.lsp:

(defun C:test ( / pt n )
  ; this code does bla
  ; and also does bla-ba
  ; its very big
  ; and also has a ton of comments

  (alert "\"Test1;Test2\"") ;; VovKa's challenge

  (+ 1  ;|2|;) ;; VovKa's challenge #2


  (if
    ; so much comments
    ; its like you are reading a book
    (and ; (and) function does..
      (setq pt (getpoint "\nPick a point: ")) ; this prompts the user for a point
      ; the comments appear out ot of nowhere
      ; that you might get lost
      (progn (initget 7) (setq n (getreal "Number: ")))
    ); and
    (progn ; the (progn) function does bla..
      (entmakex ; entmakex creates that object
        (list ; this is a list of some dxf group codes
          '(0 . "LINE")
          '(8 . "Layer3") ; thats a group code for layer
          (cons 10 (cons (+ (car pt) n) (cdr pt)))
          (cons 11 (cons (- (car pt) n) (cdr pt)))
          ;|let's remove
          all this stuff too|;
        ); list
      ); entmakex
    ); progn
  ); if
  (princ)
); defun C:test


Sample output:

(defun C:test ( / pt n )

  (alert "\"Test1;Test2\"")

  (+ 1  )
 

  (if
    (and
      (setq pt (getpoint "\nPick a point: "))
      (progn (initget 7) (setq n (getreal "Number: ")))
    )
    (progn
      (entmakex
        (list
          '(0 . "LINE")
          '(8 . "Layer3")
          (cons 10 (cons (+ (car pt) n) (cdr pt)))
          (cons 11 (cons (- (car pt) n) (cdr pt)))
                  )
      )
    )
  )
  (princ)
)


Cheers.
« Last Edit: December 19, 2017, 02:28:59 PM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

ChrisCarlson

  • Guest
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #13 on: December 18, 2017, 02:03:12 PM »
Question, why do you want to remove comments?

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #14 on: December 18, 2017, 02:05:21 PM »
It's a Trump directive.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #15 on: December 18, 2017, 03:19:46 PM »
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  ie

Code: [Select]
  ;|   This is a comment |;
-David

Yeah, David - I was thinking about writing some conditional that switches few booleans if comment is active.
And would process the list of strings from the .lsp file. But just left it as starting conception (not finished) :
Code: [Select]
(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

Unfortunately I'm tight on free time again, but would finish it if it was very important (although initially I didn't think the task would translate into coding).


Don't have time for more than quick & dirty ...

Seems to work - Thanks for sharing your skills, Michael! :)


Question, why do you want to remove comments?

Simply said: over commenting
Say I wanted to learn someone's code by reading whats happening in the plain code, but there are too many comments that I loose my mind in a redundant descriptions, (thru the mid-evaluation).
Other reason would be, if the code is commented in Spanish/Russian/German/Chineese - a lisper might don't know the language, but still would be able to understand whats happening in there (and the comments still get in the way).

Few times I had such problem, so that brought my question that there might be a global solution for this.
And I'm not talking about removing any copyrighted footers or headers(that'd be silly), but a stuff like:
Code - Auto/Visual Lisp: [Select]
  1. (+
  2.   ; Hey hey
  3.   ; This is ..
  4.   ; ...
  5.   ; ...
  6.   (1+
  7.     ; bla-bla
  8.     ; bla-bla
  9.     2
  10.   )
  11.   ; ...
  12.   3
  13. )



It's a Trump directive.

That would be "How do I remove twitter comments":laugh:

EDIT:

MP, I've forgot to mention that I did a small addition to your code:
inside nom's def.. :
Code: [Select]
(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:
Code: [Select]
(_CleanLisp "sample.lsp") or (_CleanLisp nil);)
« Last Edit: December 18, 2017, 03:25:52 PM by Grrr1337 »
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #16 on: December 18, 2017, 03:28:09 PM »

Seems to work - Thanks for sharing your skills, Michael! :)

My pleasure. PS: Removes ;| This is a comment |; type comments too.

That would be "How do I remove twitter comments":laugh:

Exactly.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #17 on: December 18, 2017, 03:31:46 PM »
I don't see the point in (_CleanLisp nil) hand holding code.

Yeah, if you pass it a nil it will crash.

So will passing nils to native functions that expect non nil values, e.g. (+ nil nil).

/2¢
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #18 on: December 18, 2017, 03:55:34 PM »

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).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #19 on: December 18, 2017, 03:58:37 PM »
Yeah, I've expected this after a quick glance, still not tested if would work for multiline ;| ... \n ... \n ... |; comments.

it will. :)

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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #20 on: December 18, 2017, 04:13:06 PM »
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.

No worries MP, that'd be stupid of me to pick on a newly offered code which I'm unable to write myself.  :uglystupid2:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #21 on: December 18, 2017, 04:16:59 PM »
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.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #22 on: December 18, 2017, 04:47:28 PM »
Disagree. What I wrote does not step outside your skill set. I was surprised to beat you to the punch to be honest.

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.

And critique away! I may not agree but any code posted should be equal candidate for examination, improvement, yada.

I agree about criticism on codes, just trying to avoid the obvious stuff (nitpick).
Because saying to you that you should check'n' error-proof the input args sounds like "Hey Lee, (strcase str) for str = nil will error out":-D
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #23 on: December 18, 2017, 05:10:44 PM »
Code: [Select]
(alert "\"Test1;Test2\"")

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #24 on: December 18, 2017, 05:48:44 PM »
Code: [Select]
(alert "\"Test1;Test2\"")

Fun!

And revised. :P
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #25 on: December 18, 2017, 05:51:08 PM »
I'd take this as a compliment

that was the intention

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.

you're doing fine from what I've read

I agree about criticism on codes, just trying to avoid the obvious stuff

generally a good strategy
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #26 on: December 18, 2017, 06:03:16 PM »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2132
  • class keyThumper<T>:ILazy<T>
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #27 on: December 18, 2017, 06:08:02 PM »

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).

Personally I'd leave the original as is and write a wrapper function to test/prompt for a filename ... have the wrapper call the function that does the work.

I've assumed the question/statement about 'why do you want to remove comments' was rhetorical.


Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #28 on: December 18, 2017, 06:38:16 PM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

VovKa

  • Water Moccasin
  • Posts: 1628
  • Ukraine
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #29 on: December 18, 2017, 07:28:19 PM »

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #30 on: December 18, 2017, 07:42:02 PM »
lol, while I could solve that I'm not going to encourage horrible coding style.  :police:
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #31 on: December 19, 2017, 02:47:37 PM »
Here's another way:
Code: [Select]
(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)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #32 on: December 20, 2017, 08:45:30 AM »
Great job MP and Lee Mac!
I should have included "{Challenge}" in the topic's name, since it looks like one.



Here's another way:
...

Approach, what exactly I had in mind:


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?)


So thanks Lee, really apreciate this one!  :yay!:

I have two (offtopic) questions, though:
  • Whats the general use of regular expressions? Is it somewhat a very powerful wcmatch function?
  • How/where one can visualise/test the regex pattern, so it works as expected?

The regex dummy asking, hope you don't mind (or anyone if could answer).
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #33 on: December 20, 2017, 10:47:56 AM »
There are a glut of regex testers -- regexstorm is not bad.

Note that in lisp backslashes and quotes etc. have to escaped, so a regex pattern like this:

("(?:[^\\"]|\\.)*")|(?:;\|[\S\s]+?\|;)|(?:;[^\n]*)

That is testable in a tool like regexstorm needs to look like this in lisp:

(\"(?:[^\\\\\"]|\\\\.)*\")|(?:;\\|[\\S\\s]+?\\|;)|(?:;[^\\n]*)

Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #34 on: December 20, 2017, 11:22:44 AM »
Thanks MP, your comment here will help me solve this regex mystery till the end of 2017.
BTW every time I use something, my head associates the first type of problem/technique/solution with the guy(s) who helped me there(or introduced), i.e.:
  (<getridofcomments> MP LeeMac) (<regex> MP)
So I just carved these additional associations in my brain.  :crazy2:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #35 on: December 20, 2017, 11:43:39 AM »
Ha, you're most welcome.

In case it wasn't obvious the regexstorm site has a reasonable regex reference.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #36 on: December 20, 2017, 12:09:08 PM »
So overall a regex pattern like this, is something like a very powerful pattern-string-search function. And can be used to manipulate these certain fragments like replacing/removing them.

Does that sound like a valid general use of regular expressions?  :thinking:
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #37 on: December 20, 2017, 12:20:42 PM »
Quote from: Dale Fugier (author of DOSLib)
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.

source
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #38 on: December 20, 2017, 12:41:06 PM »
Thanks again, Michael - I think all the stuff you posted will be enough. :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #39 on: December 20, 2017, 01:05:43 PM »
:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #40 on: December 20, 2017, 01:23:35 PM »
Where RegEx testers are concerned, I've found RegExr to be good.

This is also amazing.  :-)

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #41 on: December 20, 2017, 01:46:56 PM »
This is also amazing.  :-)

At a first glance this book looks really enjoyable to read!
Thank you, Lee! :)
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg

Lee Mac

  • Seagull
  • Posts: 12912
  • London, England
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #42 on: December 20, 2017, 02:07:19 PM »
This 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'.

Grrr1337

  • Swamp Rat
  • Posts: 812
Re: Get rid of comments (Using NP++ or VLIDE)
« Reply #43 on: December 20, 2017, 02:24:49 PM »
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'.

Yes I initially viewed it in the way you describe it - seems that its made for lecturing purposes.

For me its just amazing how good you are at so many aspects, so almost every time I browse some of your codes brings up the question: how?
(apply ''((a b c)(a b c))
  '(
    (( f L ) (apply 'strcat (f L)))
    (( L ) (if L (cons (chr (car L)) (f (cdr L)))))
    (72 101 108 108 111 32 87 111 114 108 100)
  )
)
vevo.bg