Plugin to copy from output window and insert colour codes

Posted by Nick Gammon on Mon 28 Jul 2008 07:21 AM — 3 posts, 15,778 views.

Australia Forum Administrator #0
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>


Amended on Wed 30 Jul 2008 05:42 AM by Nick Gammon
USA Global Moderator #1
This appears to be chopping off the ends of some lines.


Quote:
Shakey Speare says, "You look like a kindly sort. Although tis true I can
barely see! Would you wish to help feed some women and children? Say 'I
pledge' if you do so wish."


becomes

Quote:
@CShakey Speare says, @c"@CYou look like a kindly sort. Although tis true I ca
@C barely see! Would you wish to help feed some women and children? Say '
@C pledge' if you do so wish.@c"@w


Notice the missing "n" and "I" from the ends of the first two lines.
Australia Forum Administrator #2
Hmm - when I tested it there must have always been an empty style on the end.

I have amended the plugin to correct that, in particular this line now has a +1 on it:


local len = endcol - startcol + 1


After all, if the starting and ending columns are both 1, then the length is 1, not zero.