aboutsummaryrefslogtreecommitdiffstats
path: root/unicode.js
blob: 3c1d2f2552fa06323cb90d9c72959a9e652ebd47 (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
// ==VimperatorPlugin==
// @name           unicode escape
// @description    Copy the escaped-unicode text to the clipboard.
// @description-ja Unicode エスケープされたテキストをクリップボードにコピーする
// @license        Creative Commons 2.1 (Attribution + Share Alike)
// @version        1.0
// @author         anekos (anekos@snca.net)
// @maxVersion     2.0pre
// @minVersion     2.0pre
// ==/VimperatorPlugin==
//
// Usage:
//    :uc <MULTIBYTE_TEXT>
//    :uc! <ESCAPED_UNICODE_TEXT>
//
// Links:
//

(function () {

  function escape (s)
    s.toSource().replace(/^[^"]+"|"[^"]+$/g,'');

  function unescape (s)
    s.replace(/\\u([a-f\d]{4})/gi,function(_,c)String.fromCharCode(parseInt(c,16)));

  function copyAndEcho (s)
    (liberator.echo(s)+util.copyToClipboard(s));


  commands.addUserCommand(
    ['unicode', 'uc'],
    'unicode (un)escape',
    function (arg, bang)
      copyAndEcho((bang ? unescape : escape)(arg.string)),
    {argCount: '*', bang: true},
    true
  );

})();