Subject: Re: insert-file-contents can corrupt buffers. [Was: bug#50946: Emacs-28: Inadequate coding in hack-elisp-shorthands]

From: João Távora <[email-masked]>
Date: Sun, 03 Oct 2021 16:34:19 +0100

Eli Zaretskii <[email-masked]> writes:

> João, why didn't you simply insert
>
>   (alist-get 'elisp-shorthands (hack-local-variables--find-variables))
>
> in load-with-code-conversion, immediately after it calls
> insert-file-contents?  Are there any problems with that, and if so,
> what are they?

There shouldn't be any big problems.  As I said, I think that is
cleaner.  However, doing it "from the outside" is safer (except for
these insert-file-contents bugs/edge cases, which frankly escape me).

Your suggestion has a very minor problem, that you're doing this stuff
in lisp/international/mule.el, which slightly icky.

A bigger problem is that hack-local-variables--find-variables isn't
defined at that point and the function will then be used to load
lisp/files.el itself (which happens to be where h-l-v--f-v is defined).

So either we change file loading order -- a bit scary -- or we setup
some kind of indirection (a hook, a variable).  I tried with a hook but
it doesn't work: it breaks a shorthands test, a most basic one.  Maybe
you can understand where the problem is?  It takes me a while to debug
cause I take this stuff in loadup.el to always need a 'make bootstrap'
after any changes.

I attach the patch I used.

(BTW this is after the agreed renaming to read-symbol-shorthands, which
I just pushed)

João

diff --git a/lisp/international/mule.el b/lisp/international/mule.el
index 2a855b5673..be6d397f79 100644
--- a/lisp/international/mule.el
+++ b/lisp/international/mule.el
@@ -294,6 +294,9 @@ define-charset
 
     (apply 'define-charset-internal name (mapcar 'cdr attrs))))
 
+(defvar load-with-code-conversion-hook nil
+  "Hook run in `load-with-code-conversion'.")
+
 (defun load-with-code-conversion (fullname file &optional noerror nomessage)
   "Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
 The file contents are decoded before evaluation if necessary.
@@ -328,6 +331,7 @@ load-with-code-conversion
 	      ;; Don't let deactivate-mark remain set.
 	      (let (deactivate-mark)
 		(insert-file-contents fullname))
+              (run-hooks 'load-with-code-conversion-hook)
 	      ;; If the loaded file was inserted with no-conversion or
 	      ;; raw-text coding system, make the buffer unibyte.
 	      ;; Otherwise, eval-buffer might try to interpret random
diff --git a/lisp/loadup.el b/lisp/loadup.el
index 3fb6b81328..3a55d2c805 100644
--- a/lisp/loadup.el
+++ b/lisp/loadup.el
@@ -355,7 +355,6 @@
 (load "paren")
 
 (load "shorthands")
-(setq load-source-file-function #'load-with-shorthands-and-code-conversion)
 
 (load "emacs-lisp/eldoc")
 (load "cus-start") ;Late to reduce customize-rogue (needs loaddefs.el anyway)
diff --git a/lisp/shorthands.el b/lisp/shorthands.el
index c31ef3d216..ecf04ac587 100644
--- a/lisp/shorthands.el
+++ b/lisp/shorthands.el
@@ -28,35 +28,17 @@
 (require 'files)
 (eval-when-compile (require 'cl-lib))
 
-(defun hack-read-symbol-shorthands (fullname)
-  "Return value of `read-symbol-shorthands' file-local variable in FULLNAME.
-FULLNAME is the absolute file name of an Elisp .el file which
-potentially specifies a file-local value for
-`read-symbol-shorthands'.  The Elisp code in FULLNAME isn't read
-or evaluated in any way, except for extraction of the
-buffer-local value of `read-symbol-shorthands'."
-  (let* ((size (nth 7 (file-attributes fullname)))
-         (from (max 0 (- size 3000)))
-         (to size))
-    (with-temp-buffer
-      (while (and (< (buffer-size) 3000) (>= from 0))
-        (insert-file-contents fullname nil from to)
-        (setq to from
-              from (cond
-                    ((= from 0) -1)
-                    (t (max 0 (- from 100))))))
-      ;; FIXME: relies on the `hack-local-variables--find-variables'
-      ;; detail of files.el.  That function should be exported,
-      ;; possibly be refactored into two parts, since we're only
-      ;; interested in basic "Local Variables" parsing.
-      (alist-get 'read-symbol-shorthands (hack-local-variables--find-variables)))))
-
-(defun load-with-shorthands-and-code-conversion (fullname file noerror nomessage)
-  "Like `load-with-code-conversion', but also consider Elisp shorthands.
-This function uses shorthands defined in the file FULLNAME's local
-value of `read-symbol-shorthands', when it processes that file's Elisp code."
-  (let ((read-symbol-shorthands (hack-read-symbol-shorthands fullname)))
-    (load-with-code-conversion fullname file noerror nomessage)))
+(add-hook 'load-with-code-conversion-hook #'hack-read-symbol-shorthands)
+
+(defun hack-read-symbol-shorthands ()
+  "Set `read-symbol-shorthands' from Local Variables section."
+  ;; FIXME: relies on the `hack-local-variables--find-variables'
+  ;; detail of files.el.  That function should be exported,
+  ;; possibly be refactored into two parts, since we're only
+  ;; interested in basic "Local Variables" parsing.
+  (setq-local read-symbol-shorthands
+              (alist-get 'read-symbol-shorthands
+                         (hack-local-variables--find-variables))))
 
 
 ;; FIXME: move this all to progmodes/elisp-mode.el?  OTOH it'd make