Menu

Searching: VC++ style

s0larian
2007-11-27
2012-11-13
  • s0larian

    s0larian - 2007-11-27

    Hey there, I have just made a small fix to enable VC++ style search, where the volatile search (Ctrl-F3 in my keys) puts the search item in the history, thus allowing to continue searching with F3/Shift-F3.

    My patch is below. It's not the cleanest, but is hopefully the smallest. An invisible Find window is created in order to allow search re-runs.

    Index: Notepad_plus.cpp

    --- Notepad_plus.cpp    (revision 73)
    +++ Notepad_plus.cpp    (working copy)
    @@ -2543,21 +2543,28 @@
             case IDM_SEARCH_VOLATILE_FINDNEXT :
             case IDM_SEARCH_VOLATILE_FINDPREV :
             {
    +            // Ensure that the find dialog is created
    +            _findReplaceDlg.createDialog(FIND_DLG, _isRTL);
    +
    +            // Get the focus back
    +            ::SetFocus(_pEditView->_hSelf);
    +
    +            // Expand selection to a whole word if there is nothing selected
                 CharacterRange range = _pEditView->getSelection();
                 if (range.cpMin == range.cpMax)
                 {
                     _pEditView->expandWordSelection();
                 }
    -            else
    -            {
    -                char text2Find[MAX_PATH];
    -                _pEditView->getSelectedText(text2Find, sizeof(text2Find));

    -                FindOption op;
    -                op._isWholeWord = false;
    -                op._whichDirection = (id == IDM_SEARCH_VOLATILE_FINDNEXT?DIR_DOWN:DIR_UP);
    -                _findReplaceDlg.processFindNext(text2Find, &op);
    -            }
    +            // and now run a search
    +            char text2Find[MAX_PATH];
    +            _pEditView->getSelectedText(text2Find, sizeof(text2Find));
    +
    +            FindOption op;
    +            op._isWholeWord = false;
    +            op._whichDirection = (id == IDM_SEARCH_VOLATILE_FINDNEXT?DIR_DOWN:DIR_UP);
    +
    +            _findReplaceDlg.processFindNext(text2Find, &op);
                 break;
             }
             case IDM_SEARCH_MARKALL :
    Index: ScitillaComponent/FindReplaceDlg.cpp
    ===================================================================
    --- ScitillaComponent/FindReplaceDlg.cpp    (revision 73)
    +++ ScitillaComponent/FindReplaceDlg.cpp    (working copy)
    @@ -176,7 +176,8 @@
         if (enableDlgTheme)
             enableDlgTheme(_hSelf, ETDT_ENABLETAB);

    -    goToCenter();
    +    if (_dialogIsVisible)
    +        goToCenter();
    }

    void FindReplaceDlg::updateCombos()
    @@ -792,6 +793,11 @@

             (*_ppEditView)->scroll(0, nbColumn2Scroll);
         }
    +
    +    // We have successfully executed a search (volatile or normal) update
    +    // search history with this new item
    +    setSearchText(txt2find);
    +
         return true;
    }

    Index: ScitillaComponent/FindReplaceDlg.h

    --- ScitillaComponent/FindReplaceDlg.h    (revision 73)
    +++ ScitillaComponent/FindReplaceDlg.h    (working copy)
    @@ -159,7 +159,8 @@
    {
    friend class FindIncrementDlg;
    public :
    -    FindReplaceDlg() : StaticDialog(), _pFinder(NULL), _isRTL(false), _isRecursive(true), _maxNbCharAllocated(1024), _fileNameLenMax(1024) {
    +    FindReplaceDlg() : StaticDialog(), _pFinder(NULL), _isRTL(false), _isRecursive(true), _maxNbCharAllocated(1024),
    +        _fileNameLenMax(1024), _dialogIsVisible(false) {
             _line = new char[_maxNbCharAllocated + 3];
             _uniCharLine = new char[(_maxNbCharAllocated + 3) * 2];
             _uniFileName = new char[(_fileNameLenMax + 3) * 2];
    @@ -204,21 +205,28 @@
             ::SendMessage(::GetDlgItem(_hSelf, IDC_DISPLAYPOS_BOTTOM), BM_SETCHECK, BST_CHECKED, 0);
         };

    +    void createDialog(DIALOG_TYPE whichType, bool isRTL = false) {
    +        if (!isCreated())
    +        {
    +            create(IDD_FIND_REPLACE_DLG, isRTL);
    +            _isRTL = isRTL;
    +        }
    +
    +        if (whichType == FINDINFILES_DLG)
    +            enableFindInFilesFunc();
    +        else
    +            enableReplaceFunc(whichType == REPLACE_DLG);
    +    }
    +
         void doDialog(DIALOG_TYPE whichType, bool isRTL = false) {
    -        if (!isCreated())
    -        {
    -            create(IDD_FIND_REPLACE_DLG, isRTL);
    -            _isRTL = isRTL;
    -        }
    +        createDialog(whichType, isRTL);

    -        if (whichType == FINDINFILES_DLG)
    -            enableFindInFilesFunc();
    -        else
    -            enableReplaceFunc(whichType == REPLACE_DLG);
    +        _dialogIsVisible = true;

             ::SetFocus(::GetDlgItem(_hSelf, IDFINDWHAT));
             display();
    -    };
    +    }
    +
         bool processFindNext(const char *txt2find, FindOption *options = NULL);
         bool processReplace();

    @@ -301,8 +309,8 @@
         bool _doMarkLine;
         bool _doStyleFoundToken;
         bool _isInSelection;
    +    bool _dialogIsVisible;

    -
         RECT _findClosePos, _replaceClosePos, _findInFilesClosePos;

         ScintillaEditView **_ppEditView;

     
    • s0larian

      s0larian - 2007-11-27

      Hey Stephen, I've never seen your message as I have just started using notepad++ :) This was the first thing that pissed me off and I fixed it.

      As for the fix, the find dialog contains the code for re-running the search and, as such, is only available after an initial search has been done. IE the model is as follows: create the search dialog, run the first search, re-run last search.

      A better fix would be to refactor the code to de-couple search operations from the dialog, but that would cause a LOT more changes. I'll look at that at some stage, if Don accepts this fix. I have one more fix for incremental search in my tree.

       
    • Stephen Boissiere

      Since I posted a message about this a few weeks ago I for one would be really happy to see this become part of the main build. I was hoping somebody (I guess Don) would have an opinion on if it should be an option in the preferences dialog or something else.

      Whether or not this change was in response to my message, thanks!

      Out of interest when I looked at the code with a view to doing this I only thought it would be neccessary to call 'setSearchText'. Why is the invisible dialog required?