<commit_before>;;;; SPDX-FileCopyrightText: Atlas Engineer LLC
;;;; SPDX-License-Identifier: BSD-3-Clause

(in-package :nyxt)

;;;; TODO: Remove?

(defun session-list (&optional (buffer (current-buffer)))
  (mapcar #'pathname-name
          (uiop:directory-files (dirname (history-path buffer)))))

(defun session-name-suggestion-filter (minibuffer)
  (fuzzy-match (input-buffer minibuffer) (session-list)))

(define-command store-session-by-name ()
  "Store the session data (i.e. all the opened buffers) in the file named by user input."
  (with-data-access (history (history-path (current-buffer)))
    (sera:and-let* ((name (prompt-minibuffer
                           :input-prompt "The name to store session with"
                           :history (minibuffer-session-restore-history *browser*)
                           :suggestion-function #'session-name-suggestion-filter))
                    (path (make-instance 'history-data-path
                                         :dirname (dirname (history-path (current-buffer)))
                                         :basename name)))
      (setf (get-data path) history)
      (store (data-profile (current-buffer)) path)
      (setf (get-data path) nil))))

(define-command restore-session-by-name ()
  "Restore the session data from the file named by user input."
  (sera:and-let* ((name (prompt-minibuffer
                         :input-prompt "The name of the session to restore"
                         :history (minibuffer-session-restore-history *browser*)
                         :suggestion-function #'session-name-suggestion-filter))
                  (path (make-instance 'history-data-path
                                       :dirname (dirname (history-path (current-buffer)))
                                       :basename name)))
    (restore (data-profile (current-buffer)) path :restore-session-p t)
    ;; TODO: Maybe modify `history-path' of all the buffers instead of polluting history?
    (setf (get-data (history-path (current-buffer))) (get-data path))))

(define-command replace-session-by-name ()
  "Delete all the buffers of the current session and restore the one chosen by user."
  (let ((old-buffers (buffer-list)))
    (setf (get-data (history-path (current-buffer))) nil)
    (restore-session-by-name)
    (dolist (buffer old-buffers)
      (buffer-delete buffer))))<commit_msg>session: Make restore-session-by-name destructive.<commit_after>;;;; SPDX-FileCopyrightText: Atlas Engineer LLC
;;;; SPDX-License-Identifier: BSD-3-Clause

(in-package :nyxt)

;;;; TODO: Remove?

(defun session-list (&optional (buffer (current-buffer)))
  (mapcar #'pathname-name
          (uiop:directory-files (dirname (history-path buffer)))))

(defun session-name-suggestion-filter (minibuffer)
  (fuzzy-match (input-buffer minibuffer) (session-list)))

(define-command store-session-by-name ()
  "Store the session data (i.e. all the opened buffers) in the file named by user input."
  (with-data-access (history (history-path (current-buffer)))
    (sera:and-let* ((name (prompt-minibuffer
                           :input-prompt "The name to store session with"
                           :history (minibuffer-session-restore-history *browser*)
                           :suggestion-function #'session-name-suggestion-filter))
                    (path (make-instance 'history-data-path
                                         :dirname (dirname (history-path (current-buffer)))
                                         :basename name)))
      (setf (get-data path) history)
      (store (data-profile (current-buffer)) path)
      (setf (get-data path) nil))))

(define-command restore-session-by-name ()
  "Delete all the buffers of the current session and restore the one chosen by user."
  (let ((old-buffers (buffer-list)))
    ;; TODO: `store' current session just in case?
    (setf (get-data (history-path (current-buffer))) nil)
    (sera:and-let* ((name (prompt-minibuffer
                           :input-prompt "The name of the session to restore"
                           :history (minibuffer-session-restore-history *browser*)
                           :suggestion-function #'session-name-suggestion-filter))
                    (path (make-instance 'history-data-path
                                         :dirname (dirname (history-path (current-buffer)))
                                         :basename name)))
      (restore (data-profile (current-buffer)) path :restore-session-p t)
      (dolist (buffer old-buffers)
        (buffer-delete buffer))
      ;; TODO: Maybe modify `history-path' of all the buffers instead of polluting history?
      (setf (get-data (history-path (current-buffer))) (get-data path)))))
{'fraction_non_alphanumeric': 0.12565997888067582, 'fraction_numerical': 0.00042238648363252375, 'mean_word_length': 2.2797783933518008, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6469140', 'n_tokens_mistral': 1309, 'n_tokens_neox': 1265, 'n_words': 355}