all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* how to reverse a region of several words?
@ 2008-07-28  8:11 sunway
  2008-07-28  9:03 ` Thierry Volpiatto
                   ` (3 more replies)
  0 siblings, 4 replies; 11+ messages in thread
From: sunway @ 2008-07-28  8:11 UTC (permalink / raw)
  To: help-gnu-emacs

e.g. the region contains words like "aaa bbb ccc ddd",I want to
reverse it to "ddd ccc bbb aaa"


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

* Re: how to reverse a region of several words?
  2008-07-28  8:11 how to reverse a region of several words? sunway
@ 2008-07-28  9:03 ` Thierry Volpiatto
  2008-07-28 15:00 ` tyler
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 11+ messages in thread
From: Thierry Volpiatto @ 2008-07-28  9:03 UTC (permalink / raw)
  To: sunway; +Cc: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> e.g. the region contains words like "aaa bbb ccc ddd",I want to
> reverse it to "ddd ccc bbb aaa"
>
kill your region with some kill func (check elisp man)
and store it (A)
Be sure to remember the position of your text.
,----
| ELISP> (setq A "aaa bbb ccc ddd")
| "aaa bbb ccc ddd"
| ELISP> (setq A (split-string A))
| ("aaa" "bbb" "ccc" "ddd")
| 
| ELISP> (setq A (reverse A))
| ("ddd" "ccc" "bbb" "aaa")
| 
| ELISP> (mapconcat #'(lambda (x) x) A " ")
| "ddd ccc bbb aaa"
`----
And now insert the new text at the saved position.
-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: how to reverse a region of several words?
  2008-07-28  8:11 how to reverse a region of several words? sunway
  2008-07-28  9:03 ` Thierry Volpiatto
@ 2008-07-28 15:00 ` tyler
  2008-07-28 17:44 ` Pascal J. Bourguignon
       [not found] ` <mailman.15498.1217257259.18990.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 11+ messages in thread
From: tyler @ 2008-07-28 15:00 UTC (permalink / raw)
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> e.g. the region contains words like "aaa bbb ccc ddd",I want to
> reverse it to "ddd ccc bbb aaa"
>

I think you probably want the words themselves to stay in the original
order, i.e., one two => two one? If not, if you want to completely
reverse the text, i.e., one two => owt eno, I use the following
function:

(defun reverse-string (beg1 end2)
  "Reverse the order of characters in a region.
From a program takes two point or marker arguments, BEG1 and END2."
  (interactive "r")
  (if (> beg1 end2)
      (let (mid) (setq mid end2 end2 beg1 beg1 mid)))
  (while (< beg1 (1- end2))
	(let ((end1 (1+ beg1))
		  (beg2 (1- end2)))
	  (transpose-regions beg1 end1 beg2 end2))
	(incf beg1)
	(decf end2)))

Cheers,

Tyler
-- 
Philosophy of science is about as useful to scientists as ornithology is to
birds.                                 --Richard Feynman





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

* Re: how to reverse a region of several words?
  2008-07-28  8:11 how to reverse a region of several words? sunway
  2008-07-28  9:03 ` Thierry Volpiatto
  2008-07-28 15:00 ` tyler
@ 2008-07-28 17:44 ` Pascal J. Bourguignon
  2008-07-28 18:51   ` tyler
       [not found] ` <mailman.15498.1217257259.18990.help-gnu-emacs@gnu.org>
  3 siblings, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2008-07-28 17:44 UTC (permalink / raw)
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> e.g. the region contains words like "aaa bbb ccc ddd",I want to
> reverse it to "ddd ccc bbb aaa"

Select it and M-x reverse-line RET


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay


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

* Re: how to reverse a region of several words?
  2008-07-28 17:44 ` Pascal J. Bourguignon
@ 2008-07-28 18:51   ` tyler
  0 siblings, 0 replies; 11+ messages in thread
From: tyler @ 2008-07-28 18:51 UTC (permalink / raw)
  To: help-gnu-emacs

pjb@informatimago.com (Pascal J. Bourguignon) writes:

> sunway <sunwayforever@gmail.com> writes:
>
>> e.g. the region contains words like "aaa bbb ccc ddd",I want to
>> reverse it to "ddd ccc bbb aaa"
>
> Select it and M-x reverse-line RET

What version are you running? 23.0.60.1 doesn't have reverse-line...

Tyler

-- 
Tired of spyware? Try Firefox:

www.firefox.com





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

* Re: how to reverse a region of several words?
       [not found] ` <mailman.15498.1217257259.18990.help-gnu-emacs@gnu.org>
@ 2008-07-29  3:13   ` sunway
  2008-07-29  5:35     ` Thierry Volpiatto
  2008-07-29  5:46     ` Pascal J. Bourguignon
  0 siblings, 2 replies; 11+ messages in thread
From: sunway @ 2008-07-29  3:13 UTC (permalink / raw)
  To: help-gnu-emacs

I want to transpose "one two" to " two one"

On Jul 28, 11:00 pm, tyler <tyler.sm...@mail.mcgill.ca> wrote:
> sunway <sunwayfore...@gmail.com> writes:
> > e.g. the region contains words like "aaa bbb ccc ddd",I want to
> > reverse it to "ddd ccc bbb aaa"
>
> I think you probably want the words themselves to stay in the original
> order, i.e., one two => two one? If not, if you want to completely
> reverse the text, i.e., one two => owt eno, I use the following
> function:
>
> (defun reverse-string (beg1 end2)
>   "Reverse the order of characters in a region.
> From a program takes two point or marker arguments, BEG1 and END2."
>   (interactive "r")
>   (if (> beg1 end2)
>       (let (mid) (setq mid end2 end2 beg1 beg1 mid)))
>   (while (< beg1 (1- end2))
>         (let ((end1 (1+ beg1))
>                   (beg2 (1- end2)))
>           (transpose-regions beg1 end1 beg2 end2))
>         (incf beg1)
>         (decf end2)))
>
> Cheers,
>
> Tyler
> --
> Philosophy of science is about as useful to scientists as ornithology is to
> birds.                                 --Richard Feynman



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

* Re: how to reverse a region of several words?
  2008-07-29  3:13   ` sunway
@ 2008-07-29  5:35     ` Thierry Volpiatto
  2008-07-29  5:46     ` Pascal J. Bourguignon
  1 sibling, 0 replies; 11+ messages in thread
From: Thierry Volpiatto @ 2008-07-29  5:35 UTC (permalink / raw)
  To: sunway; +Cc: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> I want to transpose "one two" to " two one"

Put the point on two and hit M-t.

,----[ C-h k M-t ]
| M-t runs the command transpose-words, which is an interactive compiled
| Lisp function in `simple.el'.
| 
| It is bound to M-t.
| 
| (transpose-words arg)
| 
| Interchange words around point, leaving point at end of them.
| With prefix arg arg, effect is to take word before or around point
| and drag it forward past arg other words (backward if arg negative).
| If arg is zero, the words around or after point and around or after mark
| are interchanged.
| 
| [back]
`----

> On Jul 28, 11:00 pm, tyler <tyler.sm...@mail.mcgill.ca> wrote:
>> sunway <sunwayfore...@gmail.com> writes:
>> > e.g. the region contains words like "aaa bbb ccc ddd",I want to
>> > reverse it to "ddd ccc bbb aaa"
>>
>> I think you probably want the words themselves to stay in the original
>> order, i.e., one two => two one? If not, if you want to completely
>> reverse the text, i.e., one two => owt eno, I use the following
>> function:
>>
>> (defun reverse-string (beg1 end2)
>>   "Reverse the order of characters in a region.
>> From a program takes two point or marker arguments, BEG1 and END2."
>>   (interactive "r")
>>   (if (> beg1 end2)
>>       (let (mid) (setq mid end2 end2 beg1 beg1 mid)))
>>   (while (< beg1 (1- end2))
>>         (let ((end1 (1+ beg1))
>>                   (beg2 (1- end2)))
>>           (transpose-regions beg1 end1 beg2 end2))
>>         (incf beg1)
>>         (decf end2)))
>>
>> Cheers,
>>
>> Tyler
>> --
>> Philosophy of science is about as useful to scientists as ornithology is to
>> birds.                                 --Richard Feynman
>

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

* Re: how to reverse a region of several words?
  2008-07-29  3:13   ` sunway
  2008-07-29  5:35     ` Thierry Volpiatto
@ 2008-07-29  5:46     ` Pascal J. Bourguignon
  2008-07-30  6:27       ` sunway
  1 sibling, 1 reply; 11+ messages in thread
From: Pascal J. Bourguignon @ 2008-07-29  5:46 UTC (permalink / raw)
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> I want to transpose "one two" to " two one"

put the cursor between one and two and type:
M-x transpose-words RET or M-t

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NOTE: The most fundamental particles in this product are held
together by a "gluing" force about which little is currently known
and whose adhesive power can therefore not be permanently
guaranteed.


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

* Re: how to reverse a region of several words?
  2008-07-29  5:46     ` Pascal J. Bourguignon
@ 2008-07-30  6:27       ` sunway
  2008-07-30  7:00         ` Pascal J. Bourguignon
  2008-07-30  7:43         ` Thierry Volpiatto
  0 siblings, 2 replies; 11+ messages in thread
From: sunway @ 2008-07-30  6:27 UTC (permalink / raw)
  To: help-gnu-emacs

no, I want to transpose "one two three four" to "four three two one"

On Jul 29, 1:46 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> sunway <sunwayfore...@gmail.com> writes:
> > I want to transpose "one two" to " two one"
>
> put the cursor between one and two and type:
> M-x transpose-words RET or M-t
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/
>
> NOTE: The most fundamental particles in this product are held
> together by a "gluing" force about which little is currently known
> and whose adhesive power can therefore not be permanently
> guaranteed.



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

* Re: how to reverse a region of several words?
  2008-07-30  6:27       ` sunway
@ 2008-07-30  7:00         ` Pascal J. Bourguignon
  2008-07-30  7:43         ` Thierry Volpiatto
  1 sibling, 0 replies; 11+ messages in thread
From: Pascal J. Bourguignon @ 2008-07-30  7:00 UTC (permalink / raw)
  To: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:
>
> On Jul 29, 1:46 pm, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> sunway <sunwayfore...@gmail.com> writes:
>> > I want to transpose "one two" to " two one"
>>
>> put the cursor between one and two and type:
>> M-x transpose-words RET or M-t
>
> no, I want to transpose "one two three four" to "four three two one"

(defun reverse-words (start end)
  (interactive "r")
  (let ((words (reverse (split-string (buffer-substring start end)))))
    (delete-region start end)
    (dolist (word words)
      (insert word " "))
    (backward-char 1)
    (delete-char 1)))


-- 
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
----------> http://www.netmeister.org/news/learn2quote.html <-----------
---> http://homepage.ntlworld.com/g.mccaughan/g/remarks/uquote.html <---

__Pascal Bourguignon__                     http://www.informatimago.com/


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

* Re: how to reverse a region of several words?
  2008-07-30  6:27       ` sunway
  2008-07-30  7:00         ` Pascal J. Bourguignon
@ 2008-07-30  7:43         ` Thierry Volpiatto
  1 sibling, 0 replies; 11+ messages in thread
From: Thierry Volpiatto @ 2008-07-30  7:43 UTC (permalink / raw)
  To: sunway; +Cc: help-gnu-emacs

sunway <sunwayforever@gmail.com> writes:

> no, I want to transpose "one two three four" to "four three two one"

,----
| (defun reverse-sentence-region (&optional separator)
|   (interactive "P")
|   (let ((beg (point))
|         (sentence)
|         (separator))
|     (when current-prefix-arg
|       (setq separator (read-string "Separator: ")))
|     (kill-region beg (mark))
|     (setq sentence (nth 0 kill-ring))
|     (setq sentence (split-string sentence (if separator
|                                               separator)))
|     (setq sentence (reverse sentence))
|     (setq sentence (mapconcat #'(lambda (x) x)
|                               sentence
|                               (if separator
|                                   separator
|                                   " ")))
|     (insert sentence)))
`----
1) Mark region

