Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I am writing my code documentation and need to list all the Mathematica built in functions I have used - explicitly written functions only. Is there a way to extract a list of all built in functions that I have used in my code?

For example

Insert[{a, b, c}, x, 3]
a=1
Style["this text",Bold]
Range[1,20]
b=3
a*b

I would require a list containing Insert, Style and Range.

share|improve this question
    
You might also anticipate getting List, Set, and Times, since a=1 for example is equivalent to Set[a,1]. –  Mark McClure May 21 at 10:15
    
As noticed in comments you should probably explain what do you mean. 1) only explicitely written functions? 2) what is function –  Kuba May 21 at 10:49
    
@Öskå must be v9, Kuba's answer worked for the OP. –  Jacob Akkerboom May 21 at 11:05
    
Lara, would you consider accepting an answer? –  Jacob Akkerboom Nov 2 at 12:32

3 Answers 3

up vote 5 down vote accepted

Perhaps something like this:

systemSymbolsInNotebook[file_String] := Module[{n, cells, exprs, syms},
  n = NotebookOpen[file];
  SelectionMove[n, All, Notebook];
  cells = NotebookRead[n];
  NotebookClose[n];
  exprs = MakeExpression[First@#] & @@@ cells;
  syms = Cases[exprs, s_Symbol :> SymbolName@Unevaluated@s, {3, -1}, Heads -> True];
  Intersection[syms, Names["System`*"]]]

systemSymbolsInNotebook["test.nb"]
(* {"Bold", "Insert", "List", "Range", "Set", "Style", "Times"} *)
share|improve this answer
1  
Even if OP agrees with non explicitly used functions as Times or Set I don't think Bold should appear there. –  Kuba May 21 at 10:22
    
That's nice - +1 –  rasher May 21 at 10:25
    
This doesn't seem to work. I had a notebook with NotebookPut, Get and some other things and the results I got from using this code were {"CellChangeTimes", "List", "Rule", "TextCell"} –  Lara Jordan May 21 at 10:37
    
@LaraJordan, ah okay. I only tested it with the simple example in the question. –  Simon Woods May 21 at 10:59
functions[nb_] :=
 DeleteDuplicates[
  Join @@
     Extract[
      #
      ,
      Position[
       #
       ,
       x_Symbol /; Context[x] == "System`"
       ]
      ,
      HoldComplete
      ] &[
   ToExpression[Unevaluated[#], StandardForm, 
      HoldComplete] & /@ (NotebookRead@Cells[nb, GeneratedCell-> False])[[All, 1]]]
  ]

For your specific case, where you don't want symbols that never occur as the head of an expression, you could use the following.

functionsSpecific[nb_] :=
 DeleteDuplicates[
  Join @@
     Extract[
      #
      ,
      Position[
       #
       ,
       x_Symbol /; 
        Context[x] == "System`" &&
         MatchQ[SyntaxInformation[x], _[___, 
           "ArgumentsPattern" ->  _, ___]]
       ]
      ,
      HoldComplete
      ] &[
   ToExpression[Unevaluated[#], StandardForm, 
      HoldComplete] & /@ (NotebookRead@
       Cells[nb, GeneratedCell -> False])[[All, 1]]
   ]
  ]

For the notebook in which I am working, which contains a lot of functions, we then have

Complement[functions[nb], functionsSpecific[nb]]
HoldComplete[All,Bold,False,GeneratedCell,Integer,Italic,Null]

So it seems the functions that are excluded by functionsSpecific are reasonable.

share|improve this answer
    
Nice. I forgot about Cells... –  Simon Woods May 21 at 10:26
    
So this is a custom function. Do I put the filepath in place of nb_ ? When I do that I get an error message ToExpression::sntx: Invalid syntax in or before " \!(StandardForm`\\ecsdbn-srv-02\lara_jordan$\Cleaner Kumba\Cleaner Kumba Tool Non-Encoded Notebooks\Initialization.nb) " and some other error messages. –  Lara Jordan May 21 at 10:39
1  
@LaraJordan use EvaluationNotebook or other NotebookObject. You can set nb = EvaluationNotebook[] and use it on nb somewhere else so it will not interfere. –  Kuba May 21 at 10:42
    
Great :) can't +1 again :/ –  Kuba May 21 at 12:06

Evaluate this at the bottom of the notebook of interest:

Composition[
  Select[#, StringMatchQ[Evaluate[Symbol[#]]::usage, 
                         "\!\(\*RowBox[{\"" <> # <> "\", \"[\"" ~~ __] &] &,
  Intersection[#, Names["System`*"]] &,
  Union@Cases[#, _String, ∞, Heads -> True] &,
  First /@ # &,
  NotebookRead /@ # &,
  Most
  ]@Cells[EvaluationNotebook[], GeneratedCell -> False]
{"Insert", "Range", "Style"}

To find what I've assumed is a function, I've used Evaluate[Symbol[#]]::usage which should show something like

Plot::usage
Plot[f,{x,...

so I'm looking for name ~~ [ pattern. Unfortunately, it is not bulletproof, there are symbols with different usage structure:

Quiet@Select[Names["System`*"], StringMatchQ[Evaluate[Symbol[#]]::usage, 
"\!\(\*RowBox[{StyleBox[" ~~ __] &]
{AddTo,Condition,Decrement,Derivative,Divide,DivideBy,Dot,Equal,Factorial,Factorial2,Greater,GreaterEqual,Increment,Less,LessEqual,MessageName,NonCommutativeMultiply,Optional,Part,Pattern,PatternTest,Plus,Power,Put,PutAppend,Repeated,RepeatedNull,ReplaceAll,ReplaceRepeated,Rule,RuleDelayed,SameQ,Set,SetDelayed,Span,StringJoin,Subtract,SubtractFrom,TagSet,TagSetDelayed,TagUnset,Times,TimesBy,Unequal,UnsameQ,Unset,UpSet,UpSetDelayed}

some of them may be considered functions, some not. So it's really hard to make a proper answer without very precise definition of function.

share|improve this answer
    
Cells is new in v9. Same goes for Jacob. –  Öskå May 21 at 10:31
    
@Kuba it may be nice to adapt the second piece of code in my question list of dangerous functions then. My answer is basically an adaptation of the first code block. –  Jacob Akkerboom May 21 at 10:33
    
@JacobAkkerboom The dangerous functions list does not care if there is Bold or not, it will be dropped later anyway, that's why it's a little different problem. –  Kuba May 21 at 10:36
    
@Kuba yeah, thank you for reminding me that I don't really answer the question here :P. But I don't really see what symbols we should return then, short of making an explicit list of functions we don't want, like Bold. By the way how do you think symbols like $MachineEpsilon should be handled? That one does have a usage message, but your code does not find it. –  Jacob Akkerboom May 21 at 10:41
    
@JacobAkkerboom I've assumed that we need only explicitely stated functions and function is a symbol with DownValue which is a tricky part since those are hidden. –  Kuba May 21 at 10:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.