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.

Is there a function that returns the output as if you invoked Command Completion.

To help clarify I am looking for a function that returns the Cell Data as if you invoked Ctrl+Shift+K.

share|improve this question

1 Answer 1

up vote 15 down vote accepted

This took some digging but at least in Version 7 the FrontEnd command is FT, e.g.:

FE`FT["Plot"]

enter image description here

You can read the definition with Definition[FE`FT].
If you want only the Box form itself we can modify it accordingly (here for version 7):

templateCell[name_String] :=
 Module[{template},
  If[! StringQ@ToExpression[name <> "::usage"], $NewMessage[ToExpression@name, "usage"]];
  template = FE`getTemplateFromUsage[ToExpression[name <> "::usage"], name];
  template = FE`reparseBoxStructure[FE`makePlainText@template, name];
  Cell[BoxData[First @ FE`insertTagBox[template, name]], "Input"]
 ]

Now:

templateCell["Plot"] // CellPrint

enter image description here

templateCell["Plot"]
Cell[BoxData[
  RowBox[{"Plot", "[", 
    RowBox[{TagBox[FrameBox["f"], "Placeholder"], ",", 
      RowBox[{"{", 
        RowBox[{TagBox[FrameBox["x"], "Placeholder"], ",", 
          TagBox[FrameBox[SubscriptBox["x", "min"]], "Placeholder"], ",", 
          TagBox[FrameBox[SubscriptBox["x", "max"]], "Placeholder"]}], "}"}]}], 
    "]"}]], "Input"]
share|improve this answer
    
Also in 9. It also takes a second argument (integer) to get the nth template –  rm -rf Jul 26 '13 at 21:18
    
@rm-rf The second argument was apparently added after v7. –  Mr.Wizard Jul 26 '13 at 21:22
    
Quite likely, since that's when support for multiple templates was added. FWIW, it simply calls Part on the list. So there's {template1, template2} inside and you can use all integers that are valid part specifications to index this list. So -2, -1, 0, 1, 2 all work and give what's expected. –  rm -rf Jul 26 '13 at 21:24
    
@Mr.Wizard Is there a function that returns the CellData instead of adding a new cell to the current Notebook? I plan to likely use this in a Package .m if context helps. –  Liam William Jul 26 '13 at 21:27

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.