2) Run M-x reverse-sentence-region
or C-u M-x reverse-sentence-region

if the words in region are separated with something else than " "



> On Jul 29, 1:46 pm, p...@informatimago.com (Pascal J. Bourguignon)
> wrote:
>> sunway <sunwayfore...@gmail.com> writes:
>> > I want to transpose "one two" to " two one"
>>
>> put the cursor between one and two and type:
>> M-x transpose-words RET or M-t
>>
>> --
>> __Pascal Bourguignon__                    http://www.informatimago.com/
>>
>> NOTE: The most fundamental particles in this product are held
>> together by a "gluing" force about which little is currently known
>> and whose adhesive power can therefore not be permanently
>> guaranteed.
>

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France




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

end of thread, other threads:[~2008-07-30  7:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-07-28  8:11 how to reverse a region of several words? sunway
2008-07-28  9:03 ` Thierry Volpiatto
2008-07-28 15:00 ` tyler
2008-07-28 17:44 ` Pascal J. Bourguignon
2008-07-28 18:51   ` tyler
     [not found] ` <mailman.15498.1217257259.18990.help-gnu-emacs@gnu.org>
2008-07-29  3:13   ` sunway
2008-07-29  5:35     ` Thierry Volpiatto
2008-07-29  5:46     ` Pascal J. Bourguignon
2008-07-30  6:27       ` sunway
2008-07-30  7:00         ` Pascal J. Bourguignon
2008-07-30  7:43         ` Thierry Volpiatto

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.