Formatting newLISP code: tools and conventions

Started by cormullion, December 14, 2005, 04:54:38 AM

Previous topic - Next topic

cormullion

How do you guys format your newLISP code? I'm not too fluent yet (and I think I keep changing my mind about what looks good), so I'd be interested to know what tools you use, and what conventions you stick to. Or perhaps you do it all by hand?

Dmi

#1
vim and emacs have a syntax helpers for hilight and indent.



Good rules are in:

http://dept-info.labri.u-bordeaux.fr/~strandh/Teaching/Langages-Enchasses/Common/Strandh-Tutorial/indentation.html">//http://dept-info.labri.u-bordeaux.fr/~strandh/Teaching/Langages-Enchasses/Common/Strandh-Tutorial/indentation.html



And, rest of all, I'm looking for a newlisp function for fine indenting of the code.
WBR, Dmi

Lutz

#2
I see people moving slowly away from the "now new-line never preceeds closing paren" - rule to using 'C' style of identation. Not only because of better parenthesis matching but because it is easier to change code:

1: (define (f x)
2:   (when (< (g x) 3)
3:      (h x 2)
4:   )
5:   (foobar g h)
6: )


It is much easier to delete or insert before or after lines 3 and 5 when the closing parenthesis are on a different lines. You can take out or insert a new line without worrying much about destroying parethesis balance.



Personally I started out using the classic conventions like described in your linked document, but see myself and others going more the the style shown in this post. Specially if you work in a group of programmers modifying other programmers code.



Lutz

HPW

#3
Quote...but see myself and others going more the the style shown in this post.


I agree with that. I use this formating mainly with the help of ultraedit.

It simply help to manage lots of code and the  parethesis balance checker shows clean blocks. Also function detection is supported when you follow some rules, showing the function header-line in a navigation window.
Hans-Peter

pjot

#4
By the way, there is a nice trick with VIM to check your paranthesis: move your cursor to a '(' and then press the percentage symbol '%' in command mode. VIM will move you to the corresponding closing ')'. Of course this also can be done the other way around.



Peter

cormullion

#5
Trouble is, the last line of a function I've just been working on is:



    (format "%s/%s" folder item )) dupe-list -1 ))))))



which would look a bit odd if each closing parenthesis were on a new line... Or perhaps I'm just writing bad Lisp!



Obviously any good editor will help you balance the parentheses as you're working, but I'm surprised no-one has whipped up a quick 'lint' or 'tidy' function to tidy up a file that's got out of hand: it would have to keep track of the nesting level and 'tab' the parentheses out. I'll have a go sometime.

Dmi

#6
In Vim, when I need to replace some function call, I press v% on the one of it's parens and got selected whole function, that can then be deleted or changed fastly.



When I started programming in newlisp, I used closing parenthesis one by a line. I found this unuseful because:

- functional style leaves really tons of closing parens at the end of each sentence.

- indenting of the start of the sentences is quite for marking the logical structure.

- separating the closing paren gives none additional information for code readability (and makes it more hard, imho).



When I want to close some number of parenthesiss, I simply visually go through the number of indentions and add closing paren for each one - it's really simple.
WBR, Dmi

pjot

#7
Quote
but I'm surprised no-one has whipped up a quick 'lint' or 'tidy' function to tidy up a file that's got out of hand


I made a simple and small function some time ago just to check the paranthesis, maybe it helps you. It's noting fancy, but it sure helped me a lot:



http://www.turtle.dds.nl/newlisp/check.lsp">http://www.turtle.dds.nl/newlisp/check.lsp





Peter

cormullion

#8
Thanks - interesting script!



With the Balance Parentheses command in BBEdit I don't have so much of a problem with balancing. It's more the business of lining the things up and formatting that I find hard.



I had a go at writing a newLISP program, but I didn't get very far before I realised how difficult it was going to be when there were strings, macros, not enough space on the line, etc.  So for now I've given up! :-)



;; don't run this on your nicely formattted newLISP documents!!!
(set 'indent 2) ; number of spaces for each level
(define
  (spaces-for-level level )
(string (dup (dup " " indent) level)))

(dolist (file-name (2 (main-args))) ; or (main-args 2)?
    (set 'file (open file-name "read"))
    (set 'level 0 )
    (while (read-line file)
    (dolist (c (explode (current-line)))
      (cond
      ((= c "(") (begin
      (inc 'level)
      (print "n" (spaces-for-level level ) "(")))
      ((= c ")") (begin
      (dec 'level)
      (print  ")" )))
      (true (print c))
      )))
    (close file))
(exit)
[/code]

Dmi

#9
Yesterday I wrote an indenting script for my newlisp console.

Check file indent.lsp in

http://en.feautec.pp.ru/store/nlc-1.2.tgz">//http://en.feautec.pp.ru/store/nlc-1.2.tgz

About 45 lines + function dictionary.

Features are:

- indenting by 2 spaces for dictionary functions

- indenting by space after the end of function name for other functions

- indenting next by an open bracket for data lists

- indenting closing bracket just under corresponding opening one

- comments (;) and strings ("") are recognized



usage example:

(map load '("funlib.lsp" "indent.lsp"))

(join (INDENT:indent (parse (read-file "your-file") "n")) "n")
WBR, Dmi

cormullion

#10
Thanks! Will check it out tomorrow...

Lutz

#11
you can use this library import routine to automatically adapt to the OS you are running on:

(if
        ;; LINUX and FreeBSD
        (< (& 0xF (last (sys-info))) 3) (set 'library "/usr/lib/libncurses.so.5")
        ;; Mac OSX / Darwin
        (= (& 0xF (last (sys-info))) 3) (set 'library "/usr/lib/libncurses.dylib")
        ;; Solaris
        (= (& 0xF (last (sys-info))) 4) (set 'library "/usr/lib/libcurses.so.1")
        true (println "Cannot load library, OS not supported"))


Lutz



ps: NetBSD uses /usr/libcurses.so.5 or 6, OpenBSD don't know at the moment

Dmi

#12
Thanks, Lutz!

http://en.feautec.pp.ru/store/nlc-1.3.tgz">//http://en.feautec.pp.ru/store/nlc-1.3.tgz now using it.
WBR, Dmi