Clipboard tip

If copying text from like a browser to swi-prolog command line in the native GUI you could use these (uses XPCE object).

clipboard_copy(C):-
    send( '@'(display),copy,C).

clipboard_paste(E):-
    get('@'(display),cut_buffer,D), get(D,value,E).

What this fixes? If clipboard is pasted from ctrl+v (in Windows) to insert it to some variable you get sometimes errors for example below.

Or am I missing something, how would I read with read_term/2 some text from external origin that would not get mixed up by character escapes.

42 ?- A='LOGOUT Response: * BYE Logging outLIST Response: * LIST (\HasNoChildren \UnMarked) "/" Archives/2019'.
ERROR: Syntax error: Unknown character escape in quoted atom or string: `\H'
ERROR: A='LOGOUT Response: * BYE Logging outLIST Response: * LIST (
ERROR: ** here **
ERROR: \HasNoChildren \UnMarked) "/" Archives/2019' . 
42 ?- clipboard_paste(A).
A = 'LIST Response: * LIST (\\HasNoChildren \\UnMarked) "/" Archives/2019'

There is no need to quote this. If you also load library(pce) you can write the prettier @display

Good question. For swipl-win I guess we could add something like SHIFT-Ctrl-V to paste in quoted form? For a normal console it gets harder. We could add a command to the command line editor? I recently found myself in a similar situation debugging parsing expressions from the JavaScript mathlife editor.

This seems so useful that I added ^Y to the libedit command line editor to insert the current selection as Prolog quoted value. The quote used is the character before the caret, or single quoted if this is not a quote. So, to paste as back quoted code string, use

 `^Y

For the Windows console we could probably do something similar to normal ^V: paste quoted if the character before the caret is a quote?

1 Like

Maybe using the character_escapes flag ?

I was trying to apply this patch but seems a bit convoluted… I’ll let you know,

1 Like

For now, I extended swipl-win.exe with a ^Y command that is close to what libedit does on Unix-like systems, i.e.:

  • If the character before the caret is a quote, insert quoted using that quote.
  • Otherwise insert quoted using a single quote.

The difference is that libedit uses Prolog to implement the command and does the quoting using write_term/2, while swipl-win simply puts a \ before occurrences of the quote character as well as \ in the input.

Possibly you can copy some of that to the Qt based console …

1 Like

I came up with this, should read whatever is written or pasted, newline and ctrl+d ends the input. Probaply the current_input’s line buffer (4096 characters ) limits how long the atom can be, with very long inputs I noticed problems.

read_atom(A):-
    new_memory_file(B),
    open_memory_file(B,write,Stream),
    copy_stream_data(current_input,Stream),
    close(Stream),
    memory_file_to_atom(B,A),
    free_memory_file(B).