From 4f983c139e8ca6ed88be653080c7ce76b7aebff4 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sun, 28 Jan 2018 03:00:25 -0500 Subject: [PATCH] Improve doom/describe-{setting,module} commands Now grabs the setting/module at point --- core/autoload/help.el | 73 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 11 deletions(-) diff --git a/core/autoload/help.el b/core/autoload/help.el index 52ee799b6..e884c7f0f 100644 --- a/core/autoload/help.el +++ b/core/autoload/help.el @@ -1,13 +1,24 @@ ;;; 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 (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!'). + +Defaults to the " (interactive - ;; TODO try to read setting from whole line - (list (completing-read "Describe setting%s: " - (sort (mapcar #'car doom-settings) #'string-lessp) - nil t nil nil))) + (let ((sym (symbol-at-point))) + (list (completing-read "Describe setting: " + (sort (mapcar #'car doom-settings) #'string-lessp) + nil t (if (keywordp sym) (symbol-name sym)))))) (let ((fn (cdr (assq (intern setting) doom-settings)))) (unless fn (error "'%s' is not a valid DOOM setting" setting)) @@ -16,13 +27,53 @@ ;;;###autoload (defun doom/describe-module (module) "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 +from a `featurep!' or `require!' call or c) the module that the current file is +in." (interactive - ;; TODO try to read module from whole line - (list (completing-read "Describe module: " - (cl-loop for (module . sub) in (reverse (hash-table-keys doom-modules)) - collect (format "%s %s" module sub)) - nil t))) + (let ((module + (cond ((string= (file-truename user-init-file) + (file-truename buffer-file-name)) + (let* ((category + (save-excursion + (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 + (sp-beginning-of-sexp) + (unless (eq (char-after) ?\() + (backward-char)) + (let ((sexp (sexp-at-point))) + (when (memq (car-safe sexp) '(featurep! require!)) + (format "%s %s" (nth 1 sexp) (nth 2 sexp)))))) + ((file-in-directory-p buffer-file-name doom-modules-dir) + (let ((module (doom-module-from-path buffer-file-name))) + (format "%s %s" (car module) (cdr module))))))) + (list (completing-read "Describe module: " + (cl-loop for (module . sub) in (reverse (hash-table-keys doom-modules)) + collect (format "%s %s" module sub)) + nil t module)))) (cl-destructuring-bind (category submodule) (mapcar #'intern (split-string module " ")) (unless (member (cons category submodule) (doom-module-pairs))