about summary refs log blame commit diff stats
path: root/.emacs.d/lisp/init-minibuffer.el
blob: 4b5028bbfbeac2c2a08f3b7fe1fc51c117cf1a31 (plain) (tree)




















                                                                                              

                                                                         





                                                              













                                                                                       
























                                                                                    








                                                            
                

                                

                                                                                           
                 
                                                                                      





                                       
                                                 
                            
                         

                                            










                                                     
         






                                                       







                                                       







                                                              
































                                                                                          
                                        


                                                                               






                                                                                                             











































                                                                                               






















                                                                                  

                                                                           


                                                     



                                                           
                                                                   








                                                              


                                    
                                                                                   



                                                                              
                                                               
 
                                                







                                                                   
 
                               
 


                                               
                                     
 











                                                                                                  

                                       

                                        





                                                                                 


                                                                                 

















                                                                                                                
                                                                             
                                           


                                                                       



                                                                                                                  
                                                                                                                           


                                                                        
                                         
                                              

                                                                                 
                                                 









                                                                                      





                                                                           





















                                                                                     
                         



                                              
       
                    



                       
                        
                        

                              


                                   
       

                                                         




                                                                                                
                           
 















                                                                   
;;; init-minibuffer.el --- Minibuffer Completion Configuration File -*- lexical-binding: t -*-
;;; Commentary:
;; Config for completion etc in the minibuffer (vertico, embark, consult, etc)
;; Most of it is taken from the READMEs and wikis of those packages.
;; Relies on orderless config in init-completion.el
;;; Code:

