From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Michael Anckaert Newsgroups: gmane.emacs.help Subject: Re: Enabling mode for part of buffer / region Date: Fri, 24 Jan 2020 11:49:57 +0100 Message-ID: <875zh1163u.fsf@winston.localdomain> References: <87tv4mv5rg.fsf@winston.localdomain> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="15678"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: mu4e 1.1.0; emacs 26.3 Cc: Help-gnu-emacs@gnu.org To: Michael Anckaert Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Fri Jan 24 11:53:02 2020 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iuwaE-00042m-Lb for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 24 Jan 2020 11:53:02 +0100 Original-Received: from localhost ([::1]:40536 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuwaD-0006tN-PO for geh-help-gnu-emacs@m.gmane-mx.org; Fri, 24 Jan 2020 05:53:01 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:53487) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1iuwY5-0005Bb-QY for Help-gnu-emacs@gnu.org; Fri, 24 Jan 2020 05:51:57 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1iuwXO-000700-JJ for Help-gnu-emacs@gnu.org; Fri, 24 Jan 2020 05:50:49 -0500 Original-Received: from mail-wm1-x334.google.com ([2a00:1450:4864:20::334]:34844) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1iuwXO-0006oG-AZ for Help-gnu-emacs@gnu.org; Fri, 24 Jan 2020 05:50:06 -0500 Original-Received: by mail-wm1-x334.google.com with SMTP id p17so1264939wmb.0 for ; Fri, 24 Jan 2020 02:49:59 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sinax.be; s=google; h=references:user-agent:from:to:cc:subject:in-reply-to:date :message-id:mime-version; bh=C0QGssYul1sjoXLJKzScIR79G4AK+SjrpoSOl+9PVxU=; b=PB+TPdbbWzz/A171988wGDTDKWbKjIkdQL74YZtQQtD/t8dQI8UAZhz4nY8eaODPun MOLP/FkdTLCsKUVSjSvbwi0di5y+RamlWI13mU79cGYtew1hCozwfgp3rJQLI3di8Zcl 86QkupwlakbvgXM5XDV10ciodb4h2XAhASfbA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:references:user-agent:from:to:cc:subject :in-reply-to:date:message-id:mime-version; bh=C0QGssYul1sjoXLJKzScIR79G4AK+SjrpoSOl+9PVxU=; b=kVOnmzK31Y4iT22BS6HSbbWtI8uBVybpt6XBnCyk+Y3EmHWmUKEUIGFZOO5qkdgQBC kygA1jZC6expKPX2SWKvrfRRzukw0EaWeKOCQV5jCzXmD+zcDbXNmpuM5a+V/b+sdASG bjZ529pait++vlZAWBIx/ozxoR+LtOx9P4oct/wUkC21NpBc8SDVD6J6IkrAOeETw4Tv RQnEH9AoQiU1WFuwXvoZjK/w6OiKQmYFVsHjkeTJPD8wWuuyplWoGoDFGCvWn1ORkV/Y ZD7Fxx54nu1AWpqQeplPNZmBSxabT6wutRyagtX6Iq/YguD8eNMHMshsOGtwdXP8pf4x UjOQ== X-Gm-Message-State: APjAAAViPLo7XaYMrdKFpRm9+uaxIJiuz5NB5WX2F9qN454XojG5P2qk J9rtWQsYwRtR5lCyskG3oDpFNdrJievMyTQd X-Google-Smtp-Source: APXvYqy/1tXG30/xYJvC7Dm1OI5ovCq0ICBCyL2MtbZFdLkr2r12ghUt0dDCgOITapwKiOb8DHTB8w== X-Received: by 2002:a05:600c:2c06:: with SMTP id q6mr2928692wmg.154.1579862998354; Fri, 24 Jan 2020 02:49:58 -0800 (PST) Original-Received: from winston.localdomain (242.124-78-194.adsl-static.isp.belgacom.be. [194.78.124.242]) by smtp.gmail.com with ESMTPSA id f1sm6911388wrp.93.2020.01.24.02.49.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 Jan 2020 02:49:57 -0800 (PST) In-reply-to: <87tv4mv5rg.fsf@winston.localdomain> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::334 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.io gmane.emacs.help:122285 Archived-At: 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