Using Emacs, how could I format a code segment in a TXT file?

I am writing a document with Emacs. As you know, there are some code segments in the text file I'm working with. Usually, when I open this file, emacs automatically goes into text mode. And for me editing paragraphs works fine. But for those code segments, how could I put them off in gnu or linux style, exactly the same as what I could do in c-mode (c-set-style && make Ctrl-Alt- \ in a certain area) ?

By the way, I could turn the buffer into c-mode by calling Mx c-mode for this, however, I think there should be a lot of nice way in text mode.

+4
source share
2 answers

orgmode manages to do this by copying the code to a temporary buffer, where you edit and format it, and also update the changed text when you are done.

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

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

and start and end editing with Cc ' .

Edit: Emacswiki has a list of several modes .

+5
source

Perhaps you can mark the area, then narrow the view to the area, change the mode, indent, return to text mode and return to the full buffer again. Right now I'm forgetting the exact shortcuts, but it's pretty easy to turn into a function.

0
source

All Articles