(add-to-list 'default-frame-alist '(width . 120))
(add-to-list 'default-frame-alist '(height . 40))
(add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t)
(package-initialize)
(setq custom-file "~/.emacs.custom.el")
(load-file custom-file)
(load-file "~/.emacs.orgconf.el")

;; fallback to this if dbus doesn't work
;; (defun my/set-theme-based-on-time ()
;;   (let ((hour (string-to-number (format-time-string "%H"))))
;;     (if (or (< hour 7) (> hour 19) )
;;         (load-theme 'modus-vivendi-tinted t)
;;       (load-theme 'modus-operandi-tinted t))))
;; (my/set-theme-based-on-time)
;; (run-at-time "1 min" 3600 'my/set-theme-based-on-time)

(require 'dbus)

(defun my/system-dark-mode-p ()
  "Return non-nil if GNOME prefers dark mode."
  (string= 
   (string-trim
    (shell-command-to-string
     "gsettings get org.gnome.desktop.interface color-scheme"))
   "'prefer-dark'"))

(defvar my/current-theme nil)

(defun my/set-theme-based-on-system (&rest _)
  "Load Emacs theme matching system appearance."
  (let ((theme (if (my/system-dark-mode-p)
                   'modus-vivendi-tinted
                 'modus-operandi-tinted)))
    (unless (eq theme my/current-theme)
      (mapc #'disable-theme custom-enabled-themes)
      (load-theme theme t)
      (setq my/current-theme theme))))

;; Apply theme at startup
(my/set-theme-based-on-system)

;; Update immediately when GNOME theme changes
(dbus-register-signal
 :session
 "org.freedesktop.portal.Desktop"
 "/org/freedesktop/portal/desktop"
 "org.freedesktop.portal.Settings"
 "SettingChanged"
 #'my/set-theme-based-on-system)

(add-hook 'prog-mode-hook #'display-line-numbers-mode)
(add-hook 'prog-mode-hook #'completion-preview-mode)

(setq completion-preview-minimum-symbol-length 2)
(setq completion-preview-idle-delay 0.2)
(setq column-number-mode t)

(add-hook 'c-mode-hook #'eglot-ensure)
(add-hook 'c++-mode-hook #'eglot-ensure)
(add-hook 'rust-mode-hook #'eglot-ensure)

(spacious-padding-mode 1)

(use-package all-the-icons
  :if (display-graphic-p))

(use-package all-the-icons-dired
  :hook (dired-mode . all-the-icons-dired-mode)
  :config
  (setq all-the-icons-dired-monochrome nil))

(use-package doom-modeline
  :init
  (doom-modeline-mode 1))

(use-package vertico
  :init (vertico-mode))

(use-package orderless
  :custom
  (completion-styles '(orderless basic)))

(use-package marginalia
  :init (marginalia-mode))

(use-package consult)

(global-set-key (kbd "C-x b") #'consult-buffer)
(global-set-key (kbd "C-s") #'consult-line)

(add-hook 'prog-mode-hook #'outline-minor-mode)

;;(golden-ratio-mode 1)

(add-hook 'org-mode-hook #'visual-line-mode)
(add-hook 'org-mode-hook #'flyspell-mode)

(move-text-default-bindings)

;; Avoid performance issues in files with very long lines.
(global-so-long-mode 1)

(require 'multiple-cursors)
(global-set-key (kbd "C-S-c C-S-c") 'mc/edit-lines)
(global-set-key (kbd "C-§") 'mc/mark-next-like-this)
(global-set-key (kbd "C-M-!") 'mc/mark-all-like-this)


(global-set-key (kbd "C-d") 'duplicate-line)

(put 'scroll-left 'disabled nil)
