Author Topic: How to get text from the clipboard?  (Read 5297 times)

Bamsen

  • Community Member
  • Posts: 66
  • Hero Points: 8
How to get text from the clipboard?
« on: July 08, 2012, 11:13:45 AM »
Is it possible to get text from the clipboard in to a string variable without pasting it to the current buffer?
I can't find the appropriate function.....
---
Morten

Graeme

  • Senior Community Member
  • Posts: 2796
  • Hero Points: 347
Re: How to get text from the clipboard?
« Reply #1 on: July 09, 2012, 01:17:41 AM »
You probably need to use _create_temp_view and _delete_temp_view to create a temporary non visible buffer.

hs2

  • Senior Community Member
  • Posts: 2761
  • Hero Points: 292
Re: How to get text from the clipboard?
« Reply #2 on: July 14, 2012, 08:02:15 AM »
@bamsen: What are you trying to do ? I mean do you really need the clipboard ?
Dealing with arbitrary clipboard contents (CHAR,LINE,BLOCK ?, Which (named) clipboard ?) has it's own subtle issues.
I guess this is the reason why there is no handy SE function to do something like what you want.
You've been warned ;)
HS2
« Last Edit: July 14, 2012, 08:04:19 AM by hs2 »

Bamsen

  • Community Member
  • Posts: 66
  • Hero Points: 8
Re: How to get text from the clipboard?
« Reply #3 on: July 14, 2012, 08:18:15 PM »
I must admit it was mostly out of curiosity. :)
I found commands to put stuff in the clipboard and was curious to why I could not find any to get it back out.

WadeHatler

  • Community Member
  • Posts: 19
  • Hero Points: 0
  • Been using VSlick since the dawn of time
Re: How to get text from the clipboard?
« Reply #4 on: August 22, 2012, 04:07:51 AM »
It's kind of funny.  I wrote the function to get text from the clipboard years ago, but I hardly ever use it.  Most of the time, if I want to do something with the clipboard I find it much more effective to just put it in a buffer.

I start with a _command I use dozens of times a day that just inserts the current clipboard into a new window. 

Code: [Select]
/**
 * Insert the text in the clipboard into a newly created window.  Useful as a quick pseudo-clipboard viewer,
 * and for quick edits to text that's in the clipboard.
 */
_command void InsertClipboardIntoNewWindow() name_info(',') {
    _str clipFile = _temp_path() '\Clipbrd.txt';
    if(file_exists(clipFile))
        delete_file(clipFile);
    e(clipFile);

    top();
    paste();
    p_line = 1;

    // If the text is from a line clipboard, an extra blank line was added at the top of the file, so delete
    // it.  Only happens when we have line selections in VSlick.
    if (clipboard_itype(0) == 'LINE' && GetWorkline() == '')
        delete_line();

    top();
    convert_tabs2spaces();
    save();
}

I have that one in my customized menu and bound to a key because I use it so much.

The function to get the string isn't all that handy because I find most of my string manipulations work better in a buffer than in a string,s o I usually just load it and work with it.  When I do need the string, I use this function.  This is a chopped down version of my function (because I have a lot of dependancies on internal libraries) that I think works, but haven't tested it.

Code: [Select]
/**
 * Get the text from a specific line of the clipboard, or the entire clipboard. Note that as a side-effect,
 * the original file is also stored in C:\Temp\Clip.tmp, where it can be processed in other ways.
 *
 * @return              Specified clipboard text as a string.  The same text is also stored in the global <code>Workline</code>
 */
_str ClipGet() {
    InsertClipboardIntoNewWindow();
_str result = get_text(200000);
close_window('W', false);
refresh('AW');
    return result;
}