2

Right now, if I do "emacs foo1 foo2" emacs will open with foo1 and the welcome screen showing in two windows splitting the frame vertically. Instead, I want to be shown foo1 and foo2. However, if I just type "emacs" with no files, then I want to see the welcome screen. How do I accomplish this?

Ideally, I'm looking for a solution that somehow counts the number of buffers visiting a file, and uses that count to suppress the welcome screen.

1 Answer 1

3

EDIT in response to your comments

To count the file visiting buffers just before the 'show display function' executes (and show the screen conditionally), you can advise the display-startup-screen function as follows:

(defun startup-screen-advice (orig-fun &rest args)
  (when (= (seq-count #'buffer-file-name (buffer-list)) 0)
    (apply orig-fun args)))

(advice-add 'display-startup-screen :around #'startup-screen-advice)

Just add this code to your init file.

Of course, if you do not remove it subsequently, the advice stays there while Emacs is running, but I guess you will not be interested in displaying the startup screen at some later time).

END EDIT

The simple way is to inhibit showing the startup screen if any argument got passed:

(when (cdr command-line-args)
  (setq inhibit-startup-screen t))

If you'd really prefer to get this behavior only when you pass any file argument (or, with simple test below, any argument not starting with a dash), then you could use:

(when (seq-some (lambda (arg)
                  (not (string= (substring arg 0 1) "-")))
                (cdr command-line-args))
  (setq inhibit-startup-screen t))

I guess the code is self-explanatory enough (so I do not add any explanation here).

Obviously, you should add any of the above examples to your init file.

5
  • I am the OP. Not enough rep to upvote your answer, but it does solve the problem. However, being a purist, I'm wondering how it could be modified to check for whether any files are being visited, rather than just using the heuristic of checking for arguments without dashes. In other words, how many buffers are visiting a file? If greater than 0, hide the startup screen. I know that (buffer-list) will return a list of the buffers, but I didn't find any equivalent for files being visited, and am not sure how to map buffer-file-name to the output of (buffer-list).
    – Thomas
    Commented Jan 14, 2023 at 17:18
  • I am not sure what you mean/want to achieve exactly. I guess to count buffers, you could use (seq-count #'buffer-file-name (buffer-list)). But you could also count arguments that are existing files, which then, usually, will become visited files. However, both these methods will not work when you are visiting a 'newly created' file, as Emacs will not create the file immediately, and instead creates a buffer with that name, and then only write the file when you save that buffer. I think as long as it works, the pragmatic thing to do, in this case, is to suppress your purist cravings :) Commented Jan 14, 2023 at 20:38
  • If that is no option, then please modify/update your question (instead of specifying in a comment) and indicate how you'd prefer to handle this issue, so that we can come up with some alternative solution(s). Commented Jan 14, 2023 at 20:42
  • Calling (seq-count #'buffer-file-name (buffer-list)) in the init file doesn't seem to work. Even if existing file names are passed, they are not present as buffers yet. So, I guess I'll just be happy with the original solution. I suppose there must be some trigger you could attach to to run a function when the files are load? Too much work! :-)
    – Thomas
    Commented Jan 15, 2023 at 7:25
  • Ah right, I did not test that one. Well, then indeed you could advise the display-startup-screen function to only execute when that seq-count is nil. I will update the answer, as code does not get (well) formatted in comments. Commented Jan 15, 2023 at 10:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.