I have written a small function to search MDN docs and display them on the browser.

However, sometimes the function does not work and an empty query is made.

My code is as follows:

(defun search-mdn (&optional querystring)                                       
  (if (not querystring)                                                         
      (search-mdn (read-string "What do you want to look up on MDN? "))         
  (let* ( (header "https://developer.mozilla.org/en-US/search?q=")              
         (footer "&topic=api&topic=css&topic=canvas&topic=html&topic=http&topic\
         (searchurl (concat header querystring footer)))                        
    (browse-url searchurl)                                                      

Every time I call the function without parameters (search-mdn (read-string "What ...")) runs, however sometimes it does not call the search-mdn function with a parameter, although I enter one.

  • You should just build the list of arguments in body of interactive rather than calling the same function, because it will return, and then the first call will continune (with querystring set to nil.) – user12563 Nov 20 '17 at 15:35

What @DoMiNeLa10 says is that you should have written:

(defun search-mdn (querystring)                                       
   (list (read-string "What do you want to look up on MDN? ")))
  (let* ((header "https://developer.mozilla.org/en-US/search?q=")
         (footer "&topic=api&topic=css&topic=canvas&topic=html&topic=http&topic\
         (searchurl (concat header querystring footer)))
    (browse-url searchurl)))

Now the reason why this fixes your problem is not because using interactive magically fixes the problem: the root of your problem was simply that the (let ...) part of the code was written after the if rather than within the "else" part of the if, so when querystring is nil, your code first calls itself recursively with a valid string, and then continues execution "again" with the nil value.

| improve this answer | |
  • Thank you, I didn't know how interactive worked. And I couldn't foresee that the execution would continue after making another function call. I see it now. – terminalcommand Nov 20 '17 at 19:00

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.