Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Text transformation in EMACS

8 views
Skip to first unread message

Michael Haensel

unread,
Aug 11, 2011, 1:53:22 AM8/11/11
to
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.

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

Mario Lassnig

unread,
Aug 11, 2011, 2:50:43 AM8/11/11
to
On 11-8-11 7:53 , Michael Haensel wrote:
> 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?

Have you tried C-q C-J to directly "encode" the newline in the string?

Pascal J. Bourguignon

unread,
Aug 11, 2011, 8:01:12 AM8/11/11
to
Michael Haensel <mhaen...@gmail.com> writes:

> 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 {}.

Ivan Shmakov

unread,
Aug 11, 2011, 8:49:21 AM8/11/11
to
>>>>> Pascal J Bourguignon <p...@informatimago.com> writes:

[…]

> (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

Michael Haensel

unread,
Aug 11, 2011, 1:51:34 PM8/11/11
to
Hello -

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

William F Hammond

unread,
Aug 12, 2011, 3:24:18 PM8/12/11
to
Michael Haensel <mhaen...@gmail.com> writes:

> 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


0 new messages