~ubuntu-branches/ubuntu/raring/wxwidgets2.8/raring

« back to all changes in this revision

Viewing changes to wxPython/wx/tools/Editra/src/ed_vim.py

  • Committer: Package Import Robot
  • Author(s): Stéphane Graber
  • Date: 2012-01-07 13:59:25 UTC
  • mfrom: (1.1.9) (5.1.10 sid)
  • Revision ID: package-import@ubuntu.com-20120107135925-2601miy9ullcon9j
Tags: 2.8.12.1-6ubuntu1
* Resync from Debian, changes that were kept:
  - debian/rules: re-enable mediactrl. This allows libwx_gtk2u_media-2.8 to be
    built, as this is required by some applications (LP: #632984)
  - debian/control: Build-dep on libxt-dev for mediactrl.
  - Patches
    + fix-bashism-in-example
* Add conflict on python-wxgtk2.8 (<< 2.8.12.1-6ubuntu1~) to python-wxversion
  to guarantee upgrade ordering when moving from pycentral to dh_python2.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
"""
14
14
 
15
15
__author__ = "Hasan Aljudy"
16
 
__cvsid__ = "$Id: ed_vim.py 63851 2010-04-04 15:39:50Z CJP $"
17
 
__revision__ = "$Revision: 63851 $"
 
16
__cvsid__ = "$Id: ed_vim.py 66867 2011-02-08 23:52:17Z CJP $"
 
17
__revision__ = "$Revision: 66867 $"
18
18
 
19
19
# ---------------------------------------------------------------------------- #
20
20
# Imports
75
75
 
76
76
    """
77
77
    def __init__(self, keyprocessor):
 
78
        super(EditraCommander, self).__init__()
78
79
 
79
80
        # Attributes
80
81
        self.keyprocessor = keyprocessor
88
89
        self.LastFindChar = None
89
90
        self._Bookmarks = {}
90
91
 
 
92
    @property
 
93
    def STC(self):
 
94
        return self.stc
 
95
 
91
96
    def InsertMode(self):
92
97
        """Put editor in insert mode"""
93
98
        self.keyprocessor.InsertMode()
119
124
            self.RepeatChangeCommand(self.InsertRepeat - 1)
120
125
            self.InsertRepeat = 1
121
126
 
 
127
    def IsAtLineEnd(self):
 
128
        lnum = self.stc.GetCurrentLine()
 
129
        epos = self.stc.GetLineEndPosition(lnum)
 
130
        return epos == self._GetPos()
 
131
 
 
132
    def IsAtLineStart(self):
 
133
        """Is the cursor currently at the start of a line
 
134
        @return: bool
 
135
 
 
136
        """
 
137
        return self._GetCol() == 0
 
138
 
122
139
    def Undo(self, repeat):
123
140
        """Undo actions in the buffer
124
141
        @param repeat: int
141
158
 
142
159
    def _SetPos(self, pos):
143
160
        """Set caret position"""
144
 
        return self.stc.GotoPos(pos)
 
161
        self.stc.GotoPos(pos)
145
162
 
146
163
    def SelectLines(self, repeat):
147
164
        """Select specified number of lines starting with current line
149
166
        @param repeat: int
150
167
 
151
168
        """
 
169
        cline = self.stc.GetCurrentLine()
 
170
        lline = self.stc.GetLineCount() - 1
152
171
        self.GotoLineStart()
 
172
        if cline == lline:
 
173
            cpos = self.stc.GetCurrentPos() - len(self.stc.GetEOLChar())
 
174
            cpos = max(0, cpos)
 
175
            self.stc.GotoPos(cpos)
153
176
        self.PushCaret()
154
 
        self.GotoLineStart()
155
177
        self.MoveDown(repeat)
156
178
        self.StartSelection()
157
179
        self.PopCaret()
179
201
 
180
202
    def PopColumn(self, restore=True):
181
203
        """Pop caret position, and optionally discard it
182
 
 
183
204
        @keyword restore: if set to False, column will be discarded
184
205
 
185
206
        """
243
264
 
244
265
    def EndSelection(self):
245
266
        """Set the selection to start from the starting position as it was set
246
 
        by StartSeletion to the current caret position
 
267
        by StartSelection to the current caret position
247
268
 
248
269
        It doesn't matter whether the starting position is before or after the
249
270
        ending position
337
358
        self.stc.MoveCaretPos(-repeat)
338
359
 
339
360
    def MoveForward(self, repeat=1):
340
 
        # TODO: move left for RTL mode
 
361
        """Move the caret position to the right"""
341
362
        self.MoveRight(repeat)
342
363
 
343
364
    def MoveBack(self, repeat=1):
344
 
        # TODO: move right for RTL mode
 
365
        """Move the caret position to the left"""
345
366
        self.MoveLeft(repeat)
346
367
 
347
368
    def NextWord(self, repeat=1):
520
541
        self._NextIdent(repeat)
521
542
 
522
543
    def PrevIdent(self, repeat=1):
523
 
        """Find the previos occurance of identifier under cursor"""
 
544
        """Find the previous occurrence of identifier under cursor"""
524
545
        self._NextIdent(repeat, back=True)
525
546
 
526
547
    def InsertText(self, text):
601
622
    def DeleteSelection(self):
602
623
        """Yank selection and delete it"""
603
624
        start, end = self._GetSelectionRange()
 
625
        self.stc.BeginUndoAction()
604
626
        self.YankSelection()
605
627
        self.stc.Clear()
606
628
        self._SetPos(start)
 
629
        self.stc.EndUndoAction()
607
630
 
608
631
    def ChangeSelection(self):
609
632
        """Yank selection, delete it, and enter insert mode"""
 
633
        # HACK: need to adjust selection behavior for change command
 
634
        #       to better match vi behavior by not including trailing
 
635
        #       whitespace in the selection from the motion.
 
636
        stext = self.STC.GetSelectedText()
 
637
        clen = len(stext)
 
638
        slen = len(stext.rstrip())
 
639
        if slen < clen and slen > 0:
 
640
            start, end = self._GetSelectionRange()
 
641
            new_end = end - (clen - slen)
 
642
            self.STC.SetSelectionStart(start)
 
643
            self.STC.SetSelectionEnd(new_end)
610
644
        self.DeleteSelection()
611
645
        self.InsertMode()
612
646
 
624
658
        if not text:
625
659
            return
626
660
 
627
 
        if self._IsLine(text):
 
661
        bIsLine = self._IsLine(text)
 
662
        if bIsLine:
628
663
            self.GotoLineStart()
629
664
            if not before:
630
665
                self.MoveDown()
631
666
 
 
667
        self.BeginUndoAction()
632
668
        if self.HasSelection():
633
669
            # paste over selection, if any
634
670
            self.stc.Clear()
 
671
        elif not bIsLine:
 
672
            self.stc.CharRight()
635
673
 
636
 
        self.BeginUndoAction()
637
674
        for i in range(repeat):
638
675
            self.InsertText(text)
639
676
        self.EndUndoAction()
734
771
        """Goto the position of the bookmark associated with the given
735
772
        character label.
736
773
        @param mark: the character label of the book mark
 
774
        @todo: hook into Bookmark Manager ed_bookmark?
737
775
 
738
776
        """
739
777
        if not mark in self._Bookmarks:
970
1008
    if editor.IsInsertMode():
971
1009
        editor.NormalMode() # in case it was a 'c' command
972
1010
 
973
 
@vim_parser('hjkl', is_motion=True)
 
1011
@vim_parser(u'hjkl\r \x08\u013c\u013a', is_motion=True)
974
1012
def Arrows(editor, repeat, cmd):
975
1013
    """Basic arrow movement in vim.
976
1014
    @see: vim_parser
981
1019
            u'j': editor.MoveDown,
982
1020
            u'k': editor.MoveUp,
983
1021
            u'l': editor.MoveRight,
 
1022
            u'\r': editor.MoveDown,
 
1023
            u' ' : editor.MoveRight,
 
1024
            u'\x08' : editor.MoveLeft
984
1025
          }
985
 
    cmd_map[cmd](repeat)
 
1026
    if cmd in cmd_map:
 
1027
        cmd_map[cmd](repeat)
 
1028
    else:
 
1029
        # Handle motion for actual arrow keys
 
1030
        if cmd == u'\u013c' and not editor.IsAtLineEnd():
 
1031
            # Right arrow
 
1032
            editor.MoveRight()
 
1033
        elif cmd == u'\u013a' and not editor.IsAtLineStart():
 
1034
            # Left Arrow
 
1035
            editor.MoveLeft()
986
1036
 
987
1037
@vim_parser('wbeWBE[]', is_motion=True)
988
1038
def Words(editor,repeat, cmd):
1118
1168
        cmd, motion = cmd[0], cmd[1:]
1119
1169
        if motion.isdigit():
1120
1170
            return ret(NeedMore)
 
1171
 
1121
1172
        motion_repeat, motion_cmd = SplitRepeat(motion)
1122
1173
        if motion_repeat:
1123
1174
            repeat = repeat * motion_repeat
 
1175
 
1124
1176
        if motion_cmd == cmd:
1125
1177
            # Operate on whole line
1126
1178
            line_motion = True
1160
1212
    restore_x = cmd in (u'y', u'<', u'>') or cmd == u'd' and line_motion
1161
1213
    editor.PopColumn(restore_x)
1162
1214
 
1163
 
    # XXX:Some special case handling
1164
 
    #     Not the most elegant way though ..
 
1215
    # XXX: Some special case handling
 
1216
    #      Not the most elegant way though ..
1165
1217
    if cmd == u'c':
1166
 
        if line_motion:
1167
 
            editor.OpenLineUp()
1168
 
 
1169
 
    if cmd not in u'c':
 
1218
        pass
 
1219
    else:
1170
1220
        # Repeating delete/yank/indent commands doesn't insert any text
1171
1221
        editor.SetLastInsertedText(u'')
1172
1222
        # Applying them in visual mode ends visual mode
1176
1226
        # Remember this command as last change
1177
1227
        # However, if we're operating on a selection, then remembering
1178
1228
        # doesn't make much sense
1179
 
        editor.SetLastChangeCommand(lambda: Change(editor, repeat, cmd+motion))
 
1229
        editor.SetLastChangeCommand(lambda : Change(editor, repeat, cmd+motion))
1180
1230
 
1181
1231
@vim_parser(u'pP')
1182
1232
def Put(editor, repeat, cmd):