(use-package emacs
  :init
  ;; Do not allow the cursor in the minibuffer prompt
  (setq minibuffer-prompt-properties
        '(read-only t cursor-intangible t face minibuffer-prompt))
  (add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
  (setq enable-recursive-minibuffers t)
  (minibuffer-depth-indicate-mode t))

(use-package vertico
  :init
  (vertico-mode)
  (setq vertico-cycle t)
  (advice-add #'vertico--format-candidate :around
              (lambda (orig cand prefix suffix index start)
                (setq cand (funcall orig cand prefix suffix index start))
                (concat
                 (if (= vertico--index index)
                     (propertize "» " 'face 'vertico-current)
                   "  ")
                 cand)))
  :config
  (defun define-vertico-key (key &rest defs)
    "Define KEY conditionally in the vertico keymap.
DEFS is a plist associating completion categories to commands."
    (let ((default-command (lookup-key vertico-map (kbd key))))
      (define-key vertico-map (kbd key)
        (list 'menu-item nil defs :filter
              (lambda (d)
                (or (plist-get d (completion-metadata-get
                                  (completion-metadata (minibuffer-contents)
                                                       minibuffer-completion-table
                                                       minibuffer-completion-predicate)
                                  'category))
                    default-command))))))

  (defun down-from-outside ()
    "Move to next candidate in minibuffer, even when minibuffer isn't selected."
    (interactive)
    (with-selected-window (active-minibuffer-window)
      (execute-kbd-macro [down])))

  (defun up-from-outside ()
    "Move to previous candidate in minibuffer, even when minibuffer isn't selected."
    (interactive)
    (with-selected-window (active-minibuffer-window)
      (execute-kbd-macro [up])))

  (defun preview-from-outside ()
    "Preview the selected candidate, even when minibuffer isn't selected."
    (interactive)
    (with-selected-window (active-minibuffer-window)
      (execute-kbd-macro (kbd "M-."))))

  (defun to-and-fro-minibuffer ()
    "Go back and forth between minibuffer and other window."
    (interactive)
    (if (window-minibuffer-p (selected-window))
        (select-window (minibuffer-selected-window))
      (select-window (active-minibuffer-window))))

  :bind (("C-M-<" . up-from-outside)
         ("C-M->" . down-from-outside)
         ("C-M-+" . preview-from-outside)
         ("M-X" . to-and-fro-minibuffer)
         ("C-M-S-g" . minibuffer-keyboard-quit)))

;; See init-packages.el for fetching of Vertico Extenions
;; Required extensions must be in the vertico-extensions var
(use-package vertico-directory
  :after vertico
  :init
  (defvar switching-project nil)
  (defun vertico-directory-enter-or-select-project ()
    "Wrapper around vertico-directory-enter that plays nicely with selecting new projects."
    (interactive)
    ;; When selecting a project, use this to return, instead of entering the directory
    (if switching-project
        (vertico-exit)
      (vertico-directory-enter)))
  (defun read-project (orig &rest args)
    (let ((switching-project t))
      (apply orig args)))
  (advice-add 'project-prompt-project-dir :around
              'read-project)
  (define-vertico-key "/"
    'file #'vertico-directory-slash
    'project-file #'vertico-directory-slash)
  (define-vertico-key "RET"
    'file #'vertico-directory-enter-or-select-project
    'project-file #'vertico-directory-enter)
  (define-vertico-key "~"
    'file #'vertico-directory-home)
  (define-vertico-key "DEL"
    'file #'vertico-directory-delete-char
    'project-file #'vertico-directory-delete-char)
  (define-vertico-key "M-DEL"
    'file #'vertico-directory-delete-word
    'project-file #'vertico-directory-delete-word)
  :config
  (defun vertico-directory-slash ()
    (interactive)
    (if (and (>= vertico--index 0)
             (string-suffix-p "/" (vertico--candidate))
             (vertico-directory--completing-file-p))
        (vertico-insert)
      (insert "/")))
  (defun vertico-directory-home ()
    (interactive)
    (if (and (string-suffix-p "/" (vertico--candidate))
             (vertico-directory--completing-file-p))
        (insert "~/")
      (insert "~")))
  :load-path vertico-extensions-dir
  :commands vertico-directory-enter
  ;; Tidy shadowed file names
  :hook (rfn-eshadow-update-overlay . vertico-directory-tidy))

(use-package vertico-repeat
  :load-path vertico-extensions-dir
  :bind ("M-P" . vertico-repeat))

(use-package consult
  :bind (;; C-c bindings (mode-specific-map)
         ("C-c h" . consult-history)
         ("C-c m" . consult-mode-command)
         ("C-c b" . consult-bookmark)
         ("C-c k" . consult-kmacro)
         ;; C-x bindings (ctl-x-map)
         ("C-x M-:" . consult-complex-command)     ;; orig. repeat-complex-command
         ("C-x b" . consult-buffer)                ;; orig. switch-to-buffer
         ("C-x B" . consult-buffer-no-preview)     ;; orig. switch-to-buffer
         ("C-x 4 b" . consult-buffer-other-window) ;; orig. switch-to-buffer-other-window
         ("C-x 5 b" . consult-buffer-other-frame)  ;; orig. switch-to-buffer-other-frame
         ;; Custom M-# bindings for fast register access
         ("M-#" . consult-register-load)
         ("M-'" . consult-register-store)          ;; orig. abbrev-prefix-mark (unrelated)
         ("C-M-#" . consult-register)
         ;; Other custom bindings
         ("C-S-s" . consult-line)
         ("M-*" . consult-line-symbol-at-point)
         ("C-c f" . consult-recent-file)
         ("C-c r" . consult-ripgrep)
         ("C-c R" . consult-ripgrep-auto-preview)
         ("C-c *" . consult-ripgrep-symbol-at-point)
         ("M-y" . consult-yank-pop)                ;; orig. yank-pop
         ("<help> a" . consult-apropos)            ;; orig. apropos-command
         ;; M-g bindings (goto-map)
         ("M-g e" . consult-compile-error)
         ("M-g f" . consult-flycheck)
         ("M-g g" . consult-goto-line)             ;; orig. goto-line
         ("M-g M-g" . consult-goto-line)           ;; orig. goto-line
         ("M-g o" . consult-outline)               ;; Alternative: consult-org-heading
         ("M-g m" . consult-mark)
         ("M-g k" . consult-global-mark)
         ("M-g i" . consult-imenu)
         ("M-g I" . consult-imenu-multi)
         :map isearch-mode-map
         ("M-e" . consult-isearch)                 ;; orig. isearch-edit-string
         ("M-s e" . consult-isearch)               ;; orig. isearch-edit-string
         ("M-s l" . consult-line)                  ;; needed by consult-line to detect isearch
         (:map vertico-map
               ;; These are used for previewing with some consult commands (see consult-customize call below)
               ("C-S-p" . vertico-previous)
               ("C-S-n" . vertico-next)
               ;; Toggle preview on/off without changing preview-key
               ("M-P" . consult-toggle-preview)))

  :init

  ;; Optionally configure the register formatting. This improves the register
  ;; preview for `consult-register', `consult-register-load',
  ;; `consult-register-store' and the Emacs built-ins.
  (setq register-preview-delay 0
        register-preview-function #'consult-register-format)

  ;; Optionally tweak the register preview window.
  ;; This adds thin lines, sorting and hides the mode line of the window.
  (advice-add #'register-preview :override #'consult-register-window)

  ;; Optionally replace `completing-read-multiple' with an enhanced version.
  (advice-add #'completing-read-multiple :override #'consult-completing-read-multiple)

  ;; Use Consult to select xref locations with preview
  (setq xref-show-xrefs-function #'consult-xref
        xref-show-definitions-function #'consult-xref)

  :config

  (defun consult-ripgrep-symbol-at-point (&optional dir initial)
    (interactive
     (list prefix-arg (when-let ((s (symbol-at-point)))
                        (symbol-name s))))
    (consult-ripgrep dir initial))
  (defun consult-ripgrep-auto-preview (&optional dir initial)
    (interactive "P")
    (consult-ripgrep dir initial))
  (defun consult-ripgrep-unrestricted (&optional dir initial)
    (interactive "P")
    (let ((consult-ripgrep-args (replace-regexp-in-string "\\." "-uu ." consult-ripgrep-args)))
      (consult-ripgrep dir initial)))
  (defun consult-z-ripgrep (&optional dir initial)
    (interactive "P")
    (let ((consult-ripgrep-args (replace-regexp-in-string "\\." "-z ." consult-ripgrep-args)))
      (consult-ripgrep dir initial)))
  (defun consult-buffer-no-preview ()
    (interactive)
    (consult-buffer))
  (defun consult-line-symbol-at-point ()
    (interactive)
    (consult-line (thing-at-point 'symbol)))
  (defvar consult--fd-command nil)
  (defun consult--fd-builder (input)
    (unless consult--fd-command
      (setq consult--fd-command
            (if (eq 0 (call-process-shell-command "fdfind"))
                "fdfind"
              "fd")))
    (pcase-let* ((`(,arg . ,opts) (consult--command-split input))
                 (`(,re . ,hl) (funcall consult--regexp-compiler
                                        arg 'extended)))
      (when re
        (list :command (append
                        (list consult--fd-command
                              "--color=never" "--full-path"
                              (consult--join-regexps re 'extended))
                        opts)
              :highlight hl))))

  (defun consult-fd (&optional dir initial)
    (interactive "P")
    (let* ((prompt-dir (consult--directory-prompt "Fd" dir))
           (default-directory (cdr prompt-dir)))
      (find-file (consult--find (car prompt-dir) #'consult--fd-builder initial))))

  ;; Add these here, as we have two bindings for search map (M-s and C-c s)
  (define-key search-map "f" 'consult-fd)
  (define-key search-map "F" 'consult-find)
  (define-key search-map (kbd "M-f") 'consult-locate)
  (define-key search-map "g" 'consult-grep)
  (define-key search-map "G" 'consult-git-grep)
  (define-key search-map "r" 'consult-ripgrep)
  (define-key search-map "R" 'consult-ripgrep-auto-preview)
  (define-key search-map (kbd "M-r") 'consult-ripgrep-unrestricted)
  (define-key search-map "*" 'consult-ripgrep-symbol-at-point)
  (define-key search-map "z" 'consult-z-ripgrep)
  (define-key search-map "l" 'consult-line)
  (define-key search-map "L" 'consult-line-multi)
  (define-key search-map "m" 'consult-multi-occur)
  (define-key search-map "k" 'consult-keep-lines)
  (define-key search-map "u" 'consult-focus-lines)
  (define-key search-map "e" 'consult-isearch)

  (consult-customize
   consult-theme
   :preview-key '(:debounce 0.2 any)
   ;; For these commands we can use C-S/C-P to scoll and preview, or M-. to preview
   consult-ripgrep consult-git-grep consult-grep
   consult-ripgrep-unrestricted consult-ripgrep-symbol-at-point
   consult-bookmark consult-recent-file consult-xref consult-buffer-no-preview
   consult--source-file consult--source-project-file consult--source-bookmark
   :preview-key (list (kbd "M-.") (kbd "C-S-n") (kbd "C-S-p")))

  (defvar-local consult-toggle-preview-orig nil)
  (defun consult-toggle-preview ()
    "Command to enable/disable preview."
    (interactive)
    (if consult-toggle-preview-orig
        (setq consult--preview-function consult-toggle-preview-orig
              consult-toggle-preview-orig nil)
      (setq consult-toggle-preview-orig consult--preview-function
            consult--preview-function #'ignore)))

  (setq consult-narrow-key "<")

  (setq consult-project-root-function
        (lambda ()
          (when-let (project (project-current))
            (project-root project))))

  ;; Switches perspective if we select a buffer from another perspective, but note that previewing
  ;; a buffer adds it to the current perspective, so preview should be disabled before removing
  ;; perspective narrowing
  (defun consult--persp-buffer-action (orig &rest args)
    (when (not (cdr args)) ;; (cdr args) is norecord, which should distinguish preview/non-preview
      (let ((buffer (window-normalize-buffer-to-switch-to (car args))))
        (unless (persp-is-current-buffer buffer)
          (let ((other-persp (persp-buffer-in-other-p buffer)))
            (when (eq (car-safe other-persp) (selected-frame))
              (persp-switch (cdr other-persp)))))))
    (apply orig args))
  (advice-add 'consult--buffer-action :around 'consult--persp-buffer-action)

  (defvar consult-initial-narrow-config
    '((consult-buffer . ?x)
      (consult-buffer-no-preview . ?x)))
  ;; Add initial narrowing hook
  (defun consult-initial-narrow ()
    (when-let (key (alist-get this-command consult-initial-narrow-config))
      (setq unread-command-events (append unread-command-events (list key 32)))))
  (add-hook 'minibuffer-setup-hook #'consult-initial-narrow)

  (when (and (eq system-type 'darwin) (string-match-p "^find" consult-find-args))
    (setq consult-find-args (concat "g" consult-find-args)))

  (defvar consult--source-perspective-buffer
    `(:name     "Perspective Buffer"
                :narrow   (?x . "Perspective")
                :hidden   t
                :category buffer
                :face     consult-buffer
                :history  buffer-name-history
                :state    ,#'consult--buffer-state
                :enabled  ,(lambda () persp-mode)
                :items
                ,(lambda ()
                   (consult--buffer-query :sort 'visibility
                                          :predicate #'persp-is-current-buffer
                                          :as #'buffer-name)))
    "Perspective buffer candidate source for `consult-buffer'.")
  (add-to-list 'consult-buffer-sources 'consult--source-perspective-buffer t)

  ;; Copy of consult--source-project-file to use with perspective narrowing (identical except for narrowing key)
  ;; Put before consult--source-project-file so we get recentf behaviour here
  (defvar consult--source-perspective-files
    (plist-put (plist-put (copy-sequence  consult--source-project-file)
                          :name "Project File")
               :narrow '(?x . "Perspective")))
  (add-to-list 'consult-buffer-sources 'consult--source-perspective-files t)

  ;; Versions of consult--source-project-buffer and consult--source-project-file for use by consult-project-buffer
  ;; They allow narrowing with b and f (instead of p)
  ;; The file version uses fd to find items, so that all files (rather than using recentf) are listed, respecing .gitignore
  (defvar consult--project-source-project-buffer
    (plist-put (plist-put (copy-sequence consult--source-project-buffer)
                          :hidden nil)
               :narrow '(?b . "Buffer")))
  (defvar consult--project-source-project-file
    (plist-put (plist-put (plist-put (copy-sequence consult--source-project-file)
                                     :hidden nil)
                          :narrow '(?f . "File"))
               :items '(lambda ()
                         (when-let (root (consult--project-root))
                           (let ((len (length root))
                                 (inv-root (propertize root 'invisible t)))
                             (mapcar (lambda (x)
                                       (concat inv-root (substring x len)))
                                     (split-string
                                      (shell-command-to-string
                                       (format  "fd --color never -t f -0 . %s" root))
                                      "\0" t)))))))

  (defun consult-project-buffer ()
    (interactive)
    (let ((consult-buffer-sources '(consult--project-source-project-buffer
                                    consult--project-source-project-file)))
      (consult-buffer)))

  (defun consult--orderless-regexp-compiler (input type)
    (setq input (orderless-pattern-compiler input))
    (cons
     (mapcar (lambda (r) (consult--convert-regexp r type)) input)
     (lambda (str) (orderless--highlight input str))))
  (defun consult--with-orderless (&rest args)
    (minibuffer-with-setup-hook
        (lambda ()
          (setq-local consult--regexp-compiler #'consult--orderless-regexp-compiler))
      (apply args)))
  (advice-add #'consult-ripgrep :around #'consult--with-orderless))

(use-package consult-flycheck)

(use-package consult-lsp
  :bind (:map lsp-mode-map
         ([remap xref-find-apropos] . consult-lsp-symbols)))

(use-package consult-dir
  :ensure t
  :bind (("C-x C-d" . consult-dir)
         :map vertico-map
         ("C-x C-d" . consult-dir)
         ("C-x C-j" . consult-dir-jump-file)))

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

(use-package embark
  :bind
  (("C-," . embark-act)
   ("C-." . embark-dwim)
   ("M-." . embark-dwim)
   ("C-c C-o" . embark-export)
   ("C-h b" . embark-bindings)
   ("C-h B" . describe-bindings)
   (:map minibuffer-local-map
         ("M-." . embark-preview)))
  :init
  (setq prefix-help-command #'embark-prefix-help-command)
  :config
  (defun embark-preview ()
    (interactive)
    (unless (bound-and-true-p consult--preview-function) ;; Disable preview for Consult commands
      (save-selected-window
        (let ((embark-quit-after-action))
          (embark-dwim)))))

  ;; Hide the mode line of the Embark live/completions buffers
  (add-to-list 'display-buffer-alist
               '("\\`\\*Embark Collect \\(Live\\|Completions\\)\\*"
                 nil
                 (window-parameters (mode-line-format . none)))))

(use-package embark-consult
  :after (embark consult)
  :demand t ; only necessary if you have the hook below
  ;; if you want to have consult previews as you move around an
  ;; auto-updating embark collect buffer
  :hook
  (embark-collect-mode . consult-preview-at-point-mode))

(provide 'init-minibuffer)
;;; init-minibuffer.el ends here
d='n410' href='#n410'>410 411 412 413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662



                   
                   
 
                         

    
                                     

                     

                                                  
 


                         


    
                                      

                   

                                                  
 


                         


    
                                      

                      

                                                  
 


                         


    
                                            

                       

                                                  
 


                         


    
                                               

                          

                                                  
 


                         


    
                                                 

                            

                                                  
 


                         


    
                                                  

                                           

                                                  
 


                         


    
                               

                            

                                                  
 



                                             


    
                                

                                 

                                                  
 




                                             


    
                                  

                                      

                                                  
 





                                             


    
                                              

                                                 

                                                  
 





                                             


    
                                     

                                              

                                                                
 



                                                           


    
                                             

                                                   

                                                                
 




                                                           


    
                                              

                                                        

                                                                
 





                                                           


    
                                

                       

                                                  
 



                                             


    
                                              

                       

                                                                
 



                                             


    
                                   

                                     

                                                  
 




                                             


    
                                             

                                         

                                                  
 




                                             


    
                                                   

                                                 

                                                  
 




                                                     


    
                                                        

                                                                       

                                                  
 




                                                           


    
                                            

                                                          

                                                                
 





                                                         


    
                                                      

                                                                 

                                                                
 





                                                            


    
                                                            

                                                                    

                                                                
 





                                                       


    
                                                                 

                                                                                               

                                                                
 





                                                            
 

    
                                            

                                                             

                                                                
 




                                                                     


    
                                                         

                                                  

                                                                
 




                                             


    
                                                          

                                                  

                                                                
 




                                              


    
                                                                    

                                                        

                                                                
 




                                              


    
                             



                                   
                                


    
                                                  



                                   
                                


    
                                                    



                                   
                                


    
                              



                                   
                                


    
                                           



                                   
                                


    
                                            



                                   
                                


    
                                          



                                               
                                


    
                              



                                     
                                       


    
                              



                                     
                                        


    
                                    



                                     
                                            


    
                                                 



                                     
                                                


    
                                                  



                                     
                                                


    
                                                            



                                        
                                                    





                                                          
                                     


                         
                                                              











                                                       
                                     


                        
                                                              










                                                             
                                     


                         
                                                              












                                                                      
                                             


                        
                                                              










                                                                             
                                             


                         
                                                              














                                                                                     
                                                     


                        
                                                              










                                                                                             
                                                     


                         
                                                              
















                                                                                             
                                                     


                        
                                                              




                             





                                                                                             
                                                     


                        
                                                              




                             
 
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <stdlib.h>

#include "tools/parser.h"

void
parse_null_returns_null(void **state)
{
    char *inp = NULL;
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_empty_returns_null(void **state)
{
    char *inp = "";
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_space_returns_null(void **state)
{
    char *inp = "   ";
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_cmd_no_args_returns_null(void **state)
{
    char *inp = "/cmd";
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_cmd_with_space_returns_null(void **state)
{
    char *inp = "/cmd   ";
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_cmd_with_too_few_returns_null(void **state)
{
    char *inp = "/cmd arg1";
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 2, 3, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_cmd_with_too_many_returns_null(void **state)
{
    char *inp = "/cmd arg1 arg2 arg3 arg4";
    gboolean result = TRUE;
    gchar **args = parse_args(inp, 1, 3, &result);

    assert_false(result);
    assert_null(args);
    g_strfreev(args);
}

void
parse_cmd_one_arg(void **state)
{
    char *inp = "/cmd arg1";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_true(result);
    assert_int_equal(1, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    g_strfreev(args);
}

void
parse_cmd_two_args(void **state)
{
    char *inp = "/cmd arg1 arg2";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 1, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_three_args(void **state)
{
    char *inp = "/cmd arg1 arg2 arg3";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("arg3", args[2]);
    g_strfreev(args);
}

void
parse_cmd_three_args_with_spaces(void **state)
{
    char *inp = "  /cmd    arg1  arg2     arg3 ";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("arg3", args[2]);
    g_strfreev(args);
}

void
parse_cmd_with_freetext(void **state)
{
    char *inp = "/cmd this is some free text";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 1, 1, &result);

    assert_true(result);
    assert_int_equal(1, g_strv_length(args));
    assert_string_equal("this is some free text", args[0]);
    g_strfreev(args);
}

void
parse_cmd_one_arg_with_freetext(void **state)
{
    char *inp = "/cmd arg1 this is some free text";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 1, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("this is some free text", args[1]);
    g_strfreev(args);
}

void
parse_cmd_two_args_with_freetext(void **state)
{
    char *inp = "/cmd arg1 arg2 this is some free text";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 1, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("this is some free text", args[2]);
    g_strfreev(args);
}

void
parse_cmd_min_zero(void **state)
{
    char *inp = "/cmd";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 0, 2, &result);

    assert_true(result);
    assert_int_equal(0, g_strv_length(args));
    assert_null(args[0]);
    g_strfreev(args);
}

void
parse_cmd_min_zero_with_freetext(void **state)
{
    char *inp = "/cmd";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 0, 2, &result);

    assert_true(result);
    assert_int_equal(0, g_strv_length(args));
    assert_null(args[0]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted(void **state)
{
    char *inp = "/cmd \"arg1\" arg2";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted_and_space(void **state)
{
    char *inp = "/cmd \"the arg1\" arg2";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("the arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted_and_many_spaces(void **state)
{
    char *inp = "/cmd \"the arg1 is here\" arg2";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("arg2", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_many_quoted_and_many_spaces(void **state)
{
    char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\"";
    gboolean result = FALSE;
    gchar **args = parse_args(inp, 2, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("and arg2 is right here", args[1]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_quoted(void **state)
{
    char *inp = "/cmd \"arg1\" arg2 hello there whats up";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("hello there whats up", args[2]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_quoted_and_space(void **state)
{
    char *inp = "/cmd \"the arg1\" arg2 another bit of freetext";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("the arg1", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("another bit of freetext", args[2]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_quoted_and_many_spaces(void **state)
{
    char *inp = "/cmd \"the arg1 is here\" arg2 some more freetext";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("arg2", args[1]);
    assert_string_equal("some more freetext", args[2]);
    g_strfreev(args);
}

void
parse_cmd_freetext_with_many_quoted_and_many_spaces(void **state)
{
    char *inp = "/cmd \"the arg1 is here\" \"and arg2 is right here\" and heres the free text";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 3, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("the arg1 is here", args[0]);
    assert_string_equal("and arg2 is right here", args[1]);
    assert_string_equal("and heres the free text", args[2]);
    g_strfreev(args);
}

void
parse_cmd_with_quoted_freetext(void **state)
{
    char *inp = "/cmd arg1 here is \"some\" quoted freetext";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 1, 2, &result);

    assert_true(result);
    assert_int_equal(2, g_strv_length(args));
    assert_string_equal("arg1", args[0]);
    assert_string_equal("here is \"some\" quoted freetext", args[1]);
    g_strfreev(args);
}

void
parse_cmd_with_third_arg_quoted_0_min_3_max(void **state)
{
    char *inp = "/group add friends \"The User\"";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 0, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("add", args[0]);
    assert_string_equal("friends", args[1]);
    assert_string_equal("The User", args[2]);
}

void
parse_cmd_with_second_arg_quoted_0_min_3_max(void **state)
{
    char *inp = "/group add \"The Group\" friend";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 0, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("add", args[0]);
    assert_string_equal("The Group", args[1]);
    assert_string_equal("friend", args[2]);
}

void
parse_cmd_with_second_and_third_arg_quoted_0_min_3_max(void **state)
{
    char *inp = "/group add \"The Group\" \"The User\"";
    gboolean result = FALSE;
    gchar **args = parse_args_with_freetext(inp, 0, 3, &result);

    assert_true(result);
    assert_int_equal(3, g_strv_length(args));
    assert_string_equal("add", args[0]);
    assert_string_equal("The Group", args[1]);
    assert_string_equal("The User", args[2]);
}

void
count_one_token(void **state)
{
    char *inp = "one";
    int result = count_tokens(inp);

    assert_int_equal(1, result);
}

void
count_one_token_quoted_no_whitespace(void **state)
{
    char *inp = "\"one\"";
    int result = count_tokens(inp);

    assert_int_equal(1, result);
}

void
count_one_token_quoted_with_whitespace(void **state)
{
    char *inp = "\"one two\"";
    int result = count_tokens(inp);

    assert_int_equal(1, result);
}

void
count_two_tokens(void **state)
{
    char *inp = "one two";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
count_two_tokens_first_quoted(void **state)
{
    char *inp = "\"one and\" two";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
count_two_tokens_second_quoted(void **state)
{
    char *inp = "one \"two and\"";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
count_two_tokens_both_quoted(void **state)
{
    char *inp = "\"one and then\" \"two and\"";
    int result = count_tokens(inp);

    assert_int_equal(2, result);
}

void
get_first_of_one(void **state)
{
    char *inp = "one";
    char *result = get_start(inp, 2);

    assert_string_equal("one", result);
}

void
get_first_of_two(void **state)
{
    char *inp = "one two";
    char *result = get_start(inp, 2);

    assert_string_equal("one ", result);
}

void
get_first_two_of_three(void **state)
{
    char *inp = "one two three";
    char *result = get_start(inp, 3);

    assert_string_equal("one two ", result);
}

void
get_first_two_of_three_first_quoted(void **state)
{
    char *inp = "\"one\" two three";
    char *result = get_start(inp, 3);

    assert_string_equal("\"one\" two ", result);
}

void
get_first_two_of_three_second_quoted(void **state)
{
    char *inp = "one \"two\" three";
    char *result = get_start(inp, 3);

    assert_string_equal("one \"two\" ", result);
}

void
get_first_two_of_three_first_and_second_quoted(void **state)
{
    char *inp = "\"one\" \"two\" three";
    char *result = get_start(inp, 3);

    assert_string_equal("\"one\" \"two\" ", result);
}

void
parse_options_when_none_returns_empty_hasmap(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", NULL };
    gchar *keys[] = { "opt1", NULL };

    gboolean res = FALSE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_true(options != NULL);
    assert_int_equal(0, g_hash_table_size(options));
    assert_true(res);

    options_destroy(options);
}

void
parse_options_when_opt1_no_val_sets_error(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", NULL };
    gchar *keys[] = { "opt1", NULL };

    gboolean res = TRUE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_null(options);
    assert_false(res);

    options_destroy(options);
}

void
parse_options_when_one_returns_map(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", NULL };
    gchar *keys[] = { "opt1", NULL };

    gboolean res = FALSE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_int_equal(1, g_hash_table_size(options));
    assert_true(g_hash_table_contains(options, "opt1"));
    assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
    assert_true(res);

    options_destroy(options);
}

void
parse_options_when_opt2_no_val_sets_error(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", NULL };
    gchar *keys[] = { "opt1", "opt2", NULL };

    gboolean res = TRUE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_null(options);
    assert_false(res);

    options_destroy(options);
}

void
parse_options_when_two_returns_map(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", NULL };
    gchar *keys[] = { "opt1", "opt2", NULL };

    gboolean res = FALSE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_int_equal(2, g_hash_table_size(options));
    assert_true(g_hash_table_contains(options, "opt1"));
    assert_true(g_hash_table_contains(options, "opt2"));
    assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
    assert_string_equal("val2", g_hash_table_lookup(options, "opt2"));
    assert_true(res);

    options_destroy(options);
}

void
parse_options_when_opt3_no_val_sets_error(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", NULL };
    gchar *keys[] = { "opt1", "opt2", "opt3", NULL };

    gboolean res = TRUE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_null(options);
    assert_false(res);

    options_destroy(options);
}

void
parse_options_when_three_returns_map(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt3", "val3", NULL };
    gchar *keys[] = { "opt1", "opt2", "opt3", NULL };

    gboolean res = FALSE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_int_equal(3, g_hash_table_size(options));
    assert_true(g_hash_table_contains(options, "opt1"));
    assert_true(g_hash_table_contains(options, "opt2"));
    assert_true(g_hash_table_contains(options, "opt3"));
    assert_string_equal("val1", g_hash_table_lookup(options, "opt1"));
    assert_string_equal("val2", g_hash_table_lookup(options, "opt2"));
    assert_string_equal("val3", g_hash_table_lookup(options, "opt3"));
    assert_true(res);

    options_destroy(options);
}

void
parse_options_when_unknown_opt_sets_error(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "oops", "val2", "opt3", "val3", NULL };
    gchar *keys[] = { "opt1", "opt2", "opt3", NULL };

    gboolean res = TRUE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_null(options);
    assert_false(res);

    options_destroy(options);
}

void
parse_options_with_duplicated_option_sets_error(void **state)
{
    gchar *args[] = { "cmd1", "cmd2", "opt1", "val1", "opt2", "val2", "opt1", "val3", NULL };
    gchar *keys[] = { "opt1", "opt2", "opt3", NULL };

    gboolean res = TRUE;

    GHashTable *options = parse_options(&args[2], keys, &res);

    assert_null(options);
    assert_false(res);

    options_destroy(options);
}