Advertisement
Guest User

Untitled

a guest
Aug 5th, 2012
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.82 KB | None | 0 0
  1. In[1]:= getCellID[] :=
  2. "CellID" /. Developer`CellInformation[EvaluationNotebook[]]
  3.  
  4. In[2]:= ClearAll[appendEvents];
  5. appendEvents[evList_, (Rule | RuleDelayed)[lhs_, rhs_]] /;
  6. MemberQ[evList, (Rule | RuleDelayed)[lhs, _]] :=
  7. evList /. (Rule | RuleDelayed)[lhs, sth_] :> (lhs :> (sth; rhs));
  8. appendEvents[evList_, ev_] := Append[evList, ev]
  9.  
  10. In[5]:= checkCreateUndoNotebook[] := Refresh[
  11. With[{nb :=
  12. CurrentValue[
  13. EvaluationNotebook[], {TaggingRules, "undoNotebook"}]},
  14. If[(* if the undo notebook is not created or open *)!
  15. TrueQ@NotebookTools`NotebookOpenQ@nb,
  16. nb = CreateDocument[{}, CellGrouping -> Manual, Visible -> False,
  17. ShowSelection -> False,
  18. WindowTitle ->
  19. FileBaseName[
  20. WindowTitle /.
  21. AbsoluteOptions[EvaluationNotebook[], WindowTitle]] <>
  22. "_undoData"];
  23. ]], None]
  24.  
  25. In[6]:= nbdeRules = {Dynamic[rhs_] :>
  26. Dynamic[rhs; checkCreateUndoNotebook[]],
  27. Null :> Dynamic@checkCreateUndoNotebook[],
  28. rhs_ :> Dynamic[rhs; checkCreateUndoNotebook[]]};
  29.  
  30. In[7]:= parseNBEOptions[nb_] :=
  31. ReleaseHold@
  32. Replace[Hold[NotebookDynamicExpression] /.
  33. Options[nb, NotebookDynamicExpression], nbdeRules, {1}]
  34.  
  35. In[8]:= appendNDE[nb_NotebookObject] :=
  36. SetOptions[nb,
  37. parseNBEOptions[nb] /.
  38. newNDE_ :> NotebookDynamicExpression :> newNDE]
  39.  
  40. In[9]:= appendNEA[
  41. nb_NotebookObject] :=(* appends the notebook event action \
  42. WindowClose *)
  43. appendEvents[
  44. Replace[NotebookEventActions /. Options[nb, NotebookEventActions],
  45. None :> {}],
  46. "WindowClose" :>
  47. NotebookClose@
  48. CurrentValue[
  49. EvaluationNotebook[], {TaggingRules, "undoNotebook"}]] /.
  50. newOpts_ :> SetOptions[nb, NotebookEventActions :> newOpts]
  51.  
  52. In[10]:= setUpUndo[nb_NotebookObject, styles_List] :=
  53. SetOptions[initializeNotebook[nb]; nb, StyleDefinitions -> Fold[
  54. #1 /. {s_String :>
  55. Notebook[{Cell[StyleData[StyleDefinitions -> s]],
  56. Cell[StyleData[#2], cellEventActions
  57. ]}],
  58. Notebook[{bef___, Cell[StyleData[#2], cellRest___], aft___},
  59. rest___] :>
  60. Notebook[{bef,
  61. Cell[StyleData[#2], cellRest, cellEventActions], aft},
  62. rest],
  63. Notebook[{all___}, rest___] :>
  64. Notebook[{all, Cell[StyleData[#2], cellEventActions]}, rest]
  65. } &,
  66. FullOptions[nb, StyleDefinitions], styles]];
  67.  
  68. setUpUndo[nb_, styles_] := setUpUndo[nb, {styles}];
  69.  
  70. In[12]:= $defaultFrequency = 15;
  71.  
  72. In[13]:= initializeNotebook[nb_NotebookObject] :=
  73. Through@{appendNDE, appendNEA,
  74. SetOptions[#, CreateCellID -> True] &,
  75. (CurrentValue[#, {TaggingRules, "undoSettings",
  76. "frequency"}] = $defaultFrequency) &}[
  77. nb];
  78.  
  79. In[14]:= CurrentValue[nb_]
  80.  
  81. Out[14]= FrontEnd`CurrentValue[$CellContext`nb_]
  82.  
  83. In[15]:= cellEventActions =
  84. CellEventActions :> {PassEventsDown -> True,
  85. "KeyDown" :>
  86. If[CurrentValue["EventKey"] =!= None,
  87. undoSave[CurrentValue[CellID]]],
  88. "MouseClicked" :>
  89. If[CurrentValue["AltKey"], undoLoad@CurrentValue@CellID]};
  90.  
  91. In[16]:= ClearAll[undoSave];
  92. undoSave[id_Integer] :=
  93. If[Mod[ ++ idCounter[id],
  94.  
  95. CurrentValue[
  96. EvaluationNotebook[], {TaggingRules, "undoSettings",
  97. "frequency"}]] === 0,
  98. undoSave[ToString@id,
  99. CurrentValue[
  100. EvaluationNotebook[], {TaggingRules, "undoNotebook"}]]];
  101.  
  102. undoSave[id_, undoNb_] := (If[
  103. NotebookFind[undoNb, ToString@id <> "End", All,
  104. CellTags] === $Failed,
  105. createCellIDZone[id][undoNb],
  106. SelectionMove[undoNb, Before, Cell]];
  107. copyCell[EvaluationNotebook[], undoNb]);
  108.  
  109. In[19]:= createCellIDZone[id_][
  110. nb_NotebookObject] := (ToString@id /.
  111. idStr_ :> {idStr <> "Start", idStr <> "End"} /. {stag_, etag_} :>
  112. FrontEndExecute[{
  113. FrontEnd`SelectionMove[nb, After, CellGroup],
  114. FrontEnd`NotebookWrite[nb,
  115. Cell@CellGroupData[{Cell["Start", CellTags -> stag],
  116. Cell["End", CellTags -> etag]}]],
  117. FrontEnd`SelectionMove[undoNb, Previous, Cell],
  118. FrontEnd`SelectionMove[undoNb, Before, Cell]}];
  119. nb)
  120.  
  121. In[20]:= undoNb :=
  122. CurrentValue[EvaluationNotebook[], {TaggingRules, "undoNotebook"}];
  123.  
  124. In[21]:= idCounter[_] = 0;
  125.  
  126. In[22]:= ClearAll[doSthWithWholeCellSelected];
  127. doSthWithWholeCellSelected[sth_, what_: Cell][nb_] :=
  128. With[{val = CurrentValue[nb, ShowSelection],
  129. cursor = ("CursorPosition" /. Developer`CellInformation[nb])[[1,
  130. 1]]},
  131. FrontEndExecute[{
  132. FrontEnd`SetOptions[nb, ShowSelection -> False],
  133. FrontEnd`SelectionMove[nb, All, what, AutoScroll -> False]}];
  134. With[{cell = sth[nb]},
  135. {FrontEnd`SelectionMove[nb, Before, CellContents,
  136. AutoScroll -> False],
  137. FrontEnd`SelectionMove[nb, Next, Character, cursor,
  138. AutoScroll -> False],
  139. FrontEnd`SetOptions[nb, ShowSelection -> val]} /.
  140. l : {a_, b_, c_} :>
  141. If[cursor > 0, FrontEndExecute@l, FrontEndExecute[{a, c}]];
  142. cell
  143. ]
  144. ]
  145.  
  146. In[24]:= getCurrentCell[nb_NotebookObject] :=
  147. doSthWithWholeCellSelected[NotebookRead][nb];
  148.  
  149. In[25]:= copyCell[nbFrom_, nbTo_] :=
  150. NotebookWrite[nbTo, getCurrentCell[nbFrom]]
  151.  
  152. In[26]:= replaceCurrentCellContents[nb_NotebookObject, newCell_] :=
  153. doSthWithWholeCellSelected[NotebookWrite[#, newCell] &,
  154. CellContents][nb];
  155.  
  156. In[27]:= undoLoad[id_Integer] :=
  157. undoLoad[ToString@id,
  158. CurrentValue[EvaluationNotebook[], {TaggingRules, "undoNotebook"}]];
  159.  
  160. undoLoad[id_, nb_] := If[(* if it is found *)
  161. NotebookFind[undoNb, ToString@id <> "End", All,
  162. CellTags] =!= $Failed,
  163. SelectionMove[undoNb, Previous, Cell];
  164. replaceCurrentCellContents[EvaluationNotebook[],
  165. If[CurrentValue[NotebookSelection@undoNb, CellTags] =!=
  166. ToString@id <> "Start", (NotebookDelete[undoNb]; #) &@
  167. NotebookRead[undoNb], SelectionMove[undoNb, After, Cell]; ""]]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement