Changeset 20878

Show
Ignore:
Timestamp:
10/07/08 08:07:06 (2 months ago)
Author:
anekos
Message:

修正
・どこに開くかを指定できるようにした
・lopen の仕様をちょっと変更

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/javascript/vimperator-plugins/trunk/lo.js

    r20876 r20878  
    88// 
    99// Usage: 
    10 //    :fo[pen][!] <REGEXP> [-i <INTERVAL_SEC>] 
     10//    :fo[pen][!] <REGEXP> [-i <INTERVAL_SEC>] [-w <WHERE>] 
    1111//      Open filtered links by regexp. 
    1212//      When used "!", open links in foreground. 
    1313// 
    14 //    :lo[pen][!] URI 
     14//    :lo[pen][!] URI [-w <WHERE>] 
    1515//      Open URI 
    1616// 
    1717// Usage-ja: 
    18 //    :fo[pen][!] <ミゲ文字列> [-i <INTERVAL_SEC>] 
    19 //    :fo[pen][!] /<正規表現> [-i <INTERVAL_SEC>] 
     18//    :fo[pen][!] <ミゲ文字列> [-i <INTERVAL_SEC>] [-w <WHERE>] 
     19//    :fo[pen][!] /<正規表現> [-i <INTERVAL_SEC>] [-w <WHERE>] 
    2020//      ミゲ文字列か正規表現でフィルタされたリンクを開く 
    2121// 
    22 //    :lo[pen][!] URI 
     22//    :lo[pen][!] URI [-w <WHERE>] 
    2323//      URI を開く 
    2424// 
     
    2828// Variables: 
    2929//    let g:fopen_default_interval="<INTERVAL_SEC>" 
    30 // 
    31 // Notice: 
    32 //    "--where" option's implementation has not been completed yet. 
     30 
    3331 
    3432 
     
    4644 
    4745  function makeRegExp (str) { 
    48     if (!migemo) 
    49       return new RegExp(str, 'i'); 
    50     if (str.indexOf('/') == 0) 
    51       return new RegExp(str.slice(1), 'i'); 
    52     else 
    53       return migemo.getRegExp(str); 
     46    return migemo ? (str.indexOf('/') == 0) ? new RegExp(str.slice(1), 'i') 
     47                                            : migemo.getRegExp(str) 
     48                  : new RegExp(str, 'i'); 
    5449  } 
    5550 
    5651  function filteredLinks (word) { 
    5752    if (word.match(/^\s*$/)) 
    58       return []; // [it for each (it in content.document.links) if (it.href)]; 
     53      return [];  
    5954    let re = makeRegExp(word); 
    60     return [it for each (it in content.document.links) if (lmatch(re, it))]; 
     55    return [it for each (it in content.document.links) if (lmatch(re, it))];  
    6156  } 
    6257 
     
    6560      f: NEW_TAB, 
    6661      t: NEW_TAB, 
     62      n: NEW_TAB, 
    6763      b: NEW_BACKGROUND_TAB, 
    6864      c: CURRENT_TAB, 
     
    7268  } 
    7369 
    74   let foihandle; 
     70  const WHERE_COMPLETIONS = ['f', 't', 'n', 'b', 'c', 'w']; 
    7571 
    76   liberator.commands.addUserCommand( 
    77     ['fo[pen]', 'filteropen'], 
    78     'Filtered open', 
    79     function (opts, bang) { 
    80       let where = charToWhere(opts['-where'], bang ? NEW_TAB : NEW_BACKGROUND_TAB); 
    81       let [i, links] = [1, filteredLinks(opts.arguments.join(''))]; 
    82       if (!links.length) 
    83         return; 
    84       open(links[0].href, where); 
    85       if (links.length <= 1) 
    86         return; 
    87       let interval = (opts['-interval'] || liberator.globalVariables.fopen_default_interval || 1) * 1000; 
    88       foihandle = setInterval(function () { 
    89         try { 
    90           open(links[i].href, where); 
    91           if ((++i) >= links.length) 
     72 
     73  let (foihandle) { 
     74 
     75    liberator.commands.addUserCommand( 
     76      ['fo[pen]', 'filteropen'], 
     77      'Filtered open', 
     78      function (opts, bang) { 
     79        let where = charToWhere(opts['-where'], bang ? NEW_TAB : NEW_BACKGROUND_TAB); 
     80        let [i, links] = [1, filteredLinks(opts.arguments.join(''))]; 
     81        if (!links.length) 
     82          return; 
     83        open(links[0].href, where); 
     84        if (links.length <= 1) 
     85          return; 
     86        let interval = (opts['-interval'] || liberator.globalVariables.fopen_default_interval || 1) * 1000; 
     87        foihandle = setInterval(function () { 
     88          try { 
     89            open(links[i].href, where); 
     90            if ((++i) >= links.length) 
     91              clearInterval(foihandle); 
     92          } catch (e) { 
    9293            clearInterval(foihandle); 
    93         } catch (e) { 
    94           clearInterval(foihandle); 
     94          } 
     95        }, interval); 
     96      }, 
     97      { 
     98        bang: true, 
     99        options: [ 
     100          [['-interval', '-i'], liberator.commands.OPTION_INT], 
     101          [['-where', '-w'], liberator.commands.OPTION_STRING], 
     102        ], 
     103        completer: function (word) { 
     104          let links = filteredLinks(word); 
     105          return [0, [[it.href, it.textContent] for each (it in links)]]; 
     106        }, 
     107      } 
     108    ); 
     109 
     110    liberator.commands.addUserCommand( 
     111      ['stopfilteropen', 'stopfo[pen]'], 
     112      'Stop filtered open', 
     113      function () { 
     114        clearInterval(foihandle); 
     115      } 
     116    ); 
     117 
     118  } 
     119 
     120  let (  
     121    lolinks = [], 
     122    looptions = [ [['-where', '-w'], liberator.commands.OPTION_STRING, null, WHERE_COMPLETIONS] ] 
     123  ) { 
     124 
     125    liberator.commands.addUserCommand( 
     126      ['lo[pen]', 'linkopen'], 
     127      'Filtered open', 
     128      function (opts, bang) { 
     129        let where = charToWhere(opts['-where'], bang ? NEW_TAB : CURRENT_TAB); 
     130        let arg = opts.arguments[0]; 
     131        let m = arg.match(/^(\d+),/); 
     132        if (m)  
     133          liberator.buffer.followLink(lolinks[parseInt(m[1], 10)], where); 
     134      }, 
     135      { 
     136        options: looptions, 
     137        bang: true, 
     138        completer: function (word) { 
     139          if (!word || word.match(/\d+,|\s/)) 
     140            return []; 
     141          lolinks = filteredLinks(word); 
     142          return [0, [[i + ',' + lolinks[i].href, lolinks[i].textContent] for (i in lolinks || [])]]; 
    95143        } 
    96       }, interval); 
    97     }, 
    98     { 
    99       options: [ 
    100         [['-interval', '-i'], liberator.commands.OPTIONS_INT], 
    101         [['-where', '-w'], liberator.commands.OPTIONS_STRING], 
    102       ], 
    103       completer: function (word) { 
    104         let links = filteredLinks(word); 
    105         return [0, [[it.href, it.textContent] for each (it in links)]]; 
    106144      } 
    107     } 
    108   ); 
     145    ); 
    109146 
    110   liberator.commands.addUserCommand( 
    111     ['stopfilteropen', 'stopfo[pen]'], 
    112     'Stop filtered open', 
    113     function () { 
    114       clearInterval(foihandle); 
    115     } 
    116   ); 
    117  
    118   let lolinks = []; 
    119   let looptions = [ [['-where', '-w'], liberator.commands.OPTIONS_STRING], ]; 
    120  
    121   liberator.commands.addUserCommand( 
    122     ['lo[pen]', 'linkopen'], 
    123     'Filtered open', 
    124     function (opts, bang) { 
    125       let where = charToWhere(opts['-where'], bang ? NEW_TAB : CURRENT_TAB); 
    126       let uri = opts.arguments[0]; 
    127       for each (let link in lolinks) { 
    128         if (~link.href.indexOf(uri)) 
    129           return liberator.buffer.followLink(link, where); 
    130       } 
    131       if (lolinks[0]) { 
    132         liberator.buffer.followLink(lolinks[0], where); 
    133       } else { 
    134         liberator.echoerr('lol'); 
    135       } 
    136     }, 
    137     { 
    138       options: looptions, 
    139       completer: function (word) { 
    140         let opts = liberator.parseArgs(word, looptions, "1", true); 
    141         log(opts); 
    142         lolinks = filteredLinks(word);//word.match(/\bhttp\S+/)); 
    143         return [0, [[it.href, it.textContent] for each (it in lolinks)]]; 
    144       } 
    145     } 
    146   ); 
     147  } 
    147148 
    148149}catch(e){log(e);}})();