Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ViEditor.cs « Mono.TextEditor.Vi « Mono.Texteditor « core « src « main - github.com/mono/monodevelop.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 9cd39fdd76c3b764c87934a27446d9834270921c (plain)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// 
// ViEditorContext.cs
//  
// Author:
//       Michael Hutchinson <mhutchinson@novell.com>
// 
// Copyright (c) 2010 Novell, Inc. (http://www.novell.com)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Gdk;

namespace Mono.TextEditor.Vi
{
	public class ViEditor
	{
		NewViEditMode editMode;
		
		#region Register constants
		const char REG_YANK_DEFAULT = '0';
		const char REG_DELETE_DEFAULT = '1';
		const char REG_PASTE_DEFAULT = '"';
		const char REG_DELETE_SMALL = '-';
		const char REG_BLACKHOLE = '_';
		const char REG_LAST_INSERTED_TEXT = '.';
		const char REG_FILENAME_CURRENT = '%';
		const char REG_FILENAME_ALTERNATE = '#';
		const char REG_LAST_COMMANDLINE = ':';
		const char REG_X11_PRIMARY = '*';
		const char REG_X11_CLIPBOARD = '+';
		const char REG_X11_LAST_DROP = '~';
		const char REG_LAST_SEARCH = '/';
		#endregion

		Dictionary<char,string> registers = new Dictionary<char,string> ();
		Dictionary<char,ViMark> marks = new Dictionary<char, ViMark> ();
		
		public MonoTextEditor Editor { get { return editMode.Editor; } }
		public TextEditorData Data { get { return editMode.Data; } }
		public TextDocument Document { get { return Data.Document; } }
		ViBuilderContext Context { get; set; }

		public event EventHandler ModeChanged;
		public event EventHandler MessageChanged;

		string message;
		public string Message {
			get {
				return message;
			}
			private set {
				message = value;
				var e = MessageChanged;
				if (e != null)
					e (this, EventArgs.Empty);
			}
		}

		ViEditorMode mode;
		public ViEditorMode Mode {
			get {
				return mode;
			}
			private set {
				mode = value;
				var e = ModeChanged;
				if (e != null)
					e (this, EventArgs.Empty);
			}
		}

		//shared between editors in the same process
		//TODO: maybe move these into some kind of shared context to pass around explicitly
		static char lastRegisterUsed = '0';
		static string lastPattern;
		static string lastCommandline;
		static string lastInsertedText;
		
		public string LastCommandline {
			get { return lastCommandline; }
			set { lastCommandline = value; }
		}

		public string LastPattern {
			get { return lastPattern; }
			set { lastPattern = value; }
		}
		
		public string LastInsertedText {
			get { return lastInsertedText; }
			set { lastInsertedText = value; }
		}
		
		public void Reset (string message)
		{
			Context = ViBuilderContext.Create (this);
			Message = message;
			
			if (Data.Caret.Mode != CaretMode.Block) {
				Data.Caret.Mode = CaretMode.Block;
				if (Data.Caret.Column > DocumentLocation.MinColumn)
					Data.Caret.Column--;
			}
			ViActions.RetreatFromLineEnd (Data);
		}
		
		public void SetMode (ViEditorMode mode)
		{
			if (this.Mode == mode)
				return;
			this.Mode = mode;
			
			if (Data == null)
				return;
			
			switch (mode) {
			case ViEditorMode.Insert:
				Message = "-- INSERT --";
				editMode.SetCaretMode (CaretMode.Insert);
				break;
			case ViEditorMode.Normal:
				editMode.SetCaretMode (CaretMode.Block);
				break;
			case ViEditorMode.Replace:
				Message = "-- REPLACE --";
				editMode.SetCaretMode (CaretMode.Underscore);
				break;
			}
		}
		
		public void OnCaretPositionChanged ()
		{
			switch (Mode) {
			case ViEditorMode.Insert:
			case ViEditorMode.Replace:
			case ViEditorMode.Visual:
				return;
			case ViEditorMode.Normal:
				ViActions.RetreatFromLineEnd (Data);
				break;
			default:
				Reset ("");
				break;
			}
		}
		
