summaryrefslogtreecommitdiffstats
path: root/kate/part/katesearch.h
blob: ebf228dcd60add6e68e48af08f3cd889e387815d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/* This file is part of the KDE libraries
   Copyright (C) 2004-2005 Anders Lund <anders@alweb.dk>
   Copyright (C) 2002 John Firebaugh <jfirebaugh@kde.org>
   Copyright (C) 2001-2004 Christoph Cullmann <cullmann@kde.org>
   Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
   Copyright (C) 1999 Jochen Wilhelmy <digisnap@cs.tu-berlin.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#ifndef __KATE_SEARCH_H__
#define __KATE_SEARCH_H__

#include "katecursor.h"
#include "../interfaces/document.h"

#include <kdialogbase.h>

#include <tqstring.h>
#include <tqregexp.h>
#include <tqstringlist.h>
#include <tqvaluelist.h>

class KateView;
class KateDocument;
class KateSuperRangeList;

class KActionCollection;

class KateSearch : public QObject
{
  Q_OBJECT

  friend class KateDocument;

  private:
    class SearchFlags
    {
      public:
        bool caseSensitive     :1;
        bool wholeWords        :1;
        bool fromBeginning     :1;
        bool backward          :1;
        bool selected          :1;
        bool prompt            :1;
        bool replace           :1;
        bool finished          :1;
        bool regExp            :1;
        bool useBackRefs       :1;
    };

    class SConfig
    {
      public:
        SearchFlags flags;
        KateTextCursor cursor;
        KateTextCursor wrappedEnd; // after wraping around, search/replace until here
        bool wrapped; // have we allready wrapped around ?
        bool showNotFound; // pop up annoying dialogs?
        uint matchedLength;
        KateTextCursor selBegin;
        KateTextCursor selEnd;
    };

  public:
    enum Dialog_results {
      srCancel = KDialogBase::Cancel,
      srAll = KDialogBase::User1,
      srLast = KDialogBase::User2,
      srNo = KDialogBase::User3,
      srYes = KDialogBase::Ok
    };

  public:
    KateSearch( KateView* );
    ~KateSearch();

    void createActions( KActionCollection* );

  public slots:
    void find();
    /**
     * Search for @p pattern given @p flags
     * This is for the commandline "find", and is forwarded by
     * KateView.
     * @param pattern string or regex pattern to search for.
     * @param flags a OR'ed combination of KFindDialog::Options
     * @param add wether this string should be added to the recent search list
     * @param shownotfound wether to pop up "Not round: PATTERN" when that happens.
     * That must now be explicitly required -- the find dialog does, but the commandline
     * incremental search does not.
     */
    void find( const TQString &pattern, long flags, bool add=true, bool shownotfound=false );
    void replace();
    /**
     * Replace @p pattern with @p replacement given @p flags.
     * This is for the commandline "replace" and is forwarded
     * by KateView.
     * @param pattern string or regular expression to search for
     * @param replacement Replacement string.
     * @param flags OR'd combination of KFindDialog::Options
     */
    void replace( const TQString &pattern, const TQString &replacement, long flags );
    void findAgain( bool reverseDirection );

  private slots:
    void replaceSlot();
    void slotFindNext() { findAgain( false ); }
    void slotFindPrev() { findAgain( true );  }

  private:
    static void addToList( TQStringList&, const TQString& );
    static void addToSearchList( const TQString& s )  { addToList( s_searchList, s ); }
    static void addToReplaceList( const TQString& s ) { addToList( s_replaceList, s ); }
    static TQStringList s_searchList; ///< recent patterns
    static TQStringList s_replaceList; ///< recent replacement strings
    static TQString s_pattern; ///< the string to search for

    void search( SearchFlags flags );
    void wrapSearch();
    bool askContinue();

    void findAgain();
    void promptReplace();
    void replaceAll();
    void replaceOne();
    void skipOne();

    TQString getSearchText();
    KateTextCursor getCursor( SearchFlags flags );
    bool doSearch( const TQString& text );
    void exposeFound( KateTextCursor &cursor, int slen );

    inline KateView* view()    { return m_view; }
    inline KateDocument* doc() { return m_doc;  }

    KateView*     m_view;
    KateDocument* m_doc;

    KateSuperRangeList* m_arbitraryHLList;

    SConfig s;

    TQValueList<SConfig> m_searchResults;
    int                 m_resultIndex;

    int           replaces;
    TQDialog*      replacePrompt;
    TQString m_replacement;
    TQRegExp m_re;
};

/**
 * simple replace prompt dialog
 */
class KateReplacePrompt : public KDialogBase
{
  Q_OBJECT

  public:
    /**
     * Constructor
     * @param parent parent widget for the dialog
     */
    KateReplacePrompt(TQWidget *parent);

  signals:
    /**
     * button clicked
     */
    void clicked();

  protected slots:
    /**
     * ok pressed
     */
    void slotOk ();

    /**
     * close pressed
     */
    void slotClose ();

    /**
     * replace all pressed
     */
    void slotUser1 ();

    /**
     * last pressed
     */
    void slotUser2 ();

    /**
     * Yes pressed
     */
    void slotUser3 ();

    /**
     * dialog done
     * @param result dialog result
     */
    void done (int result);
};

class SearchCommand : public Kate::Command, public Kate::CommandExtension
{
  public:
    SearchCommand() : m_ifindFlags(0) {;}
    bool exec(class Kate::View *view, const TQString &cmd, TQString &errorMsg);
    bool help(class Kate::View *, const TQString &, TQString &);
    TQStringList cmds();
    bool wantsToProcessText( const TQString &/*cmdname*/ );
    void processText( Kate::View *view, const TQString& text );

  private:
    /**
     * set up properties for incremental find
     */
    void ifindInit( const TQString &cmd );
    /**
     * clear properties for incremental find
     */
    void ifindClear();

    long m_ifindFlags;
};

#endif

// kate: space-indent on; indent-width 2; replace-tabs on;