4

I have my Emacs default new frame set to

(setq default-frame-alist
      '((top . 150) (left . 400)
        (width . 120) (height . 50)))

Is there a way to write a funciton to offset each new frame by 5 units at top and left so that each new frame will not be perfectly superimposed on top of each other? In other words, I want to cascade all new frames.

My system is OS X with Emacs 24.3.1

Cœur
  • 37,241
  • 25
  • 195
  • 267
Bart Simpson
  • 749
  • 3
  • 7
  • 17

1 Answers1

4

I suggest that you modify default-frame-alist in before-make-frame-hook:

(add-hook 'before-make-frame-hook 'cascade-default-frame-alist)
(defun cascade-default-frame-alist ()
  (setq default-frame-alist
        (mapcar (lambda (kv)
                  (if (memq (car kv) '(top left))
                      (cons (car kv) (+ 5 (cdr kv)))
                      kv))
                default-frame-alist)))

If you want to modify default-frame-alist in-place, you need to create it with list instead of quote:

(setq default-frame-alist (list (cons 'top 150) (cons 'left 400)
                                (cons 'width 120) (cons 'height 50)))
(defun cascade-default-frame-alist ()
  (dolist (kv default-frame-alist)
    (when (memq (car kv) '(top left))
      (setcdr kv (+ 5 (cdr kv))))))
Community
  • 1
  • 1
sds
  • 58,617
  • 29
  • 161
  • 278
  • The second in-place version does not work on my system. Emacs warns me "Wrong type argument: integerp, (50)." I am digging the links you provided. – Bart Simpson Sep 12 '14 at 19:17
  • sorry, my fault, should be fixed now – sds Sep 12 '14 at 19:24
  • @sds I have limited elisp experience, and I can't understand where the `(mapcar (lambda (kv)` part (first block). Where the `kv` value is coming from? How can that work? – gsl Apr 16 '16 at 07:54
  • 1
    @gsl: read up on [mapcar](http://clhs.lisp.se/Body/f_mapc_.htm): https://www.gnu.org/software/emacs/manual/html_node/eintr/mapcar.html https://www.gnu.org/software/emacs/manual/html_node/elisp/Mapping-Functions.html – sds Apr 17 '16 at 01:57
  • Thank you for point that out. Lambda does not fetch `kv` anywhere, but rather it is `mapcar` that extracts it from `default-frame-alist` and feeds it to the lambda. Nice! – gsl Apr 17 '16 at 14:20