;;; lisp file for xyzzy ver0.2.2.234
;;; delete-history.l ver.0.0.0.3 // crayonzen [2006-05-06]01:19:01(+0900)

;; how-to: load
;;       : write in .xyzzy or siteinit.l
;;
;; (load-library "delete-history")

(provide "delete-history")

(in-package "editor")

(export '(delete-history-all
	  delete-history-search
	  delete-history-file
	  delete-history-minibuffer
	  delete-history-filer

	  delete-history-list
	  open-history-file
	  delete-history-forcibly
	  ))

;;;;;;;;;;;;;;;;ヒストリ変数の項目を削除する
(defun delete-history-all ()
  (interactive)
  (when (no-or-yes-p "全ての履歴の項目を削除しますか？")
    (delete-history-search)
    (delete-history-file)
    (delete-history-minibuffer)
    (delete-history-filer))
  )

;;;;;;;;;;;;;;;;;search
(defun delete-history-search ()
  (interactive)
  (when (no-or-yes-p "検索の履歴を削除しますか？")
    (setq *minibuffer-search-string-history* nil)
    (setq ed::*grep-directory-history* nil)
    (setq ed::*grep-file-history* nil))
  )

;;;;;;;;;;;;;;;;;file
(defun delete-history-file ()
  (interactive)
  (when (no-or-yes-p "ファイルの履歴を削除しますか？")
    (setq *minibuffer-file-name-history* nil)
    (setq *delete-file-name-history* nil)
    (setq ed::*session-file-history* nil)
    (add-file-history-to-menu)
    (ed::add-session-history-to-menu))
  )

;;;;;;;;;;;;;;;;;minibuffer
(defun delete-history-minibuffer ()
  (interactive)
  (when (no-or-yes-p "ミニバッファの履歴を削除しますか？（検索とファイルを含まない）")
    ;(setq *minibuffer-file-name-history* nil)
    (setq *minibuffer-directory-name-history* nil)
    ;(setq *minibuffer-search-string-history* nil)
    (setq *minibuffer-buffer-name-history* nil)
    (setq *minibuffer-symbol-name-history* nil)
    (setq *minibuffer-lisp-sexp-history* nil)
    (setq *minibuffer-execute-history* nil)
    (setq *minibuffer-complex-command-history* nil)
    (setq *minibuffer-default-history* nil))
  )

;;;;;;;;;;;;;;;;;filer
(defun delete-history-filer ()
  (interactive)
  (when (no-or-yes-p "ファイラーの履歴を削除しますか？")
    (setq ed::*filer-rename-regexp-to-history* nil)
    (setq ed::*filer-rename-regexp-from-history* nil)
    (setq ed::*filer-delete-mask-history* nil)
    (setq ed::*filer-directory-history* nil)
    (setq ed::*filer-search-pattern-history* nil)
    (setq ed::*filer-search-directory-history* nil)
    (setq ed::*filer-archive-directory-history* nil))
  )

;;;;;;;;;;;;;;;;delete history list (variable)
(defun delete-history-list (variable leave-max)
  (interactive)
  (when variable
    (setq n (- (length variable) leave-max))
    (when (> n 0)
      (setq variable (nbutlast variable n))
      ))
  )

;;;;;;;;;;;;;;;;
(defun open-history-file ()
  (interactive)
  (when *history-file-name*
    (setq buffer (find-file-internal *history-file-name*))
    (set-buffer buffer)
    (goto-char (point-min)))
  )

;;;;;;;;;;;;;;;;
(defun delete-history-forcibly ()
  (interactive "*")
  (if *history-file-name*
      (progn
	(setq buffer (find-file-internal *history-file-name*))
	(set-buffer buffer)
	(goto-char (point-min))
	(setq str1 "-history* '" str2 "-history* 'nil)")
	(while (scan-buffer str1)
	  (delete-region (point)
			 (progn
			   (goto-eol)
			   (point)))
	  (insert str2))
	(goto-char (point-min))
	(message "有効にするには別のディレクトリに保存してxyzzyを終了してからファイルを入れ替える"))
    (message "ﾋｽﾄﾘﾌｧｲﾙがないよ？ .xyzzy.history _xyzzy.his ？"))
  )

;;;;;;;;;;;;;;;;menu

(defun dummy ()
  (interactive)
  nil)
  
(export '(*delete-history-menus*
	  init-delete-history-menu
	  ))

(defvar *delete-history-menus* nil)
(defvar *delete-history-detail-menus* nil)

