Re: [code] Close editor when unsaved changes

From: Procházka Lukáš Ing. <LPr.att.pontex.cz>
Date: Tue, 03 Sep 2019 17:56:32 +0200

Hello,

On Tue, 03 Sep 2019 16:57:48 +0200, Gabriel Dubatti <gdubatti.att.gmail.com> wrote:

> Hi Lukas,
>
> El 03/09/2019 a las 10:35, Procházka Lukáš Ing. escribió:
> 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

a slight patch with prompt to discard content of unsaved buffers (also Alt+F4 binding: keys.af4 = keys.cq):

----
-- 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, unsaved_untit_bufs = {}, {}
    for _, buf in ipairs(_BUFFERS) do
      if buf.modify then
        if buf._type then
          -- Buffers like [Message Box] - ignore
        else
          local fn = buf.filename
          if not fn then
            table.insert(unsaved_untit_bufs, buf)
            fn = "(Untitled buffer #" .. #unsaved_untit_bufs .. ")"
          end
          table.insert(unsaved, fn)
        end
      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
        if #unsaved_untit_bufs > 0 then
          local ans2 = ui.dialogs.ok_msgbox{ title = "Discard untitled buffers?",
                                             text = #unsaved_untit_bufs > 1 and
                                                      "There are " .. #unsaved_untit_bufs .. " untitled unsaved buffers.\n " ..
                                                      "Discard content of these buffers?\n" or
                                                      "There is " .. #unsaved_untit_bufs .. " untitled unsaved buffer.\n " ..
                                                      "Discard content of the buffer?\n",
                                           }
          if ans2 ~= 1 then ans = nil end
        end
        if ans == 1 then
          io.save_all_files()
          quit()
        end
      elseif ans == 3 then
        -- Quit anyway
        quit()
      end
    else
      -- Nothing to save
      quit()
    end
end
keys["af4"] = keys["cq"]
----
Cheers,
Lukas
(@Gabriel: Sorry for sending twice - the same mistake.)
> Cheers,
>
> Gabriel
-- 
Ing. Lukáš Procházka | mailto:LPr.att.pontex.cz
Pontex s. r. o.      | mailto:pontex@pontex.cz | http://www.pontex.cz | IDDS:nrpt3sn
Bezová 1658
147 14 Praha 4
Mob.: +420 702 033 396
-- 
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:56:32 EDT

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