Source

js-elisp / js-micro-features.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
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
;; Micro-features.  Loading this file should only affect the namespace.

;; Each definition has a commented-out line before it with (Jane.foo) on it.
;; To try out the feature without adding to your .emacs, put the cursor on
;; the right parenthesis and type C-xC-e (or whatever eval-last-sexp is bound to.



;; (Jane.advanced)
(defun Jane.advanced ()
  "Enable advanced commands, e.g. narrowing."
  (setq-default disabled-command-function nil)
  (put 'narrow-to-region 'disabled nil)
  (custom-set-variables
   '(enable-recursive-minibuffers t)
   '(minibuffer-depth-indicate-mode t)))



;; (Jane.auto-modes)
(defun Jane.auto-modes ()
  (require 'markdown-mode)
  (defvar auto-mode-ext
    (list
     '("\\.agda$" . agda2-mode)
     '("\\.ampl$" . ampl-mode)
     '("\\.cfg$" . twelf-mode)
     '("\\.cm$" . sml-cm-mode)
     '("\\.cs$" . csharp-mode)
     '("\\.dkal$" . dkal-mode)
     '("\\.dot$" . dot-mode)
     '("\\.elf$" . twelf-mode)
     '("\\.fun$" . sml-mode)
     '("\\.gl$" . prolog-mode)
     '("\\.grm$" . sml-yacc-mode)
     '("\\.h$" . c++-mode)
     '("\\.hl$" . hol-light-mode)
     '("\\.hs$" . haskell-mode)
     '("\\.htm$" . nxhtml-mode)
     '("\\.html$" . nxhtml-mode)
     '("\\.imo$" . text-mode)
     '("\\.jpg$" . image-mode)
     '("\\.jobs$" . scheme-mode)
     '("\\.js$" . js2-mode)
     '("\\.lex$" . sml-lex-mode)
     '("\\.lhs$" . literate-haskell-mode)
     ;;'("\\.m$" . matlab-mode)
     '("\\.m$" . octave-mode)
     '("\\.mkd$" . markdown-mode)
     '("\\.mlast$" . tuareg-mode)
     '("\\.ml[iylp4]?$" . tuareg-mode)
     '("\\.ml.part$" . tuareg-mode)
     '("\\.ML$" . sml-mode)
     '("\\.mlb$" . esml-mlb-mode)
     '("\\.mod$" . ampl-mode)
     '("\\.new$" . tuareg-mode)
     '("\\.ol$" . outline-mode)
     '("\\.old$" . tuareg-mode)
     '("\\.om$" . makefile-mode)
     '("\\.omdoc$" . xml-mode)
     '("\\.org$" . org-mode)
     '("\\.p$" . prolog-mode)
     '("\\.patdiff" . lisp-mode)
     '("\\.pl$" . prolog-mode)
     '("\\.plt$" . gnuplot-mode)
     '("\\.pp$" . tuareg-mode)
     '("\\.quy$" . twelf-mode)
     '("\\.s$" . asm-mode)
     '("\\.scm$" . scheme-mode)
     '("\\.scpt$" . ascript-mode)
     '("\\.sexp$" . lisp-mode)
     '("\\.shtml$" . nxml-mode)
     '("\\.sig$" . sml-mode)
     '("\\.sml$" . sml-mode)
     '("\\.st$" . nxhtml-mode)
     '("\\.svg$" . nxml-mode)
     '("\\.tex$" . LaTeX-mode)
     '("\\.v$" . coq-mode)
     '("\\.x$" . haskell-mode)
     '("\\.xhtml$" . nxhtml-mode)
     '("\\.xml$" . nxml-mode)
     '("\\.y$" . haskell-mode)
     ;; Config files
     '("\\.hgrc$" . shell-script-mode)
     '("_config_file.*\\.txt\\'" . lisp-mode)
     '("spec\\.txt\\'" . lisp-mode)
     '("core\\.cfg\\." . lisp-mode)
     ;; Omake
     '("OMakefile$" . makefile-mode)
     '("OMakeroot$" . makefile-mode)
     ;; Mutt
     '("/mutt-[^/]*\\'" . mail-mode)
     ))
  (setq auto-mode-alist (append auto-mode-ext auto-mode-alist)))



;; (Jane.auto-revert)
(defun Jane.auto-revert ()
  "update unmodified buffer when file changes on disk"
  (global-auto-revert-mode 1)
  (custom-set-variables '(revert-without-query '("")))
  ;; fixes font-lock colors after auto-revert -- bug in emacs?
  (add-hook 'after-revert-hook 'normal-mode))



;; (Jane.backups)
(defun Jane.backups ()
  "The following would put backups and auto-saves in /tmp/ which is
almost as fast as not doing it at all, but might be a security
issue."
  (custom-set-variables
   '(version-control t)                   ; Enable versioning
   '(delete-old-versions t)               ; don't ask when deleting old backups
   '(backup-directory-alist `((".*" . ,temporary-file-directory)))
   '(auto-save-file-name-transforms `((".*" ,temporary-file-directory t)))))



;; (Jane.bell)
(defun Jane.bell ()
  "blink the screen on real errors, for more normal stuff just say 'Quit' in the minibuffer"
  (setq ring-bell-function
        (lambda ()
          (unless (memq this-command
                        '(isearch-abort abort-recursive-edit exit-minibuffer keyboard-quit))
                                        ;tell me which command failed
            (if this-command (message (symbol-name this-command)))
            (ding)))))



;; (Jane.buffer-menu)
(defun Jane.buffer-menu ()
  "Utilities to manage the buffer menu"
  ;; Delete read-only buffers from the menu
  (defun delete-read-only-buffers ()
    (interactive)
    (defun d-buffer ()
      (interactive)
      (if (search-forward "%" nil t)
          (progn (beginning-of-line)
                 (Buffer-menu-delete)
                 t)))
    (save-excursion
      (goto-char (point-min))
      (while (d-buffer))
      (Buffer-menu-execute)))
  (add-hook 'Buffer-menu-mode-hook
            (lambda ()
              (local-set-key "\C-c\C-d" 'delete-read-only-buffers))))



;; (Jane.comint)
(defun Jane.comint ()
  (ansi-color-for-comint-mode-on)
  (custom-set-variables
   '(comint-password-prompt-regexp "password\\|passphrase\\|Encryption")
   '(comint-buffer-maximum-size 10000)
   '(comint-move-point-for-output t)
   '(ansi-color-for-comint-mode t))
  (setq comint-output-filter-functions
        '(ansi-color-process-output
          comint-strip-ctrl-m
          comint-watch-for-password-prompt
          ;;comint-truncate-buffer
          comint-postoutput-scroll-to-bottom)))



;; (Jane.compilation)
(defun Jane.compilation ()
  (defcustom js-ask-before-killing-compilations t
    "If you have compilation buffers, ask about killing them before killing emacs."
    :group 'js)

  (add-hook 'compilation-mode-hook
            (lambda ()
              (setq buffer-file-coding-system 'utf-8)))
  ;; stop emacs from asking you if you really want to quit when you
  ;; have a compilation running in the background (e.g. omake -P)
  (add-hook 'compilation-start-hook
            (lambda (process)
              (set-process-query-on-exit-flag process nil)))
  (defun js-kill-emacs-compilations-p ()
    (let ((has-compilations
           (find-if
            (lambda (b)
              (string-match "*compilation" (buffer-name b))) (buffer-list))))
      (if (and js-ask-before-killing-compilations has-compilations)
          (y-or-n-p
           "You have compilations running.  Do you really want to kill them? ")
        t)))
  ;; (setq kill-emacs-query-functions nil)
  (add-to-list 'kill-emacs-query-functions 'js-kill-emacs-compilations-p)
  (custom-set-variables
   '(compilation-ask-about-save nil)
   '(compilation-read-command nil)
   '(compilation-scroll-output t)
   '(compilation-window-height 18)
   '(next-error-highlight t)
   ;; recenter buffer with error
   '(next-error-recenter (quote (4)))
   ;; ocamlc numbers columns from 0, not from 1 (like, e.g., gcc)
   '(compilation-first-column 0)))



;; (Jane.completion)
(defun Jane.completion ()
  "Make completion ignore case"
  (setq completion-ignore-case t)
  (custom-set-variables
   '(case-fold-search t)
   '(read-buffer-completion-ignore-case t)
   '(read-file-name-completion-ignore-case t)))



;; (Jane.debug)
(defun Jane.debug ()
  "Debugging utilities"
  (custom-set-variables
   '(debug-on-error t)
   '(stack-trace-on-error t)
   '(eval-expression-debug-on-error t)))



;; (Jane.desktop)
(defun Jane.desktop (&optional dont-ask)
  "save a list of open files in ~/.emacs.desktop and re-open
when I restart emacs."
  (require 'desktop)
  (let ((dont-ask (if dont-ask t 'ask)))
  ;; save the desktop file automatically if it already exists
  (custom-set-variables
   '(desktop-file-name-format 'tilde)
   '(desktop-restore-eager 5)
   '(desktop-save dont-ask)
   '(desktop-globals-to-save
     '((extended-command-history . 30)
       (file-name-history        . 100)
       (grep-history             . 30)
       (compile-history          . 30)
       (minibuffer-history       . 50)
       (query-replace-history    . 60)
       (read-expression-history  . 60)
       (regexp-history           . 60)
       (regexp-search-ring       . 20)
       (search-ring              . 20)
       (shell-command-history    . 50)
       tags-file-name
       register-alist
       kill-ring)))
  (desktop-save-mode 1)
  (add-hook 'midnight-hook 'desktop-save-in-desktop-dir)))



;; (Jane.dictionary)
(defun Jane.dictionary ()
  (require 'dictionary)
  (global-set-key "\C-c1" 'dictionary-lookup-definition)
  (global-set-key "\C-c2" 'dictionary-search))



;; (Jane.dired)
(defun Jane.dired ()
  (require 'dired)
  (require 'dired-x)
  (add-hook 'dired-mode-hook
         (function (lambda ()
                     (dired-omit-mode 1)
                     )))
  (setq dired-deletion-confirmer 'y-or-n-p)
  (setq dired-guess-shell-alist-default nil)
  ;; Don't show junk in dired
  (setq dired-omit-extensions
        '(
          "#" ".DS_Store" ".a" ".annot" ".aux" ".bbl" ".bbl" ".blg"
          ".blg" ".brf" ".byte" ".class" ".cma" ".cmi" ".cmo" ".cmx"
          ".cmxa" ".deps" ".dvi" ".elc" ".glo" ".glob" ".gls" ".hc"
          ".hi" ".hp" ".hva" ".idx" ".ilg" ".ind" ".ist" ".libdeps"
          ".lo" ".loa" ".loe" ".lof" ".lot" ".ml.d" ".mli.d" ".native"
          ".nav" ".o" ".objdeps" ".omc" ".pdfsync" ".rel" ".snm" ".so"
          ".spit" ".spot" ".synctex.gz" ".toc" ".v.d" ".vo" ".vrb" "~"
          "hgignore.in" ".build_info.c" ".hg_version.c"
          ))
  (custom-set-variables
   '(dired-enable-local-variables nil)
   '(dired-recursive-copies 'always)
   '(dired-recursive-deletes 'top)
   '(dired-omit-size-limit nil)
   '(dired-guess-shell-alist-user
     (list
      (list "\\.tar.bz2\\'" "tar -xjf" "bunzip2")
      (list "\\.tar.gz\\'" "tar -xzf" "gunzip")
      (list "\\.bz2\\'" "bunzip2")
      (list "\\.bzip\\'" "bunzip2")
      (list "\\.dvi\\'" "dvi2pdf")
      (list "\\.gz\\'" "gunzip")
      (list "\\.pdf\\'" "acroread")
      (list "\\.ps\\'" "ps2pdf")
      (list "\\.tar\\'" "tar -xf")
      (list "\\.tbz\\'" "tar -xjf")
      (list "\\.tgz\\'" "tar -xzf" "gunzip")
      (list "\\.Z\\'" "unzip")
      (list "\\.zip\\'" "unzip")
      )
     )
   ;; Make autocomplete ignore these files also
   '(completion-ignored-extensions dired-omit-extensions)
   '(dired-omit-files "^\\..*$"))
  )



;; (Jane.ediff)
(defun Jane.ediff ()
  (custom-set-variables
   '(ediff-split-window-function (quote split-window-horizontally))))



;; 3rd party code, emacs responds to edit requests from Chrome in text boxes
(defun Jane.edit-with-emacs ()
  (require 'edit-server)
  (edit-server-start))



;; (Jane.faces-basic)
(defun Jane.faces-basic ()
  "Angry fruit salad faces"
  (custom-set-faces
   '(mode-line
     ((t (:background "orange" :foreground "black"
                      :box (:line-width -1 :style released-button)))))
   '(cursor ((t (:background "red"))))
   '(highlight ((t (:background "blue4"))))
   '(region ((t (:background "blue4"))))
   '(show-paren-match ((t (:background "gray25" :weight bold))))))



;; (Jane.faces-dark)
(defun Jane.faces-dark ()
  "faces for a black background most people like"
  (custom-set-faces
   '(font-lock-comment-face ((((class color) (background dark)) (:foreground "green3"))))
   ;; In tuareg-mode, docs (** ... *) are more similar to comments
   ;; (* ... *) than strings "..."
   '(font-lock-doc-face ((t (:inherit font-lock-comment-face))))
   '(font-lock-function-name-face
     ((((class color) (background dark)) (:foreground "LightSkyBlue" :weight semi-bold))))
   '(help-argument-name ((t (:background "gray25"))))
   '(mouse ((t (:background "purple"))))
   '(tuareg-font-lock-governing-face
     ((t (:inherit font-lock-keywords-face :foreground "orange" :weight bold))))
   '(tuareg-font-lock-operator-face
     ((t (:inherit font-lock-keywords-face :foreground "lightblue"))))
   '(whitespace-trailing ((t (:background "pink" :weight bold))))))



;; (Jane.faces-default)
(defun Jane.faces-default ()
  "The faces that come standard in Emacs"
  (custom-set-faces
   '(font-lock-builitin-face ((t (:foreground "MediumOrchid4"))))
   '(font-lock-comment-face ((t (:foreground "Firebrick"))))
   '(font-lock-constant-face ((t (:foreground "Dark cyan"))))
   '(font-lock-doc-face ((t (:inherit font-lock-doc-face))))
   '(font-lock-function-name-face ((t (:foreground "Blue1"))))
   '(font-lock-keyword-face ((t (:foreground "Purple"))))
   '(font-lock-string-face ((t (:foreground "VioletRed4"))))
   '(font-lock-type-face ((t (:foreground "ForestGreen"))))
   '(font-lock-variable-name-face ((t (:foreground "sienna"))))
   '(font-lock-warning-face ((t (:foreground "Red1"))))
   '(tuareg-font-lock-governing-face ((t (:foreground "blue"))))
   '(tuareg-font-lock-operator-face ((t (:foreground "brown"))))
   '(whitespace-trailing ((t (:background "pink" :weight bold))))
   ))



;; (Jane.faces-light)
(defun Jane.faces-light ()
  "faces for a light background most people like"
  (custom-set-variables
   '(js-background "white")
   '(js-foreground "black"))
  (Jane.faces-default)
  )



;; (Jane.faces-subtle)
(defun Jane.faces-subtle ()
  (Jane.faces-dark)
  (custom-set-faces
   '(cursor ((t (:background "HotPink1"))))
   '(highlight ((t (:background "blue4"))))
   '(region ((t (:background "gray20"))))
   '(show-paren-match ((t (:background "gray25" :bold t))))))



;; CR cfalls: Duplicated in Jane.text
;; (Jane.filladapt)
(defun Jane.filladapt ()
  "fills paragraphs nicely in text mode, understands bulleted lists (try M-q)"
  (require 'filladapt))



;; (Jane.flyspell)
(defun Jane.flyspell ()
  "incremental spell checking"
  (flyspell-prog-mode)
  (custom-set-variables
   '(flyspell-mark-duplications-flag nil)
   '(flyspell-persistent-highlight nil)
   '(ispell-dictionary "american"))
  (custom-set-faces
   '(flyspell-duplicate ((t (:underline t))))
   '(flyspell-incorrect ((t (:underline t))))))



;; (Jane.frame)
(defun Jane.frame ()
  "Frame utils."
  ;; Give frames and icons good names
  (setq-default frame-title-format (list "Emacs: %f"))
  (setq-default icon-title-format "Emacs - %b")
  (setq frame-inherited-parameters '(font background-color foreground-color))
  (custom-set-variables
   ;; prefer side-by-side buffers
   '(split-height-threshold nil)))



;; (Jane.global-keybindings)
(defun Jane.global-keybindings ()
  ;; Non C-c bindings
  (global-set-key "\C-xk" 'kill-this-buffer) ; don't ask me which buffer to kill
  (global-set-key "\C-xw" 'rename-file)
  (global-set-key "\M-\ " 'fixup-whitespace)

  ;; By default, buffers should auto-indent when I hit return
  (define-key global-map [remap newline] 'newline-and-indent)
  (global-set-key [(control .)] 'repeat)  ; overrides tags-apropos
  (global-set-key [(control =)] 'bounce-sexp)

  ;; C-c bindings
  (global-set-key "\C-ca" 'align-dwim)
  (global-set-key "\C-cb" 'comment-box)
  (global-set-key "\C-cc" 'comment-dwim)  ; comment (do what i mean)
  (global-set-key "\C-cg" 'goto-line)     ; default is M-g M-g
  (global-set-key "\C-ck" 'comment-kill)
  (global-set-key "\C-cr" 'cr-dwim)       ; defined in cr.el
)



;; (Jane.grep)
(defun Jane.grep ()
  ;; For recursive grep: M-x rgrep RET foo RET ml RET dir RET
  ;; search for "foo" in ML files under dir
  (require 'grep)
  (add-to-list 'grep-files-aliases '("omake" . "*.om OMake*"))
  (add-to-list 'grep-files-aliases '("ml" . "*.ml *.mli")))



;; (Jane.haskell)
(defun Jane.haskell ()
  (load-library "haskell-site-file.el")
  (require 'haskell-mode)
  (require 'haskell-indent)
  (add-hook 'haskell-mode-hook
            (lambda()
              (abbrev-mode 1)
              (haskell-indent-mode 1))))



;; (Jane.hg-blame-summary)
(defun Jane.hg-blame-summary ()
  "while editing an hg controlled file, find the last user to have modified
the current line, as well as printing some other statistics calculated from
'hg blame'"

  (defun my-hg-blame-summary ()
    (interactive)
    (catch 'nil-file-name
      (let (users
            (file-name (buffer-file-name))
            (buffer-line-number (line-number-at-pos))
            (summary (make-hash-table :test 'equal))
            (res-line-number 1)
            buffer-line-user)
        (if (not file-name)
            (throw 'nil-file-name
                   (message "ERROR: buffer has 'nil file name")))
        (dolist (line
                 (split-string
                  (shell-command-to-string (concat "hg blame -u -d '" file-name "'"))
                  "[\r\n]+" t))
          (let ((words (split-string line))
                user count-so-far)
            (setq user (elt words 0))
            (setq count-so-far (gethash user summary :not-found))
            (if (equal count-so-far :not-found)
                (puthash user 1 summary)
              (puthash user (+ 1 (gethash user summary)) summary))
            (if (equal res-line-number buffer-line-number)
                (setq buffer-line-user user))
            (setq res-line-number (+ res-line-number 1))))
        (maphash
         (lambda (user count)
           (setq users
                 (cons (concat user ":" (number-to-string count)) users)))
         summary)
        (message
         (concat "(" buffer-line-user "; "
                 "line " (number-to-string buffer-line-number)
                 "/" (number-to-string res-line-number) ") "
                 (mapconcat (function identity) (sort users 'string<) " "))))))
  (global-unset-key "\C-xvb")
  (global-set-key "\C-xvb" 'my-hg-blame-summary))



;; (Jane.ido)
(defun Jane.ido ()
  "Like iswitchb but for buffers and files, also more actively developed."
  (require 'ido)
  (ido-mode 1)
  (custom-set-variables
   ;; C-xb will re-open files I've opened in the past by looking at recentf
   '(ido-use-virtual-buffers t) ;CRv201207 cfalls: not supported in our emacs version
   '(ido-show-dot-for-dired t)
   '(ido-use-filename-at-point 'guess)
   '(ido-rotate-file-list-default t)
   '(ido-everywhere t)))



;; (Jane.iswitchb)
(defun Jane.iswitchb ()
  "Buffer management -- C-xb shows matching buffers interactively"
  (require 'iswitchb)
  (iswitchb-mode 1)
  (custom-set-variables
   '(read-buffer-function 'iswitchb-read-buffer)
   '(iswitchb-buffer-ignore '("^ " "*Completions"))
   ;; if buffer is in some other frame, just open another one in this window anyway
   '(iswitchb-default-method 'samewindow))
  ;; Make direction keys work in iswitchb buffer
  (add-hook 'iswitchb-define-mode-map-hook
            (lambda ()
              (mapc (lambda (K)
                      (let* ((key (car K)) (fun (cdr K)))
                        (define-key iswitchb-mode-map (read-kbd-macro key) fun)))
                    '(("<right>" . iswitchb-next-match)
                      ("<left>"  . iswitchb-prev-match)
                      ("<up>"    . ignore)
                      ("<down>"  . ignore))))))



;; (Jane.linum)
(defun Jane.linum ()
  "show line numbers in the left margin"
  (require 'linum))



;; (Jane.midnight)
(defun Jane.midnight ()
  "kills unmodified buffers that haven't been displayed in 3 days or more"
  (require 'midnight)
  (custom-set-variables
   '(midnight-mode t)))



;; (Jane.multiframe)
(defun Jane.multiframe ()
  (defun my-multiframe-window (&optional arg)
    (interactive "P")
    (if arg
        (previous-multiframe-window)
      (next-multiframe-window)))
  ;; switch between buffers in all frames by default
  ;; like raise-on-hover for emacs
  (custom-set-variables '(mouse-autoselect-window 0))
  (defun my-kill-frame ()
    "quit a frame the same way no matter what kind of frame you are on"
    (interactive)
    (if (eq (car (visible-frame-list)) (selected-frame))
        ;;for parent/master frame...
        (if (> (length (visible-frame-list)) 1)
            ;;close a parent with children present
            (delete-frame (selected-frame))
          ;;close a parent with no children present
          (save-buffers-kill-emacs))
      ;;close a child frame
      (delete-frame (selected-frame))))
  ;; easier to remember than C-x 5 2
  (global-set-key "\C-x\C-n" 'make-frame)
  (global-set-key "\C-x\C-c" 'my-kill-frame)
  (global-set-key "\C-xo" 'my-multiframe-window)
)



;; CR cfalls: Fails on non-version-controlled files, and is kind of
;; slow.  Also the weird "CR @author" stuff is probably a better way
;; to do it?

;; a fork of cr-todo that guesses who it's "for" with hg annotate
;; (Jane.new-cr-todo)
(defun Jane.new-cr-todo ()
  (defun cr-dwim ()               ; dwim = do what I mean
    "Add a code review comment starting on current line or cycle CR/CRvYYYYMM/XCR"
    (interactive)
    (comment-dwim nil)
    (cond ((looking-at "CR ")
           (replace-match
            ;; inserts CRvYYYYMM where date will be between today and a year from now
            (concat "CRv" (format-time-string
                           "%Y%m" (time-add (current-time)
                                            (encode-time 0 0 0 0 (+ (random 12) 3)
                                                         1970)))
                    " ")
            t t))
          ((looking-at "CRv[0-9]+ ")
           (replace-match "XCR " t t))
          ((looking-at "XCR ")
           (replace-match "CR " t t))
          (t
           (insert "CR " (user-login-name) " for "
                   (substring (shell-command-to-string
                               (concat "hg --config defaults.annotate= annotate "
                                       (buffer-file-name (current-buffer))
                                       " -u | /bin/sed -n '" (number-to-string (line-number-at-pos))
                                       "{s/^ *\\([a-z]*\\):.*/\\1/;p}; d'"
                                       )) 0 -1)
                   ": ")
           (backward-word)))
    (forward-word)
    (forward-char 2))
  )



;; (Jane.no-clutter)
(defun Jane.no-clutter ()
  "Turn off menu bars, tool bars, scroll bars, auto-save files, etc"
  ;; Solid cursor
  (blink-cursor-mode   -1)
  ;; Show column numbers in the mode line
  (column-number-mode   1)
  ;; No menu bar
  (menu-bar-mode       -1)
  ;; No scroll bar
  (scroll-bar-mode     -1)
  ;; No tool bar
  (tool-bar-mode       -1)
  ;; No backups or auto saves or version control -- they make things slower
  (custom-set-variables
   '(make-backup-files nil)
   '(auto-save-default nil)
   ;; text-only questions, please
   '(tooltip-mode nil)
   '(history-delete-duplicates t)
   ))



;; (Jane.omake-js)
(defun Jane.omake-js ()
  (require 'omake)
  (when (equal js-elisp-dir (expand-file-name "js-test" js-site-lisp-prod))
    (Omake.set-server-program "/mnt/global/base/bin/omake_server_test.exe"))
  ;; Make sure "jomake" is called by omake-server
  (setq Omake.omake-command "jomake"))



;; (Jane.omake-old)
(defun Jane.omake-old ()
  (require 'compile)
  (custom-set-variables '(compile-command "VERSION_UTIL_SUPPORT=false LINK_EXECUTABLES=false X_LIBRARY_INLINING=false jomake -j 7 -P -w"))
  (global-set-key [(control return)] 'compile)
  ;; When I run compile, if there's already a compilation in this
  ;; omake location then it asks if I'd like to kill it and start this
  ;; new one.  Otherwise, it just starts the new one, allowing
  ;; multiple compilations at the same time on different trees.  Their
  ;; buffers will be named sensibly.
  (defun my-compilation-buffer-name (mode-name)
    (concat "*" (downcase mode-name) ":" (get-omake-root-or-current-dir) "*"))
  (custom-set-variables
   '(compilation-buffer-name-function 'my-compilation-buffer-name)
   '(compilation-read-command t)
   '(compilation-ask-about-save nil)
   '(compilation-scroll-output t)
   '(next-error-highlight t)
   ;; recenter buffer with error
   '(next-error-recenter (quote (4)))
   ;; ocamlc numbers columns from 0, not from 1 (like, e.g., gcc)
   '(compilation-first-column 0)
   )
)


;; Display page delimiter as a horizontal line instead of "^L"
;; Insert page delimiters with C-qC-l
;; Page delimiters are a good replacement for "(**********)" comments
;; Pages can be navigated with C-x[ and C-x]
(defun Jane.pages ()
  (or standard-display-table (setq standard-display-table (make-display-table)))
  (aset standard-display-table ?\f (vconcat [? ? ] (make-vector 60 ?) [? ?])))



;; (Jane.perl)
(defun Jane.perl ()
  (defalias 'perl-mode 'cperl-mode)
  (setq-default cperl-hairy t)
  (setq-default cperl-under-as-char t))



;;(Jane.prefer-other-visible-frame)
(defun Jane.prefer-other-visible-frame ()
  (defvar non-special-display-buffer-names
    '("*Completions*" " *minibuf-isearch*")
    "Buffers that should *not* be handled by `prefer-other-visible-frame'.
   (I'd rather just exclude them in `special-display-regexps', but elisp regex
   doesn't support negative lookahead, ie (?!...), so i can't.)")

  (defvar special-display-switch-to-regexp
    "^.TAGS: "
    "Regexp for buffer names that should be selected (switched to) when
   handled by `prefer-other-visible-frame'.")

  (defun prefer-other-visible-frame (buffer &optional buffer-data)
    "If other frames are visible, display the buffer in one of them.
Otherwise, display the buffer in this frame in another window. If
there's only one window, split to create another. Also hide the
buffer in all windows other than the window it gets displayed in.

Intended to be set as `special-display-function'.

Ignores buffers in `non-special-display-buffer-names'.

I can *almost* do this with frame parameters alone,
e.g. visibility, except for forcing it to use a different frame
if possible. (same-frame . t) lets you force it to use the *same*
frame, but (same-frame . nil) doesn't force a different one.

http://www.gnu.org/software/emacs/manual/html_node/emacs/Special-Buffer-Frames.html

http://www.gnu.org/software/emacs/manual/html_node/emacs/Frame-Parameters.html

I wish i could just use `switch-to-buffer-other-window' here, but it calls
`display-buffer', which ends up calling this again. Sigh."
    ;; i'd like to use `other-frame', but it raises the frame :/
    (let ((window
           (cond
            ((member (buffer-name buffer) non-special-display-buffer-names) nil)
            ;; treat short buffers normally
            ;; ((> (with-current-buffer buffer (line-number-at-pos (buffer-end 1))) 15) nil)
            ;; is the buffer already displayed in another visible window?
            ((get-buffer-window buffer 'visible))            ;; find another window
            (t (let* ((existing (next-window (selected-window) 'never-minibuf 'visible))
                      (new (if (and existing (not (eq existing (selected-window))))
                                        ; found another existing one
                               existing
                                        ; couldn't find an existing one; splitting the current one
                             (split-window))))
                 (set-window-buffer new buffer)
                 new)))))
      (when (string-match special-display-switch-to-regexp (buffer-name buffer))
        (select-frame (window-frame window))
        (select-window window))
      window))

  (custom-set-variables
   '(special-display-function 'prefer-other-visible-frame)
   '(special-display-regexps '(".*"))  ; modulo non-special-display-buffer-names
   '(special-display-buffer-names nil))
)



;; (Jane.recentf)
(defun Jane.recentf ()
  "Remember recently opened files."
  (require 'recentf)
  (defvar my-local-directory (concat "/mnt/local/sda1/" (user-login-name)))
  (defun my-recentf-keep (file)
    ;; symlinks for nyc-qws-r07, shouldn't delete from recent list when on nyc-qws-024
    (if (and (string-match my-local-directory (file-truename file))
             (not (file-readable-p my-local-directory)))
        t
      (recentf-keep-default-predicate file)))
  (custom-set-variables
   '(recentf-max-saved-items 10000)
   '(recentf-keep '(my-recentf-keep))
   '(recentf-auto-cleanup 'never))
  (recentf-mode 1)
  (add-hook 'midnight-hook 'recentf-save-list))



;; (Jane.rectangle)
(defun Jane.rectangle ()
  "Customizations of rectangular selections"
  (define-key ctl-x-map [?r ?\C-\ ] 'rm-set-mark)
  (define-key ctl-x-map "r\C-x" 'rm-exchange-point-and-mark)
  (define-key ctl-x-map "r\C-w" 'rm-kill-region)
  (define-key ctl-x-map "r\M-w" 'rm-kill-ring-save)
  (autoload 'rm-set-mark "rect-mark"
    "Set mark for rectangle." t)
  (autoload 'rm-exchange-point-and-mark "rect-mark"
    "Exchange point and mark for rectangle." t)
  (autoload 'rm-kill-region "rect-mark"
    "Kill a rectangular region and save it in the kill ring." t)
  (autoload 'rm-kill-ring-save "rect-mark"
    "Copy a rectangular region to the kill ring." t))



;; (Jane.saveplace)
(defun Jane.saveplace ()
  "when re-opening a file, the point goes to its last location"
  (require 'saveplace)
  (custom-set-variables '(save-place t)))



;; (Jane.scrolling)
(defun Jane.scrolling ()
  "Scroll smoothly (one line at a time) -- better than scroll-margin"
  (require 'smooth-scrolling)
  (custom-set-variables
   '(smooth-scroll-margin 10)
   '(isearch-allow-scroll t)))



;; (Jane.selective-display)
(defun Jane.selective-display ()
  (defun js-selective-display ()
    "Activate selective display based on the column at point"
    (interactive)
    (set-selective-display
     (if selective-display
         nil
       (+ 1 (current-column)))))
  (global-set-key "\C-x$" 'js-selective-display))



;; (Jane.server)
(defun Jane.server ()
  "Server stuff"
  (require 'server)
  ;; If there's another server running, (server-start) will
  ;; fail.  Don't die if this happens.
  (unless (server-running-p) (server-start))
  (defun server-restart ()
    "Force a restart of the emacs server"
    (interactive)
    (progn
      (server-force-delete)
      (server-start)))
  ;; Close open server connections when you kill a buffer
  (add-hook 'kill-buffer-hook
            (lambda ()
              (server-buffer-done (current-buffer) t)))
  ;; This way, "crvet.exe review interactive -emacs" won't cause the
  ;; focus to shift to emacs.
  (custom-set-variables '(server-raise-frame nil)))



;; (Jane.sml)
(defun Jane.sml ()
  (require 'sml-mode)
  (add-hook 'sml-mode-hook
            (lambda()
              (abbrev-mode 1)
              (setq comment-start "(*")
              (setq comment-end "*)*")
              )))



;; (Jane.text)
(defun Jane.text ()
  (require 'filladapt)
  (add-hook 'text-mode-hook
            (lambda ()
              (turn-on-filladapt-mode)
              (turn-on-auto-fill))))



;; (Jane.time)
(defun Jane.time ()
  "show the time and date in the mode line"
  (display-time)
  (custom-set-variables
   '(display-time-day-and-date t)))



;; (Jane.uniquify)
(defun Jane.uniquify ()
  "When two files are open with the same name, emacs defaults to
FILE FILE<1> FILE<2>.  Uniquify gives more context."
  (require 'uniquify)
  (custom-set-variables
   ;; e.g. buffer name multi/OMakefile instead of OMakefile<2>
   '(uniquify-buffer-name-style 'forward)))



;; (Jane.vc)
(defun Jane.vc ()
  "Diffing, merging and resolving conflicts"
  (require 'smerge-mode)
  (let ((map (make-sparse-keymap))
        (keep-map (make-sparse-keymap)))
    (define-key map "o" 'smerge-diff-base-other)
    (define-key map "m" 'smerge-diff-base-mine)
    (define-key map "c" 'smerge-diff-mine-other)
    (define-key map "n" 'smerge-next)
    (define-key map "p" 'smerge-prev)
    (define-key map "r" 'smerge-resolve)
    (define-key map "R" 'smerge-resolve-all)
    (define-key map "k" keep-map)
    (define-key keep-map "o" 'smerge-keep-other)
    (define-key keep-map "m" 'smerge-keep-mine)
    (define-key global-map "\C-cd" map))
  (custom-set-variables
   '(ediff-combination-pattern
     '("<<<<<<< MINE" A
       "||||||| ANCESTOR" Ancestor
       "=======" B
       ">>>>>>> OTHER"))
   ;; don't open control buffer in a new frame
   '(ediff-window-setup-function 'ediff-setup-windows-plain)
   '(ediff-split-window-function 'split-window-horizontally)))


;; deprecated
(defun Jane.whitespace ()
  (interactive)
  (warn "Remove (Jane.whitespace) from your .emacs"))

(defun Jane.whitespace-simple ()
  (interactive)
  (warn "Remove (Jane.whitespace-simple) from your .emacs"))

(defun Jane.whitespace-fancy ()
  (interactive)
  (warn "Remove (Jane.whitespace-fancy) from your .emacs"))


;; (Jane.find)
(defun Jane.find ()
  (interactive)
  (defun Jane.make-find (hash-table-key-name dominating-file-name populate-hash-table)
    ;; The keys in hash-tables-by-dominating-directory are readlinked dominating
    ;; directories, e.g. for hg repos, the directory that contains the .hg/ subdirectory.
    ;; We use readlinked so that symlinks that lead to the same directory will use the same
    ;; key.
    ;;
    ;; The values in hash-tables-by-dominating-directory are themselves hashtables created
    ;; by [populate-hash-table].  The keys are strings used by [completing-read].  The
    ;; should be filenames relative to the dominating directory.
    (let ((hash-tables-by-dominating-directory (make-hash-table :test 'equal)))
      `(lambda (&optional force-rebuild)
         ,(format "find %s under dominating %s (prefix with C-u to rebuild index)"
                  hash-table-key-name dominating-file-name)
         (interactive "P")
         (let* ((readlinked-default-directory (Shell.readlink default-directory))
                (dominating-directory
                 (Filename.strip-final-slash
                  (locate-dominating-file readlinked-default-directory
                                          ,dominating-file-name)))
                (_ (if (null dominating-directory)
                       (error (format "Can not find dominating %s" ,dominating-file-name))))
                (hash-table
                 (or (and (not force-rebuild)
                          (gethash dominating-directory
                                   ,hash-tables-by-dominating-directory))
                     (progn
                       (message "computing index (may take a few seconds)")
                       (let ((hash-table (make-hash-table :test 'equal)))
                         (Jane.with-current-directory dominating-directory
                                                      (funcall ,populate-hash-table hash-table))
                         (puthash dominating-directory hash-table
                                  ,hash-tables-by-dominating-directory)
                         hash-table))))
                (path (gethash (if (fboundp 'anything-comp-read)
                                   (anything-comp-read (concat ,hash-table-key-name ": ") hash-table
                                                       :must-match t
                                                       :alistp nil
                                                       )
                                 (completing-read (concat ,hash-table-key-name ": ") hash-table
                                                  nil
                                                  t ; require a match
                                                  ))
                               hash-table)))
           (when path
             ;; We would like for the final file that we produce to use the same form as
             ;; [default-directory] if possible, rather than [readlink
             ;; default-directory].  I.e. we would rather not expand symlinks if we
             ;; don't have to.  So, we check if the folllowing equivalence holds:
             ;;
             ;;     readlink (locate-dominating-file default-directory)
             ;;   = locate-dominating-file (readlink default-directory)
             ;;
             ;; If that holds, then we can be sure that using the un-readlinked dominating
             ;; file in the final path that is returned will lead to the same place as
             ;; the readlinked [dominating-directory].
             (let ((dominating-directory-nice-name
                    (let ((candidate
                           (Filename.strip-final-slash
                            (locate-dominating-file default-directory
                                                    ,dominating-file-name))))
                      (if (and candidate
                               (equalp (Shell.readlink candidate) dominating-directory))
                          candidate
                        dominating-directory))))
               (find-file (expand-file-name path dominating-directory-nice-name))))))))

  (global-set-key "\C-cfp" 'Jane.find-file-in-project)
  (global-set-key "\C-cfl" 'Jane.find-library-in-project)
  (global-set-key "\C-cfr" 'Jane.find-file-in-repo)

  (let (
        ;; [populate-hash-table-with-all-hg-files] adds all files in every hg repo at or
        ;; below the current directory.  Note that "hg status --subrepos" can not be used
        ;; to do this.
        (populate-hash-table-with-all-hg-files
         (lambda (hash-table)
           (dolist (descendant-dir (process-lines "find" "." "-type" "d"))
             (when (file-exists-p (format "%s/%s" descendant-dir ".hg"))
               (dolist (file (Jane.with-current-directory descendant-dir
                                                          (process-lines "hg" "manifest")))
                 (let* ((file
                         ;; the [substring _ 2] drops the leading "./"
                         (substring (format "%s/%s" descendant-dir file) 2))
                        (dir (file-name-directory file))
                        (dir (if (= (length dir) 0) "." dir)))
                   (puthash (concat (file-name-nondirectory file) " in " dir "")
                            file
                            hash-table))))))))
    (fset 'Jane.find-file-in-repo
          (Jane.make-find "file" ".hg" populate-hash-table-with-all-hg-files))
    (fset 'Jane.find-file-in-project
          (Jane.make-find "file" "OMakeroot" populate-hash-table-with-all-hg-files))
    (fset 'Jane.find-library-in-project
          (Jane.make-find "library" "OMakeroot"
                          (lambda (hash-table)
                            (dolist (line
                                     (String.lines
                                      (shell-command-to-string
                                       "\
find . -type f \
| grep -E 'OMakefile$' \
| xargs grep -E 'OCamlMake(Ordinary)?Lib' \
| sed -rn 's_^\\./(.*)OMakefile:.*OCamlMake(Ordinary)?Lib(Package)?.*\\((.*)\\).*_\\1,\\4_p' \
")))
                              (let ((line (split-string line ",")))
                                (when (= (length line) 2)
                                  (let ((path (car line))
                                        (library (cadr line)))
                                    (puthash library path hash-table))))))))
    )
  )


(provide 'js-micro-features)