41 lines · 1.4 KB
Raw Download
1
import sublime
2
import sublime_plugin
3
import os
4
import re
5
6
class IncrementalSaveCommand(sublime_plugin.TextCommand):
7
    def run(self, edit):
8
        current_file = self.view.file_name()
9
        
10
        if not current_file:
11
            # If file hasn't been saved yet, prompt for save
12
            self.view.window().run_command("save_as")
13
            return
14
        
15
        # Get directory and filename
16
        directory = os.path.dirname(current_file)
17
        filename = os.path.basename(current_file)
18
        name, ext = os.path.splitext(filename)
19
        
20
        # Check if filename ends with a number
21
        match = re.search(r'-(\d+)$', name)
22
        
23
        if match:
24
            # Extract the number and increment it
25
            current_num = int(match.group(1))
26
            new_num = current_num + 1
27
            # Replace the old number with the new one
28
            new_name = re.sub(r'-\d+$', f'-{new_num:02d}', name)
29
        else:
30
            # No number found, add -01
31
            new_name = name + '-01'
32
        
33
        new_filename = new_name + ext
34
        new_path = os.path.join(directory, new_filename)
35
        
36
        # Save the file with the new name
37
        self.view.set_scratch(False)
38
        self.view.retarget(new_path)
39
        self.view.run_command("save")
40
        
41
        sublime.status_message(f"Saved as: {new_filename}")