Menu

Show posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Show posts Menu

Topics - PapoAnaya

#1
So, what can you actually DO with newLISP? / Lorem Ipsum
November 08, 2012, 04:49:08 PM
Dolor sit amet...



;; This code is in newLisp
;;
;; Use the lipsum generator to generate Lorem Ipsum dummy paragraphs / words / bytes.
;;
;; Lorem Ipsum courtesy of www.lipsum.com by James Wilson
;;
;; @param what in "paras","words","bytes"]
;; @param amount of paras/words/bytes (for words minimum is 5, for bytes it is 27)
;; @param start always start with 'Lorem Ipsum' "true"/"false"

(define (lipsum what amount start)
   (setq lipsum-buffer (get-url (format "http://www.lipsum.com/feed/xml?what=%s&amount=%d&start=%s" what amount start)))
   (setq lipsum-list (xml-parse lipsum-buffer 7))
   (nth 1 (first (nth 2 (first (nth 2 (first lipsum-list)))))))


(lipsum "paras" 2 "true")
#2
Hi:



While working in a client site I had to port the  shell scripts used by the wmii window manager to newlisp. They are available at github.com. (https://github.com/papoanaya/wmii_newlisp">https://github.com/papoanaya/wmii_newlisp), if you want to play with them.



These programs uses newlisp to handle the wmii window manager; a stacking window manager which is available at http://wmii.suckless.org">http://wmii.suckless.org. The wmii window manager uses an external libixp wrapper for inter process communications.



Documentation is available at the github.com repository, hopefully it makes sense.



Luis
#3
newLISP in the real world / On Macros
April 21, 2012, 01:28:32 PM
Hi:

One post in comp.lang.lisp stated that "thou shall know macros to get thy mastery of ye LISP arcana", hence decided to explore macros for fun and amusement. Sadly here in the forum if you do a search on  macro (or define macros) nothing shows up, hence decided to write a small post about macros.



(define-macro ) in newlisp creates functions in which the arguments are not evaluated. In lay person terms, f you have a variable x with a value of 10, and x is being passed to a macro, then "x" is the value that is obtained from the argument and not 10.  You can look for fexpr in your favorite search engine for the details on implementation and how it differs from macro in common lisp.  More details can also be found in the newlisp manual.



Well,  this sounds nice and dandy, but what can you do with such thing.



I'm placing two examples of macros that come directly from the gnulisp manual. Only that they are implemented in newlisp (obviously :) )  It really does not have a lot of changes from the examples as written in the text.



The first one is  an implementation of the "inc" function using macros:




(define-macro (my-inc var)
  (eval (list 'setq var (list '+ 1 var) ))
)

(while (< i 10)
       (println (my-inc i))
)


The following must be noted:

[*] The macro is building code that will be evaluated. In this case, it is building

