Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ MUSHclient ➜ Plugins ➜ Plugin to copy from output window and insert colour codes

Plugin to copy from output window and insert colour codes

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Mon 28 Jul 2008 07:21 AM (UTC)

Amended on Wed 30 Jul 2008 05:42 AM (UTC) by Nick Gammon

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

Posted by Fiendish   USA  (2,533 posts)  Bio   Global Moderator
Date Reply #1 on Wed 30 Jul 2008 03:00 AM (UTC)
Message
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.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,057 posts)  Bio   Forum Administrator
Date Reply #2 on Wed 30 Jul 2008 05:43 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


12,508 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.