aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/org/org-src.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/org/org-src.el')
-rw-r--r--lisp/org/org-src.el96
1 files changed, 49 insertions, 47 deletions
diff --git a/lisp/org/org-src.el b/lisp/org/org-src.el
index 6dbf6d67fc5..501d30ab1d7 100644
--- a/lisp/org/org-src.el
+++ b/lisp/org/org-src.el
@@ -39,6 +39,8 @@
39 39
40(declare-function org-do-remove-indentation "org" (&optional n)) 40(declare-function org-do-remove-indentation "org" (&optional n))
41(declare-function org-at-table.el-p "org" ()) 41(declare-function org-at-table.el-p "org" ())
42(declare-function org-in-src-block-p "org" (&optional inside))
43(declare-function org-in-block-p "org" (names))
42(declare-function org-get-indentation "org" (&optional line)) 44(declare-function org-get-indentation "org" (&optional line))
43(declare-function org-switch-to-buffer-other-window "org" (&rest args)) 45(declare-function org-switch-to-buffer-other-window "org" (&rest args))
44(declare-function org-pop-to-buffer-same-window 46(declare-function org-pop-to-buffer-same-window
@@ -201,41 +203,40 @@ There is a mode hook, and keybindings for `org-edit-src-exit' and
201`org-edit-src-save'") 203`org-edit-src-save'")
202 204
203(defun org-edit-src-code (&optional context code edit-buffer-name) 205(defun org-edit-src-code (&optional context code edit-buffer-name)
204 "Edit the source CODE example at point. 206 "Edit the source CODE block at point.
205The example is copied to a separate buffer, and that buffer is 207The code is copied to a separate buffer and the appropriate mode
206switched to the correct language mode. When done, exit with 208is turned on. When done, exit with \\[org-edit-src-exit]. This will
207\\[org-edit-src-exit]. This will remove the original code in the 209remove the original code in the Org buffer, and replace it with the
208Org buffer, and replace it with the edited version. An optional 210edited version. An optional argument CONTEXT is used by \\[org-edit-src-save]
209argument CONTEXT is used by \\[org-edit-src-save] when calling 211when calling this function. See `org-src-window-setup' to configure
210this function. See `org-src-window-setup' to configure the 212the display of windows containing the Org buffer and the code buffer."
211display of windows containing the Org buffer and the code
212buffer."
213 (interactive) 213 (interactive)
214 (unless (eq context 'save) 214 (if (not (or (org-in-block-p '("src" "example" "latex" "html"))
215 (setq org-edit-src-saved-temp-window-config (current-window-configuration))) 215 (org-at-table.el-p)))
216 (let* ((mark (and (org-region-active-p) (mark))) 216 (user-error "Not in a source code or example block")
217 (case-fold-search t) 217 (unless (eq context 'save)
218 (info 218 (setq org-edit-src-saved-temp-window-config (current-window-configuration)))
219 ;; If the src region consists in no lines, we insert a blank 219 (let* ((mark (and (org-region-active-p) (mark)))
220 ;; line. 220 (case-fold-search t)
221 (let* ((temp (org-edit-src-find-region-and-lang)) 221 (info
222 (beg (nth 0 temp)) 222 ;; If the src region consists in no lines, we insert a blank
223 (end (nth 1 temp))) 223 ;; line.
224 (if (>= end beg) temp 224 (let* ((temp (org-edit-src-find-region-and-lang))
225 (goto-char beg) 225 (beg (nth 0 temp))
226 (insert "\n") 226 (end (nth 1 temp)))
227 (org-edit-src-find-region-and-lang)))) 227 (if (>= end beg) temp
228 (full-info (org-babel-get-src-block-info 'light)) 228 (goto-char beg)
229 (org-mode-p (derived-mode-p 'org-mode)) ;; derived-mode-p is reflexive 229 (insert "\n")
230 (beg (make-marker)) 230 (org-edit-src-find-region-and-lang))))
231 ;; Move marker with inserted text for case when src block is 231 (full-info (org-babel-get-src-block-info 'light))
232 ;; just one empty line, i.e. beg == end. 232 (org-mode-p (derived-mode-p 'org-mode)) ;; derived-mode-p is reflexive
233 (end (copy-marker nil t)) 233 (beg (make-marker))
234 (allow-write-back-p (null code)) 234 ;; Move marker with inserted text for case when src block is
235 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg 235 ;; just one empty line, i.e. beg == end.
236 begline markline markcol line col transmitted-variables) 236 (end (copy-marker (make-marker) t))
237 (if (not info) 237 (allow-write-back-p (null code))
238 nil 238 block-nindent total-nindent ovl lang lang-f single lfmt buffer msg
239 begline markline markcol line col transmitted-variables)
239 (setq beg (move-marker beg (nth 0 info)) 240 (setq beg (move-marker beg (nth 0 info))
240 end (move-marker end (nth 1 info)) 241 end (move-marker end (nth 1 info))
241 msg (if allow-write-back-p 242 msg (if allow-write-back-p
@@ -244,7 +245,7 @@ buffer."
244 "Exit with C-c ' (C-c and single quote)") 245 "Exit with C-c ' (C-c and single quote)")
245 code (or code (buffer-substring-no-properties beg end)) 246 code (or code (buffer-substring-no-properties beg end))
246 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes)) 247 lang (or (cdr (assoc (nth 2 info) org-src-lang-modes))
247 (nth 2 info)) 248 (nth 2 info))
248 lang (if (symbolp lang) (symbol-name lang) lang) 249 lang (if (symbolp lang) (symbol-name lang) lang)
249 single (nth 3 info) 250 single (nth 3 info)
250 block-nindent (nth 5 info) 251 block-nindent (nth 5 info)
@@ -527,6 +528,14 @@ the language, a switch telling if the content should be in a single line."
527 (pos (point)) 528 (pos (point))
528 re1 re2 single beg end lang lfmt match-re1 ind entry) 529 re1 re2 single beg end lang lfmt match-re1 ind entry)
529 (catch 'exit 530 (catch 'exit
531 (when (org-at-table.el-p)
532 (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
533 (setq beg (1+ (point-at-eol)))
534 (goto-char beg)
535 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
536 (progn (goto-char (point-max)) (newline)))
537 (setq end (1- (point-at-bol)))
538 (throw 'exit (list beg end 'table.el nil nil 0)))
530 (while (setq entry (pop re-list)) 539 (while (setq entry (pop re-list))
531 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry) 540 (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
532 single (nth 3 entry)) 541 single (nth 3 entry))
@@ -557,16 +566,7 @@ the language, a switch telling if the content should be in a single line."
557 (throw 'exit 566 (throw 'exit
558 (list (match-end 0) end 567 (list (match-end 0) end
559 (org-edit-src-get-lang lang) 568 (org-edit-src-get-lang lang)
560 single lfmt ind))))))))) 569 single lfmt ind))))))))))))
561 (when (org-at-table.el-p)
562 (re-search-backward "^[\t]*[^ \t|\\+]" nil t)
563 (setq beg (1+ (point-at-eol)))
564 (goto-char beg)
565 (or (re-search-forward "^[\t]*[^ \t|\\+]" nil t)
566 (progn (goto-char (point-max)) (newline)))
567 (setq end (point-at-bol))
568 (setq ind (org-edit-src-get-indentation beg))
569 (throw 'exit (list beg end 'table.el nil nil ind))))))
570 570
571(defun org-edit-src-get-lang (lang) 571(defun org-edit-src-get-lang (lang)
572 "Extract the src language." 572 "Extract the src language."
@@ -633,7 +633,8 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
633 (unless (org-bound-and-true-p org-edit-src-from-org-mode) 633 (unless (org-bound-and-true-p org-edit-src-from-org-mode)
634 (error "This is not a sub-editing buffer, something is wrong")) 634 (error "This is not a sub-editing buffer, something is wrong"))
635 (widen) 635 (widen)
636 (let* ((beg org-edit-src-beg-marker) 636 (let* ((fixed-width-p (string-match "Fixed Width" (buffer-name)))
637 (beg org-edit-src-beg-marker)
637 (end org-edit-src-end-marker) 638 (end org-edit-src-end-marker)
638 (ovl org-edit-src-overlay) 639 (ovl org-edit-src-overlay)
639 (bufstr (buffer-string)) 640 (bufstr (buffer-string))
@@ -670,7 +671,8 @@ with \",*\", \",#+\", \",,*\" and \",,#+\"."
670 (goto-char (point-max)) (insert "\\n"))) 671 (goto-char (point-max)) (insert "\\n")))
671 (goto-char (point-min)) 672 (goto-char (point-min))
672 (if (looking-at "\\s-*") (replace-match " "))) 673 (if (looking-at "\\s-*") (replace-match " ")))
673 (when (org-bound-and-true-p org-edit-src-from-org-mode) 674 (when (and (org-bound-and-true-p org-edit-src-from-org-mode)
675 (not fixed-width-p))
674 (org-escape-code-in-region (point-min) (point-max)) 676 (org-escape-code-in-region (point-min) (point-max))
675 (setq delta (+ delta 677 (setq delta (+ delta
676 (save-excursion 678 (save-excursion