root/lang/javascript/vimperator-plugins/trunk/morse.js

Revision 36714, 7.7 kB (checked in by anekos, 7 months ago)

オプションのまつがい修正

  • Property svn:executable set to *
Line 
1/* NEW BSD LICENSE {{{
2Copyright (c) 2010, 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>Morse</name>
39  <name lang="ja">Morse</name>
40  <description>Morse code (Windows only)</description>
41  <description lang="ja">モールス信号 (Windows専用)</description>
42  <version>1.2.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  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/morse.js</updateURL>
47  <minVersion>2.3</minVersion>
48  <maxVersion>2.3</maxVersion>
49  <detail><![CDATA[
50    :morse text:
51      output text by morse code.
52  ]]></detail>
53  <detail lang="ja"><![CDATA[
54    :morse text:
55      output text by morse code.
56  ]]></detail>
57</VimperatorPlugin>;
58// }}}
59// INFO {{{
60let INFO =
61<plugin name="Morse" version="1.2.1"
62        href="http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/morse.js"
63        summary="Morse code"
64        xmlns="http://vimperator.org/namespaces/liberator">
65  <author email="anekos@snca.net">anekos</author>
66  <license>New BSD License</license>
67  <project name="Vimperator" minVersion="2.3"/>
68  <p>
69  </p>
70  <item>
71    <tags>:morse</tags>
72    <spec>:morse <a>text</a></spec>
73    <description>
74      <p>
75        output <a>text</a> by morse code.
76      </p>
77    </description>
78  </item>
79</plugin>;
80// }}}
81
82
83(function () {
84
85  let codeTable = {
86    a: '.-',
87    b: '-...',
88    c: '-.-.',
89    d: '-..',
90    e: '.',
91    f: '..-.',
92    g: '--.',
93    h: '....',
94    i: '..',
95    j: '.---',
96    k: '-.-',
97    l: '.-..',
98    m: '--',
99    n: '-.',
100    o: '---',
101    p: '.--.',
102    q: '--.-',
103    r: '.-.',
104    s: '...',
105    t: '-',
106    u: '..-',
107    v: '...-',
108    w: '.--',
109    x: '-..-',
110    y: '-.--',
111    z: '--..',
112    1: '.----',
113    2: '..---',
114    3: '...--',
115    4: '....-',
116    5: '.....',
117    6: '-....',
118    7: '--...',
119    8: '---..',
120    9: '----.',
121    0: '-----',
122    '.': '.-.-.-',
123    '': '--..--',
124    '?': '..--..',
125    ' ': '-...-',
126    '-': '-....-',
127    '/': '-..-.',
128    '@': '.--.-.',
129    '\u30A4': '.-',
130    '\u30ED': '.-.-',
131    '\u30CF': '-...',
132    '\u30CB': '-.-.',
133    '\u30DB': '-..',
134    '\u30D8': '.',
135    '\u30C8': '..-..',
136    '\u30C1': '..-.',
137    '\u30EA': '--.',
138    '\u30CC': '....',
139    '\u30EB': '-.--.',
140    '\u30F2': '.---',
141    '\u30EF': '-.-',
142    '\u30AB': '.-..',
143    '\u30E8': '--',
144    '\u30BF': '-.',
145    '\u30EC': '---',
146    '\u30BD': '---.',
147    '\u30C4': '.--.',
148    '\u30CD': '--.-',
149    '\u30CA': '.-.',
150    '\u30E9': '...',
151    '\u30E0': '-',
152    '\u30A6': '..-',
153    '\u30F0': '.-..-',
154    '\u30A4': '.-',
155    '\u30ED': '.-.-',
156    '\u30CF': '-...',
157    '\u30CB': '-.-.',
158    '\u30DB': '-..',
159    '\u30D8': '.',
160    '\u30C8': '..-..',
161    '\u30C1': '..-.',
162    '\u30EA': '--.',
163    '\u30CC': '....',
164    '\u30EB': '-.--.',
165    '\u30F2': '.---',
166    '\u30EF': '-.-',
167    '\u30AB': '.-..',
168    '\u30E8': '--',
169    '\u30BF': '-.',
170    '\u30EC': '---',
171    '\u30BD': '---.',
172    '\u30C4': '.--.',
173    '\u30CD': '--.-',
174    '\u30CA': '.-.',
175    '\u30E9': '...',
176    '\u30E0': '-',
177    '\u30A6': '..-',
178    '\u30F0': '.-..-',
179    '\u30CE': '..--',
180    '\u30AA': '.-...',
181    '\u30AF': '...-',
182    '\u30E4': '.--',
183    '\u30DE': '-..-',
184    '\u30B1': '-.--',
185    '\u30D5': '--..',
186    '\u30B3': '----',
187    '\u30A8': '-.---',
188    '\u30C6': '.-.--',
189    '\u30A2': '--.--',
190    '\u30B5': '-.-.-',
191    '\u30AD': '-.-..',
192    '\u30E6': '-..--',
193    '\u30E1': '-...-',
194    '\u30DF': '..-.-',
195    '\u30B7': '--.-.',
196    '\u30F1': '.--..',
197    '\u30D2': '--..-',
198    '\u30E2': '-..-.',
199    '\u30BB': '.---.',
200    '\u30B9': '---.-',
201    '\u30F3': '.-.-.',
202  };
203
204  let [short, long, interval] = [100, 400, 200];
205
206  let keybd_event =
207    (function () {
208      try {
209        Components.utils.import("resource://gre/modules/ctypes.jsm");
210        let user32 = ctypes.open('user32.dll');
211        return user32.declare(
212            'keybd_event',
213            ctypes.stdcall_abi,
214            ctypes.void_t,
215            ctypes.uint8_t, ctypes.uint8_t, ctypes.int32_t, ctypes.int32_t
216          );
217      } catch (e) {
218        return function () "DO NOTHING";
219      }
220    })();
221
222  function Morse (short, long, interval) {
223    function bing (ch, next) {
224      function _bing () {
225        keybd_event(0x91, 0x45, 1, 0);
226        keybd_event(0x91, 0x45, 3, 0);
227      }
228
229      return function () {
230        if (ch == ' ')
231          return setTimeout(next, interval);
232        liberator.log(ch == '.' ? short : long);
233        _bing();
234        setTimeout(
235          function () (_bing(), setTimeout(next, interval)),
236          ch == '.' ? short : long
237        );
238      };
239    }
240
241    function bings (codes) {
242      let [code,]  = codes;
243      if (!code)
244        return function () "DO NOTHING";
245      return bing(code, bings(codes.slice(1)));
246    }
247
248    return function (codes) bings(codes)();
249  }
250
251  function toCode (text)
252    Array.slice(text).map(function (c) codeTable[c.toLowerCase()] || '').join(' ');
253
254  commands.addUserCommand(
255    ['morse'],
256    'Mooooooooooooorse',
257    function (args) {
258      let code = args['-raw'] ? args.literalArg : toCode(args.literalArg);
259
260      args['-clipboard'] && util.copyToClipboard(code);
261      args['-echo'] && liberator.echo(code);
262
263      [short, long, interval] =
264        [
265          args['-short'] || short,
266          args['-long'] || long,
267          args['-interval'] || interval
268        ];
269
270      Morse(short, long, interval)(code);
271    },
272    {
273      literal: 0,
274      options: [
275        [['-clipboard', '-c'], commands.OPTION_NOARG],
276        [['-echo', '-e'], commands.OPTION_NOARG],
277        [['-short', '-s'], commands.OPTION_INT],
278        [['-long', '-l'], commands.OPTION_INT],
279        [['-interval', '-i'], commands.OPTION_INT],
280        [['-raw', '-r'], commands.OPTION_NOARG]
281      ],
282    },
283    true
284  );
285
286
287})();
288
289// vim:sw=2 ts=2 et si fdm=marker:
290
Note: See TracBrowser for help on using the browser.