-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctrl_b_i_u.py
75 lines (62 loc) · 2.07 KB
/
ctrl_b_i_u.py
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
"""
Commands to go with keybindings in @settings-->@keys-->@shortcuts
to implement Ctrl-B,I,U Bold Italic Underline markup in plain text.
RST flavored, could be made language aware.
Key bindings would be something like:
markup_inline_bold ! body = Ctrl-B
markup_inline_italic ! body = Ctrl-I
markup_inline_underline ! body = Ctrl-U
"""
def markup_inline(kw, kind='unknown'):
c = kw['c']
# find out if last action was open or close, creates entry if needed
last_type = c.user_dict.setdefault(
'markup_inline', {'last': 'close'})['last']
p = c.p
delim = {
'bold': ('**','**'),
'italic': ('*','*'),
'underline': (':ul:`','`'),
}[kind]
if c.frame.body.bodyCtrl.hasSelection():
c.user_dict['markup_inline']['last'] = 'close'
i,j = c.frame.body.bodyCtrl.getSelectionRange()
txt = c.frame.body.bodyCtrl.getAllText()
p.b = "".join([
txt[:i],
delim[0],
txt[i:j],
delim[1],
txt[j:],
])
c.frame.body.bodyCtrl.setAllText(p.b)
c.frame.body.bodyCtrl.setInsertPoint(j+len(delim[0])+len(delim[1]))
else:
i = c.frame.body.bodyCtrl.getInsertPoint()
txt = c.frame.body.bodyCtrl.getAllText()
if last_type == 'close':
delim = delim[0]
c.user_dict['markup_inline']['last'] = 'open'
else:
delim = delim[1]
c.user_dict['markup_inline']['last'] = 'close'
p.b = "".join([
txt[:i],
delim,
txt[i:]
])
c.frame.body.bodyCtrl.setAllText(p.b)
c.frame.body.bodyCtrl.setInsertPoint(i+len(delim))
c.setChanged(True)
p.setDirty(True)
c.redraw()
c.bodyWantsFocusNow()
@g.command('markup_inline_bold')
def markup_inline_bold(kw):
markup_inline(kw, kind='bold')
@g.command('markup_inline_italic')
def markup_inline_italic(kw):
markup_inline(kw, kind='italic')
@g.command('markup_inline_underline')
def markup_inline_underline(kw):
markup_inline(kw, kind='underline')