~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to src/core/Mono.Texteditor/Mono.TextEditor.Vi/ViMode.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
namespace Mono.TextEditor.Vi
36
36
{
37
 
 
 
37
        public class NewViEditMode : EditMode
 
38
        {
 
39
                protected ViEditor ViEditor { get ; private set ;}
 
40
                
 
41
                public NewViEditMode ()
 
42
                {
 
43
                        ViEditor = new ViEditor (this);
 
44
                }
 
45
                
 
46
                protected override void HandleKeypress (Gdk.Key key, uint unicodeKey, Gdk.ModifierType modifier)
 
47
                {
 
48
                        ViEditor.ProcessKey (modifier, key, (char)unicodeKey);
 
49
                }
 
50
                
 
51
                public new TextEditor Editor { get { return base.Editor; } }
 
52
                public new TextEditorData Data { get { return base.Data; } }
 
53
                
 
54
                public override bool WantsToPreemptIM {
 
55
                        get {
 
56
                                switch (ViEditor.Mode) {
 
57
                                case ViEditorMode.Insert:
 
58
                                case ViEditorMode.Replace:
 
59
                                        return false;
 
60
                                case ViEditorMode.Normal:
 
61
                                case ViEditorMode.Visual:
 
62
                                case ViEditorMode.VisualLine:
 
63
                                default:
 
64
                                        return true;
 
65
                                }
 
66
                        }
 
67
                }
 
68
                
 
69
                protected override void OnAddedToEditor (TextEditorData data)
 
70
                {
 
71
                        ViEditor.SetMode (ViEditorMode.Normal);
 
72
                        SetCaretMode (CaretMode.Block, data);
 
73
                        ViActions.RetreatFromLineEnd (data);
 
74
                }
 
75
                
 
76
                protected override void OnRemovedFromEditor (TextEditorData data)
 
77
                {
 
78
                        SetCaretMode (CaretMode.Insert, data);
 
79
                }
 
80
                
 
81
                protected override void CaretPositionChanged ()
 
82
                {
 
83
                        ViEditor.OnCaretPositionChanged ();
 
84
                }
 
85
                
 
86
                public void SetCaretMode (CaretMode mode)
 
87
                {
 
88
                        SetCaretMode (mode, Data);
 
89
                }
 
90
                
 
91
                static void SetCaretMode (CaretMode mode, TextEditorData data)
 
92
                {
 
93
                        if (data.Caret.Mode == mode)
 
94
                                return;
 
95
                        data.Caret.Mode = mode;
 
96
                        data.Document.RequestUpdate (new SinglePositionUpdate (data.Caret.Line, data.Caret.Column));
 
97
                        data.Document.CommitDocumentUpdate ();
 
98
                }
 
99
        }
 
100
        
38
101
        public class ViEditMode : EditMode
39
102
        {
40
103
                bool searchBackward;
45
108
                StringBuilder commandBuffer = new StringBuilder ();
46
109
                Dictionary<char,ViMark> marks = new Dictionary<char, ViMark>();
47
110
                Dictionary<char,ViMacro> macros = new Dictionary<char, ViMacro>();
 
111
                char macros_lastplayed = '@'; // start with the illegal macro character
48
112
                string statusText = "";
49
113
                
50
114
                /// <summary>
77
141
                                        
78
142
                                int line;
79
143
                                if (int.TryParse (command.Substring (1), out line)) {
80
 
                                        if (line < 0 || line > Data.Document.LineCount) {
 
144
                                        if (line < DocumentLocation.MinLine || line > Data.Document.LineCount) {
81
145
                                                return "Invalid line number.";
82
146
                                        } else if (line == 0) {
83
147
                                                RunAction (CaretMoveActions.ToDocumentStart);
194
258
                        data.ClearSelection ();
195
259
                        
196
260
                        //Editor can be null during GUI-less tests
197
 
                        if (Editor != null)
198
 
                                Editor.HighlightSearchPattern = false;
 
261
                        // Commenting this fixes bug: Bug 622618 - Inline search fails in vi mode
 
262
//                      if (Editor != null)
 
263
//                              Editor.HighlightSearchPattern = false;
199
264
                        
200
265
                        if (CaretMode.Block != data.Caret.Mode) {
201
266
                                data.Caret.Mode = CaretMode.Block;
202
 
                                if (data.Caret.Column > 0)
 
267
                                if (data.Caret.Column > DocumentLocation.MinColumn)
203
268
                                        data.Caret.Column--;
204
269
                        }
205
270
                        ViActions.RetreatFromLineEnd (data);
354
419
                                                return;
355
420
                                                
356
421
                                        case 'x':
357
 
                                                if (Data.Caret.Column == Data.Document.GetLine (Data.Caret.Line).EditableLength)
 
422
                                                if (Data.Caret.Column == Data.Document.GetLine (Data.Caret.Line).EditableLength + 1)
358
423
                                                        return;
359
424
                                                Status = string.Empty;
360
425
                                                if (!Data.IsSomethingSelected)
365
430
                                                return;
366
431
                                                
367
432
                                        case 'X':
368
 
                                                if (Data.Caret.Column == 0)
 
433
                                                if (Data.Caret.Column == DocumentLocation.MinColumn)
369
434
                                                        return;
370
435
                                                Status = string.Empty;
371
436
                                                if (!Data.IsSomethingSelected && 0 < Caret.Offset)
423
488
                                                return;
424
489
                                                
425
490
                                        case 'H':
426
 
                                                Caret.Line = System.Math.Max (0, Editor.VisualToDocumentLocation (0, Editor.LineHeight - 1).Line);
 
491
                                                Caret.Line = System.Math.Max (DocumentLocation.MinLine, Editor.PointToLocation (0, Editor.LineHeight - 1).Line);
427
492
                                                return;
428
493
                                        case 'J':
429
494
                                                RunAction (ViActions.Join);
430
495
                                                return;
431
496
                                        case 'L':
432
 
                                                int line = Editor.VisualToDocumentLocation (0, Editor.Allocation.Height - Editor.LineHeight * 2 - 2).Line;
433
 
                                                if (line < 0)
434
 
                                                        line = Document.LineCount - 1;
 
497
                                                int line = Editor.PointToLocation (0, Editor.Allocation.Height - Editor.LineHeight * 2 - 2).Line;
 
498
                                                if (line < DocumentLocation.MinLine)
 
499
                                                        line = Document.LineCount;
435
500
                                                Caret.Line = line;
436
501
                                                return;
437
502
                                        case 'M':
438
 
                                                line = Editor.VisualToDocumentLocation (0, Editor.Allocation.Height/2).Line;
439
 
                                                if (line < 0)
440
 
                                                        line = Document.LineCount - 1;
 
503
                                                line = Editor.PointToLocation (0, Editor.Allocation.Height/2).Line;
 
504
                                                if (line < DocumentLocation.MinLine)
 
505
                                                        line = Document.LineCount;
441
506
                                                Caret.Line = line;
442
507
                                                return;
443
508
                                                
582
647
                        case State.Replace:
583
648
                                action = GetInsertAction (key, modifier);
584
649
                                
585
 
                                
586
650
                                if (action != null)
587
651
                                        RunAction (action);
588
652
                                else if (unicodeKey != 0)
766
830
                        
767
831
                        case State.PlayMacro: {
768
832
                                char k = (char) unicodeKey;
 
833
                                if (k == '@') 
 
834
                                        k = macros_lastplayed;
769
835
                                if (macros.ContainsKey(k)) {
770
836
                                        Reset ("");
 
837
                                        macros_lastplayed = k; // FIXME play nice when playing macros from inside macros?
771
838
                                        ViMacro macroToPlay = macros [k];
772
839
                                        foreach (ViMacro.KeySet keySet in macroToPlay.KeysPressed) {
773
 
                                                HandleKeypress(keySet.Key, keySet.UnicodeKey, keySet.Modifiers);
 
840
                                                HandleKeypress(keySet.Key, keySet.UnicodeKey, keySet.Modifiers); // FIXME stop on errors? essential with multipliers and nowrapscan
774
841
                                        }
775
842
                                        /* Once all the keys have been played back, quickly exit. */
776
843
                                        return;
777
844
                                } else {
778
 
                                        Reset ("Invalid Macro Name");
 
845
                                        Reset ("Invalid Macro Name '" + k + "'");
779
846
                                        return;
780
847
                                }
781
848
                        }
957
1024
                private void PasteAfter (bool linemode)
958
1025
                {
959
1026
                        TextEditorData data = Data;
 
1027
                        this.Document.BeginAtomicUndo();
960
1028
                        
961
1029
                        Gtk.Clipboard.Get (ClipboardActions.CopyOperation.CLIPBOARD_ATOM).RequestText 
962
1030
                                (delegate (Gtk.Clipboard cb, string contents) {
994
1062
                                }
995
1063
                                Reset (string.Empty);
996
1064
                        });
 
1065
                        this.Document.EndAtomicUndo();
997
1066
                }
998
1067
 
999
1068
                /// <summary>
1004
1073
                {
1005
1074
                        TextEditorData data = Data;
1006
1075
                        
 
1076
                        this.Document.BeginAtomicUndo();
1007
1077
                        Gtk.Clipboard.Get (ClipboardActions.CopyOperation.CLIPBOARD_ATOM).RequestText 
1008
1078
                                (delegate (Gtk.Clipboard cb, string contents) {
1009
1079
                                if (contents == null)
1039
1109
                                }
1040
1110
                                Reset (string.Empty);
1041
1111
                        });
 
1112
                        this.Document.EndAtomicUndo();
1042
1113
                }
1043
1114
 
1044
1115
                enum State {