Advertisement
Guest User

Untitled

a guest
Jun 28th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.44 KB | None | 0 0
  1. ;;; company-auto-expand-mode.el --- auto expand company-mode selection candidate
  2.  
  3. ;; Author: Yu Guang
  4.  
  5. ;;; Commentary:
  6. ;;
  7. ;; The core code is very simple; just shrink company candidate brefore every command
  8. ;; and expand company candidate again after every command;
  9. ;; dispaly at here maybe useful for someone.
  10.  
  11. ;; (defun company-auto-expand-frontend (command)
  12. ;; (cl-case command
  13. ;; (pre-command
  14. ;; (delete-region company-point (point))
  15. ;; (when (eq this-command 'self-insert-command)
  16. ;; (company-complete-selection)))
  17. ;; (post-command
  18. ;; (company--insert-candidate
  19. ;; (nth company-selection company-candidates)))))
  20.  
  21. ;; Have a minor bug when work together with company-pseudo-tooltip-unless-just-one-frontend-with-delay
  22. ;; you could use (if (and (not (overlayp company-preview-overlay)) (not company--auto-expand-inline)))
  23. ;; replace this line (if (not (overlayp company-preview-overlay)) in company-pseudo-....-with-delay.
  24.  
  25.  
  26. ;;; code:
  27.  
  28. (require 'company)
  29.  
  30. (defgroup company-auto-expand-mode nil
  31. "auto expand company-mode selection candidate"
  32. :group 'company)
  33.  
  34. (defcustom company-auto-expand-navigate-command
  35. 'company-select-next
  36. "which command will active company-auto-expand"
  37. :type 'symbol
  38. :group 'company-auto-expand-mode)
  39.  
  40.  
  41. ;; ---------------------------------------------------------------------------------------------------
  42.  
  43. (defvar-local company-auto-expand-active nil)
  44. (defvar-local company--auto-expand-inline nil)
  45.  
  46. (defun company-auto-expand-frontend (command)
  47. (when company-auto-expand-active
  48. (cl-case command
  49. (pre-command
  50. (company--shrink-selection)
  51. (when (and this-command
  52. (company-auto-expand-finish-commmand this-command))
  53. (company-complete-selection)))
  54. (post-command
  55. (company--expand-selection))
  56. (hide
  57. (setq-local company-auto-expand-active nil)))))
  58.  
  59. (defun company--expand-selection ()
  60. (unless company--auto-expand-inline
  61. (let (company-auto-expand-active
  62. (company--candidate (nth company-selection
  63. company-candidates)))
  64. (company-call-frontends 'pre-command)
  65. (company--insert-candidate company--candidate)
  66. (let ((company-prefix (substring-no-properties
  67. company--candidate)))
  68. (company-call-frontends 'post-command)))
  69. (when (overlayp company-preview-overlay)
  70. (company-preview-hide))
  71. (setq-local company--auto-expand-inline t)))
  72.  
  73. (defun company--shrink-selection ()
  74. (when company--auto-expand-inline
  75. (delete-region company-point (point))
  76. (setq-local company--auto-expand-inline nil)))
  77.  
  78. ;; ----------------------
  79. (defun company-auto-expand-finish-commmand (command)
  80. (or (eq command 'self-insert-command)
  81. (not (lookup-command-in-keymap-p command company-active-map))))
  82. ;; TODO: more efficient
  83. (defun lookup-command-in-keymap-p (command keymap)
  84. (when (equal command
  85. (catch 'lookup-p
  86. (do-lookup-command-in-keymap-p command keymap)))
  87. t))
  88.  
  89. (defun do-lookup-command-in-keymap-p (command keymap)
  90. (when (consp keymap)
  91. (let ((sub-keymap (cdr keymap)))
  92. (if (eq command sub-keymap)
  93. (throw 'lookup-p command)
  94. (when (listp sub-keymap)
  95. (mapc (lambda (ssub-keymap)
  96. (do-lookup-command-in-keymap-p command ssub-keymap))
  97. sub-keymap))))))
  98.  
  99. ;; ---------------------------------------------------------------------------------------------------
  100. ;; TODO: shoud more common
  101. (defun company-auto-expand-navigate-advice (orig-command &optional arg)
  102. (interactive "p")
  103. (if company-auto-expand-active
  104. (let ((current-prefix-arg arg))
  105. (call-interactively orig-command))
  106. (setq-local company-auto-expand-active t)))
  107.  
  108.  
  109. ;;;###autoload
  110. (define-minor-mode company-auto-expand-mode
  111. "A minor mode for company-mode, turn on will auto expand selection candidate.
  112. Usage: add company-auto-expand-frontend to company-frontends (should place behind
  113. pseudo-tooltip and preview) and turn on this minor mode when use company-mode."
  114. nil nil nil
  115. :global t
  116. (if company-auto-expand-mode
  117. (if company-mode
  118. (advice-add company-auto-expand-navigate-command
  119. :around #'company-auto-expand-navigate-advice)
  120. (message "Only Turn-on company-auto-expand-mode when company-mode ON")
  121. (company-auto-expand-mode -1))
  122. (when (advice-member-p #'company-auto-expand-navigate-advice
  123. company-auto-expand-navigate-command)
  124. (advice-remove company-auto-expand-navigate-command
  125. #'company-auto-expand-navigate-advice))))
  126.  
  127.  
  128. (provide 'company-auto-expand-mode)
  129. ;;; company-auto-expand-mode.el ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement