root/lang/javascript/vimperator-plugins/branches/2.0/umihara.js

Revision 29434, 8.4 kB (checked in by anekos, 19 months ago)

引数省略時のバグ修正

Line 
1/* {{{
2Copyright (c) 2008, anekos.
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without modification,
6are permitted provided that the following conditions are met:
7
8    1. Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10    2. Redistributions in binary form must reproduce the above copyright notice,
11       this list of conditions and the following disclaimer in the documentation
12       and/or other materials provided with the distribution.
13    3. The names of the authors may not be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
20INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
25THE POSSIBILITY OF SUCH DAMAGE.
26
27
28###################################################################################
29# http://sourceforge.jp/projects/opensource/wiki/licenses%2Fnew_BSD_license       #
30# に参考になる日本語訳がありますが、有効なのは上記英文となります。                #
31###################################################################################
32
33}}} */
34
35// PLUGIN_INFO {{{
36let PLUGIN_INFO =
37<VimperatorPlugin>
38  <name>Exchange Converter</name>
39  <name lang="ja">外国為替換算</name>
40  <description>for exchangeconvertion</description>
41  <description lang="ja">為替換算をします</description>
42  <version>1.1.1</version>
43  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
44  <license>new BSD License (Please read the source code comments of this plugin)</license>
45  <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
46  <minVersion>2.0pre</minVersion>
47  <maxVersion>2.0pre</maxVersion>
48  <detail><![CDATA[
49    == Usage ==
50      :kawase[!] <VALUE> [<SOURCE> [<TARGET>]]:
51      :kawase[!] <VALUE> <SOURCE1> <SOURCE2> ... <TARGET>:
52        Convert <SOURCE> to <TARGET>.
53        When used with "!", copy result to clipboard.
54    === Example ===
55       :kawase 30000 JPY THB
56    == Global Variables ==
57      - g:umihara_default_source
58      - g:umihara_default_target
59      === Example ===
60      >||
61        let g:umihara_default_source="USD"
62        let g:umihara_default_target="JPY"
63      ||<
64  ]]></detail>
65  <detail lang="ja"><![CDATA[
66    == Usage ==
67      :kawase[!] <金額> [<ソース> [<ターゲット>]]:
68      :kawase[!] <金額> <ソース1> <ソース2> ... <ターゲット>:
69        ソースをターゲットに換算します。
70        "!" 指定でクリップボードにコピーされます。
71      === Example ===
72        >||
73          :kawase 30000 JPY THB
74        ||<
75    == Global Variables ==
76      引数省略時のデフォルト値を設定します
77      - g:umihara_default_source
78      - g:umihara_default_target
79      === Example ===
80        >||
81          let g:umihara_default_source="USD"
82          let g:umihara_default_target="JPY"
83        ||<
84  ]]></detail>
85</VimperatorPlugin>;
86// }}}
87
88(function () {
89
90  const defaultSource = liberator.globalVariables.umihara_default_source || 'USD';
91  const defaultTarget = liberator.globalVariables.umihara_default_target || 'JPY';
92
93  const re = /<td nowrap>(\d+:\d+)<\/td><td>([\d,]+\.[\d,]+)<\/td><td><b>([\d,]+\.[\d,]+)<\/b><\/td><\/tr><\/table><\/div>/;
94
95  const ContryCodes = [
96    ['USD', '\u30a2\u30e1\u30ea\u30ab\u30c9\u30eb'],
97    ['GBP', '\u30a4\u30ae\u30ea\u30b9 \u30dd\u30f3\u30c9'],
98    ['INR', '\u30a4\u30f3\u30c9 \u30eb\u30d4\u30fc'],
99    ['IDR', '\u30a4\u30f3\u30c9\u30cd\u30b7\u30a2 \u30eb\u30d4\u30a2'],
100    ['ECS', '\u30a8\u30af\u30a2\u30c9\u30eb \u30b9\u30af\u30ec'],
101    ['EGP', '\u30a8\u30b8\u30d7\u30c8 \u30dd\u30f3\u30c9'],
102    ['AUD', '\u30aa\u30fc\u30b9\u30c8\u30e9\u30ea\u30a2 \u30c9\u30eb'],
103    ['CAD', '\u30ab\u30ca\u30c0 \u30c9\u30eb'],
104    ['KRW', '\u97d3\u56fd \u30a6\u30a9\u30f3'],
105    ['KWD', '\u30af\u30a6\u30a7\u30fc\u30c8 \u30c7\u30a3\u30ca\u30fc\u30eb'],
106    ['COP', '\u30b3\u30ed\u30f3\u30d3\u30a2 \u30da\u30bd'],
107    ['SAR', '\u30b5\u30a6\u30b8 \u30ea\u30a2\u30eb'],
108    ['SGD', '\u30b7\u30f3\u30ac\u30dd\u30fc\u30eb \u30c9\u30eb'],
109    ['CHF', '\u30b9\u30a4\u30b9 \u30d5\u30e9\u30f3'],
110    ['SEK', '\u30b9\u30a6\u30a7\u30fc\u30c7\u30f3 \u30af\u30ed\u30fc\u30ca'],
111    ['THB', '\u30bf\u30a4 \u30d0\u30fc\u30c4'],
112    ['TWD', '\u53f0\u6e7e \u30c9\u30eb'],
113    ['CNY', '\u4e2d\u56fd \u5143'],
114    ['CLP', '\u30c1\u30ea \u30da\u30bd'],
115    ['DKK', '\u30c7\u30f3\u30de\u30fc\u30af \u30af\u30ed\u30fc\u30cd'],
116    ['TRY', '\u30c8\u30eb\u30b3 \u30ea\u30e9'],
117    ['JPY', '\u65e5\u672c \u5186'],
118    ['NZD', '\u30cb\u30e5\u30fc\u30b8\u30fc\u30e9\u30f3\u30c9 \u30c9\u30eb'],
119    ['NOK', '\u30ce\u30eb\u30a6\u30a7\u30fc \u30af\u30ed\u30fc\u30cd'],
120    ['PYG', '\u30d1\u30e9\u30b0\u30a2\u30a4 \u30b0\u30a1\u30e9\u30cb'],
121    ['PHP', '\u30d5\u30a3\u30ea\u30d4\u30f3 \u30da\u30bd'],
122    ['BRL', '\u30d6\u30e9\u30b8\u30eb \u30ea\u30a2\u30eb'],
123    ['VEF', '\u30d9\u30cd\u30ba\u30a8\u30e9 \u30dc\u30ea\u30d0\u30eb\u30fb\u30d5\u30a8\u30eb\u30c6'],
124    ['PEN', '\u30da\u30eb\u30fc \u30bd\u30eb'],
125    ['HKD', '\u9999\u6e2f \u30c9\u30eb'],
126    ['MYR', '\u30de\u30ec\u30fc\u30b7\u30a2 \u30ea\u30f3\u30ae'],
127    ['ZAR', '\u5357\u30a2\u30d5\u30ea\u30ab \u30e9\u30f3\u30c9'],
128    ['MXN', '\u30e1\u30ad\u30b7\u30b3 \u30da\u30bd'],
129    ['AED', 'UAE \u30c0\u30fc\u30cf\u30e0'],
130    ['EUR', '\u6b27\u5dde \u30e6\u30fc\u30ed'],
131    ['JOD', '\u30e8\u30eb\u30c0\u30f3 \u30c7\u30a3\u30ca\u30fc\u30eb'],
132    ['RON', '\u30eb\u30fc\u30de\u30cb\u30a2 \u30ec\u30a6'],
133    ['LBP', '\u30ec\u30d0\u30ce\u30f3 \u30dd\u30f3\u30c9'],
134    ['RUB', '\u30ed\u30b7\u30a2\u30f3 \u30eb\u30fc\u30d6\u30eb'],
135  ];
136
137  function echo (msg) {
138    liberator.echo(<pre>{msg}</pre>);
139  }
140
141  let resultBuffer = '';
142
143  function kawase (value, clipboard, from, to) {
144    [from, to] = [from || defaultSource, to || defaultTarget].map(function (it) it.toUpperCase());
145    if (from == '-')
146      from = defaultSource;
147    if (to == '-')
148      to = defaultTarget;
149    let url = 'http://quote.yahoo.co.jp/m5?a=' + value + '&s=' + from + '&t=' + to;
150    var req = new XMLHttpRequest();
151    req.open('GET', url);
152    req.onreadystatechange = function (aEvt) {
153      if (req.readyState == 4 && req.status == 200) {
154        let m = req.responseText.match(re);
155        if (m) {
156          let text = from + ' -> ' + to +
157                     '\n ' + from + ': ' + value +
158                     '\n ' + to + ': ' + m[3] +
159                     '\n rate: ' + m[2] +
160                     '\n time: ' + m[1];
161          echo(text);
162          if (clipboard) {
163            resultBuffer += text + '\n';
164            util.copyToClipboard(resultBuffer);
165          }
166        } else {
167          liberator.echoerr('parse error');
168        }
169      }
170    };
171    req.send(null);
172  }
173
174  let extra = {
175    argCount: '+',
176    bang: true,
177    completer: function (context, args) {
178      if (args.length == 1) {
179        // TODO - history
180      } else {
181        let  def = args.length < 3 ? defaultSource
182                                   : defaultTarget;
183        context.title = ['Country Code', 'Country Name'];
184        context.completions = [['-', def]].concat(ContryCodes);
185      }
186    }
187  };
188
189  commands.addUserCommand(
190    ['kawase'],
191    'Umihara Kawase Meow',
192    function (args) {
193      let as = args;
194      resultBuffer = '';
195      liberator.echo('<<Results>>\n')
196      if (as.length == 0)
197        as.push('1');
198      while (as.length < 3)
199        as.push('-');
200      for (let i = 1, l = as.length - 1; i < l; i++) {
201        let [value, from, to] = [as[0], as[i], l == i ? '-' : as[l]];
202        liberator.log({
203          value: value,
204          from: from,
205          to: to
206        })
207        value = eval(value);
208        kawase(value, args.bang, from, to);
209      }
210    },
211    extra,
212    true
213  );
214
215})();
216
217
218// vim:sw=2 ts=2 et si fdm=marker:
Note: See TracBrowser for help on using the browser.