Editing Lisp Code

[From LotsofIrritatingSillyParentheses]

Out of interest, what editor do you guys use? I find LispLanguage syntax can be a pain when refactoring. When editing the last statement in a function it is easy to add or delete too many parentheses or but them in the wrong place. However, in general editing I don't find it a problem. I use XEmacs or DrScheme and both have parenthesis matching. If I can't see where my parentheses match it's a CodeSmell that a function is too large. -- NoelWelsh

I use the VimTextEditor. When e.g. adding another expression to a function or other implicit (begin ...), I go to the first opening parenthesis of the last form, then I press % to go to the matching closing parenthesis, then I press a, which will put me in insert mode right after the last expression. When deleting the last expression, I also go to the opening parenthesis and then do d%. Vim syntax highlighting will make it clear when parentheses are missing. Formatting the source with = will also indicate the problem. -- StephanHouben

I use GnuEmacs. I never have any parenthesis problems. I think the reason is that I do all my editing on s-expressions---never just on tokens. Extensive use of C-M-{u,n,f,b,k,t} means that you always yank and transpose delimited expressions. After than, M-q reindents the function, and your eye can immediately tell if anything moves "the wrong way". -- AlainPicard

Thanks Alain. I think that is the insight I've been missing. -- NoelWelsh

I use NotePad for most of my source editing, which explains why I write tools like Grayspace (see below). -- EdwardKiser


Many of the EmacsEditor commands are explicitly built to work with EssExpressions. These tend to be bound to the C-M-[] keymap. Thus forward-sexp, backward-sexp, kill-sexp, transpose-sexp, etc. These make editing Lisp almost completely automatic. Outsiders have a hard time believing that lisp programmers literally cannot see the parentheses anymore, but then, C programmers probably don't see the semi-colons any more (and they're a lot less regular!)


Well, the parentheses are clearly a feature. No experienced Lisp Hacker would want to live without them. Why? Because it makes editing source code so much easier.

Okay I give a hint what you could do with it.

Look at the following little and uninteresting code snippet: (expt (sin (sqr x)) x) . The whole thing is a Lisp expression and it has some nested subexpressions. So what are the parentheses doing there? They are grouping the expression(s). But as such they are a visual handle for the expression both at the beginning and the end of the expressions. So when you go there with your cursor you can use them to place direct manipulating editing commands there - especially when you use a mouse.

That is used by the LispMachine and for example Macintosh Common Lisp for a nice editing feature called 'mouse copy'. Let's explain the Macintosh Common Lisp version (which is way simpler than the Lisp Machine's sophisticated mouse support). Lisp programmers often like telling (long) names. As a typing aid, most Lisp development environments have a sophisticated completion facility.

So, how would one use that? Usually I have lot's of screen space with lost of context specific Lisp code visible. When I start to type a new function definition, I usually might type something like that (defun compute-complex-html-page (weblog articles combiner) ... Later I don't type those words anymore. I just set my cursor and command-click on the symbols and expressions to click my code together. Transposing, replacing, exchanging code then just works by clicking on the expressions - and where can you click on, when you want to click on an expression and issue a direct manipulation mouse command? On the parentheses. Voila.


I've actually developed two tools to deal with the parentheses in the SchemeLanguage:

They both have their problems, the way I wrote them. BuildSyntax requires you to write your programs in another notation and then compile that notation to Scheme. Grayspace has a cheezy lexical analyzer, and so gets confused by character constants, parentheses in strings, and the like, but I am finding I prefer it for actual use. I am careful to make sure that the confusing lexemes rarely occur, or that the parentheses cancel. -- EdwardKiser


What solutions are there for moving around lisp code? I've been programming java in eclipse, and have been finding the ability to walk through an api's source invaluable for learning about the api, implementation, dark corners of the language, etc; but I can't seem to find anything equivilant in lisp. I'm sure I'm missing something... I just don't know what.


Perhaps you want MetaPoint.


You mean for browsing code as HyperText? You can use tags tables in GNU Emacs. These are explained in http://www.gnu.org/manual/emacs/html_chapter/emacs_26.html#SEC334 Tags tables have been around for decades, first appearing in EmacsEditor on the IncompatibleTimeSharingSystem. They also work for other languages.

Incidentally, there is equivalent functionality in both ViEditor and NeditEditor?, though anyone who uses either for EditingLispCode deserves to lose. -- DonaldFisk


Another perspective on this matter (coming from a Smalltalk/XP/Forth viewpoint as well as the traditional Lisp viewpoint) would be that if you have trouble understanding what parentheses should go where, then you need to refactor your code. -- JosephDale


Also see ReadingLispCode.


CategoryLisp


EditText of this page (last edited April 17, 2006) or FindPage with title or text search