unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* correct indentation for flet and labels macros
@ 2004-08-31  2:26 Suraj Acharya
  2004-08-31  2:45 ` Davis Herring
  0 siblings, 1 reply; 5+ messages in thread
From: Suraj Acharya @ 2004-08-31  2:26 UTC (permalink / raw)


Is there any way to get flet and labels to indent their definitions like 
defun?

I want

(flet ((foo (args)
          (bar)))


instead of


(flet ((foo (args)
             (bar)))


Suraj

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: correct indentation for flet and labels macros
  2004-08-31  2:26 correct indentation for flet and labels macros Suraj Acharya
@ 2004-08-31  2:45 ` Davis Herring
  2004-08-31  3:00   ` Suraj Acharya
  0 siblings, 1 reply; 5+ messages in thread
From: Davis Herring @ 2004-08-31  2:45 UTC (permalink / raw)
  Cc: emacs-devel

> Is there any way to get flet and labels to indent their definitions like 
> defun?

It looks like you used tabs (of unknown width) that got converted to some 
other width, so it's hard to say what you really want.  But that's not 
actually relevant: the problem is that the thing controlling the 
indentation of (bar) is actually `foo', since it's the function name in 
that sexp.  However, you can probably do something deep and dark with

(put 'flet 'lisp-indent-hook 'indent-flet)
(defun indent-flet (state indent-point) ...)

But beyond that all I know is to read the source for
`calculate-lisp-indent', `lisp-indent-defform', and friends.

Davis Herring

-- 
This product is sold by volume, not by mass.  If it seems too dense or too 
sparse, it means mass-energy conversion has occurred during shipping.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: correct indentation for flet and labels macros
  2004-08-31  2:45 ` Davis Herring
@ 2004-08-31  3:00   ` Suraj Acharya
  2004-08-31  9:44     ` Andreas Schwab
  0 siblings, 1 reply; 5+ messages in thread
From: Suraj Acharya @ 2004-08-31  3:00 UTC (permalink / raw)


Davis Herring wrote:

>>Is there any way to get flet and labels to indent their definitions like 
>>defun?
> 
> 
> It looks like you used tabs (of unknown width) that got converted to some 
> other width, so it's hard to say what you really want.  But that's not 
> actually relevant: the problem is that the thing controlling the 
> indentation of (bar) is actually `foo', since it's the function name in 
> that sexp.  However, you can probably do something deep and dark with
> 
> (put 'flet 'lisp-indent-hook 'indent-flet)
> (defun indent-flet (state indent-point) ...)
> 
> But beyond that all I know is to read the source for
> `calculate-lisp-indent', `lisp-indent-defform', and friends.
> 
> Davis Herring
> 


I'm sorry, and besides foo was probably too short a name to show the 
difference well anyways. Here's what I meant:

I'd like to get

(flet ((really-long-function-name (args)
          (bar)))


instead of


(flet ((really-long-function-name (args)
                                   (bar)))

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 ? Perhaps I can 
adapt that code.

Suraj

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: correct indentation for flet and labels macros
  2004-08-31  3:00   ` Suraj Acharya
@ 2004-08-31  9:44     ` Andreas Schwab
  2004-09-01  2:06       ` Suraj Acharya
  0 siblings, 1 reply; 5+ messages in thread
From: Andreas Schwab @ 2004-08-31  9:44 UTC (permalink / raw)
  Cc: emacs-devel

Suraj Acharya <sacharya@bea.com> 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.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Maxfeldstraße 5, 90409 Nürnberg, Germany
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: correct indentation for flet and labels macros
  2004-08-31  9:44     ` Andreas Schwab
@ 2004-09-01  2:06       ` Suraj Acharya
  0 siblings, 0 replies; 5+ messages in thread
From: Suraj Acharya @ 2004-09-01  2:06 UTC (permalink / raw)


Andreas Schwab wrote:

> Suraj Acharya <sacharya@bea.com> 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.
> 
> Andreas.
> 

So here's my hack to get the indentation I want. It turned out to be a little more
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 lisp-indent-function
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
         (progn
           (if (not (> (save-excursion (forward-line 1) (point))
                       calculate-lisp-indent-last-sexp))
		(progn (goto-char calculate-lisp-indent-last-sexp)
		       (beginning-of-line)
		       (parse-partial-sexp (point)
					   calculate-lisp-indent-last-sexp 0 t)))
	    ;; 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.
           (backward-prefix-chars)
           (current-column))
       (let ((function (buffer-substring (point)
					(progn (forward-sexp 1) (point))))
	    method)
	(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 normal-indent)))))
+
+
+
+(defun apply-lisp-indent-function (state method function indent-point normal-indent)
+  (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) (point))))
+                  t)
+                (setq method (get (intern-soft function) 'lisp-indent-children-function))
+           ))
+         (apply-lisp-indent-function state method function indent-point normal-indent)
+         )))


Suraj

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2004-09-01  2:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-08-31  2:26 correct indentation for flet and labels macros Suraj Acharya
2004-08-31  2:45 ` Davis Herring
2004-08-31  3:00   ` Suraj Acharya
2004-08-31  9:44     ` Andreas Schwab
2004-09-01  2:06       ` Suraj Acharya

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).