Subject: bug#68593: 29.1; comment-dwim: Args out of range: "", 0, 1

From: Frank Ruben <[email-masked]>
Date: Fri, 19 Jan 2024 22:16:04 +0100

In GNU Emacs 29.1 (build 2, x86_64-w64-mingw32) of 2023-08-02 built on
AVALON
Windowing system distributor 'Microsoft Corp.', version 10.0.19045
System Description: Microsoft Windows 10 Pro (v10.0.2009.19045.3930)

Configured using:
 'configure --with-modules --without-dbus --with-native-compilation=aot
 --without-compress-install --with-tree-sitter CFLAGS=-O2'

Configured features:
ACL GIF GMP GNUTLS HARFBUZZ JPEG JSON LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY
W32NOTIFY PDUMPER PNG RSVG SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS
TREE_SITTER WEBP XPM ZLIB

(NATIVE_COMP present but libgccjit not available)

Important settings:
  value of $LANG: ENU
  locale-coding-system: utf-8

Major mode: C

Minor modes in effect:
  save-place-mode: t
  recentf-mode: t
  which-function-mode: t
  desktop-save-mode: t
  key-chord-mode: t
  override-global-mode: t
  global-auto-revert-mode: t
  global-so-long-mode: t
  flyspell-mode: t
  icomplete-mode: t
  fido-mode: t
  whitespace-mode: t
  electric-pair-mode: t
  savehist-mode: t
  display-time-mode: t
  shell-dirtrack-mode: t
  auto-insert-mode: t
  delete-selection-mode: t
  cua-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  mouse-wheel-mode: t
  prettify-symbols-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  size-indication-mode: t
  column-number-mode: 1
  line-number-mode: 1
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  auto-save-visited-mode: t
  abbrev-mode: t

Load-path shadows:
c:/usr/home/.emacs.d2/src/prolog hides
c:/usr/local/emacs/share/emacs/29.1/lisp/progmodes/prolog

Features:
(shadow sort mail-extr emacsbug message yank-media puny rfc822 mml mml-sec
...)

Memory information:
((conses 16 1328082 122098)
 (symbols 48 35374 0)
 (strings 32 187080 7259)
 (string-bytes 1 5454999)
 (vectors 16 81243)
 (vector-slots 8 1843247 151854)
 (floats 8 515 546)
 (intervals 56 162466 825)
 (buffers 984 88))

When in c-ts-mode.el and when having '(setq comment-end " ")' and when I
then
then run e.g. 'comment-dwim' or simpler '(comment-normalize-vars)', I'm
getting
'comment-dwim: Args out of range: "", 0, 1'.

The error comes from the two 'substring' calls in this s-sexp - where the
code
already contains my "fix" - both calls to 'substring' have been guarded by
'(if (string-equal ce "") "" ...)'.

There are surely smarter ways to fix that - if one knows what is supposed to
happen here :). But maybe that fix gives you a hint to the appropriate
solution.

(let ((ce (if (string= "" comment-end) "\n"
 (comment-string-strip comment-end t t))))
        (setq-local comment-end-skip
                    ;; We use [ \t] rather than \s- because we don't want to
                    ;; remove ^L in C mode when uncommenting.
                    (concat "[ \t]*\\(\\s>" (if comment-quote-nested "" "+")
                            "\\|" (regexp-quote (if (string-equal ce "") ""
(substring ce 0 1)))
                            (if (and comment-quote-nested (<= (length ce)
1)) "" "+")
                            (regexp-quote (if (string-equal ce "") ""
(substring ce 1)))
                            "\\)")))