Compare commits

..

No commits in common. "fbe13a46b11e0e7c99d8f82eb246a3305cabad53" and "2d89cc30995ed500c636fee1d31a2c816989d807" have entirely different histories.

View File

@ -1,8 +1,8 @@
;;; notibox.el --- PosFrame-based notification UI -*- lexical-binding: t; -*-
;; Copyright (C) 2023 mir
;; Copyright (C) 2023 mitch
;; Author: mir <mirmarq428@gmail.com>
;; Author: mitch <mitch@mitchmarq42.xyz>
;; Keywords:frames,convenience,help
;; Package-Requires: ((posframe) (alert))
@ -31,10 +31,7 @@
(require 'posframe)
(require 'alert)
(if (not (fboundp #'string-pixel-width))
(progn
(require 'shr) ;for `shr-string-pixel-width'
(defalias #'string-pixel-width #'shr-string-pixel-width)))
(defvar notibox-width 40) ; characters
(defvar notibox-height 4) ; characters
@ -58,7 +55,7 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
(let* (
(stackdepth (or stackdepth 0))
(parent-width (frame-pixel-width))
(child-width (* notibox-width (string-pixel-width " ")))
(child-width (* notibox-width (shr-string-pixel-width " ")))
(parent-height (frame-pixel-height))
(child-height (* notibox-height (line-pixel-height)))
(padded-height (+ child-height notibox-padding))
@ -77,18 +74,17 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
)
(cons x-justify y-coord)
))
;; (notibox--get-position 3)
;; (notibox--get-position)
(defun notibox--prepare-buffer (title body)
"Populate the `*notibox*' buffer with TITLE and BODY properly formatted."
(with-current-buffer (get-buffer-create "*notibox*")
(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)))
body)))))
body))))
;; (notibox--prepare-buffer "test" "this better work gadangit")
(defvar notibox-current-posframes nil)
@ -118,89 +114,28 @@ If STACKDEPTH is non-nil and nonzero, return a position that far down."
(notibox--show :timeout (unless timeout alert-fade-time)
:depth (or depth (- (length notibox-current-posframes) 1)))))
(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*")
(defun notibox--hide (frame)
"Stop showing FRAME."
(if (frame-live-p frame)
(posframe-hide (window-buffer (frame-selected-window frame)))))
(posframe-hide (window-buffer (frame-selected-window frame))))
(defun notibox-delete (frame)
"Delete the notibox FRAME.
If FRAME is the root Emacs window, or some other symbol, hide all notiboxes."
(let ((frame (notibox--resolve-frame frame)))
(if frame
(notibox--hide frame)
(notibox--hide (car notibox-current-posframes))
))
(if (and (framep frame) (not (frame-parent frame)))
(delete-frame frame)
(notibox--hide (car notibox-current-posframes)))
(pop notibox-current-posframes))
;; (frame-parent (selected-frame)) ;=> nil
;; (frame-live-p (car notibox-current-posframes))
;; (notibox-delete (car notibox-current-posframes))
(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)
;; problem: the echo area shows only cdr, whereas messages buffer shows the source fn we want..
(1 (progn
(setq source
(format "%s"
(current-buffer)))
(setq contents
(car splooted))))
(2 (progn
(setq source (car splooted))
(setq contents (cadr splooted))))
)
;; (message "[debug] message: %s" msg)
(cons source contents)))
;; (split-string "evil-forward-character: End of line" ": ")
;; (length '("evil-forward-character" "end of line"))
;; (split-string "this is a valid message" ": ")
;; (notibox--parse-message "evil-forward-char: End of line")
;; (notibox--parse-message "this is a badly formed message")
(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*"))
(defun notibox--tail-echoarea ()
"Show `current-message' in the notibox. If that does not exist, probably hide it."
(if (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
:depth 0)))
(notibox-alert `(
:title ,(format "%s" (current-buffer))
:message ,(current-message)))
(if notibox-current-posframes
(notibox-delete (car notibox-current-posframes))))
)
(notibox-delete 'current))))
(defun notibox/setup-timer ()
"Start running notibox."
@ -212,7 +147,7 @@ If the source is not obvious, use `current-buffer'."
"Show a sample notibox to prove we can."
(interactive)
(notibox-alert '(:title "five" :message "six")))
;; (notibox-alert '(:title "一" :message "二" :timeout 5 :depth 0))
;; (notibox-alert '(:title "一" :message "二" :timeout nil :depth 1))
(provide 'notibox)
;;; notibox.el ends here