1

I want to write some lisp that will take the cwd, and keep traversing up the dirctory tree until it finds a file (in this case config.rb) then when it finds it I want to parse the contents (in this case:

require 'compass/import-once/activate'                                                                                                                     
# Require any additional compass plugins here.                                                                                                             

# Set this to the root of your project when deployed:                                                                                                      
http_path = "/"                                                                                                                                           
css_dir = "stylesheets"                                                                                                                                   
sass_dir = "sass"                                                                                                                                         
images_dir = "images"                                                                                                                                     
javascripts_dir = "javascripts"

for example to extract the value for sass_dir

Then I want to navigate back into that directory.

This is all because the scss compile mode doesnt work as it tries to compile in the CWD of the buffer which is not how compass works.

I've been wanting to develop my lisp skills anyhow so learning the above techniques would be great if anyone can help.

Many thanks.


I have done:

(defcustom scss-sass-command "compass compile"
"Command used to compile SCSS files, should be sass or the complete path to your sass runnable example:"
 :group 'scss)

(defun get-compass-root-dir()
    "Get the root folder for the sass project to run compass command in"
    (locate-dominating-file buffer-file-name "config.rb"))

(defun scss-compile()
    "Compiles the directory belonging to the current buffer, using the option"
      (interactive)
      (message (get-compass-root-dir))
      (let ((default-directory (get-compass-root-dir))))
      (message (pwd))
      (compile scss-sass-command))

But I just dont seem to be nailing it. Compass complains there are no files to compile and my attempts to message out the directories to see what is going on dont seem to be working. I have byte compiled the file and relaunced emacs.

Anybody have any suggestions?

  • 1
    Use locate-dominating-file for the first criteria. Parsing a file depends on exactly what you mean by that, but you would probably get away with searching for a regexp such as "^sass_dir *= *\"\\(.+?\\)\"" – phils Oct 28 '15 at 12:47
  • Sweet, thanks for the reply. Is there any reason why I can't find locate-dominating-file for the command mini buffer (e.g esc-x locate-dominating-file) I also had this issue looking for dired-tree-up. When I autocomplete from esc-x locate I get: Possible completions are: local-set-key, local-unset-key, locate, locate-library, locate-with-filter – user2237076 Oct 28 '15 at 12:58
  • looks like it got dropped and needs importing back? github.com/tcrayford/emacs/blob/master/dominating-file.el – user2237076 Oct 28 '15 at 12:59
  • also found this with different search terms stackoverflow.com/questions/14095189/walk-up-the-directory-tree (for others looking into this) – user2237076 Oct 28 '15 at 13:01
  • 1
    user2237076: locate-dominating-file is a function, but not a command. Only commands may be called via M-x. Use C-h f locate-dominating-file RET to view its documentation. – phils Oct 29 '15 at 6:26
2

By modifying the line:

(let ((default-directory (get-compass-root-dir))))

to

(setq default-directory (get-compass-root-dir))

I get the desired result. I picked the 'let' code up from scouring other similar natured questions on the interweb, but in this instance it caused an issue. Dont ask me why - im really shooting in the dark with elisp at the minute! Anyway learning the hard way is never a bad way to learn. I haven't read this yet but the above will be probably be explained by:

https://stackoverflow.com/questions/19067632/difference-between-let-and-setq

Thanks.


With phils's suggestion, the code becomes:

(defcustom scss-sass-command "compass compile"
"Command used to compile SCSS files, should be sass or the complete path to your sass runnable example:"
 :group 'scss)

(defun get-compass-root-dir()
    "Get the root folder for the sass project to run compass command in"
    (locate-dominating-file buffer-file-name "config.rb"))

(defun scss-compile()
    "Compiles the directory belonging to the current buffer, using the option"
      (interactive)
      (message (get-compass-root-dir))
      (let ((default-directory (get-compass-root-dir)))
        (message (pwd))
        (compile scss-sass-command)))
  • 1
    let is used to establish temporary variable bindings for the expressions contained within the let form. Your version contains no expressions, so the temporary scope expires without having had any practical effect. You'll find that you wanted this: (let ((default-directory (get-compass-root-dir))) (compile scss-sass-command)) rather than: (let ((default-directory (get-compass-root-dir)))) (compile scss-sass-command) (which will be considerably more apparent with line breaks and indentation). – phils Oct 29 '15 at 6:20
  • Incidentally, please don't post an answer (let alone multiple) to your own questions if it is not actually an answer. You are supposed to edit your question to add additional information. Showing (in the question) the code you've tried thus far is actively encouraged. – phils Oct 29 '15 at 6:29
  • 1
    Note also that using (setq default-directory (get-compass-root-dir)) is a bad idea here, as it makes a persistent change to that buffer's default-directory value (a value which is used for many things by Emacs). – phils Oct 29 '15 at 11:13
  • Understood - across all aspects - and thanks for taking the time to explain everything - across all apsects! – user2237076 Oct 29 '15 at 13:01

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.