butterfly 和 tea-timer 接合

Message 的提示信息威力不够大,修改了一下 tea-timer 的代码,用 butterfly 来强制提醒休息,分享给大家。

;;; rest-timer.el --- Simple countdown timer      -*- lexical-binding: t; -*-

;; Copyright (C) 2018  Daniel Kraus
;;
;; Author: Daniel Kraus <[email protected]>
;; Version: 0.1
;; Package-Requires: ((emacs "24.4"))
;; Keywords: convenience, timer
;; URL: https://github.com/dakra/tea-timer.el

;; 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:

;; Simple countdown timer
;; TODO: Add modeline

;;; Code:


(defgroup rest-timer nil
  "rest-timer"
  :prefix "rest-timer-"
  :group 'convenience)

(defcustom rest-timer-default-duration 20
  "Default timer duration."
  :type 'integer
  :safe #'integerp
  :group 'rest-timer)

(defcustom rest-timer-message "Start to work."
  "Message to show when timer is up."
  :type 'string
  :group 'rest-timer)

(defvar rest-timer--timer nil
  "Store current running timer.")

(defun butterfly ()
  "Use butterflies to flip the desired bit on the drive platter.
Open hands and let the delicate wings flap once.  The disturbance
ripples outward, changing the flow of the eddy currents in the
upper atmosphere.  These cause momentary pockets of higher-pressure
air to form, which act as lenses that deflect incoming cosmic rays,
focusing them to strike the drive platter and flip the desired bit.
You can type `M-x butterfly C-M-c' to run it.  This is a permuted
variation of `C-x M-c M-butterfly' from url `http://xkcd.com/378/'."
  (interactive)
  (if t
      (progn
	(switch-to-buffer (get-buffer-create "*butterfly*"))
	(erase-buffer)
	(sit-for 0)
	(animate-string "Please have a rest"
			(/ (window-height) 2) (- (/ (window-width) 2) 12))
	(sit-for (* 5 (/ (abs (random)) (float most-positive-fixnum))))
	(message "Successfully flipped one bit!"))
    (message "Well, then go to xkcd.com!")
    (browse-url "http://xkcd.com/378/")))

(defun rest-timer-rest-ready ()
  "Display rest ready message and reset timer."
  (if (require 'alert nil 'no-error)
      (alert rest-timer-message)
    (butterfly))
  (setq rest-timer--timer nil))

;;;###autoload
(defun rest-timer (&optional duration)
  "Set a rest timer to DURATION in seconds or rest-timer-default-duration."
  (interactive "P")
  (when rest-timer--timer
    (when (y-or-n-p "Another rest timer already running.  Cancel the old one? ")
      (rest-timer-cancel)
      (setq rest-timer--timer nil)))
  (if rest-timer--timer
      (rest-timer-display-remaining-time)
    (message "Setting rest timer to %s minutes." (or duration rest-timer-default-duration))
    (setq rest-timer--timer
          (run-at-time (* 60 (or duration rest-timer-default-duration)) nil 'rest-timer-rest-ready))))

(defun rest-timer-display-remaining-time ()
  "Displays remaining time in the Minibuffer."
  (interactive)
  (if rest-timer--timer
      (let* ((remaining-time (decode-time (time-subtract (timer--time rest-timer--timer) (current-time)) t))
             (minutes (nth 1 remaining-time))
             (seconds (nth 0 remaining-time)))
        (message "%s minutes and %s seconds left" minutes seconds))
    (message "No rest timer active")))

(defun rest-timer-cancel ()
  "Cancel running rest timer."
  (interactive)
  (when rest-timer--timer
    (cancel-timer rest-timer--timer)
    (setq rest-timer--timer nil)))

(provide 'rest-timer)
;;; rest-timer.el ends here

6 个赞

报错 symbol’s function definition is void: string to int.

报错

(file-missing “Cannot open load file” “No such file or directory” “request”)

M-x package-list-packages 安装request就行了

这个是小白的问题

2 个赞

现在用的是 string-to-number

引起错误的原因是 你该更新某个包了

1 个赞

想把butterfly放到 org-pomodoro/org-pomodoro.el at master · marcinkoziej/org-pomodoro · GitHub 里。。。

抱歉啊,大家。我也是新手。

我把 request 这个包去掉了。

不知道原作者为什么加这个包,目前我能正常从 Emacs -q

直接:

(add-to-list 'load-path "/PATH/TO/rest-timer")
(require 'rest-timer)

就可以用了。

试试 Emacs -q 启动后来加载:

(add-to-list 'load-path "/PATH/TO/rest-timer")
(require 'rest-timer)

应该不难的,我还没有用过 pomodoro, 找到输出 Message 的点,然后用 butterfly 替换就好。

不知道rest-timer设定到时间之后为啥不切到butterfly。。。

看了看,如果timer结束的时候

如果有alert就调用alert通知该工作了?

没有alert就bufferfly通知该休息了?

这个逻辑是。。。

你怎么用的啊?

我这边只要拷贝到 *scratch* buffer, eval-buffer 然后

C-U 1 M-x rest-timer RET

也就是做1分钟的测试,在两个平台上都没问题啊。

GNU Emacs 26.3 (build 1, x86_64-w64-mingw32) of 2019-08-29

GNU Emacs 26.1 (build 1, x86_64-pc-linux-gnu) of 2018-06-29

参见我上面的回复。因为我这里有alert,所以就直接alert了一个该工作了,这逻辑不太对劲

可以试着先package-install 一个alert,然后运行看

 (defun rest-timer-rest-ready ()
  "Display rest ready message and reset timer."
  (if (require 'alert nil 'no-error)
      (alert rest-timer-message)
    (butterfly))
  (setq rest-timer--timer nil))```

感觉应该把if去掉。。。