
    g
fJ                         d Z dZdZdZdZdZddlmZ ddlmZ dd	lm	Z	 dd
lm
Z
 ddlmZ ddlmZ ddlmZ ddlmZ  G d d      Zy)z3Provides an Orca-controlled caret for text content.z$Id$z
$Revision$z$Date$z$Copyright (c) 2013-2015 Igalia, S.L.LGPL   )cmdnames)debug)input_event)keybindings)messages)
orca_state)settings_manager)AXTextc                       e Zd ZdZd Zd ZddZddZd Zd Z	d Z
dd	Zd
 ZddZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zy)CaretNavigationz=Implements the caret navigation support available to scripts.c                 ~    d| _         | j                  d      | _        t        j                         | _        d | _        y )NFT)
_suspendedget_handlers	_handlersr   KeyBindings	_bindings_last_input_event)selfs    7/usr/lib/python3/dist-packages/orca/caret_navigation.py__init__zCaretNavigation.__init__*   s5      **40$002!%    c                 r    || j                   j                         vry|j                  | j                  k(  ryy)z0Returns True if handler is a navigation command.FT)r   valuesfunctiontoggle_enabled)r   handlers     r   handles_navigationz"CaretNavigation.handles_navigation3   s6     $..//11t222r   c                     |rCd}t        j                  t         j                  |d       | j                          | j                  S | j                  j                         r| j                          | j                  S )z)Returns the caret-navigation keybindings.z&CARET NAVIGATION: Refreshing bindings.T)r   printMessage
LEVEL_INFO_setup_bindingsr   isEmpty)r   refresh
is_desktopmsgs       r   get_bindingszCaretNavigation.get_bindings>   sd     :Cu//d;  " ~~ ^^##%  "~~r   c                     |r7d}t        j                  t         j                  |d       | j                          | j                  S )z&Returns the caret-navigation handlers.z&CARET NAVIGATION: Refreshing handlers.T)r   r    r!   _setup_handlersr   )r   r$   r&   s      r   r   zCaretNavigation.get_handlersJ   s:     :Cu//d;  "~~r   c                 2   i | _         t        j                  | j                  t        j
                  | j                         | j                   d<   t        j                         j                  d      xr | j                   }t        j                  | j                  t        j                  |      | j                   d<   t        j                  | j                  t        j                  |      | j                   d<   t        j                  | j                  t        j                  |      | j                   d<   t        j                  | j                   t        j"                  |      | j                   d<   t        j                  | j$                  t        j&                  |      | j                   d<   t        j                  | j(                  t        j*                  |      | j                   d	<   t        j                  | j,                  t        j.                  |      | j                   d
<   t        j                  | j0                  t        j2                  |      | j                   d<   t        j                  | j4                  t        j6                  |      | j                   d<   t        j                  | j8                  t        j:                  |      | j                   d<   d| j                   }t=        j>                  t<        j@                  |d       y)z2Sets up the caret-navigation input event handlers.)enabledr   caretNavigationEnablednext_characterprevious_character	next_wordprevious_word	next_lineprevious_linestart_of_fileend_of_filestart_of_lineend_of_linez.CARET NAVIGATION: Handlers set up. Suspended: TN)!r   r   InputEventHandlerr   r   CARET_NAVIGATION_TOGGLEr   r
   
getManager
getSetting_next_characterCARET_NAVIGATION_NEXT_CHAR_previous_characterCARET_NAVIGATION_PREV_CHAR
_next_wordCARET_NAVIGATION_NEXT_WORD_previous_wordCARET_NAVIGATION_PREV_WORD
_next_lineCARET_NAVIGATION_NEXT_LINE_previous_lineCARET_NAVIGATION_PREV_LINE_start_of_fileCARET_NAVIGATION_FILE_START_end_of_fileCARET_NAVIGATION_FILE_END_start_of_lineCARET_NAVIGATION_LINE_START_end_of_lineCARET_NAVIGATION_LINE_ENDr   r    r!   )r   r+   r&   s      r   r)   zCaretNavigation._setup_handlersT   s     ))##00"oo-/ 	'( #--/::;ST $OO# 	 ))$$33!# 	'( ))((33!# 	+, ))33!# 	{# ))##33!# 	' ))33!# 	{# ))##33!# 	' ))##44!# 	' ))!!22!# 	}% ))##44!# 	' ))!!22!# 	}% ?t>OP5++S$7r   c           
      
   t        j                         | _        | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d| j                                t        j                         j                  d      xr | j                   }| j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d	      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d
      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             | j                  j                  t        j                  dt         j
                  t         j                  | j                  j                  d      d|             t        j                         j                  | j                  | j                  d      | _        d| j                   }t!        j"                  t         j$                  |d       | j                  g}t!        j&                  t         j$                  |d       y)z*Sets up the caret-navigation key bindings.F12r   r   r,   Rightr-   Leftr.   r/   r0   Downr1   Upr2   Endr6   Homer5   r4   r3   Fz.CARET NAVIGATION: Bindings set up. Suspended: TN)r   r   r   add
