I had a simple config to set fci-rule-column to different values depend on file types:
(use-package fill-column-indicator
:ensure t
:config
(setq fci-rule-color "LightSlateBlue")
(setq fci-handle-truncate-lines t)
(add-hook 'python-mode-hook (lambda ()
(setq-default fci-rule-column 79)
(fci-mode t)))
(add-hook 'c-mode-hook (lambda ()
(setq-default fci-rule-column 80)
(fci-mode t))))
You can see every time I add new file type, I have to repeat the work (add-hook ...). So I decide to refactor the code:
(use-package fill-column-indicator
:ensure t
:config
(setq fci-rule-color "LightSlateBlue")
(setq fci-handle-truncate-lines t)
(defun my/fci-config (mode num)
(add-hook mode (lambda ()
(progn
(setq-default fci-rule-column num)
(fci-mode t)))))
(let (mode-config-hash)
(setq mode-config-hash (make-hash-table :test 'equal))
(puthash 'python-mode-hook 79 mode-config-hash)
(puthash 'c-mode-hook 80 mode-config-hash)
(puthash 'cperl-mode-hook 80 mode-config-hash)
(maphash (lambda (k v) (my/fci-config k v)) mode-config-hash)))
With this approach, I only to put the mode and the number to hash table mode-config-hash when I configure setting for new file type.
But the code above did not work. I opened a Python or C file and the fci-mode was not enabled.
I try some debugging and realized that the line:
(setq-default fci-rule-column num)
did not ran correctly. If I change it to an integer value:
(setq-default fci-rule-column 79)
Then it worked.
So why did emacs see my variable? Is there a better way to accomplish this?
letor evenlet-lexicalwith no luck.letbinding is dynamic, unless lexical binding has been enabled for the library as a whole. If you usedlexical-letwith no success, show your code. Backquoting is a common solution in this sort of situation.lexical-letactually worked. I have some mis-typing in my attempt. It's now in my config github.com/Gnouc/emacs.d/commit/… . Thanks for your information.