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

Revision 37259, 5.0 kB (checked in by anekos, 5 months ago)

改行をシュシュッとする

Line 
1/* {{{
2Copyright (c) 2008-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>{NAME}</name>
39  <description>Write a memo to the specified file.</description>
40  <description lang="ja">指定のファイルにメモを書く</description>
41  <version>1.0.2</version>
42  <author mail="anekos@snca.net" homepage="http://d.hatena.ne.jp/nokturnalmortum/">anekos</author>
43  <license>new BSD License (Please read the source code comments of this plugin)</license>
44  <license lang="ja">修正BSDライセンス (ソースコードのコメントを参照してください)</license>
45  <updateURL>http://coderepos.org/share/export/27234/lang/javascript/vimperator-plugins/trunk/memo.js</updateURL>
46  <minVersion>2.0pre</minVersion>
47  <maxVersion>2.3</maxVersion>
48  <detail><![CDATA[
49    == Usage ==
50      :memo:
51        show the memo that was written.
52      :memo fooooobar!:
53        write "fooooobar!" to the specified memo file.
54  ]]></detail>
55  <detail lang="ja"><![CDATA[
56    == Usage ==
57      :memo
58        書かれたメモを表示する
59      :memo fooooobar!
60        "fooooobar!" と、メモに書く
61  ]]></detail>
62</VimperatorPlugin>;
63// }}}
64
65// References:
66//    http://developer.mozilla.org/index.php?title=Ja/Code_snippets/File_I%2F%2FO
67
68(function () {
69  let localfilepath = liberator.globalVariables.memo_filepath || io.expandPath('~/.vimpmemo');
70  let charset = 'UTF-8';
71
72  //ネタ的
73  let lz = function(s,n)(s+'').replace(new RegExp('^.{0,'+(n-1)+'}$'),function(s)lz('0'+s,n));
74
75  function dateTime () {
76    with (new Date())
77      return lz(getFullYear(), 4) + '/' +
78             lz(getMonth() + 1, 2) + '/' +
79             lz(getDate(), 2) + ' ' +
80             lz(getHours(), 2) + ':' +
81             lz(getMinutes(), 2);
82  }
83
84  function filepath () {
85    let result = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
86    result.initWithPath(localfilepath);
87    return result;
88  }
89
90  function puts (line) {
91    line = dateTime() + "\t" + line + "\n";
92    let out = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(Ci.nsIFileOutputStream);
93    let conv = Cc['@mozilla.org/intl/converter-output-stream;1'].
94                            createInstance(Ci.nsIConverterOutputStream);
95    out.init(filepath(), 0x02 | 0x10 | 0x08, 0664, 0);
96    conv.init(out, charset, line.length,
97              Components.interfaces.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
98    conv.writeString(line);
99    conv.close();
100    out.close();
101  }
102
103  function gets () {
104    let file = Cc['@mozilla.org/network/file-input-stream;1'].createInstance(Ci.nsIFileInputStream);
105    file.init(filepath(), 1, 0, false);
106    let conv = Cc['@mozilla.org/intl/converter-input-stream;1'].createInstance(Ci.nsIConverterInputStream);
107    conv.init(file, charset, file.available(), conv.DEFAULT_REPLACEMENT_CHARACTER);
108    let result = {};
109    conv.readString(file.available(), result);
110    conv.close();
111    file.close();
112    return result.value;
113  }
114
115  commands.addUserCommand(
116    ['memo'],
117    'Write memo',
118    function (arg) {
119      if (arg.string) {
120        puts(arg.string);
121      } else {
122        let out = <></>;
123        gets().split(/\n/).reverse().forEach(function (l) {
124          out += <li>{l}</li>
125        });
126        liberator.echo(out);
127      }
128    },
129    {},
130    true
131  );
132
133})();
Note: See TracBrowser for help on using the browser.