KeyBindingdefaultModifierMaskORCA_MODIFIER_MASKr   getr   r
   r9   r:   NO_MODIFIER_MASKCTRL_MODIFIER_MASKoverrideKeyBindingsr   r    r!   printTokens)r   r+   r&   tokenss       r   r"   zCaretNavigation._setup_bindings   s*    %002""//..""#34OO#%	& #--/::;ST $OO# 	 	""//,,""#34	 	""//,,""#78	 	""//.."";/	 	""//..""?3	 	""//,,"";/	 	""//,,""?3	 	""//,,""=1	 	""//,,""?3	 	""//..""=1	 	""//..""?3	 *446JJNNDNNE3 ?t>OP5++S$7..!%**FD9r   c                 b   | j                   duxrH | j                   t        j                  k(  xs) t        j                  j                  | j                         }| j                   | j                   j	                         }nd}d| d| }t        j                  t
        j                  |d       |S )z>Returns true if the last input event was a navigation command.NNonez)CARET NAVIGATION: Last navigation event (z) is last key event: T)r   r	   lastNonModifierKeyEventisReleaseForasSingleLineStringr   r    r!   )r   resultstringr&   s       r   'last_input_event_was_navigation_commandz7CaretNavigation.last_input_event_was_navigation_command  s     ''t3 \'':+M+MM [55BB4CYCYZ 	 !!-++>>@FF9&AVW]V^_5++S$7r   c                    d}|r|d| z  }t        j                  t         j                  |d       | j                  j                  D ]  }|j                  j                  |d       ! | j                  d      | _        | j                  d      | _        | j                  j                  D ]*  }|j                  j                  || j                          , y)z9Refreshes caret navigation bindings and grabs for script.z/CARET NAVIGATION: Refreshing bindings and grabs: T)includeGrabsN)r   r    r!   r   keyBindingsremover   r   r'   rW   r   )r   scriptreasonr&   bindings        r   refresh_bindings_and_grabsz*CaretNavigation.refresh_bindings_and_grabs&  s     @Rx= C5++S$7~~11 	BG%%gD%A	B **40**40~~11 	NG""7T__9L"M	Nr   c                    |syt        j                         }|j                  d       }|rt        j                  }nt        j
                  }|j                  |       |j                  d|       d| _        | j                  |d       y)zToggles caret navigation.Fr,   Nztoggling caret navigationT)
r
   r9   r:   r   CARET_CONTROL_ORCACARET_CONTROL_APPpresentMessage
setSettingr   rq   )r   rn   event_settings_managerr+   rg   s         r   r   zCaretNavigation.toggle_enabled7  s     ,779'223KLL00F//Ff%$$%=wG!%''0KLr   c                     || j                   k(  ryd| }|r|d| z  }t        j                  t        j                  |d       || _         | j	                  |d|        y)z=Suspends caret navigation independent of the enabled setting.Nz&CARET NAVIGATION: Commands suspended: rj   TzSuspended changed to )r   r   r    r!   rq   )r   rn   	suspendedro   r&   s        r   suspend_commandsz CaretNavigation.suspend_commandsJ  si     '6ykBRx= C5++S$7#''2G	{0STr   c                     |sy|j                   j                         \  }}|sy|| _        |j                   j                  ||       |j	                          |j                  |       |j                  |       y)zMoves to the next character.FT)	utilitiesnextContextr   setCaretPositionpresentationInterruptupdateBraillesayCharacterr   rn   rw   objoffsets        r   r;   zCaretNavigation._next_characterX  sq     &&224V!&))#v6$$&S!C r   c                     |sy|j                   j                         \  }}|sy|| _        |j                   j                  ||       |j	                          |j                  |       |j                  |       y)z Moves to the previous character.FT)r}   previousContextr   r   r   r   r   r   s        r   r=   z#CaretNavigation._previous_characteri  sq     &&668V!&))#v6$$&S!C r   c                    |sy|j                   j                  d      \  }}|j                   j                  ||      }|sy|d   d   |d   d   |d   d   }}}|r|d   j                         r|dz  }|| _        |j                   j                  ||       |j                          |j                  |       |j                  |       y)	zMoves to the next word.FT	skipSpace          r   )	r}   r~   getWordContentsAtOffsetisspacer   r   r   r   sayWord)r   rn   rw   r   r   contentsendrg   s           r   r?   zCaretNavigation._next_wordz  s     &&22T2BV##;;CH#B<?HRLOXb\!_&SfRj((*1HC!&))#s3$$&S!sr   c                 P   |sy|j                   j                  d      \  }}|j                   j                  ||      }|sy|| _        |d   d   |d   d   }}|j                   j	                  ||       |j                          |j                  |       |j                  |       y)zMoves to the previous word.FTr   r   r   )r}   r   r   r   r   r   r   r   )r   rn   rw   r   r   r   starts          r   rA   zCaretNavigation._previous_word  s     &&666FV##;;CH!&a[^Xa[^U))#u5$$&S!sr   c                 X   |sy|j                         rLt        j                         }|j                  d      r'd}t	        j
                  t        j                  |       y|j                  j                         \  }}|j                  j                  ||      }|r|d   sy|j                  j                         }|sy|| _        |d   d   |d   d   }	}|j                  j                  ||	       |j                          |j                  ||d   d          |j                  |       y)	zMoves to the next line.FrewindAndFastForwardInSayAllFCARET NAVIGATION: inSayAll and rewindAndFastforwardInSayAll is enabledTr   r   r   )priorObj)inSayAllr
   r9   r:   r   r    r!   r}   getCaretContextgetLineContentsAtOffsetgetNextLineContentsr   r   r   speakContentsdisplayContents)
