Skip to content

Commit 082ce60

Browse files
Added accept_force parameter to commands decorator. 'bp'/'bn' now also accept an '\!'.
1 parent dfed3a3 commit 082ce60

File tree

3 files changed

+59
-72
lines changed

3 files changed

+59
-72
lines changed

pyvim/commands/commands.py

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
SET_COMMANDS_TAKING_VALUE = set()
1515

1616

17+
_NO_WRITE_SINCE_LAST_CHANGE_TEXT = 'No write since last change (add ! to override)'
18+
19+
1720
def has_command_handler(command):
1821
return command in COMMANDS_TO_HANDLERS
1922

@@ -45,7 +48,7 @@ def decorator(func):
4548
return decorator
4649

4750

48-
def location_cmd(name):
51+
def location_cmd(name, accepts_force=False):
4952
"""
5053
Decorator that registers a command that takes a location as (optional)
5154
parameter.
@@ -56,30 +59,40 @@ def decorator(func):
5659
@_cmd(name)
5760
def command_wrapper(editor, variables):
5861
location = variables.get('location')
59-
func(editor, location)
62+
force = bool(variables['force'])
63+
64+
if accepts_force:
65+
func(editor, location, force=force)
66+
else:
67+
func(editor, location)
6068
return func
6169
return decorator
6270

6371

64-
def cmd(name):
72+
def cmd(name, accepts_force=False):
6573
"""
6674
Decarator that registers a command that doesn't take any parameters.
6775
"""
6876
def decorator(func):
6977
@_cmd(name)
7078
def command_wrapper(editor, variables):
71-
func(editor)
79+
force = bool(variables['force'])
80+
81+
if accepts_force:
82+
func(editor, force=force)
83+
else:
84+
func(editor)
7285
return func
7386
return decorator
7487

7588

76-
def set_cmd(name, takes_value=False):
89+
def set_cmd(name, accepts_value=False):
7790
"""
7891
Docorator that registers a ':set'-command.
7992
"""
8093
def decorator(func):
8194
SET_COMMANDS[name] = func
82-
if takes_value:
95+
if accepts_value:
8396
SET_COMMANDS_TAKING_VALUE.add(name)
8497
return func
8598
return decorator
@@ -105,20 +118,30 @@ def _(editor, variables):
105118
editor.show_message('Unknown option: %s' % option)
106119

107120

108-
@cmd('bn')
109-
def _(editor):
121+
@cmd('bn', accepts_force=True)
122+
def _bn(editor, force=False):
110123
"""
111124
Go to next buffer.
112125
"""
113-
editor.window_arrangement.go_to_next_buffer()
126+
eb = editor.window_arrangement.active_editor_buffer
127+
128+
if not force and eb.has_unsaved_changes:
129+
editor.show_message(_NO_WRITE_SINCE_LAST_CHANGE_TEXT)
130+
else:
131+
editor.window_arrangement.go_to_next_buffer()
114132

115133

116-
@cmd('bp')
117-
def _(editor):
134+
@cmd('bp', accepts_force=True)
135+
def _bp(editor, force=False):
118136
"""
119137
Go to previous buffer.
120138
"""
121-
editor.window_arrangement.go_to_previous_buffer()
139+
eb = editor.window_arrangement.active_editor_buffer
140+
141+
if not force and eb.has_unsaved_changes:
142+
editor.show_message(_NO_WRITE_SINCE_LAST_CHANGE_TEXT)
143+
else:
144+
editor.window_arrangement.go_to_previous_buffer()
122145

123146

124147
@cmd('only')
@@ -202,21 +225,15 @@ def _buffer(editor, variables, force=False):
202225
Go to one of the open buffers.
203226
"""
204227
eb = editor.window_arrangement.active_editor_buffer
228+
force = bool(variables['force'])
205229

206230
buffer_name = variables.get('buffer_name')
207231
if buffer_name:
208232
if not force and eb.has_unsaved_changes:
209-
editor.show_message('No write since last change (add ! to override)')
233+
editor.show_message(_NO_WRITE_SINCE_LAST_CHANGE_TEXT)
210234
else:
211235
editor.window_arrangement.go_to_buffer(buffer_name)
212236

213-
@_cmd('b!')
214-
@_cmd('buffer!')
215-
def _(editor, variables):
216-
"""
217-
Force go to one of the open buffers.
218-
"""
219-
_buffer(editor, variables, force=True)
220237

221238
@cmd('bw')
222239
def _(editor):
@@ -225,8 +242,7 @@ def _(editor):
225242
"""
226243
eb = editor.window_arrangement.active_editor_buffer
227244
if eb.has_unsaved_changes:
228-
editor.show_message('No write since last change for buffer. '
229-
'(add ! to override)')
245+
editor.show_message(_NO_WRITE_SINCE_LAST_CHANGE_TEXT)
230246
else:
231247
editor.window_arrangement.close_buffer()
232248

@@ -248,8 +264,8 @@ def _(editor, location):
248264
editor.window_arrangement.open_buffer(location, show_in_current_window=True)
249265

250266

251-
@cmd('q')
252-
@cmd('quit')
267+
@cmd('q', accepts_force=True)
268+
@cmd('quit', accepts_force=True)
253269
def quit(editor, all_=False, force=False):
254270
"""
255271
Quit.
@@ -258,7 +274,7 @@ def quit(editor, all_=False, force=False):
258274

