~ubuntu-branches/ubuntu/raring/gedit-plugins/raring-proposed

« back to all changes in this revision

Viewing changes to plugins/commander/modules/format.py

  • Committer: Bazaar Package Importer
  • Author(s): Michael Biebl
  • Date: 2011-07-22 20:45:45 UTC
  • mfrom: (1.1.26 upstream)
  • mto: (7.3.1 sid) (1.4.8)
  • mto: This revision was merged to the branch mainline in revision 44.
  • Revision ID: james.westby@ubuntu.com-20110722204545-15t6eui58z5tdb6l
Tags: upstream-3.0.5
ImportĀ upstreamĀ versionĀ 3.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
#
 
3
#  format.py - format commander module
 
4
#
 
5
#  Copyright (C) 2010 - Jesse van den Kieboom
 
6
#
 
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.
 
11
#
 
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.
 
16
#
 
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.
 
21
 
1
22
import commander.commands as commands
2
23
 
3
24
__commander_module__ = True
4
25
 
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>]
7
28
 
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."""
12
33
 
13
 
        if all:
14
 
                buffers = view.get_toplevel().get_documents()
15
 
        else:
16
 
                buffers = [view.get_buffer()]
17
 
 
18
 
        for buf in buffers:
19
 
                bounds = buf.get_selection_bounds()
20
 
 
21
 
                if not bounds:
22
 
                        bounds = buf.get_bounds()
23
 
 
24
 
                buf.begin_user_action()
25
 
 
26
 
                try:
27
 
                        # For each line, remove trailing spaces
28
 
                        if not bounds[1].ends_line():
29
 
                                bounds[1].forward_to_line_end()
30
 
 
31
 
                        until = buf.create_mark(None, bounds[1], False)
32
 
                        start = bounds[0]
33
 
                        start.set_line_offset(0)
34
 
 
35
 
                        while start.compare(buf.get_iter_at_mark(until)) < 0:
36
 
                                end = start.copy()
37
 
                                end.forward_to_line_end()
38
 
                                last = end.copy()
39
 
 
40
 
                                if end.equal(buf.get_end_iter()):
41
 
                                        end.backward_char()
42
 
 
43
 
                                while end.get_char().isspace() and end.compare(start) > 0:
44
 
                                        end.backward_char()
45
 
 
46
 
                                if not end.ends_line():
47
 
                                        if not end.get_char().isspace():
48
 
                                                end.forward_char()
49
 
 
50
 
                                        buf.delete(end, last)
51
 
 
52
 
                                start = end.copy()
53
 
                                start.forward_line()
54
 
 
55
 
                except Exception, e:
56
 
                        print e
57
 
 
58
 
                buf.delete_mark(until)
59
 
                buf.end_user_action()
60
 
 
61
 
        return commands.result.HIDE
 
34
    if all:
 
35
        buffers = view.get_toplevel().get_documents()
 
36
    else:
 
37
        buffers = [view.get_buffer()]
 
38
 
 
39
    for buf in buffers:
 
40
        bounds = buf.get_selection_bounds()
 
41
 
 
42
        if not bounds:
 
43
            bounds = buf.get_bounds()
 
44
 
 
45
        buf.begin_user_action()
 
46
 
 
47
        try:
 
48
            # For each line, remove trailing spaces
 
49
            if not bounds[1].ends_line():
 
50
                bounds[1].forward_to_line_end()
 
51
 
 
52
            until = buf.create_mark(None, bounds[1], False)
 
53
            start = bounds[0]
 
54
            start.set_line_offset(0)
 
55
 
 
56
            while start.compare(buf.get_iter_at_mark(until)) < 0:
 
57
                end = start.copy()
 
58
                end.forward_to_line_end()
 
59
                last = end.copy()
 
60
 
 
61
                if end.equal(buf.get_end_iter()):
 
62
                    end.backward_char()
 
63
 
 
64
                while end.get_char().isspace() and end.compare(start) > 0:
 
65
                    end.backward_char()
 
66
 
 
67
                if not end.ends_line():
 
68
                    if not end.get_char().isspace():
 
69
                        end.forward_char()
 
70
 
 
71
                    buf.delete(end, last)
 
72
 
 
73
                start = end.copy()
 
74
                start.forward_line()
 
75
 
 
76
        except Exception, e:
 
77
            print e
 
78
 
 
79
        buf.delete_mark(until)
 
80
        buf.end_user_action()
 
81
 
 
82
    return commands.result.HIDE
62
83
 
63
84
def _transform(view, how, all):
64
 
        if all:
65
 
                buffers = view.get_toplevel().get_documents()
66
 
        else:
67
 
                buffers = [view.get_buffer()]
68
 
 
69
 
        for buf in buffers:
70
 
                bounds = buf.get_selection_bounds()
71
 
 
72
 
                if not bounds:
73
 
                        start = buf.get_iter_at_mark(buf.get_insert())
74
 
                        end = start.copy()
75
 
 
76
 
                        if not end.ends_line():
77
 
                                end.forward_to_line_end()
78
 
 
79
 
                        bounds = [start, end]
80
 
 
81
 
                if not bounds[0].equal(bounds[1]):
82
 
                        text = how(bounds[0].get_text(bounds[1]))
83
 
 
84
 
                        buf.begin_user_action()
85
 
                        buf.delete(bounds[0], bounds[1])
86
 
                        buf.insert(bounds[0], text)
87
 
                        buf.end_user_action()
88
 
 
89
 
        return commands.result.HIDE
 
85
    if all:
 
86
        buffers = view.get_toplevel().get_documents()
 
87
    else:
 
88
        buffers = [view.get_buffer()]
 
89
 
 
90
    for buf in buffers:
 
91
        bounds = buf.get_selection_bounds()
 
92
 
 
93
        if not bounds:
 
94
            start = buf.get_iter_at_mark(buf.get_insert())
 
95
            end = start.copy()
 
96
 
 
97
            if not end.ends_line():
 
98
                end.forward_to_line_end()
 
99
 
 
100
            bounds = [start, end]
 
101
 
 
102
        if not bounds[0].equal(bounds[1]):
 
103
            text = how(bounds[0].get_text(bounds[1]))
 
104
 
 
105
            buf.begin_user_action()
 
106
            buf.delete(bounds[0], bounds[1])
 
107
            buf.insert(bounds[0], text)
 
108
            buf.end_user_action()
 
109
 
 
110
    return commands.result.HIDE
90
111
 
91
112
def upper(view, all=False):
92
 
        """Make upper case: format.upper [&lt;all&gt;]
 
113
    """Make upper case: format.upper [&lt;all&gt;]
93
114
 
94
115
Transform text in selection to upper case. If the optional argument &lt;all&gt;
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)
97
118
 
98
119
def lower(view, all=False):
99
 
        """Make lower case: format.lower [&lt;all&gt;]
 
120
    """Make lower case: format.lower [&lt;all&gt;]
100
121
 
101
122
Transform text in selection to lower case. If the optional argument &lt;all&gt;
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)
104
125
 
105
126
def title(view, all=False):
106
 
        """Make title case: format.title [&lt;all&gt;]
 
127
    """Make title case: format.title [&lt;all&gt;]
107
128
 
108
129
Transform text in selection to title case. If the optional argument &lt;all&gt;
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)
 
132
 
 
133
# vi:ex:ts=4:et