r   rn   rw   rx   r&   r   r   liner   r   s
             r   rC   zCaretNavigation._next_line  s    ?? 0 ; ; = ++,JK^""5#3#3S9&&668V77VDa##779!&a[^Xa[^U))#u5$$&XR<x(r   c                    |sy|j                         rLt        j                         }|j                  d      r'd}t	        j
                  t        j                  |       y|j                  j                         }|sy|| _	        |d   d   |d   d   }}|j                  j                  ||       |j                          |j                  |       |j                  |       y)zMoves to the previous line.Fr   r   Tr   r   )r   r
   r9   r:   r   r    r!   r}   getPreviousLineContentsr   r   r   r   r   )r   rn   rw   rx   r&   r   r   r   s           r   rE   zCaretNavigation._previous_line  s     ?? 0 ; ; = ++,JK^""5#3#3S9 ##;;=!&a[^Xa[^U))#u5$$&X&x(r   c                 V   |sy|j                   j                         \  }}|j                   j                  ||      }|r|d   sy|| _        |d   d   |d   d   }}|j                   j	                  ||       |j                          |j                  |       |j                  |       y)zMoves to the start of the line.Fr   r   T)r}   r   r   r   r   r   r   r   )r   rn   rw   r   r   r   r   s          r   rK   zCaretNavigation._start_of_line  s     &&668V77VDa!&!WQZaU))#u5$$&C t$r   c                    |sy|j                   j                         \  }}|j                   j                  ||      }|r|d   sy|d   d   |d   d   |d   d   }}}|j                         r|d   j	                         r|dz  }|| _        |j                   j                  ||       |j                          |j                  |       |j                  |       y)zMoves to the end of the line.Fr   r   r   r   r   T)
r}   r   r   stripr   r   r   r   r   r   )r   rn   rw   r   r   r   r   rg   s           r   rM   zCaretNavigation._end_of_line  s     &&668V77VDa8A;RT"Xa[&S<<>fRj0021HC!&))#s3$$&C t$r   c                    |sy|j                   j                         }|j                   j                  |d      \  }}|j                   j                  ||      }|sy|| _        |d   d   |d   d   }}|j                   j                  ||       |j                          |j                  |       |j                  |       y)zMoves to the start of the file.Fr   r   T)	r}   documentFramefindFirstCaretContextr   r   r   r   r   r   )r   rn   rw   documentr   r   r   s          r   rG   zCaretNavigation._start_of_file  s     ##113&&<<XqIV##;;CH!&qk!nhqk!nV))#v6$$&X&x(r   c                    |sy|j                   j                         }d|g}t        j                  t        j                  |d       |j                   j                  |      }d|d|g}t        j                  t        j                  |d       t        dt        j                  |      dz
        }|r)|j                   j                  ||      \  }}|sn||}}|r)|j                   j                  ||      }	|	sy|| _        |	d   d   |	d   d	   }}|j                   j                  ||       |j                          |j                  |	       |j                  |	       y)
zMoves to the end of the file.FzCARET NAVIGATION: Go to end ofTz CARET NAVIGATION: Last object inisr   r   r   r   )r}   r   r   r_   r!   getLastObjectInDocumentmaxr   get_character_countr~   r   r   r   r   r   r   )
r   rn   rw   r   r`   r   r   lastobj
lastoffsetr   s
             r   rI   zCaretNavigation._end_of_file  sN    ##1132H=%**FD966x@4hcJ%**FD9Q2237!;<"("2"2">">sF"KGZ!:C	  ##;;CH!&rl1ox|AV))#v6$$&X&x(r   N)FT)F) )__name__
__module____qualname____doc__r   r   r'   r   r)   r"   rh   rq   r   r{   r;   r=   r?   rA   rC   rE   rK   rM   rG   rI    r   r   r   r   '   so    G&	
K8Zs:j N"&U"",&<4&,(r   r   N)r   __id____version____date____copyright____license__r   r   r   r   r   r   r	   r
   ax_textr   r   r   r   r   <module>r      sG   * :	6        P Pr   