/
init.el
1702 lines (1440 loc) · 63 KB
/
init.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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;; init.el -- Emacs configuration file -*- lexical-binding: t -*-
;; Copyright 2004-2015 by Michal Nazarewicz (mina86/AT/mina86.com)
;; Some parts of the code may be © by their respective authors.
;;; Code:
;;{{{ System dependend data and directories
;; Add ~/.emacs.d/elisp to load path
(when (file-directory-p (concat user-emacs-directory "elisp"))
(eval-and-compile
(setq load-path (cons (concat user-emacs-directory "elisp") load-path))))
;; Must come before configurations of installed packages.
(package-initialize)
;; Packages repositories
(eval-when-compile (require 'package))
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
;; *Anyone* can edit Wiki pages so avoid Wiki packages
(unless (fboundp 'package--ac-desc-summary)
(defalias 'package--ac-desc-summary 'package-desc-doc))
(defadvice package--add-to-archive-contents
(around package-filter-wiki-packages (package archive) activate compile)
(unless (string-suffix-p "[wiki]" (package--ac-desc-summary (cdr package)))
ad-do-it))
;;}}}
;;{{{ Utilities
(defun current-local-map-create-maybe ()
"Return current local map creating one if not set yet."
(or (current-local-map)
(let ((map (make-sparse-keymap)))
(use-local-map map)
map)))
(defmacro set-key (keymap key &rest def)
"(set-key [KEYMAP] KEY . DEF)
In KEYMAP, define key sequence KEY as DEF.
KEYMAP can be :global (to mean global keymap, the
default), :local (to mean the local keymap) or an unquoted
symbol (to mean a keymap in given variable).
KEY is anything `define-key' accepts as a key except that if KEYMAP was not
given, KEY cannot be an unquoted symbol, i.e.:
(let ((key \"a\"))
(set-key key self-insert-command) ; will *not* work
(set-key :global key self-insert-command)) ; will work
If DEF is a single unquoted symbol it will be quoted, otherwise if it is
a single non-cons value it will not be quoted, otherwise it will be processed
as a lambda (see below). Thus the following do what one might expect:
(set-key \"a\" self-insert-command)
;; same as (global-set-key \"a\" 'self-insert-command)
(set-key \"\\C-h\" [(backspace)])
;; same as (global-set-key \"\\C-h\" [(backspace)])
(set-key \"\\C-d\" ())
;; same as (global-set-key \"\\C-h\" ())
However, the following will not work:
(let ((callback 'self-insert-command))
(set-key \"a\" callback))
;; same as (global-set-key \"a\" 'callback)
If DEF is a cons value, it's format is:
([:args ARGS INTERACTIVE] . BODY)
and results in the following lambda:
(lambda ARGS (interactive INTERACTIVE) . BODY)
or if :args is not given (at which point DEF == BODY):
(lambda () (interactive) . BODY)
For example:
(set-key \"\\C-B\" (goto-char (- (point) 2)))
;; same as (global-set-key \"\\C-B\"
;; (lambda () (interactive) (goto-char (- (point) 2))))
(set-key \"\\C-B\" :args (n) \"P\" (goto-char (- (point) (* 2 n))))
;; same as (global-set-key \"\\C-B\"
;; (lambda (n) (interactive \"P\")
;; (goto-char (- (point) (* 2 n)))))
This macro is not designed to be a complete replacement for `define-key',
`global-set-key' or `local-set-key', since it is not capable of dealing with
some forms of DEFs that those functions accept, but instead it is meant as
a helper to use in user configuration file to save on typing especially when
lambdas are used."
(setq keymap (cond ((eq :local keymap) '(current-local-map-create-maybe))
((eq :global keymap) '(current-global-map))
((symbolp keymap) keymap)
(t
(setq def (cons key def) key keymap) ; shift args
'(current-global-map))))
(unless def
(error "DEF argument missing"))
(list
'define-key keymap key
(cond ((or (cdr def) (consp (car def)))
(let ((args (if (eq :args (car def)) (cadr def)))
(interactive (if (eq :args (car def)) (list (car (cddr def)))))
(body (if (eq :args (car def)) (cdr (cddr def)) def)))
`(function (lambda ,args (interactive . ,interactive) ,@body))))
((symbolp (car def)) (list 'quote (car def)))
((car def)))))
(defmacro add-lambda-hook (hook &rest body)
"Add a lambda to a hook.
HOOK is either a hook (just as with `add-hook' function) or a list of
hooks in which case lambda will be added to all the hooks in HOOK
list. BODY is body of the lambda to be added."
(declare (indent 1))
(if (and (listp hook) (eq (car hook) 'quote) (listp (cadr hook)))
(let ((func (make-symbol "func")))
`(let
((,func (function (lambda () ,@body))))
. ,(mapcar (lambda (h) `(add-hook (quote ,h) ,func))
(cadr hook))))
`(add-hook ,hook (function (lambda () ,@body)))))
;;}}}
;;{{{ Auto-byte-compile
(defvar auto-byte-compile-files-list
(eval-when-compile
(append
(when user-init-file
(if (string-match "\\.elc$" user-init-file)
(list
(substring user-init-file 0 -4)
(substring user-init-file 0 -1))
(list user-init-file)))
(list (concat user-emacs-directory "/init.el")
(concat user-emacs-directory "/mail.el"))))
"List of files to auto compile.")
(defun auto-byte-compile-file (&optional files match regexp)
"Byte compiles file matching criteria.
FILES can be
- nil in which case value of the `buffer-file-name' variable will be
used unless it returns nil in which case no action will be taken;
- a string which is equivalent to passing list with that string as the
only element;
- a list of strings representing file names; or
- anything else which is equivalent to passing
`auto-byte-compile-files-list'.
Entries equal to \".\", \"..\" or ending with \"/.\" or \"/..\"
are ignored. Directories starting with a dot will be ignored.
If element is a directory it will be processed recursively but if
REGEXP is nil only files ending with \".el\" will be processed.
MATCH can be
- nil which is equivalent to passing `auto-byte-compile-files-list';
- a string which is equivalent to passing list with that string as the
only element;
- a list in which case file have to be in that list to be processed; or
- anything else in which case file will be processed regardless of name.
If any element of MATCH is a string ending with a slash ('/') it
is treated as directory name (no checking is done if it is really
a directory or even if it exists) and file is said to match such
entry if it begins with it thus all files in given directory will
match.
If called interacivelly without prefix arg will behave as with
MATCH equal t. With prefix arg will behave as with MATCH equal
nil.
REGEXP must be nil which is equivalent with passing a list
containing only empty string or a list of regular expressions
which file have to match to be processed.
So the default is to auto-compile the current file iff it exists
in `auto-byte-compile-files-list'.
Non-string elements in list will be ignored.
Auto-compilation means that file will be byte-compiled iff the
compiled version does not exits or is older then the file
itself."
(interactive (list (read-file-name "Auto byte compile file:" nil nil t)
(not current-prefix-arg)))
(when (or files (setq files buffer-file-name))
(setq files (cond ((stringp files) (list files))
((listp files) files)
(t auto-byte-compile-files-list))
match (mapcar 'expand-file-name
(cond ((null match) auto-byte-compile-files-list)
((stringp match) (list match))
((listp match) match))))
(let (f)
(while files
(setq f (expand-file-name (car files))
files (cdr files))
(cond
((string-match "\\(?:^\\|/\\)\\.\\.?$" f)
"Ignored")
((file-directory-p f)
(unless (string-match f "\\(?:^\\|/\\)\\.")
(if regexp
(setq files (append (directory-files f t nil t) files))
(auto-byte-compile-file (directory-files f t nil t)
(or match t) '("\\.el$")))))
((and (file-newer-than-file-p f (byte-compile-dest-file f))
(or (not match)
(catch 'found
(dolist (m match)
(if (string= m (if (string-match "/$" m)
(substring f 0 (length m)) f))
(throw 'found t)))))
(or (not regexp)
(catch 'found (dolist (r regexp)
(if (string-match r f) (throw 'found t)))))
(byte-compile-file f))))))))
(defun auto-byte-compile-buffer (&optional match buffer)
"Auto compiles file in given buffer if in Lisp mode.
MATCH has the same meaning as in `auto-byte-compile-file' function.
If BUFFER is nil, current buffer is used. Buffer is compiled only if
major mode of BUFFER is 'lisp-mode or 'emacs-lisp-mode.
If called interacivelly will behave as with MATCH equal t and
BUFFER equal nil unless prefix argument was given in which case
MATCH will equal nil."
(interactive (list (not current-prefix-arg) nil))
(and (buffer-file-name buffer)
(memq (if buffer (with-current-buffer buffer major-mode) major-mode)
'(lisp-mode emacs-lisp-mode))
(auto-byte-compile-file (buffer-file-name buffer) match)))
(add-hook 'kill-buffer-hook 'auto-byte-compile-buffer)
(add-lambda-hook 'kill-emacs-hook (auto-byte-compile-file t))
;;}}}
;;{{{ Bindings
;;{{{ Sequence commands
;; Sequence commands 1.4 by me, Michal Nazarewicz ;)
;; http://www.emacswiki.org/cgi-bin/wiki/DoubleKeyBinding
(defvar seq-times 0
"Number of times command was executed.
Contains random data before `seq-times' macro is called.")
(defmacro seq-times (&optional name max &rest body)
"Returns number of times command NAME was executed.
Updates `seq-times' variable accordingly to keep track. If NAME is
Nil `this-command' will be used. If MAX is specified the counter will
wrap around at the value of MAX never reaching it. If body is given
it will be evaluated if the command is run for the first time in
a sequence."
(declare (indent 2))
(let ((next (if (or (null max) (eq max 'nil))
'(1+ seq-times)
`(% (1+ seq-times) ,max)))
(cnd (cond ((or (eq name 'last-command) (eq name 't)) t)
((or (null name) (eq name 'nil)) 'this-command)
(`(or ,name this-command)))))
`(setq seq-times
,(if (eq cnd t)
next
`(if (eq last-command ,cnd)
,next
,@body
0)))))
(defmacro seq-times-nth (name body &rest list)
"Return element of the LIST depending on number of times command was called.
NAME and BODY has the same meaning as in `seq-times' function. LIST
is a list of values to return. Depending how many times command was
called, element with that index will be returned. The counter will
wrap around."
(declare (indent 2))
`(nth (seq-times ,name ,(length list) ,body) ',list))
(defmacro seq-times-do (name body &rest commands)
"Evaluates command depending on number of times command was called.
NAME and BODY has the same meaning as in `seq-times' function.
COMMANDS is a list of sexps to evaluate. Depending how many times
command was called, sexp with that index will be evaluated. The
counter will wrap around."
(declare (indent 2))
`(eval (nth (seq-times ,name ,(length commands) ,body) ',commands)))
;;}}}
;;{{{ Home/End
;; My home
(defvar my-home-end--point 0)
(defun my-home ()
"Go to beginning of line, indent or buffer.
When called once move point to beginning of line, twice - beginning of
the buffer, thrice - back to where it was at the beginning."
(interactive)
(seq-times-do nil (setq my-home-end--point (point))
(beginning-of-line)
(goto-char (point-min))
(goto-char my-home-end--point)))
(substitute-key-definition 'move-beginning-of-line 'my-home
(current-global-map))
;; My end
(defun my-end ()
"Go to end of line or buffer.
When called once move point to end of line, twice - end of buffer,
three times - back to where it was at the beginning."
(interactive)
(seq-times-do nil (setq my-home-end--point (point))
(if (bound-and-true-p folding-mode)
(folding-end-of-line) (end-of-line))
(goto-char (point-max))
(goto-char my-home-end--point)))
(substitute-key-definition 'move-end-of-line 'my-end (current-global-map))
;;}}}
;;{{{ Pager/Scrolling
(setq scroll-error-top-bottom t)
(setq scroll-preserve-screen-position t)
;; Makes paging functions work the way god intended
;; http://www.docs.uu.se/~mic/emacs.html
;; pager.el was modified by me
(when (eval-when-compile (load "pager" t))
(require 'pager)
(setq pager-goto-edge t)
(substitute-key-definition 'scroll-down 'pager-page-up
(current-global-map))
(substitute-key-definition 'scroll-down-command 'pager-page-up
(current-global-map))
(substitute-key-definition 'scroll-up 'pager-page-down
(current-global-map))
(substitute-key-definition 'scroll-up-command 'pager-page-down
(current-global-map)))
;;}}}
;;{{{ Save with no blanks
;; Save with no trailing whitespaces
(defun save-no-blanks (&optional no-strip)
"Save file stripping all trailing white space and empty lines from EOF.
When called with prefix argument (or NO-STRIP being non-nill) does not
perform stripping and behaves as plain `save-buffer'."
(interactive "P")
(unless no-strip
(save-restriction
(delete-trailing-whitespace)))
(save-buffer))
(substitute-key-definition 'save-buffer 'save-no-blanks (current-global-map))
;;}}}
;;{{{ Misc
(set-key "\C-x\C-c" :args (arg) "P"
(when (yes-or-no-p "Do you really want to quit? ")
(save-buffers-kill-emacs arg))) ; Never kill by mistake
(when (fboundp 'save-buffers-kill-terminal)
(set-key "\C-x\C-c" save-buffers-kill-emacs)
(set-key "\C-xc" save-buffers-kill-terminal))
(set-key "\C-h" [(backspace)])
(set-key [(backspace)] delete-backward-char)
(set-key [(delete)] delete-forward-char)
(set-key "\C-d" [(delete)])
(defvar mn-window-skip-modes '(compilation-mode)
"List of major modes `mn-window-skip-p' treats as uninteresting.")
(defvar mn-window-skip-names '("*Compile-Log*" "*Backtrace*" "*compilation*")
"List of buffer names `mn-window-skip-p' treats as uninteresting.
This comes as an addition to `mn-window-skip-regexp'.")
(defvar mn-window-skip-regexp nil
"A regexp matching buffer names `mn-window-skip-p' treats as uninteresting.
This comes as an addition to `mn-window-skip-names'.")
(defun mn-window-skip-p (window)
"Check whether WINDOW is uninteresting to `mn-other-window'."
(catch 'done
; catch is ugly, I know, but it saves on some indention
(let ((b (window-buffer window)) n)
(unless b
(throw 'done nil))
(when (memq (with-current-buffer b major-mode) mn-window-skip-modes)
(throw 'done t))
(unless (or mn-window-skip-names mn-window-skip-regexp)
(throw 'done nil))
(unless (setq n (buffer-name b))
(throw 'done nil))
(when (member n mn-window-skip-names)
(throw 'done t))
(when (and mn-window-skip-regexp (string-match mn-window-skip-regexp n))
(throw 'done t))
nil)))
(defun mn-other-window (count &optional all-frames)
"Select another interesting window in cyclic ordering of windows.
COUNT and ALL-FRAMES has the same meaning as in `other-window' function.
Window is not interesting id buffer it contains has one of the
major modes from `mn-window-skip-modes' list, its name is on the
`mn-window-skip-names' list or matches `mn-window-skip-regexp'."
(interactive "p")
(let ((func (cond ((> count 0) 'next-window)
((< count 0) (setq count (- count)) 'previous-window))))
(when func
(catch 'loop
(let* ((wnd (selected-window))
(w wnd))
(while (> count 0)
(setq w (funcall func w nil all-frames))
(when (eq wnd w)
(throw (quote loop) w))
(unless (mn-window-skip-p w)
(setq count (1- count))))
(select-window w))))))
(substitute-key-definition 'other-window 'mn-other-window (current-global-map))
(set-key "\C-xO" other-window)
(set-key "\C-xp" "\C-u-1\C-xo") ; C-x p prev win
(set-key "\C-xP" "\C-u-1\C-xO")
(set-key "\C-x1" delete-other-windows-vertically)
(when (fboundp 'windmove-right)
(set-key "\M-F" windmove-right)
(set-key "\M-B" windmove-left)
(set-key "\M-P" windmove-up)
(set-key "\M-N" windmove-down))
(set-key "\C-xk" kill-this-buffer) ; don't ask which buffer to kill
(set-key "\C-cr" revert-buffer) ; Reload buffer
(set-key "\C-x\C-b" (switch-to-buffer (other-buffer))) ; C-x C-b switch
;; Jump
(require 'ffap)
(defun my-jump () "Jump to the thing at point." (interactive)
(let ((thing (ffap-guesser))) (if thing (ffap thing)) t))
(set-key [(control return)] my-jump)
(set-key [(control shift mouse-1)] ffap-at-mouse)
(set-key "\C-x\C-f" ffap)
;; Make q close current buffer if it's read-only
(set-key "q" :args (n) "p"
(if (and buffer-read-only (not (= n 0)))
(kill-buffer (current-buffer))
(self-insert-command n)))
(set-default 'browse-url-browser-function 'browse-url-generic)
(set-default 'browse-url-generic-program "opera")
;; Minibuffer
(set-key minibuffer-local-map "\C-c" ; C-c clears minibuffer
delete-minibuffer-contents)
(set-key minibuffer-local-map "\C-p" previous-history-element)
(set-key minibuffer-local-map "\C-n" next-history-element)
;;}}}
;;{{{ Killing, yanking, X selection, etc
;; Regions, selections and marks
(setq mouse-yank-at-point t ;mouse yank at point not at cursor (X-win)
kill-read-only-ok t ;be silent when killing text from RO buffer
set-mark-command-repeat-pop t
kill-do-not-save-duplicates t)
(delete-selection-mode 1) ;deleting region by typing or del (like Win)
(set-key [(shift insert)]
(let ((mouse-yank-at-point nil))
(mouse-yank-primary nil)))
;;}}}
;;{{{ Just one space
(substitute-key-definition 'just-one-space
(lambda () (interactive) (cycle-spacing -1))
(current-global-map))
;;}}}
;;{{{ Tab - indent or complete
(eval-when-compile (require 'hippie-exp))
(setq hippie-expand-try-functions-list
'(
; try-expand-all-abbrevs
; try-expand-list
; try-expand-line
try-expand-dabbrev
try-expand-dabbrev-all-buffers
try-expand-dabbrev-from-kill
try-complete-file-name-partially
try-complete-file-name
try-complete-lisp-symbol-partially
try-complete-lisp-symbol)
hippie-expand-verbose nil)
;; Indent or complete
(defvar indent-or-complete-complete-function 'hippie-expand
"Function to complete the word when using `indent-or-complete'.
It is called with one argument - nil.")
(defvar indent-or-complete--last-was-complete nil)
(defun indent-or-complete ()
"Indent or complete depending on context.
In minibuffer run `minibuffer-complete', if `use-region-p' run
`indent-region', if point is at end of a word run
`inent-or-complete-complete-function', else run
`indent-for-tab-command'."
(interactive)
(cond ((and (fboundp 'minibufferp) (minibufferp)) (minibuffer-complete))
((use-region-p) (indent-region (region-beginning) (region-end)))
((setq indent-or-complete--last-was-complete
(or (and (char-before)
(= ?w (char-syntax (char-before)))
(or (not (char-after))
(/= ?w (char-syntax (char-after)))))
(and (eq last-command this-command)
indent-or-complete--last-was-complete)))
(funcall indent-or-complete-complete-function nil))
((indent-for-tab-command))))
(when (eval-when-compile (load "auto-complete-config" t))
(require 'auto-complete-config)
(add-to-list 'ac-dictionary-directories "~/.emacs.d/elisp/ac-dict")
(ac-config-default)
(setq ac-auto-start 3
ac-delay 0.1
ac-auto-show-menu 2
ac-ignore-case nil
ac-use-comphist t
indent-or-complete-complete-function 'auto-complete)
(set-key ac-completing-map "\M-o" ac-previous)
(set-key ac-completing-map "\M-p" ac-previous)
(set-key ac-completing-map "\M-n" ac-next)
(define-key (current-global-map) "\M-/"
indent-or-complete-complete-function))
;; (set-key "\t" indent-or-complete)
;; (set-key [(tab)] indent-or-complete)
(add-lambda-hook 'find-file-hooks
(unless (eq major-mode 'org-mode)
(set-key :local [(tab)] indent-or-complete)))
;;}}}
;;{{{ Filling
;; Alt+q - Fill
(defun my-fill ()
"Fills paragraph (or region) using a cyclic order alignment.
If called once fills the paragraph to the left, twice - justifies,
three times - to the right, four times - centers."
(interactive)
(fill-paragraph (seq-times-nth () () left full right center) t))
(set-key "\M-q" my-fill)
(when (fboundp 'fill-single-char-nobreak-p)
(add-hook 'fill-nobreak-predicate 'fill-single-char-nobreak-p))
;;}}}
;;{{{ Fkeys
;;{{{ F1 - Help
(defun my-help ()
"Show context dependent help.
If function given tries to `describe-function' otherwise uses
`manual-entry' to display manpage of a `current-word'."
(interactive)
(let ((var (variable-at-point)))
(if (symbolp var)
(describe-variable var)
(let ((fn (function-called-at-point)))
(if fn
(describe-function fn)
(man (current-word)))))))
(set-key [(f1)] my-help)
;;}}}
;;{{{ F2 - find configuration files
(set-key [(f2)] (find-file
(if (string-match "\\.elc$" user-init-file)
(substring user-init-file 0 -1)
user-init-file)))
(set-key [(control f2)] (find-file (concat user-emacs-directory "/mail.el")))
(set-key [(meta f2)] (find-file custom-file))
(set-key [(shift f2)] (find-file "~/.bashrc"))
;;}}}
;;{{{ F3/F4 - keyboard macros
(defun kmacro-end-or-call-possibly-on-region-lines (arg &optional no-repeat)
"End keyboard macro or call it.
End defining a keyboard macro if one is being defined and if not call
last keyboard macro ARG times or on region if `use-region-p'.
Optional argument NO-REPEAT is passed to `kmacro-call-macro' function."
(interactive "P")
(cond
(defining-kbd-macro
(if kmacro-call-repeat-key
(kmacro-call-macro arg no-repeat t)
(kmacro-end-macro arg)))
((and (eq this-command 'kmacro-view-macro) ;; We are in repeat mode!
kmacro-view-last-item)
(kmacro-exec-ring-item (car kmacro-view-last-item) arg))
((and arg (listp arg))
(kmacro-call-ring-2nd 1))
((use-region-p)
(apply-macro-to-region-lines (region-beginning) (region-end)))
(t
(kmacro-call-macro arg no-repeat))))
(set-key [(f4)] kmacro-end-or-call-possibly-on-region-lines)
;;}}}
;;{{{ F5 - Mail
(when (file-exists-p (concat user-emacs-directory "mail.el"))
(set-key [(f5)]
(load (concat user-emacs-directory "mail.el"))
(set-key [(f5)] notmuch)
(notmuch)))
;;}}}
;;{{{ F6 - notes
(require 'remember)
(when (fboundp 'remember-notes)
(setq remember-notes-auto-save-visited-file-name t
remember-notes-buffer-name "*scratch*")
(setq initial-buffer-choice
(lambda ()
(kill-buffer remember-notes-buffer-name)
(remember-notes))))
(set-key [(f6)] remember-notes)
;;}}}
;;{{{ F7 - spell checking
(require 'ispell)
(setq-default ispell-program-name "aspell")
(ispell-change-dictionary "british" t)
(defvar mn-spell-dictionaries '("polish" "british")
"List of dictionaries to cycle through with `mn-spell-switch-dictionary'.")
(defun mn-spell (&optional lang start end)
"Spell check region, buffer or from point.
If LANG is not-nil sets Ispell dictionary to LANG, then checks
region from START to END for spelling errors. The default values
for START and END are `region-beginning' and `region-end' if
`use-region-p' or `point-min' and `point-max' otherwise.
If LANG is an empty string local dictionary is set to
nil (ie. the global dictionary is used).
If START >= END this function only sets the dictionary and
returns nil. Otherwise it returns whatever `ispell-region'
returned."
(interactive
(list (completing-read
"Use new dictionary (RET for current, SPC to complete): "
(if (fboundp 'ispell-valid-dictionary-list)
(mapcar 'list (ispell-valid-dictionary-list)))
nil t)))
(if lang (ispell-change-dictionary (if (string= lang "") nil lang)))
(let* ((rp (use-region-p))
(s (or start (if rp (region-beginning) (point-min))))
(e (or end (if rp (region-end) (point-max)))))
(if (< s e) (ispell-region s e))))
(defun mn-spell-switch-dictionary (&optional global)
"Switche dictionary to the next dictionary from `mn-spell-dictionaries'.
If GLOBAL is non-nil, or with a prefix argument set global dictionary."
(interactive "P")
(ispell-change-dictionary
(let ((dic (or (and (not global) ispell-local-dictionary) ispell-dictionary))
(list mn-spell-dictionaries))
(while (and list (not (string= (car list) dic))) (setq list (cdr list)))
(or (cadr list) (car mn-spell-dictionaries)))
global))
(set-key [(f7)] (mn-spell))
(set-key [(control f7)] (mn-spell nil (point)))
(set-key [(meta f7)] mn-spell-switch-dictionary)
(set-key [(shift f7)] ispell-word)
(define-globalized-minor-mode global-flyspell-mode
flyspell-mode mn-turn-flyspell-on)
(defun mn-turn-flyspell-on ()
"Turn `flyspell-mode' or `flyspell-prog-mode' depending on major mode."
(cond ((string-prefix-p " *" (buffer-name)) nil)
((minibufferp) nil)
((or (derived-mode-p 'prog-mode)
(eq major-mode 'diff-mode))
(flyspell-prog-mode))
((memq major-mode '(notmuch-hello-mode notmuch-search-mode
notmuch-show-mode)))
((flyspell-mode t))))
(global-flyspell-mode 1)
;;}}}
;;{{{ F9 - Compilation
(defconst -mn-compile-common
" -Wall -Wextra -Wfloat-equal -Wshadow -Wwrite-strings -Winline -Wdisabled-optimization -Wstrict-aliasing=2 -pedantic -DMINA86 -ggdb -O0 -Wpointer-arith -funit-at-a-time")
(defvar mn-compile-vars
`((("CFLAGS" "-std=c99 -Werror-implicit-function-declaration -Wunreachable-code -Wstrict-prototypes -Wold-style-definition")
("CXXFLAGS" "-std=c++98")
("CPPFLAGS" ,-mn-compile-common)
("LDFLAGS" nil))
(("CFLAGS" "-std=c89 -Werror-implicit-function-declaration -Wunreachable-code -Wstrict-prototypes -Wold-style-definition")
("CXXFLAGS" "-std=c++98 -Wstrict-null-sentinel -Wctor-dtor-privacy -Woverloaded-virtual")
("CPPFLAGS" ,-mn-compile-common)
("LDFLAGS" nil)))
"Enviroment variables set by `mn-compile' priory to compilation.
The car of the list is a list of default enviroment variables to be
set and cadr is a list is a list of alternative enviroment variables.
Each list is a list of two element lists which car is a enviroment
variables name and cadr is value.")
(defun mn-compile (&optional alt recompile touch)
"Compile current file.
If ALT is omited or nil sets CFLAGS, CXXFLAGS, CPPFLAGS and
LDFLAGS enviroment variables to `mn-cflags', `mn-cxxflags',
`mn-cppflags', `mn-ldflags' respectively.
If ALT is non-nil (or when called interactive with any prefix
argument) sets CFLAGS, CXXFLAGS, CPPFLAGS and LDFLAGS enviroment
variables to `mn-alt-cflags', `mn-alt-cxxflags',
`mn-alt-cppflags', `mn-alt-ldflags' respectively.
Executes `recompile' function if RECOMPILE is non-nill or
`compile' otherwise. If TOUCH is non-nil marks buffer as
modified beforehand."
(interactive "P")
(if touch (set-buffer-modified-p t))
(save-buffer)
(if (or (eq major-mode 'lisp-mode) (eq major-mode 'emacs-lisp-mode))
(auto-byte-compile-file nil t)
(let (v (vars (if alt (cadr mn-compile-vars) (car mn-compile-vars))))
(while (set 'v (pop vars)) (setenv (car v) (cadr v))))
(if recompile (recompile) (call-interactively 'compile))))
(set-key [(f9)] mn-compile)
(set-key [(control f9)] :args (a) "P" (mn-compile a t))
(set-key [(meta f9)] :args (a) "P" (mn-compile (not a) t t))
(set-key [(shift f9)] next-error)
(eval-when-compile (require 'compile))
(setq compilation-scroll-output 'first-error ; scroll until first error
compilation-window-height 12) ; keep it readable
;;}}}
;;}}}
;;{{{ ISearch mode ace-jump-mode
(add-hook 'isearch-mode-hook (lambda ()
(set-key isearch-mode-map [(f1)] isearch-mode-help)
(set-key isearch-mode-map "\C-t" isearch-toggle-regexp)
(set-key isearch-mode-map "\C-c" isearch-toggle-case-fold)
(set-key isearch-mode-map "\C-j" isearch-edit-string)
(set-key isearch-mode-map "\C-h" isearch-del-char)
(set-key isearch-mode-map [backspace] isearch-del-char)))
(setq search-whitespace-regexp "[ \t\r]+")
(autoload 'ace-jump-mode "ace-jump-mode" "Emacs quick move minor mode" t)
(set-key "\M-s" ace-jump-mode)
(setq ace-jump-mode-scope 'window
ace-jump-mode-case-fold nil
ace-jump-mode-move-keys
(string-to-list "htnsueoagcrlp.,;mwvzkjq'difybx/-\\@#)(+}]{![*=&$"))
;;}}}
;;{{{ Copy/Kill
;; Make C-w, M-w work on word if no selection
(set-key "\C-w" (call-interactively
(if (use-region-p) 'kill-region 'kill-word)))
(set-key "\M-w"
(if (use-region-p)
(call-interactively 'kill-ring-save)
(kill-ring-save (point) (progn (forward-word 1) (point)))
(setq this-command 'kill-region)))
;;}}}
;;}}}
;;{{{ Syntax highlighting
;; Font lock
(require 'font-lock)
(global-font-lock-mode t)
(setq font-lock-global-modes '(not notmuch-search-mode notmuch-hello-mode))
;; Let customize keep config there
(setq custom-file (concat user-emacs-directory "custom.el"))
(when (file-exists-p custom-file)
(load-file custom-file))
(push custom-file auto-byte-compile-files-list)
;; Other
(show-paren-mode t) ;show matching parenthesis.
(defface my-fixme-face
'((t :background "red" :foreground "white" :weight bold))
"Face use to show FIXME and XXX markers in the text."
:group 'whitespace)
(defface my-todo-face
'((t :foreground "red" :weight bold))
"Face used to show TODO markers in the text."
:group 'whitespace)
;; Show TODO and FIXME
;; http://www.emacswiki.org/cgi-bin/wiki/EightyColumnRule
(add-lambda-hook 'font-lock-mode-hook
(unless (eq 'diff-mode major-mode)
(font-lock-add-keywords nil
'(("\\<\\(TODO:?\\)\\>" 1 'my-todo-face t)
("\\<\\(FIXME:?\\|XXX\\)\\>" 1 'my-fixme-face t)))))
;; 'lines-tail would be great but it does not really work with tabs ≠ 8
;; characters.
(setq whitespace-style '(face
space-before-tab::tab
tab-mark
tabs
big-indent
trailing)
whitespace-big-indent-regexp "^\\(\t\\{4,\\}\\)")
(global-whitespace-mode)
;;}}}
;;{{{ Misc small config
(setq use-dialog-box nil) ;never use dialog boxes
;; Frame apperence
(if (fboundp 'scroll-bar-mode) (scroll-bar-mode -1))
(if (fboundp 'tool-bar-mode) (tool-bar-mode -1))
(if (fboundp 'menu-bar-mode) (menu-bar-mode -1))
(setq default-frame-alist '((width . 80)
(right-fringe . 4)
(left-fringe . 4)
(menu-bar-lines . 0)
(tool-bar-lines . 0)
(vertical-scroll-bars)
(foreground-color . "gray")
(background-color . "black")
(background-mode . dark)
(wait-for-wm . nil)))
;; For some reason, Emacs uses latin-2 version of the font and when it
;; encounters Unicode characters it changes font for those characters only.
;; This makes for very ugly rendering. Force Unicode font in all frames.
(add-hook 'after-make-frame-functions
(lambda (frame)
(set-frame-font
"-adobe-courier-medium-r-normal-*-10-*-*-*-*-*-iso10646-1"
nil (list frame))))
(set-face-attribute
'default t :font "-adobe-courier-medium-r-normal-*-10-*-*-*-*-*-iso10646-1")
;; Modeline
(setq line-number-mode t ;show line number in modeline
column-number-mode t) ;show column number in modeline
(setq mode-line-format
'("%e"
mode-line-mule-info
mode-line-modified
mode-line-frame-identification
mode-line-buffer-identification
" "
mode-line-position
vc-mode
" "
mode-line-modes
(which-func-mode ("" which-func-format))
(global-mode-string ("" global-mode-string))
"%-")
mode-line-position
'(:eval (let* ((min (point-min)) (max (point-max))
(wide (and (= min 1) (= max (1+ (buffer-size))))))
(concat
(if wide "(" "[")
(if column-number-mode
(if (> (current-column) 80)
(concat (propertize "%02c" 'face 'error) ", ")
"%02c, ")
"")
(if line-number-mode "%2l/" "")
(number-to-string (1+ (count-lines min max)))
(if wide ")" "]"))))
mode-line-modes
'("("
(:propertize
("" mode-name mode-line-process minor-mode-alist)
help-echo "mouse-1: major mode, mouse-2: major mode help, mouse-3: toggle minor modes"
mouse-face mode-line-highlight
local-map (keymap
(header-line keymap
(down-mouse-3 . mode-line-mode-menu-1))
(mode-line keymap
(down-mouse-3 . mode-line-mode-menu-1)
(mouse-2 . describe-mode)
(down-mouse-1 . mouse-major-mode-menu))))
") "))
(add-lambda-hook 'after-init-hook
;; Highlight groups of three digits
(when (fboundp 'global-num3-mode)
(global-num3-mode t))
;; Dim buffers which are not (current-buffer)
(when (fboundp 'auto-dim-other-buffers-mode)
(auto-dim-other-buffers-mode t)))
;; Other
(require 'icomplete)
(icomplete-mode 1) ;nicer completion in minibuffer
(setq icomplete-prospects-height 2) ; don't spam my minibuffer
(setq suggest-key-bindings 3) ;suggestions for shortcut keys for 3 seconds
(setq frame-title-format "Emacs") ;frame title format
(setq history-delete-duplicates t)
(setq inhibit-splash-screen t ;don't show splash screen
inhibit-startup-buffer-menu t) ;don't show buffer menu when oppening
; many files (EMACS 21.4+)
(setq sentence-end-base "[.?!…][]\"'”)}]*" ; "…" also ends a sentence
paragraph-start " *\\([*+-]\\|\\([0-9]+\\|[a-zA-Z]\\)[.)]\\|$\\)"
require-final-newline t) ;always end file with NL
(fset 'yes-or-no-p 'y-or-n-p) ;make yes/no be y/n
(set-default 'indicate-empty-lines t) ;show empty lines at the end of file
(set-default 'indicate-buffer-boundaries t) ;show buffer boundries on fringe
(setq x-alt-keysym 'meta) ;treat Alt as Meta even if real Meta found
(setq gc-cons-threshold 4000000) ;bytes before garbage collection
(eval-when-compile (require 'ange-ftp))
(setq ange-ftp-try-passive-mode t);passive FTP
(blink-cursor-mode -1) ;do not blink cursor
(setq blink-cursor-alist '((t . box) ;seriously, don't blink,
(box . box)) ;blink-cursor-mode does not
cursor-type 'box) ;work for me.
(setq visible-bell nil) ;no visual bell
(eval-when-compile (require 'compile))
(setq compilation-auto-jump-to-first-error t)
(setq line-move-visual nil) ;move by logical lines not screen lines
(when (fboundp 'describe-char-eldoc)
(setq-default eldoc-documentation-function #'describe-char-eldoc))
;; Saving etc
(when (fboundp recentf-mode)
(recentf-mode -1)) ;no recent files
(setq backup-by-copying-when-linked t) ;preserve hard links
(auto-compression-mode 1) ;automatic compression
(setq make-backup-files nil) ;no backup
(global-auto-revert-mode 1) ;automaticly reload buffer when changed
(setq vc-handled-backends nil) ;I don't use vc-mode
;; Indention
(defun set-tab (tab &optional stop-list)