-
-
Notifications
You must be signed in to change notification settings - Fork 225
[WIP] smart-parens-mode #1916
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
base: main
Are you sure you want to change the base?
[WIP] smart-parens-mode #1916
Conversation
|
(following Discord:) I pull the branch, start Lem, start the mode with |
|
Hm. |
|
I tried it, and make was successful. |
Screencast.from.2025-08-14.01-39-12.webm |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am happy to contribute to lem.
I have made a few comments.
| (defun editor-insert-pair (open close) | ||
| (let ((p (current-point))) | ||
| (cond ((in-string-or-comment-p p) | ||
| (insert-character p open)) | ||
| ((syntax-escape-point-p p 0) | ||
| (insert-character p open)) | ||
| (t | ||
| (unless (non-space-following-context-p p) | ||
| (insert-character p #\Space)) | ||
| (insert-character p open) | ||
| (insert-character p close) | ||
| (unless (or (eolp p) (find (character-at p) *non-space-preceding-chars*)) | ||
| (insert-character p #\Space) | ||
| (character-offset p -1)) | ||
| (character-offset p -1))))) | ||
|
|
||
| ;;; smart parens specific code | ||
|
|
||
| (define-command smart-parens-insert-paren () () | ||
| (editor-insert-pair #\( #\))) | ||
|
|
||
| (define-command smart-parens-insert-bracket () () | ||
| (editor-insert-pair #\[ #\])) | ||
|
|
||
| (define-command smart-parens-insert-brace () () | ||
| (editor-insert-pair #\{ #\})) | ||
|
|
||
| (define-command smart-parens-insert-double-quote () () | ||
| (editor-insert-pair #\" #\")) | ||
|
|
||
| (define-command smart-parens-insert-single-quote () () | ||
| (editor-insert-pair #\' #\')) | ||
|
|
||
| (define-key *smart-parens-keymap* "\"" 'smart-parens-insert-double-quote) | ||
| (define-key *smart-parens-keymap* "'" 'smart-parens-insert-single-quote) | ||
| (define-key *smart-parens-keymap* "(" 'smart-parens-insert-paren) | ||
| (define-key *smart-parens-keymap* "[" 'smart-parens-insert-bracket) | ||
| (define-key *smart-parens-keymap* "{" 'smart-parens-insert-brace) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One problem with this implementation is that it implicitly relies on C-like syntax.
It would be better to refer to the syntax-table for each mode.
for example
CL-USER> (lem:syntax-open-paren-char-p #\()
(#\( . #\))
CL-USER> (lem:syntax-open-paren-char-p #\[)
(#\[ . #\])
CL-USER> (lem:syntax-open-paren-char-p #\{)
(#\{ . #\})
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, do you mean that instead of using define-key I should use some function which gets the currently inserted character like this:
(defun close-paren (given-char)
(when (lem:syntax-open-paren-char-p given-char)
(apply #'editor-insert-pair (lem:syntax-open-paren-char-p given-char))))That seems like it would only work for the parens and not the " and ' quote characters.
I don't know lem well enough. What function or macro would I pass this close-paren function to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lem/extensions/c-mode/c-mode.lisp
Line 13 in 4072439
| :paren-pairs '((#\( . #\)) |
syntax-table is defined here in C, for example.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for example, like this
(defmethod execute ((mode smart-parens-mode) (command self-insert) argument)
(let ((c (get-self-insert-char)))
(alexandria:when-let (pair (syntax-open-paren-char-p c))
(editor-insert-pair (car pair) (cdr pair))
(return-from execute))
(when (syntax-string-quote-char-p c)
(editor-insert-pair c c)
(return-from execute))
(call-next-method)))
not currently working, this is just a draft