unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Edit region in different mode
@ 2004-12-22 16:19 Albert Reiner
  2004-12-22 16:25 ` V. L. Simpson
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Albert Reiner @ 2004-12-22 16:19 UTC (permalink / raw)


Hi,

is there a way of editing the current region in a different mode?  OK,
this is emacs so there certainly is a way, but is there a simple way?

More specifically, I would like to get a new buffer with everything
between point and mark (or maybe some other positions found by
searching), set a different editing mode in that new buffer, and have
the editing take effect in the original buffer when I am done.  I
suspect this should be possible by some combination of
clone-indirect-buffer and narrowing but I don't quite know how to go
about this.

I'd be grateful for any help.

Regards,

Albert.

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

* Re: Edit region in different mode
  2004-12-22 16:19 Edit region in different mode Albert Reiner
@ 2004-12-22 16:25 ` V. L. Simpson
  2004-12-22 17:05   ` Albert Reiner
  2004-12-23 12:17 ` Mathias Dahl
  2004-12-23 12:52 ` Oliver Scholz
  2 siblings, 1 reply; 10+ messages in thread
From: V. L. Simpson @ 2004-12-22 16:25 UTC (permalink / raw)


>>>>> "Albert" == Albert Reiner <areiner@tph.tuwien.ac.at> writes:

    > Hi, is there a way of editing the current region in a
    > different mode?  OK,

Try http://mmm-mode.sourceforge.net/


-- 
V. L. Simpson

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

* Re: Edit region in different mode
  2004-12-22 16:25 ` V. L. Simpson
@ 2004-12-22 17:05   ` Albert Reiner
  2004-12-22 19:51     ` Pascal Bourguignon
  0 siblings, 1 reply; 10+ messages in thread
From: Albert Reiner @ 2004-12-22 17:05 UTC (permalink / raw)


["V. L. Simpson" <vls@freeshell.org>, 22 Dec 2004 10:25:42 -0600]:
> >>>>> "Albert" == Albert Reiner <areiner@tph.tuwien.ac.at> writes:
> 
>     > Hi, is there a way of editing the current region in a
>     > different mode?  OK,
> 
> Try http://mmm-mode.sourceforge.net/

Thanks, but I am well aware of mmm mode, and it does not seem to be a
solution to my problem: although the documentation seems hard to find,
I believe that mmm mode still requires me to put tags into the file
that indicate where to switch between modes.  This is not what I want;
in fact, it is what I already have but is giving me trouble in some
cases.  I really only want to edit in a different mode for a short
period of time, without leaving any mode-related traces in the file.

Maybe I should explain in more detail: I am using noweb mode with
noweb-code-mode set to lisp-mode (which is in fact slime mode).
Unfortunately the lisp-mode indentation and filling functions do not
work very well in lisp doc strings, and all the more so when the
quotes are not part of the same code chunk.  Part of the problem is
that noweb-mode overrides any M-x outline-mode RET in what it knows is
a code chunk.

