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

From: [email-masked]
Date: Fri, 17 Sep 2021 19:06:04 +0200

Bug No 1:

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.

You can swtich to *Messages* buffer, I have output the position and
current-word at that position. Position is correct, it is the position
before opening parenthesis for '(defmacro when ...'. But current-word
shows 'x', which seems to be somethign from the pop macro that precedes
the 'when'. You can also clicn on "subr.el' in help-mode buffer and open
the source code, and then M-: (point) RET to confirm that the position
is correct.

The docs for 'narrow-to-defun' says the "current-defun" is one that
contains the point or follows the point. However, I am getting back the
one that precedes the point. Maybe it is bug elsewhere since
current-word also shows 'x', which I think is wrong, but maybe I am
wrong :).

I am not sure what is going on here, but there seems to be a bug
somewhere, in some rare case, and I think 'narrow-to-defun' is a good
place to start. I haven't found any other symbol but 'when' that results
in wrong form  narrowed, but maybe there is one.

Forward-char does not fix it, but forward-word seems to go far enough
into the enclosing 'when' form so that narrow-to-defun return correct
code.

I have another consideration here:

this is probably the same problem, but I am not sure. To reproduce,
follow step 1. and 2. from the prevous.

For the 3. type directory-files RET.

This should insert source code for directory-files from the dired.c. And
it does, but with a slight twist: it return just the body, not the
function declaration and the docs. I don't think it's a big deal, since
both are returned anyway, and I think I can fix it to return everything,
but as a note how it works. I am not sure if it's meant to work so or
not on c-sources. Notice also that the link to open the source file says
"C source code" not the name of the file, as it says for .el files. The
header is produced by 'help-function-def--button-function' which I have
adapted in 'help--function-source' (above help-make-xref) to return the
source code. I can send a patch to return the name of the file instead
of the string "C source code".

In GNU Emacs 28.0.50 (build 2, x86_64-pc-linux-gnu, cairo version 1.17.4)
 of 2021-09-15 built on pascal
Repository revision: b189b6f2564f903cf271a46910ad7a5df9da6918
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12013000
System Description: Arch Linux

Configured using:
 'configure --with-modules --with-cairo --without-compress-install
 --with-x-toolkit=no --with-gnutls --without-gconf --without-xwidgets
 --without-toolkit-scroll-bars --without-xaw3d --without-gsettings
 --with-mailutils --with-native-compilation 'CFLAGS=-O2 -march=native
 -mtune=native''

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM HARFBUZZ JPEG JSON LCMS2
LIBOTF LIBSYSTEMD LIBXML2 M17N_FLT MODULES NATIVE_COMP NOTIFY INOTIFY
OLDXMENU PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF X11 XDBE XIM XPM
ZLIB

Important settings:
  value of $LANG: sv_SE.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug comp comp-cstr warnings rx cl-extra
message rmc puny dired dired-loaddefs rfc822 mml mml-sec epa epg rfc6068
epg-config gnus-util rmail rmail-loaddefs auth-source cl-seq eieio
eieio-core cl-macs eieio-loaddefs password-cache json map
text-property-search time-date subr-x seq byte-opt gv bytecomp
byte-compile cconv mm-decode mm-bodies mm-encode mail-parse rfc2231
mailabbrev gmm-utils mailheader sendmail rfc2047 rfc2045 ietf-drums
mm-util mail-prsvr mail-utils help-mode derived cl-loaddefs cl-lib
iso-transl tooltip eldoc electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel term/x-win x-win term/common-win x-dnd tool-bar
dnd fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting font-render-setting cairo x multi-tty
make-network-process native-compile emacs)

Memory information:
((conses 16 90686 9107)
 (symbols 48 7862 1)
 (strings 32 21940 1868)
 (string-bytes 1 742230)
 (vectors 16 16860)
 (vector-slots 8 320588 16808)
 (floats 8 31 37)
 (intervals 56 240 0)
 (buffers 992 11))