3
$\begingroup$

I have a notebook in outline form and a docked cell with a TextField[] for searching. I'd hide and show cells in a notebook to filter the cells based on the search string (any maybe type of cell and tags eventually too).

Here is how to get the searchbar in place:

searchBar = ExpressionCell[InputField[Dynamic[search], String, ContinuousAction -> True]];
SetOptions[EvaluationNotebook[], DockedCells -> Cell[ToBoxes@sb, "DockedCell"]];

Now I'm stuck. How to filter all the cells in a performant and safe way? I'm thinking some combination of Cells[], NotebookFind[], NotebookRead[], and SetOptions[] could make it work....

$\endgroup$
  • $\begingroup$ It sounds like what probably what you want/need to is dynamically set certain cells as closed while others remain open based on your search criteria?? But also is your search to be of cell contents, cell tags, cell something else... $\endgroup$ – Mike Honeychurch Jan 5 '15 at 1:08
1
$\begingroup$

Actually, it is really interesting task :) So, this code is working so as I understood your goal:

    list = {{1, 2}, {3, 4}, {5, 6}};

    Manipulate[list[[i]], {i, 1, Length@list, 1}]


   searchBar = 
      ExpressionCell[
       Row@{InputField[Dynamic[search], String, ContinuousAction -> True],
          "  ", Button["search", sdm], "  ", Button["show all", sa]}];
    SetOptions[EvaluationNotebook[], 
      DockedCells -> Cell[ToBoxes@searchBar, "DockedCell"]];

   sa := DynamicModule[{nb},
      nb = EvaluationNotebook[];
      SetOptions[#, CellOpen -> True, ShowCellBracket -> True] & /@ Cells[nb]
      ]

   sdm := DynamicModule[{nb},
      nb = EvaluationNotebook[];
      NotebookFind[nb, search, All];
      SetOptions[#, CellOpen -> False, ShowCellBracket -> False] & /@ Cells[nb];
      SetOptions[#, CellOpen -> True, ShowCellBracket -> True] & /@ SelectedCells[nb];
      ]

Fisrt two strings creates a test-content which will be a target for filtering. I've used the querry with numbers 1,2,3 which present just at the first two strings :)

Searchbar is a little bit modified version of yours searchbar with addition of buttons which are realising the trigger action for filtering.

The sa and sdm are code which namely filter the notebook content and hide the cells which does not contain anything coinciding with search pattern.

So, just enter your search querry into the search textfield and push the "search" button, or reset your filter by "show all" button.

I guess it is satisfing your requirements.

P.S. I've added the additional option for cells, ShowCellBracket to hide the brackets corresponding to the hidden cells which present at the right side of a notebook.

P.P.S. You should change the DynamicModule to usual Module if you wish to make the shown cells evaluatable as usually. I mean that the newly evaluated output which satisfy the hiding conditions will be hidden automatically in the case of DynamicModule.

$\endgroup$
  • 1
    $\begingroup$ Very interesting. Is there a way we could keep the group openers state? And what about a search/filter on only the selected cells? $\endgroup$ – P. Fonseca Jan 5 '15 at 12:19
  • $\begingroup$ If you want to search inside the results then you need to set more complicated search querry. For example, you can temporarily add the tag "current" for currently shown cells and keep it as one of a querry components during the "search in results" querry.. Visually it could be realized by the different ways, like a check box "search in results" plus corresponding modification of a pattern for a NotebookFind $\endgroup$ – Rom38 Jan 5 '15 at 12:30
  • $\begingroup$ Thanks @Rom38, but there is too much extra space, is there anyway to make the closed cells completely disappear? $\endgroup$ – M.R. Jan 6 '15 at 5:11
  • $\begingroup$ @M.R. Do you mean that there is enough long page even in the case of mostly hidden cells? Actually, I didn't think about economy of a screen area and such method of cell hiding is enough good for me. But you can look into the options of a cells, may be there are another ways present which allows you to hide it totally. I've used SetOption[CellOpen -> False] but may be SetOption[Visible -> False] will be better. $\endgroup$ – Rom38 Jan 6 '15 at 10:55
  • 1
    $\begingroup$ I found how to remove the residuals of the hidden cells. You have to add the following option to the filtered cells options: ShowCellBracket -> True It will remove the brackets corresponding to the hidden cells at right side of notebook. I'll add this to my answer. $\endgroup$ – Rom38 Jan 6 '15 at 12:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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