(defun my/dcaps-to-scaps ()
  "Convert word in DOuble CApitals to Single Capitals."
  (interactive)
  (and (= ?w (char-syntax (char-before)))
       (save-excursion
         (and (if (called-interactively-p)
                  (skip-syntax-backward "w")
                (= -3 (skip-syntax-backward "w"))
                )
              (let (case-fold-search)
                (looking-at "\\b[[:upper:]]\\{2\\}[[:lower:]]")
                )
            (capitalize-word 1)
            )
         )
       )
  )

(define-minor-mode my-dubcaps-mode
  "Toggle 'my-dubcaps-mode' and convert words in DOuble CApitals to Single Capitals as you type."
  :init-value nil
  :lighter (" DC")
  (if my-dubcaps-mode
      (add-hook 'post-self-insert-hook #'my/dcaps-to-scaps nil 'local)
    (remove-hook 'post-self-insert-hook #'my/dcaps-to-scaps 'local)))

(add-hook 'text-mode-hook #'my-dubcaps-mode)

(defun my/diminish-dubcaps ()
  (interactive)
  (diminish 'my-dubcaps-mode ""))
(add-hook 'my-dubcaps-mode-hook 'my/diminish-dubcaps)

(subword-mode)

(setq sentence-end-double-space nil)

(defun my/fill-or-unfill-paragraph (&optional unfill region)
  "Fill paragraph (or REGION). With the prefix argument UNFILL, fill it instead."
  (interactive (progn
                 (barf-if-buffer-read-only)
                 (list (if current-prefix-arg 'fill) t)))
  (let ((fill-column (if unfill fill-column (point-max))))
    (fill-paragraph nil region)))

(bind-key "M-q" 'my/fill-or-unfill-paragraph)

(defun my/fill-or-unfill-all-paragraphs (&optional unfill)
  "Fill or unfill all paragraphs in the current buffer.
With the prefix argument UNFILL, fill them instead."
  (interactive (list (if current-prefix-arg 'fill)))
  (let ((fill-column (if unfill fill-column (point-max))))
    (save-excursion
      (goto-char (point-min))
      (while (not (eobp))
        (fill-paragraph nil t)
        (forward-paragraph)))))

(bind-key "M-Q" 'my/fill-or-unfill-all-paragraphs)

(remove-hook 'text-mode-hook #'turn-on-auto-fill)
(add-hook 'text-mode-hook 'turn-on-visual-line-mode)

(setq save-abbrevs 'silently)
(setq-default abbrev-mode t)

(setq ediff-window-setup-function 'ediff-setup-windows-plain)
(setq ediff-split-window-function 'split-window-horizontally)

(setq tramp-default-method "ssh"
      tramp-backup-directory-alist backup-directory-alist
      tramp-ssh-controlmaster-options "ssh")

(bind-key "M-SPC" 'cycle-spacing)

(defun my/transform-html-links-to-org ()
"Transform all HTML <a> links in the current buffer into 'org-mode' links."
(interactive)
(goto-char (point-min))
(while (re-search-forward "<a href=\"\\(.*?\\)\">\\(.*?\\)</a>" nil t)
  (replace-match (org-make-link-string (match-string 1) (match-string 2)))))

(require 'org-clock)
(defun my/org-entry-wpm ()
  (interactive)
  (save-restriction
    (save-excursion
      (org-narrow-to-subtree)
      (goto-char (point-min))
      (let* ((words (count-words-region (point-min) (point-max)))

             (minutes (org-clock-sum-current-item))
             (wpm (/ words minutes)))
        (message "WPM: %d (words: %d, minutes: %d)" wpm words minutes)
        (kill-new (number-to-string wpm))
        )
      )
    )
  )

(setq dictionary-server "automatic")

(use-package writegood-mode
  :diminish writegood-mode
  :config
  (progn (add-hook 'text-mode-hook 'writegood-mode))
  )

(use-package ob-docker-build
  :straight (ob-docker-build :type git :host github :repo "ifitzpat/ob-docker-build")
  :defer t
  :config
  (add-to-list 'org-babel-load-languages '(docker-build . t))
  (org-babel-do-load-languages 'org-babel-load-languages org-babel-load-languages)
)

(use-package flyspell
  :config (setq ispell-program-name "hunspell"
                ispell-default-dictionary "en_US"
                )
  :diminish (flyspell-mode . "φ")
  :hook (text-mode . flyspell-mode)
  :bind (
         ("M-<f7>" . flyspell-buffer)
         ("<f7>" . flyspell-word)
         ("C-;" . flyspell-auto-correct-previous-word)
         )
  )

(use-package flyspell-correct
  :after flyspell
  :bind (:map flyspell-mode-map ("C-;" . flyspell-correct-wrapper))
)

(use-package flycheck
  :defer t
  :diminish (flycheck-mode . "")
  :config
  (setq flycheck-emacs-lisp-load-path 'inherit)
  (setq flycheck-emacs-lisp-load-path (concat user-emacs-directory "straight/build")))

(use-package flycheck-checkbashisms
  :config
  (flycheck-checkbashisms-setup)
)

(use-package yaml-mode
  :config
  (add-to-list 'auto-mode-alist '("\\.yml\\'" . yaml-mode))
  (add-to-list 'auto-mode-alist '("\\.yaml\\'" . yaml-mode))
  )

(use-package docker-compose-mode)
