3

I know, I know. "What new-line state?", you ask. Well, let me show you:

append [] w: first new-line [hello] on
== [
    hello
]

W is a now a word that creates a new-line when appended to a block.

You can turn it off, for example this way in Rebol 3:

append [] to word! w
== [hello]

But I have not found a good way to turn it on. Can you, Rebol guru?

Clarification: I am looking for some 'f such that:

append [] f to word! "hello"

has a new-line in it.

MarkI
  • 347
  • 2
  • 8
  • I can not observe in Rebol2 what you describe. **append [] to word! w** still gives the same block with the newline marker set to on. – sqlab Jan 22 '15 at 01:00
  • Sorry. Updated. You can try `append [] to word! to string! w` in Rebol 2. – MarkI Jan 22 '15 at 04:14

2 Answers2

2

Seemingly the new-line state is assigned to the word based on whether the assigned value has a preceding new-line at the time of assignment (I'll ruminate on the correctness of that statement for a while).

This function reassigns the same value (should retain context) to the word with a new new-line state:

set-new-line: func [
    'word [word!]
    state [logic!]
][
    set/any word first new-line reduce [get/any word] state
]

We can also test the new-line state of a given word:

has-new-line?: func [
    'word [word!]
][
    new-line? reduce [get/any word]
]

In use:

>> x: "Foo"
== "Foo"

>> has-new-line? x
== false

>> reduce [x]
== ["Foo"]

>> set-new-line x on
== "Foo"

>> has-new-line? x   
== true

>> reduce [x]        
== [
    "Foo"
]

>> reduce [set-new-line x on set-new-line x off set-new-line x on]  
== [
    "Foo" "Foo"
    "Foo"
]
  • should work in and

  • appears to work in v0.6.1 with one caveat: new-line? seems always to return true

rgchris
  • 3,698
  • 19
  • 19
  • this does not work, if x is a block of word, as in the question – sqlab Jan 22 '15 at 07:41
  • @sqlab In the question case, the word **hello** doesn't hold the new-line state, it's the word **x**. There's nothing you can do to **hello** to change the state. You can do **to string!** but then **hello** is a whole new word without the same context. – rgchris Jan 22 '15 at 07:45
  • There's two ways I see to interpret the question—as I see it, I've addressed the more likely: **w: first new-line [hello] on set-new-line w off append [] w** The other would be more direct, but doesn't actually answer the question: **new-line append [] w: first new-line [hello] on off** – rgchris Jan 22 '15 at 07:49
  • Or indeed: **w: 'hello set-new-line w on append [] w** —if the functions above aren't the answer, then the question is wrong!!! – rgchris Jan 22 '15 at 07:52
  • Consider: **x: "Foo" y: :x probe same? x y set-new-line x on probe reduce [has-new-line? x has-new-line? y] probe same? x y** Messy! – rgchris Jan 22 '15 at 07:57
  • I do not know the intention of the question, but I would expect something like **append [] the-requested-function to word! w** as an answer – sqlab Jan 22 '15 at 08:04
  • @sqlab would need a little clarification from marki —hopefully there's enough here to go on with... – rgchris Jan 22 '15 at 08:16
  • Incidentally @marki, you should have enough rep points to join [Rebol/Red chat](http://chat.stackoverflow.com/rooms/291/rebol-and-red). – rgchris Jan 22 '15 at 08:19
2

To answer the original question (which I know, because I asked it, in another incarnation):

f: func [w][first new-line append copy [] :w on]
MarkI
  • 132
  • 1
  • 7