Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small changes to paredit-mode #949

Merged
merged 7 commits into from
Aug 14, 2023
102 changes: 63 additions & 39 deletions extensions/paredit-mode/paredit-mode.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
:paredit-splice-backward
:paredit-splice-forward
:paredit-raise
:paredit-wrap-round
:paredit-meta-doublequote
:*paredit-mode-keymap*))
(in-package :lem-paredit-mode)

Expand Down Expand Up @@ -55,10 +57,18 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
do (character-offset point 1)))

(define-command paredit-forward (&optional (n 1)) ("p")
(forward-sexp n))
(handler-case
(forward-sexp n)
(error ()
(unless (end-buffer-p (current-point))
(lem:forward-up-list (current-point))))))

(define-command paredit-backward (&optional (n 1)) ("p")
(backward-sexp n))
(handler-case
(backward-sexp n)
(error ()
(unless (start-buffer-p (current-point))
(lem:backward-up-list (current-point))))))

(defun bolp (point)
(zerop (point-charpos point)))
Expand Down Expand Up @@ -94,6 +104,22 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
(defparameter *non-space-preceding-chars*
'(#\Space #\)))

(defun non-space-following-context-p (&optional (p (current-point)))
(or (bolp p)
(find (character-at p -1)
*non-space-following-chars*)
(eql (character-at p -1) #\#)
(and (eql (character-at p -1) #\@)
(eql (character-at p -2) #\,))
(sharp-literal-p #\' p)
(sharp-literal-p #\. p)
(sharp-literal-p #\S p)
(sharp-literal-p #\C p)
(sharp-literal-p #\+ p)
(sharp-literal-p #\- p)
(sharp-n-literal-p #\A p)
(sharp-n-literal-p #\= p)))

(define-command paredit-insert-paren () ()
(let ((p (current-point)))
(when (in-string-or-comment-p p)
Expand All @@ -102,20 +128,7 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
(when (lem-base::syntax-escape-point-p p 0)
(insert-character p #\()
(return-from paredit-insert-paren))
(unless (or (bolp p)
(find (character-at p -1)
*non-space-following-chars*)
(eql (character-at p -1) #\#)
(and (eql (character-at p -1) #\@)
(eql (character-at p -2) #\,))
(sharp-literal-p #\' p)
(sharp-literal-p #\. p)
(sharp-literal-p #\S p)
(sharp-literal-p #\C p)
(sharp-literal-p #\+ p)
(sharp-literal-p #\- p)
(sharp-n-literal-p #\A p)
(sharp-n-literal-p #\= p))
(unless (non-space-following-context-p p)
(insert-character p #\Space))
(dolist (c '(#\( #\)))
(insert-character p c))
Expand All @@ -134,7 +147,7 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
((in-string-p p)
(if (eql (character-at p) #\")
(forward-char)
(insert-string p "\"" #\\)))
(insert-string p "\\\"" #\\)))
(t (unless (or (bolp p)
(find (character-at p -1)
*non-space-following-chars*)
Expand Down Expand Up @@ -443,37 +456,47 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
(with-point ((end start)
(word-start (current-point)))
(scan-lists end 1 0)
(character-offset word-start 1)
(form-offset word-start -1)
(unless (syntax-open-paren-char-p (character-at word-start))
(character-offset word-start 1)
(form-offset word-start -1))
(with-point ((word-end word-start))
(form-offset word-end 1)
(delete-between-points word-end end)
(delete-between-points start word-start))))))))

(define-command paredit-wrap () ()

(defun %paredit-wrap (begin-char end-char)
(if (buffer-mark-p (current-buffer))
(with-point ((begin (region-beginning))
(end (region-end)))
(unless (or (in-string-or-comment-p begin)
(unless (or (in-string-or-comment-p begin)
(in-string-or-comment-p end))
(cond ((point> begin end)
(insert-character begin #\))
(insert-character end #\())
((point< begin end)
(insert-character end #\))
(insert-character begin #\()
(move-point (current-point) (character-offset begin 1)))
(t
(insert-character begin #\()
(insert-character (current-point) #\))
(insert-character (current-point) #\Space)
(move-point (current-point) (character-offset begin 1))))))
(cond ((point> begin end)
(insert-character begin end-char)
(insert-character end begin-char))
((point< begin end)
(insert-character end end-char)
(insert-character begin begin-char)
(move-point (current-point) (character-offset begin 1)))
(t
(insert-character begin begin-char)
(insert-character (current-point) end-char)
(insert-character (current-point) #\Space)
(move-point (current-point) (character-offset begin 1))))))
(with-point ((origin (current-point)))
(unless (in-string-or-comment-p origin)
(forward-sexp)
(insert-character origin #\()
(insert-character (current-point) #\))
(move-point (current-point) (character-offset origin 1))))))
(forward-sexp)
(insert-character origin begin-char)
(unless (non-space-following-context-p origin)
(insert-character origin #\Space))
(insert-character (current-point) end-char)
(move-point (current-point) (character-offset origin 1))))))

(define-command paredit-wrap-round () ()
(%paredit-wrap #\( #\)))

(define-command paredit-meta-doublequote () ()
(%paredit-wrap #\" #\"))

(loop for (k . f) in '((forward-sexp . paredit-forward)
(backward-sexp . paredit-backward)
Expand All @@ -486,7 +509,8 @@ link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
("C-Right" . paredit-slurp)
("C-Left" . paredit-barf)
("M-s" . paredit-splice)
;("M-Up" . paredit-splice-backward)
("M-Up" . paredit-splice-backward)
("M-r" . paredit-raise)
("M-(" . paredit-wrap))
("M-(" . paredit-wrap-round)
("M-\"" . paredit-meta-doublequote))
do (define-key *paredit-mode-keymap* k f))