|
| 1 | +(* ::Package:: *) |
| 2 | + |
| 3 | +BeginPackage["FrontEndNaming`"]; |
| 4 | +FENamed::usage = "A named object for display in the front end"; |
| 5 | + |
| 6 | + |
| 7 | +BeginPackage["`Package`"]; |
| 8 | + |
| 9 | + |
| 10 | +nameBoxApply::usage=""; |
| 11 | +getNamedObject::usage=""; |
| 12 | +selectNamedObject::usage=""; |
| 13 | +readNamedObjectContents::usage=""; |
| 14 | +replaceNamedObjectContents::usage=""; |
| 15 | +getNamedObjectOptions::usage=""; |
| 16 | +setNamedObjectOptions::usage=""; |
| 17 | + |
| 18 | + |
| 19 | +EndPackage[]; |
| 20 | + |
| 21 | + |
| 22 | +Begin["`Private`"]; |
| 23 | + |
| 24 | + |
| 25 | +feObjectPattern = |
| 26 | + _CellObject | _BoxObject | _NotebookObject | |
| 27 | + _FrontEnd`InputNotebook | _FrontEnd`EvaluationNotebook | _FrontEnd`ButtonNotebook | |
| 28 | + _FrontEnd`EvaluationCell | _FrontEnd`EvaluationBox |_FrontEnd`Self | _FrontEnd`Parent; |
| 29 | + |
| 30 | + |
| 31 | +FENamed//Clear |
| 32 | + |
| 33 | + |
| 34 | +(* Constructor DownValues *) |
| 35 | +FENamed[ |
| 36 | + FENamed[expr_, objOld : feObjectPattern | Automatic : Automatic, |
| 37 | + name_String, ops : OptionsPattern[TagBox] |
| 38 | + ], |
| 39 | + objNew : feObjectPattern | Automatic : Automatic, |
| 40 | + name_String, |
| 41 | + ops2 : OptionsPattern[] |
| 42 | + ] := FENamed[expr, objNew, name, Flatten[{ops2, ops}]]; |
| 43 | +FENamed[ |
| 44 | + expr_, |
| 45 | + obj : feObjectPattern | Automatic : Automatic, |
| 46 | + ops : OptionsPattern[] |
| 47 | + ] := FENamed[expr, obj, CreateUUID[], ops]; |
| 48 | + |
| 49 | + |
| 50 | +(* FormatValues *) |
| 51 | +Format[n : |
| 52 | + FENamed[expr_, obj : feObjectPattern | Automatic : Automatic, |
| 53 | + name_String, ops : OptionsPattern[]]] := |
| 54 | + RawBoxes@ |
| 55 | + TemplateBox[ |
| 56 | + { |
| 57 | + TagBox[ToBoxes[expr], name, BoxID -> name, ops], |
| 58 | + ToBoxes[obj], ToBoxes[name], ToBoxes[Flatten@{ops}] |
| 59 | + }, |
| 60 | + "FENamed", |
| 61 | + DisplayFunction -> Function[#], |
| 62 | + InterpretationFunction -> |
| 63 | + Function[ |
| 64 | + RowBox[{"FENamed", "[", #[[1]], ",", #2, ",", #3, "," , #4, |
| 65 | + "]"}]], |
| 66 | + Editable -> True |
| 67 | + ]; |
| 68 | + |
| 69 | + |
| 70 | +Clear[ |
| 71 | + nameBoxApply, |
| 72 | + nameBoxApply, |
| 73 | + getNamedObject, |
| 74 | + selectNamedObject, |
| 75 | + readNamedObjectContents, |
| 76 | + replaceNamedObjectContents, |
| 77 | + getNamedObjectOptions, |
| 78 | + setNamedObjectOptions |
| 79 | + ] |
| 80 | + |
| 81 | + |
| 82 | +parseRelSpec[stuff_]:= |
| 83 | + Fold |
| 84 | + |
| 85 | + |
| 86 | +nameBoxApply[ |
| 87 | + obj : feObjectPattern | Automatic : Automatic, |
| 88 | + sibling: _Down|_Up|_Left|_Right|None : Down[1], |
| 89 | + spec : Next | Previous : Next, |
| 90 | + refOps : _?OptionQ : {}, |
| 91 | + name_String, |
| 92 | + fn_, |
| 93 | + args___ |
| 94 | + ] := |
| 95 | + With[{fob=Replace[obj, Automatic -> FrontEnd`InputNotebook[]]}, |
| 96 | + MathLink`CallFrontEnd@ |
| 97 | + fn[ |
| 98 | + FE`BoxReference[ |
| 99 | + FE`Evaluate@fob, |
| 100 | + {{name}}, |
| 101 | + Sequence @@ |
| 102 | + DeleteDuplicatesBy[First]@ |
| 103 | + Flatten@{ |
| 104 | + refOps, |
| 105 | + FE`BoxOffset -> |
| 106 | + { |
| 107 | + Replace[sibling, |
| 108 | + { |
| 109 | + None->Nothing, |
| 110 | + deeep_[n_]:> |
| 111 | + Replace[deeep, |
| 112 | + { |
| 113 | + Up->FE`BoxParent, |
| 114 | + Down->FE`BoxChild, |
| 115 | + Left->FE`BoxLeftSibling, |
| 116 | + Right->FE`BoxRightSibling |
| 117 | + } |
| 118 | + ][n] |
| 119 | + } |
| 120 | + ] |
| 121 | + }, |
| 122 | + Switch[spec, |
| 123 | + First, |
| 124 | + FE`SearchStart -> |
| 125 | + "StartFromBeginning", |
| 126 | + Next, |
| 127 | + FE`SearchStart -> |
| 128 | + "StartFromBeginning"(*Sequence@@{}*), |
| 129 | + Previous, |
| 130 | + FE`SearchStart -> |
| 131 | + "StartFromBeginning"(*Sequence@@{}*) |
| 132 | + ], |
| 133 | + FE`SearchStop->None, |
| 134 | + Switch[ |
| 135 | + spec, |
| 136 | + First, |
| 137 | + FE`SearchDirection -> Backward, |
| 138 | + Next, |
| 139 | + FE`SearchDirection -> Forward, |
| 140 | + Previous, |
| 141 | + FE`SearchDirection -> Backward |
| 142 | + ] |
| 143 | + } |
| 144 | + ], |
| 145 | + args |
| 146 | + ] |
| 147 | + ]; |
| 148 | + |
| 149 | + |
| 150 | +getNamedObject[ |
| 151 | + obj : feObjectPattern | Automatic : Automatic, |
| 152 | + spec:Next|Previous:Next, |
| 153 | + name_String, |
| 154 | + ops:OptionsPattern[] |
| 155 | + ] := |
| 156 | + nameBoxApply[obj, spec, {ops}, name, FrontEnd`BoxReferenceBoxObject]; |
| 157 | + |
| 158 | + |
| 159 | +selectNamedObject[ |
| 160 | + obj : feObjectPattern | Automatic : Automatic, |
| 161 | + spec:Next|Previous:Next, |
| 162 | + name_String, |
| 163 | + ops:OptionsPattern[] |
| 164 | + ] := |
| 165 | + nameBoxApply[obj, spec, {ops}, |
| 166 | + name, |
| 167 | + FrontEnd`BoxReferenceFind |
| 168 | + ]; |
| 169 | + |
| 170 | + |
| 171 | +readNamedObjectContents[ |
| 172 | + obj : feObjectPattern | Automatic : Automatic, |
| 173 | + spec:Next|Previous:Next, |
| 174 | + name_String, |
| 175 | + ops:OptionsPattern[] |
| 176 | + ] := |
| 177 | + nameBoxApply[obj, spec, {ops}, |
| 178 | + name, |
| 179 | + FrontEnd`BoxReferenceRead |
| 180 | + ]; |
| 181 | + |
| 182 | + |
| 183 | +replaceNamedObjectContents[ |
| 184 | + obj : feObjectPattern | Automatic : Automatic, |
| 185 | + spec:Next|Previous:Next, |
| 186 | + name_String, |
| 187 | + boxes_, |
| 188 | + ops:OptionsPattern[] |
| 189 | + ] := |
| 190 | + nameBoxApply[obj, spec, {ops}, |
| 191 | + name, |
| 192 | + FrontEnd`BoxReferenceReplace, |
| 193 | + boxes |
| 194 | + ]; |
| 195 | + |
| 196 | + |
| 197 | +getNamedObjectOptions[ |
| 198 | + obj : feObjectPattern | Automatic : Automatic, |
| 199 | + spec:Next|Previous:Next, |
| 200 | + name_String, |
| 201 | + All, |
| 202 | + ops:OptionsPattern[] |
| 203 | + ] := |
| 204 | + nameBoxApply[ |
| 205 | + obj, |
| 206 | + spec, |
| 207 | + { |
| 208 | + ops, |
| 209 | + FE`BoxOffset -> {FE`BoxParent[1]} |
| 210 | + }, |
| 211 | + name, |
| 212 | + FrontEnd`BoxReferenceGetOptions |
| 213 | + ]; |
| 214 | +getNamedObjectOptions[ |
| 215 | + obj : feObjectPattern | Automatic : Automatic, |
| 216 | + spec:Next|Previous:Next, |
| 217 | + name_String, |
| 218 | + opt_?OptionQ, |
| 219 | + ops:OptionsPattern[] |
| 220 | + ] := |
| 221 | + nameBoxApply[obj, spec, {ops}, |
| 222 | + name, |
| 223 | + FrontEnd`BoxReferenceGetOptions, |
| 224 | + opt |
| 225 | + ] |
| 226 | + |
| 227 | + |
| 228 | +setNamedObjectOptions[ |
| 229 | + obj : feObjectPattern | Automatic : Automatic, |
| 230 | + spec:Next|Previous:Next, |
| 231 | + name_String, |
| 232 | + options_, |
| 233 | + ops:OptionsPattern[] |
| 234 | + ] := |
| 235 | + nameBoxApply[obj, spec, {ops}, |
| 236 | + name, |
| 237 | + FrontEnd`BoxReferenceSetOptions, |
| 238 | + options |
| 239 | + ]; |
| 240 | + |
| 241 | + |
| 242 | +(* UpValues interface on this *) |
| 243 | +FENamed /: |
| 244 | + BoxObject[ |
| 245 | + FENamed[_, obj : feObjectPattern | Automatic : Automatic, |
| 246 | + name_String, ___], |
| 247 | + spec:Next|Previous:Next, |
| 248 | + ops:OptionsPattern[] |
| 249 | + ] := |
| 250 | + getNamedObject[obj, spec, name, ops]; |
| 251 | +FENamed /: |
| 252 | + NotebookLocate[ |
| 253 | + FENamed[_, obj : feObjectPattern | Automatic : Automatic, |
| 254 | + name_String, ___], |
| 255 | + spec:Next|Previous:Next, |
| 256 | + ops:OptionsPattern[] |
| 257 | + ] := |
| 258 | + selectNamedObject[obj, spec, name, ops]; |
| 259 | +FENamed /: |
| 260 | + NotebookRead[ |
| 261 | + FENamed[_, obj : feObjectPattern | Automatic : Automatic, |
| 262 | + name_String, ___], |
| 263 | + spec:Next|Previous:Next, |
| 264 | + ops:OptionsPattern[] |
| 265 | + ] := |
| 266 | + readNamedObjectContents[obj, spec, name, ops]; |
| 267 | +FENamed /: |
| 268 | + NotebookWrite[ |
| 269 | + FENamed[_, |
| 270 | + obj : feObjectPattern | Automatic : Automatic, |
| 271 | + name_String, |
| 272 | + ___ |
| 273 | + ], |
| 274 | + spec:Next|Previous:Next, |
| 275 | + cnts_, |
| 276 | + ops:OptionsPattern[] |
| 277 | + ] := |
| 278 | + replaceNamedObjectContents[obj, spec, name, cnts, ops]; |
| 279 | +FENamed /: |
| 280 | + HoldPattern[ |
| 281 | + Options[ |
| 282 | + FENamed[_, |
| 283 | + obj : feObjectPattern | Automatic : Automatic, |
| 284 | + name_String, ___ |
| 285 | + ], |
| 286 | + spec:Next|Previous:Next, |
| 287 | + opt:_?OptionQ|All:All, |
| 288 | + ops:OptionsPattern[] |
| 289 | + ] |
| 290 | + ] := |
| 291 | + getNamedObjectOptions[obj, spec, name, If[Length@opt==0, All, opt], ops]; |
| 292 | +FENamed /: |
| 293 | + SetOptions[ |
| 294 | + FENamed[_, obj : feObjectPattern | Automatic : Automatic, |
| 295 | + name_String, ___], |
| 296 | + spec:Next|Previous:Next, |
| 297 | + opt_, |
| 298 | + ops:OptionsPattern[] |
| 299 | + ] := |
| 300 | + setNamedObjectOptions[obj, spec, name, opt, ops]; |
| 301 | + |
| 302 | + |
| 303 | +End[]; |
| 304 | +EndPackage[]; |
0 commit comments