Don't signal a backtrace on empty --script files

* lisp/startup.el (command-line--load-script): New function that
avoids erroring out if it turns out there's no forms in the buffer
(bug#4616).

* lisp/subr.el (delete-line): New utility function.
* lisp/international/mule.el (load-with-code-conversion): Accept
an eval function.
This commit is contained in:
Lars Ingebrigtsen 2022-02-10 13:44:55 +01:00
parent 15781beda8
commit 53da8c50fc
3 changed files with 41 additions and 10 deletions

View file

@ -298,13 +298,21 @@ attribute."
(defvar hack-read-symbol-shorthands-function nil
"Holds function to compute `read-symbol-shorthands'.")
(defun load-with-code-conversion (fullname file &optional noerror nomessage)
(defun load-with-code-conversion (fullname file &optional noerror nomessage
eval-function)
"Execute a file of Lisp code named FILE whose absolute name is FULLNAME.
The file contents are decoded before evaluation if necessary.
If optional third arg NOERROR is non-nil,
report no error if FILE doesn't exist.
Print messages at start and end of loading unless
optional fourth arg NOMESSAGE is non-nil.
If optional third arg NOERROR is non-nil, report no error if FILE
doesn't exist.
Print messages at start and end of loading unless optional fourth
arg NOMESSAGE is non-nil.
If EVAL-FUNCTION, call that instead of calling `eval-buffer'
directly. It is called with two paramameters: The buffer object
and the file name.
Return t if file exists."
(if (null (file-readable-p fullname))
(and (null noerror)
@ -353,10 +361,13 @@ Return t if file exists."
;; Have the original buffer current while we eval,
;; but consider shorthands of the eval'ed one.
(let ((read-symbol-shorthands shorthands))
(eval-buffer buffer nil
;; This is compatible with what `load' does.
(if dump-mode file fullname)
nil t)))
(if eval-function
(funcall eval-function buffer
(if dump-mode file fullname))
(eval-buffer buffer nil
;; This is compatible with what `load' does.
(if dump-mode file fullname)
nil t))))
(let (kill-buffer-hook kill-buffer-query-functions)
(kill-buffer buffer)))
(do-after-load-evaluation fullname)

View file

@ -2663,7 +2663,7 @@ nil default-directory" name)
;; actually exist on some systems.
(when (file-exists-p truename)
(setq file-ex truename))
(load file-ex nil t t)))
(command-line--load-script file-ex)))
((equal argi "-insert")
(setq inhibit-startup-screen t)
@ -2838,6 +2838,19 @@ nil default-directory" name)
(display-startup-screen (> displayable-buffers-len 0))))))
(defun command-line--load-script (file)
(load-with-code-conversion
file file nil nil
(lambda (buffer file)
(with-current-buffer buffer
(goto-char (point-min))
;; Removing the #! and then calling `eval-buffer' will make the
;; reader not signal an error if it then turns out that the
;; buffer is empty.
(when (looking-at "#!")
(delete-line))
(eval-buffer buffer nil file nil t)))))
(defun command-line-normalize-file-name (file)
"Collapse multiple slashes to one, to handle non-Emacs file names."
(save-match-data

View file

@ -6591,4 +6591,11 @@ OBJECT if it is readable."
(throw 'unreadable nil))))
(prin1-to-string object))))
(defun delete-line ()
"Delete the current line."
(delete-region (line-beginning-position)
(progn
(forward-line 1)
(point))))
;;; subr.el ends here