From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Andreas =?iso-8859-1?q?R=F6hler?= Newsgroups: gmane.emacs.devel Subject: Re: sh-script beg-end of function Date: Fri, 23 Nov 2007 16:33:36 +0100 Message-ID: <200711231633.37286.andreas.roehler@online.de> References: <200711192143.15056.andreas.roehler@online.de> <200711221956.15254.andreas.roehler@online.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1195832222 15182 80.91.229.12 (23 Nov 2007 15:37:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 23 Nov 2007 15:37:02 +0000 (UTC) Cc: Stefan Monnier , Richard Stallman To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Nov 23 16:37:08 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1Ivaaa-0008Te-8N for ged-emacs-devel@m.gmane.org; Fri, 23 Nov 2007 16:37:08 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IvaaL-00020K-EH for ged-emacs-devel@m.gmane.org; Fri, 23 Nov 2007 10:36:53 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IvaaC-0001uc-Ct for emacs-devel@gnu.org; Fri, 23 Nov 2007 10:36:44 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IvaaA-0001rp-Ex for emacs-devel@gnu.org; Fri, 23 Nov 2007 10:36:43 -0500 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IvaaA-0001rQ-9I for emacs-devel@gnu.org; Fri, 23 Nov 2007 10:36:42 -0500 Original-Received: from moutng.kundenserver.de ([212.227.126.174]) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1Ivaa5-00070n-Aq; Fri, 23 Nov 2007 10:36:37 -0500 Original-Received: from noname (p54BEBADA.dip0.t-ipconnect.de [84.190.186.218]) by mrelayeu.kundenserver.de (node=mrelayeu4) with ESMTP (Nemesis) id 0ML21M-1IvaZq3mwc-0002Md; Fri, 23 Nov 2007 16:36:32 +0100 User-Agent: KMail/1.9.5 In-Reply-To: Content-Disposition: inline X-Provags-ID: V01U2FsdGVkX19X4w0AYkNQUVJl+jsPCBLC3hFhXKg4amHM2rt h7HurpxYTP+v/0WXlQkxLqKpQQ6J31/GxSJKKQnySLrSW7SREj mLbxes0gn/A+RqEaV25jw== X-detected-kernel: by monty-python.gnu.org: Linux 2.6? (barebone, rare!) 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: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:83973 Archived-At: Am Donnerstag, 22. November 2007 22:49 schrieb Stefan Monnier: > > Your code adresses a function in it's literally sence, > > whereas my code adresses "top-level-form", a more > > abstract thing. A "top-level-form" form BTW already > > is adressed by Emacs-Lisp `end-of-defun' or > > `beginning-of-defun' and I already remarked the naming > > as somehow misleading therefore, but that's a matter > > from the past. > > I understand, but the way I see it, either you're in an sh buffer which > has functions, in which case the defun-prompt-regexp will work fine, or > you're in an sh buffer which basically only contains "unstructured" > "straight-line" code. You want to cater to this latter case as well. > > It might be OK, but in my experience which units are useful in this case > is hard to know in general because it depends a lot on the writing style > used (which is anything but standardized sadly). In many cases > paragraph-based navigation will work best. > That's true. Took your hint to make changes rely on paragraph-. > I don't claim that my defun-prompt-regexp setting is the > end-all-be-all here. It's just a good starting point. But also any > replacement should be at least as good. Most importantly: jumping to > the end of a real function should jump to the closing "}". > Not, if exists usually no closing "}" while writing. I prefer to set closings last and manually - the reports I get then are more valuable than possible savings before. > If you want to submit an improvement, please send it as a patch against > the current sh-script.el code. This will make it easier for us to > integrate your code. Here a small diff to enable modes to set beginning-of-defun-function withou= t=20 disturbing each other: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; diff -c emacs-lisp/lisp.el lisp.el *** /emacs-lisp/lisp.el 2007-07-26 07:26:47.000000000 +0200 =2D-- lisp.el 2007-11-23 15:49:52.000000000 +0100 *************** *** 178,183 **** =2D-- 178,184 ---- The function (of no args) should go to the line on which the current defun starts, and return non-nil, or should return nil if it can't find the beginning.") + (make-variable-buffer-local 'beginning-of-defun-function) =20 (defun beginning-of-defun (&optional arg) "Move backward to the beginning of a defun. *************** *** 291,296 **** =2D-- 292,298 ---- This is used to find the end of the defun instead of using the normal recipe (see `end-of-defun'). Major modes can define this if the normal method is not appropriate.") + (make-variable-buffer-local 'end-of-defun-function) =20 (defun buffer-end (arg) "Return the \"far end\" position of the buffer, in direction ARG. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; > BTW, `end-of-defun-function' seems to be used in end-of-defun in > a ... weird way, so maybe you'll want to fix that first.=20 This should be done with this diff above. Remaining bugs AFAIS occur occasionally but are deep-rooted from underlying move-functions and not to adress here. > Maybe I'll=20 > post a suggestion about it on this list later. > > > Stefan > > > PS: Comments about your original code: > - the "defcustom beginning-of-defun-function" is wrong: you can only > use defcustom for variable you *create* (that are your own), not to set > variables defined in other packages. OK, thanks. Dropped that part. > - The docstring of sh-beginning-of-function is unusable. It could > instead explain what is considered as a "defun". Changed. > - Instead of (parse-partial-sexp (point-min) (point)), you can use > syntax-ppss. Dropped that part of code completely.=20 BTW, reading doku of `syntax-ppss' "The returned value is the same as `parse-partial-sexp' except that the 2nd and 6th values of the returned state cannot be relied upon."=20 I say: so let's take `parse-partial-sexp' and rely upon... Maybe I'm wrong here? > - the docstring of `comment-beginning' says "Find the beginning of the > enclosing comment" so I'm wondering why you decided to use that in > a context where you have no idea whether or not you're inside > a comment. Why don't you just use (forward-comment (- (point-max))) Thanks. Simply wasn't aware of that. Rewrote the code. Changed "function" to "form" too in order to avoid misunderstandings. > and skip the=20 > newcomment madness =20 Wouldn't dare to speak that out :) =2E.. Thanks again Andreas R=F6hler ;;; sh-beg-end.el --- Something for C-M-a, ;;; C-M-e, M-a and M-e in shell-script-mode ;; Copyright (C) 2007 by Andreas R=F6hler ;; ;; Keywords: languages ;; This file is free software; you can redistribute it ;; and/or modify it under the terms of the GNU General ;; Public License as published by the Free Software ;; Foundation; either version 3, or (at your option) ;; any later version. ;; This file is distributed in the hope that it will be ;; useful, but WITHOUT ANY WARRANTY; without even the ;; implied warranty of MERCHANTABILITY or FITNESS FOR A ;; PARTICULAR PURPOSE. See the GNU General Public ;; License for more details. ;; You should have received a copy of the GNU General ;; Public License along with GNU Emacs; see the file ;; COPYING. If not, write to the Free Software ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, ;; Boston, MA 02110-1301, USA. ;;; Commentary:=20 ;; C-M-a, C-M-e: jump to the beginning or end of a ;; top-level-form in sh-mode - "Shell-script"-mode.=20 ;; M-a, M-e: jump to the beginning or end of command in ;; a given line, forward or backward next beginning or ;; end with. With argument do this as many times. ;;; Code: ;; this belongs into sh-script.el (make-variable-buffer-local 'beginning-of-defun-function) (make-variable-buffer-local 'end-of-defun-function) =20 (defcustom sh-beginning-of-form-regexp "^[A-Za-z_][A-Za-z_0-9]*" " " :type 'regexp :group 'lisp) (defun sh-beginning-of-form () "Move to the beginning of a top-level-form in sh-script. With numeric argument, do it that many times." (interactive) (re-search-backward sh-beginning-of-form-regexp nil t 1)) (defun sh-end-of-form (&optional arg) "Move to the end of a top-level-form in sh-script. With numeric argument, do it that many times." (interactive "p") (let ((arg (or arg 1))) (while (forward-comment 1) (forward-comment 1)) (unless (looking-back "^[ \t]*") (setq arg (1+ arg))) (forward-paragraph arg) (skip-chars-backward " \t\r\n\f") (unless (looking-at "}") (back-to-indentation)) (if (looking-back "^[ \t]+") (progn=20 (end-of-line)=20 (sh-end-of-form)) (end-of-line) (skip-chars-backward " \t\r\n\f")))) =20 (defun sh-set-beginning-of-form () "'sh-beginning-of-form" (interactive) (setq beginning-of-defun-function 'sh-beginning-of-form)) (defun sh-set-end-of-form () "'sh-end-of-form" (interactive) (setq end-of-defun-function 'sh-end-of-form)) (defun sh-beginning-of-command (&optional arg)=20 "Move point to successive beginnings of commands." (interactive "p") (let ((arg (or arg 1)) (pos (point))) (back-to-indentation) (unless (eq pos (point)) (setq arg (1- arg))) (while (< 0 arg) (forward-line (- arg)) (setq arg (1- arg)) ;; skip comments and empty lines and closing braces (let ((pos (point))) (if (forward-comment -1) (while (forward-comment -1) (forward-comment -1)) (goto-char pos))) (while (or (empty-line-p) (looking-at "}")) (forward-line -1)) (back-to-indentation)))) (defun sh-end-of-command (&optional arg)=20 "Move point to successive ends of commands." (interactive "p") (let ((arg (or arg 1)) (pos (point))) (end-of-line) (skip-chars-backward " \t\r\n\f" (line-beginning-position)) (unless (eq pos (point)) (setq arg (1- arg))) (while (< 0 arg) (forward-line arg) (setq arg (1- arg))) (end-of-line) (skip-chars-backward " \t\r\n\f" (line-beginning-position)) (while (or (empty-line-p) (forward-comment 1))=20 (forward-line 1) (end-of-line)) (skip-chars-backward " \t\r\n\f"))) (add-hook 'sh-mode-hook 'sh-set-beginning-of-form) (add-hook 'sh-mode-hook 'sh-set-end-of-form) (provide 'sh-beg-end) ;;; sh-beg-end.el ends here