help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Enabling mode for part of buffer / region


From: Michael Anckaert
Subject: Re: Enabling mode for part of buffer / region
Date: Thu, 23 Jan 2020 20:33:54 +0100
User-agent: mu4e 1.1.0; emacs 26.3

Hey Drew, thanks for your reply. Your comment actually got me in the
right direction and I think I found a solution. Maybe not perfect
yet...

To answer your question on what I want to accomplish: Let's say I'm
writing an email and want to include some Python code. It would be
really useful to mark a part of the buffer as 'contains python code',
when I enter here I want to enable python-mode when editing.

The 4 lines below contain a small Python snippet that I would like to
edit using some features of python-mode. Thanks to your answer I found
a partial solution that enables me to narrow the buffer to that point,
enable python-mode and edit the code. When I widen I can return to
text-mode.

def greet(name: str):
    print(f'Hello, {name}')

greet('Drew')

This solution is almost perfect but manually switching the modes back
and forth is somewhat of a hassle. While composing this email for
example I switched back from python-mode to text-mode, while I actually
needed to switch back to mu4e-compose-mode.

I'm looking for something like the code blocks inside an org-mode
document [0]. Just for fun I'm trying to whip up an elisp function that
will store the current mode before narrowing and restore the mode after
widening. But maybe there is a better way to achieve what I want to do.

[0] https://orgmode.org/manual/Structure-of-code-blocks.html

Drew Adams writes:

>> Suppose I'm writing an email and would like to enable python-mode in
>> the email, but only for a selected part.
>> Is this possible in Emacs?
>
> Please clarify just what you want to do.
>
> If you are using Lisp code you can use
> `save-restriction', narrow to the buffer portion
> of interest, change the mode, etc.  Perhaps that
> is sufficient?  What is it that you really want
> to do?


--
Michael Anckaert
+32 474 066 467
https://www.sinax.be



reply via email to

[Prev in Thread] Current Thread [Next in Thread]