I'm writing a small scenario for Blades of Avernum, and would like to
write less boilerplate syntax. I am developing a something to
transform text from a list of inputs into properly-formatted Avernum
script. (Avernum script is similar to C.) Newlines are causing me a
real problem! The output, from either a macro or function, looks like
this:
"begintalknode 1; ^N state = -1; ^N nextstate = 1; ^N"
It should look like:
begintalknode 1;
state = -1;
nextstate = 1;
The relevant code:
(concat
(format "begintalknode %d; ^N" Number)
(format "\tstate = %d; ^N" State))
Then ^Ns are a different color, indicating they are an embedded
newline character. That's great, but the text I need should actually
be _on separate lines_. The format command seems to ignore \n for
newlines.
So, I have two questions:
1. How can I actually produce newlines in the output instead of
embedded newline characters that don't actually go to the next line?
2. How can I embed quotes in the format string? I've tried (format "
\" "), but that displays " \" " as the result. I want a string with
contents: space quote space.
Thanks for any advice -
Michael Haensel
Have you tried C-q C-J to directly "encode" the newline in the string?
> Hello -
>
> I'm writing a small scenario for Blades of Avernum, and would like to
> write less boilerplate syntax. I am developing a something to
> transform text from a list of inputs into properly-formatted Avernum
> script. (Avernum script is similar to C.) Newlines are causing me a
> real problem! The output, from either a macro or function, looks like
> this:
>
> "begintalknode 1; ^N state = -1; ^N nextstate = 1; ^N"
>
> It should look like:
> begintalknode 1;
> state = -1;
> nextstate = 1;
>
> The relevant code:
> (concat
> (format "begintalknode %d; ^N" Number)
> (format "\tstate = %d; ^N" State))
>
> Then ^Ns are a different color, indicating they are an embedded
> newline character. That's great, but the text I need should actually
> be _on separate lines_. The format command seems to ignore \n for
> newlines.
1- there is no newline control code in ASCII.
2- C-n is not LF (line-feed) and neither CR (carriage-return), but SO
(shift-out).
3- where did you put a \n in your strings?
> So, I have two questions:
>
> 1. How can I actually produce newlines in the output instead of
> embedded newline characters that don't actually go to the next
> line?
In an emacs lisp string, a newline is represented with \n:
(format "line one\nline two") --> "line one
line two"
> 2. How can I embed quotes in the format string? I've tried (format "
> \" "), but that displays " \" " as the result. I want a string with
> contents: space quote space.
This is it's contents:
(require 'cl)
(defun char-code (ch) ch)
(map 'list 'char-code " \" ") --> (32 34 32)
--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
[…]
> (require 'cl)
> (defun char-code (ch) ch)
> (map 'list 'char-code " \" ") --> (32 34 32)
Huh?
(string-to-list " \" ")
=> (32 34 32)
To the OP: (insert " \" ") inserts “ " ” to the current buffer,
which I believe is what's needed.
--
FSF associate member #7257
The lines previously listed now look and work as designed:
(concat
(format "begintalknode %d;\n" Number)
(format "\tstate = %d;\n" State)
...
(format "\tquestion = \"%s\";\n" Question) // Note embedded
quotes
The solution is a combination of the suggestions offered. I had been
using the inferior EMACS LISP mode (M-x ielm), which does some
formatting on the strings it displays. That seems to be why the
newline wasn't actually going to the next line. Switching over to the
scratch buffer and using C-j to evaluate the function/test helped
considerably.
Once I changed that, several things related to quotes and newlines
became clear. Both \n and C-q C-J work for encoding a newline into the
format string. The scratch buffer displays the results by going to the
next line.
The quotes problem is a feature, not a bug. My function returns a
string containing the transformed text. Strings are displayed with
start and end quotes. Embedded quotes within strings are displayed as
\" but are actually a single character. I was able to confirm this
using (length "\"").
Thanks all for the help! This will significantly reduce the amount of
typing I have to do for my next script.
Michael Haensel
> 1. How can I actually produce newlines in the output instead of
> embedded newline characters that don't actually go to the next line?
> 2. How can I embed quotes in the format string? I've tried (format "
> \" "), but that displays " \" " as the result. I want a string with
> contents: space quote space.
'format' produces an elisp string, which is why the escaped quote
remains escaped. Following 'format' with 'princ', which gives the
printed representation of any Elisp object, should do what you want.
But in so doing, don't confuse the output of 'princ' with its
return.
For illustration evaluate the following 'progn' that returns nil in
your scratch buffer or your mini-buffer.
(progn (princ (format "x \" y\n")) nil)
-- Bill