aboutsummaryrefslogtreecommitdiffstats
path: root/jsh.js
blob: 1c1d78e5117f3e7b5ef8b3882451d1fe0c513d0a (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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
//  jsh.js
//
// LICENSE: {{{
//   Copyright (c) 2009 snaka<snaka.gml@gmail.com>
//
//     Distributable under the terms of an MIT-style license.
//     http://www.opensource.jp/licenses/mit-license.html
// }}}
// PLUGIN_INFO {{{
var PLUGIN_INFO = <VimperatorPlugin>
  <name>jsh</name>
  <description>Simple javascript shell</description>
  <description lang="ja">簡易Javascriptシェル</description>
  <minVersion>2.0pre</minVersion>
  <maxVersion>2.0</maxVersion>
  <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/jsh.js</updateURL>
  <author mail="snaka.gml@gmail.com" homepage="http://vimperator.g.hatena.ne.jp/snaka72/">snaka</author>
  <license>MIT style license</license>
  <version>0.1</version>
  <detail><![CDATA[
    == Subject ==

    == Commands ==
      To start shell.
      >||
      :jsh
      ||<

    == Global variables ==

    == Options ==

    == ToDo ==

  ]]></detail>
  <detail lang="ja"><![CDATA[
    == 概要 ==

    == コマンド ==
      シェルの開始
      >||
      :jsh
      ||<

    == Global variables ==

    == Options ==

    == ToDo ==

  ]]></detail>
</VimperatorPlugin>;
// }}}

liberator.plugins.jsh = (function() {
  // PUBLIC {{{
  const self = {
    start: function() {
      liberator.echo("*** Javascript SHell - Commands : quit() / cls() ***");
      waitForInput();
    }
  };
  // }}}
  // COMMAND {{{
  commands.addUserCommand(
    ["jsh"],
    "Simple javascript shell",
    function() self.start(),
    null,
    true  // for DEBUG
  );
  // }}}
  // PRIVATE {{{
  let  shellContext = libly.$U.extend({
      quit : cmdQuit,
      exit : cmdQuit,  // alias as 'quit()'
      cls  : cmdCls,
      echo : liberator.echo,
      $LXs : plugins.libly.$U.getNodesFromXPath,
      $LX  : plugins.libly.$U.getFirstNodeFromXPath
  }, liberator);

  const STATUS = {
    NORMAL: '__JAVASCRIPT_SHELL_NORMAL__',
    QUIT  : '__JAVASCRIPT_SHELL_QUIT__'
  };

  let lastStatus = STATUS.NORMAL;

  function waitForInput() {
    commandline.open("js:", "", modes.JSH);
  }

  function cmdCls() {
    // TODO Is this correct?
    commandline.close();
    waitForInput();
  }

  function cmdQuit() {
    commandline.close();
    throw STATUS.QUIT;
  }

  // TODO:Should move to _libly.js ?
  function pp(value) {
    let result;

    if (!value)
      return '';

    if (typeof value === 'object') {
      result = util.objectToString(value, true);
    } else if (typeof value === 'function') {
      result = <pre>{value.toString()}</pre>;
    } else if (typeof value === 'string' && /\n/.test(value)) {
      result = <span highlight="CmdOutput">{value}</span>;
    } else {
      result = String(value);
    }

    liberator.echo(result, commandline.FORCE_MULTILINE);
  }

  // }}}
  // MODE {{{
  modules.modes.addMode("JSH", true);
  liberator.registerCallback("cancel", modes.JSH, function(str) {
    liberator.echo("CANCELED.");
  });

  liberator.registerCallback("submit", modes.JSH, function(str) {
    try {
      var result = liberator.eval(str, shellContext);
      pp(result);
    } catch(e) {
      if (e == STATUS.QUIT)
        return;
      liberator.echoerr(e);
    }
    (function waitForOutput() {
      setTimeout( function() modes.main == modes.NORMAL
                    ? waitForInput()
                    : waitForOutput(),
                  500 );
    })();
  });

  liberator.registerCallback("complete", modes.JSH, function(context) {
    context.fork("jsh", 0, liberator.modules.completion, "javascript");
  });
  // }}}
  return self;
})();
// vim: sw=2 ts=2 et si fdm=marker: