4

Desired behaviour

While editing code blocks with org-mode by pressing C-c ' I'd like Flycheck to resolve relevant configuration the same way it does for normal files. I'm using JavaScript, and these are .jshintrc files within the same ancestry.

To be more specific, I'd like the .jshintrc settings to be picked up while editing code if I had my project arranged like this:

~/project/file.org
~/project/.jshintrc

This doesn't work, but is my ideal behavior.

Lame solution

The following setup works:

~/project/file.org
~/.jshintrc

But it's not desirable because it sets defaults across all my projects.

Possible solutions

I'm not sure if the following solutions are good ideas, and I'm unsure how implement them:

  • Set buffer-file-name of local buffer when editing source code blocks in org-mode.

  • Add another function to flycheck-locate-config-file-functions.

Rationale

Looking at the Flycheck source, my "lame solution" works because Flycheck looks at the home directory in case other attempts to look up the configuration fail. This behavior is defined by flycheck-locate-config-file-functions.

flycheck-locate-config-file-ancestor-directories looks like the function I'd like to succeed in finding the configuration file. It looks like this:

(defun flycheck-locate-config-file-ancestor-directories (filename _checker)
  "Locate a configuration FILENAME in ancestor directories.

If the current buffer has a file name, search FILENAME in the
directory of the current buffer and all ancestors thereof (see
`locate-dominating-file').  If the file is found, return its
absolute path.  Otherwise return nil.

_CHECKER is ignored."
  (-when-let* ((basefile (buffer-file-name))
               (directory (locate-dominating-file basefile filename)))
    (expand-file-name filename directory)))

So, if buffer-file-name is set to an appropriate value we should be good. So what is its value for my code buffer? nil. OK, so what are the effects of setting this variable locally for Org code buffers (and how do I do this whenever I enter from org-mode with C-c ')?

Also, if I add my own look-up function to flycheck-locate-config-file-functions, that may solve the problem. But how would I resolve the directory of the current .org file from the buffer that lets me edit the JavaScript source?

A simpler solution is welcome as well.

1 Answer 1

8

Here's how you can tell org-mode to set buffer-file-name to an appropriate value for code buffers:

(defadvice org-edit-src-code (around set-buffer-file-name activate compile)
  (let ((file-name (buffer-file-name))) ;; (1)
    ad-do-it                            ;; (2)
    (setq buffer-file-name file-name))) ;; (3)

Explanation

C-c ' is bound to org-edit-special. According to the documentation (C-h f org-edit-special RET), this command calls org-edit-src-code when point is in a code block. So in order to achieve what you want, you need to advise org-edit-src-code to make it set buffer-file-name to the name of the .org file that contains the source block you will be editing. Steps:

  1. Cache name of .org file containing source block to edit.
  2. Call original function (org-edit-src-code).
  3. Set buffer-file-name to cached file name for code buffer.

Based on the research you posted as part of your question, I don't think you need to do anything else to enable Flycheck for code buffers.

2
  • Another solution would be to set buffer-file-name in a function of the org-src-mode-hook... if org-edit-src-code wasn't deliberately setting it to nil after calling org-src-mode. Do you know the rationale behind org-src's conviction that buffer-file-name should absolutely be nil?
    – T. Verron
    Commented Aug 4, 2015 at 18:14
  • @T.Verron I have no idea... not sure if I even looked at the source to come up with this answer :)
    – itsjeyd
    Commented Aug 14, 2015 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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