[Top][All Lists]

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

Re: correct indentation for flet and labels macros

From: Suraj Acharya
Subject: Re: correct indentation for flet and labels macros
Date: Tue, 31 Aug 2004 19:06:46 -0700
User-agent: Mozilla Thunderbird 0.7.1 (Windows/20040626)

Andreas Schwab wrote:

Suraj Acharya <address@hidden> writes:

The latter is the behavior is would get from let, but since the arguments
are special for flet and labels it would be nice the former. How does
defun get its special indentation for example ?

Just by virtue of being named starting with "def".  See
lisp-indent-function, which calls lisp-indent-defform in this case.  But
the function responsible for indenting flet is lisp-indent-specform,
because flet has a lisp-indent-function property with an integer value.


So here's my hack to get the indentation I want. It turned out to be a little 
involved than setting a special indentation function for flet because the way 
the default
lisp-indent-function is set up, flet does not have any control over the 
indentation of its
descendants beyond its immediate children. So I had to add a property called
lisp-indent-children-function which is in the same spirit as 
lisp-indent-function, ie you can
set it to 'defun, or a number or a function, but it's only invoked if the 
for the function name being indented is nil.

So for

(flet ((really-long-function-name (args)
         (length args))
       (other-function (args)
         (blah args)))
  (really-long-function-name 'a))

Since "really-long-function-name" and "other-function" don't have any special 
indentation functions set
for them the lisp-indent-children-function property for their parent "flet" is 
used instead.

+(put 'flet 'lisp-indent-children-function 'defun)
+(put 'labels 'lisp-indent-children-function 'defun)

(defun lisp-indent-function (indent-point state)
  "This function is the normal value of the variable `lisp-indent-function'.
It is used when indenting a line within a function call, to see if the
called function says anything special about how to indent the line.

INDENT-POINT is the position where the user typed TAB, or equivalent.
Point is located at the point to indent under (for default indentation);
STATE is the `parse-partial-sexp' state for that position.

If the current line is in a call to a Lisp function
which has a non-nil property `lisp-indent-function',
that specifies how to do the indentation.  The property value can be
* `defun', meaning indent `defun'-style;
* an integer N, meaning indent the first N arguments specially
like ordinary function arguments and then indent any further
arguments like a body;
* a function to call just as this function was called.
If that function returns nil, that means it doesn't specify
the indentation.

This function also returns nil meaning don't specify the indentation."
  (let ((normal-indent (current-column)))
    (goto-char (1+ (elt state 1)))
    (parse-partial-sexp (point) calculate-lisp-indent-last-sexp 0 t)
    (if (and (elt state 2)
             (not (looking-at "\\sw\\|\\s_")))
        ;; car of form doesn't seem to be a symbol
          (if (not (> (save-excursion (forward-line 1) (point))
                (progn (goto-char calculate-lisp-indent-last-sexp)
                       (parse-partial-sexp (point)
                                           calculate-lisp-indent-last-sexp 0 
            ;; Indent under the list or under the first sexp on the same
            ;; line as calculate-lisp-indent-last-sexp.  Note that first
            ;; thing on that line has to be complete sexp since we are
          ;; inside the innermost containing sexp.
      (let ((function (buffer-substring (point)
                                        (progn (forward-sexp 1) (point))))
        (setq method (or (get (intern-soft function) 'lisp-indent-function)
                         (get (intern-soft function) 'lisp-indent-hook)))
-       (cond ((or (eq method 'defun)
-                  (and (null method)
-                       (> (length function) 3)
-                       (string-match "\\`def" function)))
-              (lisp-indent-defform state indent-point))
-             ((integerp method)
-              (lisp-indent-specform method state
-                                    indent-point normal-indent))
-             (method
-               (funcall method state indent-point)))))))
+        (apply-lisp-indent-function state method function indent-point 
+(defun apply-lisp-indent-function (state method function indent-point 
+  (cond ((or (eq method 'defun)
+             (and (null method)
+                  (> (length function) 3)
+                  (string-match "\\`def" function)))
+         (lisp-indent-defform state indent-point))
+        ((integerp method)
+         (lisp-indent-specform method state
+                               indent-point normal-indent))
+        (method
+         (funcall method state indent-point))
+        ((save-excursion
+           (and (null method)
+                (elt state 0)
+                (> (elt state 0) 1)
+                (progn (backward-up-list 2)
+                       (down-list 1)
+                       (not (looking-at "\\sw\\|\\s_")))
+                (progn
+                  (backward-up-list 2)
+                  (down-list 1)
+                  (setq function (buffer-substring (point)
+                                                 (progn (forward-sexp 1) 
+                  t)
+                (setq method (get (intern-soft function) 
+           ))
+         (apply-lisp-indent-function state method function indent-point 
+         )))


reply via email to

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