(defun init-delete-history-menu ()
  (or *delete-history-menus*
      (setq *delete-history-menus* (create-popup-menu)))

  (or *delete-history-detail-menus*
      (setq *delete-history-detail-menus* (create-popup-menu)))

  
  (setq *delete-history-detail-menus*
	(define-popup-menu
	  (:item nil "※注意！即削除されます" 'dummy)
	  :sep
	  (:item nil "検索" 'dummy)
	  (:item nil "*minibuffer-search-string-history*"
	   #'(lambda () (interactive) (setq *minibuffer-search-string-history* nil)))
	  (:item nil "ed::*grep-directory-history*"
	   #'(lambda () (interactive) (setq ed::*grep-directory-history* nil)))
	  (:item nil "ed::*grep-file-history*"
	   #'(lambda () (interactive) (setq ed::*grep-file-history* nil)))
	  :sep
	  (:item nil "ファイル" 'dummy)
	  (:item nil "*minibuffer-file-name-history*"
	   #'(lambda () (interactive) (setq *minibuffer-file-name-history* nil)
	       (add-file-history-to-menu)))
	  (:item nil "*delete-file-name-history*"
	   #'(lambda () (interactive) (setq *delete-file-name-history* nil)
	       (add-file-history-to-menu)))
	  (:item nil "ed::*session-file-history*"
	   #'(lambda () (interactive) (setq ed::*session-file-history* nil)
	       (ed::add-session-history-to-menu)))
	  :sep
	  (:item nil "ミニバッファ" 'dummy)
	  (:item nil "*minibuffer-file-name-history*"
	   #'(lambda () (interactive) (setq *minibuffer-file-name-history* nil)
	       (add-file-history-to-menu)))
	  (:item nil "*minibuffer-directory-name-history*"
	   #'(lambda () (interactive) (setq *minibuffer-directory-name-history* nil)))
	  (:item nil "*minibuffer-search-string-history*"
	   #'(lambda () (interactive) (setq *minibuffer-search-string-history* nil)))
	  (:item nil "*minibuffer-buffer-name-history*"
	   #'(lambda () (interactive) (setq *minibuffer-buffer-name-history* nil)))
	  (:item nil "*minibuffer-symbol-name-history*"
	   #'(lambda () (interactive) (setq *minibuffer-symbol-name-history* nil)))
	  (:item nil "*minibuffer-lisp-sexp-history*"
	   #'(lambda () (interactive) (setq *minibuffer-lisp-sexp-history* nil)))
	  (:item nil "*minibuffer-execute-history*"
	   #'(lambda () (interactive) (setq *minibuffer-execute-history* nil)))
	  (:item nil "*minibuffer-complex-command-history*"
	   #'(lambda () (interactive) (setq *minibuffer-complex-command-history* nil)))
	  (:item nil "*minibuffer-default-history*"
	   #'(lambda () (interactive) (setq *minibuffer-default-history* nil)))
	  :sep
	  (:item nil "ファイラー" 'dummy)
	  (:item nil "ed::*filer-rename-regexp-to-history*"
	   #'(lambda () (interactive) (setq ed::*filer-rename-regexp-to-history* nil)))
	  (:item nil "ed::*filer-rename-regexp-from-history*"
	   #'(lambda () (interactive) (setq ed::*filer-rename-regexp-from-history* nil)))
	  (:item nil "ed::*filer-delete-mask-history*"
	   #'(lambda () (interactive) (setq ed::*filer-delete-mask-history* nil)))
	  (:item nil "ed::*filer-directory-history*"
	   #'(lambda () (interactive) (setq ed::*filer-directory-history* nil)))
	  (:item nil "ed::*filer-search-pattern-history*"
	   #'(lambda () (interactive) (setq ed::*filer-search-pattern-history* nil)))
	  (:item nil "ed::*filer-search-directory-history*"
	   #'(lambda () (interactive) (setq ed::*filer-search-directory-history* nil)))
	  (:item nil "ed::*filer-archive-directory-history*"
	   #'(lambda () (interactive) (setq ed::*filer-archive-directory-history* nil)))
	  )
	)

  (setq *delete-history-menus*
	(define-popup-menu
	  (:popup 'delete-history-tag "履歴の削除(&H)"
	   (:item nil "履歴の削除全て(&D)"
	    'delete-history-all)
	   :sep
	   (:item nil "履歴の削除：検索(&S)"
	    'delete-history-search)
	   (:item nil "履歴の削除：ファイル(&F)"
	    'delete-history-file)
	   (:item nil "履歴の削除：ミニバッファ(&B)"
	    'delete-history-minibuffer)
	   (:item nil "履歴の削除：ファイラー(&G)"
	    'delete-history-filer)
	   :sep
	   (:menu nil "各履歴の削除"
	   *delete-history-detail-menus*))
	  
	  (:popup 'open-history-tag "ﾋｽﾄﾘﾌｧｲﾙを開く(&O)"
	   (:item nil "ﾋｽﾄﾘﾌｧｲﾙを開く(&O)"
	    'open-history-file)
	   (:item nil "ﾋｽﾄﾘﾌｧｲﾙのﾋｽﾄﾘを削除して開く(&D)"
	    'delete-history-forcibly)
	   )))

  )


;;;;;;;;;;;;;;;; 挿入メニュー６　insert menu tools ﾂｰﾙ
(add-hook '*init-app-menus-hook*
	  #'(lambda ()
	      (init-delete-history-menu)
	      (when (and *app-menu*
			 (setq menu (get-menu *app-menu* 'ed::tools)))
		(setq ip -1)
		(while (get-menu menu (incf ip) t))

		(insert-menu-separator menu ip 'delete-history-sep)
		(insert-popup-menu menu (incf ip) *delete-history-menus* "ﾋｽﾄﾘの削除(&H)")
		)
	      )
	  )

;;;;;;;;;;;;;;;;;;minibuffer
;;*minibuffer-file-name-history*
;;*minibuffer-directory-name-history*
;;*minibuffer-search-string-history*
;;*minibuffer-buffer-name-history*
;;*minibuffer-symbol-name-history*
;;*minibuffer-lisp-sexp-history*
;;*minibuffer-execute-history*
;;*minibuffer-complex-command-history*
;;*minibuffer-default-history*
;;
;;;;;;;;;;;;;;;;;;grep
;;*grep-directory-history*
;;*grep-file-history*
;;
;;;;;;;;;;;;;;;;;;session
;;*session-file-history*
;;
;;;;;;;;;;;;;;;;;;filer
;;*filer-rename-regexp-to-history*
;;*filer-rename-regexp-from-history*
;;*filer-delete-mask-history*
;;*filer-directory-history*
;;*filer-search-pattern-history*
;;*filer-search-directory-history*
;;*filer-archive-directory-history*
;;
;;;;;;;;;;;;;;;;file
;;*delete-file-name-history*
