Posted by
| Nick Gammon
Australia (22,981 posts) bio
Forum Administrator |
Message
| Er, well there is not a built-in function to do exactly that.
However this particular problem was solved a while back when we wrote a plugin to copy selected text onto the clipboard. To save hunting around for it, the relevant code is below (this is in a Lua script). (From http://www.gammon.com.au/forum/?id=8052&page=2)
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
local copystring = ""
-- iterate to build up copy text
for line = first_line, last_line do
if line < last_line then
copystring = copystring .. GetLineInfo(line).text:sub (first_column) -- copy rest of line
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
copystring = copystring .. GetLineInfo(line).text:sub (first_column, last_column - 1)
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
-- finally can set clipboard contents
SetClipboard(copystring)
end -- function CopyScript
The two relevant lines in bold, you would need to change to do whatever you want to do in the alias. The first is when nothing is selected, the second is to handle the selection. Instead of SetClipboard you would do whatever you wanted with that word or string.
Probably it could be reworked like this:
function GetSelection()
-- 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
return nil
end -- if nothing to copy from output window
local copystring = ""
-- iterate to build up copy text
for line = first_line, last_line do
if line < last_line then
copystring = copystring .. GetLineInfo(line).text:sub (first_column) -- copy rest of line
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
copystring = copystring .. GetLineInfo(line).text:sub (first_column, last_column - 1)
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
-- finally return the result
return copystring
end -- function GetSelection
Now if you call GetSelection from your alias it will return nil for no selection, or the selected string. |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|