-
Notifications
You must be signed in to change notification settings - Fork 4
/
emergency.el
316 lines (266 loc) · 9.96 KB
/
emergency.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
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
;;; init.el --- -*- lexical-binding: t; -*-
;; Copyright (C) 2022 Umar Ahmad
;; Created: May 05, 2022
;; Modified: May 05, 2022
;; Version: 0.0.1
;; Author: Umar Ahmad
;;; Commentary:
;; This file is never loaded in normal use and is meant to be used as a minimal init.el
;; in another Emacs where I don't have the liberty to clone my full config.
;; These are the principles for this file:
;; - Single file: So it's easy to copy paste - email to someone
;; - Only uses Emacs defaults - no fluff packages
;; - Assumes the latest stable Emacs version
;; - No dependency on extra packages unless absolutely necessary (for eg language major modes - but only when visiting those files)
;; - Future: can include some helpful utilities like flymake checkers and dumb jump on request basis.
;;
;;; Code:
(defconst CACHE-DIR (expand-file-name "cache/" user-emacs-directory))
(defconst RES-DIR (expand-file-name "resources/" user-emacs-directory))
(defconst IS-MAC (eq system-type 'darwin))
(defconst IS-LINUX (eq system-type 'gnu/linux))
(defconst IS-TERM (not (display-graphic-p)))
(unless IS-TERM
(define-key input-decode-map [?\C-m] [C-m])
(define-key input-decode-map [?\C-i] [C-i]))
(tool-bar-mode -1)
(scroll-bar-mode -1)
(menu-bar-mode -1)
(blink-cursor-mode -1)
(electric-pair-mode t)
(size-indication-mode t)
(line-number-mode t)
(column-number-mode t)
(display-line-numbers-mode t)
;; (setq modus-themes-subtle-line-numbers nil)
(setq modus-themes-mode-line '((padding . 4)))
(setq inhibit-startup-screen t
initial-scratch-message "")
(load-theme 'modus-operandi t)
(set-face-attribute 'default nil :height 130)
(setq-default line-spacing 0.2)
;; Keybindings
(global-set-key (kbd "M-p") 'project-find-file)
(global-set-key (kbd "M-/") 'hippie-expand)
(global-set-key (kbd "M-;") 'comment-or-uncomment-region-or-line)
(global-set-key (kbd "C-c s s") 'project-find-regexp)
(global-set-key (kbd "C-=") 'smart-mark-sexp)
(global-set-key (kbd "C-+") 'mark-this-sexp)
(global-set-key (kbd "C-:") 'other-window)
(global-set-key (kbd "C-a") 'beginning-of-line-or-indentation)
(global-set-key (kbd "M-[") 'unwrap-sexp)
(global-set-key (kbd "M-<up>") 'move-line-up)
(global-set-key (kbd "M-<down>") 'move-line-down)
(global-set-key (kbd "C-^") 'top-join-line)
(global-set-key (kbd "C-c d") 'duplicate-current-line-or-region)
(global-set-key (kbd "C-`") 'pop-eshell)
(global-set-key (kbd "<C-m> a") 'rename-all-occurences)
(when IS-MAC
(setq mac-command-modifier 'meta
;; mac-option-modifier 'control
ns-option-modifier 'super))
(fido-mode t)
(fido-vertical-mode t)
(setq project-list-file (expand-file-name "projects" CACHE-DIR))
(defalias 'yes-or-no-p 'y-or-n-p)
(setq select-enable-clipboard t ;; Enabled emacs to use system clipboard
select-enable-primary nil ;; Disable Copy on selection
display-time-default-load-average nil
save-interprogram-paste-before-kill t
kill-ring-max 200
kill-do-not-save-duplicates t
apropos-do-all t
use-dialog-box nil
ring-bell-function 'ignore
mouse-yank-at-point t
x-underline-at-descent-line t
require-final-newline t
;; scrolling
fast-but-imprecise-scrolling t
auto-window-vscroll nil
scroll-preserve-screen-position t
kill-do-not-save-duplicates t
highlight-nonselected-windows nil
ediff-window-setup-function 'ediff-setup-windows-plain)
(setq indicate-buffer-boundaries nil
indicate-empty-lines nil)
(setq enable-recursive-minibuffers t)
(setq hippie-expand-try-functions-list '(try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-file-name-partially
try-complete-file-name
try-expand-all-abbrevs
try-expand-line
try-expand-list
try-complete-lisp-symbol-partially
try-complete-lisp-symbol))
(setq search-highlight t)
(setq search-whitespace-regexp ".*?")
(setq isearch-lax-whitespace t)
(setq isearch-regexp-lax-whitespace nil)
(setq isearch-lazy-highlight t)
(setq isearch-lazy-count t)
(setq lazy-count-prefix-format nil)
(setq lazy-count-suffix-format " (%s/%s)")
(setq isearch-yank-on-move 'shift)
(setq isearch-allow-scroll 'unlimited)
(setq auto-save-list-file-prefix (concat CACHE-DIR "auto-save-list/.saves-")
auto-save-file-name-transforms `((".*" ,auto-save-list-file-prefix t))
backup-directory-alist `(("" . ,(concat CACHE-DIR "backups/per-save"))))
;; Utility functions
(defadvice kill-ring-save (before slick-copy activate compile)
"When called interactively with no active region, copy a single line instead."
(interactive (if mark-active (list (region-beginning) (region-end))
(message "Line copied") (list (line-beginning-position) (line-beginning-position 2)))))
(defadvice kill-region (before slick-cut activate compile)
"When called interactively with no active region, kill a single line instead."
(interactive
(if mark-active (list (region-beginning) (region-end))
(list (line-beginning-position)
(line-beginning-position 2)))))
(defun comment-or-uncomment-region-or-line ()
"Comment a line or region."
(interactive)
(let (beg end)
(if (region-active-p)
(setq beg (region-beginning) end (region-end))
(setq beg (line-beginning-position) end (line-end-position)))
(comment-or-uncomment-region beg end)
(forward-line)))
(defun smart-mark-sexp()
"Poor man's expand region"
(interactive)
(if (region-active-p)
(expand-sexp)
(mark-this-sexp)))
(defun expand-sexp()
(interactive)
(catch 'ex (deactivate-mark)
(let (beg start end inner )
(setq beg (point))
(setq inner t)
(condition-case nil (backward-up-list 1 t nil)
(error (progn
(push-mark (point-min))
(goto-char (point-max))
(activate-mark)
(throw 'ex 1))))
(if (eq beg (1+ (point)))
(setq inner nil))
(setq start (if inner (1+ (point)) (point)))
(forward-sexp)
(setq end (if inner (1- (point)) (point)))
(goto-char end)
(push-mark)
(goto-char start))
(activate-mark)))
(defun mark-this-sexp()
(interactive)
(ignore-errors (backward-sexp))
(mark-sexp))
(defun unwrap-sexp()
(interactive)
(save-excursion
(let (start end)
(catch 'ex
(condition-case nil (backward-up-list 1 t nil)
(error (throw 'ex 1)))
(setq start (point))
(forward-sexp)
(backward-delete-char 1)
(goto-char start)
(delete-char 1)))
(indent-sexp)))
(defun beginning-of-line-or-indentation ()
"Move to beginning of line, or indentation."
(interactive)
(let ((pt (point)))
(beginning-of-line-text)
(when (eq pt (point))
(beginning-of-line))))
(defun move-line(&optional dir)
(let (line-content)
(setq line-content (thing-at-point 'line t))
(delete-current-line)
(forward-line (if (eq dir 'up) -1 1))
(insert line-content)
(forward-line -1)
(indent-for-tab-command)))
(defun move-line-up()
(interactive)
(move-line 'up))
(defun move-line-down()
(interactive)
(move-line 'down))
(defun top-join-line()
(interactive)
(delete-indentation -1))
(defun delete-current-line ()
(save-excursion
(delete-region
(progn (forward-visible-line 0) (point))
(progn (forward-visible-line 1) (point)))))
(defun get-positions-of-line-or-region ()
"Return positions (beg . end) of the current line or region."
(let (beg end)
(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))
(cons beg end)))
(defun duplicate-current-line-or-region ()
"Duplicates the current line or region.
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)
(pcase-let* ((origin (point))
(`(,beg . ,end) (get-positions-of-line-or-region))
(region (buffer-substring-no-properties beg end)))
(goto-char end)
(newline)
(insert region)
(setq end (point))))
(defun pop-eshell()
(interactive)
(require 'eshell)
(if (eq major-mode 'eshell-mode)
(progn (bury-buffer)
(delete-window))
(let ((c-directory default-directory))
(split-window-below)
(other-window 1 nil)
(eshell)
(message "Switching to %s" c-directory)
(eshell/cd c-directory))))
(defun rename-all-occurences()
(interactive)
(let ((word (thing-at-point 'symbol t))
(final ())
(case-fold-search nil))
(when word
(setq final (read-string (format "Replace %s with : " word)))
(query-replace word final t (point-min) (point-max)))))
;; Only install very critical packages on need basis
(setq file-packages
'(("php" . (php-mode))
("go" . (go-mode))))
(defun check-essential-packages()
(let (file-name extension ipackages)
(setq file-name (buffer-file-name (current-buffer)))
(when file-name
(setq extension (file-name-extension file-name))
(mapc (lambda(o)
(when (equal extension (car o))
(setq ipackages (cdr o))
(mapc (lambda(p)
(if (not (package-installed-p p))
(progn
(message "Installing package %S" p)
(package-install p))))
ipackages)))
file-packages))))
(add-hook 'find-file-hook 'check-essential-packages)
;;; init.el ends here