-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathepic-insert.el
More file actions
440 lines (403 loc) · 18.4 KB
/
epic-insert.el
File metadata and controls
440 lines (403 loc) · 18.4 KB
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
;;;; -*- mode:emacs-lisp;coding:utf-8 -*-
(defconst edit-buffer-name "epic-edit")
;;; ---------------------------------------------------------
;;;
(defun epic-insert-text-file (filename)
"Inserts and formats a text-file in current-buffer."
(when (get-buffer edit-buffer-name)
(kill-buffer edit-buffer-name))
(interactive "FFind file: ")
(switch-to-buffer (find-file-noselect filename))
;; ---------------------------------------------------------
;; Replace commands like enter, backspace etc.
;; ---------------------------------------------------------
;; " enter " and at end of line
(setq search-string-regexp "\\ enter\\ \\| enter$")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "\n "))
;; ---------------------------------------------------------
;; " enter("
(setq search-string-regexp "\\ enter(")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "\n("))
;; ---------------------------------------------------------
;; " enter…"
(setq search-string-regexp "\\ enter…")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "\n…"))
;; ---------------------------------------------------------
;; " enter-"
(setq search-string-regexp "\\ enter-")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "\n-"))
;; ---------------------------------------------------------
;; " enter?"
(setq search-string-regexp "\\ enter?")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "\n?"))
;; ---------------------------------------------------------
;; " enter."
(setq search-string-regexp "\\ enter.")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "\n."))
;; ---------------------------------------------------------
;; "Backspace"
(setq search-string-regexp "Backspace")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp ""))
;; ---------------------------------------------------------
;; "Location"
(setq search-string-regexp "Location\\ ")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "** "))
;; ---------------------------------------------------------
;; "Persona "
(setq search-string-regexp "Persona\\ ")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "*** "))
;; ---------------------------------------------------------
;; "Einschub"
(setq search-string-regexp "Einschub[\\ ]*")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp "("))
;; ---------------------------------------------------------
;; mutliple commas
(setq search-string-regexp ",,")
(while (search-in-buffer search-string-regexp)
(replace-in-buffer search-string-regexp ","))
;; ---------------------------------------------------------
;; Clean up
(setq lines-iterated 0)
(save-excursion
(setq previous-line "")
(goto-char (point-min))
(while (< (point) (point-max))
(setq line "")
(setq current-line (get-current-line))
(unless (or (eq (string-trim current-line) "")
(<= (string-width (string-trim current-line)) 1)) ; don't copy non-sense-lines with only one real char
(cond
;; ------------------------------------------------
;; heading-style
((is-heading-style current-line)
(setq line (epic-insert-get-clean-heading-style-line current-line)))
;; ------------------------------------------------
;; location-style
((is-location-style current-line)
(setq line (epic-insert-get-clean-location-style-line current-line))
(unless (or (<= (point) 1)
(is-heading-style previous-line))
(epic-insert-edit-buffer-insert-newline)))
;; ------------------------------------------------
;; persona-style
((epic-insert-is-persona-style current-line)
(setq line (epic-insert-get-clean-persona-style-line current-line))
(setq line (concat line "\n"))
(unless (is-location-style previous-line)
(setq line (concat "\n" line))))
;; ------------------------------------------------
;; insertion-style
((epic-insert-is-insertion-style current-line)
(setq line (epic-insert-get-clean-insertion-style-line current-line)))
;; ------------------------------------------------
;; standard-style
((epic-insert-is-standard-style current-line)
(setq line (epic-insert-get-clean-standard-style-line current-line)))
;; ------------------------------------------------
;; standard-continued-style
((epic-insert-is-standard-continued-style current-line)
(setq line (epic-insert-get-clean-standard-continued-style-line current-line))
(when (or (is-standard-style previous-line)
(is-insertion-style previous-line)
(and (not (string-suffix-p "..." previous-line))
(string-suffix-p "." previous-line))
(string-suffix-p "?" previous-line)
(string-suffix-p "!" previous-line)
(string-suffix-p ":" previous-line))
(setq line (string-trim line))
(setq line (string-capitalize-first-word line))
(setq line (concat indentation line)))))
;; -------------------------------------------------
;; insert into edit-buffer
(unless (string-equal line "")
(epic-insert-edit-buffer-insert line)
(setq previous-line line)))
(forward-line)
(setq lines-iterated (+ lines-iterated 1))
(message (concat "Finished - lines-iterated: " (number-to-string lines-iterated)))))
(switch-to-buffer edit-buffer-name))
;;; ---------------------------------------------------------
;;;
(defun string-capitalize-first-word (string)
"Capitalize first word of a string."
(setq temp-string (string-trim string))
(setq start (string-match " " string))
(if (eq start nil)
(setq temp-string (capitalize temp-string))
(setq first-word (substring temp-string 0 start))
(setq first-word (capitalize first-word))
(setq temp-string (concat first-word (substring temp-string start))))
temp-string)
;;; ---------------------------------------------------------
;;;
(defun epic-insert-edit-buffer-insert-newline ()
"Inserts line into edit buffer."
(with-current-buffer (get-buffer-create edit-buffer-name)
(newline)))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-edit-buffer-insert (line)
"Inserts line into edit buffer."
(with-current-buffer (get-buffer-create edit-buffer-name)
(insert line)(newline)))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-is-persona-style (line)
"Returns if line has style of persons."
(setq temp-line line)
(setq temp-line (string-trim temp-line))
(string-prefix-p "*** " temp-line))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-is-insertion-style (line)
"Returns if line is insertion-line."
(setq temp-line line)
(setq temp-line (string-trim temp-line))
(string-prefix-p "(" temp-line))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-is-standard-style (line)
"Returns if line is standard-line."
(eq (string-match "^[a-zA-ZöäüÖÄÜ]" line) 0))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-is-standard-continued-style (line)
"Returns if line is standard-continued-line."
(eq (string-match "\s+[a-zA-ZöäüÖÄÜ]" line) 0))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-get-clean-heading-style-line (line)
"Returns a cleaned heading-style-line."
(setq line (string-single-spaces line))
(setq line (string-trim line))
(setq line (upcase line))
line)
;;; ---------------------------------------------------------
;;;
(defun epic-insert-get-clean-location-style-line (line)
"Returns a cleaned heading-style-line."
(setq line (string-single-spaces line))
(setq line (string-trim line))
(when (string-match "[a-zA-ZöäüÖÄÜ]\\'" line)
(setq line (concat line ".")))
line)
;;; ---------------------------------------------------------
;;;
(defun epic-insert-get-clean-persona-style-line (line)
"Returns a cleaned heading-style-line."
(setq line (string-single-spaces line))
(setq line (string-trim line))
(setq line (substring line (+ (string-match persona-style-prefix line) (string-width persona-style-prefix))))
(setq line (string-capitalize-first-word line))
(setq line (concat persona-style-prefix line))
(when (string-match "[a-zA-ZöäüÖÄÜ]\\'" line)
(setq line (concat line ".")))
line)
;;; ---------------------------------------------------------
;;;
(defun epic-insert-get-clean-insertion-style-line (line)
"Returns a cleaned insertion-style-line."
(setq line (string-single-spaces line))
(setq line (string-trim line))
(setq line (string-capitalize-first-word line))
(setq line (concat indentation line))
(when (string-match "[a-zA-ZöäüÖÄÜ]\\'" line)
(setq line (concat line ".")))
(when (string-match "\\.\\|:\\'" line)
(setq line (concat line ")")))
line)
;;; ---------------------------------------------------------
;;;
(defun epic-insert-get-clean-standard-style-line (line)
"Returns a cleaned standard-style-line."
(setq line (string-single-spaces line))
(setq line (string-trim line))
(setq position (string-match "(" line))
(if position
(progn
(setq index 0)
(setq new-line "")
(while (< index (string-width line))
(setq current-char (aref line index))
(when (<= index position)
(setq current-char (upcase current-char)))
(setq new-line (concat new-line (char-to-string current-char)))
(setq index (+ index 1)))
(when (string-match "[a-zA-ZöäüÖÄÜ]\\'" new-line)
(setq new-line (concat new-line ")")))
(when (string-match ")\\'" new-line)
(setq new-line (concat new-line ".")))
(when (string-suffix-p "." new-line)
(unless (string-suffix-p ")." new-line)
(setq new-line (concat (substring new-line 0 (- (string-width new-line) 1)) ")."))))
new-line)
(setq position (string-match "\\." line))
(if position
(progn
(setq index 0)
(setq new-line "")
(while (< index (string-width line))
(setq current-char (aref line index))
(when (<= index position)
(setq current-char (upcase current-char)))
(setq new-line (concat new-line (char-to-string current-char)))
(setq index (+ index 1)))
new-line)
(message "Error in epic-insert-get-clean-standard-style-line (line): position is nil.")
(debug))))
;;; ---------------------------------------------------------
;;;
(defun epic-insert-get-clean-standard-continued-style-line (line)
"Returns a cleaned standard-continuedstyle-line."
(setq line (string-single-spaces line))
(setq line (string-trim line))
(setq line (concat indentation line)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ---------------------------------------------------------
;;
;; Tests
;;
;;; ---------------------------------------------------------
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; ---------------------------------------------------------
;;;
(ert-deftest test-string-capitalize-first-word ()
"Tests string-capitalize-first-word function."
(should (string-equal (string-capitalize-first-word "dies") "Dies"))
(should (string-equal (string-capitalize-first-word "dies ist ein Test.") "Dies ist ein Test."))
(should (string-equal (string-capitalize-first-word "... dies ist ein Test.") "... dies ist ein Test.")))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-is-persona-style ()
"Tests if line is persona-style."
(should (eq (epic-insert-is-persona-style "*** Persona") t))
(should (eq (epic-insert-is-persona-style " *** Persona") t))
(should (eq (epic-insert-is-persona-style "**** Persona") nil))
(should (eq (epic-insert-is-persona-style " ** Persona") nil)))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-is-insertion-style ()
"Tests if line is insertion."
(should (eq (epic-insert-is-insertion-style "(insertion") t))
(should (eq (epic-insert-is-insertion-style " (insertion") t))
(should (eq (epic-insert-is-insertion-style " (insertion") t))
(should (eq (epic-insert-is-insertion-style " (insertion") t))
(should (eq (epic-insert-is-insertion-style " (insertion") t))
(should (eq (epic-insert-is-insertion-style " insertion") nil))
(should (eq (epic-insert-is-insertion-style "inser(tion") nil)))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-is-standard-style ()
"Tests if line is standard-style."
(should (eq (epic-insert-is-standard-style "asdDFSDFfasdf") t))
(should (eq (epic-insert-is-standard-style "ASDFsertion") t))
(should (eq (epic-insert-is-standard-style " ASDFsertion") nil))
(should (eq (epic-insert-is-standard-style "(ASDFsertion") nil))
(should (eq (epic-insert-is-standard-style "-ASDFsertion") nil)))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-is-standard-continued-style ()
"Tests if line is standard-style."
(should (eq (epic-insert-is-standard-continued-style " asdDFSDFfasdf") t))
(should (eq (epic-insert-is-standard-continued-style " ASDFsertion") t))
(should (eq (epic-insert-is-standard-continued-style "ASDFsertion") nil))
(should (eq (epic-insert-is-standard-continued-style " (ASDFsertion") nil))
(should (eq (epic-insert-is-standard-continued-style " *ASDFsertion") nil)))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-get-clean-heading-style-line ()
"Tests for clean heading-style line."
(should (string-equal (epic-insert-get-clean-heading-style-line "* Full Heading") "* FULL HEADING"))
(should (string-equal (epic-insert-get-clean-heading-style-line "* Full Heading - further text") "* FULL HEADING - FURTHER TEXT"))
(should (string-equal (epic-insert-get-clean-heading-style-line " * Full Heading ") "* FULL HEADING"))
(should (string-equal (epic-insert-get-clean-heading-style-line " * Full HeaDIng ") "* FULL HEADING"))
(should (string-equal (epic-insert-get-clean-heading-style-line " * Full HeaDIng - furTHER text ") "* FULL HEADING - FURTHER TEXT")))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-get-clean-location-style-line ()
"Test for clean location-style line."
(should (string-equal (epic-insert-get-clean-location-style-line "** Location Location Location.") "** Location Location Location."))
(should (string-equal (epic-insert-get-clean-location-style-line "** Location Location. Location") "** Location Location. Location.")))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-get-clean-persona-style-line ()
"Test for clean persona-style line."
(should (string-equal (epic-insert-get-clean-persona-style-line "*** PERSONA. Persona ") "*** PERSONA. Persona."))
(should (string-equal (epic-insert-get-clean-persona-style-line " *** PERSONA. Persona ") "*** PERSONA. Persona.")))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-get-clean-insertion-style-line ()
"Test for clean insertion-style line."
(should (string-equal (epic-insert-get-clean-insertion-style-line " (insertion insertion. insertion") (concat indentation "(Insertion insertion. insertion.)")))
(should (string-equal (epic-insert-get-clean-insertion-style-line "(insertion insertion. insertion.") (concat indentation "(Insertion insertion. insertion.)"))))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-get-clean-standard-style-line ()
"Test for clean standard-style line."
(should (string-equal (epic-insert-get-clean-standard-style-line "abc (asdf") "ABC (asdf)."))
(should (string-equal (epic-insert-get-clean-standard-style-line "abc. asdf") "ABC. asdf"))
(should (string-equal (epic-insert-get-clean-standard-style-line "ABC (asdf") "ABC (asdf)."))
(should (string-equal (epic-insert-get-clean-standard-style-line "ABC. asdf asdf asdf") "ABC. asdf asdf asdf")))
;;; ---------------------------------------------------------
;;;
(ert-deftest epic-insert-test-get-clean-standard-continued-style-line ()
"Returns a formatted standard-continued-style line."
(should (string-equal (epic-insert-get-clean-standard-continued-style-line " asdf asdf asdf") " asdf asdf asdf"))
(should (string-equal (epic-insert-get-clean-standard-continued-style-line " asdf asdf asdf") " asdf asdf asdf")))
;;; ---------------------------------------------------------
;;;
(defun string-single-spaces (s)
"Replaces multiple spaces with single space."
(setq s (replace-regexp-in-string "\s+" " " s))
s)
;;
;; [From: http://ergoemacs.org/emacs/modernization_elisp_lib_problem.html]
;;
;;; ---------------------------------------------------------
;;;
(defun string-trim-left (s)
"Remove whitespace at the beginning of S."
(if (string-match "\\`[ \t\n\r]+" s)
(replace-match "" t t s)
s))
;;; ---------------------------------------------------------
;;;
(defun string-trim-right (s)
"Remove whitespace at the end of S."
(if (string-match "[ \t\n\r]+\\'" s)
(replace-match "" t t s)
s))
;;; ---------------------------------------------------------
;;;
(defun string-trim (s)
"Remove whitespace at the beginning and end of S."
(string-trim-left (string-trim-right s)))
;;
;; [From: http://ergoemacs.org/emacs/elisp_find_replace_text.html]
;;
;;; ---------------------------------------------------------
;;;
(defun search-in-buffer (search-string-regexp)
"Search in buffer for regexp."
(let ((case-fold-search t)) ; or nil
(goto-char (point-min))
(search-forward-regexp search-string-regexp nil t)))
;;; ---------------------------------------------------------
;;;
(defun replace-in-buffer (search-string-regexp replace-string)
"Idiom for string replacement in current buffer."
(let ((case-fold-search t)) ; or nil
(goto-char (point-min))
;; if you need regexp, use search-forward-regexp
(while (search-forward-regexp search-string-regexp nil t)
(replace-match replace-string))))