5

I'm playing around with text parsing in the REPL, and sometimes want to dump in a bunch of data into a string, whether it's a bibtex entry or some EBNF notation etc. Typically there might be quotation marks in the string, and it's very tedious and error-prone to have to manually escape them..

Is there an alternative way of doing this, such as Ruby's %Q|I can use "Quotation Marks"| or heredocs etc? Or would it be possible to write a macro or modification of the reader to enable this?

timss
  • 9,982
  • 4
  • 34
  • 56
Stian Håklev
  • 1,240
  • 2
  • 14
  • 26
  • This is something I've wanted too. One way you can work around it for data is to read it in from a file with `slurp`. – DaoWen Apr 20 '13 at 01:10
  • Are you "dumping" in your runtime code or in your editor. If the later, see this question: http://stackoverflow.com/questions/11043318/does-clojure-have-raw-string – noahlz Apr 20 '13 at 09:47
  • What editor are you using? My usual approach for this sort of thing is to use an editor with a smart pasting function that adds the escapes for me. – Korny Jul 17 '16 at 06:58

2 Answers2

1

There has been some discussion about a more robust quoting syntax, but no changes to support this seem imminent.

In the meantime, to specifically handle the REPL interaction you mention, you might find this useful. Note it probably doesn't work for every REPL out there -- they don't all support read-line terribly well:

(defn read-lines []
  (->> (repeatedly read-line)
       (take-while #(not= % "."))
       (mapcat #(list % "\n"))
       (apply str)))

Use it by running (read-lines) at the REPL, pasting your content, and then adding a line with a . by itself:

user=> (read-lines)
  #_=> This "works"
  #_=> sometimes...
  #_=> .
"This \"works\"\nsometimes...\n"
user=> (print *1)
This "works"
sometimes...
nil
Chouser
  • 5,093
  • 1
  • 32
  • 24
0

How about simply using a temp-file into which you dump your text, and then read it from there using slup? That way everything is automatically escaped whenever you call it?

(slurp "tempfile.txt")

or

(def data (slurp "tempfile.txt"))

or

(defn rd [] (def data (slurp "tempfile.txt")))
Terje Dahl
  • 942
  • 10
  • 20