6

How can I open a package as a normal notebook, i.e. without the toolbars and formatting of the built-in .m file interface.

M.R.
  • 31,425
  • 8
  • 90
  • 281

2 Answers2

5

Upon opening you can change the stylesheet and remove the docked cell:

SetOptions[EvaluationNotebook[], DockedCells -> {},StyleDefinitions -> "Default.nb"]

Then if you want to change the "Code" styles to something else, possibly "Input", do this:

NotebookFind[EvaluationNotebook[], "Code", All, CellStyle] 
FrontEndExecute[FrontEndToken["StyleOther"]]
Mike Honeychurch
  • 37,541
  • 3
  • 85
  • 158
  • It doesn't seem to save, is there a way around running this command manually every time I open the notebook? – M.R. Feb 08 '15 at 22:38
  • Well perhaps that is a separate question - how to run a bit of code every time a notebook opens... what do you think? – M.R. Feb 08 '15 at 22:39
4

Here is one way. Using the parseString function from this answer of Mr.Wizard, the following will create a notebook with an entire package's code being placed in a single Code - style cell:

parseString[s_String, prep : (True | False) : True] := 
   FrontEndExecute[UndocumentedTestFEParserPacket[s, prep]]

ClearAll[openPackage];
openPackage[file_String?FileExistsQ] := 
  With[{stringCode = Import[file, "String"]},
     CreateDocument[
        Cell[First@parseString[stringCode, False], "Code"]
     ] /; stringCode =!= $Failed
  ]

For example:

openPackage@FileNameJoin[{FileNameDrop[FindFile["Combinatorica`"], -2], "Combinatorica.m"}]

You can split the code into multiple cells, but that would require a bit more work.

Leonid Shifrin
  • 114,335
  • 15
  • 329
  • 420