Skip to content

Instantly share code, notes, and snippets.

@t1anchen
Created January 17, 2024 17:22
Show Gist options
  • Save t1anchen/581cca8b68a1c368a291b9bd2156d0a6 to your computer and use it in GitHub Desktop.
Save t1anchen/581cca8b68a1c368a291b9bd2156d0a6 to your computer and use it in GitHub Desktop.
add your own include path to clangd in a dynamic way
;; when clangd is launched, add ./include to CPATH for include path
;; search so that eglot can do completion and definition navigation
;;
;; See https://clang.llvm.org/docs/CommandGuide/clang.html#envvar-CPATH
;; for details
(let
((current-proj-incl (file-name-concat (projectile-project-root ".") "include")))
(utils:env-append "CPATH" current-proj-incl))
(defun utils:emptyp (x)
"predicate `x' is empty"
(cond ((sequencep x) (= (length x) 0))
((numberp x) (= x 0))
(t (equal x nil))))
(defun utils:path-expand (x)
"expand any user or env vars in the path `x'"
(expand-file-name (substitute-env-in-file-name x)))
(defun utils:env-to-str (x)
"return a string from env var `x'. If `x' is empty, it always
return a empty-value string"
(let ((env-content (getenv x)))
(cond ((utils:emptyp env-content) "")
((stringp env-content) env-content)
((numberp env-content) (number-to-string env-content))
(t (string x)))))
(defun utils:str-to-list (x sep)
"return a list from string `x' with separated by `sep'"
(cond ((and (stringp x) (stringp sep)) (split-string x (path-separator)))
(t nil)))
(defun utils:env-to-list (x)
"return a list from env var `x' with separated by the value
of `(path-separator)'"
(let ((pathstr (utils:env-to-str x))
(pathsep (path-separator)))
(utils:str-to-list pathstr pathsep)))
(defun utils:env-from-list (x newpaths)
"set env var `x' with string-joined content from the list
`newpaths'. All empty paths would be skipped."
(when (not (utils:emptyp newpaths))
(let ((newpaths-str (string-join (seq-remove #'utils:emptyp newpaths) (path-separator))))
(setenv x newpaths-str)
(getenv x))))
(defun utils:env-append (x &rest newpaths)
"append new paths to env var `x'. All empty paths would be skipped."
(let ((paths (utils:env-to-list x)))
(dolist (newpath-vert newpaths)
(let ((newpath (utils:path-expand newpath-vert)))
(if (and (not (member newpath paths))
(not (utils:emptyp newpath)))
(push newpath paths))))
(utils:env-from-list x paths)))
(defun utils:show-list-in-current-buffer (list)
(dolist (item list)
(insert (format "%s\n" item)))
(pop-to-buffer (current-buffer)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment