łň
Š_Fc           @   s    d  Z  d e f d     YZ d S(   sî   
This module documents a class that implements paragraph operations.

@author: Lateef Alabi-Oki
@organization: The Scribes Project
@copyright: Copyright ÂŠ 2007 Lateef Alabi-Oki
@license: GNU GPLv2 or Later
@contact: mystilleef@gmail.com
t   Managerc           B   s×   e  Z d  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z	 d   Z
 d	   Z e d
  Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z RS(   s/   
	This class implements paragraph operations.
	c         C   s:   |  i  |  d d k l } l } | |  i d | d S(   s˝   
		Initialize object.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param editor: Reference to the text editor.
		@type editor: An Editor object.
		i˙˙˙˙(   t   idle_addt   PRIORITY_LOWt   priorityN(   t   _Manager__init_attributest   gobjectR   R   t   _Manager__precompile_methods(   t   selft   editorR   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __init__$   s    
c         C   s1   | |  _  | i |  _ | i |  _ | i |  _ d S(   s˝   
		Initialize object.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param editor: Reference to the text editor.
		@type editor: An Editor object.
		N(   t   _Manager__editort   gconf_clientt   _Manager__clientt
   textbuffert   _Manager__buffert   feedbackt   _Manager__feedback(   R   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __init_attributes2   s
    
	c      	   C   s
  |  i  i   } | i   } d d k l } l } yz | | d  o
 t  n |  i |  } | | d	  o
 t	  n |  i
 |  d d k l } |  i i | d d  WnX t j
 o- d d k l } |  i i | d d  n! t	 j
 o |  i
 d  n Xd	 S(
   s{   
		Move cursor to previous paragraph.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		i˙˙˙˙(   t   eqt   is_i    (   t   msg0003t   yesi   (   t   msg0004t   warningN(   R
   t   get_cursor_iteratort   get_linet   operatorR   R   t   RuntimeErrort   _Manager__find_empty_linet   Nonet
   ValueErrort   _Manager__jump_to_linet   i18nR   R   t   update_status_messageR   (   R   t   cursor_iteratort   current_lineR   R   t   lineR   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   previous_paragraphH   s     c   	   	   C   s$  |  i  i   } |  i i   } | i   } d d k l } l } y | | | d  o
 t  n |  i	 | t
  } | | d	  o
 t  n |  i |  d d k l } |  i i | d d  Wn\ t j
 o- d d k l } |  i i | d d  n% t j
 o |  i | d  n Xd	 S(
   sw   
		Move cursor to next paragraph.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		i˙˙˙˙(   R   R   i   (   t   msg0005R   i   (   t   msg0006R   N(   R   t   get_line_countR
   R   R   R   R   R   R   R   t   FalseR   R   R   R    R&   R   R!   R'   (	   R   t   number_of_linesR"   R#   R   R   R$   R&   R'   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   next_paragraph`   s"    c         C   s   yO |  i    \ } } |  i i | |  d d k l } |  i i | d d  Wn9 t j
 o- d d k l } |  i i | d d  n Xd S(   sl   
		Select a paragraph.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		i˙˙˙˙(   t   msg0007R   i   (   t   msg0001R   N(	   t    _Manager__get_paragraph_positionR   t   select_rangeR    R,   R   R!   R   R-   (   R   t   begint   endR,   R-   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   select_paragraphy   s    c         C   s:  |  i    o d Sn y |  i   \ } } Wn= t j
 o1 d d k l } |  i i | d d  d Sn X| i |  } y |  i |  } Wn= t j
 o1 d d k l	 } |  i i | d d  d Sn X|  i
 i   |  i
 i | |  |  i
 i |  |  i
 i   d d k l } | } |  i i | d d  d S(	   s   
		Reflow paragraph based on right margin position.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		Ni˙˙˙˙(   R-   R   i   (   t   msg0002(   t   msg0009R   (   t   _Manager__is_readonlyR.   R   R    R-   R   R!   t   get_textt   _Manager__reflow_textR3   R   t   begin_user_actiont   deletet   insert_at_cursort   end_user_actionR4   (   R   t   startR1   R-   t   textR3   R4   t   message(    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   reflow_paragraph   s,    		c         C   s   |  i  i |   ~  d }  d S(   sh   
		Destroy object.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		N(   R
   t   delete_attributesR   (   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   destroy¨   s    c         C   s  |  i    } | i   o | i   o
 t  n |  i |  o
 t  n |  i |  } | d j o |  i i   \ } } n |  i i	 |  } | i
   |  i | t  } | d j o |  i i   \ } } n' |  i i	 |  } | i   | i   | | f S(   sÂ   
		Get start and end paragraph positions.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@return: The position of a paragraph.
		@rtype: A Tuple object.
		N(   t#   _Manager__get_current_line_iteratort   is_startt   is_endR   t   _Manager__is_empty_lineR   R   R   t
   get_boundst   get_iter_at_linet   forward_lineR)   t   backward_linet   forward_to_line_end(   R   t   iteratort   first_paragraph_lineR0   R1   t   second_paragraph_lineR<   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __get_paragraph_positionş   s    
$


c         C   s@   |  i  i   } | i   o | Sn | i   } |  i i |  S(   sÚ   
		Get iterator at start of current line.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@return: Return the iterator at start of current line.
		@rtype: A gtk.TextIter object.
		(   R
   R   t   starts_lineR   R   RG   (   R   RK   R$   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __get_current_line_iteratorÖ   s    
c         C   s   d d k  l } xk t oc | o | | i    o d Sq[ n | | i    o d Sn |  i |  o | i   Sq q Wd S(   s  
		Search backwards for empty lines.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param iterator: The cursor position
		@type iterator: A gtk.TextIter object.

		@return: Line number of empty line or None
		@rtype: An Integer or None object.
		i˙˙˙˙(   t   not_N(   R   RQ   t   TrueRI   R   RH   RE   R   (   R   RK   t	   backwardsRQ   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __find_empty_lineĺ   s    
#c         C   sx   | i    o t Sn | i   } | i   | i |  } d d k l } | |  o t Sn | i   o t Sn t S(   s  
		Whether or not a line is empty.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param iterator: Reference a position in the buffer.
		@type iterator: A gtk.TextIter object.

		@return: True if line is empty.
		@rtype: A Boolean object.
		i˙˙˙˙(   RQ   (	   t	   ends_lineRR   t   copyRJ   R6   R   RQ   t   isspaceR)   (   R   RK   t   temporary_iteratorR=   RQ   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __is_empty_lineű   s    
c         C   sF   |  i  i |  } |  i  i |  d d k l } | |  i i  d S(   sş   
		Move cursor to a specific line.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param line: Line to jump to.
		@type line: An Integer object.
		i˙˙˙˙(   t   move_view_to_cursorN(   R   RG   t   place_cursort   SCRIBES.cursorRZ   R
   t   textview(   R   R$   RK   RZ   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __jump_to_line  s
    
c         C   sr  d d k  l } l } l } | |  p | i   o
 t  n | i d  } |  i | d  } |  i |  } g  } d }	 d i	 |  }
 |
 i
 d d  }
 |
 i   }
 |  i |
  }
 | t |
  |  o_ xi t oP |  i |
 |  \ } }	 | i |  | t |	  |  o Pn |	 i   }
 qŇ Wn | i |
  |	 o | i |	  n |  i | |  } d i	 |  S(   s%  
		Realign lines in text such that is line is not more than a
		specified character long.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param text: Text to reflow.
		@type text: A String object.

		@return: Realigned text.
		@rtype: A String object.
		i˙˙˙˙(   RQ   t   gtt   les   
i    t    t    s   	(   R   RQ   R_   R`   RW   R   t   splitt   _Manager__get_indentationt   _Manager__calculate_wrap_widtht   joint   replacet   stript   _Manager__respace_linet   lenRR   t   _Manager__shorten_linet   appendt   _Manager__indent_lines(   R   R=   RQ   R_   R`   t
   text_linest   indentationt
   wrap_widtht   reflowed_linest	   remainderR$   t   new_linet   indented_reflowed_lines(    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __reflow_text'  s*    $
c         C   sj   d d k  l } | i   } | | | d t d t } | d } d i | d  } | i   | i   f S(   s  
		Shorten a line to a specified wrap width.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param line: Line to shorten.
		@type line: A String object.

		@param wrap_width: How long the line should be.
		@type wrap_width: An Integer object.

		@return: The shortened line and other strings that exceed the
			specified wrap width.
		@rtype: A Tuple object.
		i˙˙˙˙(   t   wrapt   expand_tabst   replace_whitespacei    Rb   i   (   t   textwrapRv   Rh   R)   Rf   (   R   R$   Rp   Rv   t	   new_linesRs   Rr   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __shorten_lineL  s    
c            sP   d d k  l } | t |  d  o | Sn   f d   } t | |  } | S(   s}  
		Perform automatic indentation on each line, if necessary.

		Automatic indentation is based on the indentation of the first
		line.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param reflowed_lines: Realigned lines for indentation.
		@type reflowed_lines: A List object.

		@return: A list of indented lines.
		@rtype: A List object.
		i˙˙˙˙(   t   lti   c            s     |  i    S(    (   Rh   (   t   x(   Ro   (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   <lambda>v  s    (   R   R|   Rj   t   map(   R   Rq   Ro   R|   t   indent_linet   indented_lines(    (   Ro   s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __indent_linesd  s
    c         C   sh   g  } d d k  l } l } x< | D]4 } | | d d g |   o Pn | i |  q# Wd i |  S(   sÍ   
		Determine the indentation of a line.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param line: A line to check for indentation.
		@type line: A String object.
		i˙˙˙˙(   t   containsRQ   Rb   s   	Ra   (   R   R   RQ   Rl   Rf   (   R   R$   t   indentation_listR   RQ   t   char(    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __get_indentationz  s    
 !c         C   sS   | i  d  } x4 t o, y | i d  Wq t j
 o Pq Xq Wd i |  S(   s  
		Remove duplicate spaces in a line.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param line: A line to remove duplicate spaces from.
		@type line: A String object.

		@return: A new line with single spaces.
		@rtype: A String object.
		Rb   Ra   (   Rc   RR   t   removeR   Rf   (   R   R$   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __respace_line  s    
c         C   sw   |  i    } d d k l } l } | |  o | Sn |  i   } | i d  } | i d  } | | | | } | S(   sP  
		Determine wrap width.

		This function calculates the wrap width paying attention to
		automatic indentation.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@param indentation: An indentation strings.
		@type indentation: A String object.

		@return: wrap width.
		@rtype: An Integer object.
		i˙˙˙˙(   RQ   t   nes   	Rb   (   t    _Manager__get_right_margin_widthR   RQ   R   t   _Manager__get_tab_widtht   count(   R   Ro   t   widthRQ   R   t	   tab_widtht   number_of_tab_charst   number_of_space_chars(    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __calculate_wrap_width   s    c         C   s3   d } |  i  i d  o |  i  i d  } n | S(   sŚ   
		Get tab width from GConf.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@return: Tab width.
		@rtype: An Integer object.
		i   s   /apps/scribes/tab(   R   t   gett   get_int(   R   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __get_tab_widthš  s    
c         C   sX   d d k  l } | |  i i  o t Sn d d k l } |  i i i | d d  t	 S(   sÍ   
		Check if editor is in readonly mode.
		
		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		
		@return: True if editor is in readonly mode.
		@rtype: A Boolean object.
		i˙˙˙˙(   RQ   (   t   msg0010R   i   (
   R   RQ   R
   t   is_readonlyR)   R    R   R   R!   RR   (   R   RQ   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __is_readonlyČ  s
    
c         C   s3   d } |  i  i d  o |  i  i d  } n | S(   sš   
		Get margin or wrap width from Gconf.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.

		@return: Margin/wrap width.
		@rtype: An Integer object.
		iH   s   /apps/scribes/margin_position(   R   R   R   (   R   t   margin_position(    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __get_right_margin_widthŘ  s    
c         C   sä   yĘ d d k  l } | |  i  | |  i  | |  i  | |  i  | |  i  | |  i  | |  i  | |  i	  | |  i
  | |  i  | |  i  | |  i  | |  i  | |  i  Wn t j
 o n Xt S(   sv   
		Optimize methods using Psyco.

		@param self: Reference to the Manager instance.
		@type self: A Manager object.
		i˙˙˙˙(   t   bind(   t   psycoR   R?   R+   R%   R2   R7   Rk   Ri   Rm   Re   R.   RB   R   RE   R   t   ImportErrorR)   (   R   R   (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyt   __precompile_methodsç  s&    (   t   __name__t
   __module__t   __doc__R	   R   R%   R+   R2   R?   RA   R.   RB   RR   R   RE   R   R7   Rk   Rm   Rd   Ri   Re   R   R5   R   R   (    (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pyR       s.   												%								N(   R    t   objectR    (    (    (    s=   /home/goldenmyst/.gnome2/scribes/plugins/Paragraph/Manager.pys   <module>   s   