<commit_before>(in-package :lem)

(defstruct ncurses-view
  scrwin
  modeline-scrwin
  x
  y
  width
  height)

(defun attribute-to-bits (attribute-or-name)
  (let ((attribute (ensure-attribute attribute-or-name nil)))
    (if (null attribute)
        0
        (or (lem::attribute-%internal-value attribute)
            (let ((bits (logior (lem.term:get-color-pair (lem::attribute-foreground attribute)
                                                         (lem::attribute-background attribute))
                                (if (lem::attribute-reverse-p attribute)
                                    charms/ll:a_reverse
                                    0)
                                (if (lem::attribute-bold-p attribute)
                                    charms/ll:a_bold
                                    0)
                                (if (lem::attribute-underline-p attribute)
                                    charms/ll:a_underline
                                    0))))
              (setf (lem::attribute-%internal-value attribute) bits)
              bits)))))

(defun input-loop (editor-thread)
  (handler-case
      (loop
        :with abort-key := (char-code (keyname->keychar "C-]"))
        :do (handler-case
                (progn
                  (unless (bt:thread-alive-p editor-thread) (return))
                  (let ((code (charms/ll:getch)))
                    (cond
                      ((= code -1))
                      ((= code 410)
                       (loop :while (< 0 (lem::event-queue-length))
                             :do (sleep 0.01))
                       (send-event :resize))
                      ((= code abort-key)
                       (send-abort-event editor-thread nil))
                      (t
                       (send-event
                        (let ((nbytes (utf8-bytes code)))
                          (if (= nbytes 1)
                              (code-char code)
                              (let ((vec (make-array nbytes
                                                     :element-type '(unsigned-byte 8))))
                                (setf (aref vec 0) code)
                                (loop :for i :from 1 :below nbytes
                                      :do (setf (aref vec i) (charms/ll:getch)))
                                (schar (babel:octets-to-string vec) 0)))))))))
              #+sbcl
              (sb-sys:interactive-interrupt (c)
                (declare (ignore c))
                (send-abort-event editor-thread t))))
    (exit-editor (c) (return-from input-loop c))))

(defmethod interface-invoke ((implementation (eql :ncurses)) function)
  (let ((result nil))
    (unwind-protect
         (progn
           (lem.term:term-init)
           (send-event (lambda ()
                         (add-hook *before-init-hook*
                                   (lambda () (load-theme "emacs-dark")))))
           (let ((editor-thread (funcall function)))
             (setf result (input-loop editor-thread))))
      (lem.term:term-finalize))
    (when (and (typep result 'exit-editor)
               (exit-editor-value result))
      (format t "~&~A~%" (exit-editor-value result)))))

(defmethod interface-display-background-mode ((implementation (eql :ncurses)))
  (lem.term:background-mode))

(defmethod interface-update-foreground ((implementation (eql :ncurses)) color-name)
  (lem.term:term-set-foreground color-name))

(defmethod interface-update-background ((implementation (eql :ncurses)) color-name)
  (lem.term:term-set-background color-name))

(defmethod interface-display-width ((implementation (eql :ncurses)))
  (max 5 charms/ll:*cols*))
<commit_msg>move load-theme call<commit_after>(in-package :lem)

(defstruct ncurses-view
  scrwin
  modeline-scrwin
  x
  y
  width
  height)

(defun attribute-to-bits (attribute-or-name)
  (let ((attribute (ensure-attribute attribute-or-name nil)))
    (if (null attribute)
        0
        (or (lem::attribute-%internal-value attribute)
            (let ((bits (logior (lem.term:get-color-pair (lem::attribute-foreground attribute)
                                                         (lem::attribute-background attribute))
                                (if (lem::attribute-reverse-p attribute)
                                    charms/ll:a_reverse
                                    0)
                                (if (lem::attribute-bold-p attribute)
                                    charms/ll:a_bold
                                    0)
                                (if (lem::attribute-underline-p attribute)
                                    charms/ll:a_underline
                                    0))))
              (setf (lem::attribute-%internal-value attribute) bits)
              bits)))))

(defun input-loop (editor-thread)
  (handler-case
      (loop
        :with abort-key := (char-code (keyname->keychar "C-]"))
        :do (handler-case
                (progn
                  (unless (bt:thread-alive-p editor-thread) (return))
                  (let ((code (charms/ll:getch)))
                    (cond
                      ((= code -1))
                      ((= code 410)
                       (loop :while (< 0 (lem::event-queue-length))
                             :do (sleep 0.01))
                       (send-event :resize))
                      ((= code abort-key)
                       (send-abort-event editor-thread nil))
                      (t
                       (send-event
                        (let ((nbytes (utf8-bytes code)))
                          (if (= nbytes 1)
                              (code-char code)
                              (let ((vec (make-array nbytes
                                                     :element-type '(unsigned-byte 8))))
                                (setf (aref vec 0) code)
                                (loop :for i :from 1 :below nbytes
                                      :do (setf (aref vec i) (charms/ll:getch)))
                                (schar (babel:octets-to-string vec) 0)))))))))
              #+sbcl
              (sb-sys:interactive-interrupt (c)
                (declare (ignore c))
                (send-abort-event editor-thread t))))
    (exit-editor (c) (return-from input-loop c))))

(add-hook *before-init-hook*
          (lambda ()
            (load-theme "emacs-dark")))

(defmethod interface-invoke ((implementation (eql :ncurses)) function)
  (let ((result nil))
    (unwind-protect
         (progn
           (lem.term:term-init)
           (let ((editor-thread (funcall function)))
             (setf result (input-loop editor-thread))))
      (lem.term:term-finalize))
    (when (and (typep result 'exit-editor)
               (exit-editor-value result))
      (format t "~&~A~%" (exit-editor-value result)))))

(defmethod interface-display-background-mode ((implementation (eql :ncurses)))
  (lem.term:background-mode))

(defmethod interface-update-foreground ((implementation (eql :ncurses)) color-name)
  (lem.term:term-set-foreground color-name))

(defmethod interface-update-background ((implementation (eql :ncurses)) color-name)
  (lem.term:term-set-background color-name))

(defmethod interface-display-width ((implementation (eql :ncurses)))
  (max 5 charms/ll:*cols*))

{'fraction_non_alphanumeric': 0.12421512421512422, 'fraction_numerical': 0.005187005187005187, 'mean_word_length': 1.3113564668769717, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 33, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '628356', 'n_tokens_mistral': 2015, 'n_tokens_neox': 1910, 'n_words': 432}