1
+ ; ;----------------------------------------------------------------------------
2
+ ; ; Overwrite text when the selection is active
3
+ ; ;----------------------------------------------------------------------------
4
+ (delete-selection-mode 1 )
5
+
6
+
7
+ ; ;----------------------------------------------------------------------------
8
+ ; ; Show and edit all lines matching a regex
9
+ ; ;----------------------------------------------------------------------------
10
+ (require 'all )
11
+
12
+
13
+ ; ;----------------------------------------------------------------------------
14
+ ; ; Don't disable case-change functions
15
+ ; ;----------------------------------------------------------------------------
16
+ (put 'upcase-region 'disabled nil )
17
+ (put 'downcase-region 'disabled nil )
18
+
19
+
20
+ ; ;----------------------------------------------------------------------------
21
+ ; ; Rectangle selections
22
+ ; ;----------------------------------------------------------------------------
23
+ (setq cua-enable-cua-keys nil ) ; ; only for rectangles, with C-RET
24
+ (cua-mode t )
25
+
26
+
27
+ ; ;----------------------------------------------------------------------------
28
+ ; ; Conversion of line endings
29
+ ; ;----------------------------------------------------------------------------
30
+ ; ; Can also use "C-x ENTER f dos" / "C-x ENTER f unix" (set-buffer-file-coding-system)
31
+ (require 'eol-conversion )
32
+
33
+
34
+ ; ;----------------------------------------------------------------------------
35
+ ; ; Handy key bindings
36
+ ; ;----------------------------------------------------------------------------
37
+ ; ; To be able to M-x without meta
38
+ (global-set-key (kbd " C-x C-m" ) 'execute-extended-command )
39
+
40
+ (global-set-key (kbd " C-c j" ) 'join-line )
41
+ (global-set-key (kbd " C-c J" ) (lambda () (interactive ) (join-line 1 )))
42
+ (global-set-key (kbd " M-T" ) 'transpose-lines )
43
+
44
+ (defun duplicate-line ()
45
+ (interactive )
46
+ (save-excursion
47
+ (let ((line-text (buffer-substring-no-properties
48
+ (line-beginning-position )
49
+ (line-end-position ))))
50
+ (move-end-of-line 1 )
51
+ (newline )
52
+ (insert line-text))))
53
+
54
+ (global-set-key (kbd " C-c p" ) 'duplicate-line )
55
+
56
+ ; ; Train myself to use M-f and M-b instead
57
+ (global-unset-key [M-left])
58
+ (global-unset-key [M-right])
59
+
60
+
61
+ ; ;----------------------------------------------------------------------------
62
+ ; ; Shift lines up and down
63
+ ; ;----------------------------------------------------------------------------
64
+ (defun move-text-internal (arg )
65
+ (cond
66
+ ((and mark-active transient-mark-mode)
67
+ (if (> (point ) (mark ))
68
+ (exchange-point-and-mark ))
69
+ (let ((column (current-column ))
70
+ (text (delete-and-extract-region (point ) (mark ))))
71
+ (forward-line arg)
72
+ (move-to-column column t )
73
+ (set-mark (point ))
74
+ (insert text)
75
+ (exchange-point-and-mark )
76
+ (setq deactivate-mark nil )))
77
+ (t
78
+ (let ((column (current-column )))
79
+ (beginning-of-line )
80
+ (when (or (> arg 0 ) (not (bobp )))
81
+ (forward-line )
82
+ (when (or (< arg 0 ) (not (eobp )))
83
+ (transpose-lines arg))
84
+ (forward-line -1 ))
85
+ (move-to-column column t )))))
86
+
87
+ (defun move-text-down (arg )
88
+ " Move region (transient-mark-mode active) or current line
89
+ arg lines down."
90
+ (interactive " *p" )
91
+ (move-text-internal arg))
92
+
93
+ (defun move-text-up (arg )
94
+ " Move region (transient-mark-mode active) or current line
95
+ arg lines up."
96
+ (interactive " *p" )
97
+ (move-text-internal (- arg)))
98
+
99
+
100
+ (global-set-key [M-S-up] 'move-text-up )
101
+ (global-set-key [M-S-down] 'move-text-down )
102
+
103
+
104
+
105
+ ; ;----------------------------------------------------------------------------
106
+ ; ; Cut/copy the current line if no region is active
107
+ ; ;----------------------------------------------------------------------------
108
+ (defadvice kill-ring-save (before slick-copy activate compile) " When called
109
+ interactively with no active region, copy a single line instead."
110
+ (interactive (if mark-active (list (region-beginning ) (region-end ))
111
+ (message " Copied line " )
112
+ (list (line-beginning-position )
113
+ (line-beginning-position 2 )))))
114
+
115
+ (defadvice kill-region (before slick-cut activate compile)
116
+ " When called interactively with no active region, kill a single line instead."
117
+ (interactive
118
+ (if mark-active (list (region-beginning ) (region-end ))
119
+ (message " Killed line " )
120
+ (list (line-beginning-position )
121
+ (line-beginning-position 2 )))))
122
+
123
+
124
+ ; ;----------------------------------------------------------------------------
125
+ ; ; Easily count words (http://emacs-fu.blogspot.com/2009/01/counting-words.html)
126
+ ; ;----------------------------------------------------------------------------
127
+ (defun count-words (&optional begin end )
128
+ " count words between BEGIN and END (region); if no region defined, count words in buffer"
129
+ (interactive " r" )
130
+ (let ((b (if mark-active begin (point-min )))
131
+ (e (if mark-active end (point-max ))))
132
+ (message " Word count: %s " (how-many " \\ w+" b e))))
133
+
134
+
135
+
136
+
137
+ (provide 'init-editing-utils )
0 commit comments