admin管理员组

文章数量:1023803

While using project in Emacs I need to specify a large set of include directories for flycheck syntax checker. I use C++ on Linux and I mention the include/ dirs in .dir-locals.el.

The format used is something like:

((c++-mode . ((flycheck-checker . c/c++-gcc)
     (flycheck-gcc-include-path . (
        "/my/project/root1/include1"
        "/my/project/root1/include2"
        "/my/project/root1/include3"
        "/my/project/root1/include4"
        "/my/project/root1/include5"
        "/my/project/root1/include6"
))
    
    (flycheck-gcc-definitions . ("<Some Macro >")
              )
     )))

The problem with above approach is if I have several copies of the source code (meaning different project root areas) for doing different tasks I need to write the above for each such copy which requires management each time some include/ directory is added/deleted.

Is there a way to parametrize /my/project/root in some variable & define the variable once only instead of repeating in each include/ directory? That way each project root copies will have 1 such defn of the variable which will point to the current root & the rest would be identical. (I can then maintain a single copy of 99% of the .dir-local.el file & all source areas will use the copy & override the variable for project root on their own.)

Maybe there is a simple way of doing this, but as I am new to emacs I can't find it.

While using project in Emacs I need to specify a large set of include directories for flycheck syntax checker. I use C++ on Linux and I mention the include/ dirs in .dir-locals.el.

The format used is something like:

((c++-mode . ((flycheck-checker . c/c++-gcc)
     (flycheck-gcc-include-path . (
        "/my/project/root1/include1"
        "/my/project/root1/include2"
        "/my/project/root1/include3"
        "/my/project/root1/include4"
        "/my/project/root1/include5"
        "/my/project/root1/include6"
))
    
    (flycheck-gcc-definitions . ("<Some Macro >")
              )
     )))

The problem with above approach is if I have several copies of the source code (meaning different project root areas) for doing different tasks I need to write the above for each such copy which requires management each time some include/ directory is added/deleted.

Is there a way to parametrize /my/project/root in some variable & define the variable once only instead of repeating in each include/ directory? That way each project root copies will have 1 such defn of the variable which will point to the current root & the rest would be identical. (I can then maintain a single copy of 99% of the .dir-local.el file & all source areas will use the copy & override the variable for project root on their own.)

Maybe there is a simple way of doing this, but as I am new to emacs I can't find it.

Share Improve this question asked Nov 19, 2024 at 8:30 user23517194user23517194 12 bronze badges 1
  • This? First (setq myroot1 "/my/project/root1/"), then either of (concat myroot1 "include1") or (expand-file-name "include1" myroot1). – Erwann Commented Nov 19, 2024 at 17:31
Add a comment  | 

2 Answers 2

Reset to default 0

Like you, I'm also new to emacs, but I believe you can define a custom variable in your .dir-locals.el file to hold the project root path. Here's an example:

((nil . ((my-project-root . "/my/project/root1"))))

You can then use this variable to define your flycheck-gcc-include-path like so:

