<commit_before>#|
link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
|#

(defpackage :lem-paredit-mode
  (:use :cl
        :lem
        :lem-vi-mode.word)
  (:export :paredit-mode
           :paredit-forward
           :paredit-backward
           :paredit-insert-paren
           :paredit-backward-delete
           :paredit-close-parenthesis
           :paredit-slurp
           :paredit-barf
           :paredit-splice
           :*paredit-mode-keymap*))
(in-package :lem-paredit-mode)

(define-minor-mode paredit-mode
    (:name "paredit"
     :keymap *paredit-mode-keymap*))

(defun move-to-word-end (q)
  (loop while (not (syntax-space-char-p (character-at q)))
        do (character-offset q 1)))

(defun backward-open-paren-char-p (p)
  (with-point ((q p))
    (skip-whitespace-backward q)
    (syntax-open-paren-char-p (character-at q))))

(defun %skip-closed-parens-and-whitespaces-forward (point skip-last-whitespaces)
  (loop while (syntax-closed-paren-char-p (character-at point))
        do (progn
             (skip-whitespace-forward point)
             (character-offset point 1)))
  (character-offset point 1)
  (when skip-last-whitespaces
    (skip-whitespace-forward point)))

(define-command paredit-forward (&optional (n 1)) ("p")
  (forward-sexp n))

(define-command paredit-backward (&optional (n 1)) ("p")
  (backward-sexp n))

(defun bolp (point)
  (zerop (point-charpos point)))

(defun eolp (point)
  (let ((len (length (line-string point))))
    (or (zerop len)
        (>= (point-charpos point)
            (1- len)))))

