<commit_before>;;; lang/org/contrib/roam2.el -*- lexical-binding: t; -*-
;;;###if (featurep! +roam2)

(defvar +org-roam-auto-backlinks-buffer nil
  "If non-nil, open and close the org-roam backlinks buffer automatically.

This ensures the backlinks buffer is always present so long as an org roam file
is visible. Once they are all closed or killed, the backlinks buffer will be
closed.")

(defvar +org-roam-link-to-org-use-id 'create-if-interactive
  "`org-roam-directory' local value for `org-id-link-to-org-use-id'.
It's not recommended to set this to nil in order for other parts
of org-mode to properly utilize ID links.")


;;
;;; Packages

(use-package! org-roam
  :hook (org-load . +org-init-roam-h)
  :preface
  ;; Set this to nil so we can later detect if the user has set custom values
  ;; for these variables. If not, default values will be set in the :config
  ;; section.
  (defvar org-roam-directory nil)

  :init
  (doom-load-packages-incrementally
   '(ansi-color dash f rx seq magit-section emacsql emacsql-sqlite))

  ;; Don't display warning message dedicated for v1 users. Need to be set early.
  (setq org-roam-v2-ack t)

  (defadvice! +org-roam-suppress-sqlite-build-a (fn &rest args)
    "Suppress automatic building of sqlite3 binary when loading `org-roam'.
This is a blocking operation that can take a while to complete
and better be deferred when there will be an actual demand for
the database. See `+org-init-roam-h' for the launch process."
    :around #'emacsql-sqlite-ensure-binary
    (if (not (boundp 'org-roam-db-version))
        (apply fn args)
      (advice-remove #'emacsql-sqlite-ensure-binary #'+org-roam-suppress-sqlite-build-a)
      nil))

  :config
  (defun +org-init-roam-h ()
    "Setup `org-roam' but don't immediately initialize its database.
Instead, initialize it when it will be actually needed."
    (letf! ((#'org-roam-db-sync #'ignore))
      (org-roam-db-autosync-enable)))

  (defadvice! +org-roam-try-init-db-a (&rest _)
    "Try to initialize org-roam database at the last possible safe moment.
In case of failure, fail gracefully."
    :before #'org-roam-db-query
    (message "Initializing org-roam database...")
    (let ((run-cleanup-p t))
      (unwind-protect
          ;; Try to build the binary if it doesn't exist. In case of failure
          ;; this will error, run the cleanup and exit, and in case of success
          ;; this will return nil and sync the database.
          (setq run-cleanup-p (emacsql-sqlite-ensure-binary))
        (when run-cleanup-p
          (setq org-roam--sqlite-available-p nil)
          (org-roam-teardown)
          (message (concat "EmacSQL failied to build SQLite binary for org-roam; "
                           "see *Compile-Log* buffer for details.\n"
                           "To try reinitialize org-roam, run \"M-x org-roam-setup\"")))))
    (advice-remove 'org-roam-db-query #'+org-roam-try-init-db-a)
    (org-roam-db-sync))

  (setq org-roam-directory
        (thread-first (or org-roam-directory "roam")
          (expand-file-name org-directory)
          (file-truename)
          (file-name-as-directory))
        org-roam-node-display-template
        (format "${doom-hierarchy:*} %s %s"
                (propertize "${doom-type:15}" 'face 'font-lock-keyword-face)
                (propertize "${doom-tags:-1}" 'face 'org-tag))
        org-roam-completion-everywhere t
        org-roam-db-gc-threshold most-positive-fixnum
        ;; Reverse the default to favor faster searchers over slower ones.
        org-roam-list-files-commands '(fd fdfind rg find))

  (add-to-list 'org-roam-node-template-prefixes '("doom-tags" . "#"))
  (add-to-list 'org-roam-node-template-prefixes '("doom-type" . "@"))
<commit_msg>fix(org): roam minibuffer completion width<commit_after>;;; lang/org/contrib/roam2.el -*- lexical-binding: t; -*-
;;;###if (featurep! +roam2)

(defvar +org-roam-auto-backlinks-buffer nil
  "If non-nil, open and close the org-roam backlinks buffer automatically.

This ensures the backlinks buffer is always present so long as an org roam file
is visible. Once they are all closed or killed, the backlinks buffer will be
closed.")

(defvar +org-roam-link-to-org-use-id 'create-if-interactive
  "`org-roam-directory' local value for `org-id-link-to-org-use-id'.
It's not recommended to set this to nil in order for other parts
of org-mode to properly utilize ID links.")


;;
;;; Packages

(use-package! org-roam
  :hook (org-load . +org-init-roam-h)
  :preface
  ;; Set this to nil so we can later detect if the user has set custom values
  ;; for these variables. If not, default values will be set in the :config
  ;; section.
  (defvar org-roam-directory nil)

  :init
  (doom-load-packages-incrementally
   '(ansi-color dash f rx seq magit-section emacsql emacsql-sqlite))

  ;; Don't display warning message dedicated for v1 users. Need to be set early.
  (setq org-roam-v2-ack t)

  (defadvice! +org-roam-suppress-sqlite-build-a (fn &rest args)
    "Suppress automatic building of sqlite3 binary when loading `org-roam'.
This is a blocking operation that can take a while to complete
and better be deferred when there will be an actual demand for
the database. See `+org-init-roam-h' for the launch process."
    :around #'emacsql-sqlite-ensure-binary
    (if (not (boundp 'org-roam-db-version))
        (apply fn args)
      (advice-remove #'emacsql-sqlite-ensure-binary #'+org-roam-suppress-sqlite-build-a)
      nil))

  :config
  (defun +org-init-roam-h ()
    "Setup `org-roam' but don't immediately initialize its database.
Instead, initialize it when it will be actually needed."
    (letf! ((#'org-roam-db-sync #'ignore))
      (org-roam-db-autosync-enable)))

  (defadvice! +org-roam-try-init-db-a (&rest _)
    "Try to initialize org-roam database at the last possible safe moment.
In case of failure, fail gracefully."
    :before #'org-roam-db-query
    (message "Initializing org-roam database...")
    (let ((run-cleanup-p t))
      (unwind-protect
          ;; Try to build the binary if it doesn't exist. In case of failure
          ;; this will error, run the cleanup and exit, and in case of success
          ;; this will return nil and sync the database.
          (setq run-cleanup-p (emacsql-sqlite-ensure-binary))
        (when run-cleanup-p
          (setq org-roam--sqlite-available-p nil)
          (org-roam-teardown)
          (message (concat "EmacSQL failied to build SQLite binary for org-roam; "
                           "see *Compile-Log* buffer for details.\n"
                           "To try reinitialize org-roam, run \"M-x org-roam-setup\"")))))
    (advice-remove 'org-roam-db-query #'+org-roam-try-init-db-a)
    (org-roam-db-sync))

  (setq org-roam-directory
        (thread-first (or org-roam-directory "roam")
          (expand-file-name org-directory)
          (file-truename)
          (file-name-as-directory))
        org-roam-node-display-template
        (format "${doom-hierarchy:*} %s %s"
                (propertize "${doom-type:12}" 'face 'font-lock-keyword-face)
                (propertize "${doom-tags:42}" 'face 'org-tag))
        org-roam-completion-everywhere t
        org-roam-db-gc-threshold most-positive-fixnum
        ;; Reverse the default to favor faster searchers over slower ones.
        org-roam-list-files-commands '(fd fdfind rg find))

  (add-to-list 'org-roam-node-template-prefixes '("doom-tags" . "#"))
  (add-to-list 'org-roam-node-template-prefixes '("doom-type" . "@"))

  ;; REVIEW Remove when addressed upstream. See org-roam/org-roam#2066.
  (defadvice! +org--roam-fix-completion-width-for-vertico-a (fn &rest args)
    "Fixes completion candidate width for vertico users."
    :around #'org-roam-node-read--to-candidate
    (letf! (defun org-roam-node--format-entry (template node &optional width)
             (funcall org-roam-node--format-entry template node
                      (if (bound-and-true-p vertico-mode)
                          (if (minibufferp)
                              (window-width)
                            (1- (frame-width)))
                        width)))
      (apply fn args)))

{'fraction_non_alphanumeric': 0.12722583814319047, 'fraction_numerical': 0.0027017069875967087, 'mean_word_length': 2.671776375112714, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4533356', 'n_tokens_mistral': 2724, 'n_tokens_neox': 2650, 'n_words': 796}