进入Linux下新的Emacs桌面系统
Window Management⌗
EXWM Configuration⌗
We use the excellent EXWM module as the basis for our Emacs Desktop Environment. The EXWM Wiki is a great place to find tips about how to configure everything!
NOTE: Make sure you’ve installed nm-applet
, pasystray
and blueman
for the system tray apps to work!
(require 'exwm)
(require 'exwm-config)
(defun efs/run-in-background (command)
(let ((command-parts (split-string command "[ ]+")))
(apply #'call-process `(,(car command-parts) nil 0 nil ,@(cdr command-parts)))))
(defun efs/set-wallpaper ()
(interactive)
;; NOTE: You will need to update this to a valid background path!
(start-process-shell-command
"feh" nil "feh --bg-scale /mnt/sdb6/share/images/001c9104dcc396b76c6c7857e5f8547f.jpg"))
;; "/mnt/sdb6/share/images/001c9104dcc396b76c6c7857e5f8547f.jpg"
(defun efs/exwm-init-hook ()
;; Make workspace 1 be the one where we land at startup
(exwm-workspace-switch-create 1)
;; Open eshell by default
;;(eshell)
;; NOTE: The next two are disabled because we now use Polybar!
;; Show battery status in the mode line
;; (display-battery-mode 1)
;; Show the time and date in modeline
;; (setq display-time-day-and-date t)
;; (display-time-mode 1)
;; Also take a look at display-time-format and format-time-string
;; Start the Polybar panel
(efs/start-panel)
;; Launch apps that will run in the background
(efs/run-in-background "dunst")
;; (efs/run-in-background "nm-applet")
(efs/run-in-background "pasystray")
(efs/run-in-background "blueman-applet"))
(defun efs/exwm-update-class ()
(exwm-workspace-rename-buffer exwm-class-name))
(defun efs/exwm-update-title ()
(pcase exwm-class-name
("Firefox" (exwm-workspace-rename-buffer (format "Firefox: %s" exwm-title)))))
;; This function isn't currently used, only serves as an example how to
;; position a window
(defun efs/position-window ()
(let* ((pos (frame-position))
(pos-x (car pos))
(pos-y (cdr pos)))
(exwm-floating-move (- pos-x) (- pos-y))))
(defun efs/configure-window-by-class ()
(interactive)
(pcase exwm-class-name
("Firefox" (exwm-workspace-move-window 2))
("Sol" (exwm-workspace-move-window 3))
("mpv" (exwm-floating-toggle-floating)
(exwm-layout-toggle-mode-line))))
;; This function should be used only after configuring autorandr!
(defun efs/update-displays ()
(efs/run-in-background "autorandr --change --force")
(efs/set-wallpaper)
(message "Display config: %s"
(string-trim (shell-command-to-string "autorandr --current"))))
(use-package exwm
:config
;; Set the default number of workspaces
(setq exwm-workspace-number 5)
;; When window "class" updates, use it to set the buffer name
(add-hook 'exwm-update-class-hook #'efs/exwm-update-class)
;; When window title updates, use it to set the buffer name
(add-hook 'exwm-update-title-hook #'efs/exwm-update-title)
;; Configure windows as they're created
(add-hook 'exwm-manage-finish-hook #'efs/configure-window-by-class)
;; When EXWM starts up, do some extra confifuration
(add-hook 'exwm-init-hook #'efs/exwm-init-hook)
;; Rebind CapsLock to Ctrl
(start-process-shell-command "xmodmap" nil "xmodmap ~/.emacs.d/exwm/Xmodmap")
;; NOTE: Uncomment the following two options if you want window buffers
;; to be available on all workspaces!
;; Automatically move EXWM buffer to current workspace when selected
;; (setq exwm-layout-show-all-buffers t)
;; Display all EXWM buffers in every workspace buffer list
;; (setq exwm-workspace-show-all-buffers t)
;; NOTE: Uncomment this option if you want to detach the minibuffer!
;; Detach the minibuffer (show it with exwm-workspace-toggle-minibuffer)
;;(setq exwm-workspace-minibuffer-position 'top)
;; Set the screen resolution (update this to be the correct resolution for your screen!)
(require 'exwm-randr)
(exwm-randr-enable)
;; (start-process-shell-command "xrandr" nil "xrandr --output Virtual-1 --primary --mode 2048x1152 --pos 0x0 --rotate normal")
(start-process-shell-command "xrandr" nil "xrandr --output HDMI-1 --auto")
;; This will need to be updated to the name of a display! You can find
;; the names of your displays by looking at arandr or the output of xrandr
(setq exwm-randr-workspace-monitor-plist '(1 "HDMI-1" 3 "HDMI-1"))
;; NOTE: Uncomment these lines after setting up autorandr!
;; React to display connectivity changes, do initial display update
;; (add-hook 'exwm-randr-screen-change-hook #'efs/update-displays)
;; (efs/update-displays)
;; Set the wallpaper after changing the resolution
(efs/set-wallpaper)
;; NOTE: This is disabled because we now use Polybar!
;; Load the system tray before exwm-init
;; (require 'exwm-systemtray)
;; (setq exwm-systemtray-height 32)
;; (exwm-systemtray-enable)
;; Automatically send the mouse cursor to the selected workspace's display
(setq exwm-workspace-warp-cursor t)
;; Window focus should follow the mouse pointer
(setq mouse-autoselect-window t
focus-follows-mouse t)
;; These keys should always pass through to Emacs
(setq exwm-input-prefix-keys
'(?\C-x
?\C-u
?\C-h
?\M-x
?\M-`
?\M-&
?\M-:
?\C-\M-j ;; Buffer list
?\C-\ )) ;; Ctrl+Space
;; Ctrl+Q will enable the next key to be sent directly
(define-key exwm-mode-map [?\C-q] 'exwm-input-send-next-key)
;; Set up global key bindings. These always work, no matter the input state!
;; Keep in mind that changing this list after EXWM initializes has no effect.
(setq exwm-input-global-keys
`(
;; Reset to line-mode (C-c C-k switches to char-mode via exwm-input-release-keyboard)
([?\s-r] . exwm-reset)
;; Move between windows
([s-left] . windmove-left)
([s-right] . windmove-right)
([s-up] . windmove-up)
([s-down] . windmove-down)
;; Launch applications via shell command
([?\s-&] . (lambda (command)
(interactive (list (read-shell-command "$ ")))
(start-process-shell-command command nil command)))
;; Switch workspace
([?\s-w] . exwm-workspace-switch)
([?\s-`] . (lambda () (interactive) (exwm-workspace-switch-create 0)))
;; 's-N': Switch to certain workspace with Super (Win) plus a number key (0 - 9)
,@(mapcar (lambda (i)
`(,(kbd (format "s-%d" i)) .
(lambda ()
(interactive)
(exwm-workspace-switch-create ,i))))
(number-sequence 0 9))))
(exwm-input-set-key (kbd "s-SPC") 'counsel-linux-app)
(exwm-enable))
Desktop Environment⌗
Desktop Key Bindings⌗
We use the desktop-environment package to automatically bind to well-known programs for controlling the volume, screen brightness, media playback, and doing other things like locking the screen and taking screenshots. Make sure that you install the necessary programs to make this functionality work! Check the default programs list to know what you need to install.
(use-package desktop-environment
:after exwm
:config (desktop-environment-mode)
:custom
(desktop-environment-brightness-small-increment "2%+")
(desktop-environment-brightness-small-decrement "2%-")
(desktop-environment-brightness-normal-increment "5%+")
(desktop-environment-brightness-normal-decrement "5%-"))
Desktop File⌗
This file is used by your “login manager” (GDM, LightDM, etc) to display EXWM as a desktop environment option when you log in.
IMPORTANT: Make sure you create a symbolic link for this file into /usr/share/xsessions
:
sudo ln -f ~/.emacs.d/exwm/EXWM.desktop /usr/share/xsessions/EXWM.desktop
[Desktop Entry]
Name=EXWM
Comment=Emacs Window Manager
Exec=sh /home/daviwil/.emacs.d/exwm/start-exwm.sh
TryExec=sh
Type=Application
X-LightDM-DesktopName=exwm
DesktopNames=exwm
Launcher Script⌗
This launcher script is invoked by EXWM.desktop
to start Emacs and load our desktop environment configuration. We also start up some other helpful applications to configure the desktop experience.
# Set the screen DPI (uncomment this if needed!)
# xrdb ~/.emacs.d/exwm/Xresources
# Run the screen compositor
compton &
# Enable screen locking on suspend
xss-lock -- slock &
# Fire it up
exec dbus-launch --exit-with-session emacs -mm --debug-init -l ~/.emacs.d/desktop.el
Keyboard Configuration⌗
The Xmodmap
file will be used with the xmodmap
program to remap CapsLock to Ctrl inside of our desktop environment:
clear lock
clear control
keycode 66 = Control_L
add control = Control_L
add Lock = Control_R
DPI configuration⌗
The Xresources
file will be used with xrdb
in start-exwm.sh
to set our screen DPI:
Xft.dpi: 100 # Set this to your desired DPI! Larger number means bigger text and UI
Panel with Polybar⌗
Polybar provides a great, minimalistic panel for your EXWM desktop configuration. The following config integrates emacsclient
and Polybar with polybar-msg
to enable you to gather any information from Emacs and display it in the panel!
Check out the Polybar wiki for more details on how to configure it: https://github.com/polybar/polybar/wiki
;; Make sure the server is started (better to do this in your main Emacs config!)
(server-start)
(defvar efs/polybar-process nil
"Holds the process of the running Polybar instance, if any")
(defun efs/kill-panel ()
(interactive)
(when efs/polybar-process
(ignore-errors
(kill-process efs/polybar-process)))
(setq efs/polybar-process nil))
(defun efs/start-panel ()
(interactive)
(efs/kill-panel)
(setq efs/polybar-process (start-process-shell-command "polybar" nil "polybar panel")))
(defun efs/send-polybar-hook (module-name hook-index)
(start-process-shell-command "polybar-msg" nil (format "polybar-msg hook %s %s" module-name hook-index)))
(defun efs/send-polybar-exwm-workspace ()
(efs/send-polybar-hook "exwm-workspace" 1))
;; Update panel indicator when workspace changes
(add-hook 'exwm-workspace-switch-hook #'efs/send-polybar-exwm-workspace)
The configuration for our ingeniously named panel, “panel”. Invoke it with polybar panel
on the command line!
; Docs: https://github.com/polybar/polybar
;==========================================================
[settings]
screenchange-reload = true
[global/wm]
margin-top = 0
margin-bottom = 0
[colors]
background = #f0232635
background-alt = #576075
foreground = #A6Accd
foreground-alt = #555
primary = #ffb52a
secondary = #e60053
alert = #bd2c40
underline-1 = #c792ea
[bar/panel]
width = 100%
height = 35
offset-x = 0
offset-y = 0
fixed-center = true
enable-ipc = true
background = ${colors.background}
foreground = ${colors.foreground}
line-size = 2
line-color = #f00
border-size = 0
border-color = #00000000
padding-top = 5
padding-left = 1
padding-right = 1
module-margin = 1
font-0 = "Cantarell:size=18:weight=bold;2"
font-1 = "Font Awesome:size=14;2"
font-2 = "Material Icons:size=20;5"
font-3 = "Fira Mono:size=13;-3"
modules-left = exwm-workspace
modules-right = cpu temperature battery
tray-position = right
tray-padding = 2
tray-maxsize = 28
cursor-click = pointer
cursor-scroll = ns-resize
[module/exwm-workspace]
type = custom/ipc
hook-0 = emacsclient -e "exwm-workspace-current-index" | sed -e 's/^"//' -e 's/"$//'
initial = 1
format-underline = ${colors.underline-1}
format-padding = 1
[module/cpu]
type = internal/cpu
interval = 2
format = <label> <ramp-coreload>
format-underline = ${colors.underline-1}
click-left = emacsclient -e "(proced)"
label = %percentage:2%%
ramp-coreload-spacing = 0
ramp-coreload-0 = ▁
ramp-coreload-0-foreground = ${colors.foreground-alt}
ramp-coreload-1 = ▂
ramp-coreload-2 = ▃
ramp-coreload-3 = ▄
ramp-coreload-4 = ▅
ramp-coreload-5 = ▆
ramp-coreload-6 = ▇
[module/date]
type = internal/date
interval = 5
date = "%a %b %e"
date-alt = "%A %B %d %Y"
time = %l:%M %p
time-alt = %H:%M:%S
format-prefix-foreground = ${colors.foreground-alt}
format-underline = ${colors.underline-1}
label = %date% %time%
[module/battery]
type = internal/battery
battery = BAT0
adapter = ADP1
full-at = 98
time-format = %-l:%M
label-charging = %percentage%% / %time%
format-charging = <animation-charging> <label-charging>
format-charging-underline = ${colors.underline-1}
label-discharging = %percentage%% / %time%
format-discharging = <ramp-capacity> <label-discharging>
format-discharging-underline = ${self.format-charging-underline}
format-full = <ramp-capacity> <label-full>
format-full-underline = ${self.format-charging-underline}
ramp-capacity-0 =
ramp-capacity-1 =
ramp-capacity-2 =
ramp-capacity-3 =
ramp-capacity-4 =
animation-charging-0 =
animation-charging-1 =
animation-charging-2 =
animation-charging-3 =
animation-charging-4 =
animation-charging-framerate = 750
[module/temperature]
type = internal/temperature
thermal-zone = 0
warn-temperature = 60
format = <label>
format-underline = ${colors.underline-1}
format-warn = <label-warn>
format-warn-underline = ${self.format-underline}
label = %temperature-c%
label-warn = %temperature-c%!
label-warn-foreground = ${colors.secondary}
Desktop Notifications with Dunst⌗
We use an application called Dunst to enable the display of desktop notifications from Emacs and other applications running within EXWM. Consult the documentation for more details on how to configure this to your liking!
Here are some things you might want to consider changing:
format
- Customize how notification text contents are displayedgeometry
- Where the notification appears and how large it should be by defaulturgency_normal
, etc - configures the background and frame color for notifications of different typesmax_icon_size
- Constrain icon display since some icons will be larger than othersicon_path
- Important if your icons are not in a common location (like when using GNU Guix)idle_threshold
- Wait for user to become active for this long before hiding notificationsmouse_left/right/middle_click
- Action to take when clicking a notification- Any of the key bindings in the
shortcuts
section (though these are deprecated in 1.5.0, usedunstctl
)
[global]
### Display ###
monitor = 0
# The geometry of the window:
# [{width}]x{height}[+/-{x}+/-{y}]
geometry = "500x10-10+50"
# Show how many messages are currently hidden (because of geometry).
indicate_hidden = yes
# Shrink window if it's smaller than the width. Will be ignored if
# width is 0.
shrink = no
# The transparency of the window. Range: [0; 100].
transparency = 10
# The height of the entire notification. If the height is smaller
# than the font height and padding combined, it will be raised
# to the font height and padding.
notification_height = 0
# Draw a line of "separator_height" pixel height between two
# notifications.
# Set to 0 to disable.
separator_height = 1
separator_color = frame
# Padding between text and separator.
padding = 8
# Horizontal padding.
horizontal_padding = 8
# Defines width in pixels of frame around the notification window.
# Set to 0 to disable.
frame_width = 2
# Defines color of the frame around the notification window.
frame_color = "#89AAEB"
# Sort messages by urgency.
sort = yes
# Don't remove messages, if the user is idle (no mouse or keyboard input)
# for longer than idle_threshold seconds.
idle_threshold = 120
### Text ###
font = Cantarell 20
# The spacing between lines. If the height is smaller than the
# font height, it will get raised to the font height.
line_height = 0
markup = full
# The format of the message. Possible variables are:
# %a appname
# %s summary
# %b body
# %i iconname (including its path)
# %I iconname (without its path)
# %p progress value if set ([ 0%] to [100%]) or nothing
# %n progress value if set without any extra characters
# %% Literal %
# Markup is allowed
format = "<b>%s</b>\n%b"
# Alignment of message text.
# Possible values are "left", "center" and "right".
alignment = left
# Show age of message if message is older than show_age_threshold
# seconds.
# Set to -1 to disable.
show_age_threshold = 60
# Split notifications into multiple lines if they don't fit into
# geometry.
word_wrap = yes
# When word_wrap is set to no, specify where to make an ellipsis in long lines.
# Possible values are "start", "middle" and "end".
ellipsize = middle
# Ignore newlines '\n' in notifications.
ignore_newline = no
# Stack together notifications with the same content
stack_duplicates = true
# Hide the count of stacked notifications with the same content
hide_duplicate_count = false
# Display indicators for URLs (U) and actions (A).
show_indicators = yes
### Icons ###
# Align icons left/right/off
icon_position = left
# Scale larger icons down to this size, set to 0 to disable
max_icon_size = 88
# Paths to default icons.
icon_path = /usr/share/icons/Adwaita/96x96/status:/usr/share/icons/Adwaita/96x96/emblems
### History ###
# Should a notification popped up from history be sticky or timeout
# as if it would normally do.
sticky_history = no
# Maximum amount of notifications kept in history
history_length = 20
### Misc/Advanced ###
# Browser for opening urls in context menu.
browser = qutebrowser
# Always run rule-defined scripts, even if the notification is suppressed
always_run_script = true
# Define the title of the windows spawned by dunst
title = Dunst
# Define the class of the windows spawned by dunst
class = Dunst
startup_notification = false
verbosity = mesg
# Define the corner radius of the notification window
# in pixel size. If the radius is 0, you have no rounded
# corners.
# The radius will be automatically lowered if it exceeds half of the
# notification height to avoid clipping text and/or icons.
corner_radius = 4
mouse_left_click = close_current
mouse_middle_click = do_action
mouse_right_click = close_all
# Experimental features that may or may not work correctly. Do not expect them
# to have a consistent behaviour across releases.
[experimental]
# Calculate the dpi to use on a per-monitor basis.
# If this setting is enabled the Xft.dpi value will be ignored and instead
# dunst will attempt to calculate an appropriate dpi value for each monitor
# using the resolution and physical size. This might be useful in setups
# where there are multiple screens with very different dpi values.
per_monitor_dpi = false
[shortcuts]
# Shortcuts are specified as [modifier+][modifier+]...key
# Available modifiers are "ctrl", "mod1" (the alt-key), "mod2",
# "mod3" and "mod4" (windows-key).
# Xev might be helpful to find names for keys.
# Close notification.
#close = ctrl+space
# Close all notifications.
#close_all = ctrl+shift+space
# Redisplay last message(s).
# On the US keyboard layout "grave" is normally above TAB and left
# of "1". Make sure this key actually exists on your keyboard layout,
# e.g. check output of 'xmodmap -pke'
history = ctrl+grave
# Context menu.
context = ctrl+shift+period
[urgency_low]
# IMPORTANT: colors have to be defined in quotation marks.
# Otherwise the "#" and following would be interpreted as a comment.
background = "#222222"
foreground = "#888888"
timeout = 10
# Icon for notifications with low urgency, uncomment to enable
#icon = /path/to/icon
[urgency_normal]
background = "#1c1f26"
foreground = "#ffffff"
timeout = 10
# Icon for notifications with normal urgency, uncomment to enable
#icon = /path/to/icon
[urgency_critical]
background = "#900000"
foreground = "#ffffff"
frame_color = "#ff0000"
timeout = 0
# Icon for notifications with critical urgency, uncomment to enable
#icon = /path/to/icon
We can also set up some functions for enabling and disabling notifications at any time:
(defun efs/disable-desktop-notifications ()
(interactive)
(start-process-shell-command "notify-send" nil "notify-send \"DUNST_COMMAND_PAUSE\""))
(defun efs/enable-desktop-notifications ()
(interactive)
(start-process-shell-command "notify-send" nil "notify-send \"DUNST_COMMAND_RESUME\""))
(defun efs/toggle-desktop-notifications ()
(interactive)
(start-process-shell-command "notify-send" nil "notify-send \"DUNST_COMMAND_TOGGLE\""))