/usr/share/cherrytree/modules/lists.py is in cherrytree 0.32.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | # -*- coding: UTF-8 -*-
#
# lists.py
#
# Copyright 2009-2014 Giuseppe Penone <giuspen@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
import gtk
import re
import cons
class ListsHandler:
"""Handler of Bulleted and Numbered Lists"""
def __init__(self, dad):
"""Lists Handler boot"""
self.dad = dad
def list_todo_handler(self):
"""Handler of the ToDo List"""
if self.dad.curr_buffer.get_has_selection():
iter_start, sel_end = self.dad.curr_buffer.get_selection_bounds()
end_offset = sel_end.get_offset() - 1
else:
end_offset = 0
iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
# get info about the paragraph starting with iter_start ([Number, Whether multiple line, List Start Iter Offset])
list_info = self.get_paragraph_list_info(iter_start)
if list_info[0] == None:
# this is not a list
first_iteration = True
while first_iteration or new_par_offset < end_offset:
first_iteration = False
iter_start, iter_end = self.get_paragraph_iters(iter_start)
if not iter_start:
# it's an empty paragraph
iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
self.dad.curr_buffer.insert(iter_start, cons.CHAR_LISTTODO + cons.CHAR_SPACE)
break
elif self.is_list_indented_continuation(iter_start):
new_par_offset = iter_end.get_offset() + 1
else:
new_par_offset = iter_end.get_offset() + 2 + 1
self.dad.curr_buffer.insert(iter_start, cons.CHAR_LISTTODO + cons.CHAR_SPACE)
end_offset += 2
iter_start = self.dad.curr_buffer.get_iter_at_offset(new_par_offset)
if not iter_start: break
elif list_info[0] == 0:
# this is a bulleted list paragraph and we turn it into todo list
self.list_adjust_ahead(None, list_info[2]-1, "bul2tod")
elif list_info[0] == -1:
# this is a todo list and we turn it into normal text
first_iteration = True
while first_iteration or new_par_offset < end_offset:
first_iteration = False
iter_start, iter_end = self.get_paragraph_iters(iter_start)
if self.is_list_indented_continuation(iter_start):
new_par_offset = iter_end.get_offset() + 1
else:
new_par_offset = iter_end.get_offset() - 2 + 1
iter_end_deletion = iter_start.copy()
iter_end_deletion.forward_chars(2)
self.dad.curr_buffer.delete(iter_start, iter_end_deletion)
end_offset -= 2
iter_start = self.dad.curr_buffer.get_iter_at_offset(new_par_offset)
if not iter_start: break
else:
# this is a numbered list and we turn it into todo list
self.list_adjust_ahead(None, list_info[2]-1, "num2tod")
def list_bulleted_handler(self):
"""Handler of the Bulleted List"""
if self.dad.curr_buffer.get_has_selection():
iter_start, sel_end = self.dad.curr_buffer.get_selection_bounds()
end_offset = sel_end.get_offset() - 1
else:
end_offset = 0
iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
# get info about the paragraph starting with iter_start ([Number, Whether multiple line, List Start Iter Offset])
list_info = self.get_paragraph_list_info(iter_start)
if list_info[0] == None:
# this is not a list
first_iteration = True
while first_iteration or new_par_offset < end_offset:
first_iteration = False
iter_start, iter_end = self.get_paragraph_iters(iter_start)
if not iter_start:
# it's an empty paragraph
iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
self.dad.curr_buffer.insert(iter_start, cons.CHAR_LISTBUL + cons.CHAR_SPACE)
break
elif self.is_list_indented_continuation(iter_start):
new_par_offset = iter_end.get_offset() + 1
else:
new_par_offset = iter_end.get_offset() + 2 + 1
self.dad.curr_buffer.insert(iter_start, cons.CHAR_LISTBUL + cons.CHAR_SPACE)
end_offset += 2
iter_start = self.dad.curr_buffer.get_iter_at_offset(new_par_offset)
if not iter_start: break
elif list_info[0] == 0:
# this is a bulleted list paragraph and we turn it into normal text
first_iteration = True
while first_iteration or new_par_offset < end_offset:
first_iteration = False
iter_start, iter_end = self.get_paragraph_iters(iter_start)
if self.is_list_indented_continuation(iter_start):
new_par_offset = iter_end.get_offset() + 1
else:
new_par_offset = iter_end.get_offset() -2 + 1
iter_end_deletion = iter_start.copy()
iter_end_deletion.forward_chars(2)
self.dad.curr_buffer.delete(iter_start, iter_end_deletion)
end_offset -= 2
iter_start = self.dad.curr_buffer.get_iter_at_offset(new_par_offset)
if not iter_start: break
elif list_info[0] == -1:
# this is a todo list and we turn it into bulleted list
self.list_adjust_ahead(None, list_info[2]-1, "tod2bul")
else:
# this is a numbered list and we turn it into bulleted list
self.list_adjust_ahead(None, list_info[2]-1, "num2bul")
def list_numbered_handler(self):
"""Handler of the Numbered List"""
if self.dad.curr_buffer.get_has_selection():
iter_start, sel_end = self.dad.curr_buffer.get_selection_bounds()
end_offset = sel_end.get_offset() - 1
else:
end_offset = 0
iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
# get info about the paragraph starting with iter_start ([Number, Whether multiple line, List Start Iter Offset])
list_info = self.get_paragraph_list_info(iter_start)
if list_info[0] == None:
# this is not a list
leading_num_count = 0
while leading_num_count == 0 or new_par_offset < end_offset:
leading_num_count += 1
iter_start, iter_end = self.get_paragraph_iters(iter_start)
if not iter_start:
# it's an empty paragraph
iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
self.dad.curr_buffer.insert(iter_start, "1. ")
break
elif self.is_list_indented_continuation(iter_start):
new_par_offset = iter_end.get_offset() + 1
else:
leading_str = "%s. " % leading_num_count
new_par_offset = iter_end.get_offset() + len(leading_str) + 1
self.dad.curr_buffer.insert(iter_start, leading_str)
end_offset += len(leading_str)
iter_start = self.dad.curr_buffer.get_iter_at_offset(new_par_offset)
if not iter_start: break
elif list_info[0] == 0:
# this is a bulleted list and we turn it into numbered list
self.list_adjust_ahead(0, list_info[2]-1, "bul2num")
elif list_info[0] == -1:
# this is a todo list and we turn it into numbered list
self.list_adjust_ahead(0, list_info[2]-1, "tod2num")
else:
# this is a numbered list paragraph and we turn it into normal text
first_iteration = True
while first_iteration or new_par_offset < end_offset:
first_iteration = False
iter_start, iter_end = self.get_paragraph_iters(iter_start)
leading_str = "%s. " % list_info[0]
if self.is_list_indented_continuation(iter_start):
new_par_offset = iter_end.get_offset() + 1
else:
new_par_offset = iter_end.get_offset() -len(leading_str) + 1
iter_end_deletion = iter_start.copy()
iter_end_deletion.forward_chars(len(leading_str))
self.dad.curr_buffer.delete(iter_start, iter_end_deletion)
end_offset -= len(leading_str)
iter_start = self.dad.curr_buffer.get_iter_at_offset(new_par_offset)
if not iter_start: break
def list_adjust_ahead(self, curr_num, offset_start, adj_type):
"""Adjust the Following Numbering"""
if offset_start >= 0:
iter_start = self.dad.curr_buffer.get_iter_at_offset(offset_start)
while iter_start.get_char() != cons.CHAR_NEWLINE:
if not iter_start.forward_char(): return # end of buffer
if not iter_start.forward_char(): return
else: iter_start = self.dad.curr_buffer.get_iter_at_offset(0)
# we're at the beginning of a list item or subsequent indented line of a list item
if iter_start.get_char() == cons.CHAR_SPACE:
if iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE\
and iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE:
self.list_adjust_ahead(curr_num, iter_start.get_offset(), adj_type) # go on searching
else: return # not an indentation
elif adj_type[0:3] == "num":
match = re.match('[1-9]', iter_start.get_char())
if not match: return
iter_end = iter_start.copy()
while iter_end.forward_char() and re.match('[0-9]', iter_end.get_char()): pass
if iter_end.get_char() != ".": return # something's wrong
self.list_adjust_ahead_write_in(iter_start, iter_end, curr_num, adj_type)
elif adj_type[0:3] == "bul":
if iter_start.get_char() != cons.CHAR_LISTBUL: return
iter_end = iter_start.copy()
self.list_adjust_ahead_write_in(iter_start, iter_end, curr_num, adj_type)
elif adj_type[0:3] == "tod":
if not iter_start.get_char() in [cons.CHAR_LISTTODO, cons.CHAR_LISTDONEOK, cons.CHAR_LISTDONEFAIL]: return
iter_end = iter_start.copy()
self.list_adjust_ahead_write_in(iter_start, iter_end, curr_num, adj_type)
else: print "bad adj_type", adj_type
def list_adjust_ahead_write_in(self, iter_start, iter_end, curr_num, adj_type):
"""Write a Replacement of List Point"""
iter_end.forward_char()
self.dad.curr_buffer.delete(iter_start, iter_end)
if adj_type[-3:] == "num":
curr_num += 1
self.dad.curr_buffer.insert(iter_start, '%s.' % curr_num)
self.list_adjust_ahead(curr_num, iter_start.get_offset(), adj_type)
elif adj_type[-3:] == "bul":
self.dad.curr_buffer.insert(iter_start, cons.CHAR_LISTBUL)
self.list_adjust_ahead(None, iter_start.get_offset(), adj_type)
elif adj_type[-3:] == "tod":
self.dad.curr_buffer.insert(iter_start, cons.CHAR_LISTTODO)
self.list_adjust_ahead(None, iter_start.get_offset(), adj_type)
else: print "bad adj_type", adj_type
def list_get_number(self, iter_first_paragraph):
"""Returns a Number or None (0 fot the bulleted list)"""
iter_start = iter_first_paragraph.copy()
char = iter_start.get_char()
if char == cons.CHAR_LISTBUL:
return 0 if iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE else None
if char in [cons.CHAR_LISTTODO, cons.CHAR_LISTDONEOK, cons.CHAR_LISTDONEFAIL]:
return -1 if iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE else None
match = re.match('[1-9]', char)
if not match: return None
number_str = char
while iter_start.forward_char() and re.match('[0-9]', iter_start.get_char()):
number_str += iter_start.get_char()
return int(number_str) if iter_start.get_char() == "." and iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE else None
def get_paragraph_list_info(self, iter_start):
"""Returns [Number, Whether multiple line, List Start Iter Offset]
Number == 0 if bulleted list, >=1 if numbered list or None if not a list"""
buffer_start = False
# let's search for the paragraph start
if iter_start.get_char() == cons.CHAR_NEWLINE:
if not iter_start.backward_char(): buffer_start = True # if we are exactly on the paragraph end
if not buffer_start:
while iter_start:
if iter_start.get_char() == cons.CHAR_NEWLINE: break # we got the previous paragraph start
elif not iter_start.backward_char():
buffer_start = True
break # we reached the buffer start
if not buffer_start: iter_start.forward_char()
# get the number of the paragraph starting with iter_start
number = self.list_get_number(iter_start)
if number != None: return [number, False, iter_start.get_offset()] # multiple line = False
elif not buffer_start and iter_start.get_char() == cons.CHAR_SPACE:
if iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE\
and iter_start.forward_char() and iter_start.get_char() == cons.CHAR_SPACE:
# we are inside of a list paragraph but after a shift+return
iter_start.backward_chars(3)
list_info = self.get_paragraph_list_info(iter_start)
return [list_info[0], True, list_info[2]] # multiple line = True
return [None, None, None] # this paragraph is not a list
def get_paragraph_iters(self, force_iter=None):
"""Generates and Returns two iters indicating the paragraph bounds"""
if not force_iter and self.dad.curr_buffer.get_has_selection():
iter_start, iter_end = self.dad.curr_buffer.get_selection_bounds() # there's a selection
else:
# There's not a selection/iter forced
if not force_iter: iter_start = self.dad.curr_buffer.get_iter_at_mark(self.dad.curr_buffer.get_insert())
else: iter_start = force_iter.copy()
iter_end = iter_start.copy()
if iter_start.get_char() == cons.CHAR_NEWLINE:
# we're upon a row end
if not iter_start.backward_char(): return (None, None)
if iter_start.get_char() == cons.CHAR_NEWLINE: return (None, None)
while iter_end != None:
char = iter_end.get_char()
if char == cons.CHAR_NEWLINE: break # we got it
elif not iter_end.forward_char(): break # we reached the buffer end
while iter_start != None:
char = iter_start.get_char()
if char == cons.CHAR_NEWLINE: # we got it
iter_start.forward_char() # step forward to the beginning of the new line
break
elif not iter_start.backward_char(): break # we reached the buffer start
return (iter_start, iter_end)
def is_list_indented_continuation(self, iter_in):
"""The given iter is the beginning of an indented list item"""
if not iter_in: return False
iter_start = iter_in.copy()
if iter_start.get_char() != cons.CHAR_SPACE: return False
if not iter_start.forward_char() or iter_start.get_char() != cons.CHAR_SPACE: return False
if not iter_start.forward_char() or iter_start.get_char() != cons.CHAR_SPACE: return False
return True
def is_list_todo_beginning(self, square_bracket_open_iter):
"""Check if ☐ or ☑ or ☒"""
text_iter = square_bracket_open_iter.copy()
if text_iter.backward_char():
if text_iter.get_char() == cons.CHAR_NEWLINE:
text_iter.forward_char()
else: return False
if text_iter.get_char() in [cons.CHAR_LISTTODO, cons.CHAR_LISTDONEOK, cons.CHAR_LISTDONEFAIL]:
return True
return False
def todo_list_rotate_status(self, todo_char_iter):
"""Rotate status between ☐ and ☑ and ☒"""
iter_offset = todo_char_iter.get_offset()
if todo_char_iter.get_char() == cons.CHAR_LISTTODO:
self.dad.curr_buffer.delete(todo_char_iter, self.dad.curr_buffer.get_iter_at_offset(iter_offset+1))
self.dad.curr_buffer.insert(self.dad.curr_buffer.get_iter_at_offset(iter_offset), cons.CHAR_LISTDONEOK)
elif todo_char_iter.get_char() == cons.CHAR_LISTDONEOK:
self.dad.curr_buffer.delete(todo_char_iter, self.dad.curr_buffer.get_iter_at_offset(iter_offset+1))
self.dad.curr_buffer.insert(self.dad.curr_buffer.get_iter_at_offset(iter_offset), cons.CHAR_LISTDONEFAIL)
elif todo_char_iter.get_char() == cons.CHAR_LISTDONEFAIL:
self.dad.curr_buffer.delete(todo_char_iter, self.dad.curr_buffer.get_iter_at_offset(iter_offset+1))
self.dad.curr_buffer.insert(self.dad.curr_buffer.get_iter_at_offset(iter_offset), cons.CHAR_LISTTODO)
def char_iter_forward_to_newline(self, char_iter):
"""Forwards char iter to line end"""
if not char_iter.forward_char(): return False
while char_iter.get_char() != cons.CHAR_NEWLINE:
if not char_iter.forward_char(): return False
return True
def todo_lists_old_to_new_conversion(self, text_buffer):
"""Conversion of todo lists from old to new type for a node"""
curr_iter = text_buffer.get_start_iter()
keep_cleaning = False
first_line = True
while curr_iter:
fw_needed = True
if first_line or curr_iter.get_char() == cons.CHAR_NEWLINE and curr_iter.forward_char():
first_line = False
if keep_cleaning:
iter_bis = curr_iter.copy()
if iter_bis.get_char() == cons.CHAR_SPACE and iter_bis.forward_char()\
and iter_bis.get_char() == cons.CHAR_SPACE and iter_bis.forward_char()\
and iter_bis.get_char() == cons.CHAR_SPACE:
no_stop = self.char_iter_forward_to_newline(curr_iter)
text_buffer.remove_all_tags(iter_bis, curr_iter)
if no_stop: continue
else: break
else: keep_cleaning = False
if curr_iter.get_char() == cons.CHAR_SQ_BR_OPEN and curr_iter.forward_char()\
and curr_iter.get_char() in [cons.CHAR_SPACE, "X"]:
middle_char = curr_iter.get_char()
if curr_iter.forward_char() and curr_iter.get_char() == cons.CHAR_SQ_BR_CLOSE\
and curr_iter.forward_char():
first_iter = curr_iter.copy()
first_iter.backward_chars(3)
iter_offset = first_iter.get_offset()
text_buffer.delete(first_iter, curr_iter)
todo_char = cons.CHAR_LISTTODO if middle_char == cons.CHAR_SPACE else cons.CHAR_LISTDONEOK
text_buffer.insert(text_buffer.get_iter_at_offset(iter_offset), todo_char)
curr_iter = text_buffer.get_iter_at_offset(iter_offset)
if middle_char != cons.CHAR_SPACE:
first_iter = curr_iter.copy()
no_stop = self.char_iter_forward_to_newline(curr_iter)
#print "%s(%s),%s(%s)" % (first_iter.get_char(), first_iter.get_offset(), curr_iter.get_char(), curr_iter.get_offset())
text_buffer.remove_all_tags(first_iter, curr_iter)
keep_cleaning = True
if no_stop: continue
else: break
else: fw_needed = False
if fw_needed and not self.char_iter_forward_to_newline(curr_iter): break
if self.dad.enable_spell_check: self.dad.spell_check_set_on()
|