;;; rcd-temp-buffer.el --- RCD Temporary Buffer -*- lexical-binding: t; -*- ;; Copyright (C) 2016-2024 by Jean Louis ;; Author: Jean Louis ;; Version: 0.1 ;; Package-Requires: (rcd-utilities) ;; Keywords: convenience tools ;; URL: https:// NOT YET rcd-temp-buffer-el.html ;; This file is not part of GNU Emacs. ;; 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 . ;;; Commentary: ;;; Change Log: ;;; Code: ;;; RCD TEMPORARY BUFFERS (defvar rcd-temp-buffer-name "RCD TEMPORARY BUFFER") (defvar rcd-temp-buffer-session-count 0) (defvar rcd-temp-buffer-list nil) (defvar rcd-temp-buffer-modes '(("adoc-mode" . "adoc") ("emacs-lisp-mode" . "el") ("lisp-mode" . ".lisp") ("markdown-mode" . ".md") ("org-mode" . "org") ("sql-mode" . "sql") ("fundamental-mode" . "txt") ("html-mode" . "html"))) (defun rcd-temp-buffer-next () "Switch to next available temporary buffer." (interactive) (let ((rcd-temp-buffer-list (reverse (rcd-list-matching-buffers rcd-temp-buffer-name)))) (cond ((and rcd-temp-buffer-list (member (current-buffer) rcd-temp-buffer-list)) (switch-to-buffer (next-circular-list-item rcd-temp-buffer-list (current-buffer)))) ((and rcd-temp-buffer-list (switch-to-buffer (car rcd-temp-buffer-list)))) (t (rcd-warning-message "No temporary buffers"))))) (defun rcd-temp-buffer-destroy-em () "Destroy all temporary buffers." (interactive) (kill-matching-buffers rcd-temp-buffer-name)) (defun rcd-temp-buffer (&optional name mode) "Generate new temporary buffer. In Emacs, you can effortlessly generate a new temporary buffer to use for various purposes. A temporary buffer is essentially a buffer that exists temporarily in your current Emacs session and gets discarded once you close it. This feature is particularly handy when you want to quickly jot down notes, brainstorm ideas, or experiment with some code without cluttering your existing buffers. Standard temporary buffer named \"*scratch*\" is specifically designed for temporary use. Once you have created the temporary buffer, you can freely modify it, insert text, execute Emacs commands, or write code within it. It behaves just like any other Emacs buffer. Moreover, you can switch between this temporary buffer and other buffers in your Emacs session using the usual buffer-switching commands like \"C-x b\" or \"C-x o\". This flexibility allows you to seamlessly navigate between different buffers while working on your project. Furthermore, Emacs offers a multitude of features and functionalities that you can leverage within temporary buffers. You can customize your temporary buffer's major mode to suit your specific needs, enabling syntax highlighting, indentation, and other language-specific functionalities. This ensures that your temporary buffer provides an environment tailored to your current task, whether it's writing prose, coding, or organizing information. Temporary buffers in Emacs are incredibly versatile and create a dynamic workspace where you can experiment, brainstorm, and iterate without being confined to a particular document or file. They seamlessly complement Emacs's overall philosophy of flexibility and extensibility, making it a powerful text editor for handling various projects and workflows." (interactive) (let* ((file-name (concat rcd-temp-file-directory (format-time-string "%Y-%m-%d-%H:%M:%S-") rcd-temp-buffer-name ".txt")) (buffer (or name (concat "*" rcd-temp-buffer-name "*")))) (switch-to-buffer (generate-new-buffer buffer)) (setq buffer-file-name file-name) (save-buffer) (setq rcd-temp-buffer-session-count (1+ rcd-temp-buffer-session-count)) (rcd-general-log (concat "Opened " buffer-file-name) nil 1 nil nil nil 13) (if current-prefix-arg (let ((mode (rcd-choose (map-keys rcd-temp-buffer-modes) "Major mode: "))) (funcall (intern mode))) (funcall (intern (or mode "fundamental-mode")))) (rcd-temp-buffer-minor-mode) buffer-file-name)) (defun rcd-temp-buffer-ask-name () "Generate new temporary buffer by asking for buffer name." (interactive) (rcd-temp-buffer (rcd-ask-get "Buffer name: "))) (defun rcd-show-prominently (text) "This function was meant to make better presentation of the TEXT." (let ((buffer (rcd-temp-buffer nil "*RCD Showing Prominently*"))) (switch-to-buffer buffer) (insert text))) (defun rcd-temp-buffer-kill-current () "Kill current buffer without asking." (interactive) (let ((buffer-name (buffer-name (current-buffer)))) (kill-matching-buffers-no-ask (regexp-quote buffer-name)) (rcd-message "Killed: %s" buffer-name))) (define-minor-mode rcd-temp-buffer-minor-mode "A minor mode for RCD temporary buffers." :init-value nil :lighter "RCD Temp Buffer Mode" :keymap (let ((map (make-sparse-keymap))) (define-key map (kbd "C-x k") #'rcd-temp-buffer-kill-current) (define-key map (kbd "C-") #'kill-buffer) map)) (global-set-key (kbd "") #'rcd-temp-buffer) (provide 'rcd-temp-buffer) ;;; rcd-temp-buffer.el ends here