Source code: com/maddyhome/idea/vim/command/Command.java
1 package com.maddyhome.idea.vim.command;
2
3 /*
4 * IdeaVim - A Vim emulator plugin for IntelliJ Idea
5 * Copyright (C) 2003 Rick Maddy
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (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, Boston, MA 02111-1307, USA.
20 */
21
22 import com.intellij.openapi.actionSystem.AnAction;
23 import com.intellij.openapi.editor.actionSystem.EditorAction;
24 import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
25 import com.maddyhome.idea.vim.handler.AbstractEditorActionHandler;
26 import java.util.List;
27
28 /**
29 * This represents a single Vim command to be executed. It may optionally include an argument if appropriate for
30 * the command. The command has a count and a type.
31 */
32 public class Command
33 {
34 /** Motion flags */
35 public static final int FLAG_MOT_LINEWISE = 1 << 1;
36 public static final int FLAG_MOT_CHARACTERWISE = 1 << 2;
37 public static final int FLAG_MOT_INCLUSIVE = 1 << 3;
38 public static final int FLAG_MOT_EXCLUSIVE = 1 << 4;
39 /** Indicates that the cursor position should be saved prior to this motion command */
40 public static final int FLAG_SAVE_JUMP = 1 << 5;
41
42 /** Special command flag that indicates it is not to be repeated */
43 public static final int FLAG_NO_REPEAT = 1 << 8;
44 /** This insert command should clear all saved keystrokes from the current insert */
45 public static final int FLAG_CLEAR_STROKES = 1 << 9;
46 /** This keystroke should be saved as part of the current insert */
47 public static final int FLAG_SAVE_STROKE = 1 << 10;
48
49 /** Search Flags */
50 public static final int FLAG_SEARCH_FWD = 1 << 16;
51 public static final int FLAG_SEARCH_REV = 1 << 17;
52
53 /** Special flag used for any mappings involving operators */
54 public static final int FLAG_OP_PEND = 1 << 24;
55 /** This command starts a multi-command undo transaction */
56 public static final int FLAG_MULTIKEY_UNDO = 1 << 25;
57 /** This command should be followed by another command */
58 public static final int FLAG_EXPECT_MORE = 1 << 26;
59 /** This flag indicates the command's argument isn't used while recording */
60 public static final int FLAG_NO_ARG_RECORDING = 1 << 27;
61
62 /** Represents commands that actually move the cursor and can be arguments to operators */
63 public static final int MOTION = 1;
64 /** Represents commands that insert new text into the editor */
65 public static final int INSERT = 2;
66 /** Represents commands that remove text from the editor */
67 public static final int DELETE = 3;
68 /** Represents commands that change text in the editor */
69 public static final int CHANGE = 4;
70 /** Represents commands that copy text in the editor */
71 public static final int COPY = 5;
72 /** Represents commands that paste text into the editor */
73 public static final int PASTE = 6;
74 public static final int RESET = 7;
75 /** Represents commands that select the register */
76 public static final int SELECT_REGISTER = 8;
77 /** Represents other types of commands */
78 public static final int OTHER_READONLY = 9;
79 public static final int OTHER_WRITABLE = 10;
80
81 public static boolean isReadOnlyType(int type)
82 {
83 boolean res = false;
84 switch (type)
85 {
86 case MOTION:
87 case COPY:
88 case SELECT_REGISTER:
89 case OTHER_READONLY:
90 res = true;
91 }
92
93 return res;
94 }
95
96 /**
97 * Creates a command that doesn't require an argument
98 * @param count The number entered prior to the command (zero if no specific number)
99 * @param action The action to be executed when the command is run
100 * @param type The type of the command
101 * @param flags Any custom flags specific to this command
102 */
103 public Command(int count, AnAction action, int type, int flags)
104 {
105 this(count, action, type, flags, null);
106 }
107
108 /**
109 * Creates a command that requires an argument
110 * @param count The number entered prior to the command (zero if no specific number)
111 * @param action The action to be executed when the command is run
112 * @param type The type of the command
113 * @param flags Any custom flags specific to this command
114 * @param arg The argument to this command
115 */
116 public Command(int count, AnAction action, int type, int flags, Argument arg)
117 {
118 this.count = count;
119 this.action = action;
120 this.type = type;
121 this.flags = flags;
122 this.argument = arg;
123
124 if (action instanceof EditorAction)
125 {
126 EditorAction eaction = (EditorAction)action;
127 EditorActionHandler handler = eaction.getHandler();
128 if (handler instanceof AbstractEditorActionHandler)
129 {
130 ((AbstractEditorActionHandler)handler).process(this);
131 }
132 }
133 }
134
135 /**
136 * Returns the command count. A zero count is returned as one since that is the default for most commands
137 * @return The command count
138 */
139 public int getCount()
140 {
141 return count == 0 ? 1 : count;
142 }
143
144 /**
145 * Updates the command count to the new value
146 * @param count The new command count
147 */
148 public void setCount(int count)
149 {
150 this.count = count;
151 }
152
153 /**
154 * Gets to actual count entered by the user, including zero if no count was specified. Some commands need to
155 * know whether an actual count was specified or not.
156 * @return The actual count entered by the user
157 */
158 public int getRawCount()
159 {
160 return count;
161 }
162
163 /**
164 * Gets the command type
165 * @return The command type
166 */
167 public int getType()
168 {
169 return type;
170 }
171
172 /**
173 * Gets the flags associated with the command
174 * @return The command flags
175 */
176 public int getFlags()
177 {
178 return flags;
179 }
180
181 /**
182 * Sets new flags for the command
183 * @param flags The new flags
184 */
185 public void setFlags(int flags)
186 {
187 this.flags = flags;
188 }
189
190 /**
191 * Gets the action to execute when the command is run
192 * @return The command's action
193 */
194 public AnAction getAction()
195 {
196 return action;
197 }
198
199 /**
200 * Sets a new action for the command
201 * @param action The new action
202 */
203 public void setAction(AnAction action)
204 {
205 this.action = action;
206 }
207
208 /**
209 * Gets the command's argument, if any.
210 * @return The command's argument, null if there isn't one
211 */
212 public Argument getArgument()
213 {
214 return argument;
215 }
216
217 /**
218 * Sets the command's argument to the new value
219 * @param argument The new argument, can be null to clear the argument
220 */
221 public void setArgument(Argument argument)
222 {
223 this.argument = argument;
224 }
225
226 public List getKeys()
227 {
228 return keys;
229 }
230
231 public void setKeys(List keys)
232 {
233 this.keys = keys;
234 }
235
236 private int count;
237 private AnAction action;
238 private int type;
239 private int flags;
240 private Argument argument;
241 private List keys;
242 }