0
$\begingroup$

I want to dynamically highlight all substrings that are equal to selected substring inside string str. Also I want the string str to be displayed in a nice fashion, say, in a grid of 25 characters per row.

Example:

If I start selecting substring continuously by dragging the mouse over - M, Ma, Mat, Math, Mathe, Mathem, Mathema, Mathemat, Mathemati, Mathematic, Mathematica - I want all substring to highlight with my mouse movement. After I finish selection by releasing mouse button all the substrings remain highlighted by red color. Then I could select another substring this time highlighted with blue color, and so on...

Is it possible to do it in a DynamicModule?

str = "Mathematica® Stack Exchange is a question and answer site for \
users of Wolfram Mathematica and the Wolfram Language. It's built and \
run by you as part of the Stack Exchange network of Q&A sites. With \
your help, we're working together to build a library of detailed \
answers to every question about Mathematica.";
Partition[Characters[str], 25, 25, {1, 1}, "_"] // Grid

enter image description here

enter image description here

enter image description here

$\endgroup$
2

1 Answer 1

0
$\begingroup$

The output was created by selecting substrings "Mathematica", "question", "the", "Wolf" and "d answer" one by one in the first part of the output.

I had to divide the code into two dynamic parts - the first part for selecting strings and the highlighting is displayed by the second part of the code.

It would be more elegant to have selecting and highlighting in one place but I could not figure out how to do it.

It is also quite frustrating to have such a long cumbersome code for a simple task - a task that has also practical applications.

There is also some issue with selecting substring from the very beginning (or end) of text so I had to prepend and append _ to the original string - so avoid selecting those boundary characters _.

str = "Mathematica® Stack Exchange is a question and answer site for \
users of Wolfram Mathematica and the Wolfram Language. It's built and \
run by you as part of the Stack Exchange network of Q&A sites. With \
your help, we're working together to build a library of detailed \
answers to every question about Mathematica.";
val = "";
rep = {};
DynamicModule[{}, 
 EventHandler[
  Dynamic@Style[
    Riffle[StringPartition["_" <> str <> "_", UpTo[25]], "\n"] // 
     StringJoin, 
    LineSpacing -> {1, 1}], {"MouseUp" :> (val = 
      CurrentValue["SelectionData"]; 
     rep = Prepend[rep, {val, Length[rep] + 1}])}, 
  PassEventsDown -> True]]

Dynamic[Grid[
  Partition[
   ReplacePart[Characters[str], 
    Flatten[Map[
      d |-> (# -> 
           Highlighted[StringTake[str, {#}], 
            Background -> 
             ColorData[97, "ColorList"][[Mod[d[[2]], 15, 1]]], 
            FrameMargins -> 0, ContentPadding -> True] & /@ 
         Flatten[Range @@@ 
           StringPosition[str, 
            StringDelete[
             d[[1]] /. ($Failed) -> "" /. RowBox -> StringJoin, 
             "\n"]]]), rep]]], 25, 25, {1, 1}, ""], 
  Spacings -> {0, Automatic}]]

enter image description here

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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