* Enabling mode for part of buffer / region
@ 2020-01-23 10:14 Michael Anckaert
2020-01-23 17:00 ` Drew Adams
2020-01-24 10:49 ` Michael Anckaert
0 siblings, 2 replies; 6+ messages in thread
From: Michael Anckaert @ 2020-01-23 10:14 UTC (permalink / raw)
To: Help-gnu-emacs
Hello everyone,
I'm sure I'm missing something obvious but I wondered if I can enable a
mode for part of a buffer.
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?
Kind regards
--
Michael Anckaert
+32 474 066 467
https://www.sinax.be
^ permalink raw reply [flat|nested] 6+ messages in thread
* RE: Enabling mode for part of buffer / region
2020-01-23 10:14 Enabling mode for part of buffer / region Michael Anckaert
@ 2020-01-23 17:00 ` Drew Adams
2020-01-23 19:33 ` Michael Anckaert
2020-01-24 10:49 ` Michael Anckaert
1 sibling, 1 reply; 6+ messages in thread
From: Drew Adams @ 2020-01-23 17:00 UTC (permalink / raw)
To: Michael Anckaert, Help-gnu-emacs
> 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?
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Enabling mode for part of buffer / region
2020-01-23 17:00 ` Drew Adams
@ 2020-01-23 19:33 ` Michael Anckaert
2020-01-23 19:38 ` Drew Adams
0 siblings, 1 reply; 6+ messages in thread
From: Michael Anckaert @ 2020-01-23 19:33 UTC (permalink / raw)
To: Drew Adams; +Cc: Michael Anckaert, Help-gnu-emacs
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
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Enabling mode for part of buffer / region
2020-01-23 10:14 Enabling mode for part of buffer / region Michael Anckaert
2020-01-23 17:00 ` Drew Adams
@ 2020-01-24 10:49 ` Michael Anckaert
2020-01-24 11:16 ` tomas
1 sibling, 1 reply; 6+ messages in thread
From: Michael Anckaert @ 2020-01-24 10:49 UTC (permalink / raw)
To: Michael Anckaert; +Cc: Help-gnu-emacs
I wanted to document my problem and the solution I reached in case some
else needs it.
After some more searching (Thanks to Drew's reply I got pointed in the
direction of narrowing) I came up with a fairly simple and useful
solution.
What I want to accomplish is to edit part of a buffer in a different
mode than the buffer currently is. My biggest use case today is editing
a piece of code in an email. I compose my emails using Emacs and mu4e,
so my email buffer is in mu4e-compose-mode. But if I want to edit a
piece of lisp inside that buffer, I want to be able to take advantage of
lisp-mode.
The solution below enables me to narrow to a region and start a mode of
choice. When widening the previous buffer mode is restored.
Thanks to Drew and the folks on #Emacs for their pointers!
;;;; Narrow Edit Mode - NAD
;;;; Load the following elisp code in your Emacs. Run the function nad-edit-region to narrow the buffer to the marked region and switch to the mode you specified. After widening the buffer will restore to the previous mode.
(defvar *nad-saved-mode* nil)
(defun nad-store-mode (&rest args)
"Hook to add before narrow. Stores the current mode so we can restore it later"
(setf *nad-saved-mode* major-mode)
(message "Stored mode: %s" *nad-saved-mode*))
(defun nad-restore-mode (&rest args)
"hook to add after widen. Restores the saved mode"
(message "Restoring mode to %s" *nad-saved-mode*)
(funcall *nad-saved-mode*))
(defun nad-edit-region (mode)
"Function that prompts for the requested mode and narrows the buffer to better edit the current region"
(interactive "sMode to edit region in: ")
(narrow-to-region (mark) (point))
(message "Setting mode to %s" mode)
(funcall (intern mode)))
(advice-add 'narrow-to-region :before #'nad-store-mode)
(advice-add 'widen :after #'nad-restore-mode)
Michael Anckaert writes:
> Hello everyone,
>
> I'm sure I'm missing something obvious but I wondered if I can enable a
> mode for part of a buffer.
>
> 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?
>
> Kind regards
--
Michael Anckaert
+32 474 066 467
https://www.sinax.be
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: Enabling mode for part of buffer / region
2020-01-24 10:49 ` Michael Anckaert
@ 2020-01-24 11:16 ` tomas
0 siblings, 0 replies; 6+ messages in thread
From: tomas @ 2020-01-24 11:16 UTC (permalink / raw)
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 870 bytes --]
On Fri, Jan 24, 2020 at 11:49:57AM +0100, Michael Anckaert wrote:
> I wanted to document my problem and the solution I reached in case some
> else needs it.
[...]
Since I haven't seen it in the thread (apologies if I have
overseen it), I think this is a problem class which arises
in other contexts (e.g. Javascript embedded in HTML, SQL
embedded in some host language, etc.)
There is a whole flurry of solutions which have emerged
for that, generally under the title "multiple major modes"
Perhaps those are a bit heavyweight wrt what you had in
mind (Org doesn't try to have the whole major mode up and
running on the snippet, but offers editing in another buffer).
Emacs seems to be slowly converging towards something.
Here [1] is a good overview of what's available:
Cheers
https://www.emacswiki.org/emacs/MultipleModes
-- tomás
[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2020-01-24 11:16 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-01-23 10:14 Enabling mode for part of buffer / region Michael Anckaert
2020-01-23 17:00 ` Drew Adams
2020-01-23 19:33 ` Michael Anckaert
2020-01-23 19:38 ` Drew Adams
2020-01-24 10:49 ` Michael Anckaert
2020-01-24 11:16 ` tomas
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).