Re: [code][textadept] "standard_dropdown" dialog broken (on W32)

From: Peter Rolf <indiego.att.gmx.net>
Date: Mon, 20 Nov 2017 19:29:37 +0100

Am 20.11.2017 um 17:20 schrieb Mitchell:
> Hi Peter,
>
> On Mon, 20 Nov 2017, Peter Rolf wrote:
>
>> Hi Mitchell,
>>
>> I tried to replace my old three button dialog with
>> "ui.dialogs.standard_dropdown" (I need at least four different values),
>> but no success. The attached code snippet shows the problem (resulting
>> 'button' values in the comments).
>
> `ui.dialogs.standard_dropdown()` returns two values. It appears your
> code only asks for the first one (button). Something like:
>
>   local button, result = ui.dialogs.standard_dropdown{...}
>
> should work.
>
> Cheers,
> Mitchell

I does (the following 'converted' code is the proof). Sorry for the noise.

if _L["convert to spaces"]:find("^No Localization") then
    _L["convert to spaces"] = "Convert to spaces"
    _L["prefered tab width"] = "\n\tConverts an elastic_tabstop text to
a space only representation.\t\n\tApart from the line indentation every
other tabstop alignment\t\n\twill be destroyed in this
process.\t\n\n\tChoose the prefered number of spaces for the indentation.\n"
end

-- Converts elastic_tabstop texts to a space only representation.
local function convert_all_tabs()
    local current_tab_width = buffer.tab_width
    local select = 3
    if current_tab_width == 2 then select = 1
    elseif current_tab_width == 3 then select = 2
    elseif current_tab_width == 4 then select = 3
    elseif current_tab_width == 8 then select = 4
    end

    local button, result = ui.dialogs.standard_dropdown{
        title = _L["convert to spaces"], text = _L["prefered tab width"]
.. string.format("\tCurrent default value: [ %d ]\n", current_tab_width),
        items = {"2", "3", "4", "8"},
        select = select, -- use the current buffer.tab_width value as
default
        string_output = true, -- return label instead of index
    }

    if (button == "gtk-cancel") or (not button) then return -1 end --
[Cancel] or dialog closed
    buffer.tab_width = result

    local find = ui.find
    -- save actual settings (global)
    local save_regex, save_use_tabs = find.regex, buffer.use_tabs
    find.regex = true -- needed ("\t" + "*")
    buffer.use_tabs = false -- an undo will not reset this setting!

    buffer:begin_undo_action()
    -- converts indentation tab to n spaces (n = buffer.tab_width)
    textadept.editing.convert_indentation() -- always buffer wide
    -- conversion rules for the remaining tabs (order is crucial)
    local conversion_rules = { {"\t\t\t*", "\t"}, {"\t,", ","}, {"\t", "
"} }
    -- 1. replace multiple occurences of tab by a single tab
    -- 2. remove tab in front of a comma (no extra separation needed)
    -- 3. all remaining (single) tabs are converted to a single space
    for i = 1, #conversion_rules do
        find.find_entry_text = conversion_rules[i][1] -- search
        find.replace_entry_text = conversion_rules[i][2] -- replace
        find.replace_all() -- selection only or buffer wide (no selection)
    end
    buffer:end_undo_action()

    -- restore prior settings
    find.regex, buffer.use_tabs = save_regex, save_use_tabs

    return buffer.tab_width -- -1 if dialog is closed or cancelled
end
keys[not OSX and not CURSES and "cf8" or "mf8"] = convert_all_tabs

-- 
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 Mon 20 Nov 2017 - 13:29:37 EST

This archive was generated by hypermail 2.2.0 : Tue 21 Nov 2017 - 06:54:27 EST