From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Arthur Miller Newsgroups: gmane.emacs.bugs Subject: bug#50646: 28.0.50; narrow-to-defun sometimes narrows to wrong defun Date: Sat, 18 Sep 2021 19:27:22 +0200 Message-ID: References: <87lf3urn5v.fsf@gnus.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="31603"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: 50646@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Sep 18 19:28:20 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1mRe8S-0007zW-5q for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Sep 2021 19:28:20 +0200 Original-Received: from localhost ([::1]:60160 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mRe8Q-0004pN-8r for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 18 Sep 2021 13:28:18 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:46178) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mRe8A-0004p6-RV for bug-gnu-emacs@gnu.org; Sat, 18 Sep 2021 13:28:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:53475) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mRe8A-0000Rh-KK for bug-gnu-emacs@gnu.org; Sat, 18 Sep 2021 13:28:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1mRe8A-00007Z-Gt for bug-gnu-emacs@gnu.org; Sat, 18 Sep 2021 13:28:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Arthur Miller Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 18 Sep 2021 17:28:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 50646 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: moreinfo Original-Received: via spool by 50646-submit@debbugs.gnu.org id=B50646.1631986058432 (code B ref 50646); Sat, 18 Sep 2021 17:28:02 +0000 Original-Received: (at 50646) by debbugs.gnu.org; 18 Sep 2021 17:27:38 +0000 Original-Received: from localhost ([127.0.0.1]:36788 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mRe7l-00006u-8R for submit@debbugs.gnu.org; Sat, 18 Sep 2021 13:27:38 -0400 Original-Received: from mail-oln040092073026.outbound.protection.outlook.com ([40.92.73.26]:20302 helo=EUR04-HE1-obe.outbound.protection.outlook.com) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1mRe7f-00006c-H3 for 50646@debbugs.gnu.org; Sat, 18 Sep 2021 13:27:35 -0400 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=AlR0H9PiUFhZn/2eG50TB0Y6CWRhlywEuzeyUxxo+e2ru0Lkw3hbnaM1wFyoH9GpFaV0KnAecjoLa7jQsQYDgkr1i9mPJHvhLoHfBEp942g6pmbQfYNqTqtlyKAqilI8AJc2ME04cJPMKjenEtDELbpEXJoM99cTbAhh0eFj+Mtc82KmuXzpVOa0s3GrEP11l6DKMov7RfZ18wcOzWwut1xt3yNTMTDA+OFv/6NRqsz99PubpwB6UNTZu6XN/bC4wQ8RVVnXkEjXOPmkPTE2tzgf3hzbAONaPNaZiYI2FZ3fV7mglZAhhZYN4WUK8puW5bRQ/suRiKF0FA5PyerhKw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=8M20+b7ErMLiKGuogxGmXHcPZTs71Tr21bDVeP6Xtq0=; b=cjg1JrOsXVrXG2VCN56cWg17/EhfPZi8F888ARaBPoSbzT5jKH/7nISwThIxbgwh4AsjxpYBg/dnIRXlnZUb9M/jWz735hXnsTYjCw2by9CAc1P5wQikMP31FjHY98w3Evzx3tiUnog1T2IFxNZ0/zQO3SseiYJYgqq8bWhpwGfAvE5+puNwJfNiDZg7iMMXP2Zg3rYL6xFJWku0P7kmpSNQ0RVjEC96cXeVA120LzAkXqGfdJEXLmX4HAmE++a3K0ZEwfvrLY7GtRzkXqhxsilxTZd3h77+b9Bw49V7FcrYcn8N3mS3tLmfKNAytl8r08J8yIaH/aLXEM40ASzLZw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=none; dmarc=none; dkim=none; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=live.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=8M20+b7ErMLiKGuogxGmXHcPZTs71Tr21bDVeP6Xtq0=; b=TKSKurPAklIzefhyV1UHPOFjMyQFQ5mNS/Yxv3EjoChYF1dAsCmEmkGnjju3rn4lp0nBmxV3NjqnWqd9GdVN12iRrheWpJvEcg4/AE0ORsKuQ/vwjM//80BV0ofaU+lQqMNGFhEoROU9qVMFwKROYZA0+vYH5GL4aH1T7TCEm4iS5VEHax4FiRd4Nm5TXkBU3nLAQ06QbRyek7f7BZcVNceN4ZyCqPfQKnaTyy3Z4/xcRkkUlqvmLa2nbNvG8yNOEfd3V7pk2vlqa3lCysUnGMV4i/ZhomojoEsWBVTo3SkDiuf8vvv4UFEMhS9xd2gwcq06f/jJ9Abc5Ovz/gYmlA== Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) by AM0PR09MB2594.eurprd09.prod.outlook.com (2603:10a6:208:dd::12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.17; Sat, 18 Sep 2021 17:27:23 +0000 Original-Received: from AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::c55c:ece5:bed2:a9dc]) by AM9PR09MB4977.eurprd09.prod.outlook.com ([fe80::c55c:ece5:bed2:a9dc%9]) with mapi id 15.20.4523.018; Sat, 18 Sep 2021 17:27:23 +0000 In-Reply-To: <87lf3urn5v.fsf@gnus.org> (Lars Ingebrigtsen's message of "Sat, 18 Sep 2021 16:09:00 +0200") X-TMN: [C/Qs2WAipvVSR2feQMV3zqogCDqTAumz] X-ClientProxiedBy: AM6P192CA0024.EURP192.PROD.OUTLOOK.COM (2603:10a6:209:83::37) To AM9PR09MB4977.eurprd09.prod.outlook.com (2603:10a6:20b:304::20) X-Microsoft-Original-Message-ID: <878rzt4ww5.fsf@live.com> X-MS-Exchange-MessageSentRepresentingType: 1 Original-Received: from pascal.homepc (81.232.177.30) by AM6P192CA0024.EURP192.PROD.OUTLOOK.COM (2603:10a6:209:83::37) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.4523.16 via Frontend Transport; Sat, 18 Sep 2021 17:27:23 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id: 666c983a-90dc-471b-79d0-08d97ac9939c X-MS-TrafficTypeDiagnostic: AM0PR09MB2594: X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: aqbhgdNBb0RkMYeiIOC2TKNI0LYoZZr2DunDUcBNA4W1DgwVVfcNWQYHX11MNH2uSPCUQaVliMuaFHCA1kkdeqv22idoSR7El9Am/dSoxLIGgJ/zwec/1Ejmk/9rdhm5LGrpVHC7c5O+Eu/jSTXNg6K6KMYXZ4SRTwzfMVMcEpp18hRyJVla5J8l+B1L+FbFRLbxPgtPyJct3JAN8e+QkLCQooWxykTELUE+wRCNHiPAKxTzndwTVsWrnwKYsnve7MvugNu9lTVuJmotWulo8NAubqP+0ZAfrbclmbh3hYSj6onPfHtr6wU794Uesr81PGgRe5Ffd0x531FauJROIzN5S+ElNQbBt5sENt+baS+e50IVSxH8fW2HpLl2eBlvyjLSFglcsGPucaWKGBSih6KFBt5am0RrYpOnqKbSg//feldu3m/Y39LXUIwPRxuT X-MS-Exchange-AntiSpam-MessageData-ChunkCount: 1 X-MS-Exchange-AntiSpam-MessageData-0: TWBLeokfavhywMHqh/1attr8tRHqCjBf7MGNRkW8n0DySzJaLYTZ6R0nkQm031iP7WYOAHnNMfEPNkt6lOyFhYwCtjiF5cI7zprYmc25Z5p85N/u6/7dTVtnpSi2E4z34e7PPEKBhDJAh9/f7AhXnA== X-OriginatorOrg: sct-15-20-3174-20-msonline-outlook-72e6e.templateTenant X-MS-Exchange-CrossTenant-Network-Message-Id: 666c983a-90dc-471b-79d0-08d97ac9939c X-MS-Exchange-CrossTenant-AuthSource: AM9PR09MB4977.eurprd09.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 18 Sep 2021 17:27:23.7786 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 84df9e7f-e9f6-40af-b435-aaaaaaaaaaaa X-MS-Exchange-CrossTenant-RMS-PersistedConsumerOrg: 00000000-0000-0000-0000-000000000000 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM0PR09MB2594 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:214648 Archived-At: Lars Ingebrigtsen writes: > arthur.miller@live.com writes: > >> To reproduce this bug: >> >> 1. run Emacs -Q -l /path/to/attached/help-mode.el >> 2. type C-x f >> 3. in minibuffer type: when RET >> >> The help-mode buffer that opens should show help for 'when' form, but it >> shows the source code for the 'pop' macro which precedes the 'when' in >> subr.el. > > I didn't try to reproduce this, because the attached .el file is almost > 1K lines long. Ok, didn't know it sloc size mattered. It is just patched help-mode.el from Emacs source. > Do you have a simpler way to reproduce the issue you're seeing? No I don't have any other way. You can try to eval the src region below; that's the patched part of help-mode.el and follow step 2 and 3. #+begin_src emacs-lisp (defcustom help-mode-inline-source t "Display inlined source code for SYMBOL in `help-mode' buffer. When enabled the source code of a symbol will be displayed inlined in the help buffer, if the source code for the symbol is available." :type 'boolean :group 'help) (defun help--function-source (fun file &optional type) "Fnd and return string to be inserted in help-mode buffer for the source code of the symbol. Used internally for `help-make-refs'." (let ((src "Source code not available.") (mode (if (eq file 'C-source) 'c-mode 'emacs-lisp-mode))) (if (eq mode 'c-mode) (setq file (help-C-file-name (indirect-function fun) 'fun)) (setq file (or file (find-lisp-object-file-name fun type)))) (when file (require 'find-func) ;; Don't use find-function-noselect because it follows ;; aliases (which fails for built-in functions). (with-temp-buffer (insert-file-contents-literally (expand-file-name file source-directory)) (let* ((location (find-function-search-for-symbol fun type file)) (position (cdr location)) (mode (if (equal (file-name-sans-extension file) ".c") 'c-mode 'emacs-lisp-mode))) (if position (progn (run-hooks 'find-function-after-hook) ;; Widen the buffer if necessary to go to this position. (when (or (< position (point-min)) (> position (point-max))) (widen)) (goto-char position) (message "pos: %s word: %s" position (current-word)) ;; narrow-to-defun sometimes return defun preceding the ;; point instead of one following the point as ;; advertised in docs. Forward-char didn't fix it, but ;; forward word seems to work. ;; (forward-char) ;; (forward-word) (narrow-to-defun t) (delay-mode-hooks (funcall mode)) (if (fboundp 'font-lock-ensure) (font-lock-ensure) (with-no-warnings (font-lock-fontify-buffer))) (setq src (buffer-string))))))) src)) (defun help-setup-xref (item interactive-p) "Invoked from commands using the \"*Help*\" buffer to install some xref info. ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help buffer after following a reference. INTERACTIVE-P is non-nil if the calling command was invoked interactively. In this case the stack of items for help buffer \"back\" buttons is cleared. This should be called very early, before the output buffer is cleared, because we want to record the \"previous\" position of point so we can restore it properly when going back." (with-current-buffer (help-buffer) (when help-xref-stack-item (push (cons (point) help-xref-stack-item) help-xref-stack) (setq help-xref-forward-stack nil)) (when interactive-p (let ((tail (nthcdr 10 help-xref-stack))) ;; Truncate the stack. (if tail (setcdr tail nil)))) (setq help-xref-stack-item item))) (defvar help-xref-following nil "Non-nil when following a help cross-reference.") ;;;###autoload (defun help-buffer () "Return the name of a buffer for inserting help. If `help-xref-following' is non-nil, this is the name of the current buffer. Signal an error if this buffer is not derived from `help-mode'. Otherwise, return \"*Help*\", creating a buffer with that name if it does not already exist." (buffer-name ;for with-output-to-temp-buffer (if (not help-xref-following) (get-buffer-create "*Help*") (unless (derived-mode-p 'help-mode) (error "Current buffer is not in Help mode")) (current-buffer)))) (defvar describe-symbol-backends `((nil ,#'fboundp ,(lambda (s _b _f) (describe-function s))) (nil ,(lambda (symbol) (or (and (boundp symbol) (not (keywordp symbol))) (get symbol 'variable-documentation))) ,#'describe-variable) ("face" ,#'facep ,(lambda (s _b _f) (describe-face s)))) "List of providers of information about symbols. Each element has the form (NAME TESTFUN DESCFUN) where: NAME is a string naming a category of object, such as \"type\" or \"face\". TESTFUN is a predicate which takes a symbol and returns non-nil if the symbol is such an object. DESCFUN is a function which takes three arguments (a symbol, a buffer, and a frame), inserts the description of that symbol in the current buffer and returns that text as well.") (defun help--function-source (fun file &optional type) "Fnd and return string to be inserted in help-mode buffer for the source code of the symbol. Used internally for `help-make-refs'." (let ((src "Source code not available.") (mode (if (eq file 'C-source) 'c-mode 'emacs-lisp-mode))) (if (eq mode 'c-mode) (setq file (help-C-file-name (indirect-function fun) 'fun)) (setq file (or file (find-lisp-object-file-name fun type)))) (when file (require 'find-func) ;; Don't use find-function-noselect because it follows ;; aliases (which fails for built-in functions). (with-temp-buffer (insert-file-contents-literally (expand-file-name file source-directory)) (let* ((location (find-function-search-for-symbol fun type file)) (position (cdr location)) (mode (if (equal (file-name-sans-extension file) ".c") 'c-mode 'emacs-lisp-mode))) (if position (progn (run-hooks 'find-function-after-hook) ;; Widen the buffer if necessary to go to this position. (when (or (< position (point-min)) (> position (point-max))) (widen)) (goto-char position) (message "pos: %s word: %s" position (current-word)) ;; narrow-to-defun sometimes return defun preceding the ;; point instead of one following the point as ;; advertised in docs. Forward-char didn't fix it, but ;; forward word seems to work. ;; (forward-char) ;; (forward-word) (narrow-to-defun t) (delay-mode-hooks (funcall mode)) (if (fboundp 'font-lock-ensure) (font-lock-ensure) (with-no-warnings (font-lock-fontify-buffer))) (setq src (buffer-string))))))) src)) ;;;###autoload (defun help-make-xrefs (&optional buffer) "Parse and hyperlink documentation cross-references in the given BUFFER. Find cross-reference information in a buffer and activate such cross references for selection with `help-follow-symbol'. Cross-references have the canonical form `...' and the type of reference may be disambiguated by the preceding word(s) used in `help-xref-symbol-regexp'. Faces only get cross-referenced if preceded or followed by the word `face'. Variables without variable documentation do not get cross-referenced, unless preceded by the word `variable' or `option'. If the variable `help-xref-mule-regexp' is non-nil, find also cross-reference information related to multilingual environment \(e.g., coding-systems). This variable is also used to disambiguate the type of reference as the same way as `help-xref-symbol-regexp'. A special reference `back' is made to return back through a stack of help buffers. Variable `help-back-label' specifies the text for that." (interactive "b") (with-current-buffer (or buffer (current-buffer)) (save-excursion (goto-char (point-min)) ;; Skip the first bit, which has already been buttonized. (forward-paragraph) (let ((old-modified (buffer-modified-p))) (let ((stab (syntax-table)) (case-fold-search t) (inhibit-read-only t)) (set-syntax-table help-mode-syntax-table) ;; The following should probably be abstracted out. (unwind-protect (progn ;; Info references (save-excursion (while (re-search-forward help-xref-info-regexp nil t) (let ((data (match-string 2))) (save-match-data (unless (string-match "^([^)]+)" data) (setq data (concat "(emacs)" data))) (setq data ;; possible newlines if para filled (replace-regexp-in-string "[ \t\n]+" " " data t t))) (help-xref-button 2 'help-info data)))) ;; Customization groups. (save-excursion (while (re-search-forward help-xref-customization-group-regexp nil t) (help-xref-button 1 'help-customization-group (intern (match-string 1))))) ;; URLs (save-excursion (while (re-search-forward help-xref-url-regexp nil t) (let ((data (match-string 1))) (help-xref-button 1 'help-url data)))) ;; Mule related keywords. Do this before trying ;; `help-xref-symbol-regexp' because some of Mule ;; keywords have variable or function definitions. (if help-xref-mule-regexp (save-excursion (while (re-search-forward help-xref-mule-regexp nil t) (let* ((data (match-string 7)) (sym (intern-soft data))) (cond ((match-string 3) ; coding system (and sym (coding-system-p sym) (help-xref-button 6 'help-coding-system sym))) ((match-string 4) ; input method (and (assoc data input-method-alist) (help-xref-button 7 'help-input-method data))) ((or (match-string 5) (match-string 6)) ; charset (and sym (charsetp sym) (help-xref-button 7 'help-character-set sym))) ((assoc data input-method-alist) (help-xref-button 7 'help-input-method data)) ((and sym (coding-system-p sym)) (help-xref-button 7 'help-coding-system sym)) ((and sym (charsetp sym)) (help-xref-button 7 'help-character-set sym))))))) ;; Quoted symbols (save-excursion (while (re-search-forward help-xref-symbol-regexp nil t) (let* ((data (match-string 8)) (sym (intern-soft data))) (if sym (cond ((match-string 3) ; `variable' &c (and (or (boundp sym) ; `variable' doesn't ensure ; it's actually bound (get sym 'variable-documentation)) (help-xref-button 8 'help-variable sym))) ((match-string 4) ; `function' &c (and (fboundp sym) ; similarly (help-xref-button 8 'help-function sym))) ((match-string 5) ; `face' (and (facep sym) (help-xref-button 8 'help-face sym))) ((match-string 6)) ; nothing for `symbol' ((match-string 7) (help-xref-button 8 'help-function-def sym)) ((cl-some (lambda (x) (funcall (nth 1 x) sym)) describe-symbol-backends) (help-xref-button 8 'help-symbol sym))))))) ;; An obvious case of a key substitution: (save-excursion (while (re-search-forward ;; Assume command name is only word and symbol ;; characters to get things like `use M-x foo->bar'. ;; Command required to end with word constituent ;; to avoid `.' at end of a sentence. "\\= (current-column) col) (looking-at "\\(\\sw\\|\\s_\\)+$")) (let ((sym (intern-soft (match-string 0)))) (if (fboundp sym) (help-xref-button 0 'help-function sym)))) (forward-line))))) (set-syntax-table stab))) ;; Delete extraneous newlines at the end of the docstring (goto-char (point-max)) (while (and (not (bobp)) (bolp)) (delete-char -1)) (insert "\n") ;; get source string if needed and available (when help-mode-inline-source (insert "\nSource Code: \n") (let ((file (plist-get help-mode--current-data :file)) (fun (plist-get help-mode--current-data :symbol))) (insert (help--function-source fun file))) (insert "\n")) (when (or help-xref-stack help-xref-forward-stack) (insert "\n")) ;; Make a back-reference in this buffer if appropriate. (when help-xref-stack (help-insert-xref-button help-back-label 'help-back (current-buffer))) ;; Make a forward-reference in this buffer if appropriate. (when help-xref-forward-stack (when help-xref-stack (insert "\t")) (help-insert-xref-button help-forward-label 'help-forward (current-buffer))) (when (or help-xref-stack help-xref-forward-stack) (insert "\n"))) (set-buffer-modified-p old-modified))))) #+end_src