Emacs 24.x and 25.x have a bug in CC Mode which can result in some operations failing with the error
Invalid search bound (wrong side of point)
For example, I can reproduce it with the following file
#if defined(FOO) || \ defined(BAR)
emacs -Q -nw a.c --eval '(occur "FOO")'
M-x occur RET FOO RET after that works. I have a bigger file where this doesn't work.)
This is a known bug: #28850. The fix is in two commits 46540a1c7adb1b89b6c2f6c9150fe8680c3a5fba and ad68bbd0da4ed90117f09dc2344c0c3d9d728851 which were first released in Emacs 26.1. I can confirm that Emacs 26.3 works with both the minimal example above and my real file.
The machines I use most often run Emacs 24.5.1 or Emacs 25.2.2, so I need a workaround for Emacs 24.x and 25.x. My init file has several hot fixes for Emacs bug and I'm fine with adding one more. I normally hot-fix in an
eval-after-load form, either by advising a function or by replacing its definition.
But here there's a bug in the function
c-make-font-lock-search-form which, if I understand correctly, is used at compile time. This is the relevant part of
(eval-and-compile … (defun c-make-font-lock-search-form (regexp highlights) ;; this code needs to be patched …) (defun c-make-font-lock-context-search-function (normal &rest state-stanzas) (c-make-font-lock-search-form …)) ;; this call needs to be patched ) (c-lang-defconst c-cpp-matchers t `( … ,(c-make-font-lock-context-search-function …)))
cc-fonts.elc contains buggy byte-compiled code. Merely fixing the buggy function after
cc-fonts.elc is loaded won't help. Even fixing the function before
cc-fonts.elc wouldn't help.
How can I work around this bug? The workaround needs to work on Emacs 24.x and 25.x. The workaround needs to fix the "Invalid search bound" error, without disabling fontification. I don't mind if there's a slight loss of performance or accuracy compared to the official fix.