Re: Add syntax highlighting to own command
- To: mathgroup at smc.vnet.net
- Subject: [mg101602] Re: Add syntax highlighting to own command
- From: Peter Breitfeld <phbrf at t-online.de>
- Date: Fri, 10 Jul 2009 23:23:38 -0400 (EDT)
- References: <h340tk$guu$1@smc.vnet.net>
Bastian,
you should make a file Bastian.m (or so) to look like this:
----cut---------8<----start-------8<----------------
(* :Context: Bastian` *)
BeginPackage["Bastian`"]
Let::usage="Let[vars, expr] does ... "
Begin["`Private`"]
Attributes[Let]={HoldAll};
SyntaxInformation[Let]={"ArgumentsPattern"->{_,_}};
Let[vars_, expr_]:= <your code ... >
SetAttributes[Let,ReadProtected]; Protect[Let];
End[]
EndPackage[]
----cut---------8<----end---------8<----------------
Put this file in the Folder $UserBaseDirectory/Applications.
Look for the file $UserBaseDirectory/Kernel/init.m
If it doesn't exist, create it.
Open it with an editor and add the line
Get["Bastian`"]
Now, your file will always be loaded on startup.
To your other questions:
1. Yes, it's a bad idea to give your own functions the context System`.
Better create your own Bastian.m file and put all your functions in
it; they will have the context Bastian`. (Such a file tends to grow
over time :-))
2. Syntax Highlighting is done with the line SyntaxInformation above.
3. I can't improve your code.
Bastian Erdnuess 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
>
--
_________________________________________________________________
Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de