Permalink
Browse files

Refactoring begin/end: remove duplication selected-region / no-select…

…ed-region
  • Loading branch information...
smiller committed Feb 7, 2015
1 parent b4ddf92 commit 3548ff8cfa6918362c21f9b85b3ed87d3ef20968
Showing with 24 additions and 51 deletions.
  1. +2 −2 mode-hooks.el
  2. +21 −48 mods-org.el
  3. +1 −1 test/mods-org-test.el
View
@@ -3,8 +3,8 @@
(linum-mode -1)
(auto-fill-mode 1)
(set-input-method "TeX")
(define-key org-mode-map (kbd "M-s M-q") 'begin-end-quote)
(define-key org-mode-map (kbd "M-s M-v") 'begin-end-verse)
(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)
)
(add-hook 'text-mode-hook 'my-text-mode-hook)
View
@@ -28,57 +28,30 @@
(define-key global-map "\C-ca" 'org-agenda)
(defun my/begin-end-quote ()
(my/begin-end "quote")
)
(interactive)
(my/begin-end "quote"))
(defun my/begin-end-verse ()
(my/begin-end "verse")
)
(defun my/begin-end (variant)
(interactive)
(if (use-region-p)
(my/begin-end-selected-region variant)
(my/begin-end-no-selected-region variant)
)
)
(my/begin-end "verse"))
(defun my/begin-end-selected-region (variant)
(defun my/begin-end (variant)
(interactive)
(my/fix-old-formatting)
(insert "#+end_" variant)
(newline)
(goto-char (region-beginning))
(insert "#+begin_" variant)
(newline)
)
(defun my/begin-end-no-selected-region (variant)
(let ((cited-string "\n"))
(when (use-region-p)
(setq cited-string
(my/fix-formatting (buffer-substring-no-properties (region-beginning) (region-end))))
(delete-region (region-beginning) (region-end)))
(insert "#+begin_" variant "\n"
cited-string
"#+end_" variant "\n"))
(unless (use-region-p)
(forward-line -2)))
(defun my/fix-formatting (str)
(interactive)
(insert "#+begin_" variant)
(newline)
(newline)
(insert "#+end_" variant)
(newline)
(previous-line)
(previous-line)
)
(defun my/fix-old-formatting ()
(my/remove-old-formatting-code)
(my/indent-if-not-indented)
)
(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 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)
)
)
(if (string= (substring str 0 2) "> ")
(replace-regexp-in-string "^> " " "
(replace-regexp-in-string "\s*<br/>" ""
(replace-regexp-in-string "\n!$" "\n " str)))
str))
View
@@ -94,7 +94,7 @@
(ert-deftest test-begin-end-verse-region-newer-styling-with-medial-gt ()
"Tests begin-end-verse with selected region, new styling, with > in line"
(should (string= (with-temp-buffer
(insert " count > 42<br/>\n")
(insert " count > 42\n")
(select-region)
(my/begin-end-verse)
(buffer-string))

0 comments on commit 3548ff8

Please sign in to comment.