Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

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

Search the Archive

Re: Add new option to Notebook[]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55330] Re: Add new option to Notebook[]
  • From: "Peltio" <peltio at trilight.zone>
  • Date: Sat, 19 Mar 2005 04:45:54 -0500 (EST)
  • References: <d1bgtp$lrr$1@smc.vnet.net>
  • Reply-to: "Peltio" <peltioNOSPAM at despammed.com.invalid>
  • Sender: owner-wri-mathgroup at wolfram.com

"rod at flyingspinach.com" wrote:

>I'd like to add a new option to Notebook[], so that I could write
>Notebook[{...}, NewOption->"new_option_string"].  Then I would be able
>to say Options[Notebook, NewOption] and get back the string
>"new_option_string".  Can this be done?

Yes, "it... could... work..." (cit.) but it needs some coding.
Several months ago I wrote a notebook with some of the techniques needed to
override built-in functions (it arose as a natural spinoff of the second
version of a package for manipulating symbolic sums - I even fancied about
submitting it to the Mathematical Journal -or, more modestly, to the
Mathsource- but since I did not find the time to polish it and to write a
reasonably robust code-generating procedure I made nothing of it).
There are ways to:
    1. Recursively redefine built-in procedures
        (for example, automatically rescale Fourier's output)
    2. Add options to built-in procedures without options
        (to mimick in version 3 the new options given to Fourier,
         for example)
    3. Add options to built-in procedures with options
        (to add a ShowPoints option to Plot and ListPlot in order to
        see the points along with the line joining them, or to add a
        Mesh->False option to ParametricPlot3D)
    4. Manage the applying of functions to redefined built-in objects
        (like the derivation or integration of Sums)
    5. Switching on and off the builtin behavior

So, your request falls under point three. Basically, you have to unprotect
Notebook, Options and SetOptions. You will see that I am keeping user
defined options in a different basket that will be used only when needed.
By stripping, copying and pasting the code I used for changing ListPlot, and
using Notebook instead of ListPlot I can offer the following:

================================
Needs["Utilities`FilterOptions`"]

$override = True; Protect[$override];
Unprotect[Notebook, Options, SetOptions];
Notebook[argsandopts___ /; $override == True] :=
     Block[{$override = False},
       myNotebook[argsandopts]
       ];
Options[myNotebook] = {NewOption -> "new option string"};
Options[Notebook] /; $override == True :=
    Block[{$override = False},
       Join[Options[Notebook], Options[myNotebook]]
      ];
SetOptions[Notebook, opts__] /; $override == True :=
  Block[{$override = False},
    Join[SetOptions[Notebook, FilterOptions[Notebook, opts]],
      SetOptions[myNotebook, FilterOptions[myNotebook, opts]]
      ]
    ]
Protect[Notebook, Options, SetOptions];
======================================

This is a test for the new options

    NewOption/.Options[Notebook]
        (*this should give you "new option string" *)

    SetOptions[Notebook,NewOption->"whaddayathink"];
    NewOption/.Options[Notebook]
        (*this should return "whaddayathink"*)

As long as $override is set to True every call to Notebook will be diverted
to myNotebook (I had no way to check this for this particular procedure; I
wrote the code to modifiy procedures that don't mess with the frontend and
Notebook could be unmanageable from this point of view - you tell me : ) )
All you have to do now is to write the code for myNotebook, taking advantage
of the newly added option. You can call the builtin notebook from within
your procedure without incurring in an infinite recursion (because the Block
with $ovveride set to false set it off).

For example you could write something like this (I'm writing this off the
top of my head, so don't take it too literally).

    myNotebook[args__,opts___Rule]:=
        Module[{myopt,origopts},
            myopt=NewOption/.{opts}/.Options[myNotebook];
            (*please note that you are using myNotebook options, here
             - the other basket : )  *)
            Which[
                myopt=="new option string", doSomething[args,opts],
                myopt=="whaddayathing", do Somethingelse[args,opts],
                origopts=FilterOptions[Notebook,opts];
                True,Notebook[args,origoptsopts]
             ];
    ]

In general you would want to filter the options to pass to the builtin
Notebook[] and those to pass on to your other user-defined procedures.

The explanation looks a little messy mostly because I had to strip a lot of
stuff (I completely omitted points 1 and 2); maybe someday I will find
some spare time to finish out the notebook and the package Override.m.
I wonder when. : )

cheers,
Peltio
invalid address in reply-to. Crafty demunging required to mail me.


  • Prev by Date: Re: nonLinearFit and nonLinearRegress
  • Next by Date: Re: nonLinearFit and nonLinearRegress
  • Previous by thread: Re: Add new option to Notebook[]
  • Next by thread: functional programming excercise from Mastering Mathematica