Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to toggle fullscreen with Emacs as default?

Tags:

emacs

macos

I am using the mac emacs from http://emacsformacosx.com/, and I need to click the maximized icon when I start my emacs.

How can I set the maximized emacs window as default?

like image 668
why Avatar asked Feb 12 '12 13:02

why


1 Answers

The short answer is to add the following to your custom-set-variables:-

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 ...
 '(initial-frame-alist (quote ((fullscreen . maximized))))
 ...
 )

Given below is what I wanted as a solution to the same problem. TL;DR.

I face the same problem but in all applications and not just in Emacs. To this end, I have globally bound the shortcut key cmd-m on my Mac to the Zoom menu option which is usually the menu option for the green maximize button. Emacs however doesn't provide the Zoom menu option which is usually under the Window menu item. So I ended up with the following.

I just coded up the following last night.

;; This defines cmd-m to do the same as clicking the green titlebar button
;; usually meant for the "Window -> Zoom" menu option in Mac apps
(defun zoom () "zoom, same as clicking the green titlebar button in Mac app windows"
  (interactive)
  (set-frame-parameter
   nil 'fullscreen
   (pcase (frame-parameter nil 'fullscreen)
     (`nil 'fullheight)
     (`fullheight 'maximized)
     (`fullboth (ding) 'fullboth)
     (`fullscreen (ding) 'fullscreen)
     (_ nil))))
(global-set-key (kbd "s-m") 'zoom)

This keyboard shortcut in the last line of the code goes well with my global to Mac cmd+m key binding that I described initially. You could customize it to whatever suits you. I am used to pressing cmd-m on launching most apps until it fits screen and Emacs is one of them for me. So I don't bother with the initial-frame-alist setting.

I went on to complete the feature-set I wanted by adding the following code tonight.

;; This defines ctrl-cmd-f to do the same as clicking the toggle-fullscreen titlebar
;; icon usually meant for the "View -> Enter/Exit Full Screen" menu option in
;; Mac apps
(defun toggle-fullscreen() "toggle-fullscreen, same as clicking the
 corresponding titlebar icon in the right hand corner of Mac app windows"
  (interactive)
  (set-frame-parameter
   nil 'fullscreen
   (pcase (frame-parameter nil 'fullscreen)
     (`fullboth nil)
     (`fullscreen nil)
     (_ 'fullscreen))))
(global-set-key (kbd "C-s-f") 'toggle-fullscreen)
; For some weird reason C-s-f only means right cmd key!
(global-set-key (kbd "<C-s-268632070>") 'toggle-fullscreen)

A couple of notes:-

  1. If you're just learning to use pcase from this code, be careful not to make the same mistake as I did by misreading the backquote as a quote in the docs.
  2. fullscreen is an alias to fullboth and is not a misnomer like the latter is as a term for what it means and hence I have not only handled that case as a value for (frame-parameter nil 'fullscreen) but use that whenever I want to set-frame-parameter to fullboth

HTH

like image 99
trss Avatar answered Sep 20 '22 23:09

trss