So whenever I write a long documentation string I copy the old text to
*scratch*, change to outline-mode, write / edit there, copy the edited
text back.  Or sometimes I remove the chunk-starting line, edit
(effectively in LaTeX mode, which may be sufficient), and add back the
chunk-starting line.  I have also tried M-: (setq noweb-code-mode
'outline-mode) RET and M-x normal-mode RET later, but none of these
are very convenient.

Albert.

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

* Re: Edit region in different mode
  2004-12-22 17:05   ` Albert Reiner
@ 2004-12-22 19:51     ` Pascal Bourguignon
  2004-12-22 23:14       ` Kevin Rodgers
  2004-12-23 17:42       ` Albert Reiner
  0 siblings, 2 replies; 10+ messages in thread
From: Pascal Bourguignon @ 2004-12-22 19:51 UTC (permalink / raw)


Albert Reiner <areiner@tph.tuwien.ac.at> writes:

> ["V. L. Simpson" <vls@freeshell.org>, 22 Dec 2004 10:25:42 -0600]:
> > >>>>> "Albert" == Albert Reiner <areiner@tph.tuwien.ac.at> writes:
> > 
> >     > Hi, is there a way of editing the current region in a
> >     > different mode?  OK,
> > 
> > Try http://mmm-mode.sourceforge.net/
> 
> Thanks, but I am well aware of mmm mode, and it does not seem to be a
> solution to my problem: although the documentation seems hard to find,
> I believe that mmm mode still requires me to put tags into the file
> that indicate where to switch between modes.  This is not what I want;
> in fact, it is what I already have but is giving me trouble in some
> cases.  I really only want to edit in a different mode for a short
> period of time, without leaving any mode-related traces in the file.

I'd do narrow-to-region and xyz-mode
then widden and original-mode

> Maybe I should explain in more detail: I am using noweb mode with
> noweb-code-mode set to lisp-mode (which is in fact slime mode).
> Unfortunately the lisp-mode indentation and filling functions do not
> work very well in lisp doc strings, and all the more so when the
> quotes are not part of the same code chunk.  Part of the problem is
> that noweb-mode overrides any M-x outline-mode RET in what it knows is
> a code chunk.

Yes, I have the same problem. Here's what I have in my ~/.emacs:

(global-set-key [f5]       'set-justification-left)

(global-set-key [3 f5]    (lambda (start end) (interactive "r") 
                              (let ((mode major-mode))
                                (text-mode)
                                (set-justification-left start end)
                                (funcall mode))))

So F5 does the justification of the region in the current mode, which
is wrong in lisp mode.

C-c F5 does the justification in the text mode and then reverts to the
saved major-mode.


> So whenever I write a long documentation string I copy the old text
> to *scratch*, change to outline-mode, write / edit there, copy the
> edited text back.  Or sometimes I remove the chunk-starting line,
> edit (effectively in LaTeX mode, which may be sufficient), and add
> back the chunk-starting line.  I have also tried M-: (setq
> noweb-code-mode 'outline-mode) RET and M-x normal-mode RET later,
> but none of these are very convenient.

I guess it should be possible to write commands to _push_ and _pop_
modes...
 

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

There is no worse tyranny than to force a man to pay for what he does not
want merely because you think it would be good for him. -- Robert Heinlein

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

* Re: Edit region in different mode
  2004-12-22 19:51     ` Pascal Bourguignon
@ 2004-12-22 23:14       ` Kevin Rodgers
  2004-12-23  1:03         ` Pascal Bourguignon
  2004-12-23 17:42       ` Albert Reiner
  1 sibling, 1 reply; 10+ messages in thread
From: Kevin Rodgers @ 2004-12-22 23:14 UTC (permalink / raw)


Pascal Bourguignon wrote:
 > Yes, I have the same problem. Here's what I have in my ~/.emacs:
 >
 > (global-set-key [f5]       'set-justification-left)
 >
 > (global-set-key [3 f5]    (lambda (start end) (interactive "r")
 >                               (let ((mode major-mode))
 >                                 (text-mode)
 >                                 (set-justification-left start end)
 >                                 (funcall mode))))

These alternatives are more readable and portable than [3 f5]:

	[?\C-c f5]
	[(control c) f5]
	(kbd "C-c <f5>")

 > So F5 does the justification of the region in the current mode, which
 > is wrong in lisp mode.
 >
 > C-c F5 does the justification in the text mode and then reverts to the
 > saved major-mode.

-- 
Kevin Rodgers

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

* Re: Edit region in different mode
  2004-12-22 23:14       ` Kevin Rodgers
@ 2004-12-23  1:03         ` Pascal Bourguignon
  0 siblings, 0 replies; 10+ messages in thread
From: Pascal Bourguignon @ 2004-12-23  1:03 UTC (permalink / raw)


Kevin Rodgers <ihs_4664@yahoo.com> writes:

> Pascal Bourguignon wrote:
>  > Yes, I have the same problem. Here's what I have in my ~/.emacs:
>  >
>  > (global-set-key [f5]       'set-justification-left)
>  >
>  > (global-set-key [3 f5]    (lambda (start end) (interactive "r")
>  >                               (let ((mode major-mode))
>  >                                 (text-mode)
>  >                                 (set-justification-left start end)
>  >                                 (funcall mode))))
> 
> These alternatives are more readable and portable than [3 f5]:
> 
> 	[?\C-c f5]
> 	[(control c) f5]
> 	(kbd "C-c <f5>")

Thank you. I fought quite a time to find an accepted syntax...


>  > So F5 does the justification of the region in the current mode, which
>  > is wrong in lisp mode.
>  >
>  > C-c F5 does the justification in the text mode and then reverts to the
>  > saved major-mode.
> 
> -- 
> Kevin Rodgers

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
I need a new toy.
Tail of black dog keeps good time.
Pounce! Good dog! Good dog!

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

* Re: Edit region in different mode
  2004-12-22 16:19 Edit region in different mode Albert Reiner
  2004-12-22 16:25 ` V. L. Simpson
@ 2004-12-23 12:17 ` Mathias Dahl
  2004-12-23 12:52 ` Oliver Scholz
  2 siblings, 0 replies; 10+ messages in thread
From: Mathias Dahl @ 2004-12-23 12:17 UTC (permalink / raw)


Albert Reiner <areiner@tph.tuwien.ac.at> writes:

> ...I would like to get a new buffer with everything between point
> and mark (or maybe some other positions found by searching), set a
> different editing mode in that new buffer, and have the editing take
> effect in the original buffer when I am done.  I suspect this should
> be possible by some combination of clone-indirect-buffer and
> narrowing but I don't quite know how to go about this.


You could create two macros that helps you out, one that copies the
region and pastes it into a temporary buffer and enter some mode:

M-w			;; kill-ring-save
C-x b			;; iswitchb-buffer
tempo			;; self-insert-command * 5
RET			;; newline
C-y			;; yank
M-x			;; execute-extended-command
text			;; self-insert-command * 4
SPC			;; self-insert-command
mode			;; self-insert-command * 4
RET			;; newline

And a macro that copies all text in the temp buffer, kills the buffer,
goes back to the previous buffer, deletes the region and pastes what
was copied:

<C-home>		;; beginning-of-buffer
C-SPC			;; set-mark-command
<C-end>			;; end-of-buffer
M-w			;; kill-ring-save
C-x k			;; kill-buffer
RET			;; newline
M-x			;; execute-extended-command
delete			;; self-insert-command * 6
SPC			;; self-insert-command
reg			;; self-insert-command * 3
TAB			;; indent-for-tab-command
RET			;; newline
C-y			;; yank

For your convenience, should you decide to use them, here they are for
pasting into your .emacs:

(fset 'edit-region-in-tempo
   [?\M-w ?\C-x ?b ?t ?e ?m ?p ?o ?\C-m ?\C-y ?\M-x ?t ?e ?x ?t ?  ?m ?o ?d ?e ?\C-m])

(fset 'edit-region-in-tempo-go-back
   [C-home ?\C-  C-end ?\M-w ?\C-x ?k return ?\M-x ?d ?e ?l ?e ?t ?e ?  ?r ?e ?g tab return ?\C-y])

Ah, the wonders of keyboard macros!

Of course, neither of these macros are very hard to write in some
lines of elisp.

/Mathias

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

* Re: Edit region in different mode
  2004-12-22 16:19 Edit region in different mode Albert Reiner
  2004-12-22 16:25 ` V. L. Simpson
  2004-12-23 12:17 ` Mathias Dahl
@ 2004-12-23 12:52 ` Oliver Scholz
  2004-12-23 17:01   ` Kevin Rodgers
  2 siblings, 1 reply; 10+ messages in thread
From: Oliver Scholz @ 2004-12-23 12:52 UTC (permalink / raw)


Albert Reiner <areiner@tph.tuwien.ac.at> writes:

> Hi,
>
> is there a way of editing the current region in a different mode?  OK,
> this is emacs so there certainly is a way, but is there a simple way?
>
> More specifically, I would like to get a new buffer with everything
> between point and mark (or maybe some other positions found by
> searching), set a different editing mode in that new buffer, and have
> the editing take effect in the original buffer when I am done.  I
> suspect this should be possible by some combination of
> clone-indirect-buffer and narrowing but I don't quite know how to go
> about this.

I use the following to write Emacs Lisp in my usenet postings:

(defun egoge-write-elisp ()
  (interactive)
  (if (not (eq major-mode 'message-mode))
      (message "Not a message buffer.")
    (switch-to-buffer (make-indirect-buffer (current-buffer)
					    "*emacs-lisp-message*"))
    (emacs-lisp-mode)))


I guess, this could serve as a starter for you.

However, I do believe that mmm-mode would provide a more elegant
solution. I never really used it, but the last time I checked I was
not forced at all to use some tags, but IIRC I just issued a command
on a region and then for text in that region Emacs behaved as if a
different major mode were active, an effect that was lost once I
revisited the file.

    Oliver
-- 
3 Nivôse an 213 de la Révolution
Liberté, Egalité, Fraternité!

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

* Re: Edit region in different mode
  2004-12-23 12:52 ` Oliver Scholz
@ 2004-12-23 17:01   ` Kevin Rodgers
  0 siblings, 0 replies; 10+ messages in thread
From: Kevin Rodgers @ 2004-12-23 17:01 UTC (permalink / raw)


Oliver Scholz wrote:
 > I use the following to write Emacs Lisp in my usenet postings:
 >
 > (defun egoge-write-elisp ()
 >   (interactive)
 >   (if (not (eq major-mode 'message-mode))
 >       (message "Not a message buffer.")
 >     (switch-to-buffer (make-indirect-buffer (current-buffer)
 > 					    "*emacs-lisp-message*"))
 >     (emacs-lisp-mode)))
 >
 >
 > I guess, this could serve as a starter for you.

It's an excellent start.  Here's what I've come up with, based on that:

(defun edit-region (&optional edit-mode)
   "Edit the current region in a separate buffer.
With a prefix arg, change `major-mode' to EDIT-MODE."
   (interactive (list (if current-prefix-arg
                          (intern (completing-read (format "Major mode 
(%s): "
                                                           major-mode)
                                                   obarray
                                                   'major-mode-p
                                                   t nil nil
                                                   (symbol-name 
major-mode))))))
   (clone-indirect-buffer nil t)
   (narrow-to-region (region-beginning) (region-end))
   (shrink-window-if-larger-than-buffer)
   (when edit-mode
     (funcall edit-mode)))

(defun major-mode-p (symbol)
   "Return non-nil if SYMBOL is a major mode."
   (and (fboundp symbol)
        (let ((function-name (symbol-name symbol)))
          (and (string-match "-mode\\'" function-name)
               (not (string-match "\\`turn-\\(on\\|off\\)-" 
function-name))))
        (not (assq symbol minor-mode-alist))))

-- 
Kevin Rodgers

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

* Re: Edit region in different mode
  2004-12-22 19:51     ` Pascal Bourguignon
  2004-12-22 23:14       ` Kevin Rodgers
@ 2004-12-23 17:42       ` Albert Reiner
  1 sibling, 0 replies; 10+ messages in thread
From: Albert Reiner @ 2004-12-23 17:42 UTC (permalink / raw)


Thanks to all of you for your help!

However, most of the solutions do not seem to work here; the only
exception is Pascal's

[Pascal Bourguignon <spam@mouse-potato.com>, 22 Dec 2004 20:51:07 +0100]:
> I'd do narrow-to-region and xyz-mode
> then widden and original-mode

(Without the initial narrow-to-region, BTW, noweb-mode simply
overrides the mode change.)

All the others have some problem as they are:

[Pascal Bourguignon <spam@mouse-potato.com>, 22 Dec 2004 20:51:07 +0100]:
> Yes, I have the same problem. Here's what I have in my ~/.emacs:
> 
> (global-set-key [f5]       'set-justification-left)
> 
> (global-set-key [3 f5]    (lambda (start end) (interactive "r") 
>                               (let ((mode major-mode))
>                                 (text-mode)
>                                 (set-justification-left start end)
>                                 (funcall mode))))

Here C-c f5 ends up re-indenting things even before the beginning of
region, presumably because there is no initial blank line (and just
adding such a line does not work well with the noweb file format).

[Mathias Dahl <brakjoller@gmail.com>, 23 Dec 2004 13:17:20 +0100]:
> (fset 'edit-region-in-tempo
>    [?\M-w ?\C-x ?b ?t ?e ?m ?p ?o ?\C-m ?\C-y ?\M-x ?t ?e ?x ?t ?  ?m ?o ?d ?e ?\C-m])
> 
> (fset 'edit-region-in-tempo-go-back
>    [C-home ?\C-  C-end ?\M-w ?\C-x ?k return ?\M-x ?d ?e ?l ?e ?t ?e ?  ?r ?e ?g tab return ?\C-y])

As it stands, edit-region-in-tempo-go-back runs into an error when
killing the buffer but I suppose that could be fixed easily.  Ideally,
however, it should not depend on there being no editing in other
buffers in between, which probably requires re-writing in elisp.

[Oliver Scholz <alkibiades@gmx.de>, Thu, 23 Dec 2004 13:52:42 +0100]:
> (defun egoge-write-elisp ()
>   (interactive)
>   (if (not (eq major-mode 'message-mode))
>       (message "Not a message buffer.")
>     (switch-to-buffer (make-indirect-buffer (current-buffer)
> 					    "*emacs-lisp-message*"))
>     (emacs-lisp-mode)))

(BTW - egoge wouldn't be some classical Greek: )EGW/ GE, would it?)

[Kevin Rodgers <ihs_4664@yahoo.com>, Thu, 23 Dec 2004 10:01:01 -0700]:
> (defun edit-region (&optional edit-mode)
>    "Edit the current region in a separate buffer.
> With a prefix arg, change `major-mode' to EDIT-MODE."
>    (interactive (list (if current-prefix-arg
>                           (intern (completing-read (format "Major mode
> (%s): "
>                                                            major-mode)
>                                                    obarray
>                                                    'major-mode-p
>                                                    t nil nil
>                                                    (symbol-name
> major-mode))))))
>    (clone-indirect-buffer nil t)
>    (narrow-to-region (region-beginning) (region-end))
>    (shrink-window-if-larger-than-buffer)
>    (when edit-mode
>      (funcall edit-mode)))
> 
> (defun major-mode-p (symbol)
>    "Return non-nil if SYMBOL is a major mode."
>    (and (fboundp symbol)
>         (let ((function-name (symbol-name symbol)))
>           (and (string-match "-mode\\'" function-name)
>                (not (string-match "\\`turn-\\(on\\|off\\)-"
> function-name))))
>         (not (assq symbol minor-mode-alist))))

Both Oliver's and Kevin's solutions run into the same problem: Somehow
noweb-mode keeps interfering in the indentation, which leads to rather
bizarre results.

In the light of Oliver's comment:

[Oliver Scholz <alkibiades@gmx.de>, Thu, 23 Dec 2004 13:52:42 +0100]:
> However, I do believe that mmm-mode would provide a more elegant
> solution. I never really used it, but the last time I checked I was
> not forced at all to use some tags, but IIRC I just issued a command
> on a region and then for text in that region Emacs behaved as if a
> different major mode were active, an effect that was lost once I
> revisited the file.

I will have another look at mmm-mode first; there even seems to be an
mmm-noweb-mode.  If that does not work out I will try to come up with
something along the lines of Mathias' macros.

Thanks again for all your help,

Albert.

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

end of thread, other threads:[~2004-12-23 17:42 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-12-22 16:19 Edit region in different mode Albert Reiner
2004-12-22 16:25 ` V. L. Simpson
2004-12-22 17:05   ` Albert Reiner
2004-12-22 19:51     ` Pascal Bourguignon
2004-12-22 23:14       ` Kevin Rodgers
2004-12-23  1:03         ` Pascal Bourguignon
2004-12-23 17:42       ` Albert Reiner
2004-12-23 12:17 ` Mathias Dahl
2004-12-23 12:52 ` Oliver Scholz
2004-12-23 17:01   ` Kevin Rodgers

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).