help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Indenting Strings (How to?)


From: Martin Stone Davis
Subject: Re: Indenting Strings (How to?)
Date: Mon, 29 Dec 2003 19:49:17 -0800
User-agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007

Dan Anderson wrote:
Barry Margolin <address@hidden> writes:


In article <address@hidden>,
Dan Anderson <address@hidden> 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






reply via email to

[Prev in Thread] Current Thread [Next in Thread]