0

I am trying to write a minor mode that upon visiting a word, it manipulate it. There is a list of words which is parsed from a file. Here is the problem:

(define-minor-mode auto-gls-mode
  "Documentation goes here"
  :lighter " AGLS"
  ;(setq agls-list '("foo" "bar"))    <----------works
  (setq agls-list (agls-parse-acn-file)) <-------doesn't work
  (if auto-gls-mode
      (add-hook 'post-self-insert-hook
        'agls-check-word nil t)
    (remove-hook 'post-self-insert-hook
         'agls-check-word t)))

and the hooked function:

(defun agls-check-word ()
  "Check if the word is in the list"
  (if (= last-command-event 32)
      (do-some-stuff)))   

The agls-parse-acn-file is pretty long but in pseudo-elisp reads like this:

(defun agls-parse-acn-file()
  ;find-file-noselect
  ;while new-line
    ;extract keywords
    ;end
  local-list)

simply opens a file by find-file-noselect, parse it, extract keywords and return a list. The conundrum is that, if populate the list as pointed by arrow the function in add-hook get called properly. If I use populate the list by calling the parse function, as I type in buffers with minor mode activate, nothing happens (agls-check-word is not called)

4
  • I don't understand what's the trouble in the question title. Does (agls-parse-acn-file) work? If agls-list doesn't change, you don't need to set its value every time the mode is turned on and off.
    – xuchunyang
    Apr 29, 2019 at 15:03
  • @xuchunyang, thanks for reply. The function you mentioned does work. The agls-list gets populated, however, when I activate the minor mode and type in buffer, agls-check-word function which is added to hook does not get called. If I use the comment line and set the list in the body of define-minor-mode and comment the call to agls-parse-acn-file, then everything goes fine.
    – Pouya
    Apr 29, 2019 at 15:17
  • You can use C-h v post-self-insert-hook to see if agls-check-word is added. Maybe during agls-parse-acn-file you change the current buffer? We usually use (with-temp-buffer (insert-file-contents "path/to/file") (parse-the-file)) to read a file, instead of find-file-noselect
    – xuchunyang
    Apr 29, 2019 at 15:39
  • @xuchunyang thanks a lot! That was exactly the problem. If you turn your comment into an answer I will gladly accept it. Cheers!
    – Pouya
    Apr 29, 2019 at 15:51

1 Answer 1

1

Base on the comments, you changed the current buffer during agls-parse-acn-file, you can use the following instead:

(with-temp-buffer
  (insert-file-contents "path/to/file")
  (parse-the-file))

Your Answer

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

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