2023-03-30 01:00:22 -08:00
|
|
|
;;; notibox.el --- PosFrame-based notification UI -*- lexical-binding: t; -*-
|
|
|
|
|
2024-02-23 20:53:43 -09:00
|
|
|
;; Copyright (C) 2023 mir
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2024-10-20 08:57:05 -08:00
|
|
|
;; Author: mir <mirmarq428@gmail.com>
|
2023-03-30 01:00:22 -08:00
|
|
|
;; Keywords:frames,convenience,help
|
2024-11-15 12:21:16 -09:00
|
|
|
;; Package-Requires: ((posframe) (alert) (dash))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2023-04-07 09:15:03 -08:00
|
|
|
;; This file is NOT part of GNU Emacs.
|
2023-03-30 01:00:22 -08:00
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2023-04-21 21:40:02 -08:00
|
|
|
;; This package shows a temporary child frame which mirrors the contents of the
|
|
|
|
;; echo area. It was designed to be used with configurations that hide the echo
|
|
|
|
;; area by splitting it into a `minibuffer-frame'.
|
2023-03-30 01:00:22 -08:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'posframe)
|
2023-05-15 19:09:14 -08:00
|
|
|
(require 'alert)
|
2024-11-15 12:21:16 -09:00
|
|
|
(require 'dash)
|
2024-02-23 20:53:43 -09:00
|
|
|
(if (not (fboundp #'string-pixel-width))
|
|
|
|
(progn
|
|
|
|
(require 'shr) ;for `shr-string-pixel-width'
|
|
|
|
(defalias #'string-pixel-width #'shr-string-pixel-width)))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
|
|
|
(defvar notibox-width 40) ; characters
|
|
|
|
(defvar notibox-height 4) ; characters
|
2023-04-07 09:15:49 -08:00
|
|
|
(defvar notibox-border-color "#808080")
|
2023-08-06 18:28:15 -08:00
|
|
|
(defgroup notibox nil
|
|
|
|
"The notification box.")
|
2023-05-15 19:06:30 -08:00
|
|
|
(defcustom notibox-corner 'bottomright
|
|
|
|
"Corner in which to show the NotiBox."
|
|
|
|
:type '(choice
|
|
|
|
(const :tag "Top Left" 'topleft)
|
|
|
|
(const :tag "Bottom Left" 'bottomleft)
|
|
|
|
(const :tag "Top Right" 'topright)
|
|
|
|
(const :tag "Bottom Right" 'bottomright)))
|
2023-03-30 01:00:22 -08:00
|
|
|
(defvar notibox-padding 30)
|
2023-04-21 21:42:09 -08:00
|
|
|
(defvar alert-fade-time 5); seconds, also provided by `alert' package
|
|
|
|
(defvar notibox--refresh-delay 0.1) ; seconds. Probably don't change this one.
|
2023-08-11 19:37:10 -08:00
|
|
|
(defun notibox--get-position (&optional stackdepth)
|
|
|
|
"Return the starting coordinate at which to place the next notibox, as cons.
|
|
|
|
|
|
|
|
If STACKDEPTH is non-nil and nonzero, return a position that far down."
|
|
|
|
(let* (
|
2024-10-20 09:25:32 -08:00
|
|
|
;; need to not overlap the gtk scrollbar
|
|
|
|
(scrollbar-width (or (if (eq x-toolkit-scroll-bars 'gtk)
|
2024-11-15 12:22:50 -09:00
|
|
|
30) ; educated guess...
|
2024-10-20 09:25:32 -08:00
|
|
|
0))
|
2024-11-15 12:22:50 -09:00
|
|
|
;; (stackdepth (or stackdepth 0))
|
|
|
|
(stackdepth 0) ; we do not care actually
|
2023-08-11 19:37:10 -08:00
|
|
|
(parent-width (frame-pixel-width))
|
2024-02-23 20:53:43 -09:00
|
|
|
(child-width (* notibox-width (string-pixel-width " ")))
|
2023-03-30 01:00:22 -08:00
|
|
|
(parent-height (frame-pixel-height))
|
2023-08-11 19:37:10 -08:00
|
|
|
(child-height (* notibox-height (line-pixel-height)))
|
|
|
|
(padded-height (+ child-height notibox-padding))
|
|
|
|
(x-justify (pcase notibox-corner
|
|
|
|
((or 'topleft 'bottomleft) notibox-padding)
|
|
|
|
((or 'topright 'bottomright)
|
2024-10-20 09:25:32 -08:00
|
|
|
(- (- parent-width scrollbar-width)
|
|
|
|
(+ child-width notibox-padding)))))
|
2023-08-11 19:37:10 -08:00
|
|
|
(y-justify (pcase notibox-corner
|
|
|
|
((or 'topleft 'topright) notibox-padding)
|
|
|
|
((or 'bottomleft 'bottomright)
|
|
|
|
(- parent-height (+ child-height notibox-padding)))))
|
|
|
|
(y-direction (pcase notibox-corner
|
|
|
|
((or 'topleft 'topright) #'+)
|
|
|
|
((or 'bottomleft 'bottomright) #'-)))
|
2024-10-20 14:17:25 -08:00
|
|
|
(y-coord (funcall y-direction y-justify (* stackdepth padded-height)))
|
2023-08-11 19:37:10 -08:00
|
|
|
)
|
|
|
|
(cons x-justify y-coord)
|
|
|
|
))
|
2024-02-23 20:53:43 -09:00
|
|
|
;; (notibox--get-position 3)
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2024-10-20 14:17:25 -08:00
|
|
|
(defun notibox--prepare-buffer (title body &optional buffer)
|
2024-11-15 12:22:50 -09:00
|
|
|
"Return the notibox buffer containing TITLE and BODY, properly formatted"
|
|
|
|
(let ((buf (get-buffer-create (format " *notibox-- %s: %s *" title body))))
|
2024-10-20 14:17:25 -08:00
|
|
|
(with-current-buffer buf
|
|
|
|
(let ((inhibit-read-only t))
|
|
|
|
(erase-buffer)
|
|
|
|
(insert (format "%s\n%s\n%s" ;; (buttonize title #'view-echo-area-messages)
|
|
|
|
title
|
|
|
|
(propertize (make-string notibox-width ?─)
|
|
|
|
'face `((:foreground ,notibox-border-color)))
|
2024-11-15 12:22:50 -09:00
|
|
|
body))))
|
|
|
|
buf))
|
2024-10-20 14:17:25 -08:00
|
|
|
;; (notibox--prepare-buffer "test" "this better work gadahgit" (or nil "*notibox-2*"))
|
|
|
|
;; (get-buffer-create "*notibox-2*")
|
|
|
|
;; wait, we need to be actually basing the things on the buffers huh
|
2023-03-30 01:00:22 -08:00
|
|
|
|
|
|
|
(defvar notibox-current-posframes nil)
|
2024-10-20 14:17:25 -08:00
|
|
|
(cl-defun notibox--show (&key timeout &key depth &key buf)
|
|
|
|
"Show the notibox currently prepared, with optional TIMEOUT, at DEPTH, in BUF."
|
2024-11-15 12:22:50 -09:00
|
|
|
(let ((this-notibox
|
|
|
|
(let ((buffer (get-buffer-create (or buf "*notibox*"))))
|
|
|
|
(posframe-show buffer
|
|
|
|
:position (notibox--get-position depth)
|
|
|
|
:left-fringe 0
|
|
|
|
:right-fringe 0
|
|
|
|
:max-width notibox-width
|
|
|
|
:max-height notibox-height
|
|
|
|
:min-width notibox-width
|
|
|
|
:min-height notibox-height
|
|
|
|
:border-width 2
|
|
|
|
:border-color notibox-border-color
|
|
|
|
;; :timeout timeout
|
|
|
|
))
|
|
|
|
))
|
|
|
|
(run-with-timer timeout nil #'notibox-delete this-notibox)
|
|
|
|
(mir/push-notibox this-notibox)
|
|
|
|
;; (add-to-list 'notibox-current-posframes
|
|
|
|
;; this-notibox)
|
|
|
|
)
|
2023-04-07 09:15:33 -08:00
|
|
|
nil)
|
2023-03-30 01:00:22 -08:00
|
|
|
|
|
|
|
(defun notibox-alert (info)
|
2023-08-06 18:29:26 -08:00
|
|
|
"Show a notibox corresponding with INFO. Can* be used as a backend for `alert'."
|
2023-03-30 01:00:22 -08:00
|
|
|
(let* ((message (plist-get info :message))
|
2023-04-07 09:15:33 -08:00
|
|
|
(title (plist-get info :title))
|
2023-08-11 19:39:28 -08:00
|
|
|
(timeout (plist-get info :persistent))
|
|
|
|
(depth (plist-get info :depth)))
|
2024-11-15 12:22:50 -09:00
|
|
|
;; gotta make it move the others down...
|
2023-08-11 19:39:28 -08:00
|
|
|
(notibox--show :timeout (unless timeout alert-fade-time)
|
2024-11-15 12:22:50 -09:00
|
|
|
;; :depth (or depth (- (length notibox-current-posframes) 1))
|
|
|
|
:buf (notibox--prepare-buffer title message))))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2023-08-12 13:44:33 -08:00
|
|
|
(defun notibox--resolve-frame (object)
|
|
|
|
"Return the /frame reference/ signified by OBJECT, whatever it may be."
|
|
|
|
(cond
|
|
|
|
((framep object) object) ;this is the easy one
|
|
|
|
((windowp object) (window-frame object))
|
|
|
|
((bufferp object) (window-frame (get-window-buffer object)))
|
|
|
|
((stringp object)
|
|
|
|
(window-frame (get-buffer-window (get-buffer object))))
|
|
|
|
((symbolp object)
|
|
|
|
(window-frame (get-buffer-window (get-buffer (format "%s" object)))))
|
|
|
|
))
|
|
|
|
;; (notibox--resolve-frame "*notibox*")
|
|
|
|
|
2023-03-30 01:00:22 -08:00
|
|
|
(defun notibox--hide (frame)
|
2023-08-06 18:29:26 -08:00
|
|
|
"Stop showing FRAME."
|
2024-10-20 08:57:23 -08:00
|
|
|
(if (frame-live-p frame)
|
2024-11-15 12:22:50 -09:00
|
|
|
(posframe-delete (window-buffer (frame-selected-window frame)))))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2023-04-07 09:14:24 -08:00
|
|
|
(defun notibox-delete (frame)
|
2023-08-11 15:44:11 -08:00
|
|
|
"Delete the notibox FRAME.
|
|
|
|
|
|
|
|
If FRAME is the root Emacs window, or some other symbol, hide all notiboxes."
|
2023-08-12 13:46:48 -08:00
|
|
|
(let ((frame (notibox--resolve-frame frame)))
|
2024-02-24 18:36:12 -09:00
|
|
|
(if frame
|
2023-08-12 13:46:48 -08:00
|
|
|
(notibox--hide frame)
|
|
|
|
(notibox--hide (car notibox-current-posframes))
|
2023-08-12 16:35:12 -08:00
|
|
|
))
|
2023-08-12 13:46:48 -08:00
|
|
|
|
2023-03-30 01:00:22 -08:00
|
|
|
(pop notibox-current-posframes))
|
2023-08-11 15:44:11 -08:00
|
|
|
;; (frame-parent (selected-frame)) ;=> nil
|
2023-08-12 13:46:48 -08:00
|
|
|
;; (frame-live-p (car notibox-current-posframes))
|
|
|
|
;; (notibox-delete (car notibox-current-posframes))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2024-02-24 18:36:30 -09:00
|
|
|
(defun notibox--parse-message (msg)
|
|
|
|
"Given string MSG, return a cons of source and contents.
|
|
|
|
If the source is not obvious, use `current-buffer'."
|
|
|
|
(let* (source
|
|
|
|
contents
|
|
|
|
(splooted (split-string msg ": ")))
|
|
|
|
(pcase (length splooted)
|
2024-10-20 08:57:23 -08:00
|
|
|
;; problem: the echo area shows only cdr, whereas messages buffer shows the source fn we want..
|
2024-02-24 18:36:30 -09:00
|
|
|
(1 (progn
|
|
|
|
(setq source
|
|
|
|
(format "%s"
|
|
|
|
(current-buffer)))
|
|
|
|
(setq contents
|
|
|
|
(car splooted))))
|
2024-10-20 08:57:23 -08:00
|
|
|
(2 (progn
|
|
|
|
(setq source (car splooted))
|
|
|
|
(setq contents (cadr splooted))))
|
2024-11-15 12:22:50 -09:00
|
|
|
(3 (progn
|
|
|
|
(setq source (car splooted))
|
|
|
|
(setq contents (format "%s: %s" (cadr splooted) (caddr splooted)))))
|
2024-02-24 18:36:30 -09:00
|
|
|
)
|
2024-10-20 08:57:23 -08:00
|
|
|
;; (message "[debug] message: %s" msg)
|
2024-02-24 18:36:30 -09:00
|
|
|
(cons source contents)))
|
|
|
|
;; (split-string "evil-forward-character: End of line" ": ")
|
2024-10-20 08:57:23 -08:00
|
|
|
;; (length '("evil-forward-character" "end of line"))
|
2024-02-24 18:36:30 -09:00
|
|
|
;; (split-string "this is a valid message" ": ")
|
2024-11-15 12:22:50 -09:00
|
|
|
;; (message "evil-forward-char: End of line")
|
|
|
|
;; (message "%s" 'this\:\ is\ a\ \"badly\:\ formed\ message\")
|
2024-10-20 08:57:23 -08:00
|
|
|
(defun buflast (buffer)
|
|
|
|
"Return the last line of BUFFER as a string, without linebreaks."
|
|
|
|
(with-current-buffer buffer
|
|
|
|
(save-excursion
|
|
|
|
(goto-char (- (point-max) 1)) ; there is a \n at the end of every buffer
|
|
|
|
(buffer-substring-no-properties (point-at-bol) (point-max)))))
|
|
|
|
;; (buflast (get-buffer "*Messages*"))
|
2024-02-24 18:36:30 -09:00
|
|
|
|
2024-11-15 12:22:50 -09:00
|
|
|
(defvar last-message nil
|
|
|
|
"Previous value of `current-message', stored for notibox.")
|
2023-04-07 09:16:10 -08:00
|
|
|
(defun notibox--tail-echoarea ()
|
2023-08-06 18:29:26 -08:00
|
|
|
"Show `current-message' in the notibox. If that does not exist, probably hide it."
|
2023-04-07 09:16:10 -08:00
|
|
|
(if (current-message)
|
2024-11-15 12:22:50 -09:00
|
|
|
(unless (string= last-message (current-message))
|
|
|
|
(let* ((notibox-border-color "#0faa0f") ; hacky, can we set it elsewhere?
|
|
|
|
(parsed-msg
|
|
|
|
(notibox--parse-message
|
|
|
|
;; (current-message)
|
|
|
|
(buflast (get-buffer "*Messages*")) ; workaround for abbreviated stuff
|
|
|
|
))
|
|
|
|
(title (car parsed-msg))
|
|
|
|
(contents (cdr parsed-msg)))
|
|
|
|
(notibox-alert `( :title ,title
|
|
|
|
:message ,contents
|
|
|
|
)))
|
|
|
|
(setq last-message (current-message)))
|
|
|
|
;; (if notibox-current-posframes
|
|
|
|
;; (notibox-delete (car notibox-current-posframes)))
|
|
|
|
)
|
2024-02-24 18:36:12 -09:00
|
|
|
)
|
2023-04-07 09:16:10 -08:00
|
|
|
|
|
|
|
(defun notibox/setup-timer ()
|
2023-08-06 18:29:26 -08:00
|
|
|
"Start running notibox."
|
2023-04-07 09:16:10 -08:00
|
|
|
(interactive)
|
2023-04-21 21:42:09 -08:00
|
|
|
(run-with-timer notibox--refresh-delay notibox--refresh-delay #'notibox--tail-echoarea))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2023-08-11 19:39:28 -08:00
|
|
|
;; (notibox-delete 'anything)
|
2023-04-07 09:16:10 -08:00
|
|
|
(defun notibox-test-alert ()
|
2023-08-06 18:29:26 -08:00
|
|
|
"Show a sample notibox to prove we can."
|
2023-04-07 09:16:10 -08:00
|
|
|
(interactive)
|
2024-11-15 12:22:50 -09:00
|
|
|
(notibox-alert `(:title ,(random 8096) :message "test of notibox")))
|
|
|
|
;; (notibox-test-alert)
|
|
|
|
;; (notibox-alert '(:title "一" :message "二" :timeout 5))
|
|
|
|
;; (notibox-alert '(:title "三" :message "四" :timeout 5))
|
|
|
|
;; wanna watch the variable. how do we do that
|
|
|
|
(add-variable-watcher 'notibox-current-posframes
|
|
|
|
(lambda (symbol newval operation where)
|
|
|
|
"Display the variable in *watched* buffer."
|
|
|
|
(with-current-buffer (get-buffer-create "*watched*")
|
|
|
|
(goto-char (point-max))
|
|
|
|
(insert
|
|
|
|
(format "\n%s:\n was: %s\n now: %s"
|
|
|
|
symbol
|
|
|
|
(eval symbol)
|
|
|
|
newval)))
|
|
|
|
))
|
|
|
|
(--map (remove-variable-watcher 'notibox-current-posframes it)
|
|
|
|
(get-variable-watchers 'notibox-current-posframes))
|
2023-03-30 01:00:22 -08:00
|
|
|
|
2024-10-20 14:17:25 -08:00
|
|
|
;; begin frame moving experiments. thought we already did this...
|
|
|
|
(defun mir/move-frame (frame pos)
|
|
|
|
"Move FRAME to POS, a cons of (top . left)."
|
2024-11-15 12:22:50 -09:00
|
|
|
(if (frame-live-p frame)
|
|
|
|
(modify-frame-parameters frame
|
|
|
|
`(
|
|
|
|
(top . ,(car pos))
|
|
|
|
(left . ,(cdr pos))
|
|
|
|
))))
|
2024-10-20 14:17:25 -08:00
|
|
|
(defvar mir/framove-update-interval 0.05
|
|
|
|
"How many seconds to wait before updating incremental frame positions.")
|
|
|
|
|
|
|
|
(defun mir/framove-gradual (frame newpos duration)
|
|
|
|
"Move FRAME to NEWPOS ( a cons of top and left) over DURATION.
|
|
|
|
|
|
|
|
intermediate positions are calculated via `mir/framove-update-interval'."
|
|
|
|
(let* (
|
|
|
|
(how-many-steps (/ duration mir/framove-update-interval))
|
|
|
|
(updates-num-seq (number-sequence 1 how-many-steps))
|
|
|
|
(timestamps (--map (* mir/framove-update-interval it) updates-num-seq))
|
|
|
|
(beg-top (frame-parameter frame 'top))
|
|
|
|
(beg-left (frame-parameter frame 'left))
|
|
|
|
(end-top (car newpos))
|
|
|
|
(end-left (cdr newpos))
|
|
|
|
(top-diff (/ (- end-top beg-top) (float how-many-steps)))
|
|
|
|
(left-diff (/ (- end-left beg-left) (float how-many-steps)))
|
|
|
|
(tops (or (number-sequence beg-top end-top top-diff) 0))
|
|
|
|
(lefts (or (number-sequence beg-left end-left left-diff) 0))
|
|
|
|
(uhm
|
|
|
|
(--map (list
|
|
|
|
(or (elt timestamps it) duration)
|
|
|
|
(or (elt tops it) beg-top)
|
|
|
|
(or (elt lefts it) beg-left)
|
|
|
|
)
|
|
|
|
updates-num-seq
|
|
|
|
))
|
|
|
|
(timerfun (lambda (threelist)
|
|
|
|
(let ((timestamp (-first-item threelist))
|
|
|
|
(top (-second-item threelist))
|
|
|
|
(left (-third-item threelist)))
|
|
|
|
(if (and top left)
|
|
|
|
(run-with-timer timestamp nil ;no repeat
|
|
|
|
#'mir/move-frame
|
|
|
|
frame
|
|
|
|
(cons (round top) (round left))
|
|
|
|
)
|
|
|
|
;; (message "%s" threelist)
|
|
|
|
))))
|
|
|
|
)
|
|
|
|
(mapcar timerfun uhm)
|
|
|
|
)
|
|
|
|
nil
|
|
|
|
)
|
2024-11-15 12:22:50 -09:00
|
|
|
|
|
|
|
(defun mir/notibox-down-one (frame)
|
|
|
|
"Move FRAME down by `notibox-padding'."
|
|
|
|
(if (frame-live-p frame)
|
|
|
|
(let* ((parms (frame-parameters frame))
|
|
|
|
(old-y (alist-get 'top parms))
|
|
|
|
(old-x (alist-get 'left parms)))
|
|
|
|
(mir/framove-gradual frame (cons
|
|
|
|
(+ old-y ;; (- (* notibox-height
|
|
|
|
;; (window-text-height))
|
|
|
|
;; notibox-padding)
|
|
|
|
(/
|
|
|
|
(+ (* (window-text-height) notibox-height)
|
|
|
|
notibox-padding)
|
|
|
|
2))
|
|
|
|
old-x
|
|
|
|
) 0.125))))
|
|
|
|
;; (notibox-test-alert)
|
|
|
|
(defun notibox-test-message ()
|
|
|
|
"Send a message and see if it gets picked up by notibox."
|
|
|
|
(message "test message %s: %s is a number" (random 40) (random 500)))
|
|
|
|
;; (notibox-test-message)
|
|
|
|
|
|
|
|
(defun mir/push-notibox (frame)
|
|
|
|
"Push FRAME onto `notibox-current-posframes' and display it at the top of the stack."
|
|
|
|
(--map (mir/notibox-down-one it) ; move each by the interval - need more infra first
|
|
|
|
(reverse notibox-current-posframes))
|
|
|
|
(push frame notibox-current-posframes)
|
|
|
|
)
|
2024-10-20 14:17:25 -08:00
|
|
|
|
2023-03-30 01:00:22 -08:00
|
|
|
(provide 'notibox)
|
|
|
|
;;; notibox.el ends here
|