-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-functions.el
224 lines (195 loc) · 8.04 KB
/
base-functions.el
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
;; -*- lexical-binding: t; -*-
;;; package --- Add your custom functions
;;; (defun something
;; (do-something))
;;; Commentary:
(defun exchange-point-and-mark-no-activate ()
"Identical to \\[exchange-point-and-mark] but will not activate the region."
(interactive)
(exchange-point-and-mark)
(deactivate-mark nil))
(define-key global-map (kbd "A-s p") 'exchange-point-and-mark-no-activate)
(defun duplicate-current-line-or-region (arg)
"Duplicates the current line or region ARG times.
If there's no region, the current line will be duplicated. However, if
there's a region, all lines that region covers will be duplicated."
(interactive "p")
(let (beg end (origin (point)))
(if (and mark-active (> (point) (mark)))
(exchange-point-and-mark))
(setq beg (line-beginning-position))
(if mark-active
(exchange-point-and-mark))
(setq end (line-end-position))
(let ((region (buffer-substring-no-properties beg end)))
(dotimes (i arg)
(goto-char end)
(newline)
(insert region)
(setq end (point)))
(goto-char (+ origin (* (length region) arg) arg)))))
(defun artem/delete-line-and-move-up (&optional arg)
"remove line and move one line up"
(interactive)
(delete-region (line-beginning-position) (line-end-position))
(delete-backward-char 1))
(defun artem/delete-line-and-move-down (&optional arg)
"remove line and move one line up"
(interactive)
(delete-region (line-beginning-position) (line-end-position))
(delete-forward-char 1)
)
(defun artem/delete-until-end-of-the-line (&optional arg)
;; "delete everything until the end of line"
(interactive "^p")
(delete-region (point) (line-end-position)))
(defun artem/delete-until-beginning-of-the-line (&optional arg)
"delete everything until the end of line"
(interactive "^p")
(delete-region (point) (line-beginning-position)))
(defun artem/beginning-of-line (&optional arg)
"Move point back to beginning of visual line, then indentation, then beginning of logical line.
Move point to the beginning of visual line, then to the first non-whitespace character on the logical line, then to the beginning of logical line.
After beginning of logical line is reached toggle between the first non-whitespace character and the beginning of the logical line.
If ARG is not nil or 1, move forward ARG - 1 lines first. If
point reaches the beginning or end of the buffer, stop there."
(interactive "^p")
(setq arg (or arg 1))
;; Move lines first
(when (/= arg 1)
(let ((line-move-visual t))
(forward-line (1- arg))))
(let ((orig-point (point)))
(vertical-motion 0)
(when (= orig-point (point))
(back-to-indentation))))
(defun artem/end-of-line (arg)
"Move point to the end of visual line, then move point to the end of logical line.
If ARG is not nil or 1, move forward ARG - 1 lines first. If
point reaches the beginning or end of the buffer, stop there."
(interactive "^p")
(setq arg (or arg 1))
;; Move lines first
(when (/= arg 1)
(let ((line-move-visual nil))
(forward-line (1- arg))))
(let ((orig-point (point)))
(end-of-visual-line)
(when (= orig-point (point))
(end-of-line))))
(defun insert-current-date () (interactive)
(insert (shell-command-to-string "echo -n $(date +%Y-%m-%d)")))
;;toggle hide/show block for hs-minor-mode
(defun hs-toggle ()
(interactive)
(let ((saved-point (point)))
(end-of-line)
(hs-toggle-hiding)
(goto-char saved-point)))
(defun copy-line (arg)
"Copy lines (as many as prefix argument) in the kill ring.
Ease of use features:
- Move to start of next line.
- Appends the copy on sequential calls.
- Use newline as last char even on the last line of the buffer.
- If region is active, copy its lines."
(interactive "p")
(let ((beg (line-beginning-position))
(end (line-end-position arg)))
(when mark-active
(if (> (point) (mark))
(setq beg (save-excursion (goto-char (mark)) (line-beginning-position)))
(setq end (save-excursion (goto-char (mark)) (line-end-position)))))
(if (eq last-command 'copy-line)
(kill-append (buffer-substring beg end) (< end beg))
(kill-ring-save beg end)))
(kill-append "\n" nil)
(beginning-of-line (or (and arg (1+ arg)) 2))
(if (and arg (not (= 1 arg))) (message "%d lines copied" arg)))
(defun wrap-double-quote (&optional arg)
(interactive "P")
(sp-wrap-with-pair "\""))
(defun wrap-back-quote (&optional arg)
(interactive "P")
(sp-wrap-with-pair "`"))
(defun wrap-singe-quote (&optional arg)
(interactive "P")
(sp-wrap-with-pair "'"))
(defun insert-timestamp-default ()
"Insert the current timestamp"
(interactive)
(insert (current-time-string)))
(defalias 'its 'insert-timestamp-default)
(defun insert-timestamp-htmlcomment ()
"Insert the current timestamp (HTML comment)"
(interactive)
(insert
(concat
"<!-- "
(format-time-string "%Y-%m-%d %T ")
((lambda (x) (concat (substring x 0 3) ":" (substring x 3 5)))
(format-time-string "%z"))
" -->\n")))
(defalias 'itsh 'insert-timestamp-htmlcomment)
(defun insert-timestamp-unixtime ()
"Insert the current Unix time"
(interactive)
(let ((time (current-time)))
(let ((time1 (car time))
(time2 (car (cdr time))))
(insert (format "%d" (+ (* 65536 time1) time2))))))
(defalias 'itsu 'insert-timestamp-unixtime)
(defun insert-timestamp-iso ()
"Insert the current timestamp (ISO 8601 format)"
(interactive)
(insert
(concat
(format-time-string "%Y-%m-%dT%T")
((lambda (x) (concat (substring x 0 3) ":" (substring x 3 5)))
(format-time-string "%z")))))
(defalias 'itsi 'insert-timestamp-iso)
(defalias 'itsiso 'insert-timestamp-iso)
(defun artem/embark-consult-export-lines-to-grep (lines)
"Create a grep mode buffer listing LINES as alternative to occur.
This enables use of `wgrep' for editing of results including multi-line editing.
Because `consult-line' produces grep-like results, the use of `wgrep' helps to
increase uniformity of user experience when editing the results.
The elements of LINES are assumed to be values of category `consult-line'."
(let ((buf (generate-new-buffer "*Embark Export Grep*"))
last-buf
filename)
(with-current-buffer buf
(insert (propertize "Caution:" 'wgrep-header t 'font-lock-face '(:inherit 'compilation-warning :underline t))
(propertize " only buffers backed by a file are exported to grep-mode.\n" 'wgrep-header t 'font-lock-face '(:inherit 'compilation-warning)))
(insert (propertize "Hint:" 'font-lock-face '(:inherit 'compilation-info :underline t))
(propertize " use wgrep for editing of the results in place and saving them to respective files\n" 'font-lock-face '(:inherit 'compilation-info)))
(dolist (line lines)
(pcase-let*
((`(,loc . ,num) (consult--get-location line))
(lineno (format "%d" num))
(contents (embark-consult--strip line))
(this-buf (marker-buffer loc)))
(when (buffer-file-name this-buf)
(unless (eq this-buf last-buf)
(setq last-buf this-buf)
(setq filename (file-name-nondirectory (buffer-file-name this-buf)))
(insert "\n" (propertize (concat "file: " filename) 'wgrep-ignore t 'font-lock-face '(:inherit compilation-info :underline t)) "\n"))
(insert (propertize
(concat filename ":")
'invisible t)
lineno
":"
contents "\n" ))))
(goto-char (point-min))
(grep-mode)
(setq next-error-last-buffer buf)
;; Set up keymap before possible wgrep-setup, so that wgrep
;; restores our binding too when the user finishes editing.
(use-local-map (make-composed-keymap
embark-consult-revert-map
(current-local-map)))
(setq-local wgrep-header/footer-parser #'ignore)
(when (fboundp 'wgrep-setup) (wgrep-setup)))
(pop-to-buffer buf)))
(provide 'base-functions)
;;; .base-functions.el ends here