public
Description: My configuration files for various applications
Homepage: http://mina86.com/
Clone URL: git://github.com/mina86/dot-files.git
dot-files / dot-emacs
100644 1773 lines (1474 sloc) 66.973 kb
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
;; -*- mode: emacs-lisp; indent-tabs-mode: nil -*-
;; .emacs -- Emacs configuration file
;; Copyright 2004-2008 by Michal Nazarewicz (mina86/AT/mina86.com)
;;
;;{{{ Compatibility with old Emacses
 
(unless (boundp 'user-emacs-directory)
  (defvar user-emacs-directory (expand-file-name "~/.emacs.d/")
    "Directory beneath which additional per-user Emacs-specific files are placed.
Various programs in Emacs store information in this directory.
Note that this should end with a directory separator."))
 
(unless (boundp 'use-empty-active-region)
  (defcustom use-empty-active-region nil
    "If non-nil, an active region takes control even if empty.
This applies to certain commands which, in Transient Mark mode,
apply to the active region if there is one. If the setting is t,
these commands apply to an empty active region if there is one.
If the setting is nil, these commands treat an empty active
region as if it were not active."
    :type 'boolean
    :version "23.1"
    :group 'editing-basics))
 
(unless (fboundp 'use-region-p)
  (defun use-region-p ()
    "Return t if certain commands should apply to the region.
Certain commands normally apply to text near point,
but in Transient Mark mode when the mark is active they apply
to the region instead. Such commands should use this subroutine to
test whether to do that.
 
This function also obeys `use-empty-active-region'."
    (and (region-active-p)
         (or use-empty-active-region (> (region-end) (region-beginning))))))
 
(unless (fboundp 'region-active-p)
  (defun region-active-p ()
    "Return t if Transient Mark mode is enabled and the mark is active.
This is NOT the best function to use to test whether a command should
operate on the region instead of the usual behavior -- for that,
use `use-region-p'."
    (and transient-mark-mode mark-active)))
 
;;}}}
;;{{{ System dependend data and directories
 
;; Gnus
(when (eval-when-compile (string-match "^erwin" system-name))
  (setq gnus-home-directory (expand-file-name "~/.gnus")
        gnus-init-file
        (concat gnus-home-directory
                (if (file-exists-p (concat gnus-home-directory "/.gnus"))
                    "/.gnus" "/.gnus.el"))
        load-path (cons (concat gnus-home-directory "/gnus/lisp") load-path)
        message-directory (concat gnus-home-directory "/Mail"))
  (eval-after-load "info"
    '(if (load "gnus-load")
         (setq Info-default-directory-list
               (cons (concat gnus-home-directory "/gnus/texi")
                     Info-default-directory-list)))))
 
;; Make sure user-emacs-directory is defined
(if (string-equal user-emacs-directory "")
    (setq user-emacs-directory (expand-file-name "~/.emacs.d/")))
 
;; Add ~/.emacs.d/elisp to load path
(if (eval-when-compile
      (file-directory-p (concat user-emacs-directory "elisp")))
    (setq load-path (cons (concat user-emacs-directory "elisp") load-path)))
 
;; Use aspell
(setq-default ispell-program-name "aspell")
 
;;}}}
;;{{{ Auto-byte-compile
 
(require 'bytecomp)
 
(defvar auto-byte-compile-files-list
  (let ((ghd (or (and (boundp 'gnus-home-directory) gnus-home-directory)
                 (expand-file-name "~/.gnus"))))
    (cond
     ((string-match "\\.elc$" (or user-init-file "/home/mina86/.emacs.d/init.el"))
      (list (substring user-init-file 0 -4)
            (substring user-init-file 0 -1)
            (concat ghd "/.gnus")
            (concat ghd "/.gnus.el")))
     (t
      (list user-init-file
            (concat ghd "/.gnus")
            (concat ghd "/.gnus.el")))))
  "List of files to auto compile")
 
(defun auto-byte-compile-file (&optional file match regexp)
  "file can be
- nil in which case value returned by `buffer-file-name' 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)))
 
  (if (not (or file (setq file (buffer-file-name))))
      0
    (setq file (cond ((stringp file) (list file))
                      ((listp file) file)
                      (t auto-byte-compile-files-list))
          match (mapcar (function (lambda (i) (expand-file-name i)))
                        (cond ((not match) auto-byte-compile-files-list)
                              ((stringp match) (list match))
                              ((listp match) match)
                              (t nil))))
 
    (let (f (n 0))
      (while (setq f (car file))
        (setq file (cdr file) f (expand-file-name f))
        (cond
         ((string-match f "\\(?:^\\|/\\)\\.\\.?$"))
         ((file-directory-p f)
          (unless (string-match f "\\(?:^\\|/\\)\\.")
            (if regexp
                (setq file (append (directory-files f t nil t) file))
              (setq n (+ n (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)
               (setq n (1+ n))))))
      n)))
 
(defun auto-byte-compile-buffer (&optional match buffer)
  "Auto compiles file in given buffer (if buffer is nil current
buffer is used) providing that major mode of the buffer is
lisp-mode or emacs-lisp-mode. match has the same meaning as in
`auto-byte-compile-file'.
 
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 (save-current-buffer (set-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-hook 'kill-emacs-hook (function (lambda () (auto-byte-compile-file t))))
 
;;}}}
;;{{{ Bindings
 
(defmacro set-key-lambda (key &rest body)
  `(global-set-key ,key (function (lambda () (interactive) ,@body))))
 
;;{{{ Sequence commands
 
;; Sequence commands 1.3 by me, Michal Nazarewicz ;)
;; http://www.emacswiki.org/cgi-bin/wiki/DoubleKeyBinding
(defvar seq-times 0
  "Stores number of times command was executed. It cotnains
random data before `seq-times' macro is called.")
 
 
(defmacro seq-times (&optional name max &rest body)
  "Returns number of times command NAME was executed and updates
`seq-times' variable accordingly. 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))
 
  ;; Build incrementation part
  (setq max (cond ((null max) '(setq seq-times (1+ seq-times)))
                  ((atom max) (if (and (integerp max) (> max 0))
                                  `(setq seq-times (% (1+ seq-times) ,max))
                                '(setq seq-times (1+ seq-times))))
                  (t `(let ((max ,max))
                                 (if (and (integerp max) (> max 0))
                                     (setq seq-times (% (1+ seq-times) max))
                                   (setq seq-times (1+ seq-times)))))))
 
  ;; Make macro
  (if (eq name 'last-command)
      max
    (cond ((null name) (setq name 'this-command))
          ((consp name) (setq name `(or ,name this-command))))
    `(if (eq last-command ,name)
         ,max
       ,@body
       (setq seq-times 0))))
 
 
(defmacro seq-times-nth (name body &rest list)
  "Calls `seq-times' with arguments NAME, length and BODY
and (where length is the number of elements in LIST) then returns
`seq-times'th element of LIST."
  (declare (indent 2))
  `(nth (seq-times ,name ,(length list) ,body) ',list))
 
 
(defmacro seq-times-do (name body &rest commands)
  "Calls `seq-times' with arguments NAME, length and BODY (where
length is the number of COMMANDS) and then runs `seq-times'th
command from COMMANDS."
  (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 ()
   "Depending on how many times it was called moves the point to:
once - beginning of line; twice - indent;
three times - beginning of buffer; four times - back to where it was."
   (interactive)
   (seq-times-do nil (setq my-home-end--point (point))
     (beginning-of-line)
     (back-to-indentation)
     (goto-char (point-min))
     (goto-char my-home-end--point)))
 
(substitute-key-definition 'move-beginning-of-line 'my-home
                           (current-global-map))
;(global-set-key "\C-a" 'my-home)
;(global-set-key [(home)] 'my-home)
;(global-set-key [(kp-home)] 'my-home)
;(global-set-key [(f27)] 'my-home)
;(global-set-key "\e[1~" 'my-home)
 
 
;; My end
(defun my-end () "Go to end of line or buffer" (interactive)
  (seq-times-do nil (setq my-home-end--point (point))
    (if folding-mode (folding-end-of-line) (end-of-line))
    (forward-paragraph)
    (goto-char (point-max))
    (goto-char my-home-end--point)))
 
(substitute-key-definition 'move-end-of-line 'my-end (current-global-map))
;(global-set-key "\C-e" 'my-end)
;(global-set-key [(end)] 'my-end)
;(global-set-key [(kp-end)] 'my-end)
;(global-set-key [(select)] 'my-end)
;(global-set-key [(f33)] 'my-end)
;(global-set-key "\e[4~" 'my-end)
 
;;}}}
;;{{{ Pager/Scrolling
 
;; Makes paging functions work the way god intended
;; http://www.docs.uu.se/~mic/emacs.html
;; pager.el was modified by me
(require 'pager)
(setq pager-goto-edge t)
 
(substitute-key-definition 'scroll-down 'pager-page-up (current-global-map))
(substitute-key-definition 'scroll-up 'pager-page-down (current-global-map))
;(global-set-key "\ev" 'pager-page-up) ; Page Up
;(global-set-key [(prior)] 'pager-page-up)
;(global-set-key [(f29)] 'pager-page-up)
;(global-set-key "\C-v" 'pager-page-down) ; Page Down
;(global-set-key [(next)] 'pager-page-down)
;(global-set-key [(f35)] 'pager-page-down)
 
(set-key-lambda [(meta up)] (scroll-down 1)) ; Alt+Up
(set-key-lambda [(meta kp-8)] (scroll-down 1))
(set-key-lambda "\M-p" (scroll-down 1))
(set-key-lambda [(meta down)] (scroll-up 1)) ; Alt+Dn
(set-key-lambda [(meta kp-2)] (scroll-up 1))
(set-key-lambda "\M-n" (scroll-up 1))
 
(global-set-key [(meta shift up)] 'pager-row-up) ; Alt+Shift+Up
(global-set-key [(meta shift kp-8)] 'pager-row-up)
(global-set-key "\M-P" 'pager-row-up)
(global-set-key [(meta shift down)] 'pager-row-down); Alt+Shift+Down
(global-set-key [(meta shift kp-2)] 'pager-row-down)
(global-set-key "\M-N" 'pager-row-down)
 
;;}}}
;;{{{ Save with no blanks
 
;; Save with no trailing whitespaces
;; XXX there is a delete-trailing-whitespace function which might be
;; added to write-file-hooks.
(defun save-no-blanks (&optional arg) (interactive "P")
  (unless arg
    (save-excursion
      (save-restriction
        (widen)
        (goto-char 0) (perform-replace "[ \t]+$" "" nil t nil)
        (goto-char (point-max))
        (unless (>= (skip-chars-backward "\n") -2)
          (delete-region (1+ (point)) (point-max))))))
  (save-buffer))
 
(substitute-key-definition 'save-buffer 'save-no-blanks (current-global-map))
;(global-set-key "\C-x\C-s" 'save-no-blanks)
 
;;}}}
;;{{{ Misc
 
(when window-system (global-unset-key "\C-z"))
 
 
(global-set-key "\C-h" 'delete-backward-char)
(global-set-key [(backspace)] 'delete-backward-char)
(global-set-key [(delete)] 'delete-char)
(global-set-key [(control backspace)] 'backward-kill-word)
(global-set-key [(control delete)] 'kill-word)
 
 
;(global-set-key "\C-xg" 'goto-line) ; C-x g goto line
;Since 22.1 M-g M-g and M-g g are goto line
(global-set-key "\C-xp" "\C-u-1\C-xo") ; C-x p prev win
(global-set-key "\C-xk" 'kill-this-buffer) ; don't ask which
                                                        ; buffer to kill
(global-set-key "\C-cr" 'revert-buffer) ; Reload buffer
(set-key-lambda "\C-x\C-b" (switch-to-buffer (other-buffer))) ; C-x C-b switch
(global-set-key [(control ";")] 'comment-dwim) ; C-; comments
 
;(require 'imenu)
;(global-set-key [(shift mouse-3)] 'imenu)
;(setq imenu-sort-function 'imenu--sort-by-name)
 
 
;; Jump
(require 'ffap)
(defun my-jump () "Jump to the thing at point." (interactive)
  (let ((thing (ffap-guesser))) (if thing (ffap thing)) t))
 
(global-set-key [(control return)] 'my-jump)
(global-set-key [(control shift mouse-1)] 'ffap-at-mouse)
(global-set-key "\C-x\C-f" 'ffap)
 
 
;; Make l behave as it should in help-mode
;; http://www.emacswiki.org/cgi-bin/wiki/EmacsNiftyTricks
(add-hook 'help-mode-hook
          (function (lambda () (define-key help-mode-map "l" 'help-go-back))))
 
;; Copy to X-clipboard on C-x C-c
(defun x-set-clipboard-content-from-selection (begin end)
  "Sets X clipboard content to selected text."
  (interactive "r")
  (let ((x-select-enable-clipboard t))
    (x-select-text (buffer-substring begin end))))
 
(global-set-key "\C-xc" 'x-set-clipboard-content-from-selection)
 
;;}}}
;;{{{ DWIW %
 
;; Based on http://www.zafar.se/bkz/Articles/EmacsTips
(global-set-key [(control ?%)] 'match-paren)
 
(defun match-paren ()
  "Go to the matching parenthesis if on parenthesis."
  (interactive)
  (cond ((and (not (= (point) (point-max)))
              (equal ?( (char-syntax (char-after))))
         (forward-list 1))
        ((and (not (= (point) (point-min)))
              (equal ?) (char-syntax (char-before))))
         (backward-list 1))))
 
;;}}}
;;{{{ Just one space
 
(defun mn-just-one-space ()
  "When called for the first time Works just like `just-one-space'.
When called second time deletes all spaces, tabs and new lines after
the point."
  (interactive)
  (if (equal last-command this-command)
      (delete-region (point)
                     (save-excursion (skip-chars-forward " \t\n") (point)))
    (just-one-space)))
 
(substitute-key-definition 'just-one-space 'mn-just-one-space
                           (current-global-map))
 
;;}}}
;;{{{ Tab - indent or rotate or complete
 
;; Rotate Text + indent-or-complete
;; v0.3 by Michal Nazarewicz <mina86/AT/tlen.pl>
;; http://www.emacswiki.org/cgi-bin/wiki/EmacsNiftyTricks
;; http://www.emacswiki.org/cgi-bin/wiki/RotateText
 
;; Definition of words
(defvar rotate-text-rotations
  '(("mina86" "mina86/AT/tlen.pl" "mina86/AT/mina86.com" "http://mina86.com/")
    ("max" "min")
    ("north" "east" "south" "west")
    ("left" "top" "right" "bottom"))
  "List of lists of words to rotate between.")
 
 
;; Rotates text
(defun rotate-text (text &optional rotations)
  "Rotates given string, ie. find string which follows given
string in ROTATIONS or nil if there is no such string. If
ROTATION is nil `rotate-text-rotations' is used."
  (rotate-text--in text (or rotations rotate-text-rotations)))
 
;; Internal rotates text
(defun rotate-text--in (text rotations)
  "Similar to `rotate-text' but if ROTATIONS is nil returns nil."
  (and rotations
       (or (rotate-text--in-list text (car rotations))
           (rotate-text--in text (cdr rotations)))))
 
;; Internal rotates text in list
(defun rotate-text--in-list (text list)
  "Searches for TEXT in LIST and returns string which is follows
it (or the first string in the list if TEXT is the last one). If
string is not found on the list returns nil."
  (let ((l list))
    (while (and l (not (string-equal text (car l))))
      (setq l (cdr l)))
    (and l (or (cadr l) (car list)))))
 
 
;; Roatetes text in buffer
(defun rotate-region (&optional beg end)
  "Rotates string in buffer between BEG and END. If string was
not found in `rotate-text-rotations' returns nil otherwise t. If
BEG is nil either `region-beginning' (if `use-region-p') or
begining of word is used as the default. If END is nil either
`region-end' or end of word is used as the default. If function
was called interactivly and mark was active it lets it active."
  (let ((use-region-p (use-region-p)))
    (save-excursion
      (setq beg
            (or beg
                (and use-region-p (region-beginning))
                (progn (or (looking-at "\\<") (forward-word -1)) (point))))
      (setq end
            (or end
                (and use-region-p (region-end))
                (progn (or (looking-at "\\>") (forward-word 1)) (point)))))
    (let ((str (rotate-text (buffer-substring beg end))))
      (when str
        (delete-region beg end)
        (goto-char beg)
        (insert str)
        (setq deactivate-mark (not (and (interactive-p) use-region-p)))
        t))))
 
;; hippie-expand try function
(defvar try-rotate-string--rotations)
(defvar try-rotate-string--num)
(defvar try-rotate-string--beg)
(defvar try-rotate-string--text)
 
(defun try-rotate-string (old)
  "Try to rotates text. The argument OLD has to be nil the first
call of this function, and t for subsequent calls (for further
possible completions of the same string). It returns t if a new
completion is found, nil otherwise.
 
This function can be added to `hippie-expand-try-functions-list'
or used interactivly. To do the former use:
 
  (setq hippie-expand-try-functions-list
        (append (list 'try-rotate-string) hippie-expand-try-functions-list))"
  (interactive (list (eq this-command last-command)))
  (if old
      (when (>= (setq try-rotate-string--num (1+ try-rotate-string--num))
                (length (car try-rotate-string--rotations)))
        (setq try-rotate-string--rotations (cdr try-rotate-string--rotations)
              try-rotate-string--num 0))
    (setq try-rotate-string--rotations rotate-text-rotations
          try-rotate-string--num 1
          try-rotate-string--beg (save-excursion (forward-word -1) (point))
          try-rotate-string--text
          (buffer-substring try-rotate-string--beg (point))))
 
  (let ((text (buffer-substring try-rotate-string--beg (point)))
        replace ret)
    (while (and (not replace) try-rotate-string--rotations)
      (unless (setq replace (rotate-text--in-list
                             text (car try-rotate-string--rotations)))
        (setq try-rotate-string--rotations (cdr try-rotate-string--rotations)
              try-rotate-string--num 0
              text try-rotate-string--text)))
 
    (when (or replace old)
      (delete-region try-rotate-string--beg (point))
      (goto-char try-rotate-string--beg)
      (insert (or replace try-rotate-string--text)))
    replace))
 
(setq hippie-expand-try-functions-list
      '(try-rotate-string
; 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 ()
  "In minibuffer runs `minibuffer-complete'. Otherwise if
`use-region-p' runs `indent-region'. Otherwise if point is at end of
a word runs `inent-or-complete-complete-function'. Otherwise runs
`indent-for-tab-command'."
  (interactive)
  (cond
   ((and (fboundp 'minibufferp) (minibufferp)) (minibuffer-complete))
   ((use-region-p) (indent-region (region-beginning) (region-end)))
   ((set 'indent-or-complete--last-was-complete
         (or (looking-at "\\>")
             (and (eq last-command this-command)
                  indent-or-complete--last-was-complete)))
    (funcall indent-or-complete-complete-function nil))
   ((indent-for-tab-command))))
 
;(global-set-key "\t" 'indent-or-complete)
;(global-set-key [(tab)] 'indent-or-complete)
(global-set-key "\M-/" 'hippie-expand)
(add-hook 'find-file-hooks (function (lambda ()
  (unless (eq major-mode 'org-mode)
    (local-set-key [(tab)] 'indent-or-complete)))))
(save-current-buffer
  (set-buffer "*scratch*")
  (local-set-key [(tab)] 'indent-or-complete))
 
;;}}}
;;{{{ Centering
 
(defvar centerer--window-start nil)
 
(defun centerer ()
  "Repositions current line: once - middle, twice - top, three and
four times - `reposition-window', five times - bottom, six times -
where it was at the beginning.
   If performing some action doesn't really change position of the
window function skips to another step."
   (interactive)
   (let ((i 0) (old (window-start)))
     (while (and (<= (setq i (1+ i)) 6) (equal (window-start) old))
       (seq-times-do nil (setq centerer--window-start (window-start))
         (recenter)
         (recenter 0)
         (reposition-window)
         (reposition-window)
         (recenter -1)
         (set-window-start (selected-window) centerer--window-start)))))
 
(global-set-key "\C-l" 'centerer)
 
;;}}}
;;{{{ Filling
 
;; Alt+q - Fill
(unless (fboundp 'fill-paragraph-or-region)
  ;; Taken from emacs 23.0.50.1 (CVS)
  (defun fill-paragraph-or-region (arg)
    "Fill the active region or current paragraph.
IF `use-region-p' then it calls `fill-region', otherwise --
`fill-paragraph'."
    (interactive (progn (barf-if-buffer-read-only)
                        (list (if current-prefix-arg 'full))))
    (if (use-region-p)
        (fill-region (region-beginning) (region-end) arg)
      (fill-paragraph arg))))
 
(defun my-fill (&optional arg)
  (interactive "*P")
  (if arg
    (fill-region (save-excursion (beginning-of-line) (point))
                 (save-excursion (end-of-line) (point))
                 (seq-times-nth () () left full right center))
    (fill-paragraph-or-region (seq-times-nth () () left full right center))))
 
(global-set-key "\M-q" 'my-fill)
 
;; Alt+Q - Unfill
(defun th-unfill-paragraph-or-region ()
  "Do the opposite of `fill-paragraph-or-region'; stuff all lines
in the current paragraph into a single long line."
  (interactive)
  (let ((fill-column most-positive-fixnum))
    (if (use-region-p)
        (fill-region (region-beginning) (region-end) nil)
      (fill-paragraph nil)))
  (setq deactivate-mark nil))
 
(global-set-key "\M-Q" 'th-unfill-paragraph-or-region)
 
;;}}}
;;{{{ Fkeys
 
;;{{{ Key sequences
 
(global-set-key "\eOP" [(f1)])
(global-set-key "\e[[A" [(f1)])
(global-set-key "\eOQ" [(f2)])
(global-set-key "\e[[B" [(f2)])
(global-set-key "\eOR" [(f3)])
(global-set-key "\e[[C" [(f3)])
(global-set-key "\eOS" [(f4)])
(global-set-key "\e[[D" [(f4)])
(global-set-key "\e[15~" [(f5)])
(global-set-key "\e[[E" [(f5)])
(global-set-key "\e[17~" [(f6)])
(global-set-key "\e[18~" [(f7)])
(global-set-key "\e[19~" [(f8)])
(global-set-key "\e[20~" [(f9)])
(global-set-key "\e[21~" [(f10)])
(global-set-key "\e[23~" [(f11)])
(global-set-key "\e[24~" [(f12)])
(global-set-key "\eO5P" [(control f1)])
(global-set-key "\eO5Q" [(control f2)])
(global-set-key "\eO5R" [(control f3)])
(global-set-key "\eO5S" [(control f4)])
(global-set-key "\e[15;5~" [(control f5)])
(global-set-key "\e[17;5~" [(control f6)])
(global-set-key "\e[18;5~" [(control f7)])
(global-set-key "\e[19;5~" [(control f8)])
(global-set-key "\e[20;5~" [(control f9)])
(global-set-key "\e[21;5~" [(control f10)])
(global-set-key "\e[23;5~" [(control f11)])
(global-set-key "\e[24;5~" [(control f12)])
(global-set-key "\eO6P" [(control shift f1)])
(global-set-key "\eO6Q" [(control shift f2)])
(global-set-key "\eO6R" [(control shift f3)])
(global-set-key "\eO6S" [(control shift f4)])
(global-set-key "\e[15;6~" [(control shift f5)])
(global-set-key "\e[17;6~" [(control shift f6)])
(global-set-key "\e[18;6~" [(control shift f7)])
(global-set-key "\e[19;6~" [(control shift f8)])
(global-set-key "\e[20;6~" [(control shift f9)])
(global-set-key "\e[21;6~" [(control shift f10)])
(global-set-key "\e[23;6~" [(control shift f11)])
(global-set-key "\e[24;6~" [(control shift f12)])
(global-set-key "\eO3P" [(meta f1)])
(global-set-key "\eO3Q" [(meta f2)])
(global-set-key "\eO3R" [(meta f3)])
(global-set-key "\eO3S" [(meta f4)])
(global-set-key "\e[15;3~" [(meta f5)])
(global-set-key "\e[17;3~" [(meta f6)])
(global-set-key "\e[18;3~" [(meta f7)])
(global-set-key "\e[19;3~" [(meta f8)])
(global-set-key "\e[20;3~" [(meta f9)])
(global-set-key "\e[21;3~" [(meta f10)])
(global-set-key "\e[23;3~" [(meta f11)])
(global-set-key "\e[24;3~" [(meta f12)])
(global-set-key "\eO4P" [(meta shift f1)])
(global-set-key "\eO4Q" [(meta shift f2)])
(global-set-key "\eO4R" [(meta shift f3)])
(global-set-key "\eO4S" [(meta shift f4)])
(global-set-key "\e[15;4~" [(meta shift f5)])
(global-set-key "\e[17;4~" [(meta shift f6)])
(global-set-key "\e[18;4~" [(meta shift f7)])
(global-set-key "\e[19;4~" [(meta shift f8)])
(global-set-key "\e[20;4~" [(meta shift f9)])
(global-set-key "\e[21;4~" [(meta shift f10)])
(global-set-key "\e[23;4~" [(meta shift f11)])
(global-set-key "\e[24;4~" [(meta shift f12)])
(global-set-key "\eO2P" [(shift f1)])
(global-set-key "\e[23~" [(shift f1)])
(global-set-key "\eO2Q" [(shift f2)])
(global-set-key "\e[24~" [(shift f2)])
(global-set-key "\eO2R" [(shift f3)])
(global-set-key "\e[25~" [(shift f3)])
(global-set-key "\eO2S" [(shift f4)])
(global-set-key "\e[26~" [(shift f4)])
(global-set-key "\e[15;2~" [(shift f5)])
(global-set-key "\e[28~" [(shift f5)])
(global-set-key "\e[17;2~" [(shift f6)])
(global-set-key "\e[29~" [(shift f6)])
(global-set-key "\e[18;2~" [(shift f7)])
(global-set-key "\e[31~" [(shift f7)])
(global-set-key "\e[19;2~" [(shift f8)])
(global-set-key "\e[32~" [(shift f8)])
(global-set-key "\e[20;2~" [(shift f9)])
(global-set-key "\e[33~" [(shift f9)])
(global-set-key "\e[21;2~" [(shift f10)])
(global-set-key "\e[34~" [(shift f10)])
(global-set-key "\e[23;2~" [(shift f11)])
(global-set-key "\e[24;2~" [(shift f12)])
 
;;}}}
;;{{{ F1 - Help
 
(defun my-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)))))))
 
(global-set-key [(f1)] 'my-help)
 
;;}}}
;;{{{ F2 - find configuration files
 
(defmacro mn-kbd-find-file (file)
  `(function (lambda () (interactive) (find-file ,file))))
 
(global-set-key [(f2)] (mn-kbd-find-file
                                (if (string-match "\\.elc$" user-init-file)
                                    (if (file-exists-p
                                         (substring user-init-file 0 -1))
                                        (substring user-init-file 0 -1)
                                        (substring user-init-file 0 -4))
                                  user-init-file)))
(global-set-key [(control f2)] (mn-kbd-find-file "~/.gnus/.gnus.el"))
(global-set-key [(meta f2)] (mn-kbd-find-file custom-file))
;(global-set-key [(shift f2)] (mn-kbd-find-file "~/.bashrc"))
 
;;}}}
;;{{{ F5 - Gnus
 
;; http://www.emacswiki.org/cgi-bin/wiki/SwitchToGnus
 
(defun switch-to-gnus-or-get-new-news (&optional arg)
  "If already in Gnus group retrive new news otherwise, if Gnus
is running, switch to Gnus group buffer, otherwise start Gnus"
  (interactive "p")
  (cond
   ((eq major-mode 'gnus-group-mode)
    (and (fboundp 'gnus-group-get-new-news) (gnus-group-get-new-news)))
   ((and (boundp 'gnus-group-buffer) (fboundp 'gnus-alive-p) (gnus-alive-p))
    (switch-to-buffer (with-no-warnings gnus-group-buffer)))
   ((gnus))))
 
(global-set-key [(f5)] 'switch-to-gnus-or-get-new-news)
;(set-key-lambda [(control f5)] (let ((b (current-buffer)))
; (switch-to-buffer (other-buffer))
; (bury-buffer b)))
 
;;}}}
;;{{{ F7 - spell checking
 
(require 'ispell)
 
;; my ispell-change-dictionary bugfix now commited to CVS
;; not need for it here
 
(defvar mn-spell-dictionaries '("polish" "british")
  "List of dictionaries to cycle through with `mn-spell-switch-dictionary'.")
 
(eval-after-load "ispell"
  (ispell-change-dictionary "polish" t))
 
(defun mn-spell (&optional lang start end)
  "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 arg)
  "Switches dictionary to the next dictionary from `mn-spell-dictionaries'.
With a prefix arg sets global dictionary."
  (interactive "P")
  (ispell-change-dictionary
   (let ((dic (or (and (not arg) 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)))
   arg))
 
 
(set-key-lambda [(f7)] (mn-spell))
(set-key-lambda [(control f7)] (mn-spell nil (point)))
(global-set-key [(meta f7)] 'mn-spell-switch-dictionary)
(set-key-lambda [(shift f7)]
 (call-interactively
  (if (and (boundp 'flyspell-mode) flyspell-mode)
      'flyspell-correct-word-before-point 'ispell-word)))
 
;;}}}
;;{{{ F8 - Keyboard macros
 
(global-set-key [(f8)] 'kmacro-end-or-call-macro)
(global-set-key [(control f8)] 'kmacro-start-macro-or-insert-counter)
; Note also that as of EMACS 22.1 those are bind to F4 and F3
; respectively; I like mine bindings more though
 
;;}}}
;;{{{ 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)))
 
  "List of 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)
  "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.
 
Afterwards, if touch is non-nil marks buffer as modified. Saves
buffer (`save-buffer') and executes `recompile' if recompile is
non-nill or `compile' otherwise."
 
  (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))))
 
 
(global-set-key [(f9)] 'mn-compile) ; F9 compile
(global-set-key [(control f9)] ; C-F9 recompile
                (function (lambda (a) (interactive "P") (mn-compile a t))))
(global-set-key [(meta f9)] ; M-F9 force alt comp.
                (function (lambda (a) (interactive "P") (mn-compile (not a) t t))))
 
;;}}}
 
;;}}}
;;{{{ ISearch mode
 
(add-hook 'isearch-mode-hook (lambda ()
 (define-key isearch-mode-map "\C-h" 'isearch-mode-help)
 (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp)
 (define-key isearch-mode-map "\C-c" 'isearch-toggle-case-fold)
 (define-key isearch-mode-map "\C-j" 'isearch-edit-string)
; (define-key isearch-mode-map "\C-h" 'isearch-del-char)
 (define-key isearch-mode-map [backspace] 'isearch-del-char)
))
 
;;}}}
;;{{{ Copy/Kill
 
;; Copy w/o Selection
;; http://www.emacswiki.org/cgi-bin/wiki/CopyWithoutSelection
 
(defun copy-word (&optional arg)
  "Copy words at point"
  (interactive "P")
  (let ((beg (progn (if (looking-back "[a-zA-Z]" 1) (backward-word 1)) (point)))
        (end (progn (forward-word arg) (point))))
    (copy-region-as-kill beg end)))
 
(defun copy-line (&optional arg)
  "Save current line into Kill-Ring without mark the line "
  (interactive "P")
  (let ((beg (line-beginning-position))
        (end (line-end-position)))
    (copy-region-as-kill beg end)))
 
(defun copy-paragraph (&optional arg)
  "Copy paragraphes at point"
  (interactive "P")
  (let ((beg (progn (backward-paragraph 1) (point)))
        (end (progn (forward-paragraph arg) (point))))
    (copy-region-as-kill beg end)))
 
(global-set-key "\C-cw" 'copy-word)
(global-set-key "\C-cl" 'copy-line)
(global-set-key "\C-cp" 'copy-paragraph)
 
 
;; Make C-w, M-w work on word if no selection
(set-key-lambda "\C-w" (call-interactively
                        (if (use-region-p) 'kill-region 'kill-word)))
(set-key-lambda "\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)))
 
;; C-S-w, M-S-w, C-S-y -- works on registers
(defvar copy-or-append-to-register--register nil)
 
(defun copy-or-append-to-register (register start end &optional delete append)
  (interactive
   (let ((append-p (equal last-command this-command)))
     (list
      (or (and append-p copy-or-append-to-register--register)
          (read-char (if append-p
                       "Append to register: " "Copy to register: ") nil nil))
      (if (use-region-p) (region-beginning) (point))
      (if (use-region-p) (region-end) (progn (forward-word 1) (point)))
      prefix-arg
      append-p)))
  (if append
      (append-to-register register start end delete)
    (copy-or-append-to-register register start end delete))
  (setq copy-or-append-to-register--register register))
 
(defun kill-or-append-to-register ()
  (setq prefix-arg t)
  (call-interactively 'copy-or-append-to-register))
 
(global-set-key [(control shift w)] 'copy-or-append-to-register)
(global-set-key [(meta shift w)] 'kill-or-append-to-register)
(global-set-key [(control shift y)] 'insert-register)
 
;;}}}
 
;;}}}
;;{{{ Syntax highlighting
 
;; Font lock
(require 'font-lock)
(global-font-lock-mode t)
(setq font-lock-verbose nil) ;no messages
 
;; Let customize keep config there
(setq custom-file (concat user-emacs-directory "custom"))
(if (file-exists-p custom-file) (load-file custom-file))
(if (boundp 'auto-byte-compile-files-list)
    (setq auto-byte-compile-files-list
          (cons custom-file auto-byte-compile-files-list)))
 
 
;; Other
(setq query-replace-highlight t) ;highlight during query
(setq search-highlight t) ;highlight incremental search
(show-paren-mode t) ;show matching parenthesis.
(setq blink-matching-paren-distance nil) ;search for open-paren till point-min
 
;; Show blanks and FIXME
;; http://www.emacswiki.org/cgi-bin/wiki/EightyColumnRule
 
(defface my-tab-face
  '((((type graphical) (class color) (min-colors 216)) (:underline "#666"))
    (((class color) (min-colors 216)) (:background "#666"))
    (((class color) (min-colors 16)) (:background "yellow")))
  "Face for showing TABs."
  :group 'basic-faces)
 
(defface my-big-indent-face
  '((((type graphical) (class color) (min-colors 216)) (:underline "#966"))
    (((class color) (min-colors 216)) (:background "#966"))
    (((class color) (min-colors 16)) (:background "red"))
    (t :inverse-video t))
  "Face for showing sequence of four or five TAB characters in a row."
  :group 'basic-faces)
 
(defface my-huge-indent-face
  '((((type graphical) (class color) (min-colors 216)) (:underline "#F00"))
    (((class color) (min-colors 216)) (:background "#C66"))
    (((class color) (min-colors 16)) (:background "magenta"))
    (t :inverse-video t))
  "Face for showing sequence of at least six five TAB characters in a row."
  :group 'basic-faces)
 
(defface my-fixme-face
  '((t :background "red" :foreground "white" :weight bold))
  "Font for showing FIXME and XXX words."
  :group 'basic-faces)
 
(defface my-todo-face
  '((t :foreground "red" :weight bold))
  "Font for showing TODO words."
  :group 'basic-faces)
 
 
(add-hook 'font-lock-mode-hook (function (lambda ()
  (unless (or (eq 'diff-mode major-mode) (eq 'script-mode major-mode))
    (font-lock-add-keywords nil
     '(("\t+" 0 'my-tab-face t)
       ("^\t\\{4,5\\}" 0 'my-big-indent-face t)
       ("^\t\\{6,\\}" 0 'my-huge-indent-face t)
       ("\\<\\(TODO:?\\)\\>" 1 'my-todo-face t)
       ("\\<\\(FIXME:?\\|XXX\\)\\>" 1 'my-fixme-face t)))))))
 
;;}}}
;;{{{ Misc small config
 
;;Save some space
(setq use-dialog-box nil) ;never use dialog boxes
(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))
(and window-system (load "fringe" t)
     (set-fringe-mode (if (string-match "^erwin" system-name) 7 4)))
 
 
;; Modeline
(setq line-number-mode t) ;show line number in modeline
(setq 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 "%2c, " "")
                 (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))))
        ") "))
(if (and (string-match "^tuptus" system-name)
         (fboundp 'display-battery-mode)
         (not window-system))
    (display-battery-mode 1)) ;show battery on my laptop but only in TTY
;(setq display-time-24hr-format t) ;show time in 24h format
;(setq display-time-interval 30) ;update time & load avg every 30s
;(display-time-mode 1) ;show time in modeline
 
 
;; Other
(icomplete-mode 1) ;nicer completion in minibuffer
(define-key minibuffer-local-map "\C-c" ; C-c clears minibuffer
  (lambda () (interactive) (delete-minibuffer-contents)))
(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);don't show splash screen
(setq inhibit-splash-screen t) ;don't show splash screen
(setq inhibit-startup-echo-area-message "mina86") ;don't show "For info..." message
(setq inhibit-startup-buffer-menu t) ;don't show buffer menu when oppening
                                  ; many files (EMACS 21.4+)
(setq sentence-end-double-space 1);sentance end with double space
(setq truncate-lines nil) ;wrap lines
(fset 'yes-or-no-p 'y-or-n-p) ;make yes/no be y/n
(setq require-final-newline t) ;always end file with NL
(setq indicate-empty-lines t) ;show empty lines at the end of file
(setq x-alt-keysym 'meta) ;treat Alt as Meta even if real Meta found
(setq gc-cons-threshold 4000000) ;bytes before garbage collection
(setq ange-ftp-try-passive-mode t);passive FTP
;(setq ange-ftp-ftp-program-name "pftp") ;passive FTP
(blink-cursor-mode nil) ;do not blink cursor
;(setq mouse-autoselect-window nil) ;focus follows mouse
(when (boundp 'compilation-auto-jump-to-first-error)
  (setq compilation-auto-jump-to-first-error t))
(if (boundp 'line-move-visual)
    (setq line-move-visual nil)) ;move by logical lines not screen lines
 
;; Saving etc
(if (fboundp recentf-mode)
    (recentf-mode nil)) ;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
                                  ; it makes all folds unfload :(
                                  ; and what's worse - sux with ftp
 
 
;; Indention
(defun tab-stop-list (&optional width columns)
  "Returns list of all positive integers ordered from smallest to
largest which are multiplications of WIDTH and are lower or equal
COLUMNS. WIDTH defaults to `tab-width' and COLUMNS to 120."
  (if (not width) (setq width tab-width))
  (when (<= width (or columns 120))
    (let* (l (n (/ (or columns 120) width )))
      (while (> (setq l (cons (* n width) l) n (1- n)) 0)) l)))
 
(setq indent-tabs-mode t) ;indent using tabs
(setq c-basic-offset 4) ;4 char indent in cc-mode
(setq tab-width 4) ;tab width
(setq tab-stop-list (eval-when-compile (tab-stop-list 4)))
;; One could add this to check if tab-width and c-basic-offset correspond
;(if (not (= 0 (% c-basic-offset tab-width)))
; (warn "`tab-width' does not divide `c-basic-offset'"))
 
 
;; Regionsm selections and marks
(setq mouse-yank-at-point t) ;mouse yank at point not at cursor (X-win)
(setq kill-read-only-ok t) ;be silent when killing text from RO buffer
(delete-selection-mode 1) ;deleting region by typing or del (like Win)
(setq transient-mark-mode t)
(setq set-mark-command-repeat-pop t)
 
 
;; Scrolling/moving
(setq scroll-step 1) ;scroll one line
;(setq scroll-preserve-screen-position t) ;keep point's position on screen
(setq hscroll-step 1) ;scroll one column
(setq next-line-add-newlines nil) ;no new lines with down arrow key
 
 
;; Coding system
(set-keyboard-coding-system 'iso-8859-2)
(set-terminal-coding-system 'iso-8859-2)
 
 
;; Blink Scroll Lock LED instead of beep
(setq visible-bell nil) ;no visual bell
(setq ring-bell-function (function (lambda ()
  (call-process-shell-command "xset led 3; xset -led 3" nil 0 nil))))
 
 
;; Each list element as new paragraph
;; http://www.emacswiki.org/cgi-bin/wiki/FillParagraph
(setq paragraph-start " *\\([*+-]\\|\\([0-9]+\\|[a-zA-Z]\\)[.)]\\|$\\)"
      paragraph-separate "$")
 
 
;; GDB
(setq gdb-many-windows t
      gdb-show-main t
      gdb-use-separate-io-buffer nil)
 
 
;; Do not break line after single character when filling
(defun fill-single-char-nobreak-p ()
  "Don't break line after a single character."
  (save-excursion
    (skip-chars-backward " \t")
    (backward-char 2)
    (looking-at "[[:space:]][a-zA-Z]")))
 
(add-to-list 'fill-nobreak-predicate 'fill-single-char-nobreak-p)
 
 
;; Sort words in region
;; http://www.emacswiki.org/emacs/SortWords
(defun sort-words (reverse beg end)
  "Sort words in region alphabetically, in REVERSE if negative.
Prefixed with negative \\[universal-argument], sorts in reverse.
The variable `sort-fold-case' determines whether alphabetic case
affects the sort order.
See `sort-regexp-fields'."
  (interactive "P\nr")
  (sort-regexp-fields reverse "\\w+" "\\&" beg end))
 
;;}}}
;;{{{ Major Modes
 
;;{{{ C Default compile command
 
(eval-after-load "compile"
  (progn
    (defun cc-make-compile-command ()
      "Sets `compile-command' to \"make -k <file-name-no-ext>\"."
      (let ((fn buffer-file-name) p)
        (when fn
          (setq fn (file-name-nondirectory fn)
                p (string-match "\\.[^\\.]*$" fn 1))
          (set (make-local-variable 'compile-command)
               (concat "make -k " (substring fn 0 p))))))
 
    (add-hook 'c-common-mode-hook 'cc-make-compile-command)))
 
;;}}}
;;{{{ HTML/XML & comapny Mode
 
;; Create a link out of the preceeding word if it appears to be a URL
(if (load "thingatpt" t)
    (defun mn-linkify-maybe ()
      "If the word before cursor appears to be an URL wrap it around
in a <a href=\"...\">...</a>. Returns whether it happend."
      (interactive "*")
      (when (save-excursion
              (save-restriction
                (let ((end (point)))
                  (beginning-of-line)
                  (narrow-to-region (point) end)
                  (looking-at
                   (concat ".*\\(" thing-at-point-url-regexp "\\)\\'")))))
        (let ((url (delete-and-extract-region (match-beginning 1)
                                              (match-end 1))))
          (insert "<a href=\"" url "\">" url "</a>"))))
  (defun mn-linkify-maybe ()
    "If the word before cursor appears to be an URL wrap it
around in a <a href=\"...\">...</a>. Returns whether it happend.
 
This is however a stub implementation (because thingatpt could not be loaded) which does nothing and returns nul."
    nil))
 
 
(defun mn-magick-self-insert-command (spec &optional default when-prefix)
  "If prefix argument is not nil calls WHEN-PREFIX with single
argument being a `prefix-numeric-value' or the `prefix-arg'.
 
Otherwise looks through SPEC which is a list of 3-element
lists (called rules):
  (regexp length action)
Rule is said to match if `point' is at least LENGTH characters from
`point-min' and REGEXP matches starting at position LENGTH characters
earlier then `point'. ACTION of first rule that matches is performed.
If no rule matches DEFAULT is treated as action to perform.
 
If ACTION to perform is a string (DEFAULT may not be a string)
then `replace-match' is called otherwise it is assumed ACTION is
a function and it is called with single argument 1.
 
DEFAUTL and WHEN-PREFIX defaults to `self-insert-command'."
  (if prefix-arg
      (funcall (or when-prefix 'self-insert-command)
               (prefix-numeric-value prefix-arg))
    (let ((action (catch 'done
                    (let ((dist (- (point) (point-min))))
                      (dolist (rule spec)
                        (when (>= dist (cadr rule))
                          (save-excursion
                            (backward-char (cadr rule))
                            (when (looking-at (car rule))
                              (throw 'done (cadr (cdr rule))))))))
                    default)))
      (if (stringp action) (replace-match action)
        (funcall (or action 'self-insert-command) 1)))))
 
(defmacro mn-magick-self-insert-define
  (map key spec &optional default when-prefix)
  `(define-key ,map ,key
     (lambda () (interactive ,"*")
       (mn-magick-self-insert-command ,spec ,default ,when-prefix))))
 
(defun mn-xml-configure-bindings (mode-map)
  (mn-magick-self-insert-define mode-map " "
                                '(("[^<]\\b\\w" 2 "\\&&nbsp;")
                                  ("^\\w" 1 "\\&&nbsp;")
                                  ("&nbsp;" 6 " "))
                                (lambda (n)
                                  (mn-linkify-maybe)
                                  (self-insert-command n)))
  (mn-magick-self-insert-define mode-map "<"
                                '(("<" 1 "&lt;")
                                  ("&lt;" 4 "&#xab;")))
  (mn-magick-self-insert-define mode-map ">"
                                '(("&nbsp;&#8211;" 13 " -->")
                                  (">" 1 "&gt;")
                                  ("&gt;" 4 "&#xbb;")))
  (mn-magick-self-insert-define mode-map "&"
                                '(("&" 1 "&amp;")))
  (mn-magick-self-insert-define mode-map "."
                                '(("\\.\\." 2 "&#8230;")))
  (mn-magick-self-insert-define mode-map "\""
                                '(("\"" 1 "&quot;")))
  (mn-magick-self-insert-define mode-map "~"
                                '(("~" 1 "&nbsp;")))
  (mn-magick-self-insert-define mode-map "'"
                                '(("'" 1 "&#8221;")))
  (mn-magick-self-insert-define mode-map "`"
                                '(("`" 1 "&#8220;")))
  (mn-magick-self-insert-define mode-map ","
                                '(("," 1 "&#8222;")))
  (mn-magick-self-insert-define mode-map "."
                                '(("\\.\\." 2 "&#8230;")))
  (mn-magick-self-insert-define mode-map "-"
                                '(("[-!]-" 2 nil)
                                  (" -" 2 "&nbsp;&#8211;")
                                  ("-" 1 "&nbsp;&#8211;")
                                  ("&#8211;" 7 "&#8212;")
                                  ("\\\\" 1 "&8209;")))
  ;; (mn-magick-self-insert-define mode-map "/"
  ;; '(("<" 1
  ;; (lambda (n)
  ;; (backward-delete-char 1)
  ;; (sgml-close-tag)))))
  (mn-magick-self-insert-define mode-map [(backspace)]
                                '(("&lt;" 4 "<")
                                  ("&#xab;" 6 "&lt;")
                                  ("&gt;" 4 ">")
                                  ("&#xbb;" 6 "&gt;")
                                  ("&amp;" 5 "&")
                                  ("&quot;" 6 "\"")
                                  ("&#8230;" 7 "..")
                                  ("&#8220;" 7 "`")
                                  ("&#8221;" 7 "'")
                                  ("&#8222;" 7 ",")
                                  ("&#8212;" 7 "&#8211;")
                                  ("&#82\\(?:11\\|09\\);" 7 "-")
                                  ("&.....;" 7 "")
                                  ("&....;" 6 "")
                                  ("&...;" 5 "")
                                  ("&..;" 4 ""))
                                'delete-backward-char
                                'delete-backward-char)
  (setq indent-tabs-mode ()))
 
(eval-when-compile (require 'sgml-mode))
(eval-after-load "sgml-mode"
  (add-hook 'sgml-mode-hook
            (lambda () (mn-xml-configure-bindings sgml-mode-map))))
 
(when (fboundp 'nxml-mode)
  (eval-when-compile (require 'nxml-mode))
  (add-to-list 'auto-mode-alist '("\\.\\(?:x\\|ht\\)ml\\'" . nxml-mode))
  (eval-after-load "nxml-mode"
    (add-hook 'nxml-mode-hook
              (lambda () (mn-xml-configure-bindings nxml-mode-map)))))
 
 
 
 
(defun replace-string-pairs (list)
  (let* ((mark (use-region-p))
         (start (if mark (region-beginning) (point)))
         (end (if mark (region-end) (point-max))))
    (save-excursion
      (dolist (pair list)
        (let ((diff (- (length (cdr pair)) (length (car pair)))))
          (goto-char start)
          (while (search-forward (car pair) end t)
            (setq end (+ end diff))
            (replace-match (cdr pair) nil t)))))))
 
(defun html-escape ()
  "Replaces HTML special characters with HTML entities.
 
In Transient Mark mode, if the mark is active, operate on the contents
of the region. Otherwise, operate from point to the end of the buffer."
  (interactive)
  (replace-string-pairs
   '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;") ("\"" . "&quot;"))))
 
(defun html-unescape ()
  "Replaces (some) HTML entities with characters.
 
In Transient Mark mode, if the mark is active, operate on the contents
of the region. Otherwise, operate from point to the end of the buffer."
  (interactive)
  (replace-string-pairs
   '(("&amp;" . "&") ("&lt;" . "<") ("&gt;" . ">") ("&quot;" . "\""))))
 
;;}}}
;;{{{ (La)TeX and nroff mode
 
;; Helper for tex-space
(defmacro my-tex-looking-back (regexp len)
  "Returns non-nil if text LEN chars backward matches REGEXP
prefixed with \\b."
  `(save-excursion
     (backward-char ,len)
     (looking-at ,(concat "\\b" regexp))))
 
;; insert '~' or '\ ' instead of ' ' in LaTeX when needed
;; Also removes '~' when 2nd space added
;; http://www.debianusers.pl/article.php?aid=39
(defun tex-space (arg) (interactive "P")
  (cond
   ((re-search-backward "\\~" (- (point) 1) t)
    (delete-char 1) (self-insert-command (prefix-numeric-value arg)))
   (arg (self-insert-command (prefix-numeric-value arg)))
   ((my-tex-looking-back "[a-z]" 1)
    ; (my-tex-looking-back "[a-z][a-z]" 2))
    (insert-char ?~ 1))
   ((or (my-tex-looking-back "[a-z][a-z]\\." 3)
        (my-tex-looking-back "\\(?:tz[wn]\\|it[pd]\\)\\." 4))
    (insert "\\ "))
   (t (self-insert-command 1))))
 
(eval-when-compile (require 'tex-mode))
(add-hook 'tex-mode-hook
          (function (lambda () (define-key tex-mode-map " " 'tex-space))))
(add-hook 'latex-mode-hook
          (function (lambda () (define-key tex-mode-map " " 'tex-space))))
 
 
;; Insert '\ ' instead of ' ' in nroff when needed
;; Also removes '\' when 2nd space added
(defun nroff-space (arg) (interactive "p")
  (cond
   ((re-search-backward "\\\\ " (- (point) 2) t)
    (delete-char 1) (forward-char) (self-insert-command arg))
   ((> arg 1) (self-insert-command arg))
   ((my-tex-looking-back "[a-z]" 1)
; (my-tex-looking-back "[a-z][a-z]" 2))
; (my-tex-search-back "do\\|na\\|od\\|po\\|za\\|we\\|to\\|co" 2))
    (insert-char ?\\ 1) (self-insert-command 1))
   (t (self-insert-command 1))))
 
(eval-when-compile (require 'nroff-mode))
 
(add-hook 'nroff-mode-hook
          (function (lambda () (define-key nroff-mode-map " " 'nroff-space))))
 
;;}}}
;;{{{ Generate serialVersionUID in Java
 
(defun mn-serialVersionUID (&optional insert)
  "When called interactivly or with a non nil argument inserts
a random 64-bit hexadecimal integer prefixed with \"0x\" and
suffixed with \"L\". When called with nil argument or with
argument ommited returns that number."
  (interactive (list t))
  (if insert
      (insert (mn-serialVersionUID))
    (format "0x%04x%04x%04x%04xL"
            (random 65536) (random 65536)
            (random 65536) (random 65536))))
 
(add-hook 'java-mode-hook (lambda ()
  (add-hook 'write-contents-functions (lambda ()
    (save-excursion
      (save-restriction
        (widen)
        (goto-char (point-min))
        (while (re-search-forward "serialVersionUID\s+=\s+[0-9a-fA-FxX]+L;"
                                  nil t)
          (replace-match
           (concat "serialVersionUID = " (mn-serialVersionUID) ";")
           nil t))))))))
 
;;}}}
;;{{{ Misc
 
;; Text mode
(add-hook 'text-mode-hook (function (lambda ()
    (auto-fill-mode t)
    (setq tab-width 8)
    (setq tab-stop-list
    (set (make-local-variable 'tab-stop-list)
         (eval-when-compile (tab-stop-list 8)))))))
 
 
;; Assembler mode
(add-hook 'asm-mode-hook (function (lambda ()
    (set-variable 'tab-width 8)
    (set-variable 'tab-stop-list (tab-stop-list 16))
    (set-variable 'comment-column 40))))
 
 
;; Org mode
(setq org-insert-mode-line-in-empty-file t)
 
;; PHP mode
;; http://sourceforge.net/projects/php-mode
(autoload 'php-mode "php-mode" "PHP editing mode" t)
(add-to-list 'auto-mode-alist '("\\.php\\'" . php-mode))
 
 
;; JavaScript mode
(add-to-list 'auto-mode-alist '("\\.js\\'" . c-mode))
 
 
;; Shell mode
;; This prevents shell commands from being echoed
(eval-when-compile (require 'shell))
(add-hook 'comint-mode-hook (function (lambda ()
  (setq comint-process-echoes t))))
 
 
;; Lisp/Scheme mode
;; No tabs!
(let ((func (function (lambda () (setq indent-tabs-mode ())))))
  (add-hook 'emacs-lisp-mode-hook func)
  (add-hook 'lisp-mode-hook func)
  (add-hook 'scheme-mode-hook func))
 
 
;; Sawfish mode
(autoload 'sawfish-mode "sawfish" "Mode for editing Sawfish config files")
(add-to-list 'auto-mode-alist '(".*sawfishrc\\'" . sawfish-mode))
(add-to-list 'auto-mode-alist '(".*\\.jl\\'" . sawfish-mode))
 
 
;; FVWM mode
;; http://groups.google.fr/groups?threadm=mailman.2630.1083314129.1061.help-gnu-emacs%40gnu.org
(autoload 'fvwm-mode "fvwm-mode" "Mode fore editing fvwm's config files" t)
(add-to-list 'auto-mode-alist '("\\.fv\\(wm2?\\(rc\\)?\\)?$" . fvwm-mode))
 
 
;; Bison mode
(autoload 'bison-mode "bison-mode.el")
(add-to-list 'auto-mode-alist '("\\.y$" . bison-mode))
(setq bison-decl-type-column 8)
(setq bison-decl-token-column 16)
 
 
;; Flex
(autoload 'flex-mode "flex-mode")
(add-to-list 'auto-mode-alist '("\\.l$" . flex-mode))
 
 
;; Script mode
;(autoload 'script-mode "script-mode" "Mode fore editing scripts" t)
 
 
;; LSL mode
(autoload 'lsl-mode "lsl-mode" "Load LSL mode." t)
(add-to-list 'auto-mode-alist '("\\.lsl$" . lsl-mode))
(add-hook 'lsl-mode-hook (function (lambda nil (setq indent-tabs-mode nil))))
 
 
;; Haskell mode
(when (eval-when-compile
        (file-directory-p (concat user-emacs-directory "elisp/haskell-mode")))
  (load (concat user-emacs-directory "elisp/haskell-mode/haskell-site-file"))
  (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
  (add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
  ;;(add-hook 'haskell-mode-hook 'turn-on-haskell-simple-indent)
   (add-hook 'haskell-mode-hook
             (lambda ()
               (setq comment-padding " "
                     comment-start "--"))))
 
;;}}}
 
;;}}}
;;{{{ Various features
 
;;{{{ git
 
(when (eval-when-compile (not (string-match "^pikus" system-name)))
  (autoload 'git-status "git" "Entry point into git-status mode." t)
  ;(add-to-list 'vc-handled-backends 'GIT)
  (setq vc-handled-backends '(CVS SVN Git GIT))
  (autoload 'git-blame-mode "git-blame"
    "Minor mode for incremental blame for Git." t))
 
;;}}}
;;{{{ HTMLize
 
;; http://fly.srk.fer.hr/~hniksic/emacs/htmlize.el
(autoload 'htmlize-buffer "htmlize" "Convert buffer to HTML" t)
(autoload 'htmlize-region "htmlize" "Convert region to HTML" t)
 
;;}}}
;;{{{ Folding
 
(when (load "folding")
  (defconst folding-default-keys-function
    '(folding-bind-backward-compatible-keys))
 
  (define-key folding-mode-map "\C-cf" 'folding-toggle-show-hide)
  (define-key folding-mode-map [(control ?c) (return)] 'folding-shift-in)
  (define-key folding-mode-map [(control ?c) (delete)] 'folding-shift-out)
  (define-key folding-mode-map [(control ?c) (backspace)] 'folding-shift-out)
  (define-key folding-mode-map "\C-c\C-f" 'folding-open-buffer)
  (define-key folding-mode-map "\C-cF" 'folding-whole-buffer)
  (define-key folding-mode-map "\C-e" 'my-end)
 
  (folding-add-to-marks-list 'php-mode "// {{{" "// }}}" nil t)
  (folding-add-to-marks-list 'sawfish-mode ";;{{{" ";;}}}" nil t)
  (folding-add-to-marks-list 'javascript-mode "// {{{" "// }}}" nil t)
  (folding-add-to-marks-list 'css-mode "/* {{{" "/* }}}" " */" t)
 
  (folding-mode-add-find-file-hook))
 
;;}}}
;;{{{ Different cursor color depending on mode
 
;; http://www.emacswiki.org/cgi-bin/wiki/EmacsNiftyTricks
(defvar hcz-set-cursor-color-color "")
(defvar hcz-set-cursor-color-buffer "")
(defun hcz-set-cursor-color-according-to-mode ()
  "Change cursor color according to some minor modes."
  ;; set-cursor-color is somewhat costly, so we only call it when needed:
  (let ((color (cond (buffer-read-only "blue")
                     (overwrite-mode "red")
                     (t "yellow"))))
    (unless (and
             (string= color hcz-set-cursor-color-color)
             (string= (buffer-name) hcz-set-cursor-color-buffer))
      (set-cursor-color (setq hcz-set-cursor-color-color color))
      (setq hcz-set-cursor-color-buffer (buffer-name)))))
 
(add-hook 'post-command-hook 'hcz-set-cursor-color-according-to-mode)
 
;;}}}
;;{{{ Command Frequency mode
 
(require 'command-frequency)
(setq command-frequency-table-file
      (concat user-emacs-directory "frequencies"))
(command-frequency-table-load)
(command-frequency-mode 1)
(command-frequency-autosave-mode 1)
 
;;}}}
;;{{{ Notes in *scratch*
 
;; Notes in *scratch* v. 0.2
;; Copyright (c) 2006 by Michal Nazarewicz (mina86/AT/mina86.com)
;; Released under GNU GPL
 
(defconst scratch-file (concat user-emacs-directory "scratch")
  "File where content of *scratch* buffer will be read from and saved to.")
(defconst scratch-file-autosave (concat scratch-file ".autosave")
  "File where to autosave content of *scratch* buffer.")
 
(save-excursion
  (set-buffer (get-buffer-create "*scratch*"))
  (if (file-readable-p scratch-file)
      (if (and (file-readable-p scratch-file-autosave)
               (file-newer-than-file-p scratch-file-autosave scratch-file)
               (y-or-n-p "Recover scratch file? "))
          (insert-file-contents scratch-file-autosave nil nil nil t)
        (insert-file-contents scratch-file nil nil nil t)
        (set-buffer-modified-p nil)))
  (auto-save-mode 1)
  (setq buffer-auto-save-file-name scratch-file-autosave)
  (set (make-local-variable 'revert-buffer-function) 'scratch-revert)
  (fundamental-mode))
 
(defun scratch-revert (ignore-auto noconfirm)
  (when (file-readable-p scratch-file)
    (insert-file-contents scratch-file nil nil nil t)
    (set-buffer-modified-p nil)))
 
(defun kill-scratch-buffer ()
  (not (when (string-equal (buffer-name (current-buffer)) "*scratch*")
         (delete-region (point-min) (point-max))
         (set-buffer-modified-p nil)
         (next-buffer)
         t)))
 
(defun kill-emacs-scratch-save ()
  (save-excursion
    (set-buffer (get-buffer-create "*scratch*"))
    (write-region nil nil scratch-file)
    (unless (string-equal scratch-file buffer-auto-save-file-name)
      (delete-auto-save-file-if-necessary t))))
 
(add-hook 'kill-buffer-query-functions 'kill-scratch-buffer)
(add-hook 'kill-emacs-hook 'kill-emacs-scratch-save)
 
;;}}}
 
;; Start server
(server-start)
 
(random t)
 
;;}}}
;;{{{ Fun stuff ;)
 
(defconst q "This is EMACS not vi! Use C-x C-c instead.")
(defconst w "This is EMACS not vi! Use C-x C-s instead.")
(defconst q! q)
(defconst wq q)
(defconst wq! q)
 
 
;;}}}
 
;; Clear messages
;; You may want to comment it out for debuging
(setq message-log-max nil) ;disable messages
(kill-buffer "*Messages*") ;kill buffer with messages
 
;(when (file-directory-p gnus-home-directory) (gnus))
 
(if (fboundp 'emacs-init-time)
    (add-hook 'after-init-hook
              (function (lambda ()
                          (message "Emacs initialization took %s"
                                   (emacs-init-time))))))