;;; -*- Mode: Lisp; Last modified: <2008/01/03 22:36:03> -*-
;;;
;;; This file is not part of xyzzy.
;;;
;;;   ese-fortune.l --- dubious fortune mode
;;;
;;;     by HIE Masahiro <madoinu@ybb.ne.jp>
#|

仭奣梫

  婲摦帪偵儕僼傽儗儞僗偲偐丆愯偄偲偐丆TODO傪儔儞僟儉偵昞帵偟傑偡丅

  TKI 偝傫偺 fortune-yz 傪傕偲偵偮偔偭偨傕偺偱偡丅

    http://www2.ocn.ne.jp/~cheerful/script/xyzzy/play/startword.html


仭僀儞僗僩乕儖

  1. reference.txt 傪庢摼偟丆etc 僼僅儖僟偵僐僺乕偡傞丅

     NetInstaller 偐傜僀儞僗僩乕儖偟偨応崌偼丆etc 僼僅儖僟偵抲偐傟傑偡丅

  2. ese-fortune.l 傪 site-lisp 偵僐僺乕偡傞丅

  3. 昁梫偵墳偠丆僶僀僩僐儞僷僀儖偡傞丅(擟堄)

       M-x byte-compile-file

  4. .xyzzy 偐 siteinit.l 偵埲壓偺婰弎傪捛壛偡傞丅

       (require "ese-fortune")

  5. xyzzy 傪嵞婲摦偡傞偲丆xyzzy Reference 偑昞帵偝傟傞傛偆偵側傝傑偡丅


仭巊偄曽

    a : 帺摦峏怴偺僩僌儖
    i : 峏怴娫妘(昩)傪曄峏
    n : 師儁乕僕
    j : 巜掕 No 偵堏摦
    q : 廔椆
    . : 廔椆


仭愝掕椺

  ;;; 婲摦帪偐傜傏偆偭偲尒傞丅(帺摦峏怴儌乕僪)
  (setq *ese-fortune-absent* t)

  ;;; 傏偆偭偲尒傜傟偰傞偲偒偵丆僽儖僽儖偡傞丅
  ;;;   TKI 偝傫偺丆shakeWindow 傪捀偒丆撉傒崬傫偱偍偔丅
  ;;;   http://www2.ocn.ne.jp/~cheerful/script/xyzzy/mode/shakeWindow.html
  (require "shakeWindow")

  ;;; 僶僢僼傽僞僽傪旕昞帵偡傞丅
  (setq *ese-fortune-buffer* " *ese-fortune*")

  ;;; 娭楢偡傞僉乕儚乕僪傪 F1 偱 info-modoki-mode 偐傜堷偔丅
  ;;; 乮info-modoki-mode 偑昁梫偱偡乯
  (define-key *ese-fortune-mode-map* '#\F1 'imm-refer-selection)

  ;;; 偦偺懠丆僇僗僞儅僀僘梡曄悢傪嶲徠偟偰揔摉偵丅


仭僨乕僞宍幃

  ese-fortune 梡偺僨乕僞偼丆1峴傪1審偲偟偰埖偄傑偡丅夵峴僐乕僪偼TAB偱昞
  尰偟傑偡丅僒儞僾儖偼丆埲壓偺偲偍傝丅([  TAB  ]偼丆TAB暥帤傪偁傜傢偡)

    1|奿尵偱偡丅[  TAB  ]    劅劅 尃暫塹
    2|偹傓偔偰偍偒傟傑偣傫丅侾揰
    3|壞媥傒偺偒傑傝偛偲[  TAB  ]  * 憗怮[  TAB  ]  * 憗婲偒[  TAB  ]  * 帟杹偒!?


仭僨乕僞僼傽僀儖偺愝掕椺

  xyzzy Reference 埲奜偺僨乕僞傪昞帵偝偣傞応崌偼丆僨乕僞僼傽僀儖偺巜掕傪
  峴偄傑偡丅僨乕僞僼傽僀儖傪巜掕偡傞偵偼丆埲壓偺曽朄偑偁傝傑偡丅

  # 僨乕僞僼傽僀儖偼丆偳偙偐偐傜僥僉僩乕偵扵偟傑偡丅

    1. 僨乕僞僼傽僀儖傪巜掕偡傞丅

           (setq *ese-fortune-dat* "tentori.data")

    2. 僨乕僞僼僅儖僟傪巜掕偡傞

       *ese-fortune-dat* 偵僼僅儖僟傪巜掕偡傞偲丆僼僅儖僟偵懚嵼偡傞僼傽
       僀儖偡傋偰傪僨乕僞僼傽僀儖偲偟偰擣幆偟丆儔儞僟儉偵慖戰偟傑偡丅

           (setq *ese-fortune-dat* "F:/foo/bar/")

    3. 暋悢偺僼傽僀儖傪巜掕偡傞丅

       巊梡偡傞僨乕僞僼傽僀儖傪儔儞僟儉偵慖戰偟傑偡丅
       *ese-fortune-dat-list* 傪巜掕偟偨応崌丆*ese-fortune-dat* 偵桪愭偟
       傑偡丅

           (setq *ese-fortune-dat-list*
                 '("reference.dat"
                   "tentori.data"
                   ))

    4. etc 僼僅儖僟埲奜偺僼傽僀儖傪巜掕偡傞丅

      *ese-fortune-dat* 偵僼儖僷僗偱僼傽僀儖巜掕偟傑偡丅

           (setq *ese-fortune-dat* "d:/foo/bar.dat")

      暋悢僼傽僀儖傪巜掕偡傞応崌丅

           (setq *ese-fortune-dat-list*
                 '("d:/foo/bar.dat"
                   "piyo.txt"
                   ))


仭棙梡壜擻側僨乕僞僼傽僀儖

  [揰偲傝愯偄]

    Taro's Page [Let's Enjoy Tentori Fortune!] 偐傜丆tentori.lzh 傪捀偒丆
    偍尵梩僨乕僞(tentori.data) 傪etc 僼僅儖僟偵僐僺乕偟傑偡丅

      http://taro.haun.org/tenjoy.html

    揰偲傝愯偄僨乕僞傪巊偆応崌偼丆僨乕僞僼傽僀儖偺巜掕偑昁梫偱偡丅

      (setq *ese-fortune-dat* "tentori.data")

  [(BSD嶻偺) fortune 梡僨乕僞]

    埲壓偺庤弴偱丆ese-fortune 梡偺僼傽僀儖偵曄姺偟偰巊梡偡傞偙偲偑偱偒傑偡丅

      1. fortune 梡僨乕僞僼傽僀儖傪弨旛偡傞
      2. F12 僉乕傪墴偡
      3. 曄姺尦僼傽僀儖(fortune 梡僨乕僞僼傽僀儖)傪巜掕偡傞
      4. 曄姺愭僼傽僀儖(ese-fortune 梡僨乕僞僼傽僀儖)傪巜掕偡傞
      5. 僨乕僞僼傽僀儖傪巜掕偡傞

           (setq *ese-fortune-dat* "hoge.dat")


仭 ese-fortune 梡偺僨乕僞宍幃偱側偄僨乕僞僼傽僀儖傪捈愙巊偆

    奼挘巕傪".dats"偲偟偨僼傽僀儖傪巜掕偡傞偲丆埲壓偺傛偆側宍幃偺僼傽僀
    儖傪偦偺傑傑巊梡偡傞偙偲偑偱偒傑偡丅

    [hoge.dats]
      1|B&D 偱丆僩僀儗僢僩儁乕僷乕傪攦偆丅
      2|
      3|  怓偼僺儞僋丅
      4|
      5|%
      6|儅僉儘儞傪僩僀儗偵偍偄偰偍偔丅
      7|
      8|%
      9|傂偊偝傫丅晄嬶崌偱偡丅 侽揰

    TODO 儕僗僩偺傛偆偵丆曄峏傪懄斀塮偝偣偨偄傛偆側応崌偼丆偙偺宍幃傪棙
    梡偡傞偲傛偄偱偟傚偆丅扐偟丆幚峴偡傞搒搙僨乕僞曄姺傪峴偆偨傔丆張棟偼
    廳偔側傝傑偡丅


