Arc Forumnew | comments | leaders | submitlogin
6 points by waterhouse 5081 days ago | link | parent

Function: (cp) : copy-paste. Runs the code in your clipboard. Write code in your favorite text editor, select it, copy it, then go to the REPL and type in (cp) instead of pasting it in. Useful when a) your terminal doesn't like it when you paste in multilined input or b) you don't want to clutter up your REPL interactions. Example implementation and usage:

  (def cp () (map eval (readall:clipboard)))

  ;Now I redefine a couple of functions (e.g. to add or remove printlining)
  ; and copy the new definitions, then use (cp) at the REPL.
  arc> (cp)
  *** redefining int-nthroot
  *** redefining cint-nthroot
  *** redefining cint-omega
  (#<procedure: int-nthroot> #<procedure: cint-nthroot> #<procedure: cint-omega>)
Obviously, the above requires a (clipboard) function, which is useful in its own right:

Function: (clipboard) : Returns the clipboard as a string. Useful when you want to give your program a string from somewhere (maybe capture it with (= x (clipboard)) and then use it), and your alternatives are a) pasting it in literally (which requires escaping), b) pasting to a file and reading it in, and c) trying to call (readline) a bunch of times or something. Also useful in implementing (cp).

(clipboard) probably takes a platform-specific system call. On Mac OS X, the shell command "pbpaste" prints out the clipboard, so I have it defined as:

  (def clipboard () (tostring:system "pbpaste"))
(In fact, since I use "pbpaste" at the terminal, I find the name 'pbpaste more mnemonic.) It seems that on Linux, you can install a utility named 'xclip', and 'xclip -o' outputs the clipboard. I don't know about Windows. Alternatively, if you're using the Racket GUI libraries, there's some platform-independent way to get the clipboard string. And, for completeness, I'll mention pbcopy, which isn't as useful:

Function: (pbcopy x) : Coerces x to a string and sets the clipboard to that string. Named after the MacOSX utility. Useful mainly so I can add two spaces to each line when pasting code into this forum:

  arc> (pbcopy:tostring:map [prn "  " _] (lines:pbpaste))
  nil
Implementation: To avoid issues with escaping and stuff, what you probably want to do is write the string to a temporary file, then do "cat tmpfile | pbcopy" (where "pbcopy" might be replaced with "xclip" on Linux and whatever on Windows).

  (def pbcopy (s)
    (let f (tmpfile)
      (w/outfile gf f (disp s gf))
      (system:string "cat " f " | pbcopy")
      rmfile.f))

  (def tmpfile ()
    (let u (string "/tmp/arctmp" (rand-string 10))
      (if (file-exists u)
          (tmpfile)
          u)))