diff --git a/pangu-spacing.el b/pangu-spacing.el index e13a632..391be74 100644 --- a/pangu-spacing.el +++ b/pangu-spacing.el @@ -222,10 +222,33 @@ pangu-sapce-mode." `(pangu-spacing-search-buffer ,regexp ,beg ,end (,func (match-beginning 1) (match-end 1)))) +(defun pangu-spacing-org-mode-at-special-region () + (interactive) + (let ((element (org-element-at-point))) + (when (or (member (org-element-type element) + '(src-block keyword example-block export-block + latex-environment planning)) + (member (car (org-element-context element)) + '(inline-src-block timestamp link code verbatim))) + t))) + +(defcustom pangu-spacing-special-region-func-alist + '((org-mode . pangu-spacing-org-mode-at-special-region)) + "Alist mapping major-mode to the corresponding function to + check for special region that shall not write real pangu-space" + :group 'pangu-spacing + :type '(alist :key-type (symbol) + :value-type (function))) + (defun pangu-spacing-search-and-replace (match regexp) "Replace regexp with match in buffer." - (pangu-spacing-search-buffer regexp (point-min) (point-max) - (replace-match match nil nil))) + (let ((at-special-region-func + (cdr (assq major-mode pangu-spacing-special-region-func-alist)))) + (pangu-spacing-search-buffer + regexp (point-min) (point-max) + (unless (and at-special-region-func + (save-match-data (funcall at-special-region-func))) + (replace-match match nil nil))))) (defun pangu-spacing-overlay-p (ov) "Determine whether overlay OV was created by space-between." diff --git a/test/pangu-spacing-test.el b/test/pangu-spacing-test.el index 0d9c8a0..baa067f 100644 --- a/test/pangu-spacing-test.el +++ b/test/pangu-spacing-test.el @@ -21,6 +21,40 @@ ) ) +(ert-deftest pangu-spacing-test/org-mode-special-region () + (with-temp-buffer + (insert "* English中文English [[https://github.com/][link中文link]] + CLOSED: [2018-10-14日21:42] + - State \"DONE\" from \"TODO\" [2018-10-14日21:42] + [2018-09-17一] + English中文English +* ~English中文English~ + <2018-09-17一> + =/home/me/English中文English.conf= + #+BEGIN_SRC conf :tangle English中文English.conf + English中文English + #+END_SRC") + (message "1") + (pangu-spacing-mode 1) + (message "2") + (org-mode) + (message "3") + (let ((pangu-spacing-real-insert-separtor t)) + (pangu-spacing-modify-buffer)) + (message "4") + (should (string-equal (buffer-string) + "* English 中文 English [[https://github.com/][link中文link]] + CLOSED: [2018-10-14日21:42] + - State \"DONE\" from \"TODO\" [2018-10-14日21:42] + [2018-09-17一] + English 中文 English +* ~English中文English~ + <2018-09-17一> + =/home/me/English中文English.conf= + #+BEGIN_SRC conf :tangle English中文English.conf + English中文English + #+END_SRC")))) + (ert-deftest pangu-spacing-test/show () "Test if showing works" (with-temp-buffer