1
0
mirror of https://github.com/gryf/pentadactyl-pm.git synced 2025-12-23 14:02:28 +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> <pre>
2007-xx-xx: 2007-xx-xx:
* version 0.6 * 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 * 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 * Escape finally clears any selection made in the document
* initial start of caret mode. Start with 'i', stop with Escape; * initial start of caret mode. Start with 'i', stop with Escape;

View File

@@ -140,6 +140,8 @@ function Buffer() //{{{
return window.content.document.title; return window.content.document.title;
}); });
this.lastInputField = null; // used to keep track of the right field for "gi"
// returns an XPathResult object // returns an XPathResult object
this.evaluateXPath = function(expression, doc, ordered) 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 // this adds an event which is is called on each page load, even if the
// page is loaded in a background tab // page is loaded in a background tab
getBrowser().addEventListener("load", onPageLoad, true); 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 // called when the active document is scrolled
getBrowser().addEventListener("scroll", function (event) getBrowser().addEventListener("scroll", function (event)
@@ -378,71 +381,96 @@ function Events() //{{{
return (key == "<Esc>" || key == "<C-[>" || key == "<C-c>"); 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() this.onEscape = function()
{ {
if (!vimperator.hasMode(vimperator.modes.ESCAPE_ONE_KEY)) 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 // clear any selection made
// FIXME: need to make more general to allow caret/visual mode also for text fields
var selection = window.content.getSelection(); var selection = window.content.getSelection();
selection.collapseToStart(); selection.collapseToStart();
vimperator.setMode(vimperator.modes.NORMAL); if (vimperator.hasMode(vimperator.modes.VISUAL))
vimperator.commandline.clear(); {
vimperator.hints.disableHahMode(); if (vimperator.hasMode(vimperator.modes.TEXTAREA))
vimperator.statusline.updateUrl(); vimperator.editor.unselectText();
vimperator.focusContent(); 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) this.onKeyPress = function(event)
{ {
var key = vimperator.events.toString(event); var key = vimperator.events.toString(event);
if (!key) if (!key)
return false; 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)) if (vimperator.hasMode(vimperator.modes.MENU))
return false; 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) // handle Escape-one-key mode (Ctrl-v)
if (vimperator.hasMode(vimperator.modes.ESCAPE_ONE_KEY) && !vimperator.hasMode(vimperator.modes.ESCAPE_ALL_KEYS)) 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)) if (vimperator.hasMode(vimperator.modes.ESCAPE_ONE_KEY))
vimperator.removeMode(null, vimperator.modes.ESCAPE_ONE_KEY); // and then let flow continue vimperator.removeMode(null, vimperator.modes.ESCAPE_ONE_KEY); // and then let flow continue
else if (key == "<Esc>" || key == "<C-[>" || key == "<C-v>") 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 else
return false; 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 {{{ // // FIXME: handle middle click in content area {{{
// // alert(event.target.id); // // alert(event.target.id);
// if (/*event.type == 'mousedown' && */event.button == 1 && event.target.id == 'content') // if (/*event.type == 'mousedown' && */event.button == 1 && event.target.id == 'content')
@@ -486,8 +526,8 @@ function Events() //{{{
if (vimperator.hasMode(vimperator.modes.HINTS)) if (vimperator.hasMode(vimperator.modes.HINTS))
{ {
// never propagate this key to firefox, when hints are visible // never propagate this key to firefox, when hints are visible
event.preventDefault(); //event.preventDefault();
event.stopPropagation(); //event.stopPropagation();
var map = vimperator.mappings.get(vimperator.modes.HINTS, key); var map = vimperator.mappings.get(vimperator.modes.HINTS, key);
if (map) if (map)
@@ -568,11 +608,8 @@ function Events() //{{{
if ((vimperator.input.buffer + key).match(/^[1-9][0-9]*$/)) if ((vimperator.input.buffer + key).match(/^[1-9][0-9]*$/))
{ {
vimperator.input.buffer += key; vimperator.input.buffer += key;
vimperator.statusline.updateInputBuffer(vimperator.input.buffer);
return true;
} }
else if (vimperator.input.pendingMap)
if (vimperator.input.pendingMap)
{ {
vimperator.input.buffer = ""; vimperator.input.buffer = "";
@@ -580,8 +617,6 @@ function Events() //{{{
vimperator.input.pendingMap.execute(null, vimperator.input.count, key); vimperator.input.pendingMap.execute(null, vimperator.input.count, key);
vimperator.input.pendingMap = null; vimperator.input.pendingMap = null;
event.preventDefault();
event.stopPropagation();
} }
else if (map = vimperator.mappings.get(mode, candidate_command)) else if (map = vimperator.mappings.get(mode, candidate_command))
{ {
@@ -596,23 +631,30 @@ function Events() //{{{
else else
{ {
vimperator.input.buffer = ""; vimperator.input.buffer = "";
// vimperator.log("executed: " + candidate_command + " in mode: " + mode, 8);
map.execute(null, vimperator.input.count); map.execute(null, vimperator.input.count);
} }
event.preventDefault();
event.stopPropagation();
} }
else if (vimperator.mappings.getCandidates(mode, candidate_command).length > 0) else if (vimperator.mappings.getCandidates(mode, candidate_command).length > 0)
{ {
vimperator.input.buffer += key; vimperator.input.buffer += key;
event.preventDefault();
event.stopPropagation();
} }
else else
{ {
vimperator.input.buffer = ""; vimperator.input.buffer = "";
vimperator.input.pendingMap = null; 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); 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) this.echo = function(str, flags)
{ {
var focused = document.commandDispatcher.focusedElement; 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; return false;
if (typeof str != "string") if (typeof str != "string")
@@ -249,7 +249,7 @@ function CommandLine() //{{{
this.echoErr = function(str) this.echoErr = function(str)
{ {
var focused = document.commandDispatcher.focusedElement; 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; return false;
setErrorStyle(); setErrorStyle();
@@ -296,6 +296,7 @@ function CommandLine() //{{{
{ {
multiline_input_widget.collapsed = true; multiline_input_widget.collapsed = true;
multiline_output_widget.collapsed = true; multiline_output_widget.collapsed = true;
completionlist.hide();
setPrompt(" "); // looks faster than an empty string as most prompts are 1 char long setPrompt(" "); // looks faster than an empty string as most prompts are 1 char long
setCommand(""); setCommand("");
@@ -326,6 +327,9 @@ function CommandLine() //{{{
} }
else if (event.type == "keypress") else if (event.type == "keypress")
{ {
if (!cur_extended_mode)
return;
var key = vimperator.events.toString(event); var key = vimperator.events.toString(event);
/* user pressed ENTER to carry out a command */ /* user pressed ENTER to carry out a command */
@@ -550,7 +554,6 @@ function CommandLine() //{{{
var key = vimperator.events.toString(event); var key = vimperator.events.toString(event);
if (vimperator.events.isAcceptKey(key)) 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); var text = multiline_input_widget.value.substr(0, multiline_input_widget.selectionStart);
if (text.match(multiline_regexp)) if (text.match(multiline_regexp))
{ {

View File

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