((c++-mode
  . ((flycheck-checker . c/c++-gcc)
     (flycheck-gcc-include-path
      . ((lambda ()
           (mapcar (lambda (dir) (concat my-project-root dir))
                   '("/include1" "/include2" "/include3" "/include4" "/include5" "/include6")))))
     (flycheck-gcc-definitions . ("<Some Macro>")))))

For other root projects, simply change the path like this:

((nil . ((my-project-root . "/my/project/root2"))))

Remember, documentation is your best friend! Here's a helpful link where you can learn more about directory-local variables and related solutions: Emacs Lisp Reference Manual on Directory Local Variables.

Assuming your project is under version control, there's a function "project-root" that will find your project's root folder. The following will produce a list of include files like you showed above:

(require 'project)
(mapcar (lambda (n)
           (format "%s/include%d" (project-root (project-current)) n))
        (number-sequence 1 8))

While using project in Emacs I need to specify a large set of include directories for flycheck syntax checker. I use C++ on Linux and I mention the include/ dirs in .dir-locals.el.

The format used is something like:

((c++-mode . ((flycheck-checker . c/c++-gcc)
     (flycheck-gcc-include-path . (
        "/my/project/root1/include1"
        "/my/project/root1/include2"
        "/my/project/root1/include3"
        "/my/project/root1/include4"
        "/my/project/root1/include5"
        "/my/project/root1/include6"
))
    
    (flycheck-gcc-definitions . ("<Some Macro >")
              )
     )))

The problem with above approach is if I have several copies of the source code (meaning different project root areas) for doing different tasks I need to write the above for each such copy which requires management each time some include/ directory is added/deleted.

Is there a way to parametrize /my/project/root in some variable & define the variable once only instead of repeating in each include/ directory? That way each project root copies will have 1 such defn of the variable which will point to the current root & the rest would be identical. (I can then maintain a single copy of 99% of the .dir-local.el file & all source areas will use the copy & override the variable for project root on their own.)

Maybe there is a simple way of doing this, but as I am new to emacs I can't find it.

While using project in Emacs I need to specify a large set of include directories for flycheck syntax checker. I use C++ on Linux and I mention the include/ dirs in .dir-locals.el.

The format used is something like:

((c++-mode . ((flycheck-checker . c/c++-gcc)
     (flycheck-gcc-include-path . (
        "/my/project/root1/include1"
        "/my/project/root1/include2"
        "/my/project/root1/include3"
        "/my/project/root1/include4"
        "/my/project/root1/include5"
        "/my/project/root1/include6"
))
    
    (flycheck-gcc-definitions . ("<Some Macro >")
              )
     )))

The problem with above approach is if I have several copies of the source code (meaning different project root areas) for doing different tasks I need to write the above for each such copy which requires management each time some include/ directory is added/deleted.

Is there a way to parametrize /my/project/root in some variable & define the variable once only instead of repeating in each include/ directory? That way each project root copies will have 1 such defn of the variable which will point to the current root & the rest would be identical. (I can then maintain a single copy of 99% of the .dir-local.el file & all source areas will use the copy & override the variable for project root on their own.)

Maybe there is a simple way of doing this, but as I am new to emacs I can't find it.

Share Improve this question asked Nov 19, 2024 at 8:30 user23517194user23517194 12 bronze badges 1
  • This? First (setq myroot1 "/my/project/root1/"), then either of (concat myroot1 "include1") or (expand-file-name "include1" myroot1). – Erwann Commented Nov 19, 2024 at 17:31
Add a comment  | 

2 Answers 2

Reset to default 0

Like you, I'm also new to emacs, but I believe you can define a custom variable in your .dir-locals.el file to hold the project root path. Here's an example:

((nil . ((my-project-root . "/my/project/root1"))))

You can then use this variable to define your flycheck-gcc-include-path like so:

((c++-mode
  . ((flycheck-checker . c/c++-gcc)
     (flycheck-gcc-include-path
      . ((lambda ()
           (mapcar (lambda (dir) (concat my-project-root dir))
                   '("/include1" "/include2" "/include3" "/include4" "/include5" "/include6")))))
     (flycheck-gcc-definitions . ("<Some Macro>")))))

For other root projects, simply change the path like this:

((nil . ((my-project-root . "/my/project/root2"))))

Remember, documentation is your best friend! Here's a helpful link where you can learn more about directory-local variables and related solutions: Emacs Lisp Reference Manual on Directory Local Variables.

Assuming your project is under version control, there's a function "project-root" that will find your project's root folder. The following will produce a list of include files like you showed above:

(require 'project)
(mapcar (lambda (n)
           (format "%s/include%d" (project-root (project-current)) n))
        (number-sequence 1 8))

本文标签: flycheckParametrize project root in emacs projectStack Overflow