Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. ;; Modify company so that tab and S-tab cycle through completions without
  2. ;; needing to hit enter.
  3.  
  4. (defvar-local company-simple-complete--previous-prefix nil)
  5. (defvar-local company-simple-complete--before-complete-point nil)
  6.  
  7. (defun company-simple-complete-frontend (command)
  8. (when (or (eq command 'show)
  9. (and (eq command 'update)
  10. (not (equal company-prefix company-simple-complete--previous-prefix))))
  11. (setq company-selection -1
  12. company-simple-complete--previous-prefix company-prefix
  13. company-simple-complete--before-complete-point nil)))
  14.  
  15. (defun company-simple-complete-next (&optional arg)
  16. (interactive "p")
  17. (company-select-next arg)
  18. (company-simple-complete//complete-selection-and-stay))
  19.  
  20. (defun company-simple-complete-previous (&optional arg)
  21. (interactive "p")
  22. (company-select-previous arg)
  23. (company-simple-complete//complete-selection-and-stay))
  24.  
  25. (defun company-simple-complete//complete-selection-and-stay ()
  26. (if (cdr company-candidates)
  27. (when (company-manual-begin)
  28. (when company-simple-complete--before-complete-point
  29. (delete-region company-simple-complete--before-complete-point (point)))
  30. (setq company-simple-complete--before-complete-point (point))
  31. (unless (eq company-selection -1)
  32. (company--insert-candidate (nth company-selection company-candidates)))
  33. (company-call-frontends 'update)
  34. (company-call-frontends 'post-command)
  35. (company-call-backend 'post-completion (nth company-selection company-candidates)))
  36. (company-complete-selection)))
  37.  
  38. (defadvice company-set-selection (around allow-no-selection (selection &optional force-update))
  39. "Allow selection to be -1"
  40. (setq selection
  41. ;; TODO deal w/ wrap-around
  42. (if company-selection-wrap-around
  43. (mod selection company-candidates-length)
  44. (max -1 (min (1- company-candidates-length) selection))))
  45. (when (or force-update (not (equal selection company-selection)))
  46. (setq company-selection selection
  47. company-selection-changed t)
  48. (company-call-frontends 'update)))
  49.  
  50. (defadvice company-tooltip--lines-update-offset (before allow-no-selection (selection _num-lines _limit))
  51. "Allow selection to be -1"
  52. (when (eq selection -1)
  53. (ad-set-arg 0 0)))
  54.  
  55. (defadvice company-tooltip--simple-update-offset (before allow-no-selection (selection _num-lines limit))
  56. "Allow selection to be -1"
  57. (when (eq selection -1)
  58. (ad-set-arg 0 0)))
  59.  
  60. (with-eval-after-load 'company
  61. (define-key company-active-map [tab] 'company-simple-complete-next)
  62. (define-key company-active-map (kbd "TAB") 'company-simple-complete-next)
  63. (define-key company-active-map (kbd "<S-tab>") 'company-simple-complete-previous)
  64. (define-key company-active-map (kbd "RET") nil)
  65. (define-key company-active-map (kbd "<return>") nil)
  66.  
  67. (put 'company-simple-complete-next 'company-keep t)
  68. (put 'company-simple-complete-previous 'company-keep t)
  69. (setq company-require-match nil)
  70. (ad-activate 'company-set-selection)
  71. (ad-activate 'company-tooltip--simple-update-offset)
  72. (ad-activate 'company-tooltip--lines-update-offset)
  73. (add-to-list 'company-frontends 'company-simple-complete-frontend))
  74.  
  75. (provide 'company-simple-complete)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement