Commit b6d801ef5bec

Vincent Demeester <vincent@sbr.pm>
2025-07-21 09:37:34
tools/emacs: emacs-anywhere for wayland
because we can :D Signed-off-by: Vincent Demeester <vincent@sbr.pm>
1 parent 00d9f0a
Changed files (1)
tools
emacs
tools/emacs/init.el
@@ -1520,6 +1520,59 @@ Add this function to the `after-save-hook'."
 
 (add-hook 'prog-mode-hook #'highlight-codetags-local-mode)
 
+(defun vde/wtype-text (text)
+  "Process TEXT for wtype, handling newlines properly."
+  (let* ((has-final-newline (string-match-p "\n$" text))
+         (lines (split-string text "\n"))
+         (last-idx (1- (length lines))))
+    (string-join
+     (cl-loop for line in lines
+              for i from 0
+              collect (cond
+                       ;; Last line without final newline
+                       ((and (= i last-idx) (not has-final-newline))
+                        (format "wtype -s 50 \"%s\"" 
+                                (replace-regexp-in-string "\"" "\\\\\"" line)))
+                       ;; Any other line
+                       (t
+                        (format "wtype -s 50 \"%s\" && wtype -k Return" 
+                                (replace-regexp-in-string "\"" "\\\\\"" line)))))
+     " && ")))
+
+(define-minor-mode vde/type-mode
+  "Minor mode for inserting text via wtype."
+  :keymap `((,(kbd "C-c C-c") . ,(lambda () (interactive)
+                                   (call-process-shell-command
+                                    (vde/wtype-text (buffer-string))
+                                    nil 0)
+                                   (delete-frame)))
+            (,(kbd "C-c C-k") . ,(lambda () (interactive)
+                                   (kill-buffer (current-buffer))))))
+
+(defun vde/type ()
+  "Launch a temporary frame with a clean buffer for typing."
+  (interactive)
+  (let ((frame (make-frame '((name . "emacs-float")
+                             (fullscreen . 0)
+                             (undecorated . t)
+                             (width . 70)
+                             (height . 20))))
+        (buf (get-buffer-create "emacs-float")))
+    (select-frame frame)
+    (switch-to-buffer buf)
+    (with-current-buffer buf
+      (erase-buffer)
+      ;; (org-mode)
+      (markdown-mode) ;; more common ?
+      (flyspell-mode)
+      (vde/type-mode)
+      (setq-local header-line-format
+                  (format " %s to insert text or %s to cancel."
+                          (propertize "C-c C-c" 'face 'help-key-binding)
+			  (propertize "C-c C-k" 'face 'help-key-binding)))
+      ;; Make the frame more temporary-like
+      (set-frame-parameter frame 'delete-before-kill-buffer t)
+      (set-window-dedicated-p (selected-window) t))))
 
 (provide 'init)
 ;;; init.el ends here