mirror of https://github.com/Chizi123/.emacs.d.git

Chizi123
2019-01-06 d070a7b257cf0c42efe058bc520a585fb166f46d
commit | author | age
8c2ab1 1 (provide 'setup-editing)
C 2
3 ;; GROUP: Editing -> Editing Basics
4 (setq global-mark-ring-max 5000         ; increase mark ring to contains 5000 entries
5       mark-ring-max 5000                ; increase kill ring to contains 5000 entries
6       mode-require-final-newline t      ; add a newline to end of file
7       )
8
9 (setq c-default-style "linux")
10
11 (set-terminal-coding-system 'utf-8)
12 (set-keyboard-coding-system 'utf-8)
13 (set-language-environment "UTF-8")
14 (prefer-coding-system 'utf-8)
15
16 (setq-default indent-tabs-mode t)
17 (delete-selection-mode)
18 (global-set-key (kbd "RET") 'newline-and-indent)
19
20 ;; GROUP: Editing -> Killing
21 (setq
22  kill-ring-max 5000 ; increase kill-ring capacity
23  kill-whole-line t  ; if NIL, kill whole line and move the next line up
24  )
25
26 ;; display whitespace characters
27 (global-set-key (kbd "C-c w") 'whitespace-mode)
28
29 ;; show important whitespace in diff-mode
30 (add-hook 'diff-mode-hook (lambda ()
31                             (setq-local whitespace-style
32                                         '(face
33                                           tabs
34                                           tab-mark
35                                           spaces
36                                           space-mark
37                                           trailing
38                                           indentation::space
39                                           indentation::tab
40                                           newline
41                                           newline-mark))
42                             (whitespace-mode 1)))
43
44
45 (defun prelude-move-beginning-of-line (arg)
46   "Move point back to indentation of beginning of line.
47
48 Move point to the first non-whitespace character on this line.
49 If point is already there, move to the beginning of the line.
50 Effectively toggle between the first non-whitespace character and
51 the beginning of the line.
52
53 If ARG is not nil or 1, move forward ARG - 1 lines first. If
54 point reaches the beginning or end of the buffer, stop there."
55   (interactive "^p")
56   (setq arg (or arg 1))
57
58   ;; Move lines first
59   (when (/= arg 1)
60     (let ((line-move-visual nil))
61       (forward-line (1- arg))))
62
63   (let ((orig-point (point)))
64     (back-to-indentation)
65     (when (= orig-point (point))
66       (move-beginning-of-line 1))))
67
68 (global-set-key (kbd "C-a") 'prelude-move-beginning-of-line)
69
70 (defadvice kill-ring-save (before slick-copy activate compile)
71   "When called interactively with no active region, copy a single
72 line instead."
73   (interactive
74    (if mark-active (list (region-beginning) (region-end))
75      (message "Copied line")
76      (list (line-beginning-position)
77            (line-beginning-position 2)))))
78
79 (defadvice kill-region (before slick-cut activate compile)
80   "When called interactively with no active region, kill a single
81   line instead."
82   (interactive
83    (if mark-active (list (region-beginning) (region-end))
84      (list (line-beginning-position)
85            (line-beginning-position 2)))))
86
87 ;; kill a line, including whitespace characters until next non-whiepsace character
88 ;; of next line
89 (defadvice kill-line (before check-position activate)
90   (if (member major-mode
91               '(emacs-lisp-mode scheme-mode lisp-mode
92                                 c-mode c++-mode objc-mode
93                                 latex-mode plain-tex-mode))
94       (if (and (eolp) (not (bolp)))
95           (progn (forward-char 1)
96                  (just-one-space 0)
97                  (backward-char 1)))))
98
99 ;; taken from prelude-editor.el
100 ;; automatically indenting yanked text if in programming-modes
101 (defvar yank-indent-modes
102   '(LaTeX-mode TeX-mode)
103   "Modes in which to indent regions that are yanked (or yank-popped).
104 Only modes that don't derive from `prog-mode' should be listed here.")
105
106 (defvar yank-indent-blacklisted-modes
107   '(python-mode slim-mode haml-mode)
108   "Modes for which auto-indenting is suppressed.")
109
110 (defvar yank-advised-indent-threshold 1000
111   "Threshold (# chars) over which indentation does not automatically occur.")
112
113 (defun yank-advised-indent-function (beg end)
114   "Do indentation, as long as the region isn't too large."
115   (if (<= (- end beg) yank-advised-indent-threshold)
116       (indent-region beg end nil)))
117
118 (defadvice yank (after yank-indent activate)
119   "If current mode is one of 'yank-indent-modes,
120 indent yanked text (with prefix arg don't indent)."
121   (if (and (not (ad-get-arg 0))
122            (not (member major-mode yank-indent-blacklisted-modes))
123            (or (derived-mode-p 'prog-mode)
124                (member major-mode yank-indent-modes)))
125       (let ((transient-mark-mode nil))
126         (yank-advised-indent-function (region-beginning) (region-end)))))
127
128 (defadvice yank-pop (after yank-pop-indent activate)
129   "If current mode is one of `yank-indent-modes',
130 indent yanked text (with prefix arg don't indent)."
131   (when (and (not (ad-get-arg 0))
132              (not (member major-mode yank-indent-blacklisted-modes))
133              (or (derived-mode-p 'prog-mode)
134                  (member major-mode yank-indent-modes)))
135     (let ((transient-mark-mode nil))
136       (yank-advised-indent-function (region-beginning) (region-end)))))
137
138 ;; prelude-core.el
139 (defun prelude-duplicate-current-line-or-region (arg)
140   "Duplicates the current line or region ARG times.
141 If there's no region, the current line will be duplicated. However, if
142 there's a region, all lines that region covers will be duplicated."
143   (interactive "p")
144   (pcase-let* ((origin (point))
145                (`(,beg . ,end) (prelude-get-positions-of-line-or-region))
146                (region (buffer-substring-no-properties beg end)))
147     (-dotimes arg
148       (lambda (n)
149         (goto-char end)
150         (newline)
151         (insert region)
152         (setq end (point))))
153     (goto-char (+ origin (* (length region) arg) arg))))
154
155 ;; prelude-core.el
156 (defun indent-buffer ()
157   "Indent the currently visited buffer."
158   (interactive)
159   (indent-region (point-min) (point-max)))
160
161 ;; prelude-editing.el
162 (defcustom prelude-indent-sensitive-modes
163   '(coffee-mode python-mode slim-mode haml-mode yaml-mode)
164   "Modes for which auto-indenting is suppressed."
165   :type 'list)
166
167 (defun indent-region-or-buffer ()
168   "Indent a region if selected, otherwise the whole buffer."
169   (interactive)
170   (unless (member major-mode prelude-indent-sensitive-modes)
171     (save-excursion
172       (if (region-active-p)
173           (progn
174             (indent-region (region-beginning) (region-end))
175             (message "Indented selected region."))
176         (progn
177           (indent-buffer)
178           (message "Indented buffer.")))
179       (whitespace-cleanup))))
180
181 (global-set-key (kbd "C-c i") 'indent-region-or-buffer)
182
183 ;; add duplicate line function from Prelude
184 ;; taken from prelude-core.el
185 (defun prelude-get-positions-of-line-or-region ()
186   "Return positions (beg . end) of the current line
187 or region."
188   (let (beg end)
189     (if (and mark-active (> (point) (mark)))
190         (exchange-point-and-mark))
191     (setq beg (line-beginning-position))
192     (if mark-active
193         (exchange-point-and-mark))
194     (setq end (line-end-position))
195     (cons beg end)))
196
197 (defun kill-default-buffer ()
198   "Kill the currently active buffer -- set to C-x k so that users are not asked which buffer they want to kill."
199   (interactive)
200   (let (kill-buffer-query-functions) (kill-buffer)))
201
202 (global-set-key (kbd "C-x k") 'kill-default-buffer)
203
204 ;; smart openline
205 (defun prelude-smart-open-line (arg)
206   "Insert an empty line after the current line.
207 Position the cursor at its beginning, according to the current mode.
208 With a prefix ARG open line above the current line."
209   (interactive "P")
210   (if arg
211       (prelude-smart-open-line-above)
212     (progn
213       (move-end-of-line nil)
214       (newline-and-indent))))
215
216 (defun prelude-smart-open-line-above ()
217   "Insert an empty line above the current line.
218 Position the cursor at it's beginning, according to the current mode."
219   (interactive)
220   (move-beginning-of-line nil)
221   (newline-and-indent)
222   (forward-line -1)
223   (indent-according-to-mode))
224
225 (global-set-key (kbd "C-o") 'prelude-smart-open-line)
226 (global-set-key (kbd "M-o") 'open-line)