1
# -*- coding: utf-8 -*-
3
# format.py - format commander module
5
# Copyright (C) 2010 - Jesse van den Kieboom
7
# This program is free software; you can redistribute it and/or modify
8
# it under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 2 of the License, or
10
# (at your option) any later version.
12
# This program is distributed in the hope that it will be useful,
13
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
17
# You should have received a copy of the GNU General Public License
18
# along with this program; if not, write to the Free Software
19
# Foundation, Inc., 59 Temple Place, Suite 330,
20
# Boston, MA 02111-1307, USA.
1
22
import commander.commands as commands
3
24
__commander_module__ = True
5
26
def remove_trailing_spaces(view, all=False):
6
"""Remove trailing spaces: format.remove-trailing-spaces [<all>]
27
"""Remove trailing spaces: format.remove-trailing-spaces [<all>]
8
29
Remove trailing spaces in the selection. If there is no selection, trailing
9
30
spaces are removed from the whole document. When the optional argument
10
31
<all> is specified, trailing spaces will be removed from all
11
32
the open documents."""
14
buffers = view.get_toplevel().get_documents()
16
buffers = [view.get_buffer()]
19
bounds = buf.get_selection_bounds()
22
bounds = buf.get_bounds()
24
buf.begin_user_action()
27
# For each line, remove trailing spaces
28
if not bounds[1].ends_line():
29
bounds[1].forward_to_line_end()
31
until = buf.create_mark(None, bounds[1], False)
33
start.set_line_offset(0)
35
while start.compare(buf.get_iter_at_mark(until)) < 0:
37
end.forward_to_line_end()
40
if end.equal(buf.get_end_iter()):
43
while end.get_char().isspace() and end.compare(start) > 0:
46
if not end.ends_line():
47
if not end.get_char().isspace():
58
buf.delete_mark(until)
61
return commands.result.HIDE
35
buffers = view.get_toplevel().get_documents()
37
buffers = [view.get_buffer()]
40
bounds = buf.get_selection_bounds()
43
bounds = buf.get_bounds()
45
buf.begin_user_action()
48
# For each line, remove trailing spaces
49
if not bounds[1].ends_line():
50
bounds[1].forward_to_line_end()
52
until = buf.create_mark(None, bounds[1], False)
54
start.set_line_offset(0)
56
while start.compare(buf.get_iter_at_mark(until)) < 0:
58
end.forward_to_line_end()
61
if end.equal(buf.get_end_iter()):
64
while end.get_char().isspace() and end.compare(start) > 0:
67
if not end.ends_line():
68
if not end.get_char().isspace():
79
buf.delete_mark(until)
82
return commands.result.HIDE
63
84
def _transform(view, how, all):
65
buffers = view.get_toplevel().get_documents()
67
buffers = [view.get_buffer()]
70
bounds = buf.get_selection_bounds()
73
start = buf.get_iter_at_mark(buf.get_insert())
76
if not end.ends_line():
77
end.forward_to_line_end()
81
if not bounds[0].equal(bounds[1]):
82
text = how(bounds[0].get_text(bounds[1]))
84
buf.begin_user_action()
85
buf.delete(bounds[0], bounds[1])
86
buf.insert(bounds[0], text)
89
return commands.result.HIDE
86
buffers = view.get_toplevel().get_documents()
88
buffers = [view.get_buffer()]
91
bounds = buf.get_selection_bounds()
94
start = buf.get_iter_at_mark(buf.get_insert())
97
if not end.ends_line():
98
end.forward_to_line_end()
100
bounds = [start, end]
102
if not bounds[0].equal(bounds[1]):
103
text = how(bounds[0].get_text(bounds[1]))
105
buf.begin_user_action()
106
buf.delete(bounds[0], bounds[1])
107
buf.insert(bounds[0], text)
108
buf.end_user_action()
110
return commands.result.HIDE
91
112
def upper(view, all=False):
92
"""Make upper case: format.upper [<all>]
113
"""Make upper case: format.upper [<all>]
94
115
Transform text in selection to upper case. If the optional argument <all>
95
116
is specified, text in all the open documents will be transformed."""
96
return _transform(view, lambda x: x.upper(), all)
117
return _transform(view, lambda x: x.upper(), all)
98
119
def lower(view, all=False):
99
"""Make lower case: format.lower [<all>]
120
"""Make lower case: format.lower [<all>]
101
122
Transform text in selection to lower case. If the optional argument <all>
102
123
is specified, text in all the open documents will be transformed."""
103
return _transform(view, lambda x: x.lower(), all)
124
return _transform(view, lambda x: x.lower(), all)
105
126
def title(view, all=False):
106
"""Make title case: format.title [<all>]
127
"""Make title case: format.title [<all>]
108
129
Transform text in selection to title case. If the optional argument <all>
109
130
is specified, text in all the open documents will be transformed."""
110
return _transform(view, lambda x: x.title().replace('_', ''), all)
131
return _transform(view, lambda x: x.title().replace('_', ''), all)