259275
# When there are buffers that have unsaved changes, show balloon.
260276
if not force and any(eb.has_unsaved_changes for eb in ebs):
261-
editor.show_message('No write since last change (add ! to override)')
277+
editor.show_message(_NO_WRITE_SINCE_LAST_CHANGE_TEXT)
262278

263279
# When there is more than one buffer open.
264280
elif not all_ and len(ebs) > 1:
@@ -268,33 +284,22 @@ def quit(editor, all_=False, force=False):
268284
editor.cli.set_return_value('')
269285

270286

271-
@cmd('qa')
272-
@cmd('qall')
273-
def _(editor):
287+
@cmd('qa', accepts_force=True)
288+
@cmd('qall', accepts_force=True)
289+
def _(editor, force=False):
274290
"""
275291
Quit all.
276292
"""
277-
quit(editor, all_=True)
293+
quit(editor, all_=True, force=force)
278294

279295

280-
@cmd('q!')
281-
@cmd('qa!')
282-
@cmd('quit!')
283-
@cmd('qall!')
284-
def _(editor):
285-
"""
286-
Force quit.
287-
"""
288-
quit(editor, all_=True, force=True)
289-
290-
291-
@location_cmd('w')
292-
@location_cmd('write')
293-
def write(editor, location, overwrite=False):
296+
@location_cmd('w', accepts_force=True)
297+
@location_cmd('write', accepts_force=True)
298+
def write(editor, location, force=False):
294299
"""
295300
Write file.
296301
"""
297-
if location and not overwrite and os.path.exists(location):
302+
if location and not force and os.path.exists(location):
298303
editor.show_message('File exists (add ! to overriwe)')
299304
else:
300305
eb = editor.window_arrangement.active_editor_buffer
@@ -304,21 +309,12 @@ def write(editor, location, overwrite=False):
304309
eb.write(location)
305310

306311

307-
@location_cmd('w!')
308-
@location_cmd('write!')
309-
def _(editor, location):
310-
"""
311-
Write (and overwrite) file.
312-
"""
313-
write(editor, location, overwrite=True)
314-
315-
316-
@location_cmd('wq')
317-
def _(editor, location):
312+
@location_cmd('wq', accepts_force=True)
313+
def _(editor, location, force=False):
318314
"""
319315
Write file and quit.
320316
"""
321-
write(editor, location)
317+
write(editor, location, force=force)
322318
editor.cli.set_return_value('')
323319

324320

@@ -335,15 +331,6 @@ def _(editor):
335331
quit(editor, all_=True, force=False)
336332

337333

338-
@location_cmd('wq!')
339-
def _(editor, location):
340-
"""
341-
Write file (and overwrite) and quit.
342-
"""
343-
write(editor, location, overwrite=True)
344-
editor.cli.set_return_value('')
345-
346-
347334
@cmd('help')
348335
def _(editor):
349336
"""
@@ -472,8 +459,8 @@ def _(editor):
472459
editor.expand_tab = False
473460

474461

475-
@set_cmd('tabstop', takes_value=True)
476-
@set_cmd('ts', takes_value=True)
462+
@set_cmd('tabstop', accepts_value=True)
463+
@set_cmd('ts', accepts_value=True)
477464
def _(editor, value):
478465
"""
479466
Set tabstop.

pyvim/commands/grammar.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
\s*
1313
(
1414
# Commands accepting a location.
15-
(?P<command>%(commands_taking_locations)s) \s+ (?P<location>[^\s]+) |
15+
(?P<command>%(commands_taking_locations)s)(?P<force>!?) \s+ (?P<location>[^\s]+) |
1616
1717
# Commands accepting a buffer.
18-
(?P<command>b|buffer) \s+ (?P<buffer_name>[^\s]+) |
18+
(?P<command>b|buffer)(?P<force>!?) \s+ (?P<buffer_name>[^\s]+) |
1919
2020
# Jump to line numbers.
2121
(?P<go_to_line>\d+) |
@@ -30,8 +30,8 @@
3030
# Shell command
3131
!(?P<shell_command>.*) |
3232
33-
# Any command. (For command completions.)
34-
(?P<command>[^\s]+) |
33+
# Any other normal command.
34+
(?P<command>[^\s!]+)(?P<force>!?) |
3535
3636
# Accept the empty input as well. (Ignores everything.)
3737

pyvim/layout.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def _bufferlist_overlay_visible_condition(cli):
116116
"""
117117
text = cli.buffers[COMMAND_BUFFER].text.lstrip()
118118
return cli.current_buffer_name == COMMAND_BUFFER and (
119-
text.startswith('b ') or text.startswith('buffer '))
119+
any(text.startswith(p) for p in ['b ', 'b! ', 'buffer', 'buffer!']))
120120

121121
bufferlist_overlay_visible_filter = Condition(_bufferlist_overlay_visible_condition)
122122

0 commit comments

Comments
 (0)