Created
April 2, 2022 22:25
-
-
Save amirrajan/efd8f691f34217e1b0d0bc7feb9c27c9 to your computer and use it in GitHub Desktop.
Emacs add_advice for company-mode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(use-package company | |
:ensure t | |
:config | |
(defun amir-company-set-selection-callback (orig-fun &rest args) | |
(setq amir-company-something-selected 't) | |
(let ((res (apply orig-fun args))) | |
res)) | |
(advice-add 'company-set-selection :around #'amir-company-set-selection-callback) | |
(defun amir-company--begin-new-callback (orig-fun &rest args) | |
(setq amir-company-something-selected nil) | |
(let ((res (apply orig-fun args))) | |
res)) | |
(advice-add 'company--begin-new :around #'amir-company--begin-new-callback) | |
(defun amir-company-complete-selection-callback (orig-fun &rest args) | |
(setq amir-company-something-selected nil) | |
(let ((res (apply orig-fun args))) | |
res)) | |
(advice-add 'company-complete-selection :around #'amir-company-complete-selection-callback) | |
(add-hook 'after-init-hook 'global-company-mode) | |
(add-hook 'org-mode-hook (lambda () (company-mode 0))) | |
;;dont auto complete numbers or things with special characters in it | |
(push (apply-partially | |
#'cl-remove-if | |
(lambda (c) | |
(or (string-match-p "[^\x00-\x7F]+" c) | |
(string-match-p "[0-9]+" c) | |
(if (equal major-mode "org") | |
(>= (length c) 15))))) | |
company-transformers) | |
(setq company-idle-delay 0.03) | |
(setq company-minimum-prefix-length 2) | |
(setq company-show-numbers 't) | |
(setq gc-cons-threshold 20000000) | |
;; amir-company-complete-and-insert | |
(defun cci (string) | |
(interactive) | |
(if amir-company-something-selected | |
(call-interactively 'company-complete-selection) | |
(progn | |
(company-select-next) | |
(call-interactively 'company-complete-selection))) | |
(insert string)) | |
(defun amir-company-complete-and-insert (text) | |
(interactive) | |
(progn | |
(company-complete-selection) | |
(insert text))) | |
(defun amir-company-complete () | |
(interactive) | |
(company-complete-selection)) | |
(defun amir-company-complete-ret () | |
(interactive) | |
(company-complete-selection) | |
(newline-and-indent)) | |
(defun amir-company-complete-equal-sign () | |
(interactive) | |
(cci " = ")) | |
(defun amir-company-complete-equal-&& () | |
(interactive) | |
(cci " && ")) | |
(defun amir-company-complete-equal-template () | |
(interactive) | |
(cci "<")) | |
(defun amir-company-complete-equal-|| () | |
(interactive) | |
(cci " || ")) | |
(defun amir-company-complete-paren () | |
(interactive) | |
(cci "(")) | |
(defun amir-company-complete-end-paren () | |
(interactive) | |
(cci ")")) | |
(defun amir-company-complete-bracket () | |
(interactive) | |
(cci "[")) | |
(defun amir-company-complete-end-bracket () | |
(interactive) | |
(cci "]")) | |
(defun amir-company-complete-end-bracket-equal-sign () | |
(interactive) | |
(cci "] = ")) | |
(defun amir-company-complete-end-bracket-gt () | |
(interactive) | |
(cci "] > ")) | |
(defun amir-company-complete-end-bracket-lt () | |
(interactive) | |
(cci "] < ")) | |
(defun amir-company-complete-gt () | |
(interactive) | |
(cci " > ")) | |
(defun amir-company-complete-lt () | |
(interactive) | |
(cci " < ")) | |
(defun amir-company-complete-. () | |
(interactive) | |
(cci ".")) | |
(defun amir-company-complete-end-bracket-. () | |
(interactive) | |
(cci "].")) | |
(defun amir-company-complete-: () | |
(interactive) | |
(cci ": ")) | |
(defun amir-company-complete-comma () | |
(interactive) | |
(cci ", ")) | |
(defun amir-company-complete-end-curly () | |
(interactive) | |
(cci "}")) | |
(defun amir-company-complete-right () | |
(interactive) | |
(progn | |
(company-complete-selection) | |
(right-char))) | |
(defun amir-company-complete-space () | |
(interactive) | |
(cci " ")) | |
(defun amir-company-select-first-match () | |
(interactive) | |
(progn | |
(company-select-next) | |
(company-complete-selection))) | |
;; Make autocomplete insert text if code based keys are pressed | |
(eval-after-load 'company | |
'(progn | |
(define-key company-active-map (kbd "C-k") nil) | |
(define-key company-active-map (kbd "RET") 'amir-company-complete) | |
(define-key company-active-map (kbd ".") 'amir-company-complete-.) | |
(define-key company-active-map (kbd "(") 'amir-company-complete-paren) | |
(define-key company-active-map (kbd ")") 'amir-company-complete-end-paren)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment