doom/describe-module: detect module associated with major-mode

This commit is contained in:
Henrik Lissner 2018-02-19 20:27:18 -05:00
parent 6f4ca15445
commit 95009c08f8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -1,14 +1,5 @@
;;; core/autoload/help.el -*- lexical-binding: t; -*- ;;; core/autoload/help.el -*- lexical-binding: t; -*-
(defun doom--find-next (pred forward-fn end-fn)
(save-excursion
(catch 'found
(ignore-errors
(while (not (funcall end-fn))
(when-let* ((ret (funcall pred)))
(throw 'found ret))
(funcall forward-fn))))))
;;;###autoload ;;;###autoload
(defun doom/describe-setting (setting) (defun doom/describe-setting (setting)
"Open the documentation of SETTING (a keyword defined with `def-setting!'). "Open the documentation of SETTING (a keyword defined with `def-setting!').
@ -24,53 +15,80 @@ Defaults to the "
(error "'%s' is not a valid DOOM setting" setting)) (error "'%s' is not a valid DOOM setting" setting))
(describe-function fn))) (describe-function fn)))
;;
(defvar doom--module-mode-alist
'((c-mode :lang cc)
(c++-mode :lang cc)
(objc++-mode :lang cc)
(java-mode :lang java)
(csharp-mode :lang csharp)
(clojure-mode :lang clojure)
(emacs-lisp-mode :lang emacs-lisp)
(go-mode :lang go)
(haskell-mode :lang haskell)
(js2-mode :lang javascript)
(julia-mode :lang julia)
(latex-mode :lang latex)
(LaTeX-mode :lang latex)
(ledger-mode :lang ledger)
(lua-mode :lang lua)
(markdown-mode :lang markdown)
(gfm-mode :lang markdown)
(ocaml-mode :lang ocaml)
(org-mode :lang org)
(perl-mode :lang perl)
(php-mode :lang php)
(hack-mode :lang php)
(plantuml-mode :lang plantuml)
(purescript-mode :lang purescript)
(python-mode :lang python)
(restclient-mode :lang rest)
(ruby-mode :lang ruby)
(rust-mode :lang rust)
(scala-mode :lang scala)
(sh-mode :lang sh)
(swift-mode :lang swift)
(typescript-mode :lang typescript)
(web-mode :lang web)
(css-mode :lang web)
(scss-mode :lang web)
(sass-mode :lang web)
(less-css-mode :lang web)
(stylus-mode :lang web))
"TODO")
;;;###autoload ;;;###autoload
(defun doom/describe-module (module) (defun doom/describe-module (module)
"Open the documentation of MODULE (a string that represents the category and "Open the documentation of MODULE (a string that represents the category and
submodule in the format, e.g. ':feature evil'). submodule in the format, e.g. ':feature evil').
Defaults to either a) the module at point (in init.el), b) the module derived Defaults to either a) the module at point (in init.el), b) the module derived
from a `featurep!' or `require!' call or c) the module that the current file is from a `featurep!' or `require!' call, c) the module that the current file is
in." in, or d) the module associated with the current major mode (see
`doom--module-mode-alist')."
(interactive (interactive
(let ((module (let ((module
(cond ((and buffer-file-name (cond ((and buffer-file-name
(string= (file-truename user-init-file) (eq major-mode 'emacs-lisp-mode)
(file-truename buffer-file-name))) (string= (file-name-nondirectory buffer-file-name)
(let* ((category "init.el")
(save-excursion (thing-at-point 'sexp t)))
(goto-char (line-end-position))
(doom--find-next
(lambda ()
(and (not (sp-point-in-comment))
(keywordp (symbol-at-point))
(symbol-at-point)))
(lambda () (forward-symbol -1))
#'bobp)))
(module
(save-excursion
(goto-char (line-beginning-position))
(save-restriction
(narrow-to-region (point) (line-end-position))
(doom--find-next
(lambda ()
(and (not (sp-point-in-comment))
(not (keywordp (symbol-at-point)))
(sexp-at-point)))
#'forward-sexp
#'eobp)))))
(when (and category module)
(format "%s %s" category module))))
((save-excursion ((save-excursion
(ignore-errors
(sp-beginning-of-sexp) (sp-beginning-of-sexp)
(unless (eq (char-after) ?\() (unless (eq (char-after) ?\()
(backward-char)) (backward-char))
(let ((sexp (sexp-at-point))) (let ((sexp (sexp-at-point)))
(when (memq (car-safe sexp) '(featurep! require!)) (when (memq (car-safe sexp) '(featurep! require!))
(format "%s %s" (nth 1 sexp) (nth 2 sexp)))))) (format "%s %s" (nth 1 sexp) (nth 2 sexp)))))))
(buffer-file-name ((and buffer-file-name
(when-let* ((module (doom-module-from-path buffer-file-name))) (when-let* ((mod (doom-module-from-path buffer-file-name)))
(format "%s %s" (car module) (cdr module))))))) (format "%s %s" (car mod) (cdr mod)))))
((when-let* ((mod (cdr (assq major-mode doom--module-mode-alist))))
(format "%s %s"
(symbol-name (car mod))
(symbol-name (cadr mod))))))))
(list (completing-read "Describe module: " (list (completing-read "Describe module: "
(cl-loop for (module . sub) in (reverse (hash-table-keys doom-modules)) (cl-loop for (module . sub) in (reverse (hash-table-keys doom-modules))
collect (format "%s %s" module sub)) collect (format "%s %s" module sub))