Compare commits

..

2 Commits

View File

@ -4,7 +4,7 @@
;; Author: mir <mirmarq428@gmail.com> ;; Author: mir <mirmarq428@gmail.com>
;; Keywords:frames,convenience,help ;; Keywords:frames,convenience,help
;; Package-Requires: ((posframe) (alert)) ;; Package-Requires: ((posframe) (alert) (dash))
;; This file is NOT part of GNU Emacs. ;; This file is NOT part of GNU Emacs.
@ -31,6 +31,7 @@
(require 'posframe) (require 'posframe)
(require 'alert) (require 'alert)
(require 'dash)
(if (not (fboundp #'string-pixel-width)) (if (not (fboundp #'string-pixel-width))
(progn (progn
(require 'shr) ;for `shr-string-pixel-width' (require 'shr) ;for `shr-string-pixel-width'
@ -58,9 +59,10 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
(let* ( (let* (
;; need to not overlap the gtk scrollbar ;; need to not overlap the gtk scrollbar
(scrollbar-width (or (if (eq x-toolkit-scroll-bars 'gtk) (scrollbar-width (or (if (eq x-toolkit-scroll-bars 'gtk)
30) ; wild guess... 30) ; educated guess...
0)) 0))
(stackdepth (or stackdepth 0)) ;; (stackdepth (or stackdepth 0))
(stackdepth 0) ; we do not care actually
(parent-width (frame-pixel-width)) (parent-width (frame-pixel-width))
(child-width (* notibox-width (string-pixel-width " "))) (child-width (* notibox-width (string-pixel-width " ")))
(parent-height (frame-pixel-height)) (parent-height (frame-pixel-height))
@ -85,8 +87,8 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
;; (notibox--get-position 3) ;; (notibox--get-position 3)
(defun notibox--prepare-buffer (title body &optional buffer) (defun notibox--prepare-buffer (title body &optional buffer)
"Populate the `*notibox*' buffer (or BUFFER if specified) with TITLE and BODY properly formatted." "Return the notibox buffer containing TITLE and BODY, properly formatted"
(let ((buf (get-buffer-create (or buffer "*notibox*")))) (let ((buf (get-buffer-create (format " *notibox-- %s: %s *" title body))))
(with-current-buffer buf (with-current-buffer buf
(let ((inhibit-read-only t)) (let ((inhibit-read-only t))
(erase-buffer) (erase-buffer)
@ -94,7 +96,8 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
title title
(propertize (make-string notibox-width ?─) (propertize (make-string notibox-width ?─)
'face `((:foreground ,notibox-border-color))) 'face `((:foreground ,notibox-border-color)))
body)))))) body))))
buf))
;; (notibox--prepare-buffer "test" "this better work gadahgit" (or nil "*notibox-2*")) ;; (notibox--prepare-buffer "test" "this better work gadahgit" (or nil "*notibox-2*"))
;; (get-buffer-create "*notibox-2*") ;; (get-buffer-create "*notibox-2*")
;; wait, we need to be actually basing the things on the buffers huh ;; wait, we need to be actually basing the things on the buffers huh
@ -102,19 +105,26 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
(defvar notibox-current-posframes nil) (defvar notibox-current-posframes nil)
(cl-defun notibox--show (&key timeout &key depth &key buf) (cl-defun notibox--show (&key timeout &key depth &key buf)
"Show the notibox currently prepared, with optional TIMEOUT, at DEPTH, in BUF." "Show the notibox currently prepared, with optional TIMEOUT, at DEPTH, in BUF."
(add-to-list 'notibox-current-posframes (let ((this-notibox
(let ((buffer (get-buffer-create (or buf "*notibox*")))) (let ((buffer (get-buffer-create (or buf "*notibox*"))))
(posframe-show buffer (posframe-show buffer
:position (notibox--get-position depth) :position (notibox--get-position depth)
:left-fringe 0 :left-fringe 0
:right-fringe 0 :right-fringe 0
:max-width notibox-width :max-width notibox-width
:max-height notibox-height :max-height notibox-height
:min-width notibox-width :min-width notibox-width
:min-height notibox-height :min-height notibox-height
:border-width 2 :border-width 2
:border-color notibox-border-color :border-color notibox-border-color
:timeout timeout))) ;; :timeout timeout
))
))
(run-with-timer timeout nil #'notibox-delete this-notibox)
(mir/push-notibox this-notibox)
;; (add-to-list 'notibox-current-posframes
;; this-notibox)
)
nil) nil)
(defun notibox-alert (info) (defun notibox-alert (info)
@ -123,9 +133,10 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
(title (plist-get info :title)) (title (plist-get info :title))
(timeout (plist-get info :persistent)) (timeout (plist-get info :persistent))
(depth (plist-get info :depth))) (depth (plist-get info :depth)))
(notibox--prepare-buffer title message) ;; gotta make it move the others down...
(notibox--show :timeout (unless timeout alert-fade-time) (notibox--show :timeout (unless timeout alert-fade-time)
:depth (or depth (- (length notibox-current-posframes) 1))))) ;; :depth (or depth (- (length notibox-current-posframes) 1))
:buf (notibox--prepare-buffer title message))))
(defun notibox--resolve-frame (object) (defun notibox--resolve-frame (object)
"Return the /frame reference/ signified by OBJECT, whatever it may be." "Return the /frame reference/ signified by OBJECT, whatever it may be."
@ -143,7 +154,7 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
(defun notibox--hide (frame) (defun notibox--hide (frame)
"Stop showing FRAME." "Stop showing FRAME."
(if (frame-live-p frame) (if (frame-live-p frame)
(posframe-hide (window-buffer (frame-selected-window frame))))) (posframe-delete (window-buffer (frame-selected-window frame)))))
(defun notibox-delete (frame) (defun notibox-delete (frame)
"Delete the notibox FRAME. "Delete the notibox FRAME.
@ -177,14 +188,17 @@ If the source is not obvious, use `current-buffer'."
(2 (progn (2 (progn
(setq source (car splooted)) (setq source (car splooted))
(setq contents (cadr splooted)))) (setq contents (cadr splooted))))
(3 (progn
(setq source (car splooted))
(setq contents (format "%s: %s" (cadr splooted) (caddr splooted)))))
) )
;; (message "[debug] message: %s" msg) ;; (message "[debug] message: %s" msg)
(cons source contents))) (cons source contents)))
;; (split-string "evil-forward-character: End of line" ": ") ;; (split-string "evil-forward-character: End of line" ": ")
;; (length '("evil-forward-character" "end of line")) ;; (length '("evil-forward-character" "end of line"))
;; (split-string "this is a valid message" ": ") ;; (split-string "this is a valid message" ": ")
;; (notibox--parse-message "evil-forward-char: End of line") ;; (message "evil-forward-char: End of line")
;; (notibox--parse-message "this is a badly formed message") ;; (message "%s" 'this\:\ is\ a\ \"badly\:\ formed\ message\")
(defun buflast (buffer) (defun buflast (buffer)
"Return the last line of BUFFER as a string, without linebreaks." "Return the last line of BUFFER as a string, without linebreaks."
(with-current-buffer buffer (with-current-buffer buffer
@ -193,22 +207,27 @@ If the source is not obvious, use `current-buffer'."
(buffer-substring-no-properties (point-at-bol) (point-max))))) (buffer-substring-no-properties (point-at-bol) (point-max)))))
;; (buflast (get-buffer "*Messages*")) ;; (buflast (get-buffer "*Messages*"))
(defvar last-message nil
"Previous value of `current-message', stored for notibox.")
(defun notibox--tail-echoarea () (defun notibox--tail-echoarea ()
"Show `current-message' in the notibox. If that does not exist, probably hide it." "Show `current-message' in the notibox. If that does not exist, probably hide it."
(if (current-message) (if (current-message)
(let* ((notibox-border-color "#0faa0f") ; hacky, can we set it elsewhere? (unless (string= last-message (current-message))
(parsed-msg (let* ((notibox-border-color "#0faa0f") ; hacky, can we set it elsewhere?
(notibox--parse-message (parsed-msg
;; (current-message) (notibox--parse-message
(buflast (get-buffer "*Messages*")) ; workaround for abbreviated stuff ;; (current-message)
)) (buflast (get-buffer "*Messages*")) ; workaround for abbreviated stuff
(title (car parsed-msg)) ))
(contents (cdr parsed-msg))) (title (car parsed-msg))
(notibox-alert `( :title ,title (contents (cdr parsed-msg)))
:message ,contents (notibox-alert `( :title ,title
:depth 0))) :message ,contents
(if notibox-current-posframes )))
(notibox-delete (car notibox-current-posframes)))) (setq last-message (current-message)))
;; (if notibox-current-posframes
;; (notibox-delete (car notibox-current-posframes)))
)
) )
(defun notibox/setup-timer () (defun notibox/setup-timer ()
@ -220,23 +239,34 @@ If the source is not obvious, use `current-buffer'."
(defun notibox-test-alert () (defun notibox-test-alert ()
"Show a sample notibox to prove we can." "Show a sample notibox to prove we can."
(interactive) (interactive)
(notibox-alert '(:title "five" :message "six"))) (notibox-alert `(:title ,(random 8096) :message "test of notibox")))
;; (notibox-alert '(:title "一" :message "二" :timeout 5 :depth 0)) ;; (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))
;; begin frame moving experiments. thought we already did this... ;; begin frame moving experiments. thought we already did this...
(setq test-frame (posframe-show "*Messages*"
:position '(800 . 50)
:width 30
:height 16))
(defun mir/move-frame (frame pos) (defun mir/move-frame (frame pos)
"Move FRAME to POS, a cons of (top . left)." "Move FRAME to POS, a cons of (top . left)."
(modify-frame-parameters frame (if (frame-live-p frame)
`( (modify-frame-parameters frame
(top . ,(car pos)) `(
(left . ,(cdr pos)) (top . ,(car pos))
))) (left . ,(cdr pos))
;; (frame-parameters test-frame) ))))
;; (mir/move-frame test-frame '(300 . 1200))
(defvar mir/framove-update-interval 0.05 (defvar mir/framove-update-interval 0.05
"How many seconds to wait before updating incremental frame positions.") "How many seconds to wait before updating incremental frame positions.")
@ -281,8 +311,35 @@ intermediate positions are calculated via `mir/framove-update-interval'."
) )
nil nil
) )
;; (mir/framove-gradual test-frame '(500 . 1400) 1)
;; (mir/move-frame test-frame '(500 . 500)) (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)
)
(provide 'notibox) (provide 'notibox)
;;; notibox.el ends here ;;; notibox.el ends here