2014-12-20 04:48:31 +00:00
|
|
|
;;; helm-spacemacs.el --- Spacemacs layer exploration with `helm'.
|
|
|
|
|
|
|
|
;; Author: Sylvain Benner <sylvain.benner@gmail.com>
|
|
|
|
;; Keywords: helm, spacemacs
|
|
|
|
;; Version: 0.1
|
|
|
|
;; Package-Requires: ((helm "1.5"))
|
|
|
|
|
|
|
|
;; This file is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
|
|
|
|
;; This file is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
|
|
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
;; This package adds a convenient way to discover Spacemacs configuration
|
|
|
|
;; layers thanks to helm.
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
2015-08-27 06:58:05 +00:00
|
|
|
(require 'cl)
|
2014-12-21 07:53:14 +00:00
|
|
|
(require 'ht)
|
2015-01-27 05:16:44 +00:00
|
|
|
(require 'helm)
|
|
|
|
(require 'core-configuration-layer)
|
2014-12-21 07:53:14 +00:00
|
|
|
|
2015-08-12 12:59:25 +00:00
|
|
|
(defvar helm-spacemacs-all-layers nil
|
2014-12-21 07:53:14 +00:00
|
|
|
"Alist of all configuration layers.")
|
|
|
|
|
2015-08-12 12:59:25 +00:00
|
|
|
(defvar helm-spacemacs-all-packages nil
|
2014-12-21 07:53:14 +00:00
|
|
|
"Hash table of all packages in all layers.")
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
(define-minor-mode helm-spacemacs-mode
|
|
|
|
"Layers discovery with helm interface."
|
|
|
|
:group 'spacemacs
|
2015-08-12 12:59:25 +00:00
|
|
|
:global t)
|
|
|
|
|
|
|
|
(defun helm-spacemacs//init (&optional arg)
|
|
|
|
(when (or arg (null helm-spacemacs-all-packages))
|
|
|
|
(mapc (lambda (layer) (push (configuration-layer/make-layer layer)
|
|
|
|
helm-spacemacs-all-layers))
|
|
|
|
(configuration-layer/get-layers-list))
|
|
|
|
(dolist (layer helm-spacemacs-all-layers)
|
|
|
|
(unless (configuration-layer/layer-usedp (oref layer :name))
|
|
|
|
(configuration-layer//load-layer-files layer '("funcs.el"
|
|
|
|
"config.el"))))
|
|
|
|
(setq helm-spacemacs-all-packages (configuration-layer/get-packages
|
|
|
|
helm-spacemacs-all-layers))))
|
2014-12-20 04:48:31 +00:00
|
|
|
|
|
|
|
;;;###autoload
|
2015-08-12 12:59:25 +00:00
|
|
|
(defun helm-spacemacs (arg)
|
2014-12-20 04:48:31 +00:00
|
|
|
"Layers discovery with helm interface."
|
2015-08-12 12:59:25 +00:00
|
|
|
(interactive "P")
|
2014-12-21 07:53:14 +00:00
|
|
|
(helm-spacemacs-mode)
|
2015-08-12 12:59:25 +00:00
|
|
|
(helm-spacemacs//init arg)
|
2014-12-20 04:48:31 +00:00
|
|
|
(helm :buffer "*helm: spacemacs*"
|
2015-06-06 17:24:48 +00:00
|
|
|
:sources `(,(helm-spacemacs//documentation-source)
|
|
|
|
,(helm-spacemacs//layer-source)
|
2015-01-29 05:08:48 +00:00
|
|
|
,(helm-spacemacs//package-source)
|
2015-05-16 04:49:38 +00:00
|
|
|
,(helm-spacemacs//dotspacemacs-source)
|
2015-01-29 05:08:48 +00:00
|
|
|
,(helm-spacemacs//toggle-source))))
|
2014-12-20 04:48:31 +00:00
|
|
|
|
2015-06-06 17:24:48 +00:00
|
|
|
(defun helm-spacemacs//documentation-source ()
|
|
|
|
"Construct the helm source for the documentation section."
|
2015-06-10 05:32:44 +00:00
|
|
|
(helm-build-sync-source "Spacemacs Documentation"
|
2015-06-10 02:56:08 +00:00
|
|
|
:candidates #'helm-spacemacs//documentation-candidates
|
|
|
|
:persistent-action #'helm-spacemacs//documentation-action-open-file
|
|
|
|
:keymap helm-map
|
|
|
|
:action (helm-make-actions
|
|
|
|
"Open Documentation" #'helm-spacemacs//documentation-action-open-file)))
|
2015-06-06 17:24:48 +00:00
|
|
|
|
|
|
|
(defun helm-spacemacs//documentation-candidates ()
|
2015-06-10 15:06:30 +00:00
|
|
|
(let (result file-extension)
|
2015-06-18 06:54:48 +00:00
|
|
|
(dolist (filename (directory-files spacemacs-docs-directory))
|
|
|
|
(setq file-extension (file-name-extension filename))
|
2015-06-10 15:06:30 +00:00
|
|
|
(when (or (equal file-extension "md")
|
|
|
|
(equal file-extension "org"))
|
2015-06-18 06:54:48 +00:00
|
|
|
(push filename result)))
|
2015-06-11 05:00:54 +00:00
|
|
|
;; delete DOCUMENTATION.org to make it the first guide
|
|
|
|
(delete "DOCUMENTATION.org" result)
|
|
|
|
(push "DOCUMENTATION.org" result)
|
2015-06-10 02:56:08 +00:00
|
|
|
|
|
|
|
;; give each document an appropriate title
|
|
|
|
(mapcar (lambda (r)
|
|
|
|
(cond
|
2015-06-11 05:00:54 +00:00
|
|
|
((string-equal r "CONTRIBUTE.org")
|
2015-06-10 02:56:08 +00:00
|
|
|
`("How to contribute to Spacemacs" . ,r))
|
2015-06-11 05:00:54 +00:00
|
|
|
((string-equal r "CONVENTIONS.org")
|
2015-06-10 02:56:08 +00:00
|
|
|
`("Spacemacs conventions" . ,r))
|
2015-06-11 05:00:54 +00:00
|
|
|
((string-equal r "DOCUMENTATION.org")
|
2015-09-24 04:17:37 +00:00
|
|
|
`("Spacemacs documentation" . ,r))
|
|
|
|
((string-equal r "FAQ.org")
|
|
|
|
`("Spacemacs FAQ" . ,r))
|
2015-06-11 05:00:54 +00:00
|
|
|
((string-equal r "HOWTOs.org")
|
2015-06-10 02:56:08 +00:00
|
|
|
`("Quick HOW-TOs for Spacemacs" . ,r))
|
2015-08-05 20:09:58 +00:00
|
|
|
((string-equal r "LAYERS.org")
|
|
|
|
`("Tips on writing layers for Spacemacs" . ,r))
|
2015-09-24 04:17:37 +00:00
|
|
|
((string-equal r "QUICK_START.org")
|
|
|
|
`("Quick start guide for Spacemacs" . ,r))
|
2015-06-10 02:56:08 +00:00
|
|
|
((string-equal r "VIMUSERS.org")
|
|
|
|
`("Vim users migration guide" . ,r))
|
|
|
|
(t
|
|
|
|
`(r . ,r))))
|
|
|
|
result)))
|
2015-06-06 17:24:48 +00:00
|
|
|
|
|
|
|
(defun helm-spacemacs//documentation-action-open-file (candidate)
|
|
|
|
"Open documentation FILE."
|
|
|
|
(let ((file (concat spacemacs-docs-directory candidate)))
|
2015-06-09 03:32:55 +00:00
|
|
|
(cond ((and (equal (file-name-extension file) "md")
|
|
|
|
(not helm-current-prefix-arg))
|
|
|
|
(condition-case nil
|
|
|
|
(with-current-buffer (find-file-noselect file)
|
|
|
|
(gh-md-render-buffer)
|
|
|
|
(kill-this-buffer))
|
|
|
|
;; if anything fails, fall back to simply open file
|
|
|
|
(find-file file)))
|
|
|
|
((equal (file-name-extension file) "org")
|
2015-06-10 15:06:30 +00:00
|
|
|
(spacemacs/view-org-file file "^" 'all))
|
2015-06-09 03:32:55 +00:00
|
|
|
(t
|
|
|
|
(find-file file)))))
|
2015-06-06 17:24:48 +00:00
|
|
|
|
2014-12-20 04:48:31 +00:00
|
|
|
(defun helm-spacemacs//layer-source ()
|
|
|
|
"Construct the helm source for the layer section."
|
|
|
|
`((name . "Layers")
|
2014-12-25 19:45:27 +00:00
|
|
|
(candidates . ,(sort (configuration-layer/get-layers-list) 'string<))
|
2015-03-05 05:00:54 +00:00
|
|
|
(candidate-number-limit)
|
2015-08-09 03:31:02 +00:00
|
|
|
(action . (("Open README.org"
|
|
|
|
. helm-spacemacs//layer-action-open-readme)
|
|
|
|
("Open packages.el"
|
|
|
|
. helm-spacemacs//layer-action-open-packages)
|
|
|
|
;; TODO remove extensions in 0.105.0
|
|
|
|
("Open extensions.el"
|
2015-10-02 18:51:10 +00:00
|
|
|
. helm-spacemacs//layer-action-open-extensions)
|
|
|
|
("Open README.org (for editing)"
|
|
|
|
. helm-spacemacs//layer-action-open-readme-edit)))))
|
2014-12-20 04:48:31 +00:00
|
|
|
|
2014-12-21 07:53:14 +00:00
|
|
|
(defun helm-spacemacs//package-source ()
|
|
|
|
"Construct the helm source for the packages."
|
|
|
|
`((name . "Packages")
|
|
|
|
(candidates . ,(helm-spacemacs//package-candidates))
|
2015-03-05 05:00:54 +00:00
|
|
|
(candidate-number-limit)
|
2015-08-09 03:31:02 +00:00
|
|
|
(action . (("Go to init function"
|
|
|
|
. helm-spacemacs//package-action-goto-init-func)))))
|
2014-12-21 07:53:14 +00:00
|
|
|
|
|
|
|
(defun helm-spacemacs//package-candidates ()
|
|
|
|
"Return the sorted candidates for package source."
|
|
|
|
(let (result)
|
2015-08-09 03:31:02 +00:00
|
|
|
(dolist (pkg helm-spacemacs-all-packages)
|
|
|
|
(push (format "(%S) package: %S" (oref pkg :owner) (oref pkg :name))
|
|
|
|
result))
|
2014-12-21 07:53:14 +00:00
|
|
|
(sort result 'string<)))
|
|
|
|
|
2015-01-29 05:08:48 +00:00
|
|
|
(defun helm-spacemacs//toggle-source ()
|
|
|
|
"Construct the helm source for the toggles."
|
2015-08-27 06:58:05 +00:00
|
|
|
(helm-build-sync-source "Toggles"
|
|
|
|
:candidates #'helm-spacemacs//toggle-candidates
|
|
|
|
:persistent-action #'helm-spacemacs//toggle
|
|
|
|
:keymap helm-map
|
|
|
|
:action (helm-make-actions "Toggle" #'helm-spacemacs//toggle)))
|
2015-01-29 05:08:48 +00:00
|
|
|
|
|
|
|
(defun helm-spacemacs//toggle-candidates ()
|
|
|
|
"Return the sorted candidates for toggle source."
|
|
|
|
(let (result)
|
|
|
|
(dolist (toggle spacemacs-toggles)
|
2015-08-27 06:58:05 +00:00
|
|
|
(let* ((toggle-symbol (symbol-name (car toggle)))
|
|
|
|
(toggle-name (capitalize (replace-regexp-in-string "-" " " toggle-symbol)))
|
|
|
|
(toggle-doc (format "%s: %s" toggle-name (plist-get (cdr toggle) :documentation))))
|
|
|
|
(if (plist-member (cdr toggle) :documentation)
|
|
|
|
(push `(,toggle-doc . ,toggle-symbol) result)
|
|
|
|
(push `(,toggle-name . ,toggle-symbol) result))))
|
|
|
|
(setq result (cl-sort result 'string< :key 'car))
|
|
|
|
result))
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-05-16 04:49:38 +00:00
|
|
|
(defun helm-spacemacs//dotspacemacs-source ()
|
|
|
|
`((name . "Dotfile")
|
|
|
|
(candidates . ,(helm-spacemacs//dotspacemacs-candidates))
|
|
|
|
(candidate-number-limit)
|
|
|
|
(action . (("Go to variable" . helm-spacemacs//go-to-dotfile-variable)))))
|
|
|
|
|
|
|
|
(defun helm-spacemacs//dotspacemacs-candidates ()
|
|
|
|
"Return the sorted candidates for all the dospacemacs variables."
|
2015-09-01 03:16:14 +00:00
|
|
|
(sort (dotspacemacs/get-variable-string-list) 'string<))
|
2015-05-16 04:49:38 +00:00
|
|
|
|
2015-10-02 18:51:10 +00:00
|
|
|
(defun helm-spacemacs//layer-action-open-file (file candidate &optional edit)
|
|
|
|
"Open FILE of the passed CANDIDATE. If EDIT is false, open in view mode."
|
2015-06-12 01:23:50 +00:00
|
|
|
(let ((path (if (and (equalp file "README.org") (equalp candidate "spacemacs"))
|
2015-02-27 16:23:50 +00:00
|
|
|
;; Readme for spacemacs is in the project root
|
|
|
|
(ht-get configuration-layer-paths (intern candidate))
|
|
|
|
(file-name-as-directory
|
|
|
|
(concat (ht-get configuration-layer-paths
|
|
|
|
(intern candidate))
|
|
|
|
candidate)))))
|
2015-06-12 01:23:50 +00:00
|
|
|
(if (and (equal (file-name-extension file) "org")
|
2015-04-29 18:07:05 +00:00
|
|
|
(not helm-current-prefix-arg))
|
2015-10-02 18:51:10 +00:00
|
|
|
(if edit
|
|
|
|
(find-file (concat path file))
|
|
|
|
(spacemacs/view-org-file (concat path file) "^" 'all))
|
2015-04-23 05:20:23 +00:00
|
|
|
(find-file (concat path file)))))
|
2014-12-20 04:48:31 +00:00
|
|
|
|
|
|
|
(defun helm-spacemacs//layer-action-open-readme (candidate)
|
2015-10-02 18:51:10 +00:00
|
|
|
"Open the `README.org' file of the passed CANDIDATE for reading."
|
2015-06-12 01:23:50 +00:00
|
|
|
(helm-spacemacs//layer-action-open-file "README.org" candidate))
|
2014-12-20 04:48:31 +00:00
|
|
|
|
2015-10-02 18:51:10 +00:00
|
|
|
(defun helm-spacemacs//layer-action-open-readme-edit (candidate)
|
|
|
|
"Open the `README.org' file of the passed CANDIDATE for editing."
|
|
|
|
(helm-spacemacs//layer-action-open-file "README.org" candidate t))
|
|
|
|
|
2014-12-20 04:48:31 +00:00
|
|
|
(defun helm-spacemacs//layer-action-open-packages (candidate)
|
|
|
|
"Open the `packages.el' file of the passed CANDIDATE."
|
|
|
|
(helm-spacemacs//layer-action-open-file "packages.el" candidate))
|
|
|
|
|
2015-08-09 03:31:02 +00:00
|
|
|
;; TODO remove extensions in 0.105.0
|
2014-12-20 04:48:31 +00:00
|
|
|
(defun helm-spacemacs//layer-action-open-extensions (candidate)
|
|
|
|
"Open the `extensions.el' file of the passed CANDIDATE."
|
|
|
|
(helm-spacemacs//layer-action-open-file "extensions.el" candidate))
|
|
|
|
|
2014-12-21 07:53:14 +00:00
|
|
|
(defun helm-spacemacs//package-action-goto-init-func (candidate)
|
|
|
|
"Open the file `packages.el' and go to the init function."
|
|
|
|
(save-match-data
|
2015-05-21 04:46:06 +00:00
|
|
|
(string-match "^(\\(.+\\))\s\\(.+\\):\s\\(.+\\)$" candidate)
|
2014-12-21 07:53:14 +00:00
|
|
|
(let* ((layer (match-string 1 candidate))
|
2015-05-21 04:46:06 +00:00
|
|
|
(type (match-string 2 candidate))
|
|
|
|
(package (match-string 3 candidate))
|
2014-12-21 07:53:14 +00:00
|
|
|
(path (file-name-as-directory
|
2014-12-25 19:45:27 +00:00
|
|
|
(concat (ht-get configuration-layer-paths (intern layer))
|
2014-12-21 07:53:14 +00:00
|
|
|
layer)))
|
2015-05-21 04:46:06 +00:00
|
|
|
(filename (cond ((string-equal "package" type)
|
|
|
|
(concat path "packages.el"))
|
2015-08-09 03:31:02 +00:00
|
|
|
;; TODO remove extensions in 0.105.0
|
2015-05-21 04:46:06 +00:00
|
|
|
(t (concat path "extensions.el")))))
|
2014-12-21 07:53:14 +00:00
|
|
|
(find-file filename)
|
2014-12-24 05:21:47 +00:00
|
|
|
(goto-char (point-min))
|
2014-12-21 07:53:14 +00:00
|
|
|
(re-search-forward (format "init-%s" package))
|
|
|
|
(beginning-of-line))))
|
|
|
|
|
2015-01-29 05:08:48 +00:00
|
|
|
(defun helm-spacemacs//toggle (candidate)
|
|
|
|
"Toggle candidate."
|
|
|
|
(let ((toggle (assq (intern candidate) spacemacs-toggles)))
|
|
|
|
(when toggle
|
2015-01-30 05:18:48 +00:00
|
|
|
(funcall (plist-get (cdr toggle) :function)))))
|
2015-01-29 05:08:48 +00:00
|
|
|
|
2015-05-16 04:49:38 +00:00
|
|
|
(defun helm-spacemacs//go-to-dotfile-variable (candidate)
|
|
|
|
"Go to candidate in the dotfile."
|
|
|
|
(find-file dotspacemacs-filepath)
|
|
|
|
(goto-char (point-min))
|
|
|
|
;; try to exclude comments
|
|
|
|
(re-search-forward (format "^[a-z\s\\(\\-]*%s" candidate))
|
|
|
|
(beginning-of-line))
|
|
|
|
|
|
|
|
|
2014-12-20 04:48:31 +00:00
|
|
|
(provide 'helm-spacemacs)
|
|
|
|
|
|
|
|
;;; helm-spacemacs.el ends here
|