reddit / emacs

Adjust your laptop's screen brightness in Emacs

whatacold · 2022-05-20 16:24
Hi, Just took some time to bake a helper command to adjust the laptop's screen brightness, so as that I can easily adjust it in Emacs in a tiling window environment. The code is inspired (well, almost copied from) by `text-scale-adjust`: ```elisp (defun w/screen-brightness-adjust (inc) "Adjust the screen brightness by INC*10%. INC may be passed as a numeric prefix argument. The actual adjustment made depends on the final component of the key-binding used to invoke the command, with all modifiers removed: +, = Increase the height of the default face by one step - Decrease the height of the default face by one step After adjusting, continue to read input events and further adjust the screen brightness as long as the input event read \(with all modifiers removed) is one of the above characters. " (interactive "p") (let ((ev last-command-event) (echo-keystrokes nil)) (let* ((base (event-basic-type ev)) (step (* inc 10)) (cmd (format "brightnessctl set %s%d%%%s" ; +10% or 10%- (if (or (= ?+ base) (= ?= base)) "+" "") step (if (= ?- base) "-" "")))) (shell-command cmd) (message "Use +,- for further adjustment by %d%%" step) (set-transient-map (let ((map (make-sparse-keymap))) (dolist (mods '(() (control))) (dolist (key '(?- ?+ ?=)) ;; = is often unshifted +. (define-key map (vector (append mods (list key))) (lambda () (interactive) (w/screen-brightness-adjust (abs inc)))))) map))))) ```
ieure · 2022-05-20 16:49
See also my [blight](https://gitlab.com/ieure/blight/) package, which has a nice abstract interface / concrete implementation approach, which can be extended to give a consistent interface to any OS/platform-specific backlight mechanism.
whatacold · 2022-05-21 02:39
Thanks for sharing! I only tested my snippet on linux, and it depends on the command-line tool `brightnessctl`.
quantum_mattress · 2022-05-21 05:21
Linux and Mac user here. I haven't messed with this yet but I did find the info on changing Mac screen brightness from the shell using weird key-sequences with osascript or installing the brightness command via brew. ​ https://www.maketecheasier.com/adjust-screen-brightness-from-terminal-macos/
simbledress · 2022-05-21 10:15
See also: [desktop-environment mode](https://gitea.petton.fr/DamienCassou/desktop-environment)