仭峏怴棜楌

  [Version 1.09.1] 2008-01-03 (悈)
  丒absent-mode ON 偺傑傑僼傽僀儔傪奐偔偲丆僼傽僀儔偐傜僼僅乕僇僗偑奜傟偰
    偟傑偆偨傔丆僥僉僩僂側懳張傪偟偨丅

  [Version 1.09] 2007-12-31 (寧)
  丒儔僀僙儞僗忦崁(MIT儔僀僙儞僗)傪婰嵹丅
  丒reference.txt 偐傜 reference.dat 傪昁梫偵墳偠帺摦嶌惉偡傞傛偆偵偟偨丅
    仺 弶夞幚峴帪偲丆峏怴帪丅
  丒夵峴擣幆暥帤楍婛掕抣(*ese-fortune-br*)傪僞僽暥帤偵曄峏偟偨丅
  丒xyzzy Part7 657 偝傫偺採埬傪庢傝崬傒丆暋悢偺僨乕僞僼傽僀儖傪儔儞僟儉
    偵昞帵偱偒傞傛偆偵偟偨(崱崰)丅m(__)m
  丒ese-fortune 梡偺僨乕僞僼傽僀儖偱側偔偰傕丆幚峴帪偵曄姺偟偰尒傞偙偲傕
    偱偒傞傛偆偵偟偨丅
  丒僨乕僞僼僅儖僟傪巜掕偱偒傞傛偆偵偟偨丅
  丒info-modoki-mode 偱堷偒傗偡偄傛偆偵丆僔儞僞僢僋僗傪掕媊偟偨丅
  丒ese-fortune 梡僶僢僼傽傪丆昞帵偡傞傛偆偵偟偨丅
  丒嵞幚峴帪偵丆僶僢僼傽傪嶌惉偟側偍偝側偄傛偆偵偟偨丅
  丒傏偆偭偲尒傟傞傛偆偵偟偨丅乮傆偐偊偝傫偺傾僀僨傿傾乯
  丒TKI 偝傫偺 shakeWindow 傪撉傒崬傫偱偍偔偲丆傏偆偭偲尒偰傞偲偒偵
    僽儖僽儖偡傞傛偆偵偟偨丅

  [Version 1.08.3] 2003/07/06 (擔)
  丒杮懱偼 site-lisp 偵丆reference.dat丂偼 etc 偵抲偔傛偆偵偟偨丅
  丒dat 僼傽僀儖偼丆摨崼偟側偄傛偆偵偟偨丅
  丒偁傑傝巊傢側偝偦偆側僉乕僶僀儞僪傪嶍彍偟偨丅

  [Version 1.08.2] 2003/05/05 (寧)
  丒reference.dat 傪 reference.xml 偐傜捈愙嶌惉偡傞傛偆偵偟偨丅傑偨丄
    昞帵偟偰傕偁傑傝偆傟偟偔側偝偘側乽栚揑暿嶕堷乿偲偐乽娭悢堦棗乿
    偼僨乕僞偵娷傔側偄傛偆偵偟偨丅偙傟偵傛傝丄ref2fortune 偼嶍彍偟偨丅
  丒reference.dat 傪 reference.xml 2003.04.30 斉偐傜偮偔偭偨傕偺偵
    偟偨丅
  丒refernce.dat 偺傒傪摨崼偡傞傛偆偵偟偨丅

  [Version 1.08.1] 2003/04/19 (搚)
  丒mixed.dat 傪摨崼偟側偄傛偆偵偟偨丅偐傢傝偵嶌惉曽朄傪捛壛偟偰偍
    偄偨丅m(__)m

  [Version 1.08] 2003/04/06 (擔)
  丒.xyzzy 偵 (ese-fortune) 偲偐彂偐側偔偰傕偄偄傛偆偵偟偨丅
  丒xyzzy 婲摦帪偺 ese-fortune 幚峴帪揰偱丄*scratch*, 儈僯僶僢僼傽
    埲奜偺僶僢僼傽偑懚嵼偡傞応崌偼丄婲摦偟側偄傛偆偵偟偨乮偮傕傝乯丅
  丒摨崼偺 reference.dat 傪 reference.xml 2003.03.30 斉偐傜偮偔偭偨
    傕偺偵偟偨丅

  [Version 1.07] 2003/02/18 (壩)
  丒user 僷僢働乕僕偵堏偟偨丅乮偩偗丅乯

  [Version 1.06] 2002/12/06 (嬥)
  丒僨乕僞僼傽僀儖枛旜偺嬻敀峴傑偱慖戰偟偰偟傑偭偰偄偨偺傪廋惓偟偨丅
  丒*ese-fortune-title* 傪捛壛偟偨丅
  丒export 偡傞傕偺傪偄偔偮偐捛壛偟偨丅
  丒愝掕椺偲偐傪尒捈偟偨丅

  [Version 1.05] 2002/11/29 (嬥)
  丒xyzzy 傪 SendTo 偲偐偐傜丄僼傽僀儖柤傪巜掕偟偰婲摦偟偨応崌丄
    ese-fortune 傪幚峴偟側偄傛偆偵偟偰偄偨偮傕傝偑丄偦偆側偭偰
    側偐偭偨偺傪廋惓偟偨偮傕傝丅
  丒偦偺懠丄偄傜側偝偦偆側偲偙傠傪彮偟嶍偭偨丅
  丒偱傕丄偐偤傪傂偄偰偄傞偺偱丄旲悈偑偮偄偰偄傞偐傕偟傟側偄丅侾揰

  [Version 1.04] 2002/11/26 (壩)
  丒*ese-fortune-br* 偱夵峴擣幆暥帤楍傪巜掕偱偒傞傛偆偵偟偨丅
  丒mixed.dat 傪偮偔偭偰偍偄偨丅

  [Version 1.03] 2002/11/25(寧)
  丒ref2fortune 傪廋惓偟偨丅仺 *.dat 傪偡傋偰嶌惉偟側偍偟偨丅
    丒傕偲偺 reference 偵 \n 婰弎偝傟偰偄傞応崌偵丄夵峴偝傟偰偟傑偭偰偄偨偲偙傠丅
    丒乽仭栚揑暿嶕堷乿偲偐偑丄傑偲傑偭偰偟傑偭偰偄偨偲偙傠丅
  丒巜掕峴偵 jump 傪偮偔偭偨丅乮偳偪傜偐偲偄偆偲巹偺妋擣梡乯

  [Version 1.02] 2002/11/24(擔)
  丒ref2fortune 傪廋惓偟偨丅
    丒峴摢偺儂儚僀僩僗儁乕僗偑嶍彍偝傟偰偄偨偲偙傠丅
  丒摨崼偺 reference.dat 傪 reference.xml 2002.11.23斉 偐傜嶌傝捈偟偨丅
  丒傂偲偮偺僨傿儗僋僩儕偵傑偲傔偨丅
  丒偦偺懠嵶偐偄廋惓丅

  [Version 1.01.1] 2002/11/22(嬥)
  丒偦偭偲嵎偟懼偊丅

  [Version 1.01] 2002/11/22(嬥)
  丒random 娭悢偵丄擇偮栚偺堷悢傪梌偊偨丅嵅栰嫥弐偝傫偵姶幱丅

  [Version 1.00] 2002/11/21(栘)
  丒偲傝偁偊偢岞奐丅


