Posted by
| Nick Gammon
Australia (23,057 posts) Bio
Forum Administrator |
Message
| The plugin below lets you select text in the output window, and then put suitable colour codes in front of styles runs, for copying and pasting in colour. This particular one is customized for Aardwolf, but by changing the conversion table a little way in, should be adaptable for other colour schemes.
To use, copy between the lines and save as Copy_Colour_Output.xml
Then use File -> Plugins to install that as a plugin.
To activate it, select some text and then Ctrl+Left-click and choose "Copy with Colour", or press Ctrl+D.
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE muclient>
<!-- Plugin "CopyScript" generated by Plugin Wizard -->
<!-- Amended slightly by Nick Gammon, from Worstje's version, on 17 Feb 2008 -->
<muclient>
<plugin
name="Copy_Colour_Output"
author="Nick Gammon"
id="04d9e64f835452b045b427a7"
language="Lua"
purpose="Copies output with colour codes"
save_state="n"
date_written="2008-07-28"
requires="4.00"
version="1.1"
>
<description trim="y">
Copies text from the output window to the clipboard, with
Aardwolf colour codes.
To use: highlight text and then:
Ctrl+D copy (like Ctrl+C but different)
or: Ctrl + Left-click and select "Copy with Colour"
</description>
</plugin>
<aliases>
<alias
match="Copy_With_Colour:04d9e64f835452b045b427a7"
enabled="y"
omit_from_output="y"
sequence="100"
script="CopyScript"
name="Copy_With_Colour"
menu="y"
>
</alias>
</aliases>
<!-- Script -->
<script>
<![CDATA[
-- Thank you, Shaun Biggs, for taking your time to write the CopyScript
-- (formerly Copy2) function below. It was slightly altered by me to suit
-- my usage (wordwrapped lines and no \r\n at start of selection).
local BLACK = 1
local RED = 2
local GREEN = 3
local YELLOW = 4
local BLUE = 5
local MAGENTA = 6
local CYAN = 7
local WHITE = 8
-- how each colour is to appear (black is not supported on Aardwolf)
local conversion = {
[GetNormalColour (RED)] = "@r",
[GetNormalColour (GREEN)] = "@g",
[GetNormalColour (YELLOW)] = "@y",
[GetNormalColour (BLUE)] = "@b",
[GetNormalColour (MAGENTA)] = "@m",
[GetNormalColour (CYAN)] = "@c",
[GetNormalColour (WHITE)] = "@w",
[GetBoldColour (RED)] = "@R",
[GetBoldColour (GREEN)] = "@G",
[GetBoldColour (YELLOW)] = "@Y",
[GetBoldColour (BLUE)] = "@B",
[GetBoldColour (MAGENTA)] = "@M",
[GetBoldColour (CYAN)] = "@C",
[GetBoldColour (WHITE)] = "@W",
} -- end conversion table
function DoOneLine (styles, startcol, endcol)
-- remove unneeded style runs at the start
while next (styles) and startcol > styles [1].length do
startcol = startcol - styles [1].length
endcol = endcol - styles [1].length
table.remove (styles, 1)
end -- do
-- nothing left? uh oh
if not next (styles) then return end
-- discard unwanted part of first good style
if startcol > 1 then
styles [1].length = styles [1].length - startcol
endcol = endcol - startcol + 1
styles [1].text = styles [1].text:sub (startcol)
startcol = 1
end -- if
-- copy appropriate styles and codes into the output
while next (styles) do
local len = endcol - startcol + 1
if len < 0 or endcol < 1 then
return
end -- done
-- last style?
if len < styles [1].length then
styles [1].length = len
styles [1].text = styles [1].text:sub (1, len)
end -- if last style
-- fixup string first - change @ to @@ and ~ to @-
local text = string.gsub (styles [1].text, "@", "@@")
text = string.gsub (styles [1].text, "~", "@-")
-- put code in front, if we can find one
local code = conversion [styles [1].textcolour]
if code then
copystring = copystring .. code
end -- if code found
-- now the text
copystring = copystring .. text
-- less to go now
endcol = endcol - styles [1].length
-- done this style
table.remove (styles, 1)
end -- while
end -- DoOneLine
function CopyScript(name, line, wildcs)
-- find selection in output window, if any
local first_line, last_line = GetSelectionStartLine(),
math.min (GetSelectionEndLine(), GetLinesInBufferCount ())
local first_column, last_column = GetSelectionStartColumn(), GetSelectionEndColumn()
-- nothing selected, do normal copy
if first_line <= 0 then
DoCommand("copy")
return
end -- if nothing to copy from output window
copystring = ""
-- iterate to build up copy text
for line = first_line, last_line do
if line < last_line then
DoOneLine (GetStyleInfo(line), first_column, GetLineInfo(line).length)
first_column = 1
-- Is this a new line or merely the continuation of a paragraph?
if GetLineInfo (line, 3) then
copystring = copystring .. "\r\n"
end -- new line
else
DoOneLine (GetStyleInfo(line), first_column, last_column)
end -- if
end -- for loop
-- Get rid of a spurious extra new line at the start.
if copystring:sub (1, 2) == "\r\n" then
copystring = copystring:sub (3)
end -- if newline at start
if copystring:sub (-2) ~= "@w" then
copystring = copystring .. "@w"
end -- if newline at start
-- finally can set clipboard contents
SetClipboard(copystring)
end -- function CopyScript
AcceleratorTo ("Ctrl+D", "CallPlugin ('04d9e64f835452b045b427a7', 'CopyScript', '')", sendto.script)
]]>
</script>
</muclient>
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|