# publicleoalekseyev/mma-context

### Subversion checkout URL

You can clone with HTTPS or Subversion.

  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 1 * Project information 2 3 This package provides convenience functions for easier Mathematica context 4 management. Mathematica's namespace is, by default, global and shared across 5 notebooks. This, in most cases, is very inconvenient and can easily lead to 6 errors. See [[http://stackoverflow.com/a/4897013/133234][this StackOverflow post]] for discussion of these issues. 7 8 It is recommended that every notebook is set to use a separate namespace. 9 This is ordinarily accomplished via =SetOptions[EvaluationNotebook[], 10 CellContext -> Notebook]=. 11 12 This package provides a wrapper around this function, as well as a way to 13 easily access this private context from other notebooks. 14 15 * Mathematica package installation 16 17 To load a package in Mathematica, execute the following in the front end: 18 19 : AppendTo[$Path, "/path/to/package/dir"];  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 20 : Needs["PackageName"]; (* don't forget the backquote *) 21 22 Note: under Windows, the path will involve double backslashes, e.g. 23 24 : AppendTo[$Path, "c:\\path\\to\\package\\dir"] 25  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 26 27 To autoload the package, append the above lines to init.m in 28 : $UserBaseDirectory/Autoload/Kernel/init.m  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 29 e.g. ~/home/leo/.Mathematica/Autoload/Kernel/init.m~ 30  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 31 (To obtain exact location of$UserBaseDirectory, evaluate $UserBaseDirectory 32 in mma.) 33 34 * How to use these functions 35 36 The package provides the following functions: 37 - =setPrivate= 38 - =spawnScratch= 39 - =setAutosave= 40 - =ExposeContexts= 41 42 Docstrings can be accessed from Mathematica in the usual manner by entering 43 e.g. =?setPrivate= in the notebook. 44 45 ** setPrivate 46  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 47 Usage: =setPrivate[]=  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 48 49 Creates a private context by calling =SetOptions[EvaluationNotebook[], 50 CellContext -> Notebook]= and replaces the "setPrivate" notebook input string 51 with the "SetOptions..." command string. 52 53 Resultant assigned context can be checked via =$Context= or =$ContextPath=. 54  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 55 Alternative forms: *setPrivate["foo"] or setPrivate[{"foo","bar"}]*  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 56 57 Functions as above, but adds contexts "foo" or "foo", "bar" to the 58 =$ContextPath=. 59 60 ** spawnScratch 61  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 62 Usage: =spawnScratch[]=  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 63 64 Opens a new notebook with a private context, but its =$ContextPath= will 65 include the context of the invoking notebook (i.e. the notebook where 66 "spawnScratch" was entered). The "spawnScratch[]" command cell will disappear 67 from the invoking notebook so as not to pollute the workspace. This is 68 extremely convenient when one wants to perform throw-away calculations with 69 the current notebook's symbols. The scratch notebook can, of course, be 70 saved. 71 72 ** setAutosave 73  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 74 Usage: =setAutosave[]= or =setAutosave[True]=; =setAutosave[False]=  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 75 76 This is a convenience wrapper around 77 =SetOptions[SelectedNotebook[],NotebookAutoSave->True|False]=. 78 79 ** ExposeContexts 80  3fe377e8 » Leo Alekseyev  2012-01-11 Reformatted README 81 Usage: =ExposeContexts[{"foo","bar"}]=  34e79019 » Leo Alekseyev  2012-01-11 Initial commit These functions were split off from my collection of M… 82 83 Appends contexts to =$ContextPath=.  3fe377e8 » Leo Alekseyev ` 2012-01-11 Reformatted README 84 85 * Fancy autoreplacement hack 86 87 In my Mathematica installations, I invoke the above functions by typing 88 =_private= (which automatically expands to =setPrivate[]=) and =_scratch= 89 (which automatically expands to =spawnScratch[]=). This is accomplished by 90 tapping into the InputAutoReplacements mechanism that is ordinarily 91 responsible for replacing ~->~, ~:>~, etc. with fancy arrows in notebooks. 92 Below are my notes on how I set it up. Instructions are provided 93 as is; use them at your own risk, and make sure to back up any system files if you choose 94 to modify them :) 95 96 : Go to Format->Edit Stylesheet 97 : This will pull up a notebook of sorts. Copy the following Cell[] code: 98 : Cell[StyleData["StandardForm"], 99 : InputAutoReplacements->{ 100 : "_private" -> "setPrivate[]","_scratch" -> "spawnScratch[]", 101 : "->" -> "\[Rule]", ":>" -> "\[RuleDelayed]", "<=" -> "\[LessEqual]", ">=" -> 102 : "\[GreaterEqual]", "!=" -> "\[NotEqual]", "==" -> "\[Equal]", ParentList}] 103 : 104 : Paste the above cell (you can make other additions) into that 105 : style-editing notebook thing. Click "Install"; choose a filename. That 106 : name will now show up in the Stylesheets menu. To apply this as a default, 107 : choose the name of an existing stylesheet (like Default.nb). After 108 : restart, the autoreplacements will work for all notebooks using that 109 : stylesheet. (Under linux, should have a file under 110 : /home/leo/.Mathematica/SystemFiles/FrontEnd/StyleSheets/Default.nb. Check 111 : that this file is present, correctly named, and has the necessary code if 112 : problems arise.)