;; lisp file for xyzzy ver0.2.2.233 or  xyzzy ver0.2.2.234
;; nonentityさん作 browse-yank.l remodel ring-popup-menu.l ver.0.0.0.3 // crayonzen [2006-06-15]15:41:56(+0900)

;(load-library "ring-popup-menu")

(provide "ring-popup-menu")

(in-package "editor")

(defvar *kill-ring-popup-menu* nil)
(defvar *selection-ring-popup-menu* nil)

(defun kill-ring-popup-menu ()
  (interactive)
  (or *kill-ring-popup-menu*
      (setq *kill-ring-popup-menu* (create-popup-menu nil)))
  (while (get-menu-position *kill-ring-popup-menu* 'kill-ring-popup-menu)
    (delete-menu *kill-ring-popup-menu* 'kill-ring-popup-menu))
  (continue-pre-selection)
  (dotimes (arg (length *kill-ring*))
    (let ((str (car (ed::current-kill arg t))))
      (add-menu-item *kill-ring-popup-menu* 'kill-ring-popup-menu
		     (if (< (length str) 72)
			 str
		       (substring str 0 72))
		     #'(lambda ()
			 (interactive)
			 (if (pre-selection-p)
			     (save-excursion
			       (delete-region (selection-point) (selection-mark))))
			 (insert str)
			 (stop-selection)
			 ))))
  (track-popup-menu *kill-ring-popup-menu*))

(defun selection-ring-popup-menu ()
  (interactive)
  (or *selection-ring-popup-menu*
      (setq *selection-ring-popup-menu* (create-popup-menu nil)))
  (while  (delete-menu *selection-ring-popup-menu* 0 t))
  (continue-pre-selection)
  (let ((str (get-clipboard-data)))
    (add-selection-ring-popup-menu str "<clip> ")
    (dolist (item *selection-ring*)
      (add-selection-ring-popup-menu (cdr item))))
  (track-popup-menu *selection-ring-popup-menu*))

(defun add-selection-ring-popup-menu (&optional str clip)
  (when str
    (add-menu-item *selection-ring-popup-menu* 'add-selection-ring-tag
		   (if (<= (length str) 72)
		       (concat clip str)
		     (concat clip (substring str 0 72) " ... >"))
		   #'(lambda ()
		       (interactive)
		       (if (pre-selection-p)
			   (save-excursion
			     (delete-region (selection-point) (selection-mark))))
		       (insert str)
		       (stop-selection)
		       ))))

(global-set-key #\M-y 'kill-ring-popup-menu)
(global-set-key #\M-v 'selection-ring-popup-menu)

;;M-yにはyank-popが割り当て。yank-selectを上書き。

;;M-vにはprevious-pageが割り当て。yank-selection-selectを上書き。C-vに合わせてのwindows仕様
;;F9 'yank-selectionに割り当ての方が自然
;;S-F9 'yank-rectangle-selectionがあるので注意。C-F9はない。
;;yank-selection-and-popのコマンドがあるがキー割り当てはない。
