]> gitweb @ CieloNegro.org - sugar.git/blob - dot-files/_vimperator/plugin/relative_move_js
Auto commit by The Sugar System.
[sugar.git] / dot-files / _vimperator / plugin / relative_move_js
1 // Vimperator plugin: Relative Move
2 // Version: 0.3
3 //
4 // Usage:
5 //   If you stay "http://example.com/aaa/bbb/ccc"
6 //
7 //   :ropen ddd
8 //     move to http://example.com/aaa/bbb/cccddd
9 //   :ropen ./ddd
10 //     move to http://example.com/aaa/bbb/ccc/ddd
11 //   :ropen ../ddd
12 //     move to http://example.com/aaa/bbb/ddd
13 //   :ropen ../../ddd
14 //     move to http://example.com/aaa/ddd
15 //   :ropen /fuga
16 //     move to http://example.com/ddd
17
18
19 (function (){
20     function trim_query(url){
21         var _r;
22         var res = (_r = url.match(/^.*(?=\?)/)) ? _r[0] : url;
23         res = (_r = res.match(/^https?:\/\/.*(?=https?:\/\/)/)) ? _r[0] : res;
24         res = (_r = url.match(/^.*(?=#)/)) ? _r[0] : res;
25         return res;
26     }
27
28     function open_path(path, tab){
29         var win = window.content.window;
30         var loc = win.location;
31         var splited_path = path.toString().split(/\/+/);
32         var up = 0;
33
34         if(!tab){
35             tab = liberator.CURRENT_TAB;
36         }
37
38         switch(splited_path[0]){
39             case ".":
40                 up = -1;
41                 break;
42             case "..":
43                 while(splited_path[up] == "..") up++;
44                 break;
45             case "":
46                 up = -2;
47                 break;
48             default:
49                 break;
50         }
51
52         var url, base;
53         switch(up){
54             case -2: // "/hoge"
55                 base = loc.protocol + "//" + loc.hostname;
56                 url = base + path;
57                 break;
58             case -1: // "./hoge"
59                 base = trim_query(loc.href);
60                 path = path.toString().substring(2);
61                 if(base[base.length-1] == "/")
62                     url = base + path;
63                 else
64                     url = base + "/" + path;
65                 break;
66             case 0: // "hoge"
67                 url = loc.href + path;
68                 break;
69             default: // "../../hoge"
70                 base = trim_query(loc.href);
71                 let c = 0;
72                 while(c < up){
73                     if(c > 0) base = base.substr(0, base.length-1);
74                     [base] = base.match(/^.*\/(?=[^\/]*$)/);
75                     path = path.toString().substring(3);
76                     c++;
77                 }
78                 url = base + path;
79             break;
80         }
81         liberator.open(url, tab);
82     }
83
84     commands.addUserCommand(
85         ["ro[pen]"],
86         "Open relative URL in the current tab",
87         open_path
88     );
89
90     commands.addUserCommand(
91         ["rt[abopen]"],
92         "Open relative URL in a new tab",
93         function(path){
94             open_path(path, liberator.NEW_TAB);
95         }
96     );
97 })();