From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Yuan Fu Newsgroups: gmane.emacs.devel Subject: Re: Understanding end-of-defun Date: Thu, 10 Nov 2022 14:35:15 -0800 Message-ID: References: <46FF2036-9AB4-4C57-82B5-51260D287FC5@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.1\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_C2283A1A-0201-4FEF-8286-CB8B56311190" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40465"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel To: Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Thu Nov 10 23:36:03 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1otG9P-000A7P-RH for ged-emacs-devel@m.gmane-mx.org; Thu, 10 Nov 2022 23:35:59 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1otG8p-0008OF-2Z; Thu, 10 Nov 2022 17:35:23 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1otG8n-0008O3-Jq for emacs-devel@gnu.org; Thu, 10 Nov 2022 17:35:21 -0500 Original-Received: from mail-pg1-x52c.google.com ([2607:f8b0:4864:20::52c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1otG8l-0004t4-DJ for emacs-devel@gnu.org; Thu, 10 Nov 2022 17:35:21 -0500 Original-Received: by mail-pg1-x52c.google.com with SMTP id e129so2915885pgc.9 for ; Thu, 10 Nov 2022 14:35:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:from:to:cc:subject:date:message-id:reply-to; bh=mAqa0+a5syZUfvR49eSL2Q9hiKBXrAF9xoXSi7YBJf4=; b=cmgzwVsV2/VO1uxYfsH16HesnvYdRr7eDbGc8x3IhsKhn4+xoqzBR7mrCfN8wx+hHG ZKTA4YivQB8gD21bRG75YlsV5fStlctbdb20CrPS329rYjIQ+teUVe82C92eZi5dLv3x UhVFz+NE+HKwePAmw7Db/2ezF0iHMFoLqoz9pPkPJ0YLNsZmp76TMnefPq/A5z299jCs qqlf+bmk8gkQDR/1/sXpkIlqjFwosVxKOmzHPT1CfCe27E/+p2XZQ9yfdUp5wjzRmC/6 jwg3zWz9kAviqdF4vEngxxfXJxCkoRbWMwyeXJKKzD99zaTa92vG4Nt+hNzbfhlUgpap yC3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=references:to:cc:in-reply-to:date:subject:mime-version:message-id :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=mAqa0+a5syZUfvR49eSL2Q9hiKBXrAF9xoXSi7YBJf4=; b=yHri0wIp6inl+K5500iQwdFLMUFyGYB1qq78wdVWSIcjHf3skHPext1fGK0GAiKxeN 5zOnA81D8m9ELV1y43DlE22xbEL9SUbYkkC7PLQLWcIRFTooSL5jDBUNQj/vPW6bMM26 buG2lmFDYr6k9wV+z5b1Qfw2A6v8M+k20kUsiVFdUBbEvpcPmC/UUf2BK0EF7gf40DYE iuMU21SbHQUn/EXcWFd4Dt+CjxesTBbGGTIfF1+UIc+COiX6MiHxJ48s8cH7S4KV6da3 +OJEepXuxDWj4MJrOQYlmpE5qzU/jx3/NOW43pcWMt970+t9Tqlza0FS80LD1VLPRzm0 mGzQ== X-Gm-Message-State: ACrzQf2BprgC5n2J6tSvBy7CmhFthzXPA3X5OxNmgPMUUXJ+gNeqH7nX ws/EDc8kHx6W47GJJSVfPug= X-Google-Smtp-Source: AMsMyM7CBFbN66lHHrd/o2rbGJPmq69Aj6zceF3pfy2yJQpHpIeLEpUI2diPzyecdHVu8Q73C676ZQ== X-Received: by 2002:a62:6d82:0:b0:56d:a89d:fb9b with SMTP id i124-20020a626d82000000b0056da89dfb9bmr3839243pfc.24.1668119716716; Thu, 10 Nov 2022 14:35:16 -0800 (PST) Original-Received: from smtpclient.apple (cpe-172-117-161-177.socal.res.rr.com. [172.117.161.177]) by smtp.gmail.com with ESMTPSA id 5-20020a170902c20500b0017f36638010sm162986pll.276.2022.11.10.14.35.15 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Nov 2022 14:35:16 -0800 (PST) In-Reply-To: X-Mailer: Apple Mail (2.3696.120.41.1.1) Received-SPF: pass client-ip=2607:f8b0:4864:20::52c; envelope-from=casouri@gmail.com; helo=mail-pg1-x52c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:299526 Archived-At: --Apple-Mail=_C2283A1A-0201-4FEF-8286-CB8B56311190 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 > On Oct 27, 2022, at 8:25 AM, Stefan Monnier = wrote: >=20 >>> No, you're quite right. We should double check that >>> `beginning-of-defun-raw` was successful (and do something else if = not). >> How about: >=20 > I haven't had a chance to try it out, but it looks good to me, tho > `beginning-of-defun-raw` doesn't mention anything about its return = value > in the its docstring, so we may want to tweak that docstring (and > review its code to make sure that it indeed returns the value we need > in all cases, tho I suspect you've done so already). (Sorry for the delay) Yes, it returns nil in all cases. Pretty sure = that=E2=80=99s the intent from the very beginning (looking at git logs). = How about this patch? Yuan --Apple-Mail=_C2283A1A-0201-4FEF-8286-CB8B56311190 Content-Disposition: attachment; filename=end-of-defun.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="end-of-defun.diff" Content-Transfer-Encoding: 7bit diff --git a/lisp/emacs-lisp/lisp.el b/lisp/emacs-lisp/lisp.el index acae1a0b0a9..c8d05a084bb 100644 --- a/lisp/emacs-lisp/lisp.el +++ b/lisp/emacs-lisp/lisp.el @@ -375,7 +375,10 @@ beginning-of-defun-raw is non-nil. If variable `beginning-of-defun-function' is non-nil, its value -is called as a function to find the defun's beginning." +is called as a function to find the defun's beginning. + +Return non-nil if this function successfully found the beginning +of a defun, nil if it failed to find one." (interactive "^p") ; change this to "P", maybe, if we ever come to pass ARG ; to beginning-of-defun-function. (unless arg (setq arg 1)) @@ -543,6 +546,7 @@ end-of-defun (push-mark)) (if (or (null arg) (= arg 0)) (setq arg 1)) (let ((pos (point)) + (success nil) (beg (progn (when end-of-defun-moves-to-eol (end-of-line 1)) (beginning-of-defun-raw 1) (point))) @@ -567,9 +571,12 @@ end-of-defun (setq arg (1- arg)) ;; We started from after the end of the previous function. (goto-char pos)) + ;; At this point, point either didn't move (because we started + ;; in between two defun's), or is at the end of a defun + ;; (because we started in the middle of a defun). (unless (zerop arg) - (beginning-of-defun-raw (- arg)) - (funcall end-of-defun-function))) + (when (setq success (beginning-of-defun-raw (- arg))) + (funcall end-of-defun-function)))) ((< arg 0) ;; Moving backward. (if (< (point) pos) @@ -579,16 +586,18 @@ end-of-defun ;; We started from inside a function. (goto-char beg)) (unless (zerop arg) - (beginning-of-defun-raw (- arg)) - (setq beg (point)) - (funcall end-of-defun-function)))) + (when (setq success (beginning-of-defun-raw (- arg))) + (setq beg (point)) + (funcall end-of-defun-function))))) (funcall skip) - (while (and (< arg 0) (>= (point) pos)) + (while (and (< arg 0) (>= (point) pos) success) ;; We intended to move backward, but this ended up not doing so: ;; Try harder! (goto-char beg) - (beginning-of-defun-raw (- arg)) - (if (>= (point) beg) + (setq success (beginning-of-defun-raw (- arg))) + ;; If we successfully moved pass point, or there is no further + ;; defun beginnings anymore, stop. + (if (or (>= (point) beg) (not success)) (setq arg 0) (setq beg (point)) (funcall end-of-defun-function) --Apple-Mail=_C2283A1A-0201-4FEF-8286-CB8B56311190--