Skip to content

Instantly share code, notes, and snippets.

@helinwang
Last active November 29, 2017 20:04
Show Gist options
  • Save helinwang/a775bd75b35fcfae6859196841f652f6 to your computer and use it in GitHub Desktop.
Save helinwang/a775bd75b35fcfae6859196841f652f6 to your computer and use it in GitHub Desktop.
emacs rtags configuration
(menu-bar-mode -1)
(global-set-key "\M-g" 'goto-line)
(global-set-key "\M-." 'rtags-find-symbol-at-point)
(global-set-key "\M-," 'rtags-location-stack-back)
(global-set-key (kbd "M-]") 'next-error) ; Go to next error (or msg)
(global-set-key (kbd "M-[") 'previous-error) ; Go to previous error or msg
(global-set-key (kbd "M-m") 'rtags-find-references-at-point)
(require 'package)
(setq package-enable-at-startup nil)
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
(package-initialize)
;; Bootstrap `use-package'
(unless (package-installed-p 'use-package)
(package-refresh-contents)
(package-install 'use-package))
;; compile rtags according to https://github.com/Andersbakken/rtags
;; and put https://github.com/Andersbakken/rtags/src/rtags.el into ~/.emacs.d/cpp/
(add-to-list 'load-path "~/.emacs.d/misc/")
(use-package company
:ensure t)
(require 'popwin)
(popwin-mode 1)
(global-set-key (kbd "C-z") popwin:keymap)
(require 'rtags)
(setq rtags-autostart-diagnostics t)
(rtags-diagnostics)
(setq rtags-completions-enabled t)
(push 'company-rtags company-backends)
(global-company-mode)
(define-key c-mode-base-map (kbd "<C-tab>") (function company-complete))
;; eldoc mode for rtags: https://github.com/Andersbakken/rtags/issues/987
(require 'subr-x)
(defun fontify-string (str mode)
"Return STR fontified according to MODE."
(with-temp-buffer
(insert str)
(delay-mode-hooks (funcall mode))
(font-lock-default-function mode)
(font-lock-default-fontify-region
(point-min) (point-max) nil)
(buffer-string)))
(defun rtags-eldoc-function ()
(let ((summary (rtags-get-summary-text)))
(and summary
(fontify-string
(replace-regexp-in-string
"{[^}]*$" ""
(mapconcat
(lambda (str) (if (= 0 (length str)) "//" (string-trim str)))
(split-string summary "\r?\n")
" "))
major-mode))))
(defun rtags-eldoc-mode ()
(interactive)
(setq-local eldoc-documentation-function #'rtags-eldoc-function)
(eldoc-mode 1))
;; add hooks
(add-hook 'c-mode-hook 'rtags-start-process-unless-running)
(add-hook 'c++-mode-hook 'rtags-start-process-unless-running)
(add-hook 'c++-mode-hook 'rtags-start-process-unless-running)
(add-hook 'objc-mode-hook 'rtags-start-process-unless-running)
(add-hook 'c-mode-hook 'rtags-eldoc-mode)
(add-hook 'c++-mode-hook 'rtags-eldoc-mode)
(add-hook 'objc-mode-hook 'rtags-eldoc-mode)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment