]> git.eshelyaron.com Git - emacs.git/commitdiff
Don't signal a backtrace on empty --script files
authorLars Ingebrigtsen <larsi@gnus.org>
Thu, 10 Feb 2022 12:44:55 +0000 (13:44 +0100)
committerLars Ingebrigtsen <larsi@gnus.org>
Thu, 10 Feb 2022 12:44:55 +0000 (13:44 +0100)
* 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
lisp/startup.el
lisp/subr.el

index 0758359e1546993970919ca98d2c87f2059d4d8b..1596cdb48172f2bc1d22cd976118cb01f5c7760e 100644 (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)
index 9a4c3e2d144adb3f4b1b5ee6aa546770d0283855..8b059e756d51851a816101c7ecc818d17957215c 100644 (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
index 0b546c0e0ba8fe3bf80c4814b51d62a651a1797f..a78af09c40ee46be9c59a6aa17285c0d31c12eb6 100644 (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