Subject: Re: bug#77746: [PATCH] sh-mode: Fix incorrect word syntax for punctuation in sh-mode

From: James Cherti <[email-masked]>
Date: Mon, 14 Apr 2025 09:25:03 -0400

On 2025-04-13 13:33, Stefan Monnier wrote:
>>> In sh and Bash, the characters !%^~:.,= and are not valid in variable or
>>> function names.
> 
> I'm not positive about all of them, but at least some of those can
> appear in the names of commands.
> 
>>> Assigning them the "_" syntax causes Emacs to treat them as word
>>> constituents,
> 
> Not quite: it makes them appear as "symbol constituents".
> 
>>> disrupting navigation and completion (e.g. dabbrev-expand,
>>> forward-word, etc.).
> 
> `forward-word` for example shouldn't be affected (unless you enable
> `superword-mode`).  `dabbrev-expand` OTOH is affected, indeed.
> 
>>> The attached patch updates the syntax table in sh-mode to mark
>>> these characters as punctuation, correcting the issue.
>> TBH, such a change sounds scary, as it could cause all kinds of
>> unintended changes in behavior.
> 
> It's indeed risky/delicate.  The syntax-tables are a fairly crude tool,
> so we often need to use different tables at different places.
> 
> Rather than go straight to changing the syntax-table, I suggest you
> start by providing some concrete examples of behaviors you consider
> incorrect with the current code.  Maybe changing the main syntax-table
> of that mode will be the better option, but if so, it'll probably
> require changing other code to keep using the current
> syntax-table there.
> [ I haven't tested it, but I'd expect trouble with your patch either in
>    font-lock or indentation if you have commands with names like
>    `if-config`.  ]
> 
> 
>          Stefan
> 

I primarily wrote this patch to prevent Emacs from including
the characters !%^~:., in symbols when highlighting a
symbol at point or completing a variable name.

Actually, the ones I specifically need in my case are: .:,
I often include variable names in comments followed by these
characters.

This patch addresses two issues I encountered related to
symbols:

1. Completion
--------------
Given the variable name `varname` in a comment,
followed by ".":
   #!/usr/bin/env bash
   # The name of this variable is varname. Code:
   var

Completing "var" includes "varname." in the list of
completions.

(e.g. dabbrev completion. I am using Corfu/Cape,
which displays all suggestions)

2. Highlight symbol at point
----------------------------
When the cursor is on the comment `varname.`,
Emacs highlights `varname.` instead of `varname` when
using `(hi-lock-face-symbol-at-point)`:
   #!/usr/bin/env bash
   # The name of this variable is varname. Code:
   var

Please feel free to share suggestions that could help resolve such issues.

--
James Cherti
GitHub: https://github.com/jamescherti
Website: https://www.jamescherti.com/