Skip to content

Instantly share code, notes, and snippets.

@goose121
Last active June 4, 2020 04:05
Show Gist options
  • Select an option

  • Save goose121/acf899f3cea9088165b4585a83e8e31c to your computer and use it in GitHub Desktop.

Select an option

Save goose121/acf899f3cea9088165b4585a83e8e31c to your computer and use it in GitHub Desktop.
Make paredit and sexp-navigation functions treat dispatching reader macros correctly
(defun parse-dispatch-macro-backwards ()
(let ((old-point (point)))
(if (save-match-data
(and (not (member (char-before) '(?\( ?#)))
(re-search-backward "#[0-9]*" nil t)
(= (match-end 0) (- old-point 1))))
(point)
(goto-char old-point)
nil)))
(defun space-for-dispatch-macro (endp delim)
(save-excursion
(or endp
(not (parse-dispatch-macro-backwards)))))
(defun parse-dispatch-macro ()
(forward-comment (buffer-size))
(save-match-data
(when (looking-at "#[0-9]*[^(#0-9]")
(goto-char (match-end 0)))))
(defun cl-dispatch-macro-forward-sexp (n)
(cond
((= n 0))
((> n 0)
(dotimes (i n)
(parse-dispatch-macro)
(let ((forward-sexp-function nil))
(forward-sexp))))
((< n 0)
(dotimes (i (- n))
(let ((forward-sexp-function nil))
(forward-sexp -1))
(let ((old-point (point)))
(forward-comment (- (buffer-size)))
(unless (parse-dispatch-macro-backwards)
(goto-char old-point)))))))
(add-hook 'lisp-mode-hook
(lambda ()
(setq-local forward-sexp-function 'cl-dispatch-macro-forward-sexp)
(make-local-variable 'paredit-space-for-delimiter-predicates)
(push 'space-for-dispatch-macro paredit-space-for-delimiter-predicates)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment