summaryrefslogtreecommitdiffstats
path: root/kate/modeline/ModelinePlugin.cpp
blob: 9184f6ad18fa64ef00b35cc8cde7dfda2701ecc1 (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
 /***************************************************************************
                          ModelinePlugin.cpp  -  description
                             -------------------
    begin                : Mon Apr 1 2002
    copyright            : (C) 2002 by John Firebaugh
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <tqregexp.h>
#include <kgenericfactory.h>
#include <klocale.h>
#include <kaction.h>
#include <kdebug.h>

#include <kate/application.h>
#include <kate/documentmanager.h>
#include <kate/viewmanager.h>
#include <kate/mainwindow.h>
#include <kate/document.h>
#include <kate/view.h>

#include "ModelinePlugin.h"
#include "ModelinePlugin.moc"
                                 
class PluginView : public KXMLGUIClient
{             
  friend class ModelinePlugin;

  public:
    Kate::MainWindow *win;
};

K_EXPORT_COMPONENT_FACTORY( katemodelineplugin, KGenericFactory<ModelinePlugin>( "katemodeline" ) )

ModelinePlugin::ModelinePlugin( TQObject* parent, const char* name, const TQStringList &args )
	: Kate::Plugin ( (Kate::Application *) parent, name )
{
}

ModelinePlugin::~ModelinePlugin()
{
}

void ModelinePlugin::addView(Kate::MainWindow *win)
{
    // TODO: doesn't this have to be deleted?
    PluginView *view = new PluginView ();
               
    new KAction( i18n("Apply Modeline"), 0,
		this, TQT_SLOT(applyModeline()),
		view->actionCollection(), "edit_apply_modeline" );   
       
    view->setInstance (new KInstance("kate"));
    view->setXMLFile( "plugins/katemodeline/ui.rc" );
    win->guiFactory()->addClient (view);
    view->win = win; 
    
   m_views.append (view);
}   

void ModelinePlugin::removeView(Kate::MainWindow *win)
{
  for (uint z=0; z < m_views.count(); z++)
    if (m_views.at(z)->win == win)
    {
      PluginView *view = m_views.at(z);
      m_views.remove (view);
      win->guiFactory()->removeClient (view);
      delete view;
    }  
}

void ModelinePlugin::applyModeline()
{
        if (!application()->activeMainWindow())
          return;

	Kate::Document* doc = application()->documentManager()->activeDocument();
	Kate::View* view = application()->activeMainWindow()->viewManager()->activeView();
	if( !doc || !view ) return;
	static TQRegExp vim1( "\\b(?:vi:|vim:|ex:)[ \\t](.*)" );
	static TQRegExp vim2( "\\b(?:vi:|vim:|ex:)[ \\t]set (.*):" );
	uint foundAtLine;
	uint foundAtCol;
	uint matchLen;
	TQString options;
	if( doc->searchText( 0, 0, vim2, &foundAtLine, &foundAtCol, &matchLen ) ) {
		options = vim2.cap(1);
	} else if( doc->searchText( 0, 0, vim1, &foundAtLine, &foundAtCol, &matchLen ) ) {
		options = vim1.cap(1);
		options.replace( TQRegExp( ":" ), " " );
	} 
	uint configFlags = doc->configFlags();
	kdDebug() << "Found modeline: " << options << endl;
	if( options.find( TQRegExp( "\\bnoet\\b" ) ) >= 0 ) {
		kdDebug() << "Clearing replace tabs" << endl;
		configFlags &= ~Kate::Document::cfReplaceTabs;
	} else if( options.find( TQRegExp( "\\bet\\b" ) ) >= 0 ) {
		kdDebug() << "Setting replace tabs" << endl;
		configFlags |= Kate::Document::cfReplaceTabs;
	}
	TQRegExp ts( "ts=(\\d+)" );
	if( options.find( ts ) >= 0 ) {
		uint tabWidth = ts.cap(1).toUInt();
		kdDebug() << "Setting tab width " << tabWidth << endl;
		view->setTabWidth( tabWidth );
	}
	TQRegExp tw( "tw=(\\d+)" );
	if( options.find( tw ) >= 0 ) {
		uint textWidth = tw.cap(1).toUInt();
		kdDebug() << "Setting text width " << textWidth << endl;
		doc->setWordWrap( true );
		doc->setWordWrapAt( textWidth );
	}
	doc->setConfigFlags( configFlags );
}