From a9f3b6a30ed98c730bbeb56ee36af4fdc84b9c89 Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Tue, 14 Nov 2023 10:05:06 -0500 Subject: [PATCH 1/6] Add styles for inline WL input --- Developer/Resources/Styles.wl | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Developer/Resources/Styles.wl b/Developer/Resources/Styles.wl index ff7df5c3..15b5b678 100644 --- a/Developer/Resources/Styles.wl +++ b/Developer/Resources/Styles.wl @@ -170,6 +170,12 @@ Cell[ Symbol[ "Wolfram`Chatbook`ChatbookAction" ][ "InsertInlineReference", "ModifierTemplate", $CellContext`cell ] ] , + (* Insert WL code input template: *) + { "KeyDown", "\\" } :> With[ { $CellContext`cell = EvaluationCell[ ] }, + Quiet @ Needs[ "Wolfram`Chatbook`" -> None ]; + Symbol[ "Wolfram`Chatbook`ChatbookAction" ][ "InsertInlineReference", "WLTemplate", $CellContext`cell ] + ] + , (* Highlight cells that will be included in chat context: *) "MouseEntered" :> With[ { $CellContext`cell = EvaluationCell[ ] }, Quiet @ Needs[ "Wolfram`Chatbook`" -> None ]; @@ -1059,6 +1065,28 @@ Cell[ ] +(* ::Subsection::Closed:: *) +(*ChatbookWLTemplate*) + + +Cell[ + StyleData["ChatbookWLTemplate"], + TemplateBoxOptions -> { + DisplayFunction -> ( + NamespaceBox["ChatbookWLTemplateID", + DynamicModuleBox[{}, + DynamicBox[ToBoxes @ Wolfram`Chatbook`InlineReferences`wlTemplateBoxes[1, #input, #state, #uuid]], + Initialization :> ( + Quiet @ Needs[ "Wolfram`Chatbook`" -> None ]; + Wolfram`Chatbook`InlineReferences`Private`$lastInlineReferenceCell = EvaluationCell[ ] + ) + ] + ]&), + InterpretationFunction -> (InterpretationBox["", RowBox[{"\\",#input}]]&) + } +] + + (* ::Section::Closed:: *) (*Misc Styles*) From 56735876c234a1b99b8bd7e54ead4fd24a2e312c Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Tue, 14 Nov 2023 10:24:10 -0500 Subject: [PATCH 2/6] Defined inline WL code boxes --- Source/Chatbook/Actions.wl | 2 + Source/Chatbook/InlineReferences.wl | 189 ++++++++++++++++++++++++++++ 2 files changed, 191 insertions(+) diff --git a/Source/Chatbook/Actions.wl b/Source/Chatbook/Actions.wl index 787497d6..ff76390b 100644 --- a/Source/Chatbook/Actions.wl +++ b/Source/Chatbook/Actions.wl @@ -269,6 +269,7 @@ InsertInlineReference // endDefinition; (*insertInlineReference*) insertInlineReference // beginDefinition; +(* FIXME: these four are unused, need to remove relevant definitions *) insertInlineReference[ "Persona" , args___ ] := insertPersonaInputBox @ args; insertInlineReference[ "TrailingFunction", args___ ] := insertTrailingFunctionInputBox @ args; insertInlineReference[ "Function" , args___ ] := insertFunctionInputBox @ args; @@ -277,6 +278,7 @@ insertInlineReference[ "Modifier" , args___ ] := insertModifierInputBox @ insertInlineReference[ "PersonaTemplate" , args___ ] := insertPersonaTemplate @ args; insertInlineReference[ "FunctionTemplate", args___ ] := insertFunctionTemplate @ args; insertInlineReference[ "ModifierTemplate", args___ ] := insertModifierTemplate @ args; +insertInlineReference[ "WLTemplate" , args___ ] := insertWLTemplate @ args; insertInlineReference // endDefinition; diff --git a/Source/Chatbook/InlineReferences.wl b/Source/Chatbook/InlineReferences.wl index e422f571..02880782 100644 --- a/Source/Chatbook/InlineReferences.wl +++ b/Source/Chatbook/InlineReferences.wl @@ -14,10 +14,12 @@ BeginPackage[ "Wolfram`Chatbook`InlineReferences`" ]; `insertPersonaTemplate; `insertFunctionTemplate; `insertModifierTemplate; +`insertWLTemplate; `personaTemplateBoxes; `functionTemplateBoxes; `modifierTemplateBoxes; +`wlTemplateBoxes; `parseInlineReferences; `resolveLastInlineReference; @@ -1934,6 +1936,193 @@ insertFunctionTemplate[ name_String, parent_CellObject, nbo_NotebookObject ] := ]; +(* ::Section::Closed:: *) +(*WL Template*) + + +(* ::Subsection::Closed:: *) +(*setWLState*) + + +SetAttributes[setWLState, HoldFirst] + +setWLState[state_, "Input", input_] := ( + state = "Input"; + MathLink`CallFrontEnd[FrontEnd`BoxReferenceFind[ + FE`BoxReference[MathLink`CallFrontEnd[FrontEnd`Value[FEPrivate`Self[]]], + {FE`Parent["ChatbookWLTemplateID"]}], + AutoScroll -> True + ]]; + FrontEndExecute[FrontEnd`FrontEndToken["MoveNextPlaceHolder"]]; +) + +setWLState[state_, "Chosen", input_] := +Enclose[ + With[{cellobj = EvaluationCell[]}, + state = "Chosen"; + SelectionMove[cellobj, All, Cell]; + FrontEndExecute[FrontEnd`FrontEndToken["MoveNext"]]; + CurrentValue[cellobj, TaggingRules] = <| "WLCode" -> input |>; + ] + , + throwInternalFailure[ setWLState[state, "Chosen", input], ## ]& +] + +setWLState[ state_, "Replace", input_ ] := ( + SelectionMove[ EvaluationCell[ ], All, Cell ]; + NotebookWrite[ + InputNotebook[ ], + First[ + FrontEndExecute @ FrontEnd`ExportPacket[ Cell @ BoxData @ RowBox @ { "\\", input }, "PlainText" ], + "\\" + ] + ] +); + + +(* ::Subsection::Closed:: *) +(*wlTemplateBoxes*) + + +SetAttributes[wlTemplateBoxes, HoldRest]; + +wlTemplateBoxes[version: 1, input_, state_, uuid_, opts: OptionsPattern[]] /; state === "Input" := + EventHandler[ + Style[ + Framed[ + Grid[ + { + { + RawBoxes[TemplateBox[{}, "AssistantEvaluate"]], + InputField[ + Dynamic[ input ], + Boxes, + Alignment -> { Left, Baseline }, + ContinuousAction -> False, + BaselinePosition -> Baseline, + FieldSize -> { { 5, Infinity }, Automatic }, + Appearance -> "Frameless", + (*ContentPadding -> False,*) + FrameMargins -> 0, + BoxID -> uuid, + BaseStyle -> { + "Notebook", + "Input", + ShowCodeAssist -> True, + ShowSyntaxStyles -> True, + FontFamily -> "Source Sans Pro", + FontSize -> 14 + } + ] // overrideKeyEvents + } + }, + Spacings -> 0, + Alignment -> { Automatic, Baseline }, + BaselinePosition -> { 1, 2 } + ], + BaselinePosition -> Baseline, + FrameStyle -> GrayLevel[0.9], + ImageMargins -> {{1,1},{0,0}}, + $frameOptions + ], + "Text", + ShowStringCharacters -> False + ], + { + (*{ "KeyDown", "!" } :> NotebookWrite[ EvaluationNotebook[ ], "!" ],*) + "EscapeKeyDown" :> setWLState[state, "Replace", input], + "ReturnKeyDown" :> setWLState[state, "Chosen", input] + } + ] + +wlTemplateBoxes[ version: 1, input_, state_, uuid_, opts: OptionsPattern[ ] ] /; state === "Chosen" := + Button[ + NotebookTools`Mousedown @@ MapThread[ + Framed[ + Grid[ + { + { + RawBoxes @ TemplateBox[ { }, "AssistantEvaluate" ], + Style[ + RawBoxes @ input, + "Input", + ShowCodeAssist -> True, + ShowSyntaxStyles -> True, + FontFamily -> "Source Sans Pro", + FontSize -> 14, + ShowStringCharacters -> True + ] + } + }, + Spacings -> 0, + Alignment -> { Automatic, Baseline } + ], + BaselinePosition -> { 1, 2 }, + RoundingRadius -> 2, + Background -> #1, + FrameStyle -> #2, + FrameMargins -> 2, + ImageMargins -> { { 1, 1 }, { 0, 0 } } + ] &, + { + { GrayLevel[ 0.95 ], GrayLevel[ 0.975 ], GrayLevel[ 1.0 ] }, + { GrayLevel[ 0.90 ], GrayLevel[ 0.800 ], GrayLevel[ 0.9 ] } + } + ], + setWLState[ state, "Input", input ] + , + Appearance -> "Suppressed", + BaseStyle -> { }, + DefaultBaseStyle -> { }, + BaselinePosition -> Baseline + ]; + +(* FIXME: Add a wlTemplateBoxes rule for unknown version number *) + + +(* ::Subsection::Closed:: *) +(*wlTemplateCell*) + + +wlTemplateCell[input_String, state: ("Input" | "Chosen"), uuid_String] := +Cell[BoxData[FormBox[ + TemplateBox[<|"input" -> input, "state" -> state, "uuid" -> uuid|>, + "ChatbookWLTemplate" + ], TextForm]], + "InlineWLReference", + Background -> None, + Deployed -> True +] + + +(* ::Subsection::Closed:: *) +(*insertWLTemplate*) + + +insertWLTemplate[ cell_CellObject ] := + insertWLTemplate[ cell, parentNotebook @ cell ]; + +insertWLTemplate[ parent_CellObject, nbo_NotebookObject ] := + Module[ { uuid, cellexpr }, + resolveInlineReferences @ parent; + uuid = CreateUUID[ ]; + cellexpr = wlTemplateCell[ "", "Input", uuid ]; + NotebookWrite[ nbo, cellexpr ]; + (* FIXME: Can we get rid of the need for this UUID, and use BoxReference-something? *) + FrontEnd`MoveCursorToInputField[ nbo, uuid ] + ]; + +insertWLTemplate[ name_String, cell_CellObject ] := insertWLTemplate[ name, cell, parentNotebook @ cell ]; + +insertWLTemplate[ name_String, parent_CellObject, nbo_NotebookObject ] := + Module[ { uuid, cellexpr }, + resolveInlineReferences @ ParentCell @ parent; + uuid = CreateUUID[ ]; + cellexpr = wlTemplateCell[ name, "Input", uuid ]; + NotebookWrite[ parent, cellexpr ]; + FrontEnd`MoveCursorToInputField[ nbo, uuid ] + ]; + (* ::**************************************************************************************************************:: *) (* ::Section::Closed:: *) (*Cloud Docked Cell*) From ac924962a55f5bf4fe79f42584db986834f59f6b Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Tue, 14 Nov 2023 10:24:38 -0500 Subject: [PATCH 3/6] Expand inline WL code boxes during serialization --- Source/Chatbook/Serialization.wl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Source/Chatbook/Serialization.wl b/Source/Chatbook/Serialization.wl index 7c834513..d8082173 100644 --- a/Source/Chatbook/Serialization.wl +++ b/Source/Chatbook/Serialization.wl @@ -750,6 +750,17 @@ fasterCellToString0[ TemplateBox[ KeyValuePattern[ "input" -> string_ ], "TeXAss "$" <> string <> "$" ); +(* Inline WL code template *) +fasterCellToString0[ TemplateBox[ KeyValuePattern[ "input" -> input_ ], "ChatbookWLTemplate", ___ ] ] := + Replace[ + Quiet[ ToExpression[ input, StandardForm ], ToExpression::esntx ], + { + string_String? StringQ :> string, + $Failed :> "\n\n[Inline parse failure: " <> ToString[ fasterCellToString0 @ input, InputForm ] <> "]", + expr_ :> fasterCellToString0 @ ToBoxes @ expr + } + ]; + (* Other *) fasterCellToString0[ TemplateBox[ args_, name_String, ___ ] ] := With[ { f = $templateBoxRules @ name }, From c3e97bd3d177d256299d0c5639c6740e937c55d0 Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Tue, 14 Nov 2023 10:25:39 -0500 Subject: [PATCH 4/6] Bugfix: `delayedChatObject` requires the symbol `chatObject` in order to work properly --- Source/Chatbook/Actions.wl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Source/Chatbook/Actions.wl b/Source/Chatbook/Actions.wl index ff76390b..2e24ac0a 100644 --- a/Source/Chatbook/Actions.wl +++ b/Source/Chatbook/Actions.wl @@ -637,7 +637,7 @@ constructChatObject // beginDefinition; (* cSpell: ignore bdprompt *) constructChatObject[ messages_List ] := - With[ { chat = Quiet[ ChatObject @ standardizeMessageKeys @ messages, ChatObject::bdprompt ] }, + With[ { chat = Quiet[ chatObject @ standardizeMessageKeys @ messages, ChatObject::bdprompt ] }, chat /; MatchQ[ chat, _chatObject ] ]; @@ -646,6 +646,8 @@ constructChatObject[ messages_List ] := constructChatObject // endDefinition; +chatObject := chatObject = Symbol[ "System`ChatObject" ]; + (* ::**************************************************************************************************************:: *) (* ::Subsubsection::Closed:: *) (*standardizeMessageKeys*) From c69e7be657e01aaf44caf7aaf0d585881374a093 Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Tue, 14 Nov 2023 10:26:26 -0500 Subject: [PATCH 5/6] Bugfix: strip $CellContext` from code in interpretation boxes --- Source/Chatbook/Serialization.wl | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Source/Chatbook/Serialization.wl b/Source/Chatbook/Serialization.wl index d8082173..7945232e 100644 --- a/Source/Chatbook/Serialization.wl +++ b/Source/Chatbook/Serialization.wl @@ -977,10 +977,11 @@ fasterCellToString0[ cell: Cell[ a_, ___ ] ] := fasterCellToString0 @ a ]; -fasterCellToString0[ InterpretationBox[ _, expr_, ___ ] ] := ( +fasterCellToString0[ InterpretationBox[ _, expr_, ___ ] ] := + With[ { held = replaceCellContext @ HoldComplete @ expr }, needsBasePrompt[ "WolframLanguage" ]; - inputFormString @ Unevaluated @ expr -); + Replace[ held, HoldComplete[ e_ ] :> inputFormString @ Unevaluated @ e ] + ]; fasterCellToString0[ Cell[ TextData @ { _, _, text_String, _, Cell[ _, "ExampleCount", ___ ] }, ___ ] ] := fasterCellToString0 @ text; From 16b29376964026eca553a1d29a7fb063e1e7b48f Mon Sep 17 00:00:00 2001 From: Rick Hennigan Date: Tue, 14 Nov 2023 10:59:53 -0500 Subject: [PATCH 6/6] Rebuilt stylesheet --- Assets/DisplayFunctions.wxf | Bin 501839 -> 502259 bytes FrontEnd/StyleSheets/Chatbook.nb | 41 ++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/Assets/DisplayFunctions.wxf b/Assets/DisplayFunctions.wxf index ff491332141a2fb157fe70cfc374edca15cd6380..14ed96bbfcf2ab9f761844105fe032ed7b70af7e 100644 GIT binary patch delta 113 zcmX?qLGJTqId%&x$7J1LsmT|ar5Y7m6&YIa?_~x6YUCzX delta 49 zcmex-S?>G=Id%&x$0XfgsmT|ar5Y7m6&YI <|"StyleSheetVersion" -> "1.3.1.3908279596"|> + TaggingRules -> <|"StyleSheetVersion" -> "1.3.1.3908948274"|> ], Cell[ StyleData["Text"], @@ -1621,6 +1621,15 @@ Notebook[ cell ] ], + {"KeyDown", "\\"} :> + With[ { cell = EvaluationCell[] }, + Quiet[Needs["Wolfram`Chatbook`" -> None]]; + Symbol["Wolfram`Chatbook`ChatbookAction"][ + "InsertInlineReference", + "WLTemplate", + cell + ] + ], "MouseEntered" :> With[ { cell = EvaluationCell[] }, Quiet[Needs["Wolfram`Chatbook`" -> None]]; @@ -17058,6 +17067,36 @@ Notebook[ (InterpretationBox["", StringJoin["!", #input]] &) } ], + Cell[ + StyleData["ChatbookWLTemplate"], + TemplateBoxOptions -> { + DisplayFunction -> + (Function[ + NamespaceBox[ + "ChatbookWLTemplateID", + DynamicModuleBox[ + { }, + DynamicBox[ + ToBoxes[ + Wolfram`Chatbook`InlineReferences`wlTemplateBoxes[ + 1, + #input, + #state, + #uuid + ] + ] + ], + Initialization :> + (Quiet[Needs["Wolfram`Chatbook`" -> None]]; + Wolfram`Chatbook`InlineReferences`Private`$lastInlineReferenceCell = + EvaluationCell[]) + ] + ] + ]), + InterpretationFunction -> + (InterpretationBox["", RowBox[{"\\", #input}]] &) + } + ], Cell[ StyleData[ "InlineReferenceText",