Formatting newLISP code: tools and conventions

For the Compleat Fan
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Formatting newLISP code: tools and conventions

Post by 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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

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

Good rules are in:
http://dept-info.labri.u-bordeaux.fr/~s ... ation.html

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

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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:

Code: Select all

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
Posts: 1390
Joined: Thu Sep 26, 2002 9:15 am
Location: Germany
Contact:

Post by HPW »

...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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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


Peter

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

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! :-)

Code: Select all

;; 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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

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
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
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

Thanks! Will check it out tomorrow...

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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

Code: Select all

(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
Posts: 408
Joined: Sat Jun 04, 2005 4:16 pm
Location: Russia
Contact:

Post by Dmi »

Thanks, Lutz!
http://en.feautec.pp.ru/store/nlc-1.3.tgz now using it.
WBR, Dmi

Locked