diff --git a/lib/awful/prompt.lua b/lib/awful/prompt.lua index 5dac8164..25882169 100644 --- a/lib/awful/prompt.lua +++ b/lib/awful/prompt.lua @@ -83,6 +83,12 @@ -- -- @DOC_wibox_awidget_prompt_keypress_EXAMPLE@ -- +-- **highlighting**: +-- +-- The prompt also support custom highlighters: +-- +-- @DOC_wibox_awidget_prompt_highlight_EXAMPLE@ +-- -- @author Julien Danjou <julien@danjou.info> -- @copyright 2008 Julien Danjou -- @module awful.prompt diff --git a/tests/examples/wibox/awidget/prompt/highlight.lua b/tests/examples/wibox/awidget/prompt/highlight.lua new file mode 100644 index 00000000..1c273d80 --- /dev/null +++ b/tests/examples/wibox/awidget/prompt/highlight.lua @@ -0,0 +1,66 @@ +local parent = ... --DOC_NO_USAGE --DOC_HIDE +local wibox = require( "wibox" ) --DOC_HIDE +local awful = { prompt = require("awful.prompt") }--DOC_HIDE +local beautiful = require( "beautiful" ) --DOC_HIDE + + local amp = "&"..string.char(0x3B) + local quot = """..string.char(0x3B) + + local atextbox = wibox.widget.textbox() + + -- Create a shortcut function + local function echo_test() + awful.prompt.run { + prompt = "Echo: ", + text = 'a_very "nice" $SHELL && command', --DOC_HIDE + bg_cursor = "#ff0000", + -- To use the default `rc.lua` prompt: + --textbox = mouse.screen.mypromptbox.widget, + textbox = atextbox, + highlighter = function(b, a) + -- Add a random marker to delimitate the cursor + local cmd = b.."ZZZCURSORZZZ"..a + + -- Find shell variables + local sub = "%1" + cmd = cmd:gsub("($[A-Za-z][a-zA-Z0-9]*)", sub) + + -- Highlight " && " + sub = "%1" + cmd = cmd:gsub("( "..amp..amp..")", sub) + + -- Highlight double quotes + local quote_pos = cmd:find("[^\\]"..quot) + while quote_pos do + local old_pos = quote_pos + quote_pos = cmd:find("[^\\]"..quot, old_pos+2) + + if quote_pos then + local content = cmd:sub(old_pos+1, quote_pos+6) + cmd = table.concat({ + cmd:sub(1, old_pos), + "", + content, + "", + cmd:sub(quote_pos+7, #cmd) + }, "") + quote_pos = cmd:find("[^\\]"..quot, old_pos+38) + end + end + + -- Split the string back to the original content + -- (ignore the recursive and escaped ones) + local pos = cmd:find("ZZZCURSORZZZ") + b,a = cmd:sub(1, pos-1), cmd:sub(pos+12, #cmd) + return b,a + end, + } + end + +echo_test() --DOC_HIDE + +parent:add( wibox.widget { --DOC_HIDE + atextbox, --DOC_HIDE + bg = beautiful.bg_normal, --DOC_HIDE + widget = wibox.container.background --DOC_HIDE +}) --DOC_HIDE