MathGroup Archive 2009

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

Search the Archive

Re: Code folding and wrapping using the input->code method?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104042] Re: Code folding and wrapping using the input->code method?
  • From: David Reiss <dbreiss at gmail.com>
  • Date: Fri, 16 Oct 2009 07:21:48 -0400 (EDT)
  • References: <hb70a0$8rs$1@smc.vnet.net>

Take a look at the following MathGroup posting of mine that addresses
this:

http://groups.google.com/group/comp.soft-sys.math.mathematica/browse_thread/thread/fc042346d1fc78fd/83d3d8ea7d9d67ea

I also reproduce that posting below....

Best,

David
http://scientificarts.com/worklife

__________________________________________
Original posting:
__________________________________________

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.


On Oct 15, 7:16 am, telefunkenvf14 <rgo... at gmail.com> wrote:
> Does anyone know how to make Mathematica both fold and wrap code
> input?
>
> I really like using the code input option (because it automatically
> generates an autosave package for you, and I like the way it sets the
> code off from the rest of your work in a notebook), but the lack of
> folding and wrapping are driving me NUTS!
>
> -RG



  • Prev by Date: Piecewise and Evaluated If Not Equivalent (at least not within
  • Next by Date: Re: Coefficient Question
  • Previous by thread: Code folding and wrapping using the input->code method?
  • Next by thread: Re: Code folding and wrapping using the input->code method?