Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using digits and letters to select more company candidates by extending the ora-company-number function. #38

Open
hongyi-zhao opened this issue Oct 5, 2021 · 1 comment

Comments

@hongyi-zhao
Copy link

hongyi-zhao commented Oct 5, 2021

The function ora-company-number enables to select and insert company candidates using digits without modifier. But it can only support 10 candidates, numbered 0-9. I want to extend this feature to support more candidates indexed/identified with a single digit or letter, e.g., 0-9, q, w, e, r, t, by setting:

(setq company-tooltip-limit 16
      company-quick-access-keys '("1" "2" "3" "4" "5" "6" "7" "8" "9" "0" "q" "w" "e" "r" "t" "y"))        

See the following screenshot for the effect. This way, I can select and insert more candidates conveniently.

image

How can the above code be improved to achieve this goal? Any hints will be greatly appreciated.

Regards,
HZ

@hongyi-zhao hongyi-zhao changed the title Using digits and letters to select more company candidates by extending the ora-company-number. Using digits and letters to select more company candidates by extending the ora-company-number function. Oct 5, 2021
@hongyi-zhao
Copy link
Author

hongyi-zhao commented Oct 21, 2021

I came up with the following solution:

(use-package company
   :init
  (defun hz-company-search-toggle ()
    (interactive)
    (if company-search-mode
	(company-search-abort)
      (company-search-candidates)))
   :bind
   (:map company-active-map
	("<tab>" . hz-company-search-toggle)
	("<f1>" . company-search-repeat-forward)
	("<f2>" . company-search-repeat-backward)
	("SPC" . company-complete-selection)
	("<return>" . company-abort)

	:map company-search-map
	("<tab>" . hz-company-search-toggle)
	("<f1>" . company-search-repeat-forward)
	("<f2>" . company-search-repeat-backward)
	("SPC" . company-complete-selection)
	("<return>" . company-abort)
)

  :config
  (setq company-tooltip-limit 10)
 (defun hz-company-complete-number ()
    "Convert the company-quick-access-keys to the candidates' row NUMBER visible on the tooltip,
     and then feed it to `company-complete-number' to quickly select and insert company candidates.
     If the currently entered character is belongs to company-quick-access-keys and a part of the candidate simultaneously,
     append it to the currently entered string to construct new company-prefix."
    (interactive)
    (let* ((k (this-command-keys))
           (re (concat "^" company-prefix k)))

      (if (cl-find-if (lambda (s) (string-match re s))
                      company-candidates)
	  (self-insert-command 1)
	(company-complete-number
	 (cond
	  ((equal k "1") 1)
	  ((equal k "2") 2)
	  ((equal k "3") 3)
	  ((equal k "4") 4)
	  ((equal k "5") 5)
	  ((equal k "6") 6)
	  ((equal k "7") 7)
	  ((equal k "8") 8)
	  ((equal k "9") 9)
	  ((equal k "0") 10)
	  )
	 ))))

  (let ((c-a-map company-active-map)
	(c-s-map company-search-map))
    (mapc (lambda (x)
	    (define-key c-a-map (format "%s" x) #'hz-company-complete-number))
	  '(1 2 3 4 5 6 7 8 9 0))
    (mapc (lambda (x)
	    (define-key c-s-map (format "%s" x) #'hz-company-complete-number))
	  '(1 2 3 4 5 6 7 8 9 0)))
)

See here for the relevant discussion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant