# # The main modes defined here are: # - VisualMode # - VisualLineMode # - CaretMode # # SuppressPrintable and CountPrefix are shared utility base classes. # Movement is a shared vim-like movement base class. # # The class inheritance hierarchy is: # - Mode, SuppressPrintable, CountPrefix, Movement, [ VisualMode | CaretMode ] # - Mode, SuppressPrintable, CountPrefix, Movement, VisualMode, VisualLineMode # # The possible mode states are: # - ..., VisualMode # - ..., VisualLineMode # - ..., CaretMode # - ..., VisualMode, FindMode # - ..., VisualLineMode, FindMode # - ..., CaretMode, FindMode # # This prevents printable characters from being passed through to underlying modes or the underlying page. class SuppressPrintable extends Mode constructor: (options = {}) -> handler = (event) => return @stopBubblingAndTrue if not KeyboardUtils.isPrintable event return @suppressEvent if event.type != "keydown" # Completely suppress Backspace and Delete, they change the selection. return @suppressEvent if event.keyCode in [ keyCodes.backspace, keyCodes.deleteKey ] # Suppress propagation (but not preventDefault) for keydown, printable events. DomUtils.suppressPropagation event @stopBubblingAndFalse super extend options, keydown: handler, keypress: handler, keyup: handler # This monitors keypresses and maintains the count prefix. class CountPrefix extends SuppressPrintable constructor: (options) -> @countPrefix = "" # This is an initial multiplier for the first count. @countPrefixFactor = options.initialCountPrefix || 1 super options @push _name: "#{@id}/count-prefix" keypress: (event) => @alwaysContinueBubbling => unless event.metaKey or event.ctrlKey or event.altKey keyChar = String.fromCharCode event.charCode @countPrefix = if keyChar.length == 1 and "0" <= keyChar <= "9" and @countPrefix + keyChar != "0" @countPrefix + keyChar else "" getCountPrefix: -> count = @countPrefixFactor * (if 0 < @countPrefix.length then parseInt @countPrefix else 1) @countPrefix = ""; @countPrefixFactor = 1 count # Symbolic names for some common strings. forward = "forward" backward = "backward" character = "character" word = "word" line = "line" sentence = "sentence" paragraph = "paragraph" vimword = "vimword" lineboundary= "lineboundary" # This implements vim-like movements, and includes quite a number of gereral utility methods. class Movement extends CountPrefix opposite: forward: backward, backward: forward # Paste from clipboard. paste: (callback) -> chrome.runtime.sendMessage handler: "pasteFromClipboard", (response) -> callback response # Copy to clipboard. copy: (text, isFinalUserCopy = false) -> chrome.runtime.sendMessage handler: "copyToClipboard", data: text # If isFinalUserCopy is set, then we're copying the final text selected by the user (and exiting). # However, @protectClipboard may later try to restore the original clipboard contents. Therefore, we # disable copy so that subsequent copies do not propagate. @copy = (->) if isFinalUserCopy # This is used whenever manipulating the selection may, as a side effect, change the clipboard's contents. # We restore the original clipboard contents when we're done. May be asynchronous. We use a lock so that # calls can be nested. protectClipboard: do -> locked = false (func) -> if locked then func() else locked = true @paste (text) => func(); @copy text; locked = false # Replace the current mode with another. For example, replace caret mode with visual mode, or replace visual # mode with visual-line mode. changeMode: (mode, options = {}) -> @exit() new mode options # Return the character following (to the right of) the focus, and leave the selection unchanged. Returns # undefined if no such character exists. getNextForwardCharacter: -> beforeText = @selection.toString() if beforeText.length == 0 or @getDirection() == forward @selection.modify "extend", forward, character afterText = @selection.toString() if beforeText != afterText @selection.modify "extend", backward, character afterText[afterText.length - 1] else beforeText[0] # Existing range selection is backwards. # As above, but backwards. getNextBackwardCharacter: -> beforeText = @selection.toString() if beforeText.length == 0 or @getDirection() == backward @selection.modify "extend", backward, character afterText = @selection.toString() if beforeText != afterText @selection.modify "extend", forward, character afterText[0] else beforeText[beforeText.length - 1] # Existing range selection is forwards. # Test whether the character following the focus is a word character (and leave the selection unchanged). nextCharacterIsWordCharacter: do -> regexp = null -> # This regexp matches "word" characters (apparently in any language). # From http://stackoverflow.com/questions/150033/regular-expression-to-match-non-english-characters regexp || = /[_0-9\u0041-\u005A\u0061-\u00
## www.pubnub.com - PubNub Real-time push service in the cloud.
# coding=utf8

## PubNub Real-time Push APIs and Notifications Framework
## Copyright (c) 2010 Stephen Blum
## http://www.pubnub.com/


import sys
from Pubnub import PubnubTwisted as Pubnub

