views:

232

answers:

2

I am writing a document with Emacs. As you know, there are some code segments in the text file I am working with. Typically, when I open this file, emacs will get into text-mode automatically. And it works fine for me to edit the ordinary paragraphs. But for those code segments, how could I indent them into gnu or linux style just like what I could do in c-mode (by c-set-style && do Ctrl-Alt-\ in certain region)?

BTW, actually, I could turn the buffer into c-mode by invoking M-x c-mode to do this, however, I think there should be much a graceful way to do this in text-mode.

+5  A: 

orgmode manages to do it by copying the code out to a temporary buffer where you edit & format it, and updating the changed text when you're done.

If switching to orgmode is an option, then you do it like this:

 #+BEGIN_SRC emacs-lisp
 (defun org-xor (a b)
    "Exclusive or."
    (if a (not b) b))
 #+END_SRC

and start and finish editing with C-c '.

Edit: Emacswiki has a list of multiple modes.

pgs
Thanks pgs! I will try this. I am not familiar with it but orgmode seems great for arrange something up. Is there any other methods to do this in a straightforward way? Such as marking a region and invoking some functions or customized functions in it?
jcadam
You might also be able to do it with one of the ways of running multiple major modes, and M-x indent-region.
pgs
A: 

You might be able to mark the region, then narrow the view to the region, change the mode, indent, return to text-mode, and return to the full buffer again. I forget the exact shortcuts at the moment, but it should be fairly easy to turn into a function.

Josh Matthews
C-x n n: narrow to region. C-x n w: widen, i.e. view entire buffer.
legoscia