仭儔僀僙儞僗

  ese-fortune.l 偼MIT儔僀僙儞僗偵婎偯偄偰棙梡壜擻偱偡丅
  <http://www.opensource.org/licenses/mit-license.php>

Copyright (c) 2002-2007 HIE Masahiro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

|#

(provide "ese-fortune")

(defconstant *ese-fortune-version* "1.09")

;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 曄悢愝掕
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defvar *ese-fortune-enable* t
  "ese-fortune: 婲摦帪偵 ese-fortune 傪幚峴")

(defvar *ese-fortune-dat* "reference.dat"
  "ese-fortune: 僨乕僞僼傽僀儖")

(defvar *ese-fortune-dat-list* nil
  "ese-fortune: 僨乕僞僼傽僀儖偺儕僗僩")

(defvar *ese-fortune-br* "\t"
  "ese-fortune: 夵峴擣幆暥帤楍")

(defvar *ese-fortune-buffer* "*ese-fortune*"
  "ese-fortune: 僨乕僞昞帵梡僶僢僼傽柤")

(defvar *ese-fortune-src-type* "dats"
  "ese-fortune: 幚峴帪偵僨乕僞曄姺傪峴偆僼傽僀儖偺奼挘巕")

(defvar *ese-fortune-dat-separator* "^%\n"
  "ese-fortune: 僨乕僞帺摦曄姺帪嬫愗傝暥帤楍(惓婯昞尰)
* 偙偺惓婯昞尰偵儅僢僠偡傞晹暘偑夵峴(\n)偵曄姺偝傟傞丅
* 偙偺扨埵偑僨乕僞 1 審偲傒側偝傟傞丅")

(defvar *ese-fortune-absent* nil
  "ese-fortune: 婲摦帪偐傜傏偆偭偲尒傞")

(defvar *ese-fortune-absent-interval* 10
  "ese-fortune: 峏怴娫妘(昩)")

(defvar *ese-fortune-reference-update* t
  "ese-fortune: reference.txt 峏怴帪偵 reference.dat 傪帺摦峏怴偡傞")

(defvar *ese-fortune-title*
  " ese-fortune ver.~A --- ~A [No.~5,'0D]  a: absent, i: interval, n: next, j: jump, q: quit~%~%"
  "ese-fortune: 僞僀僩儖暥帤楍")

(defvar *ese-fortune-tmp-buffer* "*ese-fortune-tmp*"
  "ese-fortune: 堦帪僶僢僼傽")

(defvar *ese-fortune-open-filer-func* #'open-filer
  "ese-fortune: 乽僼傽僀儔傪奐偔乿僐儅儞僪")

(defvar *ese-fortune-random-state* nil)


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 懳榖揑側娭悢
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defun ese-fortune (&optional number file)
  "ese-fortune: 幚峴"
  (interactive)
  ;; *ese-fortune-dat* 偑 nil 側傜廔椆
  (unless *ese-fortune-dat*
    (return-from ese-fortune nil))
  ;; 僨乕僞僼傽僀儖偵 reference.dat 偑巜掕偝傟偰偄傞応崌偺張棟
  (when (string= *ese-fortune-dat* "reference.dat")
    (let ((dat (merge-pathnames "reference.dat" (etc-path)))
	  (txt (merge-pathnames "reference.txt" (etc-path))))
      ;; reference.dat 偑側偄 or 屆偄応崌偵嶌惉/峏怴
      (if (file-exist-p txt)
	  (when (or (not (file-exist-p dat))
		    (and *ese-fortune-reference-update*
			 (> (file-write-time txt) (file-write-time dat))))
	    (ese-fortune-reftxt2dat))
	;; txt 傕 dat 傕側偗傟偽廔椆
	(return-from ese-fortune nil)
	)))
  ;; 僨乕僞僼傽僀儖偺慖戰
  (let (dat fbuf tbuf blines num msg)
    (if file
	;; 僨乕僞僼傽僀儖偑堷悢巜掕偝傟偰偄傟偽桪愭揑偵巊梡偡傞
	(setq dat file)
      (progn
	(let (files)
	  ;; 僼傽僀儖儕僗僩傪庢摼
	  (setq files (if (file-directory-p *ese-fortune-dat*)
			  (directory *ese-fortune-dat*
				     :absolute t
				     :wild "*"
				     :file-only t)
			*ese-fortune-dat-list*))
	  ;; 僼傽僀儖儕僗僩偑懚嵼偡傟偽丆僼傽僀儖傪儔儞僟儉偵慖戰
	  (if files
	      (let ((len (list-length files)))
		(setq dat (nth (random len *ese-fortune-random-state*) files)))
	    ;; 僼傽僀儖儕僗僩偑側偗傟偽 *ese-fortune-dat* 傪僨乕僞僼傽僀儖偲偡傞
	    (setq dat *ese-fortune-dat*)))
	;; 僼傽僀儖柤偺傒巜掕偺応崌偼(etc-path)傪晅壛
	(when (string= (file-namestring dat) dat)
	  (setq dat (merge-pathnames dat (etc-path))))))
    ;; ese-fortune 僶僢僼傽偺宍惉
    (if (setq fbuf (find-buffer *ese-fortune-buffer*))
	(progn
	  (set-buffer fbuf)
	  (setq buffer-read-only nil)
	  (delete-region (point-min) (point-max)))
      (progn
	(setq fbuf (create-new-buffer *ese-fortune-buffer*))
	(set-buffer fbuf)
	(ese-fortune-mode *ese-fortune-absent*)))
    ;; 僨乕僞僼傽僀儖偐傜崱夞昞帵偡傞僨乕僞傪庢摼
    (when (file-exist-p dat)
      ;; 嶌嬈梡僶僢僼傽偲僨乕僞偺弨旛
      (setq tbuf (create-new-buffer *ese-fortune-tmp-buffer*))
      (set-buffer tbuf)
      (insert-file-contents dat)
      ;; 昁梫偵墳偠奼挘巕偺応崌偼僨乕僞曄姺張棟傪峴偆
      (when (equal *ese-fortune-src-type*
		   (pathname-type dat))
	(ese-fortune-make-dat *ese-fortune-dat-separator*))
      (setq blines (1- (buffer-lines)))
      (goto-char (point-min))
      ;; 昞帵偡傞僨乕僞偺峴悢傪儔儞僟儉偵慖戰
      (if number
	  (if (or (< number 1)
		  (> number blines))
	      (setq num -1)
	    (setq num (1- number)))
	(setq num (random blines *ese-fortune-random-state*)))
      ;; 巜掕峴偺僨乕僞傪庢摼
      (if (= num -1)
	  (setq msg (format nil "1 乣 ~D 傑偱偟偐側偄傛丅" blines))
	(progn
	  (forward-line num)
	  (setq msg (buffer-substring
		     (progn (goto-bol)(point))
		     (progn (goto-eol)(point))))))
      (setq msg (substitute-string msg *ese-fortune-br* "\n" ))
      (delete-buffer tbuf))
    ;; 僨乕僞傪昞帵偡傞
    (set-buffer fbuf)
    (insert (format nil *ese-fortune-title*
		    *ese-fortune-version*
		    (file-namestring dat)
		    (1+ (or num -1))))
    (unless num (insert "僨乕僞偑偁傝傑偣傫丅侽揰"))
    (when msg (insert msg))
    ;; 尰嵼偺僨乕僞僼傽僀儖柤傪婰壇偟偰偍偔
    (setq *ese-fortune-dat-current* dat))
  ;; 僶僢僼傽偺忬懺傪惍偊傞
  (goto-char (point-min))
  (setq buffer-read-only t)
  (set-buffer-modified-p nil)
  )