publish_key = len(sys.argv) > 1 and sys.argv[1] or 'pam'
subscribe_key = len(sys.argv) > 2 and sys.argv[2] or 'pam'
secret_key = len(sys.argv) > 3 and sys.argv[3] or 'pam'
cipher_key = len(sys.argv) > 4 and sys.argv[4] or ''
ssl_on = len(sys.argv) > 5 and bool(sys.argv[5]) or False

## -----------------------------------------------------------------------
## Initiate Pubnub State
## -----------------------------------------------------------------------
pubnub = Pubnub(publish_key=publish_key, subscribe_key=subscribe_key,
                secret_key=secret_key, cipher_key=cipher_key, ssl_on=ssl_on)
channel = 'hello_world'
authkey = "abcd"

def callback(message):
    print(message)

pubnub.revoke(channel, authkey, callback=callback, error=callback)

pubnub.start()
haracter ] else if granularity == vimword @selection.modify @alterMethod, backward, word # As above, we implement this character-by-character to get consistent behavior on Windows and Linux. if granularity == word and direction == forward while @getNextForwardCharacter() and not @nextCharacterIsWordCharacter() return unless @runMovements [ forward, character ] while @nextCharacterIsWordCharacter() return unless @runMovements [ forward, character ] else @selection.modify @alterMethod, direction, granularity # Return a simple camparable value which depends on various aspects of the selection. This is used to # detect, after a movement, whether the selection has changed. hashSelection: (debug) -> range = @selection.getRangeAt(0) [ @element?.selectionStart, @selection.toString().length, range.anchorOffset, range.focusOffset, @selection.extentOffset, @selection.baseOffset ].join "/" # Call a function; return true if the selection changed, false otherwise. selectionChanged: (func) -> before = @hashSelection(); func(); @hashSelection() != before # Run a sequence of movements, stopping if a movement fails to change the selection. runMovements: (movements...) -> for movement in movements return false unless @selectionChanged => @runMovement movement true # Swap the anchor node/offset and the focus node/offset. This allows us to work with both ends of the # selection, and implements "o" for visual mode. reverseSelection: -> direction = @getDirection() element = document.activeElement if element and DomUtils.isEditable(element) and not element.isContentEditable # Note(smblott). This implementation is unacceptably expensive if the selection is large. We only use # it here because the normal method (below) does not work for simple text inputs. length = @selection.toString().length @collapseSelectionToFocus() @runMovement @opposite[direction], character for [0...length] else # Normal method (efficient). original = @selection.getRangeAt(0).cloneRange() range = original.cloneRange() range.collapse direction == backward @setSelectionRange range which = if direction == forward then "start" else "end" @selection.extend original["#{which}Container"], original["#{which}Offset"] # Try to extend the selection one character in direction. Return positive, negative or 0, indicating # whether the selection got bigger, or smaller, or is unchanged. extendByOneCharacter: (direction) -> length = @selection.toString().length @selection.modify "extend", direction, character @selection.toString().length - length # Get the direction of the selection. The selection is "forward" if the focus is at or after the anchor, # and "backward" otherwise. # NOTE(smblott). This could be better, see: https://dom.spec.whatwg.org/#interface-range (however, that # probably wouldn't work for text inputs). getDirection: -> # Try to move the selection forward or backward, check whether it got bigger or smaller (then restore it). for direction in [ forward, backward ] if change = @extendByOneCharacter direction @extendByOneCharacter @opposite[direction] return if 0 < change then direction else @opposite[direction] forward collapseSelectionToAnchor: -> if 0 < @selection.toString().length @selection[if @getDirection() == backward then "collapseToEnd" else "collapseToStart"]() collapseSelectionToFocus: -> if 0 < @selection.toString().length @selection[if @getDirection() == forward then "collapseToEnd" else "collapseToStart"]() setSelectionRange: (range) -> @selection.removeAllRanges() @selection.addRange range # A movement can be either a string (which will be passed to @runMovement count times), or a function (which # will be called once with count as its argument). movements: "l": "forward character" "h": "backward character" "j": "forward line" "k": "backward line" "e": "forward word" "b": "backward word" "w": "forward vimword" ")": "forward sentence" "(": "backward sentence" "}": "forward paragraph" "{": "backward paragraph" "0": "backward lineboundary" "$": "forward lineboundary" "G": "forward documentboundary" "gg": "backward documentboundary" "Y": (count) -> @selectLine count; @yank() # This handles a movement, but protects to selection while doing so. runMovementKeyChar: (args...) -> @protectClipboard => @handleMovementKeyChar args... # Handle a single movement keyChar. This is extended (wrapped) by super-classes. handleMovementKeyChar: (keyChar, count = 1) -> switch typeof @movements[keyChar] when "string" then @runMovement @movements[keyChar] for [0...count] when "function" then @movements[keyChar].call @, count @scrollIntoView() constructor: (options) -> @selection = window.getSelection() @movements = extend {}, @movements @commands = {} @keyQueue = "" super options # Aliases. @movements.B = @movements.b @movements.W = @movements.w # This is the main keyboard-event handler for movements and commands for all user modes (visual, # visual-line and caret). @push _name: "#{@id}/keypress" keypress: (event) => unless event.metaKey or event.ctrlKey or event.altKey @keyQueue += String.fromCharCode event.charCode # Keep at most two keyChars in the queue. @keyQueue = @keyQueue.slice Math.max 0, @keyQueue.length - 2 for command in [ @keyQueue, @keyQueue[1..] ] if command and (@movements[command] or @commands[command]) @selection = window.getSelection() @keyQueue = "" # We need to treat "0" specially. It can be either a movement, or a continutation of a count # prefix. Don't treat it as a movement if we already have an initial count prefix. return @continueBubbling if command == "0" and 0 < @countPrefix.length if @commands[command] @commands[command].call @, @getCountPrefix() @scrollIntoView() return @suppressEvent else if @movements[command] @runMovementKeyChar command, @getCountPrefix() return @suppressEvent @continueBubbling # Install basic bindings for find mode, "n" and "N". do => doFind = (count, backwards) => initialRange = @selection.getRangeAt(0).cloneRange() for [0...count] by 1 unless FindMode.execute null, {colorSelection: false, backwards} @setSelectionRange initialRange HUD.showForDuration("No matches for '#{FindMode.query.rawQuery}'", 1000) return # The find was successfull. If we're in caret mode, then we should now have a selection, so we can # drop back into visual mode. @changeMode VisualMode if @name == "caret" and 0 < @selection.toString().length @movements.n = (count) -> doFind count, false @movements.N = (count) -> doFind count, true @movements["/"] = -> @findMode = new FindMode returnToViewport: true @findMode.onExit => @changeMode VisualMode # # End of Movement constructor. # Yank the selection; always exits; either deletes the selection or collapses it; set @yankedText and return # it. yank: (args = {}) -> @yankedText = @selection.toString() @selection.collapseToStart() message = @yankedText.replace /\s+/g, " " message = message[...12] + "..." if 15 < @yankedText.length plural = if @yankedText.length == 1 then "" else "s" @exit() HUD.showForDuration "Yanked #{@yankedText.length} character#{plural}: \"#{message}\".", 2500 @yankedText exit: (event, target) -> @selection.collapseToStart() if event?.type == "keydown" and KeyboardUtils.isEscape event super event, target # For "daw", "das", and so on. We select a lexical entity (a word, a sentence or a paragraph). # Note(smblott). It would be better if the entities could be handled symmetrically. Unfortunately, they # cannot, and we have to handle each case individually. selectLexicalEntity: (entity, count = 1) -> switch entity when word if @nextCharacterIsWordCharacter() @runMovements [ forward, character ], [ backward, word ] @collapseSelectionToFocus() @runMovements ([0...count].map -> [ forward, vimword ])... when sentence @runMovements [ forward, character ], [ backward, sentence ] @collapseSelectionToFocus() @runMovements ([0...count].map -> [ forward, sentence ])... when paragraph # Chrome's paragraph movements are weird: they're not symmetrical, and tend to stop in odd places # (like mid-paragraph, for example). Here, we define a paragraph as a new-line delimited entity, # including the terminating newline. # Note(smblott). This does not currently use the count. char = @getNextBackwardCharacter() while char? and char != "\n" return unless @runMovements [ backward, character ], [ backward, lineboundary ] char = @getNextBackwardCharacter() @collapseSelectionToFocus() char = @getNextForwardCharacter() while char? and char != "\n" return unless @runMovements [ forward, character ], [ forward, lineboundary ] char = @getNextForwardCharacter() @runMovement forward, character # Scroll the focus into view. scrollIntoView: -> @protectClipboard => if @element and DomUtils.isEditable @element if @element.clientHeight < @element.scrollHeight if @element.isContentEditable elementWithFocus = DomUtils.getElementWithFocus @selection, @getDirection() == backward position = elementWithFocus.getClientRects()[0].bottom - @element.getClientRects()[0].top - @element.clientHeight + @element.scrollTop Scroller.scrollToPosition @element, position, 0 else position = if @getDirection() == backward then @element.selectionStart else @element.selectionEnd coords = DomUtils.getCaretCoordinates @element, position Scroller.scrollToPosition @element, coords.top, coords.left else unless @selection.type == "None" elementWithFocus = DomUtils.getElementWithFocus @selection, @getDirection() == backward Scroller.scrollIntoView elementWithFocus if elementWithFocus class VisualMode extends Movement constructor: (options = {}) -> @alterMethod = "extend" defaults = name: "visual" indicator: if options.indicator? then options.indicator else "Visual mode" singleton: VisualMode exitOnEscape: true super extend defaults, options # Establish or use the initial selection. If that's not possible, then enter caret mode. if @selection.type in [ "Caret", "Range" ] selectionRect = @selection.getRangeAt(0).getBoundingClientRect() selectionRect = Rect.intersect selectionRect, (Rect.create 0, 0, window.innerWidth, window.innerHeight) if selectionRect.height >= 0 and selectionRect.width >= 0 # The selection is visible in the current viewport. if @selection.type == "Caret" # The caret is in the viewport. Make make it visible. @extendByOneCharacter(forward) or @extendByOneCharacter backward else # The selection is outside of the viewport: clear it. We guess that the user has moved on, and is # more likely to be interested in visible content. @selection.removeAllRanges() if @selection.type != "Range" @changeMode CaretMode HUD.showForDuration "No usable selection, entering caret mode...", 2500 return @push _name: "#{@id}/enter/click" # Yank on . keypress: (event) => if event.keyCode == keyCodes.enter unless event.metaKey or event.ctrlKey or event.altKey or event.shiftKey @yank() return @suppressEvent @continueBubbling # Click in a focusable element exits. click: (event) => @alwaysContinueBubbling => @exit event, event.target if DomUtils.isFocusable event.target # Visual-mode commands. @commands.y = -> @yank() @commands.p = -> chrome.runtime.sendMessage handler: "openUrlInCurrentTab", url: @yank() @commands.P = -> chrome.runtime.sendMessage handler: "openUrlInNewTab", url: @yank() @commands.V = -> @changeMode VisualLineMode @commands.c = -> @collapseSelectionToFocus(); @changeMode CaretMode @commands.o = -> @reverseSelection() # # End of VisualMode constructor. exit: (event, target) -> # Don't leave the user in insert mode just because they happen to have selected text within an input # element. if document.activeElement and DomUtils.isEditable document.activeElement document.activeElement.blur() unless event?.type == "click" super event, target if @yankedText? console.log "yank:", @yankedText if @debug @copy @yankedText, true selectLine: (count) -> @reverseSelection() if @getDirection() == forward @runMovement backward, lineboundary @reverseSelection() @runMovement forward, line for [1...count] @runMovement forward, lineboundary # Include the next character if it is a newline. @runMovement forward, character if @getNextForwardCharacter() == "\n" class VisualLineMode extends VisualMode constructor: (options = {}) -> super extend { name: "visual/line", indicator: "Visual mode (line)" }, options @extendSelection() @commands.v = -> @changeMode VisualMode handleMovementKeyChar: (args...) -> super args... @extendSelection() extendSelection: -> initialDirection = @getDirection() for direction in [ initialDirection, @opposite[initialDirection] ] @runMovement direction, lineboundary @reverseSelection() class CaretMode extends Movement constructor: (options = {}) -> @alterMethod = "move" defaults = name: "caret" indicator: "Caret mode" singleton: VisualMode exitOnEscape: true super extend defaults, options # Establish the initial caret. switch @selection.type when "None" @establishInitialSelectionAnchor() if @selection.type == "None" @exit() HUD.showForDuration "Create a selection before entering visual mode.", 2500 return when "Range" @collapseSelectionToAnchor() @selection.modify "extend", forward, character @scrollIntoView() @push _name: "#{@id}/click" # Click in a focusable element exits. click: (event) => @alwaysContinueBubbling => @exit event, event.target if DomUtils.isFocusable event.target # Commands to exit caret mode, and enter visual mode. extend @commands, v: -> @changeMode VisualMode V: -> @changeMode VisualLineMode handleMovementKeyChar: (args...) -> @collapseSelectionToAnchor() super args... @selection.modify "extend", forward, character # When visual mode starts and there's no existing selection, we launch CaretMode and try to establish a # selection. As a heuristic, we pick the first non-whitespace character of the first visible text node # which seems to be big enough to be interesting. # TODO(smblott). It might be better to do something similar to Clearly or Readability; that is, try to find # the start of the page's main textual content. establishInitialSelectionAnchor: -> nodes = document.createTreeWalker document.body, NodeFilter.SHOW_TEXT while node = nodes.nextNode() # Don't choose short text nodes; they're likely to be part of a banner. if node.nodeType == 3 and 50 <= node.data.trim().length element = node.parentElement if DomUtils.getVisibleClientRect(element) and not DomUtils.isEditable element # Start at the offset of the first non-whitespace character. offset = node.data.length - node.data.replace(/^\s+/, "").length range = document.createRange() range.setStart node, offset range.setEnd node, offset @setSelectionRange range return true false root = exports ? window root.VisualMode = VisualMode root.VisualLineMode = VisualLineMode