summary refs log tree commit diff stats
path: root/lisp/+scratch.el
blob: e9b825a3a943671bce3a6c9a82fdb05eed7cf8e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
;;; +scratch.el -*- lexical-binding: t; -*-

;;; Code:

;;(require 'scratch)

(defun +scratch-immortal ()
  "Bury, don't kill \"*scratch*\" buffer.
For `kill-buffer-query-functions'."
  (if (or (eq (current-buffer) (get-buffer "*scratch*"))
          (eq (current-buffer) (get-buffer "*text*")))
      (progn (bury-buffer)
             nil)
    t))

(defun +scratch-buffer-setup ()
    "Add comment to `scratch' buffer and name it accordingly."
    (let* ((mode (format "%s" major-mode))
           (string (concat "Scratch buffer for:" mode "\n\n")))
      (when scratch-buffer
        (save-excursion
          (insert string)
          (goto-char (point-min))
          (comment-region (point-at-bol) (point-at-eol)))
        (next-line 2))
      (rename-buffer (concat "*scratch<" mode ">*") t)))

(defun +scratch-fortune ()
  (let* ((fmt (if (executable-find "fmt")
                  (format "| fmt -%d -s" (- fill-column 2))
                ""))
         (s (string-trim
             (if (executable-find "fortune")
                 (shell-command-to-string (concat "fortune -s" fmt))
               "ABANDON ALL HOPE YE WHO ENTER HERE"))))
    (concat (replace-regexp-in-string "^" ";; " s)
            "\n\n")))

;; [[https://old.reddit.com/r/emacs/comments/ui1q41/weekly_tips_tricks_c_thread/i7ef4xg/][u/bhrgunatha]]
(defun +scratch-text-scratch ()
  "Create a \"*text*\" scratch buffer in Text mode."
  (with-current-buffer (get-buffer-create "*text*")
    (text-mode)))

(defun +scratch-toggle (buffer)
  "Switch to BUFFER, or to the previous buffer."
  (switch-to-buffer (unless (eq (current-buffer)
                                (get-buffer buffer))
                      buffer)))

(defun +scratch-switch-to-scratch ()
  "Switch to scratch buffer."
  (interactive)
  (+scratch-toggle "*scratch*"))

(defun +scratch-switch-to-text ()
  "Switch to text buffer."
  (interactive)
  (+scratch-toggle "*text*"))

(provide '+scratch)
;;; +scratch.el ends here