1

What I expect is when I type #incl and press tab, it completes #include for me.

However what I currently need to do is: I press tab first, it will pop up a list with possible words. I continue to type until an unique choice and it completes for me. But I feel very inconvenient with this style.

This is my elisp setup now. I am not good at lisp. Please help me to check if there is problem.

(eval-after-load 'company
  '(add-to-list 'company-backends 'company-irony))
(eval-after-load 'company
  '(add-to-list
'company-backends '(company-irony-c-headers company-irony)))
(require 'company)
(require 'cc-mode)
(add-hook 'after-init-hook 'global-company-mode)
(setq company-backends (delete 'company-semantic company-backends))
(define-key c-mode-map  [(tab)] 'company-complete)
(define-key c++-mode-map  [(tab)] 'company-complete) 
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
tom
  • 1,302
  • 1
  • 16
  • 30
  • A minimal working example of **company-irony-c-header** configuration/usage is explained here: https://stackoverflow.com/questions/44959535/company-backends-in-gnu-emacs/44965615#44965615 – Picaud Vincent Sep 21 '17 at 08:53

1 Answers1

2

I struggled to get it to work initially as well. I am not very good with Lisp myself.

First of all I think you will need an extra mode to make #include file works. I think you have company-irony-c-headers file.

Company-c-headers

Company-irony-c-headers

I think company-irony-c-headers should work better with company-irony. But I can't get it to work. I downloaded company-c-headers instead and it worked.

The key part is to configure these modes properly.

I will share my own configuration file here, which works for my Mac OS 10.9.

I stored all my configuration file under ~/.emacs.d/config/

For company, irony, company-irony and company-c-header modes, I configured all of them in a big .el file. You can just put it in your .emacs file which should work anyway.

Here is my file:

;; Define the modes/packages you need
(require 'company)
(require 'irony)
(require 'company-c-headers)
;; Enable company mode globally 
(add-hook 'after-init-hook 'global-company-mode)

;; Here I define a function so it can be called anytime I want to load it
(defun irony-comp-setup-basic()
;; A function to add path to company-c-headers
  (defun company-c-headers-includes ()
;; You just need to modified the path inside the quote to your header files path
    (add-to-list 'company-c-headers-path-system "$ROOTSYS/include")
    )
;; Now call this function so it add your path to company-c-header-path-system
  (company-c-headers-includes)
  ;; Irony-mode configuration
  (add-hook 'c++-mode-hook 'irony-mode)
  (add-hook 'c-mode-hook 'irony-mode)
  (add-hook 'objc-mode-hook 'irony-mode)

  ;; replace the `completion-at-point' and `complete-symbol' bindings in
  ;; irony-mode's buffers by irony-mode's function
  (defun my-irony-mode-hook ()
    (define-key irony-mode-map [remap completion-at-point]
      'irony-completion-at-point-async)
    (define-key irony-mode-map [remap complete-symbol]
      'irony-completion-at-point-async))
  (add-hook 'irony-mode-hook 'my-irony-mode-hook)
;; This customize some backends to the company-backends I took it from my friend's code
  (custom-set-variables
   '(company-backends (quote (company-irony company-elisp company-bbdb company-nxml company-css company-eclim company-semantic company-\
clang company-xcode company-ropemacs company-cmake company-capf (company-dabbrev-code company-gtags company-etags company-keywords) com\
pany-oddmuse company-files company-dabbrev))))

  (custom-set-faces
   )

;; This add your company-c-headers to company-backends
  (add-to-list 'company-backends 'company-c-headers)

;; Default config for company-irony mode
  (eval-after-load 'company
    '(add-to-list
      'company-backends 'company-irony))

;; For irony mode I think
  (add-hook 'irony-mode-hook 'company-irony-setup-begin-commands)
)
;; Now call this function to active it
(irony-comp-setup-basic)

Let me know if it's working or not.