From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Luca Ferrari Newsgroups: gmane.emacs.help Subject: Re: creating a function that works for active region or whole buffer Date: Wed, 23 Jan 2013 08:43:01 +0100 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: ger.gmane.org 1358927003 8659 80.91.229.3 (23 Jan 2013 07:43:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 23 Jan 2013 07:43:23 +0000 (UTC) To: help-gnu-emacs Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jan 23 08:43:42 2013 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Txuzf-0005vH-FJ for geh-help-gnu-emacs@m.gmane.org; Wed, 23 Jan 2013 08:43:35 +0100 Original-Received: from localhost ([::1]:49356 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TxuzN-0004St-Em for geh-help-gnu-emacs@m.gmane.org; Wed, 23 Jan 2013 02:43:17 -0500 Original-Received: from eggs.gnu.org ([208.118.235.92]:48938) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TxuzD-0004SG-RY for help-gnu-emacs@gnu.org; Wed, 23 Jan 2013 02:43:13 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Txuz9-0007Go-7C for help-gnu-emacs@gnu.org; Wed, 23 Jan 2013 02:43:07 -0500 Original-Received: from mail-wg0-f47.google.com ([74.125.82.47]:38797) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Txuz9-0007Gi-15 for help-gnu-emacs@gnu.org; Wed, 23 Jan 2013 02:43:03 -0500 Original-Received: by mail-wg0-f47.google.com with SMTP id dq11so2061866wgb.14 for ; Tue, 22 Jan 2013 23:43:02 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:sender:in-reply-to:references:date :x-google-sender-auth:message-id:subject:from:to:content-type; bh=rCH/PuJl5ljpeuMKa0pFCK2+XhE+vGW97fL+xIzDluU=; b=Px/bt+n+dPntgNnVX7wD46yAn2mYSIpKYXJYhksI2fT8wXhym6YnpSNYgHAX7V6Lzu 4wDKgX6K2R5mR1bt8MBerz/3wCnLFbi5/KwYASUxh8qGWwt0giY8qkabvDyOiGinyvUx gUiyoIjANMzIQNntfPj52PAs+h5JnJA2Jm5QRk7slZ5F41BGDfUuZtlnoyhJiz6dJf/X ytmPf3SEAIfxonEci9pWmrj9VZssmMs0BOLpUQjz+w9+CWgCZeI/w9zAAOP9Cxbrr1BW WEIiuhtOIXudtmi5FS3sIXwWCl5dOQVnNlbmEXFTAroTdSbGe5mkIWIZgwbGc92FfDa0 EB9Q== X-Received: by 10.180.84.131 with SMTP id z3mr25367216wiy.25.1358926981932; Tue, 22 Jan 2013 23:43:01 -0800 (PST) Original-Received: by 10.194.39.1 with HTTP; Tue, 22 Jan 2013 23:43:01 -0800 (PST) In-Reply-To: X-Google-Sender-Auth: k65b80uLmG7x33DiQtu8-QmF4FU X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x [fuzzy] X-Received-From: 74.125.82.47 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:88776 Archived-At: Ops...right answer to the wrong thread, sorry! However below my function that is a simple indent behavior (suggestions are welcome): defun dataflex-indent-region-or-buffer () "This function indents the whole buffer or, in the case a region is active, the active region." (interactive) (let ( (current-block-end-line 0 ) (current-block-start-line 0) (current-indentation-step 0) (next-line-indentation-step 0) ) (progn ;; if using a region indent only such region, otherwise the whole buffer (if (use-region-p) ;; using a region... (setq current-block-start-line (line-number-at-pos (region-beginning) ) current-block-end-line (line-number-at-pos (region-end) ) ) ;; ...else use the whole buffer (setq current-block-start-line (line-number-at-pos (point-min) ) current-block-end-line (line-number-at-pos (point-max) ) ) ) ;; go to the starting line (goto-line current-block-start-line) ;; go to the beginning of the line (beginning-of-line) (while (<= current-block-start-line current-block-end-line ) (if (looking-at "^[ \t]*\\(IF\\|WHILE\\|BEGIN\\|LOOP\\)") ;; the BEGIN line has to be indented at the current level, and the next ;; line at a deeper level (setq next-line-indentation-step (+ current-indentation-step dataflex-mode-indent-step) ) ;; else if looking at an END line remove the indentation (if (looking-at "^[ \t]*\\(END\\|RETURN\\|ABORT\\)") (progn (setq current-indentation-step (- current-indentation-step dataflex-mode-indent-step) ) (setq next-line-indentation-step current-indentation-step ) ) ;; else if this is a label line reset the indentation (if (looking-at "^[ \t]*\\(.+\\):[ \t\n]*") (setq current-indentation-step 0 next-line-indentation-step dataflex-mode-indent-step-labels) ) ) ) ;; security check: do not indent at negative offset (if (< current-indentation-step 0 ) (setq current-indentation-step 0) ) (if (< next-line-indentation-step 0 ) (setq next-line-indentation-step 0 ) ) ;; do the indent of the current line and go forward (indent-line-to current-indentation-step) (setq current-indentation-step next-line-indentation-step) (setq current-block-start-line (1+ current-block-start-line)) (forward-line 1) ) ) ) )