(define-command paredit-insert-paren () ()
  (let ((p (current-point)))
    (when (in-string-or-comment-p p)
      (insert-character p #\()
      (return-from paredit-insert-paren))
    (when (eql (character-at p -1) #\\)
      (insert-character p #\()
      (return-from paredit-insert-paren))
    (unless (or (eql (character-at p -1) #\Space)
                (eql (character-at p -1) #\()
                (eql (character-at p -1) #\')
                (eql (character-at p -1) #\`)
                (eql (character-at p -1) #\,)
                (and (eql (character-at p -1) #\@)
                     (eql (character-at p -2) #\,))
                (eql (character-at p -1) #\#)
                (bolp p))
      (insert-character p #\Space))
    (dolist (c '(#\( #\)))
      (insert-character p c))
    (unless (or (eolp p)
                (eql (character-at p) #\Space)
                (eql (character-at p) #\)))
      (insert-character p #\Space)
      (character-offset p -1))
    (character-offset p -1)))

(define-command paredit-insert-doublequote () ()
  (let ((p (current-point)))
    (cond
      ((eql (character-at p -1) #\\)
       (insert-character p #\"))
      ((and (in-string-p p)
            (eql (character-at p) #\"))
       (forward-char))
      ((and (in-string-p p)
            (not (eql (character-at p -1) #\\)))
       (insert-character p #\\)
       (insert-character p #\"))
      (t (unless (or (eql (character-at p -1) #\Space)
                     (eql (character-at p -1) #\()
                     (bolp p)
                     ;; Pathname literal
                     (and (or (eql (character-at p -1) #\p)
                              (eql (character-at p -1) #\P))
                          (eql (character-at p -2) #\#)))
           (insert-character p #\Space))
         (dolist (c '(#\" #\"))
           (insert-character p c))
         (unless (or (eolp p)
                     (eql (character-at p) #\Space)
                     (eql (character-at p) #\)))
           (insert-character p #\Space)
           (character-offset p -1))
         (character-offset p -1)))))

(define-command paredit-backward-delete (&optional (n 1)) ("p")
  (when (< 0 n)
    (with-point ((p (current-point)))
      (cond
        ((eql (character-at p -2) #\\)
         (delete-previous-char 2))
        ((or (and (not (in-string-or-comment-p p))
                  (eql (character-at p -1) #\()
                  (eql (character-at p) #\)))
             (and (in-string-p p)
                  (eql (character-at p -1) #\")
                  (eql (character-at p) #\")))
         (delete-next-char)
         (delete-previous-char))
        ((and (not (in-string-or-comment-p p))
              (or (eql (character-at p -1) #\))
                  (eql (character-at p -1) #\")))
         (backward-char))
        (t
         (delete-previous-char))))<commit_msg>Refactor spacing rules in paredit-insert-paren & paredit-insert-doublequote (And also some of them are added).<commit_after>#|
link : http://www.daregada.sakuraweb.com/paredit_tutorial_ja.html
|#

(defpackage :lem-paredit-mode
  (:use :cl
        :lem
        :lem-vi-mode.word)
  (:export :paredit-mode
           :paredit-forward
           :paredit-backward
           :paredit-insert-paren
           :paredit-backward-delete
           :paredit-close-parenthesis
           :paredit-slurp
           :paredit-barf
           :paredit-splice
           :*paredit-mode-keymap*))
(in-package :lem-paredit-mode)

(define-minor-mode paredit-mode
    (:name "paredit"
     :keymap *paredit-mode-keymap*))

(defun move-to-word-end (q)
  (loop while (not (syntax-space-char-p (character-at q)))
        do (character-offset q 1)))

(defun backward-open-paren-char-p (p)
  (with-point ((q p))
    (skip-whitespace-backward q)
    (syntax-open-paren-char-p (character-at q))))

(defun %skip-closed-parens-and-whitespaces-forward (point skip-last-whitespaces)
  (loop while (syntax-closed-paren-char-p (character-at point))
        do (progn
             (skip-whitespace-forward point)
             (character-offset point 1)))
  (character-offset point 1)
  (when skip-last-whitespaces
    (skip-whitespace-forward point)))

(define-command paredit-forward (&optional (n 1)) ("p")
  (forward-sexp n))

(define-command paredit-backward (&optional (n 1)) ("p")
  (backward-sexp n))

(defun bolp (point)
  (zerop (point-charpos point)))

(defun eolp (point)
  (let ((len (length (line-string point))))
    (or (zerop len)
        (>= (point-charpos point)
            (1- len)))))

(defun integer-char-p (char)
  (< (char-code #\0) (char-code char) (char-code #\9)))

(defun sharp-literal-p (char point)
  (with-point ((p point))
    (character-offset p -1)
    (and (character-at p)
         (char-equal (character-at p) char)
         (eql (character-at p -1) #\#))))

(defun sharp-n-literal-p (char point)
  (with-point ((p point))
    (character-offset p -1)
    (when (char-equal char (character-at p))
      (character-offset p -1)
      (skip-chars-backward p #'integer-char-p)
      (and (integer-char-p (character-at p))
           (eql (character-at p -1) #\#)))))

(defparameter *non-space-following-chars*
  '(#\Space #\( #\' #\` #\,))

(defparameter *non-space-preceding-chars*
  '(#\Space #\)))

(define-command paredit-insert-paren () ()
  (let ((p (current-point)))
    (when (in-string-or-comment-p p)
      (insert-character p #\()
      (return-from paredit-insert-paren))
    (when (eql (character-at p -1) #\\)
      (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))
      (insert-character p #\Space))
    (dolist (c '(#\( #\)))
      (insert-character p c))
    (unless (or (eolp p)
                (eql (character-at p) #\Space)
                (eql (character-at p) #\)))
      (insert-character p #\Space)
      (character-offset p -1))
    (character-offset p -1)))

(define-command paredit-insert-doublequote () ()
  (let ((p (current-point)))
    (cond
      ((eql (character-at p -1) #\\)
       (insert-character p #\"))
      ((and (in-string-p p)
            (eql (character-at p) #\"))
       (forward-char))
      ((and (in-string-p p)
            (not (eql (character-at p -1) #\\)))
       (insert-character p #\\)
       (insert-character p #\"))
      (t (unless (or (bolp p)
                     (find (character-at p -1)
                           *non-space-following-chars*)
                     (sharp-literal-p #\P p))
           (insert-character p #\Space))
         (dolist (c '(#\" #\"))
           (insert-character p c))
         (unless (or (eolp p)
                     (find (character-at p)
                           *non-space-preceding-chars*))
           (insert-character p #\Space)
           (character-offset p -1))
         (character-offset p -1)))))

(define-command paredit-backward-delete (&optional (n 1)) ("p")
  (when (< 0 n)
    (with-point ((p (current-point)))
      (cond
        ((eql (character-at p -2) #\\)
         (delete-previous-char 2))
        ((or (and (not (in-string-or-comment-p p))
                  (eql (character-at p -1) #\()
                  (eql (character-at p) #\)))
             (and (in-string-p p)
                  (eql (character-at p -1) #\")
                  (eql (character-at p) #\")))
         (delete-next-char)
         (delete-previous-char))
        ((and (not (in-string-or-comment-p p))
              (or (eql (character-at p -1) #\))
                  (eql (character-at p -1) #\")))
         (backward-char))
        (t
         (delete-previous-char))))
{'fraction_non_alphanumeric': 0.17598024081506639, 'fraction_numerical': 0.006895132242461665, 'mean_word_length': 1.7537546047038821, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4420942', 'n_tokens_mistral': 3479, 'n_tokens_neox': 3401, 'n_words': 755}