Skip to content

Instantly share code, notes, and snippets.

@jl2
Created November 23, 2011 21:22
Show Gist options
  • Save jl2/1389962 to your computer and use it in GitHub Desktop.
Save jl2/1389962 to your computer and use it in GitHub Desktop.
My .emacs and jl2_functions.el
(setq load-path (cons "~/emacs" load-path))
(setq load-path (cons "~/.emacs_stuff" load-path))
(setq load-path (cons "c:/emacs/site-lisp" load-path))
;; (setq load-path (cons "c:/emacs/site-lisp/color-theme" load-path))
(defadvice replace-string (around turn-off-case-fold-search)
(let ((case-fold-search nil))
ad-do-it))
(ad-activate 'replace-string)
(defadvice replace-regex (around turn-off-case-fold-search)
(let ((case-fold-search nil))
ad-do-it))
(ad-activate 'replace-regex)
(load "~/.emacs_stuff/jl2_functions.elc")
(load "~/.emacs_stuff/adl_commands.elc")
;; (load "~/.emacs_stuff/af_bookmarks.elc")
(setq vc-handled-backends nil)
(global-set-key "\C-\M-j" 'goto-line)
(global-set-key "\C-\M-c" 'comment-region)
(global-set-key "\C-\M-u" 'uncomment-region)
(global-set-key "\C-\M-t" 'lookup-tri)
(global-set-key "\C-\M-p" 'replace-python-exp)
(global-set-key [( control shift alt \[)] 'perl-func-doc)
(global-set-key "\C-\M-d" 'kill-word)
(global-set-key "\M-d" 'backward-kill-word)
;; (global-set-key [(control shift tab)] 'decrease-left-margin)
(global-set-key " " 'decrease-left-margin)
(global-set-key " " 'increase-left-margin)
;; (global-set-key [( control tab)] 'increase-left-margin)
(global-set-key [( control meta _)] 'shrink-window)
(global-set-key [( control +)] 'enlarge-window)
(global-set-key [?\C-.] 'next-error)
(global-set-key [?\C-\,] 'previous-error)
(global-set-key [?\C-\'] 'dabbrev-expand)
(global-set-key [mouse-4] 'scroll-down)
(global-set-key [mouse-5] 'scroll-up)
(global-set-key [?\M-\C-=] 'recompile)
(global-set-key [?\M-\C-+] 'compile)
(global-set-key [?\C-\]] 'match-paren)
(global-set-key [?\C-j] 'newline-and-indent)
(global-set-key [( control \) )] 'find-char)
(global-set-key [( control \( )] 'find-char-backward)
(global-set-key [( control meta \) )] 'find-char-before)
(global-set-key [( control meta \( )] 'find-char-backward-after)
(global-set-key [f3] 'adl_mk_priv)
(global-set-key [( control meta f3 )] 'adl_change_history)
(global-set-key [f4] 'adl_rollback)
(global-set-key [f5] 'jl2-revert)
; Search duckduckgo
(global-set-key [f6] 'ddg-default-search)
; Search duckduckgo, but give a "bang command"
(global-set-key "\C-\M-g" 'ddg-bang-search)
; Search in DS xref tool
(global-set-key [( control f6 )] 'xref-lookup)
(global-set-key [( meta z )] 'toggle-maximize)
(global-set-key [f7] 'toggle-kbd-macro-recording-on)
(global-set-key [f8] 'call-last-kbd-macro)
;; (global-set-key [(control f9)] 'af-bookmark-toggle )
;; (global-set-key [f9] 'af-bookmark-cycle-forward )
;; (global-set-key [(shift f9)] 'af-bookmark-cycle-reverse )
;; (global-set-key [(control shift f9)] 'af-bookmark-clear-all )
(global-set-key [(control meta {)] 'jl2-move-window)
(global-set-key [(control meta })] 'jl2-resize-window)
(define-key global-map "\C-cs" 'scheme-other-window)
(global-unset-key [( meta \ )])
;; (setq confirm-kill-emacs
;; (lambda (e)
;; (y-or-n-p-with-timeout
;; "Really exit Emacs (automatically exits in 15 secs)? " 15 t)))
(add-to-list 'auto-mode-alist '("\\.\\([pP][Llm]\\|al\\)\\'" . perl-mode))
(add-to-list 'interpreter-mode-alist '("perl" . perl-mode))
(add-to-list 'interpreter-mode-alist '("perl5" . perl-mode))
(add-to-list 'interpreter-mode-alist '("miniperl" . perl-mode))
;; mkmk compilation errors
(eval-after-load "compile" '(add-to-list 'compilation-error-regexp-alist '("^## \\+\\+ ERROR : see Output file in \\(.*\\)$" 1 nil)))
(put 'set-goal-column 'disabled nil)
;; Turn off annoying splitting in Emacs 23.1
(setq split-width-threshold nil)
(setq split-height-threshold 5)
(setq column-number-mode t)
(setq tool-bar-mode nil)
(setq menu-bar-mode nil)
(setq inhibit-startup-message t)
(setq inhibit-splash-screen t)
(setq compilation-window-height 20)
(setq grep-window-height 20)
;; (setq compile-command "ant -e -f c:/workspace/ProductionCenterDevBranch/fastbuild.xml")
(setq compile-command "mkmk -g -nomk -w -noci")
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(Info-additional-directory-list (quote ("c:/cygwin/usr/share/info/")))
'(backup-directory-alist (quote ((".*" . "c:\\backups"))))
'(blink-cursor-mode t)
'(c-default-style (quote ((c-mode . "k&r") (c++-mode . "java") (java-mode . "java") (awk-mode . "awk") (other . "gnu"))))
'(c-hanging-braces-alist (quote ((block-open after) (block-close . c-snug-do-while) (statement-cont) (substatement-open after) (brace-list-open) (brace-entry-open) (extern-lang-open after) (namespace-open after) (module-open after) (composition-open after) (inexpr-class-open after) (inexpr-class-close before) (arglist-cont-nonempty))))
'(c-offsets-alist (quote ((substatement-open . 0) (substatement-open . 0) (access-label . -) (statement-case-open . 0))))
'(column-number-mode t)
'(compilation-scroll-output t)
'(compilation-window-height 20)
'(describe-char-unidata-list (quote (name old-name general-category canonical-combining-class bidi-class decomposition decimal-digit-value digit-value numeric-value mirrored iso-10646-comment uppercase lowercase titlecase)))
'(fill-column 100)
'(font-lock-maximum-size nil)
'(grep-command "grep -i -n -e ")
'(grep-window-height 20)
'(gud-gdb-command-name "jdb -connect com.sun.jdi.SocketAttach:hostname=jl2d01spa,port=8000 -sourcepath c:\\workspace\\ProductionCenterDevBranch\\src")
'(gud-jdb-command-name "jdb -connect com.sun.jdi.SocketAttach:hostname=jl2d01spa,port=8000 -sourcepath c:\\workspace\\ProductionCenterDevBranch\\src")
'(gud-jdb-use-classpath nil)
'(indent-tabs-mode nil)
'(initial-frame-alist (quote ((top . 0) (left . 80) (width . 160) (height . 60) (cursor-color . "red") (cursor-type . box) (tool-bar-lines . 0) (menu-bar-lines . 0))))
'(menu-bar-mode nil)
;; '(nxhtml-global-minor-mode t)
;; '(nxhtml-minor-mode-modes (quote (nxml-mode html-mode sgml-mode xml-mode php-mode css-mode javascript-mode java-mode image-mode dired-mode)))
;; '(nxhtml-skip-welcome t)
'(python-default-interpreter (quote py))
'(python-use-skeletons nil)
'(scroll-bar-mode (quote left))
'(shell-file-name "cmdproxy.exe")
'(show-paren-mode t)
'(size-indication-mode t)
'(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60)))
'(tab-width 4)
'(temp-buffer-max-height 20)
'(tool-bar-mode nil)
'(transient-mark-mode nil)
'(visible-cursor t)
'(which-func-modes t)
'(which-function-mode t))
(require 'color-theme)
(color-theme-initialize)
(color-theme-charcoal-black)
(setq frame-title-format
'("" (buffer-file-name "%f" (dired-directory
dired-directory "%b"))))
;; ;; Added to use tabs instead of spaces
;; ;; Turn on tabs
(setq indent-tabs-mode nil)
(setq-default indent-tabs-mode nil)
(setq which-function-mode t)
;; Set the tab width
(setq tab-width 4)
(setq tab-width 4)
(setq c-basic-offset 4)
(fset 'yes-or-no-p 'y-or-n-p)
(load "~/.emacs_stuff/haskell-mode-2.8.0/haskell-site-file")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
(put 'downcase-region 'disabled nil)
(add-hook 'find-file-hook 'bh-choose-header-mode)
(put 'upcase-region 'disabled nil)
(add-to-list 'auto-mode-alist '("\\.cs$" . c++-mode))
(add-to-list 'auto-mode-alist '("\\.el$" . lisp-mode))
(add-to-list 'auto-mode-alist '("\\.emacs$" . lisp-mode))
(add-to-list 'auto-mode-alist '("\\.pyw$" . python-mode))
(setq auto-mode-alist (cons '("\\.lua$" . lua-mode) auto-mode-alist))
(autoload 'lua-mode "lua-mode" "Lua editing mode." t)
(setq inferior-lisp-program "c:/sbcl/sbcl.exe --noinform")
(setq ruby-program-name "c:\\Ruby192\\bin\\irb.bat --inf-ruby-mode")
(autoload 'ruby-mode "ruby-mode"
"Mode for editing ruby source files" t)
(setq auto-mode-alist
(append '(("\\.rb$" . ruby-mode)) auto-mode-alist))
(setq interpreter-mode-alist (append '(("ruby" . ruby-mode))
interpreter-mode-alist))
(autoload 'run-ruby "inf-ruby"
"Run an inferior Ruby process")
(autoload 'inf-ruby-keys "inf-ruby"
"Set local key defs for inf-ruby in ruby-mode")
(add-hook 'ruby-mode-hook
'(lambda ()
(inf-ruby-keys)))
;; (load "~/.emacs_stuff/gauche_indent.elc")
(setq python-check-command "pyc")
(setq display-time-day-and-date t)
(display-time)
(require 'server)
(when (and (= emacs-major-version 23)
(= emacs-minor-version 1)
(equal window-system 'w32))
(defun server-ensure-safe-dir (dir) "Noop" t)) ; Suppress error "directory
; ~/.emacs.d/server is unsafe"
(defun jl2-ruby-mode-hook ()
(setq comint-process-echoes t)
(turn-on-font-lock)
(auto-fill-mode))
;;; Register Ruby Mode Hook
(add-hook 'ruby-mode-hook 'jl2-ruby-mode-hook)
(server-start)
(defun find-char (chr)
"Similar to vim's f command"
(interactive "c")
(search-forward (char-to-string chr) nil 't))
(defun find-char-backward (chr)
"Similar to vim's F command"
(interactive "c")
(search-backward (char-to-string chr) nil 't))
(defun find-char-before (chr)
"Similar to vim's f command"
(interactive "c")
(search-forward (char-to-string chr) nil 't)
(backward-char))
(defun find-char-backward-after (chr)
"Similar to vim's F command"
(interactive "c")
(search-backward (char-to-string chr) nil 't)
(forward-char))
(defun match-paren (arg)
"Go to the matching brace."
(interactive "p")
(if (looking-at "[[({\"\']") (forward-sexp 1) (backward-sexp 1)))
(defun run-perl-one-liner (str)
"Execute a Perl one liner and display the result."
(interactive "sPerl: ")
(progn
(message (shell-command-to-string (concat "perl -e \"" (replace-regexp-in-string "\"" "\\\\\"" str) "\"")))))
(defun make-pdf ()
"Create a PDF from the current buffer"
(interactive)
(let ((ps_name (concat (getenv "TEMP") "/" (buffer-name) ".ps"))
(pdf_name (concat (getenv "TEMP") "/" (buffer-name) ".pdf")))
(ps-print-buffer ps_name)
(shell-command-to-string (concat "ps2pdf " ps_name " " pdf_name))
(shell-command-to-string (concat "rm " ps_name))
(find-file pdf_name)
(message (concat "Created PDF file " pdf_name))))
(defun perl-func-doc ()
(interactive)
(let* ((ival (thing-at-point 'word))
(fname (read-string "Function name: " ival)))
;; (interactive "sFunction name: " (thing-at-point 'word))
(message (shell-command-to-string (concat "perldoc -f " fname " | head -20")))))
(defun toggle-kbd-macro-recording-on ()
"One-key keyboard macros: turn recording on."
(interactive)
(substitute-key-definition 'toggle-kbd-macro-recording-on 'toggle-kbd-macro-recording-off (current-global-map))
(start-kbd-macro nil))
(defun toggle-kbd-macro-recording-off ()
"One-key keyboard macros: turn recording off."
(interactive)
(substitute-key-definition 'toggle-kbd-macro-recording-off 'toggle-kbd-macro-recording-on (current-global-map))
(end-kbd-macro))
(defun add-include ()
"When a class name is selected, adds a #include \"classname.h\" after the last #include."
(interactive)
(save-excursion
(let ((inc-name (buffer-substring-no-properties (region-beginning) (region-end)))
(rbeg)
(rend))
(search-backward-regexp "^#include")
(setq rbeg (point))
(end-of-line)
(newline)
(insert (format "#include \"%s.h\"" inc-name))
(setq rend (point))
(indent-region rbeg rend))))
;; Try to be smart about whether a .h file is C, C++, or Objective-C
;; Taken from:
;; http://www.bretthutley.com/emacs/opening-a-cobjective-cc-header-file-in-emacs/
(defun bh-choose-header-mode ()
(interactive)
(if (string-equal (substring (buffer-file-name) -2) ".h")
;; OK, we got a .h file, if a .m file exists we'll assume it's
; an objective c file. Otherwise, we'll look for a .cpp file.
(if (> (length (file-expand-wildcards "*.cpp")) 0)
(c++-mode)
(if (> (length (file-expand-wildcards "*.m")) 0)
(objc-mode)))))
(fset 'perl-starter
[?# ?! ?/ ?u ?s ?r ?/ ?b ?i ?n ?/ ?p ?e ?r ?l ?\C-j ?\C-j ?u ?s ?e ? ?s ?t ?r ?i ?c ?t ?\; ?\C-j ?u ?s ?e ? ?w ?a ?r ?n ?i ?n ?g ?s ?\; ?\C-j ?\C-j ?u ?s ?e ? ?G ?e ?t ?o ?p ?t ?: ?: ?L ?o ?n ?g ?\; ?\C-j ?\C-j ?s ?u ?b ? ?u ?s ?a ?g ?e ? ?\{ ?\C-j ?p ?r ?i ?n ?t ? ?\" ?. ?. ?. ?\\ ?n ?\" ?\; ?\C-j ?\} ?\C-j ?\C-j ?s ?u ?b ? ?m ?a ?i ?n ? ?\{ ?\C-j ?m ?y ? ?$ ?h ?e ?l ?p ?\; ?\C-j ?\C-j ?G ?e ?t ?O ?p ?t ?i ?o ?n ?s ?\( ?\' ?h ?e ?l ?p ?! ?\' ?= ?> ?\\ ?$ ?h ?e ?l ?p ?, ?\C-j ?\) ?\; ?\C-j ?\C-j ?i ?f ? ?\( ?$ ?h ?e ?l ?p ?\) ? ?\{ ?\C-j ?u ?s ?a ?g ?e ?\; ?\C-j ?e ?x ?i ?t ?\( ?0 ?\) ?\; ?\C-j ?\} ?\C-j ?\C-j ?\} ?\C-j ?\C-j ?m ?a ?i ?n ?\; ?\C-j ?\C-p ?\C-p ?\C-p ?\C-p ?\C-e tab])
(fset 'python-start
[?\M-x ?p ?y ?t ?h ?o ?n ?- ?m ?o ?d ?e ?\C-j ?# ?! ?/ ?u ?s ?r ?/ ?b ?i ?n ?/ ?e ?n ?v ? ?p ?y ?t ?h ?o ?n ?3 ?\C-m ?\C-m ?i ?m ?p ?o ?r ?t ? ?s ?y ?s ?\C-m ?\C-m ?d ?e ?f ? ?m ?a ?i ?n ?\( ?a ?r ?g ?s ?\) ?: ?\C-j ?\C-j backspace ?i ?f ? ?_ ?_ ?n ?a ?m ?e ?_ ?_ ?= ?= ?\' ?_ ?_ ?m ?a ?i ?n ?_ ?_ ?\' ?: ?\C-j ?m ?a ?i ?n ?\( ?s ?y ?s ?. ?a ?r ?g ?v ?\[ ?1 ?: ?\] ?\) ?\C-j backspace ?\C-p ?\C-p ?\C-p ?\C-m ?\C-p ?\C-e tab])
(defun replace-perl-exp ()
"Run the region as a Perl script, kill the region, insert the output of the Perl script."
(interactive)
(let* ((tmp-file (make-temp-file "tmppl"))
(rb (region-beginning))
(re (region-end))
(str (buffer-substring-no-properties rb re)))
(if (string-match "print" str)
(progn
(write-region rb re tmp-file nil 1)
(kill-region rb re))
(progn
(goto-char rb)
(insert "print ")
(goto-char (+ 6 re))
(setq re (point))
(setq str (buffer-substring-no-properties rb re))
(write-region rb re tmp-file nil 1)
(kill-region rb re)
(message str)))
(insert (shell-command-to-string (concat "perl " tmp-file)))
(delete-file tmp-file)))
(defun replace-python-exp ()
"Run the region as a Python script, kill the region, insert the output of the Python script."
(interactive)
(let* ((tmp-file (make-temp-file "tmppl"))
(rb (region-beginning))
(re (region-end))
(str (buffer-substring-no-properties rb re)))
(if (string-match "print" str)
(progn
(write-region rb re tmp-file nil 1)
(kill-region rb re))
(progn
(goto-char rb)
(insert "print(")
(goto-char (+ 6 re))
(insert ")")
(setq re (point))
(write-region rb re tmp-file nil 1)
(kill-region rb re)
(message str)))
(insert (shell-command-to-string (concat "py " tmp-file)))
(delete-file tmp-file)))
(defun jl2-maximize-frame ()
"Maximize frame"
(interactive)
(w32-send-sys-command 61488))
(defun jl2-restore-frame ()
"Maximize frame"
(interactive)
(w32-send-sys-command 61728))
(defun toggle-maximize ()
"One-key keyboard macros: turn recording on."
(interactive)
(substitute-key-definition 'toggle-maximize 'toggle-restore (current-global-map))
(w32-send-sys-command 61488))
(defun toggle-restore ()
"One-key keyboard macros: turn recording off."
(interactive)
(substitute-key-definition 'toggle-restore 'toggle-maximize (current-global-map))
(w32-send-sys-command 61728))
(defun jl2-move-window ()
"Move the window"
(interactive)
(w32-send-sys-command 61456))
(defun jl2-resize-window ()
"Resize the window"
(interactive)
(w32-send-sys-command 61440))
(defun mman ()
"Lookup symbol at point in the FreeBSD man pages."
(interactive)
(browse-url (concat "http://www.freebsd.org/cgi/man.cgi?query=" (buffer-substring-no-properties (region-beginning) (region-end))"&apropos=0&sektion=0&manpath=FreeBSD+9-current&arch=default&format=html")))
(defun ddg-default-search ()
"Search for the region on the internet"
(interactive)
(browse-url (concat "http://duckduckgo.com/?ka=h&kh=1&kj=b2&km=m&ko=1&ks=n&kt=h&ku=-1&kv=1&kw=s&kx=e&ky=b&k1=-1&q=" (replace-regexp-in-string " " "+" (buffer-substring-no-properties (region-beginning) (region-end))))))
(defun ddg-bang-search (bang)
(interactive "s!")
(let* ((search-query (replace-regexp-in-string " " "+" (buffer-substring-no-properties (region-beginning) (region-end))))
(search-string (if (> (length bang) 0)
(concat "!" bang "+" search-query)
search-query)))
(browse-url (concat "http://duckduckgo.com/?ka=h&kh=1&kj=b2&km=m&ko=1&ks=n&kt=h&ku=-1&kv=1&kw=s&kx=e&ky=b&k1=-1&q=" search-string))))
(defun pydoc ()
"Lookup the symbol at point in the Python Docs."
(interactive)
(ddg-bang-search "py3k"))
;; Don't know what I'll ever use this for, but it's amusing.
;; Taken from http://blogs.fluidinfo.com/terry/2011/11/10/emacs-buffer-mode-histogram/
(defun buffer-mode-histogram ()
"Display a histogram of emacs buffer modes."
(interactive)
(let* ((totals '())
(buffers (buffer-list()))
(total-buffers (length buffers))
(ht (make-hash-table :test 'equal)))
(save-excursion
(dolist (buffer buffers)
(set-buffer buffer)
(let
((mode-name (symbol-name major-mode)))
(puthash mode-name (1+ (gethash mode-name ht 0)) ht))))
(maphash (lambda (key value)
(setq totals (cons (list key value) totals)))
ht)
(setq totals (sort totals (lambda (x y) (> (cadr x) (cadr y)))))
(with-output-to-temp-buffer "Buffer mode histogram"
(princ (format "%d buffers open, in %d distinct modes\n\n"
total-buffers (length totals)))
(dolist (item totals)
(let
((key (car item))
(count (cadr item)))
(if (equal (substring key -5) "-mode")
(setq key (substring key 0 -5)))
(princ (format "%2d %20s %s\n" count key
(make-string count ?+))))))))
(defun jl2-revert ()
(interactive)
(revert-buffer nil 't))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment