Skip to content

Instantly share code, notes, and snippets.

@fleimgruber
Created August 22, 2017 19:53
Show Gist options
  • Save fleimgruber/cd2386d8326fad3e0e2b99ebc2f05739 to your computer and use it in GitHub Desktop.
Save fleimgruber/cd2386d8326fad3e0e2b99ebc2f05739 to your computer and use it in GitHub Desktop.
Sketch of using edit-indirect to edit docstrings in rst
(use-package edit-indirect)
(defun edit-indirect-rst-set-mode (_parent _beg _end)
(rst-mode))
(setq edit-indirect-guess-mode-function #'edit-indirect-rst-set-mode)
(defun edit-indirect-rst-remove-spaces()
(save-excursion
(beginning-of-buffer)
(while (search-forward-regexp "^ \\(.*?\\)" nil t)
(replace-match "\\1" nil))))
(defun edit-indirect-rst-add-spaces()
(save-excursion
(beginning-of-buffer)
(while (search-forward-regexp "^\\([^\n]\\)" nil t)
(replace-match " \\1" nil))
(end-of-buffer)
(insert " ")))
(add-to-list 'edit-indirect-after-creation-hook #'edit-indirect-rst-remove-spaces)
(add-to-list 'edit-indirect-before-commit-hook #'edit-indirect-rst-add-spaces)
(defun edit-indirect-region-wrap-rst (s e o)
(edit-indirect-region s e o))
(defun edit-indirect-rst ()
(interactive)
(let ((pt (point)))
(progn
(save-excursion
(setq s (re-search-backward "\\(\"\"\"\\)" nil t)
region-start (match-end 0))
(goto-char pt)
(setq e (re-search-forward "\\(\"\"\"\\)" nil t)
region-end (match-beginning 0))
(and s e (< s pt e))))
(edit-indirect-region-wrap-rst region-start region-end t)))
(global-set-key (kbd "C-c e") 'edit-indirect-rst)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment