summaryrefslogtreecommitdiff
path: root/modules/custom-text-enclose.el
blob: fa41d39fd399ec6f3111bde6462f237684b14820 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
;;; custom-text-enclose.el ---  -*- coding: utf-8; lexical-binding: t; -*-

;;; Commentary:

;; This module provides functions to surround words or regions with custom strings, and to append or prepend text to lines.

;; It includes three main functions:
;; - surround word or region with a user-specified string
;; - append text to the end of lines
;; - prepend text to the beginning of lines

;; All functions work on both the active region and the entire buffer when no region is selected.

;; Bound to keymap prefix C-; s

;;; Code:

(defun cj/surround-word-or-region ()
  "Surround the word at point or active region with a string read from the minibuffer."
  (interactive)
  (let ((str (read-string "Surround with: "))
        (regionp (use-region-p)))
    (save-excursion
      (if regionp
          (let ((beg (region-beginning))
                (end (region-end)))
            (goto-char end)
            (insert str)
            (goto-char beg)
            (insert str))
        (if (thing-at-point 'word)
            (let ((bounds (bounds-of-thing-at-point 'word)))
              (goto-char (cdr bounds))
              (insert str)
              (goto-char (car bounds))
              (insert str))
          (message "Can't insert around. No word at point and no region selected."))))))

(defun cj/append-to-lines-in-region-or-buffer (str)
  "Append STR to the end of each line in the region or entire buffer."
  (interactive "sEnter string to append: ")
  (let ((start-pos (if (use-region-p)
                       (region-beginning)
                     (point-min)))
        (end-pos (if (use-region-p)
                     (region-end)
                   (point-max))))
    (save-excursion
      (goto-char start-pos)
      (while (< (point) end-pos)
        (move-end-of-line 1)
        (insert str)
        (forward-line 1)))))

(defun cj/prepend-to-lines-in-region-or-buffer (str)
  "Prepend STR to the beginning of each line in the region or entire buffer."
  (interactive "sEnter string to prepend: ")
  (let ((start-pos (if (use-region-p)
                       (region-beginning)
                     (point-min)))
        (end-pos (if (use-region-p)
                     (region-end)
                   (point-max))))
    (save-excursion
      (goto-char start-pos)
      (while (< (point) end-pos)
        (beginning-of-line 1)
        (insert str)
        (forward-line 1)))))

;; Surround, append, prepend prefix keymap
(define-prefix-command 'cj/enclose-map nil
					   "Keymap for enclosing text: surrounding, appending, and prepending.")
(keymap-set cj/custom-keymap "s" #'cj/enclose-map)
(keymap-set cj/enclose-map "s" #'cj/surround-word-or-region)
(keymap-set cj/enclose-map "a" #'cj/append-to-lines-in-region-or-buffer)
(keymap-set cj/enclose-map "p" #'cj/prepend-to-lines-in-region-or-buffer)

(provide 'custom-text-enclose)
;;; custom-text-enclose.el ends here.