Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Preserve notebook's $ContextPath between sessions /

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104993] Re: [mg104967] Preserve notebook's $ContextPath between sessions /
  • From: Leo Alekseyev <dnquark at gmail.com>
  • Date: Tue, 17 Nov 2009 05:13:41 -0500 (EST)
  • References: <200911151056.FAA09284@smc.vnet.net>

John, thanks for your response.

Since there can be many session-dependent volatile contexts, it
probably wouldn't be a good idea to save them all by default -- which
is probably why that feature isn't there.  On the other hand, it would
be nice to offer the user a greater control over this: for instance, I
am simply trying to avoid doing

SetOptions[EvaluationNotebook[],CellContext->Notebook];
Needs/@{"foo`","bar`"} (* which have already been autoloaded by
init.m, but are now hidden *)
AppendTo[$ContexPath,"Global`"]; (* in some sessions it's good to have
a "read-only" access to global context *)

at the start of every notebook that I want to set up like that.
Specifying a "Other..." context as you suggested doesn't solve this
problem because the packages and loaded contexts are still hidden.

I am just surprised there isn't an easy mechanism to save/retrieve the
context info.

By poking around I noticed there's a variable
Global`Private`SavedContextInfo["$ContextPath"].
However, it looks like the SavedContextInfo variables cannot be made
persistent -- correct me if I am wrong.

I tried writing the following wrapper function:

test2[] := (SetOptions[EvaluationNotebook[],
   CellContext -> Notebook]; (AppendTo[$ContextPath, #1] &) /@
   Global`Private`SavedContextInfo["$ContextPath"]; $ContextPath =
   DeleteDuplicates[$ContextPath])

However, it appears that only the SetOptions[EvaluationNotebook[],
CellContext -> Notebook] command takes effect when written like this
*unless invoked twice in two separate cells with a fully-qualified
symbol name.  This seems kind of inelegant (and trying to do this
voodoo programmatically by manipulating notebooks with kernel
functions is ugly and doesn't work well).  I am still curious if there
is a good way around this.

--Leo


On Mon, Nov 16, 2009 at 10:44 AM, John Fultz <jfultz at wolfram.com> wrote:
> Yes, that's right.  Mathematica is not just changing the context, but t=
rying to
> sandbox a number of evaluation side effects from notebooks which are oper=
ating
> in another context, including...
>
> * $Context (of course)
> * $ContextPath
> * The state of In[]/Out[] numbering
> * The Out[] history
>
> In a notebook set with a unique notebook-level context, no attempt is mad=
e to
> recover the same context when you close and open the notebook again, and =
the
> context you used isn't stored anywhere in the file.  I don't think that=
 occurred
> to anyone here when designing the feature, and I'm not entirely certain w=
hether
> it would be generally desirable to do so.
>
> But, what you want to do is easy enough.  Instead of specifying a
> notebook-unique context, specify a named context of your choice (i.e., th=
e
> "Other..." option).  Everything will be remembered as long as it's usin=
g the
> same context name, and by hard-coding the name, you can guarantee that. =
 Down
> side...you'll have to make sure you pick a unique name.  But that's pro=
bably not
> so difficult...you could choose a name based upon the file name, for exam=
ple.
> You could write a button which does this, too.  I'll leave that as an e=
xercise
> for the reader.
>
> Sincerely,
>
> John Fultz
> jfultz at wolfram.com
> User Interface Group
> Wolfram Research, Inc.
>
>
> On Sun, 15 Nov 2009 05:56:32 -0500 (EST), dnquark wrote:
>> I have fallen into the habit of making the default context of the
>> notebooks private by issuing the command
>> SetOptions[EvaluationNotebook[], CellContext->Notebook]
>> at the start of every notebook session.  However, I typically add
>> several other contexts to $ContextPath; I even wrote a utility
>> function to easily do all this automatically.
>>
>> It turns out, though, that upon closing and reopening the notebook
>> Mathematica puts only "Notebook$$xx`" and "System`" contexts in
>> $ContextPath.  My question is -- is there a way to make _my custom_
>> $ContextPath persistent between sessions in the same way as {"Notebook$
>> $xx`", "System`"} context list is persistent?..  Or at least, can I
>> store custom metadata with the notebook -- i can always write a
>> wrapper function to retrieve it.
>>
>> Thanks,
>> --Leo
>
>
>
>


  • Prev by Date: Re: Fyi, Listing of Mathematica 7 functions (1869 functions)
  • Next by Date: Re: Re: Mathematica skill level snippet(s)
  • Previous by thread: Preserve notebook's $ContextPath between sessions / store custom
  • Next by thread: Re: Preserve notebook's $ContextPath between sessions / store custom