bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#50646: 28.0.50; narrow-to-defun sometimes narrows to wrong defun


From: Arthur Miller
Subject: bug#50646: 28.0.50; narrow-to-defun sometimes narrows to wrong defun
Date: Sat, 18 Sep 2021 19:27:22 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Lars Ingebrigtsen <larsi@gnus.org> 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.
                          "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)*\\sw\\)" nil t)
                    (let ((sym (intern-soft (match-string 1))))
                      (if (fboundp sym)
                          (help-xref-button 1 'help-function sym)))))
                ;; Look for commands in whole keymap substitutions:
                (save-excursion
                  ;; Make sure to find the first keymap.
                  (goto-char (point-min))
                  ;; Find a header and the column at which the command
                  ;; name will be found.

                  ;; If the keymap substitution isn't the last thing in
                  ;; the doc string, and if there is anything on the same
                  ;; line after it, this code won't recognize the end of it.
                  (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
                                            nil t)
                    (let ((col (- (match-end 1) (match-beginning 1))))
                      (while
                          (and (not (eobp))
                               ;; Stop at a pair of blank lines.
                               (not (looking-at-p "\n\\s-*\n")))
                        ;; Skip a single blank line.
                        (and (eolp) (forward-line))
                        (end-of-line)
                        (skip-chars-backward "^ \t\n")
                        (if (and (>= (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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]