From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.devel Subject: Re: How to run indent command in arbitrary buffer while respecting user settings? Date: Fri, 30 Jun 2023 08:50:12 +0300 Message-ID: <833529ts6j.fsf@gnu.org> References: <87352avys3.fsf@localhost> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="22543"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Ihor Radchenko Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Jun 30 07:50:38 2023 Return-path: Envelope-to: ged-emacs-devel@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 1qF71i-0005iD-OU for ged-emacs-devel@m.gmane-mx.org; Fri, 30 Jun 2023 07:50:38 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qF70u-0007qj-Ta; Fri, 30 Jun 2023 01:49:48 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qF70t-0007qR-6t for emacs-devel@gnu.org; Fri, 30 Jun 2023 01:49:47 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qF70s-0001rq-4O; Fri, 30 Jun 2023 01:49:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=eqfBeKc3KFxhpcivfMlsV1nSJtL6r8VmHMGGPDs37kU=; b=WN6Im4KZipmYVapPP/yz fYsf4N0IH5lHL2wstDaFDMivpLWkIqMp5Y82oC9wiZ+GcFRf8uB3CXKmYuZSKTUzEXoNKKtRt0N/q DR6V0QGLeR6gGz6Nim35dyrU+gfbjUStnWo4ZCcB46Uf+LzljRgbbcXZavZjxpVjO6s9x+tQwUaFT LRgPTsfKNl6BHM/sn/1YL9nlPf5/USmzwLvJVBPN8ed8i5tT19Xd5XnKUlAADVmTI7JeaNSzgWCPH 0fF2PeCqa1MDC79U8Yr4j2i84d53JRix/GJ78vGDImgmtRXOS0S5wIl8fS1e1VDLHsF7rjFXqJgPO nhtd52ZJo0EEuQ==; Original-Received: from [87.69.77.57] (helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qF70o-0000qJ-F5; Fri, 30 Jun 2023 01:49:43 -0400 In-Reply-To: <87352avys3.fsf@localhost> (message from Ihor Radchenko on Thu, 29 Jun 2023 19:44:44 +0000) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:307310 Archived-At: > From: Ihor Radchenko > Date: Thu, 29 Jun 2023 19:44:44 +0000 > > In Org mode, we sometimes have a need to use third-party major mode to > do indentation: > > #+begin_src emacs-lisp > (when t > 'indent-me) > #+end_src > > To do this, we currently create a major mode buffer containing the code > and run whatever is bound to there. > > This, however, is broken when user re-binds to something else. > Or when something like evil-mode re-binds > (https://github.com/emacs-evil/evil/issues/1806) > > I am now considering running > (funcall-interactively #'indent-for-tab-command) > > However, looking at 25 Indentation section of Elisp manual: > > The simplest way to perform indentation is the key. In most > major modes, this runs the command ‘indent-for-tab-command’. (In C and > related modes, runs the command ‘c-indent-line-or-region’, which > behaves similarly, *note C Indent::). > > I noticed that C and related modes have some alternative setting for > indentation. So, using `indent-for-tab-command' appear to be not as > reliable, after all. > > Is there any reliable way to perform indentation in an arbitrary major > mode programmatically? > > I notice that cc-mode.el has a comment that the current re-binding > is at least debatable: > > (substitute-key-definition 'indent-for-tab-command > ;; XXX Is this the right thing to do > ;; here? > 'c-indent-line-or-region > c-mode-base-map global-map) > > So, should I simply ignore what cc-mode does and go ahead relying upon > `indent-for-tab-command' doing the right thing? There are other, better alternatives: you could either call indent-according-to-mode or (funcall indent-line-function) instead. Yet another alternative is to use indent-region-function.