Skip to content

Commit

Permalink
sphinx: add new conventional command under , c and add , g c
Browse files Browse the repository at this point in the history
New functions in rst-sphinx:
- rst-sphinx-find-conf-py-path
- rst-sphinx-open-conf
- rst-sphinx-clean
- rst-sphinx-rebuild

Modified functions:
- rst-sphinx-compile takes now an optional argument to clean the project
- rst-sphinx-find-conf has been renamed to rst-sphinx-set-variables and now use
rst-sphinx-find-conf-py-path function

New key bindings:
- "cc" --> rst-sphinx-compile
- "cC" --> rst-sphinx-clean
- "cr" --> rst-sphinx-rebuild
- "gc" --> rst-sphinx-open-conf
- "o"  --> rst-sphinx-target-open)))
  • Loading branch information
syl20bnr committed Feb 3, 2017
1 parent 9ab4453 commit d1d58ef
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 31 deletions.
95 changes: 66 additions & 29 deletions layers/+tools/sphinx/local/rst-sphinx/rst-sphinx.el
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

;;; Code:

(require 'compile)

(defgroup rst-sphinx nil
"Settings for support of conversion of reStructuredText
document with \\[rst-sphinx]."
Expand Down Expand Up @@ -53,59 +55,94 @@ document with \\[rst-sphinx]."
"The Sphinx project target project directories."
:group 'rst-builders)

(defun rst-sphinx-find-conf ()
"Look for the configuration file in the parents of the current path."
(defun rst-sphinx-set-variables ()
"Set global variables used py `rst-sphinx'."
(let ((conf (rst-sphinx-find-conf-py-path)))
(when conf
(setq dir (directory-file-name (file-name-directory conf)))
(setq rst-sphinx-source dir)
(setq rst-sphinx-project (file-name-nondirectory dir))
;; not depend on local variables, put here just for convenience.
(setq rst-sphinx-target
(expand-file-name
(nth 2 (assoc rst-sphinx-project
rst-sphinx-target-projects))
rst-sphinx-target-parent))
(setq rst-sphinx-source-buffer (current-buffer)
rst-sphinx-source-file (buffer-file-name)))))

(defun rst-sphinx-find-conf-py-path ()
"Return path to conf.py or nil if not found."
;; (interactive)
(let* ((file-name "conf.py")
(buffer-file (buffer-file-name))
(dir (file-name-directory buffer-file)))
;; Move up in the dir hierarchy till we find a change log file.
(while (or (not (file-exists-p (concat dir file-name))))
(dir (file-name-directory buffer-file))
(conf-py (concat dir file-name)))
;; Move up in the dir hierarchy to find conf.py
(while (or (not buffer-file)
(not (file-exists-p conf-py)))
;; Move up to the parent dir and try again.
(setq buffer-file (directory-file-name
(file-name-directory buffer-file)))
(setq dir (file-name-directory buffer-file)))
(setq dir (directory-file-name
(file-name-directory buffer-file)))
(setq rst-sphinx-source dir)
(setq rst-sphinx-project (file-name-nondirectory dir))
;; not depend on local variables, put here just for convience.
(setq rst-sphinx-target
(expand-file-name
(nth 2 (assoc rst-sphinx-project
rst-sphinx-target-projects))
rst-sphinx-target-parent))
(setq rst-sphinx-source-buffer (current-buffer)
rst-sphinx-source-file (buffer-file-name))
))


(require 'compile)

(defun rst-sphinx-compile ()
"Compile Sphinx project."
(setq dir (file-name-directory buffer-file))
(setq conf-py (concat dir file-name)))
(if buffer-file
conf-py
(message "Cannot find conf.py file.")
nil)))

(defun rst-sphinx-open-conf ()
"Open conf.py file."
(interactive)
(let ((conf (rst-sphinx-find-conf-py-path)))
(if (file-exists-p conf)
(find-file-existing conf)
(message "Cannot find conf.py file."))))

(defun rst-sphinx-compile (&optional clean)
"Compile Sphinx project.
If CLEAN is non-nil then clean the project before compiling."
(interactive "P")
(when clean
(rst-sphinx-clean))
(if (not (string= (file-name-extension (buffer-file-name)) "rst"))
(print "Not a ReStructerdText file.")
(progn
(rst-sphinx-find-conf)
(rst-sphinx-set-variables)
(let ((builder (nth 1 (assoc rst-sphinx-project
rst-sphinx-target-projects))))
(compile (mapconcat 'identity
(list rst-sphinx-command
(cdr (assoc builder rst-sphinx-builder))
rst-sphinx-source
rst-sphinx-target)
" "))
))))
" "))))))

(defun rst-sphinx-clean ()
"Clean Sphinx project."
(interactive)
(let* ((conf (rst-sphinx-find-conf-py-path))
(build (when conf (concat (file-name-directory conf)
rst-sphinx-target-parent))))
(if (file-exists-p build)
(progn
(delete-directory build t)
(message "Project cleaned successfully."))
(message "Cannot find build directory \"%s\"" rst-sphinx-target-parent))))


(defun rst-sphinx-rebuild ()
"Clean and compile Sphinx project."
(interactive)
(rst-sphinx-compile 'clean))

(defun rst-sphinx-target-open ()
"Open builded Sphinx project file."
(interactive)
(if (not (string= (file-name-extension (buffer-file-name)) "rst"))
(print "Not a ReStructerdText file.")
(progn
(rst-sphinx-find-conf)
(rst-sphinx-set-variables)
(let ((builder (nth 1 (assoc rst-sphinx-project
rst-sphinx-target-projects)))
(buffer-file (nth 1 (split-string
Expand Down
7 changes: 5 additions & 2 deletions layers/+tools/sphinx/packages.el
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
(defun sphinx/post-init-rst ()
(spacemacs|use-package-add-hook rst
:post-config (spacemacs/set-leader-keys-for-major-mode 'rst-mode
"c" 'rst-sphinx-compile
"f" 'rst-sphinx-target-open)))
"cc" 'rst-sphinx-compile
"cC" ' rst-sphinx-clean
"cr" ' rst-sphinx-rebuild
"gc" 'rst-sphinx-open-conf
"o" 'rst-sphinx-target-open)))

0 comments on commit d1d58ef

Please sign in to comment.