From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Martin Stone Davis Newsgroups: gmane.emacs.help Subject: Re: Indenting Strings (How to?) Date: Mon, 29 Dec 2003 19:49:17 -0800 Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Trace: sea.gmane.org 1072756488 23520 80.91.224.253 (30 Dec 2003 03:54:48 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 30 Dec 2003 03:54:48 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Dec 30 04:54:45 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AbAxx-0006Wx-00 for ; Tue, 30 Dec 2003 04:54:45 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AbBsp-0006Ns-1F for geh-help-gnu-emacs@m.gmane.org; Mon, 29 Dec 2003 23:53:31 -0500 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.24) id 1AbBsc-0006Nk-US for help-gnu-emacs@gnu.org; Mon, 29 Dec 2003 23:53:18 -0500 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.24) id 1AbBs6-0006MY-0k for help-gnu-emacs@gnu.org; Mon, 29 Dec 2003 23:53:17 -0500 Original-Received: from [80.91.224.249] (helo=main.gmane.org) by monty-python.gnu.org with esmtp (Exim 4.24) id 1AbBs5-0006MV-IV for help-gnu-emacs@gnu.org; Mon, 29 Dec 2003 23:52:45 -0500 Original-Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1AbAub-0005gT-00 for ; Tue, 30 Dec 2003 04:51:17 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-To: help-gnu-emacs@gnu.org Original-Received: from sea.gmane.org ([80.91.224.252]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1AbAua-0005gL-00 for ; Tue, 30 Dec 2003 04:51:16 +0100 Original-Received: from news by sea.gmane.org with local (Exim 3.35 #1 (Debian)) id 1AbAuZ-00063w-00 for ; Tue, 30 Dec 2003 04:51:15 +0100 Original-Lines: 81 Original-X-Complaints-To: usenet@sea.gmane.org User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-us, en In-Reply-To: X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.2 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.org@gnu.org Xref: main.gmane.org gmane.emacs.help:15627 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:15627 Dan Anderson wrote: > Barry Margolin writes: > > >>In article , >> Dan Anderson wrote: >> >> >>> A lot of times when I'm coding I'll have a very long string or >>>comments which is some other kind of code (i.e. HTML or CSS embedded >>>in a Perl CGI script) or is text. Many times I'll try to keep the >>>indentation neat, but pressing tab in a string (or comments) doesn't >>>do anything (in CPerl mode, PHP mode, or any other mode). This means >>>that I end up having to space over manually (a royal PITA). >>> >>> Is there a good way to tell emacs to either treat all comments >>>and strings as normal text (i.e so I can get basic tabbing and >>>justification), or (even better), to set rules concerning how to treat >>>comments and strings. >> >>Type C-q TAB to insert a literal TAB character. > > > Thanks for trying to help but this is not quite what I want. > I want emacs to automatically tab my code and keep it neat and orderly > like it does outside of comments and strings. > > -Dan Ah, so *that's* what you want. I've always wondered why Emacs isn't set up to do that automatically. Hopefully this will work for you when added to .emacs. I coded it myself :) ;;BEGIN (defvar change-start nil) (defvar change-end nil) (make-variable-buffer-local 'change-start) (make-variable-buffer-local 'change-end) (defun mlisp-after-change-function (start end pre-change-length) (setf change-start (if change-start (min change-start start) start)) (setf change-end (if change-end (max change-end end) end))) (defun mlisp-post-command-hook () (when change-start (indent-region 0 (buffer-size) nil) (setf change-start nil) (setf change-end nil))) (add-hook 'after-change-functions 'mlisp-after-change-function nil nil) (add-hook 'post-command-hook 'mlisp-post-command-hook nil nil) ;;END The m in "mlisp" just stands for me, Martin. The change-start, change-end were originally there because I wanted to call indent-region to be a little bit more efficient when it indented. However, I ended up with very strange indents sometimes when I used `(indent-region change-start (buffer-size) nil)'. Try it yourself and then position your point just before the b in: (a b) (c d) If you hit the spacebar then, you'll find that strangely fourth line (with the `d') becomes completely unindented. But if you use the above code, it should work properly. It's a little inefficient, but it does the job for me, so far. Good luck, and let me know if you find something better. -Martin