Posted by
| Nick Gammon
Australia (23,120 posts) Bio
Forum Administrator |
Message
| It is not totally simple to do, because the colours may change (eg. by using triggers) to some colour that does not have an ANSI code.
However for the situation you describe, the plugin below should help you.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- MuClient version 4.14 -->
<!-- Plugin "CopyScript" generated by Plugin Wizard -->
<muclient>
<plugin
name="CopyColourCodes"
author="Nick Gammon"
id="cd4d50f5ffa67481d49243e6"
language="Lua"
purpose="Copies output from the output window with colour codes embedded"
save_state="n"
date_written="2007-11-12 08:00:00"
requires="4.00"
version="1.0"
>
</plugin>
<aliases>
<alias
match="^CopyColourCodes:Copy$"
enabled="y"
regexp="y"
menu="y"
name="Copy_Colour_Codes"
omit_from_output="y"
sequence="100"
script="CopyScript"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
Accelerator ("F8", "CopyColourCodes:Copy")
forecolour_lookup = {}
backcolour_lookup = {}
-- make table to convert RGB codes into ANSI codes
for i = 1, 8 do
forecolour_lookup [GetNormalColour (i)] = { code = ANSI (i + 29), bold = false }
forecolour_lookup [GetBoldColour (i)] = { code = ANSI (i + 29), bold = true }
backcolour_lookup [GetNormalColour (i)] = { code = ANSI (i + 39), bold = false }
end -- all 8 colours
-- output the ANSI code to set an option, if it is not already set
-- (or unset it, if it is not already unset)
function DoExtra (is_set, was_set, on_code, off_code)
if is_set and (not was_set) then
return ANSI (on_code) -- send setting code
end -- need to set
if (not is_set) and was_set then
return ANSI (off_code) -- send resetting code
end -- need to un-set it
return "" -- no change needed
end -- DoExtra
-- process each line in the output window
function ProcessEachLine (line)
local bold = false
for style = 1, GetLineInfo (line, 11) do
-- do text colour
fore = forecolour_lookup [GetStyleInfo (line, style, 14)]
if fore and fore.code ~= lastfore then
copystring = copystring .. fore.code
bold = fore.bold
lastfore = fore.code
end -- foreground colour change
-- do background colour
back = backcolour_lookup [GetStyleInfo (line, style, 15)]
if back and back.code ~= lastback then
copystring = copystring .. back.code
lastback = back.code
end -- foreground colour change
DoExtra (bold, lastbold, 1, 22)
lastbold = bold
-- do other styles (underline, blink, inverse)
bold = GetStyleInfo (line, style, 8) -- bold flag
copystring = copystring .. DoExtra (bold, lastbold, 1, 22)
lastbold = bold
ul = GetStyleInfo (line, style, 9) -- underline flag
copystring = copystring .. DoExtra (ul, lastul, 4, 24)
lastul = ul
blink = GetStyleInfo (line, style, 10) -- blink flag
copystring = copystring .. DoExtra (blink, lastblink, 3, 23)
lastblink = blink
inverse = GetStyleInfo (line, style, 11) -- inverse flag
copystring = copystring .. DoExtra (inverse, lastinverse, 7, 27)
lastinverse = inverse
-- now the text itself
copystring = copystring .. GetStyleInfo (line, style, 1)
end -- for each style
-- add line break
if GetLineInfo (line, 3) then
copystring = copystring .. "\r\n"
end -- if newline wanted
end -- ProcessEachLine
-- alias starts here
function CopyScript(name, line, wildcs)
local line1, line2 = GetSelectionStartLine(), GetSelectionEndLine()
if line1 == 0 then
utils.msgbox ("No text selected", "Cannot copy", "ok", "!")
return
end
-- start with ANSI reset
copystring = ANSI (0)
lastfore = forecolour_lookup [GetNormalColour (8)].code -- reset is white
lastback = backcolour_lookup [GetNormalColour (1)].code -- on black
lastbold = false
lastul = false
lastblink = false
lastinverse = false
for line = line1, line2 do
ProcessEachLine (line)
end
SetClipboard (copystring)
end -- function CopyScript
]]>
</script>
</muclient>
Copy between the lines, save as CopyColourCodes.xml, and load that into MUSHclient as a plugin.
Then, highlight the text you want converted (that is, select it), and then press Ctrl + left-mouse-button. (ctrl+click).
Select "Copy Colour Codes" from the menu that pops up, and it should have the ANSI codes imbedded in it. Also, it is bound to F8 using an Accelerator command, however I found that it didn't work unless you pressed F8 twice for some obscure reason.
It only copies whole lines, so if you wanted to copy part of a description you might need to delete the extra bits you don't want.
How it works is to look at the RGB codes for the colour style runs for each line, and tries to deduce what the original ANSI codes must have been and re-inserts them into the copied text. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|