# Clipboard tip

**URL:** <https://swi-prolog.discourse.group/t/clipboard-tip/8741>\
**Category:** Nice to know\
**Created:** [January 21, 2025, 4:44pm UTC](https://swi-prolog.discourse.group/t/clipboard-tip/8741 "2025-01-21T16:44:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![PasiK](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pasik/32/84_2.png) [@PasiK](https://swi-prolog.discourse.group/u/PasiK)\
**Post date:** [January 21, 2025, 4:44pm UTC](https://swi-prolog.discourse.group/t/clipboard-tip/8741/1 "2025-01-21T16:44:15Z")

</div>

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

```prolog
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.

```prolog
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'

```

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 22, 2025, 9:51am UTC](https://swi-prolog.discourse.group/t/clipboard-tip/8741/2 "2025-01-22T09:51:24Z")

</div>

> [@PasiK](#):
>
> `'@'`

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

> [@PasiK](#):
>
> how would I read with [read\_term/2](https://www.swi-prolog.org/pldoc/doc_for?object=read_term/2) some text from external origin that would not get mixed up by character escapes

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](https://cortexjs.io/mathlive/) 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?

---

<div class="post-metadata">

**Author:** ![CapelliC](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/capellic/32/17_2.png) [@CapelliC](https://swi-prolog.discourse.group/u/CapelliC)\
**Post date:** [January 22, 2025, 3:43pm UTC](https://swi-prolog.discourse.group/t/clipboard-tip/8741/3 "2025-01-22T15:43:28Z")

</div>

> [@PasiK](#):
>
> how would I read with [read\_term/2](https://www.swi-prolog.org/pldoc/doc_for?object=read_term/2) some text from external origin

Maybe using the [character\_escapes](https://www.swi-prolog.org/pldoc/man?section=flags#flag:character_escapes) flag ?

> [@jan](#):
>
> For swipl-win I guess we could add something like SHIFT-Ctrl-V to paste in quoted form?

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

---

<div class="post-metadata">

**Author:** ![jan](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/jan/32/4_2.png) [@jan](https://swi-prolog.discourse.group/u/jan)\
**Post date:** [January 22, 2025, 5:51pm UTC](https://swi-prolog.discourse.group/t/clipboard-tip/8741/4 "2025-01-22T17:51:42Z")

</div>

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 …

---

<div class="post-metadata">

**Author:** ![PasiK](https://yyz2.discourse-cdn.com/free1/user_avatar/swi-prolog.discourse.group/pasik/32/84_2.png) [@PasiK](https://swi-prolog.discourse.group/u/PasiK)\
**Post date:** [January 23, 2025, 11:33am UTC](https://swi-prolog.discourse.group/t/clipboard-tip/8741/5 "2025-01-23T11:33:39Z")

</div>

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.

```prolog
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).

```
