Permalink
Browse files

Refactoring begin/end: remove duplication verse / quote

  • Loading branch information...
smiller committed Feb 7, 2015
1 parent 33aee44 commit b4ddf920e97dc6756d58f0cad83e1a3a7c6606d6
Showing with 32 additions and 53 deletions.
  1. +20 −41 mods-org.el
  2. +12 −12 test/mods-org-test.el
View
@@ -27,77 +27,56 @@
(define-key global-map "\C-cc" 'org-capture)
(define-key global-map "\C-ca" 'org-agenda)
(defun begin-end-quote ()
(interactive)
(if (use-region-p)
(begin-end-quote-for-region)
(begin-end-quote-new)
)
(defun my/begin-end-quote ()
(my/begin-end "quote")
)
(defun begin-end-quote-for-region ()
(interactive)
(fix-old-formatting)
(insert "#+end_quote")
(newline)
(goto-char (region-beginning))
(insert "#+begin_quote")
(newline)
)
(defun begin-end-quote-new ()
(interactive)
(insert "#+begin_quote")
(newline)
(newline)
(insert "#+end_quote")
(newline)
(previous-line)
(previous-line)
)
(defun my/begin-end-verse ()
(my/begin-end "verse")
)
(defun begin-end-verse ()
(defun my/begin-end (variant)
(interactive)
(if (use-region-p)
(begin-end-verse-for-region)
(begin-end-verse-new)
(my/begin-end-selected-region variant)
(my/begin-end-no-selected-region variant)
)
)
(defun begin-end-verse-for-region ()
(defun my/begin-end-selected-region (variant)
(interactive)
(fix-old-formatting)
(insert "#+end_verse")
(my/fix-old-formatting)
(insert "#+end_" variant)
(newline)
(goto-char (region-beginning))
(insert "#+begin_verse")
(insert "#+begin_" variant)
(newline)
)
(defun begin-end-verse-new ()
(defun my/begin-end-no-selected-region (variant)
(interactive)
(insert "#+begin_verse")
(insert "#+begin_" variant)
(newline)
(newline)
(insert "#+end_verse")
(insert "#+end_" variant)
(newline)
(previous-line)
(previous-line)
)
(defun fix-old-formatting ()
(remove-old-formatting-code)
(indent-if-not-indented)
(defun my/fix-old-formatting ()
(my/remove-old-formatting-code)
(my/indent-if-not-indented)
)
(defun remove-old-formatting-code ()
(defun my/remove-old-formatting-code ()
(setq in (filter-buffer-substring (region-beginning) (region-end) t))
(setq out (replace-regexp-in-string "^> " "" in))
(setq out2 (replace-regexp-in-string "\s*<br/>$" "" out))
(insert out2)
)
(defun indent-if-not-indented ()
(defun my/indent-if-not-indented ()
(setq firstFour (filter-buffer-substring (region-beginning) (+ (region-beginning) 4)))
(if (not (string= firstFour " "))
(indent-region (region-beginning) (region-end) 4)
View
@@ -3,28 +3,28 @@
(ert-deftest test-begin-end-quote-new-content ()
"Tests begin-end-quote without selected region string"
(should (string= (with-temp-buffer
(begin-end-quote)
(my/begin-end-quote)
(buffer-string))
"#+begin_quote\n\n#+end_quote\n")))
(ert-deftest test-begin-end-quote-new-point ()
"Tests begin-end-quote without selected region cursor position"
(should (equal (with-temp-buffer
(begin-end-quote)
(my/begin-end-quote)
(point))
(length "#+begin_quote\n\n"))))
(ert-deftest test-begin-end-verse-new-content ()
"Tests begin-end-verse without selected region string"
(should (string= (with-temp-buffer
(begin-end-verse)
(my/begin-end-verse)
(buffer-string))
"#+begin_verse\n\n#+end_verse\n")))
(ert-deftest test-begin-end-verse-new-point ()
"Tests begin-end-verse without selected region cursor position"
(should (equal (with-temp-buffer
(begin-end-quote)
(my/begin-end-quote)
(point))
(length "#+begin_verse\n\n"))))
@@ -33,7 +33,7 @@
(should (string= (with-temp-buffer
(insert "> Dear Sir, your astonishment’s odd;\n")
(select-region)
(begin-end-quote)
(my/begin-end-quote)
(buffer-string))
"#+begin_quote\n Dear Sir, your astonishment’s odd;\n#+end_quote\n")))
@@ -42,7 +42,7 @@
(should (string= (with-temp-buffer
(insert "> Dear Sir, your astonishment’s odd; <br/>\n")
(select-region)
(begin-end-verse)
(my/begin-end-verse)
(buffer-string))
"#+begin_verse\n Dear Sir, your astonishment’s odd;\n#+end_verse\n")))
@@ -51,7 +51,7 @@
(should (string= (with-temp-buffer
(insert "> Dear Sir, your astonishment’s odd; <br/>\n")
(select-region)
(begin-end-verse)
(my/begin-end-verse)
(buffer-string))
"#+begin_verse\n Dear Sir, your astonishment’s odd;\n#+end_verse\n")))
@@ -60,7 +60,7 @@
(should (string= (with-temp-buffer
(insert "> Dear Sir, your astonishment’s odd;<br/>\n")
(select-region)
(begin-end-verse)
(my/begin-end-verse)
(buffer-string))
"#+begin_verse\n Dear Sir, your astonishment’s odd;\n#+end_verse\n")))
@@ -69,7 +69,7 @@
(should (string= (with-temp-buffer
(insert " talsernilli barenya-tar falon enyi\n")
(select-region)
(begin-end-quote)
(my/begin-end-quote)
(buffer-string))
"#+begin_quote\n talsernilli barenya-tar falon enyi\n#+end_quote\n")))
@@ -78,7 +78,7 @@
(should (string= (with-temp-buffer
(insert " i talessen\n talalor ar-entai kirenyi\n basleth solth sares\n")
(select-region)
(begin-end-verse)
(my/begin-end-verse)
(buffer-string))
"#+begin_verse\n i talessen\n talalor ar-entai kirenyi\n basleth solth sares\n#+end_verse\n")))
@@ -87,7 +87,7 @@
(should (string= (with-temp-buffer
(insert " count > 42\n")
(select-region)
(begin-end-quote)
(my/begin-end-quote)
(buffer-string))
"#+begin_quote\n count > 42\n#+end_quote\n")))
@@ -96,7 +96,7 @@
(should (string= (with-temp-buffer
(insert " count > 42<br/>\n")
(select-region)
(begin-end-verse)
(my/begin-end-verse)
(buffer-string))
"#+begin_verse\n count > 42\n#+end_verse\n")))

0 comments on commit b4ddf92

Please sign in to comment.