aboutsummaryrefslogtreecommitdiffstats
path: root/_smooziee.js
blob: 612b589cd50a1dc7a8c98bb54a410975306a847c (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
//
// _smooziee.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>smooziee</name>
<description>At j,k key scrolling to be smooth.</description>
<description lang="ja">j,kキーでのスクロールをスムースに</description>
<minVersion>2.0</minVersion>
<maxVersion>2.0pre</maxVersion>
<updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/_smooziee.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.9</version>
<detail><![CDATA[
== Subject ==
j,k key scrolling to be smoothly.

== Global variables ==
You can configure following variable as you like.
:smooziee_scroll_amount: Scrolling amount. Default value is 400px.
:smooziee_interval: Scrolling interval. Default value is 20ms. 

== ToDo ==

]]></detail>

<detail lang="ja"><![CDATA[
== 概要 ==
普段のj,kキーのスクロールをLDRizeライクにスムースにします

== グローバル変数 ==
以下の変数を.vimperatorrcなどで設定することで動作を調整することができます
:smooziee_scroll_amount:1回にスクロールする幅ですデフォルトは400pxです
:smooziee_interval:スクロールのインターバル単位ミリ秒デフォルトは30msです

== ToDo ==
- 読み込みの順番によっては他のプラグインと競合する可能性があるのをなんとかしたい

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

(function(){
  // configurations
  var scrollAmount  = window.eval(liberator.globalVariables.smooziee_scroll_amount || 400);
  var interval      = window.eval(liberator.globalVariables.smooziee_interval || 20);

  // 
  // Private
  //
  var next = null;
  var destY = null;
  var win = function() window.content.window.wrappedJSObject;

  // direction : positive (down) / negative (up)
  function smoothScroll(amount, direction) {
    var moment = Math.floor(amount / 2);
    win().scrollBy(0, moment * direction);

    if (moment < 1) {
      setTimeout(makeScrollTo(0, destY), interval);
      destY = null;
      return;
    }
    next = setTimeout(function() smoothScroll(moment, direction), interval);
  }

  function resetDestination() {
    clearTimeout(next);
  }

  function makeScrollTo(x, y) {
    return function() {
      win().scrollTo(x, y);
    };
  }

  //
  // Mappings 
  //
  mappings.addUserMap(
    [modes.NORMAL], 
    ["j"], 
    "Smooth scroll down", 
    function(){ 
      destY = win().scrollY + scrollAmount;
      resetDestination();
      smoothScroll(scrollAmount,  1);
    }
  ); 

  mappings.addUserMap(
    [modes.NORMAL], 
    ["k"], 
    "Smooth scroll up", 
    function(){ 
      destY = win().scrollY - scrollAmount;
      resetDestination();
      smoothScroll(scrollAmount, -1);
    }
  ); 
})();
// vim: sw=2 ts=2 et si fdm=marker: