Emacs: Get new-frame and emacsclient -c to use set frame size?
Asked Answered
P

1

8

I am new to StackOverflow and I have a question about an issue that has been virtually the only thing to irk me in my quest to master Emacs.

I configured my .emacs file to set the default frame size for Emacs to 70 rows and 80 columns like so:

(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 70))
(add-to-list 'default-frame-alist '(width . 80))

This works fine when starting Emacs, the problem is that when I launch a new frame using new-frame or emacsclient -c these settings are not respected. Is there a way to force emacsclient and new-frame to read the settings in the .emacs file when they are executed?

Edit:

Here is my .emacs file:

;;This setting is meant to force emacs to read size settings before make-frame.

(add-hook 'before-make-frame-hook
      #'(lambda ()
          (add-to-list 'default-frame-alist '(left   . 0))
          (add-to-list 'default-frame-alist '(top    . 0))
          (add-to-list 'default-frame-alist '(height . 70))
          (add-to-list 'default-frame-alist '(width  . 80))))

;;disable annoying welcome screen.
(setq inhibit-splash-screen t)
(setq inferior-lisp-program "/usr/bin/abcl")
(add-to-list 'load-path "/usr/share/emacs/site-lisp/slime/")
(require 'slime)
(slime-setup)

(add-to-list 'auto-mode-alist '("\\.lisp$" . lisp-mode))
(add-to-list 'auto-mode-alist '("\\.cl$" . lisp-mode))
(add-to-list 'auto-mode-alist '("\\.asd$" . lisp-mode))
(require 'slime)
(slime-setup)
(eval-after-load "slime"
 '(progn
   (setq slime-complete-symbol*-fancy t
      slime-complete-symbol-function 'slime-fuzzy-complete-symbol
      slime-when-complete-filename-expand t
      slime-truncate-lines nil
      slime-autodoc-use-multiline-p t)
(slime-setup '(slime-fancy slime-asdf))
(define-key slime-repl-mode-map (kbd "C-c ;")
  'slime-insert-balanced-comments)
(define-key slime-repl-mode-map (kbd "C-c M-;")
  'slime-remove-balanced-comments)
(define-key slime-mode-map (kbd "C-c ;")
  'slime-insert-balanced-comments)
(define-key slime-mode-map (kbd "C-c M-;")
  'slime-remove-balanced-comments)
(define-key slime-mode-map (kbd "RET") 'newline-and-indent)
(define-key slime-mode-map (kbd "C-j") 'newline)))

(add-to-list 'default-frame-alist '(left . 0))
(add-to-list 'default-frame-alist '(top . 0))
(add-to-list 'default-frame-alist '(height . 70))
(add-to-list 'default-frame-alist '(width . 80))

(normal-erase-is-backspace-mode 0)
(tool-bar-mode -1) ;;method for disabling changed in 24. can not nil, most negative
(scroll-bar-mode -1)

;;for loading cedet.
(load-file "/usr/share/emacs/site-lisp/cedet/common/cedet.el")
Pacer answered 13/6, 2012 at 2:8 Comment(1)
According to Emacs' documenation, new-frame is obsolete. When I use make-frame the settings get respected. emacsclient -c works fine for me too.Pe
G
6

You might try adding the following hook to your Emacs start-up file (normally, ~/.emacs), i.e.:

(add-hook 'before-make-frame-hook
          #'(lambda ()
              (add-to-list 'default-frame-alist '(left   . 0))
              (add-to-list 'default-frame-alist '(top    . 0))
              (add-to-list 'default-frame-alist '(height . 70))
              (add-to-list 'default-frame-alist '(width  . 80))))

Or, if you want to reload the whole .emacs file:

(add-hook 'before-make-frame-hook #'(lambda () (load-file "~/.emacs")))

The former hook is almost certainly preferable, as reloading the entire .emacs file is probably not only unnecessary and wasteful of resources, but also has the potential to cause errors or weird behavior (depending on the file's contents).

To elaborate, hooks are variables which define lists of commands that are executed when specific events happen within your Emacs session, e.g., the loading of a major editing mode, or, as in this case, the creation of a new frame. See the relevant Emacs manual page on hooks for more information. Generally speaking, if you want some function to be executed every time a particular event occurs within Emacs, adding said function to the right pre-existing hook is probably the best way to go about it.

Ginter answered 13/6, 2012 at 3:58 Comment(10)
Thanks for the help, but for whatever reason your first solution does not work. 'make-frame' does not respect my settings at all, and 'new-frame' only appears to respect the width settings.Pacer
@user126553, forgive me if this seems too obvious, but you did add that first command to your .emacs and actually load Emacs before trying to run emacsclient -c, right? It works perfectly on my system. Is it possible you're loading a package that modifies these settings, or they're configured somewhere else, like via the Emacs*geometry setting in an ~/.Xresources or ~/.Xdefaults file?Ginter
I know that it sounds strange, but the window is even appearing in the correct position. Besides slime and Cedet I have no no additional plugins installed. I have nothing in my .Xresources file and I only have settings for xterm in my .Xdefaults file.Pacer
@user126553, perhaps try adding the commands to after-init-hook as well to ensure they get run after everything in .emacs is loaded. If you were to execute that first add-hook command inside an Emacs buffer (in case you're unsure how, place your cursor immediately to the right of the last parenthesis in the command and hit C-x C-e to run eval-last-sexp) and then try running emacsclient, are the window dimensions correct?Ginter
@user126553, then I'm stumped. Is it possible your window manager is resizing the new frame (either because it has default values configured, or because your desired size exceeds the dimensions of your display)? The only other option I could recommend is to try configuring this via X resource settings, using Emacs*geometry.Ginter
@user126553, try executing the following command inside a running, graphical Emacs session: (make-frame '((top . 0) (left . 0) (width . 20) (height . 20) (window-system . x))). I deliberately chose smaller values for width and height to ensure that your WM isn't simply resizing the window if it exceeds your screen's boundaries. Does that work?Ginter
Yup, it works. I have a 24" screen so I don't think that size should really be an issue though. I should point out that if I set that command to height value of 70 it does not work properly :/. It's weird because it works when I start emacs normally. Just not when creating a new frame.Pacer
@user126553, well, depending on your font size, etc., a height value of 70 could be quite large. On my (admittedly smaller) screen, a value of 50 makes Emacs occupy nearly the entire height of my display. If everything other than the height works, then I'd try experimenting with other values until you find something that satisfies you. Consider filing a bug report as well, as this sounds like fairly odd behavior.Ginter
Yes, it appears that at values over 60 the frame is resized (by kde I guess). The reason the problem baffles me is that the settings work perfectly fine when starting emacs normally, the issue only occurs when creating a new frame. In any case, I can live with a frame height of 60. Thanks for the help! I'd vote you up but I lack the karma :)Pacer
@user126553, no problem, glad I could be some of assistance. I would still a file a bug report, though whether it belongs with Emacs or KDE is an open question. Anyway, if you feel I've answered your question satisfactorily, you can mark it is answered. Thanks, and good luck.Ginter

© 2022 - 2024 — McMap. All rights reserved.