2

Im writing an lsp-mode purescript client and couldn't figure out how to override the default config options using a .dir-locals file.

For example, I have a defcustom :

defcustom lsp-purescript-use-npx t                                                                                                                       
  "Whether to execute using npx or a globally installed version. Defaults to Npx"                                             
  :type '(choice (const :tag "Npx" t)                                                                                                                    
                  (const :tag "Global" nil))                                                                                                              
  :group 'lsp-purescript)

The problem I'm essentially trying to solve is how to set a variable for a minor mode. I'd like to set the value of lsp-purescript-use-npx within dir-locals but the docs ive seen only show how to set major mode variables

Any help and/or urls for documentation?

  • 1
    You can use M-x add-dir-local-variable to edit the file for you. – phils Feb 2 at 19:55
  • 1
    emacswiki.org/emacs/DirectoryVariables and gnu.org/software/emacs/manual/html_node/emacs/… should have been found pretty easily when searching; were you looking for something different? – phils Feb 2 at 20:02
  • @phils The issue im having is that the add-dir-local-variable command seems to deal with setting variables for major mode configuration - I'm trying to set a value for a minor mode. The UI isn't showing me anything relevant to set the variable I want. I've checked out those urls too but didn't find anything pertaining to minor mode configs – Vance Palacio Feb 3 at 1:58
  • Ah, I see. That isn't supported, but most likely you just want to set the variable for all major modes, which you do by specifying nil as the mode value. – phils Feb 3 at 2:29
  • If you need to use different values of lsp-purescript-use-npx depending on whether a particular minor mode is enabled or disabled, then please update the question and detail exactly what the required behaviour is. – phils Feb 3 at 3:20
5

As @phils suggested you should be able to run M-x add-dir-local-variable. You will be prompted for a mode, and you could type/select lsp-mode (I am guessing that is the mode name). Then, you will be prompted for a variable, and you could type/select lsp-purescript-use-npx. Finally, you should be prompted for the value. Depending on your setup, you may or may not get completion, and you may need to press tab to see completion.

Alternatively, type this command in a file that is in the directory you want to set the dir-local variable:

(add-dir-local-variable 'lsp-mode 'lsp-purescript-use-npx "Global") then highlight the code and run M-x eval-region. You can delete this line after you run it.

That should create a .dir-locals.el file with these contents:

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((lsp-mode
  (lsp-purescript-use-npx . "Global")))

You can also create .dir-locals.el directly, and copy that content into it.

| improve this answer | |
1

The custom variables defined by major and minor modes are all available for setting within dir-locals. You could set lsp-purescript-use-npx to nil like so:

((purescript-mode                                                                                                                                         
  (lsp-purescript-use-npx)))

Line 1 ((purescript-mode says - Only set the following variables for the major mode "purescript-mode".

The 2nd line sets the custom variable lsp-purescript-use-npx to nil. You don't need to reference the minor mode in any way to set this, and you don't need to use any major mode in particular either.

To do this using the add-dir-local-variable command, it would look like this:

  1. M-x add-dir-local-variable
  2. Type the major mode you want this to apply to (or enter nil to apply to all major modes)
  3. Type the variable you want to set (from whatever major or minor mode you want)
  4. Type the value you want it to have (or leave blank for nil)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.