Skip to content

Commit

Permalink
Add new my/begin-end-example, begin-end-src-emacs-lisp, begin-end-src…
Browse files Browse the repository at this point in the history
…-ruby
  • Loading branch information
smiller committed Feb 7, 2015
1 parent 3548ff8 commit b622482
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 7 deletions.
3 changes: 3 additions & 0 deletions mode-hooks.el
Expand Up @@ -5,6 +5,9 @@
(set-input-method "TeX") (set-input-method "TeX")
(define-key org-mode-map (kbd "M-s M-q") 'my/begin-end-quote) (define-key org-mode-map (kbd "M-s M-q") 'my/begin-end-quote)
(define-key org-mode-map (kbd "M-s M-v") 'my/begin-end-verse) (define-key org-mode-map (kbd "M-s M-v") 'my/begin-end-verse)
(define-key org-mode-map (kbd "M-s M-x") 'my/begin-end-example)
(define-key org-mode-map (kbd "M-s M-e") 'my/begin-end-src-emacs-lisp)
(define-key org-mode-map (kbd "M-s M-r") 'my/begin-end-src-ruby)
) )


(add-hook 'text-mode-hook 'my-text-mode-hook) (add-hook 'text-mode-hook 'my-text-mode-hook)
Expand Down
26 changes: 19 additions & 7 deletions mods-org.el
Expand Up @@ -29,26 +29,38 @@


(defun my/begin-end-quote () (defun my/begin-end-quote ()
(interactive) (interactive)
(my/begin-end "quote")) (my/begin-end "quote" "quote"))


(defun my/begin-end-verse () (defun my/begin-end-verse ()
(interactive) (interactive)
(my/begin-end "verse")) (my/begin-end "verse" "verse"))


(defun my/begin-end (variant) (defun my/begin-end-example ()
(interactive)
(my/begin-end "example" "example"))

(defun my/begin-end-src-emacs-lisp ()
(interactive)
(my/begin-end "src emacs-lisp" "src"))

(defun my/begin-end-src-ruby ()
(interactive)
(my/begin-end "src ruby" "src"))

(defun my/begin-end (begin-tag end-tag)
(interactive) (interactive)
(let ((cited-string "\n")) (let ((cited-string "\n"))
(when (use-region-p) (when (use-region-p)
(setq cited-string (setq cited-string
(my/fix-formatting (buffer-substring-no-properties (region-beginning) (region-end)))) (my/remove-old-citation-formatting (buffer-substring-no-properties (region-beginning) (region-end))))
(delete-region (region-beginning) (region-end))) (delete-region (region-beginning) (region-end)))
(insert "#+begin_" variant "\n" (insert "#+begin_" begin-tag "\n"
cited-string cited-string
"#+end_" variant "\n")) "#+end_" end-tag "\n"))
(unless (use-region-p) (unless (use-region-p)
(forward-line -2))) (forward-line -2)))


(defun my/fix-formatting (str) (defun my/remove-old-citation-formatting (str)
(interactive) (interactive)
(if (string= (substring str 0 2) "> ") (if (string= (substring str 0 2) "> ")
(replace-regexp-in-string "^> " " " (replace-regexp-in-string "^> " " "
Expand Down
27 changes: 27 additions & 0 deletions test/mods-org-test.el
Expand Up @@ -100,6 +100,33 @@
(buffer-string)) (buffer-string))
"#+begin_verse\n count > 42\n#+end_verse\n"))) "#+begin_verse\n count > 42\n#+end_verse\n")))


(ert-deftest test-begin-end-example ()
"Tests begin-end-example"
(should (string= (with-temp-buffer
(insert "exemplaria\ninter alia\n")
(select-region)
(my/begin-end-example)
(buffer-string))
"#+begin_example\nexemplaria\ninter alia\n#+end_example\n")))

(ert-deftest test-begin-end-src-emacs-lisp ()
"Tests begin-end-src-emacs-lisp"
(should (string= (with-temp-buffer
(insert "(defun my/begin-end-example\n(interactive)\n(my/begin-end \"example\"))\n")
(select-region)
(my/begin-end-src-emacs-lisp)
(buffer-string))
"#+begin_src emacs-lisp\n(defun my/begin-end-example\n(interactive)\n(my/begin-end \"example\"))\n#+end_src\n")))

(ert-deftest test-begin-end-src-ruby ()
"Tests begin-end-src-ruby"
(should (string= (with-temp-buffer
(insert "class User < ActiveRecord::Base\n include Permissions\n")
(select-region)
(my/begin-end-src-ruby)
(buffer-string))
"#+begin_src ruby\nclass User < ActiveRecord::Base\n include Permissions\n#+end_src\n")))

(defun select-region () (defun select-region ()
(goto-char (point-min)) (goto-char (point-min))
(set-mark-command nil) (set-mark-command nil)
Expand Down

0 comments on commit b622482

Please sign in to comment.