From: Michael Anckaert <michael.anckaert@sinax.be>
To: Michael Anckaert <michael.anckaert@sinax.be>
Cc: Help-gnu-emacs@gnu.org
Subject: Re: Enabling mode for part of buffer / region
Date: Fri, 24 Jan 2020 11:49:57 +0100 [thread overview]
Message-ID: <875zh1163u.fsf@winston.localdomain> (raw)
In-Reply-To: <87tv4mv5rg.fsf@winston.localdomain>
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
next prev parent reply other threads:[~2020-01-24 10:49 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
2020-01-24 11:16 ` tomas
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=875zh1163u.fsf@winston.localdomain \
--to=michael.anckaert@sinax.be \
--cc=Help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).