Skip to content

Instantly share code, notes, and snippets.

@gardejo
Created October 9, 2015 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gardejo/d523091b6b72103e3a10 to your computer and use it in GitHub Desktop.
Save gardejo/d523091b6b72103e3a10 to your computer and use it in GitHub Desktop.
/**
* @file KeySnail initialization sub-sucript: Hooks.
* @copyright Copyright (c) 2012-2015 MORIYA Masaki, alias "Gardejo".
* @author gardejo@ermitejo.com (MORIYA Masaki, alias "Gardejo")
* @license This file is available under The MIT License.
* See `.keysnail.d/doc/license.txt`.
* @see https://github.com/mooz/keysnail/wiki/Hook
*/
'use strict';
// ================================================================
// Quitted
// ================================================================
/**
* Escape thoroughly.
* @todo Focus on the prompt when `Prompt is already used by another command`.
* @see http://d.hatena.ne.jp/amacou/20100129/1264768730
* @see https://developer.mozilla.org/en-US/Add-ons/Code_snippets/Sidebar
*/
hook.setHook('KeyBoardQuit', function(aEvent) {
// Ignore quitting, if we're halfway through with our current key sequence.
if (key.currentKeySequence.length) {
return;
}
// Close the KeySnail find bar, if we're sure one is currently open.
command.closeFindBar();
let marked = command.marked(aEvent);
if ( util.isCaretEnabled() ) {
if (marked) {
// Reset the mark, if we're sure we're in fence mode.
command.resetMark(aEvent);
}
else {
// Focus to the content.
// if ('blur' in aEvent.target) {
// aEvent.target.blur();
// }
let elem = document.commandDispatcher.focusedElement;
if (elem) {
elem.blur();
}
gBrowser.focus();
_content.focus();
}
}
else {
// Reset the selecting.
goDoCommand('cmd_selectNone');
}
// Send the ESC key event, if we're sure browser window is currently open.
if (
KeySnail.windowType === 'navigator:browser' &&
! marked
) {
key.generateKey(aEvent.originalTarget, KeyEvent.DOM_VK_ESCAPE, true)
}
// Close the KeySnail prompt, if we're sure one is currently open.
if (
// !document.getElementById("keysnail-prompt").hidden
aEvent.target !== document.getElementById('keysnail-prompt-textbox')
) {
try {
prompt.finish(true);
}
catch (exception) {
// util.message(exception);
}
}
// Close the sidebar, if we're sure one is currently open.
toggleSidebar(); // Bookmark or History
});
// ================================================================
// Location changed
// ================================================================
/**
* Review whether a black-list should apply to a (new) page.
*/
hook.addToHook('LocationChange', function(aNsURI) {
key.suspendWhenMatched( (aNsURI ? aNsURI.spec : null), key.blackList );
});
// ================================================================
// Menu shown/hidden
// ================================================================
/**
* Suspend KeySnail on menu shown.
* Note: `MenuPopupShowing`=`MainMenuPopupShowing`+`ContextMenuPopupShowing`.
* CAVEAT: Just if the focus is on (not a menu item but) the menu bar,
* `MainMenuPopupShowing` hook cannot be invoke.
* @see HookMenuPopup plugin: https://github.com/hogelog/keysnail-plugins/
* @see http://keysnail.g.hatena.ne.jp/hogelog/20100304/1267675261
* @see http://blog.livedoor.jp/shift_e/archives/65607045.html
* @see https://gist.github.com/304619
*/
hook.addToHook('MenuPopupShowing', function() {
ext.exec('suspend-keysnail');
});
/**
* Resume KeySnail on menu hidden.
* Note: `MenuPopupHiding`=`MainMenuPopupHiding`+`ContextMenuPopupHiding`.
*/
hook.addToHook('MenuPopupHiding', function() {
ext.exec('resume-keysnail');
});
// ================================================================
// Key pressed
// ================================================================
/**
* Show pressed key code on the window for debugging.
* @see https://github.com/mooz/keysnail/wiki/Hook#keypress
*/
hook.addToHook('KeyPress', function(aEvent) {
if (
// Displaying PrettyPrint in edit mode is too late.
key.getCurrentMode(aEvent) === key.modes.EDIT ||
// Annoying `window.alert()` willbe invoked instead of PrettyPrint.
! (content ? content.document : document).body || // Has no content.
util.isFrameSetWindow(content) // Has frames.
) {
return;
}
display.prettyPrint(
key.keyEventToString(aEvent),
{
timeout: 100,
// fade: 0, // Fading is too late.
style: plugins.options['stash.style.pretty_print'],
}
);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment