aboutsummaryrefslogtreecommitdiffstats
path: root/tohex.js
blob: fb7b31c7b207def9a8c8ce11d5fc4dd517ee4b7f (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
// {{{ INFO
var INFO =xml`
<plugin name="tohex.js" version="0.1"
        summary="Get hex value from string"
        href="http://github.com/vimpr/vimperator-plugins/blob/master/tohex.js"
        xmlns="http://vimperator.org/namespaces/liberator">
  <author email="mitsugu.oyama@gmail.com">Mitsugu Oyama</author>
  <license href="http://opensource.org/licenses/mit-license.php">MIT</license>
  <project name="Vimperator" minVersion="2.3"/>
  <p>Convert to hex code from string by this plugin.</p>
  <item>
    <tags>'tohex'</tags>
    <spec>:tohex</spec>
    <description>
      <p>Convert to hex code from string by this plugin.</p>
      <p>If you use <link topic="http://github.com/vimpr/vimperator-plugins/blob/master/i_love_echo.js">i_love_echo.js</link>, you should not use tohex plugin.</p>
    </description>
  </item>
</plugin>`;
// }}}

commands.addUserCommand(
  ['tohex'],
  'convert to hex code from charactor',
  function(args){
    if(1!=args.length){
      liberator.echoerr('argument error');
      return false;
    }
    let str='';
    let max=args[0].length;
    for(let i=0;i<max;i++)
      str+=('\\u'+args[0].charCodeAt(i).toString(16));
    liberator.echo(str);
    util.copyToClipboard(str,true);
  },
  {
    literal: false
  },
  true
);
) { let keys = [ ['<Right>', 'next page', function() nextPage()], ['<Left>', 'prev page', function() prevPage()], ['^', 'first page', function() loadPage(0)], ['$', 'last page', function() loadPage(pages.length-1)], ['.', 'last page', function(count) loadPage(count?count-1:0), {count: true}], ['q', 'stop presentation', function() stop()], ]; let win; let doc; let pages = []; let curpage = 0; let pre; let header; let fontSize = 18.0; function addKeys() { keys.forEach(function([key, desc, action, extra]) mappings.addUserMap([modes.NORMAL], [key], desc, action, extra)); } function fitPage() { if(pre.innerHTML=='') return; pre.style.display = 'inline'; let parentWidth = pre.parentNode.offsetWidth; let parentHeight = pre.parentNode.offsetHeight; let width = pre.offsetWidth; let height = pre.offsetHeight; let preRatio = width/height; let winRatio = parentWidth/parentHeight; if(preRatio>winRatio) { fontSize *= 0.9*(parentWidth-10)/width; } else { fontSize *= 0.9*(parentHeight-10)/height; } pre.style.fontSize = fontSize+'px'; pre.style.display = 'block'; } function loadPage(page) { let text = pages[page]; pre.innerHTML = text; if(header) { header.innerHTML = (page+1)+'/'+pages.length; } fitPage(); } function nextPage() { curpage = curpage>=pages.length-1 ? 0 : curpage+1; loadPage(curpage); } function prevPage() { curpage = curpage<=0 ? pages.length-1 : curpage-1; loadPage(curpage); } function parsePages(text) { return text.split('----') .map(function(txt) txt.replace(/^(?:\r\n|[\r\n])|(?:\r\n|[\r\n])$/g, '')); } function save_setting(setting) { setting.fullscreen = options.fullscreen; setting.guioptions = options.guioptions; // TODO: save key mapping //setting.mappings = keys.map(function([key,]) { // let mapping = mappings.get(modes.NORMAL, key); // return [mapping.modes, key, mapping.description, mapping.action, mapping.extra]; //}); } function load_setting(setting) { options.fullscreen = setting.fullscreen; options.guioptions = setting.guioptions; // TODO: load key mapping //setting.mappings.forEach(function([modes, key, desc, action, extra]) { // mappings.addUserMap(modes, [key], desc, action, extra); //}); } let original_setting = {}; function start() { save_setting(original_setting); options.fullscreen = true; options.guioptions = ''; win = window.content; doc = win.document; let text = util.evaluateXPath('//div[@id="text"]').snapshotItem(0); pages = parsePages(text.innerHTML); addKeys(); header = util.evaluateXPath('//*[@id="header"]').snapshotItem(0); pre = util.evaluateXPath('//pre[@id="page"]').snapshotItem(0); pre.style.fontSize = fontSize+'px'; pre.style.margin = '0px'; loadPage(0); } function stop() { load_setting(original_setting); } commands.add(['presentation'], 'start presentation', //{{{ function(args) { start(); }, { argCount: '0', }); //}}} })(); // vim: fdm=marker sw=4 ts=4 et: