aboutsummaryrefslogtreecommitdiffstats
path: root/toggler.js
blob: 0f06ca9e0cdd6640ec160be3b47ad3abdde3f05c (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
/**
 * ==VimperatorPlugin==
 * @name toggler
 * @version 0.2
 * @author teramako <teramako at gmail.com>
 * ==/VimperatorPlugin==
 *
 * 何かしらの設定をクルクル変更したい人へ
 *
 * まず、最初に以下の例ように.vimperatorrcに定義する
js <<EOM
liberator.globalVariables.toggler = {
  name: [ setting commands ]],
  go: ["set go=","set go=m","set go=b"],
  sb: ["sbclose","sbar Console"],
  go_and_sb: [
    ["set go=","sbclose"],
    ["set go=mTb","sbar Console"]
  ],
  //...
};
EOM
 *
 * 次に
 * :toggle go
 * とコマンドを実行するとsetting commands配列の次を実行する
 * 最後までいくと最初に戻る
 * つまり、最初に :toggle go をすると set go=m が実行される
 * もう一度実行すると、set go=b が実行される
 * もう一度すると、最初に戻って、set go= が実行される
 *
 * :map <F2> :toggle go<CR>
 * などとやっておくとボタン一つでクルクル替わる
 */
liberator.plugins.toggler = (function(){

var settings = {};
function Toggler(name, cmds){
	if (!cmds || cmds.length < 2) throw new Error("arguments are not enough");
	this.name = name;
	this.cmds = cmds;
	this.index = 0;
}
Toggler.prototype = {
	next: function(notUpdate){
		var index = this.index + 1;
		if (index >= this.cmds.length) index = 0;
		if (!notUpdate) this.index = index;
		return this.cmds[index];
	},
	previous: function(notUpdate){
		var index = this.index - 1;
		if (index < 0) index = this.cmds.length -1;
		if (!notUpdate) this.index = index;
		return this.cmds[this.index];
	},
	list: function() template.table(this.name, this.cmds.map(function(cmd, i) [i==this.index ? "*" : "", cmd], this))
};
var manager = {
	add: function(name, cmds){
		if (name in settings){
			liberator.echoerr(name + " is already exists");
			return;
		}
		settings[name] = new Toggler(name, cmds);
	},
	get: function(name){
		if (name in settings){
			return settings[name];
		}
		liberator.echoerr(name + " is not exists");
		return false;
	},
	toggle: function(name, isPrevious){
		var toggler = this.get(name);
		if (!toggler) return;
		var cmd = isPrevious ? toggler.previous() : toggler.next();
		if (cmd instanceof Array){
			cmd.forEach(liberator.execute);
		} else if (typeof cmd == "function"){
			cmd();
		} else {
			liberator.execute(cmd);
		}
	},
	reload: function(){
		if (liberator.globalVariables.toggler){
			settings = {};
			for (let [name, toggler] in Iterator(liberator.globalVariables.toggler))
				this.add(name, toggler);
		}
	},
	list: function(name){
		var xml = <></>;
		if (name && (name in settings)){
			xml += settings[name].list();
		} else {
			for each (let setting in settings){
				xml += setting.list();
			}
		}
		liberator.echo(xml, true);
	}
};

commands.addUserCommand(["toggle"],"setting toggler",
	function(args){
		if (args["-list"] || args.length == 0){
			if (args.length == 0)
				liberator.plugins.toggler.list();
			else
				args.forEach(liberator.plugins.toggler.list);
			return;
		}
		args.forEach(function(name){
			liberator.plugins.toggler.toggle(name, args.bang);
		});
	},{
		argCount: "*",
		bang: true,
		options: [
			[["-list","-l"], commands.OPTION_NOARG]
		],
		completer: function(context,args){
			var filter = args.length > 0 ? args[args.length-1] : "";
			var reg = new RegExp(filter ? "^" + filter : "");
			context.title= ["Name", args.bang ? "Previous" : "Next"];
			var list = [];
			for (let i in settings){
				let toggler = settings[i];
				if (reg.test(i.toLowerCase()) && !args.some(function(arg) arg==i))
					list.push([i, args.bang ? toggler.previous(true) : toggler.next(true)]);
			}
			context.completions = list;
		}
	},
	true);

manager.reload();
return manager;
})();

ateDocument(null, 'html', null); var range = document.createRange(); range.setStartAfter(window.content.document.body); res.documentElement.appendChild(res.importNode(range.createContextualFragment(str), true)); return res; } // // // // function httpGET(uri,callback){ var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 200) callback.call(this,xhr.responseText); else throw new Error(xhr.statusText) } }; xhr.open("GET",uri,true); xhr.send(null); } function getNormalizedPermalink(url){ var xhr = new XMLHttpRequest(); xhr.open("GET","http://api.pathtraq.com/normalize_url?url=" + url,false); xhr.send(null); if(xhr.status != 200){ liberator.echoerr("Pathtraq: FAILED to normalize URL!!"); return undefined; } return xhr.responseText; } function getTags(arg){ liberator.plugins.hatena_tags = []; httpGET("http://b.hatena.ne.jp/my", function(mypage_text){ var mypage_html = parseHTML(mypage_text); var tags = getElementsByXPath("//ul[@id=\"tags\"]/li/a",mypage_html); tags.forEach(function(tag){ liberator.plugins.hatena_tags.push(tag.innerHTML); }); liberator.echo("HatenaBookmark: Tag parsing is finished. Taglist length: " + tags.length); }); } getTags(); function addHatenaBookmarks(user,password,url,comment,normalize){ var target = normalize ? getNormalizedPermalink(url) : url; var request = <entry xmlns="http://purl.org/atom/ns#"> <title>dummy</title> <link rel="related" type="text/html" href={target}/> <summary type="text/plain">{comment}</summary> </entry>; var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function(){ if(xhr.readyState == 4){ if(xhr.status == 201) liberator.echo("HatenaBookmark: success"); else liberator.echoerr("HatenaBookmark:" + xhr.statusText); } }; var wsse = new WSSEUtils(user,password); xhr.open("POST","http://b.hatena.ne.jp/atom/post", true); xhr.setRequestHeader("X-WSSE",wsse.getWSSEHeader()); xhr.setRequestHeader("Content-Type","application/atom+xml"); xhr.send(request.toString()); } commands.addUserCommand(['hbtags'],"Update HatenaBookmark Tags", getTags, {} ); commands.addUserCommand(['hb'],"Post to HatenaBookmark", function(args){ var arg = args.string; try { var passwordManager = Cc["@mozilla.org/login-manager;1"].getService(Ci.nsILoginManager); var logins = passwordManager.findLogins({}, 'https://www.hatena.ne.jp', 'https://www.hatena.ne.jp', null); if(logins.length) [hatenaUser, hatenaPassword] = [logins[0].username, logins[0].password]; else liberator.echoerr("HatenaBookmark: account not found"); } catch(ex){ } addHatenaBookmarks(hatenaUser,hatenaPassword,modules.buffer.URL,arg,isNormalize); },{ completer: function(context, arg, special){ let filter = context.filter; //var match_result = filter.match(/(.*)\[(\w*)$/); //[all, commited, now inputting] var match_result = filter.match(/((?:\[[^\]]*\])+)?\[?(.*)/); //[all, commited, now inputting] //var m = new RegExp("^" + match_result[2]); var m = new RegExp(XMigemoCore ? "^(" + XMigemoCore.getRegExp(match_result[2]) + ")" : "^" + match_result[2],'i'); var completionList = []; liberator.plugins.hatena_tags.forEach(function(tag){ if(m.test(tag)){ completionList.push([(match_result[1] || "") + "[" + tag + "]","Tag"]); } }); context.title = ['Tag','Description']; // context.advance(match_result[1].length); context.completions = completionList; } } ); })(); // vim:sw=4 ts=4 et: