is_modified macro function

If reporting a bug with the Zeus IDE please post the details here. Please do not post questions here.
Post Reply
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

is_modified macro function

Post by pwdiener »

Is the is_modified macro function broken? It appears to be returning true regardless of the actual modification state of the buffer. I'm using the following LUA macro to test:

Code: Select all

function key_macro()
    if is_modified() then
        message(macro_tag("$FN") .. " is modified.")
    else
        message(macro_tag("$FN") .. " is NOT modified.")
    end
end

key_macro() -- run the macro
When run (via F10), the "is modified" branch/message is always displayed, whether or not the buffer has been modified.

As a follow on to this and assuming I'm doing something wrong, what will the is_modified state be in a file save postfix event? Since it's postfix, it seems like I might have to expect that the modified tag has been reset, which would be a pain, but not entirely unexpected.

What I'm trying to do is to detect whether the file actually needed to be saved before doing my save postfix stuff, since it can be a bit time consuming, much more so than the actual saving of the file locally.

How about in a file SaveAs postfix event? It occurs to me that it is entirely possible to SaveAs an unmodified buffer, creating a new file - would I see is_modified in that case? I'm guessing I would not.

I was doing some testing in my save postfix event code to try to make that determination when I encountered the problem above, so I stripped it down to the minimum code just to make sure it wasn't unique to the save postfix and still had the problem.

Bill Diener
jussij
Site Admin
Posts: 2465
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Is the is_modified macro function broken?
This is just Lua. Your if statement needs to be this:

Code: Select all

if is_modified() == 1 then
what will the is_modified state be in a file save postfix event?
It should be not modified.
Since it's postfix, it seems like I might have to expect that the modified tag has been reset
Yep that is correct.
What I'm trying to do is to detect whether the file actually needed to be saved before doing my save postfix stuff, since it can be a bit time consuming
This can be done but you are going to need another macro.

There are these two macro functions:

Code: Select all

bool set_global_string(string item, string value [, int local_scope])
string get_global_string(string item [, int local_scope])
So what you can do is write another macro to intercept the save and use the set_global_string to save the modified state.

Then in the postfix macro you can get that state using the set_global_string function.

So in the save you would have code something like this:

Code: Select all

    -- key value
    item = "is_modified:" ... macro_tag("$fn")

    if is_modified() == 1 then
        set_global_string(item, "yes")
    else
        set_global_string(item, "no")
    end
    -- call the save
    FileSave()

Code: Select all

How about in a file SaveAs postfix event?
There is a SaveAs postfix event, but these is no SaveAs prefix event.

Think of the SaveAs trigger as "File Created" trigger.

Code: Select all

It occurs to me that it is entirely possible to SaveAs an unmodified buffer, creating a new file - would I see is_modified in that case?
No. The newly created file will be unmodified.

Aren't you trying to not do the ftp if the file was not modified before the Save As :?:

In which case create a macro using the code above to hook into the save keyboard event.

At that save point check if the file is modified and save that to the global state.

In the postfix Save As trigger get the golbal state and if it is modified do the ftp.
I was doing some testing in my save postfix event code to try to make that determination when I encountered the problem above
If you run into similar issues, I would recommend carefully reading the Lua code and comparing it to similar Lua code in the zScript folder.

I do that quite a bit myself.

I have found Lua as a language has a few of these sort of gotchas where the code looks right but just does not work because a a missing character or two.

Cheers Jussi
pwdiener
Posts: 134
Joined: Wed Jul 11, 2007 3:45 pm

Post by pwdiener »

OK, good - I've got answers to the real questions I had, and learned a little more about LUA in the process. Your suggestion was almost exactly what I had in mind if it turned out that the is_modified was no longer true in the save postfix. I'll get that implemented tomorrow.

As far as the SaveAs handling, if I create a new file, I do need to do the FTP, so there I needn't bother with checking is_modified - just do the FTP regardless. I was hoping to use exactly the same macro for the Save postfix and the SaveAs postfix, but that will have to be a little more complicated now - I'm guessing I can work that out anyway. One possibility that occurs to me is to do what you suggested with global settings in a save prefix event. Then in the postfix, I can test it as you suggested, but I can also determine if the setting is even present and maybe use it's absence to determine that I am actually doing a SaveAs, which will not have run the prefix event.

Anyway, thanks for setting me straight on the LUA.

Bill
Post Reply