;;; kkk-scratch.el --- Scratch Function              -*- lexical-binding: t; -*-

;; Copyright (C) 2023  

;; Author:  <pikuinha@t60-gentoo_musl>
;; Keywords: 

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

;; Scratch everything

;;; Code:

(defgroup kkk-scratch nil
  "Group of scratch things."
  :group 'file)

(defun kkk-scratch-helper-dwim (BUFFER BUFFER_BACK)
  "This is a helper to all to-scratch dwim functions.

BUFFER is the scratch buffer to change to.
BUFFER_BACK is the scratch buffer to change back to when is called again."
  (let ((cb (buffer-name)))
    (if (string= cb BUFFER)
	(progn
	  (switch-to-buffer (eval BUFFER_BACK))
	  (bury-buffer BUFFER))
      (progn
	(set BUFFER_BACK cb)
	(switch-to-buffer BUFFER)))))

(defvar kkk-scratch--buffer-last-buffer)
(defun kkk-scratch-buffer-dwim ()
  "Change to the scratch buffern and back."
  (interactive)
  (kkk-scratch-helper-dwim "*scratch*"
			   'kkk-scratch--bufer-last-buffer))

(defvar kkk-scratch--vterm-last-buffer)
(defun kkk-scratch-vterm-dwim ()
  "Change to a scratch vterm and back."
  (interactive)
  (let ((buffer_name "*vterm-scratch*")
	(last_buffer 'kkk-scratch--vterm-last-buffer))
    (if (get-buffer buffer_name)
	(kkk-scratch-helper-dwim buffer_name last_buffer)
      (progn
	(set last_buffer (current-buffer))
	(vterm buffer_name)))))

(defvar kkk-scratch--eshell-last-buffer)
(defun kkk-scratch-eshell-dwim ()
  "Change to a scratch vterm and back."
  (interactive)
  (let ((buffer_name "*eshell-scratch*")
	(last_buffer 'kkk-scratch--eshell-last-buffer))
    (if (get-buffer buffer_name)
	(kkk-scratch-helper-dwim buffer_name last_buffer)
      (progn
	(set last_buffer (current-buffer))
	(eshell)
	(rename-buffer buffer_name)))))

;; (defconst kkk-scratch-command-map
;;   (let ((map (make-sparse-keymap)))
;;     (define-key map (kbd "b")   #'kkk-scratch-buffer-dwim)
;;     (define-key map (kbd "x v") #'kkk-scratch-vterm-dwim)
;;     (define-key map (kbd "x e") #'kkk-scratch-eshell-dwim))
;;   "Keymap for scratch comands.")

(provide 'kkk-scratch)
;;; kkk-scratch.el ends here