(defun ese-fortune-jump (num)
  "ese-fortune: 昞帵拞偺僨乕僞僼傽僀儖巜掕峴偵堏摦"
  (interactive "nNo: ")
  (ese-fortune num *ese-fortune-dat-current*))

(defun ese-fortune-open-filer ()
  "ese-fortune: absent-mode 傪廔椆偝偣僼傽僀儔傪奐偔"
  (interactive)
  (ese-fortune-absent-mode nil)
  (funcall *ese-fortune-open-filer-func*))

(defun ese-fortune-quit ()
  "ese-fortune: 偍傢傞"
  (interactive)
  (ese-fortune-absent-mode nil)
  (delete-buffer (selected-buffer)))


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 ese-fortune-mode
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defvar *ese-fortune-mode-hook* nil)

(defvar *ese-fortune-mode-map* nil)
(unless *ese-fortune-mode-map*
  (setq *ese-fortune-mode-map* (make-sparse-keymap))
  (define-key *ese-fortune-mode-map* '#\a 'ese-fortune-absent-mode)
  (define-key *ese-fortune-mode-map* '#\i 'ese-fortune-set-absent-interval)
  (define-key *ese-fortune-mode-map* '#\n 'ese-fortune)
  (define-key *ese-fortune-mode-map* '#\j 'ese-fortune-jump)
  (define-key *ese-fortune-mode-map* '#\q 'ese-fortune-quit)
  (define-key *ese-fortune-mode-map* '#\. 'ese-fortune-quit)
  (define-key *ese-fortune-mode-map* '#\F12 'ese-fortune-fortune2dat)
  (define-key *ese-fortune-mode-map* '(#\C-c #\C-f) 'ese-fortune-open-filer)
  )

