Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
289 lines (242 sloc) 10.2 KB

Starter Kit Lisp

This is part of the Emacs Starter Kit.

Starter Kit Lisp

Support for editing list dialects including Emacs Lisp, Scheme, Common Lisp, and Clojure.

Define keys

(define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
;; (define-key lisp-mode-shared-map (kbd "C-c l") "lambda")
(define-key lisp-mode-shared-map (kbd "RET") 'reindent-then-newline-and-indent)
(define-key lisp-mode-shared-map (kbd "C-\\") 'lisp-complete-symbol)
(define-key lisp-mode-shared-map (kbd "C-c v") 'eval-buffer)

Auto-pair mode

(require 'autopair)
(autopair-global-mode) ;; to enable in all buffers
;; (autopair-global-mode 0)
;; http://code.google.com/p/autopair/issues/detail?id=32&q=slime
(add-hook 'slime-repl-mode-hook #'(lambda () (autopair-global-mode 0)))
(add-hook 'slime-repl-mode-hook #'(lambda () (setq autopair-dont-activate t)))
;; (add-hook 'comint-mode-hook #'(lambda () (setq autopair-dont-activate 0)))
(add-hook 'sldb-mode-hook #'(lambda () (setq autopair-dont-activate t)))
(add-hook 'sldb-mode-hook (autopair-mode 0))
(add-hook 'slime-mode-hook #'(lambda () (setq autopair-dont-activate t)))
(add-hook 'slime-mode-hook (autopair-mode 0))

;; (add-hook 'sldb-mode-hook #'(lambda () (setq autopair-dont-activate t)))

;; These things from the answer: http://stackoverflow.com/questions/3987489/is-it-possible-to-auto-complete-parentheses-or-quotation-marks-in-emacs
(setq skeleton-pair t)
(defvar skeletons-alist
  '((?\( . ?\))
    (?\" . ?\")
    (?[  . ?])
    (?{  . ?})
    (?$  . ?$)))

(defadvice delete-backward-char (before delete-empty-pair activate)
  (if (eq (cdr (assq (char-before) skeletons-alist)) (char-after))
      (and (char-after) (delete-char 1))))

(defadvice backward-kill-word (around delete-pair activate)
  (if (eq (char-syntax (char-before)) ?\()
      (progn
        (backward-char 1)
        (save-excursion
          (forward-sexp 1)
          (delete-char -1))
        (forward-char 1)
        (append-next-kill)
        (kill-backward-chars 1))
    ad-do-it))
      

Paredit

Paredit might seem weird at first, but it really makes writing lisp a much more comfortable experience. This is especially useful in combination with the sexp movement functions (C-M-f forward, C-M-b back, C-M-u up, C-M-d down)

(require 'paredit)

(defun turn-on-paredit ()
  (paredit-mode +1))

(add-hook 'slime-repl-mode-hook (lambda () (paredit-mode +1)))
; (add-hook 'org-mode-hook (lambda () (paredit-mode +1)))
(add-hook 'emacs-lisp-mode-hook       (lambda () (paredit-mode +1)))
(add-hook 'lisp-mode-hook             (lambda () (paredit-mode +1)))
(add-hook 'lisp-interaction-mode-hook (lambda () (paredit-mode +1)))

;; Stop SLIME's REPL from grabbing DEL,
;; which is annoying when backspacing over a '('
;  (defun override-slime-repl-bindings-with-paredit ()
;    (define-key slime-repl-mode-map
;      (read-kbd-macro paredit-backward-delete-key) nil))
;  (add-hook 'slime-repl-mode-hook 'override-slime-repl-bindings-with-paredit)
;; (eval-after-load 'paredit
;;      ;; Not sure why paredit behaves this way with comments; it's annoying
;;   '(define-key paredit-mode-map (kbd ";")   'self-insert-command))

Non-obtrusive parenthesis faces

(defface esk-paren-face
   '((((class color) (background dark))
      (:foreground "grey50"))
     (((class color) (background light))
      (:foreground "grey55")))
   "Face used to dim parentheses."
   :group 'starter-kit-faces)

Emacs Lisp

(add-hook 'emacs-lisp-mode-hook 'turn-on-eldoc-mode)
(add-hook 'emacs-lisp-mode-hook 'run-coding-hook)
(add-hook 'emacs-lisp-mode-hook 'esk-remove-elc-on-save)
;; (add-hook 'emacs-lisp-mode-hook 'idle-highlight)
(add-hook 'emacs-lisp-mode-hook 'turn-on-paredit)

(defun esk-remove-elc-on-save ()
  "If you're saving an elisp file, likely the .elc is no longer valid."
  (make-local-variable 'after-save-hook)
  (add-hook 'after-save-hook
            (lambda ()
              (if (file-exists-p (concat buffer-file-name "c"))
                  (delete-file (concat buffer-file-name "c"))))))

(font-lock-add-keywords 'emacs-lisp-mode
			'(("(\\|)" . 'esk-paren-face)))

SLIME

;; http://www.cliki.net/SLIME%20Tips
;; (add-hook 'slime-mode-hook
;;           (lambda ()
;;             (unless (slime-connected-p)
;;               (save-excursion (slime)))))

;; (defun cliki:start-slime ()
;;   (unless (slime-connected-p)
;;     (save-excursion (slime))))

;; (add-hook 'slime-mode-hook 'cliki:start-slime)

;;  (slime-setup '(slime-fancy slime-asdf))
;; http://functionalrants.wordpress.com/2008/09/06/how-to-set-up-emacs-slime-sbcl-under-gnulinux/
;; Set up the Common Lisp environment
;; (add-to-list 'load-path "/usr/share/common-lisp/source/slime/")
;; /Users/FingerMan/quicklisp/dists/quicklisp/software/slime-20110219-cvs
(add-to-list 'load-path "/Users/FingerMan/quicklisp/dists/quicklisp/software/slime-20110219-cvs")
(setq inferior-lisp-program "/usr/local/bin/sbcl")
(require 'slime-autoloads)
(slime-setup)

;;  (setq inferior-lisp-program "sbcl")
(load (expand-file-name "~/quicklisp/slime-helper.el"))

Clojure

(add-hook 'clojure-mode-hook 'run-coding-hook)
;; (add-hook 'clojure-mode-hook 'idle-highlight)

(font-lock-add-keywords 'clojure-mode
                        '(("(\\|)" . 'esk-paren-face)))

(defface esk-clojure-trace-face
   '((((class color) (background dark))
      (:foreground "grey50"))
     (((class color) (background light))
      (:foreground "grey55")))
   "Face used to dim parentheses."
   :group 'starter-kit-faces)

(setq esk-clojure-trace-face 'esk-clojure-trace-face)

;; This will make relevant lines stand out more in stack traces
(defun sldb-font-lock ()
  (font-lock-add-keywords nil
                          '(("[0-9]+: \\(clojure\.\\(core\\|lang\\).*\\)"
                             1 esk-clojure-trace-face)
                            ("[0-9]+: \\(java.*\\)"
                             1 esk-clojure-trace-face)
                            ("[0-9]+: \\(swank.*\\)"
                             1 esk-clojure-trace-face)
                            ("\\[\\([A-Z]+\\)\\]"
                             1 font-lock-function-name-face))))

(add-hook 'sldb-mode-hook 'sldb-font-lock)

(defun slime-jump-to-trace (&optional on)
  "Jump to the file/line that the current stack trace line references.
Only works with files in your project root's src/, not in dependencies."
  (interactive)
  (save-excursion
    (beginning-of-line)
    (search-forward-regexp "[0-9]: \\([^$(]+\\).*?\\([0-9]*\\))")
    (let ((line (string-to-number (match-string 2)))
          (ns-path (split-string (match-string 1) "\\."))
          (project-root (locate-dominating-file default-directory "src/")))
      (find-file (format "%s/src/%s.clj" project-root
                         (mapconcat 'identity ns-path "/")))
      (goto-line line))))

(eval-after-load 'slime
  '(progn
     (defalias 'sldb-toggle-details 'slime-jump-to-trace)
     (defun sldb-prune-initial-frames (frames)
       "Show all stack trace lines by default."
       frames)))

(eval-after-load 'find-file-in-project
  '(add-to-list 'ffip-patterns "*.clj"))

;; You might like this, but it's a bit disorienting at first:
(add-hook 'clojure-mode-hook 'turn-on-paredit)

(defun clojure-project (path)
  "Setup classpaths for a clojure project and starts a new SLIME session.

Kills existing SLIME session, if any."
  (interactive (list
                (ido-read-directory-name
                 "Project root: "
                 (locate-dominating-file default-directory "pom.xml"))))
  (when (get-buffer "*inferior-lisp*")
    (kill-buffer "*inferior-lisp*"))
  (add-to-list 'swank-clojure-extra-vm-args
               (format "-Dclojure.compile.path=%s"
                       (expand-file-name "target/classes/" path)))
  (setq swank-clojure-binary nil
        swank-clojure-jar-path (expand-file-name "target/dependency/" path)
        swank-clojure-extra-classpaths
        (append (mapcar (lambda (d) (expand-file-name d path))
                        '("src/" "target/classes/" "test/"))
                (let ((lib (expand-file-name "lib" path)))
                  (if (file-exists-p lib)
                      (directory-files lib t ".jar$"))))
        slime-lisp-implementations
        (cons `(clojure ,(swank-clojure-cmd) :init swank-clojure-init)
              (remove-if #'(lambda (x) (eq (car x) 'clojure))
                         slime-lisp-implementations)))
  (save-window-excursion
    (slime)))

Prettier function names in clojure – Thanks to Paul Hobbs for the idea to extend this feature beyond simply the fn function.

;; symbols for some overlong function names
(eval-after-load 'clojure-mode
  '(font-lock-add-keywords
    'clojure-mode
    (mapcar
     (lambda (pair)
       `(,(car pair)
         (0 (progn (compose-region
                    (match-beginning 0) (match-end 0)
                    ,(cadr pair))
                   nil))))
     '(("\\<fn\\>" )
       ("\\<comp\\>" ?∘)
       ("\\<partial\\>" )
       ("\\<complement\\>" )))))

Scheme

;; http://deaddeadgood.com/2010/11/18/mit-scheme-in-emacs-on-os-x/
;; MIT Scheme
(setenv "MITSCHEME_LIBRARY_PATH"  "/Applications/mit-scheme.app/Contents/Resources")

(require 'quack)
(add-hook 'scheme-mode-hook 'run-coding-hook)
;; (add-hook 'scheme-mode-hook 'idle-highlight)
(font-lock-add-keywords 'scheme-mode
            '(("(\\|)" . 'esk-paren-face)))

Common Lisp

(add-hook 'lisp-mode-hook 'run-coding-hook)
;; (add-hook 'lisp-mode-hook 'idle-highlight)
(add-hook 'lisp-mode-hook 'turn-on-paredit)
(font-lock-add-keywords 'lisp-mode
			'(("(\\|)" . 'esk-paren-face)))