Author Topic: Get rid of comments (Using NP++ or VLIDE)  (Read 12694 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: 1632
  • 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: 10648
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: 10648
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: 12915
  • 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: 10648
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