summaryrefslogtreecommitdiffstats
path: root/kate/part/katetemplatehandler.cpp
blob: 9607f6b9a21791f801f9f8b9c7f6cb08cf6eb89c (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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* This file is part of the KDE libraries
  Copyright (C) 2004 Joseph Wenninger <jowenn@kde.org>

  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.
*/
#include "katetemplatehandler.h"
#include "katetemplatehandler.moc"
#include "katedocument.h"
#include "katesupercursor.h"
#include "katearbitraryhighlight.h"
#include "kateview.h"
#include <tqregexp.h>
#include <kdebug.h>
#include <tqvaluelist.h>

KateTemplateHandler::KateTemplateHandler(
  KateDocument *doc,
  uint line, uint column,
  const TQString &templateString,
  const TQMap<TQString, TQString> &initialValues )
    : TQObject( doc )
    , KateKeyInterceptorFunctor()
    , m_doc( doc )
    , m_currentTabStop( -1 )
    , m_currentRange( 0 )
    , m_initOk( false )
    , m_recursion( false )
{
  connect( m_doc, TQT_SIGNAL( destroyed() ), this, TQT_SLOT( slotDocumentDestroyed() ) );
  m_ranges = new KateSuperRangeList( false, this ); //false/*,this*/);

  if ( !m_doc->setTabInterceptor( this ) )
  {
    deleteLater();
    return ;
  }

  KateArbitraryHighlight *kah = doc->arbitraryHL();
  /*KateArbitraryHighlightRange *hlr=new KateArbitraryHighlightRange(doc,KateTextCursor(line,column),
   KateTextCursor(line,column+3));
  hlr->setUnderline(true);
  hlr->setOverline(true);
  l->append(hlr);*/
  TQValueList<KateTemplateHandlerPlaceHolderInfo> buildList;
  TQRegExp rx( "([$%])\\{([^}\\s]+)\\}" );
  rx.setMinimal( true );
  int pos = 0;
  int opos = 0;
  TQString insertString = templateString;

  while ( pos >= 0 )
  {
    pos = rx.search( insertString, pos );

    if ( pos > -1 )
    {
      if ( ( pos - opos ) > 0 )
      {
        if ( insertString[ pos - 1 ] == '\\' )
        {
          insertString.remove( pos - 1, 1 );
          opos = pos;
          continue;
        }
      }

      TQString placeholder = rx.cap( 2 );
      TQString value = initialValues[ placeholder ];

      // don't add %{MACRO} to the tab navigation, unless there was not value
      if ( rx.cap( 1 ) != "%" || placeholder == value )
        buildList.append( KateTemplateHandlerPlaceHolderInfo( pos, value.length(), placeholder ) );

      insertString.replace( pos, rx.matchedLength(), value );
      pos += value.length();
      opos = pos;
    }
  }

  doc->editStart();

  if ( !doc->insertText( line, column, insertString ) )
  {
    deleteLater();
    doc->editEnd();
    return ;
  }

  if ( buildList.isEmpty() )
  {
    m_initOk = true;
    deleteLater();
    doc->editEnd();
    return ;
  }

  doc->undoSafePoint();
  doc->editEnd();
  generateRangeTable( line, column, insertString, buildList );
  kah->addHighlightToDocument( m_ranges );

  for ( KateSuperRangeList::const_iterator it = m_ranges->begin();it != m_ranges->end();++it )
  {
    m_doc->tagLines( ( *it ) ->start().line(), ( *it ) ->end().line() );
  }

  /* connect(doc,TQT_SIGNAL(charactersInteractivelyInserted(int ,int ,const TQString&)),this,
   TQT_SLOT(slotCharactersInteractivlyInserted(int,int,const TQString&)));
   connect(doc,TQT_SIGNAL(charactersSemiInteractivelyInserted(int ,int ,const TQString&)),this,
   TQT_SLOT(slotCharactersInteractivlyInserted(int,int,const TQString&)));*/
  connect( doc, TQT_SIGNAL( textInserted( int, int ) ), this, TQT_SLOT( slotTextInserted( int, int ) ) );
  connect( doc, TQT_SIGNAL( aboutToRemoveText( const KateTextRange& ) ), this, TQT_SLOT( slotAboutToRemoveText( const KateTextRange& ) ) );
  connect( doc, TQT_SIGNAL( textRemoved() ), this, TQT_SLOT( slotTextRemoved() ) );

  ( *this ) ( TQt::Key_Tab );
}

KateTemplateHandler::~KateTemplateHandler()
{
  m_ranges->setAutoManage( true );

  if ( m_doc )
  {
    m_doc->removeTabInterceptor( this );

    for ( KateSuperRangeList::const_iterator it = m_ranges->begin();it != m_ranges->end();++it )
    {
      m_doc->tagLines( ( *it ) ->start().line(), ( *it ) ->end().line() );
    }
  }

  m_ranges->clear();
}

void KateTemplateHandler::slotDocumentDestroyed() {m_doc = 0;}

void KateTemplateHandler::generateRangeTable( uint insertLine, uint insertCol, const TQString& insertString, const TQValueList<KateTemplateHandlerPlaceHolderInfo> &buildList )
{
  uint line = insertLine;
  uint col = insertCol;
  uint colInText = 0;

  for ( TQValueList<KateTemplateHandlerPlaceHolderInfo>::const_iterator it = buildList.begin();it != buildList.end();++it )
  {
    KateTemplatePlaceHolder *ph = m_dict[ ( *it ).placeholder ];

    if ( !ph )
    {
      ph = new KateTemplatePlaceHolder;
      ph->isInitialValue = true;
      ph->isCursor = ( ( *it ).placeholder == "cursor" );
      m_dict.insert( ( *it ).placeholder, ph );

      if ( !ph->isCursor ) m_tabOrder.append( ph );

      ph->ranges.setAutoManage( false );
    }

    // FIXME handle space/tab replacement correctly make it use of the indenter
    while ( colInText < ( *it ).begin )
    {
      ++col;

      if ( insertString.at( colInText ) == '\n' )
      {
        col = 0;
        line++;
      }

      ++colInText;
    }

    KateArbitraryHighlightRange *hlr = new KateArbitraryHighlightRange( m_doc, KateTextCursor( line, col ),
                                       KateTextCursor( line, ( *it ).len + col ) );
    colInText += ( *it ).len;
    col += ( *it ).len;
    hlr->allowZeroLength();
    hlr->setUnderline( true );
    hlr->setOverline( true );
    //hlr->setBehaviour(KateSuperRange::ExpandRight);
    ph->ranges.append( hlr );
    m_ranges->append( hlr );
  }

  KateTemplatePlaceHolder *cursor = m_dict[ "cursor" ];

  if ( cursor ) m_tabOrder.append( cursor );
}

void KateTemplateHandler::slotTextInserted( int line, int col )
{
#ifdef __GNUC__
#warning FIXME undo/redo detection
#endif

  if ( m_recursion ) return ;

  //if (m_editSessionNumber!=0) return; // assume that this is due an udno/redo operation right now
  KateTextCursor cur( line, col );

  if ( ( !m_currentRange ) ||
       ( ( !m_currentRange->includes( cur ) ) && ( ! ( ( m_currentRange->start() == m_currentRange->end() ) && m_currentRange->end() == cur ) )
       ) ) locateRange( cur );

  if ( !m_currentRange ) return ;

  KateTemplatePlaceHolder *ph = m_tabOrder.at( m_currentTabStop );

  TQString sourceText = m_doc->text ( m_currentRange->start().line(), m_currentRange->start().col(),
                                     m_currentRange->end().line(), m_currentRange->end().col(), false );

  ph->isInitialValue = false;
  bool undoDontMerge = m_doc->m_undoDontMerge;
  Q_ASSERT( m_doc->editSessionNumber == 0 );
  m_recursion = true;

  m_doc->editStart( /*false*/ );

  for ( KateSuperRangeList::const_iterator it = ph->ranges.begin();it != ph->ranges.end();++it )
  {
    if ( ( *it ) == m_currentRange ) continue;

    KateTextCursor start = ( *it ) ->start();
    KateTextCursor end = ( *it ) ->end();
    m_doc->removeText( start.line(), start.col(), end.line(), end.col(), false );
    m_doc->insertText( start.line(), start.col(), sourceText );
  }

  m_doc->m_undoDontMerge = false;
  m_doc->m_undoComplexMerge = true;
  m_doc->undoSafePoint();
  m_doc->editEnd();
  m_doc->m_undoDontMerge = undoDontMerge;
  m_recursion = false;

  if ( ph->isCursor ) deleteLater();
}

void KateTemplateHandler::locateRange( const KateTextCursor& cursor )
{
  /* if (m_currentRange) {
    m_doc->tagLines(m_currentRange->start().line(),m_currentRange->end().line());

   }*/

  for ( uint i = 0;i < m_tabOrder.count();i++ )
  {
    KateTemplatePlaceHolder *ph = m_tabOrder.at( i );

    for ( KateSuperRangeList::const_iterator it = ph->ranges.begin();it != ph->ranges.end();++it )
    {
      if ( ( *it ) ->includes( cursor ) )
      {
        m_currentTabStop = i;
        m_currentRange = ( *it );
        //m_doc->tagLines(m_currentRange->start().line(),m_currentRange->end().line());
        return ;
      }
    }

  }

  m_currentRange = 0;
  /*while (m_ranges->count()>0)
   delete (m_ranges->take(0));
  disconnect(m_ranges,0,0,0);
  delete m_ranges;*/
  deleteLater();
}


bool KateTemplateHandler::operator() ( KKey key )
{
  if ( key==TQt::Key_Tab )
  {
    m_currentTabStop++;

    if ( m_currentTabStop >= ( int ) m_tabOrder.count() )
      m_currentTabStop = 0;
  }
  else
  {
    m_currentTabStop--;

    if ( m_currentTabStop < 0 ) m_currentTabStop = m_tabOrder.count() - 1;
  }

  m_currentRange = m_tabOrder.at( m_currentTabStop ) ->ranges.at( 0 );

  if ( m_tabOrder.at( m_currentTabStop ) ->isInitialValue )
  {
    m_doc->activeView()->setSelection( m_currentRange->start(), m_currentRange->end() );
  }
  else m_doc->activeView()->setSelection( m_currentRange->end(), m_currentRange->end() );

  m_doc->activeView() ->setCursorPositionReal( m_currentRange->end().line(), m_currentRange->end().col() );
  m_doc->activeView() ->tagLine( m_currentRange->end() );

  return true;
}

void KateTemplateHandler::slotAboutToRemoveText( const KateTextRange &range )
{
  if ( m_recursion ) return ;

  if ( m_currentRange && ( !m_currentRange->includes( range.start() ) ) ) locateRange( range.start() );

  if ( m_currentRange != 0 )
  {
    if ( m_currentRange->end() <= range.end() ) return ;
  }

  if ( m_doc )
  {
    disconnect( m_doc, TQT_SIGNAL( textInserted( int, int ) ), this, TQT_SLOT( slotTextInserted( int, int ) ) );
    disconnect( m_doc, TQT_SIGNAL( aboutToRemoveText( const KateTextRange& ) ), this, TQT_SLOT( slotAboutToRemoveText( const KateTextRange& ) ) );
    disconnect( m_doc, TQT_SIGNAL( textRemoved() ), this, TQT_SLOT( slotTextRemoved() ) );
  }

  deleteLater();
}

void KateTemplateHandler::slotTextRemoved()
{
  if ( m_recursion ) return ;
  if ( !m_currentRange ) return ;

  slotTextInserted( m_currentRange->start().line(), m_currentRange->start().col() );
}