unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* "C-a" the first character in each line?
@ 2007-06-24 13:20 Nikos Apostolakis
  2007-06-24 14:14 ` Drew Adams
  2007-06-25  9:36 ` Nikos Apostolakis
  0 siblings, 2 replies; 7+ messages in thread
From: Nikos Apostolakis @ 2007-06-24 13:20 UTC (permalink / raw)
  To: help-gnu-emacs

Hello group,

in the scratch buffer I did

-----------------
(while (search-forward-regexp "^\\(.\\)" nil t)
  (replace-match "\% \1"))
 
laa 
moo
nii
-----------------

I expected 

% 
% laa 
% moo
% nii

but isnstead I get 

% ^A
% ^Aaa 
% ^Aoo
% ^Aii

where ^A stands for the character "C-a".

What am I missing?

TIA,
Nikos

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "C-a" the first character in each line?
       [not found] <mailman.2564.1182691580.32220.help-gnu-emacs@gnu.org>
@ 2007-06-24 13:59 ` weber
  2007-06-25  1:32   ` Barry Margolin
  2007-06-24 14:17 ` Joost Kremers
  1 sibling, 1 reply; 7+ messages in thread
From: weber @ 2007-06-24 13:59 UTC (permalink / raw)
  To: help-gnu-emacs

On Jun 24, 10:20 am, Nikos Apostolakis <nikos...@gmail.com> wrote:
> Hello group,
>
> in the scratch buffer I did
>
> -----------------
> (while (search-forward-regexp "^\\(.\\)" nil t)
>   (replace-match "\% \1"))
>
> laa
> moo
> nii
> -----------------
>
> I expected
>
> %
> % laa
> % moo
> % nii
>
> but isnstead I get
>
> % ^A
> % ^Aaa
> % ^Aoo
> % ^Aii
>
> where ^A stands for the character "C-a".
>
> What am I missing?
>
> TIA,
> Nikos

Dunno why, but \\1 instead of \1 solves it.
Or alternatively:
(while (search-forward-regexp "^\\(.\\)" nil t)
  (replace-match (concat "% "(match-string 1))))
which looks better to me...

HTH,
weber

^ permalink raw reply	[flat|nested] 7+ messages in thread

* RE: "C-a" the first character in each line?
  2007-06-24 13:20 Nikos Apostolakis
@ 2007-06-24 14:14 ` Drew Adams
  2007-06-25  9:36 ` Nikos Apostolakis
  1 sibling, 0 replies; 7+ messages in thread
From: Drew Adams @ 2007-06-24 14:14 UTC (permalink / raw)
  To: Nikos Apostolakis, help-gnu-emacs

> (while (search-forward-regexp "^\\(.\\)" nil t)
>   (replace-match "\% \1"))
>  
> laa 
> moo
> nii
> -----------------
> 
> I expected 
> 
> % 
> % laa 
> % moo
> % nii
> 
> but isnstead I get 
> 
> % ^A
> % ^Aaa 
> % ^Aoo
> % ^Aii
> 
> where ^A stands for the character "C-a".
> 
> What am I missing?

^A is octal 1. You need \\1, not \1.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "C-a" the first character in each line?
       [not found] <mailman.2564.1182691580.32220.help-gnu-emacs@gnu.org>
  2007-06-24 13:59 ` "C-a" the first character in each line? weber
@ 2007-06-24 14:17 ` Joost Kremers
  2007-06-25  9:29   ` Nikos Apostolakis
  1 sibling, 1 reply; 7+ messages in thread
From: Joost Kremers @ 2007-06-24 14:17 UTC (permalink / raw)
  To: help-gnu-emacs

Nikos Apostolakis wrote:
> Hello group,
>
> in the scratch buffer I did
>
> -----------------
> (while (search-forward-regexp "^\\(.\\)" nil t)
>   (replace-match "\% \1"))
[...]
> % ^A
> % ^Aaa 
> % ^Aoo
> % ^Aii
>
> where ^A stands for the character "C-a".
>
> What am I missing?

it's actually what you're inserting: \1 *is* ^A:

(string-match "\1" "abc\x01")

==> 3

so what you need to do is escape the backslash:

(replace-match "% \\1")

btw, you are aware of the function string-insert-rectangle?


-- 
Joost Kremers                                      joostkremers@yahoo.com
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "C-a" the first character in each line?
  2007-06-24 13:59 ` "C-a" the first character in each line? weber
@ 2007-06-25  1:32   ` Barry Margolin
  0 siblings, 0 replies; 7+ messages in thread
From: Barry Margolin @ 2007-06-25  1:32 UTC (permalink / raw)
  To: help-gnu-emacs

In article <1182693577.251257.35950@u2g2000hsc.googlegroups.com>,
 weber <hugows@gmail.com> wrote:

> On Jun 24, 10:20 am, Nikos Apostolakis <nikos...@gmail.com> wrote:
> > Hello group,
> >
> > in the scratch buffer I did
> >
> > -----------------
> > (while (search-forward-regexp "^\\(.\\)" nil t)
> >   (replace-match "\% \1"))
> >
> > laa
> > moo
> > nii
> > -----------------
> >
> > I expected
> >
> > %
> > % laa
> > % moo
> > % nii
> >
> > but isnstead I get
> >
> > % ^A
> > % ^Aaa
> > % ^Aoo
> > % ^Aii
> >
> > where ^A stands for the character "C-a".
> >
> > What am I missing?
> >
> > TIA,
> > Nikos
> 
> Dunno why, but \\1 instead of \1 solves it.

Because \ is used as an escape prefix for both Elisp strings and regular 
expressions.  It's first processed by the Elisp string parser, which 
uses \<number> to represent characters by their ASCII code, and \\ to 
represent a literal \.  You need to use the latter to get the \ passed 
through to the regexp processor.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "C-a" the first character in each line?
  2007-06-24 14:17 ` Joost Kremers
@ 2007-06-25  9:29   ` Nikos Apostolakis
  0 siblings, 0 replies; 7+ messages in thread
From: Nikos Apostolakis @ 2007-06-25  9:29 UTC (permalink / raw)
  To: help-gnu-emacs

Joost Kremers <joostkremers@yahoo.com> writes:
>
> btw, you are aware of the function string-insert-rectangle?
>

Now I am!  Thanks alot for your answer and the tip.  

Nikos

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: "C-a" the first character in each line?
  2007-06-24 13:20 Nikos Apostolakis
  2007-06-24 14:14 ` Drew Adams
@ 2007-06-25  9:36 ` Nikos Apostolakis
  1 sibling, 0 replies; 7+ messages in thread
From: Nikos Apostolakis @ 2007-06-25  9:36 UTC (permalink / raw)
  To: help-gnu-emacs

Nikos Apostolakis <nikos.ap@gmail.com> writes:
> (while (search-forward-regexp "^\\(.\\)" nil t)
>   (replace-match "\% \1"))

[...]

>
> What am I missing?
>

So I was missing a backwards slash (plues some sleep but that's
another story ...)

Many thanks to all that answered.  

Nikos

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-06-25  9:36 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.2564.1182691580.32220.help-gnu-emacs@gnu.org>
2007-06-24 13:59 ` "C-a" the first character in each line? weber
2007-06-25  1:32   ` Barry Margolin
2007-06-24 14:17 ` Joost Kremers
2007-06-25  9:29   ` Nikos Apostolakis
2007-06-24 13:20 Nikos Apostolakis
2007-06-24 14:14 ` Drew Adams
2007-06-25  9:36 ` Nikos Apostolakis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).