Overwriting files marked as read only

Hi again,

Sorry if this question has been asked before, is there a way to save over a file in 4coder that has been marked as read only? I know this sounds weird, but for example. If I have my project in some kind of source control like perforce, files are marked as read only unless they are checked out.

I find it a better workflow to be able to over write files that aren't checked out and just do a reconcile of work via perforce but I can't seem to over write read only files in 4coder.

Thoughts on this? I know other editors like sublime text or even visual studio will ask if you want to overwrite the file but still allow you to do it anyway.

Maybe this is something I can implement myself with the custom stuff?

Thanks,
Wil
I believe that you can use buffer_get_setting and buffer_set_setting in a custom function to remove the read only flag and then save normally or open a query bar if you want to ask for confirmation. I haven't tested it though.
I've had another perforce user ask a similar sort of question. Although I have to admit I'm a bit confused about this now.

First of all, the 4coder API allows you to set settings on buffers to make read only buffers, as mrmixer said, but that doesn't help with read only files.

I have been planning to just have a dialogue warning the user that their attempt to save a file failed whenever they attempt to save a read only file. But it sounds like you're looking for something a little different.

I am not sure what behavior you are asking for. Isn't the entire point of a read only file that they cannot be overwritten? Or are you saying that other editors just save over them anyway by disabling the read only flags?

Please clarify if you can.
Hi Allen, I think that the desired functionality is that when you try to save a read-only file, 4coder pops out a dialog that says something like:

"This file is marked as read-only. Do you want to overwrite it anyway?" -> YES / NO

A lot of other code editors do this thing.
DarkSeed
Hi Allen, I think that the desired functionality is that when you try to save a read-only file, 4coder pops out a dialog that says something like:

"This file is marked as read-only. Do you want to overwrite it anyway?" -> YES / NO

A lot of other code editors do this thing.


Yes thats the functionality i was asking about on your stream some time ago,
but as you said its a little bit strange because you throw away the concept of read-only.
I would like to have a warning only, telling you that the file can't be saved because it is
read-only. That way you don't screw over any other program.
Hey sorry for the late reply,

Basically what Darkseed said. Have it prompt you that the file is marked as read only on disk and allow the user to overwrite it anyway.

A lot of text editors do this and it would be helpful for people using source control tools like perforce that set the read only flag on a file unless its checked out. Usually I just edit a file without checking it out and save it, Perforce then allows you to select a directory and click 'reconcile offline work' which will find any files that are different and check them out for you which is handy.

I spent about 10 - 15 minutes on the weekend playing around with stuff and managed to do a quick implementation myself by injecting the following code into 4coder_base_commands.cpp. It's not ideal but did the trick as a temp solution.

 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
CUSTOM_COMMAND_SIG(save)
{
    auto view_summary = get_active_view(app, AccessOpen);
    auto buff_summary = get_buffer(app, view_summary.buffer_id, AccessOpen);
    if (buff_summary.exists)
    {
        const char* file_name = buff_summary.file_name;
        DWORD attrib = GetFileAttributes(file_name);
        if (attrib != INVALID_FILE_ATTRIBUTES)
        {
            const bool is_read_only = (attrib & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY;
            if (is_read_only)
            {
                Query_Bar bar = { 0 };
                bar.prompt = make_lit_string("You are trying a save a file marked as 'read only' overwrite anyway? (Y)es (N)o");
				bar.string = null_string;
                
                bool success = false;
                if (start_query_bar(app, &bar, 0) != 0 )
                {
                    while (true)
                    {
                        User_Input in = get_user_input(app, EventOnAnyKey, EventOnEsc);
                        if (in.abort)
                        {
                            return;
                        }
                        
                        if (in.key.character == 'y' || in.key.character == 'Y')
                        {
                            success = true;
                            break;
                        }
                        else if (in.key.character == 'N' || in.key.character == 'n')
                        {
                            success = false;
                            break;
                        }
                    }
                    
                    if (success)
                    {
                        attrib &= ~FILE_ATTRIBUTE_READONLY;
                        SetFileAttributes(file_name, attrib);
                        exec_command(app, cmdid_save);
                    }
                    else
                    {
                        end_query_bar(app, &bar, 0);
                    }
                }
            }
            else
            {
                exec_command(app, cmdid_save);
            }
        }
    }
}


It assumes you are working on windows, also I was trying to see if I could get the functionality similar to the prompt that asks if you want to close 4coder even though there are unsaved changes. There is a yes and no option which you can click with the mouse or select with the keyboard.

I tried to achieve this with query bars but I got a crash. I also stumbled across another forum post where Allen was saying the Query bars are a more of a hack than a more permanent solution.