Skip to content

Conversation

@daninus14
Copy link

not currently working, this is just a draft

@vindarel
Copy link
Collaborator

vindarel commented Aug 13, 2025

(following Discord:) I pull the branch, start Lem, start the mode with M-x smart-parens-mode and typing a ( inserts the pair. It works! (except it inserts too many closing parens)

@daninus14
Copy link
Author

Hm. smart-parens-mode is not showing up for me. What is your workflow? I was doing a make build after each change in the code. Are you running it from slime? How do I run it locally without building?

@cxxxr
Copy link
Member

cxxxr commented Aug 13, 2025

I tried it, and make was successful.
The feature in this mode are also working.

@cxxxr
Copy link
Member

cxxxr commented Aug 13, 2025

Screencast.from.2025-08-14.01-39-12.webm

Copy link
Member

@cxxxr cxxxr left a 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.

Comment on lines 62 to 99
(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)
Copy link
Member

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 #\{)
(#\{ . #\})

Copy link
Author

@daninus14 daninus14 Aug 13, 2025

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:paren-pairs '((#\( . #\))

syntax-table is defined here in C, for example.

Copy link
Member

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)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants