summaryrefslogtreecommitdiffstats
path: root/kjsembed/tdescript/swaptabs.js
blob: 20d19248d6c0f5d897397e467f021c0f22caa922 (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
#!/usr/bin/env kjscmd
var appID = "kate";
StdDirs.addResourceType("swaptabs", StdDirs.kde_default("data") + "/kate/scripts");

var client = new DCOPClient();
var config = new Config( this, "swaptabsrc" );
var documentIndex = client.call(appID, "KateDocumentManager", "activeDocumentNumber()");
var ui = StdDirs.findResource("swaptabs","swaptabs.ui");
var dlg = Factory.loadui(ui);

// Load prefs
dlg.count.value = config.readNumEntry("Spaces", 8 );
dlg.swap.selectedId = config.readNumEntry("Mode", 0 );

if( dlg.exec() == 1 )
{
	var spaces = dlg.count.value;

	var sourceText;
	var destText;
	if( dlg.selection.checked )
		sourceText = client.call(appID, "SelectionInterface#" + documentIndex, "selection()");
	else
		sourceText = client.call(appID, "EditInterface#" + documentIndex, "text()");

	if( dlg.swap.selectedId == 0 )
		destText = replaceSpaces( spaces, sourceText );
	else
		destText = replaceTabs( spaces, sourceText );

	if( dlg.selection.checked )
	{
		if( client.call(appID, "SelectionInterface#" + documentIndex, "hasSelection()") )
		{
			var startLine = client.call(appID, "SelectionInterfaceExt#" + documentIndex, "selStartLine()");
			var startCol = client.call(appID, "SelectionInterfaceExt#" + documentIndex, "selStartCol()");
			client.call(appID, "SelectionInterface#" + documentIndex, "removeSelectedText()");
			client.call(appID, "SelectionInterface#" + documentIndex, "clearSelection()");
			client.call(appID, "EditInterface#" + documentIndex, "insertText(uint,uint,TQString)", startLine, startCol, destText);
		}
		else
		{
			alert("You must first select text.");
			return false;
		}
	}
	else
		client.call(appID, "EditInterface#" + documentIndex, "setText(TQString)", destText );

	// save prefs
	config.writeNumEntry("Spaces", dlg.count.value );
	config.writeNumEntry("Mode", dlg.swap.selectedId );
}

function replaceSpaces( count, text )
{
	var regExp = new RegExp("[ ]{"+count+","+count+"}", "g");
	regExp.mulitline = true;

	returnText = text.replace( regExp, "\t");
	return returnText;
}

function replaceTabs( count, text )
{
	
	var regExp = new RegExp("[\t]","g");
	regExp.mulitline = true;

	var spaces = "";
	for( var idx = 0; idx < count; ++idx)
		spaces += " ";
	returnText =  text.replace( regExp, spaces);
	return returnText;
}