<commit_before>;;; ivy.el -*- lexical-binding: t; -*-

(use-package counsel
  :ensure t
  :commands (ar/counsel-ag
             ar/ivy-occur)
  :bind (:map
         global-map
         ("C-c i" . counsel-semantic-or-imenu)
         ("M-y" . counsel-yank-pop)
         ("M-x" . counsel-M-x)
         :map counsel-ag-map
         ("C-c C-e" . ar/ivy-occur))
  :validate-custom
  ;; https://oremacs.com/2017/08/04/ripgrep/
  (counsel-grep-base-command (if (executable-find "rg")
                                 "rg -i -M 120 --no-heading --line-number --color never '%s' %s"
                               counsel-grep-base-command))
  :config
  (push '(counsel-M-x . "") ivy-initial-inputs-alist)
  (push '(counsel-rg . "--glob ** -- ") ivy-initial-inputs-alist)
  (push '(counsel-ag . "--file-search-regex . -- ") ivy-initial-inputs-alist)

  ;; `ar/ivy-occur',`ar/counsel-ag', `ar/wgrep-abort-changes' and `ar/wgrep-finish-edit' replicate a more
  ;; streamlined result-editing workflow I was used to in helm-ag.
  (defvar ar/ivy-occur--win-config)
  (defun ar/ivy-occur ()
    "Stop completion and put the current candidates into a new buffer.

The new buffer remembers current action(s).

While in the *ivy-occur* buffer, selecting a candidate with RET or
a mouse click will call the appropriate action for that candidate.

There is no limit on the number of *ivy-occur* buffers."
    (interactive)
    (if (not (window-minibuffer-p))
        (user-error "No completion session is active")
      ;; ar addition start.
      (setq ar/ivy-occur--win-config
            (current-window-configuration))
      ;; ar addition end.
      (let* ((caller (ivy-state-caller ivy-last))
             (occur-fn (or (plist-get ivy--occurs-list caller)
                           #'ivy--occur-default))
             (buffer
              (generate-new-buffer
               (format "*ivy-occur%s \"%s\"*"
                       (if caller
                           (concat " " (prin1-to-string caller))
                         "")
                       ivy-text))))
        (with-current-buffer buffer
          (funcall occur-fn ivy--old-cands)
          (setf (ivy-state-text ivy-last) ivy-text)
          (setq ivy-occur-last ivy-last)
          ;; ar addition.
          (ivy-wgrep-change-to-wgrep-mode))
        (ivy-exit-with-action
         (lambda (_)
           (pop-to-buffer buffer)
           (setq next-error-last-buffer buffer)
           (setq-local next-error-function #'ivy-occur-next-error)
           ;; ar addition.
           (delete-other-windows))))))

  (defun ar/counsel-ag--strip-insert-item (item)
    "Strip ITEM of file info.
For example:

\"some-file.el:43:  gimme this text only\"
=> \"  gimme this text only\"
"
    (insert (replace-regexp-in-string "^[^:]+:[^:]+:" ""
                                      (if (stringp item)
                                          item
                                        (car item)))))

  (defun ar/counsel-ag (arg)
    (interactive "P")
    (require 'counsel)
    (ivy-set-actions
     'counsel-rg
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))
    (ivy-set-actions
     'counsel-ag
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))
    (ivy-set-actions
     'counsel-pt
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))
    (ivy-set-actions
     'counsel-ack
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))

    (defvar ar/counsel-ag--default-locaction nil)
    (when (or arg (not ar/counsel-ag--default-locaction))
      ;; Prefix consumed by ar/counsel-ag. Avoid counsel-ag from using.
      (setq current-prefix-arg nil)
      (setq ar/counsel-ag--default-locaction
            (read-directory-name "search in: " default-directory nil t)))

    (let ((kmap counsel-ag-map))
      (define-key kmap (kbd "C-x C-f") (lambda ()
                                         (interactive)
                                         (ivy-quit-and-run
                                           (ar/counsel-ag t))))

      (define-key kmap (kbd "<tab>") (lambda ()
                                       (interactive)
                                       ;; First invocation calls ivy-call.
                                       ;; Second invocation calls ivy-alt-done.
                                       (if (eq this-command last-command)
                                           (ivy-alt-done)
                                         (ivy-call))))
      (cond ((executable-find "rg")
             (counsel-rg nil ar/counsel-ag--default-locaction))
            ((executable-find "pt")
             (counsel-pt))
            ((executable-find "ag")
             (counsel-ag nil ar/counsel-ag--default-locaction))
            (t
             (counsel-ack)))))

  (defun ar/wgrep-finish-edit ()
    (interactive)
    (let ((wgrep-auto-save-buffer t))
      (wgrep-finish-edit))
    (quit-window)
    (set-window-configuration ar/ivy-occur--win-config)
    (select-window (nth 0 (window-list))))

  (defun ar/wgrep-abort-changes ()
    (interactive)
    (wgrep-abort-changes)
    (quit-window)
    (set-window-configuration ar/ivy-occur--win-config)
    (select-window (nth 0 (window-list))))

  (defun adviced:counsel-M-x-action (orig-fun &rest r)
    "Additional support for multiple cursors."
    (apply orig-fun r)
    (let ((cmd (intern (string-remove-prefix (nth 0 r) "\\^"))))
      (when (and (boundp 'multiple-cursors-mode)<commit_msg>Fixing function typo<commit_after>;;; ivy.el -*- lexical-binding: t; -*-

(use-package counsel
  :ensure t
  :commands (ar/counsel-ag
             ar/ivy-occur)
  :bind (:map
         global-map
         ("C-c i" . counsel-semantic-or-imenu)
         ("M-y" . counsel-yank-pop)
         ("M-x" . counsel-M-x)
         :map counsel-ag-map
         ("C-c C-e" . ar/ivy-occur))
  :validate-custom
  ;; https://oremacs.com/2017/08/04/ripgrep/
  (counsel-grep-base-command (if (executable-find "rg")
                                 "rg -i -M 120 --no-heading --line-number --color never '%s' %s"
                               counsel-grep-base-command))
  :config
  (push '(counsel-M-x . "") ivy-initial-inputs-alist)
  (push '(counsel-rg . "--glob ** -- ") ivy-initial-inputs-alist)
  (push '(counsel-ag . "--file-search-regex . -- ") ivy-initial-inputs-alist)

  ;; `ar/ivy-occur',`ar/counsel-ag', `ar/wgrep-abort-changes' and `ar/wgrep-finish-edit' replicate a more
  ;; streamlined result-editing workflow I was used to in helm-ag.
  (defvar ar/ivy-occur--win-config)
  (defun ar/ivy-occur ()
    "Stop completion and put the current candidates into a new buffer.

The new buffer remembers current action(s).

While in the *ivy-occur* buffer, selecting a candidate with RET or
a mouse click will call the appropriate action for that candidate.

There is no limit on the number of *ivy-occur* buffers."
    (interactive)
    (if (not (window-minibuffer-p))
        (user-error "No completion session is active")
      ;; ar addition start.
      (setq ar/ivy-occur--win-config
            (current-window-configuration))
      ;; ar addition end.
      (let* ((caller (ivy-state-caller ivy-last))
             (occur-fn (or (plist-get ivy--occurs-list caller)
                           #'ivy--occur-default))
             (buffer
              (generate-new-buffer
               (format "*ivy-occur%s \"%s\"*"
                       (if caller
                           (concat " " (prin1-to-string caller))
                         "")
                       ivy-text))))
        (with-current-buffer buffer
          (funcall occur-fn ivy--old-cands)
          (setf (ivy-state-text ivy-last) ivy-text)
          (setq ivy-occur-last ivy-last)
          ;; ar addition.
          (ivy-wgrep-change-to-wgrep-mode))
        (ivy-exit-with-action
         (lambda (_)
           (pop-to-buffer buffer)
           (setq next-error-last-buffer buffer)
           (setq-local next-error-function #'ivy-occur-next-error)
           ;; ar addition.
           (delete-other-windows))))))

  (defun ar/counsel-ag--strip-insert-item (item)
    "Strip ITEM of file info.
For example:

\"some-file.el:43:  gimme this text only\"
=> \"  gimme this text only\"
"
    (insert (replace-regexp-in-string "^[^:]+:[^:]+:" ""
                                      (if (stringp item)
                                          item
                                        (car item)))))

  (defun ar/counsel-ag (arg)
    (interactive "P")
    (require 'counsel)
    (ivy-set-actions
     'counsel-rg
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))
    (ivy-set-actions
     'counsel-ag
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))
    (ivy-set-actions
     'counsel-pt
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))
    (ivy-set-actions
     'counsel-ack
     `(("i" ,'ar/counsel-ag--strip-insert-item
        "insert")))

    (defvar ar/counsel-ag--default-location nil)
    (when (or arg (not ar/counsel-ag--default-location))
      ;; Prefix consumed by ar/counsel-ag. Avoid counsel-ag from using.
      (setq current-prefix-arg nil)
      (setq ar/counsel-ag--default-location
            (read-directory-name "search in: " default-directory nil t)))

    (let ((kmap counsel-ag-map))
      (define-key kmap (kbd "C-x C-f") (lambda ()
                                         (interactive)
                                         (ivy-quit-and-run
                                           (ar/counsel-ag t))))

      (define-key kmap (kbd "<tab>") (lambda ()
                                       (interactive)
                                       ;; First invocation calls ivy-call.
                                       ;; Second invocation calls ivy-alt-done.
                                       (if (eq this-command last-command)
                                           (ivy-alt-done)
                                         (ivy-call))))
      (cond ((executable-find "rg")
             (counsel-rg nil ar/counsel-ag--default-location))
            ((executable-find "pt")
             (counsel-pt))
            ((executable-find "ag")
             (counsel-ag nil ar/counsel-ag--default-location))
            (t
             (counsel-ack)))))

  (defun ar/wgrep-finish-edit ()
    (interactive)
    (let ((wgrep-auto-save-buffer t))
      (wgrep-finish-edit))
    (quit-window)
    (set-window-configuration ar/ivy-occur--win-config)
    (select-window (nth 0 (window-list))))

  (defun ar/wgrep-abort-changes ()
    (interactive)
    (wgrep-abort-changes)
    (quit-window)
    (set-window-configuration ar/ivy-occur--win-config)
    (select-window (nth 0 (window-list))))

  (defun adviced:counsel-M-x-action (orig-fun &rest r)
    "Additional support for multiple cursors."
    (apply orig-fun r)
    (let ((cmd (intern (string-remove-prefix (nth 0 r) "\\^"))))
      (when (and (boundp 'multiple-cursors-mode)
{'fraction_non_alphanumeric': 0.14887794198139026, 'fraction_numerical': 0.0031016237912789638, 'mean_word_length': 1.6034196152932796, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 48, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '322620', 'n_tokens_mistral': 3662, 'n_tokens_neox': 3384, 'n_words': 747}