		public void ProcessKey (Gdk.ModifierType modifiers, Key key, char ch)
		{
			if (Context == null)
				Reset ("");
			
			Context.ProcessKey (key, ch, modifiers);
			if (Context.Completed) {
				Reset (Context.Message);
			} else {
				Message = Context.Message;
			}
		}
		
		public Dictionary<char,ViMark> Marks { get { return marks; } }

		public bool SearchBackward { get; set; }
		public string LastReplacement { get; set; }

		public ViEditor (NewViEditMode editMode)
		{
			this.editMode = editMode;
			SetMode (ViEditorMode.Normal);
		}

		public string GetRegisterContents (char register)
		{
			if (register == '"')
				register = lastRegisterUsed;

			switch (register) {
				case REG_LAST_INSERTED_TEXT:
					return LastInsertedText;

				case REG_FILENAME_ALTERNATE:
				case REG_FILENAME_CURRENT:
					return Document.FileName;
				
				case REG_LAST_COMMANDLINE:
					return LastCommandline;

				case REG_LAST_SEARCH:
					return LastPattern;

				case REG_X11_PRIMARY:
				case REG_X11_CLIPBOARD:
				case REG_X11_LAST_DROP:
					throw new NotImplementedException("X11 registers");

				case REG_DELETE_SMALL:
					break;

				default:
					int regInt = (int)register;
					if (((int)'a' <= regInt && regInt <= (int)'z')
						|| ((int)'A' <= regInt && regInt <= (int)'Z')
						|| ((int)'0' <= regInt && regInt <= (int)'9'))
						break;
					else
						throw new InvalidOperationException ("Invalid register '" + register + "'.");
			}

			string value;
			registers.TryGetValue (register, out value);
			return value;
		}
		
		//FIXME: add others when implemented
		static string validSetRegisters = "\"-";
		static string validGetRegisters = ".%#:/-";
		static string validRegisters = "\".%#:/-";
		
		public static bool IsValidRegister (char c)
		{
			int regInt = (int) c;
			return (((int)'a' <= regInt && regInt <= (int)'z')
				|| ((int)'A' <= regInt && regInt <= (int)'Z')
				|| ((int)'0' <= regInt && regInt <= (int)'9')
			    || validRegisters.Contains (c));
		}
		
		public static bool IsValidSetRegister (char c)
		{
			int regInt = (int) c;
			return (((int)'a' <= regInt && regInt <= (int)'z')
				|| ((int)'A' <= regInt && regInt <= (int)'Z')
				|| ((int)'0' <= regInt && regInt <= (int)'9')
			    || validSetRegisters.Contains (c));
		}
		
		public static bool IsValidGetRegister (char c)
		{
			int regInt = (int) c;
			return (((int)'a' <= regInt && regInt <= (int)'z')
				|| ((int)'A' <= regInt && regInt <= (int)'Z')
				|| ((int)'0' <= regInt && regInt <= (int)'9')
			    || validGetRegisters.Contains (c));
		}

		public void SetRegisterContents (char register, string value)
		{
			switch (register) {
				case REG_X11_PRIMARY:
				case REG_X11_CLIPBOARD:
					throw new NotImplementedException ("GUI clipboard");

				//delete/change registers
				case '1': case '2': case '3': case '4': case '5':
				case '6': case '7': case '8':
					//move registers 1-8 downwards if needed
					for (int i = (int)'8'; i >= (int)register; i--)
						registers[(char)(i + 1)] = registers[(char)i];
					break;

				case '0':
				case '9':
				case REG_DELETE_SMALL:
					break;

				case REG_PASTE_DEFAULT:
					register = '0';
					break;

				default:
					int regInt = (int) register;
					if (((int)'a' <= regInt && regInt <= (int)'z')
						|| ((int)'A' <= regInt && regInt <= (int)'Z'))
						break;
					else
						throw new InvalidOperationException ("Cannot write to register '" + register + "'.");
			}

			lastRegisterUsed = register;
			registers[register] = value;
		}
	}
	
	public enum ViEditorMode
	{
		Normal = 0,
		Insert,						// I
		Replace,					// R
		Visual,						// v
		VisualLine,					// V
		VisualBlock,    			// ^V TODO
		Command,
	}
}