MathGroup Archive 2012

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

Search the Archive

Re: Retrieving pasteable function definition without contexts preprended

  • To: mathgroup at
  • Subject: [mg123945] Re: Retrieving pasteable function definition without contexts preprended
  • From: Szabolcs Horvát <szhorvat at>
  • Date: Sun, 1 Jan 2012 02:28:45 -0500 (EST)
  • Delivered-to:
  • References: <jd4fdh$da8$> <jdh6j4$kmd$>

On 2011.12.29. 8:58, A Retey wrote:
> Hi,
>> I'm essentially asking the same question that E. Martin-Serrano has
>> asked this February (message quoted at the end):
>> What is the best way to print the definition of a package symbols
>> without contexts, so it's more readable?
>> To his question (what is a substitute to Developer`ContextFreeForm), we
>> can use Block[{$ContextPath = Append[$ContextPath =
>> "contextIdontWantPrinted`", FullDefinition[symbol]]
>> This method still has a problem though.  The definition that's printed
>> cannot be copied directly because quotation marks are missing.  I would
>> like to copy it to Workbench for formatting.
>> Take for example the definition of RunThrough which contains
>> WriteString[tmpfile, "\n"].  The "\n" part won't show at all.
>> Are there any other solutions?  Do you have any other suggestion about
>> making it easy to read the code of already defined function (when we
>> don't have access to the source file)?
>> Note: I also asked about this here
>> <>   and got an alternative
>> answer to pasting to Workbench for formatting.
> I'm not sure whether this is really what you need, but from your
> description you are only missing an InputForm in the
> stackoverlfow-suggestion. The following gives code that can be copied
> and pasted to workbench:
> Unprotect[RunThrough];
> ClearAttributes[RunThrough, ReadProtected]
> Block[{$ContextPath = Append[$ContextPath, "System`Dump`"]},
>    Print[InputForm[FullDefinition[RunThrough]]]]

Thank you, this indeed solves the problem!  I am not sure why I thought 
that InputForm doesn't affect FullDefinition ...

It's also important to note that Print[] is essential here.  Without it 
the context names are not removed.

Based on this, here's a function to print definitions with the commonest 
context removed:

commonestContext[sym_Symbol] :=
      	Level[DownValues[sym], {-1}, HoldComplete],
         s_Symbol /; FreeQ[$ContextPath, Context[s]] :> Context[s]
      ], 1]

contextFreeDefinition[sym_Symbol] :=
         ClearAttributes[sym, ReadProtected];
         Block[ {$ContextPath =
                   Join[$ContextPath, commonestContext[sym]]},

Example use:


Szabolcs Horvát
Mma QA site proposal:

  • Prev by Date: Re: beginner question regarding units in equations
  • Next by Date: Re: PairedBarChart "Education and Training pay..."
  • Previous by thread: Re: beginner question regarding units in equations
  • Next by thread: Re: PairedBarChart "Education and Training pay..."