Re: SelectionPlaceholder in Packages
- To: mathgroup at smc.vnet.net
- Subject: [mg90313] Re: SelectionPlaceholder in Packages
- From: David Reiss <dbreiss at gmail.com>
- Date: Sat, 5 Jul 2008 04:51:19 -0400 (EDT)
- References: <g4i8ue$38p$1@smc.vnet.net>
On Jul 3, 6:16 am, "David Park" <djmp... at comcast.net> wrote:
> I'm having a lot of difficulty in using a SelectionPlaceholder in a
> CreatePalette statement that is in a package. The problem is that it gets
> the package context attached to it and if I try to write a 'Global`' in
> front of it I obtain syntax errors when loading the package. Here is a
> preliminary 'in notebook' example that does what I want. I include sev=
eral
> features that are in my actual example such as the HoldFirst attribute an=
d
> Grid construction. I also include a Global` context on the placeholder ev=
en
> though it is not necessary here. The Defer is necessary to keep from
> displaying the evaluated version of foo.
>
> Attributes[foo] = {HoldFirst};
> foo[x_] := x^2
>
> TestPalette :=
> CreatePalette[
> Defer@Grid[{{PasteButton[foo[Global` \[SelectionPlaceholder]]]}}],
> WindowTitle -> "TestPalette",
> WindowMargins -> Automatic,
> ClosingAutoSave -> False]
>
> TestPalette;
>
> That all works. Now, here is a package version. Create a TestPalette f=
older
> in your private Applications folder and save the notebook there under the
> name TestPackage'.
>
> ___________________________________________________________
>
> Notebook[{
>
> Cell[CellGroupData[{
> Cell["TestPackage", "Section"],
>
> Cell[BoxData[
> RowBox[{
> "BeginPackage", "[", "\"\<TestPalette`TestPackage`\>\"",
> "]"}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{
> RowBox[{
> RowBox[{"foo2", "::", "usage"}], "=",
> "\"\<foo2[x] is a test function.\>\""}], ";"}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{
> RowBox[{
> RowBox[{"TestPalette2", "::", "usage"}], "=",
> "\"\<TestPalette2 will launch a palette to paste the foo function.=
\
> \>\""}], ";"}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{"Begin", "[", "\"\<`Private`\>\"", "]"}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{
> RowBox[{"Attributes", "[", "foo2", "]"}], "=",
> RowBox[{"{", "HoldFirst", "}"}]}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{
> RowBox[{"foo2", "[", "x_", "]"}], ":=",
> SuperscriptBox["x", "2"]}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{"TestPalette2", ":=", "\[IndentingNewLine]",
> RowBox[{"CreatePalette", "[",
> RowBox[{
> RowBox[{"Defer", "@",
> RowBox[{"Grid", "[",
> RowBox[{"{",
> RowBox[{"{",
> RowBox[{"Global`testsymbol", ",",
> RowBox[{"PasteButton", "[",
> RowBox[{"foo2", "[", "\[SelectionPlaceholder]", "]"}]=
,
> "]"}]}], "}"}], "}"}], "]"}]}], ",", "\[IndentingNewL=
ine]",
>
> RowBox[{"WindowTitle", "\[Rule]", "\"\<TestPalette2\>\""}], ",",
> "\[IndentingNewLine]",
> RowBox[{"WindowMargins", "\[Rule]", "Automatic"}], ",",
> "\[IndentingNewLine]",
> RowBox[{"ClosingAutoSave", "\[Rule]", "False"}]}],
> "]"}]}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{"End", "[", "]"}]], "Input",
> InitializationCell->True],
>
> Cell[BoxData[
> RowBox[{"EndPackage", "[", "]"}]], "Input",
> InitializationCell->True]}, Open ]]
> },
>
> AutoGeneratedPackage->Automatic,
> WindowSize->{607, 750},
> WindowMargins->{{Automatic, 191}, {Automatic, 62}},
> FrontEndVersion->"6.0 for Microsoft Windows (32-bit) (May 21, 2008)",
> StyleDefinitions->"Default.nb"
> ]
>
> ___________________________________________________________
>
> In the TestPackage Grid I include an extra Global`testsymbol object to sh=
ow
> that usually this construction works all right. Now if one loads the pack=
age
> and evaluates TestPalette2:
>
> Needs["TestPalette`TestPackage`"]
>
> TestPalette2
>
> the palette is displayed but it has the private package context on the
> selection placeholder. But if I use Global`\[SelectionPlaceholder] then I
> obtain a syntax error when reading the package. I can't find any way to g=
et
> it into the Global context.
>
> Does anyone know how to do this? Thanks in advance.
>
> --
> David Park
> djmp... at comcast.nethttp://home.comcast.net/~djmpark/
I have run into this issue as well at some point... But I don't
remember coming up with a fully general solution.
One approach, if feasible in your particular case, might be to write a
String version of the portion of the code that is problematic and then
wrapping it in ToExpression.
--David