From: Suraj Acharya <sacharya@bea.com>
Subject: Re: correct indentation for flet and labels macros
Date: Tue, 31 Aug 2004 19:06:46 -0700 [thread overview]
Message-ID: <ch3avg$9sv$1@sea.gmane.org> (raw)
In-Reply-To: <je7jrfzlot.fsf@sykes.suse.de>
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
prev parent reply other threads:[~2004-09-01 2:06 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
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 [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='ch3avg$9sv$1@sea.gmane.org' \
--to=sacharya@bea.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).