From: Daniel Carl Date: Fri, 21 Nov 2014 22:17:40 +0000 (+0100) Subject: Added hints by letters. X-Git-Url: https://git.owens.tech/wrapped.html/wrapped.html/git?a=commitdiff_plain;h=16af4dcc6dcb52f84f090a59640405705abde3a8;p=vimb.git Added hints by letters. The new setting 'hintkeys' holds the chars to use as hint label to fire the hints. Default value is '0123456789', but it's now possible to use letters too. --- diff --git a/doc/vimb.1 b/doc/vimb.1 index 4e4647d..8901019 100644 --- a/doc/vimb.1 +++ b/doc/vimb.1 @@ -226,18 +226,18 @@ was given. .SS Hinting The hinting is the way to do what you would do with the mouse in common mouse-driven browsers. Open URI, yank URI, save page and so on. If the hinting -is started, the relevant elements on the page will be marked by numbered -labels. Hints can be selected by using , or , , by -typing the number of the label, or filtering the elements by some text that is -part of the hinted element (like URI, link text, button label) and any -combination of this methods. If is pressed, the current active hint -will be fired. If only one possible hint remains, this will be fired -automatically. +is started, the relevant elements on the page will be marked by labels +generated from configured `hintkeys'. Hints can be selected by using , + or , , by typing the chars of the label, or filtering the +elements by some text that is part of the hinted element (like URI, link text, +button label) and any combination of this methods. If is pressed, the +current active hint will be fired. If only one possible hint remains, this +will be fired automatically. .BI Syntax: " ;{mode}{hint}" Start hint mode. Different elements depending on \fImode\fP are highlighted -and numbered. Elements can be selected either by typing their number, or by +and numbered. Elements can be selected either by typing their label, or by typing part of their text (\fIhint\fP) to narrow down the result. When an element has been selected, it is automatically clicked or used (depending on \fImode\fP) and hint mode ends. @@ -1231,6 +1231,12 @@ Example: Timeout before automatically following a non-unique numerical hint. To disable auto fire of hints, set this value to 0. .TP +.B hintkeys (string) +The keys used to label and select hints. With its default value, each hint has +a unique number which can be typed to select it, while all other characters +are used to filter hints based on their text. With a value such as asdfg;lkjh, +each hint is `numbered' based on the characters of the home row. +.TP .B history-max-items (int) Maximum number of unique items stored in search-, command or URI history. If history-max-items is set to 0, the history file will not be changed. diff --git a/src/hints.c b/src/hints.c index 516f204..f5fd307 100644 --- a/src/hints.c +++ b/src/hints.c @@ -86,13 +86,6 @@ VbResult hints_keypress(int key) if (call_hints_function("update", 1, arguments)) { return RESULT_COMPLETE; } - } else if (VB_IS_DIGIT(key)) { - fire_timeout(true); - - arguments[0] = JSValueMakeNumber(hints.ctx, key - '0'); - if (call_hints_function("update", 1, arguments)) { - return RESULT_COMPLETE; - } } else if (key == KEY_TAB) { fire_timeout(false); hints_focus_next(false); @@ -103,6 +96,13 @@ VbResult hints_keypress(int key) hints_focus_next(true); return RESULT_COMPLETE; + } else { + /* try to handle the key by the javascript */ + arguments[0] = js_string_to_ref(hints.ctx, (char[]){key, '\0'}); + if (call_hints_function("update", 1, arguments)) { + fire_timeout(true); + return RESULT_COMPLETE; + } } fire_timeout(false); @@ -167,9 +167,10 @@ void hints_create(const char *input) JSValueRef arguments[] = { js_string_to_ref(hints.ctx, (char[]){hints.mode, '\0'}), JSValueMakeBoolean(hints.ctx, hints.gmode), - JSValueMakeNumber(hints.ctx, MAXIMUM_HINTS) + JSValueMakeNumber(hints.ctx, MAXIMUM_HINTS), + js_string_to_ref(hints.ctx, GET_CHAR("hintkeys")), }; - call_hints_function("init", 3, arguments); + call_hints_function("init", 4, arguments); /* if hinting is started there won't be any additional filter given and * we can go out of this function */ diff --git a/src/hints.js b/src/hints.js index 0486714..bef1193 100644 --- a/src/hints.js +++ b/src/hints.js @@ -252,8 +252,9 @@ Object.freeze((function(){ function show(fireLast) { var i, hint, newIdx, - num = 1, - matcher = getMatcher(filterText); + n = 1, + matcher = getMatcher(filterText), + str = getHintString(filterNum); /* clear the array of valid hints */ validHints = []; @@ -263,16 +264,15 @@ Object.freeze((function(){ if (!matcher(hint.text)) { hint.hide(); } else { - /* assign the new hint number to the hint */ - hint.num = num; + /* assign the new hint number/letters as label to the hint */ + hint.num = getHintString(n++); /* check for number filter */ - if (!filterNum || 0 === String(num).indexOf(String(filterNum))) { + if (!filterNum || 0 === hint.num.indexOf(str)) { hint.show(); validHints.push(hint); } else { hint.hide(); } - num++; } } if (fireLast && validHints.length <= 1) { @@ -298,6 +298,33 @@ Object.freeze((function(){ }; } + /* Retrun the hint string for a given number based on configured hintkeys */ + function getHintString(n) { + var res = [], + len = config.hintKeys.length; + do { + res.push(config.hintKeys[n % len]); + n = Math.floor(n / len); + } while (n > 0); + + return res.reverse().join(""); + } + + /* Return the hint index to a given hint string. This is the reverse of */ + /* getHintString() */ + function getHintNumber(str) { + var base = config.hintKeys.length, + res = 0, + ch, i; + + for (i = 0; i < base; i++) { + ch = str.charAt(i); + res = res * base + config.hintKeys.indexOf(ch); + } + + return res; + } + function getOffsets(doc) { var body = doc.body || doc.documentElement, style = body.style, @@ -529,7 +556,7 @@ Object.freeze((function(){ /* the api */ return { - init: function init(mode, keepOpen, maxHints) { + init: function init(mode, keepOpen, maxHints, hintKeys) { var prop, /* holds the xpaths for the different modes */ xpathmap = { @@ -552,7 +579,8 @@ Object.freeze((function(){ /* handle forms only useful when there are form fields in xpath */ /* don't handle form for Y to allow to yank form filed content */ /* instead of switching to input mode */ - handleForm: ("eot".indexOf(mode) >= 0) + handleForm: ("eot".indexOf(mode) >= 0), + hintKeys: hintKeys }; for (prop in xpathmap) { if (prop.indexOf(mode) >= 0) { @@ -578,14 +606,15 @@ Object.freeze((function(){ return show(true); }, update: function update(n) { + var pos, + keys = config.hintKeys; /* delete last filter number digit */ if (null === n && filterNum) { - filterNum = Math.floor(filterNum / 10); + filterNum = Math.floor(filterNum / keys.length); return show(false); } - if ((n >= 1 && n <= 9) || (n === 0 && filterNum)) { - /* allow a zero as non-first number */ - filterNum = (filterNum ? filterNum * 10 : 0) + n; + if ((pos = keys.indexOf(n)) >= 0) { + filterNum = filterNum * keys.length + pos; return show(true); } return "ERROR:"; diff --git a/src/setting.c b/src/setting.c index bacce09..6596669 100644 --- a/src/setting.c +++ b/src/setting.c @@ -202,6 +202,7 @@ void setting_init() setting_add("home-page", TYPE_CHAR, &SETTING_HOME_PAGE, NULL, 0, NULL); i = 1000; setting_add("hint-timeout", TYPE_INTEGER, &i, NULL, 0, NULL); + setting_add("hintkeys", TYPE_CHAR, &"0123456789", NULL, 0, NULL); setting_add("download-path", TYPE_CHAR, &"", internal, 0, &vb.config.download_dir); i = 2000; setting_add("history-max-items", TYPE_INTEGER, &i, internal, 0, &vb.config.history_max);