<commit_before>;;; defuns-buffers.el

;;;###autoload (autoload 'doom:narrow "defuns-buffers" nil t)
(evil-define-operator doom:narrow (&optional beg end bang)
  "Restrict editing in this buffer to the current region, indirectly. With BANG,
clone the buffer and hard-narrow the selection. Otherwise use fancy-narrow. If
mark isn't active, then widen the buffer (if narrowed).

Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
  (interactive "<r><!>")
  (if (region-active-p)
      (progn
        (deactivate-mark)
        (let ((buf (clone-indirect-buffer nil nil)))
          (with-current-buffer buf
            (narrow-to-region beg end))
          (switch-to-buffer buf)))
    (widen)))

;;;###autoload
(defun doom/set-read-only-region (begin end)
  "See http://stackoverflow.com/questions/7410125"
  (let ((modified (buffer-modified-p)))
    (add-text-properties begin end '(read-only t))
    (set-buffer-modified-p modified)))

;;;###autoload
(defun doom/set-region-writeable (begin end)
  "See http://stackoverflow.com/questions/7410125"
  (let ((modified (buffer-modified-p))
        (inhibit-read-only t))
    (remove-text-properties begin end '(read-only t))
    (set-buffer-modified-p modified)))


;; Buffer Life and Death ;;;;;;;;;;;;;;;

(unless window-system
  (defalias 'wg-workgroup-associated-buffers 'ignore)
  (defalias 'wg-current-workgroup 'ignore)
  (defalias 'wg-save-session 'ignore))

;;;###autoload
(defun doom/get-buffers (&optional all-p)
  "Get all buffers in the current workgroup.

    If PROJECT-P is non-nil, get all buffers in current workgroup
    If both are non-nil, get all project buffers across all workgroups"
  (let ((buffers (if (wg-current-workgroup t)
                     (doom/get-buffers-in-workgroup)
                   (buffer-list)))
        project-root)
    (append (aif (and (not all-p) (doom/project-root t))
                (funcall (if (eq all-p 'not) '-remove '-filter)
                         (lambda (b) (projectile-project-buffer-p b it))
                         buffers)
              buffers)
            (list doom-buffer))))

;;;###autoload
(defun doom/get-buffers-in-workgroup ()
  (let ((assoc-bufs (wg-workgroup-associated-buffers nil)))
    (--filter (memq it assoc-bufs) (buffer-list))))

;;;###autoload
(defun doom/get-buffer-names (&optional buffer-list)
  (mapcar #'buffer-name (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-visible-windows (&optional window-list)
  "Get a list of the visible windows in the current frame (that aren't popups)."
  (-remove #'doom/popup-p (or window-list (window-list))))

;;;###autoload
(defun doom/get-visible-buffers (&optional buffer-list)
  "Get a list of unburied buffers in the current project and workgroup."
  (-filter #'get-buffer-window (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-buried-buffers (&optional buffer-list)
  "Get a list of buried buffers in the current project and workgroup."
  (-remove 'get-buffer-window (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-matching-buffers (pattern &optional buffer-list)
  "Get a list of buffers that match the pattern"
  (--filter (string-match-p pattern (buffer-name it))
            (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-buffers-in-modes (modes &optional buffer-list)
  "Get a list of buffers whose major-mode is one of MODES"
  (--filter (memq (buffer-local-value 'major-mode it) modes)
            (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-real-buffers (&optional buffer-list)
  (-filter #'doom/real-buffer-p (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/kill-real-buffer (&optional arg)
  "Kill buffer (but only bury scratch buffer), then switch to a real buffer. Only buries
the buffer if it is being displayed in another window."
  (interactive (list t))
  (cond ((eq (current-buffer) doom-buffer)
         (doom-mode-init t))
        (t
         (let* ((old-project (doom/project-root))
                (buffer (current-buffer))
                (only-buffer-window-p (= (length (get-buffer-window-list buffer nil t)) 1)))
           (when (and only-buffer-window-p buffer-file-name (buffer-modified-p))
             (if (yes-or-no-p "Buffer is unsaved, save it?")
                 (save-buffer)
               (set-buffer-modified-p nil)))
           (when arg
             (doom/previous-real-buffer)
             (when (eq buffer (current-buffer))
               (switch-to-buffer doom-buffer t t)
               (doom|update-scratch-buffer old-project))
             (when only-buffer-window-p
               (kill-buffer buffer))))))
  t)

;;;###autoload
(defun doom/kill-unreal-buffers ()
  "Kill all buried, unreal buffers in current frame. See `doom-unreal-buffers'"
  (interactive)
  (let ((kill-list (-remove 'doom/real-buffer-p
                            (doom/get-buried-buffers (buffer-list)))))
    (mapc 'kill-buffer kill-list)
    (doom/kill-process-buffers)
    (message "Cleaned up %s buffers" (length kill-list))))

;;;###autoload
(defun doom/kill-process-buffers ()
  "Kill all buffers that represent running processes and aren't visible."<commit_msg>Conditionally use doom|update-scratch-buffer<commit_after>;;; defuns-buffers.el

;;;###autoload (autoload 'doom:narrow "defuns-buffers" nil t)
(evil-define-operator doom:narrow (&optional beg end bang)
  "Restrict editing in this buffer to the current region, indirectly. With BANG,
clone the buffer and hard-narrow the selection. Otherwise use fancy-narrow. If
mark isn't active, then widen the buffer (if narrowed).

Inspired from http://demonastery.org/2013/04/emacs-evil-narrow-region/"
  (interactive "<r><!>")
  (if (region-active-p)
      (progn
        (deactivate-mark)
        (let ((buf (clone-indirect-buffer nil nil)))
          (with-current-buffer buf
            (narrow-to-region beg end))
          (switch-to-buffer buf)))
    (widen)))

;;;###autoload
(defun doom/set-read-only-region (begin end)
  "See http://stackoverflow.com/questions/7410125"
  (let ((modified (buffer-modified-p)))
    (add-text-properties begin end '(read-only t))
    (set-buffer-modified-p modified)))

;;;###autoload
(defun doom/set-region-writeable (begin end)
  "See http://stackoverflow.com/questions/7410125"
  (let ((modified (buffer-modified-p))
        (inhibit-read-only t))
    (remove-text-properties begin end '(read-only t))
    (set-buffer-modified-p modified)))


;; Buffer Life and Death ;;;;;;;;;;;;;;;

(unless window-system
  (defalias 'wg-workgroup-associated-buffers 'ignore)
  (defalias 'wg-current-workgroup 'ignore)
  (defalias 'wg-save-session 'ignore))

;;;###autoload
(defun doom/get-buffers (&optional all-p)
  "Get all buffers in the current workgroup.

    If PROJECT-P is non-nil, get all buffers in current workgroup
    If both are non-nil, get all project buffers across all workgroups"
  (let ((buffers (if (wg-current-workgroup t)
                     (doom/get-buffers-in-workgroup)
                   (buffer-list)))
        project-root)
    (append (aif (and (not all-p) (doom/project-root t))
                (funcall (if (eq all-p 'not) '-remove '-filter)
                         (lambda (b) (projectile-project-buffer-p b it))
                         buffers)
              buffers)
            (list doom-buffer))))

;;;###autoload
(defun doom/get-buffers-in-workgroup ()
  (let ((assoc-bufs (wg-workgroup-associated-buffers nil)))
    (--filter (memq it assoc-bufs) (buffer-list))))

;;;###autoload
(defun doom/get-buffer-names (&optional buffer-list)
  (mapcar #'buffer-name (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-visible-windows (&optional window-list)
  "Get a list of the visible windows in the current frame (that aren't popups)."
  (-remove #'doom/popup-p (or window-list (window-list))))

;;;###autoload
(defun doom/get-visible-buffers (&optional buffer-list)
  "Get a list of unburied buffers in the current project and workgroup."
  (-filter #'get-buffer-window (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-buried-buffers (&optional buffer-list)
  "Get a list of buried buffers in the current project and workgroup."
  (-remove 'get-buffer-window (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-matching-buffers (pattern &optional buffer-list)
  "Get a list of buffers that match the pattern"
  (--filter (string-match-p pattern (buffer-name it))
            (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-buffers-in-modes (modes &optional buffer-list)
  "Get a list of buffers whose major-mode is one of MODES"
  (--filter (memq (buffer-local-value 'major-mode it) modes)
            (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/get-real-buffers (&optional buffer-list)
  (-filter #'doom/real-buffer-p (or buffer-list (doom/get-buffers))))

;;;###autoload
(defun doom/kill-real-buffer (&optional arg)
  "Kill buffer (but only bury scratch buffer), then switch to a real buffer. Only buries
the buffer if it is being displayed in another window."
  (interactive (list t))
  (cond ((eq (current-buffer) doom-buffer)
         (doom-mode-init t))
        (t
         (let* ((old-project (doom/project-root))
                (buffer (current-buffer))
                (only-buffer-window-p (= (length (get-buffer-window-list buffer nil t)) 1)))
           (when (and only-buffer-window-p buffer-file-name (buffer-modified-p))
             (if (yes-or-no-p "Buffer is unsaved, save it?")
                 (save-buffer)
               (set-buffer-modified-p nil)))
           (when arg
             (doom/previous-real-buffer)
             (when (eq buffer (current-buffer))
               (switch-to-buffer doom-buffer t t)
               (when (featurep 'core-scratch)
                 (doom|update-scratch-buffer old-project)))
             (when only-buffer-window-p
               (kill-buffer buffer))))))
  t)

;;;###autoload
(defun doom/kill-unreal-buffers ()
  "Kill all buried, unreal buffers in current frame. See `doom-unreal-buffers'"
  (interactive)
  (let ((kill-list (-remove 'doom/real-buffer-p
                            (doom/get-buried-buffers (buffer-list)))))
    (mapc 'kill-buffer kill-list)
    (doom/kill-process-buffers)
    (message "Cleaned up %s buffers" (length kill-list))))

;;;###autoload
(defun doom/kill-process-buffers ()
  "Kill all buffers that represent running processes and aren't visible."
{'fraction_non_alphanumeric': 0.15123574144486693, 'fraction_numerical': 0.0039923954372623575, 'mean_word_length': 2.9345549738219896, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7293758', 'n_tokens_mistral': 3531, 'n_tokens_neox': 3387, 'n_words': 926}