summaryrefslogtreecommitdiff
path: root/init.el
blob: ce258e63441b2ba548512366dae995bd4954ca97 (about) (plain)
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
;; Turn off mouse interface early in startup to avoid momentary display
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))

;; No splash screen please ...
(setq inhibit-startup-message t)

;; disable mouse scrolling
(mouse-wheel-mode -1)

(add-to-list 'load-path user-emacs-directory)
(add-to-list 'load-path "/usr/share/emacs/site-lisp/")

(require 'cask "~/.cask/cask.el")
(cask-initialize)

(require 'better-defaults)

;; Keep emacs Custom-settings in separate file
(setq custom-file (expand-file-name "custom.el" user-emacs-directory))
(load custom-file)

(require 'package)
(add-to-list 'package-archives
    '("melpa"     . "http://melpa.milkbox.net/packages/")
    '("marmalade" . "http://marmalade-repo.org/packages/"))
(package-initialize)

; sublime-themes must be installed
(load-theme 'spolsky t)
(set-cursor-color "White")
(setq evil-default-cursor '("white" box))
(set-background-color "#181818")

;; Swap C-t and C-x, so it's easier to type on Dvorak layout
(keyboard-translate ?\C-t ?\C-x)
(keyboard-translate ?\C-x ?\C-t)

;; kill current buffer
(global-set-key (kbd "C-x C-k") (lambda ()
                                  (interactive)
                                  (kill-buffer (current-buffer))))
(global-set-key (kbd "C-c r") 'replace-regexp)


; smart mode line
(setq sml/theme 'respectful)
(sml/setup)

(require 'smex)
(smex-initialize)
(global-set-key (kbd "M-x") 'smex)
(global-set-key (kbd "M-X") 'smex-major-mode-commands)
(global-set-key (kbd "<menu>") 'smex)
(global-set-key (kbd "S-<menu>") 'smex-major-mode-commands)

;; expand region
(global-set-key (kbd "M-@") 'er/expand-region)

; C-d on an empty line in the shell terminates the process.
(defun comint-delchar-or-eof-or-kill-buffer (arg)
  (interactive "p")
  (if (null (get-buffer-process (current-buffer)))
      (kill-buffer)
    (comint-delchar-or-maybe-eof arg)))

(add-hook 'shell-mode-hook
          (lambda ()
            (define-key shell-mode-map
              (kbd "C-d") 'comint-delchar-or-eof-or-kill-buffer)))
;; TODO: this isn't working
(add-hook 'term-mode-hook
          (lambda ()
            (define-key term-mode-map
              (kbd "C-d") 'comint-delchar-or-eof-or-kill-buffer)))

(add-hook 'haskell-mode-hook
          (lambda ()
            (turn-on-haskell-indentation)
            (turn-on-haskell-doc)))

(require 'tramp)
(setq tramp-default-method "ssh")

;; for root connections to remote hosts, log in via ssh with normal
;; user account first, then su/sudo to root
(add-to-list 'tramp-default-proxies-alist
             '(".*" "\\`root\\'" "/ssh:%h:"))

;; Backup (file~) disabled and auto-save (#file#) locally to prevent delays in editing remote files
(add-to-list 'backup-directory-alist
             (cons tramp-file-name-regexp nil))
(setq tramp-auto-save-directory temporary-file-directory)
(setq tramp-verbose 3)

;; Write backup files to own directory
;; TODO: conflicts with previous manipulation of backup-directory-alist
(setq backup-directory-alist
      `(("." . ,(expand-file-name
                 (concat user-emacs-directory "backups")))))

;; Make backups of files, even when they're in version control
(setq vc-make-backup-files t)

(autoload 'markdown-mode "markdown-mode"
   "Major mode for editing Markdown files" t)
(autoload 'js2-mode "js2-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

;; full screen magit-status
(require 'magit)
(defadvice magit-status (around magit-fullscreen activate)
  (window-configuration-to-register :magit-fullscreen)
  ad-do-it
  (delete-other-windows))

(defun magit-quit-session ()
  "Restores the previous window configuration and kills the magit buffer"
  (interactive)
  (kill-buffer)
  (jump-to-register :magit-fullscreen))

(defun magit-toggle-whitespace ()
  "Toggles git option -w"
  (interactive)
  (if (member "-w" magit-diff-options)
      (magit-dont-ignore-whitespace)
    (magit-ignore-whitespace)))

(defun magit-ignore-whitespace ()
  "Adds git option -w"
  (interactive)
  (add-to-list 'magit-diff-options "-w")
  (magit-refresh))

(defun magit-dont-ignore-whitespace ()
  "Removes git option -w"
  (interactive)
  (setq magit-diff-options (remove "-w" magit-diff-options))
  (magit-refresh))

(define-key magit-status-mode-map (kbd "q") 'magit-quit-session)
(define-key magit-status-mode-map (kbd "W") 'magit-toggle-whitespace)


;; ido mode
(require 'ido)
(ido-mode 1)
;; hit ~ to go straight to the home directory in ido mode
(add-hook 'ido-setup-hook
 (lambda ()
   ;; Go straight home
   (define-key ido-file-completion-map
     (kbd "~")
     (lambda ()
       (interactive)
       (if (looking-back "/")
           (insert "~/")
         (call-interactively 'self-insert-command))))))

;; Use ido everywhere
(require 'ido-ubiquitous)
(ido-ubiquitous-mode 1)
;; use ido for yes/no questions
(ido-yes-or-no-mode 1)

;; fullscreen mode with F11
(fullscreen-mode 1)
(global-set-key (kbd "<f11>") 'fullscreen-mode-fullscreen-toggle)

(setq scss-compile-at-save nil)


(global-whitespace-mode 1)
(set-face-attribute 'whitespace-space nil :background nil :foreground "gray20")
(set-face-attribute 'whitespace-newline nil :background nil :foreground "gray20")
(setq whitespace-style
       '(face spaces tabs newline space-mark tab-mark newline-mark))
(setq whitespace-display-mappings
      ;; all numbers are Unicode codepoint in decimal. try
      ;; (insert-char 182 ) to see it
      '(
        (space-mark 32 [183] [46]) ; 32 SPACE, 183 MIDDLE DOT 「·」,
                                   ; 46 FULL STOP 「.」
        (newline-mark 10 [182 10]) ; 10 LINE FEED
        (tab-mark 9 [9655 9] [92 9]) ; 9 TAB, 9655 WHITE
                                     ; RIGHT-POINTING TRIANGLE 「▷」
        ))

(desktop-save-mode t)

;; use cat as the pager in shell mode, because shell-mode is not an ANSI terminal
(setenv "PAGER" "cat")

;; ediff settings
(setq ediff-diff-options "-w")

;; TODO: what does this do?
(put 'dired-find-alternate-file 'disabled nil)

;; cause Emacs to fully redraw the display *before* it processes queued input events.
(setq redisplay-dont-pause t)

;; fewer backslashes in regexp builder
(require 're-builder)
(setq reb-re-syntax 'string)

;; remove prompt on killing process buffer
(setq kill-buffer-query-functions
  (remq 'process-kill-buffer-query-function
         kill-buffer-query-functions))

;; display tool tips in echo area only
(tooltip-mode -1)
(setq tooltip-use-echo-area t)

;; enable narrow-to-region feature
(put 'narrow-to-region 'disabled nil)


;; http://blog.vivekhaldar.com/post/4809065853/dotemacs-extract-interactively-change-font-size
(defun zoom-in ()
  "Increase font size by 10 points"
  (interactive)
  (set-face-attribute 'default nil
                      :height
                      (+ (face-attribute 'default :height)
                         10)))

(defun zoom-out ()
  "Decrease font size by 10 points"
  (interactive)
  (set-face-attribute 'default nil
                      :height
                      (- (face-attribute 'default :height)
                         10)))

;; change font size, interactively
(global-set-key (kbd "C-.") 'zoom-in)
(global-set-key (kbd "C-,") 'zoom-out)

(require 'shell-switcher)
(setq shell-switcher-mode t)
(add-hook 'eshell-mode-hook 'shell-switcher-manually-register-shell)

(global-set-key (kbd "M-o") 'other-window)

;; ---------------- rekado minor mode --------------------
(defvar rekado-permanent-mode-q nil "indicates whether permanent keys are active")
(defvar rekado-mode-map (make-sparse-keymap) "keymap for rekado-mode")
(defun rekado-mode-define-keys (define-permanent-keys)
  "Defines key bindings for rekado-mode.  If the argument is t,
the edit mode keys will be bound as well."
  (let* ((modal-keys
          '(
            ("a" . move-beginning-of-line)
            ("e" . move-end-of-line)

            ;; define JKLI (HTNC in Dvorak) as arrow keys
            ("c" . previous-line)
            ("t" . next-line)
            ("h" . backward-char)
            ("n" . forward-char)

            ;; add meta key to move more quickly
            ("M-c" . (lambda () (interactive)
                       (previous-line 5)))
            ("M-t" . (lambda () (interactive)
                       (next-line 5)))
            ("M-h" . (lambda () (interactive)
                       (backward-char 5)))
            ("M-n" . (lambda () (interactive)
                       (forward-char 5)))

            ;; define u/o (g/r in Dvorak) as backward/forward-word
            ("g" . backward-word)
            ("r" . forward-word)
            ;; define U/O (G/R in Dvorak) to kill word in direction
            ("G" . backward-kill-word)
            ("R" . kill-word)

            ;; TODO: add advice for C, T, H and N (upper case arrows in Dvorak) to mark and start selection
            ;; jump to character
            ("f" . ace-jump-char-mode)
            ("i" . rekado-permanent-mode-exit)
            ("k" . (lambda ()
                     (interactive)
                     (move-beginning-of-line nil)
                     (kill-whole-line)))
            ;; undo as in vim
            ("u" . undo)
            ;; repeat as in vim
            ("." . repeat)
            ;; paste as in vim
            ("p" . (lambda ()
                     (interactive)
                     (next-line)
                     (beginning-of-line)
                     (yank)))
            ("P" . (lambda ()
                     (interactive)
                     (beginning-of-line)
                     (yank)))
            ;; copy as in vim
            ("y" . kill-ring-save)
            ("s" . isearch-forward-regexp)
            ;; open line below/above as in vim
            ("o" . (lambda ()
                     (interactive)
                     (open-line-below)
                     (rekado-permanent-mode-exit)))
            ("O" . (lambda ()
                     (interactive)
                     (open-line-above)
                     (rekado-permanent-mode-exit)))
            ;; join lines (like J in vim)
            ("j" . (lambda ()
                     (interactive)
                     (join-line -1)))
            ;; change till the end of the line
            ("C" . (lambda ()
                     (interactive)
                     (kill-line)
                     (rekado-permanent-mode-exit)))
            )))
    (progn
      ;; toggle permanent mode
      (define-key rekado-mode-map
        (kbd "<C-return>")
        'rekado-permanent-mode-toggle)

      ;; toggle permanent mode
      (define-key rekado-mode-map
        (kbd "<M-SPC>")
        'rekado-permanent-mode-toggle)

      (if define-permanent-keys
          ;; define permanent keys
          (progn
            (setq rekado-permanent-mode-q t)
            (mapc (lambda (pair)
                    (define-key rekado-mode-map
                      (kbd (car pair))
                      (cdr pair)))
                  modal-keys))
        ;; undefine permanent keys
        (progn
          (setq rekado-permanent-mode-q nil)
          (mapc (lambda (pair)
                  (define-key rekado-mode-map
                    (kbd (car pair))
                    nil))
                modal-keys)))))
  rekado-mode-map)

(defun open-line-below ()
  (interactive)
  (end-of-line)
  (newline)
  (indent-for-tab-command))

(defun open-line-above ()
  (interactive)
  (beginning-of-line)
  (newline)
  (forward-line -1)
  (indent-for-tab-command))

(defun rekado-permanent-mode-exit ()
  (interactive)
  (set-cursor-color "White")
  (rekado-mode-define-keys nil))

(defun rekado-permanent-mode-toggle ()
  (interactive)
  (if rekado-permanent-mode-q
      (rekado-permanent-mode-exit)
    (progn
      (set-cursor-color "Red")
      (rekado-mode-define-keys 1))))

;; disable permanent keys when in the minibuffer (also when running
;; commands such as isearch)
(add-hook 'post-command-hook (lambda () (if (minibufferp) (rekado-permanent-mode-exit))))

(define-minor-mode rekado-mode
  "Cursor movement shortcuts while Alt is hold and editing
shortcuts when permanent mode is active."
  :lighter " rekado"
  :global t
  :keymap rekado-mode-map
  :after-hook (rekado-mode-define-keys nil))

(provide 'rekado-mode)
;;------------------------------------------------------------