Re: [code] Close editor when unsaved changes

From: Gabriel Dubatti <gdubatti.att.gmail.com>
Date: Tue, 3 Sep 2019 12:13:28 -0300

Hi Lukas,

El 03/09/2019 a las 11:57, Gabriel Dubatti escribió:
> Hi Lukas,
>
> El 03/09/2019 a las 10:35, Procházka Lukáš Ing. escribió:
>> Hello,
>>
>> I created a simple code which should provide "Save & quit" option for TA.
>> Unsaved changes in [Message Buffer] and similar [...] are to be
>> discarded.
>>
>> Place the code anywhere you modify your key bindings.
>>
>> In my case, I overwrite Ctrl+Q key.
>>
>> ----
>> -- Quit (save all unsaved files optionally)
>>
>> keys["cq"] = function()
>>   local quit = function()
>>     -- Clear modification flags for all buffers first
>>
>>     for _, buf in ipairs(_BUFFERS) do
>>       buf:set_save_point()
>>     end
>>
>>     quit() -- Original
>>   end
>>
>>   --
>>
>>   local unsaved = {}
>>
>>   for _, buf in ipairs(_BUFFERS) do
>>     if buf.filename and buf.modify then table.insert(unsaved,
>> buf.filename) end
>>   end
>>
>>   if #unsaved > 0 then
>>     local ans = ui.dialogs.msgbox{ title = "Quit?",
>>                                    text = "The following buffers were
>> not saved: \n\n\t" ..
>> table.concat(unsaved, "\n\t") ..
>>                                             "\n",
>>                                           icon  = "gtk-dialog-question",
>>                                           button2 = "Cancel",
>>                                           button3 = "Quit anyway",
>>                                           button1 = "Save unsaved
>> files & quit",
>>                                  }
>>
>> --ui.print(ans); do return  end
>>
>>     if ans == -1 or ans == 2 then
>>       -- Cancel - do not do anything
>>     elseif ans == 1 then
>>       -- Save & quit
>>       io.save_all_files()
>>       quit()
>>     elseif ans == 3 then
>>       -- Quit anyway
>>       quit()
>>     end
>>   else
>>     -- Nothing to save
>>     quit()
>>   end
>> end
>> ----
>>
>> Any feedback is welcome.
>>
>> Best regards,
>>
>> Lukas
>>
>>
>> On Tue, 13 Aug 2019 22:52:10 +0200, Thorsten Kampe
>> <thorsten.att.thorstenkampe.de> wrote:
>>
>>> Hi,
>>>
>>> when trying to close the editor or document, I get the options
>>> "Quit without saving" and "Cancel". Every other editor I know,
>>> offers "Save and quit" or "Save and close". Why not Textadept?
>>>
>>> Regards, Thorsten
>>>
> I think you are potentially discarding new buffers with changes.
>
> You can change the code for something like this:
>
> if buf.modify then table.insert(unsaved, buf.filename or "New file") end
>
> Cheers,
>
> Gabriel
>
I just realized that io.save_all_files() does not request the file name
of buffers that do not have one.

So, more work is needed to solve this...

Try typing something in a new buffer and then exit using your function.

Cheers,

Gabriel

PS: sorry if you receive this mail twice, I use a wrong email account.

-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Tue 03 Sep 2019 - 11:13:28 EDT

This archive was generated by hypermail 2.2.0 : Wed 04 Sep 2019 - 06:51:21 EDT