Recursive indenting of lines in a string?
Posted: Wed Oct 03, 2007 1:22 am
(setq foo "a\nb\nc\n")
(indent foo "\t\t") => "\t\ta\n\t\tb\n\t\tc\n"
Hi guys. I've been fiddling with "replace" and using regex functionality, but not sure how to make it do what I want above.
I want the start of every line to have some indent characters inserted, but if it ends with a newline, I don't want indent characters inserted after the last newline. And there isn't a newline at the beginning of the string, but I want the indent characters there. This is as close as I made it:
(replace "(^|\n)" foo "\\n\t\t" 0) => "\\n\t\ta\\n\t\tb\\n\t\tc\\n\t\t"
Can someone show me what is the right way to define the "indent" function? And if it was robust for various values of newline, such as \n\r, \r\n, and \r, that would be great too. Empty lines don't need any indent characters added, which may make it easier.
Also, would it make sense for this (indent) function to be part of the base install? Also, I am doing (constant 'cat append) to save typing when I want to join a bunch of strings; could that be made standard in subsequent release of newlisp as well?
Ted
(indent foo "\t\t") => "\t\ta\n\t\tb\n\t\tc\n"
Hi guys. I've been fiddling with "replace" and using regex functionality, but not sure how to make it do what I want above.
I want the start of every line to have some indent characters inserted, but if it ends with a newline, I don't want indent characters inserted after the last newline. And there isn't a newline at the beginning of the string, but I want the indent characters there. This is as close as I made it:
(replace "(^|\n)" foo "\\n\t\t" 0) => "\\n\t\ta\\n\t\tb\\n\t\tc\\n\t\t"
Can someone show me what is the right way to define the "indent" function? And if it was robust for various values of newline, such as \n\r, \r\n, and \r, that would be great too. Empty lines don't need any indent characters added, which may make it easier.
Also, would it make sense for this (indent) function to be part of the base install? Also, I am doing (constant 'cat append) to save typing when I want to join a bunch of strings; could that be made standard in subsequent release of newlisp as well?
Ted