diff options
author | Bastien Guerry <bzg@gnu.org> | 2012-10-26 16:42:05 +0200 |
---|---|---|
committer | Bastien Guerry <bzg@gnu.org> | 2012-10-26 16:42:05 +0200 |
commit | bdebdb64b35d6a0a28e04335f4e8e46949206ad3 (patch) | |
tree | ea3e0d00bd8da93ae89e86744a80f4e162c5361c /lisp/org/org-src.el | |
parent | ccc83f50a4ca55842bcf955b9f8156c17aa43864 (diff) | |
download | emacs-bdebdb64b35d6a0a28e04335f4e8e46949206ad3.tar.gz emacs-bdebdb64b35d6a0a28e04335f4e8e46949206ad3.tar.bz2 emacs-bdebdb64b35d6a0a28e04335f4e8e46949206ad3.zip |
Merge upstream Org (from commit acbbe2)
Diffstat (limited to 'lisp/org/org-src.el')
-rw-r--r-- | lisp/org/org-src.el | 70 |
1 files changed, 43 insertions, 27 deletions
diff --git a/lisp/org/org-src.el b/lisp/org/org-src.el index 9d6bc1aa2c8..b4d4c0489a9 100644 --- a/lisp/org/org-src.el +++ b/lisp/org/org-src.el @@ -41,10 +41,8 @@ (declare-function org-at-table.el-p "org" ()) (declare-function org-get-indentation "org" (&optional line)) (declare-function org-switch-to-buffer-other-window "org" (&rest args)) -(declare-function org-strip-protective-commas "org" (beg end)) (declare-function org-pop-to-buffer-same-window "org-compat" (&optional buffer-or-name norecord label)) -(declare-function org-strip-protective-commas "org" (beg end)) (declare-function org-base-buffer "org" (buffer)) (defcustom org-edit-src-region-extra nil @@ -311,13 +309,8 @@ buffer." (error "Language mode `%s' fails with: %S" lang-f (nth 1 e))))) (dolist (pair transmitted-variables) (org-set-local (car pair) (cadr pair))) - (if (derived-mode-p 'org-mode) - (progn - (goto-char (point-min)) - (while (re-search-forward "^," nil t) - (if (eq (org-current-line) line) (setq total-nindent (1+ total-nindent))) - (replace-match ""))) - (org-strip-protective-commas (point-min) (point-max))) + ;; Remove protecting commas from visible part of buffer. + (org-unescape-code-in-region (point-min) (point-max)) (when markline (org-goto-line (1+ (- markline begline))) (org-move-to-column @@ -590,20 +583,38 @@ the language, a switch telling if the content should be in a single line." (goto-char pos) (org-get-indentation))) -(defun org-add-protective-commas (beg end &optional line) - "Add protective commas in region. -Return the delta in size of the region." +(defun org-escape-code-in-region (beg end) + "Escape lines between BEG and END. +Escaping happens when a line starts with \"*\", \"#+\", \",*\" or +\",#+\" by appending a comma to it." (interactive "r") - (let ((org-re "^\\(.\\)") - (other-re "^\\([*]\\|[ \t]*#\\+\\)") - (delta 0)) - (save-excursion - (goto-char beg) - (while (re-search-forward (if (derived-mode-p 'org-mode) org-re other-re) - end t) - (if (and line (eq (org-current-line) line)) (setq delta (1+ delta))) - (replace-match ",\\1"))) - delta)) + (save-excursion + (goto-char beg) + (while (re-search-forward "^[ \t]*,?\\(\\*\\|#\\+\\)" end t) + (replace-match ",\\1" nil nil nil 1)))) + +(defun org-escape-code-in-string (s) + "Escape lines in string S. +Escaping happens when a line starts with \"*\", \"#+\", \",*\" or +\",#+\" by appending a comma to it." + (replace-regexp-in-string "^[ \t]*,?\\(\\*\\|#\\+\\)" ",\\1" s nil nil 1)) + +(defun org-unescape-code-in-region (beg end) + "Un-escape lines between BEG and END. +Un-escaping happens by removing the first comma on lines starting +with \",*\", \",#+\", \",,*\" and \",,#+\"." + (interactive "r") + (save-excursion + (goto-char beg) + (while (re-search-forward "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" end t) + (replace-match "" nil nil nil 1)))) + +(defun org-unescape-code-in-string (s) + "Un-escape lines in string S. +Un-escaping happens by removing the first comma on lines starting +with \",*\", \",#+\", \",,*\" and \",,#+\"." + (replace-regexp-in-string + "^[ \t]*,?\\(,\\)\\(?:\\*\\|#\\+\\)" "" s nil nil 1)) (defun org-edit-src-exit (&optional context) "Exit special edit and protect problematic lines." @@ -649,8 +660,12 @@ Return the delta in size of the region." (goto-char (point-min)) (if (looking-at "\\s-*") (replace-match " "))) (when (org-bound-and-true-p org-edit-src-from-org-mode) - (setq delta (+ delta (org-add-protective-commas - (point-min) (point-max) line)))) + (org-escape-code-in-region (point-min) (point-max)) + (setq delta (+ delta + (save-excursion + (org-goto-line line) + (if (looking-at "[ \t]*\\(,,\\)?\\(\\*\\|#+\\)") 1 + 0))))) (when (org-bound-and-true-p org-edit-src-picture) (setq preserve-indentation nil) (untabify (point-min) (point-max)) @@ -674,9 +689,10 @@ Return the delta in size of the region." (kill-buffer buffer)) (goto-char beg) (when allow-write-back-p - (delete-region beg (1- end)) - (insert code) - (delete-char 1) + (delete-region beg (max beg (1- end))) + (unless (string-match "^[ \t]*$" code) + (insert code) + (delete-char 1)) (goto-char beg) (if single (just-one-space))) (if (memq t (mapcar (lambda (overlay) |