summaryrefslogtreecommitdiff
path: root/guides/emacs/use-package.org
blob: f6cd0275fca727106ed9e4bb8eb6be8e5c8dd9d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
* Porting from ~custom-file~
Some very dumb code to generate ~use-package~ declarations from Custom
settings.  Entry point is ~c->us/port~.
#+begin_src elisp
(require 'help-fns)
(require 'radix-tree)

(defun c->us/get-custom-options ()
  (seq-map
   (pcase-lambda (`(theme-value ,option user ,value))
     (list option value))
   (get 'user 'theme-settings)))

(defun c->us/get-option-file (option)
  ;; Load packages first, otherwise symbol-file can return "loaddefs".
  (pcase-dolist
      (`(_ . ,files)
       (radix-tree-prefixes (help-definition-prefixes)
                            (symbol-name option)))
    (dolist (f files)
      (load f 'noerror 'nomessage)))
  (when-let ((file (symbol-file option)))
    (file-name-base file)))

(defun c->us/write-declaration (lib pairs)
  (insert (format "(use-package %s\n" lib))
  (insert "  :custom")
  (message "%s -> %s" lib pairs)
  (pcase-dolist
      (`(,option ,value) pairs)
    (insert (format "\n  (%s %s)"
                    option
                    (prin1-to-string value))))
  (insert ")\n\n"))

(defun c->us/symbols< (symlist1 symlist2)
  (string< (car symlist1) (car symlist2)))

(defun c->us/port ()
  (seq-map
   (pcase-lambda (`(,lib . ,pairs))
     (c->us/write-declaration lib pairs))
   (sort (seq-group-by
          (pcase-lambda (`(,option _))
            (c->us/get-option-file option))
          (sort (c->us/get-custom-options) 'c->us/symbols<))
         'c->us/symbols<)))
#+end_src