;;; lisp file for xyzzy ver0.2.2.233 or ver0.2.2.234
;;; extend-function.l ver.0.0.1.9 //crayonzen [2006-06-17]10:41:48(+0900)-[2023-02-13]08:16:38(+0900)

(provide "extend-function")

(in-package "editor")

(export '(insert-hr
	  set-insert-hr-char-number
	  insert-hr-plus
	  insert-hr-slash
	  insert-hr-asterisk
	  insert-hr-ruled-line
	  insert-hr-char
	  reverse-region-leave
	  clear-reverse-region-leave
	  ;delete-forward-spaces
	  print-buffer-list
	  print-buffer-list-tab-order
	  exchange-buffer
	  e-j-space-detach-selection
	  string-dotimes
	  new-file-text
	  new-file-currentmode
	  do-reverse-region
	  insert-pc-internal-time2
	  ))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defvar *hr-char* "-")
(defvar *hr-char-number* 68)

(defun insert-hr ()
  (interactive "*")
  (when (pre-selection-p)
    (delete-region (selection-mark) (selection-point)))
  (dotimes (i *hr-char-number*)
    (insert *hr-char*))
  )

(defun set-insert-hr-char-number (n)
  (interactive "ninsert-hr-char-number: ")
  (setq *hr-char-number* n)
  (message "set hr: ~d" *hr-char-number*)
  )

(defun insert-hr-plus ()
  (interactive "*")
  (insert-hr-char "+" 34)
  )

(defun insert-hr-slash ()
  (interactive "*")
  (insert-hr-char "/" 34)
  )

(defun insert-hr-asterisk ()
  (interactive "*")
  (insert-hr-char "*" 34)
  )

(defun insert-hr-ruled-line ()
  (interactive "*")
  (insert-hr-char "━" 17)
  )

(defun insert-hr-char (hr-char hr-char-number)
  (interactive "*")
  (when (pre-selection-p)
    (delete-region (selection-mark) (selection-point)))
  (dotimes (i hr-char-number)
    (insert hr-char))
  )

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun reverse-region-leave ()
  (interactive "*")
  (cond ((pre-selection-p)
	 (reverse-region (selection-mark) (selection-point) nil))
	((mark t)
	 (reverse-region (mark nil) (point) nil))
	(t
	 (save-excursion
	   (reverse-region (progn (goto-bol) (point))
			   (progn (goto-eol)(point)) nil))
	 ))
  )

(defun clear-reverse-region-leave ()
  (interactive "*")
  (clear-reverse-region))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;(delete-trailing-spaces) -> (skip-chars-backward " \t　")
#|(defun delete-forward-spaces ()
  (delete-region (point)
		 (progn
		   (skip-chars-forward " \t　")
		   (point))))
|#

;;;全てのパッファ名を出力
(defun print-buffer-list ()
  (interactive "*")
  (save-excursion
    (dolist (buffer (buffer-list))
      (insert (buffer-name buffer))
      (insert #\LFD)))
  )

;;;全てのパッファ名を出力　タブ順
;;(get-next-buffer (&optional buffer reverse tab-order internal))
(defun print-buffer-list-tab-order ()
  (interactive "*")
  (save-excursion
    (setq cur (selected-buffer))
    (setq nlist (list-length (buffer-list)))
    (decf nlist)
    (dotimes (i nlist)
      (insert (buffer-name cur))
      (insert #\LFD)
      (setq next (get-next-buffer cur nil t nil))
      (setq cur next)
      ))
  )

;;;最後に表示していたバッファに切り替える
(defun exchange-buffer ()
  (interactive)
  (when (bufferp (setq buffer (other-buffer)))
      (set-buffer buffer))
  )
;;;最後に表示していたバッファの移動に加え、ヒストリを残したい場合などは下記のコマンドが用意されている。
;buffer.l:252:(define-key ctl-x-map #\b 'switch-to-buffer)
;buffer.l:256:(define-key ctl-x-4-map #\b 'switch-to-buffer-other-window)

;;;;;;;;;;;;;;;;英字と漢字の間に空白を１つあける　リファレンスから
(defun e-j-space-detach-selection ()
  (interactive "*")
  (if (pre-selection-p)
      (e-j-space-detach (selection-mark) (selection-point));selection
    (e-j-space-detach (mark nil) (point));region
    ))

(defun e-j-space-detach (from to)
  (interactive "*r")
  (if (> from to)
      (rotatef from to))
  (save-excursion
    (save-restriction
      (narrow-to-region from to)
      (goto-char (point-min))
      (replace-buffer "\\([A-Za-z0-9_]\\) *\\([\X8200-\X9fff\Xe000-\Xfcff]\\)"
		      "\\1 \\2" :regexp t)
      (goto-char (point-min))
      (replace-buffer "\\([\X8200-\X9fff\Xe000-\Xfcff]\\) *\\([A-Za-z0-9_]\\)"
		      "\\1 \\2" :regexp t)
      ))
  t)

;;;;;;;;;;;;;;;;入力文字を指定回数分実行（ミニバッファで C-j や C-Enter などで改行可）
(defun string-dotimes (str arg)
  (interactive "sstring: \nndoimes:")
  (when (>= arg 1)
    (dotimes (i arg)
      (insert str)))
  )

;;;;;;;;;;;;;;;;new file text
(defun new-file-text ()
  (interactive)
  (setq default-buffer-mode *default-buffer-mode*)
;;  (setq *default-buffer-mode* 'text-mode)
  (setq *default-buffer-mode* 'essay-mode)
  (switch-to-buffer (create-new-buffer "*untitled*"))
  (setq *default-buffer-mode* default-buffer-mode))

;;;;;;;;;;;;;;;;new file current mode
(defun new-file-currentmode ()
  (interactive)
  (setq default-buffer-mode *default-buffer-mode*)
  (setq *default-buffer-mode* buffer-mode)
  (switch-to-buffer (create-new-buffer "*untitled*"))
  (setq *default-buffer-mode* default-buffer-mode))

;;;;;;;;;;;;;;;; at function reverse-region
(defun do-reverse-region (&optional (temp t))
  (interactive "*p")
  (if (mark t)
      (reverse-region (mark nil) (point) temp)
    (message "non marked (mark: C-@ or C-space)")
    )
  )