a list containing (setq i (+ 1 i))

  • [*] For the macro to execute, the created expression must be evaluated. If you

    substitute eval with println, you'll see aforementioned code. This is handy to

    debug macros.  An alternative of execution is to return the resultant list and

    modify it, but in this case, I wanted it only to execute; printing the numbers from 1 to 10.
  • [/list]

    The second example is a version of the for loop with syntactic sugar.




    (define-macro (mi-for var from init to final do body)
      (eval
      (list 'let (list (list var init))
            (cons 'while (cons (list '<= var final)
                               (append (list body) (list (list 'inc var))))))) )

    (mi-for i from 1 to 10 do
         (let ()
           (println i) )
    )



    [*] As you can see, it's another variation of the same theme. In this case

    it is building a while loop that will execute 10 times the content of the

    body.
  • [*] Being that the body is only one argument, it needs to be surrounded with a let

    function for all the statements to run.

  • [*] Eval is used to execute the code within the macro, but if substituting the eval with a println, this is what is obtained.
  • [/list]



    (let ((i 1))
     (while (<= i 10)
      (let ()
       (println i))
      (inc i)))


    This means that code can be built in a macro, returned from it and

    executed later on in your code.



    Hopefully this will help to those that are trying to understand these and hopefully find these examples useful.
    #4
    Hi again



    It's been a while, but I still use new lisp for various things as time permit. In this case this is more of a study in system or utilities  than anything else.



    One piece of software I've been dabbling of late is called dzen2. For more information you can go to http://sites.google.com/site/gotmor/dzen">//http://sites.google.com/site/gotmor/dzen. Dzen2 is a generic information application akin to conky, but it does not contain any intelligence in terms of monitoring. It accepts information from system input and display them in X. It has some auxiliary programs available for use that are used to create bars and menus.



    What I did was to write a small monitoring application for dzen in newlisp in which gathers monitoring data and display them into dzen through the use of pipes. The annotated code is here and hopefully it will make sense.



    One thing I noticed while writing this even though I've used perl and dabbled with scsh, newlisp is not slouch when it comes of handling output from command line tools in which a lot of output manipulation can be effectively done with fairly little code.  Hopefully it'll be evident in the enclosed program.



    #!/opt/home/papo/bin/newlisp
    ;; Appareance
    ;; NOTE: Although these can be used, the only one really in use is
    ;; SEPARATOR.

    (define BACKGROUND-COLOR "'#2c2c32'")
    (define ALTERNATE-BACKGROUND-COLOR "'#494b4f'")
    (define FOREGROUND-COLOR "'grey70'")
    (define SEPARATOR "^p(3)^r(3x3)^p(3)")
    (define FONT "fixed")


    ;; DZEN requires to have regular updates. Even though this is not use
    ;; right now it is set up every 10 seconds.

    ;; Main loop interval in seconds
    (define SLEEP-INTERVAL 1)


    ;; Signal handler to exit and not to get stuck in debug mode.

    (constant 'SIGINT 2)

    (define (ctrlC-handler)
      (exit)
    )

    (signal SIGINT 'ctrlC-handler)


    ;; This gets the results of df

    (define (get-disk)
      (setq df-results (exec "df"))
    ; The map is used to get the attributes that we want to monitor.
      (map (fn (x)
    (let ((y (parse x)))
      (list (nth 0 y) (nth 4 y))
    ))
    ; Using rest removes the header in one swoop.
    (rest df-results))
    )

    ;; There is a better way by writing using format, but I'm being lazy
    ;; This executes gdbar and get a graphical representation of a dzen bar.
    ;;

    (define (get-dbar fs per)
    ; By duing first, we get the string needed to write the bar

       (first (exec  (append "echo " per "| gdbar -l " fs) ))
    )

    ;
    ; This will iterate from the df results and only
    ; get the bar to the ones we care about. In this case
    ; only the ones mounted in cemnt. This can be changed, of course.


    (define (get-dbar-results)
      (let ((df-result (get-disk)) )
        (map (fn (x)
      (if (not (nil? (find "cemnt" (first x)) ))
          (get-dbar (last (parse (first x) "/"))  (nth 1 x)) ))
    df-result))
    )

    ;;
    ;; Same for the memory, get the results from free;
    ;;

    (define (get-free)
      (setq free-results (exec "free"))
    ; We only need the totals that are at the end.
      (parse (last free-results))
     )

    ;;
    ;; We get the bar by computing the percentage of
    ;; Free / Total
    ;; And get the resultant bar representation.
    ;;

    (define (get-mem-dbar-results)
      (setq free-result (get-free))
      (get-dbar "MEM:"
      (string (round (mul
    (div
     (float (nth 2 free-result))
     (float (nth 1 free-result))) 100 ) 0) ) )
    )


    ;
    ; Main
    ;

    (define (main)

    ; These pipes are used to communicate the results to the dzen2 process
    ; This way results are display without annoying blinks.
    ;

      (map set '(myin bcout) (pipe))
      (map set '(bcin myout) (pipe))

    ; Call dzen2 in the background, we only need bcin pipe.

      (setq pid (process "dzen2 -bg gray10 -x 200 -p -ta r  " bcin 0))

    ; Main loop, what it does is
    ; get the statistics and write them into
    ; the dzen2 process

      (while (not nil)
    (map (fn (x)
       (if (not (nil? (find "sd" x)))
    (write myout (format "%s " x))) )
      (get-dbar-results))
    (write myout SEPARATOR)
    (write myout " ")
    (write myout (get-mem-dbar-results))
    (write myout " ")
    ;
    ; dzen2 provides a gcpu bar.  In linux, you can create a process
    ; that reads from /proc. But I'm being lazy being program does the work.
    ; gcpubar works akin as vmstat, you need to get the second one
    ; to get an actual measure. In this case, there are 5 iterations
    ; and the last one is selected.

    (write myout (last  (exec "gcpubar -i 1 -c 5  ")))
    (write-line myout)

    ; Wait 10 seconds

    (sleep 10000)
    )
    ; Probably these should be moved in the signal handler.

      (destroy pid)
    )

    ;Start here.

    (main)

    #5
    I was dabbling one night and I decided to translate the code to create sequence diagrams from websequencediagrams.com from python to newLisp.



    The code might need some improvement, but it does work for me.





    ;;
    ;; Web Sequence Diagram Interface
    ;;

    (define (url-encode str)  
        (replace {([^a-zA-Z0-9])} str (format "%%%2X" (char $1)) 0))


    (define (url-decode str)
        (replace "+" str " ") ; optional
        (replace "%([0-9A-F][0-9A-F])" str (format "%c" (int (append "0x" $1))) 1))


    (define (getSequenceDiagram text outputfile (style "default") )
      (set 'request (list (eval 'text) (eval 'style)) )
      (set 'urladdress "http://www.websequencediagrams.com/index.php")
      (set 'urldata (join (list "message=" text "&style=" (url-encode style) )))
      (set 'result (post-url  urladdress urldata ) )
      (set 'expr (regex "(\?img=[a-zA-Z0-9]+)" result ))
      (if (nil? expr)
          (write-line "Something went wrong")
          (let ()
             (set 'image (get-url (join (list "http://www.websequencediagrams.com/" (first expr) ) )) )
             (write-file outputfile image)  
          )
      )
    )

    (getSequenceDiagram "alice->bob: authentication request n bob-->alice: response" "c:\out.png" "qsd")

    #6
    newLISP and the O.S. / New Lisp, Com? How
    October 25, 2005, 02:41:32 PM
    Hi:



    Even though NewLisp can load DLL's using the import function, is there any way to access COM components from newLisp?  Probably this would solve some of the short comings like like of ODBC by using Jet and to be able to access Office from newLisp.



    Regards,



    Papo
    #7
    Hi:



    I wrote the following small macro for syntax highlight.  It's very basic, but demonstrates how newLISP can be used when you are not in the mood of coding a lot of VBA code for mundane things like that. (I guess I could've used VBA string search routines, but I wanted to try it out anyway. :)



    Papo



    ' newlisp preable
    Public Declare Function dllEvalNewLISP Lib "c:program filesnewlispnewlisp.dll" Alias "newlispEvalStr" (ByVal LExpr As String) As Long
    Private Declare Function lstrLen Lib "kernel32" Alias "lstrlenA" (lpString As Any) As Long
    Private Declare Function lstrCpy Lib "kernel32" Alias "lstrcpyA" (lpString1 As Any, lpString2 As Any) As Long
    Private Declare Function LoadLibraryA Lib "kernel32" (ByVal s As String) As Long
    Private Declare Sub FreeLibrary Lib "kernel32" (ByVal h As Long)
    Dim NewLISPhandle As Long

    Function EvalNewLISP(LispExpression As String) As String
    Dim resHandle As Long
    Dim result As String
    resHandle = dllEvalNewLISP(LispExpression)
    result = Space$(lstrLen(ByVal resHandle))
    lstrCpy ByVal result, ByVal resHandle
    EvalNewLISP = result
    End Function

    'Gets a keyword from a list, returns "member" value
    Function GetKeyword(inkey As String)
        Dim statement As String
        Dim keywords As String
        keywords = "(setq kw '(expr strsub executesqlsimple strcat puts set mktime unixtime while for incr dbapi loaddriver if else elsif ))"
        statement = "(member ' " + inkey + " kw)"
        output = EvalNewLISP(keywords)
        output = EvalNewLISP(statement)
        GetKeyword = output
    End Function

    ' goes to a selection, if the keyword exists, then boldify, otherwise, leave
    ' alone

    Sub syntax_highlight()
       Dim CurrentString As String
       For i = 1 To Selection.Words.Count
          CurrentString = Selection.Words(i).Text
          If GetKeyword(CurrentString) <> "nil" Then
             Selection.Words(i).Bold = True
          End If
       Next i
    End Sub