From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Suraj Acharya Newsgroups: gmane.emacs.devel Subject: Re: correct indentation for flet and labels macros Date: Tue, 31 Aug 2004 19:06:46 -0700 Sender: emacs-devel-bounces+ged-emacs-devel=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 1094004566 10197 80.91.224.253 (1 Sep 2004 02:09:26 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 1 Sep 2004 02:09:26 +0000 (UTC) Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 01 04:09:19 2004 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1C2KYp-0007MV-00 for ; Wed, 01 Sep 2004 04:09:19 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C2Kdj-000460-0n for ged-emacs-devel@m.gmane.org; Tue, 31 Aug 2004 22:14:23 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1C2Kda-00045v-Oo for emacs-devel@gnu.org; Tue, 31 Aug 2004 22:14:14 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1C2Kda-00045j-1s for emacs-devel@gnu.org; Tue, 31 Aug 2004 22:14:14 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1C2KdZ-00045g-Vg for emacs-devel@gnu.org; Tue, 31 Aug 2004 22:14:14 -0400 Original-Received: from [80.91.224.249] (helo=main.gmane.org) by monty-python.gnu.org with esmtp (Exim 4.34) id 1C2KYO-0005wn-UI for emacs-devel@gnu.org; Tue, 31 Aug 2004 22:08:53 -0400 Original-Received: from list by main.gmane.org with local (Exim 3.35 #1 (Debian)) id 1C2KYN-0004ms-00 for ; Wed, 01 Sep 2004 04:08:52 +0200 Original-Received: from 63.96.165.58 ([63.96.165.58]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 01 Sep 2004 04:08:51 +0200 Original-Received: from sacharya by 63.96.165.58 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 01 Sep 2004 04:08:51 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-To: emacs-devel@gnu.org Original-Lines: 133 Original-X-Complaints-To: usenet@sea.gmane.org X-Gmane-NNTP-Posting-Host: 63.96.165.58 User-Agent: Mozilla Thunderbird 0.7.1 (Windows/20040626) X-Accept-Language: en-us, en In-Reply-To: X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:26670 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:26670 Andreas Schwab wrote: > Suraj Acharya 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