1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 09:48:00 +01:00

- TEXTAREA support! Not working perfectly, but for a one-day-work i am impressed myself,

now just needing to add commands with MOTION like cw or d0. Visual mode works already
quite well.
- gi support
- BUG: special keys like home/end in the location bar stoped working by this dramatic changes
This commit is contained in:
Martin Stubenschrott
2007-09-08 04:17:00 +00:00
parent f2345169f1
commit 009d567b0c
7 changed files with 679 additions and 269 deletions

3
NEWS
View File

@@ -1,6 +1,9 @@
<pre>
2007-xx-xx:
* version 0.6
* new gi browser command to focus last used input box
* edit TEXTAREAs with many vim commands in a vim and even visual mode
* support for emacs/bash-like ctrl-e/a/u/k/h keys in single line text fields
* support for * and # mappings to search for the text selection or the text under the cursor
* Escape finally clears any selection made in the document
* initial start of caret mode. Start with 'i', stop with Escape;

View File

@@ -140,6 +140,8 @@ function Buffer() //{{{
return window.content.document.title;
});
this.lastInputField = null; // used to keep track of the right field for "gi"
// returns an XPathResult object
this.evaluateXPath = function(expression, doc, ordered)
{

View File

@@ -64,6 +64,9 @@ function Events() //{{{
// this adds an event which is is called on each page load, even if the
// page is loaded in a background tab
getBrowser().addEventListener("load", onPageLoad, true);
// to keep track if we are in a text field
//getBrowser().addEventListener("focus", onFocus, true);
//getBrowser().addEventListener("focus", onFocus, true);
// called when the active document is scrolled
getBrowser().addEventListener("scroll", function (event)
@@ -378,71 +381,96 @@ function Events() //{{{
return (key == "<Esc>" || key == "<C-[>" || key == "<C-c>");
}
// event is delibarately not used, as i don't seem to have access to the really focus target
this.onFocusChange = function(event)
{
if (vimperator.hasMode(vimperator.modes.COMMAND_LINE))
return;
var elem = window.document.commandDispatcher.focusedElement;
if (elem && elem instanceof HTMLInputElement &&
(elem.type.toLowerCase() == "text" || elem.type.toLowerCase() == "password"))
{
vimperator.setMode(vimperator.modes.INSERT);
vimperator.buffer.lastInputField = elem;
}
else if (elem && elem instanceof HTMLTextAreaElement)
{
if (elem.selectionEnd - elem.selectionStart > 0)
vimperator.setMode(vimperator.modes.VISUAL, vimperator.modes.TEXTAREA);
else
vimperator.setMode(vimperator.modes.TEXTAREA);
vimperator.buffer.lastInputField = elem;
}
else
{
if (vimperator.hasMode(vimperator.modes.INSERT) ||
vimperator.hasMode(vimperator.modes.TEXTAREA))
vimperator.setMode(vimperator.modes.NORMAL); // FIXME: remember previous mode
}
}
// global escape handler, is called in ALL modes
// XXX: split up and move to mappings.js as closures?
this.onEscape = function()
{
if (!vimperator.hasMode(vimperator.modes.ESCAPE_ONE_KEY))
{
// setting this option will trigger an observer which will care about all other details
if (vimperator.hasMode(vimperator.modes.CARET))
Options.setFirefoxPref("accessibility.browsewithcaret", false);
// clear any selection made
// FIXME: need to make more general to allow caret/visual mode also for text fields
var selection = window.content.getSelection();
selection.collapseToStart();
vimperator.setMode(vimperator.modes.NORMAL);
vimperator.commandline.clear();
vimperator.hints.disableHahMode();
vimperator.statusline.updateUrl();
vimperator.focusContent();
if (vimperator.hasMode(vimperator.modes.VISUAL))
{
if (vimperator.hasMode(vimperator.modes.TEXTAREA))
vimperator.editor.unselectText();
vimperator.setMode(vimperator.getMode()[1], vimperator.modes.NONE);
}
else if (vimperator.hasMode(vimperator.modes.CARET))
{
// setting this option will trigger an observer which will care about all other details
// like setting the NORMAL mode
Options.setFirefoxPref("accessibility.browsewithcaret", false);
}
else if (vimperator.hasMode(vimperator.modes.INSERT))
{
if(vimperator.hasMode(vimperator.modes.TEXTAREA))
vimperator.setMode(vimperator.modes.TEXTAREA);
else
{
vimperator.editor.unselectText();
vimperator.setMode(vimperator.modes.NORMAL);
vimperator.focusContent();
}
}
else
{
vimperator.setMode(vimperator.modes.NORMAL);
vimperator.commandline.clear();
vimperator.hints.disableHahMode();
vimperator.statusline.updateUrl();
vimperator.focusContent();
}
}
}
// this keypress handler gets always called first, even if e.g.
// the commandline has focus
this.onKeyPress = function(event)
{
var key = vimperator.events.toString(event);
if (!key)
return false;
// sometimes the non-content area has focus, making our keys not work
// if (event.target.id == "main-window")
// alert("focusContent();");
var stop = true; // set to false if we should NOT consume this event but let also firefox handle it
// menus have their own command handlers
if (vimperator.hasMode(vimperator.modes.MENU))
return false;
// XXX: ugly hack for now pass certain keys to firefox as they are without beeping
// also fixes key navigation in menus, etc.
if (key == "<Tab>" || key == "<Return>" || key == "<Space>" || key == "<Up>" || key == "<Down>")
return false;
// XXX: for now only, later: input mappings if form element focused
if (isFormElemFocused())
{
if (key == "<S-Insert>")
{
var elt = window.document.commandDispatcher.focusedElement;
if (elt.setSelectionRange && readFromClipboard())
// readFromClipboard would return 'undefined' if not checked
// dunno about .setSelectionRange
{
var rangeStart = elt.selectionStart; // caret position
var rangeEnd = elt.selectionEnd;
var tempStr1 = elt.value.substring(0,rangeStart);
var tempStr2 = readFromClipboard();
var tempStr3 = elt.value.substring(rangeEnd);
elt.value = tempStr1 + tempStr2 + tempStr3;
elt.selectionStart = rangeStart + tempStr2.length;
elt.selectionEnd = elt.selectionStart;
// prevent additional firefox-clipboard pasting
event.preventDefault();
}
}
return false;
//vimperator.setMode(vimperator.modes.CARET); // FOR TESTING ONLY
}
// handle Escape-one-key mode (Ctrl-v)
if (vimperator.hasMode(vimperator.modes.ESCAPE_ONE_KEY) && !vimperator.hasMode(vimperator.modes.ESCAPE_ALL_KEYS))
{
@@ -455,11 +483,23 @@ function Events() //{{{
if (vimperator.hasMode(vimperator.modes.ESCAPE_ONE_KEY))
vimperator.removeMode(null, vimperator.modes.ESCAPE_ONE_KEY); // and then let flow continue
else if (key == "<Esc>" || key == "<C-[>" || key == "<C-v>")
; // let flow continue to handle these keys
; // let flow continue to handle these keys to cancel escape-all-keys mode
else
return false;
}
// FIXME: proper way is to have a better onFocus handler which also handles events for the XUL
if (!vimperator.hasMode(vimperator.modes.TEXTAREA) &&
!vimperator.hasMode(vimperator.modes.INSERT) &&
isFormElemFocused()) // non insert mode, but e.g. the location bar has focus
return false;
// XXX: ugly hack for now pass certain keys to firefox as they are without beeping
// also fixes key navigation in combo boxes, etc.
if (key == "<Tab>" || key == "<S-Tab>")// || key == "<Return>" || key == "<Space>" || key == "<Up>" || key == "<Down>")
return false;
// // FIXME: handle middle click in content area {{{
// // alert(event.target.id);
// if (/*event.type == 'mousedown' && */event.button == 1 && event.target.id == 'content')
@@ -486,8 +526,8 @@ function Events() //{{{
if (vimperator.hasMode(vimperator.modes.HINTS))
{
// never propagate this key to firefox, when hints are visible
event.preventDefault();
event.stopPropagation();
//event.preventDefault();
//event.stopPropagation();
var map = vimperator.mappings.get(vimperator.modes.HINTS, key);
if (map)
@@ -568,11 +608,8 @@ function Events() //{{{
if ((vimperator.input.buffer + key).match(/^[1-9][0-9]*$/))
{
vimperator.input.buffer += key;
vimperator.statusline.updateInputBuffer(vimperator.input.buffer);
return true;
}
if (vimperator.input.pendingMap)
else if (vimperator.input.pendingMap)
{
vimperator.input.buffer = "";
@@ -580,8 +617,6 @@ function Events() //{{{
vimperator.input.pendingMap.execute(null, vimperator.input.count, key);
vimperator.input.pendingMap = null;
event.preventDefault();
event.stopPropagation();
}
else if (map = vimperator.mappings.get(mode, candidate_command))
{
@@ -596,23 +631,30 @@ function Events() //{{{
else
{
vimperator.input.buffer = "";
// vimperator.log("executed: " + candidate_command + " in mode: " + mode, 8);
map.execute(null, vimperator.input.count);
}
event.preventDefault();
event.stopPropagation();
}
else if (vimperator.mappings.getCandidates(mode, candidate_command).length > 0)
{
vimperator.input.buffer += key;
event.preventDefault();
event.stopPropagation();
}
else
{
vimperator.input.buffer = "";
vimperator.input.pendingMap = null;
vimperator.beep();
if (vimperator.hasMode(vimperator.modes.INSERT) ||
vimperator.hasMode(vimperator.modes.COMMAND_LINE))
stop = false; // command was not a vimperator command, maybe it is a firefox command
else
vimperator.beep();
}
if (stop)
{
event.preventDefault();
event.stopPropagation();
}
vimperator.statusline.updateInputBuffer(vimperator.input.buffer);

File diff suppressed because it is too large Load Diff

View File

@@ -226,7 +226,7 @@ function CommandLine() //{{{
this.echo = function(str, flags)
{
var focused = document.commandDispatcher.focusedElement;
if (/*!echo_allowed && focused && */focused == command_widget.inputField)
if (focused && focused == command_widget.inputField || focused == multiline_input_widget.inputField)
return false;
if (typeof str != "string")
@@ -249,7 +249,7 @@ function CommandLine() //{{{
this.echoErr = function(str)
{
var focused = document.commandDispatcher.focusedElement;
if (/*!echo_allowed && focused && */focused == command_widget.inputField)
if (focused && focused == command_widget.inputField || focused == multiline_input_widget.inputField)
return false;
setErrorStyle();
@@ -296,6 +296,7 @@ function CommandLine() //{{{
{
multiline_input_widget.collapsed = true;
multiline_output_widget.collapsed = true;
completionlist.hide();
setPrompt(" "); // looks faster than an empty string as most prompts are 1 char long
setCommand("");
@@ -326,6 +327,9 @@ function CommandLine() //{{{
}
else if (event.type == "keypress")
{
if (!cur_extended_mode)
return;
var key = vimperator.events.toString(event);
/* user pressed ENTER to carry out a command */
@@ -550,7 +554,6 @@ function CommandLine() //{{{
var key = vimperator.events.toString(event);
if (vimperator.events.isAcceptKey(key))
{
//var lines = multiline_input_widget.value.substr(0, multiline_input_widget.selectionStart).split(/\n/);
var text = multiline_input_widget.value.substr(0, multiline_input_widget.selectionStart);
if (text.match(multiline_regexp))
{

View File

@@ -41,6 +41,7 @@ const vimperator = (function() //{{{
HINTS: 1 << 3,
COMMAND_LINE: 1 << 4,
CARET: 1 << 5, // text cursor is visible
TEXTAREA: 1 << 6, // text cursor is in a HTMLTextAreaElement
// extended modes
EX: 1 << 10,
READ_MULTILINE: 1 << 11,
@@ -60,6 +61,7 @@ const vimperator = (function() //{{{
mode_messages[modes.VISUAL] = "VISUAL";
mode_messages[modes.HINTS] = "HINTS";
mode_messages[modes.CARET] = "CARET"; // XXX: not a perfect name
mode_messages[modes.TEXTAREA] = "TEXTAREA"; // XXX: not a perfect name
mode_messages[modes.ESCAPE_ONE_KEY] = "escape one key";
mode_messages[modes.ESCAPE_ALL_KEYS] = "escape all keys";
mode_messages[modes.ESCAPE_ONE_KEY | modes.ESCAPE_ALL_KEYS] = "pass one key";
@@ -90,7 +92,7 @@ const vimperator = (function() //{{{
var str_extended = mode_messages[extended_mode];
if (!str_mode && !str_extended)
{
vimperator.echo("");
vimperator.commandline.echo("");
return;
}
@@ -99,7 +101,7 @@ const vimperator = (function() //{{{
else
str_extended = "";
vimperator.echo("-- " + str_mode + str_extended + " --");
vimperator.commandline.echo("-- " + str_mode.toUpperCase() + str_extended.toLowerCase() + " --");
}
function expandPath(path)
@@ -218,6 +220,7 @@ const vimperator = (function() //{{{
triggerCallback: function(type, mode, data)
{
// dump("type: " + type + " mode: " + mode + "data: " + data + "\n");
for (var i in callbacks)
{
var [thistype, thismode, thisfunc] = callbacks[i];
@@ -292,6 +295,7 @@ const vimperator = (function() //{{{
popup.height = box.height;
popup.width = box.width;
//popup.style.backgroundColor = "black";
////popup.showPopup(win, box.screenX, box.screenY, "popup");
//popup.showPopup(win, -1, -1, "popup", "topleft", "topleft");
@@ -383,7 +387,7 @@ const vimperator = (function() //{{{
console_service.logStringMessage('vimperator: ' + msg);
},
// logs an object to the javascript error console also prints all
// log an object to the javascript error console and print all
// properties of the object
logObject: function(object, level)
{
@@ -619,6 +623,8 @@ const vimperator = (function() //{{{
vimperator.statusline = new StatusLine();
vimperator.log("Loading module buffer...", 3);
vimperator.buffer = new Buffer();
vimperator.log("Loading module editor...", 3);
vimperator.editor = new Editor();
vimperator.log("Loading module tabs...", 3);
vimperator.tabs = new Tabs();
vimperator.log("Loading module marks...", 3);

View File

@@ -44,6 +44,7 @@ the terms of any one of the MPL, the GPL or the LGPL.
<script type="application/x-javascript;version=1.7" src="buffers.js"/>
<script type="application/x-javascript;version=1.7" src="commands.js"/>
<script type="application/x-javascript;version=1.7" src="completion.js"/>
<script type="application/x-javascript;version=1.7" src="editor.js"/>
<script type="application/x-javascript;version=1.7" src="events.js"/>
<script type="application/x-javascript;version=1.7" src="file.js"/>
<script type="application/x-javascript;version=1.7" src="find.js"/>
@@ -124,6 +125,13 @@ the terms of any one of the MPL, the GPL or the LGPL.
-->
<panel id="vimperator-visualbell"/>
<!--this notifies us also of focus events in the XUL
from: http://developer.mozilla.org/en/docs/XUL_Tutorial:Updating_Commands !-->
<commandset id="updateVimperatorFocus"
commandupdater="true"
events="focus"
oncommandupdate="vimperator.events.onFocusChange(event);"/>
</window>
</overlay>