summaryrefslogtreecommitdiff
path: root/.emacs.d/early-init.el
blob: c3d67b50709f9fe3eb71fe5c2d6ad69335eda72a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
;;; early-init.el --- frames conf. -*- lexical-binding:t;no-byte-compile:t -*-

;; Copyright (C) 2020-2022  Sean Whitton <spwhitton@spwhitton.name>
;;
;; Released under the terms of the GNU GPL as published by the FSF; either
;; version 3, or (at your option) any later version.

;;; Per docstring for `after-make-frame-functions', these additions to that
;;; list of functions are made in this file in order that they can affect the
;;; initial frame.

(defun spw/set-fontset-fonts (frame)
  "Set some fonts for specific charsets.
This has to happen after there's at least one graphical frame, or the fonts
won't be found."
  (when (display-graphic-p frame)
    (setq after-make-frame-functions
	  (delq #'spw/set-fontset-fonts after-make-frame-functions))
    (let ((spec (font-spec :name "Noto Serif CJK JP")))
      (dolist (charset '(kana han symbol cjk-misc bopomofo))
	(set-fontset-font t charset spec)))
    (set-fontset-font t 'hangul (font-spec :name "Noto Serif CJK KR"))))
(add-to-list 'after-make-frame-functions #'spw/set-fontset-fonts)

(defvar spw/tiling-wm-p nil
  "Have we had at least one frame managed by a tiling window manager?")

(defun spw/disable-mouse-autoselect-window (orig-fun &rest args)
  (let (mouse-autoselect-window)
    (apply orig-fun args)))

;; 'emacsclient --spw/update-environment' supplies us with I3SOCK, so if we
;; see that variable we know there is at least one frame under i3 or swaywm,
;; and so we want to set `focus-follows-mouse' to t.  As we can't do that
;; frame-locally, once it's set it's set.  (I used to set
;; `mouse-autoselect-window' to t too, but it often caused unwanted focus
;; changes, and it makes the tool and menu bars much fiddlier to use.)
;;
;; On the other hand, ideally functions like `spw/save-buffer-for-later'
;; decide whether to open a new frame or a new tab frame-locally, so we used
;; to temporarily set DISPLAY to (frame-parameter (selected-frame) 'display)
;; and then call wmctrl(1) to get the name of the window manager for that
;; frame, and stored it in a frame parameter.  But that works only for X11,
;; not Wayland.  So for now we just set a boolean `spw/tiling-wm-p' to which
;; functions like `spw/save-buffer-for-later' can respond.
(defun spw/detect-tiling-wm (frame)
  (unless (or spw/tiling-wm-p
	      (not (setq spw/tiling-wm-p
			 (or (getenv "I3SOCK") (getenv "SWAYSOCK")))))
    (setq focus-follows-mouse t
	  ;; mouse-autoselect-window t
	  desktop-restore-forces-onscreen nil)
    ;; Disable `mouse-autoselect-window' during `display-buffer', to avoid
    ;; surprise focus changes -- some code that calls `display-buffer' does
    ;; not expect `mouse-autoselect-window' to be on.  E.g. `magit-status' can
    ;; leave focus in the wrong window without this.
    ;; (advice-add 'display-buffer
    ;; 		:around #'spw/disable-mouse-autoselect-window)
    )
  ;; If X or Sway, we know we have a usable primary selection, so we used to
  ;; turn off additionally copying to the clipboard.  However, we do want to
  ;; be able to yank from the clipboard.  See also NEWS.24.
  ;; (setq select-enable-clipboard
  ;; 	(not (or spw/tiling-wm-p (eq (framep frame) 'x))))
  )
(add-to-list 'after-make-frame-functions #'spw/detect-tiling-wm)

(defun spw/use-tabs-not-frames (&optional frame)
  "Whether to pop up new tabs instead of new frames.
Should be t when do not have a good way to handle having lots of open
frames, as I do have under i3/swaywm with its tabbed layout, which is my
default layout."
  (not (and spw/tiling-wm-p
	    (memq (framep (or frame (selected-frame))) '(x pgtk)))))

(defun spw/set-frame-title-format (frame)
  "If we're going to be using multiple frames, make `frame-title-format' not
depend on whether there are multiple frames right now."
  (unless (spw/use-tabs-not-frames frame)
    (setq after-make-frame-functions
	  (delq #'spw/set-frame-title-format after-make-frame-functions))
    (setq frame-title-format
	  (concat "%b - "
		  (if (file-in-directory-p
		       invocation-directory (expand-file-name "~/"))
		      "in-tree" "installed")
		  " GNU Emacs at " system-name)
	  icon-title-format frame-title-format)))
;; Must come after `spw/detect-tiling-wm'.
(add-to-list 'after-make-frame-functions #'spw/set-frame-title-format t)