* putting double quotes efficiently
@ 2013-06-03 5:43 C K Kashyap
2013-06-03 7:26 ` Andreas Röhler
0 siblings, 1 reply; 20+ messages in thread
From: C K Kashyap @ 2013-06-03 5:43 UTC (permalink / raw)
To: help-gnu-emacs
Hi,
I am looking for a way to transform
print line1
print line two
print line3
into
print "line1";
print "line two";
print "line3";
most efficiently. For the first quote of each line I place the cursor
before line1 and I set the mark and then move all the way down to just
before line3 and then do a C-x-r-t " <RET>
Then I do a regexp replace to change $ to ";
I'd like to do better. While at it, can I please also know how I can use
the line range in the mark set mode in user defined function?
Regards,
Kashyap
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 5:43 putting double quotes efficiently C K Kashyap
@ 2013-06-03 7:26 ` Andreas Röhler
2013-06-03 7:55 ` Andreas Röhler
0 siblings, 1 reply; 20+ messages in thread
From: Andreas Röhler @ 2013-06-03 7:26 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org List
Am 03.06.2013 07:43, schrieb C K Kashyap:
> Hi,
> I am looking for a way to transform
>
> print line1
> print line two
> print line3
>
> into
>
> print "line1";
> print "line two";
> print "line3";
>
> most efficiently. For the first quote of each line I place the cursor
> before line1 and I set the mark and then move all the way down to just
> before line3 and then do a C-x-r-t " <RET>
> Then I do a regexp replace to change $ to ";
>
> I'd like to do better. While at it, can I please also know how I can use
> the line range in the mark set mode in user defined function?
>
> Regards,
> Kashyap
>
M-x query-replace-regexp
^\([^ ]+\) \(.+\)
RET
\1 "\2;
RET
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 7:26 ` Andreas Röhler
@ 2013-06-03 7:55 ` Andreas Röhler
2013-06-03 8:03 ` C K Kashyap
0 siblings, 1 reply; 20+ messages in thread
From: Andreas Röhler @ 2013-06-03 7:55 UTC (permalink / raw)
To: help-gnu-emacs
Am 03.06.2013 09:26, schrieb Andreas Röhler:
> Am 03.06.2013 07:43, schrieb C K Kashyap:
>> Hi,
>> I am looking for a way to transform
>>
>> print line1
>> print line two
>> print line3
>>
>> into
>>
>> print "line1";
>> print "line two";
>> print "line3";
>>
>> most efficiently. For the first quote of each line I place the cursor
>> before line1 and I set the mark and then move all the way down to just
>> before line3 and then do a C-x-r-t " <RET>
>> Then I do a regexp replace to change $ to ";
>>
>> I'd like to do better. While at it, can I please also know how I can use
>> the line range in the mark set mode in user defined function?
>>
>> Regards,
>> Kashyap
>>
>
> M-x query-replace-regexp
>
> ^\([^ ]+\) \(.+\)
> RET
> \1 "\2;
> RET
>
>
resp. with missing " in replace expression
\1 "\2";
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 7:55 ` Andreas Röhler
@ 2013-06-03 8:03 ` C K Kashyap
2013-06-03 9:10 ` Andreas Röhler
[not found] ` <mailman.918.1370250440.22516.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 20+ messages in thread
From: C K Kashyap @ 2013-06-03 8:03 UTC (permalink / raw)
To: Andreas Röhler; +Cc: help-gnu-emacs
Thanks Andreas,
This works.
Although I'd still like to know how to write custom function that work over
a marked region.
Regards,
Kashyap
On Mon, Jun 3, 2013 at 1:25 PM, Andreas Röhler <
andreas.roehler@easy-emacs.de> wrote:
> Am 03.06.2013 09:26, schrieb Andreas Röhler:
>
> Am 03.06.2013 07:43, schrieb C K Kashyap:
>>
>>> Hi,
>>> I am looking for a way to transform
>>>
>>> print line1
>>> print line two
>>> print line3
>>>
>>> into
>>>
>>> print "line1";
>>> print "line two";
>>> print "line3";
>>>
>>> most efficiently. For the first quote of each line I place the cursor
>>> before line1 and I set the mark and then move all the way down to just
>>> before line3 and then do a C-x-r-t " <RET>
>>> Then I do a regexp replace to change $ to ";
>>>
>>> I'd like to do better. While at it, can I please also know how I can use
>>> the line range in the mark set mode in user defined function?
>>>
>>> Regards,
>>> Kashyap
>>>
>>>
>> M-x query-replace-regexp
>>
>> ^\([^ ]+\) \(.+\)
>> RET
>> \1 "\2;
>> RET
>>
>>
>>
> resp. with missing " in replace expression
>
> \1 "\2";
>
>
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 8:03 ` C K Kashyap
@ 2013-06-03 9:10 ` Andreas Röhler
2013-06-03 11:52 ` C K Kashyap
[not found] ` <mailman.918.1370250440.22516.help-gnu-emacs@gnu.org>
1 sibling, 1 reply; 20+ messages in thread
From: Andreas Röhler @ 2013-06-03 9:10 UTC (permalink / raw)
To: C K Kashyap; +Cc: help-gnu-emacs
Am 03.06.2013 10:03, schrieb C K Kashyap:
> Thanks Andreas,
> This works.
> Although I'd still like to know how to write custom function that work over
> a marked region.
For example that way:
(defun my-New-Command (beg end)
(interactive "r*")
(save-excursion
(save-restriction
(narrow-to-region beg end)
(goto-char beg)
(DO-What-I-Want...
Recommend a walk through Info section Elisp, you will find plenty of useful stuff.
Cheers,
Andreas
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 9:10 ` Andreas Röhler
@ 2013-06-03 11:52 ` C K Kashyap
0 siblings, 0 replies; 20+ messages in thread
From: C K Kashyap @ 2013-06-03 11:52 UTC (permalink / raw)
To: Andreas Röhler; +Cc: help-gnu-emacs
Thanks Andreas,
This is exactly what I was looking for -
(defun kash-util-quotes (beg end st en)
(interactive "r*\nsStart Pattern: \nsEnd Pattern: \n")
(save-excursion
(save-restriction
(string-rectangle beg end st)
; (narrow-to-region beg end)
(goto-char beg)
(replace-regexp "$" en)
)))
Thanks,
Kashyap
On Mon, Jun 3, 2013 at 2:40 PM, Andreas Röhler <
andreas.roehler@easy-emacs.de> wrote:
> Am 03.06.2013 10:03, schrieb C K Kashyap:
>
> Thanks Andreas,
>> This works.
>> Although I'd still like to know how to write custom function that work
>> over
>> a marked region.
>>
>
> For example that way:
>
> (defun my-New-Command (beg end)
> (interactive "r*")
> (save-excursion
> (save-restriction
> (narrow-to-region beg end)
> (goto-char beg)
> (DO-What-I-Want...
>
> Recommend a walk through Info section Elisp, you will find plenty of
> useful stuff.
>
> Cheers,
>
> Andreas
>
^ permalink raw reply [flat|nested] 20+ messages in thread
[parent not found: <mailman.918.1370250440.22516.help-gnu-emacs@gnu.org>]
* narrowing considered harmful (was: putting double quotes efficiently)
[not found] ` <mailman.918.1370250440.22516.help-gnu-emacs@gnu.org>
@ 2013-06-13 14:40 ` Stefan Monnier
2013-06-14 3:54 ` narrowing considered harmful Leo Liu
0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier @ 2013-06-13 14:40 UTC (permalink / raw)
To: help-gnu-emacs
BTW,
I tend to consider narrow-to-region in Elisp as a problem,so I encourage
you to write your code without it.
In your example, you don't need to narrow: the only thing you need is
to keep the "end" updated when the buffer is modified, which is easy to
do by changing the "end" form an integer to a marker.
Or alternatively, you can simply work your way backward, in which case
you don't need to worry about using a marker since the "start" position
to which you're going won't be affected by the buffer modifications.
Stefan
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: narrowing considered harmful
2013-06-13 14:40 ` narrowing considered harmful (was: putting double quotes efficiently) Stefan Monnier
@ 2013-06-14 3:54 ` Leo Liu
2013-06-14 13:00 ` Stefan Monnier
0 siblings, 1 reply; 20+ messages in thread
From: Leo Liu @ 2013-06-14 3:54 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
On 2013-06-13 22:40 +0800, Stefan Monnier wrote:
> I tend to consider narrow-to-region in Elisp as a problem,so I encourage
> you to write your code without it.
>
> In your example, you don't need to narrow: the only thing you need is
> to keep the "end" updated when the buffer is modified, which is easy to
> do by changing the "end" form an integer to a marker.
>
> Or alternatively, you can simply work your way backward, in which case
> you don't need to worry about using a marker since the "start" position
> to which you're going won't be affected by the buffer modifications.
Do you think it is a good idea to add a macro along these lines:
(defmacro with-markers (markerlist &rest body)
"Execute BODY with markers in MARKERLIST bound.
Each element of MARKERLIST is a symbol (which is bound to a
marker pointing to nowhere) or a list (SYMBOL EXPR) (which binds
SYMBOL to a marker pointing to the value of EXPR)."
(declare (indent 1) (debug (sexp body)))
`(let ,(mapcar (lambda (m)
(let ((m (if (listp m)
m
(list m))))
`(,(car m) (copy-marker ,(cadr m)))))
markerlist)
(unwind-protect
(progn ,@body)
,@(mapcar (lambda (m)
(let ((m (or (car-safe m) m)))
`(and (markerp ,m) (set-marker ,m nil))))
markerlist))))
Leo
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: narrowing considered harmful
2013-06-14 3:54 ` narrowing considered harmful Leo Liu
@ 2013-06-14 13:00 ` Stefan Monnier
2013-06-14 16:51 ` Leo Liu
0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier @ 2013-06-14 13:00 UTC (permalink / raw)
To: Leo Liu; +Cc: help-gnu-emacs
> Do you think it is a good idea to add a macro along these lines:
> (defmacro with-markers (markerlist &rest body)
I don't see the point. It only saves you the calls to `copy-marker'.
Stefan
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: narrowing considered harmful
2013-06-14 13:00 ` Stefan Monnier
@ 2013-06-14 16:51 ` Leo Liu
2013-06-15 2:43 ` Stefan Monnier
0 siblings, 1 reply; 20+ messages in thread
From: Leo Liu @ 2013-06-14 16:51 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
On 2013-06-14 21:00 +0800, Stefan Monnier wrote:
> I don't see the point. It only saves you the calls to `copy-marker'.
What about set-marker to nil?
Leo
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: narrowing considered harmful
2013-06-14 16:51 ` Leo Liu
@ 2013-06-15 2:43 ` Stefan Monnier
2013-06-16 16:42 ` Leo Liu
0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier @ 2013-06-15 2:43 UTC (permalink / raw)
To: help-gnu-emacs
>> I don't see the point. It only saves you the calls to `copy-marker'.
> What about set-marker to nil?
I wouldn't worry about it. It can even be a bad idea (set-marker to
nil requires scanning the whole list of markers, so it can take a while.
It can be more efficient to let the GC collect them later on).
Stefan
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: narrowing considered harmful
2013-06-15 2:43 ` Stefan Monnier
@ 2013-06-16 16:42 ` Leo Liu
2013-06-17 14:56 ` Stefan Monnier
0 siblings, 1 reply; 20+ messages in thread
From: Leo Liu @ 2013-06-16 16:42 UTC (permalink / raw)
To: Stefan Monnier; +Cc: help-gnu-emacs
On 2013-06-15 10:43 +0800, Stefan Monnier wrote:
> I wouldn't worry about it. It can even be a bad idea (set-marker to
> nil requires scanning the whole list of markers, so it can take a while.
> It can be more efficient to let the GC collect them later on).
>
>
> Stefan
Good to know this. The manual contains this:
,----[ (info "(elisp)Overview of Markers") ]
| ;; When you are finished using a marker, make it point nowhere.
| (set-marker m1 nil)
| => #<marker in no buffer>
`----
Should that be changed?
Leo
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: narrowing considered harmful
2013-06-16 16:42 ` Leo Liu
@ 2013-06-17 14:56 ` Stefan Monnier
2013-06-17 15:20 ` Drew Adams
0 siblings, 1 reply; 20+ messages in thread
From: Stefan Monnier @ 2013-06-17 14:56 UTC (permalink / raw)
To: help-gnu-emacs
>> I wouldn't worry about it. It can even be a bad idea (set-marker to
>> nil requires scanning the whole list of markers, so it can take a while.
>> It can be more efficient to let the GC collect them later on).
>>
>>
>> Stefan
> Good to know this. The manual contains this:
> ,----[ (info "(elisp)Overview of Markers") ]
> | ;; When you are finished using a marker, make it point nowhere.
> | (set-marker m1 nil)
> | => #<marker in no buffer>
> `----
> Should that be changed?
Not necessarily: whether it's better to nil them explicitly or to leave
them as they are depends on the specific case.
Stefan
^ permalink raw reply [flat|nested] 20+ messages in thread
* RE: narrowing considered harmful
2013-06-17 14:56 ` Stefan Monnier
@ 2013-06-17 15:20 ` Drew Adams
2013-06-17 16:48 ` Stefan Monnier
0 siblings, 1 reply; 20+ messages in thread
From: Drew Adams @ 2013-06-17 15:20 UTC (permalink / raw)
To: Stefan Monnier, help-gnu-emacs
> >> It can even be a bad idea (set-marker to nil requires scanning
> >> the whole list of markers, so it can take a while. It can be
> >> more efficient to let the GC collect them later on).
>
> > Good to know this. The manual contains this:
>
> > ,----[ (info "(elisp)Overview of Markers") ]
> > | ;; When you are finished using a marker, make it point nowhere.
> > | (set-marker m1 nil)
> > | => #<marker in no buffer>
> > `----
>
> > Should that be changed?
>
> Not necessarily: whether it's better to nil them explicitly or to leave
> them as they are depends on the specific case.
For which some guidance can be given (in the manual)?
Perhaps say something like what you said here, regarding performance
as one reason you might not want to "make it point nowhere"?
The text is currently imperative: "make it point nowhere".
What you are suggesting is something different.
Please consider providing some nuance (better guidance).
^ permalink raw reply [flat|nested] 20+ messages in thread
[parent not found: <mailman.910.1370238187.22516.help-gnu-emacs@gnu.org>]
* Re: putting double quotes efficiently
[not found] <mailman.910.1370238187.22516.help-gnu-emacs@gnu.org>
@ 2013-06-03 15:50 ` Udyant Wig
2013-06-04 14:52 ` Kevin Rodgers
2013-06-03 17:39 ` Joost Kremers
1 sibling, 1 reply; 20+ messages in thread
From: Udyant Wig @ 2013-06-03 15:50 UTC (permalink / raw)
To: help-gnu-emacs
You might also try keyboard macros.
Here's how I'd do it:
0. Move point to the first line (anywhere will do).
print _!_line1
print linetwo
print line3
1. Press C-x ( to begin recording a keyboard macro
2. Press C-a to move to the beginning of the line
_!_print line1
print linetwo
print line3
3. Press M-f to move forward a word
print _!_line1
print linetwo
print line3
4. Press " to insert one double-qoutes
print "_!_line1
print linetwo
print line3
5. There are two options here, any of which work fine:
5.0. Either, M-f again to move forward,
5.1. Or, C-e to move to the end of the line
print "line1_!_
print linetwo
print line3
6. Press " to insert the second double-quotes
print "line1"_!_
print linetwo
print line3
7. Press ; to insert the semi-colon
print "line1";_!_
print linetwo
print line3
8. Press C-n to move down a line
print "line1";
print linetwo_!_
print line3
9. Press C-x ) to finish recording the keyboard macro
10. Press C-x e to repeat the keyboard macro
print "line1";
print "linetwo";
print line3_!_
11. You can now simply press `e' to repeat
print "line1";
print "linetwo";
print "line3";
The above goes very quickly when actually typing.
The Info node (emacs)Keyboard Macros is the authoritative reference.
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 15:50 ` putting double quotes efficiently Udyant Wig
@ 2013-06-04 14:52 ` Kevin Rodgers
0 siblings, 0 replies; 20+ messages in thread
From: Kevin Rodgers @ 2013-06-04 14:52 UTC (permalink / raw)
To: help-gnu-emacs
On 6/3/13 9:50 AM, Udyant Wig wrote:
> You might also try keyboard macros.
>
> Here's how I'd do it:
>
> 0. Move point to the first line (anywhere will do).
>
> print _!_line1
> print linetwo
> print line3
>
> 1. Press C-x ( to begin recording a keyboard macro
>
> 2. Press C-a to move to the beginning of the line
>
> _!_print line1
> print linetwo
> print line3
>
...
> 9. Press C-x ) to finish recording the keyboard macro
>
> 10. Press C-x e to repeat the keyboard macro
>
> print "line1";
> print "linetwo";
> print line3_!_
>
> 11. You can now simply press `e' to repeat
>
> print "line1";
> print "linetwo";
> print "line3";
Or: Mark the remaining lines you want to change and `C-x C-k r'
(apply-macro-to-region-lines).
If you plan on doing that, you can move point to the beginning of the line
before starting the keyboard macro definition and leave C-a out of the macro
(because `C-x C-k r' will move to the beginning of each line automatically).
--
Kevin Rodgers
Denver, Colorado, USA
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
[not found] <mailman.910.1370238187.22516.help-gnu-emacs@gnu.org>
2013-06-03 15:50 ` putting double quotes efficiently Udyant Wig
@ 2013-06-03 17:39 ` Joost Kremers
2013-06-04 12:21 ` C K Kashyap
1 sibling, 1 reply; 20+ messages in thread
From: Joost Kremers @ 2013-06-03 17:39 UTC (permalink / raw)
To: help-gnu-emacs
C K Kashyap wrote:
> Hi,
> I am looking for a way to transform
>
> print line1
> print line two
> print line3
>
> into
>
> print "line1";
> print "line two";
> print "line3";
>
> most efficiently.
Personally, I'd use Magnar Sveen's nifty multiple-cursors library for
this. Put the cursor on the l of `line1', activate the mark (C-SPC),
move down to the l of `line3', hit C-S-c C-S-c, then type `"', then C-e,
then `";' and done.
--
Joost Kremers joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-03 17:39 ` Joost Kremers
@ 2013-06-04 12:21 ` C K Kashyap
2013-06-04 12:21 ` C K Kashyap
0 siblings, 1 reply; 20+ messages in thread
From: C K Kashyap @ 2013-06-04 12:21 UTC (permalink / raw)
To: Joost Kremers; +Cc: help-gnu-emacs
Thanks Udyant,
Regards,
Kashyap
On Mon, Jun 3, 2013 at 11:09 PM, Joost Kremers <joostkremers@yahoo.com>wrote:
> C K Kashyap wrote:
> > Hi,
> > I am looking for a way to transform
> >
> > print line1
> > print line two
> > print line3
> >
> > into
> >
> > print "line1";
> > print "line two";
> > print "line3";
> >
> > most efficiently.
>
> Personally, I'd use Magnar Sveen's nifty multiple-cursors library for
> this. Put the cursor on the l of `line1', activate the mark (C-SPC),
> move down to the l of `line3', hit C-S-c C-S-c, then type `"', then C-e,
> then `";' and done.
>
>
>
> --
> Joost Kremers joostkremers@fastmail.fm
> Selbst in die Unterwelt dringt durch Spalten Licht
> EN:SiS(9)
>
^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: putting double quotes efficiently
2013-06-04 12:21 ` C K Kashyap
@ 2013-06-04 12:21 ` C K Kashyap
0 siblings, 0 replies; 20+ messages in thread
From: C K Kashyap @ 2013-06-04 12:21 UTC (permalink / raw)
To: Joost Kremers; +Cc: help-gnu-emacs
And Joost :)
On Tue, Jun 4, 2013 at 5:51 PM, C K Kashyap <ckkashyap@gmail.com> wrote:
> Thanks Udyant,
>
> Regards,
> Kashyap
>
>
> On Mon, Jun 3, 2013 at 11:09 PM, Joost Kremers <joostkremers@yahoo.com>wrote:
>
>> C K Kashyap wrote:
>> > Hi,
>> > I am looking for a way to transform
>> >
>> > print line1
>> > print line two
>> > print line3
>> >
>> > into
>> >
>> > print "line1";
>> > print "line two";
>> > print "line3";
>> >
>> > most efficiently.
>>
>> Personally, I'd use Magnar Sveen's nifty multiple-cursors library for
>> this. Put the cursor on the l of `line1', activate the mark (C-SPC),
>> move down to the l of `line3', hit C-S-c C-S-c, then type `"', then C-e,
>> then `";' and done.
>>
>>
>>
>> --
>> Joost Kremers joostkremers@fastmail.fm
>> Selbst in die Unterwelt dringt durch Spalten Licht
>> EN:SiS(9)
>>
>
>
^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2013-06-17 16:48 UTC | newest]
Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-03 5:43 putting double quotes efficiently C K Kashyap
2013-06-03 7:26 ` Andreas Röhler
2013-06-03 7:55 ` Andreas Röhler
2013-06-03 8:03 ` C K Kashyap
2013-06-03 9:10 ` Andreas Röhler
2013-06-03 11:52 ` C K Kashyap
[not found] ` <mailman.918.1370250440.22516.help-gnu-emacs@gnu.org>
2013-06-13 14:40 ` narrowing considered harmful (was: putting double quotes efficiently) Stefan Monnier
2013-06-14 3:54 ` narrowing considered harmful Leo Liu
2013-06-14 13:00 ` Stefan Monnier
2013-06-14 16:51 ` Leo Liu
2013-06-15 2:43 ` Stefan Monnier
2013-06-16 16:42 ` Leo Liu
2013-06-17 14:56 ` Stefan Monnier
2013-06-17 15:20 ` Drew Adams
2013-06-17 16:48 ` Stefan Monnier
[not found] <mailman.910.1370238187.22516.help-gnu-emacs@gnu.org>
2013-06-03 15:50 ` putting double quotes efficiently Udyant Wig
2013-06-04 14:52 ` Kevin Rodgers
2013-06-03 17:39 ` Joost Kremers
2013-06-04 12:21 ` C K Kashyap
2013-06-04 12:21 ` C K Kashyap
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).