(defvar *ese-fortune-mode-syntax-table* nil)
(unless *ese-fortune-mode-syntax-table*
  (setq *ese-fortune-mode-syntax-table* (make-syntax-table))
  (set-syntax-symbol *ese-fortune-mode-syntax-table*  #\-)
  (set-syntax-symbol *ese-fortune-mode-syntax-table*  #\*)
  )

(defun ese-fortune-mode(&optional absent)
  (interactive)
  (kill-all-local-variables)
  (setf buffer-mode 'ese-fortune-mode)
  (setq mode-name "ese-fortune")
  (use-keymap *ese-fortune-mode-map*)
  (use-syntax-table *ese-fortune-mode-syntax-table*)
  (setq need-not-save t)
  (setq kept-undo-information nil)
  (setq auto-save nil)
  (setq *ese-fortune-random-state* (make-random-state t))
  (let ((buf (selected-buffer)))
    (set-local-window-flags buf (+ *window-flag-newline*
				   *window-flag-tab*
				   *window-flag-full-width-space*
				   *window-flag-eof*
				   *window-flag-cursor-line*
				   *window-flag-line-number*
				   *window-flag-half-width-space*
				   *window-flag-scrolling*)
			    nil)
    (set-local-window-flags buf (+ (if (boundp '*window-flag-fold-mark*)
				       *window-flag-fold-mark*
				     *window-flag-fold-line*))
			    t))
  (when absent
    (ese-fortune-absent-mode t))
  (run-hooks '*ese-fortune-mode-hook*))


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 婲摦張棟
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

;;; xyzzy 婲摦帪偵 ese-fortune 傪幚峴
(add-hook '*post-startup-hook* #'(lambda ()
				   (when (and *ese-fortune-enable*
					      ;; 堷悢偲偟偰僼傽僀儖偑巜掕偝傟偰偄傞応崌偼摦嶌偝偣側偄傛偆偵
					      ;; 偲巚偭偨偑丆偁傫傑傝堄枴側偄丠
					      (= 2 (length (buffer-list))))
				     (ese-fortune))))


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 傏偆偭偲尒傞偨傔偺娭悢
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defun ese-fortune-set-absent-interval ()
  "ese-fortune: 夋柺偑愗傝懼傢傞昩悢傪曄峏"
  (interactive)
  (let ((absent *ese-fortune-absent-mode*))
    (ese-fortune-absent-mode nil)
    (message "尰嵼偺愝掕抣丗 ~D 昩" *ese-fortune-absent-interval*)
    (setq *ese-fortune-absent-interval* (read-integer "absent interval: "))
    (when absent
      (ese-fortune-absent-mode t))))

;;; 傏偆偭偲尒傞
(defun ese-fortune-absent ()
  (if (find-buffer *ese-fortune-buffer*)
      (when (equal *ese-fortune-buffer* (buffer-name (selected-buffer)))
	(ese-fortune)
	(when (fboundp 'shake-window)
	  (shake-window)))
    (ese-fortune-return-to-me))
  (refresh-screen))

;;; 傏偆偭偲尒巒傔傞
(defun ese-fortune-absent-start ()
  (start-timer *ese-fortune-absent-interval* 'ese-fortune-absent))

;;; 傢傟偵曉傞
(defun ese-fortune-return-to-me ()
  (stop-timer 'ese-fortune-absent))


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 ese-fortune-absent-mode
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defvar-local *ese-fortune-absent-mode* nil)

(defun ese-fortune-absent-mode (&optional (arg nil sv))
  (interactive "p")
  (ed::toggle-mode '*ese-fortune-absent-mode* arg sv)
  (update-mode-line t)
  (if *ese-fortune-absent-mode*
      (ese-fortune-absent-start)
    (ese-fortune-return-to-me))
  t)

(pushnew '(*ese-fortune-absent-mode* . "I am absentminded.") *minor-mode-alist* :key #'car)


;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;
;;;  仭 僨乕僞僼傽僀儖嶌惉
;;; 劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅劅 ;;;

(defun ese-fortune-reftxt2dat ()
  "ese-fortune: reference.txt 偐傜僨乕僞僼傽僀儖傪嶌惉"
  (interactive)
  (long-operation
    (let ((txt (merge-pathnames "reference.txt" (etc-path)))
	  (dat (merge-pathnames "reference.dat" (etc-path)))
	  ;; 偙偺惓婯昞尰偵儅僢僠偟側偄梫慺偼僗僉僢僾
	  (reg ".*\\[    section\\]:.*")
	  ;(reg ".*\\(\\[    section\\]:\\|Copyright (c)\\).*")  ; 側傫偺儔僀僙儞僗偐傢偐傜側偔側傞丅俀揰
	  (bufname "*reference.dat*")
	  buf)
      (when (file-exist-p txt)
	;; 嶌嬈梡僶僢僼傽偲僨乕僞偺弨旛
	(setq buf (create-new-buffer bufname))
	(set-buffer buf)
	(insert-file-contents txt t)
	;; 僨乕僞曄姺奐巒
	(while (scan-buffer "^仭" :regexp t :no-dup t)
	  (save-excursion
	    (save-restriction
	      (narrow-to-region (point)
				(progn
				  (or (scan-buffer "^仭" :regexp t :tail nil :no-dup t)
				      (goto-char (point-max)))
				  (forward-char -1)
				  (point)))
	      (goto-char (point-min))
	      ;; section 巜掕偺側偄梫慺偼僗僉僢僾
	      (if (string-match reg (buffer-substring (point-min) (point-max)))
		  (replace-buffer "\n" "\t" :regexp t)
		(delete-region (point-min) (point-max))))))
	;; 僶僢僼傽愭摢偐傜嵟弶偺僙僋僔儑儞傑偱傪嶍彍
	(goto-char (point-min))
	(delete-region (point)
		       (progn
			 (scan-buffer "^仭" :regexp t)
			 (point)))
	;; 夵峴偩偗偺峴傪嶍彍
	(replace-buffer "^\n" "" :regexp t)
	;; dat 僼傽僀儖偑偁傟偽戅旔偡傞
	(when (or (not (file-exist-p dat))
		  (copy-file dat (concat dat ".bak")
			     :if-exists :overwrite
			     :if-access-denied :error)
		  ;(yes-or-no-p "~A 傪忋彂偒偟偰椙偄偱偡偐丠" dat)
		  )
	  (write-file dat))
	(delete-buffer buf)))))

(defun ese-fortune-fortune2dat ()
  "ese-fortune: fortune梡僨乕僞僼傽僀儖傪ese-fortune梡僨乕僞僼傽僀儖偵曄姺"
  (interactive)
  (let ((bufname "*ese-fortune-fortune2dat*")
	buf src dat defdatname
	)
    (setq src (file-name-dialog
	       :title "曄姺尦僼傽僀儖偺巜掕 (fortune梡僨乕僞僼傽僀儖)"
	       :multiple nil
	       :must-exist t
	       :filter '(("偡傋偰偺僼傽僀儖(*.*)" . "*.*"))))
    (when (and src (file-exist-p src))
      (setq defdatname (merge-pathnames (concat (pathname-name src) ".dat") (etc-path)))
      (setq dat (file-name-dialog
		 :title "曄姺愭僼傽僀儖偺巜掕 (ese-fortune梡僨乕僞僼傽僀儖)"
		 :default defdatname
		 :multiple nil
		 :filter '(("dat僼傽僀儖(*.dat)" . "*.dat")
			   ("偡傋偰偺僼傽僀儖(*.*)" . "*.*"))))
      (when dat
	;; 嶌嬈梡僶僢僼傽偲僨乕僞偺弨旛
	(setq buf (create-new-buffer bufname))
	(set-buffer buf)
	(insert-file-contents src)
	;; 僨乕僞曄姺
	(ese-fortune-make-dat "^%\n")
	;; dat 僼傽僀儖偑偁傟偽戅旔偡傞
	(when (file-exist-p dat)
	  (copy-file dat (concat dat ".bak")
		     :if-exists :overwrite
		     :if-access-denied :error))
	;; 僼傽僀儖偵彂偒崬傒
	(write-region (point-min) (point-max) dat)
	(delete-buffer buf)
	;; 妋擣梡偵嶌惉偟偨dat僼傽僀儖傪奐偔
	(find-file dat)))))

;;; 僜乕僗僨乕僞偐傜 ese-fortune 梡僨乕僞傪揔摉偵嶌惉
;;;   堷悢偲偟偰丆僨乕僞嬫愗傝傪昞偡惓婯昞尰傪巜掕偡傞
(defun ese-fortune-make-dat (reg &optional br)
  (let ((brtag "<ese-fortune-br>"))                    ; 壖夵峴傪昞偡暥帤楍
    (untabify (point-min) (point-max))                 ; 1. TAB暥帤傪敿妏僗儁乕僗偵抲姺
    (goto-char 0) (replace-buffer reg brtag :regexp t) ; 2. 巜掕偝傟偨惓婯昞尰晹暘傪壖夵峴偵抲姺
    (goto-char 0) (replace-buffer "\n" "\t")           ; 3. 夵峴傪僞僽偵抲姺
    (goto-char 0) (replace-buffer brtag (or br "\n"))  ; 4. 壖夵峴傪夵峴偵抲姺
    (goto-char 0) (replace-buffer "^\n" "" :regexp t)  ; 5. 夵峴偩偗偺峴傪嶍彍
    (goto-char (point-max)) (insert #\LFD)             ; 6. 枛旜偵夵峴傪壛偊傞
    ))

;;; ese-fortune.l ends here.
