@@ -36,25 +36,38 @@ def to_rgbint(hex_str):
3636 return rgb (0 , 0 , 0 )
3737
3838
39+ def log (msg ):
40+ with open ("/tmp/code-highlighter.log" , "a" ) as text_file :
41+ text_file .write (str (msg ) + "\r \n \r \n " )
42+
43+
3944def highlightSourceCode (lang , style ):
4045 ctx = XSCRIPTCONTEXT
4146 doc = ctx .getDocument ()
4247 # Get the selected item
4348 selected_item = doc .getCurrentController ().getSelection ()
44- for item_idx in range (selected_item .getCount ()):
45- code_block = selected_item .getByIndex (item_idx )
46- if 'com.sun.star.drawing.Text' in code_block .SupportedServiceNames :
47- # TextBox
48- # highlight_code(style, lang, code_block)
49- code = code_block .String
50- cursor = code_block .createTextCursor ()
51- cursor .gotoStart (False )
52- else :
53- # Plain text
54- # highlight_code_string(style, lang, code_block)
55- code = code_block .getString ()
56- cursor = code_block .getText ().createTextCursorByRange (code_block )
57- cursor .goLeft (0 , False )
49+ if hasattr (selected_item , 'getCount' ):
50+ for item_idx in range (selected_item .getCount ()):
51+ code_block = selected_item .getByIndex (item_idx )
52+ if 'com.sun.star.drawing.Text' in code_block .SupportedServiceNames :
53+ # TextBox
54+ # highlight_code(style, lang, code_block)
55+ code = code_block .String
56+ cursor = code_block .createTextCursor ()
57+ cursor .gotoStart (False )
58+ else :
59+ # Plain text
60+ # highlight_code_string(style, lang, code_block)
61+ code = code_block .getString ()
62+ cursor = code_block .getText ().createTextCursorByRange (code_block )
63+ cursor .goLeft (0 , False )
64+ highlight_code (code , cursor , lang , style )
65+ elif hasattr (selected_item , 'SupportedServiceNames' ) and 'com.sun.star.text.TextCursor' in selected_item .SupportedServiceNames :
66+ # LO Impress text selection
67+ code_block = selected_item
68+ code = code_block .getString ()
69+ cursor = code_block .getText ().createTextCursorByRange (code_block )
70+ cursor .goLeft (0 , False )
5871 highlight_code (code , cursor , lang , style )
5972
6073
@@ -74,9 +87,11 @@ def highlight_code(code, cursor, lang, style):
7487 finally :
7588 cursor .goRight (0 , False ) # deselects the selected text
7689
90+
7791def highlight_automatic_default (* args ):
7892 highlightSourceCode (None , 'default' )
7993
94+
8095def highlight_abap_default (* args ):
8196 highlightSourceCode ('abap' , 'default' )
8297
@@ -252,9 +267,11 @@ def highlight_c_objdump_default(*args):
252267def highlight_c_default (* args ):
253268 highlightSourceCode ('c' , 'default' )
254269
270+
255271def highlight_cpp_default (* args ):
256272 highlightSourceCode ('cpp' , 'default' )
257273
274+
258275def highlight_ca65_default (* args ):
259276 highlightSourceCode ('ca65' , 'default' )
260277
0 commit comments