From 53da8c50fca98b5a7d0418f6030181df50af8876 Mon Sep 17 00:00:00 2001 From: Lars Ingebrigtsen Date: Thu, 10 Feb 2022 13:44:55 +0100 Subject: [PATCH] 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. --- lisp/international/mule.el | 29 ++++++++++++++++++++--------- lisp/startup.el | 15 ++++++++++++++- lisp/subr.el | 7 +++++++ 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/lisp/international/mule.el b/lisp/international/mule.el index 0758359e154..1596cdb4817 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el @@ -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) diff --git a/lisp/startup.el b/lisp/startup.el index 9a4c3e2d144..8b059e756d5 100644 --- a/lisp/startup.el +++ b/lisp/startup.el @@ -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 diff --git a/lisp/subr.el b/lisp/subr.el index 0b546c0e0ba..a78af09c40e 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -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 -- 2.39.5