MathGroup Archive 2009

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Add syntax highlighting to own command

  • To: mathgroup at smc.vnet.net
  • Subject: [mg101576] Re: [mg101539] Add syntax highlighting to own command
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Fri, 10 Jul 2009 06:47:34 -0400 (EDT)
  • References: <200907090600.CAA17547@smc.vnet.net>

Hi Bastian,

While I won't be able to authoritatively answer most of  your questions,
I would not place your function into a System` context. What
I would do is to place your function into a separate package with its
own context, and make that package autoloadable. I am not a big expert
on this, I am pretty sure you will get great advice from other people here.
As for the highlighting, I would also very much like to learn if this can be
extended to user-defined functions.

Regarding your code, I think it is a very clever piece. I don't know why
you would want to improve it - it seems quite a piece of art to me. I was
first somewhat surprised that it works. I think it very cleverly uses the
fact that  RuleDelayed, being a scoping construct, does not respect the
possible colliding variables in inner scoping constructs (like With here)
and in this respect is different from other scoping constructs such as
Module, Block, With, or Function (thus your substitution works as intended,
instead of internal variables simply being renamed).

Sorry if I wasn't exactly helpful.

Regards,
Leonid





On Wed, Jul 8, 2009 at 11:00 PM, Bastian Erdnuess <earthnut at web.de> wrote:

> I was missing a scoping construct like 'With' but where the local
> variables get assigned linearly.  E. g.
>
>  In[1] := LinearWith[ {
>               a = "Hi, ",
>               b = a <> "there!" },
>             Print[ b ] ]
>
>  Out[1] = "Hi, there!"  .
>
> I'm fairly new to Mathematica, but I know some Lisp, and somehow I got
> it.  I called my new construct Let and defined it as:
>
>  Let[ vars_, expr_ ] :=
>    If[ Length[ Unevaluated[ vars ] ] == 1,
>      With[ vars, expr ],
>    (* else *)
>      Unevaluated[ vars ] /.
>        { a_, b__ } :>
>          With[ { a },
>            Let[ { b }, expr ] ] ]
>
>  SetAttributes[ Let, HoldAll ]
>
> It seems to work fine so far.
>
> Now, I would like to have this construct load always when I start
> Mathematica and I don't want to get it cleared when I use
> 'Clear["Global`*"]'.  So I put it in the System` context and also added
> the attribute 'Protected'.  I wrote all in a file Let.m and now, I
> wonder where to put it that it gets read automatically at each startup
> of Mathematica.
>
> However, my first question: Is it a bad idea to add things to the
> System` context?  And if not, where to put my file?  And if, what would
> be better?
>
> Second, my main question: Is it somehow possible to add this nice syntax
> highlighting to the Let construct like with the With construct, where
> the local variables appear in green?
>
> Third: Can I somehow add a help page?  I have already the Let::usage.
>
> And last: Does anybody know how to make the construct better?  Is there
> something like syntax transformation rules in Mathematica?
>
> Thank you for your answers,
> Bastian
>
>


  • Prev by Date: Re: 2dFFT & image processing
  • Next by Date: Re: stirring chocolate pudding backwards: identifying coordinates
  • Previous by thread: Add syntax highlighting to own command
  • Next by thread: Re: Add syntax highlighting to own command