Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Re: Viewing Packages in mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg102740] Re: Viewing Packages in mathematica
  • From: David Reiss <dreiss at scientificarts.com>
  • Date: Mon, 24 Aug 2009 08:37:02 -0400 (EDT)
  • Reply-to: dreiss at scientificarts.com

[This was originally posted to this thread when it first appeared but 
did not appear]

There are several points to make on this.  The first is that, 
generally you want to create your Mathematica cod within a 
Mathematica notebook (unless you are using Workbench) and then set 
that notebook up so that it's Input Cells or Code cells are 
Initialization cells.  Then the notebook will save them into a 
corresponding .m file when the notebook is saved (a dialog will ask 
you this the first time you save it after you have put initialization 
cells in it).

As for indentation, Code cells will not (by their stylesheet design) 
auto indent.  The can be changed at the style sheet level.

As for working with .m files directly.  They do not retain any 
stylsheet information when they are saved.  However styles can be 
imposed on them while they are open.

Cells with code in .m files make use of the Core.nb stylesheet's Code 
cellstyle.  So if you modify cells of this style in the .m file they 
will take on those modifications for the session that the .m file is 
open.

If nb is the NotebookObject for the .m file in question then the 
following will make it behave in a way that you are desiring, 
modifying the cells with code in them.  The code comes from 
http://scientificarts.com/worklife where it is used in one of the 
palettes for exactly the purpose you are wanting:  I needed the same 
thing and wrote it into the http://scientificarts.com/worklife 
package for just this reason.




ModifyCodeStyle[nb_NotebookObject, Selection] :=
  Module[{},
   NotebookFind[nb, "Code", All, CellStyle, AutoScroll -> False];
   SetOptions[NotebookSelection[nb],
    PageWidth -> WindowWidth,
    AutoIndent -> Automatic,
    AutoSpacing -> True,
    LineBreakWithin -> Automatic,
    LineIndent -> 1,
    LinebreakAdjustments -> {0.85, 2, 10, 0, 1}]
   ];


And here is a different version which acts on the style of Code cells 
rather than on the cells directly:

ModifyCodeStyle[nb_NotebookObject, Notebook] :=

   Module[{styledefs, editable},

    editable = Editable /. Options[nb, Editable];
    SetOptions[nb, Editable -> True];

    styledefs = StyleDefinitions /. Options[nb, StyleDefinitions];

    Switch[Head[styledefs],

     Notebook,

     styledefs = Notebook[
       {
        First@First[styledefs],
        Cell[StyleData["Code"],
         PageWidth -> WindowWidth,
         AutoIndent -> Automatic,
         AutoSpacing -> True,
         LineBreakWithin -> Automatic,
         LineIndent -> 1,
         LinebreakAdjustments -> {0.85, 2, 10, 0, 1}],
        Rest@First[styledefs]
        },
       Sequence @@ Rest[List @@ styledefs]
       ],


     String | FileName | FrontEnd`FileName,
     styledefs =
      Notebook[{Cell[StyleData[StyleDefinitions -> styledefs]],
        Cell[StyleData["Code"],
         PageWidth -> WindowWidth,
         AutoIndent -> Automatic,
         AutoSpacing -> True,
         LineBreakWithin -> Automatic,
         LineIndent -> 1,
         LinebreakAdjustments -> {0.85, 2, 10, 0, 1}]},
       StyleDefinitions -> "PrivateStylesheetFormatting.nb"]

     ];

    SetOptions[nb, StyleDefinitions -> styledefs];
    SetOptions[nb, Editable -> editable]

    ];


I hope this helps...

--David
http://scientificarts.com/worklife




On Aug 21, 4:42 am, Benjamin Hell <h... at exoneon.de> wrote:
>  Hi,
>  this is a real beginner question for packages in mathematica. When I
>  view a package (.m) file in mathematica, the mathematica editor doesn't
>  show any indentation nor does any automatic line breaks when scaling the
>  window. So whereas my code can be read quite well in a standard
>  mathematica notebook, it looks ugly in a package file. Is there any way
>  to setup some viewing options to change that behaviour?
>
>  Thanks in advance,
>  Benjamin


-- 

<><><><><><><><><>><><><>><><><>
    David Reiss, Ph.D;   Scientific Arts, LLC      .
    http://www.scientificarts.com
<><><><><><><><><>><><><>><><><>
This message and any attachments, may contain confidential and/or
legally privileged information.  If you are not the intended
recipient of the message by the original sender, please destroy it.
Message and attachments Copyright (c) 2009, all rights reserved.


  • Prev by Date: Re: Viewing packages in mathematica
  • Next by Date: Newsgroup problems
  • Previous by thread: Re: Re: Viewing packages in mathematica
  • Next by thread: Re: Re: Re: Viewing packages in mathematica